]>
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" |
022c62cb | 20 | #include "exec/address-spaces.h" |
47b43a1f | 21 | #include "gic_internal.h" |
03dd024f | 22 | #include "qemu/log.h" |
9ee6e8bb PB |
23 | |
24 | typedef struct { | |
fae15286 | 25 | GICState gic; |
d713ea6c | 26 | ARMCPU *cpu; |
9ee6e8bb PB |
27 | struct { |
28 | uint32_t control; | |
29 | uint32_t reload; | |
30 | int64_t tick; | |
31 | QEMUTimer *timer; | |
32 | } systick; | |
2a29ddee PM |
33 | MemoryRegion sysregmem; |
34 | MemoryRegion gic_iomem_alias; | |
35 | MemoryRegion container; | |
a32134aa | 36 | uint32_t num_irq; |
e192becd | 37 | qemu_irq sysresetreq; |
9ee6e8bb PB |
38 | } nvic_state; |
39 | ||
1e8cae4d PM |
40 | #define TYPE_NVIC "armv7m_nvic" |
41 | /** | |
42 | * NVICClass: | |
43 | * @parent_reset: the parent class' reset handler. | |
44 | * | |
45 | * A model of the v7M NVIC and System Controller | |
46 | */ | |
47 | typedef struct NVICClass { | |
48 | /*< private >*/ | |
49 | ARMGICClass parent_class; | |
50 | /*< public >*/ | |
53111180 | 51 | DeviceRealize parent_realize; |
1e8cae4d PM |
52 | void (*parent_reset)(DeviceState *dev); |
53 | } NVICClass; | |
54 | ||
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) | |
59 | #define NVIC(obj) \ | |
60 | OBJECT_CHECK(nvic_state, (obj), TYPE_NVIC) | |
61 | ||
2a29ddee PM |
62 | static const uint8_t nvic_id[] = { |
63 | 0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1 | |
64 | }; | |
65 | ||
9ee6e8bb PB |
66 | /* qemu timers run at 1GHz. We want something closer to 1MHz. */ |
67 | #define SYSTICK_SCALE 1000ULL | |
68 | ||
69 | #define SYSTICK_ENABLE (1 << 0) | |
70 | #define SYSTICK_TICKINT (1 << 1) | |
71 | #define SYSTICK_CLKSOURCE (1 << 2) | |
72 | #define SYSTICK_COUNTFLAG (1 << 16) | |
73 | ||
7ee930d0 BS |
74 | int system_clock_scale; |
75 | ||
e57ec016 | 76 | /* Conversion factor from qemu timer to SysTick frequencies. */ |
9ee6e8bb PB |
77 | static inline int64_t systick_scale(nvic_state *s) |
78 | { | |
79 | if (s->systick.control & SYSTICK_CLKSOURCE) | |
e57ec016 | 80 | return system_clock_scale; |
9ee6e8bb PB |
81 | else |
82 | return 1000; | |
83 | } | |
84 | ||
85 | static void systick_reload(nvic_state *s, int reset) | |
86 | { | |
165cdaf8 AH |
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. | |
91 | */ | |
92 | if ((s->systick.control & SYSTICK_ENABLE) == 0) { | |
93 | return; | |
94 | } | |
95 | ||
9ee6e8bb | 96 | if (reset) |
bc72ad67 | 97 | s->systick.tick = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
9ee6e8bb | 98 | s->systick.tick += (s->systick.reload + 1) * systick_scale(s); |
bc72ad67 | 99 | timer_mod(s->systick.timer, s->systick.tick); |
9ee6e8bb PB |
100 | } |
101 | ||
102 | static void systick_timer_tick(void * opaque) | |
103 | { | |
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); | |
109 | } | |
110 | if (s->systick.reload == 0) { | |
111 | s->systick.control &= ~SYSTICK_ENABLE; | |
112 | } else { | |
113 | systick_reload(s, 0); | |
114 | } | |
115 | } | |
116 | ||
aecff692 PM |
117 | static void systick_reset(nvic_state *s) |
118 | { | |
119 | s->systick.control = 0; | |
120 | s->systick.reload = 0; | |
121 | s->systick.tick = 0; | |
bc72ad67 | 122 | timer_del(s->systick.timer); |
aecff692 PM |
123 | } |
124 | ||
9ee6e8bb PB |
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) | |
128 | { | |
129 | nvic_state *s = (nvic_state *)opaque; | |
130 | if (irq >= 16) | |
131 | irq += 16; | |
fe7e8758 | 132 | gic_set_pending_private(&s->gic, 0, irq); |
9ee6e8bb PB |
133 | } |
134 | ||
135 | /* Make pending IRQ active. */ | |
136 | int armv7m_nvic_acknowledge_irq(void *opaque) | |
137 | { | |
138 | nvic_state *s = (nvic_state *)opaque; | |
139 | uint32_t irq; | |
140 | ||
c5619bf9 | 141 | irq = gic_acknowledge_irq(&s->gic, 0, MEMTXATTRS_UNSPECIFIED); |
9ee6e8bb | 142 | if (irq == 1023) |
2ac71179 | 143 | hw_error("Interrupt but no vector\n"); |
9ee6e8bb PB |
144 | if (irq >= 32) |
145 | irq -= 16; | |
146 | return irq; | |
147 | } | |
148 | ||
149 | void armv7m_nvic_complete_irq(void *opaque, int irq) | |
150 | { | |
151 | nvic_state *s = (nvic_state *)opaque; | |
152 | if (irq >= 16) | |
153 | irq += 16; | |
f9c6a7f1 | 154 | gic_complete_irq(&s->gic, 0, irq, MEMTXATTRS_UNSPECIFIED); |
9ee6e8bb PB |
155 | } |
156 | ||
0e8153dd | 157 | static uint32_t nvic_readl(nvic_state *s, uint32_t offset) |
9ee6e8bb | 158 | { |
d713ea6c | 159 | ARMCPU *cpu = s->cpu; |
9ee6e8bb PB |
160 | uint32_t val; |
161 | int irq; | |
162 | ||
163 | switch (offset) { | |
164 | case 4: /* Interrupt Control Type. */ | |
a32134aa | 165 | return (s->num_irq / 32) - 1; |
9ee6e8bb PB |
166 | case 0x10: /* SysTick Control and Status. */ |
167 | val = s->systick.control; | |
168 | s->systick.control &= ~SYSTICK_COUNTFLAG; | |
169 | return val; | |
170 | case 0x14: /* SysTick Reload Value. */ | |
171 | return s->systick.reload; | |
172 | case 0x18: /* SysTick Current Value. */ | |
173 | { | |
174 | int64_t t; | |
175 | if ((s->systick.control & SYSTICK_ENABLE) == 0) | |
176 | return 0; | |
bc72ad67 | 177 | t = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
9ee6e8bb PB |
178 | if (t >= s->systick.tick) |
179 | return 0; | |
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) | |
185 | val = 0; | |
186 | return val; | |
187 | } | |
188 | case 0x1c: /* SysTick Calibration Value. */ | |
189 | return 10000; | |
190 | case 0xd00: /* CPUID Base. */ | |
e3da9921 | 191 | return cpu->midr; |
e03ba136 | 192 | case 0xd04: /* Interrupt Control State. */ |
9ee6e8bb | 193 | /* VECTACTIVE */ |
b06c262b | 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 | 224 | return cpu->env.v7m.vecbase; |
9ee6e8bb | 225 | case 0xd0c: /* Application Interrupt/Reset Control. */ |
b6fb3a89 | 226 | return 0xfa050000; |
9ee6e8bb PB |
227 | case 0xd10: /* System Control. */ |
228 | /* TODO: Implement SLEEPONEXIT. */ | |
229 | return 0; | |
230 | case 0xd14: /* Configuration Control. */ | |
231 | /* TODO: Implement Configuration Control bits. */ | |
232 | return 0; | |
9ee6e8bb PB |
233 | case 0xd24: /* System Handler Status. */ |
234 | val = 0; | |
fe7e8758 PB |
235 | if (s->gic.irq_state[ARMV7M_EXCP_MEM].active) val |= (1 << 0); |
236 | if (s->gic.irq_state[ARMV7M_EXCP_BUS].active) val |= (1 << 1); | |
237 | if (s->gic.irq_state[ARMV7M_EXCP_USAGE].active) val |= (1 << 3); | |
238 | if (s->gic.irq_state[ARMV7M_EXCP_SVC].active) val |= (1 << 7); | |
239 | if (s->gic.irq_state[ARMV7M_EXCP_DEBUG].active) val |= (1 << 8); | |
240 | if (s->gic.irq_state[ARMV7M_EXCP_PENDSV].active) val |= (1 << 10); | |
241 | if (s->gic.irq_state[ARMV7M_EXCP_SYSTICK].active) val |= (1 << 11); | |
242 | if (s->gic.irq_state[ARMV7M_EXCP_USAGE].pending) val |= (1 << 12); | |
243 | if (s->gic.irq_state[ARMV7M_EXCP_MEM].pending) val |= (1 << 13); | |
244 | if (s->gic.irq_state[ARMV7M_EXCP_BUS].pending) val |= (1 << 14); | |
245 | if (s->gic.irq_state[ARMV7M_EXCP_SVC].pending) val |= (1 << 15); | |
246 | if (s->gic.irq_state[ARMV7M_EXCP_MEM].enabled) val |= (1 << 16); | |
247 | if (s->gic.irq_state[ARMV7M_EXCP_BUS].enabled) val |= (1 << 17); | |
248 | if (s->gic.irq_state[ARMV7M_EXCP_USAGE].enabled) val |= (1 << 18); | |
9ee6e8bb PB |
249 | return val; |
250 | case 0xd28: /* Configurable Fault Status. */ | |
251 | /* TODO: Implement Fault Status. */ | |
e72e3ffc | 252 | qemu_log_mask(LOG_UNIMP, "Configurable Fault Status unimplemented\n"); |
9ee6e8bb PB |
253 | return 0; |
254 | case 0xd2c: /* Hard Fault Status. */ | |
255 | case 0xd30: /* Debug Fault Status. */ | |
256 | case 0xd34: /* Mem Manage Address. */ | |
257 | case 0xd38: /* Bus Fault Address. */ | |
258 | case 0xd3c: /* Aux Fault Status. */ | |
259 | /* TODO: Implement fault status registers. */ | |
e72e3ffc PM |
260 | qemu_log_mask(LOG_UNIMP, "Fault status registers unimplemented\n"); |
261 | return 0; | |
9ee6e8bb PB |
262 | case 0xd40: /* PFR0. */ |
263 | return 0x00000030; | |
264 | case 0xd44: /* PRF1. */ | |
265 | return 0x00000200; | |
266 | case 0xd48: /* DFR0. */ | |
267 | return 0x00100000; | |
268 | case 0xd4c: /* AFR0. */ | |
269 | return 0x00000000; | |
270 | case 0xd50: /* MMFR0. */ | |
271 | return 0x00000030; | |
272 | case 0xd54: /* MMFR1. */ | |
273 | return 0x00000000; | |
274 | case 0xd58: /* MMFR2. */ | |
275 | return 0x00000000; | |
276 | case 0xd5c: /* MMFR3. */ | |
277 | return 0x00000000; | |
278 | case 0xd60: /* ISAR0. */ | |
279 | return 0x01141110; | |
280 | case 0xd64: /* ISAR1. */ | |
281 | return 0x02111000; | |
282 | case 0xd68: /* ISAR2. */ | |
283 | return 0x21112231; | |
284 | case 0xd6c: /* ISAR3. */ | |
285 | return 0x01111110; | |
286 | case 0xd70: /* ISAR4. */ | |
287 | return 0x01310102; | |
288 | /* TODO: Implement debug registers. */ | |
289 | default: | |
e72e3ffc PM |
290 | qemu_log_mask(LOG_GUEST_ERROR, "NVIC: Bad read offset 0x%x\n", offset); |
291 | return 0; | |
9ee6e8bb PB |
292 | } |
293 | } | |
294 | ||
0e8153dd | 295 | static void nvic_writel(nvic_state *s, uint32_t offset, uint32_t value) |
9ee6e8bb | 296 | { |
d713ea6c | 297 | ARMCPU *cpu = s->cpu; |
9ee6e8bb PB |
298 | uint32_t oldval; |
299 | switch (offset) { | |
300 | case 0x10: /* SysTick Control and Status. */ | |
301 | oldval = s->systick.control; | |
302 | s->systick.control &= 0xfffffff8; | |
303 | s->systick.control |= value & 7; | |
304 | if ((oldval ^ value) & SYSTICK_ENABLE) { | |
bc72ad67 | 305 | int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
9ee6e8bb PB |
306 | if (value & SYSTICK_ENABLE) { |
307 | if (s->systick.tick) { | |
308 | s->systick.tick += now; | |
bc72ad67 | 309 | timer_mod(s->systick.timer, s->systick.tick); |
9ee6e8bb PB |
310 | } else { |
311 | systick_reload(s, 1); | |
312 | } | |
313 | } else { | |
bc72ad67 | 314 | timer_del(s->systick.timer); |
9ee6e8bb PB |
315 | s->systick.tick -= now; |
316 | if (s->systick.tick < 0) | |
317 | s->systick.tick = 0; | |
318 | } | |
319 | } else if ((oldval ^ value) & SYSTICK_CLKSOURCE) { | |
320 | /* This is a hack. Force the timer to be reloaded | |
321 | when the reference clock is changed. */ | |
322 | systick_reload(s, 1); | |
323 | } | |
324 | break; | |
325 | case 0x14: /* SysTick Reload Value. */ | |
326 | s->systick.reload = value; | |
327 | break; | |
328 | case 0x18: /* SysTick Current Value. Writes reload the timer. */ | |
329 | systick_reload(s, 1); | |
330 | s->systick.control &= ~SYSTICK_COUNTFLAG; | |
331 | break; | |
332 | case 0xd04: /* Interrupt Control State. */ | |
333 | if (value & (1 << 31)) { | |
334 | armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI); | |
335 | } | |
336 | if (value & (1 << 28)) { | |
337 | armv7m_nvic_set_pending(s, ARMV7M_EXCP_PENDSV); | |
338 | } else if (value & (1 << 27)) { | |
fe7e8758 PB |
339 | s->gic.irq_state[ARMV7M_EXCP_PENDSV].pending = 0; |
340 | gic_update(&s->gic); | |
9ee6e8bb PB |
341 | } |
342 | if (value & (1 << 26)) { | |
343 | armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK); | |
344 | } else if (value & (1 << 25)) { | |
fe7e8758 PB |
345 | s->gic.irq_state[ARMV7M_EXCP_SYSTICK].pending = 0; |
346 | gic_update(&s->gic); | |
9ee6e8bb PB |
347 | } |
348 | break; | |
349 | case 0xd08: /* Vector Table Offset. */ | |
4917cf44 | 350 | cpu->env.v7m.vecbase = value & 0xffffff80; |
9ee6e8bb PB |
351 | break; |
352 | case 0xd0c: /* Application Interrupt/Reset Control. */ | |
353 | if ((value >> 16) == 0x05fa) { | |
e192becd MD |
354 | if (value & 4) { |
355 | qemu_irq_pulse(s->sysresetreq); | |
356 | } | |
9ee6e8bb | 357 | if (value & 2) { |
e72e3ffc | 358 | qemu_log_mask(LOG_UNIMP, "VECTCLRACTIVE unimplemented\n"); |
9ee6e8bb | 359 | } |
e192becd | 360 | if (value & 1) { |
e72e3ffc | 361 | qemu_log_mask(LOG_UNIMP, "AIRCR system reset unimplemented\n"); |
9ee6e8bb | 362 | } |
b6fb3a89 OA |
363 | if (value & 0x700) { |
364 | qemu_log_mask(LOG_UNIMP, "PRIGROUP unimplemented\n"); | |
365 | } | |
9ee6e8bb PB |
366 | } |
367 | break; | |
368 | case 0xd10: /* System Control. */ | |
369 | case 0xd14: /* Configuration Control. */ | |
370 | /* TODO: Implement control registers. */ | |
e72e3ffc PM |
371 | qemu_log_mask(LOG_UNIMP, "NVIC: SCR and CCR unimplemented\n"); |
372 | break; | |
9ee6e8bb PB |
373 | case 0xd24: /* System Handler Control. */ |
374 | /* TODO: Real hardware allows you to set/clear the active bits | |
375 | under some circumstances. We don't implement this. */ | |
fe7e8758 PB |
376 | s->gic.irq_state[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0; |
377 | s->gic.irq_state[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0; | |
378 | s->gic.irq_state[ARMV7M_EXCP_USAGE].enabled = (value & (1 << 18)) != 0; | |
9ee6e8bb PB |
379 | break; |
380 | case 0xd28: /* Configurable Fault Status. */ | |
381 | case 0xd2c: /* Hard Fault Status. */ | |
382 | case 0xd30: /* Debug Fault Status. */ | |
383 | case 0xd34: /* Mem Manage Address. */ | |
384 | case 0xd38: /* Bus Fault Address. */ | |
385 | case 0xd3c: /* Aux Fault Status. */ | |
e72e3ffc PM |
386 | qemu_log_mask(LOG_UNIMP, |
387 | "NVIC: fault status registers unimplemented\n"); | |
388 | break; | |
2a29ddee PM |
389 | case 0xf00: /* Software Triggered Interrupt Register */ |
390 | if ((value & 0x1ff) < s->num_irq) { | |
391 | gic_set_pending_private(&s->gic, 0, value & 0x1ff); | |
392 | } | |
393 | break; | |
9ee6e8bb | 394 | default: |
e72e3ffc PM |
395 | qemu_log_mask(LOG_GUEST_ERROR, |
396 | "NVIC: Bad write offset 0x%x\n", offset); | |
9ee6e8bb PB |
397 | } |
398 | } | |
399 | ||
a8170e5e | 400 | static uint64_t nvic_sysreg_read(void *opaque, hwaddr addr, |
2a29ddee PM |
401 | unsigned size) |
402 | { | |
0e8153dd | 403 | nvic_state *s = (nvic_state *)opaque; |
2a29ddee | 404 | uint32_t offset = addr; |
0e8153dd AB |
405 | int i; |
406 | uint32_t val; | |
407 | ||
408 | switch (offset) { | |
409 | case 0xd18 ... 0xd23: /* System Handler Priority. */ | |
410 | val = 0; | |
411 | for (i = 0; i < size; i++) { | |
412 | val |= s->gic.priority1[(offset - 0xd14) + i][0] << (i * 8); | |
413 | } | |
414 | return val; | |
415 | case 0xfe0 ... 0xfff: /* ID. */ | |
2a29ddee PM |
416 | if (offset & 3) { |
417 | return 0; | |
418 | } | |
419 | return nvic_id[(offset - 0xfe0) >> 2]; | |
420 | } | |
421 | if (size == 4) { | |
0e8153dd | 422 | return nvic_readl(s, offset); |
2a29ddee | 423 | } |
e72e3ffc PM |
424 | qemu_log_mask(LOG_GUEST_ERROR, |
425 | "NVIC: Bad read of size %d at offset 0x%x\n", size, offset); | |
426 | return 0; | |
2a29ddee PM |
427 | } |
428 | ||
a8170e5e | 429 | static void nvic_sysreg_write(void *opaque, hwaddr addr, |
2a29ddee PM |
430 | uint64_t value, unsigned size) |
431 | { | |
0e8153dd | 432 | nvic_state *s = (nvic_state *)opaque; |
2a29ddee | 433 | uint32_t offset = addr; |
0e8153dd AB |
434 | int i; |
435 | ||
436 | switch (offset) { | |
437 | case 0xd18 ... 0xd23: /* System Handler Priority. */ | |
438 | for (i = 0; i < size; i++) { | |
439 | s->gic.priority1[(offset - 0xd14) + i][0] = | |
440 | (value >> (i * 8)) & 0xff; | |
441 | } | |
442 | gic_update(&s->gic); | |
443 | return; | |
444 | } | |
2a29ddee | 445 | if (size == 4) { |
0e8153dd | 446 | nvic_writel(s, offset, value); |
2a29ddee PM |
447 | return; |
448 | } | |
e72e3ffc PM |
449 | qemu_log_mask(LOG_GUEST_ERROR, |
450 | "NVIC: Bad write of size %d at offset 0x%x\n", size, offset); | |
2a29ddee PM |
451 | } |
452 | ||
453 | static const MemoryRegionOps nvic_sysreg_ops = { | |
454 | .read = nvic_sysreg_read, | |
455 | .write = nvic_sysreg_write, | |
456 | .endianness = DEVICE_NATIVE_ENDIAN, | |
457 | }; | |
458 | ||
0797226c JQ |
459 | static const VMStateDescription vmstate_nvic = { |
460 | .name = "armv7m_nvic", | |
461 | .version_id = 1, | |
462 | .minimum_version_id = 1, | |
8f1e884b | 463 | .fields = (VMStateField[]) { |
0797226c JQ |
464 | VMSTATE_UINT32(systick.control, nvic_state), |
465 | VMSTATE_UINT32(systick.reload, nvic_state), | |
466 | VMSTATE_INT64(systick.tick, nvic_state), | |
e720677e | 467 | VMSTATE_TIMER_PTR(systick.timer, nvic_state), |
0797226c JQ |
468 | VMSTATE_END_OF_LIST() |
469 | } | |
470 | }; | |
23e39294 | 471 | |
aecff692 PM |
472 | static void armv7m_nvic_reset(DeviceState *dev) |
473 | { | |
1e8cae4d PM |
474 | nvic_state *s = NVIC(dev); |
475 | NVICClass *nc = NVIC_GET_CLASS(s); | |
476 | nc->parent_reset(dev); | |
b3387ede PM |
477 | /* Common GIC reset resets to disabled; the NVIC doesn't have |
478 | * per-CPU interfaces so mark our non-existent CPU interface | |
ee3f0956 PM |
479 | * as enabled by default, and with a priority mask which allows |
480 | * all interrupts through. | |
b3387ede | 481 | */ |
32951860 | 482 | s->gic.cpu_ctlr[0] = GICC_CTLR_EN_GRP0; |
ee3f0956 | 483 | s->gic.priority_mask[0] = 0x100; |
b3387ede | 484 | /* The NVIC as a whole is always enabled. */ |
679aa175 | 485 | s->gic.ctlr = 1; |
aecff692 PM |
486 | systick_reset(s); |
487 | } | |
488 | ||
53111180 | 489 | static void armv7m_nvic_realize(DeviceState *dev, Error **errp) |
9ee6e8bb | 490 | { |
1e8cae4d PM |
491 | nvic_state *s = NVIC(dev); |
492 | NVICClass *nc = NVIC_GET_CLASS(s); | |
0175ba10 | 493 | Error *local_err = NULL; |
9ee6e8bb | 494 | |
d713ea6c MD |
495 | s->cpu = ARM_CPU(qemu_get_cpu(0)); |
496 | assert(s->cpu); | |
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 |
537 | static 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 |
556 | static 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 | 568 | static 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 | 577 | static void armv7m_nvic_register_types(void) |
fe7e8758 | 578 | { |
39bffca2 | 579 | type_register_static(&armv7m_nvic_info); |
fe7e8758 PB |
580 | } |
581 | ||
83f7d43a | 582 | type_init(armv7m_nvic_register_types) |