xref: /qemu/hw/intc/arm_gicv3_cpuif.c (revision 9be38598)
1 /*
2  * ARM Generic Interrupt Controller v3
3  *
4  * Copyright (c) 2016 Linaro Limited
5  * Written by Peter Maydell
6  *
7  * This code is licensed under the GPL, version 2 or (at your option)
8  * any later version.
9  */
10 
11 /* This file contains the code for the system register interface
12  * portions of the GICv3.
13  */
14 
15 #include "qemu/osdep.h"
16 #include "trace.h"
17 #include "gicv3_internal.h"
18 #include "cpu.h"
19 
20 static GICv3CPUState *icc_cs_from_env(CPUARMState *env)
21 {
22     /* Given the CPU, find the right GICv3CPUState struct.
23      * Since we registered the CPU interface with the EL change hook as
24      * the opaque pointer, we can just directly get from the CPU to it.
25      */
26     return arm_get_el_change_hook_opaque(arm_env_get_cpu(env));
27 }
28 
29 static bool gicv3_use_ns_bank(CPUARMState *env)
30 {
31     /* Return true if we should use the NonSecure bank for a banked GIC
32      * CPU interface register. Note that this differs from the
33      * access_secure_reg() function because GICv3 banked registers are
34      * banked even for AArch64, unlike the other CPU system registers.
35      */
36     return !arm_is_secure_below_el3(env);
37 }
38 
39 static int icc_highest_active_prio(GICv3CPUState *cs)
40 {
41     /* Calculate the current running priority based on the set bits
42      * in the Active Priority Registers.
43      */
44     int i;
45 
46     for (i = 0; i < ARRAY_SIZE(cs->icc_apr[0]); i++) {
47         uint32_t apr = cs->icc_apr[GICV3_G0][i] |
48             cs->icc_apr[GICV3_G1][i] | cs->icc_apr[GICV3_G1NS][i];
49 
50         if (!apr) {
51             continue;
52         }
53         return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1);
54     }
55     /* No current active interrupts: return idle priority */
56     return 0xff;
57 }
58 
59 static uint32_t icc_gprio_mask(GICv3CPUState *cs, int group)
60 {
61     /* Return a mask word which clears the subpriority bits from
62      * a priority value for an interrupt in the specified group.
63      * This depends on the BPR value:
64      *  a BPR of 0 means the group priority bits are [7:1];
65      *  a BPR of 1 means they are [7:2], and so on down to
66      *  a BPR of 7 meaning no group priority bits at all.
67      * Which BPR to use depends on the group of the interrupt and
68      * the current ICC_CTLR.CBPR settings.
69      */
70     if ((group == GICV3_G1 && cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR) ||
71         (group == GICV3_G1NS &&
72          cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
73         group = GICV3_G0;
74     }
75 
76     return ~0U << ((cs->icc_bpr[group] & 7) + 1);
77 }
78 
79 static bool icc_no_enabled_hppi(GICv3CPUState *cs)
80 {
81     /* Return true if there is no pending interrupt, or the
82      * highest priority pending interrupt is in a group which has been
83      * disabled at the CPU interface by the ICC_IGRPEN* register enable bits.
84      */
85     return cs->hppi.prio == 0xff || (cs->icc_igrpen[cs->hppi.grp] == 0);
86 }
87 
88 static bool icc_hppi_can_preempt(GICv3CPUState *cs)
89 {
90     /* Return true if we have a pending interrupt of sufficient
91      * priority to preempt.
92      */
93     int rprio;
94     uint32_t mask;
95 
96     if (icc_no_enabled_hppi(cs)) {
97         return false;
98     }
99 
100     if (cs->hppi.prio >= cs->icc_pmr_el1) {
101         /* Priority mask masks this interrupt */
102         return false;
103     }
104 
105     rprio = icc_highest_active_prio(cs);
106     if (rprio == 0xff) {
107         /* No currently running interrupt so we can preempt */
108         return true;
109     }
110 
111     mask = icc_gprio_mask(cs, cs->hppi.grp);
112 
113     /* We only preempt a running interrupt if the pending interrupt's
114      * group priority is sufficient (the subpriorities are not considered).
115      */
116     if ((cs->hppi.prio & mask) < (rprio & mask)) {
117         return true;
118     }
119 
120     return false;
121 }
122 
123 void gicv3_cpuif_update(GICv3CPUState *cs)
124 {
125     /* Tell the CPU about its highest priority pending interrupt */
126     int irqlevel = 0;
127     int fiqlevel = 0;
128     ARMCPU *cpu = ARM_CPU(cs->cpu);
129     CPUARMState *env = &cpu->env;
130 
131     trace_gicv3_cpuif_update(gicv3_redist_affid(cs), cs->hppi.irq,
132                              cs->hppi.grp, cs->hppi.prio);
133 
134     if (cs->hppi.grp == GICV3_G1 && !arm_feature(env, ARM_FEATURE_EL3)) {
135         /* If a Security-enabled GIC sends a G1S interrupt to a
136          * Security-disabled CPU, we must treat it as if it were G0.
137          */
138         cs->hppi.grp = GICV3_G0;
139     }
140 
141     if (icc_hppi_can_preempt(cs)) {
142         /* We have an interrupt: should we signal it as IRQ or FIQ?
143          * This is described in the GICv3 spec section 4.6.2.
144          */
145         bool isfiq;
146 
147         switch (cs->hppi.grp) {
148         case GICV3_G0:
149             isfiq = true;
150             break;
151         case GICV3_G1:
152             isfiq = (!arm_is_secure(env) ||
153                      (arm_current_el(env) == 3 && arm_el_is_aa64(env, 3)));
154             break;
155         case GICV3_G1NS:
156             isfiq = arm_is_secure(env);
157             break;
158         default:
159             g_assert_not_reached();
160         }
161 
162         if (isfiq) {
163             fiqlevel = 1;
164         } else {
165             irqlevel = 1;
166         }
167     }
168 
169     trace_gicv3_cpuif_set_irqs(gicv3_redist_affid(cs), fiqlevel, irqlevel);
170 
171     qemu_set_irq(cs->parent_fiq, fiqlevel);
172     qemu_set_irq(cs->parent_irq, irqlevel);
173 }
174 
175 static uint64_t icc_pmr_read(CPUARMState *env, const ARMCPRegInfo *ri)
176 {
177     GICv3CPUState *cs = icc_cs_from_env(env);
178     uint32_t value = cs->icc_pmr_el1;
179 
180     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env) &&
181         (env->cp15.scr_el3 & SCR_FIQ)) {
182         /* NS access and Group 0 is inaccessible to NS: return the
183          * NS view of the current priority
184          */
185         if (value & 0x80) {
186             /* Secure priorities not visible to NS */
187             value = 0;
188         } else if (value != 0xff) {
189             value = (value << 1) & 0xff;
190         }
191     }
192 
193     trace_gicv3_icc_pmr_read(gicv3_redist_affid(cs), value);
194 
195     return value;
196 }
197 
198 static void icc_pmr_write(CPUARMState *env, const ARMCPRegInfo *ri,
199                           uint64_t value)
200 {
201     GICv3CPUState *cs = icc_cs_from_env(env);
202 
203     trace_gicv3_icc_pmr_write(gicv3_redist_affid(cs), value);
204 
205     value &= 0xff;
206 
207     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env) &&
208         (env->cp15.scr_el3 & SCR_FIQ)) {
209         /* NS access and Group 0 is inaccessible to NS: return the
210          * NS view of the current priority
211          */
212         if (!(cs->icc_pmr_el1 & 0x80)) {
213             /* Current PMR in the secure range, don't allow NS to change it */
214             return;
215         }
216         value = (value >> 1) & 0x80;
217     }
218     cs->icc_pmr_el1 = value;
219     gicv3_cpuif_update(cs);
220 }
221 
222 static void icc_activate_irq(GICv3CPUState *cs, int irq)
223 {
224     /* Move the interrupt from the Pending state to Active, and update
225      * the Active Priority Registers
226      */
227     uint32_t mask = icc_gprio_mask(cs, cs->hppi.grp);
228     int prio = cs->hppi.prio & mask;
229     int aprbit = prio >> 1;
230     int regno = aprbit / 32;
231     int regbit = aprbit % 32;
232 
233     cs->icc_apr[cs->hppi.grp][regno] |= (1 << regbit);
234 
235     if (irq < GIC_INTERNAL) {
236         cs->gicr_iactiver0 = deposit32(cs->gicr_iactiver0, irq, 1, 1);
237         cs->gicr_ipendr0 = deposit32(cs->gicr_ipendr0, irq, 1, 0);
238         gicv3_redist_update(cs);
239     } else {
240         gicv3_gicd_active_set(cs->gic, irq);
241         gicv3_gicd_pending_clear(cs->gic, irq);
242         gicv3_update(cs->gic, irq, 1);
243     }
244 }
245 
246 static uint64_t icc_hppir0_value(GICv3CPUState *cs, CPUARMState *env)
247 {
248     /* Return the highest priority pending interrupt register value
249      * for group 0.
250      */
251     bool irq_is_secure;
252 
253     if (cs->hppi.prio == 0xff) {
254         return INTID_SPURIOUS;
255     }
256 
257     /* Check whether we can return the interrupt or if we should return
258      * a special identifier, as per the CheckGroup0ForSpecialIdentifiers
259      * pseudocode. (We can simplify a little because for us ICC_SRE_EL1.RM
260      * is always zero.)
261      */
262     irq_is_secure = (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) &&
263                      (cs->hppi.grp != GICV3_G1NS));
264 
265     if (cs->hppi.grp != GICV3_G0 && !arm_is_el3_or_mon(env)) {
266         return INTID_SPURIOUS;
267     }
268     if (irq_is_secure && !arm_is_secure(env)) {
269         /* Secure interrupts not visible to Nonsecure */
270         return INTID_SPURIOUS;
271     }
272 
273     if (cs->hppi.grp != GICV3_G0) {
274         /* Indicate to EL3 that there's a Group 1 interrupt for the other
275          * state pending.
276          */
277         return irq_is_secure ? INTID_SECURE : INTID_NONSECURE;
278     }
279 
280     return cs->hppi.irq;
281 }
282 
283 static uint64_t icc_hppir1_value(GICv3CPUState *cs, CPUARMState *env)
284 {
285     /* Return the highest priority pending interrupt register value
286      * for group 1.
287      */
288     bool irq_is_secure;
289 
290     if (cs->hppi.prio == 0xff) {
291         return INTID_SPURIOUS;
292     }
293 
294     /* Check whether we can return the interrupt or if we should return
295      * a special identifier, as per the CheckGroup1ForSpecialIdentifiers
296      * pseudocode. (We can simplify a little because for us ICC_SRE_EL1.RM
297      * is always zero.)
298      */
299     irq_is_secure = (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) &&
300                      (cs->hppi.grp != GICV3_G1NS));
301 
302     if (cs->hppi.grp == GICV3_G0) {
303         /* Group 0 interrupts not visible via HPPIR1 */
304         return INTID_SPURIOUS;
305     }
306     if (irq_is_secure) {
307         if (!arm_is_secure(env)) {
308             /* Secure interrupts not visible in Non-secure */
309             return INTID_SPURIOUS;
310         }
311     } else if (!arm_is_el3_or_mon(env) && arm_is_secure(env)) {
312         /* Group 1 non-secure interrupts not visible in Secure EL1 */
313         return INTID_SPURIOUS;
314     }
315 
316     return cs->hppi.irq;
317 }
318 
319 static uint64_t icc_iar0_read(CPUARMState *env, const ARMCPRegInfo *ri)
320 {
321     GICv3CPUState *cs = icc_cs_from_env(env);
322     uint64_t intid;
323 
324     if (!icc_hppi_can_preempt(cs)) {
325         intid = INTID_SPURIOUS;
326     } else {
327         intid = icc_hppir0_value(cs, env);
328     }
329 
330     if (!(intid >= INTID_SECURE && intid <= INTID_SPURIOUS)) {
331         icc_activate_irq(cs, intid);
332     }
333 
334     trace_gicv3_icc_iar0_read(gicv3_redist_affid(cs), intid);
335     return intid;
336 }
337 
338 static uint64_t icc_iar1_read(CPUARMState *env, const ARMCPRegInfo *ri)
339 {
340     GICv3CPUState *cs = icc_cs_from_env(env);
341     uint64_t intid;
342 
343     if (!icc_hppi_can_preempt(cs)) {
344         intid = INTID_SPURIOUS;
345     } else {
346         intid = icc_hppir1_value(cs, env);
347     }
348 
349     if (!(intid >= INTID_SECURE && intid <= INTID_SPURIOUS)) {
350         icc_activate_irq(cs, intid);
351     }
352 
353     trace_gicv3_icc_iar1_read(gicv3_redist_affid(cs), intid);
354     return intid;
355 }
356 
357 static void icc_drop_prio(GICv3CPUState *cs, int grp)
358 {
359     /* Drop the priority of the currently active interrupt in
360      * the specified group.
361      *
362      * Note that we can guarantee (because of the requirement to nest
363      * ICC_IAR reads [which activate an interrupt and raise priority]
364      * with ICC_EOIR writes [which drop the priority for the interrupt])
365      * that the interrupt we're being called for is the highest priority
366      * active interrupt, meaning that it has the lowest set bit in the
367      * APR registers.
368      *
369      * If the guest does not honour the ordering constraints then the
370      * behaviour of the GIC is UNPREDICTABLE, which for us means that
371      * the values of the APR registers might become incorrect and the
372      * running priority will be wrong, so interrupts that should preempt
373      * might not do so, and interrupts that should not preempt might do so.
374      */
375     int i;
376 
377     for (i = 0; i < ARRAY_SIZE(cs->icc_apr[grp]); i++) {
378         uint64_t *papr = &cs->icc_apr[grp][i];
379 
380         if (!*papr) {
381             continue;
382         }
383         /* Clear the lowest set bit */
384         *papr &= *papr - 1;
385         break;
386     }
387 
388     /* running priority change means we need an update for this cpu i/f */
389     gicv3_cpuif_update(cs);
390 }
391 
392 static bool icc_eoi_split(CPUARMState *env, GICv3CPUState *cs)
393 {
394     /* Return true if we should split priority drop and interrupt
395      * deactivation, ie whether the relevant EOIMode bit is set.
396      */
397     if (arm_is_el3_or_mon(env)) {
398         return cs->icc_ctlr_el3 & ICC_CTLR_EL3_EOIMODE_EL3;
399     }
400     if (arm_is_secure_below_el3(env)) {
401         return cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_EOIMODE;
402     } else {
403         return cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE;
404     }
405 }
406 
407 static int icc_highest_active_group(GICv3CPUState *cs)
408 {
409     /* Return the group with the highest priority active interrupt.
410      * We can do this by just comparing the APRs to see which one
411      * has the lowest set bit.
412      * (If more than one group is active at the same priority then
413      * we're in UNPREDICTABLE territory.)
414      */
415     int i;
416 
417     for (i = 0; i < ARRAY_SIZE(cs->icc_apr[0]); i++) {
418         int g0ctz = ctz32(cs->icc_apr[GICV3_G0][i]);
419         int g1ctz = ctz32(cs->icc_apr[GICV3_G1][i]);
420         int g1nsctz = ctz32(cs->icc_apr[GICV3_G1NS][i]);
421 
422         if (g1nsctz < g0ctz && g1nsctz < g1ctz) {
423             return GICV3_G1NS;
424         }
425         if (g1ctz < g0ctz) {
426             return GICV3_G1;
427         }
428         if (g0ctz < 32) {
429             return GICV3_G0;
430         }
431     }
432     /* No set active bits? UNPREDICTABLE; return -1 so the caller
433      * ignores the spurious EOI attempt.
434      */
435     return -1;
436 }
437 
438 static void icc_deactivate_irq(GICv3CPUState *cs, int irq)
439 {
440     if (irq < GIC_INTERNAL) {
441         cs->gicr_iactiver0 = deposit32(cs->gicr_iactiver0, irq, 1, 0);
442         gicv3_redist_update(cs);
443     } else {
444         gicv3_gicd_active_clear(cs->gic, irq);
445         gicv3_update(cs->gic, irq, 1);
446     }
447 }
448 
449 static void icc_eoir_write(CPUARMState *env, const ARMCPRegInfo *ri,
450                            uint64_t value)
451 {
452     /* End of Interrupt */
453     GICv3CPUState *cs = icc_cs_from_env(env);
454     int irq = value & 0xffffff;
455     int grp;
456 
457     trace_gicv3_icc_eoir_write(gicv3_redist_affid(cs), value);
458 
459     if (ri->crm == 8) {
460         /* EOIR0 */
461         grp = GICV3_G0;
462     } else {
463         /* EOIR1 */
464         if (arm_is_secure(env)) {
465             grp = GICV3_G1;
466         } else {
467             grp = GICV3_G1NS;
468         }
469     }
470 
471     if (irq >= cs->gic->num_irq) {
472         /* This handles two cases:
473          * 1. If software writes the ID of a spurious interrupt [ie 1020-1023]
474          * to the GICC_EOIR, the GIC ignores that write.
475          * 2. If software writes the number of a non-existent interrupt
476          * this must be a subcase of "value written does not match the last
477          * valid interrupt value read from the Interrupt Acknowledge
478          * register" and so this is UNPREDICTABLE. We choose to ignore it.
479          */
480         return;
481     }
482 
483     if (icc_highest_active_group(cs) != grp) {
484         return;
485     }
486 
487     icc_drop_prio(cs, grp);
488 
489     if (!icc_eoi_split(env, cs)) {
490         /* Priority drop and deactivate not split: deactivate irq now */
491         icc_deactivate_irq(cs, irq);
492     }
493 }
494 
495 static uint64_t icc_hppir0_read(CPUARMState *env, const ARMCPRegInfo *ri)
496 {
497     GICv3CPUState *cs = icc_cs_from_env(env);
498     uint64_t value = icc_hppir0_value(cs, env);
499 
500     trace_gicv3_icc_hppir0_read(gicv3_redist_affid(cs), value);
501     return value;
502 }
503 
504 static uint64_t icc_hppir1_read(CPUARMState *env, const ARMCPRegInfo *ri)
505 {
506     GICv3CPUState *cs = icc_cs_from_env(env);
507     uint64_t value = icc_hppir1_value(cs, env);
508 
509     trace_gicv3_icc_hppir1_read(gicv3_redist_affid(cs), value);
510     return value;
511 }
512 
513 static uint64_t icc_bpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
514 {
515     GICv3CPUState *cs = icc_cs_from_env(env);
516     int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1;
517     bool satinc = false;
518     uint64_t bpr;
519 
520     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
521         grp = GICV3_G1NS;
522     }
523 
524     if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) &&
525         (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) {
526         /* CBPR_EL1S means secure EL1 or AArch32 EL3 !Mon BPR1 accesses
527          * modify BPR0
528          */
529         grp = GICV3_G0;
530     }
531 
532     if (grp == GICV3_G1NS && arm_current_el(env) < 3 &&
533         (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
534         /* reads return bpr0 + 1 sat to 7, writes ignored */
535         grp = GICV3_G0;
536         satinc = true;
537     }
538 
539     bpr = cs->icc_bpr[grp];
540     if (satinc) {
541         bpr++;
542         bpr = MIN(bpr, 7);
543     }
544 
545     trace_gicv3_icc_bpr_read(gicv3_redist_affid(cs), bpr);
546 
547     return bpr;
548 }
549 
550 static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
551                           uint64_t value)
552 {
553     GICv3CPUState *cs = icc_cs_from_env(env);
554     int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1;
555 
556     trace_gicv3_icc_pmr_write(gicv3_redist_affid(cs), value);
557 
558     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
559         grp = GICV3_G1NS;
560     }
561 
562     if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) &&
563         (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) {
564         /* CBPR_EL1S means secure EL1 or AArch32 EL3 !Mon BPR1 accesses
565          * modify BPR0
566          */
567         grp = GICV3_G0;
568     }
569 
570     if (grp == GICV3_G1NS && arm_current_el(env) < 3 &&
571         (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
572         /* reads return bpr0 + 1 sat to 7, writes ignored */
573         return;
574     }
575 
576     cs->icc_bpr[grp] = value & 7;
577     gicv3_cpuif_update(cs);
578 }
579 
580 static uint64_t icc_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
581 {
582     GICv3CPUState *cs = icc_cs_from_env(env);
583     uint64_t value;
584 
585     int regno = ri->opc2 & 3;
586     int grp = ri->crm & 1 ? GICV3_G0 : GICV3_G1;
587 
588     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
589         grp = GICV3_G1NS;
590     }
591 
592     value = cs->icc_apr[grp][regno];
593 
594     trace_gicv3_icc_ap_read(regno, gicv3_redist_affid(cs), value);
595     return value;
596 }
597 
598 static void icc_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
599                          uint64_t value)
600 {
601     GICv3CPUState *cs = icc_cs_from_env(env);
602 
603     int regno = ri->opc2 & 3;
604     int grp = ri->crm & 1 ? GICV3_G0 : GICV3_G1;
605 
606     trace_gicv3_icc_ap_write(regno, gicv3_redist_affid(cs), value);
607 
608     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
609         grp = GICV3_G1NS;
610     }
611 
612     /* It's not possible to claim that a Non-secure interrupt is active
613      * at a priority outside the Non-secure range (128..255), since this
614      * would otherwise allow malicious NS code to block delivery of S interrupts
615      * by writing a bad value to these registers.
616      */
617     if (grp == GICV3_G1NS && regno < 2 && arm_feature(env, ARM_FEATURE_EL3)) {
618         return;
619     }
620 
621     cs->icc_apr[grp][regno] = value & 0xFFFFFFFFU;
622     gicv3_cpuif_update(cs);
623 }
624 
625 static void icc_dir_write(CPUARMState *env, const ARMCPRegInfo *ri,
626                           uint64_t value)
627 {
628     /* Deactivate interrupt */
629     GICv3CPUState *cs = icc_cs_from_env(env);
630     int irq = value & 0xffffff;
631     bool irq_is_secure, single_sec_state, irq_is_grp0;
632     bool route_fiq_to_el3, route_irq_to_el3, route_fiq_to_el2, route_irq_to_el2;
633 
634     trace_gicv3_icc_dir_write(gicv3_redist_affid(cs), value);
635 
636     if (irq >= cs->gic->num_irq) {
637         /* Also catches special interrupt numbers and LPIs */
638         return;
639     }
640 
641     if (!icc_eoi_split(env, cs)) {
642         return;
643     }
644 
645     int grp = gicv3_irq_group(cs->gic, cs, irq);
646 
647     single_sec_state = cs->gic->gicd_ctlr & GICD_CTLR_DS;
648     irq_is_secure = !single_sec_state && (grp != GICV3_G1NS);
649     irq_is_grp0 = grp == GICV3_G0;
650 
651     /* Check whether we're allowed to deactivate this interrupt based
652      * on its group and the current CPU state.
653      * These checks are laid out to correspond to the spec's pseudocode.
654      */
655     route_fiq_to_el3 = env->cp15.scr_el3 & SCR_FIQ;
656     route_irq_to_el3 = env->cp15.scr_el3 & SCR_IRQ;
657     /* No need to include !IsSecure in route_*_to_el2 as it's only
658      * tested in cases where we know !IsSecure is true.
659      */
660     route_fiq_to_el2 = env->cp15.hcr_el2 & HCR_FMO;
661     route_irq_to_el2 = env->cp15.hcr_el2 & HCR_FMO;
662 
663     switch (arm_current_el(env)) {
664     case 3:
665         break;
666     case 2:
667         if (single_sec_state && irq_is_grp0 && !route_fiq_to_el3) {
668             break;
669         }
670         if (!irq_is_secure && !irq_is_grp0 && !route_irq_to_el3) {
671             break;
672         }
673         return;
674     case 1:
675         if (!arm_is_secure_below_el3(env)) {
676             if (single_sec_state && irq_is_grp0 &&
677                 !route_fiq_to_el3 && !route_fiq_to_el2) {
678                 break;
679             }
680             if (!irq_is_secure && !irq_is_grp0 &&
681                 !route_irq_to_el3 && !route_irq_to_el2) {
682                 break;
683             }
684         } else {
685             if (irq_is_grp0 && !route_fiq_to_el3) {
686                 break;
687             }
688             if (!irq_is_grp0 &&
689                 (!irq_is_secure || !single_sec_state) &&
690                 !route_irq_to_el3) {
691                 break;
692             }
693         }
694         return;
695     default:
696         g_assert_not_reached();
697     }
698 
699     icc_deactivate_irq(cs, irq);
700 }
701 
702 static uint64_t icc_rpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
703 {
704     GICv3CPUState *cs = icc_cs_from_env(env);
705     int prio = icc_highest_active_prio(cs);
706 
707     if (arm_feature(env, ARM_FEATURE_EL3) &&
708         !arm_is_secure(env) && (env->cp15.scr_el3 & SCR_FIQ)) {
709         /* NS GIC access and Group 0 is inaccessible to NS */
710         if (prio & 0x80) {
711             /* NS mustn't see priorities in the Secure half of the range */
712             prio = 0;
713         } else if (prio != 0xff) {
714             /* Non-idle priority: show the Non-secure view of it */
715             prio = (prio << 1) & 0xff;
716         }
717     }
718 
719     trace_gicv3_icc_rpr_read(gicv3_redist_affid(cs), prio);
720     return prio;
721 }
722 
723 static void icc_generate_sgi(CPUARMState *env, GICv3CPUState *cs,
724                              uint64_t value, int grp, bool ns)
725 {
726     GICv3State *s = cs->gic;
727 
728     /* Extract Aff3/Aff2/Aff1 and shift into the bottom 24 bits */
729     uint64_t aff = extract64(value, 48, 8) << 16 |
730         extract64(value, 32, 8) << 8 |
731         extract64(value, 16, 8);
732     uint32_t targetlist = extract64(value, 0, 16);
733     uint32_t irq = extract64(value, 24, 4);
734     bool irm = extract64(value, 40, 1);
735     int i;
736 
737     if (grp == GICV3_G1 && s->gicd_ctlr & GICD_CTLR_DS) {
738         /* If GICD_CTLR.DS == 1, the Distributor treats Secure Group 1
739          * interrupts as Group 0 interrupts and must send Secure Group 0
740          * interrupts to the target CPUs.
741          */
742         grp = GICV3_G0;
743     }
744 
745     trace_gicv3_icc_generate_sgi(gicv3_redist_affid(cs), irq, irm,
746                                  aff, targetlist);
747 
748     for (i = 0; i < s->num_cpu; i++) {
749         GICv3CPUState *ocs = &s->cpu[i];
750 
751         if (irm) {
752             /* IRM == 1 : route to all CPUs except self */
753             if (cs == ocs) {
754                 continue;
755             }
756         } else {
757             /* IRM == 0 : route to Aff3.Aff2.Aff1.n for all n in [0..15]
758              * where the corresponding bit is set in targetlist
759              */
760             int aff0;
761 
762             if (ocs->gicr_typer >> 40 != aff) {
763                 continue;
764             }
765             aff0 = extract64(ocs->gicr_typer, 32, 8);
766             if (aff0 > 15 || extract32(targetlist, aff0, 1) == 0) {
767                 continue;
768             }
769         }
770 
771         /* The redistributor will check against its own GICR_NSACR as needed */
772         gicv3_redist_send_sgi(ocs, grp, irq, ns);
773     }
774 }
775 
776 static void icc_sgi0r_write(CPUARMState *env, const ARMCPRegInfo *ri,
777                            uint64_t value)
778 {
779     /* Generate Secure Group 0 SGI. */
780     GICv3CPUState *cs = icc_cs_from_env(env);
781     bool ns = !arm_is_secure(env);
782 
783     icc_generate_sgi(env, cs, value, GICV3_G0, ns);
784 }
785 
786 static void icc_sgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri,
787                            uint64_t value)
788 {
789     /* Generate Group 1 SGI for the current Security state */
790     GICv3CPUState *cs = icc_cs_from_env(env);
791     int grp;
792     bool ns = !arm_is_secure(env);
793 
794     grp = ns ? GICV3_G1NS : GICV3_G1;
795     icc_generate_sgi(env, cs, value, grp, ns);
796 }
797 
798 static void icc_asgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri,
799                              uint64_t value)
800 {
801     /* Generate Group 1 SGI for the Security state that is not
802      * the current state
803      */
804     GICv3CPUState *cs = icc_cs_from_env(env);
805     int grp;
806     bool ns = !arm_is_secure(env);
807 
808     grp = ns ? GICV3_G1 : GICV3_G1NS;
809     icc_generate_sgi(env, cs, value, grp, ns);
810 }
811 
812 static uint64_t icc_igrpen_read(CPUARMState *env, const ARMCPRegInfo *ri)
813 {
814     GICv3CPUState *cs = icc_cs_from_env(env);
815     int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0;
816     uint64_t value;
817 
818     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
819         grp = GICV3_G1NS;
820     }
821 
822     value = cs->icc_igrpen[grp];
823     trace_gicv3_icc_igrpen_read(gicv3_redist_affid(cs), value);
824     return value;
825 }
826 
827 static void icc_igrpen_write(CPUARMState *env, const ARMCPRegInfo *ri,
828                              uint64_t value)
829 {
830     GICv3CPUState *cs = icc_cs_from_env(env);
831     int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0;
832 
833     trace_gicv3_icc_igrpen_write(gicv3_redist_affid(cs), value);
834 
835     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
836         grp = GICV3_G1NS;
837     }
838 
839     cs->icc_igrpen[grp] = value & ICC_IGRPEN_ENABLE;
840     gicv3_cpuif_update(cs);
841 }
842 
843 static uint64_t icc_igrpen1_el3_read(CPUARMState *env, const ARMCPRegInfo *ri)
844 {
845     GICv3CPUState *cs = icc_cs_from_env(env);
846 
847     /* IGRPEN1_EL3 bits 0 and 1 are r/w aliases into IGRPEN1_EL1 NS and S */
848     return cs->icc_igrpen[GICV3_G1NS] | (cs->icc_igrpen[GICV3_G1] << 1);
849 }
850 
851 static void icc_igrpen1_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
852                                   uint64_t value)
853 {
854     GICv3CPUState *cs = icc_cs_from_env(env);
855 
856     trace_gicv3_icc_igrpen1_el3_write(gicv3_redist_affid(cs), value);
857 
858     /* IGRPEN1_EL3 bits 0 and 1 are r/w aliases into IGRPEN1_EL1 NS and S */
859     cs->icc_igrpen[GICV3_G1NS] = extract32(value, 0, 1);
860     cs->icc_igrpen[GICV3_G1] = extract32(value, 1, 1);
861     gicv3_cpuif_update(cs);
862 }
863 
864 static uint64_t icc_ctlr_el1_read(CPUARMState *env, const ARMCPRegInfo *ri)
865 {
866     GICv3CPUState *cs = icc_cs_from_env(env);
867     int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S;
868     uint64_t value;
869 
870     value = cs->icc_ctlr_el1[bank];
871     trace_gicv3_icc_ctlr_read(gicv3_redist_affid(cs), value);
872     return value;
873 }
874 
875 static void icc_ctlr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
876                                uint64_t value)
877 {
878     GICv3CPUState *cs = icc_cs_from_env(env);
879     int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S;
880     uint64_t mask;
881 
882     trace_gicv3_icc_ctlr_write(gicv3_redist_affid(cs), value);
883 
884     /* Only CBPR and EOIMODE can be RW;
885      * for us PMHE is RAZ/WI (we don't implement 1-of-N interrupts or
886      * the asseciated priority-based routing of them);
887      * if EL3 is implemented and GICD_CTLR.DS == 0, then PMHE and CBPR are RO.
888      */
889     if (arm_feature(env, ARM_FEATURE_EL3) &&
890         ((cs->gic->gicd_ctlr & GICD_CTLR_DS) == 0)) {
891         mask = ICC_CTLR_EL1_EOIMODE;
892     } else {
893         mask = ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE;
894     }
895 
896     cs->icc_ctlr_el1[bank] &= ~mask;
897     cs->icc_ctlr_el1[bank] |= (value & mask);
898     gicv3_cpuif_update(cs);
899 }
900 
901 
902 static uint64_t icc_ctlr_el3_read(CPUARMState *env, const ARMCPRegInfo *ri)
903 {
904     GICv3CPUState *cs = icc_cs_from_env(env);
905     uint64_t value;
906 
907     value = cs->icc_ctlr_el3;
908     if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) {
909         value |= ICC_CTLR_EL3_EOIMODE_EL1NS;
910     }
911     if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) {
912         value |= ICC_CTLR_EL3_CBPR_EL1NS;
913     }
914     if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) {
915         value |= ICC_CTLR_EL3_EOIMODE_EL1S;
916     }
917     if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) {
918         value |= ICC_CTLR_EL3_CBPR_EL1S;
919     }
920 
921     trace_gicv3_icc_ctlr_el3_read(gicv3_redist_affid(cs), value);
922     return value;
923 }
924 
925 static void icc_ctlr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
926                                uint64_t value)
927 {
928     GICv3CPUState *cs = icc_cs_from_env(env);
929     uint64_t mask;
930 
931     trace_gicv3_icc_ctlr_el3_write(gicv3_redist_affid(cs), value);
932 
933     /* *_EL1NS and *_EL1S bits are aliases into the ICC_CTLR_EL1 bits. */
934     cs->icc_ctlr_el1[GICV3_NS] &= (ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE);
935     if (value & ICC_CTLR_EL3_EOIMODE_EL1NS) {
936         cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_EOIMODE;
937     }
938     if (value & ICC_CTLR_EL3_CBPR_EL1NS) {
939         cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_CBPR;
940     }
941 
942     cs->icc_ctlr_el1[GICV3_S] &= (ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE);
943     if (value & ICC_CTLR_EL3_EOIMODE_EL1S) {
944         cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_EOIMODE;
945     }
946     if (value & ICC_CTLR_EL3_CBPR_EL1S) {
947         cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_CBPR;
948     }
949 
950     /* The only bit stored in icc_ctlr_el3 which is writeable is EOIMODE_EL3: */
951     mask = ICC_CTLR_EL3_EOIMODE_EL3;
952 
953     cs->icc_ctlr_el3 &= ~mask;
954     cs->icc_ctlr_el3 |= (value & mask);
955     gicv3_cpuif_update(cs);
956 }
957 
958 static CPAccessResult gicv3_irqfiq_access(CPUARMState *env,
959                                           const ARMCPRegInfo *ri, bool isread)
960 {
961     CPAccessResult r = CP_ACCESS_OK;
962 
963     if ((env->cp15.scr_el3 & (SCR_FIQ | SCR_IRQ)) == (SCR_FIQ | SCR_IRQ)) {
964         switch (arm_current_el(env)) {
965         case 1:
966             if (arm_is_secure_below_el3(env) ||
967                 ((env->cp15.hcr_el2 & (HCR_IMO | HCR_FMO)) == 0)) {
968                 r = CP_ACCESS_TRAP_EL3;
969             }
970             break;
971         case 2:
972             r = CP_ACCESS_TRAP_EL3;
973             break;
974         case 3:
975             if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
976                 r = CP_ACCESS_TRAP_EL3;
977             }
978         default:
979             g_assert_not_reached();
980         }
981     }
982 
983     if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
984         r = CP_ACCESS_TRAP;
985     }
986     return r;
987 }
988 
989 static CPAccessResult gicv3_fiq_access(CPUARMState *env,
990                                        const ARMCPRegInfo *ri, bool isread)
991 {
992     CPAccessResult r = CP_ACCESS_OK;
993 
994     if (env->cp15.scr_el3 & SCR_FIQ) {
995         switch (arm_current_el(env)) {
996         case 1:
997             if (arm_is_secure_below_el3(env) ||
998                 ((env->cp15.hcr_el2 & HCR_FMO) == 0)) {
999                 r = CP_ACCESS_TRAP_EL3;
1000             }
1001             break;
1002         case 2:
1003             r = CP_ACCESS_TRAP_EL3;
1004             break;
1005         case 3:
1006             if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
1007                 r = CP_ACCESS_TRAP_EL3;
1008             }
1009         default:
1010             g_assert_not_reached();
1011         }
1012     }
1013 
1014     if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
1015         r = CP_ACCESS_TRAP;
1016     }
1017     return r;
1018 }
1019 
1020 static CPAccessResult gicv3_irq_access(CPUARMState *env,
1021                                        const ARMCPRegInfo *ri, bool isread)
1022 {
1023     CPAccessResult r = CP_ACCESS_OK;
1024 
1025     if (env->cp15.scr_el3 & SCR_IRQ) {
1026         switch (arm_current_el(env)) {
1027         case 1:
1028             if (arm_is_secure_below_el3(env) ||
1029                 ((env->cp15.hcr_el2 & HCR_IMO) == 0)) {
1030                 r = CP_ACCESS_TRAP_EL3;
1031             }
1032             break;
1033         case 2:
1034             r = CP_ACCESS_TRAP_EL3;
1035             break;
1036         case 3:
1037             if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
1038                 r = CP_ACCESS_TRAP_EL3;
1039             }
1040             break;
1041         default:
1042             g_assert_not_reached();
1043         }
1044     }
1045 
1046     if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
1047         r = CP_ACCESS_TRAP;
1048     }
1049     return r;
1050 }
1051 
1052 static void icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1053 {
1054     GICv3CPUState *cs = icc_cs_from_env(env);
1055 
1056     cs->icc_ctlr_el1[GICV3_S] = ICC_CTLR_EL1_A3V |
1057         (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
1058         (7 << ICC_CTLR_EL1_PRIBITS_SHIFT);
1059     cs->icc_ctlr_el1[GICV3_NS] = ICC_CTLR_EL1_A3V |
1060         (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
1061         (7 << ICC_CTLR_EL1_PRIBITS_SHIFT);
1062     cs->icc_pmr_el1 = 0;
1063     cs->icc_bpr[GICV3_G0] = GIC_MIN_BPR;
1064     cs->icc_bpr[GICV3_G1] = GIC_MIN_BPR;
1065     if (arm_feature(env, ARM_FEATURE_EL3)) {
1066         cs->icc_bpr[GICV3_G1NS] = GIC_MIN_BPR_NS;
1067     } else {
1068         cs->icc_bpr[GICV3_G1NS] = GIC_MIN_BPR;
1069     }
1070     memset(cs->icc_apr, 0, sizeof(cs->icc_apr));
1071     memset(cs->icc_igrpen, 0, sizeof(cs->icc_igrpen));
1072     cs->icc_ctlr_el3 = ICC_CTLR_EL3_NDS | ICC_CTLR_EL3_A3V |
1073         (1 << ICC_CTLR_EL3_IDBITS_SHIFT) |
1074         (7 << ICC_CTLR_EL3_PRIBITS_SHIFT);
1075 }
1076 
1077 static const ARMCPRegInfo gicv3_cpuif_reginfo[] = {
1078     { .name = "ICC_PMR_EL1", .state = ARM_CP_STATE_BOTH,
1079       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 6, .opc2 = 0,
1080       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1081       .access = PL1_RW, .accessfn = gicv3_irqfiq_access,
1082       .readfn = icc_pmr_read,
1083       .writefn = icc_pmr_write,
1084       /* We hang the whole cpu interface reset routine off here
1085        * rather than parcelling it out into one little function
1086        * per register
1087        */
1088       .resetfn = icc_reset,
1089     },
1090     { .name = "ICC_IAR0_EL1", .state = ARM_CP_STATE_BOTH,
1091       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 0,
1092       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1093       .access = PL1_R, .accessfn = gicv3_fiq_access,
1094       .readfn = icc_iar0_read,
1095     },
1096     { .name = "ICC_EOIR0_EL1", .state = ARM_CP_STATE_BOTH,
1097       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 1,
1098       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1099       .access = PL1_W, .accessfn = gicv3_fiq_access,
1100       .writefn = icc_eoir_write,
1101     },
1102     { .name = "ICC_HPPIR0_EL1", .state = ARM_CP_STATE_BOTH,
1103       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 2,
1104       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1105       .access = PL1_R, .accessfn = gicv3_fiq_access,
1106       .readfn = icc_hppir0_read,
1107     },
1108     { .name = "ICC_BPR0_EL1", .state = ARM_CP_STATE_BOTH,
1109       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 3,
1110       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1111       .access = PL1_RW, .accessfn = gicv3_fiq_access,
1112       .fieldoffset = offsetof(GICv3CPUState, icc_bpr[GICV3_G0]),
1113       .writefn = icc_bpr_write,
1114     },
1115     { .name = "ICC_AP0R0_EL1", .state = ARM_CP_STATE_BOTH,
1116       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 4,
1117       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1118       .access = PL1_RW, .accessfn = gicv3_fiq_access,
1119       .fieldoffset = offsetof(GICv3CPUState, icc_apr[GICV3_G0][0]),
1120       .writefn = icc_ap_write,
1121     },
1122     { .name = "ICC_AP0R1_EL1", .state = ARM_CP_STATE_BOTH,
1123       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 5,
1124       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1125       .access = PL1_RW, .accessfn = gicv3_fiq_access,
1126       .fieldoffset = offsetof(GICv3CPUState, icc_apr[GICV3_G0][1]),
1127       .writefn = icc_ap_write,
1128     },
1129     { .name = "ICC_AP0R2_EL1", .state = ARM_CP_STATE_BOTH,
1130       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 6,
1131       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1132       .access = PL1_RW, .accessfn = gicv3_fiq_access,
1133       .fieldoffset = offsetof(GICv3CPUState, icc_apr[GICV3_G0][2]),
1134       .writefn = icc_ap_write,
1135     },
1136     { .name = "ICC_AP0R3_EL1", .state = ARM_CP_STATE_BOTH,
1137       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 7,
1138       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1139       .access = PL1_RW, .accessfn = gicv3_fiq_access,
1140       .fieldoffset = offsetof(GICv3CPUState, icc_apr[GICV3_G0][3]),
1141       .writefn = icc_ap_write,
1142     },
1143     /* All the ICC_AP1R*_EL1 registers are banked */
1144     { .name = "ICC_AP1R0_EL1", .state = ARM_CP_STATE_BOTH,
1145       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 0,
1146       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1147       .access = PL1_RW, .accessfn = gicv3_irq_access,
1148       .readfn = icc_ap_read,
1149       .writefn = icc_ap_write,
1150     },
1151     { .name = "ICC_AP1R1_EL1", .state = ARM_CP_STATE_BOTH,
1152       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 1,
1153       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1154       .access = PL1_RW, .accessfn = gicv3_irq_access,
1155       .readfn = icc_ap_read,
1156       .writefn = icc_ap_write,
1157     },
1158     { .name = "ICC_AP1R2_EL1", .state = ARM_CP_STATE_BOTH,
1159       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 2,
1160       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1161       .access = PL1_RW, .accessfn = gicv3_irq_access,
1162       .readfn = icc_ap_read,
1163       .writefn = icc_ap_write,
1164     },
1165     { .name = "ICC_AP1R3_EL1", .state = ARM_CP_STATE_BOTH,
1166       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 3,
1167       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1168       .access = PL1_RW, .accessfn = gicv3_irq_access,
1169       .readfn = icc_ap_read,
1170       .writefn = icc_ap_write,
1171     },
1172     { .name = "ICC_DIR_EL1", .state = ARM_CP_STATE_BOTH,
1173       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 1,
1174       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1175       .access = PL1_W, .accessfn = gicv3_irqfiq_access,
1176       .writefn = icc_dir_write,
1177     },
1178     { .name = "ICC_RPR_EL1", .state = ARM_CP_STATE_BOTH,
1179       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 3,
1180       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1181       .access = PL1_R, .accessfn = gicv3_irqfiq_access,
1182       .readfn = icc_rpr_read,
1183     },
1184     { .name = "ICC_SGI1R_EL1", .state = ARM_CP_STATE_AA64,
1185       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 5,
1186       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1187       .access = PL1_W, .accessfn = gicv3_irqfiq_access,
1188       .writefn = icc_sgi1r_write,
1189     },
1190     { .name = "ICC_SGI1R",
1191       .cp = 15, .opc1 = 0, .crm = 12,
1192       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
1193       .access = PL1_W, .accessfn = gicv3_irqfiq_access,
1194       .writefn = icc_sgi1r_write,
1195     },
1196     { .name = "ICC_ASGI1R_EL1", .state = ARM_CP_STATE_AA64,
1197       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 6,
1198       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1199       .access = PL1_W, .accessfn = gicv3_irqfiq_access,
1200       .writefn = icc_asgi1r_write,
1201     },
1202     { .name = "ICC_ASGI1R",
1203       .cp = 15, .opc1 = 1, .crm = 12,
1204       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
1205       .access = PL1_W, .accessfn = gicv3_irqfiq_access,
1206       .writefn = icc_asgi1r_write,
1207     },
1208     { .name = "ICC_SGI0R_EL1", .state = ARM_CP_STATE_AA64,
1209       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 7,
1210       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1211       .access = PL1_W, .accessfn = gicv3_irqfiq_access,
1212       .writefn = icc_sgi0r_write,
1213     },
1214     { .name = "ICC_SGI0R",
1215       .cp = 15, .opc1 = 2, .crm = 12,
1216       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
1217       .access = PL1_W, .accessfn = gicv3_irqfiq_access,
1218       .writefn = icc_sgi0r_write,
1219     },
1220     { .name = "ICC_IAR1_EL1", .state = ARM_CP_STATE_BOTH,
1221       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 0,
1222       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1223       .access = PL1_R, .accessfn = gicv3_irq_access,
1224       .readfn = icc_iar1_read,
1225     },
1226     { .name = "ICC_EOIR1_EL1", .state = ARM_CP_STATE_BOTH,
1227       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 1,
1228       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1229       .access = PL1_W, .accessfn = gicv3_irq_access,
1230       .writefn = icc_eoir_write,
1231     },
1232     { .name = "ICC_HPPIR1_EL1", .state = ARM_CP_STATE_BOTH,
1233       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 2,
1234       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1235       .access = PL1_R, .accessfn = gicv3_irq_access,
1236       .readfn = icc_hppir1_read,
1237     },
1238     /* This register is banked */
1239     { .name = "ICC_BPR1_EL1", .state = ARM_CP_STATE_BOTH,
1240       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 3,
1241       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1242       .access = PL1_RW, .accessfn = gicv3_irq_access,
1243       .readfn = icc_bpr_read,
1244       .writefn = icc_bpr_write,
1245     },
1246     /* This register is banked */
1247     { .name = "ICC_CTLR_EL1", .state = ARM_CP_STATE_BOTH,
1248       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 4,
1249       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1250       .access = PL1_RW, .accessfn = gicv3_irqfiq_access,
1251       .readfn = icc_ctlr_el1_read,
1252       .writefn = icc_ctlr_el1_write,
1253     },
1254     { .name = "ICC_SRE_EL1", .state = ARM_CP_STATE_BOTH,
1255       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 5,
1256       .type = ARM_CP_NO_RAW | ARM_CP_CONST,
1257       .access = PL1_RW,
1258       /* We don't support IRQ/FIQ bypass and system registers are
1259        * always enabled, so all our bits are RAZ/WI or RAO/WI.
1260        * This register is banked but since it's constant we don't
1261        * need to do anything special.
1262        */
1263       .resetvalue = 0x7,
1264     },
1265     { .name = "ICC_IGRPEN0_EL1", .state = ARM_CP_STATE_BOTH,
1266       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 6,
1267       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1268       .access = PL1_RW, .accessfn = gicv3_fiq_access,
1269       .fieldoffset = offsetof(GICv3CPUState, icc_igrpen[GICV3_G0]),
1270       .writefn = icc_igrpen_write,
1271     },
1272     /* This register is banked */
1273     { .name = "ICC_IGRPEN1_EL1", .state = ARM_CP_STATE_BOTH,
1274       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 7,
1275       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1276       .access = PL1_RW, .accessfn = gicv3_irq_access,
1277       .readfn = icc_igrpen_read,
1278       .writefn = icc_igrpen_write,
1279     },
1280     { .name = "ICC_SRE_EL2", .state = ARM_CP_STATE_BOTH,
1281       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 5,
1282       .type = ARM_CP_NO_RAW | ARM_CP_CONST,
1283       .access = PL2_RW,
1284       /* We don't support IRQ/FIQ bypass and system registers are
1285        * always enabled, so all our bits are RAZ/WI or RAO/WI.
1286        */
1287       .resetvalue = 0xf,
1288     },
1289     { .name = "ICC_CTLR_EL3", .state = ARM_CP_STATE_BOTH,
1290       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 4,
1291       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1292       .access = PL3_RW,
1293       .fieldoffset = offsetof(GICv3CPUState, icc_ctlr_el3),
1294       .readfn = icc_ctlr_el3_read,
1295       .writefn = icc_ctlr_el3_write,
1296     },
1297     { .name = "ICC_SRE_EL3", .state = ARM_CP_STATE_BOTH,
1298       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 5,
1299       .type = ARM_CP_NO_RAW | ARM_CP_CONST,
1300       .access = PL3_RW,
1301       /* We don't support IRQ/FIQ bypass and system registers are
1302        * always enabled, so all our bits are RAZ/WI or RAO/WI.
1303        */
1304       .resetvalue = 0xf,
1305     },
1306     { .name = "ICC_IGRPEN1_EL3", .state = ARM_CP_STATE_BOTH,
1307       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 7,
1308       .type = ARM_CP_IO | ARM_CP_NO_RAW,
1309       .access = PL3_RW,
1310       .readfn = icc_igrpen1_el3_read,
1311       .writefn = icc_igrpen1_el3_write,
1312     },
1313     REGINFO_SENTINEL
1314 };
1315 
1316 static void gicv3_cpuif_el_change_hook(ARMCPU *cpu, void *opaque)
1317 {
1318     GICv3CPUState *cs = opaque;
1319 
1320     gicv3_cpuif_update(cs);
1321 }
1322 
1323 void gicv3_init_cpuif(GICv3State *s)
1324 {
1325     /* Called from the GICv3 realize function; register our system
1326      * registers with the CPU
1327      */
1328     int i;
1329 
1330     for (i = 0; i < s->num_cpu; i++) {
1331         ARMCPU *cpu = ARM_CPU(qemu_get_cpu(i));
1332         GICv3CPUState *cs = &s->cpu[i];
1333 
1334         /* Note that we can't just use the GICv3CPUState as an opaque pointer
1335          * in define_arm_cp_regs_with_opaque(), because when we're called back
1336          * it might be with code translated by CPU 0 but run by CPU 1, in
1337          * which case we'd get the wrong value.
1338          * So instead we define the regs with no ri->opaque info, and
1339          * get back to the GICv3CPUState from the ARMCPU by reading back
1340          * the opaque pointer from the el_change_hook, which we're going
1341          * to need to register anyway.
1342          */
1343         define_arm_cp_regs(cpu, gicv3_cpuif_reginfo);
1344         arm_register_el_change_hook(cpu, gicv3_cpuif_el_change_hook, cs);
1345     }
1346 }
1347