1 // SPDX-License-Identifier: GPL-2.0+
3 * Device tree based initialization code for reserved memory.
5 * Copyright (c) 2013, 2015 The Linux Foundation. All Rights Reserved.
6 * Copyright (c) 2013,2014 Samsung Electronics Co., Ltd.
7 * http://www.samsung.com
8 * Author: Marek Szyprowski <m.szyprowski@samsung.com>
9 * Author: Josh Cartwright <joshc@codeaurora.org>
12 #define pr_fmt(fmt) "OF: reserved mem: " fmt
14 #include <linux/err.h>
16 #include <linux/of_fdt.h>
17 #include <linux/of_platform.h>
19 #include <linux/sizes.h>
20 #include <linux/of_reserved_mem.h>
21 #include <linux/sort.h>
22 #include <linux/slab.h>
24 #define MAX_RESERVED_REGIONS 32
25 static struct reserved_mem reserved_mem
[MAX_RESERVED_REGIONS
];
26 static int reserved_mem_count
;
28 #if defined(CONFIG_HAVE_MEMBLOCK)
29 #include <linux/memblock.h>
30 int __init __weak
early_init_dt_alloc_reserved_memory_arch(phys_addr_t size
,
31 phys_addr_t align
, phys_addr_t start
, phys_addr_t end
, bool nomap
,
32 phys_addr_t
*res_base
)
36 * We use __memblock_alloc_base() because memblock_alloc_base()
37 * panic()s on allocation failure.
39 end
= !end
? MEMBLOCK_ALLOC_ANYWHERE
: end
;
40 base
= __memblock_alloc_base(size
, align
, end
);
45 * Check if the allocated region fits in to start..end window
48 memblock_free(base
, size
);
54 return memblock_remove(base
, size
);
58 int __init __weak
early_init_dt_alloc_reserved_memory_arch(phys_addr_t size
,
59 phys_addr_t align
, phys_addr_t start
, phys_addr_t end
, bool nomap
,
60 phys_addr_t
*res_base
)
62 pr_err("Reserved memory not supported, ignoring region 0x%llx%s\n",
63 size
, nomap
? " (nomap)" : "");
69 * res_mem_save_node() - save fdt node for second pass initialization
71 void __init
fdt_reserved_mem_save_node(unsigned long node
, const char *uname
,
72 phys_addr_t base
, phys_addr_t size
)
74 struct reserved_mem
*rmem
= &reserved_mem
[reserved_mem_count
];
76 if (reserved_mem_count
== ARRAY_SIZE(reserved_mem
)) {
77 pr_err("not enough space all defined regions.\n");
81 rmem
->fdt_node
= node
;
91 * res_mem_alloc_size() - allocate reserved memory described by 'size', 'align'
92 * and 'alloc-ranges' properties
94 static int __init
__reserved_mem_alloc_size(unsigned long node
,
95 const char *uname
, phys_addr_t
*res_base
, phys_addr_t
*res_size
)
97 int t_len
= (dt_root_addr_cells
+ dt_root_size_cells
) * sizeof(__be32
);
98 phys_addr_t start
= 0, end
= 0;
99 phys_addr_t base
= 0, align
= 0, size
;
105 prop
= of_get_flat_dt_prop(node
, "size", &len
);
109 if (len
!= dt_root_size_cells
* sizeof(__be32
)) {
110 pr_err("invalid size property in '%s' node.\n", uname
);
113 size
= dt_mem_next_cell(dt_root_size_cells
, &prop
);
115 nomap
= of_get_flat_dt_prop(node
, "no-map", NULL
) != NULL
;
117 prop
= of_get_flat_dt_prop(node
, "alignment", &len
);
119 if (len
!= dt_root_addr_cells
* sizeof(__be32
)) {
120 pr_err("invalid alignment property in '%s' node.\n",
124 align
= dt_mem_next_cell(dt_root_addr_cells
, &prop
);
127 /* Need adjust the alignment to satisfy the CMA requirement */
128 if (IS_ENABLED(CONFIG_CMA
)
129 && of_flat_dt_is_compatible(node
, "shared-dma-pool")
130 && of_get_flat_dt_prop(node
, "reusable", NULL
)
131 && !of_get_flat_dt_prop(node
, "no-map", NULL
)) {
132 unsigned long order
=
133 max_t(unsigned long, MAX_ORDER
- 1, pageblock_order
);
135 align
= max(align
, (phys_addr_t
)PAGE_SIZE
<< order
);
138 prop
= of_get_flat_dt_prop(node
, "alloc-ranges", &len
);
141 if (len
% t_len
!= 0) {
142 pr_err("invalid alloc-ranges property in '%s', skipping node.\n",
150 start
= dt_mem_next_cell(dt_root_addr_cells
, &prop
);
151 end
= start
+ dt_mem_next_cell(dt_root_size_cells
,
154 ret
= early_init_dt_alloc_reserved_memory_arch(size
,
155 align
, start
, end
, nomap
, &base
);
157 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
159 (unsigned long)size
/ SZ_1M
);
166 ret
= early_init_dt_alloc_reserved_memory_arch(size
, align
,
169 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
170 uname
, &base
, (unsigned long)size
/ SZ_1M
);
174 pr_info("failed to allocate memory for node '%s'\n", uname
);
184 static const struct of_device_id __rmem_of_table_sentinel
185 __used
__section(__reservedmem_of_table_end
);
188 * res_mem_init_node() - call region specific reserved memory init code
190 static int __init
__reserved_mem_init_node(struct reserved_mem
*rmem
)
192 extern const struct of_device_id __reservedmem_of_table
[];
193 const struct of_device_id
*i
;
195 for (i
= __reservedmem_of_table
; i
< &__rmem_of_table_sentinel
; i
++) {
196 reservedmem_of_init_fn initfn
= i
->data
;
197 const char *compat
= i
->compatible
;
199 if (!of_flat_dt_is_compatible(rmem
->fdt_node
, compat
))
202 if (initfn(rmem
) == 0) {
203 pr_info("initialized node %s, compatible id %s\n",
211 static int __init
__rmem_cmp(const void *a
, const void *b
)
213 const struct reserved_mem
*ra
= a
, *rb
= b
;
215 if (ra
->base
< rb
->base
)
218 if (ra
->base
> rb
->base
)
224 static void __init
__rmem_check_for_overlap(void)
228 if (reserved_mem_count
< 2)
231 sort(reserved_mem
, reserved_mem_count
, sizeof(reserved_mem
[0]),
233 for (i
= 0; i
< reserved_mem_count
- 1; i
++) {
234 struct reserved_mem
*this, *next
;
236 this = &reserved_mem
[i
];
237 next
= &reserved_mem
[i
+ 1];
238 if (!(this->base
&& next
->base
))
240 if (this->base
+ this->size
> next
->base
) {
241 phys_addr_t this_end
, next_end
;
243 this_end
= this->base
+ this->size
;
244 next_end
= next
->base
+ next
->size
;
245 pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
246 this->name
, &this->base
, &this_end
,
247 next
->name
, &next
->base
, &next_end
);
253 * fdt_init_reserved_mem - allocate and init all saved reserved memory regions
255 void __init
fdt_init_reserved_mem(void)
259 /* check for overlapping reserved regions */
260 __rmem_check_for_overlap();
262 for (i
= 0; i
< reserved_mem_count
; i
++) {
263 struct reserved_mem
*rmem
= &reserved_mem
[i
];
264 unsigned long node
= rmem
->fdt_node
;
269 prop
= of_get_flat_dt_prop(node
, "phandle", &len
);
271 prop
= of_get_flat_dt_prop(node
, "linux,phandle", &len
);
273 rmem
->phandle
= of_read_number(prop
, len
/4);
276 err
= __reserved_mem_alloc_size(node
, rmem
->name
,
277 &rmem
->base
, &rmem
->size
);
279 __reserved_mem_init_node(rmem
);
283 static inline struct reserved_mem
*__find_rmem(struct device_node
*node
)
290 for (i
= 0; i
< reserved_mem_count
; i
++)
291 if (reserved_mem
[i
].phandle
== node
->phandle
)
292 return &reserved_mem
[i
];
296 struct rmem_assigned_device
{
298 struct reserved_mem
*rmem
;
299 struct list_head list
;
302 static LIST_HEAD(of_rmem_assigned_device_list
);
303 static DEFINE_MUTEX(of_rmem_assigned_device_mutex
);
306 * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
308 * @dev: Pointer to the device to configure
309 * @np: Pointer to the device_node with 'reserved-memory' property
310 * @idx: Index of selected region
312 * This function assigns respective DMA-mapping operations based on reserved
313 * memory region specified by 'memory-region' property in @np node to the @dev
314 * device. When driver needs to use more than one reserved memory region, it
315 * should allocate child devices and initialize regions by name for each of
318 * Returns error code or zero on success.
320 int of_reserved_mem_device_init_by_idx(struct device
*dev
,
321 struct device_node
*np
, int idx
)
323 struct rmem_assigned_device
*rd
;
324 struct device_node
*target
;
325 struct reserved_mem
*rmem
;
331 target
= of_parse_phandle(np
, "memory-region", idx
);
335 rmem
= __find_rmem(target
);
338 if (!rmem
|| !rmem
->ops
|| !rmem
->ops
->device_init
)
341 rd
= kmalloc(sizeof(struct rmem_assigned_device
), GFP_KERNEL
);
345 ret
= rmem
->ops
->device_init(rmem
, dev
);
350 mutex_lock(&of_rmem_assigned_device_mutex
);
351 list_add(&rd
->list
, &of_rmem_assigned_device_list
);
352 mutex_unlock(&of_rmem_assigned_device_mutex
);
353 /* ensure that dma_ops is set for virtual devices
354 * using reserved memory
356 of_dma_configure(dev
, np
, true);
358 dev_info(dev
, "assigned reserved memory node %s\n", rmem
->name
);
365 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx
);
368 * of_reserved_mem_device_release() - release reserved memory device structures
369 * @dev: Pointer to the device to deconfigure
371 * This function releases structures allocated for memory region handling for
374 void of_reserved_mem_device_release(struct device
*dev
)
376 struct rmem_assigned_device
*rd
;
377 struct reserved_mem
*rmem
= NULL
;
379 mutex_lock(&of_rmem_assigned_device_mutex
);
380 list_for_each_entry(rd
, &of_rmem_assigned_device_list
, list
) {
381 if (rd
->dev
== dev
) {
388 mutex_unlock(&of_rmem_assigned_device_mutex
);
390 if (!rmem
|| !rmem
->ops
|| !rmem
->ops
->device_release
)
393 rmem
->ops
->device_release(rmem
, dev
);
395 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release
);
398 * of_reserved_mem_lookup() - acquire reserved_mem from a device node
399 * @np: node pointer of the desired reserved-memory region
401 * This function allows drivers to acquire a reference to the reserved_mem
402 * struct based on a device node handle.
404 * Returns a reserved_mem reference, or NULL on error.
406 struct reserved_mem
*of_reserved_mem_lookup(struct device_node
*np
)
414 name
= kbasename(np
->full_name
);
415 for (i
= 0; i
< reserved_mem_count
; i
++)
416 if (!strcmp(reserved_mem
[i
].name
, name
))
417 return &reserved_mem
[i
];
421 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup
);