1 /*
2 * Device tree based initialization code for reserved memory.
3 *
4 * Copyright (c) 2013, 2015 The Linux Foundation. All Rights Reserved.
5 * 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
16 #include <linux/err.h>
17 #include <linux/of.h>
18 #include <linux/of_fdt.h>
19 #include <linux/of_platform.h>
20 #include <linux/mm.h>
21 #include <linux/sizes.h>
22 #include <linux/of_reserved_mem.h>
23 #include <linux/sort.h>
24
25 #define MAX_RESERVED_REGIONS 16
26 static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
27 static int reserved_mem_count;
28
29 #if defined(CONFIG_HAVE_MEMBLOCK)
30 #include <linux/memblock.h>
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)31 int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
32 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
33 phys_addr_t *res_base)
34 {
35 phys_addr_t base;
36 /*
37 * We use __memblock_alloc_base() because memblock_alloc_base()
38 * panic()s on allocation failure.
39 */
40 end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
41 base = __memblock_alloc_base(size, align, end);
42 if (!base)
43 return -ENOMEM;
44
45 /*
46 * Check if the allocated region fits in to start..end window
47 */
48 if (base < start) {
49 memblock_free(base, size);
50 return -ENOMEM;
51 }
52
53 *res_base = base;
54 if (nomap)
55 return memblock_remove(base, size);
56 return 0;
57 }
58 #else
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)59 int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
60 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
61 phys_addr_t *res_base)
62 {
63 pr_err("Reserved memory not supported, ignoring region 0x%llx%s\n",
64 size, nomap ? " (nomap)" : "");
65 return -ENOSYS;
66 }
67 #endif
68
69 /**
70 * res_mem_save_node() - save fdt node for second pass initialization
71 */
fdt_reserved_mem_save_node(unsigned long node,const char * uname,phys_addr_t base,phys_addr_t size)72 void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
73 phys_addr_t base, phys_addr_t size)
74 {
75 struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
76
77 if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) {
78 pr_err("Reserved memory: not enough space all defined regions.\n");
79 return;
80 }
81
82 rmem->fdt_node = node;
83 rmem->name = uname;
84 rmem->base = base;
85 rmem->size = size;
86
87 reserved_mem_count++;
88 return;
89 }
90
91 /**
92 * res_mem_alloc_size() - allocate reserved memory described by 'size', 'align'
93 * and 'alloc-ranges' properties
94 */
__reserved_mem_alloc_size(unsigned long node,const char * uname,phys_addr_t * res_base,phys_addr_t * res_size)95 static int __init __reserved_mem_alloc_size(unsigned long node,
96 const char *uname, phys_addr_t *res_base, phys_addr_t *res_size)
97 {
98 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
99 phys_addr_t start = 0, end = 0;
100 phys_addr_t base = 0, align = 0, size;
101 int len;
102 const __be32 *prop;
103 int nomap;
104 int ret;
105
106 prop = of_get_flat_dt_prop(node, "size", &len);
107 if (!prop)
108 return -EINVAL;
109
110 if (len != dt_root_size_cells * sizeof(__be32)) {
111 pr_err("Reserved memory: invalid size property in '%s' node.\n",
112 uname);
113 return -EINVAL;
114 }
115 size = dt_mem_next_cell(dt_root_size_cells, &prop);
116
117 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
118
119 prop = of_get_flat_dt_prop(node, "alignment", &len);
120 if (prop) {
121 if (len != dt_root_addr_cells * sizeof(__be32)) {
122 pr_err("Reserved memory: invalid alignment property in '%s' node.\n",
123 uname);
124 return -EINVAL;
125 }
126 align = dt_mem_next_cell(dt_root_addr_cells, &prop);
127 }
128
129 /* Need adjust the alignment to satisfy the CMA requirement */
130 if (IS_ENABLED(CONFIG_CMA) && of_flat_dt_is_compatible(node, "shared-dma-pool")) {
131 unsigned long order =
132 max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
133
134 align = max(align, (phys_addr_t)PAGE_SIZE << order);
135 }
136
137 prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
138 if (prop) {
139
140 if (len % t_len != 0) {
141 pr_err("Reserved memory: invalid alloc-ranges property in '%s', skipping node.\n",
142 uname);
143 return -EINVAL;
144 }
145
146 base = 0;
147
148 while (len > 0) {
149 start = dt_mem_next_cell(dt_root_addr_cells, &prop);
150 end = start + dt_mem_next_cell(dt_root_size_cells,
151 &prop);
152
153 ret = early_init_dt_alloc_reserved_memory_arch(size,
154 align, start, end, nomap, &base);
155 if (ret == 0) {
156 pr_debug("Reserved memory: allocated memory for '%s' node: base %pa, size %ld MiB\n",
157 uname, &base,
158 (unsigned long)size / SZ_1M);
159 break;
160 }
161 len -= t_len;
162 }
163
164 } else {
165 ret = early_init_dt_alloc_reserved_memory_arch(size, align,
166 0, 0, nomap, &base);
167 if (ret == 0)
168 pr_debug("Reserved memory: allocated memory for '%s' node: base %pa, size %ld MiB\n",
169 uname, &base, (unsigned long)size / SZ_1M);
170 }
171
172 if (base == 0) {
173 pr_info("Reserved memory: failed to allocate memory for node '%s'\n",
174 uname);
175 return -ENOMEM;
176 }
177
178 *res_base = base;
179 *res_size = size;
180
181 return 0;
182 }
183
184 static 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 */
__reserved_mem_init_node(struct reserved_mem * rmem)190 static 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++) {
196 reservedmem_of_init_fn initfn = i->data;
197 const char *compat = i->compatible;
198
199 if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
200 continue;
201
202 if (initfn(rmem) == 0) {
203 pr_info("Reserved memory: initialized node %s, compatible id %s\n",
204 rmem->name, compat);
205 return 0;
206 }
207 }
208 return -ENOENT;
209 }
210
__rmem_cmp(const void * a,const void * b)211 static int __init __rmem_cmp(const void *a, const void *b)
212 {
213 const struct reserved_mem *ra = a, *rb = b;
214
215 if (ra->base < rb->base)
216 return -1;
217
218 if (ra->base > rb->base)
219 return 1;
220
221 /*
222 * Put the dynamic allocations (address == 0, size == 0) before static
223 * allocations at address 0x0 so that overlap detection works
224 * correctly.
225 */
226 if (ra->size < rb->size)
227 return -1;
228 if (ra->size > rb->size)
229 return 1;
230
231 return 0;
232 }
233
__rmem_check_for_overlap(void)234 static void __init __rmem_check_for_overlap(void)
235 {
236 int i;
237
238 if (reserved_mem_count < 2)
239 return;
240
241 sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]),
242 __rmem_cmp, NULL);
243 for (i = 0; i < reserved_mem_count - 1; i++) {
244 struct reserved_mem *this, *next;
245
246 this = &reserved_mem[i];
247 next = &reserved_mem[i + 1];
248
249 if (this->base + this->size > next->base) {
250 phys_addr_t this_end, next_end;
251
252 this_end = this->base + this->size;
253 next_end = next->base + next->size;
254 pr_err("Reserved memory: OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
255 this->name, &this->base, &this_end,
256 next->name, &next->base, &next_end);
257 }
258 }
259 }
260
261 /**
262 * fdt_init_reserved_mem - allocate and init all saved reserved memory regions
263 */
fdt_init_reserved_mem(void)264 void __init fdt_init_reserved_mem(void)
265 {
266 int i;
267
268 /* check for overlapping reserved regions */
269 __rmem_check_for_overlap();
270
271 for (i = 0; i < reserved_mem_count; i++) {
272 struct reserved_mem *rmem = &reserved_mem[i];
273 unsigned long node = rmem->fdt_node;
274 int len;
275 const __be32 *prop;
276 int err = 0;
277
278 prop = of_get_flat_dt_prop(node, "phandle", &len);
279 if (!prop)
280 prop = of_get_flat_dt_prop(node, "linux,phandle", &len);
281 if (prop)
282 rmem->phandle = of_read_number(prop, len/4);
283
284 if (rmem->size == 0)
285 err = __reserved_mem_alloc_size(node, rmem->name,
286 &rmem->base, &rmem->size);
287 if (err == 0)
288 __reserved_mem_init_node(rmem);
289 }
290 }
291
__find_rmem(struct device_node * node)292 static inline struct reserved_mem *__find_rmem(struct device_node *node)
293 {
294 unsigned int i;
295
296 if (!node->phandle)
297 return NULL;
298
299 for (i = 0; i < reserved_mem_count; i++)
300 if (reserved_mem[i].phandle == node->phandle)
301 return &reserved_mem[i];
302 return NULL;
303 }
304
305 /**
306 * of_reserved_mem_device_init() - assign reserved memory region to given device
307 *
308 * This function assign memory region pointed by "memory-region" device tree
309 * property to the given device.
310 */
of_reserved_mem_device_init(struct device * dev)311 int of_reserved_mem_device_init(struct device *dev)
312 {
313 struct reserved_mem *rmem;
314 struct device_node *np;
315 int ret;
316
317 np = of_parse_phandle(dev->of_node, "memory-region", 0);
318 if (!np)
319 return -ENODEV;
320
321 rmem = __find_rmem(np);
322 of_node_put(np);
323
324 if (!rmem || !rmem->ops || !rmem->ops->device_init)
325 return -EINVAL;
326
327 ret = rmem->ops->device_init(rmem, dev);
328 if (ret == 0)
329 dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
330
331 return ret;
332 }
333 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init);
334
335 /**
336 * of_reserved_mem_device_release() - release reserved memory device structures
337 *
338 * This function releases structures allocated for memory region handling for
339 * the given device.
340 */
of_reserved_mem_device_release(struct device * dev)341 void of_reserved_mem_device_release(struct device *dev)
342 {
343 struct reserved_mem *rmem;
344 struct device_node *np;
345
346 np = of_parse_phandle(dev->of_node, "memory-region", 0);
347 if (!np)
348 return;
349
350 rmem = __find_rmem(np);
351 of_node_put(np);
352
353 if (!rmem || !rmem->ops || !rmem->ops->device_release)
354 return;
355
356 rmem->ops->device_release(rmem, dev);
357 }
358 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
359