xref: /linux/drivers/of/of_reserved_mem.c (revision 26ba30221c03364d6ed9910be8da4c1fd871b07b)
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 
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  */
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 
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  */
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  */
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 
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 
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  */
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  */
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  */
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  */
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  */
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  */
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  */
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  */
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.\n");
645 		return;
646 	}
647 
648 	rmem->name = uname;
649 	rmem->base = base;
650 	rmem->size = size;
651 
652 	nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
653 
654 	err = __reserved_mem_init_node(rmem, node);
655 	if (err != 0 && err != -ENODEV) {
656 		pr_info("node %s compatible matching fail\n", rmem->name);
657 		rmem->name = NULL;
658 
659 		if (nomap)
660 			memblock_clear_nomap(rmem->base, rmem->size);
661 		else
662 			memblock_phys_free(rmem->base, rmem->size);
663 		return;
664 	} else {
665 		phys_addr_t end = rmem->base + rmem->size - 1;
666 		bool reusable =
667 			(of_get_flat_dt_prop(node, "reusable", NULL)) != NULL;
668 
669 		pr_info("%pa..%pa (%lu KiB) %s %s %s\n",
670 			&rmem->base, &end, (unsigned long)(rmem->size / SZ_1K),
671 			nomap ? "nomap" : "map",
672 			reusable ? "reusable" : "non-reusable",
673 			rmem->name ? rmem->name : "unknown");
674 	}
675 
676 	reserved_mem_count++;
677 }
678 
679 struct rmem_assigned_device {
680 	struct device *dev;
681 	struct reserved_mem *rmem;
682 	struct list_head list;
683 };
684 
685 static LIST_HEAD(of_rmem_assigned_device_list);
686 static DEFINE_MUTEX(of_rmem_assigned_device_mutex);
687 
688 /**
689  * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
690  *					  given device
691  * @dev:	Pointer to the device to configure
692  * @np:		Pointer to the device_node with 'reserved-memory' property
693  * @idx:	Index of selected region
694  *
695  * This function assigns respective DMA-mapping operations based on reserved
696  * memory region specified by 'memory-region' property in @np node to the @dev
697  * device. When driver needs to use more than one reserved memory region, it
698  * should allocate child devices and initialize regions by name for each of
699  * child device.
700  *
701  * Returns error code or zero on success.
702  */
703 int of_reserved_mem_device_init_by_idx(struct device *dev,
704 				       struct device_node *np, int idx)
705 {
706 	struct rmem_assigned_device *rd;
707 	struct device_node *target;
708 	struct reserved_mem *rmem;
709 	int ret;
710 
711 	if (!np || !dev)
712 		return -EINVAL;
713 
714 	target = of_parse_phandle(np, "memory-region", idx);
715 	if (!target)
716 		return -ENODEV;
717 
718 	if (!of_device_is_available(target)) {
719 		of_node_put(target);
720 		return 0;
721 	}
722 
723 	rmem = of_reserved_mem_lookup(target);
724 	of_node_put(target);
725 
726 	if (!rmem || !rmem->ops || !rmem->ops->device_init)
727 		return -EINVAL;
728 
729 	rd = kmalloc_obj(struct rmem_assigned_device);
730 	if (!rd)
731 		return -ENOMEM;
732 
733 	ret = rmem->ops->device_init(rmem, dev);
734 	if (ret == 0) {
735 		rd->dev = dev;
736 		rd->rmem = rmem;
737 
738 		mutex_lock(&of_rmem_assigned_device_mutex);
739 		list_add(&rd->list, &of_rmem_assigned_device_list);
740 		mutex_unlock(&of_rmem_assigned_device_mutex);
741 
742 		dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
743 	} else {
744 		kfree(rd);
745 	}
746 
747 	return ret;
748 }
749 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx);
750 
751 /**
752  * of_reserved_mem_device_init_by_name() - assign named reserved memory region
753  *					   to given device
754  * @dev: pointer to the device to configure
755  * @np: pointer to the device node with 'memory-region' property
756  * @name: name of the selected memory region
757  *
758  * Returns: 0 on success or a negative error-code on failure.
759  */
760 int of_reserved_mem_device_init_by_name(struct device *dev,
761 					struct device_node *np,
762 					const char *name)
763 {
764 	int idx = of_property_match_string(np, "memory-region-names", name);
765 
766 	return of_reserved_mem_device_init_by_idx(dev, np, idx);
767 }
768 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_name);
769 
770 /**
771  * of_reserved_mem_device_release() - release reserved memory device structures
772  * @dev:	Pointer to the device to deconfigure
773  *
774  * This function releases structures allocated for memory region handling for
775  * the given device.
776  */
777 void of_reserved_mem_device_release(struct device *dev)
778 {
779 	struct rmem_assigned_device *rd, *tmp;
780 	LIST_HEAD(release_list);
781 
782 	mutex_lock(&of_rmem_assigned_device_mutex);
783 	list_for_each_entry_safe(rd, tmp, &of_rmem_assigned_device_list, list) {
784 		if (rd->dev == dev)
785 			list_move_tail(&rd->list, &release_list);
786 	}
787 	mutex_unlock(&of_rmem_assigned_device_mutex);
788 
789 	list_for_each_entry_safe(rd, tmp, &release_list, list) {
790 		if (rd->rmem && rd->rmem->ops && rd->rmem->ops->device_release)
791 			rd->rmem->ops->device_release(rd->rmem, dev);
792 
793 		kfree(rd);
794 	}
795 }
796 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
797 
798 /**
799  * of_reserved_mem_lookup() - acquire reserved_mem from a device node
800  * @np:		node pointer of the desired reserved-memory region
801  *
802  * This function allows drivers to acquire a reference to the reserved_mem
803  * struct based on a device node handle.
804  *
805  * Returns a reserved_mem reference, or NULL on error.
806  */
807 struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
808 {
809 	const char *name;
810 	int i;
811 
812 	if (!np->full_name)
813 		return NULL;
814 
815 	name = kbasename(np->full_name);
816 	for (i = 0; i < reserved_mem_count; i++)
817 		if (reserved_mem[i].name &&
818 		    !strcmp(reserved_mem[i].name, name))
819 			return &reserved_mem[i];
820 
821 	return NULL;
822 }
823 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);
824 
825 /**
826  * of_reserved_mem_region_to_resource() - Get a reserved memory region as a resource
827  * @np:		node containing 'memory-region' property
828  * @idx:	index of 'memory-region' property to lookup
829  * @res:	Pointer to a struct resource to fill in with reserved region
830  *
831  * This function allows drivers to lookup a node's 'memory-region' property
832  * entries by index and return a struct resource for the entry.
833  *
834  * Returns 0 on success with @res filled in. Returns -ENODEV if 'memory-region'
835  * is missing or unavailable, -EINVAL for any other error.
836  */
837 int of_reserved_mem_region_to_resource(const struct device_node *np,
838 				       unsigned int idx, struct resource *res)
839 {
840 	struct reserved_mem *rmem;
841 
842 	if (!np)
843 		return -EINVAL;
844 
845 	struct device_node *target __free(device_node) = of_parse_phandle(np, "memory-region", idx);
846 	if (!target || !of_device_is_available(target))
847 		return -ENODEV;
848 
849 	rmem = of_reserved_mem_lookup(target);
850 	if (!rmem)
851 		return -EINVAL;
852 
853 	resource_set_range(res, rmem->base, rmem->size);
854 	res->flags = IORESOURCE_MEM;
855 	res->name = rmem->name;
856 	return 0;
857 }
858 EXPORT_SYMBOL_GPL(of_reserved_mem_region_to_resource);
859 
860 /**
861  * of_reserved_mem_region_to_resource_byname() - Get a reserved memory region as a resource
862  * @np:		node containing 'memory-region' property
863  * @name:	name of 'memory-region' property entry to lookup
864  * @res:	Pointer to a struct resource to fill in with reserved region
865  *
866  * This function allows drivers to lookup a node's 'memory-region' property
867  * entries by name and return a struct resource for the entry.
868  *
869  * Returns 0 on success with @res filled in, or a negative error-code on
870  * failure.
871  */
872 int of_reserved_mem_region_to_resource_byname(const struct device_node *np,
873 					      const char *name,
874 					      struct resource *res)
875 {
876 	int idx;
877 
878 	if (!name)
879 		return -EINVAL;
880 
881 	idx = of_property_match_string(np, "memory-region-names", name);
882 	if (idx < 0)
883 		return idx;
884 
885 	return of_reserved_mem_region_to_resource(np, idx, res);
886 }
887 EXPORT_SYMBOL_GPL(of_reserved_mem_region_to_resource_byname);
888 
889 /**
890  * of_reserved_mem_region_count() - Return the number of 'memory-region' entries
891  * @np:		node containing 'memory-region' property
892  *
893  * This function allows drivers to retrieve the number of entries for a node's
894  * 'memory-region' property.
895  *
896  * Returns the number of entries on success, or negative error code on a
897  * malformed property.
898  */
899 int of_reserved_mem_region_count(const struct device_node *np)
900 {
901 	return of_count_phandle_with_args(np, "memory-region", NULL);
902 }
903 EXPORT_SYMBOL_GPL(of_reserved_mem_region_count);
904