1 /* 2 * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #ifndef __XLAT_TABLES_V2_H__ 8 #define __XLAT_TABLES_V2_H__ 9 10 #include <xlat_tables_defs.h> 11 12 #ifndef __ASSEMBLY__ 13 #include <stddef.h> 14 #include <stdint.h> 15 #include <xlat_mmu_helpers.h> 16 #include <xlat_tables_v2_helpers.h> 17 18 /* Helper macro to define entries for mmap_region_t. It creates 19 * identity mappings for each region. 20 */ 21 #define MAP_REGION_FLAT(adr, sz, attr) MAP_REGION(adr, adr, sz, attr) 22 23 /* Helper macro to define entries for mmap_region_t. It allows to 24 * re-map address mappings from 'pa' to 'va' for each region. 25 */ 26 #define MAP_REGION(_pa, _va, _sz, _attr) ((mmap_region_t){ \ 27 .base_pa = (_pa), \ 28 .base_va = (_va), \ 29 .size = (_sz), \ 30 .attr = (_attr), \ 31 }) 32 33 /* 34 * Shifts and masks to access fields of an mmap_attr_t 35 */ 36 #define MT_TYPE_MASK U(0x7) 37 #define MT_TYPE(_attr) ((_attr) & MT_TYPE_MASK) 38 /* Access permissions (RO/RW) */ 39 #define MT_PERM_SHIFT U(3) 40 /* Security state (SECURE/NS) */ 41 #define MT_SEC_SHIFT U(4) 42 /* Access permissions for instruction execution (EXECUTE/EXECUTE_NEVER) */ 43 #define MT_EXECUTE_SHIFT U(5) 44 /* All other bits are reserved */ 45 46 /* 47 * Memory mapping attributes 48 */ 49 typedef enum { 50 /* 51 * Memory types supported. 52 * These are organised so that, going down the list, the memory types 53 * are getting weaker; conversely going up the list the memory types are 54 * getting stronger. 55 */ 56 MT_DEVICE, 57 MT_NON_CACHEABLE, 58 MT_MEMORY, 59 /* Values up to 7 are reserved to add new memory types in the future */ 60 61 MT_RO = U(0) << MT_PERM_SHIFT, 62 MT_RW = U(1) << MT_PERM_SHIFT, 63 64 MT_SECURE = U(0) << MT_SEC_SHIFT, 65 MT_NS = U(1) << MT_SEC_SHIFT, 66 67 /* 68 * Access permissions for instruction execution are only relevant for 69 * normal read-only memory, i.e. MT_MEMORY | MT_RO. They are ignored 70 * (and potentially overridden) otherwise: 71 * - Device memory is always marked as execute-never. 72 * - Read-write normal memory is always marked as execute-never. 73 */ 74 MT_EXECUTE = U(0) << MT_EXECUTE_SHIFT, 75 MT_EXECUTE_NEVER = U(1) << MT_EXECUTE_SHIFT, 76 } mmap_attr_t; 77 78 #define MT_CODE (MT_MEMORY | MT_RO | MT_EXECUTE) 79 #define MT_RO_DATA (MT_MEMORY | MT_RO | MT_EXECUTE_NEVER) 80 81 /* 82 * Structure for specifying a single region of memory. 83 */ 84 typedef struct mmap_region { 85 unsigned long long base_pa; 86 uintptr_t base_va; 87 size_t size; 88 mmap_attr_t attr; 89 } mmap_region_t; 90 91 /* 92 * Declare the translation context type. 93 * Its definition is private. 94 */ 95 typedef struct xlat_ctx xlat_ctx_t; 96 97 /* 98 * Statically allocate a translation context and associated structures. Also 99 * initialize them. 100 * 101 * _ctx_name: 102 * Prefix for the translation context variable. 103 * E.g. If _ctx_name is 'foo', the variable will be called 'foo_xlat_ctx'. 104 * Useful to distinguish multiple contexts from one another. 105 * 106 * _mmap_count: 107 * Number of mmap_region_t to allocate. 108 * Would typically be MAX_MMAP_REGIONS for the translation context describing 109 * the BL image currently executing. 110 * 111 * _xlat_tables_count: 112 * Number of sub-translation tables to allocate. 113 * Would typically be MAX_XLAT_TABLES for the translation context describing 114 * the BL image currently executing. 115 * Note that this is only for sub-tables ; at the initial lookup level, there 116 * is always a single table. 117 * 118 * _virt_addr_space_size, _phy_addr_space_size: 119 * Size (in bytes) of the virtual (resp. physical) address space. 120 * Would typically be PLAT_VIRT_ADDR_SPACE_SIZE 121 * (resp. PLAT_PHY_ADDR_SPACE_SIZE) for the translation context describing the 122 * BL image currently executing. 123 */ 124 #define REGISTER_XLAT_CONTEXT(_ctx_name, _mmap_count, _xlat_tables_count, \ 125 _virt_addr_space_size, _phy_addr_space_size) \ 126 _REGISTER_XLAT_CONTEXT(_ctx_name, _mmap_count, _xlat_tables_count, \ 127 _virt_addr_space_size, _phy_addr_space_size) 128 129 /****************************************************************************** 130 * Generic translation table APIs. 131 * Each API comes in 2 variants: 132 * - one that acts on the current translation context for this BL image 133 * - another that acts on the given translation context instead. This variant 134 * is named after the 1st version, with an additional '_ctx' suffix. 135 *****************************************************************************/ 136 137 /* 138 * Initialize translation tables from the current list of mmap regions. Calling 139 * this function marks the transition point after which static regions can no 140 * longer be added. 141 */ 142 void init_xlat_tables(void); 143 void init_xlat_tables_ctx(xlat_ctx_t *ctx); 144 145 /* 146 * Add a static region with defined base PA and base VA. This function can only 147 * be used before initializing the translation tables. The region cannot be 148 * removed afterwards. 149 */ 150 void mmap_add_region(unsigned long long base_pa, uintptr_t base_va, 151 size_t size, mmap_attr_t attr); 152 void mmap_add_region_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm); 153 154 /* 155 * Add an array of static regions with defined base PA and base VA. This 156 * function can only be used before initializing the translation tables. The 157 * regions cannot be removed afterwards. 158 */ 159 void mmap_add(const mmap_region_t *mm); 160 void mmap_add_ctx(xlat_ctx_t *ctx, const mmap_region_t *mm); 161 162 163 #if PLAT_XLAT_TABLES_DYNAMIC 164 /* 165 * Add a dynamic region with defined base PA and base VA. This type of region 166 * can be added and removed even after the translation tables are initialized. 167 * 168 * Returns: 169 * 0: Success. 170 * EINVAL: Invalid values were used as arguments. 171 * ERANGE: Memory limits were surpassed. 172 * ENOMEM: Not enough space in the mmap array or not enough free xlat tables. 173 * EPERM: It overlaps another region in an invalid way. 174 */ 175 int mmap_add_dynamic_region(unsigned long long base_pa, uintptr_t base_va, 176 size_t size, mmap_attr_t attr); 177 int mmap_add_dynamic_region_ctx(xlat_ctx_t *ctx, mmap_region_t *mm); 178 179 /* 180 * Remove a region with the specified base VA and size. Only dynamic regions can 181 * be removed, and they can be removed even if the translation tables are 182 * initialized. 183 * 184 * Returns: 185 * 0: Success. 186 * EINVAL: The specified region wasn't found. 187 * EPERM: Trying to remove a static region. 188 */ 189 int mmap_remove_dynamic_region(uintptr_t base_va, size_t size); 190 int mmap_remove_dynamic_region_ctx(xlat_ctx_t *ctx, 191 uintptr_t base_va, 192 size_t size); 193 194 #endif /* PLAT_XLAT_TABLES_DYNAMIC */ 195 196 #endif /*__ASSEMBLY__*/ 197 #endif /* __XLAT_TABLES_V2_H__ */ 198