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 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 8655c84964SSandrine Bailleux /* 8755c84964SSandrine Bailleux * Declare the translation context type. 8855c84964SSandrine Bailleux * Its definition is private. 8955c84964SSandrine Bailleux */ 9055c84964SSandrine Bailleux typedef struct xlat_ctx xlat_ctx_t; 9155c84964SSandrine Bailleux 9255c84964SSandrine Bailleux /* 9355c84964SSandrine Bailleux * Statically allocate a translation context and associated structures. Also 9455c84964SSandrine Bailleux * initialize them. 9555c84964SSandrine Bailleux * 9655c84964SSandrine Bailleux * _ctx_name: 9755c84964SSandrine Bailleux * Prefix for the translation context variable. 9855c84964SSandrine Bailleux * E.g. If _ctx_name is 'foo', the variable will be called 'foo_xlat_ctx'. 9955c84964SSandrine Bailleux * Useful to distinguish multiple contexts from one another. 10055c84964SSandrine Bailleux * 10155c84964SSandrine Bailleux * _mmap_count: 10255c84964SSandrine Bailleux * Number of mmap_region_t to allocate. 10355c84964SSandrine Bailleux * Would typically be MAX_MMAP_REGIONS for the translation context describing 10455c84964SSandrine Bailleux * the BL image currently executing. 10555c84964SSandrine Bailleux * 10655c84964SSandrine Bailleux * _xlat_tables_count: 10755c84964SSandrine Bailleux * Number of sub-translation tables to allocate. 10855c84964SSandrine Bailleux * Would typically be MAX_XLAT_TABLES for the translation context describing 10955c84964SSandrine Bailleux * the BL image currently executing. 11055c84964SSandrine Bailleux * Note that this is only for sub-tables ; at the initial lookup level, there 11155c84964SSandrine Bailleux * is always a single table. 11255c84964SSandrine Bailleux * 11355c84964SSandrine Bailleux * _virt_addr_space_size, _phy_addr_space_size: 11455c84964SSandrine Bailleux * Size (in bytes) of the virtual (resp. physical) address space. 11555c84964SSandrine Bailleux * Would typically be PLAT_VIRT_ADDR_SPACE_SIZE 11655c84964SSandrine Bailleux * (resp. PLAT_PHY_ADDR_SPACE_SIZE) for the translation context describing the 11755c84964SSandrine Bailleux * BL image currently executing. 11855c84964SSandrine Bailleux */ 11955c84964SSandrine Bailleux #define REGISTER_XLAT_CONTEXT(_ctx_name, _mmap_count, _xlat_tables_count, \ 12055c84964SSandrine Bailleux _virt_addr_space_size, _phy_addr_space_size) \ 12155c84964SSandrine Bailleux _REGISTER_XLAT_CONTEXT(_ctx_name, _mmap_count, _xlat_tables_count, \ 12255c84964SSandrine Bailleux _virt_addr_space_size, _phy_addr_space_size) 12355c84964SSandrine Bailleux 124*a9ad848cSSandrine Bailleux /****************************************************************************** 125*a9ad848cSSandrine Bailleux * Generic translation table APIs. 126*a9ad848cSSandrine Bailleux * Each API comes in 2 variants: 127*a9ad848cSSandrine Bailleux * - one that acts on the current translation context for this BL image 128*a9ad848cSSandrine Bailleux * - another that acts on the given translation context instead. This variant 129*a9ad848cSSandrine Bailleux * is named after the 1st version, with an additional '_ctx' suffix. 130*a9ad848cSSandrine Bailleux *****************************************************************************/ 131e769db3eSAntonio Nino Diaz 132e769db3eSAntonio Nino Diaz /* 133e769db3eSAntonio Nino Diaz * Initialize translation tables from the current list of mmap regions. Calling 134e769db3eSAntonio Nino Diaz * this function marks the transition point after which static regions can no 135e769db3eSAntonio Nino Diaz * longer be added. 136e769db3eSAntonio Nino Diaz */ 1377bb01fb2SAntonio Nino Diaz void init_xlat_tables(void); 138*a9ad848cSSandrine Bailleux void init_xlat_tables_ctx(xlat_ctx_t *ctx); 1397bb01fb2SAntonio Nino Diaz 1407bb01fb2SAntonio Nino Diaz /* 141e769db3eSAntonio Nino Diaz * Add a static region with defined base PA and base VA. This function can only 142e769db3eSAntonio Nino Diaz * be used before initializing the translation tables. The region cannot be 143e769db3eSAntonio Nino Diaz * removed afterwards. 1447bb01fb2SAntonio Nino Diaz */ 1457bb01fb2SAntonio Nino Diaz void mmap_add_region(unsigned long long base_pa, uintptr_t base_va, 14628fa2e9eSSandrine Bailleux size_t size, mmap_attr_t attr); 147*a9ad848cSSandrine Bailleux void mmap_add_region_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm); 1487bb01fb2SAntonio Nino Diaz 1497bb01fb2SAntonio Nino Diaz /* 150*a9ad848cSSandrine Bailleux * Add an array of static regions with defined base PA and base VA. This 151*a9ad848cSSandrine Bailleux * function can only be used before initializing the translation tables. The 152*a9ad848cSSandrine Bailleux * regions cannot be removed afterwards. 153*a9ad848cSSandrine Bailleux */ 154*a9ad848cSSandrine Bailleux void mmap_add(const mmap_region_t *mm); 155*a9ad848cSSandrine Bailleux void mmap_add_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm); 156*a9ad848cSSandrine Bailleux 157*a9ad848cSSandrine Bailleux 158*a9ad848cSSandrine Bailleux #if PLAT_XLAT_TABLES_DYNAMIC 159*a9ad848cSSandrine Bailleux /* 160e769db3eSAntonio Nino Diaz * Add a dynamic region with defined base PA and base VA. This type of region 161e769db3eSAntonio Nino Diaz * can be added and removed even after the translation tables are initialized. 1620b64f4efSAntonio Nino Diaz * 1630b64f4efSAntonio Nino Diaz * Returns: 1640b64f4efSAntonio Nino Diaz * 0: Success. 1650b64f4efSAntonio Nino Diaz * EINVAL: Invalid values were used as arguments. 1660b64f4efSAntonio Nino Diaz * ERANGE: Memory limits were surpassed. 1670b64f4efSAntonio Nino Diaz * ENOMEM: Not enough space in the mmap array or not enough free xlat tables. 1680b64f4efSAntonio Nino Diaz * EPERM: It overlaps another region in an invalid way. 1690b64f4efSAntonio Nino Diaz */ 1700b64f4efSAntonio Nino Diaz int mmap_add_dynamic_region(unsigned long long base_pa, uintptr_t base_va, 17128fa2e9eSSandrine Bailleux size_t size, mmap_attr_t attr); 172*a9ad848cSSandrine Bailleux int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm); 1737bb01fb2SAntonio Nino Diaz 1740b64f4efSAntonio Nino Diaz /* 1750b64f4efSAntonio Nino Diaz * Remove a region with the specified base VA and size. Only dynamic regions can 176e769db3eSAntonio Nino Diaz * be removed, and they can be removed even if the translation tables are 177e769db3eSAntonio Nino Diaz * initialized. 1780b64f4efSAntonio Nino Diaz * 1790b64f4efSAntonio Nino Diaz * Returns: 1800b64f4efSAntonio Nino Diaz * 0: Success. 1810b64f4efSAntonio Nino Diaz * EINVAL: The specified region wasn't found. 1820b64f4efSAntonio Nino Diaz * EPERM: Trying to remove a static region. 1830b64f4efSAntonio Nino Diaz */ 1840b64f4efSAntonio Nino Diaz int mmap_remove_dynamic_region(uintptr_t base_va, size_t size); 185*a9ad848cSSandrine Bailleux int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, 186*a9ad848cSSandrine Bailleux uintptr_t base_va, 187*a9ad848cSSandrine Bailleux size_t size); 188*a9ad848cSSandrine Bailleux 189*a9ad848cSSandrine Bailleux #endif /* PLAT_XLAT_TABLES_DYNAMIC */ 1900b64f4efSAntonio Nino Diaz 1917bb01fb2SAntonio Nino Diaz #endif /*__ASSEMBLY__*/ 1927bb01fb2SAntonio Nino Diaz #endif /* __XLAT_TABLES_V2_H__ */ 193