johpow01 | 9d13402 | 2021-06-16 17:57:28 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #ifndef GPT_RME_PRIVATE_H |
| 8 | #define GPT_RME_PRIVATE_H |
| 9 | |
| 10 | #include <arch.h> |
| 11 | #include <lib/gpt_rme/gpt_rme.h> |
| 12 | #include <lib/utils_def.h> |
| 13 | |
| 14 | /******************************************************************************/ |
| 15 | /* GPT descriptor definitions */ |
| 16 | /******************************************************************************/ |
| 17 | |
| 18 | /* GPT level 0 descriptor bit definitions. */ |
| 19 | #define GPT_L0_TYPE_MASK UL(0xF) |
| 20 | #define GPT_L0_TYPE_SHIFT U(0) |
| 21 | |
| 22 | /* For now, we don't support contiguous descriptors, only table and block. */ |
| 23 | #define GPT_L0_TYPE_TBL_DESC UL(0x3) |
| 24 | #define GPT_L0_TYPE_BLK_DESC UL(0x1) |
| 25 | |
| 26 | #define GPT_L0_TBL_DESC_L1ADDR_MASK UL(0xFFFFFFFFFF) |
| 27 | #define GPT_L0_TBL_DESC_L1ADDR_SHIFT U(12) |
| 28 | |
| 29 | #define GPT_L0_BLK_DESC_GPI_MASK UL(0xF) |
| 30 | #define GPT_L0_BLK_DESC_GPI_SHIFT U(4) |
| 31 | |
| 32 | /* GPT level 1 descriptor bit definitions */ |
| 33 | #define GPT_L1_GRAN_DESC_GPI_MASK UL(0xF) |
| 34 | |
| 35 | /* |
| 36 | * This macro fills out every GPI entry in a granules descriptor to the same |
| 37 | * value. |
| 38 | */ |
| 39 | #define GPT_BUILD_L1_DESC(_gpi) (((uint64_t)(_gpi) << 4*0) | \ |
| 40 | ((uint64_t)(_gpi) << 4*1) | \ |
| 41 | ((uint64_t)(_gpi) << 4*2) | \ |
| 42 | ((uint64_t)(_gpi) << 4*3) | \ |
| 43 | ((uint64_t)(_gpi) << 4*4) | \ |
| 44 | ((uint64_t)(_gpi) << 4*5) | \ |
| 45 | ((uint64_t)(_gpi) << 4*6) | \ |
| 46 | ((uint64_t)(_gpi) << 4*7) | \ |
| 47 | ((uint64_t)(_gpi) << 4*8) | \ |
| 48 | ((uint64_t)(_gpi) << 4*9) | \ |
| 49 | ((uint64_t)(_gpi) << 4*10) | \ |
| 50 | ((uint64_t)(_gpi) << 4*11) | \ |
| 51 | ((uint64_t)(_gpi) << 4*12) | \ |
| 52 | ((uint64_t)(_gpi) << 4*13) | \ |
| 53 | ((uint64_t)(_gpi) << 4*14) | \ |
| 54 | ((uint64_t)(_gpi) << 4*15)) |
| 55 | |
| 56 | /******************************************************************************/ |
| 57 | /* GPT platform configuration */ |
| 58 | /******************************************************************************/ |
| 59 | |
| 60 | /* This value comes from GPCCR_EL3 so no externally supplied definition. */ |
| 61 | #define GPT_L0GPTSZ ((unsigned int)((read_gpccr_el3() >> \ |
| 62 | GPCCR_L0GPTSZ_SHIFT) & GPCCR_L0GPTSZ_MASK)) |
| 63 | |
| 64 | /* The "S" value is directly related to L0GPTSZ */ |
| 65 | #define GPT_S_VAL (GPT_L0GPTSZ + 30U) |
| 66 | |
| 67 | /* |
| 68 | * Map PPS values to T values. |
| 69 | * |
| 70 | * PPS Size T |
| 71 | * 0b000 4GB 32 |
| 72 | * 0b001 64GB 36 |
| 73 | * 0b010 1TB 40 |
| 74 | * 0b011 4TB 42 |
| 75 | * 0b100 16TB 44 |
| 76 | * 0b101 256TB 48 |
| 77 | * 0b110 4PB 52 |
| 78 | * |
| 79 | * See section 15.1.27 of the RME specification. |
| 80 | */ |
| 81 | typedef enum { |
| 82 | PPS_4GB_T = 32U, |
| 83 | PPS_64GB_T = 36U, |
| 84 | PPS_1TB_T = 40U, |
| 85 | PPS_4TB_T = 42U, |
| 86 | PPS_16TB_T = 44U, |
| 87 | PPS_256TB_T = 48U, |
| 88 | PPS_4PB_T = 52U |
| 89 | } gpt_t_val_e; |
| 90 | |
| 91 | /* |
| 92 | * Map PGS values to P values. |
| 93 | * |
| 94 | * PGS Size P |
| 95 | * 0b00 4KB 12 |
| 96 | * 0b10 16KB 14 |
| 97 | * 0b01 64KB 16 |
| 98 | * |
| 99 | * Note that pgs=0b10 is 16KB and pgs=0b01 is 64KB, this is not a typo. |
| 100 | * |
| 101 | * See section 15.1.27 of the RME specification. |
| 102 | */ |
| 103 | typedef enum { |
| 104 | PGS_4KB_P = 12U, |
| 105 | PGS_16KB_P = 14U, |
| 106 | PGS_64KB_P = 16U |
| 107 | } gpt_p_val_e; |
| 108 | |
| 109 | /* Max valid value for PGS. */ |
| 110 | #define GPT_PGS_MAX (2U) |
| 111 | |
| 112 | /* Max valid value for PPS. */ |
| 113 | #define GPT_PPS_MAX (6U) |
| 114 | |
| 115 | /******************************************************************************/ |
| 116 | /* L0 address attribute macros */ |
| 117 | /******************************************************************************/ |
| 118 | |
| 119 | /* |
| 120 | * If S is greater than or equal to T then there is a single L0 region covering |
| 121 | * the entire protected space so there is no L0 index, so the width (and the |
| 122 | * derivative mask value) are both zero. If we don't specifically handle this |
| 123 | * special case we'll get a negative width value which does not make sense and |
| 124 | * could cause a lot of problems. |
| 125 | */ |
| 126 | #define GPT_L0_IDX_WIDTH(_t) (((_t) > GPT_S_VAL) ? \ |
| 127 | ((_t) - GPT_S_VAL) : (0U)) |
| 128 | |
| 129 | /* Bit shift for the L0 index field in a PA. */ |
| 130 | #define GPT_L0_IDX_SHIFT (GPT_S_VAL) |
| 131 | |
| 132 | /* Mask for the L0 index field, must be shifted. */ |
| 133 | #define GPT_L0_IDX_MASK(_t) (0xFFFFFFFFFFFFFFFFUL >> \ |
| 134 | (64U - (GPT_L0_IDX_WIDTH(_t)))) |
| 135 | |
| 136 | /* Total number of L0 regions. */ |
| 137 | #define GPT_L0_REGION_COUNT(_t) ((GPT_L0_IDX_MASK(_t)) + 1U) |
| 138 | |
| 139 | /* Total size of each GPT L0 region in bytes. */ |
| 140 | #define GPT_L0_REGION_SIZE (1UL << (GPT_L0_IDX_SHIFT)) |
| 141 | |
| 142 | /* Total size in bytes of the whole L0 table. */ |
| 143 | #define GPT_L0_TABLE_SIZE(_t) ((GPT_L0_REGION_COUNT(_t)) << 3U) |
| 144 | |
| 145 | /******************************************************************************/ |
| 146 | /* L1 address attribute macros */ |
| 147 | /******************************************************************************/ |
| 148 | |
| 149 | /* Width of the L1 index field. */ |
| 150 | #define GPT_L1_IDX_WIDTH(_p) ((GPT_S_VAL - 1U) - ((_p) + 3U)) |
| 151 | |
| 152 | /* Bit shift for the L1 index field. */ |
| 153 | #define GPT_L1_IDX_SHIFT(_p) ((_p) + 4U) |
| 154 | |
| 155 | /* Mask for the L1 index field, must be shifted. */ |
| 156 | #define GPT_L1_IDX_MASK(_p) (0xFFFFFFFFFFFFFFFFUL >> \ |
| 157 | (64U - (GPT_L1_IDX_WIDTH(_p)))) |
| 158 | |
| 159 | /* Bit shift for the index of the L1 GPI in a PA. */ |
| 160 | #define GPT_L1_GPI_IDX_SHIFT(_p) (_p) |
| 161 | |
| 162 | /* Mask for the index of the L1 GPI in a PA. */ |
| 163 | #define GPT_L1_GPI_IDX_MASK (0xF) |
| 164 | |
| 165 | /* Total number of entries in each L1 table. */ |
| 166 | #define GPT_L1_ENTRY_COUNT(_p) ((GPT_L1_IDX_MASK(_p)) + 1U) |
| 167 | |
| 168 | /* Total size in bytes of each L1 table. */ |
| 169 | #define GPT_L1_TABLE_SIZE(_p) ((GPT_L1_ENTRY_COUNT(_p)) << 3U) |
| 170 | |
| 171 | /******************************************************************************/ |
| 172 | /* General helper macros */ |
| 173 | /******************************************************************************/ |
| 174 | |
| 175 | /* Protected space actual size in bytes. */ |
| 176 | #define GPT_PPS_ACTUAL_SIZE(_t) (1UL << (_t)) |
| 177 | |
| 178 | /* Granule actual size in bytes. */ |
| 179 | #define GPT_PGS_ACTUAL_SIZE(_p) (1UL << (_p)) |
| 180 | |
| 181 | /* L0 GPT region size in bytes. */ |
| 182 | #define GPT_L0GPTSZ_ACTUAL_SIZE (1UL << GPT_S_VAL) |
| 183 | |
| 184 | /* Get the index of the L0 entry from a physical address. */ |
| 185 | #define GPT_L0_IDX(_pa) ((_pa) >> GPT_L0_IDX_SHIFT) |
| 186 | |
| 187 | /* |
| 188 | * This definition is used to determine if a physical address lies on an L0 |
| 189 | * region boundary. |
| 190 | */ |
| 191 | #define GPT_IS_L0_ALIGNED(_pa) (((_pa) & (GPT_L0_REGION_SIZE - U(1))) == U(0)) |
| 192 | |
| 193 | /* Get the type field from an L0 descriptor. */ |
| 194 | #define GPT_L0_TYPE(_desc) (((_desc) >> GPT_L0_TYPE_SHIFT) & \ |
| 195 | GPT_L0_TYPE_MASK) |
| 196 | |
| 197 | /* Create an L0 block descriptor. */ |
| 198 | #define GPT_L0_BLK_DESC(_gpi) (GPT_L0_TYPE_BLK_DESC | \ |
| 199 | (((_gpi) & GPT_L0_BLK_DESC_GPI_MASK) << \ |
| 200 | GPT_L0_BLK_DESC_GPI_SHIFT)) |
| 201 | |
| 202 | /* Create an L0 table descriptor with an L1 table address. */ |
| 203 | #define GPT_L0_TBL_DESC(_pa) (GPT_L0_TYPE_TBL_DESC | ((uint64_t)(_pa) & \ |
| 204 | (GPT_L0_TBL_DESC_L1ADDR_MASK << \ |
| 205 | GPT_L0_TBL_DESC_L1ADDR_SHIFT))) |
| 206 | |
| 207 | /* Get the GPI from an L0 block descriptor. */ |
| 208 | #define GPT_L0_BLKD_GPI(_desc) (((_desc) >> GPT_L0_BLK_DESC_GPI_SHIFT) & \ |
| 209 | GPT_L0_BLK_DESC_GPI_MASK) |
| 210 | |
| 211 | /* Get the L1 address from an L0 table descriptor. */ |
| 212 | #define GPT_L0_TBLD_ADDR(_desc) ((uint64_t *)(((_desc) & \ |
| 213 | (GPT_L0_TBL_DESC_L1ADDR_MASK << \ |
| 214 | GPT_L0_TBL_DESC_L1ADDR_SHIFT)))) |
| 215 | |
| 216 | /* Get the index into the L1 table from a physical address. */ |
| 217 | #define GPT_L1_IDX(_p, _pa) (((_pa) >> GPT_L1_IDX_SHIFT(_p)) & \ |
| 218 | GPT_L1_IDX_MASK(_p)) |
| 219 | |
| 220 | /* Get the index of the GPI within an L1 table entry from a physical address. */ |
| 221 | #define GPT_L1_GPI_IDX(_p, _pa) (((_pa) >> GPT_L1_GPI_IDX_SHIFT(_p)) & \ |
| 222 | GPT_L1_GPI_IDX_MASK) |
| 223 | |
| 224 | /* Determine if an address is granule-aligned. */ |
| 225 | #define GPT_IS_L1_ALIGNED(_p, _pa) (((_pa) & (GPT_PGS_ACTUAL_SIZE(_p) - U(1))) \ |
| 226 | == U(0)) |
| 227 | |
| 228 | #endif /* GPT_RME_PRIVATE_H */ |