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