]>
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" |
6bf436cf | 20 | #include "hw/arm/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 | */ | |
50 | #define NVIC_FIRST_IRQ 16 | |
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 | |
57 | ||
2a29ddee PM |
58 | static const uint8_t nvic_id[] = { |
59 | 0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1 | |
60 | }; | |
61 | ||
da6d674e MD |
62 | static int nvic_pending_prio(NVICState *s) |
63 | { | |
64 | /* return the priority of the current pending interrupt, | |
65 | * or NVIC_NOEXC_PRIO if no interrupt is pending | |
66 | */ | |
67 | return s->vectpending ? s->vectors[s->vectpending].prio : NVIC_NOEXC_PRIO; | |
68 | } | |
69 | ||
70 | /* Return the value of the ISCR RETTOBASE bit: | |
71 | * 1 if there is exactly one active exception | |
72 | * 0 if there is more than one active exception | |
73 | * UNKNOWN if there are no active exceptions (we choose 1, | |
74 | * which matches the choice Cortex-M3 is documented as making). | |
75 | * | |
76 | * NB: some versions of the documentation talk about this | |
77 | * counting "active exceptions other than the one shown by IPSR"; | |
78 | * this is only different in the obscure corner case where guest | |
79 | * code has manually deactivated an exception and is about | |
80 | * to fail an exception-return integrity check. The definition | |
81 | * above is the one from the v8M ARM ARM and is also in line | |
82 | * with the behaviour documented for the Cortex-M3. | |
83 | */ | |
84 | static bool nvic_rettobase(NVICState *s) | |
85 | { | |
86 | int irq, nhand = 0; | |
87 | ||
88 | for (irq = ARMV7M_EXCP_RESET; irq < s->num_irq; irq++) { | |
89 | if (s->vectors[irq].active) { | |
90 | nhand++; | |
91 | if (nhand == 2) { | |
92 | return 0; | |
93 | } | |
94 | } | |
95 | } | |
96 | ||
97 | return 1; | |
98 | } | |
99 | ||
100 | /* Return the value of the ISCR ISRPENDING bit: | |
101 | * 1 if an external interrupt is pending | |
102 | * 0 if no external interrupt is pending | |
103 | */ | |
104 | static bool nvic_isrpending(NVICState *s) | |
105 | { | |
106 | int irq; | |
107 | ||
108 | /* We can shortcut if the highest priority pending interrupt | |
109 | * happens to be external or if there is nothing pending. | |
110 | */ | |
111 | if (s->vectpending > NVIC_FIRST_IRQ) { | |
112 | return true; | |
113 | } | |
114 | if (s->vectpending == 0) { | |
115 | return false; | |
116 | } | |
117 | ||
118 | for (irq = NVIC_FIRST_IRQ; irq < s->num_irq; irq++) { | |
119 | if (s->vectors[irq].pending) { | |
120 | return true; | |
121 | } | |
122 | } | |
123 | return false; | |
124 | } | |
125 | ||
126 | /* Return a mask word which clears the subpriority bits from | |
127 | * a priority value for an M-profile exception, leaving only | |
128 | * the group priority. | |
129 | */ | |
130 | static inline uint32_t nvic_gprio_mask(NVICState *s) | |
131 | { | |
132 | return ~0U << (s->prigroup + 1); | |
133 | } | |
134 | ||
135 | /* Recompute vectpending and exception_prio */ | |
136 | static void nvic_recompute_state(NVICState *s) | |
137 | { | |
138 | int i; | |
139 | int pend_prio = NVIC_NOEXC_PRIO; | |
140 | int active_prio = NVIC_NOEXC_PRIO; | |
141 | int pend_irq = 0; | |
142 | ||
143 | for (i = 1; i < s->num_irq; i++) { | |
144 | VecInfo *vec = &s->vectors[i]; | |
145 | ||
146 | if (vec->enabled && vec->pending && vec->prio < pend_prio) { | |
147 | pend_prio = vec->prio; | |
148 | pend_irq = i; | |
149 | } | |
150 | if (vec->active && vec->prio < active_prio) { | |
151 | active_prio = vec->prio; | |
152 | } | |
153 | } | |
154 | ||
155 | s->vectpending = pend_irq; | |
156 | s->exception_prio = active_prio & nvic_gprio_mask(s); | |
157 | ||
158 | trace_nvic_recompute_state(s->vectpending, s->exception_prio); | |
159 | } | |
160 | ||
161 | /* Return the current execution priority of the CPU | |
162 | * (equivalent to the pseudocode ExecutionPriority function). | |
163 | * This is a value between -2 (NMI priority) and NVIC_NOEXC_PRIO. | |
164 | */ | |
165 | static inline int nvic_exec_prio(NVICState *s) | |
166 | { | |
167 | CPUARMState *env = &s->cpu->env; | |
168 | int running; | |
169 | ||
170 | if (env->daif & PSTATE_F) { /* FAULTMASK */ | |
171 | running = -1; | |
172 | } else if (env->daif & PSTATE_I) { /* PRIMASK */ | |
173 | running = 0; | |
174 | } else if (env->v7m.basepri > 0) { | |
175 | running = env->v7m.basepri & nvic_gprio_mask(s); | |
176 | } else { | |
177 | running = NVIC_NOEXC_PRIO; /* lower than any possible priority */ | |
178 | } | |
179 | /* consider priority of active handler */ | |
180 | return MIN(running, s->exception_prio); | |
181 | } | |
182 | ||
7ecdaa4a PM |
183 | bool armv7m_nvic_can_take_pending_exception(void *opaque) |
184 | { | |
185 | NVICState *s = opaque; | |
186 | ||
187 | return nvic_exec_prio(s) > nvic_pending_prio(s); | |
188 | } | |
189 | ||
da6d674e MD |
190 | /* caller must call nvic_irq_update() after this */ |
191 | static void set_prio(NVICState *s, unsigned irq, uint8_t prio) | |
192 | { | |
193 | assert(irq > ARMV7M_EXCP_NMI); /* only use for configurable prios */ | |
194 | assert(irq < s->num_irq); | |
195 | ||
196 | s->vectors[irq].prio = prio; | |
197 | ||
198 | trace_nvic_set_prio(irq, prio); | |
199 | } | |
200 | ||
201 | /* Recompute state and assert irq line accordingly. | |
202 | * Must be called after changes to: | |
203 | * vec->active, vec->enabled, vec->pending or vec->prio for any vector | |
204 | * prigroup | |
205 | */ | |
206 | static void nvic_irq_update(NVICState *s) | |
207 | { | |
208 | int lvl; | |
209 | int pend_prio; | |
210 | ||
211 | nvic_recompute_state(s); | |
212 | pend_prio = nvic_pending_prio(s); | |
213 | ||
214 | /* Raise NVIC output if this IRQ would be taken, except that we | |
215 | * ignore the effects of the BASEPRI, FAULTMASK and PRIMASK (which | |
216 | * will be checked for in arm_v7m_cpu_exec_interrupt()); changes | |
217 | * to those CPU registers don't cause us to recalculate the NVIC | |
218 | * pending info. | |
219 | */ | |
220 | lvl = (pend_prio < s->exception_prio); | |
221 | trace_nvic_irq_update(s->vectpending, pend_prio, s->exception_prio, lvl); | |
222 | qemu_set_irq(s->excpout, lvl); | |
223 | } | |
224 | ||
225 | static void armv7m_nvic_clear_pending(void *opaque, int irq) | |
226 | { | |
227 | NVICState *s = (NVICState *)opaque; | |
228 | VecInfo *vec; | |
229 | ||
230 | assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq); | |
231 | ||
232 | vec = &s->vectors[irq]; | |
233 | trace_nvic_clear_pending(irq, vec->enabled, vec->prio); | |
234 | if (vec->pending) { | |
235 | vec->pending = 0; | |
236 | nvic_irq_update(s); | |
237 | } | |
238 | } | |
239 | ||
9ee6e8bb PB |
240 | void armv7m_nvic_set_pending(void *opaque, int irq) |
241 | { | |
f797c075 | 242 | NVICState *s = (NVICState *)opaque; |
da6d674e MD |
243 | VecInfo *vec; |
244 | ||
245 | assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq); | |
246 | ||
247 | vec = &s->vectors[irq]; | |
248 | trace_nvic_set_pending(irq, vec->enabled, vec->prio); | |
a73c98e1 MD |
249 | |
250 | ||
251 | if (irq >= ARMV7M_EXCP_HARD && irq < ARMV7M_EXCP_PENDSV) { | |
252 | /* If a synchronous exception is pending then it may be | |
253 | * escalated to HardFault if: | |
254 | * * it is equal or lower priority to current execution | |
255 | * * it is disabled | |
256 | * (ie we need to take it immediately but we can't do so). | |
257 | * Asynchronous exceptions (and interrupts) simply remain pending. | |
258 | * | |
259 | * For QEMU, we don't have any imprecise (asynchronous) faults, | |
260 | * so we can assume that PREFETCH_ABORT and DATA_ABORT are always | |
261 | * synchronous. | |
262 | * Debug exceptions are awkward because only Debug exceptions | |
263 | * resulting from the BKPT instruction should be escalated, | |
264 | * but we don't currently implement any Debug exceptions other | |
265 | * than those that result from BKPT, so we treat all debug exceptions | |
266 | * as needing escalation. | |
267 | * | |
268 | * This all means we can identify whether to escalate based only on | |
269 | * the exception number and don't (yet) need the caller to explicitly | |
270 | * tell us whether this exception is synchronous or not. | |
271 | */ | |
272 | int running = nvic_exec_prio(s); | |
273 | bool escalate = false; | |
274 | ||
275 | if (vec->prio >= running) { | |
276 | trace_nvic_escalate_prio(irq, vec->prio, running); | |
277 | escalate = true; | |
278 | } else if (!vec->enabled) { | |
279 | trace_nvic_escalate_disabled(irq); | |
280 | escalate = true; | |
281 | } | |
282 | ||
283 | if (escalate) { | |
284 | if (running < 0) { | |
285 | /* We want to escalate to HardFault but we can't take a | |
286 | * synchronous HardFault at this point either. This is a | |
287 | * Lockup condition due to a guest bug. We don't model | |
288 | * Lockup, so report via cpu_abort() instead. | |
289 | */ | |
290 | cpu_abort(&s->cpu->parent_obj, | |
291 | "Lockup: can't escalate %d to HardFault " | |
292 | "(current priority %d)\n", irq, running); | |
293 | } | |
294 | ||
295 | /* We can do the escalation, so we take HardFault instead */ | |
296 | irq = ARMV7M_EXCP_HARD; | |
297 | vec = &s->vectors[irq]; | |
298 | s->cpu->env.v7m.hfsr |= R_V7M_HFSR_FORCED_MASK; | |
299 | } | |
300 | } | |
301 | ||
da6d674e MD |
302 | if (!vec->pending) { |
303 | vec->pending = 1; | |
304 | nvic_irq_update(s); | |
305 | } | |
9ee6e8bb PB |
306 | } |
307 | ||
308 | /* Make pending IRQ active. */ | |
a5d82355 | 309 | void armv7m_nvic_acknowledge_irq(void *opaque) |
9ee6e8bb | 310 | { |
f797c075 | 311 | NVICState *s = (NVICState *)opaque; |
da6d674e MD |
312 | CPUARMState *env = &s->cpu->env; |
313 | const int pending = s->vectpending; | |
314 | const int running = nvic_exec_prio(s); | |
315 | int pendgroupprio; | |
316 | VecInfo *vec; | |
317 | ||
318 | assert(pending > ARMV7M_EXCP_RESET && pending < s->num_irq); | |
319 | ||
320 | vec = &s->vectors[pending]; | |
321 | ||
322 | assert(vec->enabled); | |
323 | assert(vec->pending); | |
324 | ||
325 | pendgroupprio = vec->prio & nvic_gprio_mask(s); | |
326 | assert(pendgroupprio < running); | |
327 | ||
328 | trace_nvic_acknowledge_irq(pending, vec->prio); | |
329 | ||
330 | vec->active = 1; | |
331 | vec->pending = 0; | |
332 | ||
333 | env->v7m.exception = s->vectpending; | |
334 | ||
335 | nvic_irq_update(s); | |
9ee6e8bb PB |
336 | } |
337 | ||
aa488fe3 | 338 | int armv7m_nvic_complete_irq(void *opaque, int irq) |
9ee6e8bb | 339 | { |
f797c075 | 340 | NVICState *s = (NVICState *)opaque; |
da6d674e | 341 | VecInfo *vec; |
aa488fe3 | 342 | int ret; |
da6d674e MD |
343 | |
344 | assert(irq > ARMV7M_EXCP_RESET && irq < s->num_irq); | |
345 | ||
346 | vec = &s->vectors[irq]; | |
347 | ||
348 | trace_nvic_complete_irq(irq); | |
349 | ||
aa488fe3 PM |
350 | if (!vec->active) { |
351 | /* Tell the caller this was an illegal exception return */ | |
352 | return -1; | |
353 | } | |
354 | ||
355 | ret = nvic_rettobase(s); | |
356 | ||
da6d674e MD |
357 | vec->active = 0; |
358 | if (vec->level) { | |
359 | /* Re-pend the exception if it's still held high; only | |
360 | * happens for extenal IRQs | |
361 | */ | |
362 | assert(irq >= NVIC_FIRST_IRQ); | |
363 | vec->pending = 1; | |
364 | } | |
365 | ||
366 | nvic_irq_update(s); | |
aa488fe3 PM |
367 | |
368 | return ret; | |
da6d674e MD |
369 | } |
370 | ||
371 | /* callback when external interrupt line is changed */ | |
372 | static void set_irq_level(void *opaque, int n, int level) | |
373 | { | |
374 | NVICState *s = opaque; | |
375 | VecInfo *vec; | |
376 | ||
377 | n += NVIC_FIRST_IRQ; | |
378 | ||
379 | assert(n >= NVIC_FIRST_IRQ && n < s->num_irq); | |
380 | ||
381 | trace_nvic_set_irq_level(n, level); | |
382 | ||
383 | /* The pending status of an external interrupt is | |
384 | * latched on rising edge and exception handler return. | |
385 | * | |
386 | * Pulsing the IRQ will always run the handler | |
387 | * once, and the handler will re-run until the | |
388 | * level is low when the handler completes. | |
389 | */ | |
390 | vec = &s->vectors[n]; | |
391 | if (level != vec->level) { | |
392 | vec->level = level; | |
393 | if (level) { | |
394 | armv7m_nvic_set_pending(s, n); | |
395 | } | |
396 | } | |
9ee6e8bb PB |
397 | } |
398 | ||
f797c075 | 399 | static uint32_t nvic_readl(NVICState *s, uint32_t offset) |
9ee6e8bb | 400 | { |
d713ea6c | 401 | ARMCPU *cpu = s->cpu; |
9ee6e8bb | 402 | uint32_t val; |
9ee6e8bb PB |
403 | |
404 | switch (offset) { | |
405 | case 4: /* Interrupt Control Type. */ | |
da6d674e | 406 | return ((s->num_irq - NVIC_FIRST_IRQ) / 32) - 1; |
9ee6e8bb | 407 | case 0xd00: /* CPUID Base. */ |
e3da9921 | 408 | return cpu->midr; |
e03ba136 | 409 | case 0xd04: /* Interrupt Control State. */ |
9ee6e8bb | 410 | /* VECTACTIVE */ |
b06c262b | 411 | val = cpu->env.v7m.exception; |
9ee6e8bb | 412 | /* VECTPENDING */ |
da6d674e MD |
413 | val |= (s->vectpending & 0xff) << 12; |
414 | /* ISRPENDING - set if any external IRQ is pending */ | |
415 | if (nvic_isrpending(s)) { | |
416 | val |= (1 << 22); | |
417 | } | |
418 | /* RETTOBASE - set if only one handler is active */ | |
419 | if (nvic_rettobase(s)) { | |
420 | val |= (1 << 11); | |
9ee6e8bb PB |
421 | } |
422 | /* PENDSTSET */ | |
da6d674e | 423 | if (s->vectors[ARMV7M_EXCP_SYSTICK].pending) { |
9ee6e8bb | 424 | val |= (1 << 26); |
da6d674e | 425 | } |
9ee6e8bb | 426 | /* PENDSVSET */ |
da6d674e | 427 | if (s->vectors[ARMV7M_EXCP_PENDSV].pending) { |
9ee6e8bb | 428 | val |= (1 << 28); |
da6d674e | 429 | } |
9ee6e8bb | 430 | /* NMIPENDSET */ |
da6d674e | 431 | if (s->vectors[ARMV7M_EXCP_NMI].pending) { |
9ee6e8bb | 432 | val |= (1 << 31); |
da6d674e MD |
433 | } |
434 | /* ISRPREEMPT not implemented */ | |
9ee6e8bb PB |
435 | return val; |
436 | case 0xd08: /* Vector Table Offset. */ | |
4917cf44 | 437 | return cpu->env.v7m.vecbase; |
9ee6e8bb | 438 | case 0xd0c: /* Application Interrupt/Reset Control. */ |
1004102a | 439 | return 0xfa050000 | (s->prigroup << 8); |
9ee6e8bb PB |
440 | case 0xd10: /* System Control. */ |
441 | /* TODO: Implement SLEEPONEXIT. */ | |
442 | return 0; | |
443 | case 0xd14: /* Configuration Control. */ | |
e6b33209 | 444 | return cpu->env.v7m.ccr; |
9ee6e8bb PB |
445 | case 0xd24: /* System Handler Status. */ |
446 | val = 0; | |
da6d674e MD |
447 | if (s->vectors[ARMV7M_EXCP_MEM].active) { |
448 | val |= (1 << 0); | |
449 | } | |
450 | if (s->vectors[ARMV7M_EXCP_BUS].active) { | |
451 | val |= (1 << 1); | |
452 | } | |
453 | if (s->vectors[ARMV7M_EXCP_USAGE].active) { | |
454 | val |= (1 << 3); | |
455 | } | |
456 | if (s->vectors[ARMV7M_EXCP_SVC].active) { | |
457 | val |= (1 << 7); | |
458 | } | |
459 | if (s->vectors[ARMV7M_EXCP_DEBUG].active) { | |
460 | val |= (1 << 8); | |
461 | } | |
462 | if (s->vectors[ARMV7M_EXCP_PENDSV].active) { | |
463 | val |= (1 << 10); | |
464 | } | |
465 | if (s->vectors[ARMV7M_EXCP_SYSTICK].active) { | |
466 | val |= (1 << 11); | |
467 | } | |
468 | if (s->vectors[ARMV7M_EXCP_USAGE].pending) { | |
469 | val |= (1 << 12); | |
470 | } | |
471 | if (s->vectors[ARMV7M_EXCP_MEM].pending) { | |
472 | val |= (1 << 13); | |
473 | } | |
474 | if (s->vectors[ARMV7M_EXCP_BUS].pending) { | |
475 | val |= (1 << 14); | |
476 | } | |
477 | if (s->vectors[ARMV7M_EXCP_SVC].pending) { | |
478 | val |= (1 << 15); | |
479 | } | |
480 | if (s->vectors[ARMV7M_EXCP_MEM].enabled) { | |
481 | val |= (1 << 16); | |
482 | } | |
483 | if (s->vectors[ARMV7M_EXCP_BUS].enabled) { | |
484 | val |= (1 << 17); | |
485 | } | |
486 | if (s->vectors[ARMV7M_EXCP_USAGE].enabled) { | |
487 | val |= (1 << 18); | |
488 | } | |
9ee6e8bb PB |
489 | return val; |
490 | case 0xd28: /* Configurable Fault Status. */ | |
e6b33209 | 491 | return cpu->env.v7m.cfsr; |
9ee6e8bb | 492 | case 0xd2c: /* Hard Fault Status. */ |
e6b33209 | 493 | return cpu->env.v7m.hfsr; |
9ee6e8bb | 494 | case 0xd30: /* Debug Fault Status. */ |
e6b33209 MD |
495 | return cpu->env.v7m.dfsr; |
496 | case 0xd34: /* MMFAR MemManage Fault Address */ | |
497 | return cpu->env.v7m.mmfar; | |
9ee6e8bb | 498 | case 0xd38: /* Bus Fault Address. */ |
e6b33209 | 499 | return cpu->env.v7m.bfar; |
9ee6e8bb PB |
500 | case 0xd3c: /* Aux Fault Status. */ |
501 | /* TODO: Implement fault status registers. */ | |
e6b33209 MD |
502 | qemu_log_mask(LOG_UNIMP, |
503 | "Aux Fault status registers unimplemented\n"); | |
e72e3ffc | 504 | return 0; |
9ee6e8bb PB |
505 | case 0xd40: /* PFR0. */ |
506 | return 0x00000030; | |
507 | case 0xd44: /* PRF1. */ | |
508 | return 0x00000200; | |
509 | case 0xd48: /* DFR0. */ | |
510 | return 0x00100000; | |
511 | case 0xd4c: /* AFR0. */ | |
512 | return 0x00000000; | |
513 | case 0xd50: /* MMFR0. */ | |
514 | return 0x00000030; | |
515 | case 0xd54: /* MMFR1. */ | |
516 | return 0x00000000; | |
517 | case 0xd58: /* MMFR2. */ | |
518 | return 0x00000000; | |
519 | case 0xd5c: /* MMFR3. */ | |
520 | return 0x00000000; | |
521 | case 0xd60: /* ISAR0. */ | |
522 | return 0x01141110; | |
523 | case 0xd64: /* ISAR1. */ | |
524 | return 0x02111000; | |
525 | case 0xd68: /* ISAR2. */ | |
526 | return 0x21112231; | |
527 | case 0xd6c: /* ISAR3. */ | |
528 | return 0x01111110; | |
529 | case 0xd70: /* ISAR4. */ | |
530 | return 0x01310102; | |
531 | /* TODO: Implement debug registers. */ | |
29c483a5 MD |
532 | case 0xd90: /* MPU_TYPE */ |
533 | /* Unified MPU; if the MPU is not present this value is zero */ | |
534 | return cpu->pmsav7_dregion << 8; | |
535 | break; | |
536 | case 0xd94: /* MPU_CTRL */ | |
537 | return cpu->env.v7m.mpu_ctrl; | |
538 | case 0xd98: /* MPU_RNR */ | |
539 | return cpu->env.cp15.c6_rgnr; | |
540 | case 0xd9c: /* MPU_RBAR */ | |
541 | case 0xda4: /* MPU_RBAR_A1 */ | |
542 | case 0xdac: /* MPU_RBAR_A2 */ | |
543 | case 0xdb4: /* MPU_RBAR_A3 */ | |
544 | { | |
545 | int region = cpu->env.cp15.c6_rgnr; | |
546 | ||
547 | if (region >= cpu->pmsav7_dregion) { | |
548 | return 0; | |
549 | } | |
550 | return (cpu->env.pmsav7.drbar[region] & 0x1f) | (region & 0xf); | |
551 | } | |
552 | case 0xda0: /* MPU_RASR */ | |
553 | case 0xda8: /* MPU_RASR_A1 */ | |
554 | case 0xdb0: /* MPU_RASR_A2 */ | |
555 | case 0xdb8: /* MPU_RASR_A3 */ | |
556 | { | |
557 | int region = cpu->env.cp15.c6_rgnr; | |
558 | ||
559 | if (region >= cpu->pmsav7_dregion) { | |
560 | return 0; | |
561 | } | |
562 | return ((cpu->env.pmsav7.dracr[region] & 0xffff) << 16) | | |
563 | (cpu->env.pmsav7.drsr[region] & 0xffff); | |
564 | } | |
9ee6e8bb | 565 | default: |
e72e3ffc PM |
566 | qemu_log_mask(LOG_GUEST_ERROR, "NVIC: Bad read offset 0x%x\n", offset); |
567 | return 0; | |
9ee6e8bb PB |
568 | } |
569 | } | |
570 | ||
f797c075 | 571 | static void nvic_writel(NVICState *s, uint32_t offset, uint32_t value) |
9ee6e8bb | 572 | { |
d713ea6c | 573 | ARMCPU *cpu = s->cpu; |
ff68dacb | 574 | |
9ee6e8bb | 575 | switch (offset) { |
9ee6e8bb PB |
576 | case 0xd04: /* Interrupt Control State. */ |
577 | if (value & (1 << 31)) { | |
578 | armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI); | |
579 | } | |
580 | if (value & (1 << 28)) { | |
581 | armv7m_nvic_set_pending(s, ARMV7M_EXCP_PENDSV); | |
582 | } else if (value & (1 << 27)) { | |
da6d674e | 583 | armv7m_nvic_clear_pending(s, ARMV7M_EXCP_PENDSV); |
9ee6e8bb PB |
584 | } |
585 | if (value & (1 << 26)) { | |
586 | armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK); | |
587 | } else if (value & (1 << 25)) { | |
da6d674e | 588 | armv7m_nvic_clear_pending(s, ARMV7M_EXCP_SYSTICK); |
9ee6e8bb PB |
589 | } |
590 | break; | |
591 | case 0xd08: /* Vector Table Offset. */ | |
4917cf44 | 592 | cpu->env.v7m.vecbase = value & 0xffffff80; |
9ee6e8bb PB |
593 | break; |
594 | case 0xd0c: /* Application Interrupt/Reset Control. */ | |
595 | if ((value >> 16) == 0x05fa) { | |
e192becd MD |
596 | if (value & 4) { |
597 | qemu_irq_pulse(s->sysresetreq); | |
598 | } | |
9ee6e8bb | 599 | if (value & 2) { |
14790f73 MD |
600 | qemu_log_mask(LOG_GUEST_ERROR, |
601 | "Setting VECTCLRACTIVE when not in DEBUG mode " | |
602 | "is UNPREDICTABLE\n"); | |
9ee6e8bb | 603 | } |
e192becd | 604 | if (value & 1) { |
14790f73 MD |
605 | qemu_log_mask(LOG_GUEST_ERROR, |
606 | "Setting VECTRESET when not in DEBUG mode " | |
607 | "is UNPREDICTABLE\n"); | |
9ee6e8bb | 608 | } |
1004102a | 609 | s->prigroup = extract32(value, 8, 3); |
da6d674e | 610 | nvic_irq_update(s); |
9ee6e8bb PB |
611 | } |
612 | break; | |
613 | case 0xd10: /* System Control. */ | |
9ee6e8bb | 614 | /* TODO: Implement control registers. */ |
e6b33209 MD |
615 | qemu_log_mask(LOG_UNIMP, "NVIC: SCR unimplemented\n"); |
616 | break; | |
617 | case 0xd14: /* Configuration Control. */ | |
618 | /* Enforce RAZ/WI on reserved and must-RAZ/WI bits */ | |
619 | value &= (R_V7M_CCR_STKALIGN_MASK | | |
620 | R_V7M_CCR_BFHFNMIGN_MASK | | |
621 | R_V7M_CCR_DIV_0_TRP_MASK | | |
622 | R_V7M_CCR_UNALIGN_TRP_MASK | | |
623 | R_V7M_CCR_USERSETMPEND_MASK | | |
624 | R_V7M_CCR_NONBASETHRDENA_MASK); | |
625 | ||
626 | cpu->env.v7m.ccr = value; | |
e72e3ffc | 627 | break; |
9ee6e8bb | 628 | case 0xd24: /* System Handler Control. */ |
5db53e35 PM |
629 | s->vectors[ARMV7M_EXCP_MEM].active = (value & (1 << 0)) != 0; |
630 | s->vectors[ARMV7M_EXCP_BUS].active = (value & (1 << 1)) != 0; | |
631 | s->vectors[ARMV7M_EXCP_USAGE].active = (value & (1 << 3)) != 0; | |
632 | s->vectors[ARMV7M_EXCP_SVC].active = (value & (1 << 7)) != 0; | |
633 | s->vectors[ARMV7M_EXCP_DEBUG].active = (value & (1 << 8)) != 0; | |
634 | s->vectors[ARMV7M_EXCP_PENDSV].active = (value & (1 << 10)) != 0; | |
635 | s->vectors[ARMV7M_EXCP_SYSTICK].active = (value & (1 << 11)) != 0; | |
636 | s->vectors[ARMV7M_EXCP_USAGE].pending = (value & (1 << 12)) != 0; | |
637 | s->vectors[ARMV7M_EXCP_MEM].pending = (value & (1 << 13)) != 0; | |
638 | s->vectors[ARMV7M_EXCP_BUS].pending = (value & (1 << 14)) != 0; | |
639 | s->vectors[ARMV7M_EXCP_SVC].pending = (value & (1 << 15)) != 0; | |
da6d674e MD |
640 | s->vectors[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0; |
641 | s->vectors[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0; | |
642 | s->vectors[ARMV7M_EXCP_USAGE].enabled = (value & (1 << 18)) != 0; | |
643 | nvic_irq_update(s); | |
9ee6e8bb PB |
644 | break; |
645 | case 0xd28: /* Configurable Fault Status. */ | |
e6b33209 MD |
646 | cpu->env.v7m.cfsr &= ~value; /* W1C */ |
647 | break; | |
9ee6e8bb | 648 | case 0xd2c: /* Hard Fault Status. */ |
e6b33209 MD |
649 | cpu->env.v7m.hfsr &= ~value; /* W1C */ |
650 | break; | |
9ee6e8bb | 651 | case 0xd30: /* Debug Fault Status. */ |
e6b33209 MD |
652 | cpu->env.v7m.dfsr &= ~value; /* W1C */ |
653 | break; | |
9ee6e8bb | 654 | case 0xd34: /* Mem Manage Address. */ |
e6b33209 MD |
655 | cpu->env.v7m.mmfar = value; |
656 | return; | |
9ee6e8bb | 657 | case 0xd38: /* Bus Fault Address. */ |
e6b33209 MD |
658 | cpu->env.v7m.bfar = value; |
659 | return; | |
9ee6e8bb | 660 | case 0xd3c: /* Aux Fault Status. */ |
e72e3ffc | 661 | qemu_log_mask(LOG_UNIMP, |
e6b33209 | 662 | "NVIC: Aux fault status registers unimplemented\n"); |
e72e3ffc | 663 | break; |
29c483a5 MD |
664 | case 0xd90: /* MPU_TYPE */ |
665 | return; /* RO */ | |
666 | case 0xd94: /* MPU_CTRL */ | |
667 | if ((value & | |
668 | (R_V7M_MPU_CTRL_HFNMIENA_MASK | R_V7M_MPU_CTRL_ENABLE_MASK)) | |
669 | == R_V7M_MPU_CTRL_HFNMIENA_MASK) { | |
670 | qemu_log_mask(LOG_GUEST_ERROR, "MPU_CTRL: HFNMIENA and !ENABLE is " | |
671 | "UNPREDICTABLE\n"); | |
672 | } | |
673 | cpu->env.v7m.mpu_ctrl = value & (R_V7M_MPU_CTRL_ENABLE_MASK | | |
674 | R_V7M_MPU_CTRL_HFNMIENA_MASK | | |
675 | R_V7M_MPU_CTRL_PRIVDEFENA_MASK); | |
676 | tlb_flush(CPU(cpu)); | |
677 | break; | |
678 | case 0xd98: /* MPU_RNR */ | |
679 | if (value >= cpu->pmsav7_dregion) { | |
680 | qemu_log_mask(LOG_GUEST_ERROR, "MPU region out of range %" | |
681 | PRIu32 "/%" PRIu32 "\n", | |
682 | value, cpu->pmsav7_dregion); | |
683 | } else { | |
684 | cpu->env.cp15.c6_rgnr = value; | |
685 | } | |
686 | break; | |
687 | case 0xd9c: /* MPU_RBAR */ | |
688 | case 0xda4: /* MPU_RBAR_A1 */ | |
689 | case 0xdac: /* MPU_RBAR_A2 */ | |
690 | case 0xdb4: /* MPU_RBAR_A3 */ | |
691 | { | |
692 | int region; | |
693 | ||
694 | if (value & (1 << 4)) { | |
695 | /* VALID bit means use the region number specified in this | |
696 | * value and also update MPU_RNR.REGION with that value. | |
697 | */ | |
698 | region = extract32(value, 0, 4); | |
699 | if (region >= cpu->pmsav7_dregion) { | |
700 | qemu_log_mask(LOG_GUEST_ERROR, | |
701 | "MPU region out of range %u/%" PRIu32 "\n", | |
702 | region, cpu->pmsav7_dregion); | |
703 | return; | |
704 | } | |
705 | cpu->env.cp15.c6_rgnr = region; | |
706 | } else { | |
707 | region = cpu->env.cp15.c6_rgnr; | |
708 | } | |
709 | ||
710 | if (region >= cpu->pmsav7_dregion) { | |
711 | return; | |
712 | } | |
713 | ||
714 | cpu->env.pmsav7.drbar[region] = value & ~0x1f; | |
715 | tlb_flush(CPU(cpu)); | |
716 | break; | |
717 | } | |
718 | case 0xda0: /* MPU_RASR */ | |
719 | case 0xda8: /* MPU_RASR_A1 */ | |
720 | case 0xdb0: /* MPU_RASR_A2 */ | |
721 | case 0xdb8: /* MPU_RASR_A3 */ | |
722 | { | |
723 | int region = cpu->env.cp15.c6_rgnr; | |
724 | ||
725 | if (region >= cpu->pmsav7_dregion) { | |
726 | return; | |
727 | } | |
728 | ||
729 | cpu->env.pmsav7.drsr[region] = value & 0xff3f; | |
730 | cpu->env.pmsav7.dracr[region] = (value >> 16) & 0x173f; | |
731 | tlb_flush(CPU(cpu)); | |
732 | break; | |
733 | } | |
2a29ddee | 734 | case 0xf00: /* Software Triggered Interrupt Register */ |
da6d674e | 735 | { |
bdd04fc7 | 736 | /* user mode can only write to STIR if CCR.USERSETMPEND permits it */ |
da6d674e MD |
737 | int excnum = (value & 0x1ff) + NVIC_FIRST_IRQ; |
738 | if (excnum < s->num_irq && | |
bdd04fc7 MD |
739 | (arm_current_el(&cpu->env) || |
740 | (cpu->env.v7m.ccr & R_V7M_CCR_USERSETMPEND_MASK))) { | |
da6d674e | 741 | armv7m_nvic_set_pending(s, excnum); |
2a29ddee PM |
742 | } |
743 | break; | |
da6d674e | 744 | } |
9ee6e8bb | 745 | default: |
e72e3ffc PM |
746 | qemu_log_mask(LOG_GUEST_ERROR, |
747 | "NVIC: Bad write offset 0x%x\n", offset); | |
9ee6e8bb PB |
748 | } |
749 | } | |
750 | ||
a8170e5e | 751 | static uint64_t nvic_sysreg_read(void *opaque, hwaddr addr, |
2a29ddee PM |
752 | unsigned size) |
753 | { | |
f797c075 | 754 | NVICState *s = (NVICState *)opaque; |
2a29ddee | 755 | uint32_t offset = addr; |
da6d674e | 756 | unsigned i, startvec, end; |
0e8153dd AB |
757 | uint32_t val; |
758 | ||
759 | switch (offset) { | |
da6d674e MD |
760 | /* reads of set and clear both return the status */ |
761 | case 0x100 ... 0x13f: /* NVIC Set enable */ | |
762 | offset += 0x80; | |
763 | /* fall through */ | |
764 | case 0x180 ... 0x1bf: /* NVIC Clear enable */ | |
765 | val = 0; | |
766 | startvec = offset - 0x180 + NVIC_FIRST_IRQ; /* vector # */ | |
767 | ||
768 | for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { | |
769 | if (s->vectors[startvec + i].enabled) { | |
770 | val |= (1 << i); | |
771 | } | |
772 | } | |
773 | break; | |
774 | case 0x200 ... 0x23f: /* NVIC Set pend */ | |
775 | offset += 0x80; | |
776 | /* fall through */ | |
777 | case 0x280 ... 0x2bf: /* NVIC Clear pend */ | |
778 | val = 0; | |
779 | startvec = offset - 0x280 + NVIC_FIRST_IRQ; /* vector # */ | |
780 | for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { | |
781 | if (s->vectors[startvec + i].pending) { | |
782 | val |= (1 << i); | |
783 | } | |
784 | } | |
785 | break; | |
786 | case 0x300 ... 0x33f: /* NVIC Active */ | |
787 | val = 0; | |
788 | startvec = offset - 0x300 + NVIC_FIRST_IRQ; /* vector # */ | |
789 | ||
790 | for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { | |
791 | if (s->vectors[startvec + i].active) { | |
792 | val |= (1 << i); | |
793 | } | |
794 | } | |
795 | break; | |
796 | case 0x400 ... 0x5ef: /* NVIC Priority */ | |
797 | val = 0; | |
798 | startvec = offset - 0x400 + NVIC_FIRST_IRQ; /* vector # */ | |
799 | ||
800 | for (i = 0; i < size && startvec + i < s->num_irq; i++) { | |
801 | val |= s->vectors[startvec + i].prio << (8 * i); | |
802 | } | |
803 | break; | |
0e8153dd AB |
804 | case 0xd18 ... 0xd23: /* System Handler Priority. */ |
805 | val = 0; | |
806 | for (i = 0; i < size; i++) { | |
da6d674e | 807 | val |= s->vectors[(offset - 0xd14) + i].prio << (i * 8); |
0e8153dd | 808 | } |
da6d674e | 809 | break; |
0e8153dd | 810 | case 0xfe0 ... 0xfff: /* ID. */ |
2a29ddee | 811 | if (offset & 3) { |
da6d674e MD |
812 | val = 0; |
813 | } else { | |
814 | val = nvic_id[(offset - 0xfe0) >> 2]; | |
815 | } | |
816 | break; | |
817 | default: | |
818 | if (size == 4) { | |
819 | val = nvic_readl(s, offset); | |
820 | } else { | |
821 | qemu_log_mask(LOG_GUEST_ERROR, | |
822 | "NVIC: Bad read of size %d at offset 0x%x\n", | |
823 | size, offset); | |
824 | val = 0; | |
2a29ddee | 825 | } |
2a29ddee | 826 | } |
da6d674e MD |
827 | |
828 | trace_nvic_sysreg_read(addr, val, size); | |
829 | return val; | |
2a29ddee PM |
830 | } |
831 | ||
a8170e5e | 832 | static void nvic_sysreg_write(void *opaque, hwaddr addr, |
2a29ddee PM |
833 | uint64_t value, unsigned size) |
834 | { | |
f797c075 | 835 | NVICState *s = (NVICState *)opaque; |
2a29ddee | 836 | uint32_t offset = addr; |
da6d674e MD |
837 | unsigned i, startvec, end; |
838 | unsigned setval = 0; | |
839 | ||
840 | trace_nvic_sysreg_write(addr, value, size); | |
0e8153dd AB |
841 | |
842 | switch (offset) { | |
da6d674e MD |
843 | case 0x100 ... 0x13f: /* NVIC Set enable */ |
844 | offset += 0x80; | |
845 | setval = 1; | |
846 | /* fall through */ | |
847 | case 0x180 ... 0x1bf: /* NVIC Clear enable */ | |
848 | startvec = 8 * (offset - 0x180) + NVIC_FIRST_IRQ; | |
849 | ||
850 | for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { | |
851 | if (value & (1 << i)) { | |
852 | s->vectors[startvec + i].enabled = setval; | |
853 | } | |
854 | } | |
855 | nvic_irq_update(s); | |
856 | return; | |
857 | case 0x200 ... 0x23f: /* NVIC Set pend */ | |
858 | /* the special logic in armv7m_nvic_set_pending() | |
859 | * is not needed since IRQs are never escalated | |
860 | */ | |
861 | offset += 0x80; | |
862 | setval = 1; | |
863 | /* fall through */ | |
864 | case 0x280 ... 0x2bf: /* NVIC Clear pend */ | |
865 | startvec = 8 * (offset - 0x280) + NVIC_FIRST_IRQ; /* vector # */ | |
866 | ||
867 | for (i = 0, end = size * 8; i < end && startvec + i < s->num_irq; i++) { | |
868 | if (value & (1 << i)) { | |
869 | s->vectors[startvec + i].pending = setval; | |
870 | } | |
871 | } | |
872 | nvic_irq_update(s); | |
873 | return; | |
874 | case 0x300 ... 0x33f: /* NVIC Active */ | |
875 | return; /* R/O */ | |
876 | case 0x400 ... 0x5ef: /* NVIC Priority */ | |
877 | startvec = 8 * (offset - 0x400) + NVIC_FIRST_IRQ; /* vector # */ | |
878 | ||
879 | for (i = 0; i < size && startvec + i < s->num_irq; i++) { | |
880 | set_prio(s, startvec + i, (value >> (i * 8)) & 0xff); | |
881 | } | |
882 | nvic_irq_update(s); | |
883 | return; | |
0e8153dd AB |
884 | case 0xd18 ... 0xd23: /* System Handler Priority. */ |
885 | for (i = 0; i < size; i++) { | |
da6d674e MD |
886 | unsigned hdlidx = (offset - 0xd14) + i; |
887 | set_prio(s, hdlidx, (value >> (i * 8)) & 0xff); | |
0e8153dd | 888 | } |
da6d674e | 889 | nvic_irq_update(s); |
0e8153dd AB |
890 | return; |
891 | } | |
2a29ddee | 892 | if (size == 4) { |
0e8153dd | 893 | nvic_writel(s, offset, value); |
2a29ddee PM |
894 | return; |
895 | } | |
e72e3ffc PM |
896 | qemu_log_mask(LOG_GUEST_ERROR, |
897 | "NVIC: Bad write of size %d at offset 0x%x\n", size, offset); | |
2a29ddee PM |
898 | } |
899 | ||
900 | static const MemoryRegionOps nvic_sysreg_ops = { | |
901 | .read = nvic_sysreg_read, | |
902 | .write = nvic_sysreg_write, | |
903 | .endianness = DEVICE_NATIVE_ENDIAN, | |
904 | }; | |
905 | ||
da6d674e MD |
906 | static int nvic_post_load(void *opaque, int version_id) |
907 | { | |
908 | NVICState *s = opaque; | |
909 | unsigned i; | |
910 | ||
911 | /* Check for out of range priority settings */ | |
912 | if (s->vectors[ARMV7M_EXCP_RESET].prio != -3 || | |
913 | s->vectors[ARMV7M_EXCP_NMI].prio != -2 || | |
914 | s->vectors[ARMV7M_EXCP_HARD].prio != -1) { | |
915 | return 1; | |
916 | } | |
917 | for (i = ARMV7M_EXCP_MEM; i < s->num_irq; i++) { | |
918 | if (s->vectors[i].prio & ~0xff) { | |
919 | return 1; | |
920 | } | |
921 | } | |
922 | ||
923 | nvic_recompute_state(s); | |
924 | ||
925 | return 0; | |
926 | } | |
927 | ||
928 | static const VMStateDescription vmstate_VecInfo = { | |
929 | .name = "armv7m_nvic_info", | |
930 | .version_id = 1, | |
931 | .minimum_version_id = 1, | |
932 | .fields = (VMStateField[]) { | |
933 | VMSTATE_INT16(prio, VecInfo), | |
934 | VMSTATE_UINT8(enabled, VecInfo), | |
935 | VMSTATE_UINT8(pending, VecInfo), | |
936 | VMSTATE_UINT8(active, VecInfo), | |
937 | VMSTATE_UINT8(level, VecInfo), | |
938 | VMSTATE_END_OF_LIST() | |
939 | } | |
940 | }; | |
941 | ||
0797226c JQ |
942 | static const VMStateDescription vmstate_nvic = { |
943 | .name = "armv7m_nvic", | |
ff68dacb PM |
944 | .version_id = 4, |
945 | .minimum_version_id = 4, | |
da6d674e | 946 | .post_load = &nvic_post_load, |
8f1e884b | 947 | .fields = (VMStateField[]) { |
da6d674e MD |
948 | VMSTATE_STRUCT_ARRAY(vectors, NVICState, NVIC_MAX_VECTORS, 1, |
949 | vmstate_VecInfo, VecInfo), | |
1004102a | 950 | VMSTATE_UINT32(prigroup, NVICState), |
0797226c JQ |
951 | VMSTATE_END_OF_LIST() |
952 | } | |
953 | }; | |
23e39294 | 954 | |
da6d674e MD |
955 | static Property props_nvic[] = { |
956 | /* Number of external IRQ lines (so excluding the 16 internal exceptions) */ | |
957 | DEFINE_PROP_UINT32("num-irq", NVICState, num_irq, 64), | |
958 | DEFINE_PROP_END_OF_LIST() | |
959 | }; | |
960 | ||
aecff692 PM |
961 | static void armv7m_nvic_reset(DeviceState *dev) |
962 | { | |
f797c075 | 963 | NVICState *s = NVIC(dev); |
da6d674e MD |
964 | |
965 | s->vectors[ARMV7M_EXCP_NMI].enabled = 1; | |
966 | s->vectors[ARMV7M_EXCP_HARD].enabled = 1; | |
967 | /* MEM, BUS, and USAGE are enabled through | |
968 | * the System Handler Control register | |
b3387ede | 969 | */ |
da6d674e MD |
970 | s->vectors[ARMV7M_EXCP_SVC].enabled = 1; |
971 | s->vectors[ARMV7M_EXCP_DEBUG].enabled = 1; | |
972 | s->vectors[ARMV7M_EXCP_PENDSV].enabled = 1; | |
973 | s->vectors[ARMV7M_EXCP_SYSTICK].enabled = 1; | |
974 | ||
975 | s->vectors[ARMV7M_EXCP_RESET].prio = -3; | |
976 | s->vectors[ARMV7M_EXCP_NMI].prio = -2; | |
977 | s->vectors[ARMV7M_EXCP_HARD].prio = -1; | |
978 | ||
979 | /* Strictly speaking the reset handler should be enabled. | |
980 | * However, we don't simulate soft resets through the NVIC, | |
981 | * and the reset vector should never be pended. | |
982 | * So we leave it disabled to catch logic errors. | |
983 | */ | |
984 | ||
985 | s->exception_prio = NVIC_NOEXC_PRIO; | |
986 | s->vectpending = 0; | |
ff68dacb | 987 | } |
da6d674e | 988 | |
ff68dacb PM |
989 | static void nvic_systick_trigger(void *opaque, int n, int level) |
990 | { | |
991 | NVICState *s = opaque; | |
992 | ||
993 | if (level) { | |
994 | /* SysTick just asked us to pend its exception. | |
995 | * (This is different from an external interrupt line's | |
996 | * behaviour.) | |
997 | */ | |
998 | armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK); | |
999 | } | |
aecff692 PM |
1000 | } |
1001 | ||
53111180 | 1002 | static void armv7m_nvic_realize(DeviceState *dev, Error **errp) |
9ee6e8bb | 1003 | { |
f797c075 | 1004 | NVICState *s = NVIC(dev); |
ff68dacb PM |
1005 | SysBusDevice *systick_sbd; |
1006 | Error *err = NULL; | |
9ee6e8bb | 1007 | |
d713ea6c MD |
1008 | s->cpu = ARM_CPU(qemu_get_cpu(0)); |
1009 | assert(s->cpu); | |
da6d674e MD |
1010 | |
1011 | if (s->num_irq > NVIC_MAX_IRQ) { | |
1012 | error_setg(errp, "num-irq %d exceeds NVIC maximum", s->num_irq); | |
53111180 PM |
1013 | return; |
1014 | } | |
da6d674e MD |
1015 | |
1016 | qdev_init_gpio_in(dev, set_irq_level, s->num_irq); | |
1017 | ||
1018 | /* include space for internal exception vectors */ | |
1019 | s->num_irq += NVIC_FIRST_IRQ; | |
1020 | ||
ff68dacb PM |
1021 | object_property_set_bool(OBJECT(&s->systick), true, "realized", &err); |
1022 | if (err != NULL) { | |
1023 | error_propagate(errp, err); | |
1024 | return; | |
1025 | } | |
1026 | systick_sbd = SYS_BUS_DEVICE(&s->systick); | |
1027 | sysbus_connect_irq(systick_sbd, 0, | |
1028 | qdev_get_gpio_in_named(dev, "systick-trigger", 0)); | |
1029 | ||
da6d674e MD |
1030 | /* The NVIC and System Control Space (SCS) starts at 0xe000e000 |
1031 | * and looks like this: | |
1032 | * 0x004 - ICTR | |
ff68dacb | 1033 | * 0x010 - 0xff - systick |
da6d674e MD |
1034 | * 0x100..0x7ec - NVIC |
1035 | * 0x7f0..0xcff - Reserved | |
1036 | * 0xd00..0xd3c - SCS registers | |
1037 | * 0xd40..0xeff - Reserved or Not implemented | |
1038 | * 0xf00 - STIR | |
1039 | * | |
1040 | * At the moment there is only one thing in the container region, | |
1041 | * but we leave it in place to allow us to pull systick out into | |
1042 | * its own device object later. | |
2a29ddee | 1043 | */ |
1437c94b | 1044 | memory_region_init(&s->container, OBJECT(s), "nvic", 0x1000); |
2a29ddee PM |
1045 | /* The system register region goes at the bottom of the priority |
1046 | * stack as it covers the whole page. | |
1047 | */ | |
1437c94b | 1048 | memory_region_init_io(&s->sysregmem, OBJECT(s), &nvic_sysreg_ops, s, |
2a29ddee PM |
1049 | "nvic_sysregs", 0x1000); |
1050 | memory_region_add_subregion(&s->container, 0, &s->sysregmem); | |
ff68dacb PM |
1051 | memory_region_add_subregion_overlap(&s->container, 0x10, |
1052 | sysbus_mmio_get_region(systick_sbd, 0), | |
1053 | 1); | |
da6d674e | 1054 | |
98957a94 | 1055 | sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->container); |
9ee6e8bb | 1056 | } |
fe7e8758 | 1057 | |
55e00a19 PM |
1058 | static void armv7m_nvic_instance_init(Object *obj) |
1059 | { | |
1060 | /* We have a different default value for the num-irq property | |
1061 | * than our superclass. This function runs after qdev init | |
1062 | * has set the defaults from the Property array and before | |
1063 | * any user-specified property setting, so just modify the | |
fae15286 | 1064 | * value in the GICState struct. |
55e00a19 | 1065 | */ |
e192becd | 1066 | DeviceState *dev = DEVICE(obj); |
f797c075 | 1067 | NVICState *nvic = NVIC(obj); |
da6d674e MD |
1068 | SysBusDevice *sbd = SYS_BUS_DEVICE(obj); |
1069 | ||
ff68dacb PM |
1070 | object_initialize(&nvic->systick, sizeof(nvic->systick), TYPE_SYSTICK); |
1071 | qdev_set_parent_bus(DEVICE(&nvic->systick), sysbus_get_default()); | |
1072 | ||
da6d674e | 1073 | sysbus_init_irq(sbd, &nvic->excpout); |
e192becd | 1074 | qdev_init_gpio_out_named(dev, &nvic->sysresetreq, "SYSRESETREQ", 1); |
ff68dacb | 1075 | qdev_init_gpio_in_named(dev, nvic_systick_trigger, "systick-trigger", 1); |
55e00a19 | 1076 | } |
39bffca2 | 1077 | |
999e12bb AL |
1078 | static void armv7m_nvic_class_init(ObjectClass *klass, void *data) |
1079 | { | |
39bffca2 | 1080 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb | 1081 | |
39bffca2 | 1082 | dc->vmsd = &vmstate_nvic; |
da6d674e | 1083 | dc->props = props_nvic; |
aecff692 | 1084 | dc->reset = armv7m_nvic_reset; |
53111180 | 1085 | dc->realize = armv7m_nvic_realize; |
999e12bb AL |
1086 | } |
1087 | ||
8c43a6f0 | 1088 | static const TypeInfo armv7m_nvic_info = { |
1e8cae4d | 1089 | .name = TYPE_NVIC, |
da6d674e | 1090 | .parent = TYPE_SYS_BUS_DEVICE, |
55e00a19 | 1091 | .instance_init = armv7m_nvic_instance_init, |
f797c075 | 1092 | .instance_size = sizeof(NVICState), |
39bffca2 | 1093 | .class_init = armv7m_nvic_class_init, |
da6d674e | 1094 | .class_size = sizeof(SysBusDeviceClass), |
a32134aa ML |
1095 | }; |
1096 | ||
83f7d43a | 1097 | static void armv7m_nvic_register_types(void) |
fe7e8758 | 1098 | { |
39bffca2 | 1099 | type_register_static(&armv7m_nvic_info); |
fe7e8758 PB |
1100 | } |
1101 | ||
83f7d43a | 1102 | type_init(armv7m_nvic_register_types) |