VirtualBox

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

Last change on this file since 89537 was 89537, checked in by vboxsync, 4 years ago

Intel IOMMU: bugref:9967 Doxygen.

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