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