1f14d1886SSoby Mathew /* 2eb68ea9bSJeenu Viswambharan * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved. 3f14d1886SSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5f14d1886SSoby Mathew */ 6f14d1886SSoby Mathew #include <arch_helpers.h> 7f14d1886SSoby Mathew #include <assert.h> 8f14d1886SSoby Mathew #include <bl_common.h> 9f14d1886SSoby Mathew #include <cassert.h> 10f14d1886SSoby Mathew #include <gic_common.h> 11f14d1886SSoby Mathew #include <gicv3.h> 12f14d1886SSoby Mathew #include <interrupt_mgmt.h> 13f14d1886SSoby Mathew #include <platform.h> 14f14d1886SSoby Mathew 153d8256b2SMasahiro Yamada #ifdef IMAGE_BL31 16f14d1886SSoby Mathew 17f14d1886SSoby Mathew /* 18f14d1886SSoby Mathew * The following platform GIC functions are weakly defined. They 19f14d1886SSoby Mathew * provide typical implementations that may be re-used by multiple 20f14d1886SSoby Mathew * platforms but may also be overridden by a platform if required. 21f14d1886SSoby Mathew */ 22f14d1886SSoby Mathew #pragma weak plat_ic_get_pending_interrupt_id 23f14d1886SSoby Mathew #pragma weak plat_ic_get_pending_interrupt_type 24f14d1886SSoby Mathew #pragma weak plat_ic_acknowledge_interrupt 25f14d1886SSoby Mathew #pragma weak plat_ic_get_interrupt_type 26f14d1886SSoby Mathew #pragma weak plat_ic_end_of_interrupt 27f14d1886SSoby Mathew #pragma weak plat_interrupt_type_to_line 28f14d1886SSoby Mathew 29eb68ea9bSJeenu Viswambharan #pragma weak plat_ic_get_running_priority 30ca43b55dSJeenu Viswambharan #pragma weak plat_ic_is_spi 31ca43b55dSJeenu Viswambharan #pragma weak plat_ic_is_ppi 32ca43b55dSJeenu Viswambharan #pragma weak plat_ic_is_sgi 33cbd3f370SJeenu Viswambharan #pragma weak plat_ic_get_interrupt_active 34979225f4SJeenu Viswambharan #pragma weak plat_ic_enable_interrupt 35979225f4SJeenu Viswambharan #pragma weak plat_ic_disable_interrupt 36f3a86600SJeenu Viswambharan #pragma weak plat_ic_set_interrupt_priority 3774dce7faSJeenu Viswambharan #pragma weak plat_ic_set_interrupt_type 388db978b5SJeenu Viswambharan #pragma weak plat_ic_raise_el3_sgi 39fc529feeSJeenu Viswambharan #pragma weak plat_ic_set_spi_routing 40a2816a16SJeenu Viswambharan #pragma weak plat_ic_set_interrupt_pending 41a2816a16SJeenu Viswambharan #pragma weak plat_ic_clear_interrupt_pending 42eb68ea9bSJeenu Viswambharan 43f14d1886SSoby Mathew CASSERT((INTR_TYPE_S_EL1 == INTR_GROUP1S) && 44f14d1886SSoby Mathew (INTR_TYPE_NS == INTR_GROUP1NS) && 45f14d1886SSoby Mathew (INTR_TYPE_EL3 == INTR_GROUP0), assert_interrupt_type_mismatch); 46f14d1886SSoby Mathew 47f14d1886SSoby Mathew /* 48f14d1886SSoby Mathew * This function returns the highest priority pending interrupt at 49f14d1886SSoby Mathew * the Interrupt controller 50f14d1886SSoby Mathew */ 51f14d1886SSoby Mathew uint32_t plat_ic_get_pending_interrupt_id(void) 52f14d1886SSoby Mathew { 53f14d1886SSoby Mathew unsigned int irqnr; 54f14d1886SSoby Mathew 55f14d1886SSoby Mathew assert(IS_IN_EL3()); 56f14d1886SSoby Mathew irqnr = gicv3_get_pending_interrupt_id(); 57f14d1886SSoby Mathew return (gicv3_is_intr_id_special_identifier(irqnr)) ? 58f14d1886SSoby Mathew INTR_ID_UNAVAILABLE : irqnr; 59f14d1886SSoby Mathew } 60f14d1886SSoby Mathew 61f14d1886SSoby Mathew /* 62f14d1886SSoby Mathew * This function returns the type of the highest priority pending interrupt 63f14d1886SSoby Mathew * at the Interrupt controller. In the case of GICv3, the Highest Priority 64f14d1886SSoby Mathew * Pending interrupt system register (`ICC_HPPIR0_EL1`) is read to determine 65f14d1886SSoby Mathew * the id of the pending interrupt. The type of interrupt depends upon the 66f14d1886SSoby Mathew * id value as follows. 67f14d1886SSoby Mathew * 1. id = PENDING_G1S_INTID (1020) is reported as a S-EL1 interrupt 68f14d1886SSoby Mathew * 2. id = PENDING_G1NS_INTID (1021) is reported as a Non-secure interrupt. 69f14d1886SSoby Mathew * 3. id = GIC_SPURIOUS_INTERRUPT (1023) is reported as an invalid interrupt 70f14d1886SSoby Mathew * type. 71f14d1886SSoby Mathew * 4. All other interrupt id's are reported as EL3 interrupt. 72f14d1886SSoby Mathew */ 73f14d1886SSoby Mathew uint32_t plat_ic_get_pending_interrupt_type(void) 74f14d1886SSoby Mathew { 75f14d1886SSoby Mathew unsigned int irqnr; 76f14d1886SSoby Mathew 77f14d1886SSoby Mathew assert(IS_IN_EL3()); 78f14d1886SSoby Mathew irqnr = gicv3_get_pending_interrupt_type(); 79f14d1886SSoby Mathew 80f14d1886SSoby Mathew switch (irqnr) { 81f14d1886SSoby Mathew case PENDING_G1S_INTID: 82f14d1886SSoby Mathew return INTR_TYPE_S_EL1; 83f14d1886SSoby Mathew case PENDING_G1NS_INTID: 84f14d1886SSoby Mathew return INTR_TYPE_NS; 85f14d1886SSoby Mathew case GIC_SPURIOUS_INTERRUPT: 86f14d1886SSoby Mathew return INTR_TYPE_INVAL; 87f14d1886SSoby Mathew default: 88f14d1886SSoby Mathew return INTR_TYPE_EL3; 89f14d1886SSoby Mathew } 90f14d1886SSoby Mathew } 91f14d1886SSoby Mathew 92f14d1886SSoby Mathew /* 93f14d1886SSoby Mathew * This function returns the highest priority pending interrupt at 94f14d1886SSoby Mathew * the Interrupt controller and indicates to the Interrupt controller 95f14d1886SSoby Mathew * that the interrupt processing has started. 96f14d1886SSoby Mathew */ 97f14d1886SSoby Mathew uint32_t plat_ic_acknowledge_interrupt(void) 98f14d1886SSoby Mathew { 99f14d1886SSoby Mathew assert(IS_IN_EL3()); 100f14d1886SSoby Mathew return gicv3_acknowledge_interrupt(); 101f14d1886SSoby Mathew } 102f14d1886SSoby Mathew 103f14d1886SSoby Mathew /* 104f14d1886SSoby Mathew * This function returns the type of the interrupt `id`, depending on how 105f14d1886SSoby Mathew * the interrupt has been configured in the interrupt controller 106f14d1886SSoby Mathew */ 107f14d1886SSoby Mathew uint32_t plat_ic_get_interrupt_type(uint32_t id) 108f14d1886SSoby Mathew { 109f14d1886SSoby Mathew assert(IS_IN_EL3()); 110f14d1886SSoby Mathew return gicv3_get_interrupt_type(id, plat_my_core_pos()); 111f14d1886SSoby Mathew } 112f14d1886SSoby Mathew 113f14d1886SSoby Mathew /* 114f14d1886SSoby Mathew * This functions is used to indicate to the interrupt controller that 115f14d1886SSoby Mathew * the processing of the interrupt corresponding to the `id` has 116f14d1886SSoby Mathew * finished. 117f14d1886SSoby Mathew */ 118f14d1886SSoby Mathew void plat_ic_end_of_interrupt(uint32_t id) 119f14d1886SSoby Mathew { 120f14d1886SSoby Mathew assert(IS_IN_EL3()); 121f14d1886SSoby Mathew gicv3_end_of_interrupt(id); 122f14d1886SSoby Mathew } 123f14d1886SSoby Mathew 124f14d1886SSoby Mathew /* 125f14d1886SSoby Mathew * An ARM processor signals interrupt exceptions through the IRQ and FIQ pins. 126f14d1886SSoby Mathew * The interrupt controller knows which pin/line it uses to signal a type of 127f14d1886SSoby Mathew * interrupt. It lets the interrupt management framework determine for a type of 128f14d1886SSoby Mathew * interrupt and security state, which line should be used in the SCR_EL3 to 129f14d1886SSoby Mathew * control its routing to EL3. The interrupt line is represented as the bit 130f14d1886SSoby Mathew * position of the IRQ or FIQ bit in the SCR_EL3. 131f14d1886SSoby Mathew */ 132f14d1886SSoby Mathew uint32_t plat_interrupt_type_to_line(uint32_t type, 133f14d1886SSoby Mathew uint32_t security_state) 134f14d1886SSoby Mathew { 135f14d1886SSoby Mathew assert(type == INTR_TYPE_S_EL1 || 136f14d1886SSoby Mathew type == INTR_TYPE_EL3 || 137f14d1886SSoby Mathew type == INTR_TYPE_NS); 138f14d1886SSoby Mathew 139f14d1886SSoby Mathew assert(sec_state_is_valid(security_state)); 140f14d1886SSoby Mathew assert(IS_IN_EL3()); 141f14d1886SSoby Mathew 142f14d1886SSoby Mathew switch (type) { 143f14d1886SSoby Mathew case INTR_TYPE_S_EL1: 144f14d1886SSoby Mathew /* 145f14d1886SSoby Mathew * The S-EL1 interrupts are signaled as IRQ in S-EL0/1 contexts 146f14d1886SSoby Mathew * and as FIQ in the NS-EL0/1/2 contexts 147f14d1886SSoby Mathew */ 148f14d1886SSoby Mathew if (security_state == SECURE) 149f14d1886SSoby Mathew return __builtin_ctz(SCR_IRQ_BIT); 150f14d1886SSoby Mathew else 151f14d1886SSoby Mathew return __builtin_ctz(SCR_FIQ_BIT); 152f14d1886SSoby Mathew case INTR_TYPE_NS: 153f14d1886SSoby Mathew /* 154f14d1886SSoby Mathew * The Non secure interrupts will be signaled as FIQ in S-EL0/1 155f14d1886SSoby Mathew * contexts and as IRQ in the NS-EL0/1/2 contexts. 156f14d1886SSoby Mathew */ 157f14d1886SSoby Mathew if (security_state == SECURE) 158f14d1886SSoby Mathew return __builtin_ctz(SCR_FIQ_BIT); 159f14d1886SSoby Mathew else 160f14d1886SSoby Mathew return __builtin_ctz(SCR_IRQ_BIT); 161f14d1886SSoby Mathew case INTR_TYPE_EL3: 162f14d1886SSoby Mathew /* 163f14d1886SSoby Mathew * The EL3 interrupts are signaled as FIQ in both S-EL0/1 and 164f14d1886SSoby Mathew * NS-EL0/1/2 contexts 165f14d1886SSoby Mathew */ 166f14d1886SSoby Mathew return __builtin_ctz(SCR_FIQ_BIT); 1678ae0df93SJonathan Wright default: 1688ae0df93SJonathan Wright panic(); 169f14d1886SSoby Mathew } 170f14d1886SSoby Mathew } 171eb68ea9bSJeenu Viswambharan 172eb68ea9bSJeenu Viswambharan unsigned int plat_ic_get_running_priority(void) 173eb68ea9bSJeenu Viswambharan { 174eb68ea9bSJeenu Viswambharan return gicv3_get_running_priority(); 175eb68ea9bSJeenu Viswambharan } 176eb68ea9bSJeenu Viswambharan 177ca43b55dSJeenu Viswambharan int plat_ic_is_spi(unsigned int id) 178ca43b55dSJeenu Viswambharan { 179ca43b55dSJeenu Viswambharan return (id >= MIN_SPI_ID) && (id <= MAX_SPI_ID); 180ca43b55dSJeenu Viswambharan } 181ca43b55dSJeenu Viswambharan 182ca43b55dSJeenu Viswambharan int plat_ic_is_ppi(unsigned int id) 183ca43b55dSJeenu Viswambharan { 184ca43b55dSJeenu Viswambharan return (id >= MIN_PPI_ID) && (id < MIN_SPI_ID); 185ca43b55dSJeenu Viswambharan } 186ca43b55dSJeenu Viswambharan 187ca43b55dSJeenu Viswambharan int plat_ic_is_sgi(unsigned int id) 188ca43b55dSJeenu Viswambharan { 189ca43b55dSJeenu Viswambharan return (id >= MIN_SGI_ID) && (id < MIN_PPI_ID); 190ca43b55dSJeenu Viswambharan } 191cbd3f370SJeenu Viswambharan 192cbd3f370SJeenu Viswambharan unsigned int plat_ic_get_interrupt_active(unsigned int id) 193cbd3f370SJeenu Viswambharan { 194cbd3f370SJeenu Viswambharan return gicv3_get_interrupt_active(id, plat_my_core_pos()); 195cbd3f370SJeenu Viswambharan } 196979225f4SJeenu Viswambharan 197979225f4SJeenu Viswambharan void plat_ic_enable_interrupt(unsigned int id) 198979225f4SJeenu Viswambharan { 199979225f4SJeenu Viswambharan gicv3_enable_interrupt(id, plat_my_core_pos()); 200979225f4SJeenu Viswambharan } 201979225f4SJeenu Viswambharan 202979225f4SJeenu Viswambharan void plat_ic_disable_interrupt(unsigned int id) 203979225f4SJeenu Viswambharan { 204979225f4SJeenu Viswambharan gicv3_disable_interrupt(id, plat_my_core_pos()); 205979225f4SJeenu Viswambharan } 206f3a86600SJeenu Viswambharan 207f3a86600SJeenu Viswambharan void plat_ic_set_interrupt_priority(unsigned int id, unsigned int priority) 208f3a86600SJeenu Viswambharan { 209f3a86600SJeenu Viswambharan gicv3_set_interrupt_priority(id, plat_my_core_pos(), priority); 210f3a86600SJeenu Viswambharan } 21174dce7faSJeenu Viswambharan 21274dce7faSJeenu Viswambharan int plat_ic_has_interrupt_type(unsigned int type) 21374dce7faSJeenu Viswambharan { 21474dce7faSJeenu Viswambharan assert((type == INTR_TYPE_EL3) || (type == INTR_TYPE_S_EL1) || 21574dce7faSJeenu Viswambharan (type == INTR_TYPE_NS)); 21674dce7faSJeenu Viswambharan return 1; 21774dce7faSJeenu Viswambharan } 21874dce7faSJeenu Viswambharan 21974dce7faSJeenu Viswambharan void plat_ic_set_interrupt_type(unsigned int id, unsigned int type) 22074dce7faSJeenu Viswambharan { 22174dce7faSJeenu Viswambharan gicv3_set_interrupt_type(id, plat_my_core_pos(), type); 22274dce7faSJeenu Viswambharan } 2238db978b5SJeenu Viswambharan 2248db978b5SJeenu Viswambharan void plat_ic_raise_el3_sgi(int sgi_num, u_register_t target) 2258db978b5SJeenu Viswambharan { 2268db978b5SJeenu Viswambharan /* Target must be a valid MPIDR in the system */ 2278db978b5SJeenu Viswambharan assert(plat_core_pos_by_mpidr(target) >= 0); 2288db978b5SJeenu Viswambharan 2298db978b5SJeenu Viswambharan /* Verify that this is a secure EL3 SGI */ 2308db978b5SJeenu Viswambharan assert(plat_ic_get_interrupt_type(sgi_num) == INTR_TYPE_EL3); 2318db978b5SJeenu Viswambharan 2328db978b5SJeenu Viswambharan gicv3_raise_secure_g0_sgi(sgi_num, target); 2338db978b5SJeenu Viswambharan } 234fc529feeSJeenu Viswambharan 235fc529feeSJeenu Viswambharan void plat_ic_set_spi_routing(unsigned int id, unsigned int routing_mode, 236fc529feeSJeenu Viswambharan u_register_t mpidr) 237fc529feeSJeenu Viswambharan { 238fc529feeSJeenu Viswambharan unsigned int irm = 0; 239fc529feeSJeenu Viswambharan 240fc529feeSJeenu Viswambharan switch (routing_mode) { 241fc529feeSJeenu Viswambharan case INTR_ROUTING_MODE_PE: 242fc529feeSJeenu Viswambharan assert(plat_core_pos_by_mpidr(mpidr) >= 0); 243fc529feeSJeenu Viswambharan irm = GICV3_IRM_PE; 244fc529feeSJeenu Viswambharan break; 245fc529feeSJeenu Viswambharan case INTR_ROUTING_MODE_ANY: 246fc529feeSJeenu Viswambharan irm = GICV3_IRM_ANY; 247fc529feeSJeenu Viswambharan break; 248fc529feeSJeenu Viswambharan default: 249fc529feeSJeenu Viswambharan assert(0); 250*649c48f5SJonathan Wright break; 251fc529feeSJeenu Viswambharan } 252fc529feeSJeenu Viswambharan 253fc529feeSJeenu Viswambharan gicv3_set_spi_routing(id, irm, mpidr); 254fc529feeSJeenu Viswambharan } 255a2816a16SJeenu Viswambharan 256a2816a16SJeenu Viswambharan void plat_ic_set_interrupt_pending(unsigned int id) 257a2816a16SJeenu Viswambharan { 258a2816a16SJeenu Viswambharan /* Disallow setting SGIs pending */ 259a2816a16SJeenu Viswambharan assert(id >= MIN_PPI_ID); 260a2816a16SJeenu Viswambharan gicv3_set_interrupt_pending(id, plat_my_core_pos()); 261a2816a16SJeenu Viswambharan } 262a2816a16SJeenu Viswambharan 263a2816a16SJeenu Viswambharan void plat_ic_clear_interrupt_pending(unsigned int id) 264a2816a16SJeenu Viswambharan { 265a2816a16SJeenu Viswambharan /* Disallow setting SGIs pending */ 266a2816a16SJeenu Viswambharan assert(id >= MIN_PPI_ID); 267a2816a16SJeenu Viswambharan gicv3_clear_interrupt_pending(id, plat_my_core_pos()); 268a2816a16SJeenu Viswambharan } 269d55a4450SJeenu Viswambharan 270d55a4450SJeenu Viswambharan unsigned int plat_ic_set_priority_mask(unsigned int mask) 271d55a4450SJeenu Viswambharan { 272d55a4450SJeenu Viswambharan return gicv3_set_pmr(mask); 273d55a4450SJeenu Viswambharan } 2744ee8d0beSJeenu Viswambharan 2754ee8d0beSJeenu Viswambharan unsigned int plat_ic_get_interrupt_id(unsigned int raw) 2764ee8d0beSJeenu Viswambharan { 2774ee8d0beSJeenu Viswambharan unsigned int id = (raw & INT_ID_MASK); 2784ee8d0beSJeenu Viswambharan 2794ee8d0beSJeenu Viswambharan return (gicv3_is_intr_id_special_identifier(id) ? 2804ee8d0beSJeenu Viswambharan INTR_ID_UNAVAILABLE : id); 2814ee8d0beSJeenu Viswambharan } 282f14d1886SSoby Mathew #endif 2833d8256b2SMasahiro Yamada #ifdef IMAGE_BL32 284f14d1886SSoby Mathew 285f14d1886SSoby Mathew #pragma weak plat_ic_get_pending_interrupt_id 286f14d1886SSoby Mathew #pragma weak plat_ic_acknowledge_interrupt 287f14d1886SSoby Mathew #pragma weak plat_ic_end_of_interrupt 288f14d1886SSoby Mathew 289877cf3ffSSoby Mathew /* In AArch32, the secure group1 interrupts are targeted to Secure PL1 */ 290877cf3ffSSoby Mathew #ifdef AARCH32 291877cf3ffSSoby Mathew #define IS_IN_EL1() IS_IN_SECURE() 292877cf3ffSSoby Mathew #endif 293877cf3ffSSoby Mathew 294f14d1886SSoby Mathew /* 295f14d1886SSoby Mathew * This function returns the highest priority pending interrupt at 296f14d1886SSoby Mathew * the Interrupt controller 297f14d1886SSoby Mathew */ 298f14d1886SSoby Mathew uint32_t plat_ic_get_pending_interrupt_id(void) 299f14d1886SSoby Mathew { 300f14d1886SSoby Mathew unsigned int irqnr; 301f14d1886SSoby Mathew 302f14d1886SSoby Mathew assert(IS_IN_EL1()); 303f14d1886SSoby Mathew irqnr = gicv3_get_pending_interrupt_id_sel1(); 304f14d1886SSoby Mathew return (irqnr == GIC_SPURIOUS_INTERRUPT) ? 305f14d1886SSoby Mathew INTR_ID_UNAVAILABLE : irqnr; 306f14d1886SSoby Mathew } 307f14d1886SSoby Mathew 308f14d1886SSoby Mathew /* 309f14d1886SSoby Mathew * This function returns the highest priority pending interrupt at 310f14d1886SSoby Mathew * the Interrupt controller and indicates to the Interrupt controller 311f14d1886SSoby Mathew * that the interrupt processing has started. 312f14d1886SSoby Mathew */ 313f14d1886SSoby Mathew uint32_t plat_ic_acknowledge_interrupt(void) 314f14d1886SSoby Mathew { 315f14d1886SSoby Mathew assert(IS_IN_EL1()); 316f14d1886SSoby Mathew return gicv3_acknowledge_interrupt_sel1(); 317f14d1886SSoby Mathew } 318f14d1886SSoby Mathew 319f14d1886SSoby Mathew /* 320f14d1886SSoby Mathew * This functions is used to indicate to the interrupt controller that 321f14d1886SSoby Mathew * the processing of the interrupt corresponding to the `id` has 322f14d1886SSoby Mathew * finished. 323f14d1886SSoby Mathew */ 324f14d1886SSoby Mathew void plat_ic_end_of_interrupt(uint32_t id) 325f14d1886SSoby Mathew { 326f14d1886SSoby Mathew assert(IS_IN_EL1()); 327f14d1886SSoby Mathew gicv3_end_of_interrupt_sel1(id); 328f14d1886SSoby Mathew } 329f14d1886SSoby Mathew #endif 330