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>
18 #include <linux/of_fdt.h>
19 #include <linux/of_platform.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)
35 * We use __memblock_alloc_base() because memblock_alloc_base()
36 * panic()s on allocation failure.
38 phys_addr_t base = __memblock_alloc_base(size, align, end);
43 * Check if the allocated region fits in to start..end window
46 memblock_free(base, size);
52 return memblock_remove(base, size);
56 int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
57 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
58 phys_addr_t *res_base)
60 pr_err("Reserved memory not supported, ignoring region 0x%llx%s\n",
61 size, nomap ? " (nomap)" : "");
67 * res_mem_save_node() - save fdt node for second pass initialization
69 void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
70 phys_addr_t base, phys_addr_t size)
72 struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
74 if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) {
75 pr_err("Reserved memory: not enough space all defined regions.\n");
79 rmem->fdt_node = node;
89 * res_mem_alloc_size() - allocate reserved memory described by 'size', 'align'
90 * and 'alloc-ranges' properties
92 static int __init __reserved_mem_alloc_size(unsigned long node,
93 const char *uname, phys_addr_t *res_base, phys_addr_t *res_size)
95 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
96 phys_addr_t start = 0, end = 0;
97 phys_addr_t base = 0, align = 0, size;
103 prop = of_get_flat_dt_prop(node, "size", &len);
107 if (len != dt_root_size_cells * sizeof(__be32)) {
108 pr_err("Reserved memory: invalid size property in '%s' node.\n",
112 size = dt_mem_next_cell(dt_root_size_cells, &prop);
114 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
116 prop = of_get_flat_dt_prop(node, "alignment", &len);
118 if (len != dt_root_addr_cells * sizeof(__be32)) {
119 pr_err("Reserved memory: invalid alignment property in '%s' node.\n",
123 align = dt_mem_next_cell(dt_root_addr_cells, &prop);
126 prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
129 if (len % t_len != 0) {
130 pr_err("Reserved memory: invalid alloc-ranges property in '%s', skipping node.\n",
138 start = dt_mem_next_cell(dt_root_addr_cells, &prop);
139 end = start + dt_mem_next_cell(dt_root_size_cells,
142 ret = early_init_dt_alloc_reserved_memory_arch(size,
143 align, start, end, nomap, &base);
145 pr_debug("Reserved memory: allocated memory for '%s' node: base %pa, size %ld MiB\n",
147 (unsigned long)size / SZ_1M);
154 ret = early_init_dt_alloc_reserved_memory_arch(size, align,
157 pr_debug("Reserved memory: allocated memory for '%s' node: base %pa, size %ld MiB\n",
158 uname, &base, (unsigned long)size / SZ_1M);
162 pr_info("Reserved memory: failed to allocate memory for node '%s'\n",
173 static const struct of_device_id __rmem_of_table_sentinel
174 __used __section(__reservedmem_of_table_end);
177 * res_mem_init_node() - call region specific reserved memory init code
179 static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
181 extern const struct of_device_id __reservedmem_of_table[];
182 const struct of_device_id *i;
184 for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
185 reservedmem_of_init_fn initfn = i->data;
186 const char *compat = i->compatible;
188 if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
191 if (initfn(rmem) == 0) {
192 pr_info("Reserved memory: initialized node %s, compatible id %s\n",
201 * fdt_init_reserved_mem - allocate and init all saved reserved memory regions
203 void __init fdt_init_reserved_mem(void)
206 for (i = 0; i < reserved_mem_count; i++) {
207 struct reserved_mem *rmem = &reserved_mem[i];
208 unsigned long node = rmem->fdt_node;
213 prop = of_get_flat_dt_prop(node, "phandle", &len);
215 prop = of_get_flat_dt_prop(node, "linux,phandle", &len);
217 rmem->phandle = of_read_number(prop, len/4);
220 err = __reserved_mem_alloc_size(node, rmem->name,
221 &rmem->base, &rmem->size);
223 __reserved_mem_init_node(rmem);
227 static inline struct reserved_mem *__find_rmem(struct device_node *node)
234 for (i = 0; i < reserved_mem_count; i++)
235 if (reserved_mem[i].phandle == node->phandle)
236 return &reserved_mem[i];
241 * of_reserved_mem_device_init() - assign reserved memory region to given device
243 * This function assign memory region pointed by "memory-region" device tree
244 * property to the given device.
246 int of_reserved_mem_device_init(struct device *dev)
248 struct reserved_mem *rmem;
249 struct device_node *np;
252 np = of_parse_phandle(dev->of_node, "memory-region", 0);
256 rmem = __find_rmem(np);
259 if (!rmem || !rmem->ops || !rmem->ops->device_init)
262 ret = rmem->ops->device_init(rmem, dev);
264 dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
268 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init);
271 * of_reserved_mem_device_release() - release reserved memory device structures
273 * This function releases structures allocated for memory region handling for
276 void of_reserved_mem_device_release(struct device *dev)
278 struct reserved_mem *rmem;
279 struct device_node *np;
281 np = of_parse_phandle(dev->of_node, "memory-region", 0);
285 rmem = __find_rmem(np);
288 if (!rmem || !rmem->ops || !rmem->ops->device_release)
291 rmem->ops->device_release(rmem, dev);
293 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);