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 #define MAX_RESERVED_REGIONS 64
26 static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
27 static int reserved_mem_count;
29 static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
30 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
31 phys_addr_t *res_base)
35 end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
36 align = !align ? SMP_CACHE_BYTES : align;
37 base = memblock_find_in_range(start, end, size, align);
43 return memblock_remove(base, size);
45 return memblock_reserve(base, size);
49 * fdt_reserved_mem_save_node() - save fdt node for second pass initialization
51 void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
52 phys_addr_t base, phys_addr_t size)
54 struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
56 if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) {
57 pr_err("not enough space for all defined regions.\n");
61 rmem->fdt_node = node;
71 * __reserved_mem_alloc_size() - allocate reserved memory described by
72 * 'size', 'alignment' and 'alloc-ranges' properties.
74 static int __init __reserved_mem_alloc_size(unsigned long node,
75 const char *uname, phys_addr_t *res_base, phys_addr_t *res_size)
77 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
78 phys_addr_t start = 0, end = 0;
79 phys_addr_t base = 0, align = 0, size;
85 prop = of_get_flat_dt_prop(node, "size", &len);
89 if (len != dt_root_size_cells * sizeof(__be32)) {
90 pr_err("invalid size property in '%s' node.\n", uname);
93 size = dt_mem_next_cell(dt_root_size_cells, &prop);
95 prop = of_get_flat_dt_prop(node, "alignment", &len);
97 if (len != dt_root_addr_cells * sizeof(__be32)) {
98 pr_err("invalid alignment property in '%s' node.\n",
102 align = dt_mem_next_cell(dt_root_addr_cells, &prop);
105 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
107 /* Need adjust the alignment to satisfy the CMA requirement */
108 if (IS_ENABLED(CONFIG_CMA)
109 && of_flat_dt_is_compatible(node, "shared-dma-pool")
110 && of_get_flat_dt_prop(node, "reusable", NULL)
112 unsigned long order =
113 max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
115 align = max(align, (phys_addr_t)PAGE_SIZE << order);
118 prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
121 if (len % t_len != 0) {
122 pr_err("invalid alloc-ranges property in '%s', skipping node.\n",
130 start = dt_mem_next_cell(dt_root_addr_cells, &prop);
131 end = start + dt_mem_next_cell(dt_root_size_cells,
134 ret = early_init_dt_alloc_reserved_memory_arch(size,
135 align, start, end, nomap, &base);
137 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
139 (unsigned long)size / SZ_1M);
146 ret = early_init_dt_alloc_reserved_memory_arch(size, align,
149 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
150 uname, &base, (unsigned long)size / SZ_1M);
154 pr_info("failed to allocate memory for node '%s'\n", uname);
164 static const struct of_device_id __rmem_of_table_sentinel
165 __used __section("__reservedmem_of_table_end");
168 * __reserved_mem_init_node() - call region specific reserved memory init code
170 static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
172 extern const struct of_device_id __reservedmem_of_table[];
173 const struct of_device_id *i;
176 for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
177 reservedmem_of_init_fn initfn = i->data;
178 const char *compat = i->compatible;
180 if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
185 pr_info("initialized node %s, compatible id %s\n",
193 static int __init __rmem_cmp(const void *a, const void *b)
195 const struct reserved_mem *ra = a, *rb = b;
197 if (ra->base < rb->base)
200 if (ra->base > rb->base)
206 static void __init __rmem_check_for_overlap(void)
210 if (reserved_mem_count < 2)
213 sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]),
215 for (i = 0; i < reserved_mem_count - 1; i++) {
216 struct reserved_mem *this, *next;
218 this = &reserved_mem[i];
219 next = &reserved_mem[i + 1];
220 if (!(this->base && next->base))
222 if (this->base + this->size > next->base) {
223 phys_addr_t this_end, next_end;
225 this_end = this->base + this->size;
226 next_end = next->base + next->size;
227 pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
228 this->name, &this->base, &this_end,
229 next->name, &next->base, &next_end);
235 * fdt_init_reserved_mem() - allocate and init all saved reserved memory regions
237 void __init fdt_init_reserved_mem(void)
241 /* check for overlapping reserved regions */
242 __rmem_check_for_overlap();
244 for (i = 0; i < reserved_mem_count; i++) {
245 struct reserved_mem *rmem = &reserved_mem[i];
246 unsigned long node = rmem->fdt_node;
252 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
253 prop = of_get_flat_dt_prop(node, "phandle", &len);
255 prop = of_get_flat_dt_prop(node, "linux,phandle", &len);
257 rmem->phandle = of_read_number(prop, len/4);
260 err = __reserved_mem_alloc_size(node, rmem->name,
261 &rmem->base, &rmem->size);
263 err = __reserved_mem_init_node(rmem);
264 if (err != 0 && err != -ENOENT) {
265 pr_info("node %s compatible matching fail\n",
267 memblock_free(rmem->base, rmem->size);
269 memblock_add(rmem->base, rmem->size);
275 static inline struct reserved_mem *__find_rmem(struct device_node *node)
282 for (i = 0; i < reserved_mem_count; i++)
283 if (reserved_mem[i].phandle == node->phandle)
284 return &reserved_mem[i];
288 struct rmem_assigned_device {
290 struct reserved_mem *rmem;
291 struct list_head list;
294 static LIST_HEAD(of_rmem_assigned_device_list);
295 static DEFINE_MUTEX(of_rmem_assigned_device_mutex);
298 * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
300 * @dev: Pointer to the device to configure
301 * @np: Pointer to the device_node with 'reserved-memory' property
302 * @idx: Index of selected region
304 * This function assigns respective DMA-mapping operations based on reserved
305 * memory region specified by 'memory-region' property in @np node to the @dev
306 * device. When driver needs to use more than one reserved memory region, it
307 * should allocate child devices and initialize regions by name for each of
310 * Returns error code or zero on success.
312 int of_reserved_mem_device_init_by_idx(struct device *dev,
313 struct device_node *np, int idx)
315 struct rmem_assigned_device *rd;
316 struct device_node *target;
317 struct reserved_mem *rmem;
323 target = of_parse_phandle(np, "memory-region", idx);
327 if (!of_device_is_available(target)) {
332 rmem = __find_rmem(target);
335 if (!rmem || !rmem->ops || !rmem->ops->device_init)
338 rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL);
342 ret = rmem->ops->device_init(rmem, dev);
347 mutex_lock(&of_rmem_assigned_device_mutex);
348 list_add(&rd->list, &of_rmem_assigned_device_list);
349 mutex_unlock(&of_rmem_assigned_device_mutex);
351 dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
358 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx);
361 * of_reserved_mem_device_init_by_name() - assign named reserved memory region
363 * @dev: pointer to the device to configure
364 * @np: pointer to the device node with 'memory-region' property
365 * @name: name of the selected memory region
367 * Returns: 0 on success or a negative error-code on failure.
369 int of_reserved_mem_device_init_by_name(struct device *dev,
370 struct device_node *np,
373 int idx = of_property_match_string(np, "memory-region-names", name);
375 return of_reserved_mem_device_init_by_idx(dev, np, idx);
377 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_name);
380 * of_reserved_mem_device_release() - release reserved memory device structures
381 * @dev: Pointer to the device to deconfigure
383 * This function releases structures allocated for memory region handling for
386 void of_reserved_mem_device_release(struct device *dev)
388 struct rmem_assigned_device *rd, *tmp;
389 LIST_HEAD(release_list);
391 mutex_lock(&of_rmem_assigned_device_mutex);
392 list_for_each_entry_safe(rd, tmp, &of_rmem_assigned_device_list, list) {
394 list_move_tail(&rd->list, &release_list);
396 mutex_unlock(&of_rmem_assigned_device_mutex);
398 list_for_each_entry_safe(rd, tmp, &release_list, list) {
399 if (rd->rmem && rd->rmem->ops && rd->rmem->ops->device_release)
400 rd->rmem->ops->device_release(rd->rmem, dev);
405 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
408 * of_reserved_mem_lookup() - acquire reserved_mem from a device node
409 * @np: node pointer of the desired reserved-memory region
411 * This function allows drivers to acquire a reference to the reserved_mem
412 * struct based on a device node handle.
414 * Returns a reserved_mem reference, or NULL on error.
416 struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
424 name = kbasename(np->full_name);
425 for (i = 0; i < reserved_mem_count; i++)
426 if (!strcmp(reserved_mem[i].name, name))
427 return &reserved_mem[i];
431 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);