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> 3653d9c9c8SScott Branden #include <utils_def.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; 850b64f4efSAntonio Nino Diaz /* 860b64f4efSAntonio Nino Diaz * Keep track of how many regions are mapped in each table. The base 870b64f4efSAntonio Nino Diaz * table can't be unmapped so it isn't needed to keep track of it. 880b64f4efSAntonio Nino Diaz */ 890b64f4efSAntonio Nino Diaz #if PLAT_XLAT_TABLES_DYNAMIC 900b64f4efSAntonio Nino Diaz int *tables_mapped_regions; 910b64f4efSAntonio 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 111*a5640252SAntonio Nino Diaz /* 112*a5640252SAntonio Nino Diaz * Bit mask that has to be ORed to the rest of a translation table 113*a5640252SAntonio Nino Diaz * descriptor in order to prohibit execution of code at the exception 114*a5640252SAntonio Nino Diaz * level of this translation context. 115*a5640252SAntonio Nino Diaz */ 116*a5640252SAntonio Nino Diaz uint64_t execute_never_mask; 117*a5640252SAntonio Nino Diaz 1187bb01fb2SAntonio Nino Diaz } xlat_ctx_t; 1197bb01fb2SAntonio Nino Diaz 1200b64f4efSAntonio Nino Diaz #if PLAT_XLAT_TABLES_DYNAMIC 1210b64f4efSAntonio Nino Diaz /* 1220b64f4efSAntonio Nino Diaz * Shifts and masks to access fields of an mmap_attr_t 1230b64f4efSAntonio Nino Diaz */ 1240b64f4efSAntonio Nino Diaz /* Dynamic or static */ 1250b64f4efSAntonio Nino Diaz #define MT_DYN_SHIFT 30 /* 31 would cause undefined behaviours */ 1260b64f4efSAntonio Nino Diaz 1270b64f4efSAntonio Nino Diaz /* 1280b64f4efSAntonio Nino Diaz * Memory mapping private attributes 1290b64f4efSAntonio Nino Diaz * 1300b64f4efSAntonio Nino Diaz * Private attributes not exposed in the mmap_attr_t enum. 1310b64f4efSAntonio Nino Diaz */ 1320b64f4efSAntonio Nino Diaz typedef enum { 1330b64f4efSAntonio Nino Diaz /* 1340b64f4efSAntonio Nino Diaz * Regions mapped before the MMU can't be unmapped dynamically (they are 1350b64f4efSAntonio Nino Diaz * static) and regions mapped with MMU enabled can be unmapped. This 1360b64f4efSAntonio Nino Diaz * behaviour can't be overridden. 1370b64f4efSAntonio Nino Diaz * 1380b64f4efSAntonio Nino Diaz * Static regions can overlap each other, dynamic regions can't. 1390b64f4efSAntonio Nino Diaz */ 1400b64f4efSAntonio Nino Diaz MT_STATIC = 0 << MT_DYN_SHIFT, 1410b64f4efSAntonio Nino Diaz MT_DYNAMIC = 1 << MT_DYN_SHIFT 1420b64f4efSAntonio Nino Diaz } mmap_priv_attr_t; 1430b64f4efSAntonio Nino Diaz 1440b64f4efSAntonio Nino Diaz /* 1450b64f4efSAntonio Nino Diaz * Function used to invalidate all levels of the translation walk for a given 1460b64f4efSAntonio Nino Diaz * virtual address. It must be called for every translation table entry that is 1470b64f4efSAntonio Nino Diaz * modified. 1480b64f4efSAntonio Nino Diaz */ 1490b64f4efSAntonio Nino Diaz void xlat_arch_tlbi_va(uintptr_t va); 1500b64f4efSAntonio Nino Diaz 1510b64f4efSAntonio Nino Diaz /* 1520b64f4efSAntonio Nino Diaz * This function has to be called at the end of any code that uses the function 1530b64f4efSAntonio Nino Diaz * xlat_arch_tlbi_va(). 1540b64f4efSAntonio Nino Diaz */ 1550b64f4efSAntonio Nino Diaz void xlat_arch_tlbi_va_sync(void); 1560b64f4efSAntonio Nino Diaz 1570b64f4efSAntonio Nino Diaz /* Add a dynamic region to the specified context. */ 1580b64f4efSAntonio Nino Diaz int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm); 1590b64f4efSAntonio Nino Diaz 1600b64f4efSAntonio Nino Diaz /* Remove a dynamic region from the specified context. */ 1610b64f4efSAntonio Nino Diaz int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, uintptr_t base_va, 1620b64f4efSAntonio Nino Diaz size_t size); 1630b64f4efSAntonio Nino Diaz 1640b64f4efSAntonio Nino Diaz #endif /* PLAT_XLAT_TABLES_DYNAMIC */ 1650b64f4efSAntonio Nino Diaz 1667bb01fb2SAntonio Nino Diaz /* Print VA, PA, size and attributes of all regions in the mmap array. */ 1677bb01fb2SAntonio Nino Diaz void print_mmap(mmap_region_t *const mmap); 1687bb01fb2SAntonio Nino Diaz 1697bb01fb2SAntonio Nino Diaz /* 1707bb01fb2SAntonio Nino Diaz * Print the current state of the translation tables by reading them from 1717bb01fb2SAntonio Nino Diaz * memory. 1727bb01fb2SAntonio Nino Diaz */ 1737bb01fb2SAntonio Nino Diaz void xlat_tables_print(xlat_ctx_t *ctx); 1747bb01fb2SAntonio Nino Diaz 1757bb01fb2SAntonio Nino Diaz /* 1767bb01fb2SAntonio Nino Diaz * Initialize the translation tables by mapping all regions added to the 1777bb01fb2SAntonio Nino Diaz * specified context. 1787bb01fb2SAntonio Nino Diaz */ 1797bb01fb2SAntonio Nino Diaz void init_xlation_table(xlat_ctx_t *ctx); 1807bb01fb2SAntonio Nino Diaz 1817bb01fb2SAntonio Nino Diaz /* Add a static region to the specified context. */ 1827bb01fb2SAntonio Nino Diaz void mmap_add_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm); 1837bb01fb2SAntonio Nino Diaz 1847bb01fb2SAntonio Nino Diaz /* 1857bb01fb2SAntonio Nino Diaz * Architecture-specific initialization code. 1867bb01fb2SAntonio Nino Diaz */ 1877bb01fb2SAntonio Nino Diaz 188*a5640252SAntonio Nino Diaz /* Returns the current Exception Level. The returned EL must be 1 or higher. */ 189*a5640252SAntonio Nino Diaz int xlat_arch_current_el(void); 190*a5640252SAntonio Nino Diaz 191*a5640252SAntonio Nino Diaz /* 192*a5640252SAntonio Nino Diaz * Returns the bit mask that has to be ORed to the rest of a translation table 193*a5640252SAntonio Nino Diaz * descriptor so that execution of code is prohibited at the given Exception 194*a5640252SAntonio Nino Diaz * Level. 195*a5640252SAntonio Nino Diaz */ 196*a5640252SAntonio Nino Diaz uint64_t xlat_arch_get_xn_desc(int el); 197*a5640252SAntonio Nino Diaz 1987bb01fb2SAntonio Nino Diaz /* Execute architecture-specific translation table initialization code. */ 1997bb01fb2SAntonio Nino Diaz void init_xlat_tables_arch(unsigned long long max_pa); 2007bb01fb2SAntonio Nino Diaz 2017bb01fb2SAntonio Nino Diaz /* Enable MMU and configure it to use the specified translation tables. */ 2027bb01fb2SAntonio Nino Diaz void enable_mmu_arch(unsigned int flags, uint64_t *base_table); 2037bb01fb2SAntonio Nino Diaz 2047bb01fb2SAntonio Nino Diaz /* Return 1 if the MMU of this Exception Level is enabled, 0 otherwise. */ 2057bb01fb2SAntonio Nino Diaz int is_mmu_enabled(void); 2067bb01fb2SAntonio Nino Diaz 2077bb01fb2SAntonio Nino Diaz #endif /* __XLAT_TABLES_PRIVATE_H__ */ 208