xref: /qemu/include/hw/intc/arm_gic_common.h (revision 83baec642a13a69398a2643a1f905606c13cd363)
183728796SAndreas Färber /*
283728796SAndreas Färber  * ARM GIC support
383728796SAndreas Färber  *
483728796SAndreas Färber  * Copyright (c) 2012 Linaro Limited
583728796SAndreas Färber  * Written by Peter Maydell
683728796SAndreas Färber  *
783728796SAndreas Färber  * This program is free software; you can redistribute it and/or modify
883728796SAndreas Färber  * it under the terms of the GNU General Public License as published by
983728796SAndreas Färber  * the Free Software Foundation, either version 2 of the License, or
1083728796SAndreas Färber  * (at your option) any later version.
1183728796SAndreas Färber  *
1283728796SAndreas Färber  * This program is distributed in the hope that it will be useful,
1383728796SAndreas Färber  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1483728796SAndreas Färber  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1583728796SAndreas Färber  * GNU General Public License for more details.
1683728796SAndreas Färber  *
1783728796SAndreas Färber  * You should have received a copy of the GNU General Public License along
1883728796SAndreas Färber  * with this program; if not, see <http://www.gnu.org/licenses/>.
1983728796SAndreas Färber  */
2083728796SAndreas Färber 
2183728796SAndreas Färber #ifndef HW_ARM_GIC_COMMON_H
2283728796SAndreas Färber #define HW_ARM_GIC_COMMON_H
2383728796SAndreas Färber 
2483728796SAndreas Färber #include "hw/sysbus.h"
25db1015e9SEduardo Habkost #include "qom/object.h"
2683728796SAndreas Färber 
2783728796SAndreas Färber /* Maximum number of possible interrupts, determined by the GIC architecture */
2883728796SAndreas Färber #define GIC_MAXIRQ 1020
2983728796SAndreas Färber /* First 32 are private to each CPU (SGIs and PPIs). */
3083728796SAndreas Färber #define GIC_INTERNAL 32
3141ab7b55SChristoffer Dall #define GIC_NR_SGIS 16
3283728796SAndreas Färber /* Maximum number of possible CPU interfaces, determined by GIC architecture */
3383728796SAndreas Färber #define GIC_NCPU 8
345773c049SLuc Michel /* Maximum number of possible CPU interfaces with their respective vCPU */
355773c049SLuc Michel #define GIC_NCPU_VCPU (GIC_NCPU * 2)
3683728796SAndreas Färber 
37a9d477c4SChristoffer Dall #define MAX_NR_GROUP_PRIO 128
38a9d477c4SChristoffer Dall #define GIC_NR_APRS (MAX_NR_GROUP_PRIO / 32)
39a9d477c4SChristoffer Dall 
40822e9cc3SFabian Aggeler #define GIC_MIN_BPR 0
41822e9cc3SFabian Aggeler #define GIC_MIN_ABPR (GIC_MIN_BPR + 1)
42822e9cc3SFabian Aggeler 
435773c049SLuc Michel /* Architectural maximum number of list registers in the virtual interface */
445773c049SLuc Michel #define GIC_MAX_LR 64
455773c049SLuc Michel 
465773c049SLuc Michel /* Only 32 priority levels and 32 preemption levels in the vCPU interfaces */
475773c049SLuc Michel #define GIC_VIRT_MAX_GROUP_PRIO_BITS 5
485773c049SLuc Michel #define GIC_VIRT_MAX_NR_GROUP_PRIO (1 << GIC_VIRT_MAX_GROUP_PRIO_BITS)
495773c049SLuc Michel #define GIC_VIRT_NR_APRS (GIC_VIRT_MAX_NR_GROUP_PRIO / 32)
505773c049SLuc Michel 
515773c049SLuc Michel #define GIC_VIRT_MIN_BPR 2
525773c049SLuc Michel #define GIC_VIRT_MIN_ABPR (GIC_VIRT_MIN_BPR + 1)
535773c049SLuc Michel 
5483728796SAndreas Färber typedef struct gic_irq_state {
5583728796SAndreas Färber     /* The enable bits are only banked for per-cpu interrupts.  */
5683728796SAndreas Färber     uint8_t enabled;
5783728796SAndreas Färber     uint8_t pending;
5883728796SAndreas Färber     uint8_t active;
5983728796SAndreas Färber     uint8_t level;
6083728796SAndreas Färber     bool model; /* 0 = N:N, 1 = 1:N */
6104050c5cSChristoffer Dall     bool edge_trigger; /* true: edge-triggered, false: level-triggered  */
62c27a5ba9SFabian Aggeler     uint8_t group;
6383728796SAndreas Färber } gic_irq_state;
6483728796SAndreas Färber 
65db1015e9SEduardo Habkost struct GICState {
6683728796SAndreas Färber     /*< private >*/
6783728796SAndreas Färber     SysBusDevice parent_obj;
6883728796SAndreas Färber     /*< public >*/
6983728796SAndreas Färber 
7083728796SAndreas Färber     qemu_irq parent_irq[GIC_NCPU];
7144f55296SFabian Aggeler     qemu_irq parent_fiq[GIC_NCPU];
726a228959SPeter Maydell     qemu_irq parent_virq[GIC_NCPU];
736a228959SPeter Maydell     qemu_irq parent_vfiq[GIC_NCPU];
74*83f32075SJinjie Ruan     qemu_irq parent_nmi[GIC_NCPU];
75*83f32075SJinjie Ruan     qemu_irq parent_vnmi[GIC_NCPU];
765773c049SLuc Michel     qemu_irq maintenance_irq[GIC_NCPU];
775773c049SLuc Michel 
78679aa175SFabian Aggeler     /* GICD_CTLR; for a GIC with the security extensions the NS banked version
79679aa175SFabian Aggeler      * of this register is just an alias of bit 1 of the S banked version.
80679aa175SFabian Aggeler      */
81679aa175SFabian Aggeler     uint32_t ctlr;
8232951860SFabian Aggeler     /* GICC_CTLR; again, the NS banked version is just aliases of bits of
8332951860SFabian Aggeler      * the S banked register, so our state only needs to store the S version.
8432951860SFabian Aggeler      */
855773c049SLuc Michel     uint32_t cpu_ctlr[GIC_NCPU_VCPU];
8683728796SAndreas Färber 
8783728796SAndreas Färber     gic_irq_state irq_state[GIC_MAXIRQ];
8883728796SAndreas Färber     uint8_t irq_target[GIC_MAXIRQ];
8983728796SAndreas Färber     uint8_t priority1[GIC_INTERNAL][GIC_NCPU];
9083728796SAndreas Färber     uint8_t priority2[GIC_MAXIRQ - GIC_INTERNAL];
9140d22500SChristoffer Dall     /* For each SGI on the target CPU, we store 8 bits
9240d22500SChristoffer Dall      * indicating which source CPUs have made this SGI
9340d22500SChristoffer Dall      * pending on the target CPU. These correspond to
9440d22500SChristoffer Dall      * the bytes in the GIC_SPENDSGIR* registers as
9540d22500SChristoffer Dall      * read by the target CPU.
9640d22500SChristoffer Dall      */
9740d22500SChristoffer Dall     uint8_t sgi_pending[GIC_NR_SGIS][GIC_NCPU];
9883728796SAndreas Färber 
995773c049SLuc Michel     uint16_t priority_mask[GIC_NCPU_VCPU];
1005773c049SLuc Michel     uint16_t running_priority[GIC_NCPU_VCPU];
1015773c049SLuc Michel     uint16_t current_pending[GIC_NCPU_VCPU];
10211411489SSai Pavan Boddu     uint32_t n_prio_bits;
10383728796SAndreas Färber 
104822e9cc3SFabian Aggeler     /* If we present the GICv2 without security extensions to a guest,
105822e9cc3SFabian Aggeler      * the guest can configure the GICC_CTLR to configure group 1 binary point
106822e9cc3SFabian Aggeler      * in the abpr.
107822e9cc3SFabian Aggeler      * For a GIC with Security Extensions we use use bpr for the
108822e9cc3SFabian Aggeler      * secure copy and abpr as storage for the non-secure copy of the register.
109aa7d461aSChristoffer Dall      */
1105773c049SLuc Michel     uint8_t  bpr[GIC_NCPU_VCPU];
1115773c049SLuc Michel     uint8_t  abpr[GIC_NCPU_VCPU];
112aa7d461aSChristoffer Dall 
113a9d477c4SChristoffer Dall     /* The APR is implementation defined, so we choose a layout identical to
114a9d477c4SChristoffer Dall      * the KVM ABI layout for QEMU's implementation of the gic:
115a9d477c4SChristoffer Dall      * If an interrupt for preemption level X is active, then
116a9d477c4SChristoffer Dall      *   APRn[X mod 32] == 0b1,  where n = X / 32
117a9d477c4SChristoffer Dall      * otherwise the bit is clear.
118a9d477c4SChristoffer Dall      */
119a9d477c4SChristoffer Dall     uint32_t apr[GIC_NR_APRS][GIC_NCPU];
12051fd06e0SPeter Maydell     uint32_t nsapr[GIC_NR_APRS][GIC_NCPU];
121a9d477c4SChristoffer Dall 
1225773c049SLuc Michel     /* Virtual interface control registers */
1235773c049SLuc Michel     uint32_t h_hcr[GIC_NCPU];
1245773c049SLuc Michel     uint32_t h_misr[GIC_NCPU];
1255773c049SLuc Michel     uint32_t h_lr[GIC_MAX_LR][GIC_NCPU];
1265773c049SLuc Michel     uint32_t h_apr[GIC_NCPU];
1275773c049SLuc Michel 
1285773c049SLuc Michel     /* Number of LRs implemented in this GIC instance */
1295773c049SLuc Michel     uint32_t num_lrs;
1305773c049SLuc Michel 
13183728796SAndreas Färber     uint32_t num_cpu;
13283728796SAndreas Färber 
13383728796SAndreas Färber     MemoryRegion iomem; /* Distributor */
13483728796SAndreas Färber     /* This is just so we can have an opaque pointer which identifies
13583728796SAndreas Färber      * both this GIC and which CPU interface we should be accessing.
13683728796SAndreas Färber      */
13783728796SAndreas Färber     struct GICState *backref[GIC_NCPU];
13883728796SAndreas Färber     MemoryRegion cpuiomem[GIC_NCPU + 1]; /* CPU interfaces */
1395773c049SLuc Michel     MemoryRegion vifaceiomem[GIC_NCPU + 1]; /* Virtual interfaces */
1405773c049SLuc Michel     MemoryRegion vcpuiomem; /* vCPU interface */
1415773c049SLuc Michel 
14283728796SAndreas Färber     uint32_t num_irq;
14383728796SAndreas Färber     uint32_t revision;
1445543d1abSFabian Aggeler     bool security_extn;
1455773c049SLuc Michel     bool virt_extn;
1468ff41f39SPeter Maydell     bool irq_reset_nonsecure; /* configure IRQs as group 1 (NS) on reset? */
1471da41cc1SChristoffer Dall     int dev_fd; /* kvm device fd if backed by kvm vgic support */
14824182fbcSPavel Fedin     Error *migration_blocker;
149db1015e9SEduardo Habkost };
150db1015e9SEduardo Habkost typedef struct GICState GICState;
15183728796SAndreas Färber 
15283728796SAndreas Färber #define TYPE_ARM_GIC_COMMON "arm_gic_common"
153db1015e9SEduardo Habkost typedef struct ARMGICCommonClass ARMGICCommonClass;
1548110fa1dSEduardo Habkost DECLARE_OBJ_CHECKERS(GICState, ARMGICCommonClass,
1558110fa1dSEduardo Habkost                      ARM_GIC_COMMON, TYPE_ARM_GIC_COMMON)
15683728796SAndreas Färber 
157db1015e9SEduardo Habkost struct ARMGICCommonClass {
15883728796SAndreas Färber     /*< private >*/
15983728796SAndreas Färber     SysBusDeviceClass parent_class;
16083728796SAndreas Färber     /*< public >*/
16183728796SAndreas Färber 
16283728796SAndreas Färber     void (*pre_save)(GICState *s);
16383728796SAndreas Färber     void (*post_load)(GICState *s);
164db1015e9SEduardo Habkost };
16583728796SAndreas Färber 
1667926c210SPavel Fedin void gic_init_irqs_and_mmio(GICState *s, qemu_irq_handler handler,
1675773c049SLuc Michel                             const MemoryRegionOps *ops,
1685773c049SLuc Michel                             const MemoryRegionOps *virt_ops);
1697926c210SPavel Fedin 
17083728796SAndreas Färber #endif
171