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