1 /* 2 * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <arch.h> 8 #include <arch_helpers.h> 9 #include <assert.h> 10 #include <bl_common.h> 11 #include <cassert.h> 12 #include <common_def.h> 13 #include <platform_def.h> 14 #include <sys/types.h> 15 #include <utils.h> 16 #include <utils_def.h> 17 #include <xlat_tables_v2.h> 18 #include "../xlat_tables_private.h" 19 20 #if defined(IMAGE_BL1) || defined(IMAGE_BL31) 21 # define IMAGE_EL 3 22 #else 23 # define IMAGE_EL 1 24 #endif 25 26 static unsigned long long calc_physical_addr_size_bits( 27 unsigned long long max_addr) 28 { 29 /* Physical address can't exceed 48 bits */ 30 assert((max_addr & ADDR_MASK_48_TO_63) == 0); 31 32 /* 48 bits address */ 33 if (max_addr & ADDR_MASK_44_TO_47) 34 return TCR_PS_BITS_256TB; 35 36 /* 44 bits address */ 37 if (max_addr & ADDR_MASK_42_TO_43) 38 return TCR_PS_BITS_16TB; 39 40 /* 42 bits address */ 41 if (max_addr & ADDR_MASK_40_TO_41) 42 return TCR_PS_BITS_4TB; 43 44 /* 40 bits address */ 45 if (max_addr & ADDR_MASK_36_TO_39) 46 return TCR_PS_BITS_1TB; 47 48 /* 36 bits address */ 49 if (max_addr & ADDR_MASK_32_TO_35) 50 return TCR_PS_BITS_64GB; 51 52 return TCR_PS_BITS_4GB; 53 } 54 55 #if ENABLE_ASSERTIONS 56 /* Physical Address ranges supported in the AArch64 Memory Model */ 57 static const unsigned int pa_range_bits_arr[] = { 58 PARANGE_0000, PARANGE_0001, PARANGE_0010, PARANGE_0011, PARANGE_0100, 59 PARANGE_0101 60 }; 61 62 unsigned long long xlat_arch_get_max_supported_pa(void) 63 { 64 u_register_t pa_range = read_id_aa64mmfr0_el1() & 65 ID_AA64MMFR0_EL1_PARANGE_MASK; 66 67 /* All other values are reserved */ 68 assert(pa_range < ARRAY_SIZE(pa_range_bits_arr)); 69 70 return (1ull << pa_range_bits_arr[pa_range]) - 1ull; 71 } 72 #endif /* ENABLE_ASSERTIONS*/ 73 74 int is_mmu_enabled(void) 75 { 76 #if IMAGE_EL == 1 77 assert(IS_IN_EL(1)); 78 return (read_sctlr_el1() & SCTLR_M_BIT) != 0; 79 #elif IMAGE_EL == 3 80 assert(IS_IN_EL(3)); 81 return (read_sctlr_el3() & SCTLR_M_BIT) != 0; 82 #endif 83 } 84 85 #if PLAT_XLAT_TABLES_DYNAMIC 86 87 void xlat_arch_tlbi_va(uintptr_t va) 88 { 89 /* 90 * Ensure the translation table write has drained into memory before 91 * invalidating the TLB entry. 92 */ 93 dsbishst(); 94 95 #if IMAGE_EL == 1 96 assert(IS_IN_EL(1)); 97 tlbivaae1is(TLBI_ADDR(va)); 98 #elif IMAGE_EL == 3 99 assert(IS_IN_EL(3)); 100 tlbivae3is(TLBI_ADDR(va)); 101 #endif 102 } 103 104 void xlat_arch_tlbi_va_sync(void) 105 { 106 /* 107 * A TLB maintenance instruction can complete at any time after 108 * it is issued, but is only guaranteed to be complete after the 109 * execution of DSB by the PE that executed the TLB maintenance 110 * instruction. After the TLB invalidate instruction is 111 * complete, no new memory accesses using the invalidated TLB 112 * entries will be observed by any observer of the system 113 * domain. See section D4.8.2 of the ARMv8 (issue k), paragraph 114 * "Ordering and completion of TLB maintenance instructions". 115 */ 116 dsbish(); 117 118 /* 119 * The effects of a completed TLB maintenance instruction are 120 * only guaranteed to be visible on the PE that executed the 121 * instruction after the execution of an ISB instruction by the 122 * PE that executed the TLB maintenance instruction. 123 */ 124 isb(); 125 } 126 127 #endif /* PLAT_XLAT_TABLES_DYNAMIC */ 128 129 int xlat_arch_current_el(void) 130 { 131 int el = GET_EL(read_CurrentEl()); 132 133 assert(el > 0); 134 135 return el; 136 } 137 138 uint64_t xlat_arch_get_xn_desc(int el) 139 { 140 if (el == 3) { 141 return UPPER_ATTRS(XN); 142 } else { 143 assert(el == 1); 144 return UPPER_ATTRS(PXN); 145 } 146 } 147 148 /******************************************************************************* 149 * Macro generating the code for the function enabling the MMU in the given 150 * exception level, assuming that the pagetables have already been created. 151 * 152 * _el: Exception level at which the function will run 153 * _tlbi_fct: Function to invalidate the TLBs at the current 154 * exception level 155 ******************************************************************************/ 156 #define DEFINE_ENABLE_MMU_EL(_el, _tlbi_fct) \ 157 static void enable_mmu_internal_el##_el(int flags, \ 158 uint64_t mair, \ 159 uint64_t tcr, \ 160 uint64_t ttbr) \ 161 { \ 162 uint32_t sctlr = read_sctlr_el##_el(); \ 163 assert((sctlr & SCTLR_M_BIT) == 0); \ 164 \ 165 /* Invalidate TLBs at the current exception level */ \ 166 _tlbi_fct(); \ 167 \ 168 write_mair_el##_el(mair); \ 169 write_tcr_el##_el(tcr); \ 170 \ 171 /* Set TTBR bits as well */ \ 172 if (ARM_ARCH_AT_LEAST(8, 2)) { \ 173 /* Enable CnP bit so as to share page tables */ \ 174 /* with all PEs. This is mandatory for */ \ 175 /* ARMv8.2 implementations. */ \ 176 ttbr |= TTBR_CNP_BIT; \ 177 } \ 178 write_ttbr0_el##_el(ttbr); \ 179 \ 180 /* Ensure all translation table writes have drained */ \ 181 /* into memory, the TLB invalidation is complete, */ \ 182 /* and translation register writes are committed */ \ 183 /* before enabling the MMU */ \ 184 dsbish(); \ 185 isb(); \ 186 \ 187 sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT; \ 188 if (flags & DISABLE_DCACHE) \ 189 sctlr &= ~SCTLR_C_BIT; \ 190 else \ 191 sctlr |= SCTLR_C_BIT; \ 192 \ 193 write_sctlr_el##_el(sctlr); \ 194 \ 195 /* Ensure the MMU enable takes effect immediately */ \ 196 isb(); \ 197 } 198 199 /* Define EL1 and EL3 variants of the function enabling the MMU */ 200 #if IMAGE_EL == 1 201 DEFINE_ENABLE_MMU_EL(1, tlbivmalle1) 202 #elif IMAGE_EL == 3 203 DEFINE_ENABLE_MMU_EL(3, tlbialle3) 204 #endif 205 206 void enable_mmu_arch(unsigned int flags, 207 uint64_t *base_table, 208 unsigned long long max_pa, 209 uintptr_t max_va) 210 { 211 uint64_t mair, ttbr, tcr; 212 213 /* Set attributes in the right indices of the MAIR. */ 214 mair = MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX); 215 mair |= MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, ATTR_IWBWA_OWBWA_NTR_INDEX); 216 mair |= MAIR_ATTR_SET(ATTR_NON_CACHEABLE, ATTR_NON_CACHEABLE_INDEX); 217 218 ttbr = (uint64_t) base_table; 219 220 /* 221 * Set TCR bits as well. 222 */ 223 224 /* 225 * Limit the input address ranges and memory region sizes translated 226 * using TTBR0 to the given virtual address space size. 227 */ 228 assert(max_va < UINTPTR_MAX); 229 uintptr_t virtual_addr_space_size = max_va + 1; 230 assert(CHECK_VIRT_ADDR_SPACE_SIZE(virtual_addr_space_size)); 231 /* 232 * __builtin_ctzll(0) is undefined but here we are guaranteed that 233 * virtual_addr_space_size is in the range [1,UINTPTR_MAX]. 234 */ 235 tcr = 64 - __builtin_ctzll(virtual_addr_space_size); 236 237 /* 238 * Set the cacheability and shareability attributes for memory 239 * associated with translation table walks. 240 */ 241 if (flags & XLAT_TABLE_NC) { 242 /* Inner & outer non-cacheable non-shareable. */ 243 tcr |= TCR_SH_NON_SHAREABLE | 244 TCR_RGN_OUTER_NC | TCR_RGN_INNER_NC; 245 } else { 246 /* Inner & outer WBWA & shareable. */ 247 tcr |= TCR_SH_INNER_SHAREABLE | 248 TCR_RGN_OUTER_WBA | TCR_RGN_INNER_WBA; 249 } 250 251 /* 252 * It is safer to restrict the max physical address accessible by the 253 * hardware as much as possible. 254 */ 255 unsigned long long tcr_ps_bits = calc_physical_addr_size_bits(max_pa); 256 257 #if IMAGE_EL == 1 258 assert(IS_IN_EL(1)); 259 tcr |= tcr_ps_bits << TCR_EL1_IPS_SHIFT; 260 enable_mmu_internal_el1(flags, mair, tcr, ttbr); 261 #elif IMAGE_EL == 3 262 assert(IS_IN_EL(3)); 263 tcr |= TCR_EL3_RES1 | (tcr_ps_bits << TCR_EL3_PS_SHIFT); 264 enable_mmu_internal_el3(flags, mair, tcr, ttbr); 265 #endif 266 } 267