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