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>
25 #include "of_private.h"
27 #define MAX_RESERVED_REGIONS 64
28 static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
29 static int reserved_mem_count;
31 static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
32 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
33 phys_addr_t *res_base)
38 end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
39 align = !align ? SMP_CACHE_BYTES : align;
40 base = memblock_phys_alloc_range(size, align, start, end);
46 err = memblock_mark_nomap(base, size);
48 memblock_free(base, size);
55 * fdt_reserved_mem_save_node() - save fdt node for second pass initialization
57 void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
58 phys_addr_t base, phys_addr_t size)
60 struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
62 if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) {
63 pr_err("not enough space for all defined regions.\n");
67 rmem->fdt_node = node;
77 * __reserved_mem_alloc_size() - allocate reserved memory described by
78 * 'size', 'alignment' and 'alloc-ranges' properties.
80 static int __init __reserved_mem_alloc_size(unsigned long node,
81 const char *uname, phys_addr_t *res_base, phys_addr_t *res_size)
83 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
84 phys_addr_t start = 0, end = 0;
85 phys_addr_t base = 0, align = 0, size;
91 prop = of_get_flat_dt_prop(node, "size", &len);
95 if (len != dt_root_size_cells * sizeof(__be32)) {
96 pr_err("invalid size property in '%s' node.\n", uname);
99 size = dt_mem_next_cell(dt_root_size_cells, &prop);
101 prop = of_get_flat_dt_prop(node, "alignment", &len);
103 if (len != dt_root_addr_cells * sizeof(__be32)) {
104 pr_err("invalid alignment property in '%s' node.\n",
108 align = dt_mem_next_cell(dt_root_addr_cells, &prop);
111 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
113 /* Need adjust the alignment to satisfy the CMA requirement */
114 if (IS_ENABLED(CONFIG_CMA)
115 && of_flat_dt_is_compatible(node, "shared-dma-pool")
116 && of_get_flat_dt_prop(node, "reusable", NULL)
118 unsigned long order =
119 max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
121 align = max(align, (phys_addr_t)PAGE_SIZE << order);
124 prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
127 if (len % t_len != 0) {
128 pr_err("invalid alloc-ranges property in '%s', skipping node.\n",
136 start = dt_mem_next_cell(dt_root_addr_cells, &prop);
137 end = start + dt_mem_next_cell(dt_root_size_cells,
140 ret = early_init_dt_alloc_reserved_memory_arch(size,
141 align, start, end, nomap, &base);
143 pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
145 (unsigned long)(size / SZ_1M));
152 ret = early_init_dt_alloc_reserved_memory_arch(size, align,
155 pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
156 uname, &base, (unsigned long)(size / SZ_1M));
160 pr_info("failed to allocate memory for node '%s'\n", uname);
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_free(rmem->base, rmem->size);
291 static inline struct reserved_mem *__find_rmem(struct device_node *node)
298 for (i = 0; i < reserved_mem_count; i++)
299 if (reserved_mem[i].phandle == node->phandle)
300 return &reserved_mem[i];
304 struct rmem_assigned_device {
306 struct reserved_mem *rmem;
307 struct list_head list;
310 static LIST_HEAD(of_rmem_assigned_device_list);
311 static DEFINE_MUTEX(of_rmem_assigned_device_mutex);
314 * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
316 * @dev: Pointer to the device to configure
317 * @np: Pointer to the device_node with 'reserved-memory' property
318 * @idx: Index of selected region
320 * This function assigns respective DMA-mapping operations based on reserved
321 * memory region specified by 'memory-region' property in @np node to the @dev
322 * device. When driver needs to use more than one reserved memory region, it
323 * should allocate child devices and initialize regions by name for each of
326 * Returns error code or zero on success.
328 int of_reserved_mem_device_init_by_idx(struct device *dev,
329 struct device_node *np, int idx)
331 struct rmem_assigned_device *rd;
332 struct device_node *target;
333 struct reserved_mem *rmem;
339 target = of_parse_phandle(np, "memory-region", idx);
343 if (!of_device_is_available(target)) {
348 rmem = __find_rmem(target);
351 if (!rmem || !rmem->ops || !rmem->ops->device_init)
354 rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL);
358 ret = rmem->ops->device_init(rmem, dev);
363 mutex_lock(&of_rmem_assigned_device_mutex);
364 list_add(&rd->list, &of_rmem_assigned_device_list);
365 mutex_unlock(&of_rmem_assigned_device_mutex);
367 dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
374 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx);
377 * of_reserved_mem_device_init_by_name() - assign named reserved memory region
379 * @dev: pointer to the device to configure
380 * @np: pointer to the device node with 'memory-region' property
381 * @name: name of the selected memory region
383 * Returns: 0 on success or a negative error-code on failure.
385 int of_reserved_mem_device_init_by_name(struct device *dev,
386 struct device_node *np,
389 int idx = of_property_match_string(np, "memory-region-names", name);
391 return of_reserved_mem_device_init_by_idx(dev, np, idx);
393 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_name);
396 * of_reserved_mem_device_release() - release reserved memory device structures
397 * @dev: Pointer to the device to deconfigure
399 * This function releases structures allocated for memory region handling for
402 void of_reserved_mem_device_release(struct device *dev)
404 struct rmem_assigned_device *rd, *tmp;
405 LIST_HEAD(release_list);
407 mutex_lock(&of_rmem_assigned_device_mutex);
408 list_for_each_entry_safe(rd, tmp, &of_rmem_assigned_device_list, list) {
410 list_move_tail(&rd->list, &release_list);
412 mutex_unlock(&of_rmem_assigned_device_mutex);
414 list_for_each_entry_safe(rd, tmp, &release_list, list) {
415 if (rd->rmem && rd->rmem->ops && rd->rmem->ops->device_release)
416 rd->rmem->ops->device_release(rd->rmem, dev);
421 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
424 * of_reserved_mem_lookup() - acquire reserved_mem from a device node
425 * @np: node pointer of the desired reserved-memory region
427 * This function allows drivers to acquire a reference to the reserved_mem
428 * struct based on a device node handle.
430 * Returns a reserved_mem reference, or NULL on error.
432 struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
440 name = kbasename(np->full_name);
441 for (i = 0; i < reserved_mem_count; i++)
442 if (!strcmp(reserved_mem[i].name, name))
443 return &reserved_mem[i];
447 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);