]> git.proxmox.com Git - qemu.git/blame - hw/arm_gic.c
Merge remote-tracking branch 'sstabellini/compile-xs' into staging
[qemu.git] / hw / 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
496dbcd1 21#include "sysbus.h"
1e8cae4d 22#include "arm_gic_internal.h"
386e2955 23
e69954b9
PB
24//#define DEBUG_GIC
25
26#ifdef DEBUG_GIC
001faf32
BS
27#define DPRINTF(fmt, ...) \
28do { printf("arm_gic: " fmt , ## __VA_ARGS__); } while (0)
e69954b9 29#else
001faf32 30#define DPRINTF(fmt, ...) do {} while(0)
e69954b9
PB
31#endif
32
2a29ddee
PM
33static const uint8_t gic_id[] = {
34 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
35};
36
c988bfad 37#define NUM_CPU(s) ((s)->num_cpu)
9ee6e8bb 38
926c4aff
PM
39static inline int gic_get_current_cpu(gic_state *s)
40{
926c4aff
PM
41 if (s->num_cpu > 1) {
42 return cpu_single_env->cpu_index;
43 }
926c4aff
PM
44 return 0;
45}
46
e69954b9
PB
47/* TODO: Many places that call this routine could be optimized. */
48/* Update interrupt status after enabled or pending bits have been changed. */
1e8cae4d 49void gic_update(gic_state *s)
e69954b9
PB
50{
51 int best_irq;
52 int best_prio;
53 int irq;
9ee6e8bb
PB
54 int level;
55 int cpu;
56 int cm;
57
c988bfad 58 for (cpu = 0; cpu < NUM_CPU(s); cpu++) {
9ee6e8bb
PB
59 cm = 1 << cpu;
60 s->current_pending[cpu] = 1023;
61 if (!s->enabled || !s->cpu_enabled[cpu]) {
c79981ce 62 qemu_irq_lower(s->parent_irq[cpu]);
9ee6e8bb
PB
63 return;
64 }
65 best_prio = 0x100;
66 best_irq = 1023;
a32134aa 67 for (irq = 0; irq < s->num_irq; irq++) {
41bf234d 68 if (GIC_TEST_ENABLED(irq, cm) && GIC_TEST_PENDING(irq, cm)) {
9ee6e8bb
PB
69 if (GIC_GET_PRIORITY(irq, cpu) < best_prio) {
70 best_prio = GIC_GET_PRIORITY(irq, cpu);
71 best_irq = irq;
72 }
e69954b9
PB
73 }
74 }
9ee6e8bb
PB
75 level = 0;
76 if (best_prio <= s->priority_mask[cpu]) {
77 s->current_pending[cpu] = best_irq;
78 if (best_prio < s->running_priority[cpu]) {
79 DPRINTF("Raised pending IRQ %d\n", best_irq);
80 level = 1;
81 }
e69954b9 82 }
9ee6e8bb 83 qemu_set_irq(s->parent_irq[cpu], level);
e69954b9
PB
84 }
85}
86
1e8cae4d 87void gic_set_pending_private(gic_state *s, int cpu, int irq)
9ee6e8bb
PB
88{
89 int cm = 1 << cpu;
90
91 if (GIC_TEST_PENDING(irq, cm))
92 return;
93
94 DPRINTF("Set %d pending cpu %d\n", irq, cpu);
95 GIC_SET_PENDING(irq, cm);
96 gic_update(s);
97}
98
99/* Process a change in an external IRQ input. */
e69954b9
PB
100static void gic_set_irq(void *opaque, int irq, int level)
101{
544d1afa
PM
102 /* Meaning of the 'irq' parameter:
103 * [0..N-1] : external interrupts
104 * [N..N+31] : PPI (internal) interrupts for CPU 0
105 * [N+32..N+63] : PPI (internal interrupts for CPU 1
106 * ...
107 */
e69954b9 108 gic_state *s = (gic_state *)opaque;
544d1afa
PM
109 int cm, target;
110 if (irq < (s->num_irq - GIC_INTERNAL)) {
111 /* The first external input line is internal interrupt 32. */
112 cm = ALL_CPU_MASK;
113 irq += GIC_INTERNAL;
114 target = GIC_TARGET(irq);
115 } else {
116 int cpu;
117 irq -= (s->num_irq - GIC_INTERNAL);
118 cpu = irq / GIC_INTERNAL;
119 irq %= GIC_INTERNAL;
120 cm = 1 << cpu;
121 target = cm;
122 }
123
124 if (level == GIC_TEST_LEVEL(irq, cm)) {
e69954b9 125 return;
544d1afa 126 }
e69954b9
PB
127
128 if (level) {
544d1afa
PM
129 GIC_SET_LEVEL(irq, cm);
130 if (GIC_TEST_TRIGGER(irq) || GIC_TEST_ENABLED(irq, cm)) {
131 DPRINTF("Set %d pending mask %x\n", irq, target);
132 GIC_SET_PENDING(irq, target);
e69954b9
PB
133 }
134 } else {
544d1afa 135 GIC_CLEAR_LEVEL(irq, cm);
e69954b9
PB
136 }
137 gic_update(s);
138}
139
9ee6e8bb 140static void gic_set_running_irq(gic_state *s, int cpu, int irq)
e69954b9 141{
9ee6e8bb
PB
142 s->running_irq[cpu] = irq;
143 if (irq == 1023) {
144 s->running_priority[cpu] = 0x100;
145 } else {
146 s->running_priority[cpu] = GIC_GET_PRIORITY(irq, cpu);
147 }
e69954b9
PB
148 gic_update(s);
149}
150
1e8cae4d 151uint32_t gic_acknowledge_irq(gic_state *s, int cpu)
e69954b9
PB
152{
153 int new_irq;
9ee6e8bb
PB
154 int cm = 1 << cpu;
155 new_irq = s->current_pending[cpu];
156 if (new_irq == 1023
157 || GIC_GET_PRIORITY(new_irq, cpu) >= s->running_priority[cpu]) {
e69954b9
PB
158 DPRINTF("ACK no pending IRQ\n");
159 return 1023;
160 }
9ee6e8bb
PB
161 s->last_active[new_irq][cpu] = s->running_irq[cpu];
162 /* Clear pending flags for both level and edge triggered interrupts.
163 Level triggered IRQs will be reasserted once they become inactive. */
164 GIC_CLEAR_PENDING(new_irq, GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm);
165 gic_set_running_irq(s, cpu, new_irq);
e69954b9
PB
166 DPRINTF("ACK %d\n", new_irq);
167 return new_irq;
168}
169
1e8cae4d 170void gic_complete_irq(gic_state *s, int cpu, int irq)
e69954b9
PB
171{
172 int update = 0;
9ee6e8bb 173 int cm = 1 << cpu;
df628ff1 174 DPRINTF("EOI %d\n", irq);
a32134aa 175 if (irq >= s->num_irq) {
217bfb44
PM
176 /* This handles two cases:
177 * 1. If software writes the ID of a spurious interrupt [ie 1023]
178 * to the GICC_EOIR, the GIC ignores that write.
179 * 2. If software writes the number of a non-existent interrupt
180 * this must be a subcase of "value written does not match the last
181 * valid interrupt value read from the Interrupt Acknowledge
182 * register" and so this is UNPREDICTABLE. We choose to ignore it.
183 */
184 return;
185 }
9ee6e8bb 186 if (s->running_irq[cpu] == 1023)
e69954b9 187 return; /* No active IRQ. */
217bfb44
PM
188 /* Mark level triggered interrupts as pending if they are still
189 raised. */
190 if (!GIC_TEST_TRIGGER(irq) && GIC_TEST_ENABLED(irq, cm)
191 && GIC_TEST_LEVEL(irq, cm) && (GIC_TARGET(irq) & cm) != 0) {
192 DPRINTF("Set %d pending mask %x\n", irq, cm);
193 GIC_SET_PENDING(irq, cm);
194 update = 1;
e69954b9 195 }
9ee6e8bb 196 if (irq != s->running_irq[cpu]) {
e69954b9 197 /* Complete an IRQ that is not currently running. */
9ee6e8bb
PB
198 int tmp = s->running_irq[cpu];
199 while (s->last_active[tmp][cpu] != 1023) {
200 if (s->last_active[tmp][cpu] == irq) {
201 s->last_active[tmp][cpu] = s->last_active[irq][cpu];
e69954b9
PB
202 break;
203 }
9ee6e8bb 204 tmp = s->last_active[tmp][cpu];
e69954b9
PB
205 }
206 if (update) {
207 gic_update(s);
208 }
209 } else {
210 /* Complete the current running IRQ. */
9ee6e8bb 211 gic_set_running_irq(s, cpu, s->last_active[s->running_irq[cpu]][cpu]);
e69954b9
PB
212 }
213}
214
c227f099 215static uint32_t gic_dist_readb(void *opaque, target_phys_addr_t offset)
e69954b9
PB
216{
217 gic_state *s = (gic_state *)opaque;
218 uint32_t res;
219 int irq;
220 int i;
9ee6e8bb
PB
221 int cpu;
222 int cm;
223 int mask;
e69954b9 224
926c4aff 225 cpu = gic_get_current_cpu(s);
9ee6e8bb 226 cm = 1 << cpu;
e69954b9
PB
227 if (offset < 0x100) {
228 if (offset == 0)
229 return s->enabled;
230 if (offset == 4)
a32134aa 231 return ((s->num_irq / 32) - 1) | ((NUM_CPU(s) - 1) << 5);
e69954b9
PB
232 if (offset < 0x08)
233 return 0;
b79f2265
RH
234 if (offset >= 0x80) {
235 /* Interrupt Security , RAZ/WI */
236 return 0;
237 }
e69954b9
PB
238 goto bad_reg;
239 } else if (offset < 0x200) {
240 /* Interrupt Set/Clear Enable. */
241 if (offset < 0x180)
242 irq = (offset - 0x100) * 8;
243 else
244 irq = (offset - 0x180) * 8;
9ee6e8bb 245 irq += GIC_BASE_IRQ;
a32134aa 246 if (irq >= s->num_irq)
e69954b9
PB
247 goto bad_reg;
248 res = 0;
249 for (i = 0; i < 8; i++) {
41bf234d 250 if (GIC_TEST_ENABLED(irq + i, cm)) {
e69954b9
PB
251 res |= (1 << i);
252 }
253 }
254 } else if (offset < 0x300) {
255 /* Interrupt Set/Clear Pending. */
256 if (offset < 0x280)
257 irq = (offset - 0x200) * 8;
258 else
259 irq = (offset - 0x280) * 8;
9ee6e8bb 260 irq += GIC_BASE_IRQ;
a32134aa 261 if (irq >= s->num_irq)
e69954b9
PB
262 goto bad_reg;
263 res = 0;
69253800 264 mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
e69954b9 265 for (i = 0; i < 8; i++) {
9ee6e8bb 266 if (GIC_TEST_PENDING(irq + i, mask)) {
e69954b9
PB
267 res |= (1 << i);
268 }
269 }
270 } else if (offset < 0x400) {
271 /* Interrupt Active. */
9ee6e8bb 272 irq = (offset - 0x300) * 8 + GIC_BASE_IRQ;
a32134aa 273 if (irq >= s->num_irq)
e69954b9
PB
274 goto bad_reg;
275 res = 0;
69253800 276 mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
e69954b9 277 for (i = 0; i < 8; i++) {
9ee6e8bb 278 if (GIC_TEST_ACTIVE(irq + i, mask)) {
e69954b9
PB
279 res |= (1 << i);
280 }
281 }
282 } else if (offset < 0x800) {
283 /* Interrupt Priority. */
9ee6e8bb 284 irq = (offset - 0x400) + GIC_BASE_IRQ;
a32134aa 285 if (irq >= s->num_irq)
e69954b9 286 goto bad_reg;
9ee6e8bb 287 res = GIC_GET_PRIORITY(irq, cpu);
e69954b9
PB
288 } else if (offset < 0xc00) {
289 /* Interrupt CPU Target. */
6b9680bb
PM
290 if (s->num_cpu == 1 && s->revision != REV_11MPCORE) {
291 /* For uniprocessor GICs these RAZ/WI */
292 res = 0;
9ee6e8bb 293 } else {
6b9680bb
PM
294 irq = (offset - 0x800) + GIC_BASE_IRQ;
295 if (irq >= s->num_irq) {
296 goto bad_reg;
297 }
298 if (irq >= 29 && irq <= 31) {
299 res = cm;
300 } else {
301 res = GIC_TARGET(irq);
302 }
9ee6e8bb 303 }
e69954b9
PB
304 } else if (offset < 0xf00) {
305 /* Interrupt Configuration. */
9ee6e8bb 306 irq = (offset - 0xc00) * 2 + GIC_BASE_IRQ;
a32134aa 307 if (irq >= s->num_irq)
e69954b9
PB
308 goto bad_reg;
309 res = 0;
310 for (i = 0; i < 4; i++) {
311 if (GIC_TEST_MODEL(irq + i))
312 res |= (1 << (i * 2));
313 if (GIC_TEST_TRIGGER(irq + i))
314 res |= (2 << (i * 2));
315 }
316 } else if (offset < 0xfe0) {
317 goto bad_reg;
318 } else /* offset >= 0xfe0 */ {
319 if (offset & 3) {
320 res = 0;
321 } else {
322 res = gic_id[(offset - 0xfe0) >> 2];
323 }
324 }
325 return res;
326bad_reg:
2ac71179 327 hw_error("gic_dist_readb: Bad offset %x\n", (int)offset);
e69954b9
PB
328 return 0;
329}
330
c227f099 331static uint32_t gic_dist_readw(void *opaque, target_phys_addr_t offset)
e69954b9
PB
332{
333 uint32_t val;
334 val = gic_dist_readb(opaque, offset);
335 val |= gic_dist_readb(opaque, offset + 1) << 8;
336 return val;
337}
338
c227f099 339static uint32_t gic_dist_readl(void *opaque, target_phys_addr_t offset)
e69954b9
PB
340{
341 uint32_t val;
342 val = gic_dist_readw(opaque, offset);
343 val |= gic_dist_readw(opaque, offset + 2) << 16;
344 return val;
345}
346
c227f099 347static void gic_dist_writeb(void *opaque, target_phys_addr_t offset,
e69954b9
PB
348 uint32_t value)
349{
350 gic_state *s = (gic_state *)opaque;
351 int irq;
352 int i;
9ee6e8bb 353 int cpu;
e69954b9 354
926c4aff 355 cpu = gic_get_current_cpu(s);
e69954b9
PB
356 if (offset < 0x100) {
357 if (offset == 0) {
358 s->enabled = (value & 1);
359 DPRINTF("Distribution %sabled\n", s->enabled ? "En" : "Dis");
360 } else if (offset < 4) {
361 /* ignored. */
b79f2265
RH
362 } else if (offset >= 0x80) {
363 /* Interrupt Security Registers, RAZ/WI */
e69954b9
PB
364 } else {
365 goto bad_reg;
366 }
367 } else if (offset < 0x180) {
368 /* Interrupt Set Enable. */
9ee6e8bb 369 irq = (offset - 0x100) * 8 + GIC_BASE_IRQ;
a32134aa 370 if (irq >= s->num_irq)
e69954b9 371 goto bad_reg;
9ee6e8bb
PB
372 if (irq < 16)
373 value = 0xff;
e69954b9
PB
374 for (i = 0; i < 8; i++) {
375 if (value & (1 << i)) {
69253800
RR
376 int mask = (irq < GIC_INTERNAL) ? (1 << cpu) : GIC_TARGET(irq);
377 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
41bf234d
RV
378
379 if (!GIC_TEST_ENABLED(irq + i, cm)) {
e69954b9 380 DPRINTF("Enabled IRQ %d\n", irq + i);
41bf234d
RV
381 }
382 GIC_SET_ENABLED(irq + i, cm);
e69954b9
PB
383 /* If a raised level triggered IRQ enabled then mark
384 is as pending. */
9ee6e8bb
PB
385 if (GIC_TEST_LEVEL(irq + i, mask)
386 && !GIC_TEST_TRIGGER(irq + i)) {
387 DPRINTF("Set %d pending mask %x\n", irq + i, mask);
388 GIC_SET_PENDING(irq + i, mask);
389 }
e69954b9
PB
390 }
391 }
392 } else if (offset < 0x200) {
393 /* Interrupt Clear Enable. */
9ee6e8bb 394 irq = (offset - 0x180) * 8 + GIC_BASE_IRQ;
a32134aa 395 if (irq >= s->num_irq)
e69954b9 396 goto bad_reg;
9ee6e8bb
PB
397 if (irq < 16)
398 value = 0;
e69954b9
PB
399 for (i = 0; i < 8; i++) {
400 if (value & (1 << i)) {
69253800 401 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
41bf234d
RV
402
403 if (GIC_TEST_ENABLED(irq + i, cm)) {
e69954b9 404 DPRINTF("Disabled IRQ %d\n", irq + i);
41bf234d
RV
405 }
406 GIC_CLEAR_ENABLED(irq + i, cm);
e69954b9
PB
407 }
408 }
409 } else if (offset < 0x280) {
410 /* Interrupt Set Pending. */
9ee6e8bb 411 irq = (offset - 0x200) * 8 + GIC_BASE_IRQ;
a32134aa 412 if (irq >= s->num_irq)
e69954b9 413 goto bad_reg;
9ee6e8bb
PB
414 if (irq < 16)
415 irq = 0;
416
e69954b9
PB
417 for (i = 0; i < 8; i++) {
418 if (value & (1 << i)) {
9ee6e8bb 419 GIC_SET_PENDING(irq + i, GIC_TARGET(irq));
e69954b9
PB
420 }
421 }
422 } else if (offset < 0x300) {
423 /* Interrupt Clear Pending. */
9ee6e8bb 424 irq = (offset - 0x280) * 8 + GIC_BASE_IRQ;
a32134aa 425 if (irq >= s->num_irq)
e69954b9
PB
426 goto bad_reg;
427 for (i = 0; i < 8; i++) {
9ee6e8bb
PB
428 /* ??? This currently clears the pending bit for all CPUs, even
429 for per-CPU interrupts. It's unclear whether this is the
430 corect behavior. */
e69954b9 431 if (value & (1 << i)) {
9ee6e8bb 432 GIC_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
e69954b9
PB
433 }
434 }
435 } else if (offset < 0x400) {
436 /* Interrupt Active. */
437 goto bad_reg;
438 } else if (offset < 0x800) {
439 /* Interrupt Priority. */
9ee6e8bb 440 irq = (offset - 0x400) + GIC_BASE_IRQ;
a32134aa 441 if (irq >= s->num_irq)
e69954b9 442 goto bad_reg;
69253800 443 if (irq < GIC_INTERNAL) {
9ee6e8bb
PB
444 s->priority1[irq][cpu] = value;
445 } else {
69253800 446 s->priority2[irq - GIC_INTERNAL] = value;
9ee6e8bb 447 }
e69954b9 448 } else if (offset < 0xc00) {
6b9680bb
PM
449 /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
450 * annoying exception of the 11MPCore's GIC.
451 */
452 if (s->num_cpu != 1 || s->revision == REV_11MPCORE) {
453 irq = (offset - 0x800) + GIC_BASE_IRQ;
454 if (irq >= s->num_irq) {
455 goto bad_reg;
456 }
457 if (irq < 29) {
458 value = 0;
459 } else if (irq < GIC_INTERNAL) {
460 value = ALL_CPU_MASK;
461 }
462 s->irq_target[irq] = value & ALL_CPU_MASK;
463 }
e69954b9
PB
464 } else if (offset < 0xf00) {
465 /* Interrupt Configuration. */
9ee6e8bb 466 irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ;
a32134aa 467 if (irq >= s->num_irq)
e69954b9 468 goto bad_reg;
69253800 469 if (irq < GIC_INTERNAL)
9ee6e8bb 470 value |= 0xaa;
e69954b9
PB
471 for (i = 0; i < 4; i++) {
472 if (value & (1 << (i * 2))) {
473 GIC_SET_MODEL(irq + i);
474 } else {
475 GIC_CLEAR_MODEL(irq + i);
476 }
477 if (value & (2 << (i * 2))) {
478 GIC_SET_TRIGGER(irq + i);
479 } else {
480 GIC_CLEAR_TRIGGER(irq + i);
481 }
482 }
483 } else {
9ee6e8bb 484 /* 0xf00 is only handled for 32-bit writes. */
e69954b9
PB
485 goto bad_reg;
486 }
487 gic_update(s);
488 return;
489bad_reg:
2ac71179 490 hw_error("gic_dist_writeb: Bad offset %x\n", (int)offset);
e69954b9
PB
491}
492
c227f099 493static void gic_dist_writew(void *opaque, target_phys_addr_t offset,
e69954b9
PB
494 uint32_t value)
495{
e69954b9
PB
496 gic_dist_writeb(opaque, offset, value & 0xff);
497 gic_dist_writeb(opaque, offset + 1, value >> 8);
498}
499
c227f099 500static void gic_dist_writel(void *opaque, target_phys_addr_t offset,
e69954b9
PB
501 uint32_t value)
502{
9ee6e8bb 503 gic_state *s = (gic_state *)opaque;
8da3ff18 504 if (offset == 0xf00) {
9ee6e8bb
PB
505 int cpu;
506 int irq;
507 int mask;
508
926c4aff 509 cpu = gic_get_current_cpu(s);
9ee6e8bb
PB
510 irq = value & 0x3ff;
511 switch ((value >> 24) & 3) {
512 case 0:
513 mask = (value >> 16) & ALL_CPU_MASK;
514 break;
515 case 1:
fa250144 516 mask = ALL_CPU_MASK ^ (1 << cpu);
9ee6e8bb
PB
517 break;
518 case 2:
fa250144 519 mask = 1 << cpu;
9ee6e8bb
PB
520 break;
521 default:
522 DPRINTF("Bad Soft Int target filter\n");
523 mask = ALL_CPU_MASK;
524 break;
525 }
526 GIC_SET_PENDING(irq, mask);
527 gic_update(s);
528 return;
529 }
e69954b9
PB
530 gic_dist_writew(opaque, offset, value & 0xffff);
531 gic_dist_writew(opaque, offset + 2, value >> 16);
532}
533
755c0802
AK
534static const MemoryRegionOps gic_dist_ops = {
535 .old_mmio = {
536 .read = { gic_dist_readb, gic_dist_readw, gic_dist_readl, },
537 .write = { gic_dist_writeb, gic_dist_writew, gic_dist_writel, },
538 },
539 .endianness = DEVICE_NATIVE_ENDIAN,
e69954b9
PB
540};
541
9ee6e8bb 542static uint32_t gic_cpu_read(gic_state *s, int cpu, int offset)
e69954b9 543{
e69954b9
PB
544 switch (offset) {
545 case 0x00: /* Control */
9ee6e8bb 546 return s->cpu_enabled[cpu];
e69954b9 547 case 0x04: /* Priority mask */
9ee6e8bb 548 return s->priority_mask[cpu];
e69954b9
PB
549 case 0x08: /* Binary Point */
550 /* ??? Not implemented. */
551 return 0;
552 case 0x0c: /* Acknowledge */
9ee6e8bb 553 return gic_acknowledge_irq(s, cpu);
66a0a2cb 554 case 0x14: /* Running Priority */
9ee6e8bb 555 return s->running_priority[cpu];
e69954b9 556 case 0x18: /* Highest Pending Interrupt */
9ee6e8bb 557 return s->current_pending[cpu];
e69954b9 558 default:
2ac71179 559 hw_error("gic_cpu_read: Bad offset %x\n", (int)offset);
e69954b9
PB
560 return 0;
561 }
562}
563
9ee6e8bb 564static void gic_cpu_write(gic_state *s, int cpu, int offset, uint32_t value)
e69954b9 565{
e69954b9
PB
566 switch (offset) {
567 case 0x00: /* Control */
9ee6e8bb 568 s->cpu_enabled[cpu] = (value & 1);
f7c70325 569 DPRINTF("CPU %d %sabled\n", cpu, s->cpu_enabled ? "En" : "Dis");
e69954b9
PB
570 break;
571 case 0x04: /* Priority mask */
9ee6e8bb 572 s->priority_mask[cpu] = (value & 0xff);
e69954b9
PB
573 break;
574 case 0x08: /* Binary Point */
575 /* ??? Not implemented. */
576 break;
577 case 0x10: /* End Of Interrupt */
9ee6e8bb 578 return gic_complete_irq(s, cpu, value & 0x3ff);
e69954b9 579 default:
2ac71179 580 hw_error("gic_cpu_write: Bad offset %x\n", (int)offset);
e69954b9
PB
581 return;
582 }
583 gic_update(s);
584}
e2c56465
PM
585
586/* Wrappers to read/write the GIC CPU interface for the current CPU */
587static uint64_t gic_thiscpu_read(void *opaque, target_phys_addr_t addr,
588 unsigned size)
589{
590 gic_state *s = (gic_state *)opaque;
926c4aff 591 return gic_cpu_read(s, gic_get_current_cpu(s), addr);
e2c56465
PM
592}
593
594static void gic_thiscpu_write(void *opaque, target_phys_addr_t addr,
595 uint64_t value, unsigned size)
596{
597 gic_state *s = (gic_state *)opaque;
926c4aff 598 gic_cpu_write(s, gic_get_current_cpu(s), addr, value);
e2c56465
PM
599}
600
601/* Wrappers to read/write the GIC CPU interface for a specific CPU.
602 * These just decode the opaque pointer into gic_state* + cpu id.
603 */
604static uint64_t gic_do_cpu_read(void *opaque, target_phys_addr_t addr,
605 unsigned size)
606{
607 gic_state **backref = (gic_state **)opaque;
608 gic_state *s = *backref;
609 int id = (backref - s->backref);
0e4a398a 610 return gic_cpu_read(s, id, addr);
e2c56465
PM
611}
612
613static void gic_do_cpu_write(void *opaque, target_phys_addr_t addr,
614 uint64_t value, unsigned size)
615{
616 gic_state **backref = (gic_state **)opaque;
617 gic_state *s = *backref;
618 int id = (backref - s->backref);
0e4a398a 619 gic_cpu_write(s, id, addr, value);
e2c56465
PM
620}
621
622static const MemoryRegionOps gic_thiscpu_ops = {
623 .read = gic_thiscpu_read,
624 .write = gic_thiscpu_write,
625 .endianness = DEVICE_NATIVE_ENDIAN,
626};
627
628static const MemoryRegionOps gic_cpu_ops = {
629 .read = gic_do_cpu_read,
630 .write = gic_do_cpu_write,
631 .endianness = DEVICE_NATIVE_ENDIAN,
632};
e69954b9 633
1e8cae4d 634void gic_init_irqs_and_distributor(gic_state *s, int num_irq)
e69954b9 635{
23e39294 636 int i;
41c1e2f5 637
544d1afa 638 i = s->num_irq - GIC_INTERNAL;
544d1afa
PM
639 /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU.
640 * GPIO array layout is thus:
641 * [0..N-1] SPIs
642 * [N..N+31] PPIs for CPU 0
643 * [N+32..N+63] PPIs for CPU 1
644 * ...
645 */
84e4fccb
PM
646 if (s->revision != REV_NVIC) {
647 i += (GIC_INTERNAL * s->num_cpu);
648 }
544d1afa 649 qdev_init_gpio_in(&s->busdev.qdev, gic_set_irq, i);
c988bfad 650 for (i = 0; i < NUM_CPU(s); i++) {
fe7e8758 651 sysbus_init_irq(&s->busdev, &s->parent_irq[i]);
e69954b9 652 }
755c0802 653 memory_region_init_io(&s->iomem, &gic_dist_ops, s, "gic_dist", 0x1000);
2b518c56
PM
654}
655
2b518c56
PM
656static int arm_gic_init(SysBusDevice *dev)
657{
658 /* Device instance init function for the GIC sysbus device */
659 int i;
660 gic_state *s = FROM_SYSBUS(gic_state, dev);
1e8cae4d
PM
661 ARMGICClass *agc = ARM_GIC_GET_CLASS(s);
662
663 agc->parent_init(dev);
664
2b518c56
PM
665 gic_init_irqs_and_distributor(s, s->num_irq);
666
e2c56465
PM
667 /* Memory regions for the CPU interfaces (NVIC doesn't have these):
668 * a region for "CPU interface for this core", then a region for
669 * "CPU interface for core 0", "for core 1", ...
670 * NB that the memory region size of 0x100 applies for the 11MPCore
671 * and also cores following the GIC v1 spec (ie A9).
672 * GIC v2 defines a larger memory region (0x1000) so this will need
673 * to be extended when we implement A15.
674 */
675 memory_region_init_io(&s->cpuiomem[0], &gic_thiscpu_ops, s,
676 "gic_cpu", 0x100);
677 for (i = 0; i < NUM_CPU(s); i++) {
678 s->backref[i] = s;
679 memory_region_init_io(&s->cpuiomem[i+1], &gic_cpu_ops, &s->backref[i],
680 "gic_cpu", 0x100);
681 }
496dbcd1
PM
682 /* Distributor */
683 sysbus_init_mmio(dev, &s->iomem);
684 /* cpu interfaces (one for "current cpu" plus one per cpu) */
685 for (i = 0; i <= NUM_CPU(s); i++) {
686 sysbus_init_mmio(dev, &s->cpuiomem[i]);
687 }
688 return 0;
689}
690
496dbcd1
PM
691static void arm_gic_class_init(ObjectClass *klass, void *data)
692{
693 DeviceClass *dc = DEVICE_CLASS(klass);
694 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
1e8cae4d
PM
695 ARMGICClass *agc = ARM_GIC_CLASS(klass);
696 agc->parent_init = sbc->init;
496dbcd1 697 sbc->init = arm_gic_init;
496dbcd1
PM
698 dc->no_user = 1;
699}
700
701static TypeInfo arm_gic_info = {
1e8cae4d
PM
702 .name = TYPE_ARM_GIC,
703 .parent = TYPE_ARM_GIC_COMMON,
496dbcd1
PM
704 .instance_size = sizeof(gic_state),
705 .class_init = arm_gic_class_init,
706};
707
708static void arm_gic_register_types(void)
709{
710 type_register_static(&arm_gic_info);
711}
712
713type_init(arm_gic_register_types)