gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / drivers / of / of_reserved_mem.c
blob26fb7b0b856cd60fd743dbd0793b24005faf74e7
1 /*
2 * Device tree based initialization code for reserved memory.
4 * Copyright (c) 2013, 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>
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.
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>
24 #define MAX_RESERVED_REGIONS 16
25 static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
26 static int reserved_mem_count;
28 #if defined(CONFIG_HAVE_MEMBLOCK)
29 #include <linux/memblock.h>
30 int __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)
34 phys_addr_t base;
36 * We use __memblock_alloc_base() because memblock_alloc_base()
37 * panic()s on allocation failure.
39 end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
40 base = __memblock_alloc_base(size, align, end);
41 if (!base)
42 return -ENOMEM;
45 * Check if the allocated region fits in to start..end window
47 if (base < start) {
48 memblock_free(base, size);
49 return -ENOMEM;
52 *res_base = base;
53 if (nomap)
54 return memblock_remove(base, size);
55 return 0;
57 #else
58 int __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)
62 pr_err("Reserved memory not supported, ignoring region 0x%llx%s\n",
63 size, nomap ? " (nomap)" : "");
64 return -ENOSYS;
66 #endif
68 /**
69 * res_mem_save_node() - save fdt node for second pass initialization
71 void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
72 phys_addr_t base, phys_addr_t size)
74 struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
76 if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) {
77 pr_err("Reserved memory: not enough space all defined regions.\n");
78 return;
81 rmem->fdt_node = node;
82 rmem->name = uname;
83 rmem->base = base;
84 rmem->size = size;
86 reserved_mem_count++;
87 return;
90 /**
91 * res_mem_alloc_size() - allocate reserved memory described by 'size', 'align'
92 * and 'alloc-ranges' properties
94 static int __init __reserved_mem_alloc_size(unsigned long node,
95 const char *uname, phys_addr_t *res_base, phys_addr_t *res_size)
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;
100 int len;
101 const __be32 *prop;
102 int nomap;
103 int ret;
105 prop = of_get_flat_dt_prop(node, "size", &len);
106 if (!prop)
107 return -EINVAL;
109 if (len != dt_root_size_cells * sizeof(__be32)) {
110 pr_err("Reserved memory: invalid size property in '%s' node.\n",
111 uname);
112 return -EINVAL;
114 size = dt_mem_next_cell(dt_root_size_cells, &prop);
116 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
118 prop = of_get_flat_dt_prop(node, "alignment", &len);
119 if (prop) {
120 if (len != dt_root_addr_cells * sizeof(__be32)) {
121 pr_err("Reserved memory: invalid alignment property in '%s' node.\n",
122 uname);
123 return -EINVAL;
125 align = dt_mem_next_cell(dt_root_addr_cells, &prop);
128 prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
129 if (prop) {
131 if (len % t_len != 0) {
132 pr_err("Reserved memory: invalid alloc-ranges property in '%s', skipping node.\n",
133 uname);
134 return -EINVAL;
137 base = 0;
139 while (len > 0) {
140 start = dt_mem_next_cell(dt_root_addr_cells, &prop);
141 end = start + dt_mem_next_cell(dt_root_size_cells,
142 &prop);
144 ret = early_init_dt_alloc_reserved_memory_arch(size,
145 align, start, end, nomap, &base);
146 if (ret == 0) {
147 pr_debug("Reserved memory: allocated memory for '%s' node: base %pa, size %ld MiB\n",
148 uname, &base,
149 (unsigned long)size / SZ_1M);
150 break;
152 len -= t_len;
155 } else {
156 ret = early_init_dt_alloc_reserved_memory_arch(size, align,
157 0, 0, nomap, &base);
158 if (ret == 0)
159 pr_debug("Reserved memory: allocated memory for '%s' node: base %pa, size %ld MiB\n",
160 uname, &base, (unsigned long)size / SZ_1M);
163 if (base == 0) {
164 pr_info("Reserved memory: failed to allocate memory for node '%s'\n",
165 uname);
166 return -ENOMEM;
169 *res_base = base;
170 *res_size = size;
172 return 0;
175 static const struct of_device_id __rmem_of_table_sentinel
176 __used __section(__reservedmem_of_table_end);
179 * res_mem_init_node() - call region specific reserved memory init code
181 static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
183 extern const struct of_device_id __reservedmem_of_table[];
184 const struct of_device_id *i;
186 for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
187 reservedmem_of_init_fn initfn = i->data;
188 const char *compat = i->compatible;
190 if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
191 continue;
193 if (initfn(rmem) == 0) {
194 pr_info("Reserved memory: initialized node %s, compatible id %s\n",
195 rmem->name, compat);
196 return 0;
199 return -ENOENT;
203 * fdt_init_reserved_mem - allocate and init all saved reserved memory regions
205 void __init fdt_init_reserved_mem(void)
207 int i;
208 for (i = 0; i < reserved_mem_count; i++) {
209 struct reserved_mem *rmem = &reserved_mem[i];
210 unsigned long node = rmem->fdt_node;
211 int len;
212 const __be32 *prop;
213 int err = 0;
215 prop = of_get_flat_dt_prop(node, "phandle", &len);
216 if (!prop)
217 prop = of_get_flat_dt_prop(node, "linux,phandle", &len);
218 if (prop)
219 rmem->phandle = of_read_number(prop, len/4);
221 if (rmem->size == 0)
222 err = __reserved_mem_alloc_size(node, rmem->name,
223 &rmem->base, &rmem->size);
224 if (err == 0)
225 __reserved_mem_init_node(rmem);
229 static inline struct reserved_mem *__find_rmem(struct device_node *node)
231 unsigned int i;
233 if (!node->phandle)
234 return NULL;
236 for (i = 0; i < reserved_mem_count; i++)
237 if (reserved_mem[i].phandle == node->phandle)
238 return &reserved_mem[i];
239 return NULL;
243 * of_reserved_mem_device_init() - assign reserved memory region to given device
245 * This function assign memory region pointed by "memory-region" device tree
246 * property to the given device.
248 int of_reserved_mem_device_init(struct device *dev)
250 struct reserved_mem *rmem;
251 struct device_node *np;
252 int ret;
254 np = of_parse_phandle(dev->of_node, "memory-region", 0);
255 if (!np)
256 return -ENODEV;
258 rmem = __find_rmem(np);
259 of_node_put(np);
261 if (!rmem || !rmem->ops || !rmem->ops->device_init)
262 return -EINVAL;
264 ret = rmem->ops->device_init(rmem, dev);
265 if (ret == 0)
266 dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
268 return ret;
270 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init);
273 * of_reserved_mem_device_release() - release reserved memory device structures
275 * This function releases structures allocated for memory region handling for
276 * the given device.
278 void of_reserved_mem_device_release(struct device *dev)
280 struct reserved_mem *rmem;
281 struct device_node *np;
283 np = of_parse_phandle(dev->of_node, "memory-region", 0);
284 if (!np)
285 return;
287 rmem = __find_rmem(np);
288 of_node_put(np);
290 if (!rmem || !rmem->ops || !rmem->ops->device_release)
291 return;
293 rmem->ops->device_release(rmem, dev);
295 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);