VirtualBox

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

Last change on this file since 89453 was 89453, checked in by vboxsync, 3 years ago

Intel IOMMU: bugref:9967 typo.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 177.0 KB
Line 
1/* $Id: DevIommuIntel.cpp 89453 2021-06-02 07:49:24Z 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.virtualbox.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/** Gets the page offset mask given the number of bits to shift. */
58#define DMAR_GET_PAGE_OFF_MASK(a_cShift) (~(UINT64_C(0xffffffffffffffff) << (a_cShift)))
59
60/** Acquires the DMAR lock but returns with the given busy error code on failure. */
61#define DMAR_LOCK_RET(a_pDevIns, a_pThisCC, a_rcBusy) \
62 do { \
63 if ((a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnLock((a_pDevIns), (a_rcBusy)) == VINF_SUCCESS) \
64 { /* likely */ } \
65 else \
66 return (a_rcBusy); \
67 } while (0)
68
69/** Acquires the DMAR lock (not expected to fail). */
70#ifdef IN_RING3
71# define DMAR_LOCK(a_pDevIns, a_pThisCC) (a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnLock((a_pDevIns), VERR_IGNORED)
72#else
73# define DMAR_LOCK(a_pDevIns, a_pThisCC) \
74 do { \
75 int const rcLock = (a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnLock((a_pDevIns), VINF_SUCCESS); \
76 AssertRC(rcLock); \
77 } while (0)
78#endif
79
80/** Release the DMAR lock. */
81#define DMAR_UNLOCK(a_pDevIns, a_pThisCC) (a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnUnlock(a_pDevIns)
82
83/** Asserts that the calling thread owns the DMAR lock. */
84#define DMAR_ASSERT_LOCK_IS_OWNER(a_pDevIns, a_pThisCC) \
85 do { \
86 Assert((a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnLockIsOwner(a_pDevIns)); \
87 RT_NOREF1(a_pThisCC); \
88 } while (0)
89
90/** Asserts that the calling thread does not own the DMAR lock. */
91#define DMAR_ASSERT_LOCK_IS_NOT_OWNER(a_pDevIns, a_pThisCC) \
92 do { \
93 Assert((a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnLockIsOwner(a_pDevIns) == false); \
94 RT_NOREF1(a_pThisCC); \
95 } while (0)
96
97/** The number of fault recording registers our implementation supports.
98 * Normal guest operation shouldn't trigger faults anyway, so we only support the
99 * minimum number of registers (which is 1).
100 *
101 * See Intel VT-d spec. 10.4.2 "Capability Register" (CAP_REG.NFR). */
102#define DMAR_FRCD_REG_COUNT UINT32_C(1)
103
104/** Offset of first register in group 0. */
105#define DMAR_MMIO_GROUP_0_OFF_FIRST VTD_MMIO_OFF_VER_REG
106/** Offset of last register in group 0 (inclusive). */
107#define DMAR_MMIO_GROUP_0_OFF_LAST VTD_MMIO_OFF_MTRR_PHYSMASK9_REG
108/** Last valid offset in group 0 (exclusive). */
109#define DMAR_MMIO_GROUP_0_OFF_END (DMAR_MMIO_GROUP_0_OFF_LAST + 8 /* sizeof MTRR_PHYSMASK9_REG */)
110/** Size of the group 0 (in bytes). */
111#define DMAR_MMIO_GROUP_0_SIZE (DMAR_MMIO_GROUP_0_OFF_END - DMAR_MMIO_GROUP_0_OFF_FIRST)
112/**< Implementation-specific MMIO offset of IVA_REG. */
113#define DMAR_MMIO_OFF_IVA_REG 0xe50
114/**< Implementation-specific MMIO offset of IOTLB_REG. */
115#define DMAR_MMIO_OFF_IOTLB_REG 0xe58
116/**< Implementation-specific MMIO offset of FRCD_LO_REG. */
117#define DMAR_MMIO_OFF_FRCD_LO_REG 0xe70
118/**< Implementation-specific MMIO offset of FRCD_HI_REG. */
119#define DMAR_MMIO_OFF_FRCD_HI_REG 0xe78
120AssertCompile(!(DMAR_MMIO_OFF_FRCD_LO_REG & 0xf));
121
122/** Offset of first register in group 1. */
123#define DMAR_MMIO_GROUP_1_OFF_FIRST VTD_MMIO_OFF_VCCAP_REG
124/** Offset of last register in group 1 (inclusive). */
125#define DMAR_MMIO_GROUP_1_OFF_LAST (DMAR_MMIO_OFF_FRCD_LO_REG + 8) * DMAR_FRCD_REG_COUNT
126/** Last valid offset in group 1 (exclusive). */
127#define DMAR_MMIO_GROUP_1_OFF_END (DMAR_MMIO_GROUP_1_OFF_LAST + 8 /* sizeof FRCD_HI_REG */)
128/** Size of the group 1 (in bytes). */
129#define DMAR_MMIO_GROUP_1_SIZE (DMAR_MMIO_GROUP_1_OFF_END - DMAR_MMIO_GROUP_1_OFF_FIRST)
130
131/** DMAR implementation's major version number (exposed to software).
132 * We report 6 as the major version since we support queued-invalidations as
133 * software may make assumptions based on that.
134 *
135 * See Intel VT-d spec. 10.4.7 "Context Command Register" (CCMD_REG.CAIG). */
136#define DMAR_VER_MAJOR 6
137/** DMAR implementation's minor version number (exposed to software). */
138#define DMAR_VER_MINOR 0
139
140/** Number of domain supported (0=16, 1=64, 2=256, 3=1K, 4=4K, 5=16K, 6=64K,
141 * 7=Reserved). */
142#define DMAR_ND 6
143
144/** @name DMAR_PERM_XXX: DMA request permissions.
145 * The order of R, W, X bits is important as it corresponds to those bits in
146 * page-table entries.
147 *
148 * @{ */
149/** DMA request permission: Read. */
150#define DMAR_PERM_READ RT_BIT(0)
151/** DMA request permission: Write. */
152#define DMAR_PERM_WRITE RT_BIT(1)
153/** DMA request permission: Execute (ER). */
154#define DMAR_PERM_EXE RT_BIT(2)
155/** DMA request permission: Supervisor privilege (PR). */
156#define DMAR_PERM_PRIV RT_BIT(3)
157/** DMA request permissions: All. */
158#define DMAR_PERM_ALL (DMAR_PERM_READ | DMAR_PERM_WRITE | DMAR_PERM_EXE | DMAR_PERM_PRIV)
159/** @} */
160
161/** Release log prefix string. */
162#define DMAR_LOG_PFX "Intel-IOMMU"
163/** The current saved state version. */
164#define DMAR_SAVED_STATE_VERSION 1
165
166
167/*********************************************************************************************************************************
168* Structures and Typedefs *
169*********************************************************************************************************************************/
170/**
171 * DMAR error diagnostics.
172 * Sorted alphabetically so it's easier to add and locate items, no other reason.
173 *
174 * @note Members of this enum are used as array indices, so no gaps in enum
175 * values are not allowed. Update g_apszDmarDiagDesc when you modify
176 * fields in this enum.
177 */
178typedef enum
179{
180 /* No error, this must be zero! */
181 kDmarDiag_None = 0,
182
183 /* Address Translation Faults. */
184 kDmarDiag_Atf_Lct_1,
185 kDmarDiag_Atf_Lct_2,
186 kDmarDiag_Atf_Lct_3,
187 kDmarDiag_Atf_Lct_4_1,
188 kDmarDiag_Atf_Lct_4_2,
189 kDmarDiag_Atf_Lct_4_3,
190 kDmarDiag_Atf_Lct_5,
191 kDmarDiag_Atf_Lgn_1_1,
192 kDmarDiag_Atf_Lgn_1_2,
193 kDmarDiag_Atf_Lgn_1_3,
194 kDmarDiag_Atf_Lgn_4,
195 kDmarDiag_Atf_Lrt_1,
196 kDmarDiag_Atf_Lrt_2,
197 kDmarDiag_Atf_Lrt_3,
198 kDmarDiag_Atf_Lsl_1,
199 kDmarDiag_Atf_Lsl_2,
200 kDmarDiag_Atf_Lsl_2_LargePage,
201 kDmarDiag_Atf_Rta_1_1,
202 kDmarDiag_Atf_Rta_1_2,
203 kDmarDiag_Atf_Rta_1_3,
204 kDmarDiag_Atf_Sgn_5,
205 kDmarDiag_Atf_Sgn_8,
206 kDmarDiag_Atf_Ssl_1,
207 kDmarDiag_Atf_Ssl_2,
208 kDmarDiag_Atf_Ssl_3,
209 kDmarDiag_Atf_Ssl_3_LargePage,
210
211 /* CCMD_REG faults. */
212 kDmarDiag_CcmdReg_NotSupported,
213 kDmarDiag_CcmdReg_Qi_Enabled,
214 kDmarDiag_CcmdReg_Ttm_Invalid,
215
216 /* IQA_REG faults. */
217 kDmarDiag_IqaReg_Dsc_Fetch_Error,
218 kDmarDiag_IqaReg_Dw_128_Invalid,
219 kDmarDiag_IqaReg_Dw_256_Invalid,
220
221 /* Invalidation Queue Error Info. */
222 kDmarDiag_Iqei_Dsc_Type_Invalid,
223 kDmarDiag_Iqei_Inv_Wait_Dsc_0_1_Rsvd,
224 kDmarDiag_Iqei_Inv_Wait_Dsc_2_3_Rsvd,
225 kDmarDiag_Iqei_Inv_Wait_Dsc_Invalid,
226 kDmarDiag_Iqei_Ttm_Rsvd,
227
228 /* IQT_REG faults. */
229 kDmarDiag_IqtReg_Qt_Invalid,
230 kDmarDiag_IqtReg_Qt_NotAligned,
231
232 /* Compatibility Format Interrupt Faults. */
233 kDmarDiag_Ir_Cfi_Blocked,
234
235 /* Remappable Format Interrupt Faults. */
236 kDmarDiag_Ir_Rfi_Intr_Index_Invalid,
237 kDmarDiag_Ir_Rfi_Irte_Mode_Invalid,
238 kDmarDiag_Ir_Rfi_Irte_Not_Present,
239 kDmarDiag_Ir_Rfi_Irte_Read_Failed,
240 kDmarDiag_Ir_Rfi_Irte_Rsvd,
241 kDmarDiag_Ir_Rfi_Irte_Svt_Bus,
242 kDmarDiag_Ir_Rfi_Irte_Svt_Masked,
243 kDmarDiag_Ir_Rfi_Irte_Svt_Rsvd,
244 kDmarDiag_Ir_Rfi_Rsvd,
245
246 /* Member for determining array index limit. */
247 kDmarDiag_End,
248
249 /* Usual 32-bit type size hack. */
250 kDmarDiag_32Bit_Hack = 0x7fffffff
251} DMARDIAG;
252AssertCompileSize(DMARDIAG, 4);
253
254/** DMAR diagnostic enum description expansion.
255 * The below construct ensures typos in the input to this macro are caught
256 * during compile time. */
257#define DMARDIAG_DESC(a_Name) RT_CONCAT(kDmarDiag_, a_Name) < kDmarDiag_End ? RT_STR(a_Name) : "Ignored"
258
259/** DMAR diagnostics description for members in DMARDIAG. */
260static const char *const g_apszDmarDiagDesc[] =
261{
262 DMARDIAG_DESC(None ),
263 DMARDIAG_DESC(Atf_Lct_1 ),
264 DMARDIAG_DESC(Atf_Lct_2 ),
265 DMARDIAG_DESC(Atf_Lct_3 ),
266 DMARDIAG_DESC(Atf_Lct_4_1 ),
267 DMARDIAG_DESC(Atf_Lct_4_2 ),
268 DMARDIAG_DESC(Atf_Lct_4_3 ),
269 DMARDIAG_DESC(Atf_Lct_5 ),
270 DMARDIAG_DESC(Atf_Lgn_1_1 ),
271 DMARDIAG_DESC(Atf_Lgn_1_2 ),
272 DMARDIAG_DESC(Atf_Lgn_1_3 ),
273 DMARDIAG_DESC(Atf_Lgn_4 ),
274 DMARDIAG_DESC(Atf_Lrt_1 ),
275 DMARDIAG_DESC(Atf_Lrt_2 ),
276 DMARDIAG_DESC(Atf_Lrt_3 ),
277 DMARDIAG_DESC(Atf_Lsl_1 ),
278 DMARDIAG_DESC(Atf_Lsl_2 ),
279 DMARDIAG_DESC(Atf_Lsl_2_LargePage ),
280 DMARDIAG_DESC(Atf_Rta_1_1 ),
281 DMARDIAG_DESC(Atf_Rta_1_2 ),
282 DMARDIAG_DESC(Atf_Rta_1_3 ),
283 DMARDIAG_DESC(Atf_Sgn_5 ),
284 DMARDIAG_DESC(Atf_Sgn_8 ),
285 DMARDIAG_DESC(Atf_Ssl_1 ),
286 DMARDIAG_DESC(Atf_Ssl_2 ),
287 DMARDIAG_DESC(Atf_Ssl_3 ),
288 DMARDIAG_DESC(Atf_Ssl_3_LargePage ),
289 DMARDIAG_DESC(CcmdReg_NotSupported ),
290 DMARDIAG_DESC(CcmdReg_Qi_Enabled ),
291 DMARDIAG_DESC(CcmdReg_Ttm_Invalid ),
292 DMARDIAG_DESC(IqaReg_Dsc_Fetch_Error ),
293 DMARDIAG_DESC(IqaReg_Dw_128_Invalid ),
294 DMARDIAG_DESC(IqaReg_Dw_256_Invalid ),
295 DMARDIAG_DESC(Iqei_Dsc_Type_Invalid ),
296 DMARDIAG_DESC(Iqei_Inv_Wait_Dsc_0_1_Rsvd),
297 DMARDIAG_DESC(Iqei_Inv_Wait_Dsc_2_3_Rsvd),
298 DMARDIAG_DESC(Iqei_Inv_Wait_Dsc_Invalid ),
299 DMARDIAG_DESC(Iqei_Ttm_Rsvd ),
300 DMARDIAG_DESC(IqtReg_Qt_Invalid ),
301 DMARDIAG_DESC(IqtReg_Qt_NotAligned ),
302 DMARDIAG_DESC(Ir_Cfi_Blocked ),
303 DMARDIAG_DESC(Ir_Rfi_Intr_Index_Invalid ),
304 DMARDIAG_DESC(Ir_Rfi_Irte_Mode_Invalid ),
305 DMARDIAG_DESC(Ir_Rfi_Irte_Not_Present ),
306 DMARDIAG_DESC(Ir_Rfi_Irte_Read_Failed ),
307 DMARDIAG_DESC(Ir_Rfi_Irte_Rsvd ),
308 DMARDIAG_DESC(Ir_Rfi_Irte_Svt_Bus ),
309 DMARDIAG_DESC(Ir_Rfi_Irte_Svt_Masked ),
310 DMARDIAG_DESC(Ir_Rfi_Irte_Svt_Rsvd ),
311 DMARDIAG_DESC(Ir_Rfi_Rsvd ),
312 /* kDmarDiag_End */
313};
314AssertCompile(RT_ELEMENTS(g_apszDmarDiagDesc) == kDmarDiag_End);
315#undef DMARDIAG_DESC
316
317/**
318 * The shared DMAR device state.
319 */
320typedef struct DMAR
321{
322 /** IOMMU device index. */
323 uint32_t idxIommu;
324 /** DMAR magic. */
325 uint32_t u32Magic;
326
327 /** Registers (group 0). */
328 uint8_t abRegs0[DMAR_MMIO_GROUP_0_SIZE];
329 /** Registers (group 1). */
330 uint8_t abRegs1[DMAR_MMIO_GROUP_1_SIZE];
331
332 /** @name Lazily activated registers.
333 * These are the active values for lazily activated registers. Software is free to
334 * modify the actual register values while remapping/translation is enabled but they
335 * take effect only when explicitly signaled by software, hence we need to hold the
336 * active values separately.
337 * @{ */
338 /** Currently active IRTA_REG. */
339 uint64_t uIrtaReg;
340 /** Currently active RTADDR_REG. */
341 uint64_t uRtaddrReg;
342 /** @} */
343
344 /** @name Register copies for a tiny bit faster and more convenient access.
345 * @{ */
346 /** Copy of VER_REG. */
347 uint8_t uVerReg;
348 /** Alignment. */
349 uint8_t abPadding[7];
350 /** Copy of CAP_REG. */
351 uint64_t fCapReg;
352 /** Copy of ECAP_REG. */
353 uint64_t fExtCapReg;
354 /** @} */
355
356 /** Host-address width (HAW) base address mask. */
357 uint64_t fHawBaseMask;
358 /** Maximum guest-address width (MGAW) invalid address mask. */
359 uint64_t fInvMgawMask;
360 /** Maximum supported paging level (3, 4 or 5). */
361 uint8_t cMaxPagingLevel;
362 /** DMA request valid permissions mask. */
363 uint8_t fPermValidMask;
364
365 /** The event semaphore the invalidation-queue thread waits on. */
366 SUPSEMEVENT hEvtInvQueue;
367 /** Padding. */
368 uint32_t uPadding0;
369 /** Error diagnostic. */
370 DMARDIAG enmDiag;
371 /** The MMIO handle. */
372 IOMMMIOHANDLE hMmio;
373
374#ifdef VBOX_WITH_STATISTICS
375 STAMCOUNTER StatMmioReadR3; /**< Number of MMIO reads in R3. */
376 STAMCOUNTER StatMmioReadRZ; /**< Number of MMIO reads in RZ. */
377 STAMCOUNTER StatMmioWriteR3; /**< Number of MMIO writes in R3. */
378 STAMCOUNTER StatMmioWriteRZ; /**< Number of MMIO writes in RZ. */
379
380 STAMCOUNTER StatMsiRemapCfiR3; /**< Number of compatibility-format interrupts remap requests in R3. */
381 STAMCOUNTER StatMsiRemapCfiRZ; /**< Number of compatibility-format interrupts remap requests in RZ. */
382 STAMCOUNTER StatMsiRemapRfiR3; /**< Number of remappable-format interrupts remap requests in R3. */
383 STAMCOUNTER StatMsiRemapRfiRZ; /**< Number of remappable-format interrupts remap requests in RZ. */
384
385 STAMCOUNTER StatMemReadR3; /**< Number of memory read translation requests in R3. */
386 STAMCOUNTER StatMemReadRZ; /**< Number of memory read translation requests in RZ. */
387 STAMCOUNTER StatMemWriteR3; /**< Number of memory write translation requests in R3. */
388 STAMCOUNTER StatMemWriteRZ; /**< Number of memory write translation requests in RZ. */
389
390 STAMCOUNTER StatMemBulkReadR3; /**< Number of memory read bulk translation requests in R3. */
391 STAMCOUNTER StatMemBulkReadRZ; /**< Number of memory read bulk translation requests in RZ. */
392 STAMCOUNTER StatMemBulkWriteR3; /**< Number of memory write bulk translation requests in R3. */
393 STAMCOUNTER StatMemBulkWriteRZ; /**< Number of memory write bulk translation requests in RZ. */
394
395 STAMCOUNTER StatCcInvDsc; /**< Number of Context-cache descriptors processed. */
396 STAMCOUNTER StatIotlbInvDsc; /**< Number of IOTLB descriptors processed. */
397 STAMCOUNTER StatDevtlbInvDsc; /**< Number of Device-TLB descriptors processed. */
398 STAMCOUNTER StatIecInvDsc; /**< Number of Interrupt-Entry cache descriptors processed. */
399 STAMCOUNTER StatInvWaitDsc; /**< Number of Invalidation wait descriptors processed. */
400 STAMCOUNTER StatPasidIotlbInvDsc; /**< Number of PASID-based IOTLB descriptors processed. */
401 STAMCOUNTER StatPasidCacheInvDsc; /**< Number of PASID-cache descriptors processed. */
402 STAMCOUNTER StatPasidDevtlbInvDsc; /**< Number of PASID-based device-TLB descriptors processed. */
403#endif
404} DMAR;
405/** Pointer to the DMAR device state. */
406typedef DMAR *PDMAR;
407/** Pointer to the const DMAR device state. */
408typedef DMAR const *PCDMAR;
409AssertCompileMemberAlignment(DMAR, abRegs0, 8);
410AssertCompileMemberAlignment(DMAR, abRegs1, 8);
411
412/**
413 * The ring-3 DMAR device state.
414 */
415typedef struct DMARR3
416{
417 /** Device instance. */
418 PPDMDEVINSR3 pDevInsR3;
419 /** The IOMMU helper. */
420 R3PTRTYPE(PCPDMIOMMUHLPR3) pIommuHlpR3;
421 /** The invalidation-queue thread. */
422 R3PTRTYPE(PPDMTHREAD) pInvQueueThread;
423} DMARR3;
424/** Pointer to the ring-3 DMAR device state. */
425typedef DMARR3 *PDMARR3;
426/** Pointer to the const ring-3 DMAR device state. */
427typedef DMARR3 const *PCDMARR3;
428
429/**
430 * The ring-0 DMAR device state.
431 */
432typedef struct DMARR0
433{
434 /** Device instance. */
435 PPDMDEVINSR0 pDevInsR0;
436 /** The IOMMU helper. */
437 R0PTRTYPE(PCPDMIOMMUHLPR0) pIommuHlpR0;
438} DMARR0;
439/** Pointer to the ring-0 IOMMU device state. */
440typedef DMARR0 *PDMARR0;
441/** Pointer to the const ring-0 IOMMU device state. */
442typedef DMARR0 const *PCDMARR0;
443
444/**
445 * The raw-mode DMAR device state.
446 */
447typedef struct DMARRC
448{
449 /** Device instance. */
450 PPDMDEVINSRC pDevInsRC;
451 /** The IOMMU helper. */
452 RCPTRTYPE(PCPDMIOMMUHLPRC) pIommuHlpRC;
453} DMARRC;
454/** Pointer to the raw-mode DMAR device state. */
455typedef DMARRC *PDMARRC;
456/** Pointer to the const raw-mode DMAR device state. */
457typedef DMARRC const *PCIDMARRC;
458
459/** The DMAR device state for the current context. */
460typedef CTX_SUFF(DMAR) DMARCC;
461/** Pointer to the DMAR device state for the current context. */
462typedef CTX_SUFF(PDMAR) PDMARCC;
463/** Pointer to the const DMAR device state for the current context. */
464typedef CTX_SUFF(PDMAR) const PCDMARCC;
465
466/**
467 * DMAR originated events that generate interrupts.
468 */
469typedef enum DMAREVENTTYPE
470{
471 /** Invalidation completion event. */
472 DMAREVENTTYPE_INV_COMPLETE = 0,
473 /** Fault event. */
474 DMAREVENTTYPE_FAULT
475} DMAREVENTTYPE;
476
477/**
478 * I/O TLB entry.
479 */
480typedef struct DMARIOTLBE
481{
482 /** The system-physical base address of the translation. */
483 RTGCPHYS GCPhysBase;
484 /** The domain ID of the translated region. */
485 uint16_t idDomain;
486 /** The permissions granted. */
487 uint8_t fPerm;
488 /** The page shift of the translation. */
489 uint8_t cShift;
490} DMARIOTLBE;
491/** Pointer to an IOTLB entry. */
492typedef DMARIOTLBE *PDMARIOTLBE;
493/** Pointer to a const IOTLB entry. */
494typedef DMARIOTLBE const *PCDMARIOTLBE;
495AssertCompileSizeAlignment(DMARIOTLBE, 4);
496
497/**
498 * DMA Memory Request.
499 */
500typedef struct DMARMEMREQ
501{
502 /** The address being accessed. */
503 uint64_t uDmaAddr;
504 /** The size of the access (in bytes). */
505 size_t cbDma;
506 /** The requested permissions (DMAR_PERM_XXX). */
507 uint8_t fReqPerm;
508 /** The source device ID (bus, device, function). */
509 uint16_t idDevice;
510 /** The PASID if present (can be NIL_PCIPASID). */
511 PCIPASID Pasid;
512 /* The address translation type. */
513 PCIADDRTYPE enmAddrType;
514 /** The request type. */
515 VTDREQTYPE enmReqType;
516} DMARMEMREQ;
517/** Pointer to a DMA memory request. */
518typedef DMARMEMREQ *PDMARMEMREQ;
519/** Pointer to a const DMA memory request. */
520typedef DMARMEMREQ const *PCDMARMEMREQ;
521
522/**
523 * DMA Memory Request Remapping Information.
524 */
525typedef struct DMARMEMREQREMAP
526{
527 /* The DMA memory request. */
528 DMARMEMREQ Req;
529 /** The table translation mode (VTD_TTM_XXX). */
530 uint8_t fTtm;
531 /** The fault processing disabled (FPD) bit. */
532 uint8_t fFpd;
533
534 /** The IOTLBE result for this remapping. */
535 DMARIOTLBE Iotlbe;
536 /** The size of the contiguous translated region (in bytes). */
537 size_t cbContiguous;
538} DMARMEMREQREMAP;
539/** Pointer to a DMA remap info. */
540typedef DMARMEMREQREMAP *PDMARMEMREQREMAP;
541/** Pointer to a const DMA remap info. */
542typedef DMARMEMREQREMAP const *PCDMARMEMREQREMAP;
543
544
545/*********************************************************************************************************************************
546* Global Variables *
547*********************************************************************************************************************************/
548/**
549 * Read-write masks for DMAR registers (group 0).
550 */
551static uint32_t const g_au32RwMasks0[] =
552{
553 /* Offset Register Low High */
554 /* 0x000 VER_REG */ VTD_VER_REG_RW_MASK,
555 /* 0x004 Reserved */ 0,
556 /* 0x008 CAP_REG */ DMAR_LO_U32(VTD_CAP_REG_RW_MASK), DMAR_HI_U32(VTD_CAP_REG_RW_MASK),
557 /* 0x010 ECAP_REG */ DMAR_LO_U32(VTD_ECAP_REG_RW_MASK), DMAR_HI_U32(VTD_ECAP_REG_RW_MASK),
558 /* 0x018 GCMD_REG */ VTD_GCMD_REG_RW_MASK,
559 /* 0x01c GSTS_REG */ VTD_GSTS_REG_RW_MASK,
560 /* 0x020 RTADDR_REG */ DMAR_LO_U32(VTD_RTADDR_REG_RW_MASK), DMAR_HI_U32(VTD_RTADDR_REG_RW_MASK),
561 /* 0x028 CCMD_REG */ DMAR_LO_U32(VTD_CCMD_REG_RW_MASK), DMAR_HI_U32(VTD_CCMD_REG_RW_MASK),
562 /* 0x030 Reserved */ 0,
563 /* 0x034 FSTS_REG */ VTD_FSTS_REG_RW_MASK,
564 /* 0x038 FECTL_REG */ VTD_FECTL_REG_RW_MASK,
565 /* 0x03c FEDATA_REG */ VTD_FEDATA_REG_RW_MASK,
566 /* 0x040 FEADDR_REG */ VTD_FEADDR_REG_RW_MASK,
567 /* 0x044 FEUADDR_REG */ VTD_FEUADDR_REG_RW_MASK,
568 /* 0x048 Reserved */ 0, 0,
569 /* 0x050 Reserved */ 0, 0,
570 /* 0x058 AFLOG_REG */ DMAR_LO_U32(VTD_AFLOG_REG_RW_MASK), DMAR_HI_U32(VTD_AFLOG_REG_RW_MASK),
571 /* 0x060 Reserved */ 0,
572 /* 0x064 PMEN_REG */ 0, /* RO as we don't support PLMR and PHMR. */
573 /* 0x068 PLMBASE_REG */ 0, /* RO as we don't support PLMR. */
574 /* 0x06c PLMLIMIT_REG */ 0, /* RO as we don't support PLMR. */
575 /* 0x070 PHMBASE_REG */ 0, 0, /* RO as we don't support PHMR. */
576 /* 0x078 PHMLIMIT_REG */ 0, 0, /* RO as we don't support PHMR. */
577 /* 0x080 IQH_REG */ DMAR_LO_U32(VTD_IQH_REG_RW_MASK), DMAR_HI_U32(VTD_IQH_REG_RW_MASK),
578 /* 0x088 IQT_REG */ DMAR_LO_U32(VTD_IQT_REG_RW_MASK), DMAR_HI_U32(VTD_IQT_REG_RW_MASK),
579 /* 0x090 IQA_REG */ DMAR_LO_U32(VTD_IQA_REG_RW_MASK), DMAR_HI_U32(VTD_IQA_REG_RW_MASK),
580 /* 0x098 Reserved */ 0,
581 /* 0x09c ICS_REG */ VTD_ICS_REG_RW_MASK,
582 /* 0x0a0 IECTL_REG */ VTD_IECTL_REG_RW_MASK,
583 /* 0x0a4 IEDATA_REG */ VTD_IEDATA_REG_RW_MASK,
584 /* 0x0a8 IEADDR_REG */ VTD_IEADDR_REG_RW_MASK,
585 /* 0x0ac IEUADDR_REG */ VTD_IEUADDR_REG_RW_MASK,
586 /* 0x0b0 IQERCD_REG */ DMAR_LO_U32(VTD_IQERCD_REG_RW_MASK), DMAR_HI_U32(VTD_IQERCD_REG_RW_MASK),
587 /* 0x0b8 IRTA_REG */ DMAR_LO_U32(VTD_IRTA_REG_RW_MASK), DMAR_HI_U32(VTD_IRTA_REG_RW_MASK),
588 /* 0x0c0 PQH_REG */ DMAR_LO_U32(VTD_PQH_REG_RW_MASK), DMAR_HI_U32(VTD_PQH_REG_RW_MASK),
589 /* 0x0c8 PQT_REG */ DMAR_LO_U32(VTD_PQT_REG_RW_MASK), DMAR_HI_U32(VTD_PQT_REG_RW_MASK),
590 /* 0x0d0 PQA_REG */ DMAR_LO_U32(VTD_PQA_REG_RW_MASK), DMAR_HI_U32(VTD_PQA_REG_RW_MASK),
591 /* 0x0d8 Reserved */ 0,
592 /* 0x0dc PRS_REG */ VTD_PRS_REG_RW_MASK,
593 /* 0x0e0 PECTL_REG */ VTD_PECTL_REG_RW_MASK,
594 /* 0x0e4 PEDATA_REG */ VTD_PEDATA_REG_RW_MASK,
595 /* 0x0e8 PEADDR_REG */ VTD_PEADDR_REG_RW_MASK,
596 /* 0x0ec PEUADDR_REG */ VTD_PEUADDR_REG_RW_MASK,
597 /* 0x0f0 Reserved */ 0, 0,
598 /* 0x0f8 Reserved */ 0, 0,
599 /* 0x100 MTRRCAP_REG */ DMAR_LO_U32(VTD_MTRRCAP_REG_RW_MASK), DMAR_HI_U32(VTD_MTRRCAP_REG_RW_MASK),
600 /* 0x108 MTRRDEF_REG */ 0, 0, /* RO as we don't support MTS. */
601 /* 0x110 Reserved */ 0, 0,
602 /* 0x118 Reserved */ 0, 0,
603 /* 0x120 MTRR_FIX64_00000_REG */ 0, 0, /* RO as we don't support MTS. */
604 /* 0x128 MTRR_FIX16K_80000_REG */ 0, 0,
605 /* 0x130 MTRR_FIX16K_A0000_REG */ 0, 0,
606 /* 0x138 MTRR_FIX4K_C0000_REG */ 0, 0,
607 /* 0x140 MTRR_FIX4K_C8000_REG */ 0, 0,
608 /* 0x148 MTRR_FIX4K_D0000_REG */ 0, 0,
609 /* 0x150 MTRR_FIX4K_D8000_REG */ 0, 0,
610 /* 0x158 MTRR_FIX4K_E0000_REG */ 0, 0,
611 /* 0x160 MTRR_FIX4K_E8000_REG */ 0, 0,
612 /* 0x168 MTRR_FIX4K_F0000_REG */ 0, 0,
613 /* 0x170 MTRR_FIX4K_F8000_REG */ 0, 0,
614 /* 0x178 Reserved */ 0, 0,
615 /* 0x180 MTRR_PHYSBASE0_REG */ 0, 0, /* RO as we don't support MTS. */
616 /* 0x188 MTRR_PHYSMASK0_REG */ 0, 0,
617 /* 0x190 MTRR_PHYSBASE1_REG */ 0, 0,
618 /* 0x198 MTRR_PHYSMASK1_REG */ 0, 0,
619 /* 0x1a0 MTRR_PHYSBASE2_REG */ 0, 0,
620 /* 0x1a8 MTRR_PHYSMASK2_REG */ 0, 0,
621 /* 0x1b0 MTRR_PHYSBASE3_REG */ 0, 0,
622 /* 0x1b8 MTRR_PHYSMASK3_REG */ 0, 0,
623 /* 0x1c0 MTRR_PHYSBASE4_REG */ 0, 0,
624 /* 0x1c8 MTRR_PHYSMASK4_REG */ 0, 0,
625 /* 0x1d0 MTRR_PHYSBASE5_REG */ 0, 0,
626 /* 0x1d8 MTRR_PHYSMASK5_REG */ 0, 0,
627 /* 0x1e0 MTRR_PHYSBASE6_REG */ 0, 0,
628 /* 0x1e8 MTRR_PHYSMASK6_REG */ 0, 0,
629 /* 0x1f0 MTRR_PHYSBASE7_REG */ 0, 0,
630 /* 0x1f8 MTRR_PHYSMASK7_REG */ 0, 0,
631 /* 0x200 MTRR_PHYSBASE8_REG */ 0, 0,
632 /* 0x208 MTRR_PHYSMASK8_REG */ 0, 0,
633 /* 0x210 MTRR_PHYSBASE9_REG */ 0, 0,
634 /* 0x218 MTRR_PHYSMASK9_REG */ 0, 0,
635};
636AssertCompile(sizeof(g_au32RwMasks0) == DMAR_MMIO_GROUP_0_SIZE);
637
638/**
639 * Read-only Status, Write-1-to-clear masks for DMAR registers (group 0).
640 */
641static uint32_t const g_au32Rw1cMasks0[] =
642{
643 /* Offset Register Low High */
644 /* 0x000 VER_REG */ 0,
645 /* 0x004 Reserved */ 0,
646 /* 0x008 CAP_REG */ 0, 0,
647 /* 0x010 ECAP_REG */ 0, 0,
648 /* 0x018 GCMD_REG */ 0,
649 /* 0x01c GSTS_REG */ 0,
650 /* 0x020 RTADDR_REG */ 0, 0,
651 /* 0x028 CCMD_REG */ 0, 0,
652 /* 0x030 Reserved */ 0,
653 /* 0x034 FSTS_REG */ VTD_FSTS_REG_RW1C_MASK,
654 /* 0x038 FECTL_REG */ 0,
655 /* 0x03c FEDATA_REG */ 0,
656 /* 0x040 FEADDR_REG */ 0,
657 /* 0x044 FEUADDR_REG */ 0,
658 /* 0x048 Reserved */ 0, 0,
659 /* 0x050 Reserved */ 0, 0,
660 /* 0x058 AFLOG_REG */ 0, 0,
661 /* 0x060 Reserved */ 0,
662 /* 0x064 PMEN_REG */ 0,
663 /* 0x068 PLMBASE_REG */ 0,
664 /* 0x06c PLMLIMIT_REG */ 0,
665 /* 0x070 PHMBASE_REG */ 0, 0,
666 /* 0x078 PHMLIMIT_REG */ 0, 0,
667 /* 0x080 IQH_REG */ 0, 0,
668 /* 0x088 IQT_REG */ 0, 0,
669 /* 0x090 IQA_REG */ 0, 0,
670 /* 0x098 Reserved */ 0,
671 /* 0x09c ICS_REG */ VTD_ICS_REG_RW1C_MASK,
672 /* 0x0a0 IECTL_REG */ 0,
673 /* 0x0a4 IEDATA_REG */ 0,
674 /* 0x0a8 IEADDR_REG */ 0,
675 /* 0x0ac IEUADDR_REG */ 0,
676 /* 0x0b0 IQERCD_REG */ 0, 0,
677 /* 0x0b8 IRTA_REG */ 0, 0,
678 /* 0x0c0 PQH_REG */ 0, 0,
679 /* 0x0c8 PQT_REG */ 0, 0,
680 /* 0x0d0 PQA_REG */ 0, 0,
681 /* 0x0d8 Reserved */ 0,
682 /* 0x0dc PRS_REG */ 0,
683 /* 0x0e0 PECTL_REG */ 0,
684 /* 0x0e4 PEDATA_REG */ 0,
685 /* 0x0e8 PEADDR_REG */ 0,
686 /* 0x0ec PEUADDR_REG */ 0,
687 /* 0x0f0 Reserved */ 0, 0,
688 /* 0x0f8 Reserved */ 0, 0,
689 /* 0x100 MTRRCAP_REG */ 0, 0,
690 /* 0x108 MTRRDEF_REG */ 0, 0,
691 /* 0x110 Reserved */ 0, 0,
692 /* 0x118 Reserved */ 0, 0,
693 /* 0x120 MTRR_FIX64_00000_REG */ 0, 0,
694 /* 0x128 MTRR_FIX16K_80000_REG */ 0, 0,
695 /* 0x130 MTRR_FIX16K_A0000_REG */ 0, 0,
696 /* 0x138 MTRR_FIX4K_C0000_REG */ 0, 0,
697 /* 0x140 MTRR_FIX4K_C8000_REG */ 0, 0,
698 /* 0x148 MTRR_FIX4K_D0000_REG */ 0, 0,
699 /* 0x150 MTRR_FIX4K_D8000_REG */ 0, 0,
700 /* 0x158 MTRR_FIX4K_E0000_REG */ 0, 0,
701 /* 0x160 MTRR_FIX4K_E8000_REG */ 0, 0,
702 /* 0x168 MTRR_FIX4K_F0000_REG */ 0, 0,
703 /* 0x170 MTRR_FIX4K_F8000_REG */ 0, 0,
704 /* 0x178 Reserved */ 0, 0,
705 /* 0x180 MTRR_PHYSBASE0_REG */ 0, 0,
706 /* 0x188 MTRR_PHYSMASK0_REG */ 0, 0,
707 /* 0x190 MTRR_PHYSBASE1_REG */ 0, 0,
708 /* 0x198 MTRR_PHYSMASK1_REG */ 0, 0,
709 /* 0x1a0 MTRR_PHYSBASE2_REG */ 0, 0,
710 /* 0x1a8 MTRR_PHYSMASK2_REG */ 0, 0,
711 /* 0x1b0 MTRR_PHYSBASE3_REG */ 0, 0,
712 /* 0x1b8 MTRR_PHYSMASK3_REG */ 0, 0,
713 /* 0x1c0 MTRR_PHYSBASE4_REG */ 0, 0,
714 /* 0x1c8 MTRR_PHYSMASK4_REG */ 0, 0,
715 /* 0x1d0 MTRR_PHYSBASE5_REG */ 0, 0,
716 /* 0x1d8 MTRR_PHYSMASK5_REG */ 0, 0,
717 /* 0x1e0 MTRR_PHYSBASE6_REG */ 0, 0,
718 /* 0x1e8 MTRR_PHYSMASK6_REG */ 0, 0,
719 /* 0x1f0 MTRR_PHYSBASE7_REG */ 0, 0,
720 /* 0x1f8 MTRR_PHYSMASK7_REG */ 0, 0,
721 /* 0x200 MTRR_PHYSBASE8_REG */ 0, 0,
722 /* 0x208 MTRR_PHYSMASK8_REG */ 0, 0,
723 /* 0x210 MTRR_PHYSBASE9_REG */ 0, 0,
724 /* 0x218 MTRR_PHYSMASK9_REG */ 0, 0,
725};
726AssertCompile(sizeof(g_au32Rw1cMasks0) == DMAR_MMIO_GROUP_0_SIZE);
727
728/**
729 * Read-write masks for DMAR registers (group 1).
730 */
731static uint32_t const g_au32RwMasks1[] =
732{
733 /* Offset Register Low High */
734 /* 0xe00 VCCAP_REG */ DMAR_LO_U32(VTD_VCCAP_REG_RW_MASK), DMAR_HI_U32(VTD_VCCAP_REG_RW_MASK),
735 /* 0xe08 VCMD_EO_REG */ DMAR_LO_U32(VTD_VCMD_EO_REG_RW_MASK), DMAR_HI_U32(VTD_VCMD_EO_REG_RW_MASK),
736 /* 0xe10 VCMD_REG */ 0, 0, /* RO: VCS not supported. */
737 /* 0xe18 VCMDRSVD_REG */ 0, 0,
738 /* 0xe20 VCRSP_REG */ 0, 0, /* RO: VCS not supported. */
739 /* 0xe28 VCRSPRSVD_REG */ 0, 0,
740 /* 0xe30 Reserved */ 0, 0,
741 /* 0xe38 Reserved */ 0, 0,
742 /* 0xe40 Reserved */ 0, 0,
743 /* 0xe48 Reserved */ 0, 0,
744 /* 0xe50 IVA_REG */ DMAR_LO_U32(VTD_IVA_REG_RW_MASK), DMAR_HI_U32(VTD_IVA_REG_RW_MASK),
745 /* 0xe58 IOTLB_REG */ DMAR_LO_U32(VTD_IOTLB_REG_RW_MASK), DMAR_HI_U32(VTD_IOTLB_REG_RW_MASK),
746 /* 0xe60 Reserved */ 0, 0,
747 /* 0xe68 Reserved */ 0, 0,
748 /* 0xe70 FRCD_REG_LO */ DMAR_LO_U32(VTD_FRCD_REG_LO_RW_MASK), DMAR_HI_U32(VTD_FRCD_REG_LO_RW_MASK),
749 /* 0xe78 FRCD_REG_HI */ DMAR_LO_U32(VTD_FRCD_REG_HI_RW_MASK), DMAR_HI_U32(VTD_FRCD_REG_HI_RW_MASK),
750};
751AssertCompile(sizeof(g_au32RwMasks1) == DMAR_MMIO_GROUP_1_SIZE);
752AssertCompile((DMAR_MMIO_OFF_FRCD_LO_REG - DMAR_MMIO_GROUP_1_OFF_FIRST) + DMAR_FRCD_REG_COUNT * 2 * sizeof(uint64_t) );
753
754/**
755 * Read-only Status, Write-1-to-clear masks for DMAR registers (group 1).
756 */
757static uint32_t const g_au32Rw1cMasks1[] =
758{
759 /* Offset Register Low High */
760 /* 0xe00 VCCAP_REG */ 0, 0,
761 /* 0xe08 VCMD_EO_REG */ 0, 0,
762 /* 0xe10 VCMD_REG */ 0, 0,
763 /* 0xe18 VCMDRSVD_REG */ 0, 0,
764 /* 0xe20 VCRSP_REG */ 0, 0,
765 /* 0xe28 VCRSPRSVD_REG */ 0, 0,
766 /* 0xe30 Reserved */ 0, 0,
767 /* 0xe38 Reserved */ 0, 0,
768 /* 0xe40 Reserved */ 0, 0,
769 /* 0xe48 Reserved */ 0, 0,
770 /* 0xe50 IVA_REG */ 0, 0,
771 /* 0xe58 IOTLB_REG */ 0, 0,
772 /* 0xe60 Reserved */ 0, 0,
773 /* 0xe68 Reserved */ 0, 0,
774 /* 0xe70 FRCD_REG_LO */ DMAR_LO_U32(VTD_FRCD_REG_LO_RW1C_MASK), DMAR_HI_U32(VTD_FRCD_REG_LO_RW1C_MASK),
775 /* 0xe78 FRCD_REG_HI */ DMAR_LO_U32(VTD_FRCD_REG_HI_RW1C_MASK), DMAR_HI_U32(VTD_FRCD_REG_HI_RW1C_MASK),
776};
777AssertCompile(sizeof(g_au32Rw1cMasks1) == DMAR_MMIO_GROUP_1_SIZE);
778
779/** Array of RW masks for each register group. */
780static uint8_t const *g_apbRwMasks[] = { (uint8_t *)&g_au32RwMasks0[0], (uint8_t *)&g_au32RwMasks1[0] };
781
782/** Array of RW1C masks for each register group. */
783static uint8_t const *g_apbRw1cMasks[] = { (uint8_t *)&g_au32Rw1cMasks0[0], (uint8_t *)&g_au32Rw1cMasks1[0] };
784
785/* Masks arrays must be identical in size (even bounds checking code assumes this). */
786AssertCompile(sizeof(g_apbRw1cMasks) == sizeof(g_apbRwMasks));
787
788/** Array of valid domain-ID bits. */
789static uint16_t const g_auNdMask[] = { 0xf, 0x3f, 0xff, 0x3ff, 0xfff, 0x3fff, 0xffff, 0 };
790AssertCompile(RT_ELEMENTS(g_auNdMask) >= DMAR_ND);
791
792
793#ifndef VBOX_DEVICE_STRUCT_TESTCASE
794/** @todo Add IOMMU struct size/alignment verification, see
795 * Devices/testcase/Makefile.kmk and
796 * Devices/testcase/tstDeviceStructSize[RC].cpp */
797
798/**
799 * Returns the supported adjusted guest-address width (SAGAW) given the maximum
800 * guest address width (MGAW).
801 *
802 * @returns The CAP_REG.SAGAW value.
803 * @param uMgaw The CAP_REG.MGAW value.
804 */
805static uint8_t vtdCapRegGetSagaw(uint8_t uMgaw)
806{
807 /*
808 * It doesn't make sense to me that a CPU (or IOMMU hardware) will ever support
809 * 5-level paging but not 4 or 3-level paging. So smaller page-table levels
810 * are always OR'ed in below.
811 *
812 * The bit values below (57, 48, 39 bits) represents the levels of page-table walks
813 * for 4KB base page size (5-level, 4-level and 3-level paging respectively).
814 *
815 * See Intel VT-d spec. 10.4.2 "Capability Register".
816 */
817 ++uMgaw;
818 uint8_t const fSagaw = uMgaw >= 57 ? RT_BIT(3) | RT_BIT(2) | RT_BIT(1)
819 : uMgaw >= 48 ? RT_BIT(2) | RT_BIT(1)
820 : uMgaw >= 39 ? RT_BIT(1)
821 : 0;
822 return fSagaw;
823}
824
825
826/**
827 * Returns the maximum supported paging level given the supported adjusted
828 * guest-address width (SAGAW) field.
829 *
830 * @returns The highest paging level supported, 0 if invalid.
831 * @param fSagaw The CAP_REG.SAGAW value.
832 */
833static uint8_t vtdCapRegGetMaxPagingLevel(uint8_t fSagaw)
834{
835 uint8_t const cMaxPagingLevel = fSagaw & RT_BIT(3) ? 5
836 : fSagaw & RT_BIT(2) ? 4
837 : fSagaw & RT_BIT(1) ? 3
838 : 0;
839 return cMaxPagingLevel;
840}
841
842
843/**
844 * Returns whether the interrupt remapping (IR) fault is qualified or not.
845 *
846 * @returns @c true if qualified, @c false otherwise.
847 * @param enmIrFault The interrupt remapping fault condition.
848 */
849static bool vtdIrFaultIsQualified(VTDIRFAULT enmIrFault)
850{
851 switch (enmIrFault)
852 {
853 case VTDIRFAULT_IRTE_NOT_PRESENT:
854 case VTDIRFAULT_IRTE_PRESENT_RSVD:
855 case VTDIRFAULT_IRTE_PRESENT_INVALID:
856 case VTDIRFAULT_PID_READ_FAILED:
857 case VTDIRFAULT_PID_RSVD:
858 return true;
859 default:
860 return false;
861 }
862}
863
864
865/**
866 * Returns table translation mode's descriptive name.
867 *
868 * @returns The descriptive name.
869 * @param uTtm The RTADDR_REG.TTM value.
870 */
871static const char* vtdRtaddrRegGetTtmDesc(uint8_t uTtm)
872{
873 Assert(!(uTtm & 3));
874 static const char* s_apszTtmNames[] =
875 {
876 "Legacy Mode",
877 "Scalable Mode",
878 "Reserved",
879 "Abort-DMA Mode"
880 };
881 return s_apszTtmNames[uTtm & (RT_ELEMENTS(s_apszTtmNames) - 1)];
882}
883
884
885/**
886 * Gets the index of the group the register belongs to given its MMIO offset.
887 *
888 * @returns The group index.
889 * @param offReg The MMIO offset of the register.
890 * @param cbReg The size of the access being made (for bounds checking on
891 * debug builds).
892 */
893DECLINLINE(uint8_t) dmarRegGetGroupIndex(uint16_t offReg, uint8_t cbReg)
894{
895 uint16_t const offLast = offReg + cbReg - 1;
896 AssertCompile(DMAR_MMIO_GROUP_0_OFF_FIRST == 0);
897 AssertMsg(DMAR_IS_MMIO_OFF_VALID(offLast), ("off=%#x cb=%u\n", offReg, cbReg));
898 return !(offLast < DMAR_MMIO_GROUP_0_OFF_END);
899}
900
901
902/**
903 * Gets the group the register belongs to given its MMIO offset.
904 *
905 * @returns Pointer to the first element of the register group.
906 * @param pThis The shared DMAR device state.
907 * @param offReg The MMIO offset of the register.
908 * @param cbReg The size of the access being made (for bounds checking on
909 * debug builds).
910 * @param pIdxGroup Where to store the index of the register group the register
911 * belongs to.
912 */
913DECLINLINE(uint8_t *) dmarRegGetGroup(PDMAR pThis, uint16_t offReg, uint8_t cbReg, uint8_t *pIdxGroup)
914{
915 *pIdxGroup = dmarRegGetGroupIndex(offReg, cbReg);
916 uint8_t *apbRegs[] = { &pThis->abRegs0[0], &pThis->abRegs1[0] };
917 return apbRegs[*pIdxGroup];
918}
919
920
921/**
922 * Const/read-only version of dmarRegGetGroup.
923 *
924 * @copydoc dmarRegGetGroup
925 */
926DECLINLINE(uint8_t const*) dmarRegGetGroupRo(PCDMAR pThis, uint16_t offReg, uint8_t cbReg, uint8_t *pIdxGroup)
927{
928 *pIdxGroup = dmarRegGetGroupIndex(offReg, cbReg);
929 uint8_t const *apbRegs[] = { &pThis->abRegs0[0], &pThis->abRegs1[0] };
930 return apbRegs[*pIdxGroup];
931}
932
933
934/**
935 * Writes a 32-bit register with the exactly the supplied value.
936 *
937 * @param pThis The shared DMAR device state.
938 * @param offReg The MMIO offset of the register.
939 * @param uReg The 32-bit value to write.
940 */
941static void dmarRegWriteRaw32(PDMAR pThis, uint16_t offReg, uint32_t uReg)
942{
943 uint8_t idxGroup;
944 uint8_t *pabRegs = dmarRegGetGroup(pThis, offReg, sizeof(uint32_t), &idxGroup);
945 NOREF(idxGroup);
946 *(uint32_t *)(pabRegs + offReg) = uReg;
947}
948
949
950/**
951 * Writes a 64-bit register with the exactly the supplied value.
952 *
953 * @param pThis The shared DMAR device state.
954 * @param offReg The MMIO offset of the register.
955 * @param uReg The 64-bit value to write.
956 */
957static void dmarRegWriteRaw64(PDMAR pThis, uint16_t offReg, uint64_t uReg)
958{
959 uint8_t idxGroup;
960 uint8_t *pabRegs = dmarRegGetGroup(pThis, offReg, sizeof(uint64_t), &idxGroup);
961 NOREF(idxGroup);
962 *(uint64_t *)(pabRegs + offReg) = uReg;
963}
964
965
966/**
967 * Reads a 32-bit register with exactly the value it contains.
968 *
969 * @returns The raw register value.
970 * @param pThis The shared DMAR device state.
971 * @param offReg The MMIO offset of the register.
972 */
973static uint32_t dmarRegReadRaw32(PCDMAR pThis, uint16_t offReg)
974{
975 uint8_t idxGroup;
976 uint8_t const *pabRegs = dmarRegGetGroupRo(pThis, offReg, sizeof(uint32_t), &idxGroup);
977 NOREF(idxGroup);
978 return *(uint32_t *)(pabRegs + offReg);
979}
980
981
982/**
983 * Reads a 64-bit register with exactly the value it contains.
984 *
985 * @returns The raw register value.
986 * @param pThis The shared DMAR device state.
987 * @param offReg The MMIO offset of the register.
988 */
989static uint64_t dmarRegReadRaw64(PCDMAR pThis, uint16_t offReg)
990{
991 uint8_t idxGroup;
992 uint8_t const *pabRegs = dmarRegGetGroupRo(pThis, offReg, sizeof(uint64_t), &idxGroup);
993 NOREF(idxGroup);
994 return *(uint64_t *)(pabRegs + offReg);
995}
996
997
998/**
999 * Reads a 32-bit register with exactly the value it contains along with their
1000 * corresponding masks
1001 *
1002 * @param pThis The shared DMAR device state.
1003 * @param offReg The MMIO offset of the register.
1004 * @param puReg Where to store the raw 32-bit register value.
1005 * @param pfRwMask Where to store the RW mask corresponding to this register.
1006 * @param pfRw1cMask Where to store the RW1C mask corresponding to this register.
1007 */
1008static void dmarRegReadRaw32Ex(PCDMAR pThis, uint16_t offReg, uint32_t *puReg, uint32_t *pfRwMask, uint32_t *pfRw1cMask)
1009{
1010 uint8_t idxGroup;
1011 uint8_t const *pabRegs = dmarRegGetGroupRo(pThis, offReg, sizeof(uint32_t), &idxGroup);
1012 Assert(idxGroup < RT_ELEMENTS(g_apbRwMasks));
1013 uint8_t const *pabRwMasks = g_apbRwMasks[idxGroup];
1014 uint8_t const *pabRw1cMasks = g_apbRw1cMasks[idxGroup];
1015 *puReg = *(uint32_t *)(pabRegs + offReg);
1016 *pfRwMask = *(uint32_t *)(pabRwMasks + offReg);
1017 *pfRw1cMask = *(uint32_t *)(pabRw1cMasks + offReg);
1018}
1019
1020
1021/**
1022 * Reads a 64-bit register with exactly the value it contains along with their
1023 * corresponding masks.
1024 *
1025 * @param pThis The shared DMAR device state.
1026 * @param offReg The MMIO offset of the register.
1027 * @param puReg Where to store the raw 64-bit register value.
1028 * @param pfRwMask Where to store the RW mask corresponding to this register.
1029 * @param pfRw1cMask Where to store the RW1C mask corresponding to this register.
1030 */
1031static void dmarRegReadRaw64Ex(PCDMAR pThis, uint16_t offReg, uint64_t *puReg, uint64_t *pfRwMask, uint64_t *pfRw1cMask)
1032{
1033 uint8_t idxGroup;
1034 uint8_t const *pabRegs = dmarRegGetGroupRo(pThis, offReg, sizeof(uint64_t), &idxGroup);
1035 Assert(idxGroup < RT_ELEMENTS(g_apbRwMasks));
1036 uint8_t const *pabRwMasks = g_apbRwMasks[idxGroup];
1037 uint8_t const *pabRw1cMasks = g_apbRw1cMasks[idxGroup];
1038 *puReg = *(uint64_t *)(pabRegs + offReg);
1039 *pfRwMask = *(uint64_t *)(pabRwMasks + offReg);
1040 *pfRw1cMask = *(uint64_t *)(pabRw1cMasks + offReg);
1041}
1042
1043
1044/**
1045 * Writes a 32-bit register as it would be when written by software.
1046 * This will preserve read-only bits, mask off reserved bits and clear RW1C bits.
1047 *
1048 * @returns The value that's actually written to the register.
1049 * @param pThis The shared DMAR device state.
1050 * @param offReg The MMIO offset of the register.
1051 * @param uReg The 32-bit value to write.
1052 * @param puPrev Where to store the register value prior to writing.
1053 */
1054static uint32_t dmarRegWrite32(PDMAR pThis, uint16_t offReg, uint32_t uReg, uint32_t *puPrev)
1055{
1056 /* Read current value from the 32-bit register. */
1057 uint32_t uCurReg;
1058 uint32_t fRwMask;
1059 uint32_t fRw1cMask;
1060 dmarRegReadRaw32Ex(pThis, offReg, &uCurReg, &fRwMask, &fRw1cMask);
1061 *puPrev = uCurReg;
1062
1063 uint32_t const fRoBits = uCurReg & ~fRwMask; /* Preserve current read-only and reserved bits. */
1064 uint32_t const fRwBits = uReg & fRwMask; /* Merge newly written read/write bits. */
1065 uint32_t const fRw1cBits = uReg & fRw1cMask; /* Clear 1s written to RW1C bits. */
1066 uint32_t const uNewReg = (fRoBits | fRwBits) & ~fRw1cBits;
1067
1068 /* Write new value to the 32-bit register. */
1069 dmarRegWriteRaw32(pThis, offReg, uNewReg);
1070 return uNewReg;
1071}
1072
1073
1074/**
1075 * Writes a 64-bit register as it would be when written by software.
1076 * This will preserve read-only bits, mask off reserved bits and clear RW1C bits.
1077 *
1078 * @returns The value that's actually written to the register.
1079 * @param pThis The shared DMAR device state.
1080 * @param offReg The MMIO offset of the register.
1081 * @param uReg The 64-bit value to write.
1082 * @param puPrev Where to store the register value prior to writing.
1083 */
1084static uint64_t dmarRegWrite64(PDMAR pThis, uint16_t offReg, uint64_t uReg, uint64_t *puPrev)
1085{
1086 /* Read current value from the 64-bit register. */
1087 uint64_t uCurReg;
1088 uint64_t fRwMask;
1089 uint64_t fRw1cMask;
1090 dmarRegReadRaw64Ex(pThis, offReg, &uCurReg, &fRwMask, &fRw1cMask);
1091 *puPrev = uCurReg;
1092
1093 uint64_t const fRoBits = uCurReg & ~fRwMask; /* Preserve current read-only and reserved bits. */
1094 uint64_t const fRwBits = uReg & fRwMask; /* Merge newly written read/write bits. */
1095 uint64_t const fRw1cBits = uReg & fRw1cMask; /* Clear 1s written to RW1C bits. */
1096 uint64_t const uNewReg = (fRoBits | fRwBits) & ~fRw1cBits;
1097
1098 /* Write new value to the 64-bit register. */
1099 dmarRegWriteRaw64(pThis, offReg, uNewReg);
1100 return uNewReg;
1101}
1102
1103
1104/**
1105 * Reads a 32-bit register as it would be when read by software.
1106 *
1107 * @returns The register value.
1108 * @param pThis The shared DMAR device state.
1109 * @param offReg The MMIO offset of the register.
1110 */
1111static uint32_t dmarRegRead32(PCDMAR pThis, uint16_t offReg)
1112{
1113 return dmarRegReadRaw32(pThis, offReg);
1114}
1115
1116
1117/**
1118 * Reads a 64-bit register as it would be when read by software.
1119 *
1120 * @returns The register value.
1121 * @param pThis The shared DMAR device state.
1122 * @param offReg The MMIO offset of the register.
1123 */
1124static uint64_t dmarRegRead64(PCDMAR pThis, uint16_t offReg)
1125{
1126 return dmarRegReadRaw64(pThis, offReg);
1127}
1128
1129
1130/**
1131 * Modifies a 32-bit register.
1132 *
1133 * @param pThis The shared DMAR device state.
1134 * @param offReg The MMIO offset of the register.
1135 * @param fAndMask The AND mask (applied first).
1136 * @param fOrMask The OR mask.
1137 * @remarks This does NOT apply RO or RW1C masks while modifying the
1138 * register.
1139 */
1140static void dmarRegChangeRaw32(PDMAR pThis, uint16_t offReg, uint32_t fAndMask, uint32_t fOrMask)
1141{
1142 uint32_t uReg = dmarRegReadRaw32(pThis, offReg);
1143 uReg = (uReg & fAndMask) | fOrMask;
1144 dmarRegWriteRaw32(pThis, offReg, uReg);
1145}
1146
1147
1148/**
1149 * Modifies a 64-bit register.
1150 *
1151 * @param pThis The shared DMAR device state.
1152 * @param offReg The MMIO offset of the register.
1153 * @param fAndMask The AND mask (applied first).
1154 * @param fOrMask The OR mask.
1155 * @remarks This does NOT apply RO or RW1C masks while modifying the
1156 * register.
1157 */
1158static void dmarRegChangeRaw64(PDMAR pThis, uint16_t offReg, uint64_t fAndMask, uint64_t fOrMask)
1159{
1160 uint64_t uReg = dmarRegReadRaw64(pThis, offReg);
1161 uReg = (uReg & fAndMask) | fOrMask;
1162 dmarRegWriteRaw64(pThis, offReg, uReg);
1163}
1164
1165
1166/**
1167 * Checks if the invalidation-queue is empty.
1168 *
1169 * Extended version which optionally returns the current queue head and tail
1170 * offsets.
1171 *
1172 * @returns @c true if empty, @c false otherwise.
1173 * @param pThis The shared DMAR device state.
1174 * @param poffQh Where to store the queue head offset. Optional, can be NULL.
1175 * @param poffQt Where to store the queue tail offset. Optional, can be NULL.
1176 */
1177static bool dmarInvQueueIsEmptyEx(PCDMAR pThis, uint32_t *poffQh, uint32_t *poffQt)
1178{
1179 /* Read only the low-32 bits of the queue head and queue tail as high bits are all RsvdZ.*/
1180 uint32_t const uIqtReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_IQT_REG);
1181 uint32_t const uIqhReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_IQH_REG);
1182
1183 /* Don't bother masking QT, QH since other bits are RsvdZ. */
1184 Assert(!(uIqtReg & ~VTD_BF_IQT_REG_QT_MASK));
1185 Assert(!(uIqhReg & ~VTD_BF_IQH_REG_QH_MASK));
1186 if (poffQh)
1187 *poffQh = uIqhReg;
1188 if (poffQt)
1189 *poffQt = uIqtReg;
1190 return uIqtReg == uIqhReg;
1191}
1192
1193
1194/**
1195 * Checks if the invalidation-queue is empty.
1196 *
1197 * @returns @c true if empty, @c false otherwise.
1198 * @param pThis The shared DMAR device state.
1199 */
1200static bool dmarInvQueueIsEmpty(PCDMAR pThis)
1201{
1202 return dmarInvQueueIsEmptyEx(pThis, NULL /* poffQh */, NULL /* poffQt */);
1203}
1204
1205
1206/**
1207 * Checks if the invalidation-queue is capable of processing requests.
1208 *
1209 * @returns @c true if the invalidation-queue can process requests, @c false
1210 * otherwise.
1211 * @param pThis The shared DMAR device state.
1212 */
1213static bool dmarInvQueueCanProcessRequests(PCDMAR pThis)
1214{
1215 /* Check if queued-invalidation is enabled. */
1216 uint32_t const uGstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_GSTS_REG);
1217 if (uGstsReg & VTD_BF_GSTS_REG_QIES_MASK)
1218 {
1219 /* Check if there are no invalidation-queue or timeout errors. */
1220 uint32_t const uFstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FSTS_REG);
1221 if (!(uFstsReg & (VTD_BF_FSTS_REG_IQE_MASK | VTD_BF_FSTS_REG_ITE_MASK)))
1222 return true;
1223 }
1224 return false;
1225}
1226
1227
1228/**
1229 * Wakes up the invalidation-queue thread if there are requests to be processed.
1230 *
1231 * @param pDevIns The IOMMU device instance.
1232 */
1233static void dmarInvQueueThreadWakeUpIfNeeded(PPDMDEVINS pDevIns)
1234{
1235 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1236 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
1237 Log4Func(("\n"));
1238
1239 DMAR_ASSERT_LOCK_IS_OWNER(pDevIns, pThisCC);
1240
1241 if ( dmarInvQueueCanProcessRequests(pThis)
1242 && !dmarInvQueueIsEmpty(pThis))
1243 {
1244 Log4Func(("Signaling the invalidation-queue thread\n"));
1245 PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hEvtInvQueue);
1246 }
1247}
1248
1249
1250/**
1251 * Raises an event on behalf of the DMAR.
1252 *
1253 * These are events that are generated by the DMAR itself (like faults and
1254 * invalidation completion notifications).
1255 *
1256 * @param pDevIns The IOMMU device instance.
1257 * @param enmEventType The DMAR event type.
1258 *
1259 * @remarks The DMAR lock must be held while calling this function.
1260 */
1261static void dmarEventRaiseInterrupt(PPDMDEVINS pDevIns, DMAREVENTTYPE enmEventType)
1262{
1263 uint16_t offCtlReg;
1264 uint32_t fIntrMaskedMask;
1265 uint32_t fIntrPendingMask;
1266 uint16_t offMsiAddrLoReg;
1267 uint16_t offMsiAddrHiReg;
1268 uint16_t offMsiDataReg;
1269 switch (enmEventType)
1270 {
1271 case DMAREVENTTYPE_INV_COMPLETE:
1272 {
1273 offCtlReg = VTD_MMIO_OFF_IECTL_REG;
1274 fIntrMaskedMask = VTD_BF_IECTL_REG_IM_MASK;
1275 fIntrPendingMask = VTD_BF_IECTL_REG_IP_MASK;
1276 offMsiAddrLoReg = VTD_MMIO_OFF_IEADDR_REG;
1277 offMsiAddrHiReg = VTD_MMIO_OFF_IEUADDR_REG;
1278 offMsiDataReg = VTD_MMIO_OFF_IEDATA_REG;
1279 break;
1280 }
1281
1282 case DMAREVENTTYPE_FAULT:
1283 {
1284 offCtlReg = VTD_MMIO_OFF_FECTL_REG;
1285 fIntrMaskedMask = VTD_BF_FECTL_REG_IM_MASK;
1286 fIntrPendingMask = VTD_BF_FECTL_REG_IP_MASK;
1287 offMsiAddrLoReg = VTD_MMIO_OFF_FEADDR_REG;
1288 offMsiAddrHiReg = VTD_MMIO_OFF_FEUADDR_REG;
1289 offMsiDataReg = VTD_MMIO_OFF_FEDATA_REG;
1290 break;
1291 }
1292
1293 default:
1294 {
1295 /* Shouldn't ever happen. */
1296 AssertMsgFailedReturnVoid(("DMAR event type %#x unknown!\n", enmEventType));
1297 }
1298 }
1299
1300 /* Check if software has masked the interrupt. */
1301 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1302 uint32_t uCtlReg = dmarRegReadRaw32(pThis, offCtlReg);
1303 if (!(uCtlReg & fIntrMaskedMask))
1304 {
1305 /*
1306 * Interrupt is unmasked, raise it.
1307 * Interrupts generated by the DMAR have trigger mode and level as 0.
1308 * See Intel spec. 5.1.6 "Remapping Hardware Event Interrupt Programming".
1309 */
1310 MSIMSG Msi;
1311 Msi.Addr.au32[0] = dmarRegReadRaw32(pThis, offMsiAddrLoReg);
1312 Msi.Addr.au32[1] = (pThis->fExtCapReg & VTD_BF_ECAP_REG_EIM_MASK) ? dmarRegReadRaw32(pThis, offMsiAddrHiReg) : 0;
1313 Msi.Data.u32 = dmarRegReadRaw32(pThis, offMsiDataReg);
1314 Assert(Msi.Data.n.u1Level == 0);
1315 Assert(Msi.Data.n.u1TriggerMode == 0);
1316
1317 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
1318 pThisCC->CTX_SUFF(pIommuHlp)->pfnSendMsi(pDevIns, &Msi, 0 /* uTagSrc */);
1319
1320 /* Clear interrupt pending bit. */
1321 uCtlReg &= ~fIntrPendingMask;
1322 dmarRegWriteRaw32(pThis, offCtlReg, uCtlReg);
1323 }
1324 else
1325 {
1326 /* Interrupt is masked, set the interrupt pending bit. */
1327 uCtlReg |= fIntrPendingMask;
1328 dmarRegWriteRaw32(pThis, offCtlReg, uCtlReg);
1329 }
1330}
1331
1332
1333/**
1334 * Raises an interrupt in response to a fault event.
1335 *
1336 * @param pDevIns The IOMMU device instance.
1337 *
1338 * @remarks This assumes the caller has already set the required status bits in the
1339 * FSTS_REG (namely one or more of PPF, PFO, IQE, ICE or ITE bits).
1340 */
1341static void dmarFaultEventRaiseInterrupt(PPDMDEVINS pDevIns)
1342{
1343 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1344 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
1345 DMAR_ASSERT_LOCK_IS_OWNER(pDevIns, pThisCC);
1346
1347#ifdef RT_STRICT
1348 {
1349 uint32_t const uFstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FSTS_REG);
1350 uint32_t const fFaultMask = VTD_BF_FSTS_REG_PPF_MASK | VTD_BF_FSTS_REG_PFO_MASK
1351 /* | VTD_BF_FSTS_REG_APF_MASK | VTD_BF_FSTS_REG_AFO_MASK */ /* AFL not supported */
1352 /* | VTD_BF_FSTS_REG_ICE_MASK | VTD_BF_FSTS_REG_ITE_MASK */ /* Device-TLBs not supported */
1353 | VTD_BF_FSTS_REG_IQE_MASK;
1354 Assert(uFstsReg & fFaultMask);
1355 }
1356#endif
1357 dmarEventRaiseInterrupt(pDevIns, DMAREVENTTYPE_FAULT);
1358}
1359
1360
1361#ifdef IN_RING3
1362/**
1363 * Raises an interrupt in response to an invalidation (complete) event.
1364 *
1365 * @param pDevIns The IOMMU device instance.
1366 */
1367static void dmarR3InvEventRaiseInterrupt(PPDMDEVINS pDevIns)
1368{
1369 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1370 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
1371 DMAR_ASSERT_LOCK_IS_OWNER(pDevIns, pThisCC);
1372
1373 uint32_t const uIcsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_ICS_REG);
1374 if (!(uIcsReg & VTD_BF_ICS_REG_IWC_MASK))
1375 {
1376 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_ICS_REG, UINT32_MAX, VTD_BF_ICS_REG_IWC_MASK);
1377 dmarEventRaiseInterrupt(pDevIns, DMAREVENTTYPE_INV_COMPLETE);
1378 }
1379}
1380#endif /* IN_RING3 */
1381
1382
1383/**
1384 * Checks if a primary fault can be recorded.
1385 *
1386 * @returns @c true if the fault can be recorded, @c false otherwise.
1387 * @param pDevIns The IOMMU device instance.
1388 * @param pThis The shared DMAR device state.
1389 *
1390 * @remarks Warning: This function has side-effects wrt the DMAR register state. Do
1391 * NOT call it unless there is a fault condition!
1392 */
1393static bool dmarPrimaryFaultCanRecord(PPDMDEVINS pDevIns, PDMAR pThis)
1394{
1395 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
1396 DMAR_ASSERT_LOCK_IS_OWNER(pDevIns, pThisCC);
1397
1398 uint32_t uFstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FSTS_REG);
1399 if (uFstsReg & VTD_BF_FSTS_REG_PFO_MASK)
1400 return false;
1401
1402 /*
1403 * If we add more FRCD registers, we'll have to loop through them here.
1404 * Since we support only one FRCD_REG, we don't support "compression of multiple faults",
1405 * nor do we need to increment FRI.
1406 *
1407 * See Intel VT-d spec. 7.2.1 "Primary Fault Logging".
1408 */
1409 AssertCompile(DMAR_FRCD_REG_COUNT == 1);
1410 uint64_t const uFrcdRegHi = dmarRegReadRaw64(pThis, DMAR_MMIO_OFF_FRCD_HI_REG);
1411 if (uFrcdRegHi & VTD_BF_1_FRCD_REG_F_MASK)
1412 {
1413 uFstsReg |= VTD_BF_FSTS_REG_PFO_MASK;
1414 dmarRegWriteRaw32(pThis, VTD_MMIO_OFF_FSTS_REG, uFstsReg);
1415 return false;
1416 }
1417
1418 return true;
1419}
1420
1421
1422/**
1423 * Records a primary fault.
1424 *
1425 * @param pDevIns The IOMMU device instance.
1426 * @param enmDiag The diagnostic reason.
1427 * @param uFrcdHi The FRCD_HI_REG value for this fault.
1428 * @param uFrcdLo The FRCD_LO_REG value for this fault.
1429 */
1430static void dmarPrimaryFaultRecord(PPDMDEVINS pDevIns, DMARDIAG enmDiag, uint64_t uFrcdHi, uint64_t uFrcdLo)
1431{
1432 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1433 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
1434
1435 DMAR_LOCK(pDevIns, pThisCC);
1436
1437 /* Update the diagnostic reason. */
1438 pThis->enmDiag = enmDiag;
1439
1440 /* We don't support advance fault logging. */
1441 Assert(!(dmarRegRead32(pThis, VTD_MMIO_OFF_GSTS_REG) & VTD_BF_GSTS_REG_AFLS_MASK));
1442
1443 if (dmarPrimaryFaultCanRecord(pDevIns, pThis))
1444 {
1445 /* Update the fault recording registers with the fault information. */
1446 dmarRegWriteRaw64(pThis, DMAR_MMIO_OFF_FRCD_HI_REG, uFrcdHi);
1447 dmarRegWriteRaw64(pThis, DMAR_MMIO_OFF_FRCD_LO_REG, uFrcdLo);
1448
1449 /* Set the Pending Primary Fault (PPF) field in the status register. */
1450 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_FSTS_REG, UINT32_MAX, VTD_BF_FSTS_REG_PPF_MASK);
1451
1452 /* Raise interrupt if necessary. */
1453 dmarFaultEventRaiseInterrupt(pDevIns);
1454 }
1455
1456 DMAR_UNLOCK(pDevIns, pThisCC);
1457}
1458
1459
1460/**
1461 * Records an interrupt request fault.
1462 *
1463 * @param pDevIns The IOMMU device instance.
1464 * @param enmDiag The diagnostic reason.
1465 * @param enmIrFault The interrupt fault reason.
1466 * @param idDevice The device ID (bus, device, function).
1467 * @param idxIntr The interrupt index.
1468 */
1469static void dmarIrFaultRecord(PPDMDEVINS pDevIns, DMARDIAG enmDiag, VTDIRFAULT enmIrFault, uint16_t idDevice, uint16_t idxIntr)
1470{
1471 uint64_t const uFrcdHi = RT_BF_MAKE(VTD_BF_1_FRCD_REG_SID, idDevice)
1472 | RT_BF_MAKE(VTD_BF_1_FRCD_REG_FR, enmIrFault)
1473 | RT_BF_MAKE(VTD_BF_1_FRCD_REG_F, 1);
1474 uint64_t const uFrcdLo = (uint64_t)idxIntr << 48;
1475 dmarPrimaryFaultRecord(pDevIns, enmDiag, uFrcdHi, uFrcdLo);
1476}
1477
1478
1479/**
1480 * Records a qualified interrupt request fault.
1481 *
1482 * Qualified faults are those that can be suppressed by software using the FPD bit
1483 * in the IRTE.
1484 *
1485 * @param pDevIns The IOMMU device instance.
1486 * @param enmDiag The diagnostic reason.
1487 * @param enmIrFault The interrupt fault reason.
1488 * @param idDevice The device ID (bus, device, function).
1489 * @param idxIntr The interrupt index.
1490 * @param pIrte The IRTE that caused this fault.
1491 */
1492static void dmarIrFaultRecordQualified(PPDMDEVINS pDevIns, DMARDIAG enmDiag, VTDIRFAULT enmIrFault, uint16_t idDevice,
1493 uint16_t idxIntr, PCVTD_IRTE_T pIrte)
1494{
1495 Assert(vtdIrFaultIsQualified(enmIrFault));
1496 Assert(pIrte);
1497 if (!(pIrte->au64[0] & VTD_BF_0_IRTE_FPD_MASK))
1498 return dmarIrFaultRecord(pDevIns, enmDiag, enmIrFault, idDevice, idxIntr);
1499}
1500
1501
1502/**
1503 * Records an address translation fault.
1504 *
1505 * @param pDevIns The IOMMU device instance.
1506 * @param enmDiag The diagnostic reason.
1507 * @param enmAtFault The address translation fault reason.
1508 * @param pMemReqRemap The DMA memory request remapping info.
1509 */
1510static void dmarAtFaultRecord(PPDMDEVINS pDevIns, DMARDIAG enmDiag, VTDATFAULT enmAtFault, PCDMARMEMREQREMAP pMemReqRemap)
1511{
1512 /*
1513 * Qualified faults are those that can be suppressed by software using the FPD bit
1514 * in the contex entry, scalable-mode context entry etc.
1515 */
1516 if (!pMemReqRemap->fFpd)
1517 {
1518 uint16_t const idDevice = pMemReqRemap->Req.idDevice;
1519 uint8_t const fType1 = pMemReqRemap->Req.enmReqType & RT_BIT(1);
1520 uint8_t const fType2 = pMemReqRemap->Req.enmReqType & RT_BIT(0);
1521 uint8_t const fExec = pMemReqRemap->Req.fReqPerm & DMAR_PERM_EXE;
1522 uint8_t const fPriv = pMemReqRemap->Req.fReqPerm & DMAR_PERM_PRIV;
1523 bool const fHasPasid = PCIPASID_IS_VALID(pMemReqRemap->Req.Pasid);
1524 uint32_t const uPasid = PCIPASID_VAL(pMemReqRemap->Req.Pasid);
1525 PCIADDRTYPE const enmAt = pMemReqRemap->Req.enmAddrType;
1526
1527 uint64_t const uFrcdHi = RT_BF_MAKE(VTD_BF_1_FRCD_REG_SID, idDevice)
1528 | RT_BF_MAKE(VTD_BF_1_FRCD_REG_T2, fType2)
1529 | RT_BF_MAKE(VTD_BF_1_FRCD_REG_PP, fHasPasid)
1530 | RT_BF_MAKE(VTD_BF_1_FRCD_REG_EXE, fExec)
1531 | RT_BF_MAKE(VTD_BF_1_FRCD_REG_PRIV, fPriv)
1532 | RT_BF_MAKE(VTD_BF_1_FRCD_REG_FR, enmAtFault)
1533 | RT_BF_MAKE(VTD_BF_1_FRCD_REG_PV, uPasid)
1534 | RT_BF_MAKE(VTD_BF_1_FRCD_REG_AT, enmAt)
1535 | RT_BF_MAKE(VTD_BF_1_FRCD_REG_T1, fType1)
1536 | RT_BF_MAKE(VTD_BF_1_FRCD_REG_F, 1);
1537 uint64_t const uFrcdLo = pMemReqRemap->Req.uDmaAddr & X86_PAGE_BASE_MASK;
1538 dmarPrimaryFaultRecord(pDevIns, enmDiag, uFrcdHi, uFrcdLo);
1539 }
1540}
1541
1542
1543/**
1544 * Records an IQE fault.
1545 *
1546 * @param pDevIns The IOMMU device instance.
1547 * @param enmIqei The IQE information.
1548 * @param enmDiag The diagnostic reason.
1549 */
1550static void dmarIqeFaultRecord(PPDMDEVINS pDevIns, DMARDIAG enmDiag, VTDIQEI enmIqei)
1551{
1552 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1553 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
1554
1555 DMAR_LOCK(pDevIns, pThisCC);
1556
1557 /* Update the diagnostic reason. */
1558 pThis->enmDiag = enmDiag;
1559
1560 /* Set the error bit. */
1561 uint32_t const fIqe = RT_BF_MAKE(VTD_BF_FSTS_REG_IQE, 1);
1562 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_FSTS_REG, UINT32_MAX, fIqe);
1563
1564 /* Set the error information. */
1565 uint64_t const fIqei = RT_BF_MAKE(VTD_BF_IQERCD_REG_IQEI, enmIqei);
1566 dmarRegChangeRaw64(pThis, VTD_MMIO_OFF_IQERCD_REG, UINT64_MAX, fIqei);
1567
1568 dmarFaultEventRaiseInterrupt(pDevIns);
1569
1570 DMAR_UNLOCK(pDevIns, pThisCC);
1571}
1572
1573
1574/**
1575 * Handles writes to GCMD_REG.
1576 *
1577 * @returns Strict VBox status code.
1578 * @param pDevIns The IOMMU device instance.
1579 * @param uGcmdReg The value written to GCMD_REG.
1580 */
1581static VBOXSTRICTRC dmarGcmdRegWrite(PPDMDEVINS pDevIns, uint32_t uGcmdReg)
1582{
1583 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1584 uint32_t const uGstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_GSTS_REG);
1585 uint32_t const fChanged = uGstsReg ^ uGcmdReg;
1586 uint64_t const fExtCapReg = pThis->fExtCapReg;
1587
1588 /* Queued-invalidation. */
1589 if ( (fExtCapReg & VTD_BF_ECAP_REG_QI_MASK)
1590 && (fChanged & VTD_BF_GCMD_REG_QIE_MASK))
1591 {
1592 if (uGcmdReg & VTD_BF_GCMD_REG_QIE_MASK)
1593 {
1594 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_GSTS_REG, UINT32_MAX, VTD_BF_GSTS_REG_QIES_MASK);
1595 dmarInvQueueThreadWakeUpIfNeeded(pDevIns);
1596 }
1597 else
1598 {
1599 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_GSTS_REG, ~VTD_BF_GSTS_REG_QIES_MASK, 0 /* fOrMask */);
1600 dmarRegWriteRaw32(pThis, VTD_MMIO_OFF_IQH_REG, 0);
1601 }
1602 }
1603
1604 if (fExtCapReg & VTD_BF_ECAP_REG_IR_MASK)
1605 {
1606 /* Set Interrupt Remapping Table Pointer (SIRTP). */
1607 if (uGcmdReg & VTD_BF_GCMD_REG_SIRTP_MASK)
1608 {
1609 /** @todo Perform global invalidation of all interrupt-entry cache when ESIRTPS is
1610 * supported. */
1611 pThis->uIrtaReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_IRTA_REG);
1612 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_GSTS_REG, UINT32_MAX, VTD_BF_GSTS_REG_IRTPS_MASK);
1613 }
1614
1615 /* Interrupt remapping. */
1616 if (fChanged & VTD_BF_GCMD_REG_IRE_MASK)
1617 {
1618 if (uGcmdReg & VTD_BF_GCMD_REG_IRE_MASK)
1619 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_GSTS_REG, UINT32_MAX, VTD_BF_GSTS_REG_IRES_MASK);
1620 else
1621 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_GSTS_REG, ~VTD_BF_GSTS_REG_IRES_MASK, 0 /* fOrMask */);
1622 }
1623
1624 /* Compatibility format interrupts. */
1625 if (fChanged & VTD_BF_GCMD_REG_CFI_MASK)
1626 {
1627 if (uGcmdReg & VTD_BF_GCMD_REG_CFI_MASK)
1628 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_GSTS_REG, UINT32_MAX, VTD_BF_GSTS_REG_CFIS_MASK);
1629 else
1630 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_GSTS_REG, ~VTD_BF_GSTS_REG_CFIS_MASK, 0 /* fOrMask */);
1631 }
1632 }
1633
1634 /* Set Root Table Pointer (SRTP). */
1635 if (uGcmdReg & VTD_BF_GCMD_REG_SRTP_MASK)
1636 {
1637 /** @todo Perform global invalidation of all remapping translation caches when
1638 * ESRTPS is supported. */
1639 pThis->uRtaddrReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_RTADDR_REG);
1640 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_GSTS_REG, UINT32_MAX, VTD_BF_GSTS_REG_RTPS_MASK);
1641 }
1642
1643 /* Translation (DMA remapping). */
1644 if (fChanged & VTD_BF_GCMD_REG_TE_MASK)
1645 {
1646 if (uGcmdReg & VTD_BF_GCMD_REG_TE_MASK)
1647 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_GSTS_REG, UINT32_MAX, VTD_BF_GSTS_REG_TES_MASK);
1648 else
1649 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_GSTS_REG, ~VTD_BF_GSTS_REG_TES_MASK, 0 /* fOrMask */);
1650 }
1651
1652 return VINF_SUCCESS;
1653}
1654
1655
1656/**
1657 * Handles writes to CCMD_REG.
1658 *
1659 * @returns Strict VBox status code.
1660 * @param pDevIns The IOMMU device instance.
1661 * @param offReg The MMIO register offset.
1662 * @param cbReg The size of the MMIO access (in bytes).
1663 * @param uCcmdReg The value written to CCMD_REG.
1664 */
1665static VBOXSTRICTRC dmarCcmdRegWrite(PPDMDEVINS pDevIns, uint16_t offReg, uint8_t cbReg, uint64_t uCcmdReg)
1666{
1667 /* At present, we only care about responding to high 32-bits writes, low 32-bits are data. */
1668 if (offReg + cbReg > VTD_MMIO_OFF_CCMD_REG + 4)
1669 {
1670 /* Check if we need to invalidate the context-context. */
1671 bool const fIcc = RT_BF_GET(uCcmdReg, VTD_BF_CCMD_REG_ICC);
1672 if (fIcc)
1673 {
1674 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1675 uint8_t const uMajorVersion = RT_BF_GET(pThis->uVerReg, VTD_BF_VER_REG_MAX);
1676 if (uMajorVersion < 6)
1677 {
1678 /* Register-based invalidation can only be used when queued-invalidations are not enabled. */
1679 uint32_t const uGstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_GSTS_REG);
1680 if (!(uGstsReg & VTD_BF_GSTS_REG_QIES_MASK))
1681 {
1682 /* Verify table translation mode is legacy. */
1683 uint8_t const fTtm = RT_BF_GET(pThis->uRtaddrReg, VTD_BF_RTADDR_REG_TTM);
1684 if (fTtm == VTD_TTM_LEGACY_MODE)
1685 {
1686 /** @todo Invalidate. */
1687 return VINF_SUCCESS;
1688 }
1689 pThis->enmDiag = kDmarDiag_CcmdReg_Ttm_Invalid;
1690 }
1691 else
1692 pThis->enmDiag = kDmarDiag_CcmdReg_Qi_Enabled;
1693 }
1694 else
1695 pThis->enmDiag = kDmarDiag_CcmdReg_NotSupported;
1696 dmarRegChangeRaw64(pThis, VTD_MMIO_OFF_GSTS_REG, ~VTD_BF_CCMD_REG_CAIG_MASK, 0 /* fOrMask */);
1697 }
1698 }
1699 return VINF_SUCCESS;
1700}
1701
1702
1703/**
1704 * Handles writes to FECTL_REG.
1705 *
1706 * @returns Strict VBox status code.
1707 * @param pDevIns The IOMMU device instance.
1708 * @param uFectlReg The value written to FECTL_REG.
1709 */
1710static VBOXSTRICTRC dmarFectlRegWrite(PPDMDEVINS pDevIns, uint32_t uFectlReg)
1711{
1712 /*
1713 * If software unmasks the interrupt when the interrupt is pending, we must raise
1714 * the interrupt now (which will consequently clear the interrupt pending (IP) bit).
1715 */
1716 if ( (uFectlReg & VTD_BF_FECTL_REG_IP_MASK)
1717 && ~(uFectlReg & VTD_BF_FECTL_REG_IM_MASK))
1718 dmarEventRaiseInterrupt(pDevIns, DMAREVENTTYPE_FAULT);
1719 return VINF_SUCCESS;
1720}
1721
1722
1723/**
1724 * Handles writes to FSTS_REG.
1725 *
1726 * @returns Strict VBox status code.
1727 * @param pDevIns The IOMMU device instance.
1728 * @param uFstsReg The value written to FSTS_REG.
1729 * @param uPrev The value in FSTS_REG prior to writing it.
1730 */
1731static VBOXSTRICTRC dmarFstsRegWrite(PPDMDEVINS pDevIns, uint32_t uFstsReg, uint32_t uPrev)
1732{
1733 /*
1734 * If software clears other status bits in FSTS_REG (pertaining to primary fault logging),
1735 * the interrupt pending (IP) bit must be cleared.
1736 *
1737 * See Intel VT-d spec. 10.4.10 "Fault Event Control Register".
1738 */
1739 uint32_t const fChanged = uPrev ^ uFstsReg;
1740 if (fChanged & ( VTD_BF_FSTS_REG_ICE_MASK | VTD_BF_FSTS_REG_ITE_MASK
1741 | VTD_BF_FSTS_REG_IQE_MASK | VTD_BF_FSTS_REG_PFO_MASK))
1742 {
1743 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1744 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_FECTL_REG, ~VTD_BF_FECTL_REG_IP_MASK, 0 /* fOrMask */);
1745 }
1746 return VINF_SUCCESS;
1747}
1748
1749
1750/**
1751 * Handles writes to IQT_REG.
1752 *
1753 * @returns Strict VBox status code.
1754 * @param pDevIns The IOMMU device instance.
1755 * @param offReg The MMIO register offset.
1756 * @param uIqtReg The value written to IQT_REG.
1757 */
1758static VBOXSTRICTRC dmarIqtRegWrite(PPDMDEVINS pDevIns, uint16_t offReg, uint64_t uIqtReg)
1759{
1760 /* We only care about the low 32-bits, high 32-bits are reserved. */
1761 Assert(offReg == VTD_MMIO_OFF_IQT_REG);
1762 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1763
1764 /* Paranoia. */
1765 Assert(!(uIqtReg & ~VTD_BF_IQT_REG_QT_MASK));
1766
1767 uint32_t const offQt = uIqtReg;
1768 uint64_t const uIqaReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_IQA_REG);
1769 uint8_t const fDw = RT_BF_GET(uIqaReg, VTD_BF_IQA_REG_DW);
1770
1771 /* If the descriptor width is 256-bits, the queue tail offset must be aligned accordingly. */
1772 if ( fDw != VTD_IQA_REG_DW_256_BIT
1773 || !(offQt & RT_BIT(4)))
1774 dmarInvQueueThreadWakeUpIfNeeded(pDevIns);
1775 else
1776 {
1777 /* Hardware treats bit 4 as RsvdZ in this situation, so clear it. */
1778 dmarRegChangeRaw32(pThis, offReg, ~RT_BIT(4), 0 /* fOrMask */);
1779 dmarIqeFaultRecord(pDevIns, kDmarDiag_IqtReg_Qt_NotAligned, VTDIQEI_QUEUE_TAIL_MISALIGNED);
1780 }
1781 return VINF_SUCCESS;
1782}
1783
1784
1785/**
1786 * Handles writes to IQA_REG.
1787 *
1788 * @returns Strict VBox status code.
1789 * @param pDevIns The IOMMU device instance.
1790 * @param offReg The MMIO register offset.
1791 * @param uIqaReg The value written to IQA_REG.
1792 */
1793static VBOXSTRICTRC dmarIqaRegWrite(PPDMDEVINS pDevIns, uint16_t offReg, uint64_t uIqaReg)
1794{
1795 /* At present, we only care about the low 32-bits, high 32-bits are data. */
1796 Assert(offReg == VTD_MMIO_OFF_IQA_REG); NOREF(offReg);
1797
1798 /** @todo What happens if IQA_REG is written when dmarInvQueueCanProcessRequests
1799 * returns true? The Intel VT-d spec. doesn't state anywhere that it
1800 * cannot happen or that it's ignored when it does happen. */
1801
1802 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1803 uint8_t const fDw = RT_BF_GET(uIqaReg, VTD_BF_IQA_REG_DW);
1804 if (fDw == VTD_IQA_REG_DW_256_BIT)
1805 {
1806 bool const fSupports256BitDw = (pThis->fExtCapReg & (VTD_BF_ECAP_REG_SMTS_MASK | VTD_BF_ECAP_REG_ADMS_MASK));
1807 if (fSupports256BitDw)
1808 { /* likely */ }
1809 else
1810 dmarIqeFaultRecord(pDevIns, kDmarDiag_IqaReg_Dw_256_Invalid, VTDIQEI_INVALID_DESCRIPTOR_WIDTH);
1811 }
1812 /* else: 128-bit descriptor width is validated lazily, see explanation in dmarR3InvQueueProcessRequests. */
1813
1814 return VINF_SUCCESS;
1815}
1816
1817
1818/**
1819 * Handles writes to ICS_REG.
1820 *
1821 * @returns Strict VBox status code.
1822 * @param pDevIns The IOMMU device instance.
1823 * @param uIcsReg The value written to ICS_REG.
1824 */
1825static VBOXSTRICTRC dmarIcsRegWrite(PPDMDEVINS pDevIns, uint32_t uIcsReg)
1826{
1827 /*
1828 * If the IP field is set when software services the interrupt condition,
1829 * (by clearing the IWC field), the IP field must be cleared.
1830 */
1831 if (!(uIcsReg & VTD_BF_ICS_REG_IWC_MASK))
1832 {
1833 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1834 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_IECTL_REG, ~VTD_BF_IECTL_REG_IP_MASK, 0 /* fOrMask */);
1835 }
1836 return VINF_SUCCESS;
1837}
1838
1839
1840/**
1841 * Handles writes to IECTL_REG.
1842 *
1843 * @returns Strict VBox status code.
1844 * @param pDevIns The IOMMU device instance.
1845 * @param uIectlReg The value written to IECTL_REG.
1846 */
1847static VBOXSTRICTRC dmarIectlRegWrite(PPDMDEVINS pDevIns, uint32_t uIectlReg)
1848{
1849 /*
1850 * If software unmasks the interrupt when the interrupt is pending, we must raise
1851 * the interrupt now (which will consequently clear the interrupt pending (IP) bit).
1852 */
1853 if ( (uIectlReg & VTD_BF_IECTL_REG_IP_MASK)
1854 && ~(uIectlReg & VTD_BF_IECTL_REG_IM_MASK))
1855 dmarEventRaiseInterrupt(pDevIns, DMAREVENTTYPE_INV_COMPLETE);
1856 return VINF_SUCCESS;
1857}
1858
1859
1860/**
1861 * Handles writes to FRCD_REG (High 64-bits).
1862 *
1863 * @returns Strict VBox status code.
1864 * @param pDevIns The IOMMU device instance.
1865 * @param offReg The MMIO register offset.
1866 * @param cbReg The size of the MMIO access (in bytes).
1867 * @param uFrcdHiReg The value written to FRCD_REG.
1868 * @param uPrev The value in FRCD_REG prior to writing it.
1869 */
1870static VBOXSTRICTRC dmarFrcdHiRegWrite(PPDMDEVINS pDevIns, uint16_t offReg, uint8_t cbReg, uint64_t uFrcdHiReg, uint64_t uPrev)
1871{
1872 /* We only care about responding to high 32-bits, low 32-bits are read-only. */
1873 if (offReg + cbReg > DMAR_MMIO_OFF_FRCD_HI_REG + 4)
1874 {
1875 /*
1876 * If software cleared the RW1C F (fault) bit in all FRCD_REGs, hardware clears the
1877 * Primary Pending Fault (PPF) and the interrupt pending (IP) bits. Our implementation
1878 * has only 1 FRCD register.
1879 *
1880 * See Intel VT-d spec. 10.4.10 "Fault Event Control Register".
1881 */
1882 AssertCompile(DMAR_FRCD_REG_COUNT == 1);
1883 uint64_t const fChanged = uPrev ^ uFrcdHiReg;
1884 if (fChanged & VTD_BF_1_FRCD_REG_F_MASK)
1885 {
1886 Assert(!(uFrcdHiReg & VTD_BF_1_FRCD_REG_F_MASK)); /* Software should only ever be able to clear this bit. */
1887 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
1888 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_FSTS_REG, ~VTD_BF_FSTS_REG_PPF_MASK, 0 /* fOrMask */);
1889 dmarRegChangeRaw32(pThis, VTD_MMIO_OFF_FECTL_REG, ~VTD_BF_FECTL_REG_IP_MASK, 0 /* fOrMask */);
1890 }
1891 }
1892 return VINF_SUCCESS;
1893}
1894
1895
1896/**
1897 * Performs a PCI target abort for a DMA remapping (DR) operation.
1898 *
1899 * @param pDevIns The IOMMU device instance.
1900 */
1901static void dmarDrTargetAbort(PPDMDEVINS pDevIns)
1902{
1903 /** @todo r=ramshankar: I don't know for sure if a PCI target abort is caused or not
1904 * as the Intel VT-d spec. is vague. Wording seems to suggest it does, but
1905 * who knows. */
1906 PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
1907 uint16_t const u16Status = PDMPciDevGetStatus(pPciDev) | VBOX_PCI_STATUS_SIG_TARGET_ABORT;
1908 PDMPciDevSetStatus(pPciDev, u16Status);
1909}
1910
1911
1912/**
1913 * Checks whether the address width (AW) is supported by our hardware
1914 * implementation for legacy mode address translation.
1915 *
1916 * @returns @c true if it's supported, @c false otherwise.
1917 * @param pThis The shared DMAR device state.
1918 * @param pCtxEntry The context entry.
1919 * @param pcPagingLevel Where to store the paging level. Optional, can be
1920 * NULL.
1921 */
1922static bool dmarDrLegacyModeIsAwValid(PCDMAR pThis, PCVTD_CONTEXT_ENTRY_T pCtxEntry, uint8_t *pcPagingLevel)
1923{
1924 uint8_t const fTt = RT_BF_GET(pCtxEntry->au64[0], VTD_BF_0_CONTEXT_ENTRY_TT);
1925 uint8_t const fAw = RT_BF_GET(pCtxEntry->au64[1], VTD_BF_1_CONTEXT_ENTRY_AW);
1926 uint8_t const fAwMask = RT_BIT(fAw);
1927 uint8_t const fSagaw = RT_BF_GET(pThis->fCapReg, VTD_BF_CAP_REG_SAGAW);
1928 Assert(!(fSagaw & ~(RT_BIT(1) | RT_BIT(2) | RT_BIT(3))));
1929
1930 uint8_t const cPagingLevel = fAw + 2;
1931 if (pcPagingLevel)
1932 *pcPagingLevel = cPagingLevel;
1933
1934 /* With pass-through, the address width must be the largest AGAW supported by hardware. */
1935 if (fTt == VTD_TT_UNTRANSLATED_PT)
1936 {
1937 Assert(pThis->cMaxPagingLevel >= 3 && pThis->cMaxPagingLevel <= 5); /* Paranoia. */
1938 return cPagingLevel == pThis->cMaxPagingLevel;
1939 }
1940
1941 /* The address width must be any of the ones supported by hardware. */
1942 if (fAw < 4)
1943 return (fSagaw & fAwMask) != 0;
1944
1945 return false;
1946}
1947
1948
1949/**
1950 * Reads a root entry from guest memory.
1951 *
1952 * @returns VBox status code.
1953 * @param pDevIns The IOMMU device instance.
1954 * @param uRtaddrReg The current RTADDR_REG value.
1955 * @param idxRootEntry The index of the root entry to read.
1956 * @param pRootEntry Where to store the read root entry.
1957 */
1958static int dmarDrReadRootEntry(PPDMDEVINS pDevIns, uint64_t uRtaddrReg, uint8_t idxRootEntry, PVTD_ROOT_ENTRY_T pRootEntry)
1959{
1960 size_t const cbRootEntry = sizeof(*pRootEntry);
1961 RTGCPHYS const GCPhysRootEntry = (uRtaddrReg & VTD_BF_RTADDR_REG_RTA_MASK) + (idxRootEntry * cbRootEntry);
1962 return PDMDevHlpPhysReadMeta(pDevIns, GCPhysRootEntry, pRootEntry, cbRootEntry);
1963}
1964
1965
1966/**
1967 * Reads a context entry from guest memory.
1968 *
1969 * @returns VBox status code.
1970 * @param pDevIns The IOMMU device instance.
1971 * @param GCPhysCtxTable The physical address of the context table.
1972 * @param idxCtxEntry The index of the context entry to read.
1973 * @param pCtxEntry Where to store the read context entry.
1974 */
1975static int dmarDrReadCtxEntry(PPDMDEVINS pDevIns, RTGCPHYS GCPhysCtxTable, uint8_t idxCtxEntry, PVTD_CONTEXT_ENTRY_T pCtxEntry)
1976{
1977 /* We don't verify bits 63:HAW of GCPhysCtxTable is 0 since reading from such an address should fail anyway. */
1978 size_t const cbCtxEntry = sizeof(*pCtxEntry);
1979 RTGCPHYS const GCPhysCtxEntry = GCPhysCtxTable + (idxCtxEntry * cbCtxEntry);
1980 return PDMDevHlpPhysReadMeta(pDevIns, GCPhysCtxEntry, pCtxEntry, cbCtxEntry);
1981}
1982
1983
1984/**
1985 * Reads a second-level paging entry from guest memory.
1986 *
1987 * @returns VBox status code.
1988 * @param pDevIns The IOMMU device instance.
1989 * @param GCPhysSlptPtr The physical address of the SLPTPTR.
1990 * @param pSlpEntry Where to store the read SLPTPTR.
1991 */
1992static int dmarDrReadSlpPtr(PPDMDEVINS pDevIns, RTGCPHYS GCPhysSlptPtr, PVTD_SLP_ENTRY_T pSlpEntry)
1993{
1994 /* We don't verify bits 63:HAW of GCPhysSlptPtr is 0 since reading from such an address should fail anyway. */
1995 return PDMDevHlpPhysReadMeta(pDevIns, GCPhysSlptPtr, pSlpEntry, sizeof(*pSlpEntry));
1996}
1997
1998
1999/**
2000 * Validates the output address of a translation and updates the IOTLB entry for the
2001 * given memory request remapping.
2002 *
2003 * @returns VBox status code.
2004 * @param pDevIns The IOMMU device instance.
2005 * @param GCPhysBase The output address of the translation.
2006 * @param cShift The page shift of the translation.
2007 * @param fPerm The permissions granted for the translated.
2008 * @param idDomain The domain ID of the translated region.
2009 * @param pMemReqRemap The DMA memory request remapping info.
2010 */
2011static int dmarDrValidateAndUpdateIotlbe(PPDMDEVINS pDevIns, RTGCPHYS GCPhysBase, uint8_t cShift, uint8_t fPerm,
2012 uint16_t idDomain, PDMARMEMREQREMAP pMemReqRemap)
2013{
2014 Assert( pMemReqRemap->fTtm == VTD_TTM_LEGACY_MODE
2015 || pMemReqRemap->fTtm == VTD_TTM_SCALABLE_MODE);
2016 Assert(!(GCPhysBase & X86_PAGE_4K_OFFSET_MASK));
2017
2018 /* Ensure the output address is not in the interrupt address range. */
2019 if (GCPhysBase - VBOX_MSI_ADDR_BASE >= VBOX_MSI_ADDR_SIZE)
2020 {
2021 pMemReqRemap->Iotlbe.GCPhysBase = GCPhysBase;
2022 pMemReqRemap->Iotlbe.cShift = cShift;
2023 pMemReqRemap->Iotlbe.fPerm = fPerm;
2024 pMemReqRemap->Iotlbe.idDomain = idDomain;
2025 return VINF_SUCCESS;
2026 }
2027
2028 if (pMemReqRemap->fTtm == VTD_TTM_LEGACY_MODE)
2029 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lgn_4, VTDATFAULT_LGN_4, pMemReqRemap);
2030 else
2031 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Sgn_8, VTDATFAULT_SGN_8, pMemReqRemap);
2032 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
2033}
2034
2035
2036/**
2037 * Performs second level translation.
2038 *
2039 * @returns VBox status code.
2040 * @param pDevIns The IOMMU device instance.
2041 * @param SlpEntry The second-level paging entry.
2042 * @param cPagingLevel The paging level.
2043 * @param idDomain The domain ID for the translation.
2044 * @param pMemReqRemap The DMA memory request remapping info.
2045 */
2046static int dmarDrSecondLevelTranslate(PPDMDEVINS pDevIns, VTD_SLP_ENTRY_T SlpEntry, uint8_t cPagingLevel, uint16_t idDomain,
2047 PDMARMEMREQREMAP pMemReqRemap)
2048{
2049 PCDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PCDMAR);
2050
2051 /* Mask of valid paging entry bits. */
2052 static uint64_t const s_auPtEntityRsvd[] = { VTD_SL_PTE_VALID_MASK,
2053 VTD_SL_PDE_VALID_MASK,
2054 VTD_SL_PDPE_VALID_MASK,
2055 VTD_SL_PML4E_VALID_MASK,
2056 VTD_SL_PML5E_VALID_MASK };
2057
2058 /* Mask of valid large-page (1GB, 2MB) paging entries. */
2059 static uint64_t const s_auLargePageRsvd[] = { 0,
2060 VTD_SL_PDE2M_VALID_MASK,
2061 VTD_SL_PDPE1G_VALID_MASK,
2062 0,
2063 0 };
2064
2065 /* Paranoia. */
2066 Assert(cPagingLevel >= 3 && cPagingLevel <= 5);
2067 AssertCompile(RT_ELEMENTS(s_auPtEntityRsvd) == RT_ELEMENTS(s_auLargePageRsvd));
2068 AssertCompile(RT_ELEMENTS(s_auPtEntityRsvd) == 5);
2069
2070 /* Second-level translations restricts input address to an implementation-specific MGAW. */
2071 uint64_t const uDmaAddr = pMemReqRemap->Req.uDmaAddr;
2072 if (!(uDmaAddr & pThis->fInvMgawMask))
2073 { /* likely */ }
2074 else
2075 {
2076 if (pMemReqRemap->fTtm == VTD_TTM_LEGACY_MODE)
2077 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lgn_1_1, VTDATFAULT_LGN_1_1, pMemReqRemap);
2078 else
2079 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Sgn_5, VTDATFAULT_SGN_5, pMemReqRemap);
2080 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
2081 }
2082
2083 /*
2084 * Traverse the I/O page table starting with the SLPTPTR (second-level page table pointer).
2085 * Unlike AMD IOMMU paging, here there is no feature for "skipping" levels.
2086 */
2087 uint64_t uPtEntity = SlpEntry;
2088 for (int8_t iLevel = cPagingLevel - 1; iLevel >= 0; iLevel--)
2089 {
2090 /*
2091 * Read the paging entry for the current level.
2092 */
2093 uint8_t const cLevelShift = 12 + ((iLevel - 1) * 9);
2094 {
2095 uint16_t const idxPte = (uDmaAddr >> cLevelShift) & UINT64_C(0x1ff);
2096 uint64_t const offPte = idxPte << 3;
2097 RTGCPHYS const GCPhysPtEntity = uPtEntity | offPte;
2098 int const rc = PDMDevHlpPhysReadMeta(pDevIns, GCPhysPtEntity, &uPtEntity, sizeof(uPtEntity));
2099 if (RT_SUCCESS(rc))
2100 { /* likely */ }
2101 else
2102 {
2103 if (pMemReqRemap->fTtm == VTD_TTM_LEGACY_MODE)
2104 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lsl_1, VTDATFAULT_LSL_1, pMemReqRemap);
2105 else
2106 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Ssl_1, VTDATFAULT_SSL_1, pMemReqRemap);
2107 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
2108 }
2109 }
2110
2111 /*
2112 * Check I/O permissions.
2113 * This must be done prior to check reserved bits for properly reporting errors SSL.2 and SSL.3.
2114 *
2115 * See Intel spec. 7.1.3 "Fault conditions and Remapping hardware behavior for various request".
2116 */
2117 uint8_t const fReqPerm = pMemReqRemap->Req.fReqPerm & pThis->fPermValidMask;
2118 uint8_t const fPtPerm = uPtEntity & pThis->fPermValidMask;
2119 if ((fPtPerm & fReqPerm) == fReqPerm)
2120 { /* likely */ }
2121 else
2122 {
2123 if (pMemReqRemap->fTtm == VTD_TTM_LEGACY_MODE)
2124 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lsl_2, VTDATFAULT_LSL_2, pMemReqRemap);
2125 else
2126 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Ssl_2, VTDATFAULT_SSL_2, pMemReqRemap);
2127 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
2128 }
2129
2130 /*
2131 * Validate reserved bits of the current paging entry.
2132 */
2133 if (!(uPtEntity & ~s_auPtEntityRsvd[iLevel]))
2134 { /* likely */ }
2135 else
2136 {
2137 if (pMemReqRemap->fTtm == VTD_TTM_LEGACY_MODE)
2138 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lsl_2, VTDATFAULT_LSL_2, pMemReqRemap);
2139 else
2140 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Ssl_3, VTDATFAULT_SSL_3, pMemReqRemap);
2141 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
2142 }
2143
2144 /*
2145 * Check if this is a 1GB page or a 2MB page.
2146 */
2147 AssertCompile(VTD_BF_SL_PDE_PS_MASK == VTD_BF_SL_PDPE_PS_MASK);
2148 uint8_t const fLargePage = RT_BF_GET(uPtEntity, VTD_BF_SL_PDE_PS);
2149 if (fLargePage && iLevel > 0)
2150 {
2151 Assert(iLevel == 1 || iLevel == 2);
2152 uint8_t const fSllpsMask = RT_BF_GET(pThis->fCapReg, VTD_BF_CAP_REG_SLLPS);
2153 if (fSllpsMask & RT_BIT(iLevel - 1))
2154 {
2155 RTGCPHYS const GCPhysBase = uPtEntity & ~(RT_BIT_64(cLevelShift) - 1);
2156 return dmarDrValidateAndUpdateIotlbe(pDevIns, GCPhysBase, cLevelShift, fPtPerm, idDomain, pMemReqRemap);
2157 }
2158
2159 if (pMemReqRemap->fTtm == VTD_TTM_LEGACY_MODE)
2160 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lsl_2_LargePage, VTDATFAULT_LSL_2, pMemReqRemap);
2161 else
2162 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Ssl_3_LargePage, VTDATFAULT_SSL_3, pMemReqRemap);
2163 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
2164 }
2165
2166 /*
2167 * If this is the final PTE, compute the translation address and we're done.
2168 */
2169 if (iLevel == 0)
2170 {
2171 RTGCPHYS const GCPhysBase = uPtEntity & ~(RT_BIT_64(cLevelShift) - 1);
2172 return dmarDrValidateAndUpdateIotlbe(pDevIns, GCPhysBase, cLevelShift, fPtPerm, idDomain, pMemReqRemap);
2173 }
2174 }
2175
2176 /* Shouldn't ever reach here. */
2177 return VERR_IOMMU_IPE_0;
2178}
2179
2180
2181/**
2182 * Handles remapping of DMA address requests in legacy mode.
2183 *
2184 * @returns VBox status code.
2185 * @param pDevIns The IOMMU device instance.
2186 * @param uRtaddrReg The current RTADDR_REG value.
2187 * @param pMemReqRemap The DMA memory request remapping info.
2188 */
2189static int dmarDrLegacyModeRemapAddr(PPDMDEVINS pDevIns, uint64_t uRtaddrReg, PDMARMEMREQREMAP pMemReqRemap)
2190{
2191 Assert(pMemReqRemap->fTtm == VTD_TTM_LEGACY_MODE); /* Paranoia. */
2192
2193 /* Read the root-entry from guest memory. */
2194 uint8_t const idxRootEntry = RT_HI_U8(pMemReqRemap->Req.idDevice);
2195 VTD_ROOT_ENTRY_T RootEntry;
2196 int rc = dmarDrReadRootEntry(pDevIns, uRtaddrReg, idxRootEntry, &RootEntry);
2197 if (RT_SUCCESS(rc))
2198 {
2199 /* Check if the root entry is present (must be done before validating reserved bits). */
2200 uint64_t const uRootEntryQword0 = RootEntry.au64[0];
2201 uint64_t const uRootEntryQword1 = RootEntry.au64[1];
2202 bool const fRootEntryPresent = RT_BF_GET(uRootEntryQword0, VTD_BF_0_ROOT_ENTRY_P);
2203 if (fRootEntryPresent)
2204 {
2205 /* Validate reserved bits in the root entry. */
2206 if ( !(uRootEntryQword0 & ~VTD_ROOT_ENTRY_0_VALID_MASK)
2207 && !(uRootEntryQword1 & ~VTD_ROOT_ENTRY_1_VALID_MASK))
2208 {
2209 /* Read the context-entry from guest memory. */
2210 RTGCPHYS const GCPhysCtxTable = RT_BF_GET(uRootEntryQword0, VTD_BF_0_ROOT_ENTRY_CTP);
2211 uint8_t const idxCtxEntry = RT_LO_U8(pMemReqRemap->Req.idDevice);
2212 VTD_CONTEXT_ENTRY_T CtxEntry;
2213 rc = dmarDrReadCtxEntry(pDevIns, GCPhysCtxTable, idxCtxEntry, &CtxEntry);
2214 if (RT_SUCCESS(rc))
2215 {
2216 uint64_t const uCtxEntryQword0 = CtxEntry.au64[0];
2217 uint64_t const uCtxEntryQword1 = CtxEntry.au64[1];
2218
2219 /* Note the FPD bit which software can use to supress translation faults from here on in. */
2220 pMemReqRemap->fFpd = RT_BF_GET(uCtxEntryQword0, VTD_BF_0_CONTEXT_ENTRY_FPD);
2221
2222 /* Check if the context-entry is present (must be done before validating reserved bits). */
2223 bool const fCtxEntryPresent = RT_BF_GET(uCtxEntryQword0, VTD_BF_0_CONTEXT_ENTRY_P);
2224 if (fCtxEntryPresent)
2225 {
2226 /* Validate reserved bits in the context-entry. */
2227 if ( !(uCtxEntryQword0 & ~VTD_CONTEXT_ENTRY_0_VALID_MASK)
2228 && !(uCtxEntryQword1 & ~VTD_CONTEXT_ENTRY_1_VALID_MASK))
2229 {
2230 /* Get the domain ID for this mapping. */
2231 uint16_t const idDomain = RT_BF_GET(uCtxEntryQword1, VTD_BF_1_CONTEXT_ENTRY_DID);
2232
2233 /* Validate the translation type (TT). */
2234 PCDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PCDMAR);
2235 uint8_t const fTt = RT_BF_GET(uCtxEntryQword0, VTD_BF_0_CONTEXT_ENTRY_TT);
2236 switch (fTt)
2237 {
2238 case VTD_TT_UNTRANSLATED_SLP:
2239 {
2240 /*
2241 * Untranslated requests are translated using second-level paging structures referenced
2242 * through SLPTPTR. Translated requests and Translation Requests are blocked.
2243 */
2244 if (pMemReqRemap->Req.enmAddrType == PCIADDRTYPE_UNTRANSLATED)
2245 {
2246 /* Validate the address width and get the paging level. */
2247 uint8_t cPagingLevel;
2248 if (dmarDrLegacyModeIsAwValid(pThis, &CtxEntry, &cPagingLevel))
2249 {
2250 /* Read the SLPTPTR from guest memory. */
2251 RTGCPHYS const GCPhysSlptPtr = uCtxEntryQword0 & VTD_BF_0_CONTEXT_ENTRY_SLPTPTR_MASK;
2252 VTD_SLP_ENTRY_T SlpEntry;
2253 rc = dmarDrReadSlpPtr(pDevIns, GCPhysSlptPtr, &SlpEntry);
2254 if (RT_SUCCESS(rc))
2255 {
2256 /* Finally... perform second-level translation. */
2257 return dmarDrSecondLevelTranslate(pDevIns, SlpEntry, cPagingLevel, idDomain,
2258 pMemReqRemap);
2259 }
2260 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lct_4_3, VTDATFAULT_LCT_4_3, pMemReqRemap);
2261 }
2262 else
2263 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lct_4_1, VTDATFAULT_LCT_4_1, pMemReqRemap);
2264 }
2265 else
2266 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lct_5, VTDATFAULT_LCT_5, pMemReqRemap);
2267 break;
2268 }
2269
2270 case VTD_TT_UNTRANSLATED_PT:
2271 {
2272 /*
2273 * Untranslated requests are processed as pass-through (PT) if PT is supported.
2274 * Translated and translation requests are blocked. If PT isn't supported this TT value
2275 * is reserved which I assume raises a fault (hence fallthru below).
2276 */
2277 if (pThis->fExtCapReg & VTD_BF_ECAP_REG_PT_MASK)
2278 {
2279 if (pMemReqRemap->Req.enmAddrType == PCIADDRTYPE_UNTRANSLATED)
2280 {
2281 /** @todo Check AW == maximum SAGAW bit? */
2282 pMemReqRemap->Iotlbe.GCPhysBase = pMemReqRemap->Req.uDmaAddr & X86_PAGE_4K_BASE_MASK;
2283 pMemReqRemap->Iotlbe.cShift = X86_PAGE_4K_SHIFT;
2284 pMemReqRemap->Iotlbe.fPerm = DMAR_PERM_ALL;
2285 pMemReqRemap->Iotlbe.idDomain = idDomain;
2286 pMemReqRemap->cbContiguous = pMemReqRemap->Req.cbDma;
2287 return VINF_SUCCESS;
2288 }
2289 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lct_5, VTDATFAULT_LCT_5, pMemReqRemap);
2290 break;
2291 }
2292 RT_FALL_THRU();
2293 }
2294
2295 case VTD_TT_UNTRANSLATED_DEV_TLB:
2296 {
2297 /*
2298 * Untranslated, translated and translation requests are supported but requires
2299 * device-TLB support. We don't support device-TLBs, so it's treated as reserved.
2300 */
2301 Assert(!(pThis->fExtCapReg & VTD_BF_ECAP_REG_DT_MASK));
2302 RT_FALL_THRU();
2303 }
2304
2305 default:
2306 {
2307 /* Any other TT value is reserved. */
2308 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lct_4_2, VTDATFAULT_LCT_4_2, pMemReqRemap);
2309 break;
2310 }
2311 }
2312 }
2313 else
2314 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lct_3, VTDATFAULT_LCT_3, pMemReqRemap);
2315 }
2316 else
2317 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lct_2, VTDATFAULT_LCT_2, pMemReqRemap);
2318 }
2319 else
2320 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lct_1, VTDATFAULT_LCT_1, pMemReqRemap);
2321 }
2322 else
2323 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lrt_3, VTDATFAULT_LRT_3, pMemReqRemap);
2324 }
2325 else
2326 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lrt_2, VTDATFAULT_LRT_2, pMemReqRemap);
2327 }
2328 else
2329 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Lrt_1, VTDATFAULT_LRT_1, pMemReqRemap);
2330 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
2331}
2332
2333
2334/**
2335 * Handles remapping of DMA address requests in scalable mode.
2336 *
2337 * @returns VBox status code.
2338 * @param pDevIns The IOMMU device instance.
2339 * @param uRtaddrReg The current RTADDR_REG value.
2340 * @param pMemReqRemap The DMA memory request remapping info.
2341 */
2342static int dmarDrScalableModeRemapAddr(PPDMDEVINS pDevIns, uint64_t uRtaddrReg, PDMARMEMREQREMAP pMemReqRemap)
2343{
2344 PCDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
2345 if (pThis->fExtCapReg & VTD_BF_ECAP_REG_SMTS_MASK)
2346 {
2347 RT_NOREF1(uRtaddrReg);
2348 return VERR_NOT_IMPLEMENTED;
2349 }
2350
2351 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Rta_1_3, VTDATFAULT_RTA_1_3, pMemReqRemap);
2352 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
2353}
2354
2355
2356/**
2357 * Memory access bulk (one or more 4K pages) request from a device.
2358 *
2359 * @returns VBox status code.
2360 * @param pDevIns The IOMMU device instance.
2361 * @param idDevice The device ID (bus, device, function).
2362 * @param cIovas The number of addresses being accessed.
2363 * @param pauIovas The I/O virtual addresses for each page being accessed.
2364 * @param fFlags The access flags, see PDMIOMMU_MEM_F_XXX.
2365 * @param paGCPhysSpa Where to store the translated physical addresses.
2366 *
2367 * @thread Any.
2368 */
2369static DECLCALLBACK(int) iommuIntelMemBulkAccess(PPDMDEVINS pDevIns, uint16_t idDevice, size_t cIovas, uint64_t const *pauIovas,
2370 uint32_t fFlags, PRTGCPHYS paGCPhysSpa)
2371{
2372 RT_NOREF6(pDevIns, idDevice, cIovas, pauIovas, fFlags, paGCPhysSpa);
2373 return VERR_NOT_IMPLEMENTED;
2374}
2375
2376
2377/**
2378 * Memory access transaction from a device.
2379 *
2380 * @returns VBox status code.
2381 * @param pDevIns The IOMMU device instance.
2382 * @param idDevice The device ID (bus, device, function).
2383 * @param uIova The I/O virtual address being accessed.
2384 * @param cbIova The size of the access.
2385 * @param fFlags The access flags, see PDMIOMMU_MEM_F_XXX.
2386 * @param pGCPhysSpa Where to store the translated system physical address.
2387 * @param pcbContiguous Where to store the number of contiguous bytes translated
2388 * and permission-checked.
2389 *
2390 * @thread Any.
2391 */
2392static DECLCALLBACK(int) iommuIntelMemAccess(PPDMDEVINS pDevIns, uint16_t idDevice, uint64_t uIova, size_t cbIova,
2393 uint32_t fFlags, PRTGCPHYS pGCPhysSpa, size_t *pcbContiguous)
2394{
2395 /* Validate. */
2396 AssertPtr(pDevIns);
2397 AssertPtr(pGCPhysSpa);
2398 AssertPtr(pcbContiguous);
2399 Assert(cbIova > 0); /** @todo Are we going to support ZLR (zero-length reads to write-only pages)? */
2400 Assert(!(fFlags & ~PDMIOMMU_MEM_F_VALID_MASK));
2401
2402 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
2403 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
2404
2405 DMAR_LOCK(pDevIns, pThisCC);
2406 uint32_t const uGstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_GSTS_REG);
2407 uint64_t const uRtaddrReg = pThis->uRtaddrReg;
2408 DMAR_UNLOCK(pDevIns, pThisCC);
2409
2410 if (uGstsReg & VTD_BF_GSTS_REG_TES_MASK)
2411 {
2412 VTDREQTYPE enmReqType;
2413 uint8_t fReqPerm;
2414 if (fFlags & PDMIOMMU_MEM_F_READ)
2415 {
2416 enmReqType = VTDREQTYPE_READ;
2417 fReqPerm = DMAR_PERM_READ;
2418 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMemRead));
2419 }
2420 else
2421 {
2422 enmReqType = VTDREQTYPE_WRITE;
2423 fReqPerm = DMAR_PERM_WRITE;
2424 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMemWrite));
2425 }
2426
2427 uint8_t const fTtm = RT_BF_GET(uRtaddrReg, VTD_BF_RTADDR_REG_TTM);
2428 DMARMEMREQREMAP MemReqRemap;
2429 RT_ZERO(MemReqRemap);
2430 MemReqRemap.Req.uDmaAddr = uIova;
2431 MemReqRemap.Req.cbDma = cbIova;
2432 MemReqRemap.Req.fReqPerm = fReqPerm;
2433 MemReqRemap.Req.idDevice = idDevice;
2434 MemReqRemap.Req.Pasid = NIL_PCIPASID;
2435 MemReqRemap.Req.enmAddrType = PCIADDRTYPE_UNTRANSLATED;
2436 MemReqRemap.Req.enmReqType = enmReqType;
2437 MemReqRemap.fTtm = fTtm;
2438 MemReqRemap.Iotlbe.GCPhysBase = NIL_RTGCPHYS;
2439
2440 int rc;
2441 switch (fTtm)
2442 {
2443 case VTD_TTM_LEGACY_MODE:
2444 {
2445 rc = dmarDrLegacyModeRemapAddr(pDevIns, uRtaddrReg, &MemReqRemap);
2446 break;
2447 }
2448
2449 case VTD_TTM_SCALABLE_MODE:
2450 {
2451 rc = dmarDrScalableModeRemapAddr(pDevIns, uRtaddrReg, &MemReqRemap);
2452 break;
2453 }
2454
2455 case VTD_TTM_ABORT_DMA_MODE:
2456 {
2457 rc = VERR_IOMMU_ADDR_TRANSLATION_FAILED;
2458 if (pThis->fExtCapReg & VTD_BF_ECAP_REG_ADMS_MASK)
2459 dmarDrTargetAbort(pDevIns);
2460 else
2461 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Rta_1_1, VTDATFAULT_RTA_1_1, &MemReqRemap);
2462 break;
2463 }
2464
2465 default:
2466 {
2467 rc = VERR_IOMMU_ADDR_TRANSLATION_FAILED;
2468 dmarAtFaultRecord(pDevIns, kDmarDiag_Atf_Rta_1_2, VTDATFAULT_RTA_1_2, &MemReqRemap);
2469 break;
2470 }
2471 }
2472
2473 *pcbContiguous = MemReqRemap.cbContiguous;
2474 *pGCPhysSpa = MemReqRemap.Iotlbe.GCPhysBase | DMAR_GET_PAGE_OFF_MASK(MemReqRemap.Iotlbe.cShift);
2475 return rc;
2476 }
2477
2478 *pGCPhysSpa = uIova;
2479 *pcbContiguous = cbIova;
2480 return VINF_SUCCESS;
2481}
2482
2483
2484/**
2485 * Reads an IRTE from guest memory.
2486 *
2487 * @returns VBox status code.
2488 * @param pDevIns The IOMMU device instance.
2489 * @param uIrtaReg The IRTA_REG.
2490 * @param idxIntr The interrupt index.
2491 * @param pIrte Where to store the read IRTE.
2492 */
2493static int dmarIrReadIrte(PPDMDEVINS pDevIns, uint64_t uIrtaReg, uint16_t idxIntr, PVTD_IRTE_T pIrte)
2494{
2495 Assert(idxIntr < VTD_IRTA_REG_GET_ENTRY_COUNT(uIrtaReg));
2496
2497 size_t const cbIrte = sizeof(*pIrte);
2498 RTGCPHYS const GCPhysIrte = (uIrtaReg & VTD_BF_IRTA_REG_IRTA_MASK) + (idxIntr * cbIrte);
2499 return PDMDevHlpPhysReadMeta(pDevIns, GCPhysIrte, pIrte, cbIrte);
2500}
2501
2502
2503/**
2504 * Remaps the source MSI to the destination MSI given the IRTE.
2505 *
2506 * @param fExtIntrMode Whether extended interrupt mode is enabled (i.e
2507 * IRTA_REG.EIME).
2508 * @param pIrte The IRTE used for the remapping.
2509 * @param pMsiIn The source MSI (currently unused).
2510 * @param pMsiOut Where to store the remapped MSI.
2511 */
2512static void dmarIrRemapFromIrte(bool fExtIntrMode, PCVTD_IRTE_T pIrte, PCMSIMSG pMsiIn, PMSIMSG pMsiOut)
2513{
2514 NOREF(pMsiIn);
2515 uint64_t const uIrteQword0 = pIrte->au64[0];
2516
2517 /*
2518 * Let's start with a clean slate and preserve unspecified bits if the need arises.
2519 * For instance, address bits 1:0 is supposed to be "ignored" by remapping hardware,
2520 * but it's not clear if hardware zeroes out these bits in the remapped MSI or if
2521 * it copies it from the source MSI.
2522 */
2523 RT_ZERO(*pMsiOut);
2524 pMsiOut->Addr.n.u1DestMode = RT_BF_GET(uIrteQword0, VTD_BF_0_IRTE_DM);
2525 pMsiOut->Addr.n.u1RedirHint = RT_BF_GET(uIrteQword0, VTD_BF_0_IRTE_RH);
2526 pMsiOut->Addr.n.u12Addr = VBOX_MSI_ADDR_BASE >> VBOX_MSI_ADDR_SHIFT;
2527 if (fExtIntrMode)
2528 {
2529 /*
2530 * Apparently the DMAR stuffs the high 24-bits of the destination ID into the
2531 * high 24-bits of the upper 32-bits of the message address, see @bugref{9967#c22}.
2532 */
2533 uint32_t const idDest = RT_BF_GET(uIrteQword0, VTD_BF_0_IRTE_DST);
2534 pMsiOut->Addr.n.u8DestId = idDest;
2535 pMsiOut->Addr.n.u32Rsvd0 = idDest & UINT32_C(0xffffff00);
2536 }
2537 else
2538 pMsiOut->Addr.n.u8DestId = RT_BF_GET(uIrteQword0, VTD_BF_0_IRTE_DST_XAPIC);
2539
2540 pMsiOut->Data.n.u8Vector = RT_BF_GET(uIrteQword0, VTD_BF_0_IRTE_V);
2541 pMsiOut->Data.n.u3DeliveryMode = RT_BF_GET(uIrteQword0, VTD_BF_0_IRTE_DLM);
2542 pMsiOut->Data.n.u1Level = 1;
2543 pMsiOut->Data.n.u1TriggerMode = RT_BF_GET(uIrteQword0, VTD_BF_0_IRTE_TM);
2544}
2545
2546
2547/**
2548 * Handles remapping of interrupts in remappable interrupt format.
2549 *
2550 * @returns VBox status code.
2551 * @param pDevIns The IOMMU device instance.
2552 * @param uIrtaReg The IRTA_REG.
2553 * @param idDevice The device ID (bus, device, function).
2554 * @param pMsiIn The source MSI.
2555 * @param pMsiOut Where to store the remapped MSI.
2556 */
2557static int dmarIrRemapIntr(PPDMDEVINS pDevIns, uint64_t uIrtaReg, uint16_t idDevice, PCMSIMSG pMsiIn, PMSIMSG pMsiOut)
2558{
2559 Assert(pMsiIn->Addr.dmar_remap.fIntrFormat == VTD_INTR_FORMAT_REMAPPABLE);
2560
2561 /* Validate reserved bits in the interrupt request. */
2562 AssertCompile(VTD_REMAPPABLE_MSI_ADDR_VALID_MASK == UINT32_MAX);
2563 if (!(pMsiIn->Data.u32 & ~VTD_REMAPPABLE_MSI_DATA_VALID_MASK))
2564 {
2565 /* Compute the index into the interrupt remap table. */
2566 uint16_t const uHandleHi = RT_BF_GET(pMsiIn->Addr.au32[0], VTD_BF_REMAPPABLE_MSI_ADDR_HANDLE_HI);
2567 uint16_t const uHandleLo = RT_BF_GET(pMsiIn->Addr.au32[0], VTD_BF_REMAPPABLE_MSI_ADDR_HANDLE_LO);
2568 uint16_t const uHandle = uHandleLo | (uHandleHi << 15);
2569 bool const fSubHandleValid = RT_BF_GET(pMsiIn->Addr.au32[0], VTD_BF_REMAPPABLE_MSI_ADDR_SHV);
2570 uint16_t const idxIntr = fSubHandleValid
2571 ? uHandle + RT_BF_GET(pMsiIn->Data.u32, VTD_BF_REMAPPABLE_MSI_DATA_SUBHANDLE)
2572 : uHandle;
2573
2574 /* Validate the index. */
2575 uint32_t const cEntries = VTD_IRTA_REG_GET_ENTRY_COUNT(uIrtaReg);
2576 if (idxIntr < cEntries)
2577 {
2578 /** @todo Implement and read IRTE from interrupt-entry cache here. */
2579
2580 /* Read the interrupt remap table entry (IRTE) at the index. */
2581 VTD_IRTE_T Irte;
2582 int rc = dmarIrReadIrte(pDevIns, uIrtaReg, idxIntr, &Irte);
2583 if (RT_SUCCESS(rc))
2584 {
2585 /* Check if the IRTE is present (this must be done -before- checking reserved bits). */
2586 uint64_t const uIrteQword0 = Irte.au64[0];
2587 uint64_t const uIrteQword1 = Irte.au64[1];
2588 bool const fPresent = RT_BF_GET(uIrteQword0, VTD_BF_0_IRTE_P);
2589 if (fPresent)
2590 {
2591 /* Validate reserved bits in the IRTE. */
2592 bool const fExtIntrMode = RT_BF_GET(uIrtaReg, VTD_BF_IRTA_REG_EIME);
2593 uint64_t const fQw0ValidMask = fExtIntrMode ? VTD_IRTE_0_X2APIC_VALID_MASK : VTD_IRTE_0_XAPIC_VALID_MASK;
2594 if ( !(uIrteQword0 & ~fQw0ValidMask)
2595 && !(uIrteQword1 & ~VTD_IRTE_1_VALID_MASK))
2596 {
2597 /* Validate requester id (the device ID) as configured in the IRTE. */
2598 bool fSrcValid;
2599 DMARDIAG enmIrDiag;
2600 uint8_t const fSvt = RT_BF_GET(uIrteQword1, VTD_BF_1_IRTE_SVT);
2601 switch (fSvt)
2602 {
2603 case VTD_IRTE_SVT_NONE:
2604 {
2605 fSrcValid = true;
2606 enmIrDiag = kDmarDiag_None;
2607 break;
2608 }
2609
2610 case VTD_IRTE_SVT_VALIDATE_MASK:
2611 {
2612 static uint16_t const s_afValidMasks[] = { 0xffff, 0xfffb, 0xfff9, 0xfff8 };
2613 uint8_t const idxMask = RT_BF_GET(uIrteQword1, VTD_BF_1_IRTE_SQ) & 3;
2614 uint16_t const fValidMask = s_afValidMasks[idxMask];
2615 uint16_t const idSource = RT_BF_GET(uIrteQword1, VTD_BF_1_IRTE_SID);
2616 fSrcValid = (idDevice & fValidMask) == (idSource & fValidMask);
2617 enmIrDiag = kDmarDiag_Ir_Rfi_Irte_Svt_Masked;
2618 break;
2619 }
2620
2621 case VTD_IRTE_SVT_VALIDATE_BUS_RANGE:
2622 {
2623 uint16_t const idSource = RT_BF_GET(uIrteQword1, VTD_BF_1_IRTE_SID);
2624 uint8_t const uBusFirst = RT_HI_U8(idSource);
2625 uint8_t const uBusLast = RT_LO_U8(idSource);
2626 uint8_t const idDeviceBus = idDevice >> VBOX_PCI_BUS_SHIFT;
2627 fSrcValid = (idDeviceBus >= uBusFirst && idDeviceBus <= uBusLast);
2628 enmIrDiag = kDmarDiag_Ir_Rfi_Irte_Svt_Bus;
2629 break;
2630 }
2631
2632 default:
2633 {
2634 fSrcValid = false;
2635 enmIrDiag = kDmarDiag_Ir_Rfi_Irte_Svt_Bus;
2636 break;
2637 }
2638 }
2639
2640 if (fSrcValid)
2641 {
2642 uint8_t const fPostedMode = RT_BF_GET(uIrteQword0, VTD_BF_0_IRTE_IM);
2643 if (!fPostedMode)
2644 {
2645 dmarIrRemapFromIrte(fExtIntrMode, &Irte, pMsiIn, pMsiOut);
2646 return VINF_SUCCESS;
2647 }
2648 dmarIrFaultRecordQualified(pDevIns, kDmarDiag_Ir_Rfi_Irte_Mode_Invalid,
2649 VTDIRFAULT_IRTE_PRESENT_RSVD, idDevice, idxIntr, &Irte);
2650 }
2651 else
2652 dmarIrFaultRecordQualified(pDevIns, enmIrDiag, VTDIRFAULT_IRTE_PRESENT_RSVD, idDevice, idxIntr,
2653 &Irte);
2654 }
2655 else
2656 dmarIrFaultRecordQualified(pDevIns, kDmarDiag_Ir_Rfi_Irte_Rsvd, VTDIRFAULT_IRTE_PRESENT_RSVD,
2657 idDevice, idxIntr, &Irte);
2658 }
2659 else
2660 dmarIrFaultRecordQualified(pDevIns, kDmarDiag_Ir_Rfi_Irte_Not_Present, VTDIRFAULT_IRTE_NOT_PRESENT,
2661 idDevice, idxIntr, &Irte);
2662 }
2663 else
2664 dmarIrFaultRecord(pDevIns, kDmarDiag_Ir_Rfi_Irte_Read_Failed, VTDIRFAULT_IRTE_READ_FAILED, idDevice, idxIntr);
2665 }
2666 else
2667 dmarIrFaultRecord(pDevIns, kDmarDiag_Ir_Rfi_Intr_Index_Invalid, VTDIRFAULT_INTR_INDEX_INVALID, idDevice, idxIntr);
2668 }
2669 else
2670 dmarIrFaultRecord(pDevIns, kDmarDiag_Ir_Rfi_Rsvd, VTDIRFAULT_REMAPPABLE_INTR_RSVD, idDevice, 0 /* idxIntr */);
2671 return VERR_IOMMU_INTR_REMAP_DENIED;
2672}
2673
2674
2675/**
2676 * Interrupt remap request from a device.
2677 *
2678 * @returns VBox status code.
2679 * @param pDevIns The IOMMU device instance.
2680 * @param idDevice The device ID (bus, device, function).
2681 * @param pMsiIn The source MSI.
2682 * @param pMsiOut Where to store the remapped MSI.
2683 */
2684static DECLCALLBACK(int) iommuIntelMsiRemap(PPDMDEVINS pDevIns, uint16_t idDevice, PCMSIMSG pMsiIn, PMSIMSG pMsiOut)
2685{
2686 /* Validate. */
2687 Assert(pDevIns);
2688 Assert(pMsiIn);
2689 Assert(pMsiOut);
2690 RT_NOREF1(idDevice);
2691
2692 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
2693 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
2694
2695 /* Lock and read all registers required for interrupt remapping up-front. */
2696 DMAR_LOCK(pDevIns, pThisCC);
2697 uint32_t const uGstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_GSTS_REG);
2698 uint64_t const uIrtaReg = pThis->uIrtaReg;
2699 DMAR_UNLOCK(pDevIns, pThisCC);
2700
2701 /* Check if interrupt remapping is enabled. */
2702 if (uGstsReg & VTD_BF_GSTS_REG_IRES_MASK)
2703 {
2704 bool const fIsRemappable = RT_BF_GET(pMsiIn->Addr.au32[0], VTD_BF_REMAPPABLE_MSI_ADDR_INTR_FMT);
2705 if (!fIsRemappable)
2706 {
2707 /* Handle compatibility format interrupts. */
2708 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMsiRemapCfi));
2709
2710 /* If EIME is enabled or CFIs are disabled, block the interrupt. */
2711 if ( (uIrtaReg & VTD_BF_IRTA_REG_EIME_MASK)
2712 || !(uGstsReg & VTD_BF_GSTS_REG_CFIS_MASK))
2713 {
2714 dmarIrFaultRecord(pDevIns, kDmarDiag_Ir_Cfi_Blocked, VTDIRFAULT_CFI_BLOCKED, idDevice, 0 /* idxIntr */);
2715 return VERR_IOMMU_INTR_REMAP_DENIED;
2716 }
2717
2718 /* Interrupt isn't subject to remapping, pass-through the interrupt. */
2719 *pMsiOut = *pMsiIn;
2720 return VINF_SUCCESS;
2721 }
2722
2723 /* Handle remappable format interrupts. */
2724 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMsiRemapRfi));
2725 return dmarIrRemapIntr(pDevIns, uIrtaReg, idDevice, pMsiIn, pMsiOut);
2726 }
2727
2728 /* Interrupt-remapping isn't enabled, all interrupts are pass-through. */
2729 *pMsiOut = *pMsiIn;
2730 return VINF_SUCCESS;
2731}
2732
2733
2734/**
2735 * @callback_method_impl{FNIOMMMIONEWWRITE}
2736 */
2737static DECLCALLBACK(VBOXSTRICTRC) dmarMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
2738{
2739 RT_NOREF1(pvUser);
2740 DMAR_ASSERT_MMIO_ACCESS_RET(off, cb);
2741
2742 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
2743 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMmioWrite));
2744
2745 uint16_t const offReg = off;
2746 uint16_t const offLast = offReg + cb - 1;
2747 if (DMAR_IS_MMIO_OFF_VALID(offLast))
2748 {
2749 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
2750 DMAR_LOCK_RET(pDevIns, pThisCC, VINF_IOM_R3_MMIO_WRITE);
2751
2752 uint64_t uPrev = 0;
2753 uint64_t const uRegWritten = cb == 8 ? dmarRegWrite64(pThis, offReg, *(uint64_t *)pv, &uPrev)
2754 : dmarRegWrite32(pThis, offReg, *(uint32_t *)pv, (uint32_t *)&uPrev);
2755 VBOXSTRICTRC rcStrict = VINF_SUCCESS;
2756 switch (off)
2757 {
2758 case VTD_MMIO_OFF_GCMD_REG: /* 32-bit */
2759 {
2760 rcStrict = dmarGcmdRegWrite(pDevIns, uRegWritten);
2761 break;
2762 }
2763
2764 case VTD_MMIO_OFF_CCMD_REG: /* 64-bit */
2765 case VTD_MMIO_OFF_CCMD_REG + 4:
2766 {
2767 rcStrict = dmarCcmdRegWrite(pDevIns, offReg, cb, uRegWritten);
2768 break;
2769 }
2770
2771 case VTD_MMIO_OFF_FSTS_REG: /* 32-bit */
2772 {
2773 rcStrict = dmarFstsRegWrite(pDevIns, uRegWritten, uPrev);
2774 break;
2775 }
2776
2777 case VTD_MMIO_OFF_FECTL_REG: /* 32-bit */
2778 {
2779 rcStrict = dmarFectlRegWrite(pDevIns, uRegWritten);
2780 break;
2781 }
2782
2783 case VTD_MMIO_OFF_IQT_REG: /* 64-bit */
2784 /* VTD_MMIO_OFF_IQT_REG + 4: */ /* High 32-bits reserved. */
2785 {
2786 rcStrict = dmarIqtRegWrite(pDevIns, offReg, uRegWritten);
2787 break;
2788 }
2789
2790 case VTD_MMIO_OFF_IQA_REG: /* 64-bit */
2791 /* VTD_MMIO_OFF_IQA_REG + 4: */ /* High 32-bits data. */
2792 {
2793 rcStrict = dmarIqaRegWrite(pDevIns, offReg, uRegWritten);
2794 break;
2795 }
2796
2797 case VTD_MMIO_OFF_ICS_REG: /* 32-bit */
2798 {
2799 rcStrict = dmarIcsRegWrite(pDevIns, uRegWritten);
2800 break;
2801 }
2802
2803 case VTD_MMIO_OFF_IECTL_REG: /* 32-bit */
2804 {
2805 rcStrict = dmarIectlRegWrite(pDevIns, uRegWritten);
2806 break;
2807 }
2808
2809 case DMAR_MMIO_OFF_FRCD_HI_REG: /* 64-bit */
2810 case DMAR_MMIO_OFF_FRCD_HI_REG + 4:
2811 {
2812 rcStrict = dmarFrcdHiRegWrite(pDevIns, offReg, cb, uRegWritten, uPrev);
2813 break;
2814 }
2815 }
2816
2817 DMAR_UNLOCK(pDevIns, pThisCC);
2818 LogFlowFunc(("offReg=%#x uRegWritten=%#RX64 rc=%Rrc\n", offReg, uRegWritten, VBOXSTRICTRC_VAL(rcStrict)));
2819 return rcStrict;
2820 }
2821
2822 return VINF_IOM_MMIO_UNUSED_FF;
2823}
2824
2825
2826/**
2827 * @callback_method_impl{FNIOMMMIONEWREAD}
2828 */
2829static DECLCALLBACK(VBOXSTRICTRC) dmarMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
2830{
2831 RT_NOREF1(pvUser);
2832 DMAR_ASSERT_MMIO_ACCESS_RET(off, cb);
2833
2834 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
2835 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMmioRead));
2836
2837 uint16_t const offReg = off;
2838 uint16_t const offLast = offReg + cb - 1;
2839 if (DMAR_IS_MMIO_OFF_VALID(offLast))
2840 {
2841 PCDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARCC);
2842 DMAR_LOCK_RET(pDevIns, pThisCC, VINF_IOM_R3_MMIO_READ);
2843
2844 if (cb == 8)
2845 {
2846 *(uint64_t *)pv = dmarRegRead64(pThis, offReg);
2847 LogFlowFunc(("offReg=%#x pv=%#RX64\n", offReg, *(uint64_t *)pv));
2848 }
2849 else
2850 {
2851 *(uint32_t *)pv = dmarRegRead32(pThis, offReg);
2852 LogFlowFunc(("offReg=%#x pv=%#RX32\n", offReg, *(uint32_t *)pv));
2853 }
2854
2855 DMAR_UNLOCK(pDevIns, pThisCC);
2856 return VINF_SUCCESS;
2857 }
2858
2859 return VINF_IOM_MMIO_UNUSED_FF;
2860}
2861
2862
2863#ifdef IN_RING3
2864/**
2865 * Process requests in the invalidation queue.
2866 *
2867 * @param pDevIns The IOMMU device instance.
2868 * @param pvRequests The requests to process.
2869 * @param cbRequests The size of all requests (in bytes).
2870 * @param fDw The descriptor width (VTD_IQA_REG_DW_128_BIT or
2871 * VTD_IQA_REG_DW_256_BIT).
2872 * @param fTtm The table translation mode. Must not be VTD_TTM_RSVD.
2873 */
2874static void dmarR3InvQueueProcessRequests(PPDMDEVINS pDevIns, void const *pvRequests, uint32_t cbRequests, uint8_t fDw,
2875 uint8_t fTtm)
2876{
2877#define DMAR_IQE_FAULT_RECORD_RET(a_enmDiag, a_enmIqei) \
2878 do \
2879 { \
2880 dmarIqeFaultRecord(pDevIns, (a_enmDiag), (a_enmIqei)); \
2881 return; \
2882 } while (0)
2883
2884 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
2885 PCDMARR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARR3);
2886
2887 DMAR_ASSERT_LOCK_IS_NOT_OWNER(pDevIns, pThisR3);
2888 Assert(fTtm != VTD_TTM_RSVD); /* Should've beeen handled by caller. */
2889
2890 /*
2891 * The below check is redundant since we check both TTM and DW for each
2892 * descriptor type we process. However, the error reported by hardware
2893 * may differ hence this is kept commented out but not removed from the code
2894 * if we need to change this in the future.
2895 *
2896 * In our implementation, we would report the descriptor type as invalid,
2897 * while on real hardware it may report descriptor width as invalid.
2898 * The Intel VT-d spec. is not clear which error takes preceedence.
2899 */
2900#if 0
2901 /*
2902 * Verify that 128-bit descriptors are not used when operating in scalable mode.
2903 * We don't check this while software writes IQA_REG but defer it until now because
2904 * RTADDR_REG can be updated lazily (via GCMD_REG.SRTP). The 256-bit descriptor check
2905 * -IS- performed when software writes IQA_REG since it only requires checking against
2906 * immutable hardware features.
2907 */
2908 if ( fTtm != VTD_TTM_SCALABLE_MODE
2909 || fDw != VTD_IQA_REG_DW_128_BIT)
2910 { /* likely */ }
2911 else
2912 DMAR_IQE_FAULT_RECORD_RET(kDmarDiag_IqaReg_Dw_128_Invalid, VTDIQEI_INVALID_DESCRIPTOR_WIDTH);
2913#endif
2914
2915 /*
2916 * Process requests in FIFO order.
2917 */
2918 uint8_t const cbDsc = fDw == VTD_IQA_REG_DW_256_BIT ? 32 : 16;
2919 for (uint32_t offDsc = 0; offDsc < cbRequests; offDsc += cbDsc)
2920 {
2921 uint64_t const *puDscQwords = (uint64_t const *)((uintptr_t)pvRequests + offDsc);
2922 uint64_t const uQword0 = puDscQwords[0];
2923 uint64_t const uQword1 = puDscQwords[1];
2924 uint8_t const fDscType = VTD_GENERIC_INV_DSC_GET_TYPE(uQword0);
2925 switch (fDscType)
2926 {
2927 case VTD_INV_WAIT_DSC_TYPE:
2928 {
2929 /* Validate descriptor type. */
2930 if ( fTtm == VTD_TTM_LEGACY_MODE
2931 || fDw == VTD_IQA_REG_DW_256_BIT)
2932 { /* likely */ }
2933 else
2934 DMAR_IQE_FAULT_RECORD_RET(kDmarDiag_Iqei_Inv_Wait_Dsc_Invalid, VTDIQEI_INVALID_DESCRIPTOR_TYPE);
2935
2936 /* Validate reserved bits. */
2937 uint64_t const fValidMask0 = !(pThis->fExtCapReg & VTD_BF_ECAP_REG_PDS_MASK)
2938 ? VTD_INV_WAIT_DSC_0_VALID_MASK & ~VTD_BF_0_INV_WAIT_DSC_PD_MASK
2939 : VTD_INV_WAIT_DSC_0_VALID_MASK;
2940 if ( !(uQword0 & ~fValidMask0)
2941 && !(uQword1 & ~VTD_INV_WAIT_DSC_1_VALID_MASK))
2942 { /* likely */ }
2943 else
2944 DMAR_IQE_FAULT_RECORD_RET(kDmarDiag_Iqei_Inv_Wait_Dsc_0_1_Rsvd, VTDIQEI_RSVD_FIELD_VIOLATION);
2945
2946 if (fDw == VTD_IQA_REG_DW_256_BIT)
2947 {
2948 if ( !puDscQwords[2]
2949 && !puDscQwords[3])
2950 { /* likely */ }
2951 else
2952 DMAR_IQE_FAULT_RECORD_RET(kDmarDiag_Iqei_Inv_Wait_Dsc_2_3_Rsvd, VTDIQEI_RSVD_FIELD_VIOLATION);
2953 }
2954
2955 /* Perform status write (this must be done prior to generating the completion interrupt). */
2956 bool const fSw = RT_BF_GET(uQword0, VTD_BF_0_INV_WAIT_DSC_SW);
2957 if (fSw)
2958 {
2959 uint32_t const uStatus = RT_BF_GET(uQword0, VTD_BF_0_INV_WAIT_DSC_STDATA);
2960 RTGCPHYS const GCPhysStatus = uQword1 & VTD_BF_1_INV_WAIT_DSC_STADDR_MASK;
2961 int const rc = PDMDevHlpPhysWrite(pDevIns, GCPhysStatus, (void const*)&uStatus, sizeof(uStatus));
2962 AssertRC(rc);
2963 }
2964
2965 /* Generate invalidation event interrupt. */
2966 bool const fIf = RT_BF_GET(uQword0, VTD_BF_0_INV_WAIT_DSC_IF);
2967 if (fIf)
2968 {
2969 DMAR_LOCK(pDevIns, pThisR3);
2970 dmarR3InvEventRaiseInterrupt(pDevIns);
2971 DMAR_UNLOCK(pDevIns, pThisR3);
2972 }
2973
2974 STAM_COUNTER_INC(&pThis->StatInvWaitDsc);
2975 break;
2976 }
2977
2978 case VTD_CC_INV_DSC_TYPE: STAM_COUNTER_INC(&pThis->StatCcInvDsc); break;
2979 case VTD_IOTLB_INV_DSC_TYPE: STAM_COUNTER_INC(&pThis->StatIotlbInvDsc); break;
2980 case VTD_DEV_TLB_INV_DSC_TYPE: STAM_COUNTER_INC(&pThis->StatDevtlbInvDsc); break;
2981 case VTD_IEC_INV_DSC_TYPE: STAM_COUNTER_INC(&pThis->StatIecInvDsc); break;
2982 case VTD_P_IOTLB_INV_DSC_TYPE: STAM_COUNTER_INC(&pThis->StatPasidIotlbInvDsc); break;
2983 case VTD_PC_INV_DSC_TYPE: STAM_COUNTER_INC(&pThis->StatPasidCacheInvDsc); break;
2984 case VTD_P_DEV_TLB_INV_DSC_TYPE: STAM_COUNTER_INC(&pThis->StatPasidDevtlbInvDsc); break;
2985 default:
2986 {
2987 /* Stop processing further requests. */
2988 LogFunc(("Invalid descriptor type: %#x\n", fDscType));
2989 DMAR_IQE_FAULT_RECORD_RET(kDmarDiag_Iqei_Dsc_Type_Invalid, VTDIQEI_INVALID_DESCRIPTOR_TYPE);
2990 }
2991 }
2992 }
2993#undef DMAR_IQE_FAULT_RECORD_RET
2994}
2995
2996
2997/**
2998 * The invalidation-queue thread.
2999 *
3000 * @returns VBox status code.
3001 * @param pDevIns The IOMMU device instance.
3002 * @param pThread The command thread.
3003 */
3004static DECLCALLBACK(int) dmarR3InvQueueThread(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
3005{
3006 NOREF(pThread);
3007 LogFlowFunc(("\n"));
3008
3009 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
3010 return VINF_SUCCESS;
3011
3012 /*
3013 * Pre-allocate the maximum size of the invalidation queue allowed by the spec.
3014 * This prevents trashing the heap as well as deal with out-of-memory situations
3015 * up-front while starting the VM. It also simplifies the code from having to
3016 * dynamically grow/shrink the allocation based on how software sizes the queue.
3017 * Guests normally don't alter the queue size all the time, but that's not an
3018 * assumption we can make.
3019 */
3020 uint8_t const cMaxPages = 1 << VTD_BF_IQA_REG_QS_MASK;
3021 size_t const cbMaxQs = cMaxPages << X86_PAGE_SHIFT;
3022 void *pvRequests = RTMemAllocZ(cbMaxQs);
3023 AssertPtrReturn(pvRequests, VERR_NO_MEMORY);
3024
3025 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
3026 PCDMARR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARR3);
3027
3028 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
3029 {
3030 /*
3031 * Sleep until we are woken up.
3032 */
3033 {
3034 int const rc = PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pThis->hEvtInvQueue, RT_INDEFINITE_WAIT);
3035 AssertLogRelMsgReturn(RT_SUCCESS(rc) || rc == VERR_INTERRUPTED, ("%Rrc\n", rc), rc);
3036 if (RT_UNLIKELY(pThread->enmState != PDMTHREADSTATE_RUNNING))
3037 break;
3038 }
3039
3040 DMAR_LOCK(pDevIns, pThisR3);
3041 if (dmarInvQueueCanProcessRequests(pThis))
3042 {
3043 uint32_t offQueueHead;
3044 uint32_t offQueueTail;
3045 bool const fIsEmpty = dmarInvQueueIsEmptyEx(pThis, &offQueueHead, &offQueueTail);
3046 if (!fIsEmpty)
3047 {
3048 /*
3049 * Get the current queue size, descriptor width, queue base address and the
3050 * table translation mode while the lock is still held.
3051 */
3052 uint64_t const uIqaReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_IQA_REG);
3053 uint8_t const cQueuePages = 1 << (uIqaReg & VTD_BF_IQA_REG_QS_MASK);
3054 uint32_t const cbQueue = cQueuePages << X86_PAGE_SHIFT;
3055 uint8_t const fDw = RT_BF_GET(uIqaReg, VTD_BF_IQA_REG_DW);
3056 uint8_t const fTtm = RT_BF_GET(pThis->uRtaddrReg, VTD_BF_RTADDR_REG_TTM);
3057 RTGCPHYS const GCPhysRequests = (uIqaReg & VTD_BF_IQA_REG_IQA_MASK) + offQueueHead;
3058
3059 /* Paranoia. */
3060 Assert(cbQueue <= cbMaxQs);
3061 Assert(!(offQueueTail & ~VTD_BF_IQT_REG_QT_MASK));
3062 Assert(!(offQueueHead & ~VTD_BF_IQH_REG_QH_MASK));
3063 Assert(fDw != VTD_IQA_REG_DW_256_BIT || !(offQueueTail & RT_BIT(4)));
3064 Assert(fDw != VTD_IQA_REG_DW_256_BIT || !(offQueueHead & RT_BIT(4)));
3065 Assert(offQueueHead < cbQueue);
3066
3067 /*
3068 * A table translation mode of "reserved" isn't valid for any descriptor type.
3069 * However, RTADDR_REG can be modified in parallel to invalidation-queue processing,
3070 * but if ESRTPS is support, we will perform a global invalidation when software
3071 * changes RTADDR_REG, or it's the responsibility of software to do it explicitly.
3072 * So caching TTM while reading all descriptors should not be a problem.
3073 *
3074 * Also, validate the queue tail offset as it's mutable by software.
3075 */
3076 if ( fTtm != VTD_TTM_RSVD
3077 && offQueueTail < cbQueue)
3078 {
3079 /* Don't hold the lock while reading (a potentially large amount of) requests */
3080 DMAR_UNLOCK(pDevIns, pThisR3);
3081
3082 int rc;
3083 uint32_t cbRequests;
3084 if (offQueueTail > offQueueHead)
3085 {
3086 /* The requests have not wrapped around, read them in one go. */
3087 cbRequests = offQueueTail - offQueueHead;
3088 rc = PDMDevHlpPhysReadMeta(pDevIns, GCPhysRequests, pvRequests, cbRequests);
3089 }
3090 else
3091 {
3092 /* The requests have wrapped around, read forward and wrapped-around. */
3093 uint32_t const cbForward = cbQueue - offQueueHead;
3094 rc = PDMDevHlpPhysReadMeta(pDevIns, GCPhysRequests, pvRequests, cbForward);
3095
3096 uint32_t const cbWrapped = offQueueTail;
3097 if ( RT_SUCCESS(rc)
3098 && cbWrapped > 0)
3099 {
3100 rc = PDMDevHlpPhysReadMeta(pDevIns, GCPhysRequests + cbForward,
3101 (void *)((uintptr_t)pvRequests + cbForward), cbWrapped);
3102 }
3103 cbRequests = cbForward + cbWrapped;
3104 }
3105
3106 /* Re-acquire the lock since we need to update device state. */
3107 DMAR_LOCK(pDevIns, pThisR3);
3108
3109 if (RT_SUCCESS(rc))
3110 {
3111 /* Indicate to software we've fetched all requests. */
3112 dmarRegWriteRaw64(pThis, VTD_MMIO_OFF_IQH_REG, offQueueTail);
3113
3114 /* Don't hold the lock while processing requests. */
3115 DMAR_UNLOCK(pDevIns, pThisR3);
3116
3117 /* Process all requests. */
3118 Assert(cbRequests <= cbQueue);
3119 dmarR3InvQueueProcessRequests(pDevIns, pvRequests, cbRequests, fDw, fTtm);
3120
3121 /*
3122 * We've processed all requests and the lock shouldn't be held at this point.
3123 * Using 'continue' here allows us to skip re-acquiring the lock just to release
3124 * it again before going back to the thread loop. It's a bit ugly but it certainly
3125 * helps with performance.
3126 */
3127 DMAR_ASSERT_LOCK_IS_NOT_OWNER(pDevIns, pThisR3);
3128 continue;
3129 }
3130 else
3131 dmarIqeFaultRecord(pDevIns, kDmarDiag_IqaReg_Dsc_Fetch_Error, VTDIQEI_FETCH_DESCRIPTOR_ERR);
3132 }
3133 else
3134 {
3135 if (fTtm == VTD_TTM_RSVD)
3136 dmarIqeFaultRecord(pDevIns, kDmarDiag_Iqei_Ttm_Rsvd, VTDIQEI_INVALID_TTM);
3137 else
3138 {
3139 Assert(offQueueTail >= cbQueue);
3140 dmarIqeFaultRecord(pDevIns, kDmarDiag_IqtReg_Qt_Invalid, VTDIQEI_INVALID_TAIL_PTR);
3141 }
3142 }
3143 }
3144 }
3145 DMAR_UNLOCK(pDevIns, pThisR3);
3146 }
3147
3148 RTMemFree(pvRequests);
3149 pvRequests = NULL;
3150
3151 LogFlowFunc(("Invalidation-queue thread terminating\n"));
3152 return VINF_SUCCESS;
3153}
3154
3155
3156/**
3157 * Wakes up the invalidation-queue thread so it can respond to a state
3158 * change.
3159 *
3160 * @returns VBox status code.
3161 * @param pDevIns The IOMMU device instance.
3162 * @param pThread The invalidation-queue thread.
3163 *
3164 * @thread EMT.
3165 */
3166static DECLCALLBACK(int) dmarR3InvQueueThreadWakeUp(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
3167{
3168 RT_NOREF(pThread);
3169 LogFlowFunc(("\n"));
3170 PCDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
3171 return PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hEvtInvQueue);
3172}
3173
3174
3175/**
3176 * @callback_method_impl{FNDBGFHANDLERDEV}
3177 */
3178static DECLCALLBACK(void) dmarR3DbgInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
3179{
3180 PCDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
3181 PCDMARR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARR3);
3182 bool const fVerbose = RTStrCmp(pszArgs, "verbose") == 0;
3183
3184 /*
3185 * We lock the device to get a consistent register state as it is
3186 * ASSUMED pHlp->pfnPrintf is expensive, so we copy the registers (the
3187 * ones we care about here) into temporaries and release the lock ASAP.
3188 *
3189 * Order of register being read and outputted is in accordance with the
3190 * spec. for no particular reason.
3191 * See Intel VT-d spec. 10.4 "Register Descriptions".
3192 */
3193 DMAR_LOCK(pDevIns, pThisR3);
3194
3195 DMARDIAG const enmDiag = pThis->enmDiag;
3196 uint32_t const uVerReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_VER_REG);
3197 uint64_t const uCapReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_CAP_REG);
3198 uint64_t const uEcapReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_ECAP_REG);
3199 uint32_t const uGcmdReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_GCMD_REG);
3200 uint32_t const uGstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_GSTS_REG);
3201 uint64_t const uRtaddrReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_RTADDR_REG);
3202 uint64_t const uCcmdReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_CCMD_REG);
3203 uint32_t const uFstsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FSTS_REG);
3204 uint32_t const uFectlReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FECTL_REG);
3205 uint32_t const uFedataReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FEDATA_REG);
3206 uint32_t const uFeaddrReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FEADDR_REG);
3207 uint32_t const uFeuaddrReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_FEUADDR_REG);
3208 uint64_t const uAflogReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_AFLOG_REG);
3209 uint32_t const uPmenReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_PMEN_REG);
3210 uint32_t const uPlmbaseReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_PLMBASE_REG);
3211 uint32_t const uPlmlimitReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_PLMLIMIT_REG);
3212 uint64_t const uPhmbaseReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_PHMBASE_REG);
3213 uint64_t const uPhmlimitReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_PHMLIMIT_REG);
3214 uint64_t const uIqhReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_IQH_REG);
3215 uint64_t const uIqtReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_IQT_REG);
3216 uint64_t const uIqaReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_IQA_REG);
3217 uint32_t const uIcsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_ICS_REG);
3218 uint32_t const uIectlReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_IECTL_REG);
3219 uint32_t const uIedataReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_IEDATA_REG);
3220 uint32_t const uIeaddrReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_IEADDR_REG);
3221 uint32_t const uIeuaddrReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_IEUADDR_REG);
3222 uint64_t const uIqercdReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_IQERCD_REG);
3223 uint64_t const uIrtaReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_IRTA_REG);
3224 uint64_t const uPqhReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_PQH_REG);
3225 uint64_t const uPqtReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_PQT_REG);
3226 uint64_t const uPqaReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_PQA_REG);
3227 uint32_t const uPrsReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_PRS_REG);
3228 uint32_t const uPectlReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_PECTL_REG);
3229 uint32_t const uPedataReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_PEDATA_REG);
3230 uint32_t const uPeaddrReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_PEADDR_REG);
3231 uint32_t const uPeuaddrReg = dmarRegReadRaw32(pThis, VTD_MMIO_OFF_PEUADDR_REG);
3232 uint64_t const uMtrrcapReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_MTRRCAP_REG);
3233 uint64_t const uMtrrdefReg = dmarRegReadRaw64(pThis, VTD_MMIO_OFF_MTRRDEF_REG);
3234
3235 DMAR_UNLOCK(pDevIns, pThisR3);
3236
3237 const char *const pszDiag = enmDiag < RT_ELEMENTS(g_apszDmarDiagDesc) ? g_apszDmarDiagDesc[enmDiag] : "(Unknown)";
3238 pHlp->pfnPrintf(pHlp, "Intel-IOMMU:\n");
3239 pHlp->pfnPrintf(pHlp, " Diag = %s\n", pszDiag);
3240
3241 /*
3242 * Non-verbose output.
3243 */
3244 if (!fVerbose)
3245 {
3246 pHlp->pfnPrintf(pHlp, " VER_REG = %#RX32\n", uVerReg);
3247 pHlp->pfnPrintf(pHlp, " CAP_REG = %#RX64\n", uCapReg);
3248 pHlp->pfnPrintf(pHlp, " ECAP_REG = %#RX64\n", uEcapReg);
3249 pHlp->pfnPrintf(pHlp, " GCMD_REG = %#RX32\n", uGcmdReg);
3250 pHlp->pfnPrintf(pHlp, " GSTS_REG = %#RX32\n", uGstsReg);
3251 pHlp->pfnPrintf(pHlp, " RTADDR_REG = %#RX64\n", uRtaddrReg);
3252 pHlp->pfnPrintf(pHlp, " CCMD_REG = %#RX64\n", uCcmdReg);
3253 pHlp->pfnPrintf(pHlp, " FSTS_REG = %#RX32\n", uFstsReg);
3254 pHlp->pfnPrintf(pHlp, " FECTL_REG = %#RX32\n", uFectlReg);
3255 pHlp->pfnPrintf(pHlp, " FEDATA_REG = %#RX32\n", uFedataReg);
3256 pHlp->pfnPrintf(pHlp, " FEADDR_REG = %#RX32\n", uFeaddrReg);
3257 pHlp->pfnPrintf(pHlp, " FEUADDR_REG = %#RX32\n", uFeuaddrReg);
3258 pHlp->pfnPrintf(pHlp, " AFLOG_REG = %#RX64\n", uAflogReg);
3259 pHlp->pfnPrintf(pHlp, " PMEN_REG = %#RX32\n", uPmenReg);
3260 pHlp->pfnPrintf(pHlp, " PLMBASE_REG = %#RX32\n", uPlmbaseReg);
3261 pHlp->pfnPrintf(pHlp, " PLMLIMIT_REG = %#RX32\n", uPlmlimitReg);
3262 pHlp->pfnPrintf(pHlp, " PHMBASE_REG = %#RX64\n", uPhmbaseReg);
3263 pHlp->pfnPrintf(pHlp, " PHMLIMIT_REG = %#RX64\n", uPhmlimitReg);
3264 pHlp->pfnPrintf(pHlp, " IQH_REG = %#RX64\n", uIqhReg);
3265 pHlp->pfnPrintf(pHlp, " IQT_REG = %#RX64\n", uIqtReg);
3266 pHlp->pfnPrintf(pHlp, " IQA_REG = %#RX64\n", uIqaReg);
3267 pHlp->pfnPrintf(pHlp, " ICS_REG = %#RX32\n", uIcsReg);
3268 pHlp->pfnPrintf(pHlp, " IECTL_REG = %#RX32\n", uIectlReg);
3269 pHlp->pfnPrintf(pHlp, " IEDATA_REG = %#RX32\n", uIedataReg);
3270 pHlp->pfnPrintf(pHlp, " IEADDR_REG = %#RX32\n", uIeaddrReg);
3271 pHlp->pfnPrintf(pHlp, " IEUADDR_REG = %#RX32\n", uIeuaddrReg);
3272 pHlp->pfnPrintf(pHlp, " IQERCD_REG = %#RX64\n", uIqercdReg);
3273 pHlp->pfnPrintf(pHlp, " IRTA_REG = %#RX64\n", uIrtaReg);
3274 pHlp->pfnPrintf(pHlp, " PQH_REG = %#RX64\n", uPqhReg);
3275 pHlp->pfnPrintf(pHlp, " PQT_REG = %#RX64\n", uPqtReg);
3276 pHlp->pfnPrintf(pHlp, " PQA_REG = %#RX64\n", uPqaReg);
3277 pHlp->pfnPrintf(pHlp, " PRS_REG = %#RX32\n", uPrsReg);
3278 pHlp->pfnPrintf(pHlp, " PECTL_REG = %#RX32\n", uPectlReg);
3279 pHlp->pfnPrintf(pHlp, " PEDATA_REG = %#RX32\n", uPedataReg);
3280 pHlp->pfnPrintf(pHlp, " PEADDR_REG = %#RX32\n", uPeaddrReg);
3281 pHlp->pfnPrintf(pHlp, " PEUADDR_REG = %#RX32\n", uPeuaddrReg);
3282 pHlp->pfnPrintf(pHlp, " MTRRCAP_REG = %#RX64\n", uMtrrcapReg);
3283 pHlp->pfnPrintf(pHlp, " MTRRDEF_REG = %#RX64\n", uMtrrdefReg);
3284 pHlp->pfnPrintf(pHlp, "\n");
3285 return;
3286 }
3287
3288 /*
3289 * Verbose output.
3290 */
3291 pHlp->pfnPrintf(pHlp, " VER_REG = %#RX32\n", uVerReg);
3292 {
3293 pHlp->pfnPrintf(pHlp, " MAJ = %#x\n", RT_BF_GET(uVerReg, VTD_BF_VER_REG_MAX));
3294 pHlp->pfnPrintf(pHlp, " MIN = %#x\n", RT_BF_GET(uVerReg, VTD_BF_VER_REG_MIN));
3295 }
3296 pHlp->pfnPrintf(pHlp, " CAP_REG = %#RX64\n", uCapReg);
3297 {
3298 uint8_t const uMgaw = RT_BF_GET(uCapReg, VTD_BF_CAP_REG_MGAW);
3299 uint8_t const uNfr = RT_BF_GET(uCapReg, VTD_BF_CAP_REG_NFR);
3300 pHlp->pfnPrintf(pHlp, " ND = %u\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_ND));
3301 pHlp->pfnPrintf(pHlp, " AFL = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_AFL));
3302 pHlp->pfnPrintf(pHlp, " RWBF = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_RWBF));
3303 pHlp->pfnPrintf(pHlp, " PLMR = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_PLMR));
3304 pHlp->pfnPrintf(pHlp, " PHMR = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_PHMR));
3305 pHlp->pfnPrintf(pHlp, " CM = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_CM));
3306 pHlp->pfnPrintf(pHlp, " SAGAW = %#x\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_SAGAW));
3307 pHlp->pfnPrintf(pHlp, " MGAW = %#x (%u bits)\n", uMgaw, uMgaw + 1);
3308 pHlp->pfnPrintf(pHlp, " ZLR = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_ZLR));
3309 pHlp->pfnPrintf(pHlp, " FRO = %#x bytes\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_FRO));
3310 pHlp->pfnPrintf(pHlp, " SLLPS = %#x\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_SLLPS));
3311 pHlp->pfnPrintf(pHlp, " PSI = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_PSI));
3312 pHlp->pfnPrintf(pHlp, " NFR = %u (%u FRCD register%s)\n", uNfr, uNfr + 1, uNfr > 0 ? "s" : "");
3313 pHlp->pfnPrintf(pHlp, " MAMV = %#x\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_MAMV));
3314 pHlp->pfnPrintf(pHlp, " DWD = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_DWD));
3315 pHlp->pfnPrintf(pHlp, " DRD = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_DRD));
3316 pHlp->pfnPrintf(pHlp, " FL1GP = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_FL1GP));
3317 pHlp->pfnPrintf(pHlp, " PI = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_PI));
3318 pHlp->pfnPrintf(pHlp, " FL5LP = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_FL5LP));
3319 pHlp->pfnPrintf(pHlp, " ESIRTPS = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_ESIRTPS));
3320 pHlp->pfnPrintf(pHlp, " ESRTPS = %RTbool\n", RT_BF_GET(uCapReg, VTD_BF_CAP_REG_ESRTPS));
3321 }
3322 pHlp->pfnPrintf(pHlp, " ECAP_REG = %#RX64\n", uEcapReg);
3323 {
3324 uint8_t const uPss = RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_PSS);
3325 pHlp->pfnPrintf(pHlp, " C = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_C));
3326 pHlp->pfnPrintf(pHlp, " QI = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_QI));
3327 pHlp->pfnPrintf(pHlp, " DT = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_DT));
3328 pHlp->pfnPrintf(pHlp, " IR = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_IR));
3329 pHlp->pfnPrintf(pHlp, " EIM = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_EIM));
3330 pHlp->pfnPrintf(pHlp, " PT = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_PT));
3331 pHlp->pfnPrintf(pHlp, " SC = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_SC));
3332 pHlp->pfnPrintf(pHlp, " IRO = %#x bytes\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_IRO));
3333 pHlp->pfnPrintf(pHlp, " MHMV = %#x\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_MHMV));
3334 pHlp->pfnPrintf(pHlp, " MTS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_MTS));
3335 pHlp->pfnPrintf(pHlp, " NEST = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_NEST));
3336 pHlp->pfnPrintf(pHlp, " PRS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_PRS));
3337 pHlp->pfnPrintf(pHlp, " ERS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_ERS));
3338 pHlp->pfnPrintf(pHlp, " SRS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_SRS));
3339 pHlp->pfnPrintf(pHlp, " NWFS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_NWFS));
3340 pHlp->pfnPrintf(pHlp, " EAFS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_EAFS));
3341 pHlp->pfnPrintf(pHlp, " PSS = %u (%u bits)\n", uPss, uPss > 0 ? uPss + 1 : 0);
3342 pHlp->pfnPrintf(pHlp, " PASID = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_PASID));
3343 pHlp->pfnPrintf(pHlp, " DIT = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_DIT));
3344 pHlp->pfnPrintf(pHlp, " PDS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_PDS));
3345 pHlp->pfnPrintf(pHlp, " SMTS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_SMTS));
3346 pHlp->pfnPrintf(pHlp, " VCS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_VCS));
3347 pHlp->pfnPrintf(pHlp, " SLADS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_SLADS));
3348 pHlp->pfnPrintf(pHlp, " SLTS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_SLTS));
3349 pHlp->pfnPrintf(pHlp, " FLTS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_FLTS));
3350 pHlp->pfnPrintf(pHlp, " SMPWCS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_SMPWCS));
3351 pHlp->pfnPrintf(pHlp, " RPS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_RPS));
3352 pHlp->pfnPrintf(pHlp, " ADMS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_ADMS));
3353 pHlp->pfnPrintf(pHlp, " RPRIVS = %RTbool\n", RT_BF_GET(uEcapReg, VTD_BF_ECAP_REG_RPRIVS));
3354 }
3355 pHlp->pfnPrintf(pHlp, " GCMD_REG = %#RX32\n", uGcmdReg);
3356 {
3357 uint8_t const fCfi = RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_CFI);
3358 pHlp->pfnPrintf(pHlp, " CFI = %u (%s)\n", fCfi, fCfi ? "Passthrough" : "Blocked");
3359 pHlp->pfnPrintf(pHlp, " SIRTP = %u\n", RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_SIRTP));
3360 pHlp->pfnPrintf(pHlp, " IRE = %u\n", RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_IRE));
3361 pHlp->pfnPrintf(pHlp, " QIE = %u\n", RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_QIE));
3362 pHlp->pfnPrintf(pHlp, " WBF = %u\n", RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_WBF));
3363 pHlp->pfnPrintf(pHlp, " EAFL = %u\n", RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_SFL));
3364 pHlp->pfnPrintf(pHlp, " SFL = %u\n", RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_SFL));
3365 pHlp->pfnPrintf(pHlp, " SRTP = %u\n", RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_SRTP));
3366 pHlp->pfnPrintf(pHlp, " TE = %u\n", RT_BF_GET(uGcmdReg, VTD_BF_GCMD_REG_TE));
3367 }
3368 pHlp->pfnPrintf(pHlp, " GSTS_REG = %#RX32\n", uGstsReg);
3369 {
3370 uint8_t const fCfis = RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_CFIS);
3371 pHlp->pfnPrintf(pHlp, " CFIS = %u (%s)\n", fCfis, fCfis ? "Passthrough" : "Blocked");
3372 pHlp->pfnPrintf(pHlp, " IRTPS = %u\n", RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_IRTPS));
3373 pHlp->pfnPrintf(pHlp, " IRES = %u\n", RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_IRES));
3374 pHlp->pfnPrintf(pHlp, " QIES = %u\n", RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_QIES));
3375 pHlp->pfnPrintf(pHlp, " WBFS = %u\n", RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_WBFS));
3376 pHlp->pfnPrintf(pHlp, " AFLS = %u\n", RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_AFLS));
3377 pHlp->pfnPrintf(pHlp, " FLS = %u\n", RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_FLS));
3378 pHlp->pfnPrintf(pHlp, " RTPS = %u\n", RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_RTPS));
3379 pHlp->pfnPrintf(pHlp, " TES = %u\n", RT_BF_GET(uGstsReg, VTD_BF_GSTS_REG_TES));
3380 }
3381 pHlp->pfnPrintf(pHlp, " RTADDR_REG = %#RX64\n", uRtaddrReg);
3382 {
3383 uint8_t const uTtm = RT_BF_GET(uRtaddrReg, VTD_BF_RTADDR_REG_TTM);
3384 pHlp->pfnPrintf(pHlp, " RTA = %#RX64\n", uRtaddrReg & VTD_BF_RTADDR_REG_RTA_MASK);
3385 pHlp->pfnPrintf(pHlp, " TTM = %u (%s)\n", uTtm, vtdRtaddrRegGetTtmDesc(uTtm));
3386 }
3387 pHlp->pfnPrintf(pHlp, " CCMD_REG = %#RX64\n", uCcmdReg);
3388 pHlp->pfnPrintf(pHlp, " FSTS_REG = %#RX32\n", uFstsReg);
3389 {
3390 pHlp->pfnPrintf(pHlp, " PFO = %u\n", RT_BF_GET(uFstsReg, VTD_BF_FSTS_REG_PFO));
3391 pHlp->pfnPrintf(pHlp, " PPF = %u\n", RT_BF_GET(uFstsReg, VTD_BF_FSTS_REG_PPF));
3392 pHlp->pfnPrintf(pHlp, " AFO = %u\n", RT_BF_GET(uFstsReg, VTD_BF_FSTS_REG_AFO));
3393 pHlp->pfnPrintf(pHlp, " APF = %u\n", RT_BF_GET(uFstsReg, VTD_BF_FSTS_REG_APF));
3394 pHlp->pfnPrintf(pHlp, " IQE = %u\n", RT_BF_GET(uFstsReg, VTD_BF_FSTS_REG_IQE));
3395 pHlp->pfnPrintf(pHlp, " ICS = %u\n", RT_BF_GET(uFstsReg, VTD_BF_FSTS_REG_ICE));
3396 pHlp->pfnPrintf(pHlp, " ITE = %u\n", RT_BF_GET(uFstsReg, VTD_BF_FSTS_REG_ITE));
3397 pHlp->pfnPrintf(pHlp, " FRI = %u\n", RT_BF_GET(uFstsReg, VTD_BF_FSTS_REG_FRI));
3398 }
3399 pHlp->pfnPrintf(pHlp, " FECTL_REG = %#RX32\n", uFectlReg);
3400 {
3401 pHlp->pfnPrintf(pHlp, " IM = %RTbool\n", RT_BF_GET(uFectlReg, VTD_BF_FECTL_REG_IM));
3402 pHlp->pfnPrintf(pHlp, " IP = %RTbool\n", RT_BF_GET(uFectlReg, VTD_BF_FECTL_REG_IP));
3403 }
3404 pHlp->pfnPrintf(pHlp, " FEDATA_REG = %#RX32\n", uFedataReg);
3405 pHlp->pfnPrintf(pHlp, " FEADDR_REG = %#RX32\n", uFeaddrReg);
3406 pHlp->pfnPrintf(pHlp, " FEUADDR_REG = %#RX32\n", uFeuaddrReg);
3407 pHlp->pfnPrintf(pHlp, " AFLOG_REG = %#RX64\n", uAflogReg);
3408 pHlp->pfnPrintf(pHlp, " PMEN_REG = %#RX32\n", uPmenReg);
3409 pHlp->pfnPrintf(pHlp, " PLMBASE_REG = %#RX32\n", uPlmbaseReg);
3410 pHlp->pfnPrintf(pHlp, " PLMLIMIT_REG = %#RX32\n", uPlmlimitReg);
3411 pHlp->pfnPrintf(pHlp, " PHMBASE_REG = %#RX64\n", uPhmbaseReg);
3412 pHlp->pfnPrintf(pHlp, " PHMLIMIT_REG = %#RX64\n", uPhmlimitReg);
3413 pHlp->pfnPrintf(pHlp, " IQH_REG = %#RX64\n", uIqhReg);
3414 pHlp->pfnPrintf(pHlp, " IQT_REG = %#RX64\n", uIqtReg);
3415 pHlp->pfnPrintf(pHlp, " IQA_REG = %#RX64\n", uIqaReg);
3416 {
3417 uint8_t const fDw = RT_BF_GET(uIqaReg, VTD_BF_IQA_REG_DW);
3418 uint8_t const fQs = RT_BF_GET(uIqaReg, VTD_BF_IQA_REG_QS);
3419 uint8_t const cQueuePages = 1 << fQs;
3420 pHlp->pfnPrintf(pHlp, " DW = %u (%s)\n", fDw, fDw == VTD_IQA_REG_DW_128_BIT ? "128-bit" : "256-bit");
3421 pHlp->pfnPrintf(pHlp, " QS = %u (%u page%s)\n", fQs, cQueuePages, cQueuePages > 1 ? "s" : "");
3422 }
3423 pHlp->pfnPrintf(pHlp, " ICS_REG = %#RX32\n", uIcsReg);
3424 {
3425 pHlp->pfnPrintf(pHlp, " IWC = %u\n", RT_BF_GET(uIcsReg, VTD_BF_ICS_REG_IWC));
3426 }
3427 pHlp->pfnPrintf(pHlp, " IECTL_REG = %#RX32\n", uIectlReg);
3428 {
3429 pHlp->pfnPrintf(pHlp, " IM = %RTbool\n", RT_BF_GET(uIectlReg, VTD_BF_IECTL_REG_IM));
3430 pHlp->pfnPrintf(pHlp, " IP = %RTbool\n", RT_BF_GET(uIectlReg, VTD_BF_IECTL_REG_IP));
3431 }
3432 pHlp->pfnPrintf(pHlp, " IEDATA_REG = %#RX32\n", uIedataReg);
3433 pHlp->pfnPrintf(pHlp, " IEADDR_REG = %#RX32\n", uIeaddrReg);
3434 pHlp->pfnPrintf(pHlp, " IEUADDR_REG = %#RX32\n", uIeuaddrReg);
3435 pHlp->pfnPrintf(pHlp, " IQERCD_REG = %#RX64\n", uIqercdReg);
3436 {
3437 pHlp->pfnPrintf(pHlp, " ICESID = %#RX32\n", RT_BF_GET(uIqercdReg, VTD_BF_IQERCD_REG_ICESID));
3438 pHlp->pfnPrintf(pHlp, " ITESID = %#RX32\n", RT_BF_GET(uIqercdReg, VTD_BF_IQERCD_REG_ITESID));
3439 pHlp->pfnPrintf(pHlp, " IQEI = %#RX32\n", RT_BF_GET(uIqercdReg, VTD_BF_IQERCD_REG_IQEI));
3440 }
3441 pHlp->pfnPrintf(pHlp, " IRTA_REG = %#RX64\n", uIrtaReg);
3442 {
3443 uint32_t const cIrtEntries = VTD_IRTA_REG_GET_ENTRY_COUNT(uIrtaReg);
3444 uint32_t const cbIrt = sizeof(VTD_IRTE_T) * cIrtEntries;
3445 pHlp->pfnPrintf(pHlp, " IRTA = %#RX64\n", uIrtaReg & VTD_BF_IRTA_REG_IRTA_MASK);
3446 pHlp->pfnPrintf(pHlp, " EIME = %RTbool\n", RT_BF_GET(uIrtaReg, VTD_BF_IRTA_REG_EIME));
3447 pHlp->pfnPrintf(pHlp, " S = %u entries (%u bytes)\n", cIrtEntries, cbIrt);
3448 }
3449 pHlp->pfnPrintf(pHlp, " PQH_REG = %#RX64\n", uPqhReg);
3450 pHlp->pfnPrintf(pHlp, " PQT_REG = %#RX64\n", uPqtReg);
3451 pHlp->pfnPrintf(pHlp, " PQA_REG = %#RX64\n", uPqaReg);
3452 pHlp->pfnPrintf(pHlp, " PRS_REG = %#RX32\n", uPrsReg);
3453 pHlp->pfnPrintf(pHlp, " PECTL_REG = %#RX32\n", uPectlReg);
3454 pHlp->pfnPrintf(pHlp, " PEDATA_REG = %#RX32\n", uPedataReg);
3455 pHlp->pfnPrintf(pHlp, " PEADDR_REG = %#RX32\n", uPeaddrReg);
3456 pHlp->pfnPrintf(pHlp, " PEUADDR_REG = %#RX32\n", uPeuaddrReg);
3457 pHlp->pfnPrintf(pHlp, " MTRRCAP_REG = %#RX64\n", uMtrrcapReg);
3458 pHlp->pfnPrintf(pHlp, " MTRRDEF_REG = %#RX64\n", uMtrrdefReg);
3459 pHlp->pfnPrintf(pHlp, "\n");
3460}
3461
3462
3463/**
3464 * Initializes all registers in the DMAR unit.
3465 *
3466 * @param pDevIns The IOMMU device instance.
3467 */
3468static void dmarR3RegsInit(PPDMDEVINS pDevIns)
3469{
3470 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
3471
3472 /*
3473 * Wipe all registers (required on reset).
3474 */
3475 RT_ZERO(pThis->abRegs0);
3476 RT_ZERO(pThis->abRegs1);
3477
3478 /*
3479 * Initialize registers not mutable by software prior to initializing other registers.
3480 */
3481 /* VER_REG */
3482 {
3483 pThis->uVerReg = RT_BF_MAKE(VTD_BF_VER_REG_MIN, DMAR_VER_MINOR)
3484 | RT_BF_MAKE(VTD_BF_VER_REG_MAX, DMAR_VER_MAJOR);
3485 dmarRegWriteRaw64(pThis, VTD_MMIO_OFF_VER_REG, pThis->uVerReg);
3486 }
3487
3488 uint8_t const fFlts = 1; /* First-level translation support. */
3489 uint8_t const fSlts = 1; /* Second-level translation support. */
3490 uint8_t const fPt = 1; /* Pass-Through support. */
3491 uint8_t const fSmts = fFlts & fSlts & fPt; /* Scalable mode translation support.*/
3492 uint8_t const fNest = 0; /* Nested translation support. */
3493
3494 /* CAP_REG */
3495 {
3496 uint8_t cGstPhysAddrBits;
3497 uint8_t cGstLinearAddrBits;
3498 PDMDevHlpCpuGetGuestAddrWidths(pDevIns, &cGstPhysAddrBits, &cGstLinearAddrBits);
3499
3500 uint8_t const fFl1gp = 1; /* First-level 1GB pages support. */
3501 uint8_t const fFl5lp = 1; /* First-level 5-level paging support (PML5E). */
3502 uint8_t const fSl2mp = 1; /* Second-level 2MB pages support. */
3503 uint8_t const fSl2gp = fSl2mp & 1; /* Second-level 1GB pages support. */
3504 uint8_t const fSllps = fSl2mp | (fSl2gp << 1); /* Second-level large page support. */
3505 uint8_t const fMamv = (fSl2gp ? X86_PAGE_1G_SHIFT /* Maximum address mask value (for 2nd-level invalidations). */
3506 : X86_PAGE_2M_SHIFT)
3507 - X86_PAGE_4K_SHIFT;
3508 uint8_t const fNd = DMAR_ND; /* Number of domains supported. */
3509 uint8_t const fPsi = 1; /* Page selective invalidation. */
3510 uint8_t const uMgaw = cGstPhysAddrBits - 1; /* Maximum guest address width. */
3511 uint8_t const fSagaw = vtdCapRegGetSagaw(uMgaw); /* Supported adjust guest address width. */
3512 uint16_t const offFro = DMAR_MMIO_OFF_FRCD_LO_REG >> 4; /* MMIO offset of FRCD registers. */
3513 uint8_t const fEsrtps = 1; /* Enhanced SRTPS (auto invalidate cache on SRTP). */
3514 uint8_t const fEsirtps = 1; /* Enhanced SIRTPS (auto invalidate cache on SIRTP). */
3515 AssertCompile(DMAR_ND <= 6);
3516
3517 pThis->fCapReg = RT_BF_MAKE(VTD_BF_CAP_REG_ND, fNd)
3518 | RT_BF_MAKE(VTD_BF_CAP_REG_AFL, 0) /* Advanced fault logging not supported. */
3519 | RT_BF_MAKE(VTD_BF_CAP_REG_RWBF, 0) /* Software need not flush write-buffers. */
3520 | RT_BF_MAKE(VTD_BF_CAP_REG_PLMR, 0) /* Protected Low-Memory Region not supported. */
3521 | RT_BF_MAKE(VTD_BF_CAP_REG_PHMR, 0) /* Protected High-Memory Region not supported. */
3522 | RT_BF_MAKE(VTD_BF_CAP_REG_CM, 1) /* Software should invalidate on mapping structure changes. */
3523 | RT_BF_MAKE(VTD_BF_CAP_REG_SAGAW, fSlts & fSagaw)
3524 | RT_BF_MAKE(VTD_BF_CAP_REG_MGAW, uMgaw)
3525 | RT_BF_MAKE(VTD_BF_CAP_REG_ZLR, 1) /** @todo Figure out if/how to support zero-length reads. */
3526 | RT_BF_MAKE(VTD_BF_CAP_REG_FRO, offFro)
3527 | RT_BF_MAKE(VTD_BF_CAP_REG_SLLPS, fSlts & fSllps)
3528 | RT_BF_MAKE(VTD_BF_CAP_REG_PSI, fPsi)
3529 | RT_BF_MAKE(VTD_BF_CAP_REG_NFR, DMAR_FRCD_REG_COUNT - 1)
3530 | RT_BF_MAKE(VTD_BF_CAP_REG_MAMV, fPsi & fMamv)
3531 | RT_BF_MAKE(VTD_BF_CAP_REG_DWD, 1)
3532 | RT_BF_MAKE(VTD_BF_CAP_REG_DRD, 1)
3533 | RT_BF_MAKE(VTD_BF_CAP_REG_FL1GP, fFlts & fFl1gp)
3534 | RT_BF_MAKE(VTD_BF_CAP_REG_PI, 0) /* Posted Interrupts not supported. */
3535 | RT_BF_MAKE(VTD_BF_CAP_REG_FL5LP, fFlts & fFl5lp)
3536 | RT_BF_MAKE(VTD_BF_CAP_REG_ESIRTPS, fEsirtps)
3537 | RT_BF_MAKE(VTD_BF_CAP_REG_ESRTPS, fEsrtps);
3538 dmarRegWriteRaw64(pThis, VTD_MMIO_OFF_CAP_REG, pThis->fCapReg);
3539
3540 pThis->fHawBaseMask = ~(UINT64_MAX << cGstPhysAddrBits) & X86_PAGE_4K_BASE_MASK;
3541 pThis->fInvMgawMask = UINT64_MAX << cGstPhysAddrBits;
3542 pThis->cMaxPagingLevel = vtdCapRegGetMaxPagingLevel(fSagaw);
3543 }
3544
3545 /* ECAP_REG */
3546 {
3547 uint8_t const fQi = 1; /* Queued-invalidations. */
3548 uint8_t const fIr = !!(DMAR_ACPI_DMAR_FLAGS & ACPI_DMAR_F_INTR_REMAP); /* Interrupt remapping support. */
3549 uint8_t const fMhmv = 0xf; /* Maximum handle mask value. */
3550 uint16_t const offIro = DMAR_MMIO_OFF_IVA_REG >> 4; /* MMIO offset of IOTLB registers. */
3551 uint8_t const fEim = 1; /* Extended interrupt mode.*/
3552 uint8_t const fAdms = 1; /* Abort DMA mode support. */
3553 uint8_t const fErs = 0; /* Execute Request (not supported). */
3554
3555 pThis->fExtCapReg = RT_BF_MAKE(VTD_BF_ECAP_REG_C, 0) /* Accesses don't snoop CPU cache. */
3556 | RT_BF_MAKE(VTD_BF_ECAP_REG_QI, fQi)
3557 | RT_BF_MAKE(VTD_BF_ECAP_REG_DT, 0) /* Device-TLBs not supported. */
3558 | RT_BF_MAKE(VTD_BF_ECAP_REG_IR, fQi & fIr)
3559 | RT_BF_MAKE(VTD_BF_ECAP_REG_EIM, fIr & fEim)
3560 | RT_BF_MAKE(VTD_BF_ECAP_REG_PT, fPt)
3561 | RT_BF_MAKE(VTD_BF_ECAP_REG_SC, 0) /* Snoop control not supported. */
3562 | RT_BF_MAKE(VTD_BF_ECAP_REG_IRO, offIro)
3563 | RT_BF_MAKE(VTD_BF_ECAP_REG_MHMV, fIr & fMhmv)
3564 | RT_BF_MAKE(VTD_BF_ECAP_REG_MTS, 0) /* Memory type not supported. */
3565 | RT_BF_MAKE(VTD_BF_ECAP_REG_NEST, fNest)
3566 | RT_BF_MAKE(VTD_BF_ECAP_REG_PRS, 0) /* 0 as DT not supported. */
3567 | RT_BF_MAKE(VTD_BF_ECAP_REG_ERS, fErs)
3568 | RT_BF_MAKE(VTD_BF_ECAP_REG_SRS, 0) /* Supervisor request not supported. */
3569 | RT_BF_MAKE(VTD_BF_ECAP_REG_NWFS, 0) /* 0 as DT not supported. */
3570 | RT_BF_MAKE(VTD_BF_ECAP_REG_EAFS, 0) /** @todo figure out if EAFS is required? */
3571 | RT_BF_MAKE(VTD_BF_ECAP_REG_PSS, 0) /* 0 as PASID not supported. */
3572 | RT_BF_MAKE(VTD_BF_ECAP_REG_PASID, 0) /* PASID support. */
3573 | RT_BF_MAKE(VTD_BF_ECAP_REG_DIT, 0) /* 0 as DT not supported. */
3574 | RT_BF_MAKE(VTD_BF_ECAP_REG_PDS, 0) /* 0 as DT not supported. */
3575 | RT_BF_MAKE(VTD_BF_ECAP_REG_SMTS, fSmts)
3576 | RT_BF_MAKE(VTD_BF_ECAP_REG_VCS, 0) /* 0 as PASID not supported (commands seem PASID specific). */
3577 | RT_BF_MAKE(VTD_BF_ECAP_REG_SLADS, 0) /* Second-level accessed/dirty not supported. */
3578 | RT_BF_MAKE(VTD_BF_ECAP_REG_SLTS, fSlts)
3579 | RT_BF_MAKE(VTD_BF_ECAP_REG_FLTS, fFlts)
3580 | RT_BF_MAKE(VTD_BF_ECAP_REG_SMPWCS, 0) /* 0 as PASID not supported. */
3581 | RT_BF_MAKE(VTD_BF_ECAP_REG_RPS, 0) /* We don't support RID_PASID field in SM context entry. */
3582 | RT_BF_MAKE(VTD_BF_ECAP_REG_ADMS, fAdms)
3583 | RT_BF_MAKE(VTD_BF_ECAP_REG_RPRIVS, 0); /* 0 as SRS not supported. */
3584 dmarRegWriteRaw64(pThis, VTD_MMIO_OFF_ECAP_REG, pThis->fExtCapReg);
3585
3586 pThis->fPermValidMask = DMAR_PERM_READ | DMAR_PERM_WRITE;
3587 if (fErs)
3588 pThis->fPermValidMask = DMAR_PERM_EXE;
3589 }
3590
3591 /*
3592 * Initialize registers mutable by software.
3593 */
3594 /* FECTL_REG */
3595 {
3596 uint32_t const uCtl = RT_BF_MAKE(VTD_BF_FECTL_REG_IM, 1);
3597 dmarRegWriteRaw32(pThis, VTD_MMIO_OFF_FECTL_REG, uCtl);
3598 }
3599
3600 /* ICETL_REG */
3601 {
3602 uint32_t const uCtl = RT_BF_MAKE(VTD_BF_IECTL_REG_IM, 1);
3603 dmarRegWriteRaw32(pThis, VTD_MMIO_OFF_IECTL_REG, uCtl);
3604 }
3605
3606#ifdef VBOX_STRICT
3607 Assert(!RT_BF_GET(pThis->fExtCapReg, VTD_BF_ECAP_REG_PRS)); /* PECTL_REG - Reserved if don't support PRS. */
3608 Assert(!RT_BF_GET(pThis->fExtCapReg, VTD_BF_ECAP_REG_MTS)); /* MTRRCAP_REG - Reserved if we don't support MTS. */
3609#endif
3610}
3611
3612
3613/**
3614 * @interface_method_impl{PDMDEVREG,pfnReset}
3615 */
3616static DECLCALLBACK(void) iommuIntelR3Reset(PPDMDEVINS pDevIns)
3617{
3618 PCDMARR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARR3);
3619 LogFlowFunc(("\n"));
3620
3621 DMAR_LOCK(pDevIns, pThisR3);
3622 dmarR3RegsInit(pDevIns);
3623 DMAR_UNLOCK(pDevIns, pThisR3);
3624}
3625
3626
3627/**
3628 * @interface_method_impl{PDMDEVREG,pfnDestruct}
3629 */
3630static DECLCALLBACK(int) iommuIntelR3Destruct(PPDMDEVINS pDevIns)
3631{
3632 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
3633 PCDMARR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PCDMARR3);
3634 LogFlowFunc(("\n"));
3635
3636 DMAR_LOCK(pDevIns, pThisR3);
3637
3638 if (pThis->hEvtInvQueue != NIL_SUPSEMEVENT)
3639 {
3640 PDMDevHlpSUPSemEventClose(pDevIns, pThis->hEvtInvQueue);
3641 pThis->hEvtInvQueue = NIL_SUPSEMEVENT;
3642 }
3643
3644 DMAR_UNLOCK(pDevIns, pThisR3);
3645 return VINF_SUCCESS;
3646}
3647
3648
3649/**
3650 * @interface_method_impl{PDMDEVREG,pfnConstruct}
3651 */
3652static DECLCALLBACK(int) iommuIntelR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
3653{
3654 RT_NOREF(pCfg);
3655
3656 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
3657 PDMARR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PDMARR3);
3658 pThisR3->pDevInsR3 = pDevIns;
3659
3660 LogFlowFunc(("iInstance=%d\n", iInstance));
3661 NOREF(iInstance);
3662
3663 /*
3664 * Register the IOMMU with PDM.
3665 */
3666 PDMIOMMUREGR3 IommuReg;
3667 RT_ZERO(IommuReg);
3668 IommuReg.u32Version = PDM_IOMMUREGCC_VERSION;
3669 IommuReg.pfnMemAccess = iommuIntelMemAccess;
3670 IommuReg.pfnMemBulkAccess = iommuIntelMemBulkAccess;
3671 IommuReg.pfnMsiRemap = iommuIntelMsiRemap;
3672 IommuReg.u32TheEnd = PDM_IOMMUREGCC_VERSION;
3673 int rc = PDMDevHlpIommuRegister(pDevIns, &IommuReg, &pThisR3->CTX_SUFF(pIommuHlp), &pThis->idxIommu);
3674 if (RT_FAILURE(rc))
3675 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to register ourselves as an IOMMU device"));
3676 if (pThisR3->CTX_SUFF(pIommuHlp)->u32Version != PDM_IOMMUHLPR3_VERSION)
3677 return PDMDevHlpVMSetError(pDevIns, VERR_VERSION_MISMATCH, RT_SRC_POS,
3678 N_("IOMMU helper version mismatch; got %#x expected %#x"),
3679 pThisR3->CTX_SUFF(pIommuHlp)->u32Version, PDM_IOMMUHLPR3_VERSION);
3680 if (pThisR3->CTX_SUFF(pIommuHlp)->u32TheEnd != PDM_IOMMUHLPR3_VERSION)
3681 return PDMDevHlpVMSetError(pDevIns, VERR_VERSION_MISMATCH, RT_SRC_POS,
3682 N_("IOMMU helper end-version mismatch; got %#x expected %#x"),
3683 pThisR3->CTX_SUFF(pIommuHlp)->u32TheEnd, PDM_IOMMUHLPR3_VERSION);
3684 AssertPtr(pThisR3->pIommuHlpR3->pfnLock);
3685 AssertPtr(pThisR3->pIommuHlpR3->pfnUnlock);
3686 AssertPtr(pThisR3->pIommuHlpR3->pfnLockIsOwner);
3687 AssertPtr(pThisR3->pIommuHlpR3->pfnSendMsi);
3688
3689 /*
3690 * Use PDM's critical section (via helpers) for the IOMMU device.
3691 */
3692 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
3693 AssertRCReturn(rc, rc);
3694
3695 /*
3696 * Initialize PCI configuration registers.
3697 */
3698 PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
3699 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
3700
3701 /* Header. */
3702 PDMPciDevSetVendorId(pPciDev, DMAR_PCI_VENDOR_ID); /* Intel */
3703 PDMPciDevSetDeviceId(pPciDev, DMAR_PCI_DEVICE_ID); /* VirtualBox DMAR device */
3704 PDMPciDevSetRevisionId(pPciDev, DMAR_PCI_REVISION_ID); /* VirtualBox specific device implementation revision */
3705 PDMPciDevSetClassBase(pPciDev, VBOX_PCI_CLASS_SYSTEM); /* System Base Peripheral */
3706 PDMPciDevSetClassSub(pPciDev, VBOX_PCI_SUB_SYSTEM_OTHER); /* Other */
3707 PDMPciDevSetHeaderType(pPciDev, 0); /* Single function, type 0 */
3708 PDMPciDevSetSubSystemId(pPciDev, DMAR_PCI_DEVICE_ID); /* VirtualBox DMAR device */
3709 PDMPciDevSetSubSystemVendorId(pPciDev, DMAR_PCI_VENDOR_ID); /* Intel */
3710
3711 /** @todo Chipset spec says PCI Express Capability Id. Relevant for us? */
3712 PDMPciDevSetStatus(pPciDev, 0);
3713 PDMPciDevSetCapabilityList(pPciDev, 0);
3714
3715 /** @todo VTBAR at 0x180? */
3716
3717 /*
3718 * Register the PCI function with PDM.
3719 */
3720 rc = PDMDevHlpPCIRegister(pDevIns, pPciDev);
3721 AssertLogRelRCReturn(rc, rc);
3722
3723 /** @todo Register MSI but what's the MSI capability offset? */
3724#if 0
3725 /*
3726 * Register MSI support for the PCI device.
3727 * This must be done -after- registering it as a PCI device!
3728 */
3729#endif
3730
3731 /*
3732 * Register MMIO region.
3733 */
3734 AssertCompile(!(DMAR_MMIO_BASE_PHYSADDR & X86_PAGE_4K_OFFSET_MASK));
3735 rc = PDMDevHlpMmioCreateAndMap(pDevIns, DMAR_MMIO_BASE_PHYSADDR, DMAR_MMIO_SIZE, dmarMmioWrite, dmarMmioRead,
3736 IOMMMIO_FLAGS_READ_DWORD_QWORD | IOMMMIO_FLAGS_WRITE_DWORD_QWORD_ZEROED, "Intel-IOMMU",
3737 &pThis->hMmio);
3738 AssertLogRelRCReturn(rc, rc);
3739
3740 /*
3741 * Register debugger info items.
3742 */
3743 rc = PDMDevHlpDBGFInfoRegister(pDevIns, "iommu", "Display IOMMU state.", dmarR3DbgInfo);
3744 AssertLogRelRCReturn(rc, rc);
3745
3746#ifdef VBOX_WITH_STATISTICS
3747 /*
3748 * Statistics.
3749 */
3750 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioReadR3, STAMTYPE_COUNTER, "R3/MmioRead", STAMUNIT_OCCURENCES, "Number of MMIO reads in R3");
3751 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioReadRZ, STAMTYPE_COUNTER, "RZ/MmioRead", STAMUNIT_OCCURENCES, "Number of MMIO reads in RZ.");
3752
3753 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioWriteR3, STAMTYPE_COUNTER, "R3/MmioWrite", STAMUNIT_OCCURENCES, "Number of MMIO writes in R3.");
3754 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioWriteRZ, STAMTYPE_COUNTER, "RZ/MmioWrite", STAMUNIT_OCCURENCES, "Number of MMIO writes in RZ.");
3755
3756 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMsiRemapCfiR3, STAMTYPE_COUNTER, "R3/MsiRemapCfi", STAMUNIT_OCCURENCES, "Number of compatibility-format interrupt remap requests in R3.");
3757 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMsiRemapCfiRZ, STAMTYPE_COUNTER, "RZ/MsiRemapCfi", STAMUNIT_OCCURENCES, "Number of compatibility-format interrupt remap requests in RZ.");
3758 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMsiRemapRfiR3, STAMTYPE_COUNTER, "R3/MsiRemapRfi", STAMUNIT_OCCURENCES, "Number of remappable-format interrupt remap requests in R3.");
3759 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMsiRemapRfiRZ, STAMTYPE_COUNTER, "RZ/MsiRemapRfi", STAMUNIT_OCCURENCES, "Number of remappable-format interrupt remap requests in RZ.");
3760
3761 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemReadR3, STAMTYPE_COUNTER, "R3/MemRead", STAMUNIT_OCCURENCES, "Number of memory read translation requests in R3.");
3762 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemReadRZ, STAMTYPE_COUNTER, "RZ/MemRead", STAMUNIT_OCCURENCES, "Number of memory read translation requests in RZ.");
3763
3764 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemWriteR3, STAMTYPE_COUNTER, "R3/MemWrite", STAMUNIT_OCCURENCES, "Number of memory write translation requests in R3.");
3765 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemWriteRZ, STAMTYPE_COUNTER, "RZ/MemWrite", STAMUNIT_OCCURENCES, "Number of memory write translation requests in RZ.");
3766
3767 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemBulkReadR3, STAMTYPE_COUNTER, "R3/MemBulkRead", STAMUNIT_OCCURENCES, "Number of memory bulk read translation requests in R3.");
3768 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemBulkReadRZ, STAMTYPE_COUNTER, "RZ/MemBulkRead", STAMUNIT_OCCURENCES, "Number of memory bulk read translation requests in RZ.");
3769
3770 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemBulkWriteR3, STAMTYPE_COUNTER, "R3/MemBulkWrite", STAMUNIT_OCCURENCES, "Number of memory bulk write translation requests in R3.");
3771 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemBulkWriteRZ, STAMTYPE_COUNTER, "RZ/MemBulkWrite", STAMUNIT_OCCURENCES, "Number of memory bulk write translation requests in RZ.");
3772
3773 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCcInvDsc, STAMTYPE_COUNTER, "R3/QI/CcInv", STAMUNIT_OCCURENCES, "Number of cc_inv_dsc processed.");
3774 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIotlbInvDsc, STAMTYPE_COUNTER, "R3/QI/IotlbInv", STAMUNIT_OCCURENCES, "Number of iotlb_inv_dsc processed.");
3775 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatDevtlbInvDsc, STAMTYPE_COUNTER, "R3/QI/DevtlbInv", STAMUNIT_OCCURENCES, "Number of dev_tlb_inv_dsc processed.");
3776 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIecInvDsc, STAMTYPE_COUNTER, "R3/QI/IecInv", STAMUNIT_OCCURENCES, "Number of iec_inv processed.");
3777 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatInvWaitDsc, STAMTYPE_COUNTER, "R3/QI/InvWait", STAMUNIT_OCCURENCES, "Number of inv_wait_dsc processed.");
3778 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatPasidIotlbInvDsc, STAMTYPE_COUNTER, "R3/QI/PasidIotlbInv", STAMUNIT_OCCURENCES, "Number of p_iotlb_inv_dsc processed.");
3779 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatPasidCacheInvDsc, STAMTYPE_COUNTER, "R3/QI/PasidCacheInv", STAMUNIT_OCCURENCES, "Number of pc_inv_dsc pprocessed.");
3780 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatPasidDevtlbInvDsc, STAMTYPE_COUNTER, "R3/QI/PasidDevtlbInv", STAMUNIT_OCCURENCES, "Number of p_dev_tlb_inv_dsc processed.");
3781#endif
3782
3783 /*
3784 * Initialize registers.
3785 */
3786 dmarR3RegsInit(pDevIns);
3787
3788 /*
3789 * Create invalidation-queue thread and semaphore.
3790 */
3791 char szInvQueueThread[32];
3792 RT_ZERO(szInvQueueThread);
3793 RTStrPrintf(szInvQueueThread, sizeof(szInvQueueThread), "IOMMU-QI-%u", iInstance);
3794 rc = PDMDevHlpThreadCreate(pDevIns, &pThisR3->pInvQueueThread, pThis, dmarR3InvQueueThread, dmarR3InvQueueThreadWakeUp,
3795 0 /* cbStack */, RTTHREADTYPE_IO, szInvQueueThread);
3796 AssertLogRelRCReturn(rc, rc);
3797
3798 rc = PDMDevHlpSUPSemEventCreate(pDevIns, &pThis->hEvtInvQueue);
3799 AssertLogRelRCReturn(rc, rc);
3800
3801 /*
3802 * Log some of the features exposed to software.
3803 */
3804 uint32_t const uVerReg = pThis->uVerReg;
3805 uint8_t const cMgawBits = RT_BF_GET(pThis->fCapReg, VTD_BF_CAP_REG_MGAW) + 1;
3806 uint8_t const fSagaw = RT_BF_GET(pThis->fCapReg, VTD_BF_CAP_REG_SAGAW);
3807 uint16_t const offFrcd = RT_BF_GET(pThis->fCapReg, VTD_BF_CAP_REG_FRO);
3808 uint16_t const offIva = RT_BF_GET(pThis->fExtCapReg, VTD_BF_ECAP_REG_IRO);
3809 LogRel(("%s: VER=%u.%u CAP=%#RX64 ECAP=%#RX64 (MGAW=%u bits, SAGAW=%#x HAW_Base=%#RX64 MGAW_Inv=%#RX64 FRO=%#x IRO=%#x) mapped at %#RGp\n",
3810 DMAR_LOG_PFX, RT_BF_GET(uVerReg, VTD_BF_VER_REG_MAX), RT_BF_GET(uVerReg, VTD_BF_VER_REG_MIN),
3811 pThis->fCapReg, pThis->fExtCapReg, cMgawBits, fSagaw, pThis->fHawBaseMask, pThis->fInvMgawMask, offFrcd, offIva,
3812 DMAR_MMIO_BASE_PHYSADDR));
3813
3814 return VINF_SUCCESS;
3815}
3816
3817#else
3818
3819/**
3820 * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
3821 */
3822static DECLCALLBACK(int) iommuIntelRZConstruct(PPDMDEVINS pDevIns)
3823{
3824 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
3825 PDMAR pThis = PDMDEVINS_2_DATA(pDevIns, PDMAR);
3826 PDMARCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PDMARCC);
3827 pThisCC->CTX_SUFF(pDevIns) = pDevIns;
3828
3829 /* We will use PDM's critical section (via helpers) for the IOMMU device. */
3830 int rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
3831 AssertRCReturn(rc, rc);
3832
3833 /* Set up the MMIO RZ handlers. */
3834 rc = PDMDevHlpMmioSetUpContext(pDevIns, pThis->hMmio, dmarMmioWrite, dmarMmioRead, NULL /* pvUser */);
3835 AssertRCReturn(rc, rc);
3836
3837 /* Set up the IOMMU RZ callbacks. */
3838 PDMIOMMUREGCC IommuReg;
3839 RT_ZERO(IommuReg);
3840 IommuReg.u32Version = PDM_IOMMUREGCC_VERSION;
3841 IommuReg.idxIommu = pThis->idxIommu;
3842 IommuReg.pfnMemAccess = iommuIntelMemAccess;
3843 IommuReg.pfnMemBulkAccess = iommuIntelMemBulkAccess;
3844 IommuReg.pfnMsiRemap = iommuIntelMsiRemap;
3845 IommuReg.u32TheEnd = PDM_IOMMUREGCC_VERSION;
3846
3847 rc = PDMDevHlpIommuSetUpContext(pDevIns, &IommuReg, &pThisCC->CTX_SUFF(pIommuHlp));
3848 AssertRCReturn(rc, rc);
3849 AssertPtrReturn(pThisCC->CTX_SUFF(pIommuHlp), VERR_IOMMU_IPE_1);
3850 AssertReturn(pThisCC->CTX_SUFF(pIommuHlp)->u32Version == CTX_MID(PDM_IOMMUHLP,_VERSION), VERR_VERSION_MISMATCH);
3851 AssertReturn(pThisCC->CTX_SUFF(pIommuHlp)->u32TheEnd == CTX_MID(PDM_IOMMUHLP,_VERSION), VERR_VERSION_MISMATCH);
3852 AssertPtr(pThisCC->CTX_SUFF(pIommuHlp)->pfnLock);
3853 AssertPtr(pThisCC->CTX_SUFF(pIommuHlp)->pfnUnlock);
3854 AssertPtr(pThisCC->CTX_SUFF(pIommuHlp)->pfnLockIsOwner);
3855 AssertPtr(pThisCC->CTX_SUFF(pIommuHlp)->pfnSendMsi);
3856
3857 return VINF_SUCCESS;
3858}
3859
3860#endif
3861
3862
3863/**
3864 * The device registration structure.
3865 */
3866PDMDEVREG const g_DeviceIommuIntel =
3867{
3868 /* .u32Version = */ PDM_DEVREG_VERSION,
3869 /* .uReserved0 = */ 0,
3870 /* .szName = */ "iommu-intel",
3871 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE,
3872 /* .fClass = */ PDM_DEVREG_CLASS_PCI_BUILTIN,
3873 /* .cMaxInstances = */ 1,
3874 /* .uSharedVersion = */ 42,
3875 /* .cbInstanceShared = */ sizeof(DMAR),
3876 /* .cbInstanceCC = */ sizeof(DMARCC),
3877 /* .cbInstanceRC = */ sizeof(DMARRC),
3878 /* .cMaxPciDevices = */ 1,
3879 /* .cMaxMsixVectors = */ 0,
3880 /* .pszDescription = */ "IOMMU (Intel)",
3881#if defined(IN_RING3)
3882 /* .pszRCMod = */ "VBoxDDRC.rc",
3883 /* .pszR0Mod = */ "VBoxDDR0.r0",
3884 /* .pfnConstruct = */ iommuIntelR3Construct,
3885 /* .pfnDestruct = */ iommuIntelR3Destruct,
3886 /* .pfnRelocate = */ NULL,
3887 /* .pfnMemSetup = */ NULL,
3888 /* .pfnPowerOn = */ NULL,
3889 /* .pfnReset = */ iommuIntelR3Reset,
3890 /* .pfnSuspend = */ NULL,
3891 /* .pfnResume = */ NULL,
3892 /* .pfnAttach = */ NULL,
3893 /* .pfnDetach = */ NULL,
3894 /* .pfnQueryInterface = */ NULL,
3895 /* .pfnInitComplete = */ NULL,
3896 /* .pfnPowerOff = */ NULL,
3897 /* .pfnSoftReset = */ NULL,
3898 /* .pfnReserved0 = */ NULL,
3899 /* .pfnReserved1 = */ NULL,
3900 /* .pfnReserved2 = */ NULL,
3901 /* .pfnReserved3 = */ NULL,
3902 /* .pfnReserved4 = */ NULL,
3903 /* .pfnReserved5 = */ NULL,
3904 /* .pfnReserved6 = */ NULL,
3905 /* .pfnReserved7 = */ NULL,
3906#elif defined(IN_RING0)
3907 /* .pfnEarlyConstruct = */ NULL,
3908 /* .pfnConstruct = */ iommuIntelRZConstruct,
3909 /* .pfnDestruct = */ NULL,
3910 /* .pfnFinalDestruct = */ NULL,
3911 /* .pfnRequest = */ NULL,
3912 /* .pfnReserved0 = */ NULL,
3913 /* .pfnReserved1 = */ NULL,
3914 /* .pfnReserved2 = */ NULL,
3915 /* .pfnReserved3 = */ NULL,
3916 /* .pfnReserved4 = */ NULL,
3917 /* .pfnReserved5 = */ NULL,
3918 /* .pfnReserved6 = */ NULL,
3919 /* .pfnReserved7 = */ NULL,
3920#elif defined(IN_RC)
3921 /* .pfnConstruct = */ iommuIntelRZConstruct,
3922 /* .pfnReserved0 = */ NULL,
3923 /* .pfnReserved1 = */ NULL,
3924 /* .pfnReserved2 = */ NULL,
3925 /* .pfnReserved3 = */ NULL,
3926 /* .pfnReserved4 = */ NULL,
3927 /* .pfnReserved5 = */ NULL,
3928 /* .pfnReserved6 = */ NULL,
3929 /* .pfnReserved7 = */ NULL,
3930#else
3931# error "Not in IN_RING3, IN_RING0 or IN_RC!"
3932#endif
3933 /* .u32VersionEnd = */ PDM_DEVREG_VERSION
3934};
3935
3936#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
3937
Note: See TracBrowser for help on using the repository browser.

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