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