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