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