]> git.proxmox.com Git - mirror_qemu.git/blame - hw/intc/armv7m_nvic.c
qemu-common: push cpu.h inclusion out of qemu-common.h
[mirror_qemu.git] / hw / intc / armv7m_nvic.c
CommitLineData
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"
022c62cb 20#include "exec/address-spaces.h"
47b43a1f 21#include "gic_internal.h"
9ee6e8bb
PB
22
23typedef struct {
fae15286 24 GICState gic;
9ee6e8bb
PB
25 struct {
26 uint32_t control;
27 uint32_t reload;
28 int64_t tick;
29 QEMUTimer *timer;
30 } systick;
2a29ddee
PM
31 MemoryRegion sysregmem;
32 MemoryRegion gic_iomem_alias;
33 MemoryRegion container;
a32134aa 34 uint32_t num_irq;
e192becd 35 qemu_irq sysresetreq;
9ee6e8bb
PB
36} nvic_state;
37
1e8cae4d
PM
38#define TYPE_NVIC "armv7m_nvic"
39/**
40 * NVICClass:
41 * @parent_reset: the parent class' reset handler.
42 *
43 * A model of the v7M NVIC and System Controller
44 */
45typedef struct NVICClass {
46 /*< private >*/
47 ARMGICClass parent_class;
48 /*< public >*/
53111180 49 DeviceRealize parent_realize;
1e8cae4d
PM
50 void (*parent_reset)(DeviceState *dev);
51} NVICClass;
52
53#define NVIC_CLASS(klass) \
54 OBJECT_CLASS_CHECK(NVICClass, (klass), TYPE_NVIC)
55#define NVIC_GET_CLASS(obj) \
56 OBJECT_GET_CLASS(NVICClass, (obj), TYPE_NVIC)
57#define NVIC(obj) \
58 OBJECT_CHECK(nvic_state, (obj), TYPE_NVIC)
59
2a29ddee
PM
60static const uint8_t nvic_id[] = {
61 0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1
62};
63
9ee6e8bb
PB
64/* qemu timers run at 1GHz. We want something closer to 1MHz. */
65#define SYSTICK_SCALE 1000ULL
66
67#define SYSTICK_ENABLE (1 << 0)
68#define SYSTICK_TICKINT (1 << 1)
69#define SYSTICK_CLKSOURCE (1 << 2)
70#define SYSTICK_COUNTFLAG (1 << 16)
71
7ee930d0
BS
72int system_clock_scale;
73
e57ec016 74/* Conversion factor from qemu timer to SysTick frequencies. */
9ee6e8bb
PB
75static inline int64_t systick_scale(nvic_state *s)
76{
77 if (s->systick.control & SYSTICK_CLKSOURCE)
e57ec016 78 return system_clock_scale;
9ee6e8bb
PB
79 else
80 return 1000;
81}
82
83static void systick_reload(nvic_state *s, int reset)
84{
165cdaf8
AH
85 /* The Cortex-M3 Devices Generic User Guide says that "When the
86 * ENABLE bit is set to 1, the counter loads the RELOAD value from the
87 * SYST RVR register and then counts down". So, we need to check the
88 * ENABLE bit before reloading the value.
89 */
90 if ((s->systick.control & SYSTICK_ENABLE) == 0) {
91 return;
92 }
93
9ee6e8bb 94 if (reset)
bc72ad67 95 s->systick.tick = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
9ee6e8bb 96 s->systick.tick += (s->systick.reload + 1) * systick_scale(s);
bc72ad67 97 timer_mod(s->systick.timer, s->systick.tick);
9ee6e8bb
PB
98}
99
100static void systick_timer_tick(void * opaque)
101{
102 nvic_state *s = (nvic_state *)opaque;
103 s->systick.control |= SYSTICK_COUNTFLAG;
104 if (s->systick.control & SYSTICK_TICKINT) {
105 /* Trigger the interrupt. */
106 armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK);
107 }
108 if (s->systick.reload == 0) {
109 s->systick.control &= ~SYSTICK_ENABLE;
110 } else {
111 systick_reload(s, 0);
112 }
113}
114
aecff692
PM
115static void systick_reset(nvic_state *s)
116{
117 s->systick.control = 0;
118 s->systick.reload = 0;
119 s->systick.tick = 0;
bc72ad67 120 timer_del(s->systick.timer);
aecff692
PM
121}
122
9ee6e8bb
PB
123/* The external routines use the hardware vector numbering, ie. the first
124 IRQ is #16. The internal GIC routines use #32 as the first IRQ. */
125void armv7m_nvic_set_pending(void *opaque, int irq)
126{
127 nvic_state *s = (nvic_state *)opaque;
128 if (irq >= 16)
129 irq += 16;
fe7e8758 130 gic_set_pending_private(&s->gic, 0, irq);
9ee6e8bb
PB
131}
132
133/* Make pending IRQ active. */
134int armv7m_nvic_acknowledge_irq(void *opaque)
135{
136 nvic_state *s = (nvic_state *)opaque;
137 uint32_t irq;
138
c5619bf9 139 irq = gic_acknowledge_irq(&s->gic, 0, MEMTXATTRS_UNSPECIFIED);
9ee6e8bb 140 if (irq == 1023)
2ac71179 141 hw_error("Interrupt but no vector\n");
9ee6e8bb
PB
142 if (irq >= 32)
143 irq -= 16;
144 return irq;
145}
146
147void armv7m_nvic_complete_irq(void *opaque, int irq)
148{
149 nvic_state *s = (nvic_state *)opaque;
150 if (irq >= 16)
151 irq += 16;
f9c6a7f1 152 gic_complete_irq(&s->gic, 0, irq, MEMTXATTRS_UNSPECIFIED);
9ee6e8bb
PB
153}
154
0e8153dd 155static uint32_t nvic_readl(nvic_state *s, uint32_t offset)
9ee6e8bb 156{
4917cf44 157 ARMCPU *cpu;
9ee6e8bb
PB
158 uint32_t val;
159 int irq;
160
161 switch (offset) {
162 case 4: /* Interrupt Control Type. */
a32134aa 163 return (s->num_irq / 32) - 1;
9ee6e8bb
PB
164 case 0x10: /* SysTick Control and Status. */
165 val = s->systick.control;
166 s->systick.control &= ~SYSTICK_COUNTFLAG;
167 return val;
168 case 0x14: /* SysTick Reload Value. */
169 return s->systick.reload;
170 case 0x18: /* SysTick Current Value. */
171 {
172 int64_t t;
173 if ((s->systick.control & SYSTICK_ENABLE) == 0)
174 return 0;
bc72ad67 175 t = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
9ee6e8bb
PB
176 if (t >= s->systick.tick)
177 return 0;
178 val = ((s->systick.tick - (t + 1)) / systick_scale(s)) + 1;
179 /* The interrupt in triggered when the timer reaches zero.
180 However the counter is not reloaded until the next clock
181 tick. This is a hack to return zero during the first tick. */
182 if (val > s->systick.reload)
183 val = 0;
184 return val;
185 }
186 case 0x1c: /* SysTick Calibration Value. */
187 return 10000;
188 case 0xd00: /* CPUID Base. */
4917cf44 189 cpu = ARM_CPU(current_cpu);
e3da9921 190 return cpu->midr;
e03ba136 191 case 0xd04: /* Interrupt Control State. */
9ee6e8bb 192 /* VECTACTIVE */
b06c262b
PM
193 cpu = ARM_CPU(current_cpu);
194 val = cpu->env.v7m.exception;
9ee6e8bb
PB
195 if (val == 1023) {
196 val = 0;
197 } else if (val >= 32) {
198 val -= 16;
199 }
9ee6e8bb 200 /* VECTPENDING */
fe7e8758
PB
201 if (s->gic.current_pending[0] != 1023)
202 val |= (s->gic.current_pending[0] << 12);
b06c262b 203 /* ISRPENDING and RETTOBASE */
a32134aa 204 for (irq = 32; irq < s->num_irq; irq++) {
fe7e8758 205 if (s->gic.irq_state[irq].pending) {
9ee6e8bb
PB
206 val |= (1 << 22);
207 break;
208 }
b06c262b
PM
209 if (irq != cpu->env.v7m.exception && s->gic.irq_state[irq].active) {
210 val |= (1 << 11);
211 }
9ee6e8bb
PB
212 }
213 /* PENDSTSET */
fe7e8758 214 if (s->gic.irq_state[ARMV7M_EXCP_SYSTICK].pending)
9ee6e8bb
PB
215 val |= (1 << 26);
216 /* PENDSVSET */
fe7e8758 217 if (s->gic.irq_state[ARMV7M_EXCP_PENDSV].pending)
9ee6e8bb
PB
218 val |= (1 << 28);
219 /* NMIPENDSET */
fe7e8758 220 if (s->gic.irq_state[ARMV7M_EXCP_NMI].pending)
9ee6e8bb
PB
221 val |= (1 << 31);
222 return val;
223 case 0xd08: /* Vector Table Offset. */
4917cf44
AF
224 cpu = ARM_CPU(current_cpu);
225 return cpu->env.v7m.vecbase;
9ee6e8bb 226 case 0xd0c: /* Application Interrupt/Reset Control. */
b6fb3a89 227 return 0xfa050000;
9ee6e8bb
PB
228 case 0xd10: /* System Control. */
229 /* TODO: Implement SLEEPONEXIT. */
230 return 0;
231 case 0xd14: /* Configuration Control. */
232 /* TODO: Implement Configuration Control bits. */
233 return 0;
9ee6e8bb
PB
234 case 0xd24: /* System Handler Status. */
235 val = 0;
fe7e8758
PB
236 if (s->gic.irq_state[ARMV7M_EXCP_MEM].active) val |= (1 << 0);
237 if (s->gic.irq_state[ARMV7M_EXCP_BUS].active) val |= (1 << 1);
238 if (s->gic.irq_state[ARMV7M_EXCP_USAGE].active) val |= (1 << 3);
239 if (s->gic.irq_state[ARMV7M_EXCP_SVC].active) val |= (1 << 7);
240 if (s->gic.irq_state[ARMV7M_EXCP_DEBUG].active) val |= (1 << 8);
241 if (s->gic.irq_state[ARMV7M_EXCP_PENDSV].active) val |= (1 << 10);
242 if (s->gic.irq_state[ARMV7M_EXCP_SYSTICK].active) val |= (1 << 11);
243 if (s->gic.irq_state[ARMV7M_EXCP_USAGE].pending) val |= (1 << 12);
244 if (s->gic.irq_state[ARMV7M_EXCP_MEM].pending) val |= (1 << 13);
245 if (s->gic.irq_state[ARMV7M_EXCP_BUS].pending) val |= (1 << 14);
246 if (s->gic.irq_state[ARMV7M_EXCP_SVC].pending) val |= (1 << 15);
247 if (s->gic.irq_state[ARMV7M_EXCP_MEM].enabled) val |= (1 << 16);
248 if (s->gic.irq_state[ARMV7M_EXCP_BUS].enabled) val |= (1 << 17);
249 if (s->gic.irq_state[ARMV7M_EXCP_USAGE].enabled) val |= (1 << 18);
9ee6e8bb
PB
250 return val;
251 case 0xd28: /* Configurable Fault Status. */
252 /* TODO: Implement Fault Status. */
e72e3ffc 253 qemu_log_mask(LOG_UNIMP, "Configurable Fault Status unimplemented\n");
9ee6e8bb
PB
254 return 0;
255 case 0xd2c: /* Hard Fault Status. */
256 case 0xd30: /* Debug Fault Status. */
257 case 0xd34: /* Mem Manage Address. */
258 case 0xd38: /* Bus Fault Address. */
259 case 0xd3c: /* Aux Fault Status. */
260 /* TODO: Implement fault status registers. */
e72e3ffc
PM
261 qemu_log_mask(LOG_UNIMP, "Fault status registers unimplemented\n");
262 return 0;
9ee6e8bb
PB
263 case 0xd40: /* PFR0. */
264 return 0x00000030;
265 case 0xd44: /* PRF1. */
266 return 0x00000200;
267 case 0xd48: /* DFR0. */
268 return 0x00100000;
269 case 0xd4c: /* AFR0. */
270 return 0x00000000;
271 case 0xd50: /* MMFR0. */
272 return 0x00000030;
273 case 0xd54: /* MMFR1. */
274 return 0x00000000;
275 case 0xd58: /* MMFR2. */
276 return 0x00000000;
277 case 0xd5c: /* MMFR3. */
278 return 0x00000000;
279 case 0xd60: /* ISAR0. */
280 return 0x01141110;
281 case 0xd64: /* ISAR1. */
282 return 0x02111000;
283 case 0xd68: /* ISAR2. */
284 return 0x21112231;
285 case 0xd6c: /* ISAR3. */
286 return 0x01111110;
287 case 0xd70: /* ISAR4. */
288 return 0x01310102;
289 /* TODO: Implement debug registers. */
290 default:
e72e3ffc
PM
291 qemu_log_mask(LOG_GUEST_ERROR, "NVIC: Bad read offset 0x%x\n", offset);
292 return 0;
9ee6e8bb
PB
293 }
294}
295
0e8153dd 296static void nvic_writel(nvic_state *s, uint32_t offset, uint32_t value)
9ee6e8bb 297{
4917cf44 298 ARMCPU *cpu;
9ee6e8bb
PB
299 uint32_t oldval;
300 switch (offset) {
301 case 0x10: /* SysTick Control and Status. */
302 oldval = s->systick.control;
303 s->systick.control &= 0xfffffff8;
304 s->systick.control |= value & 7;
305 if ((oldval ^ value) & SYSTICK_ENABLE) {
bc72ad67 306 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
9ee6e8bb
PB
307 if (value & SYSTICK_ENABLE) {
308 if (s->systick.tick) {
309 s->systick.tick += now;
bc72ad67 310 timer_mod(s->systick.timer, s->systick.tick);
9ee6e8bb
PB
311 } else {
312 systick_reload(s, 1);
313 }
314 } else {
bc72ad67 315 timer_del(s->systick.timer);
9ee6e8bb
PB
316 s->systick.tick -= now;
317 if (s->systick.tick < 0)
318 s->systick.tick = 0;
319 }
320 } else if ((oldval ^ value) & SYSTICK_CLKSOURCE) {
321 /* This is a hack. Force the timer to be reloaded
322 when the reference clock is changed. */
323 systick_reload(s, 1);
324 }
325 break;
326 case 0x14: /* SysTick Reload Value. */
327 s->systick.reload = value;
328 break;
329 case 0x18: /* SysTick Current Value. Writes reload the timer. */
330 systick_reload(s, 1);
331 s->systick.control &= ~SYSTICK_COUNTFLAG;
332 break;
333 case 0xd04: /* Interrupt Control State. */
334 if (value & (1 << 31)) {
335 armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI);
336 }
337 if (value & (1 << 28)) {
338 armv7m_nvic_set_pending(s, ARMV7M_EXCP_PENDSV);
339 } else if (value & (1 << 27)) {
fe7e8758
PB
340 s->gic.irq_state[ARMV7M_EXCP_PENDSV].pending = 0;
341 gic_update(&s->gic);
9ee6e8bb
PB
342 }
343 if (value & (1 << 26)) {
344 armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK);
345 } else if (value & (1 << 25)) {
fe7e8758
PB
346 s->gic.irq_state[ARMV7M_EXCP_SYSTICK].pending = 0;
347 gic_update(&s->gic);
9ee6e8bb
PB
348 }
349 break;
350 case 0xd08: /* Vector Table Offset. */
4917cf44
AF
351 cpu = ARM_CPU(current_cpu);
352 cpu->env.v7m.vecbase = value & 0xffffff80;
9ee6e8bb
PB
353 break;
354 case 0xd0c: /* Application Interrupt/Reset Control. */
355 if ((value >> 16) == 0x05fa) {
e192becd
MD
356 if (value & 4) {
357 qemu_irq_pulse(s->sysresetreq);
358 }
9ee6e8bb 359 if (value & 2) {
e72e3ffc 360 qemu_log_mask(LOG_UNIMP, "VECTCLRACTIVE unimplemented\n");
9ee6e8bb 361 }
e192becd 362 if (value & 1) {
e72e3ffc 363 qemu_log_mask(LOG_UNIMP, "AIRCR system reset unimplemented\n");
9ee6e8bb 364 }
b6fb3a89
OA
365 if (value & 0x700) {
366 qemu_log_mask(LOG_UNIMP, "PRIGROUP unimplemented\n");
367 }
9ee6e8bb
PB
368 }
369 break;
370 case 0xd10: /* System Control. */
371 case 0xd14: /* Configuration Control. */
372 /* TODO: Implement control registers. */
e72e3ffc
PM
373 qemu_log_mask(LOG_UNIMP, "NVIC: SCR and CCR unimplemented\n");
374 break;
9ee6e8bb
PB
375 case 0xd24: /* System Handler Control. */
376 /* TODO: Real hardware allows you to set/clear the active bits
377 under some circumstances. We don't implement this. */
fe7e8758
PB
378 s->gic.irq_state[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0;
379 s->gic.irq_state[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0;
380 s->gic.irq_state[ARMV7M_EXCP_USAGE].enabled = (value & (1 << 18)) != 0;
9ee6e8bb
PB
381 break;
382 case 0xd28: /* Configurable Fault Status. */
383 case 0xd2c: /* Hard Fault Status. */
384 case 0xd30: /* Debug Fault Status. */
385 case 0xd34: /* Mem Manage Address. */
386 case 0xd38: /* Bus Fault Address. */
387 case 0xd3c: /* Aux Fault Status. */
e72e3ffc
PM
388 qemu_log_mask(LOG_UNIMP,
389 "NVIC: fault status registers unimplemented\n");
390 break;
2a29ddee
PM
391 case 0xf00: /* Software Triggered Interrupt Register */
392 if ((value & 0x1ff) < s->num_irq) {
393 gic_set_pending_private(&s->gic, 0, value & 0x1ff);
394 }
395 break;
9ee6e8bb 396 default:
e72e3ffc
PM
397 qemu_log_mask(LOG_GUEST_ERROR,
398 "NVIC: Bad write offset 0x%x\n", offset);
9ee6e8bb
PB
399 }
400}
401
a8170e5e 402static uint64_t nvic_sysreg_read(void *opaque, hwaddr addr,
2a29ddee
PM
403 unsigned size)
404{
0e8153dd 405 nvic_state *s = (nvic_state *)opaque;
2a29ddee 406 uint32_t offset = addr;
0e8153dd
AB
407 int i;
408 uint32_t val;
409
410 switch (offset) {
411 case 0xd18 ... 0xd23: /* System Handler Priority. */
412 val = 0;
413 for (i = 0; i < size; i++) {
414 val |= s->gic.priority1[(offset - 0xd14) + i][0] << (i * 8);
415 }
416 return val;
417 case 0xfe0 ... 0xfff: /* ID. */
2a29ddee
PM
418 if (offset & 3) {
419 return 0;
420 }
421 return nvic_id[(offset - 0xfe0) >> 2];
422 }
423 if (size == 4) {
0e8153dd 424 return nvic_readl(s, offset);
2a29ddee 425 }
e72e3ffc
PM
426 qemu_log_mask(LOG_GUEST_ERROR,
427 "NVIC: Bad read of size %d at offset 0x%x\n", size, offset);
428 return 0;
2a29ddee
PM
429}
430
a8170e5e 431static void nvic_sysreg_write(void *opaque, hwaddr addr,
2a29ddee
PM
432 uint64_t value, unsigned size)
433{
0e8153dd 434 nvic_state *s = (nvic_state *)opaque;
2a29ddee 435 uint32_t offset = addr;
0e8153dd
AB
436 int i;
437
438 switch (offset) {
439 case 0xd18 ... 0xd23: /* System Handler Priority. */
440 for (i = 0; i < size; i++) {
441 s->gic.priority1[(offset - 0xd14) + i][0] =
442 (value >> (i * 8)) & 0xff;
443 }
444 gic_update(&s->gic);
445 return;
446 }
2a29ddee 447 if (size == 4) {
0e8153dd 448 nvic_writel(s, offset, value);
2a29ddee
PM
449 return;
450 }
e72e3ffc
PM
451 qemu_log_mask(LOG_GUEST_ERROR,
452 "NVIC: Bad write of size %d at offset 0x%x\n", size, offset);
2a29ddee
PM
453}
454
455static const MemoryRegionOps nvic_sysreg_ops = {
456 .read = nvic_sysreg_read,
457 .write = nvic_sysreg_write,
458 .endianness = DEVICE_NATIVE_ENDIAN,
459};
460
0797226c
JQ
461static const VMStateDescription vmstate_nvic = {
462 .name = "armv7m_nvic",
463 .version_id = 1,
464 .minimum_version_id = 1,
8f1e884b 465 .fields = (VMStateField[]) {
0797226c
JQ
466 VMSTATE_UINT32(systick.control, nvic_state),
467 VMSTATE_UINT32(systick.reload, nvic_state),
468 VMSTATE_INT64(systick.tick, nvic_state),
e720677e 469 VMSTATE_TIMER_PTR(systick.timer, nvic_state),
0797226c
JQ
470 VMSTATE_END_OF_LIST()
471 }
472};
23e39294 473
aecff692
PM
474static void armv7m_nvic_reset(DeviceState *dev)
475{
1e8cae4d
PM
476 nvic_state *s = NVIC(dev);
477 NVICClass *nc = NVIC_GET_CLASS(s);
478 nc->parent_reset(dev);
b3387ede
PM
479 /* Common GIC reset resets to disabled; the NVIC doesn't have
480 * per-CPU interfaces so mark our non-existent CPU interface
ee3f0956
PM
481 * as enabled by default, and with a priority mask which allows
482 * all interrupts through.
b3387ede 483 */
32951860 484 s->gic.cpu_ctlr[0] = GICC_CTLR_EN_GRP0;
ee3f0956 485 s->gic.priority_mask[0] = 0x100;
b3387ede 486 /* The NVIC as a whole is always enabled. */
679aa175 487 s->gic.ctlr = 1;
aecff692
PM
488 systick_reset(s);
489}
490
53111180 491static void armv7m_nvic_realize(DeviceState *dev, Error **errp)
9ee6e8bb 492{
1e8cae4d
PM
493 nvic_state *s = NVIC(dev);
494 NVICClass *nc = NVIC_GET_CLASS(s);
0175ba10 495 Error *local_err = NULL;
9ee6e8bb 496
c48c6522
PM
497 /* The NVIC always has only one CPU */
498 s->gic.num_cpu = 1;
306a571a
PM
499 /* Tell the common code we're an NVIC */
500 s->gic.revision = 0xffffffff;
55e00a19 501 s->num_irq = s->gic.num_irq;
0175ba10
MA
502 nc->parent_realize(dev, &local_err);
503 if (local_err) {
504 error_propagate(errp, local_err);
53111180
PM
505 return;
506 }
7b95a508 507 gic_init_irqs_and_distributor(&s->gic);
2a29ddee
PM
508 /* The NVIC and system controller register area looks like this:
509 * 0..0xff : system control registers, including systick
510 * 0x100..0xcff : GIC-like registers
511 * 0xd00..0xfff : system control registers
512 * We use overlaying to put the GIC like registers
513 * over the top of the system control register region.
514 */
1437c94b 515 memory_region_init(&s->container, OBJECT(s), "nvic", 0x1000);
2a29ddee
PM
516 /* The system register region goes at the bottom of the priority
517 * stack as it covers the whole page.
518 */
1437c94b 519 memory_region_init_io(&s->sysregmem, OBJECT(s), &nvic_sysreg_ops, s,
2a29ddee
PM
520 "nvic_sysregs", 0x1000);
521 memory_region_add_subregion(&s->container, 0, &s->sysregmem);
522 /* Alias the GIC region so we can get only the section of it
523 * we need, and layer it on top of the system register region.
524 */
1437c94b
PB
525 memory_region_init_alias(&s->gic_iomem_alias, OBJECT(s),
526 "nvic-gic", &s->gic.iomem,
2a29ddee 527 0x100, 0xc00);
9892cae3
MI
528 memory_region_add_subregion_overlap(&s->container, 0x100,
529 &s->gic_iomem_alias, 1);
2a29ddee
PM
530 /* Map the whole thing into system memory at the location required
531 * by the v7M architecture.
532 */
533 memory_region_add_subregion(get_system_memory(), 0xe000e000, &s->container);
bc72ad67 534 s->systick.timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, systick_timer_tick, s);
9ee6e8bb 535}
fe7e8758 536
55e00a19
PM
537static void armv7m_nvic_instance_init(Object *obj)
538{
539 /* We have a different default value for the num-irq property
540 * than our superclass. This function runs after qdev init
541 * has set the defaults from the Property array and before
542 * any user-specified property setting, so just modify the
fae15286 543 * value in the GICState struct.
55e00a19 544 */
fae15286 545 GICState *s = ARM_GIC_COMMON(obj);
e192becd
MD
546 DeviceState *dev = DEVICE(obj);
547 nvic_state *nvic = NVIC(obj);
39bffca2
AL
548 /* The ARM v7m may have anything from 0 to 496 external interrupt
549 * IRQ lines. We default to 64. Other boards may differ and should
55e00a19 550 * set the num-irq property appropriately.
39bffca2 551 */
55e00a19 552 s->num_irq = 64;
e192becd 553 qdev_init_gpio_out_named(dev, &nvic->sysresetreq, "SYSRESETREQ", 1);
55e00a19 554}
39bffca2 555
999e12bb
AL
556static void armv7m_nvic_class_init(ObjectClass *klass, void *data)
557{
1e8cae4d 558 NVICClass *nc = NVIC_CLASS(klass);
39bffca2 559 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 560
1e8cae4d 561 nc->parent_reset = dc->reset;
53111180 562 nc->parent_realize = dc->realize;
39bffca2 563 dc->vmsd = &vmstate_nvic;
aecff692 564 dc->reset = armv7m_nvic_reset;
53111180 565 dc->realize = armv7m_nvic_realize;
999e12bb
AL
566}
567
8c43a6f0 568static const TypeInfo armv7m_nvic_info = {
1e8cae4d
PM
569 .name = TYPE_NVIC,
570 .parent = TYPE_ARM_GIC_COMMON,
55e00a19 571 .instance_init = armv7m_nvic_instance_init,
39bffca2
AL
572 .instance_size = sizeof(nvic_state),
573 .class_init = armv7m_nvic_class_init,
1e8cae4d 574 .class_size = sizeof(NVICClass),
a32134aa
ML
575};
576
83f7d43a 577static void armv7m_nvic_register_types(void)
fe7e8758 578{
39bffca2 579 type_register_static(&armv7m_nvic_info);
fe7e8758
PB
580}
581
83f7d43a 582type_init(armv7m_nvic_register_types)