1d2db1de6SPeter Maydell /* 2d2db1de6SPeter Maydell * ARMv7M NVIC object 3d2db1de6SPeter Maydell * 4d2db1de6SPeter Maydell * Copyright (c) 2017 Linaro Ltd 5d2db1de6SPeter Maydell * Written by Peter Maydell <peter.maydell@linaro.org> 6d2db1de6SPeter Maydell * 7d2db1de6SPeter Maydell * This code is licensed under the GPL version 2 or later. 8d2db1de6SPeter Maydell */ 9d2db1de6SPeter Maydell 10d2db1de6SPeter Maydell #ifndef HW_ARM_ARMV7M_NVIC_H 11d2db1de6SPeter Maydell #define HW_ARM_ARMV7M_NVIC_H 12d2db1de6SPeter Maydell 13d2db1de6SPeter Maydell #include "target/arm/cpu.h" 14d2db1de6SPeter Maydell #include "hw/sysbus.h" 15d2db1de6SPeter Maydell #include "hw/timer/armv7m_systick.h" 16db1015e9SEduardo Habkost #include "qom/object.h" 17d2db1de6SPeter Maydell 18d2db1de6SPeter Maydell #define TYPE_NVIC "armv7m_nvic" 199b772b19SPhilippe Mathieu-Daudé OBJECT_DECLARE_SIMPLE_TYPE(NVICState, NVIC) 20d2db1de6SPeter Maydell 21d2db1de6SPeter Maydell /* Highest permitted number of exceptions (architectural limit) */ 22d2db1de6SPeter Maydell #define NVIC_MAX_VECTORS 512 2317906a16SPeter Maydell /* Number of internal exceptions */ 2417906a16SPeter Maydell #define NVIC_INTERNAL_VECTORS 16 25d2db1de6SPeter Maydell 26d2db1de6SPeter Maydell typedef struct VecInfo { 27d2db1de6SPeter Maydell /* Exception priorities can range from -3 to 255; only the unmodifiable 28d2db1de6SPeter Maydell * priority values for RESET, NMI and HardFault can be negative. 29d2db1de6SPeter Maydell */ 30d2db1de6SPeter Maydell int16_t prio; 31d2db1de6SPeter Maydell uint8_t enabled; 32d2db1de6SPeter Maydell uint8_t pending; 33d2db1de6SPeter Maydell uint8_t active; 34d2db1de6SPeter Maydell uint8_t level; /* exceptions <=15 never set level */ 35d2db1de6SPeter Maydell } VecInfo; 36d2db1de6SPeter Maydell 37db1015e9SEduardo Habkost struct NVICState { 38d2db1de6SPeter Maydell /*< private >*/ 39d2db1de6SPeter Maydell SysBusDevice parent_obj; 40d2db1de6SPeter Maydell /*< public >*/ 41d2db1de6SPeter Maydell 42d2db1de6SPeter Maydell ARMCPU *cpu; 43d2db1de6SPeter Maydell 44d2db1de6SPeter Maydell VecInfo vectors[NVIC_MAX_VECTORS]; 4517906a16SPeter Maydell /* If the v8M security extension is implemented, some of the internal 4617906a16SPeter Maydell * exceptions are banked between security states (ie there exists both 4717906a16SPeter Maydell * a Secure and a NonSecure version of the exception and its state): 4817906a16SPeter Maydell * HardFault, MemManage, UsageFault, SVCall, PendSV, SysTick (R_PJHV) 4917906a16SPeter Maydell * The rest (including all the external exceptions) are not banked, though 5017906a16SPeter Maydell * they may be configurable to target either Secure or NonSecure state. 5117906a16SPeter Maydell * We store the secure exception state in sec_vectors[] for the banked 5217906a16SPeter Maydell * exceptions, and otherwise use only vectors[] (including for exceptions 5317906a16SPeter Maydell * like SecureFault that unconditionally target Secure state). 5417906a16SPeter Maydell * Entries in sec_vectors[] for non-banked exception numbers are unused. 5517906a16SPeter Maydell */ 5617906a16SPeter Maydell VecInfo sec_vectors[NVIC_INTERNAL_VECTORS]; 573b2e9344SPeter Maydell /* The PRIGROUP field in AIRCR is banked */ 583b2e9344SPeter Maydell uint32_t prigroup[M_REG_NUM_BANKS]; 59c4379b48SJulia Suvorova uint8_t num_prio_bits; 60d2db1de6SPeter Maydell 61e1be0a57SPeter Maydell /* v8M NVIC_ITNS state (stored as a bool per bit) */ 62e1be0a57SPeter Maydell bool itns[NVIC_MAX_VECTORS]; 63e1be0a57SPeter Maydell 64e93bc2acSPeter Maydell /* The following fields are all cached state that can be recalculated 65e93bc2acSPeter Maydell * from the vectors[] and sec_vectors[] arrays and the prigroup field: 66e93bc2acSPeter Maydell * - vectpending 67e93bc2acSPeter Maydell * - vectpending_is_secure 68e93bc2acSPeter Maydell * - exception_prio 695255fcf8SPeter Maydell * - vectpending_prio 70d2db1de6SPeter Maydell */ 71d2db1de6SPeter Maydell unsigned int vectpending; /* highest prio pending enabled exception */ 72e93bc2acSPeter Maydell /* true if vectpending is a banked secure exception, ie it is in 73e93bc2acSPeter Maydell * sec_vectors[] rather than vectors[] 74e93bc2acSPeter Maydell */ 75e93bc2acSPeter Maydell bool vectpending_is_s_banked; 76d2db1de6SPeter Maydell int exception_prio; /* group prio of the highest prio active exception */ 775255fcf8SPeter Maydell int vectpending_prio; /* group prio of the exeception in vectpending */ 78d2db1de6SPeter Maydell 79d2db1de6SPeter Maydell MemoryRegion sysregmem; 80d2db1de6SPeter Maydell 81d2db1de6SPeter Maydell uint32_t num_irq; 82d2db1de6SPeter Maydell qemu_irq excpout; 83d2db1de6SPeter Maydell qemu_irq sysresetreq; 84db1015e9SEduardo Habkost }; 85d2db1de6SPeter Maydell 86*165876f2SPhilippe Mathieu-Daudé /* Interface between CPU and Interrupt controller. */ 87*165876f2SPhilippe Mathieu-Daudé /** 88*165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_set_pending: mark the specified exception as pending 89*165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 90*165876f2SPhilippe Mathieu-Daudé * @irq: the exception number to mark pending 91*165876f2SPhilippe Mathieu-Daudé * @secure: false for non-banked exceptions or for the nonsecure 92*165876f2SPhilippe Mathieu-Daudé * version of a banked exception, true for the secure version of a banked 93*165876f2SPhilippe Mathieu-Daudé * exception. 94*165876f2SPhilippe Mathieu-Daudé * 95*165876f2SPhilippe Mathieu-Daudé * Marks the specified exception as pending. Note that we will assert() 96*165876f2SPhilippe Mathieu-Daudé * if @secure is true and @irq does not specify one of the fixed set 97*165876f2SPhilippe Mathieu-Daudé * of architecturally banked exceptions. 98*165876f2SPhilippe Mathieu-Daudé */ 99*165876f2SPhilippe Mathieu-Daudé void armv7m_nvic_set_pending(NVICState *s, int irq, bool secure); 100*165876f2SPhilippe Mathieu-Daudé /** 101*165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_set_pending_derived: mark this derived exception as pending 102*165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 103*165876f2SPhilippe Mathieu-Daudé * @irq: the exception number to mark pending 104*165876f2SPhilippe Mathieu-Daudé * @secure: false for non-banked exceptions or for the nonsecure 105*165876f2SPhilippe Mathieu-Daudé * version of a banked exception, true for the secure version of a banked 106*165876f2SPhilippe Mathieu-Daudé * exception. 107*165876f2SPhilippe Mathieu-Daudé * 108*165876f2SPhilippe Mathieu-Daudé * Similar to armv7m_nvic_set_pending(), but specifically for derived 109*165876f2SPhilippe Mathieu-Daudé * exceptions (exceptions generated in the course of trying to take 110*165876f2SPhilippe Mathieu-Daudé * a different exception). 111*165876f2SPhilippe Mathieu-Daudé */ 112*165876f2SPhilippe Mathieu-Daudé void armv7m_nvic_set_pending_derived(NVICState *s, int irq, bool secure); 113*165876f2SPhilippe Mathieu-Daudé /** 114*165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_set_pending_lazyfp: mark this lazy FP exception as pending 115*165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 116*165876f2SPhilippe Mathieu-Daudé * @irq: the exception number to mark pending 117*165876f2SPhilippe Mathieu-Daudé * @secure: false for non-banked exceptions or for the nonsecure 118*165876f2SPhilippe Mathieu-Daudé * version of a banked exception, true for the secure version of a banked 119*165876f2SPhilippe Mathieu-Daudé * exception. 120*165876f2SPhilippe Mathieu-Daudé * 121*165876f2SPhilippe Mathieu-Daudé * Similar to armv7m_nvic_set_pending(), but specifically for exceptions 122*165876f2SPhilippe Mathieu-Daudé * generated in the course of lazy stacking of FP registers. 123*165876f2SPhilippe Mathieu-Daudé */ 124*165876f2SPhilippe Mathieu-Daudé void armv7m_nvic_set_pending_lazyfp(NVICState *s, int irq, bool secure); 125*165876f2SPhilippe Mathieu-Daudé /** 126*165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_get_pending_irq_info: return highest priority pending 127*165876f2SPhilippe Mathieu-Daudé * exception, and whether it targets Secure state 128*165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 129*165876f2SPhilippe Mathieu-Daudé * @pirq: set to pending exception number 130*165876f2SPhilippe Mathieu-Daudé * @ptargets_secure: set to whether pending exception targets Secure 131*165876f2SPhilippe Mathieu-Daudé * 132*165876f2SPhilippe Mathieu-Daudé * This function writes the number of the highest priority pending 133*165876f2SPhilippe Mathieu-Daudé * exception (the one which would be made active by 134*165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_acknowledge_irq()) to @pirq, and sets @ptargets_secure 135*165876f2SPhilippe Mathieu-Daudé * to true if the current highest priority pending exception should 136*165876f2SPhilippe Mathieu-Daudé * be taken to Secure state, false for NS. 137*165876f2SPhilippe Mathieu-Daudé */ 138*165876f2SPhilippe Mathieu-Daudé void armv7m_nvic_get_pending_irq_info(NVICState *s, int *pirq, 139*165876f2SPhilippe Mathieu-Daudé bool *ptargets_secure); 140*165876f2SPhilippe Mathieu-Daudé /** 141*165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_acknowledge_irq: make highest priority pending exception active 142*165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 143*165876f2SPhilippe Mathieu-Daudé * 144*165876f2SPhilippe Mathieu-Daudé * Move the current highest priority pending exception from the pending 145*165876f2SPhilippe Mathieu-Daudé * state to the active state, and update v7m.exception to indicate that 146*165876f2SPhilippe Mathieu-Daudé * it is the exception currently being handled. 147*165876f2SPhilippe Mathieu-Daudé */ 148*165876f2SPhilippe Mathieu-Daudé void armv7m_nvic_acknowledge_irq(NVICState *s); 149*165876f2SPhilippe Mathieu-Daudé /** 150*165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_complete_irq: complete specified interrupt or exception 151*165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 152*165876f2SPhilippe Mathieu-Daudé * @irq: the exception number to complete 153*165876f2SPhilippe Mathieu-Daudé * @secure: true if this exception was secure 154*165876f2SPhilippe Mathieu-Daudé * 155*165876f2SPhilippe Mathieu-Daudé * Returns: -1 if the irq was not active 156*165876f2SPhilippe Mathieu-Daudé * 1 if completing this irq brought us back to base (no active irqs) 157*165876f2SPhilippe Mathieu-Daudé * 0 if there is still an irq active after this one was completed 158*165876f2SPhilippe Mathieu-Daudé * (Ignoring -1, this is the same as the RETTOBASE value before completion.) 159*165876f2SPhilippe Mathieu-Daudé */ 160*165876f2SPhilippe Mathieu-Daudé int armv7m_nvic_complete_irq(NVICState *s, int irq, bool secure); 161*165876f2SPhilippe Mathieu-Daudé /** 162*165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_get_ready_status(void *opaque, int irq, bool secure) 163*165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 164*165876f2SPhilippe Mathieu-Daudé * @irq: the exception number to mark pending 165*165876f2SPhilippe Mathieu-Daudé * @secure: false for non-banked exceptions or for the nonsecure 166*165876f2SPhilippe Mathieu-Daudé * version of a banked exception, true for the secure version of a banked 167*165876f2SPhilippe Mathieu-Daudé * exception. 168*165876f2SPhilippe Mathieu-Daudé * 169*165876f2SPhilippe Mathieu-Daudé * Return whether an exception is "ready", i.e. whether the exception is 170*165876f2SPhilippe Mathieu-Daudé * enabled and is configured at a priority which would allow it to 171*165876f2SPhilippe Mathieu-Daudé * interrupt the current execution priority. This controls whether the 172*165876f2SPhilippe Mathieu-Daudé * RDY bit for it in the FPCCR is set. 173*165876f2SPhilippe Mathieu-Daudé */ 174*165876f2SPhilippe Mathieu-Daudé bool armv7m_nvic_get_ready_status(NVICState *s, int irq, bool secure); 175*165876f2SPhilippe Mathieu-Daudé /** 176*165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_raw_execution_priority: return the raw execution priority 177*165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 178*165876f2SPhilippe Mathieu-Daudé * 179*165876f2SPhilippe Mathieu-Daudé * Returns: the raw execution priority as defined by the v8M architecture. 180*165876f2SPhilippe Mathieu-Daudé * This is the execution priority minus the effects of AIRCR.PRIS, 181*165876f2SPhilippe Mathieu-Daudé * and minus any PRIMASK/FAULTMASK/BASEPRI priority boosting. 182*165876f2SPhilippe Mathieu-Daudé * (v8M ARM ARM I_PKLD.) 183*165876f2SPhilippe Mathieu-Daudé */ 184*165876f2SPhilippe Mathieu-Daudé int armv7m_nvic_raw_execution_priority(NVICState *s); 185*165876f2SPhilippe Mathieu-Daudé /** 186*165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_neg_prio_requested: return true if the requested execution 187*165876f2SPhilippe Mathieu-Daudé * priority is negative for the specified security state. 188*165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 189*165876f2SPhilippe Mathieu-Daudé * @secure: the security state to test 190*165876f2SPhilippe Mathieu-Daudé * This corresponds to the pseudocode IsReqExecPriNeg(). 191*165876f2SPhilippe Mathieu-Daudé */ 192*165876f2SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY 193*165876f2SPhilippe Mathieu-Daudé bool armv7m_nvic_neg_prio_requested(NVICState *s, bool secure); 194*165876f2SPhilippe Mathieu-Daudé #else 195*165876f2SPhilippe Mathieu-Daudé static inline bool armv7m_nvic_neg_prio_requested(NVICState *s, bool secure) 196*165876f2SPhilippe Mathieu-Daudé { 197*165876f2SPhilippe Mathieu-Daudé return false; 198*165876f2SPhilippe Mathieu-Daudé } 199*165876f2SPhilippe Mathieu-Daudé #endif 200*165876f2SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY 201*165876f2SPhilippe Mathieu-Daudé bool armv7m_nvic_can_take_pending_exception(NVICState *s); 202*165876f2SPhilippe Mathieu-Daudé #else 203*165876f2SPhilippe Mathieu-Daudé static inline bool armv7m_nvic_can_take_pending_exception(NVICState *s) 204*165876f2SPhilippe Mathieu-Daudé { 205*165876f2SPhilippe Mathieu-Daudé return true; 206*165876f2SPhilippe Mathieu-Daudé } 207*165876f2SPhilippe Mathieu-Daudé #endif 208*165876f2SPhilippe Mathieu-Daudé 209d2db1de6SPeter Maydell #endif 210