2 * Device tree based initialization code for reserved memory.
4 * Copyright (c) 2013, 2015 The Linux Foundation. All Rights Reserved.
5 * Copyright (c) 2013,2014 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com
7 * Author: Marek Szyprowski <m.szyprowski@samsung.com>
8 * Author: Josh Cartwright <joshc@codeaurora.org>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License or (at your optional) any later version of the license.
16 #define pr_fmt(fmt) "OF: reserved mem: " fmt
18 #include <linux/err.h>
20 #include <linux/of_fdt.h>
21 #include <linux/of_platform.h>
23 #include <linux/sizes.h>
24 #include <linux/of_reserved_mem.h>
25 #include <linux/sort.h>
26 #include <linux/slab.h>
28 #define MAX_RESERVED_REGIONS 32
29 static struct reserved_mem reserved_mem
[MAX_RESERVED_REGIONS
];
30 static int reserved_mem_count
;
32 #if defined(CONFIG_HAVE_MEMBLOCK)
33 #include <linux/memblock.h>
34 int __init __weak
early_init_dt_alloc_reserved_memory_arch(phys_addr_t size
,
35 phys_addr_t align
, phys_addr_t start
, phys_addr_t end
, bool nomap
,
36 phys_addr_t
*res_base
)
40 * We use __memblock_alloc_base() because memblock_alloc_base()
41 * panic()s on allocation failure.
43 end
= !end
? MEMBLOCK_ALLOC_ANYWHERE
: end
;
44 base
= __memblock_alloc_base(size
, align
, end
);
49 * Check if the allocated region fits in to start..end window
52 memblock_free(base
, size
);
58 return memblock_remove(base
, size
);
62 int __init __weak
early_init_dt_alloc_reserved_memory_arch(phys_addr_t size
,
63 phys_addr_t align
, phys_addr_t start
, phys_addr_t end
, bool nomap
,
64 phys_addr_t
*res_base
)
66 pr_err("Reserved memory not supported, ignoring region 0x%llx%s\n",
67 size
, nomap
? " (nomap)" : "");
73 * res_mem_save_node() - save fdt node for second pass initialization
75 void __init
fdt_reserved_mem_save_node(unsigned long node
, const char *uname
,
76 phys_addr_t base
, phys_addr_t size
)
78 struct reserved_mem
*rmem
= &reserved_mem
[reserved_mem_count
];
80 if (reserved_mem_count
== ARRAY_SIZE(reserved_mem
)) {
81 pr_err("not enough space all defined regions.\n");
85 rmem
->fdt_node
= node
;
95 * res_mem_alloc_size() - allocate reserved memory described by 'size', 'align'
96 * and 'alloc-ranges' properties
98 static int __init
__reserved_mem_alloc_size(unsigned long node
,
99 const char *uname
, phys_addr_t
*res_base
, phys_addr_t
*res_size
)
101 int t_len
= (dt_root_addr_cells
+ dt_root_size_cells
) * sizeof(__be32
);
102 phys_addr_t start
= 0, end
= 0;
103 phys_addr_t base
= 0, align
= 0, size
;
109 prop
= of_get_flat_dt_prop(node
, "size", &len
);
113 if (len
!= dt_root_size_cells
* sizeof(__be32
)) {
114 pr_err("invalid size property in '%s' node.\n", uname
);
117 size
= dt_mem_next_cell(dt_root_size_cells
, &prop
);
119 nomap
= of_get_flat_dt_prop(node
, "no-map", NULL
) != NULL
;
121 prop
= of_get_flat_dt_prop(node
, "alignment", &len
);
123 if (len
!= dt_root_addr_cells
* sizeof(__be32
)) {
124 pr_err("invalid alignment property in '%s' node.\n",
128 align
= dt_mem_next_cell(dt_root_addr_cells
, &prop
);
131 /* Need adjust the alignment to satisfy the CMA requirement */
132 if (IS_ENABLED(CONFIG_CMA
)
133 && of_flat_dt_is_compatible(node
, "shared-dma-pool")
134 && of_get_flat_dt_prop(node
, "reusable", NULL
)
135 && !of_get_flat_dt_prop(node
, "no-map", NULL
)) {
136 unsigned long order
=
137 max_t(unsigned long, MAX_ORDER
- 1, pageblock_order
);
139 align
= max(align
, (phys_addr_t
)PAGE_SIZE
<< order
);
142 prop
= of_get_flat_dt_prop(node
, "alloc-ranges", &len
);
145 if (len
% t_len
!= 0) {
146 pr_err("invalid alloc-ranges property in '%s', skipping node.\n",
154 start
= dt_mem_next_cell(dt_root_addr_cells
, &prop
);
155 end
= start
+ dt_mem_next_cell(dt_root_size_cells
,
158 ret
= early_init_dt_alloc_reserved_memory_arch(size
,
159 align
, start
, end
, nomap
, &base
);
161 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
163 (unsigned long)size
/ SZ_1M
);
170 ret
= early_init_dt_alloc_reserved_memory_arch(size
, align
,
173 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
174 uname
, &base
, (unsigned long)size
/ SZ_1M
);
178 pr_info("failed to allocate memory for node '%s'\n", uname
);
188 static const struct of_device_id __rmem_of_table_sentinel
189 __used
__section(__reservedmem_of_table_end
);
192 * res_mem_init_node() - call region specific reserved memory init code
194 static int __init
__reserved_mem_init_node(struct reserved_mem
*rmem
)
196 extern const struct of_device_id __reservedmem_of_table
[];
197 const struct of_device_id
*i
;
199 for (i
= __reservedmem_of_table
; i
< &__rmem_of_table_sentinel
; i
++) {
200 reservedmem_of_init_fn initfn
= i
->data
;
201 const char *compat
= i
->compatible
;
203 if (!of_flat_dt_is_compatible(rmem
->fdt_node
, compat
))
206 if (initfn(rmem
) == 0) {
207 pr_info("initialized node %s, compatible id %s\n",
215 static int __init
__rmem_cmp(const void *a
, const void *b
)
217 const struct reserved_mem
*ra
= a
, *rb
= b
;
219 if (ra
->base
< rb
->base
)
222 if (ra
->base
> rb
->base
)
228 static void __init
__rmem_check_for_overlap(void)
232 if (reserved_mem_count
< 2)
235 sort(reserved_mem
, reserved_mem_count
, sizeof(reserved_mem
[0]),
237 for (i
= 0; i
< reserved_mem_count
- 1; i
++) {
238 struct reserved_mem
*this, *next
;
240 this = &reserved_mem
[i
];
241 next
= &reserved_mem
[i
+ 1];
242 if (!(this->base
&& next
->base
))
244 if (this->base
+ this->size
> next
->base
) {
245 phys_addr_t this_end
, next_end
;
247 this_end
= this->base
+ this->size
;
248 next_end
= next
->base
+ next
->size
;
249 pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
250 this->name
, &this->base
, &this_end
,
251 next
->name
, &next
->base
, &next_end
);
257 * fdt_init_reserved_mem - allocate and init all saved reserved memory regions
259 void __init
fdt_init_reserved_mem(void)
263 /* check for overlapping reserved regions */
264 __rmem_check_for_overlap();
266 for (i
= 0; i
< reserved_mem_count
; i
++) {
267 struct reserved_mem
*rmem
= &reserved_mem
[i
];
268 unsigned long node
= rmem
->fdt_node
;
273 prop
= of_get_flat_dt_prop(node
, "phandle", &len
);
275 prop
= of_get_flat_dt_prop(node
, "linux,phandle", &len
);
277 rmem
->phandle
= of_read_number(prop
, len
/4);
280 err
= __reserved_mem_alloc_size(node
, rmem
->name
,
281 &rmem
->base
, &rmem
->size
);
283 __reserved_mem_init_node(rmem
);
287 static inline struct reserved_mem
*__find_rmem(struct device_node
*node
)
294 for (i
= 0; i
< reserved_mem_count
; i
++)
295 if (reserved_mem
[i
].phandle
== node
->phandle
)
296 return &reserved_mem
[i
];
300 struct rmem_assigned_device
{
302 struct reserved_mem
*rmem
;
303 struct list_head list
;
306 static LIST_HEAD(of_rmem_assigned_device_list
);
307 static DEFINE_MUTEX(of_rmem_assigned_device_mutex
);
310 * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
312 * @dev: Pointer to the device to configure
313 * @np: Pointer to the device_node with 'reserved-memory' property
314 * @idx: Index of selected region
316 * This function assigns respective DMA-mapping operations based on reserved
317 * memory region specified by 'memory-region' property in @np node to the @dev
318 * device. When driver needs to use more than one reserved memory region, it
319 * should allocate child devices and initialize regions by name for each of
322 * Returns error code or zero on success.
324 int of_reserved_mem_device_init_by_idx(struct device
*dev
,
325 struct device_node
*np
, int idx
)
327 struct rmem_assigned_device
*rd
;
328 struct device_node
*target
;
329 struct reserved_mem
*rmem
;
335 target
= of_parse_phandle(np
, "memory-region", idx
);
339 rmem
= __find_rmem(target
);
342 if (!rmem
|| !rmem
->ops
|| !rmem
->ops
->device_init
)
345 rd
= kmalloc(sizeof(struct rmem_assigned_device
), GFP_KERNEL
);
349 ret
= rmem
->ops
->device_init(rmem
, dev
);
354 mutex_lock(&of_rmem_assigned_device_mutex
);
355 list_add(&rd
->list
, &of_rmem_assigned_device_list
);
356 mutex_unlock(&of_rmem_assigned_device_mutex
);
357 /* ensure that dma_ops is set for virtual devices
358 * using reserved memory
360 of_dma_configure(dev
, np
);
362 dev_info(dev
, "assigned reserved memory node %s\n", rmem
->name
);
369 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx
);
372 * of_reserved_mem_device_release() - release reserved memory device structures
373 * @dev: Pointer to the device to deconfigure
375 * This function releases structures allocated for memory region handling for
378 void of_reserved_mem_device_release(struct device
*dev
)
380 struct rmem_assigned_device
*rd
;
381 struct reserved_mem
*rmem
= NULL
;
383 mutex_lock(&of_rmem_assigned_device_mutex
);
384 list_for_each_entry(rd
, &of_rmem_assigned_device_list
, list
) {
385 if (rd
->dev
== dev
) {
392 mutex_unlock(&of_rmem_assigned_device_mutex
);
394 if (!rmem
|| !rmem
->ops
|| !rmem
->ops
->device_release
)
397 rmem
->ops
->device_release(rmem
, dev
);
399 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release
);
402 * of_reserved_mem_lookup() - acquire reserved_mem from a device node
403 * @np: node pointer of the desired reserved-memory region
405 * This function allows drivers to acquire a reference to the reserved_mem
406 * struct based on a device node handle.
408 * Returns a reserved_mem reference, or NULL on error.
410 struct reserved_mem
*of_reserved_mem_lookup(struct device_node
*np
)
418 name
= kbasename(np
->full_name
);
419 for (i
= 0; i
< reserved_mem_count
; i
++)
420 if (!strcmp(reserved_mem
[i
].name
, name
))
421 return &reserved_mem
[i
];
425 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup
);