blob: 6e04f65dd742b77e43eed22eba3dd39d02680ff6 [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
31#include <assert.h>
32#include <platform.h>
33#include <string.h>
34#include <xlat_tables.h>
35
36
37#ifndef DEBUG_XLAT_TABLE
38#define DEBUG_XLAT_TABLE 0
39#endif
40
41#if DEBUG_XLAT_TABLE
42#define debug_print(...) printf(__VA_ARGS__)
43#else
44#define debug_print(...) ((void)0)
45#endif
46
47
48#define UNSET_DESC ~0ul
49
50#define NUM_L1_ENTRIES (ADDR_SPACE_SIZE >> L1_XLAT_ADDRESS_SHIFT)
51
52uint64_t l1_xlation_table[NUM_L1_ENTRIES]
53__aligned(NUM_L1_ENTRIES * sizeof(uint64_t));
54
55static uint64_t xlat_tables[MAX_XLAT_TABLES][XLAT_TABLE_ENTRIES]
56__aligned(XLAT_TABLE_SIZE) __attribute__((section("xlat_table")));
57
58static unsigned next_xlat;
59
60/*
61 * Array of all memory regions stored in order of ascending base address.
62 * The list is terminated by the first entry with size == 0.
63 */
Dan Handleye2712bc2014-04-10 15:37:22 +010064static mmap_region_t mmap[MAX_MMAP_REGIONS + 1];
Jon Medhurstbb1fe202014-01-24 15:41:33 +000065
66
67static void print_mmap(void)
68{
69#if DEBUG_XLAT_TABLE
70 debug_print("mmap:\n");
Dan Handleye2712bc2014-04-10 15:37:22 +010071 mmap_region_t *mm = mmap;
Jon Medhurstbb1fe202014-01-24 15:41:33 +000072 while (mm->size) {
73 debug_print(" %010lx %10lx %x\n", mm->base, mm->size, mm->attr);
74 ++mm;
75 };
76 debug_print("\n");
77#endif
78}
79
80void mmap_add_region(unsigned long base, unsigned long size, unsigned attr)
81{
Dan Handleye2712bc2014-04-10 15:37:22 +010082 mmap_region_t *mm = mmap;
83 mmap_region_t *mm_last = mm + sizeof(mmap) / sizeof(mmap[0]) - 1;
Jon Medhurstbb1fe202014-01-24 15:41:33 +000084
85 assert(IS_PAGE_ALIGNED(base));
86 assert(IS_PAGE_ALIGNED(size));
87
88 if (!size)
89 return;
90
91 /* Find correct place in mmap to insert new region */
92 while (mm->base < base && mm->size)
93 ++mm;
94
95 /* Make room for new region by moving other regions up by one place */
96 memmove(mm + 1, mm, (uintptr_t)mm_last - (uintptr_t)mm);
97
98 /* Check we haven't lost the empty sentinal from the end of the array */
99 assert(mm_last->size == 0);
100
101 mm->base = base;
102 mm->size = size;
103 mm->attr = attr;
104}
105
Dan Handleye2712bc2014-04-10 15:37:22 +0100106void mmap_add(const mmap_region_t *mm)
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000107{
108 while (mm->size) {
109 mmap_add_region(mm->base, mm->size, mm->attr);
110 ++mm;
111 }
112}
113
114static unsigned long mmap_desc(unsigned attr, unsigned long addr,
115 unsigned level)
116{
117 unsigned long desc = addr;
118
119 desc |= level == 3 ? TABLE_DESC : BLOCK_DESC;
120
121 desc |= attr & MT_NS ? LOWER_ATTRS(NS) : 0;
122
123 desc |= attr & MT_RW ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
124
125 desc |= LOWER_ATTRS(ACCESS_FLAG);
126
127 if (attr & MT_MEMORY) {
128 desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);
129 if (attr & MT_RW)
130 desc |= UPPER_ATTRS(XN);
131 } else {
132 desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
133 desc |= UPPER_ATTRS(XN);
134 }
135
136 debug_print(attr & MT_MEMORY ? "MEM" : "DEV");
137 debug_print(attr & MT_RW ? "-RW" : "-RO");
138 debug_print(attr & MT_NS ? "-NS" : "-S");
139
140 return desc;
141}
142
Dan Handleye2712bc2014-04-10 15:37:22 +0100143static int mmap_region_attr(mmap_region_t *mm, unsigned long base,
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000144 unsigned long size)
145{
146 int attr = mm->attr;
147
148 for (;;) {
149 ++mm;
150
151 if (!mm->size)
152 return attr; /* Reached end of list */
153
154 if (mm->base >= base + size)
155 return attr; /* Next region is after area so end */
156
157 if (mm->base + mm->size <= base)
158 continue; /* Next region has already been overtaken */
159
160 if ((mm->attr & attr) == attr)
161 continue; /* Region doesn't override attribs so skip */
162
163 attr &= mm->attr;
164
165 if (mm->base > base || mm->base + mm->size < base + size)
166 return -1; /* Region doesn't fully cover our area */
167 }
168}
169
Dan Handleye2712bc2014-04-10 15:37:22 +0100170static mmap_region_t *init_xlation_table(mmap_region_t *mm, unsigned long base,
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000171 unsigned long *table, unsigned level)
172{
173 unsigned level_size_shift = L1_XLAT_ADDRESS_SHIFT - (level - 1) *
174 XLAT_TABLE_ENTRIES_SHIFT;
175 unsigned level_size = 1 << level_size_shift;
176 unsigned level_index_mask = XLAT_TABLE_ENTRIES_MASK << level_size_shift;
177
178 assert(level <= 3);
179
180 debug_print("New xlat table:\n");
181
182 do {
183 unsigned long desc = UNSET_DESC;
184
185 if (mm->base + mm->size <= base) {
186 /* Area now after the region so skip it */
187 ++mm;
188 continue;
189 }
190
191 debug_print(" %010lx %8lx " + 6 - 2 * level, base, level_size);
192
193 if (mm->base >= base + level_size) {
194 /* Next region is after area so nothing to map yet */
195 desc = INVALID_DESC;
196 } else if (mm->base <= base &&
197 mm->base + mm->size >= base + level_size) {
198 /* Next region covers all of area */
199 int attr = mmap_region_attr(mm, base, level_size);
200 if (attr >= 0)
201 desc = mmap_desc(attr, base, level);
202 }
203 /* else Next region only partially covers area, so need */
204
205 if (desc == UNSET_DESC) {
206 /* Area not covered by a region so need finer table */
207 unsigned long *new_table = xlat_tables[next_xlat++];
208 assert(next_xlat <= MAX_XLAT_TABLES);
209 desc = TABLE_DESC | (unsigned long)new_table;
210
211 /* Recurse to fill in new table */
212 mm = init_xlation_table(mm, base, new_table, level+1);
213 }
214
215 debug_print("\n");
216
217 *table++ = desc;
218 base += level_size;
219 } while (mm->size && (base & level_index_mask));
220
221 return mm;
222}
223
224void init_xlat_tables(void)
225{
226 print_mmap();
227 init_xlation_table(mmap, 0, l1_xlation_table, 1);
228}