xref: /openbmc/qemu/hw/intc/arm_gicv3_cpuif.c (revision e3ae2bbf)
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(grp, gicv3_redist_affid(cs), value);
616     return value;
617 }
618 
619 static void icv_activate_irq(GICv3CPUState *cs, int idx, int grp)
620 {
621     /* Activate the interrupt in the specified list register
622      * by moving it from Pending to Active state, and update the
623      * Active Priority Registers.
624      */
625     uint32_t mask = icv_gprio_mask(cs, grp);
626     int prio = ich_lr_prio(cs->ich_lr_el2[idx]) & mask;
627     int aprbit = prio >> (8 - cs->vprebits);
628     int regno = aprbit / 32;
629     int regbit = aprbit % 32;
630 
631     cs->ich_lr_el2[idx] &= ~ICH_LR_EL2_STATE_PENDING_BIT;
632     cs->ich_lr_el2[idx] |= ICH_LR_EL2_STATE_ACTIVE_BIT;
633     cs->ich_apr[grp][regno] |= (1 << regbit);
634 }
635 
636 static uint64_t icv_iar_read(CPUARMState *env, const ARMCPRegInfo *ri)
637 {
638     GICv3CPUState *cs = icc_cs_from_env(env);
639     int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
640     int idx = hppvi_index(cs);
641     uint64_t intid = INTID_SPURIOUS;
642 
643     if (idx >= 0) {
644         uint64_t lr = cs->ich_lr_el2[idx];
645         int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
646 
647         if (thisgrp == grp && icv_hppi_can_preempt(cs, lr)) {
648             intid = ich_lr_vintid(lr);
649             if (!gicv3_intid_is_special(intid)) {
650                 icv_activate_irq(cs, idx, grp);
651             } else {
652                 /* Interrupt goes from Pending to Invalid */
653                 cs->ich_lr_el2[idx] &= ~ICH_LR_EL2_STATE_PENDING_BIT;
654                 /* We will now return the (bogus) ID from the list register,
655                  * as per the pseudocode.
656                  */
657             }
658         }
659     }
660 
661     trace_gicv3_icv_iar_read(ri->crm == 8 ? 0 : 1,
662                              gicv3_redist_affid(cs), intid);
663 
664     gicv3_cpuif_virt_update(cs);
665 
666     return intid;
667 }
668 
669 static int icc_highest_active_prio(GICv3CPUState *cs)
670 {
671     /* Calculate the current running priority based on the set bits
672      * in the Active Priority Registers.
673      */
674     int i;
675 
676     for (i = 0; i < ARRAY_SIZE(cs->icc_apr[0]); i++) {
677         uint32_t apr = cs->icc_apr[GICV3_G0][i] |
678             cs->icc_apr[GICV3_G1][i] | cs->icc_apr[GICV3_G1NS][i];
679 
680         if (!apr) {
681             continue;
682         }
683         return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1);
684     }
685     /* No current active interrupts: return idle priority */
686     return 0xff;
687 }
688 
689 static uint32_t icc_gprio_mask(GICv3CPUState *cs, int group)
690 {
691     /* Return a mask word which clears the subpriority bits from
692      * a priority value for an interrupt in the specified group.
693      * This depends on the BPR value. For CBPR0 (S or NS):
694      *  a BPR of 0 means the group priority bits are [7:1];
695      *  a BPR of 1 means they are [7:2], and so on down to
696      *  a BPR of 7 meaning no group priority bits at all.
697      * For CBPR1 NS:
698      *  a BPR of 0 is impossible (the minimum value is 1)
699      *  a BPR of 1 means the group priority bits are [7:1];
700      *  a BPR of 2 means they are [7:2], and so on down to
701      *  a BPR of 7 meaning the group priority is [7].
702      *
703      * Which BPR to use depends on the group of the interrupt and
704      * the current ICC_CTLR.CBPR settings.
705      *
706      * This corresponds to the GroupBits() pseudocode.
707      */
708     int bpr;
709 
710     if ((group == GICV3_G1 && cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR) ||
711         (group == GICV3_G1NS &&
712          cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
713         group = GICV3_G0;
714     }
715 
716     bpr = cs->icc_bpr[group] & 7;
717 
718     if (group == GICV3_G1NS) {
719         assert(bpr > 0);
720         bpr--;
721     }
722 
723     return ~0U << (bpr + 1);
724 }
725 
726 static bool icc_no_enabled_hppi(GICv3CPUState *cs)
727 {
728     /* Return true if there is no pending interrupt, or the
729      * highest priority pending interrupt is in a group which has been
730      * disabled at the CPU interface by the ICC_IGRPEN* register enable bits.
731      */
732     return cs->hppi.prio == 0xff || (cs->icc_igrpen[cs->hppi.grp] == 0);
733 }
734 
735 static bool icc_hppi_can_preempt(GICv3CPUState *cs)
736 {
737     /* Return true if we have a pending interrupt of sufficient
738      * priority to preempt.
739      */
740     int rprio;
741     uint32_t mask;
742 
743     if (icc_no_enabled_hppi(cs)) {
744         return false;
745     }
746 
747     if (cs->hppi.prio >= cs->icc_pmr_el1) {
748         /* Priority mask masks this interrupt */
749         return false;
750     }
751 
752     rprio = icc_highest_active_prio(cs);
753     if (rprio == 0xff) {
754         /* No currently running interrupt so we can preempt */
755         return true;
756     }
757 
758     mask = icc_gprio_mask(cs, cs->hppi.grp);
759 
760     /* We only preempt a running interrupt if the pending interrupt's
761      * group priority is sufficient (the subpriorities are not considered).
762      */
763     if ((cs->hppi.prio & mask) < (rprio & mask)) {
764         return true;
765     }
766 
767     return false;
768 }
769 
770 void gicv3_cpuif_update(GICv3CPUState *cs)
771 {
772     /* Tell the CPU about its highest priority pending interrupt */
773     int irqlevel = 0;
774     int fiqlevel = 0;
775     ARMCPU *cpu = ARM_CPU(cs->cpu);
776     CPUARMState *env = &cpu->env;
777 
778     g_assert(qemu_mutex_iothread_locked());
779 
780     trace_gicv3_cpuif_update(gicv3_redist_affid(cs), cs->hppi.irq,
781                              cs->hppi.grp, cs->hppi.prio);
782 
783     if (cs->hppi.grp == GICV3_G1 && !arm_feature(env, ARM_FEATURE_EL3)) {
784         /* If a Security-enabled GIC sends a G1S interrupt to a
785          * Security-disabled CPU, we must treat it as if it were G0.
786          */
787         cs->hppi.grp = GICV3_G0;
788     }
789 
790     if (icc_hppi_can_preempt(cs)) {
791         /* We have an interrupt: should we signal it as IRQ or FIQ?
792          * This is described in the GICv3 spec section 4.6.2.
793          */
794         bool isfiq;
795 
796         switch (cs->hppi.grp) {
797         case GICV3_G0:
798             isfiq = true;
799             break;
800         case GICV3_G1:
801             isfiq = (!arm_is_secure(env) ||
802                      (arm_current_el(env) == 3 && arm_el_is_aa64(env, 3)));
803             break;
804         case GICV3_G1NS:
805             isfiq = arm_is_secure(env);
806             break;
807         default:
808             g_assert_not_reached();
809         }
810 
811         if (isfiq) {
812             fiqlevel = 1;
813         } else {
814             irqlevel = 1;
815         }
816     }
817 
818     trace_gicv3_cpuif_set_irqs(gicv3_redist_affid(cs), fiqlevel, irqlevel);
819 
820     qemu_set_irq(cs->parent_fiq, fiqlevel);
821     qemu_set_irq(cs->parent_irq, irqlevel);
822 }
823 
824 static uint64_t icc_pmr_read(CPUARMState *env, const ARMCPRegInfo *ri)
825 {
826     GICv3CPUState *cs = icc_cs_from_env(env);
827     uint32_t value = cs->icc_pmr_el1;
828 
829     if (icv_access(env, HCR_FMO | HCR_IMO)) {
830         return icv_pmr_read(env, ri);
831     }
832 
833     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env) &&
834         (env->cp15.scr_el3 & SCR_FIQ)) {
835         /* NS access and Group 0 is inaccessible to NS: return the
836          * NS view of the current priority
837          */
838         if ((value & 0x80) == 0) {
839             /* Secure priorities not visible to NS */
840             value = 0;
841         } else if (value != 0xff) {
842             value = (value << 1) & 0xff;
843         }
844     }
845 
846     trace_gicv3_icc_pmr_read(gicv3_redist_affid(cs), value);
847 
848     return value;
849 }
850 
851 static void icc_pmr_write(CPUARMState *env, const ARMCPRegInfo *ri,
852                           uint64_t value)
853 {
854     GICv3CPUState *cs = icc_cs_from_env(env);
855 
856     if (icv_access(env, HCR_FMO | HCR_IMO)) {
857         return icv_pmr_write(env, ri, value);
858     }
859 
860     trace_gicv3_icc_pmr_write(gicv3_redist_affid(cs), value);
861 
862     value &= 0xff;
863 
864     if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env) &&
865         (env->cp15.scr_el3 & SCR_FIQ)) {
866         /* NS access and Group 0 is inaccessible to NS: return the
867          * NS view of the current priority
868          */
869         if (!(cs->icc_pmr_el1 & 0x80)) {
870             /* Current PMR in the secure range, don't allow NS to change it */
871             return;
872         }
873         value = (value >> 1) | 0x80;
874     }
875     cs->icc_pmr_el1 = value;
876     gicv3_cpuif_update(cs);
877 }
878 
879 static void icc_activate_irq(GICv3CPUState *cs, int irq)
880 {
881     /* Move the interrupt from the Pending state to Active, and update
882      * the Active Priority Registers
883      */
884     uint32_t mask = icc_gprio_mask(cs, cs->hppi.grp);
885     int prio = cs->hppi.prio & mask;
886     int aprbit = prio >> 1;
887     int regno = aprbit / 32;
888     int regbit = aprbit % 32;
889 
890     cs->icc_apr[cs->hppi.grp][regno] |= (1 << regbit);
891 
892     if (irq < GIC_INTERNAL) {
893         cs->gicr_iactiver0 = deposit32(cs->gicr_iactiver0, irq, 1, 1);
894         cs->gicr_ipendr0 = deposit32(cs->gicr_ipendr0, irq, 1, 0);
895         gicv3_redist_update(cs);
896     } else if (irq < GICV3_LPI_INTID_START) {
897         gicv3_gicd_active_set(cs->gic, irq);
898         gicv3_gicd_pending_clear(cs->gic, irq);
899         gicv3_update(cs->gic, irq, 1);
900     } else {
901         gicv3_redist_lpi_pending(cs, irq, 0);
902     }
903 }
904 
905 static uint64_t icc_hppir0_value(GICv3CPUState *cs, CPUARMState *env)
906 {
907     /* Return the highest priority pending interrupt register value
908      * for group 0.
909      */
910     bool irq_is_secure;
911 
912     if (cs->hppi.prio == 0xff) {
913         return INTID_SPURIOUS;
914     }
915 
916     /* Check whether we can return the interrupt or if we should return
917      * a special identifier, as per the CheckGroup0ForSpecialIdentifiers
918      * pseudocode. (We can simplify a little because for us ICC_SRE_EL1.RM
919      * is always zero.)
920      */
921     irq_is_secure = (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) &&
922                      (cs->hppi.grp != GICV3_G1NS));
923 
924     if (cs->hppi.grp != GICV3_G0 && !arm_is_el3_or_mon(env)) {
925         return INTID_SPURIOUS;
926     }
927     if (irq_is_secure && !arm_is_secure(env)) {
928         /* Secure interrupts not visible to Nonsecure */
929         return INTID_SPURIOUS;
930     }
931 
932     if (cs->hppi.grp != GICV3_G0) {
933         /* Indicate to EL3 that there's a Group 1 interrupt for the other
934          * state pending.
935          */
936         return irq_is_secure ? INTID_SECURE : INTID_NONSECURE;
937     }
938 
939     return cs->hppi.irq;
940 }
941 
942 static uint64_t icc_hppir1_value(GICv3CPUState *cs, CPUARMState *env)
943 {
944     /* Return the highest priority pending interrupt register value
945      * for group 1.
946      */
947     bool irq_is_secure;
948 
949     if (cs->hppi.prio == 0xff) {
950         return INTID_SPURIOUS;
951     }
952 
953     /* Check whether we can return the interrupt or if we should return
954      * a special identifier, as per the CheckGroup1ForSpecialIdentifiers
955      * pseudocode. (We can simplify a little because for us ICC_SRE_EL1.RM
956      * is always zero.)
957      */
958     irq_is_secure = (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) &&
959                      (cs->hppi.grp != GICV3_G1NS));
960 
961     if (cs->hppi.grp == GICV3_G0) {
962         /* Group 0 interrupts not visible via HPPIR1 */
963         return INTID_SPURIOUS;
964     }
965     if (irq_is_secure) {
966         if (!arm_is_secure(env)) {
967             /* Secure interrupts not visible in Non-secure */
968             return INTID_SPURIOUS;
969         }
970     } else if (!arm_is_el3_or_mon(env) && arm_is_secure(env)) {
971         /* Group 1 non-secure interrupts not visible in Secure EL1 */
972         return INTID_SPURIOUS;
973     }
974 
975     return cs->hppi.irq;
976 }
977 
978 static uint64_t icc_iar0_read(CPUARMState *env, const ARMCPRegInfo *ri)
979 {
980     GICv3CPUState *cs = icc_cs_from_env(env);
981     uint64_t intid;
982 
983     if (icv_access(env, HCR_FMO)) {
984         return icv_iar_read(env, ri);
985     }
986 
987     if (!icc_hppi_can_preempt(cs)) {
988         intid = INTID_SPURIOUS;
989     } else {
990         intid = icc_hppir0_value(cs, env);
991     }
992 
993     if (!gicv3_intid_is_special(intid)) {
994         icc_activate_irq(cs, intid);
995     }
996 
997     trace_gicv3_icc_iar0_read(gicv3_redist_affid(cs), intid);
998     return intid;
999 }
1000 
1001 static uint64_t icc_iar1_read(CPUARMState *env, const ARMCPRegInfo *ri)
1002 {
1003     GICv3CPUState *cs = icc_cs_from_env(env);
1004     uint64_t intid;
1005 
1006     if (icv_access(env, HCR_IMO)) {
1007         return icv_iar_read(env, ri);
1008     }
1009 
1010     if (!icc_hppi_can_preempt(cs)) {
1011         intid = INTID_SPURIOUS;
1012     } else {
1013         intid = icc_hppir1_value(cs, env);
1014     }
1015 
1016     if (!gicv3_intid_is_special(intid)) {
1017         icc_activate_irq(cs, intid);
1018     }
1019 
1020     trace_gicv3_icc_iar1_read(gicv3_redist_affid(cs), intid);
1021     return intid;
1022 }
1023 
1024 static void icc_drop_prio(GICv3CPUState *cs, int grp)
1025 {
1026     /* Drop the priority of the currently active interrupt in
1027      * the specified group.
1028      *
1029      * Note that we can guarantee (because of the requirement to nest
1030      * ICC_IAR reads [which activate an interrupt and raise priority]
1031      * with ICC_EOIR writes [which drop the priority for the interrupt])
1032      * that the interrupt we're being called for is the highest priority
1033      * active interrupt, meaning that it has the lowest set bit in the
1034      * APR registers.
1035      *
1036      * If the guest does not honour the ordering constraints then the
1037      * behaviour of the GIC is UNPREDICTABLE, which for us means that
1038      * the values of the APR registers might become incorrect and the
1039      * running priority will be wrong, so interrupts that should preempt
1040      * might not do so, and interrupts that should not preempt might do so.
1041      */
1042     int i;
1043 
1044     for (i = 0; i < ARRAY_SIZE(cs->icc_apr[grp]); i++) {
1045         uint64_t *papr = &cs->icc_apr[grp][i];
1046 
1047         if (!*papr) {
1048             continue;
1049         }
1050         /* Clear the lowest set bit */
1051         *papr &= *papr - 1;
1052         break;
1053     }
1054 
1055     /* running priority change means we need an update for this cpu i/f */
1056     gicv3_cpuif_update(cs);
1057 }
1058 
1059 static bool icc_eoi_split(CPUARMState *env, GICv3CPUState *cs)
1060 {
1061     /* Return true if we should split priority drop and interrupt
1062      * deactivation, ie whether the relevant EOIMode bit is set.
1063      */
1064     if (arm_is_el3_or_mon(env)) {
1065         return cs->icc_ctlr_el3 & ICC_CTLR_EL3_EOIMODE_EL3;
1066     }
1067     if (arm_is_secure_below_el3(env)) {
1068         return cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_EOIMODE;
1069     } else {
1070         return cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE;
1071     }
1072 }
1073 
1074 static int icc_highest_active_group(GICv3CPUState *cs)
1075 {
1076     /* Return the group with the highest priority active interrupt.
1077      * We can do this by just comparing the APRs to see which one
1078      * has the lowest set bit.
1079      * (If more than one group is active at the same priority then
1080      * we're in UNPREDICTABLE territory.)
1081      */
1082     int i;
1083 
1084     for (i = 0; i < ARRAY_SIZE(cs->icc_apr[0]); i++) {
1085         int g0ctz = ctz32(cs->icc_apr[GICV3_G0][i]);
1086         int g1ctz = ctz32(cs->icc_apr[GICV3_G1][i]);
1087         int g1nsctz = ctz32(cs->icc_apr[GICV3_G1NS][i]);
1088 
1089         if (g1nsctz < g0ctz && g1nsctz < g1ctz) {
1090             return GICV3_G1NS;
1091         }
1092         if (g1ctz < g0ctz) {
1093             return GICV3_G1;
1094         }
1095         if (g0ctz < 32) {
1096             return GICV3_G0;
1097         }
1098     }
1099     /* No set active bits? UNPREDICTABLE; return -1 so the caller
1100      * ignores the spurious EOI attempt.
1101      */
1102     return -1;
1103 }
1104 
1105 static void icc_deactivate_irq(GICv3CPUState *cs, int irq)
1106 {
1107     if (irq < GIC_INTERNAL) {
1108         cs->gicr_iactiver0 = deposit32(cs->gicr_iactiver0, irq, 1, 0);
1109         gicv3_redist_update(cs);
1110     } else {
1111         gicv3_gicd_active_clear(cs->gic, irq);
1112         gicv3_update(cs->gic, irq, 1);
1113     }
1114 }
1115 
1116 static bool icv_eoi_split(CPUARMState *env, GICv3CPUState *cs)
1117 {
1118     /* Return true if we should split priority drop and interrupt
1119      * deactivation, ie whether the virtual EOIMode bit is set.
1120      */
1121     return cs->ich_vmcr_el2 & ICH_VMCR_EL2_VEOIM;
1122 }
1123 
1124 static int icv_find_active(GICv3CPUState *cs, int irq)
1125 {
1126     /* Given an interrupt number for an active interrupt, return the index
1127      * of the corresponding list register, or -1 if there is no match.
1128      * Corresponds to FindActiveVirtualInterrupt pseudocode.
1129      */
1130     int i;
1131 
1132     for (i = 0; i < cs->num_list_regs; i++) {
1133         uint64_t lr = cs->ich_lr_el2[i];
1134 
1135         if ((lr & ICH_LR_EL2_STATE_ACTIVE_BIT) && ich_lr_vintid(lr) == irq) {
1136             return i;
1137         }
1138     }
1139 
1140     return -1;
1141 }
1142 
1143 static void icv_deactivate_irq(GICv3CPUState *cs, int idx)
1144 {
1145     /* Deactivate the interrupt in the specified list register index */
1146     uint64_t lr = cs->ich_lr_el2[idx];
1147 
1148     if (lr & ICH_LR_EL2_HW) {
1149         /* Deactivate the associated physical interrupt */
1150         int pirq = ich_lr_pintid(lr);
1151 
1152         if (pirq < INTID_SECURE) {
1153             icc_deactivate_irq(cs, pirq);
1154         }
1155     }
1156 
1157     /* Clear the 'active' part of the state, so ActivePending->Pending
1158      * and Active->Invalid.
1159      */
1160     lr &= ~ICH_LR_EL2_STATE_ACTIVE_BIT;
1161     cs->ich_lr_el2[idx] = lr;
1162 }
1163 
1164 static void icv_increment_eoicount(GICv3CPUState *cs)
1165 {
1166     /* Increment the EOICOUNT field in ICH_HCR_EL2 */
1167     int eoicount = extract64(cs->ich_hcr_el2, ICH_HCR_EL2_EOICOUNT_SHIFT,
1168                              ICH_HCR_EL2_EOICOUNT_LENGTH);
1169 
1170     cs->ich_hcr_el2 = deposit64(cs->ich_hcr_el2, ICH_HCR_EL2_EOICOUNT_SHIFT,
1171                                 ICH_HCR_EL2_EOICOUNT_LENGTH, eoicount + 1);
1172 }
1173 
1174 static int icv_drop_prio(GICv3CPUState *cs)
1175 {
1176     /* Drop the priority of the currently active virtual interrupt
1177      * (favouring group 0 if there is a set active bit at
1178      * the same priority for both group 0 and group 1).
1179      * Return the priority value for the bit we just cleared,
1180      * or 0xff if no bits were set in the AP registers at all.
1181      * Note that though the ich_apr[] are uint64_t only the low
1182      * 32 bits are actually relevant.
1183      */
1184     int i;
1185     int aprmax = 1 << (cs->vprebits - 5);
1186 
1187     assert(aprmax <= ARRAY_SIZE(cs->ich_apr[0]));
1188 
1189     for (i = 0; i < aprmax; i++) {
1190         uint64_t *papr0 = &cs->ich_apr[GICV3_G0][i];
1191         uint64_t *papr1 = &cs->ich_apr[GICV3_G1NS][i];
1192         int apr0count, apr1count;
1193 
1194         if (!*papr0 && !*papr1) {
1195             continue;
1196         }
1197 
1198         /* We can't just use the bit-twiddling hack icc_drop_prio() does
1199          * because we need to return the bit number we cleared so
1200          * it can be compared against the list register's priority field.
1201          */
1202         apr0count = ctz32(*papr0);
1203         apr1count = ctz32(*papr1);
1204 
1205         if (apr0count <= apr1count) {
1206             *papr0 &= *papr0 - 1;
1207             return (apr0count + i * 32) << (icv_min_vbpr(cs) + 1);
1208         } else {
1209             *papr1 &= *papr1 - 1;
1210             return (apr1count + i * 32) << (icv_min_vbpr(cs) + 1);
1211         }
1212     }
1213     return 0xff;
1214 }
1215 
1216 static void icv_dir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1217                           uint64_t value)
1218 {
1219     /* Deactivate interrupt */
1220     GICv3CPUState *cs = icc_cs_from_env(env);
1221     int idx;
1222     int irq = value & 0xffffff;
1223 
1224     trace_gicv3_icv_dir_write(gicv3_redist_affid(cs), value);
1225 
1226     if (irq >= GICV3_MAXIRQ) {
1227         /* Also catches special interrupt numbers and LPIs */
1228         return;
1229     }
1230 
1231     if (!icv_eoi_split(env, cs)) {
1232         return;
1233     }
1234 
1235     idx = icv_find_active(cs, irq);
1236 
1237     if (idx < 0) {
1238         /* No list register matching this, so increment the EOI count
1239          * (might trigger a maintenance interrupt)
1240          */
1241         icv_increment_eoicount(cs);
1242     } else {
1243         icv_deactivate_irq(cs, idx);
1244     }
1245 
1246     gicv3_cpuif_virt_update(cs);
1247 }
1248 
1249 static void icv_eoir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1250                            uint64_t value)
1251 {
1252     /* End of Interrupt */
1253     GICv3CPUState *cs = icc_cs_from_env(env);
1254     int irq = value & 0xffffff;
1255     int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
1256     int idx, dropprio;
1257 
1258     trace_gicv3_icv_eoir_write(ri->crm == 8 ? 0 : 1,
1259                                gicv3_redist_affid(cs), value);
1260 
1261     if (gicv3_intid_is_special(irq)) {
1262         return;
1263     }
1264 
1265     /* We implement the IMPDEF choice of "drop priority before doing
1266      * error checks" (because that lets us avoid scanning the AP
1267      * registers twice).
1268      */
1269     dropprio = icv_drop_prio(cs);
1270     if (dropprio == 0xff) {
1271         /* No active interrupt. It is CONSTRAINED UNPREDICTABLE
1272          * whether the list registers are checked in this
1273          * situation; we choose not to.
1274          */
1275         return;
1276     }
1277 
1278     idx = icv_find_active(cs, irq);
1279 
1280     if (idx < 0) {
1281         /* No valid list register corresponding to EOI ID */
1282         icv_increment_eoicount(cs);
1283     } else {
1284         uint64_t lr = cs->ich_lr_el2[idx];
1285         int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
1286         int lr_gprio = ich_lr_prio(lr) & icv_gprio_mask(cs, grp);
1287 
1288         if (thisgrp == grp && lr_gprio == dropprio) {
1289             if (!icv_eoi_split(env, cs)) {
1290                 /* Priority drop and deactivate not split: deactivate irq now */
1291                 icv_deactivate_irq(cs, idx);
1292             }
1293         }
1294     }
1295 
1296     gicv3_cpuif_virt_update(cs);
1297 }
1298 
1299 static void icc_eoir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1300                            uint64_t value)
1301 {
1302     /* End of Interrupt */
1303     GICv3CPUState *cs = icc_cs_from_env(env);
1304     int irq = value & 0xffffff;
1305     int grp;
1306     bool is_eoir0 = ri->crm == 8;
1307 
1308     if (icv_access(env, is_eoir0 ? HCR_FMO : HCR_IMO)) {
1309         icv_eoir_write(env, ri, value);
1310         return;
1311     }
1312 
1313     trace_gicv3_icc_eoir_write(is_eoir0 ? 0 : 1,
1314                                gicv3_redist_affid(cs), value);
1315 
1316     if ((irq >= cs->gic->num_irq) &&
1317         !(cs->gic->lpi_enable && (irq >= GICV3_LPI_INTID_START))) {
1318         /* This handles two cases:
1319          * 1. If software writes the ID of a spurious interrupt [ie 1020-1023]
1320          * to the GICC_EOIR, the GIC ignores that write.
1321          * 2. If software writes the number of a non-existent interrupt
1322          * this must be a subcase of "value written does not match the last
1323          * valid interrupt value read from the Interrupt Acknowledge
1324          * register" and so this is UNPREDICTABLE. We choose to ignore it.
1325          */
1326         return;
1327     }
1328 
1329     grp = icc_highest_active_group(cs);
1330     switch (grp) {
1331     case GICV3_G0:
1332         if (!is_eoir0) {
1333             return;
1334         }
1335         if (!(cs->gic->gicd_ctlr & GICD_CTLR_DS)
1336             && arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env)) {
1337             return;
1338         }
1339         break;
1340     case GICV3_G1:
1341         if (is_eoir0) {
1342             return;
1343         }
1344         if (!arm_is_secure(env)) {
1345             return;
1346         }
1347         break;
1348     case GICV3_G1NS:
1349         if (is_eoir0) {
1350             return;
1351         }
1352         if (!arm_is_el3_or_mon(env) && arm_is_secure(env)) {
1353             return;
1354         }
1355         break;
1356     default:
1357         qemu_log_mask(LOG_GUEST_ERROR,
1358                       "%s: IRQ %d isn't active\n", __func__, irq);
1359         return;
1360     }
1361 
1362     icc_drop_prio(cs, grp);
1363 
1364     if (!icc_eoi_split(env, cs)) {
1365         /* Priority drop and deactivate not split: deactivate irq now */
1366         icc_deactivate_irq(cs, irq);
1367     }
1368 }
1369 
1370 static uint64_t icc_hppir0_read(CPUARMState *env, const ARMCPRegInfo *ri)
1371 {
1372     GICv3CPUState *cs = icc_cs_from_env(env);
1373     uint64_t value;
1374 
1375     if (icv_access(env, HCR_FMO)) {
1376         return icv_hppir_read(env, ri);
1377     }
1378 
1379     value = icc_hppir0_value(cs, env);
1380     trace_gicv3_icc_hppir0_read(gicv3_redist_affid(cs), value);
1381     return value;
1382 }
1383 
1384 static uint64_t icc_hppir1_read(CPUARMState *env, const ARMCPRegInfo *ri)
1385 {
1386     GICv3CPUState *cs = icc_cs_from_env(env);
1387     uint64_t value;
1388 
1389     if (icv_access(env, HCR_IMO)) {
1390         return icv_hppir_read(env, ri);
1391     }
1392 
1393     value = icc_hppir1_value(cs, env);
1394     trace_gicv3_icc_hppir1_read(gicv3_redist_affid(cs), value);
1395     return value;
1396 }
1397 
1398 static uint64_t icc_bpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1399 {
1400     GICv3CPUState *cs = icc_cs_from_env(env);
1401     int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1;
1402     bool satinc = false;
1403     uint64_t bpr;
1404 
1405     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1406         return icv_bpr_read(env, ri);
1407     }
1408 
1409     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1410         grp = GICV3_G1NS;
1411     }
1412 
1413     if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) &&
1414         (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) {
1415         /* CBPR_EL1S means secure EL1 or AArch32 EL3 !Mon BPR1 accesses
1416          * modify BPR0
1417          */
1418         grp = GICV3_G0;
1419     }
1420 
1421     if (grp == GICV3_G1NS && arm_current_el(env) < 3 &&
1422         (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
1423         /* reads return bpr0 + 1 sat to 7, writes ignored */
1424         grp = GICV3_G0;
1425         satinc = true;
1426     }
1427 
1428     bpr = cs->icc_bpr[grp];
1429     if (satinc) {
1430         bpr++;
1431         bpr = MIN(bpr, 7);
1432     }
1433 
1434     trace_gicv3_icc_bpr_read(ri->crm == 8 ? 0 : 1, gicv3_redist_affid(cs), bpr);
1435 
1436     return bpr;
1437 }
1438 
1439 static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1440                           uint64_t value)
1441 {
1442     GICv3CPUState *cs = icc_cs_from_env(env);
1443     int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1;
1444     uint64_t minval;
1445 
1446     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1447         icv_bpr_write(env, ri, value);
1448         return;
1449     }
1450 
1451     trace_gicv3_icc_bpr_write(ri->crm == 8 ? 0 : 1,
1452                               gicv3_redist_affid(cs), value);
1453 
1454     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1455         grp = GICV3_G1NS;
1456     }
1457 
1458     if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) &&
1459         (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) {
1460         /* CBPR_EL1S means secure EL1 or AArch32 EL3 !Mon BPR1 accesses
1461          * modify BPR0
1462          */
1463         grp = GICV3_G0;
1464     }
1465 
1466     if (grp == GICV3_G1NS && arm_current_el(env) < 3 &&
1467         (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
1468         /* reads return bpr0 + 1 sat to 7, writes ignored */
1469         return;
1470     }
1471 
1472     minval = (grp == GICV3_G1NS) ? GIC_MIN_BPR_NS : GIC_MIN_BPR;
1473     if (value < minval) {
1474         value = minval;
1475     }
1476 
1477     cs->icc_bpr[grp] = value & 7;
1478     gicv3_cpuif_update(cs);
1479 }
1480 
1481 static uint64_t icc_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
1482 {
1483     GICv3CPUState *cs = icc_cs_from_env(env);
1484     uint64_t value;
1485 
1486     int regno = ri->opc2 & 3;
1487     int grp = (ri->crm & 1) ? GICV3_G1 : GICV3_G0;
1488 
1489     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1490         return icv_ap_read(env, ri);
1491     }
1492 
1493     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1494         grp = GICV3_G1NS;
1495     }
1496 
1497     value = cs->icc_apr[grp][regno];
1498 
1499     trace_gicv3_icc_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
1500     return value;
1501 }
1502 
1503 static void icc_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1504                          uint64_t value)
1505 {
1506     GICv3CPUState *cs = icc_cs_from_env(env);
1507 
1508     int regno = ri->opc2 & 3;
1509     int grp = (ri->crm & 1) ? GICV3_G1 : GICV3_G0;
1510 
1511     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1512         icv_ap_write(env, ri, value);
1513         return;
1514     }
1515 
1516     trace_gicv3_icc_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
1517 
1518     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1519         grp = GICV3_G1NS;
1520     }
1521 
1522     /* It's not possible to claim that a Non-secure interrupt is active
1523      * at a priority outside the Non-secure range (128..255), since this
1524      * would otherwise allow malicious NS code to block delivery of S interrupts
1525      * by writing a bad value to these registers.
1526      */
1527     if (grp == GICV3_G1NS && regno < 2 && arm_feature(env, ARM_FEATURE_EL3)) {
1528         return;
1529     }
1530 
1531     cs->icc_apr[grp][regno] = value & 0xFFFFFFFFU;
1532     gicv3_cpuif_update(cs);
1533 }
1534 
1535 static void icc_dir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1536                           uint64_t value)
1537 {
1538     /* Deactivate interrupt */
1539     GICv3CPUState *cs = icc_cs_from_env(env);
1540     int irq = value & 0xffffff;
1541     bool irq_is_secure, single_sec_state, irq_is_grp0;
1542     bool route_fiq_to_el3, route_irq_to_el3, route_fiq_to_el2, route_irq_to_el2;
1543 
1544     if (icv_access(env, HCR_FMO | HCR_IMO)) {
1545         icv_dir_write(env, ri, value);
1546         return;
1547     }
1548 
1549     trace_gicv3_icc_dir_write(gicv3_redist_affid(cs), value);
1550 
1551     if (irq >= cs->gic->num_irq) {
1552         /* Also catches special interrupt numbers and LPIs */
1553         return;
1554     }
1555 
1556     if (!icc_eoi_split(env, cs)) {
1557         return;
1558     }
1559 
1560     int grp = gicv3_irq_group(cs->gic, cs, irq);
1561 
1562     single_sec_state = cs->gic->gicd_ctlr & GICD_CTLR_DS;
1563     irq_is_secure = !single_sec_state && (grp != GICV3_G1NS);
1564     irq_is_grp0 = grp == GICV3_G0;
1565 
1566     /* Check whether we're allowed to deactivate this interrupt based
1567      * on its group and the current CPU state.
1568      * These checks are laid out to correspond to the spec's pseudocode.
1569      */
1570     route_fiq_to_el3 = env->cp15.scr_el3 & SCR_FIQ;
1571     route_irq_to_el3 = env->cp15.scr_el3 & SCR_IRQ;
1572     /* No need to include !IsSecure in route_*_to_el2 as it's only
1573      * tested in cases where we know !IsSecure is true.
1574      */
1575     uint64_t hcr_el2 = arm_hcr_el2_eff(env);
1576     route_fiq_to_el2 = hcr_el2 & HCR_FMO;
1577     route_irq_to_el2 = hcr_el2 & HCR_IMO;
1578 
1579     switch (arm_current_el(env)) {
1580     case 3:
1581         break;
1582     case 2:
1583         if (single_sec_state && irq_is_grp0 && !route_fiq_to_el3) {
1584             break;
1585         }
1586         if (!irq_is_secure && !irq_is_grp0 && !route_irq_to_el3) {
1587             break;
1588         }
1589         return;
1590     case 1:
1591         if (!arm_is_secure_below_el3(env)) {
1592             if (single_sec_state && irq_is_grp0 &&
1593                 !route_fiq_to_el3 && !route_fiq_to_el2) {
1594                 break;
1595             }
1596             if (!irq_is_secure && !irq_is_grp0 &&
1597                 !route_irq_to_el3 && !route_irq_to_el2) {
1598                 break;
1599             }
1600         } else {
1601             if (irq_is_grp0 && !route_fiq_to_el3) {
1602                 break;
1603             }
1604             if (!irq_is_grp0 &&
1605                 (!irq_is_secure || !single_sec_state) &&
1606                 !route_irq_to_el3) {
1607                 break;
1608             }
1609         }
1610         return;
1611     default:
1612         g_assert_not_reached();
1613     }
1614 
1615     icc_deactivate_irq(cs, irq);
1616 }
1617 
1618 static uint64_t icc_rpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1619 {
1620     GICv3CPUState *cs = icc_cs_from_env(env);
1621     int prio;
1622 
1623     if (icv_access(env, HCR_FMO | HCR_IMO)) {
1624         return icv_rpr_read(env, ri);
1625     }
1626 
1627     prio = icc_highest_active_prio(cs);
1628 
1629     if (arm_feature(env, ARM_FEATURE_EL3) &&
1630         !arm_is_secure(env) && (env->cp15.scr_el3 & SCR_FIQ)) {
1631         /* NS GIC access and Group 0 is inaccessible to NS */
1632         if ((prio & 0x80) == 0) {
1633             /* NS mustn't see priorities in the Secure half of the range */
1634             prio = 0;
1635         } else if (prio != 0xff) {
1636             /* Non-idle priority: show the Non-secure view of it */
1637             prio = (prio << 1) & 0xff;
1638         }
1639     }
1640 
1641     trace_gicv3_icc_rpr_read(gicv3_redist_affid(cs), prio);
1642     return prio;
1643 }
1644 
1645 static void icc_generate_sgi(CPUARMState *env, GICv3CPUState *cs,
1646                              uint64_t value, int grp, bool ns)
1647 {
1648     GICv3State *s = cs->gic;
1649 
1650     /* Extract Aff3/Aff2/Aff1 and shift into the bottom 24 bits */
1651     uint64_t aff = extract64(value, 48, 8) << 16 |
1652         extract64(value, 32, 8) << 8 |
1653         extract64(value, 16, 8);
1654     uint32_t targetlist = extract64(value, 0, 16);
1655     uint32_t irq = extract64(value, 24, 4);
1656     bool irm = extract64(value, 40, 1);
1657     int i;
1658 
1659     if (grp == GICV3_G1 && s->gicd_ctlr & GICD_CTLR_DS) {
1660         /* If GICD_CTLR.DS == 1, the Distributor treats Secure Group 1
1661          * interrupts as Group 0 interrupts and must send Secure Group 0
1662          * interrupts to the target CPUs.
1663          */
1664         grp = GICV3_G0;
1665     }
1666 
1667     trace_gicv3_icc_generate_sgi(gicv3_redist_affid(cs), irq, irm,
1668                                  aff, targetlist);
1669 
1670     for (i = 0; i < s->num_cpu; i++) {
1671         GICv3CPUState *ocs = &s->cpu[i];
1672 
1673         if (irm) {
1674             /* IRM == 1 : route to all CPUs except self */
1675             if (cs == ocs) {
1676                 continue;
1677             }
1678         } else {
1679             /* IRM == 0 : route to Aff3.Aff2.Aff1.n for all n in [0..15]
1680              * where the corresponding bit is set in targetlist
1681              */
1682             int aff0;
1683 
1684             if (ocs->gicr_typer >> 40 != aff) {
1685                 continue;
1686             }
1687             aff0 = extract64(ocs->gicr_typer, 32, 8);
1688             if (aff0 > 15 || extract32(targetlist, aff0, 1) == 0) {
1689                 continue;
1690             }
1691         }
1692 
1693         /* The redistributor will check against its own GICR_NSACR as needed */
1694         gicv3_redist_send_sgi(ocs, grp, irq, ns);
1695     }
1696 }
1697 
1698 static void icc_sgi0r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1699                            uint64_t value)
1700 {
1701     /* Generate Secure Group 0 SGI. */
1702     GICv3CPUState *cs = icc_cs_from_env(env);
1703     bool ns = !arm_is_secure(env);
1704 
1705     icc_generate_sgi(env, cs, value, GICV3_G0, ns);
1706 }
1707 
1708 static void icc_sgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1709                            uint64_t value)
1710 {
1711     /* Generate Group 1 SGI for the current Security state */
1712     GICv3CPUState *cs = icc_cs_from_env(env);
1713     int grp;
1714     bool ns = !arm_is_secure(env);
1715 
1716     grp = ns ? GICV3_G1NS : GICV3_G1;
1717     icc_generate_sgi(env, cs, value, grp, ns);
1718 }
1719 
1720 static void icc_asgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1721                              uint64_t value)
1722 {
1723     /* Generate Group 1 SGI for the Security state that is not
1724      * the current state
1725      */
1726     GICv3CPUState *cs = icc_cs_from_env(env);
1727     int grp;
1728     bool ns = !arm_is_secure(env);
1729 
1730     grp = ns ? GICV3_G1 : GICV3_G1NS;
1731     icc_generate_sgi(env, cs, value, grp, ns);
1732 }
1733 
1734 static uint64_t icc_igrpen_read(CPUARMState *env, const ARMCPRegInfo *ri)
1735 {
1736     GICv3CPUState *cs = icc_cs_from_env(env);
1737     int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0;
1738     uint64_t value;
1739 
1740     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1741         return icv_igrpen_read(env, ri);
1742     }
1743 
1744     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1745         grp = GICV3_G1NS;
1746     }
1747 
1748     value = cs->icc_igrpen[grp];
1749     trace_gicv3_icc_igrpen_read(ri->opc2 & 1 ? 1 : 0,
1750                                 gicv3_redist_affid(cs), value);
1751     return value;
1752 }
1753 
1754 static void icc_igrpen_write(CPUARMState *env, const ARMCPRegInfo *ri,
1755                              uint64_t value)
1756 {
1757     GICv3CPUState *cs = icc_cs_from_env(env);
1758     int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0;
1759 
1760     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1761         icv_igrpen_write(env, ri, value);
1762         return;
1763     }
1764 
1765     trace_gicv3_icc_igrpen_write(ri->opc2 & 1 ? 1 : 0,
1766                                  gicv3_redist_affid(cs), value);
1767 
1768     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1769         grp = GICV3_G1NS;
1770     }
1771 
1772     cs->icc_igrpen[grp] = value & ICC_IGRPEN_ENABLE;
1773     gicv3_cpuif_update(cs);
1774 }
1775 
1776 static uint64_t icc_igrpen1_el3_read(CPUARMState *env, const ARMCPRegInfo *ri)
1777 {
1778     GICv3CPUState *cs = icc_cs_from_env(env);
1779     uint64_t value;
1780 
1781     /* IGRPEN1_EL3 bits 0 and 1 are r/w aliases into IGRPEN1_EL1 NS and S */
1782     value = cs->icc_igrpen[GICV3_G1NS] | (cs->icc_igrpen[GICV3_G1] << 1);
1783     trace_gicv3_icc_igrpen1_el3_read(gicv3_redist_affid(cs), value);
1784     return value;
1785 }
1786 
1787 static void icc_igrpen1_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
1788                                   uint64_t value)
1789 {
1790     GICv3CPUState *cs = icc_cs_from_env(env);
1791 
1792     trace_gicv3_icc_igrpen1_el3_write(gicv3_redist_affid(cs), value);
1793 
1794     /* IGRPEN1_EL3 bits 0 and 1 are r/w aliases into IGRPEN1_EL1 NS and S */
1795     cs->icc_igrpen[GICV3_G1NS] = extract32(value, 0, 1);
1796     cs->icc_igrpen[GICV3_G1] = extract32(value, 1, 1);
1797     gicv3_cpuif_update(cs);
1798 }
1799 
1800 static uint64_t icc_ctlr_el1_read(CPUARMState *env, const ARMCPRegInfo *ri)
1801 {
1802     GICv3CPUState *cs = icc_cs_from_env(env);
1803     int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S;
1804     uint64_t value;
1805 
1806     if (icv_access(env, HCR_FMO | HCR_IMO)) {
1807         return icv_ctlr_read(env, ri);
1808     }
1809 
1810     value = cs->icc_ctlr_el1[bank];
1811     trace_gicv3_icc_ctlr_read(gicv3_redist_affid(cs), value);
1812     return value;
1813 }
1814 
1815 static void icc_ctlr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
1816                                uint64_t value)
1817 {
1818     GICv3CPUState *cs = icc_cs_from_env(env);
1819     int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S;
1820     uint64_t mask;
1821 
1822     if (icv_access(env, HCR_FMO | HCR_IMO)) {
1823         icv_ctlr_write(env, ri, value);
1824         return;
1825     }
1826 
1827     trace_gicv3_icc_ctlr_write(gicv3_redist_affid(cs), value);
1828 
1829     /* Only CBPR and EOIMODE can be RW;
1830      * for us PMHE is RAZ/WI (we don't implement 1-of-N interrupts or
1831      * the asseciated priority-based routing of them);
1832      * if EL3 is implemented and GICD_CTLR.DS == 0, then PMHE and CBPR are RO.
1833      */
1834     if (arm_feature(env, ARM_FEATURE_EL3) &&
1835         ((cs->gic->gicd_ctlr & GICD_CTLR_DS) == 0)) {
1836         mask = ICC_CTLR_EL1_EOIMODE;
1837     } else {
1838         mask = ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE;
1839     }
1840 
1841     cs->icc_ctlr_el1[bank] &= ~mask;
1842     cs->icc_ctlr_el1[bank] |= (value & mask);
1843     gicv3_cpuif_update(cs);
1844 }
1845 
1846 
1847 static uint64_t icc_ctlr_el3_read(CPUARMState *env, const ARMCPRegInfo *ri)
1848 {
1849     GICv3CPUState *cs = icc_cs_from_env(env);
1850     uint64_t value;
1851 
1852     value = cs->icc_ctlr_el3;
1853     if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) {
1854         value |= ICC_CTLR_EL3_EOIMODE_EL1NS;
1855     }
1856     if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) {
1857         value |= ICC_CTLR_EL3_CBPR_EL1NS;
1858     }
1859     if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) {
1860         value |= ICC_CTLR_EL3_EOIMODE_EL1S;
1861     }
1862     if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) {
1863         value |= ICC_CTLR_EL3_CBPR_EL1S;
1864     }
1865 
1866     trace_gicv3_icc_ctlr_el3_read(gicv3_redist_affid(cs), value);
1867     return value;
1868 }
1869 
1870 static void icc_ctlr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
1871                                uint64_t value)
1872 {
1873     GICv3CPUState *cs = icc_cs_from_env(env);
1874     uint64_t mask;
1875 
1876     trace_gicv3_icc_ctlr_el3_write(gicv3_redist_affid(cs), value);
1877 
1878     /* *_EL1NS and *_EL1S bits are aliases into the ICC_CTLR_EL1 bits. */
1879     cs->icc_ctlr_el1[GICV3_NS] &= ~(ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE);
1880     if (value & ICC_CTLR_EL3_EOIMODE_EL1NS) {
1881         cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_EOIMODE;
1882     }
1883     if (value & ICC_CTLR_EL3_CBPR_EL1NS) {
1884         cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_CBPR;
1885     }
1886 
1887     cs->icc_ctlr_el1[GICV3_S] &= ~(ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE);
1888     if (value & ICC_CTLR_EL3_EOIMODE_EL1S) {
1889         cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_EOIMODE;
1890     }
1891     if (value & ICC_CTLR_EL3_CBPR_EL1S) {
1892         cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_CBPR;
1893     }
1894 
1895     /* The only bit stored in icc_ctlr_el3 which is writeable is EOIMODE_EL3: */
1896     mask = ICC_CTLR_EL3_EOIMODE_EL3;
1897 
1898     cs->icc_ctlr_el3 &= ~mask;
1899     cs->icc_ctlr_el3 |= (value & mask);
1900     gicv3_cpuif_update(cs);
1901 }
1902 
1903 static CPAccessResult gicv3_irqfiq_access(CPUARMState *env,
1904                                           const ARMCPRegInfo *ri, bool isread)
1905 {
1906     CPAccessResult r = CP_ACCESS_OK;
1907     GICv3CPUState *cs = icc_cs_from_env(env);
1908     int el = arm_current_el(env);
1909 
1910     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TC) &&
1911         el == 1 && !arm_is_secure_below_el3(env)) {
1912         /* Takes priority over a possible EL3 trap */
1913         return CP_ACCESS_TRAP_EL2;
1914     }
1915 
1916     if ((env->cp15.scr_el3 & (SCR_FIQ | SCR_IRQ)) == (SCR_FIQ | SCR_IRQ)) {
1917         switch (el) {
1918         case 1:
1919             /* Note that arm_hcr_el2_eff takes secure state into account.  */
1920             if ((arm_hcr_el2_eff(env) & (HCR_IMO | HCR_FMO)) == 0) {
1921                 r = CP_ACCESS_TRAP_EL3;
1922             }
1923             break;
1924         case 2:
1925             r = CP_ACCESS_TRAP_EL3;
1926             break;
1927         case 3:
1928             if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
1929                 r = CP_ACCESS_TRAP_EL3;
1930             }
1931             break;
1932         default:
1933             g_assert_not_reached();
1934         }
1935     }
1936 
1937     if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
1938         r = CP_ACCESS_TRAP;
1939     }
1940     return r;
1941 }
1942 
1943 static CPAccessResult gicv3_dir_access(CPUARMState *env,
1944                                        const ARMCPRegInfo *ri, bool isread)
1945 {
1946     GICv3CPUState *cs = icc_cs_from_env(env);
1947 
1948     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TDIR) &&
1949         arm_current_el(env) == 1 && !arm_is_secure_below_el3(env)) {
1950         /* Takes priority over a possible EL3 trap */
1951         return CP_ACCESS_TRAP_EL2;
1952     }
1953 
1954     return gicv3_irqfiq_access(env, ri, isread);
1955 }
1956 
1957 static CPAccessResult gicv3_sgi_access(CPUARMState *env,
1958                                        const ARMCPRegInfo *ri, bool isread)
1959 {
1960     if (arm_current_el(env) == 1 &&
1961         (arm_hcr_el2_eff(env) & (HCR_IMO | HCR_FMO)) != 0) {
1962         /* Takes priority over a possible EL3 trap */
1963         return CP_ACCESS_TRAP_EL2;
1964     }
1965 
1966     return gicv3_irqfiq_access(env, ri, isread);
1967 }
1968 
1969 static CPAccessResult gicv3_fiq_access(CPUARMState *env,
1970                                        const ARMCPRegInfo *ri, bool isread)
1971 {
1972     CPAccessResult r = CP_ACCESS_OK;
1973     GICv3CPUState *cs = icc_cs_from_env(env);
1974     int el = arm_current_el(env);
1975 
1976     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TALL0) &&
1977         el == 1 && !arm_is_secure_below_el3(env)) {
1978         /* Takes priority over a possible EL3 trap */
1979         return CP_ACCESS_TRAP_EL2;
1980     }
1981 
1982     if (env->cp15.scr_el3 & SCR_FIQ) {
1983         switch (el) {
1984         case 1:
1985             if ((arm_hcr_el2_eff(env) & HCR_FMO) == 0) {
1986                 r = CP_ACCESS_TRAP_EL3;
1987             }
1988             break;
1989         case 2:
1990             r = CP_ACCESS_TRAP_EL3;
1991             break;
1992         case 3:
1993             if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
1994                 r = CP_ACCESS_TRAP_EL3;
1995             }
1996             break;
1997         default:
1998             g_assert_not_reached();
1999         }
2000     }
2001 
2002     if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
2003         r = CP_ACCESS_TRAP;
2004     }
2005     return r;
2006 }
2007 
2008 static CPAccessResult gicv3_irq_access(CPUARMState *env,
2009                                        const ARMCPRegInfo *ri, bool isread)
2010 {
2011     CPAccessResult r = CP_ACCESS_OK;
2012     GICv3CPUState *cs = icc_cs_from_env(env);
2013     int el = arm_current_el(env);
2014 
2015     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TALL1) &&
2016         el == 1 && !arm_is_secure_below_el3(env)) {
2017         /* Takes priority over a possible EL3 trap */
2018         return CP_ACCESS_TRAP_EL2;
2019     }
2020 
2021     if (env->cp15.scr_el3 & SCR_IRQ) {
2022         switch (el) {
2023         case 1:
2024             if ((arm_hcr_el2_eff(env) & HCR_IMO) == 0) {
2025                 r = CP_ACCESS_TRAP_EL3;
2026             }
2027             break;
2028         case 2:
2029             r = CP_ACCESS_TRAP_EL3;
2030             break;
2031         case 3:
2032             if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
2033                 r = CP_ACCESS_TRAP_EL3;
2034             }
2035             break;
2036         default:
2037             g_assert_not_reached();
2038         }
2039     }
2040 
2041     if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
2042         r = CP_ACCESS_TRAP;
2043     }
2044     return r;
2045 }
2046 
2047 static void icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2048 {
2049     GICv3CPUState *cs = icc_cs_from_env(env);
2050 
2051     cs->icc_ctlr_el1[GICV3_S] = ICC_CTLR_EL1_A3V |
2052         (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
2053         (7 << ICC_CTLR_EL1_PRIBITS_SHIFT);
2054     cs->icc_ctlr_el1[GICV3_NS] = ICC_CTLR_EL1_A3V |
2055         (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
2056         (7 << ICC_CTLR_EL1_PRIBITS_SHIFT);
2057     cs->icc_pmr_el1 = 0;
2058     cs->icc_bpr[GICV3_G0] = GIC_MIN_BPR;
2059     cs->icc_bpr[GICV3_G1] = GIC_MIN_BPR;
2060     cs->icc_bpr[GICV3_G1NS] = GIC_MIN_BPR_NS;
2061     memset(cs->icc_apr, 0, sizeof(cs->icc_apr));
2062     memset(cs->icc_igrpen, 0, sizeof(cs->icc_igrpen));
2063     cs->icc_ctlr_el3 = ICC_CTLR_EL3_NDS | ICC_CTLR_EL3_A3V |
2064         (1 << ICC_CTLR_EL3_IDBITS_SHIFT) |
2065         (7 << ICC_CTLR_EL3_PRIBITS_SHIFT);
2066 
2067     memset(cs->ich_apr, 0, sizeof(cs->ich_apr));
2068     cs->ich_hcr_el2 = 0;
2069     memset(cs->ich_lr_el2, 0, sizeof(cs->ich_lr_el2));
2070     cs->ich_vmcr_el2 = ICH_VMCR_EL2_VFIQEN |
2071         ((icv_min_vbpr(cs) + 1) << ICH_VMCR_EL2_VBPR1_SHIFT) |
2072         (icv_min_vbpr(cs) << ICH_VMCR_EL2_VBPR0_SHIFT);
2073 }
2074 
2075 static const ARMCPRegInfo gicv3_cpuif_reginfo[] = {
2076     { .name = "ICC_PMR_EL1", .state = ARM_CP_STATE_BOTH,
2077       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 6, .opc2 = 0,
2078       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2079       .access = PL1_RW, .accessfn = gicv3_irqfiq_access,
2080       .readfn = icc_pmr_read,
2081       .writefn = icc_pmr_write,
2082       /* We hang the whole cpu interface reset routine off here
2083        * rather than parcelling it out into one little function
2084        * per register
2085        */
2086       .resetfn = icc_reset,
2087     },
2088     { .name = "ICC_IAR0_EL1", .state = ARM_CP_STATE_BOTH,
2089       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 0,
2090       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2091       .access = PL1_R, .accessfn = gicv3_fiq_access,
2092       .readfn = icc_iar0_read,
2093     },
2094     { .name = "ICC_EOIR0_EL1", .state = ARM_CP_STATE_BOTH,
2095       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 1,
2096       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2097       .access = PL1_W, .accessfn = gicv3_fiq_access,
2098       .writefn = icc_eoir_write,
2099     },
2100     { .name = "ICC_HPPIR0_EL1", .state = ARM_CP_STATE_BOTH,
2101       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 2,
2102       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2103       .access = PL1_R, .accessfn = gicv3_fiq_access,
2104       .readfn = icc_hppir0_read,
2105     },
2106     { .name = "ICC_BPR0_EL1", .state = ARM_CP_STATE_BOTH,
2107       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 3,
2108       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2109       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2110       .readfn = icc_bpr_read,
2111       .writefn = icc_bpr_write,
2112     },
2113     { .name = "ICC_AP0R0_EL1", .state = ARM_CP_STATE_BOTH,
2114       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 4,
2115       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2116       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2117       .readfn = icc_ap_read,
2118       .writefn = icc_ap_write,
2119     },
2120     { .name = "ICC_AP0R1_EL1", .state = ARM_CP_STATE_BOTH,
2121       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 5,
2122       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2123       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2124       .readfn = icc_ap_read,
2125       .writefn = icc_ap_write,
2126     },
2127     { .name = "ICC_AP0R2_EL1", .state = ARM_CP_STATE_BOTH,
2128       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 6,
2129       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2130       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2131       .readfn = icc_ap_read,
2132       .writefn = icc_ap_write,
2133     },
2134     { .name = "ICC_AP0R3_EL1", .state = ARM_CP_STATE_BOTH,
2135       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 7,
2136       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2137       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2138       .readfn = icc_ap_read,
2139       .writefn = icc_ap_write,
2140     },
2141     /* All the ICC_AP1R*_EL1 registers are banked */
2142     { .name = "ICC_AP1R0_EL1", .state = ARM_CP_STATE_BOTH,
2143       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 0,
2144       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2145       .access = PL1_RW, .accessfn = gicv3_irq_access,
2146       .readfn = icc_ap_read,
2147       .writefn = icc_ap_write,
2148     },
2149     { .name = "ICC_AP1R1_EL1", .state = ARM_CP_STATE_BOTH,
2150       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 1,
2151       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2152       .access = PL1_RW, .accessfn = gicv3_irq_access,
2153       .readfn = icc_ap_read,
2154       .writefn = icc_ap_write,
2155     },
2156     { .name = "ICC_AP1R2_EL1", .state = ARM_CP_STATE_BOTH,
2157       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 2,
2158       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2159       .access = PL1_RW, .accessfn = gicv3_irq_access,
2160       .readfn = icc_ap_read,
2161       .writefn = icc_ap_write,
2162     },
2163     { .name = "ICC_AP1R3_EL1", .state = ARM_CP_STATE_BOTH,
2164       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 3,
2165       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2166       .access = PL1_RW, .accessfn = gicv3_irq_access,
2167       .readfn = icc_ap_read,
2168       .writefn = icc_ap_write,
2169     },
2170     { .name = "ICC_DIR_EL1", .state = ARM_CP_STATE_BOTH,
2171       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 1,
2172       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2173       .access = PL1_W, .accessfn = gicv3_dir_access,
2174       .writefn = icc_dir_write,
2175     },
2176     { .name = "ICC_RPR_EL1", .state = ARM_CP_STATE_BOTH,
2177       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 3,
2178       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2179       .access = PL1_R, .accessfn = gicv3_irqfiq_access,
2180       .readfn = icc_rpr_read,
2181     },
2182     { .name = "ICC_SGI1R_EL1", .state = ARM_CP_STATE_AA64,
2183       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 5,
2184       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2185       .access = PL1_W, .accessfn = gicv3_sgi_access,
2186       .writefn = icc_sgi1r_write,
2187     },
2188     { .name = "ICC_SGI1R",
2189       .cp = 15, .opc1 = 0, .crm = 12,
2190       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2191       .access = PL1_W, .accessfn = gicv3_sgi_access,
2192       .writefn = icc_sgi1r_write,
2193     },
2194     { .name = "ICC_ASGI1R_EL1", .state = ARM_CP_STATE_AA64,
2195       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 6,
2196       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2197       .access = PL1_W, .accessfn = gicv3_sgi_access,
2198       .writefn = icc_asgi1r_write,
2199     },
2200     { .name = "ICC_ASGI1R",
2201       .cp = 15, .opc1 = 1, .crm = 12,
2202       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2203       .access = PL1_W, .accessfn = gicv3_sgi_access,
2204       .writefn = icc_asgi1r_write,
2205     },
2206     { .name = "ICC_SGI0R_EL1", .state = ARM_CP_STATE_AA64,
2207       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 7,
2208       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2209       .access = PL1_W, .accessfn = gicv3_sgi_access,
2210       .writefn = icc_sgi0r_write,
2211     },
2212     { .name = "ICC_SGI0R",
2213       .cp = 15, .opc1 = 2, .crm = 12,
2214       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2215       .access = PL1_W, .accessfn = gicv3_sgi_access,
2216       .writefn = icc_sgi0r_write,
2217     },
2218     { .name = "ICC_IAR1_EL1", .state = ARM_CP_STATE_BOTH,
2219       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 0,
2220       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2221       .access = PL1_R, .accessfn = gicv3_irq_access,
2222       .readfn = icc_iar1_read,
2223     },
2224     { .name = "ICC_EOIR1_EL1", .state = ARM_CP_STATE_BOTH,
2225       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 1,
2226       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2227       .access = PL1_W, .accessfn = gicv3_irq_access,
2228       .writefn = icc_eoir_write,
2229     },
2230     { .name = "ICC_HPPIR1_EL1", .state = ARM_CP_STATE_BOTH,
2231       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 2,
2232       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2233       .access = PL1_R, .accessfn = gicv3_irq_access,
2234       .readfn = icc_hppir1_read,
2235     },
2236     /* This register is banked */
2237     { .name = "ICC_BPR1_EL1", .state = ARM_CP_STATE_BOTH,
2238       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 3,
2239       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2240       .access = PL1_RW, .accessfn = gicv3_irq_access,
2241       .readfn = icc_bpr_read,
2242       .writefn = icc_bpr_write,
2243     },
2244     /* This register is banked */
2245     { .name = "ICC_CTLR_EL1", .state = ARM_CP_STATE_BOTH,
2246       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 4,
2247       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2248       .access = PL1_RW, .accessfn = gicv3_irqfiq_access,
2249       .readfn = icc_ctlr_el1_read,
2250       .writefn = icc_ctlr_el1_write,
2251     },
2252     { .name = "ICC_SRE_EL1", .state = ARM_CP_STATE_BOTH,
2253       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 5,
2254       .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2255       .access = PL1_RW,
2256       /* We don't support IRQ/FIQ bypass and system registers are
2257        * always enabled, so all our bits are RAZ/WI or RAO/WI.
2258        * This register is banked but since it's constant we don't
2259        * need to do anything special.
2260        */
2261       .resetvalue = 0x7,
2262     },
2263     { .name = "ICC_IGRPEN0_EL1", .state = ARM_CP_STATE_BOTH,
2264       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 6,
2265       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2266       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2267       .readfn = icc_igrpen_read,
2268       .writefn = icc_igrpen_write,
2269     },
2270     /* This register is banked */
2271     { .name = "ICC_IGRPEN1_EL1", .state = ARM_CP_STATE_BOTH,
2272       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 7,
2273       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2274       .access = PL1_RW, .accessfn = gicv3_irq_access,
2275       .readfn = icc_igrpen_read,
2276       .writefn = icc_igrpen_write,
2277     },
2278     { .name = "ICC_SRE_EL2", .state = ARM_CP_STATE_BOTH,
2279       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 5,
2280       .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2281       .access = PL2_RW,
2282       /* We don't support IRQ/FIQ bypass and system registers are
2283        * always enabled, so all our bits are RAZ/WI or RAO/WI.
2284        */
2285       .resetvalue = 0xf,
2286     },
2287     { .name = "ICC_CTLR_EL3", .state = ARM_CP_STATE_BOTH,
2288       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 4,
2289       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2290       .access = PL3_RW,
2291       .readfn = icc_ctlr_el3_read,
2292       .writefn = icc_ctlr_el3_write,
2293     },
2294     { .name = "ICC_SRE_EL3", .state = ARM_CP_STATE_BOTH,
2295       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 5,
2296       .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2297       .access = PL3_RW,
2298       /* We don't support IRQ/FIQ bypass and system registers are
2299        * always enabled, so all our bits are RAZ/WI or RAO/WI.
2300        */
2301       .resetvalue = 0xf,
2302     },
2303     { .name = "ICC_IGRPEN1_EL3", .state = ARM_CP_STATE_BOTH,
2304       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 7,
2305       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2306       .access = PL3_RW,
2307       .readfn = icc_igrpen1_el3_read,
2308       .writefn = icc_igrpen1_el3_write,
2309     },
2310     REGINFO_SENTINEL
2311 };
2312 
2313 static uint64_t ich_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2314 {
2315     GICv3CPUState *cs = icc_cs_from_env(env);
2316     int regno = ri->opc2 & 3;
2317     int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
2318     uint64_t value;
2319 
2320     value = cs->ich_apr[grp][regno];
2321     trace_gicv3_ich_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
2322     return value;
2323 }
2324 
2325 static void ich_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2326                          uint64_t value)
2327 {
2328     GICv3CPUState *cs = icc_cs_from_env(env);
2329     int regno = ri->opc2 & 3;
2330     int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
2331 
2332     trace_gicv3_ich_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
2333 
2334     cs->ich_apr[grp][regno] = value & 0xFFFFFFFFU;
2335     gicv3_cpuif_virt_update(cs);
2336 }
2337 
2338 static uint64_t ich_hcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2339 {
2340     GICv3CPUState *cs = icc_cs_from_env(env);
2341     uint64_t value = cs->ich_hcr_el2;
2342 
2343     trace_gicv3_ich_hcr_read(gicv3_redist_affid(cs), value);
2344     return value;
2345 }
2346 
2347 static void ich_hcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2348                           uint64_t value)
2349 {
2350     GICv3CPUState *cs = icc_cs_from_env(env);
2351 
2352     trace_gicv3_ich_hcr_write(gicv3_redist_affid(cs), value);
2353 
2354     value &= ICH_HCR_EL2_EN | ICH_HCR_EL2_UIE | ICH_HCR_EL2_LRENPIE |
2355         ICH_HCR_EL2_NPIE | ICH_HCR_EL2_VGRP0EIE | ICH_HCR_EL2_VGRP0DIE |
2356         ICH_HCR_EL2_VGRP1EIE | ICH_HCR_EL2_VGRP1DIE | ICH_HCR_EL2_TC |
2357         ICH_HCR_EL2_TALL0 | ICH_HCR_EL2_TALL1 | ICH_HCR_EL2_TSEI |
2358         ICH_HCR_EL2_TDIR | ICH_HCR_EL2_EOICOUNT_MASK;
2359 
2360     cs->ich_hcr_el2 = value;
2361     gicv3_cpuif_virt_update(cs);
2362 }
2363 
2364 static uint64_t ich_vmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2365 {
2366     GICv3CPUState *cs = icc_cs_from_env(env);
2367     uint64_t value = cs->ich_vmcr_el2;
2368 
2369     trace_gicv3_ich_vmcr_read(gicv3_redist_affid(cs), value);
2370     return value;
2371 }
2372 
2373 static void ich_vmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2374                          uint64_t value)
2375 {
2376     GICv3CPUState *cs = icc_cs_from_env(env);
2377 
2378     trace_gicv3_ich_vmcr_write(gicv3_redist_affid(cs), value);
2379 
2380     value &= ICH_VMCR_EL2_VENG0 | ICH_VMCR_EL2_VENG1 | ICH_VMCR_EL2_VCBPR |
2381         ICH_VMCR_EL2_VEOIM | ICH_VMCR_EL2_VBPR1_MASK |
2382         ICH_VMCR_EL2_VBPR0_MASK | ICH_VMCR_EL2_VPMR_MASK;
2383     value |= ICH_VMCR_EL2_VFIQEN;
2384 
2385     cs->ich_vmcr_el2 = value;
2386     /* Enforce "writing BPRs to less than minimum sets them to the minimum"
2387      * by reading and writing back the fields.
2388      */
2389     write_vbpr(cs, GICV3_G0, read_vbpr(cs, GICV3_G0));
2390     write_vbpr(cs, GICV3_G1, read_vbpr(cs, GICV3_G1));
2391 
2392     gicv3_cpuif_virt_update(cs);
2393 }
2394 
2395 static uint64_t ich_lr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2396 {
2397     GICv3CPUState *cs = icc_cs_from_env(env);
2398     int regno = ri->opc2 | ((ri->crm & 1) << 3);
2399     uint64_t value;
2400 
2401     /* This read function handles all of:
2402      * 64-bit reads of the whole LR
2403      * 32-bit reads of the low half of the LR
2404      * 32-bit reads of the high half of the LR
2405      */
2406     if (ri->state == ARM_CP_STATE_AA32) {
2407         if (ri->crm >= 14) {
2408             value = extract64(cs->ich_lr_el2[regno], 32, 32);
2409             trace_gicv3_ich_lrc_read(regno, gicv3_redist_affid(cs), value);
2410         } else {
2411             value = extract64(cs->ich_lr_el2[regno], 0, 32);
2412             trace_gicv3_ich_lr32_read(regno, gicv3_redist_affid(cs), value);
2413         }
2414     } else {
2415         value = cs->ich_lr_el2[regno];
2416         trace_gicv3_ich_lr_read(regno, gicv3_redist_affid(cs), value);
2417     }
2418 
2419     return value;
2420 }
2421 
2422 static void ich_lr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2423                          uint64_t value)
2424 {
2425     GICv3CPUState *cs = icc_cs_from_env(env);
2426     int regno = ri->opc2 | ((ri->crm & 1) << 3);
2427 
2428     /* This write function handles all of:
2429      * 64-bit writes to the whole LR
2430      * 32-bit writes to the low half of the LR
2431      * 32-bit writes to the high half of the LR
2432      */
2433     if (ri->state == ARM_CP_STATE_AA32) {
2434         if (ri->crm >= 14) {
2435             trace_gicv3_ich_lrc_write(regno, gicv3_redist_affid(cs), value);
2436             value = deposit64(cs->ich_lr_el2[regno], 32, 32, value);
2437         } else {
2438             trace_gicv3_ich_lr32_write(regno, gicv3_redist_affid(cs), value);
2439             value = deposit64(cs->ich_lr_el2[regno], 0, 32, value);
2440         }
2441     } else {
2442         trace_gicv3_ich_lr_write(regno, gicv3_redist_affid(cs), value);
2443     }
2444 
2445     /* Enforce RES0 bits in priority field */
2446     if (cs->vpribits < 8) {
2447         value = deposit64(value, ICH_LR_EL2_PRIORITY_SHIFT,
2448                           8 - cs->vpribits, 0);
2449     }
2450 
2451     cs->ich_lr_el2[regno] = value;
2452     gicv3_cpuif_virt_update(cs);
2453 }
2454 
2455 static uint64_t ich_vtr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2456 {
2457     GICv3CPUState *cs = icc_cs_from_env(env);
2458     uint64_t value;
2459 
2460     value = ((cs->num_list_regs - 1) << ICH_VTR_EL2_LISTREGS_SHIFT)
2461         | ICH_VTR_EL2_TDS | ICH_VTR_EL2_NV4 | ICH_VTR_EL2_A3V
2462         | (1 << ICH_VTR_EL2_IDBITS_SHIFT)
2463         | ((cs->vprebits - 1) << ICH_VTR_EL2_PREBITS_SHIFT)
2464         | ((cs->vpribits - 1) << ICH_VTR_EL2_PRIBITS_SHIFT);
2465 
2466     trace_gicv3_ich_vtr_read(gicv3_redist_affid(cs), value);
2467     return value;
2468 }
2469 
2470 static uint64_t ich_misr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2471 {
2472     GICv3CPUState *cs = icc_cs_from_env(env);
2473     uint64_t value = maintenance_interrupt_state(cs);
2474 
2475     trace_gicv3_ich_misr_read(gicv3_redist_affid(cs), value);
2476     return value;
2477 }
2478 
2479 static uint64_t ich_eisr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2480 {
2481     GICv3CPUState *cs = icc_cs_from_env(env);
2482     uint64_t value = eoi_maintenance_interrupt_state(cs, NULL);
2483 
2484     trace_gicv3_ich_eisr_read(gicv3_redist_affid(cs), value);
2485     return value;
2486 }
2487 
2488 static uint64_t ich_elrsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2489 {
2490     GICv3CPUState *cs = icc_cs_from_env(env);
2491     uint64_t value = 0;
2492     int i;
2493 
2494     for (i = 0; i < cs->num_list_regs; i++) {
2495         uint64_t lr = cs->ich_lr_el2[i];
2496 
2497         if ((lr & ICH_LR_EL2_STATE_MASK) == 0 &&
2498             ((lr & ICH_LR_EL2_HW) != 0 || (lr & ICH_LR_EL2_EOI) == 0)) {
2499             value |= (1 << i);
2500         }
2501     }
2502 
2503     trace_gicv3_ich_elrsr_read(gicv3_redist_affid(cs), value);
2504     return value;
2505 }
2506 
2507 static const ARMCPRegInfo gicv3_cpuif_hcr_reginfo[] = {
2508     { .name = "ICH_AP0R0_EL2", .state = ARM_CP_STATE_BOTH,
2509       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 0,
2510       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2511       .access = PL2_RW,
2512       .readfn = ich_ap_read,
2513       .writefn = ich_ap_write,
2514     },
2515     { .name = "ICH_AP1R0_EL2", .state = ARM_CP_STATE_BOTH,
2516       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 0,
2517       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2518       .access = PL2_RW,
2519       .readfn = ich_ap_read,
2520       .writefn = ich_ap_write,
2521     },
2522     { .name = "ICH_HCR_EL2", .state = ARM_CP_STATE_BOTH,
2523       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 0,
2524       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2525       .access = PL2_RW,
2526       .readfn = ich_hcr_read,
2527       .writefn = ich_hcr_write,
2528     },
2529     { .name = "ICH_VTR_EL2", .state = ARM_CP_STATE_BOTH,
2530       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 1,
2531       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2532       .access = PL2_R,
2533       .readfn = ich_vtr_read,
2534     },
2535     { .name = "ICH_MISR_EL2", .state = ARM_CP_STATE_BOTH,
2536       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 2,
2537       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2538       .access = PL2_R,
2539       .readfn = ich_misr_read,
2540     },
2541     { .name = "ICH_EISR_EL2", .state = ARM_CP_STATE_BOTH,
2542       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 3,
2543       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2544       .access = PL2_R,
2545       .readfn = ich_eisr_read,
2546     },
2547     { .name = "ICH_ELRSR_EL2", .state = ARM_CP_STATE_BOTH,
2548       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 5,
2549       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2550       .access = PL2_R,
2551       .readfn = ich_elrsr_read,
2552     },
2553     { .name = "ICH_VMCR_EL2", .state = ARM_CP_STATE_BOTH,
2554       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 7,
2555       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2556       .access = PL2_RW,
2557       .readfn = ich_vmcr_read,
2558       .writefn = ich_vmcr_write,
2559     },
2560     REGINFO_SENTINEL
2561 };
2562 
2563 static const ARMCPRegInfo gicv3_cpuif_ich_apxr1_reginfo[] = {
2564     { .name = "ICH_AP0R1_EL2", .state = ARM_CP_STATE_BOTH,
2565       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 1,
2566       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2567       .access = PL2_RW,
2568       .readfn = ich_ap_read,
2569       .writefn = ich_ap_write,
2570     },
2571     { .name = "ICH_AP1R1_EL2", .state = ARM_CP_STATE_BOTH,
2572       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 1,
2573       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2574       .access = PL2_RW,
2575       .readfn = ich_ap_read,
2576       .writefn = ich_ap_write,
2577     },
2578     REGINFO_SENTINEL
2579 };
2580 
2581 static const ARMCPRegInfo gicv3_cpuif_ich_apxr23_reginfo[] = {
2582     { .name = "ICH_AP0R2_EL2", .state = ARM_CP_STATE_BOTH,
2583       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 2,
2584       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2585       .access = PL2_RW,
2586       .readfn = ich_ap_read,
2587       .writefn = ich_ap_write,
2588     },
2589     { .name = "ICH_AP0R3_EL2", .state = ARM_CP_STATE_BOTH,
2590       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 3,
2591       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2592       .access = PL2_RW,
2593       .readfn = ich_ap_read,
2594       .writefn = ich_ap_write,
2595     },
2596     { .name = "ICH_AP1R2_EL2", .state = ARM_CP_STATE_BOTH,
2597       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 2,
2598       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2599       .access = PL2_RW,
2600       .readfn = ich_ap_read,
2601       .writefn = ich_ap_write,
2602     },
2603     { .name = "ICH_AP1R3_EL2", .state = ARM_CP_STATE_BOTH,
2604       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 3,
2605       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2606       .access = PL2_RW,
2607       .readfn = ich_ap_read,
2608       .writefn = ich_ap_write,
2609     },
2610     REGINFO_SENTINEL
2611 };
2612 
2613 static void gicv3_cpuif_el_change_hook(ARMCPU *cpu, void *opaque)
2614 {
2615     GICv3CPUState *cs = opaque;
2616 
2617     gicv3_cpuif_update(cs);
2618 }
2619 
2620 void gicv3_init_cpuif(GICv3State *s)
2621 {
2622     /* Called from the GICv3 realize function; register our system
2623      * registers with the CPU
2624      */
2625     int i;
2626 
2627     for (i = 0; i < s->num_cpu; i++) {
2628         ARMCPU *cpu = ARM_CPU(qemu_get_cpu(i));
2629         GICv3CPUState *cs = &s->cpu[i];
2630 
2631         /* Note that we can't just use the GICv3CPUState as an opaque pointer
2632          * in define_arm_cp_regs_with_opaque(), because when we're called back
2633          * it might be with code translated by CPU 0 but run by CPU 1, in
2634          * which case we'd get the wrong value.
2635          * So instead we define the regs with no ri->opaque info, and
2636          * get back to the GICv3CPUState from the CPUARMState.
2637          */
2638         define_arm_cp_regs(cpu, gicv3_cpuif_reginfo);
2639         if (arm_feature(&cpu->env, ARM_FEATURE_EL2)
2640             && cpu->gic_num_lrs) {
2641             int j;
2642 
2643             cs->num_list_regs = cpu->gic_num_lrs;
2644             cs->vpribits = cpu->gic_vpribits;
2645             cs->vprebits = cpu->gic_vprebits;
2646 
2647             /* Check against architectural constraints: getting these
2648              * wrong would be a bug in the CPU code defining these,
2649              * and the implementation relies on them holding.
2650              */
2651             g_assert(cs->vprebits <= cs->vpribits);
2652             g_assert(cs->vprebits >= 5 && cs->vprebits <= 7);
2653             g_assert(cs->vpribits >= 5 && cs->vpribits <= 8);
2654 
2655             define_arm_cp_regs(cpu, gicv3_cpuif_hcr_reginfo);
2656 
2657             for (j = 0; j < cs->num_list_regs; j++) {
2658                 /* Note that the AArch64 LRs are 64-bit; the AArch32 LRs
2659                  * are split into two cp15 regs, LR (the low part, with the
2660                  * same encoding as the AArch64 LR) and LRC (the high part).
2661                  */
2662                 ARMCPRegInfo lr_regset[] = {
2663                     { .name = "ICH_LRn_EL2", .state = ARM_CP_STATE_BOTH,
2664                       .opc0 = 3, .opc1 = 4, .crn = 12,
2665                       .crm = 12 + (j >> 3), .opc2 = j & 7,
2666                       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2667                       .access = PL2_RW,
2668                       .readfn = ich_lr_read,
2669                       .writefn = ich_lr_write,
2670                     },
2671                     { .name = "ICH_LRCn_EL2", .state = ARM_CP_STATE_AA32,
2672                       .cp = 15, .opc1 = 4, .crn = 12,
2673                       .crm = 14 + (j >> 3), .opc2 = j & 7,
2674                       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2675                       .access = PL2_RW,
2676                       .readfn = ich_lr_read,
2677                       .writefn = ich_lr_write,
2678                     },
2679                     REGINFO_SENTINEL
2680                 };
2681                 define_arm_cp_regs(cpu, lr_regset);
2682             }
2683             if (cs->vprebits >= 6) {
2684                 define_arm_cp_regs(cpu, gicv3_cpuif_ich_apxr1_reginfo);
2685             }
2686             if (cs->vprebits == 7) {
2687                 define_arm_cp_regs(cpu, gicv3_cpuif_ich_apxr23_reginfo);
2688             }
2689         }
2690         arm_register_el_change_hook(cpu, gicv3_cpuif_el_change_hook, cs);
2691     }
2692 }
2693