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