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