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> 16*55c84964SSandrine Bailleux #include <xlat_tables_v2_helpers.h> 177bb01fb2SAntonio Nino Diaz 187bb01fb2SAntonio Nino Diaz /* Helper macro to define entries for mmap_region_t. It creates 197bb01fb2SAntonio Nino Diaz * identity mappings for each region. 207bb01fb2SAntonio Nino Diaz */ 217bb01fb2SAntonio Nino Diaz #define MAP_REGION_FLAT(adr, sz, attr) MAP_REGION(adr, adr, sz, attr) 227bb01fb2SAntonio Nino Diaz 237bb01fb2SAntonio Nino Diaz /* Helper macro to define entries for mmap_region_t. It allows to 247bb01fb2SAntonio Nino Diaz * re-map address mappings from 'pa' to 'va' for each region. 257bb01fb2SAntonio Nino Diaz */ 267bb01fb2SAntonio Nino Diaz #define MAP_REGION(pa, va, sz, attr) {(pa), (va), (sz), (attr)} 277bb01fb2SAntonio Nino Diaz 287bb01fb2SAntonio Nino Diaz /* 297bb01fb2SAntonio Nino Diaz * Shifts and masks to access fields of an mmap_attr_t 307bb01fb2SAntonio Nino Diaz */ 31030567e6SVarun Wadekar #define MT_TYPE_MASK U(0x7) 327bb01fb2SAntonio Nino Diaz #define MT_TYPE(_attr) ((_attr) & MT_TYPE_MASK) 337bb01fb2SAntonio Nino Diaz /* Access permissions (RO/RW) */ 34030567e6SVarun Wadekar #define MT_PERM_SHIFT U(3) 357bb01fb2SAntonio Nino Diaz /* Security state (SECURE/NS) */ 36030567e6SVarun Wadekar #define MT_SEC_SHIFT U(4) 377bb01fb2SAntonio Nino Diaz /* Access permissions for instruction execution (EXECUTE/EXECUTE_NEVER) */ 38030567e6SVarun Wadekar #define MT_EXECUTE_SHIFT U(5) 390b64f4efSAntonio Nino Diaz /* All other bits are reserved */ 407bb01fb2SAntonio Nino Diaz 417bb01fb2SAntonio Nino Diaz /* 427bb01fb2SAntonio Nino Diaz * Memory mapping attributes 437bb01fb2SAntonio Nino Diaz */ 447bb01fb2SAntonio Nino Diaz typedef enum { 457bb01fb2SAntonio Nino Diaz /* 467bb01fb2SAntonio Nino Diaz * Memory types supported. 477bb01fb2SAntonio Nino Diaz * These are organised so that, going down the list, the memory types 487bb01fb2SAntonio Nino Diaz * are getting weaker; conversely going up the list the memory types are 497bb01fb2SAntonio Nino Diaz * getting stronger. 507bb01fb2SAntonio Nino Diaz */ 517bb01fb2SAntonio Nino Diaz MT_DEVICE, 527bb01fb2SAntonio Nino Diaz MT_NON_CACHEABLE, 537bb01fb2SAntonio Nino Diaz MT_MEMORY, 547bb01fb2SAntonio Nino Diaz /* Values up to 7 are reserved to add new memory types in the future */ 557bb01fb2SAntonio Nino Diaz 56030567e6SVarun Wadekar MT_RO = U(0) << MT_PERM_SHIFT, 57030567e6SVarun Wadekar MT_RW = U(1) << MT_PERM_SHIFT, 587bb01fb2SAntonio Nino Diaz 59030567e6SVarun Wadekar MT_SECURE = U(0) << MT_SEC_SHIFT, 60030567e6SVarun Wadekar MT_NS = U(1) << MT_SEC_SHIFT, 617bb01fb2SAntonio Nino Diaz 627bb01fb2SAntonio Nino Diaz /* 637bb01fb2SAntonio Nino Diaz * Access permissions for instruction execution are only relevant for 647bb01fb2SAntonio Nino Diaz * normal read-only memory, i.e. MT_MEMORY | MT_RO. They are ignored 657bb01fb2SAntonio Nino Diaz * (and potentially overridden) otherwise: 667bb01fb2SAntonio Nino Diaz * - Device memory is always marked as execute-never. 677bb01fb2SAntonio Nino Diaz * - Read-write normal memory is always marked as execute-never. 687bb01fb2SAntonio Nino Diaz */ 69030567e6SVarun Wadekar MT_EXECUTE = U(0) << MT_EXECUTE_SHIFT, 70030567e6SVarun Wadekar MT_EXECUTE_NEVER = U(1) << MT_EXECUTE_SHIFT, 717bb01fb2SAntonio Nino Diaz } mmap_attr_t; 727bb01fb2SAntonio Nino Diaz 737bb01fb2SAntonio Nino Diaz #define MT_CODE (MT_MEMORY | MT_RO | MT_EXECUTE) 747bb01fb2SAntonio Nino Diaz #define MT_RO_DATA (MT_MEMORY | MT_RO | MT_EXECUTE_NEVER) 757bb01fb2SAntonio Nino Diaz 767bb01fb2SAntonio Nino Diaz /* 777bb01fb2SAntonio Nino Diaz * Structure for specifying a single region of memory. 787bb01fb2SAntonio Nino Diaz */ 797bb01fb2SAntonio Nino Diaz typedef struct mmap_region { 807bb01fb2SAntonio Nino Diaz unsigned long long base_pa; 817bb01fb2SAntonio Nino Diaz uintptr_t base_va; 827bb01fb2SAntonio Nino Diaz size_t size; 837bb01fb2SAntonio Nino Diaz mmap_attr_t attr; 847bb01fb2SAntonio Nino Diaz } mmap_region_t; 857bb01fb2SAntonio Nino Diaz 86*55c84964SSandrine Bailleux /* 87*55c84964SSandrine Bailleux * Declare the translation context type. 88*55c84964SSandrine Bailleux * Its definition is private. 89*55c84964SSandrine Bailleux */ 90*55c84964SSandrine Bailleux typedef struct xlat_ctx xlat_ctx_t; 91*55c84964SSandrine Bailleux 92*55c84964SSandrine Bailleux /* 93*55c84964SSandrine Bailleux * Statically allocate a translation context and associated structures. Also 94*55c84964SSandrine Bailleux * initialize them. 95*55c84964SSandrine Bailleux * 96*55c84964SSandrine Bailleux * _ctx_name: 97*55c84964SSandrine Bailleux * Prefix for the translation context variable. 98*55c84964SSandrine Bailleux * E.g. If _ctx_name is 'foo', the variable will be called 'foo_xlat_ctx'. 99*55c84964SSandrine Bailleux * Useful to distinguish multiple contexts from one another. 100*55c84964SSandrine Bailleux * 101*55c84964SSandrine Bailleux * _mmap_count: 102*55c84964SSandrine Bailleux * Number of mmap_region_t to allocate. 103*55c84964SSandrine Bailleux * Would typically be MAX_MMAP_REGIONS for the translation context describing 104*55c84964SSandrine Bailleux * the BL image currently executing. 105*55c84964SSandrine Bailleux * 106*55c84964SSandrine Bailleux * _xlat_tables_count: 107*55c84964SSandrine Bailleux * Number of sub-translation tables to allocate. 108*55c84964SSandrine Bailleux * Would typically be MAX_XLAT_TABLES for the translation context describing 109*55c84964SSandrine Bailleux * the BL image currently executing. 110*55c84964SSandrine Bailleux * Note that this is only for sub-tables ; at the initial lookup level, there 111*55c84964SSandrine Bailleux * is always a single table. 112*55c84964SSandrine Bailleux * 113*55c84964SSandrine Bailleux * _virt_addr_space_size, _phy_addr_space_size: 114*55c84964SSandrine Bailleux * Size (in bytes) of the virtual (resp. physical) address space. 115*55c84964SSandrine Bailleux * Would typically be PLAT_VIRT_ADDR_SPACE_SIZE 116*55c84964SSandrine Bailleux * (resp. PLAT_PHY_ADDR_SPACE_SIZE) for the translation context describing the 117*55c84964SSandrine Bailleux * BL image currently executing. 118*55c84964SSandrine Bailleux */ 119*55c84964SSandrine Bailleux #define REGISTER_XLAT_CONTEXT(_ctx_name, _mmap_count, _xlat_tables_count, \ 120*55c84964SSandrine Bailleux _virt_addr_space_size, _phy_addr_space_size) \ 121*55c84964SSandrine Bailleux _REGISTER_XLAT_CONTEXT(_ctx_name, _mmap_count, _xlat_tables_count, \ 122*55c84964SSandrine Bailleux _virt_addr_space_size, _phy_addr_space_size) 123*55c84964SSandrine Bailleux 1247bb01fb2SAntonio Nino Diaz /* Generic translation table APIs */ 125e769db3eSAntonio Nino Diaz 126e769db3eSAntonio Nino Diaz /* 127e769db3eSAntonio Nino Diaz * Initialize translation tables from the current list of mmap regions. Calling 128e769db3eSAntonio Nino Diaz * this function marks the transition point after which static regions can no 129e769db3eSAntonio Nino Diaz * longer be added. 130e769db3eSAntonio Nino Diaz */ 1317bb01fb2SAntonio Nino Diaz void init_xlat_tables(void); 1327bb01fb2SAntonio Nino Diaz 1337bb01fb2SAntonio Nino Diaz /* 134e769db3eSAntonio Nino Diaz * Add a static region with defined base PA and base VA. This function can only 135e769db3eSAntonio Nino Diaz * be used before initializing the translation tables. The region cannot be 136e769db3eSAntonio Nino Diaz * removed afterwards. 1377bb01fb2SAntonio Nino Diaz */ 1387bb01fb2SAntonio Nino Diaz void mmap_add_region(unsigned long long base_pa, uintptr_t base_va, 13928fa2e9eSSandrine Bailleux size_t size, mmap_attr_t attr); 1407bb01fb2SAntonio Nino Diaz 1417bb01fb2SAntonio Nino Diaz /* 142e769db3eSAntonio Nino Diaz * Add a dynamic region with defined base PA and base VA. This type of region 143e769db3eSAntonio Nino Diaz * can be added and removed even after the translation tables are initialized. 1440b64f4efSAntonio Nino Diaz * 1450b64f4efSAntonio Nino Diaz * Returns: 1460b64f4efSAntonio Nino Diaz * 0: Success. 1470b64f4efSAntonio Nino Diaz * EINVAL: Invalid values were used as arguments. 1480b64f4efSAntonio Nino Diaz * ERANGE: Memory limits were surpassed. 1490b64f4efSAntonio Nino Diaz * ENOMEM: Not enough space in the mmap array or not enough free xlat tables. 1500b64f4efSAntonio Nino Diaz * EPERM: It overlaps another region in an invalid way. 1510b64f4efSAntonio Nino Diaz */ 1520b64f4efSAntonio Nino Diaz int mmap_add_dynamic_region(unsigned long long base_pa, uintptr_t base_va, 15328fa2e9eSSandrine Bailleux size_t size, mmap_attr_t attr); 1540b64f4efSAntonio Nino Diaz 1550b64f4efSAntonio Nino Diaz /* 156e769db3eSAntonio Nino Diaz * Add an array of static regions with defined base PA and base VA. This 157e769db3eSAntonio Nino Diaz * function can only be used before initializing the translation tables. The 158e769db3eSAntonio Nino Diaz * regions cannot be removed afterwards. 1597bb01fb2SAntonio Nino Diaz */ 1607bb01fb2SAntonio Nino Diaz void mmap_add(const mmap_region_t *mm); 1617bb01fb2SAntonio Nino Diaz 1620b64f4efSAntonio Nino Diaz /* 1630b64f4efSAntonio Nino Diaz * Remove a region with the specified base VA and size. Only dynamic regions can 164e769db3eSAntonio Nino Diaz * be removed, and they can be removed even if the translation tables are 165e769db3eSAntonio Nino Diaz * initialized. 1660b64f4efSAntonio Nino Diaz * 1670b64f4efSAntonio Nino Diaz * Returns: 1680b64f4efSAntonio Nino Diaz * 0: Success. 1690b64f4efSAntonio Nino Diaz * EINVAL: The specified region wasn't found. 1700b64f4efSAntonio Nino Diaz * EPERM: Trying to remove a static region. 1710b64f4efSAntonio Nino Diaz */ 1720b64f4efSAntonio Nino Diaz int mmap_remove_dynamic_region(uintptr_t base_va, size_t size); 1730b64f4efSAntonio Nino Diaz 1747bb01fb2SAntonio Nino Diaz #endif /*__ASSEMBLY__*/ 1757bb01fb2SAntonio Nino Diaz #endif /* __XLAT_TABLES_V2_H__ */ 176