blob: d686c8dc54e561326701ac3b86c467daaa77b764 [file] [log] [blame]
Soby Mathew44170c42016-03-22 15:51:08 +00001/*
2 * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <arch.h>
32#include <arch_helpers.h>
33#include <assert.h>
34#include <bl_common.h>
35#include <cassert.h>
36#include <debug.h>
37#include <platform_def.h>
38#include <string.h>
39#include <xlat_tables.h>
40
41#if LOG_LEVEL >= LOG_LEVEL_VERBOSE
42#define LVL0_SPACER ""
43#define LVL1_SPACER " "
44#define LVL2_SPACER " "
45#define LVL3_SPACER " "
46#define get_level_spacer(level) \
47 (((level) == 0) ? LVL0_SPACER : \
48 (((level) == 1) ? LVL1_SPACER : \
49 (((level) == 2) ? LVL2_SPACER : LVL3_SPACER)))
50#define debug_print(...) tf_printf(__VA_ARGS__)
51#else
52#define debug_print(...) ((void)0)
53#endif
54
55#define UNSET_DESC ~0ul
56
57static uint64_t xlat_tables[MAX_XLAT_TABLES][XLAT_TABLE_ENTRIES]
58 __aligned(XLAT_TABLE_SIZE) __section("xlat_table");
59
60static unsigned next_xlat;
61static unsigned long long xlat_max_pa;
62static uintptr_t xlat_max_va;
63
64/*
65 * Array of all memory regions stored in order of ascending base address.
66 * The list is terminated by the first entry with size == 0.
67 */
68static mmap_region_t mmap[MAX_MMAP_REGIONS + 1];
69
70
71void print_mmap(void)
72{
73#if LOG_LEVEL >= LOG_LEVEL_VERBOSE
74 debug_print("mmap:\n");
75 mmap_region_t *mm = mmap;
76 while (mm->size) {
77 debug_print(" VA:%p PA:0x%llx size:0x%zx attr:0x%x\n",
78 (void *)mm->base_va, mm->base_pa,
79 mm->size, mm->attr);
80 ++mm;
81 };
82 debug_print("\n");
83#endif
84}
85
86void mmap_add_region(unsigned long long base_pa, uintptr_t base_va,
87 size_t size, unsigned int attr)
88{
89 mmap_region_t *mm = mmap;
90 mmap_region_t *mm_last = mm + ARRAY_SIZE(mmap) - 1;
91 unsigned long long pa_end = base_pa + size - 1;
92 uintptr_t va_end = base_va + size - 1;
93
94 assert(IS_PAGE_ALIGNED(base_pa));
95 assert(IS_PAGE_ALIGNED(base_va));
96 assert(IS_PAGE_ALIGNED(size));
97
98 if (!size)
99 return;
100
101 /* Find correct place in mmap to insert new region */
102 while (mm->base_va < base_va && mm->size)
103 ++mm;
104
105 /* Make room for new region by moving other regions up by one place */
106 memmove(mm + 1, mm, (uintptr_t)mm_last - (uintptr_t)mm);
107
108 /* Check we haven't lost the empty sentinal from the end of the array */
109 assert(mm_last->size == 0);
110
111 mm->base_pa = base_pa;
112 mm->base_va = base_va;
113 mm->size = size;
114 mm->attr = attr;
115
116 if (pa_end > xlat_max_pa)
117 xlat_max_pa = pa_end;
118 if (va_end > xlat_max_va)
119 xlat_max_va = va_end;
120}
121
122void mmap_add(const mmap_region_t *mm)
123{
124 while (mm->size) {
125 mmap_add_region(mm->base_pa, mm->base_va, mm->size, mm->attr);
126 ++mm;
127 }
128}
129
130static uint64_t mmap_desc(unsigned attr, unsigned long long addr_pa,
131 int level)
132{
133 uint64_t desc = addr_pa;
134 int mem_type;
135
136 desc |= level == 3 ? TABLE_DESC : BLOCK_DESC;
137
138 desc |= attr & MT_NS ? LOWER_ATTRS(NS) : 0;
139
140 desc |= attr & MT_RW ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
141
142 desc |= LOWER_ATTRS(ACCESS_FLAG);
143
144 mem_type = MT_TYPE(attr);
145 if (mem_type == MT_MEMORY) {
146 desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);
147 if (attr & MT_RW)
148 desc |= UPPER_ATTRS(XN);
149 } else if (mem_type == MT_NON_CACHEABLE) {
150 desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH);
151 if (attr & MT_RW)
152 desc |= UPPER_ATTRS(XN);
153 } else {
154 assert(mem_type == MT_DEVICE);
155 desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
156 desc |= UPPER_ATTRS(XN);
157 }
158
159 debug_print((mem_type == MT_MEMORY) ? "MEM" :
160 ((mem_type == MT_NON_CACHEABLE) ? "NC" : "DEV"));
161 debug_print(attr & MT_RW ? "-RW" : "-RO");
162 debug_print(attr & MT_NS ? "-NS" : "-S");
163
164 return desc;
165}
166
167static int mmap_region_attr(mmap_region_t *mm, uintptr_t base_va,
168 size_t size)
169{
170 int attr = mm->attr;
171 int old_mem_type, new_mem_type;
172
173 for (;;) {
174 ++mm;
175
176 if (!mm->size)
177 return attr; /* Reached end of list */
178
179 if (mm->base_va >= base_va + size)
180 return attr; /* Next region is after area so end */
181
182 if (mm->base_va + mm->size <= base_va)
183 continue; /* Next region has already been overtaken */
184
185 if ((mm->attr & attr) == attr)
186 continue; /* Region doesn't override attribs so skip */
187
188 /*
189 * Update memory mapping attributes in 2 steps:
190 * 1) Update access permissions and security state flags
191 * 2) Update memory type.
192 *
193 * See xlat_tables.h for details about the attributes priority
194 * system and the rules dictating whether attributes should be
195 * updated.
196 */
197 old_mem_type = MT_TYPE(attr);
198 new_mem_type = MT_TYPE(mm->attr);
199 attr &= mm->attr;
200 if (new_mem_type < old_mem_type)
201 attr = (attr & ~MT_TYPE_MASK) | new_mem_type;
202
203 if (mm->base_va > base_va ||
204 mm->base_va + mm->size < base_va + size)
205 return -1; /* Region doesn't fully cover our area */
206 }
207}
208
209static mmap_region_t *init_xlation_table_inner(mmap_region_t *mm,
210 uintptr_t base_va,
211 uint64_t *table,
212 int level)
213{
214 unsigned level_size_shift = L1_XLAT_ADDRESS_SHIFT - (level - 1) *
215 XLAT_TABLE_ENTRIES_SHIFT;
216 unsigned level_size = 1 << level_size_shift;
217 unsigned long long level_index_mask = XLAT_TABLE_ENTRIES_MASK <<
218 level_size_shift;
219
220 assert(level > 0 && level <= 3);
221
222 debug_print("New xlat table:\n");
223
224 do {
225 uint64_t desc = UNSET_DESC;
226
227 if (!mm->size) {
228 /* Done mapping regions; finish zeroing the table */
229 desc = INVALID_DESC;
230 } else if (mm->base_va + mm->size <= base_va) {
231 /* Area now after the region so skip it */
232 ++mm;
233 continue;
234 }
235
236 debug_print("%s VA:%p size:0x%x ", get_level_spacer(level),
237 (void *)base_va, level_size);
238
239 if (mm->base_va >= base_va + level_size) {
240 /* Next region is after area so nothing to map yet */
241 desc = INVALID_DESC;
242 } else if (mm->base_va <= base_va && mm->base_va + mm->size >=
243 base_va + level_size) {
244 /* Next region covers all of area */
245 int attr = mmap_region_attr(mm, base_va, level_size);
246 if (attr >= 0)
247 desc = mmap_desc(attr,
248 base_va - mm->base_va + mm->base_pa,
249 level);
250 }
251 /* else Next region only partially covers area, so need */
252
253 if (desc == UNSET_DESC) {
254 /* Area not covered by a region so need finer table */
255 uint64_t *new_table = xlat_tables[next_xlat++];
256 assert(next_xlat <= MAX_XLAT_TABLES);
257 desc = TABLE_DESC | (unsigned long)new_table;
258
259 /* Recurse to fill in new table */
260 mm = init_xlation_table_inner(mm, base_va,
261 new_table, level+1);
262 }
263
264 debug_print("\n");
265
266 *table++ = desc;
267 base_va += level_size;
268 } while ((base_va & level_index_mask) && (base_va < ADDR_SPACE_SIZE));
269
270 return mm;
271}
272
273void init_xlation_table(uintptr_t base_va, uint64_t *table,
274 int level, uintptr_t *max_va,
275 unsigned long long *max_pa)
276{
277
278 init_xlation_table_inner(mmap, base_va, table, level);
279 *max_va = xlat_max_va;
280 *max_pa = xlat_max_pa;
281}