xref: /qemu/include/hw/intc/arm_gicv3_common.h (revision 07e2034d0817b8006ae4eff07d9d67169d52855a)
1ff8f06eeSShlomo Pongratz /*
2ff8f06eeSShlomo Pongratz  * ARM GIC support
3ff8f06eeSShlomo Pongratz  *
4ff8f06eeSShlomo Pongratz  * Copyright (c) 2012 Linaro Limited
5ff8f06eeSShlomo Pongratz  * Copyright (c) 2015 Huawei.
6*07e2034dSPavel Fedin  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7ff8f06eeSShlomo Pongratz  * Written by Peter Maydell
8*07e2034dSPavel 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 
30*07e2034dSPavel Fedin /*
31*07e2034dSPavel Fedin  * Maximum number of possible interrupts, determined by the GIC architecture.
32*07e2034dSPavel Fedin  * Note that this does not include LPIs. When implemented, these should be
33*07e2034dSPavel Fedin  * dealt with separately.
34*07e2034dSPavel Fedin  */
35*07e2034dSPavel Fedin #define GICV3_MAXIRQ 1020
36*07e2034dSPavel Fedin #define GICV3_MAXSPI (GICV3_MAXIRQ - GIC_INTERNAL)
37*07e2034dSPavel Fedin 
38*07e2034dSPavel Fedin /* Minimum BPR for Secure, or when security not enabled */
39*07e2034dSPavel Fedin #define GIC_MIN_BPR 0
40*07e2034dSPavel Fedin /* Minimum BPR for Nonsecure when security is enabled */
41*07e2034dSPavel Fedin #define GIC_MIN_BPR_NS (GIC_MIN_BPR + 1)
42*07e2034dSPavel Fedin 
43*07e2034dSPavel Fedin /* For some distributor fields we want to model the array of 32-bit
44*07e2034dSPavel Fedin  * register values which hold various bitmaps corresponding to enabled,
45*07e2034dSPavel Fedin  * pending, etc bits. These macros and functions facilitate that; the
46*07e2034dSPavel Fedin  * APIs are generally modelled on the generic bitmap.h functions
47*07e2034dSPavel Fedin  * (which are unsuitable here because they use 'unsigned long' as the
48*07e2034dSPavel Fedin  * underlying storage type, which is very awkward when you need to
49*07e2034dSPavel Fedin  * access the data as 32-bit values.)
50*07e2034dSPavel Fedin  * Each bitmap contains a bit for each interrupt. Although there is
51*07e2034dSPavel Fedin  * space for the PPIs and SGIs, those bits (the first 32) are never
52*07e2034dSPavel Fedin  * used as that state lives in the redistributor. The unused bits are
53*07e2034dSPavel Fedin  * provided purely so that interrupt X's state is always in bit X; this
54*07e2034dSPavel Fedin  * avoids bugs where we forget to subtract GIC_INTERNAL from an
55*07e2034dSPavel Fedin  * interrupt number.
56*07e2034dSPavel Fedin  */
57*07e2034dSPavel Fedin #define GICV3_BMP_SIZE (DIV_ROUND_UP(GICV3_MAXIRQ, 32))
58*07e2034dSPavel Fedin 
59*07e2034dSPavel Fedin #define GIC_DECLARE_BITMAP(name) \
60*07e2034dSPavel Fedin     uint32_t name[GICV3_BMP_SIZE]
61*07e2034dSPavel Fedin 
62*07e2034dSPavel Fedin #define GIC_BIT_MASK(nr) (1U << ((nr) % 32))
63*07e2034dSPavel Fedin #define GIC_BIT_WORD(nr) ((nr) / 32)
64*07e2034dSPavel Fedin 
65*07e2034dSPavel Fedin static inline void gic_bmp_set_bit(int nr, uint32_t *addr)
66*07e2034dSPavel Fedin {
67*07e2034dSPavel Fedin     uint32_t mask = GIC_BIT_MASK(nr);
68*07e2034dSPavel Fedin     uint32_t *p = addr + GIC_BIT_WORD(nr);
69*07e2034dSPavel Fedin 
70*07e2034dSPavel Fedin     *p |= mask;
71*07e2034dSPavel Fedin }
72*07e2034dSPavel Fedin 
73*07e2034dSPavel Fedin static inline void gic_bmp_clear_bit(int nr, uint32_t *addr)
74*07e2034dSPavel Fedin {
75*07e2034dSPavel Fedin     uint32_t mask = GIC_BIT_MASK(nr);
76*07e2034dSPavel Fedin     uint32_t *p = addr + GIC_BIT_WORD(nr);
77*07e2034dSPavel Fedin 
78*07e2034dSPavel Fedin     *p &= ~mask;
79*07e2034dSPavel Fedin }
80*07e2034dSPavel Fedin 
81*07e2034dSPavel Fedin static inline int gic_bmp_test_bit(int nr, const uint32_t *addr)
82*07e2034dSPavel Fedin {
83*07e2034dSPavel Fedin     return 1U & (addr[GIC_BIT_WORD(nr)] >> (nr & 31));
84*07e2034dSPavel Fedin }
85*07e2034dSPavel Fedin 
86*07e2034dSPavel Fedin static inline void gic_bmp_replace_bit(int nr, uint32_t *addr, int val)
87*07e2034dSPavel Fedin {
88*07e2034dSPavel Fedin     uint32_t mask = GIC_BIT_MASK(nr);
89*07e2034dSPavel Fedin     uint32_t *p = addr + GIC_BIT_WORD(nr);
90*07e2034dSPavel Fedin 
91*07e2034dSPavel Fedin     *p &= ~mask;
92*07e2034dSPavel Fedin     *p |= (val & 1U) << (nr % 32);
93*07e2034dSPavel Fedin }
94*07e2034dSPavel Fedin 
95*07e2034dSPavel Fedin /* Return a pointer to the 32-bit word containing the specified bit. */
96*07e2034dSPavel Fedin static inline uint32_t *gic_bmp_ptr32(uint32_t *addr, int nr)
97*07e2034dSPavel Fedin {
98*07e2034dSPavel Fedin     return addr + GIC_BIT_WORD(nr);
99*07e2034dSPavel Fedin }
100*07e2034dSPavel Fedin 
101*07e2034dSPavel Fedin typedef struct GICv3State GICv3State;
102*07e2034dSPavel Fedin typedef struct GICv3CPUState GICv3CPUState;
103*07e2034dSPavel Fedin 
104*07e2034dSPavel Fedin /* Some CPU interface registers come in three flavours:
105*07e2034dSPavel Fedin  * Group0, Group1 (Secure) and Group1 (NonSecure)
106*07e2034dSPavel Fedin  * (where the latter two are exposed as a single banked system register).
107*07e2034dSPavel Fedin  * In the state struct they are implemented as a 3-element array which
108*07e2034dSPavel Fedin  * can be indexed into by the GICV3_G0, GICV3_G1 and GICV3_G1NS constants.
109*07e2034dSPavel Fedin  * If the CPU doesn't support EL3 then the G1 element is unused.
110*07e2034dSPavel Fedin  *
111*07e2034dSPavel Fedin  * These constants are also used to communicate the group to use for
112*07e2034dSPavel Fedin  * an interrupt or SGI when it is passed between the cpu interface and
113*07e2034dSPavel Fedin  * the redistributor or distributor. For those purposes the receiving end
114*07e2034dSPavel Fedin  * must be prepared to cope with a Group 1 Secure interrupt even if it does
115*07e2034dSPavel Fedin  * not have security support enabled, because security can be disabled
116*07e2034dSPavel Fedin  * independently in the CPU and in the GIC. In that case the receiver should
117*07e2034dSPavel Fedin  * treat an incoming Group 1 Secure interrupt as if it were Group 0.
118*07e2034dSPavel Fedin  * (This architectural requirement is why the _G1 element is the unused one
119*07e2034dSPavel Fedin  * in a no-EL3 CPU:  we would otherwise have to translate back and forth
120*07e2034dSPavel Fedin  * between (G0, G1NS) from the distributor and (G0, G1) in the CPU i/f.)
121*07e2034dSPavel Fedin  */
122*07e2034dSPavel Fedin #define GICV3_G0 0
123*07e2034dSPavel Fedin #define GICV3_G1 1
124*07e2034dSPavel Fedin #define GICV3_G1NS 2
125*07e2034dSPavel Fedin 
126*07e2034dSPavel Fedin /* ICC_CTLR_EL1, GICD_STATUSR and GICR_STATUSR are banked but not
127*07e2034dSPavel Fedin  * group-related, so those indices are just 0 for S and 1 for NS.
128*07e2034dSPavel Fedin  * (If the CPU or the GIC, respectively, don't support the Security
129*07e2034dSPavel Fedin  * extensions then the S element is unused.)
130*07e2034dSPavel Fedin  */
131*07e2034dSPavel Fedin #define GICV3_S 0
132*07e2034dSPavel Fedin #define GICV3_NS 1
133*07e2034dSPavel Fedin 
134*07e2034dSPavel Fedin struct GICv3CPUState {
135*07e2034dSPavel Fedin     GICv3State *gic;
136*07e2034dSPavel Fedin     CPUState *cpu;
137*07e2034dSPavel Fedin 
138*07e2034dSPavel Fedin     /* Redistributor */
139*07e2034dSPavel Fedin     uint32_t level;                  /* Current IRQ level */
140*07e2034dSPavel Fedin     /* RD_base page registers */
141*07e2034dSPavel Fedin     uint32_t gicr_ctlr;
142*07e2034dSPavel Fedin     uint64_t gicr_typer;
143*07e2034dSPavel Fedin     uint32_t gicr_statusr[2];
144*07e2034dSPavel Fedin     uint32_t gicr_waker;
145*07e2034dSPavel Fedin     uint64_t gicr_propbaser;
146*07e2034dSPavel Fedin     uint64_t gicr_pendbaser;
147*07e2034dSPavel Fedin     /* SGI_base page registers */
148*07e2034dSPavel Fedin     uint32_t gicr_igroupr0;
149*07e2034dSPavel Fedin     uint32_t gicr_ienabler0;
150*07e2034dSPavel Fedin     uint32_t gicr_ipendr0;
151*07e2034dSPavel Fedin     uint32_t gicr_iactiver0;
152*07e2034dSPavel Fedin     uint32_t edge_trigger; /* ICFGR0 and ICFGR1 even bits */
153*07e2034dSPavel Fedin     uint32_t gicr_igrpmodr0;
154*07e2034dSPavel Fedin     uint32_t gicr_nsacr;
155*07e2034dSPavel Fedin     uint8_t gicr_ipriorityr[GIC_INTERNAL];
156*07e2034dSPavel Fedin 
157*07e2034dSPavel Fedin     /* CPU interface */
158*07e2034dSPavel Fedin     uint64_t icc_ctlr_el1[2];
159*07e2034dSPavel Fedin     uint64_t icc_pmr_el1;
160*07e2034dSPavel Fedin     uint64_t icc_bpr[3];
161*07e2034dSPavel Fedin     uint64_t icc_apr[3][4];
162*07e2034dSPavel Fedin     uint64_t icc_igrpen[3];
163*07e2034dSPavel Fedin     uint64_t icc_ctlr_el3;
164*07e2034dSPavel Fedin };
165*07e2034dSPavel Fedin 
166*07e2034dSPavel Fedin struct GICv3State {
167ff8f06eeSShlomo Pongratz     /*< private >*/
168ff8f06eeSShlomo Pongratz     SysBusDevice parent_obj;
169ff8f06eeSShlomo Pongratz     /*< public >*/
170ff8f06eeSShlomo Pongratz 
171ff8f06eeSShlomo Pongratz     qemu_irq *parent_irq;
172ff8f06eeSShlomo Pongratz     qemu_irq *parent_fiq;
173ff8f06eeSShlomo Pongratz 
174ff8f06eeSShlomo Pongratz     MemoryRegion iomem_dist; /* Distributor */
175ff8f06eeSShlomo Pongratz     MemoryRegion iomem_redist; /* Redistributors */
176ff8f06eeSShlomo Pongratz 
177ff8f06eeSShlomo Pongratz     uint32_t num_cpu;
178ff8f06eeSShlomo Pongratz     uint32_t num_irq;
179ff8f06eeSShlomo Pongratz     uint32_t revision;
180ff8f06eeSShlomo Pongratz     bool security_extn;
181*07e2034dSPavel Fedin     bool irq_reset_nonsecure;
182ff8f06eeSShlomo Pongratz 
183ff8f06eeSShlomo Pongratz     int dev_fd; /* kvm device fd if backed by kvm vgic support */
184*07e2034dSPavel Fedin     Error *migration_blocker;
185*07e2034dSPavel Fedin 
186*07e2034dSPavel Fedin     /* Distributor */
187*07e2034dSPavel Fedin 
188*07e2034dSPavel Fedin     /* for a GIC with the security extensions the NS banked version of this
189*07e2034dSPavel Fedin      * register is just an alias of bit 1 of the S banked version.
190*07e2034dSPavel Fedin      */
191*07e2034dSPavel Fedin     uint32_t gicd_ctlr;
192*07e2034dSPavel Fedin     uint32_t gicd_statusr[2];
193*07e2034dSPavel Fedin     GIC_DECLARE_BITMAP(group);        /* GICD_IGROUPR */
194*07e2034dSPavel Fedin     GIC_DECLARE_BITMAP(grpmod);       /* GICD_IGRPMODR */
195*07e2034dSPavel Fedin     GIC_DECLARE_BITMAP(enabled);      /* GICD_ISENABLER */
196*07e2034dSPavel Fedin     GIC_DECLARE_BITMAP(pending);      /* GICD_ISPENDR */
197*07e2034dSPavel Fedin     GIC_DECLARE_BITMAP(active);       /* GICD_ISACTIVER */
198*07e2034dSPavel Fedin     GIC_DECLARE_BITMAP(level);        /* Current level */
199*07e2034dSPavel Fedin     GIC_DECLARE_BITMAP(edge_trigger); /* GICD_ICFGR even bits */
200*07e2034dSPavel Fedin     uint8_t gicd_ipriority[GICV3_MAXIRQ];
201*07e2034dSPavel Fedin     uint64_t gicd_irouter[GICV3_MAXIRQ];
202*07e2034dSPavel Fedin     uint32_t gicd_nsacr[DIV_ROUND_UP(GICV3_MAXIRQ, 16)];
203*07e2034dSPavel Fedin 
204*07e2034dSPavel Fedin     GICv3CPUState *cpu;
205*07e2034dSPavel Fedin };
206*07e2034dSPavel Fedin 
207*07e2034dSPavel Fedin #define GICV3_BITMAP_ACCESSORS(BMP)                                     \
208*07e2034dSPavel Fedin     static inline void gicv3_gicd_##BMP##_set(GICv3State *s, int irq)   \
209*07e2034dSPavel Fedin     {                                                                   \
210*07e2034dSPavel Fedin         gic_bmp_set_bit(irq, s->BMP);                                   \
211*07e2034dSPavel Fedin     }                                                                   \
212*07e2034dSPavel Fedin     static inline int gicv3_gicd_##BMP##_test(GICv3State *s, int irq)   \
213*07e2034dSPavel Fedin     {                                                                   \
214*07e2034dSPavel Fedin         return gic_bmp_test_bit(irq, s->BMP);                           \
215*07e2034dSPavel Fedin     }                                                                   \
216*07e2034dSPavel Fedin     static inline void gicv3_gicd_##BMP##_clear(GICv3State *s, int irq) \
217*07e2034dSPavel Fedin     {                                                                   \
218*07e2034dSPavel Fedin         gic_bmp_clear_bit(irq, s->BMP);                                 \
219*07e2034dSPavel Fedin     }                                                                   \
220*07e2034dSPavel Fedin     static inline void gicv3_gicd_##BMP##_replace(GICv3State *s,        \
221*07e2034dSPavel Fedin                                                   int irq, int value)   \
222*07e2034dSPavel Fedin     {                                                                   \
223*07e2034dSPavel Fedin         gic_bmp_replace_bit(irq, s->BMP, value);                        \
224*07e2034dSPavel Fedin     }
225*07e2034dSPavel Fedin 
226*07e2034dSPavel Fedin GICV3_BITMAP_ACCESSORS(group)
227*07e2034dSPavel Fedin GICV3_BITMAP_ACCESSORS(grpmod)
228*07e2034dSPavel Fedin GICV3_BITMAP_ACCESSORS(enabled)
229*07e2034dSPavel Fedin GICV3_BITMAP_ACCESSORS(pending)
230*07e2034dSPavel Fedin GICV3_BITMAP_ACCESSORS(active)
231*07e2034dSPavel Fedin GICV3_BITMAP_ACCESSORS(level)
232*07e2034dSPavel Fedin GICV3_BITMAP_ACCESSORS(edge_trigger)
233ff8f06eeSShlomo Pongratz 
234ff8f06eeSShlomo Pongratz #define TYPE_ARM_GICV3_COMMON "arm-gicv3-common"
235ff8f06eeSShlomo Pongratz #define ARM_GICV3_COMMON(obj) \
236ff8f06eeSShlomo Pongratz      OBJECT_CHECK(GICv3State, (obj), TYPE_ARM_GICV3_COMMON)
237ff8f06eeSShlomo Pongratz #define ARM_GICV3_COMMON_CLASS(klass) \
238ff8f06eeSShlomo Pongratz      OBJECT_CLASS_CHECK(ARMGICv3CommonClass, (klass), TYPE_ARM_GICV3_COMMON)
239ff8f06eeSShlomo Pongratz #define ARM_GICV3_COMMON_GET_CLASS(obj) \
240ff8f06eeSShlomo Pongratz      OBJECT_GET_CLASS(ARMGICv3CommonClass, (obj), TYPE_ARM_GICV3_COMMON)
241ff8f06eeSShlomo Pongratz 
242ff8f06eeSShlomo Pongratz typedef struct ARMGICv3CommonClass {
243ff8f06eeSShlomo Pongratz     /*< private >*/
244ff8f06eeSShlomo Pongratz     SysBusDeviceClass parent_class;
245ff8f06eeSShlomo Pongratz     /*< public >*/
246ff8f06eeSShlomo Pongratz 
247ff8f06eeSShlomo Pongratz     void (*pre_save)(GICv3State *s);
248ff8f06eeSShlomo Pongratz     void (*post_load)(GICv3State *s);
249ff8f06eeSShlomo Pongratz } ARMGICv3CommonClass;
250ff8f06eeSShlomo Pongratz 
251ff8f06eeSShlomo Pongratz void gicv3_init_irqs_and_mmio(GICv3State *s, qemu_irq_handler handler,
252ff8f06eeSShlomo Pongratz                               const MemoryRegionOps *ops);
253ff8f06eeSShlomo Pongratz 
254ff8f06eeSShlomo Pongratz #endif
255