17bb01fb2SAntonio Nino Diaz /* 27bb01fb2SAntonio Nino Diaz * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. 37bb01fb2SAntonio Nino Diaz * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 57bb01fb2SAntonio Nino Diaz */ 67bb01fb2SAntonio Nino Diaz 77bb01fb2SAntonio Nino Diaz #ifndef __XLAT_TABLES_V2_H__ 87bb01fb2SAntonio Nino Diaz #define __XLAT_TABLES_V2_H__ 97bb01fb2SAntonio Nino Diaz 107bb01fb2SAntonio Nino Diaz #include <xlat_tables_defs.h> 117bb01fb2SAntonio Nino Diaz 127bb01fb2SAntonio Nino Diaz #ifndef __ASSEMBLY__ 137bb01fb2SAntonio Nino Diaz #include <stddef.h> 147bb01fb2SAntonio Nino Diaz #include <stdint.h> 157bb01fb2SAntonio Nino Diaz #include <xlat_mmu_helpers.h> 1655c84964SSandrine Bailleux #include <xlat_tables_v2_helpers.h> 177bb01fb2SAntonio Nino Diaz 18fdb1964cSSandrine Bailleux /* 19fdb1964cSSandrine Bailleux * Default granularity size for an mmap_region_t. 20fdb1964cSSandrine Bailleux * Useful when no specific granularity is required. 21fdb1964cSSandrine Bailleux * 22fdb1964cSSandrine Bailleux * By default, choose the biggest possible block size allowed by the 23fdb1964cSSandrine Bailleux * architectural state and granule size in order to minimize the number of page 24fdb1964cSSandrine Bailleux * tables required for the mapping. 257bb01fb2SAntonio Nino Diaz */ 26fdb1964cSSandrine Bailleux #define REGION_DEFAULT_GRANULARITY XLAT_BLOCK_SIZE(MIN_LVL_BLOCK_DESC) 277bb01fb2SAntonio Nino Diaz 28fdb1964cSSandrine Bailleux /* Helper macro to define an mmap_region_t. */ 29fdb1964cSSandrine Bailleux #define MAP_REGION(_pa, _va, _sz, _attr) \ 30fdb1964cSSandrine Bailleux _MAP_REGION_FULL_SPEC(_pa, _va, _sz, _attr, REGION_DEFAULT_GRANULARITY) 31fdb1964cSSandrine Bailleux 32fdb1964cSSandrine Bailleux /* Helper macro to define an mmap_region_t with an identity mapping. */ 33fdb1964cSSandrine Bailleux #define MAP_REGION_FLAT(_adr, _sz, _attr) \ 34fdb1964cSSandrine Bailleux MAP_REGION(_adr, _adr, _sz, _attr) 35fdb1964cSSandrine Bailleux 36fdb1964cSSandrine Bailleux /* 37fdb1964cSSandrine Bailleux * Helper macro to define an mmap_region_t to map with the desired granularity 38fdb1964cSSandrine Bailleux * of translation tables. 39fdb1964cSSandrine Bailleux * 40fdb1964cSSandrine Bailleux * The granularity value passed to this macro must be a valid block or page 41fdb1964cSSandrine Bailleux * size. When using a 4KB translation granule, this might be 4KB, 2MB or 1GB. 42fdb1964cSSandrine Bailleux * Passing REGION_DEFAULT_GRANULARITY is also allowed and means that the library 43fdb1964cSSandrine Bailleux * is free to choose the granularity for this region. In this case, it is 44fdb1964cSSandrine Bailleux * equivalent to the MAP_REGION() macro. 457bb01fb2SAntonio Nino Diaz */ 46fdb1964cSSandrine Bailleux #define MAP_REGION2(_pa, _va, _sz, _attr, _gr) \ 47fdb1964cSSandrine Bailleux _MAP_REGION_FULL_SPEC(_pa, _va, _sz, _attr, _gr) 487bb01fb2SAntonio Nino Diaz 497bb01fb2SAntonio Nino Diaz /* 507bb01fb2SAntonio Nino Diaz * Shifts and masks to access fields of an mmap_attr_t 517bb01fb2SAntonio Nino Diaz */ 52030567e6SVarun Wadekar #define MT_TYPE_MASK U(0x7) 537bb01fb2SAntonio Nino Diaz #define MT_TYPE(_attr) ((_attr) & MT_TYPE_MASK) 547bb01fb2SAntonio Nino Diaz /* Access permissions (RO/RW) */ 55030567e6SVarun Wadekar #define MT_PERM_SHIFT U(3) 567bb01fb2SAntonio Nino Diaz /* Security state (SECURE/NS) */ 57030567e6SVarun Wadekar #define MT_SEC_SHIFT U(4) 587bb01fb2SAntonio Nino Diaz /* Access permissions for instruction execution (EXECUTE/EXECUTE_NEVER) */ 59030567e6SVarun Wadekar #define MT_EXECUTE_SHIFT U(5) 60*609c9191SAntonio Nino Diaz /* 61*609c9191SAntonio Nino Diaz * In the EL1&0 translation regime, mark the region as User (EL0) or 62*609c9191SAntonio Nino Diaz * Privileged (EL1). In the EL3 translation regime this has no effect. 63*609c9191SAntonio Nino Diaz */ 64*609c9191SAntonio Nino Diaz #define MT_USER_SHIFT U(6) 650b64f4efSAntonio Nino Diaz /* All other bits are reserved */ 667bb01fb2SAntonio Nino Diaz 677bb01fb2SAntonio Nino Diaz /* 687bb01fb2SAntonio Nino Diaz * Memory mapping attributes 697bb01fb2SAntonio Nino Diaz */ 707bb01fb2SAntonio Nino Diaz typedef enum { 717bb01fb2SAntonio Nino Diaz /* 727bb01fb2SAntonio Nino Diaz * Memory types supported. 737bb01fb2SAntonio Nino Diaz * These are organised so that, going down the list, the memory types 747bb01fb2SAntonio Nino Diaz * are getting weaker; conversely going up the list the memory types are 757bb01fb2SAntonio Nino Diaz * getting stronger. 767bb01fb2SAntonio Nino Diaz */ 777bb01fb2SAntonio Nino Diaz MT_DEVICE, 787bb01fb2SAntonio Nino Diaz MT_NON_CACHEABLE, 797bb01fb2SAntonio Nino Diaz MT_MEMORY, 807bb01fb2SAntonio Nino Diaz /* Values up to 7 are reserved to add new memory types in the future */ 817bb01fb2SAntonio Nino Diaz 82030567e6SVarun Wadekar MT_RO = U(0) << MT_PERM_SHIFT, 83030567e6SVarun Wadekar MT_RW = U(1) << MT_PERM_SHIFT, 847bb01fb2SAntonio Nino Diaz 85030567e6SVarun Wadekar MT_SECURE = U(0) << MT_SEC_SHIFT, 86030567e6SVarun Wadekar MT_NS = U(1) << MT_SEC_SHIFT, 877bb01fb2SAntonio Nino Diaz 887bb01fb2SAntonio Nino Diaz /* 897bb01fb2SAntonio Nino Diaz * Access permissions for instruction execution are only relevant for 907bb01fb2SAntonio Nino Diaz * normal read-only memory, i.e. MT_MEMORY | MT_RO. They are ignored 917bb01fb2SAntonio Nino Diaz * (and potentially overridden) otherwise: 927bb01fb2SAntonio Nino Diaz * - Device memory is always marked as execute-never. 937bb01fb2SAntonio Nino Diaz * - Read-write normal memory is always marked as execute-never. 947bb01fb2SAntonio Nino Diaz */ 95030567e6SVarun Wadekar MT_EXECUTE = U(0) << MT_EXECUTE_SHIFT, 96030567e6SVarun Wadekar MT_EXECUTE_NEVER = U(1) << MT_EXECUTE_SHIFT, 97*609c9191SAntonio Nino Diaz 98*609c9191SAntonio Nino Diaz /* 99*609c9191SAntonio Nino Diaz * When mapping a region at EL0 or EL1, this attribute will be used to 100*609c9191SAntonio Nino Diaz * determine if a User mapping (EL0) will be created or a Privileged 101*609c9191SAntonio Nino Diaz * mapping (EL1). 102*609c9191SAntonio Nino Diaz */ 103*609c9191SAntonio Nino Diaz MT_USER = U(1) << MT_USER_SHIFT, 104*609c9191SAntonio Nino Diaz MT_PRIVILEGED = U(0) << MT_USER_SHIFT, 1057bb01fb2SAntonio Nino Diaz } mmap_attr_t; 1067bb01fb2SAntonio Nino Diaz 107*609c9191SAntonio Nino Diaz /* Compound attributes for most common usages */ 1087bb01fb2SAntonio Nino Diaz #define MT_CODE (MT_MEMORY | MT_RO | MT_EXECUTE) 1097bb01fb2SAntonio Nino Diaz #define MT_RO_DATA (MT_MEMORY | MT_RO | MT_EXECUTE_NEVER) 110*609c9191SAntonio Nino Diaz #define MT_RW_DATA (MT_MEMORY | MT_RW | MT_EXECUTE_NEVER) 1117bb01fb2SAntonio Nino Diaz 1127bb01fb2SAntonio Nino Diaz /* 1137bb01fb2SAntonio Nino Diaz * Structure for specifying a single region of memory. 1147bb01fb2SAntonio Nino Diaz */ 1157bb01fb2SAntonio Nino Diaz typedef struct mmap_region { 1167bb01fb2SAntonio Nino Diaz unsigned long long base_pa; 1177bb01fb2SAntonio Nino Diaz uintptr_t base_va; 1187bb01fb2SAntonio Nino Diaz size_t size; 1197bb01fb2SAntonio Nino Diaz mmap_attr_t attr; 120fdb1964cSSandrine Bailleux /* Desired granularity. See the MAP_REGION2() macro for more details. */ 121fdb1964cSSandrine Bailleux size_t granularity; 1227bb01fb2SAntonio Nino Diaz } mmap_region_t; 1237bb01fb2SAntonio Nino Diaz 12455c84964SSandrine Bailleux /* 125b4ae615bSDouglas Raillard * Translation regimes supported by this library. 126b4ae615bSDouglas Raillard */ 127b4ae615bSDouglas Raillard typedef enum xlat_regime { 128b4ae615bSDouglas Raillard EL1_EL0_REGIME, 129b4ae615bSDouglas Raillard EL3_REGIME, 130b4ae615bSDouglas Raillard } xlat_regime_t; 131b4ae615bSDouglas Raillard 132b4ae615bSDouglas Raillard /* 13355c84964SSandrine Bailleux * Declare the translation context type. 13455c84964SSandrine Bailleux * Its definition is private. 13555c84964SSandrine Bailleux */ 13655c84964SSandrine Bailleux typedef struct xlat_ctx xlat_ctx_t; 13755c84964SSandrine Bailleux 13855c84964SSandrine Bailleux /* 13955c84964SSandrine Bailleux * Statically allocate a translation context and associated structures. Also 14055c84964SSandrine Bailleux * initialize them. 14155c84964SSandrine Bailleux * 14255c84964SSandrine Bailleux * _ctx_name: 14355c84964SSandrine Bailleux * Prefix for the translation context variable. 14455c84964SSandrine Bailleux * E.g. If _ctx_name is 'foo', the variable will be called 'foo_xlat_ctx'. 14555c84964SSandrine Bailleux * Useful to distinguish multiple contexts from one another. 14655c84964SSandrine Bailleux * 14755c84964SSandrine Bailleux * _mmap_count: 14855c84964SSandrine Bailleux * Number of mmap_region_t to allocate. 14955c84964SSandrine Bailleux * Would typically be MAX_MMAP_REGIONS for the translation context describing 15055c84964SSandrine Bailleux * the BL image currently executing. 15155c84964SSandrine Bailleux * 15255c84964SSandrine Bailleux * _xlat_tables_count: 15355c84964SSandrine Bailleux * Number of sub-translation tables to allocate. 15455c84964SSandrine Bailleux * Would typically be MAX_XLAT_TABLES for the translation context describing 15555c84964SSandrine Bailleux * the BL image currently executing. 15655c84964SSandrine Bailleux * Note that this is only for sub-tables ; at the initial lookup level, there 15755c84964SSandrine Bailleux * is always a single table. 15855c84964SSandrine Bailleux * 15955c84964SSandrine Bailleux * _virt_addr_space_size, _phy_addr_space_size: 16055c84964SSandrine Bailleux * Size (in bytes) of the virtual (resp. physical) address space. 16155c84964SSandrine Bailleux * Would typically be PLAT_VIRT_ADDR_SPACE_SIZE 16255c84964SSandrine Bailleux * (resp. PLAT_PHY_ADDR_SPACE_SIZE) for the translation context describing the 16355c84964SSandrine Bailleux * BL image currently executing. 16455c84964SSandrine Bailleux */ 16555c84964SSandrine Bailleux #define REGISTER_XLAT_CONTEXT(_ctx_name, _mmap_count, _xlat_tables_count, \ 16655c84964SSandrine Bailleux _virt_addr_space_size, _phy_addr_space_size) \ 167*609c9191SAntonio Nino Diaz _REGISTER_XLAT_CONTEXT_FULL_SPEC(_ctx_name, _mmap_count, \ 168*609c9191SAntonio Nino Diaz _xlat_tables_count, \ 169*609c9191SAntonio Nino Diaz _virt_addr_space_size, \ 170*609c9191SAntonio Nino Diaz _phy_addr_space_size, \ 171*609c9191SAntonio Nino Diaz IMAGE_XLAT_DEFAULT_REGIME) 172*609c9191SAntonio Nino Diaz 173*609c9191SAntonio Nino Diaz /* 174*609c9191SAntonio Nino Diaz * Same as REGISTER_XLAT_CONTEXT plus the additional parameter _xlat_regime to 175*609c9191SAntonio Nino Diaz * specify the translation regime managed by this xlat_ctx_t instance. The 176*609c9191SAntonio Nino Diaz * values are the one from xlat_regime_t enumeration. 177*609c9191SAntonio Nino Diaz */ 178*609c9191SAntonio Nino Diaz #define REGISTER_XLAT_CONTEXT2(_ctx_name, _mmap_count, _xlat_tables_count, \ 179*609c9191SAntonio Nino Diaz _virt_addr_space_size, _phy_addr_space_size, \ 180*609c9191SAntonio Nino Diaz _xlat_regime) \ 181*609c9191SAntonio Nino Diaz _REGISTER_XLAT_CONTEXT_FULL_SPEC(_ctx_name, _mmap_count, \ 182*609c9191SAntonio Nino Diaz _xlat_tables_count, \ 183*609c9191SAntonio Nino Diaz _virt_addr_space_size, \ 184*609c9191SAntonio Nino Diaz _phy_addr_space_size, \ 185*609c9191SAntonio Nino Diaz _xlat_regime) 18655c84964SSandrine Bailleux 187a9ad848cSSandrine Bailleux /****************************************************************************** 188a9ad848cSSandrine Bailleux * Generic translation table APIs. 189a9ad848cSSandrine Bailleux * Each API comes in 2 variants: 190a9ad848cSSandrine Bailleux * - one that acts on the current translation context for this BL image 191a9ad848cSSandrine Bailleux * - another that acts on the given translation context instead. This variant 192a9ad848cSSandrine Bailleux * is named after the 1st version, with an additional '_ctx' suffix. 193a9ad848cSSandrine Bailleux *****************************************************************************/ 194e769db3eSAntonio Nino Diaz 195e769db3eSAntonio Nino Diaz /* 196e769db3eSAntonio Nino Diaz * Initialize translation tables from the current list of mmap regions. Calling 197e769db3eSAntonio Nino Diaz * this function marks the transition point after which static regions can no 198e769db3eSAntonio Nino Diaz * longer be added. 199e769db3eSAntonio Nino Diaz */ 2007bb01fb2SAntonio Nino Diaz void init_xlat_tables(void); 201a9ad848cSSandrine Bailleux void init_xlat_tables_ctx(xlat_ctx_t *ctx); 2027bb01fb2SAntonio Nino Diaz 2037bb01fb2SAntonio Nino Diaz /* 204e769db3eSAntonio Nino Diaz * Add a static region with defined base PA and base VA. This function can only 205e769db3eSAntonio Nino Diaz * be used before initializing the translation tables. The region cannot be 206e769db3eSAntonio Nino Diaz * removed afterwards. 2077bb01fb2SAntonio Nino Diaz */ 2087bb01fb2SAntonio Nino Diaz void mmap_add_region(unsigned long long base_pa, uintptr_t base_va, 20928fa2e9eSSandrine Bailleux size_t size, mmap_attr_t attr); 210a9ad848cSSandrine Bailleux void mmap_add_region_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm); 2117bb01fb2SAntonio Nino Diaz 2127bb01fb2SAntonio Nino Diaz /* 213a9ad848cSSandrine Bailleux * Add an array of static regions with defined base PA and base VA. This 214a9ad848cSSandrine Bailleux * function can only be used before initializing the translation tables. The 215a9ad848cSSandrine Bailleux * regions cannot be removed afterwards. 216a9ad848cSSandrine Bailleux */ 217a9ad848cSSandrine Bailleux void mmap_add(const mmap_region_t *mm); 218a9ad848cSSandrine Bailleux void mmap_add_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm); 219a9ad848cSSandrine Bailleux 220a9ad848cSSandrine Bailleux 221a9ad848cSSandrine Bailleux #if PLAT_XLAT_TABLES_DYNAMIC 222a9ad848cSSandrine Bailleux /* 223e769db3eSAntonio Nino Diaz * Add a dynamic region with defined base PA and base VA. This type of region 224e769db3eSAntonio Nino Diaz * can be added and removed even after the translation tables are initialized. 2250b64f4efSAntonio Nino Diaz * 2260b64f4efSAntonio Nino Diaz * Returns: 2270b64f4efSAntonio Nino Diaz * 0: Success. 2280b64f4efSAntonio Nino Diaz * EINVAL: Invalid values were used as arguments. 2290b64f4efSAntonio Nino Diaz * ERANGE: Memory limits were surpassed. 2300b64f4efSAntonio Nino Diaz * ENOMEM: Not enough space in the mmap array or not enough free xlat tables. 2310b64f4efSAntonio Nino Diaz * EPERM: It overlaps another region in an invalid way. 2320b64f4efSAntonio Nino Diaz */ 2330b64f4efSAntonio Nino Diaz int mmap_add_dynamic_region(unsigned long long base_pa, uintptr_t base_va, 23428fa2e9eSSandrine Bailleux size_t size, mmap_attr_t attr); 235a9ad848cSSandrine Bailleux int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm); 2367bb01fb2SAntonio Nino Diaz 2370b64f4efSAntonio Nino Diaz /* 2380b64f4efSAntonio Nino Diaz * Remove a region with the specified base VA and size. Only dynamic regions can 239e769db3eSAntonio Nino Diaz * be removed, and they can be removed even if the translation tables are 240e769db3eSAntonio Nino Diaz * initialized. 2410b64f4efSAntonio Nino Diaz * 2420b64f4efSAntonio Nino Diaz * Returns: 2430b64f4efSAntonio Nino Diaz * 0: Success. 2440b64f4efSAntonio Nino Diaz * EINVAL: The specified region wasn't found. 2450b64f4efSAntonio Nino Diaz * EPERM: Trying to remove a static region. 2460b64f4efSAntonio Nino Diaz */ 2470b64f4efSAntonio Nino Diaz int mmap_remove_dynamic_region(uintptr_t base_va, size_t size); 248a9ad848cSSandrine Bailleux int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, 249a9ad848cSSandrine Bailleux uintptr_t base_va, 250a9ad848cSSandrine Bailleux size_t size); 251a9ad848cSSandrine Bailleux 252a9ad848cSSandrine Bailleux #endif /* PLAT_XLAT_TABLES_DYNAMIC */ 2530b64f4efSAntonio Nino Diaz 2547bb01fb2SAntonio Nino Diaz #endif /*__ASSEMBLY__*/ 2557bb01fb2SAntonio Nino Diaz #endif /* __XLAT_TABLES_V2_H__ */ 256