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>
23 #include <linux/memblock.h>
24 #include <linux/kmemleak.h>
25 #include <linux/cma.h>
27 #include "of_private.h"
29 #define MAX_RESERVED_REGIONS 64
30 static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
31 static int reserved_mem_count;
33 static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
34 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
35 phys_addr_t *res_base)
40 end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
41 align = !align ? SMP_CACHE_BYTES : align;
42 base = memblock_phys_alloc_range(size, align, start, end);
48 err = memblock_mark_nomap(base, size);
50 memblock_phys_free(base, size);
51 kmemleak_ignore_phys(base);
58 * fdt_reserved_mem_save_node() - save fdt node for second pass initialization
60 void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
61 phys_addr_t base, phys_addr_t size)
63 struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
65 if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) {
66 pr_err("not enough space for all defined regions.\n");
70 rmem->fdt_node = node;
80 * __reserved_mem_alloc_size() - allocate reserved memory described by
81 * 'size', 'alignment' and 'alloc-ranges' properties.
83 static int __init __reserved_mem_alloc_size(unsigned long node,
84 const char *uname, phys_addr_t *res_base, phys_addr_t *res_size)
86 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
87 phys_addr_t start = 0, end = 0;
88 phys_addr_t base = 0, align = 0, size;
94 prop = of_get_flat_dt_prop(node, "size", &len);
98 if (len != dt_root_size_cells * sizeof(__be32)) {
99 pr_err("invalid size property in '%s' node.\n", uname);
102 size = dt_mem_next_cell(dt_root_size_cells, &prop);
104 prop = of_get_flat_dt_prop(node, "alignment", &len);
106 if (len != dt_root_addr_cells * sizeof(__be32)) {
107 pr_err("invalid alignment property in '%s' node.\n",
111 align = dt_mem_next_cell(dt_root_addr_cells, &prop);
114 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
116 /* Need adjust the alignment to satisfy the CMA requirement */
117 if (IS_ENABLED(CONFIG_CMA)
118 && of_flat_dt_is_compatible(node, "shared-dma-pool")
119 && of_get_flat_dt_prop(node, "reusable", NULL)
121 align = max_t(phys_addr_t, align, CMA_MIN_ALIGNMENT_BYTES);
123 prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
126 if (len % t_len != 0) {
127 pr_err("invalid alloc-ranges property in '%s', skipping node.\n",
135 start = dt_mem_next_cell(dt_root_addr_cells, &prop);
136 end = start + dt_mem_next_cell(dt_root_size_cells,
139 ret = early_init_dt_alloc_reserved_memory_arch(size,
140 align, start, end, nomap, &base);
142 pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
144 (unsigned long)(size / SZ_1M));
151 ret = early_init_dt_alloc_reserved_memory_arch(size, align,
154 pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
155 uname, &base, (unsigned long)(size / SZ_1M));
159 pr_err("failed to allocate memory for node '%s': size %lu MiB\n",
160 uname, (unsigned long)(size / SZ_1M));
170 static const struct of_device_id __rmem_of_table_sentinel
171 __used __section("__reservedmem_of_table_end");
174 * __reserved_mem_init_node() - call region specific reserved memory init code
176 static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
178 extern const struct of_device_id __reservedmem_of_table[];
179 const struct of_device_id *i;
182 for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
183 reservedmem_of_init_fn initfn = i->data;
184 const char *compat = i->compatible;
186 if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
191 pr_info("initialized node %s, compatible id %s\n",
199 static int __init __rmem_cmp(const void *a, const void *b)
201 const struct reserved_mem *ra = a, *rb = b;
203 if (ra->base < rb->base)
206 if (ra->base > rb->base)
210 * Put the dynamic allocations (address == 0, size == 0) before static
211 * allocations at address 0x0 so that overlap detection works
214 if (ra->size < rb->size)
216 if (ra->size > rb->size)
222 static void __init __rmem_check_for_overlap(void)
226 if (reserved_mem_count < 2)
229 sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]),
231 for (i = 0; i < reserved_mem_count - 1; i++) {
232 struct reserved_mem *this, *next;
234 this = &reserved_mem[i];
235 next = &reserved_mem[i + 1];
237 if (this->base + this->size > next->base) {
238 phys_addr_t this_end, next_end;
240 this_end = this->base + this->size;
241 next_end = next->base + next->size;
242 pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
243 this->name, &this->base, &this_end,
244 next->name, &next->base, &next_end);
250 * fdt_init_reserved_mem() - allocate and init all saved reserved memory regions
252 void __init fdt_init_reserved_mem(void)
256 /* check for overlapping reserved regions */
257 __rmem_check_for_overlap();
259 for (i = 0; i < reserved_mem_count; i++) {
260 struct reserved_mem *rmem = &reserved_mem[i];
261 unsigned long node = rmem->fdt_node;
267 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
268 prop = of_get_flat_dt_prop(node, "phandle", &len);
270 prop = of_get_flat_dt_prop(node, "linux,phandle", &len);
272 rmem->phandle = of_read_number(prop, len/4);
275 err = __reserved_mem_alloc_size(node, rmem->name,
276 &rmem->base, &rmem->size);
278 err = __reserved_mem_init_node(rmem);
279 if (err != 0 && err != -ENOENT) {
280 pr_info("node %s compatible matching fail\n",
283 memblock_clear_nomap(rmem->base, rmem->size);
285 memblock_phys_free(rmem->base,
292 static inline struct reserved_mem *__find_rmem(struct device_node *node)
299 for (i = 0; i < reserved_mem_count; i++)
300 if (reserved_mem[i].phandle == node->phandle)
301 return &reserved_mem[i];
305 struct rmem_assigned_device {
307 struct reserved_mem *rmem;
308 struct list_head list;
311 static LIST_HEAD(of_rmem_assigned_device_list);
312 static DEFINE_MUTEX(of_rmem_assigned_device_mutex);
315 * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
317 * @dev: Pointer to the device to configure
318 * @np: Pointer to the device_node with 'reserved-memory' property
319 * @idx: Index of selected region
321 * This function assigns respective DMA-mapping operations based on reserved
322 * memory region specified by 'memory-region' property in @np node to the @dev
323 * device. When driver needs to use more than one reserved memory region, it
324 * should allocate child devices and initialize regions by name for each of
327 * Returns error code or zero on success.
329 int of_reserved_mem_device_init_by_idx(struct device *dev,
330 struct device_node *np, int idx)
332 struct rmem_assigned_device *rd;
333 struct device_node *target;
334 struct reserved_mem *rmem;
340 target = of_parse_phandle(np, "memory-region", idx);
344 if (!of_device_is_available(target)) {
349 rmem = __find_rmem(target);
352 if (!rmem || !rmem->ops || !rmem->ops->device_init)
355 rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL);
359 ret = rmem->ops->device_init(rmem, dev);
364 mutex_lock(&of_rmem_assigned_device_mutex);
365 list_add(&rd->list, &of_rmem_assigned_device_list);
366 mutex_unlock(&of_rmem_assigned_device_mutex);
368 dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
375 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx);
378 * of_reserved_mem_device_init_by_name() - assign named reserved memory region
380 * @dev: pointer to the device to configure
381 * @np: pointer to the device node with 'memory-region' property
382 * @name: name of the selected memory region
384 * Returns: 0 on success or a negative error-code on failure.
386 int of_reserved_mem_device_init_by_name(struct device *dev,
387 struct device_node *np,
390 int idx = of_property_match_string(np, "memory-region-names", name);
392 return of_reserved_mem_device_init_by_idx(dev, np, idx);
394 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_name);
397 * of_reserved_mem_device_release() - release reserved memory device structures
398 * @dev: Pointer to the device to deconfigure
400 * This function releases structures allocated for memory region handling for
403 void of_reserved_mem_device_release(struct device *dev)
405 struct rmem_assigned_device *rd, *tmp;
406 LIST_HEAD(release_list);
408 mutex_lock(&of_rmem_assigned_device_mutex);
409 list_for_each_entry_safe(rd, tmp, &of_rmem_assigned_device_list, list) {
411 list_move_tail(&rd->list, &release_list);
413 mutex_unlock(&of_rmem_assigned_device_mutex);
415 list_for_each_entry_safe(rd, tmp, &release_list, list) {
416 if (rd->rmem && rd->rmem->ops && rd->rmem->ops->device_release)
417 rd->rmem->ops->device_release(rd->rmem, dev);
422 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
425 * of_reserved_mem_lookup() - acquire reserved_mem from a device node
426 * @np: node pointer of the desired reserved-memory region
428 * This function allows drivers to acquire a reference to the reserved_mem
429 * struct based on a device node handle.
431 * Returns a reserved_mem reference, or NULL on error.
433 struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
441 name = kbasename(np->full_name);
442 for (i = 0; i < reserved_mem_count; i++)
443 if (!strcmp(reserved_mem[i].name, name))
444 return &reserved_mem[i];
448 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);