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 128
30 static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
31 static int reserved_mem_count;
32
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)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 */
fdt_reserved_mem_save_node(unsigned long node,const char * uname,phys_addr_t base,phys_addr_t size)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_in_range() - allocate reserved memory described with
82 * 'alloc-ranges'. Choose bottom-up/top-down depending on nearby existing
83 * reserved regions to keep the reserved memory contiguous if possible.
84 */
__reserved_mem_alloc_in_range(phys_addr_t size,phys_addr_t align,phys_addr_t start,phys_addr_t end,bool nomap,phys_addr_t * res_base)85 static int __init __reserved_mem_alloc_in_range(phys_addr_t size,
86 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
87 phys_addr_t *res_base)
88 {
89 bool prev_bottom_up = memblock_bottom_up();
90 bool bottom_up = false, top_down = false;
91 int ret, i;
92
93 for (i = 0; i < reserved_mem_count; i++) {
94 struct reserved_mem *rmem = &reserved_mem[i];
95
96 /* Skip regions that were not reserved yet */
97 if (rmem->size == 0)
98 continue;
99
100 /*
101 * If range starts next to an existing reservation, use bottom-up:
102 * |....RRRR................RRRRRRRR..............|
103 * --RRRR------
104 */
105 if (start >= rmem->base && start <= (rmem->base + rmem->size))
106 bottom_up = true;
107
108 /*
109 * If range ends next to an existing reservation, use top-down:
110 * |....RRRR................RRRRRRRR..............|
111 * -------RRRR-----
112 */
113 if (end >= rmem->base && end <= (rmem->base + rmem->size))
114 top_down = true;
115 }
116
117 /* Change setting only if either bottom-up or top-down was selected */
118 if (bottom_up != top_down)
119 memblock_set_bottom_up(bottom_up);
120
121 ret = early_init_dt_alloc_reserved_memory_arch(size, align,
122 start, end, nomap, res_base);
123
124 /* Restore old setting if needed */
125 if (bottom_up != top_down)
126 memblock_set_bottom_up(prev_bottom_up);
127
128 return ret;
129 }
130
131 /*
132 * __reserved_mem_alloc_size() - allocate reserved memory described by
133 * 'size', 'alignment' and 'alloc-ranges' properties.
134 */
__reserved_mem_alloc_size(unsigned long node,const char * uname,phys_addr_t * res_base,phys_addr_t * res_size)135 static int __init __reserved_mem_alloc_size(unsigned long node,
136 const char *uname, phys_addr_t *res_base, phys_addr_t *res_size)
137 {
138 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
139 phys_addr_t start = 0, end = 0;
140 phys_addr_t base = 0, align = 0, size;
141 int len;
142 const __be32 *prop;
143 bool nomap;
144 int ret;
145
146 prop = of_get_flat_dt_prop(node, "size", &len);
147 if (!prop)
148 return -EINVAL;
149
150 if (len != dt_root_size_cells * sizeof(__be32)) {
151 pr_err("invalid size property in '%s' node.\n", uname);
152 return -EINVAL;
153 }
154 size = dt_mem_next_cell(dt_root_size_cells, &prop);
155
156 prop = of_get_flat_dt_prop(node, "alignment", &len);
157 if (prop) {
158 if (len != dt_root_addr_cells * sizeof(__be32)) {
159 pr_err("invalid alignment property in '%s' node.\n",
160 uname);
161 return -EINVAL;
162 }
163 align = dt_mem_next_cell(dt_root_addr_cells, &prop);
164 }
165
166 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
167
168 /* Need adjust the alignment to satisfy the CMA requirement */
169 if (IS_ENABLED(CONFIG_CMA)
170 && of_flat_dt_is_compatible(node, "shared-dma-pool")
171 && of_get_flat_dt_prop(node, "reusable", NULL)
172 && !nomap)
173 align = max_t(phys_addr_t, align, CMA_MIN_ALIGNMENT_BYTES);
174
175 prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
176 if (prop) {
177
178 if (len % t_len != 0) {
179 pr_err("invalid alloc-ranges property in '%s', skipping node.\n",
180 uname);
181 return -EINVAL;
182 }
183
184 base = 0;
185
186 while (len > 0) {
187 start = dt_mem_next_cell(dt_root_addr_cells, &prop);
188 end = start + dt_mem_next_cell(dt_root_size_cells,
189 &prop);
190
191 ret = __reserved_mem_alloc_in_range(size, align,
192 start, end, nomap, &base);
193 if (ret == 0) {
194 pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
195 uname, &base,
196 (unsigned long)(size / SZ_1M));
197 break;
198 }
199 len -= t_len;
200 }
201
202 } else {
203 ret = early_init_dt_alloc_reserved_memory_arch(size, align,
204 0, 0, nomap, &base);
205 if (ret == 0)
206 pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
207 uname, &base, (unsigned long)(size / SZ_1M));
208 }
209
210 if (base == 0) {
211 pr_err("failed to allocate memory for node '%s': size %lu MiB\n",
212 uname, (unsigned long)(size / SZ_1M));
213 return -ENOMEM;
214 }
215
216 *res_base = base;
217 *res_size = size;
218
219 return 0;
220 }
221
222 static const struct of_device_id __rmem_of_table_sentinel
223 __used __section("__reservedmem_of_table_end");
224
225 /*
226 * __reserved_mem_init_node() - call region specific reserved memory init code
227 */
__reserved_mem_init_node(struct reserved_mem * rmem)228 static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
229 {
230 extern const struct of_device_id __reservedmem_of_table[];
231 const struct of_device_id *i;
232 int ret = -ENOENT;
233
234 for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
235 reservedmem_of_init_fn initfn = i->data;
236 const char *compat = i->compatible;
237
238 if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
239 continue;
240
241 ret = initfn(rmem);
242 if (ret == 0) {
243 pr_info("initialized node %s, compatible id %s\n",
244 rmem->name, compat);
245 break;
246 }
247 }
248 return ret;
249 }
250
__rmem_cmp(const void * a,const void * b)251 static int __init __rmem_cmp(const void *a, const void *b)
252 {
253 const struct reserved_mem *ra = a, *rb = b;
254
255 if (ra->base < rb->base)
256 return -1;
257
258 if (ra->base > rb->base)
259 return 1;
260
261 /*
262 * Put the dynamic allocations (address == 0, size == 0) before static
263 * allocations at address 0x0 so that overlap detection works
264 * correctly.
265 */
266 if (ra->size < rb->size)
267 return -1;
268 if (ra->size > rb->size)
269 return 1;
270
271 if (ra->fdt_node < rb->fdt_node)
272 return -1;
273 if (ra->fdt_node > rb->fdt_node)
274 return 1;
275
276 return 0;
277 }
278
__rmem_check_for_overlap(void)279 static void __init __rmem_check_for_overlap(void)
280 {
281 int i;
282
283 if (reserved_mem_count < 2)
284 return;
285
286 sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]),
287 __rmem_cmp, NULL);
288 for (i = 0; i < reserved_mem_count - 1; i++) {
289 struct reserved_mem *this, *next;
290
291 this = &reserved_mem[i];
292 next = &reserved_mem[i + 1];
293
294 if (this->base + this->size > next->base) {
295 phys_addr_t this_end, next_end;
296
297 this_end = this->base + this->size;
298 next_end = next->base + next->size;
299 pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
300 this->name, &this->base, &this_end,
301 next->name, &next->base, &next_end);
302 }
303 }
304 }
305
306 /**
307 * fdt_init_reserved_mem() - allocate and init all saved reserved memory regions
308 */
fdt_init_reserved_mem(void)309 void __init fdt_init_reserved_mem(void)
310 {
311 int i;
312
313 /* check for overlapping reserved regions */
314 __rmem_check_for_overlap();
315
316 for (i = 0; i < reserved_mem_count; i++) {
317 struct reserved_mem *rmem = &reserved_mem[i];
318 unsigned long node = rmem->fdt_node;
319 int len;
320 const __be32 *prop;
321 int err = 0;
322 bool nomap, reusable;
323
324 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
325 reusable = of_get_flat_dt_prop(node, "reusable", NULL) != NULL;
326 prop = of_get_flat_dt_prop(node, "phandle", &len);
327 if (!prop)
328 prop = of_get_flat_dt_prop(node, "linux,phandle", &len);
329 if (prop)
330 rmem->phandle = of_read_number(prop, len/4);
331
332 if (rmem->size == 0)
333 err = __reserved_mem_alloc_size(node, rmem->name,
334 &rmem->base, &rmem->size);
335 if (err == 0) {
336 err = __reserved_mem_init_node(rmem);
337 if (err != 0 && err != -ENOENT) {
338 pr_info("node %s compatible matching fail\n",
339 rmem->name);
340 if (nomap)
341 memblock_clear_nomap(rmem->base, rmem->size);
342 else
343 memblock_phys_free(rmem->base,
344 rmem->size);
345 } else {
346 phys_addr_t end = rmem->base + rmem->size - 1;
347
348 pr_info("%pa..%pa (%lu KiB) %s %s %s\n",
349 &rmem->base, &end, (unsigned long)(rmem->size / SZ_1K),
350 nomap ? "nomap" : "map",
351 reusable ? "reusable" : "non-reusable",
352 rmem->name ? rmem->name : "unknown");
353
354 memblock_memsize_record(rmem->name, rmem->base,
355 rmem->size, nomap,
356 reusable);
357 if (reusable &&
358 !of_flat_dt_is_compatible(node, "shared-dma-pool"))
359 memblock_memsize_mod_reusable_size(rmem->size);
360 }
361 }
362 }
363 }
364
__find_rmem(struct device_node * node)365 static inline struct reserved_mem *__find_rmem(struct device_node *node)
366 {
367 unsigned int i;
368
369 if (!node->phandle)
370 return NULL;
371
372 for (i = 0; i < reserved_mem_count; i++)
373 if (reserved_mem[i].phandle == node->phandle)
374 return &reserved_mem[i];
375 return NULL;
376 }
377
378 struct rmem_assigned_device {
379 struct device *dev;
380 struct reserved_mem *rmem;
381 struct list_head list;
382 };
383
384 static LIST_HEAD(of_rmem_assigned_device_list);
385 static DEFINE_MUTEX(of_rmem_assigned_device_mutex);
386
387 /**
388 * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
389 * given device
390 * @dev: Pointer to the device to configure
391 * @np: Pointer to the device_node with 'reserved-memory' property
392 * @idx: Index of selected region
393 *
394 * This function assigns respective DMA-mapping operations based on reserved
395 * memory region specified by 'memory-region' property in @np node to the @dev
396 * device. When driver needs to use more than one reserved memory region, it
397 * should allocate child devices and initialize regions by name for each of
398 * child device.
399 *
400 * Returns error code or zero on success.
401 */
of_reserved_mem_device_init_by_idx(struct device * dev,struct device_node * np,int idx)402 int of_reserved_mem_device_init_by_idx(struct device *dev,
403 struct device_node *np, int idx)
404 {
405 struct rmem_assigned_device *rd;
406 struct device_node *target;
407 struct reserved_mem *rmem;
408 int ret;
409
410 if (!np || !dev)
411 return -EINVAL;
412
413 target = of_parse_phandle(np, "memory-region", idx);
414 if (!target)
415 return -ENODEV;
416
417 if (!of_device_is_available(target)) {
418 of_node_put(target);
419 return 0;
420 }
421
422 rmem = __find_rmem(target);
423 of_node_put(target);
424
425 if (!rmem || !rmem->ops || !rmem->ops->device_init)
426 return -EINVAL;
427
428 rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL);
429 if (!rd)
430 return -ENOMEM;
431
432 ret = rmem->ops->device_init(rmem, dev);
433 if (ret == 0) {
434 rd->dev = dev;
435 rd->rmem = rmem;
436
437 mutex_lock(&of_rmem_assigned_device_mutex);
438 list_add(&rd->list, &of_rmem_assigned_device_list);
439 mutex_unlock(&of_rmem_assigned_device_mutex);
440
441 dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
442 } else {
443 kfree(rd);
444 }
445
446 return ret;
447 }
448 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx);
449
450 /**
451 * of_reserved_mem_device_init_by_name() - assign named reserved memory region
452 * to given device
453 * @dev: pointer to the device to configure
454 * @np: pointer to the device node with 'memory-region' property
455 * @name: name of the selected memory region
456 *
457 * Returns: 0 on success or a negative error-code on failure.
458 */
of_reserved_mem_device_init_by_name(struct device * dev,struct device_node * np,const char * name)459 int of_reserved_mem_device_init_by_name(struct device *dev,
460 struct device_node *np,
461 const char *name)
462 {
463 int idx = of_property_match_string(np, "memory-region-names", name);
464
465 return of_reserved_mem_device_init_by_idx(dev, np, idx);
466 }
467 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_name);
468
469 /**
470 * of_reserved_mem_device_release() - release reserved memory device structures
471 * @dev: Pointer to the device to deconfigure
472 *
473 * This function releases structures allocated for memory region handling for
474 * the given device.
475 */
of_reserved_mem_device_release(struct device * dev)476 void of_reserved_mem_device_release(struct device *dev)
477 {
478 struct rmem_assigned_device *rd, *tmp;
479 LIST_HEAD(release_list);
480
481 mutex_lock(&of_rmem_assigned_device_mutex);
482 list_for_each_entry_safe(rd, tmp, &of_rmem_assigned_device_list, list) {
483 if (rd->dev == dev)
484 list_move_tail(&rd->list, &release_list);
485 }
486 mutex_unlock(&of_rmem_assigned_device_mutex);
487
488 list_for_each_entry_safe(rd, tmp, &release_list, list) {
489 if (rd->rmem && rd->rmem->ops && rd->rmem->ops->device_release)
490 rd->rmem->ops->device_release(rd->rmem, dev);
491
492 kfree(rd);
493 }
494 }
495 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
496
497 /**
498 * of_reserved_mem_lookup() - acquire reserved_mem from a device node
499 * @np: node pointer of the desired reserved-memory region
500 *
501 * This function allows drivers to acquire a reference to the reserved_mem
502 * struct based on a device node handle.
503 *
504 * Returns a reserved_mem reference, or NULL on error.
505 */
of_reserved_mem_lookup(struct device_node * np)506 struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
507 {
508 const char *name;
509 int i;
510
511 if (!np->full_name)
512 return NULL;
513
514 name = kbasename(np->full_name);
515 for (i = 0; i < reserved_mem_count; i++)
516 if (!strcmp(reserved_mem[i].name, name))
517 return &reserved_mem[i];
518
519 return NULL;
520 }
521 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);
522