blob: 29b81dbd3c37ca455cbcc0da8bd53e57ba7fd173 [file] [log] [blame]
Jon Medhurstbb1fe202014-01-24 15:41:33 +00001/*
2 * Copyright (c) 2014, 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
Dan Handleyb226a4d2014-05-16 14:08:45 +010031#include <arch.h>
32#include <arch_helpers.h>
Jon Medhurstbb1fe202014-01-24 15:41:33 +000033#include <assert.h>
Dan Handleyed6ff952014-05-14 17:44:19 +010034#include <platform_def.h>
Jon Medhurstbb1fe202014-01-24 15:41:33 +000035#include <string.h>
36#include <xlat_tables.h>
37
38
39#ifndef DEBUG_XLAT_TABLE
40#define DEBUG_XLAT_TABLE 0
41#endif
42
43#if DEBUG_XLAT_TABLE
44#define debug_print(...) printf(__VA_ARGS__)
45#else
46#define debug_print(...) ((void)0)
47#endif
48
49
50#define UNSET_DESC ~0ul
51
52#define NUM_L1_ENTRIES (ADDR_SPACE_SIZE >> L1_XLAT_ADDRESS_SHIFT)
53
Dan Handleyb226a4d2014-05-16 14:08:45 +010054static uint64_t l1_xlation_table[NUM_L1_ENTRIES]
Jon Medhurstbb1fe202014-01-24 15:41:33 +000055__aligned(NUM_L1_ENTRIES * sizeof(uint64_t));
56
57static uint64_t xlat_tables[MAX_XLAT_TABLES][XLAT_TABLE_ENTRIES]
58__aligned(XLAT_TABLE_SIZE) __attribute__((section("xlat_table")));
59
60static unsigned next_xlat;
61
62/*
63 * Array of all memory regions stored in order of ascending base address.
64 * The list is terminated by the first entry with size == 0.
65 */
Dan Handleye2712bc2014-04-10 15:37:22 +010066static mmap_region_t mmap[MAX_MMAP_REGIONS + 1];
Jon Medhurstbb1fe202014-01-24 15:41:33 +000067
68
69static void print_mmap(void)
70{
71#if DEBUG_XLAT_TABLE
72 debug_print("mmap:\n");
Dan Handleye2712bc2014-04-10 15:37:22 +010073 mmap_region_t *mm = mmap;
Jon Medhurstbb1fe202014-01-24 15:41:33 +000074 while (mm->size) {
75 debug_print(" %010lx %10lx %x\n", mm->base, mm->size, mm->attr);
76 ++mm;
77 };
78 debug_print("\n");
79#endif
80}
81
82void mmap_add_region(unsigned long base, unsigned long size, unsigned attr)
83{
Dan Handleye2712bc2014-04-10 15:37:22 +010084 mmap_region_t *mm = mmap;
85 mmap_region_t *mm_last = mm + sizeof(mmap) / sizeof(mmap[0]) - 1;
Jon Medhurstbb1fe202014-01-24 15:41:33 +000086
87 assert(IS_PAGE_ALIGNED(base));
88 assert(IS_PAGE_ALIGNED(size));
89
90 if (!size)
91 return;
92
93 /* Find correct place in mmap to insert new region */
94 while (mm->base < base && mm->size)
95 ++mm;
96
97 /* Make room for new region by moving other regions up by one place */
98 memmove(mm + 1, mm, (uintptr_t)mm_last - (uintptr_t)mm);
99
100 /* Check we haven't lost the empty sentinal from the end of the array */
101 assert(mm_last->size == 0);
102
103 mm->base = base;
104 mm->size = size;
105 mm->attr = attr;
106}
107
Dan Handleye2712bc2014-04-10 15:37:22 +0100108void mmap_add(const mmap_region_t *mm)
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000109{
110 while (mm->size) {
111 mmap_add_region(mm->base, mm->size, mm->attr);
112 ++mm;
113 }
114}
115
116static unsigned long mmap_desc(unsigned attr, unsigned long addr,
117 unsigned level)
118{
119 unsigned long desc = addr;
120
121 desc |= level == 3 ? TABLE_DESC : BLOCK_DESC;
122
123 desc |= attr & MT_NS ? LOWER_ATTRS(NS) : 0;
124
125 desc |= attr & MT_RW ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
126
127 desc |= LOWER_ATTRS(ACCESS_FLAG);
128
129 if (attr & MT_MEMORY) {
130 desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);
131 if (attr & MT_RW)
132 desc |= UPPER_ATTRS(XN);
133 } else {
134 desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
135 desc |= UPPER_ATTRS(XN);
136 }
137
138 debug_print(attr & MT_MEMORY ? "MEM" : "DEV");
139 debug_print(attr & MT_RW ? "-RW" : "-RO");
140 debug_print(attr & MT_NS ? "-NS" : "-S");
141
142 return desc;
143}
144
Dan Handleye2712bc2014-04-10 15:37:22 +0100145static int mmap_region_attr(mmap_region_t *mm, unsigned long base,
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000146 unsigned long size)
147{
148 int attr = mm->attr;
149
150 for (;;) {
151 ++mm;
152
153 if (!mm->size)
154 return attr; /* Reached end of list */
155
156 if (mm->base >= base + size)
157 return attr; /* Next region is after area so end */
158
159 if (mm->base + mm->size <= base)
160 continue; /* Next region has already been overtaken */
161
162 if ((mm->attr & attr) == attr)
163 continue; /* Region doesn't override attribs so skip */
164
165 attr &= mm->attr;
166
167 if (mm->base > base || mm->base + mm->size < base + size)
168 return -1; /* Region doesn't fully cover our area */
169 }
170}
171
Dan Handleye2712bc2014-04-10 15:37:22 +0100172static mmap_region_t *init_xlation_table(mmap_region_t *mm, unsigned long base,
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000173 unsigned long *table, unsigned level)
174{
175 unsigned level_size_shift = L1_XLAT_ADDRESS_SHIFT - (level - 1) *
176 XLAT_TABLE_ENTRIES_SHIFT;
177 unsigned level_size = 1 << level_size_shift;
Lin Ma0b9d59f2014-05-20 11:25:55 -0700178 unsigned long level_index_mask = XLAT_TABLE_ENTRIES_MASK << level_size_shift;
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000179
180 assert(level <= 3);
181
182 debug_print("New xlat table:\n");
183
184 do {
185 unsigned long desc = UNSET_DESC;
186
187 if (mm->base + mm->size <= base) {
188 /* Area now after the region so skip it */
189 ++mm;
190 continue;
191 }
192
193 debug_print(" %010lx %8lx " + 6 - 2 * level, base, level_size);
194
195 if (mm->base >= base + level_size) {
196 /* Next region is after area so nothing to map yet */
197 desc = INVALID_DESC;
198 } else if (mm->base <= base &&
199 mm->base + mm->size >= base + level_size) {
200 /* Next region covers all of area */
201 int attr = mmap_region_attr(mm, base, level_size);
202 if (attr >= 0)
203 desc = mmap_desc(attr, base, level);
204 }
205 /* else Next region only partially covers area, so need */
206
207 if (desc == UNSET_DESC) {
208 /* Area not covered by a region so need finer table */
209 unsigned long *new_table = xlat_tables[next_xlat++];
210 assert(next_xlat <= MAX_XLAT_TABLES);
211 desc = TABLE_DESC | (unsigned long)new_table;
212
213 /* Recurse to fill in new table */
214 mm = init_xlation_table(mm, base, new_table, level+1);
215 }
216
217 debug_print("\n");
218
219 *table++ = desc;
220 base += level_size;
221 } while (mm->size && (base & level_index_mask));
222
223 return mm;
224}
225
226void init_xlat_tables(void)
227{
228 print_mmap();
229 init_xlation_table(mmap, 0, l1_xlation_table, 1);
230}
Dan Handleyb226a4d2014-05-16 14:08:45 +0100231
232/*******************************************************************************
233 * Macro generating the code for the function enabling the MMU in the given
234 * exception level, assuming that the pagetables have already been created.
235 *
236 * _el: Exception level at which the function will run
237 * _tcr_extra: Extra bits to set in the TCR register. This mask will
238 * be OR'ed with the default TCR value.
239 * _tlbi_fct: Function to invalidate the TLBs at the current
240 * exception level
241 ******************************************************************************/
242#define DEFINE_ENABLE_MMU_EL(_el, _tcr_extra, _tlbi_fct) \
243 void enable_mmu_el##_el(void) \
244 { \
245 uint64_t mair, tcr, ttbr; \
246 uint32_t sctlr; \
247 \
248 assert(IS_IN_EL(_el)); \
249 assert((read_sctlr_el##_el() & SCTLR_M_BIT) == 0); \
250 \
251 /* Set attributes in the right indices of the MAIR */ \
252 mair = MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX); \
253 mair |= MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, \
254 ATTR_IWBWA_OWBWA_NTR_INDEX); \
255 write_mair_el##_el(mair); \
256 \
257 /* Invalidate TLBs at the current exception level */ \
258 _tlbi_fct(); \
259 \
260 /* Set TCR bits as well. */ \
261 /* Inner & outer WBWA & shareable + T0SZ = 32 */ \
262 tcr = TCR_SH_INNER_SHAREABLE | TCR_RGN_OUTER_WBA | \
263 TCR_RGN_INNER_WBA | TCR_T0SZ_4GB; \
264 tcr |= _tcr_extra; \
265 write_tcr_el##_el(tcr); \
266 \
267 /* Set TTBR bits as well */ \
268 ttbr = (uint64_t) l1_xlation_table; \
269 write_ttbr0_el##_el(ttbr); \
270 \
271 /* Ensure all translation table writes have drained */ \
272 /* into memory, the TLB invalidation is complete, */ \
273 /* and translation register writes are committed */ \
274 /* before enabling the MMU */ \
275 dsb(); \
276 isb(); \
277 \
278 sctlr = read_sctlr_el##_el(); \
279 sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT | SCTLR_I_BIT; \
280 sctlr |= SCTLR_A_BIT | SCTLR_C_BIT; \
281 write_sctlr_el##_el(sctlr); \
282 \
283 /* Ensure the MMU enable takes effect immediately */ \
284 isb(); \
285 }
286
287/* Define EL1 and EL3 variants of the function enabling the MMU */
288DEFINE_ENABLE_MMU_EL(1, 0, tlbivmalle1)
289DEFINE_ENABLE_MMU_EL(3, TCR_EL3_RES1, tlbialle3)