VirtualBox

source: vbox/trunk/src/VBox/Devices/Bus/DevIommuIntel.cpp@ 88808

最後變更 在這個檔案從88808是 88808,由 vboxsync 提交於 4 年 前

Intel IOMMU: bugref:9967 DbgInfo.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 120.7 KB
 
1/* $Id: DevIommuIntel.cpp 88808 2021-04-30 14:09:50Z vboxsync $ */
2/** @file
3 * IOMMU - Input/Output Memory Management Unit - Intel implementation.
4 */
5
6/*
7 * Copyright (C) 2021 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DEV_IOMMU
23#include "VBoxDD.h"
24#include "DevIommuIntel.h"
25
26#include <iprt/mem.h>
27#include <iprt/string.h>
28
29
30/*********************************************************************************************************************************
31* Defined Constants And Macros *
32*********************************************************************************************************************************/
33/** Gets the low uint32_t of a uint64_t or something equivalent.
34 *
35 * This is suitable for casting constants outside code (since RT_LO_U32 can't be
36 * used as it asserts for correctness when compiling on certain compilers). */
37#define DMAR_LO_U32(a) (uint32_t)(UINT32_MAX & (a))
38
39/** Gets the high uint32_t of a uint64_t or something equivalent.
40 *
41 * This is suitable for casting constants outside code (since RT_HI_U32 can't be
42 * used as it asserts for correctness when compiling on certain compilers). */
43#define DMAR_HI_U32(a) (uint32_t)((a) >> 32)
44
45/** Asserts MMIO access' offset and size are valid or returns appropriate error
46 * code suitable for returning from MMIO access handlers. */
47#define DMAR_ASSERT_MMIO_ACCESS_RET(a_off, a_cb) \
48 do { \
49 AssertReturn((a_cb) == 4 || (a_cb) == 8, VINF_IOM_MMIO_UNUSED_FF); \
50 AssertReturn(!((a_off) & ((a_cb) - 1)), VINF_IOM_MMIO_UNUSED_FF); \
51 } while (0)
52
53/** Checks if the MMIO offset is valid. */
54#define DMAR_IS_MMIO_OFF_VALID(a_off) ( (a_off) < DMAR_MMIO_GROUP_0_OFF_END \
55 || (a_off) - DMAR_MMIO_GROUP_1_OFF_FIRST < DMAR_MMIO_GROUP_1_SIZE)
56
57/** Acquires the DMAR lock but returns with the given error code on failure. */
58#define DMAR_LOCK_RET(a_pDevIns, a_pThisCC, a_rcBusy) \
59 do { \
60 if ((a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnLock((a_pDevIns), (a_rcBusy)) == VINF_SUCCESS) \
61 { /* likely */ } \
62 else \
63 return (a_rcBusy); \
64 } while (0)
65
66/** Acquires the DMAR lock and is not expected to fail. */
67#define DMAR_LOCK(a_pDevIns, a_pThisCC) \
68 do { \
69 int const rcLock = (a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnLock((a_pDevIns), VERR_IGNORED); \
70 Assert(rcLock == VINF_SUCCESS); \
71 } while (0)
72
73/** Release the DMAR lock. */
74#define DMAR_UNLOCK(a_pDevIns, a_pThisCC) (a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnUnlock(a_pDevIns)
75
76/** Asserts that the calling thread owns the DMAR lock. */
77#define DMAR_ASSERT_LOCK_IS_OWNER(a_pDevIns, a_pThisCC) \
78 do { \
79 Assert((a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnLockIsOwner(a_pDevIns)); \
80 RT_NOREF1(a_pThisCC); \
81 } while (0)
82
83/** Asserts that the calling thread does not own the DMAR lock. */
84#define DMAR_ASSERT_LOCK_IS_NOT_OWNER(a_pDevIns, a_pThisCC) \
85 do { \
86 Assert((a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnLockIsOwner(a_pDevIns) == false); \
87 RT_NOREF1(a_pThisCC); \
88 } while (0)
89
90/** The number of fault recording registers our implementation supports.
91 * Normal guest operation shouldn't trigger faults anyway, so we only support the
92 * minimum number of registers (which is 1).
93 *
94 * See Intel VT-d spec. 10.4.2 "Capability Register" (CAP_REG.NFR). */
95#define DMAR_FRCD_REG_COUNT UINT32_C(1)
96
97/** Offset of first register in group 0. */
98#define DMAR_MMIO_GROUP_0_OFF_FIRST VTD_MMIO_OFF_VER_REG
99/** Offset of last register in group 0 (inclusive). */
100#define DMAR_MMIO_GROUP_0_OFF_LAST VTD_MMIO_OFF_MTRR_PHYSMASK9_REG
101/** Last valid offset in group 0 (exclusive). */
102#define DMAR_MMIO_GROUP_0_OFF_END (DMAR_MMIO_GROUP_0_OFF_LAST + 8 /* sizeof MTRR_PHYSMASK9_REG */)
103/** Size of the group 0 (in bytes). */
104#define DMAR_MMIO_GROUP_0_SIZE (DMAR_MMIO_GROUP_0_OFF_END - DMAR_MMIO_GROUP_0_OFF_FIRST)
105/**< Implementation-specific MMIO offset of IVA_REG. */
106#define DMAR_MMIO_OFF_IVA_REG 0xe50
107/**< Implementation-specific MMIO offset of IOTLB_REG. */
108#define DMAR_MMIO_OFF_IOTLB_REG 0xe58
109/**< Implementation-specific MMIO offset of FRCD_LO_REG. */
110#define DMAR_MMIO_OFF_FRCD_LO_REG 0xe70
111/**< Implementation-specific MMIO offset of FRCD_HI_REG. */
112#define DMAR_MMIO_OFF_FRCD_HI_REG 0xe78
113AssertCompile(!(DMAR_MMIO_OFF_FRCD_LO_REG & 0xf));
114
115/** Offset of first register in group 1. */
116#define DMAR_MMIO_GROUP_1_OFF_FIRST VTD_MMIO_OFF_VCCAP_REG
117/** Offset of last register in group 1 (inclusive). */
118#define DMAR_MMIO_GROUP_1_OFF_LAST (DMAR_MMIO_OFF_FRCD_LO_REG + 8) * DMAR_FRCD_REG_COUNT
119/** Last valid offset in group 1 (exclusive). */
120#define DMAR_MMIO_GROUP_1_OFF_END (DMAR_MMIO_GROUP_1_OFF_LAST + 8 /* sizeof FRCD_HI_REG */)
121/** Size of the group 1 (in bytes). */
122#define DMAR_MMIO_GROUP_1_SIZE (DMAR_MMIO_GROUP_1_OFF_END - DMAR_MMIO_GROUP_1_OFF_FIRST)
123
124/** DMAR implementation's major version number (exposed to software).
125 * We report 6 as the major version since we support queued-invalidations as
126 * software may make assumptions based on that.
127 *
128 * See Intel VT-d spec. 10.4.7 "Context Command Register" (CCMD_REG.CAIG). */
129#define DMAR_VER_MAJOR 6
130/** DMAR implementation's minor version number (exposed to software). */
131#define DMAR_VER_MINOR 0
132
133/** Release log prefix string. */
134#define DMAR_LOG_PFX "Intel-IOMMU"
135/** The current saved state version. */
136#define DMAR_SAVED_STATE_VERSION 1
137
138
139/*********************************************************************************************************************************
140* Structures and Typedefs *
141*********************************************************************************************************************************/
142/**
143 * DMAR error diagnostics.
144 * Sorted alphabetically so it's easier to add and locate items, no other reason.
145 *
146 * @note Members of this enum are used as array indices, so no gaps in enum
147 * values are not allowed. Update g_apszDmarDiagDesc when you modify
148 * fields in this enum.
149 */
150typedef enum
151{
152 kDmarDiag_None = 0,
153 kDmarDiag_CcmdReg_NotSupported,
154 kDmarDiag_CcmdReg_Qi_Enabled,
155 kDmarDiag_CcmdReg_Ttm_Invalid,
156 kDmarDiag_IqaReg_Dsc_Fetch_Error,
157 kDmarDiag_IqaReg_Dw_128_Invalid,
158 kDmarDiag_IqaReg_Dw_256_Invalid,
159 kDmarDiag_Iqei_Dsc_Type_Invalid,
160 kDmarDiag_Iqei_Inv_Wait_Dsc_0_1_Rsvd,
161 kDmarDiag_Iqei_Inv_Wait_Dsc_2_3_Rsvd,
162 kDmarDiag_Iqei_Inv_Wait_Dsc_Invalid,
163 kDmarDiag_Iqei_Ttm_Rsvd,
164 kDmarDiag_IqtReg_Qt_Invalid,
165 kDmarDiag_IqtReg_Qt_NotAligned,
166 /* Member for determining array index limit. */
167 kDmarDiag_End,
168 /* Type size hack. */
169 kDmarDiag_32Bit_Hack = 0x7fffffff
170} DMARDIAG;
171AssertCompileSize(DMARDIAG, 4);
172
173/** DMAR diagnostic enum description expansion.
174 * The below construct ensures typos in the input to this macro are caught
175 * during compile time. */
176#define DMARDIAG_DESC(a_Name) RT_CONCAT(kDmarDiag_, a_Name) < kDmarDiag_End ? RT_STR(a_Name) : "Ignored"
177
178/** DMAR diagnostics description for members in DMARDIAG. */
179static const char *const g_apszDmarDiagDesc[] =
180{
181 DMARDIAG_DESC(None ),
182 DMARDIAG_DESC(CcmdReg_NotSupported ),
183 DMARDIAG_DESC(CcmdReg_Qi_Enabled ),
184 DMARDIAG_DESC(CcmdReg_Ttm_Invalid ),
185 DMARDIAG_DESC(IqaReg_Dsc_Fetch_Error ),
186 DMARDIAG_DESC(IqaReg_Dw_128_Invalid ),
187 DMARDIAG_DESC(IqaReg_Dw_256_Invalid ),
188 DMARDIAG_DESC(Iqei_Dsc_Type_Invalid ),
189 DMARDIAG_DESC(Iqei_Inv_Wait_Dsc_0_1_Rsvd),
190 DMARDIAG_DESC(Iqei_Inv_Wait_Dsc_2_3_Rsvd),
191 DMARDIAG_DESC(Iqei_Inv_Wait_Dsc_Invalid ),
192 DMARDIAG_DESC(Iqei_Ttm_Rsvd ),
193 DMARDIAG_DESC(IqtReg_Qt_Invalid ),
194 DMARDIAG_DESC(IqtReg_Qt_NotAligned )
195 /* kDmarDiag_End */
196};
197AssertCompile(RT_ELEMENTS(g_apszDmarDiagDesc) == kDmarDiag_End);
198#undef DMARDIAG_DESC
199
200/**
201 * The shared DMAR device state.
202 */
203typedef struct DMAR
204{
205 /** IOMMU device index. */
206 uint32_t idxIommu;
207 /** DMAR magic. */
208 uint32_t u32Magic;
209
210 /** Registers (group 0). */
211 uint8_t abRegs0[DMAR_MMIO_GROUP_0_SIZE];
212 /** Registers (group 1). */
213 uint8_t abRegs1[DMAR_MMIO_GROUP_1_SIZE];
214
215 /** @name Lazily activated registers.
216 * These are the active values for lazily activated registers. Software is free to
217 * modify the actual register values while remapping/translation is enabled but they
218 * take effect only when explicitly signaled by software, hence we need to hold the
219 * active values separately.
220 * @{ */
221 /** Currently active IRTA_REG. */
222 uint64_t uIrtaReg;
223 /** Currently active RTADDR_REG. */
224 uint64_t uRtaddrReg;
225 /** @} */
226
227 /** @name Register copies for a tiny bit faster and more convenient access.
228 * @{ */
229 /** Copy of VER_REG. */
230 uint8_t uVerReg;
231 /** Alignment. */
232 uint8_t abPadding[7];
233 /** Copy of CAP_REG. */
234 uint64_t fCapReg;
235 /** Copy of ECAP_REG. */
236 uint64_t fExtCapReg;
237 /** @} */
238
239 /** The event semaphore the invalidation-queue thread waits on. */
240 SUPSEMEVENT hEvtInvQueue;
241 /** Whether the invalidation-queue thread has been signaled. */
242 bool volatile fInvQueueThreadSignaled;
243 /** Padding. */
244 bool afPadding0[3];
245 /** Error diagnostic. */
246 DMARDIAG enmDiag;
247 /** The MMIO handle. */
248 IOMMMIOHANDLE hMmio;
249
250#ifdef VBOX_WITH_STATISTICS
251 STAMCOUNTER StatMmioReadR3; /**< Number of MMIO reads in R3. */
252 STAMCOUNTER StatMmioReadRZ; /**< Number of MMIO reads in RZ. */
253 STAMCOUNTER StatMmioWriteR3; /**< Number of MMIO writes in R3. */
254 STAMCOUNTER StatMmioWriteRZ; /**< Number of MMIO writes in RZ. */
255
256 STAMCOUNTER StatMsiRemapR3; /**< Number of MSI remap requests in R3. */
257 STAMCOUNTER StatMsiRemapRZ; /**< Number of MSI remap requests in RZ. */
258
259 STAMCOUNTER StatMemReadR3; /**< Number of memory read translation requests in R3. */
260 STAMCOUNTER StatMemReadRZ; /**< Number of memory read translation requests in RZ. */
261 STAMCOUNTER StatMemWriteR3; /**< Number of memory write translation requests in R3. */
262 STAMCOUNTER StatMemWriteRZ; /**< Number of memory write translation requests in RZ. */
263
264 STAMCOUNTER StatMemBulkReadR3; /**< Number of memory read bulk translation requests in R3. */
265 STAMCOUNTER StatMemBulkReadRZ; /**< Number of memory read bulk translation requests in RZ. */
266 STAMCOUNTER StatMemBulkWriteR3; /**< Number of memory write bulk translation requests in R3. */
267 STAMCOUNTER StatMemBulkWriteRZ; /**< Number of memory write bulk translation requests in RZ. */
268
269 STAMCOUNTER StatCcInvDsc; /**< Number of Context-cache descriptors processed. */
270 STAMCOUNTER StatIotlbInvDsc; /**< Number of IOTLB descriptors processed. */
271 STAMCOUNTER StatDevtlbInvDsc; /**< Number of Device-TLB descriptors processed. */
272 STAMCOUNTER StatIecInvDsc; /**< Number of Interrupt-Entry cache descriptors processed. */
273 STAMCOUNTER StatInvWaitDsc; /**< Number of Invalidation wait descriptors processed. */
274 STAMCOUNTER StatPasidIotlbInvDsc; /**< Number of PASID-based IOTLB descriptors processed. */
275 STAMCOUNTER StatPasidCacheInvDsc; /**< Number of PASID-cache descriptors processed. */
276 STAMCOUNTER StatPasidDevtlbInvDsc; /**< Number of PASID-based device-TLB descriptors processed. */
277#endif
278} DMAR;
279/** Pointer to the DMAR device state. */
280typedef DMAR *PDMAR;
281/** Pointer to the const DMAR device state. */
282typedef DMAR const *PCDMAR;
283AssertCompileMemberAlignment(DMAR, abRegs0, 8);
284AssertCompileMemberAlignment(DMAR, abRegs1, 8);
285
286/**
287 * The ring-3 DMAR device state.
288 */
289typedef struct DMARR3
290{
291 /** Device instance. */
292 PPDMDEVINSR3 pDevInsR3;
293 /** The IOMMU helper. */
294 R3PTRTYPE(PCPDMIOMMUHLPR3) pIommuHlpR3;
295 /** The invalidation-queue thread. */
296 R3PTRTYPE(PPDMTHREAD) pInvQueueThread;
297} DMARR3;
298/** Pointer to the ring-3 DMAR device state. */
299typedef DMARR3 *PDMARR3;
300/** Pointer to the const ring-3 DMAR device state. */
301typedef DMARR3 const *PCDMARR3;
302
303/**
304 * The ring-0 DMAR device state.
305 */
306typedef struct DMARR0
307{
308 /** Device instance. */
309 PPDMDEVINSR0 pDevInsR0;
310 /** The IOMMU helper. */
311 R0PTRTYPE(PCPDMIOMMUHLPR0) pIommuHlpR0;
312} DMARR0;
313/** Pointer to the ring-0 IOMMU device state. */
314typedef DMARR0 *PDMARR0;
315/** Pointer to the const ring-0 IOMMU device state. */
316typedef DMARR0 const *PCDMARR0;
317
318/**
319 * The raw-mode DMAR device state.
320 */
321typedef struct DMARRC
322{
323 /** Device instance. */
324 PPDMDEVINSRC pDevInsRC;
325 /** The IOMMU helper. */
326 RCPTRTYPE(PCPDMIOMMUHLPRC) pIommuHlpRC;
327} DMARRC;
328/** Pointer to the raw-mode DMAR device state. */
329typedef DMARRC *PDMARRC;
330/** Pointer to the const raw-mode DMAR device state. */
331typedef DMARRC const *PCIDMARRC;
332
333/** The DMAR device state for the current context. */
334typedef CTX_SUFF(DMAR) DMARCC;
335/** Pointer to the DMAR device state for the current context. */
336typedef CTX_SUFF(PDMAR) PDMARCC;
337/** Pointer to the const DMAR device state for the current context. */
338typedef CTX_SUFF(PDMAR) const PCDMARCC;
339
340
341/*********************************************************************************************************************************
342* Global Variables *
343*********************************************************************************************************************************/
344/**
345 * Read-write masks for DMAR registers (group 0).
346 */
347static uint32_t const g_au32RwMasks0[] =
348{
349 /* Offset Register Low High */
350 /* 0x000 VER_REG */ VTD_VER_REG_RW_MASK,
351 /* 0x004 Reserved */ 0,
352 /* 0x008 CAP_REG */ DMAR_LO_U32(VTD_CAP_REG_RW_MASK), DMAR_HI_U32(VTD_CAP_REG_RW_MASK),
353 /* 0x010 ECAP_REG */ DMAR_LO_U32(VTD_ECAP_REG_RW_MASK), DMAR_HI_U32(VTD_ECAP_REG_RW_MASK),
354 /* 0x018 GCMD_REG */ VTD_GCMD_REG_RW_MASK,
355 /* 0x01c GSTS_REG */ VTD_GSTS_REG_RW_MASK,
356 /* 0x020 RTADDR_REG */ DMAR_LO_U32(VTD_RTADDR_REG_RW_MASK), DMAR_HI_U32(VTD_RTADDR_REG_RW_MASK),
357 /* 0x028 CCMD_REG */ DMAR_LO_U32(VTD_CCMD_REG_RW_MASK), DMAR_HI_U32(VTD_CCMD_REG_RW_MASK),
358 /* 0x030 Reserved */ 0,
359 /* 0x034 FSTS_REG */ VTD_FSTS_REG_RW_MASK,
360 /* 0x038 FECTL_REG */ VTD_FECTL_REG_RW_MASK,
361 /* 0x03c FEDATA_REG */ VTD_FEDATA_REG_RW_MASK,
362 /* 0x040 FEADDR_REG */ VTD_FEADDR_REG_RW_MASK,
363 /* 0x044 FEUADDR_REG */ VTD_FEUADDR_REG_RW_MASK,
364 /* 0x048 Reserved */ 0, 0,
365 /* 0x050 Reserved */ 0, 0,
366 /* 0x058 AFLOG_REG */ DMAR_LO_U32(VTD_AFLOG_REG_RW_MASK), DMAR_HI_U32(VTD_AFLOG_REG_RW_MASK),
367 /* 0x060 Reserved */ 0,
368 /* 0x064 PMEN_REG */ 0, /* RO as we don't support PLMR and PHMR. */
369 /* 0x068 PLMBASE_REG */ 0, /* RO as we don't support PLMR. */
370 /* 0x06c PLMLIMIT_REG */ 0, /* RO as we don't support PLMR. */
371 /* 0x070 PHMBASE_REG */ 0, 0, /* RO as we don't support PHMR. */
372 /* 0x078 PHMLIMIT_REG */ 0, 0, /* RO as we don't support PHMR. */
373 /* 0x080 IQH_REG */ DMAR_LO_U32(VTD_IQH_REG_RW_MASK), DMAR_HI_U32(VTD_IQH_REG_RW_MASK),
374 /* 0x088 IQT_REG */ DMAR_LO_U32(VTD_IQT_REG_RW_MASK), DMAR_HI_U32(VTD_IQT_REG_RW_MASK),
375 /* 0x090 IQA_REG */ DMAR_LO_U32(VTD_IQA_REG_RW_MASK), DMAR_HI_U32(VTD_IQA_REG_RW_MASK),
376 /* 0x098 Reserved */ 0,
377 /* 0x09c ICS_REG */ VTD_ICS_REG_RW_MASK,
378 /* 0x0a0 IECTL_REG */ VTD_IECTL_REG_RW_MASK,
379 /* 0x0a4 IEDATA_REG */ VTD_IEDATA_REG_RW_MASK,
380 /* 0x0a8 IEADDR_REG */ VTD_IEADDR_REG_RW_MASK,
381 /* 0x0ac IEUADDR_REG */ VTD_IEUADDR_REG_RW_MASK,
382 /* 0x0b0 IQERCD_REG */ DMAR_LO_U32(VTD_IQERCD_REG_RW_MASK), DMAR_HI_U32(VTD_IQERCD_REG_RW_MASK),
383 /* 0x0b8 IRTA_REG */ DMAR_LO_U32(VTD_IRTA_REG_RW_MASK), DMAR_HI_U32(VTD_IRTA_REG_RW_MASK),
384 /* 0x0c0 PQH_REG */ DMAR_LO_U32(VTD_PQH_REG_RW_MASK), DMAR_HI_U32(VTD_PQH_REG_RW_MASK),
385 /* 0x0c8 PQT_REG */ DMAR_LO_U32(VTD_PQT_REG_RW_MASK), DMAR_HI_U32(VTD_PQT_REG_RW_MASK),
386 /* 0x0d0 PQA_REG */ DMAR_LO_U32(VTD_PQA_REG_RW_MASK), DMAR_HI_U32(VTD_PQA_REG_RW_MASK),
387 /* 0x0d8 Reserved */ 0,
388 /* 0x0dc PRS_REG */ VTD_PRS_REG_RW_MASK,
389 /* 0x0e0 PECTL_REG */ VTD_PECTL_REG_RW_MASK,
390 /* 0x0e4 PEDATA_REG */ VTD_PEDATA_REG_RW_MASK,
391 /* 0x0e8 PEADDR_REG */ VTD_PEADDR_REG_RW_MASK,
392 /* 0x0ec PEUADDR_REG */ VTD_PEUADDR_REG_RW_MASK,
393 /* 0x0f0 Reserved */ 0, 0,
394 /* 0x0f8 Reserved */ 0, 0,
395 /* 0x100 MTRRCAP_REG */ DMAR_LO_U32(VTD_MTRRCAP_REG_RW_MASK), DMAR_HI_U32(VTD_MTRRCAP_REG_RW_MASK),
396 /* 0x108 MTRRDEF_REG */ 0, 0, /* RO as we don't support MTS. */
397 /* 0x110 Reserved */ 0, 0,
398 /* 0x118 Reserved */ 0, 0,
399 /* 0x120 MTRR_FIX64_00000_REG */ 0, 0, /* RO as we don't support MTS. */
400 /* 0x128 MTRR_FIX16K_80000_REG */ 0, 0,
401 /* 0x130 MTRR_FIX16K_A0000_REG */ 0, 0,
402 /* 0x138 MTRR_FIX4K_C0000_REG */ 0, 0,
403 /* 0x140 MTRR_FIX4K_C8000_REG */ 0, 0,
404 /* 0x148 MTRR_FIX4K_D0000_REG */ 0, 0,
405 /* 0x150 MTRR_FIX4K_D8000_REG */ 0, 0,
406 /* 0x158 MTRR_FIX4K_E0000_REG */ 0, 0,
407 /* 0x160 MTRR_FIX4K_E8000_REG */ 0, 0,
408 /* 0x168 MTRR_FIX4K_F0000_REG */ 0, 0,
409 /* 0x170 MTRR_FIX4K_F8000_REG */ 0, 0,
410 /* 0x178 Reserved */ 0, 0,
411 /* 0x180 MTRR_PHYSBASE0_REG */ 0, 0, /* RO as we don't support MTS. */
412 /* 0x188 MTRR_PHYSMASK0_REG */ 0, 0,
413 /* 0x190 MTRR_PHYSBASE1_REG */ 0, 0,
414 /* 0x198 MTRR_PHYSMASK1_REG */ 0, 0,
415 /* 0x1a0 MTRR_PHYSBASE2_REG */ 0, 0,
416 /* 0x1a8 MTRR_PHYSMASK2_REG */ 0, 0,
417 /* 0x1b0 MTRR_PHYSBASE3_REG */ 0, 0,
418 /* 0x1b8 MTRR_PHYSMASK3_REG */ 0, 0,
419 /* 0x1c0 MTRR_PHYSBASE4_REG */ 0, 0,
420 /* 0x1c8 MTRR_PHYSMASK4_REG */ 0, 0,
421 /* 0x1d0 MTRR_PHYSBASE5_REG */ 0, 0,
422 /* 0x1d8 MTRR_PHYSMASK5_REG */ 0, 0,
423 /* 0x1e0 MTRR_PHYSBASE6_REG */ 0, 0,
424 /* 0x1e8 MTRR_PHYSMASK6_REG */ 0, 0,
425 /* 0x1f0 MTRR_PHYSBASE7_REG */ 0, 0,
426 /* 0x1f8 MTRR_PHYSMASK7_REG */ 0, 0,
427 /* 0x200 MTRR_PHYSBASE8_REG */ 0, 0,
428 /* 0x208 MTRR_PHYSMASK8_REG */ 0, 0,
429 /* 0x210 MTRR_PHYSBASE9_REG */ 0, 0,
430 /* 0x218 MTRR_PHYSMASK9_REG */ 0, 0,
431};
432AssertCompile(sizeof(g_au32RwMasks0) == DMAR_MMIO_GROUP_0_SIZE);
433
434/**
435 * Read-only Status, Write-1-to-clear masks for DMAR registers (group 0).
436 */
437static uint32_t const g_au32Rw1cMasks0[] =
438{
439 /* Offset Register Low High */
440 /* 0x000 VER_REG */ 0,
441 /* 0x004 Reserved */ 0,
442 /* 0x008 CAP_REG */ 0, 0,
443 /* 0x010 ECAP_REG */ 0, 0,
444 /* 0x018 GCMD_REG */ 0,
445 /* 0x01c GSTS_REG */ 0,
446 /* 0x020 RTADDR_REG */ 0, 0,
447 /* 0x028 CCMD_REG */ 0, 0,
448 /* 0x030 Reserved */ 0,
449 /* 0x034 FSTS_REG */ VTD_FSTS_REG_RW1C_MASK,
450 /* 0x038 FECTL_REG */ 0,
451 /* 0x03c FEDATA_REG */ 0,
452 /* 0x040 FEADDR_REG */ 0,
453 /* 0x044 FEUADDR_REG */ 0,
454 /* 0x048 Reserved */ 0, 0,
455 /* 0x050 Reserved */ 0, 0,
456 /* 0x058 AFLOG_REG */ 0, 0,
457 /* 0x060 Reserved */ 0,
458 /* 0x064 PMEN_REG */ 0,
459 /* 0x068 PLMBASE_REG */ 0,
460 /* 0x06c PLMLIMIT_REG */ 0,
461 /* 0x070 PHMBASE_REG */ 0, 0,
462 /* 0x078 PHMLIMIT_REG */ 0, 0,
463 /* 0x080 IQH_REG */ 0, 0,
464 /* 0x088 IQT_REG */ 0, 0,
465 /* 0x090 IQA_REG */ 0, 0,
466 /* 0x098 Reserved */ 0,
467 /* 0x09c ICS_REG */ VTD_ICS_REG_RW1C_MASK,
468 /* 0x0a0 IECTL_REG */ 0,
469 /* 0x0a4 IEDATA_REG */ 0,
470 /* 0x0a8 IEADDR_REG */ 0,
471 /* 0x0ac IEUADDR_REG */ 0,
472 /* 0x0b0 IQERCD_REG */ 0, 0,
473 /* 0x0b8 IRTA_REG */ 0, 0,
474 /* 0x0c0 PQH_REG */ 0, 0,
475 /* 0x0c8 PQT_REG */ 0, 0,
476 /* 0x0d0 PQA_REG */ 0, 0,
477 /* 0x0d8 Reserved */ 0,
478 /* 0x0dc PRS_REG */ 0,
479 /* 0x0e0 PECTL_REG */ 0,
480 /* 0x0e4 PEDATA_REG */ 0,
481 /* 0x0e8 PEADDR_REG */ 0,
482 /* 0x0ec PEUADDR_REG */ 0,
483 /* 0x0f0 Reserved */ 0, 0,
484 /* 0x0f8 Reserved */ 0, 0,
485 /* 0x100 MTRRCAP_REG */ 0, 0,
486 /* 0x108 MTRRDEF_REG */ 0, 0,
487 /* 0x110 Reserved */ 0, 0,
488 /* 0x118 Reserved */ 0, 0,
489 /* 0x120 MTRR_FIX64_00000_REG */ 0, 0,
490 /* 0x128 MTRR_FIX16K_80000_REG */ 0, 0,
491 /* 0x130 MTRR_FIX16K_A0000_REG */ 0, 0,
492 /* 0x138 MTRR_FIX4K_C0000_REG */ 0, 0,
493 /* 0x140 MTRR_FIX4K_C8000_REG */ 0, 0,
494 /* 0x148 MTRR_FIX4K_D0000_REG */ 0, 0,
495 /* 0x150 MTRR_FIX4K_D8000_REG */ 0, 0,
496 /* 0x158 MTRR_FIX4K_E0000_REG */ 0, 0,
497 /* 0x160 MTRR_FIX4K_E8000_REG */ 0, 0,
498 /* 0x168 MTRR_FIX4K_F0000_REG */ 0, 0,
499 /* 0x170 MTRR_FIX4K_F8000_REG */ 0, 0,
500 /* 0x178 Reserved */ 0, 0,
501 /* 0x180 MTRR_PHYSBASE0_REG */ 0, 0,
502 /* 0x188 MTRR_PHYSMASK0_REG */ 0, 0,
503 /* 0x190 MTRR_PHYSBASE1_REG */ 0, 0,
504 /* 0x198 MTRR_PHYSMASK1_REG */ 0, 0,
505 /* 0x1a0 MTRR_PHYSBASE2_REG */ 0, 0,
506 /* 0x1a8 MTRR_PHYSMASK2_REG */ 0, 0,
507 /* 0x1b0 MTRR_PHYSBASE3_REG */ 0, 0,
508 /* 0x1b8 MTRR_PHYSMASK3_REG */ 0, 0,
509 /* 0x1c0 MTRR_PHYSBASE4_REG */ 0, 0,
510 /* 0x1c8 MTRR_PHYSMASK4_REG */ 0, 0,
511 /* 0x1d0 MTRR_PHYSBASE5_REG */ 0, 0,
512 /* 0x1d8 MTRR_PHYSMASK5_REG */ 0, 0,
513 /* 0x1e0 MTRR_PHYSBASE6_REG */ 0, 0,
514 /* 0x1e8 MTRR_PHYSMASK6_REG */ 0, 0,
515 /* 0x1f0 MTRR_PHYSBASE7_REG */ 0, 0,
516 /* 0x1f8 MTRR_PHYSMASK7_REG */ 0, 0,
517 /* 0x200 MTRR_PHYSBASE8_REG */ 0, 0,
518 /* 0x208 MTRR_PHYSMASK8_REG */ 0, 0,
519 /* 0x210 MTRR_PHYSBASE9_REG */ 0, 0,
520 /* 0x218 MTRR_PHYSMASK9_REG */ 0, 0,
521};
522AssertCompile(sizeof(g_au32Rw1cMasks0) == DMAR_MMIO_GROUP_0_SIZE);
523
524/**
525 * Read-write masks for DMAR registers (group 1).
526 */
527static uint32_t const g_au32RwMasks1[] =
528{
529 /* Offset Register Low High */
530 /* 0xe00 VCCAP_REG */ DMAR_LO_U32(VTD_VCCAP_REG_RW_MASK), DMAR_HI_U32(VTD_VCCAP_REG_RW_MASK),
531 /* 0xe08 VCMD_EO_REG */ DMAR_LO_U32(VTD_VCMD_EO_REG_RW_MASK), DMAR_HI_U32(VTD_VCMD_EO_REG_RW_MASK),
532 /* 0xe10 VCMD_REG */ 0, 0, /* RO: VCS not supported. */
533 /* 0xe18 VCMDRSVD_REG */ 0, 0,
534 /* 0xe20 VCRSP_REG */ 0, 0, /* RO: VCS not supported. */
535 /* 0xe28 VCRSPRSVD_REG */ 0, 0,
536 /* 0xe30 Reserved */ 0, 0,
537 /* 0xe38 Reserved */ 0, 0,
538 /* 0xe40 Reserved */ 0, 0,
539 /* 0xe48 Reserved */ 0, 0,
540 /* 0xe50 IVA_REG */ DMAR_LO_U32(VTD_IVA_REG_RW_MASK), DMAR_HI_U32(VTD_IVA_REG_RW_MASK),
541 /* 0xe58 IOTLB_REG */ DMAR_LO_U32(VTD_IOTLB_REG_RW_MASK), DMAR_HI_U32(VTD_IOTLB_REG_RW_MASK),
542 /* 0xe60 Reserved */ 0, 0,
543 /* 0xe68 Reserved */ 0, 0,
544 /* 0xe70 FRCD_REG_LO */ DMAR_LO_U32(VTD_FRCD_REG_LO_RW_MASK), DMAR_HI_U32(VTD_FRCD_REG_LO_RW_MASK),
545 /* 0xe78 FRCD_REG_HI */ DMAR_LO_U32(VTD_FRCD_REG_HI_RW_MASK), DMAR_HI_U32(VTD_FRCD_REG_HI_RW_MASK),
546};
547AssertCompile(sizeof(g_au32RwMasks1) == DMAR_MMIO_GROUP_1_SIZE);
548AssertCompile((DMAR_MMIO_OFF_FRCD_LO_REG - DMAR_MMIO_GROUP_1_OFF_FIRST) + DMAR_FRCD_REG_COUNT * 2 * sizeof(uint64_t) );
549
550/**
551 * Read-only Status, Write-1-to-clear masks for DMAR registers (group 1).
552 */
553static uint32_t const g_au32Rw1cMasks1[] =
554{
555 /* Offset Register Low High */
556 /* 0xe00 VCCAP_REG */ 0, 0,
557 /* 0xe08 VCMD_EO_REG */ 0, 0,
558 /* 0xe10 VCMD_REG */ 0, 0,
559 /* 0xe18 VCMDRSVD_REG */ 0, 0,
560 /* 0xe20 VCRSP_REG */ 0, 0,
561 /* 0xe28 VCRSPRSVD_REG */ 0, 0,
562 /* 0xe30 Reserved */ 0, 0,
563 /* 0xe38 Reserved */ 0, 0,
564 /* 0xe40 Reserved */ 0, 0,
565 /* 0xe48 Reserved */ 0, 0,
566 /* 0xe50 IVA_REG */ 0, 0,
567 /* 0xe58 IOTLB_REG */ 0, 0,
568 /* 0xe60 Reserved */ 0, 0,
569 /* 0xe68 Reserved */ 0, 0,
570 /* 0xe70 FRCD_REG_LO */ DMAR_LO_U32(VTD_FRCD_REG_LO_RW1C_MASK), DMAR_HI_U32(VTD_FRCD_REG_LO_RW1C_MASK),
571 /* 0xe78 FRCD_REG_HI */ DMAR_LO_U32(VTD_FRCD_REG_HI_RW1C_MASK), DMAR_HI_U32(VTD_FRCD_REG_HI_RW1C_MASK),
572};
573AssertCompile(sizeof(g_au32Rw1cMasks1) == DMAR_MMIO_GROUP_1_SIZE);
574
575/** Array of RW masks for each register group. */
576static uint8_t const *g_apbRwMasks[] = { (uint8_t *)&g_au32RwMasks0[0], (uint8_t *)&g_au32RwMasks1[0] };
577
578/** Array of RW1C masks for each register group. */
579static uint8_t const *g_apbRw1cMasks[] = { (uint8_t *)&g_au32Rw1cMasks0[0], (uint8_t *)&g_au32Rw1cMasks1[0] };
580
581/* Masks arrays must be identical in size (even bounds checking code assumes this). */
582AssertCompile(sizeof(g_apbRw1cMasks) == sizeof(g_apbRwMasks));
583
584
585#ifndef VBOX_DEVICE_STRUCT_TESTCASE
586/** @todo Add IOMMU struct size/alignment verification, see
587 * Devices/testcase/Makefile.kmk and
588 * Devices/testcase/tstDeviceStructSize[RC].cpp */
589
590/**
591 * Returns the number of supported adjusted guest-address width (SAGAW) in bits
592 * given a CAP_REG.SAGAW value.
593 *
594 * @returns Number of SAGAW bits.
595 * @param uSagaw The CAP_REG.SAGAW value.
596 */
597static uint8_t vtdCapRegGetSagawBits(uint8_t uSagaw)
598{
599 if (RT_LIKELY(uSagaw > 0 && uSagaw < 4))
600 return 30 + (uSagaw * 9);
601 return 0;
602}
603
604
605/**
606 * Returns the supported adjusted guest-address width (SAGAW) given the maximum
607 * guest address width (MGAW).
608 *
609 * @returns The CAP_REG.SAGAW value.
610 * @param uMgaw The CAP_REG.MGAW value.
611 */
612static uint8_t vtdCapRegGetSagaw(uint8_t uMgaw)
613{
614 switch (uMgaw + 1)
615 {
616 case 39: return 1;
617 case 48: return 2;
618 case 57: return 3;
619 }
620 return 0;
621}
622
623
624/**
625 * Returns table translation mode's descriptive name.
626 *
627 * @returns The descriptive name.
628 * @param uTtm The RTADDR_REG.TTM value.
629 */
630static const char* vtdRtaddrRegGetTtmDesc(uint8_t uTtm)
631{
632 Assert(!(uTtm & 3));
633 static const char* s_apszTtmNames[] =
634 {
635 "Legacy Mode",
636 "Scalable Mode",
637 "Reserved",
638 "Abort-DMA Mode"
639 };
640 return s_apszTtmNames[uTtm & (RT_ELEMENTS(s_apszTtmNames) - 1)];
641}
642
643
644/**
645 * Gets the index of the group the register belongs to given its MMIO offset.
646 *
647 * @returns The group index.
648 * @param offReg The MMIO offset of the register.
649 * @param cbReg The size of the access being made (for bounds checking on
650 * debug builds).
651 */
652DECLINLINE(uint8_t) dmarRegGetGroupIndex(uint16_t offReg, uint8_t cbReg)
653{
654 uint16_t const offLast = offReg + cbReg - 1;
655 AssertCompile(DMAR_MMIO_GROUP_0_OFF_FIRST == 0);
656 AssertMsg(DMAR_IS_MMIO_OFF_VALID(offLast), ("off=%#x cb=%u\n", offReg, cbReg));
657 return !(offLast < DMAR_MMIO_GROUP_0_OFF_END);
658}
659
660
661/**
662 * Gets the group the register belongs to given its MMIO offset.
663 *
664 * @returns Pointer to the first element of the register group.
665 * @param pThis The shared DMAR device state.
666 * @param offReg The MMIO offset of the register.
667 * @param cbReg The size of the access being made (for bounds checking on
668 * debug builds).
669 * @param pIdxGroup Where to store the index of the register group the register
670 * belongs to.
671 */
672DECLINLINE(uint8_t *) dmarRegGetGroup(PDMAR pThis, uint16_t offReg, uint8_t cbReg, uint8_t *pIdxGroup)
673{
674 *pIdxGroup = dmarRegGetGroupIndex(offReg, cbReg);
675 uint8_t *apbRegs[] = { &pThis->abRegs0[0], &pThis->abRegs1[0] };
676 return apbRegs[*pIdxGroup];
677}
678
679
680/**
681 * Const/read-only version of dmarRegGetGroup.
682 *
683 * @copydoc dmarRegGetGroup
684 */
685DECLINLINE(uint8_t const*) dmarRegGetGroupRo(PCDMAR pThis, uint16_t offReg, uint8_t cbReg, uint8_t *pIdxGroup)
686{
687 *pIdxGroup = dmarRegGetGroupIndex(offReg, cbReg);
688 uint8_t const *apbRegs[] = { &pThis->abRegs0[0], &pThis->abRegs1[0] };
689 return apbRegs[*pIdxGroup];
690}
691
692
693/**
694 * Writes a 32-bit register with the exactly the supplied value.
695 *
696 * @param pThis The shared DMAR device state.
697 * @param offReg The MMIO offset of the register.
698 * @param uReg The 32-bit value to write.
699 */
700static void dmarRegWriteRaw32(PDMAR pThis, uint16_t offReg, uint32_t uReg)
701{
702 uint8_t idxGroup;
703 uint8_t *pabRegs = dmarRegGetGroup(pThis, offReg, sizeof(uint32_t), &idxGroup);
704 NOREF(idxGroup);
705 *(uint32_t *)(pabRegs + offReg) = uReg;
706}
707
708
709/**
710 * Writes a 64-bit register with the exactly the supplied value.
711 *
712 * @param pThis The shared DMAR device state.
713 * @param offReg The MMIO offset of the register.
714 * @param uReg The 64-bit value to write.
715 */
716static void dmarRegWriteRaw64(PDMAR pThis, uint16_t offReg, uint64_t uReg)
717{
718 uint8_t idxGroup;
719 uint8_t *pabRegs = dmarRegGetGroup(pThis, offReg, sizeof(uint64_t), &idxGroup);
720 NOREF(idxGroup);
721 *(uint64_t *)(pabRegs + offReg) = uReg;
722}
723
724
725/**
726 * Reads a 32-bit register with exactly the value it contains.
727 *
728 * @returns The raw register value.
729 * @param pThis The shared DMAR device state.
730 * @param offReg The MMIO offset of the register.
731 */
732static uint32_t dmarRegReadRaw32(PCDMAR pThis, uint16_t offReg)
733{
734 uint8_t idxGroup;
735 uint8_t const *pabRegs = dmarRegGetGroupRo(pThis, offReg, sizeof(uint32_t), &idxGroup);
736 NOREF(idxGroup);
737 return *(uint32_t *)(pabRegs + offReg);
738}
739
740
741/**
742 * Reads a 64-bit register with exactly the value it contains.
743 *
744 * @returns The raw register value.
745 * @param pThis The shared DMAR device state.
746 * @param offReg The MMIO offset of the register.
747 */
748static uint64_t dmarRegReadRaw64(PCDMAR pThis, uint16_t offReg)
749{
750 uint8_t idxGroup;
751 uint8_t const *pabRegs = dmarRegGetGroupRo(pThis, offReg, sizeof(uint64_t), &idxGroup);
752 NOREF(idxGroup);
753 return *(uint64_t *)(pabRegs + offReg);
754}
755
756
757/**
758 * Reads a 32-bit register with exactly the value it contains along with their
759 * corresponding masks
760 *
761 * @param pThis The shared DMAR device state.
762 * @param offReg The MMIO offset of the register.
763 * @param puReg Where to store the raw 32-bit register value.
764 * @param pfRwMask Where to store the RW mask corresponding to this register.
765 * @param pfRw1cMask Where to store the RW1C mask corresponding to this register.
766 */
767static void dmarRegReadRaw32Ex(PCDMAR pThis, uint16_t offReg, uint32_t *puReg, uint32_t *pfRwMask, uint32_t *pfRw1cMask)
768{
769 uint8_t idxGroup;
770 uint8_t const *pabRegs = dmarRegGetGroupRo(pThis, offReg, sizeof(uint32_t), &idxGroup);
771 Assert(idxGroup < RT_ELEMENTS(g_apbRwMasks));
772 uint8_t const *pabRwMasks = g_apbRwMasks[idxGroup];
773 uint8_t const *pabRw1cMasks = g_apbRw1cMasks[idxGroup];
774 *puReg = *(uint32_t *)(pabRegs + offReg);
775 *pfRwMask = *(uint32_t *)(pabRwMasks + offReg);
776 *pfRw1cMask = *(uint32_t *)(pabRw1cMasks + offReg);
777}
778
779
780/**
781 * Reads a 64-bit register with exactly the value it contains along with their
782 * corresponding masks.
783 *
784 * @param pThis The shared DMAR device state.
785 * @param offReg The MMIO offset of the register.
786 * @param puReg Where to store the raw 64-bit register value.
787 * @param pfRwMask Where to store the RW mask corresponding to this register.
788 * @param pfRw1cMask Where to store the RW1C mask corresponding to this register.
789 */
790static void dmarRegReadRaw64Ex(PCDMAR pThis, uint16_t offReg, uint64_t *puReg, uint64_t *pfRwMask, uint64_t *pfRw1cMask)
791{
792 uint8_t idxGroup;
793 uint8_t const *pabRegs = dmarRegGetGroupRo(pThis, offReg, sizeof(uint64_t), &idxGroup);
794 Assert(idxGroup < RT_ELEMENTS(g_apbRwMasks));
795 uint8_t const *pabRwMasks = g_apbRwMasks[idxGroup];
796 uint8_t const *pabRw1cMasks = g_apbRw1cMasks[idxGroup];
797 *puReg = *(uint64_t *)(pabRegs + offReg);
798 *pfRwMask = *(uint64_t *)(pabRwMasks + offReg);
799 *pfRw1cMask = *(uint64_t *)(pabRw1cMasks + offReg);
800}
801
802
803/**
804 * Writes a 32-bit register as it would be when written by software.
805 * This will preserve read-only bits, mask off reserved bits and clear RW1C bits.
806 *
807 * @returns The value that's actually written to the register.
808 * @param pThis The shared DMAR device state.
809 * @param offReg The MMIO offset of the register.
810 * @param uReg The 32-bit value to write.
811 */
812static uint32_t dmarRegWrite32(PDMAR pThis, uint16_t offReg, uint32_t uReg)
813{
814 /* Read current value from the 32-bit register. */
815 uint32_t uCurReg;
816 uint32_t fRwMask;
817 uint32_t fRw1cMask;
818 dmarRegReadRaw32Ex(pThis, offReg, &uCurReg, &fRwMask, &fRw1cMask);
819
820 uint32_t const fRoBits = uCurReg & ~fRwMask; /* Preserve current read-only and reserved bits. */
821 uint32_t const fRwBits = uReg & fRwMask; /* Merge newly written read/write bits. */
822 uint32_t const fRw1cBits = uReg & fRw1cMask; /* Clear 1s written to RW1C bits. */
823 uint32_t const uNewReg = (fRoBits | fRwBits) & ~fRw1cBits;
824
825 /* Write new value to the 32-bit register. */
826 dmarRegWriteRaw32(pThis, offReg, uNewReg);
827 return uNewReg;
828}
829
830
831/**
832 * Writes a 64-bit register as it would be when written by software.
833 * This will preserve read-only bits, mask off reserved bits and clear RW1C bits.
834 *
835 * @returns The value that's actually written to the register.
836 * @param pThis The shared DMAR device state.
837 * @param offReg The MMIO offset of the register.
838 * @param uReg The 64-bit value to write.
839 */
840static uint64_t dmarRegWrite64(PDMAR pThis, uint16_t offReg, uint64_t uReg)
841{
842 /* Read current value from the 64-bit register. */
843 uint64_t uCurReg;
844 uint64_t fRwMask;
845 uint64_t fRw1cMask;
846 dmarRegReadRaw64Ex(pThis, offReg, &uCurReg, &fRwMask, &fRw1cMask);
847
848 uint64_t const fRoBits = uCurReg & ~fRwMask; /* Preserve current read-only and reserved bits. */
849 uint64_t const fRwBits = uReg & fRwMask; /* Merge newly written read/write bits. */
850 uint64_t const fRw1cBits = uReg & fRw1cMask; /* Clear 1s written to RW1C bits. */
851 uint64_t const uNewReg = (fRoBits | fRwBits) & ~fRw1cBits;
852
853 /* Write new value to the 64-bit register. */
854 dmarRegWriteRaw64(pThis, offReg, uNewReg);
855 return uNewReg;
856}
857
858
859/**
860 * Reads a 32-bit register as it would be when read by software.
861 *
862 * @returns The register value.
863 * @param pThis The shared DMAR device state.
864 * @param offReg The MMIO offset of the register.
865 */
866static uint32_t dmarRegRead32(PCDMAR pThis, uint16_t offReg)
867{
868 return dmarRegReadRaw32(pThis, offReg);
869}
870
871
872/**
873 * Reads a 64-bit register as it would be when read by software.
874 *
875 * @returns The register value.
876 * @param pThis The shared DMAR device state.
877 * @param offReg The MMIO offset of the register.
878 */
879static uint64_t dmarRegRead64(PCDMAR pThis, uint16_t offReg)
880{
881 return dmarRegReadRaw64(pThis, offReg);
882}
883
884
885/**
886 * Modifies a 32-bit register.
887 *
888 * @param pThis The shared DMAR device state.
889 * @param offReg The MMIO offset of the register.
890 * @param fAndMask The AND mask (applied first).
891 * @param fOrMask The OR mask.
892 * @remarks This does NOT apply RO or RW1C masks while modifying the
893 * register.
894 */
895static void dmarRegChangeRaw32(PDMAR pThis, uint16_t offReg, uint32_t fAndMask, uint32_t fOrMask)
896{
897 uint32_t uReg = dmarRegReadRaw32(pThis, offReg);
898 uReg = (uReg & fAndMask) | fOrMask;
899 dmarRegWriteRaw32(pThis, offReg, uReg);
900}
901
902
903/**
904 * Modifies a 64-bit register.
905 *
906 * @param pThis The shared DMAR device state.
907 * @param offReg The MMIO offset of the register.
908 * @param fAndMask The AND mask (applied first).
909 * @param fOrMask The OR mask.
910 * @remarks This does NOT apply RO or RW1C masks while modifying the
911 * register.
912 */
913static void dmarRegChangeRaw64(PDMAR pThis, uint16_t offReg, uint64_t fAndMask, uint64_t fOrMask)
914{
915 uint64_t uReg = dmarRegReadRaw64(pThis, offReg);
916 uReg = (uReg & fAndMask) | fOrMask;
917 dmarRegWriteRaw64(pThis, offReg, uReg);
918}
919
920
921/**
922 * Checks if the invalidation-queue is empty.
923 *
924 * Extended version which optionally returns the current queue head and tail
925 * offsets.
926 *
927 * @returns @c true if empty, @c false otherwise.
928 * @param pThis The shared DMAR device state.
929 * @param poffQh Where to store the queue head offset. Optional, can be NULL.
930 * @param poffQt Where to store the queue tail offset. Optional, can be NULL.
931 */
932static bool dmarInvQueueIsEmptyEx(PCDMAR pThis, uint32_t *poffQh, uint32_t *poffQt)
933{
934 /* Read only the low-32 bits of the queue head and queue tail as high bits are all RsvdZ.*/
935 uint32_t const uIqtReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_IQT_REG);
936 uint32_t const uIqhReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_IQH_REG);
937
938 /* Don't bother masking QT, QH since other bits are RsvdZ. */
939 Assert(!(uIqtReg & ~VTD_BF_IQT_REG_QT_MASK));
940 Assert(!(uIqhReg & ~VTD_BF_IQH_REG_QH_MASK));
941 if (poffQh)
942 *poffQh = uIqhReg;
943 if (poffQt)
944 *poffQt = uIqtReg;
945 return uIqtReg == uIqhReg;
946}
947
948
949/**
950 * Checks if the invalidation-queue is empty.
951 *
952 * @returns @c true if empty, @c false otherwise.
953 * @param pThis The shared DMAR device state.
954 */
955static bool dmarInvQueueIsEmpty(PCDMAR pThis)
956{
957 return dmarInvQueueIsEmptyEx(pThis, NULL /* poffQh */, NULL /* poffQt */);
958}
959
960
961/**
962 * Checks if the invalidation-queue is capable of processing requests.
963 *
964 * @returns @c true if the invalidation-queue can process requests, @c false
965 * otherwise.
966 * @param pThis The shared DMAR device state.
967 */
968static bool dmarInvQueueCanProcessRequests(PCDMAR pThis)
969{
970 /* Check if queued-invalidation is enabled. */
971 uint32_t const uGstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_GSTS_REG);
972 if (uGstsReg & VTD_BF_GSTS_REG_QIES_MASK)
973 {
974 /* Check if there are no invalidation-queue or timeout errors. */
975 uint32_t const uFstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FSTS_REG);
976 if (!(uFstsReg & (VTD_BF_FSTS_REG_IQE_MASK | VTD_BF_FSTS_REG_ITE_MASK)))
977 return true;
978 }
979 return false;
980}
981
982
983/**
984 * Wakes up the invalidation-queue thread if there are requests to be processed.
985 *
986 * @param pDevIns The IOMMU device instance.
987 */
988static void dmarInvQueueThreadWakeUpIfNeeded(PPDMDEVINS pDevIns)
989{
990 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
991 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
992 Log4Func(("\n"));
993
994 DMAR_ASSERT_LOCK_IS_OWNER(pDevIns, pThisCC);
995
996 if ( dmarInvQueueCanProcessRequests(pThis)
997 && !dmarInvQueueIsEmpty(pThis)
998 && !ASMAtomicXchgBool(&pThis->fInvQueueThreadSignaled, true))
999 {
1000 Log4Func(("Signaling the invalidation-queue thread\n"));
1001 PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hEvtInvQueue);
1002 }
1003}
1004
1005
1006/**
1007 * Raises an interrupt in response to a fault event.
1008 *
1009 * @param pDevIns The IOMMU device instance.
1010 */
1011static void dmarFaultEventRaiseInterrupt(PPDMDEVINS pDevIns)
1012{
1013 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1014#ifdef RT_STRICT
1015 {
1016 uint32_t const uFstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FSTS_REG);
1017 uint32_t const fFaultMask = VTD_BF_FSTS_REG_PPF_MASK | VTD_BF_FSTS_REG_PFO_MASK
1018 /* | VTD_BF_FSTS_REG_APF_MASK | VTD_BF_FSTS_REG_AFO_MASK */ /* AFL not supported */
1019 /* | VTD_BF_FSTS_REG_ICE_MASK | VTD_BF_FSTS_REG_ITE_MASK */ /* Device-TLBs not supported */
1020 | VTD_BF_FSTS_REG_IQE_MASK;
1021 Assert(uFstsReg & fFaultMask);
1022 }
1023#endif
1024
1025 uint32_t uFectlReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FECTL_REG);
1026 if (!(uFectlReg & VTD_BF_FECTL_REG_IM_MASK))
1027 {
1028 /* Software has unmasked the interrupt, raise it. */
1029 MSIMSG Msi;
1030 Msi.Addr.au32[0] = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FEADDR_REG);
1031 Msi.Addr.au32[1] = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FEUADDR_REG);
1032 Msi.Data.u32 = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FEDATA_REG);
1033
1034 /** @todo Assert Msi.Addr is in the MSR_IA32_APICBASE_ADDR range and ensure on
1035 * FEADD_REG write it can't be anything else? */
1036 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
1037 pThisCC->CTX_SUFF(pIommuHlp)->pfnSendMsi(pDevIns, &Msi, 0 /* uTagSrc */);
1038
1039 /* Clear interrupt pending bit. */
1040 uFectlReg &= ~VTD_BF_FECTL_REG_IP_MASK;
1041 dmarRegWriteRaw32(pThis, VTD_MMIO_OFF_FECTL_REG, uFectlReg);
1042 }
1043 else
1044 {
1045 /* Interrupt is masked, set the interrupt pending bit. */
1046 uFectlReg |= VTD_BF_FECTL_REG_IP_MASK;
1047 dmarRegWriteRaw32(pThis, VTD_MMIO_OFF_FECTL_REG, uFectlReg);
1048 }
1049}
1050
1051
1052#ifdef IN_RING3
1053/**
1054 * Raises an interrupt in response to an invalidation (complete) event.
1055 *
1056 * @param pDevIns The IOMMU device instance.
1057 */
1058static void dmarR3InvEventRaiseInterrupt(PPDMDEVINS pDevIns)
1059{
1060 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1061 uint32_t const uIcsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_ICS_REG);
1062 if (uIcsReg & VTD_BF_ICS_REG_IWC_MASK)
1063 return;
1064
1065 uint32_t uIectlReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_IECTL_REG);
1066 if (!(uIectlReg & VTD_BF_IECTL_REG_IM_MASK))
1067 {
1068 /* Software has unmasked the interrupt, raise it. */
1069 MSIMSG Msi;
1070 Msi.Addr.au32[0] = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_IEADDR_REG);
1071 Msi.Addr.au32[1] = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_IEUADDR_REG);
1072 Msi.Data.u32 = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_IEDATA_REG);
1073
1074 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
1075 pThisCC->CTX_SUFF(pIommuHlp)->pfnSendMsi(pDevIns, &Msi, 0 /* uTagSrc */);
1076
1077 /* Clear interrupt pending bit. */
1078 uIectlReg &= ~VTD_BF_IECTL_REG_IP_MASK;
1079 dmarRegWriteRaw32(pThis, VTD_MMIO_OFF_IECTL_REG, uIectlReg);
1080 }
1081 else
1082 {
1083 /* Interrupt is masked, set the interrupt pending bit. */
1084 uIectlReg |= VTD_BF_IECTL_REG_IP_MASK;
1085 dmarRegWriteRaw32(pThis, VTD_MMIO_OFF_IECTL_REG, uIectlReg);
1086 }
1087}
1088#endif /* IN_RING3 */
1089
1090
1091#if 0
1092/**
1093 * Checks if a primary fault can be recorded.
1094 *
1095 * @returns @c true if the fault can be recorded, @c false otherwise.
1096 * @param pDevIns The IOMMU device instance.
1097 * @param pThis The shared DMAR device state.
1098 */
1099static bool dmarPrimaryFaultCanRecord(PPDMDEVINS pDevIns, PDMAR pThis)
1100{
1101 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
1102 DMAR_ASSERT_LOCK_IS_OWNER(pDevIns, pThisCC);
1103
1104 uint32_t uFstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FSTS_REG);
1105 if (uFstsReg & VTD_BF_FSTS_REG_PFO_MASK)
1106 return false;
1107
1108 /*
1109 * If we add more FRCD registers, we'll have to loop through them here.
1110 * Since we support only one FRCD_REG, we don't support "compression of multiple faults",
1111 * nor do we need to increment FRI.
1112 *
1113 * See Intel VT-d spec. 7.2.1 "Primary Fault Logging".
1114 */
1115 AssertCompile(DMAR_FRCD_REG_COUNT == 1);
1116 uint64_t const uFrcdRegHi = dmarRegReadRaw64(pThis, DMAR_MMIO_OFF_FRCD_HI_REG);
1117 if (uFrcdRegHi & VTD_BF_1_FRCD_REG_F_MASK)
1118 {
1119 uFstsReg |= VTD_BF_FSTS_REG_PFO_MASK;
1120 dmarRegWrite32(pThis, VTD_MMIO_OFF_FSTS_REG, uFstsReg);
1121 return false;
1122 }
1123
1124 uFstsReg |= VTD_BF_FSTS_REG_PPF_MASK;
1125 dmarRegWrite32(pThis, VTD_MMIO_OFF_FSTS_REG, uFstsReg);
1126 return true;
1127}
1128#endif
1129
1130
1131/**
1132 * Records an IQE fault.
1133 *
1134 * @param pDevIns The IOMMU device instance.
1135 * @param enmIqei The IQE information.
1136 * @param enmDiag The diagnostic reason.
1137 */
1138static void dmarIqeFaultRecord(PPDMDEVINS pDevIns, DMARDIAG enmDiag, VTD_IQEI_T enmIqei)
1139{
1140 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1141 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
1142 DMAR_ASSERT_LOCK_IS_OWNER(pDevIns, pThisCC);
1143
1144 /* Update the diagnostic reason. */
1145 pThis->enmDiag = enmDiag;
1146
1147 /* Set the error bit. */
1148 uint32_t const fIqe = RT_BF_MAKE(VTD_BF_FSTS_REG_IQE, 1);
1149 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_FSTS_REG, UINT32_MAX, fIqe);
1150
1151 /* Set the error information. */
1152 uint64_t const fIqei = RT_BF_MAKE(VTD_BF_IQERCD_REG_IQEI, enmIqei);
1153 dmarRegChangeRaw64(pThis, VTD_MMIO_OFF_IQERCD_REG, UINT64_MAX, fIqei);
1154
1155 dmarFaultEventRaiseInterrupt(pDevIns);
1156}
1157
1158
1159/**
1160 * Handles writes to GCMD_REG.
1161 *
1162 * @returns Strict VBox status code.
1163 * @param pDevIns The IOMMU device instance.
1164 * @param uGcmdReg The value written to GCMD_REG.
1165 */
1166static VBOXSTRICTRC dmarGcmdRegWrite(PPDMDEVINS pDevIns, uint32_t uGcmdReg)
1167{
1168 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1169 uint32_t const uGstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_GSTS_REG);
1170 uint32_t const fChanged = uGstsReg ^ uGcmdReg;
1171 uint64_t const fExtCapReg = pThis->fExtCapReg;
1172
1173 /*
1174 * Queued-invalidation.
1175 */
1176 if ( (fExtCapReg & VTD_BF_ECAP_REG_QI_MASK)
1177 && (fChanged & VTD_BF_GCMD_REG_QIE_MASK))
1178 {
1179 if (uGcmdReg & VTD_BF_GCMD_REG_QIE_MASK)
1180 {
1181 /* Enable the invalidation-queue. */
1182 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_GSTS_REG, UINT32_MAX /* fAndMask */, VTD_BF_GSTS_REG_QIES_MASK /* fOrMask */);
1183 dmarInvQueueThreadWakeUpIfNeeded(pDevIns);
1184 }
1185 else
1186 {
1187 /* Disable the invalidation-queue. */
1188 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_GSTS_REG, ~VTD_BF_GSTS_REG_QIES_MASK /* fAndMask */, 0 /* fOrMask */);
1189 dmarRegWriteRaw32(pThis, VTD_MMIO_OFF_IQH_REG, 0);
1190 }
1191 }
1192
1193 /*
1194 * Set Interrupt Remapping Table Pointer (SIRTP).
1195 */
1196 if ( (fExtCapReg & VTD_BF_ECAP_REG_IR_MASK)
1197 && (uGcmdReg & VTD_BF_GCMD_REG_SIRTP_MASK))
1198 {
1199 /** @todo Perform global invalidation of all interrupt-entry cache when ESIRTPS is
1200 * supported. */
1201 pThis->uIrtaReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_IRTA_REG);
1202 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_GSTS_REG, UINT32_MAX /* fAndMask */, VTD_BF_GSTS_REG_IRTPS_MASK /* fOrMask */);
1203 }
1204
1205 /*
1206 * Set Root Table Pointer (SRTP).
1207 */
1208 if (uGcmdReg & VTD_BF_GCMD_REG_SRTP_MASK)
1209 {
1210 /** @todo Perform global invalidation of all remapping translation caches when
1211 * ESRTPS is supported. */
1212 pThis->uRtaddrReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_RTADDR_REG);
1213 }
1214
1215 /** @todo Rest of the bits. */
1216
1217 return VINF_SUCCESS;
1218}
1219
1220
1221/**
1222 * Handles writes to CCMD_REG.
1223 *
1224 * @returns Strict VBox status code.
1225 * @param pDevIns The IOMMU device instance.
1226 * @param offReg The MMIO register offset.
1227 * @param cbReg The size of the MMIO access (in bytes).
1228 * @param uCcmdReg The value written to CCMD_REG.
1229 */
1230static VBOXSTRICTRC dmarCcmdRegWrite(PPDMDEVINS pDevIns, uint16_t offReg, uint8_t cbReg, uint64_t uCcmdReg)
1231{
1232 /* At present, we only care about responding to high 32-bits writes, low 32-bits are data. */
1233 if (offReg + cbReg > VTD_MMIO_OFF_CCMD_REG + 4)
1234 {
1235 /* Check if we need to invalidate the context-context. */
1236 bool const fIcc = RT_BF_GET(uCcmdReg, VTD_BF_CCMD_REG_ICC);
1237 if (fIcc)
1238 {
1239 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1240 uint8_t const uMajorVersion = RT_BF_GET(pThis->uVerReg, VTD_BF_VER_REG_MAX);
1241 if (uMajorVersion < 6)
1242 {
1243 /* Register-based invalidation can only be used when queued-invalidations are not enabled. */
1244 uint32_t const uGstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_GSTS_REG);
1245 if (!(uGstsReg & VTD_BF_GSTS_REG_QIES_MASK))
1246 {
1247 /* Verify table translation mode is legacy. */
1248 uint8_t const fTtm = RT_BF_GET(pThis->uRtaddrReg, VTD_BF_RTADDR_REG_TTM);
1249 if (fTtm == VTD_TTM_LEGACY_MODE)
1250 {
1251 /** @todo Invalidate. */
1252 return VINF_SUCCESS;
1253 }
1254 pThis->enmDiag = kDmarDiag_CcmdReg_Ttm_Invalid;
1255 }
1256 else
1257 pThis->enmDiag = kDmarDiag_CcmdReg_Qi_Enabled;
1258 }
1259 else
1260 pThis->enmDiag = kDmarDiag_CcmdReg_NotSupported;
1261 dmarRegChangeRaw64(pThis, VTD_MMIO_OFF_GSTS_REG, ~VTD_BF_CCMD_REG_CAIG_MASK /* fAndMask */, 0 /* fOrMask */);
1262 }
1263 }
1264 return VINF_SUCCESS;
1265}
1266
1267
1268/**
1269 * Handles writes to IQT_REG.
1270 *
1271 * @returns Strict VBox status code.
1272 * @param pDevIns The IOMMU device instance.
1273 * @param offReg The MMIO register offset.
1274 * @param uIqtReg The value written to IQT_REG.
1275 */
1276static VBOXSTRICTRC dmarIqtRegWrite(PPDMDEVINS pDevIns, uint16_t offReg, uint64_t uIqtReg)
1277{
1278 /* We only care about the low 32-bits, high 32-bits are reserved. */
1279 Assert(offReg == VTD_MMIO_OFF_IQT_REG);
1280 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1281
1282 /* Paranoia. */
1283 Assert(!(uIqtReg & ~VTD_BF_IQT_REG_QT_MASK));
1284
1285 uint32_t const offQt = uIqtReg;
1286 uint64_t const uIqaReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_IQA_REG);
1287 uint8_t const fDw = RT_BF_GET(uIqaReg, VTD_BF_IQA_REG_DW);
1288
1289 /* If the descriptor width is 256-bits, the queue tail offset must be aligned accordingly. */
1290 if ( fDw != VTD_IQA_REG_DW_256_BIT
1291 || !(offQt & RT_BIT(4)))
1292 dmarInvQueueThreadWakeUpIfNeeded(pDevIns);
1293 else
1294 {
1295 /* Hardware treats bit 4 as RsvdZ in this situation, so clear it. */
1296 dmarRegChangeRaw32(pThis, offReg, ~RT_BIT(4) /* fAndMask*/ , 0 /* fOrMask */);
1297 dmarIqeFaultRecord(pDevIns, kDmarDiag_IqtReg_Qt_NotAligned, kIqei_QueueTailNotAligned);
1298 }
1299 return VINF_SUCCESS;
1300}
1301
1302
1303/**
1304 * Handles writes to IQA_REG.
1305 *
1306 * @returns Strict VBox status code.
1307 * @param pDevIns The IOMMU device instance.
1308 * @param offReg The MMIO register offset.
1309 * @param uIqaReg The value written to IQA_REG.
1310 */
1311static VBOXSTRICTRC dmarIqaRegWrite(PPDMDEVINS pDevIns, uint16_t offReg, uint64_t uIqaReg)
1312{
1313 /* At present, we only care about the low 32-bits, high 32-bits are data. */
1314 Assert(offReg == VTD_MMIO_OFF_IQA_REG);
1315
1316 /** @todo What happens if IQA_REG is written when dmarInvQueueCanProcessRequests
1317 * returns true? The Intel VT-d spec. doesn't state anywhere that it
1318 * cannot happen or that it's ignored when it does happen. */
1319
1320 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1321 uint8_t const fDw = RT_BF_GET(uIqaReg, VTD_BF_IQA_REG_DW);
1322 if (fDw == VTD_IQA_REG_DW_256_BIT)
1323 {
1324 bool const fSupports256BitDw = (pThis->fExtCapReg & (VTD_BF_ECAP_REG_SMTS_MASK | VTD_BF_ECAP_REG_ADMS_MASK));
1325 if (fSupports256BitDw)
1326 { /* likely */ }
1327 else
1328 dmarIqeFaultRecord(pDevIns, kDmarDiag_IqaReg_Dw_256_Invalid, kIqei_InvalidDescriptorWidth);
1329 }
1330 /* else: 128-bit descriptor width is validated lazily, see explanation in dmarR3InvQueueProcessRequests. */
1331
1332 return VINF_SUCCESS;
1333}
1334
1335
1336/**
1337 * Memory access bulk (one or more 4K pages) request from a device.
1338 *
1339 * @returns VBox status code.
1340 * @param pDevIns The IOMMU device instance.
1341 * @param idDevice The device ID (bus, device, function).
1342 * @param cIovas The number of addresses being accessed.
1343 * @param pauIovas The I/O virtual addresses for each page being accessed.
1344 * @param fFlags The access flags, see PDMIOMMU_MEM_F_XXX.
1345 * @param paGCPhysSpa Where to store the translated physical addresses.
1346 *
1347 * @thread Any.
1348 */
1349static DECLCALLBACK(int) iommuIntelMemBulkAccess(PPDMDEVINS pDevIns, uint16_t idDevice, size_t cIovas, uint64_t const *pauIovas,
1350 uint32_t fFlags, PRTGCPHYS paGCPhysSpa)
1351{
1352 RT_NOREF6(pDevIns, idDevice, cIovas, pauIovas, fFlags, paGCPhysSpa);
1353 return VERR_NOT_IMPLEMENTED;
1354}
1355
1356
1357/**
1358 * Memory access transaction from a device.
1359 *
1360 * @returns VBox status code.
1361 * @param pDevIns The IOMMU device instance.
1362 * @param idDevice The device ID (bus, device, function).
1363 * @param uIova The I/O virtual address being accessed.
1364 * @param cbIova The size of the access.
1365 * @param fFlags The access flags, see PDMIOMMU_MEM_F_XXX.
1366 * @param pGCPhysSpa Where to store the translated system physical address.
1367 * @param pcbContiguous Where to store the number of contiguous bytes translated
1368 * and permission-checked.
1369 *
1370 * @thread Any.
1371 */
1372static DECLCALLBACK(int) iommuIntelMemAccess(PPDMDEVINS pDevIns, uint16_t idDevice, uint64_t uIova, size_t cbIova,
1373 uint32_t fFlags, PRTGCPHYS pGCPhysSpa, size_t *pcbContiguous)
1374{
1375 RT_NOREF7(pDevIns, idDevice, uIova, cbIova, fFlags, pGCPhysSpa, pcbContiguous);
1376 return VERR_NOT_IMPLEMENTED;
1377}
1378
1379
1380/**
1381 * Interrupt remap request from a device.
1382 *
1383 * @returns VBox status code.
1384 * @param pDevIns The IOMMU device instance.
1385 * @param idDevice The device ID (bus, device, function).
1386 * @param pMsiIn The source MSI.
1387 * @param pMsiOut Where to store the remapped MSI.
1388 */
1389static DECLCALLBACK(int) iommuIntelMsiRemap(PPDMDEVINS pDevIns, uint16_t idDevice, PCMSIMSG pMsiIn, PMSIMSG pMsiOut)
1390{
1391 RT_NOREF3(idDevice, pMsiIn, pMsiOut);
1392 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1393 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMsiRemap)); NOREF(pThis);
1394
1395 return VERR_NOT_IMPLEMENTED;
1396}
1397
1398
1399/**
1400 * @callback_method_impl{FNIOMMMIONEWWRITE}
1401 */
1402static DECLCALLBACK(VBOXSTRICTRC) dmarMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
1403{
1404 RT_NOREF1(pvUser);
1405 DMAR_ASSERT_MMIO_ACCESS_RET(off, cb);
1406
1407 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1408 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMmioWrite));
1409
1410 uint16_t const offReg = off;
1411 uint16_t const offLast = offReg + cb - 1;
1412 if (DMAR_IS_MMIO_OFF_VALID(offLast))
1413 {
1414 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
1415 DMAR_LOCK_RET(pDevIns, pThisCC, VINF_IOM_R3_MMIO_WRITE);
1416
1417 uint64_t const uRegWritten = cb == 8 ? dmarRegWrite64(pThis, offReg, *(uint64_t *)pv)
1418 : dmarRegWrite32(pThis, offReg, *(uint32_t *)pv);
1419 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
1420 switch (off)
1421 {
1422 case VTD_MMIO_OFF_GCMD_REG: /* 32-bit */
1423 {
1424 rcStrict = dmarGcmdRegWrite(pDevIns, uRegWritten);
1425 break;
1426 }
1427
1428 case VTD_MMIO_OFF_CCMD_REG: /* 64-bit */
1429 case VTD_MMIO_OFF_CCMD_REG + 4:
1430 {
1431 rcStrict = dmarCcmdRegWrite(pDevIns, offReg, cb, uRegWritten);
1432 break;
1433 }
1434
1435 case VTD_MMIO_OFF_IQT_REG: /* 64-bit */
1436 /* VTD_MMIO_OFF_IQT_REG + 4: */ /* High 32-bits reserved. */
1437 {
1438 rcStrict = dmarIqtRegWrite(pDevIns, offReg, uRegWritten);
1439 break;
1440 }
1441
1442 case VTD_MMIO_OFF_IQA_REG: /* 64-bit */
1443 /* VTD_MMIO_OFF_IQA_REG + 4: */ /* High 32-bits data. */
1444 {
1445 rcStrict = dmarIqaRegWrite(pDevIns, offReg, uRegWritten);
1446 break;
1447 }
1448 }
1449
1450 DMAR_UNLOCK(pDevIns, pThisCC);
1451 LogFlowFunc(("offReg=%#x uRegWritten=%#RX64 rc=%Rrc\n", offReg, uRegWritten, VBOXSTRICTRC_VAL(rcStrict)));
1452 return rcStrict;
1453 }
1454
1455 return VINF_IOM_MMIO_UNUSED_FF;
1456}
1457
1458
1459/**
1460 * @callback_method_impl{FNIOMMMIONEWREAD}
1461 */
1462static DECLCALLBACK(VBOXSTRICTRC) dmarMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
1463{
1464 RT_NOREF1(pvUser);
1465 DMAR_ASSERT_MMIO_ACCESS_RET(off, cb);
1466
1467 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1468 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMmioRead));
1469
1470 uint16_t const offReg = off;
1471 uint16_t const offLast = offReg + cb - 1;
1472 if (DMAR_IS_MMIO_OFF_VALID(offLast))
1473 {
1474 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
1475 DMAR_LOCK_RET(pDevIns, pThisCC, VINF_IOM_R3_MMIO_READ);
1476
1477 if (cb == 8)
1478 {
1479 *(uint64_t *)pv = dmarRegRead64(pThis, offReg);
1480 LogFlowFunc(("offReg=%#x pv=%#RX64\n", offReg, *(uint64_t *)pv));
1481 }
1482 else
1483 {
1484 *(uint32_t *)pv = dmarRegRead32(pThis, offReg);
1485 LogFlowFunc(("offReg=%#x pv=%#RX32\n", offReg, *(uint32_t *)pv));
1486 }
1487
1488 DMAR_UNLOCK(pDevIns, pThisCC);
1489 return VINF_SUCCESS;
1490 }
1491
1492 return VINF_IOM_MMIO_UNUSED_FF;
1493}
1494
1495
1496#ifdef IN_RING3
1497/**
1498 * Process requests in the invalidation queue.
1499 *
1500 * @param pDevIns The IOMMU device instance.
1501 * @param pvRequests The requests to process.
1502 * @param cbRequests The size of all requests (in bytes).
1503 * @param fDw The descriptor width (VTD_IQA_REG_DW_128_BIT or
1504 * VTD_IQA_REG_DW_256_BIT).
1505 * @param fTtm The table translation mode. Must not be VTD_TTM_RSVD.
1506 */
1507static void dmarR3InvQueueProcessRequests(PPDMDEVINS pDevIns, void const *pvRequests, uint32_t cbRequests, uint8_t fDw,
1508 uint8_t fTtm)
1509{
1510#define DMAR_IQE_FAULT_RECORD_RET(a_enmDiag, a_enmIqei) \
1511 do \
1512 { \
1513 DMAR_LOCK(pDevIns, pThisR3); \
1514 dmarIqeFaultRecord(pDevIns, (a_enmDiag), (a_enmIqei)); \
1515 DMAR_UNLOCK(pDevIns, pThisR3); \
1516 return; \
1517 } while (0)
1518
1519 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1520 PCDMARR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARR3);
1521
1522 DMAR_ASSERT_LOCK_IS_NOT_OWNER(pDevIns, pThisR3);
1523 Assert(fTtm != VTD_TTM_RSVD); /* Should've beeen handled by caller. */
1524
1525 /*
1526 * The below check is redundant since we check both TTM and DW for each
1527 * descriptor type we process. However, the error reported by hardware
1528 * may differ hence this is kept commented out but not removed from the code
1529 * if we need to change this in the future.
1530 *
1531 * In our implementation, we would report the descriptor type as invalid,
1532 * while on real hardware it may report descriptor width as invalid.
1533 * The Intel VT-d spec. is not clear which error takes preceedence.
1534 */
1535#if 0
1536 /*
1537 * Verify that 128-bit descriptors are not used when operating in scalable mode.
1538 * We don't check this while software writes IQA_REG but defer it until now because
1539 * RTADDR_REG can be updated lazily (via GCMD_REG.SRTP). The 256-bit descriptor check
1540 * -IS- performed when software writes IQA_REG since it only requires checking against
1541 * immutable hardware features.
1542 */
1543 if ( fTtm != VTD_TTM_SCALABLE_MODE
1544 || fDw != VTD_IQA_REG_DW_128_BIT)
1545 { /* likely */ }
1546 else
1547 DMAR_IQE_FAULT_RECORD_RET(kDmarDiag_IqaReg_Dw_128_Invalid, kIqei_InvalidDescriptorWidth);
1548#endif
1549
1550 /*
1551 * Process requests in FIFO order.
1552 */
1553 uint8_t const cbDsc = fDw == VTD_IQA_REG_DW_256_BIT ? 32 : 16;
1554 for (uint32_t offDsc = 0; offDsc < cbRequests; offDsc += cbDsc)
1555 {
1556 uint64_t const *puDscQwords = (uint64_t const *)((uintptr_t)pvRequests + offDsc);
1557 uint64_t const uQword0 = puDscQwords[0];
1558 uint64_t const uQword1 = puDscQwords[1];
1559 uint8_t const fDscType = VTD_GENERIC_INV_DSC_GET_TYPE(uQword0);
1560 switch (fDscType)
1561 {
1562 case VTD_INV_WAIT_DSC_TYPE:
1563 {
1564 /* Validate descriptor type. */
1565 if ( fTtm == VTD_TTM_LEGACY_MODE
1566 || fDw == VTD_IQA_REG_DW_256_BIT)
1567 { /* likely */ }
1568 else
1569 DMAR_IQE_FAULT_RECORD_RET(kDmarDiag_Iqei_Inv_Wait_Dsc_Invalid, kIqei_InvalidDescriptorType);
1570
1571 /* Validate reserved bits. */
1572 uint64_t const fValidMask0 = !(pThis->fExtCapReg & VTD_BF_ECAP_REG_PDS_MASK)
1573 ? VTD_INV_WAIT_DSC_0_VALID_MASK & ~VTD_BF_0_INV_WAIT_DSC_PD_MASK
1574 : VTD_INV_WAIT_DSC_0_VALID_MASK;
1575 if ( !(uQword0 & ~fValidMask0)
1576 && !(uQword1 & ~VTD_INV_WAIT_DSC_1_VALID_MASK))
1577 { /* likely */ }
1578 else
1579 DMAR_IQE_FAULT_RECORD_RET(kDmarDiag_Iqei_Inv_Wait_Dsc_0_1_Rsvd, kIqei_RsvdFieldViolation);
1580
1581 if (fDw == VTD_IQA_REG_DW_256_BIT)
1582 {
1583 if ( !puDscQwords[2]
1584 && !puDscQwords[3])
1585 { /* likely */ }
1586 else
1587 DMAR_IQE_FAULT_RECORD_RET(kDmarDiag_Iqei_Inv_Wait_Dsc_2_3_Rsvd, kIqei_RsvdFieldViolation);
1588 }
1589
1590 /* Perform status write (this must be done prior to generating the completion interrupt). */
1591 bool const fSw = RT_BF_GET(uQword0, VTD_BF_0_INV_WAIT_DSC_SW);
1592 if (fSw)
1593 {
1594 uint32_t const uStatus = RT_BF_GET(uQword0, VTD_BF_0_INV_WAIT_DSC_STDATA);
1595 RTGCPHYS const GCPhysStatus = uQword1 & VTD_BF_1_INV_WAIT_DSC_STADDR_MASK;
1596 int const rc = PDMDevHlpPhysWrite(pDevIns, GCPhysStatus, (void const*)&uStatus, sizeof(uStatus));
1597 AssertRC(rc);
1598 }
1599
1600 /* Generate invalidation event interrupt. */
1601 bool const fIf = RT_BF_GET(uQword0, VTD_BF_0_INV_WAIT_DSC_IF);
1602 if (fIf)
1603 {
1604 DMAR_LOCK(pDevIns, pThisR3);
1605 dmarR3InvEventRaiseInterrupt(pDevIns);
1606 DMAR_UNLOCK(pDevIns, pThisR3);
1607 }
1608
1609 STAM_COUNTER_INC(&pThis->StatInvWaitDsc);
1610 break;
1611 }
1612
1613 case VTD_CC_INV_DSC_TYPE: STAM_COUNTER_INC(&pThis->StatCcInvDsc); break;
1614 case VTD_IOTLB_INV_DSC_TYPE: STAM_COUNTER_INC(&pThis->StatIotlbInvDsc); break;
1615 case VTD_DEV_TLB_INV_DSC_TYPE: STAM_COUNTER_INC(&pThis->StatDevtlbInvDsc); break;
1616 case VTD_IEC_INV_DSC_TYPE: STAM_COUNTER_INC(&pThis->StatIecInvDsc); break;
1617 case VTD_P_IOTLB_INV_DSC_TYPE: STAM_COUNTER_INC(&pThis->StatPasidIotlbInvDsc); break;
1618 case VTD_PC_INV_DSC_TYPE: STAM_COUNTER_INC(&pThis->StatPasidCacheInvDsc); break;
1619 case VTD_P_DEV_TLB_INV_DSC_TYPE: STAM_COUNTER_INC(&pThis->StatPasidDevtlbInvDsc); break;
1620 default:
1621 {
1622 /* Stop processing further requests. */
1623 LogFunc(("Invalid descriptor type: %#x\n", fDscType));
1624 DMAR_IQE_FAULT_RECORD_RET(kDmarDiag_Iqei_Dsc_Type_Invalid, kIqei_InvalidDescriptorType);
1625 }
1626 }
1627 }
1628#undef DMAR_IQE_FAULT_RECORD_RET
1629}
1630
1631
1632/**
1633 * The invalidation-queue thread.
1634 *
1635 * @returns VBox status code.
1636 * @param pDevIns The IOMMU device instance.
1637 * @param pThread The command thread.
1638 */
1639static DECLCALLBACK(int) dmarR3InvQueueThread(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
1640{
1641 NOREF(pThread);
1642 LogFlowFunc(("\n"));
1643
1644 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
1645 return VINF_SUCCESS;
1646
1647 /*
1648 * Pre-allocate the maximum size of the invalidation queue allowed by the spec.
1649 * This prevents trashing the heap as well as deal with out-of-memory situations
1650 * up-front while starting the VM. It also simplifies the code from having to
1651 * dynamically grow/shrink the allocation based on how software sizes the queue.
1652 * Guests normally don't alter the queue size all the time, but that's not an
1653 * assumption we can make.
1654 */
1655 uint8_t const cMaxPages = 1 << VTD_BF_IQA_REG_QS_MASK;
1656 size_t const cbMaxQs = cMaxPages << X86_PAGE_SHIFT;
1657 void *pvRequests = RTMemAllocZ(cbMaxQs);
1658 AssertPtrReturn(pvRequests, VERR_NO_MEMORY);
1659
1660 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1661 PCDMARR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARR3);
1662
1663 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
1664 {
1665 /*
1666 * Sleep until we are woken up.
1667 */
1668 bool const fSignaled = ASMAtomicXchgBool(&pThis->fInvQueueThreadSignaled, false);
1669 if (!fSignaled)
1670 {
1671 int const rc = PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pThis->hEvtInvQueue, RT_INDEFINITE_WAIT);
1672 AssertLogRelMsgReturn(RT_SUCCESS(rc) || rc == VERR_INTERRUPTED, ("%Rrc\n", rc), rc);
1673 if (RT_UNLIKELY(pThread->enmState != PDMTHREADSTATE_RUNNING))
1674 break;
1675 ASMAtomicWriteBool(&pThis->fInvQueueThreadSignaled, false);
1676 }
1677
1678 DMAR_LOCK(pDevIns, pThisR3);
1679 if (dmarInvQueueCanProcessRequests(pThis))
1680 {
1681 uint32_t offQueueHead;
1682 uint32_t offQueueTail;
1683 bool const fIsEmpty = dmarInvQueueIsEmptyEx(pThis, &offQueueHead, &offQueueTail);
1684 if (!fIsEmpty)
1685 {
1686 /*
1687 * Get the current queue size, descriptor width, queue base address and the
1688 * table translation mode while the lock is still held.
1689 */
1690 uint64_t const uIqaReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_IQA_REG);
1691 uint8_t const cQueuePages = 1 << (uIqaReg & VTD_BF_IQA_REG_QS_MASK);
1692 uint32_t const cbQueue = cQueuePages << X86_PAGE_SHIFT;
1693 uint8_t const fDw = RT_BF_GET(uIqaReg, VTD_BF_IQA_REG_DW);
1694 uint8_t const fTtm = RT_BF_GET(pThis->uRtaddrReg, VTD_BF_RTADDR_REG_TTM);
1695 RTGCPHYS const GCPhysRequests = (uIqaReg & VTD_BF_IQA_REG_IQA_MASK) + offQueueHead;
1696
1697 /* Paranoia. */
1698 Assert(cbQueue <= cbMaxQs);
1699 Assert(!(offQueueTail & ~VTD_IQT_REG_RW_MASK));
1700 Assert(!(offQueueHead & ~VTD_IQH_REG_RW_MASK));
1701 Assert(fDw != VTD_IQA_REG_DW_256_BIT || !(offQueueTail & RT_BIT(4)));
1702 Assert(fDw != VTD_IQA_REG_DW_256_BIT || !(offQueueHead & RT_BIT(4)));
1703 Assert(offQueueHead < cbQueue);
1704
1705 /*
1706 * A table translation mode of "reserved" isn't valid for any descriptor type.
1707 * However, RTADDR_REG can be modified in parallel to invalidation-queue processing,
1708 * but if ESRTPS is support, we will perform a global invalidation when software
1709 * changes RTADDR_REG, or it's the responsibility of software to do it explicitly.
1710 * So caching TTM while reading all descriptors should not be a problem.
1711 *
1712 * Also, validate the queue tail offset as it's mutable by software.
1713 */
1714 if ( fTtm != VTD_TTM_RSVD
1715 && offQueueTail < cbQueue)
1716 {
1717 /* Don't hold the lock while reading (a potentially large amount of) requests */
1718 DMAR_UNLOCK(pDevIns, pThisR3);
1719
1720 int rc;
1721 uint32_t cbRequests;
1722 if (offQueueTail > offQueueHead)
1723 {
1724 /* The requests have not wrapped around, read them in one go. */
1725 cbRequests = offQueueTail - offQueueHead;
1726 rc = PDMDevHlpPhysRead(pDevIns, GCPhysRequests, pvRequests, cbRequests);
1727 }
1728 else
1729 {
1730 /* The requests have wrapped around, read forward and wrapped-around. */
1731 uint32_t const cbForward = cbQueue - offQueueHead;
1732 rc = PDMDevHlpPhysRead(pDevIns, GCPhysRequests, pvRequests, cbForward);
1733
1734 uint32_t const cbWrapped = offQueueTail;
1735 if ( RT_SUCCESS(rc)
1736 && cbWrapped > 0)
1737 {
1738 rc = PDMDevHlpPhysRead(pDevIns, GCPhysRequests + cbForward,
1739 (void *)((uintptr_t)pvRequests + cbForward), cbWrapped);
1740 }
1741 cbRequests = cbForward + cbWrapped;
1742 }
1743
1744 /* Re-acquire the lock since we need to update device state. */
1745 DMAR_LOCK(pDevIns, pThisR3);
1746
1747 if (RT_SUCCESS(rc))
1748 {
1749 /* Indicate to software we've fetched all requests. */
1750 dmarRegWriteRaw64(pThis, VTD_MMIO_OFF_IQH_REG, offQueueTail);
1751
1752 /* Don't hold the lock while processing requests. */
1753 DMAR_UNLOCK(pDevIns, pThisR3);
1754
1755 /* Process all requests. */
1756 Assert(cbRequests <= cbQueue);
1757 dmarR3InvQueueProcessRequests(pDevIns, pvRequests, cbRequests, fDw, fTtm);
1758
1759 /*
1760 * We've processed all requests and the lock shouldn't be held at this point.
1761 * Using 'continue' here allows us to skip re-acquiring the lock just to release
1762 * it again before going back to the thread loop. It's a bit ugly but it certainly
1763 * helps with performance.
1764 */
1765 DMAR_ASSERT_LOCK_IS_NOT_OWNER(pDevIns, pThisR3);
1766 continue;
1767 }
1768 else
1769 dmarIqeFaultRecord(pDevIns, kDmarDiag_IqaReg_Dsc_Fetch_Error, kIqei_FetchDescriptorError);
1770 }
1771 else
1772 {
1773 if (fTtm == VTD_TTM_RSVD)
1774 dmarIqeFaultRecord(pDevIns, kDmarDiag_Iqei_Ttm_Rsvd, kIqei_InvalidTtm);
1775 else
1776 {
1777 Assert(offQueueTail >= cbQueue);
1778 dmarIqeFaultRecord(pDevIns, kDmarDiag_IqtReg_Qt_Invalid, kIqei_InvalidTailPointer);
1779 }
1780 }
1781 }
1782 }
1783 DMAR_UNLOCK(pDevIns, pThisR3);
1784 }
1785
1786 RTMemFree(pvRequests);
1787 pvRequests = NULL;
1788
1789 LogFlowFunc(("Invalidation-queue thread terminating\n"));
1790 return VINF_SUCCESS;
1791}
1792
1793
1794/**
1795 * Wakes up the invalidation-queue thread so it can respond to a state
1796 * change.
1797 *
1798 * @returns VBox status code.
1799 * @param pDevIns The IOMMU device instance.
1800 * @param pThread The invalidation-queue thread.
1801 *
1802 * @thread EMT.
1803 */
1804static DECLCALLBACK(int) dmarR3InvQueueThreadWakeUp(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
1805{
1806 RT_NOREF(pThread);
1807 LogFlowFunc(("\n"));
1808 PCDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1809 return PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hEvtInvQueue);
1810}
1811
1812
1813/**
1814 * @callback_method_impl{FNDBGFHANDLERDEV}
1815 */
1816static DECLCALLBACK(void) dmarR3DbgInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
1817{
1818 PCDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1819 PCDMARR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARR3);
1820 PCPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
1821 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
1822
1823 bool const fVerbose = RTStrCmp(pszArgs, "verbose") == 0;
1824
1825 /*
1826 * We lock the device to get a consistent register state, but it is
1827 * ASSUMED pHlp->pfnPrintf is expensive, so we copy the registers into
1828 * temporaries and release the lock ASAP.
1829 *
1830 * Order of register being read and outputted is in accordance with the
1831 * spec. for no particular reason.
1832 * See Intel VT-d spec. 10.4 "Register Descriptions".
1833 */
1834 DMAR_LOCK(pDevIns, pThisR3);
1835
1836 DMARDIAG const enmDiag = pThis->enmDiag;
1837 uint32_t const uVerReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_VER_REG);
1838 uint64_t const uCapReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_CAP_REG);
1839 uint64_t const uEcapReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_ECAP_REG);
1840 uint32_t const uGcmdReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_GCMD_REG);
1841 uint32_t const uGstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_GSTS_REG);
1842 uint64_t const uRtaddrReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_RTADDR_REG);
1843 uint64_t const uCcmdReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_CCMD_REG);
1844 uint32_t const uFstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FSTS_REG);
1845 uint32_t const uFectlReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FECTL_REG);
1846 uint32_t const uFedataReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FEDATA_REG);
1847 uint32_t const uFeaddrReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FEADDR_REG);
1848 uint32_t const uFeuaddrReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FEUADDR_REG);
1849 uint64_t const uAflogReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_AFLOG_REG);
1850 uint32_t const uPmenReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_PMEN_REG);
1851 uint32_t const uPlmbaseReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_PLMBASE_REG);
1852 uint32_t const uPlmlimitReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_PLMLIMIT_REG);
1853 uint64_t const uPhmbaseReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_PHMBASE_REG);
1854 uint64_t const uPhmlimitReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_PHMLIMIT_REG);
1855 uint64_t const uIqhReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_IQH_REG);
1856 uint64_t const uIqtReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_IQT_REG);
1857 uint64_t const uIqaReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_IQA_REG);
1858 uint32_t const uIcsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_ICS_REG);
1859 uint32_t const uIectlReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_IECTL_REG);
1860 uint32_t const uIedataReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_IEDATA_REG);
1861 uint32_t const uIeaddrReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_IEADDR_REG);
1862 uint32_t const uIeuaddrReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_IEUADDR_REG);
1863 uint64_t const uIqercdReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_IQERCD_REG);
1864 uint64_t const uIrtaReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_IRTA_REG);
1865 uint64_t const uPqhReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_PQH_REG);
1866 uint64_t const uPqtReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_PQT_REG);
1867 uint64_t const uPqaReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_PQA_REG);
1868 uint32_t const uPrsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_PRS_REG);
1869 uint32_t const uPectlReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_PECTL_REG);
1870 uint32_t const uPedataReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_PEDATA_REG);
1871 uint32_t const uPeaddrReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_PEADDR_REG);
1872 uint32_t const uPeuaddrReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_PEUADDR_REG);
1873 uint64_t const uMtrrcapReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_MTRRCAP_REG);
1874 uint64_t const uMtrrdefReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_MTRRDEF_REG);
1875 /** @todo Do other registers as required, we don't implement them for now. */
1876
1877 DMAR_UNLOCK(pDevIns, pThisR3);
1878
1879 const char *const pszDiag = enmDiag < RT_ELEMENTS(g_apszDmarDiagDesc) ? g_apszDmarDiagDesc[enmDiag] : "(Unknown)";
1880 pHlp->pfnPrintf(pHlp, "Intel-IOMMU:\n");
1881 pHlp->pfnPrintf(pHlp, " Diag = %s\n", pszDiag);
1882 if (!fVerbose)
1883 {
1884 pHlp->pfnPrintf(pHlp, " VER_REG = %#RX32\n", uVerReg);
1885 pHlp->pfnPrintf(pHlp, " CAP_REG = %#RX64\n", uCapReg);
1886 pHlp->pfnPrintf(pHlp, " ECAP_REG = %#RX64\n", uEcapReg);
1887 pHlp->pfnPrintf(pHlp, " GCMD_REG = %#RX32\n", uGcmdReg);
1888 pHlp->pfnPrintf(pHlp, " GSTS_REG = %#RX32\n", uGstsReg);
1889 pHlp->pfnPrintf(pHlp, " RTADDR_REG = %#RX64\n", uRtaddrReg);
1890 pHlp->pfnPrintf(pHlp, " CCMD_REG = %#RX64\n", uCcmdReg);
1891 pHlp->pfnPrintf(pHlp, " FSTS_REG = %#RX32\n", uFstsReg);
1892 pHlp->pfnPrintf(pHlp, " FECTL_REG = %#RX32\n", uFectlReg);
1893 pHlp->pfnPrintf(pHlp, " FEDATA_REG = %#RX32\n", uFedataReg);
1894 pHlp->pfnPrintf(pHlp, " FEADDR_REG = %#RX32\n", uFeaddrReg);
1895 pHlp->pfnPrintf(pHlp, " FEUADDR_REG = %#RX32\n", uFeuaddrReg);
1896 pHlp->pfnPrintf(pHlp, " AFLOG_REG = %#RX64\n", uAflogReg);
1897 pHlp->pfnPrintf(pHlp, " PMEN_REG = %#RX32\n", uPmenReg);
1898 pHlp->pfnPrintf(pHlp, " PLMBASE_REG = %#RX32\n", uPlmbaseReg);
1899 pHlp->pfnPrintf(pHlp, " PLMLIMIT_REG = %#RX32\n", uPlmlimitReg);
1900 pHlp->pfnPrintf(pHlp, " PHMBASE_REG = %#RX64\n", uPhmbaseReg);
1901 pHlp->pfnPrintf(pHlp, " PHMLIMIT_REG = %#RX64\n", uPhmlimitReg);
1902 pHlp->pfnPrintf(pHlp, " IQH_REG = %#RX64\n", uIqhReg);
1903 pHlp->pfnPrintf(pHlp, " IQT_REG = %#RX64\n", uIqtReg);
1904 pHlp->pfnPrintf(pHlp, " IQA_REG = %#RX64\n", uIqaReg);
1905 pHlp->pfnPrintf(pHlp, " ICS_REG = %#RX32\n", uIcsReg);
1906 pHlp->pfnPrintf(pHlp, " IECTL_REG = %#RX32\n", uIectlReg);
1907 pHlp->pfnPrintf(pHlp, " IEDATA_REG = %#RX32\n", uIedataReg);
1908 pHlp->pfnPrintf(pHlp, " IEADDR_REG = %#RX32\n", uIeaddrReg);
1909 pHlp->pfnPrintf(pHlp, " IEUADDR_REG = %#RX32\n", uIeuaddrReg);
1910 pHlp->pfnPrintf(pHlp, " IQERCD_REG = %#RX64\n", uIqercdReg);
1911 pHlp->pfnPrintf(pHlp, " IRTA_REG = %#RX64\n", uIrtaReg);
1912 pHlp->pfnPrintf(pHlp, " PQH_REG = %#RX64\n", uPqhReg);
1913 pHlp->pfnPrintf(pHlp, " PQT_REG = %#RX64\n", uPqtReg);
1914 pHlp->pfnPrintf(pHlp, " PQA_REG = %#RX64\n", uPqaReg);
1915 pHlp->pfnPrintf(pHlp, " PRS_REG = %#RX32\n", uPrsReg);
1916 pHlp->pfnPrintf(pHlp, " PECTL_REG = %#RX32\n", uPectlReg);
1917 pHlp->pfnPrintf(pHlp, " PEDATA_REG = %#RX32\n", uPedataReg);
1918 pHlp->pfnPrintf(pHlp, " PEADDR_REG = %#RX32\n", uPeaddrReg);
1919 pHlp->pfnPrintf(pHlp, " PEUADDR_REG = %#RX32\n", uPeuaddrReg);
1920 pHlp->pfnPrintf(pHlp, " MTRRCAP_REG = %#RX64\n", uMtrrcapReg);
1921 pHlp->pfnPrintf(pHlp, " MTRRDEF_REG = %#RX64\n", uMtrrdefReg);
1922 pHlp->pfnPrintf(pHlp, "\n");
1923 }
1924 else
1925 {
1926 pHlp->pfnPrintf(pHlp, " VER_REG = %#RX32\n", uVerReg);
1927 {
1928 pHlp->pfnPrintf(pHlp, " MAJ = %#x\n", RT_BF_GET(uVerReg, VTD_BF_VER_REG_MAX));
1929 pHlp->pfnPrintf(pHlp, " MIN = %#x\n", RT_BF_GET(uVerReg, VTD_BF_VER_REG_MIN));
1930 }
1931 pHlp->pfnPrintf(pHlp, " CAP_REG = %#RX64\n", uCapReg);
1932 {
1933 uint8_t const uSagaw = RT_BF_GET(uCapReg, VTD_BF_CAP_REG_SAGAW);
1934 uint8_t const uMgaw = RT_BF_GET(uCapReg, VTD_BF_CAP_REG_MGAW);
1935 uint8_t const uNfr = RT_BF_GET(uCapReg, VTD_BF_CAP_REG_NFR);
1936 pHlp->pfnPrintf(pHlp, " ND = %u\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_ND));
1937 pHlp->pfnPrintf(pHlp, " AFL = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_AFL));
1938 pHlp->pfnPrintf(pHlp, " RWBF = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_RWBF));
1939 pHlp->pfnPrintf(pHlp, " PLMR = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_PLMR));
1940 pHlp->pfnPrintf(pHlp, " PHMR = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_PHMR));
1941 pHlp->pfnPrintf(pHlp, " CM = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_CM));
1942 pHlp->pfnPrintf(pHlp, " SAGAW = %#x (%u bits)\n", uSagaw, vtdCapRegGetSagawBits(uSagaw));
1943 pHlp->pfnPrintf(pHlp, " MGAW = %#x (%u bits)\n", uMgaw, uMgaw + 1);
1944 pHlp->pfnPrintf(pHlp, " ZLR = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_ZLR));
1945 pHlp->pfnPrintf(pHlp, " FRO = %#x bytes\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_FRO));
1946 pHlp->pfnPrintf(pHlp, " SLLPS = %#x\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_SLLPS));
1947 pHlp->pfnPrintf(pHlp, " PSI = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_PSI));
1948 pHlp->pfnPrintf(pHlp, " NFR = %u (%u FRCD register%s)\n", uNfr, uNfr + 1, uNfr > 0 ? "s" : "");
1949 pHlp->pfnPrintf(pHlp, " MAMV = %#x\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_MAMV));
1950 pHlp->pfnPrintf(pHlp, " DWD = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_DWD));
1951 pHlp->pfnPrintf(pHlp, " DRD = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_DRD));
1952 pHlp->pfnPrintf(pHlp, " FL1GP = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_FL1GP));
1953 pHlp->pfnPrintf(pHlp, " PI = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_PI));
1954 pHlp->pfnPrintf(pHlp, " FL5LP = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_FL5LP));
1955 pHlp->pfnPrintf(pHlp, " ESIRTPS = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_ESIRTPS));
1956 pHlp->pfnPrintf(pHlp, " ESRTPS = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_ESRTPS));
1957 }
1958 pHlp->pfnPrintf(pHlp, " ECAP_REG = %#RX64\n", uEcapReg);
1959 {
1960 uint8_t const uPss = RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_PSS);
1961 pHlp->pfnPrintf(pHlp, " C = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_C));
1962 pHlp->pfnPrintf(pHlp, " QI = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_QI));
1963 pHlp->pfnPrintf(pHlp, " DT = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_DT));
1964 pHlp->pfnPrintf(pHlp, " IR = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_IR));
1965 pHlp->pfnPrintf(pHlp, " EIM = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_EIM));
1966 pHlp->pfnPrintf(pHlp, " PT = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_PT));
1967 pHlp->pfnPrintf(pHlp, " SC = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_SC));
1968 pHlp->pfnPrintf(pHlp, " IRO = %#x bytes\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_IRO));
1969 pHlp->pfnPrintf(pHlp, " MHMV = %#x\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_MHMV));
1970 pHlp->pfnPrintf(pHlp, " MTS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_MTS));
1971 pHlp->pfnPrintf(pHlp, " NEST = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_NEST));
1972 pHlp->pfnPrintf(pHlp, " PRS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_PRS));
1973 pHlp->pfnPrintf(pHlp, " ERS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_ERS));
1974 pHlp->pfnPrintf(pHlp, " SRS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_SRS));
1975 pHlp->pfnPrintf(pHlp, " NWFS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_NWFS));
1976 pHlp->pfnPrintf(pHlp, " EAFS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_EAFS));
1977 pHlp->pfnPrintf(pHlp, " PSS = %u (%u bits)\n", uPss, uPss > 0 ? uPss + 1 : 0);
1978 pHlp->pfnPrintf(pHlp, " PASID = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_PASID));
1979 pHlp->pfnPrintf(pHlp, " DIT = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_DIT));
1980 pHlp->pfnPrintf(pHlp, " PDS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_PDS));
1981 pHlp->pfnPrintf(pHlp, " SMTS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_SMTS));
1982 pHlp->pfnPrintf(pHlp, " VCS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_VCS));
1983 pHlp->pfnPrintf(pHlp, " SLADS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_SLADS));
1984 pHlp->pfnPrintf(pHlp, " SLTS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_SLTS));
1985 pHlp->pfnPrintf(pHlp, " FLTS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_FLTS));
1986 pHlp->pfnPrintf(pHlp, " SMPWCS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_SMPWCS));
1987 pHlp->pfnPrintf(pHlp, " RPS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_RPS));
1988 pHlp->pfnPrintf(pHlp, " ADMS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_ADMS));
1989 pHlp->pfnPrintf(pHlp, " RPRIVS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_RPRIVS));
1990 }
1991 pHlp->pfnPrintf(pHlp, " GCMD_REG = %#RX32\n", uGcmdReg);
1992 {
1993 uint8_t const fCfi = RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_CFI);
1994 pHlp->pfnPrintf(pHlp, " CFI = %u (%s)\n", fCfi, fCfi ? "Bypass interrupt remapping"
1995 : "Block compatible format interrupts");
1996 pHlp->pfnPrintf(pHlp, " SIRTP = %u\n", RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_SIRTP));
1997 pHlp->pfnPrintf(pHlp, " IRE = %u\n", RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_IRE));
1998 pHlp->pfnPrintf(pHlp, " QIE = %u\n", RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_QIE));
1999 pHlp->pfnPrintf(pHlp, " WBF = %u\n", RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_WBF));
2000 pHlp->pfnPrintf(pHlp, " EAFL = %u\n", RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_SFL));
2001 pHlp->pfnPrintf(pHlp, " SFL = %u\n", RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_SFL));
2002 pHlp->pfnPrintf(pHlp, " SRTP = %u\n", RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_SRTP));
2003 pHlp->pfnPrintf(pHlp, " TE = %u\n", RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_TE));
2004 }
2005 pHlp->pfnPrintf(pHlp, " GSTS_REG = %#RX32\n", uGstsReg);
2006 {
2007 uint8_t const fCfis = RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_CFIS);
2008 pHlp->pfnPrintf(pHlp, " CFIS = %u (%s)\n", fCfis, fCfis ? "Bypass interrupt remapping"
2009 : "Block compatible format interrupts");
2010 pHlp->pfnPrintf(pHlp, " IRTPS = %u\n", RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_IRTPS));
2011 pHlp->pfnPrintf(pHlp, " IRES = %u\n", RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_IRES));
2012 pHlp->pfnPrintf(pHlp, " QIES = %u\n", RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_QIES));
2013 pHlp->pfnPrintf(pHlp, " WBFS = %u\n", RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_WBFS));
2014 pHlp->pfnPrintf(pHlp, " AFLS = %u\n", RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_AFLS));
2015 pHlp->pfnPrintf(pHlp, " FLS = %u\n", RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_FLS));
2016 pHlp->pfnPrintf(pHlp, " RTPS = %u\n", RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_RTPS));
2017 pHlp->pfnPrintf(pHlp, " TES = %u\n", RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_TES));
2018 }
2019 pHlp->pfnPrintf(pHlp, " RTADDR_REG = %#RX64\n", uRtaddrReg);
2020 {
2021 uint8_t const uTtm = RT_BF_GET(uRtaddrReg, VTD_BF_RTADDR_REG_TTM);
2022 pHlp->pfnPrintf(pHlp, " TTM = %u (%s)\n", uTtm, vtdRtaddrRegGetTtmDesc(uTtm));
2023 pHlp->pfnPrintf(pHlp, " RTA = %#RX64\n", RT_BF_GET(uRtaddrReg, VTD_BF_RTADDR_REG_RTA));
2024 }
2025 pHlp->pfnPrintf(pHlp, " CCMD_REG = %#RX64\n", uCcmdReg);
2026 pHlp->pfnPrintf(pHlp, " FSTS_REG = %#RX32\n", uFstsReg);
2027 {
2028 pHlp->pfnPrintf(pHlp, " PFO = %u\n", RT_BF_GET(uFstsReg, VTD_BF_FSTS_REG_PFO));
2029 pHlp->pfnPrintf(pHlp, " PPF = %u\n", RT_BF_GET(uFstsReg, VTD_BF_FSTS_REG_PPF));
2030 pHlp->pfnPrintf(pHlp, " AFO = %u\n", RT_BF_GET(uFstsReg, VTD_BF_FSTS_REG_AFO));
2031 pHlp->pfnPrintf(pHlp, " APF = %u\n", RT_BF_GET(uFstsReg, VTD_BF_FSTS_REG_APF));
2032 pHlp->pfnPrintf(pHlp, " IQE = %u\n", RT_BF_GET(uFstsReg, VTD_BF_FSTS_REG_IQE));
2033 pHlp->pfnPrintf(pHlp, " ICS = %u\n", RT_BF_GET(uFstsReg, VTD_BF_FSTS_REG_ICE));
2034 pHlp->pfnPrintf(pHlp, " ITE = %u\n", RT_BF_GET(uFstsReg, VTD_BF_FSTS_REG_ITE));
2035 pHlp->pfnPrintf(pHlp, " FRI = %u\n", RT_BF_GET(uFstsReg, VTD_BF_FSTS_REG_FRI));
2036 }
2037 pHlp->pfnPrintf(pHlp, " FECTL_REG = %#RX32\n", uFectlReg);
2038 {
2039 pHlp->pfnPrintf(pHlp, " IM = %RTbool\n", RT_BF_GET(uFectlReg, VTD_BF_FECTL_REG_IM));
2040 pHlp->pfnPrintf(pHlp, " IP = %RTbool\n", RT_BF_GET(uFectlReg, VTD_BF_FECTL_REG_IP));
2041 }
2042 pHlp->pfnPrintf(pHlp, " FEDATA_REG = %#RX32\n", uFedataReg);
2043 pHlp->pfnPrintf(pHlp, " FEADDR_REG = %#RX32\n", uFeaddrReg);
2044 pHlp->pfnPrintf(pHlp, " FEUADDR_REG = %#RX32\n", uFeuaddrReg);
2045 /** @todo Verbose others as needed during debugging/rainy day. */
2046 pHlp->pfnPrintf(pHlp, " AFLOG_REG = %#RX64\n", uAflogReg);
2047 pHlp->pfnPrintf(pHlp, " PMEN_REG = %#RX32\n", uPmenReg);
2048 pHlp->pfnPrintf(pHlp, " PLMBASE_REG = %#RX32\n", uPlmbaseReg);
2049 pHlp->pfnPrintf(pHlp, " PLMLIMIT_REG = %#RX32\n", uPlmlimitReg);
2050 pHlp->pfnPrintf(pHlp, " PHMBASE_REG = %#RX64\n", uPhmbaseReg);
2051 pHlp->pfnPrintf(pHlp, " PHMLIMIT_REG = %#RX64\n", uPhmlimitReg);
2052 pHlp->pfnPrintf(pHlp, " IQH_REG = %#RX64\n", uIqhReg);
2053 pHlp->pfnPrintf(pHlp, " IQT_REG = %#RX64\n", uIqtReg);
2054 pHlp->pfnPrintf(pHlp, " IQA_REG = %#RX64\n", uIqaReg);
2055 {
2056 uint8_t const fDw = RT_BF_GET(uIqaReg, VTD_BF_IQA_REG_DW);
2057 uint8_t const fQs = RT_BF_GET(uIqaReg, VTD_BF_IQA_REG_QS);
2058 uint8_t const cQueuePages = 1 << fQs;
2059 pHlp->pfnPrintf(pHlp, " DW = %u (%s)\n", fDw, fDw == VTD_IQA_REG_DW_128_BIT ? "128-bit" : "256-bit");
2060 pHlp->pfnPrintf(pHlp, " QS = %u (%u page%s)\n", fQs, cQueuePages, cQueuePages > 1 ? "s" : "");
2061 }
2062 pHlp->pfnPrintf(pHlp, " ICS_REG = %#RX32\n", uIcsReg);
2063 {
2064 pHlp->pfnPrintf(pHlp, " IWC = %u\n", RT_BF_GET(uIcsReg, VTD_BF_ICS_REG_IWC));
2065 }
2066 pHlp->pfnPrintf(pHlp, " IECTL_REG = %#RX32\n", uIectlReg);
2067 {
2068 pHlp->pfnPrintf(pHlp, " IM = %RTbool\n", RT_BF_GET(uIectlReg, VTD_BF_IECTL_REG_IM));
2069 pHlp->pfnPrintf(pHlp, " IP = %RTbool\n", RT_BF_GET(uIectlReg, VTD_BF_IECTL_REG_IP));
2070 }
2071 pHlp->pfnPrintf(pHlp, " IEDATA_REG = %#RX32\n", uIedataReg);
2072 pHlp->pfnPrintf(pHlp, " IEADDR_REG = %#RX32\n", uIeaddrReg);
2073 pHlp->pfnPrintf(pHlp, " IEUADDR_REG = %#RX32\n", uIeuaddrReg);
2074 pHlp->pfnPrintf(pHlp, " IQERCD_REG = %#RX64\n", uIqercdReg);
2075 {
2076 pHlp->pfnPrintf(pHlp, " ICESID = %#RX32\n", RT_BF_GET(uIqercdReg, VTD_BF_IQERCD_REG_ICESID));
2077 pHlp->pfnPrintf(pHlp, " ITESID = %#RX32\n", RT_BF_GET(uIqercdReg, VTD_BF_IQERCD_REG_ITESID));
2078 pHlp->pfnPrintf(pHlp, " IQEI = %#RX32\n", RT_BF_GET(uIqercdReg, VTD_BF_IQERCD_REG_IQEI));
2079 }
2080 pHlp->pfnPrintf(pHlp, " IRTA_REG = %#RX64\n", uIrtaReg);
2081 pHlp->pfnPrintf(pHlp, " PQH_REG = %#RX64\n", uPqhReg);
2082 pHlp->pfnPrintf(pHlp, " PQT_REG = %#RX64\n", uPqtReg);
2083 pHlp->pfnPrintf(pHlp, " PQA_REG = %#RX64\n", uPqaReg);
2084 pHlp->pfnPrintf(pHlp, " PRS_REG = %#RX32\n", uPrsReg);
2085 pHlp->pfnPrintf(pHlp, " PECTL_REG = %#RX32\n", uPectlReg);
2086 pHlp->pfnPrintf(pHlp, " PEDATA_REG = %#RX32\n", uPedataReg);
2087 pHlp->pfnPrintf(pHlp, " PEADDR_REG = %#RX32\n", uPeaddrReg);
2088 pHlp->pfnPrintf(pHlp, " PEUADDR_REG = %#RX32\n", uPeuaddrReg);
2089 pHlp->pfnPrintf(pHlp, " MTRRCAP_REG = %#RX64\n", uMtrrcapReg);
2090 pHlp->pfnPrintf(pHlp, " MTRRDEF_REG = %#RX64\n", uMtrrdefReg);
2091 pHlp->pfnPrintf(pHlp, "\n");
2092 }
2093}
2094
2095
2096/**
2097 * Initializes all registers in the DMAR unit.
2098 *
2099 * @param pDevIns The IOMMU device instance.
2100 */
2101static void dmarR3RegsInit(PPDMDEVINS pDevIns)
2102{
2103 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
2104
2105 /*
2106 * Wipe all registers (required on reset).
2107 */
2108 RT_ZERO(pThis->abRegs0);
2109 RT_ZERO(pThis->abRegs1);
2110
2111 /*
2112 * Initialize registers not mutable by software prior to initializing other registers.
2113 */
2114 /* VER_REG */
2115 {
2116 pThis->uVerReg = RT_BF_MAKE(VTD_BF_VER_REG_MIN, DMAR_VER_MINOR)
2117 | RT_BF_MAKE(VTD_BF_VER_REG_MAX, DMAR_VER_MAJOR);
2118 dmarRegWriteRaw64(pThis, VTD_MMIO_OFF_VER_REG, pThis->uVerReg);
2119 }
2120
2121 uint8_t const fFlts = 1; /* First-Level translation support. */
2122 uint8_t const fSlts = 1; /* Second-Level translation support. */
2123 uint8_t const fPt = 1; /* Pass-Through support. */
2124 uint8_t const fSmts = fFlts & fSlts & fPt; /* Scalable mode translation support.*/
2125 uint8_t const fNest = 0; /* Nested translation support. */
2126
2127 /* CAP_REG */
2128 {
2129 uint8_t cGstPhysAddrBits;
2130 uint8_t cGstLinearAddrBits;
2131 PDMDevHlpCpuGetGuestAddrWidths(pDevIns, &cGstPhysAddrBits, &cGstLinearAddrBits);
2132
2133 uint8_t const fFl1gp = 1; /* First-Level 1GB pages support. */
2134 uint8_t const fFl5lp = 1; /* First-level 5-level paging support (PML5E). */
2135 uint8_t const fSl2mp = fSlts & 1; /* Second-Level 2MB pages support. */
2136 uint8_t const fSl2gp = fSlts & 1; /* Second-Level 1GB pages support. */
2137 uint8_t const fSllps = fSl2mp /* Second-Level large page Support. */
2138 | ((fSl2mp & fFl1gp) & RT_BIT(1));
2139 uint8_t const fMamv = (fSl2gp ? X86_PAGE_1G_SHIFT /* Maximum address mask value (for 2nd-level invalidations). */
2140 : X86_PAGE_2M_SHIFT)
2141 - X86_PAGE_4K_SHIFT;
2142 uint8_t const fNd = 2; /* Number of domains supported (0=16, 1=64, 2=256, 3=1K, 4=4K,
2143 5=16K, 6=64K, 7=Reserved). */
2144 uint8_t const fPsi = 1; /* Page selective invalidation. */
2145 uint8_t const uMgaw = cGstPhysAddrBits - 1; /* Maximum guest address width. */
2146 uint8_t const uSagaw = vtdCapRegGetSagaw(uMgaw); /* Supported adjust guest address width. */
2147 uint16_t const offFro = DMAR_MMIO_OFF_FRCD_LO_REG >> 4; /* MMIO offset of FRCD registers. */
2148 uint8_t const fEsrtps = 1; /* Enhanced SRTPS (auto invalidate cache on SRTP). */
2149 uint8_t const fEsirtps = 1; /* Enhanced SIRTPS (auto invalidate cache on SIRTP). */
2150
2151 pThis->fCapReg = RT_BF_MAKE(VTD_BF_CAP_REG_ND, fNd)
2152 | RT_BF_MAKE(VTD_BF_CAP_REG_AFL, 0) /* Advanced fault logging not supported. */
2153 | RT_BF_MAKE(VTD_BF_CAP_REG_RWBF, 0) /* Software need not flush write-buffers. */
2154 | RT_BF_MAKE(VTD_BF_CAP_REG_PLMR, 0) /* Protected Low-Memory Region not supported. */
2155 | RT_BF_MAKE(VTD_BF_CAP_REG_PHMR, 0) /* Protected High-Memory Region not supported. */
2156 | RT_BF_MAKE(VTD_BF_CAP_REG_CM, 1) /** @todo Figure out if required when we impl. caching. */
2157 | RT_BF_MAKE(VTD_BF_CAP_REG_SAGAW, fSlts & uSagaw)
2158 | RT_BF_MAKE(VTD_BF_CAP_REG_MGAW, uMgaw)
2159 | RT_BF_MAKE(VTD_BF_CAP_REG_ZLR, 1) /** @todo Figure out if/how to support zero-length reads. */
2160 | RT_BF_MAKE(VTD_BF_CAP_REG_FRO, offFro)
2161 | RT_BF_MAKE(VTD_BF_CAP_REG_SLLPS, fSlts & fSllps)
2162 | RT_BF_MAKE(VTD_BF_CAP_REG_PSI, fPsi)
2163 | RT_BF_MAKE(VTD_BF_CAP_REG_NFR, DMAR_FRCD_REG_COUNT - 1)
2164 | RT_BF_MAKE(VTD_BF_CAP_REG_MAMV, fPsi & fMamv)
2165 | RT_BF_MAKE(VTD_BF_CAP_REG_DWD, 1)
2166 | RT_BF_MAKE(VTD_BF_CAP_REG_DRD, 1)
2167 | RT_BF_MAKE(VTD_BF_CAP_REG_FL1GP, fFlts & fFl1gp)
2168 | RT_BF_MAKE(VTD_BF_CAP_REG_PI, 0) /* Posted Interrupts not supported. */
2169 | RT_BF_MAKE(VTD_BF_CAP_REG_FL5LP, fFlts & fFl5lp)
2170 | RT_BF_MAKE(VTD_BF_CAP_REG_ESIRTPS, fEsirtps)
2171 | RT_BF_MAKE(VTD_BF_CAP_REG_ESRTPS, fEsrtps);
2172 dmarRegWriteRaw64(pThis, VTD_MMIO_OFF_CAP_REG, pThis->fCapReg);
2173 }
2174
2175 /* ECAP_REG */
2176 {
2177 uint8_t const fQi = 1; /* Queued-invalidations. */
2178 uint8_t const fIr = !!(DMAR_ACPI_DMAR_FLAGS & ACPI_DMAR_F_INTR_REMAP); /* Interrupt remapping support. */
2179 uint8_t const fMhmv = 0xf; /* Maximum handle mask value. */
2180 uint16_t const offIro = DMAR_MMIO_OFF_IVA_REG >> 4; /* MMIO offset of IOTLB registers. */
2181 uint8_t const fSrs = 1; /* Supervisor request support. */
2182 uint8_t const fEim = 1; /* Extended interrupt mode.*/
2183 uint8_t const fAdms = 1; /* Abort DMA mode support. */
2184
2185 pThis->fExtCapReg = RT_BF_MAKE(VTD_BF_ECAP_REG_C, 0) /* Accesses don't snoop CPU cache. */
2186 | RT_BF_MAKE(VTD_BF_ECAP_REG_QI, fQi)
2187 | RT_BF_MAKE(VTD_BF_ECAP_REG_DT, 0) /* Device-TLBs not supported. */
2188 | RT_BF_MAKE(VTD_BF_ECAP_REG_IR, fQi & fIr)
2189 | RT_BF_MAKE(VTD_BF_ECAP_REG_EIM, fIr & fEim)
2190 | RT_BF_MAKE(VTD_BF_ECAP_REG_PT, fPt)
2191 | RT_BF_MAKE(VTD_BF_ECAP_REG_SC, 0) /* Snoop control not supported. */
2192 | RT_BF_MAKE(VTD_BF_ECAP_REG_IRO, offIro)
2193 | RT_BF_MAKE(VTD_BF_ECAP_REG_MHMV, fIr & fMhmv)
2194 | RT_BF_MAKE(VTD_BF_ECAP_REG_MTS, 0) /* Memory type not supported. */
2195 | RT_BF_MAKE(VTD_BF_ECAP_REG_NEST, fNest)
2196 | RT_BF_MAKE(VTD_BF_ECAP_REG_PRS, 0) /* 0 as DT not supported. */
2197 | RT_BF_MAKE(VTD_BF_ECAP_REG_ERS, 0) /* Execute request not supported. */
2198 | RT_BF_MAKE(VTD_BF_ECAP_REG_SRS, fSmts & fSrs)
2199 | RT_BF_MAKE(VTD_BF_ECAP_REG_NWFS, 0) /* 0 as DT not supported. */
2200 | RT_BF_MAKE(VTD_BF_ECAP_REG_EAFS, 0) /** @todo figure out if EAFS is required? */
2201 | RT_BF_MAKE(VTD_BF_ECAP_REG_PSS, 0) /* 0 as PASID not supported. */
2202 | RT_BF_MAKE(VTD_BF_ECAP_REG_PASID, 0) /* PASID support. */
2203 | RT_BF_MAKE(VTD_BF_ECAP_REG_DIT, 0) /* 0 as DT not supported. */
2204 | RT_BF_MAKE(VTD_BF_ECAP_REG_PDS, 0) /* 0 as DT not supported. */
2205 | RT_BF_MAKE(VTD_BF_ECAP_REG_SMTS, fSmts)
2206 | RT_BF_MAKE(VTD_BF_ECAP_REG_VCS, 0) /* 0 as PASID not supported (commands seem PASID specific). */
2207 | RT_BF_MAKE(VTD_BF_ECAP_REG_SLADS, 0) /* Second-level accessed/dirty not supported. */
2208 | RT_BF_MAKE(VTD_BF_ECAP_REG_SLTS, fSlts)
2209 | RT_BF_MAKE(VTD_BF_ECAP_REG_FLTS, fFlts)
2210 | RT_BF_MAKE(VTD_BF_ECAP_REG_SMPWCS, 0) /* 0 as PASID not supported. */
2211 | RT_BF_MAKE(VTD_BF_ECAP_REG_RPS, 0) /* We don't support RID_PASID field in SM context entry. */
2212 | RT_BF_MAKE(VTD_BF_ECAP_REG_ADMS, fAdms)
2213 | RT_BF_MAKE(VTD_BF_ECAP_REG_RPRIVS, 0); /** @todo figure out if we should/can support this? */
2214 dmarRegWriteRaw64(pThis, VTD_MMIO_OFF_ECAP_REG, pThis->fExtCapReg);
2215 }
2216
2217 /*
2218 * Initialize registers mutable by software.
2219 */
2220 /* FECTL_REG */
2221 {
2222 uint32_t const uCtl = RT_BF_MAKE(VTD_BF_FECTL_REG_IM, 1);
2223 dmarRegWriteRaw32(pThis, VTD_MMIO_OFF_FECTL_REG, uCtl);
2224 }
2225
2226 /* ICETL_REG */
2227 {
2228 uint32_t const uCtl = RT_BF_MAKE(VTD_BF_IECTL_REG_IM, 1);
2229 dmarRegWriteRaw32(pThis, VTD_MMIO_OFF_IECTL_REG, uCtl);
2230 }
2231
2232#ifdef VBOX_STRICT
2233 Assert(!RT_BF_GET(pThis->fExtCapReg, VTD_BF_ECAP_REG_PRS)); /* PECTL_REG - Reserved if don't support PRS. */
2234 Assert(!RT_BF_GET(pThis->fExtCapReg, VTD_BF_ECAP_REG_MTS)); /* MTRRCAP_REG - Reserved if we don't support MTS. */
2235#endif
2236}
2237
2238
2239/**
2240 * @interface_method_impl{PDMDEVREG,pfnReset}
2241 */
2242static DECLCALLBACK(void) iommuIntelR3Reset(PPDMDEVINS pDevIns)
2243{
2244 RT_NOREF1(pDevIns);
2245 LogFlowFunc(("\n"));
2246
2247 PCDMARR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARR3);
2248 DMAR_LOCK(pDevIns, pThisR3);
2249
2250 dmarR3RegsInit(pDevIns);
2251
2252 DMAR_UNLOCK(pDevIns, pThisR3);
2253}
2254
2255
2256/**
2257 * @interface_method_impl{PDMDEVREG,pfnDestruct}
2258 */
2259static DECLCALLBACK(int) iommuIntelR3Destruct(PPDMDEVINS pDevIns)
2260{
2261 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
2262 PCDMARR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARR3);
2263 LogFlowFunc(("\n"));
2264
2265 DMAR_LOCK(pDevIns, pThisR3);
2266
2267 if (pThis->hEvtInvQueue != NIL_SUPSEMEVENT)
2268 {
2269 PDMDevHlpSUPSemEventClose(pDevIns, pThis->hEvtInvQueue);
2270 pThis->hEvtInvQueue = NIL_SUPSEMEVENT;
2271 }
2272
2273 DMAR_UNLOCK(pDevIns, pThisR3);
2274 return VINF_SUCCESS;
2275}
2276
2277
2278/**
2279 * @interface_method_impl{PDMDEVREG,pfnConstruct}
2280 */
2281static DECLCALLBACK(int) iommuIntelR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
2282{
2283 RT_NOREF(pCfg);
2284
2285 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
2286 PDMARR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PDMARR3);
2287 pThisR3->pDevInsR3 = pDevIns;
2288
2289 LogFlowFunc(("iInstance=%d\n", iInstance));
2290 NOREF(iInstance);
2291
2292 /*
2293 * Register the IOMMU with PDM.
2294 */
2295 PDMIOMMUREGR3 IommuReg;
2296 RT_ZERO(IommuReg);
2297 IommuReg.u32Version = PDM_IOMMUREGCC_VERSION;
2298 IommuReg.pfnMemAccess = iommuIntelMemAccess;
2299 IommuReg.pfnMemBulkAccess = iommuIntelMemBulkAccess;
2300 IommuReg.pfnMsiRemap = iommuIntelMsiRemap;
2301 IommuReg.u32TheEnd = PDM_IOMMUREGCC_VERSION;
2302 int rc = PDMDevHlpIommuRegister(pDevIns, &IommuReg, &pThisR3->CTX_SUFF(pIommuHlp), &pThis->idxIommu);
2303 if (RT_FAILURE(rc))
2304 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to register ourselves as an IOMMU device"));
2305 if (pThisR3->CTX_SUFF(pIommuHlp)->u32Version != PDM_IOMMUHLPR3_VERSION)
2306 return PDMDevHlpVMSetError(pDevIns, VERR_VERSION_MISMATCH, RT_SRC_POS,
2307 N_("IOMMU helper version mismatch; got %#x expected %#x"),
2308 pThisR3->CTX_SUFF(pIommuHlp)->u32Version, PDM_IOMMUHLPR3_VERSION);
2309 if (pThisR3->CTX_SUFF(pIommuHlp)->u32TheEnd != PDM_IOMMUHLPR3_VERSION)
2310 return PDMDevHlpVMSetError(pDevIns, VERR_VERSION_MISMATCH, RT_SRC_POS,
2311 N_("IOMMU helper end-version mismatch; got %#x expected %#x"),
2312 pThisR3->CTX_SUFF(pIommuHlp)->u32TheEnd, PDM_IOMMUHLPR3_VERSION);
2313 /*
2314 * Use PDM's critical section (via helpers) for the IOMMU device.
2315 */
2316 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
2317 AssertRCReturn(rc, rc);
2318
2319 /*
2320 * Initialize PCI configuration registers.
2321 */
2322 PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
2323 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
2324
2325 /* Header. */
2326 PDMPciDevSetVendorId(pPciDev, DMAR_PCI_VENDOR_ID); /* Intel */
2327 PDMPciDevSetDeviceId(pPciDev, DMAR_PCI_DEVICE_ID); /* VirtualBox DMAR device */
2328 PDMPciDevSetRevisionId(pPciDev, DMAR_PCI_REVISION_ID); /* VirtualBox specific device implementation revision */
2329 PDMPciDevSetClassBase(pPciDev, VBOX_PCI_CLASS_SYSTEM); /* System Base Peripheral */
2330 PDMPciDevSetClassSub(pPciDev, VBOX_PCI_SUB_SYSTEM_OTHER); /* Other */
2331 PDMPciDevSetHeaderType(pPciDev, 0); /* Single function, type 0 */
2332 PDMPciDevSetSubSystemId(pPciDev, DMAR_PCI_DEVICE_ID); /* VirtualBox DMAR device */
2333 PDMPciDevSetSubSystemVendorId(pPciDev, DMAR_PCI_VENDOR_ID); /* Intel */
2334
2335 /** @todo Chipset spec says PCI Express Capability Id. Relevant for us? */
2336 PDMPciDevSetStatus(pPciDev, 0);
2337 PDMPciDevSetCapabilityList(pPciDev, 0);
2338
2339 /** @todo VTBAR at 0x180? */
2340
2341 /*
2342 * Register the PCI function with PDM.
2343 */
2344 rc = PDMDevHlpPCIRegister(pDevIns, pPciDev);
2345 AssertLogRelRCReturn(rc, rc);
2346
2347 /** @todo Register MSI but what's the MSI capability offset? */
2348#if 0
2349 /*
2350 * Register MSI support for the PCI device.
2351 * This must be done -after- registering it as a PCI device!
2352 */
2353#endif
2354
2355 /*
2356 * Register MMIO region.
2357 */
2358 AssertCompile(!(DMAR_MMIO_BASE_PHYSADDR & X86_PAGE_4K_OFFSET_MASK));
2359 rc = PDMDevHlpMmioCreateAndMap(pDevIns, DMAR_MMIO_BASE_PHYSADDR, DMAR_MMIO_SIZE, dmarMmioWrite, dmarMmioRead,
2360 IOMMMIO_FLAGS_READ_DWORD_QWORD | IOMMMIO_FLAGS_WRITE_DWORD_QWORD_ZEROED, "Intel-IOMMU",
2361 &pThis->hMmio);
2362 AssertLogRelRCReturn(rc, rc);
2363
2364 /*
2365 * Register debugger info items.
2366 */
2367 rc = PDMDevHlpDBGFInfoRegister(pDevIns, "iommu", "Display IOMMU state.", dmarR3DbgInfo);
2368 AssertLogRelRCReturn(rc, rc);
2369
2370#ifdef VBOX_WITH_STATISTICS
2371 /*
2372 * Statistics.
2373 */
2374 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioReadR3, STAMTYPE_COUNTER, "R3/MmioRead", STAMUNIT_OCCURENCES, "Number of MMIO reads in R3");
2375 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioReadRZ, STAMTYPE_COUNTER, "RZ/MmioRead", STAMUNIT_OCCURENCES, "Number of MMIO reads in RZ.");
2376
2377 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioWriteR3, STAMTYPE_COUNTER, "R3/MmioWrite", STAMUNIT_OCCURENCES, "Number of MMIO writes in R3.");
2378 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioWriteRZ, STAMTYPE_COUNTER, "RZ/MmioWrite", STAMUNIT_OCCURENCES, "Number of MMIO writes in RZ.");
2379
2380 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMsiRemapR3, STAMTYPE_COUNTER, "R3/MsiRemap", STAMUNIT_OCCURENCES, "Number of interrupt remap requests in R3.");
2381 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMsiRemapRZ, STAMTYPE_COUNTER, "RZ/MsiRemap", STAMUNIT_OCCURENCES, "Number of interrupt remap requests in RZ.");
2382
2383 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemReadR3, STAMTYPE_COUNTER, "R3/MemRead", STAMUNIT_OCCURENCES, "Number of memory read translation requests in R3.");
2384 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemReadRZ, STAMTYPE_COUNTER, "RZ/MemRead", STAMUNIT_OCCURENCES, "Number of memory read translation requests in RZ.");
2385
2386 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemWriteR3, STAMTYPE_COUNTER, "R3/MemWrite", STAMUNIT_OCCURENCES, "Number of memory write translation requests in R3.");
2387 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemWriteRZ, STAMTYPE_COUNTER, "RZ/MemWrite", STAMUNIT_OCCURENCES, "Number of memory write translation requests in RZ.");
2388
2389 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemBulkReadR3, STAMTYPE_COUNTER, "R3/MemBulkRead", STAMUNIT_OCCURENCES, "Number of memory bulk read translation requests in R3.");
2390 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemBulkReadRZ, STAMTYPE_COUNTER, "RZ/MemBulkRead", STAMUNIT_OCCURENCES, "Number of memory bulk read translation requests in RZ.");
2391
2392 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemBulkWriteR3, STAMTYPE_COUNTER, "R3/MemBulkWrite", STAMUNIT_OCCURENCES, "Number of memory bulk write translation requests in R3.");
2393 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemBulkWriteRZ, STAMTYPE_COUNTER, "RZ/MemBulkWrite", STAMUNIT_OCCURENCES, "Number of memory bulk write translation requests in RZ.");
2394
2395 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCcInvDsc, STAMTYPE_COUNTER, "R3/QI/CcInv", STAMUNIT_OCCURENCES, "Number of cc_inv_dsc processed.");
2396 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIotlbInvDsc, STAMTYPE_COUNTER, "R3/QI/IotlbInv", STAMUNIT_OCCURENCES, "Number of iotlb_inv_dsc processed.");
2397 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatDevtlbInvDsc, STAMTYPE_COUNTER, "R3/QI/DevtlbInv", STAMUNIT_OCCURENCES, "Number of dev_tlb_inv_dsc processed.");
2398 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIecInvDsc, STAMTYPE_COUNTER, "R3/QI/IecInv", STAMUNIT_OCCURENCES, "Number of iec_inv processed.");
2399 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatInvWaitDsc, STAMTYPE_COUNTER, "R3/QI/InvWait", STAMUNIT_OCCURENCES, "Number of inv_wait_dsc processed.");
2400 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatPasidIotlbInvDsc, STAMTYPE_COUNTER, "R3/QI/PasidIotlbInv", STAMUNIT_OCCURENCES, "Number of p_iotlb_inv_dsc processed.");
2401 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatPasidCacheInvDsc, STAMTYPE_COUNTER, "R3/QI/PasidCacheInv", STAMUNIT_OCCURENCES, "Number of pc_inv_dsc pprocessed.");
2402 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatPasidDevtlbInvDsc, STAMTYPE_COUNTER, "R3/QI/PasidDevtlbInv", STAMUNIT_OCCURENCES, "Number of p_dev_tlb_inv_dsc processed.");
2403#endif
2404
2405 /*
2406 * Initialize registers.
2407 */
2408 dmarR3RegsInit(pDevIns);
2409
2410 /*
2411 * Create invalidation-queue thread and semaphore.
2412 */
2413 char szInvQueueThread[32];
2414 RT_ZERO(szInvQueueThread);
2415 RTStrPrintf(szInvQueueThread, sizeof(szInvQueueThread), "IOMMU-QI-%u", iInstance);
2416 rc = PDMDevHlpThreadCreate(pDevIns, &pThisR3->pInvQueueThread, pThis, dmarR3InvQueueThread, dmarR3InvQueueThreadWakeUp,
2417 0 /* cbStack */, RTTHREADTYPE_IO, szInvQueueThread);
2418 AssertLogRelRCReturn(rc, rc);
2419
2420 rc = PDMDevHlpSUPSemEventCreate(pDevIns, &pThis->hEvtInvQueue);
2421 AssertLogRelRCReturn(rc, rc);
2422
2423 /*
2424 * Log some of the features exposed to software.
2425 */
2426 uint32_t const uVerReg = pThis->uVerReg;
2427 uint8_t const cMaxGstAddrBits = RT_BF_GET(pThis->fCapReg, VTD_BF_CAP_REG_MGAW) + 1;
2428 uint8_t const cSupGstAddrBits = vtdCapRegGetSagawBits(RT_BF_GET(pThis->fCapReg, VTD_BF_CAP_REG_SAGAW));
2429 uint16_t const offFrcd = RT_BF_GET(pThis->fCapReg, VTD_BF_CAP_REG_FRO);
2430 uint16_t const offIva = RT_BF_GET(pThis->fExtCapReg, VTD_BF_ECAP_REG_IRO);
2431 LogRel(("%s: VER=%u.%u CAP=%#RX64 ECAP=%#RX64 (MGAW=%u bits, SAGAW=%u bits, FRO=%#x, IRO=%#x) mapped at %#RGp\n",
2432 DMAR_LOG_PFX, RT_BF_GET(uVerReg, VTD_BF_VER_REG_MAX), RT_BF_GET(uVerReg, VTD_BF_VER_REG_MIN),
2433 pThis->fCapReg, pThis->fExtCapReg, cMaxGstAddrBits, cSupGstAddrBits, offFrcd, offIva, DMAR_MMIO_BASE_PHYSADDR));
2434
2435 return VINF_SUCCESS;
2436}
2437
2438#else
2439
2440/**
2441 * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
2442 */
2443static DECLCALLBACK(int) iommuIntelRZConstruct(PPDMDEVINS pDevIns)
2444{
2445 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
2446 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
2447 PDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDMARCC);
2448 pThisCC->CTX_SUFF(pDevIns) = pDevIns;
2449
2450 /* We will use PDM's critical section (via helpers) for the IOMMU device. */
2451 int rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
2452 AssertRCReturn(rc, rc);
2453
2454 /* Set up the MMIO RZ handlers. */
2455 rc = PDMDevHlpMmioSetUpContext(pDevIns, pThis->hMmio, dmarMmioWrite, dmarMmioRead, NULL /* pvUser */);
2456 AssertRCReturn(rc, rc);
2457
2458 /* Set up the IOMMU RZ callbacks. */
2459 PDMIOMMUREGCC IommuReg;
2460 RT_ZERO(IommuReg);
2461 IommuReg.u32Version = PDM_IOMMUREGCC_VERSION;
2462 IommuReg.idxIommu = pThis->idxIommu;
2463 IommuReg.pfnMemAccess = iommuIntelMemAccess;
2464 IommuReg.pfnMemBulkAccess = iommuIntelMemBulkAccess;
2465 IommuReg.pfnMsiRemap = iommuIntelMsiRemap;
2466 IommuReg.u32TheEnd = PDM_IOMMUREGCC_VERSION;
2467
2468 rc = PDMDevHlpIommuSetUpContext(pDevIns, &IommuReg, &pThisCC->CTX_SUFF(pIommuHlp));
2469 AssertRCReturn(rc, rc);
2470 AssertPtrReturn(pThisCC->CTX_SUFF(pIommuHlp), VERR_IOMMU_IPE_1);
2471 AssertReturn(pThisCC->CTX_SUFF(pIommuHlp)->u32Version == CTX_MID(PDM_IOMMUHLP,_VERSION), VERR_VERSION_MISMATCH);
2472 AssertReturn(pThisCC->CTX_SUFF(pIommuHlp)->u32TheEnd == CTX_MID(PDM_IOMMUHLP,_VERSION), VERR_VERSION_MISMATCH);
2473 AssertPtrReturn(pThisCC->CTX_SUFF(pIommuHlp)->pfnLock, VERR_INVALID_POINTER);
2474 AssertPtrReturn(pThisCC->CTX_SUFF(pIommuHlp)->pfnUnlock, VERR_INVALID_POINTER);
2475 AssertPtrReturn(pThisCC->CTX_SUFF(pIommuHlp)->pfnLockIsOwner, VERR_INVALID_POINTER);
2476 AssertPtrReturn(pThisCC->CTX_SUFF(pIommuHlp)->pfnSendMsi, VERR_INVALID_POINTER);
2477
2478 return VINF_SUCCESS;
2479}
2480
2481#endif
2482
2483
2484/**
2485 * The device registration structure.
2486 */
2487PDMDEVREG const g_DeviceIommuIntel =
2488{
2489 /* .u32Version = */ PDM_DEVREG_VERSION,
2490 /* .uReserved0 = */ 0,
2491 /* .szName = */ "iommu-intel",
2492 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE,
2493 /* .fClass = */ PDM_DEVREG_CLASS_PCI_BUILTIN,
2494 /* .cMaxInstances = */ 1,
2495 /* .uSharedVersion = */ 42,
2496 /* .cbInstanceShared = */ sizeof(DMAR),
2497 /* .cbInstanceCC = */ sizeof(DMARCC),
2498 /* .cbInstanceRC = */ sizeof(DMARRC),
2499 /* .cMaxPciDevices = */ 1,
2500 /* .cMaxMsixVectors = */ 0,
2501 /* .pszDescription = */ "IOMMU (Intel)",
2502#if defined(IN_RING3)
2503 /* .pszRCMod = */ "VBoxDDRC.rc",
2504 /* .pszR0Mod = */ "VBoxDDR0.r0",
2505 /* .pfnConstruct = */ iommuIntelR3Construct,
2506 /* .pfnDestruct = */ iommuIntelR3Destruct,
2507 /* .pfnRelocate = */ NULL,
2508 /* .pfnMemSetup = */ NULL,
2509 /* .pfnPowerOn = */ NULL,
2510 /* .pfnReset = */ iommuIntelR3Reset,
2511 /* .pfnSuspend = */ NULL,
2512 /* .pfnResume = */ NULL,
2513 /* .pfnAttach = */ NULL,
2514 /* .pfnDetach = */ NULL,
2515 /* .pfnQueryInterface = */ NULL,
2516 /* .pfnInitComplete = */ NULL,
2517 /* .pfnPowerOff = */ NULL,
2518 /* .pfnSoftReset = */ NULL,
2519 /* .pfnReserved0 = */ NULL,
2520 /* .pfnReserved1 = */ NULL,
2521 /* .pfnReserved2 = */ NULL,
2522 /* .pfnReserved3 = */ NULL,
2523 /* .pfnReserved4 = */ NULL,
2524 /* .pfnReserved5 = */ NULL,
2525 /* .pfnReserved6 = */ NULL,
2526 /* .pfnReserved7 = */ NULL,
2527#elif defined(IN_RING0)
2528 /* .pfnEarlyConstruct = */ NULL,
2529 /* .pfnConstruct = */ iommuIntelRZConstruct,
2530 /* .pfnDestruct = */ NULL,
2531 /* .pfnFinalDestruct = */ NULL,
2532 /* .pfnRequest = */ NULL,
2533 /* .pfnReserved0 = */ NULL,
2534 /* .pfnReserved1 = */ NULL,
2535 /* .pfnReserved2 = */ NULL,
2536 /* .pfnReserved3 = */ NULL,
2537 /* .pfnReserved4 = */ NULL,
2538 /* .pfnReserved5 = */ NULL,
2539 /* .pfnReserved6 = */ NULL,
2540 /* .pfnReserved7 = */ NULL,
2541#elif defined(IN_RC)
2542 /* .pfnConstruct = */ iommuIntelRZConstruct,
2543 /* .pfnReserved0 = */ NULL,
2544 /* .pfnReserved1 = */ NULL,
2545 /* .pfnReserved2 = */ NULL,
2546 /* .pfnReserved3 = */ NULL,
2547 /* .pfnReserved4 = */ NULL,
2548 /* .pfnReserved5 = */ NULL,
2549 /* .pfnReserved6 = */ NULL,
2550 /* .pfnReserved7 = */ NULL,
2551#else
2552# error "Not in IN_RING3, IN_RING0 or IN_RC!"
2553#endif
2554 /* .u32VersionEnd = */ PDM_DEVREG_VERSION
2555};
2556
2557#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
2558
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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