docs/fuzz: add information about useful libFuzzer flags
[qemu/ar7.git] / hw / intc / arm_gicv3_cpuif.c
blob08e000e33c630f40ae2b47243ed3763185a415c9
1 /*
2 * ARM Generic Interrupt Controller v3
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/main-loop.h"
18 #include "trace.h"
19 #include "gicv3_internal.h"
20 #include "hw/irq.h"
21 #include "cpu.h"
23 void gicv3_set_gicv3state(CPUState *cpu, GICv3CPUState *s)
25 ARMCPU *arm_cpu = ARM_CPU(cpu);
26 CPUARMState *env = &arm_cpu->env;
28 env->gicv3state = (void *)s;
31 static GICv3CPUState *icc_cs_from_env(CPUARMState *env)
33 return env->gicv3state;
36 static bool gicv3_use_ns_bank(CPUARMState *env)
38 /* Return true if we should use the NonSecure bank for a banked GIC
39 * CPU interface register. Note that this differs from the
40 * access_secure_reg() function because GICv3 banked registers are
41 * banked even for AArch64, unlike the other CPU system registers.
43 return !arm_is_secure_below_el3(env);
46 /* The minimum BPR for the virtual interface is a configurable property */
47 static inline int icv_min_vbpr(GICv3CPUState *cs)
49 return 7 - cs->vprebits;
52 /* Simple accessor functions for LR fields */
53 static uint32_t ich_lr_vintid(uint64_t lr)
55 return extract64(lr, ICH_LR_EL2_VINTID_SHIFT, ICH_LR_EL2_VINTID_LENGTH);
58 static uint32_t ich_lr_pintid(uint64_t lr)
60 return extract64(lr, ICH_LR_EL2_PINTID_SHIFT, ICH_LR_EL2_PINTID_LENGTH);
63 static uint32_t ich_lr_prio(uint64_t lr)
65 return extract64(lr, ICH_LR_EL2_PRIORITY_SHIFT, ICH_LR_EL2_PRIORITY_LENGTH);
68 static int ich_lr_state(uint64_t lr)
70 return extract64(lr, ICH_LR_EL2_STATE_SHIFT, ICH_LR_EL2_STATE_LENGTH);
73 static bool icv_access(CPUARMState *env, int hcr_flags)
75 /* Return true if this ICC_ register access should really be
76 * directed to an ICV_ access. hcr_flags is a mask of
77 * HCR_EL2 bits to check: we treat this as an ICV_ access
78 * if we are in NS EL1 and at least one of the specified
79 * HCR_EL2 bits is set.
81 * ICV registers fall into four categories:
82 * * access if NS EL1 and HCR_EL2.FMO == 1:
83 * all ICV regs with '0' in their name
84 * * access if NS EL1 and HCR_EL2.IMO == 1:
85 * all ICV regs with '1' in their name
86 * * access if NS EL1 and either IMO or FMO == 1:
87 * CTLR, DIR, PMR, RPR
89 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
90 bool flagmatch = hcr_el2 & hcr_flags & (HCR_IMO | HCR_FMO);
92 return flagmatch && arm_current_el(env) == 1
93 && !arm_is_secure_below_el3(env);
96 static int read_vbpr(GICv3CPUState *cs, int grp)
98 /* Read VBPR value out of the VMCR field (caller must handle
99 * VCBPR effects if required)
101 if (grp == GICV3_G0) {
102 return extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR0_SHIFT,
103 ICH_VMCR_EL2_VBPR0_LENGTH);
104 } else {
105 return extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR1_SHIFT,
106 ICH_VMCR_EL2_VBPR1_LENGTH);
110 static void write_vbpr(GICv3CPUState *cs, int grp, int value)
112 /* Write new VBPR1 value, handling the "writing a value less than
113 * the minimum sets it to the minimum" semantics.
115 int min = icv_min_vbpr(cs);
117 if (grp != GICV3_G0) {
118 min++;
121 value = MAX(value, min);
123 if (grp == GICV3_G0) {
124 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR0_SHIFT,
125 ICH_VMCR_EL2_VBPR0_LENGTH, value);
126 } else {
127 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VBPR1_SHIFT,
128 ICH_VMCR_EL2_VBPR1_LENGTH, value);
132 static uint32_t icv_fullprio_mask(GICv3CPUState *cs)
134 /* Return a mask word which clears the unimplemented priority bits
135 * from a priority value for a virtual interrupt. (Not to be confused
136 * with the group priority, whose mask depends on the value of VBPR
137 * for the interrupt group.)
139 return ~0U << (8 - cs->vpribits);
142 static int ich_highest_active_virt_prio(GICv3CPUState *cs)
144 /* Calculate the current running priority based on the set bits
145 * in the ICH Active Priority Registers.
147 int i;
148 int aprmax = 1 << (cs->vprebits - 5);
150 assert(aprmax <= ARRAY_SIZE(cs->ich_apr[0]));
152 for (i = 0; i < aprmax; i++) {
153 uint32_t apr = cs->ich_apr[GICV3_G0][i] |
154 cs->ich_apr[GICV3_G1NS][i];
156 if (!apr) {
157 continue;
159 return (i * 32 + ctz32(apr)) << (icv_min_vbpr(cs) + 1);
161 /* No current active interrupts: return idle priority */
162 return 0xff;
165 static int hppvi_index(GICv3CPUState *cs)
167 /* Return the list register index of the highest priority pending
168 * virtual interrupt, as per the HighestPriorityVirtualInterrupt
169 * pseudocode. If no pending virtual interrupts, return -1.
171 int idx = -1;
172 int i;
173 /* Note that a list register entry with a priority of 0xff will
174 * never be reported by this function; this is the architecturally
175 * correct behaviour.
177 int prio = 0xff;
179 if (!(cs->ich_vmcr_el2 & (ICH_VMCR_EL2_VENG0 | ICH_VMCR_EL2_VENG1))) {
180 /* Both groups disabled, definitely nothing to do */
181 return idx;
184 for (i = 0; i < cs->num_list_regs; i++) {
185 uint64_t lr = cs->ich_lr_el2[i];
186 int thisprio;
188 if (ich_lr_state(lr) != ICH_LR_EL2_STATE_PENDING) {
189 /* Not Pending */
190 continue;
193 /* Ignore interrupts if relevant group enable not set */
194 if (lr & ICH_LR_EL2_GROUP) {
195 if (!(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
196 continue;
198 } else {
199 if (!(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG0)) {
200 continue;
204 thisprio = ich_lr_prio(lr);
206 if (thisprio < prio) {
207 prio = thisprio;
208 idx = i;
212 return idx;
215 static uint32_t icv_gprio_mask(GICv3CPUState *cs, int group)
217 /* Return a mask word which clears the subpriority bits from
218 * a priority value for a virtual interrupt in the specified group.
219 * This depends on the VBPR value.
220 * If using VBPR0 then:
221 * a BPR of 0 means the group priority bits are [7:1];
222 * a BPR of 1 means they are [7:2], and so on down to
223 * a BPR of 7 meaning no group priority bits at all.
224 * If using VBPR1 then:
225 * a BPR of 0 is impossible (the minimum value is 1)
226 * a BPR of 1 means the group priority bits are [7:1];
227 * a BPR of 2 means they are [7:2], and so on down to
228 * a BPR of 7 meaning the group priority is [7].
230 * Which BPR to use depends on the group of the interrupt and
231 * the current ICH_VMCR_EL2.VCBPR settings.
233 * This corresponds to the VGroupBits() pseudocode.
235 int bpr;
237 if (group == GICV3_G1NS && cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR) {
238 group = GICV3_G0;
241 bpr = read_vbpr(cs, group);
242 if (group == GICV3_G1NS) {
243 assert(bpr > 0);
244 bpr--;
247 return ~0U << (bpr + 1);
250 static bool icv_hppi_can_preempt(GICv3CPUState *cs, uint64_t lr)
252 /* Return true if we can signal this virtual interrupt defined by
253 * the given list register value; see the pseudocode functions
254 * CanSignalVirtualInterrupt and CanSignalVirtualInt.
255 * Compare also icc_hppi_can_preempt() which is the non-virtual
256 * equivalent of these checks.
258 int grp;
259 uint32_t mask, prio, rprio, vpmr;
261 if (!(cs->ich_hcr_el2 & ICH_HCR_EL2_EN)) {
262 /* Virtual interface disabled */
263 return false;
266 /* We don't need to check that this LR is in Pending state because
267 * that has already been done in hppvi_index().
270 prio = ich_lr_prio(lr);
271 vpmr = extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
272 ICH_VMCR_EL2_VPMR_LENGTH);
274 if (prio >= vpmr) {
275 /* Priority mask masks this interrupt */
276 return false;
279 rprio = ich_highest_active_virt_prio(cs);
280 if (rprio == 0xff) {
281 /* No running interrupt so we can preempt */
282 return true;
285 grp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
287 mask = icv_gprio_mask(cs, grp);
289 /* We only preempt a running interrupt if the pending interrupt's
290 * group priority is sufficient (the subpriorities are not considered).
292 if ((prio & mask) < (rprio & mask)) {
293 return true;
296 return false;
299 static uint32_t eoi_maintenance_interrupt_state(GICv3CPUState *cs,
300 uint32_t *misr)
302 /* Return a set of bits indicating the EOI maintenance interrupt status
303 * for each list register. The EOI maintenance interrupt status is
304 * 1 if LR.State == 0 && LR.HW == 0 && LR.EOI == 1
305 * (see the GICv3 spec for the ICH_EISR_EL2 register).
306 * If misr is not NULL then we should also collect the information
307 * about the MISR.EOI, MISR.NP and MISR.U bits.
309 uint32_t value = 0;
310 int validcount = 0;
311 bool seenpending = false;
312 int i;
314 for (i = 0; i < cs->num_list_regs; i++) {
315 uint64_t lr = cs->ich_lr_el2[i];
317 if ((lr & (ICH_LR_EL2_STATE_MASK | ICH_LR_EL2_HW | ICH_LR_EL2_EOI))
318 == ICH_LR_EL2_EOI) {
319 value |= (1 << i);
321 if ((lr & ICH_LR_EL2_STATE_MASK)) {
322 validcount++;
324 if (ich_lr_state(lr) == ICH_LR_EL2_STATE_PENDING) {
325 seenpending = true;
329 if (misr) {
330 if (validcount < 2 && (cs->ich_hcr_el2 & ICH_HCR_EL2_UIE)) {
331 *misr |= ICH_MISR_EL2_U;
333 if (!seenpending && (cs->ich_hcr_el2 & ICH_HCR_EL2_NPIE)) {
334 *misr |= ICH_MISR_EL2_NP;
336 if (value) {
337 *misr |= ICH_MISR_EL2_EOI;
340 return value;
343 static uint32_t maintenance_interrupt_state(GICv3CPUState *cs)
345 /* Return a set of bits indicating the maintenance interrupt status
346 * (as seen in the ICH_MISR_EL2 register).
348 uint32_t value = 0;
350 /* Scan list registers and fill in the U, NP and EOI bits */
351 eoi_maintenance_interrupt_state(cs, &value);
353 if (cs->ich_hcr_el2 & (ICH_HCR_EL2_LRENPIE | ICH_HCR_EL2_EOICOUNT_MASK)) {
354 value |= ICH_MISR_EL2_LRENP;
357 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP0EIE) &&
358 (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG0)) {
359 value |= ICH_MISR_EL2_VGRP0E;
362 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP0DIE) &&
363 !(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
364 value |= ICH_MISR_EL2_VGRP0D;
366 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP1EIE) &&
367 (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
368 value |= ICH_MISR_EL2_VGRP1E;
371 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_VGRP1DIE) &&
372 !(cs->ich_vmcr_el2 & ICH_VMCR_EL2_VENG1)) {
373 value |= ICH_MISR_EL2_VGRP1D;
376 return value;
379 static void gicv3_cpuif_virt_update(GICv3CPUState *cs)
381 /* Tell the CPU about any pending virtual interrupts or
382 * maintenance interrupts, following a change to the state
383 * of the CPU interface relevant to virtual interrupts.
385 * CAUTION: this function will call qemu_set_irq() on the
386 * CPU maintenance IRQ line, which is typically wired up
387 * to the GIC as a per-CPU interrupt. This means that it
388 * will recursively call back into the GIC code via
389 * gicv3_redist_set_irq() and thus into the CPU interface code's
390 * gicv3_cpuif_update(). It is therefore important that this
391 * function is only called as the final action of a CPU interface
392 * register write implementation, after all the GIC state
393 * fields have been updated. gicv3_cpuif_update() also must
394 * not cause this function to be called, but that happens
395 * naturally as a result of there being no architectural
396 * linkage between the physical and virtual GIC logic.
398 int idx;
399 int irqlevel = 0;
400 int fiqlevel = 0;
401 int maintlevel = 0;
403 idx = hppvi_index(cs);
404 trace_gicv3_cpuif_virt_update(gicv3_redist_affid(cs), idx);
405 if (idx >= 0) {
406 uint64_t lr = cs->ich_lr_el2[idx];
408 if (icv_hppi_can_preempt(cs, lr)) {
409 /* Virtual interrupts are simple: G0 are always FIQ, and G1 IRQ */
410 if (lr & ICH_LR_EL2_GROUP) {
411 irqlevel = 1;
412 } else {
413 fiqlevel = 1;
418 if (cs->ich_hcr_el2 & ICH_HCR_EL2_EN) {
419 maintlevel = maintenance_interrupt_state(cs);
422 trace_gicv3_cpuif_virt_set_irqs(gicv3_redist_affid(cs), fiqlevel,
423 irqlevel, maintlevel);
425 qemu_set_irq(cs->parent_vfiq, fiqlevel);
426 qemu_set_irq(cs->parent_virq, irqlevel);
427 qemu_set_irq(cs->maintenance_irq, maintlevel);
430 static uint64_t icv_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
432 GICv3CPUState *cs = icc_cs_from_env(env);
433 int regno = ri->opc2 & 3;
434 int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
435 uint64_t value = cs->ich_apr[grp][regno];
437 trace_gicv3_icv_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
438 return value;
441 static void icv_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
442 uint64_t value)
444 GICv3CPUState *cs = icc_cs_from_env(env);
445 int regno = ri->opc2 & 3;
446 int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
448 trace_gicv3_icv_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
450 cs->ich_apr[grp][regno] = value & 0xFFFFFFFFU;
452 gicv3_cpuif_virt_update(cs);
453 return;
456 static uint64_t icv_bpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
458 GICv3CPUState *cs = icc_cs_from_env(env);
459 int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1NS;
460 uint64_t bpr;
461 bool satinc = false;
463 if (grp == GICV3_G1NS && (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR)) {
464 /* reads return bpr0 + 1 saturated to 7, writes ignored */
465 grp = GICV3_G0;
466 satinc = true;
469 bpr = read_vbpr(cs, grp);
471 if (satinc) {
472 bpr++;
473 bpr = MIN(bpr, 7);
476 trace_gicv3_icv_bpr_read(ri->crm == 8 ? 0 : 1, gicv3_redist_affid(cs), bpr);
478 return bpr;
481 static void icv_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
482 uint64_t value)
484 GICv3CPUState *cs = icc_cs_from_env(env);
485 int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1NS;
487 trace_gicv3_icv_bpr_write(ri->crm == 8 ? 0 : 1,
488 gicv3_redist_affid(cs), value);
490 if (grp == GICV3_G1NS && (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR)) {
491 /* reads return bpr0 + 1 saturated to 7, writes ignored */
492 return;
495 write_vbpr(cs, grp, value);
497 gicv3_cpuif_virt_update(cs);
500 static uint64_t icv_pmr_read(CPUARMState *env, const ARMCPRegInfo *ri)
502 GICv3CPUState *cs = icc_cs_from_env(env);
503 uint64_t value;
505 value = extract64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
506 ICH_VMCR_EL2_VPMR_LENGTH);
508 trace_gicv3_icv_pmr_read(gicv3_redist_affid(cs), value);
509 return value;
512 static void icv_pmr_write(CPUARMState *env, const ARMCPRegInfo *ri,
513 uint64_t value)
515 GICv3CPUState *cs = icc_cs_from_env(env);
517 trace_gicv3_icv_pmr_write(gicv3_redist_affid(cs), value);
519 value &= icv_fullprio_mask(cs);
521 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VPMR_SHIFT,
522 ICH_VMCR_EL2_VPMR_LENGTH, value);
524 gicv3_cpuif_virt_update(cs);
527 static uint64_t icv_igrpen_read(CPUARMState *env, const ARMCPRegInfo *ri)
529 GICv3CPUState *cs = icc_cs_from_env(env);
530 int enbit;
531 uint64_t value;
533 enbit = ri->opc2 & 1 ? ICH_VMCR_EL2_VENG1_SHIFT : ICH_VMCR_EL2_VENG0_SHIFT;
534 value = extract64(cs->ich_vmcr_el2, enbit, 1);
536 trace_gicv3_icv_igrpen_read(ri->opc2 & 1 ? 1 : 0,
537 gicv3_redist_affid(cs), value);
538 return value;
541 static void icv_igrpen_write(CPUARMState *env, const ARMCPRegInfo *ri,
542 uint64_t value)
544 GICv3CPUState *cs = icc_cs_from_env(env);
545 int enbit;
547 trace_gicv3_icv_igrpen_write(ri->opc2 & 1 ? 1 : 0,
548 gicv3_redist_affid(cs), value);
550 enbit = ri->opc2 & 1 ? ICH_VMCR_EL2_VENG1_SHIFT : ICH_VMCR_EL2_VENG0_SHIFT;
552 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, enbit, 1, value);
553 gicv3_cpuif_virt_update(cs);
556 static uint64_t icv_ctlr_read(CPUARMState *env, const ARMCPRegInfo *ri)
558 GICv3CPUState *cs = icc_cs_from_env(env);
559 uint64_t value;
561 /* Note that the fixed fields here (A3V, SEIS, IDbits, PRIbits)
562 * should match the ones reported in ich_vtr_read().
564 value = ICC_CTLR_EL1_A3V | (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
565 (7 << ICC_CTLR_EL1_PRIBITS_SHIFT);
567 if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VEOIM) {
568 value |= ICC_CTLR_EL1_EOIMODE;
571 if (cs->ich_vmcr_el2 & ICH_VMCR_EL2_VCBPR) {
572 value |= ICC_CTLR_EL1_CBPR;
575 trace_gicv3_icv_ctlr_read(gicv3_redist_affid(cs), value);
576 return value;
579 static void icv_ctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
580 uint64_t value)
582 GICv3CPUState *cs = icc_cs_from_env(env);
584 trace_gicv3_icv_ctlr_write(gicv3_redist_affid(cs), value);
586 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VCBPR_SHIFT,
587 1, value & ICC_CTLR_EL1_CBPR ? 1 : 0);
588 cs->ich_vmcr_el2 = deposit64(cs->ich_vmcr_el2, ICH_VMCR_EL2_VEOIM_SHIFT,
589 1, value & ICC_CTLR_EL1_EOIMODE ? 1 : 0);
591 gicv3_cpuif_virt_update(cs);
594 static uint64_t icv_rpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
596 GICv3CPUState *cs = icc_cs_from_env(env);
597 int prio = ich_highest_active_virt_prio(cs);
599 trace_gicv3_icv_rpr_read(gicv3_redist_affid(cs), prio);
600 return prio;
603 static uint64_t icv_hppir_read(CPUARMState *env, const ARMCPRegInfo *ri)
605 GICv3CPUState *cs = icc_cs_from_env(env);
606 int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
607 int idx = hppvi_index(cs);
608 uint64_t value = INTID_SPURIOUS;
610 if (idx >= 0) {
611 uint64_t lr = cs->ich_lr_el2[idx];
612 int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
614 if (grp == thisgrp) {
615 value = ich_lr_vintid(lr);
619 trace_gicv3_icv_hppir_read(grp, gicv3_redist_affid(cs), value);
620 return value;
623 static void icv_activate_irq(GICv3CPUState *cs, int idx, int grp)
625 /* Activate the interrupt in the specified list register
626 * by moving it from Pending to Active state, and update the
627 * Active Priority Registers.
629 uint32_t mask = icv_gprio_mask(cs, grp);
630 int prio = ich_lr_prio(cs->ich_lr_el2[idx]) & mask;
631 int aprbit = prio >> (8 - cs->vprebits);
632 int regno = aprbit / 32;
633 int regbit = aprbit % 32;
635 cs->ich_lr_el2[idx] &= ~ICH_LR_EL2_STATE_PENDING_BIT;
636 cs->ich_lr_el2[idx] |= ICH_LR_EL2_STATE_ACTIVE_BIT;
637 cs->ich_apr[grp][regno] |= (1 << regbit);
640 static uint64_t icv_iar_read(CPUARMState *env, const ARMCPRegInfo *ri)
642 GICv3CPUState *cs = icc_cs_from_env(env);
643 int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
644 int idx = hppvi_index(cs);
645 uint64_t intid = INTID_SPURIOUS;
647 if (idx >= 0) {
648 uint64_t lr = cs->ich_lr_el2[idx];
649 int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
651 if (thisgrp == grp && icv_hppi_can_preempt(cs, lr)) {
652 intid = ich_lr_vintid(lr);
653 if (intid < INTID_SECURE) {
654 icv_activate_irq(cs, idx, grp);
655 } else {
656 /* Interrupt goes from Pending to Invalid */
657 cs->ich_lr_el2[idx] &= ~ICH_LR_EL2_STATE_PENDING_BIT;
658 /* We will now return the (bogus) ID from the list register,
659 * as per the pseudocode.
665 trace_gicv3_icv_iar_read(ri->crm == 8 ? 0 : 1,
666 gicv3_redist_affid(cs), intid);
668 gicv3_cpuif_virt_update(cs);
670 return intid;
673 static int icc_highest_active_prio(GICv3CPUState *cs)
675 /* Calculate the current running priority based on the set bits
676 * in the Active Priority Registers.
678 int i;
680 for (i = 0; i < ARRAY_SIZE(cs->icc_apr[0]); i++) {
681 uint32_t apr = cs->icc_apr[GICV3_G0][i] |
682 cs->icc_apr[GICV3_G1][i] | cs->icc_apr[GICV3_G1NS][i];
684 if (!apr) {
685 continue;
687 return (i * 32 + ctz32(apr)) << (GIC_MIN_BPR + 1);
689 /* No current active interrupts: return idle priority */
690 return 0xff;
693 static uint32_t icc_gprio_mask(GICv3CPUState *cs, int group)
695 /* Return a mask word which clears the subpriority bits from
696 * a priority value for an interrupt in the specified group.
697 * This depends on the BPR value. For CBPR0 (S or NS):
698 * a BPR of 0 means the group priority bits are [7:1];
699 * a BPR of 1 means they are [7:2], and so on down to
700 * a BPR of 7 meaning no group priority bits at all.
701 * For CBPR1 NS:
702 * a BPR of 0 is impossible (the minimum value is 1)
703 * a BPR of 1 means the group priority bits are [7:1];
704 * a BPR of 2 means they are [7:2], and so on down to
705 * a BPR of 7 meaning the group priority is [7].
707 * Which BPR to use depends on the group of the interrupt and
708 * the current ICC_CTLR.CBPR settings.
710 * This corresponds to the GroupBits() pseudocode.
712 int bpr;
714 if ((group == GICV3_G1 && cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR) ||
715 (group == GICV3_G1NS &&
716 cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
717 group = GICV3_G0;
720 bpr = cs->icc_bpr[group] & 7;
722 if (group == GICV3_G1NS) {
723 assert(bpr > 0);
724 bpr--;
727 return ~0U << (bpr + 1);
730 static bool icc_no_enabled_hppi(GICv3CPUState *cs)
732 /* Return true if there is no pending interrupt, or the
733 * highest priority pending interrupt is in a group which has been
734 * disabled at the CPU interface by the ICC_IGRPEN* register enable bits.
736 return cs->hppi.prio == 0xff || (cs->icc_igrpen[cs->hppi.grp] == 0);
739 static bool icc_hppi_can_preempt(GICv3CPUState *cs)
741 /* Return true if we have a pending interrupt of sufficient
742 * priority to preempt.
744 int rprio;
745 uint32_t mask;
747 if (icc_no_enabled_hppi(cs)) {
748 return false;
751 if (cs->hppi.prio >= cs->icc_pmr_el1) {
752 /* Priority mask masks this interrupt */
753 return false;
756 rprio = icc_highest_active_prio(cs);
757 if (rprio == 0xff) {
758 /* No currently running interrupt so we can preempt */
759 return true;
762 mask = icc_gprio_mask(cs, cs->hppi.grp);
764 /* We only preempt a running interrupt if the pending interrupt's
765 * group priority is sufficient (the subpriorities are not considered).
767 if ((cs->hppi.prio & mask) < (rprio & mask)) {
768 return true;
771 return false;
774 void gicv3_cpuif_update(GICv3CPUState *cs)
776 /* Tell the CPU about its highest priority pending interrupt */
777 int irqlevel = 0;
778 int fiqlevel = 0;
779 ARMCPU *cpu = ARM_CPU(cs->cpu);
780 CPUARMState *env = &cpu->env;
782 g_assert(qemu_mutex_iothread_locked());
784 trace_gicv3_cpuif_update(gicv3_redist_affid(cs), cs->hppi.irq,
785 cs->hppi.grp, cs->hppi.prio);
787 if (cs->hppi.grp == GICV3_G1 && !arm_feature(env, ARM_FEATURE_EL3)) {
788 /* If a Security-enabled GIC sends a G1S interrupt to a
789 * Security-disabled CPU, we must treat it as if it were G0.
791 cs->hppi.grp = GICV3_G0;
794 if (icc_hppi_can_preempt(cs)) {
795 /* We have an interrupt: should we signal it as IRQ or FIQ?
796 * This is described in the GICv3 spec section 4.6.2.
798 bool isfiq;
800 switch (cs->hppi.grp) {
801 case GICV3_G0:
802 isfiq = true;
803 break;
804 case GICV3_G1:
805 isfiq = (!arm_is_secure(env) ||
806 (arm_current_el(env) == 3 && arm_el_is_aa64(env, 3)));
807 break;
808 case GICV3_G1NS:
809 isfiq = arm_is_secure(env);
810 break;
811 default:
812 g_assert_not_reached();
815 if (isfiq) {
816 fiqlevel = 1;
817 } else {
818 irqlevel = 1;
822 trace_gicv3_cpuif_set_irqs(gicv3_redist_affid(cs), fiqlevel, irqlevel);
824 qemu_set_irq(cs->parent_fiq, fiqlevel);
825 qemu_set_irq(cs->parent_irq, irqlevel);
828 static uint64_t icc_pmr_read(CPUARMState *env, const ARMCPRegInfo *ri)
830 GICv3CPUState *cs = icc_cs_from_env(env);
831 uint32_t value = cs->icc_pmr_el1;
833 if (icv_access(env, HCR_FMO | HCR_IMO)) {
834 return icv_pmr_read(env, ri);
837 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env) &&
838 (env->cp15.scr_el3 & SCR_FIQ)) {
839 /* NS access and Group 0 is inaccessible to NS: return the
840 * NS view of the current priority
842 if ((value & 0x80) == 0) {
843 /* Secure priorities not visible to NS */
844 value = 0;
845 } else if (value != 0xff) {
846 value = (value << 1) & 0xff;
850 trace_gicv3_icc_pmr_read(gicv3_redist_affid(cs), value);
852 return value;
855 static void icc_pmr_write(CPUARMState *env, const ARMCPRegInfo *ri,
856 uint64_t value)
858 GICv3CPUState *cs = icc_cs_from_env(env);
860 if (icv_access(env, HCR_FMO | HCR_IMO)) {
861 return icv_pmr_write(env, ri, value);
864 trace_gicv3_icc_pmr_write(gicv3_redist_affid(cs), value);
866 value &= 0xff;
868 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_is_secure(env) &&
869 (env->cp15.scr_el3 & SCR_FIQ)) {
870 /* NS access and Group 0 is inaccessible to NS: return the
871 * NS view of the current priority
873 if (!(cs->icc_pmr_el1 & 0x80)) {
874 /* Current PMR in the secure range, don't allow NS to change it */
875 return;
877 value = (value >> 1) | 0x80;
879 cs->icc_pmr_el1 = value;
880 gicv3_cpuif_update(cs);
883 static void icc_activate_irq(GICv3CPUState *cs, int irq)
885 /* Move the interrupt from the Pending state to Active, and update
886 * the Active Priority Registers
888 uint32_t mask = icc_gprio_mask(cs, cs->hppi.grp);
889 int prio = cs->hppi.prio & mask;
890 int aprbit = prio >> 1;
891 int regno = aprbit / 32;
892 int regbit = aprbit % 32;
894 cs->icc_apr[cs->hppi.grp][regno] |= (1 << regbit);
896 if (irq < GIC_INTERNAL) {
897 cs->gicr_iactiver0 = deposit32(cs->gicr_iactiver0, irq, 1, 1);
898 cs->gicr_ipendr0 = deposit32(cs->gicr_ipendr0, irq, 1, 0);
899 gicv3_redist_update(cs);
900 } else {
901 gicv3_gicd_active_set(cs->gic, irq);
902 gicv3_gicd_pending_clear(cs->gic, irq);
903 gicv3_update(cs->gic, irq, 1);
907 static uint64_t icc_hppir0_value(GICv3CPUState *cs, CPUARMState *env)
909 /* Return the highest priority pending interrupt register value
910 * for group 0.
912 bool irq_is_secure;
914 if (cs->hppi.prio == 0xff) {
915 return INTID_SPURIOUS;
918 /* Check whether we can return the interrupt or if we should return
919 * a special identifier, as per the CheckGroup0ForSpecialIdentifiers
920 * pseudocode. (We can simplify a little because for us ICC_SRE_EL1.RM
921 * is always zero.)
923 irq_is_secure = (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) &&
924 (cs->hppi.grp != GICV3_G1NS));
926 if (cs->hppi.grp != GICV3_G0 && !arm_is_el3_or_mon(env)) {
927 return INTID_SPURIOUS;
929 if (irq_is_secure && !arm_is_secure(env)) {
930 /* Secure interrupts not visible to Nonsecure */
931 return INTID_SPURIOUS;
934 if (cs->hppi.grp != GICV3_G0) {
935 /* Indicate to EL3 that there's a Group 1 interrupt for the other
936 * state pending.
938 return irq_is_secure ? INTID_SECURE : INTID_NONSECURE;
941 return cs->hppi.irq;
944 static uint64_t icc_hppir1_value(GICv3CPUState *cs, CPUARMState *env)
946 /* Return the highest priority pending interrupt register value
947 * for group 1.
949 bool irq_is_secure;
951 if (cs->hppi.prio == 0xff) {
952 return INTID_SPURIOUS;
955 /* Check whether we can return the interrupt or if we should return
956 * a special identifier, as per the CheckGroup1ForSpecialIdentifiers
957 * pseudocode. (We can simplify a little because for us ICC_SRE_EL1.RM
958 * is always zero.)
960 irq_is_secure = (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) &&
961 (cs->hppi.grp != GICV3_G1NS));
963 if (cs->hppi.grp == GICV3_G0) {
964 /* Group 0 interrupts not visible via HPPIR1 */
965 return INTID_SPURIOUS;
967 if (irq_is_secure) {
968 if (!arm_is_secure(env)) {
969 /* Secure interrupts not visible in Non-secure */
970 return INTID_SPURIOUS;
972 } else if (!arm_is_el3_or_mon(env) && arm_is_secure(env)) {
973 /* Group 1 non-secure interrupts not visible in Secure EL1 */
974 return INTID_SPURIOUS;
977 return cs->hppi.irq;
980 static uint64_t icc_iar0_read(CPUARMState *env, const ARMCPRegInfo *ri)
982 GICv3CPUState *cs = icc_cs_from_env(env);
983 uint64_t intid;
985 if (icv_access(env, HCR_FMO)) {
986 return icv_iar_read(env, ri);
989 if (!icc_hppi_can_preempt(cs)) {
990 intid = INTID_SPURIOUS;
991 } else {
992 intid = icc_hppir0_value(cs, env);
995 if (!(intid >= INTID_SECURE && intid <= INTID_SPURIOUS)) {
996 icc_activate_irq(cs, intid);
999 trace_gicv3_icc_iar0_read(gicv3_redist_affid(cs), intid);
1000 return intid;
1003 static uint64_t icc_iar1_read(CPUARMState *env, const ARMCPRegInfo *ri)
1005 GICv3CPUState *cs = icc_cs_from_env(env);
1006 uint64_t intid;
1008 if (icv_access(env, HCR_IMO)) {
1009 return icv_iar_read(env, ri);
1012 if (!icc_hppi_can_preempt(cs)) {
1013 intid = INTID_SPURIOUS;
1014 } else {
1015 intid = icc_hppir1_value(cs, env);
1018 if (!(intid >= INTID_SECURE && intid <= INTID_SPURIOUS)) {
1019 icc_activate_irq(cs, intid);
1022 trace_gicv3_icc_iar1_read(gicv3_redist_affid(cs), intid);
1023 return intid;
1026 static void icc_drop_prio(GICv3CPUState *cs, int grp)
1028 /* Drop the priority of the currently active interrupt in
1029 * the specified group.
1031 * Note that we can guarantee (because of the requirement to nest
1032 * ICC_IAR reads [which activate an interrupt and raise priority]
1033 * with ICC_EOIR writes [which drop the priority for the interrupt])
1034 * that the interrupt we're being called for is the highest priority
1035 * active interrupt, meaning that it has the lowest set bit in the
1036 * APR registers.
1038 * If the guest does not honour the ordering constraints then the
1039 * behaviour of the GIC is UNPREDICTABLE, which for us means that
1040 * the values of the APR registers might become incorrect and the
1041 * running priority will be wrong, so interrupts that should preempt
1042 * might not do so, and interrupts that should not preempt might do so.
1044 int i;
1046 for (i = 0; i < ARRAY_SIZE(cs->icc_apr[grp]); i++) {
1047 uint64_t *papr = &cs->icc_apr[grp][i];
1049 if (!*papr) {
1050 continue;
1052 /* Clear the lowest set bit */
1053 *papr &= *papr - 1;
1054 break;
1057 /* running priority change means we need an update for this cpu i/f */
1058 gicv3_cpuif_update(cs);
1061 static bool icc_eoi_split(CPUARMState *env, GICv3CPUState *cs)
1063 /* Return true if we should split priority drop and interrupt
1064 * deactivation, ie whether the relevant EOIMode bit is set.
1066 if (arm_is_el3_or_mon(env)) {
1067 return cs->icc_ctlr_el3 & ICC_CTLR_EL3_EOIMODE_EL3;
1069 if (arm_is_secure_below_el3(env)) {
1070 return cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_EOIMODE;
1071 } else {
1072 return cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE;
1076 static int icc_highest_active_group(GICv3CPUState *cs)
1078 /* Return the group with the highest priority active interrupt.
1079 * We can do this by just comparing the APRs to see which one
1080 * has the lowest set bit.
1081 * (If more than one group is active at the same priority then
1082 * we're in UNPREDICTABLE territory.)
1084 int i;
1086 for (i = 0; i < ARRAY_SIZE(cs->icc_apr[0]); i++) {
1087 int g0ctz = ctz32(cs->icc_apr[GICV3_G0][i]);
1088 int g1ctz = ctz32(cs->icc_apr[GICV3_G1][i]);
1089 int g1nsctz = ctz32(cs->icc_apr[GICV3_G1NS][i]);
1091 if (g1nsctz < g0ctz && g1nsctz < g1ctz) {
1092 return GICV3_G1NS;
1094 if (g1ctz < g0ctz) {
1095 return GICV3_G1;
1097 if (g0ctz < 32) {
1098 return GICV3_G0;
1101 /* No set active bits? UNPREDICTABLE; return -1 so the caller
1102 * ignores the spurious EOI attempt.
1104 return -1;
1107 static void icc_deactivate_irq(GICv3CPUState *cs, int irq)
1109 if (irq < GIC_INTERNAL) {
1110 cs->gicr_iactiver0 = deposit32(cs->gicr_iactiver0, irq, 1, 0);
1111 gicv3_redist_update(cs);
1112 } else {
1113 gicv3_gicd_active_clear(cs->gic, irq);
1114 gicv3_update(cs->gic, irq, 1);
1118 static bool icv_eoi_split(CPUARMState *env, GICv3CPUState *cs)
1120 /* Return true if we should split priority drop and interrupt
1121 * deactivation, ie whether the virtual EOIMode bit is set.
1123 return cs->ich_vmcr_el2 & ICH_VMCR_EL2_VEOIM;
1126 static int icv_find_active(GICv3CPUState *cs, int irq)
1128 /* Given an interrupt number for an active interrupt, return the index
1129 * of the corresponding list register, or -1 if there is no match.
1130 * Corresponds to FindActiveVirtualInterrupt pseudocode.
1132 int i;
1134 for (i = 0; i < cs->num_list_regs; i++) {
1135 uint64_t lr = cs->ich_lr_el2[i];
1137 if ((lr & ICH_LR_EL2_STATE_ACTIVE_BIT) && ich_lr_vintid(lr) == irq) {
1138 return i;
1142 return -1;
1145 static void icv_deactivate_irq(GICv3CPUState *cs, int idx)
1147 /* Deactivate the interrupt in the specified list register index */
1148 uint64_t lr = cs->ich_lr_el2[idx];
1150 if (lr & ICH_LR_EL2_HW) {
1151 /* Deactivate the associated physical interrupt */
1152 int pirq = ich_lr_pintid(lr);
1154 if (pirq < INTID_SECURE) {
1155 icc_deactivate_irq(cs, pirq);
1159 /* Clear the 'active' part of the state, so ActivePending->Pending
1160 * and Active->Invalid.
1162 lr &= ~ICH_LR_EL2_STATE_ACTIVE_BIT;
1163 cs->ich_lr_el2[idx] = lr;
1166 static void icv_increment_eoicount(GICv3CPUState *cs)
1168 /* Increment the EOICOUNT field in ICH_HCR_EL2 */
1169 int eoicount = extract64(cs->ich_hcr_el2, ICH_HCR_EL2_EOICOUNT_SHIFT,
1170 ICH_HCR_EL2_EOICOUNT_LENGTH);
1172 cs->ich_hcr_el2 = deposit64(cs->ich_hcr_el2, ICH_HCR_EL2_EOICOUNT_SHIFT,
1173 ICH_HCR_EL2_EOICOUNT_LENGTH, eoicount + 1);
1176 static int icv_drop_prio(GICv3CPUState *cs)
1178 /* Drop the priority of the currently active virtual interrupt
1179 * (favouring group 0 if there is a set active bit at
1180 * the same priority for both group 0 and group 1).
1181 * Return the priority value for the bit we just cleared,
1182 * or 0xff if no bits were set in the AP registers at all.
1183 * Note that though the ich_apr[] are uint64_t only the low
1184 * 32 bits are actually relevant.
1186 int i;
1187 int aprmax = 1 << (cs->vprebits - 5);
1189 assert(aprmax <= ARRAY_SIZE(cs->ich_apr[0]));
1191 for (i = 0; i < aprmax; i++) {
1192 uint64_t *papr0 = &cs->ich_apr[GICV3_G0][i];
1193 uint64_t *papr1 = &cs->ich_apr[GICV3_G1NS][i];
1194 int apr0count, apr1count;
1196 if (!*papr0 && !*papr1) {
1197 continue;
1200 /* We can't just use the bit-twiddling hack icc_drop_prio() does
1201 * because we need to return the bit number we cleared so
1202 * it can be compared against the list register's priority field.
1204 apr0count = ctz32(*papr0);
1205 apr1count = ctz32(*papr1);
1207 if (apr0count <= apr1count) {
1208 *papr0 &= *papr0 - 1;
1209 return (apr0count + i * 32) << (icv_min_vbpr(cs) + 1);
1210 } else {
1211 *papr1 &= *papr1 - 1;
1212 return (apr1count + i * 32) << (icv_min_vbpr(cs) + 1);
1215 return 0xff;
1218 static void icv_dir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1219 uint64_t value)
1221 /* Deactivate interrupt */
1222 GICv3CPUState *cs = icc_cs_from_env(env);
1223 int idx;
1224 int irq = value & 0xffffff;
1226 trace_gicv3_icv_dir_write(gicv3_redist_affid(cs), value);
1228 if (irq >= cs->gic->num_irq) {
1229 /* Also catches special interrupt numbers and LPIs */
1230 return;
1233 if (!icv_eoi_split(env, cs)) {
1234 return;
1237 idx = icv_find_active(cs, irq);
1239 if (idx < 0) {
1240 /* No list register matching this, so increment the EOI count
1241 * (might trigger a maintenance interrupt)
1243 icv_increment_eoicount(cs);
1244 } else {
1245 icv_deactivate_irq(cs, idx);
1248 gicv3_cpuif_virt_update(cs);
1251 static void icv_eoir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1252 uint64_t value)
1254 /* End of Interrupt */
1255 GICv3CPUState *cs = icc_cs_from_env(env);
1256 int irq = value & 0xffffff;
1257 int grp = ri->crm == 8 ? GICV3_G0 : GICV3_G1NS;
1258 int idx, dropprio;
1260 trace_gicv3_icv_eoir_write(ri->crm == 8 ? 0 : 1,
1261 gicv3_redist_affid(cs), value);
1263 if (irq >= cs->gic->num_irq) {
1264 /* Also catches special interrupt numbers and LPIs */
1265 return;
1268 /* We implement the IMPDEF choice of "drop priority before doing
1269 * error checks" (because that lets us avoid scanning the AP
1270 * registers twice).
1272 dropprio = icv_drop_prio(cs);
1273 if (dropprio == 0xff) {
1274 /* No active interrupt. It is CONSTRAINED UNPREDICTABLE
1275 * whether the list registers are checked in this
1276 * situation; we choose not to.
1278 return;
1281 idx = icv_find_active(cs, irq);
1283 if (idx < 0) {
1284 /* No valid list register corresponding to EOI ID */
1285 icv_increment_eoicount(cs);
1286 } else {
1287 uint64_t lr = cs->ich_lr_el2[idx];
1288 int thisgrp = (lr & ICH_LR_EL2_GROUP) ? GICV3_G1NS : GICV3_G0;
1289 int lr_gprio = ich_lr_prio(lr) & icv_gprio_mask(cs, grp);
1291 if (thisgrp == grp && lr_gprio == dropprio) {
1292 if (!icv_eoi_split(env, cs)) {
1293 /* Priority drop and deactivate not split: deactivate irq now */
1294 icv_deactivate_irq(cs, idx);
1299 gicv3_cpuif_virt_update(cs);
1302 static void icc_eoir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1303 uint64_t value)
1305 /* End of Interrupt */
1306 GICv3CPUState *cs = icc_cs_from_env(env);
1307 int irq = value & 0xffffff;
1308 int grp;
1310 if (icv_access(env, ri->crm == 8 ? HCR_FMO : HCR_IMO)) {
1311 icv_eoir_write(env, ri, value);
1312 return;
1315 trace_gicv3_icc_eoir_write(ri->crm == 8 ? 0 : 1,
1316 gicv3_redist_affid(cs), value);
1318 if (ri->crm == 8) {
1319 /* EOIR0 */
1320 grp = GICV3_G0;
1321 } else {
1322 /* EOIR1 */
1323 if (arm_is_secure(env)) {
1324 grp = GICV3_G1;
1325 } else {
1326 grp = GICV3_G1NS;
1330 if (irq >= cs->gic->num_irq) {
1331 /* This handles two cases:
1332 * 1. If software writes the ID of a spurious interrupt [ie 1020-1023]
1333 * to the GICC_EOIR, the GIC ignores that write.
1334 * 2. If software writes the number of a non-existent interrupt
1335 * this must be a subcase of "value written does not match the last
1336 * valid interrupt value read from the Interrupt Acknowledge
1337 * register" and so this is UNPREDICTABLE. We choose to ignore it.
1339 return;
1342 if (icc_highest_active_group(cs) != grp) {
1343 return;
1346 icc_drop_prio(cs, grp);
1348 if (!icc_eoi_split(env, cs)) {
1349 /* Priority drop and deactivate not split: deactivate irq now */
1350 icc_deactivate_irq(cs, irq);
1354 static uint64_t icc_hppir0_read(CPUARMState *env, const ARMCPRegInfo *ri)
1356 GICv3CPUState *cs = icc_cs_from_env(env);
1357 uint64_t value;
1359 if (icv_access(env, HCR_FMO)) {
1360 return icv_hppir_read(env, ri);
1363 value = icc_hppir0_value(cs, env);
1364 trace_gicv3_icc_hppir0_read(gicv3_redist_affid(cs), value);
1365 return value;
1368 static uint64_t icc_hppir1_read(CPUARMState *env, const ARMCPRegInfo *ri)
1370 GICv3CPUState *cs = icc_cs_from_env(env);
1371 uint64_t value;
1373 if (icv_access(env, HCR_IMO)) {
1374 return icv_hppir_read(env, ri);
1377 value = icc_hppir1_value(cs, env);
1378 trace_gicv3_icc_hppir1_read(gicv3_redist_affid(cs), value);
1379 return value;
1382 static uint64_t icc_bpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1384 GICv3CPUState *cs = icc_cs_from_env(env);
1385 int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1;
1386 bool satinc = false;
1387 uint64_t bpr;
1389 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1390 return icv_bpr_read(env, ri);
1393 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1394 grp = GICV3_G1NS;
1397 if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) &&
1398 (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) {
1399 /* CBPR_EL1S means secure EL1 or AArch32 EL3 !Mon BPR1 accesses
1400 * modify BPR0
1402 grp = GICV3_G0;
1405 if (grp == GICV3_G1NS && arm_current_el(env) < 3 &&
1406 (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
1407 /* reads return bpr0 + 1 sat to 7, writes ignored */
1408 grp = GICV3_G0;
1409 satinc = true;
1412 bpr = cs->icc_bpr[grp];
1413 if (satinc) {
1414 bpr++;
1415 bpr = MIN(bpr, 7);
1418 trace_gicv3_icc_bpr_read(ri->crm == 8 ? 0 : 1, gicv3_redist_affid(cs), bpr);
1420 return bpr;
1423 static void icc_bpr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1424 uint64_t value)
1426 GICv3CPUState *cs = icc_cs_from_env(env);
1427 int grp = (ri->crm == 8) ? GICV3_G0 : GICV3_G1;
1428 uint64_t minval;
1430 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1431 icv_bpr_write(env, ri, value);
1432 return;
1435 trace_gicv3_icc_bpr_write(ri->crm == 8 ? 0 : 1,
1436 gicv3_redist_affid(cs), value);
1438 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1439 grp = GICV3_G1NS;
1442 if (grp == GICV3_G1 && !arm_is_el3_or_mon(env) &&
1443 (cs->icc_ctlr_el1[GICV3_S] & ICC_CTLR_EL1_CBPR)) {
1444 /* CBPR_EL1S means secure EL1 or AArch32 EL3 !Mon BPR1 accesses
1445 * modify BPR0
1447 grp = GICV3_G0;
1450 if (grp == GICV3_G1NS && arm_current_el(env) < 3 &&
1451 (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR)) {
1452 /* reads return bpr0 + 1 sat to 7, writes ignored */
1453 return;
1456 minval = (grp == GICV3_G1NS) ? GIC_MIN_BPR_NS : GIC_MIN_BPR;
1457 if (value < minval) {
1458 value = minval;
1461 cs->icc_bpr[grp] = value & 7;
1462 gicv3_cpuif_update(cs);
1465 static uint64_t icc_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
1467 GICv3CPUState *cs = icc_cs_from_env(env);
1468 uint64_t value;
1470 int regno = ri->opc2 & 3;
1471 int grp = (ri->crm & 1) ? GICV3_G1 : GICV3_G0;
1473 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1474 return icv_ap_read(env, ri);
1477 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1478 grp = GICV3_G1NS;
1481 value = cs->icc_apr[grp][regno];
1483 trace_gicv3_icc_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
1484 return value;
1487 static void icc_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
1488 uint64_t value)
1490 GICv3CPUState *cs = icc_cs_from_env(env);
1492 int regno = ri->opc2 & 3;
1493 int grp = (ri->crm & 1) ? GICV3_G1 : GICV3_G0;
1495 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1496 icv_ap_write(env, ri, value);
1497 return;
1500 trace_gicv3_icc_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
1502 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1503 grp = GICV3_G1NS;
1506 /* It's not possible to claim that a Non-secure interrupt is active
1507 * at a priority outside the Non-secure range (128..255), since this
1508 * would otherwise allow malicious NS code to block delivery of S interrupts
1509 * by writing a bad value to these registers.
1511 if (grp == GICV3_G1NS && regno < 2 && arm_feature(env, ARM_FEATURE_EL3)) {
1512 return;
1515 cs->icc_apr[grp][regno] = value & 0xFFFFFFFFU;
1516 gicv3_cpuif_update(cs);
1519 static void icc_dir_write(CPUARMState *env, const ARMCPRegInfo *ri,
1520 uint64_t value)
1522 /* Deactivate interrupt */
1523 GICv3CPUState *cs = icc_cs_from_env(env);
1524 int irq = value & 0xffffff;
1525 bool irq_is_secure, single_sec_state, irq_is_grp0;
1526 bool route_fiq_to_el3, route_irq_to_el3, route_fiq_to_el2, route_irq_to_el2;
1528 if (icv_access(env, HCR_FMO | HCR_IMO)) {
1529 icv_dir_write(env, ri, value);
1530 return;
1533 trace_gicv3_icc_dir_write(gicv3_redist_affid(cs), value);
1535 if (irq >= cs->gic->num_irq) {
1536 /* Also catches special interrupt numbers and LPIs */
1537 return;
1540 if (!icc_eoi_split(env, cs)) {
1541 return;
1544 int grp = gicv3_irq_group(cs->gic, cs, irq);
1546 single_sec_state = cs->gic->gicd_ctlr & GICD_CTLR_DS;
1547 irq_is_secure = !single_sec_state && (grp != GICV3_G1NS);
1548 irq_is_grp0 = grp == GICV3_G0;
1550 /* Check whether we're allowed to deactivate this interrupt based
1551 * on its group and the current CPU state.
1552 * These checks are laid out to correspond to the spec's pseudocode.
1554 route_fiq_to_el3 = env->cp15.scr_el3 & SCR_FIQ;
1555 route_irq_to_el3 = env->cp15.scr_el3 & SCR_IRQ;
1556 /* No need to include !IsSecure in route_*_to_el2 as it's only
1557 * tested in cases where we know !IsSecure is true.
1559 uint64_t hcr_el2 = arm_hcr_el2_eff(env);
1560 route_fiq_to_el2 = hcr_el2 & HCR_FMO;
1561 route_irq_to_el2 = hcr_el2 & HCR_IMO;
1563 switch (arm_current_el(env)) {
1564 case 3:
1565 break;
1566 case 2:
1567 if (single_sec_state && irq_is_grp0 && !route_fiq_to_el3) {
1568 break;
1570 if (!irq_is_secure && !irq_is_grp0 && !route_irq_to_el3) {
1571 break;
1573 return;
1574 case 1:
1575 if (!arm_is_secure_below_el3(env)) {
1576 if (single_sec_state && irq_is_grp0 &&
1577 !route_fiq_to_el3 && !route_fiq_to_el2) {
1578 break;
1580 if (!irq_is_secure && !irq_is_grp0 &&
1581 !route_irq_to_el3 && !route_irq_to_el2) {
1582 break;
1584 } else {
1585 if (irq_is_grp0 && !route_fiq_to_el3) {
1586 break;
1588 if (!irq_is_grp0 &&
1589 (!irq_is_secure || !single_sec_state) &&
1590 !route_irq_to_el3) {
1591 break;
1594 return;
1595 default:
1596 g_assert_not_reached();
1599 icc_deactivate_irq(cs, irq);
1602 static uint64_t icc_rpr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1604 GICv3CPUState *cs = icc_cs_from_env(env);
1605 int prio;
1607 if (icv_access(env, HCR_FMO | HCR_IMO)) {
1608 return icv_rpr_read(env, ri);
1611 prio = icc_highest_active_prio(cs);
1613 if (arm_feature(env, ARM_FEATURE_EL3) &&
1614 !arm_is_secure(env) && (env->cp15.scr_el3 & SCR_FIQ)) {
1615 /* NS GIC access and Group 0 is inaccessible to NS */
1616 if ((prio & 0x80) == 0) {
1617 /* NS mustn't see priorities in the Secure half of the range */
1618 prio = 0;
1619 } else if (prio != 0xff) {
1620 /* Non-idle priority: show the Non-secure view of it */
1621 prio = (prio << 1) & 0xff;
1625 trace_gicv3_icc_rpr_read(gicv3_redist_affid(cs), prio);
1626 return prio;
1629 static void icc_generate_sgi(CPUARMState *env, GICv3CPUState *cs,
1630 uint64_t value, int grp, bool ns)
1632 GICv3State *s = cs->gic;
1634 /* Extract Aff3/Aff2/Aff1 and shift into the bottom 24 bits */
1635 uint64_t aff = extract64(value, 48, 8) << 16 |
1636 extract64(value, 32, 8) << 8 |
1637 extract64(value, 16, 8);
1638 uint32_t targetlist = extract64(value, 0, 16);
1639 uint32_t irq = extract64(value, 24, 4);
1640 bool irm = extract64(value, 40, 1);
1641 int i;
1643 if (grp == GICV3_G1 && s->gicd_ctlr & GICD_CTLR_DS) {
1644 /* If GICD_CTLR.DS == 1, the Distributor treats Secure Group 1
1645 * interrupts as Group 0 interrupts and must send Secure Group 0
1646 * interrupts to the target CPUs.
1648 grp = GICV3_G0;
1651 trace_gicv3_icc_generate_sgi(gicv3_redist_affid(cs), irq, irm,
1652 aff, targetlist);
1654 for (i = 0; i < s->num_cpu; i++) {
1655 GICv3CPUState *ocs = &s->cpu[i];
1657 if (irm) {
1658 /* IRM == 1 : route to all CPUs except self */
1659 if (cs == ocs) {
1660 continue;
1662 } else {
1663 /* IRM == 0 : route to Aff3.Aff2.Aff1.n for all n in [0..15]
1664 * where the corresponding bit is set in targetlist
1666 int aff0;
1668 if (ocs->gicr_typer >> 40 != aff) {
1669 continue;
1671 aff0 = extract64(ocs->gicr_typer, 32, 8);
1672 if (aff0 > 15 || extract32(targetlist, aff0, 1) == 0) {
1673 continue;
1677 /* The redistributor will check against its own GICR_NSACR as needed */
1678 gicv3_redist_send_sgi(ocs, grp, irq, ns);
1682 static void icc_sgi0r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1683 uint64_t value)
1685 /* Generate Secure Group 0 SGI. */
1686 GICv3CPUState *cs = icc_cs_from_env(env);
1687 bool ns = !arm_is_secure(env);
1689 icc_generate_sgi(env, cs, value, GICV3_G0, ns);
1692 static void icc_sgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1693 uint64_t value)
1695 /* Generate Group 1 SGI for the current Security state */
1696 GICv3CPUState *cs = icc_cs_from_env(env);
1697 int grp;
1698 bool ns = !arm_is_secure(env);
1700 grp = ns ? GICV3_G1NS : GICV3_G1;
1701 icc_generate_sgi(env, cs, value, grp, ns);
1704 static void icc_asgi1r_write(CPUARMState *env, const ARMCPRegInfo *ri,
1705 uint64_t value)
1707 /* Generate Group 1 SGI for the Security state that is not
1708 * the current state
1710 GICv3CPUState *cs = icc_cs_from_env(env);
1711 int grp;
1712 bool ns = !arm_is_secure(env);
1714 grp = ns ? GICV3_G1 : GICV3_G1NS;
1715 icc_generate_sgi(env, cs, value, grp, ns);
1718 static uint64_t icc_igrpen_read(CPUARMState *env, const ARMCPRegInfo *ri)
1720 GICv3CPUState *cs = icc_cs_from_env(env);
1721 int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0;
1722 uint64_t value;
1724 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1725 return icv_igrpen_read(env, ri);
1728 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1729 grp = GICV3_G1NS;
1732 value = cs->icc_igrpen[grp];
1733 trace_gicv3_icc_igrpen_read(ri->opc2 & 1 ? 1 : 0,
1734 gicv3_redist_affid(cs), value);
1735 return value;
1738 static void icc_igrpen_write(CPUARMState *env, const ARMCPRegInfo *ri,
1739 uint64_t value)
1741 GICv3CPUState *cs = icc_cs_from_env(env);
1742 int grp = ri->opc2 & 1 ? GICV3_G1 : GICV3_G0;
1744 if (icv_access(env, grp == GICV3_G0 ? HCR_FMO : HCR_IMO)) {
1745 icv_igrpen_write(env, ri, value);
1746 return;
1749 trace_gicv3_icc_igrpen_write(ri->opc2 & 1 ? 1 : 0,
1750 gicv3_redist_affid(cs), value);
1752 if (grp == GICV3_G1 && gicv3_use_ns_bank(env)) {
1753 grp = GICV3_G1NS;
1756 cs->icc_igrpen[grp] = value & ICC_IGRPEN_ENABLE;
1757 gicv3_cpuif_update(cs);
1760 static uint64_t icc_igrpen1_el3_read(CPUARMState *env, const ARMCPRegInfo *ri)
1762 GICv3CPUState *cs = icc_cs_from_env(env);
1763 uint64_t value;
1765 /* IGRPEN1_EL3 bits 0 and 1 are r/w aliases into IGRPEN1_EL1 NS and S */
1766 value = cs->icc_igrpen[GICV3_G1NS] | (cs->icc_igrpen[GICV3_G1] << 1);
1767 trace_gicv3_icc_igrpen1_el3_read(gicv3_redist_affid(cs), value);
1768 return value;
1771 static void icc_igrpen1_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
1772 uint64_t value)
1774 GICv3CPUState *cs = icc_cs_from_env(env);
1776 trace_gicv3_icc_igrpen1_el3_write(gicv3_redist_affid(cs), value);
1778 /* IGRPEN1_EL3 bits 0 and 1 are r/w aliases into IGRPEN1_EL1 NS and S */
1779 cs->icc_igrpen[GICV3_G1NS] = extract32(value, 0, 1);
1780 cs->icc_igrpen[GICV3_G1] = extract32(value, 1, 1);
1781 gicv3_cpuif_update(cs);
1784 static uint64_t icc_ctlr_el1_read(CPUARMState *env, const ARMCPRegInfo *ri)
1786 GICv3CPUState *cs = icc_cs_from_env(env);
1787 int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S;
1788 uint64_t value;
1790 if (icv_access(env, HCR_FMO | HCR_IMO)) {
1791 return icv_ctlr_read(env, ri);
1794 value = cs->icc_ctlr_el1[bank];
1795 trace_gicv3_icc_ctlr_read(gicv3_redist_affid(cs), value);
1796 return value;
1799 static void icc_ctlr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
1800 uint64_t value)
1802 GICv3CPUState *cs = icc_cs_from_env(env);
1803 int bank = gicv3_use_ns_bank(env) ? GICV3_NS : GICV3_S;
1804 uint64_t mask;
1806 if (icv_access(env, HCR_FMO | HCR_IMO)) {
1807 icv_ctlr_write(env, ri, value);
1808 return;
1811 trace_gicv3_icc_ctlr_write(gicv3_redist_affid(cs), value);
1813 /* Only CBPR and EOIMODE can be RW;
1814 * for us PMHE is RAZ/WI (we don't implement 1-of-N interrupts or
1815 * the asseciated priority-based routing of them);
1816 * if EL3 is implemented and GICD_CTLR.DS == 0, then PMHE and CBPR are RO.
1818 if (arm_feature(env, ARM_FEATURE_EL3) &&
1819 ((cs->gic->gicd_ctlr & GICD_CTLR_DS) == 0)) {
1820 mask = ICC_CTLR_EL1_EOIMODE;
1821 } else {
1822 mask = ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE;
1825 cs->icc_ctlr_el1[bank] &= ~mask;
1826 cs->icc_ctlr_el1[bank] |= (value & mask);
1827 gicv3_cpuif_update(cs);
1831 static uint64_t icc_ctlr_el3_read(CPUARMState *env, const ARMCPRegInfo *ri)
1833 GICv3CPUState *cs = icc_cs_from_env(env);
1834 uint64_t value;
1836 value = cs->icc_ctlr_el3;
1837 if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) {
1838 value |= ICC_CTLR_EL3_EOIMODE_EL1NS;
1840 if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) {
1841 value |= ICC_CTLR_EL3_CBPR_EL1NS;
1843 if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_EOIMODE) {
1844 value |= ICC_CTLR_EL3_EOIMODE_EL1S;
1846 if (cs->icc_ctlr_el1[GICV3_NS] & ICC_CTLR_EL1_CBPR) {
1847 value |= ICC_CTLR_EL3_CBPR_EL1S;
1850 trace_gicv3_icc_ctlr_el3_read(gicv3_redist_affid(cs), value);
1851 return value;
1854 static void icc_ctlr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri,
1855 uint64_t value)
1857 GICv3CPUState *cs = icc_cs_from_env(env);
1858 uint64_t mask;
1860 trace_gicv3_icc_ctlr_el3_write(gicv3_redist_affid(cs), value);
1862 /* *_EL1NS and *_EL1S bits are aliases into the ICC_CTLR_EL1 bits. */
1863 cs->icc_ctlr_el1[GICV3_NS] &= ~(ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE);
1864 if (value & ICC_CTLR_EL3_EOIMODE_EL1NS) {
1865 cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_EOIMODE;
1867 if (value & ICC_CTLR_EL3_CBPR_EL1NS) {
1868 cs->icc_ctlr_el1[GICV3_NS] |= ICC_CTLR_EL1_CBPR;
1871 cs->icc_ctlr_el1[GICV3_S] &= ~(ICC_CTLR_EL1_CBPR | ICC_CTLR_EL1_EOIMODE);
1872 if (value & ICC_CTLR_EL3_EOIMODE_EL1S) {
1873 cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_EOIMODE;
1875 if (value & ICC_CTLR_EL3_CBPR_EL1S) {
1876 cs->icc_ctlr_el1[GICV3_S] |= ICC_CTLR_EL1_CBPR;
1879 /* The only bit stored in icc_ctlr_el3 which is writeable is EOIMODE_EL3: */
1880 mask = ICC_CTLR_EL3_EOIMODE_EL3;
1882 cs->icc_ctlr_el3 &= ~mask;
1883 cs->icc_ctlr_el3 |= (value & mask);
1884 gicv3_cpuif_update(cs);
1887 static CPAccessResult gicv3_irqfiq_access(CPUARMState *env,
1888 const ARMCPRegInfo *ri, bool isread)
1890 CPAccessResult r = CP_ACCESS_OK;
1891 GICv3CPUState *cs = icc_cs_from_env(env);
1892 int el = arm_current_el(env);
1894 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TC) &&
1895 el == 1 && !arm_is_secure_below_el3(env)) {
1896 /* Takes priority over a possible EL3 trap */
1897 return CP_ACCESS_TRAP_EL2;
1900 if ((env->cp15.scr_el3 & (SCR_FIQ | SCR_IRQ)) == (SCR_FIQ | SCR_IRQ)) {
1901 switch (el) {
1902 case 1:
1903 /* Note that arm_hcr_el2_eff takes secure state into account. */
1904 if ((arm_hcr_el2_eff(env) & (HCR_IMO | HCR_FMO)) == 0) {
1905 r = CP_ACCESS_TRAP_EL3;
1907 break;
1908 case 2:
1909 r = CP_ACCESS_TRAP_EL3;
1910 break;
1911 case 3:
1912 if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
1913 r = CP_ACCESS_TRAP_EL3;
1915 break;
1916 default:
1917 g_assert_not_reached();
1921 if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
1922 r = CP_ACCESS_TRAP;
1924 return r;
1927 static CPAccessResult gicv3_dir_access(CPUARMState *env,
1928 const ARMCPRegInfo *ri, bool isread)
1930 GICv3CPUState *cs = icc_cs_from_env(env);
1932 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TDIR) &&
1933 arm_current_el(env) == 1 && !arm_is_secure_below_el3(env)) {
1934 /* Takes priority over a possible EL3 trap */
1935 return CP_ACCESS_TRAP_EL2;
1938 return gicv3_irqfiq_access(env, ri, isread);
1941 static CPAccessResult gicv3_sgi_access(CPUARMState *env,
1942 const ARMCPRegInfo *ri, bool isread)
1944 if (arm_current_el(env) == 1 &&
1945 (arm_hcr_el2_eff(env) & (HCR_IMO | HCR_FMO)) != 0) {
1946 /* Takes priority over a possible EL3 trap */
1947 return CP_ACCESS_TRAP_EL2;
1950 return gicv3_irqfiq_access(env, ri, isread);
1953 static CPAccessResult gicv3_fiq_access(CPUARMState *env,
1954 const ARMCPRegInfo *ri, bool isread)
1956 CPAccessResult r = CP_ACCESS_OK;
1957 GICv3CPUState *cs = icc_cs_from_env(env);
1958 int el = arm_current_el(env);
1960 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TALL0) &&
1961 el == 1 && !arm_is_secure_below_el3(env)) {
1962 /* Takes priority over a possible EL3 trap */
1963 return CP_ACCESS_TRAP_EL2;
1966 if (env->cp15.scr_el3 & SCR_FIQ) {
1967 switch (el) {
1968 case 1:
1969 if ((arm_hcr_el2_eff(env) & HCR_FMO) == 0) {
1970 r = CP_ACCESS_TRAP_EL3;
1972 break;
1973 case 2:
1974 r = CP_ACCESS_TRAP_EL3;
1975 break;
1976 case 3:
1977 if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
1978 r = CP_ACCESS_TRAP_EL3;
1980 break;
1981 default:
1982 g_assert_not_reached();
1986 if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
1987 r = CP_ACCESS_TRAP;
1989 return r;
1992 static CPAccessResult gicv3_irq_access(CPUARMState *env,
1993 const ARMCPRegInfo *ri, bool isread)
1995 CPAccessResult r = CP_ACCESS_OK;
1996 GICv3CPUState *cs = icc_cs_from_env(env);
1997 int el = arm_current_el(env);
1999 if ((cs->ich_hcr_el2 & ICH_HCR_EL2_TALL1) &&
2000 el == 1 && !arm_is_secure_below_el3(env)) {
2001 /* Takes priority over a possible EL3 trap */
2002 return CP_ACCESS_TRAP_EL2;
2005 if (env->cp15.scr_el3 & SCR_IRQ) {
2006 switch (el) {
2007 case 1:
2008 if ((arm_hcr_el2_eff(env) & HCR_IMO) == 0) {
2009 r = CP_ACCESS_TRAP_EL3;
2011 break;
2012 case 2:
2013 r = CP_ACCESS_TRAP_EL3;
2014 break;
2015 case 3:
2016 if (!is_a64(env) && !arm_is_el3_or_mon(env)) {
2017 r = CP_ACCESS_TRAP_EL3;
2019 break;
2020 default:
2021 g_assert_not_reached();
2025 if (r == CP_ACCESS_TRAP_EL3 && !arm_el_is_aa64(env, 3)) {
2026 r = CP_ACCESS_TRAP;
2028 return r;
2031 static void icc_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2033 GICv3CPUState *cs = icc_cs_from_env(env);
2035 cs->icc_ctlr_el1[GICV3_S] = ICC_CTLR_EL1_A3V |
2036 (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
2037 (7 << ICC_CTLR_EL1_PRIBITS_SHIFT);
2038 cs->icc_ctlr_el1[GICV3_NS] = ICC_CTLR_EL1_A3V |
2039 (1 << ICC_CTLR_EL1_IDBITS_SHIFT) |
2040 (7 << ICC_CTLR_EL1_PRIBITS_SHIFT);
2041 cs->icc_pmr_el1 = 0;
2042 cs->icc_bpr[GICV3_G0] = GIC_MIN_BPR;
2043 cs->icc_bpr[GICV3_G1] = GIC_MIN_BPR;
2044 cs->icc_bpr[GICV3_G1NS] = GIC_MIN_BPR_NS;
2045 memset(cs->icc_apr, 0, sizeof(cs->icc_apr));
2046 memset(cs->icc_igrpen, 0, sizeof(cs->icc_igrpen));
2047 cs->icc_ctlr_el3 = ICC_CTLR_EL3_NDS | ICC_CTLR_EL3_A3V |
2048 (1 << ICC_CTLR_EL3_IDBITS_SHIFT) |
2049 (7 << ICC_CTLR_EL3_PRIBITS_SHIFT);
2051 memset(cs->ich_apr, 0, sizeof(cs->ich_apr));
2052 cs->ich_hcr_el2 = 0;
2053 memset(cs->ich_lr_el2, 0, sizeof(cs->ich_lr_el2));
2054 cs->ich_vmcr_el2 = ICH_VMCR_EL2_VFIQEN |
2055 ((icv_min_vbpr(cs) + 1) << ICH_VMCR_EL2_VBPR1_SHIFT) |
2056 (icv_min_vbpr(cs) << ICH_VMCR_EL2_VBPR0_SHIFT);
2059 static const ARMCPRegInfo gicv3_cpuif_reginfo[] = {
2060 { .name = "ICC_PMR_EL1", .state = ARM_CP_STATE_BOTH,
2061 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 6, .opc2 = 0,
2062 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2063 .access = PL1_RW, .accessfn = gicv3_irqfiq_access,
2064 .readfn = icc_pmr_read,
2065 .writefn = icc_pmr_write,
2066 /* We hang the whole cpu interface reset routine off here
2067 * rather than parcelling it out into one little function
2068 * per register
2070 .resetfn = icc_reset,
2072 { .name = "ICC_IAR0_EL1", .state = ARM_CP_STATE_BOTH,
2073 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 0,
2074 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2075 .access = PL1_R, .accessfn = gicv3_fiq_access,
2076 .readfn = icc_iar0_read,
2078 { .name = "ICC_EOIR0_EL1", .state = ARM_CP_STATE_BOTH,
2079 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 1,
2080 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2081 .access = PL1_W, .accessfn = gicv3_fiq_access,
2082 .writefn = icc_eoir_write,
2084 { .name = "ICC_HPPIR0_EL1", .state = ARM_CP_STATE_BOTH,
2085 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 2,
2086 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2087 .access = PL1_R, .accessfn = gicv3_fiq_access,
2088 .readfn = icc_hppir0_read,
2090 { .name = "ICC_BPR0_EL1", .state = ARM_CP_STATE_BOTH,
2091 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 3,
2092 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2093 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2094 .readfn = icc_bpr_read,
2095 .writefn = icc_bpr_write,
2097 { .name = "ICC_AP0R0_EL1", .state = ARM_CP_STATE_BOTH,
2098 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 4,
2099 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2100 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2101 .readfn = icc_ap_read,
2102 .writefn = icc_ap_write,
2104 { .name = "ICC_AP0R1_EL1", .state = ARM_CP_STATE_BOTH,
2105 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 5,
2106 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2107 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2108 .readfn = icc_ap_read,
2109 .writefn = icc_ap_write,
2111 { .name = "ICC_AP0R2_EL1", .state = ARM_CP_STATE_BOTH,
2112 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 6,
2113 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2114 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2115 .readfn = icc_ap_read,
2116 .writefn = icc_ap_write,
2118 { .name = "ICC_AP0R3_EL1", .state = ARM_CP_STATE_BOTH,
2119 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 8, .opc2 = 7,
2120 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2121 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2122 .readfn = icc_ap_read,
2123 .writefn = icc_ap_write,
2125 /* All the ICC_AP1R*_EL1 registers are banked */
2126 { .name = "ICC_AP1R0_EL1", .state = ARM_CP_STATE_BOTH,
2127 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 0,
2128 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2129 .access = PL1_RW, .accessfn = gicv3_irq_access,
2130 .readfn = icc_ap_read,
2131 .writefn = icc_ap_write,
2133 { .name = "ICC_AP1R1_EL1", .state = ARM_CP_STATE_BOTH,
2134 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 1,
2135 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2136 .access = PL1_RW, .accessfn = gicv3_irq_access,
2137 .readfn = icc_ap_read,
2138 .writefn = icc_ap_write,
2140 { .name = "ICC_AP1R2_EL1", .state = ARM_CP_STATE_BOTH,
2141 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 2,
2142 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2143 .access = PL1_RW, .accessfn = gicv3_irq_access,
2144 .readfn = icc_ap_read,
2145 .writefn = icc_ap_write,
2147 { .name = "ICC_AP1R3_EL1", .state = ARM_CP_STATE_BOTH,
2148 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 9, .opc2 = 3,
2149 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2150 .access = PL1_RW, .accessfn = gicv3_irq_access,
2151 .readfn = icc_ap_read,
2152 .writefn = icc_ap_write,
2154 { .name = "ICC_DIR_EL1", .state = ARM_CP_STATE_BOTH,
2155 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 1,
2156 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2157 .access = PL1_W, .accessfn = gicv3_dir_access,
2158 .writefn = icc_dir_write,
2160 { .name = "ICC_RPR_EL1", .state = ARM_CP_STATE_BOTH,
2161 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 3,
2162 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2163 .access = PL1_R, .accessfn = gicv3_irqfiq_access,
2164 .readfn = icc_rpr_read,
2166 { .name = "ICC_SGI1R_EL1", .state = ARM_CP_STATE_AA64,
2167 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 5,
2168 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2169 .access = PL1_W, .accessfn = gicv3_sgi_access,
2170 .writefn = icc_sgi1r_write,
2172 { .name = "ICC_SGI1R",
2173 .cp = 15, .opc1 = 0, .crm = 12,
2174 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2175 .access = PL1_W, .accessfn = gicv3_sgi_access,
2176 .writefn = icc_sgi1r_write,
2178 { .name = "ICC_ASGI1R_EL1", .state = ARM_CP_STATE_AA64,
2179 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 6,
2180 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2181 .access = PL1_W, .accessfn = gicv3_sgi_access,
2182 .writefn = icc_asgi1r_write,
2184 { .name = "ICC_ASGI1R",
2185 .cp = 15, .opc1 = 1, .crm = 12,
2186 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2187 .access = PL1_W, .accessfn = gicv3_sgi_access,
2188 .writefn = icc_asgi1r_write,
2190 { .name = "ICC_SGI0R_EL1", .state = ARM_CP_STATE_AA64,
2191 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 11, .opc2 = 7,
2192 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2193 .access = PL1_W, .accessfn = gicv3_sgi_access,
2194 .writefn = icc_sgi0r_write,
2196 { .name = "ICC_SGI0R",
2197 .cp = 15, .opc1 = 2, .crm = 12,
2198 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_RAW,
2199 .access = PL1_W, .accessfn = gicv3_sgi_access,
2200 .writefn = icc_sgi0r_write,
2202 { .name = "ICC_IAR1_EL1", .state = ARM_CP_STATE_BOTH,
2203 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 0,
2204 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2205 .access = PL1_R, .accessfn = gicv3_irq_access,
2206 .readfn = icc_iar1_read,
2208 { .name = "ICC_EOIR1_EL1", .state = ARM_CP_STATE_BOTH,
2209 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 1,
2210 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2211 .access = PL1_W, .accessfn = gicv3_irq_access,
2212 .writefn = icc_eoir_write,
2214 { .name = "ICC_HPPIR1_EL1", .state = ARM_CP_STATE_BOTH,
2215 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 2,
2216 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2217 .access = PL1_R, .accessfn = gicv3_irq_access,
2218 .readfn = icc_hppir1_read,
2220 /* This register is banked */
2221 { .name = "ICC_BPR1_EL1", .state = ARM_CP_STATE_BOTH,
2222 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 3,
2223 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2224 .access = PL1_RW, .accessfn = gicv3_irq_access,
2225 .readfn = icc_bpr_read,
2226 .writefn = icc_bpr_write,
2228 /* This register is banked */
2229 { .name = "ICC_CTLR_EL1", .state = ARM_CP_STATE_BOTH,
2230 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 4,
2231 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2232 .access = PL1_RW, .accessfn = gicv3_irqfiq_access,
2233 .readfn = icc_ctlr_el1_read,
2234 .writefn = icc_ctlr_el1_write,
2236 { .name = "ICC_SRE_EL1", .state = ARM_CP_STATE_BOTH,
2237 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 5,
2238 .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2239 .access = PL1_RW,
2240 /* We don't support IRQ/FIQ bypass and system registers are
2241 * always enabled, so all our bits are RAZ/WI or RAO/WI.
2242 * This register is banked but since it's constant we don't
2243 * need to do anything special.
2245 .resetvalue = 0x7,
2247 { .name = "ICC_IGRPEN0_EL1", .state = ARM_CP_STATE_BOTH,
2248 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 6,
2249 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2250 .access = PL1_RW, .accessfn = gicv3_fiq_access,
2251 .readfn = icc_igrpen_read,
2252 .writefn = icc_igrpen_write,
2254 /* This register is banked */
2255 { .name = "ICC_IGRPEN1_EL1", .state = ARM_CP_STATE_BOTH,
2256 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 12, .opc2 = 7,
2257 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2258 .access = PL1_RW, .accessfn = gicv3_irq_access,
2259 .readfn = icc_igrpen_read,
2260 .writefn = icc_igrpen_write,
2262 { .name = "ICC_SRE_EL2", .state = ARM_CP_STATE_BOTH,
2263 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 5,
2264 .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2265 .access = PL2_RW,
2266 /* We don't support IRQ/FIQ bypass and system registers are
2267 * always enabled, so all our bits are RAZ/WI or RAO/WI.
2269 .resetvalue = 0xf,
2271 { .name = "ICC_CTLR_EL3", .state = ARM_CP_STATE_BOTH,
2272 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 4,
2273 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2274 .access = PL3_RW,
2275 .readfn = icc_ctlr_el3_read,
2276 .writefn = icc_ctlr_el3_write,
2278 { .name = "ICC_SRE_EL3", .state = ARM_CP_STATE_BOTH,
2279 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 5,
2280 .type = ARM_CP_NO_RAW | ARM_CP_CONST,
2281 .access = PL3_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_IGRPEN1_EL3", .state = ARM_CP_STATE_BOTH,
2288 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 12, .opc2 = 7,
2289 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2290 .access = PL3_RW,
2291 .readfn = icc_igrpen1_el3_read,
2292 .writefn = icc_igrpen1_el3_write,
2294 REGINFO_SENTINEL
2297 static uint64_t ich_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
2299 GICv3CPUState *cs = icc_cs_from_env(env);
2300 int regno = ri->opc2 & 3;
2301 int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
2302 uint64_t value;
2304 value = cs->ich_apr[grp][regno];
2305 trace_gicv3_ich_ap_read(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
2306 return value;
2309 static void ich_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2310 uint64_t value)
2312 GICv3CPUState *cs = icc_cs_from_env(env);
2313 int regno = ri->opc2 & 3;
2314 int grp = (ri->crm & 1) ? GICV3_G1NS : GICV3_G0;
2316 trace_gicv3_ich_ap_write(ri->crm & 1, regno, gicv3_redist_affid(cs), value);
2318 cs->ich_apr[grp][regno] = value & 0xFFFFFFFFU;
2319 gicv3_cpuif_virt_update(cs);
2322 static uint64_t ich_hcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2324 GICv3CPUState *cs = icc_cs_from_env(env);
2325 uint64_t value = cs->ich_hcr_el2;
2327 trace_gicv3_ich_hcr_read(gicv3_redist_affid(cs), value);
2328 return value;
2331 static void ich_hcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2332 uint64_t value)
2334 GICv3CPUState *cs = icc_cs_from_env(env);
2336 trace_gicv3_ich_hcr_write(gicv3_redist_affid(cs), value);
2338 value &= ICH_HCR_EL2_EN | ICH_HCR_EL2_UIE | ICH_HCR_EL2_LRENPIE |
2339 ICH_HCR_EL2_NPIE | ICH_HCR_EL2_VGRP0EIE | ICH_HCR_EL2_VGRP0DIE |
2340 ICH_HCR_EL2_VGRP1EIE | ICH_HCR_EL2_VGRP1DIE | ICH_HCR_EL2_TC |
2341 ICH_HCR_EL2_TALL0 | ICH_HCR_EL2_TALL1 | ICH_HCR_EL2_TSEI |
2342 ICH_HCR_EL2_TDIR | ICH_HCR_EL2_EOICOUNT_MASK;
2344 cs->ich_hcr_el2 = value;
2345 gicv3_cpuif_virt_update(cs);
2348 static uint64_t ich_vmcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2350 GICv3CPUState *cs = icc_cs_from_env(env);
2351 uint64_t value = cs->ich_vmcr_el2;
2353 trace_gicv3_ich_vmcr_read(gicv3_redist_affid(cs), value);
2354 return value;
2357 static void ich_vmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2358 uint64_t value)
2360 GICv3CPUState *cs = icc_cs_from_env(env);
2362 trace_gicv3_ich_vmcr_write(gicv3_redist_affid(cs), value);
2364 value &= ICH_VMCR_EL2_VENG0 | ICH_VMCR_EL2_VENG1 | ICH_VMCR_EL2_VCBPR |
2365 ICH_VMCR_EL2_VEOIM | ICH_VMCR_EL2_VBPR1_MASK |
2366 ICH_VMCR_EL2_VBPR0_MASK | ICH_VMCR_EL2_VPMR_MASK;
2367 value |= ICH_VMCR_EL2_VFIQEN;
2369 cs->ich_vmcr_el2 = value;
2370 /* Enforce "writing BPRs to less than minimum sets them to the minimum"
2371 * by reading and writing back the fields.
2373 write_vbpr(cs, GICV3_G0, read_vbpr(cs, GICV3_G0));
2374 write_vbpr(cs, GICV3_G1, read_vbpr(cs, GICV3_G1));
2376 gicv3_cpuif_virt_update(cs);
2379 static uint64_t ich_lr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2381 GICv3CPUState *cs = icc_cs_from_env(env);
2382 int regno = ri->opc2 | ((ri->crm & 1) << 3);
2383 uint64_t value;
2385 /* This read function handles all of:
2386 * 64-bit reads of the whole LR
2387 * 32-bit reads of the low half of the LR
2388 * 32-bit reads of the high half of the LR
2390 if (ri->state == ARM_CP_STATE_AA32) {
2391 if (ri->crm >= 14) {
2392 value = extract64(cs->ich_lr_el2[regno], 32, 32);
2393 trace_gicv3_ich_lrc_read(regno, gicv3_redist_affid(cs), value);
2394 } else {
2395 value = extract64(cs->ich_lr_el2[regno], 0, 32);
2396 trace_gicv3_ich_lr32_read(regno, gicv3_redist_affid(cs), value);
2398 } else {
2399 value = cs->ich_lr_el2[regno];
2400 trace_gicv3_ich_lr_read(regno, gicv3_redist_affid(cs), value);
2403 return value;
2406 static void ich_lr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2407 uint64_t value)
2409 GICv3CPUState *cs = icc_cs_from_env(env);
2410 int regno = ri->opc2 | ((ri->crm & 1) << 3);
2412 /* This write function handles all of:
2413 * 64-bit writes to the whole LR
2414 * 32-bit writes to the low half of the LR
2415 * 32-bit writes to the high half of the LR
2417 if (ri->state == ARM_CP_STATE_AA32) {
2418 if (ri->crm >= 14) {
2419 trace_gicv3_ich_lrc_write(regno, gicv3_redist_affid(cs), value);
2420 value = deposit64(cs->ich_lr_el2[regno], 32, 32, value);
2421 } else {
2422 trace_gicv3_ich_lr32_write(regno, gicv3_redist_affid(cs), value);
2423 value = deposit64(cs->ich_lr_el2[regno], 0, 32, value);
2425 } else {
2426 trace_gicv3_ich_lr_write(regno, gicv3_redist_affid(cs), value);
2429 /* Enforce RES0 bits in priority field */
2430 if (cs->vpribits < 8) {
2431 value = deposit64(value, ICH_LR_EL2_PRIORITY_SHIFT,
2432 8 - cs->vpribits, 0);
2435 cs->ich_lr_el2[regno] = value;
2436 gicv3_cpuif_virt_update(cs);
2439 static uint64_t ich_vtr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2441 GICv3CPUState *cs = icc_cs_from_env(env);
2442 uint64_t value;
2444 value = ((cs->num_list_regs - 1) << ICH_VTR_EL2_LISTREGS_SHIFT)
2445 | ICH_VTR_EL2_TDS | ICH_VTR_EL2_NV4 | ICH_VTR_EL2_A3V
2446 | (1 << ICH_VTR_EL2_IDBITS_SHIFT)
2447 | ((cs->vprebits - 1) << ICH_VTR_EL2_PREBITS_SHIFT)
2448 | ((cs->vpribits - 1) << ICH_VTR_EL2_PRIBITS_SHIFT);
2450 trace_gicv3_ich_vtr_read(gicv3_redist_affid(cs), value);
2451 return value;
2454 static uint64_t ich_misr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2456 GICv3CPUState *cs = icc_cs_from_env(env);
2457 uint64_t value = maintenance_interrupt_state(cs);
2459 trace_gicv3_ich_misr_read(gicv3_redist_affid(cs), value);
2460 return value;
2463 static uint64_t ich_eisr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2465 GICv3CPUState *cs = icc_cs_from_env(env);
2466 uint64_t value = eoi_maintenance_interrupt_state(cs, NULL);
2468 trace_gicv3_ich_eisr_read(gicv3_redist_affid(cs), value);
2469 return value;
2472 static uint64_t ich_elrsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2474 GICv3CPUState *cs = icc_cs_from_env(env);
2475 uint64_t value = 0;
2476 int i;
2478 for (i = 0; i < cs->num_list_regs; i++) {
2479 uint64_t lr = cs->ich_lr_el2[i];
2481 if ((lr & ICH_LR_EL2_STATE_MASK) == 0 &&
2482 ((lr & ICH_LR_EL2_HW) != 0 || (lr & ICH_LR_EL2_EOI) == 0)) {
2483 value |= (1 << i);
2487 trace_gicv3_ich_elrsr_read(gicv3_redist_affid(cs), value);
2488 return value;
2491 static const ARMCPRegInfo gicv3_cpuif_hcr_reginfo[] = {
2492 { .name = "ICH_AP0R0_EL2", .state = ARM_CP_STATE_BOTH,
2493 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 0,
2494 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2495 .access = PL2_RW,
2496 .readfn = ich_ap_read,
2497 .writefn = ich_ap_write,
2499 { .name = "ICH_AP1R0_EL2", .state = ARM_CP_STATE_BOTH,
2500 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 0,
2501 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2502 .access = PL2_RW,
2503 .readfn = ich_ap_read,
2504 .writefn = ich_ap_write,
2506 { .name = "ICH_HCR_EL2", .state = ARM_CP_STATE_BOTH,
2507 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 0,
2508 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2509 .access = PL2_RW,
2510 .readfn = ich_hcr_read,
2511 .writefn = ich_hcr_write,
2513 { .name = "ICH_VTR_EL2", .state = ARM_CP_STATE_BOTH,
2514 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 1,
2515 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2516 .access = PL2_R,
2517 .readfn = ich_vtr_read,
2519 { .name = "ICH_MISR_EL2", .state = ARM_CP_STATE_BOTH,
2520 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 2,
2521 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2522 .access = PL2_R,
2523 .readfn = ich_misr_read,
2525 { .name = "ICH_EISR_EL2", .state = ARM_CP_STATE_BOTH,
2526 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 3,
2527 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2528 .access = PL2_R,
2529 .readfn = ich_eisr_read,
2531 { .name = "ICH_ELRSR_EL2", .state = ARM_CP_STATE_BOTH,
2532 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 5,
2533 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2534 .access = PL2_R,
2535 .readfn = ich_elrsr_read,
2537 { .name = "ICH_VMCR_EL2", .state = ARM_CP_STATE_BOTH,
2538 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 11, .opc2 = 7,
2539 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2540 .access = PL2_RW,
2541 .readfn = ich_vmcr_read,
2542 .writefn = ich_vmcr_write,
2544 REGINFO_SENTINEL
2547 static const ARMCPRegInfo gicv3_cpuif_ich_apxr1_reginfo[] = {
2548 { .name = "ICH_AP0R1_EL2", .state = ARM_CP_STATE_BOTH,
2549 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 1,
2550 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2551 .access = PL2_RW,
2552 .readfn = ich_ap_read,
2553 .writefn = ich_ap_write,
2555 { .name = "ICH_AP1R1_EL2", .state = ARM_CP_STATE_BOTH,
2556 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 1,
2557 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2558 .access = PL2_RW,
2559 .readfn = ich_ap_read,
2560 .writefn = ich_ap_write,
2562 REGINFO_SENTINEL
2565 static const ARMCPRegInfo gicv3_cpuif_ich_apxr23_reginfo[] = {
2566 { .name = "ICH_AP0R2_EL2", .state = ARM_CP_STATE_BOTH,
2567 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 2,
2568 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2569 .access = PL2_RW,
2570 .readfn = ich_ap_read,
2571 .writefn = ich_ap_write,
2573 { .name = "ICH_AP0R3_EL2", .state = ARM_CP_STATE_BOTH,
2574 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 8, .opc2 = 3,
2575 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2576 .access = PL2_RW,
2577 .readfn = ich_ap_read,
2578 .writefn = ich_ap_write,
2580 { .name = "ICH_AP1R2_EL2", .state = ARM_CP_STATE_BOTH,
2581 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 2,
2582 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2583 .access = PL2_RW,
2584 .readfn = ich_ap_read,
2585 .writefn = ich_ap_write,
2587 { .name = "ICH_AP1R3_EL2", .state = ARM_CP_STATE_BOTH,
2588 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 9, .opc2 = 3,
2589 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2590 .access = PL2_RW,
2591 .readfn = ich_ap_read,
2592 .writefn = ich_ap_write,
2594 REGINFO_SENTINEL
2597 static void gicv3_cpuif_el_change_hook(ARMCPU *cpu, void *opaque)
2599 GICv3CPUState *cs = opaque;
2601 gicv3_cpuif_update(cs);
2604 void gicv3_init_cpuif(GICv3State *s)
2606 /* Called from the GICv3 realize function; register our system
2607 * registers with the CPU
2609 int i;
2611 for (i = 0; i < s->num_cpu; i++) {
2612 ARMCPU *cpu = ARM_CPU(qemu_get_cpu(i));
2613 GICv3CPUState *cs = &s->cpu[i];
2615 /* Note that we can't just use the GICv3CPUState as an opaque pointer
2616 * in define_arm_cp_regs_with_opaque(), because when we're called back
2617 * it might be with code translated by CPU 0 but run by CPU 1, in
2618 * which case we'd get the wrong value.
2619 * So instead we define the regs with no ri->opaque info, and
2620 * get back to the GICv3CPUState from the CPUARMState.
2622 define_arm_cp_regs(cpu, gicv3_cpuif_reginfo);
2623 if (arm_feature(&cpu->env, ARM_FEATURE_EL2)
2624 && cpu->gic_num_lrs) {
2625 int j;
2627 cs->maintenance_irq = cpu->gicv3_maintenance_interrupt;
2629 cs->num_list_regs = cpu->gic_num_lrs;
2630 cs->vpribits = cpu->gic_vpribits;
2631 cs->vprebits = cpu->gic_vprebits;
2633 /* Check against architectural constraints: getting these
2634 * wrong would be a bug in the CPU code defining these,
2635 * and the implementation relies on them holding.
2637 g_assert(cs->vprebits <= cs->vpribits);
2638 g_assert(cs->vprebits >= 5 && cs->vprebits <= 7);
2639 g_assert(cs->vpribits >= 5 && cs->vpribits <= 8);
2641 define_arm_cp_regs(cpu, gicv3_cpuif_hcr_reginfo);
2643 for (j = 0; j < cs->num_list_regs; j++) {
2644 /* Note that the AArch64 LRs are 64-bit; the AArch32 LRs
2645 * are split into two cp15 regs, LR (the low part, with the
2646 * same encoding as the AArch64 LR) and LRC (the high part).
2648 ARMCPRegInfo lr_regset[] = {
2649 { .name = "ICH_LRn_EL2", .state = ARM_CP_STATE_BOTH,
2650 .opc0 = 3, .opc1 = 4, .crn = 12,
2651 .crm = 12 + (j >> 3), .opc2 = j & 7,
2652 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2653 .access = PL2_RW,
2654 .readfn = ich_lr_read,
2655 .writefn = ich_lr_write,
2657 { .name = "ICH_LRCn_EL2", .state = ARM_CP_STATE_AA32,
2658 .cp = 15, .opc1 = 4, .crn = 12,
2659 .crm = 14 + (j >> 3), .opc2 = j & 7,
2660 .type = ARM_CP_IO | ARM_CP_NO_RAW,
2661 .access = PL2_RW,
2662 .readfn = ich_lr_read,
2663 .writefn = ich_lr_write,
2665 REGINFO_SENTINEL
2667 define_arm_cp_regs(cpu, lr_regset);
2669 if (cs->vprebits >= 6) {
2670 define_arm_cp_regs(cpu, gicv3_cpuif_ich_apxr1_reginfo);
2672 if (cs->vprebits == 7) {
2673 define_arm_cp_regs(cpu, gicv3_cpuif_ich_apxr23_reginfo);
2676 arm_register_el_change_hook(cpu, gicv3_cpuif_el_change_hook, cs);