1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "qemu/osdep.h"
22#include "hw/irq.h"
23#include "hw/sysbus.h"
24#include "gic_internal.h"
25#include "qapi/error.h"
26#include "hw/core/cpu.h"
27#include "qemu/log.h"
28#include "qemu/module.h"
29#include "trace.h"
30#include "sysemu/kvm.h"
31
32#include "hw/fdt_generic_util.h"
33
34
35
36#ifdef DEBUG_GIC
37#define DEBUG_GIC_GATE 1
38#else
39#define DEBUG_GIC_GATE 0
40#endif
41
42#define DPRINTF(fmt, ...) do { \
43 if (DEBUG_GIC_GATE) { \
44 fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \
45 } \
46 } while (0)
47
48static const uint8_t gic_id_11mpcore[] = {
49 0x00, 0x00, 0x00, 0x00, 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
50};
51
52static const uint8_t gic_id_gicv1[] = {
53 0x04, 0x00, 0x00, 0x00, 0x90, 0xb3, 0x1b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
54};
55
56static const uint8_t gic_id_gicv2[] = {
57 0x04, 0x00, 0x00, 0x00, 0x90, 0xb4, 0x2b, 0x00, 0x0d, 0xf0, 0x05, 0xb1
58};
59
60static inline int gic_get_current_cpu(GICState *s)
61{
62 if ((s->num_cpu > 1) && (current_cpu)) {
63 return current_cpu->cpu_index % s->num_cpu;
64 }
65 return 0;
66}
67
68static inline int gic_get_current_vcpu(GICState *s)
69{
70 return gic_get_current_cpu(s) + GIC_NCPU;
71}
72
73
74
75
76static inline bool gic_has_groups(GICState *s)
77{
78 return s->revision == 2 || s->security_extn;
79}
80
81static inline bool gic_cpu_ns_access(GICState *s, int cpu, MemTxAttrs attrs)
82{
83 return !gic_is_vcpu(cpu) && s->security_extn && !attrs.secure;
84}
85
86static inline void gic_get_best_irq(GICState *s, int cpu,
87 int *best_irq, int *best_prio, int *group)
88{
89 int irq;
90 int cm = 1 << cpu;
91
92 *best_irq = 1023;
93 *best_prio = 0x100;
94
95 for (irq = 0; irq < s->num_irq; irq++) {
96 if (GIC_DIST_TEST_ENABLED(irq, cm) && gic_test_pending(s, irq, cm) &&
97 (!GIC_DIST_TEST_ACTIVE(irq, cm)) &&
98 (irq < GIC_INTERNAL || GIC_DIST_TARGET(irq) & cm)) {
99 if (GIC_DIST_GET_PRIORITY(irq, cpu) < *best_prio) {
100 *best_prio = GIC_DIST_GET_PRIORITY(irq, cpu);
101 *best_irq = irq;
102 }
103 }
104 }
105
106 if (*best_irq < 1023) {
107 *group = GIC_DIST_TEST_GROUP(*best_irq, cm);
108 }
109}
110
111static inline void gic_get_best_virq(GICState *s, int cpu,
112 int *best_irq, int *best_prio, int *group)
113{
114 int lr_idx = 0;
115
116 *best_irq = 1023;
117 *best_prio = 0x100;
118
119 for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) {
120 uint32_t lr_entry = s->h_lr[lr_idx][cpu];
121 int state = GICH_LR_STATE(lr_entry);
122
123 if (state == GICH_LR_STATE_PENDING) {
124 int prio = GICH_LR_PRIORITY(lr_entry);
125
126 if (prio < *best_prio) {
127 *best_prio = prio;
128 *best_irq = GICH_LR_VIRT_ID(lr_entry);
129 *group = GICH_LR_GROUP(lr_entry);
130 }
131 }
132 }
133}
134
135
136
137
138
139
140
141
142
143static inline bool gic_irq_signaling_enabled(GICState *s, int cpu, bool virt,
144 int group_mask)
145{
146 if (!virt && !(s->ctlr & group_mask)) {
147 return false;
148 }
149
150 if (virt && !(s->h_hcr[cpu] & R_GICH_HCR_EN_MASK)) {
151 return false;
152 }
153
154 if (!(s->cpu_ctlr[cpu] & group_mask)) {
155 return false;
156 }
157
158 return true;
159}
160
161
162
163static inline void gic_update_internal(GICState *s, bool virt)
164{
165 int best_irq;
166 int best_prio;
167 int irq_level, fiq_level;
168 int cpu, cpu_iface;
169 int group = 0;
170 qemu_irq *irq_lines = virt ? s->parent_virq : s->parent_irq;
171 qemu_irq *fiq_lines = virt ? s->parent_vfiq : s->parent_fiq;
172
173 for (cpu = 0; cpu < s->num_cpu; cpu++) {
174 cpu_iface = virt ? (cpu + GIC_NCPU) : cpu;
175
176 s->current_pending[cpu_iface] = 1023;
177 if (!gic_irq_signaling_enabled(s, cpu, virt,
178 GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1)) {
179 qemu_irq_lower(irq_lines[cpu]);
180 qemu_irq_lower(fiq_lines[cpu]);
181 continue;
182 }
183
184 if (virt) {
185 gic_get_best_virq(s, cpu, &best_irq, &best_prio, &group);
186 } else {
187 gic_get_best_irq(s, cpu, &best_irq, &best_prio, &group);
188 }
189
190 if (best_irq != 1023) {
191 trace_gic_update_bestirq(virt ? "vcpu" : "cpu", cpu,
192 best_irq, best_prio,
193 s->priority_mask[cpu_iface],
194 s->running_priority[cpu_iface]);
195 }
196
197 irq_level = fiq_level = 0;
198
199 if (best_prio < s->priority_mask[cpu_iface]) {
200 s->current_pending[cpu_iface] = best_irq;
201 if (best_prio < s->running_priority[cpu_iface]) {
202 if (gic_irq_signaling_enabled(s, cpu, virt, 1 << group)) {
203 if (group == 0 &&
204 s->cpu_ctlr[cpu_iface] & GICC_CTLR_FIQ_EN) {
205 DPRINTF("Raised pending FIQ %d (cpu %d)\n",
206 best_irq, cpu_iface);
207 fiq_level = 1;
208 trace_gic_update_set_irq(cpu, virt ? "vfiq" : "fiq",
209 fiq_level);
210 } else {
211 DPRINTF("Raised pending IRQ %d (cpu %d)\n",
212 best_irq, cpu_iface);
213 irq_level = 1;
214 trace_gic_update_set_irq(cpu, virt ? "virq" : "irq",
215 irq_level);
216 }
217 }
218 }
219 }
220
221 qemu_set_irq(irq_lines[cpu], irq_level);
222 qemu_set_irq(fiq_lines[cpu], fiq_level);
223 }
224}
225
226static void gic_update(GICState *s)
227{
228 gic_update_internal(s, false);
229}
230
231
232
233
234static inline bool gic_lr_entry_is_free(uint32_t entry)
235{
236 return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID)
237 && (GICH_LR_HW(entry) || !GICH_LR_EOI(entry));
238}
239
240
241
242
243static inline bool gic_lr_entry_is_eoi(uint32_t entry)
244{
245 return (GICH_LR_STATE(entry) == GICH_LR_STATE_INVALID)
246 && !GICH_LR_HW(entry) && GICH_LR_EOI(entry);
247}
248
249static inline void gic_extract_lr_info(GICState *s, int cpu,
250 int *num_eoi, int *num_valid, int *num_pending)
251{
252 int lr_idx;
253
254 *num_eoi = 0;
255 *num_valid = 0;
256 *num_pending = 0;
257
258 for (lr_idx = 0; lr_idx < s->num_lrs; lr_idx++) {
259 uint32_t *entry = &s->h_lr[lr_idx][cpu];
260
261 if (gic_lr_entry_is_eoi(*entry)) {
262 (*num_eoi)++;
263 }
264
265 if (GICH_LR_STATE(*entry) != GICH_LR_STATE_INVALID) {
266 (*num_valid)++;
267 }
268
269 if (GICH_LR_STATE(*entry) == GICH_LR_STATE_PENDING) {
270 (*num_pending)++;
271 }
272 }
273}
274
275static void gic_compute_misr(GICState *s, int cpu)
276{
277 uint32_t value = 0;
278 int vcpu = cpu + GIC_NCPU;
279
280 int num_eoi, num_valid, num_pending;
281
282 gic_extract_lr_info(s, cpu, &num_eoi, &num_valid, &num_pending);
283
284
285 if (num_eoi) {
286 value |= R_GICH_MISR_EOI_MASK;
287 }
288
289
290 if ((s->h_hcr[cpu] & R_GICH_HCR_UIE_MASK) && (num_valid < 2)) {
291 value |= R_GICH_MISR_U_MASK;
292 }
293
294
295 if ((s->h_hcr[cpu] & R_GICH_HCR_LRENPIE_MASK) &&
296 ((s->h_hcr[cpu] & R_GICH_HCR_EOICount_MASK) != 0)) {
297 value |= R_GICH_MISR_LRENP_MASK;
298 }
299
300
301 if ((s->h_hcr[cpu] & R_GICH_HCR_NPIE_MASK) && (num_pending == 0)) {
302 value |= R_GICH_MISR_NP_MASK;
303 }
304
305
306 if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0EIE_MASK) &&
307 (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) {
308 value |= R_GICH_MISR_VGrp0E_MASK;
309 }
310
311
312 if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP0DIE_MASK) &&
313 !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP0)) {
314 value |= R_GICH_MISR_VGrp0D_MASK;
315 }
316
317
318 if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1EIE_MASK) &&
319 (s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) {
320 value |= R_GICH_MISR_VGrp1E_MASK;
321 }
322
323
324 if ((s->h_hcr[cpu] & R_GICH_HCR_VGRP1DIE_MASK) &&
325 !(s->cpu_ctlr[vcpu] & GICC_CTLR_EN_GRP1)) {
326 value |= R_GICH_MISR_VGrp1D_MASK;
327 }
328
329 s->h_misr[cpu] = value;
330}
331
332static void gic_update_maintenance(GICState *s)
333{
334 int cpu = 0;
335 int maint_level;
336
337 for (cpu = 0; cpu < s->num_cpu; cpu++) {
338 gic_compute_misr(s, cpu);
339 maint_level = (s->h_hcr[cpu] & R_GICH_HCR_EN_MASK) && s->h_misr[cpu];
340
341 trace_gic_update_maintenance_irq(cpu, maint_level);
342 qemu_set_irq(s->maintenance_irq[cpu], maint_level);
343 }
344}
345
346static void gic_update_virt(GICState *s)
347{
348 gic_update_internal(s, true);
349 gic_update_maintenance(s);
350}
351
352static void gic_set_irq_11mpcore(GICState *s, int irq, int level,
353 int cm, int target)
354{
355 if (level) {
356 GIC_DIST_SET_LEVEL(irq, cm);
357 if (GIC_DIST_TEST_EDGE_TRIGGER(irq) || GIC_DIST_TEST_ENABLED(irq, cm)) {
358 DPRINTF("Set %d pending mask %x\n", irq, target);
359 GIC_DIST_SET_PENDING(irq, target);
360 }
361 } else {
362 GIC_DIST_CLEAR_LEVEL(irq, cm);
363 }
364}
365
366static void gic_set_irq_generic(GICState *s, int irq, int level,
367 int cm, int target)
368{
369 if (level) {
370 GIC_DIST_SET_LEVEL(irq, cm);
371 DPRINTF("Set %d pending mask %x\n", irq, target);
372 if (GIC_DIST_TEST_EDGE_TRIGGER(irq)) {
373 GIC_DIST_SET_PENDING(irq, target);
374 }
375 } else {
376 GIC_DIST_CLEAR_LEVEL(irq, cm);
377 }
378}
379
380
381static void gic_set_irq(void *opaque, int irq, int level)
382{
383
384
385
386
387
388
389 GICState *s = (GICState *)opaque;
390 int cm, target;
391 if (irq < (s->num_irq - GIC_INTERNAL)) {
392
393 cm = ALL_CPU_MASK;
394 irq += GIC_INTERNAL;
395 target = GIC_DIST_TARGET(irq);
396 } else {
397 int cpu;
398 irq -= (s->num_irq - GIC_INTERNAL);
399 cpu = irq / GIC_INTERNAL;
400 irq %= GIC_INTERNAL;
401 cm = 1 << cpu;
402 target = cm;
403 }
404
405 assert(irq >= GIC_NR_SGIS);
406
407 if (level == GIC_DIST_TEST_LEVEL(irq, cm)) {
408 return;
409 }
410
411 if (s->revision == REV_11MPCORE) {
412 gic_set_irq_11mpcore(s, irq, level, cm, target);
413 } else {
414 gic_set_irq_generic(s, irq, level, cm, target);
415 }
416 trace_gic_set_irq(irq, level, cm, target);
417
418 gic_update(s);
419}
420
421static uint16_t gic_get_current_pending_irq(GICState *s, int cpu,
422 MemTxAttrs attrs)
423{
424 uint16_t pending_irq = s->current_pending[cpu];
425
426 if (pending_irq < GIC_MAXIRQ && gic_has_groups(s)) {
427 int group = gic_test_group(s, pending_irq, cpu);
428
429
430
431
432 bool secure = !gic_cpu_ns_access(s, cpu, attrs);
433
434 if (group == 0 && !secure) {
435
436 return 1023;
437 }
438 if (group == 1 && secure && !(s->cpu_ctlr[cpu] & GICC_CTLR_ACK_CTL)) {
439
440
441
442 return 1022;
443 }
444 }
445 return pending_irq;
446}
447
448static int gic_get_group_priority(GICState *s, int cpu, int irq)
449{
450
451
452
453
454 int bpr;
455 uint32_t mask;
456
457 if (gic_has_groups(s) &&
458 !(s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) &&
459 gic_test_group(s, irq, cpu)) {
460 bpr = s->abpr[cpu] - 1;
461 assert(bpr >= 0);
462 } else {
463 bpr = s->bpr[cpu];
464 }
465
466
467
468
469
470 mask = ~0U << ((bpr & 7) + 1);
471
472 return gic_get_priority(s, irq, cpu) & mask;
473}
474
475static void gic_activate_irq(GICState *s, int cpu, int irq)
476{
477
478
479
480 int prio = gic_get_group_priority(s, cpu, irq);
481 int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
482 int preemption_level = prio >> (min_bpr + 1);
483 int regno = preemption_level / 32;
484 int bitno = preemption_level % 32;
485 uint32_t *papr = NULL;
486
487 if (gic_is_vcpu(cpu)) {
488 assert(regno == 0);
489 papr = &s->h_apr[gic_get_vcpu_real_id(cpu)];
490 } else if (gic_has_groups(s) && gic_test_group(s, irq, cpu)) {
491 papr = &s->nsapr[regno][cpu];
492 } else {
493 papr = &s->apr[regno][cpu];
494 }
495
496 *papr |= (1 << bitno);
497
498 s->running_priority[cpu] = prio;
499 gic_set_active(s, irq, cpu);
500}
501
502static int gic_get_prio_from_apr_bits(GICState *s, int cpu)
503{
504
505
506
507 int i;
508
509 if (gic_is_vcpu(cpu)) {
510 uint32_t apr = s->h_apr[gic_get_vcpu_real_id(cpu)];
511 if (apr) {
512 return ctz32(apr) << (GIC_VIRT_MIN_BPR + 1);
513 } else {
514 return 0x100;
515 }
516 }
517
518 for (i = 0; i < GIC_NR_APRS; i++) {
519 uint32_t apr = s->apr[i][cpu] | s->nsapr[i][cpu];
520 if (!apr) {
521 continue;
522 }
523 return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1);
524 }
525 return 0x100;
526}
527
528static void gic_drop_prio(GICState *s, int cpu, int group)
529{
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546 if (gic_is_vcpu(cpu)) {
547 int rcpu = gic_get_vcpu_real_id(cpu);
548
549 if (s->h_apr[rcpu]) {
550
551 s->h_apr[rcpu] &= s->h_apr[rcpu] - 1;
552 }
553 } else {
554 int i;
555
556 for (i = 0; i < GIC_NR_APRS; i++) {
557 uint32_t *papr = group ? &s->nsapr[i][cpu] : &s->apr[i][cpu];
558 if (!*papr) {
559 continue;
560 }
561
562 *papr &= *papr - 1;
563 break;
564 }
565 }
566
567 s->running_priority[cpu] = gic_get_prio_from_apr_bits(s, cpu);
568}
569
570static inline uint32_t gic_clear_pending_sgi(GICState *s, int irq, int cpu)
571{
572 int src;
573 uint32_t ret;
574
575 if (!gic_is_vcpu(cpu)) {
576
577
578
579
580 assert(s->sgi_pending[irq][cpu] != 0);
581 src = ctz32(s->sgi_pending[irq][cpu]);
582 s->sgi_pending[irq][cpu] &= ~(1 << src);
583 if (s->sgi_pending[irq][cpu] == 0) {
584 gic_clear_pending(s, irq, cpu);
585 }
586 ret = irq | ((src & 0x7) << 10);
587 } else {
588 uint32_t *lr_entry = gic_get_lr_entry(s, irq, cpu);
589 src = GICH_LR_CPUID(*lr_entry);
590
591 gic_clear_pending(s, irq, cpu);
592 ret = irq | (src << 10);
593 }
594
595 return ret;
596}
597
598uint32_t gic_acknowledge_irq(GICState *s, int cpu, MemTxAttrs attrs)
599{
600 int ret, irq;
601
602
603
604
605
606 irq = gic_get_current_pending_irq(s, cpu, attrs);
607 trace_gic_acknowledge_irq(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
608 gic_get_vcpu_real_id(cpu), irq);
609
610 if (irq >= GIC_MAXIRQ) {
611 DPRINTF("ACK, no pending interrupt or it is hidden: %d\n", irq);
612 return irq;
613 }
614
615 if (gic_get_priority(s, irq, cpu) >= s->running_priority[cpu]) {
616 DPRINTF("ACK, pending interrupt (%d) has insufficient priority\n", irq);
617 return 1023;
618 }
619
620 gic_activate_irq(s, cpu, irq);
621
622 if (s->revision == REV_11MPCORE) {
623
624
625
626 gic_clear_pending(s, irq, cpu);
627 ret = irq;
628 } else {
629 if (irq < GIC_NR_SGIS) {
630 ret = gic_clear_pending_sgi(s, irq, cpu);
631 } else {
632 gic_clear_pending(s, irq, cpu);
633 ret = irq;
634 }
635 }
636
637 if (gic_is_vcpu(cpu)) {
638 gic_update_virt(s);
639 } else {
640 gic_update(s);
641 }
642 DPRINTF("ACK %d\n", irq);
643 return ret;
644}
645
646static uint32_t gic_fullprio_mask(GICState *s, int cpu)
647{
648
649
650
651
652
653 int priBits;
654
655 if (gic_is_vcpu(cpu)) {
656 priBits = GIC_VIRT_MAX_GROUP_PRIO_BITS;
657 } else {
658 priBits = s->n_prio_bits;
659 }
660 return ~0U << (8 - priBits);
661}
662
663void gic_dist_set_priority(GICState *s, int cpu, int irq, uint8_t val,
664 MemTxAttrs attrs)
665{
666 if (s->security_extn && !attrs.secure) {
667 if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) {
668 return;
669 }
670 val = 0x80 | (val >> 1);
671 }
672
673 val &= gic_fullprio_mask(s, cpu);
674
675 if (irq < GIC_INTERNAL) {
676 s->priority1[irq][cpu] = val;
677 } else {
678 s->priority2[(irq) - GIC_INTERNAL] = val;
679 }
680}
681
682static uint32_t gic_dist_get_priority(GICState *s, int cpu, int irq,
683 MemTxAttrs attrs)
684{
685 uint32_t prio = GIC_DIST_GET_PRIORITY(irq, cpu);
686
687 if (s->security_extn && !attrs.secure) {
688 if (!GIC_DIST_TEST_GROUP(irq, (1 << cpu))) {
689 return 0;
690 }
691 prio = (prio << 1) & 0xff;
692 }
693 return prio & gic_fullprio_mask(s, cpu);
694}
695
696static void gic_set_priority_mask(GICState *s, int cpu, uint8_t pmask,
697 MemTxAttrs attrs)
698{
699 if (gic_cpu_ns_access(s, cpu, attrs)) {
700 if (s->priority_mask[cpu] & 0x80) {
701
702 pmask = 0x80 | (pmask >> 1);
703 } else {
704
705 return;
706 }
707 }
708 s->priority_mask[cpu] = pmask & gic_fullprio_mask(s, cpu);
709}
710
711static uint32_t gic_get_priority_mask(GICState *s, int cpu, MemTxAttrs attrs)
712{
713 uint32_t pmask = s->priority_mask[cpu];
714
715 if (gic_cpu_ns_access(s, cpu, attrs)) {
716 if (pmask & 0x80) {
717
718 pmask = (pmask << 1) & 0xff;
719 } else {
720
721 pmask = 0;
722 }
723 }
724 return pmask;
725}
726
727static uint32_t gic_get_cpu_control(GICState *s, int cpu, MemTxAttrs attrs)
728{
729 uint32_t ret = s->cpu_ctlr[cpu];
730
731 if (gic_cpu_ns_access(s, cpu, attrs)) {
732
733
734
735
736
737 ret = (ret & (GICC_CTLR_EN_GRP1 | GICC_CTLR_EOIMODE_NS)) >> 1;
738 }
739 return ret;
740}
741
742static void gic_set_cpu_control(GICState *s, int cpu, uint32_t value,
743 MemTxAttrs attrs)
744{
745 uint32_t mask;
746
747 if (gic_cpu_ns_access(s, cpu, attrs)) {
748
749
750
751 mask = GICC_CTLR_EN_GRP1;
752 if (s->revision == 2) {
753 mask |= GICC_CTLR_EOIMODE_NS;
754 }
755 s->cpu_ctlr[cpu] &= ~mask;
756 s->cpu_ctlr[cpu] |= (value << 1) & mask;
757 } else {
758 if (s->revision == 2) {
759 mask = s->security_extn ? GICC_CTLR_V2_S_MASK : GICC_CTLR_V2_MASK;
760 } else {
761 mask = s->security_extn ? GICC_CTLR_V1_S_MASK : GICC_CTLR_V1_MASK;
762 }
763 s->cpu_ctlr[cpu] = value & mask;
764 }
765 DPRINTF("CPU Interface %d: Group0 Interrupts %sabled, "
766 "Group1 Interrupts %sabled\n", cpu,
767 (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP0) ? "En" : "Dis",
768 (s->cpu_ctlr[cpu] & GICC_CTLR_EN_GRP1) ? "En" : "Dis");
769}
770
771static uint8_t gic_get_running_priority(GICState *s, int cpu, MemTxAttrs attrs)
772{
773 if ((s->revision != REV_11MPCORE) && (s->running_priority[cpu] > 0xff)) {
774
775 return 0xff;
776 }
777
778 if (gic_cpu_ns_access(s, cpu, attrs)) {
779 if (s->running_priority[cpu] & 0x80) {
780
781
782
783 return s->running_priority[cpu] << 1;
784 } else {
785
786 return 0;
787 }
788 } else {
789 return s->running_priority[cpu];
790 }
791}
792
793
794
795
796static bool gic_eoi_split(GICState *s, int cpu, MemTxAttrs attrs)
797{
798 if (s->revision != 2) {
799
800 return false;
801 }
802 if (gic_cpu_ns_access(s, cpu, attrs)) {
803 return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE_NS;
804 }
805 return s->cpu_ctlr[cpu] & GICC_CTLR_EOIMODE;
806}
807
808static void gic_deactivate_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
809{
810 int group;
811
812 if (irq >= GIC_MAXIRQ || (!gic_is_vcpu(cpu) && irq >= s->num_irq)) {
813
814
815
816
817
818
819
820
821
822 return;
823 }
824
825 if (!gic_eoi_split(s, cpu, attrs)) {
826
827 qemu_log_mask(LOG_GUEST_ERROR,
828 "gic_deactivate_irq: GICC_DIR write when EOIMode clear");
829 return;
830 }
831
832 if (gic_is_vcpu(cpu) && !gic_virq_is_valid(s, irq, cpu)) {
833
834
835
836 int rcpu = gic_get_vcpu_real_id(cpu);
837 s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT;
838
839
840
841
842 gic_update_virt(s);
843 return;
844 }
845
846 group = gic_has_groups(s) && gic_test_group(s, irq, cpu);
847
848 if (gic_cpu_ns_access(s, cpu, attrs) && !group) {
849 DPRINTF("Non-secure DI for Group0 interrupt %d ignored\n", irq);
850 return;
851 }
852
853 gic_clear_active(s, irq, cpu);
854}
855
856static void gic_complete_irq(GICState *s, int cpu, int irq, MemTxAttrs attrs)
857{
858 int cm = 1 << cpu;
859 int group;
860
861 DPRINTF("EOI %d\n", irq);
862 if (gic_is_vcpu(cpu)) {
863
864
865
866 bool prio_drop = s->running_priority[cpu] < 0x100;
867
868 if (irq >= GIC_MAXIRQ) {
869
870 return;
871 }
872
873 gic_drop_prio(s, cpu, 0);
874
875 if (!gic_eoi_split(s, cpu, attrs)) {
876 bool valid = gic_virq_is_valid(s, irq, cpu);
877 if (prio_drop && !valid) {
878
879
880
881
882
883
884
885 int rcpu = gic_get_vcpu_real_id(cpu);
886 s->h_hcr[rcpu] += 1 << R_GICH_HCR_EOICount_SHIFT;
887 } else if (valid) {
888 gic_clear_active(s, irq, cpu);
889 }
890 }
891
892 gic_update_virt(s);
893 return;
894 }
895
896 if (irq >= s->num_irq) {
897
898
899
900
901
902
903
904
905 return;
906 }
907 if (s->running_priority[cpu] == 0x100) {
908 return;
909 }
910
911 if (s->revision == REV_11MPCORE) {
912
913
914 if (!GIC_DIST_TEST_EDGE_TRIGGER(irq) && GIC_DIST_TEST_ENABLED(irq, cm)
915 && GIC_DIST_TEST_LEVEL(irq, cm)
916 && (GIC_DIST_TARGET(irq) & cm) != 0) {
917 DPRINTF("Set %d pending mask %x\n", irq, cm);
918 GIC_DIST_SET_PENDING(irq, cm);
919 }
920 }
921
922 group = gic_has_groups(s) && gic_test_group(s, irq, cpu);
923
924 if (gic_cpu_ns_access(s, cpu, attrs) && !group) {
925 DPRINTF("Non-secure EOI for Group0 interrupt %d ignored\n", irq);
926 return;
927 }
928
929
930
931
932
933
934 gic_drop_prio(s, cpu, group);
935
936
937 if (!gic_eoi_split(s, cpu, attrs)) {
938 gic_clear_active(s, irq, cpu);
939 }
940 gic_update(s);
941}
942
943static uint32_t gic_dist_readb(void *opaque, hwaddr offset, MemTxAttrs attrs)
944{
945 GICState *s = (GICState *)opaque;
946 uint32_t res;
947 int irq;
948 int i;
949 int cpu;
950 int cm;
951 int mask;
952
953 cpu = gic_get_current_cpu(s);
954 cm = 1 << cpu;
955 if (offset < 0x100) {
956 if (offset == 0) {
957 if (s->security_extn && !attrs.secure) {
958
959
960
961 return extract32(s->ctlr, 1, 1);
962 } else {
963 return s->ctlr;
964 }
965 }
966 if (offset == 4)
967
968 return ((s->num_irq / 32) - 1)
969 | ((s->num_cpu - 1) << 5)
970 | (s->security_extn << 10);
971 if (offset < 0x08)
972 return 0;
973 if (offset >= 0x80) {
974
975
976
977
978 res = 0;
979 if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
980
981 irq = (offset - 0x080) * 8;
982 if (irq >= s->num_irq) {
983 goto bad_reg;
984 }
985 for (i = 0; i < 8; i++) {
986 if (GIC_DIST_TEST_GROUP(irq + i, cm)) {
987 res |= (1 << i);
988 }
989 }
990 }
991 return res;
992 }
993 goto bad_reg;
994 } else if (offset < 0x200) {
995
996 if (offset < 0x180)
997 irq = (offset - 0x100) * 8;
998 else
999 irq = (offset - 0x180) * 8;
1000 if (irq >= s->num_irq)
1001 goto bad_reg;
1002 res = 0;
1003 for (i = 0; i < 8; i++) {
1004 if (s->security_extn && !attrs.secure &&
1005 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1006 continue;
1007 }
1008
1009 if (GIC_DIST_TEST_ENABLED(irq + i, cm)) {
1010 res |= (1 << i);
1011 }
1012 }
1013 } else if (offset < 0x300) {
1014
1015 if (offset < 0x280)
1016 irq = (offset - 0x200) * 8;
1017 else
1018 irq = (offset - 0x280) * 8;
1019 if (irq >= s->num_irq)
1020 goto bad_reg;
1021 res = 0;
1022 mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
1023 for (i = 0; i < 8; i++) {
1024 if (s->security_extn && !attrs.secure &&
1025 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1026 continue;
1027 }
1028
1029 if (gic_test_pending(s, irq + i, mask)) {
1030 res |= (1 << i);
1031 }
1032 }
1033 } else if (offset < 0x400) {
1034
1035 if (offset < 0x380) {
1036 irq = (offset - 0x300) * 8;
1037 } else if (s->revision == 2) {
1038 irq = (offset - 0x380) * 8;
1039 } else {
1040 goto bad_reg;
1041 }
1042
1043 if (irq >= s->num_irq)
1044 goto bad_reg;
1045 res = 0;
1046 mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
1047 for (i = 0; i < 8; i++) {
1048 if (s->security_extn && !attrs.secure &&
1049 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1050 continue;
1051 }
1052
1053 if (GIC_DIST_TEST_ACTIVE(irq + i, mask)) {
1054 res |= (1 << i);
1055 }
1056 }
1057 } else if (offset < 0x800) {
1058
1059 irq = (offset - 0x400);
1060 if (irq >= s->num_irq)
1061 goto bad_reg;
1062 res = gic_dist_get_priority(s, cpu, irq, attrs);
1063 } else if (offset < 0xc00) {
1064
1065 if (s->num_cpu == 1 && s->revision != REV_11MPCORE) {
1066
1067 res = 0;
1068 } else {
1069 irq = (offset - 0x800);
1070 if (irq >= s->num_irq) {
1071 goto bad_reg;
1072 }
1073 if (irq < 29 && s->revision == REV_11MPCORE) {
1074 res = 0;
1075 } else if (irq < GIC_INTERNAL) {
1076 res = cm;
1077 } else {
1078 res = GIC_DIST_TARGET(irq);
1079 }
1080 }
1081 } else if (offset < 0xf00) {
1082
1083 irq = (offset - 0xc00) * 4;
1084 if (irq >= s->num_irq)
1085 goto bad_reg;
1086 res = 0;
1087 for (i = 0; i < 4; i++) {
1088 if (s->security_extn && !attrs.secure &&
1089 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1090 continue;
1091 }
1092
1093 if (GIC_DIST_TEST_MODEL(irq + i)) {
1094 res |= (1 << (i * 2));
1095 }
1096 if (GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) {
1097 res |= (2 << (i * 2));
1098 }
1099 }
1100 } else if (offset < 0xf10) {
1101 goto bad_reg;
1102 } else if (offset < 0xf30) {
1103 if (s->revision == REV_11MPCORE) {
1104 goto bad_reg;
1105 }
1106
1107 if (offset < 0xf20) {
1108
1109 irq = (offset - 0xf10);
1110 } else {
1111 irq = (offset - 0xf20);
1112
1113 }
1114
1115 if (s->security_extn && !attrs.secure &&
1116 !GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
1117 res = 0;
1118 } else {
1119 res = s->sgi_pending[irq][cpu];
1120 }
1121 } else if (offset < 0xfd0) {
1122 goto bad_reg;
1123 } else if (offset < 0x1000) {
1124 if (offset & 3) {
1125 res = 0;
1126 } else {
1127 switch (s->revision) {
1128 case REV_11MPCORE:
1129 res = gic_id_11mpcore[(offset - 0xfd0) >> 2];
1130 break;
1131 case 1:
1132 res = gic_id_gicv1[(offset - 0xfd0) >> 2];
1133 break;
1134 case 2:
1135 res = gic_id_gicv2[(offset - 0xfd0) >> 2];
1136 break;
1137 default:
1138 res = 0;
1139 }
1140 }
1141 } else {
1142 g_assert_not_reached();
1143 }
1144 return res;
1145bad_reg:
1146 qemu_log_mask(LOG_GUEST_ERROR,
1147 "gic_dist_readb: Bad offset %x\n", (int)offset);
1148 return 0;
1149}
1150
1151static MemTxResult gic_dist_read(void *opaque, hwaddr offset, uint64_t *data,
1152 unsigned size, MemTxAttrs attrs)
1153{
1154 switch (size) {
1155 case 1:
1156 *data = gic_dist_readb(opaque, offset, attrs);
1157 break;
1158 case 2:
1159 *data = gic_dist_readb(opaque, offset, attrs);
1160 *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
1161 break;
1162 case 4:
1163 *data = gic_dist_readb(opaque, offset, attrs);
1164 *data |= gic_dist_readb(opaque, offset + 1, attrs) << 8;
1165 *data |= gic_dist_readb(opaque, offset + 2, attrs) << 16;
1166 *data |= gic_dist_readb(opaque, offset + 3, attrs) << 24;
1167 break;
1168 default:
1169 return MEMTX_ERROR;
1170 }
1171
1172 trace_gic_dist_read(offset, size, *data);
1173 return MEMTX_OK;
1174}
1175
1176static void gic_dist_writeb(void *opaque, hwaddr offset,
1177 uint32_t value, MemTxAttrs attrs)
1178{
1179 GICState *s = (GICState *)opaque;
1180 int irq;
1181 int i;
1182 int cpu;
1183
1184 cpu = gic_get_current_cpu(s);
1185 if (offset < 0x100) {
1186 if (offset == 0) {
1187 if (s->security_extn && !attrs.secure) {
1188
1189 s->ctlr = deposit32(s->ctlr, 1, 1, value);
1190 } else if (gic_has_groups(s)) {
1191 s->ctlr = value & (GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1);
1192 } else {
1193 s->ctlr = value & GICD_CTLR_EN_GRP0;
1194 }
1195 DPRINTF("Distributor: Group0 %sabled; Group 1 %sabled\n",
1196 s->ctlr & GICD_CTLR_EN_GRP0 ? "En" : "Dis",
1197 s->ctlr & GICD_CTLR_EN_GRP1 ? "En" : "Dis");
1198 } else if (offset < 4) {
1199
1200 } else if (offset >= 0x80) {
1201
1202
1203
1204 if (!(s->security_extn && !attrs.secure) && gic_has_groups(s)) {
1205
1206 irq = (offset - 0x80) * 8;
1207 if (irq >= s->num_irq) {
1208 goto bad_reg;
1209 }
1210 for (i = 0; i < 8; i++) {
1211
1212 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
1213 if (value & (1 << i)) {
1214
1215 GIC_DIST_SET_GROUP(irq + i, cm);
1216 } else {
1217
1218 GIC_DIST_CLEAR_GROUP(irq + i, cm);
1219 }
1220 }
1221 }
1222 } else {
1223 goto bad_reg;
1224 }
1225 } else if (offset < 0x180) {
1226
1227 irq = (offset - 0x100) * 8;
1228 if (irq >= s->num_irq)
1229 goto bad_reg;
1230 if (irq < GIC_NR_SGIS) {
1231 value = 0xff;
1232 }
1233
1234 for (i = 0; i < 8; i++) {
1235 if (value & (1 << i)) {
1236 int mask =
1237 (irq < GIC_INTERNAL) ? (1 << cpu)
1238 : GIC_DIST_TARGET(irq + i);
1239 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
1240
1241 if (s->security_extn && !attrs.secure &&
1242 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1243 continue;
1244 }
1245
1246 if (!GIC_DIST_TEST_ENABLED(irq + i, cm)) {
1247 DPRINTF("Enabled IRQ %d\n", irq + i);
1248 trace_gic_enable_irq(irq + i);
1249 }
1250 GIC_DIST_SET_ENABLED(irq + i, cm);
1251
1252
1253 if (GIC_DIST_TEST_LEVEL(irq + i, mask)
1254 && !GIC_DIST_TEST_EDGE_TRIGGER(irq + i)) {
1255 DPRINTF("Set %d pending mask %x\n", irq + i, mask);
1256 GIC_DIST_SET_PENDING(irq + i, mask);
1257 }
1258 }
1259 }
1260 } else if (offset < 0x200) {
1261
1262 irq = (offset - 0x180) * 8;
1263 if (irq >= s->num_irq)
1264 goto bad_reg;
1265 if (irq < GIC_NR_SGIS) {
1266 value = 0;
1267 }
1268
1269 for (i = 0; i < 8; i++) {
1270 if (value & (1 << i)) {
1271 int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
1272
1273 if (s->security_extn && !attrs.secure &&
1274 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1275 continue;
1276 }
1277
1278 if (GIC_DIST_TEST_ENABLED(irq + i, cm)) {
1279 DPRINTF("Disabled IRQ %d\n", irq + i);
1280 trace_gic_disable_irq(irq + i);
1281 }
1282 GIC_DIST_CLEAR_ENABLED(irq + i, cm);
1283 }
1284 }
1285 } else if (offset < 0x280) {
1286
1287 irq = (offset - 0x200) * 8;
1288 if (irq >= s->num_irq)
1289 goto bad_reg;
1290 if (irq < GIC_NR_SGIS) {
1291 value = 0;
1292 }
1293
1294 for (i = 0; i < 8; i++) {
1295 if (value & (1 << i)) {
1296 if (s->security_extn && !attrs.secure &&
1297 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1298 continue;
1299 }
1300
1301 GIC_DIST_SET_PENDING(irq + i, GIC_DIST_TARGET(irq + i));
1302 }
1303 }
1304 } else if (offset < 0x300) {
1305
1306 irq = (offset - 0x280) * 8;
1307 if (irq >= s->num_irq)
1308 goto bad_reg;
1309 if (irq < GIC_NR_SGIS) {
1310 value = 0;
1311 }
1312
1313 for (i = 0; i < 8; i++) {
1314 if (s->security_extn && !attrs.secure &&
1315 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1316 continue;
1317 }
1318
1319
1320
1321
1322 if (value & (1 << i)) {
1323 GIC_DIST_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
1324 }
1325 }
1326 } else if (offset < 0x380) {
1327
1328 if (s->revision != 2) {
1329 goto bad_reg;
1330 }
1331
1332 irq = (offset - 0x300) * 8;
1333 if (irq >= s->num_irq) {
1334 goto bad_reg;
1335 }
1336
1337
1338 int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK;
1339
1340 for (i = 0; i < 8; i++) {
1341 if (s->security_extn && !attrs.secure &&
1342 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1343 continue;
1344 }
1345
1346 if (value & (1 << i)) {
1347 GIC_DIST_SET_ACTIVE(irq + i, cm);
1348 }
1349 }
1350 } else if (offset < 0x400) {
1351
1352 if (s->revision != 2) {
1353 goto bad_reg;
1354 }
1355
1356 irq = (offset - 0x380) * 8;
1357 if (irq >= s->num_irq) {
1358 goto bad_reg;
1359 }
1360
1361
1362 int cm = irq < GIC_INTERNAL ? (1 << cpu) : ALL_CPU_MASK;
1363
1364 for (i = 0; i < 8; i++) {
1365 if (s->security_extn && !attrs.secure &&
1366 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1367 continue;
1368 }
1369
1370 if (value & (1 << i)) {
1371 GIC_DIST_CLEAR_ACTIVE(irq + i, cm);
1372 }
1373 }
1374 } else if (offset < 0x800) {
1375
1376 irq = (offset - 0x400);
1377 if (irq >= s->num_irq)
1378 goto bad_reg;
1379 gic_dist_set_priority(s, cpu, irq, value, attrs);
1380 } else if (offset < 0xc00) {
1381
1382
1383
1384 if (s->num_cpu != 1 || s->revision == REV_11MPCORE) {
1385 irq = (offset - 0x800);
1386 if (irq >= s->num_irq) {
1387 goto bad_reg;
1388 }
1389 if (irq < 29 && s->revision == REV_11MPCORE) {
1390 value = 0;
1391 } else if (irq < GIC_INTERNAL) {
1392 value = ALL_CPU_MASK;
1393 }
1394 s->irq_target[irq] = value & ALL_CPU_MASK;
1395 }
1396 } else if (offset < 0xf00) {
1397
1398 irq = (offset - 0xc00) * 4;
1399 if (irq >= s->num_irq)
1400 goto bad_reg;
1401 if (irq < GIC_NR_SGIS)
1402 value |= 0xaa;
1403 for (i = 0; i < 4; i++) {
1404 if (s->security_extn && !attrs.secure &&
1405 !GIC_DIST_TEST_GROUP(irq + i, 1 << cpu)) {
1406 continue;
1407 }
1408
1409 if (s->revision == REV_11MPCORE) {
1410 if (value & (1 << (i * 2))) {
1411 GIC_DIST_SET_MODEL(irq + i);
1412 } else {
1413 GIC_DIST_CLEAR_MODEL(irq + i);
1414 }
1415 }
1416 if (value & (2 << (i * 2))) {
1417 GIC_DIST_SET_EDGE_TRIGGER(irq + i);
1418 } else {
1419 GIC_DIST_CLEAR_EDGE_TRIGGER(irq + i);
1420 }
1421 }
1422 } else if (offset < 0xf10) {
1423
1424 goto bad_reg;
1425 } else if (offset < 0xf20) {
1426
1427 if (s->revision == REV_11MPCORE) {
1428 goto bad_reg;
1429 }
1430 irq = (offset - 0xf10);
1431
1432 if (!s->security_extn || attrs.secure ||
1433 GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
1434 s->sgi_pending[irq][cpu] &= ~value;
1435 if (s->sgi_pending[irq][cpu] == 0) {
1436 GIC_DIST_CLEAR_PENDING(irq, 1 << cpu);
1437 }
1438 }
1439 } else if (offset < 0xf30) {
1440
1441 if (s->revision == REV_11MPCORE) {
1442 goto bad_reg;
1443 }
1444 irq = (offset - 0xf20);
1445
1446 if (!s->security_extn || attrs.secure ||
1447 GIC_DIST_TEST_GROUP(irq, 1 << cpu)) {
1448 GIC_DIST_SET_PENDING(irq, 1 << cpu);
1449 s->sgi_pending[irq][cpu] |= value;
1450 }
1451 } else {
1452 goto bad_reg;
1453 }
1454 gic_update(s);
1455 return;
1456bad_reg:
1457 qemu_log_mask(LOG_GUEST_ERROR,
1458 "gic_dist_writeb: Bad offset %x\n", (int)offset);
1459}
1460
1461static void gic_dist_writew(void *opaque, hwaddr offset,
1462 uint32_t value, MemTxAttrs attrs)
1463{
1464 gic_dist_writeb(opaque, offset, value & 0xff, attrs);
1465 gic_dist_writeb(opaque, offset + 1, value >> 8, attrs);
1466}
1467
1468static void gic_dist_writel(void *opaque, hwaddr offset,
1469 uint32_t value, MemTxAttrs attrs)
1470{
1471 GICState *s = (GICState *)opaque;
1472 if (offset == 0xf00) {
1473 int cpu;
1474 int irq;
1475 int mask;
1476 int target_cpu;
1477
1478 cpu = gic_get_current_cpu(s);
1479 irq = value & 0x3ff;
1480 switch ((value >> 24) & 3) {
1481 case 0:
1482 mask = (value >> 16) & ALL_CPU_MASK;
1483 break;
1484 case 1:
1485 mask = ALL_CPU_MASK ^ (1 << cpu);
1486 break;
1487 case 2:
1488 mask = 1 << cpu;
1489 break;
1490 default:
1491 DPRINTF("Bad Soft Int target filter\n");
1492 mask = ALL_CPU_MASK;
1493 break;
1494 }
1495 GIC_DIST_SET_PENDING(irq, mask);
1496 target_cpu = ctz32(mask);
1497 while (target_cpu < GIC_NCPU) {
1498 s->sgi_pending[irq][target_cpu] |= (1 << cpu);
1499 mask &= ~(1 << target_cpu);
1500 target_cpu = ctz32(mask);
1501 }
1502 gic_update(s);
1503 return;
1504 }
1505 gic_dist_writew(opaque, offset, value & 0xffff, attrs);
1506 gic_dist_writew(opaque, offset + 2, value >> 16, attrs);
1507}
1508
1509static MemTxResult gic_dist_write(void *opaque, hwaddr offset, uint64_t data,
1510 unsigned size, MemTxAttrs attrs)
1511{
1512 trace_gic_dist_write(offset, size, data);
1513
1514 switch (size) {
1515 case 1:
1516 gic_dist_writeb(opaque, offset, data, attrs);
1517 return MEMTX_OK;
1518 case 2:
1519 gic_dist_writew(opaque, offset, data, attrs);
1520 return MEMTX_OK;
1521 case 4:
1522 gic_dist_writel(opaque, offset, data, attrs);
1523 return MEMTX_OK;
1524 default:
1525 return MEMTX_ERROR;
1526 }
1527}
1528
1529static inline uint32_t gic_apr_ns_view(GICState *s, int cpu, int regno)
1530{
1531
1532
1533
1534 switch (GIC_MIN_BPR) {
1535 case 0:
1536 if (regno < 2) {
1537 return s->nsapr[regno + 2][cpu];
1538 }
1539 break;
1540 case 1:
1541 if (regno == 0) {
1542 return s->nsapr[regno + 1][cpu];
1543 }
1544 break;
1545 case 2:
1546 if (regno == 0) {
1547 return extract32(s->nsapr[0][cpu], 16, 16);
1548 }
1549 break;
1550 case 3:
1551 if (regno == 0) {
1552 return extract32(s->nsapr[0][cpu], 8, 8);
1553 }
1554 break;
1555 default:
1556 g_assert_not_reached();
1557 }
1558 return 0;
1559}
1560
1561static inline void gic_apr_write_ns_view(GICState *s, int cpu, int regno,
1562 uint32_t value)
1563{
1564
1565 switch (GIC_MIN_BPR) {
1566 case 0:
1567 if (regno < 2) {
1568 s->nsapr[regno + 2][cpu] = value;
1569 }
1570 break;
1571 case 1:
1572 if (regno == 0) {
1573 s->nsapr[regno + 1][cpu] = value;
1574 }
1575 break;
1576 case 2:
1577 if (regno == 0) {
1578 s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 16, 16, value);
1579 }
1580 break;
1581 case 3:
1582 if (regno == 0) {
1583 s->nsapr[0][cpu] = deposit32(s->nsapr[0][cpu], 8, 8, value);
1584 }
1585 break;
1586 default:
1587 g_assert_not_reached();
1588 }
1589}
1590
1591static MemTxResult gic_cpu_read(GICState *s, int cpu, int offset,
1592 uint64_t *data, MemTxAttrs attrs)
1593{
1594 switch (offset) {
1595 case 0x00:
1596 *data = gic_get_cpu_control(s, cpu, attrs);
1597 break;
1598 case 0x04:
1599 *data = gic_get_priority_mask(s, cpu, attrs);
1600 break;
1601 case 0x08:
1602 if (gic_cpu_ns_access(s, cpu, attrs)) {
1603 if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) {
1604
1605 *data = MIN(s->bpr[cpu] + 1, 7);
1606 } else {
1607
1608 *data = s->abpr[cpu];
1609 }
1610 } else {
1611 *data = s->bpr[cpu];
1612 }
1613 break;
1614 case 0x0c:
1615 *data = gic_acknowledge_irq(s, cpu, attrs);
1616 break;
1617 case 0x14:
1618 *data = gic_get_running_priority(s, cpu, attrs);
1619 break;
1620 case 0x18:
1621 *data = gic_get_current_pending_irq(s, cpu, attrs);
1622 break;
1623 case 0x1c:
1624
1625
1626
1627
1628
1629 if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
1630 *data = 0;
1631 } else {
1632 *data = s->abpr[cpu];
1633 }
1634 break;
1635 case 0xd0: case 0xd4: case 0xd8: case 0xdc:
1636 {
1637 int regno = (offset - 0xd0) / 4;
1638 int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS;
1639
1640 if (regno >= nr_aprs || s->revision != 2) {
1641 *data = 0;
1642 } else if (gic_is_vcpu(cpu)) {
1643 *data = s->h_apr[gic_get_vcpu_real_id(cpu)];
1644 } else if (gic_cpu_ns_access(s, cpu, attrs)) {
1645
1646 *data = gic_apr_ns_view(s, regno, cpu);
1647 } else {
1648 *data = s->apr[regno][cpu];
1649 }
1650 break;
1651 }
1652 case 0xe0: case 0xe4: case 0xe8: case 0xec:
1653 {
1654 int regno = (offset - 0xe0) / 4;
1655
1656 if (regno >= GIC_NR_APRS || s->revision != 2 || !gic_has_groups(s) ||
1657 gic_cpu_ns_access(s, cpu, attrs) || gic_is_vcpu(cpu)) {
1658 *data = 0;
1659 } else {
1660 *data = s->nsapr[regno][cpu];
1661 }
1662 break;
1663 }
1664 case 0xfc:
1665 *data = s->c_iidr;
1666 break;
1667 default:
1668 qemu_log_mask(LOG_GUEST_ERROR,
1669 "gic_cpu_read: Bad offset %x\n", (int)offset);
1670 *data = 0;
1671 break;
1672 }
1673
1674 trace_gic_cpu_read(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
1675 gic_get_vcpu_real_id(cpu), offset, *data);
1676 return MEMTX_OK;
1677}
1678
1679static MemTxResult gic_cpu_write(GICState *s, int cpu, int offset,
1680 uint32_t value, MemTxAttrs attrs)
1681{
1682 trace_gic_cpu_write(gic_is_vcpu(cpu) ? "vcpu" : "cpu",
1683 gic_get_vcpu_real_id(cpu), offset, value);
1684
1685 switch (offset) {
1686 case 0x00:
1687 gic_set_cpu_control(s, cpu, value, attrs);
1688 break;
1689 case 0x04:
1690 gic_set_priority_mask(s, cpu, value, attrs);
1691 break;
1692 case 0x08:
1693 if (gic_cpu_ns_access(s, cpu, attrs)) {
1694 if (s->cpu_ctlr[cpu] & GICC_CTLR_CBPR) {
1695
1696 return MEMTX_OK;
1697 } else {
1698 s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1699 }
1700 } else {
1701 int min_bpr = gic_is_vcpu(cpu) ? GIC_VIRT_MIN_BPR : GIC_MIN_BPR;
1702 s->bpr[cpu] = MAX(value & 0x7, min_bpr);
1703 }
1704 break;
1705 case 0x10:
1706 gic_complete_irq(s, cpu, value & 0x3ff, attrs);
1707 return MEMTX_OK;
1708 case 0x1c:
1709 if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
1710
1711 return MEMTX_OK;
1712 } else {
1713 s->abpr[cpu] = MAX(value & 0x7, GIC_MIN_ABPR);
1714 }
1715 break;
1716 case 0xd0: case 0xd4: case 0xd8: case 0xdc:
1717 {
1718 int regno = (offset - 0xd0) / 4;
1719 int nr_aprs = gic_is_vcpu(cpu) ? GIC_VIRT_NR_APRS : GIC_NR_APRS;
1720
1721 if (regno >= nr_aprs || s->revision != 2) {
1722 return MEMTX_OK;
1723 }
1724 if (gic_is_vcpu(cpu)) {
1725 s->h_apr[gic_get_vcpu_real_id(cpu)] = value;
1726 } else if (gic_cpu_ns_access(s, cpu, attrs)) {
1727
1728 gic_apr_write_ns_view(s, regno, cpu, value);
1729 } else {
1730 s->apr[regno][cpu] = value;
1731 }
1732 break;
1733 }
1734 case 0xe0: case 0xe4: case 0xe8: case 0xec:
1735 {
1736 int regno = (offset - 0xe0) / 4;
1737
1738 if (regno >= GIC_NR_APRS || s->revision != 2) {
1739 return MEMTX_OK;
1740 }
1741 if (gic_is_vcpu(cpu)) {
1742 return MEMTX_OK;
1743 }
1744 if (!gic_has_groups(s) || (gic_cpu_ns_access(s, cpu, attrs))) {
1745 return MEMTX_OK;
1746 }
1747 s->nsapr[regno][cpu] = value;
1748 break;
1749 }
1750
1751 case 0x10000:
1752 case 0x1000:
1753
1754 gic_deactivate_irq(s, cpu, value & 0x3ff, attrs);
1755 break;
1756 default:
1757 qemu_log_mask(LOG_GUEST_ERROR,
1758 "gic_cpu_write: Bad offset %x\n", (int)offset);
1759 return MEMTX_OK;
1760 }
1761
1762 if (gic_is_vcpu(cpu)) {
1763 gic_update_virt(s);
1764 } else {
1765 gic_update(s);
1766 }
1767
1768 return MEMTX_OK;
1769}
1770
1771
1772static MemTxResult gic_thiscpu_read(void *opaque, hwaddr addr, uint64_t *data,
1773 unsigned size, MemTxAttrs attrs)
1774{
1775 GICState *s = (GICState *)opaque;
1776 return gic_cpu_read(s, gic_get_current_cpu(s), addr, data, attrs);
1777}
1778
1779static MemTxResult gic_thiscpu_write(void *opaque, hwaddr addr,
1780 uint64_t value, unsigned size,
1781 MemTxAttrs attrs)
1782{
1783 GICState *s = (GICState *)opaque;
1784 return gic_cpu_write(s, gic_get_current_cpu(s), addr, value, attrs);
1785}
1786
1787
1788
1789
1790static MemTxResult gic_do_cpu_read(void *opaque, hwaddr addr, uint64_t *data,
1791 unsigned size, MemTxAttrs attrs)
1792{
1793 GICState **backref = (GICState **)opaque;
1794 GICState *s = *backref;
1795 int id = (backref - s->backref);
1796 return gic_cpu_read(s, id, addr, data, attrs);
1797}
1798
1799static MemTxResult gic_do_cpu_write(void *opaque, hwaddr addr,
1800 uint64_t value, unsigned size,
1801 MemTxAttrs attrs)
1802{
1803 GICState **backref = (GICState **)opaque;
1804 GICState *s = *backref;
1805 int id = (backref - s->backref);
1806 return gic_cpu_write(s, id, addr, value, attrs);
1807}
1808
1809static MemTxResult gic_thisvcpu_read(void *opaque, hwaddr addr, uint64_t *data,
1810 unsigned size, MemTxAttrs attrs)
1811{
1812 GICState *s = (GICState *)opaque;
1813
1814 return gic_cpu_read(s, gic_get_current_vcpu(s), addr, data, attrs);
1815}
1816
1817static MemTxResult gic_thisvcpu_write(void *opaque, hwaddr addr,
1818 uint64_t value, unsigned size,
1819 MemTxAttrs attrs)
1820{
1821 GICState *s = (GICState *)opaque;
1822
1823 return gic_cpu_write(s, gic_get_current_vcpu(s), addr, value, attrs);
1824}
1825
1826static uint32_t gic_compute_eisr(GICState *s, int cpu, int lr_start)
1827{
1828 int lr_idx;
1829 uint32_t ret = 0;
1830
1831 for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) {
1832 uint32_t *entry = &s->h_lr[lr_idx][cpu];
1833 ret = deposit32(ret, lr_idx - lr_start, 1,
1834 gic_lr_entry_is_eoi(*entry));
1835 }
1836
1837 return ret;
1838}
1839
1840static uint32_t gic_compute_elrsr(GICState *s, int cpu, int lr_start)
1841{
1842 int lr_idx;
1843 uint32_t ret = 0;
1844
1845 for (lr_idx = lr_start; lr_idx < s->num_lrs; lr_idx++) {
1846 uint32_t *entry = &s->h_lr[lr_idx][cpu];
1847 ret = deposit32(ret, lr_idx - lr_start, 1,
1848 gic_lr_entry_is_free(*entry));
1849 }
1850
1851 return ret;
1852}
1853
1854static void gic_vmcr_write(GICState *s, uint32_t value, MemTxAttrs attrs)
1855{
1856 int vcpu = gic_get_current_vcpu(s);
1857 uint32_t ctlr;
1858 uint32_t abpr;
1859 uint32_t bpr;
1860 uint32_t prio_mask;
1861
1862 ctlr = FIELD_EX32(value, GICH_VMCR, VMCCtlr);
1863 abpr = FIELD_EX32(value, GICH_VMCR, VMABP);
1864 bpr = FIELD_EX32(value, GICH_VMCR, VMBP);
1865 prio_mask = FIELD_EX32(value, GICH_VMCR, VMPriMask) << 3;
1866
1867 gic_set_cpu_control(s, vcpu, ctlr, attrs);
1868 s->abpr[vcpu] = MAX(abpr, GIC_VIRT_MIN_ABPR);
1869 s->bpr[vcpu] = MAX(bpr, GIC_VIRT_MIN_BPR);
1870 gic_set_priority_mask(s, vcpu, prio_mask, attrs);
1871}
1872
1873static MemTxResult gic_hyp_read(void *opaque, int cpu, hwaddr addr,
1874 uint64_t *data, MemTxAttrs attrs)
1875{
1876 GICState *s = ARM_GIC(opaque);
1877 int vcpu = cpu + GIC_NCPU;
1878
1879 switch (addr) {
1880 case A_GICH_HCR:
1881 *data = s->h_hcr[cpu];
1882 break;
1883
1884 case A_GICH_VTR:
1885 *data = FIELD_DP32(0, GICH_VTR, ListRegs, s->num_lrs - 1);
1886 *data = FIELD_DP32(*data, GICH_VTR, PREbits,
1887 GIC_VIRT_MAX_GROUP_PRIO_BITS - 1);
1888 *data = FIELD_DP32(*data, GICH_VTR, PRIbits,
1889 (7 - GIC_VIRT_MIN_BPR) - 1);
1890 break;
1891
1892 case A_GICH_VMCR:
1893 *data = FIELD_DP32(0, GICH_VMCR, VMCCtlr,
1894 extract32(s->cpu_ctlr[vcpu], 0, 10));
1895 *data = FIELD_DP32(*data, GICH_VMCR, VMABP, s->abpr[vcpu]);
1896 *data = FIELD_DP32(*data, GICH_VMCR, VMBP, s->bpr[vcpu]);
1897 *data = FIELD_DP32(*data, GICH_VMCR, VMPriMask,
1898 extract32(s->priority_mask[vcpu], 3, 5));
1899 break;
1900
1901 case A_GICH_MISR:
1902 *data = s->h_misr[cpu];
1903 break;
1904
1905 case A_GICH_EISR0:
1906 case A_GICH_EISR1:
1907 *data = gic_compute_eisr(s, cpu, (addr - A_GICH_EISR0) * 8);
1908 break;
1909
1910 case A_GICH_ELRSR0:
1911 case A_GICH_ELRSR1:
1912 *data = gic_compute_elrsr(s, cpu, (addr - A_GICH_ELRSR0) * 8);
1913 break;
1914
1915 case A_GICH_APR:
1916 *data = s->h_apr[cpu];
1917 break;
1918
1919 case A_GICH_LR0 ... A_GICH_LR63:
1920 {
1921 int lr_idx = (addr - A_GICH_LR0) / 4;
1922
1923 if (lr_idx > s->num_lrs) {
1924 *data = 0;
1925 } else {
1926 *data = s->h_lr[lr_idx][cpu];
1927 }
1928 break;
1929 }
1930
1931 default:
1932 qemu_log_mask(LOG_GUEST_ERROR,
1933 "gic_hyp_read: Bad offset %" HWADDR_PRIx "\n", addr);
1934 return MEMTX_OK;
1935 }
1936
1937 trace_gic_hyp_read(addr, *data);
1938 return MEMTX_OK;
1939}
1940
1941static MemTxResult gic_hyp_write(void *opaque, int cpu, hwaddr addr,
1942 uint64_t value, MemTxAttrs attrs)
1943{
1944 GICState *s = ARM_GIC(opaque);
1945 int vcpu = cpu + GIC_NCPU;
1946
1947 trace_gic_hyp_write(addr, value);
1948
1949 switch (addr) {
1950 case A_GICH_HCR:
1951 s->h_hcr[cpu] = value & GICH_HCR_MASK;
1952 break;
1953
1954 case A_GICH_VMCR:
1955 gic_vmcr_write(s, value, attrs);
1956 break;
1957
1958 case A_GICH_APR:
1959 s->h_apr[cpu] = value;
1960 s->running_priority[vcpu] = gic_get_prio_from_apr_bits(s, vcpu);
1961 break;
1962
1963 case A_GICH_LR0 ... A_GICH_LR63:
1964 {
1965 int lr_idx = (addr - A_GICH_LR0) / 4;
1966
1967 if (lr_idx > s->num_lrs) {
1968 return MEMTX_OK;
1969 }
1970
1971 s->h_lr[lr_idx][cpu] = value & GICH_LR_MASK;
1972 trace_gic_lr_entry(cpu, lr_idx, s->h_lr[lr_idx][cpu]);
1973 break;
1974 }
1975
1976 default:
1977 qemu_log_mask(LOG_GUEST_ERROR,
1978 "gic_hyp_write: Bad offset %" HWADDR_PRIx "\n", addr);
1979 return MEMTX_OK;
1980 }
1981
1982 gic_update_virt(s);
1983 return MEMTX_OK;
1984}
1985
1986static MemTxResult gic_thiscpu_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
1987 unsigned size, MemTxAttrs attrs)
1988{
1989 GICState *s = (GICState *)opaque;
1990
1991 return gic_hyp_read(s, gic_get_current_cpu(s), addr, data, attrs);
1992}
1993
1994static MemTxResult gic_thiscpu_hyp_write(void *opaque, hwaddr addr,
1995 uint64_t value, unsigned size,
1996 MemTxAttrs attrs)
1997{
1998 GICState *s = (GICState *)opaque;
1999
2000 return gic_hyp_write(s, gic_get_current_cpu(s), addr, value, attrs);
2001}
2002
2003static MemTxResult gic_do_hyp_read(void *opaque, hwaddr addr, uint64_t *data,
2004 unsigned size, MemTxAttrs attrs)
2005{
2006 GICState **backref = (GICState **)opaque;
2007 GICState *s = *backref;
2008 int id = (backref - s->backref);
2009
2010 return gic_hyp_read(s, id, addr, data, attrs);
2011}
2012
2013static MemTxResult gic_do_hyp_write(void *opaque, hwaddr addr,
2014 uint64_t value, unsigned size,
2015 MemTxAttrs attrs)
2016{
2017 GICState **backref = (GICState **)opaque;
2018 GICState *s = *backref;
2019 int id = (backref - s->backref);
2020
2021 return gic_hyp_write(s, id + GIC_NCPU, addr, value, attrs);
2022
2023}
2024
2025static const MemoryRegionOps gic_ops[2] = {
2026 {
2027 .read_with_attrs = gic_dist_read,
2028 .write_with_attrs = gic_dist_write,
2029 .endianness = DEVICE_NATIVE_ENDIAN,
2030 },
2031 {
2032 .read_with_attrs = gic_thiscpu_read,
2033 .write_with_attrs = gic_thiscpu_write,
2034 .endianness = DEVICE_NATIVE_ENDIAN,
2035 }
2036};
2037
2038static const MemoryRegionOps gic_cpu_ops = {
2039 .read_with_attrs = gic_do_cpu_read,
2040 .write_with_attrs = gic_do_cpu_write,
2041 .endianness = DEVICE_NATIVE_ENDIAN,
2042};
2043
2044static const MemoryRegionOps gic_virt_ops[2] = {
2045 {
2046 .read_with_attrs = gic_thiscpu_hyp_read,
2047 .write_with_attrs = gic_thiscpu_hyp_write,
2048 .endianness = DEVICE_NATIVE_ENDIAN,
2049 },
2050 {
2051 .read_with_attrs = gic_thisvcpu_read,
2052 .write_with_attrs = gic_thisvcpu_write,
2053 .endianness = DEVICE_NATIVE_ENDIAN,
2054 }
2055};
2056
2057static const MemoryRegionOps gic_viface_ops = {
2058 .read_with_attrs = gic_do_hyp_read,
2059 .write_with_attrs = gic_do_hyp_write,
2060 .endianness = DEVICE_NATIVE_ENDIAN,
2061};
2062
2063static void arm_gic_realize(DeviceState *dev, Error **errp)
2064{
2065
2066 int i;
2067 GICState *s = ARM_GIC(dev);
2068 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
2069 ARMGICClass *agc = ARM_GIC_GET_CLASS(s);
2070 Error *local_err = NULL;
2071
2072 agc->parent_realize(dev, &local_err);
2073 if (local_err) {
2074 error_propagate(errp, local_err);
2075 return;
2076 }
2077
2078 if (kvm_enabled() && !kvm_arm_supports_user_irq()) {
2079 error_setg(errp, "KVM with user space irqchip only works when the "
2080 "host kernel supports KVM_CAP_ARM_USER_IRQ");
2081 return;
2082 }
2083
2084 if (s->n_prio_bits > GIC_MAX_PRIORITY_BITS ||
2085 (s->virt_extn ? s->n_prio_bits < GIC_VIRT_MAX_GROUP_PRIO_BITS :
2086 s->n_prio_bits < GIC_MIN_PRIORITY_BITS)) {
2087 error_setg(errp, "num-priority-bits cannot be greater than %d"
2088 " or less than %d", GIC_MAX_PRIORITY_BITS,
2089 s->virt_extn ? GIC_VIRT_MAX_GROUP_PRIO_BITS :
2090 GIC_MIN_PRIORITY_BITS);
2091 return;
2092 }
2093
2094
2095
2096
2097
2098 gic_init_irqs_and_mmio(s, gic_set_irq, gic_ops, gic_virt_ops);
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108 for (i = 0; i < s->num_cpu; i++) {
2109 s->backref[i] = s;
2110 memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops,
2111 &s->backref[i], "gic_cpu", 0x100);
2112 sysbus_init_mmio(sbd, &s->cpuiomem[i+1]);
2113 }
2114
2115
2116
2117
2118 if (s->virt_extn) {
2119 for (i = 0; i < s->num_cpu; i++) {
2120 memory_region_init_io(&s->vifaceiomem[i + 1], OBJECT(s),
2121 &gic_viface_ops, &s->backref[i],
2122 "gic_viface", 0x200);
2123 sysbus_init_mmio(sbd, &s->vifaceiomem[i + 1]);
2124 }
2125 }
2126
2127 if (!s->c_iidr) {
2128 s->c_iidr |= s->revision << 16;
2129 s->c_iidr |= 0x43B;
2130 }
2131}
2132
2133static void arm_gic_fdt_auto_parent(FDTGenericIntc *obj, Error **errp)
2134{
2135 GICState *s = ARM_GIC(obj);
2136 CPUState *cs;
2137 int i = 0;
2138
2139 for (cs = first_cpu; cs; cs = CPU_NEXT(cs)) {
2140 if (i >= s->num_cpu) {
2141 break;
2142 }
2143 qdev_connect_gpio_out_named(DEVICE(obj), "irq", i,
2144 qdev_get_gpio_in(DEVICE(cs), 0));
2145 i++;
2146 }
2147
2148
2149}
2150
2151static const FDTGenericGPIOSet arm_gic_client_gpios [] = {
2152 {
2153 .names = &fdt_generic_gpio_name_set_interrupts,
2154 .gpios = (FDTGenericGPIOConnection []) {
2155 { .name = "irq", .range = 8 },
2156 { .name = "virq", .range = 8, .fdt_index = 8},
2157 { .name = "fiq", .range = 8, .fdt_index = 16 },
2158 { .name = "vfiq", .range = 8, .fdt_index = 24 },
2159 { .name = "maint", .range = 4, .fdt_index = 32 },
2160 { },
2161 },
2162 },
2163 {
2164 .names = &fdt_generic_gpio_name_set_gpio,
2165 .gpios = (FDTGenericGPIOConnection []) {
2166 { .name = "pwr_cntrl", .range = 1, .fdt_index = 0 },
2167 { .name = "rst_cntrl", .range = 1, .fdt_index = 1 },
2168 { },
2169 },
2170 },
2171 { },
2172};
2173
2174static void arm_gic_class_init(ObjectClass *klass, void *data)
2175{
2176 DeviceClass *dc = DEVICE_CLASS(klass);
2177 ARMGICClass *agc = ARM_GIC_CLASS(klass);
2178 FDTGenericIntcClass *fgic = FDT_GENERIC_INTC_CLASS(klass);
2179 FDTGenericGPIOClass *fggc = FDT_GENERIC_GPIO_CLASS(klass);
2180
2181 agc->irq_handler = gic_set_irq;
2182 device_class_set_parent_realize(dc, arm_gic_realize, &agc->parent_realize);
2183 fgic->auto_parent = arm_gic_fdt_auto_parent;
2184 fggc->client_gpios = arm_gic_client_gpios;
2185}
2186
2187static const TypeInfo arm_gic_info = {
2188 .name = TYPE_ARM_GIC,
2189 .parent = TYPE_ARM_GIC_COMMON,
2190 .instance_size = sizeof(GICState),
2191 .class_init = arm_gic_class_init,
2192 .class_size = sizeof(ARMGICClass),
2193};
2194
2195static void arm_gic_register_types(void)
2196{
2197 type_register_static(&arm_gic_info);
2198}
2199
2200type_init(arm_gic_register_types)
2201