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 */ 77*673d8215SMichael Tokarev int vectpending_prio; /* group prio of the exception 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 86165876f2SPhilippe Mathieu-Daudé /* Interface between CPU and Interrupt controller. */ 87165876f2SPhilippe Mathieu-Daudé /** 88165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_set_pending: mark the specified exception as pending 89165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 90165876f2SPhilippe Mathieu-Daudé * @irq: the exception number to mark pending 91165876f2SPhilippe Mathieu-Daudé * @secure: false for non-banked exceptions or for the nonsecure 92165876f2SPhilippe Mathieu-Daudé * version of a banked exception, true for the secure version of a banked 93165876f2SPhilippe Mathieu-Daudé * exception. 94165876f2SPhilippe Mathieu-Daudé * 95165876f2SPhilippe Mathieu-Daudé * Marks the specified exception as pending. Note that we will assert() 96165876f2SPhilippe Mathieu-Daudé * if @secure is true and @irq does not specify one of the fixed set 97165876f2SPhilippe Mathieu-Daudé * of architecturally banked exceptions. 98165876f2SPhilippe Mathieu-Daudé */ 99165876f2SPhilippe Mathieu-Daudé void armv7m_nvic_set_pending(NVICState *s, int irq, bool secure); 100165876f2SPhilippe Mathieu-Daudé /** 101165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_set_pending_derived: mark this derived exception as pending 102165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 103165876f2SPhilippe Mathieu-Daudé * @irq: the exception number to mark pending 104165876f2SPhilippe Mathieu-Daudé * @secure: false for non-banked exceptions or for the nonsecure 105165876f2SPhilippe Mathieu-Daudé * version of a banked exception, true for the secure version of a banked 106165876f2SPhilippe Mathieu-Daudé * exception. 107165876f2SPhilippe Mathieu-Daudé * 108165876f2SPhilippe Mathieu-Daudé * Similar to armv7m_nvic_set_pending(), but specifically for derived 109165876f2SPhilippe Mathieu-Daudé * exceptions (exceptions generated in the course of trying to take 110165876f2SPhilippe Mathieu-Daudé * a different exception). 111165876f2SPhilippe Mathieu-Daudé */ 112165876f2SPhilippe Mathieu-Daudé void armv7m_nvic_set_pending_derived(NVICState *s, int irq, bool secure); 113165876f2SPhilippe Mathieu-Daudé /** 114165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_set_pending_lazyfp: mark this lazy FP exception as pending 115165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 116165876f2SPhilippe Mathieu-Daudé * @irq: the exception number to mark pending 117165876f2SPhilippe Mathieu-Daudé * @secure: false for non-banked exceptions or for the nonsecure 118165876f2SPhilippe Mathieu-Daudé * version of a banked exception, true for the secure version of a banked 119165876f2SPhilippe Mathieu-Daudé * exception. 120165876f2SPhilippe Mathieu-Daudé * 121165876f2SPhilippe Mathieu-Daudé * Similar to armv7m_nvic_set_pending(), but specifically for exceptions 122165876f2SPhilippe Mathieu-Daudé * generated in the course of lazy stacking of FP registers. 123165876f2SPhilippe Mathieu-Daudé */ 124165876f2SPhilippe Mathieu-Daudé void armv7m_nvic_set_pending_lazyfp(NVICState *s, int irq, bool secure); 125165876f2SPhilippe Mathieu-Daudé /** 126165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_get_pending_irq_info: return highest priority pending 127165876f2SPhilippe Mathieu-Daudé * exception, and whether it targets Secure state 128165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 129165876f2SPhilippe Mathieu-Daudé * @pirq: set to pending exception number 130165876f2SPhilippe Mathieu-Daudé * @ptargets_secure: set to whether pending exception targets Secure 131165876f2SPhilippe Mathieu-Daudé * 132165876f2SPhilippe Mathieu-Daudé * This function writes the number of the highest priority pending 133165876f2SPhilippe Mathieu-Daudé * exception (the one which would be made active by 134165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_acknowledge_irq()) to @pirq, and sets @ptargets_secure 135165876f2SPhilippe Mathieu-Daudé * to true if the current highest priority pending exception should 136165876f2SPhilippe Mathieu-Daudé * be taken to Secure state, false for NS. 137165876f2SPhilippe Mathieu-Daudé */ 138165876f2SPhilippe Mathieu-Daudé void armv7m_nvic_get_pending_irq_info(NVICState *s, int *pirq, 139165876f2SPhilippe Mathieu-Daudé bool *ptargets_secure); 140165876f2SPhilippe Mathieu-Daudé /** 141165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_acknowledge_irq: make highest priority pending exception active 142165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 143165876f2SPhilippe Mathieu-Daudé * 144165876f2SPhilippe Mathieu-Daudé * Move the current highest priority pending exception from the pending 145165876f2SPhilippe Mathieu-Daudé * state to the active state, and update v7m.exception to indicate that 146165876f2SPhilippe Mathieu-Daudé * it is the exception currently being handled. 147165876f2SPhilippe Mathieu-Daudé */ 148165876f2SPhilippe Mathieu-Daudé void armv7m_nvic_acknowledge_irq(NVICState *s); 149165876f2SPhilippe Mathieu-Daudé /** 150165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_complete_irq: complete specified interrupt or exception 151165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 152165876f2SPhilippe Mathieu-Daudé * @irq: the exception number to complete 153165876f2SPhilippe Mathieu-Daudé * @secure: true if this exception was secure 154165876f2SPhilippe Mathieu-Daudé * 155165876f2SPhilippe Mathieu-Daudé * Returns: -1 if the irq was not active 156165876f2SPhilippe Mathieu-Daudé * 1 if completing this irq brought us back to base (no active irqs) 157165876f2SPhilippe Mathieu-Daudé * 0 if there is still an irq active after this one was completed 158165876f2SPhilippe Mathieu-Daudé * (Ignoring -1, this is the same as the RETTOBASE value before completion.) 159165876f2SPhilippe Mathieu-Daudé */ 160165876f2SPhilippe Mathieu-Daudé int armv7m_nvic_complete_irq(NVICState *s, int irq, bool secure); 161165876f2SPhilippe Mathieu-Daudé /** 162165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_get_ready_status(void *opaque, int irq, bool secure) 163165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 164165876f2SPhilippe Mathieu-Daudé * @irq: the exception number to mark pending 165165876f2SPhilippe Mathieu-Daudé * @secure: false for non-banked exceptions or for the nonsecure 166165876f2SPhilippe Mathieu-Daudé * version of a banked exception, true for the secure version of a banked 167165876f2SPhilippe Mathieu-Daudé * exception. 168165876f2SPhilippe Mathieu-Daudé * 169165876f2SPhilippe Mathieu-Daudé * Return whether an exception is "ready", i.e. whether the exception is 170165876f2SPhilippe Mathieu-Daudé * enabled and is configured at a priority which would allow it to 171165876f2SPhilippe Mathieu-Daudé * interrupt the current execution priority. This controls whether the 172165876f2SPhilippe Mathieu-Daudé * RDY bit for it in the FPCCR is set. 173165876f2SPhilippe Mathieu-Daudé */ 174165876f2SPhilippe Mathieu-Daudé bool armv7m_nvic_get_ready_status(NVICState *s, int irq, bool secure); 175165876f2SPhilippe Mathieu-Daudé /** 176165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_raw_execution_priority: return the raw execution priority 177165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 178165876f2SPhilippe Mathieu-Daudé * 179165876f2SPhilippe Mathieu-Daudé * Returns: the raw execution priority as defined by the v8M architecture. 180165876f2SPhilippe Mathieu-Daudé * This is the execution priority minus the effects of AIRCR.PRIS, 181165876f2SPhilippe Mathieu-Daudé * and minus any PRIMASK/FAULTMASK/BASEPRI priority boosting. 182165876f2SPhilippe Mathieu-Daudé * (v8M ARM ARM I_PKLD.) 183165876f2SPhilippe Mathieu-Daudé */ 184165876f2SPhilippe Mathieu-Daudé int armv7m_nvic_raw_execution_priority(NVICState *s); 185165876f2SPhilippe Mathieu-Daudé /** 186165876f2SPhilippe Mathieu-Daudé * armv7m_nvic_neg_prio_requested: return true if the requested execution 187165876f2SPhilippe Mathieu-Daudé * priority is negative for the specified security state. 188165876f2SPhilippe Mathieu-Daudé * @s: the NVIC 189165876f2SPhilippe Mathieu-Daudé * @secure: the security state to test 190165876f2SPhilippe Mathieu-Daudé * This corresponds to the pseudocode IsReqExecPriNeg(). 191165876f2SPhilippe Mathieu-Daudé */ 192165876f2SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY 193165876f2SPhilippe Mathieu-Daudé bool armv7m_nvic_neg_prio_requested(NVICState *s, bool secure); 194165876f2SPhilippe Mathieu-Daudé #else 195165876f2SPhilippe Mathieu-Daudé static inline bool armv7m_nvic_neg_prio_requested(NVICState *s, bool secure) 196165876f2SPhilippe Mathieu-Daudé { 197165876f2SPhilippe Mathieu-Daudé return false; 198165876f2SPhilippe Mathieu-Daudé } 199165876f2SPhilippe Mathieu-Daudé #endif 200165876f2SPhilippe Mathieu-Daudé #ifndef CONFIG_USER_ONLY 201165876f2SPhilippe Mathieu-Daudé bool armv7m_nvic_can_take_pending_exception(NVICState *s); 202165876f2SPhilippe Mathieu-Daudé #else 203165876f2SPhilippe Mathieu-Daudé static inline bool armv7m_nvic_can_take_pending_exception(NVICState *s) 204165876f2SPhilippe Mathieu-Daudé { 205165876f2SPhilippe Mathieu-Daudé return true; 206165876f2SPhilippe Mathieu-Daudé } 207165876f2SPhilippe Mathieu-Daudé #endif 208165876f2SPhilippe Mathieu-Daudé 209d2db1de6SPeter Maydell #endif 210