1 /* 2 * Copyright (c) 2022-2026, 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 <lib/gpt_rme/gpt_rme.h> 11 #include <lib/utils_def.h> 12 13 /******************************************************************************/ 14 /* GPT descriptor definitions */ 15 /******************************************************************************/ 16 17 /* GPT level 0 descriptor bit definitions */ 18 #define GPT_L0_TYPE_MASK UL(0xF) 19 #define GPT_L0_TYPE_SHIFT U(0) 20 21 /* GPT level 0 table and block descriptors */ 22 #define GPT_L0_TYPE_TBL_DESC UL(3) 23 #define GPT_L0_TYPE_BLK_DESC UL(1) 24 25 #define GPT_L0_TBL_DESC_L1ADDR_MASK UL(0xFFFFFFFFFF) 26 #define GPT_L0_TBL_DESC_L1ADDR_SHIFT U(12) 27 28 #define GPT_L0_BLK_DESC_GPI_MASK UL(0xF) 29 #define GPT_L0_BLK_DESC_GPI_SHIFT U(4) 30 31 /* GPT level 1 Contiguous descriptor */ 32 #define GPT_L1_TYPE_CONT_DESC_MASK UL(0xF) 33 #define GPT_L1_TYPE_CONT_DESC UL(1) 34 35 /* GPT level 1 Contiguous descriptor definitions */ 36 #define GPT_L1_CONTIG_2MB UL(1) 37 #define GPT_L1_CONTIG_32MB UL(2) 38 #define GPT_L1_CONTIG_512MB UL(3) 39 40 #define GPT_L1_CONT_DESC_GPI_SHIFT U(4) 41 #define GPT_L1_CONT_DESC_GPI_MASK UL(0xF) 42 #define GPT_L1_CONT_DESC_CONTIG_SHIFT U(8) 43 #define GPT_L1_CONT_DESC_CONTIG_MASK UL(3) 44 45 /* GPT level 1 Granules descriptor bit definitions */ 46 #define GPT_L1_GRAN_DESC_GPI_MASK UL(0xF) 47 48 /* L1 Contiguous descriptors templates */ 49 #define GPT_L1_CONT_DESC_2MB \ 50 (GPT_L1_TYPE_CONT_DESC | \ 51 (GPT_L1_CONTIG_2MB << GPT_L1_CONT_DESC_CONTIG_SHIFT)) 52 #define GPT_L1_CONT_DESC_32MB \ 53 (GPT_L1_TYPE_CONT_DESC | \ 54 (GPT_L1_CONTIG_32MB << GPT_L1_CONT_DESC_CONTIG_SHIFT)) 55 #define GPT_L1_CONT_DESC_512MB \ 56 (GPT_L1_TYPE_CONT_DESC | \ 57 (GPT_L1_CONTIG_512MB << GPT_L1_CONT_DESC_CONTIG_SHIFT)) 58 59 /* Create L1 Contiguous descriptor from GPI and template */ 60 #define GPT_L1_GPI_CONT_DESC(_gpi, _desc) \ 61 ((_desc) | ((uint64_t)(_gpi) << GPT_L1_CONT_DESC_GPI_SHIFT)) 62 63 /* Create L1 Contiguous descriptor from Granules descriptor and size */ 64 #define GPT_L1_CONT_DESC(_desc, _size) \ 65 (GPT_L1_CONT_DESC_##_size | \ 66 (((_desc) & GPT_L1_GRAN_DESC_GPI_MASK) << \ 67 GPT_L1_CONT_DESC_GPI_SHIFT)) 68 69 /* Create L1 Contiguous descriptor from GPI and size */ 70 #define GPT_L1_CONT_DESC_SIZE(_gpi, _size) \ 71 (GPT_L1_CONT_DESC_##_size | \ 72 (((uint64_t)(_gpi) << GPT_L1_CONT_DESC_GPI_SHIFT)) 73 74 #define GPT_L1_GPI_BYTE(_gpi) (uint64_t)((_gpi) | ((_gpi) << 4)) 75 #define GPT_L1_GPI_HALF(_gpi) (GPT_L1_GPI_BYTE(_gpi) | (GPT_L1_GPI_BYTE(_gpi) << 8)) 76 #define GPT_L1_GPI_WORD(_gpi) (GPT_L1_GPI_HALF(_gpi) | (GPT_L1_GPI_HALF(_gpi) << 16)) 77 78 /* 79 * This macro generates a Granules descriptor 80 * with the same value for every GPI entry. 81 */ 82 #define GPT_BUILD_L1_DESC(_gpi) (GPT_L1_GPI_WORD(_gpi) | (GPT_L1_GPI_WORD(_gpi) << 32)) 83 84 #define GPT_L1_SECURE_DESC GPT_BUILD_L1_DESC(GPT_GPI_SECURE) 85 #define GPT_L1_NS_DESC GPT_BUILD_L1_DESC(GPT_GPI_NS) 86 #define GPT_L1_REALM_DESC GPT_BUILD_L1_DESC(GPT_GPI_REALM) 87 #define GPT_L1_NSO_DESC GPT_BUILD_L1_DESC(GPT_GPI_NSO) 88 #define GPT_L1_ROOT_DESC GPT_BUILD_L1_DESC(GPT_GPI_ROOT) 89 #define GPT_L1_SA_DESC GPT_BUILD_L1_DESC(GPT_GPI_SA) 90 #define GPT_L1_NSP_DESC GPT_BUILD_L1_DESC(GPT_GPI_NSP) 91 #define GPT_L1_ANY_DESC GPT_BUILD_L1_DESC(GPT_GPI_ANY) 92 #define GPT_L1_INVALID_DESC UL(0xFFFFFFFFFFFFFFFF) 93 94 /******************************************************************************/ 95 /* GPT platform configuration */ 96 /******************************************************************************/ 97 98 /* This value comes from GPCCR_EL3 so no externally supplied definition */ 99 #define GPT_L0GPTSZ ((unsigned int)((read_gpccr_el3() >> \ 100 GPCCR_L0GPTSZ_SHIFT) & GPCCR_L0GPTSZ_MASK)) 101 102 /* The "S" value is directly related to L0GPTSZ */ 103 #define GPT_S_VAL (GPT_L0GPTSZ + 30U) 104 105 /* 106 * Map PPS values to T values. 107 * 108 * PPS Size T 109 * 0b000 4GB 32 110 * 0b001 64GB 36 111 * 0b010 1TB 40 112 * 0b011 4TB 42 113 * 0b100 16TB 44 114 * 0b101 256TB 48 115 * 0b110 4PB 52 116 * 117 * See section 15.1.27 of the RME specification. 118 */ 119 typedef enum { 120 PPS_4GB_T = 32U, 121 PPS_64GB_T = 36U, 122 PPS_1TB_T = 40U, 123 PPS_4TB_T = 42U, 124 PPS_16TB_T = 44U, 125 PPS_256TB_T = 48U, 126 PPS_4PB_T = 52U 127 } gpt_t_val_e; 128 129 /* 130 * Map PGS values to P values. 131 * 132 * PGS Size P 133 * 0b00 4KB 12 134 * 0b10 16KB 14 135 * 0b01 64KB 16 136 * 137 * Note that pgs=0b10 is 16KB and pgs=0b01 is 64KB, this is not a typo. 138 * 139 * See section 15.1.27 of the RME specification. 140 */ 141 typedef enum { 142 PGS_4KB_P = 12U, 143 PGS_16KB_P = 14U, 144 PGS_64KB_P = 16U 145 } gpt_p_val_e; 146 147 /* 148 * Internal structure to retrieve the values from get_gpi_params(); 149 */ 150 typedef struct { 151 uint64_t gpt_l1_desc; 152 uint64_t *gpt_l1_addr; 153 unsigned int idx; 154 unsigned int gpi_shift; 155 unsigned int gpi; 156 #if (RME_GPT_BITLOCK_BLOCK != 0) 157 bitlock_t *lock; 158 LOCK_TYPE mask; 159 #endif 160 } gpi_info_t; 161 162 /* 163 * Look up structure for contiguous blocks and descriptors 164 */ 165 typedef struct { 166 size_t size; 167 unsigned int desc; 168 } gpt_fill_lookup_t; 169 170 typedef void (*gpt_shatter_func)(uintptr_t base, const gpi_info_t *gpi_info, 171 uint64_t l1_desc); 172 typedef void (*gpt_tlbi_func)(uintptr_t base); 173 174 /* 175 * Look-up structure for 176 * invalidating TLBs of GPT entries by Physical address, last level. 177 */ 178 typedef struct { 179 gpt_tlbi_func function; 180 size_t mask; 181 } gpt_tlbi_lookup_t; 182 183 /* Max valid value for PGS */ 184 #define GPT_PGS_MAX (2U) 185 186 /* Max valid value for PPS */ 187 #define GPT_PPS_MAX (6U) 188 189 /******************************************************************************/ 190 /* L0 address attribute macros */ 191 /******************************************************************************/ 192 193 /* 194 * Width of the L0 index field. 195 * 196 * If S is greater than or equal to T then there is a single L0 region covering 197 * the entire protected space so there is no L0 index, so the width (and the 198 * derivative mask value) are both zero. If we don't specifically handle this 199 * special case we'll get a negative width value which does not make sense and 200 * would cause problems. 201 */ 202 #define GPT_L0_IDX_WIDTH(_t) (((unsigned int)(_t) > GPT_S_VAL) ? \ 203 ((unsigned int)(_t) - GPT_S_VAL) : (0U)) 204 205 /* Bit shift for the L0 index field in a PA */ 206 #define GPT_L0_IDX_SHIFT (GPT_S_VAL) 207 208 /* 209 * Mask for the L0 index field, must be shifted. 210 * 211 * The value 0x3FFFFF is 22 bits wide which is the maximum possible width of the 212 * L0 index within a physical address. This is calculated by 213 * ((t_max - 1) - s_min + 1) where t_max is 52 for 4PB, the largest PPS, and 214 * s_min is 30 for 1GB, the smallest L0GPTSZ. 215 */ 216 #define GPT_L0_IDX_MASK(_t) (0x3FFFFFUL >> (22U - \ 217 (GPT_L0_IDX_WIDTH(_t)))) 218 219 /* Total number of L0 regions */ 220 #define GPT_L0_REGION_COUNT(_t) ((GPT_L0_IDX_MASK(_t)) + 1U) 221 222 /* Total size of each GPT L0 region in bytes */ 223 #define GPT_L0_REGION_SIZE (1UL << (GPT_L0_IDX_SHIFT)) 224 225 /* Total size in bytes of the whole L0 table */ 226 #define GPT_L0_TABLE_SIZE(_t) ((GPT_L0_REGION_COUNT(_t)) << 3U) 227 228 /******************************************************************************/ 229 /* L1 address attribute macros */ 230 /******************************************************************************/ 231 232 /* 233 * Width of the L1 index field. 234 * 235 * This field does not have a special case to handle widths less than zero like 236 * the L0 index field above since all valid combinations of PGS (p) and L0GPTSZ 237 * (s) will result in a positive width value. 238 */ 239 #define GPT_L1_IDX_WIDTH(_p) ((GPT_S_VAL - 1U) - \ 240 ((unsigned int)(_p) + 3U)) 241 242 /* Bit shift for the L1 index field */ 243 #define GPT_L1_IDX_SHIFT(_p) ((unsigned int)(_p) + 4U) 244 245 /* 246 * Mask for the L1 index field, must be shifted. 247 * 248 * The value 0x7FFFFF is 23 bits wide and is the maximum possible width of the 249 * L1 index within a physical address. It is calculated by 250 * ((s_max - 1) - (p_min + 4) + 1) where s_max is 39 for 512GB, the largest 251 * L0GPTSZ, and p_min is 12 for 4KB granules, the smallest PGS. 252 */ 253 #define GPT_L1_IDX_MASK(_p) (0x7FFFFFUL >> (23U - \ 254 (GPT_L1_IDX_WIDTH(_p)))) 255 256 /* Bit shift for the index of the L1 GPI in a PA */ 257 #define GPT_L1_GPI_IDX_SHIFT(_p) (_p) 258 259 /* Mask for the index of the L1 GPI in a PA */ 260 #define GPT_L1_GPI_IDX_MASK (0xF) 261 262 /* Total number of entries in each L1 table */ 263 #define GPT_L1_ENTRY_COUNT(_p) ((GPT_L1_IDX_MASK(_p)) + 1UL) 264 265 /* Number of L1 entries in 2MB block */ 266 #define GPT_L1_ENTRY_COUNT_2MB(_p) (SZ_2M >> GPT_L1_IDX_SHIFT(_p)) 267 268 /* Total size in bytes of each L1 table */ 269 #define GPT_L1_TABLE_SIZE(_p) ((GPT_L1_ENTRY_COUNT(_p)) << 3U) 270 271 /******************************************************************************/ 272 /* General helper macros */ 273 /******************************************************************************/ 274 275 /* Protected space actual size in bytes */ 276 #define GPT_PPS_ACTUAL_SIZE(_t) (1UL << (unsigned int)(_t)) 277 278 /* Granule actual size in bytes */ 279 #define GPT_PGS_ACTUAL_SIZE(_p) (1UL << (unsigned int)(_p)) 280 281 /* Number of granules in 2MB block */ 282 #define GPT_PGS_COUNT_2MB(_p) (1UL << (21U - (unsigned int)(_p))) 283 284 /* L0 GPT region size in bytes */ 285 #define GPT_L0GPTSZ_ACTUAL_SIZE (1UL << GPT_S_VAL) 286 287 /* Get the index of the L0 entry from a physical address */ 288 #define GPT_L0_IDX(_pa) ((_pa) >> GPT_L0_IDX_SHIFT) 289 290 /* 291 * This definition is used to determine if a physical address lies on an L0 292 * region boundary. 293 */ 294 #define GPT_IS_L0_ALIGNED(_pa) \ 295 (((_pa) & (GPT_L0_REGION_SIZE - UL(1))) == UL(0)) 296 297 /* Get the type field from an L0 descriptor */ 298 #define GPT_L0_TYPE(_desc) (((_desc) >> GPT_L0_TYPE_SHIFT) & \ 299 GPT_L0_TYPE_MASK) 300 301 /* Create an L0 block descriptor */ 302 #define GPT_L0_BLK_DESC(_gpi) (GPT_L0_TYPE_BLK_DESC | \ 303 (((_gpi) & GPT_L0_BLK_DESC_GPI_MASK) << \ 304 GPT_L0_BLK_DESC_GPI_SHIFT)) 305 306 /* Create an L0 table descriptor with an L1 table address */ 307 #define GPT_L0_TBL_DESC(_pa) (GPT_L0_TYPE_TBL_DESC | ((uint64_t)(_pa) & \ 308 (GPT_L0_TBL_DESC_L1ADDR_MASK << \ 309 GPT_L0_TBL_DESC_L1ADDR_SHIFT))) 310 311 /* Get the GPI from an L0 block descriptor */ 312 #define GPT_L0_BLKD_GPI(_desc) (((_desc) >> GPT_L0_BLK_DESC_GPI_SHIFT) & \ 313 GPT_L0_BLK_DESC_GPI_MASK) 314 315 /* Get the L1 address from an L0 table descriptor */ 316 #define GPT_L0_TBLD_ADDR(_desc) ((uint64_t *)(((_desc) & \ 317 (GPT_L0_TBL_DESC_L1ADDR_MASK << \ 318 GPT_L0_TBL_DESC_L1ADDR_SHIFT)))) 319 320 /* Get the GPI from L1 Contiguous descriptor */ 321 #define GPT_L1_CONT_GPI(_desc) \ 322 (((_desc) >> GPT_L1_CONT_DESC_GPI_SHIFT) & GPT_L1_CONT_DESC_GPI_MASK) 323 324 /* Get the GPI from L1 Granules descriptor */ 325 #define GPT_L1_GRAN_GPI(_desc) ((_desc) & GPT_L1_GRAN_DESC_GPI_MASK) 326 327 /* Get the Contig from L1 Contiguous descriptor */ 328 #define GPT_L1_CONT_CONTIG(_desc) \ 329 (((_desc) >> GPT_L1_CONT_DESC_CONTIG_SHIFT) & \ 330 GPT_L1_CONT_DESC_CONTIG_MASK) 331 332 /* Get the index into the L1 table from a physical address */ 333 #define GPT_L1_IDX(_p, _pa) \ 334 (((_pa) >> GPT_L1_IDX_SHIFT(_p)) & GPT_L1_IDX_MASK(_p)) 335 336 /* Get the index of the GPI within an L1 table entry from a physical address */ 337 #define GPT_L1_GPI_IDX(_p, _pa) \ 338 (((_pa) >> GPT_L1_GPI_IDX_SHIFT(_p)) & GPT_L1_GPI_IDX_MASK) 339 340 /* Determine if an address is granule-aligned */ 341 #define GPT_IS_L1_ALIGNED(_p, _pa) \ 342 (((_pa) & (GPT_PGS_ACTUAL_SIZE(_p) - UL(1))) == UL(0)) 343 344 /* Get aligned addresses */ 345 #define ALIGN_2MB(_addr) ((_addr) & ~(SZ_2M - 1UL)) 346 #define ALIGN_32MB(_addr) ((_addr) & ~(SZ_32M - 1UL)) 347 #define ALIGN_512MB(_addr) ((_addr) & ~(SZ_512M - 1UL)) 348 349 /* Determine if region is contiguous */ 350 #define GPT_REGION_IS_CONT(_len, _addr, _size) \ 351 (((_len) >= (_size)) && (((_addr) & ((_size) - UL(1))) == UL(0))) 352 353 /* Get 32MB block number in 512MB block: 0-15 */ 354 #define GET_32MB_NUM(_addr) ((_addr >> 25) & 0xF) 355 356 /* Get 2MB block number in 32MB block: 0-15 */ 357 #define GET_2MB_NUM(_addr) ((_addr >> 21) & 0xF) 358 359 #endif /* GPT_RME_PRIVATE_H */ 360