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