blob: c807c28d04a7ee8f466769d680da71791c188cb2 [file] [log] [blame]
Marek Szyprowski3f0c8202014-02-28 14:42:48 +01001/*
2 * Device tree based initialization code for reserved memory.
3 *
Liam Mark6b6b7492017-04-13 17:11:54 -07004 * Copyright (c) 2013, 2015, 2017 The Linux Foundation. All Rights Reserved.
Marek Szyprowski3f0c8202014-02-28 14:42:48 +01005 * Copyright (c) 2013,2014 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com
7 * Author: Marek Szyprowski <m.szyprowski@samsung.com>
8 * Author: Josh Cartwright <joshc@codeaurora.org>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License or (at your optional) any later version of the license.
14 */
15
Rob Herring606ad422016-06-15 08:32:18 -050016#define pr_fmt(fmt) "OF: reserved mem: " fmt
17
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010018#include <linux/err.h>
19#include <linux/of.h>
20#include <linux/of_fdt.h>
21#include <linux/of_platform.h>
22#include <linux/mm.h>
23#include <linux/sizes.h>
24#include <linux/of_reserved_mem.h>
Mitchel Humpherysae1add22015-09-15 18:30:36 -070025#include <linux/sort.h>
Marek Szyprowski59ce4032016-05-24 15:31:24 +020026#include <linux/slab.h>
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010027
Liam Mark6b6b7492017-04-13 17:11:54 -070028#define MAX_RESERVED_REGIONS 32
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010029static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
30static int reserved_mem_count;
31
32#if defined(CONFIG_HAVE_MEMBLOCK)
33#include <linux/memblock.h>
34int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
35 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
36 phys_addr_t *res_base)
37{
Vinayak Menone53b50c2016-02-22 19:15:44 +053038 phys_addr_t base;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010039 /*
40 * We use __memblock_alloc_base() because memblock_alloc_base()
41 * panic()s on allocation failure.
42 */
Vinayak Menone53b50c2016-02-22 19:15:44 +053043 end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
44 base = __memblock_alloc_base(size, align, end);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010045 if (!base)
46 return -ENOMEM;
47
48 /*
49 * Check if the allocated region fits in to start..end window
50 */
51 if (base < start) {
52 memblock_free(base, size);
53 return -ENOMEM;
54 }
55
56 *res_base = base;
57 if (nomap)
58 return memblock_remove(base, size);
59 return 0;
60}
61#else
62int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
63 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
64 phys_addr_t *res_base)
65{
66 pr_err("Reserved memory not supported, ignoring region 0x%llx%s\n",
67 size, nomap ? " (nomap)" : "");
68 return -ENOSYS;
69}
70#endif
71
72/**
73 * res_mem_save_node() - save fdt node for second pass initialization
74 */
75void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
76 phys_addr_t base, phys_addr_t size)
77{
78 struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
79
80 if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) {
Rob Herring606ad422016-06-15 08:32:18 -050081 pr_err("not enough space all defined regions.\n");
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010082 return;
83 }
84
85 rmem->fdt_node = node;
86 rmem->name = uname;
87 rmem->base = base;
88 rmem->size = size;
89
90 reserved_mem_count++;
91 return;
92}
93
94/**
95 * res_mem_alloc_size() - allocate reserved memory described by 'size', 'align'
96 * and 'alloc-ranges' properties
97 */
98static int __init __reserved_mem_alloc_size(unsigned long node,
99 const char *uname, phys_addr_t *res_base, phys_addr_t *res_size)
100{
101 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
102 phys_addr_t start = 0, end = 0;
103 phys_addr_t base = 0, align = 0, size;
Rob Herring9d0c4df2014-04-01 23:49:03 -0500104 int len;
105 const __be32 *prop;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100106 int nomap;
107 int ret;
108
109 prop = of_get_flat_dt_prop(node, "size", &len);
110 if (!prop)
111 return -EINVAL;
112
113 if (len != dt_root_size_cells * sizeof(__be32)) {
Rob Herring606ad422016-06-15 08:32:18 -0500114 pr_err("invalid size property in '%s' node.\n", uname);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100115 return -EINVAL;
116 }
117 size = dt_mem_next_cell(dt_root_size_cells, &prop);
118
119 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
120
121 prop = of_get_flat_dt_prop(node, "alignment", &len);
122 if (prop) {
123 if (len != dt_root_addr_cells * sizeof(__be32)) {
Rob Herring606ad422016-06-15 08:32:18 -0500124 pr_err("invalid alignment property in '%s' node.\n",
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100125 uname);
126 return -EINVAL;
127 }
128 align = dt_mem_next_cell(dt_root_addr_cells, &prop);
129 }
130
Jason Liu1cc8e342015-11-10 20:30:26 +0800131 /* Need adjust the alignment to satisfy the CMA requirement */
Jaewon7d482812016-05-25 13:29:50 +0900132 if (IS_ENABLED(CONFIG_CMA)
133 && of_flat_dt_is_compatible(node, "shared-dma-pool")
134 && of_get_flat_dt_prop(node, "reusable", NULL)
Stephen Rothwellaaaab562016-05-31 09:38:56 +1000135 && !of_get_flat_dt_prop(node, "no-map", NULL)) {
136 unsigned long order =
137 max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
138
139 align = max(align, (phys_addr_t)PAGE_SIZE << order);
140 }
Jason Liu1cc8e342015-11-10 20:30:26 +0800141
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100142 prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
143 if (prop) {
144
145 if (len % t_len != 0) {
Rob Herring606ad422016-06-15 08:32:18 -0500146 pr_err("invalid alloc-ranges property in '%s', skipping node.\n",
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100147 uname);
148 return -EINVAL;
149 }
150
151 base = 0;
152
153 while (len > 0) {
154 start = dt_mem_next_cell(dt_root_addr_cells, &prop);
155 end = start + dt_mem_next_cell(dt_root_size_cells,
156 &prop);
157
158 ret = early_init_dt_alloc_reserved_memory_arch(size,
159 align, start, end, nomap, &base);
160 if (ret == 0) {
Rob Herring606ad422016-06-15 08:32:18 -0500161 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100162 uname, &base,
163 (unsigned long)size / SZ_1M);
164 break;
165 }
166 len -= t_len;
167 }
168
169 } else {
170 ret = early_init_dt_alloc_reserved_memory_arch(size, align,
171 0, 0, nomap, &base);
172 if (ret == 0)
Rob Herring606ad422016-06-15 08:32:18 -0500173 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100174 uname, &base, (unsigned long)size / SZ_1M);
175 }
176
177 if (base == 0) {
Rob Herring606ad422016-06-15 08:32:18 -0500178 pr_info("failed to allocate memory for node '%s'\n", uname);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100179 return -ENOMEM;
180 }
181
182 *res_base = base;
183 *res_size = size;
184
185 return 0;
186}
187
Marek Szyprowskif618c472014-02-28 14:42:49 +0100188static const struct of_device_id __rmem_of_table_sentinel
189 __used __section(__reservedmem_of_table_end);
190
191/**
192 * res_mem_init_node() - call region specific reserved memory init code
193 */
194static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
195{
196 extern const struct of_device_id __reservedmem_of_table[];
197 const struct of_device_id *i;
198
199 for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
200 reservedmem_of_init_fn initfn = i->data;
201 const char *compat = i->compatible;
202
203 if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
204 continue;
205
Rob Herring9dd31072014-05-08 16:06:17 -0500206 if (initfn(rmem) == 0) {
Rob Herring606ad422016-06-15 08:32:18 -0500207 pr_info("initialized node %s, compatible id %s\n",
Marek Szyprowskif618c472014-02-28 14:42:49 +0100208 rmem->name, compat);
209 return 0;
210 }
211 }
212 return -ENOENT;
213}
214
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700215static int __init __rmem_cmp(const void *a, const void *b)
216{
217 const struct reserved_mem *ra = a, *rb = b;
218
Michael Ellerman9eb8cd22015-11-18 21:46:38 +1100219 if (ra->base < rb->base)
220 return -1;
221
222 if (ra->base > rb->base)
223 return 1;
224
225 return 0;
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700226}
227
228static void __init __rmem_check_for_overlap(void)
229{
230 int i;
231
232 if (reserved_mem_count < 2)
233 return;
234
235 sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]),
236 __rmem_cmp, NULL);
237 for (i = 0; i < reserved_mem_count - 1; i++) {
238 struct reserved_mem *this, *next;
239
240 this = &reserved_mem[i];
241 next = &reserved_mem[i + 1];
242 if (!(this->base && next->base))
243 continue;
244 if (this->base + this->size > next->base) {
245 phys_addr_t this_end, next_end;
246
247 this_end = this->base + this->size;
248 next_end = next->base + next->size;
Rob Herring606ad422016-06-15 08:32:18 -0500249 pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
Michael Ellerman85a1c772015-11-10 16:08:33 +1100250 this->name, &this->base, &this_end,
251 next->name, &next->base, &next_end);
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700252 }
253 }
254}
255
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100256/**
257 * fdt_init_reserved_mem - allocate and init all saved reserved memory regions
258 */
259void __init fdt_init_reserved_mem(void)
260{
261 int i;
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700262
263 /* check for overlapping reserved regions */
264 __rmem_check_for_overlap();
265
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100266 for (i = 0; i < reserved_mem_count; i++) {
267 struct reserved_mem *rmem = &reserved_mem[i];
268 unsigned long node = rmem->fdt_node;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200269 int len;
270 const __be32 *prop;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100271 int err = 0;
272
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200273 prop = of_get_flat_dt_prop(node, "phandle", &len);
274 if (!prop)
275 prop = of_get_flat_dt_prop(node, "linux,phandle", &len);
276 if (prop)
277 rmem->phandle = of_read_number(prop, len/4);
278
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100279 if (rmem->size == 0)
280 err = __reserved_mem_alloc_size(node, rmem->name,
281 &rmem->base, &rmem->size);
Marek Szyprowskif618c472014-02-28 14:42:49 +0100282 if (err == 0)
283 __reserved_mem_init_node(rmem);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100284 }
285}
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200286
287static inline struct reserved_mem *__find_rmem(struct device_node *node)
288{
289 unsigned int i;
290
291 if (!node->phandle)
292 return NULL;
293
294 for (i = 0; i < reserved_mem_count; i++)
295 if (reserved_mem[i].phandle == node->phandle)
296 return &reserved_mem[i];
297 return NULL;
298}
299
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200300struct rmem_assigned_device {
301 struct device *dev;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200302 struct reserved_mem *rmem;
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200303 struct list_head list;
304};
305
306static LIST_HEAD(of_rmem_assigned_device_list);
307static DEFINE_MUTEX(of_rmem_assigned_device_mutex);
308
309/**
310 * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
311 * given device
312 * @dev: Pointer to the device to configure
313 * @np: Pointer to the device_node with 'reserved-memory' property
314 * @idx: Index of selected region
315 *
316 * This function assigns respective DMA-mapping operations based on reserved
317 * memory region specified by 'memory-region' property in @np node to the @dev
318 * device. When driver needs to use more than one reserved memory region, it
319 * should allocate child devices and initialize regions by name for each of
320 * child device.
321 *
322 * Returns error code or zero on success.
323 */
324int of_reserved_mem_device_init_by_idx(struct device *dev,
325 struct device_node *np, int idx)
326{
327 struct rmem_assigned_device *rd;
328 struct device_node *target;
329 struct reserved_mem *rmem;
Marek Szyprowski47f29df2014-10-29 14:50:29 -0700330 int ret;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200331
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200332 if (!np || !dev)
333 return -EINVAL;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200334
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200335 target = of_parse_phandle(np, "memory-region", idx);
336 if (!target)
Marek Szyprowski9f5a8022016-06-08 03:51:53 -0300337 return -ENODEV;
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200338
339 rmem = __find_rmem(target);
340 of_node_put(target);
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200341
342 if (!rmem || !rmem->ops || !rmem->ops->device_init)
Marek Szyprowski47f29df2014-10-29 14:50:29 -0700343 return -EINVAL;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200344
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200345 rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL);
346 if (!rd)
347 return -ENOMEM;
348
Marek Szyprowski47f29df2014-10-29 14:50:29 -0700349 ret = rmem->ops->device_init(rmem, dev);
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200350 if (ret == 0) {
351 rd->dev = dev;
352 rd->rmem = rmem;
353
354 mutex_lock(&of_rmem_assigned_device_mutex);
355 list_add(&rd->list, &of_rmem_assigned_device_list);
356 mutex_unlock(&of_rmem_assigned_device_mutex);
357
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);