Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | /* * Copyright 2020 Broadcom * * SPDX-License-Identifier: Apache-2.0 */ #include <sys/__assert.h> #include <sw_isr_table.h> #include <dt-bindings/interrupt-controller/arm-gic.h> #include <drivers/interrupt_controller/gic.h> #include "intc_gic_common_priv.h" #include "intc_gicv3_priv.h" /* Redistributor base addresses for each core */ mem_addr_t gic_rdists[CONFIG_MP_NUM_CPUS]; #if defined(CONFIG_ARMV8_A_NS) || defined(CONFIG_GIC_SINGLE_SECURITY_STATE) #define IGROUPR_VAL 0xFFFFFFFFU #else #define IGROUPR_VAL 0x0U #endif static inline mem_addr_t gic_get_rdist(void) { return gic_rdists[arch_curr_cpu()->id]; } /* * Wait for register write pending * TODO: add timed wait */ static int gic_wait_rwp(uint32_t intid) { uint32_t rwp_mask; mem_addr_t base; if (intid < GIC_SPI_INT_BASE) { base = (gic_get_rdist() + GICR_CTLR); rwp_mask = BIT(GICR_CTLR_RWP); } else { base = GICD_CTLR; rwp_mask = BIT(GICD_CTLR_RWP); } while (sys_read32(base) & rwp_mask) ; return 0; } void arm_gic_irq_set_priority(unsigned int intid, unsigned int prio, uint32_t flags) { uint32_t mask = BIT(intid & (GIC_NUM_INTR_PER_REG - 1)); uint32_t idx = intid / GIC_NUM_INTR_PER_REG; uint32_t shift; uint32_t val; mem_addr_t base = GET_DIST_BASE(intid); /* Disable the interrupt */ sys_write32(mask, ICENABLER(base, idx)); gic_wait_rwp(intid); /* PRIORITYR registers provide byte access */ sys_write8(prio & GIC_PRI_MASK, IPRIORITYR(base, intid)); /* Interrupt type config */ if (!GIC_IS_SGI(intid)) { idx = intid / GIC_NUM_CFG_PER_REG; shift = (intid & (GIC_NUM_CFG_PER_REG - 1)) * 2; val = sys_read32(ICFGR(base, idx)); val &= ~(GICD_ICFGR_MASK << shift); if (flags & IRQ_TYPE_EDGE) { val |= (GICD_ICFGR_TYPE << shift); } sys_write32(val, ICFGR(base, idx)); } } void arm_gic_irq_enable(unsigned int intid) { uint32_t mask = BIT(intid & (GIC_NUM_INTR_PER_REG - 1)); uint32_t idx = intid / GIC_NUM_INTR_PER_REG; sys_write32(mask, ISENABLER(GET_DIST_BASE(intid), idx)); } void arm_gic_irq_disable(unsigned int intid) { uint32_t mask = BIT(intid & (GIC_NUM_INTR_PER_REG - 1)); uint32_t idx = intid / GIC_NUM_INTR_PER_REG; sys_write32(mask, ICENABLER(GET_DIST_BASE(intid), idx)); /* poll to ensure write is complete */ gic_wait_rwp(intid); } bool arm_gic_irq_is_enabled(unsigned int intid) { uint32_t mask = BIT(intid & (GIC_NUM_INTR_PER_REG - 1)); uint32_t idx = intid / GIC_NUM_INTR_PER_REG; uint32_t val; val = sys_read32(ISENABLER(GET_DIST_BASE(intid), idx)); return (val & mask) != 0; } unsigned int arm_gic_get_active(void) { int intid; /* (Pending -> Active / AP) or (AP -> AP) */ intid = read_sysreg(ICC_IAR1_EL1); return intid; } void arm_gic_eoi(unsigned int intid) { /* * Interrupt request deassertion from peripheral to GIC happens * by clearing interrupt condition by a write to the peripheral * register. It is desired that the write transfer is complete * before the core tries to change GIC state from 'AP/Active' to * a new state on seeing 'EOI write'. * Since ICC interface writes are not ordered against Device * memory writes, a barrier is required to ensure the ordering. * The dsb will also ensure *completion* of previous writes with * DEVICE nGnRnE attribute. */ __DSB(); /* (AP -> Pending) Or (Active -> Inactive) or (AP to AP) nested case */ write_sysreg(intid, ICC_EOIR1_EL1); } void gic_raise_sgi(unsigned int sgi_id, uint64_t target_aff, uint16_t target_list) { uint32_t aff3, aff2, aff1; uint64_t sgi_val; __ASSERT_NO_MSG(GIC_IS_SGI(sgi_id)); /* Extract affinity fields from target */ aff1 = MPIDR_AFFLVL(target_aff, 1); aff2 = MPIDR_AFFLVL(target_aff, 2); aff3 = MPIDR_AFFLVL(target_aff, 3); sgi_val = GICV3_SGIR_VALUE(aff3, aff2, aff1, sgi_id, SGIR_IRM_TO_AFF, target_list); __DSB(); write_sysreg(sgi_val, ICC_SGI1R); __ISB(); } /* * Wake up GIC redistributor. * clear ProcessorSleep and wait till ChildAsleep is cleared. * ProcessSleep to be cleared only when ChildAsleep is set * Check if redistributor is not powered already. */ static void gicv3_rdist_enable(mem_addr_t rdist) { if (!(sys_read32(rdist + GICR_WAKER) & BIT(GICR_WAKER_CA))) return; sys_clear_bit(rdist + GICR_WAKER, GICR_WAKER_PS); while (sys_read32(rdist + GICR_WAKER) & BIT(GICR_WAKER_CA)) ; } /* * Initialize the cpu interface. This should be called by each core. */ static void gicv3_cpuif_init(void) { uint32_t icc_sre; uint32_t intid; mem_addr_t base = gic_get_rdist() + GICR_SGI_BASE_OFF; /* Disable all sgi ppi */ sys_write32(BIT_MASK(GIC_NUM_INTR_PER_REG), ICENABLER(base, 0)); /* Any sgi/ppi intid ie. 0-31 will select GICR_CTRL */ gic_wait_rwp(0); /* Clear pending */ sys_write32(BIT_MASK(GIC_NUM_INTR_PER_REG), ICPENDR(base, 0)); /* Configure all SGIs/PPIs as G1S or G1NS depending on Zephyr * is run in EL1S or EL1NS respectively. * All interrupts will be delivered as irq */ sys_write32(IGROUPR_VAL, IGROUPR(base, 0)); sys_write32(BIT_MASK(GIC_NUM_INTR_PER_REG), IGROUPMODR(base, 0)); /* * Configure default priorities for SGI 0:15 and PPI 0:15. */ for (intid = 0; intid < GIC_SPI_INT_BASE; intid += GIC_NUM_PRI_PER_REG) { sys_write32(GIC_INT_DEF_PRI_X4, IPRIORITYR(base, intid)); } /* Configure PPIs as level triggered */ sys_write32(0, ICFGR(base, 1)); /* * Check if system interface can be enabled. * 'icc_sre_el3' needs to be configured at 'EL3' * to allow access to 'icc_sre_el1' at 'EL1' * eg: z_arch_el3_plat_init can be used by platform. */ icc_sre = read_sysreg(ICC_SRE_EL1); if (!(icc_sre & ICC_SRE_ELx_SRE_BIT)) { icc_sre = (icc_sre | ICC_SRE_ELx_SRE_BIT | ICC_SRE_ELx_DIB_BIT | ICC_SRE_ELx_DFB_BIT); write_sysreg(icc_sre, ICC_SRE_EL1); icc_sre = read_sysreg(ICC_SRE_EL1); __ASSERT_NO_MSG(icc_sre & ICC_SRE_ELx_SRE_BIT); } write_sysreg(GIC_IDLE_PRIO, ICC_PMR_EL1); /* Allow group1 interrupts */ write_sysreg(1, ICC_IGRPEN1_EL1); } /* * TODO: Consider Zephyr in EL1NS. */ static void gicv3_dist_init(void) { unsigned int num_ints; unsigned int intid; unsigned int idx; mem_addr_t base = GIC_DIST_BASE; num_ints = sys_read32(GICD_TYPER); num_ints &= GICD_TYPER_ITLINESNUM_MASK; num_ints = (num_ints + 1) << 5; /* Disable the distributor */ sys_write32(0, GICD_CTLR); gic_wait_rwp(GIC_SPI_INT_BASE); #ifdef CONFIG_GIC_SINGLE_SECURITY_STATE /* * Before configuration, we need to check whether * the GIC single security state mode is supported. * Make sure GICD_CTRL_NS is 1. */ sys_set_bit(GICD_CTLR, GICD_CTRL_NS); __ASSERT(sys_test_bit(GICD_CTLR, GICD_CTRL_NS), "Current GIC does not support single security state"); #endif /* * Default configuration of all SPIs */ for (intid = GIC_SPI_INT_BASE; intid < num_ints; intid += GIC_NUM_INTR_PER_REG) { idx = intid / GIC_NUM_INTR_PER_REG; /* Disable interrupt */ sys_write32(BIT_MASK(GIC_NUM_INTR_PER_REG), ICENABLER(base, idx)); /* Clear pending */ sys_write32(BIT_MASK(GIC_NUM_INTR_PER_REG), ICPENDR(base, idx)); sys_write32(IGROUPR_VAL, IGROUPR(base, idx)); sys_write32(BIT_MASK(GIC_NUM_INTR_PER_REG), IGROUPMODR(base, idx)); } /* wait for rwp on GICD */ gic_wait_rwp(GIC_SPI_INT_BASE); /* Configure default priorities for all SPIs. */ for (intid = GIC_SPI_INT_BASE; intid < num_ints; intid += GIC_NUM_PRI_PER_REG) { sys_write32(GIC_INT_DEF_PRI_X4, IPRIORITYR(base, intid)); } /* Configure all SPIs as active low, level triggered by default */ for (intid = GIC_SPI_INT_BASE; intid < num_ints; intid += GIC_NUM_CFG_PER_REG) { idx = intid / GIC_NUM_CFG_PER_REG; sys_write32(0, ICFGR(base, idx)); } #ifdef CONFIG_ARMV8_A_NS /* Enable distributor with ARE */ sys_write32(BIT(GICD_CTRL_ARE_NS) | BIT(GICD_CTLR_ENABLE_G1NS), GICD_CTLR); #elif defined(CONFIG_GIC_SINGLE_SECURITY_STATE) /* * For GIC single security state, the config GIC_SINGLE_SECURITY_STATE * means the GIC is under single security state which has only two * groups: group 0 and group 1. * Then set GICD_CTLR_ARE and GICD_CTLR_ENABLE_G1 to enable Group 1 * interrupt. * Since the GICD_CTLR_ARE and GICD_CTRL_ARE_S share BIT(4), and * similarly the GICD_CTLR_ENABLE_G1 and GICD_CTLR_ENABLE_G1NS share * BIT(1), we can reuse them. */ sys_write32(BIT(GICD_CTRL_ARE_S) | BIT(GICD_CTLR_ENABLE_G1NS), GICD_CTLR); #else /* enable Group 1 secure interrupts */ sys_set_bit(GICD_CTLR, GICD_CTLR_ENABLE_G1S); #endif } static void __arm_gic_init(void) { uint8_t cpu; cpu = arch_curr_cpu()->id; gic_rdists[cpu] = GIC_RDIST_BASE + MPIDR_TO_CORE(GET_MPIDR()) * 0x20000; gicv3_rdist_enable(gic_get_rdist()); gicv3_cpuif_init(); } int arm_gic_init(const struct device *unused) { ARG_UNUSED(unused); gicv3_dist_init(); __arm_gic_init(); return 0; } SYS_INIT(arm_gic_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT); #ifdef CONFIG_SMP void arm_gic_secondary_init(void) { __arm_gic_init(); } #endif |