2 * ARM Nested Vectored Interrupt Controller
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
9 * The ARMv7M System controller is fairly tightly tied in with the
10 * NVIC. Much of that is also implemented here.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu-common.h"
17 #include "hw/sysbus.h"
18 #include "qemu/timer.h"
19 #include "hw/arm/arm.h"
20 #include "exec/address-spaces.h"
21 #include "gic_internal.h"
33 MemoryRegion sysregmem
;
34 MemoryRegion gic_iomem_alias
;
35 MemoryRegion container
;
40 #define TYPE_NVIC "armv7m_nvic"
43 * @parent_reset: the parent class' reset handler.
45 * A model of the v7M NVIC and System Controller
47 typedef struct NVICClass
{
49 ARMGICClass parent_class
;
51 DeviceRealize parent_realize
;
52 void (*parent_reset
)(DeviceState
*dev
);
55 #define NVIC_CLASS(klass) \
56 OBJECT_CLASS_CHECK(NVICClass, (klass), TYPE_NVIC)
57 #define NVIC_GET_CLASS(obj) \
58 OBJECT_GET_CLASS(NVICClass, (obj), TYPE_NVIC)
60 OBJECT_CHECK(nvic_state, (obj), TYPE_NVIC)
62 static const uint8_t nvic_id
[] = {
63 0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1
66 /* qemu timers run at 1GHz. We want something closer to 1MHz. */
67 #define SYSTICK_SCALE 1000ULL
69 #define SYSTICK_ENABLE (1 << 0)
70 #define SYSTICK_TICKINT (1 << 1)
71 #define SYSTICK_CLKSOURCE (1 << 2)
72 #define SYSTICK_COUNTFLAG (1 << 16)
74 int system_clock_scale
;
76 /* Conversion factor from qemu timer to SysTick frequencies. */
77 static inline int64_t systick_scale(nvic_state
*s
)
79 if (s
->systick
.control
& SYSTICK_CLKSOURCE
)
80 return system_clock_scale
;
85 static void systick_reload(nvic_state
*s
, int reset
)
87 /* The Cortex-M3 Devices Generic User Guide says that "When the
88 * ENABLE bit is set to 1, the counter loads the RELOAD value from the
89 * SYST RVR register and then counts down". So, we need to check the
90 * ENABLE bit before reloading the value.
92 if ((s
->systick
.control
& SYSTICK_ENABLE
) == 0) {
97 s
->systick
.tick
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
98 s
->systick
.tick
+= (s
->systick
.reload
+ 1) * systick_scale(s
);
99 timer_mod(s
->systick
.timer
, s
->systick
.tick
);
102 static void systick_timer_tick(void * opaque
)
104 nvic_state
*s
= (nvic_state
*)opaque
;
105 s
->systick
.control
|= SYSTICK_COUNTFLAG
;
106 if (s
->systick
.control
& SYSTICK_TICKINT
) {
107 /* Trigger the interrupt. */
108 armv7m_nvic_set_pending(s
, ARMV7M_EXCP_SYSTICK
);
110 if (s
->systick
.reload
== 0) {
111 s
->systick
.control
&= ~SYSTICK_ENABLE
;
113 systick_reload(s
, 0);
117 static void systick_reset(nvic_state
*s
)
119 s
->systick
.control
= 0;
120 s
->systick
.reload
= 0;
122 timer_del(s
->systick
.timer
);
125 /* The external routines use the hardware vector numbering, ie. the first
126 IRQ is #16. The internal GIC routines use #32 as the first IRQ. */
127 void armv7m_nvic_set_pending(void *opaque
, int irq
)
129 nvic_state
*s
= (nvic_state
*)opaque
;
132 gic_set_pending_private(&s
->gic
, 0, irq
);
135 /* Make pending IRQ active. */
136 int armv7m_nvic_acknowledge_irq(void *opaque
)
138 nvic_state
*s
= (nvic_state
*)opaque
;
141 irq
= gic_acknowledge_irq(&s
->gic
, 0, MEMTXATTRS_UNSPECIFIED
);
143 hw_error("Interrupt but no vector\n");
149 void armv7m_nvic_complete_irq(void *opaque
, int irq
)
151 nvic_state
*s
= (nvic_state
*)opaque
;
154 gic_complete_irq(&s
->gic
, 0, irq
, MEMTXATTRS_UNSPECIFIED
);
157 static uint32_t nvic_readl(nvic_state
*s
, uint32_t offset
)
159 ARMCPU
*cpu
= s
->cpu
;
164 case 4: /* Interrupt Control Type. */
165 return (s
->num_irq
/ 32) - 1;
166 case 0x10: /* SysTick Control and Status. */
167 val
= s
->systick
.control
;
168 s
->systick
.control
&= ~SYSTICK_COUNTFLAG
;
170 case 0x14: /* SysTick Reload Value. */
171 return s
->systick
.reload
;
172 case 0x18: /* SysTick Current Value. */
175 if ((s
->systick
.control
& SYSTICK_ENABLE
) == 0)
177 t
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
178 if (t
>= s
->systick
.tick
)
180 val
= ((s
->systick
.tick
- (t
+ 1)) / systick_scale(s
)) + 1;
181 /* The interrupt in triggered when the timer reaches zero.
182 However the counter is not reloaded until the next clock
183 tick. This is a hack to return zero during the first tick. */
184 if (val
> s
->systick
.reload
)
188 case 0x1c: /* SysTick Calibration Value. */
190 case 0xd00: /* CPUID Base. */
192 case 0xd04: /* Interrupt Control State. */
194 val
= cpu
->env
.v7m
.exception
;
197 } else if (val
>= 32) {
201 if (s
->gic
.current_pending
[0] != 1023)
202 val
|= (s
->gic
.current_pending
[0] << 12);
203 /* ISRPENDING and RETTOBASE */
204 for (irq
= 32; irq
< s
->num_irq
; irq
++) {
205 if (s
->gic
.irq_state
[irq
].pending
) {
209 if (irq
!= cpu
->env
.v7m
.exception
&& s
->gic
.irq_state
[irq
].active
) {
214 if (s
->gic
.irq_state
[ARMV7M_EXCP_SYSTICK
].pending
)
217 if (s
->gic
.irq_state
[ARMV7M_EXCP_PENDSV
].pending
)
220 if (s
->gic
.irq_state
[ARMV7M_EXCP_NMI
].pending
)
223 case 0xd08: /* Vector Table Offset. */
224 return cpu
->env
.v7m
.vecbase
;
225 case 0xd0c: /* Application Interrupt/Reset Control. */
227 case 0xd10: /* System Control. */
228 /* TODO: Implement SLEEPONEXIT. */
230 case 0xd14: /* Configuration Control. */
231 return cpu
->env
.v7m
.ccr
;
232 case 0xd24: /* System Handler Status. */
234 if (s
->gic
.irq_state
[ARMV7M_EXCP_MEM
].active
) val
|= (1 << 0);
235 if (s
->gic
.irq_state
[ARMV7M_EXCP_BUS
].active
) val
|= (1 << 1);
236 if (s
->gic
.irq_state
[ARMV7M_EXCP_USAGE
].active
) val
|= (1 << 3);
237 if (s
->gic
.irq_state
[ARMV7M_EXCP_SVC
].active
) val
|= (1 << 7);
238 if (s
->gic
.irq_state
[ARMV7M_EXCP_DEBUG
].active
) val
|= (1 << 8);
239 if (s
->gic
.irq_state
[ARMV7M_EXCP_PENDSV
].active
) val
|= (1 << 10);
240 if (s
->gic
.irq_state
[ARMV7M_EXCP_SYSTICK
].active
) val
|= (1 << 11);
241 if (s
->gic
.irq_state
[ARMV7M_EXCP_USAGE
].pending
) val
|= (1 << 12);
242 if (s
->gic
.irq_state
[ARMV7M_EXCP_MEM
].pending
) val
|= (1 << 13);
243 if (s
->gic
.irq_state
[ARMV7M_EXCP_BUS
].pending
) val
|= (1 << 14);
244 if (s
->gic
.irq_state
[ARMV7M_EXCP_SVC
].pending
) val
|= (1 << 15);
245 if (s
->gic
.irq_state
[ARMV7M_EXCP_MEM
].enabled
) val
|= (1 << 16);
246 if (s
->gic
.irq_state
[ARMV7M_EXCP_BUS
].enabled
) val
|= (1 << 17);
247 if (s
->gic
.irq_state
[ARMV7M_EXCP_USAGE
].enabled
) val
|= (1 << 18);
249 case 0xd28: /* Configurable Fault Status. */
250 return cpu
->env
.v7m
.cfsr
;
251 case 0xd2c: /* Hard Fault Status. */
252 return cpu
->env
.v7m
.hfsr
;
253 case 0xd30: /* Debug Fault Status. */
254 return cpu
->env
.v7m
.dfsr
;
255 case 0xd34: /* MMFAR MemManage Fault Address */
256 return cpu
->env
.v7m
.mmfar
;
257 case 0xd38: /* Bus Fault Address. */
258 return cpu
->env
.v7m
.bfar
;
259 case 0xd3c: /* Aux Fault Status. */
260 /* TODO: Implement fault status registers. */
261 qemu_log_mask(LOG_UNIMP
,
262 "Aux Fault status registers unimplemented\n");
264 case 0xd40: /* PFR0. */
266 case 0xd44: /* PRF1. */
268 case 0xd48: /* DFR0. */
270 case 0xd4c: /* AFR0. */
272 case 0xd50: /* MMFR0. */
274 case 0xd54: /* MMFR1. */
276 case 0xd58: /* MMFR2. */
278 case 0xd5c: /* MMFR3. */
280 case 0xd60: /* ISAR0. */
282 case 0xd64: /* ISAR1. */
284 case 0xd68: /* ISAR2. */
286 case 0xd6c: /* ISAR3. */
288 case 0xd70: /* ISAR4. */
290 /* TODO: Implement debug registers. */
292 qemu_log_mask(LOG_GUEST_ERROR
, "NVIC: Bad read offset 0x%x\n", offset
);
297 static void nvic_writel(nvic_state
*s
, uint32_t offset
, uint32_t value
)
299 ARMCPU
*cpu
= s
->cpu
;
302 case 0x10: /* SysTick Control and Status. */
303 oldval
= s
->systick
.control
;
304 s
->systick
.control
&= 0xfffffff8;
305 s
->systick
.control
|= value
& 7;
306 if ((oldval
^ value
) & SYSTICK_ENABLE
) {
307 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
308 if (value
& SYSTICK_ENABLE
) {
309 if (s
->systick
.tick
) {
310 s
->systick
.tick
+= now
;
311 timer_mod(s
->systick
.timer
, s
->systick
.tick
);
313 systick_reload(s
, 1);
316 timer_del(s
->systick
.timer
);
317 s
->systick
.tick
-= now
;
318 if (s
->systick
.tick
< 0)
321 } else if ((oldval
^ value
) & SYSTICK_CLKSOURCE
) {
322 /* This is a hack. Force the timer to be reloaded
323 when the reference clock is changed. */
324 systick_reload(s
, 1);
327 case 0x14: /* SysTick Reload Value. */
328 s
->systick
.reload
= value
;
330 case 0x18: /* SysTick Current Value. Writes reload the timer. */
331 systick_reload(s
, 1);
332 s
->systick
.control
&= ~SYSTICK_COUNTFLAG
;
334 case 0xd04: /* Interrupt Control State. */
335 if (value
& (1 << 31)) {
336 armv7m_nvic_set_pending(s
, ARMV7M_EXCP_NMI
);
338 if (value
& (1 << 28)) {
339 armv7m_nvic_set_pending(s
, ARMV7M_EXCP_PENDSV
);
340 } else if (value
& (1 << 27)) {
341 s
->gic
.irq_state
[ARMV7M_EXCP_PENDSV
].pending
= 0;
344 if (value
& (1 << 26)) {
345 armv7m_nvic_set_pending(s
, ARMV7M_EXCP_SYSTICK
);
346 } else if (value
& (1 << 25)) {
347 s
->gic
.irq_state
[ARMV7M_EXCP_SYSTICK
].pending
= 0;
351 case 0xd08: /* Vector Table Offset. */
352 cpu
->env
.v7m
.vecbase
= value
& 0xffffff80;
354 case 0xd0c: /* Application Interrupt/Reset Control. */
355 if ((value
>> 16) == 0x05fa) {
357 qemu_irq_pulse(s
->sysresetreq
);
360 qemu_log_mask(LOG_UNIMP
, "VECTCLRACTIVE unimplemented\n");
363 qemu_log_mask(LOG_UNIMP
, "AIRCR system reset unimplemented\n");
366 qemu_log_mask(LOG_UNIMP
, "PRIGROUP unimplemented\n");
370 case 0xd10: /* System Control. */
371 /* TODO: Implement control registers. */
372 qemu_log_mask(LOG_UNIMP
, "NVIC: SCR unimplemented\n");
374 case 0xd14: /* Configuration Control. */
375 /* Enforce RAZ/WI on reserved and must-RAZ/WI bits */
376 value
&= (R_V7M_CCR_STKALIGN_MASK
|
377 R_V7M_CCR_BFHFNMIGN_MASK
|
378 R_V7M_CCR_DIV_0_TRP_MASK
|
379 R_V7M_CCR_UNALIGN_TRP_MASK
|
380 R_V7M_CCR_USERSETMPEND_MASK
|
381 R_V7M_CCR_NONBASETHRDENA_MASK
);
383 cpu
->env
.v7m
.ccr
= value
;
385 case 0xd24: /* System Handler Control. */
386 /* TODO: Real hardware allows you to set/clear the active bits
387 under some circumstances. We don't implement this. */
388 s
->gic
.irq_state
[ARMV7M_EXCP_MEM
].enabled
= (value
& (1 << 16)) != 0;
389 s
->gic
.irq_state
[ARMV7M_EXCP_BUS
].enabled
= (value
& (1 << 17)) != 0;
390 s
->gic
.irq_state
[ARMV7M_EXCP_USAGE
].enabled
= (value
& (1 << 18)) != 0;
392 case 0xd28: /* Configurable Fault Status. */
393 cpu
->env
.v7m
.cfsr
&= ~value
; /* W1C */
395 case 0xd2c: /* Hard Fault Status. */
396 cpu
->env
.v7m
.hfsr
&= ~value
; /* W1C */
398 case 0xd30: /* Debug Fault Status. */
399 cpu
->env
.v7m
.dfsr
&= ~value
; /* W1C */
401 case 0xd34: /* Mem Manage Address. */
402 cpu
->env
.v7m
.mmfar
= value
;
404 case 0xd38: /* Bus Fault Address. */
405 cpu
->env
.v7m
.bfar
= value
;
407 case 0xd3c: /* Aux Fault Status. */
408 qemu_log_mask(LOG_UNIMP
,
409 "NVIC: Aux fault status registers unimplemented\n");
411 case 0xf00: /* Software Triggered Interrupt Register */
412 /* user mode can only write to STIR if CCR.USERSETMPEND permits it */
413 if ((value
& 0x1ff) < s
->num_irq
&&
414 (arm_current_el(&cpu
->env
) ||
415 (cpu
->env
.v7m
.ccr
& R_V7M_CCR_USERSETMPEND_MASK
))) {
416 gic_set_pending_private(&s
->gic
, 0, value
& 0x1ff);
420 qemu_log_mask(LOG_GUEST_ERROR
,
421 "NVIC: Bad write offset 0x%x\n", offset
);
425 static uint64_t nvic_sysreg_read(void *opaque
, hwaddr addr
,
428 nvic_state
*s
= (nvic_state
*)opaque
;
429 uint32_t offset
= addr
;
434 case 0xd18 ... 0xd23: /* System Handler Priority. */
436 for (i
= 0; i
< size
; i
++) {
437 val
|= s
->gic
.priority1
[(offset
- 0xd14) + i
][0] << (i
* 8);
440 case 0xfe0 ... 0xfff: /* ID. */
444 return nvic_id
[(offset
- 0xfe0) >> 2];
447 return nvic_readl(s
, offset
);
449 qemu_log_mask(LOG_GUEST_ERROR
,
450 "NVIC: Bad read of size %d at offset 0x%x\n", size
, offset
);
454 static void nvic_sysreg_write(void *opaque
, hwaddr addr
,
455 uint64_t value
, unsigned size
)
457 nvic_state
*s
= (nvic_state
*)opaque
;
458 uint32_t offset
= addr
;
462 case 0xd18 ... 0xd23: /* System Handler Priority. */
463 for (i
= 0; i
< size
; i
++) {
464 s
->gic
.priority1
[(offset
- 0xd14) + i
][0] =
465 (value
>> (i
* 8)) & 0xff;
471 nvic_writel(s
, offset
, value
);
474 qemu_log_mask(LOG_GUEST_ERROR
,
475 "NVIC: Bad write of size %d at offset 0x%x\n", size
, offset
);
478 static const MemoryRegionOps nvic_sysreg_ops
= {
479 .read
= nvic_sysreg_read
,
480 .write
= nvic_sysreg_write
,
481 .endianness
= DEVICE_NATIVE_ENDIAN
,
484 static const VMStateDescription vmstate_nvic
= {
485 .name
= "armv7m_nvic",
487 .minimum_version_id
= 1,
488 .fields
= (VMStateField
[]) {
489 VMSTATE_UINT32(systick
.control
, nvic_state
),
490 VMSTATE_UINT32(systick
.reload
, nvic_state
),
491 VMSTATE_INT64(systick
.tick
, nvic_state
),
492 VMSTATE_TIMER_PTR(systick
.timer
, nvic_state
),
493 VMSTATE_END_OF_LIST()
497 static void armv7m_nvic_reset(DeviceState
*dev
)
499 nvic_state
*s
= NVIC(dev
);
500 NVICClass
*nc
= NVIC_GET_CLASS(s
);
501 nc
->parent_reset(dev
);
502 /* Common GIC reset resets to disabled; the NVIC doesn't have
503 * per-CPU interfaces so mark our non-existent CPU interface
504 * as enabled by default, and with a priority mask which allows
505 * all interrupts through.
507 s
->gic
.cpu_ctlr
[0] = GICC_CTLR_EN_GRP0
;
508 s
->gic
.priority_mask
[0] = 0x100;
509 /* The NVIC as a whole is always enabled. */
514 static void armv7m_nvic_realize(DeviceState
*dev
, Error
**errp
)
516 nvic_state
*s
= NVIC(dev
);
517 NVICClass
*nc
= NVIC_GET_CLASS(s
);
518 Error
*local_err
= NULL
;
520 s
->cpu
= ARM_CPU(qemu_get_cpu(0));
522 /* The NVIC always has only one CPU */
524 /* Tell the common code we're an NVIC */
525 s
->gic
.revision
= 0xffffffff;
526 s
->num_irq
= s
->gic
.num_irq
;
527 nc
->parent_realize(dev
, &local_err
);
529 error_propagate(errp
, local_err
);
532 gic_init_irqs_and_distributor(&s
->gic
);
533 /* The NVIC and system controller register area looks like this:
534 * 0..0xff : system control registers, including systick
535 * 0x100..0xcff : GIC-like registers
536 * 0xd00..0xfff : system control registers
537 * We use overlaying to put the GIC like registers
538 * over the top of the system control register region.
540 memory_region_init(&s
->container
, OBJECT(s
), "nvic", 0x1000);
541 /* The system register region goes at the bottom of the priority
542 * stack as it covers the whole page.
544 memory_region_init_io(&s
->sysregmem
, OBJECT(s
), &nvic_sysreg_ops
, s
,
545 "nvic_sysregs", 0x1000);
546 memory_region_add_subregion(&s
->container
, 0, &s
->sysregmem
);
547 /* Alias the GIC region so we can get only the section of it
548 * we need, and layer it on top of the system register region.
550 memory_region_init_alias(&s
->gic_iomem_alias
, OBJECT(s
),
551 "nvic-gic", &s
->gic
.iomem
,
553 memory_region_add_subregion_overlap(&s
->container
, 0x100,
554 &s
->gic_iomem_alias
, 1);
555 /* Map the whole thing into system memory at the location required
556 * by the v7M architecture.
558 memory_region_add_subregion(get_system_memory(), 0xe000e000, &s
->container
);
559 s
->systick
.timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, systick_timer_tick
, s
);
562 static void armv7m_nvic_instance_init(Object
*obj
)
564 /* We have a different default value for the num-irq property
565 * than our superclass. This function runs after qdev init
566 * has set the defaults from the Property array and before
567 * any user-specified property setting, so just modify the
568 * value in the GICState struct.
570 GICState
*s
= ARM_GIC_COMMON(obj
);
571 DeviceState
*dev
= DEVICE(obj
);
572 nvic_state
*nvic
= NVIC(obj
);
573 /* The ARM v7m may have anything from 0 to 496 external interrupt
574 * IRQ lines. We default to 64. Other boards may differ and should
575 * set the num-irq property appropriately.
578 qdev_init_gpio_out_named(dev
, &nvic
->sysresetreq
, "SYSRESETREQ", 1);
581 static void armv7m_nvic_class_init(ObjectClass
*klass
, void *data
)
583 NVICClass
*nc
= NVIC_CLASS(klass
);
584 DeviceClass
*dc
= DEVICE_CLASS(klass
);
586 nc
->parent_reset
= dc
->reset
;
587 nc
->parent_realize
= dc
->realize
;
588 dc
->vmsd
= &vmstate_nvic
;
589 dc
->reset
= armv7m_nvic_reset
;
590 dc
->realize
= armv7m_nvic_realize
;
593 static const TypeInfo armv7m_nvic_info
= {
595 .parent
= TYPE_ARM_GIC_COMMON
,
596 .instance_init
= armv7m_nvic_instance_init
,
597 .instance_size
= sizeof(nvic_state
),
598 .class_init
= armv7m_nvic_class_init
,
599 .class_size
= sizeof(NVICClass
),
602 static void armv7m_nvic_register_types(void)
604 type_register_static(&armv7m_nvic_info
);
607 type_init(armv7m_nvic_register_types
)