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