1 | /* $Id: GICInternal.h 109033 2025-04-21 07:01:04Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * GIC - Generic Interrupt Controller Architecture (GIC).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2023-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef VMM_INCLUDED_SRC_include_GICInternal_h
|
---|
29 | #define VMM_INCLUDED_SRC_include_GICInternal_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #include <VBox/gic.h>
|
---|
35 | #include <VBox/vmm/pdmdev.h>
|
---|
36 | #include <VBox/vmm/pdmgic.h>
|
---|
37 | #include <VBox/vmm/stam.h>
|
---|
38 |
|
---|
39 | #include "GITSInternal.h"
|
---|
40 |
|
---|
41 | /** @defgroup grp_gic_int Internal
|
---|
42 | * @ingroup grp_gic
|
---|
43 | * @internal
|
---|
44 | * @{
|
---|
45 | */
|
---|
46 |
|
---|
47 | #ifdef VBOX_INCLUDED_vmm_pdmgic_h
|
---|
48 | /** The VirtualBox GIC backend. */
|
---|
49 | extern const PDMGICBACKEND g_GicBackend;
|
---|
50 | # ifdef RT_OS_DARWIN
|
---|
51 | /** The Hypervisor.Framework GIC backend. */
|
---|
52 | extern const PDMGICBACKEND g_GicHvfBackend;
|
---|
53 | # elif defined(RT_OS_WINDOWS)
|
---|
54 | /** The Hyper-V GIC backend. */
|
---|
55 | extern const PDMGICBACKEND g_GicHvBackend;
|
---|
56 | # elif defined(RT_OS_LINUX)
|
---|
57 | /** The KVM GIC backend. */
|
---|
58 | extern const PDMGICBACKEND g_GicKvmBackend;
|
---|
59 | # endif
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | #define VMCPU_TO_GICCPU(a_pVCpu) (&(a_pVCpu)->gic.s)
|
---|
63 | #define VM_TO_GIC(a_pVM) (&(a_pVM)->gic.s)
|
---|
64 | #define VM_TO_GICDEV(a_pVM) CTX_SUFF(VM_TO_GIC(a_pVM)->pGicDev)
|
---|
65 | #define GICDEV_TO_GITSDEV(a_GicDev) (&(a_GicDev)->Gits)
|
---|
66 | #ifdef IN_RING3
|
---|
67 | # define VMCPU_TO_DEVINS(a_pVCpu) ((a_pVCpu)->pVMR3->gic.s.pDevInsR3)
|
---|
68 | #elif defined(IN_RING0)
|
---|
69 | # error "Not implemented!"
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | /** Acquire the device critical section. */
|
---|
73 | #define GIC_CRIT_SECT_ENTER(a_pDevIns) \
|
---|
74 | do \
|
---|
75 | { \
|
---|
76 | int const rcLock_ = PDMDevHlpCritSectEnter((a_pDevIns), (a_pDevIns)->pCritSectRoR3, VINF_SUCCESS); \
|
---|
77 | PDM_CRITSECT_RELEASE_ASSERT_RC_DEV((a_pDevIns), (a_pDevIns)->pCritSectRoR3, rcLock_); \
|
---|
78 | } while(0)
|
---|
79 |
|
---|
80 | /** Release the device critical section. */
|
---|
81 | #define GIC_CRIT_SECT_LEAVE(a_pDevIns) PDMDevHlpCritSectLeave((a_pDevIns), (a_pDevIns)->CTX_SUFF(pCritSectRo))
|
---|
82 |
|
---|
83 | /** Returns whether the critical section is held. */
|
---|
84 | #define GIC_CRIT_SECT_IS_OWNER(a_pDevIns) PDMDevHlpCritSectIsOwner((a_pDevIns), (a_pDevIns)->CTX_SUFF(pCritSectRo))
|
---|
85 |
|
---|
86 | /** Returns whether the given register offset is within the specified range. */
|
---|
87 | #define GIC_IS_REG_IN_RANGE(a_offReg, a_offFirst, a_cbRegion) ((uint32_t)(a_offReg) - (a_offFirst) < (a_cbRegion))
|
---|
88 |
|
---|
89 | /** @def GIC_SET_REG_U64_FULL
|
---|
90 | * Sets a 64-bit GIC register.
|
---|
91 | * @param a_uReg The 64-bit register to set.
|
---|
92 | * @param a_uValue The 64-bit value being written.
|
---|
93 | * @param a_fRwMask The 64-bit mask of valid read-write bits.
|
---|
94 | */
|
---|
95 | #define GIC_SET_REG_U64_FULL(a_uReg, a_uValue, a_fRwMask) \
|
---|
96 | do \
|
---|
97 | { \
|
---|
98 | AssertCompile(sizeof(a_uReg) == sizeof(uint64_t)); \
|
---|
99 | AssertCompile(sizeof(a_fRwMask) == sizeof(uint64_t)); \
|
---|
100 | (a_uReg) = ((a_uReg) & ~(a_fRwMask)) | ((a_uValue) & (a_fRwMask)); \
|
---|
101 | } while (0)
|
---|
102 |
|
---|
103 | /** @def GIC_SET_REG_U64_LO
|
---|
104 | * Sets the lower half of a 64-bit GIC register.
|
---|
105 | * @param a_uReg The lower half of a 64-bit register to set.
|
---|
106 | * @param a_uValue The value being written (only lower 32-bits are used).
|
---|
107 | * @param a_fRwMask The 64-bit mask of valid read-write bits.
|
---|
108 | */
|
---|
109 | #define GIC_SET_REG_U64_LO(a_uReg, a_uValue, a_fRwMask) \
|
---|
110 | do \
|
---|
111 | { \
|
---|
112 | AssertCompile(sizeof(a_uReg) == sizeof(uint32_t)); \
|
---|
113 | AssertCompile(sizeof(a_fRwMask) == sizeof(uint64_t)); \
|
---|
114 | (a_uReg) = ((a_uReg) & ~(RT_LO_U32(a_fRwMask))) | ((uint32_t)(a_uValue) & (RT_LO_U32(a_fRwMask))); \
|
---|
115 | } while (0)
|
---|
116 |
|
---|
117 | /** @def GIC_SET_REG_U64_HI
|
---|
118 | * Sets the upper half of a 64-bit GIC register.
|
---|
119 | * @param a_uReg The upper half of the 64-bit register to set.
|
---|
120 | * @param a_uValue The value being written (only lower 32-bits are used).
|
---|
121 | * @param a_fRwMask The 64-bit mask of valid read-write bits.
|
---|
122 | */
|
---|
123 | #define GIC_SET_REG_U64_HI(a_uReg, a_uValue, a_fRwMask) \
|
---|
124 | do \
|
---|
125 | { \
|
---|
126 | AssertCompile(sizeof(a_uReg) == sizeof(uint32_t)); \
|
---|
127 | AssertCompile(sizeof(a_fRwMask) == sizeof(uint64_t)); \
|
---|
128 | (a_uReg) = ((a_uReg) & ~(RT_HI_U32(a_fRwMask))) | ((uint32_t)(a_uValue) & (RT_HI_U32(a_fRwMask))); \
|
---|
129 | } while (0)
|
---|
130 |
|
---|
131 | /** @def GIC_SET_REG_U32
|
---|
132 | * Sets a 32-bit GIC register.
|
---|
133 | * @param a_uReg The 32-bit register to set.
|
---|
134 | * @param a_uValue The 32-bit value being written (only lower 32-bits are
|
---|
135 | * used).
|
---|
136 | * @param a_fRwMask The mask of valid read-write bits (only lower 32-bits are
|
---|
137 | * used).
|
---|
138 | */
|
---|
139 | #define GIC_SET_REG_U32(a_uReg, a_uValue, a_fRwMask) \
|
---|
140 | do \
|
---|
141 | { \
|
---|
142 | AssertCompile(sizeof(a_uReg) == sizeof(uint32_t)); \
|
---|
143 | (a_uReg) = ((a_uReg) & ~(a_fRwMask)) | ((uint32_t)(a_uValue) & (uint32_t)(a_fRwMask)); \
|
---|
144 | } while (0)
|
---|
145 |
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * GIC PDM instance data (per-VM).
|
---|
149 | */
|
---|
150 | typedef struct GICDEV
|
---|
151 | {
|
---|
152 | /** @name Distributor register state.
|
---|
153 | * @{
|
---|
154 | */
|
---|
155 | /** Interrupt group bitmap. */
|
---|
156 | uint32_t bmIntrGroup[64];
|
---|
157 | /** Interrupt config bitmap (edge-triggered vs level-sensitive). */
|
---|
158 | uint32_t bmIntrConfig[128];
|
---|
159 | /** Interrupt enabled bitmap. */
|
---|
160 | uint32_t bmIntrEnabled[64];
|
---|
161 | /** Interrupt pending bitmap. */
|
---|
162 | uint32_t bmIntrPending[64];
|
---|
163 | /** Interrupt active bitmap. */
|
---|
164 | uint32_t bmIntrActive[64];
|
---|
165 | /** Interrupt priorities. */
|
---|
166 | uint8_t abIntrPriority[2048];
|
---|
167 | /** Interrupt routing info. */
|
---|
168 | uint32_t au32IntrRouting[2048];
|
---|
169 | /** Interrupt routine mode bitmap. */
|
---|
170 | uint32_t bmIntrRoutingMode[64];
|
---|
171 | /** Flag whether group 0 interrupts are enabled. */
|
---|
172 | bool fIntrGroup0Enabled;
|
---|
173 | /** Flag whether group 1 interrupts are enabled. */
|
---|
174 | bool fIntrGroup1Enabled;
|
---|
175 | /** Flag whether affinity routing is enabled. */
|
---|
176 | bool fAffRoutingEnabled;
|
---|
177 | /** @} */
|
---|
178 |
|
---|
179 | /** @name Configurables.
|
---|
180 | * @{ */
|
---|
181 | /** The GIC architecture revision (GICD_PIDR2.ArchRev and GICR_PIDR2.ArchRev). */
|
---|
182 | uint8_t uArchRev;
|
---|
183 | /** The GIC architecture minor revision (currently 1 as we only support GICv3.1). */
|
---|
184 | uint8_t uArchRevMinor;
|
---|
185 | /** The maximum SPI supported (GICD_TYPER.ItLinesNumber). */
|
---|
186 | uint8_t uMaxSpi;
|
---|
187 | /** Whether extended SPIs are supported (GICD_ESPI). */
|
---|
188 | bool fExtSpi;
|
---|
189 | /** The maximum extended SPI supported (GICD_TYPER.ESPI_range). */
|
---|
190 | uint8_t uMaxExtSpi;
|
---|
191 | /** Whether extended PPIs are supported. */
|
---|
192 | bool fExtPpi;
|
---|
193 | /** The maximum extended PPI supported (GICR_TYPER.PPInum). */
|
---|
194 | uint8_t uMaxExtPpi;
|
---|
195 | /** Whether range-selector is supported (GICD_TYPER.RSS and ICC_CTLR_EL1.RSS). */
|
---|
196 | bool fRangeSel;
|
---|
197 | /** Whether NMIs are supported (GICD_TYPER.NMI). */
|
---|
198 | bool fNmi;
|
---|
199 | /** Whether message-based interrupts are supported (GICD_TYPER.MBIS). */
|
---|
200 | bool fMbi;
|
---|
201 | /** Whether non-zero affinity 3 levels are supported (GICD_TYPER.A3V) and
|
---|
202 | * (ICC_CTLR.A3V). */
|
---|
203 | bool fAff3Levels;
|
---|
204 | /** Whether LPIs are supported (GICD_TYPER.PLPIS). */
|
---|
205 | bool fLpi;
|
---|
206 | /** The maximum LPI supported (GICD_TYPER.num_LPI). */
|
---|
207 | uint8_t uMaxLpi;
|
---|
208 | /** @} */
|
---|
209 |
|
---|
210 | /** @name GITS device data and LPIs.
|
---|
211 | * @{ */
|
---|
212 | /** Whether LPIs are enabled (GICR_CTLR.EnableLpis of all redistributors). */
|
---|
213 | bool fEnableLpis;
|
---|
214 | /** Padding. */
|
---|
215 | bool afPadding1[3];
|
---|
216 | /** ITS device state. */
|
---|
217 | GITSDEV Gits;
|
---|
218 | /** LPI config table. */
|
---|
219 | uint8_t abLpiConfig[4096];
|
---|
220 | /** The LPI config table base register (GICR_PROPBASER). */
|
---|
221 | RTUINT64U uLpiConfigBaseReg;
|
---|
222 | /** The LPI pending table base register (GICR_PENDBASER). */
|
---|
223 | RTUINT64U uLpiPendingBaseReg;
|
---|
224 | /** @} */
|
---|
225 |
|
---|
226 | /** @name MMIO data.
|
---|
227 | * @{ */
|
---|
228 | /** The distributor MMIO handle. */
|
---|
229 | IOMMMIOHANDLE hMmioDist;
|
---|
230 | /** The redistributor MMIO handle. */
|
---|
231 | IOMMMIOHANDLE hMmioReDist;
|
---|
232 | /** The interrupt translation service MMIO handle. */
|
---|
233 | IOMMMIOHANDLE hMmioGits;
|
---|
234 | /** @} */
|
---|
235 | } GICDEV;
|
---|
236 | /** Pointer to a GIC device. */
|
---|
237 | typedef GICDEV *PGICDEV;
|
---|
238 | /** Pointer to a const GIC device. */
|
---|
239 | typedef GICDEV const *PCGICDEV;
|
---|
240 | AssertCompileMemberSizeAlignment(GICDEV, Gits, 8);
|
---|
241 | AssertCompileMemberAlignment(GICDEV, abLpiConfig, 8);
|
---|
242 | AssertCompileMemberAlignment(GICDEV, hMmioDist, 8);
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * GIC VM Instance data.
|
---|
246 | */
|
---|
247 | typedef struct GIC
|
---|
248 | {
|
---|
249 | /** The ring-3 device instance. */
|
---|
250 | PPDMDEVINSR3 pDevInsR3;
|
---|
251 | } GIC;
|
---|
252 | /** Pointer to GIC VM instance data. */
|
---|
253 | typedef GIC *PGIC;
|
---|
254 | /** Pointer to const GIC VM instance data. */
|
---|
255 | typedef GIC const *PCGIC;
|
---|
256 | AssertCompileSizeAlignment(GIC, 8);
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * GIC VMCPU Instance data.
|
---|
260 | */
|
---|
261 | typedef struct GICCPU
|
---|
262 | {
|
---|
263 | /** @name Redistributor register state.
|
---|
264 | * @{ */
|
---|
265 | /** Interrupt group bitmap. */
|
---|
266 | uint32_t bmIntrGroup[3];
|
---|
267 | /** Interrupt config bitmap (edge-triggered vs level-sensitive). */
|
---|
268 | uint32_t bmIntrConfig[6];
|
---|
269 | /** Interrupt enabled bitmap. */
|
---|
270 | uint32_t bmIntrEnabled[3];
|
---|
271 | /** Interrupt pending bitmap. */
|
---|
272 | uint32_t bmIntrPending[3];
|
---|
273 | /** Interrupt active bitmap. */
|
---|
274 | uint32_t bmIntrActive[3];
|
---|
275 | /** Interrupt priorities. */
|
---|
276 | uint8_t abIntrPriority[96];
|
---|
277 | /** @} */
|
---|
278 |
|
---|
279 | /** @name ICC system register state.
|
---|
280 | * @{ */
|
---|
281 | /** The control register (ICC_CTLR_EL1). */
|
---|
282 | uint64_t uIccCtlr;
|
---|
283 | /** The interrupt priority mask of the CPU interface (ICC_PMR_EL1). */
|
---|
284 | uint8_t bIntrPriorityMask;
|
---|
285 | /** The index to the current running priority. */
|
---|
286 | uint8_t idxRunningPriority;
|
---|
287 | /** The running priorities caused by preemption. */
|
---|
288 | uint8_t abRunningPriorities[256];
|
---|
289 | /** The active priorities group 0 bitmap. */
|
---|
290 | uint32_t bmActivePriorityGroup0[4];
|
---|
291 | /** The active priorities group 0 bitmap. */
|
---|
292 | uint32_t bmActivePriorityGroup1[4];
|
---|
293 | /** The binary point register for group 0 interrupts. */
|
---|
294 | uint8_t bBinaryPtGroup0;
|
---|
295 | /** The binary point register for group 1 interrupts. */
|
---|
296 | uint8_t bBinaryPtGroup1;
|
---|
297 | /** Flag whether group 0 interrupts are enabled. */
|
---|
298 | bool fIntrGroup0Enabled;
|
---|
299 | /** Flag whether group 1 interrupts are enabled. */
|
---|
300 | bool fIntrGroup1Enabled;
|
---|
301 | /** @} */
|
---|
302 |
|
---|
303 | /** @name LPIs.
|
---|
304 | * @{ */
|
---|
305 | /** LPI pending bitmap. */
|
---|
306 | uint64_t bmLpiPending[64];
|
---|
307 | /** @} */
|
---|
308 |
|
---|
309 | /** @name Statistics.
|
---|
310 | * @{ */
|
---|
311 | #ifdef VBOX_WITH_STATISTICS
|
---|
312 | /** Number of MMIO reads. */
|
---|
313 | STAMCOUNTER StatMmioRead;
|
---|
314 | /** Number of MMIO writes. */
|
---|
315 | STAMCOUNTER StatMmioWrite;
|
---|
316 | /** Number of MSR reads. */
|
---|
317 | STAMCOUNTER StatSysRegRead;
|
---|
318 | /** Number of MSR writes. */
|
---|
319 | STAMCOUNTER StatSysRegWrite;
|
---|
320 | /** Number of set SPI callbacks. */
|
---|
321 | STAMCOUNTER StatSetSpi;
|
---|
322 | /** Number of set PPI callbacks. */
|
---|
323 | STAMCOUNTER StatSetPpi;
|
---|
324 | /** Number of SGIs generated. */
|
---|
325 | STAMCOUNTER StatSetSgi;
|
---|
326 |
|
---|
327 | /** Profiling of interrupt acknowledge (IAR). */
|
---|
328 | STAMPROFILE StatProfIntrAck;
|
---|
329 | /** Profiling of set SPI callback. */
|
---|
330 | STAMPROFILE StatProfSetSpi;
|
---|
331 | /** Profiling of set PPI callback. */
|
---|
332 | STAMPROFILE StatProfSetPpi;
|
---|
333 | /** Profiling of set SGI function. */
|
---|
334 | STAMPROFILE StatProfSetSgi;
|
---|
335 | #endif
|
---|
336 | /** @} */
|
---|
337 | } GICCPU;
|
---|
338 | /** Pointer to GIC VMCPU instance data. */
|
---|
339 | typedef GICCPU *PGICCPU;
|
---|
340 | /** Pointer to a const GIC VMCPU instance data. */
|
---|
341 | typedef GICCPU const *PCGICCPU;
|
---|
342 | /* Ensure the LPI pending bitmap's capacity is sufficient for the number of LPIs we support. */
|
---|
343 | AssertCompileMemberSize(GICCPU, bmLpiPending, RT_ELEMENTS(GICDEV::abLpiConfig) / 8);
|
---|
344 | AssertCompileMemberAlignment(GICCPU, bmLpiPending, 8);
|
---|
345 |
|
---|
346 | DECL_HIDDEN_CALLBACK(VBOXSTRICTRC) gicDistMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb);
|
---|
347 | DECL_HIDDEN_CALLBACK(VBOXSTRICTRC) gicDistMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb);
|
---|
348 | DECL_HIDDEN_CALLBACK(VBOXSTRICTRC) gicReDistMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb);
|
---|
349 | DECL_HIDDEN_CALLBACK(VBOXSTRICTRC) gicReDistMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb);
|
---|
350 | DECL_HIDDEN_CALLBACK(VBOXSTRICTRC) gicItsMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb);
|
---|
351 | DECL_HIDDEN_CALLBACK(VBOXSTRICTRC) gicItsMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb);
|
---|
352 |
|
---|
353 | DECLHIDDEN(void) gicDistReadLpiConfigTableFromMem(PPDMDEVINS pDevIns);
|
---|
354 |
|
---|
355 | DECLHIDDEN(void) gicResetCpu(PPDMDEVINS pDevIns, PVMCPUCC pVCpu);
|
---|
356 | DECLHIDDEN(void) gicReset(PPDMDEVINS pDevIns);
|
---|
357 | DECLHIDDEN(uint16_t) gicReDistGetIntIdFromIndex(uint16_t idxIntr);
|
---|
358 | DECLHIDDEN(uint16_t) gicDistGetIntIdFromIndex(uint16_t idxIntr);
|
---|
359 |
|
---|
360 | DECLCALLBACK(int) gicR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg);
|
---|
361 | DECLCALLBACK(int) gicR3Destruct(PPDMDEVINS pDevIns);
|
---|
362 | DECLCALLBACK(void) gicR3Reset(PPDMDEVINS pDevIns);
|
---|
363 |
|
---|
364 | /** @} */
|
---|
365 |
|
---|
366 | #endif /* !VMM_INCLUDED_SRC_include_GICInternal_h */
|
---|
367 |
|
---|