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