xref: /openbmc/qemu/hw/intc/arm_gicv3_cpuif.c (revision 8193d4617cfdaec6e95c2087d9a13197dc261ef0)
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     uint64_t minval;
1392 
1393     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1394         icv_bpr_write(env, ri, value);
1395         return;
1396     }
1397 
1398     trace_gicv3_icc_bpr_write(ri->crm == 8 ? 0 : 1,
1399                               gicv3_redist_affid(cs), value);
1400 
1401     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1402         grp = GICV3_G1NS;
1403     }
1404 
1405     if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) &&
1406         (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) {
1407         /* CBPR_EL1S means secure EL1 or AArch32 EL3 !Mon BPR1 accesses
1408          * modify BPR0
1409          */
1410         grp = GICV3_G0;
1411     }
1412 
1413     if (grp == GICV3_G1NS && arm_current_el(env) < 3 &&
1414         (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
1415         /* reads return bpr0 + 1 sat to 7, writes ignored */
1416         return;
1417     }
1418 
1419     minval = (grp == GICV3_G1NS) ? GIC_MIN_BPR_NS : GIC_MIN_BPR;
1420     if (value < minval) {
1421         value = minval;
1422     }
1423 
1424     cs->icc_bpr[grp] = value & 7;
1425     gicv3_cpuif_update(cs);
1426 }
1427 
1428 static uint64_t icc_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
1429 {
1430     GICv3CPUState *cs = icc_cs_from_env(env);
1431     uint64_t value;
1432 
1433     int regno = ri->opc2 & 3;
1434     int grp = ri->crm & 1 ? GICV3_G0 : GICV3_G1;
1435 
1436     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1437         return icv_ap_read(env, ri);
1438     }
1439 
1440     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1441         grp = GICV3_G1NS;
1442     }
1443 
1444     value = cs->icc_apr[grp][regno];
1445 
1446     trace_gicv3_icc_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
1447     return value;
1448 }
1449 
1450 static void icc_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1451                          uint64_t value)
1452 {
1453     GICv3CPUState *cs = icc_cs_from_env(env);
1454 
1455     int regno = ri->opc2 & 3;
1456     int grp = ri->crm & 1 ? GICV3_G0 : GICV3_G1;
1457 
1458     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1459         icv_ap_write(env, ri, value);
1460         return;
1461     }
1462 
1463     trace_gicv3_icc_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
1464 
1465     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1466         grp = GICV3_G1NS;
1467     }
1468 
1469     /* It's not possible to claim that a Non-secure interrupt is active
1470      * at a priority outside the Non-secure range (128..255), since this
1471      * would otherwise allow malicious NS code to block delivery of S interrupts
1472      * by writing a bad value to these registers.
1473      */
1474     if (grp == GICV3_G1NS && regno < 2 && arm_feature(env, ARM_FEATURE_EL3)) {
1475         return;
1476     }
1477 
1478     cs->icc_apr[grp][regno] = value & 0xFFFFFFFFU;
1479     gicv3_cpuif_update(cs);
1480 }
1481 
1482 static void icc_dir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1483                           uint64_t value)
1484 {
1485     /* Deactivate interrupt */
1486     GICv3CPUState *cs = icc_cs_from_env(env);
1487     int irq = value & 0xffffff;
1488     bool irq_is_secure, single_sec_state, irq_is_grp0;
1489     bool route_fiq_to_el3, route_irq_to_el3, route_fiq_to_el2, route_irq_to_el2;
1490 
1491     if (icv_access(env, HCR_FMO | HCR_IMO)) {
1492         icv_dir_write(env, ri, value);
1493         return;
1494     }
1495 
1496     trace_gicv3_icc_dir_write(gicv3_redist_affid(cs), value);
1497 
1498     if (irq >= cs->gic->num_irq) {
1499         /* Also catches special interrupt numbers and LPIs */
1500         return;
1501     }
1502 
1503     if (!icc_eoi_split(env, cs)) {
1504         return;
1505     }
1506 
1507     int grp = gicv3_irq_group(cs->gic, cs, irq);
1508 
1509     single_sec_state = cs->gic->gicd_ctlr & GICD_CTLR_DS;
1510     irq_is_secure = !single_sec_state && (grp != GICV3_G1NS);
1511     irq_is_grp0 = grp == GICV3_G0;
1512 
1513     /* Check whether we're allowed to deactivate this interrupt based
1514      * on its group and the current CPU state.
1515      * These checks are laid out to correspond to the spec's pseudocode.
1516      */
1517     route_fiq_to_el3 = env->cp15.scr_el3 & SCR_FIQ;
1518     route_irq_to_el3 = env->cp15.scr_el3 & SCR_IRQ;
1519     /* No need to include !IsSecure in route_*_to_el2 as it's only
1520      * tested in cases where we know !IsSecure is true.
1521      */
1522     route_fiq_to_el2 = env->cp15.hcr_el2 & HCR_FMO;
1523     route_irq_to_el2 = env->cp15.hcr_el2 & HCR_FMO;
1524 
1525     switch (arm_current_el(env)) {
1526     case 3:
1527         break;
1528     case 2:
1529         if (single_sec_state && irq_is_grp0 && !route_fiq_to_el3) {
1530             break;
1531         }
1532         if (!irq_is_secure && !irq_is_grp0 && !route_irq_to_el3) {
1533             break;
1534         }
1535         return;
1536     case 1:
1537         if (!arm_is_secure_below_el3(env)) {
1538             if (single_sec_state && irq_is_grp0 &&
1539                 !route_fiq_to_el3 && !route_fiq_to_el2) {
1540                 break;
1541             }
1542             if (!irq_is_secure && !irq_is_grp0 &&
1543                 !route_irq_to_el3 && !route_irq_to_el2) {
1544                 break;
1545             }
1546         } else {
1547             if (irq_is_grp0 && !route_fiq_to_el3) {
1548                 break;
1549             }
1550             if (!irq_is_grp0 &&
1551                 (!irq_is_secure || !single_sec_state) &&
1552                 !route_irq_to_el3) {
1553                 break;
1554             }
1555         }
1556         return;
1557     default:
1558         g_assert_not_reached();
1559     }
1560 
1561     icc_deactivate_irq(cs, irq);
1562 }
1563 
1564 static uint64_t icc_rpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1565 {
1566     GICv3CPUState *cs = icc_cs_from_env(env);
1567     int prio;
1568 
1569     if (icv_access(env, HCR_FMO | HCR_IMO)) {
1570         return icv_rpr_read(env, ri);
1571     }
1572 
1573     prio = icc_highest_active_prio(cs);
1574 
1575     if (arm_feature(env, ARM_FEATURE_EL3) &&
1576         !arm_is_secure(env) && (env->cp15.scr_el3 & SCR_FIQ)) {
1577         /* NS GIC access and Group 0 is inaccessible to NS */
1578         if (prio & 0x80) {
1579             /* NS mustn't see priorities in the Secure half of the range */
1580             prio = 0;
1581         } else if (prio != 0xff) {
1582             /* Non-idle priority: show the Non-secure view of it */
1583             prio = (prio << 1) & 0xff;
1584         }
1585     }
1586 
1587     trace_gicv3_icc_rpr_read(gicv3_redist_affid(cs), prio);
1588     return prio;
1589 }
1590 
1591 static void icc_generate_sgi(CPUARMState *env, GICv3CPUState *cs,
1592                              uint64_t value, int grp, bool ns)
1593 {
1594     GICv3State *s = cs->gic;
1595 
1596     /* Extract Aff3/Aff2/Aff1 and shift into the bottom 24 bits */
1597     uint64_t aff = extract64(value, 48, 8) << 16 |
1598         extract64(value, 32, 8) << 8 |
1599         extract64(value, 16, 8);
1600     uint32_t targetlist = extract64(value, 0, 16);
1601     uint32_t irq = extract64(value, 24, 4);
1602     bool irm = extract64(value, 40, 1);
1603     int i;
1604 
1605     if (grp == GICV3_G1 && s->gicd_ctlr & GICD_CTLR_DS) {
1606         /* If GICD_CTLR.DS == 1, the Distributor treats Secure Group 1
1607          * interrupts as Group 0 interrupts and must send Secure Group 0
1608          * interrupts to the target CPUs.
1609          */
1610         grp = GICV3_G0;
1611     }
1612 
1613     trace_gicv3_icc_generate_sgi(gicv3_redist_affid(cs), irq, irm,
1614                                  aff, targetlist);
1615 
1616     for (i = 0; i < s->num_cpu; i++) {
1617         GICv3CPUState *ocs = &s->cpu[i];
1618 
1619         if (irm) {
1620             /* IRM == 1 : route to all CPUs except self */
1621             if (cs == ocs) {
1622                 continue;
1623             }
1624         } else {
1625             /* IRM == 0 : route to Aff3.Aff2.Aff1.n for all n in [0..15]
1626              * where the corresponding bit is set in targetlist
1627              */
1628             int aff0;
1629 
1630             if (ocs->gicr_typer >> 40 != aff) {
1631                 continue;
1632             }
1633             aff0 = extract64(ocs->gicr_typer, 32, 8);
1634             if (aff0 > 15 || extract32(targetlist, aff0, 1) == 0) {
1635                 continue;
1636             }
1637         }
1638 
1639         /* The redistributor will check against its own GICR_NSACR as needed */
1640         gicv3_redist_send_sgi(ocs, grp, irq, ns);
1641     }
1642 }
1643 
1644 static void icc_sgi0r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1645                            uint64_t value)
1646 {
1647     /* Generate Secure Group 0 SGI. */
1648     GICv3CPUState *cs = icc_cs_from_env(env);
1649     bool ns = !arm_is_secure(env);
1650 
1651     icc_generate_sgi(env, cs, value, GICV3_G0, ns);
1652 }
1653 
1654 static void icc_sgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1655                            uint64_t value)
1656 {
1657     /* Generate Group 1 SGI for the current Security state */
1658     GICv3CPUState *cs = icc_cs_from_env(env);
1659     int grp;
1660     bool ns = !arm_is_secure(env);
1661 
1662     grp = ns ? GICV3_G1NS : GICV3_G1;
1663     icc_generate_sgi(env, cs, value, grp, ns);
1664 }
1665 
1666 static void icc_asgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1667                              uint64_t value)
1668 {
1669     /* Generate Group 1 SGI for the Security state that is not
1670      * the current state
1671      */
1672     GICv3CPUState *cs = icc_cs_from_env(env);
1673     int grp;
1674     bool ns = !arm_is_secure(env);
1675 
1676     grp = ns ? GICV3_G1 : GICV3_G1NS;
1677     icc_generate_sgi(env, cs, value, grp, ns);
1678 }
1679 
1680 static uint64_t icc_igrpen_read(CPUARMState *env, const ARMCPRegInfo *ri)
1681 {
1682     GICv3CPUState *cs = icc_cs_from_env(env);
1683     int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0;
1684     uint64_t value;
1685 
1686     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1687         return icv_igrpen_read(env, ri);
1688     }
1689 
1690     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1691         grp = GICV3_G1NS;
1692     }
1693 
1694     value = cs->icc_igrpen[grp];
1695     trace_gicv3_icc_igrpen_read(ri->opc2 & 1 ? 1 : 0,
1696                                 gicv3_redist_affid(cs), value);
1697     return value;
1698 }
1699 
1700 static void icc_igrpen_write(CPUARMState *env, const ARMCPRegInfo *ri,
1701                              uint64_t value)
1702 {
1703     GICv3CPUState *cs = icc_cs_from_env(env);
1704     int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0;
1705 
1706     if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1707         icv_igrpen_write(env, ri, value);
1708         return;
1709     }
1710 
1711     trace_gicv3_icc_igrpen_write(ri->opc2 & 1 ? 1 : 0,
1712                                  gicv3_redist_affid(cs), value);
1713 
1714     if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1715         grp = GICV3_G1NS;
1716     }
1717 
1718     cs->icc_igrpen[grp] = value & ICC_IGRPEN_ENABLE;
1719     gicv3_cpuif_update(cs);
1720 }
1721 
1722 static uint64_t icc_igrpen1_el3_read(CPUARMState *env, const ARMCPRegInfo *ri)
1723 {
1724     GICv3CPUState *cs = icc_cs_from_env(env);
1725     uint64_t value;
1726 
1727     /* IGRPEN1_EL3 bits 0 and 1 are r/w aliases into IGRPEN1_EL1 NS and S */
1728     value = cs->icc_igrpen[GICV3_G1NS] | (cs->icc_igrpen[GICV3_G1] << 1);
1729     trace_gicv3_icc_igrpen1_el3_read(gicv3_redist_affid(cs), value);
1730     return value;
1731 }
1732 
1733 static void icc_igrpen1_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
1734                                   uint64_t value)
1735 {
1736     GICv3CPUState *cs = icc_cs_from_env(env);
1737 
1738     trace_gicv3_icc_igrpen1_el3_write(gicv3_redist_affid(cs), value);
1739 
1740     /* IGRPEN1_EL3 bits 0 and 1 are r/w aliases into IGRPEN1_EL1 NS and S */
1741     cs->icc_igrpen[GICV3_G1NS] = extract32(value, 0, 1);
1742     cs->icc_igrpen[GICV3_G1] = extract32(value, 1, 1);
1743     gicv3_cpuif_update(cs);
1744 }
1745 
1746 static uint64_t icc_ctlr_el1_read(CPUARMState *env, const ARMCPRegInfo *ri)
1747 {
1748     GICv3CPUState *cs = icc_cs_from_env(env);
1749     int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S;
1750     uint64_t value;
1751 
1752     if (icv_access(env, HCR_FMO | HCR_IMO)) {
1753         return icv_ctlr_read(env, ri);
1754     }
1755 
1756     value = cs->icc_ctlr_el1[bank];
1757     trace_gicv3_icc_ctlr_read(gicv3_redist_affid(cs), value);
1758     return value;
1759 }
1760 
1761 static void icc_ctlr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
1762                                uint64_t value)
1763 {
1764     GICv3CPUState *cs = icc_cs_from_env(env);
1765     int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S;
1766     uint64_t mask;
1767 
1768     if (icv_access(env, HCR_FMO | HCR_IMO)) {
1769         icv_ctlr_write(env, ri, value);
1770         return;
1771     }
1772 
1773     trace_gicv3_icc_ctlr_write(gicv3_redist_affid(cs), value);
1774 
1775     /* Only CBPR and EOIMODE can be RW;
1776      * for us PMHE is RAZ/WI (we don't implement 1-of-N interrupts or
1777      * the asseciated priority-based routing of them);
1778      * if EL3 is implemented and GICD_CTLR.DS == 0, then PMHE and CBPR are RO.
1779      */
1780     if (arm_feature(env, ARM_FEATURE_EL3) &&
1781         ((cs->gic->gicd_ctlr & GICD_CTLR_DS) == 0)) {
1782         mask = ICC_CTLR_EL1_EOIMODE;
1783     } else {
1784         mask = ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE;
1785     }
1786 
1787     cs->icc_ctlr_el1[bank] &= ~mask;
1788     cs->icc_ctlr_el1[bank] |= (value & mask);
1789     gicv3_cpuif_update(cs);
1790 }
1791 
1792 
1793 static uint64_t icc_ctlr_el3_read(CPUARMState *env, const ARMCPRegInfo *ri)
1794 {
1795     GICv3CPUState *cs = icc_cs_from_env(env);
1796     uint64_t value;
1797 
1798     value = cs->icc_ctlr_el3;
1799     if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) {
1800         value |= ICC_CTLR_EL3_EOIMODE_EL1NS;
1801     }
1802     if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) {
1803         value |= ICC_CTLR_EL3_CBPR_EL1NS;
1804     }
1805     if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) {
1806         value |= ICC_CTLR_EL3_EOIMODE_EL1S;
1807     }
1808     if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) {
1809         value |= ICC_CTLR_EL3_CBPR_EL1S;
1810     }
1811 
1812     trace_gicv3_icc_ctlr_el3_read(gicv3_redist_affid(cs), value);
1813     return value;
1814 }
1815 
1816 static void icc_ctlr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
1817                                uint64_t value)
1818 {
1819     GICv3CPUState *cs = icc_cs_from_env(env);
1820     uint64_t mask;
1821 
1822     trace_gicv3_icc_ctlr_el3_write(gicv3_redist_affid(cs), value);
1823 
1824     /* *_EL1NS and *_EL1S bits are aliases into the ICC_CTLR_EL1 bits. */
1825     cs->icc_ctlr_el1[GICV3_NS] &= (ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE);
1826     if (value & ICC_CTLR_EL3_EOIMODE_EL1NS) {
1827         cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_EOIMODE;
1828     }
1829     if (value & ICC_CTLR_EL3_CBPR_EL1NS) {
1830         cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_CBPR;
1831     }
1832 
1833     cs->icc_ctlr_el1[GICV3_S] &= (ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE);
1834     if (value & ICC_CTLR_EL3_EOIMODE_EL1S) {
1835         cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_EOIMODE;
1836     }
1837     if (value & ICC_CTLR_EL3_CBPR_EL1S) {
1838         cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_CBPR;
1839     }
1840 
1841     /* The only bit stored in icc_ctlr_el3 which is writeable is EOIMODE_EL3: */
1842     mask = ICC_CTLR_EL3_EOIMODE_EL3;
1843 
1844     cs->icc_ctlr_el3 &= ~mask;
1845     cs->icc_ctlr_el3 |= (value & mask);
1846     gicv3_cpuif_update(cs);
1847 }
1848 
1849 static CPAccessResult gicv3_irqfiq_access(CPUARMState *env,
1850                                           const ARMCPRegInfo *ri, bool isread)
1851 {
1852     CPAccessResult r = CP_ACCESS_OK;
1853     GICv3CPUState *cs = icc_cs_from_env(env);
1854     int el = arm_current_el(env);
1855 
1856     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TC) &&
1857         el == 1 && !arm_is_secure_below_el3(env)) {
1858         /* Takes priority over a possible EL3 trap */
1859         return CP_ACCESS_TRAP_EL2;
1860     }
1861 
1862     if ((env->cp15.scr_el3 & (SCR_FIQ | SCR_IRQ)) == (SCR_FIQ | SCR_IRQ)) {
1863         switch (el) {
1864         case 1:
1865             if (arm_is_secure_below_el3(env) ||
1866                 ((env->cp15.hcr_el2 & (HCR_IMO | HCR_FMO)) == 0)) {
1867                 r = CP_ACCESS_TRAP_EL3;
1868             }
1869             break;
1870         case 2:
1871             r = CP_ACCESS_TRAP_EL3;
1872             break;
1873         case 3:
1874             if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
1875                 r = CP_ACCESS_TRAP_EL3;
1876             }
1877             break;
1878         default:
1879             g_assert_not_reached();
1880         }
1881     }
1882 
1883     if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
1884         r = CP_ACCESS_TRAP;
1885     }
1886     return r;
1887 }
1888 
1889 static CPAccessResult gicv3_dir_access(CPUARMState *env,
1890                                        const ARMCPRegInfo *ri, bool isread)
1891 {
1892     GICv3CPUState *cs = icc_cs_from_env(env);
1893 
1894     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TDIR) &&
1895         arm_current_el(env) == 1 && !arm_is_secure_below_el3(env)) {
1896         /* Takes priority over a possible EL3 trap */
1897         return CP_ACCESS_TRAP_EL2;
1898     }
1899 
1900     return gicv3_irqfiq_access(env, ri, isread);
1901 }
1902 
1903 static CPAccessResult gicv3_sgi_access(CPUARMState *env,
1904                                        const ARMCPRegInfo *ri, bool isread)
1905 {
1906     if ((env->cp15.hcr_el2 & (HCR_IMO | HCR_FMO)) &&
1907         arm_current_el(env) == 1 && !arm_is_secure_below_el3(env)) {
1908         /* Takes priority over a possible EL3 trap */
1909         return CP_ACCESS_TRAP_EL2;
1910     }
1911 
1912     return gicv3_irqfiq_access(env, ri, isread);
1913 }
1914 
1915 static CPAccessResult gicv3_fiq_access(CPUARMState *env,
1916                                        const ARMCPRegInfo *ri, bool isread)
1917 {
1918     CPAccessResult r = CP_ACCESS_OK;
1919     GICv3CPUState *cs = icc_cs_from_env(env);
1920     int el = arm_current_el(env);
1921 
1922     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TALL0) &&
1923         el == 1 && !arm_is_secure_below_el3(env)) {
1924         /* Takes priority over a possible EL3 trap */
1925         return CP_ACCESS_TRAP_EL2;
1926     }
1927 
1928     if (env->cp15.scr_el3 & SCR_FIQ) {
1929         switch (el) {
1930         case 1:
1931             if (arm_is_secure_below_el3(env) ||
1932                 ((env->cp15.hcr_el2 & HCR_FMO) == 0)) {
1933                 r = CP_ACCESS_TRAP_EL3;
1934             }
1935             break;
1936         case 2:
1937             r = CP_ACCESS_TRAP_EL3;
1938             break;
1939         case 3:
1940             if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
1941                 r = CP_ACCESS_TRAP_EL3;
1942             }
1943             break;
1944         default:
1945             g_assert_not_reached();
1946         }
1947     }
1948 
1949     if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
1950         r = CP_ACCESS_TRAP;
1951     }
1952     return r;
1953 }
1954 
1955 static CPAccessResult gicv3_irq_access(CPUARMState *env,
1956                                        const ARMCPRegInfo *ri, bool isread)
1957 {
1958     CPAccessResult r = CP_ACCESS_OK;
1959     GICv3CPUState *cs = icc_cs_from_env(env);
1960     int el = arm_current_el(env);
1961 
1962     if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TALL1) &&
1963         el == 1 && !arm_is_secure_below_el3(env)) {
1964         /* Takes priority over a possible EL3 trap */
1965         return CP_ACCESS_TRAP_EL2;
1966     }
1967 
1968     if (env->cp15.scr_el3 & SCR_IRQ) {
1969         switch (el) {
1970         case 1:
1971             if (arm_is_secure_below_el3(env) ||
1972                 ((env->cp15.hcr_el2 & HCR_IMO) == 0)) {
1973                 r = CP_ACCESS_TRAP_EL3;
1974             }
1975             break;
1976         case 2:
1977             r = CP_ACCESS_TRAP_EL3;
1978             break;
1979         case 3:
1980             if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
1981                 r = CP_ACCESS_TRAP_EL3;
1982             }
1983             break;
1984         default:
1985             g_assert_not_reached();
1986         }
1987     }
1988 
1989     if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
1990         r = CP_ACCESS_TRAP;
1991     }
1992     return r;
1993 }
1994 
1995 static void icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1996 {
1997     GICv3CPUState *cs = icc_cs_from_env(env);
1998 
1999     cs->icc_ctlr_el1[GICV3_S] = ICC_CTLR_EL1_A3V |
2000         (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
2001         (7 << ICC_CTLR_EL1_PRIBITS_SHIFT);
2002     cs->icc_ctlr_el1[GICV3_NS] = ICC_CTLR_EL1_A3V |
2003         (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
2004         (7 << ICC_CTLR_EL1_PRIBITS_SHIFT);
2005     cs->icc_pmr_el1 = 0;
2006     cs->icc_bpr[GICV3_G0] = GIC_MIN_BPR;
2007     cs->icc_bpr[GICV3_G1] = GIC_MIN_BPR;
2008     if (arm_feature(env, ARM_FEATURE_EL3)) {
2009         cs->icc_bpr[GICV3_G1NS] = GIC_MIN_BPR_NS;
2010     } else {
2011         cs->icc_bpr[GICV3_G1NS] = GIC_MIN_BPR;
2012     }
2013     memset(cs->icc_apr, 0, sizeof(cs->icc_apr));
2014     memset(cs->icc_igrpen, 0, sizeof(cs->icc_igrpen));
2015     cs->icc_ctlr_el3 = ICC_CTLR_EL3_NDS | ICC_CTLR_EL3_A3V |
2016         (1 << ICC_CTLR_EL3_IDBITS_SHIFT) |
2017         (7 << ICC_CTLR_EL3_PRIBITS_SHIFT);
2018 
2019     memset(cs->ich_apr, 0, sizeof(cs->ich_apr));
2020     cs->ich_hcr_el2 = 0;
2021     memset(cs->ich_lr_el2, 0, sizeof(cs->ich_lr_el2));
2022     cs->ich_vmcr_el2 = ICH_VMCR_EL2_VFIQEN |
2023         ((icv_min_vbpr(cs) + 1) << ICH_VMCR_EL2_VBPR1_SHIFT) |
2024         (icv_min_vbpr(cs) << ICH_VMCR_EL2_VBPR0_SHIFT);
2025 }
2026 
2027 static const ARMCPRegInfo gicv3_cpuif_reginfo[] = {
2028     { .name = "ICC_PMR_EL1", .state = ARM_CP_STATE_BOTH,
2029       .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 6, .opc2 = 0,
2030       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2031       .access = PL1_RW, .accessfn = gicv3_irqfiq_access,
2032       .readfn = icc_pmr_read,
2033       .writefn = icc_pmr_write,
2034       /* We hang the whole cpu interface reset routine off here
2035        * rather than parcelling it out into one little function
2036        * per register
2037        */
2038       .resetfn = icc_reset,
2039     },
2040     { .name = "ICC_IAR0_EL1", .state = ARM_CP_STATE_BOTH,
2041       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 0,
2042       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2043       .access = PL1_R, .accessfn = gicv3_fiq_access,
2044       .readfn = icc_iar0_read,
2045     },
2046     { .name = "ICC_EOIR0_EL1", .state = ARM_CP_STATE_BOTH,
2047       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 1,
2048       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2049       .access = PL1_W, .accessfn = gicv3_fiq_access,
2050       .writefn = icc_eoir_write,
2051     },
2052     { .name = "ICC_HPPIR0_EL1", .state = ARM_CP_STATE_BOTH,
2053       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 2,
2054       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2055       .access = PL1_R, .accessfn = gicv3_fiq_access,
2056       .readfn = icc_hppir0_read,
2057     },
2058     { .name = "ICC_BPR0_EL1", .state = ARM_CP_STATE_BOTH,
2059       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 3,
2060       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2061       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2062       .readfn = icc_bpr_read,
2063       .writefn = icc_bpr_write,
2064     },
2065     { .name = "ICC_AP0R0_EL1", .state = ARM_CP_STATE_BOTH,
2066       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 4,
2067       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2068       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2069       .readfn = icc_ap_read,
2070       .writefn = icc_ap_write,
2071     },
2072     { .name = "ICC_AP0R1_EL1", .state = ARM_CP_STATE_BOTH,
2073       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 5,
2074       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2075       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2076       .readfn = icc_ap_read,
2077       .writefn = icc_ap_write,
2078     },
2079     { .name = "ICC_AP0R2_EL1", .state = ARM_CP_STATE_BOTH,
2080       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 6,
2081       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2082       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2083       .readfn = icc_ap_read,
2084       .writefn = icc_ap_write,
2085     },
2086     { .name = "ICC_AP0R3_EL1", .state = ARM_CP_STATE_BOTH,
2087       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 7,
2088       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2089       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2090       .readfn = icc_ap_read,
2091       .writefn = icc_ap_write,
2092     },
2093     /* All the ICC_AP1R*_EL1 registers are banked */
2094     { .name = "ICC_AP1R0_EL1", .state = ARM_CP_STATE_BOTH,
2095       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 0,
2096       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2097       .access = PL1_RW, .accessfn = gicv3_irq_access,
2098       .readfn = icc_ap_read,
2099       .writefn = icc_ap_write,
2100     },
2101     { .name = "ICC_AP1R1_EL1", .state = ARM_CP_STATE_BOTH,
2102       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 1,
2103       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2104       .access = PL1_RW, .accessfn = gicv3_irq_access,
2105       .readfn = icc_ap_read,
2106       .writefn = icc_ap_write,
2107     },
2108     { .name = "ICC_AP1R2_EL1", .state = ARM_CP_STATE_BOTH,
2109       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 2,
2110       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2111       .access = PL1_RW, .accessfn = gicv3_irq_access,
2112       .readfn = icc_ap_read,
2113       .writefn = icc_ap_write,
2114     },
2115     { .name = "ICC_AP1R3_EL1", .state = ARM_CP_STATE_BOTH,
2116       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 3,
2117       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2118       .access = PL1_RW, .accessfn = gicv3_irq_access,
2119       .readfn = icc_ap_read,
2120       .writefn = icc_ap_write,
2121     },
2122     { .name = "ICC_DIR_EL1", .state = ARM_CP_STATE_BOTH,
2123       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 1,
2124       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2125       .access = PL1_W, .accessfn = gicv3_dir_access,
2126       .writefn = icc_dir_write,
2127     },
2128     { .name = "ICC_RPR_EL1", .state = ARM_CP_STATE_BOTH,
2129       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 3,
2130       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2131       .access = PL1_R, .accessfn = gicv3_irqfiq_access,
2132       .readfn = icc_rpr_read,
2133     },
2134     { .name = "ICC_SGI1R_EL1", .state = ARM_CP_STATE_AA64,
2135       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 5,
2136       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2137       .access = PL1_W, .accessfn = gicv3_sgi_access,
2138       .writefn = icc_sgi1r_write,
2139     },
2140     { .name = "ICC_SGI1R",
2141       .cp = 15, .opc1 = 0, .crm = 12,
2142       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2143       .access = PL1_W, .accessfn = gicv3_sgi_access,
2144       .writefn = icc_sgi1r_write,
2145     },
2146     { .name = "ICC_ASGI1R_EL1", .state = ARM_CP_STATE_AA64,
2147       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 6,
2148       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2149       .access = PL1_W, .accessfn = gicv3_sgi_access,
2150       .writefn = icc_asgi1r_write,
2151     },
2152     { .name = "ICC_ASGI1R",
2153       .cp = 15, .opc1 = 1, .crm = 12,
2154       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2155       .access = PL1_W, .accessfn = gicv3_sgi_access,
2156       .writefn = icc_asgi1r_write,
2157     },
2158     { .name = "ICC_SGI0R_EL1", .state = ARM_CP_STATE_AA64,
2159       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 7,
2160       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2161       .access = PL1_W, .accessfn = gicv3_sgi_access,
2162       .writefn = icc_sgi0r_write,
2163     },
2164     { .name = "ICC_SGI0R",
2165       .cp = 15, .opc1 = 2, .crm = 12,
2166       .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2167       .access = PL1_W, .accessfn = gicv3_sgi_access,
2168       .writefn = icc_sgi0r_write,
2169     },
2170     { .name = "ICC_IAR1_EL1", .state = ARM_CP_STATE_BOTH,
2171       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 0,
2172       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2173       .access = PL1_R, .accessfn = gicv3_irq_access,
2174       .readfn = icc_iar1_read,
2175     },
2176     { .name = "ICC_EOIR1_EL1", .state = ARM_CP_STATE_BOTH,
2177       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 1,
2178       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2179       .access = PL1_W, .accessfn = gicv3_irq_access,
2180       .writefn = icc_eoir_write,
2181     },
2182     { .name = "ICC_HPPIR1_EL1", .state = ARM_CP_STATE_BOTH,
2183       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 2,
2184       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2185       .access = PL1_R, .accessfn = gicv3_irq_access,
2186       .readfn = icc_hppir1_read,
2187     },
2188     /* This register is banked */
2189     { .name = "ICC_BPR1_EL1", .state = ARM_CP_STATE_BOTH,
2190       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 3,
2191       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2192       .access = PL1_RW, .accessfn = gicv3_irq_access,
2193       .readfn = icc_bpr_read,
2194       .writefn = icc_bpr_write,
2195     },
2196     /* This register is banked */
2197     { .name = "ICC_CTLR_EL1", .state = ARM_CP_STATE_BOTH,
2198       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 4,
2199       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2200       .access = PL1_RW, .accessfn = gicv3_irqfiq_access,
2201       .readfn = icc_ctlr_el1_read,
2202       .writefn = icc_ctlr_el1_write,
2203     },
2204     { .name = "ICC_SRE_EL1", .state = ARM_CP_STATE_BOTH,
2205       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 5,
2206       .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2207       .access = PL1_RW,
2208       /* We don't support IRQ/FIQ bypass and system registers are
2209        * always enabled, so all our bits are RAZ/WI or RAO/WI.
2210        * This register is banked but since it's constant we don't
2211        * need to do anything special.
2212        */
2213       .resetvalue = 0x7,
2214     },
2215     { .name = "ICC_IGRPEN0_EL1", .state = ARM_CP_STATE_BOTH,
2216       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 6,
2217       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2218       .access = PL1_RW, .accessfn = gicv3_fiq_access,
2219       .readfn = icc_igrpen_read,
2220       .writefn = icc_igrpen_write,
2221     },
2222     /* This register is banked */
2223     { .name = "ICC_IGRPEN1_EL1", .state = ARM_CP_STATE_BOTH,
2224       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 7,
2225       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2226       .access = PL1_RW, .accessfn = gicv3_irq_access,
2227       .readfn = icc_igrpen_read,
2228       .writefn = icc_igrpen_write,
2229     },
2230     { .name = "ICC_SRE_EL2", .state = ARM_CP_STATE_BOTH,
2231       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 5,
2232       .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2233       .access = PL2_RW,
2234       /* We don't support IRQ/FIQ bypass and system registers are
2235        * always enabled, so all our bits are RAZ/WI or RAO/WI.
2236        */
2237       .resetvalue = 0xf,
2238     },
2239     { .name = "ICC_CTLR_EL3", .state = ARM_CP_STATE_BOTH,
2240       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 4,
2241       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2242       .access = PL3_RW,
2243       .readfn = icc_ctlr_el3_read,
2244       .writefn = icc_ctlr_el3_write,
2245     },
2246     { .name = "ICC_SRE_EL3", .state = ARM_CP_STATE_BOTH,
2247       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 5,
2248       .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2249       .access = PL3_RW,
2250       /* We don't support IRQ/FIQ bypass and system registers are
2251        * always enabled, so all our bits are RAZ/WI or RAO/WI.
2252        */
2253       .resetvalue = 0xf,
2254     },
2255     { .name = "ICC_IGRPEN1_EL3", .state = ARM_CP_STATE_BOTH,
2256       .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 7,
2257       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2258       .access = PL3_RW,
2259       .readfn = icc_igrpen1_el3_read,
2260       .writefn = icc_igrpen1_el3_write,
2261     },
2262     REGINFO_SENTINEL
2263 };
2264 
2265 static uint64_t ich_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2266 {
2267     GICv3CPUState *cs = icc_cs_from_env(env);
2268     int regno = ri->opc2 & 3;
2269     int grp = ri->crm & 1 ? GICV3_G0 : GICV3_G1NS;
2270     uint64_t value;
2271 
2272     value = cs->ich_apr[grp][regno];
2273     trace_gicv3_ich_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
2274     return value;
2275 }
2276 
2277 static void ich_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2278                          uint64_t value)
2279 {
2280     GICv3CPUState *cs = icc_cs_from_env(env);
2281     int regno = ri->opc2 & 3;
2282     int grp = ri->crm & 1 ? GICV3_G0 : GICV3_G1NS;
2283 
2284     trace_gicv3_ich_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
2285 
2286     cs->ich_apr[grp][regno] = value & 0xFFFFFFFFU;
2287     gicv3_cpuif_virt_update(cs);
2288 }
2289 
2290 static uint64_t ich_hcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2291 {
2292     GICv3CPUState *cs = icc_cs_from_env(env);
2293     uint64_t value = cs->ich_hcr_el2;
2294 
2295     trace_gicv3_ich_hcr_read(gicv3_redist_affid(cs), value);
2296     return value;
2297 }
2298 
2299 static void ich_hcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2300                           uint64_t value)
2301 {
2302     GICv3CPUState *cs = icc_cs_from_env(env);
2303 
2304     trace_gicv3_ich_hcr_write(gicv3_redist_affid(cs), value);
2305 
2306     value &= ICH_HCR_EL2_EN | ICH_HCR_EL2_UIE | ICH_HCR_EL2_LRENPIE |
2307         ICH_HCR_EL2_NPIE | ICH_HCR_EL2_VGRP0EIE | ICH_HCR_EL2_VGRP0DIE |
2308         ICH_HCR_EL2_VGRP1EIE | ICH_HCR_EL2_VGRP1DIE | ICH_HCR_EL2_TC |
2309         ICH_HCR_EL2_TALL0 | ICH_HCR_EL2_TALL1 | ICH_HCR_EL2_TSEI |
2310         ICH_HCR_EL2_TDIR | ICH_HCR_EL2_EOICOUNT_MASK;
2311 
2312     cs->ich_hcr_el2 = value;
2313     gicv3_cpuif_virt_update(cs);
2314 }
2315 
2316 static uint64_t ich_vmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2317 {
2318     GICv3CPUState *cs = icc_cs_from_env(env);
2319     uint64_t value = cs->ich_vmcr_el2;
2320 
2321     trace_gicv3_ich_vmcr_read(gicv3_redist_affid(cs), value);
2322     return value;
2323 }
2324 
2325 static void ich_vmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2326                          uint64_t value)
2327 {
2328     GICv3CPUState *cs = icc_cs_from_env(env);
2329 
2330     trace_gicv3_ich_vmcr_write(gicv3_redist_affid(cs), value);
2331 
2332     value &= ICH_VMCR_EL2_VENG0 | ICH_VMCR_EL2_VENG1 | ICH_VMCR_EL2_VCBPR |
2333         ICH_VMCR_EL2_VEOIM | ICH_VMCR_EL2_VBPR1_MASK |
2334         ICH_VMCR_EL2_VBPR0_MASK | ICH_VMCR_EL2_VPMR_MASK;
2335     value |= ICH_VMCR_EL2_VFIQEN;
2336 
2337     cs->ich_vmcr_el2 = value;
2338     /* Enforce "writing BPRs to less than minimum sets them to the minimum"
2339      * by reading and writing back the fields.
2340      */
2341     write_vbpr(cs, GICV3_G1, read_vbpr(cs, GICV3_G0));
2342     write_vbpr(cs, GICV3_G1, read_vbpr(cs, GICV3_G1));
2343 
2344     gicv3_cpuif_virt_update(cs);
2345 }
2346 
2347 static uint64_t ich_lr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2348 {
2349     GICv3CPUState *cs = icc_cs_from_env(env);
2350     int regno = ri->opc2 | ((ri->crm & 1) << 3);
2351     uint64_t value;
2352 
2353     /* This read function handles all of:
2354      * 64-bit reads of the whole LR
2355      * 32-bit reads of the low half of the LR
2356      * 32-bit reads of the high half of the LR
2357      */
2358     if (ri->state == ARM_CP_STATE_AA32) {
2359         if (ri->crm >= 14) {
2360             value = extract64(cs->ich_lr_el2[regno], 32, 32);
2361             trace_gicv3_ich_lrc_read(regno, gicv3_redist_affid(cs), value);
2362         } else {
2363             value = extract64(cs->ich_lr_el2[regno], 0, 32);
2364             trace_gicv3_ich_lr32_read(regno, gicv3_redist_affid(cs), value);
2365         }
2366     } else {
2367         value = cs->ich_lr_el2[regno];
2368         trace_gicv3_ich_lr_read(regno, gicv3_redist_affid(cs), value);
2369     }
2370 
2371     return value;
2372 }
2373 
2374 static void ich_lr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2375                          uint64_t value)
2376 {
2377     GICv3CPUState *cs = icc_cs_from_env(env);
2378     int regno = ri->opc2 | ((ri->crm & 1) << 3);
2379 
2380     /* This write function handles all of:
2381      * 64-bit writes to the whole LR
2382      * 32-bit writes to the low half of the LR
2383      * 32-bit writes to the high half of the LR
2384      */
2385     if (ri->state == ARM_CP_STATE_AA32) {
2386         if (ri->crm >= 14) {
2387             trace_gicv3_ich_lrc_write(regno, gicv3_redist_affid(cs), value);
2388             value = deposit64(cs->ich_lr_el2[regno], 32, 32, value);
2389         } else {
2390             trace_gicv3_ich_lr32_write(regno, gicv3_redist_affid(cs), value);
2391             value = deposit64(cs->ich_lr_el2[regno], 0, 32, value);
2392         }
2393     } else {
2394         trace_gicv3_ich_lr_write(regno, gicv3_redist_affid(cs), value);
2395     }
2396 
2397     /* Enforce RES0 bits in priority field */
2398     if (cs->vpribits < 8) {
2399         value = deposit64(value, ICH_LR_EL2_PRIORITY_SHIFT,
2400                           8 - cs->vpribits, 0);
2401     }
2402 
2403     cs->ich_lr_el2[regno] = value;
2404     gicv3_cpuif_virt_update(cs);
2405 }
2406 
2407 static uint64_t ich_vtr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2408 {
2409     GICv3CPUState *cs = icc_cs_from_env(env);
2410     uint64_t value;
2411 
2412     value = ((cs->num_list_regs - 1) << ICH_VTR_EL2_LISTREGS_SHIFT)
2413         | ICH_VTR_EL2_TDS | ICH_VTR_EL2_NV4 | ICH_VTR_EL2_A3V
2414         | (1 << ICH_VTR_EL2_IDBITS_SHIFT)
2415         | ((cs->vprebits - 1) << ICH_VTR_EL2_PREBITS_SHIFT)
2416         | ((cs->vpribits - 1) << ICH_VTR_EL2_PRIBITS_SHIFT);
2417 
2418     trace_gicv3_ich_vtr_read(gicv3_redist_affid(cs), value);
2419     return value;
2420 }
2421 
2422 static uint64_t ich_misr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2423 {
2424     GICv3CPUState *cs = icc_cs_from_env(env);
2425     uint64_t value = maintenance_interrupt_state(cs);
2426 
2427     trace_gicv3_ich_misr_read(gicv3_redist_affid(cs), value);
2428     return value;
2429 }
2430 
2431 static uint64_t ich_eisr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2432 {
2433     GICv3CPUState *cs = icc_cs_from_env(env);
2434     uint64_t value = eoi_maintenance_interrupt_state(cs, NULL);
2435 
2436     trace_gicv3_ich_eisr_read(gicv3_redist_affid(cs), value);
2437     return value;
2438 }
2439 
2440 static uint64_t ich_elrsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2441 {
2442     GICv3CPUState *cs = icc_cs_from_env(env);
2443     uint64_t value = 0;
2444     int i;
2445 
2446     for (i = 0; i < cs->num_list_regs; i++) {
2447         uint64_t lr = cs->ich_lr_el2[i];
2448 
2449         if ((lr & ICH_LR_EL2_STATE_MASK) == 0 &&
2450             ((lr & ICH_LR_EL2_HW) != 0 || (lr & ICH_LR_EL2_EOI) == 0)) {
2451             value |= (1 << i);
2452         }
2453     }
2454 
2455     trace_gicv3_ich_elrsr_read(gicv3_redist_affid(cs), value);
2456     return value;
2457 }
2458 
2459 static const ARMCPRegInfo gicv3_cpuif_hcr_reginfo[] = {
2460     { .name = "ICH_AP0R0_EL2", .state = ARM_CP_STATE_BOTH,
2461       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 0,
2462       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2463       .access = PL2_RW,
2464       .readfn = ich_ap_read,
2465       .writefn = ich_ap_write,
2466     },
2467     { .name = "ICH_AP1R0_EL2", .state = ARM_CP_STATE_BOTH,
2468       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 0,
2469       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2470       .access = PL2_RW,
2471       .readfn = ich_ap_read,
2472       .writefn = ich_ap_write,
2473     },
2474     { .name = "ICH_HCR_EL2", .state = ARM_CP_STATE_BOTH,
2475       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 0,
2476       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2477       .access = PL2_RW,
2478       .readfn = ich_hcr_read,
2479       .writefn = ich_hcr_write,
2480     },
2481     { .name = "ICH_VTR_EL2", .state = ARM_CP_STATE_BOTH,
2482       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 1,
2483       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2484       .access = PL2_R,
2485       .readfn = ich_vtr_read,
2486     },
2487     { .name = "ICH_MISR_EL2", .state = ARM_CP_STATE_BOTH,
2488       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 2,
2489       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2490       .access = PL2_R,
2491       .readfn = ich_misr_read,
2492     },
2493     { .name = "ICH_EISR_EL2", .state = ARM_CP_STATE_BOTH,
2494       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 3,
2495       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2496       .access = PL2_R,
2497       .readfn = ich_eisr_read,
2498     },
2499     { .name = "ICH_ELRSR_EL2", .state = ARM_CP_STATE_BOTH,
2500       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 5,
2501       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2502       .access = PL2_R,
2503       .readfn = ich_elrsr_read,
2504     },
2505     { .name = "ICH_VMCR_EL2", .state = ARM_CP_STATE_BOTH,
2506       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 7,
2507       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2508       .access = PL2_RW,
2509       .readfn = ich_vmcr_read,
2510       .writefn = ich_vmcr_write,
2511     },
2512     REGINFO_SENTINEL
2513 };
2514 
2515 static const ARMCPRegInfo gicv3_cpuif_ich_apxr1_reginfo[] = {
2516     { .name = "ICH_AP0R1_EL2", .state = ARM_CP_STATE_BOTH,
2517       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 1,
2518       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2519       .access = PL2_RW,
2520       .readfn = ich_ap_read,
2521       .writefn = ich_ap_write,
2522     },
2523     { .name = "ICH_AP1R1_EL2", .state = ARM_CP_STATE_BOTH,
2524       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 1,
2525       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2526       .access = PL2_RW,
2527       .readfn = ich_ap_read,
2528       .writefn = ich_ap_write,
2529     },
2530     REGINFO_SENTINEL
2531 };
2532 
2533 static const ARMCPRegInfo gicv3_cpuif_ich_apxr23_reginfo[] = {
2534     { .name = "ICH_AP0R2_EL2", .state = ARM_CP_STATE_BOTH,
2535       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 2,
2536       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2537       .access = PL2_RW,
2538       .readfn = ich_ap_read,
2539       .writefn = ich_ap_write,
2540     },
2541     { .name = "ICH_AP0R3_EL2", .state = ARM_CP_STATE_BOTH,
2542       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 3,
2543       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2544       .access = PL2_RW,
2545       .readfn = ich_ap_read,
2546       .writefn = ich_ap_write,
2547     },
2548     { .name = "ICH_AP1R2_EL2", .state = ARM_CP_STATE_BOTH,
2549       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 2,
2550       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2551       .access = PL2_RW,
2552       .readfn = ich_ap_read,
2553       .writefn = ich_ap_write,
2554     },
2555     { .name = "ICH_AP1R3_EL2", .state = ARM_CP_STATE_BOTH,
2556       .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 3,
2557       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2558       .access = PL2_RW,
2559       .readfn = ich_ap_read,
2560       .writefn = ich_ap_write,
2561     },
2562     REGINFO_SENTINEL
2563 };
2564 
2565 static void gicv3_cpuif_el_change_hook(ARMCPU *cpu, void *opaque)
2566 {
2567     GICv3CPUState *cs = opaque;
2568 
2569     gicv3_cpuif_update(cs);
2570 }
2571 
2572 void gicv3_init_cpuif(GICv3State *s)
2573 {
2574     /* Called from the GICv3 realize function; register our system
2575      * registers with the CPU
2576      */
2577     int i;
2578 
2579     for (i = 0; i < s->num_cpu; i++) {
2580         ARMCPU *cpu = ARM_CPU(qemu_get_cpu(i));
2581         GICv3CPUState *cs = &s->cpu[i];
2582 
2583         /* Note that we can't just use the GICv3CPUState as an opaque pointer
2584          * in define_arm_cp_regs_with_opaque(), because when we're called back
2585          * it might be with code translated by CPU 0 but run by CPU 1, in
2586          * which case we'd get the wrong value.
2587          * So instead we define the regs with no ri->opaque info, and
2588          * get back to the GICv3CPUState from the ARMCPU by reading back
2589          * the opaque pointer from the el_change_hook, which we're going
2590          * to need to register anyway.
2591          */
2592         define_arm_cp_regs(cpu, gicv3_cpuif_reginfo);
2593         if (arm_feature(&cpu->env, ARM_FEATURE_EL2)
2594             && cpu->gic_num_lrs) {
2595             int j;
2596 
2597             cs->maintenance_irq = cpu->gicv3_maintenance_interrupt;
2598 
2599             cs->num_list_regs = cpu->gic_num_lrs;
2600             cs->vpribits = cpu->gic_vpribits;
2601             cs->vprebits = cpu->gic_vprebits;
2602 
2603             /* Check against architectural constraints: getting these
2604              * wrong would be a bug in the CPU code defining these,
2605              * and the implementation relies on them holding.
2606              */
2607             g_assert(cs->vprebits <= cs->vpribits);
2608             g_assert(cs->vprebits >= 5 && cs->vprebits <= 7);
2609             g_assert(cs->vpribits >= 5 && cs->vpribits <= 8);
2610 
2611             define_arm_cp_regs(cpu, gicv3_cpuif_hcr_reginfo);
2612 
2613             for (j = 0; j < cs->num_list_regs; j++) {
2614                 /* Note that the AArch64 LRs are 64-bit; the AArch32 LRs
2615                  * are split into two cp15 regs, LR (the low part, with the
2616                  * same encoding as the AArch64 LR) and LRC (the high part).
2617                  */
2618                 ARMCPRegInfo lr_regset[] = {
2619                     { .name = "ICH_LRn_EL2", .state = ARM_CP_STATE_BOTH,
2620                       .opc0 = 3, .opc1 = 4, .crn = 12,
2621                       .crm = 12 + (j >> 3), .opc2 = j & 7,
2622                       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2623                       .access = PL2_RW,
2624                       .readfn = ich_lr_read,
2625                       .writefn = ich_lr_write,
2626                     },
2627                     { .name = "ICH_LRCn_EL2", .state = ARM_CP_STATE_AA32,
2628                       .cp = 15, .opc1 = 4, .crn = 12,
2629                       .crm = 14 + (j >> 3), .opc2 = j & 7,
2630                       .type = ARM_CP_IO | ARM_CP_NO_RAW,
2631                       .access = PL2_RW,
2632                       .readfn = ich_lr_read,
2633                       .writefn = ich_lr_write,
2634                     },
2635                     REGINFO_SENTINEL
2636                 };
2637                 define_arm_cp_regs(cpu, lr_regset);
2638             }
2639             if (cs->vprebits >= 6) {
2640                 define_arm_cp_regs(cpu, gicv3_cpuif_ich_apxr1_reginfo);
2641             }
2642             if (cs->vprebits == 7) {
2643                 define_arm_cp_regs(cpu, gicv3_cpuif_ich_apxr23_reginfo);
2644             }
2645         }
2646         arm_register_el_change_hook(cpu, gicv3_cpuif_el_change_hook, cs);
2647     }
2648 }
2649