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