blob: a7fbc5e37e19ef67457425fc9381862aa66895c5 [file] [log] [blame]
Rob Herringaf6074f2017-12-27 12:55:14 -06001// SPDX-License-Identifier: GPL-2.0+
Marek Szyprowski3f0c8202014-02-28 14:42:48 +01002/*
3 * Device tree based initialization code for reserved memory.
4 *
Mitchel Humpherysae1add22015-09-15 18:30:36 -07005 * Copyright (c) 2013, 2015 The Linux Foundation. All Rights Reserved.
Marek Szyprowski3f0c8202014-02-28 14:42:48 +01006 * 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>
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010010 */
11
Rob Herring606ad422016-06-15 08:32:18 -050012#define pr_fmt(fmt) "OF: reserved mem: " fmt
13
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010014#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>
Mitchel Humpherysae1add22015-09-15 18:30:36 -070021#include <linux/sort.h>
Marek Szyprowski59ce4032016-05-24 15:31:24 +020022#include <linux/slab.h>
Mike Rapoportaca52c32018-10-30 15:07:44 -070023#include <linux/memblock.h>
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010024
Patrick Daly632c9902020-02-24 10:02:32 -080025#define MAX_RESERVED_REGIONS 64
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010026static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
27static int reserved_mem_count;
28
Christoph Hellwig221e1e02019-02-11 14:35:45 +010029static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010030 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
31 phys_addr_t *res_base)
32{
Vinayak Menone53b50c2016-02-22 19:15:44 +053033 phys_addr_t base;
Mike Rapoport42b46ae2019-03-11 23:29:31 -070034
Vinayak Menone53b50c2016-02-22 19:15:44 +053035 end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
Mike Rapoport7e1c4e22018-10-30 15:09:57 -070036 align = !align ? SMP_CACHE_BYTES : align;
Mike Rapoport5c01a252019-03-11 23:30:58 -070037 base = memblock_find_in_range(start, end, size, align);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010038 if (!base)
39 return -ENOMEM;
40
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010041 *res_base = base;
42 if (nomap)
43 return memblock_remove(base, size);
Mike Rapoport5c01a252019-03-11 23:30:58 -070044
45 return memblock_reserve(base, size);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010046}
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010047
48/**
chenqiwuc8813f72020-05-11 23:04:57 +080049 * fdt_reserved_mem_save_node() - save fdt node for second pass initialization
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010050 */
51void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
52 phys_addr_t base, phys_addr_t size)
53{
54 struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
55
56 if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) {
Danny Lin418370f2020-06-03 22:49:00 -070057 pr_err("not enough space for all defined regions.\n");
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010058 return;
59 }
60
61 rmem->fdt_node = node;
62 rmem->name = uname;
63 rmem->base = base;
64 rmem->size = size;
65
66 reserved_mem_count++;
67 return;
68}
69
70/**
chenqiwuc8813f72020-05-11 23:04:57 +080071 * __reserved_mem_alloc_size() - allocate reserved memory described by
Yue Hu6f1188b2020-07-30 17:23:53 +080072 * 'size', 'alignment' and 'alloc-ranges' properties.
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010073 */
74static int __init __reserved_mem_alloc_size(unsigned long node,
75 const char *uname, phys_addr_t *res_base, phys_addr_t *res_size)
76{
77 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
78 phys_addr_t start = 0, end = 0;
79 phys_addr_t base = 0, align = 0, size;
Rob Herring9d0c4df2014-04-01 23:49:03 -050080 int len;
81 const __be32 *prop;
Yue Hu6f1188b2020-07-30 17:23:53 +080082 bool nomap;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010083 int ret;
84
85 prop = of_get_flat_dt_prop(node, "size", &len);
86 if (!prop)
87 return -EINVAL;
88
89 if (len != dt_root_size_cells * sizeof(__be32)) {
Rob Herring606ad422016-06-15 08:32:18 -050090 pr_err("invalid size property in '%s' node.\n", uname);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010091 return -EINVAL;
92 }
93 size = dt_mem_next_cell(dt_root_size_cells, &prop);
94
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010095 prop = of_get_flat_dt_prop(node, "alignment", &len);
96 if (prop) {
97 if (len != dt_root_addr_cells * sizeof(__be32)) {
Rob Herring606ad422016-06-15 08:32:18 -050098 pr_err("invalid alignment property in '%s' node.\n",
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010099 uname);
100 return -EINVAL;
101 }
102 align = dt_mem_next_cell(dt_root_addr_cells, &prop);
103 }
104
Yue Hu6f1188b2020-07-30 17:23:53 +0800105 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
106
Jason Liu1cc8e342015-11-10 20:30:26 +0800107 /* Need adjust the alignment to satisfy the CMA requirement */
Jaewon7d482812016-05-25 13:29:50 +0900108 if (IS_ENABLED(CONFIG_CMA)
109 && of_flat_dt_is_compatible(node, "shared-dma-pool")
110 && of_get_flat_dt_prop(node, "reusable", NULL)
Yue Hu6f1188b2020-07-30 17:23:53 +0800111 && !nomap) {
Stephen Rothwellaaaab562016-05-31 09:38:56 +1000112 unsigned long order =
113 max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
114
115 align = max(align, (phys_addr_t)PAGE_SIZE << order);
116 }
Jason Liu1cc8e342015-11-10 20:30:26 +0800117
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100118 prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
119 if (prop) {
120
121 if (len % t_len != 0) {
Rob Herring606ad422016-06-15 08:32:18 -0500122 pr_err("invalid alloc-ranges property in '%s', skipping node.\n",
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100123 uname);
124 return -EINVAL;
125 }
126
127 base = 0;
128
129 while (len > 0) {
130 start = dt_mem_next_cell(dt_root_addr_cells, &prop);
131 end = start + dt_mem_next_cell(dt_root_size_cells,
132 &prop);
133
134 ret = early_init_dt_alloc_reserved_memory_arch(size,
135 align, start, end, nomap, &base);
136 if (ret == 0) {
Rob Herring606ad422016-06-15 08:32:18 -0500137 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100138 uname, &base,
139 (unsigned long)size / SZ_1M);
140 break;
141 }
142 len -= t_len;
143 }
144
145 } else {
146 ret = early_init_dt_alloc_reserved_memory_arch(size, align,
147 0, 0, nomap, &base);
148 if (ret == 0)
Rob Herring606ad422016-06-15 08:32:18 -0500149 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100150 uname, &base, (unsigned long)size / SZ_1M);
151 }
152
153 if (base == 0) {
Rob Herring606ad422016-06-15 08:32:18 -0500154 pr_info("failed to allocate memory for node '%s'\n", uname);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100155 return -ENOMEM;
156 }
157
158 *res_base = base;
159 *res_size = size;
160
161 return 0;
162}
163
Marek Szyprowskif618c472014-02-28 14:42:49 +0100164static const struct of_device_id __rmem_of_table_sentinel
Joe Perches33def842020-10-21 19:36:07 -0700165 __used __section("__reservedmem_of_table_end");
Marek Szyprowskif618c472014-02-28 14:42:49 +0100166
167/**
chenqiwuc8813f72020-05-11 23:04:57 +0800168 * __reserved_mem_init_node() - call region specific reserved memory init code
Marek Szyprowskif618c472014-02-28 14:42:49 +0100169 */
170static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
171{
172 extern const struct of_device_id __reservedmem_of_table[];
173 const struct of_device_id *i;
pierre Kuod0b8ed42019-02-19 15:45:00 +0800174 int ret = -ENOENT;
Marek Szyprowskif618c472014-02-28 14:42:49 +0100175
176 for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
Rob Herringdf3ed932017-05-11 10:15:10 -0500177 reservedmem_of_init_fn initfn = i->data;
Marek Szyprowskif618c472014-02-28 14:42:49 +0100178 const char *compat = i->compatible;
179
180 if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
181 continue;
182
pierre Kuod0b8ed42019-02-19 15:45:00 +0800183 ret = initfn(rmem);
184 if (ret == 0) {
Rob Herring606ad422016-06-15 08:32:18 -0500185 pr_info("initialized node %s, compatible id %s\n",
Marek Szyprowskif618c472014-02-28 14:42:49 +0100186 rmem->name, compat);
pierre Kuod0b8ed42019-02-19 15:45:00 +0800187 break;
Marek Szyprowskif618c472014-02-28 14:42:49 +0100188 }
189 }
pierre Kuod0b8ed42019-02-19 15:45:00 +0800190 return ret;
Marek Szyprowskif618c472014-02-28 14:42:49 +0100191}
192
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700193static int __init __rmem_cmp(const void *a, const void *b)
194{
195 const struct reserved_mem *ra = a, *rb = b;
196
Michael Ellerman9eb8cd22015-11-18 21:46:38 +1100197 if (ra->base < rb->base)
198 return -1;
199
200 if (ra->base > rb->base)
201 return 1;
202
Vincent Whitchurchca05f332020-10-21 11:53:59 +0200203 /*
204 * Put the dynamic allocations (address == 0, size == 0) before static
205 * allocations at address 0x0 so that overlap detection works
206 * correctly.
207 */
208 if (ra->size < rb->size)
209 return -1;
210 if (ra->size > rb->size)
211 return 1;
212
Michael Ellerman9eb8cd22015-11-18 21:46:38 +1100213 return 0;
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700214}
215
216static void __init __rmem_check_for_overlap(void)
217{
218 int i;
219
220 if (reserved_mem_count < 2)
221 return;
222
223 sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]),
224 __rmem_cmp, NULL);
225 for (i = 0; i < reserved_mem_count - 1; i++) {
226 struct reserved_mem *this, *next;
227
228 this = &reserved_mem[i];
229 next = &reserved_mem[i + 1];
Vincent Whitchurchca05f332020-10-21 11:53:59 +0200230
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700231 if (this->base + this->size > next->base) {
232 phys_addr_t this_end, next_end;
233
234 this_end = this->base + this->size;
235 next_end = next->base + next->size;
Rob Herring606ad422016-06-15 08:32:18 -0500236 pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
Michael Ellerman85a1c772015-11-10 16:08:33 +1100237 this->name, &this->base, &this_end,
238 next->name, &next->base, &next_end);
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700239 }
240 }
241}
242
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100243/**
chenqiwuc8813f72020-05-11 23:04:57 +0800244 * fdt_init_reserved_mem() - allocate and init all saved reserved memory regions
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100245 */
246void __init fdt_init_reserved_mem(void)
247{
248 int i;
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700249
250 /* check for overlapping reserved regions */
251 __rmem_check_for_overlap();
252
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100253 for (i = 0; i < reserved_mem_count; i++) {
254 struct reserved_mem *rmem = &reserved_mem[i];
255 unsigned long node = rmem->fdt_node;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200256 int len;
257 const __be32 *prop;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100258 int err = 0;
Yue Hu6f1188b2020-07-30 17:23:53 +0800259 bool nomap;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100260
pierre Kuod0b8ed42019-02-19 15:45:00 +0800261 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200262 prop = of_get_flat_dt_prop(node, "phandle", &len);
263 if (!prop)
264 prop = of_get_flat_dt_prop(node, "linux,phandle", &len);
265 if (prop)
266 rmem->phandle = of_read_number(prop, len/4);
267
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100268 if (rmem->size == 0)
269 err = __reserved_mem_alloc_size(node, rmem->name,
270 &rmem->base, &rmem->size);
pierre Kuod0b8ed42019-02-19 15:45:00 +0800271 if (err == 0) {
272 err = __reserved_mem_init_node(rmem);
273 if (err != 0 && err != -ENOENT) {
274 pr_info("node %s compatible matching fail\n",
275 rmem->name);
276 memblock_free(rmem->base, rmem->size);
277 if (nomap)
278 memblock_add(rmem->base, rmem->size);
279 }
280 }
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100281 }
282}
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200283
284static inline struct reserved_mem *__find_rmem(struct device_node *node)
285{
286 unsigned int i;
287
288 if (!node->phandle)
289 return NULL;
290
291 for (i = 0; i < reserved_mem_count; i++)
292 if (reserved_mem[i].phandle == node->phandle)
293 return &reserved_mem[i];
294 return NULL;
295}
296
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200297struct rmem_assigned_device {
298 struct device *dev;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200299 struct reserved_mem *rmem;
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200300 struct list_head list;
301};
302
303static LIST_HEAD(of_rmem_assigned_device_list);
304static DEFINE_MUTEX(of_rmem_assigned_device_mutex);
305
306/**
307 * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
308 * given device
309 * @dev: Pointer to the device to configure
310 * @np: Pointer to the device_node with 'reserved-memory' property
311 * @idx: Index of selected region
312 *
313 * This function assigns respective DMA-mapping operations based on reserved
314 * memory region specified by 'memory-region' property in @np node to the @dev
315 * device. When driver needs to use more than one reserved memory region, it
316 * should allocate child devices and initialize regions by name for each of
317 * child device.
318 *
319 * Returns error code or zero on success.
320 */
321int of_reserved_mem_device_init_by_idx(struct device *dev,
322 struct device_node *np, int idx)
323{
324 struct rmem_assigned_device *rd;
325 struct device_node *target;
326 struct reserved_mem *rmem;
Marek Szyprowski47f29df2014-10-29 14:50:29 -0700327 int ret;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200328
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200329 if (!np || !dev)
330 return -EINVAL;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200331
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200332 target = of_parse_phandle(np, "memory-region", idx);
333 if (!target)
Marek Szyprowski9f5a8022016-06-08 03:51:53 -0300334 return -ENODEV;
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200335
Chris Goldsworthy5dba5172019-10-19 18:57:24 -0700336 if (!of_device_is_available(target)) {
337 of_node_put(target);
Krishna Reddyd698a382019-05-22 16:17:11 +0530338 return 0;
Chris Goldsworthy5dba5172019-10-19 18:57:24 -0700339 }
Krishna Reddyd698a382019-05-22 16:17:11 +0530340
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200341 rmem = __find_rmem(target);
342 of_node_put(target);
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200343
344 if (!rmem || !rmem->ops || !rmem->ops->device_init)
Marek Szyprowski47f29df2014-10-29 14:50:29 -0700345 return -EINVAL;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200346
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200347 rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL);
348 if (!rd)
349 return -ENOMEM;
350
Marek Szyprowski47f29df2014-10-29 14:50:29 -0700351 ret = rmem->ops->device_init(rmem, dev);
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200352 if (ret == 0) {
353 rd->dev = dev;
354 rd->rmem = rmem;
355
356 mutex_lock(&of_rmem_assigned_device_mutex);
357 list_add(&rd->list, &of_rmem_assigned_device_list);
358 mutex_unlock(&of_rmem_assigned_device_mutex);
359
Marek Szyprowski47f29df2014-10-29 14:50:29 -0700360 dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200361 } else {
362 kfree(rd);
363 }
Marek Szyprowski47f29df2014-10-29 14:50:29 -0700364
365 return ret;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200366}
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200367EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx);
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200368
369/**
Thierry Reding0da0e312020-04-03 19:44:52 +0200370 * of_reserved_mem_device_init_by_name() - assign named reserved memory region
371 * to given device
372 * @dev: pointer to the device to configure
373 * @np: pointer to the device node with 'memory-region' property
374 * @name: name of the selected memory region
375 *
376 * Returns: 0 on success or a negative error-code on failure.
377 */
378int of_reserved_mem_device_init_by_name(struct device *dev,
379 struct device_node *np,
380 const char *name)
381{
382 int idx = of_property_match_string(np, "memory-region-names", name);
383
384 return of_reserved_mem_device_init_by_idx(dev, np, idx);
385}
386EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_name);
387
388/**
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200389 * of_reserved_mem_device_release() - release reserved memory device structures
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200390 * @dev: Pointer to the device to deconfigure
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200391 *
392 * This function releases structures allocated for memory region handling for
393 * the given device.
394 */
395void of_reserved_mem_device_release(struct device *dev)
396{
Thierry Reding081df762020-04-03 19:46:57 +0200397 struct rmem_assigned_device *rd, *tmp;
398 LIST_HEAD(release_list);
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200399
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200400 mutex_lock(&of_rmem_assigned_device_mutex);
Thierry Reding081df762020-04-03 19:46:57 +0200401 list_for_each_entry_safe(rd, tmp, &of_rmem_assigned_device_list, list) {
402 if (rd->dev == dev)
403 list_move_tail(&rd->list, &release_list);
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200404 }
405 mutex_unlock(&of_rmem_assigned_device_mutex);
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200406
Thierry Reding081df762020-04-03 19:46:57 +0200407 list_for_each_entry_safe(rd, tmp, &release_list, list) {
408 if (rd->rmem && rd->rmem->ops && rd->rmem->ops->device_release)
409 rd->rmem->ops->device_release(rd->rmem, dev);
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200410
Thierry Reding081df762020-04-03 19:46:57 +0200411 kfree(rd);
412 }
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200413}
George G. Davis615fde72015-01-09 09:29:05 -0500414EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
Bjorn Anderssoneb297bc2017-10-10 22:08:54 -0700415
416/**
417 * of_reserved_mem_lookup() - acquire reserved_mem from a device node
418 * @np: node pointer of the desired reserved-memory region
419 *
420 * This function allows drivers to acquire a reference to the reserved_mem
421 * struct based on a device node handle.
422 *
423 * Returns a reserved_mem reference, or NULL on error.
424 */
425struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
426{
427 const char *name;
428 int i;
429
430 if (!np->full_name)
431 return NULL;
432
433 name = kbasename(np->full_name);
434 for (i = 0; i < reserved_mem_count; i++)
435 if (!strcmp(reserved_mem[i].name, name))
436 return &reserved_mem[i];
437
438 return NULL;
439}
440EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);