17bb01fb2SAntonio Nino Diaz /*
2*aaaf2cc3SSona Mathew * Copyright (c) 2017-2024, Arm Limited and Contributors. All rights reserved.
37bb01fb2SAntonio Nino Diaz *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
57bb01fb2SAntonio Nino Diaz */
67bb01fb2SAntonio Nino Diaz
709d40e0eSAntonio Nino Diaz #include <assert.h>
809d40e0eSAntonio Nino Diaz #include <stdbool.h>
909d40e0eSAntonio Nino Diaz
1009d40e0eSAntonio Nino Diaz #include <platform_def.h>
1109d40e0eSAntonio Nino Diaz
127bb01fb2SAntonio Nino Diaz #include <arch.h>
132559b2c8SAntonio Nino Diaz #include <arch_features.h>
147bb01fb2SAntonio Nino Diaz #include <arch_helpers.h>
1509d40e0eSAntonio Nino Diaz #include <lib/cassert.h>
1609d40e0eSAntonio Nino Diaz #include <lib/utils_def.h>
1709d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_v2.h>
1809d40e0eSAntonio Nino Diaz
197bb01fb2SAntonio Nino Diaz #include "../xlat_tables_private.h"
207bb01fb2SAntonio Nino Diaz
21e7b9886cSAntonio Nino Diaz #if (ARM_ARCH_MAJOR == 7) && !defined(ARMV7_SUPPORTS_LARGE_PAGE_ADDRESSING)
2251b992ecSEtienne Carriere #error ARMv7 target does not support LPAE MMU descriptors
2351b992ecSEtienne Carriere #endif
2451b992ecSEtienne Carriere
25a0b9bb79SAntonio Nino Diaz /*
265b395e37SAntonio Nino Diaz * Returns true if the provided granule size is supported, false otherwise.
27a0b9bb79SAntonio Nino Diaz */
xlat_arch_is_granule_size_supported(size_t size)285b395e37SAntonio Nino Diaz bool xlat_arch_is_granule_size_supported(size_t size)
29a0b9bb79SAntonio Nino Diaz {
30a0b9bb79SAntonio Nino Diaz /*
311dd6c051SAntonio Nino Diaz * The library uses the long descriptor translation table format, which
321dd6c051SAntonio Nino Diaz * supports 4 KiB pages only.
33a0b9bb79SAntonio Nino Diaz */
345b395e37SAntonio Nino Diaz return size == PAGE_SIZE_4KB;
35a0b9bb79SAntonio Nino Diaz }
36a0b9bb79SAntonio Nino Diaz
xlat_arch_get_max_supported_granule_size(void)37a0b9bb79SAntonio Nino Diaz size_t xlat_arch_get_max_supported_granule_size(void)
38a0b9bb79SAntonio Nino Diaz {
39e7b9886cSAntonio Nino Diaz return PAGE_SIZE_4KB;
40a0b9bb79SAntonio Nino Diaz }
41a0b9bb79SAntonio Nino Diaz
4236218238SZelalem Aweke /*
4336218238SZelalem Aweke * Determine the physical address space encoded in the 'attr' parameter.
4436218238SZelalem Aweke *
4536218238SZelalem Aweke * The physical address will fall into one of two spaces; secure or
4636218238SZelalem Aweke * nonsecure.
4736218238SZelalem Aweke */
xlat_arch_get_pas(uint32_t attr)4836218238SZelalem Aweke uint32_t xlat_arch_get_pas(uint32_t attr)
4936218238SZelalem Aweke {
5036218238SZelalem Aweke uint32_t pas = MT_PAS(attr);
5136218238SZelalem Aweke
5236218238SZelalem Aweke if (pas == MT_NS) {
5336218238SZelalem Aweke return LOWER_ATTRS(NS);
5436218238SZelalem Aweke } else { /* MT_SECURE */
5536218238SZelalem Aweke return 0U;
5636218238SZelalem Aweke }
5736218238SZelalem Aweke }
5836218238SZelalem Aweke
59aa61368eSAntonio Nino Diaz #if ENABLE_ASSERTIONS
xlat_arch_get_max_supported_pa(void)6099f60798SSandrine Bailleux unsigned long long xlat_arch_get_max_supported_pa(void)
617bb01fb2SAntonio Nino Diaz {
627bb01fb2SAntonio Nino Diaz /* Physical address space size for long descriptor format. */
635724481fSDavid Cunado return (1ULL << 40) - 1ULL;
647bb01fb2SAntonio Nino Diaz }
65cedfa04bSSathees Balya
66cedfa04bSSathees Balya /*
67cedfa04bSSathees Balya * Return minimum virtual address space size supported by the architecture
68cedfa04bSSathees Balya */
xlat_get_min_virt_addr_space_size(void)69cedfa04bSSathees Balya uintptr_t xlat_get_min_virt_addr_space_size(void)
70cedfa04bSSathees Balya {
71cedfa04bSSathees Balya return MIN_VIRT_ADDR_SPACE_SIZE;
72cedfa04bSSathees Balya }
73aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS*/
747bb01fb2SAntonio Nino Diaz
is_mmu_enabled_ctx(const xlat_ctx_t * ctx)751a92a0e0SAntonio Nino Diaz bool is_mmu_enabled_ctx(const xlat_ctx_t *ctx)
767bb01fb2SAntonio Nino Diaz {
771a92a0e0SAntonio Nino Diaz if (ctx->xlat_regime == EL1_EL0_REGIME) {
781a92a0e0SAntonio Nino Diaz assert(xlat_arch_current_el() == 1U);
791a92a0e0SAntonio Nino Diaz return (read_sctlr() & SCTLR_M_BIT) != 0U;
801a92a0e0SAntonio Nino Diaz } else {
811a92a0e0SAntonio Nino Diaz assert(ctx->xlat_regime == EL2_REGIME);
821a92a0e0SAntonio Nino Diaz assert(xlat_arch_current_el() == 2U);
831a92a0e0SAntonio Nino Diaz return (read_hsctlr() & HSCTLR_M_BIT) != 0U;
841a92a0e0SAntonio Nino Diaz }
857bb01fb2SAntonio Nino Diaz }
867bb01fb2SAntonio Nino Diaz
is_dcache_enabled(void)873e318e40SAntonio Nino Diaz bool is_dcache_enabled(void)
883e318e40SAntonio Nino Diaz {
891a92a0e0SAntonio Nino Diaz if (IS_IN_EL2()) {
901a92a0e0SAntonio Nino Diaz return (read_hsctlr() & HSCTLR_C_BIT) != 0U;
911a92a0e0SAntonio Nino Diaz } else {
921a92a0e0SAntonio Nino Diaz return (read_sctlr() & SCTLR_C_BIT) != 0U;
931a92a0e0SAntonio Nino Diaz }
943e318e40SAntonio Nino Diaz }
953e318e40SAntonio Nino Diaz
xlat_arch_regime_get_xn_desc(int xlat_regime)961a92a0e0SAntonio Nino Diaz uint64_t xlat_arch_regime_get_xn_desc(int xlat_regime)
97468e2382SAntonio Nino Diaz {
981a92a0e0SAntonio Nino Diaz if (xlat_regime == EL1_EL0_REGIME) {
991a92a0e0SAntonio Nino Diaz return UPPER_ATTRS(XN) | UPPER_ATTRS(PXN);
1001a92a0e0SAntonio Nino Diaz } else {
1011a92a0e0SAntonio Nino Diaz assert(xlat_regime == EL2_REGIME);
102468e2382SAntonio Nino Diaz return UPPER_ATTRS(XN);
103468e2382SAntonio Nino Diaz }
1041a92a0e0SAntonio Nino Diaz }
105468e2382SAntonio Nino Diaz
xlat_arch_tlbi_va(uintptr_t va,int xlat_regime)1061a92a0e0SAntonio Nino Diaz void xlat_arch_tlbi_va(uintptr_t va, int xlat_regime)
107b4ae615bSDouglas Raillard {
108b4ae615bSDouglas Raillard /*
109b4ae615bSDouglas Raillard * Ensure the translation table write has drained into memory before
110b4ae615bSDouglas Raillard * invalidating the TLB entry.
111b4ae615bSDouglas Raillard */
112b4ae615bSDouglas Raillard dsbishst();
113b4ae615bSDouglas Raillard
1141a92a0e0SAntonio Nino Diaz if (xlat_regime == EL1_EL0_REGIME) {
115b4ae615bSDouglas Raillard tlbimvaais(TLBI_ADDR(va));
1161a92a0e0SAntonio Nino Diaz } else {
1171a92a0e0SAntonio Nino Diaz assert(xlat_regime == EL2_REGIME);
1181a92a0e0SAntonio Nino Diaz tlbimvahis(TLBI_ADDR(va));
1191a92a0e0SAntonio Nino Diaz }
120b4ae615bSDouglas Raillard }
121b4ae615bSDouglas Raillard
xlat_arch_tlbi_va_sync(void)1220b64f4efSAntonio Nino Diaz void xlat_arch_tlbi_va_sync(void)
1230b64f4efSAntonio Nino Diaz {
1240b64f4efSAntonio Nino Diaz /* Invalidate all entries from branch predictors. */
1250b64f4efSAntonio Nino Diaz bpiallis();
1260b64f4efSAntonio Nino Diaz
1270b64f4efSAntonio Nino Diaz /*
1280b64f4efSAntonio Nino Diaz * A TLB maintenance instruction can complete at any time after
1290b64f4efSAntonio Nino Diaz * it is issued, but is only guaranteed to be complete after the
1300b64f4efSAntonio Nino Diaz * execution of DSB by the PE that executed the TLB maintenance
1310b64f4efSAntonio Nino Diaz * instruction. After the TLB invalidate instruction is
1320b64f4efSAntonio Nino Diaz * complete, no new memory accesses using the invalidated TLB
1330b64f4efSAntonio Nino Diaz * entries will be observed by any observer of the system
1340b64f4efSAntonio Nino Diaz * domain. See section D4.8.2 of the ARMv8 (issue k), paragraph
1350b64f4efSAntonio Nino Diaz * "Ordering and completion of TLB maintenance instructions".
1360b64f4efSAntonio Nino Diaz */
1370b64f4efSAntonio Nino Diaz dsbish();
1380b64f4efSAntonio Nino Diaz
1390b64f4efSAntonio Nino Diaz /*
1400b64f4efSAntonio Nino Diaz * The effects of a completed TLB maintenance instruction are
1410b64f4efSAntonio Nino Diaz * only guaranteed to be visible on the PE that executed the
1420b64f4efSAntonio Nino Diaz * instruction after the execution of an ISB instruction by the
1430b64f4efSAntonio Nino Diaz * PE that executed the TLB maintenance instruction.
1440b64f4efSAntonio Nino Diaz */
1450b64f4efSAntonio Nino Diaz isb();
1460b64f4efSAntonio Nino Diaz }
1470b64f4efSAntonio Nino Diaz
xlat_arch_current_el(void)148e7b9886cSAntonio Nino Diaz unsigned int xlat_arch_current_el(void)
149a5640252SAntonio Nino Diaz {
1501a92a0e0SAntonio Nino Diaz if (IS_IN_HYP()) {
1511a92a0e0SAntonio Nino Diaz return 2U;
1521a92a0e0SAntonio Nino Diaz } else {
1531a92a0e0SAntonio Nino Diaz assert(IS_IN_SVC() || IS_IN_MON());
154a5640252SAntonio Nino Diaz /*
1551a92a0e0SAntonio Nino Diaz * If EL3 is in AArch32 mode, all secure PL1 modes (Monitor,
1561a92a0e0SAntonio Nino Diaz * System, SVC, Abort, UND, IRQ and FIQ modes) execute at EL3.
157aa1d5f60SAntonio Nino Diaz *
1581a92a0e0SAntonio Nino Diaz * The PL1&0 translation regime in AArch32 behaves like the
1591a92a0e0SAntonio Nino Diaz * EL1&0 regime in AArch64 except for the XN bits, but we set
1601a92a0e0SAntonio Nino Diaz * and unset them at the same time, so there's no difference in
1611a92a0e0SAntonio Nino Diaz * practice.
162a5640252SAntonio Nino Diaz */
163e7b9886cSAntonio Nino Diaz return 1U;
164a5640252SAntonio Nino Diaz }
1651a92a0e0SAntonio Nino Diaz }
166a5640252SAntonio Nino Diaz
1677bb01fb2SAntonio Nino Diaz /*******************************************************************************
1681a92a0e0SAntonio Nino Diaz * Function for enabling the MMU in PL1 or PL2, assuming that the page tables
169d83f3579SSandrine Bailleux * have already been created.
1707bb01fb2SAntonio Nino Diaz ******************************************************************************/
setup_mmu_cfg(uint64_t * params,unsigned int flags,const uint64_t * base_table,unsigned long long max_pa,uintptr_t max_va,__unused int xlat_regime)17163ddbae3SAntonio Nino Diaz void setup_mmu_cfg(uint64_t *params, unsigned int flags,
17263ddbae3SAntonio Nino Diaz const uint64_t *base_table, unsigned long long max_pa,
17363ddbae3SAntonio Nino Diaz uintptr_t max_va, __unused int xlat_regime)
1747bb01fb2SAntonio Nino Diaz {
1756563c0beSAntonio Nino Diaz uint64_t mair, ttbr0;
1766563c0beSAntonio Nino Diaz uint32_t ttbcr;
1777bb01fb2SAntonio Nino Diaz
1787bb01fb2SAntonio Nino Diaz /* Set attributes in the right indices of the MAIR */
1796563c0beSAntonio Nino Diaz mair = MAIR0_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX);
1806563c0beSAntonio Nino Diaz mair |= MAIR0_ATTR_SET(ATTR_IWBWA_OWBWA_NTR,
1817bb01fb2SAntonio Nino Diaz ATTR_IWBWA_OWBWA_NTR_INDEX);
1826563c0beSAntonio Nino Diaz mair |= MAIR0_ATTR_SET(ATTR_NON_CACHEABLE,
1837bb01fb2SAntonio Nino Diaz ATTR_NON_CACHEABLE_INDEX);
1847bb01fb2SAntonio Nino Diaz
1857bb01fb2SAntonio Nino Diaz /*
1861a92a0e0SAntonio Nino Diaz * Configure the control register for stage 1 of the PL1&0 or EL2
1871a92a0e0SAntonio Nino Diaz * translation regimes.
188d83f3579SSandrine Bailleux */
189d83f3579SSandrine Bailleux
190d83f3579SSandrine Bailleux /* Use the Long-descriptor translation table format. */
191d83f3579SSandrine Bailleux ttbcr = TTBCR_EAE_BIT;
192d83f3579SSandrine Bailleux
1931a92a0e0SAntonio Nino Diaz if (xlat_regime == EL1_EL0_REGIME) {
1941a92a0e0SAntonio Nino Diaz assert(IS_IN_SVC() || IS_IN_MON());
195d83f3579SSandrine Bailleux /*
1961a92a0e0SAntonio Nino Diaz * Disable translation table walk for addresses that are
1971a92a0e0SAntonio Nino Diaz * translated using TTBR1. Therefore, only TTBR0 is used.
198d83f3579SSandrine Bailleux */
199d83f3579SSandrine Bailleux ttbcr |= TTBCR_EPD1_BIT;
2001a92a0e0SAntonio Nino Diaz } else {
2011a92a0e0SAntonio Nino Diaz assert(xlat_regime == EL2_REGIME);
2021a92a0e0SAntonio Nino Diaz assert(IS_IN_HYP());
2031a92a0e0SAntonio Nino Diaz
2041a92a0e0SAntonio Nino Diaz /*
2051a92a0e0SAntonio Nino Diaz * Set HTCR bits as well. Set HTTBR table properties
2061a92a0e0SAntonio Nino Diaz * as Inner & outer WBWA & shareable.
2071a92a0e0SAntonio Nino Diaz */
2081a92a0e0SAntonio Nino Diaz ttbcr |= HTCR_RES1 |
2091a92a0e0SAntonio Nino Diaz HTCR_SH0_INNER_SHAREABLE | HTCR_RGN0_OUTER_WBA |
2101a92a0e0SAntonio Nino Diaz HTCR_RGN0_INNER_WBA;
2111a92a0e0SAntonio Nino Diaz }
212d83f3579SSandrine Bailleux
213d83f3579SSandrine Bailleux /*
214d83f3579SSandrine Bailleux * Limit the input address ranges and memory region sizes translated
215347621bbSSandrine Bailleux * using TTBR0 to the given virtual address space size, if smaller than
216347621bbSSandrine Bailleux * 32 bits.
217d83f3579SSandrine Bailleux */
218347621bbSSandrine Bailleux if (max_va != UINT32_MAX) {
219e7b9886cSAntonio Nino Diaz uintptr_t virtual_addr_space_size = max_va + 1U;
220e7b9886cSAntonio Nino Diaz
221cedfa04bSSathees Balya assert(virtual_addr_space_size >=
222cedfa04bSSathees Balya xlat_get_min_virt_addr_space_size());
223cedfa04bSSathees Balya assert(IS_POWER_OF_TWO(virtual_addr_space_size));
224cedfa04bSSathees Balya
225347621bbSSandrine Bailleux /*
2260044231dSSandrine Bailleux * __builtin_ctzll(0) is undefined but here we are guaranteed
227347621bbSSandrine Bailleux * that virtual_addr_space_size is in the range [1, UINT32_MAX].
228347621bbSSandrine Bailleux */
229e7b9886cSAntonio Nino Diaz int t0sz = 32 - __builtin_ctzll(virtual_addr_space_size);
230e7b9886cSAntonio Nino Diaz
231e7b9886cSAntonio Nino Diaz ttbcr |= (uint32_t) t0sz;
232347621bbSSandrine Bailleux }
233d83f3579SSandrine Bailleux
234d83f3579SSandrine Bailleux /*
235d83f3579SSandrine Bailleux * Set the cacheability and shareability attributes for memory
236d83f3579SSandrine Bailleux * associated with translation table walks using TTBR0.
2377bb01fb2SAntonio Nino Diaz */
238e7b9886cSAntonio Nino Diaz if ((flags & XLAT_TABLE_NC) != 0U) {
2395d21b037SSummer Qin /* Inner & outer non-cacheable non-shareable. */
240d83f3579SSandrine Bailleux ttbcr |= TTBCR_SH0_NON_SHAREABLE | TTBCR_RGN0_OUTER_NC |
241d83f3579SSandrine Bailleux TTBCR_RGN0_INNER_NC;
2425d21b037SSummer Qin } else {
2435d21b037SSummer Qin /* Inner & outer WBWA & shareable. */
244d83f3579SSandrine Bailleux ttbcr |= TTBCR_SH0_INNER_SHAREABLE | TTBCR_RGN0_OUTER_WBA |
245d83f3579SSandrine Bailleux TTBCR_RGN0_INNER_WBA;
2465d21b037SSummer Qin }
2477bb01fb2SAntonio Nino Diaz
2487bb01fb2SAntonio Nino Diaz /* Set TTBR0 bits as well */
2497bb01fb2SAntonio Nino Diaz ttbr0 = (uint64_t)(uintptr_t) base_table;
2506563c0beSAntonio Nino Diaz
251*aaaf2cc3SSona Mathew if (is_feat_ttcnp_present()) {
2522559b2c8SAntonio Nino Diaz /* Enable CnP bit so as to share page tables with all PEs. */
2539fce2725SIsla Mitchell ttbr0 |= TTBR_CNP_BIT;
2542559b2c8SAntonio Nino Diaz }
255d83f3579SSandrine Bailleux
2560cc7aa89SJeenu Viswambharan /* Now populate MMU configuration */
25763ddbae3SAntonio Nino Diaz params[MMU_CFG_MAIR] = mair;
25863ddbae3SAntonio Nino Diaz params[MMU_CFG_TCR] = (uint64_t) ttbcr;
25963ddbae3SAntonio Nino Diaz params[MMU_CFG_TTBR0] = ttbr0;
2607bb01fb2SAntonio Nino Diaz }
261