1*3ca9928dSSoby Mathew /* 2*3ca9928dSSoby Mathew * Copyright (c) 2014-2016, ARM Limited and Contributors. All rights reserved. 3*3ca9928dSSoby Mathew * 4*3ca9928dSSoby Mathew * Redistribution and use in source and binary forms, with or without 5*3ca9928dSSoby Mathew * modification, are permitted provided that the following conditions are met: 6*3ca9928dSSoby Mathew * 7*3ca9928dSSoby Mathew * Redistributions of source code must retain the above copyright notice, this 8*3ca9928dSSoby Mathew * list of conditions and the following disclaimer. 9*3ca9928dSSoby Mathew * 10*3ca9928dSSoby Mathew * Redistributions in binary form must reproduce the above copyright notice, 11*3ca9928dSSoby Mathew * this list of conditions and the following disclaimer in the documentation 12*3ca9928dSSoby Mathew * and/or other materials provided with the distribution. 13*3ca9928dSSoby Mathew * 14*3ca9928dSSoby Mathew * Neither the name of ARM nor the names of its contributors may be used 15*3ca9928dSSoby Mathew * to endorse or promote products derived from this software without specific 16*3ca9928dSSoby Mathew * prior written permission. 17*3ca9928dSSoby Mathew * 18*3ca9928dSSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19*3ca9928dSSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*3ca9928dSSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*3ca9928dSSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22*3ca9928dSSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*3ca9928dSSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*3ca9928dSSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*3ca9928dSSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*3ca9928dSSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*3ca9928dSSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*3ca9928dSSoby Mathew * POSSIBILITY OF SUCH DAMAGE. 29*3ca9928dSSoby Mathew */ 30*3ca9928dSSoby Mathew 31*3ca9928dSSoby Mathew #include <arch.h> 32*3ca9928dSSoby Mathew #include <arch_helpers.h> 33*3ca9928dSSoby Mathew #include <assert.h> 34*3ca9928dSSoby Mathew #include <cassert.h> 35*3ca9928dSSoby Mathew #include <platform_def.h> 36*3ca9928dSSoby Mathew #include <xlat_tables.h> 37*3ca9928dSSoby Mathew #include "../xlat_tables_private.h" 38*3ca9928dSSoby Mathew 39*3ca9928dSSoby Mathew #define IS_POWER_OF_TWO(x) (((x) & ((x) - 1)) == 0) 40*3ca9928dSSoby Mathew 41*3ca9928dSSoby Mathew /* 42*3ca9928dSSoby Mathew * The virtual address space size must be a power of two (as set in TCR.T0SZ). 43*3ca9928dSSoby Mathew * As we start the initial lookup at level 1, it must also be between 2 GB and 44*3ca9928dSSoby Mathew * 512 GB (with the virtual address size therefore 31 to 39 bits). See section 45*3ca9928dSSoby Mathew * D4.2.5 in the ARMv8-A Architecture Reference Manual (DDI 0487A.i) for more 46*3ca9928dSSoby Mathew * information. 47*3ca9928dSSoby Mathew */ 48*3ca9928dSSoby Mathew CASSERT(ADDR_SPACE_SIZE >= (1ull << 31) && ADDR_SPACE_SIZE <= (1ull << 39) && 49*3ca9928dSSoby Mathew IS_POWER_OF_TWO(ADDR_SPACE_SIZE), assert_valid_addr_space_size); 50*3ca9928dSSoby Mathew 51*3ca9928dSSoby Mathew #define UNSET_DESC ~0ul 52*3ca9928dSSoby Mathew #define NUM_L1_ENTRIES (ADDR_SPACE_SIZE >> L1_XLAT_ADDRESS_SHIFT) 53*3ca9928dSSoby Mathew 54*3ca9928dSSoby Mathew static uint64_t l1_xlation_table[NUM_L1_ENTRIES] 55*3ca9928dSSoby Mathew __aligned(NUM_L1_ENTRIES * sizeof(uint64_t)); 56*3ca9928dSSoby Mathew 57*3ca9928dSSoby Mathew static unsigned long long tcr_ps_bits; 58*3ca9928dSSoby Mathew 59*3ca9928dSSoby Mathew static unsigned long long calc_physical_addr_size_bits( 60*3ca9928dSSoby Mathew unsigned long long max_addr) 61*3ca9928dSSoby Mathew { 62*3ca9928dSSoby Mathew /* Physical address can't exceed 48 bits */ 63*3ca9928dSSoby Mathew assert((max_addr & ADDR_MASK_48_TO_63) == 0); 64*3ca9928dSSoby Mathew 65*3ca9928dSSoby Mathew /* 48 bits address */ 66*3ca9928dSSoby Mathew if (max_addr & ADDR_MASK_44_TO_47) 67*3ca9928dSSoby Mathew return TCR_PS_BITS_256TB; 68*3ca9928dSSoby Mathew 69*3ca9928dSSoby Mathew /* 44 bits address */ 70*3ca9928dSSoby Mathew if (max_addr & ADDR_MASK_42_TO_43) 71*3ca9928dSSoby Mathew return TCR_PS_BITS_16TB; 72*3ca9928dSSoby Mathew 73*3ca9928dSSoby Mathew /* 42 bits address */ 74*3ca9928dSSoby Mathew if (max_addr & ADDR_MASK_40_TO_41) 75*3ca9928dSSoby Mathew return TCR_PS_BITS_4TB; 76*3ca9928dSSoby Mathew 77*3ca9928dSSoby Mathew /* 40 bits address */ 78*3ca9928dSSoby Mathew if (max_addr & ADDR_MASK_36_TO_39) 79*3ca9928dSSoby Mathew return TCR_PS_BITS_1TB; 80*3ca9928dSSoby Mathew 81*3ca9928dSSoby Mathew /* 36 bits address */ 82*3ca9928dSSoby Mathew if (max_addr & ADDR_MASK_32_TO_35) 83*3ca9928dSSoby Mathew return TCR_PS_BITS_64GB; 84*3ca9928dSSoby Mathew 85*3ca9928dSSoby Mathew return TCR_PS_BITS_4GB; 86*3ca9928dSSoby Mathew } 87*3ca9928dSSoby Mathew 88*3ca9928dSSoby Mathew void init_xlat_tables(void) 89*3ca9928dSSoby Mathew { 90*3ca9928dSSoby Mathew unsigned long long max_pa; 91*3ca9928dSSoby Mathew uintptr_t max_va; 92*3ca9928dSSoby Mathew print_mmap(); 93*3ca9928dSSoby Mathew init_xlation_table(0, l1_xlation_table, 1, &max_va, &max_pa); 94*3ca9928dSSoby Mathew tcr_ps_bits = calc_physical_addr_size_bits(max_pa); 95*3ca9928dSSoby Mathew assert(max_va < ADDR_SPACE_SIZE); 96*3ca9928dSSoby Mathew } 97*3ca9928dSSoby Mathew 98*3ca9928dSSoby Mathew /******************************************************************************* 99*3ca9928dSSoby Mathew * Macro generating the code for the function enabling the MMU in the given 100*3ca9928dSSoby Mathew * exception level, assuming that the pagetables have already been created. 101*3ca9928dSSoby Mathew * 102*3ca9928dSSoby Mathew * _el: Exception level at which the function will run 103*3ca9928dSSoby Mathew * _tcr_extra: Extra bits to set in the TCR register. This mask will 104*3ca9928dSSoby Mathew * be OR'ed with the default TCR value. 105*3ca9928dSSoby Mathew * _tlbi_fct: Function to invalidate the TLBs at the current 106*3ca9928dSSoby Mathew * exception level 107*3ca9928dSSoby Mathew ******************************************************************************/ 108*3ca9928dSSoby Mathew #define DEFINE_ENABLE_MMU_EL(_el, _tcr_extra, _tlbi_fct) \ 109*3ca9928dSSoby Mathew void enable_mmu_el##_el(unsigned int flags) \ 110*3ca9928dSSoby Mathew { \ 111*3ca9928dSSoby Mathew uint64_t mair, tcr, ttbr; \ 112*3ca9928dSSoby Mathew uint32_t sctlr; \ 113*3ca9928dSSoby Mathew \ 114*3ca9928dSSoby Mathew assert(IS_IN_EL(_el)); \ 115*3ca9928dSSoby Mathew assert((read_sctlr_el##_el() & SCTLR_M_BIT) == 0); \ 116*3ca9928dSSoby Mathew \ 117*3ca9928dSSoby Mathew /* Set attributes in the right indices of the MAIR */ \ 118*3ca9928dSSoby Mathew mair = MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX); \ 119*3ca9928dSSoby Mathew mair |= MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, \ 120*3ca9928dSSoby Mathew ATTR_IWBWA_OWBWA_NTR_INDEX); \ 121*3ca9928dSSoby Mathew mair |= MAIR_ATTR_SET(ATTR_NON_CACHEABLE, \ 122*3ca9928dSSoby Mathew ATTR_NON_CACHEABLE_INDEX); \ 123*3ca9928dSSoby Mathew write_mair_el##_el(mair); \ 124*3ca9928dSSoby Mathew \ 125*3ca9928dSSoby Mathew /* Invalidate TLBs at the current exception level */ \ 126*3ca9928dSSoby Mathew _tlbi_fct(); \ 127*3ca9928dSSoby Mathew \ 128*3ca9928dSSoby Mathew /* Set TCR bits as well. */ \ 129*3ca9928dSSoby Mathew /* Inner & outer WBWA & shareable + T0SZ = 32 */ \ 130*3ca9928dSSoby Mathew tcr = TCR_SH_INNER_SHAREABLE | TCR_RGN_OUTER_WBA | \ 131*3ca9928dSSoby Mathew TCR_RGN_INNER_WBA | \ 132*3ca9928dSSoby Mathew (64 - __builtin_ctzl(ADDR_SPACE_SIZE)); \ 133*3ca9928dSSoby Mathew tcr |= _tcr_extra; \ 134*3ca9928dSSoby Mathew write_tcr_el##_el(tcr); \ 135*3ca9928dSSoby Mathew \ 136*3ca9928dSSoby Mathew /* Set TTBR bits as well */ \ 137*3ca9928dSSoby Mathew ttbr = (uint64_t) l1_xlation_table; \ 138*3ca9928dSSoby Mathew write_ttbr0_el##_el(ttbr); \ 139*3ca9928dSSoby Mathew \ 140*3ca9928dSSoby Mathew /* Ensure all translation table writes have drained */ \ 141*3ca9928dSSoby Mathew /* into memory, the TLB invalidation is complete, */ \ 142*3ca9928dSSoby Mathew /* and translation register writes are committed */ \ 143*3ca9928dSSoby Mathew /* before enabling the MMU */ \ 144*3ca9928dSSoby Mathew dsb(); \ 145*3ca9928dSSoby Mathew isb(); \ 146*3ca9928dSSoby Mathew \ 147*3ca9928dSSoby Mathew sctlr = read_sctlr_el##_el(); \ 148*3ca9928dSSoby Mathew sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT; \ 149*3ca9928dSSoby Mathew \ 150*3ca9928dSSoby Mathew if (flags & DISABLE_DCACHE) \ 151*3ca9928dSSoby Mathew sctlr &= ~SCTLR_C_BIT; \ 152*3ca9928dSSoby Mathew else \ 153*3ca9928dSSoby Mathew sctlr |= SCTLR_C_BIT; \ 154*3ca9928dSSoby Mathew \ 155*3ca9928dSSoby Mathew write_sctlr_el##_el(sctlr); \ 156*3ca9928dSSoby Mathew \ 157*3ca9928dSSoby Mathew /* Ensure the MMU enable takes effect immediately */ \ 158*3ca9928dSSoby Mathew isb(); \ 159*3ca9928dSSoby Mathew } 160*3ca9928dSSoby Mathew 161*3ca9928dSSoby Mathew /* Define EL1 and EL3 variants of the function enabling the MMU */ 162*3ca9928dSSoby Mathew DEFINE_ENABLE_MMU_EL(1, 163*3ca9928dSSoby Mathew (tcr_ps_bits << TCR_EL1_IPS_SHIFT), 164*3ca9928dSSoby Mathew tlbivmalle1) 165*3ca9928dSSoby Mathew DEFINE_ENABLE_MMU_EL(3, 166*3ca9928dSSoby Mathew TCR_EL3_RES1 | (tcr_ps_bits << TCR_EL3_PS_SHIFT), 167*3ca9928dSSoby Mathew tlbialle3) 168