• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/of.h>
16 #include <linux/of_fdt.h>
17 #include <linux/of_platform.h>
18 #include <linux/mm.h>
19 #include <linux/sizes.h>
20 #include <linux/of_reserved_mem.h>
21 #include <linux/sort.h>
22 #include <linux/slab.h>
23 #include <linux/memblock.h>
24 
25 #if defined(CONFIG_DEBUG_FS)
26 #include <linux/debugfs.h>
27 #include <linux/seq_file.h>
28 #endif
29 
30 #define MAX_RESERVED_REGIONS	64
31 static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
32 static int reserved_mem_count;
33 #if defined(CONFIG_DEBUG_FS)
34 #define DT_RESERVED_MEM "dt_reserved_mem"
35 static int dynamic_reserved_mem_count;
36 static const char *dynamic_reserved_mem_array[MAX_RESERVED_REGIONS];
37 static int cma_reserved_mem_count;
38 static const char *cma_reserved_mem_array[MAX_RESERVED_REGIONS];
39 #endif
40 
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)41 static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
42 	phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
43 	phys_addr_t *res_base)
44 {
45 	phys_addr_t base;
46 
47 	end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
48 	align = !align ? SMP_CACHE_BYTES : align;
49 	base = memblock_find_in_range(start, end, size, align);
50 	if (!base)
51 		return -ENOMEM;
52 
53 	*res_base = base;
54 	if (nomap)
55 		return memblock_remove(base, size);
56 
57 	return memblock_reserve(base, size);
58 }
59 
60 /**
61  * fdt_reserved_mem_save_node() - save fdt node for second pass initialization
62  */
fdt_reserved_mem_save_node(unsigned long node,const char * uname,phys_addr_t base,phys_addr_t size)63 void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
64 				      phys_addr_t base, phys_addr_t size)
65 {
66 	struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
67 
68 	if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) {
69 		pr_err("not enough space for all defined regions.\n");
70 		return;
71 	}
72 
73 	rmem->fdt_node = node;
74 	rmem->name = uname;
75 	rmem->base = base;
76 	rmem->size = size;
77 #if defined(CONFIG_DEBUG_FS)
78 	if ((of_get_flat_dt_prop(node, "reusable", NULL))
79 		&& (cma_reserved_mem_count < MAX_RESERVED_REGIONS)) {
80 		cma_reserved_mem_array[cma_reserved_mem_count] = uname;
81 		cma_reserved_mem_count++;
82 	}
83 #endif
84 
85 	reserved_mem_count++;
86 	return;
87 }
88 
89 /**
90  * __reserved_mem_alloc_size() - allocate reserved memory described by
91  *	'size', 'alignment'  and 'alloc-ranges' properties.
92  */
__reserved_mem_alloc_size(unsigned long node,const char * uname,phys_addr_t * res_base,phys_addr_t * res_size)93 static int __init __reserved_mem_alloc_size(unsigned long node,
94 	const char *uname, phys_addr_t *res_base, phys_addr_t *res_size)
95 {
96 	int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
97 	phys_addr_t start = 0, end = 0;
98 	phys_addr_t base = 0, align = 0, size;
99 	int len;
100 	const __be32 *prop;
101 	bool nomap;
102 	int ret;
103 
104 	prop = of_get_flat_dt_prop(node, "size", &len);
105 	if (!prop)
106 		return -EINVAL;
107 
108 	if (len != dt_root_size_cells * sizeof(__be32)) {
109 		pr_err("invalid size property in '%s' node.\n", uname);
110 		return -EINVAL;
111 	}
112 	size = dt_mem_next_cell(dt_root_size_cells, &prop);
113 
114 	prop = of_get_flat_dt_prop(node, "alignment", &len);
115 	if (prop) {
116 		if (len != dt_root_addr_cells * sizeof(__be32)) {
117 			pr_err("invalid alignment property in '%s' node.\n",
118 				uname);
119 			return -EINVAL;
120 		}
121 		align = dt_mem_next_cell(dt_root_addr_cells, &prop);
122 	}
123 
124 	nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
125 
126 	/* Need adjust the alignment to satisfy the CMA requirement */
127 	if (IS_ENABLED(CONFIG_CMA)
128 	    && of_flat_dt_is_compatible(node, "shared-dma-pool")
129 	    && of_get_flat_dt_prop(node, "reusable", NULL)
130 	    && !nomap) {
131 		unsigned long order =
132 			max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
133 
134 		align = max(align, (phys_addr_t)PAGE_SIZE << order);
135 	}
136 
137 	prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
138 	if (prop) {
139 
140 		if (len % t_len != 0) {
141 			pr_err("invalid alloc-ranges property in '%s', skipping node.\n",
142 			       uname);
143 			return -EINVAL;
144 		}
145 
146 		base = 0;
147 
148 		while (len > 0) {
149 			start = dt_mem_next_cell(dt_root_addr_cells, &prop);
150 			end = start + dt_mem_next_cell(dt_root_size_cells,
151 						       &prop);
152 
153 			ret = early_init_dt_alloc_reserved_memory_arch(size,
154 					align, start, end, nomap, &base);
155 			if (ret == 0) {
156 				pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
157 					uname, &base,
158 					(unsigned long)(size / SZ_1M));
159 				break;
160 			}
161 			len -= t_len;
162 		}
163 
164 	} else {
165 		ret = early_init_dt_alloc_reserved_memory_arch(size, align,
166 							0, 0, nomap, &base);
167 		if (ret == 0)
168 			pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
169 				uname, &base, (unsigned long)(size / SZ_1M));
170 	}
171 
172 	if (base == 0) {
173 		pr_info("failed to allocate memory for node '%s'\n", uname);
174 		return -ENOMEM;
175 	}
176 
177 #if defined(CONFIG_DEBUG_FS)
178 	if (dynamic_reserved_mem_count < MAX_RESERVED_REGIONS) {
179 		dynamic_reserved_mem_array[dynamic_reserved_mem_count] = uname;
180 		dynamic_reserved_mem_count++;
181 	}
182 #endif
183 
184 	*res_base = base;
185 	*res_size = size;
186 
187 	return 0;
188 }
189 
190 static const struct of_device_id __rmem_of_table_sentinel
191 	__used __section("__reservedmem_of_table_end");
192 
193 /**
194  * __reserved_mem_init_node() - call region specific reserved memory init code
195  */
__reserved_mem_init_node(struct reserved_mem * rmem)196 static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
197 {
198 	extern const struct of_device_id __reservedmem_of_table[];
199 	const struct of_device_id *i;
200 	int ret = -ENOENT;
201 
202 	for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
203 		reservedmem_of_init_fn initfn = i->data;
204 		const char *compat = i->compatible;
205 
206 		if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
207 			continue;
208 
209 		ret = initfn(rmem);
210 		if (ret == 0) {
211 			pr_info("initialized node %s, compatible id %s\n",
212 				rmem->name, compat);
213 			break;
214 		}
215 	}
216 	return ret;
217 }
218 
__rmem_cmp(const void * a,const void * b)219 static int __init __rmem_cmp(const void *a, const void *b)
220 {
221 	const struct reserved_mem *ra = a, *rb = b;
222 
223 	if (ra->base < rb->base)
224 		return -1;
225 
226 	if (ra->base > rb->base)
227 		return 1;
228 
229 	/*
230 	 * Put the dynamic allocations (address == 0, size == 0) before static
231 	 * allocations at address 0x0 so that overlap detection works
232 	 * correctly.
233 	 */
234 	if (ra->size < rb->size)
235 		return -1;
236 	if (ra->size > rb->size)
237 		return 1;
238 
239 	return 0;
240 }
241 
__rmem_check_for_overlap(void)242 static void __init __rmem_check_for_overlap(void)
243 {
244 	int i;
245 
246 	if (reserved_mem_count < 2)
247 		return;
248 
249 	sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]),
250 	     __rmem_cmp, NULL);
251 	for (i = 0; i < reserved_mem_count - 1; i++) {
252 		struct reserved_mem *this, *next;
253 
254 		this = &reserved_mem[i];
255 		next = &reserved_mem[i + 1];
256 
257 		if (this->base + this->size > next->base) {
258 			phys_addr_t this_end, next_end;
259 
260 			this_end = this->base + this->size;
261 			next_end = next->base + next->size;
262 			pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
263 			       this->name, &this->base, &this_end,
264 			       next->name, &next->base, &next_end);
265 		}
266 	}
267 }
268 
269 /**
270  * fdt_init_reserved_mem() - allocate and init all saved reserved memory regions
271  */
fdt_init_reserved_mem(void)272 void __init fdt_init_reserved_mem(void)
273 {
274 	int i;
275 
276 	/* check for overlapping reserved regions */
277 	__rmem_check_for_overlap();
278 
279 	for (i = 0; i < reserved_mem_count; i++) {
280 		struct reserved_mem *rmem = &reserved_mem[i];
281 		unsigned long node = rmem->fdt_node;
282 		int len;
283 		const __be32 *prop;
284 		int err = 0;
285 		bool nomap;
286 
287 		nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
288 		prop = of_get_flat_dt_prop(node, "phandle", &len);
289 		if (!prop)
290 			prop = of_get_flat_dt_prop(node, "linux,phandle", &len);
291 		if (prop)
292 			rmem->phandle = of_read_number(prop, len/4);
293 
294 		if (rmem->size == 0)
295 			err = __reserved_mem_alloc_size(node, rmem->name,
296 						 &rmem->base, &rmem->size);
297 		if (err == 0) {
298 			err = __reserved_mem_init_node(rmem);
299 			if (err != 0 && err != -ENOENT) {
300 				pr_info("node %s compatible matching fail\n",
301 					rmem->name);
302 				memblock_free(rmem->base, rmem->size);
303 				if (nomap)
304 					memblock_add(rmem->base, rmem->size);
305 			}
306 		}
307 	}
308 }
309 
__find_rmem(struct device_node * node)310 static inline struct reserved_mem *__find_rmem(struct device_node *node)
311 {
312 	unsigned int i;
313 
314 	if (!node->phandle)
315 		return NULL;
316 
317 	for (i = 0; i < reserved_mem_count; i++)
318 		if (reserved_mem[i].phandle == node->phandle)
319 			return &reserved_mem[i];
320 	return NULL;
321 }
322 
323 struct rmem_assigned_device {
324 	struct device *dev;
325 	struct reserved_mem *rmem;
326 	struct list_head list;
327 };
328 
329 static LIST_HEAD(of_rmem_assigned_device_list);
330 static DEFINE_MUTEX(of_rmem_assigned_device_mutex);
331 
332 /**
333  * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
334  *					  given device
335  * @dev:	Pointer to the device to configure
336  * @np:		Pointer to the device_node with 'reserved-memory' property
337  * @idx:	Index of selected region
338  *
339  * This function assigns respective DMA-mapping operations based on reserved
340  * memory region specified by 'memory-region' property in @np node to the @dev
341  * device. When driver needs to use more than one reserved memory region, it
342  * should allocate child devices and initialize regions by name for each of
343  * child device.
344  *
345  * Returns error code or zero on success.
346  */
of_reserved_mem_device_init_by_idx(struct device * dev,struct device_node * np,int idx)347 int of_reserved_mem_device_init_by_idx(struct device *dev,
348 				       struct device_node *np, int idx)
349 {
350 	struct rmem_assigned_device *rd;
351 	struct device_node *target;
352 	struct reserved_mem *rmem;
353 	int ret;
354 
355 	if (!np || !dev)
356 		return -EINVAL;
357 
358 	target = of_parse_phandle(np, "memory-region", idx);
359 	if (!target)
360 		return -ENODEV;
361 
362 	if (!of_device_is_available(target)) {
363 		of_node_put(target);
364 		return 0;
365 	}
366 
367 	rmem = __find_rmem(target);
368 	of_node_put(target);
369 
370 	if (!rmem || !rmem->ops || !rmem->ops->device_init)
371 		return -EINVAL;
372 
373 	rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL);
374 	if (!rd)
375 		return -ENOMEM;
376 
377 	ret = rmem->ops->device_init(rmem, dev);
378 	if (ret == 0) {
379 		rd->dev = dev;
380 		rd->rmem = rmem;
381 
382 		mutex_lock(&of_rmem_assigned_device_mutex);
383 		list_add(&rd->list, &of_rmem_assigned_device_list);
384 		mutex_unlock(&of_rmem_assigned_device_mutex);
385 
386 		dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
387 	} else {
388 		kfree(rd);
389 	}
390 
391 	return ret;
392 }
393 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx);
394 
395 /**
396  * of_reserved_mem_device_init_by_name() - assign named reserved memory region
397  *					   to given device
398  * @dev: pointer to the device to configure
399  * @np: pointer to the device node with 'memory-region' property
400  * @name: name of the selected memory region
401  *
402  * Returns: 0 on success or a negative error-code on failure.
403  */
of_reserved_mem_device_init_by_name(struct device * dev,struct device_node * np,const char * name)404 int of_reserved_mem_device_init_by_name(struct device *dev,
405 					struct device_node *np,
406 					const char *name)
407 {
408 	int idx = of_property_match_string(np, "memory-region-names", name);
409 
410 	return of_reserved_mem_device_init_by_idx(dev, np, idx);
411 }
412 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_name);
413 
414 /**
415  * of_reserved_mem_device_release() - release reserved memory device structures
416  * @dev:	Pointer to the device to deconfigure
417  *
418  * This function releases structures allocated for memory region handling for
419  * the given device.
420  */
of_reserved_mem_device_release(struct device * dev)421 void of_reserved_mem_device_release(struct device *dev)
422 {
423 	struct rmem_assigned_device *rd, *tmp;
424 	LIST_HEAD(release_list);
425 
426 	mutex_lock(&of_rmem_assigned_device_mutex);
427 	list_for_each_entry_safe(rd, tmp, &of_rmem_assigned_device_list, list) {
428 		if (rd->dev == dev)
429 			list_move_tail(&rd->list, &release_list);
430 	}
431 	mutex_unlock(&of_rmem_assigned_device_mutex);
432 
433 	list_for_each_entry_safe(rd, tmp, &release_list, list) {
434 		if (rd->rmem && rd->rmem->ops && rd->rmem->ops->device_release)
435 			rd->rmem->ops->device_release(rd->rmem, dev);
436 
437 		kfree(rd);
438 	}
439 }
440 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
441 
442 /**
443  * of_reserved_mem_lookup() - acquire reserved_mem from a device node
444  * @np:		node pointer of the desired reserved-memory region
445  *
446  * This function allows drivers to acquire a reference to the reserved_mem
447  * struct based on a device node handle.
448  *
449  * Returns a reserved_mem reference, or NULL on error.
450  */
of_reserved_mem_lookup(struct device_node * np)451 struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
452 {
453 	const char *name;
454 	int i;
455 
456 	if (!np->full_name)
457 		return NULL;
458 
459 	name = kbasename(np->full_name);
460 	for (i = 0; i < reserved_mem_count; i++)
461 		if (!strcmp(reserved_mem[i].name, name))
462 			return &reserved_mem[i];
463 
464 	return NULL;
465 }
466 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);
467 
468 #if defined(CONFIG_DEBUG_FS)
dt_reserved_memory_debug_show(struct seq_file * m,void * private)469 static int dt_reserved_memory_debug_show(struct seq_file *m, void *private)
470 {
471 	struct reserved_mem *dt_reserved_mem = m->private;
472 	struct reserved_mem *rmem = NULL;
473 	int i = 0;
474 	int j = 0;
475 	int cma = 0;
476 	int dynamic = 0;
477 
478 	seq_printf(m, "  num [start            ....              end]      [size]"
479 		   "       [d/s]     [cma]       [name]\n");
480 
481 	for (i = 0; i < reserved_mem_count; i++) {
482 		cma = 0;
483 		dynamic = 0;
484 		rmem = &(dt_reserved_mem[i]);
485 
486 		/* find out dynamic reserved memory node  */
487 		for (j = 0; j < dynamic_reserved_mem_count; j++) {
488 			if (!strcmp(rmem->name, dynamic_reserved_mem_array[j])) {
489 				dynamic = 1;
490 				break;
491 			}
492 		}
493 
494 		/* find out cma reserved memory node */
495 		for (j = 0; j < cma_reserved_mem_count; j++) {
496 			if (!strcmp(rmem->name, cma_reserved_mem_array[j])) {
497 				cma = 1;
498 				break;
499 			}
500 		}
501 
502 		seq_printf(m, "%4d: [0x%016llx..0x%016llx]  %8llukB   %8s %8s        %8s\n",
503 				i,
504 				(unsigned long long)rmem->base,
505 				(unsigned long long)(rmem->base + rmem->size - 1),
506 				(unsigned long long)rmem->size / SZ_1K,
507 				(dynamic == 1) ? "d" : "s",
508 				(cma == 1) ? "y" : "n",
509 				rmem->name);
510 	}
511 
512 	return 0;
513 }
514 
dt_reserved_memory_debug_open(struct inode * inode,struct file * file)515 static int dt_reserved_memory_debug_open(struct inode *inode, struct file *file)
516 {
517 	return single_open(file, dt_reserved_memory_debug_show, inode->i_private);
518 }
519 
520 static const struct file_operations dt_reserved_memory_debug_fops = {
521 	.open = dt_reserved_memory_debug_open,
522 	.read = seq_read,
523 	.llseek = seq_lseek,
524 	.release = single_release,
525 };
526 
dt_reserved_memory_init_debugfs(void)527 static int __init dt_reserved_memory_init_debugfs(void)
528 {
529 	struct dentry *root = debugfs_create_dir(DT_RESERVED_MEM, NULL);
530 	if (!root)
531 		return -ENXIO;
532 	debugfs_create_file("dt_reserved_memory",
533 			    S_IRUGO,
534 			    root,
535 			    reserved_mem,
536 			    &dt_reserved_memory_debug_fops);
537 
538 	return 0;
539 }
540 __initcall(dt_reserved_memory_init_debugfs);
541 #endif /* CONFIG_DEBUG_FS */
542