1*f19dc624Sjohpow01 /* 2*f19dc624Sjohpow01 * Copyright (c) 2021, Arm Limited. All rights reserved. 3*f19dc624Sjohpow01 * 4*f19dc624Sjohpow01 * SPDX-License-Identifier: BSD-3-Clause 5*f19dc624Sjohpow01 */ 6*f19dc624Sjohpow01 7*f19dc624Sjohpow01 #include <assert.h> 8*f19dc624Sjohpow01 #include <errno.h> 9*f19dc624Sjohpow01 #include <limits.h> 10*f19dc624Sjohpow01 #include <stdint.h> 11*f19dc624Sjohpow01 12*f19dc624Sjohpow01 #include <arch.h> 13*f19dc624Sjohpow01 #include <arch_helpers.h> 14*f19dc624Sjohpow01 #include <common/debug.h> 15*f19dc624Sjohpow01 #include "gpt_rme_private.h" 16*f19dc624Sjohpow01 #include <lib/gpt_rme/gpt_rme.h> 17*f19dc624Sjohpow01 #include <lib/smccc.h> 18*f19dc624Sjohpow01 #include <lib/spinlock.h> 19*f19dc624Sjohpow01 #include <lib/xlat_tables/xlat_tables_v2.h> 20*f19dc624Sjohpow01 21*f19dc624Sjohpow01 #if !ENABLE_RME 22*f19dc624Sjohpow01 #error "ENABLE_RME must be enabled to use the GPT library." 23*f19dc624Sjohpow01 #endif 24*f19dc624Sjohpow01 25*f19dc624Sjohpow01 /* 26*f19dc624Sjohpow01 * Lookup T from PPS 27*f19dc624Sjohpow01 * 28*f19dc624Sjohpow01 * PPS Size T 29*f19dc624Sjohpow01 * 0b000 4GB 32 30*f19dc624Sjohpow01 * 0b001 64GB 36 31*f19dc624Sjohpow01 * 0b010 1TB 40 32*f19dc624Sjohpow01 * 0b011 4TB 42 33*f19dc624Sjohpow01 * 0b100 16TB 44 34*f19dc624Sjohpow01 * 0b101 256TB 48 35*f19dc624Sjohpow01 * 0b110 4PB 52 36*f19dc624Sjohpow01 * 37*f19dc624Sjohpow01 * See section 15.1.27 of the RME specification. 38*f19dc624Sjohpow01 */ 39*f19dc624Sjohpow01 static const gpt_t_val_e gpt_t_lookup[] = {PPS_4GB_T, PPS_64GB_T, 40*f19dc624Sjohpow01 PPS_1TB_T, PPS_4TB_T, 41*f19dc624Sjohpow01 PPS_16TB_T, PPS_256TB_T, 42*f19dc624Sjohpow01 PPS_4PB_T}; 43*f19dc624Sjohpow01 44*f19dc624Sjohpow01 /* 45*f19dc624Sjohpow01 * Lookup P from PGS 46*f19dc624Sjohpow01 * 47*f19dc624Sjohpow01 * PGS Size P 48*f19dc624Sjohpow01 * 0b00 4KB 12 49*f19dc624Sjohpow01 * 0b10 16KB 14 50*f19dc624Sjohpow01 * 0b01 64KB 16 51*f19dc624Sjohpow01 * 52*f19dc624Sjohpow01 * Note that pgs=0b10 is 16KB and pgs=0b01 is 64KB, this is not a typo. 53*f19dc624Sjohpow01 * 54*f19dc624Sjohpow01 * See section 15.1.27 of the RME specification. 55*f19dc624Sjohpow01 */ 56*f19dc624Sjohpow01 static const gpt_p_val_e gpt_p_lookup[] = {PGS_4KB_P, PGS_64KB_P, PGS_16KB_P}; 57*f19dc624Sjohpow01 58*f19dc624Sjohpow01 /* 59*f19dc624Sjohpow01 * This structure contains GPT configuration data. 60*f19dc624Sjohpow01 */ 61*f19dc624Sjohpow01 typedef struct { 62*f19dc624Sjohpow01 uintptr_t plat_gpt_l0_base; 63*f19dc624Sjohpow01 gpccr_pps_e pps; 64*f19dc624Sjohpow01 gpt_t_val_e t; 65*f19dc624Sjohpow01 gpccr_pgs_e pgs; 66*f19dc624Sjohpow01 gpt_p_val_e p; 67*f19dc624Sjohpow01 } gpt_config_t; 68*f19dc624Sjohpow01 69*f19dc624Sjohpow01 static gpt_config_t gpt_config; 70*f19dc624Sjohpow01 71*f19dc624Sjohpow01 /* These variables are used during initialization of the L1 tables. */ 72*f19dc624Sjohpow01 static unsigned int gpt_next_l1_tbl_idx; 73*f19dc624Sjohpow01 static uintptr_t gpt_l1_tbl; 74*f19dc624Sjohpow01 75*f19dc624Sjohpow01 /* 76*f19dc624Sjohpow01 * This function checks to see if a GPI value is valid. 77*f19dc624Sjohpow01 * 78*f19dc624Sjohpow01 * These are valid GPI values. 79*f19dc624Sjohpow01 * GPT_GPI_NO_ACCESS U(0x0) 80*f19dc624Sjohpow01 * GPT_GPI_SECURE U(0x8) 81*f19dc624Sjohpow01 * GPT_GPI_NS U(0x9) 82*f19dc624Sjohpow01 * GPT_GPI_ROOT U(0xA) 83*f19dc624Sjohpow01 * GPT_GPI_REALM U(0xB) 84*f19dc624Sjohpow01 * GPT_GPI_ANY U(0xF) 85*f19dc624Sjohpow01 * 86*f19dc624Sjohpow01 * Parameters 87*f19dc624Sjohpow01 * gpi GPI to check for validity. 88*f19dc624Sjohpow01 * 89*f19dc624Sjohpow01 * Return 90*f19dc624Sjohpow01 * true for a valid GPI, false for an invalid one. 91*f19dc624Sjohpow01 */ 92*f19dc624Sjohpow01 static bool gpt_is_gpi_valid(unsigned int gpi) 93*f19dc624Sjohpow01 { 94*f19dc624Sjohpow01 if ((gpi == GPT_GPI_NO_ACCESS) || (gpi == GPT_GPI_ANY) || 95*f19dc624Sjohpow01 ((gpi >= GPT_GPI_SECURE) && (gpi <= GPT_GPI_REALM))) { 96*f19dc624Sjohpow01 return true; 97*f19dc624Sjohpow01 } else { 98*f19dc624Sjohpow01 return false; 99*f19dc624Sjohpow01 } 100*f19dc624Sjohpow01 } 101*f19dc624Sjohpow01 102*f19dc624Sjohpow01 /* 103*f19dc624Sjohpow01 * This function checks to see if two PAS regions overlap. 104*f19dc624Sjohpow01 * 105*f19dc624Sjohpow01 * Parameters 106*f19dc624Sjohpow01 * base_1: base address of first PAS 107*f19dc624Sjohpow01 * size_1: size of first PAS 108*f19dc624Sjohpow01 * base_2: base address of second PAS 109*f19dc624Sjohpow01 * size_2: size of second PAS 110*f19dc624Sjohpow01 * 111*f19dc624Sjohpow01 * Return 112*f19dc624Sjohpow01 * True if PAS regions overlap, false if they do not. 113*f19dc624Sjohpow01 */ 114*f19dc624Sjohpow01 static bool gpt_check_pas_overlap(uintptr_t base_1, size_t size_1, 115*f19dc624Sjohpow01 uintptr_t base_2, size_t size_2) 116*f19dc624Sjohpow01 { 117*f19dc624Sjohpow01 if (((base_1 + size_1) > base_2) && ((base_2 + size_2) > base_1)) { 118*f19dc624Sjohpow01 return true; 119*f19dc624Sjohpow01 } else { 120*f19dc624Sjohpow01 return false; 121*f19dc624Sjohpow01 } 122*f19dc624Sjohpow01 } 123*f19dc624Sjohpow01 124*f19dc624Sjohpow01 /* 125*f19dc624Sjohpow01 * This helper function checks to see if a PAS region from index 0 to 126*f19dc624Sjohpow01 * (pas_idx - 1) occupies the L0 region at index l0_idx in the L0 table. 127*f19dc624Sjohpow01 * 128*f19dc624Sjohpow01 * Parameters 129*f19dc624Sjohpow01 * l0_idx: Index of the L0 entry to check 130*f19dc624Sjohpow01 * pas_regions: PAS region array 131*f19dc624Sjohpow01 * pas_idx: Upper bound of the PAS array index. 132*f19dc624Sjohpow01 * 133*f19dc624Sjohpow01 * Return 134*f19dc624Sjohpow01 * True if a PAS region occupies the L0 region in question, false if not. 135*f19dc624Sjohpow01 */ 136*f19dc624Sjohpow01 static bool gpt_does_previous_pas_exist_here(unsigned int l0_idx, 137*f19dc624Sjohpow01 pas_region_t *pas_regions, 138*f19dc624Sjohpow01 unsigned int pas_idx) 139*f19dc624Sjohpow01 { 140*f19dc624Sjohpow01 /* Iterate over PAS regions up to pas_idx. */ 141*f19dc624Sjohpow01 for (unsigned int i = 0U; i < pas_idx; i++) { 142*f19dc624Sjohpow01 if (gpt_check_pas_overlap((GPT_L0GPTSZ_ACTUAL_SIZE * l0_idx), 143*f19dc624Sjohpow01 GPT_L0GPTSZ_ACTUAL_SIZE, 144*f19dc624Sjohpow01 pas_regions[i].base_pa, pas_regions[i].size)) { 145*f19dc624Sjohpow01 return true; 146*f19dc624Sjohpow01 } 147*f19dc624Sjohpow01 } 148*f19dc624Sjohpow01 return false; 149*f19dc624Sjohpow01 } 150*f19dc624Sjohpow01 151*f19dc624Sjohpow01 /* 152*f19dc624Sjohpow01 * This function iterates over all of the PAS regions and checks them to ensure 153*f19dc624Sjohpow01 * proper alignment of base and size, that the GPI is valid, and that no regions 154*f19dc624Sjohpow01 * overlap. As a part of the overlap checks, this function checks existing L0 155*f19dc624Sjohpow01 * mappings against the new PAS regions in the event that gpt_init_pas_l1_tables 156*f19dc624Sjohpow01 * is called multiple times to place L1 tables in different areas of memory. It 157*f19dc624Sjohpow01 * also counts the number of L1 tables needed and returns it on success. 158*f19dc624Sjohpow01 * 159*f19dc624Sjohpow01 * Parameters 160*f19dc624Sjohpow01 * *pas_regions Pointer to array of PAS region structures. 161*f19dc624Sjohpow01 * pas_region_cnt Total number of PAS regions in the array. 162*f19dc624Sjohpow01 * 163*f19dc624Sjohpow01 * Return 164*f19dc624Sjohpow01 * Negative Linux error code in the event of a failure, number of L1 regions 165*f19dc624Sjohpow01 * required when successful. 166*f19dc624Sjohpow01 */ 167*f19dc624Sjohpow01 static int gpt_validate_pas_mappings(pas_region_t *pas_regions, 168*f19dc624Sjohpow01 unsigned int pas_region_cnt) 169*f19dc624Sjohpow01 { 170*f19dc624Sjohpow01 unsigned int idx; 171*f19dc624Sjohpow01 unsigned int l1_cnt = 0U; 172*f19dc624Sjohpow01 unsigned int pas_l1_cnt; 173*f19dc624Sjohpow01 uint64_t *l0_desc = (uint64_t *)gpt_config.plat_gpt_l0_base; 174*f19dc624Sjohpow01 175*f19dc624Sjohpow01 assert(pas_regions != NULL); 176*f19dc624Sjohpow01 assert(pas_region_cnt != 0U); 177*f19dc624Sjohpow01 178*f19dc624Sjohpow01 for (idx = 0U; idx < pas_region_cnt; idx++) { 179*f19dc624Sjohpow01 /* Check for arithmetic overflow in region. */ 180*f19dc624Sjohpow01 if ((ULONG_MAX - pas_regions[idx].base_pa) < 181*f19dc624Sjohpow01 pas_regions[idx].size) { 182*f19dc624Sjohpow01 ERROR("[GPT] Address overflow in PAS[%u]!\n", idx); 183*f19dc624Sjohpow01 return -EOVERFLOW; 184*f19dc624Sjohpow01 } 185*f19dc624Sjohpow01 186*f19dc624Sjohpow01 /* Initial checks for PAS validity. */ 187*f19dc624Sjohpow01 if (((pas_regions[idx].base_pa + pas_regions[idx].size) > 188*f19dc624Sjohpow01 GPT_PPS_ACTUAL_SIZE(gpt_config.t)) || 189*f19dc624Sjohpow01 !gpt_is_gpi_valid(GPT_PAS_ATTR_GPI(pas_regions[idx].attrs))) { 190*f19dc624Sjohpow01 ERROR("[GPT] PAS[%u] is invalid!\n", idx); 191*f19dc624Sjohpow01 return -EFAULT; 192*f19dc624Sjohpow01 } 193*f19dc624Sjohpow01 194*f19dc624Sjohpow01 /* 195*f19dc624Sjohpow01 * Make sure this PAS does not overlap with another one. We 196*f19dc624Sjohpow01 * start from idx + 1 instead of 0 since prior PAS mappings will 197*f19dc624Sjohpow01 * have already checked themselves against this one. 198*f19dc624Sjohpow01 */ 199*f19dc624Sjohpow01 for (unsigned int i = idx + 1; i < pas_region_cnt; i++) { 200*f19dc624Sjohpow01 if (gpt_check_pas_overlap(pas_regions[idx].base_pa, 201*f19dc624Sjohpow01 pas_regions[idx].size, 202*f19dc624Sjohpow01 pas_regions[i].base_pa, 203*f19dc624Sjohpow01 pas_regions[i].size)) { 204*f19dc624Sjohpow01 ERROR("[GPT] PAS[%u] overlaps with PAS[%u]\n", 205*f19dc624Sjohpow01 i, idx); 206*f19dc624Sjohpow01 return -EFAULT; 207*f19dc624Sjohpow01 } 208*f19dc624Sjohpow01 } 209*f19dc624Sjohpow01 210*f19dc624Sjohpow01 /* 211*f19dc624Sjohpow01 * Since this function can be called multiple times with 212*f19dc624Sjohpow01 * separate L1 tables we need to check the existing L0 mapping 213*f19dc624Sjohpow01 * to see if this PAS would fall into one that has already been 214*f19dc624Sjohpow01 * initialized. 215*f19dc624Sjohpow01 */ 216*f19dc624Sjohpow01 for (unsigned int i = GPT_L0_IDX(pas_regions[idx].base_pa); 217*f19dc624Sjohpow01 i <= GPT_L0_IDX(pas_regions[idx].base_pa + pas_regions[idx].size - 1); 218*f19dc624Sjohpow01 i++) { 219*f19dc624Sjohpow01 if ((GPT_L0_TYPE(l0_desc[i]) == GPT_L0_TYPE_BLK_DESC) && 220*f19dc624Sjohpow01 (GPT_L0_BLKD_GPI(l0_desc[i]) == GPT_GPI_ANY)) { 221*f19dc624Sjohpow01 /* This descriptor is unused so continue. */ 222*f19dc624Sjohpow01 continue; 223*f19dc624Sjohpow01 } 224*f19dc624Sjohpow01 225*f19dc624Sjohpow01 /* 226*f19dc624Sjohpow01 * This descriptor has been initialized in a previous 227*f19dc624Sjohpow01 * call to this function so cannot be initialized again. 228*f19dc624Sjohpow01 */ 229*f19dc624Sjohpow01 ERROR("[GPT] PAS[%u] overlaps with previous L0[%d]!\n", 230*f19dc624Sjohpow01 idx, i); 231*f19dc624Sjohpow01 return -EFAULT; 232*f19dc624Sjohpow01 } 233*f19dc624Sjohpow01 234*f19dc624Sjohpow01 /* Check for block mapping (L0) type. */ 235*f19dc624Sjohpow01 if (GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs) == 236*f19dc624Sjohpow01 GPT_PAS_ATTR_MAP_TYPE_BLOCK) { 237*f19dc624Sjohpow01 /* Make sure base and size are block-aligned. */ 238*f19dc624Sjohpow01 if (!GPT_IS_L0_ALIGNED(pas_regions[idx].base_pa) || 239*f19dc624Sjohpow01 !GPT_IS_L0_ALIGNED(pas_regions[idx].size)) { 240*f19dc624Sjohpow01 ERROR("[GPT] PAS[%u] is not block-aligned!\n", 241*f19dc624Sjohpow01 idx); 242*f19dc624Sjohpow01 return -EFAULT; 243*f19dc624Sjohpow01 } 244*f19dc624Sjohpow01 245*f19dc624Sjohpow01 continue; 246*f19dc624Sjohpow01 } 247*f19dc624Sjohpow01 248*f19dc624Sjohpow01 /* Check for granule mapping (L1) type. */ 249*f19dc624Sjohpow01 if (GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs) == 250*f19dc624Sjohpow01 GPT_PAS_ATTR_MAP_TYPE_GRANULE) { 251*f19dc624Sjohpow01 /* Make sure base and size are granule-aligned. */ 252*f19dc624Sjohpow01 if (!GPT_IS_L1_ALIGNED(gpt_config.p, pas_regions[idx].base_pa) || 253*f19dc624Sjohpow01 !GPT_IS_L1_ALIGNED(gpt_config.p, pas_regions[idx].size)) { 254*f19dc624Sjohpow01 ERROR("[GPT] PAS[%u] is not granule-aligned!\n", 255*f19dc624Sjohpow01 idx); 256*f19dc624Sjohpow01 return -EFAULT; 257*f19dc624Sjohpow01 } 258*f19dc624Sjohpow01 259*f19dc624Sjohpow01 /* Find how many L1 tables this PAS occupies. */ 260*f19dc624Sjohpow01 pas_l1_cnt = (GPT_L0_IDX(pas_regions[idx].base_pa + 261*f19dc624Sjohpow01 pas_regions[idx].size - 1) - 262*f19dc624Sjohpow01 GPT_L0_IDX(pas_regions[idx].base_pa) + 1); 263*f19dc624Sjohpow01 264*f19dc624Sjohpow01 /* 265*f19dc624Sjohpow01 * This creates a situation where, if multiple PAS 266*f19dc624Sjohpow01 * regions occupy the same table descriptor, we can get 267*f19dc624Sjohpow01 * an artificially high total L1 table count. The way we 268*f19dc624Sjohpow01 * handle this is by checking each PAS against those 269*f19dc624Sjohpow01 * before it in the array, and if they both occupy the 270*f19dc624Sjohpow01 * same PAS we subtract from pas_l1_cnt and only the 271*f19dc624Sjohpow01 * first PAS in the array gets to count it. 272*f19dc624Sjohpow01 */ 273*f19dc624Sjohpow01 274*f19dc624Sjohpow01 /* 275*f19dc624Sjohpow01 * If L1 count is greater than 1 we know the start and 276*f19dc624Sjohpow01 * end PAs are in different L0 regions so we must check 277*f19dc624Sjohpow01 * both for overlap against other PAS. 278*f19dc624Sjohpow01 */ 279*f19dc624Sjohpow01 if (pas_l1_cnt > 1) { 280*f19dc624Sjohpow01 if (gpt_does_previous_pas_exist_here( 281*f19dc624Sjohpow01 GPT_L0_IDX(pas_regions[idx].base_pa + 282*f19dc624Sjohpow01 pas_regions[idx].size - 1), 283*f19dc624Sjohpow01 pas_regions, idx)) { 284*f19dc624Sjohpow01 pas_l1_cnt = pas_l1_cnt - 1; 285*f19dc624Sjohpow01 } 286*f19dc624Sjohpow01 } 287*f19dc624Sjohpow01 288*f19dc624Sjohpow01 if (gpt_does_previous_pas_exist_here( 289*f19dc624Sjohpow01 GPT_L0_IDX(pas_regions[idx].base_pa), 290*f19dc624Sjohpow01 pas_regions, idx)) { 291*f19dc624Sjohpow01 pas_l1_cnt = pas_l1_cnt - 1; 292*f19dc624Sjohpow01 } 293*f19dc624Sjohpow01 294*f19dc624Sjohpow01 l1_cnt += pas_l1_cnt; 295*f19dc624Sjohpow01 continue; 296*f19dc624Sjohpow01 } 297*f19dc624Sjohpow01 298*f19dc624Sjohpow01 /* If execution reaches this point, mapping type is invalid. */ 299*f19dc624Sjohpow01 ERROR("[GPT] PAS[%u] has invalid mapping type 0x%x.\n", idx, 300*f19dc624Sjohpow01 GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs)); 301*f19dc624Sjohpow01 return -EINVAL; 302*f19dc624Sjohpow01 } 303*f19dc624Sjohpow01 304*f19dc624Sjohpow01 return l1_cnt; 305*f19dc624Sjohpow01 } 306*f19dc624Sjohpow01 307*f19dc624Sjohpow01 /* 308*f19dc624Sjohpow01 * This function validates L0 initialization parameters. 309*f19dc624Sjohpow01 * 310*f19dc624Sjohpow01 * Parameters 311*f19dc624Sjohpow01 * l0_mem_base Base address of memory used for L0 tables. 312*f19dc624Sjohpow01 * l1_mem_size Size of memory available for L0 tables. 313*f19dc624Sjohpow01 * 314*f19dc624Sjohpow01 * Return 315*f19dc624Sjohpow01 * Negative Linux error code in the event of a failure, 0 for success. 316*f19dc624Sjohpow01 */ 317*f19dc624Sjohpow01 static int gpt_validate_l0_params(gpccr_pps_e pps, uintptr_t l0_mem_base, 318*f19dc624Sjohpow01 size_t l0_mem_size) 319*f19dc624Sjohpow01 { 320*f19dc624Sjohpow01 size_t l0_alignment; 321*f19dc624Sjohpow01 322*f19dc624Sjohpow01 /* 323*f19dc624Sjohpow01 * Make sure PPS is valid and then store it since macros need this value 324*f19dc624Sjohpow01 * to work. 325*f19dc624Sjohpow01 */ 326*f19dc624Sjohpow01 if (pps > GPT_PPS_MAX) { 327*f19dc624Sjohpow01 ERROR("[GPT] Invalid PPS: 0x%x\n", pps); 328*f19dc624Sjohpow01 return -EINVAL; 329*f19dc624Sjohpow01 } 330*f19dc624Sjohpow01 gpt_config.pps = pps; 331*f19dc624Sjohpow01 gpt_config.t = gpt_t_lookup[pps]; 332*f19dc624Sjohpow01 333*f19dc624Sjohpow01 /* Alignment must be the greater of 4k or l0 table size. */ 334*f19dc624Sjohpow01 l0_alignment = PAGE_SIZE_4KB; 335*f19dc624Sjohpow01 if (l0_alignment < GPT_L0_TABLE_SIZE(gpt_config.t)) { 336*f19dc624Sjohpow01 l0_alignment = GPT_L0_TABLE_SIZE(gpt_config.t); 337*f19dc624Sjohpow01 } 338*f19dc624Sjohpow01 339*f19dc624Sjohpow01 /* Check base address. */ 340*f19dc624Sjohpow01 if ((l0_mem_base == 0U) || ((l0_mem_base & (l0_alignment - 1)) != 0U)) { 341*f19dc624Sjohpow01 ERROR("[GPT] Invalid L0 base address: 0x%lx\n", l0_mem_base); 342*f19dc624Sjohpow01 return -EFAULT; 343*f19dc624Sjohpow01 } 344*f19dc624Sjohpow01 345*f19dc624Sjohpow01 /* Check size. */ 346*f19dc624Sjohpow01 if (l0_mem_size < GPT_L0_TABLE_SIZE(gpt_config.t)) { 347*f19dc624Sjohpow01 ERROR("[GPT] Inadequate L0 memory: need 0x%lx, have 0x%lx)\n", 348*f19dc624Sjohpow01 GPT_L0_TABLE_SIZE(gpt_config.t), 349*f19dc624Sjohpow01 l0_mem_size); 350*f19dc624Sjohpow01 return -ENOMEM; 351*f19dc624Sjohpow01 } 352*f19dc624Sjohpow01 353*f19dc624Sjohpow01 return 0; 354*f19dc624Sjohpow01 } 355*f19dc624Sjohpow01 356*f19dc624Sjohpow01 /* 357*f19dc624Sjohpow01 * In the event that L1 tables are needed, this function validates 358*f19dc624Sjohpow01 * the L1 table generation parameters. 359*f19dc624Sjohpow01 * 360*f19dc624Sjohpow01 * Parameters 361*f19dc624Sjohpow01 * l1_mem_base Base address of memory used for L1 table allocation. 362*f19dc624Sjohpow01 * l1_mem_size Total size of memory available for L1 tables. 363*f19dc624Sjohpow01 * l1_gpt_cnt Number of L1 tables needed. 364*f19dc624Sjohpow01 * 365*f19dc624Sjohpow01 * Return 366*f19dc624Sjohpow01 * Negative Linux error code in the event of a failure, 0 for success. 367*f19dc624Sjohpow01 */ 368*f19dc624Sjohpow01 static int gpt_validate_l1_params(uintptr_t l1_mem_base, size_t l1_mem_size, 369*f19dc624Sjohpow01 unsigned int l1_gpt_cnt) 370*f19dc624Sjohpow01 { 371*f19dc624Sjohpow01 size_t l1_gpt_mem_sz; 372*f19dc624Sjohpow01 373*f19dc624Sjohpow01 /* Check if the granularity is supported */ 374*f19dc624Sjohpow01 if (!xlat_arch_is_granule_size_supported( 375*f19dc624Sjohpow01 GPT_PGS_ACTUAL_SIZE(gpt_config.p))) { 376*f19dc624Sjohpow01 return -EPERM; 377*f19dc624Sjohpow01 } 378*f19dc624Sjohpow01 379*f19dc624Sjohpow01 /* Make sure L1 tables are aligned to their size. */ 380*f19dc624Sjohpow01 if ((l1_mem_base & (GPT_L1_TABLE_SIZE(gpt_config.p) - 1)) != 0U) { 381*f19dc624Sjohpow01 ERROR("[GPT] Unaligned L1 GPT base address: 0x%lx\n", 382*f19dc624Sjohpow01 l1_mem_base); 383*f19dc624Sjohpow01 return -EFAULT; 384*f19dc624Sjohpow01 } 385*f19dc624Sjohpow01 386*f19dc624Sjohpow01 /* Get total memory needed for L1 tables. */ 387*f19dc624Sjohpow01 l1_gpt_mem_sz = l1_gpt_cnt * GPT_L1_TABLE_SIZE(gpt_config.p); 388*f19dc624Sjohpow01 389*f19dc624Sjohpow01 /* Check for overflow. */ 390*f19dc624Sjohpow01 if ((l1_gpt_mem_sz / GPT_L1_TABLE_SIZE(gpt_config.p)) != l1_gpt_cnt) { 391*f19dc624Sjohpow01 ERROR("[GPT] Overflow calculating L1 memory size.\n"); 392*f19dc624Sjohpow01 return -ENOMEM; 393*f19dc624Sjohpow01 } 394*f19dc624Sjohpow01 395*f19dc624Sjohpow01 /* Make sure enough space was supplied. */ 396*f19dc624Sjohpow01 if (l1_mem_size < l1_gpt_mem_sz) { 397*f19dc624Sjohpow01 ERROR("[GPT] Inadequate memory for L1 GPTs. "); 398*f19dc624Sjohpow01 ERROR(" Expected 0x%lx bytes. Got 0x%lx bytes\n", 399*f19dc624Sjohpow01 l1_gpt_mem_sz, l1_mem_size); 400*f19dc624Sjohpow01 return -ENOMEM; 401*f19dc624Sjohpow01 } 402*f19dc624Sjohpow01 403*f19dc624Sjohpow01 VERBOSE("[GPT] Requested 0x%lx bytes for L1 GPTs.\n", l1_gpt_mem_sz); 404*f19dc624Sjohpow01 return 0; 405*f19dc624Sjohpow01 } 406*f19dc624Sjohpow01 407*f19dc624Sjohpow01 /* 408*f19dc624Sjohpow01 * This function initializes L0 block descriptors (regions that cannot be 409*f19dc624Sjohpow01 * transitioned at the granule level) according to the provided PAS. 410*f19dc624Sjohpow01 * 411*f19dc624Sjohpow01 * Parameters 412*f19dc624Sjohpow01 * *pas Pointer to the structure defining the PAS region to 413*f19dc624Sjohpow01 * initialize. 414*f19dc624Sjohpow01 */ 415*f19dc624Sjohpow01 static void gpt_generate_l0_blk_desc(pas_region_t *pas) 416*f19dc624Sjohpow01 { 417*f19dc624Sjohpow01 uint64_t gpt_desc; 418*f19dc624Sjohpow01 unsigned int end_idx; 419*f19dc624Sjohpow01 unsigned int idx; 420*f19dc624Sjohpow01 uint64_t *l0_gpt_arr; 421*f19dc624Sjohpow01 422*f19dc624Sjohpow01 assert(gpt_config.plat_gpt_l0_base != 0U); 423*f19dc624Sjohpow01 assert(pas != NULL); 424*f19dc624Sjohpow01 425*f19dc624Sjohpow01 /* 426*f19dc624Sjohpow01 * Checking of PAS parameters has already been done in 427*f19dc624Sjohpow01 * gpt_validate_pas_mappings so no need to check the same things again. 428*f19dc624Sjohpow01 */ 429*f19dc624Sjohpow01 430*f19dc624Sjohpow01 l0_gpt_arr = (uint64_t *)gpt_config.plat_gpt_l0_base; 431*f19dc624Sjohpow01 432*f19dc624Sjohpow01 /* Create the GPT Block descriptor for this PAS region */ 433*f19dc624Sjohpow01 gpt_desc = GPT_L0_BLK_DESC(GPT_PAS_ATTR_GPI(pas->attrs)); 434*f19dc624Sjohpow01 435*f19dc624Sjohpow01 /* Start index of this region in L0 GPTs */ 436*f19dc624Sjohpow01 idx = pas->base_pa >> GPT_L0_IDX_SHIFT; 437*f19dc624Sjohpow01 438*f19dc624Sjohpow01 /* 439*f19dc624Sjohpow01 * Determine number of L0 GPT descriptors covered by 440*f19dc624Sjohpow01 * this PAS region and use the count to populate these 441*f19dc624Sjohpow01 * descriptors. 442*f19dc624Sjohpow01 */ 443*f19dc624Sjohpow01 end_idx = (pas->base_pa + pas->size) >> GPT_L0_IDX_SHIFT; 444*f19dc624Sjohpow01 445*f19dc624Sjohpow01 /* Generate the needed block descriptors. */ 446*f19dc624Sjohpow01 for (; idx < end_idx; idx++) { 447*f19dc624Sjohpow01 l0_gpt_arr[idx] = gpt_desc; 448*f19dc624Sjohpow01 VERBOSE("[GPT] L0 entry (BLOCK) index %u [%p]: GPI = 0x%llx (0x%llx)\n", 449*f19dc624Sjohpow01 idx, &l0_gpt_arr[idx], 450*f19dc624Sjohpow01 (gpt_desc >> GPT_L0_BLK_DESC_GPI_SHIFT) & 451*f19dc624Sjohpow01 GPT_L0_BLK_DESC_GPI_MASK, l0_gpt_arr[idx]); 452*f19dc624Sjohpow01 } 453*f19dc624Sjohpow01 } 454*f19dc624Sjohpow01 455*f19dc624Sjohpow01 /* 456*f19dc624Sjohpow01 * Helper function to determine if the end physical address lies in the same L0 457*f19dc624Sjohpow01 * region as the current physical address. If true, the end physical address is 458*f19dc624Sjohpow01 * returned else, the start address of the next region is returned. 459*f19dc624Sjohpow01 * 460*f19dc624Sjohpow01 * Parameters 461*f19dc624Sjohpow01 * cur_pa Physical address of the current PA in the loop through 462*f19dc624Sjohpow01 * the range. 463*f19dc624Sjohpow01 * end_pa Physical address of the end PA in a PAS range. 464*f19dc624Sjohpow01 * 465*f19dc624Sjohpow01 * Return 466*f19dc624Sjohpow01 * The PA of the end of the current range. 467*f19dc624Sjohpow01 */ 468*f19dc624Sjohpow01 static uintptr_t gpt_get_l1_end_pa(uintptr_t cur_pa, uintptr_t end_pa) 469*f19dc624Sjohpow01 { 470*f19dc624Sjohpow01 uintptr_t cur_idx; 471*f19dc624Sjohpow01 uintptr_t end_idx; 472*f19dc624Sjohpow01 473*f19dc624Sjohpow01 cur_idx = cur_pa >> GPT_L0_IDX_SHIFT; 474*f19dc624Sjohpow01 end_idx = end_pa >> GPT_L0_IDX_SHIFT; 475*f19dc624Sjohpow01 476*f19dc624Sjohpow01 assert(cur_idx <= end_idx); 477*f19dc624Sjohpow01 478*f19dc624Sjohpow01 if (cur_idx == end_idx) { 479*f19dc624Sjohpow01 return end_pa; 480*f19dc624Sjohpow01 } 481*f19dc624Sjohpow01 482*f19dc624Sjohpow01 return (cur_idx + 1U) << GPT_L0_IDX_SHIFT; 483*f19dc624Sjohpow01 } 484*f19dc624Sjohpow01 485*f19dc624Sjohpow01 /* 486*f19dc624Sjohpow01 * Helper function to fill out GPI entries in a single L1 table. This function 487*f19dc624Sjohpow01 * fills out entire L1 descriptors at a time to save memory writes. 488*f19dc624Sjohpow01 * 489*f19dc624Sjohpow01 * Parameters 490*f19dc624Sjohpow01 * gpi GPI to set this range to 491*f19dc624Sjohpow01 * l1 Pointer to L1 table to fill out 492*f19dc624Sjohpow01 * first Address of first granule in range. 493*f19dc624Sjohpow01 * last Address of last granule in range (inclusive). 494*f19dc624Sjohpow01 */ 495*f19dc624Sjohpow01 static void gpt_fill_l1_tbl(uint64_t gpi, uint64_t *l1, uintptr_t first, 496*f19dc624Sjohpow01 uintptr_t last) 497*f19dc624Sjohpow01 { 498*f19dc624Sjohpow01 uint64_t gpi_field = GPT_BUILD_L1_DESC(gpi); 499*f19dc624Sjohpow01 uint64_t gpi_mask = 0xFFFFFFFFFFFFFFFF; 500*f19dc624Sjohpow01 501*f19dc624Sjohpow01 assert(first <= last); 502*f19dc624Sjohpow01 assert((first & (GPT_PGS_ACTUAL_SIZE(gpt_config.p) - 1)) == 0U); 503*f19dc624Sjohpow01 assert((last & (GPT_PGS_ACTUAL_SIZE(gpt_config.p) - 1)) == 0U); 504*f19dc624Sjohpow01 assert(GPT_L0_IDX(first) == GPT_L0_IDX(last)); 505*f19dc624Sjohpow01 assert(l1 != NULL); 506*f19dc624Sjohpow01 507*f19dc624Sjohpow01 /* Shift the mask if we're starting in the middle of an L1 entry. */ 508*f19dc624Sjohpow01 gpi_mask = gpi_mask << (GPT_L1_GPI_IDX(gpt_config.p, first) << 2); 509*f19dc624Sjohpow01 510*f19dc624Sjohpow01 /* Fill out each L1 entry for this region. */ 511*f19dc624Sjohpow01 for (unsigned int i = GPT_L1_IDX(gpt_config.p, first); 512*f19dc624Sjohpow01 i <= GPT_L1_IDX(gpt_config.p, last); i++) { 513*f19dc624Sjohpow01 /* Account for stopping in the middle of an L1 entry. */ 514*f19dc624Sjohpow01 if (i == GPT_L1_IDX(gpt_config.p, last)) { 515*f19dc624Sjohpow01 gpi_mask &= (gpi_mask >> ((15 - 516*f19dc624Sjohpow01 GPT_L1_GPI_IDX(gpt_config.p, last)) << 2)); 517*f19dc624Sjohpow01 } 518*f19dc624Sjohpow01 519*f19dc624Sjohpow01 /* Write GPI values. */ 520*f19dc624Sjohpow01 assert((l1[i] & gpi_mask) == 521*f19dc624Sjohpow01 (GPT_BUILD_L1_DESC(GPT_GPI_ANY) & gpi_mask)); 522*f19dc624Sjohpow01 l1[i] = (l1[i] & ~gpi_mask) | (gpi_mask & gpi_field); 523*f19dc624Sjohpow01 524*f19dc624Sjohpow01 /* Reset mask. */ 525*f19dc624Sjohpow01 gpi_mask = 0xFFFFFFFFFFFFFFFF; 526*f19dc624Sjohpow01 } 527*f19dc624Sjohpow01 } 528*f19dc624Sjohpow01 529*f19dc624Sjohpow01 /* 530*f19dc624Sjohpow01 * This function finds the next available unused L1 table and initializes all 531*f19dc624Sjohpow01 * granules descriptor entries to GPI_ANY. This ensures that there are no chunks 532*f19dc624Sjohpow01 * of GPI_NO_ACCESS (0b0000) memory floating around in the system in the 533*f19dc624Sjohpow01 * event that a PAS region stops midway through an L1 table, thus guaranteeing 534*f19dc624Sjohpow01 * that all memory not explicitly assigned is GPI_ANY. This function does not 535*f19dc624Sjohpow01 * check for overflow conditions, that should be done by the caller. 536*f19dc624Sjohpow01 * 537*f19dc624Sjohpow01 * Return 538*f19dc624Sjohpow01 * Pointer to the next available L1 table. 539*f19dc624Sjohpow01 */ 540*f19dc624Sjohpow01 static uint64_t *gpt_get_new_l1_tbl(void) 541*f19dc624Sjohpow01 { 542*f19dc624Sjohpow01 /* Retrieve the next L1 table. */ 543*f19dc624Sjohpow01 uint64_t *l1 = (uint64_t *)((uint64_t)(gpt_l1_tbl) + 544*f19dc624Sjohpow01 (GPT_L1_TABLE_SIZE(gpt_config.p) * 545*f19dc624Sjohpow01 gpt_next_l1_tbl_idx)); 546*f19dc624Sjohpow01 547*f19dc624Sjohpow01 /* Increment L1 counter. */ 548*f19dc624Sjohpow01 gpt_next_l1_tbl_idx++; 549*f19dc624Sjohpow01 550*f19dc624Sjohpow01 /* Initialize all GPIs to GPT_GPI_ANY */ 551*f19dc624Sjohpow01 for (unsigned int i = 0U; i < GPT_L1_ENTRY_COUNT(gpt_config.p); i++) { 552*f19dc624Sjohpow01 l1[i] = GPT_BUILD_L1_DESC(GPT_GPI_ANY); 553*f19dc624Sjohpow01 } 554*f19dc624Sjohpow01 555*f19dc624Sjohpow01 return l1; 556*f19dc624Sjohpow01 } 557*f19dc624Sjohpow01 558*f19dc624Sjohpow01 /* 559*f19dc624Sjohpow01 * When L1 tables are needed, this function creates the necessary L0 table 560*f19dc624Sjohpow01 * descriptors and fills out the L1 table entries according to the supplied 561*f19dc624Sjohpow01 * PAS range. 562*f19dc624Sjohpow01 * 563*f19dc624Sjohpow01 * Parameters 564*f19dc624Sjohpow01 * *pas Pointer to the structure defining the PAS region. 565*f19dc624Sjohpow01 */ 566*f19dc624Sjohpow01 static void gpt_generate_l0_tbl_desc(pas_region_t *pas) 567*f19dc624Sjohpow01 { 568*f19dc624Sjohpow01 uintptr_t end_pa; 569*f19dc624Sjohpow01 uintptr_t cur_pa; 570*f19dc624Sjohpow01 uintptr_t last_gran_pa; 571*f19dc624Sjohpow01 uint64_t *l0_gpt_base; 572*f19dc624Sjohpow01 uint64_t *l1_gpt_arr; 573*f19dc624Sjohpow01 unsigned int l0_idx; 574*f19dc624Sjohpow01 575*f19dc624Sjohpow01 assert(gpt_config.plat_gpt_l0_base != 0U); 576*f19dc624Sjohpow01 assert(pas != NULL); 577*f19dc624Sjohpow01 578*f19dc624Sjohpow01 /* 579*f19dc624Sjohpow01 * Checking of PAS parameters has already been done in 580*f19dc624Sjohpow01 * gpt_validate_pas_mappings so no need to check the same things again. 581*f19dc624Sjohpow01 */ 582*f19dc624Sjohpow01 583*f19dc624Sjohpow01 end_pa = pas->base_pa + pas->size; 584*f19dc624Sjohpow01 l0_gpt_base = (uint64_t *)gpt_config.plat_gpt_l0_base; 585*f19dc624Sjohpow01 586*f19dc624Sjohpow01 /* We start working from the granule at base PA */ 587*f19dc624Sjohpow01 cur_pa = pas->base_pa; 588*f19dc624Sjohpow01 589*f19dc624Sjohpow01 /* Iterate over each L0 region in this memory range. */ 590*f19dc624Sjohpow01 for (l0_idx = GPT_L0_IDX(pas->base_pa); 591*f19dc624Sjohpow01 l0_idx <= GPT_L0_IDX(end_pa - 1U); 592*f19dc624Sjohpow01 l0_idx++) { 593*f19dc624Sjohpow01 594*f19dc624Sjohpow01 /* 595*f19dc624Sjohpow01 * See if the L0 entry is already a table descriptor or if we 596*f19dc624Sjohpow01 * need to create one. 597*f19dc624Sjohpow01 */ 598*f19dc624Sjohpow01 if (GPT_L0_TYPE(l0_gpt_base[l0_idx]) == GPT_L0_TYPE_TBL_DESC) { 599*f19dc624Sjohpow01 /* Get the L1 array from the L0 entry. */ 600*f19dc624Sjohpow01 l1_gpt_arr = GPT_L0_TBLD_ADDR(l0_gpt_base[l0_idx]); 601*f19dc624Sjohpow01 } else { 602*f19dc624Sjohpow01 /* Get a new L1 table from the L1 memory space. */ 603*f19dc624Sjohpow01 l1_gpt_arr = gpt_get_new_l1_tbl(); 604*f19dc624Sjohpow01 605*f19dc624Sjohpow01 /* Fill out the L0 descriptor and flush it. */ 606*f19dc624Sjohpow01 l0_gpt_base[l0_idx] = GPT_L0_TBL_DESC(l1_gpt_arr); 607*f19dc624Sjohpow01 } 608*f19dc624Sjohpow01 609*f19dc624Sjohpow01 VERBOSE("[GPT] L0 entry (TABLE) index %u [%p] ==> L1 Addr 0x%llx (0x%llx)\n", 610*f19dc624Sjohpow01 l0_idx, &l0_gpt_base[l0_idx], 611*f19dc624Sjohpow01 (unsigned long long)(l1_gpt_arr), 612*f19dc624Sjohpow01 l0_gpt_base[l0_idx]); 613*f19dc624Sjohpow01 614*f19dc624Sjohpow01 /* 615*f19dc624Sjohpow01 * Determine the PA of the last granule in this L0 descriptor. 616*f19dc624Sjohpow01 */ 617*f19dc624Sjohpow01 last_gran_pa = gpt_get_l1_end_pa(cur_pa, end_pa) - 618*f19dc624Sjohpow01 GPT_PGS_ACTUAL_SIZE(gpt_config.p); 619*f19dc624Sjohpow01 620*f19dc624Sjohpow01 /* 621*f19dc624Sjohpow01 * Fill up L1 GPT entries between these two addresses. This 622*f19dc624Sjohpow01 * function needs the addresses of the first granule and last 623*f19dc624Sjohpow01 * granule in the range. 624*f19dc624Sjohpow01 */ 625*f19dc624Sjohpow01 gpt_fill_l1_tbl(GPT_PAS_ATTR_GPI(pas->attrs), l1_gpt_arr, 626*f19dc624Sjohpow01 cur_pa, last_gran_pa); 627*f19dc624Sjohpow01 628*f19dc624Sjohpow01 /* Advance cur_pa to first granule in next L0 region. */ 629*f19dc624Sjohpow01 cur_pa = gpt_get_l1_end_pa(cur_pa, end_pa); 630*f19dc624Sjohpow01 } 631*f19dc624Sjohpow01 } 632*f19dc624Sjohpow01 633*f19dc624Sjohpow01 /* 634*f19dc624Sjohpow01 * This function flushes a range of L0 descriptors used by a given PAS region 635*f19dc624Sjohpow01 * array. There is a chance that some unmodified L0 descriptors would be flushed 636*f19dc624Sjohpow01 * in the case that there are "holes" in an array of PAS regions but overall 637*f19dc624Sjohpow01 * this should be faster than individually flushing each modified L0 descriptor 638*f19dc624Sjohpow01 * as they are created. 639*f19dc624Sjohpow01 * 640*f19dc624Sjohpow01 * Parameters 641*f19dc624Sjohpow01 * *pas Pointer to an array of PAS regions. 642*f19dc624Sjohpow01 * pas_count Number of entries in the PAS array. 643*f19dc624Sjohpow01 */ 644*f19dc624Sjohpow01 static void flush_l0_for_pas_array(pas_region_t *pas, unsigned int pas_count) 645*f19dc624Sjohpow01 { 646*f19dc624Sjohpow01 unsigned int idx; 647*f19dc624Sjohpow01 unsigned int start_idx; 648*f19dc624Sjohpow01 unsigned int end_idx; 649*f19dc624Sjohpow01 uint64_t *l0 = (uint64_t *)gpt_config.plat_gpt_l0_base; 650*f19dc624Sjohpow01 651*f19dc624Sjohpow01 assert(pas != NULL); 652*f19dc624Sjohpow01 assert(pas_count > 0); 653*f19dc624Sjohpow01 654*f19dc624Sjohpow01 /* Initial start and end values. */ 655*f19dc624Sjohpow01 start_idx = GPT_L0_IDX(pas[0].base_pa); 656*f19dc624Sjohpow01 end_idx = GPT_L0_IDX(pas[0].base_pa + pas[0].size - 1); 657*f19dc624Sjohpow01 658*f19dc624Sjohpow01 /* Find lowest and highest L0 indices used in this PAS array. */ 659*f19dc624Sjohpow01 for (idx = 1; idx < pas_count; idx++) { 660*f19dc624Sjohpow01 if (GPT_L0_IDX(pas[idx].base_pa) < start_idx) { 661*f19dc624Sjohpow01 start_idx = GPT_L0_IDX(pas[idx].base_pa); 662*f19dc624Sjohpow01 } 663*f19dc624Sjohpow01 if (GPT_L0_IDX(pas[idx].base_pa + pas[idx].size - 1) > end_idx) { 664*f19dc624Sjohpow01 end_idx = GPT_L0_IDX(pas[idx].base_pa + pas[idx].size - 1); 665*f19dc624Sjohpow01 } 666*f19dc624Sjohpow01 } 667*f19dc624Sjohpow01 668*f19dc624Sjohpow01 /* 669*f19dc624Sjohpow01 * Flush all covered L0 descriptors, add 1 because we need to include 670*f19dc624Sjohpow01 * the end index value. 671*f19dc624Sjohpow01 */ 672*f19dc624Sjohpow01 flush_dcache_range((uintptr_t)&l0[start_idx], 673*f19dc624Sjohpow01 ((end_idx + 1) - start_idx) * sizeof(uint64_t)); 674*f19dc624Sjohpow01 } 675*f19dc624Sjohpow01 676*f19dc624Sjohpow01 /* 677*f19dc624Sjohpow01 * Public API to enable granule protection checks once the tables have all been 678*f19dc624Sjohpow01 * initialized. This function is called at first initialization and then again 679*f19dc624Sjohpow01 * later during warm boots of CPU cores. 680*f19dc624Sjohpow01 * 681*f19dc624Sjohpow01 * Return 682*f19dc624Sjohpow01 * Negative Linux error code in the event of a failure, 0 for success. 683*f19dc624Sjohpow01 */ 684*f19dc624Sjohpow01 int gpt_enable(void) 685*f19dc624Sjohpow01 { 686*f19dc624Sjohpow01 u_register_t gpccr_el3; 687*f19dc624Sjohpow01 688*f19dc624Sjohpow01 /* 689*f19dc624Sjohpow01 * Granule tables must be initialised before enabling 690*f19dc624Sjohpow01 * granule protection. 691*f19dc624Sjohpow01 */ 692*f19dc624Sjohpow01 if (gpt_config.plat_gpt_l0_base == 0U) { 693*f19dc624Sjohpow01 ERROR("[GPT] Tables have not been initialized!\n"); 694*f19dc624Sjohpow01 return -EPERM; 695*f19dc624Sjohpow01 } 696*f19dc624Sjohpow01 697*f19dc624Sjohpow01 /* Invalidate any stale TLB entries */ 698*f19dc624Sjohpow01 tlbipaallos(); 699*f19dc624Sjohpow01 dsb(); 700*f19dc624Sjohpow01 701*f19dc624Sjohpow01 /* Write the base address of the L0 tables into GPTBR */ 702*f19dc624Sjohpow01 write_gptbr_el3(((gpt_config.plat_gpt_l0_base >> GPTBR_BADDR_VAL_SHIFT) 703*f19dc624Sjohpow01 >> GPTBR_BADDR_SHIFT) & GPTBR_BADDR_MASK); 704*f19dc624Sjohpow01 705*f19dc624Sjohpow01 /* GPCCR_EL3.PPS */ 706*f19dc624Sjohpow01 gpccr_el3 = SET_GPCCR_PPS(gpt_config.pps); 707*f19dc624Sjohpow01 708*f19dc624Sjohpow01 /* GPCCR_EL3.PGS */ 709*f19dc624Sjohpow01 gpccr_el3 |= SET_GPCCR_PGS(gpt_config.pgs); 710*f19dc624Sjohpow01 711*f19dc624Sjohpow01 /* Set shareability attribute to Outher Shareable */ 712*f19dc624Sjohpow01 gpccr_el3 |= SET_GPCCR_SH(GPCCR_SH_OS); 713*f19dc624Sjohpow01 714*f19dc624Sjohpow01 /* Outer and Inner cacheability set to Normal memory, WB, RA, WA. */ 715*f19dc624Sjohpow01 gpccr_el3 |= SET_GPCCR_ORGN(GPCCR_ORGN_WB_RA_WA); 716*f19dc624Sjohpow01 gpccr_el3 |= SET_GPCCR_IRGN(GPCCR_IRGN_WB_RA_WA); 717*f19dc624Sjohpow01 718*f19dc624Sjohpow01 /* Enable GPT */ 719*f19dc624Sjohpow01 gpccr_el3 |= GPCCR_GPC_BIT; 720*f19dc624Sjohpow01 721*f19dc624Sjohpow01 /* TODO: Configure GPCCR_EL3_GPCP for Fault control. */ 722*f19dc624Sjohpow01 write_gpccr_el3(gpccr_el3); 723*f19dc624Sjohpow01 tlbipaallos(); 724*f19dc624Sjohpow01 dsb(); 725*f19dc624Sjohpow01 isb(); 726*f19dc624Sjohpow01 727*f19dc624Sjohpow01 return 0; 728*f19dc624Sjohpow01 } 729*f19dc624Sjohpow01 730*f19dc624Sjohpow01 /* 731*f19dc624Sjohpow01 * Public API to disable granule protection checks. 732*f19dc624Sjohpow01 */ 733*f19dc624Sjohpow01 void gpt_disable(void) 734*f19dc624Sjohpow01 { 735*f19dc624Sjohpow01 u_register_t gpccr_el3 = read_gpccr_el3(); 736*f19dc624Sjohpow01 737*f19dc624Sjohpow01 write_gpccr_el3(gpccr_el3 & ~GPCCR_GPC_BIT); 738*f19dc624Sjohpow01 dsbsy(); 739*f19dc624Sjohpow01 isb(); 740*f19dc624Sjohpow01 } 741*f19dc624Sjohpow01 742*f19dc624Sjohpow01 /* 743*f19dc624Sjohpow01 * Public API that initializes the entire protected space to GPT_GPI_ANY using 744*f19dc624Sjohpow01 * the L0 tables (block descriptors). Ideally, this function is invoked prior 745*f19dc624Sjohpow01 * to DDR discovery and initialization. The MMU must be initialized before 746*f19dc624Sjohpow01 * calling this function. 747*f19dc624Sjohpow01 * 748*f19dc624Sjohpow01 * Parameters 749*f19dc624Sjohpow01 * pps PPS value to use for table generation 750*f19dc624Sjohpow01 * l0_mem_base Base address of L0 tables in memory. 751*f19dc624Sjohpow01 * l0_mem_size Total size of memory available for L0 tables. 752*f19dc624Sjohpow01 * 753*f19dc624Sjohpow01 * Return 754*f19dc624Sjohpow01 * Negative Linux error code in the event of a failure, 0 for success. 755*f19dc624Sjohpow01 */ 756*f19dc624Sjohpow01 int gpt_init_l0_tables(unsigned int pps, uintptr_t l0_mem_base, 757*f19dc624Sjohpow01 size_t l0_mem_size) 758*f19dc624Sjohpow01 { 759*f19dc624Sjohpow01 int ret; 760*f19dc624Sjohpow01 uint64_t gpt_desc; 761*f19dc624Sjohpow01 762*f19dc624Sjohpow01 /* Ensure that MMU and caches are enabled. */ 763*f19dc624Sjohpow01 assert((read_sctlr_el3() & SCTLR_C_BIT) != 0U); 764*f19dc624Sjohpow01 765*f19dc624Sjohpow01 /* Validate other parameters. */ 766*f19dc624Sjohpow01 ret = gpt_validate_l0_params(pps, l0_mem_base, l0_mem_size); 767*f19dc624Sjohpow01 if (ret < 0) { 768*f19dc624Sjohpow01 return ret; 769*f19dc624Sjohpow01 } 770*f19dc624Sjohpow01 771*f19dc624Sjohpow01 /* Create the descriptor to initialize L0 entries with. */ 772*f19dc624Sjohpow01 gpt_desc = GPT_L0_BLK_DESC(GPT_GPI_ANY); 773*f19dc624Sjohpow01 774*f19dc624Sjohpow01 /* Iterate through all L0 entries */ 775*f19dc624Sjohpow01 for (unsigned int i = 0U; i < GPT_L0_REGION_COUNT(gpt_config.t); i++) { 776*f19dc624Sjohpow01 ((uint64_t *)l0_mem_base)[i] = gpt_desc; 777*f19dc624Sjohpow01 } 778*f19dc624Sjohpow01 779*f19dc624Sjohpow01 /* Flush updated L0 tables to memory. */ 780*f19dc624Sjohpow01 flush_dcache_range((uintptr_t)l0_mem_base, 781*f19dc624Sjohpow01 (size_t)GPT_L0_TABLE_SIZE(gpt_config.t)); 782*f19dc624Sjohpow01 783*f19dc624Sjohpow01 /* Stash the L0 base address once initial setup is complete. */ 784*f19dc624Sjohpow01 gpt_config.plat_gpt_l0_base = l0_mem_base; 785*f19dc624Sjohpow01 786*f19dc624Sjohpow01 return 0; 787*f19dc624Sjohpow01 } 788*f19dc624Sjohpow01 789*f19dc624Sjohpow01 /* 790*f19dc624Sjohpow01 * Public API that carves out PAS regions from the L0 tables and builds any L1 791*f19dc624Sjohpow01 * tables that are needed. This function ideally is run after DDR discovery and 792*f19dc624Sjohpow01 * initialization. The L0 tables must have already been initialized to GPI_ANY 793*f19dc624Sjohpow01 * when this function is called. 794*f19dc624Sjohpow01 * 795*f19dc624Sjohpow01 * This function can be called multiple times with different L1 memory ranges 796*f19dc624Sjohpow01 * and PAS regions if it is desirable to place L1 tables in different locations 797*f19dc624Sjohpow01 * in memory. (ex: you have multiple DDR banks and want to place the L1 tables 798*f19dc624Sjohpow01 * in the DDR bank that they control) 799*f19dc624Sjohpow01 * 800*f19dc624Sjohpow01 * Parameters 801*f19dc624Sjohpow01 * pgs PGS value to use for table generation. 802*f19dc624Sjohpow01 * l1_mem_base Base address of memory used for L1 tables. 803*f19dc624Sjohpow01 * l1_mem_size Total size of memory available for L1 tables. 804*f19dc624Sjohpow01 * *pas_regions Pointer to PAS regions structure array. 805*f19dc624Sjohpow01 * pas_count Total number of PAS regions. 806*f19dc624Sjohpow01 * 807*f19dc624Sjohpow01 * Return 808*f19dc624Sjohpow01 * Negative Linux error code in the event of a failure, 0 for success. 809*f19dc624Sjohpow01 */ 810*f19dc624Sjohpow01 int gpt_init_pas_l1_tables(gpccr_pgs_e pgs, uintptr_t l1_mem_base, 811*f19dc624Sjohpow01 size_t l1_mem_size, pas_region_t *pas_regions, 812*f19dc624Sjohpow01 unsigned int pas_count) 813*f19dc624Sjohpow01 { 814*f19dc624Sjohpow01 int ret; 815*f19dc624Sjohpow01 int l1_gpt_cnt; 816*f19dc624Sjohpow01 817*f19dc624Sjohpow01 /* Ensure that MMU and caches are enabled. */ 818*f19dc624Sjohpow01 assert((read_sctlr_el3() & SCTLR_C_BIT) != 0U); 819*f19dc624Sjohpow01 820*f19dc624Sjohpow01 /* PGS is needed for gpt_validate_pas_mappings so check it now. */ 821*f19dc624Sjohpow01 if (pgs > GPT_PGS_MAX) { 822*f19dc624Sjohpow01 ERROR("[GPT] Invalid PGS: 0x%x\n", pgs); 823*f19dc624Sjohpow01 return -EINVAL; 824*f19dc624Sjohpow01 } 825*f19dc624Sjohpow01 gpt_config.pgs = pgs; 826*f19dc624Sjohpow01 gpt_config.p = gpt_p_lookup[pgs]; 827*f19dc624Sjohpow01 828*f19dc624Sjohpow01 /* Make sure L0 tables have been initialized. */ 829*f19dc624Sjohpow01 if (gpt_config.plat_gpt_l0_base == 0U) { 830*f19dc624Sjohpow01 ERROR("[GPT] L0 tables must be initialized first!\n"); 831*f19dc624Sjohpow01 return -EPERM; 832*f19dc624Sjohpow01 } 833*f19dc624Sjohpow01 834*f19dc624Sjohpow01 /* Check if L1 GPTs are required and how many. */ 835*f19dc624Sjohpow01 l1_gpt_cnt = gpt_validate_pas_mappings(pas_regions, pas_count); 836*f19dc624Sjohpow01 if (l1_gpt_cnt < 0) { 837*f19dc624Sjohpow01 return l1_gpt_cnt; 838*f19dc624Sjohpow01 } 839*f19dc624Sjohpow01 840*f19dc624Sjohpow01 VERBOSE("[GPT] %u L1 GPTs requested.\n", l1_gpt_cnt); 841*f19dc624Sjohpow01 842*f19dc624Sjohpow01 /* If L1 tables are needed then validate the L1 parameters. */ 843*f19dc624Sjohpow01 if (l1_gpt_cnt > 0) { 844*f19dc624Sjohpow01 ret = gpt_validate_l1_params(l1_mem_base, l1_mem_size, 845*f19dc624Sjohpow01 l1_gpt_cnt); 846*f19dc624Sjohpow01 if (ret < 0) { 847*f19dc624Sjohpow01 return ret; 848*f19dc624Sjohpow01 } 849*f19dc624Sjohpow01 850*f19dc624Sjohpow01 /* Set up parameters for L1 table generation. */ 851*f19dc624Sjohpow01 gpt_l1_tbl = l1_mem_base; 852*f19dc624Sjohpow01 gpt_next_l1_tbl_idx = 0U; 853*f19dc624Sjohpow01 } 854*f19dc624Sjohpow01 855*f19dc624Sjohpow01 INFO("[GPT] Boot Configuration\n"); 856*f19dc624Sjohpow01 INFO(" PPS/T: 0x%x/%u\n", gpt_config.pps, gpt_config.t); 857*f19dc624Sjohpow01 INFO(" PGS/P: 0x%x/%u\n", gpt_config.pgs, gpt_config.p); 858*f19dc624Sjohpow01 INFO(" L0GPTSZ/S: 0x%x/%u\n", GPT_L0GPTSZ, GPT_S_VAL); 859*f19dc624Sjohpow01 INFO(" PAS count: 0x%x\n", pas_count); 860*f19dc624Sjohpow01 INFO(" L0 base: 0x%lx\n", gpt_config.plat_gpt_l0_base); 861*f19dc624Sjohpow01 862*f19dc624Sjohpow01 /* Generate the tables in memory. */ 863*f19dc624Sjohpow01 for (unsigned int idx = 0U; idx < pas_count; idx++) { 864*f19dc624Sjohpow01 INFO("[GPT] PAS[%u]: base 0x%lx, size 0x%lx, GPI 0x%x, type 0x%x\n", 865*f19dc624Sjohpow01 idx, pas_regions[idx].base_pa, pas_regions[idx].size, 866*f19dc624Sjohpow01 GPT_PAS_ATTR_GPI(pas_regions[idx].attrs), 867*f19dc624Sjohpow01 GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs)); 868*f19dc624Sjohpow01 869*f19dc624Sjohpow01 /* Check if a block or table descriptor is required */ 870*f19dc624Sjohpow01 if (GPT_PAS_ATTR_MAP_TYPE(pas_regions[idx].attrs) == 871*f19dc624Sjohpow01 GPT_PAS_ATTR_MAP_TYPE_BLOCK) { 872*f19dc624Sjohpow01 gpt_generate_l0_blk_desc(&pas_regions[idx]); 873*f19dc624Sjohpow01 874*f19dc624Sjohpow01 } else { 875*f19dc624Sjohpow01 gpt_generate_l0_tbl_desc(&pas_regions[idx]); 876*f19dc624Sjohpow01 } 877*f19dc624Sjohpow01 } 878*f19dc624Sjohpow01 879*f19dc624Sjohpow01 /* Flush modified L0 tables. */ 880*f19dc624Sjohpow01 flush_l0_for_pas_array(pas_regions, pas_count); 881*f19dc624Sjohpow01 882*f19dc624Sjohpow01 /* Flush L1 tables if needed. */ 883*f19dc624Sjohpow01 if (l1_gpt_cnt > 0) { 884*f19dc624Sjohpow01 flush_dcache_range(l1_mem_base, 885*f19dc624Sjohpow01 GPT_L1_TABLE_SIZE(gpt_config.p) * 886*f19dc624Sjohpow01 l1_gpt_cnt); 887*f19dc624Sjohpow01 } 888*f19dc624Sjohpow01 889*f19dc624Sjohpow01 /* Make sure that all the entries are written to the memory. */ 890*f19dc624Sjohpow01 dsbishst(); 891*f19dc624Sjohpow01 892*f19dc624Sjohpow01 return 0; 893*f19dc624Sjohpow01 } 894*f19dc624Sjohpow01 895*f19dc624Sjohpow01 /* 896*f19dc624Sjohpow01 * Public API to initialize the runtime gpt_config structure based on the values 897*f19dc624Sjohpow01 * present in the GPTBR_EL3 and GPCCR_EL3 registers. GPT initialization 898*f19dc624Sjohpow01 * typically happens in a bootloader stage prior to setting up the EL3 runtime 899*f19dc624Sjohpow01 * environment for the granule transition service so this function detects the 900*f19dc624Sjohpow01 * initialization from a previous stage. Granule protection checks must be 901*f19dc624Sjohpow01 * enabled already or this function will return an error. 902*f19dc624Sjohpow01 * 903*f19dc624Sjohpow01 * Return 904*f19dc624Sjohpow01 * Negative Linux error code in the event of a failure, 0 for success. 905*f19dc624Sjohpow01 */ 906*f19dc624Sjohpow01 int gpt_runtime_init(void) 907*f19dc624Sjohpow01 { 908*f19dc624Sjohpow01 u_register_t reg; 909*f19dc624Sjohpow01 910*f19dc624Sjohpow01 /* Ensure that MMU and caches are enabled. */ 911*f19dc624Sjohpow01 assert((read_sctlr_el3() & SCTLR_C_BIT) != 0U); 912*f19dc624Sjohpow01 913*f19dc624Sjohpow01 /* Ensure GPC are already enabled. */ 914*f19dc624Sjohpow01 if ((read_gpccr_el3() & GPCCR_GPC_BIT) == 0U) { 915*f19dc624Sjohpow01 ERROR("[GPT] Granule protection checks are not enabled!\n"); 916*f19dc624Sjohpow01 return -EPERM; 917*f19dc624Sjohpow01 } 918*f19dc624Sjohpow01 919*f19dc624Sjohpow01 /* 920*f19dc624Sjohpow01 * Read the L0 table address from GPTBR, we don't need the L1 base 921*f19dc624Sjohpow01 * address since those are included in the L0 tables as needed. 922*f19dc624Sjohpow01 */ 923*f19dc624Sjohpow01 reg = read_gptbr_el3(); 924*f19dc624Sjohpow01 gpt_config.plat_gpt_l0_base = ((reg >> GPTBR_BADDR_SHIFT) & 925*f19dc624Sjohpow01 GPTBR_BADDR_MASK) << 926*f19dc624Sjohpow01 GPTBR_BADDR_VAL_SHIFT; 927*f19dc624Sjohpow01 928*f19dc624Sjohpow01 /* Read GPCCR to get PGS and PPS values. */ 929*f19dc624Sjohpow01 reg = read_gpccr_el3(); 930*f19dc624Sjohpow01 gpt_config.pps = (reg >> GPCCR_PPS_SHIFT) & GPCCR_PPS_MASK; 931*f19dc624Sjohpow01 gpt_config.t = gpt_t_lookup[gpt_config.pps]; 932*f19dc624Sjohpow01 gpt_config.pgs = (reg >> GPCCR_PGS_SHIFT) & GPCCR_PGS_MASK; 933*f19dc624Sjohpow01 gpt_config.p = gpt_p_lookup[gpt_config.pgs]; 934*f19dc624Sjohpow01 935*f19dc624Sjohpow01 VERBOSE("[GPT] Runtime Configuration\n"); 936*f19dc624Sjohpow01 VERBOSE(" PPS/T: 0x%x/%u\n", gpt_config.pps, gpt_config.t); 937*f19dc624Sjohpow01 VERBOSE(" PGS/P: 0x%x/%u\n", gpt_config.pgs, gpt_config.p); 938*f19dc624Sjohpow01 VERBOSE(" L0GPTSZ/S: 0x%x/%u\n", GPT_L0GPTSZ, GPT_S_VAL); 939*f19dc624Sjohpow01 VERBOSE(" L0 base: 0x%lx\n", gpt_config.plat_gpt_l0_base); 940*f19dc624Sjohpow01 941*f19dc624Sjohpow01 return 0; 942*f19dc624Sjohpow01 } 943*f19dc624Sjohpow01 944*f19dc624Sjohpow01 /* 945*f19dc624Sjohpow01 * The L1 descriptors are protected by a spinlock to ensure that multiple 946*f19dc624Sjohpow01 * CPUs do not attempt to change the descriptors at once. In the future it 947*f19dc624Sjohpow01 * would be better to have separate spinlocks for each L1 descriptor. 948*f19dc624Sjohpow01 */ 949*f19dc624Sjohpow01 static spinlock_t gpt_lock; 950*f19dc624Sjohpow01 951*f19dc624Sjohpow01 /* 952*f19dc624Sjohpow01 * Check if caller is allowed to transition a PAS. 953*f19dc624Sjohpow01 * 954*f19dc624Sjohpow01 * - Secure world caller can only request S <-> NS transitions on a 955*f19dc624Sjohpow01 * granule that is already in either S or NS PAS. 956*f19dc624Sjohpow01 * 957*f19dc624Sjohpow01 * - Realm world caller can only request R <-> NS transitions on a 958*f19dc624Sjohpow01 * granule that is already in either R or NS PAS. 959*f19dc624Sjohpow01 * 960*f19dc624Sjohpow01 * Parameters 961*f19dc624Sjohpow01 * src_sec_state Security state of the caller. 962*f19dc624Sjohpow01 * current_gpi Current GPI of the granule. 963*f19dc624Sjohpow01 * target_gpi Requested new GPI for the granule. 964*f19dc624Sjohpow01 * 965*f19dc624Sjohpow01 * Return 966*f19dc624Sjohpow01 * Negative Linux error code in the event of a failure, 0 for success. 967*f19dc624Sjohpow01 */ 968*f19dc624Sjohpow01 static int gpt_check_transition_gpi(unsigned int src_sec_state, 969*f19dc624Sjohpow01 unsigned int current_gpi, 970*f19dc624Sjohpow01 unsigned int target_gpi) 971*f19dc624Sjohpow01 { 972*f19dc624Sjohpow01 unsigned int check_gpi; 973*f19dc624Sjohpow01 974*f19dc624Sjohpow01 /* Cannot transition a granule to the state it is already in. */ 975*f19dc624Sjohpow01 if (current_gpi == target_gpi) { 976*f19dc624Sjohpow01 return -EINVAL; 977*f19dc624Sjohpow01 } 978*f19dc624Sjohpow01 979*f19dc624Sjohpow01 /* Check security state, only secure and realm can transition. */ 980*f19dc624Sjohpow01 if (src_sec_state == SMC_FROM_REALM) { 981*f19dc624Sjohpow01 check_gpi = GPT_GPI_REALM; 982*f19dc624Sjohpow01 } else if (src_sec_state == SMC_FROM_SECURE) { 983*f19dc624Sjohpow01 check_gpi = GPT_GPI_SECURE; 984*f19dc624Sjohpow01 } else { 985*f19dc624Sjohpow01 return -EINVAL; 986*f19dc624Sjohpow01 } 987*f19dc624Sjohpow01 988*f19dc624Sjohpow01 /* Make sure security state is allowed to make the transition. */ 989*f19dc624Sjohpow01 if ((target_gpi != check_gpi) && (target_gpi != GPT_GPI_NS)) { 990*f19dc624Sjohpow01 return -EINVAL; 991*f19dc624Sjohpow01 } 992*f19dc624Sjohpow01 if ((current_gpi != check_gpi) && (current_gpi != GPT_GPI_NS)) { 993*f19dc624Sjohpow01 return -EINVAL; 994*f19dc624Sjohpow01 } 995*f19dc624Sjohpow01 996*f19dc624Sjohpow01 return 0; 997*f19dc624Sjohpow01 } 998*f19dc624Sjohpow01 999*f19dc624Sjohpow01 /* 1000*f19dc624Sjohpow01 * This function is the core of the granule transition service. When a granule 1001*f19dc624Sjohpow01 * transition request occurs it is routed to this function where the request is 1002*f19dc624Sjohpow01 * validated then fulfilled if possible. 1003*f19dc624Sjohpow01 * 1004*f19dc624Sjohpow01 * TODO: implement support for transitioning multiple granules at once. 1005*f19dc624Sjohpow01 * 1006*f19dc624Sjohpow01 * Parameters 1007*f19dc624Sjohpow01 * base Base address of the region to transition, must be 1008*f19dc624Sjohpow01 * aligned to granule size. 1009*f19dc624Sjohpow01 * size Size of region to transition, must be aligned to granule 1010*f19dc624Sjohpow01 * size. 1011*f19dc624Sjohpow01 * src_sec_state Security state of the caller. 1012*f19dc624Sjohpow01 * target_pas Target PAS of the specified memory region. 1013*f19dc624Sjohpow01 * 1014*f19dc624Sjohpow01 * Return 1015*f19dc624Sjohpow01 * Negative Linux error code in the event of a failure, 0 for success. 1016*f19dc624Sjohpow01 */ 1017*f19dc624Sjohpow01 int gpt_transition_pas(uint64_t base, size_t size, unsigned int src_sec_state, 1018*f19dc624Sjohpow01 unsigned int target_pas) 1019*f19dc624Sjohpow01 { 1020*f19dc624Sjohpow01 int idx; 1021*f19dc624Sjohpow01 unsigned int gpi_shift; 1022*f19dc624Sjohpow01 unsigned int gpi; 1023*f19dc624Sjohpow01 uint64_t gpt_l0_desc; 1024*f19dc624Sjohpow01 uint64_t gpt_l1_desc; 1025*f19dc624Sjohpow01 uint64_t *gpt_l1_addr; 1026*f19dc624Sjohpow01 uint64_t *gpt_l0_base; 1027*f19dc624Sjohpow01 1028*f19dc624Sjohpow01 /* Ensure that the tables have been set up before taking requests. */ 1029*f19dc624Sjohpow01 assert(gpt_config.plat_gpt_l0_base != 0U); 1030*f19dc624Sjohpow01 1031*f19dc624Sjohpow01 /* Check for address range overflow. */ 1032*f19dc624Sjohpow01 if ((ULONG_MAX - base) < size) { 1033*f19dc624Sjohpow01 VERBOSE("[GPT] Transition request address overflow!\n"); 1034*f19dc624Sjohpow01 VERBOSE(" Base=0x%llx\n", base); 1035*f19dc624Sjohpow01 VERBOSE(" Size=0x%lx\n", size); 1036*f19dc624Sjohpow01 return -EINVAL; 1037*f19dc624Sjohpow01 } 1038*f19dc624Sjohpow01 1039*f19dc624Sjohpow01 /* Make sure base and size are valid. */ 1040*f19dc624Sjohpow01 if (((base & (GPT_PGS_ACTUAL_SIZE(gpt_config.p) - 1)) != 0U) || 1041*f19dc624Sjohpow01 ((size & (GPT_PGS_ACTUAL_SIZE(gpt_config.p) - 1)) != 0U) || 1042*f19dc624Sjohpow01 (size == 0U) || 1043*f19dc624Sjohpow01 ((base + size) >= GPT_PPS_ACTUAL_SIZE(gpt_config.t))) { 1044*f19dc624Sjohpow01 VERBOSE("[GPT] Invalid granule transition address range!\n"); 1045*f19dc624Sjohpow01 VERBOSE(" Base=0x%llx\n", base); 1046*f19dc624Sjohpow01 VERBOSE(" Size=0x%lx\n", size); 1047*f19dc624Sjohpow01 return -EINVAL; 1048*f19dc624Sjohpow01 } 1049*f19dc624Sjohpow01 1050*f19dc624Sjohpow01 /* See if this is a single granule transition or a range of granules. */ 1051*f19dc624Sjohpow01 if (size != GPT_PGS_ACTUAL_SIZE(gpt_config.p)) { 1052*f19dc624Sjohpow01 /* 1053*f19dc624Sjohpow01 * TODO: Add support for transitioning multiple granules with a 1054*f19dc624Sjohpow01 * single call to this function. 1055*f19dc624Sjohpow01 */ 1056*f19dc624Sjohpow01 panic(); 1057*f19dc624Sjohpow01 } 1058*f19dc624Sjohpow01 1059*f19dc624Sjohpow01 /* Get the L0 descriptor and make sure it is for a table. */ 1060*f19dc624Sjohpow01 gpt_l0_base = (uint64_t *)gpt_config.plat_gpt_l0_base; 1061*f19dc624Sjohpow01 gpt_l0_desc = gpt_l0_base[GPT_L0_IDX(base)]; 1062*f19dc624Sjohpow01 if (GPT_L0_TYPE(gpt_l0_desc) != GPT_L0_TYPE_TBL_DESC) { 1063*f19dc624Sjohpow01 VERBOSE("[GPT] Granule is not covered by a table descriptor!\n"); 1064*f19dc624Sjohpow01 VERBOSE(" Base=0x%llx\n", base); 1065*f19dc624Sjohpow01 return -EINVAL; 1066*f19dc624Sjohpow01 } 1067*f19dc624Sjohpow01 1068*f19dc624Sjohpow01 /* Get the table index and GPI shift from PA. */ 1069*f19dc624Sjohpow01 gpt_l1_addr = GPT_L0_TBLD_ADDR(gpt_l0_desc); 1070*f19dc624Sjohpow01 idx = GPT_L1_IDX(gpt_config.p, base); 1071*f19dc624Sjohpow01 gpi_shift = GPT_L1_GPI_IDX(gpt_config.p, base) << 2; 1072*f19dc624Sjohpow01 1073*f19dc624Sjohpow01 /* 1074*f19dc624Sjohpow01 * Access to L1 tables is controlled by a global lock to ensure 1075*f19dc624Sjohpow01 * that no more than one CPU is allowed to make changes at any 1076*f19dc624Sjohpow01 * given time. 1077*f19dc624Sjohpow01 */ 1078*f19dc624Sjohpow01 spin_lock(&gpt_lock); 1079*f19dc624Sjohpow01 gpt_l1_desc = gpt_l1_addr[idx]; 1080*f19dc624Sjohpow01 gpi = (gpt_l1_desc >> gpi_shift) & GPT_L1_GRAN_DESC_GPI_MASK; 1081*f19dc624Sjohpow01 1082*f19dc624Sjohpow01 /* Make sure caller state and source/target PAS are allowed. */ 1083*f19dc624Sjohpow01 if (gpt_check_transition_gpi(src_sec_state, gpi, target_pas) < 0) { 1084*f19dc624Sjohpow01 spin_unlock(&gpt_lock); 1085*f19dc624Sjohpow01 VERBOSE("[GPT] Invalid caller state and PAS combo!\n"); 1086*f19dc624Sjohpow01 VERBOSE(" Caller: %u, Current GPI: %u, Target GPI: %u\n", 1087*f19dc624Sjohpow01 src_sec_state, gpi, target_pas); 1088*f19dc624Sjohpow01 return -EPERM; 1089*f19dc624Sjohpow01 } 1090*f19dc624Sjohpow01 1091*f19dc624Sjohpow01 /* Clear existing GPI encoding and transition granule. */ 1092*f19dc624Sjohpow01 gpt_l1_desc &= ~(GPT_L1_GRAN_DESC_GPI_MASK << gpi_shift); 1093*f19dc624Sjohpow01 gpt_l1_desc |= ((uint64_t)target_pas << gpi_shift); 1094*f19dc624Sjohpow01 gpt_l1_addr[idx] = gpt_l1_desc; 1095*f19dc624Sjohpow01 1096*f19dc624Sjohpow01 /* Ensure that the write operation happens before the unlock. */ 1097*f19dc624Sjohpow01 dmbishst(); 1098*f19dc624Sjohpow01 1099*f19dc624Sjohpow01 /* Unlock access to the L1 tables. */ 1100*f19dc624Sjohpow01 spin_unlock(&gpt_lock); 1101*f19dc624Sjohpow01 1102*f19dc624Sjohpow01 /* Cache maintenance. */ 1103*f19dc624Sjohpow01 clean_dcache_range((uintptr_t)&gpt_l1_addr[idx], 1104*f19dc624Sjohpow01 sizeof(uint64_t)); 1105*f19dc624Sjohpow01 gpt_tlbi_by_pa(base, GPT_PGS_ACTUAL_SIZE(gpt_config.p)); 1106*f19dc624Sjohpow01 dsbishst(); 1107*f19dc624Sjohpow01 1108*f19dc624Sjohpow01 VERBOSE("[GPT] Granule 0x%llx, GPI 0x%x->0x%x\n", base, gpi, 1109*f19dc624Sjohpow01 target_pas); 1110*f19dc624Sjohpow01 1111*f19dc624Sjohpow01 return 0; 1112*f19dc624Sjohpow01 } 1113