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