1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Device tree based initialization code for reserved memory.
4 *
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>
10 */
11
12 #define pr_fmt(fmt) "OF: reserved mem: " fmt
13
14 #include <linux/err.h>
15 #include <linux/ioport.h>
16 #include <linux/libfdt.h>
17 #include <linux/of.h>
18 #include <linux/of_fdt.h>
19 #include <linux/of_platform.h>
20 #include <linux/mm.h>
21 #include <linux/sizes.h>
22 #include <linux/of_reserved_mem.h>
23 #include <linux/sort.h>
24 #include <linux/slab.h>
25 #include <linux/memblock.h>
26 #include <linux/kmemleak.h>
27 #include <linux/cma.h>
28 #include <linux/dma-map-ops.h>
29
30 #include "of_private.h"
31
32 static struct reserved_mem reserved_mem_array[MAX_RESERVED_REGIONS] __initdata;
33 static struct reserved_mem *reserved_mem __refdata = reserved_mem_array;
34 static int total_reserved_mem_cnt = MAX_RESERVED_REGIONS;
35 static int reserved_mem_count;
36
early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,phys_addr_t align,phys_addr_t start,phys_addr_t end,bool nomap,phys_addr_t * res_base)37 static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
38 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
39 phys_addr_t *res_base)
40 {
41 phys_addr_t base;
42 int err = 0;
43
44 end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
45 align = !align ? SMP_CACHE_BYTES : align;
46 base = memblock_phys_alloc_range(size, align, start, end);
47 if (!base)
48 return -ENOMEM;
49
50 *res_base = base;
51 if (nomap) {
52 err = memblock_mark_nomap(base, size);
53 if (err)
54 memblock_phys_free(base, size);
55 }
56
57 if (!err)
58 kmemleak_ignore_phys(base);
59
60 return err;
61 }
62
63 /*
64 * alloc_reserved_mem_array() - allocate memory for the reserved_mem
65 * array using memblock
66 *
67 * This function is used to allocate memory for the reserved_mem
68 * array according to the total number of reserved memory regions
69 * defined in the DT.
70 * After the new array is allocated, the information stored in
71 * the initial static array is copied over to this new array and
72 * the new array is used from this point on.
73 */
alloc_reserved_mem_array(void)74 static void __init alloc_reserved_mem_array(void)
75 {
76 struct reserved_mem *new_array;
77 size_t alloc_size, copy_size, memset_size;
78
79 alloc_size = array_size(total_reserved_mem_cnt, sizeof(*new_array));
80 if (alloc_size == SIZE_MAX) {
81 pr_err("Failed to allocate memory for reserved_mem array with err: %d", -EOVERFLOW);
82 return;
83 }
84
85 new_array = memblock_alloc(alloc_size, SMP_CACHE_BYTES);
86 if (!new_array) {
87 pr_err("Failed to allocate memory for reserved_mem array with err: %d", -ENOMEM);
88 return;
89 }
90
91 copy_size = array_size(reserved_mem_count, sizeof(*new_array));
92 if (copy_size == SIZE_MAX) {
93 memblock_free(new_array, alloc_size);
94 total_reserved_mem_cnt = MAX_RESERVED_REGIONS;
95 pr_err("Failed to allocate memory for reserved_mem array with err: %d", -EOVERFLOW);
96 return;
97 }
98
99 memset_size = alloc_size - copy_size;
100
101 memcpy(new_array, reserved_mem, copy_size);
102 memset(new_array + reserved_mem_count, 0, memset_size);
103
104 reserved_mem = new_array;
105 }
106
107 static void __init fdt_init_reserved_mem_node(struct reserved_mem *rmem);
108 /*
109 * fdt_reserved_mem_save_node() - save fdt node for second pass initialization
110 */
fdt_reserved_mem_save_node(unsigned long node,const char * uname,phys_addr_t base,phys_addr_t size)111 static void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
112 phys_addr_t base, phys_addr_t size)
113 {
114 struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
115
116 if (reserved_mem_count == total_reserved_mem_cnt) {
117 pr_err("not enough space for all defined regions.\n");
118 return;
119 }
120
121 rmem->fdt_node = node;
122 rmem->name = uname;
123 rmem->base = base;
124 rmem->size = size;
125
126 /* Call the region specific initialization function */
127 fdt_init_reserved_mem_node(rmem);
128
129 reserved_mem_count++;
130 }
131
early_init_dt_reserve_memory(phys_addr_t base,phys_addr_t size,bool nomap)132 static int __init early_init_dt_reserve_memory(phys_addr_t base,
133 phys_addr_t size, bool nomap)
134 {
135 if (nomap) {
136 /*
137 * If the memory is already reserved (by another region), we
138 * should not allow it to be marked nomap, but don't worry
139 * if the region isn't memory as it won't be mapped.
140 */
141 if (memblock_overlaps_region(&memblock.memory, base, size) &&
142 memblock_is_region_reserved(base, size))
143 return -EBUSY;
144
145 return memblock_mark_nomap(base, size);
146 }
147 return memblock_reserve(base, size);
148 }
149
150 /*
151 * __reserved_mem_reserve_reg() - reserve all memory described in 'reg' property
152 */
__reserved_mem_reserve_reg(unsigned long node,const char * uname)153 static int __init __reserved_mem_reserve_reg(unsigned long node,
154 const char *uname)
155 {
156 phys_addr_t base, size;
157 int i, len;
158 const __be32 *prop;
159 bool nomap, default_cma;
160
161 prop = of_flat_dt_get_addr_size_prop(node, "reg", &len);
162 if (!prop)
163 return -ENOENT;
164
165 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
166 default_cma = of_get_flat_dt_prop(node, "linux,cma-default", NULL);
167
168 if (default_cma && cma_skip_dt_default_reserved_mem()) {
169 pr_err("Skipping dt linux,cma-default for \"cma=\" kernel param.\n");
170 return -EINVAL;
171 }
172
173 for (i = 0; i < len; i++) {
174 u64 b, s;
175
176 of_flat_dt_read_addr_size(prop, i, &b, &s);
177
178 base = b;
179 size = s;
180
181 if (size && early_init_dt_reserve_memory(base, size, nomap) == 0) {
182 /* Architecture specific contiguous memory fixup. */
183 if (of_flat_dt_is_compatible(node, "shared-dma-pool") &&
184 of_get_flat_dt_prop(node, "reusable", NULL))
185 dma_contiguous_early_fixup(base, size);
186 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n",
187 uname, &base, (unsigned long)(size / SZ_1M));
188 } else {
189 pr_err("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n",
190 uname, &base, (unsigned long)(size / SZ_1M));
191 }
192 }
193 return 0;
194 }
195
196 /*
197 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
198 * in /reserved-memory matches the values supported by the current implementation,
199 * also check if ranges property has been provided
200 */
__reserved_mem_check_root(unsigned long node)201 static int __init __reserved_mem_check_root(unsigned long node)
202 {
203 const __be32 *prop;
204
205 prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
206 if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
207 return -EINVAL;
208
209 prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
210 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
211 return -EINVAL;
212
213 prop = of_get_flat_dt_prop(node, "ranges", NULL);
214 if (!prop)
215 return -EINVAL;
216 return 0;
217 }
218
219 static void __init __rmem_check_for_overlap(void);
220
221 /**
222 * fdt_scan_reserved_mem_reg_nodes() - Store info for the "reg" defined
223 * reserved memory regions.
224 *
225 * This function is used to scan through the DT and store the
226 * information for the reserved memory regions that are defined using
227 * the "reg" property. The region node number, name, base address, and
228 * size are all stored in the reserved_mem array by calling the
229 * fdt_reserved_mem_save_node() function.
230 */
fdt_scan_reserved_mem_reg_nodes(void)231 void __init fdt_scan_reserved_mem_reg_nodes(void)
232 {
233 const void *fdt = initial_boot_params;
234 phys_addr_t base, size;
235 int node, child;
236
237 if (!fdt)
238 return;
239
240 node = fdt_path_offset(fdt, "/reserved-memory");
241 if (node < 0) {
242 pr_info("Reserved memory: No reserved-memory node in the DT\n");
243 return;
244 }
245
246 /* Attempt dynamic allocation of a new reserved_mem array */
247 alloc_reserved_mem_array();
248
249 if (__reserved_mem_check_root(node)) {
250 pr_err("Reserved memory: unsupported node format, ignoring\n");
251 return;
252 }
253
254 fdt_for_each_subnode(child, fdt, node) {
255 const char *uname;
256 bool default_cma = of_get_flat_dt_prop(child, "linux,cma-default", NULL);
257 u64 b, s;
258
259 if (!of_fdt_device_is_available(fdt, child))
260 continue;
261 if (default_cma && cma_skip_dt_default_reserved_mem())
262 continue;
263
264 if (!of_flat_dt_get_addr_size(child, "reg", &b, &s))
265 continue;
266
267 base = b;
268 size = s;
269
270 if (size) {
271 uname = fdt_get_name(fdt, child, NULL);
272 fdt_reserved_mem_save_node(child, uname, base, size);
273 }
274 }
275
276 /* check for overlapping reserved regions */
277 __rmem_check_for_overlap();
278 }
279
280 static int __init __reserved_mem_alloc_size(unsigned long node, const char *uname);
281
282 /*
283 * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
284 */
fdt_scan_reserved_mem(void)285 int __init fdt_scan_reserved_mem(void)
286 {
287 int node, child;
288 int dynamic_nodes_cnt = 0, count = 0;
289 int dynamic_nodes[MAX_RESERVED_REGIONS];
290 const void *fdt = initial_boot_params;
291
292 node = fdt_path_offset(fdt, "/reserved-memory");
293 if (node < 0)
294 return -ENODEV;
295
296 if (__reserved_mem_check_root(node) != 0) {
297 pr_err("Reserved memory: unsupported node format, ignoring\n");
298 return -EINVAL;
299 }
300
301 fdt_for_each_subnode(child, fdt, node) {
302 const char *uname;
303 int err;
304
305 if (!of_fdt_device_is_available(fdt, child))
306 continue;
307
308 uname = fdt_get_name(fdt, child, NULL);
309
310 err = __reserved_mem_reserve_reg(child, uname);
311 if (!err)
312 count++;
313 /*
314 * Save the nodes for the dynamically-placed regions
315 * into an array which will be used for allocation right
316 * after all the statically-placed regions are reserved
317 * or marked as no-map. This is done to avoid dynamically
318 * allocating from one of the statically-placed regions.
319 */
320 if (err == -ENOENT && of_get_flat_dt_prop(child, "size", NULL)) {
321 dynamic_nodes[dynamic_nodes_cnt] = child;
322 dynamic_nodes_cnt++;
323 }
324 }
325 for (int i = 0; i < dynamic_nodes_cnt; i++) {
326 const char *uname;
327 int err;
328
329 child = dynamic_nodes[i];
330 uname = fdt_get_name(fdt, child, NULL);
331 err = __reserved_mem_alloc_size(child, uname);
332 if (!err)
333 count++;
334 }
335 total_reserved_mem_cnt = count;
336 return 0;
337 }
338
339 /*
340 * __reserved_mem_alloc_in_range() - allocate reserved memory described with
341 * 'alloc-ranges'. Choose bottom-up/top-down depending on nearby existing
342 * reserved regions to keep the reserved memory contiguous if possible.
343 */
__reserved_mem_alloc_in_range(phys_addr_t size,phys_addr_t align,phys_addr_t start,phys_addr_t end,bool nomap,phys_addr_t * res_base)344 static int __init __reserved_mem_alloc_in_range(phys_addr_t size,
345 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
346 phys_addr_t *res_base)
347 {
348 bool prev_bottom_up = memblock_bottom_up();
349 bool bottom_up = false, top_down = false;
350 int ret, i;
351
352 for (i = 0; i < reserved_mem_count; i++) {
353 struct reserved_mem *rmem = &reserved_mem[i];
354
355 /* Skip regions that were not reserved yet */
356 if (rmem->size == 0)
357 continue;
358
359 /*
360 * If range starts next to an existing reservation, use bottom-up:
361 * |....RRRR................RRRRRRRR..............|
362 * --RRRR------
363 */
364 if (start >= rmem->base && start <= (rmem->base + rmem->size))
365 bottom_up = true;
366
367 /*
368 * If range ends next to an existing reservation, use top-down:
369 * |....RRRR................RRRRRRRR..............|
370 * -------RRRR-----
371 */
372 if (end >= rmem->base && end <= (rmem->base + rmem->size))
373 top_down = true;
374 }
375
376 /* Change setting only if either bottom-up or top-down was selected */
377 if (bottom_up != top_down)
378 memblock_set_bottom_up(bottom_up);
379
380 ret = early_init_dt_alloc_reserved_memory_arch(size, align,
381 start, end, nomap, res_base);
382
383 /* Restore old setting if needed */
384 if (bottom_up != top_down)
385 memblock_set_bottom_up(prev_bottom_up);
386
387 return ret;
388 }
389
390 /*
391 * __reserved_mem_alloc_size() - allocate reserved memory described by
392 * 'size', 'alignment' and 'alloc-ranges' properties.
393 */
__reserved_mem_alloc_size(unsigned long node,const char * uname)394 static int __init __reserved_mem_alloc_size(unsigned long node, const char *uname)
395 {
396 phys_addr_t start = 0, end = 0;
397 phys_addr_t base = 0, align = 0, size;
398 int i, len;
399 const __be32 *prop;
400 bool nomap, default_cma;
401 int ret;
402
403 prop = of_get_flat_dt_prop(node, "size", &len);
404 if (!prop)
405 return -EINVAL;
406
407 if (len != dt_root_size_cells * sizeof(__be32)) {
408 pr_err("invalid size property in '%s' node.\n", uname);
409 return -EINVAL;
410 }
411 size = dt_mem_next_cell(dt_root_size_cells, &prop);
412
413 prop = of_get_flat_dt_prop(node, "alignment", &len);
414 if (prop) {
415 if (len != dt_root_addr_cells * sizeof(__be32)) {
416 pr_err("invalid alignment property in '%s' node.\n",
417 uname);
418 return -EINVAL;
419 }
420 align = dt_mem_next_cell(dt_root_addr_cells, &prop);
421 }
422
423 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
424 default_cma = of_get_flat_dt_prop(node, "linux,cma-default", NULL);
425
426 if (default_cma && cma_skip_dt_default_reserved_mem()) {
427 pr_err("Skipping dt linux,cma-default for \"cma=\" kernel param.\n");
428 return -EINVAL;
429 }
430
431 /* Need adjust the alignment to satisfy the CMA requirement */
432 if (IS_ENABLED(CONFIG_CMA)
433 && of_flat_dt_is_compatible(node, "shared-dma-pool")
434 && of_get_flat_dt_prop(node, "reusable", NULL)
435 && !nomap)
436 align = max_t(phys_addr_t, align, CMA_MIN_ALIGNMENT_BYTES);
437
438 prop = of_flat_dt_get_addr_size_prop(node, "alloc-ranges", &len);
439 if (prop) {
440 for (i = 0; i < len; i++) {
441 u64 b, s;
442
443 of_flat_dt_read_addr_size(prop, i, &b, &s);
444
445 start = b;
446 end = b + s;
447
448 base = 0;
449 ret = __reserved_mem_alloc_in_range(size, align,
450 start, end, nomap, &base);
451 if (ret == 0) {
452 pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
453 uname, &base,
454 (unsigned long)(size / SZ_1M));
455 break;
456 }
457 }
458 } else {
459 ret = early_init_dt_alloc_reserved_memory_arch(size, align,
460 0, 0, nomap, &base);
461 if (ret == 0)
462 pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
463 uname, &base, (unsigned long)(size / SZ_1M));
464 }
465
466 if (base == 0) {
467 pr_err("failed to allocate memory for node '%s': size %lu MiB\n",
468 uname, (unsigned long)(size / SZ_1M));
469 return -ENOMEM;
470 }
471 /* Architecture specific contiguous memory fixup. */
472 if (of_flat_dt_is_compatible(node, "shared-dma-pool") &&
473 of_get_flat_dt_prop(node, "reusable", NULL))
474 dma_contiguous_early_fixup(base, size);
475 /* Save region in the reserved_mem array */
476 fdt_reserved_mem_save_node(node, uname, base, size);
477 return 0;
478 }
479
480 static const struct of_device_id __rmem_of_table_sentinel
481 __used __section("__reservedmem_of_table_end");
482
483 /*
484 * __reserved_mem_init_node() - call region specific reserved memory init code
485 */
__reserved_mem_init_node(struct reserved_mem * rmem)486 static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
487 {
488 extern const struct of_device_id __reservedmem_of_table[];
489 const struct of_device_id *i;
490 int ret = -ENOENT;
491
492 for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
493 reservedmem_of_init_fn initfn = i->data;
494 const char *compat = i->compatible;
495
496 if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
497 continue;
498
499 ret = initfn(rmem);
500 if (ret == 0) {
501 pr_info("initialized node %s, compatible id %s\n",
502 rmem->name, compat);
503 break;
504 }
505 }
506 return ret;
507 }
508
__rmem_cmp(const void * a,const void * b)509 static int __init __rmem_cmp(const void *a, const void *b)
510 {
511 const struct reserved_mem *ra = a, *rb = b;
512
513 if (ra->base < rb->base)
514 return -1;
515
516 if (ra->base > rb->base)
517 return 1;
518
519 /*
520 * Put the dynamic allocations (address == 0, size == 0) before static
521 * allocations at address 0x0 so that overlap detection works
522 * correctly.
523 */
524 if (ra->size < rb->size)
525 return -1;
526 if (ra->size > rb->size)
527 return 1;
528
529 if (ra->fdt_node < rb->fdt_node)
530 return -1;
531 if (ra->fdt_node > rb->fdt_node)
532 return 1;
533
534 return 0;
535 }
536
__rmem_check_for_overlap(void)537 static void __init __rmem_check_for_overlap(void)
538 {
539 int i;
540
541 if (reserved_mem_count < 2)
542 return;
543
544 sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]),
545 __rmem_cmp, NULL);
546 for (i = 0; i < reserved_mem_count - 1; i++) {
547 struct reserved_mem *this, *next;
548
549 this = &reserved_mem[i];
550 next = &reserved_mem[i + 1];
551
552 if (this->base + this->size > next->base) {
553 phys_addr_t this_end, next_end;
554
555 this_end = this->base + this->size;
556 next_end = next->base + next->size;
557 pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
558 this->name, &this->base, &this_end,
559 next->name, &next->base, &next_end);
560 }
561 }
562 }
563
564 /**
565 * fdt_init_reserved_mem_node() - Initialize a reserved memory region
566 * @rmem: reserved_mem struct of the memory region to be initialized.
567 *
568 * This function is used to call the region specific initialization
569 * function for a reserved memory region.
570 */
fdt_init_reserved_mem_node(struct reserved_mem * rmem)571 static void __init fdt_init_reserved_mem_node(struct reserved_mem *rmem)
572 {
573 unsigned long node = rmem->fdt_node;
574 int err = 0;
575 bool nomap;
576
577 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
578
579 err = __reserved_mem_init_node(rmem);
580 if (err != 0 && err != -ENOENT) {
581 pr_info("node %s compatible matching fail\n", rmem->name);
582 if (nomap)
583 memblock_clear_nomap(rmem->base, rmem->size);
584 else
585 memblock_phys_free(rmem->base, rmem->size);
586 } else {
587 phys_addr_t end = rmem->base + rmem->size - 1;
588 bool reusable =
589 (of_get_flat_dt_prop(node, "reusable", NULL)) != NULL;
590
591 pr_info("%pa..%pa (%lu KiB) %s %s %s\n",
592 &rmem->base, &end, (unsigned long)(rmem->size / SZ_1K),
593 nomap ? "nomap" : "map",
594 reusable ? "reusable" : "non-reusable",
595 rmem->name ? rmem->name : "unknown");
596 }
597 }
598
599 struct rmem_assigned_device {
600 struct device *dev;
601 struct reserved_mem *rmem;
602 struct list_head list;
603 };
604
605 static LIST_HEAD(of_rmem_assigned_device_list);
606 static DEFINE_MUTEX(of_rmem_assigned_device_mutex);
607
608 /**
609 * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
610 * given device
611 * @dev: Pointer to the device to configure
612 * @np: Pointer to the device_node with 'reserved-memory' property
613 * @idx: Index of selected region
614 *
615 * This function assigns respective DMA-mapping operations based on reserved
616 * memory region specified by 'memory-region' property in @np node to the @dev
617 * device. When driver needs to use more than one reserved memory region, it
618 * should allocate child devices and initialize regions by name for each of
619 * child device.
620 *
621 * Returns error code or zero on success.
622 */
of_reserved_mem_device_init_by_idx(struct device * dev,struct device_node * np,int idx)623 int of_reserved_mem_device_init_by_idx(struct device *dev,
624 struct device_node *np, int idx)
625 {
626 struct rmem_assigned_device *rd;
627 struct device_node *target;
628 struct reserved_mem *rmem;
629 int ret;
630
631 if (!np || !dev)
632 return -EINVAL;
633
634 target = of_parse_phandle(np, "memory-region", idx);
635 if (!target)
636 return -ENODEV;
637
638 if (!of_device_is_available(target)) {
639 of_node_put(target);
640 return 0;
641 }
642
643 rmem = of_reserved_mem_lookup(target);
644 of_node_put(target);
645
646 if (!rmem || !rmem->ops || !rmem->ops->device_init)
647 return -EINVAL;
648
649 rd = kmalloc_obj(struct rmem_assigned_device);
650 if (!rd)
651 return -ENOMEM;
652
653 ret = rmem->ops->device_init(rmem, dev);
654 if (ret == 0) {
655 rd->dev = dev;
656 rd->rmem = rmem;
657
658 mutex_lock(&of_rmem_assigned_device_mutex);
659 list_add(&rd->list, &of_rmem_assigned_device_list);
660 mutex_unlock(&of_rmem_assigned_device_mutex);
661
662 dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
663 } else {
664 kfree(rd);
665 }
666
667 return ret;
668 }
669 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx);
670
671 /**
672 * of_reserved_mem_device_init_by_name() - assign named reserved memory region
673 * to given device
674 * @dev: pointer to the device to configure
675 * @np: pointer to the device node with 'memory-region' property
676 * @name: name of the selected memory region
677 *
678 * Returns: 0 on success or a negative error-code on failure.
679 */
of_reserved_mem_device_init_by_name(struct device * dev,struct device_node * np,const char * name)680 int of_reserved_mem_device_init_by_name(struct device *dev,
681 struct device_node *np,
682 const char *name)
683 {
684 int idx = of_property_match_string(np, "memory-region-names", name);
685
686 return of_reserved_mem_device_init_by_idx(dev, np, idx);
687 }
688 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_name);
689
690 /**
691 * of_reserved_mem_device_release() - release reserved memory device structures
692 * @dev: Pointer to the device to deconfigure
693 *
694 * This function releases structures allocated for memory region handling for
695 * the given device.
696 */
of_reserved_mem_device_release(struct device * dev)697 void of_reserved_mem_device_release(struct device *dev)
698 {
699 struct rmem_assigned_device *rd, *tmp;
700 LIST_HEAD(release_list);
701
702 mutex_lock(&of_rmem_assigned_device_mutex);
703 list_for_each_entry_safe(rd, tmp, &of_rmem_assigned_device_list, list) {
704 if (rd->dev == dev)
705 list_move_tail(&rd->list, &release_list);
706 }
707 mutex_unlock(&of_rmem_assigned_device_mutex);
708
709 list_for_each_entry_safe(rd, tmp, &release_list, list) {
710 if (rd->rmem && rd->rmem->ops && rd->rmem->ops->device_release)
711 rd->rmem->ops->device_release(rd->rmem, dev);
712
713 kfree(rd);
714 }
715 }
716 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
717
718 /**
719 * of_reserved_mem_lookup() - acquire reserved_mem from a device node
720 * @np: node pointer of the desired reserved-memory region
721 *
722 * This function allows drivers to acquire a reference to the reserved_mem
723 * struct based on a device node handle.
724 *
725 * Returns a reserved_mem reference, or NULL on error.
726 */
of_reserved_mem_lookup(struct device_node * np)727 struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
728 {
729 const char *name;
730 int i;
731
732 if (!np->full_name)
733 return NULL;
734
735 name = kbasename(np->full_name);
736 for (i = 0; i < reserved_mem_count; i++)
737 if (!strcmp(reserved_mem[i].name, name))
738 return &reserved_mem[i];
739
740 return NULL;
741 }
742 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);
743
744 /**
745 * of_reserved_mem_region_to_resource() - Get a reserved memory region as a resource
746 * @np: node containing 'memory-region' property
747 * @idx: index of 'memory-region' property to lookup
748 * @res: Pointer to a struct resource to fill in with reserved region
749 *
750 * This function allows drivers to lookup a node's 'memory-region' property
751 * entries by index and return a struct resource for the entry.
752 *
753 * Returns 0 on success with @res filled in. Returns -ENODEV if 'memory-region'
754 * is missing or unavailable, -EINVAL for any other error.
755 */
of_reserved_mem_region_to_resource(const struct device_node * np,unsigned int idx,struct resource * res)756 int of_reserved_mem_region_to_resource(const struct device_node *np,
757 unsigned int idx, struct resource *res)
758 {
759 struct reserved_mem *rmem;
760
761 if (!np)
762 return -EINVAL;
763
764 struct device_node *target __free(device_node) = of_parse_phandle(np, "memory-region", idx);
765 if (!target || !of_device_is_available(target))
766 return -ENODEV;
767
768 rmem = of_reserved_mem_lookup(target);
769 if (!rmem)
770 return -EINVAL;
771
772 resource_set_range(res, rmem->base, rmem->size);
773 res->flags = IORESOURCE_MEM;
774 res->name = rmem->name;
775 return 0;
776 }
777 EXPORT_SYMBOL_GPL(of_reserved_mem_region_to_resource);
778
779 /**
780 * of_reserved_mem_region_to_resource_byname() - Get a reserved memory region as a resource
781 * @np: node containing 'memory-region' property
782 * @name: name of 'memory-region' property entry to lookup
783 * @res: Pointer to a struct resource to fill in with reserved region
784 *
785 * This function allows drivers to lookup a node's 'memory-region' property
786 * entries by name and return a struct resource for the entry.
787 *
788 * Returns 0 on success with @res filled in, or a negative error-code on
789 * failure.
790 */
of_reserved_mem_region_to_resource_byname(const struct device_node * np,const char * name,struct resource * res)791 int of_reserved_mem_region_to_resource_byname(const struct device_node *np,
792 const char *name,
793 struct resource *res)
794 {
795 int idx;
796
797 if (!name)
798 return -EINVAL;
799
800 idx = of_property_match_string(np, "memory-region-names", name);
801 if (idx < 0)
802 return idx;
803
804 return of_reserved_mem_region_to_resource(np, idx, res);
805 }
806 EXPORT_SYMBOL_GPL(of_reserved_mem_region_to_resource_byname);
807
808 /**
809 * of_reserved_mem_region_count() - Return the number of 'memory-region' entries
810 * @np: node containing 'memory-region' property
811 *
812 * This function allows drivers to retrieve the number of entries for a node's
813 * 'memory-region' property.
814 *
815 * Returns the number of entries on success, or negative error code on a
816 * malformed property.
817 */
of_reserved_mem_region_count(const struct device_node * np)818 int of_reserved_mem_region_count(const struct device_node *np)
819 {
820 return of_count_phandle_with_args(np, "memory-region", NULL);
821 }
822 EXPORT_SYMBOL_GPL(of_reserved_mem_region_count);
823