Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 1 | /* |
Manish V Badarkhe | 173c296 | 2022-05-09 21:55:19 +0100 | [diff] [blame] | 2 | * Copyright (c) 2015-2022, Arm Limited and Contributors. All rights reserved. |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 7 | #include <assert.h> |
| 8 | |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 9 | #include <arch.h> |
| 10 | #include <arch_helpers.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 11 | #include <common/debug.h> |
| 12 | #include <common/interrupt_props.h> |
| 13 | #include <drivers/arm/gic_common.h> |
| 14 | |
Claus Pedersen | 785e66c | 2022-09-12 22:42:58 +0000 | [diff] [blame] | 15 | #include <platform_def.h> |
| 16 | |
Soby Mathew | 50f6fe4 | 2016-02-01 17:59:22 +0000 | [diff] [blame] | 17 | #include "../common/gic_common_private.h" |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 18 | #include "gicv3_private.h" |
| 19 | |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 20 | /****************************************************************************** |
| 21 | * This function marks the core as awake in the re-distributor and |
| 22 | * ensures that the interface is active. |
| 23 | *****************************************************************************/ |
| 24 | void gicv3_rdistif_mark_core_awake(uintptr_t gicr_base) |
| 25 | { |
| 26 | /* |
| 27 | * The WAKER_PS_BIT should be changed to 0 |
| 28 | * only when WAKER_CA_BIT is 1. |
| 29 | */ |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 30 | assert((gicr_read_waker(gicr_base) & WAKER_CA_BIT) != 0U); |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 31 | |
| 32 | /* Mark the connected core as awake */ |
| 33 | gicr_write_waker(gicr_base, gicr_read_waker(gicr_base) & ~WAKER_PS_BIT); |
| 34 | |
| 35 | /* Wait till the WAKER_CA_BIT changes to 0 */ |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 36 | while ((gicr_read_waker(gicr_base) & WAKER_CA_BIT) != 0U) { |
| 37 | } |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 38 | } |
| 39 | |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 40 | /****************************************************************************** |
| 41 | * This function marks the core as asleep in the re-distributor and ensures |
| 42 | * that the interface is quiescent. |
| 43 | *****************************************************************************/ |
| 44 | void gicv3_rdistif_mark_core_asleep(uintptr_t gicr_base) |
| 45 | { |
| 46 | /* Mark the connected core as asleep */ |
| 47 | gicr_write_waker(gicr_base, gicr_read_waker(gicr_base) | WAKER_PS_BIT); |
| 48 | |
| 49 | /* Wait till the WAKER_CA_BIT changes to 1 */ |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 50 | while ((gicr_read_waker(gicr_base) & WAKER_CA_BIT) == 0U) { |
| 51 | } |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 54 | /******************************************************************************* |
| 55 | * This function probes the Redistributor frames when the driver is initialised |
| 56 | * and saves their base addresses. These base addresses are used later to |
| 57 | * initialise each Redistributor interface. |
| 58 | ******************************************************************************/ |
| 59 | void gicv3_rdistif_base_addrs_probe(uintptr_t *rdistif_base_addrs, |
| 60 | unsigned int rdistif_num, |
| 61 | uintptr_t gicr_base, |
| 62 | mpidr_hash_fn mpidr_to_core_pos) |
| 63 | { |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 64 | u_register_t mpidr; |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 65 | unsigned int proc_num; |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 66 | uint64_t typer_val; |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 67 | uintptr_t rdistif_base = gicr_base; |
| 68 | |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 69 | assert(rdistif_base_addrs != NULL); |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * Iterate over the Redistributor frames. Store the base address of each |
| 73 | * frame in the platform provided array. Use the "Processor Number" |
| 74 | * field to index into the array if the platform has not provided a hash |
| 75 | * function to convert an MPIDR (obtained from the "Affinity Value" |
| 76 | * field into a linear index. |
| 77 | */ |
| 78 | do { |
| 79 | typer_val = gicr_read_typer(rdistif_base); |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 80 | if (mpidr_to_core_pos != NULL) { |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 81 | mpidr = mpidr_from_gicr_typer(typer_val); |
| 82 | proc_num = mpidr_to_core_pos(mpidr); |
| 83 | } else { |
| 84 | proc_num = (typer_val >> TYPER_PROC_NUM_SHIFT) & |
| 85 | TYPER_PROC_NUM_MASK; |
| 86 | } |
Soby Mathew | d1463bd | 2019-01-17 14:57:54 +0000 | [diff] [blame] | 87 | |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 88 | if (proc_num < rdistif_num) { |
Soby Mathew | d1463bd | 2019-01-17 14:57:54 +0000 | [diff] [blame] | 89 | rdistif_base_addrs[proc_num] = rdistif_base; |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 90 | } |
Andre Przywara | f70f4b9 | 2021-05-18 15:51:06 +0100 | [diff] [blame] | 91 | rdistif_base += gicv3_redist_size(typer_val); |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 92 | } while ((typer_val & TYPER_LAST_BIT) == 0U); |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /******************************************************************************* |
Heyi Guo | 06f85b4 | 2021-01-20 18:50:16 +0800 | [diff] [blame] | 96 | * Helper function to get the maximum SPI INTID + 1. |
| 97 | ******************************************************************************/ |
| 98 | unsigned int gicv3_get_spi_limit(uintptr_t gicd_base) |
| 99 | { |
| 100 | unsigned int spi_limit; |
| 101 | unsigned int typer_reg = gicd_read_typer(gicd_base); |
| 102 | |
| 103 | /* (maximum SPI INTID + 1) is equal to 32 * (GICD_TYPER.ITLinesNumber+1) */ |
| 104 | spi_limit = ((typer_reg & TYPER_IT_LINES_NO_MASK) + 1U) << 5; |
| 105 | |
| 106 | /* Filter out special INTIDs 1020-1023 */ |
| 107 | if (spi_limit > (MAX_SPI_ID + 1U)) { |
| 108 | return MAX_SPI_ID + 1U; |
| 109 | } |
| 110 | |
| 111 | return spi_limit; |
| 112 | } |
| 113 | |
Heyi Guo | 60ce825 | 2021-01-20 18:50:16 +0800 | [diff] [blame] | 114 | #if GIC_EXT_INTID |
| 115 | /******************************************************************************* |
| 116 | * Helper function to get the maximum ESPI INTID + 1. |
| 117 | ******************************************************************************/ |
| 118 | unsigned int gicv3_get_espi_limit(uintptr_t gicd_base) |
| 119 | { |
| 120 | unsigned int typer_reg = gicd_read_typer(gicd_base); |
| 121 | |
| 122 | /* Check if extended SPI range is implemented */ |
| 123 | if ((typer_reg & TYPER_ESPI) != 0U) { |
| 124 | /* |
| 125 | * (maximum ESPI INTID + 1) is equal to |
| 126 | * 32 * (GICD_TYPER.ESPI_range + 1) + 4096 |
| 127 | */ |
| 128 | return ((((typer_reg >> TYPER_ESPI_RANGE_SHIFT) & |
| 129 | TYPER_ESPI_RANGE_MASK) + 1U) << 5) + MIN_ESPI_ID; |
| 130 | } |
| 131 | |
| 132 | return 0U; |
| 133 | } |
| 134 | #endif /* GIC_EXT_INTID */ |
| 135 | |
Heyi Guo | 06f85b4 | 2021-01-20 18:50:16 +0800 | [diff] [blame] | 136 | /******************************************************************************* |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 137 | * Helper function to configure the default attributes of (E)SPIs. |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 138 | ******************************************************************************/ |
Daniel Boulby | 4e83abb | 2018-05-01 15:15:34 +0100 | [diff] [blame] | 139 | void gicv3_spis_config_defaults(uintptr_t gicd_base) |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 140 | { |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 141 | unsigned int i, num_ints; |
| 142 | #if GIC_EXT_INTID |
| 143 | unsigned int num_eints; |
| 144 | #endif |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 145 | |
Heyi Guo | 79bc7a7 | 2021-01-20 19:05:51 +0800 | [diff] [blame] | 146 | num_ints = gicv3_get_spi_limit(gicd_base); |
Heyi Guo | ce38025 | 2021-01-21 10:34:00 +0800 | [diff] [blame] | 147 | INFO("Maximum SPI INTID supported: %u\n", num_ints - 1); |
Heyi Guo | 0d5d24d | 2020-05-19 15:41:14 +0800 | [diff] [blame] | 148 | |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 149 | /* Treat all (E)SPIs as G1NS by default. We do 32 at a time. */ |
| 150 | for (i = MIN_SPI_ID; i < num_ints; i += (1U << IGROUPR_SHIFT)) { |
| 151 | gicd_write_igroupr(gicd_base, i, ~0U); |
| 152 | } |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 153 | |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 154 | #if GIC_EXT_INTID |
Heyi Guo | 79bc7a7 | 2021-01-20 19:05:51 +0800 | [diff] [blame] | 155 | num_eints = gicv3_get_espi_limit(gicd_base); |
| 156 | if (num_eints != 0U) { |
Heyi Guo | ce38025 | 2021-01-21 10:34:00 +0800 | [diff] [blame] | 157 | INFO("Maximum ESPI INTID supported: %u\n", num_eints - 1); |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 158 | |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 159 | for (i = MIN_ESPI_ID; i < num_eints; |
| 160 | i += (1U << IGROUPR_SHIFT)) { |
| 161 | gicd_write_igroupr(gicd_base, i, ~0U); |
| 162 | } |
| 163 | } else { |
Heyi Guo | ce38025 | 2021-01-21 10:34:00 +0800 | [diff] [blame] | 164 | INFO("ESPI range is not implemented.\n"); |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 165 | } |
| 166 | #endif |
| 167 | |
| 168 | /* Setup the default (E)SPI priorities doing four at a time */ |
| 169 | for (i = MIN_SPI_ID; i < num_ints; i += (1U << IPRIORITYR_SHIFT)) { |
| 170 | gicd_write_ipriorityr(gicd_base, i, GICD_IPRIORITYR_DEF_VAL); |
| 171 | } |
| 172 | |
| 173 | #if GIC_EXT_INTID |
| 174 | for (i = MIN_ESPI_ID; i < num_eints; |
| 175 | i += (1U << IPRIORITYR_SHIFT)) { |
| 176 | gicd_write_ipriorityr(gicd_base, i, GICD_IPRIORITYR_DEF_VAL); |
| 177 | } |
| 178 | #endif |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 179 | /* |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 180 | * Treat all (E)SPIs as level triggered by default, write 16 at a time |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 181 | */ |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 182 | for (i = MIN_SPI_ID; i < num_ints; i += (1U << ICFGR_SHIFT)) { |
| 183 | gicd_write_icfgr(gicd_base, i, 0U); |
| 184 | } |
| 185 | |
| 186 | #if GIC_EXT_INTID |
| 187 | for (i = MIN_ESPI_ID; i < num_eints; i += (1U << ICFGR_SHIFT)) { |
| 188 | gicd_write_icfgr(gicd_base, i, 0U); |
| 189 | } |
| 190 | #endif |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 193 | /******************************************************************************* |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 194 | * Helper function to configure properties of secure (E)SPIs |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 195 | ******************************************************************************/ |
Daniel Boulby | 4e83abb | 2018-05-01 15:15:34 +0100 | [diff] [blame] | 196 | unsigned int gicv3_secure_spis_config_props(uintptr_t gicd_base, |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 197 | const interrupt_prop_t *interrupt_props, |
| 198 | unsigned int interrupt_props_num) |
| 199 | { |
| 200 | unsigned int i; |
| 201 | const interrupt_prop_t *current_prop; |
| 202 | unsigned long long gic_affinity_val; |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 203 | unsigned int ctlr_enable = 0U; |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 204 | |
| 205 | /* Make sure there's a valid property array */ |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 206 | if (interrupt_props_num > 0U) { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 207 | assert(interrupt_props != NULL); |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 208 | } |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 209 | |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 210 | for (i = 0U; i < interrupt_props_num; i++) { |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 211 | current_prop = &interrupt_props[i]; |
| 212 | |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 213 | unsigned int intr_num = current_prop->intr_num; |
| 214 | |
| 215 | /* Skip SGI, (E)PPI and LPI interrupts */ |
| 216 | if (!IS_SPI(intr_num)) { |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 217 | continue; |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 218 | } |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 219 | |
| 220 | /* Configure this interrupt as a secure interrupt */ |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 221 | gicd_clr_igroupr(gicd_base, intr_num); |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 222 | |
| 223 | /* Configure this interrupt as G0 or a G1S interrupt */ |
| 224 | assert((current_prop->intr_grp == INTR_GROUP0) || |
| 225 | (current_prop->intr_grp == INTR_GROUP1S)); |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 226 | |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 227 | if (current_prop->intr_grp == INTR_GROUP1S) { |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 228 | gicd_set_igrpmodr(gicd_base, intr_num); |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 229 | ctlr_enable |= CTLR_ENABLE_G1S_BIT; |
| 230 | } else { |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 231 | gicd_clr_igrpmodr(gicd_base, intr_num); |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 232 | ctlr_enable |= CTLR_ENABLE_G0_BIT; |
| 233 | } |
| 234 | |
| 235 | /* Set interrupt configuration */ |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 236 | gicd_set_icfgr(gicd_base, intr_num, current_prop->intr_cfg); |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 237 | |
| 238 | /* Set the priority of this interrupt */ |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 239 | gicd_set_ipriorityr(gicd_base, intr_num, |
| 240 | current_prop->intr_pri); |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 241 | |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 242 | /* Target (E)SPIs to the primary CPU */ |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 243 | gic_affinity_val = |
| 244 | gicd_irouter_val_from_mpidr(read_mpidr(), 0U); |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 245 | gicd_write_irouter(gicd_base, intr_num, |
| 246 | gic_affinity_val); |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 247 | |
| 248 | /* Enable this interrupt */ |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 249 | gicd_set_isenabler(gicd_base, intr_num); |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | return ctlr_enable; |
| 253 | } |
| 254 | |
| 255 | /******************************************************************************* |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 256 | * Helper function to configure the default attributes of (E)SPIs |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 257 | ******************************************************************************/ |
Daniel Boulby | 4e83abb | 2018-05-01 15:15:34 +0100 | [diff] [blame] | 258 | void gicv3_ppi_sgi_config_defaults(uintptr_t gicr_base) |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 259 | { |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 260 | unsigned int i, ppi_regs_num, regs_num; |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 261 | |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 262 | #if GIC_EXT_INTID |
| 263 | /* Calculate number of PPI registers */ |
| 264 | ppi_regs_num = (unsigned int)((gicr_read_typer(gicr_base) >> |
| 265 | TYPER_PPI_NUM_SHIFT) & TYPER_PPI_NUM_MASK) + 1; |
| 266 | /* All other values except PPInum [0-2] are reserved */ |
| 267 | if (ppi_regs_num > 3U) { |
| 268 | ppi_regs_num = 1U; |
| 269 | } |
| 270 | #else |
| 271 | ppi_regs_num = 1U; |
| 272 | #endif |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 273 | /* |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 274 | * Disable all SGIs (imp. def.)/(E)PPIs before configuring them. |
| 275 | * This is a more scalable approach as it avoids clearing |
| 276 | * the enable bits in the GICD_CTLR. |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 277 | */ |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 278 | for (i = 0U; i < ppi_regs_num; ++i) { |
| 279 | gicr_write_icenabler(gicr_base, i, ~0U); |
| 280 | } |
| 281 | |
| 282 | /* Wait for pending writes to GICR_ICENABLER */ |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 283 | gicr_wait_for_pending_write(gicr_base); |
| 284 | |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 285 | /* 32 interrupt IDs per GICR_IGROUPR register */ |
| 286 | for (i = 0U; i < ppi_regs_num; ++i) { |
| 287 | /* Treat all SGIs/(E)PPIs as G1NS by default */ |
| 288 | gicr_write_igroupr(gicr_base, i, ~0U); |
| 289 | } |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 290 | |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 291 | /* 4 interrupt IDs per GICR_IPRIORITYR register */ |
| 292 | regs_num = ppi_regs_num << 3; |
| 293 | for (i = 0U; i < regs_num; ++i) { |
| 294 | /* Setup the default (E)PPI/SGI priorities doing 4 at a time */ |
| 295 | gicr_write_ipriorityr(gicr_base, i, GICD_IPRIORITYR_DEF_VAL); |
| 296 | } |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 297 | |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 298 | /* 16 interrupt IDs per GICR_ICFGR register */ |
| 299 | regs_num = ppi_regs_num << 1; |
| 300 | for (i = (MIN_PPI_ID >> ICFGR_SHIFT); i < regs_num; ++i) { |
| 301 | /* Configure all (E)PPIs as level triggered by default */ |
| 302 | gicr_write_icfgr(gicr_base, i, 0U); |
| 303 | } |
Achin Gupta | 92712a5 | 2015-09-03 14:18:02 +0100 | [diff] [blame] | 304 | } |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 305 | |
| 306 | /******************************************************************************* |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 307 | * Helper function to configure properties of secure G0 and G1S (E)PPIs and SGIs |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 308 | ******************************************************************************/ |
Daniel Boulby | 4e83abb | 2018-05-01 15:15:34 +0100 | [diff] [blame] | 309 | unsigned int gicv3_secure_ppi_sgi_config_props(uintptr_t gicr_base, |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 310 | const interrupt_prop_t *interrupt_props, |
| 311 | unsigned int interrupt_props_num) |
| 312 | { |
| 313 | unsigned int i; |
| 314 | const interrupt_prop_t *current_prop; |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 315 | unsigned int ctlr_enable = 0U; |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 316 | |
| 317 | /* Make sure there's a valid property array */ |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 318 | if (interrupt_props_num > 0U) { |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 319 | assert(interrupt_props != NULL); |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 320 | } |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 321 | |
Antonio Nino Diaz | ca994e7 | 2018-08-21 10:02:33 +0100 | [diff] [blame] | 322 | for (i = 0U; i < interrupt_props_num; i++) { |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 323 | current_prop = &interrupt_props[i]; |
| 324 | |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 325 | unsigned int intr_num = current_prop->intr_num; |
| 326 | |
| 327 | /* Skip (E)SPI interrupt */ |
| 328 | if (!IS_SGI_PPI(intr_num)) { |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 329 | continue; |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 330 | } |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 331 | |
| 332 | /* Configure this interrupt as a secure interrupt */ |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 333 | gicr_clr_igroupr(gicr_base, intr_num); |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 334 | |
| 335 | /* Configure this interrupt as G0 or a G1S interrupt */ |
| 336 | assert((current_prop->intr_grp == INTR_GROUP0) || |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 337 | (current_prop->intr_grp == INTR_GROUP1S)); |
| 338 | |
Jeenu Viswambharan | 88d8f45 | 2017-11-07 08:38:23 +0000 | [diff] [blame] | 339 | if (current_prop->intr_grp == INTR_GROUP1S) { |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 340 | gicr_set_igrpmodr(gicr_base, intr_num); |
Jeenu Viswambharan | 88d8f45 | 2017-11-07 08:38:23 +0000 | [diff] [blame] | 341 | ctlr_enable |= CTLR_ENABLE_G1S_BIT; |
| 342 | } else { |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 343 | gicr_clr_igrpmodr(gicr_base, intr_num); |
Jeenu Viswambharan | 88d8f45 | 2017-11-07 08:38:23 +0000 | [diff] [blame] | 344 | ctlr_enable |= CTLR_ENABLE_G0_BIT; |
| 345 | } |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 346 | |
| 347 | /* Set the priority of this interrupt */ |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 348 | gicr_set_ipriorityr(gicr_base, intr_num, |
| 349 | current_prop->intr_pri); |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 350 | |
| 351 | /* |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 352 | * Set interrupt configuration for (E)PPIs. |
| 353 | * Configurations for SGIs 0-15 are ignored. |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 354 | */ |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 355 | if (intr_num >= MIN_PPI_ID) { |
| 356 | gicr_set_icfgr(gicr_base, intr_num, |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 357 | current_prop->intr_cfg); |
| 358 | } |
| 359 | |
| 360 | /* Enable this interrupt */ |
Alexei Fedorov | a6e6ae0 | 2020-04-06 16:27:54 +0100 | [diff] [blame] | 361 | gicr_set_isenabler(gicr_base, intr_num); |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 362 | } |
Jeenu Viswambharan | 88d8f45 | 2017-11-07 08:38:23 +0000 | [diff] [blame] | 363 | |
| 364 | return ctlr_enable; |
Jeenu Viswambharan | aeb267c | 2017-09-22 08:32:09 +0100 | [diff] [blame] | 365 | } |
Andre Przywara | 95581b4 | 2020-09-07 14:53:58 +0100 | [diff] [blame] | 366 | |
| 367 | /** |
| 368 | * gicv3_rdistif_get_number_frames() - determine size of GICv3 GICR region |
| 369 | * @gicr_frame: base address of the GICR region to check |
| 370 | * |
| 371 | * This iterates over the GICR_TYPER registers of multiple GICR frames in |
| 372 | * a GICR region, to find the instance which has the LAST bit set. For most |
| 373 | * systems this corresponds to the number of cores handled by a redistributor, |
| 374 | * but there could be disabled cores among them. |
| 375 | * It assumes that each GICR region is fully accessible (till the LAST bit |
| 376 | * marks the end of the region). |
| 377 | * If a platform has multiple GICR regions, this function would need to be |
| 378 | * called multiple times, providing the respective GICR base address each time. |
| 379 | * |
| 380 | * Return: number of valid GICR frames (at least 1, up to PLATFORM_CORE_COUNT) |
| 381 | ******************************************************************************/ |
| 382 | unsigned int gicv3_rdistif_get_number_frames(const uintptr_t gicr_frame) |
| 383 | { |
| 384 | uintptr_t rdistif_base = gicr_frame; |
| 385 | unsigned int count; |
| 386 | |
Andre Przywara | f70f4b9 | 2021-05-18 15:51:06 +0100 | [diff] [blame] | 387 | for (count = 1U; count < PLATFORM_CORE_COUNT; count++) { |
| 388 | uint64_t typer_val = gicr_read_typer(rdistif_base); |
| 389 | |
| 390 | if ((typer_val & TYPER_LAST_BIT) != 0U) { |
Andre Przywara | 95581b4 | 2020-09-07 14:53:58 +0100 | [diff] [blame] | 391 | break; |
| 392 | } |
Andre Przywara | f70f4b9 | 2021-05-18 15:51:06 +0100 | [diff] [blame] | 393 | rdistif_base += gicv3_redist_size(typer_val); |
Andre Przywara | 95581b4 | 2020-09-07 14:53:58 +0100 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | return count; |
| 397 | } |
Andre Przywara | b8da1c6 | 2021-08-24 10:03:57 +0100 | [diff] [blame] | 398 | |
| 399 | unsigned int gicv3_get_component_partnum(const uintptr_t gic_frame) |
| 400 | { |
| 401 | unsigned int part_id; |
| 402 | |
| 403 | /* |
| 404 | * The lower 8 bits of PIDR0, complemented by the lower 4 bits of |
| 405 | * PIDR1 contain a part number identifying the GIC component at a |
| 406 | * particular base address. |
| 407 | */ |
| 408 | part_id = mmio_read_32(gic_frame + GICD_PIDR0_GICV3) & 0xff; |
| 409 | part_id |= (mmio_read_32(gic_frame + GICD_PIDR1_GICV3) << 8) & 0xf00; |
| 410 | |
| 411 | return part_id; |
| 412 | } |
Manish V Badarkhe | 173c296 | 2022-05-09 21:55:19 +0100 | [diff] [blame] | 413 | |
| 414 | /******************************************************************************* |
| 415 | * Helper function to return product ID and revision of GIC |
| 416 | * @gicd_base: base address of the GIC distributor |
| 417 | * @gic_prod_id: retrieved product id of GIC |
| 418 | * @gic_rev: retrieved revision of GIC |
| 419 | ******************************************************************************/ |
| 420 | void gicv3_get_component_prodid_rev(const uintptr_t gicd_base, |
| 421 | unsigned int *gic_prod_id, |
| 422 | uint8_t *gic_rev) |
| 423 | { |
| 424 | unsigned int gicd_iidr; |
| 425 | uint8_t gic_variant; |
| 426 | |
| 427 | gicd_iidr = gicd_read_iidr(gicd_base); |
| 428 | *gic_prod_id = gicd_iidr >> IIDR_PRODUCT_ID_SHIFT; |
| 429 | *gic_prod_id &= IIDR_PRODUCT_ID_MASK; |
| 430 | |
| 431 | gic_variant = gicd_iidr >> IIDR_VARIANT_SHIFT; |
| 432 | gic_variant &= IIDR_VARIANT_MASK; |
| 433 | |
| 434 | *gic_rev = gicd_iidr >> IIDR_REV_SHIFT; |
| 435 | *gic_rev &= IIDR_REV_MASK; |
| 436 | |
| 437 | /* |
| 438 | * pack gic variant and gic_rev in 1 byte |
| 439 | * gic_rev = gic_variant[7:4] and gic_rev[0:3] |
| 440 | */ |
| 441 | *gic_rev = *gic_rev | gic_variant << 0x4; |
| 442 | |
| 443 | } |