blob: c4f422ccb87dfba5f4fd7e8d540f98d56108563b [file] [log] [blame]
Achin Gupta7c88f3f2014-02-18 18:09:12 +00001/*
2 * Copyright (c) 2013-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 <string.h>
32#include <assert.h>
33#include <arch_helpers.h>
34#include <platform.h>
35#include <bl_common.h>
36
37#define BL32_NUM_L3_PAGETABLES 3
38#define BL32_TZRAM_PAGETABLE 0
39#define BL32_TZDRAM_PAGETABLE 1
40#define BL32_NSRAM_PAGETABLE 2
41
42/*******************************************************************************
43 * Level 1 translation tables need 4 entries for the 4GB address space accessib-
44 * le by the secure firmware. Input address space will be restricted using the
45 * T0SZ settings in the TCR.
46 ******************************************************************************/
47static unsigned long l1_xlation_table[ADDR_SPACE_SIZE >> 30]
48__attribute__ ((aligned((ADDR_SPACE_SIZE >> 30) << 3)));
49
50/*******************************************************************************
51 * Level 2 translation tables describe the first & second gb of the address
52 * space needed to address secure peripherals e.g. trusted ROM and RAM.
53 ******************************************************************************/
54static unsigned long l2_xlation_table[NUM_L2_PAGETABLES][NUM_2MB_IN_GB]
55__attribute__ ((aligned(NUM_2MB_IN_GB << 3),
56 section("xlat_table")));
57
58/*******************************************************************************
59 * Level 3 translation tables (2 sets) describe the trusted & non-trusted RAM
60 * regions at a granularity of 4K.
61 ******************************************************************************/
62static unsigned long l3_xlation_table[BL32_NUM_L3_PAGETABLES][NUM_4K_IN_2MB]
63__attribute__ ((aligned(NUM_4K_IN_2MB << 3),
64 section("xlat_table")));
65
66/*******************************************************************************
67 * Create page tables as per the platform memory map. Certain aspects of page
68 * talble creating have been abstracted in the above routines. This can be impr-
69 * oved further.
70 * TODO: Move the page table setup helpers into the arch or lib directory
71 *******************************************************************************/
72unsigned long fill_xlation_tables(meminfo *tzdram_layout,
73 unsigned long ro_start,
74 unsigned long ro_limit,
75 unsigned long coh_start,
76 unsigned long coh_limit)
77{
78 unsigned long l2_desc, l3_desc;
79 unsigned long *xt_addr = 0, *pt_addr, off = 0;
80 unsigned long trom_start_index, trom_end_index;
81 unsigned long tzram_start_index, tzram_end_index;
82 unsigned long flash0_start_index, flash0_end_index;
83 unsigned long flash1_start_index, flash1_end_index;
84 unsigned long vram_start_index, vram_end_index;
85 unsigned long nsram_start_index, nsram_end_index;
86 unsigned long tzdram_start_index, tzdram_end_index;
87 unsigned long dram_start_index, dram_end_index;
88 unsigned long dev0_start_index, dev0_end_index;
89 unsigned long dev1_start_index, dev1_end_index;
90 unsigned int idx;
91
92 /*****************************************************************
93 * LEVEL1 PAGETABLE SETUP
94 *
95 * Find the start and end indices of the memory peripherals in the
96 * first level pagetables. These are the main areas we care about.
97 * Also bump the end index by one if its equal to the start to
98 * allow for regions which lie completely in a GB.
99 *****************************************************************/
100 trom_start_index = ONE_GB_INDEX(TZROM_BASE);
101 dev0_start_index = ONE_GB_INDEX(TZRNG_BASE);
102 dram_start_index = ONE_GB_INDEX(DRAM_BASE);
103 dram_end_index = ONE_GB_INDEX(DRAM_BASE + DRAM_SIZE);
104
105 if (dram_end_index == dram_start_index)
106 dram_end_index++;
107
108 /*
109 * Fill up the level1 translation table first
110 */
111 for (idx = 0; idx < (ADDR_SPACE_SIZE >> 30); idx++) {
112
113 /*
114 * Fill up the entry for the TZROM. This will cover
115 * everything in the first GB.
116 */
117 if (idx == trom_start_index) {
118 xt_addr = &l2_xlation_table[GB1_L2_PAGETABLE][0];
119 l1_xlation_table[idx] = create_table_desc(xt_addr);
120 continue;
121 }
122
123 /*
124 * Mark the second gb as device
125 */
126 if (idx == dev0_start_index) {
127 xt_addr = &l2_xlation_table[GB2_L2_PAGETABLE][0];
128 l1_xlation_table[idx] = create_table_desc(xt_addr);
129 continue;
130 }
131
132 /*
133 * Fill up the block entry for the DRAM with Normal
134 * inner-WBWA outer-WBWA non-transient attributes.
135 * This will cover 2-4GB. Note that the acesses are
136 * marked as non-secure.
137 */
138 if ((idx >= dram_start_index) && (idx < dram_end_index)) {
139 l1_xlation_table[idx] = create_rwmem_block(idx, LEVEL1,
140 NS);
141 continue;
142 }
143
144 assert(0);
145 }
146
147
148 /*****************************************************************
149 * LEVEL2 PAGETABLE SETUP
150 *
151 * Find the start and end indices of the memory & peripherals in the
152 * second level pagetables.
153 ******************************************************************/
154
155 /* Initializations for the 1st GB */
156 trom_start_index = TWO_MB_INDEX(TZROM_BASE);
157 trom_end_index = TWO_MB_INDEX(TZROM_BASE + TZROM_SIZE);
158 if (trom_end_index == trom_start_index)
159 trom_end_index++;
160
161 tzdram_start_index = TWO_MB_INDEX(TZDRAM_BASE);
162 tzdram_end_index = TWO_MB_INDEX(TZDRAM_BASE + TZDRAM_SIZE);
163 if (tzdram_end_index == tzdram_start_index)
164 tzdram_end_index++;
165
166 flash0_start_index = TWO_MB_INDEX(FLASH0_BASE);
167 flash0_end_index = TWO_MB_INDEX(FLASH0_BASE + TZROM_SIZE);
168 if (flash0_end_index == flash0_start_index)
169 flash0_end_index++;
170
171 flash1_start_index = TWO_MB_INDEX(FLASH1_BASE);
172 flash1_end_index = TWO_MB_INDEX(FLASH1_BASE + FLASH1_SIZE);
173 if (flash1_end_index == flash1_start_index)
174 flash1_end_index++;
175
176 vram_start_index = TWO_MB_INDEX(VRAM_BASE);
177 vram_end_index = TWO_MB_INDEX(VRAM_BASE + VRAM_SIZE);
178 if (vram_end_index == vram_start_index)
179 vram_end_index++;
180
181 dev0_start_index = TWO_MB_INDEX(DEVICE0_BASE);
182 dev0_end_index = TWO_MB_INDEX(DEVICE0_BASE + DEVICE0_SIZE);
183 if (dev0_end_index == dev0_start_index)
184 dev0_end_index++;
185
186 dev1_start_index = TWO_MB_INDEX(DEVICE1_BASE);
187 dev1_end_index = TWO_MB_INDEX(DEVICE1_BASE + DEVICE1_SIZE);
188 if (dev1_end_index == dev1_start_index)
189 dev1_end_index++;
190
191 /* Since the size is < 2M this is a single index */
192 tzram_start_index = TWO_MB_INDEX(TZRAM_BASE);
193 nsram_start_index = TWO_MB_INDEX(NSRAM_BASE);
194
195 /*
196 * Fill up the level2 translation table for the first GB next
197 */
198 for (idx = 0; idx < NUM_2MB_IN_GB; idx++) {
199
200 l2_desc = INVALID_DESC;
201 xt_addr = &l2_xlation_table[GB1_L2_PAGETABLE][idx];
202
203 /* Block entries for 64M of trusted Boot ROM */
204 if ((idx >= trom_start_index) && (idx < trom_end_index))
205 l2_desc = create_romem_block(idx, LEVEL2, 0);
206
207 /* Single L3 page table entry for 256K of TZRAM */
208 if (idx == tzram_start_index) {
209 pt_addr = &l3_xlation_table[BL32_TZRAM_PAGETABLE][0];
210 l2_desc = create_table_desc(pt_addr);
211 }
212
213 /*
214 * Single L3 page table entry for first 2MB of TZDRAM.
215 * TODO: We are assuming the BL32 image will not
216 * exceed this
217 */
218 if (idx == tzdram_start_index) {
219 pt_addr = &l3_xlation_table[BL32_TZDRAM_PAGETABLE][0];
220 l2_desc = create_table_desc(pt_addr);
221 }
222
223 /* Block entries for 32M of trusted DRAM */
224 if ((idx >= tzdram_start_index + 1) && (idx <= tzdram_end_index))
225 l2_desc = create_rwmem_block(idx, LEVEL2, 0);
226
227 /* Block entries for 64M of aliased trusted Boot ROM */
228 if ((idx >= flash0_start_index) && (idx < flash0_end_index))
229 l2_desc = create_romem_block(idx, LEVEL2, 0);
230
231 /* Block entries for 64M of flash1 */
232 if ((idx >= flash1_start_index) && (idx < flash1_end_index))
233 l2_desc = create_romem_block(idx, LEVEL2, 0);
234
235 /* Block entries for 32M of VRAM */
236 if ((idx >= vram_start_index) && (idx < vram_end_index))
237 l2_desc = create_rwmem_block(idx, LEVEL2, 0);
238
239 /* Block entries for all the devices in the first gb */
240 if ((idx >= dev0_start_index) && (idx < dev0_end_index))
241 l2_desc = create_device_block(idx, LEVEL2, 0);
242
243 /* Block entries for all the devices in the first gb */
244 if ((idx >= dev1_start_index) && (idx < dev1_end_index))
245 l2_desc = create_device_block(idx, LEVEL2, 0);
246
247 /* Single L3 page table entry for 64K of NSRAM */
248 if (idx == nsram_start_index) {
249 pt_addr = &l3_xlation_table[BL32_NSRAM_PAGETABLE][0];
250 l2_desc = create_table_desc(pt_addr);
251 }
252
253 *xt_addr = l2_desc;
254 }
255
256
257 /*
258 * Initializations for the 2nd GB. Mark everything as device
259 * for the time being as the memory map is not final. Each
260 * index will need to be offset'ed to allow absolute values
261 */
262 off = NUM_2MB_IN_GB;
263 for (idx = off; idx < (NUM_2MB_IN_GB + off); idx++) {
264 l2_desc = create_device_block(idx, LEVEL2, 0);
265 xt_addr = &l2_xlation_table[GB2_L2_PAGETABLE][idx - off];
266 *xt_addr = l2_desc;
267 }
268
269
270 /*****************************************************************
271 * LEVEL3 PAGETABLE SETUP
272 *****************************************************************/
273
274 /* Fill up the level3 pagetable for the trusted SRAM. */
275 tzram_start_index = FOUR_KB_INDEX(TZRAM_BASE);
276 tzram_end_index = FOUR_KB_INDEX(TZRAM_BASE + TZRAM_SIZE);
277 if (tzram_end_index == tzram_start_index)
278 tzram_end_index++;
279
280 /* Each index will need to be offset'ed to allow absolute values */
281 off = FOUR_KB_INDEX(TZRAM_BASE);
282 for (idx = off; idx < (NUM_4K_IN_2MB + off); idx++) {
283
284 l3_desc = INVALID_DESC;
285 xt_addr = &l3_xlation_table[BL32_TZRAM_PAGETABLE][idx - off];
286
287 /*
288 * TODO: All TZRAM memory is being mapped as RW while
289 * earlier boot stages map parts of it as Device. This
290 * could lead to possible coherency issues. Ditto for
291 * the way earlier boot loader stages map TZDRAM
292 */
293 if (idx >= tzram_start_index && idx < tzram_end_index)
294 l3_desc = create_rwmem_block(idx, LEVEL3, 0);
295
296 *xt_addr = l3_desc;
297 }
298
299 /* Fill up the level3 pagetable for the trusted DRAM. */
300 tzdram_start_index = FOUR_KB_INDEX(tzdram_layout->total_base);
301 tzdram_end_index = FOUR_KB_INDEX(tzdram_layout->total_base +
302 tzdram_layout->total_size);
303 if (tzdram_end_index == tzdram_start_index)
304 tzdram_end_index++;
305
306 /* Reusing trom* to mark RO memory. */
307 trom_start_index = FOUR_KB_INDEX(ro_start);
308 trom_end_index = FOUR_KB_INDEX(ro_limit);
309 if (trom_end_index == trom_start_index)
310 trom_end_index++;
311
312 /* Reusing dev* to mark coherent device memory. */
313 dev0_start_index = FOUR_KB_INDEX(coh_start);
314 dev0_end_index = FOUR_KB_INDEX(coh_limit);
315 if (dev0_end_index == dev0_start_index)
316 dev0_end_index++;
317
318 /* Each index will need to be offset'ed to allow absolute values */
319 off = FOUR_KB_INDEX(TZDRAM_BASE);
320 for (idx = off; idx < (NUM_4K_IN_2MB + off); idx++) {
321
322 l3_desc = INVALID_DESC;
323 xt_addr = &l3_xlation_table[BL32_TZDRAM_PAGETABLE][idx - off];
324
325 if (idx >= tzdram_start_index && idx < tzdram_end_index)
326 l3_desc = create_rwmem_block(idx, LEVEL3, 0);
327
328 if (idx >= trom_start_index && idx < trom_end_index)
329 l3_desc = create_romem_block(idx, LEVEL3, 0);
330
331 if (idx >= dev0_start_index && idx < dev0_end_index)
332 l3_desc = create_device_block(idx, LEVEL3, 0);
333
334 *xt_addr = l3_desc;
335 }
336
337 /* Fill up the level3 pagetable for the non-trusted SRAM. */
338 nsram_start_index = FOUR_KB_INDEX(NSRAM_BASE);
339 nsram_end_index = FOUR_KB_INDEX(NSRAM_BASE + NSRAM_SIZE);
340 if (nsram_end_index == nsram_start_index)
341 nsram_end_index++;
342
343 /* Each index will need to be offset'ed to allow absolute values */
344 off = FOUR_KB_INDEX(NSRAM_BASE);
345 for (idx = off; idx < (NUM_4K_IN_2MB + off); idx++) {
346
347 l3_desc = INVALID_DESC;
348 xt_addr = &l3_xlation_table[BL32_NSRAM_PAGETABLE][idx - off];
349
350 if (idx >= nsram_start_index && idx < nsram_end_index)
351 l3_desc = create_rwmem_block(idx, LEVEL3, NS);
352
353 *xt_addr = l3_desc;
354 }
355
356 return (unsigned long) l1_xlation_table;
357}