2 * ARM Nested Vectored Interrupt Controller
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
9 * The ARMv7M System controller is fairly tightly tied in with the
10 * NVIC. Much of that is also implemented here.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu-common.h"
17 #include "hw/sysbus.h"
18 #include "qemu/timer.h"
19 #include "hw/arm/arm.h"
20 #include "hw/arm/armv7m_nvic.h"
21 #include "target/arm/cpu.h"
25 /* IRQ number counting:
27 * the num-irq property counts the number of external IRQ lines
29 * NVICState::num_irq counts the total number of exceptions
30 * (external IRQs, the 15 internal exceptions including reset,
31 * and one for the unused exception number 0).
33 * NVIC_MAX_IRQ is the highest permitted number of external IRQ lines.
35 * NVIC_MAX_VECTORS is the highest permitted number of exceptions.
37 * Iterating through all exceptions should typically be done with
38 * for (i = 1; i < s->num_irq; i++) to avoid the unused slot 0.
40 * The external qemu_irq lines are the NVIC's external IRQ lines,
41 * so line 0 is exception 16.
43 * In the terminology of the architecture manual, "interrupts" are
44 * a subcategory of exception referring to the external interrupts
45 * (which are exception numbers NVIC_FIRST_IRQ and upward).
46 * For historical reasons QEMU tends to use "interrupt" and
47 * "exception" more or less interchangeably.
49 #define NVIC_FIRST_IRQ 16
50 #define NVIC_MAX_IRQ (NVIC_MAX_VECTORS - NVIC_FIRST_IRQ)
52 /* Effective running priority of the CPU when no exception is active
53 * (higher than the highest possible priority value)
55 #define NVIC_NOEXC_PRIO 0x100
57 static const uint8_t nvic_id
[] = {
58 0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1
61 static int nvic_pending_prio(NVICState
*s
)
63 /* return the priority of the current pending interrupt,
64 * or NVIC_NOEXC_PRIO if no interrupt is pending
66 return s
->vectpending
? s
->vectors
[s
->vectpending
].prio
: NVIC_NOEXC_PRIO
;
69 /* Return the value of the ISCR RETTOBASE bit:
70 * 1 if there is exactly one active exception
71 * 0 if there is more than one active exception
72 * UNKNOWN if there are no active exceptions (we choose 1,
73 * which matches the choice Cortex-M3 is documented as making).
75 * NB: some versions of the documentation talk about this
76 * counting "active exceptions other than the one shown by IPSR";
77 * this is only different in the obscure corner case where guest
78 * code has manually deactivated an exception and is about
79 * to fail an exception-return integrity check. The definition
80 * above is the one from the v8M ARM ARM and is also in line
81 * with the behaviour documented for the Cortex-M3.
83 static bool nvic_rettobase(NVICState
*s
)
87 for (irq
= ARMV7M_EXCP_RESET
; irq
< s
->num_irq
; irq
++) {
88 if (s
->vectors
[irq
].active
) {
99 /* Return the value of the ISCR ISRPENDING bit:
100 * 1 if an external interrupt is pending
101 * 0 if no external interrupt is pending
103 static bool nvic_isrpending(NVICState
*s
)
107 /* We can shortcut if the highest priority pending interrupt
108 * happens to be external or if there is nothing pending.
110 if (s
->vectpending
> NVIC_FIRST_IRQ
) {
113 if (s
->vectpending
== 0) {
117 for (irq
= NVIC_FIRST_IRQ
; irq
< s
->num_irq
; irq
++) {
118 if (s
->vectors
[irq
].pending
) {
125 /* Return a mask word which clears the subpriority bits from
126 * a priority value for an M-profile exception, leaving only
127 * the group priority.
129 static inline uint32_t nvic_gprio_mask(NVICState
*s
)
131 return ~0U << (s
->prigroup
+ 1);
134 /* Recompute vectpending and exception_prio */
135 static void nvic_recompute_state(NVICState
*s
)
138 int pend_prio
= NVIC_NOEXC_PRIO
;
139 int active_prio
= NVIC_NOEXC_PRIO
;
142 for (i
= 1; i
< s
->num_irq
; i
++) {
143 VecInfo
*vec
= &s
->vectors
[i
];
145 if (vec
->enabled
&& vec
->pending
&& vec
->prio
< pend_prio
) {
146 pend_prio
= vec
->prio
;
149 if (vec
->active
&& vec
->prio
< active_prio
) {
150 active_prio
= vec
->prio
;
154 s
->vectpending
= pend_irq
;
155 s
->exception_prio
= active_prio
& nvic_gprio_mask(s
);
157 trace_nvic_recompute_state(s
->vectpending
, s
->exception_prio
);
160 /* Return the current execution priority of the CPU
161 * (equivalent to the pseudocode ExecutionPriority function).
162 * This is a value between -2 (NMI priority) and NVIC_NOEXC_PRIO.
164 static inline int nvic_exec_prio(NVICState
*s
)
166 CPUARMState
*env
= &s
->cpu
->env
;
169 if (env
->daif
& PSTATE_F
) { /* FAULTMASK */
171 } else if (env
->daif
& PSTATE_I
) { /* PRIMASK */
173 } else if (env
->v7m
.basepri
> 0) {
174 running
= env
->v7m
.basepri
& nvic_gprio_mask(s
);
176 running
= NVIC_NOEXC_PRIO
; /* lower than any possible priority */
178 /* consider priority of active handler */
179 return MIN(running
, s
->exception_prio
);
182 bool armv7m_nvic_can_take_pending_exception(void *opaque
)
184 NVICState
*s
= opaque
;
186 return nvic_exec_prio(s
) > nvic_pending_prio(s
);
189 /* caller must call nvic_irq_update() after this */
190 static void set_prio(NVICState
*s
, unsigned irq
, uint8_t prio
)
192 assert(irq
> ARMV7M_EXCP_NMI
); /* only use for configurable prios */
193 assert(irq
< s
->num_irq
);
195 s
->vectors
[irq
].prio
= prio
;
197 trace_nvic_set_prio(irq
, prio
);
200 /* Recompute state and assert irq line accordingly.
201 * Must be called after changes to:
202 * vec->active, vec->enabled, vec->pending or vec->prio for any vector
205 static void nvic_irq_update(NVICState
*s
)
210 nvic_recompute_state(s
);
211 pend_prio
= nvic_pending_prio(s
);
213 /* Raise NVIC output if this IRQ would be taken, except that we
214 * ignore the effects of the BASEPRI, FAULTMASK and PRIMASK (which
215 * will be checked for in arm_v7m_cpu_exec_interrupt()); changes
216 * to those CPU registers don't cause us to recalculate the NVIC
219 lvl
= (pend_prio
< s
->exception_prio
);
220 trace_nvic_irq_update(s
->vectpending
, pend_prio
, s
->exception_prio
, lvl
);
221 qemu_set_irq(s
->excpout
, lvl
);
224 static void armv7m_nvic_clear_pending(void *opaque
, int irq
)
226 NVICState
*s
= (NVICState
*)opaque
;
229 assert(irq
> ARMV7M_EXCP_RESET
&& irq
< s
->num_irq
);
231 vec
= &s
->vectors
[irq
];
232 trace_nvic_clear_pending(irq
, vec
->enabled
, vec
->prio
);
239 void armv7m_nvic_set_pending(void *opaque
, int irq
)
241 NVICState
*s
= (NVICState
*)opaque
;
244 assert(irq
> ARMV7M_EXCP_RESET
&& irq
< s
->num_irq
);
246 vec
= &s
->vectors
[irq
];
247 trace_nvic_set_pending(irq
, vec
->enabled
, vec
->prio
);
250 if (irq
>= ARMV7M_EXCP_HARD
&& irq
< ARMV7M_EXCP_PENDSV
) {
251 /* If a synchronous exception is pending then it may be
252 * escalated to HardFault if:
253 * * it is equal or lower priority to current execution
255 * (ie we need to take it immediately but we can't do so).
256 * Asynchronous exceptions (and interrupts) simply remain pending.
258 * For QEMU, we don't have any imprecise (asynchronous) faults,
259 * so we can assume that PREFETCH_ABORT and DATA_ABORT are always
261 * Debug exceptions are awkward because only Debug exceptions
262 * resulting from the BKPT instruction should be escalated,
263 * but we don't currently implement any Debug exceptions other
264 * than those that result from BKPT, so we treat all debug exceptions
265 * as needing escalation.
267 * This all means we can identify whether to escalate based only on
268 * the exception number and don't (yet) need the caller to explicitly
269 * tell us whether this exception is synchronous or not.
271 int running
= nvic_exec_prio(s
);
272 bool escalate
= false;
274 if (vec
->prio
>= running
) {
275 trace_nvic_escalate_prio(irq
, vec
->prio
, running
);
277 } else if (!vec
->enabled
) {
278 trace_nvic_escalate_disabled(irq
);
284 /* We want to escalate to HardFault but we can't take a
285 * synchronous HardFault at this point either. This is a
286 * Lockup condition due to a guest bug. We don't model
287 * Lockup, so report via cpu_abort() instead.
289 cpu_abort(&s
->cpu
->parent_obj
,
290 "Lockup: can't escalate %d to HardFault "
291 "(current priority %d)\n", irq
, running
);
294 /* We can do the escalation, so we take HardFault instead */
295 irq
= ARMV7M_EXCP_HARD
;
296 vec
= &s
->vectors
[irq
];
297 s
->cpu
->env
.v7m
.hfsr
|= R_V7M_HFSR_FORCED_MASK
;
307 /* Make pending IRQ active. */
308 void armv7m_nvic_acknowledge_irq(void *opaque
)
310 NVICState
*s
= (NVICState
*)opaque
;
311 CPUARMState
*env
= &s
->cpu
->env
;
312 const int pending
= s
->vectpending
;
313 const int running
= nvic_exec_prio(s
);
317 assert(pending
> ARMV7M_EXCP_RESET
&& pending
< s
->num_irq
);
319 vec
= &s
->vectors
[pending
];
321 assert(vec
->enabled
);
322 assert(vec
->pending
);
324 pendgroupprio
= vec
->prio
& nvic_gprio_mask(s
);
325 assert(pendgroupprio
< running
);
327 trace_nvic_acknowledge_irq(pending
, vec
->prio
);
332 env
->v7m
.exception
= s
->vectpending
;
337 int armv7m_nvic_complete_irq(void *opaque
, int irq
)
339 NVICState
*s
= (NVICState
*)opaque
;
343 assert(irq
> ARMV7M_EXCP_RESET
&& irq
< s
->num_irq
);
345 vec
= &s
->vectors
[irq
];
347 trace_nvic_complete_irq(irq
);
350 /* Tell the caller this was an illegal exception return */
354 ret
= nvic_rettobase(s
);
358 /* Re-pend the exception if it's still held high; only
359 * happens for extenal IRQs
361 assert(irq
>= NVIC_FIRST_IRQ
);
370 /* callback when external interrupt line is changed */
371 static void set_irq_level(void *opaque
, int n
, int level
)
373 NVICState
*s
= opaque
;
378 assert(n
>= NVIC_FIRST_IRQ
&& n
< s
->num_irq
);
380 trace_nvic_set_irq_level(n
, level
);
382 /* The pending status of an external interrupt is
383 * latched on rising edge and exception handler return.
385 * Pulsing the IRQ will always run the handler
386 * once, and the handler will re-run until the
387 * level is low when the handler completes.
389 vec
= &s
->vectors
[n
];
390 if (level
!= vec
->level
) {
393 armv7m_nvic_set_pending(s
, n
);
398 static uint32_t nvic_readl(NVICState
*s
, uint32_t offset
)
400 ARMCPU
*cpu
= s
->cpu
;
404 case 4: /* Interrupt Control Type. */
405 return ((s
->num_irq
- NVIC_FIRST_IRQ
) / 32) - 1;
406 case 0xd00: /* CPUID Base. */
408 case 0xd04: /* Interrupt Control State. */
410 val
= cpu
->env
.v7m
.exception
;
412 val
|= (s
->vectpending
& 0xff) << 12;
413 /* ISRPENDING - set if any external IRQ is pending */
414 if (nvic_isrpending(s
)) {
417 /* RETTOBASE - set if only one handler is active */
418 if (nvic_rettobase(s
)) {
422 if (s
->vectors
[ARMV7M_EXCP_SYSTICK
].pending
) {
426 if (s
->vectors
[ARMV7M_EXCP_PENDSV
].pending
) {
430 if (s
->vectors
[ARMV7M_EXCP_NMI
].pending
) {
433 /* ISRPREEMPT not implemented */
435 case 0xd08: /* Vector Table Offset. */
436 return cpu
->env
.v7m
.vecbase
;
437 case 0xd0c: /* Application Interrupt/Reset Control. */
438 return 0xfa050000 | (s
->prigroup
<< 8);
439 case 0xd10: /* System Control. */
440 /* TODO: Implement SLEEPONEXIT. */
442 case 0xd14: /* Configuration Control. */
443 return cpu
->env
.v7m
.ccr
;
444 case 0xd24: /* System Handler Status. */
446 if (s
->vectors
[ARMV7M_EXCP_MEM
].active
) {
449 if (s
->vectors
[ARMV7M_EXCP_BUS
].active
) {
452 if (s
->vectors
[ARMV7M_EXCP_USAGE
].active
) {
455 if (s
->vectors
[ARMV7M_EXCP_SVC
].active
) {
458 if (s
->vectors
[ARMV7M_EXCP_DEBUG
].active
) {
461 if (s
->vectors
[ARMV7M_EXCP_PENDSV
].active
) {
464 if (s
->vectors
[ARMV7M_EXCP_SYSTICK
].active
) {
467 if (s
->vectors
[ARMV7M_EXCP_USAGE
].pending
) {
470 if (s
->vectors
[ARMV7M_EXCP_MEM
].pending
) {
473 if (s
->vectors
[ARMV7M_EXCP_BUS
].pending
) {
476 if (s
->vectors
[ARMV7M_EXCP_SVC
].pending
) {
479 if (s
->vectors
[ARMV7M_EXCP_MEM
].enabled
) {
482 if (s
->vectors
[ARMV7M_EXCP_BUS
].enabled
) {
485 if (s
->vectors
[ARMV7M_EXCP_USAGE
].enabled
) {
489 case 0xd28: /* Configurable Fault Status. */
490 return cpu
->env
.v7m
.cfsr
;
491 case 0xd2c: /* Hard Fault Status. */
492 return cpu
->env
.v7m
.hfsr
;
493 case 0xd30: /* Debug Fault Status. */
494 return cpu
->env
.v7m
.dfsr
;
495 case 0xd34: /* MMFAR MemManage Fault Address */
496 return cpu
->env
.v7m
.mmfar
;
497 case 0xd38: /* Bus Fault Address. */
498 return cpu
->env
.v7m
.bfar
;
499 case 0xd3c: /* Aux Fault Status. */
500 /* TODO: Implement fault status registers. */
501 qemu_log_mask(LOG_UNIMP
,
502 "Aux Fault status registers unimplemented\n");
504 case 0xd40: /* PFR0. */
506 case 0xd44: /* PRF1. */
508 case 0xd48: /* DFR0. */
510 case 0xd4c: /* AFR0. */
512 case 0xd50: /* MMFR0. */
514 case 0xd54: /* MMFR1. */
516 case 0xd58: /* MMFR2. */
518 case 0xd5c: /* MMFR3. */
520 case 0xd60: /* ISAR0. */
522 case 0xd64: /* ISAR1. */
524 case 0xd68: /* ISAR2. */
526 case 0xd6c: /* ISAR3. */
528 case 0xd70: /* ISAR4. */
530 /* TODO: Implement debug registers. */
532 qemu_log_mask(LOG_GUEST_ERROR
, "NVIC: Bad read offset 0x%x\n", offset
);
537 static void nvic_writel(NVICState
*s
, uint32_t offset
, uint32_t value
)
539 ARMCPU
*cpu
= s
->cpu
;
542 case 0xd04: /* Interrupt Control State. */
543 if (value
& (1 << 31)) {
544 armv7m_nvic_set_pending(s
, ARMV7M_EXCP_NMI
);
546 if (value
& (1 << 28)) {
547 armv7m_nvic_set_pending(s
, ARMV7M_EXCP_PENDSV
);
548 } else if (value
& (1 << 27)) {
549 armv7m_nvic_clear_pending(s
, ARMV7M_EXCP_PENDSV
);
551 if (value
& (1 << 26)) {
552 armv7m_nvic_set_pending(s
, ARMV7M_EXCP_SYSTICK
);
553 } else if (value
& (1 << 25)) {
554 armv7m_nvic_clear_pending(s
, ARMV7M_EXCP_SYSTICK
);
557 case 0xd08: /* Vector Table Offset. */
558 cpu
->env
.v7m
.vecbase
= value
& 0xffffff80;
560 case 0xd0c: /* Application Interrupt/Reset Control. */
561 if ((value
>> 16) == 0x05fa) {
563 qemu_irq_pulse(s
->sysresetreq
);
566 qemu_log_mask(LOG_GUEST_ERROR
,
567 "Setting VECTCLRACTIVE when not in DEBUG mode "
568 "is UNPREDICTABLE\n");
571 qemu_log_mask(LOG_GUEST_ERROR
,
572 "Setting VECTRESET when not in DEBUG mode "
573 "is UNPREDICTABLE\n");
575 s
->prigroup
= extract32(value
, 8, 3);
579 case 0xd10: /* System Control. */
580 /* TODO: Implement control registers. */
581 qemu_log_mask(LOG_UNIMP
, "NVIC: SCR unimplemented\n");
583 case 0xd14: /* Configuration Control. */
584 /* Enforce RAZ/WI on reserved and must-RAZ/WI bits */
585 value
&= (R_V7M_CCR_STKALIGN_MASK
|
586 R_V7M_CCR_BFHFNMIGN_MASK
|
587 R_V7M_CCR_DIV_0_TRP_MASK
|
588 R_V7M_CCR_UNALIGN_TRP_MASK
|
589 R_V7M_CCR_USERSETMPEND_MASK
|
590 R_V7M_CCR_NONBASETHRDENA_MASK
);
592 cpu
->env
.v7m
.ccr
= value
;
594 case 0xd24: /* System Handler Control. */
595 s
->vectors
[ARMV7M_EXCP_MEM
].active
= (value
& (1 << 0)) != 0;
596 s
->vectors
[ARMV7M_EXCP_BUS
].active
= (value
& (1 << 1)) != 0;
597 s
->vectors
[ARMV7M_EXCP_USAGE
].active
= (value
& (1 << 3)) != 0;
598 s
->vectors
[ARMV7M_EXCP_SVC
].active
= (value
& (1 << 7)) != 0;
599 s
->vectors
[ARMV7M_EXCP_DEBUG
].active
= (value
& (1 << 8)) != 0;
600 s
->vectors
[ARMV7M_EXCP_PENDSV
].active
= (value
& (1 << 10)) != 0;
601 s
->vectors
[ARMV7M_EXCP_SYSTICK
].active
= (value
& (1 << 11)) != 0;
602 s
->vectors
[ARMV7M_EXCP_USAGE
].pending
= (value
& (1 << 12)) != 0;
603 s
->vectors
[ARMV7M_EXCP_MEM
].pending
= (value
& (1 << 13)) != 0;
604 s
->vectors
[ARMV7M_EXCP_BUS
].pending
= (value
& (1 << 14)) != 0;
605 s
->vectors
[ARMV7M_EXCP_SVC
].pending
= (value
& (1 << 15)) != 0;
606 s
->vectors
[ARMV7M_EXCP_MEM
].enabled
= (value
& (1 << 16)) != 0;
607 s
->vectors
[ARMV7M_EXCP_BUS
].enabled
= (value
& (1 << 17)) != 0;
608 s
->vectors
[ARMV7M_EXCP_USAGE
].enabled
= (value
& (1 << 18)) != 0;
611 case 0xd28: /* Configurable Fault Status. */
612 cpu
->env
.v7m
.cfsr
&= ~value
; /* W1C */
614 case 0xd2c: /* Hard Fault Status. */
615 cpu
->env
.v7m
.hfsr
&= ~value
; /* W1C */
617 case 0xd30: /* Debug Fault Status. */
618 cpu
->env
.v7m
.dfsr
&= ~value
; /* W1C */
620 case 0xd34: /* Mem Manage Address. */
621 cpu
->env
.v7m
.mmfar
= value
;
623 case 0xd38: /* Bus Fault Address. */
624 cpu
->env
.v7m
.bfar
= value
;
626 case 0xd3c: /* Aux Fault Status. */
627 qemu_log_mask(LOG_UNIMP
,
628 "NVIC: Aux fault status registers unimplemented\n");
630 case 0xf00: /* Software Triggered Interrupt Register */
632 /* user mode can only write to STIR if CCR.USERSETMPEND permits it */
633 int excnum
= (value
& 0x1ff) + NVIC_FIRST_IRQ
;
634 if (excnum
< s
->num_irq
&&
635 (arm_current_el(&cpu
->env
) ||
636 (cpu
->env
.v7m
.ccr
& R_V7M_CCR_USERSETMPEND_MASK
))) {
637 armv7m_nvic_set_pending(s
, excnum
);
642 qemu_log_mask(LOG_GUEST_ERROR
,
643 "NVIC: Bad write offset 0x%x\n", offset
);
647 static uint64_t nvic_sysreg_read(void *opaque
, hwaddr addr
,
650 NVICState
*s
= (NVICState
*)opaque
;
651 uint32_t offset
= addr
;
652 unsigned i
, startvec
, end
;
656 /* reads of set and clear both return the status */
657 case 0x100 ... 0x13f: /* NVIC Set enable */
660 case 0x180 ... 0x1bf: /* NVIC Clear enable */
662 startvec
= offset
- 0x180 + NVIC_FIRST_IRQ
; /* vector # */
664 for (i
= 0, end
= size
* 8; i
< end
&& startvec
+ i
< s
->num_irq
; i
++) {
665 if (s
->vectors
[startvec
+ i
].enabled
) {
670 case 0x200 ... 0x23f: /* NVIC Set pend */
673 case 0x280 ... 0x2bf: /* NVIC Clear pend */
675 startvec
= offset
- 0x280 + NVIC_FIRST_IRQ
; /* vector # */
676 for (i
= 0, end
= size
* 8; i
< end
&& startvec
+ i
< s
->num_irq
; i
++) {
677 if (s
->vectors
[startvec
+ i
].pending
) {
682 case 0x300 ... 0x33f: /* NVIC Active */
684 startvec
= offset
- 0x300 + NVIC_FIRST_IRQ
; /* vector # */
686 for (i
= 0, end
= size
* 8; i
< end
&& startvec
+ i
< s
->num_irq
; i
++) {
687 if (s
->vectors
[startvec
+ i
].active
) {
692 case 0x400 ... 0x5ef: /* NVIC Priority */
694 startvec
= offset
- 0x400 + NVIC_FIRST_IRQ
; /* vector # */
696 for (i
= 0; i
< size
&& startvec
+ i
< s
->num_irq
; i
++) {
697 val
|= s
->vectors
[startvec
+ i
].prio
<< (8 * i
);
700 case 0xd18 ... 0xd23: /* System Handler Priority. */
702 for (i
= 0; i
< size
; i
++) {
703 val
|= s
->vectors
[(offset
- 0xd14) + i
].prio
<< (i
* 8);
706 case 0xfe0 ... 0xfff: /* ID. */
710 val
= nvic_id
[(offset
- 0xfe0) >> 2];
715 val
= nvic_readl(s
, offset
);
717 qemu_log_mask(LOG_GUEST_ERROR
,
718 "NVIC: Bad read of size %d at offset 0x%x\n",
724 trace_nvic_sysreg_read(addr
, val
, size
);
728 static void nvic_sysreg_write(void *opaque
, hwaddr addr
,
729 uint64_t value
, unsigned size
)
731 NVICState
*s
= (NVICState
*)opaque
;
732 uint32_t offset
= addr
;
733 unsigned i
, startvec
, end
;
736 trace_nvic_sysreg_write(addr
, value
, size
);
739 case 0x100 ... 0x13f: /* NVIC Set enable */
743 case 0x180 ... 0x1bf: /* NVIC Clear enable */
744 startvec
= 8 * (offset
- 0x180) + NVIC_FIRST_IRQ
;
746 for (i
= 0, end
= size
* 8; i
< end
&& startvec
+ i
< s
->num_irq
; i
++) {
747 if (value
& (1 << i
)) {
748 s
->vectors
[startvec
+ i
].enabled
= setval
;
753 case 0x200 ... 0x23f: /* NVIC Set pend */
754 /* the special logic in armv7m_nvic_set_pending()
755 * is not needed since IRQs are never escalated
760 case 0x280 ... 0x2bf: /* NVIC Clear pend */
761 startvec
= 8 * (offset
- 0x280) + NVIC_FIRST_IRQ
; /* vector # */
763 for (i
= 0, end
= size
* 8; i
< end
&& startvec
+ i
< s
->num_irq
; i
++) {
764 if (value
& (1 << i
)) {
765 s
->vectors
[startvec
+ i
].pending
= setval
;
770 case 0x300 ... 0x33f: /* NVIC Active */
772 case 0x400 ... 0x5ef: /* NVIC Priority */
773 startvec
= 8 * (offset
- 0x400) + NVIC_FIRST_IRQ
; /* vector # */
775 for (i
= 0; i
< size
&& startvec
+ i
< s
->num_irq
; i
++) {
776 set_prio(s
, startvec
+ i
, (value
>> (i
* 8)) & 0xff);
780 case 0xd18 ... 0xd23: /* System Handler Priority. */
781 for (i
= 0; i
< size
; i
++) {
782 unsigned hdlidx
= (offset
- 0xd14) + i
;
783 set_prio(s
, hdlidx
, (value
>> (i
* 8)) & 0xff);
789 nvic_writel(s
, offset
, value
);
792 qemu_log_mask(LOG_GUEST_ERROR
,
793 "NVIC: Bad write of size %d at offset 0x%x\n", size
, offset
);
796 static const MemoryRegionOps nvic_sysreg_ops
= {
797 .read
= nvic_sysreg_read
,
798 .write
= nvic_sysreg_write
,
799 .endianness
= DEVICE_NATIVE_ENDIAN
,
802 static int nvic_post_load(void *opaque
, int version_id
)
804 NVICState
*s
= opaque
;
807 /* Check for out of range priority settings */
808 if (s
->vectors
[ARMV7M_EXCP_RESET
].prio
!= -3 ||
809 s
->vectors
[ARMV7M_EXCP_NMI
].prio
!= -2 ||
810 s
->vectors
[ARMV7M_EXCP_HARD
].prio
!= -1) {
813 for (i
= ARMV7M_EXCP_MEM
; i
< s
->num_irq
; i
++) {
814 if (s
->vectors
[i
].prio
& ~0xff) {
819 nvic_recompute_state(s
);
824 static const VMStateDescription vmstate_VecInfo
= {
825 .name
= "armv7m_nvic_info",
827 .minimum_version_id
= 1,
828 .fields
= (VMStateField
[]) {
829 VMSTATE_INT16(prio
, VecInfo
),
830 VMSTATE_UINT8(enabled
, VecInfo
),
831 VMSTATE_UINT8(pending
, VecInfo
),
832 VMSTATE_UINT8(active
, VecInfo
),
833 VMSTATE_UINT8(level
, VecInfo
),
834 VMSTATE_END_OF_LIST()
838 static const VMStateDescription vmstate_nvic
= {
839 .name
= "armv7m_nvic",
841 .minimum_version_id
= 4,
842 .post_load
= &nvic_post_load
,
843 .fields
= (VMStateField
[]) {
844 VMSTATE_STRUCT_ARRAY(vectors
, NVICState
, NVIC_MAX_VECTORS
, 1,
845 vmstate_VecInfo
, VecInfo
),
846 VMSTATE_UINT32(prigroup
, NVICState
),
847 VMSTATE_END_OF_LIST()
851 static Property props_nvic
[] = {
852 /* Number of external IRQ lines (so excluding the 16 internal exceptions) */
853 DEFINE_PROP_UINT32("num-irq", NVICState
, num_irq
, 64),
854 DEFINE_PROP_END_OF_LIST()
857 static void armv7m_nvic_reset(DeviceState
*dev
)
859 NVICState
*s
= NVIC(dev
);
861 s
->vectors
[ARMV7M_EXCP_NMI
].enabled
= 1;
862 s
->vectors
[ARMV7M_EXCP_HARD
].enabled
= 1;
863 /* MEM, BUS, and USAGE are enabled through
864 * the System Handler Control register
866 s
->vectors
[ARMV7M_EXCP_SVC
].enabled
= 1;
867 s
->vectors
[ARMV7M_EXCP_DEBUG
].enabled
= 1;
868 s
->vectors
[ARMV7M_EXCP_PENDSV
].enabled
= 1;
869 s
->vectors
[ARMV7M_EXCP_SYSTICK
].enabled
= 1;
871 s
->vectors
[ARMV7M_EXCP_RESET
].prio
= -3;
872 s
->vectors
[ARMV7M_EXCP_NMI
].prio
= -2;
873 s
->vectors
[ARMV7M_EXCP_HARD
].prio
= -1;
875 /* Strictly speaking the reset handler should be enabled.
876 * However, we don't simulate soft resets through the NVIC,
877 * and the reset vector should never be pended.
878 * So we leave it disabled to catch logic errors.
881 s
->exception_prio
= NVIC_NOEXC_PRIO
;
885 static void nvic_systick_trigger(void *opaque
, int n
, int level
)
887 NVICState
*s
= opaque
;
890 /* SysTick just asked us to pend its exception.
891 * (This is different from an external interrupt line's
894 armv7m_nvic_set_pending(s
, ARMV7M_EXCP_SYSTICK
);
898 static void armv7m_nvic_realize(DeviceState
*dev
, Error
**errp
)
900 NVICState
*s
= NVIC(dev
);
901 SysBusDevice
*systick_sbd
;
904 s
->cpu
= ARM_CPU(qemu_get_cpu(0));
907 if (s
->num_irq
> NVIC_MAX_IRQ
) {
908 error_setg(errp
, "num-irq %d exceeds NVIC maximum", s
->num_irq
);
912 qdev_init_gpio_in(dev
, set_irq_level
, s
->num_irq
);
914 /* include space for internal exception vectors */
915 s
->num_irq
+= NVIC_FIRST_IRQ
;
917 object_property_set_bool(OBJECT(&s
->systick
), true, "realized", &err
);
919 error_propagate(errp
, err
);
922 systick_sbd
= SYS_BUS_DEVICE(&s
->systick
);
923 sysbus_connect_irq(systick_sbd
, 0,
924 qdev_get_gpio_in_named(dev
, "systick-trigger", 0));
926 /* The NVIC and System Control Space (SCS) starts at 0xe000e000
927 * and looks like this:
929 * 0x010 - 0xff - systick
930 * 0x100..0x7ec - NVIC
931 * 0x7f0..0xcff - Reserved
932 * 0xd00..0xd3c - SCS registers
933 * 0xd40..0xeff - Reserved or Not implemented
936 * At the moment there is only one thing in the container region,
937 * but we leave it in place to allow us to pull systick out into
938 * its own device object later.
940 memory_region_init(&s
->container
, OBJECT(s
), "nvic", 0x1000);
941 /* The system register region goes at the bottom of the priority
942 * stack as it covers the whole page.
944 memory_region_init_io(&s
->sysregmem
, OBJECT(s
), &nvic_sysreg_ops
, s
,
945 "nvic_sysregs", 0x1000);
946 memory_region_add_subregion(&s
->container
, 0, &s
->sysregmem
);
947 memory_region_add_subregion_overlap(&s
->container
, 0x10,
948 sysbus_mmio_get_region(systick_sbd
, 0),
951 sysbus_init_mmio(SYS_BUS_DEVICE(dev
), &s
->container
);
954 static void armv7m_nvic_instance_init(Object
*obj
)
956 /* We have a different default value for the num-irq property
957 * than our superclass. This function runs after qdev init
958 * has set the defaults from the Property array and before
959 * any user-specified property setting, so just modify the
960 * value in the GICState struct.
962 DeviceState
*dev
= DEVICE(obj
);
963 NVICState
*nvic
= NVIC(obj
);
964 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
966 object_initialize(&nvic
->systick
, sizeof(nvic
->systick
), TYPE_SYSTICK
);
967 qdev_set_parent_bus(DEVICE(&nvic
->systick
), sysbus_get_default());
969 sysbus_init_irq(sbd
, &nvic
->excpout
);
970 qdev_init_gpio_out_named(dev
, &nvic
->sysresetreq
, "SYSRESETREQ", 1);
971 qdev_init_gpio_in_named(dev
, nvic_systick_trigger
, "systick-trigger", 1);
974 static void armv7m_nvic_class_init(ObjectClass
*klass
, void *data
)
976 DeviceClass
*dc
= DEVICE_CLASS(klass
);
978 dc
->vmsd
= &vmstate_nvic
;
979 dc
->props
= props_nvic
;
980 dc
->reset
= armv7m_nvic_reset
;
981 dc
->realize
= armv7m_nvic_realize
;
984 static const TypeInfo armv7m_nvic_info
= {
986 .parent
= TYPE_SYS_BUS_DEVICE
,
987 .instance_init
= armv7m_nvic_instance_init
,
988 .instance_size
= sizeof(NVICState
),
989 .class_init
= armv7m_nvic_class_init
,
990 .class_size
= sizeof(SysBusDeviceClass
),
993 static void armv7m_nvic_register_types(void)
995 type_register_static(&armv7m_nvic_info
);
998 type_init(armv7m_nvic_register_types
)