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