target/ppc: implement vclrrb
[qemu/rayw.git] / hw / intc / arm_gicv3_cpuif.c
blobd7e03d0cab8b73d57dfaf43057114ebff1c166e7
1 /*
2 * ARM Generic Interrupt Controller v3 (emulation)
4 * Copyright (c) 2016 Linaro Limited
5 * Written by Peter Maydell
7 * This code is licensed under the GPL, version 2 or (at your option)
8 * any later version.
9 */
11 /* This file contains the code for the system register interface
12 * portions of the GICv3.
15 #include "qemu/osdep.h"
16 #include "qemu/bitops.h"
17 #include "qemu/log.h"
18 #include "qemu/main-loop.h"
19 #include "trace.h"
20 #include "gicv3_internal.h"
21 #include "hw/irq.h"
22 #include "cpu.h"
24 static GICv3CPUState *icc_cs_from_env(CPUARMState *env)
26 return env->gicv3state;
29 static bool gicv3_use_ns_bank(CPUARMState *env)
31 /* Return true if we should use the NonSecure bank for a banked GIC
32 * CPU interface register. Note that this differs from the
33 * access_secure_reg() function because GICv3 banked registers are
34 * banked even for AArch64, unlike the other CPU system registers.
36 return !arm_is_secure_below_el3(env);
39 /* The minimum BPR for the virtual interface is a configurable property */
40 static inline int icv_min_vbpr(GICv3CPUState *cs)
42 return 7 - cs->vprebits;
45 /* Simple accessor functions for LR fields */
46 static uint32_t ich_lr_vintid(uint64_t lr)
48 return extract64(lr, ICH_LR_EL2_VINTID_SHIFT, ICH_LR_EL2_VINTID_LENGTH);
51 static uint32_t ich_lr_pintid(uint64_t lr)
53 return extract64(lr, ICH_LR_EL2_PINTID_SHIFT, ICH_LR_EL2_PINTID_LENGTH);
56 static uint32_t ich_lr_prio(uint64_t lr)
58 return extract64(lr, ICH_LR_EL2_PRIORITY_SHIFT, ICH_LR_EL2_PRIORITY_LENGTH);
61 static int ich_lr_state(uint64_t lr)
63 return extract64(lr, ICH_LR_EL2_STATE_SHIFT, ICH_LR_EL2_STATE_LENGTH);
66 static bool icv_access(CPUARMState *env, int hcr_flags)
68 /* Return true if this ICC_ register access should really be
69 * directed to an ICV_ access. hcr_flags is a mask of
70 * HCR_EL2 bits to check: we treat this as an ICV_ access
71 * if we are in NS EL1 and at least one of the specified
72 * HCR_EL2 bits is set.
74 * ICV registers fall into four categories:
75 * * access if NS EL1 and HCR_EL2.FMO == 1:
76 * all ICV regs with '0' in their name
77 * * access if NS EL1 and HCR_EL2.IMO == 1:
78 * all ICV regs with '1' in their name
79 * * access if NS EL1 and either IMO or FMO == 1:
80 * CTLR, DIR, PMR, RPR
82 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
83 bool flagmatch = hcr_el2 & hcr_flags & (HCR_IMO | HCR_FMO);
85 return flagmatch && arm_current_el(env) == 1
86 && !arm_is_secure_below_el3(env);
89 static int read_vbpr(GICv3CPUState *cs, int grp)
91 /* Read VBPR value out of the VMCR field (caller must handle
92 * VCBPR effects if required)
94 if (grp == GICV3_G0) {
95 return extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR0_SHIFT,
96 ICH_VMCR_EL2_VBPR0_LENGTH);
97 } else {
98 return extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR1_SHIFT,
99 ICH_VMCR_EL2_VBPR1_LENGTH);
103 static void write_vbpr(GICv3CPUState *cs, int grp, int value)
105 /* Write new VBPR1 value, handling the "writing a value less than
106 * the minimum sets it to the minimum" semantics.
108 int min = icv_min_vbpr(cs);
110 if (grp != GICV3_G0) {
111 min++;
114 value = MAX(value, min);
116 if (grp == GICV3_G0) {
117 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR0_SHIFT,
118 ICH_VMCR_EL2_VBPR0_LENGTH, value);
119 } else {
120 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR1_SHIFT,
121 ICH_VMCR_EL2_VBPR1_LENGTH, value);
125 static uint32_t icv_fullprio_mask(GICv3CPUState *cs)
127 /* Return a mask word which clears the unimplemented priority bits
128 * from a priority value for a virtual interrupt. (Not to be confused
129 * with the group priority, whose mask depends on the value of VBPR
130 * for the interrupt group.)
132 return ~0U << (8 - cs->vpribits);
135 static int ich_highest_active_virt_prio(GICv3CPUState *cs)
137 /* Calculate the current running priority based on the set bits
138 * in the ICH Active Priority Registers.
140 int i;
141 int aprmax = 1 << (cs->vprebits - 5);
143 assert(aprmax <= ARRAY_SIZE(cs->ich_apr[0]));
145 for (i = 0; i < aprmax; i++) {
146 uint32_t apr = cs->ich_apr[GICV3_G0][i] |
147 cs->ich_apr[GICV3_G1NS][i];
149 if (!apr) {
150 continue;
152 return (i * 32 + ctz32(apr)) << (icv_min_vbpr(cs) + 1);
154 /* No current active interrupts: return idle priority */
155 return 0xff;
158 static int hppvi_index(GICv3CPUState *cs)
160 /* Return the list register index of the highest priority pending
161 * virtual interrupt, as per the HighestPriorityVirtualInterrupt
162 * pseudocode. If no pending virtual interrupts, return -1.
164 int idx = -1;
165 int i;
166 /* Note that a list register entry with a priority of 0xff will
167 * never be reported by this function; this is the architecturally
168 * correct behaviour.
170 int prio = 0xff;
172 if (!(cs->ich_vmcr_el2 & (ICH_VMCR_EL2_VENG0 | ICH_VMCR_EL2_VENG1))) {
173 /* Both groups disabled, definitely nothing to do */
174 return idx;
177 for (i = 0; i < cs->num_list_regs; i++) {
178 uint64_t lr = cs->ich_lr_el2[i];
179 int thisprio;
181 if (ich_lr_state(lr) != ICH_LR_EL2_STATE_PENDING) {
182 /* Not Pending */
183 continue;
186 /* Ignore interrupts if relevant group enable not set */
187 if (lr & ICH_LR_EL2_GROUP) {
188 if (!(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
189 continue;
191 } else {
192 if (!(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG0)) {
193 continue;
197 thisprio = ich_lr_prio(lr);
199 if (thisprio < prio) {
200 prio = thisprio;
201 idx = i;
205 return idx;
208 static uint32_t icv_gprio_mask(GICv3CPUState *cs, int group)
210 /* Return a mask word which clears the subpriority bits from
211 * a priority value for a virtual interrupt in the specified group.
212 * This depends on the VBPR value.
213 * If using VBPR0 then:
214 * a BPR of 0 means the group priority bits are [7:1];
215 * a BPR of 1 means they are [7:2], and so on down to
216 * a BPR of 7 meaning no group priority bits at all.
217 * If using VBPR1 then:
218 * a BPR of 0 is impossible (the minimum value is 1)
219 * a BPR of 1 means the group priority bits are [7:1];
220 * a BPR of 2 means they are [7:2], and so on down to
221 * a BPR of 7 meaning the group priority is [7].
223 * Which BPR to use depends on the group of the interrupt and
224 * the current ICH_VMCR_EL2.VCBPR settings.
226 * This corresponds to the VGroupBits() pseudocode.
228 int bpr;
230 if (group == GICV3_G1NS && cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR) {
231 group = GICV3_G0;
234 bpr = read_vbpr(cs, group);
235 if (group == GICV3_G1NS) {
236 assert(bpr > 0);
237 bpr--;
240 return ~0U << (bpr + 1);
243 static bool icv_hppi_can_preempt(GICv3CPUState *cs, uint64_t lr)
245 /* Return true if we can signal this virtual interrupt defined by
246 * the given list register value; see the pseudocode functions
247 * CanSignalVirtualInterrupt and CanSignalVirtualInt.
248 * Compare also icc_hppi_can_preempt() which is the non-virtual
249 * equivalent of these checks.
251 int grp;
252 uint32_t mask, prio, rprio, vpmr;
254 if (!(cs->ich_hcr_el2 & ICH_HCR_EL2_EN)) {
255 /* Virtual interface disabled */
256 return false;
259 /* We don't need to check that this LR is in Pending state because
260 * that has already been done in hppvi_index().
263 prio = ich_lr_prio(lr);
264 vpmr = extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
265 ICH_VMCR_EL2_VPMR_LENGTH);
267 if (prio >= vpmr) {
268 /* Priority mask masks this interrupt */
269 return false;
272 rprio = ich_highest_active_virt_prio(cs);
273 if (rprio == 0xff) {
274 /* No running interrupt so we can preempt */
275 return true;
278 grp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
280 mask = icv_gprio_mask(cs, grp);
282 /* We only preempt a running interrupt if the pending interrupt's
283 * group priority is sufficient (the subpriorities are not considered).
285 if ((prio & mask) < (rprio & mask)) {
286 return true;
289 return false;
292 static uint32_t eoi_maintenance_interrupt_state(GICv3CPUState *cs,
293 uint32_t *misr)
295 /* Return a set of bits indicating the EOI maintenance interrupt status
296 * for each list register. The EOI maintenance interrupt status is
297 * 1 if LR.State == 0 && LR.HW == 0 && LR.EOI == 1
298 * (see the GICv3 spec for the ICH_EISR_EL2 register).
299 * If misr is not NULL then we should also collect the information
300 * about the MISR.EOI, MISR.NP and MISR.U bits.
302 uint32_t value = 0;
303 int validcount = 0;
304 bool seenpending = false;
305 int i;
307 for (i = 0; i < cs->num_list_regs; i++) {
308 uint64_t lr = cs->ich_lr_el2[i];
310 if ((lr & (ICH_LR_EL2_STATE_MASK | ICH_LR_EL2_HW | ICH_LR_EL2_EOI))
311 == ICH_LR_EL2_EOI) {
312 value |= (1 << i);
314 if ((lr & ICH_LR_EL2_STATE_MASK)) {
315 validcount++;
317 if (ich_lr_state(lr) == ICH_LR_EL2_STATE_PENDING) {
318 seenpending = true;
322 if (misr) {
323 if (validcount < 2 && (cs->ich_hcr_el2 & ICH_HCR_EL2_UIE)) {
324 *misr |= ICH_MISR_EL2_U;
326 if (!seenpending && (cs->ich_hcr_el2 & ICH_HCR_EL2_NPIE)) {
327 *misr |= ICH_MISR_EL2_NP;
329 if (value) {
330 *misr |= ICH_MISR_EL2_EOI;
333 return value;
336 static uint32_t maintenance_interrupt_state(GICv3CPUState *cs)
338 /* Return a set of bits indicating the maintenance interrupt status
339 * (as seen in the ICH_MISR_EL2 register).
341 uint32_t value = 0;
343 /* Scan list registers and fill in the U, NP and EOI bits */
344 eoi_maintenance_interrupt_state(cs, &value);
346 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_LRENPIE) &&
347 (cs->ich_hcr_el2 & ICH_HCR_EL2_EOICOUNT_MASK)) {
348 value |= ICH_MISR_EL2_LRENP;
351 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP0EIE) &&
352 (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG0)) {
353 value |= ICH_MISR_EL2_VGRP0E;
356 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP0DIE) &&
357 !(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
358 value |= ICH_MISR_EL2_VGRP0D;
360 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP1EIE) &&
361 (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
362 value |= ICH_MISR_EL2_VGRP1E;
365 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP1DIE) &&
366 !(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
367 value |= ICH_MISR_EL2_VGRP1D;
370 return value;
373 static void gicv3_cpuif_virt_update(GICv3CPUState *cs)
375 /* Tell the CPU about any pending virtual interrupts or
376 * maintenance interrupts, following a change to the state
377 * of the CPU interface relevant to virtual interrupts.
379 * CAUTION: this function will call qemu_set_irq() on the
380 * CPU maintenance IRQ line, which is typically wired up
381 * to the GIC as a per-CPU interrupt. This means that it
382 * will recursively call back into the GIC code via
383 * gicv3_redist_set_irq() and thus into the CPU interface code's
384 * gicv3_cpuif_update(). It is therefore important that this
385 * function is only called as the final action of a CPU interface
386 * register write implementation, after all the GIC state
387 * fields have been updated. gicv3_cpuif_update() also must
388 * not cause this function to be called, but that happens
389 * naturally as a result of there being no architectural
390 * linkage between the physical and virtual GIC logic.
392 int idx;
393 int irqlevel = 0;
394 int fiqlevel = 0;
395 int maintlevel = 0;
396 ARMCPU *cpu = ARM_CPU(cs->cpu);
398 idx = hppvi_index(cs);
399 trace_gicv3_cpuif_virt_update(gicv3_redist_affid(cs), idx);
400 if (idx >= 0) {
401 uint64_t lr = cs->ich_lr_el2[idx];
403 if (icv_hppi_can_preempt(cs, lr)) {
404 /* Virtual interrupts are simple: G0 are always FIQ, and G1 IRQ */
405 if (lr & ICH_LR_EL2_GROUP) {
406 irqlevel = 1;
407 } else {
408 fiqlevel = 1;
413 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_EN) &&
414 maintenance_interrupt_state(cs) != 0) {
415 maintlevel = 1;
418 trace_gicv3_cpuif_virt_set_irqs(gicv3_redist_affid(cs), fiqlevel,
419 irqlevel, maintlevel);
421 qemu_set_irq(cs->parent_vfiq, fiqlevel);
422 qemu_set_irq(cs->parent_virq, irqlevel);
423 qemu_set_irq(cpu->gicv3_maintenance_interrupt, maintlevel);
426 static uint64_t icv_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
428 GICv3CPUState *cs = icc_cs_from_env(env);
429 int regno = ri->opc2 & 3;
430 int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
431 uint64_t value = cs->ich_apr[grp][regno];
433 trace_gicv3_icv_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
434 return value;
437 static void icv_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
438 uint64_t value)
440 GICv3CPUState *cs = icc_cs_from_env(env);
441 int regno = ri->opc2 & 3;
442 int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
444 trace_gicv3_icv_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
446 cs->ich_apr[grp][regno] = value & 0xFFFFFFFFU;
448 gicv3_cpuif_virt_update(cs);
449 return;
452 static uint64_t icv_bpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
454 GICv3CPUState *cs = icc_cs_from_env(env);
455 int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1NS;
456 uint64_t bpr;
457 bool satinc = false;
459 if (grp == GICV3_G1NS && (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR)) {
460 /* reads return bpr0 + 1 saturated to 7, writes ignored */
461 grp = GICV3_G0;
462 satinc = true;
465 bpr = read_vbpr(cs, grp);
467 if (satinc) {
468 bpr++;
469 bpr = MIN(bpr, 7);
472 trace_gicv3_icv_bpr_read(ri->crm == 8 ? 0 : 1, gicv3_redist_affid(cs), bpr);
474 return bpr;
477 static void icv_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
478 uint64_t value)
480 GICv3CPUState *cs = icc_cs_from_env(env);
481 int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1NS;
483 trace_gicv3_icv_bpr_write(ri->crm == 8 ? 0 : 1,
484 gicv3_redist_affid(cs), value);
486 if (grp == GICV3_G1NS && (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR)) {
487 /* reads return bpr0 + 1 saturated to 7, writes ignored */
488 return;
491 write_vbpr(cs, grp, value);
493 gicv3_cpuif_virt_update(cs);
496 static uint64_t icv_pmr_read(CPUARMState *env, const ARMCPRegInfo *ri)
498 GICv3CPUState *cs = icc_cs_from_env(env);
499 uint64_t value;
501 value = extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
502 ICH_VMCR_EL2_VPMR_LENGTH);
504 trace_gicv3_icv_pmr_read(gicv3_redist_affid(cs), value);
505 return value;
508 static void icv_pmr_write(CPUARMState *env, const ARMCPRegInfo *ri,
509 uint64_t value)
511 GICv3CPUState *cs = icc_cs_from_env(env);
513 trace_gicv3_icv_pmr_write(gicv3_redist_affid(cs), value);
515 value &= icv_fullprio_mask(cs);
517 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
518 ICH_VMCR_EL2_VPMR_LENGTH, value);
520 gicv3_cpuif_virt_update(cs);
523 static uint64_t icv_igrpen_read(CPUARMState *env, const ARMCPRegInfo *ri)
525 GICv3CPUState *cs = icc_cs_from_env(env);
526 int enbit;
527 uint64_t value;
529 enbit = ri->opc2 & 1 ? ICH_VMCR_EL2_VENG1_SHIFT : ICH_VMCR_EL2_VENG0_SHIFT;
530 value = extract64(cs->ich_vmcr_el2, enbit, 1);
532 trace_gicv3_icv_igrpen_read(ri->opc2 & 1 ? 1 : 0,
533 gicv3_redist_affid(cs), value);
534 return value;
537 static void icv_igrpen_write(CPUARMState *env, const ARMCPRegInfo *ri,
538 uint64_t value)
540 GICv3CPUState *cs = icc_cs_from_env(env);
541 int enbit;
543 trace_gicv3_icv_igrpen_write(ri->opc2 & 1 ? 1 : 0,
544 gicv3_redist_affid(cs), value);
546 enbit = ri->opc2 & 1 ? ICH_VMCR_EL2_VENG1_SHIFT : ICH_VMCR_EL2_VENG0_SHIFT;
548 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, enbit, 1, value);
549 gicv3_cpuif_virt_update(cs);
552 static uint64_t icv_ctlr_read(CPUARMState *env, const ARMCPRegInfo *ri)
554 GICv3CPUState *cs = icc_cs_from_env(env);
555 uint64_t value;
557 /* Note that the fixed fields here (A3V, SEIS, IDbits, PRIbits)
558 * should match the ones reported in ich_vtr_read().
560 value = ICC_CTLR_EL1_A3V | (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
561 (7 << ICC_CTLR_EL1_PRIBITS_SHIFT);
563 if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VEOIM) {
564 value |= ICC_CTLR_EL1_EOIMODE;
567 if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR) {
568 value |= ICC_CTLR_EL1_CBPR;
571 trace_gicv3_icv_ctlr_read(gicv3_redist_affid(cs), value);
572 return value;
575 static void icv_ctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
576 uint64_t value)
578 GICv3CPUState *cs = icc_cs_from_env(env);
580 trace_gicv3_icv_ctlr_write(gicv3_redist_affid(cs), value);
582 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VCBPR_SHIFT,
583 1, value & ICC_CTLR_EL1_CBPR ? 1 : 0);
584 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VEOIM_SHIFT,
585 1, value & ICC_CTLR_EL1_EOIMODE ? 1 : 0);
587 gicv3_cpuif_virt_update(cs);
590 static uint64_t icv_rpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
592 GICv3CPUState *cs = icc_cs_from_env(env);
593 int prio = ich_highest_active_virt_prio(cs);
595 trace_gicv3_icv_rpr_read(gicv3_redist_affid(cs), prio);
596 return prio;
599 static uint64_t icv_hppir_read(CPUARMState *env, const ARMCPRegInfo *ri)
601 GICv3CPUState *cs = icc_cs_from_env(env);
602 int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
603 int idx = hppvi_index(cs);
604 uint64_t value = INTID_SPURIOUS;
606 if (idx >= 0) {
607 uint64_t lr = cs->ich_lr_el2[idx];
608 int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
610 if (grp == thisgrp) {
611 value = ich_lr_vintid(lr);
615 trace_gicv3_icv_hppir_read(grp, gicv3_redist_affid(cs), value);
616 return value;
619 static void icv_activate_irq(GICv3CPUState *cs, int idx, int grp)
621 /* Activate the interrupt in the specified list register
622 * by moving it from Pending to Active state, and update the
623 * Active Priority Registers.
625 uint32_t mask = icv_gprio_mask(cs, grp);
626 int prio = ich_lr_prio(cs->ich_lr_el2[idx]) & mask;
627 int aprbit = prio >> (8 - cs->vprebits);
628 int regno = aprbit / 32;
629 int regbit = aprbit % 32;
631 cs->ich_lr_el2[idx] &= ~ICH_LR_EL2_STATE_PENDING_BIT;
632 cs->ich_lr_el2[idx] |= ICH_LR_EL2_STATE_ACTIVE_BIT;
633 cs->ich_apr[grp][regno] |= (1 << regbit);
636 static uint64_t icv_iar_read(CPUARMState *env, const ARMCPRegInfo *ri)
638 GICv3CPUState *cs = icc_cs_from_env(env);
639 int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
640 int idx = hppvi_index(cs);
641 uint64_t intid = INTID_SPURIOUS;
643 if (idx >= 0) {
644 uint64_t lr = cs->ich_lr_el2[idx];
645 int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
647 if (thisgrp == grp && icv_hppi_can_preempt(cs, lr)) {
648 intid = ich_lr_vintid(lr);
649 if (!gicv3_intid_is_special(intid)) {
650 icv_activate_irq(cs, idx, grp);
651 } else {
652 /* Interrupt goes from Pending to Invalid */
653 cs->ich_lr_el2[idx] &= ~ICH_LR_EL2_STATE_PENDING_BIT;
654 /* We will now return the (bogus) ID from the list register,
655 * as per the pseudocode.
661 trace_gicv3_icv_iar_read(ri->crm == 8 ? 0 : 1,
662 gicv3_redist_affid(cs), intid);
664 gicv3_cpuif_virt_update(cs);
666 return intid;
669 static int icc_highest_active_prio(GICv3CPUState *cs)
671 /* Calculate the current running priority based on the set bits
672 * in the Active Priority Registers.
674 int i;
676 for (i = 0; i < ARRAY_SIZE(cs->icc_apr[0]); i++) {
677 uint32_t apr = cs->icc_apr[GICV3_G0][i] |
678 cs->icc_apr[GICV3_G1][i] | cs->icc_apr[GICV3_G1NS][i];
680 if (!apr) {
681 continue;
683 return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1);
685 /* No current active interrupts: return idle priority */
686 return 0xff;
689 static uint32_t icc_gprio_mask(GICv3CPUState *cs, int group)
691 /* Return a mask word which clears the subpriority bits from
692 * a priority value for an interrupt in the specified group.
693 * This depends on the BPR value. For CBPR0 (S or NS):
694 * a BPR of 0 means the group priority bits are [7:1];
695 * a BPR of 1 means they are [7:2], and so on down to
696 * a BPR of 7 meaning no group priority bits at all.
697 * For CBPR1 NS:
698 * a BPR of 0 is impossible (the minimum value is 1)
699 * a BPR of 1 means the group priority bits are [7:1];
700 * a BPR of 2 means they are [7:2], and so on down to
701 * a BPR of 7 meaning the group priority is [7].
703 * Which BPR to use depends on the group of the interrupt and
704 * the current ICC_CTLR.CBPR settings.
706 * This corresponds to the GroupBits() pseudocode.
708 int bpr;
710 if ((group == GICV3_G1 && cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR) ||
711 (group == GICV3_G1NS &&
712 cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
713 group = GICV3_G0;
716 bpr = cs->icc_bpr[group] & 7;
718 if (group == GICV3_G1NS) {
719 assert(bpr > 0);
720 bpr--;
723 return ~0U << (bpr + 1);
726 static bool icc_no_enabled_hppi(GICv3CPUState *cs)
728 /* Return true if there is no pending interrupt, or the
729 * highest priority pending interrupt is in a group which has been
730 * disabled at the CPU interface by the ICC_IGRPEN* register enable bits.
732 return cs->hppi.prio == 0xff || (cs->icc_igrpen[cs->hppi.grp] == 0);
735 static bool icc_hppi_can_preempt(GICv3CPUState *cs)
737 /* Return true if we have a pending interrupt of sufficient
738 * priority to preempt.
740 int rprio;
741 uint32_t mask;
743 if (icc_no_enabled_hppi(cs)) {
744 return false;
747 if (cs->hppi.prio >= cs->icc_pmr_el1) {
748 /* Priority mask masks this interrupt */
749 return false;
752 rprio = icc_highest_active_prio(cs);
753 if (rprio == 0xff) {
754 /* No currently running interrupt so we can preempt */
755 return true;
758 mask = icc_gprio_mask(cs, cs->hppi.grp);
760 /* We only preempt a running interrupt if the pending interrupt's
761 * group priority is sufficient (the subpriorities are not considered).
763 if ((cs->hppi.prio & mask) < (rprio & mask)) {
764 return true;
767 return false;
770 void gicv3_cpuif_update(GICv3CPUState *cs)
772 /* Tell the CPU about its highest priority pending interrupt */
773 int irqlevel = 0;
774 int fiqlevel = 0;
775 ARMCPU *cpu = ARM_CPU(cs->cpu);
776 CPUARMState *env = &cpu->env;
778 g_assert(qemu_mutex_iothread_locked());
780 trace_gicv3_cpuif_update(gicv3_redist_affid(cs), cs->hppi.irq,
781 cs->hppi.grp, cs->hppi.prio);
783 if (cs->hppi.grp == GICV3_G1 && !arm_feature(env, ARM_FEATURE_EL3)) {
784 /* If a Security-enabled GIC sends a G1S interrupt to a
785 * Security-disabled CPU, we must treat it as if it were G0.
787 cs->hppi.grp = GICV3_G0;
790 if (icc_hppi_can_preempt(cs)) {
791 /* We have an interrupt: should we signal it as IRQ or FIQ?
792 * This is described in the GICv3 spec section 4.6.2.
794 bool isfiq;
796 switch (cs->hppi.grp) {
797 case GICV3_G0:
798 isfiq = true;
799 break;
800 case GICV3_G1:
801 isfiq = (!arm_is_secure(env) ||
802 (arm_current_el(env) == 3 && arm_el_is_aa64(env, 3)));
803 break;
804 case GICV3_G1NS:
805 isfiq = arm_is_secure(env);
806 break;
807 default:
808 g_assert_not_reached();
811 if (isfiq) {
812 fiqlevel = 1;
813 } else {
814 irqlevel = 1;
818 trace_gicv3_cpuif_set_irqs(gicv3_redist_affid(cs), fiqlevel, irqlevel);
820 qemu_set_irq(cs->parent_fiq, fiqlevel);
821 qemu_set_irq(cs->parent_irq, irqlevel);
824 static uint64_t icc_pmr_read(CPUARMState *env, const ARMCPRegInfo *ri)
826 GICv3CPUState *cs = icc_cs_from_env(env);
827 uint32_t value = cs->icc_pmr_el1;
829 if (icv_access(env, HCR_FMO | HCR_IMO)) {
830 return icv_pmr_read(env, ri);
833 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env) &&
834 (env->cp15.scr_el3 & SCR_FIQ)) {
835 /* NS access and Group 0 is inaccessible to NS: return the
836 * NS view of the current priority
838 if ((value & 0x80) == 0) {
839 /* Secure priorities not visible to NS */
840 value = 0;
841 } else if (value != 0xff) {
842 value = (value << 1) & 0xff;
846 trace_gicv3_icc_pmr_read(gicv3_redist_affid(cs), value);
848 return value;
851 static void icc_pmr_write(CPUARMState *env, const ARMCPRegInfo *ri,
852 uint64_t value)
854 GICv3CPUState *cs = icc_cs_from_env(env);
856 if (icv_access(env, HCR_FMO | HCR_IMO)) {
857 return icv_pmr_write(env, ri, value);
860 trace_gicv3_icc_pmr_write(gicv3_redist_affid(cs), value);
862 value &= 0xff;
864 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env) &&
865 (env->cp15.scr_el3 & SCR_FIQ)) {
866 /* NS access and Group 0 is inaccessible to NS: return the
867 * NS view of the current priority
869 if (!(cs->icc_pmr_el1 & 0x80)) {
870 /* Current PMR in the secure range, don't allow NS to change it */
871 return;
873 value = (value >> 1) | 0x80;
875 cs->icc_pmr_el1 = value;
876 gicv3_cpuif_update(cs);
879 static void icc_activate_irq(GICv3CPUState *cs, int irq)
881 /* Move the interrupt from the Pending state to Active, and update
882 * the Active Priority Registers
884 uint32_t mask = icc_gprio_mask(cs, cs->hppi.grp);
885 int prio = cs->hppi.prio & mask;
886 int aprbit = prio >> 1;
887 int regno = aprbit / 32;
888 int regbit = aprbit % 32;
890 cs->icc_apr[cs->hppi.grp][regno] |= (1 << regbit);
892 if (irq < GIC_INTERNAL) {
893 cs->gicr_iactiver0 = deposit32(cs->gicr_iactiver0, irq, 1, 1);
894 cs->gicr_ipendr0 = deposit32(cs->gicr_ipendr0, irq, 1, 0);
895 gicv3_redist_update(cs);
896 } else if (irq < GICV3_LPI_INTID_START) {
897 gicv3_gicd_active_set(cs->gic, irq);
898 gicv3_gicd_pending_clear(cs->gic, irq);
899 gicv3_update(cs->gic, irq, 1);
900 } else {
901 gicv3_redist_lpi_pending(cs, irq, 0);
905 static uint64_t icc_hppir0_value(GICv3CPUState *cs, CPUARMState *env)
907 /* Return the highest priority pending interrupt register value
908 * for group 0.
910 bool irq_is_secure;
912 if (cs->hppi.prio == 0xff) {
913 return INTID_SPURIOUS;
916 /* Check whether we can return the interrupt or if we should return
917 * a special identifier, as per the CheckGroup0ForSpecialIdentifiers
918 * pseudocode. (We can simplify a little because for us ICC_SRE_EL1.RM
919 * is always zero.)
921 irq_is_secure = (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) &&
922 (cs->hppi.grp != GICV3_G1NS));
924 if (cs->hppi.grp != GICV3_G0 && !arm_is_el3_or_mon(env)) {
925 return INTID_SPURIOUS;
927 if (irq_is_secure && !arm_is_secure(env)) {
928 /* Secure interrupts not visible to Nonsecure */
929 return INTID_SPURIOUS;
932 if (cs->hppi.grp != GICV3_G0) {
933 /* Indicate to EL3 that there's a Group 1 interrupt for the other
934 * state pending.
936 return irq_is_secure ? INTID_SECURE : INTID_NONSECURE;
939 return cs->hppi.irq;
942 static uint64_t icc_hppir1_value(GICv3CPUState *cs, CPUARMState *env)
944 /* Return the highest priority pending interrupt register value
945 * for group 1.
947 bool irq_is_secure;
949 if (cs->hppi.prio == 0xff) {
950 return INTID_SPURIOUS;
953 /* Check whether we can return the interrupt or if we should return
954 * a special identifier, as per the CheckGroup1ForSpecialIdentifiers
955 * pseudocode. (We can simplify a little because for us ICC_SRE_EL1.RM
956 * is always zero.)
958 irq_is_secure = (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) &&
959 (cs->hppi.grp != GICV3_G1NS));
961 if (cs->hppi.grp == GICV3_G0) {
962 /* Group 0 interrupts not visible via HPPIR1 */
963 return INTID_SPURIOUS;
965 if (irq_is_secure) {
966 if (!arm_is_secure(env)) {
967 /* Secure interrupts not visible in Non-secure */
968 return INTID_SPURIOUS;
970 } else if (!arm_is_el3_or_mon(env) && arm_is_secure(env)) {
971 /* Group 1 non-secure interrupts not visible in Secure EL1 */
972 return INTID_SPURIOUS;
975 return cs->hppi.irq;
978 static uint64_t icc_iar0_read(CPUARMState *env, const ARMCPRegInfo *ri)
980 GICv3CPUState *cs = icc_cs_from_env(env);
981 uint64_t intid;
983 if (icv_access(env, HCR_FMO)) {
984 return icv_iar_read(env, ri);
987 if (!icc_hppi_can_preempt(cs)) {
988 intid = INTID_SPURIOUS;
989 } else {
990 intid = icc_hppir0_value(cs, env);
993 if (!gicv3_intid_is_special(intid)) {
994 icc_activate_irq(cs, intid);
997 trace_gicv3_icc_iar0_read(gicv3_redist_affid(cs), intid);
998 return intid;
1001 static uint64_t icc_iar1_read(CPUARMState *env, const ARMCPRegInfo *ri)
1003 GICv3CPUState *cs = icc_cs_from_env(env);
1004 uint64_t intid;
1006 if (icv_access(env, HCR_IMO)) {
1007 return icv_iar_read(env, ri);
1010 if (!icc_hppi_can_preempt(cs)) {
1011 intid = INTID_SPURIOUS;
1012 } else {
1013 intid = icc_hppir1_value(cs, env);
1016 if (!gicv3_intid_is_special(intid)) {
1017 icc_activate_irq(cs, intid);
1020 trace_gicv3_icc_iar1_read(gicv3_redist_affid(cs), intid);
1021 return intid;
1024 static void icc_drop_prio(GICv3CPUState *cs, int grp)
1026 /* Drop the priority of the currently active interrupt in
1027 * the specified group.
1029 * Note that we can guarantee (because of the requirement to nest
1030 * ICC_IAR reads [which activate an interrupt and raise priority]
1031 * with ICC_EOIR writes [which drop the priority for the interrupt])
1032 * that the interrupt we're being called for is the highest priority
1033 * active interrupt, meaning that it has the lowest set bit in the
1034 * APR registers.
1036 * If the guest does not honour the ordering constraints then the
1037 * behaviour of the GIC is UNPREDICTABLE, which for us means that
1038 * the values of the APR registers might become incorrect and the
1039 * running priority will be wrong, so interrupts that should preempt
1040 * might not do so, and interrupts that should not preempt might do so.
1042 int i;
1044 for (i = 0; i < ARRAY_SIZE(cs->icc_apr[grp]); i++) {
1045 uint64_t *papr = &cs->icc_apr[grp][i];
1047 if (!*papr) {
1048 continue;
1050 /* Clear the lowest set bit */
1051 *papr &= *papr - 1;
1052 break;
1055 /* running priority change means we need an update for this cpu i/f */
1056 gicv3_cpuif_update(cs);
1059 static bool icc_eoi_split(CPUARMState *env, GICv3CPUState *cs)
1061 /* Return true if we should split priority drop and interrupt
1062 * deactivation, ie whether the relevant EOIMode bit is set.
1064 if (arm_is_el3_or_mon(env)) {
1065 return cs->icc_ctlr_el3 & ICC_CTLR_EL3_EOIMODE_EL3;
1067 if (arm_is_secure_below_el3(env)) {
1068 return cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_EOIMODE;
1069 } else {
1070 return cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE;
1074 static int icc_highest_active_group(GICv3CPUState *cs)
1076 /* Return the group with the highest priority active interrupt.
1077 * We can do this by just comparing the APRs to see which one
1078 * has the lowest set bit.
1079 * (If more than one group is active at the same priority then
1080 * we're in UNPREDICTABLE territory.)
1082 int i;
1084 for (i = 0; i < ARRAY_SIZE(cs->icc_apr[0]); i++) {
1085 int g0ctz = ctz32(cs->icc_apr[GICV3_G0][i]);
1086 int g1ctz = ctz32(cs->icc_apr[GICV3_G1][i]);
1087 int g1nsctz = ctz32(cs->icc_apr[GICV3_G1NS][i]);
1089 if (g1nsctz < g0ctz && g1nsctz < g1ctz) {
1090 return GICV3_G1NS;
1092 if (g1ctz < g0ctz) {
1093 return GICV3_G1;
1095 if (g0ctz < 32) {
1096 return GICV3_G0;
1099 /* No set active bits? UNPREDICTABLE; return -1 so the caller
1100 * ignores the spurious EOI attempt.
1102 return -1;
1105 static void icc_deactivate_irq(GICv3CPUState *cs, int irq)
1107 if (irq < GIC_INTERNAL) {
1108 cs->gicr_iactiver0 = deposit32(cs->gicr_iactiver0, irq, 1, 0);
1109 gicv3_redist_update(cs);
1110 } else {
1111 gicv3_gicd_active_clear(cs->gic, irq);
1112 gicv3_update(cs->gic, irq, 1);
1116 static bool icv_eoi_split(CPUARMState *env, GICv3CPUState *cs)
1118 /* Return true if we should split priority drop and interrupt
1119 * deactivation, ie whether the virtual EOIMode bit is set.
1121 return cs->ich_vmcr_el2 & ICH_VMCR_EL2_VEOIM;
1124 static int icv_find_active(GICv3CPUState *cs, int irq)
1126 /* Given an interrupt number for an active interrupt, return the index
1127 * of the corresponding list register, or -1 if there is no match.
1128 * Corresponds to FindActiveVirtualInterrupt pseudocode.
1130 int i;
1132 for (i = 0; i < cs->num_list_regs; i++) {
1133 uint64_t lr = cs->ich_lr_el2[i];
1135 if ((lr & ICH_LR_EL2_STATE_ACTIVE_BIT) && ich_lr_vintid(lr) == irq) {
1136 return i;
1140 return -1;
1143 static void icv_deactivate_irq(GICv3CPUState *cs, int idx)
1145 /* Deactivate the interrupt in the specified list register index */
1146 uint64_t lr = cs->ich_lr_el2[idx];
1148 if (lr & ICH_LR_EL2_HW) {
1149 /* Deactivate the associated physical interrupt */
1150 int pirq = ich_lr_pintid(lr);
1152 if (pirq < INTID_SECURE) {
1153 icc_deactivate_irq(cs, pirq);
1157 /* Clear the 'active' part of the state, so ActivePending->Pending
1158 * and Active->Invalid.
1160 lr &= ~ICH_LR_EL2_STATE_ACTIVE_BIT;
1161 cs->ich_lr_el2[idx] = lr;
1164 static void icv_increment_eoicount(GICv3CPUState *cs)
1166 /* Increment the EOICOUNT field in ICH_HCR_EL2 */
1167 int eoicount = extract64(cs->ich_hcr_el2, ICH_HCR_EL2_EOICOUNT_SHIFT,
1168 ICH_HCR_EL2_EOICOUNT_LENGTH);
1170 cs->ich_hcr_el2 = deposit64(cs->ich_hcr_el2, ICH_HCR_EL2_EOICOUNT_SHIFT,
1171 ICH_HCR_EL2_EOICOUNT_LENGTH, eoicount + 1);
1174 static int icv_drop_prio(GICv3CPUState *cs)
1176 /* Drop the priority of the currently active virtual interrupt
1177 * (favouring group 0 if there is a set active bit at
1178 * the same priority for both group 0 and group 1).
1179 * Return the priority value for the bit we just cleared,
1180 * or 0xff if no bits were set in the AP registers at all.
1181 * Note that though the ich_apr[] are uint64_t only the low
1182 * 32 bits are actually relevant.
1184 int i;
1185 int aprmax = 1 << (cs->vprebits - 5);
1187 assert(aprmax <= ARRAY_SIZE(cs->ich_apr[0]));
1189 for (i = 0; i < aprmax; i++) {
1190 uint64_t *papr0 = &cs->ich_apr[GICV3_G0][i];
1191 uint64_t *papr1 = &cs->ich_apr[GICV3_G1NS][i];
1192 int apr0count, apr1count;
1194 if (!*papr0 && !*papr1) {
1195 continue;
1198 /* We can't just use the bit-twiddling hack icc_drop_prio() does
1199 * because we need to return the bit number we cleared so
1200 * it can be compared against the list register's priority field.
1202 apr0count = ctz32(*papr0);
1203 apr1count = ctz32(*papr1);
1205 if (apr0count <= apr1count) {
1206 *papr0 &= *papr0 - 1;
1207 return (apr0count + i * 32) << (icv_min_vbpr(cs) + 1);
1208 } else {
1209 *papr1 &= *papr1 - 1;
1210 return (apr1count + i * 32) << (icv_min_vbpr(cs) + 1);
1213 return 0xff;
1216 static void icv_dir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1217 uint64_t value)
1219 /* Deactivate interrupt */
1220 GICv3CPUState *cs = icc_cs_from_env(env);
1221 int idx;
1222 int irq = value & 0xffffff;
1224 trace_gicv3_icv_dir_write(gicv3_redist_affid(cs), value);
1226 if (irq >= GICV3_MAXIRQ) {
1227 /* Also catches special interrupt numbers and LPIs */
1228 return;
1231 if (!icv_eoi_split(env, cs)) {
1232 return;
1235 idx = icv_find_active(cs, irq);
1237 if (idx < 0) {
1238 /* No list register matching this, so increment the EOI count
1239 * (might trigger a maintenance interrupt)
1241 icv_increment_eoicount(cs);
1242 } else {
1243 icv_deactivate_irq(cs, idx);
1246 gicv3_cpuif_virt_update(cs);
1249 static void icv_eoir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1250 uint64_t value)
1252 /* End of Interrupt */
1253 GICv3CPUState *cs = icc_cs_from_env(env);
1254 int irq = value & 0xffffff;
1255 int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
1256 int idx, dropprio;
1258 trace_gicv3_icv_eoir_write(ri->crm == 8 ? 0 : 1,
1259 gicv3_redist_affid(cs), value);
1261 if (gicv3_intid_is_special(irq)) {
1262 return;
1265 /* We implement the IMPDEF choice of "drop priority before doing
1266 * error checks" (because that lets us avoid scanning the AP
1267 * registers twice).
1269 dropprio = icv_drop_prio(cs);
1270 if (dropprio == 0xff) {
1271 /* No active interrupt. It is CONSTRAINED UNPREDICTABLE
1272 * whether the list registers are checked in this
1273 * situation; we choose not to.
1275 return;
1278 idx = icv_find_active(cs, irq);
1280 if (idx < 0) {
1281 /* No valid list register corresponding to EOI ID */
1282 icv_increment_eoicount(cs);
1283 } else {
1284 uint64_t lr = cs->ich_lr_el2[idx];
1285 int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
1286 int lr_gprio = ich_lr_prio(lr) & icv_gprio_mask(cs, grp);
1288 if (thisgrp == grp && lr_gprio == dropprio) {
1289 if (!icv_eoi_split(env, cs)) {
1290 /* Priority drop and deactivate not split: deactivate irq now */
1291 icv_deactivate_irq(cs, idx);
1296 gicv3_cpuif_virt_update(cs);
1299 static void icc_eoir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1300 uint64_t value)
1302 /* End of Interrupt */
1303 GICv3CPUState *cs = icc_cs_from_env(env);
1304 int irq = value & 0xffffff;
1305 int grp;
1306 bool is_eoir0 = ri->crm == 8;
1308 if (icv_access(env, is_eoir0 ? HCR_FMO : HCR_IMO)) {
1309 icv_eoir_write(env, ri, value);
1310 return;
1313 trace_gicv3_icc_eoir_write(is_eoir0 ? 0 : 1,
1314 gicv3_redist_affid(cs), value);
1316 if ((irq >= cs->gic->num_irq) &&
1317 !(cs->gic->lpi_enable && (irq >= GICV3_LPI_INTID_START))) {
1318 /* This handles two cases:
1319 * 1. If software writes the ID of a spurious interrupt [ie 1020-1023]
1320 * to the GICC_EOIR, the GIC ignores that write.
1321 * 2. If software writes the number of a non-existent interrupt
1322 * this must be a subcase of "value written does not match the last
1323 * valid interrupt value read from the Interrupt Acknowledge
1324 * register" and so this is UNPREDICTABLE. We choose to ignore it.
1326 return;
1329 grp = icc_highest_active_group(cs);
1330 switch (grp) {
1331 case GICV3_G0:
1332 if (!is_eoir0) {
1333 return;
1335 if (!(cs->gic->gicd_ctlr & GICD_CTLR_DS)
1336 && arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env)) {
1337 return;
1339 break;
1340 case GICV3_G1:
1341 if (is_eoir0) {
1342 return;
1344 if (!arm_is_secure(env)) {
1345 return;
1347 break;
1348 case GICV3_G1NS:
1349 if (is_eoir0) {
1350 return;
1352 if (!arm_is_el3_or_mon(env) && arm_is_secure(env)) {
1353 return;
1355 break;
1356 default:
1357 qemu_log_mask(LOG_GUEST_ERROR,
1358 "%s: IRQ %d isn't active\n", __func__, irq);
1359 return;
1362 icc_drop_prio(cs, grp);
1364 if (!icc_eoi_split(env, cs)) {
1365 /* Priority drop and deactivate not split: deactivate irq now */
1366 icc_deactivate_irq(cs, irq);
1370 static uint64_t icc_hppir0_read(CPUARMState *env, const ARMCPRegInfo *ri)
1372 GICv3CPUState *cs = icc_cs_from_env(env);
1373 uint64_t value;
1375 if (icv_access(env, HCR_FMO)) {
1376 return icv_hppir_read(env, ri);
1379 value = icc_hppir0_value(cs, env);
1380 trace_gicv3_icc_hppir0_read(gicv3_redist_affid(cs), value);
1381 return value;
1384 static uint64_t icc_hppir1_read(CPUARMState *env, const ARMCPRegInfo *ri)
1386 GICv3CPUState *cs = icc_cs_from_env(env);
1387 uint64_t value;
1389 if (icv_access(env, HCR_IMO)) {
1390 return icv_hppir_read(env, ri);
1393 value = icc_hppir1_value(cs, env);
1394 trace_gicv3_icc_hppir1_read(gicv3_redist_affid(cs), value);
1395 return value;
1398 static uint64_t icc_bpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1400 GICv3CPUState *cs = icc_cs_from_env(env);
1401 int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1;
1402 bool satinc = false;
1403 uint64_t bpr;
1405 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1406 return icv_bpr_read(env, ri);
1409 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1410 grp = GICV3_G1NS;
1413 if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) &&
1414 (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) {
1415 /* CBPR_EL1S means secure EL1 or AArch32 EL3 !Mon BPR1 accesses
1416 * modify BPR0
1418 grp = GICV3_G0;
1421 if (grp == GICV3_G1NS && arm_current_el(env) < 3 &&
1422 (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
1423 /* reads return bpr0 + 1 sat to 7, writes ignored */
1424 grp = GICV3_G0;
1425 satinc = true;
1428 bpr = cs->icc_bpr[grp];
1429 if (satinc) {
1430 bpr++;
1431 bpr = MIN(bpr, 7);
1434 trace_gicv3_icc_bpr_read(ri->crm == 8 ? 0 : 1, gicv3_redist_affid(cs), bpr);
1436 return bpr;
1439 static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1440 uint64_t value)
1442 GICv3CPUState *cs = icc_cs_from_env(env);
1443 int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1;
1444 uint64_t minval;
1446 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1447 icv_bpr_write(env, ri, value);
1448 return;
1451 trace_gicv3_icc_bpr_write(ri->crm == 8 ? 0 : 1,
1452 gicv3_redist_affid(cs), value);
1454 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1455 grp = GICV3_G1NS;
1458 if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) &&
1459 (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) {
1460 /* CBPR_EL1S means secure EL1 or AArch32 EL3 !Mon BPR1 accesses
1461 * modify BPR0
1463 grp = GICV3_G0;
1466 if (grp == GICV3_G1NS && arm_current_el(env) < 3 &&
1467 (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
1468 /* reads return bpr0 + 1 sat to 7, writes ignored */
1469 return;
1472 minval = (grp == GICV3_G1NS) ? GIC_MIN_BPR_NS : GIC_MIN_BPR;
1473 if (value < minval) {
1474 value = minval;
1477 cs->icc_bpr[grp] = value & 7;
1478 gicv3_cpuif_update(cs);
1481 static uint64_t icc_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
1483 GICv3CPUState *cs = icc_cs_from_env(env);
1484 uint64_t value;
1486 int regno = ri->opc2 & 3;
1487 int grp = (ri->crm & 1) ? GICV3_G1 : GICV3_G0;
1489 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1490 return icv_ap_read(env, ri);
1493 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1494 grp = GICV3_G1NS;
1497 value = cs->icc_apr[grp][regno];
1499 trace_gicv3_icc_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
1500 return value;
1503 static void icc_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1504 uint64_t value)
1506 GICv3CPUState *cs = icc_cs_from_env(env);
1508 int regno = ri->opc2 & 3;
1509 int grp = (ri->crm & 1) ? GICV3_G1 : GICV3_G0;
1511 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1512 icv_ap_write(env, ri, value);
1513 return;
1516 trace_gicv3_icc_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
1518 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1519 grp = GICV3_G1NS;
1522 /* It's not possible to claim that a Non-secure interrupt is active
1523 * at a priority outside the Non-secure range (128..255), since this
1524 * would otherwise allow malicious NS code to block delivery of S interrupts
1525 * by writing a bad value to these registers.
1527 if (grp == GICV3_G1NS && regno < 2 && arm_feature(env, ARM_FEATURE_EL3)) {
1528 return;
1531 cs->icc_apr[grp][regno] = value & 0xFFFFFFFFU;
1532 gicv3_cpuif_update(cs);
1535 static void icc_dir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1536 uint64_t value)
1538 /* Deactivate interrupt */
1539 GICv3CPUState *cs = icc_cs_from_env(env);
1540 int irq = value & 0xffffff;
1541 bool irq_is_secure, single_sec_state, irq_is_grp0;
1542 bool route_fiq_to_el3, route_irq_to_el3, route_fiq_to_el2, route_irq_to_el2;
1544 if (icv_access(env, HCR_FMO | HCR_IMO)) {
1545 icv_dir_write(env, ri, value);
1546 return;
1549 trace_gicv3_icc_dir_write(gicv3_redist_affid(cs), value);
1551 if (irq >= cs->gic->num_irq) {
1552 /* Also catches special interrupt numbers and LPIs */
1553 return;
1556 if (!icc_eoi_split(env, cs)) {
1557 return;
1560 int grp = gicv3_irq_group(cs->gic, cs, irq);
1562 single_sec_state = cs->gic->gicd_ctlr & GICD_CTLR_DS;
1563 irq_is_secure = !single_sec_state && (grp != GICV3_G1NS);
1564 irq_is_grp0 = grp == GICV3_G0;
1566 /* Check whether we're allowed to deactivate this interrupt based
1567 * on its group and the current CPU state.
1568 * These checks are laid out to correspond to the spec's pseudocode.
1570 route_fiq_to_el3 = env->cp15.scr_el3 & SCR_FIQ;
1571 route_irq_to_el3 = env->cp15.scr_el3 & SCR_IRQ;
1572 /* No need to include !IsSecure in route_*_to_el2 as it's only
1573 * tested in cases where we know !IsSecure is true.
1575 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
1576 route_fiq_to_el2 = hcr_el2 & HCR_FMO;
1577 route_irq_to_el2 = hcr_el2 & HCR_IMO;
1579 switch (arm_current_el(env)) {
1580 case 3:
1581 break;
1582 case 2:
1583 if (single_sec_state && irq_is_grp0 && !route_fiq_to_el3) {
1584 break;
1586 if (!irq_is_secure && !irq_is_grp0 && !route_irq_to_el3) {
1587 break;
1589 return;
1590 case 1:
1591 if (!arm_is_secure_below_el3(env)) {
1592 if (single_sec_state && irq_is_grp0 &&
1593 !route_fiq_to_el3 && !route_fiq_to_el2) {
1594 break;
1596 if (!irq_is_secure && !irq_is_grp0 &&
1597 !route_irq_to_el3 && !route_irq_to_el2) {
1598 break;
1600 } else {
1601 if (irq_is_grp0 && !route_fiq_to_el3) {
1602 break;
1604 if (!irq_is_grp0 &&
1605 (!irq_is_secure || !single_sec_state) &&
1606 !route_irq_to_el3) {
1607 break;
1610 return;
1611 default:
1612 g_assert_not_reached();
1615 icc_deactivate_irq(cs, irq);
1618 static uint64_t icc_rpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1620 GICv3CPUState *cs = icc_cs_from_env(env);
1621 int prio;
1623 if (icv_access(env, HCR_FMO | HCR_IMO)) {
1624 return icv_rpr_read(env, ri);
1627 prio = icc_highest_active_prio(cs);
1629 if (arm_feature(env, ARM_FEATURE_EL3) &&
1630 !arm_is_secure(env) && (env->cp15.scr_el3 & SCR_FIQ)) {
1631 /* NS GIC access and Group 0 is inaccessible to NS */
1632 if ((prio & 0x80) == 0) {
1633 /* NS mustn't see priorities in the Secure half of the range */
1634 prio = 0;
1635 } else if (prio != 0xff) {
1636 /* Non-idle priority: show the Non-secure view of it */
1637 prio = (prio << 1) & 0xff;
1641 trace_gicv3_icc_rpr_read(gicv3_redist_affid(cs), prio);
1642 return prio;
1645 static void icc_generate_sgi(CPUARMState *env, GICv3CPUState *cs,
1646 uint64_t value, int grp, bool ns)
1648 GICv3State *s = cs->gic;
1650 /* Extract Aff3/Aff2/Aff1 and shift into the bottom 24 bits */
1651 uint64_t aff = extract64(value, 48, 8) << 16 |
1652 extract64(value, 32, 8) << 8 |
1653 extract64(value, 16, 8);
1654 uint32_t targetlist = extract64(value, 0, 16);
1655 uint32_t irq = extract64(value, 24, 4);
1656 bool irm = extract64(value, 40, 1);
1657 int i;
1659 if (grp == GICV3_G1 && s->gicd_ctlr & GICD_CTLR_DS) {
1660 /* If GICD_CTLR.DS == 1, the Distributor treats Secure Group 1
1661 * interrupts as Group 0 interrupts and must send Secure Group 0
1662 * interrupts to the target CPUs.
1664 grp = GICV3_G0;
1667 trace_gicv3_icc_generate_sgi(gicv3_redist_affid(cs), irq, irm,
1668 aff, targetlist);
1670 for (i = 0; i < s->num_cpu; i++) {
1671 GICv3CPUState *ocs = &s->cpu[i];
1673 if (irm) {
1674 /* IRM == 1 : route to all CPUs except self */
1675 if (cs == ocs) {
1676 continue;
1678 } else {
1679 /* IRM == 0 : route to Aff3.Aff2.Aff1.n for all n in [0..15]
1680 * where the corresponding bit is set in targetlist
1682 int aff0;
1684 if (ocs->gicr_typer >> 40 != aff) {
1685 continue;
1687 aff0 = extract64(ocs->gicr_typer, 32, 8);
1688 if (aff0 > 15 || extract32(targetlist, aff0, 1) == 0) {
1689 continue;
1693 /* The redistributor will check against its own GICR_NSACR as needed */
1694 gicv3_redist_send_sgi(ocs, grp, irq, ns);
1698 static void icc_sgi0r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1699 uint64_t value)
1701 /* Generate Secure Group 0 SGI. */
1702 GICv3CPUState *cs = icc_cs_from_env(env);
1703 bool ns = !arm_is_secure(env);
1705 icc_generate_sgi(env, cs, value, GICV3_G0, ns);
1708 static void icc_sgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1709 uint64_t value)
1711 /* Generate Group 1 SGI for the current Security state */
1712 GICv3CPUState *cs = icc_cs_from_env(env);
1713 int grp;
1714 bool ns = !arm_is_secure(env);
1716 grp = ns ? GICV3_G1NS : GICV3_G1;
1717 icc_generate_sgi(env, cs, value, grp, ns);
1720 static void icc_asgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1721 uint64_t value)
1723 /* Generate Group 1 SGI for the Security state that is not
1724 * the current state
1726 GICv3CPUState *cs = icc_cs_from_env(env);
1727 int grp;
1728 bool ns = !arm_is_secure(env);
1730 grp = ns ? GICV3_G1 : GICV3_G1NS;
1731 icc_generate_sgi(env, cs, value, grp, ns);
1734 static uint64_t icc_igrpen_read(CPUARMState *env, const ARMCPRegInfo *ri)
1736 GICv3CPUState *cs = icc_cs_from_env(env);
1737 int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0;
1738 uint64_t value;
1740 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1741 return icv_igrpen_read(env, ri);
1744 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1745 grp = GICV3_G1NS;
1748 value = cs->icc_igrpen[grp];
1749 trace_gicv3_icc_igrpen_read(ri->opc2 & 1 ? 1 : 0,
1750 gicv3_redist_affid(cs), value);
1751 return value;
1754 static void icc_igrpen_write(CPUARMState *env, const ARMCPRegInfo *ri,
1755 uint64_t value)
1757 GICv3CPUState *cs = icc_cs_from_env(env);
1758 int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0;
1760 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1761 icv_igrpen_write(env, ri, value);
1762 return;
1765 trace_gicv3_icc_igrpen_write(ri->opc2 & 1 ? 1 : 0,
1766 gicv3_redist_affid(cs), value);
1768 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1769 grp = GICV3_G1NS;
1772 cs->icc_igrpen[grp] = value & ICC_IGRPEN_ENABLE;
1773 gicv3_cpuif_update(cs);
1776 static uint64_t icc_igrpen1_el3_read(CPUARMState *env, const ARMCPRegInfo *ri)
1778 GICv3CPUState *cs = icc_cs_from_env(env);
1779 uint64_t value;
1781 /* IGRPEN1_EL3 bits 0 and 1 are r/w aliases into IGRPEN1_EL1 NS and S */
1782 value = cs->icc_igrpen[GICV3_G1NS] | (cs->icc_igrpen[GICV3_G1] << 1);
1783 trace_gicv3_icc_igrpen1_el3_read(gicv3_redist_affid(cs), value);
1784 return value;
1787 static void icc_igrpen1_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
1788 uint64_t value)
1790 GICv3CPUState *cs = icc_cs_from_env(env);
1792 trace_gicv3_icc_igrpen1_el3_write(gicv3_redist_affid(cs), value);
1794 /* IGRPEN1_EL3 bits 0 and 1 are r/w aliases into IGRPEN1_EL1 NS and S */
1795 cs->icc_igrpen[GICV3_G1NS] = extract32(value, 0, 1);
1796 cs->icc_igrpen[GICV3_G1] = extract32(value, 1, 1);
1797 gicv3_cpuif_update(cs);
1800 static uint64_t icc_ctlr_el1_read(CPUARMState *env, const ARMCPRegInfo *ri)
1802 GICv3CPUState *cs = icc_cs_from_env(env);
1803 int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S;
1804 uint64_t value;
1806 if (icv_access(env, HCR_FMO | HCR_IMO)) {
1807 return icv_ctlr_read(env, ri);
1810 value = cs->icc_ctlr_el1[bank];
1811 trace_gicv3_icc_ctlr_read(gicv3_redist_affid(cs), value);
1812 return value;
1815 static void icc_ctlr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
1816 uint64_t value)
1818 GICv3CPUState *cs = icc_cs_from_env(env);
1819 int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S;
1820 uint64_t mask;
1822 if (icv_access(env, HCR_FMO | HCR_IMO)) {
1823 icv_ctlr_write(env, ri, value);
1824 return;
1827 trace_gicv3_icc_ctlr_write(gicv3_redist_affid(cs), value);
1829 /* Only CBPR and EOIMODE can be RW;
1830 * for us PMHE is RAZ/WI (we don't implement 1-of-N interrupts or
1831 * the asseciated priority-based routing of them);
1832 * if EL3 is implemented and GICD_CTLR.DS == 0, then PMHE and CBPR are RO.
1834 if (arm_feature(env, ARM_FEATURE_EL3) &&
1835 ((cs->gic->gicd_ctlr & GICD_CTLR_DS) == 0)) {
1836 mask = ICC_CTLR_EL1_EOIMODE;
1837 } else {
1838 mask = ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE;
1841 cs->icc_ctlr_el1[bank] &= ~mask;
1842 cs->icc_ctlr_el1[bank] |= (value & mask);
1843 gicv3_cpuif_update(cs);
1847 static uint64_t icc_ctlr_el3_read(CPUARMState *env, const ARMCPRegInfo *ri)
1849 GICv3CPUState *cs = icc_cs_from_env(env);
1850 uint64_t value;
1852 value = cs->icc_ctlr_el3;
1853 if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) {
1854 value |= ICC_CTLR_EL3_EOIMODE_EL1NS;
1856 if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) {
1857 value |= ICC_CTLR_EL3_CBPR_EL1NS;
1859 if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) {
1860 value |= ICC_CTLR_EL3_EOIMODE_EL1S;
1862 if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) {
1863 value |= ICC_CTLR_EL3_CBPR_EL1S;
1866 trace_gicv3_icc_ctlr_el3_read(gicv3_redist_affid(cs), value);
1867 return value;
1870 static void icc_ctlr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
1871 uint64_t value)
1873 GICv3CPUState *cs = icc_cs_from_env(env);
1874 uint64_t mask;
1876 trace_gicv3_icc_ctlr_el3_write(gicv3_redist_affid(cs), value);
1878 /* *_EL1NS and *_EL1S bits are aliases into the ICC_CTLR_EL1 bits. */
1879 cs->icc_ctlr_el1[GICV3_NS] &= ~(ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE);
1880 if (value & ICC_CTLR_EL3_EOIMODE_EL1NS) {
1881 cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_EOIMODE;
1883 if (value & ICC_CTLR_EL3_CBPR_EL1NS) {
1884 cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_CBPR;
1887 cs->icc_ctlr_el1[GICV3_S] &= ~(ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE);
1888 if (value & ICC_CTLR_EL3_EOIMODE_EL1S) {
1889 cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_EOIMODE;
1891 if (value & ICC_CTLR_EL3_CBPR_EL1S) {
1892 cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_CBPR;
1895 /* The only bit stored in icc_ctlr_el3 which is writeable is EOIMODE_EL3: */
1896 mask = ICC_CTLR_EL3_EOIMODE_EL3;
1898 cs->icc_ctlr_el3 &= ~mask;
1899 cs->icc_ctlr_el3 |= (value & mask);
1900 gicv3_cpuif_update(cs);
1903 static CPAccessResult gicv3_irqfiq_access(CPUARMState *env,
1904 const ARMCPRegInfo *ri, bool isread)
1906 CPAccessResult r = CP_ACCESS_OK;
1907 GICv3CPUState *cs = icc_cs_from_env(env);
1908 int el = arm_current_el(env);
1910 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TC) &&
1911 el == 1 && !arm_is_secure_below_el3(env)) {
1912 /* Takes priority over a possible EL3 trap */
1913 return CP_ACCESS_TRAP_EL2;
1916 if ((env->cp15.scr_el3 & (SCR_FIQ | SCR_IRQ)) == (SCR_FIQ | SCR_IRQ)) {
1917 switch (el) {
1918 case 1:
1919 /* Note that arm_hcr_el2_eff takes secure state into account. */
1920 if ((arm_hcr_el2_eff(env) & (HCR_IMO | HCR_FMO)) == 0) {
1921 r = CP_ACCESS_TRAP_EL3;
1923 break;
1924 case 2:
1925 r = CP_ACCESS_TRAP_EL3;
1926 break;
1927 case 3:
1928 if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
1929 r = CP_ACCESS_TRAP_EL3;
1931 break;
1932 default:
1933 g_assert_not_reached();
1937 if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
1938 r = CP_ACCESS_TRAP;
1940 return r;
1943 static CPAccessResult gicv3_dir_access(CPUARMState *env,
1944 const ARMCPRegInfo *ri, bool isread)
1946 GICv3CPUState *cs = icc_cs_from_env(env);
1948 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TDIR) &&
1949 arm_current_el(env) == 1 && !arm_is_secure_below_el3(env)) {
1950 /* Takes priority over a possible EL3 trap */
1951 return CP_ACCESS_TRAP_EL2;
1954 return gicv3_irqfiq_access(env, ri, isread);
1957 static CPAccessResult gicv3_sgi_access(CPUARMState *env,
1958 const ARMCPRegInfo *ri, bool isread)
1960 if (arm_current_el(env) == 1 &&
1961 (arm_hcr_el2_eff(env) & (HCR_IMO | HCR_FMO)) != 0) {
1962 /* Takes priority over a possible EL3 trap */
1963 return CP_ACCESS_TRAP_EL2;
1966 return gicv3_irqfiq_access(env, ri, isread);
1969 static CPAccessResult gicv3_fiq_access(CPUARMState *env,
1970 const ARMCPRegInfo *ri, bool isread)
1972 CPAccessResult r = CP_ACCESS_OK;
1973 GICv3CPUState *cs = icc_cs_from_env(env);
1974 int el = arm_current_el(env);
1976 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TALL0) &&
1977 el == 1 && !arm_is_secure_below_el3(env)) {
1978 /* Takes priority over a possible EL3 trap */
1979 return CP_ACCESS_TRAP_EL2;
1982 if (env->cp15.scr_el3 & SCR_FIQ) {
1983 switch (el) {
1984 case 1:
1985 if ((arm_hcr_el2_eff(env) & HCR_FMO) == 0) {
1986 r = CP_ACCESS_TRAP_EL3;
1988 break;
1989 case 2:
1990 r = CP_ACCESS_TRAP_EL3;
1991 break;
1992 case 3:
1993 if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
1994 r = CP_ACCESS_TRAP_EL3;
1996 break;
1997 default:
1998 g_assert_not_reached();
2002 if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
2003 r = CP_ACCESS_TRAP;
2005 return r;
2008 static CPAccessResult gicv3_irq_access(CPUARMState *env,
2009 const ARMCPRegInfo *ri, bool isread)
2011 CPAccessResult r = CP_ACCESS_OK;
2012 GICv3CPUState *cs = icc_cs_from_env(env);
2013 int el = arm_current_el(env);
2015 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TALL1) &&
2016 el == 1 && !arm_is_secure_below_el3(env)) {
2017 /* Takes priority over a possible EL3 trap */
2018 return CP_ACCESS_TRAP_EL2;
2021 if (env->cp15.scr_el3 & SCR_IRQ) {
2022 switch (el) {
2023 case 1:
2024 if ((arm_hcr_el2_eff(env) & HCR_IMO) == 0) {
2025 r = CP_ACCESS_TRAP_EL3;
2027 break;
2028 case 2:
2029 r = CP_ACCESS_TRAP_EL3;
2030 break;
2031 case 3:
2032 if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
2033 r = CP_ACCESS_TRAP_EL3;
2035 break;
2036 default:
2037 g_assert_not_reached();
2041 if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
2042 r = CP_ACCESS_TRAP;
2044 return r;
2047 static void icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2049 GICv3CPUState *cs = icc_cs_from_env(env);
2051 cs->icc_ctlr_el1[GICV3_S] = ICC_CTLR_EL1_A3V |
2052 (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
2053 (7 << ICC_CTLR_EL1_PRIBITS_SHIFT);
2054 cs->icc_ctlr_el1[GICV3_NS] = ICC_CTLR_EL1_A3V |
2055 (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
2056 (7 << ICC_CTLR_EL1_PRIBITS_SHIFT);
2057 cs->icc_pmr_el1 = 0;
2058 cs->icc_bpr[GICV3_G0] = GIC_MIN_BPR;
2059 cs->icc_bpr[GICV3_G1] = GIC_MIN_BPR;
2060 cs->icc_bpr[GICV3_G1NS] = GIC_MIN_BPR_NS;
2061 memset(cs->icc_apr, 0, sizeof(cs->icc_apr));
2062 memset(cs->icc_igrpen, 0, sizeof(cs->icc_igrpen));
2063 cs->icc_ctlr_el3 = ICC_CTLR_EL3_NDS | ICC_CTLR_EL3_A3V |
2064 (1 << ICC_CTLR_EL3_IDBITS_SHIFT) |
2065 (7 << ICC_CTLR_EL3_PRIBITS_SHIFT);
2067 memset(cs->ich_apr, 0, sizeof(cs->ich_apr));
2068 cs->ich_hcr_el2 = 0;
2069 memset(cs->ich_lr_el2, 0, sizeof(cs->ich_lr_el2));
2070 cs->ich_vmcr_el2 = ICH_VMCR_EL2_VFIQEN |
2071 ((icv_min_vbpr(cs) + 1) << ICH_VMCR_EL2_VBPR1_SHIFT) |
2072 (icv_min_vbpr(cs) << ICH_VMCR_EL2_VBPR0_SHIFT);
2075 static const ARMCPRegInfo gicv3_cpuif_reginfo[] = {
2076 { .name = "ICC_PMR_EL1", .state = ARM_CP_STATE_BOTH,
2077 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 6, .opc2 = 0,
2078 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2079 .access = PL1_RW, .accessfn = gicv3_irqfiq_access,
2080 .readfn = icc_pmr_read,
2081 .writefn = icc_pmr_write,
2082 /* We hang the whole cpu interface reset routine off here
2083 * rather than parcelling it out into one little function
2084 * per register
2086 .resetfn = icc_reset,
2088 { .name = "ICC_IAR0_EL1", .state = ARM_CP_STATE_BOTH,
2089 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 0,
2090 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2091 .access = PL1_R, .accessfn = gicv3_fiq_access,
2092 .readfn = icc_iar0_read,
2094 { .name = "ICC_EOIR0_EL1", .state = ARM_CP_STATE_BOTH,
2095 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 1,
2096 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2097 .access = PL1_W, .accessfn = gicv3_fiq_access,
2098 .writefn = icc_eoir_write,
2100 { .name = "ICC_HPPIR0_EL1", .state = ARM_CP_STATE_BOTH,
2101 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 2,
2102 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2103 .access = PL1_R, .accessfn = gicv3_fiq_access,
2104 .readfn = icc_hppir0_read,
2106 { .name = "ICC_BPR0_EL1", .state = ARM_CP_STATE_BOTH,
2107 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 3,
2108 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2109 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2110 .readfn = icc_bpr_read,
2111 .writefn = icc_bpr_write,
2113 { .name = "ICC_AP0R0_EL1", .state = ARM_CP_STATE_BOTH,
2114 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 4,
2115 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2116 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2117 .readfn = icc_ap_read,
2118 .writefn = icc_ap_write,
2120 { .name = "ICC_AP0R1_EL1", .state = ARM_CP_STATE_BOTH,
2121 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 5,
2122 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2123 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2124 .readfn = icc_ap_read,
2125 .writefn = icc_ap_write,
2127 { .name = "ICC_AP0R2_EL1", .state = ARM_CP_STATE_BOTH,
2128 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 6,
2129 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2130 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2131 .readfn = icc_ap_read,
2132 .writefn = icc_ap_write,
2134 { .name = "ICC_AP0R3_EL1", .state = ARM_CP_STATE_BOTH,
2135 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 7,
2136 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2137 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2138 .readfn = icc_ap_read,
2139 .writefn = icc_ap_write,
2141 /* All the ICC_AP1R*_EL1 registers are banked */
2142 { .name = "ICC_AP1R0_EL1", .state = ARM_CP_STATE_BOTH,
2143 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 0,
2144 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2145 .access = PL1_RW, .accessfn = gicv3_irq_access,
2146 .readfn = icc_ap_read,
2147 .writefn = icc_ap_write,
2149 { .name = "ICC_AP1R1_EL1", .state = ARM_CP_STATE_BOTH,
2150 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 1,
2151 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2152 .access = PL1_RW, .accessfn = gicv3_irq_access,
2153 .readfn = icc_ap_read,
2154 .writefn = icc_ap_write,
2156 { .name = "ICC_AP1R2_EL1", .state = ARM_CP_STATE_BOTH,
2157 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 2,
2158 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2159 .access = PL1_RW, .accessfn = gicv3_irq_access,
2160 .readfn = icc_ap_read,
2161 .writefn = icc_ap_write,
2163 { .name = "ICC_AP1R3_EL1", .state = ARM_CP_STATE_BOTH,
2164 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 3,
2165 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2166 .access = PL1_RW, .accessfn = gicv3_irq_access,
2167 .readfn = icc_ap_read,
2168 .writefn = icc_ap_write,
2170 { .name = "ICC_DIR_EL1", .state = ARM_CP_STATE_BOTH,
2171 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 1,
2172 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2173 .access = PL1_W, .accessfn = gicv3_dir_access,
2174 .writefn = icc_dir_write,
2176 { .name = "ICC_RPR_EL1", .state = ARM_CP_STATE_BOTH,
2177 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 3,
2178 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2179 .access = PL1_R, .accessfn = gicv3_irqfiq_access,
2180 .readfn = icc_rpr_read,
2182 { .name = "ICC_SGI1R_EL1", .state = ARM_CP_STATE_AA64,
2183 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 5,
2184 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2185 .access = PL1_W, .accessfn = gicv3_sgi_access,
2186 .writefn = icc_sgi1r_write,
2188 { .name = "ICC_SGI1R",
2189 .cp = 15, .opc1 = 0, .crm = 12,
2190 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2191 .access = PL1_W, .accessfn = gicv3_sgi_access,
2192 .writefn = icc_sgi1r_write,
2194 { .name = "ICC_ASGI1R_EL1", .state = ARM_CP_STATE_AA64,
2195 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 6,
2196 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2197 .access = PL1_W, .accessfn = gicv3_sgi_access,
2198 .writefn = icc_asgi1r_write,
2200 { .name = "ICC_ASGI1R",
2201 .cp = 15, .opc1 = 1, .crm = 12,
2202 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2203 .access = PL1_W, .accessfn = gicv3_sgi_access,
2204 .writefn = icc_asgi1r_write,
2206 { .name = "ICC_SGI0R_EL1", .state = ARM_CP_STATE_AA64,
2207 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 7,
2208 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2209 .access = PL1_W, .accessfn = gicv3_sgi_access,
2210 .writefn = icc_sgi0r_write,
2212 { .name = "ICC_SGI0R",
2213 .cp = 15, .opc1 = 2, .crm = 12,
2214 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2215 .access = PL1_W, .accessfn = gicv3_sgi_access,
2216 .writefn = icc_sgi0r_write,
2218 { .name = "ICC_IAR1_EL1", .state = ARM_CP_STATE_BOTH,
2219 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 0,
2220 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2221 .access = PL1_R, .accessfn = gicv3_irq_access,
2222 .readfn = icc_iar1_read,
2224 { .name = "ICC_EOIR1_EL1", .state = ARM_CP_STATE_BOTH,
2225 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 1,
2226 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2227 .access = PL1_W, .accessfn = gicv3_irq_access,
2228 .writefn = icc_eoir_write,
2230 { .name = "ICC_HPPIR1_EL1", .state = ARM_CP_STATE_BOTH,
2231 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 2,
2232 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2233 .access = PL1_R, .accessfn = gicv3_irq_access,
2234 .readfn = icc_hppir1_read,
2236 /* This register is banked */
2237 { .name = "ICC_BPR1_EL1", .state = ARM_CP_STATE_BOTH,
2238 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 3,
2239 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2240 .access = PL1_RW, .accessfn = gicv3_irq_access,
2241 .readfn = icc_bpr_read,
2242 .writefn = icc_bpr_write,
2244 /* This register is banked */
2245 { .name = "ICC_CTLR_EL1", .state = ARM_CP_STATE_BOTH,
2246 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 4,
2247 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2248 .access = PL1_RW, .accessfn = gicv3_irqfiq_access,
2249 .readfn = icc_ctlr_el1_read,
2250 .writefn = icc_ctlr_el1_write,
2252 { .name = "ICC_SRE_EL1", .state = ARM_CP_STATE_BOTH,
2253 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 5,
2254 .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2255 .access = PL1_RW,
2256 /* We don't support IRQ/FIQ bypass and system registers are
2257 * always enabled, so all our bits are RAZ/WI or RAO/WI.
2258 * This register is banked but since it's constant we don't
2259 * need to do anything special.
2261 .resetvalue = 0x7,
2263 { .name = "ICC_IGRPEN0_EL1", .state = ARM_CP_STATE_BOTH,
2264 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 6,
2265 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2266 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2267 .readfn = icc_igrpen_read,
2268 .writefn = icc_igrpen_write,
2270 /* This register is banked */
2271 { .name = "ICC_IGRPEN1_EL1", .state = ARM_CP_STATE_BOTH,
2272 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 7,
2273 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2274 .access = PL1_RW, .accessfn = gicv3_irq_access,
2275 .readfn = icc_igrpen_read,
2276 .writefn = icc_igrpen_write,
2278 { .name = "ICC_SRE_EL2", .state = ARM_CP_STATE_BOTH,
2279 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 5,
2280 .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2281 .access = PL2_RW,
2282 /* We don't support IRQ/FIQ bypass and system registers are
2283 * always enabled, so all our bits are RAZ/WI or RAO/WI.
2285 .resetvalue = 0xf,
2287 { .name = "ICC_CTLR_EL3", .state = ARM_CP_STATE_BOTH,
2288 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 4,
2289 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2290 .access = PL3_RW,
2291 .readfn = icc_ctlr_el3_read,
2292 .writefn = icc_ctlr_el3_write,
2294 { .name = "ICC_SRE_EL3", .state = ARM_CP_STATE_BOTH,
2295 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 5,
2296 .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2297 .access = PL3_RW,
2298 /* We don't support IRQ/FIQ bypass and system registers are
2299 * always enabled, so all our bits are RAZ/WI or RAO/WI.
2301 .resetvalue = 0xf,
2303 { .name = "ICC_IGRPEN1_EL3", .state = ARM_CP_STATE_BOTH,
2304 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 7,
2305 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2306 .access = PL3_RW,
2307 .readfn = icc_igrpen1_el3_read,
2308 .writefn = icc_igrpen1_el3_write,
2310 REGINFO_SENTINEL
2313 static uint64_t ich_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2315 GICv3CPUState *cs = icc_cs_from_env(env);
2316 int regno = ri->opc2 & 3;
2317 int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
2318 uint64_t value;
2320 value = cs->ich_apr[grp][regno];
2321 trace_gicv3_ich_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
2322 return value;
2325 static void ich_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2326 uint64_t value)
2328 GICv3CPUState *cs = icc_cs_from_env(env);
2329 int regno = ri->opc2 & 3;
2330 int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
2332 trace_gicv3_ich_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
2334 cs->ich_apr[grp][regno] = value & 0xFFFFFFFFU;
2335 gicv3_cpuif_virt_update(cs);
2338 static uint64_t ich_hcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2340 GICv3CPUState *cs = icc_cs_from_env(env);
2341 uint64_t value = cs->ich_hcr_el2;
2343 trace_gicv3_ich_hcr_read(gicv3_redist_affid(cs), value);
2344 return value;
2347 static void ich_hcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2348 uint64_t value)
2350 GICv3CPUState *cs = icc_cs_from_env(env);
2352 trace_gicv3_ich_hcr_write(gicv3_redist_affid(cs), value);
2354 value &= ICH_HCR_EL2_EN | ICH_HCR_EL2_UIE | ICH_HCR_EL2_LRENPIE |
2355 ICH_HCR_EL2_NPIE | ICH_HCR_EL2_VGRP0EIE | ICH_HCR_EL2_VGRP0DIE |
2356 ICH_HCR_EL2_VGRP1EIE | ICH_HCR_EL2_VGRP1DIE | ICH_HCR_EL2_TC |
2357 ICH_HCR_EL2_TALL0 | ICH_HCR_EL2_TALL1 | ICH_HCR_EL2_TSEI |
2358 ICH_HCR_EL2_TDIR | ICH_HCR_EL2_EOICOUNT_MASK;
2360 cs->ich_hcr_el2 = value;
2361 gicv3_cpuif_virt_update(cs);
2364 static uint64_t ich_vmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2366 GICv3CPUState *cs = icc_cs_from_env(env);
2367 uint64_t value = cs->ich_vmcr_el2;
2369 trace_gicv3_ich_vmcr_read(gicv3_redist_affid(cs), value);
2370 return value;
2373 static void ich_vmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2374 uint64_t value)
2376 GICv3CPUState *cs = icc_cs_from_env(env);
2378 trace_gicv3_ich_vmcr_write(gicv3_redist_affid(cs), value);
2380 value &= ICH_VMCR_EL2_VENG0 | ICH_VMCR_EL2_VENG1 | ICH_VMCR_EL2_VCBPR |
2381 ICH_VMCR_EL2_VEOIM | ICH_VMCR_EL2_VBPR1_MASK |
2382 ICH_VMCR_EL2_VBPR0_MASK | ICH_VMCR_EL2_VPMR_MASK;
2383 value |= ICH_VMCR_EL2_VFIQEN;
2385 cs->ich_vmcr_el2 = value;
2386 /* Enforce "writing BPRs to less than minimum sets them to the minimum"
2387 * by reading and writing back the fields.
2389 write_vbpr(cs, GICV3_G0, read_vbpr(cs, GICV3_G0));
2390 write_vbpr(cs, GICV3_G1, read_vbpr(cs, GICV3_G1));
2392 gicv3_cpuif_virt_update(cs);
2395 static uint64_t ich_lr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2397 GICv3CPUState *cs = icc_cs_from_env(env);
2398 int regno = ri->opc2 | ((ri->crm & 1) << 3);
2399 uint64_t value;
2401 /* This read function handles all of:
2402 * 64-bit reads of the whole LR
2403 * 32-bit reads of the low half of the LR
2404 * 32-bit reads of the high half of the LR
2406 if (ri->state == ARM_CP_STATE_AA32) {
2407 if (ri->crm >= 14) {
2408 value = extract64(cs->ich_lr_el2[regno], 32, 32);
2409 trace_gicv3_ich_lrc_read(regno, gicv3_redist_affid(cs), value);
2410 } else {
2411 value = extract64(cs->ich_lr_el2[regno], 0, 32);
2412 trace_gicv3_ich_lr32_read(regno, gicv3_redist_affid(cs), value);
2414 } else {
2415 value = cs->ich_lr_el2[regno];
2416 trace_gicv3_ich_lr_read(regno, gicv3_redist_affid(cs), value);
2419 return value;
2422 static void ich_lr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2423 uint64_t value)
2425 GICv3CPUState *cs = icc_cs_from_env(env);
2426 int regno = ri->opc2 | ((ri->crm & 1) << 3);
2428 /* This write function handles all of:
2429 * 64-bit writes to the whole LR
2430 * 32-bit writes to the low half of the LR
2431 * 32-bit writes to the high half of the LR
2433 if (ri->state == ARM_CP_STATE_AA32) {
2434 if (ri->crm >= 14) {
2435 trace_gicv3_ich_lrc_write(regno, gicv3_redist_affid(cs), value);
2436 value = deposit64(cs->ich_lr_el2[regno], 32, 32, value);
2437 } else {
2438 trace_gicv3_ich_lr32_write(regno, gicv3_redist_affid(cs), value);
2439 value = deposit64(cs->ich_lr_el2[regno], 0, 32, value);
2441 } else {
2442 trace_gicv3_ich_lr_write(regno, gicv3_redist_affid(cs), value);
2445 /* Enforce RES0 bits in priority field */
2446 if (cs->vpribits < 8) {
2447 value = deposit64(value, ICH_LR_EL2_PRIORITY_SHIFT,
2448 8 - cs->vpribits, 0);
2451 cs->ich_lr_el2[regno] = value;
2452 gicv3_cpuif_virt_update(cs);
2455 static uint64_t ich_vtr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2457 GICv3CPUState *cs = icc_cs_from_env(env);
2458 uint64_t value;
2460 value = ((cs->num_list_regs - 1) << ICH_VTR_EL2_LISTREGS_SHIFT)
2461 | ICH_VTR_EL2_TDS | ICH_VTR_EL2_NV4 | ICH_VTR_EL2_A3V
2462 | (1 << ICH_VTR_EL2_IDBITS_SHIFT)
2463 | ((cs->vprebits - 1) << ICH_VTR_EL2_PREBITS_SHIFT)
2464 | ((cs->vpribits - 1) << ICH_VTR_EL2_PRIBITS_SHIFT);
2466 trace_gicv3_ich_vtr_read(gicv3_redist_affid(cs), value);
2467 return value;
2470 static uint64_t ich_misr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2472 GICv3CPUState *cs = icc_cs_from_env(env);
2473 uint64_t value = maintenance_interrupt_state(cs);
2475 trace_gicv3_ich_misr_read(gicv3_redist_affid(cs), value);
2476 return value;
2479 static uint64_t ich_eisr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2481 GICv3CPUState *cs = icc_cs_from_env(env);
2482 uint64_t value = eoi_maintenance_interrupt_state(cs, NULL);
2484 trace_gicv3_ich_eisr_read(gicv3_redist_affid(cs), value);
2485 return value;
2488 static uint64_t ich_elrsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2490 GICv3CPUState *cs = icc_cs_from_env(env);
2491 uint64_t value = 0;
2492 int i;
2494 for (i = 0; i < cs->num_list_regs; i++) {
2495 uint64_t lr = cs->ich_lr_el2[i];
2497 if ((lr & ICH_LR_EL2_STATE_MASK) == 0 &&
2498 ((lr & ICH_LR_EL2_HW) != 0 || (lr & ICH_LR_EL2_EOI) == 0)) {
2499 value |= (1 << i);
2503 trace_gicv3_ich_elrsr_read(gicv3_redist_affid(cs), value);
2504 return value;
2507 static const ARMCPRegInfo gicv3_cpuif_hcr_reginfo[] = {
2508 { .name = "ICH_AP0R0_EL2", .state = ARM_CP_STATE_BOTH,
2509 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 0,
2510 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2511 .access = PL2_RW,
2512 .readfn = ich_ap_read,
2513 .writefn = ich_ap_write,
2515 { .name = "ICH_AP1R0_EL2", .state = ARM_CP_STATE_BOTH,
2516 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 0,
2517 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2518 .access = PL2_RW,
2519 .readfn = ich_ap_read,
2520 .writefn = ich_ap_write,
2522 { .name = "ICH_HCR_EL2", .state = ARM_CP_STATE_BOTH,
2523 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 0,
2524 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2525 .access = PL2_RW,
2526 .readfn = ich_hcr_read,
2527 .writefn = ich_hcr_write,
2529 { .name = "ICH_VTR_EL2", .state = ARM_CP_STATE_BOTH,
2530 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 1,
2531 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2532 .access = PL2_R,
2533 .readfn = ich_vtr_read,
2535 { .name = "ICH_MISR_EL2", .state = ARM_CP_STATE_BOTH,
2536 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 2,
2537 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2538 .access = PL2_R,
2539 .readfn = ich_misr_read,
2541 { .name = "ICH_EISR_EL2", .state = ARM_CP_STATE_BOTH,
2542 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 3,
2543 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2544 .access = PL2_R,
2545 .readfn = ich_eisr_read,
2547 { .name = "ICH_ELRSR_EL2", .state = ARM_CP_STATE_BOTH,
2548 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 5,
2549 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2550 .access = PL2_R,
2551 .readfn = ich_elrsr_read,
2553 { .name = "ICH_VMCR_EL2", .state = ARM_CP_STATE_BOTH,
2554 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 7,
2555 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2556 .access = PL2_RW,
2557 .readfn = ich_vmcr_read,
2558 .writefn = ich_vmcr_write,
2560 REGINFO_SENTINEL
2563 static const ARMCPRegInfo gicv3_cpuif_ich_apxr1_reginfo[] = {
2564 { .name = "ICH_AP0R1_EL2", .state = ARM_CP_STATE_BOTH,
2565 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 1,
2566 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2567 .access = PL2_RW,
2568 .readfn = ich_ap_read,
2569 .writefn = ich_ap_write,
2571 { .name = "ICH_AP1R1_EL2", .state = ARM_CP_STATE_BOTH,
2572 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 1,
2573 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2574 .access = PL2_RW,
2575 .readfn = ich_ap_read,
2576 .writefn = ich_ap_write,
2578 REGINFO_SENTINEL
2581 static const ARMCPRegInfo gicv3_cpuif_ich_apxr23_reginfo[] = {
2582 { .name = "ICH_AP0R2_EL2", .state = ARM_CP_STATE_BOTH,
2583 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 2,
2584 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2585 .access = PL2_RW,
2586 .readfn = ich_ap_read,
2587 .writefn = ich_ap_write,
2589 { .name = "ICH_AP0R3_EL2", .state = ARM_CP_STATE_BOTH,
2590 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 3,
2591 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2592 .access = PL2_RW,
2593 .readfn = ich_ap_read,
2594 .writefn = ich_ap_write,
2596 { .name = "ICH_AP1R2_EL2", .state = ARM_CP_STATE_BOTH,
2597 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 2,
2598 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2599 .access = PL2_RW,
2600 .readfn = ich_ap_read,
2601 .writefn = ich_ap_write,
2603 { .name = "ICH_AP1R3_EL2", .state = ARM_CP_STATE_BOTH,
2604 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 3,
2605 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2606 .access = PL2_RW,
2607 .readfn = ich_ap_read,
2608 .writefn = ich_ap_write,
2610 REGINFO_SENTINEL
2613 static void gicv3_cpuif_el_change_hook(ARMCPU *cpu, void *opaque)
2615 GICv3CPUState *cs = opaque;
2617 gicv3_cpuif_update(cs);
2620 void gicv3_init_cpuif(GICv3State *s)
2622 /* Called from the GICv3 realize function; register our system
2623 * registers with the CPU
2625 int i;
2627 for (i = 0; i < s->num_cpu; i++) {
2628 ARMCPU *cpu = ARM_CPU(qemu_get_cpu(i));
2629 GICv3CPUState *cs = &s->cpu[i];
2631 /* Note that we can't just use the GICv3CPUState as an opaque pointer
2632 * in define_arm_cp_regs_with_opaque(), because when we're called back
2633 * it might be with code translated by CPU 0 but run by CPU 1, in
2634 * which case we'd get the wrong value.
2635 * So instead we define the regs with no ri->opaque info, and
2636 * get back to the GICv3CPUState from the CPUARMState.
2638 define_arm_cp_regs(cpu, gicv3_cpuif_reginfo);
2639 if (arm_feature(&cpu->env, ARM_FEATURE_EL2)
2640 && cpu->gic_num_lrs) {
2641 int j;
2643 cs->num_list_regs = cpu->gic_num_lrs;
2644 cs->vpribits = cpu->gic_vpribits;
2645 cs->vprebits = cpu->gic_vprebits;
2647 /* Check against architectural constraints: getting these
2648 * wrong would be a bug in the CPU code defining these,
2649 * and the implementation relies on them holding.
2651 g_assert(cs->vprebits <= cs->vpribits);
2652 g_assert(cs->vprebits >= 5 && cs->vprebits <= 7);
2653 g_assert(cs->vpribits >= 5 && cs->vpribits <= 8);
2655 define_arm_cp_regs(cpu, gicv3_cpuif_hcr_reginfo);
2657 for (j = 0; j < cs->num_list_regs; j++) {
2658 /* Note that the AArch64 LRs are 64-bit; the AArch32 LRs
2659 * are split into two cp15 regs, LR (the low part, with the
2660 * same encoding as the AArch64 LR) and LRC (the high part).
2662 ARMCPRegInfo lr_regset[] = {
2663 { .name = "ICH_LRn_EL2", .state = ARM_CP_STATE_BOTH,
2664 .opc0 = 3, .opc1 = 4, .crn = 12,
2665 .crm = 12 + (j >> 3), .opc2 = j & 7,
2666 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2667 .access = PL2_RW,
2668 .readfn = ich_lr_read,
2669 .writefn = ich_lr_write,
2671 { .name = "ICH_LRCn_EL2", .state = ARM_CP_STATE_AA32,
2672 .cp = 15, .opc1 = 4, .crn = 12,
2673 .crm = 14 + (j >> 3), .opc2 = j & 7,
2674 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2675 .access = PL2_RW,
2676 .readfn = ich_lr_read,
2677 .writefn = ich_lr_write,
2679 REGINFO_SENTINEL
2681 define_arm_cp_regs(cpu, lr_regset);
2683 if (cs->vprebits >= 6) {
2684 define_arm_cp_regs(cpu, gicv3_cpuif_ich_apxr1_reginfo);
2686 if (cs->vprebits == 7) {
2687 define_arm_cp_regs(cpu, gicv3_cpuif_ich_apxr23_reginfo);
2690 arm_register_el_change_hook(cpu, gicv3_cpuif_el_change_hook, cs);