blob: 25fa43cb2d4895d9a05ef81bde298857d732e132 [file] [log] [blame]
pbrook9ee6e8b2007-11-11 00:04:49 +00001/*
2 * ARM Nested Vectored Interrupt Controller
3 *
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
6 *
Matthew Fernandez8e31bf32011-06-26 12:21:35 +10007 * This code is licensed under the GPL.
pbrook9ee6e8b2007-11-11 00:04:49 +00008 *
9 * The ARMv7M System controller is fairly tightly tied in with the
10 * NVIC. Much of that is also implemented here.
11 */
12
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010013#include "hw/sysbus.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010014#include "qemu/timer.h"
Peter Maydellbd2be152013-04-09 15:26:55 +010015#include "hw/arm/arm.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010016#include "exec/address-spaces.h"
Paolo Bonzini47b43a12013-03-18 17:36:02 +010017#include "gic_internal.h"
pbrook9ee6e8b2007-11-11 00:04:49 +000018
19typedef struct {
Peter Maydellfae15282012-10-12 11:54:39 +010020 GICState gic;
pbrook9ee6e8b2007-11-11 00:04:49 +000021 struct {
22 uint32_t control;
23 uint32_t reload;
24 int64_t tick;
25 QEMUTimer *timer;
26 } systick;
Peter Maydell2a29dde2012-05-02 16:49:39 +000027 MemoryRegion sysregmem;
28 MemoryRegion gic_iomem_alias;
29 MemoryRegion container;
Mark Langsdorfa32134a2012-01-17 10:54:07 +000030 uint32_t num_irq;
pbrook9ee6e8b2007-11-11 00:04:49 +000031} nvic_state;
32
Peter Maydell1e8cae42012-05-02 16:49:42 +000033#define TYPE_NVIC "armv7m_nvic"
34/**
35 * NVICClass:
36 * @parent_reset: the parent class' reset handler.
37 *
38 * A model of the v7M NVIC and System Controller
39 */
40typedef struct NVICClass {
41 /*< private >*/
42 ARMGICClass parent_class;
43 /*< public >*/
Peter Maydell53111182013-03-05 00:34:42 +000044 DeviceRealize parent_realize;
Peter Maydell1e8cae42012-05-02 16:49:42 +000045 void (*parent_reset)(DeviceState *dev);
46} NVICClass;
47
48#define NVIC_CLASS(klass) \
49 OBJECT_CLASS_CHECK(NVICClass, (klass), TYPE_NVIC)
50#define NVIC_GET_CLASS(obj) \
51 OBJECT_GET_CLASS(NVICClass, (obj), TYPE_NVIC)
52#define NVIC(obj) \
53 OBJECT_CHECK(nvic_state, (obj), TYPE_NVIC)
54
Peter Maydell2a29dde2012-05-02 16:49:39 +000055static const uint8_t nvic_id[] = {
56 0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1
57};
58
pbrook9ee6e8b2007-11-11 00:04:49 +000059/* qemu timers run at 1GHz. We want something closer to 1MHz. */
60#define SYSTICK_SCALE 1000ULL
61
62#define SYSTICK_ENABLE (1 << 0)
63#define SYSTICK_TICKINT (1 << 1)
64#define SYSTICK_CLKSOURCE (1 << 2)
65#define SYSTICK_COUNTFLAG (1 << 16)
66
blueswir17ee930d2008-09-17 19:04:14 +000067int system_clock_scale;
68
pbrooke57ec012007-11-24 03:09:07 +000069/* Conversion factor from qemu timer to SysTick frequencies. */
pbrook9ee6e8b2007-11-11 00:04:49 +000070static inline int64_t systick_scale(nvic_state *s)
71{
72 if (s->systick.control & SYSTICK_CLKSOURCE)
pbrooke57ec012007-11-24 03:09:07 +000073 return system_clock_scale;
pbrook9ee6e8b2007-11-11 00:04:49 +000074 else
75 return 1000;
76}
77
78static void systick_reload(nvic_state *s, int reset)
79{
80 if (reset)
Paolo Bonzini74475452011-03-11 16:47:48 +010081 s->systick.tick = qemu_get_clock_ns(vm_clock);
pbrook9ee6e8b2007-11-11 00:04:49 +000082 s->systick.tick += (s->systick.reload + 1) * systick_scale(s);
83 qemu_mod_timer(s->systick.timer, s->systick.tick);
84}
85
86static void systick_timer_tick(void * opaque)
87{
88 nvic_state *s = (nvic_state *)opaque;
89 s->systick.control |= SYSTICK_COUNTFLAG;
90 if (s->systick.control & SYSTICK_TICKINT) {
91 /* Trigger the interrupt. */
92 armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK);
93 }
94 if (s->systick.reload == 0) {
95 s->systick.control &= ~SYSTICK_ENABLE;
96 } else {
97 systick_reload(s, 0);
98 }
99}
100
Peter Maydellaecff692012-04-13 11:39:09 +0000101static void systick_reset(nvic_state *s)
102{
103 s->systick.control = 0;
104 s->systick.reload = 0;
105 s->systick.tick = 0;
106 qemu_del_timer(s->systick.timer);
107}
108
pbrook9ee6e8b2007-11-11 00:04:49 +0000109/* The external routines use the hardware vector numbering, ie. the first
110 IRQ is #16. The internal GIC routines use #32 as the first IRQ. */
111void armv7m_nvic_set_pending(void *opaque, int irq)
112{
113 nvic_state *s = (nvic_state *)opaque;
114 if (irq >= 16)
115 irq += 16;
Paul Brookfe7e8752009-05-14 22:35:08 +0100116 gic_set_pending_private(&s->gic, 0, irq);
pbrook9ee6e8b2007-11-11 00:04:49 +0000117}
118
119/* Make pending IRQ active. */
120int armv7m_nvic_acknowledge_irq(void *opaque)
121{
122 nvic_state *s = (nvic_state *)opaque;
123 uint32_t irq;
124
Paul Brookfe7e8752009-05-14 22:35:08 +0100125 irq = gic_acknowledge_irq(&s->gic, 0);
pbrook9ee6e8b2007-11-11 00:04:49 +0000126 if (irq == 1023)
Paul Brook2ac71172009-05-08 02:35:15 +0100127 hw_error("Interrupt but no vector\n");
pbrook9ee6e8b2007-11-11 00:04:49 +0000128 if (irq >= 32)
129 irq -= 16;
130 return irq;
131}
132
133void armv7m_nvic_complete_irq(void *opaque, int irq)
134{
135 nvic_state *s = (nvic_state *)opaque;
136 if (irq >= 16)
137 irq += 16;
Paul Brookfe7e8752009-05-14 22:35:08 +0100138 gic_complete_irq(&s->gic, 0, irq);
pbrook9ee6e8b2007-11-11 00:04:49 +0000139}
140
Andre Beckus0e8153d2012-10-30 07:45:07 +0000141static uint32_t nvic_readl(nvic_state *s, uint32_t offset)
pbrook9ee6e8b2007-11-11 00:04:49 +0000142{
pbrook9ee6e8b2007-11-11 00:04:49 +0000143 uint32_t val;
144 int irq;
145
146 switch (offset) {
147 case 4: /* Interrupt Control Type. */
Mark Langsdorfa32134a2012-01-17 10:54:07 +0000148 return (s->num_irq / 32) - 1;
pbrook9ee6e8b2007-11-11 00:04:49 +0000149 case 0x10: /* SysTick Control and Status. */
150 val = s->systick.control;
151 s->systick.control &= ~SYSTICK_COUNTFLAG;
152 return val;
153 case 0x14: /* SysTick Reload Value. */
154 return s->systick.reload;
155 case 0x18: /* SysTick Current Value. */
156 {
157 int64_t t;
158 if ((s->systick.control & SYSTICK_ENABLE) == 0)
159 return 0;
Paolo Bonzini74475452011-03-11 16:47:48 +0100160 t = qemu_get_clock_ns(vm_clock);
pbrook9ee6e8b2007-11-11 00:04:49 +0000161 if (t >= s->systick.tick)
162 return 0;
163 val = ((s->systick.tick - (t + 1)) / systick_scale(s)) + 1;
164 /* The interrupt in triggered when the timer reaches zero.
165 However the counter is not reloaded until the next clock
166 tick. This is a hack to return zero during the first tick. */
167 if (val > s->systick.reload)
168 val = 0;
169 return val;
170 }
171 case 0x1c: /* SysTick Calibration Value. */
172 return 10000;
173 case 0xd00: /* CPUID Base. */
174 return cpu_single_env->cp15.c0_cpuid;
Peter Maydelle03ba132013-04-09 12:48:19 +0100175 case 0xd04: /* Interrupt Control State. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000176 /* VECTACTIVE */
Paul Brookfe7e8752009-05-14 22:35:08 +0100177 val = s->gic.running_irq[0];
pbrook9ee6e8b2007-11-11 00:04:49 +0000178 if (val == 1023) {
179 val = 0;
180 } else if (val >= 32) {
181 val -= 16;
182 }
183 /* RETTOBASE */
Paul Brookfe7e8752009-05-14 22:35:08 +0100184 if (s->gic.running_irq[0] == 1023
185 || s->gic.last_active[s->gic.running_irq[0]][0] == 1023) {
pbrook9ee6e8b2007-11-11 00:04:49 +0000186 val |= (1 << 11);
187 }
188 /* VECTPENDING */
Paul Brookfe7e8752009-05-14 22:35:08 +0100189 if (s->gic.current_pending[0] != 1023)
190 val |= (s->gic.current_pending[0] << 12);
pbrook9ee6e8b2007-11-11 00:04:49 +0000191 /* ISRPENDING */
Mark Langsdorfa32134a2012-01-17 10:54:07 +0000192 for (irq = 32; irq < s->num_irq; irq++) {
Paul Brookfe7e8752009-05-14 22:35:08 +0100193 if (s->gic.irq_state[irq].pending) {
pbrook9ee6e8b2007-11-11 00:04:49 +0000194 val |= (1 << 22);
195 break;
196 }
197 }
198 /* PENDSTSET */
Paul Brookfe7e8752009-05-14 22:35:08 +0100199 if (s->gic.irq_state[ARMV7M_EXCP_SYSTICK].pending)
pbrook9ee6e8b2007-11-11 00:04:49 +0000200 val |= (1 << 26);
201 /* PENDSVSET */
Paul Brookfe7e8752009-05-14 22:35:08 +0100202 if (s->gic.irq_state[ARMV7M_EXCP_PENDSV].pending)
pbrook9ee6e8b2007-11-11 00:04:49 +0000203 val |= (1 << 28);
204 /* NMIPENDSET */
Paul Brookfe7e8752009-05-14 22:35:08 +0100205 if (s->gic.irq_state[ARMV7M_EXCP_NMI].pending)
pbrook9ee6e8b2007-11-11 00:04:49 +0000206 val |= (1 << 31);
207 return val;
208 case 0xd08: /* Vector Table Offset. */
209 return cpu_single_env->v7m.vecbase;
210 case 0xd0c: /* Application Interrupt/Reset Control. */
211 return 0xfa05000;
212 case 0xd10: /* System Control. */
213 /* TODO: Implement SLEEPONEXIT. */
214 return 0;
215 case 0xd14: /* Configuration Control. */
216 /* TODO: Implement Configuration Control bits. */
217 return 0;
pbrook9ee6e8b2007-11-11 00:04:49 +0000218 case 0xd24: /* System Handler Status. */
219 val = 0;
Paul Brookfe7e8752009-05-14 22:35:08 +0100220 if (s->gic.irq_state[ARMV7M_EXCP_MEM].active) val |= (1 << 0);
221 if (s->gic.irq_state[ARMV7M_EXCP_BUS].active) val |= (1 << 1);
222 if (s->gic.irq_state[ARMV7M_EXCP_USAGE].active) val |= (1 << 3);
223 if (s->gic.irq_state[ARMV7M_EXCP_SVC].active) val |= (1 << 7);
224 if (s->gic.irq_state[ARMV7M_EXCP_DEBUG].active) val |= (1 << 8);
225 if (s->gic.irq_state[ARMV7M_EXCP_PENDSV].active) val |= (1 << 10);
226 if (s->gic.irq_state[ARMV7M_EXCP_SYSTICK].active) val |= (1 << 11);
227 if (s->gic.irq_state[ARMV7M_EXCP_USAGE].pending) val |= (1 << 12);
228 if (s->gic.irq_state[ARMV7M_EXCP_MEM].pending) val |= (1 << 13);
229 if (s->gic.irq_state[ARMV7M_EXCP_BUS].pending) val |= (1 << 14);
230 if (s->gic.irq_state[ARMV7M_EXCP_SVC].pending) val |= (1 << 15);
231 if (s->gic.irq_state[ARMV7M_EXCP_MEM].enabled) val |= (1 << 16);
232 if (s->gic.irq_state[ARMV7M_EXCP_BUS].enabled) val |= (1 << 17);
233 if (s->gic.irq_state[ARMV7M_EXCP_USAGE].enabled) val |= (1 << 18);
pbrook9ee6e8b2007-11-11 00:04:49 +0000234 return val;
235 case 0xd28: /* Configurable Fault Status. */
236 /* TODO: Implement Fault Status. */
Peter Maydelle72e3ff2012-10-30 07:45:10 +0000237 qemu_log_mask(LOG_UNIMP, "Configurable Fault Status unimplemented\n");
pbrook9ee6e8b2007-11-11 00:04:49 +0000238 return 0;
239 case 0xd2c: /* Hard Fault Status. */
240 case 0xd30: /* Debug Fault Status. */
241 case 0xd34: /* Mem Manage Address. */
242 case 0xd38: /* Bus Fault Address. */
243 case 0xd3c: /* Aux Fault Status. */
244 /* TODO: Implement fault status registers. */
Peter Maydelle72e3ff2012-10-30 07:45:10 +0000245 qemu_log_mask(LOG_UNIMP, "Fault status registers unimplemented\n");
246 return 0;
pbrook9ee6e8b2007-11-11 00:04:49 +0000247 case 0xd40: /* PFR0. */
248 return 0x00000030;
249 case 0xd44: /* PRF1. */
250 return 0x00000200;
251 case 0xd48: /* DFR0. */
252 return 0x00100000;
253 case 0xd4c: /* AFR0. */
254 return 0x00000000;
255 case 0xd50: /* MMFR0. */
256 return 0x00000030;
257 case 0xd54: /* MMFR1. */
258 return 0x00000000;
259 case 0xd58: /* MMFR2. */
260 return 0x00000000;
261 case 0xd5c: /* MMFR3. */
262 return 0x00000000;
263 case 0xd60: /* ISAR0. */
264 return 0x01141110;
265 case 0xd64: /* ISAR1. */
266 return 0x02111000;
267 case 0xd68: /* ISAR2. */
268 return 0x21112231;
269 case 0xd6c: /* ISAR3. */
270 return 0x01111110;
271 case 0xd70: /* ISAR4. */
272 return 0x01310102;
273 /* TODO: Implement debug registers. */
274 default:
Peter Maydelle72e3ff2012-10-30 07:45:10 +0000275 qemu_log_mask(LOG_GUEST_ERROR, "NVIC: Bad read offset 0x%x\n", offset);
276 return 0;
pbrook9ee6e8b2007-11-11 00:04:49 +0000277 }
278}
279
Andre Beckus0e8153d2012-10-30 07:45:07 +0000280static void nvic_writel(nvic_state *s, uint32_t offset, uint32_t value)
pbrook9ee6e8b2007-11-11 00:04:49 +0000281{
pbrook9ee6e8b2007-11-11 00:04:49 +0000282 uint32_t oldval;
283 switch (offset) {
284 case 0x10: /* SysTick Control and Status. */
285 oldval = s->systick.control;
286 s->systick.control &= 0xfffffff8;
287 s->systick.control |= value & 7;
288 if ((oldval ^ value) & SYSTICK_ENABLE) {
Paolo Bonzini74475452011-03-11 16:47:48 +0100289 int64_t now = qemu_get_clock_ns(vm_clock);
pbrook9ee6e8b2007-11-11 00:04:49 +0000290 if (value & SYSTICK_ENABLE) {
291 if (s->systick.tick) {
292 s->systick.tick += now;
293 qemu_mod_timer(s->systick.timer, s->systick.tick);
294 } else {
295 systick_reload(s, 1);
296 }
297 } else {
298 qemu_del_timer(s->systick.timer);
299 s->systick.tick -= now;
300 if (s->systick.tick < 0)
301 s->systick.tick = 0;
302 }
303 } else if ((oldval ^ value) & SYSTICK_CLKSOURCE) {
304 /* This is a hack. Force the timer to be reloaded
305 when the reference clock is changed. */
306 systick_reload(s, 1);
307 }
308 break;
309 case 0x14: /* SysTick Reload Value. */
310 s->systick.reload = value;
311 break;
312 case 0x18: /* SysTick Current Value. Writes reload the timer. */
313 systick_reload(s, 1);
314 s->systick.control &= ~SYSTICK_COUNTFLAG;
315 break;
316 case 0xd04: /* Interrupt Control State. */
317 if (value & (1 << 31)) {
318 armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI);
319 }
320 if (value & (1 << 28)) {
321 armv7m_nvic_set_pending(s, ARMV7M_EXCP_PENDSV);
322 } else if (value & (1 << 27)) {
Paul Brookfe7e8752009-05-14 22:35:08 +0100323 s->gic.irq_state[ARMV7M_EXCP_PENDSV].pending = 0;
324 gic_update(&s->gic);
pbrook9ee6e8b2007-11-11 00:04:49 +0000325 }
326 if (value & (1 << 26)) {
327 armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK);
328 } else if (value & (1 << 25)) {
Paul Brookfe7e8752009-05-14 22:35:08 +0100329 s->gic.irq_state[ARMV7M_EXCP_SYSTICK].pending = 0;
330 gic_update(&s->gic);
pbrook9ee6e8b2007-11-11 00:04:49 +0000331 }
332 break;
333 case 0xd08: /* Vector Table Offset. */
334 cpu_single_env->v7m.vecbase = value & 0xffffff80;
335 break;
336 case 0xd0c: /* Application Interrupt/Reset Control. */
337 if ((value >> 16) == 0x05fa) {
338 if (value & 2) {
Peter Maydelle72e3ff2012-10-30 07:45:10 +0000339 qemu_log_mask(LOG_UNIMP, "VECTCLRACTIVE unimplemented\n");
pbrook9ee6e8b2007-11-11 00:04:49 +0000340 }
341 if (value & 5) {
Peter Maydelle72e3ff2012-10-30 07:45:10 +0000342 qemu_log_mask(LOG_UNIMP, "AIRCR system reset unimplemented\n");
pbrook9ee6e8b2007-11-11 00:04:49 +0000343 }
344 }
345 break;
346 case 0xd10: /* System Control. */
347 case 0xd14: /* Configuration Control. */
348 /* TODO: Implement control registers. */
Peter Maydelle72e3ff2012-10-30 07:45:10 +0000349 qemu_log_mask(LOG_UNIMP, "NVIC: SCR and CCR unimplemented\n");
350 break;
pbrook9ee6e8b2007-11-11 00:04:49 +0000351 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. */
Paul Brookfe7e8752009-05-14 22:35:08 +0100354 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;
pbrook9ee6e8b2007-11-11 00:04:49 +0000357 break;
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. */
Peter Maydelle72e3ff2012-10-30 07:45:10 +0000364 qemu_log_mask(LOG_UNIMP,
365 "NVIC: fault status registers unimplemented\n");
366 break;
Peter Maydell2a29dde2012-05-02 16:49:39 +0000367 case 0xf00: /* Software Triggered Interrupt Register */
368 if ((value & 0x1ff) < s->num_irq) {
369 gic_set_pending_private(&s->gic, 0, value & 0x1ff);
370 }
371 break;
pbrook9ee6e8b2007-11-11 00:04:49 +0000372 default:
Peter Maydelle72e3ff2012-10-30 07:45:10 +0000373 qemu_log_mask(LOG_GUEST_ERROR,
374 "NVIC: Bad write offset 0x%x\n", offset);
pbrook9ee6e8b2007-11-11 00:04:49 +0000375 }
376}
377
Avi Kivitya8170e52012-10-23 12:30:10 +0200378static uint64_t nvic_sysreg_read(void *opaque, hwaddr addr,
Peter Maydell2a29dde2012-05-02 16:49:39 +0000379 unsigned size)
380{
Andre Beckus0e8153d2012-10-30 07:45:07 +0000381 nvic_state *s = (nvic_state *)opaque;
Peter Maydell2a29dde2012-05-02 16:49:39 +0000382 uint32_t offset = addr;
Andre Beckus0e8153d2012-10-30 07:45:07 +0000383 int i;
384 uint32_t val;
385
386 switch (offset) {
387 case 0xd18 ... 0xd23: /* System Handler Priority. */
388 val = 0;
389 for (i = 0; i < size; i++) {
390 val |= s->gic.priority1[(offset - 0xd14) + i][0] << (i * 8);
391 }
392 return val;
393 case 0xfe0 ... 0xfff: /* ID. */
Peter Maydell2a29dde2012-05-02 16:49:39 +0000394 if (offset & 3) {
395 return 0;
396 }
397 return nvic_id[(offset - 0xfe0) >> 2];
398 }
399 if (size == 4) {
Andre Beckus0e8153d2012-10-30 07:45:07 +0000400 return nvic_readl(s, offset);
Peter Maydell2a29dde2012-05-02 16:49:39 +0000401 }
Peter Maydelle72e3ff2012-10-30 07:45:10 +0000402 qemu_log_mask(LOG_GUEST_ERROR,
403 "NVIC: Bad read of size %d at offset 0x%x\n", size, offset);
404 return 0;
Peter Maydell2a29dde2012-05-02 16:49:39 +0000405}
406
Avi Kivitya8170e52012-10-23 12:30:10 +0200407static void nvic_sysreg_write(void *opaque, hwaddr addr,
Peter Maydell2a29dde2012-05-02 16:49:39 +0000408 uint64_t value, unsigned size)
409{
Andre Beckus0e8153d2012-10-30 07:45:07 +0000410 nvic_state *s = (nvic_state *)opaque;
Peter Maydell2a29dde2012-05-02 16:49:39 +0000411 uint32_t offset = addr;
Andre Beckus0e8153d2012-10-30 07:45:07 +0000412 int i;
413
414 switch (offset) {
415 case 0xd18 ... 0xd23: /* System Handler Priority. */
416 for (i = 0; i < size; i++) {
417 s->gic.priority1[(offset - 0xd14) + i][0] =
418 (value >> (i * 8)) & 0xff;
419 }
420 gic_update(&s->gic);
421 return;
422 }
Peter Maydell2a29dde2012-05-02 16:49:39 +0000423 if (size == 4) {
Andre Beckus0e8153d2012-10-30 07:45:07 +0000424 nvic_writel(s, offset, value);
Peter Maydell2a29dde2012-05-02 16:49:39 +0000425 return;
426 }
Peter Maydelle72e3ff2012-10-30 07:45:10 +0000427 qemu_log_mask(LOG_GUEST_ERROR,
428 "NVIC: Bad write of size %d at offset 0x%x\n", size, offset);
Peter Maydell2a29dde2012-05-02 16:49:39 +0000429}
430
431static const MemoryRegionOps nvic_sysreg_ops = {
432 .read = nvic_sysreg_read,
433 .write = nvic_sysreg_write,
434 .endianness = DEVICE_NATIVE_ENDIAN,
435};
436
Juan Quintela07972262010-12-02 02:17:33 +0100437static const VMStateDescription vmstate_nvic = {
438 .name = "armv7m_nvic",
439 .version_id = 1,
440 .minimum_version_id = 1,
441 .minimum_version_id_old = 1,
442 .fields = (VMStateField[]) {
443 VMSTATE_UINT32(systick.control, nvic_state),
444 VMSTATE_UINT32(systick.reload, nvic_state),
445 VMSTATE_INT64(systick.tick, nvic_state),
446 VMSTATE_TIMER(systick.timer, nvic_state),
447 VMSTATE_END_OF_LIST()
448 }
449};
pbrook23e39292008-07-02 16:48:32 +0000450
Peter Maydellaecff692012-04-13 11:39:09 +0000451static void armv7m_nvic_reset(DeviceState *dev)
452{
Peter Maydell1e8cae42012-05-02 16:49:42 +0000453 nvic_state *s = NVIC(dev);
454 NVICClass *nc = NVIC_GET_CLASS(s);
455 nc->parent_reset(dev);
Peter Maydellb3387ed2012-05-02 16:49:39 +0000456 /* Common GIC reset resets to disabled; the NVIC doesn't have
457 * per-CPU interfaces so mark our non-existent CPU interface
Peter Maydellee3f0952012-12-11 11:30:37 +0000458 * as enabled by default, and with a priority mask which allows
459 * all interrupts through.
Peter Maydellb3387ed2012-05-02 16:49:39 +0000460 */
Peter Maydellc3037772013-04-05 16:17:59 +0100461 s->gic.cpu_enabled[0] = true;
Peter Maydellee3f0952012-12-11 11:30:37 +0000462 s->gic.priority_mask[0] = 0x100;
Peter Maydellb3387ed2012-05-02 16:49:39 +0000463 /* The NVIC as a whole is always enabled. */
Peter Maydellc3037772013-04-05 16:17:59 +0100464 s->gic.enabled = true;
Peter Maydellaecff692012-04-13 11:39:09 +0000465 systick_reset(s);
466}
467
Peter Maydell53111182013-03-05 00:34:42 +0000468static void armv7m_nvic_realize(DeviceState *dev, Error **errp)
pbrook9ee6e8b2007-11-11 00:04:49 +0000469{
Peter Maydell1e8cae42012-05-02 16:49:42 +0000470 nvic_state *s = NVIC(dev);
471 NVICClass *nc = NVIC_GET_CLASS(s);
pbrook9ee6e8b2007-11-11 00:04:49 +0000472
Peter Maydellc48c6522012-05-02 16:49:39 +0000473 /* The NVIC always has only one CPU */
474 s->gic.num_cpu = 1;
Peter Maydell306a5712012-05-02 16:49:40 +0000475 /* Tell the common code we're an NVIC */
476 s->gic.revision = 0xffffffff;
Peter Maydell55e00a12012-08-13 11:04:05 +0100477 s->num_irq = s->gic.num_irq;
Peter Maydell53111182013-03-05 00:34:42 +0000478 nc->parent_realize(dev, errp);
479 if (error_is_set(errp)) {
480 return;
481 }
Peter Maydell2b518c52012-05-02 16:49:41 +0000482 gic_init_irqs_and_distributor(&s->gic, s->num_irq);
Peter Maydell2a29dde2012-05-02 16:49:39 +0000483 /* The NVIC and system controller register area looks like this:
484 * 0..0xff : system control registers, including systick
485 * 0x100..0xcff : GIC-like registers
486 * 0xd00..0xfff : system control registers
487 * We use overlaying to put the GIC like registers
488 * over the top of the system control register region.
489 */
490 memory_region_init(&s->container, "nvic", 0x1000);
491 /* The system register region goes at the bottom of the priority
492 * stack as it covers the whole page.
493 */
494 memory_region_init_io(&s->sysregmem, &nvic_sysreg_ops, s,
495 "nvic_sysregs", 0x1000);
496 memory_region_add_subregion(&s->container, 0, &s->sysregmem);
497 /* Alias the GIC region so we can get only the section of it
498 * we need, and layer it on top of the system register region.
499 */
500 memory_region_init_alias(&s->gic_iomem_alias, "nvic-gic", &s->gic.iomem,
501 0x100, 0xc00);
Meador Inge9892cae2012-09-26 16:46:28 +0100502 memory_region_add_subregion_overlap(&s->container, 0x100,
503 &s->gic_iomem_alias, 1);
Peter Maydell2a29dde2012-05-02 16:49:39 +0000504 /* Map the whole thing into system memory at the location required
505 * by the v7M architecture.
506 */
507 memory_region_add_subregion(get_system_memory(), 0xe000e000, &s->container);
Paolo Bonzini74475452011-03-11 16:47:48 +0100508 s->systick.timer = qemu_new_timer_ns(vm_clock, systick_timer_tick, s);
pbrook9ee6e8b2007-11-11 00:04:49 +0000509}
Paul Brookfe7e8752009-05-14 22:35:08 +0100510
Peter Maydell55e00a12012-08-13 11:04:05 +0100511static void armv7m_nvic_instance_init(Object *obj)
512{
513 /* We have a different default value for the num-irq property
514 * than our superclass. This function runs after qdev init
515 * has set the defaults from the Property array and before
516 * any user-specified property setting, so just modify the
Peter Maydellfae15282012-10-12 11:54:39 +0100517 * value in the GICState struct.
Peter Maydell55e00a12012-08-13 11:04:05 +0100518 */
Peter Maydellfae15282012-10-12 11:54:39 +0100519 GICState *s = ARM_GIC_COMMON(obj);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600520 /* The ARM v7m may have anything from 0 to 496 external interrupt
521 * IRQ lines. We default to 64. Other boards may differ and should
Peter Maydell55e00a12012-08-13 11:04:05 +0100522 * set the num-irq property appropriately.
Anthony Liguori39bffca2011-12-07 21:34:16 -0600523 */
Peter Maydell55e00a12012-08-13 11:04:05 +0100524 s->num_irq = 64;
525}
Anthony Liguori39bffca2011-12-07 21:34:16 -0600526
Anthony Liguori999e12b2012-01-24 13:12:29 -0600527static void armv7m_nvic_class_init(ObjectClass *klass, void *data)
528{
Peter Maydell1e8cae42012-05-02 16:49:42 +0000529 NVICClass *nc = NVIC_CLASS(klass);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600530 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600531
Peter Maydell1e8cae42012-05-02 16:49:42 +0000532 nc->parent_reset = dc->reset;
Peter Maydell53111182013-03-05 00:34:42 +0000533 nc->parent_realize = dc->realize;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600534 dc->vmsd = &vmstate_nvic;
Peter Maydellaecff692012-04-13 11:39:09 +0000535 dc->reset = armv7m_nvic_reset;
Peter Maydell53111182013-03-05 00:34:42 +0000536 dc->realize = armv7m_nvic_realize;
Anthony Liguori999e12b2012-01-24 13:12:29 -0600537}
538
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100539static const TypeInfo armv7m_nvic_info = {
Peter Maydell1e8cae42012-05-02 16:49:42 +0000540 .name = TYPE_NVIC,
541 .parent = TYPE_ARM_GIC_COMMON,
Peter Maydell55e00a12012-08-13 11:04:05 +0100542 .instance_init = armv7m_nvic_instance_init,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600543 .instance_size = sizeof(nvic_state),
544 .class_init = armv7m_nvic_class_init,
Peter Maydell1e8cae42012-05-02 16:49:42 +0000545 .class_size = sizeof(NVICClass),
Mark Langsdorfa32134a2012-01-17 10:54:07 +0000546};
547
Andreas Färber83f7d432012-02-09 15:20:55 +0100548static void armv7m_nvic_register_types(void)
Paul Brookfe7e8752009-05-14 22:35:08 +0100549{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600550 type_register_static(&armv7m_nvic_info);
Paul Brookfe7e8752009-05-14 22:35:08 +0100551}
552
Andreas Färber83f7d432012-02-09 15:20:55 +0100553type_init(armv7m_nvic_register_types)