xref: /qemu/include/hw/intc/arm_gicv3_common.h (revision 3faf2b0cd5451c452fdaab32f9d2fb870b084f80)
1ff8f06eeSShlomo Pongratz /*
2ff8f06eeSShlomo Pongratz  * ARM GIC support
3ff8f06eeSShlomo Pongratz  *
4ff8f06eeSShlomo Pongratz  * Copyright (c) 2012 Linaro Limited
5ff8f06eeSShlomo Pongratz  * Copyright (c) 2015 Huawei.
607e2034dSPavel Fedin  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7ff8f06eeSShlomo Pongratz  * Written by Peter Maydell
807e2034dSPavel Fedin  * Reworked for GICv3 by Shlomo Pongratz and Pavel Fedin
9ff8f06eeSShlomo Pongratz  *
10ff8f06eeSShlomo Pongratz  * This program is free software; you can redistribute it and/or modify
11ff8f06eeSShlomo Pongratz  * it under the terms of the GNU General Public License as published by
12ff8f06eeSShlomo Pongratz  * the Free Software Foundation, either version 2 of the License, or
13ff8f06eeSShlomo Pongratz  * (at your option) any later version.
14ff8f06eeSShlomo Pongratz  *
15ff8f06eeSShlomo Pongratz  * This program is distributed in the hope that it will be useful,
16ff8f06eeSShlomo Pongratz  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17ff8f06eeSShlomo Pongratz  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18ff8f06eeSShlomo Pongratz  * GNU General Public License for more details.
19ff8f06eeSShlomo Pongratz  *
20ff8f06eeSShlomo Pongratz  * You should have received a copy of the GNU General Public License along
21ff8f06eeSShlomo Pongratz  * with this program; if not, see <http://www.gnu.org/licenses/>.
22ff8f06eeSShlomo Pongratz  */
23ff8f06eeSShlomo Pongratz 
24ff8f06eeSShlomo Pongratz #ifndef HW_ARM_GICV3_COMMON_H
25ff8f06eeSShlomo Pongratz #define HW_ARM_GICV3_COMMON_H
26ff8f06eeSShlomo Pongratz 
27ff8f06eeSShlomo Pongratz #include "hw/sysbus.h"
28ff8f06eeSShlomo Pongratz #include "hw/intc/arm_gic_common.h"
29ff8f06eeSShlomo Pongratz 
3007e2034dSPavel Fedin /*
3107e2034dSPavel Fedin  * Maximum number of possible interrupts, determined by the GIC architecture.
3207e2034dSPavel Fedin  * Note that this does not include LPIs. When implemented, these should be
3307e2034dSPavel Fedin  * dealt with separately.
3407e2034dSPavel Fedin  */
3507e2034dSPavel Fedin #define GICV3_MAXIRQ 1020
3607e2034dSPavel Fedin #define GICV3_MAXSPI (GICV3_MAXIRQ - GIC_INTERNAL)
3707e2034dSPavel Fedin 
3807e2034dSPavel Fedin /* Minimum BPR for Secure, or when security not enabled */
3907e2034dSPavel Fedin #define GIC_MIN_BPR 0
4007e2034dSPavel Fedin /* Minimum BPR for Nonsecure when security is enabled */
4107e2034dSPavel Fedin #define GIC_MIN_BPR_NS (GIC_MIN_BPR + 1)
4207e2034dSPavel Fedin 
4307e2034dSPavel Fedin /* For some distributor fields we want to model the array of 32-bit
4407e2034dSPavel Fedin  * register values which hold various bitmaps corresponding to enabled,
4507e2034dSPavel Fedin  * pending, etc bits. These macros and functions facilitate that; the
4607e2034dSPavel Fedin  * APIs are generally modelled on the generic bitmap.h functions
4707e2034dSPavel Fedin  * (which are unsuitable here because they use 'unsigned long' as the
4807e2034dSPavel Fedin  * underlying storage type, which is very awkward when you need to
4907e2034dSPavel Fedin  * access the data as 32-bit values.)
5007e2034dSPavel Fedin  * Each bitmap contains a bit for each interrupt. Although there is
5107e2034dSPavel Fedin  * space for the PPIs and SGIs, those bits (the first 32) are never
5207e2034dSPavel Fedin  * used as that state lives in the redistributor. The unused bits are
5307e2034dSPavel Fedin  * provided purely so that interrupt X's state is always in bit X; this
5407e2034dSPavel Fedin  * avoids bugs where we forget to subtract GIC_INTERNAL from an
5507e2034dSPavel Fedin  * interrupt number.
5607e2034dSPavel Fedin  */
5707e2034dSPavel Fedin #define GICV3_BMP_SIZE (DIV_ROUND_UP(GICV3_MAXIRQ, 32))
5807e2034dSPavel Fedin 
5907e2034dSPavel Fedin #define GIC_DECLARE_BITMAP(name) \
6007e2034dSPavel Fedin     uint32_t name[GICV3_BMP_SIZE]
6107e2034dSPavel Fedin 
6207e2034dSPavel Fedin #define GIC_BIT_MASK(nr) (1U << ((nr) % 32))
6307e2034dSPavel Fedin #define GIC_BIT_WORD(nr) ((nr) / 32)
6407e2034dSPavel Fedin 
6507e2034dSPavel Fedin static inline void gic_bmp_set_bit(int nr, uint32_t *addr)
6607e2034dSPavel Fedin {
6707e2034dSPavel Fedin     uint32_t mask = GIC_BIT_MASK(nr);
6807e2034dSPavel Fedin     uint32_t *p = addr + GIC_BIT_WORD(nr);
6907e2034dSPavel Fedin 
7007e2034dSPavel Fedin     *p |= mask;
7107e2034dSPavel Fedin }
7207e2034dSPavel Fedin 
7307e2034dSPavel Fedin static inline void gic_bmp_clear_bit(int nr, uint32_t *addr)
7407e2034dSPavel Fedin {
7507e2034dSPavel Fedin     uint32_t mask = GIC_BIT_MASK(nr);
7607e2034dSPavel Fedin     uint32_t *p = addr + GIC_BIT_WORD(nr);
7707e2034dSPavel Fedin 
7807e2034dSPavel Fedin     *p &= ~mask;
7907e2034dSPavel Fedin }
8007e2034dSPavel Fedin 
8107e2034dSPavel Fedin static inline int gic_bmp_test_bit(int nr, const uint32_t *addr)
8207e2034dSPavel Fedin {
8307e2034dSPavel Fedin     return 1U & (addr[GIC_BIT_WORD(nr)] >> (nr & 31));
8407e2034dSPavel Fedin }
8507e2034dSPavel Fedin 
8607e2034dSPavel Fedin static inline void gic_bmp_replace_bit(int nr, uint32_t *addr, int val)
8707e2034dSPavel Fedin {
8807e2034dSPavel Fedin     uint32_t mask = GIC_BIT_MASK(nr);
8907e2034dSPavel Fedin     uint32_t *p = addr + GIC_BIT_WORD(nr);
9007e2034dSPavel Fedin 
9107e2034dSPavel Fedin     *p &= ~mask;
9207e2034dSPavel Fedin     *p |= (val & 1U) << (nr % 32);
9307e2034dSPavel Fedin }
9407e2034dSPavel Fedin 
9507e2034dSPavel Fedin /* Return a pointer to the 32-bit word containing the specified bit. */
9607e2034dSPavel Fedin static inline uint32_t *gic_bmp_ptr32(uint32_t *addr, int nr)
9707e2034dSPavel Fedin {
9807e2034dSPavel Fedin     return addr + GIC_BIT_WORD(nr);
9907e2034dSPavel Fedin }
10007e2034dSPavel Fedin 
10107e2034dSPavel Fedin typedef struct GICv3State GICv3State;
10207e2034dSPavel Fedin typedef struct GICv3CPUState GICv3CPUState;
10307e2034dSPavel Fedin 
10407e2034dSPavel Fedin /* Some CPU interface registers come in three flavours:
10507e2034dSPavel Fedin  * Group0, Group1 (Secure) and Group1 (NonSecure)
10607e2034dSPavel Fedin  * (where the latter two are exposed as a single banked system register).
10707e2034dSPavel Fedin  * In the state struct they are implemented as a 3-element array which
10807e2034dSPavel Fedin  * can be indexed into by the GICV3_G0, GICV3_G1 and GICV3_G1NS constants.
10907e2034dSPavel Fedin  * If the CPU doesn't support EL3 then the G1 element is unused.
11007e2034dSPavel Fedin  *
11107e2034dSPavel Fedin  * These constants are also used to communicate the group to use for
11207e2034dSPavel Fedin  * an interrupt or SGI when it is passed between the cpu interface and
11307e2034dSPavel Fedin  * the redistributor or distributor. For those purposes the receiving end
11407e2034dSPavel Fedin  * must be prepared to cope with a Group 1 Secure interrupt even if it does
11507e2034dSPavel Fedin  * not have security support enabled, because security can be disabled
11607e2034dSPavel Fedin  * independently in the CPU and in the GIC. In that case the receiver should
11707e2034dSPavel Fedin  * treat an incoming Group 1 Secure interrupt as if it were Group 0.
11807e2034dSPavel Fedin  * (This architectural requirement is why the _G1 element is the unused one
11907e2034dSPavel Fedin  * in a no-EL3 CPU:  we would otherwise have to translate back and forth
12007e2034dSPavel Fedin  * between (G0, G1NS) from the distributor and (G0, G1) in the CPU i/f.)
12107e2034dSPavel Fedin  */
12207e2034dSPavel Fedin #define GICV3_G0 0
12307e2034dSPavel Fedin #define GICV3_G1 1
12407e2034dSPavel Fedin #define GICV3_G1NS 2
12507e2034dSPavel Fedin 
12607e2034dSPavel Fedin /* ICC_CTLR_EL1, GICD_STATUSR and GICR_STATUSR are banked but not
12707e2034dSPavel Fedin  * group-related, so those indices are just 0 for S and 1 for NS.
12807e2034dSPavel Fedin  * (If the CPU or the GIC, respectively, don't support the Security
12907e2034dSPavel Fedin  * extensions then the S element is unused.)
13007e2034dSPavel Fedin  */
13107e2034dSPavel Fedin #define GICV3_S 0
13207e2034dSPavel Fedin #define GICV3_NS 1
13307e2034dSPavel Fedin 
13407e2034dSPavel Fedin struct GICv3CPUState {
13507e2034dSPavel Fedin     GICv3State *gic;
13607e2034dSPavel Fedin     CPUState *cpu;
137*3faf2b0cSPeter Maydell     qemu_irq parent_irq;
138*3faf2b0cSPeter Maydell     qemu_irq parent_fiq;
13907e2034dSPavel Fedin 
14007e2034dSPavel Fedin     /* Redistributor */
14107e2034dSPavel Fedin     uint32_t level;                  /* Current IRQ level */
14207e2034dSPavel Fedin     /* RD_base page registers */
14307e2034dSPavel Fedin     uint32_t gicr_ctlr;
14407e2034dSPavel Fedin     uint64_t gicr_typer;
14507e2034dSPavel Fedin     uint32_t gicr_statusr[2];
14607e2034dSPavel Fedin     uint32_t gicr_waker;
14707e2034dSPavel Fedin     uint64_t gicr_propbaser;
14807e2034dSPavel Fedin     uint64_t gicr_pendbaser;
14907e2034dSPavel Fedin     /* SGI_base page registers */
15007e2034dSPavel Fedin     uint32_t gicr_igroupr0;
15107e2034dSPavel Fedin     uint32_t gicr_ienabler0;
15207e2034dSPavel Fedin     uint32_t gicr_ipendr0;
15307e2034dSPavel Fedin     uint32_t gicr_iactiver0;
15407e2034dSPavel Fedin     uint32_t edge_trigger; /* ICFGR0 and ICFGR1 even bits */
15507e2034dSPavel Fedin     uint32_t gicr_igrpmodr0;
15607e2034dSPavel Fedin     uint32_t gicr_nsacr;
15707e2034dSPavel Fedin     uint8_t gicr_ipriorityr[GIC_INTERNAL];
15807e2034dSPavel Fedin 
15907e2034dSPavel Fedin     /* CPU interface */
16007e2034dSPavel Fedin     uint64_t icc_ctlr_el1[2];
16107e2034dSPavel Fedin     uint64_t icc_pmr_el1;
16207e2034dSPavel Fedin     uint64_t icc_bpr[3];
16307e2034dSPavel Fedin     uint64_t icc_apr[3][4];
16407e2034dSPavel Fedin     uint64_t icc_igrpen[3];
16507e2034dSPavel Fedin     uint64_t icc_ctlr_el3;
16607e2034dSPavel Fedin };
16707e2034dSPavel Fedin 
16807e2034dSPavel Fedin struct GICv3State {
169ff8f06eeSShlomo Pongratz     /*< private >*/
170ff8f06eeSShlomo Pongratz     SysBusDevice parent_obj;
171ff8f06eeSShlomo Pongratz     /*< public >*/
172ff8f06eeSShlomo Pongratz 
173ff8f06eeSShlomo Pongratz     MemoryRegion iomem_dist; /* Distributor */
174ff8f06eeSShlomo Pongratz     MemoryRegion iomem_redist; /* Redistributors */
175ff8f06eeSShlomo Pongratz 
176ff8f06eeSShlomo Pongratz     uint32_t num_cpu;
177ff8f06eeSShlomo Pongratz     uint32_t num_irq;
178ff8f06eeSShlomo Pongratz     uint32_t revision;
179ff8f06eeSShlomo Pongratz     bool security_extn;
18007e2034dSPavel Fedin     bool irq_reset_nonsecure;
181ff8f06eeSShlomo Pongratz 
182ff8f06eeSShlomo Pongratz     int dev_fd; /* kvm device fd if backed by kvm vgic support */
18307e2034dSPavel Fedin     Error *migration_blocker;
18407e2034dSPavel Fedin 
18507e2034dSPavel Fedin     /* Distributor */
18607e2034dSPavel Fedin 
18707e2034dSPavel Fedin     /* for a GIC with the security extensions the NS banked version of this
18807e2034dSPavel Fedin      * register is just an alias of bit 1 of the S banked version.
18907e2034dSPavel Fedin      */
19007e2034dSPavel Fedin     uint32_t gicd_ctlr;
19107e2034dSPavel Fedin     uint32_t gicd_statusr[2];
19207e2034dSPavel Fedin     GIC_DECLARE_BITMAP(group);        /* GICD_IGROUPR */
19307e2034dSPavel Fedin     GIC_DECLARE_BITMAP(grpmod);       /* GICD_IGRPMODR */
19407e2034dSPavel Fedin     GIC_DECLARE_BITMAP(enabled);      /* GICD_ISENABLER */
19507e2034dSPavel Fedin     GIC_DECLARE_BITMAP(pending);      /* GICD_ISPENDR */
19607e2034dSPavel Fedin     GIC_DECLARE_BITMAP(active);       /* GICD_ISACTIVER */
19707e2034dSPavel Fedin     GIC_DECLARE_BITMAP(level);        /* Current level */
19807e2034dSPavel Fedin     GIC_DECLARE_BITMAP(edge_trigger); /* GICD_ICFGR even bits */
19907e2034dSPavel Fedin     uint8_t gicd_ipriority[GICV3_MAXIRQ];
20007e2034dSPavel Fedin     uint64_t gicd_irouter[GICV3_MAXIRQ];
20107e2034dSPavel Fedin     uint32_t gicd_nsacr[DIV_ROUND_UP(GICV3_MAXIRQ, 16)];
20207e2034dSPavel Fedin 
20307e2034dSPavel Fedin     GICv3CPUState *cpu;
20407e2034dSPavel Fedin };
20507e2034dSPavel Fedin 
20607e2034dSPavel Fedin #define GICV3_BITMAP_ACCESSORS(BMP)                                     \
20707e2034dSPavel Fedin     static inline void gicv3_gicd_##BMP##_set(GICv3State *s, int irq)   \
20807e2034dSPavel Fedin     {                                                                   \
20907e2034dSPavel Fedin         gic_bmp_set_bit(irq, s->BMP);                                   \
21007e2034dSPavel Fedin     }                                                                   \
21107e2034dSPavel Fedin     static inline int gicv3_gicd_##BMP##_test(GICv3State *s, int irq)   \
21207e2034dSPavel Fedin     {                                                                   \
21307e2034dSPavel Fedin         return gic_bmp_test_bit(irq, s->BMP);                           \
21407e2034dSPavel Fedin     }                                                                   \
21507e2034dSPavel Fedin     static inline void gicv3_gicd_##BMP##_clear(GICv3State *s, int irq) \
21607e2034dSPavel Fedin     {                                                                   \
21707e2034dSPavel Fedin         gic_bmp_clear_bit(irq, s->BMP);                                 \
21807e2034dSPavel Fedin     }                                                                   \
21907e2034dSPavel Fedin     static inline void gicv3_gicd_##BMP##_replace(GICv3State *s,        \
22007e2034dSPavel Fedin                                                   int irq, int value)   \
22107e2034dSPavel Fedin     {                                                                   \
22207e2034dSPavel Fedin         gic_bmp_replace_bit(irq, s->BMP, value);                        \
22307e2034dSPavel Fedin     }
22407e2034dSPavel Fedin 
22507e2034dSPavel Fedin GICV3_BITMAP_ACCESSORS(group)
22607e2034dSPavel Fedin GICV3_BITMAP_ACCESSORS(grpmod)
22707e2034dSPavel Fedin GICV3_BITMAP_ACCESSORS(enabled)
22807e2034dSPavel Fedin GICV3_BITMAP_ACCESSORS(pending)
22907e2034dSPavel Fedin GICV3_BITMAP_ACCESSORS(active)
23007e2034dSPavel Fedin GICV3_BITMAP_ACCESSORS(level)
23107e2034dSPavel Fedin GICV3_BITMAP_ACCESSORS(edge_trigger)
232ff8f06eeSShlomo Pongratz 
233ff8f06eeSShlomo Pongratz #define TYPE_ARM_GICV3_COMMON "arm-gicv3-common"
234ff8f06eeSShlomo Pongratz #define ARM_GICV3_COMMON(obj) \
235ff8f06eeSShlomo Pongratz      OBJECT_CHECK(GICv3State, (obj), TYPE_ARM_GICV3_COMMON)
236ff8f06eeSShlomo Pongratz #define ARM_GICV3_COMMON_CLASS(klass) \
237ff8f06eeSShlomo Pongratz      OBJECT_CLASS_CHECK(ARMGICv3CommonClass, (klass), TYPE_ARM_GICV3_COMMON)
238ff8f06eeSShlomo Pongratz #define ARM_GICV3_COMMON_GET_CLASS(obj) \
239ff8f06eeSShlomo Pongratz      OBJECT_GET_CLASS(ARMGICv3CommonClass, (obj), TYPE_ARM_GICV3_COMMON)
240ff8f06eeSShlomo Pongratz 
241ff8f06eeSShlomo Pongratz typedef struct ARMGICv3CommonClass {
242ff8f06eeSShlomo Pongratz     /*< private >*/
243ff8f06eeSShlomo Pongratz     SysBusDeviceClass parent_class;
244ff8f06eeSShlomo Pongratz     /*< public >*/
245ff8f06eeSShlomo Pongratz 
246ff8f06eeSShlomo Pongratz     void (*pre_save)(GICv3State *s);
247ff8f06eeSShlomo Pongratz     void (*post_load)(GICv3State *s);
248ff8f06eeSShlomo Pongratz } ARMGICv3CommonClass;
249ff8f06eeSShlomo Pongratz 
250ff8f06eeSShlomo Pongratz void gicv3_init_irqs_and_mmio(GICv3State *s, qemu_irq_handler handler,
251ff8f06eeSShlomo Pongratz                               const MemoryRegionOps *ops);
252ff8f06eeSShlomo Pongratz 
253ff8f06eeSShlomo Pongratz #endif
254