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) 600b64f4efSAntonio Nino Diaz /* All other bits are reserved */ 617bb01fb2SAntonio Nino Diaz 627bb01fb2SAntonio Nino Diaz /* 637bb01fb2SAntonio Nino Diaz * Memory mapping attributes 647bb01fb2SAntonio Nino Diaz */ 657bb01fb2SAntonio Nino Diaz typedef enum { 667bb01fb2SAntonio Nino Diaz /* 677bb01fb2SAntonio Nino Diaz * Memory types supported. 687bb01fb2SAntonio Nino Diaz * These are organised so that, going down the list, the memory types 697bb01fb2SAntonio Nino Diaz * are getting weaker; conversely going up the list the memory types are 707bb01fb2SAntonio Nino Diaz * getting stronger. 717bb01fb2SAntonio Nino Diaz */ 727bb01fb2SAntonio Nino Diaz MT_DEVICE, 737bb01fb2SAntonio Nino Diaz MT_NON_CACHEABLE, 747bb01fb2SAntonio Nino Diaz MT_MEMORY, 757bb01fb2SAntonio Nino Diaz /* Values up to 7 are reserved to add new memory types in the future */ 767bb01fb2SAntonio Nino Diaz 77030567e6SVarun Wadekar MT_RO = U(0) << MT_PERM_SHIFT, 78030567e6SVarun Wadekar MT_RW = U(1) << MT_PERM_SHIFT, 797bb01fb2SAntonio Nino Diaz 80030567e6SVarun Wadekar MT_SECURE = U(0) << MT_SEC_SHIFT, 81030567e6SVarun Wadekar MT_NS = U(1) << MT_SEC_SHIFT, 827bb01fb2SAntonio Nino Diaz 837bb01fb2SAntonio Nino Diaz /* 847bb01fb2SAntonio Nino Diaz * Access permissions for instruction execution are only relevant for 857bb01fb2SAntonio Nino Diaz * normal read-only memory, i.e. MT_MEMORY | MT_RO. They are ignored 867bb01fb2SAntonio Nino Diaz * (and potentially overridden) otherwise: 877bb01fb2SAntonio Nino Diaz * - Device memory is always marked as execute-never. 887bb01fb2SAntonio Nino Diaz * - Read-write normal memory is always marked as execute-never. 897bb01fb2SAntonio Nino Diaz */ 90030567e6SVarun Wadekar MT_EXECUTE = U(0) << MT_EXECUTE_SHIFT, 91030567e6SVarun Wadekar MT_EXECUTE_NEVER = U(1) << MT_EXECUTE_SHIFT, 927bb01fb2SAntonio Nino Diaz } mmap_attr_t; 937bb01fb2SAntonio Nino Diaz 947bb01fb2SAntonio Nino Diaz #define MT_CODE (MT_MEMORY | MT_RO | MT_EXECUTE) 957bb01fb2SAntonio Nino Diaz #define MT_RO_DATA (MT_MEMORY | MT_RO | MT_EXECUTE_NEVER) 967bb01fb2SAntonio Nino Diaz 977bb01fb2SAntonio Nino Diaz /* 987bb01fb2SAntonio Nino Diaz * Structure for specifying a single region of memory. 997bb01fb2SAntonio Nino Diaz */ 1007bb01fb2SAntonio Nino Diaz typedef struct mmap_region { 1017bb01fb2SAntonio Nino Diaz unsigned long long base_pa; 1027bb01fb2SAntonio Nino Diaz uintptr_t base_va; 1037bb01fb2SAntonio Nino Diaz size_t size; 1047bb01fb2SAntonio Nino Diaz mmap_attr_t attr; 105fdb1964cSSandrine Bailleux /* Desired granularity. See the MAP_REGION2() macro for more details. */ 106fdb1964cSSandrine Bailleux size_t granularity; 1077bb01fb2SAntonio Nino Diaz } mmap_region_t; 1087bb01fb2SAntonio Nino Diaz 10955c84964SSandrine Bailleux /* 110*b4ae615bSDouglas Raillard * Translation regimes supported by this library. 111*b4ae615bSDouglas Raillard */ 112*b4ae615bSDouglas Raillard typedef enum xlat_regime { 113*b4ae615bSDouglas Raillard EL1_EL0_REGIME, 114*b4ae615bSDouglas Raillard EL3_REGIME, 115*b4ae615bSDouglas Raillard } xlat_regime_t; 116*b4ae615bSDouglas Raillard 117*b4ae615bSDouglas Raillard /* 11855c84964SSandrine Bailleux * Declare the translation context type. 11955c84964SSandrine Bailleux * Its definition is private. 12055c84964SSandrine Bailleux */ 12155c84964SSandrine Bailleux typedef struct xlat_ctx xlat_ctx_t; 12255c84964SSandrine Bailleux 12355c84964SSandrine Bailleux /* 12455c84964SSandrine Bailleux * Statically allocate a translation context and associated structures. Also 12555c84964SSandrine Bailleux * initialize them. 12655c84964SSandrine Bailleux * 12755c84964SSandrine Bailleux * _ctx_name: 12855c84964SSandrine Bailleux * Prefix for the translation context variable. 12955c84964SSandrine Bailleux * E.g. If _ctx_name is 'foo', the variable will be called 'foo_xlat_ctx'. 13055c84964SSandrine Bailleux * Useful to distinguish multiple contexts from one another. 13155c84964SSandrine Bailleux * 13255c84964SSandrine Bailleux * _mmap_count: 13355c84964SSandrine Bailleux * Number of mmap_region_t to allocate. 13455c84964SSandrine Bailleux * Would typically be MAX_MMAP_REGIONS for the translation context describing 13555c84964SSandrine Bailleux * the BL image currently executing. 13655c84964SSandrine Bailleux * 13755c84964SSandrine Bailleux * _xlat_tables_count: 13855c84964SSandrine Bailleux * Number of sub-translation tables to allocate. 13955c84964SSandrine Bailleux * Would typically be MAX_XLAT_TABLES for the translation context describing 14055c84964SSandrine Bailleux * the BL image currently executing. 14155c84964SSandrine Bailleux * Note that this is only for sub-tables ; at the initial lookup level, there 14255c84964SSandrine Bailleux * is always a single table. 14355c84964SSandrine Bailleux * 14455c84964SSandrine Bailleux * _virt_addr_space_size, _phy_addr_space_size: 14555c84964SSandrine Bailleux * Size (in bytes) of the virtual (resp. physical) address space. 14655c84964SSandrine Bailleux * Would typically be PLAT_VIRT_ADDR_SPACE_SIZE 14755c84964SSandrine Bailleux * (resp. PLAT_PHY_ADDR_SPACE_SIZE) for the translation context describing the 14855c84964SSandrine Bailleux * BL image currently executing. 14955c84964SSandrine Bailleux */ 15055c84964SSandrine Bailleux #define REGISTER_XLAT_CONTEXT(_ctx_name, _mmap_count, _xlat_tables_count, \ 15155c84964SSandrine Bailleux _virt_addr_space_size, _phy_addr_space_size) \ 15255c84964SSandrine Bailleux _REGISTER_XLAT_CONTEXT(_ctx_name, _mmap_count, _xlat_tables_count, \ 15355c84964SSandrine Bailleux _virt_addr_space_size, _phy_addr_space_size) 15455c84964SSandrine Bailleux 155a9ad848cSSandrine Bailleux /****************************************************************************** 156a9ad848cSSandrine Bailleux * Generic translation table APIs. 157a9ad848cSSandrine Bailleux * Each API comes in 2 variants: 158a9ad848cSSandrine Bailleux * - one that acts on the current translation context for this BL image 159a9ad848cSSandrine Bailleux * - another that acts on the given translation context instead. This variant 160a9ad848cSSandrine Bailleux * is named after the 1st version, with an additional '_ctx' suffix. 161a9ad848cSSandrine Bailleux *****************************************************************************/ 162e769db3eSAntonio Nino Diaz 163e769db3eSAntonio Nino Diaz /* 164e769db3eSAntonio Nino Diaz * Initialize translation tables from the current list of mmap regions. Calling 165e769db3eSAntonio Nino Diaz * this function marks the transition point after which static regions can no 166e769db3eSAntonio Nino Diaz * longer be added. 167e769db3eSAntonio Nino Diaz */ 1687bb01fb2SAntonio Nino Diaz void init_xlat_tables(void); 169a9ad848cSSandrine Bailleux void init_xlat_tables_ctx(xlat_ctx_t *ctx); 1707bb01fb2SAntonio Nino Diaz 1717bb01fb2SAntonio Nino Diaz /* 172e769db3eSAntonio Nino Diaz * Add a static region with defined base PA and base VA. This function can only 173e769db3eSAntonio Nino Diaz * be used before initializing the translation tables. The region cannot be 174e769db3eSAntonio Nino Diaz * removed afterwards. 1757bb01fb2SAntonio Nino Diaz */ 1767bb01fb2SAntonio Nino Diaz void mmap_add_region(unsigned long long base_pa, uintptr_t base_va, 17728fa2e9eSSandrine Bailleux size_t size, mmap_attr_t attr); 178a9ad848cSSandrine Bailleux void mmap_add_region_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm); 1797bb01fb2SAntonio Nino Diaz 1807bb01fb2SAntonio Nino Diaz /* 181a9ad848cSSandrine Bailleux * Add an array of static regions with defined base PA and base VA. This 182a9ad848cSSandrine Bailleux * function can only be used before initializing the translation tables. The 183a9ad848cSSandrine Bailleux * regions cannot be removed afterwards. 184a9ad848cSSandrine Bailleux */ 185a9ad848cSSandrine Bailleux void mmap_add(const mmap_region_t *mm); 186a9ad848cSSandrine Bailleux void mmap_add_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm); 187a9ad848cSSandrine Bailleux 188a9ad848cSSandrine Bailleux 189a9ad848cSSandrine Bailleux #if PLAT_XLAT_TABLES_DYNAMIC 190a9ad848cSSandrine Bailleux /* 191e769db3eSAntonio Nino Diaz * Add a dynamic region with defined base PA and base VA. This type of region 192e769db3eSAntonio Nino Diaz * can be added and removed even after the translation tables are initialized. 1930b64f4efSAntonio Nino Diaz * 1940b64f4efSAntonio Nino Diaz * Returns: 1950b64f4efSAntonio Nino Diaz * 0: Success. 1960b64f4efSAntonio Nino Diaz * EINVAL: Invalid values were used as arguments. 1970b64f4efSAntonio Nino Diaz * ERANGE: Memory limits were surpassed. 1980b64f4efSAntonio Nino Diaz * ENOMEM: Not enough space in the mmap array or not enough free xlat tables. 1990b64f4efSAntonio Nino Diaz * EPERM: It overlaps another region in an invalid way. 2000b64f4efSAntonio Nino Diaz */ 2010b64f4efSAntonio Nino Diaz int mmap_add_dynamic_region(unsigned long long base_pa, uintptr_t base_va, 20228fa2e9eSSandrine Bailleux size_t size, mmap_attr_t attr); 203a9ad848cSSandrine Bailleux int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm); 2047bb01fb2SAntonio Nino Diaz 2050b64f4efSAntonio Nino Diaz /* 2060b64f4efSAntonio Nino Diaz * Remove a region with the specified base VA and size. Only dynamic regions can 207e769db3eSAntonio Nino Diaz * be removed, and they can be removed even if the translation tables are 208e769db3eSAntonio Nino Diaz * initialized. 2090b64f4efSAntonio Nino Diaz * 2100b64f4efSAntonio Nino Diaz * Returns: 2110b64f4efSAntonio Nino Diaz * 0: Success. 2120b64f4efSAntonio Nino Diaz * EINVAL: The specified region wasn't found. 2130b64f4efSAntonio Nino Diaz * EPERM: Trying to remove a static region. 2140b64f4efSAntonio Nino Diaz */ 2150b64f4efSAntonio Nino Diaz int mmap_remove_dynamic_region(uintptr_t base_va, size_t size); 216a9ad848cSSandrine Bailleux int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, 217a9ad848cSSandrine Bailleux uintptr_t base_va, 218a9ad848cSSandrine Bailleux size_t size); 219a9ad848cSSandrine Bailleux 220a9ad848cSSandrine Bailleux #endif /* PLAT_XLAT_TABLES_DYNAMIC */ 2210b64f4efSAntonio Nino Diaz 2227bb01fb2SAntonio Nino Diaz #endif /*__ASSEMBLY__*/ 2237bb01fb2SAntonio Nino Diaz #endif /* __XLAT_TABLES_V2_H__ */ 224