]> Git Repo - qemu.git/blame - hw/intc/armv7m_nvic.c
hw/intc/arm_gicv3: Fix secure-GIC NS ICC_PMR and ICC_RPR accesses
[qemu.git] / hw / intc / armv7m_nvic.c
CommitLineData
9ee6e8bb
PB
1/*
2 * ARM Nested Vectored Interrupt Controller
3 *
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
6 *
8e31bf38 7 * This code is licensed under the GPL.
9ee6e8bb
PB
8 *
9 * The ARMv7M System controller is fairly tightly tied in with the
10 * NVIC. Much of that is also implemented here.
11 */
12
8ef94f0b 13#include "qemu/osdep.h"
da34e65c 14#include "qapi/error.h"
4771d756 15#include "qemu-common.h"
33c11879 16#include "cpu.h"
83c9f4ca 17#include "hw/sysbus.h"
1de7afc9 18#include "qemu/timer.h"
bd2be150 19#include "hw/arm/arm.h"
d2db1de6 20#include "hw/intc/armv7m_nvic.h"
da6d674e 21#include "target/arm/cpu.h"
29c483a5 22#include "exec/exec-all.h"
03dd024f 23#include "qemu/log.h"
da6d674e
MD
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 */
17906a16 50#define NVIC_FIRST_IRQ NVIC_INTERNAL_VECTORS
da6d674e
MD
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
ff96c64a
PM
57/* Maximum priority of non-secure exceptions when AIRCR.PRIS is set */
58#define NVIC_NS_PRIO_LIMIT 0x80
da6d674e 59
2a29ddee
PM
60static const uint8_t nvic_id[] = {
61 0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1
62};
63
da6d674e
MD
64static int nvic_pending_prio(NVICState *s)
65{
5255fcf8 66 /* return the group priority of the current pending interrupt,
da6d674e
MD
67 * or NVIC_NOEXC_PRIO if no interrupt is pending
68 */
5255fcf8 69 return s->vectpending_prio;
da6d674e
MD
70}
71
72/* Return the value of the ISCR RETTOBASE bit:
73 * 1 if there is exactly one active exception
74 * 0 if there is more than one active exception
75 * UNKNOWN if there are no active exceptions (we choose 1,
76 * which matches the choice Cortex-M3 is documented as making).
77 *
78 * NB: some versions of the documentation talk about this
79 * counting "active exceptions other than the one shown by IPSR";
80 * this is only different in the obscure corner case where guest
81 * code has manually deactivated an exception and is about
82 * to fail an exception-return integrity check. The definition
83 * above is the one from the v8M ARM ARM and is also in line
84 * with the behaviour documented for the Cortex-M3.
85 */
86static bool nvic_rettobase(NVICState *s)
87{
88 int irq, nhand = 0;
028b0da4 89 bool check_sec = arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY);
da6d674e
MD
90
91 for (irq = ARMV7M_EXCP_RESET; irq < s->num_irq; irq++) {
028b0da4
PM
92 if (s->vectors[irq].active ||
93 (check_sec && irq < NVIC_INTERNAL_VECTORS &&
94 s->sec_vectors[irq].active)) {
da6d674e
MD
95 nhand++;
96 if (nhand == 2) {
97 return 0;
98 }
99 }
100 }
101
102 return 1;
103}
104
105/* Return the value of the ISCR ISRPENDING bit:
106 * 1 if an external interrupt is pending
107 * 0 if no external interrupt is pending
108 */
109static bool nvic_isrpending(NVICState *s)
110{
111 int irq;
112
113 /* We can shortcut if the highest priority pending interrupt
114 * happens to be external or if there is nothing pending.
115 */
116 if (s->vectpending > NVIC_FIRST_IRQ) {
117 return true;
118 }
119 if (s->vectpending == 0) {
120 return false;
121 }
122
123 for (irq = NVIC_FIRST_IRQ; irq < s->num_irq; irq++) {
124 if (s->vectors[irq].pending) {
125 return true;
126 }
127 }
128 return false;
129}
130
ff96c64a
PM
131static bool exc_is_banked(int exc)
132{
133 /* Return true if this is one of the limited set of exceptions which
134 * are banked (and thus have state in sec_vectors[])
135 */
136 return exc == ARMV7M_EXCP_HARD ||
137 exc == ARMV7M_EXCP_MEM ||
138 exc == ARMV7M_EXCP_USAGE ||
139 exc == ARMV7M_EXCP_SVC ||
140 exc == ARMV7M_EXCP_PENDSV ||
141 exc == ARMV7M_EXCP_SYSTICK;
142}
143
da6d674e
MD
144/* Return a mask word which clears the subpriority bits from
145 * a priority value for an M-profile exception, leaving only
146 * the group priority.
147 */
ff96c64a 148static inline uint32_t nvic_gprio_mask(NVICState *s, bool secure)
da6d674e 149{
ff96c64a
PM
150 return ~0U << (s->prigroup[secure] + 1);
151}
152
153static bool exc_targets_secure(NVICState *s, int exc)
154{
155 /* Return true if this non-banked exception targets Secure state. */
156 if (!arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
157 return false;
158 }
159
160 if (exc >= NVIC_FIRST_IRQ) {
161 return !s->itns[exc];
162 }
163
164 /* Function shouldn't be called for banked exceptions. */
165 assert(!exc_is_banked(exc));
166
167 switch (exc) {
168 case ARMV7M_EXCP_NMI:
169 case ARMV7M_EXCP_BUS:
170 return !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK);
171 case ARMV7M_EXCP_SECURE:
172 return true;
173 case ARMV7M_EXCP_DEBUG:
174 /* TODO: controlled by DEMCR.SDME, which we don't yet implement */
175 return false;
176 default:
177 /* reset, and reserved (unused) low exception numbers.
178 * We'll get called by code that loops through all the exception
179 * numbers, but it doesn't matter what we return here as these
180 * non-existent exceptions will never be pended or active.
181 */
182 return true;
183 }
184}
185
186static int exc_group_prio(NVICState *s, int rawprio, bool targets_secure)
187{
188 /* Return the group priority for this exception, given its raw
189 * (group-and-subgroup) priority value and whether it is targeting
190 * secure state or not.
191 */
192 if (rawprio < 0) {
193 return rawprio;
194 }
195 rawprio &= nvic_gprio_mask(s, targets_secure);
196 /* AIRCR.PRIS causes us to squash all NS priorities into the
197 * lower half of the total range
198 */
199 if (!targets_secure &&
200 (s->cpu->env.v7m.aircr & R_V7M_AIRCR_PRIS_MASK)) {
201 rawprio = (rawprio >> 1) + NVIC_NS_PRIO_LIMIT;
202 }
203 return rawprio;
204}
205
206/* Recompute vectpending and exception_prio for a CPU which implements
207 * the Security extension
208 */
209static void nvic_recompute_state_secure(NVICState *s)
210{
211 int i, bank;
212 int pend_prio = NVIC_NOEXC_PRIO;
213 int active_prio = NVIC_NOEXC_PRIO;
214 int pend_irq = 0;
215 bool pending_is_s_banked = false;
216
217 /* R_CQRV: precedence is by:
218 * - lowest group priority; if both the same then
219 * - lowest subpriority; if both the same then
220 * - lowest exception number; if both the same (ie banked) then
221 * - secure exception takes precedence
222 * Compare pseudocode RawExecutionPriority.
223 * Annoyingly, now we have two prigroup values (for S and NS)
224 * we can't do the loop comparison on raw priority values.
225 */
226 for (i = 1; i < s->num_irq; i++) {
227 for (bank = M_REG_S; bank >= M_REG_NS; bank--) {
228 VecInfo *vec;
229 int prio;
230 bool targets_secure;
231
232 if (bank == M_REG_S) {
233 if (!exc_is_banked(i)) {
234 continue;
235 }
236 vec = &s->sec_vectors[i];
237 targets_secure = true;
238 } else {
239 vec = &s->vectors[i];
240 targets_secure = !exc_is_banked(i) && exc_targets_secure(s, i);
241 }
242
243 prio = exc_group_prio(s, vec->prio, targets_secure);
244 if (vec->enabled && vec->pending && prio < pend_prio) {
245 pend_prio = prio;
246 pend_irq = i;
247 pending_is_s_banked = (bank == M_REG_S);
248 }
249 if (vec->active && prio < active_prio) {
250 active_prio = prio;
251 }
252 }
253 }
254
255 s->vectpending_is_s_banked = pending_is_s_banked;
256 s->vectpending = pend_irq;
257 s->vectpending_prio = pend_prio;
258 s->exception_prio = active_prio;
259
260 trace_nvic_recompute_state_secure(s->vectpending,
261 s->vectpending_is_s_banked,
262 s->vectpending_prio,
263 s->exception_prio);
da6d674e
MD
264}
265
266/* Recompute vectpending and exception_prio */
267static void nvic_recompute_state(NVICState *s)
268{
269 int i;
270 int pend_prio = NVIC_NOEXC_PRIO;
271 int active_prio = NVIC_NOEXC_PRIO;
272 int pend_irq = 0;
273
ff96c64a
PM
274 /* In theory we could write one function that handled both
275 * the "security extension present" and "not present"; however
276 * the security related changes significantly complicate the
277 * recomputation just by themselves and mixing both cases together
278 * would be even worse, so we retain a separate non-secure-only
279 * version for CPUs which don't implement the security extension.
280 */
281 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
282 nvic_recompute_state_secure(s);
283 return;
284 }
285
da6d674e
MD
286 for (i = 1; i < s->num_irq; i++) {
287 VecInfo *vec = &s->vectors[i];
288
289 if (vec->enabled && vec->pending && vec->prio < pend_prio) {
290 pend_prio = vec->prio;
291 pend_irq = i;
292 }
293 if (vec->active && vec->prio < active_prio) {
294 active_prio = vec->prio;
295 }
296 }
297
22a9c26a 298 if (active_prio > 0) {
ff96c64a 299 active_prio &= nvic_gprio_mask(s, false);
22a9c26a
PM
300 }
301
5255fcf8 302 if (pend_prio > 0) {
ff96c64a 303 pend_prio &= nvic_gprio_mask(s, false);
5255fcf8
PM
304 }
305
da6d674e 306 s->vectpending = pend_irq;
5255fcf8 307 s->vectpending_prio = pend_prio;
22a9c26a 308 s->exception_prio = active_prio;
da6d674e 309
5255fcf8
PM
310 trace_nvic_recompute_state(s->vectpending,
311 s->vectpending_prio,
312 s->exception_prio);
da6d674e
MD
313}
314
315/* Return the current execution priority of the CPU
316 * (equivalent to the pseudocode ExecutionPriority function).
317 * This is a value between -2 (NMI priority) and NVIC_NOEXC_PRIO.
318 */
319static inline int nvic_exec_prio(NVICState *s)
320{
321 CPUARMState *env = &s->cpu->env;
49c80c38 322 int running = NVIC_NOEXC_PRIO;
da6d674e 323
49c80c38
PM
324 if (env->v7m.basepri[M_REG_NS] > 0) {
325 running = exc_group_prio(s, env->v7m.basepri[M_REG_NS], M_REG_NS);
326 }
327
328 if (env->v7m.basepri[M_REG_S] > 0) {
329 int basepri = exc_group_prio(s, env->v7m.basepri[M_REG_S], M_REG_S);
330 if (running > basepri) {
331 running = basepri;
332 }
333 }
334
335 if (env->v7m.primask[M_REG_NS]) {
336 if (env->v7m.aircr & R_V7M_AIRCR_PRIS_MASK) {
337 if (running > NVIC_NS_PRIO_LIMIT) {
338 running = NVIC_NS_PRIO_LIMIT;
339 }
340 } else {
341 running = 0;
342 }
343 }
344
345 if (env->v7m.primask[M_REG_S]) {
da6d674e 346 running = 0;
da6d674e 347 }
49c80c38
PM
348
349 if (env->v7m.faultmask[M_REG_NS]) {
350 if (env->v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) {
351 running = -1;
352 } else {
353 if (env->v7m.aircr & R_V7M_AIRCR_PRIS_MASK) {
354 if (running > NVIC_NS_PRIO_LIMIT) {
355 running = NVIC_NS_PRIO_LIMIT;
356 }
357 } else {
358 running = 0;
359 }
360 }
361 }
362
363 if (env->v7m.faultmask[M_REG_S]) {
364 running = (env->v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) ? -3 : -1;
365 }
366
da6d674e
MD
367 /* consider priority of active handler */
368 return MIN(running, s->exception_prio);
369}
370
5d479199
PM
371bool armv7m_nvic_neg_prio_requested(void *opaque, bool secure)
372{
373 /* Return true if the requested execution priority is negative
374 * for the specified security state, ie that security state
375 * has an active NMI or HardFault or has set its FAULTMASK.
376 * Note that this is not the same as whether the execution
377 * priority is actually negative (for instance AIRCR.PRIS may
378 * mean we don't allow FAULTMASK_NS to actually make the execution
379 * priority negative). Compare pseudocode IsReqExcPriNeg().
380 */
381 NVICState *s = opaque;
382
383 if (s->cpu->env.v7m.faultmask[secure]) {
384 return true;
385 }
386
387 if (secure ? s->sec_vectors[ARMV7M_EXCP_HARD].active :
388 s->vectors[ARMV7M_EXCP_HARD].active) {
389 return true;
390 }
391
392 if (s->vectors[ARMV7M_EXCP_NMI].active &&
393 exc_targets_secure(s, ARMV7M_EXCP_NMI) == secure) {
394 return true;
395 }
396
397 return false;
398}
399
7ecdaa4a
PM
400bool armv7m_nvic_can_take_pending_exception(void *opaque)
401{
402 NVICState *s = opaque;
403
404 return nvic_exec_prio(s) > nvic_pending_prio(s);
405}
406
42a6686b
PM
407int armv7m_nvic_raw_execution_priority(void *opaque)
408{
409 NVICState *s = opaque;
410
411 return s->exception_prio;
412}
413
e6a0d350
PM
414/* caller must call nvic_irq_update() after this.
415 * secure indicates the bank to use for banked exceptions (we assert if
416 * we are passed secure=true for a non-banked exception).
417 */
418static void set_prio(NVICState *s, unsigned irq, bool secure, uint8_t prio)
da6d674e
MD
419{
420 assert(irq > ARMV7M_EXCP_NMI); /* only use for configurable prios */
421 assert(irq < s->num_irq);
422
e6a0d350
PM
423 if (secure) {
424 assert(exc_is_banked(irq));
425 s->sec_vectors[irq].prio = prio;
426 } else {
427 s->vectors[irq].prio = prio;
428 }
429
430 trace_nvic_set_prio(irq, secure, prio);
431}
432
433/* Return the current raw priority register value.
434 * secure indicates the bank to use for banked exceptions (we assert if
435 * we are passed secure=true for a non-banked exception).
436 */
437static int get_prio(NVICState *s, unsigned irq, bool secure)
438{
439 assert(irq > ARMV7M_EXCP_NMI); /* only use for configurable prios */
440 assert(irq < s->num_irq);
da6d674e 441
e6a0d350
PM
442 if (secure) {
443 assert(exc_is_banked(irq));
444 return s->sec_vectors[irq].prio;
445 } else {
446 return s->vectors[irq].prio;
447 }
da6d674e
MD
448}
449
450/* Recompute state and assert irq line accordingly.
451 * Must be called after changes to:
452 * vec->active, vec->enabled, vec->pending or vec->prio for any vector
453 * prigroup
454 */
455static void nvic_irq_update(NVICState *s)
456{
457 int lvl;
458 int pend_prio;
459
460 nvic_recompute_state(s);
461 pend_prio = nvic_pending_prio(s);
462
463 /* Raise NVIC output if this IRQ would be taken, except that we
464 * ignore the effects of the BASEPRI, FAULTMASK and PRIMASK (which
465 * will be checked for in arm_v7m_cpu_exec_interrupt()); changes
466 * to those CPU registers don't cause us to recalculate the NVIC
467 * pending info.
468 */
469 lvl = (pend_prio < s->exception_prio);
470 trace_nvic_irq_update(s->vectpending, pend_prio, s->exception_prio, lvl);
471 qemu_set_irq(s->excpout, lvl);
472}
473
2fb50a33
PM
474/**
475 * armv7m_nvic_clear_pending: mark the specified exception as not pending
476 * @opaque: the NVIC
477 * @irq: the exception number to mark as not pending
478 * @secure: false for non-banked exceptions or for the nonsecure
479 * version of a banked exception, true for the secure version of a banked
480 * exception.
481 *
482 * Marks the specified exception as not pending. Note that we will assert()
483 * if @secure is true and @irq does not specify one of the fixed set
484 * of architecturally banked exceptions.
485 */
486static void armv7m_nvic_clear_pending(void *opaque, int irq, bool secure)
da6d674e
MD
487{
488 NVICState *s = (NVICState *)opaque;
489 VecInfo *vec;
490
491 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
492
2fb50a33
PM
493 if (secure) {
494 assert(exc_is_banked(irq));
495 vec = &s->sec_vectors[irq];
496 } else {
497 vec = &s->vectors[irq];
498 }
499 trace_nvic_clear_pending(irq, secure, vec->enabled, vec->prio);
da6d674e
MD
500 if (vec->pending) {
501 vec->pending = 0;
502 nvic_irq_update(s);
503 }
504}
505
5ede82b8
PM
506static void do_armv7m_nvic_set_pending(void *opaque, int irq, bool secure,
507 bool derived)
9ee6e8bb 508{
5ede82b8
PM
509 /* Pend an exception, including possibly escalating it to HardFault.
510 *
511 * This function handles both "normal" pending of interrupts and
512 * exceptions, and also derived exceptions (ones which occur as
513 * a result of trying to take some other exception).
514 *
515 * If derived == true, the caller guarantees that we are part way through
516 * trying to take an exception (but have not yet called
517 * armv7m_nvic_acknowledge_irq() to make it active), and so:
518 * - s->vectpending is the "original exception" we were trying to take
519 * - irq is the "derived exception"
520 * - nvic_exec_prio(s) gives the priority before exception entry
521 * Here we handle the prioritization logic which the pseudocode puts
522 * in the DerivedLateArrival() function.
523 */
524
f797c075 525 NVICState *s = (NVICState *)opaque;
2fb50a33 526 bool banked = exc_is_banked(irq);
da6d674e
MD
527 VecInfo *vec;
528
529 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
2fb50a33 530 assert(!secure || banked);
da6d674e 531
2fb50a33 532 vec = (banked && secure) ? &s->sec_vectors[irq] : &s->vectors[irq];
a73c98e1 533
5ede82b8
PM
534 trace_nvic_set_pending(irq, secure, derived, vec->enabled, vec->prio);
535
536 if (derived) {
537 /* Derived exceptions are always synchronous. */
538 assert(irq >= ARMV7M_EXCP_HARD && irq < ARMV7M_EXCP_PENDSV);
539
540 if (irq == ARMV7M_EXCP_DEBUG &&
541 exc_group_prio(s, vec->prio, secure) >= nvic_exec_prio(s)) {
542 /* DebugMonitorFault, but its priority is lower than the
543 * preempted exception priority: just ignore it.
544 */
545 return;
546 }
547
548 if (irq == ARMV7M_EXCP_HARD && vec->prio >= s->vectpending_prio) {
549 /* If this is a terminal exception (one which means we cannot
550 * take the original exception, like a failure to read its
551 * vector table entry), then we must take the derived exception.
552 * If the derived exception can't take priority over the
553 * original exception, then we go into Lockup.
554 *
555 * For QEMU, we rely on the fact that a derived exception is
556 * terminal if and only if it's reported to us as HardFault,
557 * which saves having to have an extra argument is_terminal
558 * that we'd only use in one place.
559 */
560 cpu_abort(&s->cpu->parent_obj,
561 "Lockup: can't take terminal derived exception "
562 "(original exception priority %d)\n",
563 s->vectpending_prio);
564 }
565 /* We now continue with the same code as for a normal pending
566 * exception, which will cause us to pend the derived exception.
567 * We'll then take either the original or the derived exception
568 * based on which is higher priority by the usual mechanism
569 * for selecting the highest priority pending interrupt.
570 */
571 }
a73c98e1
MD
572
573 if (irq >= ARMV7M_EXCP_HARD && irq < ARMV7M_EXCP_PENDSV) {
574 /* If a synchronous exception is pending then it may be
575 * escalated to HardFault if:
576 * * it is equal or lower priority to current execution
577 * * it is disabled
578 * (ie we need to take it immediately but we can't do so).
579 * Asynchronous exceptions (and interrupts) simply remain pending.
580 *
581 * For QEMU, we don't have any imprecise (asynchronous) faults,
582 * so we can assume that PREFETCH_ABORT and DATA_ABORT are always
583 * synchronous.
584 * Debug exceptions are awkward because only Debug exceptions
585 * resulting from the BKPT instruction should be escalated,
586 * but we don't currently implement any Debug exceptions other
587 * than those that result from BKPT, so we treat all debug exceptions
588 * as needing escalation.
589 *
590 * This all means we can identify whether to escalate based only on
591 * the exception number and don't (yet) need the caller to explicitly
592 * tell us whether this exception is synchronous or not.
593 */
594 int running = nvic_exec_prio(s);
595 bool escalate = false;
596
80ac2390 597 if (exc_group_prio(s, vec->prio, secure) >= running) {
a73c98e1
MD
598 trace_nvic_escalate_prio(irq, vec->prio, running);
599 escalate = true;
600 } else if (!vec->enabled) {
601 trace_nvic_escalate_disabled(irq);
602 escalate = true;
603 }
604
605 if (escalate) {
a73c98e1 606
94a34abe 607 /* We need to escalate this exception to a synchronous HardFault.
2fb50a33
PM
608 * If BFHFNMINS is set then we escalate to the banked HF for
609 * the target security state of the original exception; otherwise
610 * we take a Secure HardFault.
611 */
a73c98e1 612 irq = ARMV7M_EXCP_HARD;
2fb50a33
PM
613 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY) &&
614 (secure ||
615 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK))) {
616 vec = &s->sec_vectors[irq];
617 } else {
618 vec = &s->vectors[irq];
619 }
94a34abe
PM
620 if (running <= vec->prio) {
621 /* We want to escalate to HardFault but we can't take the
622 * synchronous HardFault at this point either. This is a
623 * Lockup condition due to a guest bug. We don't model
624 * Lockup, so report via cpu_abort() instead.
625 */
626 cpu_abort(&s->cpu->parent_obj,
627 "Lockup: can't escalate %d to HardFault "
628 "(current priority %d)\n", irq, running);
629 }
630
2fb50a33 631 /* HF may be banked but there is only one shared HFSR */
a73c98e1
MD
632 s->cpu->env.v7m.hfsr |= R_V7M_HFSR_FORCED_MASK;
633 }
634 }
635
da6d674e
MD
636 if (!vec->pending) {
637 vec->pending = 1;
638 nvic_irq_update(s);
639 }
9ee6e8bb
PB
640}
641
5ede82b8
PM
642void armv7m_nvic_set_pending(void *opaque, int irq, bool secure)
643{
644 do_armv7m_nvic_set_pending(opaque, irq, secure, false);
645}
646
647void armv7m_nvic_set_pending_derived(void *opaque, int irq, bool secure)
648{
649 do_armv7m_nvic_set_pending(opaque, irq, secure, true);
650}
651
9ee6e8bb 652/* Make pending IRQ active. */
6c948518 653void armv7m_nvic_acknowledge_irq(void *opaque)
9ee6e8bb 654{
f797c075 655 NVICState *s = (NVICState *)opaque;
da6d674e
MD
656 CPUARMState *env = &s->cpu->env;
657 const int pending = s->vectpending;
658 const int running = nvic_exec_prio(s);
da6d674e
MD
659 VecInfo *vec;
660
661 assert(pending > ARMV7M_EXCP_RESET && pending < s->num_irq);
662
5cb18069
PM
663 if (s->vectpending_is_s_banked) {
664 vec = &s->sec_vectors[pending];
5cb18069
PM
665 } else {
666 vec = &s->vectors[pending];
5cb18069 667 }
da6d674e
MD
668
669 assert(vec->enabled);
670 assert(vec->pending);
671
5255fcf8 672 assert(s->vectpending_prio < running);
da6d674e 673
6c948518 674 trace_nvic_acknowledge_irq(pending, s->vectpending_prio);
da6d674e
MD
675
676 vec->active = 1;
677 vec->pending = 0;
678
de2db7ec 679 write_v7m_exception(env, s->vectpending);
da6d674e
MD
680
681 nvic_irq_update(s);
6c948518
PM
682}
683
684void armv7m_nvic_get_pending_irq_info(void *opaque,
685 int *pirq, bool *ptargets_secure)
686{
687 NVICState *s = (NVICState *)opaque;
688 const int pending = s->vectpending;
689 bool targets_secure;
690
691 assert(pending > ARMV7M_EXCP_RESET && pending < s->num_irq);
692
693 if (s->vectpending_is_s_banked) {
694 targets_secure = true;
695 } else {
696 targets_secure = !exc_is_banked(pending) &&
697 exc_targets_secure(s, pending);
698 }
699
700 trace_nvic_get_pending_irq_info(pending, targets_secure);
5cb18069 701
6c948518
PM
702 *ptargets_secure = targets_secure;
703 *pirq = pending;
9ee6e8bb
PB
704}
705
5cb18069 706int armv7m_nvic_complete_irq(void *opaque, int irq, bool secure)
9ee6e8bb 707{
f797c075 708 NVICState *s = (NVICState *)opaque;
da6d674e 709 VecInfo *vec;
aa488fe3 710 int ret;
da6d674e
MD
711
712 assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq);
713
5cb18069
PM
714 if (secure && exc_is_banked(irq)) {
715 vec = &s->sec_vectors[irq];
716 } else {
717 vec = &s->vectors[irq];
718 }
da6d674e 719
5cb18069 720 trace_nvic_complete_irq(irq, secure);
da6d674e 721
aa488fe3
PM
722 if (!vec->active) {
723 /* Tell the caller this was an illegal exception return */
724 return -1;
725 }
726
727 ret = nvic_rettobase(s);
728
da6d674e
MD
729 vec->active = 0;
730 if (vec->level) {
731 /* Re-pend the exception if it's still held high; only
732 * happens for extenal IRQs
733 */
734 assert(irq >= NVIC_FIRST_IRQ);
735 vec->pending = 1;
736 }
737
738 nvic_irq_update(s);
aa488fe3
PM
739
740 return ret;
da6d674e
MD
741}
742
743/* callback when external interrupt line is changed */
744static void set_irq_level(void *opaque, int n, int level)
745{
746 NVICState *s = opaque;
747 VecInfo *vec;
748
749 n += NVIC_FIRST_IRQ;
750
751 assert(n >= NVIC_FIRST_IRQ && n < s->num_irq);
752
753 trace_nvic_set_irq_level(n, level);
754
755 /* The pending status of an external interrupt is
756 * latched on rising edge and exception handler return.
757 *
758 * Pulsing the IRQ will always run the handler
759 * once, and the handler will re-run until the
760 * level is low when the handler completes.
761 */
762 vec = &s->vectors[n];
763 if (level != vec->level) {
764 vec->level = level;
765 if (level) {
2fb50a33 766 armv7m_nvic_set_pending(s, n, false);
da6d674e
MD
767 }
768 }
9ee6e8bb
PB
769}
770
45db7ba6 771static uint32_t nvic_readl(NVICState *s, uint32_t offset, MemTxAttrs attrs)
9ee6e8bb 772{
d713ea6c 773 ARMCPU *cpu = s->cpu;
9ee6e8bb 774 uint32_t val;
9ee6e8bb
PB
775
776 switch (offset) {
777 case 4: /* Interrupt Control Type. */
da6d674e 778 return ((s->num_irq - NVIC_FIRST_IRQ) / 32) - 1;
ae7c5c85
PM
779 case 0xc: /* CPPWR */
780 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
781 goto bad_offset;
782 }
783 /* We make the IMPDEF choice that nothing can ever go into a
784 * non-retentive power state, which allows us to RAZ/WI this.
785 */
786 return 0;
e1be0a57
PM
787 case 0x380 ... 0x3bf: /* NVIC_ITNS<n> */
788 {
cf5f7937 789 int startvec = 8 * (offset - 0x380) + NVIC_FIRST_IRQ;
e1be0a57
PM
790 int i;
791
792 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
793 goto bad_offset;
794 }
795 if (!attrs.secure) {
796 return 0;
797 }
798 val = 0;
799 for (i = 0; i < 32 && startvec + i < s->num_irq; i++) {
800 if (s->itns[startvec + i]) {
801 val |= (1 << i);
802 }
803 }
804 return val;
805 }
9ee6e8bb 806 case 0xd00: /* CPUID Base. */
e3da9921 807 return cpu->midr;
3f1e0eb7 808 case 0xd04: /* Interrupt Control State (ICSR) */
9ee6e8bb 809 /* VECTACTIVE */
b06c262b 810 val = cpu->env.v7m.exception;
9ee6e8bb 811 /* VECTPENDING */
da6d674e
MD
812 val |= (s->vectpending & 0xff) << 12;
813 /* ISRPENDING - set if any external IRQ is pending */
814 if (nvic_isrpending(s)) {
815 val |= (1 << 22);
816 }
817 /* RETTOBASE - set if only one handler is active */
818 if (nvic_rettobase(s)) {
819 val |= (1 << 11);
9ee6e8bb 820 }
3f1e0eb7
PM
821 if (attrs.secure) {
822 /* PENDSTSET */
823 if (s->sec_vectors[ARMV7M_EXCP_SYSTICK].pending) {
824 val |= (1 << 26);
825 }
826 /* PENDSVSET */
827 if (s->sec_vectors[ARMV7M_EXCP_PENDSV].pending) {
828 val |= (1 << 28);
829 }
830 } else {
831 /* PENDSTSET */
832 if (s->vectors[ARMV7M_EXCP_SYSTICK].pending) {
833 val |= (1 << 26);
834 }
835 /* PENDSVSET */
836 if (s->vectors[ARMV7M_EXCP_PENDSV].pending) {
837 val |= (1 << 28);
838 }
da6d674e 839 }
9ee6e8bb 840 /* NMIPENDSET */
4f2eff36
PM
841 if ((attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK))
842 && s->vectors[ARMV7M_EXCP_NMI].pending) {
9ee6e8bb 843 val |= (1 << 31);
da6d674e 844 }
3f1e0eb7
PM
845 /* ISRPREEMPT: RES0 when halting debug not implemented */
846 /* STTNS: RES0 for the Main Extension */
9ee6e8bb
PB
847 return val;
848 case 0xd08: /* Vector Table Offset. */
45db7ba6 849 return cpu->env.v7m.vecbase[attrs.secure];
3b2e9344
PM
850 case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */
851 val = 0xfa050000 | (s->prigroup[attrs.secure] << 8);
852 if (attrs.secure) {
853 /* s->aircr stores PRIS, BFHFNMINS, SYSRESETREQS */
854 val |= cpu->env.v7m.aircr;
855 } else {
856 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
857 /* BFHFNMINS is R/O from NS; other bits are RAZ/WI. If
858 * security isn't supported then BFHFNMINS is RAO (and
859 * the bit in env.v7m.aircr is always set).
860 */
861 val |= cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK;
862 }
863 }
864 return val;
9ee6e8bb 865 case 0xd10: /* System Control. */
24ac0fb1 866 return cpu->env.v7m.scr[attrs.secure];
9ee6e8bb 867 case 0xd14: /* Configuration Control. */
9d40cd8a
PM
868 /* The BFHFNMIGN bit is the only non-banked bit; we
869 * keep it in the non-secure copy of the register.
870 */
871 val = cpu->env.v7m.ccr[attrs.secure];
872 val |= cpu->env.v7m.ccr[M_REG_NS] & R_V7M_CCR_BFHFNMIGN_MASK;
873 return val;
437d59c1 874 case 0xd24: /* System Handler Control and State (SHCSR) */
9ee6e8bb 875 val = 0;
437d59c1
PM
876 if (attrs.secure) {
877 if (s->sec_vectors[ARMV7M_EXCP_MEM].active) {
878 val |= (1 << 0);
879 }
880 if (s->sec_vectors[ARMV7M_EXCP_HARD].active) {
881 val |= (1 << 2);
882 }
883 if (s->sec_vectors[ARMV7M_EXCP_USAGE].active) {
884 val |= (1 << 3);
885 }
886 if (s->sec_vectors[ARMV7M_EXCP_SVC].active) {
887 val |= (1 << 7);
888 }
889 if (s->sec_vectors[ARMV7M_EXCP_PENDSV].active) {
890 val |= (1 << 10);
891 }
892 if (s->sec_vectors[ARMV7M_EXCP_SYSTICK].active) {
893 val |= (1 << 11);
894 }
895 if (s->sec_vectors[ARMV7M_EXCP_USAGE].pending) {
896 val |= (1 << 12);
897 }
898 if (s->sec_vectors[ARMV7M_EXCP_MEM].pending) {
899 val |= (1 << 13);
900 }
901 if (s->sec_vectors[ARMV7M_EXCP_SVC].pending) {
902 val |= (1 << 15);
903 }
904 if (s->sec_vectors[ARMV7M_EXCP_MEM].enabled) {
905 val |= (1 << 16);
906 }
907 if (s->sec_vectors[ARMV7M_EXCP_USAGE].enabled) {
908 val |= (1 << 18);
909 }
910 if (s->sec_vectors[ARMV7M_EXCP_HARD].pending) {
911 val |= (1 << 21);
912 }
913 /* SecureFault is not banked but is always RAZ/WI to NS */
914 if (s->vectors[ARMV7M_EXCP_SECURE].active) {
915 val |= (1 << 4);
916 }
917 if (s->vectors[ARMV7M_EXCP_SECURE].enabled) {
918 val |= (1 << 19);
919 }
920 if (s->vectors[ARMV7M_EXCP_SECURE].pending) {
921 val |= (1 << 20);
922 }
923 } else {
924 if (s->vectors[ARMV7M_EXCP_MEM].active) {
925 val |= (1 << 0);
926 }
927 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
928 /* HARDFAULTACT, HARDFAULTPENDED not present in v7M */
929 if (s->vectors[ARMV7M_EXCP_HARD].active) {
930 val |= (1 << 2);
931 }
932 if (s->vectors[ARMV7M_EXCP_HARD].pending) {
933 val |= (1 << 21);
934 }
935 }
936 if (s->vectors[ARMV7M_EXCP_USAGE].active) {
937 val |= (1 << 3);
938 }
939 if (s->vectors[ARMV7M_EXCP_SVC].active) {
940 val |= (1 << 7);
941 }
942 if (s->vectors[ARMV7M_EXCP_PENDSV].active) {
943 val |= (1 << 10);
944 }
945 if (s->vectors[ARMV7M_EXCP_SYSTICK].active) {
946 val |= (1 << 11);
947 }
948 if (s->vectors[ARMV7M_EXCP_USAGE].pending) {
949 val |= (1 << 12);
950 }
951 if (s->vectors[ARMV7M_EXCP_MEM].pending) {
952 val |= (1 << 13);
953 }
954 if (s->vectors[ARMV7M_EXCP_SVC].pending) {
955 val |= (1 << 15);
956 }
957 if (s->vectors[ARMV7M_EXCP_MEM].enabled) {
958 val |= (1 << 16);
959 }
960 if (s->vectors[ARMV7M_EXCP_USAGE].enabled) {
961 val |= (1 << 18);
962 }
da6d674e 963 }
437d59c1
PM
964 if (attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
965 if (s->vectors[ARMV7M_EXCP_BUS].active) {
966 val |= (1 << 1);
967 }
968 if (s->vectors[ARMV7M_EXCP_BUS].pending) {
969 val |= (1 << 14);
970 }
971 if (s->vectors[ARMV7M_EXCP_BUS].enabled) {
972 val |= (1 << 17);
973 }
974 if (arm_feature(&cpu->env, ARM_FEATURE_V8) &&
975 s->vectors[ARMV7M_EXCP_NMI].active) {
976 /* NMIACT is not present in v7M */
977 val |= (1 << 5);
978 }
da6d674e 979 }
437d59c1
PM
980
981 /* TODO: this is RAZ/WI from NS if DEMCR.SDME is set */
da6d674e
MD
982 if (s->vectors[ARMV7M_EXCP_DEBUG].active) {
983 val |= (1 << 8);
984 }
9ee6e8bb 985 return val;
9ee6e8bb 986 case 0xd2c: /* Hard Fault Status. */
e6b33209 987 return cpu->env.v7m.hfsr;
9ee6e8bb 988 case 0xd30: /* Debug Fault Status. */
e6b33209
MD
989 return cpu->env.v7m.dfsr;
990 case 0xd34: /* MMFAR MemManage Fault Address */
c51a5cfc 991 return cpu->env.v7m.mmfar[attrs.secure];
9ee6e8bb 992 case 0xd38: /* Bus Fault Address. */
e6b33209 993 return cpu->env.v7m.bfar;
9ee6e8bb
PB
994 case 0xd3c: /* Aux Fault Status. */
995 /* TODO: Implement fault status registers. */
e6b33209
MD
996 qemu_log_mask(LOG_UNIMP,
997 "Aux Fault status registers unimplemented\n");
e72e3ffc 998 return 0;
9ee6e8bb 999 case 0xd40: /* PFR0. */
5a53e2c1
PM
1000 return cpu->id_pfr0;
1001 case 0xd44: /* PFR1. */
1002 return cpu->id_pfr1;
9ee6e8bb 1003 case 0xd48: /* DFR0. */
5a53e2c1 1004 return cpu->id_dfr0;
9ee6e8bb 1005 case 0xd4c: /* AFR0. */
5a53e2c1 1006 return cpu->id_afr0;
9ee6e8bb 1007 case 0xd50: /* MMFR0. */
5a53e2c1 1008 return cpu->id_mmfr0;
9ee6e8bb 1009 case 0xd54: /* MMFR1. */
5a53e2c1 1010 return cpu->id_mmfr1;
9ee6e8bb 1011 case 0xd58: /* MMFR2. */
5a53e2c1 1012 return cpu->id_mmfr2;
9ee6e8bb 1013 case 0xd5c: /* MMFR3. */
5a53e2c1 1014 return cpu->id_mmfr3;
9ee6e8bb 1015 case 0xd60: /* ISAR0. */
5a53e2c1 1016 return cpu->id_isar0;
9ee6e8bb 1017 case 0xd64: /* ISAR1. */
5a53e2c1 1018 return cpu->id_isar1;
9ee6e8bb 1019 case 0xd68: /* ISAR2. */
5a53e2c1 1020 return cpu->id_isar2;
9ee6e8bb 1021 case 0xd6c: /* ISAR3. */
5a53e2c1 1022 return cpu->id_isar3;
9ee6e8bb 1023 case 0xd70: /* ISAR4. */
5a53e2c1
PM
1024 return cpu->id_isar4;
1025 case 0xd74: /* ISAR5. */
1026 return cpu->id_isar5;
43bbce7f
PM
1027 case 0xd78: /* CLIDR */
1028 return cpu->clidr;
1029 case 0xd7c: /* CTR */
1030 return cpu->ctr;
1031 case 0xd80: /* CSSIDR */
1032 {
1033 int idx = cpu->env.v7m.csselr[attrs.secure] & R_V7M_CSSELR_INDEX_MASK;
1034 return cpu->ccsidr[idx];
1035 }
1036 case 0xd84: /* CSSELR */
1037 return cpu->env.v7m.csselr[attrs.secure];
9ee6e8bb 1038 /* TODO: Implement debug registers. */
29c483a5
MD
1039 case 0xd90: /* MPU_TYPE */
1040 /* Unified MPU; if the MPU is not present this value is zero */
1041 return cpu->pmsav7_dregion << 8;
1042 break;
1043 case 0xd94: /* MPU_CTRL */
ecf5e8ea 1044 return cpu->env.v7m.mpu_ctrl[attrs.secure];
29c483a5 1045 case 0xd98: /* MPU_RNR */
1bc04a88 1046 return cpu->env.pmsav7.rnr[attrs.secure];
29c483a5
MD
1047 case 0xd9c: /* MPU_RBAR */
1048 case 0xda4: /* MPU_RBAR_A1 */
1049 case 0xdac: /* MPU_RBAR_A2 */
1050 case 0xdb4: /* MPU_RBAR_A3 */
1051 {
1bc04a88 1052 int region = cpu->env.pmsav7.rnr[attrs.secure];
29c483a5 1053
0e1a46bb
PM
1054 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1055 /* PMSAv8M handling of the aliases is different from v7M:
1056 * aliases A1, A2, A3 override the low two bits of the region
1057 * number in MPU_RNR, and there is no 'region' field in the
1058 * RBAR register.
1059 */
1060 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
1061 if (aliasno) {
1062 region = deposit32(region, 0, 2, aliasno);
1063 }
1064 if (region >= cpu->pmsav7_dregion) {
1065 return 0;
1066 }
62c58ee0 1067 return cpu->env.pmsav8.rbar[attrs.secure][region];
0e1a46bb
PM
1068 }
1069
29c483a5
MD
1070 if (region >= cpu->pmsav7_dregion) {
1071 return 0;
1072 }
2b75ef01 1073 return (cpu->env.pmsav7.drbar[region] & ~0x1f) | (region & 0xf);
29c483a5 1074 }
0e1a46bb
PM
1075 case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */
1076 case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */
1077 case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */
1078 case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */
29c483a5 1079 {
1bc04a88 1080 int region = cpu->env.pmsav7.rnr[attrs.secure];
29c483a5 1081
0e1a46bb
PM
1082 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1083 /* PMSAv8M handling of the aliases is different from v7M:
1084 * aliases A1, A2, A3 override the low two bits of the region
1085 * number in MPU_RNR.
1086 */
1087 int aliasno = (offset - 0xda0) / 8; /* 0..3 */
1088 if (aliasno) {
1089 region = deposit32(region, 0, 2, aliasno);
1090 }
1091 if (region >= cpu->pmsav7_dregion) {
1092 return 0;
1093 }
62c58ee0 1094 return cpu->env.pmsav8.rlar[attrs.secure][region];
0e1a46bb
PM
1095 }
1096
29c483a5
MD
1097 if (region >= cpu->pmsav7_dregion) {
1098 return 0;
1099 }
1100 return ((cpu->env.pmsav7.dracr[region] & 0xffff) << 16) |
1101 (cpu->env.pmsav7.drsr[region] & 0xffff);
1102 }
0e1a46bb
PM
1103 case 0xdc0: /* MPU_MAIR0 */
1104 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1105 goto bad_offset;
1106 }
4125e6fe 1107 return cpu->env.pmsav8.mair0[attrs.secure];
0e1a46bb
PM
1108 case 0xdc4: /* MPU_MAIR1 */
1109 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1110 goto bad_offset;
1111 }
4125e6fe 1112 return cpu->env.pmsav8.mair1[attrs.secure];
9901c576
PM
1113 case 0xdd0: /* SAU_CTRL */
1114 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1115 goto bad_offset;
1116 }
1117 if (!attrs.secure) {
1118 return 0;
1119 }
1120 return cpu->env.sau.ctrl;
1121 case 0xdd4: /* SAU_TYPE */
1122 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1123 goto bad_offset;
1124 }
1125 if (!attrs.secure) {
1126 return 0;
1127 }
1128 return cpu->sau_sregion;
1129 case 0xdd8: /* SAU_RNR */
1130 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1131 goto bad_offset;
1132 }
1133 if (!attrs.secure) {
1134 return 0;
1135 }
1136 return cpu->env.sau.rnr;
1137 case 0xddc: /* SAU_RBAR */
1138 {
1139 int region = cpu->env.sau.rnr;
1140
1141 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1142 goto bad_offset;
1143 }
1144 if (!attrs.secure) {
1145 return 0;
1146 }
1147 if (region >= cpu->sau_sregion) {
1148 return 0;
1149 }
1150 return cpu->env.sau.rbar[region];
1151 }
1152 case 0xde0: /* SAU_RLAR */
1153 {
1154 int region = cpu->env.sau.rnr;
1155
1156 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1157 goto bad_offset;
1158 }
1159 if (!attrs.secure) {
1160 return 0;
1161 }
1162 if (region >= cpu->sau_sregion) {
1163 return 0;
1164 }
1165 return cpu->env.sau.rlar[region];
1166 }
bed079da
PM
1167 case 0xde4: /* SFSR */
1168 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1169 goto bad_offset;
1170 }
1171 if (!attrs.secure) {
1172 return 0;
1173 }
1174 return cpu->env.v7m.sfsr;
1175 case 0xde8: /* SFAR */
1176 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1177 goto bad_offset;
1178 }
1179 if (!attrs.secure) {
1180 return 0;
1181 }
1182 return cpu->env.v7m.sfar;
9ee6e8bb 1183 default:
0e1a46bb 1184 bad_offset:
e72e3ffc
PM
1185 qemu_log_mask(LOG_GUEST_ERROR, "NVIC: Bad read offset 0x%x\n", offset);
1186 return 0;
9ee6e8bb
PB
1187 }
1188}
1189
45db7ba6
PM
1190static void nvic_writel(NVICState *s, uint32_t offset, uint32_t value,
1191 MemTxAttrs attrs)
9ee6e8bb 1192{
d713ea6c 1193 ARMCPU *cpu = s->cpu;
ff68dacb 1194
9ee6e8bb 1195 switch (offset) {
ae7c5c85
PM
1196 case 0xc: /* CPPWR */
1197 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1198 goto bad_offset;
1199 }
1200 /* Make the IMPDEF choice to RAZ/WI this. */
1201 break;
e1be0a57
PM
1202 case 0x380 ... 0x3bf: /* NVIC_ITNS<n> */
1203 {
cf5f7937 1204 int startvec = 8 * (offset - 0x380) + NVIC_FIRST_IRQ;
e1be0a57
PM
1205 int i;
1206
1207 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1208 goto bad_offset;
1209 }
1210 if (!attrs.secure) {
1211 break;
1212 }
1213 for (i = 0; i < 32 && startvec + i < s->num_irq; i++) {
1214 s->itns[startvec + i] = (value >> i) & 1;
1215 }
1216 nvic_irq_update(s);
1217 break;
1218 }
3f1e0eb7 1219 case 0xd04: /* Interrupt Control State (ICSR) */
4f2eff36 1220 if (attrs.secure || cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) {
3f1e0eb7
PM
1221 if (value & (1 << 31)) {
1222 armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI, false);
1223 } else if (value & (1 << 30) &&
1224 arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1225 /* PENDNMICLR didn't exist in v7M */
1226 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_NMI, false);
1227 }
9ee6e8bb
PB
1228 }
1229 if (value & (1 << 28)) {
2fb50a33 1230 armv7m_nvic_set_pending(s, ARMV7M_EXCP_PENDSV, attrs.secure);
9ee6e8bb 1231 } else if (value & (1 << 27)) {
2fb50a33 1232 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_PENDSV, attrs.secure);
9ee6e8bb
PB
1233 }
1234 if (value & (1 << 26)) {
2fb50a33 1235 armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK, attrs.secure);
9ee6e8bb 1236 } else if (value & (1 << 25)) {
2fb50a33 1237 armv7m_nvic_clear_pending(s, ARMV7M_EXCP_SYSTICK, attrs.secure);
9ee6e8bb
PB
1238 }
1239 break;
1240 case 0xd08: /* Vector Table Offset. */
45db7ba6 1241 cpu->env.v7m.vecbase[attrs.secure] = value & 0xffffff80;
9ee6e8bb 1242 break;
3b2e9344
PM
1243 case 0xd0c: /* Application Interrupt/Reset Control (AIRCR) */
1244 if ((value >> R_V7M_AIRCR_VECTKEY_SHIFT) == 0x05fa) {
1245 if (value & R_V7M_AIRCR_SYSRESETREQ_MASK) {
1246 if (attrs.secure ||
1247 !(cpu->env.v7m.aircr & R_V7M_AIRCR_SYSRESETREQS_MASK)) {
1248 qemu_irq_pulse(s->sysresetreq);
1249 }
e192becd 1250 }
3b2e9344 1251 if (value & R_V7M_AIRCR_VECTCLRACTIVE_MASK) {
14790f73
MD
1252 qemu_log_mask(LOG_GUEST_ERROR,
1253 "Setting VECTCLRACTIVE when not in DEBUG mode "
1254 "is UNPREDICTABLE\n");
9ee6e8bb 1255 }
3b2e9344
PM
1256 if (value & R_V7M_AIRCR_VECTRESET_MASK) {
1257 /* NB: this bit is RES0 in v8M */
14790f73
MD
1258 qemu_log_mask(LOG_GUEST_ERROR,
1259 "Setting VECTRESET when not in DEBUG mode "
1260 "is UNPREDICTABLE\n");
9ee6e8bb 1261 }
3b2e9344
PM
1262 s->prigroup[attrs.secure] = extract32(value,
1263 R_V7M_AIRCR_PRIGROUP_SHIFT,
1264 R_V7M_AIRCR_PRIGROUP_LENGTH);
1265 if (attrs.secure) {
1266 /* These bits are only writable by secure */
1267 cpu->env.v7m.aircr = value &
1268 (R_V7M_AIRCR_SYSRESETREQS_MASK |
1269 R_V7M_AIRCR_BFHFNMINS_MASK |
1270 R_V7M_AIRCR_PRIS_MASK);
7208b426
PM
1271 /* BFHFNMINS changes the priority of Secure HardFault, and
1272 * allows a pending Non-secure HardFault to preempt (which
1273 * we implement by marking it enabled).
1274 */
331f4bae
PM
1275 if (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) {
1276 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -3;
7208b426 1277 s->vectors[ARMV7M_EXCP_HARD].enabled = 1;
331f4bae
PM
1278 } else {
1279 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -1;
7208b426 1280 s->vectors[ARMV7M_EXCP_HARD].enabled = 0;
331f4bae 1281 }
3b2e9344 1282 }
da6d674e 1283 nvic_irq_update(s);
9ee6e8bb
PB
1284 }
1285 break;
1286 case 0xd10: /* System Control. */
24ac0fb1
PM
1287 /* We don't implement deep-sleep so these bits are RAZ/WI.
1288 * The other bits in the register are banked.
1289 * QEMU's implementation ignores SEVONPEND and SLEEPONEXIT, which
1290 * is architecturally permitted.
1291 */
1292 value &= ~(R_V7M_SCR_SLEEPDEEP_MASK | R_V7M_SCR_SLEEPDEEPS_MASK);
1293 cpu->env.v7m.scr[attrs.secure] = value;
e6b33209
MD
1294 break;
1295 case 0xd14: /* Configuration Control. */
1296 /* Enforce RAZ/WI on reserved and must-RAZ/WI bits */
1297 value &= (R_V7M_CCR_STKALIGN_MASK |
1298 R_V7M_CCR_BFHFNMIGN_MASK |
1299 R_V7M_CCR_DIV_0_TRP_MASK |
1300 R_V7M_CCR_UNALIGN_TRP_MASK |
1301 R_V7M_CCR_USERSETMPEND_MASK |
1302 R_V7M_CCR_NONBASETHRDENA_MASK);
1303
9d40cd8a
PM
1304 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1305 /* v8M makes NONBASETHRDENA and STKALIGN be RES1 */
1306 value |= R_V7M_CCR_NONBASETHRDENA_MASK
1307 | R_V7M_CCR_STKALIGN_MASK;
1308 }
1309 if (attrs.secure) {
1310 /* the BFHFNMIGN bit is not banked; keep that in the NS copy */
1311 cpu->env.v7m.ccr[M_REG_NS] =
1312 (cpu->env.v7m.ccr[M_REG_NS] & ~R_V7M_CCR_BFHFNMIGN_MASK)
1313 | (value & R_V7M_CCR_BFHFNMIGN_MASK);
1314 value &= ~R_V7M_CCR_BFHFNMIGN_MASK;
1315 }
1316
1317 cpu->env.v7m.ccr[attrs.secure] = value;
e72e3ffc 1318 break;
437d59c1
PM
1319 case 0xd24: /* System Handler Control and State (SHCSR) */
1320 if (attrs.secure) {
1321 s->sec_vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0;
1322 /* Secure HardFault active bit cannot be written */
1323 s->sec_vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0;
1324 s->sec_vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0;
1325 s->sec_vectors[ARMV7M_EXCP_PENDSV].active =
1326 (value & (1 << 10)) != 0;
1327 s->sec_vectors[ARMV7M_EXCP_SYSTICK].active =
1328 (value & (1 << 11)) != 0;
1329 s->sec_vectors[ARMV7M_EXCP_USAGE].pending =
1330 (value & (1 << 12)) != 0;
1331 s->sec_vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0;
1332 s->sec_vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0;
1333 s->sec_vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0;
1334 s->sec_vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0;
1335 s->sec_vectors[ARMV7M_EXCP_USAGE].enabled =
1336 (value & (1 << 18)) != 0;
04829ce3 1337 s->sec_vectors[ARMV7M_EXCP_HARD].pending = (value & (1 << 21)) != 0;
437d59c1
PM
1338 /* SecureFault not banked, but RAZ/WI to NS */
1339 s->vectors[ARMV7M_EXCP_SECURE].active = (value & (1 << 4)) != 0;
1340 s->vectors[ARMV7M_EXCP_SECURE].enabled = (value & (1 << 19)) != 0;
1341 s->vectors[ARMV7M_EXCP_SECURE].pending = (value & (1 << 20)) != 0;
1342 } else {
1343 s->vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0;
1344 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1345 /* HARDFAULTPENDED is not present in v7M */
1346 s->vectors[ARMV7M_EXCP_HARD].pending = (value & (1 << 21)) != 0;
1347 }
1348 s->vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0;
1349 s->vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0;
1350 s->vectors[ARMV7M_EXCP_PENDSV].active = (value & (1 << 10)) != 0;
1351 s->vectors[ARMV7M_EXCP_SYSTICK].active = (value & (1 << 11)) != 0;
1352 s->vectors[ARMV7M_EXCP_USAGE].pending = (value & (1 << 12)) != 0;
1353 s->vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0;
1354 s->vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0;
1355 s->vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0;
1356 s->vectors[ARMV7M_EXCP_USAGE].enabled = (value & (1 << 18)) != 0;
1357 }
1358 if (attrs.secure || (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
1359 s->vectors[ARMV7M_EXCP_BUS].active = (value & (1 << 1)) != 0;
1360 s->vectors[ARMV7M_EXCP_BUS].pending = (value & (1 << 14)) != 0;
1361 s->vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0;
1362 }
1363 /* NMIACT can only be written if the write is of a zero, with
1364 * BFHFNMINS 1, and by the CPU in secure state via the NS alias.
1365 */
1366 if (!attrs.secure && cpu->env.v7m.secure &&
1367 (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) &&
1368 (value & (1 << 5)) == 0) {
1369 s->vectors[ARMV7M_EXCP_NMI].active = 0;
1370 }
1371 /* HARDFAULTACT can only be written if the write is of a zero
1372 * to the non-secure HardFault state by the CPU in secure state.
1373 * The only case where we can be targeting the non-secure HF state
1374 * when in secure state is if this is a write via the NS alias
1375 * and BFHFNMINS is 1.
1376 */
1377 if (!attrs.secure && cpu->env.v7m.secure &&
1378 (cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK) &&
1379 (value & (1 << 2)) == 0) {
1380 s->vectors[ARMV7M_EXCP_HARD].active = 0;
1381 }
1382
1383 /* TODO: this is RAZ/WI from NS if DEMCR.SDME is set */
5db53e35 1384 s->vectors[ARMV7M_EXCP_DEBUG].active = (value & (1 << 8)) != 0;
da6d674e 1385 nvic_irq_update(s);
9ee6e8bb 1386 break;
9ee6e8bb 1387 case 0xd2c: /* Hard Fault Status. */
e6b33209
MD
1388 cpu->env.v7m.hfsr &= ~value; /* W1C */
1389 break;
9ee6e8bb 1390 case 0xd30: /* Debug Fault Status. */
e6b33209
MD
1391 cpu->env.v7m.dfsr &= ~value; /* W1C */
1392 break;
9ee6e8bb 1393 case 0xd34: /* Mem Manage Address. */
c51a5cfc 1394 cpu->env.v7m.mmfar[attrs.secure] = value;
e6b33209 1395 return;
9ee6e8bb 1396 case 0xd38: /* Bus Fault Address. */
e6b33209
MD
1397 cpu->env.v7m.bfar = value;
1398 return;
9ee6e8bb 1399 case 0xd3c: /* Aux Fault Status. */
e72e3ffc 1400 qemu_log_mask(LOG_UNIMP,
e6b33209 1401 "NVIC: Aux fault status registers unimplemented\n");
e72e3ffc 1402 break;
43bbce7f
PM
1403 case 0xd84: /* CSSELR */
1404 if (!arm_v7m_csselr_razwi(cpu)) {
1405 cpu->env.v7m.csselr[attrs.secure] = value & R_V7M_CSSELR_INDEX_MASK;
1406 }
1407 break;
29c483a5
MD
1408 case 0xd90: /* MPU_TYPE */
1409 return; /* RO */
1410 case 0xd94: /* MPU_CTRL */
1411 if ((value &
1412 (R_V7M_MPU_CTRL_HFNMIENA_MASK | R_V7M_MPU_CTRL_ENABLE_MASK))
1413 == R_V7M_MPU_CTRL_HFNMIENA_MASK) {
1414 qemu_log_mask(LOG_GUEST_ERROR, "MPU_CTRL: HFNMIENA and !ENABLE is "
1415 "UNPREDICTABLE\n");
1416 }
ecf5e8ea
PM
1417 cpu->env.v7m.mpu_ctrl[attrs.secure]
1418 = value & (R_V7M_MPU_CTRL_ENABLE_MASK |
1419 R_V7M_MPU_CTRL_HFNMIENA_MASK |
1420 R_V7M_MPU_CTRL_PRIVDEFENA_MASK);
29c483a5
MD
1421 tlb_flush(CPU(cpu));
1422 break;
1423 case 0xd98: /* MPU_RNR */
1424 if (value >= cpu->pmsav7_dregion) {
1425 qemu_log_mask(LOG_GUEST_ERROR, "MPU region out of range %"
1426 PRIu32 "/%" PRIu32 "\n",
1427 value, cpu->pmsav7_dregion);
1428 } else {
1bc04a88 1429 cpu->env.pmsav7.rnr[attrs.secure] = value;
29c483a5
MD
1430 }
1431 break;
1432 case 0xd9c: /* MPU_RBAR */
1433 case 0xda4: /* MPU_RBAR_A1 */
1434 case 0xdac: /* MPU_RBAR_A2 */
1435 case 0xdb4: /* MPU_RBAR_A3 */
1436 {
1437 int region;
1438
0e1a46bb
PM
1439 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1440 /* PMSAv8M handling of the aliases is different from v7M:
1441 * aliases A1, A2, A3 override the low two bits of the region
1442 * number in MPU_RNR, and there is no 'region' field in the
1443 * RBAR register.
1444 */
1445 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
1446
1bc04a88 1447 region = cpu->env.pmsav7.rnr[attrs.secure];
0e1a46bb
PM
1448 if (aliasno) {
1449 region = deposit32(region, 0, 2, aliasno);
1450 }
1451 if (region >= cpu->pmsav7_dregion) {
1452 return;
1453 }
62c58ee0 1454 cpu->env.pmsav8.rbar[attrs.secure][region] = value;
0e1a46bb
PM
1455 tlb_flush(CPU(cpu));
1456 return;
1457 }
1458
29c483a5
MD
1459 if (value & (1 << 4)) {
1460 /* VALID bit means use the region number specified in this
1461 * value and also update MPU_RNR.REGION with that value.
1462 */
1463 region = extract32(value, 0, 4);
1464 if (region >= cpu->pmsav7_dregion) {
1465 qemu_log_mask(LOG_GUEST_ERROR,
1466 "MPU region out of range %u/%" PRIu32 "\n",
1467 region, cpu->pmsav7_dregion);
1468 return;
1469 }
1bc04a88 1470 cpu->env.pmsav7.rnr[attrs.secure] = region;
29c483a5 1471 } else {
1bc04a88 1472 region = cpu->env.pmsav7.rnr[attrs.secure];
29c483a5
MD
1473 }
1474
1475 if (region >= cpu->pmsav7_dregion) {
1476 return;
1477 }
1478
1479 cpu->env.pmsav7.drbar[region] = value & ~0x1f;
1480 tlb_flush(CPU(cpu));
1481 break;
1482 }
0e1a46bb
PM
1483 case 0xda0: /* MPU_RASR (v7M), MPU_RLAR (v8M) */
1484 case 0xda8: /* MPU_RASR_A1 (v7M), MPU_RLAR_A1 (v8M) */
1485 case 0xdb0: /* MPU_RASR_A2 (v7M), MPU_RLAR_A2 (v8M) */
1486 case 0xdb8: /* MPU_RASR_A3 (v7M), MPU_RLAR_A3 (v8M) */
29c483a5 1487 {
1bc04a88 1488 int region = cpu->env.pmsav7.rnr[attrs.secure];
29c483a5 1489
0e1a46bb
PM
1490 if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1491 /* PMSAv8M handling of the aliases is different from v7M:
1492 * aliases A1, A2, A3 override the low two bits of the region
1493 * number in MPU_RNR.
1494 */
1495 int aliasno = (offset - 0xd9c) / 8; /* 0..3 */
1496
1bc04a88 1497 region = cpu->env.pmsav7.rnr[attrs.secure];
0e1a46bb
PM
1498 if (aliasno) {
1499 region = deposit32(region, 0, 2, aliasno);
1500 }
1501 if (region >= cpu->pmsav7_dregion) {
1502 return;
1503 }
62c58ee0 1504 cpu->env.pmsav8.rlar[attrs.secure][region] = value;
0e1a46bb
PM
1505 tlb_flush(CPU(cpu));
1506 return;
1507 }
1508
29c483a5
MD
1509 if (region >= cpu->pmsav7_dregion) {
1510 return;
1511 }
1512
1513 cpu->env.pmsav7.drsr[region] = value & 0xff3f;
1514 cpu->env.pmsav7.dracr[region] = (value >> 16) & 0x173f;
1515 tlb_flush(CPU(cpu));
1516 break;
1517 }
0e1a46bb
PM
1518 case 0xdc0: /* MPU_MAIR0 */
1519 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1520 goto bad_offset;
1521 }
1522 if (cpu->pmsav7_dregion) {
1523 /* Register is RES0 if no MPU regions are implemented */
4125e6fe 1524 cpu->env.pmsav8.mair0[attrs.secure] = value;
0e1a46bb
PM
1525 }
1526 /* We don't need to do anything else because memory attributes
1527 * only affect cacheability, and we don't implement caching.
1528 */
1529 break;
1530 case 0xdc4: /* MPU_MAIR1 */
1531 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1532 goto bad_offset;
1533 }
1534 if (cpu->pmsav7_dregion) {
1535 /* Register is RES0 if no MPU regions are implemented */
4125e6fe 1536 cpu->env.pmsav8.mair1[attrs.secure] = value;
0e1a46bb
PM
1537 }
1538 /* We don't need to do anything else because memory attributes
1539 * only affect cacheability, and we don't implement caching.
1540 */
1541 break;
9901c576
PM
1542 case 0xdd0: /* SAU_CTRL */
1543 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1544 goto bad_offset;
1545 }
1546 if (!attrs.secure) {
1547 return;
1548 }
1549 cpu->env.sau.ctrl = value & 3;
a94bb9cd 1550 break;
9901c576
PM
1551 case 0xdd4: /* SAU_TYPE */
1552 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1553 goto bad_offset;
1554 }
1555 break;
1556 case 0xdd8: /* SAU_RNR */
1557 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1558 goto bad_offset;
1559 }
1560 if (!attrs.secure) {
1561 return;
1562 }
1563 if (value >= cpu->sau_sregion) {
1564 qemu_log_mask(LOG_GUEST_ERROR, "SAU region out of range %"
1565 PRIu32 "/%" PRIu32 "\n",
1566 value, cpu->sau_sregion);
1567 } else {
1568 cpu->env.sau.rnr = value;
1569 }
1570 break;
1571 case 0xddc: /* SAU_RBAR */
1572 {
1573 int region = cpu->env.sau.rnr;
1574
1575 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1576 goto bad_offset;
1577 }
1578 if (!attrs.secure) {
1579 return;
1580 }
1581 if (region >= cpu->sau_sregion) {
1582 return;
1583 }
1584 cpu->env.sau.rbar[region] = value & ~0x1f;
1585 tlb_flush(CPU(cpu));
1586 break;
1587 }
1588 case 0xde0: /* SAU_RLAR */
1589 {
1590 int region = cpu->env.sau.rnr;
1591
1592 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1593 goto bad_offset;
1594 }
1595 if (!attrs.secure) {
1596 return;
1597 }
1598 if (region >= cpu->sau_sregion) {
1599 return;
1600 }
1601 cpu->env.sau.rlar[region] = value & ~0x1c;
1602 tlb_flush(CPU(cpu));
1603 break;
1604 }
bed079da
PM
1605 case 0xde4: /* SFSR */
1606 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1607 goto bad_offset;
1608 }
1609 if (!attrs.secure) {
1610 return;
1611 }
1612 cpu->env.v7m.sfsr &= ~value; /* W1C */
1613 break;
1614 case 0xde8: /* SFAR */
1615 if (!arm_feature(&cpu->env, ARM_FEATURE_V8)) {
1616 goto bad_offset;
1617 }
1618 if (!attrs.secure) {
1619 return;
1620 }
1621 cpu->env.v7m.sfsr = value;
1622 break;
2a29ddee 1623 case 0xf00: /* Software Triggered Interrupt Register */
da6d674e 1624 {
da6d674e 1625 int excnum = (value & 0x1ff) + NVIC_FIRST_IRQ;
eb578a23 1626 if (excnum < s->num_irq) {
2fb50a33 1627 armv7m_nvic_set_pending(s, excnum, false);
2a29ddee
PM
1628 }
1629 break;
da6d674e 1630 }
e8ab26c4
PM
1631 case 0xf50: /* ICIALLU */
1632 case 0xf58: /* ICIMVAU */
1633 case 0xf5c: /* DCIMVAC */
1634 case 0xf60: /* DCISW */
1635 case 0xf64: /* DCCMVAU */
1636 case 0xf68: /* DCCMVAC */
1637 case 0xf6c: /* DCCSW */
1638 case 0xf70: /* DCCIMVAC */
1639 case 0xf74: /* DCCISW */
1640 case 0xf78: /* BPIALL */
1641 /* Cache and branch predictor maintenance: for QEMU these always NOP */
1642 break;
9ee6e8bb 1643 default:
0e1a46bb 1644 bad_offset:
e72e3ffc
PM
1645 qemu_log_mask(LOG_GUEST_ERROR,
1646 "NVIC: Bad write offset 0x%x\n", offset);
9ee6e8bb
PB
1647 }
1648}
1649
9d40cd8a 1650static bool nvic_user_access_ok(NVICState *s, hwaddr offset, MemTxAttrs attrs)
eb578a23
PM
1651{
1652 /* Return true if unprivileged access to this register is permitted. */
1653 switch (offset) {
1654 case 0xf00: /* STIR: accessible only if CCR.USERSETMPEND permits */
9d40cd8a
PM
1655 /* For access via STIR_NS it is the NS CCR.USERSETMPEND that
1656 * controls access even though the CPU is in Secure state (I_QDKX).
1657 */
1658 return s->cpu->env.v7m.ccr[attrs.secure] & R_V7M_CCR_USERSETMPEND_MASK;
eb578a23
PM
1659 default:
1660 /* All other user accesses cause a BusFault unconditionally */
1661 return false;
1662 }
1663}
1664
e6a0d350
PM
1665static int shpr_bank(NVICState *s, int exc, MemTxAttrs attrs)
1666{
1667 /* Behaviour for the SHPR register field for this exception:
1668 * return M_REG_NS to use the nonsecure vector (including for
1669 * non-banked exceptions), M_REG_S for the secure version of
1670 * a banked exception, and -1 if this field should RAZ/WI.
1671 */
1672 switch (exc) {
1673 case ARMV7M_EXCP_MEM:
1674 case ARMV7M_EXCP_USAGE:
1675 case ARMV7M_EXCP_SVC:
1676 case ARMV7M_EXCP_PENDSV:
1677 case ARMV7M_EXCP_SYSTICK:
1678 /* Banked exceptions */
1679 return attrs.secure;
1680 case ARMV7M_EXCP_BUS:
1681 /* Not banked, RAZ/WI from nonsecure if BFHFNMINS is zero */
1682 if (!attrs.secure &&
1683 !(s->cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK)) {
1684 return -1;
1685 }
1686 return M_REG_NS;
1687 case ARMV7M_EXCP_SECURE:
1688 /* Not banked, RAZ/WI from nonsecure */
1689 if (!attrs.secure) {
1690 return -1;
1691 }
1692 return M_REG_NS;
1693 case ARMV7M_EXCP_DEBUG:
1694 /* Not banked. TODO should RAZ/WI if DEMCR.SDME is set */
1695 return M_REG_NS;
1696 case 8 ... 10:
1697 case 13:
1698 /* RES0 */
1699 return -1;
1700 default:
1701 /* Not reachable due to decode of SHPR register addresses */
1702 g_assert_not_reached();
1703 }
1704}
1705
eb578a23
PM
1706static MemTxResult nvic_sysreg_read(void *opaque, hwaddr addr,
1707 uint64_t *data, unsigned size,
1708 MemTxAttrs attrs)
2a29ddee 1709{
f797c075 1710 NVICState *s = (NVICState *)opaque;
2a29ddee 1711 uint32_t offset = addr;
da6d674e 1712 unsigned i, startvec, end;
0e8153dd
AB
1713 uint32_t val;
1714
9d40cd8a 1715 if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) {
eb578a23
PM
1716 /* Generate BusFault for unprivileged accesses */
1717 return MEMTX_ERROR;
1718 }
1719
0e8153dd 1720 switch (offset) {
da6d674e
MD
1721 /* reads of set and clear both return the status */
1722 case 0x100 ... 0x13f: /* NVIC Set enable */
1723 offset += 0x80;
1724 /* fall through */
1725 case 0x180 ... 0x1bf: /* NVIC Clear enable */
1726 val = 0;
12fbf1a1 1727 startvec = 8 * (offset - 0x180) + NVIC_FIRST_IRQ; /* vector # */
da6d674e
MD
1728
1729 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
e1be0a57
PM
1730 if (s->vectors[startvec + i].enabled &&
1731 (attrs.secure || s->itns[startvec + i])) {
da6d674e
MD
1732 val |= (1 << i);
1733 }
1734 }
1735 break;
1736 case 0x200 ... 0x23f: /* NVIC Set pend */
1737 offset += 0x80;
1738 /* fall through */
1739 case 0x280 ... 0x2bf: /* NVIC Clear pend */
1740 val = 0;
12fbf1a1 1741 startvec = 8 * (offset - 0x280) + NVIC_FIRST_IRQ; /* vector # */
da6d674e 1742 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
e1be0a57
PM
1743 if (s->vectors[startvec + i].pending &&
1744 (attrs.secure || s->itns[startvec + i])) {
da6d674e
MD
1745 val |= (1 << i);
1746 }
1747 }
1748 break;
1749 case 0x300 ... 0x33f: /* NVIC Active */
1750 val = 0;
12fbf1a1 1751 startvec = 8 * (offset - 0x300) + NVIC_FIRST_IRQ; /* vector # */
da6d674e
MD
1752
1753 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
e1be0a57
PM
1754 if (s->vectors[startvec + i].active &&
1755 (attrs.secure || s->itns[startvec + i])) {
da6d674e
MD
1756 val |= (1 << i);
1757 }
1758 }
1759 break;
1760 case 0x400 ... 0x5ef: /* NVIC Priority */
1761 val = 0;
1762 startvec = offset - 0x400 + NVIC_FIRST_IRQ; /* vector # */
1763
1764 for (i = 0; i < size && startvec + i < s->num_irq; i++) {
e1be0a57
PM
1765 if (attrs.secure || s->itns[startvec + i]) {
1766 val |= s->vectors[startvec + i].prio << (8 * i);
1767 }
da6d674e
MD
1768 }
1769 break;
e6a0d350 1770 case 0xd18 ... 0xd23: /* System Handler Priority (SHPR1, SHPR2, SHPR3) */
0e8153dd
AB
1771 val = 0;
1772 for (i = 0; i < size; i++) {
e6a0d350
PM
1773 unsigned hdlidx = (offset - 0xd14) + i;
1774 int sbank = shpr_bank(s, hdlidx, attrs);
1775
1776 if (sbank < 0) {
1777 continue;
1778 }
1779 val = deposit32(val, i * 8, 8, get_prio(s, hdlidx, sbank));
0e8153dd 1780 }
da6d674e 1781 break;
4b9774ef
PM
1782 case 0xd28 ... 0xd2b: /* Configurable Fault Status (CFSR) */
1783 /* The BFSR bits [15:8] are shared between security states
1784 * and we store them in the NS copy
1785 */
1786 val = s->cpu->env.v7m.cfsr[attrs.secure];
1787 val |= s->cpu->env.v7m.cfsr[M_REG_NS] & R_V7M_CFSR_BFSR_MASK;
1788 val = extract32(val, (offset - 0xd28) * 8, size * 8);
1789 break;
0e8153dd 1790 case 0xfe0 ... 0xfff: /* ID. */
2a29ddee 1791 if (offset & 3) {
da6d674e
MD
1792 val = 0;
1793 } else {
1794 val = nvic_id[(offset - 0xfe0) >> 2];
1795 }
1796 break;
1797 default:
1798 if (size == 4) {
45db7ba6 1799 val = nvic_readl(s, offset, attrs);
da6d674e
MD
1800 } else {
1801 qemu_log_mask(LOG_GUEST_ERROR,
1802 "NVIC: Bad read of size %d at offset 0x%x\n",
1803 size, offset);
1804 val = 0;
2a29ddee 1805 }
2a29ddee 1806 }
da6d674e
MD
1807
1808 trace_nvic_sysreg_read(addr, val, size);
eb578a23
PM
1809 *data = val;
1810 return MEMTX_OK;
2a29ddee
PM
1811}
1812
eb578a23
PM
1813static MemTxResult nvic_sysreg_write(void *opaque, hwaddr addr,
1814 uint64_t value, unsigned size,
1815 MemTxAttrs attrs)
2a29ddee 1816{
f797c075 1817 NVICState *s = (NVICState *)opaque;
2a29ddee 1818 uint32_t offset = addr;
da6d674e
MD
1819 unsigned i, startvec, end;
1820 unsigned setval = 0;
1821
1822 trace_nvic_sysreg_write(addr, value, size);
0e8153dd 1823
9d40cd8a 1824 if (attrs.user && !nvic_user_access_ok(s, addr, attrs)) {
eb578a23
PM
1825 /* Generate BusFault for unprivileged accesses */
1826 return MEMTX_ERROR;
1827 }
1828
0e8153dd 1829 switch (offset) {
da6d674e
MD
1830 case 0x100 ... 0x13f: /* NVIC Set enable */
1831 offset += 0x80;
1832 setval = 1;
1833 /* fall through */
1834 case 0x180 ... 0x1bf: /* NVIC Clear enable */
1835 startvec = 8 * (offset - 0x180) + NVIC_FIRST_IRQ;
1836
1837 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
e1be0a57
PM
1838 if (value & (1 << i) &&
1839 (attrs.secure || s->itns[startvec + i])) {
da6d674e
MD
1840 s->vectors[startvec + i].enabled = setval;
1841 }
1842 }
1843 nvic_irq_update(s);
eb578a23 1844 return MEMTX_OK;
da6d674e
MD
1845 case 0x200 ... 0x23f: /* NVIC Set pend */
1846 /* the special logic in armv7m_nvic_set_pending()
1847 * is not needed since IRQs are never escalated
1848 */
1849 offset += 0x80;
1850 setval = 1;
1851 /* fall through */
1852 case 0x280 ... 0x2bf: /* NVIC Clear pend */
1853 startvec = 8 * (offset - 0x280) + NVIC_FIRST_IRQ; /* vector # */
1854
1855 for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) {
e1be0a57
PM
1856 if (value & (1 << i) &&
1857 (attrs.secure || s->itns[startvec + i])) {
da6d674e
MD
1858 s->vectors[startvec + i].pending = setval;
1859 }
1860 }
1861 nvic_irq_update(s);
eb578a23 1862 return MEMTX_OK;
da6d674e 1863 case 0x300 ... 0x33f: /* NVIC Active */
eb578a23 1864 return MEMTX_OK; /* R/O */
da6d674e 1865 case 0x400 ... 0x5ef: /* NVIC Priority */
12fbf1a1 1866 startvec = (offset - 0x400) + NVIC_FIRST_IRQ; /* vector # */
da6d674e
MD
1867
1868 for (i = 0; i < size && startvec + i < s->num_irq; i++) {
e1be0a57 1869 if (attrs.secure || s->itns[startvec + i]) {
e6a0d350 1870 set_prio(s, startvec + i, false, (value >> (i * 8)) & 0xff);
e1be0a57 1871 }
da6d674e
MD
1872 }
1873 nvic_irq_update(s);
eb578a23 1874 return MEMTX_OK;
e6a0d350 1875 case 0xd18 ... 0xd23: /* System Handler Priority (SHPR1, SHPR2, SHPR3) */
0e8153dd 1876 for (i = 0; i < size; i++) {
da6d674e 1877 unsigned hdlidx = (offset - 0xd14) + i;
e6a0d350
PM
1878 int newprio = extract32(value, i * 8, 8);
1879 int sbank = shpr_bank(s, hdlidx, attrs);
1880
1881 if (sbank < 0) {
1882 continue;
1883 }
1884 set_prio(s, hdlidx, sbank, newprio);
0e8153dd 1885 }
da6d674e 1886 nvic_irq_update(s);
eb578a23 1887 return MEMTX_OK;
4b9774ef
PM
1888 case 0xd28 ... 0xd2b: /* Configurable Fault Status (CFSR) */
1889 /* All bits are W1C, so construct 32 bit value with 0s in
1890 * the parts not written by the access size
1891 */
1892 value <<= ((offset - 0xd28) * 8);
1893
1894 s->cpu->env.v7m.cfsr[attrs.secure] &= ~value;
1895 if (attrs.secure) {
1896 /* The BFSR bits [15:8] are shared between security states
1897 * and we store them in the NS copy.
1898 */
1899 s->cpu->env.v7m.cfsr[M_REG_NS] &= ~(value & R_V7M_CFSR_BFSR_MASK);
1900 }
1901 return MEMTX_OK;
0e8153dd 1902 }
2a29ddee 1903 if (size == 4) {
45db7ba6 1904 nvic_writel(s, offset, value, attrs);
eb578a23 1905 return MEMTX_OK;
2a29ddee 1906 }
e72e3ffc
PM
1907 qemu_log_mask(LOG_GUEST_ERROR,
1908 "NVIC: Bad write of size %d at offset 0x%x\n", size, offset);
eb578a23
PM
1909 /* This is UNPREDICTABLE; treat as RAZ/WI */
1910 return MEMTX_OK;
2a29ddee
PM
1911}
1912
1913static const MemoryRegionOps nvic_sysreg_ops = {
eb578a23
PM
1914 .read_with_attrs = nvic_sysreg_read,
1915 .write_with_attrs = nvic_sysreg_write,
2a29ddee
PM
1916 .endianness = DEVICE_NATIVE_ENDIAN,
1917};
1918
f104919d
PM
1919static MemTxResult nvic_sysreg_ns_write(void *opaque, hwaddr addr,
1920 uint64_t value, unsigned size,
1921 MemTxAttrs attrs)
1922{
62f01848
PM
1923 MemoryRegion *mr = opaque;
1924
f104919d
PM
1925 if (attrs.secure) {
1926 /* S accesses to the alias act like NS accesses to the real region */
1927 attrs.secure = 0;
62f01848 1928 return memory_region_dispatch_write(mr, addr, value, size, attrs);
f104919d
PM
1929 } else {
1930 /* NS attrs are RAZ/WI for privileged, and BusFault for user */
1931 if (attrs.user) {
1932 return MEMTX_ERROR;
1933 }
1934 return MEMTX_OK;
1935 }
1936}
1937
1938static MemTxResult nvic_sysreg_ns_read(void *opaque, hwaddr addr,
1939 uint64_t *data, unsigned size,
1940 MemTxAttrs attrs)
1941{
62f01848
PM
1942 MemoryRegion *mr = opaque;
1943
f104919d
PM
1944 if (attrs.secure) {
1945 /* S accesses to the alias act like NS accesses to the real region */
1946 attrs.secure = 0;
62f01848 1947 return memory_region_dispatch_read(mr, addr, data, size, attrs);
f104919d
PM
1948 } else {
1949 /* NS attrs are RAZ/WI for privileged, and BusFault for user */
1950 if (attrs.user) {
1951 return MEMTX_ERROR;
1952 }
1953 *data = 0;
1954 return MEMTX_OK;
1955 }
1956}
1957
1958static const MemoryRegionOps nvic_sysreg_ns_ops = {
1959 .read_with_attrs = nvic_sysreg_ns_read,
1960 .write_with_attrs = nvic_sysreg_ns_write,
1961 .endianness = DEVICE_NATIVE_ENDIAN,
1962};
1963
27f26bfe
PM
1964static MemTxResult nvic_systick_write(void *opaque, hwaddr addr,
1965 uint64_t value, unsigned size,
1966 MemTxAttrs attrs)
1967{
1968 NVICState *s = opaque;
1969 MemoryRegion *mr;
1970
1971 /* Direct the access to the correct systick */
1972 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systick[attrs.secure]), 0);
1973 return memory_region_dispatch_write(mr, addr, value, size, attrs);
1974}
1975
1976static MemTxResult nvic_systick_read(void *opaque, hwaddr addr,
1977 uint64_t *data, unsigned size,
1978 MemTxAttrs attrs)
1979{
1980 NVICState *s = opaque;
1981 MemoryRegion *mr;
1982
1983 /* Direct the access to the correct systick */
1984 mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->systick[attrs.secure]), 0);
1985 return memory_region_dispatch_read(mr, addr, data, size, attrs);
1986}
1987
1988static const MemoryRegionOps nvic_systick_ops = {
1989 .read_with_attrs = nvic_systick_read,
1990 .write_with_attrs = nvic_systick_write,
1991 .endianness = DEVICE_NATIVE_ENDIAN,
1992};
1993
da6d674e
MD
1994static int nvic_post_load(void *opaque, int version_id)
1995{
1996 NVICState *s = opaque;
1997 unsigned i;
331f4bae 1998 int resetprio;
da6d674e
MD
1999
2000 /* Check for out of range priority settings */
331f4bae
PM
2001 resetprio = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? -4 : -3;
2002
2003 if (s->vectors[ARMV7M_EXCP_RESET].prio != resetprio ||
da6d674e
MD
2004 s->vectors[ARMV7M_EXCP_NMI].prio != -2 ||
2005 s->vectors[ARMV7M_EXCP_HARD].prio != -1) {
2006 return 1;
2007 }
2008 for (i = ARMV7M_EXCP_MEM; i < s->num_irq; i++) {
2009 if (s->vectors[i].prio & ~0xff) {
2010 return 1;
2011 }
2012 }
2013
2014 nvic_recompute_state(s);
2015
2016 return 0;
2017}
2018
2019static const VMStateDescription vmstate_VecInfo = {
2020 .name = "armv7m_nvic_info",
2021 .version_id = 1,
2022 .minimum_version_id = 1,
2023 .fields = (VMStateField[]) {
2024 VMSTATE_INT16(prio, VecInfo),
2025 VMSTATE_UINT8(enabled, VecInfo),
2026 VMSTATE_UINT8(pending, VecInfo),
2027 VMSTATE_UINT8(active, VecInfo),
2028 VMSTATE_UINT8(level, VecInfo),
2029 VMSTATE_END_OF_LIST()
2030 }
2031};
2032
17906a16
PM
2033static bool nvic_security_needed(void *opaque)
2034{
2035 NVICState *s = opaque;
2036
2037 return arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY);
2038}
2039
2040static int nvic_security_post_load(void *opaque, int version_id)
2041{
2042 NVICState *s = opaque;
2043 int i;
2044
2045 /* Check for out of range priority settings */
331f4bae
PM
2046 if (s->sec_vectors[ARMV7M_EXCP_HARD].prio != -1
2047 && s->sec_vectors[ARMV7M_EXCP_HARD].prio != -3) {
2048 /* We can't cross-check against AIRCR.BFHFNMINS as we don't know
2049 * if the CPU state has been migrated yet; a mismatch won't
2050 * cause the emulation to blow up, though.
2051 */
17906a16
PM
2052 return 1;
2053 }
2054 for (i = ARMV7M_EXCP_MEM; i < ARRAY_SIZE(s->sec_vectors); i++) {
2055 if (s->sec_vectors[i].prio & ~0xff) {
2056 return 1;
2057 }
2058 }
2059 return 0;
2060}
2061
2062static const VMStateDescription vmstate_nvic_security = {
2063 .name = "nvic/m-security",
2064 .version_id = 1,
2065 .minimum_version_id = 1,
2066 .needed = nvic_security_needed,
2067 .post_load = &nvic_security_post_load,
2068 .fields = (VMStateField[]) {
2069 VMSTATE_STRUCT_ARRAY(sec_vectors, NVICState, NVIC_INTERNAL_VECTORS, 1,
2070 vmstate_VecInfo, VecInfo),
3b2e9344 2071 VMSTATE_UINT32(prigroup[M_REG_S], NVICState),
e1be0a57 2072 VMSTATE_BOOL_ARRAY(itns, NVICState, NVIC_MAX_VECTORS),
17906a16
PM
2073 VMSTATE_END_OF_LIST()
2074 }
2075};
2076
0797226c
JQ
2077static const VMStateDescription vmstate_nvic = {
2078 .name = "armv7m_nvic",
ff68dacb
PM
2079 .version_id = 4,
2080 .minimum_version_id = 4,
da6d674e 2081 .post_load = &nvic_post_load,
8f1e884b 2082 .fields = (VMStateField[]) {
da6d674e
MD
2083 VMSTATE_STRUCT_ARRAY(vectors, NVICState, NVIC_MAX_VECTORS, 1,
2084 vmstate_VecInfo, VecInfo),
3b2e9344 2085 VMSTATE_UINT32(prigroup[M_REG_NS], NVICState),
0797226c 2086 VMSTATE_END_OF_LIST()
17906a16
PM
2087 },
2088 .subsections = (const VMStateDescription*[]) {
2089 &vmstate_nvic_security,
2090 NULL
0797226c
JQ
2091 }
2092};
23e39294 2093
da6d674e
MD
2094static Property props_nvic[] = {
2095 /* Number of external IRQ lines (so excluding the 16 internal exceptions) */
2096 DEFINE_PROP_UINT32("num-irq", NVICState, num_irq, 64),
2097 DEFINE_PROP_END_OF_LIST()
2098};
2099
aecff692
PM
2100static void armv7m_nvic_reset(DeviceState *dev)
2101{
331f4bae 2102 int resetprio;
f797c075 2103 NVICState *s = NVIC(dev);
da6d674e 2104
8ff26a33
PM
2105 memset(s->vectors, 0, sizeof(s->vectors));
2106 memset(s->sec_vectors, 0, sizeof(s->sec_vectors));
2107 s->prigroup[M_REG_NS] = 0;
2108 s->prigroup[M_REG_S] = 0;
2109
da6d674e 2110 s->vectors[ARMV7M_EXCP_NMI].enabled = 1;
da6d674e
MD
2111 /* MEM, BUS, and USAGE are enabled through
2112 * the System Handler Control register
b3387ede 2113 */
da6d674e
MD
2114 s->vectors[ARMV7M_EXCP_SVC].enabled = 1;
2115 s->vectors[ARMV7M_EXCP_DEBUG].enabled = 1;
2116 s->vectors[ARMV7M_EXCP_PENDSV].enabled = 1;
2117 s->vectors[ARMV7M_EXCP_SYSTICK].enabled = 1;
2118
331f4bae
PM
2119 resetprio = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? -4 : -3;
2120 s->vectors[ARMV7M_EXCP_RESET].prio = resetprio;
da6d674e
MD
2121 s->vectors[ARMV7M_EXCP_NMI].prio = -2;
2122 s->vectors[ARMV7M_EXCP_HARD].prio = -1;
2123
17906a16
PM
2124 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
2125 s->sec_vectors[ARMV7M_EXCP_HARD].enabled = 1;
2126 s->sec_vectors[ARMV7M_EXCP_SVC].enabled = 1;
2127 s->sec_vectors[ARMV7M_EXCP_PENDSV].enabled = 1;
2128 s->sec_vectors[ARMV7M_EXCP_SYSTICK].enabled = 1;
2129
2130 /* AIRCR.BFHFNMINS resets to 0 so Secure HF is priority -1 (R_CMTC) */
2131 s->sec_vectors[ARMV7M_EXCP_HARD].prio = -1;
7208b426
PM
2132 /* If AIRCR.BFHFNMINS is 0 then NS HF is (effectively) disabled */
2133 s->vectors[ARMV7M_EXCP_HARD].enabled = 0;
2134 } else {
2135 s->vectors[ARMV7M_EXCP_HARD].enabled = 1;
17906a16
PM
2136 }
2137
da6d674e
MD
2138 /* Strictly speaking the reset handler should be enabled.
2139 * However, we don't simulate soft resets through the NVIC,
2140 * and the reset vector should never be pended.
2141 * So we leave it disabled to catch logic errors.
2142 */
2143
2144 s->exception_prio = NVIC_NOEXC_PRIO;
2145 s->vectpending = 0;
e93bc2ac 2146 s->vectpending_is_s_banked = false;
5255fcf8 2147 s->vectpending_prio = NVIC_NOEXC_PRIO;
e1be0a57
PM
2148
2149 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
2150 memset(s->itns, 0, sizeof(s->itns));
2151 } else {
2152 /* This state is constant and not guest accessible in a non-security
2153 * NVIC; we set the bits to true to avoid having to do a feature
2154 * bit check in the NVIC enable/pend/etc register accessors.
2155 */
2156 int i;
2157
2158 for (i = NVIC_FIRST_IRQ; i < ARRAY_SIZE(s->itns); i++) {
2159 s->itns[i] = true;
2160 }
2161 }
ff68dacb 2162}
da6d674e 2163
ff68dacb
PM
2164static void nvic_systick_trigger(void *opaque, int n, int level)
2165{
2166 NVICState *s = opaque;
2167
2168 if (level) {
2169 /* SysTick just asked us to pend its exception.
2170 * (This is different from an external interrupt line's
2171 * behaviour.)
27f26bfe
PM
2172 * n == 0 : NonSecure systick
2173 * n == 1 : Secure systick
ff68dacb 2174 */
27f26bfe 2175 armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK, n);
ff68dacb 2176 }
aecff692
PM
2177}
2178
53111180 2179static void armv7m_nvic_realize(DeviceState *dev, Error **errp)
9ee6e8bb 2180{
f797c075 2181 NVICState *s = NVIC(dev);
ff68dacb 2182 Error *err = NULL;
f104919d 2183 int regionlen;
9ee6e8bb 2184
d713ea6c
MD
2185 s->cpu = ARM_CPU(qemu_get_cpu(0));
2186 assert(s->cpu);
da6d674e
MD
2187
2188 if (s->num_irq > NVIC_MAX_IRQ) {
2189 error_setg(errp, "num-irq %d exceeds NVIC maximum", s->num_irq);
53111180
PM
2190 return;
2191 }
da6d674e
MD
2192
2193 qdev_init_gpio_in(dev, set_irq_level, s->num_irq);
2194
2195 /* include space for internal exception vectors */
2196 s->num_irq += NVIC_FIRST_IRQ;
2197
27f26bfe
PM
2198 object_property_set_bool(OBJECT(&s->systick[M_REG_NS]), true,
2199 "realized", &err);
ff68dacb
PM
2200 if (err != NULL) {
2201 error_propagate(errp, err);
2202 return;
2203 }
27f26bfe
PM
2204 sysbus_connect_irq(SYS_BUS_DEVICE(&s->systick[M_REG_NS]), 0,
2205 qdev_get_gpio_in_named(dev, "systick-trigger",
2206 M_REG_NS));
2207
2208 if (arm_feature(&s->cpu->env, ARM_FEATURE_M_SECURITY)) {
2209 /* We couldn't init the secure systick device in instance_init
2210 * as we didn't know then if the CPU had the security extensions;
2211 * so we have to do it here.
2212 */
2213 object_initialize(&s->systick[M_REG_S], sizeof(s->systick[M_REG_S]),
2214 TYPE_SYSTICK);
2215 qdev_set_parent_bus(DEVICE(&s->systick[M_REG_S]), sysbus_get_default());
2216
2217 object_property_set_bool(OBJECT(&s->systick[M_REG_S]), true,
2218 "realized", &err);
2219 if (err != NULL) {
2220 error_propagate(errp, err);
2221 return;
2222 }
2223 sysbus_connect_irq(SYS_BUS_DEVICE(&s->systick[M_REG_S]), 0,
2224 qdev_get_gpio_in_named(dev, "systick-trigger",
2225 M_REG_S));
2226 }
ff68dacb 2227
da6d674e
MD
2228 /* The NVIC and System Control Space (SCS) starts at 0xe000e000
2229 * and looks like this:
2230 * 0x004 - ICTR
ff68dacb 2231 * 0x010 - 0xff - systick
da6d674e
MD
2232 * 0x100..0x7ec - NVIC
2233 * 0x7f0..0xcff - Reserved
2234 * 0xd00..0xd3c - SCS registers
2235 * 0xd40..0xeff - Reserved or Not implemented
2236 * 0xf00 - STIR
f104919d
PM
2237 *
2238 * Some registers within this space are banked between security states.
2239 * In v8M there is a second range 0xe002e000..0xe002efff which is the
2240 * NonSecure alias SCS; secure accesses to this behave like NS accesses
2241 * to the main SCS range, and non-secure accesses (including when
2242 * the security extension is not implemented) are RAZ/WI.
2243 * Note that both the main SCS range and the alias range are defined
2244 * to be exempt from memory attribution (R_BLJT) and so the memory
2245 * transaction attribute always matches the current CPU security
2246 * state (attrs.secure == env->v7m.secure). In the nvic_sysreg_ns_ops
2247 * wrappers we change attrs.secure to indicate the NS access; so
2248 * generally code determining which banked register to use should
2249 * use attrs.secure; code determining actual behaviour of the system
2250 * should use env->v7m.secure.
2a29ddee 2251 */
f104919d
PM
2252 regionlen = arm_feature(&s->cpu->env, ARM_FEATURE_V8) ? 0x21000 : 0x1000;
2253 memory_region_init(&s->container, OBJECT(s), "nvic", regionlen);
2a29ddee
PM
2254 /* The system register region goes at the bottom of the priority
2255 * stack as it covers the whole page.
2256 */
1437c94b 2257 memory_region_init_io(&s->sysregmem, OBJECT(s), &nvic_sysreg_ops, s,
2a29ddee
PM
2258 "nvic_sysregs", 0x1000);
2259 memory_region_add_subregion(&s->container, 0, &s->sysregmem);
27f26bfe
PM
2260
2261 memory_region_init_io(&s->systickmem, OBJECT(s),
2262 &nvic_systick_ops, s,
2263 "nvic_systick", 0xe0);
2264
ff68dacb 2265 memory_region_add_subregion_overlap(&s->container, 0x10,
27f26bfe 2266 &s->systickmem, 1);
da6d674e 2267
f104919d
PM
2268 if (arm_feature(&s->cpu->env, ARM_FEATURE_V8)) {
2269 memory_region_init_io(&s->sysreg_ns_mem, OBJECT(s),
62f01848 2270 &nvic_sysreg_ns_ops, &s->sysregmem,
f104919d
PM
2271 "nvic_sysregs_ns", 0x1000);
2272 memory_region_add_subregion(&s->container, 0x20000, &s->sysreg_ns_mem);
27f26bfe
PM
2273 memory_region_init_io(&s->systick_ns_mem, OBJECT(s),
2274 &nvic_sysreg_ns_ops, &s->systickmem,
2275 "nvic_systick_ns", 0xe0);
2276 memory_region_add_subregion_overlap(&s->container, 0x20010,
2277 &s->systick_ns_mem, 1);
f104919d
PM
2278 }
2279
98957a94 2280 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->container);
9ee6e8bb 2281}
fe7e8758 2282
55e00a19
PM
2283static void armv7m_nvic_instance_init(Object *obj)
2284{
2285 /* We have a different default value for the num-irq property
2286 * than our superclass. This function runs after qdev init
2287 * has set the defaults from the Property array and before
2288 * any user-specified property setting, so just modify the
fae15286 2289 * value in the GICState struct.
55e00a19 2290 */
e192becd 2291 DeviceState *dev = DEVICE(obj);
f797c075 2292 NVICState *nvic = NVIC(obj);
da6d674e
MD
2293 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
2294
27f26bfe
PM
2295 object_initialize(&nvic->systick[M_REG_NS],
2296 sizeof(nvic->systick[M_REG_NS]), TYPE_SYSTICK);
2297 qdev_set_parent_bus(DEVICE(&nvic->systick[M_REG_NS]), sysbus_get_default());
2298 /* We can't initialize the secure systick here, as we don't know
2299 * yet if we need it.
2300 */
ff68dacb 2301
da6d674e 2302 sysbus_init_irq(sbd, &nvic->excpout);
e192becd 2303 qdev_init_gpio_out_named(dev, &nvic->sysresetreq, "SYSRESETREQ", 1);
27f26bfe
PM
2304 qdev_init_gpio_in_named(dev, nvic_systick_trigger, "systick-trigger",
2305 M_REG_NUM_BANKS);
55e00a19 2306}
39bffca2 2307
999e12bb
AL
2308static void armv7m_nvic_class_init(ObjectClass *klass, void *data)
2309{
39bffca2 2310 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 2311
39bffca2 2312 dc->vmsd = &vmstate_nvic;
da6d674e 2313 dc->props = props_nvic;
aecff692 2314 dc->reset = armv7m_nvic_reset;
53111180 2315 dc->realize = armv7m_nvic_realize;
999e12bb
AL
2316}
2317
8c43a6f0 2318static const TypeInfo armv7m_nvic_info = {
1e8cae4d 2319 .name = TYPE_NVIC,
da6d674e 2320 .parent = TYPE_SYS_BUS_DEVICE,
55e00a19 2321 .instance_init = armv7m_nvic_instance_init,
f797c075 2322 .instance_size = sizeof(NVICState),
39bffca2 2323 .class_init = armv7m_nvic_class_init,
da6d674e 2324 .class_size = sizeof(SysBusDeviceClass),
a32134aa
ML
2325};
2326
83f7d43a 2327static void armv7m_nvic_register_types(void)
fe7e8758 2328{
39bffca2 2329 type_register_static(&armv7m_nvic_info);
fe7e8758
PB
2330}
2331
83f7d43a 2332type_init(armv7m_nvic_register_types)
This page took 1.026148 seconds and 4 git commands to generate.