Commit | Line | Data |
---|---|---|
6fa3eb70 S |
1 | /* |
2 | * Device tree based initialization code for reserved memory. | |
3 | * | |
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> | |
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 <mach/mtk_memcfg.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> | |
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 | /* | |
36 | * We use __memblock_alloc_base() because memblock_alloc_base() | |
37 | * panic()s on allocation failure. | |
38 | */ | |
39 | phys_addr_t base = __memblock_alloc_base(size, align, end); | |
40 | if (!base) | |
41 | return -ENOMEM; | |
42 | ||
43 | /* | |
44 | * Check if the allocated region fits in to start..end window | |
45 | */ | |
46 | if (base < start) { | |
47 | memblock_free(base, size); | |
48 | return -ENOMEM; | |
49 | } | |
50 | ||
51 | *res_base = base; | |
52 | if (nomap) | |
53 | return memblock_remove(base, size); | |
54 | return 0; | |
55 | } | |
56 | #else | |
57 | int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size, | |
58 | phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap, | |
59 | phys_addr_t *res_base) | |
60 | { | |
61 | pr_err("Reserved memory not supported, ignoring region 0x%llx%s\n", | |
62 | size, nomap ? " (nomap)" : ""); | |
63 | return -ENOSYS; | |
64 | } | |
65 | #endif | |
66 | ||
67 | /** | |
68 | * res_mem_save_node() - save fdt node for second pass initialization | |
69 | */ | |
70 | void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname, | |
71 | phys_addr_t base, phys_addr_t size) | |
72 | { | |
73 | struct reserved_mem *rmem = &reserved_mem[reserved_mem_count]; | |
74 | ||
75 | if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) { | |
76 | pr_err("Reserved memory: not enough space all defined regions.\n"); | |
77 | return; | |
78 | } | |
79 | ||
80 | rmem->fdt_node = node; | |
81 | rmem->name = uname; | |
82 | rmem->base = base; | |
83 | rmem->size = size; | |
84 | ||
85 | reserved_mem_count++; | |
86 | return; | |
87 | } | |
88 | ||
89 | /** | |
90 | * res_mem_alloc_size() - allocate reserved memory described by 'size', 'align' | |
91 | * and 'alloc-ranges' properties | |
92 | */ | |
93 | static int __init __reserved_mem_alloc_size(unsigned long node, | |
94 | const char *uname, phys_addr_t *res_base, phys_addr_t *res_size) | |
95 | { | |
96 | int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32); | |
97 | phys_addr_t start = 0, end = 0; | |
98 | phys_addr_t base = 0, align = 0, size; | |
99 | unsigned long len; | |
100 | __be32 *prop; | |
101 | int nomap; | |
102 | int ret; | |
103 | ||
104 | prop = of_get_flat_dt_prop(node, "size", &len); | |
105 | if (!prop) | |
106 | return -EINVAL; | |
107 | ||
108 | if (len != dt_root_size_cells * sizeof(__be32)) { | |
109 | pr_err("Reserved memory: invalid size property in '%s' node.\n", | |
110 | uname); | |
111 | return -EINVAL; | |
112 | } | |
113 | size = dt_mem_next_cell(dt_root_size_cells, &prop); | |
114 | ||
115 | nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL; | |
116 | ||
117 | prop = of_get_flat_dt_prop(node, "alignment", &len); | |
118 | if (prop) { | |
119 | if (len != dt_root_addr_cells * sizeof(__be32)) { | |
120 | pr_err("Reserved memory: invalid alignment property in '%s' node.\n", | |
121 | uname); | |
122 | return -EINVAL; | |
123 | } | |
124 | align = dt_mem_next_cell(dt_root_addr_cells, &prop); | |
125 | } | |
126 | ||
127 | prop = of_get_flat_dt_prop(node, "alloc-ranges", &len); | |
128 | if (prop) { | |
129 | ||
130 | if (len % t_len != 0) { | |
131 | pr_err("Reserved memory: invalid alloc-ranges property in '%s', skipping node.\n", | |
132 | uname); | |
133 | return -EINVAL; | |
134 | } | |
135 | ||
136 | base = 0; | |
137 | ||
138 | while (len > 0) { | |
139 | start = dt_mem_next_cell(dt_root_addr_cells, &prop); | |
140 | end = start + dt_mem_next_cell(dt_root_size_cells, | |
141 | &prop); | |
142 | ||
143 | ret = early_init_dt_alloc_reserved_memory_arch(size, | |
144 | align, start, end, nomap, &base); | |
145 | if (ret == 0) { | |
146 | pr_debug("Reserved memory: allocated memory for '%s' node: base %pa, size %ld MiB\n", | |
147 | uname, &base, | |
148 | (unsigned long)size / SZ_1M); | |
149 | break; | |
150 | } | |
151 | len -= t_len; | |
152 | } | |
153 | ||
154 | } else { | |
155 | ret = early_init_dt_alloc_reserved_memory_arch(size, align, | |
156 | 0, 0, nomap, &base); | |
157 | if (ret == 0) | |
158 | pr_debug("Reserved memory: allocated memory for '%s' node: base %pa, size %ld MiB\n", | |
159 | uname, &base, (unsigned long)size / SZ_1M); | |
160 | } | |
161 | ||
162 | if (base == 0) { | |
163 | pr_info("Reserved memory: failed to allocate memory for node '%s'\n", | |
164 | uname); | |
165 | return -ENOMEM; | |
166 | } | |
167 | ||
168 | *res_base = base; | |
169 | *res_size = size; | |
170 | MTK_MEMCFG_LOG_AND_PRINTK(KERN_ALERT | |
171 | "[PHY layout]%s : " | |
172 | "0x%08llx - 0x%08llx (0x%llx)\n", | |
173 | uname, | |
174 | (unsigned long long)base, | |
175 | (unsigned long long)base + size - 1, | |
176 | (unsigned long long)size); | |
177 | ||
178 | return 0; | |
179 | } | |
180 | ||
181 | static const struct of_device_id __rmem_of_table_sentinel | |
182 | __used __section(__reservedmem_of_table_end); | |
183 | ||
184 | /** | |
185 | * res_mem_init_node() - call region specific reserved memory init code | |
186 | */ | |
187 | static int __init __reserved_mem_init_node(struct reserved_mem *rmem) | |
188 | { | |
189 | extern const struct of_device_id __reservedmem_of_table[]; | |
190 | const struct of_device_id *i; | |
191 | ||
192 | for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) { | |
193 | reservedmem_of_init_fn initfn = i->data; | |
194 | const char *compat = i->compatible; | |
195 | ||
196 | if (!of_flat_dt_is_compatible(rmem->fdt_node, compat)) | |
197 | continue; | |
198 | ||
199 | if (initfn(rmem, rmem->fdt_node, rmem->name) == 0) { | |
200 | pr_info("Reserved memory: initialized node %s, compatible id %s\n", | |
201 | rmem->name, compat); | |
202 | return 0; | |
203 | } | |
204 | } | |
205 | return -ENOENT; | |
206 | } | |
207 | ||
208 | /** | |
209 | * fdt_init_reserved_mem - allocate and init all saved reserved memory regions | |
210 | */ | |
211 | void __init fdt_init_reserved_mem(void) | |
212 | { | |
213 | int i; | |
214 | for (i = 0; i < reserved_mem_count; i++) { | |
215 | struct reserved_mem *rmem = &reserved_mem[i]; | |
216 | unsigned long node = rmem->fdt_node; | |
217 | int err = 0; | |
218 | ||
219 | if (rmem->size == 0) | |
220 | err = __reserved_mem_alloc_size(node, rmem->name, | |
221 | &rmem->base, &rmem->size); | |
222 | if (err == 0) | |
223 | __reserved_mem_init_node(rmem); | |
224 | } | |
225 | } |