xref: /openbmc/qemu/hw/intc/arm_gicv3_cpuif.c (revision de799beb)
1 /*
2  * ARM Generic Interrupt Controller v3 (emulation)
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 "qemu/bitops.h"
17 #include "qemu/log.h"
18 #include "qemu/main-loop.h"
19 #include "trace.h"
20 #include "gicv3_internal.h"
21 #include "hw/irq.h"
22 #include "cpu.h"
23 #include "target/arm/cpregs.h"
24 
25 /*
26  * Special case return value from hppvi_index(); must be larger than
27  * the architecturally maximum possible list register index (which is 15)
28  */
29 #define HPPVI_INDEX_VLPI 16
30 
31 static GICv3CPUState *icc_cs_from_env(CPUARMState *env)
32 {
33     return env->gicv3state;
34 }
35 
36 static bool gicv3_use_ns_bank(CPUARMState *env)
37 {
38     /* Return true if we should use the NonSecure bank for a banked GIC
39      * CPU interface register. Note that this differs from the
40      * access_secure_reg() function because GICv3 banked registers are
41      * banked even for AArch64, unlike the other CPU system registers.
42      */
43     return !arm_is_secure_below_el3(env);
44 }
45 
46 /* The minimum BPR for the virtual interface is a configurable property */
47 static inline int icv_min_vbpr(GICv3CPUState *cs)
48 {
49     return 7 - cs->vprebits;
50 }
51 
52 static inline int ich_num_aprs(GICv3CPUState *cs)
53 {
54     /* Return the number of virtual APR registers (1, 2, or 4) */
55     int aprmax = 1 << (cs->vprebits - 5);
56     assert(aprmax <= ARRAY_SIZE(cs->ich_apr[0]));
57     return aprmax;
58 }
59 
60 /* Simple accessor functions for LR fields */
61 static uint32_t ich_lr_vintid(uint64_t lr)
62 {
63     return extract64(lr, ICH_LR_EL2_VINTID_SHIFT, ICH_LR_EL2_VINTID_LENGTH);
64 }
65 
66 static uint32_t ich_lr_pintid(uint64_t lr)
67 {
68     return extract64(lr, ICH_LR_EL2_PINTID_SHIFT, ICH_LR_EL2_PINTID_LENGTH);
69 }
70 
71 static uint32_t ich_lr_prio(uint64_t lr)
72 {
73     return extract64(lr, ICH_LR_EL2_PRIORITY_SHIFT, ICH_LR_EL2_PRIORITY_LENGTH);
74 }
75 
76 static int ich_lr_state(uint64_t lr)
77 {
78     return extract64(lr, ICH_LR_EL2_STATE_SHIFT, ICH_LR_EL2_STATE_LENGTH);
79 }
80 
81 static bool icv_access(CPUARMState *env, int hcr_flags)
82 {
83     /* Return true if this ICC_ register access should really be
84      * directed to an ICV_ access. hcr_flags is a mask of
85      * HCR_EL2 bits to check: we treat this as an ICV_ access
86      * if we are in NS EL1 and at least one of the specified
87      * HCR_EL2 bits is set.
88      *
89      * ICV registers fall into four categories:
90      *  * access if NS EL1 and HCR_EL2.FMO == 1:
91      *    all ICV regs with '0' in their name
92      *  * access if NS EL1 and HCR_EL2.IMO == 1:
93      *    all ICV regs with '1' in their name
94      *  * access if NS EL1 and either IMO or FMO == 1:
95      *    CTLR, DIR, PMR, RPR
96      */
97     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
98     bool flagmatch = hcr_el2 & hcr_flags & (HCR_IMO | HCR_FMO);
99 
100     return flagmatch && arm_current_el(env) == 1
101         && !arm_is_secure_below_el3(env);
102 }
103 
104 static int read_vbpr(GICv3CPUState *cs, int grp)
105 {
106     /* Read VBPR value out of the VMCR field (caller must handle
107      * VCBPR effects if required)
108      */
109     if (grp == GICV3_G0) {
110         return extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR0_SHIFT,
111                      ICH_VMCR_EL2_VBPR0_LENGTH);
112     } else {
113         return extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR1_SHIFT,
114                          ICH_VMCR_EL2_VBPR1_LENGTH);
115     }
116 }
117 
118 static void write_vbpr(GICv3CPUState *cs, int grp, int value)
119 {
120     /* Write new VBPR1 value, handling the "writing a value less than
121      * the minimum sets it to the minimum" semantics.
122      */
123     int min = icv_min_vbpr(cs);
124 
125     if (grp != GICV3_G0) {
126         min++;
127     }
128 
129     value = MAX(value, min);
130 
131     if (grp == GICV3_G0) {
132         cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR0_SHIFT,
133                                      ICH_VMCR_EL2_VBPR0_LENGTH, value);
134     } else {
135         cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR1_SHIFT,
136                                      ICH_VMCR_EL2_VBPR1_LENGTH, value);
137     }
138 }
139 
140 static uint32_t icv_fullprio_mask(GICv3CPUState *cs)
141 {
142     /* Return a mask word which clears the unimplemented priority bits
143      * from a priority value for a virtual interrupt. (Not to be confused
144      * with the group priority, whose mask depends on the value of VBPR
145      * for the interrupt group.)
146      */
147     return ~0U << (8 - cs->vpribits);
148 }
149 
150 static int ich_highest_active_virt_prio(GICv3CPUState *cs)
151 {
152     /* Calculate the current running priority based on the set bits
153      * in the ICH Active Priority Registers.
154      */
155     int i;
156     int aprmax = ich_num_aprs(cs);
157 
158     for (i = 0; i < aprmax; i++) {
159         uint32_t apr = cs->ich_apr[GICV3_G0][i] |
160             cs->ich_apr[GICV3_G1NS][i];
161 
162         if (!apr) {
163             continue;
164         }
165         return (i * 32 + ctz32(apr)) << (icv_min_vbpr(cs) + 1);
166     }
167     /* No current active interrupts: return idle priority */
168     return 0xff;
169 }
170 
171 static int hppvi_index(GICv3CPUState *cs)
172 {
173     /*
174      * Return the list register index of the highest priority pending
175      * virtual interrupt, as per the HighestPriorityVirtualInterrupt
176      * pseudocode. If no pending virtual interrupts, return -1.
177      * If the highest priority pending virtual interrupt is a vLPI,
178      * return HPPVI_INDEX_VLPI.
179      * (The pseudocode handles checking whether the vLPI is higher
180      * priority than the highest priority list register at every
181      * callsite of HighestPriorityVirtualInterrupt; we check it here.)
182      */
183     ARMCPU *cpu = ARM_CPU(cs->cpu);
184     CPUARMState *env = &cpu->env;
185     int idx = -1;
186     int i;
187     /* Note that a list register entry with a priority of 0xff will
188      * never be reported by this function; this is the architecturally
189      * correct behaviour.
190      */
191     int prio = 0xff;
192 
193     if (!(cs->ich_vmcr_el2 & (ICH_VMCR_EL2_VENG0 | ICH_VMCR_EL2_VENG1))) {
194         /* Both groups disabled, definitely nothing to do */
195         return idx;
196     }
197 
198     for (i = 0; i < cs->num_list_regs; i++) {
199         uint64_t lr = cs->ich_lr_el2[i];
200         int thisprio;
201 
202         if (ich_lr_state(lr) != ICH_LR_EL2_STATE_PENDING) {
203             /* Not Pending */
204             continue;
205         }
206 
207         /* Ignore interrupts if relevant group enable not set */
208         if (lr & ICH_LR_EL2_GROUP) {
209             if (!(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
210                 continue;
211             }
212         } else {
213             if (!(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG0)) {
214                 continue;
215             }
216         }
217 
218         thisprio = ich_lr_prio(lr);
219 
220         if (thisprio < prio) {
221             prio = thisprio;
222             idx = i;
223         }
224     }
225 
226     /*
227      * "no pending vLPI" is indicated with prio = 0xff, which always
228      * fails the priority check here. vLPIs are only considered
229      * when we are in Non-Secure state.
230      */
231     if (cs->hppvlpi.prio < prio && !arm_is_secure(env)) {
232         if (cs->hppvlpi.grp == GICV3_G0) {
233             if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG0) {
234                 return HPPVI_INDEX_VLPI;
235             }
236         } else {
237             if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1) {
238                 return HPPVI_INDEX_VLPI;
239             }
240         }
241     }
242 
243     return idx;
244 }
245 
246 static uint32_t icv_gprio_mask(GICv3CPUState *cs, int group)
247 {
248     /* Return a mask word which clears the subpriority bits from
249      * a priority value for a virtual interrupt in the specified group.
250      * This depends on the VBPR value.
251      * If using VBPR0 then:
252      *  a BPR of 0 means the group priority bits are [7:1];
253      *  a BPR of 1 means they are [7:2], and so on down to
254      *  a BPR of 7 meaning no group priority bits at all.
255      * If using VBPR1 then:
256      *  a BPR of 0 is impossible (the minimum value is 1)
257      *  a BPR of 1 means the group priority bits are [7:1];
258      *  a BPR of 2 means they are [7:2], and so on down to
259      *  a BPR of 7 meaning the group priority is [7].
260      *
261      * Which BPR to use depends on the group of the interrupt and
262      * the current ICH_VMCR_EL2.VCBPR settings.
263      *
264      * This corresponds to the VGroupBits() pseudocode.
265      */
266     int bpr;
267 
268     if (group == GICV3_G1NS && cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR) {
269         group = GICV3_G0;
270     }
271 
272     bpr = read_vbpr(cs, group);
273     if (group == GICV3_G1NS) {
274         assert(bpr > 0);
275         bpr--;
276     }
277 
278     return ~0U << (bpr + 1);
279 }
280 
281 static bool icv_hppi_can_preempt(GICv3CPUState *cs, uint64_t lr)
282 {
283     /* Return true if we can signal this virtual interrupt defined by
284      * the given list register value; see the pseudocode functions
285      * CanSignalVirtualInterrupt and CanSignalVirtualInt.
286      * Compare also icc_hppi_can_preempt() which is the non-virtual
287      * equivalent of these checks.
288      */
289     int grp;
290     uint32_t mask, prio, rprio, vpmr;
291 
292     if (!(cs->ich_hcr_el2 & ICH_HCR_EL2_EN)) {
293         /* Virtual interface disabled */
294         return false;
295     }
296 
297     /* We don't need to check that this LR is in Pending state because
298      * that has already been done in hppvi_index().
299      */
300 
301     prio = ich_lr_prio(lr);
302     vpmr = extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
303                      ICH_VMCR_EL2_VPMR_LENGTH);
304 
305     if (prio >= vpmr) {
306         /* Priority mask masks this interrupt */
307         return false;
308     }
309 
310     rprio = ich_highest_active_virt_prio(cs);
311     if (rprio == 0xff) {
312         /* No running interrupt so we can preempt */
313         return true;
314     }
315 
316     grp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
317 
318     mask = icv_gprio_mask(cs, grp);
319 
320     /* We only preempt a running interrupt if the pending interrupt's
321      * group priority is sufficient (the subpriorities are not considered).
322      */
323     if ((prio & mask) < (rprio & mask)) {
324         return true;
325     }
326 
327     return false;
328 }
329 
330 static bool icv_hppvlpi_can_preempt(GICv3CPUState *cs)
331 {
332     /*
333      * Return true if we can signal the highest priority pending vLPI.
334      * We can assume we're Non-secure because hppvi_index() already
335      * tested for that.
336      */
337     uint32_t mask, rprio, vpmr;
338 
339     if (!(cs->ich_hcr_el2 & ICH_HCR_EL2_EN)) {
340         /* Virtual interface disabled */
341         return false;
342     }
343 
344     vpmr = extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
345                      ICH_VMCR_EL2_VPMR_LENGTH);
346 
347     if (cs->hppvlpi.prio >= vpmr) {
348         /* Priority mask masks this interrupt */
349         return false;
350     }
351 
352     rprio = ich_highest_active_virt_prio(cs);
353     if (rprio == 0xff) {
354         /* No running interrupt so we can preempt */
355         return true;
356     }
357 
358     mask = icv_gprio_mask(cs, cs->hppvlpi.grp);
359 
360     /*
361      * We only preempt a running interrupt if the pending interrupt's
362      * group priority is sufficient (the subpriorities are not considered).
363      */
364     if ((cs->hppvlpi.prio & mask) < (rprio & mask)) {
365         return true;
366     }
367 
368     return false;
369 }
370 
371 static uint32_t eoi_maintenance_interrupt_state(GICv3CPUState *cs,
372                                                 uint32_t *misr)
373 {
374     /* Return a set of bits indicating the EOI maintenance interrupt status
375      * for each list register. The EOI maintenance interrupt status is
376      * 1 if LR.State == 0 && LR.HW == 0 && LR.EOI == 1
377      * (see the GICv3 spec for the ICH_EISR_EL2 register).
378      * If misr is not NULL then we should also collect the information
379      * about the MISR.EOI, MISR.NP and MISR.U bits.
380      */
381     uint32_t value = 0;
382     int validcount = 0;
383     bool seenpending = false;
384     int i;
385 
386     for (i = 0; i < cs->num_list_regs; i++) {
387         uint64_t lr = cs->ich_lr_el2[i];
388 
389         if ((lr & (ICH_LR_EL2_STATE_MASK | ICH_LR_EL2_HW | ICH_LR_EL2_EOI))
390             == ICH_LR_EL2_EOI) {
391             value |= (1 << i);
392         }
393         if ((lr & ICH_LR_EL2_STATE_MASK)) {
394             validcount++;
395         }
396         if (ich_lr_state(lr) == ICH_LR_EL2_STATE_PENDING) {
397             seenpending = true;
398         }
399     }
400 
401     if (misr) {
402         if (validcount < 2 && (cs->ich_hcr_el2 & ICH_HCR_EL2_UIE)) {
403             *misr |= ICH_MISR_EL2_U;
404         }
405         if (!seenpending && (cs->ich_hcr_el2 & ICH_HCR_EL2_NPIE)) {
406             *misr |= ICH_MISR_EL2_NP;
407         }
408         if (value) {
409             *misr |= ICH_MISR_EL2_EOI;
410         }
411     }
412     return value;
413 }
414 
415 static uint32_t maintenance_interrupt_state(GICv3CPUState *cs)
416 {
417     /* Return a set of bits indicating the maintenance interrupt status
418      * (as seen in the ICH_MISR_EL2 register).
419      */
420     uint32_t value = 0;
421 
422     /* Scan list registers and fill in the U, NP and EOI bits */
423     eoi_maintenance_interrupt_state(cs, &value);
424 
425     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_LRENPIE) &&
426         (cs->ich_hcr_el2 & ICH_HCR_EL2_EOICOUNT_MASK)) {
427         value |= ICH_MISR_EL2_LRENP;
428     }
429 
430     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP0EIE) &&
431         (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG0)) {
432         value |= ICH_MISR_EL2_VGRP0E;
433     }
434 
435     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP0DIE) &&
436         !(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
437         value |= ICH_MISR_EL2_VGRP0D;
438     }
439     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP1EIE) &&
440         (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
441         value |= ICH_MISR_EL2_VGRP1E;
442     }
443 
444     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP1DIE) &&
445         !(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
446         value |= ICH_MISR_EL2_VGRP1D;
447     }
448 
449     return value;
450 }
451 
452 void gicv3_cpuif_virt_irq_fiq_update(GICv3CPUState *cs)
453 {
454     /*
455      * Tell the CPU about any pending virtual interrupts.
456      * This should only be called for changes that affect the
457      * vIRQ and vFIQ status and do not change the maintenance
458      * interrupt status. This means that unlike gicv3_cpuif_virt_update()
459      * this function won't recursively call back into the GIC code.
460      * The main use of this is when the redistributor has changed the
461      * highest priority pending virtual LPI.
462      */
463     int idx;
464     int irqlevel = 0;
465     int fiqlevel = 0;
466 
467     idx = hppvi_index(cs);
468     trace_gicv3_cpuif_virt_update(gicv3_redist_affid(cs), idx,
469                                   cs->hppvlpi.irq, cs->hppvlpi.grp,
470                                   cs->hppvlpi.prio);
471     if (idx == HPPVI_INDEX_VLPI) {
472         if (icv_hppvlpi_can_preempt(cs)) {
473             if (cs->hppvlpi.grp == GICV3_G0) {
474                 fiqlevel = 1;
475             } else {
476                 irqlevel = 1;
477             }
478         }
479     } else if (idx >= 0) {
480         uint64_t lr = cs->ich_lr_el2[idx];
481 
482         if (icv_hppi_can_preempt(cs, lr)) {
483             /* Virtual interrupts are simple: G0 are always FIQ, and G1 IRQ */
484             if (lr & ICH_LR_EL2_GROUP) {
485                 irqlevel = 1;
486             } else {
487                 fiqlevel = 1;
488             }
489         }
490     }
491 
492     trace_gicv3_cpuif_virt_set_irqs(gicv3_redist_affid(cs), fiqlevel, irqlevel);
493     qemu_set_irq(cs->parent_vfiq, fiqlevel);
494     qemu_set_irq(cs->parent_virq, irqlevel);
495 }
496 
497 static void gicv3_cpuif_virt_update(GICv3CPUState *cs)
498 {
499     /*
500      * Tell the CPU about any pending virtual interrupts or
501      * maintenance interrupts, following a change to the state
502      * of the CPU interface relevant to virtual interrupts.
503      *
504      * CAUTION: this function will call qemu_set_irq() on the
505      * CPU maintenance IRQ line, which is typically wired up
506      * to the GIC as a per-CPU interrupt. This means that it
507      * will recursively call back into the GIC code via
508      * gicv3_redist_set_irq() and thus into the CPU interface code's
509      * gicv3_cpuif_update(). It is therefore important that this
510      * function is only called as the final action of a CPU interface
511      * register write implementation, after all the GIC state
512      * fields have been updated. gicv3_cpuif_update() also must
513      * not cause this function to be called, but that happens
514      * naturally as a result of there being no architectural
515      * linkage between the physical and virtual GIC logic.
516      */
517     ARMCPU *cpu = ARM_CPU(cs->cpu);
518     int maintlevel = 0;
519 
520     gicv3_cpuif_virt_irq_fiq_update(cs);
521 
522     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_EN) &&
523         maintenance_interrupt_state(cs) != 0) {
524         maintlevel = 1;
525     }
526 
527     trace_gicv3_cpuif_virt_set_maint_irq(gicv3_redist_affid(cs), maintlevel);
528     qemu_set_irq(cpu->gicv3_maintenance_interrupt, maintlevel);
529 }
530 
531 static uint64_t icv_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
532 {
533     GICv3CPUState *cs = icc_cs_from_env(env);
534     int regno = ri->opc2 & 3;
535     int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
536     uint64_t value = cs->ich_apr[grp][regno];
537 
538     trace_gicv3_icv_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
539     return value;
540 }
541 
542 static void icv_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
543                          uint64_t value)
544 {
545     GICv3CPUState *cs = icc_cs_from_env(env);
546     int regno = ri->opc2 & 3;
547     int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
548 
549     trace_gicv3_icv_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
550 
551     cs->ich_apr[grp][regno] = value & 0xFFFFFFFFU;
552 
553     gicv3_cpuif_virt_irq_fiq_update(cs);
554     return;
555 }
556 
557 static uint64_t icv_bpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
558 {
559     GICv3CPUState *cs = icc_cs_from_env(env);
560     int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1NS;
561     uint64_t bpr;
562     bool satinc = false;
563 
564     if (grp == GICV3_G1NS && (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR)) {
565         /* reads return bpr0 + 1 saturated to 7, writes ignored */
566         grp = GICV3_G0;
567         satinc = true;
568     }
569 
570     bpr = read_vbpr(cs, grp);
571 
572     if (satinc) {
573         bpr++;
574         bpr = MIN(bpr, 7);
575     }
576 
577     trace_gicv3_icv_bpr_read(ri->crm == 8 ? 0 : 1, gicv3_redist_affid(cs), bpr);
578 
579     return bpr;
580 }
581 
582 static void icv_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
583                           uint64_t value)
584 {
585     GICv3CPUState *cs = icc_cs_from_env(env);
586     int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1NS;
587 
588     trace_gicv3_icv_bpr_write(ri->crm == 8 ? 0 : 1,
589                               gicv3_redist_affid(cs), value);
590 
591     if (grp == GICV3_G1NS && (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR)) {
592         /* reads return bpr0 + 1 saturated to 7, writes ignored */
593         return;
594     }
595 
596     write_vbpr(cs, grp, value);
597 
598     gicv3_cpuif_virt_irq_fiq_update(cs);
599 }
600 
601 static uint64_t icv_pmr_read(CPUARMState *env, const ARMCPRegInfo *ri)
602 {
603     GICv3CPUState *cs = icc_cs_from_env(env);
604     uint64_t value;
605 
606     value = extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
607                       ICH_VMCR_EL2_VPMR_LENGTH);
608 
609     trace_gicv3_icv_pmr_read(gicv3_redist_affid(cs), value);
610     return value;
611 }
612 
613 static void icv_pmr_write(CPUARMState *env, const ARMCPRegInfo *ri,
614                           uint64_t value)
615 {
616     GICv3CPUState *cs = icc_cs_from_env(env);
617 
618     trace_gicv3_icv_pmr_write(gicv3_redist_affid(cs), value);
619 
620     value &= icv_fullprio_mask(cs);
621 
622     cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
623                                  ICH_VMCR_EL2_VPMR_LENGTH, value);
624 
625     gicv3_cpuif_virt_irq_fiq_update(cs);
626 }
627 
628 static uint64_t icv_igrpen_read(CPUARMState *env, const ARMCPRegInfo *ri)
629 {
630     GICv3CPUState *cs = icc_cs_from_env(env);
631     int enbit;
632     uint64_t value;
633 
634     enbit = ri->opc2 & 1 ? ICH_VMCR_EL2_VENG1_SHIFT : ICH_VMCR_EL2_VENG0_SHIFT;
635     value = extract64(cs->ich_vmcr_el2, enbit, 1);
636 
637     trace_gicv3_icv_igrpen_read(ri->opc2 & 1 ? 1 : 0,
638                                 gicv3_redist_affid(cs), value);
639     return value;
640 }
641 
642 static void icv_igrpen_write(CPUARMState *env, const ARMCPRegInfo *ri,
643                              uint64_t value)
644 {
645     GICv3CPUState *cs = icc_cs_from_env(env);
646     int enbit;
647 
648     trace_gicv3_icv_igrpen_write(ri->opc2 & 1 ? 1 : 0,
649                                  gicv3_redist_affid(cs), value);
650 
651     enbit = ri->opc2 & 1 ? ICH_VMCR_EL2_VENG1_SHIFT : ICH_VMCR_EL2_VENG0_SHIFT;
652 
653     cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, enbit, 1, value);
654     gicv3_cpuif_virt_update(cs);
655 }
656 
657 static uint64_t icv_ctlr_read(CPUARMState *env, const ARMCPRegInfo *ri)
658 {
659     GICv3CPUState *cs = icc_cs_from_env(env);
660     uint64_t value;
661 
662     /* Note that the fixed fields here (A3V, SEIS, IDbits, PRIbits)
663      * should match the ones reported in ich_vtr_read().
664      */
665     value = ICC_CTLR_EL1_A3V | (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
666         ((cs->vpribits - 1) << ICC_CTLR_EL1_PRIBITS_SHIFT);
667 
668     if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VEOIM) {
669         value |= ICC_CTLR_EL1_EOIMODE;
670     }
671 
672     if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR) {
673         value |= ICC_CTLR_EL1_CBPR;
674     }
675 
676     trace_gicv3_icv_ctlr_read(gicv3_redist_affid(cs), value);
677     return value;
678 }
679 
680 static void icv_ctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
681                                uint64_t value)
682 {
683     GICv3CPUState *cs = icc_cs_from_env(env);
684 
685     trace_gicv3_icv_ctlr_write(gicv3_redist_affid(cs), value);
686 
687     cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VCBPR_SHIFT,
688                                  1, value & ICC_CTLR_EL1_CBPR ? 1 : 0);
689     cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VEOIM_SHIFT,
690                                  1, value & ICC_CTLR_EL1_EOIMODE ? 1 : 0);
691 
692     gicv3_cpuif_virt_irq_fiq_update(cs);
693 }
694 
695 static uint64_t icv_rpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
696 {
697     GICv3CPUState *cs = icc_cs_from_env(env);
698     int prio = ich_highest_active_virt_prio(cs);
699 
700     trace_gicv3_icv_rpr_read(gicv3_redist_affid(cs), prio);
701     return prio;
702 }
703 
704 static uint64_t icv_hppir_read(CPUARMState *env, const ARMCPRegInfo *ri)
705 {
706     GICv3CPUState *cs = icc_cs_from_env(env);
707     int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
708     int idx = hppvi_index(cs);
709     uint64_t value = INTID_SPURIOUS;
710 
711     if (idx == HPPVI_INDEX_VLPI) {
712         if (cs->hppvlpi.grp == grp) {
713             value = cs->hppvlpi.irq;
714         }
715     } else if (idx >= 0) {
716         uint64_t lr = cs->ich_lr_el2[idx];
717         int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
718 
719         if (grp == thisgrp) {
720             value = ich_lr_vintid(lr);
721         }
722     }
723 
724     trace_gicv3_icv_hppir_read(ri->crm == 8 ? 0 : 1,
725                                gicv3_redist_affid(cs), value);
726     return value;
727 }
728 
729 static void icv_activate_irq(GICv3CPUState *cs, int idx, int grp)
730 {
731     /* Activate the interrupt in the specified list register
732      * by moving it from Pending to Active state, and update the
733      * Active Priority Registers.
734      */
735     uint32_t mask = icv_gprio_mask(cs, grp);
736     int prio = ich_lr_prio(cs->ich_lr_el2[idx]) & mask;
737     int aprbit = prio >> (8 - cs->vprebits);
738     int regno = aprbit / 32;
739     int regbit = aprbit % 32;
740 
741     cs->ich_lr_el2[idx] &= ~ICH_LR_EL2_STATE_PENDING_BIT;
742     cs->ich_lr_el2[idx] |= ICH_LR_EL2_STATE_ACTIVE_BIT;
743     cs->ich_apr[grp][regno] |= (1 << regbit);
744 }
745 
746 static void icv_activate_vlpi(GICv3CPUState *cs)
747 {
748     uint32_t mask = icv_gprio_mask(cs, cs->hppvlpi.grp);
749     int prio = cs->hppvlpi.prio & mask;
750     int aprbit = prio >> (8 - cs->vprebits);
751     int regno = aprbit / 32;
752     int regbit = aprbit % 32;
753 
754     cs->ich_apr[cs->hppvlpi.grp][regno] |= (1 << regbit);
755     gicv3_redist_vlpi_pending(cs, cs->hppvlpi.irq, 0);
756 }
757 
758 static uint64_t icv_iar_read(CPUARMState *env, const ARMCPRegInfo *ri)
759 {
760     GICv3CPUState *cs = icc_cs_from_env(env);
761     int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
762     int idx = hppvi_index(cs);
763     uint64_t intid = INTID_SPURIOUS;
764 
765     if (idx == HPPVI_INDEX_VLPI) {
766         if (cs->hppvlpi.grp == grp && icv_hppvlpi_can_preempt(cs)) {
767             intid = cs->hppvlpi.irq;
768             icv_activate_vlpi(cs);
769         }
770     } else if (idx >= 0) {
771         uint64_t lr = cs->ich_lr_el2[idx];
772         int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
773 
774         if (thisgrp == grp && icv_hppi_can_preempt(cs, lr)) {
775             intid = ich_lr_vintid(lr);
776             if (!gicv3_intid_is_special(intid)) {
777                 icv_activate_irq(cs, idx, grp);
778             } else {
779                 /* Interrupt goes from Pending to Invalid */
780                 cs->ich_lr_el2[idx] &= ~ICH_LR_EL2_STATE_PENDING_BIT;
781                 /* We will now return the (bogus) ID from the list register,
782                  * as per the pseudocode.
783                  */
784             }
785         }
786     }
787 
788     trace_gicv3_icv_iar_read(ri->crm == 8 ? 0 : 1,
789                              gicv3_redist_affid(cs), intid);
790 
791     gicv3_cpuif_virt_update(cs);
792 
793     return intid;
794 }
795 
796 static uint32_t icc_fullprio_mask(GICv3CPUState *cs)
797 {
798     /*
799      * Return a mask word which clears the unimplemented priority bits
800      * from a priority value for a physical interrupt. (Not to be confused
801      * with the group priority, whose mask depends on the value of BPR
802      * for the interrupt group.)
803      */
804     return ~0U << (8 - cs->pribits);
805 }
806 
807 static inline int icc_min_bpr(GICv3CPUState *cs)
808 {
809     /* The minimum BPR for the physical interface. */
810     return 7 - cs->prebits;
811 }
812 
813 static inline int icc_min_bpr_ns(GICv3CPUState *cs)
814 {
815     return icc_min_bpr(cs) + 1;
816 }
817 
818 static inline int icc_num_aprs(GICv3CPUState *cs)
819 {
820     /* Return the number of APR registers (1, 2, or 4) */
821     int aprmax = 1 << MAX(cs->prebits - 5, 0);
822     assert(aprmax <= ARRAY_SIZE(cs->icc_apr[0]));
823     return aprmax;
824 }
825 
826 static int icc_highest_active_prio(GICv3CPUState *cs)
827 {
828     /* Calculate the current running priority based on the set bits
829      * in the Active Priority Registers.
830      */
831     int i;
832 
833     for (i = 0; i < icc_num_aprs(cs); i++) {
834         uint32_t apr = cs->icc_apr[GICV3_G0][i] |
835             cs->icc_apr[GICV3_G1][i] | cs->icc_apr[GICV3_G1NS][i];
836 
837         if (!apr) {
838             continue;
839         }
840         return (i * 32 + ctz32(apr)) << (icc_min_bpr(cs) + 1);
841     }
842     /* No current active interrupts: return idle priority */
843     return 0xff;
844 }
845 
846 static uint32_t icc_gprio_mask(GICv3CPUState *cs, int group)
847 {
848     /* Return a mask word which clears the subpriority bits from
849      * a priority value for an interrupt in the specified group.
850      * This depends on the BPR value. For CBPR0 (S or NS):
851      *  a BPR of 0 means the group priority bits are [7:1];
852      *  a BPR of 1 means they are [7:2], and so on down to
853      *  a BPR of 7 meaning no group priority bits at all.
854      * For CBPR1 NS:
855      *  a BPR of 0 is impossible (the minimum value is 1)
856      *  a BPR of 1 means the group priority bits are [7:1];
857      *  a BPR of 2 means they are [7:2], and so on down to
858      *  a BPR of 7 meaning the group priority is [7].
859      *
860      * Which BPR to use depends on the group of the interrupt and
861      * the current ICC_CTLR.CBPR settings.
862      *
863      * This corresponds to the GroupBits() pseudocode.
864      */
865     int bpr;
866 
867     if ((group == GICV3_G1 && cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR) ||
868         (group == GICV3_G1NS &&
869          cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
870         group = GICV3_G0;
871     }
872 
873     bpr = cs->icc_bpr[group] & 7;
874 
875     if (group == GICV3_G1NS) {
876         assert(bpr > 0);
877         bpr--;
878     }
879 
880     return ~0U << (bpr + 1);
881 }
882 
883 static bool icc_no_enabled_hppi(GICv3CPUState *cs)
884 {
885     /* Return true if there is no pending interrupt, or the
886      * highest priority pending interrupt is in a group which has been
887      * disabled at the CPU interface by the ICC_IGRPEN* register enable bits.
888      */
889     return cs->hppi.prio == 0xff || (cs->icc_igrpen[cs->hppi.grp] == 0);
890 }
891 
892 static bool icc_hppi_can_preempt(GICv3CPUState *cs)
893 {
894     /* Return true if we have a pending interrupt of sufficient
895      * priority to preempt.
896      */
897     int rprio;
898     uint32_t mask;
899 
900     if (icc_no_enabled_hppi(cs)) {
901         return false;
902     }
903 
904     if (cs->hppi.prio >= cs->icc_pmr_el1) {
905         /* Priority mask masks this interrupt */
906         return false;
907     }
908 
909     rprio = icc_highest_active_prio(cs);
910     if (rprio == 0xff) {
911         /* No currently running interrupt so we can preempt */
912         return true;
913     }
914 
915     mask = icc_gprio_mask(cs, cs->hppi.grp);
916 
917     /* We only preempt a running interrupt if the pending interrupt's
918      * group priority is sufficient (the subpriorities are not considered).
919      */
920     if ((cs->hppi.prio & mask) < (rprio & mask)) {
921         return true;
922     }
923 
924     return false;
925 }
926 
927 void gicv3_cpuif_update(GICv3CPUState *cs)
928 {
929     /* Tell the CPU about its highest priority pending interrupt */
930     int irqlevel = 0;
931     int fiqlevel = 0;
932     ARMCPU *cpu = ARM_CPU(cs->cpu);
933     CPUARMState *env = &cpu->env;
934 
935     g_assert(qemu_mutex_iothread_locked());
936 
937     trace_gicv3_cpuif_update(gicv3_redist_affid(cs), cs->hppi.irq,
938                              cs->hppi.grp, cs->hppi.prio);
939 
940     if (cs->hppi.grp == GICV3_G1 && !arm_feature(env, ARM_FEATURE_EL3)) {
941         /* If a Security-enabled GIC sends a G1S interrupt to a
942          * Security-disabled CPU, we must treat it as if it were G0.
943          */
944         cs->hppi.grp = GICV3_G0;
945     }
946 
947     if (icc_hppi_can_preempt(cs)) {
948         /* We have an interrupt: should we signal it as IRQ or FIQ?
949          * This is described in the GICv3 spec section 4.6.2.
950          */
951         bool isfiq;
952 
953         switch (cs->hppi.grp) {
954         case GICV3_G0:
955             isfiq = true;
956             break;
957         case GICV3_G1:
958             isfiq = (!arm_is_secure(env) ||
959                      (arm_current_el(env) == 3 && arm_el_is_aa64(env, 3)));
960             break;
961         case GICV3_G1NS:
962             isfiq = arm_is_secure(env);
963             break;
964         default:
965             g_assert_not_reached();
966         }
967 
968         if (isfiq) {
969             fiqlevel = 1;
970         } else {
971             irqlevel = 1;
972         }
973     }
974 
975     trace_gicv3_cpuif_set_irqs(gicv3_redist_affid(cs), fiqlevel, irqlevel);
976 
977     qemu_set_irq(cs->parent_fiq, fiqlevel);
978     qemu_set_irq(cs->parent_irq, irqlevel);
979 }
980 
981 static uint64_t icc_pmr_read(CPUARMState *env, const ARMCPRegInfo *ri)
982 {
983     GICv3CPUState *cs = icc_cs_from_env(env);
984     uint32_t value = cs->icc_pmr_el1;
985 
986     if (icv_access(env, HCR_FMO | HCR_IMO)) {
987         return icv_pmr_read(env, ri);
988     }
989 
990     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env) &&
991         (env->cp15.scr_el3 & SCR_FIQ)) {
992         /* NS access and Group 0 is inaccessible to NS: return the
993          * NS view of the current priority
994          */
995         if ((value & 0x80) == 0) {
996             /* Secure priorities not visible to NS */
997             value = 0;
998         } else if (value != 0xff) {
999             value = (value << 1) & 0xff;
1000         }
1001     }
1002 
1003     trace_gicv3_icc_pmr_read(gicv3_redist_affid(cs), value);
1004 
1005     return value;
1006 }
1007 
1008 static void icc_pmr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1009                           uint64_t value)
1010 {
1011     GICv3CPUState *cs = icc_cs_from_env(env);
1012 
1013     if (icv_access(env, HCR_FMO | HCR_IMO)) {
1014         return icv_pmr_write(env, ri, value);
1015     }
1016 
1017     trace_gicv3_icc_pmr_write(gicv3_redist_affid(cs), value);
1018 
1019     value &= icc_fullprio_mask(cs);
1020 
1021     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env) &&
1022         (env->cp15.scr_el3 & SCR_FIQ)) {
1023         /* NS access and Group 0 is inaccessible to NS: return the
1024          * NS view of the current priority
1025          */
1026         if (!(cs->icc_pmr_el1 & 0x80)) {
1027             /* Current PMR in the secure range, don't allow NS to change it */
1028             return;
1029         }
1030         value = (value >> 1) | 0x80;
1031     }
1032     cs->icc_pmr_el1 = value;
1033     gicv3_cpuif_update(cs);
1034 }
1035 
1036 static void icc_activate_irq(GICv3CPUState *cs, int irq)
1037 {
1038     /* Move the interrupt from the Pending state to Active, and update
1039      * the Active Priority Registers
1040      */
1041     uint32_t mask = icc_gprio_mask(cs, cs->hppi.grp);
1042     int prio = cs->hppi.prio & mask;
1043     int aprbit = prio >> (8 - cs->prebits);
1044     int regno = aprbit / 32;
1045     int regbit = aprbit % 32;
1046 
1047     cs->icc_apr[cs->hppi.grp][regno] |= (1 << regbit);
1048 
1049     if (irq < GIC_INTERNAL) {
1050         cs->gicr_iactiver0 = deposit32(cs->gicr_iactiver0, irq, 1, 1);
1051         cs->gicr_ipendr0 = deposit32(cs->gicr_ipendr0, irq, 1, 0);
1052         gicv3_redist_update(cs);
1053     } else if (irq < GICV3_LPI_INTID_START) {
1054         gicv3_gicd_active_set(cs->gic, irq);
1055         gicv3_gicd_pending_clear(cs->gic, irq);
1056         gicv3_update(cs->gic, irq, 1);
1057     } else {
1058         gicv3_redist_lpi_pending(cs, irq, 0);
1059     }
1060 }
1061 
1062 static uint64_t icc_hppir0_value(GICv3CPUState *cs, CPUARMState *env)
1063 {
1064     /* Return the highest priority pending interrupt register value
1065      * for group 0.
1066      */
1067     bool irq_is_secure;
1068 
1069     if (cs->hppi.prio == 0xff) {
1070         return INTID_SPURIOUS;
1071     }
1072 
1073     /* Check whether we can return the interrupt or if we should return
1074      * a special identifier, as per the CheckGroup0ForSpecialIdentifiers
1075      * pseudocode. (We can simplify a little because for us ICC_SRE_EL1.RM
1076      * is always zero.)
1077      */
1078     irq_is_secure = (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) &&
1079                      (cs->hppi.grp != GICV3_G1NS));
1080 
1081     if (cs->hppi.grp != GICV3_G0 && !arm_is_el3_or_mon(env)) {
1082         return INTID_SPURIOUS;
1083     }
1084     if (irq_is_secure && !arm_is_secure(env)) {
1085         /* Secure interrupts not visible to Nonsecure */
1086         return INTID_SPURIOUS;
1087     }
1088 
1089     if (cs->hppi.grp != GICV3_G0) {
1090         /* Indicate to EL3 that there's a Group 1 interrupt for the other
1091          * state pending.
1092          */
1093         return irq_is_secure ? INTID_SECURE : INTID_NONSECURE;
1094     }
1095 
1096     return cs->hppi.irq;
1097 }
1098 
1099 static uint64_t icc_hppir1_value(GICv3CPUState *cs, CPUARMState *env)
1100 {
1101     /* Return the highest priority pending interrupt register value
1102      * for group 1.
1103      */
1104     bool irq_is_secure;
1105 
1106     if (cs->hppi.prio == 0xff) {
1107         return INTID_SPURIOUS;
1108     }
1109 
1110     /* Check whether we can return the interrupt or if we should return
1111      * a special identifier, as per the CheckGroup1ForSpecialIdentifiers
1112      * pseudocode. (We can simplify a little because for us ICC_SRE_EL1.RM
1113      * is always zero.)
1114      */
1115     irq_is_secure = (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) &&
1116                      (cs->hppi.grp != GICV3_G1NS));
1117 
1118     if (cs->hppi.grp == GICV3_G0) {
1119         /* Group 0 interrupts not visible via HPPIR1 */
1120         return INTID_SPURIOUS;
1121     }
1122     if (irq_is_secure) {
1123         if (!arm_is_secure(env)) {
1124             /* Secure interrupts not visible in Non-secure */
1125             return INTID_SPURIOUS;
1126         }
1127     } else if (!arm_is_el3_or_mon(env) && arm_is_secure(env)) {
1128         /* Group 1 non-secure interrupts not visible in Secure EL1 */
1129         return INTID_SPURIOUS;
1130     }
1131 
1132     return cs->hppi.irq;
1133 }
1134 
1135 static uint64_t icc_iar0_read(CPUARMState *env, const ARMCPRegInfo *ri)
1136 {
1137     GICv3CPUState *cs = icc_cs_from_env(env);
1138     uint64_t intid;
1139 
1140     if (icv_access(env, HCR_FMO)) {
1141         return icv_iar_read(env, ri);
1142     }
1143 
1144     if (!icc_hppi_can_preempt(cs)) {
1145         intid = INTID_SPURIOUS;
1146     } else {
1147         intid = icc_hppir0_value(cs, env);
1148     }
1149 
1150     if (!gicv3_intid_is_special(intid)) {
1151         icc_activate_irq(cs, intid);
1152     }
1153 
1154     trace_gicv3_icc_iar0_read(gicv3_redist_affid(cs), intid);
1155     return intid;
1156 }
1157 
1158 static uint64_t icc_iar1_read(CPUARMState *env, const ARMCPRegInfo *ri)
1159 {
1160     GICv3CPUState *cs = icc_cs_from_env(env);
1161     uint64_t intid;
1162 
1163     if (icv_access(env, HCR_IMO)) {
1164         return icv_iar_read(env, ri);
1165     }
1166 
1167     if (!icc_hppi_can_preempt(cs)) {
1168         intid = INTID_SPURIOUS;
1169     } else {
1170         intid = icc_hppir1_value(cs, env);
1171     }
1172 
1173     if (!gicv3_intid_is_special(intid)) {
1174         icc_activate_irq(cs, intid);
1175     }
1176 
1177     trace_gicv3_icc_iar1_read(gicv3_redist_affid(cs), intid);
1178     return intid;
1179 }
1180 
1181 static void icc_drop_prio(GICv3CPUState *cs, int grp)
1182 {
1183     /* Drop the priority of the currently active interrupt in
1184      * the specified group.
1185      *
1186      * Note that we can guarantee (because of the requirement to nest
1187      * ICC_IAR reads [which activate an interrupt and raise priority]
1188      * with ICC_EOIR writes [which drop the priority for the interrupt])
1189      * that the interrupt we're being called for is the highest priority
1190      * active interrupt, meaning that it has the lowest set bit in the
1191      * APR registers.
1192      *
1193      * If the guest does not honour the ordering constraints then the
1194      * behaviour of the GIC is UNPREDICTABLE, which for us means that
1195      * the values of the APR registers might become incorrect and the
1196      * running priority will be wrong, so interrupts that should preempt
1197      * might not do so, and interrupts that should not preempt might do so.
1198      */
1199     int i;
1200 
1201     for (i = 0; i < icc_num_aprs(cs); i++) {
1202         uint64_t *papr = &cs->icc_apr[grp][i];
1203 
1204         if (!*papr) {
1205             continue;
1206         }
1207         /* Clear the lowest set bit */
1208         *papr &= *papr - 1;
1209         break;
1210     }
1211 
1212     /* running priority change means we need an update for this cpu i/f */
1213     gicv3_cpuif_update(cs);
1214 }
1215 
1216 static bool icc_eoi_split(CPUARMState *env, GICv3CPUState *cs)
1217 {
1218     /* Return true if we should split priority drop and interrupt
1219      * deactivation, ie whether the relevant EOIMode bit is set.
1220      */
1221     if (arm_is_el3_or_mon(env)) {
1222         return cs->icc_ctlr_el3 & ICC_CTLR_EL3_EOIMODE_EL3;
1223     }
1224     if (arm_is_secure_below_el3(env)) {
1225         return cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_EOIMODE;
1226     } else {
1227         return cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE;
1228     }
1229 }
1230 
1231 static int icc_highest_active_group(GICv3CPUState *cs)
1232 {
1233     /* Return the group with the highest priority active interrupt.
1234      * We can do this by just comparing the APRs to see which one
1235      * has the lowest set bit.
1236      * (If more than one group is active at the same priority then
1237      * we're in UNPREDICTABLE territory.)
1238      */
1239     int i;
1240 
1241     for (i = 0; i < ARRAY_SIZE(cs->icc_apr[0]); i++) {
1242         int g0ctz = ctz32(cs->icc_apr[GICV3_G0][i]);
1243         int g1ctz = ctz32(cs->icc_apr[GICV3_G1][i]);
1244         int g1nsctz = ctz32(cs->icc_apr[GICV3_G1NS][i]);
1245 
1246         if (g1nsctz < g0ctz && g1nsctz < g1ctz) {
1247             return GICV3_G1NS;
1248         }
1249         if (g1ctz < g0ctz) {
1250             return GICV3_G1;
1251         }
1252         if (g0ctz < 32) {
1253             return GICV3_G0;
1254         }
1255     }
1256     /* No set active bits? UNPREDICTABLE; return -1 so the caller
1257      * ignores the spurious EOI attempt.
1258      */
1259     return -1;
1260 }
1261 
1262 static void icc_deactivate_irq(GICv3CPUState *cs, int irq)
1263 {
1264     if (irq < GIC_INTERNAL) {
1265         cs->gicr_iactiver0 = deposit32(cs->gicr_iactiver0, irq, 1, 0);
1266         gicv3_redist_update(cs);
1267     } else {
1268         gicv3_gicd_active_clear(cs->gic, irq);
1269         gicv3_update(cs->gic, irq, 1);
1270     }
1271 }
1272 
1273 static bool icv_eoi_split(CPUARMState *env, GICv3CPUState *cs)
1274 {
1275     /* Return true if we should split priority drop and interrupt
1276      * deactivation, ie whether the virtual EOIMode bit is set.
1277      */
1278     return cs->ich_vmcr_el2 & ICH_VMCR_EL2_VEOIM;
1279 }
1280 
1281 static int icv_find_active(GICv3CPUState *cs, int irq)
1282 {
1283     /* Given an interrupt number for an active interrupt, return the index
1284      * of the corresponding list register, or -1 if there is no match.
1285      * Corresponds to FindActiveVirtualInterrupt pseudocode.
1286      */
1287     int i;
1288 
1289     for (i = 0; i < cs->num_list_regs; i++) {
1290         uint64_t lr = cs->ich_lr_el2[i];
1291 
1292         if ((lr & ICH_LR_EL2_STATE_ACTIVE_BIT) && ich_lr_vintid(lr) == irq) {
1293             return i;
1294         }
1295     }
1296 
1297     return -1;
1298 }
1299 
1300 static void icv_deactivate_irq(GICv3CPUState *cs, int idx)
1301 {
1302     /* Deactivate the interrupt in the specified list register index */
1303     uint64_t lr = cs->ich_lr_el2[idx];
1304 
1305     if (lr & ICH_LR_EL2_HW) {
1306         /* Deactivate the associated physical interrupt */
1307         int pirq = ich_lr_pintid(lr);
1308 
1309         if (pirq < INTID_SECURE) {
1310             icc_deactivate_irq(cs, pirq);
1311         }
1312     }
1313 
1314     /* Clear the 'active' part of the state, so ActivePending->Pending
1315      * and Active->Invalid.
1316      */
1317     lr &= ~ICH_LR_EL2_STATE_ACTIVE_BIT;
1318     cs->ich_lr_el2[idx] = lr;
1319 }
1320 
1321 static void icv_increment_eoicount(GICv3CPUState *cs)
1322 {
1323     /* Increment the EOICOUNT field in ICH_HCR_EL2 */
1324     int eoicount = extract64(cs->ich_hcr_el2, ICH_HCR_EL2_EOICOUNT_SHIFT,
1325                              ICH_HCR_EL2_EOICOUNT_LENGTH);
1326 
1327     cs->ich_hcr_el2 = deposit64(cs->ich_hcr_el2, ICH_HCR_EL2_EOICOUNT_SHIFT,
1328                                 ICH_HCR_EL2_EOICOUNT_LENGTH, eoicount + 1);
1329 }
1330 
1331 static int icv_drop_prio(GICv3CPUState *cs)
1332 {
1333     /* Drop the priority of the currently active virtual interrupt
1334      * (favouring group 0 if there is a set active bit at
1335      * the same priority for both group 0 and group 1).
1336      * Return the priority value for the bit we just cleared,
1337      * or 0xff if no bits were set in the AP registers at all.
1338      * Note that though the ich_apr[] are uint64_t only the low
1339      * 32 bits are actually relevant.
1340      */
1341     int i;
1342     int aprmax = ich_num_aprs(cs);
1343 
1344     for (i = 0; i < aprmax; i++) {
1345         uint64_t *papr0 = &cs->ich_apr[GICV3_G0][i];
1346         uint64_t *papr1 = &cs->ich_apr[GICV3_G1NS][i];
1347         int apr0count, apr1count;
1348 
1349         if (!*papr0 && !*papr1) {
1350             continue;
1351         }
1352 
1353         /* We can't just use the bit-twiddling hack icc_drop_prio() does
1354          * because we need to return the bit number we cleared so
1355          * it can be compared against the list register's priority field.
1356          */
1357         apr0count = ctz32(*papr0);
1358         apr1count = ctz32(*papr1);
1359 
1360         if (apr0count <= apr1count) {
1361             *papr0 &= *papr0 - 1;
1362             return (apr0count + i * 32) << (icv_min_vbpr(cs) + 1);
1363         } else {
1364             *papr1 &= *papr1 - 1;
1365             return (apr1count + i * 32) << (icv_min_vbpr(cs) + 1);
1366         }
1367     }
1368     return 0xff;
1369 }
1370 
1371 static void icv_dir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1372                           uint64_t value)
1373 {
1374     /* Deactivate interrupt */
1375     GICv3CPUState *cs = icc_cs_from_env(env);
1376     int idx;
1377     int irq = value & 0xffffff;
1378 
1379     trace_gicv3_icv_dir_write(gicv3_redist_affid(cs), value);
1380 
1381     if (irq >= GICV3_MAXIRQ) {
1382         /* Also catches special interrupt numbers and LPIs */
1383         return;
1384     }
1385 
1386     if (!icv_eoi_split(env, cs)) {
1387         return;
1388     }
1389 
1390     idx = icv_find_active(cs, irq);
1391 
1392     if (idx < 0) {
1393         /* No list register matching this, so increment the EOI count
1394          * (might trigger a maintenance interrupt)
1395          */
1396         icv_increment_eoicount(cs);
1397     } else {
1398         icv_deactivate_irq(cs, idx);
1399     }
1400 
1401     gicv3_cpuif_virt_update(cs);
1402 }
1403 
1404 static void icv_eoir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1405                            uint64_t value)
1406 {
1407     /* End of Interrupt */
1408     GICv3CPUState *cs = icc_cs_from_env(env);
1409     int irq = value & 0xffffff;
1410     int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
1411     int idx, dropprio;
1412 
1413     trace_gicv3_icv_eoir_write(ri->crm == 8 ? 0 : 1,
1414                                gicv3_redist_affid(cs), value);
1415 
1416     if (gicv3_intid_is_special(irq)) {
1417         return;
1418     }
1419 
1420     /* We implement the IMPDEF choice of "drop priority before doing
1421      * error checks" (because that lets us avoid scanning the AP
1422      * registers twice).
1423      */
1424     dropprio = icv_drop_prio(cs);
1425     if (dropprio == 0xff) {
1426         /* No active interrupt. It is CONSTRAINED UNPREDICTABLE
1427          * whether the list registers are checked in this
1428          * situation; we choose not to.
1429          */
1430         return;
1431     }
1432 
1433     idx = icv_find_active(cs, irq);
1434 
1435     if (idx < 0) {
1436         /* No valid list register corresponding to EOI ID */
1437         icv_increment_eoicount(cs);
1438     } else {
1439         uint64_t lr = cs->ich_lr_el2[idx];
1440         int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
1441         int lr_gprio = ich_lr_prio(lr) & icv_gprio_mask(cs, grp);
1442 
1443         if (thisgrp == grp && lr_gprio == dropprio) {
1444             if (!icv_eoi_split(env, cs)) {
1445                 /* Priority drop and deactivate not split: deactivate irq now */
1446                 icv_deactivate_irq(cs, idx);
1447             }
1448         }
1449     }
1450 
1451     gicv3_cpuif_virt_update(cs);
1452 }
1453 
1454 static void icc_eoir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1455                            uint64_t value)
1456 {
1457     /* End of Interrupt */
1458     GICv3CPUState *cs = icc_cs_from_env(env);
1459     int irq = value & 0xffffff;
1460     int grp;
1461     bool is_eoir0 = ri->crm == 8;
1462 
1463     if (icv_access(env, is_eoir0 ? HCR_FMO : HCR_IMO)) {
1464         icv_eoir_write(env, ri, value);
1465         return;
1466     }
1467 
1468     trace_gicv3_icc_eoir_write(is_eoir0 ? 0 : 1,
1469                                gicv3_redist_affid(cs), value);
1470 
1471     if ((irq >= cs->gic->num_irq) &&
1472         !(cs->gic->lpi_enable && (irq >= GICV3_LPI_INTID_START))) {
1473         /* This handles two cases:
1474          * 1. If software writes the ID of a spurious interrupt [ie 1020-1023]
1475          * to the GICC_EOIR, the GIC ignores that write.
1476          * 2. If software writes the number of a non-existent interrupt
1477          * this must be a subcase of "value written does not match the last
1478          * valid interrupt value read from the Interrupt Acknowledge
1479          * register" and so this is UNPREDICTABLE. We choose to ignore it.
1480          */
1481         return;
1482     }
1483 
1484     grp = icc_highest_active_group(cs);
1485     switch (grp) {
1486     case GICV3_G0:
1487         if (!is_eoir0) {
1488             return;
1489         }
1490         if (!(cs->gic->gicd_ctlr & GICD_CTLR_DS)
1491             && arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env)) {
1492             return;
1493         }
1494         break;
1495     case GICV3_G1:
1496         if (is_eoir0) {
1497             return;
1498         }
1499         if (!arm_is_secure(env)) {
1500             return;
1501         }
1502         break;
1503     case GICV3_G1NS:
1504         if (is_eoir0) {
1505             return;
1506         }
1507         if (!arm_is_el3_or_mon(env) && arm_is_secure(env)) {
1508             return;
1509         }
1510         break;
1511     default:
1512         qemu_log_mask(LOG_GUEST_ERROR,
1513                       "%s: IRQ %d isn't active\n", __func__, irq);
1514         return;
1515     }
1516 
1517     icc_drop_prio(cs, grp);
1518 
1519     if (!icc_eoi_split(env, cs)) {
1520         /* Priority drop and deactivate not split: deactivate irq now */
1521         icc_deactivate_irq(cs, irq);
1522     }
1523 }
1524 
1525 static uint64_t icc_hppir0_read(CPUARMState *env, const ARMCPRegInfo *ri)
1526 {
1527     GICv3CPUState *cs = icc_cs_from_env(env);
1528     uint64_t value;
1529 
1530     if (icv_access(env, HCR_FMO)) {
1531         return icv_hppir_read(env, ri);
1532     }
1533 
1534     value = icc_hppir0_value(cs, env);
1535     trace_gicv3_icc_hppir0_read(gicv3_redist_affid(cs), value);
1536     return value;
1537 }
1538 
1539 static uint64_t icc_hppir1_read(CPUARMState *env, const ARMCPRegInfo *ri)
1540 {
1541     GICv3CPUState *cs = icc_cs_from_env(env);
1542     uint64_t value;
1543 
1544     if (icv_access(env, HCR_IMO)) {
1545         return icv_hppir_read(env, ri);
1546     }
1547 
1548     value = icc_hppir1_value(cs, env);
1549     trace_gicv3_icc_hppir1_read(gicv3_redist_affid(cs), value);
1550     return value;
1551 }
1552 
1553 static uint64_t icc_bpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1554 {
1555     GICv3CPUState *cs = icc_cs_from_env(env);
1556     int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1;
1557     bool satinc = false;
1558     uint64_t bpr;
1559 
1560     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1561         return icv_bpr_read(env, ri);
1562     }
1563 
1564     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1565         grp = GICV3_G1NS;
1566     }
1567 
1568     if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) &&
1569         (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) {
1570         /* CBPR_EL1S means secure EL1 or AArch32 EL3 !Mon BPR1 accesses
1571          * modify BPR0
1572          */
1573         grp = GICV3_G0;
1574     }
1575 
1576     if (grp == GICV3_G1NS && arm_current_el(env) < 3 &&
1577         (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
1578         /* reads return bpr0 + 1 sat to 7, writes ignored */
1579         grp = GICV3_G0;
1580         satinc = true;
1581     }
1582 
1583     bpr = cs->icc_bpr[grp];
1584     if (satinc) {
1585         bpr++;
1586         bpr = MIN(bpr, 7);
1587     }
1588 
1589     trace_gicv3_icc_bpr_read(ri->crm == 8 ? 0 : 1, gicv3_redist_affid(cs), bpr);
1590 
1591     return bpr;
1592 }
1593 
1594 static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1595                           uint64_t value)
1596 {
1597     GICv3CPUState *cs = icc_cs_from_env(env);
1598     int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1;
1599     uint64_t minval;
1600 
1601     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1602         icv_bpr_write(env, ri, value);
1603         return;
1604     }
1605 
1606     trace_gicv3_icc_bpr_write(ri->crm == 8 ? 0 : 1,
1607                               gicv3_redist_affid(cs), value);
1608 
1609     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1610         grp = GICV3_G1NS;
1611     }
1612 
1613     if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) &&
1614         (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) {
1615         /* CBPR_EL1S means secure EL1 or AArch32 EL3 !Mon BPR1 accesses
1616          * modify BPR0
1617          */
1618         grp = GICV3_G0;
1619     }
1620 
1621     if (grp == GICV3_G1NS && arm_current_el(env) < 3 &&
1622         (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
1623         /* reads return bpr0 + 1 sat to 7, writes ignored */
1624         return;
1625     }
1626 
1627     minval = (grp == GICV3_G1NS) ? icc_min_bpr_ns(cs) : icc_min_bpr(cs);
1628     if (value < minval) {
1629         value = minval;
1630     }
1631 
1632     cs->icc_bpr[grp] = value & 7;
1633     gicv3_cpuif_update(cs);
1634 }
1635 
1636 static uint64_t icc_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
1637 {
1638     GICv3CPUState *cs = icc_cs_from_env(env);
1639     uint64_t value;
1640 
1641     int regno = ri->opc2 & 3;
1642     int grp = (ri->crm & 1) ? GICV3_G1 : GICV3_G0;
1643 
1644     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1645         return icv_ap_read(env, ri);
1646     }
1647 
1648     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1649         grp = GICV3_G1NS;
1650     }
1651 
1652     value = cs->icc_apr[grp][regno];
1653 
1654     trace_gicv3_icc_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
1655     return value;
1656 }
1657 
1658 static void icc_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1659                          uint64_t value)
1660 {
1661     GICv3CPUState *cs = icc_cs_from_env(env);
1662 
1663     int regno = ri->opc2 & 3;
1664     int grp = (ri->crm & 1) ? GICV3_G1 : GICV3_G0;
1665 
1666     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1667         icv_ap_write(env, ri, value);
1668         return;
1669     }
1670 
1671     trace_gicv3_icc_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
1672 
1673     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1674         grp = GICV3_G1NS;
1675     }
1676 
1677     /* It's not possible to claim that a Non-secure interrupt is active
1678      * at a priority outside the Non-secure range (128..255), since this
1679      * would otherwise allow malicious NS code to block delivery of S interrupts
1680      * by writing a bad value to these registers.
1681      */
1682     if (grp == GICV3_G1NS && regno < 2 && arm_feature(env, ARM_FEATURE_EL3)) {
1683         return;
1684     }
1685 
1686     cs->icc_apr[grp][regno] = value & 0xFFFFFFFFU;
1687     gicv3_cpuif_update(cs);
1688 }
1689 
1690 static void icc_dir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1691                           uint64_t value)
1692 {
1693     /* Deactivate interrupt */
1694     GICv3CPUState *cs = icc_cs_from_env(env);
1695     int irq = value & 0xffffff;
1696     bool irq_is_secure, single_sec_state, irq_is_grp0;
1697     bool route_fiq_to_el3, route_irq_to_el3, route_fiq_to_el2, route_irq_to_el2;
1698 
1699     if (icv_access(env, HCR_FMO | HCR_IMO)) {
1700         icv_dir_write(env, ri, value);
1701         return;
1702     }
1703 
1704     trace_gicv3_icc_dir_write(gicv3_redist_affid(cs), value);
1705 
1706     if (irq >= cs->gic->num_irq) {
1707         /* Also catches special interrupt numbers and LPIs */
1708         return;
1709     }
1710 
1711     if (!icc_eoi_split(env, cs)) {
1712         return;
1713     }
1714 
1715     int grp = gicv3_irq_group(cs->gic, cs, irq);
1716 
1717     single_sec_state = cs->gic->gicd_ctlr & GICD_CTLR_DS;
1718     irq_is_secure = !single_sec_state && (grp != GICV3_G1NS);
1719     irq_is_grp0 = grp == GICV3_G0;
1720 
1721     /* Check whether we're allowed to deactivate this interrupt based
1722      * on its group and the current CPU state.
1723      * These checks are laid out to correspond to the spec's pseudocode.
1724      */
1725     route_fiq_to_el3 = env->cp15.scr_el3 & SCR_FIQ;
1726     route_irq_to_el3 = env->cp15.scr_el3 & SCR_IRQ;
1727     /* No need to include !IsSecure in route_*_to_el2 as it's only
1728      * tested in cases where we know !IsSecure is true.
1729      */
1730     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
1731     route_fiq_to_el2 = hcr_el2 & HCR_FMO;
1732     route_irq_to_el2 = hcr_el2 & HCR_IMO;
1733 
1734     switch (arm_current_el(env)) {
1735     case 3:
1736         break;
1737     case 2:
1738         if (single_sec_state && irq_is_grp0 && !route_fiq_to_el3) {
1739             break;
1740         }
1741         if (!irq_is_secure && !irq_is_grp0 && !route_irq_to_el3) {
1742             break;
1743         }
1744         return;
1745     case 1:
1746         if (!arm_is_secure_below_el3(env)) {
1747             if (single_sec_state && irq_is_grp0 &&
1748                 !route_fiq_to_el3 && !route_fiq_to_el2) {
1749                 break;
1750             }
1751             if (!irq_is_secure && !irq_is_grp0 &&
1752                 !route_irq_to_el3 && !route_irq_to_el2) {
1753                 break;
1754             }
1755         } else {
1756             if (irq_is_grp0 && !route_fiq_to_el3) {
1757                 break;
1758             }
1759             if (!irq_is_grp0 &&
1760                 (!irq_is_secure || !single_sec_state) &&
1761                 !route_irq_to_el3) {
1762                 break;
1763             }
1764         }
1765         return;
1766     default:
1767         g_assert_not_reached();
1768     }
1769 
1770     icc_deactivate_irq(cs, irq);
1771 }
1772 
1773 static uint64_t icc_rpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1774 {
1775     GICv3CPUState *cs = icc_cs_from_env(env);
1776     int prio;
1777 
1778     if (icv_access(env, HCR_FMO | HCR_IMO)) {
1779         return icv_rpr_read(env, ri);
1780     }
1781 
1782     prio = icc_highest_active_prio(cs);
1783 
1784     if (arm_feature(env, ARM_FEATURE_EL3) &&
1785         !arm_is_secure(env) && (env->cp15.scr_el3 & SCR_FIQ)) {
1786         /* NS GIC access and Group 0 is inaccessible to NS */
1787         if ((prio & 0x80) == 0) {
1788             /* NS mustn't see priorities in the Secure half of the range */
1789             prio = 0;
1790         } else if (prio != 0xff) {
1791             /* Non-idle priority: show the Non-secure view of it */
1792             prio = (prio << 1) & 0xff;
1793         }
1794     }
1795 
1796     trace_gicv3_icc_rpr_read(gicv3_redist_affid(cs), prio);
1797     return prio;
1798 }
1799 
1800 static void icc_generate_sgi(CPUARMState *env, GICv3CPUState *cs,
1801                              uint64_t value, int grp, bool ns)
1802 {
1803     GICv3State *s = cs->gic;
1804 
1805     /* Extract Aff3/Aff2/Aff1 and shift into the bottom 24 bits */
1806     uint64_t aff = extract64(value, 48, 8) << 16 |
1807         extract64(value, 32, 8) << 8 |
1808         extract64(value, 16, 8);
1809     uint32_t targetlist = extract64(value, 0, 16);
1810     uint32_t irq = extract64(value, 24, 4);
1811     bool irm = extract64(value, 40, 1);
1812     int i;
1813 
1814     if (grp == GICV3_G1 && s->gicd_ctlr & GICD_CTLR_DS) {
1815         /* If GICD_CTLR.DS == 1, the Distributor treats Secure Group 1
1816          * interrupts as Group 0 interrupts and must send Secure Group 0
1817          * interrupts to the target CPUs.
1818          */
1819         grp = GICV3_G0;
1820     }
1821 
1822     trace_gicv3_icc_generate_sgi(gicv3_redist_affid(cs), irq, irm,
1823                                  aff, targetlist);
1824 
1825     for (i = 0; i < s->num_cpu; i++) {
1826         GICv3CPUState *ocs = &s->cpu[i];
1827 
1828         if (irm) {
1829             /* IRM == 1 : route to all CPUs except self */
1830             if (cs == ocs) {
1831                 continue;
1832             }
1833         } else {
1834             /* IRM == 0 : route to Aff3.Aff2.Aff1.n for all n in [0..15]
1835              * where the corresponding bit is set in targetlist
1836              */
1837             int aff0;
1838 
1839             if (ocs->gicr_typer >> 40 != aff) {
1840                 continue;
1841             }
1842             aff0 = extract64(ocs->gicr_typer, 32, 8);
1843             if (aff0 > 15 || extract32(targetlist, aff0, 1) == 0) {
1844                 continue;
1845             }
1846         }
1847 
1848         /* The redistributor will check against its own GICR_NSACR as needed */
1849         gicv3_redist_send_sgi(ocs, grp, irq, ns);
1850     }
1851 }
1852 
1853 static void icc_sgi0r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1854                            uint64_t value)
1855 {
1856     /* Generate Secure Group 0 SGI. */
1857     GICv3CPUState *cs = icc_cs_from_env(env);
1858     bool ns = !arm_is_secure(env);
1859 
1860     icc_generate_sgi(env, cs, value, GICV3_G0, ns);
1861 }
1862 
1863 static void icc_sgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1864                            uint64_t value)
1865 {
1866     /* Generate Group 1 SGI for the current Security state */
1867     GICv3CPUState *cs = icc_cs_from_env(env);
1868     int grp;
1869     bool ns = !arm_is_secure(env);
1870 
1871     grp = ns ? GICV3_G1NS : GICV3_G1;
1872     icc_generate_sgi(env, cs, value, grp, ns);
1873 }
1874 
1875 static void icc_asgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1876                              uint64_t value)
1877 {
1878     /* Generate Group 1 SGI for the Security state that is not
1879      * the current state
1880      */
1881     GICv3CPUState *cs = icc_cs_from_env(env);
1882     int grp;
1883     bool ns = !arm_is_secure(env);
1884 
1885     grp = ns ? GICV3_G1 : GICV3_G1NS;
1886     icc_generate_sgi(env, cs, value, grp, ns);
1887 }
1888 
1889 static uint64_t icc_igrpen_read(CPUARMState *env, const ARMCPRegInfo *ri)
1890 {
1891     GICv3CPUState *cs = icc_cs_from_env(env);
1892     int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0;
1893     uint64_t value;
1894 
1895     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1896         return icv_igrpen_read(env, ri);
1897     }
1898 
1899     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1900         grp = GICV3_G1NS;
1901     }
1902 
1903     value = cs->icc_igrpen[grp];
1904     trace_gicv3_icc_igrpen_read(ri->opc2 & 1 ? 1 : 0,
1905                                 gicv3_redist_affid(cs), value);
1906     return value;
1907 }
1908 
1909 static void icc_igrpen_write(CPUARMState *env, const ARMCPRegInfo *ri,
1910                              uint64_t value)
1911 {
1912     GICv3CPUState *cs = icc_cs_from_env(env);
1913     int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0;
1914 
1915     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1916         icv_igrpen_write(env, ri, value);
1917         return;
1918     }
1919 
1920     trace_gicv3_icc_igrpen_write(ri->opc2 & 1 ? 1 : 0,
1921                                  gicv3_redist_affid(cs), value);
1922 
1923     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1924         grp = GICV3_G1NS;
1925     }
1926 
1927     cs->icc_igrpen[grp] = value & ICC_IGRPEN_ENABLE;
1928     gicv3_cpuif_update(cs);
1929 }
1930 
1931 static uint64_t icc_igrpen1_el3_read(CPUARMState *env, const ARMCPRegInfo *ri)
1932 {
1933     GICv3CPUState *cs = icc_cs_from_env(env);
1934     uint64_t value;
1935 
1936     /* IGRPEN1_EL3 bits 0 and 1 are r/w aliases into IGRPEN1_EL1 NS and S */
1937     value = cs->icc_igrpen[GICV3_G1NS] | (cs->icc_igrpen[GICV3_G1] << 1);
1938     trace_gicv3_icc_igrpen1_el3_read(gicv3_redist_affid(cs), value);
1939     return value;
1940 }
1941 
1942 static void icc_igrpen1_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
1943                                   uint64_t value)
1944 {
1945     GICv3CPUState *cs = icc_cs_from_env(env);
1946 
1947     trace_gicv3_icc_igrpen1_el3_write(gicv3_redist_affid(cs), value);
1948 
1949     /* IGRPEN1_EL3 bits 0 and 1 are r/w aliases into IGRPEN1_EL1 NS and S */
1950     cs->icc_igrpen[GICV3_G1NS] = extract32(value, 0, 1);
1951     cs->icc_igrpen[GICV3_G1] = extract32(value, 1, 1);
1952     gicv3_cpuif_update(cs);
1953 }
1954 
1955 static uint64_t icc_ctlr_el1_read(CPUARMState *env, const ARMCPRegInfo *ri)
1956 {
1957     GICv3CPUState *cs = icc_cs_from_env(env);
1958     int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S;
1959     uint64_t value;
1960 
1961     if (icv_access(env, HCR_FMO | HCR_IMO)) {
1962         return icv_ctlr_read(env, ri);
1963     }
1964 
1965     value = cs->icc_ctlr_el1[bank];
1966     trace_gicv3_icc_ctlr_read(gicv3_redist_affid(cs), value);
1967     return value;
1968 }
1969 
1970 static void icc_ctlr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
1971                                uint64_t value)
1972 {
1973     GICv3CPUState *cs = icc_cs_from_env(env);
1974     int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S;
1975     uint64_t mask;
1976 
1977     if (icv_access(env, HCR_FMO | HCR_IMO)) {
1978         icv_ctlr_write(env, ri, value);
1979         return;
1980     }
1981 
1982     trace_gicv3_icc_ctlr_write(gicv3_redist_affid(cs), value);
1983 
1984     /* Only CBPR and EOIMODE can be RW;
1985      * for us PMHE is RAZ/WI (we don't implement 1-of-N interrupts or
1986      * the asseciated priority-based routing of them);
1987      * if EL3 is implemented and GICD_CTLR.DS == 0, then PMHE and CBPR are RO.
1988      */
1989     if (arm_feature(env, ARM_FEATURE_EL3) &&
1990         ((cs->gic->gicd_ctlr & GICD_CTLR_DS) == 0)) {
1991         mask = ICC_CTLR_EL1_EOIMODE;
1992     } else {
1993         mask = ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE;
1994     }
1995 
1996     cs->icc_ctlr_el1[bank] &= ~mask;
1997     cs->icc_ctlr_el1[bank] |= (value & mask);
1998     gicv3_cpuif_update(cs);
1999 }
2000 
2001 
2002 static uint64_t icc_ctlr_el3_read(CPUARMState *env, const ARMCPRegInfo *ri)
2003 {
2004     GICv3CPUState *cs = icc_cs_from_env(env);
2005     uint64_t value;
2006 
2007     value = cs->icc_ctlr_el3;
2008     if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) {
2009         value |= ICC_CTLR_EL3_EOIMODE_EL1NS;
2010     }
2011     if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) {
2012         value |= ICC_CTLR_EL3_CBPR_EL1NS;
2013     }
2014     if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) {
2015         value |= ICC_CTLR_EL3_EOIMODE_EL1S;
2016     }
2017     if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) {
2018         value |= ICC_CTLR_EL3_CBPR_EL1S;
2019     }
2020 
2021     trace_gicv3_icc_ctlr_el3_read(gicv3_redist_affid(cs), value);
2022     return value;
2023 }
2024 
2025 static void icc_ctlr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2026                                uint64_t value)
2027 {
2028     GICv3CPUState *cs = icc_cs_from_env(env);
2029     uint64_t mask;
2030 
2031     trace_gicv3_icc_ctlr_el3_write(gicv3_redist_affid(cs), value);
2032 
2033     /* *_EL1NS and *_EL1S bits are aliases into the ICC_CTLR_EL1 bits. */
2034     cs->icc_ctlr_el1[GICV3_NS] &= ~(ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE);
2035     if (value & ICC_CTLR_EL3_EOIMODE_EL1NS) {
2036         cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_EOIMODE;
2037     }
2038     if (value & ICC_CTLR_EL3_CBPR_EL1NS) {
2039         cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_CBPR;
2040     }
2041 
2042     cs->icc_ctlr_el1[GICV3_S] &= ~(ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE);
2043     if (value & ICC_CTLR_EL3_EOIMODE_EL1S) {
2044         cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_EOIMODE;
2045     }
2046     if (value & ICC_CTLR_EL3_CBPR_EL1S) {
2047         cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_CBPR;
2048     }
2049 
2050     /* The only bit stored in icc_ctlr_el3 which is writable is EOIMODE_EL3: */
2051     mask = ICC_CTLR_EL3_EOIMODE_EL3;
2052 
2053     cs->icc_ctlr_el3 &= ~mask;
2054     cs->icc_ctlr_el3 |= (value & mask);
2055     gicv3_cpuif_update(cs);
2056 }
2057 
2058 static CPAccessResult gicv3_irqfiq_access(CPUARMState *env,
2059                                           const ARMCPRegInfo *ri, bool isread)
2060 {
2061     CPAccessResult r = CP_ACCESS_OK;
2062     GICv3CPUState *cs = icc_cs_from_env(env);
2063     int el = arm_current_el(env);
2064 
2065     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TC) &&
2066         el == 1 && !arm_is_secure_below_el3(env)) {
2067         /* Takes priority over a possible EL3 trap */
2068         return CP_ACCESS_TRAP_EL2;
2069     }
2070 
2071     if ((env->cp15.scr_el3 & (SCR_FIQ | SCR_IRQ)) == (SCR_FIQ | SCR_IRQ)) {
2072         switch (el) {
2073         case 1:
2074             /* Note that arm_hcr_el2_eff takes secure state into account.  */
2075             if ((arm_hcr_el2_eff(env) & (HCR_IMO | HCR_FMO)) == 0) {
2076                 r = CP_ACCESS_TRAP_EL3;
2077             }
2078             break;
2079         case 2:
2080             r = CP_ACCESS_TRAP_EL3;
2081             break;
2082         case 3:
2083             if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
2084                 r = CP_ACCESS_TRAP_EL3;
2085             }
2086             break;
2087         default:
2088             g_assert_not_reached();
2089         }
2090     }
2091 
2092     if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
2093         r = CP_ACCESS_TRAP;
2094     }
2095     return r;
2096 }
2097 
2098 static CPAccessResult gicv3_dir_access(CPUARMState *env,
2099                                        const ARMCPRegInfo *ri, bool isread)
2100 {
2101     GICv3CPUState *cs = icc_cs_from_env(env);
2102 
2103     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TDIR) &&
2104         arm_current_el(env) == 1 && !arm_is_secure_below_el3(env)) {
2105         /* Takes priority over a possible EL3 trap */
2106         return CP_ACCESS_TRAP_EL2;
2107     }
2108 
2109     return gicv3_irqfiq_access(env, ri, isread);
2110 }
2111 
2112 static CPAccessResult gicv3_sgi_access(CPUARMState *env,
2113                                        const ARMCPRegInfo *ri, bool isread)
2114 {
2115     if (arm_current_el(env) == 1 &&
2116         (arm_hcr_el2_eff(env) & (HCR_IMO | HCR_FMO)) != 0) {
2117         /* Takes priority over a possible EL3 trap */
2118         return CP_ACCESS_TRAP_EL2;
2119     }
2120 
2121     return gicv3_irqfiq_access(env, ri, isread);
2122 }
2123 
2124 static CPAccessResult gicv3_fiq_access(CPUARMState *env,
2125                                        const ARMCPRegInfo *ri, bool isread)
2126 {
2127     CPAccessResult r = CP_ACCESS_OK;
2128     GICv3CPUState *cs = icc_cs_from_env(env);
2129     int el = arm_current_el(env);
2130 
2131     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TALL0) &&
2132         el == 1 && !arm_is_secure_below_el3(env)) {
2133         /* Takes priority over a possible EL3 trap */
2134         return CP_ACCESS_TRAP_EL2;
2135     }
2136 
2137     if (env->cp15.scr_el3 & SCR_FIQ) {
2138         switch (el) {
2139         case 1:
2140             if ((arm_hcr_el2_eff(env) & HCR_FMO) == 0) {
2141                 r = CP_ACCESS_TRAP_EL3;
2142             }
2143             break;
2144         case 2:
2145             r = CP_ACCESS_TRAP_EL3;
2146             break;
2147         case 3:
2148             if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
2149                 r = CP_ACCESS_TRAP_EL3;
2150             }
2151             break;
2152         default:
2153             g_assert_not_reached();
2154         }
2155     }
2156 
2157     if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
2158         r = CP_ACCESS_TRAP;
2159     }
2160     return r;
2161 }
2162 
2163 static CPAccessResult gicv3_irq_access(CPUARMState *env,
2164                                        const ARMCPRegInfo *ri, bool isread)
2165 {
2166     CPAccessResult r = CP_ACCESS_OK;
2167     GICv3CPUState *cs = icc_cs_from_env(env);
2168     int el = arm_current_el(env);
2169 
2170     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TALL1) &&
2171         el == 1 && !arm_is_secure_below_el3(env)) {
2172         /* Takes priority over a possible EL3 trap */
2173         return CP_ACCESS_TRAP_EL2;
2174     }
2175 
2176     if (env->cp15.scr_el3 & SCR_IRQ) {
2177         switch (el) {
2178         case 1:
2179             if ((arm_hcr_el2_eff(env) & HCR_IMO) == 0) {
2180                 r = CP_ACCESS_TRAP_EL3;
2181             }
2182             break;
2183         case 2:
2184             r = CP_ACCESS_TRAP_EL3;
2185             break;
2186         case 3:
2187             if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
2188                 r = CP_ACCESS_TRAP_EL3;
2189             }
2190             break;
2191         default:
2192             g_assert_not_reached();
2193         }
2194     }
2195 
2196     if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
2197         r = CP_ACCESS_TRAP;
2198     }
2199     return r;
2200 }
2201 
2202 static void icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2203 {
2204     GICv3CPUState *cs = icc_cs_from_env(env);
2205 
2206     cs->icc_ctlr_el1[GICV3_S] = ICC_CTLR_EL1_A3V |
2207         (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
2208         ((cs->pribits - 1) << ICC_CTLR_EL1_PRIBITS_SHIFT);
2209     cs->icc_ctlr_el1[GICV3_NS] = ICC_CTLR_EL1_A3V |
2210         (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
2211         ((cs->pribits - 1) << ICC_CTLR_EL1_PRIBITS_SHIFT);
2212     cs->icc_pmr_el1 = 0;
2213     cs->icc_bpr[GICV3_G0] = icc_min_bpr(cs);
2214     cs->icc_bpr[GICV3_G1] = icc_min_bpr(cs);
2215     cs->icc_bpr[GICV3_G1NS] = icc_min_bpr_ns(cs);
2216     memset(cs->icc_apr, 0, sizeof(cs->icc_apr));
2217     memset(cs->icc_igrpen, 0, sizeof(cs->icc_igrpen));
2218     cs->icc_ctlr_el3 = ICC_CTLR_EL3_NDS | ICC_CTLR_EL3_A3V |
2219         (1 << ICC_CTLR_EL3_IDBITS_SHIFT) |
2220         ((cs->pribits - 1) << ICC_CTLR_EL3_PRIBITS_SHIFT);
2221 
2222     memset(cs->ich_apr, 0, sizeof(cs->ich_apr));
2223     cs->ich_hcr_el2 = 0;
2224     memset(cs->ich_lr_el2, 0, sizeof(cs->ich_lr_el2));
2225     cs->ich_vmcr_el2 = ICH_VMCR_EL2_VFIQEN |
2226         ((icv_min_vbpr(cs) + 1) << ICH_VMCR_EL2_VBPR1_SHIFT) |
2227         (icv_min_vbpr(cs) << ICH_VMCR_EL2_VBPR0_SHIFT);
2228 }
2229 
2230 static const ARMCPRegInfo gicv3_cpuif_reginfo[] = {
2231     { .name = "ICC_PMR_EL1", .state = ARM_CP_STATE_BOTH,
2232       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 6, .opc2 = 0,
2233       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2234       .access = PL1_RW, .accessfn = gicv3_irqfiq_access,
2235       .readfn = icc_pmr_read,
2236       .writefn = icc_pmr_write,
2237       /* We hang the whole cpu interface reset routine off here
2238        * rather than parcelling it out into one little function
2239        * per register
2240        */
2241       .resetfn = icc_reset,
2242     },
2243     { .name = "ICC_IAR0_EL1", .state = ARM_CP_STATE_BOTH,
2244       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 0,
2245       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2246       .access = PL1_R, .accessfn = gicv3_fiq_access,
2247       .readfn = icc_iar0_read,
2248     },
2249     { .name = "ICC_EOIR0_EL1", .state = ARM_CP_STATE_BOTH,
2250       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 1,
2251       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2252       .access = PL1_W, .accessfn = gicv3_fiq_access,
2253       .writefn = icc_eoir_write,
2254     },
2255     { .name = "ICC_HPPIR0_EL1", .state = ARM_CP_STATE_BOTH,
2256       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 2,
2257       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2258       .access = PL1_R, .accessfn = gicv3_fiq_access,
2259       .readfn = icc_hppir0_read,
2260     },
2261     { .name = "ICC_BPR0_EL1", .state = ARM_CP_STATE_BOTH,
2262       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 3,
2263       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2264       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2265       .readfn = icc_bpr_read,
2266       .writefn = icc_bpr_write,
2267     },
2268     { .name = "ICC_AP0R0_EL1", .state = ARM_CP_STATE_BOTH,
2269       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 4,
2270       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2271       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2272       .readfn = icc_ap_read,
2273       .writefn = icc_ap_write,
2274     },
2275     /* All the ICC_AP1R*_EL1 registers are banked */
2276     { .name = "ICC_AP1R0_EL1", .state = ARM_CP_STATE_BOTH,
2277       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 0,
2278       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2279       .access = PL1_RW, .accessfn = gicv3_irq_access,
2280       .readfn = icc_ap_read,
2281       .writefn = icc_ap_write,
2282     },
2283     { .name = "ICC_DIR_EL1", .state = ARM_CP_STATE_BOTH,
2284       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 1,
2285       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2286       .access = PL1_W, .accessfn = gicv3_dir_access,
2287       .writefn = icc_dir_write,
2288     },
2289     { .name = "ICC_RPR_EL1", .state = ARM_CP_STATE_BOTH,
2290       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 3,
2291       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2292       .access = PL1_R, .accessfn = gicv3_irqfiq_access,
2293       .readfn = icc_rpr_read,
2294     },
2295     { .name = "ICC_SGI1R_EL1", .state = ARM_CP_STATE_AA64,
2296       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 5,
2297       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2298       .access = PL1_W, .accessfn = gicv3_sgi_access,
2299       .writefn = icc_sgi1r_write,
2300     },
2301     { .name = "ICC_SGI1R",
2302       .cp = 15, .opc1 = 0, .crm = 12,
2303       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2304       .access = PL1_W, .accessfn = gicv3_sgi_access,
2305       .writefn = icc_sgi1r_write,
2306     },
2307     { .name = "ICC_ASGI1R_EL1", .state = ARM_CP_STATE_AA64,
2308       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 6,
2309       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2310       .access = PL1_W, .accessfn = gicv3_sgi_access,
2311       .writefn = icc_asgi1r_write,
2312     },
2313     { .name = "ICC_ASGI1R",
2314       .cp = 15, .opc1 = 1, .crm = 12,
2315       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2316       .access = PL1_W, .accessfn = gicv3_sgi_access,
2317       .writefn = icc_asgi1r_write,
2318     },
2319     { .name = "ICC_SGI0R_EL1", .state = ARM_CP_STATE_AA64,
2320       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 7,
2321       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2322       .access = PL1_W, .accessfn = gicv3_sgi_access,
2323       .writefn = icc_sgi0r_write,
2324     },
2325     { .name = "ICC_SGI0R",
2326       .cp = 15, .opc1 = 2, .crm = 12,
2327       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2328       .access = PL1_W, .accessfn = gicv3_sgi_access,
2329       .writefn = icc_sgi0r_write,
2330     },
2331     { .name = "ICC_IAR1_EL1", .state = ARM_CP_STATE_BOTH,
2332       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 0,
2333       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2334       .access = PL1_R, .accessfn = gicv3_irq_access,
2335       .readfn = icc_iar1_read,
2336     },
2337     { .name = "ICC_EOIR1_EL1", .state = ARM_CP_STATE_BOTH,
2338       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 1,
2339       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2340       .access = PL1_W, .accessfn = gicv3_irq_access,
2341       .writefn = icc_eoir_write,
2342     },
2343     { .name = "ICC_HPPIR1_EL1", .state = ARM_CP_STATE_BOTH,
2344       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 2,
2345       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2346       .access = PL1_R, .accessfn = gicv3_irq_access,
2347       .readfn = icc_hppir1_read,
2348     },
2349     /* This register is banked */
2350     { .name = "ICC_BPR1_EL1", .state = ARM_CP_STATE_BOTH,
2351       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 3,
2352       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2353       .access = PL1_RW, .accessfn = gicv3_irq_access,
2354       .readfn = icc_bpr_read,
2355       .writefn = icc_bpr_write,
2356     },
2357     /* This register is banked */
2358     { .name = "ICC_CTLR_EL1", .state = ARM_CP_STATE_BOTH,
2359       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 4,
2360       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2361       .access = PL1_RW, .accessfn = gicv3_irqfiq_access,
2362       .readfn = icc_ctlr_el1_read,
2363       .writefn = icc_ctlr_el1_write,
2364     },
2365     { .name = "ICC_SRE_EL1", .state = ARM_CP_STATE_BOTH,
2366       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 5,
2367       .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2368       .access = PL1_RW,
2369       /* We don't support IRQ/FIQ bypass and system registers are
2370        * always enabled, so all our bits are RAZ/WI or RAO/WI.
2371        * This register is banked but since it's constant we don't
2372        * need to do anything special.
2373        */
2374       .resetvalue = 0x7,
2375     },
2376     { .name = "ICC_IGRPEN0_EL1", .state = ARM_CP_STATE_BOTH,
2377       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 6,
2378       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2379       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2380       .readfn = icc_igrpen_read,
2381       .writefn = icc_igrpen_write,
2382     },
2383     /* This register is banked */
2384     { .name = "ICC_IGRPEN1_EL1", .state = ARM_CP_STATE_BOTH,
2385       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 7,
2386       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2387       .access = PL1_RW, .accessfn = gicv3_irq_access,
2388       .readfn = icc_igrpen_read,
2389       .writefn = icc_igrpen_write,
2390     },
2391     { .name = "ICC_SRE_EL2", .state = ARM_CP_STATE_BOTH,
2392       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 5,
2393       .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2394       .access = PL2_RW,
2395       /* We don't support IRQ/FIQ bypass and system registers are
2396        * always enabled, so all our bits are RAZ/WI or RAO/WI.
2397        */
2398       .resetvalue = 0xf,
2399     },
2400     { .name = "ICC_CTLR_EL3", .state = ARM_CP_STATE_BOTH,
2401       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 4,
2402       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2403       .access = PL3_RW,
2404       .readfn = icc_ctlr_el3_read,
2405       .writefn = icc_ctlr_el3_write,
2406     },
2407     { .name = "ICC_SRE_EL3", .state = ARM_CP_STATE_BOTH,
2408       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 5,
2409       .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2410       .access = PL3_RW,
2411       /* We don't support IRQ/FIQ bypass and system registers are
2412        * always enabled, so all our bits are RAZ/WI or RAO/WI.
2413        */
2414       .resetvalue = 0xf,
2415     },
2416     { .name = "ICC_IGRPEN1_EL3", .state = ARM_CP_STATE_BOTH,
2417       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 7,
2418       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2419       .access = PL3_RW,
2420       .readfn = icc_igrpen1_el3_read,
2421       .writefn = icc_igrpen1_el3_write,
2422     },
2423 };
2424 
2425 static const ARMCPRegInfo gicv3_cpuif_icc_apxr1_reginfo[] = {
2426     { .name = "ICC_AP0R1_EL1", .state = ARM_CP_STATE_BOTH,
2427       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 5,
2428       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2429       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2430       .readfn = icc_ap_read,
2431       .writefn = icc_ap_write,
2432     },
2433     { .name = "ICC_AP1R1_EL1", .state = ARM_CP_STATE_BOTH,
2434       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 1,
2435       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2436       .access = PL1_RW, .accessfn = gicv3_irq_access,
2437       .readfn = icc_ap_read,
2438       .writefn = icc_ap_write,
2439     },
2440 };
2441 
2442 static const ARMCPRegInfo gicv3_cpuif_icc_apxr23_reginfo[] = {
2443     { .name = "ICC_AP0R2_EL1", .state = ARM_CP_STATE_BOTH,
2444       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 6,
2445       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2446       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2447       .readfn = icc_ap_read,
2448       .writefn = icc_ap_write,
2449     },
2450     { .name = "ICC_AP0R3_EL1", .state = ARM_CP_STATE_BOTH,
2451       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 7,
2452       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2453       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2454       .readfn = icc_ap_read,
2455       .writefn = icc_ap_write,
2456     },
2457     { .name = "ICC_AP1R2_EL1", .state = ARM_CP_STATE_BOTH,
2458       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 2,
2459       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2460       .access = PL1_RW, .accessfn = gicv3_irq_access,
2461       .readfn = icc_ap_read,
2462       .writefn = icc_ap_write,
2463     },
2464     { .name = "ICC_AP1R3_EL1", .state = ARM_CP_STATE_BOTH,
2465       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 3,
2466       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2467       .access = PL1_RW, .accessfn = gicv3_irq_access,
2468       .readfn = icc_ap_read,
2469       .writefn = icc_ap_write,
2470     },
2471 };
2472 
2473 static uint64_t ich_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2474 {
2475     GICv3CPUState *cs = icc_cs_from_env(env);
2476     int regno = ri->opc2 & 3;
2477     int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
2478     uint64_t value;
2479 
2480     value = cs->ich_apr[grp][regno];
2481     trace_gicv3_ich_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
2482     return value;
2483 }
2484 
2485 static void ich_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2486                          uint64_t value)
2487 {
2488     GICv3CPUState *cs = icc_cs_from_env(env);
2489     int regno = ri->opc2 & 3;
2490     int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
2491 
2492     trace_gicv3_ich_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
2493 
2494     cs->ich_apr[grp][regno] = value & 0xFFFFFFFFU;
2495     gicv3_cpuif_virt_irq_fiq_update(cs);
2496 }
2497 
2498 static uint64_t ich_hcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2499 {
2500     GICv3CPUState *cs = icc_cs_from_env(env);
2501     uint64_t value = cs->ich_hcr_el2;
2502 
2503     trace_gicv3_ich_hcr_read(gicv3_redist_affid(cs), value);
2504     return value;
2505 }
2506 
2507 static void ich_hcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2508                           uint64_t value)
2509 {
2510     GICv3CPUState *cs = icc_cs_from_env(env);
2511 
2512     trace_gicv3_ich_hcr_write(gicv3_redist_affid(cs), value);
2513 
2514     value &= ICH_HCR_EL2_EN | ICH_HCR_EL2_UIE | ICH_HCR_EL2_LRENPIE |
2515         ICH_HCR_EL2_NPIE | ICH_HCR_EL2_VGRP0EIE | ICH_HCR_EL2_VGRP0DIE |
2516         ICH_HCR_EL2_VGRP1EIE | ICH_HCR_EL2_VGRP1DIE | ICH_HCR_EL2_TC |
2517         ICH_HCR_EL2_TALL0 | ICH_HCR_EL2_TALL1 | ICH_HCR_EL2_TSEI |
2518         ICH_HCR_EL2_TDIR | ICH_HCR_EL2_EOICOUNT_MASK;
2519 
2520     cs->ich_hcr_el2 = value;
2521     gicv3_cpuif_virt_update(cs);
2522 }
2523 
2524 static uint64_t ich_vmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2525 {
2526     GICv3CPUState *cs = icc_cs_from_env(env);
2527     uint64_t value = cs->ich_vmcr_el2;
2528 
2529     trace_gicv3_ich_vmcr_read(gicv3_redist_affid(cs), value);
2530     return value;
2531 }
2532 
2533 static void ich_vmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2534                          uint64_t value)
2535 {
2536     GICv3CPUState *cs = icc_cs_from_env(env);
2537 
2538     trace_gicv3_ich_vmcr_write(gicv3_redist_affid(cs), value);
2539 
2540     value &= ICH_VMCR_EL2_VENG0 | ICH_VMCR_EL2_VENG1 | ICH_VMCR_EL2_VCBPR |
2541         ICH_VMCR_EL2_VEOIM | ICH_VMCR_EL2_VBPR1_MASK |
2542         ICH_VMCR_EL2_VBPR0_MASK | ICH_VMCR_EL2_VPMR_MASK;
2543     value |= ICH_VMCR_EL2_VFIQEN;
2544 
2545     cs->ich_vmcr_el2 = value;
2546     /* Enforce "writing BPRs to less than minimum sets them to the minimum"
2547      * by reading and writing back the fields.
2548      */
2549     write_vbpr(cs, GICV3_G0, read_vbpr(cs, GICV3_G0));
2550     write_vbpr(cs, GICV3_G1, read_vbpr(cs, GICV3_G1));
2551 
2552     gicv3_cpuif_virt_update(cs);
2553 }
2554 
2555 static uint64_t ich_lr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2556 {
2557     GICv3CPUState *cs = icc_cs_from_env(env);
2558     int regno = ri->opc2 | ((ri->crm & 1) << 3);
2559     uint64_t value;
2560 
2561     /* This read function handles all of:
2562      * 64-bit reads of the whole LR
2563      * 32-bit reads of the low half of the LR
2564      * 32-bit reads of the high half of the LR
2565      */
2566     if (ri->state == ARM_CP_STATE_AA32) {
2567         if (ri->crm >= 14) {
2568             value = extract64(cs->ich_lr_el2[regno], 32, 32);
2569             trace_gicv3_ich_lrc_read(regno, gicv3_redist_affid(cs), value);
2570         } else {
2571             value = extract64(cs->ich_lr_el2[regno], 0, 32);
2572             trace_gicv3_ich_lr32_read(regno, gicv3_redist_affid(cs), value);
2573         }
2574     } else {
2575         value = cs->ich_lr_el2[regno];
2576         trace_gicv3_ich_lr_read(regno, gicv3_redist_affid(cs), value);
2577     }
2578 
2579     return value;
2580 }
2581 
2582 static void ich_lr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2583                          uint64_t value)
2584 {
2585     GICv3CPUState *cs = icc_cs_from_env(env);
2586     int regno = ri->opc2 | ((ri->crm & 1) << 3);
2587 
2588     /* This write function handles all of:
2589      * 64-bit writes to the whole LR
2590      * 32-bit writes to the low half of the LR
2591      * 32-bit writes to the high half of the LR
2592      */
2593     if (ri->state == ARM_CP_STATE_AA32) {
2594         if (ri->crm >= 14) {
2595             trace_gicv3_ich_lrc_write(regno, gicv3_redist_affid(cs), value);
2596             value = deposit64(cs->ich_lr_el2[regno], 32, 32, value);
2597         } else {
2598             trace_gicv3_ich_lr32_write(regno, gicv3_redist_affid(cs), value);
2599             value = deposit64(cs->ich_lr_el2[regno], 0, 32, value);
2600         }
2601     } else {
2602         trace_gicv3_ich_lr_write(regno, gicv3_redist_affid(cs), value);
2603     }
2604 
2605     /* Enforce RES0 bits in priority field */
2606     if (cs->vpribits < 8) {
2607         value = deposit64(value, ICH_LR_EL2_PRIORITY_SHIFT,
2608                           8 - cs->vpribits, 0);
2609     }
2610 
2611     cs->ich_lr_el2[regno] = value;
2612     gicv3_cpuif_virt_update(cs);
2613 }
2614 
2615 static uint64_t ich_vtr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2616 {
2617     GICv3CPUState *cs = icc_cs_from_env(env);
2618     uint64_t value;
2619 
2620     value = ((cs->num_list_regs - 1) << ICH_VTR_EL2_LISTREGS_SHIFT)
2621         | ICH_VTR_EL2_TDS | ICH_VTR_EL2_A3V
2622         | (1 << ICH_VTR_EL2_IDBITS_SHIFT)
2623         | ((cs->vprebits - 1) << ICH_VTR_EL2_PREBITS_SHIFT)
2624         | ((cs->vpribits - 1) << ICH_VTR_EL2_PRIBITS_SHIFT);
2625 
2626     if (cs->gic->revision < 4) {
2627         value |= ICH_VTR_EL2_NV4;
2628     }
2629 
2630     trace_gicv3_ich_vtr_read(gicv3_redist_affid(cs), value);
2631     return value;
2632 }
2633 
2634 static uint64_t ich_misr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2635 {
2636     GICv3CPUState *cs = icc_cs_from_env(env);
2637     uint64_t value = maintenance_interrupt_state(cs);
2638 
2639     trace_gicv3_ich_misr_read(gicv3_redist_affid(cs), value);
2640     return value;
2641 }
2642 
2643 static uint64_t ich_eisr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2644 {
2645     GICv3CPUState *cs = icc_cs_from_env(env);
2646     uint64_t value = eoi_maintenance_interrupt_state(cs, NULL);
2647 
2648     trace_gicv3_ich_eisr_read(gicv3_redist_affid(cs), value);
2649     return value;
2650 }
2651 
2652 static uint64_t ich_elrsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2653 {
2654     GICv3CPUState *cs = icc_cs_from_env(env);
2655     uint64_t value = 0;
2656     int i;
2657 
2658     for (i = 0; i < cs->num_list_regs; i++) {
2659         uint64_t lr = cs->ich_lr_el2[i];
2660 
2661         if ((lr & ICH_LR_EL2_STATE_MASK) == 0 &&
2662             ((lr & ICH_LR_EL2_HW) != 0 || (lr & ICH_LR_EL2_EOI) == 0)) {
2663             value |= (1 << i);
2664         }
2665     }
2666 
2667     trace_gicv3_ich_elrsr_read(gicv3_redist_affid(cs), value);
2668     return value;
2669 }
2670 
2671 static const ARMCPRegInfo gicv3_cpuif_hcr_reginfo[] = {
2672     { .name = "ICH_AP0R0_EL2", .state = ARM_CP_STATE_BOTH,
2673       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 0,
2674       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2675       .access = PL2_RW,
2676       .readfn = ich_ap_read,
2677       .writefn = ich_ap_write,
2678     },
2679     { .name = "ICH_AP1R0_EL2", .state = ARM_CP_STATE_BOTH,
2680       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 0,
2681       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2682       .access = PL2_RW,
2683       .readfn = ich_ap_read,
2684       .writefn = ich_ap_write,
2685     },
2686     { .name = "ICH_HCR_EL2", .state = ARM_CP_STATE_BOTH,
2687       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 0,
2688       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2689       .access = PL2_RW,
2690       .readfn = ich_hcr_read,
2691       .writefn = ich_hcr_write,
2692     },
2693     { .name = "ICH_VTR_EL2", .state = ARM_CP_STATE_BOTH,
2694       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 1,
2695       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2696       .access = PL2_R,
2697       .readfn = ich_vtr_read,
2698     },
2699     { .name = "ICH_MISR_EL2", .state = ARM_CP_STATE_BOTH,
2700       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 2,
2701       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2702       .access = PL2_R,
2703       .readfn = ich_misr_read,
2704     },
2705     { .name = "ICH_EISR_EL2", .state = ARM_CP_STATE_BOTH,
2706       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 3,
2707       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2708       .access = PL2_R,
2709       .readfn = ich_eisr_read,
2710     },
2711     { .name = "ICH_ELRSR_EL2", .state = ARM_CP_STATE_BOTH,
2712       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 5,
2713       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2714       .access = PL2_R,
2715       .readfn = ich_elrsr_read,
2716     },
2717     { .name = "ICH_VMCR_EL2", .state = ARM_CP_STATE_BOTH,
2718       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 7,
2719       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2720       .access = PL2_RW,
2721       .readfn = ich_vmcr_read,
2722       .writefn = ich_vmcr_write,
2723     },
2724 };
2725 
2726 static const ARMCPRegInfo gicv3_cpuif_ich_apxr1_reginfo[] = {
2727     { .name = "ICH_AP0R1_EL2", .state = ARM_CP_STATE_BOTH,
2728       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 1,
2729       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2730       .access = PL2_RW,
2731       .readfn = ich_ap_read,
2732       .writefn = ich_ap_write,
2733     },
2734     { .name = "ICH_AP1R1_EL2", .state = ARM_CP_STATE_BOTH,
2735       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 1,
2736       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2737       .access = PL2_RW,
2738       .readfn = ich_ap_read,
2739       .writefn = ich_ap_write,
2740     },
2741 };
2742 
2743 static const ARMCPRegInfo gicv3_cpuif_ich_apxr23_reginfo[] = {
2744     { .name = "ICH_AP0R2_EL2", .state = ARM_CP_STATE_BOTH,
2745       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 2,
2746       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2747       .access = PL2_RW,
2748       .readfn = ich_ap_read,
2749       .writefn = ich_ap_write,
2750     },
2751     { .name = "ICH_AP0R3_EL2", .state = ARM_CP_STATE_BOTH,
2752       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 3,
2753       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2754       .access = PL2_RW,
2755       .readfn = ich_ap_read,
2756       .writefn = ich_ap_write,
2757     },
2758     { .name = "ICH_AP1R2_EL2", .state = ARM_CP_STATE_BOTH,
2759       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 2,
2760       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2761       .access = PL2_RW,
2762       .readfn = ich_ap_read,
2763       .writefn = ich_ap_write,
2764     },
2765     { .name = "ICH_AP1R3_EL2", .state = ARM_CP_STATE_BOTH,
2766       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 3,
2767       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2768       .access = PL2_RW,
2769       .readfn = ich_ap_read,
2770       .writefn = ich_ap_write,
2771     },
2772 };
2773 
2774 static void gicv3_cpuif_el_change_hook(ARMCPU *cpu, void *opaque)
2775 {
2776     GICv3CPUState *cs = opaque;
2777 
2778     gicv3_cpuif_update(cs);
2779     /*
2780      * Because vLPIs are only pending in NonSecure state,
2781      * an EL change can change the VIRQ/VFIQ status (but
2782      * cannot affect the maintenance interrupt state)
2783      */
2784     gicv3_cpuif_virt_irq_fiq_update(cs);
2785 }
2786 
2787 void gicv3_init_cpuif(GICv3State *s)
2788 {
2789     /* Called from the GICv3 realize function; register our system
2790      * registers with the CPU
2791      */
2792     int i;
2793 
2794     for (i = 0; i < s->num_cpu; i++) {
2795         ARMCPU *cpu = ARM_CPU(qemu_get_cpu(i));
2796         GICv3CPUState *cs = &s->cpu[i];
2797 
2798         /*
2799          * If the CPU doesn't define a GICv3 configuration, probably because
2800          * in real hardware it doesn't have one, then we use default values
2801          * matching the one used by most Arm CPUs. This applies to:
2802          *  cpu->gic_num_lrs
2803          *  cpu->gic_vpribits
2804          *  cpu->gic_vprebits
2805          *  cpu->gic_pribits
2806          */
2807 
2808         /* Note that we can't just use the GICv3CPUState as an opaque pointer
2809          * in define_arm_cp_regs_with_opaque(), because when we're called back
2810          * it might be with code translated by CPU 0 but run by CPU 1, in
2811          * which case we'd get the wrong value.
2812          * So instead we define the regs with no ri->opaque info, and
2813          * get back to the GICv3CPUState from the CPUARMState.
2814          */
2815         define_arm_cp_regs(cpu, gicv3_cpuif_reginfo);
2816 
2817         /*
2818          * The CPU implementation specifies the number of supported
2819          * bits of physical priority. For backwards compatibility
2820          * of migration, we have a compat property that forces use
2821          * of 8 priority bits regardless of what the CPU really has.
2822          */
2823         if (s->force_8bit_prio) {
2824             cs->pribits = 8;
2825         } else {
2826             cs->pribits = cpu->gic_pribits ?: 5;
2827         }
2828 
2829         /*
2830          * The GICv3 has separate ID register fields for virtual priority
2831          * and preemption bit values, but only a single ID register field
2832          * for the physical priority bits. The preemption bit count is
2833          * always the same as the priority bit count, except that 8 bits
2834          * of priority means 7 preemption bits. We precalculate the
2835          * preemption bits because it simplifies the code and makes the
2836          * parallels between the virtual and physical bits of the GIC
2837          * a bit clearer.
2838          */
2839         cs->prebits = cs->pribits;
2840         if (cs->prebits == 8) {
2841             cs->prebits--;
2842         }
2843         /*
2844          * Check that CPU code defining pribits didn't violate
2845          * architectural constraints our implementation relies on.
2846          */
2847         g_assert(cs->pribits >= 4 && cs->pribits <= 8);
2848 
2849         /*
2850          * gicv3_cpuif_reginfo[] defines ICC_AP*R0_EL1; add definitions
2851          * for ICC_AP*R{1,2,3}_EL1 if the prebits value requires them.
2852          */
2853         if (cs->prebits >= 6) {
2854             define_arm_cp_regs(cpu, gicv3_cpuif_icc_apxr1_reginfo);
2855         }
2856         if (cs->prebits == 7) {
2857             define_arm_cp_regs(cpu, gicv3_cpuif_icc_apxr23_reginfo);
2858         }
2859 
2860         if (arm_feature(&cpu->env, ARM_FEATURE_EL2)) {
2861             int j;
2862 
2863             cs->num_list_regs = cpu->gic_num_lrs ?: 4;
2864             cs->vpribits = cpu->gic_vpribits ?: 5;
2865             cs->vprebits = cpu->gic_vprebits ?: 5;
2866 
2867             /* Check against architectural constraints: getting these
2868              * wrong would be a bug in the CPU code defining these,
2869              * and the implementation relies on them holding.
2870              */
2871             g_assert(cs->vprebits <= cs->vpribits);
2872             g_assert(cs->vprebits >= 5 && cs->vprebits <= 7);
2873             g_assert(cs->vpribits >= 5 && cs->vpribits <= 8);
2874 
2875             define_arm_cp_regs(cpu, gicv3_cpuif_hcr_reginfo);
2876 
2877             for (j = 0; j < cs->num_list_regs; j++) {
2878                 /* Note that the AArch64 LRs are 64-bit; the AArch32 LRs
2879                  * are split into two cp15 regs, LR (the low part, with the
2880                  * same encoding as the AArch64 LR) and LRC (the high part).
2881                  */
2882                 ARMCPRegInfo lr_regset[] = {
2883                     { .name = "ICH_LRn_EL2", .state = ARM_CP_STATE_BOTH,
2884                       .opc0 = 3, .opc1 = 4, .crn = 12,
2885                       .crm = 12 + (j >> 3), .opc2 = j & 7,
2886                       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2887                       .access = PL2_RW,
2888                       .readfn = ich_lr_read,
2889                       .writefn = ich_lr_write,
2890                     },
2891                     { .name = "ICH_LRCn_EL2", .state = ARM_CP_STATE_AA32,
2892                       .cp = 15, .opc1 = 4, .crn = 12,
2893                       .crm = 14 + (j >> 3), .opc2 = j & 7,
2894                       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2895                       .access = PL2_RW,
2896                       .readfn = ich_lr_read,
2897                       .writefn = ich_lr_write,
2898                     },
2899                 };
2900                 define_arm_cp_regs(cpu, lr_regset);
2901             }
2902             if (cs->vprebits >= 6) {
2903                 define_arm_cp_regs(cpu, gicv3_cpuif_ich_apxr1_reginfo);
2904             }
2905             if (cs->vprebits == 7) {
2906                 define_arm_cp_regs(cpu, gicv3_cpuif_ich_apxr23_reginfo);
2907             }
2908         }
2909         arm_register_el_change_hook(cpu, gicv3_cpuif_el_change_hook, cs);
2910     }
2911 }
2912