Soby Mathew | 935c2e7 | 2016-06-30 15:11:07 +0100 | [diff] [blame] | 1 | /* |
Antonio Nino Diaz | 3759e3f | 2017-03-22 15:48:51 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | 935c2e7 | 2016-06-30 15:11:07 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Soby Mathew | 935c2e7 | 2016-06-30 15:11:07 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <arch.h> |
| 8 | #include <arch_helpers.h> |
| 9 | #include <assert.h> |
Soby Mathew | 935c2e7 | 2016-06-30 15:11:07 +0100 | [diff] [blame] | 10 | #include <platform_def.h> |
| 11 | #include <utils.h> |
Sandrine Bailleux | 090c849 | 2017-05-19 09:59:37 +0100 | [diff] [blame] | 12 | #include <xlat_tables_arch.h> |
Soby Mathew | 935c2e7 | 2016-06-30 15:11:07 +0100 | [diff] [blame] | 13 | #include <xlat_tables.h> |
| 14 | #include "../xlat_tables_private.h" |
| 15 | |
Sandrine Bailleux | 090c849 | 2017-05-19 09:59:37 +0100 | [diff] [blame] | 16 | #define XLAT_TABLE_LEVEL_BASE \ |
| 17 | GET_XLAT_TABLE_LEVEL_BASE(PLAT_VIRT_ADDR_SPACE_SIZE) |
Antonio Nino Diaz | d48ae61 | 2016-08-02 09:21:41 +0100 | [diff] [blame] | 18 | |
Sandrine Bailleux | 090c849 | 2017-05-19 09:59:37 +0100 | [diff] [blame] | 19 | #define NUM_BASE_LEVEL_ENTRIES \ |
| 20 | GET_NUM_BASE_LEVEL_ENTRIES(PLAT_VIRT_ADDR_SPACE_SIZE) |
Antonio Nino Diaz | d48ae61 | 2016-08-02 09:21:41 +0100 | [diff] [blame] | 21 | |
| 22 | static uint64_t base_xlation_table[NUM_BASE_LEVEL_ENTRIES] |
| 23 | __aligned(NUM_BASE_LEVEL_ENTRIES * sizeof(uint64_t)); |
Soby Mathew | 935c2e7 | 2016-06-30 15:11:07 +0100 | [diff] [blame] | 24 | |
Antonio Nino Diaz | 3759e3f | 2017-03-22 15:48:51 +0000 | [diff] [blame] | 25 | #if ENABLE_ASSERTIONS |
Antonio Nino Diaz | d1beee2 | 2016-12-13 15:28:54 +0000 | [diff] [blame] | 26 | static unsigned long long get_max_supported_pa(void) |
| 27 | { |
| 28 | /* Physical address space size for long descriptor format. */ |
| 29 | return (1ULL << 40) - 1ULL; |
| 30 | } |
Antonio Nino Diaz | 3759e3f | 2017-03-22 15:48:51 +0000 | [diff] [blame] | 31 | #endif /* ENABLE_ASSERTIONS */ |
Antonio Nino Diaz | d1beee2 | 2016-12-13 15:28:54 +0000 | [diff] [blame] | 32 | |
Antonio Nino Diaz | efabaa9 | 2017-04-27 13:30:22 +0100 | [diff] [blame] | 33 | int xlat_arch_current_el(void) |
| 34 | { |
| 35 | /* |
| 36 | * If EL3 is in AArch32 mode, all secure PL1 modes (Monitor, System, |
| 37 | * SVC, Abort, UND, IRQ and FIQ modes) execute at EL3. |
| 38 | */ |
| 39 | return 3; |
| 40 | } |
| 41 | |
| 42 | uint64_t xlat_arch_get_xn_desc(int el __unused) |
| 43 | { |
| 44 | return UPPER_ATTRS(XN); |
| 45 | } |
| 46 | |
Soby Mathew | 935c2e7 | 2016-06-30 15:11:07 +0100 | [diff] [blame] | 47 | void init_xlat_tables(void) |
| 48 | { |
| 49 | unsigned long long max_pa; |
| 50 | uintptr_t max_va; |
| 51 | print_mmap(); |
Antonio Nino Diaz | d48ae61 | 2016-08-02 09:21:41 +0100 | [diff] [blame] | 52 | init_xlation_table(0, base_xlation_table, XLAT_TABLE_LEVEL_BASE, |
| 53 | &max_va, &max_pa); |
Antonio Nino Diaz | d1beee2 | 2016-12-13 15:28:54 +0000 | [diff] [blame] | 54 | |
| 55 | assert(max_va <= PLAT_VIRT_ADDR_SPACE_SIZE - 1); |
| 56 | assert(max_pa <= PLAT_PHY_ADDR_SPACE_SIZE - 1); |
| 57 | assert((PLAT_PHY_ADDR_SPACE_SIZE - 1) <= get_max_supported_pa()); |
Soby Mathew | 935c2e7 | 2016-06-30 15:11:07 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /******************************************************************************* |
| 61 | * Function for enabling the MMU in Secure PL1, assuming that the |
| 62 | * page-tables have already been created. |
| 63 | ******************************************************************************/ |
| 64 | void enable_mmu_secure(unsigned int flags) |
| 65 | { |
| 66 | unsigned int mair0, ttbcr, sctlr; |
| 67 | uint64_t ttbr0; |
| 68 | |
| 69 | assert(IS_IN_SECURE()); |
| 70 | assert((read_sctlr() & SCTLR_M_BIT) == 0); |
| 71 | |
| 72 | /* Set attributes in the right indices of the MAIR */ |
| 73 | mair0 = MAIR0_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX); |
| 74 | mair0 |= MAIR0_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, |
| 75 | ATTR_IWBWA_OWBWA_NTR_INDEX); |
| 76 | mair0 |= MAIR0_ATTR_SET(ATTR_NON_CACHEABLE, |
| 77 | ATTR_NON_CACHEABLE_INDEX); |
| 78 | write_mair0(mair0); |
| 79 | |
| 80 | /* Invalidate TLBs at the current exception level */ |
| 81 | tlbiall(); |
| 82 | |
| 83 | /* |
Summer Qin | daf5dbb | 2017-03-16 17:16:34 +0000 | [diff] [blame] | 84 | * Set TTBCR bits as well. Set TTBR0 table properties. Disable TTBR1. |
Soby Mathew | 935c2e7 | 2016-06-30 15:11:07 +0100 | [diff] [blame] | 85 | */ |
Summer Qin | daf5dbb | 2017-03-16 17:16:34 +0000 | [diff] [blame] | 86 | if (flags & XLAT_TABLE_NC) { |
| 87 | /* Inner & outer non-cacheable non-shareable. */ |
| 88 | ttbcr = TTBCR_EAE_BIT | |
| 89 | TTBCR_SH0_NON_SHAREABLE | TTBCR_RGN0_OUTER_NC | |
| 90 | TTBCR_RGN0_INNER_NC | |
Sandrine Bailleux | 12e8644 | 2017-07-19 10:11:13 +0100 | [diff] [blame] | 91 | (32 - __builtin_ctzll(PLAT_VIRT_ADDR_SPACE_SIZE)); |
Summer Qin | daf5dbb | 2017-03-16 17:16:34 +0000 | [diff] [blame] | 92 | } else { |
| 93 | /* Inner & outer WBWA & shareable. */ |
| 94 | ttbcr = TTBCR_EAE_BIT | |
| 95 | TTBCR_SH0_INNER_SHAREABLE | TTBCR_RGN0_OUTER_WBA | |
| 96 | TTBCR_RGN0_INNER_WBA | |
Sandrine Bailleux | 12e8644 | 2017-07-19 10:11:13 +0100 | [diff] [blame] | 97 | (32 - __builtin_ctzll(PLAT_VIRT_ADDR_SPACE_SIZE)); |
Summer Qin | daf5dbb | 2017-03-16 17:16:34 +0000 | [diff] [blame] | 98 | } |
Soby Mathew | 935c2e7 | 2016-06-30 15:11:07 +0100 | [diff] [blame] | 99 | ttbcr |= TTBCR_EPD1_BIT; |
| 100 | write_ttbcr(ttbcr); |
| 101 | |
| 102 | /* Set TTBR0 bits as well */ |
Antonio Nino Diaz | d48ae61 | 2016-08-02 09:21:41 +0100 | [diff] [blame] | 103 | ttbr0 = (uintptr_t) base_xlation_table; |
Soby Mathew | 935c2e7 | 2016-06-30 15:11:07 +0100 | [diff] [blame] | 104 | write64_ttbr0(ttbr0); |
| 105 | write64_ttbr1(0); |
| 106 | |
| 107 | /* |
| 108 | * Ensure all translation table writes have drained |
| 109 | * into memory, the TLB invalidation is complete, |
| 110 | * and translation register writes are committed |
| 111 | * before enabling the MMU |
| 112 | */ |
Dimitris Papastamos | 12f8be5 | 2017-06-20 09:25:10 +0100 | [diff] [blame] | 113 | dsbish(); |
Soby Mathew | 935c2e7 | 2016-06-30 15:11:07 +0100 | [diff] [blame] | 114 | isb(); |
| 115 | |
| 116 | sctlr = read_sctlr(); |
| 117 | sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT; |
| 118 | |
| 119 | if (flags & DISABLE_DCACHE) |
| 120 | sctlr &= ~SCTLR_C_BIT; |
| 121 | else |
| 122 | sctlr |= SCTLR_C_BIT; |
| 123 | |
| 124 | write_sctlr(sctlr); |
| 125 | |
| 126 | /* Ensure the MMU enable takes effect immediately */ |
| 127 | isb(); |
| 128 | } |