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