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