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