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