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