1f14d1886SSoby Mathew /* 2ab80cf35SMadhukar Pappireddy * Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved. 3dcb31ff7SFlorian Lugou * Portions copyright (c) 2021-2022, ProvenRun S.A.S. All rights reserved. 4f14d1886SSoby Mathew * 582cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 6f14d1886SSoby Mathew */ 709d40e0eSAntonio Nino Diaz 8f14d1886SSoby Mathew #include <assert.h> 9e0ced7a9SAntonio Nino Diaz #include <stdbool.h> 10f14d1886SSoby Mathew 1109d40e0eSAntonio Nino Diaz #include <bl31/interrupt_mgmt.h> 1209d40e0eSAntonio Nino Diaz #include <drivers/arm/gic_common.h> 1309d40e0eSAntonio Nino Diaz #include <drivers/arm/gicv2.h> 1409d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 1509d40e0eSAntonio Nino Diaz 16f14d1886SSoby Mathew /* 17f14d1886SSoby Mathew * The following platform GIC functions are weakly defined. They 18f14d1886SSoby Mathew * provide typical implementations that may be re-used by multiple 19f14d1886SSoby Mathew * platforms but may also be overridden by a platform if required. 20f14d1886SSoby Mathew */ 21f14d1886SSoby Mathew #pragma weak plat_ic_get_pending_interrupt_id 22f14d1886SSoby Mathew #pragma weak plat_ic_get_pending_interrupt_type 23f14d1886SSoby Mathew #pragma weak plat_ic_acknowledge_interrupt 24f14d1886SSoby Mathew #pragma weak plat_ic_get_interrupt_type 25f14d1886SSoby Mathew #pragma weak plat_ic_end_of_interrupt 26f14d1886SSoby Mathew #pragma weak plat_interrupt_type_to_line 27f14d1886SSoby Mathew 28eb68ea9bSJeenu Viswambharan #pragma weak plat_ic_get_running_priority 29ca43b55dSJeenu Viswambharan #pragma weak plat_ic_is_spi 30ca43b55dSJeenu Viswambharan #pragma weak plat_ic_is_ppi 31ca43b55dSJeenu Viswambharan #pragma weak plat_ic_is_sgi 32cbd3f370SJeenu Viswambharan #pragma weak plat_ic_get_interrupt_active 33979225f4SJeenu Viswambharan #pragma weak plat_ic_enable_interrupt 34979225f4SJeenu Viswambharan #pragma weak plat_ic_disable_interrupt 35f3a86600SJeenu Viswambharan #pragma weak plat_ic_set_interrupt_priority 3674dce7faSJeenu Viswambharan #pragma weak plat_ic_set_interrupt_type 378db978b5SJeenu Viswambharan #pragma weak plat_ic_raise_el3_sgi 38dcb31ff7SFlorian Lugou #pragma weak plat_ic_raise_ns_sgi 39dcb31ff7SFlorian Lugou #pragma weak plat_ic_raise_s_el1_sgi 40fc529feeSJeenu Viswambharan #pragma weak plat_ic_set_spi_routing 41eb68ea9bSJeenu Viswambharan 42f14d1886SSoby Mathew /* 43f14d1886SSoby Mathew * This function returns the highest priority pending interrupt at 44f14d1886SSoby Mathew * the Interrupt controller 45f14d1886SSoby Mathew */ 46f14d1886SSoby Mathew uint32_t plat_ic_get_pending_interrupt_id(void) 47f14d1886SSoby Mathew { 48f14d1886SSoby Mathew unsigned int id; 49f14d1886SSoby Mathew 50f14d1886SSoby Mathew id = gicv2_get_pending_interrupt_id(); 51*50029b9aSMaheedhar Bollapalli if (id == GIC_SPURIOUS_INTERRUPT) { 52*50029b9aSMaheedhar Bollapalli id = INTR_ID_UNAVAILABLE; 53*50029b9aSMaheedhar Bollapalli } 54f14d1886SSoby Mathew 55f14d1886SSoby Mathew return id; 56f14d1886SSoby Mathew } 57f14d1886SSoby Mathew 58f14d1886SSoby Mathew /* 59f14d1886SSoby Mathew * This function returns the type of the highest priority pending interrupt 60f14d1886SSoby Mathew * at the Interrupt controller. In the case of GICv2, the Highest Priority 61f14d1886SSoby Mathew * Pending interrupt register (`GICC_HPPIR`) is read to determine the id of 62f14d1886SSoby Mathew * the pending interrupt. The type of interrupt depends upon the id value 63f14d1886SSoby Mathew * as follows. 64f14d1886SSoby Mathew * 1. id < PENDING_G1_INTID (1022) is reported as a S-EL1 interrupt 65f14d1886SSoby Mathew * 2. id = PENDING_G1_INTID (1022) is reported as a Non-secure interrupt. 66f14d1886SSoby Mathew * 3. id = GIC_SPURIOUS_INTERRUPT (1023) is reported as an invalid interrupt 67f14d1886SSoby Mathew * type. 68f14d1886SSoby Mathew */ 69f14d1886SSoby Mathew uint32_t plat_ic_get_pending_interrupt_type(void) 70f14d1886SSoby Mathew { 71f14d1886SSoby Mathew unsigned int id; 72*50029b9aSMaheedhar Bollapalli uint32_t interrupt_type; 73f14d1886SSoby Mathew 74f14d1886SSoby Mathew id = gicv2_get_pending_interrupt_type(); 75f14d1886SSoby Mathew 76f14d1886SSoby Mathew /* Assume that all secure interrupts are S-EL1 interrupts */ 7774dce7faSJeenu Viswambharan if (id < PENDING_G1_INTID) { 7874dce7faSJeenu Viswambharan #if GICV2_G0_FOR_EL3 79*50029b9aSMaheedhar Bollapalli interrupt_type = INTR_TYPE_EL3; 8074dce7faSJeenu Viswambharan #else 81*50029b9aSMaheedhar Bollapalli interrupt_type = INTR_TYPE_S_EL1; 8274dce7faSJeenu Viswambharan #endif 83*50029b9aSMaheedhar Bollapalli } else { 84f14d1886SSoby Mathew 857e288d11SMaheedhar Bollapalli if (id == GIC_SPURIOUS_INTERRUPT) { 86*50029b9aSMaheedhar Bollapalli interrupt_type = INTR_TYPE_INVAL; 87*50029b9aSMaheedhar Bollapalli } else { 88*50029b9aSMaheedhar Bollapalli interrupt_type = INTR_TYPE_NS; 897e288d11SMaheedhar Bollapalli } 90*50029b9aSMaheedhar Bollapalli } 91*50029b9aSMaheedhar Bollapalli 92*50029b9aSMaheedhar Bollapalli return interrupt_type; 93f14d1886SSoby Mathew } 94f14d1886SSoby Mathew 95f14d1886SSoby Mathew /* 96f14d1886SSoby Mathew * This function returns the highest priority pending interrupt at 97f14d1886SSoby Mathew * the Interrupt controller and indicates to the Interrupt controller 98f14d1886SSoby Mathew * that the interrupt processing has started. 99f14d1886SSoby Mathew */ 100f14d1886SSoby Mathew uint32_t plat_ic_acknowledge_interrupt(void) 101f14d1886SSoby Mathew { 102f14d1886SSoby Mathew return gicv2_acknowledge_interrupt(); 103f14d1886SSoby Mathew } 104f14d1886SSoby Mathew 105f14d1886SSoby Mathew /* 106f14d1886SSoby Mathew * This function returns the type of the interrupt `id`, depending on how 107f14d1886SSoby Mathew * the interrupt has been configured in the interrupt controller 108f14d1886SSoby Mathew */ 109f14d1886SSoby Mathew uint32_t plat_ic_get_interrupt_type(uint32_t id) 110f14d1886SSoby Mathew { 111f14d1886SSoby Mathew unsigned int type; 112f14d1886SSoby Mathew 113f14d1886SSoby Mathew type = gicv2_get_interrupt_group(id); 114f14d1886SSoby Mathew 115f14d1886SSoby Mathew /* Assume that all secure interrupts are S-EL1 interrupts */ 116e0ced7a9SAntonio Nino Diaz return (type == GICV2_INTR_GROUP1) ? INTR_TYPE_NS : 11774dce7faSJeenu Viswambharan #if GICV2_G0_FOR_EL3 11874dce7faSJeenu Viswambharan INTR_TYPE_EL3; 11974dce7faSJeenu Viswambharan #else 12074dce7faSJeenu Viswambharan INTR_TYPE_S_EL1; 12174dce7faSJeenu Viswambharan #endif 122f14d1886SSoby Mathew } 123f14d1886SSoby Mathew 124f14d1886SSoby Mathew /* 125f14d1886SSoby Mathew * This functions is used to indicate to the interrupt controller that 126f14d1886SSoby Mathew * the processing of the interrupt corresponding to the `id` has 127f14d1886SSoby Mathew * finished. 128f14d1886SSoby Mathew */ 129f14d1886SSoby Mathew void plat_ic_end_of_interrupt(uint32_t id) 130f14d1886SSoby Mathew { 131f14d1886SSoby Mathew gicv2_end_of_interrupt(id); 132f14d1886SSoby Mathew } 133f14d1886SSoby Mathew 134f14d1886SSoby Mathew /* 135f14d1886SSoby Mathew * An ARM processor signals interrupt exceptions through the IRQ and FIQ pins. 136f14d1886SSoby Mathew * The interrupt controller knows which pin/line it uses to signal a type of 137f14d1886SSoby Mathew * interrupt. It lets the interrupt management framework determine 138f14d1886SSoby Mathew * for a type of interrupt and security state, which line should be used in the 139f14d1886SSoby Mathew * SCR_EL3 to control its routing to EL3. The interrupt line is represented 140f14d1886SSoby Mathew * as the bit position of the IRQ or FIQ bit in the SCR_EL3. 141f14d1886SSoby Mathew */ 142f14d1886SSoby Mathew uint32_t plat_interrupt_type_to_line(uint32_t type, 143f14d1886SSoby Mathew uint32_t security_state) 144f14d1886SSoby Mathew { 145e0ced7a9SAntonio Nino Diaz assert((type == INTR_TYPE_S_EL1) || (type == INTR_TYPE_EL3) || 146e0ced7a9SAntonio Nino Diaz (type == INTR_TYPE_NS)); 147f14d1886SSoby Mathew 14853a98be3SSanteri Salko assert(sec_state_is_valid(security_state)); 14953a98be3SSanteri Salko 150f14d1886SSoby Mathew /* Non-secure interrupts are signaled on the IRQ line always */ 1517e288d11SMaheedhar Bollapalli if (type == INTR_TYPE_NS) { 152f14d1886SSoby Mathew return __builtin_ctz(SCR_IRQ_BIT); 1537e288d11SMaheedhar Bollapalli } 154f14d1886SSoby Mathew 155f14d1886SSoby Mathew /* 156f14d1886SSoby Mathew * Secure interrupts are signaled using the IRQ line if the FIQ is 157f14d1886SSoby Mathew * not enabled else they are signaled using the FIQ line. 158f14d1886SSoby Mathew */ 159e0ced7a9SAntonio Nino Diaz return ((gicv2_is_fiq_enabled() != 0U) ? __builtin_ctz(SCR_FIQ_BIT) : 160f14d1886SSoby Mathew __builtin_ctz(SCR_IRQ_BIT)); 161f14d1886SSoby Mathew } 162eb68ea9bSJeenu Viswambharan 163eb68ea9bSJeenu Viswambharan unsigned int plat_ic_get_running_priority(void) 164eb68ea9bSJeenu Viswambharan { 165eb68ea9bSJeenu Viswambharan return gicv2_get_running_priority(); 166eb68ea9bSJeenu Viswambharan } 167ca43b55dSJeenu Viswambharan 168ca43b55dSJeenu Viswambharan int plat_ic_is_spi(unsigned int id) 169ca43b55dSJeenu Viswambharan { 170ca43b55dSJeenu Viswambharan return (id >= MIN_SPI_ID) && (id <= MAX_SPI_ID); 171ca43b55dSJeenu Viswambharan } 172ca43b55dSJeenu Viswambharan 173ca43b55dSJeenu Viswambharan int plat_ic_is_ppi(unsigned int id) 174ca43b55dSJeenu Viswambharan { 175ca43b55dSJeenu Viswambharan return (id >= MIN_PPI_ID) && (id < MIN_SPI_ID); 176ca43b55dSJeenu Viswambharan } 177ca43b55dSJeenu Viswambharan 178ca43b55dSJeenu Viswambharan int plat_ic_is_sgi(unsigned int id) 179ca43b55dSJeenu Viswambharan { 180ca43b55dSJeenu Viswambharan return (id >= MIN_SGI_ID) && (id < MIN_PPI_ID); 181ca43b55dSJeenu Viswambharan } 182cbd3f370SJeenu Viswambharan 183cbd3f370SJeenu Viswambharan unsigned int plat_ic_get_interrupt_active(unsigned int id) 184cbd3f370SJeenu Viswambharan { 185cbd3f370SJeenu Viswambharan return gicv2_get_interrupt_active(id); 186cbd3f370SJeenu Viswambharan } 187979225f4SJeenu Viswambharan 188979225f4SJeenu Viswambharan void plat_ic_enable_interrupt(unsigned int id) 189979225f4SJeenu Viswambharan { 190979225f4SJeenu Viswambharan gicv2_enable_interrupt(id); 191979225f4SJeenu Viswambharan } 192979225f4SJeenu Viswambharan 193979225f4SJeenu Viswambharan void plat_ic_disable_interrupt(unsigned int id) 194979225f4SJeenu Viswambharan { 195979225f4SJeenu Viswambharan gicv2_disable_interrupt(id); 196979225f4SJeenu Viswambharan } 197f3a86600SJeenu Viswambharan 198f3a86600SJeenu Viswambharan void plat_ic_set_interrupt_priority(unsigned int id, unsigned int priority) 199f3a86600SJeenu Viswambharan { 200f3a86600SJeenu Viswambharan gicv2_set_interrupt_priority(id, priority); 201f3a86600SJeenu Viswambharan } 20274dce7faSJeenu Viswambharan 2031f6bb41dSMadhukar Pappireddy bool plat_ic_has_interrupt_type(unsigned int type) 20474dce7faSJeenu Viswambharan { 2051f6bb41dSMadhukar Pappireddy bool has_interrupt_type = false; 206649c48f5SJonathan Wright 20774dce7faSJeenu Viswambharan switch (type) { 20874dce7faSJeenu Viswambharan #if GICV2_G0_FOR_EL3 20974dce7faSJeenu Viswambharan case INTR_TYPE_EL3: 21074dce7faSJeenu Viswambharan #else 21174dce7faSJeenu Viswambharan case INTR_TYPE_S_EL1: 21274dce7faSJeenu Viswambharan #endif 21374dce7faSJeenu Viswambharan case INTR_TYPE_NS: 2141f6bb41dSMadhukar Pappireddy has_interrupt_type = true; 215649c48f5SJonathan Wright break; 21674dce7faSJeenu Viswambharan default: 217649c48f5SJonathan Wright /* Do nothing in default case */ 218649c48f5SJonathan Wright break; 21974dce7faSJeenu Viswambharan } 220649c48f5SJonathan Wright 221649c48f5SJonathan Wright return has_interrupt_type; 22274dce7faSJeenu Viswambharan } 22374dce7faSJeenu Viswambharan 22474dce7faSJeenu Viswambharan void plat_ic_set_interrupt_type(unsigned int id, unsigned int type) 22574dce7faSJeenu Viswambharan { 226ab80cf35SMadhukar Pappireddy unsigned int gicv2_group = 0U; 22774dce7faSJeenu Viswambharan 22874dce7faSJeenu Viswambharan /* Map canonical interrupt type to GICv2 type */ 22974dce7faSJeenu Viswambharan switch (type) { 23074dce7faSJeenu Viswambharan #if GICV2_G0_FOR_EL3 23174dce7faSJeenu Viswambharan case INTR_TYPE_EL3: 23274dce7faSJeenu Viswambharan #else 23374dce7faSJeenu Viswambharan case INTR_TYPE_S_EL1: 23474dce7faSJeenu Viswambharan #endif 235ab80cf35SMadhukar Pappireddy gicv2_group = GICV2_INTR_GROUP0; 23674dce7faSJeenu Viswambharan break; 23774dce7faSJeenu Viswambharan case INTR_TYPE_NS: 238ab80cf35SMadhukar Pappireddy gicv2_group = GICV2_INTR_GROUP1; 23974dce7faSJeenu Viswambharan break; 24074dce7faSJeenu Viswambharan default: 241ab80cf35SMadhukar Pappireddy assert(false); /* Unreachable */ 242649c48f5SJonathan Wright break; 24374dce7faSJeenu Viswambharan } 24474dce7faSJeenu Viswambharan 245ab80cf35SMadhukar Pappireddy gicv2_set_interrupt_group(id, gicv2_group); 24674dce7faSJeenu Viswambharan } 2478db978b5SJeenu Viswambharan 2488db978b5SJeenu Viswambharan void plat_ic_raise_el3_sgi(int sgi_num, u_register_t target) 2498db978b5SJeenu Viswambharan { 2508db978b5SJeenu Viswambharan #if GICV2_G0_FOR_EL3 2518db978b5SJeenu Viswambharan int id; 2528db978b5SJeenu Viswambharan 2538db978b5SJeenu Viswambharan /* Target must be a valid MPIDR in the system */ 2548db978b5SJeenu Viswambharan id = plat_core_pos_by_mpidr(target); 2558db978b5SJeenu Viswambharan assert(id >= 0); 2568db978b5SJeenu Viswambharan 2578db978b5SJeenu Viswambharan /* Verify that this is a secure SGI */ 2588db978b5SJeenu Viswambharan assert(plat_ic_get_interrupt_type(sgi_num) == INTR_TYPE_EL3); 2598db978b5SJeenu Viswambharan 260dcb31ff7SFlorian Lugou gicv2_raise_sgi(sgi_num, false, id); 2618db978b5SJeenu Viswambharan #else 262e0ced7a9SAntonio Nino Diaz assert(false); 2638db978b5SJeenu Viswambharan #endif 2648db978b5SJeenu Viswambharan } 265fc529feeSJeenu Viswambharan 266dcb31ff7SFlorian Lugou void plat_ic_raise_ns_sgi(int sgi_num, u_register_t target) 267dcb31ff7SFlorian Lugou { 268dcb31ff7SFlorian Lugou int id; 269dcb31ff7SFlorian Lugou 270dcb31ff7SFlorian Lugou /* Target must be a valid MPIDR in the system */ 271dcb31ff7SFlorian Lugou id = plat_core_pos_by_mpidr(target); 272dcb31ff7SFlorian Lugou assert(id >= 0); 273dcb31ff7SFlorian Lugou 274dcb31ff7SFlorian Lugou /* Verify that this is a non-secure SGI */ 275dcb31ff7SFlorian Lugou assert(plat_ic_get_interrupt_type(sgi_num) == INTR_TYPE_NS); 276dcb31ff7SFlorian Lugou 277dcb31ff7SFlorian Lugou gicv2_raise_sgi(sgi_num, true, id); 278dcb31ff7SFlorian Lugou } 279dcb31ff7SFlorian Lugou 280dcb31ff7SFlorian Lugou void plat_ic_raise_s_el1_sgi(int sgi_num, u_register_t target) 281dcb31ff7SFlorian Lugou { 282dcb31ff7SFlorian Lugou #if GICV2_G0_FOR_EL3 283dcb31ff7SFlorian Lugou assert(false); 284dcb31ff7SFlorian Lugou #else 285dcb31ff7SFlorian Lugou int id; 286dcb31ff7SFlorian Lugou 287dcb31ff7SFlorian Lugou /* Target must be a valid MPIDR in the system */ 288dcb31ff7SFlorian Lugou id = plat_core_pos_by_mpidr(target); 289dcb31ff7SFlorian Lugou assert(id >= 0); 290dcb31ff7SFlorian Lugou 291dcb31ff7SFlorian Lugou /* Verify that this is a secure EL1 SGI */ 292dcb31ff7SFlorian Lugou assert(plat_ic_get_interrupt_type(sgi_num) == INTR_TYPE_S_EL1); 293dcb31ff7SFlorian Lugou 294dcb31ff7SFlorian Lugou gicv2_raise_sgi(sgi_num, false, id); 295dcb31ff7SFlorian Lugou #endif 296dcb31ff7SFlorian Lugou } 297dcb31ff7SFlorian Lugou 298fc529feeSJeenu Viswambharan void plat_ic_set_spi_routing(unsigned int id, unsigned int routing_mode, 299fc529feeSJeenu Viswambharan u_register_t mpidr) 300fc529feeSJeenu Viswambharan { 301fc529feeSJeenu Viswambharan int proc_num = 0; 302fc529feeSJeenu Viswambharan 303fc529feeSJeenu Viswambharan switch (routing_mode) { 304fc529feeSJeenu Viswambharan case INTR_ROUTING_MODE_PE: 305fc529feeSJeenu Viswambharan proc_num = plat_core_pos_by_mpidr(mpidr); 306fc529feeSJeenu Viswambharan assert(proc_num >= 0); 307fc529feeSJeenu Viswambharan break; 308fc529feeSJeenu Viswambharan case INTR_ROUTING_MODE_ANY: 309fc529feeSJeenu Viswambharan /* Bit mask selecting all 8 CPUs as candidates */ 310fc529feeSJeenu Viswambharan proc_num = -1; 311fc529feeSJeenu Viswambharan break; 312fc529feeSJeenu Viswambharan default: 313a08a2014SDaniel Boulby assert(0); /* Unreachable */ 314649c48f5SJonathan Wright break; 315fc529feeSJeenu Viswambharan } 316fc529feeSJeenu Viswambharan 317fc529feeSJeenu Viswambharan gicv2_set_spi_routing(id, proc_num); 318fc529feeSJeenu Viswambharan } 319a2816a16SJeenu Viswambharan 320a2816a16SJeenu Viswambharan void plat_ic_set_interrupt_pending(unsigned int id) 321a2816a16SJeenu Viswambharan { 322a2816a16SJeenu Viswambharan gicv2_set_interrupt_pending(id); 323a2816a16SJeenu Viswambharan } 324a2816a16SJeenu Viswambharan 325a2816a16SJeenu Viswambharan void plat_ic_clear_interrupt_pending(unsigned int id) 326a2816a16SJeenu Viswambharan { 327a2816a16SJeenu Viswambharan gicv2_clear_interrupt_pending(id); 328a2816a16SJeenu Viswambharan } 329d55a4450SJeenu Viswambharan 330d55a4450SJeenu Viswambharan unsigned int plat_ic_set_priority_mask(unsigned int mask) 331d55a4450SJeenu Viswambharan { 332d55a4450SJeenu Viswambharan return gicv2_set_pmr(mask); 333d55a4450SJeenu Viswambharan } 3344ee8d0beSJeenu Viswambharan 3354ee8d0beSJeenu Viswambharan unsigned int plat_ic_get_interrupt_id(unsigned int raw) 3364ee8d0beSJeenu Viswambharan { 3374ee8d0beSJeenu Viswambharan unsigned int id = (raw & INT_ID_MASK); 3384ee8d0beSJeenu Viswambharan 3397e288d11SMaheedhar Bollapalli if (id == GIC_SPURIOUS_INTERRUPT) { 3404ee8d0beSJeenu Viswambharan id = INTR_ID_UNAVAILABLE; 3417e288d11SMaheedhar Bollapalli } 3424ee8d0beSJeenu Viswambharan 3434ee8d0beSJeenu Viswambharan return id; 3444ee8d0beSJeenu Viswambharan } 345