vhost-user: use VHOST_USER_XXX macro for switch statement
[qemu/ar7.git] / hw / intc / arm_gic.c
blob8bad132d5a395ac5e03906cce4b4d14fe30abd06
1 /*
2 * ARM Generic/Distributed Interrupt Controller
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
8 */
10 /* This file contains implementation code for the RealView EB interrupt
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.
21 #include "hw/sysbus.h"
22 #include "gic_internal.h"
23 #include "qom/cpu.h"
25 //#define DEBUG_GIC
27 #ifdef DEBUG_GIC
28 #define DPRINTF(fmt, ...) \
29 do { fprintf(stderr, "arm_gic: " fmt , ## __VA_ARGS__); } while (0)
30 #else
31 #define DPRINTF(fmt, ...) do {} while(0)
32 #endif
34 static const uint8_t gic_id[] = {
35 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
38 #define NUM_CPU(s) ((s)->num_cpu)
40 static inline int gic_get_current_cpu(GICState *s)
42 if (s->num_cpu > 1) {
43 return current_cpu->cpu_index;
45 return 0;
48 /* Return true if this GIC config has interrupt groups, which is
49 * true if we're a GICv2, or a GICv1 with the security extensions.
51 static inline bool gic_has_groups(GICState *s)
53 return s->revision == 2 || s->security_extn;
56 /* TODO: Many places that call this routine could be optimized. */
57 /* Update interrupt status after enabled or pending bits have been changed. */
58 void gic_update(GICState *s)
60 int best_irq;
61 int best_prio;
62 int irq;
63 int irq_level, fiq_level;
64 int cpu;
65 int cm;
67 for (cpu = 0; cpu < NUM_CPU(s); cpu++) {
68 cm = 1 << cpu;
69 s->current_pending[cpu] = 1023;
70 if (!(s->ctlr & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1))
71 || !(s->cpu_ctlr[cpu] & (GICC_CTLR_EN_GRP0 | GICC_CTLR_EN_GRP1))) {
72 qemu_irq_lower(s->parent_irq[cpu]);
73 qemu_irq_lower(s->parent_fiq[cpu]);
74 continue;
76 best_prio = 0x100;
77 best_irq = 1023;
78 for (irq = 0; irq < s->num_irq; irq++) {
79 if (GIC_TEST_ENABLED(irq, cm) && gic_test_pending(s, irq, cm) &&
80 (irq < GIC_INTERNAL || GIC_TARGET(irq) & cm)) {
81 if (GIC_GET_PRIORITY(irq, cpu) < best_prio) {
82 best_prio = GIC_GET_PRIORITY(irq, cpu);
83 best_irq = irq;
88 irq_level = fiq_level = 0;
90 if (best_prio < s->priority_mask[cpu]) {
91 s->current_pending[cpu] = best_irq;
92 if (best_prio < s->running_priority[cpu]) {
93 int group = GIC_TEST_GROUP(best_irq, cm);
95 if (extract32(s->ctlr, group, 1) &&
96 extract32(s->cpu_ctlr[cpu], group, 1)) {
97 if (group == 0 && s->cpu_ctlr[cpu] & GICC_CTLR_FIQ_EN) {
98 DPRINTF("Raised pending FIQ %d (cpu %d)\n",
99 best_irq, cpu);
100 fiq_level = 1;
101 } else {
102 DPRINTF("Raised pending IRQ %d (cpu %d)\n",
103 best_irq, cpu);
104 irq_level = 1;
110 qemu_set_irq(s->parent_irq[cpu], irq_level);
111 qemu_set_irq(s->parent_fiq[cpu], fiq_level);
115 void gic_set_pending_private(GICState *s, int cpu, int irq)
117 int cm = 1 << cpu;
119 if (gic_test_pending(s, irq, cm)) {
120 return;
123 DPRINTF("Set %d pending cpu %d\n", irq, cpu);
124 GIC_SET_PENDING(irq, cm);
125 gic_update(s);
128 static void gic_set_irq_11mpcore(GICState *s, int irq, int level,
129 int cm, int target)
131 if (level) {
132 GIC_SET_LEVEL(irq, cm);
133 if (GIC_TEST_EDGE_TRIGGER(irq) || GIC_TEST_ENABLED(irq, cm)) {
134 DPRINTF("Set %d pending mask %x\n", irq, target);
135 GIC_SET_PENDING(irq, target);
137 } else {
138 GIC_CLEAR_LEVEL(irq, cm);
142 static void gic_set_irq_generic(GICState *s, int irq, int level,
143 int cm, int target)
145 if (level) {
146 GIC_SET_LEVEL(irq, cm);
147 DPRINTF("Set %d pending mask %x\n", irq, target);
148 if (GIC_TEST_EDGE_TRIGGER(irq)) {
149 GIC_SET_PENDING(irq, target);
151 } else {
152 GIC_CLEAR_LEVEL(irq, cm);
156 /* Process a change in an external IRQ input. */
157 static void gic_set_irq(void *opaque, int irq, int level)
159 /* Meaning of the 'irq' parameter:
160 * [0..N-1] : external interrupts
161 * [N..N+31] : PPI (internal) interrupts for CPU 0
162 * [N+32..N+63] : PPI (internal interrupts for CPU 1
163 * ...
165 GICState *s = (GICState *)opaque;
166 int cm, target;
167 if (irq < (s->num_irq - GIC_INTERNAL)) {
168 /* The first external input line is internal interrupt 32. */
169 cm = ALL_CPU_MASK;
170 irq += GIC_INTERNAL;
171 target = GIC_TARGET(irq);
172 } else {
173 int cpu;
174 irq -= (s->num_irq - GIC_INTERNAL);
175 cpu = irq / GIC_INTERNAL;
176 irq %= GIC_INTERNAL;
177 cm = 1 << cpu;
178 target = cm;
181 assert(irq >= GIC_NR_SGIS);
183 if (level == GIC_TEST_LEVEL(irq, cm)) {
184 return;
187 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
188 gic_set_irq_11mpcore(s, irq, level, cm, target);
189 } else {
190 gic_set_irq_generic(s, irq, level, cm, target);
193 gic_update(s);
196 static uint16_t gic_get_current_pending_irq(GICState *s, int cpu,
197 MemTxAttrs attrs)
199 uint16_t pending_irq = s->current_pending[cpu];
201 if (pending_irq < GIC_MAXIRQ && gic_has_groups(s)) {
202 int group = GIC_TEST_GROUP(pending_irq, (1 << cpu));
203 /* On a GIC without the security extensions, reading this register
204 * behaves in the same way as a secure access to a GIC with them.
206 bool secure = !s->security_extn || attrs.secure;
208 if (group == 0 && !secure) {
209 /* Group0 interrupts hidden from Non-secure access */
210 return 1023;
212 if (group == 1 && secure && !(s->cpu_ctlr[cpu] & GICC_CTLR_ACK_CTL)) {
213 /* Group1 interrupts only seen by Secure access if
214 * AckCtl bit set.
216 return 1022;
219 return pending_irq;
222 static int gic_get_group_priority(GICState *s, int cpu, int irq)
224 /* Return the group priority of the specified interrupt
225 * (which is the top bits of its priority, with the number
226 * of bits masked determined by the applicable binary point register).
228 int bpr;
229 uint32_t mask;
231 if (gic_has_groups(s) &&
232 !(s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) &&
233 GIC_TEST_GROUP(irq, (1 << cpu))) {
234 bpr = s->abpr[cpu];
235 } else {
236 bpr = s->bpr[cpu];
239 /* a BPR of 0 means the group priority bits are [7:1];
240 * a BPR of 1 means they are [7:2], and so on down to
241 * a BPR of 7 meaning no group priority bits at all.
243 mask = ~0U << ((bpr & 7) + 1);
245 return GIC_GET_PRIORITY(irq, cpu) & mask;
248 static void gic_activate_irq(GICState *s, int cpu, int irq)
250 /* Set the appropriate Active Priority Register bit for this IRQ,
251 * and update the running priority.
253 int prio = gic_get_group_priority(s, cpu, irq);
254 int preemption_level = prio >> (GIC_MIN_BPR + 1);
255 int regno = preemption_level / 32;
256 int bitno = preemption_level % 32;
258 if (gic_has_groups(s) && GIC_TEST_GROUP(irq, (1 << cpu))) {
259 s->nsapr[regno][cpu] &= (1 << bitno);
260 } else {
261 s->apr[regno][cpu] &= (1 << bitno);
264 s->running_priority[cpu] = prio;
265 GIC_SET_ACTIVE(irq, 1 << cpu);
268 static int gic_get_prio_from_apr_bits(GICState *s, int cpu)
270 /* Recalculate the current running priority for this CPU based
271 * on the set bits in the Active Priority Registers.
273 int i;
274 for (i = 0; i < GIC_NR_APRS; i++) {
275 uint32_t apr = s->apr[i][cpu] | s->nsapr[i][cpu];
276 if (!apr) {
277 continue;
279 return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1);
281 return 0x100;
284 static void gic_drop_prio(GICState *s, int cpu, int group)
286 /* Drop the priority of the currently active interrupt in the
287 * specified group.
289 * Note that we can guarantee (because of the requirement to nest
290 * GICC_IAR reads [which activate an interrupt and raise priority]
291 * with GICC_EOIR writes [which drop the priority for the interrupt])
292 * that the interrupt we're being called for is the highest priority
293 * active interrupt, meaning that it has the lowest set bit in the
294 * APR registers.
296 * If the guest does not honour the ordering constraints then the
297 * behaviour of the GIC is UNPREDICTABLE, which for us means that
298 * the values of the APR registers might become incorrect and the
299 * running priority will be wrong, so interrupts that should preempt
300 * might not do so, and interrupts that should not preempt might do so.
302 int i;
304 for (i = 0; i < GIC_NR_APRS; i++) {
305 uint32_t *papr = group ? &s->nsapr[i][cpu] : &s->apr[i][cpu];
306 if (!*papr) {
307 continue;
309 /* Clear lowest set bit */
310 *papr &= *papr - 1;
311 break;
314 s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu);
317 uint32_t gic_acknowledge_irq(GICState *s, int cpu, MemTxAttrs attrs)
319 int ret, irq, src;
320 int cm = 1 << cpu;
322 /* gic_get_current_pending_irq() will return 1022 or 1023 appropriately
323 * for the case where this GIC supports grouping and the pending interrupt
324 * is in the wrong group.
326 irq = gic_get_current_pending_irq(s, cpu, attrs);
328 if (irq >= GIC_MAXIRQ) {
329 DPRINTF("ACK, no pending interrupt or it is hidden: %d\n", irq);
330 return irq;
333 if (GIC_GET_PRIORITY(irq, cpu) >= s->running_priority[cpu]) {
334 DPRINTF("ACK, pending interrupt (%d) has insufficient priority\n", irq);
335 return 1023;
338 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
339 /* Clear pending flags for both level and edge triggered interrupts.
340 * Level triggered IRQs will be reasserted once they become inactive.
342 GIC_CLEAR_PENDING(irq, GIC_TEST_MODEL(irq) ? ALL_CPU_MASK : cm);
343 ret = irq;
344 } else {
345 if (irq < GIC_NR_SGIS) {
346 /* Lookup the source CPU for the SGI and clear this in the
347 * sgi_pending map. Return the src and clear the overall pending
348 * state on this CPU if the SGI is not pending from any CPUs.
350 assert(s->sgi_pending[irq][cpu] != 0);
351 src = ctz32(s->sgi_pending[irq][cpu]);
352 s->sgi_pending[irq][cpu] &= ~(1 << src);
353 if (s->sgi_pending[irq][cpu] == 0) {
354 GIC_CLEAR_PENDING(irq, GIC_TEST_MODEL(irq) ? ALL_CPU_MASK : cm);
356 ret = irq | ((src & 0x7) << 10);
357 } else {
358 /* Clear pending state for both level and edge triggered
359 * interrupts. (level triggered interrupts with an active line
360 * remain pending, see gic_test_pending)
362 GIC_CLEAR_PENDING(irq, GIC_TEST_MODEL(irq) ? ALL_CPU_MASK : cm);
363 ret = irq;
367 gic_activate_irq(s, cpu, irq);
368 gic_update(s);
369 DPRINTF("ACK %d\n", irq);
370 return ret;
373 void gic_set_priority(GICState *s, int cpu, int irq, uint8_t val,
374 MemTxAttrs attrs)
376 if (s->security_extn && !attrs.secure) {
377 if (!GIC_TEST_GROUP(irq, (1 << cpu))) {
378 return; /* Ignore Non-secure access of Group0 IRQ */
380 val = 0x80 | (val >> 1); /* Non-secure view */
383 if (irq < GIC_INTERNAL) {
384 s->priority1[irq][cpu] = val;
385 } else {
386 s->priority2[(irq) - GIC_INTERNAL] = val;
390 static uint32_t gic_get_priority(GICState *s, int cpu, int irq,
391 MemTxAttrs attrs)
393 uint32_t prio = GIC_GET_PRIORITY(irq, cpu);
395 if (s->security_extn && !attrs.secure) {
396 if (!GIC_TEST_GROUP(irq, (1 << cpu))) {
397 return 0; /* Non-secure access cannot read priority of Group0 IRQ */
399 prio = (prio << 1) & 0xff; /* Non-secure view */
401 return prio;
404 static void gic_set_priority_mask(GICState *s, int cpu, uint8_t pmask,
405 MemTxAttrs attrs)
407 if (s->security_extn && !attrs.secure) {
408 if (s->priority_mask[cpu] & 0x80) {
409 /* Priority Mask in upper half */
410 pmask = 0x80 | (pmask >> 1);
411 } else {
412 /* Non-secure write ignored if priority mask is in lower half */
413 return;
416 s->priority_mask[cpu] = pmask;
419 static uint32_t gic_get_priority_mask(GICState *s, int cpu, MemTxAttrs attrs)
421 uint32_t pmask = s->priority_mask[cpu];
423 if (s->security_extn && !attrs.secure) {
424 if (pmask & 0x80) {
425 /* Priority Mask in upper half, return Non-secure view */
426 pmask = (pmask << 1) & 0xff;
427 } else {
428 /* Priority Mask in lower half, RAZ */
429 pmask = 0;
432 return pmask;
435 static uint32_t gic_get_cpu_control(GICState *s, int cpu, MemTxAttrs attrs)
437 uint32_t ret = s->cpu_ctlr[cpu];
439 if (s->security_extn && !attrs.secure) {
440 /* Construct the NS banked view of GICC_CTLR from the correct
441 * bits of the S banked view. We don't need to move the bypass
442 * control bits because we don't implement that (IMPDEF) part
443 * of the GIC architecture.
445 ret = (ret & (GICC_CTLR_EN_GRP1 | GICC_CTLR_EOIMODE_NS)) >> 1;
447 return ret;
450 static void gic_set_cpu_control(GICState *s, int cpu, uint32_t value,
451 MemTxAttrs attrs)
453 uint32_t mask;
455 if (s->security_extn && !attrs.secure) {
456 /* The NS view can only write certain bits in the register;
457 * the rest are unchanged
459 mask = GICC_CTLR_EN_GRP1;
460 if (s->revision == 2) {
461 mask |= GICC_CTLR_EOIMODE_NS;
463 s->cpu_ctlr[cpu] &= ~mask;
464 s->cpu_ctlr[cpu] |= (value << 1) & mask;
465 } else {
466 if (s->revision == 2) {
467 mask = s->security_extn ? GICC_CTLR_V2_S_MASK : GICC_CTLR_V2_MASK;
468 } else {
469 mask = s->security_extn ? GICC_CTLR_V1_S_MASK : GICC_CTLR_V1_MASK;
471 s->cpu_ctlr[cpu] = value & mask;
473 DPRINTF("CPU Interface %d: Group0 Interrupts %sabled, "
474 "Group1 Interrupts %sabled\n", cpu,
475 (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP0) ? "En" : "Dis",
476 (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP1) ? "En" : "Dis");
479 static uint8_t gic_get_running_priority(GICState *s, int cpu, MemTxAttrs attrs)
481 if (s->security_extn && !attrs.secure) {
482 if (s->running_priority[cpu] & 0x80) {
483 /* Running priority in upper half of range: return the Non-secure
484 * view of the priority.
486 return s->running_priority[cpu] << 1;
487 } else {
488 /* Running priority in lower half of range: RAZ */
489 return 0;
491 } else {
492 return s->running_priority[cpu];
496 void gic_complete_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
498 int cm = 1 << cpu;
499 int group;
501 DPRINTF("EOI %d\n", irq);
502 if (irq >= s->num_irq) {
503 /* This handles two cases:
504 * 1. If software writes the ID of a spurious interrupt [ie 1023]
505 * to the GICC_EOIR, the GIC ignores that write.
506 * 2. If software writes the number of a non-existent interrupt
507 * this must be a subcase of "value written does not match the last
508 * valid interrupt value read from the Interrupt Acknowledge
509 * register" and so this is UNPREDICTABLE. We choose to ignore it.
511 return;
513 if (s->running_priority[cpu] == 0x100) {
514 return; /* No active IRQ. */
517 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
518 /* Mark level triggered interrupts as pending if they are still
519 raised. */
520 if (!GIC_TEST_EDGE_TRIGGER(irq) && GIC_TEST_ENABLED(irq, cm)
521 && GIC_TEST_LEVEL(irq, cm) && (GIC_TARGET(irq) & cm) != 0) {
522 DPRINTF("Set %d pending mask %x\n", irq, cm);
523 GIC_SET_PENDING(irq, cm);
527 group = gic_has_groups(s) && GIC_TEST_GROUP(irq, cm);
529 if (s->security_extn && !attrs.secure && !group) {
530 DPRINTF("Non-secure EOI for Group0 interrupt %d ignored\n", irq);
531 return;
534 /* Secure EOI with GICC_CTLR.AckCtl == 0 when the IRQ is a Group 1
535 * interrupt is UNPREDICTABLE. We choose to handle it as if AckCtl == 1,
536 * i.e. go ahead and complete the irq anyway.
539 gic_drop_prio(s, cpu, group);
540 GIC_CLEAR_ACTIVE(irq, cm);
541 gic_update(s);
544 static uint32_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs)
546 GICState *s = (GICState *)opaque;
547 uint32_t res;
548 int irq;
549 int i;
550 int cpu;
551 int cm;
552 int mask;
554 cpu = gic_get_current_cpu(s);
555 cm = 1 << cpu;
556 if (offset < 0x100) {
557 if (offset == 0) { /* GICD_CTLR */
558 if (s->security_extn && !attrs.secure) {
559 /* The NS bank of this register is just an alias of the
560 * EnableGrp1 bit in the S bank version.
562 return extract32(s->ctlr, 1, 1);
563 } else {
564 return s->ctlr;
567 if (offset == 4)
568 /* Interrupt Controller Type Register */
569 return ((s->num_irq / 32) - 1)
570 | ((NUM_CPU(s) - 1) << 5)
571 | (s->security_extn << 10);
572 if (offset < 0x08)
573 return 0;
574 if (offset >= 0x80) {
575 /* Interrupt Group Registers: these RAZ/WI if this is an NS
576 * access to a GIC with the security extensions, or if the GIC
577 * doesn't have groups at all.
579 res = 0;
580 if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
581 /* Every byte offset holds 8 group status bits */
582 irq = (offset - 0x080) * 8 + GIC_BASE_IRQ;
583 if (irq >= s->num_irq) {
584 goto bad_reg;
586 for (i = 0; i < 8; i++) {
587 if (GIC_TEST_GROUP(irq + i, cm)) {
588 res |= (1 << i);
592 return res;
594 goto bad_reg;
595 } else if (offset < 0x200) {
596 /* Interrupt Set/Clear Enable. */
597 if (offset < 0x180)
598 irq = (offset - 0x100) * 8;
599 else
600 irq = (offset - 0x180) * 8;
601 irq += GIC_BASE_IRQ;
602 if (irq >= s->num_irq)
603 goto bad_reg;
604 res = 0;
605 for (i = 0; i < 8; i++) {
606 if (GIC_TEST_ENABLED(irq + i, cm)) {
607 res |= (1 << i);
610 } else if (offset < 0x300) {
611 /* Interrupt Set/Clear Pending. */
612 if (offset < 0x280)
613 irq = (offset - 0x200) * 8;
614 else
615 irq = (offset - 0x280) * 8;
616 irq += GIC_BASE_IRQ;
617 if (irq >= s->num_irq)
618 goto bad_reg;
619 res = 0;
620 mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
621 for (i = 0; i < 8; i++) {
622 if (gic_test_pending(s, irq + i, mask)) {
623 res |= (1 << i);
626 } else if (offset < 0x400) {
627 /* Interrupt Active. */
628 irq = (offset - 0x300) * 8 + GIC_BASE_IRQ;
629 if (irq >= s->num_irq)
630 goto bad_reg;
631 res = 0;
632 mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
633 for (i = 0; i < 8; i++) {
634 if (GIC_TEST_ACTIVE(irq + i, mask)) {
635 res |= (1 << i);
638 } else if (offset < 0x800) {
639 /* Interrupt Priority. */
640 irq = (offset - 0x400) + GIC_BASE_IRQ;
641 if (irq >= s->num_irq)
642 goto bad_reg;
643 res = gic_get_priority(s, cpu, irq, attrs);
644 } else if (offset < 0xc00) {
645 /* Interrupt CPU Target. */
646 if (s->num_cpu == 1 && s->revision != REV_11MPCORE) {
647 /* For uniprocessor GICs these RAZ/WI */
648 res = 0;
649 } else {
650 irq = (offset - 0x800) + GIC_BASE_IRQ;
651 if (irq >= s->num_irq) {
652 goto bad_reg;
654 if (irq >= 29 && irq <= 31) {
655 res = cm;
656 } else {
657 res = GIC_TARGET(irq);
660 } else if (offset < 0xf00) {
661 /* Interrupt Configuration. */
662 irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ;
663 if (irq >= s->num_irq)
664 goto bad_reg;
665 res = 0;
666 for (i = 0; i < 4; i++) {
667 if (GIC_TEST_MODEL(irq + i))
668 res |= (1 << (i * 2));
669 if (GIC_TEST_EDGE_TRIGGER(irq + i))
670 res |= (2 << (i * 2));
672 } else if (offset < 0xf10) {
673 goto bad_reg;
674 } else if (offset < 0xf30) {
675 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
676 goto bad_reg;
679 if (offset < 0xf20) {
680 /* GICD_CPENDSGIRn */
681 irq = (offset - 0xf10);
682 } else {
683 irq = (offset - 0xf20);
684 /* GICD_SPENDSGIRn */
687 res = s->sgi_pending[irq][cpu];
688 } else if (offset < 0xfe0) {
689 goto bad_reg;
690 } else /* offset >= 0xfe0 */ {
691 if (offset & 3) {
692 res = 0;
693 } else {
694 res = gic_id[(offset - 0xfe0) >> 2];
697 return res;
698 bad_reg:
699 qemu_log_mask(LOG_GUEST_ERROR,
700 "gic_dist_readb: Bad offset %x\n", (int)offset);
701 return 0;
704 static MemTxResult gic_dist_read(void *opaque, hwaddr offset, uint64_t *data,
705 unsigned size, MemTxAttrs attrs)
707 switch (size) {
708 case 1:
709 *data = gic_dist_readb(opaque, offset, attrs);
710 return MEMTX_OK;
711 case 2:
712 *data = gic_dist_readb(opaque, offset, attrs);
713 *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
714 return MEMTX_OK;
715 case 4:
716 *data = gic_dist_readb(opaque, offset, attrs);
717 *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
718 *data |= gic_dist_readb(opaque, offset + 2, attrs) << 16;
719 *data |= gic_dist_readb(opaque, offset + 3, attrs) << 24;
720 return MEMTX_OK;
721 default:
722 return MEMTX_ERROR;
726 static void gic_dist_writeb(void *opaque, hwaddr offset,
727 uint32_t value, MemTxAttrs attrs)
729 GICState *s = (GICState *)opaque;
730 int irq;
731 int i;
732 int cpu;
734 cpu = gic_get_current_cpu(s);
735 if (offset < 0x100) {
736 if (offset == 0) {
737 if (s->security_extn && !attrs.secure) {
738 /* NS version is just an alias of the S version's bit 1 */
739 s->ctlr = deposit32(s->ctlr, 1, 1, value);
740 } else if (gic_has_groups(s)) {
741 s->ctlr = value & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1);
742 } else {
743 s->ctlr = value & GICD_CTLR_EN_GRP0;
745 DPRINTF("Distributor: Group0 %sabled; Group 1 %sabled\n",
746 s->ctlr & GICD_CTLR_EN_GRP0 ? "En" : "Dis",
747 s->ctlr & GICD_CTLR_EN_GRP1 ? "En" : "Dis");
748 } else if (offset < 4) {
749 /* ignored. */
750 } else if (offset >= 0x80) {
751 /* Interrupt Group Registers: RAZ/WI for NS access to secure
752 * GIC, or for GICs without groups.
754 if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
755 /* Every byte offset holds 8 group status bits */
756 irq = (offset - 0x80) * 8 + GIC_BASE_IRQ;
757 if (irq >= s->num_irq) {
758 goto bad_reg;
760 for (i = 0; i < 8; i++) {
761 /* Group bits are banked for private interrupts */
762 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
763 if (value & (1 << i)) {
764 /* Group1 (Non-secure) */
765 GIC_SET_GROUP(irq + i, cm);
766 } else {
767 /* Group0 (Secure) */
768 GIC_CLEAR_GROUP(irq + i, cm);
772 } else {
773 goto bad_reg;
775 } else if (offset < 0x180) {
776 /* Interrupt Set Enable. */
777 irq = (offset - 0x100) * 8 + GIC_BASE_IRQ;
778 if (irq >= s->num_irq)
779 goto bad_reg;
780 if (irq < GIC_NR_SGIS) {
781 value = 0xff;
784 for (i = 0; i < 8; i++) {
785 if (value & (1 << i)) {
786 int mask =
787 (irq < GIC_INTERNAL) ? (1 << cpu) : GIC_TARGET(irq + i);
788 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
790 if (!GIC_TEST_ENABLED(irq + i, cm)) {
791 DPRINTF("Enabled IRQ %d\n", irq + i);
793 GIC_SET_ENABLED(irq + i, cm);
794 /* If a raised level triggered IRQ enabled then mark
795 is as pending. */
796 if (GIC_TEST_LEVEL(irq + i, mask)
797 && !GIC_TEST_EDGE_TRIGGER(irq + i)) {
798 DPRINTF("Set %d pending mask %x\n", irq + i, mask);
799 GIC_SET_PENDING(irq + i, mask);
803 } else if (offset < 0x200) {
804 /* Interrupt Clear Enable. */
805 irq = (offset - 0x180) * 8 + GIC_BASE_IRQ;
806 if (irq >= s->num_irq)
807 goto bad_reg;
808 if (irq < GIC_NR_SGIS) {
809 value = 0;
812 for (i = 0; i < 8; i++) {
813 if (value & (1 << i)) {
814 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
816 if (GIC_TEST_ENABLED(irq + i, cm)) {
817 DPRINTF("Disabled IRQ %d\n", irq + i);
819 GIC_CLEAR_ENABLED(irq + i, cm);
822 } else if (offset < 0x280) {
823 /* Interrupt Set Pending. */
824 irq = (offset - 0x200) * 8 + GIC_BASE_IRQ;
825 if (irq >= s->num_irq)
826 goto bad_reg;
827 if (irq < GIC_NR_SGIS) {
828 value = 0;
831 for (i = 0; i < 8; i++) {
832 if (value & (1 << i)) {
833 GIC_SET_PENDING(irq + i, GIC_TARGET(irq + i));
836 } else if (offset < 0x300) {
837 /* Interrupt Clear Pending. */
838 irq = (offset - 0x280) * 8 + GIC_BASE_IRQ;
839 if (irq >= s->num_irq)
840 goto bad_reg;
841 if (irq < GIC_NR_SGIS) {
842 value = 0;
845 for (i = 0; i < 8; i++) {
846 /* ??? This currently clears the pending bit for all CPUs, even
847 for per-CPU interrupts. It's unclear whether this is the
848 corect behavior. */
849 if (value & (1 << i)) {
850 GIC_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
853 } else if (offset < 0x400) {
854 /* Interrupt Active. */
855 goto bad_reg;
856 } else if (offset < 0x800) {
857 /* Interrupt Priority. */
858 irq = (offset - 0x400) + GIC_BASE_IRQ;
859 if (irq >= s->num_irq)
860 goto bad_reg;
861 gic_set_priority(s, cpu, irq, value, attrs);
862 } else if (offset < 0xc00) {
863 /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
864 * annoying exception of the 11MPCore's GIC.
866 if (s->num_cpu != 1 || s->revision == REV_11MPCORE) {
867 irq = (offset - 0x800) + GIC_BASE_IRQ;
868 if (irq >= s->num_irq) {
869 goto bad_reg;
871 if (irq < 29) {
872 value = 0;
873 } else if (irq < GIC_INTERNAL) {
874 value = ALL_CPU_MASK;
876 s->irq_target[irq] = value & ALL_CPU_MASK;
878 } else if (offset < 0xf00) {
879 /* Interrupt Configuration. */
880 irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ;
881 if (irq >= s->num_irq)
882 goto bad_reg;
883 if (irq < GIC_NR_SGIS)
884 value |= 0xaa;
885 for (i = 0; i < 4; i++) {
886 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
887 if (value & (1 << (i * 2))) {
888 GIC_SET_MODEL(irq + i);
889 } else {
890 GIC_CLEAR_MODEL(irq + i);
893 if (value & (2 << (i * 2))) {
894 GIC_SET_EDGE_TRIGGER(irq + i);
895 } else {
896 GIC_CLEAR_EDGE_TRIGGER(irq + i);
899 } else if (offset < 0xf10) {
900 /* 0xf00 is only handled for 32-bit writes. */
901 goto bad_reg;
902 } else if (offset < 0xf20) {
903 /* GICD_CPENDSGIRn */
904 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
905 goto bad_reg;
907 irq = (offset - 0xf10);
909 s->sgi_pending[irq][cpu] &= ~value;
910 if (s->sgi_pending[irq][cpu] == 0) {
911 GIC_CLEAR_PENDING(irq, 1 << cpu);
913 } else if (offset < 0xf30) {
914 /* GICD_SPENDSGIRn */
915 if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
916 goto bad_reg;
918 irq = (offset - 0xf20);
920 GIC_SET_PENDING(irq, 1 << cpu);
921 s->sgi_pending[irq][cpu] |= value;
922 } else {
923 goto bad_reg;
925 gic_update(s);
926 return;
927 bad_reg:
928 qemu_log_mask(LOG_GUEST_ERROR,
929 "gic_dist_writeb: Bad offset %x\n", (int)offset);
932 static void gic_dist_writew(void *opaque, hwaddr offset,
933 uint32_t value, MemTxAttrs attrs)
935 gic_dist_writeb(opaque, offset, value & 0xff, attrs);
936 gic_dist_writeb(opaque, offset + 1, value >> 8, attrs);
939 static void gic_dist_writel(void *opaque, hwaddr offset,
940 uint32_t value, MemTxAttrs attrs)
942 GICState *s = (GICState *)opaque;
943 if (offset == 0xf00) {
944 int cpu;
945 int irq;
946 int mask;
947 int target_cpu;
949 cpu = gic_get_current_cpu(s);
950 irq = value & 0x3ff;
951 switch ((value >> 24) & 3) {
952 case 0:
953 mask = (value >> 16) & ALL_CPU_MASK;
954 break;
955 case 1:
956 mask = ALL_CPU_MASK ^ (1 << cpu);
957 break;
958 case 2:
959 mask = 1 << cpu;
960 break;
961 default:
962 DPRINTF("Bad Soft Int target filter\n");
963 mask = ALL_CPU_MASK;
964 break;
966 GIC_SET_PENDING(irq, mask);
967 target_cpu = ctz32(mask);
968 while (target_cpu < GIC_NCPU) {
969 s->sgi_pending[irq][target_cpu] |= (1 << cpu);
970 mask &= ~(1 << target_cpu);
971 target_cpu = ctz32(mask);
973 gic_update(s);
974 return;
976 gic_dist_writew(opaque, offset, value & 0xffff, attrs);
977 gic_dist_writew(opaque, offset + 2, value >> 16, attrs);
980 static MemTxResult gic_dist_write(void *opaque, hwaddr offset, uint64_t data,
981 unsigned size, MemTxAttrs attrs)
983 switch (size) {
984 case 1:
985 gic_dist_writeb(opaque, offset, data, attrs);
986 return MEMTX_OK;
987 case 2:
988 gic_dist_writew(opaque, offset, data, attrs);
989 return MEMTX_OK;
990 case 4:
991 gic_dist_writel(opaque, offset, data, attrs);
992 return MEMTX_OK;
993 default:
994 return MEMTX_ERROR;
998 static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno)
1000 /* Return the Nonsecure view of GICC_APR<regno>. This is the
1001 * second half of GICC_NSAPR.
1003 switch (GIC_MIN_BPR) {
1004 case 0:
1005 if (regno < 2) {
1006 return s->nsapr[regno + 2][cpu];
1008 break;
1009 case 1:
1010 if (regno == 0) {
1011 return s->nsapr[regno + 1][cpu];
1013 break;
1014 case 2:
1015 if (regno == 0) {
1016 return extract32(s->nsapr[0][cpu], 16, 16);
1018 break;
1019 case 3:
1020 if (regno == 0) {
1021 return extract32(s->nsapr[0][cpu], 8, 8);
1023 break;
1024 default:
1025 g_assert_not_reached();
1027 return 0;
1030 static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno,
1031 uint32_t value)
1033 /* Write the Nonsecure view of GICC_APR<regno>. */
1034 switch (GIC_MIN_BPR) {
1035 case 0:
1036 if (regno < 2) {
1037 s->nsapr[regno + 2][cpu] = value;
1039 break;
1040 case 1:
1041 if (regno == 0) {
1042 s->nsapr[regno + 1][cpu] = value;
1044 break;
1045 case 2:
1046 if (regno == 0) {
1047 s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 16, 16, value);
1049 break;
1050 case 3:
1051 if (regno == 0) {
1052 s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 8, 8, value);
1054 break;
1055 default:
1056 g_assert_not_reached();
1060 static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset,
1061 uint64_t *data, MemTxAttrs attrs)
1063 switch (offset) {
1064 case 0x00: /* Control */
1065 *data = gic_get_cpu_control(s, cpu, attrs);
1066 break;
1067 case 0x04: /* Priority mask */
1068 *data = gic_get_priority_mask(s, cpu, attrs);
1069 break;
1070 case 0x08: /* Binary Point */
1071 if (s->security_extn && !attrs.secure) {
1072 /* BPR is banked. Non-secure copy stored in ABPR. */
1073 *data = s->abpr[cpu];
1074 } else {
1075 *data = s->bpr[cpu];
1077 break;
1078 case 0x0c: /* Acknowledge */
1079 *data = gic_acknowledge_irq(s, cpu, attrs);
1080 break;
1081 case 0x14: /* Running Priority */
1082 *data = gic_get_running_priority(s, cpu, attrs);
1083 break;
1084 case 0x18: /* Highest Pending Interrupt */
1085 *data = gic_get_current_pending_irq(s, cpu, attrs);
1086 break;
1087 case 0x1c: /* Aliased Binary Point */
1088 /* GIC v2, no security: ABPR
1089 * GIC v1, no security: not implemented (RAZ/WI)
1090 * With security extensions, secure access: ABPR (alias of NS BPR)
1091 * With security extensions, nonsecure access: RAZ/WI
1093 if (!gic_has_groups(s) || (s->security_extn && !attrs.secure)) {
1094 *data = 0;
1095 } else {
1096 *data = s->abpr[cpu];
1098 break;
1099 case 0xd0: case 0xd4: case 0xd8: case 0xdc:
1101 int regno = (offset - 0xd0) / 4;
1103 if (regno >= GIC_NR_APRS || s->revision != 2) {
1104 *data = 0;
1105 } else if (s->security_extn && !attrs.secure) {
1106 /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
1107 *data = gic_apr_ns_view(s, regno, cpu);
1108 } else {
1109 *data = s->apr[regno][cpu];
1111 break;
1113 case 0xe0: case 0xe4: case 0xe8: case 0xec:
1115 int regno = (offset - 0xe0) / 4;
1117 if (regno >= GIC_NR_APRS || s->revision != 2 || !gic_has_groups(s) ||
1118 (s->security_extn && !attrs.secure)) {
1119 *data = 0;
1120 } else {
1121 *data = s->nsapr[regno][cpu];
1123 break;
1125 default:
1126 qemu_log_mask(LOG_GUEST_ERROR,
1127 "gic_cpu_read: Bad offset %x\n", (int)offset);
1128 return MEMTX_ERROR;
1130 return MEMTX_OK;
1133 static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset,
1134 uint32_t value, MemTxAttrs attrs)
1136 switch (offset) {
1137 case 0x00: /* Control */
1138 gic_set_cpu_control(s, cpu, value, attrs);
1139 break;
1140 case 0x04: /* Priority mask */
1141 gic_set_priority_mask(s, cpu, value, attrs);
1142 break;
1143 case 0x08: /* Binary Point */
1144 if (s->security_extn && !attrs.secure) {
1145 s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1146 } else {
1147 s->bpr[cpu] = MAX(value & 0x7, GIC_MIN_BPR);
1149 break;
1150 case 0x10: /* End Of Interrupt */
1151 gic_complete_irq(s, cpu, value & 0x3ff, attrs);
1152 return MEMTX_OK;
1153 case 0x1c: /* Aliased Binary Point */
1154 if (!gic_has_groups(s) || (s->security_extn && !attrs.secure)) {
1155 /* unimplemented, or NS access: RAZ/WI */
1156 return MEMTX_OK;
1157 } else {
1158 s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1160 break;
1161 case 0xd0: case 0xd4: case 0xd8: case 0xdc:
1163 int regno = (offset - 0xd0) / 4;
1165 if (regno >= GIC_NR_APRS || s->revision != 2) {
1166 return MEMTX_OK;
1168 if (s->security_extn && !attrs.secure) {
1169 /* NS view of GICC_APR<n> is the top half of GIC_NSAPR<n> */
1170 gic_apr_write_ns_view(s, regno, cpu, value);
1171 } else {
1172 s->apr[regno][cpu] = value;
1174 break;
1176 case 0xe0: case 0xe4: case 0xe8: case 0xec:
1178 int regno = (offset - 0xe0) / 4;
1180 if (regno >= GIC_NR_APRS || s->revision != 2) {
1181 return MEMTX_OK;
1183 if (!gic_has_groups(s) || (s->security_extn && !attrs.secure)) {
1184 return MEMTX_OK;
1186 s->nsapr[regno][cpu] = value;
1187 break;
1189 default:
1190 qemu_log_mask(LOG_GUEST_ERROR,
1191 "gic_cpu_write: Bad offset %x\n", (int)offset);
1192 return MEMTX_ERROR;
1194 gic_update(s);
1195 return MEMTX_OK;
1198 /* Wrappers to read/write the GIC CPU interface for the current CPU */
1199 static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data,
1200 unsigned size, MemTxAttrs attrs)
1202 GICState *s = (GICState *)opaque;
1203 return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs);
1206 static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr,
1207 uint64_t value, unsigned size,
1208 MemTxAttrs attrs)
1210 GICState *s = (GICState *)opaque;
1211 return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs);
1214 /* Wrappers to read/write the GIC CPU interface for a specific CPU.
1215 * These just decode the opaque pointer into GICState* + cpu id.
1217 static MemTxResult gic_do_cpu_read(void *opaque, hwaddr addr, uint64_t *data,
1218 unsigned size, MemTxAttrs attrs)
1220 GICState **backref = (GICState **)opaque;
1221 GICState *s = *backref;
1222 int id = (backref - s->backref);
1223 return gic_cpu_read(s, id, addr, data, attrs);
1226 static MemTxResult gic_do_cpu_write(void *opaque, hwaddr addr,
1227 uint64_t value, unsigned size,
1228 MemTxAttrs attrs)
1230 GICState **backref = (GICState **)opaque;
1231 GICState *s = *backref;
1232 int id = (backref - s->backref);
1233 return gic_cpu_write(s, id, addr, value, attrs);
1236 static const MemoryRegionOps gic_ops[2] = {
1238 .read_with_attrs = gic_dist_read,
1239 .write_with_attrs = gic_dist_write,
1240 .endianness = DEVICE_NATIVE_ENDIAN,
1243 .read_with_attrs = gic_thiscpu_read,
1244 .write_with_attrs = gic_thiscpu_write,
1245 .endianness = DEVICE_NATIVE_ENDIAN,
1249 static const MemoryRegionOps gic_cpu_ops = {
1250 .read_with_attrs = gic_do_cpu_read,
1251 .write_with_attrs = gic_do_cpu_write,
1252 .endianness = DEVICE_NATIVE_ENDIAN,
1255 /* This function is used by nvic model */
1256 void gic_init_irqs_and_distributor(GICState *s)
1258 gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops);
1261 static void arm_gic_realize(DeviceState *dev, Error **errp)
1263 /* Device instance realize function for the GIC sysbus device */
1264 int i;
1265 GICState *s = ARM_GIC(dev);
1266 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
1267 ARMGICClass *agc = ARM_GIC_GET_CLASS(s);
1268 Error *local_err = NULL;
1270 agc->parent_realize(dev, &local_err);
1271 if (local_err) {
1272 error_propagate(errp, local_err);
1273 return;
1276 /* This creates distributor and main CPU interface (s->cpuiomem[0]) */
1277 gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops);
1279 /* Extra core-specific regions for the CPU interfaces. This is
1280 * necessary for "franken-GIC" implementations, for example on
1281 * Exynos 4.
1282 * NB that the memory region size of 0x100 applies for the 11MPCore
1283 * and also cores following the GIC v1 spec (ie A9).
1284 * GIC v2 defines a larger memory region (0x1000) so this will need
1285 * to be extended when we implement A15.
1287 for (i = 0; i < NUM_CPU(s); i++) {
1288 s->backref[i] = s;
1289 memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops,
1290 &s->backref[i], "gic_cpu", 0x100);
1291 sysbus_init_mmio(sbd, &s->cpuiomem[i+1]);
1295 static void arm_gic_class_init(ObjectClass *klass, void *data)
1297 DeviceClass *dc = DEVICE_CLASS(klass);
1298 ARMGICClass *agc = ARM_GIC_CLASS(klass);
1300 agc->parent_realize = dc->realize;
1301 dc->realize = arm_gic_realize;
1304 static const TypeInfo arm_gic_info = {
1305 .name = TYPE_ARM_GIC,
1306 .parent = TYPE_ARM_GIC_COMMON,
1307 .instance_size = sizeof(GICState),
1308 .class_init = arm_gic_class_init,
1309 .class_size = sizeof(ARMGICClass),
1312 static void arm_gic_register_types(void)
1314 type_register_static(&arm_gic_info);
1317 type_init(arm_gic_register_types)