1*f14d1886SSoby Mathew /* 2*f14d1886SSoby Mathew * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved. 3*f14d1886SSoby Mathew * 4*f14d1886SSoby Mathew * Redistribution and use in source and binary forms, with or without 5*f14d1886SSoby Mathew * modification, are permitted provided that the following conditions are met: 6*f14d1886SSoby Mathew * 7*f14d1886SSoby Mathew * Redistributions of source code must retain the above copyright notice, this 8*f14d1886SSoby Mathew * list of conditions and the following disclaimer. 9*f14d1886SSoby Mathew * 10*f14d1886SSoby Mathew * Redistributions in binary form must reproduce the above copyright notice, 11*f14d1886SSoby Mathew * this list of conditions and the following disclaimer in the documentation 12*f14d1886SSoby Mathew * and/or other materials provided with the distribution. 13*f14d1886SSoby Mathew * 14*f14d1886SSoby Mathew * Neither the name of ARM nor the names of its contributors may be used 15*f14d1886SSoby Mathew * to endorse or promote products derived from this software without specific 16*f14d1886SSoby Mathew * prior written permission. 17*f14d1886SSoby Mathew * 18*f14d1886SSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19*f14d1886SSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*f14d1886SSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*f14d1886SSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22*f14d1886SSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*f14d1886SSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*f14d1886SSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*f14d1886SSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*f14d1886SSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*f14d1886SSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*f14d1886SSoby Mathew * POSSIBILITY OF SUCH DAMAGE. 29*f14d1886SSoby Mathew */ 30*f14d1886SSoby Mathew #include <arch_helpers.h> 31*f14d1886SSoby Mathew #include <assert.h> 32*f14d1886SSoby Mathew #include <bl_common.h> 33*f14d1886SSoby Mathew #include <cassert.h> 34*f14d1886SSoby Mathew #include <gic_common.h> 35*f14d1886SSoby Mathew #include <gicv3.h> 36*f14d1886SSoby Mathew #include <interrupt_mgmt.h> 37*f14d1886SSoby Mathew #include <platform.h> 38*f14d1886SSoby Mathew 39*f14d1886SSoby Mathew #if IMAGE_BL31 40*f14d1886SSoby Mathew 41*f14d1886SSoby Mathew /* 42*f14d1886SSoby Mathew * The following platform GIC functions are weakly defined. They 43*f14d1886SSoby Mathew * provide typical implementations that may be re-used by multiple 44*f14d1886SSoby Mathew * platforms but may also be overridden by a platform if required. 45*f14d1886SSoby Mathew */ 46*f14d1886SSoby Mathew #pragma weak plat_ic_get_pending_interrupt_id 47*f14d1886SSoby Mathew #pragma weak plat_ic_get_pending_interrupt_type 48*f14d1886SSoby Mathew #pragma weak plat_ic_acknowledge_interrupt 49*f14d1886SSoby Mathew #pragma weak plat_ic_get_interrupt_type 50*f14d1886SSoby Mathew #pragma weak plat_ic_end_of_interrupt 51*f14d1886SSoby Mathew #pragma weak plat_interrupt_type_to_line 52*f14d1886SSoby Mathew 53*f14d1886SSoby Mathew CASSERT((INTR_TYPE_S_EL1 == INTR_GROUP1S) && 54*f14d1886SSoby Mathew (INTR_TYPE_NS == INTR_GROUP1NS) && 55*f14d1886SSoby Mathew (INTR_TYPE_EL3 == INTR_GROUP0), assert_interrupt_type_mismatch); 56*f14d1886SSoby Mathew 57*f14d1886SSoby Mathew /* 58*f14d1886SSoby Mathew * This function returns the highest priority pending interrupt at 59*f14d1886SSoby Mathew * the Interrupt controller 60*f14d1886SSoby Mathew */ 61*f14d1886SSoby Mathew uint32_t plat_ic_get_pending_interrupt_id(void) 62*f14d1886SSoby Mathew { 63*f14d1886SSoby Mathew unsigned int irqnr; 64*f14d1886SSoby Mathew 65*f14d1886SSoby Mathew assert(IS_IN_EL3()); 66*f14d1886SSoby Mathew irqnr = gicv3_get_pending_interrupt_id(); 67*f14d1886SSoby Mathew return (gicv3_is_intr_id_special_identifier(irqnr)) ? 68*f14d1886SSoby Mathew INTR_ID_UNAVAILABLE : irqnr; 69*f14d1886SSoby Mathew } 70*f14d1886SSoby Mathew 71*f14d1886SSoby Mathew /* 72*f14d1886SSoby Mathew * This function returns the type of the highest priority pending interrupt 73*f14d1886SSoby Mathew * at the Interrupt controller. In the case of GICv3, the Highest Priority 74*f14d1886SSoby Mathew * Pending interrupt system register (`ICC_HPPIR0_EL1`) is read to determine 75*f14d1886SSoby Mathew * the id of the pending interrupt. The type of interrupt depends upon the 76*f14d1886SSoby Mathew * id value as follows. 77*f14d1886SSoby Mathew * 1. id = PENDING_G1S_INTID (1020) is reported as a S-EL1 interrupt 78*f14d1886SSoby Mathew * 2. id = PENDING_G1NS_INTID (1021) is reported as a Non-secure interrupt. 79*f14d1886SSoby Mathew * 3. id = GIC_SPURIOUS_INTERRUPT (1023) is reported as an invalid interrupt 80*f14d1886SSoby Mathew * type. 81*f14d1886SSoby Mathew * 4. All other interrupt id's are reported as EL3 interrupt. 82*f14d1886SSoby Mathew */ 83*f14d1886SSoby Mathew uint32_t plat_ic_get_pending_interrupt_type(void) 84*f14d1886SSoby Mathew { 85*f14d1886SSoby Mathew unsigned int irqnr; 86*f14d1886SSoby Mathew 87*f14d1886SSoby Mathew assert(IS_IN_EL3()); 88*f14d1886SSoby Mathew irqnr = gicv3_get_pending_interrupt_type(); 89*f14d1886SSoby Mathew 90*f14d1886SSoby Mathew switch (irqnr) { 91*f14d1886SSoby Mathew case PENDING_G1S_INTID: 92*f14d1886SSoby Mathew return INTR_TYPE_S_EL1; 93*f14d1886SSoby Mathew case PENDING_G1NS_INTID: 94*f14d1886SSoby Mathew return INTR_TYPE_NS; 95*f14d1886SSoby Mathew case GIC_SPURIOUS_INTERRUPT: 96*f14d1886SSoby Mathew return INTR_TYPE_INVAL; 97*f14d1886SSoby Mathew default: 98*f14d1886SSoby Mathew return INTR_TYPE_EL3; 99*f14d1886SSoby Mathew } 100*f14d1886SSoby Mathew } 101*f14d1886SSoby Mathew 102*f14d1886SSoby Mathew /* 103*f14d1886SSoby Mathew * This function returns the highest priority pending interrupt at 104*f14d1886SSoby Mathew * the Interrupt controller and indicates to the Interrupt controller 105*f14d1886SSoby Mathew * that the interrupt processing has started. 106*f14d1886SSoby Mathew */ 107*f14d1886SSoby Mathew uint32_t plat_ic_acknowledge_interrupt(void) 108*f14d1886SSoby Mathew { 109*f14d1886SSoby Mathew assert(IS_IN_EL3()); 110*f14d1886SSoby Mathew return gicv3_acknowledge_interrupt(); 111*f14d1886SSoby Mathew } 112*f14d1886SSoby Mathew 113*f14d1886SSoby Mathew /* 114*f14d1886SSoby Mathew * This function returns the type of the interrupt `id`, depending on how 115*f14d1886SSoby Mathew * the interrupt has been configured in the interrupt controller 116*f14d1886SSoby Mathew */ 117*f14d1886SSoby Mathew uint32_t plat_ic_get_interrupt_type(uint32_t id) 118*f14d1886SSoby Mathew { 119*f14d1886SSoby Mathew assert(IS_IN_EL3()); 120*f14d1886SSoby Mathew return gicv3_get_interrupt_type(id, plat_my_core_pos()); 121*f14d1886SSoby Mathew } 122*f14d1886SSoby Mathew 123*f14d1886SSoby Mathew /* 124*f14d1886SSoby Mathew * This functions is used to indicate to the interrupt controller that 125*f14d1886SSoby Mathew * the processing of the interrupt corresponding to the `id` has 126*f14d1886SSoby Mathew * finished. 127*f14d1886SSoby Mathew */ 128*f14d1886SSoby Mathew void plat_ic_end_of_interrupt(uint32_t id) 129*f14d1886SSoby Mathew { 130*f14d1886SSoby Mathew assert(IS_IN_EL3()); 131*f14d1886SSoby Mathew gicv3_end_of_interrupt(id); 132*f14d1886SSoby Mathew } 133*f14d1886SSoby Mathew 134*f14d1886SSoby Mathew /* 135*f14d1886SSoby Mathew * An ARM processor signals interrupt exceptions through the IRQ and FIQ pins. 136*f14d1886SSoby Mathew * The interrupt controller knows which pin/line it uses to signal a type of 137*f14d1886SSoby Mathew * interrupt. It lets the interrupt management framework determine for a type of 138*f14d1886SSoby Mathew * interrupt and security state, which line should be used in the SCR_EL3 to 139*f14d1886SSoby Mathew * control its routing to EL3. The interrupt line is represented as the bit 140*f14d1886SSoby Mathew * position of the IRQ or FIQ bit in the SCR_EL3. 141*f14d1886SSoby Mathew */ 142*f14d1886SSoby Mathew uint32_t plat_interrupt_type_to_line(uint32_t type, 143*f14d1886SSoby Mathew uint32_t security_state) 144*f14d1886SSoby Mathew { 145*f14d1886SSoby Mathew assert(type == INTR_TYPE_S_EL1 || 146*f14d1886SSoby Mathew type == INTR_TYPE_EL3 || 147*f14d1886SSoby Mathew type == INTR_TYPE_NS); 148*f14d1886SSoby Mathew 149*f14d1886SSoby Mathew assert(sec_state_is_valid(security_state)); 150*f14d1886SSoby Mathew assert(IS_IN_EL3()); 151*f14d1886SSoby Mathew 152*f14d1886SSoby Mathew switch (type) { 153*f14d1886SSoby Mathew case INTR_TYPE_S_EL1: 154*f14d1886SSoby Mathew /* 155*f14d1886SSoby Mathew * The S-EL1 interrupts are signaled as IRQ in S-EL0/1 contexts 156*f14d1886SSoby Mathew * and as FIQ in the NS-EL0/1/2 contexts 157*f14d1886SSoby Mathew */ 158*f14d1886SSoby Mathew if (security_state == SECURE) 159*f14d1886SSoby Mathew return __builtin_ctz(SCR_IRQ_BIT); 160*f14d1886SSoby Mathew else 161*f14d1886SSoby Mathew return __builtin_ctz(SCR_FIQ_BIT); 162*f14d1886SSoby Mathew case INTR_TYPE_NS: 163*f14d1886SSoby Mathew /* 164*f14d1886SSoby Mathew * The Non secure interrupts will be signaled as FIQ in S-EL0/1 165*f14d1886SSoby Mathew * contexts and as IRQ in the NS-EL0/1/2 contexts. 166*f14d1886SSoby Mathew */ 167*f14d1886SSoby Mathew if (security_state == SECURE) 168*f14d1886SSoby Mathew return __builtin_ctz(SCR_FIQ_BIT); 169*f14d1886SSoby Mathew else 170*f14d1886SSoby Mathew return __builtin_ctz(SCR_IRQ_BIT); 171*f14d1886SSoby Mathew default: 172*f14d1886SSoby Mathew assert(0); 173*f14d1886SSoby Mathew /* Fall through in the release build */ 174*f14d1886SSoby Mathew case INTR_TYPE_EL3: 175*f14d1886SSoby Mathew /* 176*f14d1886SSoby Mathew * The EL3 interrupts are signaled as FIQ in both S-EL0/1 and 177*f14d1886SSoby Mathew * NS-EL0/1/2 contexts 178*f14d1886SSoby Mathew */ 179*f14d1886SSoby Mathew return __builtin_ctz(SCR_FIQ_BIT); 180*f14d1886SSoby Mathew } 181*f14d1886SSoby Mathew } 182*f14d1886SSoby Mathew #endif 183*f14d1886SSoby Mathew #if IMAGE_BL32 184*f14d1886SSoby Mathew 185*f14d1886SSoby Mathew #pragma weak plat_ic_get_pending_interrupt_id 186*f14d1886SSoby Mathew #pragma weak plat_ic_acknowledge_interrupt 187*f14d1886SSoby Mathew #pragma weak plat_ic_end_of_interrupt 188*f14d1886SSoby Mathew 189*f14d1886SSoby Mathew /* 190*f14d1886SSoby Mathew * This function returns the highest priority pending interrupt at 191*f14d1886SSoby Mathew * the Interrupt controller 192*f14d1886SSoby Mathew */ 193*f14d1886SSoby Mathew uint32_t plat_ic_get_pending_interrupt_id(void) 194*f14d1886SSoby Mathew { 195*f14d1886SSoby Mathew unsigned int irqnr; 196*f14d1886SSoby Mathew 197*f14d1886SSoby Mathew assert(IS_IN_EL1()); 198*f14d1886SSoby Mathew irqnr = gicv3_get_pending_interrupt_id_sel1(); 199*f14d1886SSoby Mathew return (irqnr == GIC_SPURIOUS_INTERRUPT) ? 200*f14d1886SSoby Mathew INTR_ID_UNAVAILABLE : irqnr; 201*f14d1886SSoby Mathew } 202*f14d1886SSoby Mathew 203*f14d1886SSoby Mathew /* 204*f14d1886SSoby Mathew * This function returns the highest priority pending interrupt at 205*f14d1886SSoby Mathew * the Interrupt controller and indicates to the Interrupt controller 206*f14d1886SSoby Mathew * that the interrupt processing has started. 207*f14d1886SSoby Mathew */ 208*f14d1886SSoby Mathew uint32_t plat_ic_acknowledge_interrupt(void) 209*f14d1886SSoby Mathew { 210*f14d1886SSoby Mathew assert(IS_IN_EL1()); 211*f14d1886SSoby Mathew return gicv3_acknowledge_interrupt_sel1(); 212*f14d1886SSoby Mathew } 213*f14d1886SSoby Mathew 214*f14d1886SSoby Mathew /* 215*f14d1886SSoby Mathew * This functions is used to indicate to the interrupt controller that 216*f14d1886SSoby Mathew * the processing of the interrupt corresponding to the `id` has 217*f14d1886SSoby Mathew * finished. 218*f14d1886SSoby Mathew */ 219*f14d1886SSoby Mathew void plat_ic_end_of_interrupt(uint32_t id) 220*f14d1886SSoby Mathew { 221*f14d1886SSoby Mathew assert(IS_IN_EL1()); 222*f14d1886SSoby Mathew gicv3_end_of_interrupt_sel1(id); 223*f14d1886SSoby Mathew } 224*f14d1886SSoby Mathew #endif 225