xref: /qemu/include/hw/intc/arm_gic_common.h (revision db1015e92e04835c9eb50c29625fe566d1202dbd)
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"
25*db1015e9SEduardo 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 
65*db1015e9SEduardo 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];
745773c049SLuc Michel     qemu_irq maintenance_irq[GIC_NCPU];
755773c049SLuc Michel 
76679aa175SFabian Aggeler     /* GICD_CTLR; for a GIC with the security extensions the NS banked version
77679aa175SFabian Aggeler      * of this register is just an alias of bit 1 of the S banked version.
78679aa175SFabian Aggeler      */
79679aa175SFabian Aggeler     uint32_t ctlr;
8032951860SFabian Aggeler     /* GICC_CTLR; again, the NS banked version is just aliases of bits of
8132951860SFabian Aggeler      * the S banked register, so our state only needs to store the S version.
8232951860SFabian Aggeler      */
835773c049SLuc Michel     uint32_t cpu_ctlr[GIC_NCPU_VCPU];
8483728796SAndreas Färber 
8583728796SAndreas Färber     gic_irq_state irq_state[GIC_MAXIRQ];
8683728796SAndreas Färber     uint8_t irq_target[GIC_MAXIRQ];
8783728796SAndreas Färber     uint8_t priority1[GIC_INTERNAL][GIC_NCPU];
8883728796SAndreas Färber     uint8_t priority2[GIC_MAXIRQ - GIC_INTERNAL];
8940d22500SChristoffer Dall     /* For each SGI on the target CPU, we store 8 bits
9040d22500SChristoffer Dall      * indicating which source CPUs have made this SGI
9140d22500SChristoffer Dall      * pending on the target CPU. These correspond to
9240d22500SChristoffer Dall      * the bytes in the GIC_SPENDSGIR* registers as
9340d22500SChristoffer Dall      * read by the target CPU.
9440d22500SChristoffer Dall      */
9540d22500SChristoffer Dall     uint8_t sgi_pending[GIC_NR_SGIS][GIC_NCPU];
9683728796SAndreas Färber 
975773c049SLuc Michel     uint16_t priority_mask[GIC_NCPU_VCPU];
985773c049SLuc Michel     uint16_t running_priority[GIC_NCPU_VCPU];
995773c049SLuc Michel     uint16_t current_pending[GIC_NCPU_VCPU];
10011411489SSai Pavan Boddu     uint32_t n_prio_bits;
10183728796SAndreas Färber 
102822e9cc3SFabian Aggeler     /* If we present the GICv2 without security extensions to a guest,
103822e9cc3SFabian Aggeler      * the guest can configure the GICC_CTLR to configure group 1 binary point
104822e9cc3SFabian Aggeler      * in the abpr.
105822e9cc3SFabian Aggeler      * For a GIC with Security Extensions we use use bpr for the
106822e9cc3SFabian Aggeler      * secure copy and abpr as storage for the non-secure copy of the register.
107aa7d461aSChristoffer Dall      */
1085773c049SLuc Michel     uint8_t  bpr[GIC_NCPU_VCPU];
1095773c049SLuc Michel     uint8_t  abpr[GIC_NCPU_VCPU];
110aa7d461aSChristoffer Dall 
111a9d477c4SChristoffer Dall     /* The APR is implementation defined, so we choose a layout identical to
112a9d477c4SChristoffer Dall      * the KVM ABI layout for QEMU's implementation of the gic:
113a9d477c4SChristoffer Dall      * If an interrupt for preemption level X is active, then
114a9d477c4SChristoffer Dall      *   APRn[X mod 32] == 0b1,  where n = X / 32
115a9d477c4SChristoffer Dall      * otherwise the bit is clear.
116a9d477c4SChristoffer Dall      */
117a9d477c4SChristoffer Dall     uint32_t apr[GIC_NR_APRS][GIC_NCPU];
11851fd06e0SPeter Maydell     uint32_t nsapr[GIC_NR_APRS][GIC_NCPU];
119a9d477c4SChristoffer Dall 
1205773c049SLuc Michel     /* Virtual interface control registers */
1215773c049SLuc Michel     uint32_t h_hcr[GIC_NCPU];
1225773c049SLuc Michel     uint32_t h_misr[GIC_NCPU];
1235773c049SLuc Michel     uint32_t h_lr[GIC_MAX_LR][GIC_NCPU];
1245773c049SLuc Michel     uint32_t h_apr[GIC_NCPU];
1255773c049SLuc Michel 
1265773c049SLuc Michel     /* Number of LRs implemented in this GIC instance */
1275773c049SLuc Michel     uint32_t num_lrs;
1285773c049SLuc Michel 
12983728796SAndreas Färber     uint32_t num_cpu;
13083728796SAndreas Färber 
13183728796SAndreas Färber     MemoryRegion iomem; /* Distributor */
13283728796SAndreas Färber     /* This is just so we can have an opaque pointer which identifies
13383728796SAndreas Färber      * both this GIC and which CPU interface we should be accessing.
13483728796SAndreas Färber      */
13583728796SAndreas Färber     struct GICState *backref[GIC_NCPU];
13683728796SAndreas Färber     MemoryRegion cpuiomem[GIC_NCPU + 1]; /* CPU interfaces */
1375773c049SLuc Michel     MemoryRegion vifaceiomem[GIC_NCPU + 1]; /* Virtual interfaces */
1385773c049SLuc Michel     MemoryRegion vcpuiomem; /* vCPU interface */
1395773c049SLuc Michel 
14083728796SAndreas Färber     uint32_t num_irq;
14183728796SAndreas Färber     uint32_t revision;
1425543d1abSFabian Aggeler     bool security_extn;
1435773c049SLuc Michel     bool virt_extn;
1448ff41f39SPeter Maydell     bool irq_reset_nonsecure; /* configure IRQs as group 1 (NS) on reset? */
1451da41cc1SChristoffer Dall     int dev_fd; /* kvm device fd if backed by kvm vgic support */
14624182fbcSPavel Fedin     Error *migration_blocker;
147*db1015e9SEduardo Habkost };
148*db1015e9SEduardo Habkost typedef struct GICState GICState;
14983728796SAndreas Färber 
15083728796SAndreas Färber #define TYPE_ARM_GIC_COMMON "arm_gic_common"
151*db1015e9SEduardo Habkost typedef struct ARMGICCommonClass ARMGICCommonClass;
15283728796SAndreas Färber #define ARM_GIC_COMMON(obj) \
15383728796SAndreas Färber      OBJECT_CHECK(GICState, (obj), TYPE_ARM_GIC_COMMON)
15483728796SAndreas Färber #define ARM_GIC_COMMON_CLASS(klass) \
15583728796SAndreas Färber      OBJECT_CLASS_CHECK(ARMGICCommonClass, (klass), TYPE_ARM_GIC_COMMON)
15683728796SAndreas Färber #define ARM_GIC_COMMON_GET_CLASS(obj) \
15783728796SAndreas Färber      OBJECT_GET_CLASS(ARMGICCommonClass, (obj), TYPE_ARM_GIC_COMMON)
15883728796SAndreas Färber 
159*db1015e9SEduardo Habkost struct ARMGICCommonClass {
16083728796SAndreas Färber     /*< private >*/
16183728796SAndreas Färber     SysBusDeviceClass parent_class;
16283728796SAndreas Färber     /*< public >*/
16383728796SAndreas Färber 
16483728796SAndreas Färber     void (*pre_save)(GICState *s);
16583728796SAndreas Färber     void (*post_load)(GICState *s);
166*db1015e9SEduardo Habkost };
16783728796SAndreas Färber 
1687926c210SPavel Fedin void gic_init_irqs_and_mmio(GICState *s, qemu_irq_handler handler,
1695773c049SLuc Michel                             const MemoryRegionOps *ops,
1705773c049SLuc Michel                             const MemoryRegionOps *virt_ops);
1717926c210SPavel Fedin 
17283728796SAndreas Färber #endif
173