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 <assert.h> 31*f14d1886SSoby Mathew #include <gic_common.h> 32*f14d1886SSoby Mathew #include <gicv2.h> 33*f14d1886SSoby Mathew #include <interrupt_mgmt.h> 34*f14d1886SSoby Mathew 35*f14d1886SSoby Mathew /* 36*f14d1886SSoby Mathew * The following platform GIC functions are weakly defined. They 37*f14d1886SSoby Mathew * provide typical implementations that may be re-used by multiple 38*f14d1886SSoby Mathew * platforms but may also be overridden by a platform if required. 39*f14d1886SSoby Mathew */ 40*f14d1886SSoby Mathew #pragma weak plat_ic_get_pending_interrupt_id 41*f14d1886SSoby Mathew #pragma weak plat_ic_get_pending_interrupt_type 42*f14d1886SSoby Mathew #pragma weak plat_ic_acknowledge_interrupt 43*f14d1886SSoby Mathew #pragma weak plat_ic_get_interrupt_type 44*f14d1886SSoby Mathew #pragma weak plat_ic_end_of_interrupt 45*f14d1886SSoby Mathew #pragma weak plat_interrupt_type_to_line 46*f14d1886SSoby Mathew 47*f14d1886SSoby Mathew /* 48*f14d1886SSoby Mathew * This function returns the highest priority pending interrupt at 49*f14d1886SSoby Mathew * the Interrupt controller 50*f14d1886SSoby Mathew */ 51*f14d1886SSoby Mathew uint32_t plat_ic_get_pending_interrupt_id(void) 52*f14d1886SSoby Mathew { 53*f14d1886SSoby Mathew unsigned int id; 54*f14d1886SSoby Mathew 55*f14d1886SSoby Mathew id = gicv2_get_pending_interrupt_id(); 56*f14d1886SSoby Mathew if (id == GIC_SPURIOUS_INTERRUPT) 57*f14d1886SSoby Mathew return INTR_ID_UNAVAILABLE; 58*f14d1886SSoby Mathew 59*f14d1886SSoby Mathew return id; 60*f14d1886SSoby Mathew } 61*f14d1886SSoby Mathew 62*f14d1886SSoby Mathew /* 63*f14d1886SSoby Mathew * This function returns the type of the highest priority pending interrupt 64*f14d1886SSoby Mathew * at the Interrupt controller. In the case of GICv2, the Highest Priority 65*f14d1886SSoby Mathew * Pending interrupt register (`GICC_HPPIR`) is read to determine the id of 66*f14d1886SSoby Mathew * the pending interrupt. The type of interrupt depends upon the id value 67*f14d1886SSoby Mathew * as follows. 68*f14d1886SSoby Mathew * 1. id < PENDING_G1_INTID (1022) is reported as a S-EL1 interrupt 69*f14d1886SSoby Mathew * 2. id = PENDING_G1_INTID (1022) is reported as a Non-secure interrupt. 70*f14d1886SSoby Mathew * 3. id = GIC_SPURIOUS_INTERRUPT (1023) is reported as an invalid interrupt 71*f14d1886SSoby Mathew * type. 72*f14d1886SSoby Mathew */ 73*f14d1886SSoby Mathew uint32_t plat_ic_get_pending_interrupt_type(void) 74*f14d1886SSoby Mathew { 75*f14d1886SSoby Mathew unsigned int id; 76*f14d1886SSoby Mathew 77*f14d1886SSoby Mathew id = gicv2_get_pending_interrupt_type(); 78*f14d1886SSoby Mathew 79*f14d1886SSoby Mathew /* Assume that all secure interrupts are S-EL1 interrupts */ 80*f14d1886SSoby Mathew if (id < PENDING_G1_INTID) 81*f14d1886SSoby Mathew return INTR_TYPE_S_EL1; 82*f14d1886SSoby Mathew 83*f14d1886SSoby Mathew if (id == GIC_SPURIOUS_INTERRUPT) 84*f14d1886SSoby Mathew return INTR_TYPE_INVAL; 85*f14d1886SSoby Mathew 86*f14d1886SSoby Mathew return INTR_TYPE_NS; 87*f14d1886SSoby Mathew } 88*f14d1886SSoby Mathew 89*f14d1886SSoby Mathew /* 90*f14d1886SSoby Mathew * This function returns the highest priority pending interrupt at 91*f14d1886SSoby Mathew * the Interrupt controller and indicates to the Interrupt controller 92*f14d1886SSoby Mathew * that the interrupt processing has started. 93*f14d1886SSoby Mathew */ 94*f14d1886SSoby Mathew uint32_t plat_ic_acknowledge_interrupt(void) 95*f14d1886SSoby Mathew { 96*f14d1886SSoby Mathew return gicv2_acknowledge_interrupt(); 97*f14d1886SSoby Mathew } 98*f14d1886SSoby Mathew 99*f14d1886SSoby Mathew /* 100*f14d1886SSoby Mathew * This function returns the type of the interrupt `id`, depending on how 101*f14d1886SSoby Mathew * the interrupt has been configured in the interrupt controller 102*f14d1886SSoby Mathew */ 103*f14d1886SSoby Mathew uint32_t plat_ic_get_interrupt_type(uint32_t id) 104*f14d1886SSoby Mathew { 105*f14d1886SSoby Mathew unsigned int type; 106*f14d1886SSoby Mathew 107*f14d1886SSoby Mathew type = gicv2_get_interrupt_group(id); 108*f14d1886SSoby Mathew 109*f14d1886SSoby Mathew /* Assume that all secure interrupts are S-EL1 interrupts */ 110*f14d1886SSoby Mathew return (type) ? INTR_TYPE_NS : INTR_TYPE_S_EL1; 111*f14d1886SSoby Mathew } 112*f14d1886SSoby Mathew 113*f14d1886SSoby Mathew /* 114*f14d1886SSoby Mathew * This functions is used to indicate to the interrupt controller that 115*f14d1886SSoby Mathew * the processing of the interrupt corresponding to the `id` has 116*f14d1886SSoby Mathew * finished. 117*f14d1886SSoby Mathew */ 118*f14d1886SSoby Mathew void plat_ic_end_of_interrupt(uint32_t id) 119*f14d1886SSoby Mathew { 120*f14d1886SSoby Mathew gicv2_end_of_interrupt(id); 121*f14d1886SSoby Mathew } 122*f14d1886SSoby Mathew 123*f14d1886SSoby Mathew /* 124*f14d1886SSoby Mathew * An ARM processor signals interrupt exceptions through the IRQ and FIQ pins. 125*f14d1886SSoby Mathew * The interrupt controller knows which pin/line it uses to signal a type of 126*f14d1886SSoby Mathew * interrupt. It lets the interrupt management framework determine 127*f14d1886SSoby Mathew * for a type of interrupt and security state, which line should be used in the 128*f14d1886SSoby Mathew * SCR_EL3 to control its routing to EL3. The interrupt line is represented 129*f14d1886SSoby Mathew * as the bit position of the IRQ or FIQ bit in the SCR_EL3. 130*f14d1886SSoby Mathew */ 131*f14d1886SSoby Mathew uint32_t plat_interrupt_type_to_line(uint32_t type, 132*f14d1886SSoby Mathew uint32_t security_state) 133*f14d1886SSoby Mathew { 134*f14d1886SSoby Mathew assert(type == INTR_TYPE_S_EL1 || 135*f14d1886SSoby Mathew type == INTR_TYPE_EL3 || 136*f14d1886SSoby Mathew type == INTR_TYPE_NS); 137*f14d1886SSoby Mathew 138*f14d1886SSoby Mathew /* Non-secure interrupts are signaled on the IRQ line always */ 139*f14d1886SSoby Mathew if (type == INTR_TYPE_NS) 140*f14d1886SSoby Mathew return __builtin_ctz(SCR_IRQ_BIT); 141*f14d1886SSoby Mathew 142*f14d1886SSoby Mathew /* 143*f14d1886SSoby Mathew * Secure interrupts are signaled using the IRQ line if the FIQ is 144*f14d1886SSoby Mathew * not enabled else they are signaled using the FIQ line. 145*f14d1886SSoby Mathew */ 146*f14d1886SSoby Mathew return ((gicv2_is_fiq_enabled()) ? __builtin_ctz(SCR_FIQ_BIT) : 147*f14d1886SSoby Mathew __builtin_ctz(SCR_IRQ_BIT)); 148*f14d1886SSoby Mathew } 149