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 39*fc529feeSJeenu Viswambharan #pragma weak plat_ic_set_spi_routing 40eb68ea9bSJeenu Viswambharan 41f14d1886SSoby Mathew CASSERT((INTR_TYPE_S_EL1 == INTR_GROUP1S) && 42f14d1886SSoby Mathew (INTR_TYPE_NS == INTR_GROUP1NS) && 43f14d1886SSoby Mathew (INTR_TYPE_EL3 == INTR_GROUP0), assert_interrupt_type_mismatch); 44f14d1886SSoby Mathew 45f14d1886SSoby Mathew /* 46f14d1886SSoby Mathew * This function returns the highest priority pending interrupt at 47f14d1886SSoby Mathew * the Interrupt controller 48f14d1886SSoby Mathew */ 49f14d1886SSoby Mathew uint32_t plat_ic_get_pending_interrupt_id(void) 50f14d1886SSoby Mathew { 51f14d1886SSoby Mathew unsigned int irqnr; 52f14d1886SSoby Mathew 53f14d1886SSoby Mathew assert(IS_IN_EL3()); 54f14d1886SSoby Mathew irqnr = gicv3_get_pending_interrupt_id(); 55f14d1886SSoby Mathew return (gicv3_is_intr_id_special_identifier(irqnr)) ? 56f14d1886SSoby Mathew INTR_ID_UNAVAILABLE : irqnr; 57f14d1886SSoby Mathew } 58f14d1886SSoby Mathew 59f14d1886SSoby Mathew /* 60f14d1886SSoby Mathew * This function returns the type of the highest priority pending interrupt 61f14d1886SSoby Mathew * at the Interrupt controller. In the case of GICv3, the Highest Priority 62f14d1886SSoby Mathew * Pending interrupt system register (`ICC_HPPIR0_EL1`) is read to determine 63f14d1886SSoby Mathew * the id of the pending interrupt. The type of interrupt depends upon the 64f14d1886SSoby Mathew * id value as follows. 65f14d1886SSoby Mathew * 1. id = PENDING_G1S_INTID (1020) is reported as a S-EL1 interrupt 66f14d1886SSoby Mathew * 2. id = PENDING_G1NS_INTID (1021) is reported as a Non-secure interrupt. 67f14d1886SSoby Mathew * 3. id = GIC_SPURIOUS_INTERRUPT (1023) is reported as an invalid interrupt 68f14d1886SSoby Mathew * type. 69f14d1886SSoby Mathew * 4. All other interrupt id's are reported as EL3 interrupt. 70f14d1886SSoby Mathew */ 71f14d1886SSoby Mathew uint32_t plat_ic_get_pending_interrupt_type(void) 72f14d1886SSoby Mathew { 73f14d1886SSoby Mathew unsigned int irqnr; 74f14d1886SSoby Mathew 75f14d1886SSoby Mathew assert(IS_IN_EL3()); 76f14d1886SSoby Mathew irqnr = gicv3_get_pending_interrupt_type(); 77f14d1886SSoby Mathew 78f14d1886SSoby Mathew switch (irqnr) { 79f14d1886SSoby Mathew case PENDING_G1S_INTID: 80f14d1886SSoby Mathew return INTR_TYPE_S_EL1; 81f14d1886SSoby Mathew case PENDING_G1NS_INTID: 82f14d1886SSoby Mathew return INTR_TYPE_NS; 83f14d1886SSoby Mathew case GIC_SPURIOUS_INTERRUPT: 84f14d1886SSoby Mathew return INTR_TYPE_INVAL; 85f14d1886SSoby Mathew default: 86f14d1886SSoby Mathew return INTR_TYPE_EL3; 87f14d1886SSoby Mathew } 88f14d1886SSoby Mathew } 89f14d1886SSoby Mathew 90f14d1886SSoby Mathew /* 91f14d1886SSoby Mathew * This function returns the highest priority pending interrupt at 92f14d1886SSoby Mathew * the Interrupt controller and indicates to the Interrupt controller 93f14d1886SSoby Mathew * that the interrupt processing has started. 94f14d1886SSoby Mathew */ 95f14d1886SSoby Mathew uint32_t plat_ic_acknowledge_interrupt(void) 96f14d1886SSoby Mathew { 97f14d1886SSoby Mathew assert(IS_IN_EL3()); 98f14d1886SSoby Mathew return gicv3_acknowledge_interrupt(); 99f14d1886SSoby Mathew } 100f14d1886SSoby Mathew 101f14d1886SSoby Mathew /* 102f14d1886SSoby Mathew * This function returns the type of the interrupt `id`, depending on how 103f14d1886SSoby Mathew * the interrupt has been configured in the interrupt controller 104f14d1886SSoby Mathew */ 105f14d1886SSoby Mathew uint32_t plat_ic_get_interrupt_type(uint32_t id) 106f14d1886SSoby Mathew { 107f14d1886SSoby Mathew assert(IS_IN_EL3()); 108f14d1886SSoby Mathew return gicv3_get_interrupt_type(id, plat_my_core_pos()); 109f14d1886SSoby Mathew } 110f14d1886SSoby Mathew 111f14d1886SSoby Mathew /* 112f14d1886SSoby Mathew * This functions is used to indicate to the interrupt controller that 113f14d1886SSoby Mathew * the processing of the interrupt corresponding to the `id` has 114f14d1886SSoby Mathew * finished. 115f14d1886SSoby Mathew */ 116f14d1886SSoby Mathew void plat_ic_end_of_interrupt(uint32_t id) 117f14d1886SSoby Mathew { 118f14d1886SSoby Mathew assert(IS_IN_EL3()); 119f14d1886SSoby Mathew gicv3_end_of_interrupt(id); 120f14d1886SSoby Mathew } 121f14d1886SSoby Mathew 122f14d1886SSoby Mathew /* 123f14d1886SSoby Mathew * An ARM processor signals interrupt exceptions through the IRQ and FIQ pins. 124f14d1886SSoby Mathew * The interrupt controller knows which pin/line it uses to signal a type of 125f14d1886SSoby Mathew * interrupt. It lets the interrupt management framework determine for a type of 126f14d1886SSoby Mathew * interrupt and security state, which line should be used in the SCR_EL3 to 127f14d1886SSoby Mathew * control its routing to EL3. The interrupt line is represented as the bit 128f14d1886SSoby Mathew * position of the IRQ or FIQ bit in the SCR_EL3. 129f14d1886SSoby Mathew */ 130f14d1886SSoby Mathew uint32_t plat_interrupt_type_to_line(uint32_t type, 131f14d1886SSoby Mathew uint32_t security_state) 132f14d1886SSoby Mathew { 133f14d1886SSoby Mathew assert(type == INTR_TYPE_S_EL1 || 134f14d1886SSoby Mathew type == INTR_TYPE_EL3 || 135f14d1886SSoby Mathew type == INTR_TYPE_NS); 136f14d1886SSoby Mathew 137f14d1886SSoby Mathew assert(sec_state_is_valid(security_state)); 138f14d1886SSoby Mathew assert(IS_IN_EL3()); 139f14d1886SSoby Mathew 140f14d1886SSoby Mathew switch (type) { 141f14d1886SSoby Mathew case INTR_TYPE_S_EL1: 142f14d1886SSoby Mathew /* 143f14d1886SSoby Mathew * The S-EL1 interrupts are signaled as IRQ in S-EL0/1 contexts 144f14d1886SSoby Mathew * and as FIQ in the NS-EL0/1/2 contexts 145f14d1886SSoby Mathew */ 146f14d1886SSoby Mathew if (security_state == SECURE) 147f14d1886SSoby Mathew return __builtin_ctz(SCR_IRQ_BIT); 148f14d1886SSoby Mathew else 149f14d1886SSoby Mathew return __builtin_ctz(SCR_FIQ_BIT); 150f14d1886SSoby Mathew case INTR_TYPE_NS: 151f14d1886SSoby Mathew /* 152f14d1886SSoby Mathew * The Non secure interrupts will be signaled as FIQ in S-EL0/1 153f14d1886SSoby Mathew * contexts and as IRQ in the NS-EL0/1/2 contexts. 154f14d1886SSoby Mathew */ 155f14d1886SSoby Mathew if (security_state == SECURE) 156f14d1886SSoby Mathew return __builtin_ctz(SCR_FIQ_BIT); 157f14d1886SSoby Mathew else 158f14d1886SSoby Mathew return __builtin_ctz(SCR_IRQ_BIT); 159f14d1886SSoby Mathew default: 160f14d1886SSoby Mathew assert(0); 161f14d1886SSoby Mathew /* Fall through in the release build */ 162f14d1886SSoby Mathew case INTR_TYPE_EL3: 163f14d1886SSoby Mathew /* 164f14d1886SSoby Mathew * The EL3 interrupts are signaled as FIQ in both S-EL0/1 and 165f14d1886SSoby Mathew * NS-EL0/1/2 contexts 166f14d1886SSoby Mathew */ 167f14d1886SSoby Mathew return __builtin_ctz(SCR_FIQ_BIT); 168f14d1886SSoby Mathew } 169f14d1886SSoby Mathew } 170eb68ea9bSJeenu Viswambharan 171eb68ea9bSJeenu Viswambharan unsigned int plat_ic_get_running_priority(void) 172eb68ea9bSJeenu Viswambharan { 173eb68ea9bSJeenu Viswambharan return gicv3_get_running_priority(); 174eb68ea9bSJeenu Viswambharan } 175eb68ea9bSJeenu Viswambharan 176ca43b55dSJeenu Viswambharan int plat_ic_is_spi(unsigned int id) 177ca43b55dSJeenu Viswambharan { 178ca43b55dSJeenu Viswambharan return (id >= MIN_SPI_ID) && (id <= MAX_SPI_ID); 179ca43b55dSJeenu Viswambharan } 180ca43b55dSJeenu Viswambharan 181ca43b55dSJeenu Viswambharan int plat_ic_is_ppi(unsigned int id) 182ca43b55dSJeenu Viswambharan { 183ca43b55dSJeenu Viswambharan return (id >= MIN_PPI_ID) && (id < MIN_SPI_ID); 184ca43b55dSJeenu Viswambharan } 185ca43b55dSJeenu Viswambharan 186ca43b55dSJeenu Viswambharan int plat_ic_is_sgi(unsigned int id) 187ca43b55dSJeenu Viswambharan { 188ca43b55dSJeenu Viswambharan return (id >= MIN_SGI_ID) && (id < MIN_PPI_ID); 189ca43b55dSJeenu Viswambharan } 190cbd3f370SJeenu Viswambharan 191cbd3f370SJeenu Viswambharan unsigned int plat_ic_get_interrupt_active(unsigned int id) 192cbd3f370SJeenu Viswambharan { 193cbd3f370SJeenu Viswambharan return gicv3_get_interrupt_active(id, plat_my_core_pos()); 194cbd3f370SJeenu Viswambharan } 195979225f4SJeenu Viswambharan 196979225f4SJeenu Viswambharan void plat_ic_enable_interrupt(unsigned int id) 197979225f4SJeenu Viswambharan { 198979225f4SJeenu Viswambharan gicv3_enable_interrupt(id, plat_my_core_pos()); 199979225f4SJeenu Viswambharan } 200979225f4SJeenu Viswambharan 201979225f4SJeenu Viswambharan void plat_ic_disable_interrupt(unsigned int id) 202979225f4SJeenu Viswambharan { 203979225f4SJeenu Viswambharan gicv3_disable_interrupt(id, plat_my_core_pos()); 204979225f4SJeenu Viswambharan } 205f3a86600SJeenu Viswambharan 206f3a86600SJeenu Viswambharan void plat_ic_set_interrupt_priority(unsigned int id, unsigned int priority) 207f3a86600SJeenu Viswambharan { 208f3a86600SJeenu Viswambharan gicv3_set_interrupt_priority(id, plat_my_core_pos(), priority); 209f3a86600SJeenu Viswambharan } 21074dce7faSJeenu Viswambharan 21174dce7faSJeenu Viswambharan int plat_ic_has_interrupt_type(unsigned int type) 21274dce7faSJeenu Viswambharan { 21374dce7faSJeenu Viswambharan assert((type == INTR_TYPE_EL3) || (type == INTR_TYPE_S_EL1) || 21474dce7faSJeenu Viswambharan (type == INTR_TYPE_NS)); 21574dce7faSJeenu Viswambharan return 1; 21674dce7faSJeenu Viswambharan } 21774dce7faSJeenu Viswambharan 21874dce7faSJeenu Viswambharan void plat_ic_set_interrupt_type(unsigned int id, unsigned int type) 21974dce7faSJeenu Viswambharan { 22074dce7faSJeenu Viswambharan gicv3_set_interrupt_type(id, plat_my_core_pos(), type); 22174dce7faSJeenu Viswambharan } 2228db978b5SJeenu Viswambharan 2238db978b5SJeenu Viswambharan void plat_ic_raise_el3_sgi(int sgi_num, u_register_t target) 2248db978b5SJeenu Viswambharan { 2258db978b5SJeenu Viswambharan /* Target must be a valid MPIDR in the system */ 2268db978b5SJeenu Viswambharan assert(plat_core_pos_by_mpidr(target) >= 0); 2278db978b5SJeenu Viswambharan 2288db978b5SJeenu Viswambharan /* Verify that this is a secure EL3 SGI */ 2298db978b5SJeenu Viswambharan assert(plat_ic_get_interrupt_type(sgi_num) == INTR_TYPE_EL3); 2308db978b5SJeenu Viswambharan 2318db978b5SJeenu Viswambharan gicv3_raise_secure_g0_sgi(sgi_num, target); 2328db978b5SJeenu Viswambharan } 233*fc529feeSJeenu Viswambharan 234*fc529feeSJeenu Viswambharan void plat_ic_set_spi_routing(unsigned int id, unsigned int routing_mode, 235*fc529feeSJeenu Viswambharan u_register_t mpidr) 236*fc529feeSJeenu Viswambharan { 237*fc529feeSJeenu Viswambharan unsigned int irm = 0; 238*fc529feeSJeenu Viswambharan 239*fc529feeSJeenu Viswambharan switch (routing_mode) { 240*fc529feeSJeenu Viswambharan case INTR_ROUTING_MODE_PE: 241*fc529feeSJeenu Viswambharan assert(plat_core_pos_by_mpidr(mpidr) >= 0); 242*fc529feeSJeenu Viswambharan irm = GICV3_IRM_PE; 243*fc529feeSJeenu Viswambharan break; 244*fc529feeSJeenu Viswambharan case INTR_ROUTING_MODE_ANY: 245*fc529feeSJeenu Viswambharan irm = GICV3_IRM_ANY; 246*fc529feeSJeenu Viswambharan break; 247*fc529feeSJeenu Viswambharan default: 248*fc529feeSJeenu Viswambharan assert(0); 249*fc529feeSJeenu Viswambharan } 250*fc529feeSJeenu Viswambharan 251*fc529feeSJeenu Viswambharan gicv3_set_spi_routing(id, irm, mpidr); 252*fc529feeSJeenu Viswambharan } 253f14d1886SSoby Mathew #endif 2543d8256b2SMasahiro Yamada #ifdef IMAGE_BL32 255f14d1886SSoby Mathew 256f14d1886SSoby Mathew #pragma weak plat_ic_get_pending_interrupt_id 257f14d1886SSoby Mathew #pragma weak plat_ic_acknowledge_interrupt 258f14d1886SSoby Mathew #pragma weak plat_ic_end_of_interrupt 259f14d1886SSoby Mathew 260877cf3ffSSoby Mathew /* In AArch32, the secure group1 interrupts are targeted to Secure PL1 */ 261877cf3ffSSoby Mathew #ifdef AARCH32 262877cf3ffSSoby Mathew #define IS_IN_EL1() IS_IN_SECURE() 263877cf3ffSSoby Mathew #endif 264877cf3ffSSoby Mathew 265f14d1886SSoby Mathew /* 266f14d1886SSoby Mathew * This function returns the highest priority pending interrupt at 267f14d1886SSoby Mathew * the Interrupt controller 268f14d1886SSoby Mathew */ 269f14d1886SSoby Mathew uint32_t plat_ic_get_pending_interrupt_id(void) 270f14d1886SSoby Mathew { 271f14d1886SSoby Mathew unsigned int irqnr; 272f14d1886SSoby Mathew 273f14d1886SSoby Mathew assert(IS_IN_EL1()); 274f14d1886SSoby Mathew irqnr = gicv3_get_pending_interrupt_id_sel1(); 275f14d1886SSoby Mathew return (irqnr == GIC_SPURIOUS_INTERRUPT) ? 276f14d1886SSoby Mathew INTR_ID_UNAVAILABLE : irqnr; 277f14d1886SSoby Mathew } 278f14d1886SSoby Mathew 279f14d1886SSoby Mathew /* 280f14d1886SSoby Mathew * This function returns the highest priority pending interrupt at 281f14d1886SSoby Mathew * the Interrupt controller and indicates to the Interrupt controller 282f14d1886SSoby Mathew * that the interrupt processing has started. 283f14d1886SSoby Mathew */ 284f14d1886SSoby Mathew uint32_t plat_ic_acknowledge_interrupt(void) 285f14d1886SSoby Mathew { 286f14d1886SSoby Mathew assert(IS_IN_EL1()); 287f14d1886SSoby Mathew return gicv3_acknowledge_interrupt_sel1(); 288f14d1886SSoby Mathew } 289f14d1886SSoby Mathew 290f14d1886SSoby Mathew /* 291f14d1886SSoby Mathew * This functions is used to indicate to the interrupt controller that 292f14d1886SSoby Mathew * the processing of the interrupt corresponding to the `id` has 293f14d1886SSoby Mathew * finished. 294f14d1886SSoby Mathew */ 295f14d1886SSoby Mathew void plat_ic_end_of_interrupt(uint32_t id) 296f14d1886SSoby Mathew { 297f14d1886SSoby Mathew assert(IS_IN_EL1()); 298f14d1886SSoby Mathew gicv3_end_of_interrupt_sel1(id); 299f14d1886SSoby Mathew } 300f14d1886SSoby Mathew #endif 301