2 * ARM Nested Vectored Interrupt Controller
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the GPL.
9 * The ARMv7M System controller is fairly tightly tied in with the
10 * NVIC. Much of that is also implemented here.
14 #include "qemu-timer.h"
17 /* 32 internal lines (16 used for system exceptions) plus 64 external
23 /* Only a single "CPU" interface is present. */
25 gic_get_current_cpu(void)
30 static uint32_t nvic_readl(void *opaque
, uint32_t offset
);
31 static void nvic_writel(void *opaque
, uint32_t offset
, uint32_t value
);
45 /* qemu timers run at 1GHz. We want something closer to 1MHz. */
46 #define SYSTICK_SCALE 1000ULL
48 #define SYSTICK_ENABLE (1 << 0)
49 #define SYSTICK_TICKINT (1 << 1)
50 #define SYSTICK_CLKSOURCE (1 << 2)
51 #define SYSTICK_COUNTFLAG (1 << 16)
53 /* Multiplication factor to convert from system clock ticks to qemu timer
55 int system_clock_scale
;
57 /* Conversion factor from qemu timer to SysTick frequencies. */
58 static inline int64_t systick_scale(nvic_state
*s
)
60 if (s
->systick
.control
& SYSTICK_CLKSOURCE
)
61 return system_clock_scale
;
66 static void systick_reload(nvic_state
*s
, int reset
)
69 s
->systick
.tick
= qemu_get_clock(vm_clock
);
70 s
->systick
.tick
+= (s
->systick
.reload
+ 1) * systick_scale(s
);
71 qemu_mod_timer(s
->systick
.timer
, s
->systick
.tick
);
74 static void systick_timer_tick(void * opaque
)
76 nvic_state
*s
= (nvic_state
*)opaque
;
77 s
->systick
.control
|= SYSTICK_COUNTFLAG
;
78 if (s
->systick
.control
& SYSTICK_TICKINT
) {
79 /* Trigger the interrupt. */
80 armv7m_nvic_set_pending(s
, ARMV7M_EXCP_SYSTICK
);
82 if (s
->systick
.reload
== 0) {
83 s
->systick
.control
&= ~SYSTICK_ENABLE
;
89 /* The external routines use the hardware vector numbering, ie. the first
90 IRQ is #16. The internal GIC routines use #32 as the first IRQ. */
91 void armv7m_nvic_set_pending(void *opaque
, int irq
)
93 nvic_state
*s
= (nvic_state
*)opaque
;
96 gic_set_pending_private(s
->gic
, 0, irq
);
99 /* Make pending IRQ active. */
100 int armv7m_nvic_acknowledge_irq(void *opaque
)
102 nvic_state
*s
= (nvic_state
*)opaque
;
105 irq
= gic_acknowledge_irq(s
->gic
, 0);
107 cpu_abort(cpu_single_env
, "Interrupt but no vector\n");
113 void armv7m_nvic_complete_irq(void *opaque
, int irq
)
115 nvic_state
*s
= (nvic_state
*)opaque
;
118 gic_complete_irq(s
->gic
, 0, irq
);
121 static uint32_t nvic_readl(void *opaque
, uint32_t offset
)
123 nvic_state
*s
= (nvic_state
*)opaque
;
128 case 4: /* Interrupt Control Type. */
129 return (GIC_NIRQ
/ 32) - 1;
130 case 0x10: /* SysTick Control and Status. */
131 val
= s
->systick
.control
;
132 s
->systick
.control
&= ~SYSTICK_COUNTFLAG
;
134 case 0x14: /* SysTick Reload Value. */
135 return s
->systick
.reload
;
136 case 0x18: /* SysTick Current Value. */
139 if ((s
->systick
.control
& SYSTICK_ENABLE
) == 0)
141 t
= qemu_get_clock(vm_clock
);
142 if (t
>= s
->systick
.tick
)
144 val
= ((s
->systick
.tick
- (t
+ 1)) / systick_scale(s
)) + 1;
145 /* The interrupt in triggered when the timer reaches zero.
146 However the counter is not reloaded until the next clock
147 tick. This is a hack to return zero during the first tick. */
148 if (val
> s
->systick
.reload
)
152 case 0x1c: /* SysTick Calibration Value. */
154 case 0xd00: /* CPUID Base. */
155 return cpu_single_env
->cp15
.c0_cpuid
;
156 case 0xd04: /* Interrypt Control State. */
158 val
= s
->gic
->running_irq
[0];
161 } else if (val
>= 32) {
165 if (s
->gic
->running_irq
[0] == 1023
166 || s
->gic
->last_active
[s
->gic
->running_irq
[0]][0] == 1023) {
170 if (s
->gic
->current_pending
[0] != 1023)
171 val
|= (s
->gic
->current_pending
[0] << 12);
173 for (irq
= 32; irq
< GIC_NIRQ
; irq
++) {
174 if (s
->gic
->irq_state
[irq
].pending
) {
180 if (s
->gic
->irq_state
[ARMV7M_EXCP_SYSTICK
].pending
)
183 if (s
->gic
->irq_state
[ARMV7M_EXCP_PENDSV
].pending
)
186 if (s
->gic
->irq_state
[ARMV7M_EXCP_NMI
].pending
)
189 case 0xd08: /* Vector Table Offset. */
190 return cpu_single_env
->v7m
.vecbase
;
191 case 0xd0c: /* Application Interrupt/Reset Control. */
193 case 0xd10: /* System Control. */
194 /* TODO: Implement SLEEPONEXIT. */
196 case 0xd14: /* Configuration Control. */
197 /* TODO: Implement Configuration Control bits. */
199 case 0xd18: case 0xd1c: case 0xd20: /* System Handler Priority. */
200 irq
= offset
- 0xd14;
202 val
= s
->gic
->priority1
[irq
++][0];
203 val
= s
->gic
->priority1
[irq
++][0] << 8;
204 val
= s
->gic
->priority1
[irq
++][0] << 16;
205 val
= s
->gic
->priority1
[irq
][0] << 24;
207 case 0xd24: /* System Handler Status. */
209 if (s
->gic
->irq_state
[ARMV7M_EXCP_MEM
].active
) val
|= (1 << 0);
210 if (s
->gic
->irq_state
[ARMV7M_EXCP_BUS
].active
) val
|= (1 << 1);
211 if (s
->gic
->irq_state
[ARMV7M_EXCP_USAGE
].active
) val
|= (1 << 3);
212 if (s
->gic
->irq_state
[ARMV7M_EXCP_SVC
].active
) val
|= (1 << 7);
213 if (s
->gic
->irq_state
[ARMV7M_EXCP_DEBUG
].active
) val
|= (1 << 8);
214 if (s
->gic
->irq_state
[ARMV7M_EXCP_PENDSV
].active
) val
|= (1 << 10);
215 if (s
->gic
->irq_state
[ARMV7M_EXCP_SYSTICK
].active
) val
|= (1 << 11);
216 if (s
->gic
->irq_state
[ARMV7M_EXCP_USAGE
].pending
) val
|= (1 << 12);
217 if (s
->gic
->irq_state
[ARMV7M_EXCP_MEM
].pending
) val
|= (1 << 13);
218 if (s
->gic
->irq_state
[ARMV7M_EXCP_BUS
].pending
) val
|= (1 << 14);
219 if (s
->gic
->irq_state
[ARMV7M_EXCP_SVC
].pending
) val
|= (1 << 15);
220 if (s
->gic
->irq_state
[ARMV7M_EXCP_MEM
].enabled
) val
|= (1 << 16);
221 if (s
->gic
->irq_state
[ARMV7M_EXCP_BUS
].enabled
) val
|= (1 << 17);
222 if (s
->gic
->irq_state
[ARMV7M_EXCP_USAGE
].enabled
) val
|= (1 << 18);
224 case 0xd28: /* Configurable Fault Status. */
225 /* TODO: Implement Fault Status. */
226 cpu_abort(cpu_single_env
,
227 "Not implemented: Configurable Fault Status.");
229 case 0xd2c: /* Hard Fault Status. */
230 case 0xd30: /* Debug Fault Status. */
231 case 0xd34: /* Mem Manage Address. */
232 case 0xd38: /* Bus Fault Address. */
233 case 0xd3c: /* Aux Fault Status. */
234 /* TODO: Implement fault status registers. */
236 case 0xd40: /* PFR0. */
238 case 0xd44: /* PRF1. */
240 case 0xd48: /* DFR0. */
242 case 0xd4c: /* AFR0. */
244 case 0xd50: /* MMFR0. */
246 case 0xd54: /* MMFR1. */
248 case 0xd58: /* MMFR2. */
250 case 0xd5c: /* MMFR3. */
252 case 0xd60: /* ISAR0. */
254 case 0xd64: /* ISAR1. */
256 case 0xd68: /* ISAR2. */
258 case 0xd6c: /* ISAR3. */
260 case 0xd70: /* ISAR4. */
262 /* TODO: Implement debug registers. */
265 cpu_abort(cpu_single_env
, "NVIC: Bad read offset 0x%x\n", offset
);
269 static void nvic_writel(void *opaque
, uint32_t offset
, uint32_t value
)
271 nvic_state
*s
= (nvic_state
*)opaque
;
274 case 0x10: /* SysTick Control and Status. */
275 oldval
= s
->systick
.control
;
276 s
->systick
.control
&= 0xfffffff8;
277 s
->systick
.control
|= value
& 7;
278 if ((oldval
^ value
) & SYSTICK_ENABLE
) {
279 int64_t now
= qemu_get_clock(vm_clock
);
280 if (value
& SYSTICK_ENABLE
) {
281 if (s
->systick
.tick
) {
282 s
->systick
.tick
+= now
;
283 qemu_mod_timer(s
->systick
.timer
, s
->systick
.tick
);
285 systick_reload(s
, 1);
288 qemu_del_timer(s
->systick
.timer
);
289 s
->systick
.tick
-= now
;
290 if (s
->systick
.tick
< 0)
293 } else if ((oldval
^ value
) & SYSTICK_CLKSOURCE
) {
294 /* This is a hack. Force the timer to be reloaded
295 when the reference clock is changed. */
296 systick_reload(s
, 1);
299 case 0x14: /* SysTick Reload Value. */
300 s
->systick
.reload
= value
;
302 case 0x18: /* SysTick Current Value. Writes reload the timer. */
303 systick_reload(s
, 1);
304 s
->systick
.control
&= ~SYSTICK_COUNTFLAG
;
306 case 0xd04: /* Interrupt Control State. */
307 if (value
& (1 << 31)) {
308 armv7m_nvic_set_pending(s
, ARMV7M_EXCP_NMI
);
310 if (value
& (1 << 28)) {
311 armv7m_nvic_set_pending(s
, ARMV7M_EXCP_PENDSV
);
312 } else if (value
& (1 << 27)) {
313 s
->gic
->irq_state
[ARMV7M_EXCP_PENDSV
].pending
= 0;
316 if (value
& (1 << 26)) {
317 armv7m_nvic_set_pending(s
, ARMV7M_EXCP_SYSTICK
);
318 } else if (value
& (1 << 25)) {
319 s
->gic
->irq_state
[ARMV7M_EXCP_SYSTICK
].pending
= 0;
323 case 0xd08: /* Vector Table Offset. */
324 cpu_single_env
->v7m
.vecbase
= value
& 0xffffff80;
326 case 0xd0c: /* Application Interrupt/Reset Control. */
327 if ((value
>> 16) == 0x05fa) {
329 cpu_abort(cpu_single_env
, "VECTCLRACTIVE not implemented");
332 cpu_abort(cpu_single_env
, "System reset");
336 case 0xd10: /* System Control. */
337 case 0xd14: /* Configuration Control. */
338 /* TODO: Implement control registers. */
340 case 0xd18: case 0xd1c: case 0xd20: /* System Handler Priority. */
343 irq
= offset
- 0xd14;
344 s
->gic
->priority1
[irq
++][0] = value
& 0xff;
345 s
->gic
->priority1
[irq
++][0] = (value
>> 8) & 0xff;
346 s
->gic
->priority1
[irq
++][0] = (value
>> 16) & 0xff;
347 s
->gic
->priority1
[irq
][0] = (value
>> 24) & 0xff;
351 case 0xd24: /* System Handler Control. */
352 /* TODO: Real hardware allows you to set/clear the active bits
353 under some circumstances. We don't implement this. */
354 s
->gic
->irq_state
[ARMV7M_EXCP_MEM
].enabled
= (value
& (1 << 16)) != 0;
355 s
->gic
->irq_state
[ARMV7M_EXCP_BUS
].enabled
= (value
& (1 << 17)) != 0;
356 s
->gic
->irq_state
[ARMV7M_EXCP_USAGE
].enabled
= (value
& (1 << 18)) != 0;
358 case 0xd28: /* Configurable Fault Status. */
359 case 0xd2c: /* Hard Fault Status. */
360 case 0xd30: /* Debug Fault Status. */
361 case 0xd34: /* Mem Manage Address. */
362 case 0xd38: /* Bus Fault Address. */
363 case 0xd3c: /* Aux Fault Status. */
367 cpu_abort(cpu_single_env
, "NVIC: Bad write offset 0x%x\n", offset
);
371 qemu_irq
*armv7m_nvic_init(CPUState
*env
)
376 parent
= arm_pic_init_cpu(env
);
377 s
= (nvic_state
*)qemu_mallocz(sizeof(nvic_state
));
378 s
->gic
= gic_init(0xe000e000, &parent
[ARM_PIC_CPU_IRQ
]);
380 s
->systick
.timer
= qemu_new_timer(vm_clock
, systick_timer_tick
, s
);
382 cpu_abort(env
, "CPU can only have one NVIC\n");