1c9fe6fedSEtienne Carriere /* 2c9fe6fedSEtienne Carriere * Copyright (c) 2016-2017, Linaro Limited. All rights reserved. 3*4249e8b9SJustin Chadwell * Copyright (c) 2014-2019, Arm Limited. All rights reserved. 4c9fe6fedSEtienne Carriere * Copyright (c) 2014, STMicroelectronics International N.V. 5c9fe6fedSEtienne Carriere * All rights reserved. 6c9fe6fedSEtienne Carriere * 7c9fe6fedSEtienne Carriere * SPDX-License-Identifier: BSD-3-Clause 8c9fe6fedSEtienne Carriere */ 9c9fe6fedSEtienne Carriere 10c9fe6fedSEtienne Carriere #include <assert.h> 11c9fe6fedSEtienne Carriere #include <stdio.h> 12c9fe6fedSEtienne Carriere #include <string.h> 13c9fe6fedSEtienne Carriere 14c9fe6fedSEtienne Carriere #include <platform_def.h> 15c9fe6fedSEtienne Carriere 16c9fe6fedSEtienne Carriere #include <arch.h> 17c9fe6fedSEtienne Carriere #include <arch_helpers.h> 18c9fe6fedSEtienne Carriere #include <common/debug.h> 19c9fe6fedSEtienne Carriere #include <lib/cassert.h> 20c9fe6fedSEtienne Carriere #include <lib/utils.h> 21c9fe6fedSEtienne Carriere #include <lib/xlat_tables/xlat_tables.h> 22c9fe6fedSEtienne Carriere 23c9fe6fedSEtienne Carriere #include "../xlat_tables_private.h" 24c9fe6fedSEtienne Carriere 25c9fe6fedSEtienne Carriere #ifdef ARMV7_SUPPORTS_LARGE_PAGE_ADDRESSING 26c9fe6fedSEtienne Carriere #error "ARMV7_SUPPORTS_LARGE_PAGE_ADDRESSING flag is set. \ 27c9fe6fedSEtienne Carriere This module is to be used when LPAE is not supported" 28c9fe6fedSEtienne Carriere #endif 29c9fe6fedSEtienne Carriere 30c9fe6fedSEtienne Carriere CASSERT(PLAT_VIRT_ADDR_SPACE_SIZE == (1ULL << 32), invalid_vaddr_space_size); 31c9fe6fedSEtienne Carriere CASSERT(PLAT_PHY_ADDR_SPACE_SIZE == (1ULL << 32), invalid_paddr_space_size); 32c9fe6fedSEtienne Carriere 33c9fe6fedSEtienne Carriere #define MMU32B_UNSET_DESC ~0ul 34c9fe6fedSEtienne Carriere #define MMU32B_INVALID_DESC 0ul 35c9fe6fedSEtienne Carriere 36c9fe6fedSEtienne Carriere #define MT_UNKNOWN ~0U 37c9fe6fedSEtienne Carriere 38c9fe6fedSEtienne Carriere /* 39c9fe6fedSEtienne Carriere * MMU related values 40c9fe6fedSEtienne Carriere */ 41c9fe6fedSEtienne Carriere 42c9fe6fedSEtienne Carriere /* Sharable */ 43c9fe6fedSEtienne Carriere #define MMU32B_TTB_S (1 << 1) 44c9fe6fedSEtienne Carriere 45c9fe6fedSEtienne Carriere /* Not Outer Sharable */ 46c9fe6fedSEtienne Carriere #define MMU32B_TTB_NOS (1 << 5) 47c9fe6fedSEtienne Carriere 48c9fe6fedSEtienne Carriere /* Normal memory, Inner Non-cacheable */ 49c9fe6fedSEtienne Carriere #define MMU32B_TTB_IRGN_NC 0 50c9fe6fedSEtienne Carriere 51c9fe6fedSEtienne Carriere /* Normal memory, Inner Write-Back Write-Allocate Cacheable */ 52c9fe6fedSEtienne Carriere #define MMU32B_TTB_IRGN_WBWA (1 << 6) 53c9fe6fedSEtienne Carriere 54c9fe6fedSEtienne Carriere /* Normal memory, Inner Write-Through Cacheable */ 55c9fe6fedSEtienne Carriere #define MMU32B_TTB_IRGN_WT 1 56c9fe6fedSEtienne Carriere 57c9fe6fedSEtienne Carriere /* Normal memory, Inner Write-Back no Write-Allocate Cacheable */ 58c9fe6fedSEtienne Carriere #define MMU32B_TTB_IRGN_WB (1 | (1 << 6)) 59c9fe6fedSEtienne Carriere 60c9fe6fedSEtienne Carriere /* Normal memory, Outer Write-Back Write-Allocate Cacheable */ 61c9fe6fedSEtienne Carriere #define MMU32B_TTB_RNG_WBWA (1 << 3) 62c9fe6fedSEtienne Carriere 63c9fe6fedSEtienne Carriere #define MMU32B_DEFAULT_ATTRS \ 64c9fe6fedSEtienne Carriere (MMU32B_TTB_S | MMU32B_TTB_NOS | \ 65c9fe6fedSEtienne Carriere MMU32B_TTB_IRGN_WBWA | MMU32B_TTB_RNG_WBWA) 66c9fe6fedSEtienne Carriere 67c9fe6fedSEtienne Carriere /* armv7 memory mapping attributes: section mapping */ 68c9fe6fedSEtienne Carriere #define SECTION_SECURE (0 << 19) 69c9fe6fedSEtienne Carriere #define SECTION_NOTSECURE (1 << 19) 70c9fe6fedSEtienne Carriere #define SECTION_SHARED (1 << 16) 71c9fe6fedSEtienne Carriere #define SECTION_NOTGLOBAL (1 << 17) 72c9fe6fedSEtienne Carriere #define SECTION_ACCESS_FLAG (1 << 10) 73c9fe6fedSEtienne Carriere #define SECTION_UNPRIV (1 << 11) 74c9fe6fedSEtienne Carriere #define SECTION_RO (1 << 15) 75c9fe6fedSEtienne Carriere #define SECTION_TEX(tex) ((((tex) >> 2) << 12) | \ 76c9fe6fedSEtienne Carriere ((((tex) >> 1) & 0x1) << 3) | \ 77c9fe6fedSEtienne Carriere (((tex) & 0x1) << 2)) 78c9fe6fedSEtienne Carriere #define SECTION_DEVICE SECTION_TEX(MMU32B_ATTR_DEVICE_INDEX) 79c9fe6fedSEtienne Carriere #define SECTION_NORMAL SECTION_TEX(MMU32B_ATTR_DEVICE_INDEX) 80c9fe6fedSEtienne Carriere #define SECTION_NORMAL_CACHED \ 81c9fe6fedSEtienne Carriere SECTION_TEX(MMU32B_ATTR_IWBWA_OWBWA_INDEX) 82c9fe6fedSEtienne Carriere 83c9fe6fedSEtienne Carriere #define SECTION_XN (1 << 4) 84c9fe6fedSEtienne Carriere #define SECTION_PXN (1 << 0) 85c9fe6fedSEtienne Carriere #define SECTION_SECTION (2 << 0) 86c9fe6fedSEtienne Carriere 87c9fe6fedSEtienne Carriere #define SECTION_PT_NOTSECURE (1 << 3) 88c9fe6fedSEtienne Carriere #define SECTION_PT_PT (1 << 0) 89c9fe6fedSEtienne Carriere 90c9fe6fedSEtienne Carriere #define SMALL_PAGE_SMALL_PAGE (1 << 1) 91c9fe6fedSEtienne Carriere #define SMALL_PAGE_SHARED (1 << 10) 92c9fe6fedSEtienne Carriere #define SMALL_PAGE_NOTGLOBAL (1 << 11) 93c9fe6fedSEtienne Carriere #define SMALL_PAGE_TEX(tex) ((((tex) >> 2) << 6) | \ 94c9fe6fedSEtienne Carriere ((((tex) >> 1) & 0x1) << 3) | \ 95c9fe6fedSEtienne Carriere (((tex) & 0x1) << 2)) 96c9fe6fedSEtienne Carriere #define SMALL_PAGE_DEVICE \ 97c9fe6fedSEtienne Carriere SMALL_PAGE_TEX(MMU32B_ATTR_DEVICE_INDEX) 98c9fe6fedSEtienne Carriere #define SMALL_PAGE_NORMAL \ 99c9fe6fedSEtienne Carriere SMALL_PAGE_TEX(MMU32B_ATTR_DEVICE_INDEX) 100c9fe6fedSEtienne Carriere #define SMALL_PAGE_NORMAL_CACHED \ 101c9fe6fedSEtienne Carriere SMALL_PAGE_TEX(MMU32B_ATTR_IWBWA_OWBWA_INDEX) 102c9fe6fedSEtienne Carriere #define SMALL_PAGE_ACCESS_FLAG (1 << 4) 103c9fe6fedSEtienne Carriere #define SMALL_PAGE_UNPRIV (1 << 5) 104c9fe6fedSEtienne Carriere #define SMALL_PAGE_RO (1 << 9) 105c9fe6fedSEtienne Carriere #define SMALL_PAGE_XN (1 << 0) 106c9fe6fedSEtienne Carriere 107c9fe6fedSEtienne Carriere /* The TEX, C and B bits concatenated */ 108c9fe6fedSEtienne Carriere #define MMU32B_ATTR_DEVICE_INDEX 0x0 109c9fe6fedSEtienne Carriere #define MMU32B_ATTR_IWBWA_OWBWA_INDEX 0x1 110c9fe6fedSEtienne Carriere 111c9fe6fedSEtienne Carriere #define MMU32B_PRRR_IDX(idx, tr, nos) (((tr) << (2 * (idx))) | \ 112c9fe6fedSEtienne Carriere ((uint32_t)(nos) << ((idx) + 24))) 113c9fe6fedSEtienne Carriere #define MMU32B_NMRR_IDX(idx, ir, or) (((ir) << (2 * (idx))) | \ 114c9fe6fedSEtienne Carriere ((uint32_t)(or) << (2 * (idx) + 16))) 115c9fe6fedSEtienne Carriere #define MMU32B_PRRR_DS0 (1 << 16) 116c9fe6fedSEtienne Carriere #define MMU32B_PRRR_DS1 (1 << 17) 117c9fe6fedSEtienne Carriere #define MMU32B_PRRR_NS0 (1 << 18) 118c9fe6fedSEtienne Carriere #define MMU32B_PRRR_NS1 (1 << 19) 119c9fe6fedSEtienne Carriere 120c9fe6fedSEtienne Carriere #define DACR_DOMAIN(num, perm) ((perm) << ((num) * 2)) 121c9fe6fedSEtienne Carriere #define DACR_DOMAIN_PERM_NO_ACCESS 0x0 122c9fe6fedSEtienne Carriere #define DACR_DOMAIN_PERM_CLIENT 0x1 123c9fe6fedSEtienne Carriere #define DACR_DOMAIN_PERM_MANAGER 0x3 124c9fe6fedSEtienne Carriere 1253e43121eSJustin Chadwell #define NUM_1MB_IN_4GB (1U << 12) 1263e43121eSJustin Chadwell #define NUM_4K_IN_1MB (1U << 8) 127c9fe6fedSEtienne Carriere 128c9fe6fedSEtienne Carriere #define ONE_MB_SHIFT 20 129c9fe6fedSEtienne Carriere 130c9fe6fedSEtienne Carriere /* mmu 32b integration */ 131c9fe6fedSEtienne Carriere #define MMU32B_L1_TABLE_SIZE (NUM_1MB_IN_4GB * 4) 132c9fe6fedSEtienne Carriere #define MMU32B_L2_TABLE_SIZE (NUM_4K_IN_1MB * 4) 133c9fe6fedSEtienne Carriere #define MMU32B_L1_TABLE_ALIGN (1 << 14) 134c9fe6fedSEtienne Carriere #define MMU32B_L2_TABLE_ALIGN (1 << 10) 135c9fe6fedSEtienne Carriere 136c9fe6fedSEtienne Carriere static unsigned int next_xlat; 137c9fe6fedSEtienne Carriere static unsigned long long xlat_max_pa; 138c9fe6fedSEtienne Carriere static uintptr_t xlat_max_va; 139c9fe6fedSEtienne Carriere 140c9fe6fedSEtienne Carriere static uint32_t mmu_l1_base[NUM_1MB_IN_4GB] 141c9fe6fedSEtienne Carriere __aligned(MMU32B_L1_TABLE_ALIGN) __attribute__((section("xlat_table"))); 142c9fe6fedSEtienne Carriere 143c9fe6fedSEtienne Carriere static uint32_t mmu_l2_base[MAX_XLAT_TABLES][NUM_4K_IN_1MB] 144c9fe6fedSEtienne Carriere __aligned(MMU32B_L2_TABLE_ALIGN) __attribute__((section("xlat_table"))); 145c9fe6fedSEtienne Carriere 146c9fe6fedSEtienne Carriere /* 147c9fe6fedSEtienne Carriere * Array of all memory regions stored in order of ascending base address. 148c9fe6fedSEtienne Carriere * The list is terminated by the first entry with size == 0. 149c9fe6fedSEtienne Carriere */ 150c9fe6fedSEtienne Carriere static mmap_region_t mmap[MAX_MMAP_REGIONS + 1]; 151c9fe6fedSEtienne Carriere 152c9fe6fedSEtienne Carriere void print_mmap(void) 153c9fe6fedSEtienne Carriere { 154c9fe6fedSEtienne Carriere #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 155c9fe6fedSEtienne Carriere mmap_region_t *mm = mmap; 156c9fe6fedSEtienne Carriere 157c9fe6fedSEtienne Carriere printf("init xlat - l1:%p l2:%p (%d)\n", 158c9fe6fedSEtienne Carriere (void *)mmu_l1_base, (void *)mmu_l2_base, MAX_XLAT_TABLES); 159c9fe6fedSEtienne Carriere printf("mmap:\n"); 160c9fe6fedSEtienne Carriere while (mm->size) { 161c9fe6fedSEtienne Carriere printf(" VA:%p PA:0x%llx size:0x%zx attr:0x%x\n", 162c9fe6fedSEtienne Carriere (void *)mm->base_va, mm->base_pa, 163c9fe6fedSEtienne Carriere mm->size, mm->attr); 164c9fe6fedSEtienne Carriere ++mm; 165c9fe6fedSEtienne Carriere }; 166c9fe6fedSEtienne Carriere printf("\n"); 167c9fe6fedSEtienne Carriere #endif 168c9fe6fedSEtienne Carriere } 169c9fe6fedSEtienne Carriere 170c9fe6fedSEtienne Carriere void mmap_add(const mmap_region_t *mm) 171c9fe6fedSEtienne Carriere { 172c9fe6fedSEtienne Carriere const mmap_region_t *mm_cursor = mm; 173c9fe6fedSEtienne Carriere 174c9fe6fedSEtienne Carriere while ((mm_cursor->size != 0U) || (mm_cursor->attr != 0U)) { 175c9fe6fedSEtienne Carriere mmap_add_region(mm_cursor->base_pa, mm_cursor->base_va, 176c9fe6fedSEtienne Carriere mm_cursor->size, mm_cursor->attr); 177c9fe6fedSEtienne Carriere mm_cursor++; 178c9fe6fedSEtienne Carriere } 179c9fe6fedSEtienne Carriere } 180c9fe6fedSEtienne Carriere 181c9fe6fedSEtienne Carriere void mmap_add_region(unsigned long long base_pa, uintptr_t base_va, 182c9fe6fedSEtienne Carriere size_t size, unsigned int attr) 183c9fe6fedSEtienne Carriere { 184c9fe6fedSEtienne Carriere mmap_region_t *mm = mmap; 185c9fe6fedSEtienne Carriere const mmap_region_t *mm_last = mm + ARRAY_SIZE(mmap) - 1U; 186c9fe6fedSEtienne Carriere unsigned long long end_pa = base_pa + size - 1U; 187c9fe6fedSEtienne Carriere uintptr_t end_va = base_va + size - 1U; 188c9fe6fedSEtienne Carriere 189c9fe6fedSEtienne Carriere assert(IS_PAGE_ALIGNED(base_pa)); 190c9fe6fedSEtienne Carriere assert(IS_PAGE_ALIGNED(base_va)); 191c9fe6fedSEtienne Carriere assert(IS_PAGE_ALIGNED(size)); 192c9fe6fedSEtienne Carriere 193c9fe6fedSEtienne Carriere if (size == 0U) 194c9fe6fedSEtienne Carriere return; 195c9fe6fedSEtienne Carriere 196c9fe6fedSEtienne Carriere assert(base_pa < end_pa); /* Check for overflows */ 197c9fe6fedSEtienne Carriere assert(base_va < end_va); 198c9fe6fedSEtienne Carriere 199c9fe6fedSEtienne Carriere assert((base_va + (uintptr_t)size - (uintptr_t)1) <= 200c9fe6fedSEtienne Carriere (PLAT_VIRT_ADDR_SPACE_SIZE - 1U)); 201c9fe6fedSEtienne Carriere assert((base_pa + (unsigned long long)size - 1ULL) <= 202c9fe6fedSEtienne Carriere (PLAT_PHY_ADDR_SPACE_SIZE - 1U)); 203c9fe6fedSEtienne Carriere 204c9fe6fedSEtienne Carriere #if ENABLE_ASSERTIONS 205c9fe6fedSEtienne Carriere 206c9fe6fedSEtienne Carriere /* Check for PAs and VAs overlaps with all other regions */ 207c9fe6fedSEtienne Carriere for (mm = mmap; mm->size; ++mm) { 208c9fe6fedSEtienne Carriere 209c9fe6fedSEtienne Carriere uintptr_t mm_end_va = mm->base_va + mm->size - 1U; 210c9fe6fedSEtienne Carriere 211c9fe6fedSEtienne Carriere /* 212c9fe6fedSEtienne Carriere * Check if one of the regions is completely inside the other 213c9fe6fedSEtienne Carriere * one. 214c9fe6fedSEtienne Carriere */ 215c9fe6fedSEtienne Carriere bool fully_overlapped_va = 216c9fe6fedSEtienne Carriere ((base_va >= mm->base_va) && (end_va <= mm_end_va)) || 217c9fe6fedSEtienne Carriere ((mm->base_va >= base_va) && (mm_end_va <= end_va)); 218c9fe6fedSEtienne Carriere 219c9fe6fedSEtienne Carriere /* 220c9fe6fedSEtienne Carriere * Full VA overlaps are only allowed if both regions are 221c9fe6fedSEtienne Carriere * identity mapped (zero offset) or have the same VA to PA 222c9fe6fedSEtienne Carriere * offset. Also, make sure that it's not the exact same area. 223c9fe6fedSEtienne Carriere */ 224c9fe6fedSEtienne Carriere if (fully_overlapped_va) { 225c9fe6fedSEtienne Carriere assert((mm->base_va - mm->base_pa) == 226c9fe6fedSEtienne Carriere (base_va - base_pa)); 227c9fe6fedSEtienne Carriere assert((base_va != mm->base_va) || (size != mm->size)); 228c9fe6fedSEtienne Carriere } else { 229c9fe6fedSEtienne Carriere /* 230c9fe6fedSEtienne Carriere * If the regions do not have fully overlapping VAs, 231c9fe6fedSEtienne Carriere * then they must have fully separated VAs and PAs. 232c9fe6fedSEtienne Carriere * Partial overlaps are not allowed 233c9fe6fedSEtienne Carriere */ 234c9fe6fedSEtienne Carriere 235c9fe6fedSEtienne Carriere unsigned long long mm_end_pa = 236c9fe6fedSEtienne Carriere mm->base_pa + mm->size - 1; 237c9fe6fedSEtienne Carriere 238c9fe6fedSEtienne Carriere bool separated_pa = (end_pa < mm->base_pa) || 239c9fe6fedSEtienne Carriere (base_pa > mm_end_pa); 240c9fe6fedSEtienne Carriere bool separated_va = (end_va < mm->base_va) || 241c9fe6fedSEtienne Carriere (base_va > mm_end_va); 242c9fe6fedSEtienne Carriere 243c9fe6fedSEtienne Carriere assert(separated_va && separated_pa); 244c9fe6fedSEtienne Carriere } 245c9fe6fedSEtienne Carriere } 246c9fe6fedSEtienne Carriere 247c9fe6fedSEtienne Carriere mm = mmap; /* Restore pointer to the start of the array */ 248c9fe6fedSEtienne Carriere 249c9fe6fedSEtienne Carriere #endif /* ENABLE_ASSERTIONS */ 250c9fe6fedSEtienne Carriere 251c9fe6fedSEtienne Carriere /* Find correct place in mmap to insert new region */ 252c9fe6fedSEtienne Carriere while ((mm->base_va < base_va) && (mm->size != 0U)) 253c9fe6fedSEtienne Carriere ++mm; 254c9fe6fedSEtienne Carriere 255c9fe6fedSEtienne Carriere /* 256c9fe6fedSEtienne Carriere * If a section is contained inside another one with the same base 257c9fe6fedSEtienne Carriere * address, it must be placed after the one it is contained in: 258c9fe6fedSEtienne Carriere * 259c9fe6fedSEtienne Carriere * 1st |-----------------------| 260c9fe6fedSEtienne Carriere * 2nd |------------| 261c9fe6fedSEtienne Carriere * 3rd |------| 262c9fe6fedSEtienne Carriere * 263c9fe6fedSEtienne Carriere * This is required for mmap_region_attr() to get the attributes of the 264c9fe6fedSEtienne Carriere * small region correctly. 265c9fe6fedSEtienne Carriere */ 266c9fe6fedSEtienne Carriere while ((mm->base_va == base_va) && (mm->size > size)) 267c9fe6fedSEtienne Carriere ++mm; 268c9fe6fedSEtienne Carriere 269c9fe6fedSEtienne Carriere /* Make room for new region by moving other regions up by one place */ 270c9fe6fedSEtienne Carriere (void)memmove(mm + 1, mm, (uintptr_t)mm_last - (uintptr_t)mm); 271c9fe6fedSEtienne Carriere 272c9fe6fedSEtienne Carriere /* Check we haven't lost the empty sentinal from the end of the array */ 273c9fe6fedSEtienne Carriere assert(mm_last->size == 0U); 274c9fe6fedSEtienne Carriere 275c9fe6fedSEtienne Carriere mm->base_pa = base_pa; 276c9fe6fedSEtienne Carriere mm->base_va = base_va; 277c9fe6fedSEtienne Carriere mm->size = size; 278c9fe6fedSEtienne Carriere mm->attr = attr; 279c9fe6fedSEtienne Carriere 280c9fe6fedSEtienne Carriere if (end_pa > xlat_max_pa) 281c9fe6fedSEtienne Carriere xlat_max_pa = end_pa; 282c9fe6fedSEtienne Carriere if (end_va > xlat_max_va) 283c9fe6fedSEtienne Carriere xlat_max_va = end_va; 284c9fe6fedSEtienne Carriere } 285c9fe6fedSEtienne Carriere 286c9fe6fedSEtienne Carriere /* map all memory as shared/global/domain0/no-usr access */ 287c9fe6fedSEtienne Carriere static unsigned long mmap_desc(unsigned attr, unsigned long addr_pa, 288c9fe6fedSEtienne Carriere unsigned int level) 289c9fe6fedSEtienne Carriere { 290c9fe6fedSEtienne Carriere unsigned long desc; 291c9fe6fedSEtienne Carriere 292c9fe6fedSEtienne Carriere switch (level) { 293c9fe6fedSEtienne Carriere case 1: 294c9fe6fedSEtienne Carriere assert(!(addr_pa & (MMU32B_L1_TABLE_ALIGN - 1))); 295c9fe6fedSEtienne Carriere 296c9fe6fedSEtienne Carriere desc = SECTION_SECTION | SECTION_SHARED; 297c9fe6fedSEtienne Carriere 298c9fe6fedSEtienne Carriere desc |= attr & MT_NS ? SECTION_NOTSECURE : 0; 299c9fe6fedSEtienne Carriere 300c9fe6fedSEtienne Carriere desc |= SECTION_ACCESS_FLAG; 301c9fe6fedSEtienne Carriere desc |= attr & MT_RW ? 0 : SECTION_RO; 302c9fe6fedSEtienne Carriere 303c9fe6fedSEtienne Carriere desc |= attr & MT_MEMORY ? 304c9fe6fedSEtienne Carriere SECTION_NORMAL_CACHED : SECTION_DEVICE; 305c9fe6fedSEtienne Carriere 306c9fe6fedSEtienne Carriere if ((attr & MT_RW) || !(attr & MT_MEMORY)) 307c9fe6fedSEtienne Carriere desc |= SECTION_XN; 308c9fe6fedSEtienne Carriere break; 309c9fe6fedSEtienne Carriere case 2: 310c9fe6fedSEtienne Carriere assert(!(addr_pa & (MMU32B_L2_TABLE_ALIGN - 1))); 311c9fe6fedSEtienne Carriere 312c9fe6fedSEtienne Carriere desc = SMALL_PAGE_SMALL_PAGE | SMALL_PAGE_SHARED; 313c9fe6fedSEtienne Carriere 314c9fe6fedSEtienne Carriere desc |= SMALL_PAGE_ACCESS_FLAG; 315c9fe6fedSEtienne Carriere desc |= attr & MT_RW ? 0 : SMALL_PAGE_RO; 316c9fe6fedSEtienne Carriere 317c9fe6fedSEtienne Carriere desc |= attr & MT_MEMORY ? 318c9fe6fedSEtienne Carriere SMALL_PAGE_NORMAL_CACHED : SMALL_PAGE_DEVICE; 319c9fe6fedSEtienne Carriere 320c9fe6fedSEtienne Carriere if ((attr & MT_RW) || !(attr & MT_MEMORY)) 321c9fe6fedSEtienne Carriere desc |= SMALL_PAGE_XN; 322c9fe6fedSEtienne Carriere break; 323c9fe6fedSEtienne Carriere default: 324c9fe6fedSEtienne Carriere panic(); 325c9fe6fedSEtienne Carriere } 326c9fe6fedSEtienne Carriere #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 327c9fe6fedSEtienne Carriere /* dump only the non-lpae level 2 tables */ 328c9fe6fedSEtienne Carriere if (level == 2) { 329c9fe6fedSEtienne Carriere printf(attr & MT_MEMORY ? "MEM" : "dev"); 330c9fe6fedSEtienne Carriere printf(attr & MT_RW ? "-rw" : "-RO"); 331c9fe6fedSEtienne Carriere printf(attr & MT_NS ? "-NS" : "-S"); 332c9fe6fedSEtienne Carriere } 333c9fe6fedSEtienne Carriere #endif 334c9fe6fedSEtienne Carriere return desc | addr_pa; 335c9fe6fedSEtienne Carriere } 336c9fe6fedSEtienne Carriere 337c9fe6fedSEtienne Carriere static unsigned int mmap_region_attr(const mmap_region_t *mm, uintptr_t base_va, 338c9fe6fedSEtienne Carriere size_t size, unsigned int *attr) 339c9fe6fedSEtienne Carriere { 340c9fe6fedSEtienne Carriere /* Don't assume that the area is contained in the first region */ 341c9fe6fedSEtienne Carriere unsigned int ret = MT_UNKNOWN; 342c9fe6fedSEtienne Carriere 343c9fe6fedSEtienne Carriere /* 344c9fe6fedSEtienne Carriere * Get attributes from last (innermost) region that contains the 345c9fe6fedSEtienne Carriere * requested area. Don't stop as soon as one region doesn't contain it 346c9fe6fedSEtienne Carriere * because there may be other internal regions that contain this area: 347c9fe6fedSEtienne Carriere * 348c9fe6fedSEtienne Carriere * |-----------------------------1-----------------------------| 349c9fe6fedSEtienne Carriere * |----2----| |-------3-------| |----5----| 350c9fe6fedSEtienne Carriere * |--4--| 351c9fe6fedSEtienne Carriere * 352c9fe6fedSEtienne Carriere * |---| <- Area we want the attributes of. 353c9fe6fedSEtienne Carriere * 354c9fe6fedSEtienne Carriere * In this example, the area is contained in regions 1, 3 and 4 but not 355c9fe6fedSEtienne Carriere * in region 2. The loop shouldn't stop at region 2 as inner regions 356c9fe6fedSEtienne Carriere * have priority over outer regions, it should stop at region 5. 357c9fe6fedSEtienne Carriere */ 358c9fe6fedSEtienne Carriere for ( ; ; ++mm) { 359c9fe6fedSEtienne Carriere 360c9fe6fedSEtienne Carriere if (mm->size == 0U) 361c9fe6fedSEtienne Carriere return ret; /* Reached end of list */ 362c9fe6fedSEtienne Carriere 363c9fe6fedSEtienne Carriere if (mm->base_va > (base_va + size - 1U)) 364c9fe6fedSEtienne Carriere return ret; /* Next region is after area so end */ 365c9fe6fedSEtienne Carriere 366c9fe6fedSEtienne Carriere if ((mm->base_va + mm->size - 1U) < base_va) 367c9fe6fedSEtienne Carriere continue; /* Next region has already been overtaken */ 368c9fe6fedSEtienne Carriere 369c9fe6fedSEtienne Carriere if ((ret == 0U) && (mm->attr == *attr)) 370c9fe6fedSEtienne Carriere continue; /* Region doesn't override attribs so skip */ 371c9fe6fedSEtienne Carriere 372c9fe6fedSEtienne Carriere if ((mm->base_va > base_va) || 373c9fe6fedSEtienne Carriere ((mm->base_va + mm->size - 1U) < (base_va + size - 1U))) 374c9fe6fedSEtienne Carriere return MT_UNKNOWN; /* Region doesn't fully cover area */ 375c9fe6fedSEtienne Carriere 376c9fe6fedSEtienne Carriere *attr = mm->attr; 377c9fe6fedSEtienne Carriere ret = 0U; 378c9fe6fedSEtienne Carriere } 379c9fe6fedSEtienne Carriere return ret; 380c9fe6fedSEtienne Carriere } 381c9fe6fedSEtienne Carriere 382c9fe6fedSEtienne Carriere static mmap_region_t *init_xlation_table_inner(mmap_region_t *mm, 383c9fe6fedSEtienne Carriere unsigned long base_va, 384c9fe6fedSEtienne Carriere unsigned long *table, 385c9fe6fedSEtienne Carriere unsigned int level) 386c9fe6fedSEtienne Carriere { 387c9fe6fedSEtienne Carriere unsigned int level_size_shift = (level == 1) ? 388c9fe6fedSEtienne Carriere ONE_MB_SHIFT : FOUR_KB_SHIFT; 389c9fe6fedSEtienne Carriere unsigned int level_size = 1 << level_size_shift; 390c9fe6fedSEtienne Carriere unsigned long level_index_mask = (level == 1) ? 391c9fe6fedSEtienne Carriere (NUM_1MB_IN_4GB - 1) << ONE_MB_SHIFT : 392c9fe6fedSEtienne Carriere (NUM_4K_IN_1MB - 1) << FOUR_KB_SHIFT; 393c9fe6fedSEtienne Carriere 394c9fe6fedSEtienne Carriere assert(level == 1 || level == 2); 395c9fe6fedSEtienne Carriere 396c9fe6fedSEtienne Carriere VERBOSE("init xlat table at %p (level%1d)\n", (void *)table, level); 397c9fe6fedSEtienne Carriere 398c9fe6fedSEtienne Carriere do { 399c9fe6fedSEtienne Carriere unsigned long desc = MMU32B_UNSET_DESC; 400c9fe6fedSEtienne Carriere 401c9fe6fedSEtienne Carriere if (mm->base_va + mm->size <= base_va) { 402c9fe6fedSEtienne Carriere /* Area now after the region so skip it */ 403c9fe6fedSEtienne Carriere ++mm; 404c9fe6fedSEtienne Carriere continue; 405c9fe6fedSEtienne Carriere } 406c9fe6fedSEtienne Carriere #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 407c9fe6fedSEtienne Carriere /* dump only non-lpae level 2 tables content */ 408c9fe6fedSEtienne Carriere if (level == 2) 409c9fe6fedSEtienne Carriere printf(" 0x%lx %x " + 6 - 2 * level, 410c9fe6fedSEtienne Carriere base_va, level_size); 411c9fe6fedSEtienne Carriere #endif 412c9fe6fedSEtienne Carriere if (mm->base_va >= base_va + level_size) { 413c9fe6fedSEtienne Carriere /* Next region is after area so nothing to map yet */ 414c9fe6fedSEtienne Carriere desc = MMU32B_INVALID_DESC; 415c9fe6fedSEtienne Carriere } else if (mm->base_va <= base_va && mm->base_va + mm->size >= 416c9fe6fedSEtienne Carriere base_va + level_size) { 417c9fe6fedSEtienne Carriere /* Next region covers all of area */ 418c9fe6fedSEtienne Carriere unsigned int attr = mm->attr; 419c9fe6fedSEtienne Carriere unsigned int r = mmap_region_attr(mm, base_va, 420c9fe6fedSEtienne Carriere level_size, &attr); 421c9fe6fedSEtienne Carriere 422c9fe6fedSEtienne Carriere if (r == 0U) { 423c9fe6fedSEtienne Carriere desc = mmap_desc(attr, 424c9fe6fedSEtienne Carriere base_va - mm->base_va + mm->base_pa, 425c9fe6fedSEtienne Carriere level); 426c9fe6fedSEtienne Carriere } 427c9fe6fedSEtienne Carriere } 428c9fe6fedSEtienne Carriere 429c9fe6fedSEtienne Carriere if (desc == MMU32B_UNSET_DESC) { 430c9fe6fedSEtienne Carriere unsigned long xlat_table; 431c9fe6fedSEtienne Carriere 432c9fe6fedSEtienne Carriere /* 433c9fe6fedSEtienne Carriere * Area not covered by a region so need finer table 434c9fe6fedSEtienne Carriere * Reuse next level table if any (assert attrib matching). 435c9fe6fedSEtienne Carriere * Otherwise allocate a xlat table. 436c9fe6fedSEtienne Carriere */ 437c9fe6fedSEtienne Carriere if (*table) { 438c9fe6fedSEtienne Carriere assert((*table & 3) == SECTION_PT_PT); 439c9fe6fedSEtienne Carriere assert(!(*table & SECTION_PT_NOTSECURE) == 440c9fe6fedSEtienne Carriere !(mm->attr & MT_NS)); 441c9fe6fedSEtienne Carriere 442c9fe6fedSEtienne Carriere xlat_table = (*table) & 443c9fe6fedSEtienne Carriere ~(MMU32B_L1_TABLE_ALIGN - 1); 444c9fe6fedSEtienne Carriere desc = *table; 445c9fe6fedSEtienne Carriere } else { 446c9fe6fedSEtienne Carriere xlat_table = (unsigned long)mmu_l2_base + 447c9fe6fedSEtienne Carriere next_xlat * MMU32B_L2_TABLE_SIZE; 448*4249e8b9SJustin Chadwell next_xlat++; 449*4249e8b9SJustin Chadwell assert(next_xlat <= MAX_XLAT_TABLES); 450c9fe6fedSEtienne Carriere memset((char *)xlat_table, 0, 451c9fe6fedSEtienne Carriere MMU32B_L2_TABLE_SIZE); 452c9fe6fedSEtienne Carriere 453c9fe6fedSEtienne Carriere desc = xlat_table | SECTION_PT_PT; 454c9fe6fedSEtienne Carriere desc |= mm->attr & MT_NS ? 455c9fe6fedSEtienne Carriere SECTION_PT_NOTSECURE : 0; 456c9fe6fedSEtienne Carriere } 457c9fe6fedSEtienne Carriere /* Recurse to fill in new table */ 458c9fe6fedSEtienne Carriere mm = init_xlation_table_inner(mm, base_va, 459c9fe6fedSEtienne Carriere (unsigned long *)xlat_table, 460c9fe6fedSEtienne Carriere level + 1); 461c9fe6fedSEtienne Carriere } 462c9fe6fedSEtienne Carriere #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 463c9fe6fedSEtienne Carriere /* dump only non-lpae level 2 tables content */ 464c9fe6fedSEtienne Carriere if (level == 2) 465c9fe6fedSEtienne Carriere printf("\n"); 466c9fe6fedSEtienne Carriere #endif 467c9fe6fedSEtienne Carriere *table++ = desc; 468c9fe6fedSEtienne Carriere base_va += level_size; 469c9fe6fedSEtienne Carriere } while (mm->size && (base_va & level_index_mask)); 470c9fe6fedSEtienne Carriere 471c9fe6fedSEtienne Carriere return mm; 472c9fe6fedSEtienne Carriere } 473c9fe6fedSEtienne Carriere 474c9fe6fedSEtienne Carriere void init_xlat_tables(void) 475c9fe6fedSEtienne Carriere { 476c9fe6fedSEtienne Carriere print_mmap(); 477c9fe6fedSEtienne Carriere 478c9fe6fedSEtienne Carriere assert(!((unsigned int)mmu_l1_base & (MMU32B_L1_TABLE_ALIGN - 1))); 479c9fe6fedSEtienne Carriere assert(!((unsigned int)mmu_l2_base & (MMU32B_L2_TABLE_ALIGN - 1))); 480c9fe6fedSEtienne Carriere 481c9fe6fedSEtienne Carriere memset(mmu_l1_base, 0, MMU32B_L1_TABLE_SIZE); 482c9fe6fedSEtienne Carriere 483c9fe6fedSEtienne Carriere init_xlation_table_inner(mmap, 0, (unsigned long *)mmu_l1_base, 1); 484c9fe6fedSEtienne Carriere 485c9fe6fedSEtienne Carriere VERBOSE("init xlat - max_va=%p, max_pa=%llx\n", 486c9fe6fedSEtienne Carriere (void *)xlat_max_va, xlat_max_pa); 487c9fe6fedSEtienne Carriere assert(xlat_max_va <= PLAT_VIRT_ADDR_SPACE_SIZE - 1); 488c9fe6fedSEtienne Carriere assert(xlat_max_pa <= PLAT_VIRT_ADDR_SPACE_SIZE - 1); 489c9fe6fedSEtienne Carriere } 490c9fe6fedSEtienne Carriere 491c9fe6fedSEtienne Carriere /******************************************************************************* 492c9fe6fedSEtienne Carriere * Function for enabling the MMU in Secure PL1, assuming that the 493c9fe6fedSEtienne Carriere * page-tables have already been created. 494c9fe6fedSEtienne Carriere ******************************************************************************/ 495c9fe6fedSEtienne Carriere void enable_mmu_svc_mon(unsigned int flags) 496c9fe6fedSEtienne Carriere { 497c9fe6fedSEtienne Carriere unsigned int prrr; 498c9fe6fedSEtienne Carriere unsigned int nmrr; 499c9fe6fedSEtienne Carriere unsigned int sctlr; 500c9fe6fedSEtienne Carriere 501c9fe6fedSEtienne Carriere assert(IS_IN_SECURE()); 502c9fe6fedSEtienne Carriere assert((read_sctlr() & SCTLR_M_BIT) == 0); 503c9fe6fedSEtienne Carriere 504c9fe6fedSEtienne Carriere /* Enable Access flag (simplified access permissions) and TEX remap */ 505c9fe6fedSEtienne Carriere write_sctlr(read_sctlr() | SCTLR_AFE_BIT | SCTLR_TRE_BIT); 506c9fe6fedSEtienne Carriere 507c9fe6fedSEtienne Carriere prrr = MMU32B_PRRR_IDX(MMU32B_ATTR_DEVICE_INDEX, 1, 0) \ 508c9fe6fedSEtienne Carriere | MMU32B_PRRR_IDX(MMU32B_ATTR_IWBWA_OWBWA_INDEX, 2, 1); 509c9fe6fedSEtienne Carriere nmrr = MMU32B_NMRR_IDX(MMU32B_ATTR_DEVICE_INDEX, 0, 0) \ 510c9fe6fedSEtienne Carriere | MMU32B_NMRR_IDX(MMU32B_ATTR_IWBWA_OWBWA_INDEX, 1, 1); 511c9fe6fedSEtienne Carriere 512c9fe6fedSEtienne Carriere prrr |= MMU32B_PRRR_NS1 | MMU32B_PRRR_DS1; 513c9fe6fedSEtienne Carriere 514c9fe6fedSEtienne Carriere write_prrr(prrr); 515c9fe6fedSEtienne Carriere write_nmrr(nmrr); 516c9fe6fedSEtienne Carriere 517c9fe6fedSEtienne Carriere /* Program Domain access control register: domain 0 only */ 518c9fe6fedSEtienne Carriere write_dacr(DACR_DOMAIN(0, DACR_DOMAIN_PERM_CLIENT)); 519c9fe6fedSEtienne Carriere 520c9fe6fedSEtienne Carriere /* Invalidate TLBs at the current exception level */ 521c9fe6fedSEtienne Carriere tlbiall(); 522c9fe6fedSEtienne Carriere 523c9fe6fedSEtienne Carriere /* set MMU base xlat table entry (use only TTBR0) */ 524c9fe6fedSEtienne Carriere write_ttbr0((uint32_t)mmu_l1_base | MMU32B_DEFAULT_ATTRS); 525c9fe6fedSEtienne Carriere write_ttbr1(0); 526c9fe6fedSEtienne Carriere 527c9fe6fedSEtienne Carriere /* 528c9fe6fedSEtienne Carriere * Ensure all translation table writes have drained 529c9fe6fedSEtienne Carriere * into memory, the TLB invalidation is complete, 530c9fe6fedSEtienne Carriere * and translation register writes are committed 531c9fe6fedSEtienne Carriere * before enabling the MMU 532c9fe6fedSEtienne Carriere */ 533c9fe6fedSEtienne Carriere dsb(); 534c9fe6fedSEtienne Carriere isb(); 535c9fe6fedSEtienne Carriere 536c9fe6fedSEtienne Carriere sctlr = read_sctlr(); 537c9fe6fedSEtienne Carriere sctlr |= SCTLR_M_BIT; 538c9fe6fedSEtienne Carriere #if ARMV7_SUPPORTS_VIRTUALIZATION 539c9fe6fedSEtienne Carriere sctlr |= SCTLR_WXN_BIT; 540c9fe6fedSEtienne Carriere #endif 541c9fe6fedSEtienne Carriere 542c9fe6fedSEtienne Carriere if (flags & DISABLE_DCACHE) 543c9fe6fedSEtienne Carriere sctlr &= ~SCTLR_C_BIT; 544c9fe6fedSEtienne Carriere else 545c9fe6fedSEtienne Carriere sctlr |= SCTLR_C_BIT; 546c9fe6fedSEtienne Carriere 547c9fe6fedSEtienne Carriere write_sctlr(sctlr); 548c9fe6fedSEtienne Carriere 549c9fe6fedSEtienne Carriere /* Ensure the MMU enable takes effect immediately */ 550c9fe6fedSEtienne Carriere isb(); 551c9fe6fedSEtienne Carriere } 552