VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/GICR3.cpp@ 108835

Last change on this file since 108835 was 108835, checked in by vboxsync, 3 weeks ago

VMM/GIC: bugref:10877 GITS work-in-progress, remove unused R0/RC callbacks, fixed typo in debug info dumping ITS table base adderss registers and other nits.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 48.7 KB
Line 
1/* $Id: GICR3.cpp 108835 2025-04-03 10:40:08Z 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
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_DEV_GIC
33#include <VBox/log.h>
34#include "GICInternal.h"
35#include <VBox/vmm/pdmgic.h>
36#include <VBox/vmm/cpum.h>
37#include <VBox/vmm/hm.h>
38#include <VBox/vmm/mm.h>
39#include <VBox/vmm/pdmdev.h>
40#include <VBox/vmm/ssm.h>
41#include <VBox/vmm/vm.h>
42
43#include <iprt/armv8.h>
44#include <iprt/mem.h>
45
46
47#ifndef VBOX_DEVICE_STRUCT_TESTCASE
48
49
50/*********************************************************************************************************************************
51* Defined Constants And Macros *
52*********************************************************************************************************************************/
53/** GIC saved state version. */
54#define GIC_SAVED_STATE_VERSION 8
55
56# define GIC_SYSREGRANGE(a_uFirst, a_uLast, a_szName) \
57 { (a_uFirst), (a_uLast), kCpumSysRegRdFn_GicIcc, kCpumSysRegWrFn_GicIcc, 0, 0, 0, 0, 0, 0, a_szName, { 0 }, { 0 }, { 0 }, { 0 } }
58
59
60/*********************************************************************************************************************************
61* Global Variables *
62*********************************************************************************************************************************/
63/**
64 * System register ranges for the GIC.
65 */
66static CPUMSYSREGRANGE const g_aSysRegRanges_GIC[] =
67{
68 GIC_SYSREGRANGE(ARMV8_AARCH64_SYSREG_ICC_PMR_EL1, ARMV8_AARCH64_SYSREG_ICC_PMR_EL1, "ICC_PMR_EL1"),
69 GIC_SYSREGRANGE(ARMV8_AARCH64_SYSREG_ICC_IAR0_EL1, ARMV8_AARCH64_SYSREG_ICC_AP0R3_EL1, "ICC_IAR0_EL1 - ICC_AP0R3_EL1"),
70 GIC_SYSREGRANGE(ARMV8_AARCH64_SYSREG_ICC_AP1R0_EL1, ARMV8_AARCH64_SYSREG_ICC_NMIAR1_EL1, "ICC_AP1R0_EL1 - ICC_NMIAR1_EL1"),
71 GIC_SYSREGRANGE(ARMV8_AARCH64_SYSREG_ICC_DIR_EL1, ARMV8_AARCH64_SYSREG_ICC_SGI0R_EL1, "ICC_DIR_EL1 - ICC_SGI0R_EL1"),
72 GIC_SYSREGRANGE(ARMV8_AARCH64_SYSREG_ICC_IAR1_EL1, ARMV8_AARCH64_SYSREG_ICC_IGRPEN1_EL1, "ICC_IAR1_EL1 - ICC_IGRPEN1_EL1"),
73 GIC_SYSREGRANGE(ARMV8_AARCH64_SYSREG_ICC_SRE_EL2, ARMV8_AARCH64_SYSREG_ICC_SRE_EL2, "ICC_SRE_EL2"),
74 GIC_SYSREGRANGE(ARMV8_AARCH64_SYSREG_ICC_SRE_EL3, ARMV8_AARCH64_SYSREG_ICC_SRE_EL3, "ICC_SRE_EL3")
75};
76
77
78/**
79 * Dumps basic GIC state.
80 *
81 * @param pVM The cross context VM structure.
82 * @param pHlp The info helpers.
83 * @param pszArgs Arguments, ignored.
84 */
85static DECLCALLBACK(void) gicR3DbgInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
86{
87 RT_NOREF(pszArgs);
88 PCGIC pGic = VM_TO_GIC(pVM);
89 PPDMDEVINS pDevIns = pGic->CTX_SUFF(pDevIns);
90 PCGICDEV pGicDev = PDMDEVINS_2_DATA(pDevIns, PCGICDEV);
91
92 pHlp->pfnPrintf(pHlp, "GIC:\n");
93 pHlp->pfnPrintf(pHlp, " uArchRev = %u\n", pGicDev->uArchRev);
94 pHlp->pfnPrintf(pHlp, " uArchRevMinor = %u\n", pGicDev->uArchRevMinor);
95 pHlp->pfnPrintf(pHlp, " uMaxSpi = %u (upto IntId %u)\n", pGicDev->uMaxSpi, 32 * (pGicDev->uMaxSpi + 1));
96 pHlp->pfnPrintf(pHlp, " fExtSpi = %RTbool\n", pGicDev->fExtSpi);
97 pHlp->pfnPrintf(pHlp, " uMaxExtSpi = %u (upto IntId %u)\n", pGicDev->uMaxExtSpi,
98 GIC_INTID_RANGE_EXT_SPI_START - 1 + 32 * (pGicDev->uMaxExtSpi + 1));
99 pHlp->pfnPrintf(pHlp, " fExtPpi = %RTbool\n", pGicDev->fExtPpi);
100 pHlp->pfnPrintf(pHlp, " uMaxExtPpi = %u (upto IntId %u)\n", pGicDev->uMaxExtPpi,
101 pGicDev->uMaxExtPpi == GIC_REDIST_REG_TYPER_PPI_NUM_MAX_1087 ? 1087 : GIC_INTID_RANGE_EXT_PPI_LAST);
102 pHlp->pfnPrintf(pHlp, " fRangeSelSupport = %RTbool\n", pGicDev->fRangeSel);
103 pHlp->pfnPrintf(pHlp, " fNmi = %RTbool\n", pGicDev->fNmi);
104 pHlp->pfnPrintf(pHlp, " fMbi = %RTbool\n", pGicDev->fMbi);
105 pHlp->pfnPrintf(pHlp, " fAff3Levels = %RTbool\n", pGicDev->fAff3Levels);
106 pHlp->pfnPrintf(pHlp, " fLpi = %RTbool\n", pGicDev->fLpi);
107}
108
109
110/**
111 * Dumps GIC Distributor information.
112 *
113 * @param pVM The cross context VM structure.
114 * @param pHlp The info helpers.
115 * @param pszArgs Arguments, ignored.
116 */
117static DECLCALLBACK(void) gicR3DbgInfoDist(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
118{
119 RT_NOREF(pszArgs);
120
121 PGIC pGic = VM_TO_GIC(pVM);
122 PPDMDEVINS pDevIns = pGic->CTX_SUFF(pDevIns);
123 PCGICDEV pGicDev = PDMDEVINS_2_DATA(pDevIns, PCGICDEV);
124
125#define GIC_DBGFINFO_DIST_INTR_BITMAP(a_Name, a_bmIntr) \
126 do \
127 { \
128 pHlp->pfnPrintf(pHlp, " " a_Name " =\n"); \
129 for (uint32_t i = 0; i < RT_ELEMENTS(a_bmIntr); i += 8) \
130 pHlp->pfnPrintf(pHlp, " [%2u..%-2u] %#010x %#010x %#010x %#010x %#010x %#010x %#010x %#010x\n", i, i + 7, \
131 (a_bmIntr)[i], (a_bmIntr)[i+1], (a_bmIntr)[i+2], (a_bmIntr)[i+3], \
132 (a_bmIntr)[i+4], (a_bmIntr)[i+5], (a_bmIntr)[i+6], (a_bmIntr)[i+7]); \
133 } while (0)
134
135 pHlp->pfnPrintf(pHlp, "GIC Distributor:\n");
136 pHlp->pfnPrintf(pHlp, " fIntrGroup0Enabled = %RTbool\n", pGicDev->fIntrGroup0Enabled);
137 pHlp->pfnPrintf(pHlp, " fIntrGroup1Enabled = %RTbool\n", pGicDev->fIntrGroup1Enabled);
138 pHlp->pfnPrintf(pHlp, " fAffRoutingEnabled = %RTbool\n", pGicDev->fAffRoutingEnabled);
139 GIC_DBGFINFO_DIST_INTR_BITMAP("bmIntrGroup", pGicDev->bmIntrGroup);
140 GIC_DBGFINFO_DIST_INTR_BITMAP("bmIntrEnabled", pGicDev->bmIntrEnabled);
141 GIC_DBGFINFO_DIST_INTR_BITMAP("bmIntrPending", pGicDev->bmIntrPending);
142 GIC_DBGFINFO_DIST_INTR_BITMAP("bmIntrActive", pGicDev->bmIntrActive);
143
144 /* Interrupt priorities.*/
145 {
146 uint32_t const cPriorities = RT_ELEMENTS(pGicDev->abIntrPriority);
147 AssertCompile(!(cPriorities % 16));
148 pHlp->pfnPrintf(pHlp, " Interrupt priorities:\n");
149 for (uint32_t i = 0; i < cPriorities; i += 16)
150 pHlp->pfnPrintf(pHlp, " IntId[%4u..%-4u] = %3u %3u %3u %3u %3u %3u %3u %3u"
151 " IntId[%4u..%-4u] = %3u %3u %3u %3u %3u %3u %3u %3u\n",
152 gicDistGetIntIdFromIndex(i), gicDistGetIntIdFromIndex(i + 7),
153 pGicDev->abIntrPriority[i], pGicDev->abIntrPriority[i + 1],
154 pGicDev->abIntrPriority[i + 2], pGicDev->abIntrPriority[i + 3],
155 pGicDev->abIntrPriority[i + 4], pGicDev->abIntrPriority[i + 5],
156 pGicDev->abIntrPriority[i + 6], pGicDev->abIntrPriority[i + 7],
157 gicDistGetIntIdFromIndex(i + 8), gicDistGetIntIdFromIndex(i + 15),
158 pGicDev->abIntrPriority[i + 8], pGicDev->abIntrPriority[i + 9],
159 pGicDev->abIntrPriority[i + 10], pGicDev->abIntrPriority[i + 11],
160 pGicDev->abIntrPriority[i + 12], pGicDev->abIntrPriority[i + 13],
161 pGicDev->abIntrPriority[i + 14], pGicDev->abIntrPriority[i + 15]);
162 }
163
164 /* Interrupt routing.*/
165 {
166 /** @todo Interrupt rounting mode. */
167 uint32_t const cRouting = RT_ELEMENTS(pGicDev->au32IntrRouting);
168 AssertCompile(!(cRouting % 16));
169 pHlp->pfnPrintf(pHlp, " Interrupt routing:\n");
170 for (uint32_t i = 0; i < cRouting; i += 16)
171 pHlp->pfnPrintf(pHlp, " IntId[%4u..%-4u] = %3u %3u %3u %3u %3u %3u %3u %3u"
172 " IntId[%4u..%-4u] = %3u %3u %3u %3u %3u %3u %3u %3u\n",
173 gicDistGetIntIdFromIndex(i), gicDistGetIntIdFromIndex(i + 7),
174 pGicDev->au32IntrRouting[i], pGicDev->au32IntrRouting[i + 1],
175 pGicDev->au32IntrRouting[i + 2], pGicDev->au32IntrRouting[i + 3],
176 pGicDev->au32IntrRouting[i + 4], pGicDev->au32IntrRouting[i + 5],
177 pGicDev->au32IntrRouting[i + 6], pGicDev->au32IntrRouting[i + 7],
178 gicDistGetIntIdFromIndex(i + 8), gicDistGetIntIdFromIndex(i + 15),
179 pGicDev->au32IntrRouting[i + 8], pGicDev->au32IntrRouting[i + 9],
180 pGicDev->au32IntrRouting[i + 10], pGicDev->au32IntrRouting[i + 11],
181 pGicDev->au32IntrRouting[i + 12], pGicDev->au32IntrRouting[i + 13],
182 pGicDev->au32IntrRouting[i + 14], pGicDev->au32IntrRouting[i + 15]);
183 }
184
185#undef GIC_DBGFINFO_DIST_INTR_BITMAP
186}
187
188
189/**
190 * Dumps the GIC Redistributor information.
191 *
192 * @param pVM The cross context VM structure.
193 * @param pHlp The info helpers.
194 * @param pszArgs Arguments, ignored.
195 */
196static DECLCALLBACK(void) gicR3DbgInfoReDist(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
197{
198 NOREF(pszArgs);
199 PVMCPU pVCpu = VMMGetCpu(pVM);
200 if (!pVCpu)
201 pVCpu = pVM->apCpusR3[0];
202
203 PCGICCPU pGicCpu = VMCPU_TO_GICCPU(pVCpu);
204
205 pHlp->pfnPrintf(pHlp, "VCPU[%u] Redistributor:\n", pVCpu->idCpu);
206 AssertCompile(RT_ELEMENTS(pGicCpu->bmIntrGroup) >= 3);
207 AssertCompile(RT_ELEMENTS(pGicCpu->bmIntrEnabled) >= 3);
208 AssertCompile(RT_ELEMENTS(pGicCpu->bmIntrPending) >= 3);
209 AssertCompile(RT_ELEMENTS(pGicCpu->bmIntrActive) >= 3);
210
211#define GIC_DBGFINFO_REDIST_INTR_BITMAPS_3(a_bmIntr) pGicCpu->a_bmIntr[0], pGicCpu->a_bmIntr[1], pGicCpu->a_bmIntr[2]
212 pHlp->pfnPrintf(pHlp, " bmIntrGroup[0..2] = %#010x %#010x %#010x\n", GIC_DBGFINFO_REDIST_INTR_BITMAPS_3(bmIntrGroup));
213 pHlp->pfnPrintf(pHlp, " bmIntrEnabled[0..2] = %#010x %#010x %#010x\n", GIC_DBGFINFO_REDIST_INTR_BITMAPS_3(bmIntrEnabled));
214 pHlp->pfnPrintf(pHlp, " bmIntrPending[0..2] = %#010x %#010x %#010x\n", GIC_DBGFINFO_REDIST_INTR_BITMAPS_3(bmIntrPending));
215 pHlp->pfnPrintf(pHlp, " bmIntrActive[0..2] = %#010x %#010x %#010x\n", GIC_DBGFINFO_REDIST_INTR_BITMAPS_3(bmIntrActive));
216#undef GIC_DBGFINFO_REDIST_INTR_BITMAPS
217
218 /* Interrupt priorities. */
219 {
220 uint32_t const cPriorities = RT_ELEMENTS(pGicCpu->abIntrPriority);
221 AssertCompile(!(cPriorities % 16));
222 pHlp->pfnPrintf(pHlp, " Interrupt priorities:\n");
223 for (uint32_t i = 0; i < cPriorities; i += 16)
224 pHlp->pfnPrintf(pHlp, " IntId[%4u..%-4u] = %3u %3u %3u %3u %3u %3u %3u %3u"
225 " IntId[%4u..%-4u] = %3u %3u %3u %3u %3u %3u %3u %3u\n",
226 gicReDistGetIntIdFromIndex(i), gicReDistGetIntIdFromIndex(i + 7),
227 pGicCpu->abIntrPriority[i], pGicCpu->abIntrPriority[i + 1],
228 pGicCpu->abIntrPriority[i + 2], pGicCpu->abIntrPriority[i + 3],
229 pGicCpu->abIntrPriority[i + 4], pGicCpu->abIntrPriority[i + 5],
230 pGicCpu->abIntrPriority[i + 6], pGicCpu->abIntrPriority[i + 7],
231 gicReDistGetIntIdFromIndex(i + 8), gicReDistGetIntIdFromIndex(i + 15),
232 pGicCpu->abIntrPriority[i + 8], pGicCpu->abIntrPriority[i + 9],
233 pGicCpu->abIntrPriority[i + 10], pGicCpu->abIntrPriority[i + 11],
234 pGicCpu->abIntrPriority[i + 12], pGicCpu->abIntrPriority[i + 13],
235 pGicCpu->abIntrPriority[i + 14], pGicCpu->abIntrPriority[i + 15]);
236 }
237
238 pHlp->pfnPrintf(pHlp, "\nVCPU[%u] ICC system register state:\n", pVCpu->idCpu);
239 pHlp->pfnPrintf(pHlp, " uIccCtlr = %#RX64\n", pGicCpu->uIccCtlr);
240 pHlp->pfnPrintf(pHlp, " fIntrGroup0Enabled = %RTbool\n", pGicCpu->fIntrGroup0Enabled);
241 pHlp->pfnPrintf(pHlp, " fIntrGroup1Enabled = %RTbool\n", pGicCpu->fIntrGroup1Enabled);
242 pHlp->pfnPrintf(pHlp, " bBinaryPtGroup0 = %#x\n", pGicCpu->bBinaryPtGroup0);
243 pHlp->pfnPrintf(pHlp, " bBinaryPtGroup1 = %#x\n", pGicCpu->bBinaryPtGroup1);
244 pHlp->pfnPrintf(pHlp, " idxRunningPriority = %#x\n", pGicCpu->idxRunningPriority);
245 pHlp->pfnPrintf(pHlp, " Running priority = %#x\n", pGicCpu->abRunningPriorities[pGicCpu->idxRunningPriority]);
246
247 /* Running interrupt priorities. */
248 {
249 uint32_t const cPriorities = RT_ELEMENTS(pGicCpu->abRunningPriorities);
250 AssertCompile(!(cPriorities % 16));
251 pHlp->pfnPrintf(pHlp, " Running-interrupt priorities:\n");
252 for (uint32_t i = 0; i < cPriorities; i += 16)
253 pHlp->pfnPrintf(pHlp, " [%3u..%-3u] = %3u %3u %3u %3u %3u %3u %3u %3u"
254 " [%3u..%-3u] = %3u %3u %3u %3u %3u %3u %3u %3u\n",
255 i, i + 7,
256 pGicCpu->abRunningPriorities[i], pGicCpu->abRunningPriorities[i + 1],
257 pGicCpu->abRunningPriorities[i + 2], pGicCpu->abRunningPriorities[i + 3],
258 pGicCpu->abRunningPriorities[i + 4], pGicCpu->abRunningPriorities[i + 5],
259 pGicCpu->abRunningPriorities[i + 6], pGicCpu->abRunningPriorities[i + 7],
260 i + 8, i + 15,
261 pGicCpu->abRunningPriorities[i + 8], pGicCpu->abRunningPriorities[i + 9],
262 pGicCpu->abRunningPriorities[i + 10], pGicCpu->abRunningPriorities[i + 11],
263 pGicCpu->abRunningPriorities[i + 12], pGicCpu->abRunningPriorities[i + 13],
264 pGicCpu->abRunningPriorities[i + 14], pGicCpu->abRunningPriorities[i + 15]);
265 }
266
267 AssertCompile(RT_ELEMENTS(pGicCpu->bmActivePriorityGroup0) >= 4);
268 pHlp->pfnPrintf(pHlp, " Active-interrupt priorities Group 0:\n");
269 pHlp->pfnPrintf(pHlp, " [0..3] = %#010x %#010x %#010x %#010x\n",
270 pGicCpu->bmActivePriorityGroup0[0], pGicCpu->bmActivePriorityGroup0[1],
271 pGicCpu->bmActivePriorityGroup0[2], pGicCpu->bmActivePriorityGroup0[3]);
272 AssertCompile(RT_ELEMENTS(pGicCpu->bmActivePriorityGroup1) >= 4);
273 pHlp->pfnPrintf(pHlp, " Active-interrupt priorities Group 1:\n");
274 pHlp->pfnPrintf(pHlp, " [0..3] = %#010x %#010x %#010x %#010x\n",
275 pGicCpu->bmActivePriorityGroup1[0], pGicCpu->bmActivePriorityGroup1[1],
276 pGicCpu->bmActivePriorityGroup1[2], pGicCpu->bmActivePriorityGroup1[3]);
277}
278
279
280/**
281 * Dumps the GIC ITS information.
282 *
283 * @param pVM The cross context VM structure.
284 * @param pHlp The info helpers.
285 * @param pszArgs Arguments, ignored.
286 */
287static DECLCALLBACK(void) gicR3DbgInfoIts(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
288{
289 PGIC pGic = VM_TO_GIC(pVM);
290 PPDMDEVINS pDevIns = pGic->CTX_SUFF(pDevIns);
291 PCGICDEV pGicDev = PDMDEVINS_2_DATA(pDevIns, PCGICDEV);
292 if (pGicDev->hMmioGits != NIL_IOMMMIOHANDLE)
293 gitsR3DbgInfo(&pGicDev->Gits, pHlp, pszArgs);
294 else
295 pHlp->pfnPrintf(pHlp, "GIC ITS is not mapped/configured for the VM\n");
296}
297
298
299/**
300 * The GIC ITS command-queue thread.
301 *
302 * @returns VBox status code.
303 * @param pDevIns The device instance.
304 * @param pThread The command thread.
305 */
306static DECLCALLBACK(int) gicItsR3CmdQueueThread(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
307{
308 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
309 return VINF_SUCCESS;
310
311 PGICDEV pGicDev = PDMDEVINS_2_DATA(pDevIns, PGICDEV);
312 AssertPtrReturn(pGicDev, VERR_INVALID_PARAMETER);
313 LogFlowFunc(("Command-queue thread spawned and initialized\n"));
314
315 /*
316 * Pre-allocate the maximum size of the command queue allowed by the spec.
317 * This prevents trashing the heap as well as deal with out-of-memory situations
318 * up-front while starting the VM. It also simplifies the code from having to
319 * dynamically grow/shrink the allocation based on how software sizes the queue.
320 * Guests normally don't alter the queue size all the time, but that's not an
321 * assumption we can make.
322 */
323 uint16_t const cMaxPages = GITS_BF_CTRL_REG_CBASER_SIZE_MASK + 1;
324 size_t const cbCmdQueue = cMaxPages << GUEST_PAGE_SHIFT;
325 void *pvCommands = RTMemAllocZ(cbCmdQueue);
326 AssertLogRelMsgReturn(pvCommands, ("Failed to alloc %.Rhcb (%zu bytes) for GITS command queue\n", cbCmdQueue, cbCmdQueue),
327 VERR_NO_MEMORY);
328
329 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
330 {
331 /*
332 * Sleep until we are woken up.
333 */
334 {
335 int const rc = PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pGicDev->hEvtCmdQueue, RT_INDEFINITE_WAIT);
336 AssertLogRelMsgReturnStmt(RT_SUCCESS(rc) || rc == VERR_INTERRUPTED, ("%Rrc\n", rc), RTMemFree(pvCommands), rc);
337 if (pThread->enmState != PDMTHREADSTATE_RUNNING)
338 break;
339 }
340
341 int const rcLock = PDMDevHlpCritSectEnter(pDevIns, pDevIns->pCritSectRoR3, VINF_SUCCESS);
342 PDM_CRITSECT_RELEASE_ASSERT_RC_DEV(pDevIns, pDevIns->pCritSectRoR3, rcLock);
343
344 /** @todo Process commands. */
345
346 PDMDevHlpCritSectLeave(pDevIns, pDevIns->pCritSectRoR3);
347 }
348
349 RTMemFree(pvCommands);
350
351 LogFlowFunc(("Command-queue thread terminating\n"));
352 return VINF_SUCCESS;
353}
354
355
356/**
357 * Wakes up the command-queue thread so it can respond to a state change.
358 *
359 * @returns VBox status code.
360 * @param pDevIns The device instance.
361 * @param pThread The command-queue thread.
362 *
363 * @thread EMT.
364 */
365static DECLCALLBACK(int) gicItsR3CmdQueueThreadWakeUp(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
366{
367 RT_NOREF2(pDevIns, pThread);
368 LogFlowFunc(("\n"));
369 PGICDEV pGicDev = PDMDEVINS_2_DATA(pDevIns, PGICDEV);
370 return PDMDevHlpSUPSemEventSignal(pDevIns, pGicDev->hEvtCmdQueue);
371}
372
373
374/**
375 * @copydoc FNSSMDEVSAVEEXEC
376 */
377static DECLCALLBACK(int) gicR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
378{
379 PCVM pVM = PDMDevHlpGetVM(pDevIns);
380 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
381 PCGICDEV pGicDev = PDMDEVINS_2_DATA(pDevIns, PCGICDEV);
382 AssertPtrReturn(pVM, VERR_INVALID_VM_HANDLE);
383 LogFlowFunc(("\n"));
384
385 /*
386 * Save per-VM data.
387 */
388 pHlp->pfnSSMPutU32(pSSM, pVM->cCpus);
389 pHlp->pfnSSMPutU8(pSSM, pGicDev->uArchRev);
390 pHlp->pfnSSMPutU8(pSSM, pGicDev->uArchRevMinor);
391 pHlp->pfnSSMPutU8(pSSM, pGicDev->uMaxSpi);
392 pHlp->pfnSSMPutBool(pSSM, pGicDev->fExtSpi);
393 pHlp->pfnSSMPutU8(pSSM, pGicDev->uMaxExtSpi);
394 pHlp->pfnSSMPutBool(pSSM, pGicDev->fExtPpi);
395 pHlp->pfnSSMPutU8(pSSM, pGicDev->uMaxExtPpi);
396 pHlp->pfnSSMPutBool(pSSM, pGicDev->fRangeSel);
397 pHlp->pfnSSMPutBool(pSSM, pGicDev->fNmi);
398 pHlp->pfnSSMPutBool(pSSM, pGicDev->fMbi);
399 pHlp->pfnSSMPutBool(pSSM, pGicDev->fAff3Levels);
400 pHlp->pfnSSMPutBool(pSSM, pGicDev->fLpi);
401
402 /* Distributor state. */
403 pHlp->pfnSSMPutBool(pSSM, pGicDev->fIntrGroup0Enabled);
404 pHlp->pfnSSMPutBool(pSSM, pGicDev->fIntrGroup1Enabled);
405 pHlp->pfnSSMPutBool(pSSM, pGicDev->fAffRoutingEnabled);
406 pHlp->pfnSSMPutMem(pSSM, &pGicDev->bmIntrGroup[0], sizeof(pGicDev->bmIntrGroup));
407 pHlp->pfnSSMPutMem(pSSM, &pGicDev->bmIntrConfig[0], sizeof(pGicDev->bmIntrConfig));
408 pHlp->pfnSSMPutMem(pSSM, &pGicDev->bmIntrEnabled[0], sizeof(pGicDev->bmIntrEnabled));
409 pHlp->pfnSSMPutMem(pSSM, &pGicDev->bmIntrPending[0], sizeof(pGicDev->bmIntrPending));
410 pHlp->pfnSSMPutMem(pSSM, &pGicDev->bmIntrActive[0], sizeof(pGicDev->bmIntrActive));
411 pHlp->pfnSSMPutMem(pSSM, &pGicDev->abIntrPriority[0], sizeof(pGicDev->abIntrPriority));
412 pHlp->pfnSSMPutMem(pSSM, &pGicDev->au32IntrRouting[0], sizeof(pGicDev->au32IntrRouting));
413 pHlp->pfnSSMPutMem(pSSM, &pGicDev->bmIntrRoutingMode[0], sizeof(pGicDev->bmIntrRoutingMode));
414
415 /* We store the size followed by the data because we currently do not support the full LPI range. */
416 pHlp->pfnSSMPutU32(pSSM, sizeof(pGicDev->abLpiConfig));
417 pHlp->pfnSSMPutMem(pSSM, &pGicDev->abLpiConfig[0], sizeof(pGicDev->abLpiConfig));
418 pHlp->pfnSSMPutU32(pSSM, sizeof(pGicDev->bmLpiPending));
419 pHlp->pfnSSMPutMem(pSSM, &pGicDev->bmLpiPending[0], sizeof(pGicDev->bmLpiPending));
420
421 /** @todo GITS data. */
422
423 /*
424 * Save per-VCPU data.
425 */
426 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
427 {
428 PCGICCPU pGicCpu = VMCPU_TO_GICCPU(pVM->apCpusR3[idCpu]);
429 Assert(pGicCpu);
430
431 /* Redistributor state. */
432 pHlp->pfnSSMPutMem(pSSM, &pGicCpu->bmIntrGroup[0], sizeof(pGicCpu->bmIntrGroup));
433 pHlp->pfnSSMPutMem(pSSM, &pGicCpu->bmIntrConfig[0], sizeof(pGicCpu->bmIntrConfig));
434 pHlp->pfnSSMPutMem(pSSM, &pGicCpu->bmIntrEnabled[0], sizeof(pGicCpu->bmIntrEnabled));
435 pHlp->pfnSSMPutMem(pSSM, &pGicCpu->bmIntrPending[0], sizeof(pGicCpu->bmIntrPending));
436 pHlp->pfnSSMPutMem(pSSM, &pGicCpu->bmIntrActive[0], sizeof(pGicCpu->bmIntrActive));
437 pHlp->pfnSSMPutMem(pSSM, &pGicCpu->abIntrPriority[0], sizeof(pGicCpu->abIntrPriority));
438
439 /* ICC system register state. */
440 pHlp->pfnSSMPutU64(pSSM, pGicCpu->uIccCtlr);
441 pHlp->pfnSSMPutU8(pSSM, pGicCpu->bIntrPriorityMask);
442 pHlp->pfnSSMPutU8(pSSM, pGicCpu->idxRunningPriority);
443 pHlp->pfnSSMPutMem(pSSM, &pGicCpu->abRunningPriorities[0], sizeof(pGicCpu->abRunningPriorities));
444 pHlp->pfnSSMPutMem(pSSM, &pGicCpu->bmActivePriorityGroup0[0], sizeof(pGicCpu->bmActivePriorityGroup0));
445 pHlp->pfnSSMPutMem(pSSM, &pGicCpu->bmActivePriorityGroup1[0], sizeof(pGicCpu->bmActivePriorityGroup1));
446 pHlp->pfnSSMPutU8(pSSM, pGicCpu->bBinaryPtGroup0);
447 pHlp->pfnSSMPutU8(pSSM, pGicCpu->bBinaryPtGroup1);
448 pHlp->pfnSSMPutBool(pSSM, pGicCpu->fIntrGroup0Enabled);
449 pHlp->pfnSSMPutBool(pSSM, pGicCpu->fIntrGroup1Enabled);
450 }
451
452 return pHlp->pfnSSMPutU32(pSSM, UINT32_MAX);
453}
454
455
456/**
457 * @copydoc FNSSMDEVLOADEXEC
458 */
459static DECLCALLBACK(int) gicR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
460{
461 PVM pVM = PDMDevHlpGetVM(pDevIns);
462 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
463
464 AssertPtrReturn(pVM, VERR_INVALID_VM_HANDLE);
465 AssertReturn(uPass == SSM_PASS_FINAL, VERR_WRONG_ORDER);
466 LogFlowFunc(("uVersion=%u uPass=%#x\n", uVersion, uPass));
467
468 /*
469 * Validate supported saved-state versions.
470 */
471 if (uVersion != GIC_SAVED_STATE_VERSION)
472 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Invalid saved-state version %u"), uVersion);
473
474 /*
475 * Load per-VM data.
476 */
477 uint32_t cCpus;
478 pHlp->pfnSSMGetU32(pSSM, &cCpus);
479 if (cCpus != pVM->cCpus)
480 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch: cCpus: got=%u expected=%u"), cCpus, pVM->cCpus);
481
482 PGICDEV pGicDev = PDMDEVINS_2_DATA(pDevIns, PGICDEV);
483 pHlp->pfnSSMGetU8(pSSM, &pGicDev->uArchRev);
484 pHlp->pfnSSMGetU8(pSSM, &pGicDev->uArchRevMinor);
485 pHlp->pfnSSMGetU8(pSSM, &pGicDev->uMaxSpi);
486 pHlp->pfnSSMGetBool(pSSM, &pGicDev->fExtSpi);
487 pHlp->pfnSSMGetU8(pSSM, &pGicDev->uMaxExtSpi);
488 pHlp->pfnSSMGetBool(pSSM, &pGicDev->fExtPpi);
489 pHlp->pfnSSMGetU8(pSSM, &pGicDev->uMaxExtPpi);
490 pHlp->pfnSSMGetBool(pSSM, &pGicDev->fRangeSel);
491 pHlp->pfnSSMGetBool(pSSM, &pGicDev->fNmi);
492 pHlp->pfnSSMGetBool(pSSM, &pGicDev->fMbi);
493 pHlp->pfnSSMGetBool(pSSM, &pGicDev->fAff3Levels);
494 pHlp->pfnSSMGetBool(pSSM, &pGicDev->fLpi);
495
496 /* Distributor state. */
497 pHlp->pfnSSMGetBool(pSSM, &pGicDev->fIntrGroup0Enabled);
498 pHlp->pfnSSMGetBool(pSSM, &pGicDev->fIntrGroup1Enabled);
499 pHlp->pfnSSMGetBool(pSSM, &pGicDev->fAffRoutingEnabled);
500 pHlp->pfnSSMGetMem(pSSM, &pGicDev->bmIntrGroup[0], sizeof(pGicDev->bmIntrGroup));
501 pHlp->pfnSSMGetMem(pSSM, &pGicDev->bmIntrConfig[0], sizeof(pGicDev->bmIntrConfig));
502 pHlp->pfnSSMGetMem(pSSM, &pGicDev->bmIntrEnabled[0], sizeof(pGicDev->bmIntrEnabled));
503 pHlp->pfnSSMGetMem(pSSM, &pGicDev->bmIntrPending[0], sizeof(pGicDev->bmIntrPending));
504 pHlp->pfnSSMGetMem(pSSM, &pGicDev->bmIntrActive[0], sizeof(pGicDev->bmIntrActive));
505 pHlp->pfnSSMGetMem(pSSM, &pGicDev->abIntrPriority[0], sizeof(pGicDev->abIntrPriority));
506 pHlp->pfnSSMGetMem(pSSM, &pGicDev->au32IntrRouting[0], sizeof(pGicDev->au32IntrRouting));
507 pHlp->pfnSSMGetMem(pSSM, &pGicDev->bmIntrRoutingMode[0], sizeof(pGicDev->bmIntrRoutingMode));
508
509 /* LPI config. */
510 {
511 uint32_t cbData = 0;
512 int const rc = pHlp->pfnSSMGetU32(pSSM, &cbData);
513 AssertRCReturn(rc, rc);
514 if (cbData <= sizeof(pGicDev->abLpiConfig))
515 pHlp->pfnSSMGetMem(pSSM, &pGicDev->abLpiConfig[0], cbData);
516 else
517 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch: LPI config table size: got=%u expected=%u"),
518 cbData, sizeof(pGicDev->abLpiConfig));
519 }
520 /* LPI pending. */
521 {
522 uint32_t cbData = 0;
523 int const rc = pHlp->pfnSSMGetU32(pSSM, &cbData);
524 AssertRCReturn(rc, rc);
525 if (cbData <= sizeof(pGicDev->bmLpiPending))
526 pHlp->pfnSSMGetMem(pSSM, &pGicDev->bmLpiPending[0], cbData);
527 else
528 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch: LPI pending bitmap size: got=%u expected=%u"),
529 cbData, sizeof(pGicDev->bmLpiPending));
530 }
531
532 /** @todo GITS data. */
533
534 /*
535 * Load per-VCPU data.
536 */
537 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
538 {
539 PGICCPU pGicCpu = VMCPU_TO_GICCPU(pVM->apCpusR3[idCpu]);
540 Assert(pGicCpu);
541
542 /* Redistributor state. */
543 pHlp->pfnSSMGetMem(pSSM, &pGicCpu->bmIntrGroup[0], sizeof(pGicCpu->bmIntrGroup));
544 pHlp->pfnSSMGetMem(pSSM, &pGicCpu->bmIntrConfig[0], sizeof(pGicCpu->bmIntrConfig));
545 pHlp->pfnSSMGetMem(pSSM, &pGicCpu->bmIntrEnabled[0], sizeof(pGicCpu->bmIntrEnabled));
546 pHlp->pfnSSMGetMem(pSSM, &pGicCpu->bmIntrPending[0], sizeof(pGicCpu->bmIntrPending));
547 pHlp->pfnSSMGetMem(pSSM, &pGicCpu->bmIntrActive[0], sizeof(pGicCpu->bmIntrActive));
548 pHlp->pfnSSMGetMem(pSSM, &pGicCpu->abIntrPriority[0], sizeof(pGicCpu->abIntrPriority));
549
550 /* ICC system register state. */
551 pHlp->pfnSSMGetU64(pSSM, &pGicCpu->uIccCtlr);
552 pHlp->pfnSSMGetU8(pSSM, &pGicCpu->bIntrPriorityMask);
553 pHlp->pfnSSMGetU8(pSSM, &pGicCpu->idxRunningPriority);
554 pHlp->pfnSSMGetMem(pSSM, &pGicCpu->abRunningPriorities[0], sizeof(pGicCpu->abRunningPriorities));
555 pHlp->pfnSSMGetMem(pSSM, &pGicCpu->bmActivePriorityGroup0[0], sizeof(pGicCpu->bmActivePriorityGroup0));
556 pHlp->pfnSSMGetMem(pSSM, &pGicCpu->bmActivePriorityGroup1[0], sizeof(pGicCpu->bmActivePriorityGroup1));
557 pHlp->pfnSSMGetU8(pSSM, &pGicCpu->bBinaryPtGroup0);
558 pHlp->pfnSSMGetU8(pSSM, &pGicCpu->bBinaryPtGroup1);
559 pHlp->pfnSSMGetBool(pSSM, &pGicCpu->fIntrGroup0Enabled);
560 pHlp->pfnSSMGetBool(pSSM, &pGicCpu->fIntrGroup1Enabled);
561 }
562
563 /*
564 * Check that we're still good wrt restored data.
565 */
566 int rc = pHlp->pfnSSMHandleGetStatus(pSSM);
567 AssertRCReturn(rc, rc);
568
569 uint32_t uMarker = 0;
570 rc = pHlp->pfnSSMGetU32(pSSM, &uMarker);
571 AssertRCReturn(rc, rc);
572 if (uMarker == UINT32_MAX)
573 { /* likely */ }
574 else
575 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch: Marker: got=%u expected=%u"), uMarker, UINT32_MAX);
576
577 /*
578 * Finally, perform sanity checks.
579 */
580 if (pGicDev->uArchRev <= GIC_DIST_REG_PIDR2_ARCHREV_GICV4)
581 { /* likely */ }
582 else
583 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Invalid uArchRev, got %u expected range [1,31]"), pGicDev->uArchRev,
584 GIC_DIST_REG_PIDR2_ARCHREV_GICV1, GIC_DIST_REG_PIDR2_ARCHREV_GICV4);
585 if (pGicDev->uArchRevMinor == 1)
586 { /* likely */ }
587 else
588 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Invalid uArchRevMinor, got %u expected 1"), pGicDev->uArchRevMinor);
589 if (pGicDev->uMaxSpi - 1 < 31)
590 { /* likely */ }
591 else
592 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Invalid MaxSpi, got %u expected range [1,31]"), pGicDev->uMaxSpi);
593 if (pGicDev->uMaxExtSpi <= 31)
594 { /* likely */ }
595 else
596 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Invalid MaxExtSpi, got %u expected range [0,31]"), pGicDev->uMaxExtSpi);
597 if ( pGicDev->uMaxExtPpi == GIC_REDIST_REG_TYPER_PPI_NUM_MAX_1087
598 || pGicDev->uMaxExtPpi == GIC_REDIST_REG_TYPER_PPI_NUM_MAX_1119)
599 { /* likely */ }
600 else
601 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Invalid MaxExtPpi, got %u expected range [1,2]"), pGicDev->uMaxExtPpi);
602 bool const fIsGitsEnabled = RT_BOOL(pGicDev->hMmioGits != NIL_IOMMMIOHANDLE);
603 if (fIsGitsEnabled == pGicDev->fLpi)
604 { /* likely */ }
605 else
606 return pHlp->pfnSSMSetCfgError(pSSM, RT_SRC_POS, N_("Config mismatch: LPIs are %s when ITS is %s"),
607 fIsGitsEnabled ? "enabled" : "disabled", pGicDev->fLpi ? "enabled" : "disabled");
608 return rc;
609}
610
611
612/**
613 * @interface_method_impl{PDMDEVREG,pfnReset}
614 */
615DECLCALLBACK(void) gicR3Reset(PPDMDEVINS pDevIns)
616{
617 PVM pVM = PDMDevHlpGetVM(pDevIns);
618 VM_ASSERT_EMT0(pVM);
619 VM_ASSERT_IS_NOT_RUNNING(pVM);
620
621 LogFlow(("GIC: gicR3Reset\n"));
622
623 gicReset(pDevIns);
624 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
625 {
626 PVMCPU pVCpuDest = pVM->apCpusR3[idCpu];
627 gicResetCpu(pDevIns, pVCpuDest);
628 }
629}
630
631
632/**
633 * @interface_method_impl{PDMDEVREG,pfnDestruct}
634 */
635DECLCALLBACK(int) gicR3Destruct(PPDMDEVINS pDevIns)
636{
637 LogFlowFunc(("pDevIns=%p\n", pDevIns));
638 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
639
640 PGICDEV pGicDev = PDMDEVINS_2_DATA(pDevIns, PGICDEV);
641 if (pGicDev->hEvtCmdQueue != NIL_SUPSEMEVENT)
642 {
643 PDMDevHlpSUPSemEventClose(pDevIns, pGicDev->hEvtCmdQueue);
644 pGicDev->hEvtCmdQueue = NIL_SUPSEMEVENT;
645 }
646
647 return VINF_SUCCESS;
648}
649
650
651/**
652 * @interface_method_impl{PDMDEVREG,pfnConstruct}
653 */
654DECLCALLBACK(int) gicR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
655{
656 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
657 PGICDEV pGicDev = PDMDEVINS_2_DATA(pDevIns, PGICDEV);
658 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
659 PVM pVM = PDMDevHlpGetVM(pDevIns);
660 PGIC pGic = VM_TO_GIC(pVM);
661 Assert(iInstance == 0);
662
663 /*
664 * Init the data.
665 */
666 pGic->pDevInsR3 = pDevIns;
667
668 /*
669 * Validate GIC settings.
670 */
671 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "DistributorMmioBase|RedistributorMmioBase|ItsMmioBase"
672 "|ArchRev"
673 "|ArchRevMinor"
674 "|MaxSpi"
675 "|ExtSpi"
676 "|MaxExtSpi"
677 "|ExtPpi"
678 "|MaxExtPpi"
679 "|RangeSel"
680 "|Nmi"
681 "|Mbi"
682 "|Aff3Levels"
683 "|Lpi"
684 "|MaxLpi", "");
685
686#if 0
687 /*
688 * Disable automatic PDM locking for this device.
689 */
690 int rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
691 AssertRCReturn(rc, rc);
692#endif
693
694 /** @devcfgm{gic, ArchRev, uint8_t, 3}
695 * Configures the GIC architecture revision (GICD_PIDR2.ArchRev, GICR_PIDR2.ArchRev
696 * and GITS_PIDR2.ArchRev).
697 *
698 * Currently we only support GICv3 and the architecture revision reported is the
699 * same for both the GIC and the ITS. */
700 int rc = pHlp->pfnCFGMQueryU8Def(pCfg, "ArchRev", &pGicDev->uArchRev, 3);
701 AssertLogRelRCReturn(rc, rc);
702 if (pGicDev->uArchRev == GIC_DIST_REG_PIDR2_ARCHREV_GICV3)
703 {
704 AssertCompile(GIC_DIST_REG_PIDR2_ARCHREV_GICV3 == GITS_CTRL_REG_PIDR2_ARCHREV_GICV3);
705 pGicDev->Gits.uArchRev = pGicDev->uArchRev;
706 }
707 else
708 return PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
709 N_("Configuration error: \"ArchRev\" must be %u, other revisions not supported"),
710 GIC_DIST_REG_PIDR2_ARCHREV_GICV3);
711
712 /** @devcfgm{gic, ArchRevMinor, uint8_t, 1}
713 * Configures the GIC architecture revision minor version.
714 *
715 * Currently we support GICv3.1 only. GICv3.1's only addition to GICv3 is supported
716 * for extended INTID ranges which we currently always support. */
717 rc = pHlp->pfnCFGMQueryU8Def(pCfg, "ArchRevMinor", &pGicDev->uArchRevMinor, 1);
718 AssertLogRelRCReturn(rc, rc);
719 if (pGicDev->uArchRevMinor == 1)
720 { /* likely */ }
721 else
722 return PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
723 N_("Configuration error: \"ArchRevMinor\" must be 1, other minor revisions not supported"));
724
725 /** @devcfgm{gic, MaxSpi, uint8_t, 31}
726 * Configures GICD_TYPER.ItLinesNumber.
727 *
728 * For the IntId range [32,1023], configures the maximum SPI supported. Valid values
729 * are [1,31] which equates to interrupt IDs [63,1023]. A value of 0 implies SPIs
730 * are not supported. We don't allow configuring this value as it's expected that
731 * most guests would assume support for SPIs. */
732 AssertCompile(GIC_DIST_REG_TYPER_NUM_ITLINES == 31);
733 /** @todo This currently isn't implemented and the full range is always
734 * reported to the guest. */
735 rc = pHlp->pfnCFGMQueryU8Def(pCfg, "MaxSpi", &pGicDev->uMaxSpi, 31 /* Upto and incl. IntId 1023 */);
736 AssertLogRelRCReturn(rc, rc);
737 if (pGicDev->uMaxSpi - 1 < 31)
738 { /* likely */ }
739 else
740 return PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
741 N_("Configuration error: \"MaxSpi\" must be in the range [1,%u]"),
742 GIC_DIST_REG_TYPER_NUM_ITLINES);
743
744 /** @devcfgm{gic, ExtSpi, bool, false}
745 * Configures whether extended SPIs supported is enabled (GICD_TYPER.ESPI). */
746 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "ExtSpi", &pGicDev->fExtSpi, true);
747 AssertLogRelRCReturn(rc, rc);
748
749 /** @devcfgm{gic, MaxExtSpi, uint8_t, 31}
750 * Configures GICD_TYPER.ESPI_range.
751 *
752 * For the extended SPI range [4096,5119], configures the maximum extended SPI
753 * supported. Valid values are [0,31] which equates to extended SPI IntIds
754 * [4127,5119]. This is ignored (set to 0 in the register) when extended SPIs are
755 * disabled. */
756 AssertCompile(GIC_DIST_REG_TYPER_ESPI_RANGE >> GIC_DIST_REG_TYPER_ESPI_RANGE_BIT == 31);
757 /** @todo This currently isn't implemented and the full range is always
758 * reported to the guest. */
759 rc = pHlp->pfnCFGMQueryU8Def(pCfg, "MaxExtSpi", &pGicDev->uMaxExtSpi, 31);
760 AssertLogRelRCReturn(rc, rc);
761 if (pGicDev->uMaxExtSpi <= 31)
762 { /* likely */ }
763 else
764 return PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
765 N_("Configuration error: \"MaxExtSpi\" must be in the range [0,31]"));
766
767 /** @devcfgm{gic, ExtPpi, bool, true}
768 * Configures whether extended PPIs support is enabled. */
769 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "ExtPpi", &pGicDev->fExtPpi, true);
770 AssertLogRelRCReturn(rc, rc);
771
772 /** @devcfgm{gic, MaxExtPpi, uint8_t, 2}
773 * Configures GICR_TYPER.PPInum.
774 *
775 * For the extended PPI range [1056,5119], configures the maximum extended PPI
776 * supported. Valid values are [1,2] which equates to extended PPI IntIds
777 * [1087,1119]. This is unused when extended PPIs are disabled. */
778 /** @todo This currently isn't implemented and the full range is always
779 * reported to the guest. */
780 rc = pHlp->pfnCFGMQueryU8Def(pCfg, "MaxExtPpi", &pGicDev->uMaxExtPpi, 2);
781 AssertLogRelRCReturn(rc, rc);
782 if ( pGicDev->uMaxExtPpi == GIC_REDIST_REG_TYPER_PPI_NUM_MAX_1087
783 || pGicDev->uMaxExtPpi == GIC_REDIST_REG_TYPER_PPI_NUM_MAX_1119)
784 { /* likely */ }
785 else
786 return PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
787 N_("Configuration error: \"MaxExtPpi\" must be in the range [0,2]"));
788
789 /** @devcfgm{gic, RangeSel, bool, true}
790 * Configures whether range-selector support is enabled (GICD_TYPER.RSS and
791 * ICC_CTLR_EL1.RSS). */
792 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "RangeSel", &pGicDev->fRangeSel, true);
793 AssertLogRelRCReturn(rc, rc);
794
795 /** @devcfgm{gic, Nmi, bool, false}
796 * Configures whether non-maskable interrupts (NMIs) are supported
797 * (GICD_TYPER.NMI). */
798 /** @todo NMIs are currently not implemented. */
799 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "Nmi", &pGicDev->fNmi, false);
800 AssertLogRelRCReturn(rc, rc);
801
802 /** @devcfgm{gic, Mbi, bool, false}
803 * Configures whether message-based interrupts (MBIs) are supported
804 * (GICD_TYPER.MBIS).
805 *
806 * Guests typically can't use MBIs without an ITS. */
807 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "Mbi", &pGicDev->fMbi, false);
808 AssertLogRelRCReturn(rc, rc);
809
810 /** @devcfgm{gic, Aff3Levels, bool, true}
811 * Configures whether non-zero affinity 3 levels (A3V) are supported
812 * (GICD_TYPER.A3V and ICC_CTLR.A3V). */
813 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "Aff3Levels", &pGicDev->fAff3Levels, true);
814 AssertLogRelRCReturn(rc, rc);
815
816 /** @devcfgm{gic, Lpi, bool, false}
817 * Configures whether physical LPIs are supported (GICD_TYPER.LPIS and
818 * GICR_TYPER.PLPIS).
819 *
820 * This currently requires an ITS as we do not support direction injection of
821 * LPIs as most guests do not use them anyway. */
822 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "Lpi", &pGicDev->fLpi, false);
823 AssertLogRelRCReturn(rc, rc);
824
825 /** @devcfgm{gic, MaxLpi, uint8_t, 14}
826 * Configures GICD_TYPER.num_LPIs.
827 *
828 * For the physical LPI range [8192,65535], configures the number of physical LPI
829 * supported. Valid values are [3,14] which equates to LPI IntIds 8192 to
830 * [8207,40959]. A value of 15 or higher would exceed the maximum INTID size of
831 * 16-bits since 8192 + 2^(NumLpi+1) is >= 73727. A value of 2 or lower support
832 * fewer than 15 LPIs which seem pointless and is hence disallowed. This value is
833 * ignored (set to 0 in the register) when LPIs are disabled. */
834 rc = pHlp->pfnCFGMQueryU8Def(pCfg, "MaxLpi", &pGicDev->uMaxLpi, 10);
835 AssertLogRelRCReturn(rc, rc);
836
837 /* We currently support 2048 LPIs until we need to support more. */
838 if (pGicDev->uMaxLpi == 10)
839 { /* likely */ }
840 else
841 return PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
842 N_("Configuration error: \"MaxLpi\" must be in the range [3,14]"));
843 AssertRelease(UINT32_C(2) << pGicDev->uMaxLpi <= RT_ELEMENTS(pGicDev->abLpiConfig));
844
845 /*
846 * Register the GIC with PDM.
847 */
848 rc = PDMDevHlpIcRegister(pDevIns);
849 AssertLogRelRCReturn(rc, rc);
850
851 rc = PDMGicRegisterBackend(pVM, PDMGICBACKENDTYPE_VBOX, &g_GicBackend);
852 AssertLogRelRCReturn(rc, rc);
853
854 /*
855 * Insert the GIC system registers.
856 */
857 for (uint32_t i = 0; i < RT_ELEMENTS(g_aSysRegRanges_GIC); i++)
858 {
859 rc = CPUMR3SysRegRangesInsert(pVM, &g_aSysRegRanges_GIC[i]);
860 AssertLogRelRCReturn(rc, rc);
861 }
862
863 /*
864 * Register the MMIO ranges.
865 */
866 /* Distributor. */
867 {
868 RTGCPHYS GCPhysMmioBase = 0;
869 rc = pHlp->pfnCFGMQueryU64(pCfg, "DistributorMmioBase", &GCPhysMmioBase);
870 if (RT_FAILURE(rc))
871 return PDMDEV_SET_ERROR(pDevIns, rc,
872 N_("Configuration error: Failed to get the \"DistributorMmioBase\" value"));
873
874 rc = PDMDevHlpMmioCreateAndMap(pDevIns, GCPhysMmioBase, GIC_DIST_REG_FRAME_SIZE, gicDistMmioWrite, gicDistMmioRead,
875 IOMMMIO_FLAGS_READ_DWORD | IOMMMIO_FLAGS_WRITE_DWORD_ZEROED, "GIC Distributor",
876 &pGicDev->hMmioDist);
877 AssertRCReturn(rc, rc);
878 }
879
880 /* Redistributor. */
881 {
882 RTGCPHYS GCPhysMmioBase = 0;
883 rc = pHlp->pfnCFGMQueryU64(pCfg, "RedistributorMmioBase", &GCPhysMmioBase);
884 if (RT_FAILURE(rc))
885 return PDMDEV_SET_ERROR(pDevIns, rc,
886 N_("Configuration error: Failed to get the \"RedistributorMmioBase\" value"));
887
888 RTGCPHYS const cbRegion = (RTGCPHYS)pVM->cCpus
889 * (GIC_REDIST_REG_FRAME_SIZE + GIC_REDIST_SGI_PPI_REG_FRAME_SIZE); /* Adjacent and per vCPU. */
890 rc = PDMDevHlpMmioCreateAndMap(pDevIns, GCPhysMmioBase, cbRegion, gicReDistMmioWrite, gicReDistMmioRead,
891 IOMMMIO_FLAGS_READ_DWORD | IOMMMIO_FLAGS_WRITE_DWORD_ZEROED, "GIC Redistributor",
892 &pGicDev->hMmioReDist);
893 AssertRCReturn(rc, rc);
894 }
895
896 /* ITS. */
897 {
898 RTGCPHYS GCPhysMmioBase = 0;
899 rc = pHlp->pfnCFGMQueryU64(pCfg, "ItsMmioBase", &GCPhysMmioBase);
900 if (RT_SUCCESS(rc))
901 {
902 Assert(pGicDev->hMmioGits != NIL_IOMMMIOHANDLE); /* paranoia */
903 RTGCPHYS const cbRegion = 2 * GITS_REG_FRAME_SIZE; /* 2 frames for GICv3. */
904 rc = PDMDevHlpMmioCreateAndMap(pDevIns, GCPhysMmioBase, cbRegion, gicItsMmioWrite, gicItsMmioRead,
905 IOMMMIO_FLAGS_READ_DWORD_QWORD
906 | IOMMMIO_FLAGS_WRITE_DWORD_QWORD_ZEROED
907 | IOMMMIO_FLAGS_DBGSTOP_ON_COMPLICATED_READ
908 | IOMMMIO_FLAGS_DBGSTOP_ON_COMPLICATED_WRITE,
909 "GIC ITS", &pGicDev->hMmioGits);
910 AssertLogRelRCReturn(rc, rc);
911
912 /* When the ITS is enabled we must support LPIs. */
913 if (!pGicDev->fLpi)
914 return PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
915 N_("Configuration error: \"Lpi\" must be enabled when ITS is enabled\n"));
916
917 /* Create ITS command-queue thread and semaphore. */
918 char szCmdQueueThread[32];
919 RT_ZERO(szCmdQueueThread);
920 RTStrPrintf(szCmdQueueThread, sizeof(szCmdQueueThread), "Gits-CmdQ-%u", iInstance);
921 rc = PDMDevHlpThreadCreate(pDevIns, &pGicDev->pCmdQueueThread, &pGicDev, gicItsR3CmdQueueThread,
922 gicItsR3CmdQueueThreadWakeUp, 0 /* cbStack */, RTTHREADTYPE_IO, szCmdQueueThread);
923 AssertLogRelRCReturn(rc, rc);
924
925 rc = PDMDevHlpSUPSemEventCreate(pDevIns, &pGicDev->hEvtCmdQueue);
926 AssertLogRelRCReturn(rc, rc);
927 }
928 else
929 {
930 pGicDev->hMmioGits = NIL_IOMMMIOHANDLE;
931
932 /* When the ITS is disabled we don't support LPIs as we do not support direct LPI injection (guests don't use it). */
933 if (pGicDev->fLpi)
934 return PDMDevHlpVMSetError(pDevIns, VERR_INVALID_PARAMETER, RT_SRC_POS,
935 N_("Configuration error: \"Lpi\" must be disabled when ITS is disabled\n"));
936 }
937 }
938
939 /*
940 * Register saved state callbacks.
941 */
942 rc = PDMDevHlpSSMRegister(pDevIns, GIC_SAVED_STATE_VERSION, 0, gicR3SaveExec, gicR3LoadExec);
943 AssertRCReturn(rc, rc);
944
945 /*
946 * Register debugger info callbacks.
947 *
948 * We use separate callbacks rather than arguments so they can also be
949 * dumped in an automated fashion while collecting crash diagnostics and
950 * not just used during live debugging via the VM debugger.
951 */
952 DBGFR3InfoRegisterInternalEx(pVM, "gic", "Dumps GIC basic information.", gicR3DbgInfo, DBGFINFO_FLAGS_ALL_EMTS);
953 DBGFR3InfoRegisterInternalEx(pVM, "gicdist", "Dumps GIC distributor information.", gicR3DbgInfoDist, DBGFINFO_FLAGS_ALL_EMTS);
954 DBGFR3InfoRegisterInternalEx(pVM, "gicredist", "Dumps GIC redistributor information.", gicR3DbgInfoReDist, DBGFINFO_FLAGS_ALL_EMTS);
955 DBGFR3InfoRegisterInternalEx(pVM, "gicits", "Dumps GIC ITS information.", gicR3DbgInfoIts, DBGFINFO_FLAGS_ALL_EMTS);
956
957 /*
958 * Statistics.
959 */
960#ifdef VBOX_WITH_STATISTICS
961# define GIC_REG_COUNTER(a_pvReg, a_pszNameFmt, a_pszDesc) \
962 PDMDevHlpSTAMRegisterF(pDevIns, a_pvReg, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_OCCURENCES, \
963 a_pszDesc, a_pszNameFmt, idCpu)
964# define GIC_PROF_COUNTER(a_pvReg, a_pszNameFmt, a_pszDesc) \
965 PDMDevHlpSTAMRegisterF(pDevIns, a_pvReg, STAMTYPE_PROFILE, STAMVISIBILITY_ALWAYS, STAMUNIT_TICKS_PER_CALL, \
966 a_pszDesc, a_pszNameFmt, idCpu)
967
968 for (VMCPUID idCpu = 0; idCpu < pVM->cCpus; idCpu++)
969 {
970 PVMCPU pVCpu = pVM->apCpusR3[idCpu];
971 PGICCPU pGicCpu = VMCPU_TO_GICCPU(pVCpu);
972
973 GIC_REG_COUNTER(&pGicCpu->StatMmioReadR3, "%u/MmioRead", "Number of MMIO reads in R3.");
974 GIC_REG_COUNTER(&pGicCpu->StatMmioWriteR3, "%u/MmioWrite", "Number of MMIO writes in R3.");
975 GIC_REG_COUNTER(&pGicCpu->StatSysRegReadR3, "%u/SysRegRead", "Number of system register reads in R3.");
976 GIC_REG_COUNTER(&pGicCpu->StatSysRegWriteR3, "%u/SysRegWrite", "Number of system register writes in R3.");
977 GIC_REG_COUNTER(&pGicCpu->StatSetSpiR3, "%u/SetSpi", "Number of set SPI callbacks in R3.");
978 GIC_REG_COUNTER(&pGicCpu->StatSetPpiR3, "%u/SetPpi", "Number of set PPI callbacks in R3.");
979 GIC_REG_COUNTER(&pGicCpu->StatSetSgiR3, "%u/SetSgi", "Number of SGIs generated in R3.");
980
981 GIC_PROF_COUNTER(&pGicCpu->StatProfIntrAckR3, "%u/Prof/IntrAck", "Profiling of interrupt acknowledge (IAR) in R3.");
982 GIC_PROF_COUNTER(&pGicCpu->StatProfSetSpiR3, "%u/Prof/SetSpi", "Profiling of set SPI callback in R3.");
983 GIC_PROF_COUNTER(&pGicCpu->StatProfSetPpiR3, "%u/Prof/SetPpi", "Profiling of set PPI callback in R3.");
984 GIC_PROF_COUNTER(&pGicCpu->StatProfSetSgiR3, "%u/Prof/SetSgi", "Profiling of SGIs generated in R3.");
985 }
986# undef GIC_REG_COUNTER
987# undef GIC_PROF_COUNTER
988#endif
989
990 gicR3Reset(pDevIns);
991
992 /*
993 * Log some of the features exposed to software.
994 */
995 uint8_t const uArchRev = pGicDev->uArchRev;
996 uint8_t const uArchRevMinor = pGicDev->uArchRevMinor;
997 uint8_t const uMaxSpi = pGicDev->uMaxSpi;
998 bool const fExtSpi = pGicDev->fExtSpi;
999 uint8_t const uMaxExtSpi = pGicDev->uMaxExtSpi;
1000 bool const fExtPpi = pGicDev->fExtPpi;
1001 uint8_t const uMaxExtPpi = pGicDev->uMaxExtPpi;
1002 bool const fRangeSel = pGicDev->fRangeSel;
1003 bool const fNmi = pGicDev->fNmi;
1004 bool const fMbi = pGicDev->fMbi;
1005 bool const fAff3Levels = pGicDev->fAff3Levels;
1006 bool const fLpi = pGicDev->fLpi;
1007 uint32_t const uMaxLpi = pGicDev->uMaxLpi;
1008 uint16_t const uExtPpiLast = uMaxExtPpi == GIC_REDIST_REG_TYPER_PPI_NUM_MAX_1087 ? 1087 : GIC_INTID_RANGE_EXT_PPI_LAST;
1009 LogRel(("GIC: ArchRev=%u.%u RangeSel=%RTbool Nmi=%RTbool Mbi=%RTbool Aff3Levels=%RTbool\n",
1010 uArchRev, uArchRevMinor, fRangeSel, fNmi, fMbi, fAff3Levels));
1011 LogRel(("GIC: SPIs=true (%u:32..%u) ExtSPIs=%RTbool (%u:4095..%u) ExtPPIs=%RTbool (%u:1056..%u)\n",
1012 uMaxSpi, 32 * (uMaxSpi + 1),
1013 fExtSpi, uMaxExtSpi, GIC_INTID_RANGE_EXT_SPI_START - 1 + 32 * (uMaxExtSpi + 1),
1014 fExtPpi, uMaxExtPpi, uExtPpiLast));
1015 LogRel(("GIC: ITS=%s LPIs=%RTbool (%u:%u..%u)\n",
1016 pGicDev->hMmioGits != NIL_IOMMMIOHANDLE ? "enabled" : "disabled", fLpi,
1017 uMaxLpi, GIC_INTID_RANGE_LPI_START, GIC_INTID_RANGE_LPI_START - 1 + (UINT32_C(2) << uMaxLpi)));
1018 return VINF_SUCCESS;
1019}
1020
1021#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
1022
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette