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