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>
15 #include <linux/libfdt.h>
17 #include <linux/of_fdt.h>
18 #include <linux/of_platform.h>
20 #include <linux/sizes.h>
21 #include <linux/of_reserved_mem.h>
22 #include <linux/sort.h>
23 #include <linux/slab.h>
24 #include <linux/memblock.h>
25 #include <linux/kmemleak.h>
26 #include <linux/cma.h>
28 #include "of_private.h"
30 #define MAX_RESERVED_REGIONS 64
31 static struct reserved_mem reserved_mem
[MAX_RESERVED_REGIONS
];
32 static int reserved_mem_count
;
34 static int __init
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
)
41 end
= !end
? MEMBLOCK_ALLOC_ANYWHERE
: end
;
42 align
= !align
? SMP_CACHE_BYTES
: align
;
43 base
= memblock_phys_alloc_range(size
, align
, start
, end
);
49 err
= memblock_mark_nomap(base
, size
);
51 memblock_phys_free(base
, size
);
54 kmemleak_ignore_phys(base
);
60 * fdt_reserved_mem_save_node() - save fdt node for second pass initialization
62 static void __init
fdt_reserved_mem_save_node(unsigned long node
, const char *uname
,
63 phys_addr_t base
, phys_addr_t size
)
65 struct reserved_mem
*rmem
= &reserved_mem
[reserved_mem_count
];
67 if (reserved_mem_count
== ARRAY_SIZE(reserved_mem
)) {
68 pr_err("not enough space for all defined regions.\n");
72 rmem
->fdt_node
= node
;
81 static int __init
early_init_dt_reserve_memory(phys_addr_t base
,
82 phys_addr_t size
, bool nomap
)
86 * If the memory is already reserved (by another region), we
87 * should not allow it to be marked nomap, but don't worry
88 * if the region isn't memory as it won't be mapped.
90 if (memblock_overlaps_region(&memblock
.memory
, base
, size
) &&
91 memblock_is_region_reserved(base
, size
))
94 return memblock_mark_nomap(base
, size
);
96 return memblock_reserve(base
, size
);
100 * __reserved_mem_reserve_reg() - reserve all memory described in 'reg' property
102 static int __init
__reserved_mem_reserve_reg(unsigned long node
,
105 int t_len
= (dt_root_addr_cells
+ dt_root_size_cells
) * sizeof(__be32
);
106 phys_addr_t base
, size
;
112 prop
= of_get_flat_dt_prop(node
, "reg", &len
);
116 if (len
&& len
% t_len
!= 0) {
117 pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
122 nomap
= of_get_flat_dt_prop(node
, "no-map", NULL
) != NULL
;
124 while (len
>= t_len
) {
125 base
= dt_mem_next_cell(dt_root_addr_cells
, &prop
);
126 size
= dt_mem_next_cell(dt_root_size_cells
, &prop
);
129 early_init_dt_reserve_memory(base
, size
, nomap
) == 0)
130 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n",
131 uname
, &base
, (unsigned long)(size
/ SZ_1M
));
133 pr_err("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n",
134 uname
, &base
, (unsigned long)(size
/ SZ_1M
));
138 fdt_reserved_mem_save_node(node
, uname
, base
, size
);
146 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
147 * in /reserved-memory matches the values supported by the current implementation,
148 * also check if ranges property has been provided
150 static int __init
__reserved_mem_check_root(unsigned long node
)
154 prop
= of_get_flat_dt_prop(node
, "#size-cells", NULL
);
155 if (!prop
|| be32_to_cpup(prop
) != dt_root_size_cells
)
158 prop
= of_get_flat_dt_prop(node
, "#address-cells", NULL
);
159 if (!prop
|| be32_to_cpup(prop
) != dt_root_addr_cells
)
162 prop
= of_get_flat_dt_prop(node
, "ranges", NULL
);
169 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
171 int __init
fdt_scan_reserved_mem(void)
174 const void *fdt
= initial_boot_params
;
176 node
= fdt_path_offset(fdt
, "/reserved-memory");
180 if (__reserved_mem_check_root(node
) != 0) {
181 pr_err("Reserved memory: unsupported node format, ignoring\n");
185 fdt_for_each_subnode(child
, fdt
, node
) {
189 if (!of_fdt_device_is_available(fdt
, child
))
192 uname
= fdt_get_name(fdt
, child
, NULL
);
194 err
= __reserved_mem_reserve_reg(child
, uname
);
195 if (err
== -ENOENT
&& of_get_flat_dt_prop(child
, "size", NULL
))
196 fdt_reserved_mem_save_node(child
, uname
, 0, 0);
202 * __reserved_mem_alloc_in_range() - allocate reserved memory described with
203 * 'alloc-ranges'. Choose bottom-up/top-down depending on nearby existing
204 * reserved regions to keep the reserved memory contiguous if possible.
206 static int __init
__reserved_mem_alloc_in_range(phys_addr_t size
,
207 phys_addr_t align
, phys_addr_t start
, phys_addr_t end
, bool nomap
,
208 phys_addr_t
*res_base
)
210 bool prev_bottom_up
= memblock_bottom_up();
211 bool bottom_up
= false, top_down
= false;
214 for (i
= 0; i
< reserved_mem_count
; i
++) {
215 struct reserved_mem
*rmem
= &reserved_mem
[i
];
217 /* Skip regions that were not reserved yet */
222 * If range starts next to an existing reservation, use bottom-up:
223 * |....RRRR................RRRRRRRR..............|
226 if (start
>= rmem
->base
&& start
<= (rmem
->base
+ rmem
->size
))
230 * If range ends next to an existing reservation, use top-down:
231 * |....RRRR................RRRRRRRR..............|
234 if (end
>= rmem
->base
&& end
<= (rmem
->base
+ rmem
->size
))
238 /* Change setting only if either bottom-up or top-down was selected */
239 if (bottom_up
!= top_down
)
240 memblock_set_bottom_up(bottom_up
);
242 ret
= early_init_dt_alloc_reserved_memory_arch(size
, align
,
243 start
, end
, nomap
, res_base
);
245 /* Restore old setting if needed */
246 if (bottom_up
!= top_down
)
247 memblock_set_bottom_up(prev_bottom_up
);
253 * __reserved_mem_alloc_size() - allocate reserved memory described by
254 * 'size', 'alignment' and 'alloc-ranges' properties.
256 static int __init
__reserved_mem_alloc_size(unsigned long node
,
257 const char *uname
, phys_addr_t
*res_base
, phys_addr_t
*res_size
)
259 int t_len
= (dt_root_addr_cells
+ dt_root_size_cells
) * sizeof(__be32
);
260 phys_addr_t start
= 0, end
= 0;
261 phys_addr_t base
= 0, align
= 0, size
;
267 prop
= of_get_flat_dt_prop(node
, "size", &len
);
271 if (len
!= dt_root_size_cells
* sizeof(__be32
)) {
272 pr_err("invalid size property in '%s' node.\n", uname
);
275 size
= dt_mem_next_cell(dt_root_size_cells
, &prop
);
277 prop
= of_get_flat_dt_prop(node
, "alignment", &len
);
279 if (len
!= dt_root_addr_cells
* sizeof(__be32
)) {
280 pr_err("invalid alignment property in '%s' node.\n",
284 align
= dt_mem_next_cell(dt_root_addr_cells
, &prop
);
287 nomap
= of_get_flat_dt_prop(node
, "no-map", NULL
) != NULL
;
289 /* Need adjust the alignment to satisfy the CMA requirement */
290 if (IS_ENABLED(CONFIG_CMA
)
291 && of_flat_dt_is_compatible(node
, "shared-dma-pool")
292 && of_get_flat_dt_prop(node
, "reusable", NULL
)
294 align
= max_t(phys_addr_t
, align
, CMA_MIN_ALIGNMENT_BYTES
);
296 prop
= of_get_flat_dt_prop(node
, "alloc-ranges", &len
);
299 if (len
% t_len
!= 0) {
300 pr_err("invalid alloc-ranges property in '%s', skipping node.\n",
308 start
= dt_mem_next_cell(dt_root_addr_cells
, &prop
);
309 end
= start
+ dt_mem_next_cell(dt_root_size_cells
,
312 ret
= __reserved_mem_alloc_in_range(size
, align
,
313 start
, end
, nomap
, &base
);
315 pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
317 (unsigned long)(size
/ SZ_1M
));
324 ret
= early_init_dt_alloc_reserved_memory_arch(size
, align
,
327 pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
328 uname
, &base
, (unsigned long)(size
/ SZ_1M
));
332 pr_err("failed to allocate memory for node '%s': size %lu MiB\n",
333 uname
, (unsigned long)(size
/ SZ_1M
));
343 static const struct of_device_id __rmem_of_table_sentinel
344 __used
__section("__reservedmem_of_table_end");
347 * __reserved_mem_init_node() - call region specific reserved memory init code
349 static int __init
__reserved_mem_init_node(struct reserved_mem
*rmem
)
351 extern const struct of_device_id __reservedmem_of_table
[];
352 const struct of_device_id
*i
;
355 for (i
= __reservedmem_of_table
; i
< &__rmem_of_table_sentinel
; i
++) {
356 reservedmem_of_init_fn initfn
= i
->data
;
357 const char *compat
= i
->compatible
;
359 if (!of_flat_dt_is_compatible(rmem
->fdt_node
, compat
))
364 pr_info("initialized node %s, compatible id %s\n",
372 static int __init
__rmem_cmp(const void *a
, const void *b
)
374 const struct reserved_mem
*ra
= a
, *rb
= b
;
376 if (ra
->base
< rb
->base
)
379 if (ra
->base
> rb
->base
)
383 * Put the dynamic allocations (address == 0, size == 0) before static
384 * allocations at address 0x0 so that overlap detection works
387 if (ra
->size
< rb
->size
)
389 if (ra
->size
> rb
->size
)
392 if (ra
->fdt_node
< rb
->fdt_node
)
394 if (ra
->fdt_node
> rb
->fdt_node
)
400 static void __init
__rmem_check_for_overlap(void)
404 if (reserved_mem_count
< 2)
407 sort(reserved_mem
, reserved_mem_count
, sizeof(reserved_mem
[0]),
409 for (i
= 0; i
< reserved_mem_count
- 1; i
++) {
410 struct reserved_mem
*this, *next
;
412 this = &reserved_mem
[i
];
413 next
= &reserved_mem
[i
+ 1];
415 if (this->base
+ this->size
> next
->base
) {
416 phys_addr_t this_end
, next_end
;
418 this_end
= this->base
+ this->size
;
419 next_end
= next
->base
+ next
->size
;
420 pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
421 this->name
, &this->base
, &this_end
,
422 next
->name
, &next
->base
, &next_end
);
428 * fdt_init_reserved_mem() - allocate and init all saved reserved memory regions
430 void __init
fdt_init_reserved_mem(void)
434 /* check for overlapping reserved regions */
435 __rmem_check_for_overlap();
437 for (i
= 0; i
< reserved_mem_count
; i
++) {
438 struct reserved_mem
*rmem
= &reserved_mem
[i
];
439 unsigned long node
= rmem
->fdt_node
;
443 nomap
= of_get_flat_dt_prop(node
, "no-map", NULL
) != NULL
;
446 err
= __reserved_mem_alloc_size(node
, rmem
->name
,
447 &rmem
->base
, &rmem
->size
);
449 err
= __reserved_mem_init_node(rmem
);
450 if (err
!= 0 && err
!= -ENOENT
) {
451 pr_info("node %s compatible matching fail\n",
454 memblock_clear_nomap(rmem
->base
, rmem
->size
);
456 memblock_phys_free(rmem
->base
,
459 phys_addr_t end
= rmem
->base
+ rmem
->size
- 1;
461 (of_get_flat_dt_prop(node
, "reusable", NULL
)) != NULL
;
463 pr_info("%pa..%pa (%lu KiB) %s %s %s\n",
464 &rmem
->base
, &end
, (unsigned long)(rmem
->size
/ SZ_1K
),
465 nomap
? "nomap" : "map",
466 reusable
? "reusable" : "non-reusable",
467 rmem
->name
? rmem
->name
: "unknown");
473 struct rmem_assigned_device
{
475 struct reserved_mem
*rmem
;
476 struct list_head list
;
479 static LIST_HEAD(of_rmem_assigned_device_list
);
480 static DEFINE_MUTEX(of_rmem_assigned_device_mutex
);
483 * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
485 * @dev: Pointer to the device to configure
486 * @np: Pointer to the device_node with 'reserved-memory' property
487 * @idx: Index of selected region
489 * This function assigns respective DMA-mapping operations based on reserved
490 * memory region specified by 'memory-region' property in @np node to the @dev
491 * device. When driver needs to use more than one reserved memory region, it
492 * should allocate child devices and initialize regions by name for each of
495 * Returns error code or zero on success.
497 int of_reserved_mem_device_init_by_idx(struct device
*dev
,
498 struct device_node
*np
, int idx
)
500 struct rmem_assigned_device
*rd
;
501 struct device_node
*target
;
502 struct reserved_mem
*rmem
;
508 target
= of_parse_phandle(np
, "memory-region", idx
);
512 if (!of_device_is_available(target
)) {
517 rmem
= of_reserved_mem_lookup(target
);
520 if (!rmem
|| !rmem
->ops
|| !rmem
->ops
->device_init
)
523 rd
= kmalloc(sizeof(struct rmem_assigned_device
), GFP_KERNEL
);
527 ret
= rmem
->ops
->device_init(rmem
, dev
);
532 mutex_lock(&of_rmem_assigned_device_mutex
);
533 list_add(&rd
->list
, &of_rmem_assigned_device_list
);
534 mutex_unlock(&of_rmem_assigned_device_mutex
);
536 dev_info(dev
, "assigned reserved memory node %s\n", rmem
->name
);
543 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx
);
546 * of_reserved_mem_device_init_by_name() - assign named reserved memory region
548 * @dev: pointer to the device to configure
549 * @np: pointer to the device node with 'memory-region' property
550 * @name: name of the selected memory region
552 * Returns: 0 on success or a negative error-code on failure.
554 int of_reserved_mem_device_init_by_name(struct device
*dev
,
555 struct device_node
*np
,
558 int idx
= of_property_match_string(np
, "memory-region-names", name
);
560 return of_reserved_mem_device_init_by_idx(dev
, np
, idx
);
562 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_name
);
565 * of_reserved_mem_device_release() - release reserved memory device structures
566 * @dev: Pointer to the device to deconfigure
568 * This function releases structures allocated for memory region handling for
571 void of_reserved_mem_device_release(struct device
*dev
)
573 struct rmem_assigned_device
*rd
, *tmp
;
574 LIST_HEAD(release_list
);
576 mutex_lock(&of_rmem_assigned_device_mutex
);
577 list_for_each_entry_safe(rd
, tmp
, &of_rmem_assigned_device_list
, list
) {
579 list_move_tail(&rd
->list
, &release_list
);
581 mutex_unlock(&of_rmem_assigned_device_mutex
);
583 list_for_each_entry_safe(rd
, tmp
, &release_list
, list
) {
584 if (rd
->rmem
&& rd
->rmem
->ops
&& rd
->rmem
->ops
->device_release
)
585 rd
->rmem
->ops
->device_release(rd
->rmem
, dev
);
590 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release
);
593 * of_reserved_mem_lookup() - acquire reserved_mem from a device node
594 * @np: node pointer of the desired reserved-memory region
596 * This function allows drivers to acquire a reference to the reserved_mem
597 * struct based on a device node handle.
599 * Returns a reserved_mem reference, or NULL on error.
601 struct reserved_mem
*of_reserved_mem_lookup(struct device_node
*np
)
609 name
= kbasename(np
->full_name
);
610 for (i
= 0; i
< reserved_mem_count
; i
++)
611 if (!strcmp(reserved_mem
[i
].name
, name
))
612 return &reserved_mem
[i
];
616 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup
);