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
)
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
);
45 * Check if the allocated region fits in to start..end window
48 memblock_free(base
, size
);
54 return memblock_remove(base
, size
);
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)" : "");
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");
81 rmem
->fdt_node
= node
;
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
;
105 prop
= of_get_flat_dt_prop(node
, "size", &len
);
109 if (len
!= dt_root_size_cells
* sizeof(__be32
)) {
110 pr_err("Reserved memory: invalid size property in '%s' node.\n",
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
);
120 if (len
!= dt_root_addr_cells
* sizeof(__be32
)) {
121 pr_err("Reserved memory: invalid alignment property in '%s' node.\n",
125 align
= dt_mem_next_cell(dt_root_addr_cells
, &prop
);
128 prop
= of_get_flat_dt_prop(node
, "alloc-ranges", &len
);
131 if (len
% t_len
!= 0) {
132 pr_err("Reserved memory: invalid alloc-ranges property in '%s', skipping node.\n",
140 start
= dt_mem_next_cell(dt_root_addr_cells
, &prop
);
141 end
= start
+ dt_mem_next_cell(dt_root_size_cells
,
144 ret
= early_init_dt_alloc_reserved_memory_arch(size
,
145 align
, start
, end
, nomap
, &base
);
147 pr_debug("Reserved memory: allocated memory for '%s' node: base %pa, size %ld MiB\n",
149 (unsigned long)size
/ SZ_1M
);
156 ret
= early_init_dt_alloc_reserved_memory_arch(size
, align
,
159 pr_debug("Reserved memory: allocated memory for '%s' node: base %pa, size %ld MiB\n",
160 uname
, &base
, (unsigned long)size
/ SZ_1M
);
164 pr_info("Reserved memory: failed to allocate memory for node '%s'\n",
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
))
193 if (initfn(rmem
) == 0) {
194 pr_info("Reserved memory: initialized node %s, compatible id %s\n",
203 * fdt_init_reserved_mem - allocate and init all saved reserved memory regions
205 void __init
fdt_init_reserved_mem(void)
208 for (i
= 0; i
< reserved_mem_count
; i
++) {
209 struct reserved_mem
*rmem
= &reserved_mem
[i
];
210 unsigned long node
= rmem
->fdt_node
;
215 prop
= of_get_flat_dt_prop(node
, "phandle", &len
);
217 prop
= of_get_flat_dt_prop(node
, "linux,phandle", &len
);
219 rmem
->phandle
= of_read_number(prop
, len
/4);
222 err
= __reserved_mem_alloc_size(node
, rmem
->name
,
223 &rmem
->base
, &rmem
->size
);
225 __reserved_mem_init_node(rmem
);
229 static inline struct reserved_mem
*__find_rmem(struct device_node
*node
)
236 for (i
= 0; i
< reserved_mem_count
; i
++)
237 if (reserved_mem
[i
].phandle
== node
->phandle
)
238 return &reserved_mem
[i
];
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
;
254 np
= of_parse_phandle(dev
->of_node
, "memory-region", 0);
258 rmem
= __find_rmem(np
);
261 if (!rmem
|| !rmem
->ops
|| !rmem
->ops
->device_init
)
264 ret
= rmem
->ops
->device_init(rmem
, dev
);
266 dev_info(dev
, "assigned reserved memory node %s\n", rmem
->name
);
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
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);
287 rmem
= __find_rmem(np
);
290 if (!rmem
|| !rmem
->ops
|| !rmem
->ops
->device_release
)
293 rmem
->ops
->device_release(rmem
, dev
);
295 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release
);