blob: 895c83e0c7b6c4c816b1aad72c67e2a1a5235a88 [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>
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010023
Stewart Smith22f8cc62017-09-26 18:40:00 +100024#define MAX_RESERVED_REGIONS 32
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010025static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
26static int reserved_mem_count;
27
28#if defined(CONFIG_HAVE_MEMBLOCK)
29#include <linux/memblock.h>
30int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
31 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
32 phys_addr_t *res_base)
33{
Vinayak Menone53b50c2016-02-22 19:15:44 +053034 phys_addr_t base;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010035 /*
36 * We use __memblock_alloc_base() because memblock_alloc_base()
37 * panic()s on allocation failure.
38 */
Vinayak Menone53b50c2016-02-22 19:15:44 +053039 end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
40 base = __memblock_alloc_base(size, align, end);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010041 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#else
58int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
59 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
60 phys_addr_t *res_base)
61{
62 pr_err("Reserved memory not supported, ignoring region 0x%llx%s\n",
63 size, nomap ? " (nomap)" : "");
64 return -ENOSYS;
65}
66#endif
67
68/**
69 * res_mem_save_node() - save fdt node for second pass initialization
70 */
71void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
72 phys_addr_t base, phys_addr_t size)
73{
74 struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
75
76 if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) {
Rob Herring606ad422016-06-15 08:32:18 -050077 pr_err("not enough space all defined regions.\n");
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010078 return;
79 }
80
81 rmem->fdt_node = node;
82 rmem->name = uname;
83 rmem->base = base;
84 rmem->size = size;
85
86 reserved_mem_count++;
87 return;
88}
89
90/**
91 * res_mem_alloc_size() - allocate reserved memory described by 'size', 'align'
92 * and 'alloc-ranges' properties
93 */
94static int __init __reserved_mem_alloc_size(unsigned long node,
95 const char *uname, phys_addr_t *res_base, phys_addr_t *res_size)
96{
97 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
98 phys_addr_t start = 0, end = 0;
99 phys_addr_t base = 0, align = 0, size;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500100 int len;
101 const __be32 *prop;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100102 int nomap;
103 int ret;
104
105 prop = of_get_flat_dt_prop(node, "size", &len);
106 if (!prop)
107 return -EINVAL;
108
109 if (len != dt_root_size_cells * sizeof(__be32)) {
Rob Herring606ad422016-06-15 08:32:18 -0500110 pr_err("invalid size property in '%s' node.\n", uname);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100111 return -EINVAL;
112 }
113 size = dt_mem_next_cell(dt_root_size_cells, &prop);
114
115 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
116
117 prop = of_get_flat_dt_prop(node, "alignment", &len);
118 if (prop) {
119 if (len != dt_root_addr_cells * sizeof(__be32)) {
Rob Herring606ad422016-06-15 08:32:18 -0500120 pr_err("invalid alignment property in '%s' node.\n",
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100121 uname);
122 return -EINVAL;
123 }
124 align = dt_mem_next_cell(dt_root_addr_cells, &prop);
125 }
126
Jason Liu1cc8e342015-11-10 20:30:26 +0800127 /* Need adjust the alignment to satisfy the CMA requirement */
Jaewon7d482812016-05-25 13:29:50 +0900128 if (IS_ENABLED(CONFIG_CMA)
129 && of_flat_dt_is_compatible(node, "shared-dma-pool")
130 && of_get_flat_dt_prop(node, "reusable", NULL)
Stephen Rothwellaaaab562016-05-31 09:38:56 +1000131 && !of_get_flat_dt_prop(node, "no-map", NULL)) {
132 unsigned long order =
133 max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
134
135 align = max(align, (phys_addr_t)PAGE_SIZE << order);
136 }
Jason Liu1cc8e342015-11-10 20:30:26 +0800137
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100138 prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
139 if (prop) {
140
141 if (len % t_len != 0) {
Rob Herring606ad422016-06-15 08:32:18 -0500142 pr_err("invalid alloc-ranges property in '%s', skipping node.\n",
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100143 uname);
144 return -EINVAL;
145 }
146
147 base = 0;
148
149 while (len > 0) {
150 start = dt_mem_next_cell(dt_root_addr_cells, &prop);
151 end = start + dt_mem_next_cell(dt_root_size_cells,
152 &prop);
153
154 ret = early_init_dt_alloc_reserved_memory_arch(size,
155 align, start, end, nomap, &base);
156 if (ret == 0) {
Rob Herring606ad422016-06-15 08:32:18 -0500157 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100158 uname, &base,
159 (unsigned long)size / SZ_1M);
160 break;
161 }
162 len -= t_len;
163 }
164
165 } else {
166 ret = early_init_dt_alloc_reserved_memory_arch(size, align,
167 0, 0, nomap, &base);
168 if (ret == 0)
Rob Herring606ad422016-06-15 08:32:18 -0500169 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100170 uname, &base, (unsigned long)size / SZ_1M);
171 }
172
173 if (base == 0) {
Rob Herring606ad422016-06-15 08:32:18 -0500174 pr_info("failed to allocate memory for node '%s'\n", uname);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100175 return -ENOMEM;
176 }
177
178 *res_base = base;
179 *res_size = size;
180
181 return 0;
182}
183
Marek Szyprowskif618c472014-02-28 14:42:49 +0100184static const struct of_device_id __rmem_of_table_sentinel
185 __used __section(__reservedmem_of_table_end);
186
187/**
188 * res_mem_init_node() - call region specific reserved memory init code
189 */
190static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
191{
192 extern const struct of_device_id __reservedmem_of_table[];
193 const struct of_device_id *i;
194
195 for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
Rob Herringdf3ed932017-05-11 10:15:10 -0500196 reservedmem_of_init_fn initfn = i->data;
Marek Szyprowskif618c472014-02-28 14:42:49 +0100197 const char *compat = i->compatible;
198
199 if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
200 continue;
201
Rob Herring9dd31072014-05-08 16:06:17 -0500202 if (initfn(rmem) == 0) {
Rob Herring606ad422016-06-15 08:32:18 -0500203 pr_info("initialized node %s, compatible id %s\n",
Marek Szyprowskif618c472014-02-28 14:42:49 +0100204 rmem->name, compat);
205 return 0;
206 }
207 }
208 return -ENOENT;
209}
210
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700211static int __init __rmem_cmp(const void *a, const void *b)
212{
213 const struct reserved_mem *ra = a, *rb = b;
214
Michael Ellerman9eb8cd22015-11-18 21:46:38 +1100215 if (ra->base < rb->base)
216 return -1;
217
218 if (ra->base > rb->base)
219 return 1;
220
221 return 0;
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700222}
223
224static void __init __rmem_check_for_overlap(void)
225{
226 int i;
227
228 if (reserved_mem_count < 2)
229 return;
230
231 sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]),
232 __rmem_cmp, NULL);
233 for (i = 0; i < reserved_mem_count - 1; i++) {
234 struct reserved_mem *this, *next;
235
236 this = &reserved_mem[i];
237 next = &reserved_mem[i + 1];
238 if (!(this->base && next->base))
239 continue;
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;
Rob Herring606ad422016-06-15 08:32:18 -0500245 pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
Michael Ellerman85a1c772015-11-10 16:08:33 +1100246 this->name, &this->base, &this_end,
247 next->name, &next->base, &next_end);
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700248 }
249 }
250}
251
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100252/**
253 * fdt_init_reserved_mem - allocate and init all saved reserved memory regions
254 */
255void __init fdt_init_reserved_mem(void)
256{
257 int i;
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700258
259 /* check for overlapping reserved regions */
260 __rmem_check_for_overlap();
261
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100262 for (i = 0; i < reserved_mem_count; i++) {
263 struct reserved_mem *rmem = &reserved_mem[i];
264 unsigned long node = rmem->fdt_node;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200265 int len;
266 const __be32 *prop;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100267 int err = 0;
268
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200269 prop = of_get_flat_dt_prop(node, "phandle", &len);
270 if (!prop)
271 prop = of_get_flat_dt_prop(node, "linux,phandle", &len);
272 if (prop)
273 rmem->phandle = of_read_number(prop, len/4);
274
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100275 if (rmem->size == 0)
276 err = __reserved_mem_alloc_size(node, rmem->name,
277 &rmem->base, &rmem->size);
Marek Szyprowskif618c472014-02-28 14:42:49 +0100278 if (err == 0)
279 __reserved_mem_init_node(rmem);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100280 }
281}
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200282
283static inline struct reserved_mem *__find_rmem(struct device_node *node)
284{
285 unsigned int i;
286
287 if (!node->phandle)
288 return NULL;
289
290 for (i = 0; i < reserved_mem_count; i++)
291 if (reserved_mem[i].phandle == node->phandle)
292 return &reserved_mem[i];
293 return NULL;
294}
295
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200296struct rmem_assigned_device {
297 struct device *dev;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200298 struct reserved_mem *rmem;
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200299 struct list_head list;
300};
301
302static LIST_HEAD(of_rmem_assigned_device_list);
303static DEFINE_MUTEX(of_rmem_assigned_device_mutex);
304
305/**
306 * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
307 * given device
308 * @dev: Pointer to the device to configure
309 * @np: Pointer to the device_node with 'reserved-memory' property
310 * @idx: Index of selected region
311 *
312 * This function assigns respective DMA-mapping operations based on reserved
313 * memory region specified by 'memory-region' property in @np node to the @dev
314 * device. When driver needs to use more than one reserved memory region, it
315 * should allocate child devices and initialize regions by name for each of
316 * child device.
317 *
318 * Returns error code or zero on success.
319 */
320int of_reserved_mem_device_init_by_idx(struct device *dev,
321 struct device_node *np, int idx)
322{
323 struct rmem_assigned_device *rd;
324 struct device_node *target;
325 struct reserved_mem *rmem;
Marek Szyprowski47f29df2014-10-29 14:50:29 -0700326 int ret;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200327
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200328 if (!np || !dev)
329 return -EINVAL;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200330
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200331 target = of_parse_phandle(np, "memory-region", idx);
332 if (!target)
Marek Szyprowski9f5a8022016-06-08 03:51:53 -0300333 return -ENODEV;
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200334
335 rmem = __find_rmem(target);
336 of_node_put(target);
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200337
338 if (!rmem || !rmem->ops || !rmem->ops->device_init)
Marek Szyprowski47f29df2014-10-29 14:50:29 -0700339 return -EINVAL;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200340
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200341 rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL);
342 if (!rd)
343 return -ENOMEM;
344
Marek Szyprowski47f29df2014-10-29 14:50:29 -0700345 ret = rmem->ops->device_init(rmem, dev);
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200346 if (ret == 0) {
347 rd->dev = dev;
348 rd->rmem = rmem;
349
350 mutex_lock(&of_rmem_assigned_device_mutex);
351 list_add(&rd->list, &of_rmem_assigned_device_list);
352 mutex_unlock(&of_rmem_assigned_device_mutex);
Smitha T Murthya3b398e2016-12-21 11:14:31 +0530353 /* ensure that dma_ops is set for virtual devices
354 * using reserved memory
355 */
Christoph Hellwig3d6ce862018-05-03 16:25:08 +0200356 of_dma_configure(dev, np, true);
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200357
Marek Szyprowski47f29df2014-10-29 14:50:29 -0700358 dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200359 } else {
360 kfree(rd);
361 }
Marek Szyprowski47f29df2014-10-29 14:50:29 -0700362
363 return ret;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200364}
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200365EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx);
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200366
367/**
368 * of_reserved_mem_device_release() - release reserved memory device structures
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200369 * @dev: Pointer to the device to deconfigure
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200370 *
371 * This function releases structures allocated for memory region handling for
372 * the given device.
373 */
374void of_reserved_mem_device_release(struct device *dev)
375{
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200376 struct rmem_assigned_device *rd;
377 struct reserved_mem *rmem = NULL;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200378
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200379 mutex_lock(&of_rmem_assigned_device_mutex);
380 list_for_each_entry(rd, &of_rmem_assigned_device_list, list) {
381 if (rd->dev == dev) {
382 rmem = rd->rmem;
383 list_del(&rd->list);
384 kfree(rd);
385 break;
386 }
387 }
388 mutex_unlock(&of_rmem_assigned_device_mutex);
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200389
390 if (!rmem || !rmem->ops || !rmem->ops->device_release)
391 return;
392
393 rmem->ops->device_release(rmem, dev);
394}
George G. Davis615fde72015-01-09 09:29:05 -0500395EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
Bjorn Anderssoneb297bc2017-10-10 22:08:54 -0700396
397/**
398 * of_reserved_mem_lookup() - acquire reserved_mem from a device node
399 * @np: node pointer of the desired reserved-memory region
400 *
401 * This function allows drivers to acquire a reference to the reserved_mem
402 * struct based on a device node handle.
403 *
404 * Returns a reserved_mem reference, or NULL on error.
405 */
406struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
407{
408 const char *name;
409 int i;
410
411 if (!np->full_name)
412 return NULL;
413
414 name = kbasename(np->full_name);
415 for (i = 0; i < reserved_mem_count; i++)
416 if (!strcmp(reserved_mem[i].name, name))
417 return &reserved_mem[i];
418
419 return NULL;
420}
421EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);