17bb01fb2SAntonio Nino Diaz /* 27bb01fb2SAntonio Nino Diaz * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. 37bb01fb2SAntonio Nino Diaz * 47bb01fb2SAntonio Nino Diaz * Redistribution and use in source and binary forms, with or without 57bb01fb2SAntonio Nino Diaz * modification, are permitted provided that the following conditions are met: 67bb01fb2SAntonio Nino Diaz * 77bb01fb2SAntonio Nino Diaz * Redistributions of source code must retain the above copyright notice, this 87bb01fb2SAntonio Nino Diaz * list of conditions and the following disclaimer. 97bb01fb2SAntonio Nino Diaz * 107bb01fb2SAntonio Nino Diaz * Redistributions in binary form must reproduce the above copyright notice, 117bb01fb2SAntonio Nino Diaz * this list of conditions and the following disclaimer in the documentation 127bb01fb2SAntonio Nino Diaz * and/or other materials provided with the distribution. 137bb01fb2SAntonio Nino Diaz * 147bb01fb2SAntonio Nino Diaz * Neither the name of ARM nor the names of its contributors may be used 157bb01fb2SAntonio Nino Diaz * to endorse or promote products derived from this software without specific 167bb01fb2SAntonio Nino Diaz * prior written permission. 177bb01fb2SAntonio Nino Diaz * 187bb01fb2SAntonio Nino Diaz * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 197bb01fb2SAntonio Nino Diaz * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 207bb01fb2SAntonio Nino Diaz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 217bb01fb2SAntonio Nino Diaz * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 227bb01fb2SAntonio Nino Diaz * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 237bb01fb2SAntonio Nino Diaz * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 247bb01fb2SAntonio Nino Diaz * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 257bb01fb2SAntonio Nino Diaz * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 267bb01fb2SAntonio Nino Diaz * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 277bb01fb2SAntonio Nino Diaz * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 287bb01fb2SAntonio Nino Diaz * POSSIBILITY OF SUCH DAMAGE. 297bb01fb2SAntonio Nino Diaz */ 307bb01fb2SAntonio Nino Diaz 317bb01fb2SAntonio Nino Diaz #ifndef __XLAT_TABLES_PRIVATE_H__ 327bb01fb2SAntonio Nino Diaz #define __XLAT_TABLES_PRIVATE_H__ 337bb01fb2SAntonio Nino Diaz 347bb01fb2SAntonio Nino Diaz #include <cassert.h> 357bb01fb2SAntonio Nino Diaz #include <platform_def.h> 367bb01fb2SAntonio Nino Diaz #include <utils.h> 377bb01fb2SAntonio Nino Diaz 387bb01fb2SAntonio Nino Diaz /* 397bb01fb2SAntonio Nino Diaz * If the platform hasn't defined a physical and a virtual address space size 407bb01fb2SAntonio Nino Diaz * default to ADDR_SPACE_SIZE. 417bb01fb2SAntonio Nino Diaz */ 427bb01fb2SAntonio Nino Diaz #if ERROR_DEPRECATED 437bb01fb2SAntonio Nino Diaz # ifdef ADDR_SPACE_SIZE 447bb01fb2SAntonio Nino Diaz # error "ADDR_SPACE_SIZE is deprecated. Use PLAT_xxx_ADDR_SPACE_SIZE instead." 457bb01fb2SAntonio Nino Diaz # endif 467bb01fb2SAntonio Nino Diaz #elif defined(ADDR_SPACE_SIZE) 477bb01fb2SAntonio Nino Diaz # ifndef PLAT_PHY_ADDR_SPACE_SIZE 487bb01fb2SAntonio Nino Diaz # define PLAT_PHY_ADDR_SPACE_SIZE ADDR_SPACE_SIZE 497bb01fb2SAntonio Nino Diaz # endif 507bb01fb2SAntonio Nino Diaz # ifndef PLAT_VIRT_ADDR_SPACE_SIZE 517bb01fb2SAntonio Nino Diaz # define PLAT_VIRT_ADDR_SPACE_SIZE ADDR_SPACE_SIZE 527bb01fb2SAntonio Nino Diaz # endif 537bb01fb2SAntonio Nino Diaz #endif 547bb01fb2SAntonio Nino Diaz 557bb01fb2SAntonio Nino Diaz /* The virtual and physical address space sizes must be powers of two. */ 567bb01fb2SAntonio Nino Diaz CASSERT(IS_POWER_OF_TWO(PLAT_VIRT_ADDR_SPACE_SIZE), 577bb01fb2SAntonio Nino Diaz assert_valid_virt_addr_space_size); 587bb01fb2SAntonio Nino Diaz CASSERT(IS_POWER_OF_TWO(PLAT_PHY_ADDR_SPACE_SIZE), 597bb01fb2SAntonio Nino Diaz assert_valid_phy_addr_space_size); 607bb01fb2SAntonio Nino Diaz 617bb01fb2SAntonio Nino Diaz /* Struct that holds all information about the translation tables. */ 627bb01fb2SAntonio Nino Diaz typedef struct { 637bb01fb2SAntonio Nino Diaz 647bb01fb2SAntonio Nino Diaz /* 657bb01fb2SAntonio Nino Diaz * Max allowed Virtual and Physical Addresses. 667bb01fb2SAntonio Nino Diaz */ 677bb01fb2SAntonio Nino Diaz unsigned long long pa_max_address; 687bb01fb2SAntonio Nino Diaz uintptr_t va_max_address; 697bb01fb2SAntonio Nino Diaz 707bb01fb2SAntonio Nino Diaz /* 717bb01fb2SAntonio Nino Diaz * Array of all memory regions stored in order of ascending end address 727bb01fb2SAntonio Nino Diaz * and ascending size to simplify the code that allows overlapping 737bb01fb2SAntonio Nino Diaz * regions. The list is terminated by the first entry with size == 0. 747bb01fb2SAntonio Nino Diaz */ 757bb01fb2SAntonio Nino Diaz mmap_region_t *mmap; /* mmap_num + 1 elements */ 767bb01fb2SAntonio Nino Diaz int mmap_num; 777bb01fb2SAntonio Nino Diaz 787bb01fb2SAntonio Nino Diaz /* 797bb01fb2SAntonio Nino Diaz * Array of finer-grain translation tables. 807bb01fb2SAntonio Nino Diaz * For example, if the initial lookup level is 1 then this array would 817bb01fb2SAntonio Nino Diaz * contain both level-2 and level-3 entries. 827bb01fb2SAntonio Nino Diaz */ 837bb01fb2SAntonio Nino Diaz uint64_t (*tables)[XLAT_TABLE_ENTRIES]; 847bb01fb2SAntonio Nino Diaz int tables_num; 85*0b64f4efSAntonio Nino Diaz /* 86*0b64f4efSAntonio Nino Diaz * Keep track of how many regions are mapped in each table. The base 87*0b64f4efSAntonio Nino Diaz * table can't be unmapped so it isn't needed to keep track of it. 88*0b64f4efSAntonio Nino Diaz */ 89*0b64f4efSAntonio Nino Diaz #if PLAT_XLAT_TABLES_DYNAMIC 90*0b64f4efSAntonio Nino Diaz int *tables_mapped_regions; 91*0b64f4efSAntonio Nino Diaz #endif /* PLAT_XLAT_TABLES_DYNAMIC */ 927bb01fb2SAntonio Nino Diaz 937bb01fb2SAntonio Nino Diaz int next_table; 947bb01fb2SAntonio Nino Diaz 957bb01fb2SAntonio Nino Diaz /* 967bb01fb2SAntonio Nino Diaz * Base translation table. It doesn't need to have the same amount of 977bb01fb2SAntonio Nino Diaz * entries as the ones used for other levels. 987bb01fb2SAntonio Nino Diaz */ 997bb01fb2SAntonio Nino Diaz uint64_t *base_table; 1007bb01fb2SAntonio Nino Diaz int base_table_entries; 1017bb01fb2SAntonio Nino Diaz 1027bb01fb2SAntonio Nino Diaz unsigned long long max_pa; 1037bb01fb2SAntonio Nino Diaz uintptr_t max_va; 1047bb01fb2SAntonio Nino Diaz 1057bb01fb2SAntonio Nino Diaz /* Level of the base translation table. */ 1067bb01fb2SAntonio Nino Diaz int base_level; 1077bb01fb2SAntonio Nino Diaz 1087bb01fb2SAntonio Nino Diaz /* Set to 1 when the translation tables are initialized. */ 1097bb01fb2SAntonio Nino Diaz int initialized; 1107bb01fb2SAntonio Nino Diaz 1117bb01fb2SAntonio Nino Diaz } xlat_ctx_t; 1127bb01fb2SAntonio Nino Diaz 113*0b64f4efSAntonio Nino Diaz #if PLAT_XLAT_TABLES_DYNAMIC 114*0b64f4efSAntonio Nino Diaz /* 115*0b64f4efSAntonio Nino Diaz * Shifts and masks to access fields of an mmap_attr_t 116*0b64f4efSAntonio Nino Diaz */ 117*0b64f4efSAntonio Nino Diaz /* Dynamic or static */ 118*0b64f4efSAntonio Nino Diaz #define MT_DYN_SHIFT 30 /* 31 would cause undefined behaviours */ 119*0b64f4efSAntonio Nino Diaz 120*0b64f4efSAntonio Nino Diaz /* 121*0b64f4efSAntonio Nino Diaz * Memory mapping private attributes 122*0b64f4efSAntonio Nino Diaz * 123*0b64f4efSAntonio Nino Diaz * Private attributes not exposed in the mmap_attr_t enum. 124*0b64f4efSAntonio Nino Diaz */ 125*0b64f4efSAntonio Nino Diaz typedef enum { 126*0b64f4efSAntonio Nino Diaz /* 127*0b64f4efSAntonio Nino Diaz * Regions mapped before the MMU can't be unmapped dynamically (they are 128*0b64f4efSAntonio Nino Diaz * static) and regions mapped with MMU enabled can be unmapped. This 129*0b64f4efSAntonio Nino Diaz * behaviour can't be overridden. 130*0b64f4efSAntonio Nino Diaz * 131*0b64f4efSAntonio Nino Diaz * Static regions can overlap each other, dynamic regions can't. 132*0b64f4efSAntonio Nino Diaz */ 133*0b64f4efSAntonio Nino Diaz MT_STATIC = 0 << MT_DYN_SHIFT, 134*0b64f4efSAntonio Nino Diaz MT_DYNAMIC = 1 << MT_DYN_SHIFT 135*0b64f4efSAntonio Nino Diaz } mmap_priv_attr_t; 136*0b64f4efSAntonio Nino Diaz 137*0b64f4efSAntonio Nino Diaz /* 138*0b64f4efSAntonio Nino Diaz * Function used to invalidate all levels of the translation walk for a given 139*0b64f4efSAntonio Nino Diaz * virtual address. It must be called for every translation table entry that is 140*0b64f4efSAntonio Nino Diaz * modified. 141*0b64f4efSAntonio Nino Diaz */ 142*0b64f4efSAntonio Nino Diaz void xlat_arch_tlbi_va(uintptr_t va); 143*0b64f4efSAntonio Nino Diaz 144*0b64f4efSAntonio Nino Diaz /* 145*0b64f4efSAntonio Nino Diaz * This function has to be called at the end of any code that uses the function 146*0b64f4efSAntonio Nino Diaz * xlat_arch_tlbi_va(). 147*0b64f4efSAntonio Nino Diaz */ 148*0b64f4efSAntonio Nino Diaz void xlat_arch_tlbi_va_sync(void); 149*0b64f4efSAntonio Nino Diaz 150*0b64f4efSAntonio Nino Diaz /* Add a dynamic region to the specified context. */ 151*0b64f4efSAntonio Nino Diaz int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm); 152*0b64f4efSAntonio Nino Diaz 153*0b64f4efSAntonio Nino Diaz /* Remove a dynamic region from the specified context. */ 154*0b64f4efSAntonio Nino Diaz int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, uintptr_t base_va, 155*0b64f4efSAntonio Nino Diaz size_t size); 156*0b64f4efSAntonio Nino Diaz 157*0b64f4efSAntonio Nino Diaz #endif /* PLAT_XLAT_TABLES_DYNAMIC */ 158*0b64f4efSAntonio Nino Diaz 1597bb01fb2SAntonio Nino Diaz /* Print VA, PA, size and attributes of all regions in the mmap array. */ 1607bb01fb2SAntonio Nino Diaz void print_mmap(mmap_region_t *const mmap); 1617bb01fb2SAntonio Nino Diaz 1627bb01fb2SAntonio Nino Diaz /* 1637bb01fb2SAntonio Nino Diaz * Print the current state of the translation tables by reading them from 1647bb01fb2SAntonio Nino Diaz * memory. 1657bb01fb2SAntonio Nino Diaz */ 1667bb01fb2SAntonio Nino Diaz void xlat_tables_print(xlat_ctx_t *ctx); 1677bb01fb2SAntonio Nino Diaz 1687bb01fb2SAntonio Nino Diaz /* 1697bb01fb2SAntonio Nino Diaz * Initialize the translation tables by mapping all regions added to the 1707bb01fb2SAntonio Nino Diaz * specified context. 1717bb01fb2SAntonio Nino Diaz */ 1727bb01fb2SAntonio Nino Diaz void init_xlation_table(xlat_ctx_t *ctx); 1737bb01fb2SAntonio Nino Diaz 1747bb01fb2SAntonio Nino Diaz /* Add a static region to the specified context. */ 1757bb01fb2SAntonio Nino Diaz void mmap_add_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm); 1767bb01fb2SAntonio Nino Diaz 1777bb01fb2SAntonio Nino Diaz /* 1787bb01fb2SAntonio Nino Diaz * Architecture-specific initialization code. 1797bb01fb2SAntonio Nino Diaz */ 1807bb01fb2SAntonio Nino Diaz 1817bb01fb2SAntonio Nino Diaz /* Execute architecture-specific translation table initialization code. */ 1827bb01fb2SAntonio Nino Diaz void init_xlat_tables_arch(unsigned long long max_pa); 1837bb01fb2SAntonio Nino Diaz 1847bb01fb2SAntonio Nino Diaz /* Enable MMU and configure it to use the specified translation tables. */ 1857bb01fb2SAntonio Nino Diaz void enable_mmu_arch(unsigned int flags, uint64_t *base_table); 1867bb01fb2SAntonio Nino Diaz 1877bb01fb2SAntonio Nino Diaz /* Return 1 if the MMU of this Exception Level is enabled, 0 otherwise. */ 1887bb01fb2SAntonio Nino Diaz int is_mmu_enabled(void); 1897bb01fb2SAntonio Nino Diaz 1907bb01fb2SAntonio Nino Diaz #endif /* __XLAT_TABLES_PRIVATE_H__ */ 191