]> git.proxmox.com Git - mirror_qemu.git/blame - hw/intc/arm_gic.c
Merge remote-tracking branch 'remotes/kraxel/tags/pull-smbios-2' into staging
[mirror_qemu.git] / hw / intc / arm_gic.c
CommitLineData
5fafdf24 1/*
9ee6e8bb 2 * ARM Generic/Distributed Interrupt Controller
e69954b9 3 *
9ee6e8bb 4 * Copyright (c) 2006-2007 CodeSourcery.
e69954b9
PB
5 * Written by Paul Brook
6 *
8e31bf38 7 * This code is licensed under the GPL.
e69954b9
PB
8 */
9
9ee6e8bb 10/* This file contains implementation code for the RealView EB interrupt
0d256bdc
PM
11 * controller, MPCore distributed interrupt controller and ARMv7-M
12 * Nested Vectored Interrupt Controller.
13 * It is compiled in two ways:
14 * (1) as a standalone file to produce a sysbus device which is a GIC
15 * that can be used on the realview board and as one of the builtin
16 * private peripherals for the ARM MP CPUs (11MPCore, A9, etc)
17 * (2) by being directly #included into armv7m_nvic.c to produce the
18 * armv7m_nvic device.
19 */
e69954b9 20
83c9f4ca 21#include "hw/sysbus.h"
47b43a1f 22#include "gic_internal.h"
dfc08079 23#include "qom/cpu.h"
386e2955 24
e69954b9
PB
25//#define DEBUG_GIC
26
27#ifdef DEBUG_GIC
001faf32 28#define DPRINTF(fmt, ...) \
5eb98401 29do { fprintf(stderr, "arm_gic: " fmt , ## __VA_ARGS__); } while (0)
e69954b9 30#else
001faf32 31#define DPRINTF(fmt, ...) do {} while(0)
e69954b9
PB
32#endif
33
2a29ddee
PM
34static const uint8_t gic_id[] = {
35 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
36};
37
c988bfad 38#define NUM_CPU(s) ((s)->num_cpu)
9ee6e8bb 39
fae15286 40static inline int gic_get_current_cpu(GICState *s)
926c4aff 41{
926c4aff 42 if (s->num_cpu > 1) {
4917cf44 43 return current_cpu->cpu_index;
926c4aff 44 }
926c4aff
PM
45 return 0;
46}
47
e69954b9
PB
48/* TODO: Many places that call this routine could be optimized. */
49/* Update interrupt status after enabled or pending bits have been changed. */
fae15286 50void gic_update(GICState *s)
e69954b9
PB
51{
52 int best_irq;
53 int best_prio;
54 int irq;
9ee6e8bb
PB
55 int level;
56 int cpu;
57 int cm;
58
c988bfad 59 for (cpu = 0; cpu < NUM_CPU(s); cpu++) {
9ee6e8bb
PB
60 cm = 1 << cpu;
61 s->current_pending[cpu] = 1023;
62 if (!s->enabled || !s->cpu_enabled[cpu]) {
c79981ce 63 qemu_irq_lower(s->parent_irq[cpu]);
9ee6e8bb
PB
64 return;
65 }
66 best_prio = 0x100;
67 best_irq = 1023;
a32134aa 68 for (irq = 0; irq < s->num_irq; irq++) {
8d999995 69 if (GIC_TEST_ENABLED(irq, cm) && gic_test_pending(s, irq, cm)) {
9ee6e8bb
PB
70 if (GIC_GET_PRIORITY(irq, cpu) < best_prio) {
71 best_prio = GIC_GET_PRIORITY(irq, cpu);
72 best_irq = irq;
73 }
e69954b9
PB
74 }
75 }
9ee6e8bb 76 level = 0;
cad065f1 77 if (best_prio < s->priority_mask[cpu]) {
9ee6e8bb
PB
78 s->current_pending[cpu] = best_irq;
79 if (best_prio < s->running_priority[cpu]) {
8c815fb3 80 DPRINTF("Raised pending IRQ %d (cpu %d)\n", best_irq, cpu);
9ee6e8bb
PB
81 level = 1;
82 }
e69954b9 83 }
9ee6e8bb 84 qemu_set_irq(s->parent_irq[cpu], level);
e69954b9
PB
85 }
86}
87
fae15286 88void gic_set_pending_private(GICState *s, int cpu, int irq)
9ee6e8bb
PB
89{
90 int cm = 1 << cpu;
91
8d999995 92 if (gic_test_pending(s, irq, cm)) {
9ee6e8bb 93 return;
8d999995 94 }
9ee6e8bb
PB
95
96 DPRINTF("Set %d pending cpu %d\n", irq, cpu);
97 GIC_SET_PENDING(irq, cm);
98 gic_update(s);
99}
100
8d999995
CD
101static void gic_set_irq_11mpcore(GICState *s, int irq, int level,
102 int cm, int target)
103{
104 if (level) {
105 GIC_SET_LEVEL(irq, cm);
106 if (GIC_TEST_EDGE_TRIGGER(irq) || GIC_TEST_ENABLED(irq, cm)) {
107 DPRINTF("Set %d pending mask %x\n", irq, target);
108 GIC_SET_PENDING(irq, target);
109 }
110 } else {
111 GIC_CLEAR_LEVEL(irq, cm);
112 }
113}
114
115static void gic_set_irq_generic(GICState *s, int irq, int level,
116 int cm, int target)
117{
118 if (level) {
119 GIC_SET_LEVEL(irq, cm);
120 DPRINTF("Set %d pending mask %x\n", irq, target);
121 if (GIC_TEST_EDGE_TRIGGER(irq)) {
122 GIC_SET_PENDING(irq, target);
123 }
124 } else {
125 GIC_CLEAR_LEVEL(irq, cm);
126 }
127}
128
9ee6e8bb 129/* Process a change in an external IRQ input. */
e69954b9
PB
130static void gic_set_irq(void *opaque, int irq, int level)
131{
544d1afa
PM
132 /* Meaning of the 'irq' parameter:
133 * [0..N-1] : external interrupts
134 * [N..N+31] : PPI (internal) interrupts for CPU 0
135 * [N+32..N+63] : PPI (internal interrupts for CPU 1
136 * ...
137 */
fae15286 138 GICState *s = (GICState *)opaque;
544d1afa
PM
139 int cm, target;
140 if (irq < (s->num_irq - GIC_INTERNAL)) {
141 /* The first external input line is internal interrupt 32. */
142 cm = ALL_CPU_MASK;
143 irq += GIC_INTERNAL;
144 target = GIC_TARGET(irq);
145 } else {
146 int cpu;
147 irq -= (s->num_irq - GIC_INTERNAL);
148 cpu = irq / GIC_INTERNAL;
149 irq %= GIC_INTERNAL;
150 cm = 1 << cpu;
151 target = cm;
152 }
153
40d22500
CD
154 assert(irq >= GIC_NR_SGIS);
155
544d1afa 156 if (level == GIC_TEST_LEVEL(irq, cm)) {
e69954b9 157 return;
544d1afa 158 }
e69954b9 159
8d999995
CD
160 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
161 gic_set_irq_11mpcore(s, irq, level, cm, target);
e69954b9 162 } else {
8d999995 163 gic_set_irq_generic(s, irq, level, cm, target);
e69954b9 164 }
8d999995 165
e69954b9
PB
166 gic_update(s);
167}
168
fae15286 169static void gic_set_running_irq(GICState *s, int cpu, int irq)
e69954b9 170{
9ee6e8bb
PB
171 s->running_irq[cpu] = irq;
172 if (irq == 1023) {
173 s->running_priority[cpu] = 0x100;
174 } else {
175 s->running_priority[cpu] = GIC_GET_PRIORITY(irq, cpu);
176 }
e69954b9
PB
177 gic_update(s);
178}
179
fae15286 180uint32_t gic_acknowledge_irq(GICState *s, int cpu)
e69954b9 181{
40d22500 182 int ret, irq, src;
9ee6e8bb 183 int cm = 1 << cpu;
40d22500
CD
184 irq = s->current_pending[cpu];
185 if (irq == 1023
186 || GIC_GET_PRIORITY(irq, cpu) >= s->running_priority[cpu]) {
e69954b9
PB
187 DPRINTF("ACK no pending IRQ\n");
188 return 1023;
189 }
40d22500
CD
190 s->last_active[irq][cpu] = s->running_irq[cpu];
191
87316902 192 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
40d22500
CD
193 /* Clear pending flags for both level and edge triggered interrupts.
194 * Level triggered IRQs will be reasserted once they become inactive.
195 */
196 GIC_CLEAR_PENDING(irq, GIC_TEST_MODEL(irq) ? ALL_CPU_MASK : cm);
197 ret = irq;
198 } else {
199 if (irq < GIC_NR_SGIS) {
200 /* Lookup the source CPU for the SGI and clear this in the
201 * sgi_pending map. Return the src and clear the overall pending
202 * state on this CPU if the SGI is not pending from any CPUs.
203 */
204 assert(s->sgi_pending[irq][cpu] != 0);
205 src = ctz32(s->sgi_pending[irq][cpu]);
206 s->sgi_pending[irq][cpu] &= ~(1 << src);
207 if (s->sgi_pending[irq][cpu] == 0) {
208 GIC_CLEAR_PENDING(irq, GIC_TEST_MODEL(irq) ? ALL_CPU_MASK : cm);
209 }
210 ret = irq | ((src & 0x7) << 10);
211 } else {
212 /* Clear pending state for both level and edge triggered
213 * interrupts. (level triggered interrupts with an active line
214 * remain pending, see gic_test_pending)
215 */
216 GIC_CLEAR_PENDING(irq, GIC_TEST_MODEL(irq) ? ALL_CPU_MASK : cm);
217 ret = irq;
218 }
219 }
220
221 gic_set_running_irq(s, cpu, irq);
222 DPRINTF("ACK %d\n", irq);
223 return ret;
e69954b9
PB
224}
225
9df90ad0
CD
226void gic_set_priority(GICState *s, int cpu, int irq, uint8_t val)
227{
228 if (irq < GIC_INTERNAL) {
229 s->priority1[irq][cpu] = val;
230 } else {
231 s->priority2[(irq) - GIC_INTERNAL] = val;
232 }
233}
234
fae15286 235void gic_complete_irq(GICState *s, int cpu, int irq)
e69954b9
PB
236{
237 int update = 0;
9ee6e8bb 238 int cm = 1 << cpu;
df628ff1 239 DPRINTF("EOI %d\n", irq);
a32134aa 240 if (irq >= s->num_irq) {
217bfb44
PM
241 /* This handles two cases:
242 * 1. If software writes the ID of a spurious interrupt [ie 1023]
243 * to the GICC_EOIR, the GIC ignores that write.
244 * 2. If software writes the number of a non-existent interrupt
245 * this must be a subcase of "value written does not match the last
246 * valid interrupt value read from the Interrupt Acknowledge
247 * register" and so this is UNPREDICTABLE. We choose to ignore it.
248 */
249 return;
250 }
9ee6e8bb 251 if (s->running_irq[cpu] == 1023)
e69954b9 252 return; /* No active IRQ. */
8d999995
CD
253
254 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
255 /* Mark level triggered interrupts as pending if they are still
256 raised. */
257 if (!GIC_TEST_EDGE_TRIGGER(irq) && GIC_TEST_ENABLED(irq, cm)
258 && GIC_TEST_LEVEL(irq, cm) && (GIC_TARGET(irq) & cm) != 0) {
259 DPRINTF("Set %d pending mask %x\n", irq, cm);
260 GIC_SET_PENDING(irq, cm);
261 update = 1;
262 }
e69954b9 263 }
8d999995 264
9ee6e8bb 265 if (irq != s->running_irq[cpu]) {
e69954b9 266 /* Complete an IRQ that is not currently running. */
9ee6e8bb
PB
267 int tmp = s->running_irq[cpu];
268 while (s->last_active[tmp][cpu] != 1023) {
269 if (s->last_active[tmp][cpu] == irq) {
270 s->last_active[tmp][cpu] = s->last_active[irq][cpu];
e69954b9
PB
271 break;
272 }
9ee6e8bb 273 tmp = s->last_active[tmp][cpu];
e69954b9
PB
274 }
275 if (update) {
276 gic_update(s);
277 }
278 } else {
279 /* Complete the current running IRQ. */
9ee6e8bb 280 gic_set_running_irq(s, cpu, s->last_active[s->running_irq[cpu]][cpu]);
e69954b9
PB
281 }
282}
283
a8170e5e 284static uint32_t gic_dist_readb(void *opaque, hwaddr offset)
e69954b9 285{
fae15286 286 GICState *s = (GICState *)opaque;
e69954b9
PB
287 uint32_t res;
288 int irq;
289 int i;
9ee6e8bb
PB
290 int cpu;
291 int cm;
292 int mask;
e69954b9 293
926c4aff 294 cpu = gic_get_current_cpu(s);
9ee6e8bb 295 cm = 1 << cpu;
e69954b9
PB
296 if (offset < 0x100) {
297 if (offset == 0)
298 return s->enabled;
299 if (offset == 4)
a32134aa 300 return ((s->num_irq / 32) - 1) | ((NUM_CPU(s) - 1) << 5);
e69954b9
PB
301 if (offset < 0x08)
302 return 0;
b79f2265
RH
303 if (offset >= 0x80) {
304 /* Interrupt Security , RAZ/WI */
305 return 0;
306 }
e69954b9
PB
307 goto bad_reg;
308 } else if (offset < 0x200) {
309 /* Interrupt Set/Clear Enable. */
310 if (offset < 0x180)
311 irq = (offset - 0x100) * 8;
312 else
313 irq = (offset - 0x180) * 8;
9ee6e8bb 314 irq += GIC_BASE_IRQ;
a32134aa 315 if (irq >= s->num_irq)
e69954b9
PB
316 goto bad_reg;
317 res = 0;
318 for (i = 0; i < 8; i++) {
41bf234d 319 if (GIC_TEST_ENABLED(irq + i, cm)) {
e69954b9
PB
320 res |= (1 << i);
321 }
322 }
323 } else if (offset < 0x300) {
324 /* Interrupt Set/Clear Pending. */
325 if (offset < 0x280)
326 irq = (offset - 0x200) * 8;
327 else
328 irq = (offset - 0x280) * 8;
9ee6e8bb 329 irq += GIC_BASE_IRQ;
a32134aa 330 if (irq >= s->num_irq)
e69954b9
PB
331 goto bad_reg;
332 res = 0;
69253800 333 mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
e69954b9 334 for (i = 0; i < 8; i++) {
8d999995 335 if (gic_test_pending(s, irq + i, mask)) {
e69954b9
PB
336 res |= (1 << i);
337 }
338 }
339 } else if (offset < 0x400) {
340 /* Interrupt Active. */
9ee6e8bb 341 irq = (offset - 0x300) * 8 + GIC_BASE_IRQ;
a32134aa 342 if (irq >= s->num_irq)
e69954b9
PB
343 goto bad_reg;
344 res = 0;
69253800 345 mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
e69954b9 346 for (i = 0; i < 8; i++) {
9ee6e8bb 347 if (GIC_TEST_ACTIVE(irq + i, mask)) {
e69954b9
PB
348 res |= (1 << i);
349 }
350 }
351 } else if (offset < 0x800) {
352 /* Interrupt Priority. */
9ee6e8bb 353 irq = (offset - 0x400) + GIC_BASE_IRQ;
a32134aa 354 if (irq >= s->num_irq)
e69954b9 355 goto bad_reg;
9ee6e8bb 356 res = GIC_GET_PRIORITY(irq, cpu);
e69954b9
PB
357 } else if (offset < 0xc00) {
358 /* Interrupt CPU Target. */
6b9680bb
PM
359 if (s->num_cpu == 1 && s->revision != REV_11MPCORE) {
360 /* For uniprocessor GICs these RAZ/WI */
361 res = 0;
9ee6e8bb 362 } else {
6b9680bb
PM
363 irq = (offset - 0x800) + GIC_BASE_IRQ;
364 if (irq >= s->num_irq) {
365 goto bad_reg;
366 }
367 if (irq >= 29 && irq <= 31) {
368 res = cm;
369 } else {
370 res = GIC_TARGET(irq);
371 }
9ee6e8bb 372 }
e69954b9
PB
373 } else if (offset < 0xf00) {
374 /* Interrupt Configuration. */
9ee6e8bb 375 irq = (offset - 0xc00) * 2 + GIC_BASE_IRQ;
a32134aa 376 if (irq >= s->num_irq)
e69954b9
PB
377 goto bad_reg;
378 res = 0;
379 for (i = 0; i < 4; i++) {
380 if (GIC_TEST_MODEL(irq + i))
381 res |= (1 << (i * 2));
04050c5c 382 if (GIC_TEST_EDGE_TRIGGER(irq + i))
e69954b9
PB
383 res |= (2 << (i * 2));
384 }
40d22500
CD
385 } else if (offset < 0xf10) {
386 goto bad_reg;
387 } else if (offset < 0xf30) {
388 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
389 goto bad_reg;
390 }
391
392 if (offset < 0xf20) {
393 /* GICD_CPENDSGIRn */
394 irq = (offset - 0xf10);
395 } else {
396 irq = (offset - 0xf20);
397 /* GICD_SPENDSGIRn */
398 }
399
400 res = s->sgi_pending[irq][cpu];
e69954b9
PB
401 } else if (offset < 0xfe0) {
402 goto bad_reg;
403 } else /* offset >= 0xfe0 */ {
404 if (offset & 3) {
405 res = 0;
406 } else {
407 res = gic_id[(offset - 0xfe0) >> 2];
408 }
409 }
410 return res;
411bad_reg:
8c8dc39f
PM
412 qemu_log_mask(LOG_GUEST_ERROR,
413 "gic_dist_readb: Bad offset %x\n", (int)offset);
e69954b9
PB
414 return 0;
415}
416
a8170e5e 417static uint32_t gic_dist_readw(void *opaque, hwaddr offset)
e69954b9
PB
418{
419 uint32_t val;
420 val = gic_dist_readb(opaque, offset);
421 val |= gic_dist_readb(opaque, offset + 1) << 8;
422 return val;
423}
424
a8170e5e 425static uint32_t gic_dist_readl(void *opaque, hwaddr offset)
e69954b9
PB
426{
427 uint32_t val;
428 val = gic_dist_readw(opaque, offset);
429 val |= gic_dist_readw(opaque, offset + 2) << 16;
430 return val;
431}
432
a8170e5e 433static void gic_dist_writeb(void *opaque, hwaddr offset,
e69954b9
PB
434 uint32_t value)
435{
fae15286 436 GICState *s = (GICState *)opaque;
e69954b9
PB
437 int irq;
438 int i;
9ee6e8bb 439 int cpu;
e69954b9 440
926c4aff 441 cpu = gic_get_current_cpu(s);
e69954b9
PB
442 if (offset < 0x100) {
443 if (offset == 0) {
444 s->enabled = (value & 1);
445 DPRINTF("Distribution %sabled\n", s->enabled ? "En" : "Dis");
446 } else if (offset < 4) {
447 /* ignored. */
b79f2265
RH
448 } else if (offset >= 0x80) {
449 /* Interrupt Security Registers, RAZ/WI */
e69954b9
PB
450 } else {
451 goto bad_reg;
452 }
453 } else if (offset < 0x180) {
454 /* Interrupt Set Enable. */
9ee6e8bb 455 irq = (offset - 0x100) * 8 + GIC_BASE_IRQ;
a32134aa 456 if (irq >= s->num_irq)
e69954b9 457 goto bad_reg;
41ab7b55
CD
458 if (irq < GIC_NR_SGIS) {
459 value = 0xff;
460 }
461
e69954b9
PB
462 for (i = 0; i < 8; i++) {
463 if (value & (1 << i)) {
f47b48fb
DS
464 int mask =
465 (irq < GIC_INTERNAL) ? (1 << cpu) : GIC_TARGET(irq + i);
69253800 466 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
41bf234d
RV
467
468 if (!GIC_TEST_ENABLED(irq + i, cm)) {
e69954b9 469 DPRINTF("Enabled IRQ %d\n", irq + i);
41bf234d
RV
470 }
471 GIC_SET_ENABLED(irq + i, cm);
e69954b9
PB
472 /* If a raised level triggered IRQ enabled then mark
473 is as pending. */
9ee6e8bb 474 if (GIC_TEST_LEVEL(irq + i, mask)
04050c5c 475 && !GIC_TEST_EDGE_TRIGGER(irq + i)) {
9ee6e8bb
PB
476 DPRINTF("Set %d pending mask %x\n", irq + i, mask);
477 GIC_SET_PENDING(irq + i, mask);
478 }
e69954b9
PB
479 }
480 }
481 } else if (offset < 0x200) {
482 /* Interrupt Clear Enable. */
9ee6e8bb 483 irq = (offset - 0x180) * 8 + GIC_BASE_IRQ;
a32134aa 484 if (irq >= s->num_irq)
e69954b9 485 goto bad_reg;
41ab7b55
CD
486 if (irq < GIC_NR_SGIS) {
487 value = 0;
488 }
489
e69954b9
PB
490 for (i = 0; i < 8; i++) {
491 if (value & (1 << i)) {
69253800 492 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
41bf234d
RV
493
494 if (GIC_TEST_ENABLED(irq + i, cm)) {
e69954b9 495 DPRINTF("Disabled IRQ %d\n", irq + i);
41bf234d
RV
496 }
497 GIC_CLEAR_ENABLED(irq + i, cm);
e69954b9
PB
498 }
499 }
500 } else if (offset < 0x280) {
501 /* Interrupt Set Pending. */
9ee6e8bb 502 irq = (offset - 0x200) * 8 + GIC_BASE_IRQ;
a32134aa 503 if (irq >= s->num_irq)
e69954b9 504 goto bad_reg;
41ab7b55 505 if (irq < GIC_NR_SGIS) {
5b0adce1 506 value = 0;
41ab7b55 507 }
9ee6e8bb 508
e69954b9
PB
509 for (i = 0; i < 8; i++) {
510 if (value & (1 << i)) {
f47b48fb 511 GIC_SET_PENDING(irq + i, GIC_TARGET(irq + i));
e69954b9
PB
512 }
513 }
514 } else if (offset < 0x300) {
515 /* Interrupt Clear Pending. */
9ee6e8bb 516 irq = (offset - 0x280) * 8 + GIC_BASE_IRQ;
a32134aa 517 if (irq >= s->num_irq)
e69954b9 518 goto bad_reg;
5b0adce1
CD
519 if (irq < GIC_NR_SGIS) {
520 value = 0;
521 }
522
e69954b9 523 for (i = 0; i < 8; i++) {
9ee6e8bb
PB
524 /* ??? This currently clears the pending bit for all CPUs, even
525 for per-CPU interrupts. It's unclear whether this is the
526 corect behavior. */
e69954b9 527 if (value & (1 << i)) {
9ee6e8bb 528 GIC_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
e69954b9
PB
529 }
530 }
531 } else if (offset < 0x400) {
532 /* Interrupt Active. */
533 goto bad_reg;
534 } else if (offset < 0x800) {
535 /* Interrupt Priority. */
9ee6e8bb 536 irq = (offset - 0x400) + GIC_BASE_IRQ;
a32134aa 537 if (irq >= s->num_irq)
e69954b9 538 goto bad_reg;
9df90ad0 539 gic_set_priority(s, cpu, irq, value);
e69954b9 540 } else if (offset < 0xc00) {
6b9680bb
PM
541 /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
542 * annoying exception of the 11MPCore's GIC.
543 */
544 if (s->num_cpu != 1 || s->revision == REV_11MPCORE) {
545 irq = (offset - 0x800) + GIC_BASE_IRQ;
546 if (irq >= s->num_irq) {
547 goto bad_reg;
548 }
549 if (irq < 29) {
550 value = 0;
551 } else if (irq < GIC_INTERNAL) {
552 value = ALL_CPU_MASK;
553 }
554 s->irq_target[irq] = value & ALL_CPU_MASK;
555 }
e69954b9
PB
556 } else if (offset < 0xf00) {
557 /* Interrupt Configuration. */
9ee6e8bb 558 irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ;
a32134aa 559 if (irq >= s->num_irq)
e69954b9 560 goto bad_reg;
69253800 561 if (irq < GIC_INTERNAL)
9ee6e8bb 562 value |= 0xaa;
e69954b9
PB
563 for (i = 0; i < 4; i++) {
564 if (value & (1 << (i * 2))) {
565 GIC_SET_MODEL(irq + i);
566 } else {
567 GIC_CLEAR_MODEL(irq + i);
568 }
569 if (value & (2 << (i * 2))) {
04050c5c 570 GIC_SET_EDGE_TRIGGER(irq + i);
e69954b9 571 } else {
04050c5c 572 GIC_CLEAR_EDGE_TRIGGER(irq + i);
e69954b9
PB
573 }
574 }
40d22500 575 } else if (offset < 0xf10) {
9ee6e8bb 576 /* 0xf00 is only handled for 32-bit writes. */
e69954b9 577 goto bad_reg;
40d22500
CD
578 } else if (offset < 0xf20) {
579 /* GICD_CPENDSGIRn */
580 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
581 goto bad_reg;
582 }
583 irq = (offset - 0xf10);
584
585 s->sgi_pending[irq][cpu] &= ~value;
586 if (s->sgi_pending[irq][cpu] == 0) {
587 GIC_CLEAR_PENDING(irq, 1 << cpu);
588 }
589 } else if (offset < 0xf30) {
590 /* GICD_SPENDSGIRn */
591 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
592 goto bad_reg;
593 }
594 irq = (offset - 0xf20);
595
596 GIC_SET_PENDING(irq, 1 << cpu);
597 s->sgi_pending[irq][cpu] |= value;
598 } else {
599 goto bad_reg;
e69954b9
PB
600 }
601 gic_update(s);
602 return;
603bad_reg:
8c8dc39f
PM
604 qemu_log_mask(LOG_GUEST_ERROR,
605 "gic_dist_writeb: Bad offset %x\n", (int)offset);
e69954b9
PB
606}
607
a8170e5e 608static void gic_dist_writew(void *opaque, hwaddr offset,
e69954b9
PB
609 uint32_t value)
610{
e69954b9
PB
611 gic_dist_writeb(opaque, offset, value & 0xff);
612 gic_dist_writeb(opaque, offset + 1, value >> 8);
613}
614
a8170e5e 615static void gic_dist_writel(void *opaque, hwaddr offset,
e69954b9
PB
616 uint32_t value)
617{
fae15286 618 GICState *s = (GICState *)opaque;
8da3ff18 619 if (offset == 0xf00) {
9ee6e8bb
PB
620 int cpu;
621 int irq;
622 int mask;
40d22500 623 int target_cpu;
9ee6e8bb 624
926c4aff 625 cpu = gic_get_current_cpu(s);
9ee6e8bb
PB
626 irq = value & 0x3ff;
627 switch ((value >> 24) & 3) {
628 case 0:
629 mask = (value >> 16) & ALL_CPU_MASK;
630 break;
631 case 1:
fa250144 632 mask = ALL_CPU_MASK ^ (1 << cpu);
9ee6e8bb
PB
633 break;
634 case 2:
fa250144 635 mask = 1 << cpu;
9ee6e8bb
PB
636 break;
637 default:
638 DPRINTF("Bad Soft Int target filter\n");
639 mask = ALL_CPU_MASK;
640 break;
641 }
642 GIC_SET_PENDING(irq, mask);
40d22500
CD
643 target_cpu = ctz32(mask);
644 while (target_cpu < GIC_NCPU) {
645 s->sgi_pending[irq][target_cpu] |= (1 << cpu);
646 mask &= ~(1 << target_cpu);
647 target_cpu = ctz32(mask);
648 }
9ee6e8bb
PB
649 gic_update(s);
650 return;
651 }
e69954b9
PB
652 gic_dist_writew(opaque, offset, value & 0xffff);
653 gic_dist_writew(opaque, offset + 2, value >> 16);
654}
655
755c0802
AK
656static const MemoryRegionOps gic_dist_ops = {
657 .old_mmio = {
658 .read = { gic_dist_readb, gic_dist_readw, gic_dist_readl, },
659 .write = { gic_dist_writeb, gic_dist_writew, gic_dist_writel, },
660 },
661 .endianness = DEVICE_NATIVE_ENDIAN,
e69954b9
PB
662};
663
fae15286 664static uint32_t gic_cpu_read(GICState *s, int cpu, int offset)
e69954b9 665{
e69954b9
PB
666 switch (offset) {
667 case 0x00: /* Control */
9ee6e8bb 668 return s->cpu_enabled[cpu];
e69954b9 669 case 0x04: /* Priority mask */
9ee6e8bb 670 return s->priority_mask[cpu];
e69954b9 671 case 0x08: /* Binary Point */
aa7d461a 672 return s->bpr[cpu];
e69954b9 673 case 0x0c: /* Acknowledge */
9ee6e8bb 674 return gic_acknowledge_irq(s, cpu);
66a0a2cb 675 case 0x14: /* Running Priority */
9ee6e8bb 676 return s->running_priority[cpu];
e69954b9 677 case 0x18: /* Highest Pending Interrupt */
9ee6e8bb 678 return s->current_pending[cpu];
aa7d461a
CD
679 case 0x1c: /* Aliased Binary Point */
680 return s->abpr[cpu];
a9d477c4
CD
681 case 0xd0: case 0xd4: case 0xd8: case 0xdc:
682 return s->apr[(offset - 0xd0) / 4][cpu];
e69954b9 683 default:
8c8dc39f
PM
684 qemu_log_mask(LOG_GUEST_ERROR,
685 "gic_cpu_read: Bad offset %x\n", (int)offset);
e69954b9
PB
686 return 0;
687 }
688}
689
fae15286 690static void gic_cpu_write(GICState *s, int cpu, int offset, uint32_t value)
e69954b9 691{
e69954b9
PB
692 switch (offset) {
693 case 0x00: /* Control */
9ee6e8bb 694 s->cpu_enabled[cpu] = (value & 1);
9ab1b605 695 DPRINTF("CPU %d %sabled\n", cpu, s->cpu_enabled[cpu] ? "En" : "Dis");
e69954b9
PB
696 break;
697 case 0x04: /* Priority mask */
9ee6e8bb 698 s->priority_mask[cpu] = (value & 0xff);
e69954b9
PB
699 break;
700 case 0x08: /* Binary Point */
aa7d461a 701 s->bpr[cpu] = (value & 0x7);
e69954b9
PB
702 break;
703 case 0x10: /* End Of Interrupt */
9ee6e8bb 704 return gic_complete_irq(s, cpu, value & 0x3ff);
aa7d461a
CD
705 case 0x1c: /* Aliased Binary Point */
706 if (s->revision >= 2) {
707 s->abpr[cpu] = (value & 0x7);
708 }
709 break;
a9d477c4
CD
710 case 0xd0: case 0xd4: case 0xd8: case 0xdc:
711 qemu_log_mask(LOG_UNIMP, "Writing APR not implemented\n");
712 break;
e69954b9 713 default:
8c8dc39f
PM
714 qemu_log_mask(LOG_GUEST_ERROR,
715 "gic_cpu_write: Bad offset %x\n", (int)offset);
e69954b9
PB
716 return;
717 }
718 gic_update(s);
719}
e2c56465
PM
720
721/* Wrappers to read/write the GIC CPU interface for the current CPU */
a8170e5e 722static uint64_t gic_thiscpu_read(void *opaque, hwaddr addr,
e2c56465
PM
723 unsigned size)
724{
fae15286 725 GICState *s = (GICState *)opaque;
926c4aff 726 return gic_cpu_read(s, gic_get_current_cpu(s), addr);
e2c56465
PM
727}
728
a8170e5e 729static void gic_thiscpu_write(void *opaque, hwaddr addr,
e2c56465
PM
730 uint64_t value, unsigned size)
731{
fae15286 732 GICState *s = (GICState *)opaque;
926c4aff 733 gic_cpu_write(s, gic_get_current_cpu(s), addr, value);
e2c56465
PM
734}
735
736/* Wrappers to read/write the GIC CPU interface for a specific CPU.
fae15286 737 * These just decode the opaque pointer into GICState* + cpu id.
e2c56465 738 */
a8170e5e 739static uint64_t gic_do_cpu_read(void *opaque, hwaddr addr,
e2c56465
PM
740 unsigned size)
741{
fae15286
PM
742 GICState **backref = (GICState **)opaque;
743 GICState *s = *backref;
e2c56465 744 int id = (backref - s->backref);
0e4a398a 745 return gic_cpu_read(s, id, addr);
e2c56465
PM
746}
747
a8170e5e 748static void gic_do_cpu_write(void *opaque, hwaddr addr,
e2c56465
PM
749 uint64_t value, unsigned size)
750{
fae15286
PM
751 GICState **backref = (GICState **)opaque;
752 GICState *s = *backref;
e2c56465 753 int id = (backref - s->backref);
0e4a398a 754 gic_cpu_write(s, id, addr, value);
e2c56465
PM
755}
756
757static const MemoryRegionOps gic_thiscpu_ops = {
758 .read = gic_thiscpu_read,
759 .write = gic_thiscpu_write,
760 .endianness = DEVICE_NATIVE_ENDIAN,
761};
762
763static const MemoryRegionOps gic_cpu_ops = {
764 .read = gic_do_cpu_read,
765 .write = gic_do_cpu_write,
766 .endianness = DEVICE_NATIVE_ENDIAN,
767};
e69954b9 768
fae15286 769void gic_init_irqs_and_distributor(GICState *s, int num_irq)
e69954b9 770{
285b4432 771 SysBusDevice *sbd = SYS_BUS_DEVICE(s);
23e39294 772 int i;
41c1e2f5 773
544d1afa 774 i = s->num_irq - GIC_INTERNAL;
544d1afa
PM
775 /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU.
776 * GPIO array layout is thus:
777 * [0..N-1] SPIs
778 * [N..N+31] PPIs for CPU 0
779 * [N+32..N+63] PPIs for CPU 1
780 * ...
781 */
84e4fccb
PM
782 if (s->revision != REV_NVIC) {
783 i += (GIC_INTERNAL * s->num_cpu);
784 }
285b4432 785 qdev_init_gpio_in(DEVICE(s), gic_set_irq, i);
c988bfad 786 for (i = 0; i < NUM_CPU(s); i++) {
285b4432 787 sysbus_init_irq(sbd, &s->parent_irq[i]);
e69954b9 788 }
1437c94b
PB
789 memory_region_init_io(&s->iomem, OBJECT(s), &gic_dist_ops, s,
790 "gic_dist", 0x1000);
2b518c56
PM
791}
792
53111180 793static void arm_gic_realize(DeviceState *dev, Error **errp)
2b518c56 794{
53111180 795 /* Device instance realize function for the GIC sysbus device */
2b518c56 796 int i;
53111180
PM
797 GICState *s = ARM_GIC(dev);
798 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
1e8cae4d
PM
799 ARMGICClass *agc = ARM_GIC_GET_CLASS(s);
800
53111180
PM
801 agc->parent_realize(dev, errp);
802 if (error_is_set(errp)) {
803 return;
804 }
1e8cae4d 805
2b518c56
PM
806 gic_init_irqs_and_distributor(s, s->num_irq);
807
e2c56465
PM
808 /* Memory regions for the CPU interfaces (NVIC doesn't have these):
809 * a region for "CPU interface for this core", then a region for
810 * "CPU interface for core 0", "for core 1", ...
811 * NB that the memory region size of 0x100 applies for the 11MPCore
812 * and also cores following the GIC v1 spec (ie A9).
813 * GIC v2 defines a larger memory region (0x1000) so this will need
814 * to be extended when we implement A15.
815 */
1437c94b 816 memory_region_init_io(&s->cpuiomem[0], OBJECT(s), &gic_thiscpu_ops, s,
e2c56465
PM
817 "gic_cpu", 0x100);
818 for (i = 0; i < NUM_CPU(s); i++) {
819 s->backref[i] = s;
1437c94b
PB
820 memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops,
821 &s->backref[i], "gic_cpu", 0x100);
e2c56465 822 }
496dbcd1 823 /* Distributor */
53111180 824 sysbus_init_mmio(sbd, &s->iomem);
496dbcd1
PM
825 /* cpu interfaces (one for "current cpu" plus one per cpu) */
826 for (i = 0; i <= NUM_CPU(s); i++) {
53111180 827 sysbus_init_mmio(sbd, &s->cpuiomem[i]);
496dbcd1 828 }
496dbcd1
PM
829}
830
496dbcd1
PM
831static void arm_gic_class_init(ObjectClass *klass, void *data)
832{
833 DeviceClass *dc = DEVICE_CLASS(klass);
1e8cae4d 834 ARMGICClass *agc = ARM_GIC_CLASS(klass);
53111180 835
53111180
PM
836 agc->parent_realize = dc->realize;
837 dc->realize = arm_gic_realize;
496dbcd1
PM
838}
839
8c43a6f0 840static const TypeInfo arm_gic_info = {
1e8cae4d
PM
841 .name = TYPE_ARM_GIC,
842 .parent = TYPE_ARM_GIC_COMMON,
fae15286 843 .instance_size = sizeof(GICState),
496dbcd1 844 .class_init = arm_gic_class_init,
998a74bc 845 .class_size = sizeof(ARMGICClass),
496dbcd1
PM
846};
847
848static void arm_gic_register_types(void)
849{
850 type_register_static(&arm_gic_info);
851}
852
853type_init(arm_gic_register_types)