blob: 03caf36fba7cf743e0af0d8a42d9eaae511301f8 [file] [log] [blame]
Soby Mathew935c2e72016-06-30 15:11:07 +01001/*
Antonio Nino Diaz3759e3f2017-03-22 15:48:51 +00002 * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
Soby Mathew935c2e72016-06-30 15:11:07 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathew935c2e72016-06-30 15:11:07 +01005 */
6
7#include <arch.h>
8#include <arch_helpers.h>
9#include <assert.h>
10#include <cassert.h>
11#include <platform_def.h>
12#include <utils.h>
13#include <xlat_tables.h>
14#include "../xlat_tables_private.h"
15
16/*
Antonio Nino Diazd48ae612016-08-02 09:21:41 +010017 * Each platform can define the size of the virtual address space, which is
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000018 * defined in PLAT_VIRT_ADDR_SPACE_SIZE. TTBCR.TxSZ is calculated as 32 minus
19 * the width of said address space. The value of TTBCR.TxSZ must be in the
20 * range 0 to 7 [1], which means that the virtual address space width must be
21 * in the range 32 to 25 bits.
Antonio Nino Diazd48ae612016-08-02 09:21:41 +010022 *
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000023 * Here we calculate the initial lookup level from the value of
24 * PLAT_VIRT_ADDR_SPACE_SIZE. For a 4 KB page size, level 1 supports virtual
25 * address spaces of widths 32 to 31 bits, and level 2 from 30 to 25. Wider or
26 * narrower address spaces are not supported. As a result, level 3 cannot be
27 * used as initial lookup level with 4 KB granularity [1].
Antonio Nino Diazd48ae612016-08-02 09:21:41 +010028 *
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000029 * For example, for a 31-bit address space (i.e. PLAT_VIRT_ADDR_SPACE_SIZE ==
30 * 1 << 31), TTBCR.TxSZ will be programmed to (32 - 31) = 1. According to Table
31 * G4-5 in the ARM ARM, the initial lookup level for an address space like that
32 * is 1.
Antonio Nino Diazd48ae612016-08-02 09:21:41 +010033 *
34 * See the ARMv8-A Architecture Reference Manual (DDI 0487A.j) for more
35 * information:
36 * [1] Section G4.6.5
Soby Mathew935c2e72016-06-30 15:11:07 +010037 */
Soby Mathew935c2e72016-06-30 15:11:07 +010038
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000039#if PLAT_VIRT_ADDR_SPACE_SIZE > (1ULL << (32 - TTBCR_TxSZ_MIN))
Soby Mathew935c2e72016-06-30 15:11:07 +010040
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000041# error "PLAT_VIRT_ADDR_SPACE_SIZE is too big."
Antonio Nino Diazd48ae612016-08-02 09:21:41 +010042
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000043#elif PLAT_VIRT_ADDR_SPACE_SIZE > (1 << L1_XLAT_ADDRESS_SHIFT)
Antonio Nino Diazd48ae612016-08-02 09:21:41 +010044
45# define XLAT_TABLE_LEVEL_BASE 1
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000046# define NUM_BASE_LEVEL_ENTRIES \
47 (PLAT_VIRT_ADDR_SPACE_SIZE >> L1_XLAT_ADDRESS_SHIFT)
Antonio Nino Diazd48ae612016-08-02 09:21:41 +010048
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000049#elif PLAT_VIRT_ADDR_SPACE_SIZE >= (1 << (32 - TTBCR_TxSZ_MAX))
Antonio Nino Diazd48ae612016-08-02 09:21:41 +010050
51# define XLAT_TABLE_LEVEL_BASE 2
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000052# define NUM_BASE_LEVEL_ENTRIES \
53 (PLAT_VIRT_ADDR_SPACE_SIZE >> L2_XLAT_ADDRESS_SHIFT)
Antonio Nino Diazd48ae612016-08-02 09:21:41 +010054
55#else
56
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000057# error "PLAT_VIRT_ADDR_SPACE_SIZE is too small."
Antonio Nino Diazd48ae612016-08-02 09:21:41 +010058
59#endif
60
61static uint64_t base_xlation_table[NUM_BASE_LEVEL_ENTRIES]
62 __aligned(NUM_BASE_LEVEL_ENTRIES * sizeof(uint64_t));
Soby Mathew935c2e72016-06-30 15:11:07 +010063
Antonio Nino Diaz3759e3f2017-03-22 15:48:51 +000064#if ENABLE_ASSERTIONS
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000065static unsigned long long get_max_supported_pa(void)
66{
67 /* Physical address space size for long descriptor format. */
68 return (1ULL << 40) - 1ULL;
69}
Antonio Nino Diaz3759e3f2017-03-22 15:48:51 +000070#endif /* ENABLE_ASSERTIONS */
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000071
Soby Mathew935c2e72016-06-30 15:11:07 +010072void init_xlat_tables(void)
73{
74 unsigned long long max_pa;
75 uintptr_t max_va;
76 print_mmap();
Antonio Nino Diazd48ae612016-08-02 09:21:41 +010077 init_xlation_table(0, base_xlation_table, XLAT_TABLE_LEVEL_BASE,
78 &max_va, &max_pa);
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000079
80 assert(max_va <= PLAT_VIRT_ADDR_SPACE_SIZE - 1);
81 assert(max_pa <= PLAT_PHY_ADDR_SPACE_SIZE - 1);
82 assert((PLAT_PHY_ADDR_SPACE_SIZE - 1) <= get_max_supported_pa());
Soby Mathew935c2e72016-06-30 15:11:07 +010083}
84
85/*******************************************************************************
86 * Function for enabling the MMU in Secure PL1, assuming that the
87 * page-tables have already been created.
88 ******************************************************************************/
89void enable_mmu_secure(unsigned int flags)
90{
91 unsigned int mair0, ttbcr, sctlr;
92 uint64_t ttbr0;
93
94 assert(IS_IN_SECURE());
95 assert((read_sctlr() & SCTLR_M_BIT) == 0);
96
97 /* Set attributes in the right indices of the MAIR */
98 mair0 = MAIR0_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX);
99 mair0 |= MAIR0_ATTR_SET(ATTR_IWBWA_OWBWA_NTR,
100 ATTR_IWBWA_OWBWA_NTR_INDEX);
101 mair0 |= MAIR0_ATTR_SET(ATTR_NON_CACHEABLE,
102 ATTR_NON_CACHEABLE_INDEX);
103 write_mair0(mair0);
104
105 /* Invalidate TLBs at the current exception level */
106 tlbiall();
107
108 /*
Summer Qindaf5dbb2017-03-16 17:16:34 +0000109 * Set TTBCR bits as well. Set TTBR0 table properties. Disable TTBR1.
Soby Mathew935c2e72016-06-30 15:11:07 +0100110 */
Summer Qindaf5dbb2017-03-16 17:16:34 +0000111 if (flags & XLAT_TABLE_NC) {
112 /* Inner & outer non-cacheable non-shareable. */
113 ttbcr = TTBCR_EAE_BIT |
114 TTBCR_SH0_NON_SHAREABLE | TTBCR_RGN0_OUTER_NC |
115 TTBCR_RGN0_INNER_NC |
116 (32 - __builtin_ctzl((uintptr_t)PLAT_VIRT_ADDR_SPACE_SIZE));
117 } else {
118 /* Inner & outer WBWA & shareable. */
119 ttbcr = TTBCR_EAE_BIT |
120 TTBCR_SH0_INNER_SHAREABLE | TTBCR_RGN0_OUTER_WBA |
121 TTBCR_RGN0_INNER_WBA |
122 (32 - __builtin_ctzl((uintptr_t)PLAT_VIRT_ADDR_SPACE_SIZE));
123 }
Soby Mathew935c2e72016-06-30 15:11:07 +0100124 ttbcr |= TTBCR_EPD1_BIT;
125 write_ttbcr(ttbcr);
126
127 /* Set TTBR0 bits as well */
Antonio Nino Diazd48ae612016-08-02 09:21:41 +0100128 ttbr0 = (uintptr_t) base_xlation_table;
Soby Mathew935c2e72016-06-30 15:11:07 +0100129 write64_ttbr0(ttbr0);
130 write64_ttbr1(0);
131
132 /*
133 * Ensure all translation table writes have drained
134 * into memory, the TLB invalidation is complete,
135 * and translation register writes are committed
136 * before enabling the MMU
137 */
138 dsb();
139 isb();
140
141 sctlr = read_sctlr();
142 sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT;
143
144 if (flags & DISABLE_DCACHE)
145 sctlr &= ~SCTLR_C_BIT;
146 else
147 sctlr |= SCTLR_C_BIT;
148
149 write_sctlr(sctlr);
150
151 /* Ensure the MMU enable takes effect immediately */
152 isb();
153}