nvic: Implement AIRCR changes for v8M
[qemu.git] / hw / intc / armv7m_nvic.c
1 /*
2  * ARM Nested Vectored Interrupt Controller
3  *
4  * Copyright (c) 2006-2007 CodeSourcery.
5  * Written by Paul Brook
6  *
7  * This code is licensed under the GPL.
8  *
9  * The ARMv7M System controller is fairly tightly tied in with the
10  * NVIC.  Much of that is also implemented here.
11  */
12
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu-common.h"
16 #include "cpu.h"
17 #include "hw/sysbus.h"
18 #include "qemu/timer.h"
19 #include "hw/arm/arm.h"
20 #include "hw/intc/armv7m_nvic.h"
21 #include "target/arm/cpu.h"
22 #include "exec/exec-all.h"
23 #include "qemu/log.h"
24 #include "trace.h"
25
26 /* IRQ number counting:
27  *
28  * the num-irq property counts the number of external IRQ lines
29  *
30  * NVICState::num_irq counts the total number of exceptions
31  * (external IRQs, the 15 internal exceptions including reset,
32  * and one for the unused exception number 0).
33  *
34  * NVIC_MAX_IRQ is the highest permitted number of external IRQ lines.
35  *
36  * NVIC_MAX_VECTORS is the highest permitted number of exceptions.
37  *
38  * Iterating through all exceptions should typically be done with
39  * for (i = 1; i < s->num_irq; i++) to avoid the unused slot 0.
40  *
41  * The external qemu_irq lines are the NVIC's external IRQ lines,
42  * so line 0 is exception 16.
43  *
44  * In the terminology of the architecture manual, "interrupts" are
45  * a subcategory of exception referring to the external interrupts
46  * (which are exception numbers NVIC_FIRST_IRQ and upward).
47  * For historical reasons QEMU tends to use "interrupt" and
48  * "exception" more or less interchangeably.
49  */
50 #define NVIC_FIRST_IRQ NVIC_INTERNAL_VECTORS
51 #define NVIC_MAX_IRQ (NVIC_MAX_VECTORS - NVIC_FIRST_IRQ)
52
53 /* Effective running priority of the CPU when no exception is active
54  * (higher than the highest possible priority value)
55  */
56 #define NVIC_NOEXC_PRIO 0x100
57
58 static const uint8_t nvic_id[] = {
59     0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1
60 };
61
62 static int nvic_pending_prio(NVICState *s)
63 {
64     /* return the group priority of the current pending interrupt,
65      * or NVIC_NOEXC_PRIO if no interrupt is pending
66      */
67     return s->vectpending_prio;
68 }
69
70 /* Return the value of the ISCR RETTOBASE bit:
71  * 1 if there is exactly one active exception
72  * 0 if there is more than one active exception
73  * UNKNOWN if there are no active exceptions (we choose 1,
74  * which matches the choice Cortex-M3 is documented as making).
75  *
76  * NB: some versions of the documentation talk about this
77  * counting "active exceptions other than the one shown by IPSR";
78  * this is only different in the obscure corner case where guest
79  * code has manually deactivated an exception and is about
80  * to fail an exception-return integrity check. The definition
81  * above is the one from the v8M ARM ARM and is also in line
82  * with the behaviour documented for the Cortex-M3.
83  */
84 static bool nvic_rettobase(NVICState *s)
85 {
86     int irq, nhand = 0;
87
88     for (irq = ARMV7M_EXCP_RESET; irq < s->num_irq; irq++) {
89         if (s->vectors[irq].active) {
90             nhand++;
91             if (nhand == 2) {
92                 return 0;
93             }
94         }
95     }
96
97     return 1;
98 }
99
100 /* Return the value of the ISCR ISRPENDING bit:
101  * 1 if an external interrupt is pending
102  * 0 if no external interrupt is pending
103  */
104 static bool nvic_isrpending(NVICState *s)
105 {
106     int irq;
107
108     /* We can shortcut if the highest priority pending interrupt
109      * happens to be external or if there is nothing pending.
110      */
111     if (s->vectpending > NVIC_FIRST_IRQ) {
112         return true;
113     }
114     if (s->vectpending == 0) {
115         return false;
116     }
117
118     for (irq = NVIC_FIRST_IRQ; irq < s->num_irq; irq++) {
119         if (s->vectors[irq].pending) {
120             return true;
121         }
122     }
123     return false;
124 }
125
126 /* Return a mask word which clears the subpriority bits from
127  * a priority value for an M-profile exception, leaving only
128  * the group priority.
129  */
130 static inline uint32_t nvic_gprio_mask(NVICState *s)
131 {
132     return ~0U << (s->prigroup[M_REG_NS] + 1);
133 }
134
135 /* Recompute vectpending and exception_prio */
136 static void nvic_recompute_state(NVICState *s)
137 {
138     int i;
139     int pend_prio = NVIC_NOEXC_PRIO;
140     int active_prio = NVIC_NOEXC_PRIO;
141     int pend_irq = 0;
142
143     for (i = 1; i < s->num_irq; i++) {
144         VecInfo *vec = &s->vectors[i];
145
146         if (vec->enabled && vec->pending && vec->prio < pend_prio) {
147             pend_prio = vec->prio;
148             pend_irq = i;
149         }
150         if (vec->active && vec->prio < active_prio) {
151             active_prio = vec->prio;
152         }
153     }
154
155     if (active_prio > 0) {
156         active_prio &= nvic_gprio_mask(s);
157     }
158
159     if (pend_prio > 0) {
160         pend_prio &= nvic_gprio_mask(s);
161     }
162
163     s->vectpending = pend_irq;
164     s->vectpending_prio = pend_prio;
165     s->exception_prio = active_prio;
166
167     trace_nvic_recompute_state(s->vectpending,
168                                s->vectpending_prio,
169                                s->exception_prio);
170 }
171
172 /* Return the current execution priority of the CPU
173  * (equivalent to the pseudocode ExecutionPriority function).
174  * This is a value between -2 (NMI priority) and NVIC_NOEXC_PRIO.
175  */
176 static inline int nvic_exec_prio(NVICState *s)
177 {
178     CPUARMState *env = &s->cpu->env;
179     int running;
180
181     if (env->v7m.faultmask[env->v7m.secure]) {
182         running = -1;
183     } else if (env->v7m.primask[env->v7m.secure]) {
184         running = 0;
185     } else if (env->v7m.basepri[env->v7m.secure] > 0) {
186         running = env->v7m.basepri[env->v7m.secure] & nvic_gprio_mask(s);
187     } else {
188         running = NVIC_NOEXC_PRIO; /* lower than any possible priority */
189     }
190     /* consider priority of active handler */
191     return MIN(running, s->exception_prio);
192 }
193
194 bool armv7m_nvic_can_take_pending_exception(void *opaque)
195 {
196     NVICState *s = opaque;
197
198     return nvic_exec_prio(s) > nvic_pending_prio(s);
199 }
200
201 int armv7m_nvic_raw_execution_priority(void *opaque)
202 {
203     NVICState *s = opaque;
204
205     return s->exception_prio;
206 }
207
208 /* caller must call nvic_irq_update() after this */
209 static void set_prio(NVICState *s, unsigned irq, uint8_t prio)
210 {
211     assert(irq > ARMV7M_EXCP_NMI); /* only use for configurable prios */
212     assert(irq < s->num_irq);
213
214     s->vectors[irq].prio = prio;
215
216     trace_nvic_set_prio(irq, prio);
217 }
218
219 /* Recompute state and assert irq line accordingly.
220  * Must be called after changes to:
221  *  vec->active, vec->enabled, vec->pending or vec->prio for any vector
222  *  prigroup
223  */
224 static void nvic_irq_update(NVICState *s)
225 {
226     int lvl;
227     int pend_prio;
228
229     nvic_recompute_state(s);
230     pend_prio = nvic_pending_prio(s);
231
232     /* Raise NVIC output if this IRQ would be taken, except that we
233      * ignore the effects of the BASEPRI, FAULTMASK and PRIMASK (which
234      * will be checked for in arm_v7m_cpu_exec_interrupt()); changes
235      * to those CPU registers don't cause us to recalculate the NVIC
236      * pending info.
237      */
238     lvl = (pend_prio < s->exception_prio);
239     trace_nvic_irq_update(s->vectpending, pend_prio, s->exception_prio, lvl);
240     qemu_set_irq(s->excpout, lvl);
241 }
242
243 static void armv7m_nvic_clear_pending(void *opaque, int irq)
244 {
245     NVICState *s = (NVICState *)opaque;
246     VecInfo *vec;
247
248     assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
249
250     vec = &s->vectors[irq];
251     trace_nvic_clear_pending(irq, vec->enabled, vec->prio);
252     if (vec->pending) {
253         vec->pending = 0;
254         nvic_irq_update(s);
255     }
256 }
257
258 void armv7m_nvic_set_pending(void *opaque, int irq)
259 {
260     NVICState *s = (NVICState *)opaque;
261     VecInfo *vec;
262
263     assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
264
265     vec = &s->vectors[irq];
266     trace_nvic_set_pending(irq, vec->enabled, vec->prio);
267
268
269     if (irq >= ARMV7M_EXCP_HARD && irq < ARMV7M_EXCP_PENDSV) {
270         /* If a synchronous exception is pending then it may be
271          * escalated to HardFault if:
272          *  * it is equal or lower priority to current execution
273          *  * it is disabled
274          * (ie we need to take it immediately but we can't do so).
275          * Asynchronous exceptions (and interrupts) simply remain pending.
276          *
277          * For QEMU, we don't have any imprecise (asynchronous) faults,
278          * so we can assume that PREFETCH_ABORT and DATA_ABORT are always
279          * synchronous.
280          * Debug exceptions are awkward because only Debug exceptions
281          * resulting from the BKPT instruction should be escalated,
282          * but we don't currently implement any Debug exceptions other
283          * than those that result from BKPT, so we treat all debug exceptions
284          * as needing escalation.
285          *
286          * This all means we can identify whether to escalate based only on
287          * the exception number and don't (yet) need the caller to explicitly
288          * tell us whether this exception is synchronous or not.
289          */
290         int running = nvic_exec_prio(s);
291         bool escalate = false;
292
293         if (vec->prio >= running) {
294             trace_nvic_escalate_prio(irq, vec->prio, running);
295             escalate = true;
296         } else if (!vec->enabled) {
297             trace_nvic_escalate_disabled(irq);
298             escalate = true;
299         }
300
301         if (escalate) {
302             if (running < 0) {
303                 /* We want to escalate to HardFault but we can't take a
304                  * synchronous HardFault at this point either. This is a
305                  * Lockup condition due to a guest bug. We don't model
306                  * Lockup, so report via cpu_abort() instead.
307                  */
308                 cpu_abort(&s->cpu->parent_obj,
309                           "Lockup: can't escalate %d to HardFault "
310                           "(current priority %d)\n", irq, running);
311             }
312
313             /* We can do the escalation, so we take HardFault instead */
314             irq = ARMV7M_EXCP_HARD;
315             vec = &s->vectors[irq];
316             s->cpu->env.v7m.hfsr |= R_V7M_HFSR_FORCED_MASK;
317         }
318     }
319
320     if (!vec->pending) {
321         vec->pending = 1;
322         nvic_irq_update(s);
323     }
324 }
325
326 /* Make pending IRQ active.  */
327 void armv7m_nvic_acknowledge_irq(void *opaque)
328 {
329     NVICState *s = (NVICState *)opaque;
330     CPUARMState *env = &s->cpu->env;
331     const int pending = s->vectpending;
332     const int running = nvic_exec_prio(s);
333     VecInfo *vec;
334
335     assert(pending > ARMV7M_EXCP_RESET && pending < s->num_irq);
336
337     vec = &s->vectors[pending];
338
339     assert(vec->enabled);
340     assert(vec->pending);
341
342     assert(s->vectpending_prio < running);
343
344     trace_nvic_acknowledge_irq(pending, s->vectpending_prio);
345
346     vec->active = 1;
347     vec->pending = 0;
348
349     env->v7m.exception = s->vectpending;
350
351     nvic_irq_update(s);
352 }
353
354 int armv7m_nvic_complete_irq(void *opaque, int irq)
355 {
356     NVICState *s = (NVICState *)opaque;
357     VecInfo *vec;
358     int ret;
359
360     assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
361
362     vec = &s->vectors[irq];
363
364     trace_nvic_complete_irq(irq);
365
366     if (!vec->active) {
367         /* Tell the caller this was an illegal exception return */
368         return -1;
369     }
370
371     ret = nvic_rettobase(s);
372
373     vec->active = 0;
374     if (vec->level) {
375         /* Re-pend the exception if it's still held high; only
376          * happens for extenal IRQs
377          */
378         assert(irq >= NVIC_FIRST_IRQ);
379         vec->pending = 1;
380     }
381
382     nvic_irq_update(s);
383
384     return ret;
385 }
386
387 /* callback when external interrupt line is changed */
388 static void set_irq_level(void *opaque, int n, int level)
389 {
390     NVICState *s = opaque;
391     VecInfo *vec;
392
393     n += NVIC_FIRST_IRQ;
394
395     assert(n >= NVIC_FIRST_IRQ && n < s->num_irq);
396
397     trace_nvic_set_irq_level(n, level);
398
399     /* The pending status of an external interrupt is
400      * latched on rising edge and exception handler return.
401      *
402      * Pulsing the IRQ will always run the handler
403      * once, and the handler will re-run until the
404      * level is low when the handler completes.
405      */
406     vec = &s->vectors[n];
407     if (level != vec->level) {
408         vec->level = level;
409         if (level) {
410             armv7m_nvic_set_pending(s, n);
411         }
412     }
413 }
414
415 static uint32_t nvic_readl(NVICState *s, uint32_t offset, MemTxAttrs attrs)
416 {
417     ARMCPU *cpu = s->cpu;
418     uint32_t val;
419
420     switch (offset) {
421     case 4: /* Interrupt Control Type.  */
422         return ((s->num_irq - NVIC_FIRST_IRQ) / 32) - 1;
423     case 0xd00: /* CPUID Base.  */
424         return cpu->midr;
425     case 0xd04: /* Interrupt Control State.  */
426         /* VECTACTIVE */
427         val = cpu->env.v7m.exception;
428         /* VECTPENDING */
429         val |= (s->vectpending & 0xff) << 12;
430         /* ISRPENDING - set if any external IRQ is pending */
431         if (nvic_isrpending(s)) {
432             val |= (1 << 22);
433         }
434         /* RETTOBASE - set if only one handler is active */
435         if (nvic_rettobase(s)) {
436             val |= (1 << 11);
437         }
438         /* PENDSTSET */
439         if (s->vectors[ARMV7M_EXCP_SYSTICK].pending) {
440             val |= (1 << 26);
441         }
442         /* PENDSVSET */
443         if (s->vectors[ARMV7M_EXCP_PENDSV].pending) {
444             val |= (1 << 28);
445         }
446         /* NMIPENDSET */
447         if (s->vectors[ARMV7M_EXCP_NMI].pending) {
448             val |= (1 << 31);
449         }
450         /* ISRPREEMPT not implemented */
451         return val;
452     case 0xd08: /* Vector Table Offset.  */
453         return cpu->env.v7m.vecbase[attrs.secure];
454     case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */
455         val = 0xfa050000 | (s->prigroup[attrs.secure] << 8);
456         if (attrs.secure) {
457             /* s->aircr stores PRIS, BFHFNMINS, SYSRESETREQS */
458             val |= cpu->env.v7m.aircr;
459         } else {
460             if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
461                 /* BFHFNMINS is R/O from NS; other bits are RAZ/WI. If
462                  * security isn't supported then BFHFNMINS is RAO (and
463                  * the bit in env.v7m.aircr is always set).
464                  */
465                 val |= cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK;
466             }
467         }
468         return val;
469     case 0xd10: /* System Control.  */
470         /* TODO: Implement SLEEPONEXIT.  */
471         return 0;
472     case 0xd14: /* Configuration Control.  */
473         /* The BFHFNMIGN bit is the only non-banked bit; we
474          * keep it in the non-secure copy of the register.
475          */
476         val = cpu->env.v7m.ccr[attrs.secure];
477         val |= cpu->env.v7m.ccr[M_REG_NS] & R_V7M_CCR_BFHFNMIGN_MASK;
478         return val;
479     case 0xd24: /* System Handler Status.  */
480         val = 0;
481         if (s->vectors[ARMV7M_EXCP_MEM].active) {
482             val |= (1 << 0);
483         }
484         if (s->vectors[ARMV7M_EXCP_BUS].active) {
485             val |= (1 << 1);
486         }
487         if (s->vectors[ARMV7M_EXCP_USAGE].active) {
488             val |= (1 << 3);
489         }
490         if (s->vectors[ARMV7M_EXCP_SVC].active) {
491             val |= (1 << 7);
492         }
493         if (s->vectors[ARMV7M_EXCP_DEBUG].active) {
494             val |= (1 << 8);
495         }
496         if (s->vectors[ARMV7M_EXCP_PENDSV].active) {
497             val |= (1 << 10);
498         }
499         if (s->vectors[ARMV7M_EXCP_SYSTICK].active) {
500             val |= (1 << 11);
501         }
502         if (s->vectors[ARMV7M_EXCP_USAGE].pending) {
503             val |= (1 << 12);
504         }
505         if (s->vectors[ARMV7M_EXCP_MEM].pending) {
506             val |= (1 << 13);
507         }
508         if (s->vectors[ARMV7M_EXCP_BUS].pending) {
509             val |= (1 << 14);
510         }
511         if (s->vectors[ARMV7M_EXCP_SVC].pending) {
512             val |= (1 << 15);
513         }
514         if (s->vectors[ARMV7M_EXCP_MEM].enabled) {
515             val |= (1 << 16);
516         }
517         if (s->vectors[ARMV7M_EXCP_BUS].enabled) {
518             val |= (1 << 17);
519         }
520         if (s->vectors[ARMV7M_EXCP_USAGE].enabled) {
521             val |= (1 << 18);
522         }
523         return val;
524     case 0xd28: /* Configurable Fault Status.  */
525         /* The BFSR bits [15:8] are shared between security states
526          * and we store them in the NS copy
527          */
528         val = cpu->env.v7m.cfsr[attrs.secure];
529         val |= cpu->env.v7m.cfsr[M_REG_NS] & R_V7M_CFSR_BFSR_MASK;
530         return val;
531     case 0xd2c: /* Hard Fault Status.  */
532         return cpu->env.v7m.hfsr;
533     case 0xd30: /* Debug Fault Status.  */
534         return cpu->env.v7m.dfsr;
535     case 0xd34: /* MMFAR MemManage Fault Address */
536         return cpu->env.v7m.mmfar[attrs.secure];
537     case 0xd38: /* Bus Fault Address.  */
538         return cpu->env.v7m.bfar;
539     case 0xd3c: /* Aux Fault Status.  */
540         /* TODO: Implement fault status registers.  */
541         qemu_log_mask(LOG_UNIMP,
542                       "Aux Fault status registers unimplemented\n");
543         return 0;
544     case 0xd40: /* PFR0.  */
545         return 0x00000030;
546     case 0xd44: /* PRF1.  */
547         return 0x00000200;
548     case 0xd48: /* DFR0.  */
549         return 0x00100000;
550     case 0xd4c: /* AFR0.  */
551         return 0x00000000;
552     case 0xd50: /* MMFR0.  */
553         return 0x00000030;
554     case 0xd54: /* MMFR1.  */
555         return 0x00000000;
556     case 0xd58: /* MMFR2.  */
557         return 0x00000000;
558     case 0xd5c: /* MMFR3.  */
559         return 0x00000000;
560     case 0xd60: /* ISAR0.  */
561         return 0x01141110;
562     case 0xd64: /* ISAR1.  */
563         return 0x02111000;
564     case 0xd68: /* ISAR2.  */
565         return 0x21112231;
566     case 0xd6c: /* ISAR3.  */
567         return 0x01111110;
568     case 0xd70: /* ISAR4.  */
569         return 0x01310102;
570     /* TODO: Implement debug registers.  */
571     case 0xd90: /* MPU_TYPE */
572         /* Unified MPU; if the MPU is not present this value is zero */
573         return cpu->pmsav7_dregion << 8;
574         break;
575     case 0xd94: /* MPU_CTRL */
576         return cpu->env.v7m.mpu_ctrl[attrs.secure];
577     case 0xd98: /* MPU_RNR */
578         return cpu->env.pmsav7.rnr[attrs.secure];
579     case 0xd9c: /* MPU_RBAR */
580     case 0xda4: /* MPU_RBAR_A1 */
581     case 0xdac: /* MPU_RBAR_A2 */
582     case 0xdb4: /* MPU_RBAR_A3 */
583     {
584         int region = cpu->env.pmsav7.rnr[attrs.secure];
585
586         if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
587             /* PMSAv8M handling of the aliases is different from v7M:
588              * aliases A1, A2, A3 override the low two bits of the region
589              * number in MPU_RNR, and there is no 'region' field in the
590              * RBAR register.
591              */
592             int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
593             if (aliasno) {
594                 region = deposit32(region, 0, 2, aliasno);
595             }
596             if (region >= cpu->pmsav7_dregion) {
597                 return 0;
598             }
599             return cpu->env.pmsav8.rbar[attrs.secure][region];
600         }
601
602         if (region >= cpu->pmsav7_dregion) {
603             return 0;
604         }
605         return (cpu->env.pmsav7.drbar[region] & 0x1f) | (region & 0xf);
606     }
607     case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */
608     case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */
609     case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */
610     case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */
611     {
612         int region = cpu->env.pmsav7.rnr[attrs.secure];
613
614         if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
615             /* PMSAv8M handling of the aliases is different from v7M:
616              * aliases A1, A2, A3 override the low two bits of the region
617              * number in MPU_RNR.
618              */
619             int aliasno = (offset - 0xda0) / 8; /* 0..3 */
620             if (aliasno) {
621                 region = deposit32(region, 0, 2, aliasno);
622             }
623             if (region >= cpu->pmsav7_dregion) {
624                 return 0;
625             }
626             return cpu->env.pmsav8.rlar[attrs.secure][region];
627         }
628
629         if (region >= cpu->pmsav7_dregion) {
630             return 0;
631         }
632         return ((cpu->env.pmsav7.dracr[region] & 0xffff) << 16) |
633             (cpu->env.pmsav7.drsr[region] & 0xffff);
634     }
635     case 0xdc0: /* MPU_MAIR0 */
636         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
637             goto bad_offset;
638         }
639         return cpu->env.pmsav8.mair0[attrs.secure];
640     case 0xdc4: /* MPU_MAIR1 */
641         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
642             goto bad_offset;
643         }
644         return cpu->env.pmsav8.mair1[attrs.secure];
645     default:
646     bad_offset:
647         qemu_log_mask(LOG_GUEST_ERROR, "NVIC: Bad read offset 0x%x\n", offset);
648         return 0;
649     }
650 }
651
652 static void nvic_writel(NVICState *s, uint32_t offset, uint32_t value,
653                         MemTxAttrs attrs)
654 {
655     ARMCPU *cpu = s->cpu;
656
657     switch (offset) {
658     case 0xd04: /* Interrupt Control State.  */
659         if (value & (1 << 31)) {
660             armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI);
661         }
662         if (value & (1 << 28)) {
663             armv7m_nvic_set_pending(s, ARMV7M_EXCP_PENDSV);
664         } else if (value & (1 << 27)) {
665             armv7m_nvic_clear_pending(s, ARMV7M_EXCP_PENDSV);
666         }
667         if (value & (1 << 26)) {
668             armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK);
669         } else if (value & (1 << 25)) {
670             armv7m_nvic_clear_pending(s, ARMV7M_EXCP_SYSTICK);
671         }
672         break;
673     case 0xd08: /* Vector Table Offset.  */
674         cpu->env.v7m.vecbase[attrs.secure] = value & 0xffffff80;
675         break;
676     case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */
677         if ((value >> R_V7M_AIRCR_VECTKEY_SHIFT) == 0x05fa) {
678             if (value & R_V7M_AIRCR_SYSRESETREQ_MASK) {
679                 if (attrs.secure ||
680                     !(cpu->env.v7m.aircr & R_V7M_AIRCR_SYSRESETREQS_MASK)) {
681                     qemu_irq_pulse(s->sysresetreq);
682                 }
683             }
684             if (value & R_V7M_AIRCR_VECTCLRACTIVE_MASK) {
685                 qemu_log_mask(LOG_GUEST_ERROR,
686                               "Setting VECTCLRACTIVE when not in DEBUG mode "
687                               "is UNPREDICTABLE\n");
688             }
689             if (value & R_V7M_AIRCR_VECTRESET_MASK) {
690                 /* NB: this bit is RES0 in v8M */
691                 qemu_log_mask(LOG_GUEST_ERROR,
692                               "Setting VECTRESET when not in DEBUG mode "
693                               "is UNPREDICTABLE\n");
694             }
695             s->prigroup[attrs.secure] = extract32(value,
696                                                   R_V7M_AIRCR_PRIGROUP_SHIFT,
697                                                   R_V7M_AIRCR_PRIGROUP_LENGTH);
698             if (attrs.secure) {
699                 /* These bits are only writable by secure */
700                 cpu->env.v7m.aircr = value &
701                     (R_V7M_AIRCR_SYSRESETREQS_MASK |
702                      R_V7M_AIRCR_BFHFNMINS_MASK |
703                      R_V7M_AIRCR_PRIS_MASK);
704             }
705             nvic_irq_update(s);
706         }
707         break;
708     case 0xd10: /* System Control.  */
709         /* TODO: Implement control registers.  */
710         qemu_log_mask(LOG_UNIMP, "NVIC: SCR unimplemented\n");
711         break;
712     case 0xd14: /* Configuration Control.  */
713         /* Enforce RAZ/WI on reserved and must-RAZ/WI bits */
714         value &= (R_V7M_CCR_STKALIGN_MASK |
715                   R_V7M_CCR_BFHFNMIGN_MASK |
716                   R_V7M_CCR_DIV_0_TRP_MASK |
717                   R_V7M_CCR_UNALIGN_TRP_MASK |
718                   R_V7M_CCR_USERSETMPEND_MASK |
719                   R_V7M_CCR_NONBASETHRDENA_MASK);
720
721         if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
722             /* v8M makes NONBASETHRDENA and STKALIGN be RES1 */
723             value |= R_V7M_CCR_NONBASETHRDENA_MASK
724                 | R_V7M_CCR_STKALIGN_MASK;
725         }
726         if (attrs.secure) {
727             /* the BFHFNMIGN bit is not banked; keep that in the NS copy */
728             cpu->env.v7m.ccr[M_REG_NS] =
729                 (cpu->env.v7m.ccr[M_REG_NS] & ~R_V7M_CCR_BFHFNMIGN_MASK)
730                 | (value & R_V7M_CCR_BFHFNMIGN_MASK);
731             value &= ~R_V7M_CCR_BFHFNMIGN_MASK;
732         }
733
734         cpu->env.v7m.ccr[attrs.secure] = value;
735         break;
736     case 0xd24: /* System Handler Control.  */
737         s->vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0;
738         s->vectors[ARMV7M_EXCP_BUS].active = (value & (1 << 1)) != 0;
739         s->vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0;
740         s->vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0;
741         s->vectors[ARMV7M_EXCP_DEBUG].active = (value & (1 << 8)) != 0;
742         s->vectors[ARMV7M_EXCP_PENDSV].active = (value & (1 << 10)) != 0;
743         s->vectors[ARMV7M_EXCP_SYSTICK].active = (value & (1 << 11)) != 0;
744         s->vectors[ARMV7M_EXCP_USAGE].pending = (value & (1 << 12)) != 0;
745         s->vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0;
746         s->vectors[ARMV7M_EXCP_BUS].pending = (value & (1 << 14)) != 0;
747         s->vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0;
748         s->vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0;
749         s->vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0;
750         s->vectors[ARMV7M_EXCP_USAGE].enabled = (value & (1 << 18)) != 0;
751         nvic_irq_update(s);
752         break;
753     case 0xd28: /* Configurable Fault Status.  */
754         cpu->env.v7m.cfsr[attrs.secure] &= ~value; /* W1C */
755         if (attrs.secure) {
756             /* The BFSR bits [15:8] are shared between security states
757              * and we store them in the NS copy.
758              */
759             cpu->env.v7m.cfsr[M_REG_NS] &= ~(value & R_V7M_CFSR_BFSR_MASK);
760         }
761         break;
762     case 0xd2c: /* Hard Fault Status.  */
763         cpu->env.v7m.hfsr &= ~value; /* W1C */
764         break;
765     case 0xd30: /* Debug Fault Status.  */
766         cpu->env.v7m.dfsr &= ~value; /* W1C */
767         break;
768     case 0xd34: /* Mem Manage Address.  */
769         cpu->env.v7m.mmfar[attrs.secure] = value;
770         return;
771     case 0xd38: /* Bus Fault Address.  */
772         cpu->env.v7m.bfar = value;
773         return;
774     case 0xd3c: /* Aux Fault Status.  */
775         qemu_log_mask(LOG_UNIMP,
776                       "NVIC: Aux fault status registers unimplemented\n");
777         break;
778     case 0xd90: /* MPU_TYPE */
779         return; /* RO */
780     case 0xd94: /* MPU_CTRL */
781         if ((value &
782              (R_V7M_MPU_CTRL_HFNMIENA_MASK | R_V7M_MPU_CTRL_ENABLE_MASK))
783             == R_V7M_MPU_CTRL_HFNMIENA_MASK) {
784             qemu_log_mask(LOG_GUEST_ERROR, "MPU_CTRL: HFNMIENA and !ENABLE is "
785                           "UNPREDICTABLE\n");
786         }
787         cpu->env.v7m.mpu_ctrl[attrs.secure]
788             = value & (R_V7M_MPU_CTRL_ENABLE_MASK |
789                        R_V7M_MPU_CTRL_HFNMIENA_MASK |
790                        R_V7M_MPU_CTRL_PRIVDEFENA_MASK);
791         tlb_flush(CPU(cpu));
792         break;
793     case 0xd98: /* MPU_RNR */
794         if (value >= cpu->pmsav7_dregion) {
795             qemu_log_mask(LOG_GUEST_ERROR, "MPU region out of range %"
796                           PRIu32 "/%" PRIu32 "\n",
797                           value, cpu->pmsav7_dregion);
798         } else {
799             cpu->env.pmsav7.rnr[attrs.secure] = value;
800         }
801         break;
802     case 0xd9c: /* MPU_RBAR */
803     case 0xda4: /* MPU_RBAR_A1 */
804     case 0xdac: /* MPU_RBAR_A2 */
805     case 0xdb4: /* MPU_RBAR_A3 */
806     {
807         int region;
808
809         if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
810             /* PMSAv8M handling of the aliases is different from v7M:
811              * aliases A1, A2, A3 override the low two bits of the region
812              * number in MPU_RNR, and there is no 'region' field in the
813              * RBAR register.
814              */
815             int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
816
817             region = cpu->env.pmsav7.rnr[attrs.secure];
818             if (aliasno) {
819                 region = deposit32(region, 0, 2, aliasno);
820             }
821             if (region >= cpu->pmsav7_dregion) {
822                 return;
823             }
824             cpu->env.pmsav8.rbar[attrs.secure][region] = value;
825             tlb_flush(CPU(cpu));
826             return;
827         }
828
829         if (value & (1 << 4)) {
830             /* VALID bit means use the region number specified in this
831              * value and also update MPU_RNR.REGION with that value.
832              */
833             region = extract32(value, 0, 4);
834             if (region >= cpu->pmsav7_dregion) {
835                 qemu_log_mask(LOG_GUEST_ERROR,
836                               "MPU region out of range %u/%" PRIu32 "\n",
837                               region, cpu->pmsav7_dregion);
838                 return;
839             }
840             cpu->env.pmsav7.rnr[attrs.secure] = region;
841         } else {
842             region = cpu->env.pmsav7.rnr[attrs.secure];
843         }
844
845         if (region >= cpu->pmsav7_dregion) {
846             return;
847         }
848
849         cpu->env.pmsav7.drbar[region] = value & ~0x1f;
850         tlb_flush(CPU(cpu));
851         break;
852     }
853     case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */
854     case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */
855     case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */
856     case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */
857     {
858         int region = cpu->env.pmsav7.rnr[attrs.secure];
859
860         if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
861             /* PMSAv8M handling of the aliases is different from v7M:
862              * aliases A1, A2, A3 override the low two bits of the region
863              * number in MPU_RNR.
864              */
865             int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
866
867             region = cpu->env.pmsav7.rnr[attrs.secure];
868             if (aliasno) {
869                 region = deposit32(region, 0, 2, aliasno);
870             }
871             if (region >= cpu->pmsav7_dregion) {
872                 return;
873             }
874             cpu->env.pmsav8.rlar[attrs.secure][region] = value;
875             tlb_flush(CPU(cpu));
876             return;
877         }
878
879         if (region >= cpu->pmsav7_dregion) {
880             return;
881         }
882
883         cpu->env.pmsav7.drsr[region] = value & 0xff3f;
884         cpu->env.pmsav7.dracr[region] = (value >> 16) & 0x173f;
885         tlb_flush(CPU(cpu));
886         break;
887     }
888     case 0xdc0: /* MPU_MAIR0 */
889         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
890             goto bad_offset;
891         }
892         if (cpu->pmsav7_dregion) {
893             /* Register is RES0 if no MPU regions are implemented */
894             cpu->env.pmsav8.mair0[attrs.secure] = value;
895         }
896         /* We don't need to do anything else because memory attributes
897          * only affect cacheability, and we don't implement caching.
898          */
899         break;
900     case 0xdc4: /* MPU_MAIR1 */
901         if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
902             goto bad_offset;
903         }
904         if (cpu->pmsav7_dregion) {
905             /* Register is RES0 if no MPU regions are implemented */
906             cpu->env.pmsav8.mair1[attrs.secure] = value;
907         }
908         /* We don't need to do anything else because memory attributes
909          * only affect cacheability, and we don't implement caching.
910          */
911         break;
912     case 0xf00: /* Software Triggered Interrupt Register */
913     {
914         int excnum = (value & 0x1ff) + NVIC_FIRST_IRQ;
915         if (excnum < s->num_irq) {
916             armv7m_nvic_set_pending(s, excnum);
917         }
918         break;
919     }
920     default:
921     bad_offset:
922         qemu_log_mask(LOG_GUEST_ERROR,
923                       "NVIC: Bad write offset 0x%x\n", offset);
924     }
925 }
926
927 static bool nvic_user_access_ok(NVICState *s, hwaddr offset, MemTxAttrs attrs)
928 {
929     /* Return true if unprivileged access to this register is permitted. */
930     switch (offset) {
931     case 0xf00: /* STIR: accessible only if CCR.USERSETMPEND permits */
932         /* For access via STIR_NS it is the NS CCR.USERSETMPEND that
933          * controls access even though the CPU is in Secure state (I_QDKX).
934          */
935         return s->cpu->env.v7m.ccr[attrs.secure] & R_V7M_CCR_USERSETMPEND_MASK;
936     default:
937         /* All other user accesses cause a BusFault unconditionally */
938         return false;
939     }
940 }
941
942 static MemTxResult nvic_sysreg_read(void *opaque, hwaddr addr,
943                                     uint64_t *data, unsigned size,
944                                     MemTxAttrs attrs)
945 {
946     NVICState *s = (NVICState *)opaque;
947     uint32_t offset = addr;
948     unsigned i, startvec, end;
949     uint32_t val;
950
951     if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) {
952         /* Generate BusFault for unprivileged accesses */
953         return MEMTX_ERROR;
954     }
955
956     switch (offset) {
957     /* reads of set and clear both return the status */
958     case 0x100 ... 0x13f: /* NVIC Set enable */
959         offset += 0x80;
960         /* fall through */
961     case 0x180 ... 0x1bf: /* NVIC Clear enable */
962         val = 0;
963         startvec = offset - 0x180 + NVIC_FIRST_IRQ; /* vector # */
964
965         for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
966             if (s->vectors[startvec + i].enabled) {
967                 val |= (1 << i);
968             }
969         }
970         break;
971     case 0x200 ... 0x23f: /* NVIC Set pend */
972         offset += 0x80;
973         /* fall through */
974     case 0x280 ... 0x2bf: /* NVIC Clear pend */
975         val = 0;
976         startvec = offset - 0x280 + NVIC_FIRST_IRQ; /* vector # */
977         for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
978             if (s->vectors[startvec + i].pending) {
979                 val |= (1 << i);
980             }
981         }
982         break;
983     case 0x300 ... 0x33f: /* NVIC Active */
984         val = 0;
985         startvec = offset - 0x300 + NVIC_FIRST_IRQ; /* vector # */
986
987         for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
988             if (s->vectors[startvec + i].active) {
989                 val |= (1 << i);
990             }
991         }
992         break;
993     case 0x400 ... 0x5ef: /* NVIC Priority */
994         val = 0;
995         startvec = offset - 0x400 + NVIC_FIRST_IRQ; /* vector # */
996
997         for (i = 0; i < size && startvec + i < s->num_irq; i++) {
998             val |= s->vectors[startvec + i].prio << (8 * i);
999         }
1000         break;
1001     case 0xd18 ... 0xd23: /* System Handler Priority.  */
1002         val = 0;
1003         for (i = 0; i < size; i++) {
1004             val |= s->vectors[(offset - 0xd14) + i].prio << (i * 8);
1005         }
1006         break;
1007     case 0xfe0 ... 0xfff: /* ID.  */
1008         if (offset & 3) {
1009             val = 0;
1010         } else {
1011             val = nvic_id[(offset - 0xfe0) >> 2];
1012         }
1013         break;
1014     default:
1015         if (size == 4) {
1016             val = nvic_readl(s, offset, attrs);
1017         } else {
1018             qemu_log_mask(LOG_GUEST_ERROR,
1019                           "NVIC: Bad read of size %d at offset 0x%x\n",
1020                           size, offset);
1021             val = 0;
1022         }
1023     }
1024
1025     trace_nvic_sysreg_read(addr, val, size);
1026     *data = val;
1027     return MEMTX_OK;
1028 }
1029
1030 static MemTxResult nvic_sysreg_write(void *opaque, hwaddr addr,
1031                                      uint64_t value, unsigned size,
1032                                      MemTxAttrs attrs)
1033 {
1034     NVICState *s = (NVICState *)opaque;
1035     uint32_t offset = addr;
1036     unsigned i, startvec, end;
1037     unsigned setval = 0;
1038
1039     trace_nvic_sysreg_write(addr, value, size);
1040
1041     if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) {
1042         /* Generate BusFault for unprivileged accesses */
1043         return MEMTX_ERROR;
1044     }
1045
1046     switch (offset) {
1047     case 0x100 ... 0x13f: /* NVIC Set enable */
1048         offset += 0x80;
1049         setval = 1;
1050         /* fall through */
1051     case 0x180 ... 0x1bf: /* NVIC Clear enable */
1052         startvec = 8 * (offset - 0x180) + NVIC_FIRST_IRQ;
1053
1054         for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
1055             if (value & (1 << i)) {
1056                 s->vectors[startvec + i].enabled = setval;
1057             }
1058         }
1059         nvic_irq_update(s);
1060         return MEMTX_OK;
1061     case 0x200 ... 0x23f: /* NVIC Set pend */
1062         /* the special logic in armv7m_nvic_set_pending()
1063          * is not needed since IRQs are never escalated
1064          */
1065         offset += 0x80;
1066         setval = 1;
1067         /* fall through */
1068     case 0x280 ... 0x2bf: /* NVIC Clear pend */
1069         startvec = 8 * (offset - 0x280) + NVIC_FIRST_IRQ; /* vector # */
1070
1071         for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
1072             if (value & (1 << i)) {
1073                 s->vectors[startvec + i].pending = setval;
1074             }
1075         }
1076         nvic_irq_update(s);
1077         return MEMTX_OK;
1078     case 0x300 ... 0x33f: /* NVIC Active */
1079         return MEMTX_OK; /* R/O */
1080     case 0x400 ... 0x5ef: /* NVIC Priority */
1081         startvec = 8 * (offset - 0x400) + NVIC_FIRST_IRQ; /* vector # */
1082
1083         for (i = 0; i < size && startvec + i < s->num_irq; i++) {
1084             set_prio(s, startvec + i, (value >> (i * 8)) & 0xff);
1085         }
1086         nvic_irq_update(s);
1087         return MEMTX_OK;
1088     case 0xd18 ... 0xd23: /* System Handler Priority.  */
1089         for (i = 0; i < size; i++) {
1090             unsigned hdlidx = (offset - 0xd14) + i;
1091             set_prio(s, hdlidx, (value >> (i * 8)) & 0xff);
1092         }
1093         nvic_irq_update(s);
1094         return MEMTX_OK;
1095     }
1096     if (size == 4) {
1097         nvic_writel(s, offset, value, attrs);
1098         return MEMTX_OK;
1099     }
1100     qemu_log_mask(LOG_GUEST_ERROR,
1101                   "NVIC: Bad write of size %d at offset 0x%x\n", size, offset);
1102     /* This is UNPREDICTABLE; treat as RAZ/WI */
1103     return MEMTX_OK;
1104 }
1105
1106 static const MemoryRegionOps nvic_sysreg_ops = {
1107     .read_with_attrs = nvic_sysreg_read,
1108     .write_with_attrs = nvic_sysreg_write,
1109     .endianness = DEVICE_NATIVE_ENDIAN,
1110 };
1111
1112 static MemTxResult nvic_sysreg_ns_write(void *opaque, hwaddr addr,
1113                                         uint64_t value, unsigned size,
1114                                         MemTxAttrs attrs)
1115 {
1116     if (attrs.secure) {
1117         /* S accesses to the alias act like NS accesses to the real region */
1118         attrs.secure = 0;
1119         return nvic_sysreg_write(opaque, addr, value, size, attrs);
1120     } else {
1121         /* NS attrs are RAZ/WI for privileged, and BusFault for user */
1122         if (attrs.user) {
1123             return MEMTX_ERROR;
1124         }
1125         return MEMTX_OK;
1126     }
1127 }
1128
1129 static MemTxResult nvic_sysreg_ns_read(void *opaque, hwaddr addr,
1130                                        uint64_t *data, unsigned size,
1131                                        MemTxAttrs attrs)
1132 {
1133     if (attrs.secure) {
1134         /* S accesses to the alias act like NS accesses to the real region */
1135         attrs.secure = 0;
1136         return nvic_sysreg_read(opaque, addr, data, size, attrs);
1137     } else {
1138         /* NS attrs are RAZ/WI for privileged, and BusFault for user */
1139         if (attrs.user) {
1140             return MEMTX_ERROR;
1141         }
1142         *data = 0;
1143         return MEMTX_OK;
1144     }
1145 }
1146
1147 static const MemoryRegionOps nvic_sysreg_ns_ops = {
1148     .read_with_attrs = nvic_sysreg_ns_read,
1149     .write_with_attrs = nvic_sysreg_ns_write,
1150     .endianness = DEVICE_NATIVE_ENDIAN,
1151 };
1152
1153 static int nvic_post_load(void *opaque, int version_id)
1154 {
1155     NVICState *s = opaque;
1156     unsigned i;
1157
1158     /* Check for out of range priority settings */
1159     if (s->vectors[ARMV7M_EXCP_RESET].prio != -3 ||
1160         s->vectors[ARMV7M_EXCP_NMI].prio != -2 ||
1161         s->vectors[ARMV7M_EXCP_HARD].prio != -1) {
1162         return 1;
1163     }
1164     for (i = ARMV7M_EXCP_MEM; i < s->num_irq; i++) {
1165         if (s->vectors[i].prio & ~0xff) {
1166             return 1;
1167         }
1168     }
1169
1170     nvic_recompute_state(s);
1171
1172     return 0;
1173 }
1174
1175 static const VMStateDescription vmstate_VecInfo = {
1176     .name = "armv7m_nvic_info",
1177     .version_id = 1,
1178     .minimum_version_id = 1,
1179     .fields = (VMStateField[]) {
1180         VMSTATE_INT16(prio, VecInfo),
1181         VMSTATE_UINT8(enabled, VecInfo),
1182         VMSTATE_UINT8(pending, VecInfo),
1183         VMSTATE_UINT8(active, VecInfo),
1184         VMSTATE_UINT8(level, VecInfo),
1185         VMSTATE_END_OF_LIST()
1186     }
1187 };
1188
1189 static bool nvic_security_needed(void *opaque)
1190 {
1191     NVICState *s = opaque;
1192
1193     return arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY);
1194 }
1195
1196 static int nvic_security_post_load(void *opaque, int version_id)
1197 {
1198     NVICState *s = opaque;
1199     int i;
1200
1201     /* Check for out of range priority settings */
1202     if (s->sec_vectors[ARMV7M_EXCP_HARD].prio != -1) {
1203         return 1;
1204     }
1205     for (i = ARMV7M_EXCP_MEM; i < ARRAY_SIZE(s->sec_vectors); i++) {
1206         if (s->sec_vectors[i].prio & ~0xff) {
1207             return 1;
1208         }
1209     }
1210     return 0;
1211 }
1212
1213 static const VMStateDescription vmstate_nvic_security = {
1214     .name = "nvic/m-security",
1215     .version_id = 1,
1216     .minimum_version_id = 1,
1217     .needed = nvic_security_needed,
1218     .post_load = &nvic_security_post_load,
1219     .fields = (VMStateField[]) {
1220         VMSTATE_STRUCT_ARRAY(sec_vectors, NVICState, NVIC_INTERNAL_VECTORS, 1,
1221                              vmstate_VecInfo, VecInfo),
1222         VMSTATE_UINT32(prigroup[M_REG_S], NVICState),
1223         VMSTATE_END_OF_LIST()
1224     }
1225 };
1226
1227 static const VMStateDescription vmstate_nvic = {
1228     .name = "armv7m_nvic",
1229     .version_id = 4,
1230     .minimum_version_id = 4,
1231     .post_load = &nvic_post_load,
1232     .fields = (VMStateField[]) {
1233         VMSTATE_STRUCT_ARRAY(vectors, NVICState, NVIC_MAX_VECTORS, 1,
1234                              vmstate_VecInfo, VecInfo),
1235         VMSTATE_UINT32(prigroup[M_REG_NS], NVICState),
1236         VMSTATE_END_OF_LIST()
1237     },
1238     .subsections = (const VMStateDescription*[]) {
1239         &vmstate_nvic_security,
1240         NULL
1241     }
1242 };
1243
1244 static Property props_nvic[] = {
1245     /* Number of external IRQ lines (so excluding the 16 internal exceptions) */
1246     DEFINE_PROP_UINT32("num-irq", NVICState, num_irq, 64),
1247     DEFINE_PROP_END_OF_LIST()
1248 };
1249
1250 static void armv7m_nvic_reset(DeviceState *dev)
1251 {
1252     NVICState *s = NVIC(dev);
1253
1254     s->vectors[ARMV7M_EXCP_NMI].enabled = 1;
1255     s->vectors[ARMV7M_EXCP_HARD].enabled = 1;
1256     /* MEM, BUS, and USAGE are enabled through
1257      * the System Handler Control register
1258      */
1259     s->vectors[ARMV7M_EXCP_SVC].enabled = 1;
1260     s->vectors[ARMV7M_EXCP_DEBUG].enabled = 1;
1261     s->vectors[ARMV7M_EXCP_PENDSV].enabled = 1;
1262     s->vectors[ARMV7M_EXCP_SYSTICK].enabled = 1;
1263
1264     s->vectors[ARMV7M_EXCP_RESET].prio = -3;
1265     s->vectors[ARMV7M_EXCP_NMI].prio = -2;
1266     s->vectors[ARMV7M_EXCP_HARD].prio = -1;
1267
1268     if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
1269         s->sec_vectors[ARMV7M_EXCP_HARD].enabled = 1;
1270         s->sec_vectors[ARMV7M_EXCP_SVC].enabled = 1;
1271         s->sec_vectors[ARMV7M_EXCP_PENDSV].enabled = 1;
1272         s->sec_vectors[ARMV7M_EXCP_SYSTICK].enabled = 1;
1273
1274         /* AIRCR.BFHFNMINS resets to 0 so Secure HF is priority -1 (R_CMTC) */
1275         s->sec_vectors[ARMV7M_EXCP_HARD].prio = -1;
1276     }
1277
1278     /* Strictly speaking the reset handler should be enabled.
1279      * However, we don't simulate soft resets through the NVIC,
1280      * and the reset vector should never be pended.
1281      * So we leave it disabled to catch logic errors.
1282      */
1283
1284     s->exception_prio = NVIC_NOEXC_PRIO;
1285     s->vectpending = 0;
1286     s->vectpending_is_s_banked = false;
1287     s->vectpending_prio = NVIC_NOEXC_PRIO;
1288 }
1289
1290 static void nvic_systick_trigger(void *opaque, int n, int level)
1291 {
1292     NVICState *s = opaque;
1293
1294     if (level) {
1295         /* SysTick just asked us to pend its exception.
1296          * (This is different from an external interrupt line's
1297          * behaviour.)
1298          */
1299         armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK);
1300     }
1301 }
1302
1303 static void armv7m_nvic_realize(DeviceState *dev, Error **errp)
1304 {
1305     NVICState *s = NVIC(dev);
1306     SysBusDevice *systick_sbd;
1307     Error *err = NULL;
1308     int regionlen;
1309
1310     s->cpu = ARM_CPU(qemu_get_cpu(0));
1311     assert(s->cpu);
1312
1313     if (s->num_irq > NVIC_MAX_IRQ) {
1314         error_setg(errp, "num-irq %d exceeds NVIC maximum", s->num_irq);
1315         return;
1316     }
1317
1318     qdev_init_gpio_in(dev, set_irq_level, s->num_irq);
1319
1320     /* include space for internal exception vectors */
1321     s->num_irq += NVIC_FIRST_IRQ;
1322
1323     object_property_set_bool(OBJECT(&s->systick), true, "realized", &err);
1324     if (err != NULL) {
1325         error_propagate(errp, err);
1326         return;
1327     }
1328     systick_sbd = SYS_BUS_DEVICE(&s->systick);
1329     sysbus_connect_irq(systick_sbd, 0,
1330                        qdev_get_gpio_in_named(dev, "systick-trigger", 0));
1331
1332     /* The NVIC and System Control Space (SCS) starts at 0xe000e000
1333      * and looks like this:
1334      *  0x004 - ICTR
1335      *  0x010 - 0xff - systick
1336      *  0x100..0x7ec - NVIC
1337      *  0x7f0..0xcff - Reserved
1338      *  0xd00..0xd3c - SCS registers
1339      *  0xd40..0xeff - Reserved or Not implemented
1340      *  0xf00 - STIR
1341      *
1342      * Some registers within this space are banked between security states.
1343      * In v8M there is a second range 0xe002e000..0xe002efff which is the
1344      * NonSecure alias SCS; secure accesses to this behave like NS accesses
1345      * to the main SCS range, and non-secure accesses (including when
1346      * the security extension is not implemented) are RAZ/WI.
1347      * Note that both the main SCS range and the alias range are defined
1348      * to be exempt from memory attribution (R_BLJT) and so the memory
1349      * transaction attribute always matches the current CPU security
1350      * state (attrs.secure == env->v7m.secure). In the nvic_sysreg_ns_ops
1351      * wrappers we change attrs.secure to indicate the NS access; so
1352      * generally code determining which banked register to use should
1353      * use attrs.secure; code determining actual behaviour of the system
1354      * should use env->v7m.secure.
1355      */
1356     regionlen = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? 0x21000 : 0x1000;
1357     memory_region_init(&s->container, OBJECT(s), "nvic", regionlen);
1358     /* The system register region goes at the bottom of the priority
1359      * stack as it covers the whole page.
1360      */
1361     memory_region_init_io(&s->sysregmem, OBJECT(s), &nvic_sysreg_ops, s,
1362                           "nvic_sysregs", 0x1000);
1363     memory_region_add_subregion(&s->container, 0, &s->sysregmem);
1364     memory_region_add_subregion_overlap(&s->container, 0x10,
1365                                         sysbus_mmio_get_region(systick_sbd, 0),
1366                                         1);
1367
1368     if (arm_feature(&s->cpu->env, ARM_FEATURE_V8)) {
1369         memory_region_init_io(&s->sysreg_ns_mem, OBJECT(s),
1370                               &nvic_sysreg_ns_ops, s,
1371                               "nvic_sysregs_ns", 0x1000);
1372         memory_region_add_subregion(&s->container, 0x20000, &s->sysreg_ns_mem);
1373     }
1374
1375     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->container);
1376 }
1377
1378 static void armv7m_nvic_instance_init(Object *obj)
1379 {
1380     /* We have a different default value for the num-irq property
1381      * than our superclass. This function runs after qdev init
1382      * has set the defaults from the Property array and before
1383      * any user-specified property setting, so just modify the
1384      * value in the GICState struct.
1385      */
1386     DeviceState *dev = DEVICE(obj);
1387     NVICState *nvic = NVIC(obj);
1388     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
1389
1390     object_initialize(&nvic->systick, sizeof(nvic->systick), TYPE_SYSTICK);
1391     qdev_set_parent_bus(DEVICE(&nvic->systick), sysbus_get_default());
1392
1393     sysbus_init_irq(sbd, &nvic->excpout);
1394     qdev_init_gpio_out_named(dev, &nvic->sysresetreq, "SYSRESETREQ", 1);
1395     qdev_init_gpio_in_named(dev, nvic_systick_trigger, "systick-trigger", 1);
1396 }
1397
1398 static void armv7m_nvic_class_init(ObjectClass *klass, void *data)
1399 {
1400     DeviceClass *dc = DEVICE_CLASS(klass);
1401
1402     dc->vmsd  = &vmstate_nvic;
1403     dc->props = props_nvic;
1404     dc->reset = armv7m_nvic_reset;
1405     dc->realize = armv7m_nvic_realize;
1406 }
1407
1408 static const TypeInfo armv7m_nvic_info = {
1409     .name          = TYPE_NVIC,
1410     .parent        = TYPE_SYS_BUS_DEVICE,
1411     .instance_init = armv7m_nvic_instance_init,
1412     .instance_size = sizeof(NVICState),
1413     .class_init    = armv7m_nvic_class_init,
1414     .class_size    = sizeof(SysBusDeviceClass),
1415 };
1416
1417 static void armv7m_nvic_register_types(void)
1418 {
1419     type_register_static(&armv7m_nvic_info);
1420 }
1421
1422 type_init(armv7m_nvic_register_types)
This page took 0.106034 seconds and 4 git commands to generate.