xref: /openbmc/linux/drivers/of/of_reserved_mem.c (revision cce8e04c)
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 #define MAX_RESERVED_REGIONS	32
26 static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
27 static int reserved_mem_count;
28 
29 int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
30 	phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
31 	phys_addr_t *res_base)
32 {
33 	phys_addr_t base;
34 	/*
35 	 * We use __memblock_alloc_base() because memblock_alloc_base()
36 	 * panic()s on allocation failure.
37 	 */
38 	end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
39 	align = !align ? SMP_CACHE_BYTES : align;
40 	base = __memblock_alloc_base(size, align, end);
41 	if (!base)
42 		return -ENOMEM;
43 
44 	/*
45 	 * Check if the allocated region fits in to start..end window
46 	 */
47 	if (base < start) {
48 		memblock_free(base, size);
49 		return -ENOMEM;
50 	}
51 
52 	*res_base = base;
53 	if (nomap)
54 		return memblock_remove(base, size);
55 	return 0;
56 }
57 
58 /**
59  * res_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 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  * res_mem_alloc_size() - allocate reserved memory described by 'size', 'align'
82  *			  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 	int 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 	nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
106 
107 	prop = of_get_flat_dt_prop(node, "alignment", &len);
108 	if (prop) {
109 		if (len != dt_root_addr_cells * sizeof(__be32)) {
110 			pr_err("invalid alignment property in '%s' node.\n",
111 				uname);
112 			return -EINVAL;
113 		}
114 		align = dt_mem_next_cell(dt_root_addr_cells, &prop);
115 	}
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 	    && !of_get_flat_dt_prop(node, "no-map", NULL)) {
122 		unsigned long order =
123 			max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
124 
125 		align = max(align, (phys_addr_t)PAGE_SIZE << order);
126 	}
127 
128 	prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
129 	if (prop) {
130 
131 		if (len % t_len != 0) {
132 			pr_err("invalid alloc-ranges property in '%s', skipping node.\n",
133 			       uname);
134 			return -EINVAL;
135 		}
136 
137 		base = 0;
138 
139 		while (len > 0) {
140 			start = dt_mem_next_cell(dt_root_addr_cells, &prop);
141 			end = start + dt_mem_next_cell(dt_root_size_cells,
142 						       &prop);
143 
144 			ret = early_init_dt_alloc_reserved_memory_arch(size,
145 					align, start, end, nomap, &base);
146 			if (ret == 0) {
147 				pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
148 					uname, &base,
149 					(unsigned long)size / SZ_1M);
150 				break;
151 			}
152 			len -= t_len;
153 		}
154 
155 	} else {
156 		ret = early_init_dt_alloc_reserved_memory_arch(size, align,
157 							0, 0, nomap, &base);
158 		if (ret == 0)
159 			pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
160 				uname, &base, (unsigned long)size / SZ_1M);
161 	}
162 
163 	if (base == 0) {
164 		pr_info("failed to allocate memory for node '%s'\n", uname);
165 		return -ENOMEM;
166 	}
167 
168 	*res_base = base;
169 	*res_size = size;
170 
171 	return 0;
172 }
173 
174 static const struct of_device_id __rmem_of_table_sentinel
175 	__used __section(__reservedmem_of_table_end);
176 
177 /**
178  * res_mem_init_node() - call region specific reserved memory init code
179  */
180 static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
181 {
182 	extern const struct of_device_id __reservedmem_of_table[];
183 	const struct of_device_id *i;
184 
185 	for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
186 		reservedmem_of_init_fn initfn = i->data;
187 		const char *compat = i->compatible;
188 
189 		if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
190 			continue;
191 
192 		if (initfn(rmem) == 0) {
193 			pr_info("initialized node %s, compatible id %s\n",
194 				rmem->name, compat);
195 			return 0;
196 		}
197 	}
198 	return -ENOENT;
199 }
200 
201 static int __init __rmem_cmp(const void *a, const void *b)
202 {
203 	const struct reserved_mem *ra = a, *rb = b;
204 
205 	if (ra->base < rb->base)
206 		return -1;
207 
208 	if (ra->base > rb->base)
209 		return 1;
210 
211 	return 0;
212 }
213 
214 static void __init __rmem_check_for_overlap(void)
215 {
216 	int i;
217 
218 	if (reserved_mem_count < 2)
219 		return;
220 
221 	sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]),
222 	     __rmem_cmp, NULL);
223 	for (i = 0; i < reserved_mem_count - 1; i++) {
224 		struct reserved_mem *this, *next;
225 
226 		this = &reserved_mem[i];
227 		next = &reserved_mem[i + 1];
228 		if (!(this->base && next->base))
229 			continue;
230 		if (this->base + this->size > next->base) {
231 			phys_addr_t this_end, next_end;
232 
233 			this_end = this->base + this->size;
234 			next_end = next->base + next->size;
235 			pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
236 			       this->name, &this->base, &this_end,
237 			       next->name, &next->base, &next_end);
238 		}
239 	}
240 }
241 
242 /**
243  * fdt_init_reserved_mem - allocate and init all saved reserved memory regions
244  */
245 void __init fdt_init_reserved_mem(void)
246 {
247 	int i;
248 
249 	/* check for overlapping reserved regions */
250 	__rmem_check_for_overlap();
251 
252 	for (i = 0; i < reserved_mem_count; i++) {
253 		struct reserved_mem *rmem = &reserved_mem[i];
254 		unsigned long node = rmem->fdt_node;
255 		int len;
256 		const __be32 *prop;
257 		int err = 0;
258 
259 		prop = of_get_flat_dt_prop(node, "phandle", &len);
260 		if (!prop)
261 			prop = of_get_flat_dt_prop(node, "linux,phandle", &len);
262 		if (prop)
263 			rmem->phandle = of_read_number(prop, len/4);
264 
265 		if (rmem->size == 0)
266 			err = __reserved_mem_alloc_size(node, rmem->name,
267 						 &rmem->base, &rmem->size);
268 		if (err == 0)
269 			__reserved_mem_init_node(rmem);
270 	}
271 }
272 
273 static inline struct reserved_mem *__find_rmem(struct device_node *node)
274 {
275 	unsigned int i;
276 
277 	if (!node->phandle)
278 		return NULL;
279 
280 	for (i = 0; i < reserved_mem_count; i++)
281 		if (reserved_mem[i].phandle == node->phandle)
282 			return &reserved_mem[i];
283 	return NULL;
284 }
285 
286 struct rmem_assigned_device {
287 	struct device *dev;
288 	struct reserved_mem *rmem;
289 	struct list_head list;
290 };
291 
292 static LIST_HEAD(of_rmem_assigned_device_list);
293 static DEFINE_MUTEX(of_rmem_assigned_device_mutex);
294 
295 /**
296  * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
297  *					  given device
298  * @dev:	Pointer to the device to configure
299  * @np:		Pointer to the device_node with 'reserved-memory' property
300  * @idx:	Index of selected region
301  *
302  * This function assigns respective DMA-mapping operations based on reserved
303  * memory region specified by 'memory-region' property in @np node to the @dev
304  * device. When driver needs to use more than one reserved memory region, it
305  * should allocate child devices and initialize regions by name for each of
306  * child device.
307  *
308  * Returns error code or zero on success.
309  */
310 int of_reserved_mem_device_init_by_idx(struct device *dev,
311 				       struct device_node *np, int idx)
312 {
313 	struct rmem_assigned_device *rd;
314 	struct device_node *target;
315 	struct reserved_mem *rmem;
316 	int ret;
317 
318 	if (!np || !dev)
319 		return -EINVAL;
320 
321 	target = of_parse_phandle(np, "memory-region", idx);
322 	if (!target)
323 		return -ENODEV;
324 
325 	rmem = __find_rmem(target);
326 	of_node_put(target);
327 
328 	if (!rmem || !rmem->ops || !rmem->ops->device_init)
329 		return -EINVAL;
330 
331 	rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL);
332 	if (!rd)
333 		return -ENOMEM;
334 
335 	ret = rmem->ops->device_init(rmem, dev);
336 	if (ret == 0) {
337 		rd->dev = dev;
338 		rd->rmem = rmem;
339 
340 		mutex_lock(&of_rmem_assigned_device_mutex);
341 		list_add(&rd->list, &of_rmem_assigned_device_list);
342 		mutex_unlock(&of_rmem_assigned_device_mutex);
343 		/* ensure that dma_ops is set for virtual devices
344 		 * using reserved memory
345 		 */
346 		of_dma_configure(dev, np, true);
347 
348 		dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
349 	} else {
350 		kfree(rd);
351 	}
352 
353 	return ret;
354 }
355 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx);
356 
357 /**
358  * of_reserved_mem_device_release() - release reserved memory device structures
359  * @dev:	Pointer to the device to deconfigure
360  *
361  * This function releases structures allocated for memory region handling for
362  * the given device.
363  */
364 void of_reserved_mem_device_release(struct device *dev)
365 {
366 	struct rmem_assigned_device *rd;
367 	struct reserved_mem *rmem = NULL;
368 
369 	mutex_lock(&of_rmem_assigned_device_mutex);
370 	list_for_each_entry(rd, &of_rmem_assigned_device_list, list) {
371 		if (rd->dev == dev) {
372 			rmem = rd->rmem;
373 			list_del(&rd->list);
374 			kfree(rd);
375 			break;
376 		}
377 	}
378 	mutex_unlock(&of_rmem_assigned_device_mutex);
379 
380 	if (!rmem || !rmem->ops || !rmem->ops->device_release)
381 		return;
382 
383 	rmem->ops->device_release(rmem, dev);
384 }
385 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
386 
387 /**
388  * of_reserved_mem_lookup() - acquire reserved_mem from a device node
389  * @np:		node pointer of the desired reserved-memory region
390  *
391  * This function allows drivers to acquire a reference to the reserved_mem
392  * struct based on a device node handle.
393  *
394  * Returns a reserved_mem reference, or NULL on error.
395  */
396 struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
397 {
398 	const char *name;
399 	int i;
400 
401 	if (!np->full_name)
402 		return NULL;
403 
404 	name = kbasename(np->full_name);
405 	for (i = 0; i < reserved_mem_count; i++)
406 		if (!strcmp(reserved_mem[i].name, name))
407 			return &reserved_mem[i];
408 
409 	return NULL;
410 }
411 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);
412