VirtualBox

source: vbox/trunk/src/VBox/Devices/Bus/DevIommuAmd.cpp@ 87839

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

AMD IOMMU: bugref:9654 SSM bits.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 301.4 KB
Line 
1/* $Id: DevIommuAmd.cpp 87839 2021-02-23 07:30:40Z vboxsync $ */
2/** @file
3 * IOMMU - Input/Output Memory Management Unit - AMD implementation.
4 */
5
6/*
7 * Copyright (C) 2020 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 <VBox/msi.h>
24#include <VBox/iommu-amd.h>
25#include <VBox/vmm/pdmdev.h>
26#include <VBox/AssertGuest.h>
27
28#include <iprt/x86.h>
29#include <iprt/string.h>
30#include <iprt/avl.h>
31#ifdef IN_RING3
32# include <iprt/mem.h>
33#endif
34
35#include "VBoxDD.h"
36#include "DevIommuAmd.h"
37
38
39/*********************************************************************************************************************************
40* Defined Constants And Macros *
41*********************************************************************************************************************************/
42/** Release log prefix string. */
43#define IOMMU_LOG_PFX "AMD-IOMMU"
44/** The current saved state version. */
45#define IOMMU_SAVED_STATE_VERSION 1
46/** The IOMMU device instance magic. */
47#define IOMMU_MAGIC 0x10acce55
48
49/** Enable the IOTLBE cache only in ring-3 for now, see @bugref{9654#c95}. */
50#ifdef IN_RING3
51# define IOMMU_WITH_IOTLBE_CACHE
52#endif
53/** Enable the interrupt cache. */
54#define IOMMU_WITH_IRTE_CACHE
55
56/* The DTE cache is mandatory for the IOTLB or interrupt cache to work. */
57#if defined(IOMMU_WITH_IOTLBE_CACHE) || defined(IOMMU_WITH_IRTE_CACHE)
58# define IOMMU_WITH_DTE_CACHE
59/** The maximum number of device IDs in the cache. */
60# define IOMMU_DEV_CACHE_COUNT 16
61/** An empty device ID. */
62# define IOMMU_DTE_CACHE_KEY_NIL 0
63#endif
64
65#ifdef IOMMU_WITH_IRTE_CACHE
66/** The maximum number of IRTE cache entries. */
67# define IOMMU_IRTE_CACHE_COUNT 32
68/** A NIL IRTE cache entry key. */
69# define IOMMU_IRTE_CACHE_KEY_NIL (~(uint32_t)0U)
70/** Gets the device ID from an IRTE cache entry key. */
71#define IOMMU_IRTE_CACHE_KEY_GET_DEVICE_ID(a_Key) RT_HIWORD(a_Key)
72/** Gets the IOVA from the IOTLB entry key. */
73# define IOMMU_IRTE_CACHE_KEY_GET_OFF(a_Key) RT_LOWORD(a_Key)
74/** Makes an IRTE cache entry key.
75 *
76 * Bits 31:16 is the device ID (Bus, Device, Function).
77 * Bits 15:0 is the the offset into the IRTE table.
78 */
79# define IOMMU_IRTE_CACHE_KEY_MAKE(a_DevId, a_off) RT_MAKE_U32(a_off, a_DevId)
80#endif /* IOMMU_WITH_IRTE_CACHE */
81
82#ifdef IOMMU_WITH_IOTLBE_CACHE
83/** The maximum number of IOTLB entries. */
84# define IOMMU_IOTLBE_MAX 96
85/** The mask of bits covering the domain ID in the IOTLBE key. */
86# define IOMMU_IOTLB_DOMAIN_ID_MASK UINT64_C(0xffffff0000000000)
87/** The mask of bits covering the IOVA in the IOTLBE key. */
88# define IOMMU_IOTLB_IOVA_MASK (~IOMMU_IOTLB_DOMAIN_ID_MASK)
89/** The number of bits to shift for the domain ID of the IOTLBE key. */
90# define IOMMU_IOTLB_DOMAIN_ID_SHIFT 40
91/** A NIL IOTLB key. */
92# define IOMMU_IOTLB_KEY_NIL UINT64_C(0)
93/** Gets the domain ID from an IOTLB entry key. */
94# define IOMMU_IOTLB_KEY_GET_DOMAIN_ID(a_Key) ((a_Key) >> IOMMU_IOTLB_DOMAIN_ID_SHIFT)
95/** Gets the IOVA from the IOTLB entry key. */
96# define IOMMU_IOTLB_KEY_GET_IOVA(a_Key) (((a_Key) & IOMMU_IOTLB_IOVA_MASK) << X86_PAGE_4K_SHIFT)
97/** Makes an IOTLB entry key.
98 *
99 * Address bits 63:52 of the IOVA are zero extended, so top 12 bits are free.
100 * Address bits 11:0 of the IOVA are offset into the minimum page size of 4K,
101 * so bottom 12 bits are free.
102 *
103 * Thus we use the top 24 bits of key to hold bits 15:0 of the domain ID.
104 * We use the bottom 40 bits of the key to hold bits 51:12 of the IOVA.
105 */
106# define IOMMU_IOTLB_KEY_MAKE(a_DomainId, a_uIova) ( ((uint64_t)(a_DomainId) << IOMMU_IOTLB_DOMAIN_ID_SHIFT) \
107 | (((a_uIova) >> X86_PAGE_4K_SHIFT) & IOMMU_IOTLB_IOVA_MASK))
108#endif /* IOMMU_WITH_IOTLBE_CACHE */
109
110#ifdef IOMMU_WITH_DTE_CACHE
111/** @name IOMMU_DTE_CACHE_F_XXX: DTE cache flags.
112 *
113 * Some of these flags are "basic" i.e. they correspond directly to their bits in
114 * the DTE. The rest of the flags are based on checks or operations on several DTE
115 * bits.
116 *
117 * The basic flags are:
118 * - VALID (DTE.V)
119 * - IO_PERM_READ (DTE.IR)
120 * - IO_PERM_WRITE (DTE.IW)
121 * - IO_PERM_RSVD (bit following DTW.IW reserved for future & to keep
122 * masking consistent)
123 * - SUPPRESS_ALL_IOPF (DTE.SA)
124 * - SUPPRESS_IOPF (DTE.SE)
125 * - INTR_MAP_VALID (DTE.IV)
126 * - IGNORE_UNMAPPED_INTR (DTE.IG)
127 *
128 * @see iommuAmdGetBasicDevFlags()
129 * @{ */
130/** The DTE is present. */
131# define IOMMU_DTE_CACHE_F_PRESENT RT_BIT(0)
132/** The DTE is valid. */
133# define IOMMU_DTE_CACHE_F_VALID RT_BIT(1)
134/** The DTE permissions apply for address translations. */
135# define IOMMU_DTE_CACHE_F_IO_PERM RT_BIT(2)
136/** DTE permission - I/O read allowed. */
137# define IOMMU_DTE_CACHE_F_IO_PERM_READ RT_BIT(3)
138/** DTE permission - I/O write allowed. */
139# define IOMMU_DTE_CACHE_F_IO_PERM_WRITE RT_BIT(4)
140/** DTE permission - reserved. */
141# define IOMMU_DTE_CACHE_F_IO_PERM_RSVD RT_BIT(5)
142/** Address translation required. */
143# define IOMMU_DTE_CACHE_F_ADDR_TRANSLATE RT_BIT(6)
144/** Suppress all I/O page faults. */
145# define IOMMU_DTE_CACHE_F_SUPPRESS_ALL_IOPF RT_BIT(7)
146/** Suppress I/O page faults. */
147# define IOMMU_DTE_CACHE_F_SUPPRESS_IOPF RT_BIT(8)
148/** Interrupt map valid. */
149# define IOMMU_DTE_CACHE_F_INTR_MAP_VALID RT_BIT(9)
150/** Ignore unmapped interrupts. */
151# define IOMMU_DTE_CACHE_F_IGNORE_UNMAPPED_INTR RT_BIT(10)
152/** An I/O page fault has been raised for this device. */
153# define IOMMU_DTE_CACHE_F_IO_PAGE_FAULT_RAISED RT_BIT(11)
154/** Fixed and arbitrary interrupt control: Target Abort. */
155# define IOMMU_DTE_CACHE_F_INTR_CTRL_TARGET_ABORT RT_BIT(12)
156/** Fixed and arbitrary interrupt control: Forward unmapped. */
157# define IOMMU_DTE_CACHE_F_INTR_CTRL_FWD_UNMAPPED RT_BIT(13)
158/** Fixed and arbitrary interrupt control: Remapped. */
159# define IOMMU_DTE_CACHE_F_INTR_CTRL_REMAPPED RT_BIT(14)
160/** Fixed and arbitrary interrupt control: Reserved. */
161# define IOMMU_DTE_CACHE_F_INTR_CTRL_RSVD RT_BIT(15)
162/** @} */
163
164/** The number of bits to shift I/O device flags for DTE permissions. */
165# define IOMMU_DTE_CACHE_F_IO_PERM_SHIFT 3
166/** The mask of DTE permissions in I/O device flags. */
167# define IOMMU_DTE_CACHE_F_IO_PERM_MASK 0x3
168/** The number of bits to shift I/O device flags for interrupt control bits. */
169# define IOMMU_DTE_CACHE_F_INTR_CTRL_SHIFT 12
170/** The mask of interrupt control bits in I/O device flags. */
171# define IOMMU_DTE_CACHE_F_INTR_CTRL_MASK 0x3
172/** The number of bits to shift for ignore-unmapped interrupts bit. */
173# define IOMMU_DTE_CACHE_F_IGNORE_UNMAPPED_INTR_SHIFT 10
174
175/** Acquires the cache lock. */
176# define IOMMU_LOCK_CACHE(a_pDevIns, a_pThis) \
177 do { \
178 int const rcLock = PDMDevHlpCritSectEnter((a_pDevIns), &(a_pThis)->CritSectCache, VERR_SEM_BUSY); \
179 if (rcLock == VINF_SUCCESS) \
180 { /* likely */ } \
181 else \
182 { \
183 AssertRC(rcLock); \
184 return rcLock; \
185 } \
186 } while (0)
187
188/** Acquires the cache lock (asserts on failure). */
189# define IOMMU_LOCK_CACHE_NORET(a_pDevIns, a_pThis) \
190 do { \
191 int const rcLock = PDMDevHlpCritSectEnter((a_pDevIns), &(a_pThis)->CritSectCache, VERR_SEM_BUSY); \
192 AssertRC(rcLock); \
193 } while (0)
194
195/** Releases the cache lock. */
196# define IOMMU_UNLOCK_CACHE(a_pDevIns, a_pThis) PDMDevHlpCritSectLeave((a_pDevIns), &(a_pThis)->CritSectCache)
197#endif /* IOMMU_WITH_DTE_CACHE */
198
199/** Gets the page offset mask given the number of bits to shift. */
200#define IOMMU_GET_PAGE_OFF_MASK(a_cShift) (~(UINT64_C(0xffffffffffffffff) << (a_cShift)))
201
202/** Acquires the PDM lock. */
203#define IOMMU_LOCK(a_pDevIns, a_pThisCC) \
204 do { \
205 int const rcLock = (a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnLock((a_pDevIns), VERR_SEM_BUSY); \
206 if (RT_LIKELY(rcLock == VINF_SUCCESS)) \
207 { /* likely */ } \
208 else \
209 return rcLock; \
210 } while (0)
211
212/** Acquires the PDM lock (asserts on failure). */
213#define IOMMU_LOCK_NORET(a_pDevIns, a_pThisCC) \
214 do { \
215 int const rcLock = (a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnLock((a_pDevIns), VERR_SEM_BUSY); \
216 AssertRC(rcLock); \
217 } while (0)
218
219/** Releases the PDM lock. */
220# define IOMMU_UNLOCK(a_pDevIns, a_pThisCC) (a_pThisCC)->CTX_SUFF(pIommuHlp)->pfnUnlock((a_pDevIns))
221
222
223/*********************************************************************************************************************************
224* Structures and Typedefs *
225*********************************************************************************************************************************/
226/**
227 * IOMMU operation (transaction).
228 */
229typedef enum IOMMUOP
230{
231 /** Address translation request. */
232 IOMMUOP_TRANSLATE_REQ = 0,
233 /** Memory read request. */
234 IOMMUOP_MEM_READ,
235 /** Memory write request. */
236 IOMMUOP_MEM_WRITE,
237 /** Interrupt request. */
238 IOMMUOP_INTR_REQ,
239 /** Command. */
240 IOMMUOP_CMD
241} IOMMUOP;
242/** Pointer to a IOMMU operation. */
243typedef IOMMUOP *PIOMMUOP;
244
245/**
246 * I/O page lookup.
247 */
248typedef struct IOPAGELOOKUP
249{
250 /** The translated system physical address. */
251 RTGCPHYS GCPhysSpa;
252 /** The number of offset bits in the system physical address. */
253 uint8_t cShift;
254 /** The I/O permissions for this translation, see IOMMU_IO_PERM_XXX. */
255 uint8_t fPerm;
256} IOPAGELOOKUP;
257/** Pointer to an I/O page lookup. */
258typedef IOPAGELOOKUP *PIOPAGELOOKUP;
259/** Pointer to a const I/O page lookup. */
260typedef IOPAGELOOKUP const *PCIOPAGELOOKUP;
261
262/**
263 * I/O address range.
264 */
265typedef struct IOADDRRANGE
266{
267 /** The address (virtual or physical). */
268 uint64_t uAddr;
269 /** The size of the access in bytes. */
270 size_t cb;
271 /** The I/O permissions for this translation, see IOMMU_IO_PERM_XXX. */
272 uint8_t fPerm;
273} IOADDRRANGE;
274/** Pointer to an I/O address range. */
275typedef IOADDRRANGE *PIOADDRRANGE;
276/** Pointer to a const I/O address range. */
277typedef IOADDRRANGE const *PCIOADDRRANGE;
278
279#ifdef IOMMU_WITH_DTE_CACHE
280/**
281 * Device Table Entry Cache.
282 */
283typedef struct DTECACHE
284{
285 /** This device's flags, see IOMMU_DTE_CACHE_F_XXX. */
286 uint16_t fFlags;
287 /** The domain ID assigned for this device by software. */
288 uint16_t uDomainId;
289} DTECACHE;
290/** Pointer to an I/O device struct. */
291typedef DTECACHE *PDTECACHE;
292/** Pointer to a const I/O device struct. */
293typedef DTECACHE *PCDTECACHE;
294AssertCompileSize(DTECACHE, 4);
295#endif /* IOMMU_WITH_DTE_CACHE */
296
297#ifdef IOMMU_WITH_IOTLBE_CACHE
298/**
299 * I/O TLB Entry.
300 * Keep this as small and aligned as possible.
301 */
302typedef struct IOTLBE
303{
304 /** The AVL tree node. */
305 AVLU64NODECORE Core;
306 /** The least recently used (LRU) list node. */
307 RTLISTNODE NdLru;
308 /** The I/O page lookup results of the translation. */
309 IOPAGELOOKUP PageLookup;
310 /** Whether the entry needs to be evicted from the cache. */
311 bool fEvictPending;
312} IOTLBE;
313/** Pointer to an IOMMU I/O TLB entry struct. */
314typedef IOTLBE *PIOTLBE;
315/** Pointer to a const IOMMU I/O TLB entry struct. */
316typedef IOTLBE const *PCIOTLBE;
317AssertCompileSizeAlignment(IOTLBE, 8);
318AssertCompileMemberOffset(IOTLBE, Core, 0);
319#endif /* IOMMU_WITH_IOTLBE_CACHE */
320
321#ifdef IOMMU_WITH_IRTE_CACHE
322/**
323 * Interrupt Remap Table Entry Cache.
324 */
325typedef struct IRTECACHE
326{
327 /** The key, see IOMMU_IRTE_CACHE_KEY_MAKE. */
328 uint32_t uKey;
329 /** The IRTE. */
330 IRTE_T Irte;
331} IRTECACHE;
332/** Pointer to an IRTE cache struct. */
333typedef IRTECACHE *PIRTECACHE;
334/** Pointer to a const IRTE cache struct. */
335typedef IRTECACHE const *PCIRTECACHE;
336AssertCompileSizeAlignment(IRTECACHE, 4);
337#endif /* IOMMU_WITH_IRTE_CACHE */
338
339/**
340 * The shared IOMMU device state.
341 */
342typedef struct IOMMU
343{
344 /** IOMMU device index (0 is at the top of the PCI tree hierarchy). */
345 uint32_t idxIommu;
346 /** IOMMU magic. */
347 uint32_t u32Magic;
348
349 /** The MMIO handle. */
350 IOMMMIOHANDLE hMmio;
351 /** The event semaphore the command thread waits on. */
352 SUPSEMEVENT hEvtCmdThread;
353 /** Whether the command thread has been signaled for wake up. */
354 bool volatile fCmdThreadSignaled;
355 /** Padding. */
356 bool afPadding0[7];
357
358#ifdef IOMMU_WITH_DTE_CACHE
359 /** The critsect that protects the cache from concurrent access. */
360 PDMCRITSECT CritSectCache;
361 /** Array of device IDs. */
362 uint16_t aDeviceIds[IOMMU_DEV_CACHE_COUNT];
363 /** Array of DTE cache entries. */
364 DTECACHE aDteCache[IOMMU_DEV_CACHE_COUNT];
365#endif
366#ifdef IOMMU_WITH_IRTE_CACHE
367 /** Array of IRTE cache entries. */
368 IRTECACHE aIrteCache[IOMMU_IRTE_CACHE_COUNT];
369#endif
370
371 /** @name PCI: Base capability block registers.
372 * @{ */
373 IOMMU_BAR_T IommuBar; /**< IOMMU base address register. */
374 /** @} */
375
376 /** @name MMIO: Control and status registers.
377 * @{ */
378 DEV_TAB_BAR_T aDevTabBaseAddrs[8]; /**< Device table base address registers. */
379 CMD_BUF_BAR_T CmdBufBaseAddr; /**< Command buffer base address register. */
380 EVT_LOG_BAR_T EvtLogBaseAddr; /**< Event log base address register. */
381 IOMMU_CTRL_T Ctrl; /**< IOMMU control register. */
382 IOMMU_EXCL_RANGE_BAR_T ExclRangeBaseAddr; /**< IOMMU exclusion range base register. */
383 IOMMU_EXCL_RANGE_LIMIT_T ExclRangeLimit; /**< IOMMU exclusion range limit. */
384 IOMMU_EXT_FEAT_T ExtFeat; /**< IOMMU extended feature register. */
385 /** @} */
386
387 /** @name MMIO: Peripheral Page Request (PPR) Log registers.
388 * @{ */
389 PPR_LOG_BAR_T PprLogBaseAddr; /**< PPR Log base address register. */
390 IOMMU_HW_EVT_HI_T HwEvtHi; /**< IOMMU hardware event register (Hi). */
391 IOMMU_HW_EVT_LO_T HwEvtLo; /**< IOMMU hardware event register (Lo). */
392 IOMMU_HW_EVT_STATUS_T HwEvtStatus; /**< IOMMU hardware event status. */
393 /** @} */
394
395 /** @todo IOMMU: SMI filter. */
396
397 /** @name MMIO: Guest Virtual-APIC Log registers.
398 * @{ */
399 GALOG_BAR_T GALogBaseAddr; /**< Guest Virtual-APIC Log base address register. */
400 GALOG_TAIL_ADDR_T GALogTailAddr; /**< Guest Virtual-APIC Log Tail address register. */
401 /** @} */
402
403 /** @name MMIO: Alternate PPR and Event Log registers.
404 * @{ */
405 PPR_LOG_B_BAR_T PprLogBBaseAddr; /**< PPR Log B base address register. */
406 EVT_LOG_B_BAR_T EvtLogBBaseAddr; /**< Event Log B base address register. */
407 /** @} */
408
409 /** @name MMIO: Device-specific feature registers.
410 * @{ */
411 DEV_SPECIFIC_FEAT_T DevSpecificFeat; /**< Device-specific feature extension register (DSFX). */
412 DEV_SPECIFIC_CTRL_T DevSpecificCtrl; /**< Device-specific control extension register (DSCX). */
413 DEV_SPECIFIC_STATUS_T DevSpecificStatus; /**< Device-specific status extension register (DSSX). */
414 /** @} */
415
416 /** @name MMIO: MSI Capability Block registers.
417 * @{ */
418 MSI_MISC_INFO_T MiscInfo; /**< MSI Misc. info registers / MSI Vector registers. */
419 /** @} */
420
421 /** @name MMIO: Performance Optimization Control registers.
422 * @{ */
423 IOMMU_PERF_OPT_CTRL_T PerfOptCtrl; /**< IOMMU Performance optimization control register. */
424 /** @} */
425
426 /** @name MMIO: x2APIC Control registers.
427 * @{ */
428 IOMMU_XT_GEN_INTR_CTRL_T XtGenIntrCtrl; /**< IOMMU X2APIC General interrupt control register. */
429 IOMMU_XT_PPR_INTR_CTRL_T XtPprIntrCtrl; /**< IOMMU X2APIC PPR interrupt control register. */
430 IOMMU_XT_GALOG_INTR_CTRL_T XtGALogIntrCtrl; /**< IOMMU X2APIC Guest Log interrupt control register. */
431 /** @} */
432
433 /** @name MMIO: Memory Address Routing & Control (MARC) registers.
434 * @{ */
435 MARC_APER_T aMarcApers[4]; /**< MARC Aperture Registers. */
436 /** @} */
437
438 /** @name MMIO: Reserved register.
439 * @{ */
440 IOMMU_RSVD_REG_T RsvdReg; /**< IOMMU Reserved Register. */
441 /** @} */
442
443 /** @name MMIO: Command and Event Log pointer registers.
444 * @{ */
445 CMD_BUF_HEAD_PTR_T CmdBufHeadPtr; /**< Command buffer head pointer register. */
446 CMD_BUF_TAIL_PTR_T CmdBufTailPtr; /**< Command buffer tail pointer register. */
447 EVT_LOG_HEAD_PTR_T EvtLogHeadPtr; /**< Event log head pointer register. */
448 EVT_LOG_TAIL_PTR_T EvtLogTailPtr; /**< Event log tail pointer register. */
449 /** @} */
450
451 /** @name MMIO: Command and Event Status register.
452 * @{ */
453 IOMMU_STATUS_T Status; /**< IOMMU status register. */
454 /** @} */
455
456 /** @name MMIO: PPR Log Head and Tail pointer registers.
457 * @{ */
458 PPR_LOG_HEAD_PTR_T PprLogHeadPtr; /**< IOMMU PPR log head pointer register. */
459 PPR_LOG_TAIL_PTR_T PprLogTailPtr; /**< IOMMU PPR log tail pointer register. */
460 /** @} */
461
462 /** @name MMIO: Guest Virtual-APIC Log Head and Tail pointer registers.
463 * @{ */
464 GALOG_HEAD_PTR_T GALogHeadPtr; /**< Guest Virtual-APIC log head pointer register. */
465 GALOG_TAIL_PTR_T GALogTailPtr; /**< Guest Virtual-APIC log tail pointer register. */
466 /** @} */
467
468 /** @name MMIO: PPR Log B Head and Tail pointer registers.
469 * @{ */
470 PPR_LOG_B_HEAD_PTR_T PprLogBHeadPtr; /**< PPR log B head pointer register. */
471 PPR_LOG_B_TAIL_PTR_T PprLogBTailPtr; /**< PPR log B tail pointer register. */
472 /** @} */
473
474 /** @name MMIO: Event Log B Head and Tail pointer registers.
475 * @{ */
476 EVT_LOG_B_HEAD_PTR_T EvtLogBHeadPtr; /**< Event log B head pointer register. */
477 EVT_LOG_B_TAIL_PTR_T EvtLogBTailPtr; /**< Event log B tail pointer register. */
478 /** @} */
479
480 /** @name MMIO: PPR Log Overflow protection registers.
481 * @{ */
482 PPR_LOG_AUTO_RESP_T PprLogAutoResp; /**< PPR Log Auto Response register. */
483 PPR_LOG_OVERFLOW_EARLY_T PprLogOverflowEarly; /**< PPR Log Overflow Early Indicator register. */
484 PPR_LOG_B_OVERFLOW_EARLY_T PprLogBOverflowEarly; /**< PPR Log B Overflow Early Indicator register. */
485 /** @} */
486
487 /** @todo IOMMU: IOMMU Event counter registers. */
488
489#ifdef VBOX_WITH_STATISTICS
490 /** @name IOMMU: Stat counters.
491 * @{ */
492 STAMCOUNTER StatMmioReadR3; /**< Number of MMIO reads in R3. */
493 STAMCOUNTER StatMmioReadRZ; /**< Number of MMIO reads in RZ. */
494 STAMCOUNTER StatMmioWriteR3; /**< Number of MMIO writes in R3. */
495 STAMCOUNTER StatMmioWriteRZ; /**< Number of MMIO writes in RZ. */
496
497 STAMCOUNTER StatMsiRemapR3; /**< Number of MSI remap requests in R3. */
498 STAMCOUNTER StatMsiRemapRZ; /**< Number of MSI remap requests in RZ. */
499
500 STAMCOUNTER StatMemReadR3; /**< Number of memory read translation requests in R3. */
501 STAMCOUNTER StatMemReadRZ; /**< Number of memory read translation requests in RZ. */
502 STAMCOUNTER StatMemWriteR3; /**< Number of memory write translation requests in R3. */
503 STAMCOUNTER StatMemWriteRZ; /**< Number of memory write translation requests in RZ. */
504
505 STAMCOUNTER StatMemBulkReadR3; /**< Number of memory read bulk translation requests in R3. */
506 STAMCOUNTER StatMemBulkReadRZ; /**< Number of memory read bulk translation requests in RZ. */
507 STAMCOUNTER StatMemBulkWriteR3; /**< Number of memory write bulk translation requests in R3. */
508 STAMCOUNTER StatMemBulkWriteRZ; /**< Number of memory write bulk translation requests in RZ. */
509
510 STAMCOUNTER StatCmd; /**< Number of commands processed in total. */
511 STAMCOUNTER StatCmdCompWait; /**< Number of Completion Wait commands processed. */
512 STAMCOUNTER StatCmdInvDte; /**< Number of Invalidate DTE commands processed. */
513 STAMCOUNTER StatCmdInvIommuPages; /**< Number of Invalidate IOMMU pages commands processed. */
514 STAMCOUNTER StatCmdInvIotlbPages; /**< Number of Invalidate IOTLB pages commands processed. */
515 STAMCOUNTER StatCmdInvIntrTable; /**< Number of Invalidate Interrupt Table commands processed. */
516 STAMCOUNTER StatCmdPrefIommuPages; /**< Number of Prefetch IOMMU Pages commands processed. */
517 STAMCOUNTER StatCmdCompletePprReq; /**< Number of Complete PPR Requests commands processed. */
518 STAMCOUNTER StatCmdInvIommuAll; /**< Number of Invalidate IOMMU All commands processed. */
519
520 STAMCOUNTER StatIotlbeCached; /**< Number of IOTLB entries in the cache. */
521 STAMCOUNTER StatIotlbeLazyEvictReuse; /**< Number of IOTLB entries re-used after lazy eviction. */
522
523 STAMPROFILEADV StatProfDteLookup; /**< Profiling of I/O page walk (from memory). */
524 STAMPROFILEADV StatProfIotlbeLookup; /**< Profiling of IOTLB entry lookup (from cache). */
525
526 STAMPROFILEADV StatProfIrteLookup; /**< Profiling of IRTE entry lookup (from memory). */
527 STAMPROFILEADV StatProfIrteCacheLookup; /**< Profiling of IRTE entry lookup (from cache). */
528
529 STAMCOUNTER StatAccessCacheHit; /**< Number of IOTLB cache hits. */
530 STAMCOUNTER StatAccessCacheHitFull; /**< Number of accesses that were fully looked up from the cache. */
531 STAMCOUNTER StatAccessCacheMiss; /**< Number of cache misses (resulting in DTE lookups). */
532 STAMCOUNTER StatAccessCacheNonContig; /**< Number of cache accesses resulting in non-contiguous access. */
533 STAMCOUNTER StatAccessCachePermDenied; /**< Number of cache accesses resulting in insufficient permissions. */
534 STAMCOUNTER StatAccessDteNonContig; /**< Number of DTE accesses resulting in non-contiguous access. */
535 STAMCOUNTER StatAccessDtePermDenied; /**< Number of DTE accesses resulting in insufficient permissions. */
536
537 STAMCOUNTER StatIntrCacheHit; /**< Number of interrupt cache hits. */
538 STAMCOUNTER StatIntrCacheMiss; /**< Number of interrupt cache misses. */
539 /** @} */
540#endif
541} IOMMU;
542/** Pointer to the IOMMU device state. */
543typedef IOMMU *PIOMMU;
544/** Pointer to the const IOMMU device state. */
545typedef const IOMMU *PCIOMMU;
546AssertCompileMemberAlignment(IOMMU, hMmio, 8);
547#ifdef IOMMU_WITH_DTE_CACHE
548AssertCompileMemberAlignment(IOMMU, CritSectCache, 8);
549AssertCompileMemberAlignment(IOMMU, aDeviceIds, 8);
550AssertCompileMemberAlignment(IOMMU, aDteCache, 8);
551#endif
552#ifdef IOMMU_WITH_IRTE_CACHE
553AssertCompileMemberAlignment(IOMMU, aIrteCache, 8);
554#endif
555AssertCompileMemberAlignment(IOMMU, IommuBar, 8);
556AssertCompileMemberAlignment(IOMMU, aDevTabBaseAddrs, 8);
557AssertCompileMemberAlignment(IOMMU, CmdBufHeadPtr, 8);
558AssertCompileMemberAlignment(IOMMU, Status, 8);
559
560/**
561 * The ring-3 IOMMU device state.
562 */
563typedef struct IOMMUR3
564{
565 /** Device instance. */
566 PPDMDEVINSR3 pDevInsR3;
567 /** The IOMMU helpers. */
568 R3PTRTYPE(PCPDMIOMMUHLPR3) pIommuHlpR3;
569 /** The command thread handle. */
570 R3PTRTYPE(PPDMTHREAD) pCmdThread;
571#ifdef IOMMU_WITH_IOTLBE_CACHE
572 /** Pointer to array of pre-allocated IOTLBEs. */
573 PIOTLBE paIotlbes;
574 /** Maps [DomainId,Iova] to [IOTLBE]. */
575 AVLU64TREE TreeIotlbe;
576 /** LRU list anchor for IOTLB entries. */
577 RTLISTANCHOR LstLruIotlbe;
578 /** Index of the next unused IOTLB. */
579 uint32_t idxUnusedIotlbe;
580 /** Number of cached IOTLB entries in the tree. */
581 uint32_t cCachedIotlbes;
582#endif
583} IOMMUR3;
584/** Pointer to the ring-3 IOMMU device state. */
585typedef IOMMUR3 *PIOMMUR3;
586/** Pointer to the const ring-3 IOMMU device state. */
587typedef const IOMMUR3 *PCIOMMUR3;
588#ifdef IOMMU_WITH_IOTLBE_CACHE
589AssertCompileMemberAlignment(IOMMUR3, paIotlbes, 8);
590AssertCompileMemberAlignment(IOMMUR3, TreeIotlbe, 8);
591AssertCompileMemberAlignment(IOMMUR3, LstLruIotlbe, 8);
592#endif
593
594/**
595 * The ring-0 IOMMU device state.
596 */
597typedef struct IOMMUR0
598{
599 /** Device instance. */
600 PPDMDEVINSR0 pDevInsR0;
601 /** The IOMMU helpers. */
602 R0PTRTYPE(PCPDMIOMMUHLPR0) pIommuHlpR0;
603} IOMMUR0;
604/** Pointer to the ring-0 IOMMU device state. */
605typedef IOMMUR0 *PIOMMUR0;
606
607/**
608 * The raw-mode IOMMU device state.
609 */
610typedef struct IOMMURC
611{
612 /** Device instance. */
613 PPDMDEVINSRC pDevInsRC;
614 /** The IOMMU helpers. */
615 RCPTRTYPE(PCPDMIOMMUHLPRC) pIommuHlpRC;
616} IOMMURC;
617/** Pointer to the raw-mode IOMMU device state. */
618typedef IOMMURC *PIOMMURC;
619
620/** The IOMMU device state for the current context. */
621typedef CTX_SUFF(IOMMU) IOMMUCC;
622/** Pointer to the IOMMU device state for the current context. */
623typedef CTX_SUFF(PIOMMU) PIOMMUCC;
624
625/**
626 * IOMMU register access.
627 */
628typedef struct IOMMUREGACC
629{
630 const char *pszName;
631 VBOXSTRICTRC (*pfnRead)(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value);
632 VBOXSTRICTRC (*pfnWrite)(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value);
633} IOMMUREGACC;
634/** Pointer to an IOMMU register access. */
635typedef IOMMUREGACC *PIOMMUREGACC;
636/** Pointer to a const IOMMU register access. */
637typedef IOMMUREGACC const *PCIOMMUREGACC;
638
639#ifdef IOMMU_WITH_IOTLBE_CACHE
640/**
641 * IOTLBE flush argument.
642 */
643typedef struct IOTLBEFLUSHARG
644{
645 /** The ring-3 IOMMU device state. */
646 PIOMMUR3 pIommuR3;
647 /** The domain ID to flush. */
648 uint16_t uDomainId;
649} IOTLBEFLUSHARG;
650/** Pointer to an IOTLBE flush argument. */
651typedef IOTLBEFLUSHARG *PIOTLBEFLUSHARG;
652/** Pointer to a const IOTLBE flush argument. */
653typedef IOTLBEFLUSHARG const *PCIOTLBEFLUSHARG;
654
655/**
656 * IOTLBE Info. argument.
657 */
658typedef struct IOTLBEINFOARG
659{
660 /** The ring-3 IOMMU device state. */
661 PIOMMUR3 pIommuR3;
662 /** The info helper. */
663 PCDBGFINFOHLP pHlp;
664 /** The domain ID to dump IOTLB entry. */
665 uint16_t uDomainId;
666} IOTLBEINFOARG;
667/** Pointer to an IOTLBE flush argument. */
668typedef IOTLBEINFOARG *PIOTLBEINFOARG;
669/** Pointer to a const IOTLBE flush argument. */
670typedef IOTLBEINFOARG const *PCIOTLBEINFOARG;
671#endif
672
673/**
674 * IOMMU operation auxiliary info.
675 */
676typedef struct IOMMUOPAUX
677{
678 /** The IOMMU operation being performed. */
679 IOMMUOP enmOp;
680 /** The device table entry (can be NULL). */
681 PCDTE_T pDte;
682 /** The device ID. */
683 uint16_t uDeviceId;
684 /** The domain ID (when the DTE isn't provided). */
685 uint16_t uDomainId;
686} IOMMUOPAUX;
687/** Pointer to an I/O address lookup struct. */
688typedef IOMMUOPAUX *PIOMMUOPAUX;
689/** Pointer to a const I/O address lookup struct. */
690typedef IOMMUOPAUX const *PCIOMMUOPAUX;
691
692typedef DECLCALLBACKTYPE(int, FNIOPAGELOOKUP,(PPDMDEVINS pDevIns, uint64_t uIovaPage, uint8_t fPerm, PCIOMMUOPAUX pAux,
693 PIOPAGELOOKUP pPageLookup));
694typedef FNIOPAGELOOKUP *PFNIOPAGELOOKUP;
695
696
697/*********************************************************************************************************************************
698* Global Variables *
699*********************************************************************************************************************************/
700/**
701 * An array of the number of device table segments supported.
702 * Indexed by u2DevTabSegSup.
703 */
704static uint8_t const g_acDevTabSegs[] = { 0, 2, 4, 8 };
705
706/**
707 * An array of the masks to select the device table segment index from a device ID.
708 */
709static uint16_t const g_auDevTabSegMasks[] = { 0x0, 0x8000, 0xc000, 0xe000 };
710
711/**
712 * An array of the shift values to select the device table segment index from a
713 * device ID.
714 */
715static uint8_t const g_auDevTabSegShifts[] = { 0, 15, 14, 13 };
716
717/**
718 * The maximum size (inclusive) of each device table segment (0 to 7).
719 * Indexed by the device table segment index.
720 */
721static uint16_t const g_auDevTabSegMaxSizes[] = { 0x1ff, 0xff, 0x7f, 0x7f, 0x3f, 0x3f, 0x3f, 0x3f };
722
723/**
724 * The IOMMU I/O permission names.
725 */
726static const char * const g_aszPerm[] = { "none", "read", "write", "read+write" };
727
728
729#ifndef VBOX_DEVICE_STRUCT_TESTCASE
730/**
731 * Gets the maximum number of buffer entries for the given buffer length.
732 *
733 * @returns Number of buffer entries.
734 * @param uEncodedLen The length (power-of-2 encoded).
735 */
736DECLINLINE(uint32_t) iommuAmdGetBufMaxEntries(uint8_t uEncodedLen)
737{
738 Assert(uEncodedLen > 7);
739 Assert(uEncodedLen < 16);
740 return 2 << (uEncodedLen - 1);
741}
742
743
744/**
745 * Gets the total length of the buffer given a base register's encoded length.
746 *
747 * @returns The length of the buffer in bytes.
748 * @param uEncodedLen The length (power-of-2 encoded).
749 */
750DECLINLINE(uint32_t) iommuAmdGetTotalBufLength(uint8_t uEncodedLen)
751{
752 Assert(uEncodedLen > 7);
753 Assert(uEncodedLen < 16);
754 return (2 << (uEncodedLen - 1)) << 4;
755}
756
757
758/**
759 * Gets the number of (unconsumed) entries in the event log.
760 *
761 * @returns The number of entries in the event log.
762 * @param pThis The shared IOMMU device state.
763 */
764static uint32_t iommuAmdGetEvtLogEntryCount(PIOMMU pThis)
765{
766 uint32_t const idxTail = pThis->EvtLogTailPtr.n.off >> IOMMU_EVT_GENERIC_SHIFT;
767 uint32_t const idxHead = pThis->EvtLogHeadPtr.n.off >> IOMMU_EVT_GENERIC_SHIFT;
768 if (idxTail >= idxHead)
769 return idxTail - idxHead;
770
771 uint32_t const cMaxEvts = iommuAmdGetBufMaxEntries(pThis->EvtLogBaseAddr.n.u4Len);
772 return cMaxEvts - idxHead + idxTail;
773}
774
775
776/**
777 * Gets the descriptive I/O permission name for a memory access.
778 *
779 * @returns The I/O permission name.
780 * @param fPerm The I/O permissions for the access, see IOMMU_IO_PERM_XXX.
781 */
782static const char *iommuAmdMemAccessGetPermName(uint8_t fPerm)
783{
784 /* We shouldn't construct an access with "none" or "read+write" (must be read or write) permissions. */
785 Assert(fPerm > 0 && fPerm < RT_ELEMENTS(g_aszPerm));
786 return g_aszPerm[fPerm & IOMMU_IO_PERM_MASK];
787}
788
789
790/**
791 * Checks whether two consecutive I/O page lookup results translates to a physically
792 * contiguous region.
793 *
794 * @returns @c true if they are contiguous, @c false otherwise.
795 * @param pPageLookupPrev The I/O page lookup result of the previous page.
796 * @param pPageLookup The I/O page lookup result of the current page.
797 */
798static bool iommuAmdLookupIsAccessContig(PCIOPAGELOOKUP pPageLookupPrev, PCIOPAGELOOKUP pPageLookup)
799{
800 Assert(pPageLookupPrev->fPerm == pPageLookup->fPerm);
801 size_t const cbPrev = RT_BIT_64(pPageLookupPrev->cShift);
802 RTGCPHYS const GCPhysPrev = pPageLookupPrev->GCPhysSpa;
803 RTGCPHYS const GCPhys = pPageLookup->GCPhysSpa;
804 uint64_t const offMaskPrev = IOMMU_GET_PAGE_OFF_MASK(pPageLookupPrev->cShift);
805 uint64_t const offMask = IOMMU_GET_PAGE_OFF_MASK(pPageLookup->cShift);
806
807 /* Paranoia: Ensure offset bits are 0. */
808 Assert(!(GCPhysPrev & offMaskPrev));
809 Assert(!(GCPhys & offMask));
810
811 if ((GCPhysPrev & ~offMaskPrev) + cbPrev == (GCPhys & ~offMask))
812 return true;
813 return false;
814}
815
816
817/**
818 * Gets the basic I/O device flags for the given device table entry.
819 *
820 * @returns The basic I/O device flags.
821 * @param pDte The device table entry.
822 */
823static uint16_t iommuAmdGetBasicDevFlags(PCDTE_T pDte)
824{
825 /* Extract basic flags from bits 127:0 of the DTE. */
826 uint16_t fFlags = 0;
827 if (pDte->n.u1Valid)
828 {
829 fFlags |= IOMMU_DTE_CACHE_F_VALID;
830
831 /** @todo Skip the if checks here (shift/mask the relevant bits over). */
832 if (pDte->n.u1SuppressAllPfEvents)
833 fFlags |= IOMMU_DTE_CACHE_F_SUPPRESS_ALL_IOPF;
834 if (pDte->n.u1SuppressPfEvents)
835 fFlags |= IOMMU_DTE_CACHE_F_SUPPRESS_IOPF;
836
837 uint16_t const fDtePerm = (pDte->au64[0] >> IOMMU_IO_PERM_SHIFT) & IOMMU_IO_PERM_MASK;
838 AssertCompile(IOMMU_DTE_CACHE_F_IO_PERM_MASK == IOMMU_IO_PERM_MASK);
839 fFlags |= fDtePerm << IOMMU_DTE_CACHE_F_IO_PERM_SHIFT;
840 }
841
842 /* Extract basic flags from bits 255:128 of the DTE. */
843 if (pDte->n.u1IntrMapValid)
844 {
845 fFlags |= IOMMU_DTE_CACHE_F_INTR_MAP_VALID;
846
847 /** @todo Skip the if check here (shift/mask the relevant bit over). */
848 if (pDte->n.u1IgnoreUnmappedIntrs)
849 fFlags |= IOMMU_DTE_CACHE_F_IGNORE_UNMAPPED_INTR;
850
851 uint16_t const fIntrCtrl = IOMMU_DTE_GET_INTR_CTRL(pDte);
852 AssertCompile(IOMMU_DTE_CACHE_F_INTR_CTRL_MASK == IOMMU_DTE_INTR_CTRL_MASK);
853 fFlags |= fIntrCtrl << IOMMU_DTE_CACHE_F_INTR_CTRL_SHIFT;
854 }
855 return fFlags;
856}
857
858
859/**
860 * Remaps the source MSI to the destination MSI given the IRTE.
861 *
862 * @param pMsiIn The source MSI.
863 * @param pMsiOut Where to store the remapped MSI.
864 * @param pIrte The IRTE used for the remapping.
865 */
866static void iommuAmdIrteRemapMsi(PCMSIMSG pMsiIn, PMSIMSG pMsiOut, PCIRTE_T pIrte)
867{
868 /* Preserve all bits from the source MSI address and data that don't map 1:1 from the IRTE. */
869 *pMsiOut = *pMsiIn;
870
871 pMsiOut->Addr.n.u1DestMode = pIrte->n.u1DestMode;
872 pMsiOut->Addr.n.u8DestId = pIrte->n.u8Dest;
873
874 pMsiOut->Data.n.u8Vector = pIrte->n.u8Vector;
875 pMsiOut->Data.n.u3DeliveryMode = pIrte->n.u3IntrType;
876}
877
878
879#ifdef IOMMU_WITH_DTE_CACHE
880/**
881 * Looks up an entry in the DTE cache for the given device ID.
882 *
883 * @returns The index of the entry, or the cache capacity if no entry was found.
884 * @param pThis The shared IOMMU device state.
885 * @param uDevId The device ID (bus, device, function).
886 */
887DECLINLINE(uint16_t) iommuAmdDteCacheEntryLookup(PIOMMU pThis, uint16_t uDevId)
888{
889 uint16_t const cDeviceIds = RT_ELEMENTS(pThis->aDeviceIds);
890 for (uint16_t i = 0; i < cDeviceIds; i++)
891 {
892 if (pThis->aDeviceIds[i] == uDevId)
893 return i;
894 }
895 return cDeviceIds;
896}
897
898
899/**
900 * Gets an free/unused DTE cache entry.
901 *
902 * @returns The index of an unused entry, or cache capacity if the cache is full.
903 * @param pThis The shared IOMMU device state.
904 */
905DECLINLINE(uint16_t) iommuAmdDteCacheEntryGetUnused(PCIOMMU pThis)
906{
907 /*
908 * ASSUMES device ID 0 is the PCI host bridge or the IOMMU itself
909 * (the latter being an ugly hack) and cannot be a valid device ID.
910 */
911 uint16_t const cDeviceIds = RT_ELEMENTS(pThis->aDeviceIds);
912 for (uint16_t i = 0; i < cDeviceIds; i++)
913 {
914 if (!pThis->aDeviceIds[i])
915 return i;
916 }
917 return cDeviceIds;
918}
919
920
921/**
922 * Adds or updates the I/O device flags for the given device ID.
923 *
924 * @returns VBox status code.
925 * @retval VERR_OUT_OF_RESOURCES if the cache is full.
926 *
927 * @param pDevIns The IOMMU instance data.
928 * @param uDevId The device ID (bus, device, function).
929 * @param pDte The device table entry.
930 * @param fOrMask The device flags (usually compound flags) to OR in with the
931 * basic flags, see IOMMU_DTE_CACHE_F_XXX.
932 */
933static int iommuAmdDteCacheAdd(PPDMDEVINS pDevIns, uint16_t uDevId, PCDTE_T pDte, uint16_t fOrMask)
934{
935 Assert(pDte);
936 Assert(uDevId);
937
938 int rc = VINF_SUCCESS;
939 uint16_t const fFlags = iommuAmdGetBasicDevFlags(pDte) | IOMMU_DTE_CACHE_F_PRESENT | fOrMask;
940 uint16_t const uDomainId = pDte->n.u16DomainId;
941
942 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
943 IOMMU_LOCK_CACHE_NORET(pDevIns, pThis);
944
945 uint16_t const cDteCache = RT_ELEMENTS(pThis->aDteCache);
946 uint16_t idxDte = iommuAmdDteCacheEntryLookup(pThis, uDevId);
947 if (idxDte < cDteCache)
948 {
949 pThis->aDteCache[idxDte].fFlags = fFlags;
950 pThis->aDteCache[idxDte].uDomainId = uDomainId;
951 }
952 else if ((idxDte = iommuAmdDteCacheEntryGetUnused(pThis)) < cDteCache)
953 {
954 pThis->aDeviceIds[idxDte] = uDevId;
955 pThis->aDteCache[idxDte].fFlags = fFlags;
956 pThis->aDteCache[idxDte].uDomainId = uDomainId;
957 }
958 else
959 rc = VERR_OUT_OF_RESOURCES;
960
961 IOMMU_UNLOCK_CACHE(pDevIns, pThis);
962 return rc;
963}
964
965
966/**
967 * Adds one or more I/O device flags if the device is already present in the cache.
968 *
969 * @param pDevIns The IOMMU instance data.
970 * @param uDevId The device ID (bus, device, function).
971 * @param fFlags Additional device flags to OR with existing flags, see
972 * IOMMU_DTE_CACHE_F_XXX.
973 */
974static void iommuAmdDteCacheAddFlags(PPDMDEVINS pDevIns, uint16_t uDevId, uint16_t fFlags)
975{
976 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
977 IOMMU_LOCK_CACHE_NORET(pDevIns, pThis);
978
979 uint16_t const cDteCache = RT_ELEMENTS(pThis->aDteCache);
980 uint16_t const idxDte = iommuAmdDteCacheEntryLookup(pThis, uDevId);
981 if ( idxDte < cDteCache
982 && (pThis->aDteCache[idxDte].fFlags & IOMMU_DTE_CACHE_F_PRESENT))
983 pThis->aDteCache[idxDte].fFlags |= fFlags;
984
985 IOMMU_UNLOCK_CACHE(pDevIns, pThis);
986}
987
988
989/**
990 * Removes a DTE cache entry.
991 *
992 * @param pDevIns The IOMMU instance data.
993 * @param uDevId The device ID to remove cache entries for.
994 */
995static void iommuAmdDteCacheRemove(PPDMDEVINS pDevIns, uint16_t uDevId)
996{
997 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
998 IOMMU_LOCK_CACHE_NORET(pDevIns, pThis);
999
1000 uint16_t const cDteCache = RT_ELEMENTS(pThis->aDteCache);
1001 uint16_t const idxDte = iommuAmdDteCacheEntryLookup(pThis, uDevId);
1002 if (idxDte < cDteCache)
1003 {
1004 pThis->aDteCache[idxDte].fFlags = 0;
1005 pThis->aDteCache[idxDte].uDomainId = 0;
1006 }
1007
1008 IOMMU_UNLOCK_CACHE(pDevIns, pThis);
1009}
1010
1011
1012/**
1013 * Removes all entries in the device table entry cache.
1014 *
1015 * @param pDevIns The IOMMU instance data.
1016 */
1017static void iommuAmdDteCacheRemoveAll(PPDMDEVINS pDevIns)
1018{
1019 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
1020 IOMMU_LOCK_CACHE_NORET(pDevIns, pThis);
1021 RT_ZERO(pThis->aDeviceIds);
1022 RT_ZERO(pThis->aDteCache);
1023 IOMMU_UNLOCK_CACHE(pDevIns, pThis);
1024}
1025#endif /* IOMMU_WITH_DTE_CACHE */
1026
1027
1028#ifdef IOMMU_WITH_IOTLBE_CACHE
1029/**
1030 * Moves the IOTLB entry to the least recently used slot.
1031 *
1032 * @param pThisR3 The ring-3 IOMMU device state.
1033 * @param pIotlbe The IOTLB entry to move.
1034 */
1035static void iommuAmdIotlbEntryMoveToLru(PIOMMUR3 pThisR3, PIOTLBE pIotlbe)
1036{
1037 if (!RTListNodeIsFirst(&pThisR3->LstLruIotlbe, &pIotlbe->NdLru))
1038 {
1039 RTListNodeRemove(&pIotlbe->NdLru);
1040 RTListPrepend(&pThisR3->LstLruIotlbe, &pIotlbe->NdLru);
1041 }
1042}
1043
1044
1045/**
1046 * Moves the IOTLB entry to the most recently used slot.
1047 *
1048 * @param pThisR3 The ring-3 IOMMU device state.
1049 * @param pIotlbe The IOTLB entry to move.
1050 */
1051DECLINLINE(void) iommuAmdIotlbEntryMoveToMru(PIOMMUR3 pThisR3, PIOTLBE pIotlbe)
1052{
1053 if (!RTListNodeIsLast(&pThisR3->LstLruIotlbe, &pIotlbe->NdLru))
1054 {
1055 RTListNodeRemove(&pIotlbe->NdLru);
1056 RTListAppend(&pThisR3->LstLruIotlbe, &pIotlbe->NdLru);
1057 }
1058}
1059
1060
1061# ifdef IN_RING3
1062/**
1063 * Dumps the IOTLB entry via the debug info helper.
1064 *
1065 * @returns VINF_SUCCESS.
1066 * @param pNode Pointer to an IOTLB entry to dump info.
1067 * @param pvUser Pointer to an IOTLBEINFOARG.
1068 */
1069static DECLCALLBACK(int) iommuAmdR3IotlbEntryInfo(PAVLU64NODECORE pNode, void *pvUser)
1070{
1071 /* Validate. */
1072 PCIOTLBEINFOARG pArgs = (PCIOTLBEINFOARG)pvUser;
1073 AssertPtr(pArgs);
1074 AssertPtr(pArgs->pIommuR3);
1075 AssertPtr(pArgs->pHlp);
1076 //Assert(pArgs->pIommuCC->u32Magic == IOMMU_MAGIC);
1077
1078 uint16_t const uDomainId = IOMMU_IOTLB_KEY_GET_DOMAIN_ID(pNode->Key);
1079 if (uDomainId == pArgs->uDomainId)
1080 {
1081 PCIOTLBE pIotlbe = (PCIOTLBE)pNode;
1082 AVLU64KEY const uKey = pIotlbe->Core.Key;
1083 uint64_t const uIova = IOMMU_IOTLB_KEY_GET_IOVA(uKey);
1084 RTGCPHYS const GCPhysSpa = pIotlbe->PageLookup.GCPhysSpa;
1085 uint8_t const cShift = pIotlbe->PageLookup.cShift;
1086 size_t const cbPage = RT_BIT_64(cShift);
1087 uint8_t const fPerm = pIotlbe->PageLookup.fPerm;
1088 const char *pszPerm = iommuAmdMemAccessGetPermName(fPerm);
1089 bool const fEvictPending = pIotlbe->fEvictPending;
1090
1091 PCDBGFINFOHLP pHlp = pArgs->pHlp;
1092 pHlp->pfnPrintf(pHlp, " Key = %#RX64 (%#RX64)\n", uKey, uIova);
1093 pHlp->pfnPrintf(pHlp, " GCPhys = %#RGp\n", GCPhysSpa);
1094 pHlp->pfnPrintf(pHlp, " cShift = %u (%zu bytes)\n", cShift, cbPage);
1095 pHlp->pfnPrintf(pHlp, " fPerm = %#x (%s)\n", fPerm, pszPerm);
1096 pHlp->pfnPrintf(pHlp, " fEvictPending = %RTbool\n", fEvictPending);
1097 }
1098
1099 return VINF_SUCCESS;
1100}
1101# endif /* IN_RING3 */
1102
1103
1104/**
1105 * Removes the IOTLB entry if it's associated with the specified domain ID.
1106 *
1107 * @returns VINF_SUCCESS.
1108 * @param pNode Pointer to an IOTLBE.
1109 * @param pvUser Pointer to an IOTLBEFLUSHARG containing the domain ID.
1110 */
1111static DECLCALLBACK(int) iommuAmdIotlbEntryRemoveDomainId(PAVLU64NODECORE pNode, void *pvUser)
1112{
1113 /* Validate. */
1114 PCIOTLBEFLUSHARG pArgs = (PCIOTLBEFLUSHARG)pvUser;
1115 AssertPtr(pArgs);
1116 AssertPtr(pArgs->pIommuR3);
1117 //Assert(pArgs->pIommuR3->u32Magic == IOMMU_MAGIC);
1118
1119 uint16_t const uDomainId = IOMMU_IOTLB_KEY_GET_DOMAIN_ID(pNode->Key);
1120 if (uDomainId == pArgs->uDomainId)
1121 {
1122 /* Mark this entry is as invalidated and needs to be evicted later. */
1123 PIOTLBE pIotlbe = (PIOTLBE)pNode;
1124 pIotlbe->fEvictPending = true;
1125 iommuAmdIotlbEntryMoveToLru(pArgs->pIommuR3, (PIOTLBE)pNode);
1126 }
1127 return VINF_SUCCESS;
1128}
1129
1130
1131/**
1132 * Inserts an IOTLB entry into the cache.
1133 *
1134 * @param pThis The shared IOMMU device state.
1135 * @param pThisR3 The ring-3 IOMMU device state.
1136 * @param pIotlbe The IOTLB entry to initialize and insert.
1137 * @param uDomainId The domain ID.
1138 * @param uIova The I/O virtual address.
1139 * @param pPageLookup The I/O page lookup result of the access.
1140 */
1141static void iommuAmdIotlbEntryInsert(PIOMMU pThis, PIOMMUR3 pThisR3, PIOTLBE pIotlbe, uint16_t uDomainId, uint64_t uIova,
1142 PCIOPAGELOOKUP pPageLookup)
1143{
1144 /* Initialize the IOTLB entry with results of the I/O page walk. */
1145 pIotlbe->Core.Key = IOMMU_IOTLB_KEY_MAKE(uDomainId, uIova);
1146 pIotlbe->PageLookup = *pPageLookup;
1147
1148 /* Validate. */
1149 Assert(pIotlbe->Core.Key != IOMMU_IOTLB_KEY_NIL);
1150 Assert(!pIotlbe->fEvictPending);
1151
1152 /* Check if the entry already exists. */
1153 PIOTLBE pFound = (PIOTLBE)RTAvlU64Get(&pThisR3->TreeIotlbe, pIotlbe->Core.Key);
1154 if (!pFound)
1155 {
1156 /* Insert the entry into the cache. */
1157 bool const fInserted = RTAvlU64Insert(&pThisR3->TreeIotlbe, &pIotlbe->Core);
1158 Assert(fInserted); NOREF(fInserted);
1159 Assert(pThisR3->cCachedIotlbes < IOMMU_IOTLBE_MAX);
1160 ++pThisR3->cCachedIotlbes;
1161 STAM_COUNTER_INC(&pThis->StatIotlbeCached);
1162 }
1163 else
1164 {
1165 /* Update the existing entry. */
1166 if (pFound->fEvictPending)
1167 {
1168 pFound->fEvictPending = false;
1169 STAM_COUNTER_INC(&pThis->StatIotlbeLazyEvictReuse);
1170 }
1171 Assert(pFound->PageLookup.cShift == pPageLookup->cShift);
1172 pFound->PageLookup.fPerm = pPageLookup->fPerm;
1173 pFound->PageLookup.GCPhysSpa = pPageLookup->GCPhysSpa;
1174 }
1175}
1176
1177
1178/**
1179 * Removes an IOTLB entry from the cache for the given key.
1180 *
1181 * @returns Pointer to the removed IOTLB entry, NULL if the entry wasn't found in
1182 * the tree.
1183 * @param pThis The shared IOMMU device state.
1184 * @param pThisR3 The ring-3 IOMMU device state.
1185 * @param uKey The key of the IOTLB entry to remove.
1186 */
1187static PIOTLBE iommuAmdIotlbEntryRemove(PIOMMU pThis, PIOMMUR3 pThisR3, AVLU64KEY uKey)
1188{
1189 PIOTLBE pIotlbe = (PIOTLBE)RTAvlU64Remove(&pThisR3->TreeIotlbe, uKey);
1190 if (pIotlbe)
1191 {
1192 if (pIotlbe->fEvictPending)
1193 STAM_COUNTER_INC(&pThis->StatIotlbeLazyEvictReuse);
1194
1195 RT_ZERO(pIotlbe->Core);
1196 RT_ZERO(pIotlbe->PageLookup);
1197 /* We must not erase the LRU node connections here! */
1198 pIotlbe->fEvictPending = false;
1199 Assert(pIotlbe->Core.Key == IOMMU_IOTLB_KEY_NIL);
1200
1201 Assert(pThisR3->cCachedIotlbes > 0);
1202 --pThisR3->cCachedIotlbes;
1203 STAM_COUNTER_DEC(&pThis->StatIotlbeCached);
1204 }
1205 return pIotlbe;
1206}
1207
1208
1209/**
1210 * Looks up an IOTLB from the cache.
1211 *
1212 * @returns Pointer to IOTLB entry if found, NULL otherwise.
1213 * @param pThis The shared IOMMU device state.
1214 * @param pThisR3 The ring-3 IOMMU device state.
1215 * @param uDomainId The domain ID.
1216 * @param uIova The I/O virtual address.
1217 */
1218static PIOTLBE iommuAmdIotlbLookup(PIOMMU pThis, PIOMMUR3 pThisR3, uint64_t uDomainId, uint64_t uIova)
1219{
1220 RT_NOREF(pThis);
1221
1222 uint64_t const uKey = IOMMU_IOTLB_KEY_MAKE(uDomainId, uIova);
1223 PIOTLBE pIotlbe = (PIOTLBE)RTAvlU64Get(&pThisR3->TreeIotlbe, uKey);
1224 if ( pIotlbe
1225 && !pIotlbe->fEvictPending)
1226 return pIotlbe;
1227
1228 /*
1229 * Domain Id wildcard invalidations only marks entries for eviction later but doesn't remove
1230 * them from the cache immediately. We found an entry pending eviction, just return that
1231 * nothing was found (rather than evicting now).
1232 */
1233 return NULL;
1234}
1235
1236
1237/**
1238 * Adds an IOTLB entry to the cache.
1239 *
1240 * @param pThis The shared IOMMU device state.
1241 * @param pThis The ring-3 IOMMU device state.
1242 * @param uDomainId The domain ID.
1243 * @param uIova The I/O virtual address.
1244 * @param pPageLookup The I/O page lookup result of the access.
1245 */
1246static void iommuAmdIotlbAdd(PIOMMU pThis, PIOMMUR3 pThisR3, uint16_t uDomainId, uint64_t uIova, PCIOPAGELOOKUP pPageLookup)
1247{
1248 Assert(!(uIova & X86_PAGE_4K_OFFSET_MASK));
1249 Assert(pPageLookup);
1250 Assert(pPageLookup->cShift <= 31);
1251 Assert(pPageLookup->fPerm != IOMMU_IO_PERM_NONE);
1252
1253 /*
1254 * If there are no unused IOTLB entries, evict the LRU entry.
1255 * Otherwise, get a new IOTLB entry from the pre-allocated list.
1256 */
1257 if (pThisR3->idxUnusedIotlbe == IOMMU_IOTLBE_MAX)
1258 {
1259 /* Grab the least recently used entry. */
1260 PIOTLBE pIotlbe = RTListGetFirst(&pThisR3->LstLruIotlbe, IOTLBE, NdLru);
1261 Assert(pIotlbe);
1262
1263 /* If the entry is in the cache, remove it. */
1264 if (pIotlbe->Core.Key != IOMMU_IOTLB_KEY_NIL)
1265 iommuAmdIotlbEntryRemove(pThis, pThisR3, pIotlbe->Core.Key);
1266
1267 /* Initialize and insert the IOTLB entry into the cache. */
1268 iommuAmdIotlbEntryInsert(pThis, pThisR3, pIotlbe, uDomainId, uIova, pPageLookup);
1269
1270 /* Move the entry to the most recently used slot. */
1271 iommuAmdIotlbEntryMoveToMru(pThisR3, pIotlbe);
1272 }
1273 else
1274 {
1275 /* Grab an unused IOTLB entry from the pre-allocated list. */
1276 PIOTLBE pIotlbe = &pThisR3->paIotlbes[pThisR3->idxUnusedIotlbe];
1277 ++pThisR3->idxUnusedIotlbe;
1278
1279 /* Initialize and insert the IOTLB entry into the cache. */
1280 iommuAmdIotlbEntryInsert(pThis, pThisR3, pIotlbe, uDomainId, uIova, pPageLookup);
1281
1282 /* Add the entry to the most recently used slot. */
1283 RTListAppend(&pThisR3->LstLruIotlbe, &pIotlbe->NdLru);
1284 }
1285}
1286
1287
1288/**
1289 * Removes all IOTLB entries from the cache.
1290 *
1291 * @param pDevIns The IOMMU instance data.
1292 */
1293static void iommuAmdIotlbRemoveAll(PPDMDEVINS pDevIns)
1294{
1295 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
1296 PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
1297 IOMMU_LOCK_CACHE_NORET(pDevIns, pThis);
1298
1299 if (pThisR3->cCachedIotlbes > 0)
1300 {
1301 size_t const cbIotlbes = sizeof(IOTLBE) * IOMMU_IOTLBE_MAX;
1302 RT_BZERO(pThisR3->paIotlbes, cbIotlbes);
1303 pThisR3->idxUnusedIotlbe = 0;
1304 pThisR3->cCachedIotlbes = 0;
1305 STAM_COUNTER_RESET(&pThis->StatIotlbeCached);
1306 RTListInit(&pThisR3->LstLruIotlbe);
1307 }
1308
1309 IOMMU_UNLOCK_CACHE(pDevIns, pThis);
1310}
1311
1312
1313/**
1314 * Removes IOTLB entries for the range of I/O virtual addresses and the specified
1315 * domain ID from the cache.
1316 *
1317 * @param pDevIns The IOMMU instance data.
1318 * @param uDomainId The domain ID.
1319 * @param uIova The I/O virtual address to invalidate.
1320 * @param cbInvalidate The size of the invalidation (must be 4K aligned).
1321 */
1322static void iommuAmdIotlbRemoveRange(PPDMDEVINS pDevIns, uint16_t uDomainId, uint64_t uIova, size_t cbInvalidate)
1323{
1324 /* Validate. */
1325 Assert(!(uIova & X86_PAGE_4K_OFFSET_MASK));
1326 Assert(!(cbInvalidate & X86_PAGE_4K_OFFSET_MASK));
1327 Assert(cbInvalidate >= X86_PAGE_4K_SIZE);
1328
1329 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
1330 PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
1331 IOMMU_LOCK_CACHE_NORET(pDevIns, pThis);
1332
1333 do
1334 {
1335 uint64_t const uKey = IOMMU_IOTLB_KEY_MAKE(uDomainId, uIova);
1336 PIOTLBE pIotlbe = iommuAmdIotlbEntryRemove(pThis, pThisR3, uKey);
1337 if (pIotlbe)
1338 iommuAmdIotlbEntryMoveToLru(pThisR3, pIotlbe);
1339 uIova += X86_PAGE_4K_SIZE;
1340 cbInvalidate -= X86_PAGE_4K_SIZE;
1341 } while (cbInvalidate > 0);
1342
1343 IOMMU_UNLOCK_CACHE(pDevIns, pThis);
1344}
1345
1346
1347/**
1348 * Removes all IOTLB entries for the specified domain ID.
1349 *
1350 * @param pDevIns The IOMMU instance data.
1351 * @param uDomainId The domain ID.
1352 */
1353static void iommuAmdIotlbRemoveDomainId(PPDMDEVINS pDevIns, uint16_t uDomainId)
1354{
1355 /*
1356 * We need to iterate the tree and search based on the domain ID.
1357 * But it seems we cannot remove items while iterating the tree.
1358 * Thus, we simply mark entries for eviction later but move them to the LRU
1359 * so they will eventually get evicted and re-cycled as the cache gets re-populated.
1360 */
1361 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
1362 PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
1363 IOMMU_LOCK_CACHE_NORET(pDevIns, pThis);
1364
1365 IOTLBEFLUSHARG Args;
1366 Args.pIommuR3 = pThisR3;
1367 Args.uDomainId = uDomainId;
1368 RTAvlU64DoWithAll(&pThisR3->TreeIotlbe, true /* fFromLeft */, iommuAmdIotlbEntryRemoveDomainId, &Args);
1369
1370 IOMMU_UNLOCK_CACHE(pDevIns, pThis);
1371}
1372
1373
1374/**
1375 * Adds or updates IOTLB entries for the given range of I/O virtual addresses.
1376 *
1377 * @param pDevIns The IOMMU instance data.
1378 * @param uDomainId The domain ID.
1379 * @param uIova The I/O virtual address.
1380 * @param cbAccess The size of the access (must be 4K aligned).
1381 * @param GCPhysSpa The translated system-physical address.
1382 * @param fPerm The I/O permissions for the access, see IOMMU_IO_PERM_XXX.
1383 */
1384static void iommuAmdIotlbAddRange(PPDMDEVINS pDevIns, uint16_t uDomainId, uint64_t uIova, size_t cbAccess, RTGCPHYS GCPhysSpa,
1385 uint8_t fPerm)
1386{
1387 Assert(!(uIova & X86_PAGE_4K_OFFSET_MASK));
1388 Assert(!(GCPhysSpa & X86_PAGE_4K_OFFSET_MASK));
1389 Assert(!(cbAccess & X86_PAGE_4K_OFFSET_MASK));
1390 Assert(cbAccess >= X86_PAGE_4K_SIZE);
1391
1392 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
1393 PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
1394
1395 /* Add IOTLB entries for every page in the access. */
1396 IOPAGELOOKUP PageLookup;
1397 RT_ZERO(PageLookup);
1398 PageLookup.cShift = X86_PAGE_4K_SHIFT;
1399 PageLookup.fPerm = fPerm;
1400 PageLookup.GCPhysSpa = GCPhysSpa;
1401
1402 size_t cPages = cbAccess / X86_PAGE_4K_SIZE;
1403 cPages = RT_MIN(cPages, IOMMU_IOTLBE_MAX);
1404
1405 IOMMU_LOCK_CACHE_NORET(pDevIns, pThis);
1406 /** @todo Re-check DTE cache? */
1407 do
1408 {
1409 iommuAmdIotlbAdd(pThis, pThisR3, uDomainId, uIova, &PageLookup);
1410 uIova += X86_PAGE_4K_SIZE;
1411 PageLookup.GCPhysSpa += X86_PAGE_4K_SIZE;
1412 --cPages;
1413 } while (cPages > 0);
1414 IOMMU_UNLOCK_CACHE(pDevIns, pThis);
1415}
1416#endif /* IOMMU_WITH_IOTLBE_CACHE */
1417
1418
1419#ifdef IOMMU_WITH_IRTE_CACHE
1420/**
1421 * Looks up an IRTE cache entry.
1422 *
1423 * @returns Index of the found entry, or cache capacity if not found.
1424 * @param pThis The shared IOMMU device state.
1425 * @param uDevId The device ID (bus, device, function).
1426 * @param offIrte The offset into the interrupt remap table.
1427 */
1428static uint16_t iommuAmdIrteCacheEntryLookup(PCIOMMU pThis, uint16_t uDevId, uint16_t offIrte)
1429{
1430 /** @todo Consider sorting and binary search when the cache capacity grows.
1431 * For the IRTE cache this should be okay since typically guests do not alter the
1432 * interrupt remapping once programmed, so hopefully sorting shouldn't happen
1433 * often. */
1434 uint32_t const uKey = IOMMU_IRTE_CACHE_KEY_MAKE(uDevId, offIrte);
1435 uint16_t const cIrteCache = RT_ELEMENTS(pThis->aIrteCache);
1436 for (uint16_t i = 0; i < cIrteCache; i++)
1437 if (pThis->aIrteCache[i].uKey == uKey)
1438 return i;
1439 return cIrteCache;
1440}
1441
1442
1443/**
1444 * Gets a free/unused IRTE cache entry.
1445 *
1446 * @returns The index of an unused entry, or cache capacity if the cache is full.
1447 * @param pThis The shared IOMMU device state.
1448 */
1449static uint16_t iommuAmdIrteCacheEntryGetUnused(PCIOMMU pThis)
1450{
1451 uint16_t const cIrteCache = RT_ELEMENTS(pThis->aIrteCache);
1452 for (uint16_t i = 0; i < cIrteCache; i++)
1453 if (pThis->aIrteCache[i].uKey == IOMMU_IRTE_CACHE_KEY_NIL)
1454 {
1455 Assert(!pThis->aIrteCache[i].Irte.u32);
1456 return i;
1457 }
1458 return cIrteCache;
1459}
1460
1461
1462/**
1463 * Looks up the IRTE cache for the given MSI.
1464 *
1465 * @returns VBox status code.
1466 * @param pDevIns The IOMMU instance data.
1467 * @param uDevId The device ID (bus, device, function).
1468 * @param enmOp The IOMMU operation being performed.
1469 * @param pMsiIn The source MSI.
1470 * @param pMsiOut Where to store the remapped MSI.
1471 */
1472static int iommuAmdIrteCacheLookup(PPDMDEVINS pDevIns, uint16_t uDevId, IOMMUOP enmOp, PCMSIMSG pMsiIn, PMSIMSG pMsiOut)
1473{
1474 RT_NOREF(enmOp); /* May need it if we have to report errors (currently we fallback to the slower path to do that). */
1475
1476 int rc = VERR_NOT_FOUND;
1477 /* Deal with such cases in the slower/fallback path. */
1478 if ((pMsiIn->Addr.u64 & VBOX_MSI_ADDR_ADDR_MASK) == VBOX_MSI_ADDR_BASE)
1479 { /* likely */ }
1480 else
1481 return rc;
1482
1483 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
1484 IOMMU_LOCK_CACHE_NORET(pDevIns, pThis);
1485
1486 uint16_t const idxDteCache = iommuAmdDteCacheEntryLookup(pThis, uDevId);
1487 if (idxDteCache < RT_ELEMENTS(pThis->aDteCache))
1488 {
1489 PCDTECACHE pDteCache = &pThis->aDteCache[idxDteCache];
1490 if ((pDteCache->fFlags & (IOMMU_DTE_CACHE_F_PRESENT | IOMMU_DTE_CACHE_F_INTR_MAP_VALID))
1491 == (IOMMU_DTE_CACHE_F_PRESENT | IOMMU_DTE_CACHE_F_INTR_MAP_VALID))
1492 {
1493 Assert((pMsiIn->Addr.u64 & VBOX_MSI_ADDR_ADDR_MASK) == VBOX_MSI_ADDR_BASE); /* Paranoia. */
1494
1495 /* Currently, we only cache remapping of fixed and arbitrated interrupts. */
1496 uint8_t const u8DeliveryMode = pMsiIn->Data.n.u3DeliveryMode;
1497 if (u8DeliveryMode <= VBOX_MSI_DELIVERY_MODE_LOWEST_PRIO)
1498 {
1499 uint8_t const uIntrCtrl = (pDteCache->fFlags >> IOMMU_DTE_CACHE_F_INTR_CTRL_SHIFT)
1500 & IOMMU_DTE_CACHE_F_INTR_CTRL_MASK;
1501 if (uIntrCtrl == IOMMU_INTR_CTRL_REMAP)
1502 {
1503 /* Interrupt table length has been verified prior to adding entries to the cache. */
1504 uint16_t const offIrte = IOMMU_GET_IRTE_OFF(pMsiIn->Data.u32);
1505 uint16_t const idxIrteCache = iommuAmdIrteCacheEntryLookup(pThis, uDevId, offIrte);
1506 if (idxIrteCache < RT_ELEMENTS(pThis->aIrteCache))
1507 {
1508 PCIRTE_T pIrte = &pThis->aIrteCache[idxIrteCache].Irte;
1509 Assert(pIrte->n.u1RemapEnable);
1510 Assert(pIrte->n.u3IntrType <= VBOX_MSI_DELIVERY_MODE_LOWEST_PRIO);
1511 iommuAmdIrteRemapMsi(pMsiIn, pMsiOut, pIrte);
1512 rc = VINF_SUCCESS;
1513 }
1514 }
1515 else if (uIntrCtrl == IOMMU_INTR_CTRL_FWD_UNMAPPED)
1516 {
1517 *pMsiOut = *pMsiIn;
1518 rc = VINF_SUCCESS;
1519 }
1520 }
1521 }
1522 else if (pDteCache->fFlags & IOMMU_DTE_CACHE_F_PRESENT)
1523 {
1524 *pMsiOut = *pMsiIn;
1525 rc = VINF_SUCCESS;
1526 }
1527 }
1528
1529 IOMMU_UNLOCK_CACHE(pDevIns, pThis);
1530 return rc;
1531}
1532
1533
1534/**
1535 * Adds or updates the IRTE cache for the given IRTE.
1536 *
1537 * @returns VBox status code.
1538 * @retval VERR_OUT_OF_RESOURCES if the cache is full.
1539 *
1540 * @param pDevIns The IOMMU instance data.
1541 * @param uDevId The device ID (bus, device, function).
1542 * @param offIrte The offset into the interrupt remap table.
1543 * @param pIrte The IRTE to cache.
1544 */
1545static int iommuAmdIrteCacheAdd(PPDMDEVINS pDevIns, uint16_t uDevId, uint16_t offIrte, PCIRTE_T pIrte)
1546{
1547 Assert(offIrte != 0xffff); /* Shouldn't be a valid IRTE table offset since sizeof(IRTE) is a multiple of 4. */
1548
1549 int rc = VINF_SUCCESS;
1550 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
1551 IOMMU_LOCK_CACHE_NORET(pDevIns, pThis);
1552
1553 /* Find an existing entry or get an unused slot. */
1554 uint16_t const cIrteCache = RT_ELEMENTS(pThis->aIrteCache);
1555 uint16_t idxIrteCache = iommuAmdIrteCacheEntryLookup(pThis, uDevId, offIrte);
1556 if ( idxIrteCache < cIrteCache
1557 || (idxIrteCache = iommuAmdIrteCacheEntryGetUnused(pThis)) < cIrteCache)
1558 {
1559 pThis->aIrteCache[idxIrteCache].uKey = IOMMU_IRTE_CACHE_KEY_MAKE(uDevId, offIrte);
1560 pThis->aIrteCache[idxIrteCache].Irte = *pIrte;
1561 }
1562 else
1563 rc = VERR_OUT_OF_RESOURCES;
1564
1565 IOMMU_UNLOCK_CACHE(pDevIns, pThis);
1566 return rc;
1567}
1568
1569
1570/**
1571 * Removes IRTE cache entries for the given device ID.
1572 *
1573 * @param pDevIns The IOMMU instance data.
1574 * @param uDevId The device ID (bus, device, function).
1575 */
1576static void iommuAmdIrteCacheRemove(PPDMDEVINS pDevIns, uint16_t uDevId)
1577{
1578 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
1579 IOMMU_LOCK_CACHE_NORET(pDevIns, pThis);
1580 uint16_t const cIrteCache = RT_ELEMENTS(pThis->aIrteCache);
1581 for (uint16_t i = 0; i < cIrteCache; i++)
1582 {
1583 PIRTECACHE pIrteCache = &pThis->aIrteCache[i];
1584 if (uDevId == IOMMU_IRTE_CACHE_KEY_GET_DEVICE_ID(pIrteCache->uKey))
1585 {
1586 pIrteCache->uKey = IOMMU_IRTE_CACHE_KEY_NIL;
1587 pIrteCache->Irte.u32 = 0;
1588 /* There could multiple IRTE entries for a device ID, continue searching. */
1589 }
1590 }
1591 IOMMU_UNLOCK_CACHE(pDevIns, pThis);
1592}
1593
1594
1595/**
1596 * Removes all IRTE cache entries.
1597 *
1598 * @param pDevIns The IOMMU instance data.
1599 */
1600static void iommuAmdIrteCacheRemoveAll(PPDMDEVINS pDevIns)
1601{
1602 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
1603 IOMMU_LOCK_CACHE_NORET(pDevIns, pThis);
1604 uint16_t const cIrteCache = RT_ELEMENTS(pThis->aIrteCache);
1605 for (uint16_t i = 0; i < cIrteCache; i++)
1606 {
1607 pThis->aIrteCache[i].uKey = IOMMU_IRTE_CACHE_KEY_NIL;
1608 pThis->aIrteCache[i].Irte.u32 = 0;
1609 }
1610 IOMMU_UNLOCK_CACHE(pDevIns, pThis);
1611}
1612#endif /* IOMMU_WITH_IRTE_CACHE */
1613
1614
1615/**
1616 * Atomically reads the control register without locking the IOMMU device.
1617 *
1618 * @returns The control register.
1619 * @param pThis The shared IOMMU device state.
1620 */
1621DECL_FORCE_INLINE(IOMMU_CTRL_T) iommuAmdGetCtrlUnlocked(PCIOMMU pThis)
1622{
1623 IOMMU_CTRL_T Ctrl;
1624 Ctrl.u64 = ASMAtomicReadU64((volatile uint64_t *)&pThis->Ctrl.u64);
1625 return Ctrl;
1626}
1627
1628
1629/**
1630 * Returns whether MSI is enabled for the IOMMU.
1631 *
1632 * @returns Whether MSI is enabled.
1633 * @param pDevIns The IOMMU device instance.
1634 *
1635 * @note There should be a PCIDevXxx function for this.
1636 */
1637static bool iommuAmdIsMsiEnabled(PPDMDEVINS pDevIns)
1638{
1639 MSI_CAP_HDR_T MsiCapHdr;
1640 MsiCapHdr.u32 = PDMPciDevGetDWord(pDevIns->apPciDevs[0], IOMMU_PCI_OFF_MSI_CAP_HDR);
1641 return MsiCapHdr.n.u1MsiEnable;
1642}
1643
1644
1645/**
1646 * Signals a PCI target abort.
1647 *
1648 * @param pDevIns The IOMMU device instance.
1649 */
1650static void iommuAmdSetPciTargetAbort(PPDMDEVINS pDevIns)
1651{
1652 PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
1653 uint16_t const u16Status = PDMPciDevGetStatus(pPciDev) | VBOX_PCI_STATUS_SIG_TARGET_ABORT;
1654 PDMPciDevSetStatus(pPciDev, u16Status);
1655}
1656
1657
1658/**
1659 * Wakes up the command thread if there are commands to be processed.
1660 *
1661 * @param pDevIns The IOMMU device instance.
1662 *
1663 * @remarks The IOMMU lock must be held while calling this!
1664 */
1665static void iommuAmdCmdThreadWakeUpIfNeeded(PPDMDEVINS pDevIns)
1666{
1667 Log4Func(("\n"));
1668
1669 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
1670 if ( pThis->Status.n.u1CmdBufRunning
1671 && pThis->CmdBufTailPtr.n.off != pThis->CmdBufHeadPtr.n.off
1672 && !ASMAtomicXchgBool(&pThis->fCmdThreadSignaled, true))
1673 {
1674 Log4Func(("Signaling command thread\n"));
1675 PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hEvtCmdThread);
1676 }
1677}
1678
1679
1680/**
1681 * Reads the Device Table Base Address Register.
1682 */
1683static VBOXSTRICTRC iommuAmdDevTabBar_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1684{
1685 RT_NOREF(pDevIns, offReg);
1686 *pu64Value = pThis->aDevTabBaseAddrs[0].u64;
1687 return VINF_SUCCESS;
1688}
1689
1690
1691/**
1692 * Reads the Command Buffer Base Address Register.
1693 */
1694static VBOXSTRICTRC iommuAmdCmdBufBar_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1695{
1696 RT_NOREF(pDevIns, offReg);
1697 *pu64Value = pThis->CmdBufBaseAddr.u64;
1698 return VINF_SUCCESS;
1699}
1700
1701
1702/**
1703 * Reads the Event Log Base Address Register.
1704 */
1705static VBOXSTRICTRC iommuAmdEvtLogBar_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1706{
1707 RT_NOREF(pDevIns, offReg);
1708 *pu64Value = pThis->EvtLogBaseAddr.u64;
1709 return VINF_SUCCESS;
1710}
1711
1712
1713/**
1714 * Reads the Control Register.
1715 */
1716static VBOXSTRICTRC iommuAmdCtrl_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1717{
1718 RT_NOREF(pDevIns, offReg);
1719 *pu64Value = pThis->Ctrl.u64;
1720 return VINF_SUCCESS;
1721}
1722
1723
1724/**
1725 * Reads the Exclusion Range Base Address Register.
1726 */
1727static VBOXSTRICTRC iommuAmdExclRangeBar_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1728{
1729 RT_NOREF(pDevIns, offReg);
1730 *pu64Value = pThis->ExclRangeBaseAddr.u64;
1731 return VINF_SUCCESS;
1732}
1733
1734
1735/**
1736 * Reads to the Exclusion Range Limit Register.
1737 */
1738static VBOXSTRICTRC iommuAmdExclRangeLimit_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1739{
1740 RT_NOREF(pDevIns, offReg);
1741 *pu64Value = pThis->ExclRangeLimit.u64;
1742 return VINF_SUCCESS;
1743}
1744
1745
1746/**
1747 * Reads to the Extended Feature Register.
1748 */
1749static VBOXSTRICTRC iommuAmdExtFeat_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1750{
1751 RT_NOREF(pDevIns, offReg);
1752 *pu64Value = pThis->ExtFeat.u64;
1753 return VINF_SUCCESS;
1754}
1755
1756
1757/**
1758 * Reads to the PPR Log Base Address Register.
1759 */
1760static VBOXSTRICTRC iommuAmdPprLogBar_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1761{
1762 RT_NOREF(pDevIns, offReg);
1763 *pu64Value = pThis->PprLogBaseAddr.u64;
1764 return VINF_SUCCESS;
1765}
1766
1767
1768/**
1769 * Writes the Hardware Event Register (Hi).
1770 */
1771static VBOXSTRICTRC iommuAmdHwEvtHi_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1772{
1773 RT_NOREF(pDevIns, offReg);
1774 *pu64Value = pThis->HwEvtHi.u64;
1775 return VINF_SUCCESS;
1776}
1777
1778
1779/**
1780 * Reads the Hardware Event Register (Lo).
1781 */
1782static VBOXSTRICTRC iommuAmdHwEvtLo_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1783{
1784 RT_NOREF(pDevIns, offReg);
1785 *pu64Value = pThis->HwEvtLo;
1786 return VINF_SUCCESS;
1787}
1788
1789
1790/**
1791 * Reads the Hardware Event Status Register.
1792 */
1793static VBOXSTRICTRC iommuAmdHwEvtStatus_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1794{
1795 RT_NOREF(pDevIns, offReg);
1796 *pu64Value = pThis->HwEvtStatus.u64;
1797 return VINF_SUCCESS;
1798}
1799
1800
1801/**
1802 * Reads to the GA Log Base Address Register.
1803 */
1804static VBOXSTRICTRC iommuAmdGALogBar_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1805{
1806 RT_NOREF(pDevIns, offReg);
1807 *pu64Value = pThis->GALogBaseAddr.u64;
1808 return VINF_SUCCESS;
1809}
1810
1811
1812/**
1813 * Reads to the PPR Log B Base Address Register.
1814 */
1815static VBOXSTRICTRC iommuAmdPprLogBBaseAddr_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1816{
1817 RT_NOREF(pDevIns, offReg);
1818 *pu64Value = pThis->PprLogBBaseAddr.u64;
1819 return VINF_SUCCESS;
1820}
1821
1822
1823/**
1824 * Reads to the Event Log B Base Address Register.
1825 */
1826static VBOXSTRICTRC iommuAmdEvtLogBBaseAddr_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1827{
1828 RT_NOREF(pDevIns, offReg);
1829 *pu64Value = pThis->EvtLogBBaseAddr.u64;
1830 return VINF_SUCCESS;
1831}
1832
1833
1834/**
1835 * Reads the Device Table Segment Base Address Register.
1836 */
1837static VBOXSTRICTRC iommuAmdDevTabSegBar_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1838{
1839 RT_NOREF(pDevIns);
1840
1841 /* Figure out which segment is being written. */
1842 uint8_t const offSegment = (offReg - IOMMU_MMIO_OFF_DEV_TAB_SEG_FIRST) >> 3;
1843 uint8_t const idxSegment = offSegment + 1;
1844 Assert(idxSegment < RT_ELEMENTS(pThis->aDevTabBaseAddrs));
1845
1846 *pu64Value = pThis->aDevTabBaseAddrs[idxSegment].u64;
1847 return VINF_SUCCESS;
1848}
1849
1850
1851/**
1852 * Reads the Device Specific Feature Extension (DSFX) Register.
1853 */
1854static VBOXSTRICTRC iommuAmdDevSpecificFeat_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1855{
1856 RT_NOREF(pDevIns, offReg);
1857 *pu64Value = pThis->DevSpecificFeat.u64;
1858 return VINF_SUCCESS;
1859}
1860
1861/**
1862 * Reads the Device Specific Control Extension (DSCX) Register.
1863 */
1864static VBOXSTRICTRC iommuAmdDevSpecificCtrl_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1865{
1866 RT_NOREF(pDevIns, offReg);
1867 *pu64Value = pThis->DevSpecificCtrl.u64;
1868 return VINF_SUCCESS;
1869}
1870
1871
1872/**
1873 * Reads the Device Specific Status Extension (DSSX) Register.
1874 */
1875static VBOXSTRICTRC iommuAmdDevSpecificStatus_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1876{
1877 RT_NOREF(pDevIns, offReg);
1878 *pu64Value = pThis->DevSpecificStatus.u64;
1879 return VINF_SUCCESS;
1880}
1881
1882
1883/**
1884 * Reads the MSI Vector Register 0 (32-bit) and the MSI Vector Register 1 (32-bit).
1885 */
1886static VBOXSTRICTRC iommuAmdDevMsiVector_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1887{
1888 RT_NOREF(pDevIns, offReg);
1889 uint32_t const uLo = pThis->MiscInfo.au32[0];
1890 uint32_t const uHi = pThis->MiscInfo.au32[1];
1891 *pu64Value = RT_MAKE_U64(uLo, uHi);
1892 return VINF_SUCCESS;
1893}
1894
1895
1896/**
1897 * Reads the MSI Capability Header Register (32-bit) and the MSI Address (Lo)
1898 * Register (32-bit).
1899 */
1900static VBOXSTRICTRC iommuAmdMsiCapHdrAndAddrLo_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1901{
1902 RT_NOREF(pThis, offReg);
1903 PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
1904 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
1905 uint32_t const uLo = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_CAP_HDR);
1906 uint32_t const uHi = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_ADDR_LO);
1907 *pu64Value = RT_MAKE_U64(uLo, uHi);
1908 return VINF_SUCCESS;
1909}
1910
1911
1912/**
1913 * Reads the MSI Address (Hi) Register (32-bit) and the MSI data register (32-bit).
1914 */
1915static VBOXSTRICTRC iommuAmdMsiAddrHiAndData_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1916{
1917 RT_NOREF(pThis, offReg);
1918 PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
1919 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
1920 uint32_t const uLo = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_ADDR_HI);
1921 uint32_t const uHi = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_DATA);
1922 *pu64Value = RT_MAKE_U64(uLo, uHi);
1923 return VINF_SUCCESS;
1924}
1925
1926
1927/**
1928 * Reads the Command Buffer Head Pointer Register.
1929 */
1930static VBOXSTRICTRC iommuAmdCmdBufHeadPtr_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1931{
1932 RT_NOREF(pDevIns, offReg);
1933 *pu64Value = pThis->CmdBufHeadPtr.u64;
1934 return VINF_SUCCESS;
1935}
1936
1937
1938/**
1939 * Reads the Command Buffer Tail Pointer Register.
1940 */
1941static VBOXSTRICTRC iommuAmdCmdBufTailPtr_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1942{
1943 RT_NOREF(pDevIns, offReg);
1944 *pu64Value = pThis->CmdBufTailPtr.u64;
1945 return VINF_SUCCESS;
1946}
1947
1948
1949/**
1950 * Reads the Event Log Head Pointer Register.
1951 */
1952static VBOXSTRICTRC iommuAmdEvtLogHeadPtr_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1953{
1954 RT_NOREF(pDevIns, offReg);
1955 *pu64Value = pThis->EvtLogHeadPtr.u64;
1956 return VINF_SUCCESS;
1957}
1958
1959
1960/**
1961 * Reads the Event Log Tail Pointer Register.
1962 */
1963static VBOXSTRICTRC iommuAmdEvtLogTailPtr_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1964{
1965 RT_NOREF(pDevIns, offReg);
1966 *pu64Value = pThis->EvtLogTailPtr.u64;
1967 return VINF_SUCCESS;
1968}
1969
1970
1971/**
1972 * Reads the Status Register.
1973 */
1974static VBOXSTRICTRC iommuAmdStatus_r(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t *pu64Value)
1975{
1976 RT_NOREF(pDevIns, offReg);
1977 *pu64Value = pThis->Status.u64;
1978 return VINF_SUCCESS;
1979}
1980
1981
1982/**
1983 * Writes the Device Table Base Address Register.
1984 */
1985static VBOXSTRICTRC iommuAmdDevTabBar_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
1986{
1987 RT_NOREF(pDevIns, offReg);
1988
1989 /* Mask out all unrecognized bits. */
1990 u64Value &= IOMMU_DEV_TAB_BAR_VALID_MASK;
1991
1992 /* Update the register. */
1993 pThis->aDevTabBaseAddrs[0].u64 = u64Value;
1994
1995 /* Paranoia. */
1996 Assert(pThis->aDevTabBaseAddrs[0].n.u9Size <= g_auDevTabSegMaxSizes[0]);
1997 return VINF_SUCCESS;
1998}
1999
2000
2001/**
2002 * Writes the Command Buffer Base Address Register.
2003 */
2004static VBOXSTRICTRC iommuAmdCmdBufBar_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2005{
2006 RT_NOREF(pDevIns, offReg);
2007
2008 /*
2009 * While this is not explicitly specified like the event log base address register,
2010 * the AMD IOMMU spec. does specify "CmdBufRun must be 0b to modify the command buffer registers properly".
2011 * Inconsistent specs :/
2012 */
2013 if (pThis->Status.n.u1CmdBufRunning)
2014 {
2015 LogFunc(("Setting CmdBufBar (%#RX64) when command buffer is running -> Ignored\n", u64Value));
2016 return VINF_SUCCESS;
2017 }
2018
2019 /* Mask out all unrecognized bits. */
2020 CMD_BUF_BAR_T CmdBufBaseAddr;
2021 CmdBufBaseAddr.u64 = u64Value & IOMMU_CMD_BUF_BAR_VALID_MASK;
2022
2023 /* Validate the length. */
2024 if (CmdBufBaseAddr.n.u4Len >= 8)
2025 {
2026 /* Update the register. */
2027 pThis->CmdBufBaseAddr.u64 = CmdBufBaseAddr.u64;
2028
2029 /*
2030 * Writing the command buffer base address, clears the command buffer head and tail pointers.
2031 * See AMD IOMMU spec. 2.4 "Commands".
2032 */
2033 pThis->CmdBufHeadPtr.u64 = 0;
2034 pThis->CmdBufTailPtr.u64 = 0;
2035 }
2036 else
2037 LogFunc(("Command buffer length (%#x) invalid -> Ignored\n", CmdBufBaseAddr.n.u4Len));
2038
2039 return VINF_SUCCESS;
2040}
2041
2042
2043/**
2044 * Writes the Event Log Base Address Register.
2045 */
2046static VBOXSTRICTRC iommuAmdEvtLogBar_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2047{
2048 RT_NOREF(pDevIns, offReg);
2049
2050 /*
2051 * IOMMU behavior is undefined when software writes this register when event logging is running.
2052 * In our emulation, we ignore the write entirely.
2053 * See AMD IOMMU spec. "Event Log Base Address Register".
2054 */
2055 if (pThis->Status.n.u1EvtLogRunning)
2056 {
2057 LogFunc(("Setting EvtLogBar (%#RX64) when event logging is running -> Ignored\n", u64Value));
2058 return VINF_SUCCESS;
2059 }
2060
2061 /* Mask out all unrecognized bits. */
2062 u64Value &= IOMMU_EVT_LOG_BAR_VALID_MASK;
2063 EVT_LOG_BAR_T EvtLogBaseAddr;
2064 EvtLogBaseAddr.u64 = u64Value;
2065
2066 /* Validate the length. */
2067 if (EvtLogBaseAddr.n.u4Len >= 8)
2068 {
2069 /* Update the register. */
2070 pThis->EvtLogBaseAddr.u64 = EvtLogBaseAddr.u64;
2071
2072 /*
2073 * Writing the event log base address, clears the event log head and tail pointers.
2074 * See AMD IOMMU spec. 2.5 "Event Logging".
2075 */
2076 pThis->EvtLogHeadPtr.u64 = 0;
2077 pThis->EvtLogTailPtr.u64 = 0;
2078 }
2079 else
2080 LogFunc(("Event log length (%#x) invalid -> Ignored\n", EvtLogBaseAddr.n.u4Len));
2081
2082 return VINF_SUCCESS;
2083}
2084
2085
2086/**
2087 * Writes the Control Register.
2088 */
2089static VBOXSTRICTRC iommuAmdCtrl_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2090{
2091 RT_NOREF(pDevIns, offReg);
2092
2093 /* Mask out all unrecognized bits. */
2094 u64Value &= IOMMU_CTRL_VALID_MASK;
2095 IOMMU_CTRL_T NewCtrl;
2096 NewCtrl.u64 = u64Value;
2097
2098 /* Ensure the device table segments are within limits. */
2099 if (NewCtrl.n.u3DevTabSegEn <= pThis->ExtFeat.n.u2DevTabSegSup)
2100 {
2101 IOMMU_CTRL_T const OldCtrl = pThis->Ctrl;
2102
2103 /* Update the register. */
2104 ASMAtomicWriteU64(&pThis->Ctrl.u64, NewCtrl.u64);
2105
2106 bool const fNewIommuEn = NewCtrl.n.u1IommuEn;
2107 bool const fOldIommuEn = OldCtrl.n.u1IommuEn;
2108
2109 /* Enable or disable event logging when the bit transitions. */
2110 bool const fOldEvtLogEn = OldCtrl.n.u1EvtLogEn;
2111 bool const fNewEvtLogEn = NewCtrl.n.u1EvtLogEn;
2112 if ( fOldEvtLogEn != fNewEvtLogEn
2113 || fOldIommuEn != fNewIommuEn)
2114 {
2115 if ( fNewIommuEn
2116 && fNewEvtLogEn)
2117 {
2118 ASMAtomicAndU64(&pThis->Status.u64, ~IOMMU_STATUS_EVT_LOG_OVERFLOW);
2119 ASMAtomicOrU64(&pThis->Status.u64, IOMMU_STATUS_EVT_LOG_RUNNING);
2120 }
2121 else
2122 ASMAtomicAndU64(&pThis->Status.u64, ~IOMMU_STATUS_EVT_LOG_RUNNING);
2123 }
2124
2125 /* Enable or disable command buffer processing when the bit transitions. */
2126 bool const fOldCmdBufEn = OldCtrl.n.u1CmdBufEn;
2127 bool const fNewCmdBufEn = NewCtrl.n.u1CmdBufEn;
2128 if ( fOldCmdBufEn != fNewCmdBufEn
2129 || fOldIommuEn != fNewIommuEn)
2130 {
2131 if ( fNewCmdBufEn
2132 && fNewIommuEn)
2133 {
2134 ASMAtomicOrU64(&pThis->Status.u64, IOMMU_STATUS_CMD_BUF_RUNNING);
2135 LogFunc(("Command buffer enabled\n"));
2136
2137 /* Wake up the command thread to start processing commands if any. */
2138 iommuAmdCmdThreadWakeUpIfNeeded(pDevIns);
2139 }
2140 else
2141 {
2142 ASMAtomicAndU64(&pThis->Status.u64, ~IOMMU_STATUS_CMD_BUF_RUNNING);
2143 LogFunc(("Command buffer disabled\n"));
2144 }
2145 }
2146 }
2147 else
2148 {
2149 LogFunc(("Invalid number of device table segments enabled, exceeds %#x (%#RX64) -> Ignored!\n",
2150 pThis->ExtFeat.n.u2DevTabSegSup, NewCtrl.u64));
2151 }
2152
2153 return VINF_SUCCESS;
2154}
2155
2156
2157/**
2158 * Writes to the Exclusion Range Base Address Register.
2159 */
2160static VBOXSTRICTRC iommuAmdExclRangeBar_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2161{
2162 RT_NOREF(pDevIns, offReg);
2163 pThis->ExclRangeBaseAddr.u64 = u64Value & IOMMU_EXCL_RANGE_BAR_VALID_MASK;
2164 return VINF_SUCCESS;
2165}
2166
2167
2168/**
2169 * Writes to the Exclusion Range Limit Register.
2170 */
2171static VBOXSTRICTRC iommuAmdExclRangeLimit_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2172{
2173 RT_NOREF(pDevIns, offReg);
2174 u64Value &= IOMMU_EXCL_RANGE_LIMIT_VALID_MASK;
2175 u64Value |= UINT64_C(0xfff);
2176 pThis->ExclRangeLimit.u64 = u64Value;
2177 return VINF_SUCCESS;
2178}
2179
2180
2181/**
2182 * Writes the Hardware Event Register (Hi).
2183 */
2184static VBOXSTRICTRC iommuAmdHwEvtHi_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2185{
2186 /** @todo IOMMU: Why the heck is this marked read/write by the AMD IOMMU spec? */
2187 RT_NOREF(pDevIns, offReg);
2188 LogFlowFunc(("Writing %#RX64 to hardware event (Hi) register!\n", u64Value));
2189 pThis->HwEvtHi.u64 = u64Value;
2190 return VINF_SUCCESS;
2191}
2192
2193
2194/**
2195 * Writes the Hardware Event Register (Lo).
2196 */
2197static VBOXSTRICTRC iommuAmdHwEvtLo_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2198{
2199 /** @todo IOMMU: Why the heck is this marked read/write by the AMD IOMMU spec? */
2200 RT_NOREF(pDevIns, offReg);
2201 LogFlowFunc(("Writing %#RX64 to hardware event (Lo) register!\n", u64Value));
2202 pThis->HwEvtLo = u64Value;
2203 return VINF_SUCCESS;
2204}
2205
2206
2207/**
2208 * Writes the Hardware Event Status Register.
2209 */
2210static VBOXSTRICTRC iommuAmdHwEvtStatus_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2211{
2212 RT_NOREF(pDevIns, offReg);
2213
2214 /* Mask out all unrecognized bits. */
2215 u64Value &= IOMMU_HW_EVT_STATUS_VALID_MASK;
2216
2217 /*
2218 * The two bits (HEO and HEV) are RW1C (Read/Write 1-to-Clear; writing 0 has no effect).
2219 * If the current status bits or the bits being written are both 0, we've nothing to do.
2220 * The Overflow bit (bit 1) is only valid when the Valid bit (bit 0) is 1.
2221 */
2222 uint64_t HwStatus = pThis->HwEvtStatus.u64;
2223 if (!(HwStatus & RT_BIT(0)))
2224 return VINF_SUCCESS;
2225 if (u64Value & HwStatus & RT_BIT_64(0))
2226 HwStatus &= ~RT_BIT_64(0);
2227 if (u64Value & HwStatus & RT_BIT_64(1))
2228 HwStatus &= ~RT_BIT_64(1);
2229
2230 /* Update the register. */
2231 pThis->HwEvtStatus.u64 = HwStatus;
2232 return VINF_SUCCESS;
2233}
2234
2235
2236/**
2237 * Writes the Device Table Segment Base Address Register.
2238 */
2239static VBOXSTRICTRC iommuAmdDevTabSegBar_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2240{
2241 RT_NOREF(pDevIns);
2242
2243 /* Figure out which segment is being written. */
2244 uint8_t const offSegment = (offReg - IOMMU_MMIO_OFF_DEV_TAB_SEG_FIRST) >> 3;
2245 uint8_t const idxSegment = offSegment + 1;
2246 Assert(idxSegment < RT_ELEMENTS(pThis->aDevTabBaseAddrs));
2247
2248 /* Mask out all unrecognized bits. */
2249 u64Value &= IOMMU_DEV_TAB_SEG_BAR_VALID_MASK;
2250 DEV_TAB_BAR_T DevTabSegBar;
2251 DevTabSegBar.u64 = u64Value;
2252
2253 /* Validate the size. */
2254 uint16_t const uSegSize = DevTabSegBar.n.u9Size;
2255 uint16_t const uMaxSegSize = g_auDevTabSegMaxSizes[idxSegment];
2256 if (uSegSize <= uMaxSegSize)
2257 {
2258 /* Update the register. */
2259 pThis->aDevTabBaseAddrs[idxSegment].u64 = u64Value;
2260 }
2261 else
2262 LogFunc(("Device table segment (%u) size invalid (%#RX32) -> Ignored\n", idxSegment, uSegSize));
2263
2264 return VINF_SUCCESS;
2265}
2266
2267
2268/**
2269 * Writes the MSI Vector Register 0 (32-bit) and the MSI Vector Register 1 (32-bit).
2270 */
2271static VBOXSTRICTRC iommuAmdDevMsiVector_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2272{
2273 RT_NOREF(pDevIns, offReg);
2274
2275 /* MSI Vector Register 0 is read-only. */
2276 /* MSI Vector Register 1. */
2277 uint32_t const uReg = u64Value >> 32;
2278 pThis->MiscInfo.au32[1] = uReg & IOMMU_MSI_VECTOR_1_VALID_MASK;
2279 return VINF_SUCCESS;
2280}
2281
2282
2283/**
2284 * Writes the MSI Capability Header Register (32-bit) or the MSI Address (Lo)
2285 * Register (32-bit).
2286 */
2287static VBOXSTRICTRC iommuAmdMsiCapHdrAndAddrLo_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2288{
2289 RT_NOREF(pThis, offReg);
2290 PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
2291 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
2292
2293 /* MSI capability header. */
2294 {
2295 uint32_t const uReg = u64Value;
2296 MSI_CAP_HDR_T MsiCapHdr;
2297 MsiCapHdr.u32 = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_CAP_HDR);
2298 MsiCapHdr.n.u1MsiEnable = RT_BOOL(uReg & IOMMU_MSI_CAP_HDR_MSI_EN_MASK);
2299 PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MSI_CAP_HDR, MsiCapHdr.u32);
2300 }
2301
2302 /* MSI Address Lo. */
2303 {
2304 uint32_t const uReg = u64Value >> 32;
2305 uint32_t const uMsiAddrLo = uReg & VBOX_MSI_ADDR_VALID_MASK;
2306 PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MSI_ADDR_LO, uMsiAddrLo);
2307 }
2308
2309 return VINF_SUCCESS;
2310}
2311
2312
2313/**
2314 * Writes the MSI Address (Hi) Register (32-bit) or the MSI data register (32-bit).
2315 */
2316static VBOXSTRICTRC iommuAmdMsiAddrHiAndData_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2317{
2318 RT_NOREF(pThis, offReg);
2319 PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
2320 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
2321
2322 /* MSI Address Hi. */
2323 {
2324 uint32_t const uReg = u64Value;
2325 PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MSI_ADDR_HI, uReg);
2326 }
2327
2328 /* MSI Data. */
2329 {
2330 uint32_t const uReg = u64Value >> 32;
2331 uint32_t const uMsiData = uReg & VBOX_MSI_DATA_VALID_MASK;
2332 PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MSI_DATA, uMsiData);
2333 }
2334
2335 return VINF_SUCCESS;
2336}
2337
2338
2339/**
2340 * Writes the Command Buffer Head Pointer Register.
2341 */
2342static VBOXSTRICTRC iommuAmdCmdBufHeadPtr_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2343{
2344 RT_NOREF(pDevIns, offReg);
2345
2346 /*
2347 * IOMMU behavior is undefined when software writes this register when the command buffer is running.
2348 * In our emulation, we ignore the write entirely.
2349 * See AMD IOMMU spec. 3.3.13 "Command and Event Log Pointer Registers".
2350 */
2351 if (pThis->Status.n.u1CmdBufRunning)
2352 {
2353 LogFunc(("Setting CmdBufHeadPtr (%#RX64) when command buffer is running -> Ignored\n", u64Value));
2354 return VINF_SUCCESS;
2355 }
2356
2357 /*
2358 * IOMMU behavior is undefined when software writes a value outside the buffer length.
2359 * In our emulation, we ignore the write entirely.
2360 */
2361 uint32_t const offBuf = u64Value & IOMMU_CMD_BUF_HEAD_PTR_VALID_MASK;
2362 uint32_t const cbBuf = iommuAmdGetTotalBufLength(pThis->CmdBufBaseAddr.n.u4Len);
2363 Assert(cbBuf <= _512K);
2364 if (offBuf >= cbBuf)
2365 {
2366 LogFunc(("Setting CmdBufHeadPtr (%#RX32) to a value that exceeds buffer length (%#RX23) -> Ignored\n", offBuf, cbBuf));
2367 return VINF_SUCCESS;
2368 }
2369
2370 /* Update the register. */
2371 pThis->CmdBufHeadPtr.au32[0] = offBuf;
2372
2373 iommuAmdCmdThreadWakeUpIfNeeded(pDevIns);
2374
2375 Log4Func(("Set CmdBufHeadPtr to %#RX32\n", offBuf));
2376 return VINF_SUCCESS;
2377}
2378
2379
2380/**
2381 * Writes the Command Buffer Tail Pointer Register.
2382 */
2383static VBOXSTRICTRC iommuAmdCmdBufTailPtr_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2384{
2385 RT_NOREF(pDevIns, offReg);
2386
2387 /*
2388 * IOMMU behavior is undefined when software writes a value outside the buffer length.
2389 * In our emulation, we ignore the write entirely.
2390 * See AMD IOMMU spec. 3.3.13 "Command and Event Log Pointer Registers".
2391 */
2392 uint32_t const offBuf = u64Value & IOMMU_CMD_BUF_TAIL_PTR_VALID_MASK;
2393 uint32_t const cbBuf = iommuAmdGetTotalBufLength(pThis->CmdBufBaseAddr.n.u4Len);
2394 Assert(cbBuf <= _512K);
2395 if (offBuf >= cbBuf)
2396 {
2397 LogFunc(("Setting CmdBufTailPtr (%#RX32) to a value that exceeds buffer length (%#RX32) -> Ignored\n", offBuf, cbBuf));
2398 return VINF_SUCCESS;
2399 }
2400
2401 /*
2402 * IOMMU behavior is undefined if software advances the tail pointer equal to or beyond the
2403 * head pointer after adding one or more commands to the buffer.
2404 *
2405 * However, we cannot enforce this strictly because it's legal for software to shrink the
2406 * command queue (by reducing the offset) as well as wrap around the pointer (when head isn't
2407 * at 0). Software might even make the queue empty by making head and tail equal which is
2408 * allowed. I don't think we can or should try too hard to prevent software shooting itself
2409 * in the foot here. As long as we make sure the offset value is within the circular buffer
2410 * bounds (which we do by masking bits above) it should be sufficient.
2411 */
2412 pThis->CmdBufTailPtr.au32[0] = offBuf;
2413
2414 iommuAmdCmdThreadWakeUpIfNeeded(pDevIns);
2415
2416 Log4Func(("Set CmdBufTailPtr to %#RX32\n", offBuf));
2417 return VINF_SUCCESS;
2418}
2419
2420
2421/**
2422 * Writes the Event Log Head Pointer Register.
2423 */
2424static VBOXSTRICTRC iommuAmdEvtLogHeadPtr_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2425{
2426 RT_NOREF(pDevIns, offReg);
2427
2428 /*
2429 * IOMMU behavior is undefined when software writes a value outside the buffer length.
2430 * In our emulation, we ignore the write entirely.
2431 * See AMD IOMMU spec. 3.3.13 "Command and Event Log Pointer Registers".
2432 */
2433 uint32_t const offBuf = u64Value & IOMMU_EVT_LOG_HEAD_PTR_VALID_MASK;
2434 uint32_t const cbBuf = iommuAmdGetTotalBufLength(pThis->EvtLogBaseAddr.n.u4Len);
2435 Assert(cbBuf <= _512K);
2436 if (offBuf >= cbBuf)
2437 {
2438 LogFunc(("Setting EvtLogHeadPtr (%#RX32) to a value that exceeds buffer length (%#RX32) -> Ignored\n", offBuf, cbBuf));
2439 return VINF_SUCCESS;
2440 }
2441
2442 /* Update the register. */
2443 pThis->EvtLogHeadPtr.au32[0] = offBuf;
2444
2445 LogFlowFunc(("Set EvtLogHeadPtr to %#RX32\n", offBuf));
2446 return VINF_SUCCESS;
2447}
2448
2449
2450/**
2451 * Writes the Event Log Tail Pointer Register.
2452 */
2453static VBOXSTRICTRC iommuAmdEvtLogTailPtr_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2454{
2455 RT_NOREF(pDevIns, offReg);
2456 NOREF(pThis);
2457
2458 /*
2459 * IOMMU behavior is undefined when software writes this register when the event log is running.
2460 * In our emulation, we ignore the write entirely.
2461 * See AMD IOMMU spec. 3.3.13 "Command and Event Log Pointer Registers".
2462 */
2463 if (pThis->Status.n.u1EvtLogRunning)
2464 {
2465 LogFunc(("Setting EvtLogTailPtr (%#RX64) when event log is running -> Ignored\n", u64Value));
2466 return VINF_SUCCESS;
2467 }
2468
2469 /*
2470 * IOMMU behavior is undefined when software writes a value outside the buffer length.
2471 * In our emulation, we ignore the write entirely.
2472 */
2473 uint32_t const offBuf = u64Value & IOMMU_EVT_LOG_TAIL_PTR_VALID_MASK;
2474 uint32_t const cbBuf = iommuAmdGetTotalBufLength(pThis->EvtLogBaseAddr.n.u4Len);
2475 Assert(cbBuf <= _512K);
2476 if (offBuf >= cbBuf)
2477 {
2478 LogFunc(("Setting EvtLogTailPtr (%#RX32) to a value that exceeds buffer length (%#RX32) -> Ignored\n", offBuf, cbBuf));
2479 return VINF_SUCCESS;
2480 }
2481
2482 /* Update the register. */
2483 pThis->EvtLogTailPtr.au32[0] = offBuf;
2484
2485 LogFlowFunc(("Set EvtLogTailPtr to %#RX32\n", offBuf));
2486 return VINF_SUCCESS;
2487}
2488
2489
2490/**
2491 * Writes the Status Register.
2492 */
2493static VBOXSTRICTRC iommuAmdStatus_w(PPDMDEVINS pDevIns, PIOMMU pThis, uint32_t offReg, uint64_t u64Value)
2494{
2495 RT_NOREF(pDevIns, offReg);
2496
2497 /* Mask out all unrecognized bits. */
2498 u64Value &= IOMMU_STATUS_VALID_MASK;
2499
2500 /*
2501 * Compute RW1C (read-only, write-1-to-clear) bits and preserve the rest (which are read-only).
2502 * Writing 0 to an RW1C bit has no effect. Writing 1 to an RW1C bit, clears the bit if it's already 1.
2503 */
2504 IOMMU_STATUS_T const OldStatus = pThis->Status;
2505 uint64_t const fOldRw1cBits = (OldStatus.u64 & IOMMU_STATUS_RW1C_MASK);
2506 uint64_t const fOldRoBits = (OldStatus.u64 & ~IOMMU_STATUS_RW1C_MASK);
2507 uint64_t const fNewRw1cBits = (u64Value & IOMMU_STATUS_RW1C_MASK);
2508
2509 uint64_t const uNewStatus = (fOldRw1cBits & ~fNewRw1cBits) | fOldRoBits;
2510
2511 /* Update the register. */
2512 ASMAtomicWriteU64(&pThis->Status.u64, uNewStatus);
2513 return VINF_SUCCESS;
2514}
2515
2516
2517/**
2518 * Register access table 0.
2519 * The MMIO offset of each entry must be a multiple of 8!
2520 */
2521static const IOMMUREGACC g_aRegAccess0[] =
2522{
2523 /* MMIO off. Register name Read function Write function */
2524 { /* 0x00 */ "DEV_TAB_BAR", iommuAmdDevTabBar_r, iommuAmdDevTabBar_w },
2525 { /* 0x08 */ "CMD_BUF_BAR", iommuAmdCmdBufBar_r, iommuAmdCmdBufBar_w },
2526 { /* 0x10 */ "EVT_LOG_BAR", iommuAmdEvtLogBar_r, iommuAmdEvtLogBar_w },
2527 { /* 0x18 */ "CTRL", iommuAmdCtrl_r, iommuAmdCtrl_w },
2528 { /* 0x20 */ "EXCL_BAR", iommuAmdExclRangeBar_r, iommuAmdExclRangeBar_w },
2529 { /* 0x28 */ "EXCL_RANGE_LIMIT", iommuAmdExclRangeLimit_r, iommuAmdExclRangeLimit_w },
2530 { /* 0x30 */ "EXT_FEAT", iommuAmdExtFeat_r, NULL },
2531 { /* 0x38 */ "PPR_LOG_BAR", iommuAmdPprLogBar_r, NULL },
2532 { /* 0x40 */ "HW_EVT_HI", iommuAmdHwEvtHi_r, iommuAmdHwEvtHi_w },
2533 { /* 0x48 */ "HW_EVT_LO", iommuAmdHwEvtLo_r, iommuAmdHwEvtLo_w },
2534 { /* 0x50 */ "HW_EVT_STATUS", iommuAmdHwEvtStatus_r, iommuAmdHwEvtStatus_w },
2535 { /* 0x58 */ NULL, NULL, NULL },
2536
2537 { /* 0x60 */ "SMI_FLT_0", NULL, NULL },
2538 { /* 0x68 */ "SMI_FLT_1", NULL, NULL },
2539 { /* 0x70 */ "SMI_FLT_2", NULL, NULL },
2540 { /* 0x78 */ "SMI_FLT_3", NULL, NULL },
2541 { /* 0x80 */ "SMI_FLT_4", NULL, NULL },
2542 { /* 0x88 */ "SMI_FLT_5", NULL, NULL },
2543 { /* 0x90 */ "SMI_FLT_6", NULL, NULL },
2544 { /* 0x98 */ "SMI_FLT_7", NULL, NULL },
2545 { /* 0xa0 */ "SMI_FLT_8", NULL, NULL },
2546 { /* 0xa8 */ "SMI_FLT_9", NULL, NULL },
2547 { /* 0xb0 */ "SMI_FLT_10", NULL, NULL },
2548 { /* 0xb8 */ "SMI_FLT_11", NULL, NULL },
2549 { /* 0xc0 */ "SMI_FLT_12", NULL, NULL },
2550 { /* 0xc8 */ "SMI_FLT_13", NULL, NULL },
2551 { /* 0xd0 */ "SMI_FLT_14", NULL, NULL },
2552 { /* 0xd8 */ "SMI_FLT_15", NULL, NULL },
2553
2554 { /* 0xe0 */ "GALOG_BAR", iommuAmdGALogBar_r, NULL },
2555 { /* 0xe8 */ "GALOG_TAIL_ADDR", NULL, NULL },
2556 { /* 0xf0 */ "PPR_LOG_B_BAR", iommuAmdPprLogBBaseAddr_r, NULL },
2557 { /* 0xf8 */ "PPR_EVT_B_BAR", iommuAmdEvtLogBBaseAddr_r, NULL },
2558
2559 { /* 0x100 */ "DEV_TAB_SEG_1", iommuAmdDevTabSegBar_r, iommuAmdDevTabSegBar_w },
2560 { /* 0x108 */ "DEV_TAB_SEG_2", iommuAmdDevTabSegBar_r, iommuAmdDevTabSegBar_w },
2561 { /* 0x110 */ "DEV_TAB_SEG_3", iommuAmdDevTabSegBar_r, iommuAmdDevTabSegBar_w },
2562 { /* 0x118 */ "DEV_TAB_SEG_4", iommuAmdDevTabSegBar_r, iommuAmdDevTabSegBar_w },
2563 { /* 0x120 */ "DEV_TAB_SEG_5", iommuAmdDevTabSegBar_r, iommuAmdDevTabSegBar_w },
2564 { /* 0x128 */ "DEV_TAB_SEG_6", iommuAmdDevTabSegBar_r, iommuAmdDevTabSegBar_w },
2565 { /* 0x130 */ "DEV_TAB_SEG_7", iommuAmdDevTabSegBar_r, iommuAmdDevTabSegBar_w },
2566
2567 { /* 0x138 */ "DEV_SPECIFIC_FEAT", iommuAmdDevSpecificFeat_r, NULL },
2568 { /* 0x140 */ "DEV_SPECIFIC_CTRL", iommuAmdDevSpecificCtrl_r, NULL },
2569 { /* 0x148 */ "DEV_SPECIFIC_STATUS", iommuAmdDevSpecificStatus_r, NULL },
2570
2571 { /* 0x150 */ "MSI_VECTOR_0 or MSI_VECTOR_1", iommuAmdDevMsiVector_r, iommuAmdDevMsiVector_w },
2572 { /* 0x158 */ "MSI_CAP_HDR or MSI_ADDR_LO", iommuAmdMsiCapHdrAndAddrLo_r, iommuAmdMsiCapHdrAndAddrLo_w },
2573 { /* 0x160 */ "MSI_ADDR_HI or MSI_DATA", iommuAmdMsiAddrHiAndData_r, iommuAmdMsiAddrHiAndData_w },
2574 { /* 0x168 */ "MSI_MAPPING_CAP_HDR or PERF_OPT_CTRL", NULL, NULL },
2575
2576 { /* 0x170 */ "XT_GEN_INTR_CTRL", NULL, NULL },
2577 { /* 0x178 */ "XT_PPR_INTR_CTRL", NULL, NULL },
2578 { /* 0x180 */ "XT_GALOG_INT_CTRL", NULL, NULL },
2579};
2580AssertCompile(RT_ELEMENTS(g_aRegAccess0) == (IOMMU_MMIO_OFF_QWORD_TABLE_0_END - IOMMU_MMIO_OFF_QWORD_TABLE_0_START) / 8);
2581
2582/**
2583 * Register access table 1.
2584 * The MMIO offset of each entry must be a multiple of 8!
2585 */
2586static const IOMMUREGACC g_aRegAccess1[] =
2587{
2588 /* MMIO offset Register name Read function Write function */
2589 { /* 0x200 */ "MARC_APER_BAR_0", NULL, NULL },
2590 { /* 0x208 */ "MARC_APER_RELOC_0", NULL, NULL },
2591 { /* 0x210 */ "MARC_APER_LEN_0", NULL, NULL },
2592 { /* 0x218 */ "MARC_APER_BAR_1", NULL, NULL },
2593 { /* 0x220 */ "MARC_APER_RELOC_1", NULL, NULL },
2594 { /* 0x228 */ "MARC_APER_LEN_1", NULL, NULL },
2595 { /* 0x230 */ "MARC_APER_BAR_2", NULL, NULL },
2596 { /* 0x238 */ "MARC_APER_RELOC_2", NULL, NULL },
2597 { /* 0x240 */ "MARC_APER_LEN_2", NULL, NULL },
2598 { /* 0x248 */ "MARC_APER_BAR_3", NULL, NULL },
2599 { /* 0x250 */ "MARC_APER_RELOC_3", NULL, NULL },
2600 { /* 0x258 */ "MARC_APER_LEN_3", NULL, NULL }
2601};
2602AssertCompile(RT_ELEMENTS(g_aRegAccess1) == (IOMMU_MMIO_OFF_QWORD_TABLE_1_END - IOMMU_MMIO_OFF_QWORD_TABLE_1_START) / 8);
2603
2604/**
2605 * Register access table 2.
2606 * The MMIO offset of each entry must be a multiple of 8!
2607 */
2608static const IOMMUREGACC g_aRegAccess2[] =
2609{
2610 /* MMIO offset Register name Read Function Write function */
2611 { /* 0x1ff8 */ "RSVD_REG", NULL, NULL },
2612
2613 { /* 0x2000 */ "CMD_BUF_HEAD_PTR", iommuAmdCmdBufHeadPtr_r, iommuAmdCmdBufHeadPtr_w },
2614 { /* 0x2008 */ "CMD_BUF_TAIL_PTR", iommuAmdCmdBufTailPtr_r , iommuAmdCmdBufTailPtr_w },
2615 { /* 0x2010 */ "EVT_LOG_HEAD_PTR", iommuAmdEvtLogHeadPtr_r, iommuAmdEvtLogHeadPtr_w },
2616 { /* 0x2018 */ "EVT_LOG_TAIL_PTR", iommuAmdEvtLogTailPtr_r, iommuAmdEvtLogTailPtr_w },
2617
2618 { /* 0x2020 */ "STATUS", iommuAmdStatus_r, iommuAmdStatus_w },
2619 { /* 0x2028 */ NULL, NULL, NULL },
2620
2621 { /* 0x2030 */ "PPR_LOG_HEAD_PTR", NULL, NULL },
2622 { /* 0x2038 */ "PPR_LOG_TAIL_PTR", NULL, NULL },
2623
2624 { /* 0x2040 */ "GALOG_HEAD_PTR", NULL, NULL },
2625 { /* 0x2048 */ "GALOG_TAIL_PTR", NULL, NULL },
2626
2627 { /* 0x2050 */ "PPR_LOG_B_HEAD_PTR", NULL, NULL },
2628 { /* 0x2058 */ "PPR_LOG_B_TAIL_PTR", NULL, NULL },
2629
2630 { /* 0x2060 */ NULL, NULL, NULL },
2631 { /* 0x2068 */ NULL, NULL, NULL },
2632
2633 { /* 0x2070 */ "EVT_LOG_B_HEAD_PTR", NULL, NULL },
2634 { /* 0x2078 */ "EVT_LOG_B_TAIL_PTR", NULL, NULL },
2635
2636 { /* 0x2080 */ "PPR_LOG_AUTO_RESP", NULL, NULL },
2637 { /* 0x2088 */ "PPR_LOG_OVERFLOW_EARLY", NULL, NULL },
2638 { /* 0x2090 */ "PPR_LOG_B_OVERFLOW_EARLY", NULL, NULL }
2639};
2640AssertCompile(RT_ELEMENTS(g_aRegAccess2) == (IOMMU_MMIO_OFF_QWORD_TABLE_2_END - IOMMU_MMIO_OFF_QWORD_TABLE_2_START) / 8);
2641
2642
2643/**
2644 * Gets the register access structure given its MMIO offset.
2645 *
2646 * @returns The register access structure, or NULL if the offset is invalid.
2647 * @param off The MMIO offset of the register being accessed.
2648 */
2649static PCIOMMUREGACC iommuAmdGetRegAccess(uint32_t off)
2650{
2651 /* Figure out which table the register belongs to and validate its index. */
2652 PCIOMMUREGACC pReg;
2653 if (off < IOMMU_MMIO_OFF_QWORD_TABLE_0_END)
2654 {
2655 uint32_t const idxReg = off >> 3;
2656 Assert(idxReg < RT_ELEMENTS(g_aRegAccess0));
2657 pReg = &g_aRegAccess0[idxReg];
2658 }
2659 else if ( off < IOMMU_MMIO_OFF_QWORD_TABLE_1_END
2660 && off >= IOMMU_MMIO_OFF_QWORD_TABLE_1_START)
2661 {
2662 uint32_t const idxReg = (off - IOMMU_MMIO_OFF_QWORD_TABLE_1_START) >> 3;
2663 Assert(idxReg < RT_ELEMENTS(g_aRegAccess1));
2664 pReg = &g_aRegAccess1[idxReg];
2665 }
2666 else if ( off < IOMMU_MMIO_OFF_QWORD_TABLE_2_END
2667 && off >= IOMMU_MMIO_OFF_QWORD_TABLE_2_START)
2668 {
2669 uint32_t const idxReg = (off - IOMMU_MMIO_OFF_QWORD_TABLE_2_START) >> 3;
2670 Assert(idxReg < RT_ELEMENTS(g_aRegAccess2));
2671 pReg = &g_aRegAccess2[idxReg];
2672 }
2673 else
2674 return NULL;
2675 return pReg;
2676}
2677
2678
2679/**
2680 * Writes an IOMMU register (32-bit and 64-bit).
2681 *
2682 * @returns Strict VBox status code.
2683 * @param pDevIns The IOMMU device instance.
2684 * @param off MMIO byte offset to the register.
2685 * @param cb The size of the write access.
2686 * @param uValue The value being written.
2687 *
2688 * @thread EMT.
2689 */
2690static VBOXSTRICTRC iommuAmdRegisterWrite(PPDMDEVINS pDevIns, uint32_t off, uint8_t cb, uint64_t uValue)
2691{
2692 /*
2693 * Validate the access in case of IOM bug or incorrect assumption.
2694 */
2695 Assert(off < IOMMU_MMIO_REGION_SIZE);
2696 AssertMsgReturn(cb == 4 || cb == 8, ("Invalid access size %u\n", cb), VINF_SUCCESS);
2697 AssertMsgReturn(!(off & 3), ("Invalid offset %#x\n", off), VINF_SUCCESS);
2698
2699 Log4Func(("off=%#x cb=%u uValue=%#RX64\n", off, cb, uValue));
2700
2701 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
2702 PCIOMMUREGACC pReg = iommuAmdGetRegAccess(off);
2703 if (pReg)
2704 { /* likely */ }
2705 else
2706 {
2707 LogFunc(("Writing unknown register %#x with %#RX64 -> Ignored\n", off, uValue));
2708 return VINF_SUCCESS;
2709 }
2710
2711 /* If a write handler doesn't exist, it's either a reserved or read-only register. */
2712 if (pReg->pfnWrite)
2713 { /* likely */ }
2714 else
2715 {
2716 LogFunc(("Writing reserved or read-only register off=%#x (cb=%u) with %#RX64 -> Ignored\n", off, cb, uValue));
2717 return VINF_SUCCESS;
2718 }
2719
2720 /*
2721 * If the write access is 64-bits and aligned on a 64-bit boundary, dispatch right away.
2722 * This handles writes to 64-bit registers as well as aligned, 64-bit writes to two
2723 * consecutive 32-bit registers.
2724 */
2725 if (cb == 8)
2726 {
2727 if (!(off & 7))
2728 return pReg->pfnWrite(pDevIns, pThis, off, uValue);
2729
2730 LogFunc(("Misaligned access while writing register at off=%#x (cb=%u) with %#RX64 -> Ignored\n", off, cb, uValue));
2731 return VINF_SUCCESS;
2732 }
2733
2734 /* We shouldn't get sizes other than 32 bits here as we've specified so with IOM. */
2735 Assert(cb == 4);
2736 if (!(off & 7))
2737 {
2738 /*
2739 * Lower 32 bits of a 64-bit register or a 32-bit register is being written.
2740 * Merge with higher 32 bits (after reading the full 64-bits) and perform a 64-bit write.
2741 */
2742 uint64_t u64Read;
2743 if (pReg->pfnRead)
2744 {
2745 VBOXSTRICTRC rcStrict = pReg->pfnRead(pDevIns, pThis, off, &u64Read);
2746 if (RT_FAILURE(rcStrict))
2747 {
2748 LogFunc(("Reading off %#x during split write failed! rc=%Rrc\n -> Ignored", off, VBOXSTRICTRC_VAL(rcStrict)));
2749 return rcStrict;
2750 }
2751 }
2752 else
2753 u64Read = 0;
2754
2755 uValue = (u64Read & UINT64_C(0xffffffff00000000)) | uValue;
2756 return pReg->pfnWrite(pDevIns, pThis, off, uValue);
2757 }
2758
2759 /*
2760 * Higher 32 bits of a 64-bit register or a 32-bit register at a 32-bit boundary is being written.
2761 * Merge with lower 32 bits (after reading the full 64-bits) and perform a 64-bit write.
2762 */
2763 Assert(!(off & 3));
2764 Assert(off & 7);
2765 Assert(off >= 4);
2766 uint64_t u64Read;
2767 if (pReg->pfnRead)
2768 {
2769 VBOXSTRICTRC rcStrict = pReg->pfnRead(pDevIns, pThis, off - 4, &u64Read);
2770 if (RT_FAILURE(rcStrict))
2771 {
2772 LogFunc(("Reading off %#x during split write failed! rc=%Rrc\n -> Ignored", off, VBOXSTRICTRC_VAL(rcStrict)));
2773 return rcStrict;
2774 }
2775 }
2776 else
2777 u64Read = 0;
2778
2779 uValue = (uValue << 32) | (u64Read & UINT64_C(0xffffffff));
2780 return pReg->pfnWrite(pDevIns, pThis, off - 4, uValue);
2781}
2782
2783
2784/**
2785 * Reads an IOMMU register (64-bit) given its MMIO offset.
2786 *
2787 * All reads are 64-bit but reads to 32-bit registers that are aligned on an 8-byte
2788 * boundary include the lower half of the subsequent register.
2789 *
2790 * This is because most registers are 64-bit and aligned on 8-byte boundaries but
2791 * some are really 32-bit registers aligned on an 8-byte boundary. We cannot assume
2792 * software will only perform 32-bit reads on those 32-bit registers that are
2793 * aligned on 8-byte boundaries.
2794 *
2795 * @returns Strict VBox status code.
2796 * @param pDevIns The IOMMU device instance.
2797 * @param off The MMIO offset of the register in bytes.
2798 * @param puResult Where to store the value being read.
2799 *
2800 * @thread EMT.
2801 */
2802static VBOXSTRICTRC iommuAmdRegisterRead(PPDMDEVINS pDevIns, uint32_t off, uint64_t *puResult)
2803{
2804 Assert(off < IOMMU_MMIO_REGION_SIZE);
2805 Assert(!(off & 7) || !(off & 3));
2806
2807 Log4Func(("off=%#x\n", off));
2808
2809 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
2810 PCPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
2811 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev); NOREF(pPciDev);
2812
2813 PCIOMMUREGACC pReg = iommuAmdGetRegAccess(off);
2814 if (pReg)
2815 { /* likely */ }
2816 else
2817 {
2818 LogFunc(("Reading unknown register %#x -> Ignored\n", off));
2819 return VINF_IOM_MMIO_UNUSED_FF;
2820 }
2821
2822 /* If a read handler doesn't exist, it's a reserved or unknown register. */
2823 if (pReg->pfnRead)
2824 { /* likely */ }
2825 else
2826 {
2827 LogFunc(("Reading reserved or unknown register off=%#x -> returning 0s\n", off));
2828 return VINF_IOM_MMIO_UNUSED_00;
2829 }
2830
2831 /*
2832 * If the read access is aligned on a 64-bit boundary, read the full 64-bits and return.
2833 * The caller takes care of truncating upper 32 bits for 32-bit reads.
2834 */
2835 if (!(off & 7))
2836 return pReg->pfnRead(pDevIns, pThis, off, puResult);
2837
2838 /*
2839 * High 32 bits of a 64-bit register or a 32-bit register at a non 64-bit boundary is being read.
2840 * Read full 64 bits at the previous 64-bit boundary but return only the high 32 bits.
2841 */
2842 Assert(!(off & 3));
2843 Assert(off & 7);
2844 Assert(off >= 4);
2845 VBOXSTRICTRC rcStrict = pReg->pfnRead(pDevIns, pThis, off - 4, puResult);
2846 if (RT_SUCCESS(rcStrict))
2847 *puResult >>= 32;
2848 else
2849 {
2850 *puResult = 0;
2851 LogFunc(("Reading off %#x during split read failed! rc=%Rrc\n -> Ignored", off, VBOXSTRICTRC_VAL(rcStrict)));
2852 }
2853
2854 return rcStrict;
2855}
2856
2857
2858/**
2859 * Raises the MSI interrupt for the IOMMU device.
2860 *
2861 * @param pDevIns The IOMMU device instance.
2862 *
2863 * @thread Any.
2864 * @remarks The IOMMU lock may or may not be held.
2865 */
2866static void iommuAmdMsiInterruptRaise(PPDMDEVINS pDevIns)
2867{
2868 LogFlowFunc(("\n"));
2869 if (iommuAmdIsMsiEnabled(pDevIns))
2870 {
2871 LogFunc(("Raising MSI\n"));
2872 PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_HIGH);
2873 }
2874}
2875
2876#if 0
2877/**
2878 * Clears the MSI interrupt for the IOMMU device.
2879 *
2880 * @param pDevIns The IOMMU device instance.
2881 *
2882 * @thread Any.
2883 * @remarks The IOMMU lock may or may not be held.
2884 */
2885static void iommuAmdMsiInterruptClear(PPDMDEVINS pDevIns)
2886{
2887 if (iommuAmdIsMsiEnabled(pDevIns))
2888 PDMDevHlpPCISetIrq(pDevIns, 0, PDM_IRQ_LEVEL_LOW);
2889}
2890#endif
2891
2892/**
2893 * Writes an entry to the event log in memory.
2894 *
2895 * @returns VBox status code.
2896 * @param pDevIns The IOMMU device instance.
2897 * @param pEvent The event to log.
2898 *
2899 * @thread Any.
2900 * @remarks The IOMMU lock must be held while calling this function.
2901 */
2902static int iommuAmdEvtLogEntryWrite(PPDMDEVINS pDevIns, PCEVT_GENERIC_T pEvent)
2903{
2904 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
2905 PIOMMUCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUCC);
2906
2907 IOMMU_LOCK_NORET(pDevIns, pThisCC);
2908
2909 /* Check if event logging is active and the log has not overflowed. */
2910 IOMMU_STATUS_T const Status = pThis->Status;
2911 if ( Status.n.u1EvtLogRunning
2912 && !Status.n.u1EvtOverflow)
2913 {
2914 uint32_t const cbEvt = sizeof(*pEvent);
2915
2916 /* Get the offset we need to write the event to in memory (circular buffer offset). */
2917 uint32_t const offEvt = pThis->EvtLogTailPtr.n.off;
2918 Assert(!(offEvt & ~IOMMU_EVT_LOG_TAIL_PTR_VALID_MASK));
2919
2920 /* Ensure we have space in the event log. */
2921 uint32_t const cMaxEvts = iommuAmdGetBufMaxEntries(pThis->EvtLogBaseAddr.n.u4Len);
2922 uint32_t const cEvts = iommuAmdGetEvtLogEntryCount(pThis);
2923 if (cEvts + 1 < cMaxEvts)
2924 {
2925 /* Write the event log entry to memory. */
2926 RTGCPHYS const GCPhysEvtLog = pThis->EvtLogBaseAddr.n.u40Base << X86_PAGE_4K_SHIFT;
2927 RTGCPHYS const GCPhysEvtLogEntry = GCPhysEvtLog + offEvt;
2928 int rc = PDMDevHlpPCIPhysWrite(pDevIns, GCPhysEvtLogEntry, pEvent, cbEvt);
2929 if (RT_FAILURE(rc))
2930 LogFunc(("Failed to write event log entry at %#RGp. rc=%Rrc\n", GCPhysEvtLogEntry, rc));
2931
2932 /* Increment the event log tail pointer. */
2933 uint32_t const cbEvtLog = iommuAmdGetTotalBufLength(pThis->EvtLogBaseAddr.n.u4Len);
2934 pThis->EvtLogTailPtr.n.off = (offEvt + cbEvt) % cbEvtLog;
2935
2936 /* Indicate that an event log entry was written. */
2937 ASMAtomicOrU64(&pThis->Status.u64, IOMMU_STATUS_EVT_LOG_INTR);
2938
2939 /* Check and signal an interrupt if software wants to receive one when an event log entry is written. */
2940 if (pThis->Ctrl.n.u1EvtIntrEn)
2941 iommuAmdMsiInterruptRaise(pDevIns);
2942 }
2943 else
2944 {
2945 /* Indicate that the event log has overflowed. */
2946 ASMAtomicOrU64(&pThis->Status.u64, IOMMU_STATUS_EVT_LOG_OVERFLOW);
2947
2948 /* Check and signal an interrupt if software wants to receive one when the event log has overflowed. */
2949 if (pThis->Ctrl.n.u1EvtIntrEn)
2950 iommuAmdMsiInterruptRaise(pDevIns);
2951 }
2952 }
2953
2954 IOMMU_UNLOCK(pDevIns, pThisCC);
2955
2956 return VINF_SUCCESS;
2957}
2958
2959
2960/**
2961 * Sets an event in the hardware error registers.
2962 *
2963 * @param pDevIns The IOMMU device instance.
2964 * @param pEvent The event.
2965 *
2966 * @thread Any.
2967 */
2968static void iommuAmdHwErrorSet(PPDMDEVINS pDevIns, PCEVT_GENERIC_T pEvent)
2969{
2970 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
2971 if (pThis->ExtFeat.n.u1HwErrorSup)
2972 {
2973 if (pThis->HwEvtStatus.n.u1Valid)
2974 pThis->HwEvtStatus.n.u1Overflow = 1;
2975 pThis->HwEvtStatus.n.u1Valid = 1;
2976 pThis->HwEvtHi.u64 = RT_MAKE_U64(pEvent->au32[0], pEvent->au32[1]);
2977 pThis->HwEvtLo = RT_MAKE_U64(pEvent->au32[2], pEvent->au32[3]);
2978 Assert( pThis->HwEvtHi.n.u4EvtCode == IOMMU_EVT_DEV_TAB_HW_ERROR
2979 || pThis->HwEvtHi.n.u4EvtCode == IOMMU_EVT_PAGE_TAB_HW_ERROR
2980 || pThis->HwEvtHi.n.u4EvtCode == IOMMU_EVT_COMMAND_HW_ERROR);
2981 }
2982}
2983
2984
2985/**
2986 * Initializes a PAGE_TAB_HARDWARE_ERROR event.
2987 *
2988 * @param uDevId The device ID (bus, device, function).
2989 * @param uDomainId The domain ID.
2990 * @param GCPhysPtEntity The system physical address of the page table
2991 * entity.
2992 * @param enmOp The IOMMU operation being performed.
2993 * @param pEvtPageTabHwErr Where to store the initialized event.
2994 */
2995static void iommuAmdPageTabHwErrorEventInit(uint16_t uDevId, uint16_t uDomainId, RTGCPHYS GCPhysPtEntity, IOMMUOP enmOp,
2996 PEVT_PAGE_TAB_HW_ERR_T pEvtPageTabHwErr)
2997{
2998 memset(pEvtPageTabHwErr, 0, sizeof(*pEvtPageTabHwErr));
2999 pEvtPageTabHwErr->n.u16DevId = uDevId;
3000 pEvtPageTabHwErr->n.u16DomainOrPasidLo = uDomainId;
3001 pEvtPageTabHwErr->n.u1GuestOrNested = 0;
3002 pEvtPageTabHwErr->n.u1Interrupt = RT_BOOL(enmOp == IOMMUOP_INTR_REQ);
3003 pEvtPageTabHwErr->n.u1ReadWrite = RT_BOOL(enmOp == IOMMUOP_MEM_WRITE);
3004 pEvtPageTabHwErr->n.u1Translation = RT_BOOL(enmOp == IOMMUOP_TRANSLATE_REQ);
3005 pEvtPageTabHwErr->n.u2Type = enmOp == IOMMUOP_CMD ? HWEVTTYPE_DATA_ERROR : HWEVTTYPE_TARGET_ABORT;
3006 pEvtPageTabHwErr->n.u4EvtCode = IOMMU_EVT_PAGE_TAB_HW_ERROR;
3007 pEvtPageTabHwErr->n.u64Addr = GCPhysPtEntity;
3008}
3009
3010
3011/**
3012 * Raises a PAGE_TAB_HARDWARE_ERROR event.
3013 *
3014 * @param pDevIns The IOMMU device instance.
3015 * @param enmOp The IOMMU operation being performed.
3016 * @param pEvtPageTabHwErr The page table hardware error event.
3017 *
3018 * @thread Any.
3019 */
3020static void iommuAmdPageTabHwErrorEventRaise(PPDMDEVINS pDevIns, IOMMUOP enmOp, PEVT_PAGE_TAB_HW_ERR_T pEvtPageTabHwErr)
3021{
3022 AssertCompile(sizeof(EVT_GENERIC_T) == sizeof(EVT_PAGE_TAB_HW_ERR_T));
3023 PCEVT_GENERIC_T pEvent = (PCEVT_GENERIC_T)pEvtPageTabHwErr;
3024
3025 PIOMMUCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUCC);
3026 IOMMU_LOCK_NORET(pDevIns, pThisCC);
3027
3028 iommuAmdHwErrorSet(pDevIns, (PCEVT_GENERIC_T)pEvent);
3029 iommuAmdEvtLogEntryWrite(pDevIns, (PCEVT_GENERIC_T)pEvent);
3030 if (enmOp != IOMMUOP_CMD)
3031 iommuAmdSetPciTargetAbort(pDevIns);
3032
3033 IOMMU_UNLOCK(pDevIns, pThisCC);
3034
3035 LogFunc(("Raised PAGE_TAB_HARDWARE_ERROR. uDevId=%#x uDomainId=%#x GCPhysPtEntity=%#RGp enmOp=%u u2Type=%u\n",
3036 pEvtPageTabHwErr->n.u16DevId, pEvtPageTabHwErr->n.u16DomainOrPasidLo, pEvtPageTabHwErr->n.u64Addr, enmOp,
3037 pEvtPageTabHwErr->n.u2Type));
3038}
3039
3040
3041#ifdef IN_RING3
3042/**
3043 * Initializes a COMMAND_HARDWARE_ERROR event.
3044 *
3045 * @param GCPhysAddr The system physical address the IOMMU attempted to access.
3046 * @param pEvtCmdHwErr Where to store the initialized event.
3047 */
3048static void iommuAmdCmdHwErrorEventInit(RTGCPHYS GCPhysAddr, PEVT_CMD_HW_ERR_T pEvtCmdHwErr)
3049{
3050 memset(pEvtCmdHwErr, 0, sizeof(*pEvtCmdHwErr));
3051 pEvtCmdHwErr->n.u2Type = HWEVTTYPE_DATA_ERROR;
3052 pEvtCmdHwErr->n.u4EvtCode = IOMMU_EVT_COMMAND_HW_ERROR;
3053 pEvtCmdHwErr->n.u64Addr = GCPhysAddr;
3054}
3055
3056
3057/**
3058 * Raises a COMMAND_HARDWARE_ERROR event.
3059 *
3060 * @param pDevIns The IOMMU device instance.
3061 * @param pEvtCmdHwErr The command hardware error event.
3062 *
3063 * @thread Any.
3064 */
3065static void iommuAmdCmdHwErrorEventRaise(PPDMDEVINS pDevIns, PCEVT_CMD_HW_ERR_T pEvtCmdHwErr)
3066{
3067 AssertCompile(sizeof(EVT_GENERIC_T) == sizeof(EVT_CMD_HW_ERR_T));
3068 PCEVT_GENERIC_T pEvent = (PCEVT_GENERIC_T)pEvtCmdHwErr;
3069 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
3070
3071 PIOMMUCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUCC);
3072 IOMMU_LOCK_NORET(pDevIns, pThisCC);
3073
3074 iommuAmdHwErrorSet(pDevIns, (PCEVT_GENERIC_T)pEvent);
3075 iommuAmdEvtLogEntryWrite(pDevIns, (PCEVT_GENERIC_T)pEvent);
3076 ASMAtomicAndU64(&pThis->Status.u64, ~IOMMU_STATUS_CMD_BUF_RUNNING);
3077
3078 IOMMU_UNLOCK(pDevIns, pThisCC);
3079
3080 LogFunc(("Raised COMMAND_HARDWARE_ERROR. GCPhysCmd=%#RGp u2Type=%u\n", pEvtCmdHwErr->n.u64Addr, pEvtCmdHwErr->n.u2Type));
3081}
3082#endif /* IN_RING3 */
3083
3084
3085/**
3086 * Initializes a DEV_TAB_HARDWARE_ERROR event.
3087 *
3088 * @param uDevId The device ID (bus, device, function).
3089 * @param GCPhysDte The system physical address of the failed device table
3090 * access.
3091 * @param enmOp The IOMMU operation being performed.
3092 * @param pEvtDevTabHwErr Where to store the initialized event.
3093 */
3094static void iommuAmdDevTabHwErrorEventInit(uint16_t uDevId, RTGCPHYS GCPhysDte, IOMMUOP enmOp,
3095 PEVT_DEV_TAB_HW_ERROR_T pEvtDevTabHwErr)
3096{
3097 memset(pEvtDevTabHwErr, 0, sizeof(*pEvtDevTabHwErr));
3098 pEvtDevTabHwErr->n.u16DevId = uDevId;
3099 pEvtDevTabHwErr->n.u1Intr = RT_BOOL(enmOp == IOMMUOP_INTR_REQ);
3100 /** @todo IOMMU: Any other transaction type that can set read/write bit? */
3101 pEvtDevTabHwErr->n.u1ReadWrite = RT_BOOL(enmOp == IOMMUOP_MEM_WRITE);
3102 pEvtDevTabHwErr->n.u1Translation = RT_BOOL(enmOp == IOMMUOP_TRANSLATE_REQ);
3103 pEvtDevTabHwErr->n.u2Type = enmOp == IOMMUOP_CMD ? HWEVTTYPE_DATA_ERROR : HWEVTTYPE_TARGET_ABORT;
3104 pEvtDevTabHwErr->n.u4EvtCode = IOMMU_EVT_DEV_TAB_HW_ERROR;
3105 pEvtDevTabHwErr->n.u64Addr = GCPhysDte;
3106}
3107
3108
3109/**
3110 * Raises a DEV_TAB_HARDWARE_ERROR event.
3111 *
3112 * @param pDevIns The IOMMU device instance.
3113 * @param enmOp The IOMMU operation being performed.
3114 * @param pEvtDevTabHwErr The device table hardware error event.
3115 *
3116 * @thread Any.
3117 */
3118static void iommuAmdDevTabHwErrorEventRaise(PPDMDEVINS pDevIns, IOMMUOP enmOp, PEVT_DEV_TAB_HW_ERROR_T pEvtDevTabHwErr)
3119{
3120 AssertCompile(sizeof(EVT_GENERIC_T) == sizeof(EVT_DEV_TAB_HW_ERROR_T));
3121 PCEVT_GENERIC_T pEvent = (PCEVT_GENERIC_T)pEvtDevTabHwErr;
3122
3123 PIOMMUCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUCC);
3124 IOMMU_LOCK_NORET(pDevIns, pThisCC);
3125
3126 iommuAmdHwErrorSet(pDevIns, (PCEVT_GENERIC_T)pEvent);
3127 iommuAmdEvtLogEntryWrite(pDevIns, (PCEVT_GENERIC_T)pEvent);
3128 if (enmOp != IOMMUOP_CMD)
3129 iommuAmdSetPciTargetAbort(pDevIns);
3130
3131 IOMMU_UNLOCK(pDevIns, pThisCC);
3132
3133 LogFunc(("Raised DEV_TAB_HARDWARE_ERROR. uDevId=%#x GCPhysDte=%#RGp enmOp=%u u2Type=%u\n", pEvtDevTabHwErr->n.u16DevId,
3134 pEvtDevTabHwErr->n.u64Addr, enmOp, pEvtDevTabHwErr->n.u2Type));
3135}
3136
3137
3138#ifdef IN_RING3
3139/**
3140 * Initializes an ILLEGAL_COMMAND_ERROR event.
3141 *
3142 * @param GCPhysCmd The system physical address of the failed command
3143 * access.
3144 * @param pEvtIllegalCmd Where to store the initialized event.
3145 */
3146static void iommuAmdIllegalCmdEventInit(RTGCPHYS GCPhysCmd, PEVT_ILLEGAL_CMD_ERR_T pEvtIllegalCmd)
3147{
3148 Assert(!(GCPhysCmd & UINT64_C(0xf)));
3149 memset(pEvtIllegalCmd, 0, sizeof(*pEvtIllegalCmd));
3150 pEvtIllegalCmd->n.u4EvtCode = IOMMU_EVT_ILLEGAL_CMD_ERROR;
3151 pEvtIllegalCmd->n.u64Addr = GCPhysCmd;
3152}
3153
3154
3155/**
3156 * Raises an ILLEGAL_COMMAND_ERROR event.
3157 *
3158 * @param pDevIns The IOMMU device instance.
3159 * @param pEvtIllegalCmd The illegal command error event.
3160 */
3161static void iommuAmdIllegalCmdEventRaise(PPDMDEVINS pDevIns, PCEVT_ILLEGAL_CMD_ERR_T pEvtIllegalCmd)
3162{
3163 AssertCompile(sizeof(EVT_GENERIC_T) == sizeof(EVT_ILLEGAL_DTE_T));
3164 PCEVT_GENERIC_T pEvent = (PCEVT_GENERIC_T)pEvtIllegalCmd;
3165 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
3166
3167 iommuAmdEvtLogEntryWrite(pDevIns, pEvent);
3168 ASMAtomicAndU64(&pThis->Status.u64, ~IOMMU_STATUS_CMD_BUF_RUNNING);
3169
3170 LogFunc(("Raised ILLEGAL_COMMAND_ERROR. Addr=%#RGp\n", pEvtIllegalCmd->n.u64Addr));
3171}
3172#endif /* IN_RING3 */
3173
3174
3175/**
3176 * Initializes an ILLEGAL_DEV_TABLE_ENTRY event.
3177 *
3178 * @param uDevId The device ID (bus, device, function).
3179 * @param uIova The I/O virtual address.
3180 * @param fRsvdNotZero Whether reserved bits are not zero. Pass @c false if the
3181 * event was caused by an invalid level encoding in the
3182 * DTE.
3183 * @param enmOp The IOMMU operation being performed.
3184 * @param pEvtIllegalDte Where to store the initialized event.
3185 */
3186static void iommuAmdIllegalDteEventInit(uint16_t uDevId, uint64_t uIova, bool fRsvdNotZero, IOMMUOP enmOp,
3187 PEVT_ILLEGAL_DTE_T pEvtIllegalDte)
3188{
3189 memset(pEvtIllegalDte, 0, sizeof(*pEvtIllegalDte));
3190 pEvtIllegalDte->n.u16DevId = uDevId;
3191 pEvtIllegalDte->n.u1Interrupt = RT_BOOL(enmOp == IOMMUOP_INTR_REQ);
3192 pEvtIllegalDte->n.u1ReadWrite = RT_BOOL(enmOp == IOMMUOP_MEM_WRITE);
3193 pEvtIllegalDte->n.u1RsvdNotZero = fRsvdNotZero;
3194 pEvtIllegalDte->n.u1Translation = RT_BOOL(enmOp == IOMMUOP_TRANSLATE_REQ);
3195 pEvtIllegalDte->n.u4EvtCode = IOMMU_EVT_ILLEGAL_DEV_TAB_ENTRY;
3196 pEvtIllegalDte->n.u64Addr = uIova & ~UINT64_C(0x3);
3197 /** @todo r=ramshankar: Not sure why the last 2 bits are marked as reserved by the
3198 * IOMMU spec here but not for this field for I/O page fault event. */
3199 Assert(!(uIova & UINT64_C(0x3)));
3200}
3201
3202
3203/**
3204 * Raises an ILLEGAL_DEV_TABLE_ENTRY event.
3205 *
3206 * @param pDevIns The IOMMU instance data.
3207 * @param enmOp The IOMMU operation being performed.
3208 * @param pEvtIllegalDte The illegal device table entry event.
3209 * @param enmEvtType The illegal device table entry event type.
3210 *
3211 * @thread Any.
3212 */
3213static void iommuAmdIllegalDteEventRaise(PPDMDEVINS pDevIns, IOMMUOP enmOp, PCEVT_ILLEGAL_DTE_T pEvtIllegalDte,
3214 EVT_ILLEGAL_DTE_TYPE_T enmEvtType)
3215{
3216 AssertCompile(sizeof(EVT_GENERIC_T) == sizeof(EVT_ILLEGAL_DTE_T));
3217 PCEVT_GENERIC_T pEvent = (PCEVT_GENERIC_T)pEvtIllegalDte;
3218
3219 iommuAmdEvtLogEntryWrite(pDevIns, pEvent);
3220 if (enmOp != IOMMUOP_CMD)
3221 iommuAmdSetPciTargetAbort(pDevIns);
3222
3223 LogFunc(("Raised ILLEGAL_DTE_EVENT. uDevId=%#x uIova=%#RX64 enmOp=%u enmEvtType=%u\n", pEvtIllegalDte->n.u16DevId,
3224 pEvtIllegalDte->n.u64Addr, enmOp, enmEvtType));
3225 NOREF(enmEvtType);
3226}
3227
3228
3229/**
3230 * Initializes an IO_PAGE_FAULT event.
3231 *
3232 * @param uDevId The device ID (bus, device, function).
3233 * @param uDomainId The domain ID.
3234 * @param uIova The I/O virtual address being accessed.
3235 * @param fPresent Transaction to a page marked as present (including
3236 * DTE.V=1) or interrupt marked as remapped
3237 * (IRTE.RemapEn=1).
3238 * @param fRsvdNotZero Whether reserved bits are not zero. Pass @c false if
3239 * the I/O page fault was caused by invalid level
3240 * encoding.
3241 * @param fPermDenied Permission denied for the address being accessed.
3242 * @param enmOp The IOMMU operation being performed.
3243 * @param pEvtIoPageFault Where to store the initialized event.
3244 */
3245static void iommuAmdIoPageFaultEventInit(uint16_t uDevId, uint16_t uDomainId, uint64_t uIova, bool fPresent, bool fRsvdNotZero,
3246 bool fPermDenied, IOMMUOP enmOp, PEVT_IO_PAGE_FAULT_T pEvtIoPageFault)
3247{
3248 Assert(!fPermDenied || fPresent);
3249 memset(pEvtIoPageFault, 0, sizeof(*pEvtIoPageFault));
3250 pEvtIoPageFault->n.u16DevId = uDevId;
3251 //pEvtIoPageFault->n.u4PasidHi = 0;
3252 pEvtIoPageFault->n.u16DomainOrPasidLo = uDomainId;
3253 //pEvtIoPageFault->n.u1GuestOrNested = 0;
3254 //pEvtIoPageFault->n.u1NoExecute = 0;
3255 //pEvtIoPageFault->n.u1User = 0;
3256 pEvtIoPageFault->n.u1Interrupt = RT_BOOL(enmOp == IOMMUOP_INTR_REQ);
3257 pEvtIoPageFault->n.u1Present = fPresent;
3258 pEvtIoPageFault->n.u1ReadWrite = RT_BOOL(enmOp == IOMMUOP_MEM_WRITE);
3259 pEvtIoPageFault->n.u1PermDenied = fPermDenied;
3260 pEvtIoPageFault->n.u1RsvdNotZero = fRsvdNotZero;
3261 pEvtIoPageFault->n.u1Translation = RT_BOOL(enmOp == IOMMUOP_TRANSLATE_REQ);
3262 pEvtIoPageFault->n.u4EvtCode = IOMMU_EVT_IO_PAGE_FAULT;
3263 pEvtIoPageFault->n.u64Addr = uIova;
3264}
3265
3266
3267/**
3268 * Raises an IO_PAGE_FAULT event.
3269 *
3270 * @param pDevIns The IOMMU instance data.
3271 * @param fIoDevFlags The I/O device flags, see IOMMU_DTE_CACHE_F_XXX.
3272 * @param pIrte The interrupt remapping table entry, can be NULL.
3273 * @param enmOp The IOMMU operation being performed.
3274 * @param pEvtIoPageFault The I/O page fault event.
3275 * @param enmEvtType The I/O page fault event type.
3276 *
3277 * @thread Any.
3278 */
3279static void iommuAmdIoPageFaultEventRaise(PPDMDEVINS pDevIns, uint16_t fIoDevFlags, PCIRTE_T pIrte, IOMMUOP enmOp,
3280 PCEVT_IO_PAGE_FAULT_T pEvtIoPageFault, EVT_IO_PAGE_FAULT_TYPE_T enmEvtType)
3281{
3282 AssertCompile(sizeof(EVT_GENERIC_T) == sizeof(EVT_IO_PAGE_FAULT_T));
3283 PCEVT_GENERIC_T pEvent = (PCEVT_GENERIC_T)pEvtIoPageFault;
3284
3285#ifdef IOMMU_WITH_DTE_CACHE
3286# define IOMMU_DTE_CACHE_SET_PF_RAISED(a_pDevIns, a_DevId) iommuAmdDteCacheAddFlags((a_pDevIns), (a_DevId), \
3287 IOMMU_DTE_CACHE_F_IO_PAGE_FAULT_RAISED)
3288#else
3289# define IOMMU_DTE_CACHE_SET_PF_RAISED(a_pDevIns, a_DevId) do { } while (0)
3290#endif
3291
3292 bool fSuppressEvtLogging = false;
3293 if ( enmOp == IOMMUOP_MEM_READ
3294 || enmOp == IOMMUOP_MEM_WRITE)
3295 {
3296 uint16_t const fSuppressIopf = IOMMU_DTE_CACHE_F_VALID
3297 | IOMMU_DTE_CACHE_F_SUPPRESS_IOPF | IOMMU_DTE_CACHE_F_IO_PAGE_FAULT_RAISED;
3298 uint16_t const fSuppressAllIopf = IOMMU_DTE_CACHE_F_VALID | IOMMU_DTE_CACHE_F_SUPPRESS_ALL_IOPF;
3299 if ( (fIoDevFlags & fSuppressAllIopf) == fSuppressAllIopf
3300 || (fIoDevFlags & fSuppressIopf) == fSuppressIopf)
3301 {
3302 fSuppressEvtLogging = true;
3303 }
3304 }
3305 else if (enmOp == IOMMUOP_INTR_REQ)
3306 {
3307 uint16_t const fSuppressIopf = IOMMU_DTE_CACHE_F_INTR_MAP_VALID | IOMMU_DTE_CACHE_F_IGNORE_UNMAPPED_INTR;
3308 if ((fIoDevFlags & fSuppressIopf) == fSuppressIopf)
3309 fSuppressEvtLogging = true;
3310 else if (pIrte) /** @todo Make this compulsary and assert if it isn't provided. */
3311 fSuppressEvtLogging = pIrte->n.u1SuppressIoPf;
3312 }
3313 /* else: Events are never suppressed for commands. */
3314
3315 switch (enmEvtType)
3316 {
3317 case kIoPageFaultType_PermDenied:
3318 {
3319 /* Cannot be triggered by a command. */
3320 Assert(enmOp != IOMMUOP_CMD);
3321 RT_FALL_THRU();
3322 }
3323 case kIoPageFaultType_DteRsvdPagingMode:
3324 case kIoPageFaultType_PteInvalidPageSize:
3325 case kIoPageFaultType_PteInvalidLvlEncoding:
3326 case kIoPageFaultType_SkippedLevelIovaNotZero:
3327 case kIoPageFaultType_PteRsvdNotZero:
3328 case kIoPageFaultType_PteValidNotSet:
3329 case kIoPageFaultType_DteTranslationDisabled:
3330 case kIoPageFaultType_PasidInvalidRange:
3331 {
3332 /*
3333 * For a translation request, the IOMMU doesn't signal an I/O page fault nor does it
3334 * create an event log entry. See AMD IOMMU spec. 2.1.3.2 "I/O Page Faults".
3335 */
3336 if (enmOp != IOMMUOP_TRANSLATE_REQ)
3337 {
3338 if (!fSuppressEvtLogging)
3339 {
3340 iommuAmdEvtLogEntryWrite(pDevIns, pEvent);
3341 IOMMU_DTE_CACHE_SET_PF_RAISED(pDevIns, pEvtIoPageFault->n.u16DevId);
3342 }
3343 if (enmOp != IOMMUOP_CMD)
3344 iommuAmdSetPciTargetAbort(pDevIns);
3345 }
3346 break;
3347 }
3348
3349 case kIoPageFaultType_UserSupervisor:
3350 {
3351 /* Access is blocked and only creates an event log entry. */
3352 if (!fSuppressEvtLogging)
3353 {
3354 iommuAmdEvtLogEntryWrite(pDevIns, pEvent);
3355 IOMMU_DTE_CACHE_SET_PF_RAISED(pDevIns, pEvtIoPageFault->n.u16DevId);
3356 }
3357 break;
3358 }
3359
3360 case kIoPageFaultType_IrteAddrInvalid:
3361 case kIoPageFaultType_IrteRsvdNotZero:
3362 case kIoPageFaultType_IrteRemapEn:
3363 case kIoPageFaultType_IrteRsvdIntType:
3364 case kIoPageFaultType_IntrReqAborted:
3365 case kIoPageFaultType_IntrWithPasid:
3366 {
3367 /* Only trigerred by interrupt requests. */
3368 Assert(enmOp == IOMMUOP_INTR_REQ);
3369 if (!fSuppressEvtLogging)
3370 {
3371 iommuAmdEvtLogEntryWrite(pDevIns, pEvent);
3372 IOMMU_DTE_CACHE_SET_PF_RAISED(pDevIns, pEvtIoPageFault->n.u16DevId);
3373 }
3374 iommuAmdSetPciTargetAbort(pDevIns);
3375 break;
3376 }
3377
3378 case kIoPageFaultType_SmiFilterMismatch:
3379 {
3380 /* Not supported and probably will never be, assert. */
3381 AssertMsgFailed(("kIoPageFaultType_SmiFilterMismatch - Upstream SMI requests not supported/implemented."));
3382 break;
3383 }
3384
3385 case kIoPageFaultType_DevId_Invalid:
3386 {
3387 /* Cannot be triggered by a command. */
3388 Assert(enmOp != IOMMUOP_CMD);
3389 Assert(enmOp != IOMMUOP_TRANSLATE_REQ); /** @todo IOMMU: We don't support translation requests yet. */
3390 if (!fSuppressEvtLogging)
3391 {
3392 iommuAmdEvtLogEntryWrite(pDevIns, pEvent);
3393 IOMMU_DTE_CACHE_SET_PF_RAISED(pDevIns, pEvtIoPageFault->n.u16DevId);
3394 }
3395 if ( enmOp == IOMMUOP_MEM_READ
3396 || enmOp == IOMMUOP_MEM_WRITE)
3397 iommuAmdSetPciTargetAbort(pDevIns);
3398 break;
3399 }
3400 }
3401
3402#undef IOMMU_DTE_CACHE_SET_PF_RAISED
3403}
3404
3405
3406/**
3407 * Raises an IO_PAGE_FAULT event given the DTE.
3408 *
3409 * @param pDevIns The IOMMU instance data.
3410 * @param pDte The device table entry.
3411 * @param pIrte The interrupt remapping table entry, can be NULL.
3412 * @param enmOp The IOMMU operation being performed.
3413 * @param pEvtIoPageFault The I/O page fault event.
3414 * @param enmEvtType The I/O page fault event type.
3415 *
3416 * @thread Any.
3417 */
3418static void iommuAmdIoPageFaultEventRaiseWithDte(PPDMDEVINS pDevIns, PCDTE_T pDte, PCIRTE_T pIrte, IOMMUOP enmOp,
3419 PCEVT_IO_PAGE_FAULT_T pEvtIoPageFault, EVT_IO_PAGE_FAULT_TYPE_T enmEvtType)
3420{
3421 Assert(pDte);
3422 uint16_t const fIoDevFlags = iommuAmdGetBasicDevFlags(pDte);
3423 return iommuAmdIoPageFaultEventRaise(pDevIns, fIoDevFlags, pIrte, enmOp, pEvtIoPageFault, enmEvtType);
3424}
3425
3426
3427/**
3428 * Reads a device table entry for the given the device ID.
3429 *
3430 * @returns VBox status code.
3431 * @param pDevIns The IOMMU device instance.
3432 * @param uDevId The device ID (bus, device, function).
3433 * @param enmOp The IOMMU operation being performed.
3434 * @param pDte Where to store the device table entry.
3435 *
3436 * @thread Any.
3437 */
3438static int iommuAmdDteRead(PPDMDEVINS pDevIns, uint16_t uDevId, IOMMUOP enmOp, PDTE_T pDte)
3439{
3440 PCIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
3441 PIOMMUCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUCC);
3442
3443 IOMMU_LOCK(pDevIns, pThisCC);
3444
3445 /* Figure out which device table segment is being accessed. */
3446 uint8_t const idxSegsEn = pThis->Ctrl.n.u3DevTabSegEn;
3447 Assert(idxSegsEn < RT_ELEMENTS(g_auDevTabSegShifts));
3448
3449 uint8_t const idxSeg = (uDevId & g_auDevTabSegMasks[idxSegsEn]) >> g_auDevTabSegShifts[idxSegsEn];
3450 Assert(idxSeg < RT_ELEMENTS(pThis->aDevTabBaseAddrs));
3451 AssertCompile(RT_ELEMENTS(g_auDevTabSegShifts) == RT_ELEMENTS(g_auDevTabSegMasks));
3452
3453 RTGCPHYS const GCPhysDevTab = pThis->aDevTabBaseAddrs[idxSeg].n.u40Base << X86_PAGE_4K_SHIFT;
3454 uint32_t const offDte = (uDevId & ~g_auDevTabSegMasks[idxSegsEn]) * sizeof(DTE_T);
3455 RTGCPHYS const GCPhysDte = GCPhysDevTab + offDte;
3456
3457 /* Ensure the DTE falls completely within the device table segment. */
3458 uint32_t const cbDevTabSeg = (pThis->aDevTabBaseAddrs[idxSeg].n.u9Size + 1) << X86_PAGE_4K_SHIFT;
3459
3460 IOMMU_UNLOCK(pDevIns, pThisCC);
3461
3462 if (offDte + sizeof(DTE_T) <= cbDevTabSeg)
3463 {
3464 /* Read the device table entry from guest memory. */
3465 Assert(!(GCPhysDevTab & X86_PAGE_4K_OFFSET_MASK));
3466 int rc = PDMDevHlpPCIPhysRead(pDevIns, GCPhysDte, pDte, sizeof(*pDte));
3467 if (RT_SUCCESS(rc))
3468 return rc;
3469
3470 /* Raise a device table hardware error. */
3471 LogFunc(("Failed to read device table entry at %#RGp. rc=%Rrc -> DevTabHwError\n", GCPhysDte, rc));
3472
3473 EVT_DEV_TAB_HW_ERROR_T EvtDevTabHwErr;
3474 iommuAmdDevTabHwErrorEventInit(uDevId, GCPhysDte, enmOp, &EvtDevTabHwErr);
3475 iommuAmdDevTabHwErrorEventRaise(pDevIns, enmOp, &EvtDevTabHwErr);
3476 return VERR_IOMMU_DTE_READ_FAILED;
3477 }
3478
3479 /* Raise an I/O page fault for out-of-bounds acccess. */
3480 EVT_IO_PAGE_FAULT_T EvtIoPageFault;
3481 iommuAmdIoPageFaultEventInit(uDevId, 0 /* uDomainId */, 0 /* uIova */, false /* fPresent */, false /* fRsvdNotZero */,
3482 false /* fPermDenied */, enmOp, &EvtIoPageFault);
3483 iommuAmdIoPageFaultEventRaise(pDevIns, 0 /* fIoDevFlags */, NULL /* pIrte */, enmOp, &EvtIoPageFault,
3484 kIoPageFaultType_DevId_Invalid);
3485 return VERR_IOMMU_DTE_BAD_OFFSET;
3486}
3487
3488
3489/**
3490 * Performs pre-translation checks for the given device table entry.
3491 *
3492 * @returns VBox status code.
3493 * @retval VINF_SUCCESS if the DTE is valid and supports address translation.
3494 * @retval VINF_IOMMU_ADDR_TRANSLATION_DISABLED if the DTE is valid but address
3495 * translation is disabled.
3496 * @retval VERR_IOMMU_ADDR_TRANSLATION_FAILED if an error occurred and any
3497 * corresponding event was raised.
3498 * @retval VERR_IOMMU_ADDR_ACCESS_DENIED if the DTE denies the requested
3499 * permissions.
3500 *
3501 * @param pDevIns The IOMMU device instance.
3502 * @param uIova The I/O virtual address to translate.
3503 * @param uDevId The device ID (bus, device, function).
3504 * @param fPerm The I/O permissions for this access, see
3505 * IOMMU_IO_PERM_XXX.
3506 * @param pDte The device table entry.
3507 * @param enmOp The IOMMU operation being performed.
3508 *
3509 * @thread Any.
3510 */
3511static int iommuAmdPreTranslateChecks(PPDMDEVINS pDevIns, uint16_t uDevId, uint64_t uIova, uint8_t fPerm, PCDTE_T pDte,
3512 IOMMUOP enmOp)
3513{
3514 /*
3515 * Check if the translation is valid, otherwise raise an I/O page fault.
3516 */
3517 if (pDte->n.u1TranslationValid)
3518 { /* likely */ }
3519 else
3520 {
3521 /** @todo r=ramshankar: The AMD IOMMU spec. says page walk is terminated but
3522 * doesn't explicitly say whether an I/O page fault is raised. From other
3523 * places in the spec. it seems early page walk terminations (starting with
3524 * the DTE) return the state computed so far and raises an I/O page fault. So
3525 * returning an invalid translation rather than skipping translation. */
3526 LogFunc(("Translation valid bit not set -> IOPF\n"));
3527 EVT_IO_PAGE_FAULT_T EvtIoPageFault;
3528 iommuAmdIoPageFaultEventInit(uDevId, pDte->n.u16DomainId, uIova, false /* fPresent */, false /* fRsvdNotZero */,
3529 false /* fPermDenied */, enmOp, &EvtIoPageFault);
3530 iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
3531 kIoPageFaultType_DteTranslationDisabled);
3532 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
3533 }
3534
3535 /*
3536 * Check permissions bits in the DTE.
3537 * Note: This MUST be checked prior to checking the root page table level below!
3538 */
3539 uint8_t const fDtePerm = (pDte->au64[0] >> IOMMU_IO_PERM_SHIFT) & IOMMU_IO_PERM_MASK;
3540 if ((fPerm & fDtePerm) == fPerm)
3541 { /* likely */ }
3542 else
3543 {
3544 LogFunc(("Permission denied by DTE (fPerm=%#x fDtePerm=%#x) -> IOPF\n", fPerm, fDtePerm));
3545 EVT_IO_PAGE_FAULT_T EvtIoPageFault;
3546 iommuAmdIoPageFaultEventInit(uDevId, pDte->n.u16DomainId, uIova, true /* fPresent */, false /* fRsvdNotZero */,
3547 true /* fPermDenied */, enmOp, &EvtIoPageFault);
3548 iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
3549 kIoPageFaultType_PermDenied);
3550 return VERR_IOMMU_ADDR_ACCESS_DENIED;
3551 }
3552
3553 /*
3554 * If the root page table level is 0, translation is disabled and GPA=SPA and
3555 * the DTE.IR and DTE.IW bits control permissions (verified above).
3556 */
3557 uint8_t const uMaxLevel = pDte->n.u3Mode;
3558 if (uMaxLevel != 0)
3559 { /* likely */ }
3560 else
3561 {
3562 Assert((fPerm & fDtePerm) == fPerm); /* Verify we've checked permissions. */
3563 return VINF_IOMMU_ADDR_TRANSLATION_DISABLED;
3564 }
3565
3566 /*
3567 * If the root page table level exceeds the allowed host-address translation level,
3568 * page walk is terminated and translation fails.
3569 */
3570 if (uMaxLevel <= IOMMU_MAX_HOST_PT_LEVEL)
3571 { /* likely */ }
3572 else
3573 {
3574 /** @todo r=ramshankar: I cannot make out from the AMD IOMMU spec. if I should be
3575 * raising an ILLEGAL_DEV_TABLE_ENTRY event or an IO_PAGE_FAULT event here.
3576 * I'm just going with I/O page fault. */
3577 LogFunc(("Invalid root page table level %#x (uDevId=%#x) -> IOPF\n", uMaxLevel, uDevId));
3578 EVT_IO_PAGE_FAULT_T EvtIoPageFault;
3579 iommuAmdIoPageFaultEventInit(uDevId, pDte->n.u16DomainId, uIova, true /* fPresent */, false /* fRsvdNotZero */,
3580 false /* fPermDenied */, enmOp, &EvtIoPageFault);
3581 iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
3582 kIoPageFaultType_PteInvalidLvlEncoding);
3583 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
3584 }
3585
3586 /* The DTE allows translations for this device. */
3587 return VINF_SUCCESS;
3588}
3589
3590
3591/**
3592 * Walks the I/O page table to translate the I/O virtual address to a system
3593 * physical address.
3594 *
3595 * @returns VBox status code.
3596 * @param pDevIns The IOMMU device instance.
3597 * @param uIova The I/O virtual address to translate. Must be 4K aligned.
3598 * @param fPerm The I/O permissions for this access, see
3599 * IOMMU_IO_PERM_XXX.
3600 * @param uDevId The device ID (bus, device, function).
3601 * @param pDte The device table entry.
3602 * @param enmOp The IOMMU operation being performed.
3603 * @param pPageLookup Where to store the results of the I/O page lookup. This
3604 * is only updated when VINF_SUCCESS is returned.
3605 *
3606 * @thread Any.
3607 */
3608static int iommuAmdIoPageTableWalk(PPDMDEVINS pDevIns, uint64_t uIova, uint8_t fPerm, uint16_t uDevId, PCDTE_T pDte,
3609 IOMMUOP enmOp, PIOPAGELOOKUP pPageLookup)
3610{
3611 Assert(pDte->n.u1Valid);
3612 Assert(!(uIova & X86_PAGE_4K_OFFSET_MASK));
3613
3614 /* The virtual address bits indexing table. */
3615 static uint8_t const s_acIovaLevelShifts[] = { 0, 12, 21, 30, 39, 48, 57, 0 };
3616 static uint64_t const s_auIovaLevelMasks[] = { UINT64_C(0x0000000000000000),
3617 UINT64_C(0x00000000001ff000),
3618 UINT64_C(0x000000003fe00000),
3619 UINT64_C(0x0000007fc0000000),
3620 UINT64_C(0x0000ff8000000000),
3621 UINT64_C(0x01ff000000000000),
3622 UINT64_C(0xfe00000000000000),
3623 UINT64_C(0x0000000000000000) };
3624 AssertCompile(RT_ELEMENTS(s_acIovaLevelShifts) == RT_ELEMENTS(s_auIovaLevelMasks));
3625 AssertCompile(RT_ELEMENTS(s_acIovaLevelShifts) > IOMMU_MAX_HOST_PT_LEVEL);
3626
3627 /* Traverse the I/O page table starting with the page directory in the DTE. */
3628 IOPTENTITY_T PtEntity;
3629 PtEntity.u64 = pDte->au64[0];
3630 for (;;)
3631 {
3632 /* Figure out the system physical address of the page table at the current level. */
3633 uint8_t const uLevel = PtEntity.n.u3NextLevel;
3634
3635 /* Read the page table entity at the current level. */
3636 {
3637 Assert(uLevel > 0 && uLevel < RT_ELEMENTS(s_acIovaLevelShifts));
3638 Assert(uLevel <= IOMMU_MAX_HOST_PT_LEVEL);
3639 uint16_t const idxPte = (uIova >> s_acIovaLevelShifts[uLevel]) & UINT64_C(0x1ff);
3640 uint64_t const offPte = idxPte << 3;
3641 RTGCPHYS const GCPhysPtEntity = (PtEntity.u64 & IOMMU_PTENTITY_ADDR_MASK) + offPte;
3642 int rc = PDMDevHlpPCIPhysRead(pDevIns, GCPhysPtEntity, &PtEntity.u64, sizeof(PtEntity));
3643 if (RT_FAILURE(rc))
3644 {
3645 LogFunc(("Failed to read page table entry at %#RGp. rc=%Rrc -> PageTabHwError\n", GCPhysPtEntity, rc));
3646 EVT_PAGE_TAB_HW_ERR_T EvtPageTabHwErr;
3647 iommuAmdPageTabHwErrorEventInit(uDevId, pDte->n.u16DomainId, GCPhysPtEntity, enmOp, &EvtPageTabHwErr);
3648 iommuAmdPageTabHwErrorEventRaise(pDevIns, enmOp, &EvtPageTabHwErr);
3649 return VERR_IOMMU_IPE_2;
3650 }
3651 }
3652
3653 /* Check present bit. */
3654 if (PtEntity.n.u1Present)
3655 { /* likely */ }
3656 else
3657 {
3658 LogFunc(("Page table entry not present (uDevId=%#x) -> IOPF\n", uDevId));
3659 EVT_IO_PAGE_FAULT_T EvtIoPageFault;
3660 iommuAmdIoPageFaultEventInit(uDevId, pDte->n.u16DomainId, uIova, false /* fPresent */, false /* fRsvdNotZero */,
3661 false /* fPermDenied */, enmOp, &EvtIoPageFault);
3662 iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
3663 kIoPageFaultType_PermDenied);
3664 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
3665 }
3666
3667 /* Check permission bits. */
3668 uint8_t const fPtePerm = (PtEntity.u64 >> IOMMU_IO_PERM_SHIFT) & IOMMU_IO_PERM_MASK;
3669 if ((fPerm & fPtePerm) == fPerm)
3670 { /* likely */ }
3671 else
3672 {
3673 LogFunc(("Page table entry access denied (uDevId=%#x fPerm=%#x fPtePerm=%#x) -> IOPF\n", uDevId, fPerm, fPtePerm));
3674 EVT_IO_PAGE_FAULT_T EvtIoPageFault;
3675 iommuAmdIoPageFaultEventInit(uDevId, pDte->n.u16DomainId, uIova, true /* fPresent */, false /* fRsvdNotZero */,
3676 true /* fPermDenied */, enmOp, &EvtIoPageFault);
3677 iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
3678 kIoPageFaultType_PermDenied);
3679 return VERR_IOMMU_ADDR_ACCESS_DENIED;
3680 }
3681
3682 /* If this is a PTE, we're at the final level and we're done. */
3683 uint8_t const uNextLevel = PtEntity.n.u3NextLevel;
3684 if (uNextLevel == 0)
3685 {
3686 /* The page size of the translation is the default (4K). */
3687 pPageLookup->GCPhysSpa = PtEntity.u64 & IOMMU_PTENTITY_ADDR_MASK;
3688 pPageLookup->cShift = X86_PAGE_4K_SHIFT;
3689 pPageLookup->fPerm = fPtePerm;
3690 return VINF_SUCCESS;
3691 }
3692 if (uNextLevel == 7)
3693 {
3694 /* The default page size of the translation is overridden. */
3695 RTGCPHYS const GCPhysPte = PtEntity.u64 & IOMMU_PTENTITY_ADDR_MASK;
3696 uint8_t cShift = X86_PAGE_4K_SHIFT;
3697 while (GCPhysPte & RT_BIT_64(cShift++))
3698 ;
3699
3700 /* The page size must be larger than the default size and lower than the default size of the higher level. */
3701 Assert(uLevel < IOMMU_MAX_HOST_PT_LEVEL); /* PTE at level 6 handled outside the loop, uLevel should be <= 5. */
3702 if ( cShift > s_acIovaLevelShifts[uLevel]
3703 && cShift < s_acIovaLevelShifts[uLevel + 1])
3704 {
3705 pPageLookup->GCPhysSpa = GCPhysPte;
3706 pPageLookup->cShift = cShift;
3707 pPageLookup->fPerm = fPtePerm;
3708 return VINF_SUCCESS;
3709 }
3710
3711 LogFunc(("Page size invalid cShift=%#x -> IOPF\n", cShift));
3712 EVT_IO_PAGE_FAULT_T EvtIoPageFault;
3713 iommuAmdIoPageFaultEventInit(uDevId, pDte->n.u16DomainId, uIova, true /* fPresent */, false /* fRsvdNotZero */,
3714 false /* fPermDenied */, enmOp, &EvtIoPageFault);
3715 iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
3716 kIoPageFaultType_PteInvalidPageSize);
3717 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
3718 }
3719
3720 /* Validate the next level encoding of the PDE. */
3721#if IOMMU_MAX_HOST_PT_LEVEL < 6
3722 if (uNextLevel <= IOMMU_MAX_HOST_PT_LEVEL)
3723 { /* likely */ }
3724 else
3725 {
3726 LogFunc(("Next level of PDE invalid uNextLevel=%#x -> IOPF\n", uNextLevel));
3727 EVT_IO_PAGE_FAULT_T EvtIoPageFault;
3728 iommuAmdIoPageFaultEventInit(uDevId, pDte->n.u16DomainId, uIova, true /* fPresent */, false /* fRsvdNotZero */,
3729 false /* fPermDenied */, enmOp, &EvtIoPageFault);
3730 iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
3731 kIoPageFaultType_PteInvalidLvlEncoding);
3732 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
3733 }
3734#else
3735 Assert(uNextLevel <= IOMMU_MAX_HOST_PT_LEVEL);
3736#endif
3737
3738 /* Validate level transition. */
3739 if (uNextLevel < uLevel)
3740 { /* likely */ }
3741 else
3742 {
3743 LogFunc(("Next level (%#x) must be less than the current level (%#x) -> IOPF\n", uNextLevel, uLevel));
3744 EVT_IO_PAGE_FAULT_T EvtIoPageFault;
3745 iommuAmdIoPageFaultEventInit(uDevId, pDte->n.u16DomainId, uIova, true /* fPresent */, false /* fRsvdNotZero */,
3746 false /* fPermDenied */, enmOp, &EvtIoPageFault);
3747 iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
3748 kIoPageFaultType_PteInvalidLvlEncoding);
3749 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
3750 }
3751
3752 /* Ensure IOVA bits of skipped levels are zero. */
3753 Assert(uLevel > 0);
3754 uint64_t uIovaSkipMask = 0;
3755 for (unsigned idxLevel = uLevel - 1; idxLevel > uNextLevel; idxLevel--)
3756 uIovaSkipMask |= s_auIovaLevelMasks[idxLevel];
3757 if (!(uIova & uIovaSkipMask))
3758 { /* likely */ }
3759 else
3760 {
3761 LogFunc(("IOVA of skipped levels are not zero %#RX64 (SkipMask=%#RX64) -> IOPF\n", uIova, uIovaSkipMask));
3762 EVT_IO_PAGE_FAULT_T EvtIoPageFault;
3763 iommuAmdIoPageFaultEventInit(uDevId, pDte->n.u16DomainId, uIova, true /* fPresent */, false /* fRsvdNotZero */,
3764 false /* fPermDenied */, enmOp, &EvtIoPageFault);
3765 iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
3766 kIoPageFaultType_SkippedLevelIovaNotZero);
3767 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
3768 }
3769
3770 /* Continue with traversing the page directory at this level. */
3771 }
3772}
3773
3774
3775/**
3776 * Page lookup callback for finding an I/O page from guest memory.
3777 *
3778 * @returns VBox status code.
3779 * @retval VINF_SUCCESS when the page is found and has the right permissions.
3780 * @retval VERR_IOMMU_ADDR_TRANSLATION_FAILED when address translation fails.
3781 * @retval VERR_IOMMU_ADDR_ACCESS_DENIED when the page is found but permissions are
3782 * insufficient to what is requested.
3783 *
3784 * @param pDevIns The IOMMU instance data.
3785 * @param uIovaPage The I/O virtual address to lookup in the cache (must be
3786 * 4K aligned).
3787 * @param fPerm The I/O permissions for this access, see
3788 * IOMMU_IO_PERM_XXX.
3789 * @param pAux The auxiliary information required during lookup.
3790 * @param pPageLookup Where to store the looked up I/O page.
3791 */
3792static DECLCALLBACK(int) iommuAmdDteLookupPage(PPDMDEVINS pDevIns, uint64_t uIovaPage, uint8_t fPerm, PCIOMMUOPAUX pAux,
3793 PIOPAGELOOKUP pPageLookup)
3794{
3795 AssertPtr(pAux);
3796 AssertPtr(pPageLookup);
3797 Assert(!(uIovaPage & X86_PAGE_4K_OFFSET_MASK));
3798
3799 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
3800 STAM_PROFILE_ADV_START(&pThis->StatProfDteLookup, a);
3801 int rc = iommuAmdIoPageTableWalk(pDevIns, uIovaPage, fPerm, pAux->uDeviceId, pAux->pDte, pAux->enmOp, pPageLookup);
3802 STAM_PROFILE_ADV_STOP(&pThis->StatProfDteLookup, a);
3803 return rc;
3804}
3805
3806
3807/**
3808 * Looks up a range of I/O virtual addresses.
3809 *
3810 * @returns VBox status code.
3811 * @param pDevIns The IOMMU instance data.
3812 * @param pfnIoPageLookup The lookup function to use.
3813 * @param pAddrIn The I/O address range to lookup.
3814 * @param pAux The auxiliary information required by the lookup
3815 * function.
3816 * @param pAddrOut Where to store the translated I/O address range.
3817 * @param pcbPages Where to store the size of the access (round up to
3818 * the page size). Optional, can be NULL.
3819 */
3820static int iommuAmdLookupIoAddrRange(PPDMDEVINS pDevIns, PFNIOPAGELOOKUP pfnIoPageLookup, PCIOADDRRANGE pAddrIn,
3821 PCIOMMUOPAUX pAux, PIOADDRRANGE pAddrOut, size_t *pcbPages)
3822{
3823 AssertPtr(pfnIoPageLookup);
3824 AssertPtr(pAddrIn);
3825 AssertPtr(pAddrOut);
3826
3827 int rc;
3828 size_t const cbAccess = pAddrIn->cb;
3829 uint8_t const fPerm = pAddrIn->fPerm;
3830 uint64_t const uIova = pAddrIn->uAddr;
3831 RTGCPHYS GCPhysSpa = NIL_RTGCPHYS;
3832 size_t cbRemaining = cbAccess;
3833 uint64_t uIovaPage = pAddrIn->uAddr & X86_PAGE_4K_BASE_MASK;
3834 uint64_t offIova = pAddrIn->uAddr & X86_PAGE_4K_OFFSET_MASK;
3835 uint64_t cbPages = 0;
3836
3837 IOPAGELOOKUP PageLookupPrev;
3838 RT_ZERO(PageLookupPrev);
3839 for (;;)
3840 {
3841 IOPAGELOOKUP PageLookup;
3842 rc = pfnIoPageLookup(pDevIns, uIovaPage, fPerm, pAux, &PageLookup);
3843 if (RT_SUCCESS(rc))
3844 {
3845 Assert(PageLookup.cShift >= X86_PAGE_4K_SHIFT);
3846
3847 /* Store the translated address before continuing to access more pages. */
3848 if (cbRemaining == cbAccess)
3849 {
3850 uint64_t const offMask = IOMMU_GET_PAGE_OFF_MASK(PageLookup.cShift);
3851 uint64_t const offSpa = uIova & offMask;
3852 Assert(!(PageLookup.GCPhysSpa & offMask));
3853 GCPhysSpa = PageLookup.GCPhysSpa | offSpa;
3854 }
3855 /* Check if addresses translated so far result in a physically contiguous region. */
3856 else if (!iommuAmdLookupIsAccessContig(&PageLookupPrev, &PageLookup))
3857 {
3858 rc = VERR_OUT_OF_RANGE;
3859 break;
3860 }
3861
3862 /* Store the page lookup result from the first/previous page. */
3863 PageLookupPrev = PageLookup;
3864
3865 /* Update size of all pages read thus far. */
3866 uint64_t const cbPage = RT_BIT_64(PageLookup.cShift);
3867 cbPages += cbPage;
3868
3869 /* Check if we need to access more pages. */
3870 if (cbRemaining > cbPage - offIova)
3871 {
3872 cbRemaining -= (cbPage - offIova); /* Calculate how much more we need to access. */
3873 uIovaPage += cbPage; /* Update address of the next access. */
3874 offIova = 0; /* After first page, all pages are accessed from off 0. */
3875 }
3876 else
3877 {
3878 cbRemaining = 0;
3879 break;
3880 }
3881 }
3882 else
3883 break;
3884 }
3885
3886 pAddrOut->uAddr = GCPhysSpa; /* Update the translated address. */
3887 pAddrOut->cb = cbAccess - cbRemaining; /* Update the size of the contiguous memory region. */
3888 pAddrOut->fPerm = PageLookupPrev.fPerm; /* Update the allowed permissions for this access. */
3889 if (pcbPages)
3890 *pcbPages = cbPages; /* Update the size of the pages accessed. */
3891 return rc;
3892}
3893
3894
3895/**
3896 * Looks up an I/O virtual address from the device table.
3897 *
3898 * @returns VBox status code.
3899 * @param pDevIns The IOMMU instance data.
3900 * @param uDevId The device ID (bus, device, function).
3901 * @param uIova The I/O virtual address to lookup.
3902 * @param cbAccess The size of the access.
3903 * @param fPerm The I/O permissions for this access, see
3904 * IOMMU_IO_PERM_XXX.
3905 * @param enmOp The IOMMU operation being performed.
3906 * @param pGCPhysSpa Where to store the translated system physical address.
3907 * @param pcbContiguous Where to store the number of contiguous bytes translated
3908 * and permission-checked.
3909 *
3910 * @thread Any.
3911 */
3912static int iommuAmdDteLookup(PPDMDEVINS pDevIns, uint16_t uDevId, uint64_t uIova, size_t cbAccess, uint8_t fPerm, IOMMUOP enmOp,
3913 PRTGCPHYS pGCPhysSpa, size_t *pcbContiguous)
3914{
3915 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
3916 RTGCPHYS GCPhysSpa = NIL_RTGCPHYS;
3917 size_t cbContiguous = 0;
3918
3919 /* Read the device table entry from memory. */
3920 DTE_T Dte;
3921 int rc = iommuAmdDteRead(pDevIns, uDevId, enmOp, &Dte);
3922 if (RT_SUCCESS(rc))
3923 {
3924 if (Dte.n.u1Valid)
3925 {
3926 /* Validate bits 127:0 of the device table entry when DTE.V is 1. */
3927 uint64_t const fRsvd0 = Dte.au64[0] & ~(IOMMU_DTE_QWORD_0_VALID_MASK & ~IOMMU_DTE_QWORD_0_FEAT_MASK);
3928 uint64_t const fRsvd1 = Dte.au64[1] & ~(IOMMU_DTE_QWORD_1_VALID_MASK & ~IOMMU_DTE_QWORD_1_FEAT_MASK);
3929 if (RT_LIKELY(!fRsvd0 && !fRsvd1))
3930 {
3931 /*
3932 * Check if the DTE is configured for translating addresses.
3933 * Note: Addresses cannot be subject to exclusion as we do -not- support remote IOTLBs,
3934 * so there's no need to check the address exclusion base/limit here.
3935 */
3936 rc = iommuAmdPreTranslateChecks(pDevIns, uDevId, uIova, fPerm, &Dte, enmOp);
3937 if (rc == VINF_SUCCESS)
3938 {
3939 IOADDRRANGE AddrIn;
3940 AddrIn.uAddr = uIova;
3941 AddrIn.cb = cbAccess;
3942 AddrIn.fPerm = fPerm;
3943
3944 IOMMUOPAUX Aux;
3945 Aux.enmOp = enmOp;
3946 Aux.pDte = &Dte;
3947 Aux.uDeviceId = uDevId;
3948 Aux.uDomainId = Dte.n.u16DomainId;
3949
3950 IOADDRRANGE AddrOut;
3951
3952 /* Lookup the address from the DTE and I/O page tables.*/
3953 size_t cbPages = 0;
3954 rc = iommuAmdLookupIoAddrRange(pDevIns, iommuAmdDteLookupPage, &AddrIn, &Aux, &AddrOut, &cbPages);
3955 GCPhysSpa = AddrOut.uAddr;
3956 cbContiguous = AddrOut.cb;
3957
3958 /* If we stopped since translation resulted in non-contiguous physical addresses,
3959 what we translated so far is still valid. */
3960 if (rc == VERR_OUT_OF_RANGE)
3961 {
3962 Assert(cbContiguous > 0 && cbContiguous < cbAccess);
3963 rc = VINF_SUCCESS;
3964 STAM_COUNTER_INC(&pThis->StatAccessDteNonContig);
3965 }
3966
3967 if (rc == VERR_IOMMU_ADDR_ACCESS_DENIED)
3968 STAM_COUNTER_INC(&pThis->StatAccessDtePermDenied);
3969
3970#ifdef IOMMU_WITH_IOTLBE_CACHE
3971 if (RT_SUCCESS(rc))
3972 {
3973 /* Update that addresses requires translation (cumulative permissions of DTE and I/O page tables). */
3974 iommuAmdDteCacheAdd(pDevIns, uDevId, &Dte, IOMMU_DTE_CACHE_F_ADDR_TRANSLATE);
3975 /* Update IOTLB for the contiguous range of I/O virtual addresses. */
3976 iommuAmdIotlbAddRange(pDevIns, Dte.n.u16DomainId, uIova & X86_PAGE_4K_BASE_MASK, cbPages,
3977 GCPhysSpa & X86_PAGE_4K_BASE_MASK, AddrOut.fPerm);
3978 }
3979#endif
3980 }
3981 else if (rc == VINF_IOMMU_ADDR_TRANSLATION_DISABLED)
3982 {
3983 /*
3984 * Translation is disabled for this device (root paging mode is 0).
3985 * GPA=SPA, but the permission bits are important and controls accesses.
3986 */
3987 GCPhysSpa = uIova;
3988 cbContiguous = cbAccess;
3989 rc = VINF_SUCCESS;
3990
3991#ifdef IOMMU_WITH_IOTLBE_CACHE
3992 /* Update that addresses permissions of DTE apply (but omit address translation). */
3993 iommuAmdDteCacheAdd(pDevIns, uDevId, &Dte, IOMMU_DTE_CACHE_F_IO_PERM);
3994#endif
3995 }
3996 else
3997 {
3998 /* Address translation failed or access is denied. */
3999 Assert(rc == VERR_IOMMU_ADDR_ACCESS_DENIED || rc == VERR_IOMMU_ADDR_TRANSLATION_FAILED);
4000 GCPhysSpa = NIL_RTGCPHYS;
4001 cbContiguous = 0;
4002 STAM_COUNTER_INC(&pThis->StatAccessDtePermDenied);
4003 }
4004 }
4005 else
4006 {
4007 /* Invalid reserved bits in the DTE, raise an error event. */
4008 LogFunc(("Invalid DTE reserved bits (u64[0]=%#RX64 u64[1]=%#RX64) -> Illegal DTE\n", fRsvd0, fRsvd1));
4009 EVT_ILLEGAL_DTE_T Event;
4010 iommuAmdIllegalDteEventInit(uDevId, uIova, true /* fRsvdNotZero */, enmOp, &Event);
4011 iommuAmdIllegalDteEventRaise(pDevIns, enmOp, &Event, kIllegalDteType_RsvdNotZero);
4012 rc = VERR_IOMMU_ADDR_TRANSLATION_FAILED;
4013 }
4014 }
4015 else
4016 {
4017 /*
4018 * The DTE is not valid, forward addresses untranslated.
4019 * See AMD IOMMU spec. "Table 5: Feature Enablement for Address Translation".
4020 */
4021 GCPhysSpa = uIova;
4022 cbContiguous = cbAccess;
4023
4024#ifdef IOMMU_WITH_IOTLBE_CACHE
4025 /* Update that addresses don't require translation (nor permission checks) but a DTE is present. */
4026 iommuAmdDteCacheAdd(pDevIns, uDevId, &Dte, 0 /* fFlags */);
4027#endif
4028 }
4029 }
4030 else
4031 {
4032 LogFunc(("Failed to read device table entry. uDevId=%#x rc=%Rrc\n", uDevId, rc));
4033 rc = VERR_IOMMU_ADDR_TRANSLATION_FAILED;
4034 }
4035
4036 *pGCPhysSpa = GCPhysSpa;
4037 *pcbContiguous = cbContiguous;
4038 AssertMsg(rc != VINF_SUCCESS || cbContiguous > 0, ("cbContiguous=%zu\n", cbContiguous));
4039 return rc;
4040}
4041
4042
4043#ifdef IOMMU_WITH_IOTLBE_CACHE
4044/**
4045 * I/O page lookup callback for finding an I/O page from the IOTLB.
4046 *
4047 * @returns VBox status code.
4048 * @retval VINF_SUCCESS when the page is found and has the right permissions.
4049 * @retval VERR_NOT_FOUND when the page is not found.
4050 * @retval VERR_IOMMU_ADDR_ACCESS_DENIED when the page is found but permissions are
4051 * insufficient to what is requested.
4052 *
4053 * @param pDevIns The IOMMU instance data.
4054 * @param uIovaPage The I/O virtual address to lookup in the cache (must be
4055 * 4K aligned).
4056 * @param fPerm The I/O permissions for this access, see
4057 * IOMMU_IO_PERM_XXX.
4058 * @param pAux The auxiliary information required during lookup.
4059 * @param pPageLookup Where to store the looked up I/O page.
4060 */
4061static DECLCALLBACK(int) iommuAmdCacheLookupPage(PPDMDEVINS pDevIns, uint64_t uIovaPage, uint8_t fPerm, PCIOMMUOPAUX pAux,
4062 PIOPAGELOOKUP pPageLookup)
4063{
4064 Assert(pAux);
4065 Assert(pPageLookup);
4066 Assert(!(uIovaPage & X86_PAGE_4K_OFFSET_MASK));
4067
4068 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
4069 PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
4070
4071 STAM_PROFILE_ADV_START(&pThis->StatProfIotlbeLookup, a);
4072 PCIOTLBE pIotlbe = iommuAmdIotlbLookup(pThis, pThisR3, pAux->uDomainId, uIovaPage);
4073 STAM_PROFILE_ADV_STOP(&pThis->StatProfIotlbeLookup, a);
4074 if (pIotlbe)
4075 {
4076 *pPageLookup = pIotlbe->PageLookup;
4077 if ((pPageLookup->fPerm & fPerm) == fPerm)
4078 {
4079 STAM_COUNTER_INC(&pThis->StatAccessCacheHit);
4080 return VINF_SUCCESS;
4081 }
4082 return VERR_IOMMU_ADDR_ACCESS_DENIED;
4083 }
4084 return VERR_NOT_FOUND;
4085}
4086
4087
4088/**
4089 * Lookups a memory access from the IOMMU cache.
4090 *
4091 * @returns VBox status code.
4092 * @retval VINF_SUCCESS if the access was cached and permissions are verified.
4093 * @retval VERR_OUT_OF_RANGE if the access resulted in a non-contiguous physical
4094 * address region.
4095 * @retval VERR_NOT_FOUND if the access was not cached.
4096 * @retval VERR_IOMMU_ADDR_ACCESS_DENIED if the access was cached but permissions
4097 * are insufficient.
4098 *
4099 * @param pDevIns The IOMMU instance data.
4100 * @param uDevId The device ID (bus, device, function).
4101 * @param uIova The I/O virtual address to lookup.
4102 * @param cbAccess The size of the access.
4103 * @param fPerm The I/O permissions for this access, see
4104 * IOMMU_IO_PERM_XXX.
4105 * @param enmOp The IOMMU operation being performed.
4106 * @param pGCPhysSpa Where to store the translated system physical address.
4107 * @param pcbContiguous Where to store the number of contiguous bytes translated
4108 * and permission-checked.
4109 */
4110static int iommuAmdCacheLookup(PPDMDEVINS pDevIns, uint16_t uDevId, uint64_t uIova, size_t cbAccess, uint8_t fPerm, IOMMUOP enmOp,
4111 PRTGCPHYS pGCPhysSpa, size_t *pcbContiguous)
4112{
4113 int rc;
4114 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
4115
4116#define IOMMU_IOTLB_LOOKUP_FAILED(a_rc) \
4117 do { \
4118 *pGCPhysSpa = NIL_RTGCPHYS; \
4119 *pcbContiguous = 0; \
4120 rc = (a_rc); \
4121 } while (0)
4122
4123 /*
4124 * We hold the cache lock across both the DTE and the IOTLB lookups (if any) because
4125 * we don't want the DTE cache to be invalidate while we perform IOTBL lookups.
4126 */
4127 IOMMU_LOCK_CACHE(pDevIns, pThis);
4128
4129 /* Lookup the DTE cache entry. */
4130 uint16_t const idxDteCache = iommuAmdDteCacheEntryLookup(pThis, uDevId);
4131 if (idxDteCache < RT_ELEMENTS(pThis->aDteCache))
4132 {
4133 PCDTECACHE pDteCache = &pThis->aDteCache[idxDteCache];
4134 if ((pDteCache->fFlags & (IOMMU_DTE_CACHE_F_PRESENT | IOMMU_DTE_CACHE_F_VALID | IOMMU_DTE_CACHE_F_ADDR_TRANSLATE))
4135 == (IOMMU_DTE_CACHE_F_PRESENT | IOMMU_DTE_CACHE_F_VALID | IOMMU_DTE_CACHE_F_ADDR_TRANSLATE))
4136 {
4137 /* Lookup IOTLB entries. */
4138 IOADDRRANGE AddrIn;
4139 AddrIn.uAddr = uIova;
4140 AddrIn.cb = cbAccess;
4141 AddrIn.fPerm = fPerm;
4142
4143 IOMMUOPAUX Aux;
4144 Aux.enmOp = enmOp;
4145 Aux.pDte = NULL;
4146 Aux.uDeviceId = uDevId;
4147 Aux.uDomainId = pDteCache->uDomainId;
4148
4149 IOADDRRANGE AddrOut;
4150 rc = iommuAmdLookupIoAddrRange(pDevIns, iommuAmdCacheLookupPage, &AddrIn, &Aux, &AddrOut, NULL /* pcbPages */);
4151 Assert(AddrOut.cb <= cbAccess);
4152 *pGCPhysSpa = AddrOut.uAddr;
4153 *pcbContiguous = AddrOut.cb;
4154 }
4155 else if ((pDteCache->fFlags & (IOMMU_DTE_CACHE_F_PRESENT | IOMMU_DTE_CACHE_F_VALID | IOMMU_DTE_CACHE_F_IO_PERM))
4156 == (IOMMU_DTE_CACHE_F_PRESENT | IOMMU_DTE_CACHE_F_VALID | IOMMU_DTE_CACHE_F_IO_PERM))
4157 {
4158 /* Address translation is disabled, but DTE permissions apply. */
4159 Assert(!(pDteCache->fFlags & IOMMU_DTE_CACHE_F_ADDR_TRANSLATE));
4160 uint8_t const fDtePerm = (pDteCache->fFlags >> IOMMU_DTE_CACHE_F_IO_PERM_SHIFT) & IOMMU_DTE_CACHE_F_IO_PERM_MASK;
4161 if ((fDtePerm & fPerm) == fPerm)
4162 {
4163 *pGCPhysSpa = uIova;
4164 *pcbContiguous = cbAccess;
4165 rc = VINF_SUCCESS;
4166 }
4167 else
4168 IOMMU_IOTLB_LOOKUP_FAILED(VERR_IOMMU_ADDR_ACCESS_DENIED);
4169 }
4170 else if (pDteCache->fFlags & IOMMU_DTE_CACHE_F_PRESENT)
4171 {
4172 /* Forward addresses untranslated, without checking permissions. */
4173 *pGCPhysSpa = uIova;
4174 *pcbContiguous = cbAccess;
4175 rc = VINF_SUCCESS;
4176 }
4177 else
4178 IOMMU_IOTLB_LOOKUP_FAILED(VERR_NOT_FOUND);
4179 }
4180 else
4181 IOMMU_IOTLB_LOOKUP_FAILED(VERR_NOT_FOUND);
4182
4183 IOMMU_UNLOCK_CACHE(pDevIns, pThis);
4184
4185 return rc;
4186
4187#undef IOMMU_IOTLB_LOOKUP_FAILED
4188}
4189#endif /* IOMMU_WITH_IOTLBE_CACHE */
4190
4191
4192/**
4193 * Gets the I/O permission and IOMMU operation type for the given access flags.
4194 *
4195 * @param fFlags The PDM IOMMU flags, PDMIOMMU_MEM_F_XXX.
4196 * @param penmOp Where to store the IOMMU operation.
4197 * @param pfPerm Where to store the IOMMU I/O permission.
4198 * @param pStatRead The stat counter to increment for a read operation.
4199 * @param pStatWrite The stat counter to increment for a write operation.
4200 */
4201DECLINLINE(void) iommuAmdMemAccessGetPermAndOp(uint32_t fFlags, PIOMMUOP penmOp, uint8_t *pfPerm, PSTAMCOUNTER pStatRead,
4202 PSTAMCOUNTER pStatWrite)
4203{
4204 if (fFlags & PDMIOMMU_MEM_F_WRITE)
4205 {
4206 *penmOp = IOMMUOP_MEM_WRITE;
4207 *pfPerm = IOMMU_IO_PERM_WRITE;
4208 STAM_COUNTER_INC(pStatWrite);
4209 }
4210 else
4211 {
4212 Assert(fFlags & PDMIOMMU_MEM_F_READ);
4213 *penmOp = IOMMUOP_MEM_READ;
4214 *pfPerm = IOMMU_IO_PERM_READ;
4215 STAM_COUNTER_INC(pStatRead);
4216 }
4217}
4218
4219
4220/**
4221 * Memory access transaction from a device.
4222 *
4223 * @returns VBox status code.
4224 * @param pDevIns The IOMMU device instance.
4225 * @param uDevId The device ID (bus, device, function).
4226 * @param uIova The I/O virtual address being accessed.
4227 * @param cbAccess The number of bytes being accessed.
4228 * @param fFlags The access flags, see PDMIOMMU_MEM_F_XXX.
4229 * @param pGCPhysSpa Where to store the translated system physical address.
4230 * @param pcbContiguous Where to store the number of contiguous bytes translated
4231 * and permission-checked.
4232 *
4233 * @thread Any.
4234 */
4235static DECLCALLBACK(int) iommuAmdMemAccess(PPDMDEVINS pDevIns, uint16_t uDevId, uint64_t uIova, size_t cbAccess, uint32_t fFlags,
4236 PRTGCPHYS pGCPhysSpa, size_t *pcbContiguous)
4237{
4238 /* Validate. */
4239 AssertPtr(pDevIns);
4240 AssertPtr(pGCPhysSpa);
4241 Assert(cbAccess > 0);
4242 Assert(!(fFlags & ~PDMIOMMU_MEM_F_VALID_MASK));
4243
4244 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
4245 IOMMU_CTRL_T const Ctrl = iommuAmdGetCtrlUnlocked(pThis);
4246 if (Ctrl.n.u1IommuEn)
4247 {
4248 IOMMUOP enmOp;
4249 uint8_t fPerm;
4250 iommuAmdMemAccessGetPermAndOp(fFlags, &enmOp, &fPerm, &pThis->CTX_SUFF_Z(StatMemRead), &pThis->CTX_SUFF_Z(StatMemWrite));
4251 LogFlowFunc(("%s: uDevId=%#x uIova=%#RX64 cb=%zu\n", iommuAmdMemAccessGetPermName(fPerm), uDevId, uIova, cbAccess));
4252
4253 int rc;
4254#ifdef IOMMU_WITH_IOTLBE_CACHE
4255 /* Lookup the IOVA from the cache. */
4256 rc = iommuAmdCacheLookup(pDevIns, uDevId, uIova, cbAccess, fPerm, enmOp, pGCPhysSpa, pcbContiguous);
4257 if (rc == VINF_SUCCESS)
4258 {
4259 /* Entire access was cached and permissions were valid. */
4260 Assert(*pcbContiguous == cbAccess);
4261 Assert(*pGCPhysSpa != NIL_RTGCPHYS);
4262 STAM_COUNTER_INC(&pThis->StatAccessCacheHitFull);
4263 return rc;
4264 }
4265 if (rc != VERR_OUT_OF_RANGE)
4266 { /* likely */ }
4267 else
4268 {
4269 /* Access stopped when translations resulted in non-contiguous memory, let caller resume access. */
4270 Assert(*pcbContiguous > 0 && *pcbContiguous < cbAccess);
4271 STAM_COUNTER_INC(&pThis->StatAccessCacheNonContig);
4272 return VINF_SUCCESS;
4273 }
4274
4275 /*
4276 * Access incomplete as not all pages were in the cache.
4277 * Or permissions were denied for the access (which typically doesn't happen)
4278 * so go through the slower path and raise the required event.
4279 */
4280 AssertMsg(*pcbContiguous < cbAccess, ("Invalid size: cbContiguous=%zu cbAccess=%zu\n", *pcbContiguous, cbAccess));
4281 uIova += *pcbContiguous;
4282 cbAccess -= *pcbContiguous;
4283 /* FYI: We currently would be also be including permission denied as cache misses too.*/
4284 STAM_COUNTER_INC(&pThis->StatAccessCacheMiss);
4285#endif
4286
4287 /* Lookup the IOVA from the device table. */
4288 rc = iommuAmdDteLookup(pDevIns, uDevId, uIova, cbAccess, fPerm, enmOp, pGCPhysSpa, pcbContiguous);
4289 if (RT_SUCCESS(rc))
4290 { /* likely */ }
4291 else
4292 {
4293 Assert(rc != VERR_OUT_OF_RANGE);
4294 LogFunc(("DTE lookup failed! uDevId=%#x uIova=%#RX64 fPerm=%u cbAccess=%zu rc=%#Rrc\n", uDevId, uIova, fPerm,
4295 cbAccess, rc));
4296 }
4297
4298 return rc;
4299 }
4300
4301 /* Addresses are forwarded without translation when the IOMMU is disabled. */
4302 *pGCPhysSpa = uIova;
4303 *pcbContiguous = cbAccess;
4304 return VINF_SUCCESS;
4305}
4306
4307
4308/**
4309 * Memory access bulk (one or more 4K pages) request from a device.
4310 *
4311 * @returns VBox status code.
4312 * @param pDevIns The IOMMU device instance.
4313 * @param uDevId The device ID (bus, device, function).
4314 * @param cIovas The number of addresses being accessed.
4315 * @param pauIovas The I/O virtual addresses for each page being accessed.
4316 * @param fFlags The access flags, see PDMIOMMU_MEM_F_XXX.
4317 * @param paGCPhysSpa Where to store the translated physical addresses.
4318 *
4319 * @thread Any.
4320 */
4321static DECLCALLBACK(int) iommuAmdMemBulkAccess(PPDMDEVINS pDevIns, uint16_t uDevId, size_t cIovas, uint64_t const *pauIovas,
4322 uint32_t fFlags, PRTGCPHYS paGCPhysSpa)
4323{
4324 /* Validate. */
4325 AssertPtr(pDevIns);
4326 Assert(cIovas > 0);
4327 AssertPtr(pauIovas);
4328 AssertPtr(paGCPhysSpa);
4329 Assert(!(fFlags & ~PDMIOMMU_MEM_F_VALID_MASK));
4330
4331 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
4332 IOMMU_CTRL_T const Ctrl = iommuAmdGetCtrlUnlocked(pThis);
4333 if (Ctrl.n.u1IommuEn)
4334 {
4335 IOMMUOP enmOp;
4336 uint8_t fPerm;
4337 iommuAmdMemAccessGetPermAndOp(fFlags, &enmOp, &fPerm, &pThis->CTX_SUFF_Z(StatMemBulkRead),
4338 &pThis->CTX_SUFF_Z(StatMemBulkWrite));
4339 LogFlowFunc(("%s: uDevId=%#x cIovas=%zu\n", iommuAmdMemAccessGetPermName(fPerm), uDevId, cIovas));
4340
4341 /** @todo IOMMU: IOTLB cache lookup. */
4342
4343 /* Lookup each IOVA from the device table. */
4344 for (size_t i = 0; i < cIovas; i++)
4345 {
4346 size_t cbContig;
4347 int rc = iommuAmdDteLookup(pDevIns, uDevId, pauIovas[i], X86_PAGE_SIZE, fPerm, enmOp, &paGCPhysSpa[i], &cbContig);
4348 if (RT_SUCCESS(rc))
4349 { /* likely */ }
4350 else
4351 {
4352 LogFunc(("Failed! uDevId=%#x uIova=%#RX64 fPerm=%u rc=%Rrc\n", uDevId, pauIovas[i], fPerm, rc));
4353 return rc;
4354 }
4355 Assert(cbContig == X86_PAGE_SIZE);
4356 }
4357 }
4358 else
4359 {
4360 /* Addresses are forwarded without translation when the IOMMU is disabled. */
4361 for (size_t i = 0; i < cIovas; i++)
4362 paGCPhysSpa[i] = pauIovas[i];
4363 }
4364
4365 return VINF_SUCCESS;
4366}
4367
4368
4369/**
4370 * Reads an interrupt remapping table entry from guest memory given its DTE.
4371 *
4372 * @returns VBox status code.
4373 * @param pDevIns The IOMMU device instance.
4374 * @param uDevId The device ID (bus, device, function).
4375 * @param pDte The device table entry.
4376 * @param GCPhysIn The source MSI address (used for reporting errors).
4377 * @param uDataIn The source MSI data.
4378 * @param enmOp The IOMMU operation being performed.
4379 * @param pIrte Where to store the interrupt remapping table entry.
4380 *
4381 * @thread Any.
4382 */
4383static int iommuAmdIrteRead(PPDMDEVINS pDevIns, uint16_t uDevId, PCDTE_T pDte, RTGCPHYS GCPhysIn, uint32_t uDataIn,
4384 IOMMUOP enmOp, PIRTE_T pIrte)
4385{
4386 /* Ensure the IRTE length is valid. */
4387 Assert(pDte->n.u4IntrTableLength < IOMMU_DTE_INTR_TAB_LEN_MAX);
4388
4389 RTGCPHYS const GCPhysIntrTable = pDte->au64[2] & IOMMU_DTE_IRTE_ROOT_PTR_MASK;
4390 uint16_t const cbIntrTable = IOMMU_DTE_GET_INTR_TAB_LEN(pDte);
4391 uint16_t const offIrte = IOMMU_GET_IRTE_OFF(uDataIn);
4392 RTGCPHYS const GCPhysIrte = GCPhysIntrTable + offIrte;
4393
4394 /* Ensure the IRTE falls completely within the interrupt table. */
4395 if (offIrte + sizeof(IRTE_T) <= cbIntrTable)
4396 { /* likely */ }
4397 else
4398 {
4399 LogFunc(("IRTE exceeds table length (GCPhysIntrTable=%#RGp cbIntrTable=%u offIrte=%#x uDataIn=%#x) -> IOPF\n",
4400 GCPhysIntrTable, cbIntrTable, offIrte, uDataIn));
4401
4402 EVT_IO_PAGE_FAULT_T EvtIoPageFault;
4403 iommuAmdIoPageFaultEventInit(uDevId, pDte->n.u16DomainId, GCPhysIn, false /* fPresent */, false /* fRsvdNotZero */,
4404 false /* fPermDenied */, enmOp, &EvtIoPageFault);
4405 iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, NULL /* pIrte */, enmOp, &EvtIoPageFault,
4406 kIoPageFaultType_IrteAddrInvalid);
4407 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
4408 }
4409
4410 /* Read the IRTE from memory. */
4411 Assert(!(GCPhysIrte & 3));
4412 int rc = PDMDevHlpPCIPhysRead(pDevIns, GCPhysIrte, pIrte, sizeof(*pIrte));
4413 if (RT_SUCCESS(rc))
4414 return VINF_SUCCESS;
4415
4416 /** @todo The IOMMU spec. does not tell what kind of error is reported in this
4417 * situation. Is it an I/O page fault or a device table hardware error?
4418 * There's no interrupt table hardware error event, but it's unclear what
4419 * we should do here. */
4420 LogFunc(("Failed to read interrupt table entry at %#RGp. rc=%Rrc -> ???\n", GCPhysIrte, rc));
4421 return VERR_IOMMU_IPE_4;
4422}
4423
4424
4425/**
4426 * Remaps the interrupt using the interrupt remapping table.
4427 *
4428 * @returns VBox status code.
4429 * @param pDevIns The IOMMU instance data.
4430 * @param uDevId The device ID (bus, device, function).
4431 * @param pDte The device table entry.
4432 * @param enmOp The IOMMU operation being performed.
4433 * @param pMsiIn The source MSI.
4434 * @param pMsiOut Where to store the remapped MSI.
4435 *
4436 * @thread Any.
4437 */
4438static int iommuAmdIntrRemap(PPDMDEVINS pDevIns, uint16_t uDevId, PCDTE_T pDte, IOMMUOP enmOp, PCMSIMSG pMsiIn,
4439 PMSIMSG pMsiOut)
4440{
4441 Assert(pDte->n.u2IntrCtrl == IOMMU_INTR_CTRL_REMAP);
4442
4443 IRTE_T Irte;
4444 uint32_t const uMsiInData = pMsiIn->Data.u32;
4445 int rc = iommuAmdIrteRead(pDevIns, uDevId, pDte, pMsiIn->Addr.u64, uMsiInData, enmOp, &Irte);
4446 if (RT_SUCCESS(rc))
4447 {
4448 if (Irte.n.u1RemapEnable)
4449 {
4450 if (!Irte.n.u1GuestMode)
4451 {
4452 if (Irte.n.u3IntrType <= VBOX_MSI_DELIVERY_MODE_LOWEST_PRIO)
4453 {
4454 iommuAmdIrteRemapMsi(pMsiIn, pMsiOut, &Irte);
4455#ifdef IOMMU_WITH_IRTE_CACHE
4456 iommuAmdIrteCacheAdd(pDevIns, uDevId, IOMMU_GET_IRTE_OFF(uMsiInData), &Irte);
4457#endif
4458 return VINF_SUCCESS;
4459 }
4460
4461 LogFunc(("Interrupt type (%#x) invalid -> IOPF\n", Irte.n.u3IntrType));
4462 EVT_IO_PAGE_FAULT_T EvtIoPageFault;
4463 iommuAmdIoPageFaultEventInit(uDevId, pDte->n.u16DomainId, pMsiIn->Addr.u64, Irte.n.u1RemapEnable,
4464 true /* fRsvdNotZero */, false /* fPermDenied */, enmOp, &EvtIoPageFault);
4465 iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, &Irte, enmOp, &EvtIoPageFault,
4466 kIoPageFaultType_IrteRsvdIntType);
4467 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
4468 }
4469
4470 LogFunc(("Guest mode not supported -> IOPF\n"));
4471 EVT_IO_PAGE_FAULT_T EvtIoPageFault;
4472 iommuAmdIoPageFaultEventInit(uDevId, pDte->n.u16DomainId, pMsiIn->Addr.u64, Irte.n.u1RemapEnable,
4473 true /* fRsvdNotZero */, false /* fPermDenied */, enmOp, &EvtIoPageFault);
4474 iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, &Irte, enmOp, &EvtIoPageFault, kIoPageFaultType_IrteRsvdNotZero);
4475 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
4476 }
4477
4478 LogFunc(("Remapping disabled -> IOPF\n"));
4479 EVT_IO_PAGE_FAULT_T EvtIoPageFault;
4480 iommuAmdIoPageFaultEventInit(uDevId, pDte->n.u16DomainId, pMsiIn->Addr.u64, Irte.n.u1RemapEnable,
4481 false /* fRsvdNotZero */, false /* fPermDenied */, enmOp, &EvtIoPageFault);
4482 iommuAmdIoPageFaultEventRaiseWithDte(pDevIns, pDte, &Irte, enmOp, &EvtIoPageFault, kIoPageFaultType_IrteRemapEn);
4483 return VERR_IOMMU_ADDR_TRANSLATION_FAILED;
4484 }
4485
4486 return rc;
4487}
4488
4489
4490/**
4491 * Looks up an MSI interrupt from the interrupt remapping table.
4492 *
4493 * @returns VBox status code.
4494 * @param pDevIns The IOMMU instance data.
4495 * @param uDevId The device ID (bus, device, function).
4496 * @param enmOp The IOMMU operation being performed.
4497 * @param pMsiIn The source MSI.
4498 * @param pMsiOut Where to store the remapped MSI.
4499 *
4500 * @thread Any.
4501 */
4502static int iommuAmdIntrTableLookup(PPDMDEVINS pDevIns, uint16_t uDevId, IOMMUOP enmOp, PCMSIMSG pMsiIn, PMSIMSG pMsiOut)
4503{
4504 LogFlowFunc(("uDevId=%#x (%#x:%#x:%#x) enmOp=%u\n", uDevId,
4505 ((uDevId >> VBOX_PCI_BUS_SHIFT) & VBOX_PCI_BUS_MASK),
4506 ((uDevId >> VBOX_PCI_DEVFN_DEV_SHIFT) & VBOX_PCI_DEVFN_DEV_MASK), (uDevId & VBOX_PCI_DEVFN_FUN_MASK), enmOp));
4507
4508 /* Read the device table entry from memory. */
4509 DTE_T Dte;
4510 int rc = iommuAmdDteRead(pDevIns, uDevId, enmOp, &Dte);
4511 if (RT_SUCCESS(rc))
4512 {
4513#ifdef IOMMU_WITH_IRTE_CACHE
4514 iommuAmdDteCacheAdd(pDevIns, uDevId, &Dte, 0 /* fFlags */);
4515#endif
4516 /* If the DTE is not valid, all interrupts are forwarded without remapping. */
4517 if (Dte.n.u1IntrMapValid)
4518 {
4519 /* Validate bits 255:128 of the device table entry when DTE.IV is 1. */
4520 uint64_t const fRsvd0 = Dte.au64[2] & ~IOMMU_DTE_QWORD_2_VALID_MASK;
4521 uint64_t const fRsvd1 = Dte.au64[3] & ~IOMMU_DTE_QWORD_3_VALID_MASK;
4522 if (RT_LIKELY(!fRsvd0 && !fRsvd1))
4523 { /* likely */ }
4524 else
4525 {
4526 LogFunc(("Invalid reserved bits in DTE (u64[2]=%#RX64 u64[3]=%#RX64) -> Illegal DTE\n", fRsvd0, fRsvd1));
4527 EVT_ILLEGAL_DTE_T Event;
4528 iommuAmdIllegalDteEventInit(uDevId, pMsiIn->Addr.u64, true /* fRsvdNotZero */, enmOp, &Event);
4529 iommuAmdIllegalDteEventRaise(pDevIns, enmOp, &Event, kIllegalDteType_RsvdNotZero);
4530 return VERR_IOMMU_INTR_REMAP_FAILED;
4531 }
4532
4533 /*
4534 * LINT0/LINT1 pins cannot be driven by PCI(e) devices. Perhaps for a Southbridge
4535 * that's connected through HyperTransport it might be possible; but for us, it
4536 * doesn't seem we need to specially handle these pins.
4537 */
4538
4539 /*
4540 * Validate the MSI source address.
4541 *
4542 * 64-bit MSIs are supported by the PCI and AMD IOMMU spec. However as far as the
4543 * CPU is concerned, the MSI region is fixed and we must ensure no other device
4544 * claims the region as I/O space.
4545 *
4546 * See PCI spec. 6.1.4. "Message Signaled Interrupt (MSI) Support".
4547 * See AMD IOMMU spec. 2.8 "IOMMU Interrupt Support".
4548 * See Intel spec. 10.11.1 "Message Address Register Format".
4549 */
4550 if ((pMsiIn->Addr.u64 & VBOX_MSI_ADDR_ADDR_MASK) == VBOX_MSI_ADDR_BASE)
4551 {
4552 /*
4553 * The IOMMU remaps fixed and arbitrated interrupts using the IRTE.
4554 * See AMD IOMMU spec. "2.2.5.1 Interrupt Remapping Tables, Guest Virtual APIC Not Enabled".
4555 */
4556 uint8_t const u8DeliveryMode = pMsiIn->Data.n.u3DeliveryMode;
4557 bool fPassThru = false;
4558 switch (u8DeliveryMode)
4559 {
4560 case VBOX_MSI_DELIVERY_MODE_FIXED:
4561 case VBOX_MSI_DELIVERY_MODE_LOWEST_PRIO:
4562 {
4563 uint8_t const uIntrCtrl = Dte.n.u2IntrCtrl;
4564 if (uIntrCtrl == IOMMU_INTR_CTRL_REMAP)
4565 {
4566 /* Validate the encoded interrupt table length when IntCtl specifies remapping. */
4567 uint8_t const uIntrTabLen = Dte.n.u4IntrTableLength;
4568 if (uIntrTabLen < IOMMU_DTE_INTR_TAB_LEN_MAX)
4569 {
4570 /*
4571 * We don't support guest interrupt remapping yet. When we do, we'll need to
4572 * check Ctrl.u1GstVirtApicEn and use the guest Virtual APIC Table Root Pointer
4573 * in the DTE rather than the Interrupt Root Table Pointer. Since the caller
4574 * already reads the control register, add that as a parameter when we eventually
4575 * support guest interrupt remapping. For now, just assert.
4576 */
4577 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
4578 Assert(!pThis->ExtFeat.n.u1GstVirtApicSup);
4579 NOREF(pThis);
4580
4581 return iommuAmdIntrRemap(pDevIns, uDevId, &Dte, enmOp, pMsiIn, pMsiOut);
4582 }
4583
4584 LogFunc(("Invalid interrupt table length %#x -> Illegal DTE\n", uIntrTabLen));
4585 EVT_ILLEGAL_DTE_T Event;
4586 iommuAmdIllegalDteEventInit(uDevId, pMsiIn->Addr.u64, false /* fRsvdNotZero */, enmOp, &Event);
4587 iommuAmdIllegalDteEventRaise(pDevIns, enmOp, &Event, kIllegalDteType_RsvdIntTabLen);
4588 return VERR_IOMMU_INTR_REMAP_FAILED;
4589 }
4590
4591 if (uIntrCtrl == IOMMU_INTR_CTRL_FWD_UNMAPPED)
4592 {
4593 fPassThru = true;
4594 break;
4595 }
4596
4597 if (uIntrCtrl == IOMMU_INTR_CTRL_TARGET_ABORT)
4598 {
4599 LogFunc(("IntCtl=0: Remapping disallowed for fixed/arbitrated interrupt (%#x) -> Target abort\n",
4600 pMsiIn->Data.n.u8Vector));
4601 iommuAmdSetPciTargetAbort(pDevIns);
4602 return VERR_IOMMU_INTR_REMAP_DENIED;
4603 }
4604
4605 Assert(uIntrCtrl == IOMMU_INTR_CTRL_RSVD); /* Paranoia. */
4606 LogFunc(("IntCtl mode invalid %#x -> Illegal DTE\n", uIntrCtrl));
4607 EVT_ILLEGAL_DTE_T Event;
4608 iommuAmdIllegalDteEventInit(uDevId, pMsiIn->Addr.u64, true /* fRsvdNotZero */, enmOp, &Event);
4609 iommuAmdIllegalDteEventRaise(pDevIns, enmOp, &Event, kIllegalDteType_RsvdIntCtl);
4610 return VERR_IOMMU_INTR_REMAP_FAILED;
4611 }
4612
4613 /* SMIs are passed through unmapped. We don't implement SMI filters. */
4614 case VBOX_MSI_DELIVERY_MODE_SMI: fPassThru = true; break;
4615 case VBOX_MSI_DELIVERY_MODE_NMI: fPassThru = Dte.n.u1NmiPassthru; break;
4616 case VBOX_MSI_DELIVERY_MODE_INIT: fPassThru = Dte.n.u1InitPassthru; break;
4617 case VBOX_MSI_DELIVERY_MODE_EXT_INT: fPassThru = Dte.n.u1ExtIntPassthru; break;
4618 default:
4619 {
4620 LogFunc(("MSI data delivery mode invalid %#x -> Target abort\n", u8DeliveryMode));
4621 iommuAmdSetPciTargetAbort(pDevIns);
4622 return VERR_IOMMU_INTR_REMAP_FAILED;
4623 }
4624 }
4625
4626 /*
4627 * For those other than fixed and arbitrated interrupts, destination mode must be 0 (physical).
4628 * See AMD IOMMU spec. The note below Table 19: "IOMMU Controls and Actions for Upstream Interrupts".
4629 */
4630 if ( u8DeliveryMode <= VBOX_MSI_DELIVERY_MODE_LOWEST_PRIO
4631 || !pMsiIn->Addr.n.u1DestMode)
4632 {
4633 if (fPassThru)
4634 {
4635 *pMsiOut = *pMsiIn;
4636 return VINF_SUCCESS;
4637 }
4638 LogFunc(("Remapping/passthru disallowed for interrupt %#x -> Target abort\n", pMsiIn->Data.n.u8Vector));
4639 }
4640 else
4641 LogFunc(("Logical destination mode invalid for delivery mode %#x\n -> Target abort\n", u8DeliveryMode));
4642
4643 iommuAmdSetPciTargetAbort(pDevIns);
4644 return VERR_IOMMU_INTR_REMAP_DENIED;
4645 }
4646 else
4647 {
4648 /** @todo should be cause a PCI target abort here? */
4649 LogFunc(("MSI address region invalid %#RX64\n", pMsiIn->Addr.u64));
4650 return VERR_IOMMU_INTR_REMAP_FAILED;
4651 }
4652 }
4653 else
4654 {
4655 LogFlowFunc(("DTE interrupt map not valid\n"));
4656 *pMsiOut = *pMsiIn;
4657 return VINF_SUCCESS;
4658 }
4659 }
4660
4661 LogFunc(("Failed to read device table entry. uDevId=%#x rc=%Rrc\n", uDevId, rc));
4662 return VERR_IOMMU_INTR_REMAP_FAILED;
4663}
4664
4665
4666/**
4667 * Interrupt remap request from a device.
4668 *
4669 * @returns VBox status code.
4670 * @param pDevIns The IOMMU device instance.
4671 * @param uDevId The device ID (bus, device, function).
4672 * @param pMsiIn The source MSI.
4673 * @param pMsiOut Where to store the remapped MSI.
4674 */
4675static DECLCALLBACK(int) iommuAmdMsiRemap(PPDMDEVINS pDevIns, uint16_t uDevId, PCMSIMSG pMsiIn, PMSIMSG pMsiOut)
4676{
4677 /* Validate. */
4678 Assert(pDevIns);
4679 Assert(pMsiIn);
4680 Assert(pMsiOut);
4681
4682 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
4683
4684 /* Interrupts are forwarded with remapping when the IOMMU is disabled. */
4685 IOMMU_CTRL_T const Ctrl = iommuAmdGetCtrlUnlocked(pThis);
4686 if (Ctrl.n.u1IommuEn)
4687 {
4688 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMsiRemap));
4689
4690 int rc;
4691#ifdef IOMMU_WITH_IRTE_CACHE
4692 STAM_PROFILE_ADV_START(&pThis->StatProfIrteCacheLookup, a);
4693 rc = iommuAmdIrteCacheLookup(pDevIns, uDevId, IOMMUOP_INTR_REQ, pMsiIn, pMsiOut);
4694 STAM_PROFILE_ADV_STOP(&pThis->StatProfIrteCacheLookup, a);
4695 if (RT_SUCCESS(rc))
4696 {
4697 STAM_COUNTER_INC(&pThis->StatIntrCacheHit);
4698 return VINF_SUCCESS;
4699 }
4700 STAM_COUNTER_INC(&pThis->StatIntrCacheMiss);
4701#endif
4702
4703 STAM_PROFILE_ADV_START(&pThis->StatProfIrteLookup, a);
4704 rc = iommuAmdIntrTableLookup(pDevIns, uDevId, IOMMUOP_INTR_REQ, pMsiIn, pMsiOut);
4705 STAM_PROFILE_ADV_STOP(&pThis->StatProfIrteLookup, a);
4706 return rc;
4707 }
4708
4709 *pMsiOut = *pMsiIn;
4710 return VINF_SUCCESS;
4711}
4712
4713
4714/**
4715 * @callback_method_impl{FNIOMMMIONEWWRITE}
4716 */
4717static DECLCALLBACK(VBOXSTRICTRC) iommuAmdMmioWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
4718{
4719 NOREF(pvUser);
4720 Assert(cb == 4 || cb == 8);
4721 Assert(!(off & (cb - 1)));
4722
4723 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
4724 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMmioWrite)); NOREF(pThis);
4725
4726 uint64_t const uValue = cb == 8 ? *(uint64_t const *)pv : *(uint32_t const *)pv;
4727 return iommuAmdRegisterWrite(pDevIns, off, cb, uValue);
4728}
4729
4730
4731/**
4732 * @callback_method_impl{FNIOMMMIONEWREAD}
4733 */
4734static DECLCALLBACK(VBOXSTRICTRC) iommuAmdMmioRead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
4735{
4736 NOREF(pvUser);
4737 Assert(cb == 4 || cb == 8);
4738 Assert(!(off & (cb - 1)));
4739
4740 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
4741 STAM_COUNTER_INC(&pThis->CTX_SUFF_Z(StatMmioRead)); NOREF(pThis);
4742
4743 uint64_t uResult;
4744 VBOXSTRICTRC rcStrict = iommuAmdRegisterRead(pDevIns, off, &uResult);
4745 if (cb == 8)
4746 *(uint64_t *)pv = uResult;
4747 else
4748 *(uint32_t *)pv = (uint32_t)uResult;
4749
4750 return rcStrict;
4751}
4752
4753
4754#ifdef IN_RING3
4755/**
4756 * Processes an IOMMU command.
4757 *
4758 * @returns VBox status code.
4759 * @param pDevIns The IOMMU device instance.
4760 * @param pCmd The command to process.
4761 * @param GCPhysCmd The system physical address of the command.
4762 * @param pEvtError Where to store the error event in case of failures.
4763 *
4764 * @thread Command thread.
4765 */
4766static int iommuAmdR3CmdProcess(PPDMDEVINS pDevIns, PCCMD_GENERIC_T pCmd, RTGCPHYS GCPhysCmd, PEVT_GENERIC_T pEvtError)
4767{
4768 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
4769 PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
4770
4771 STAM_COUNTER_INC(&pThis->StatCmd);
4772
4773 uint8_t const bCmd = pCmd->n.u4Opcode;
4774 switch (bCmd)
4775 {
4776 case IOMMU_CMD_COMPLETION_WAIT:
4777 {
4778 STAM_COUNTER_INC(&pThis->StatCmdCompWait);
4779
4780 PCCMD_COMWAIT_T pCmdComWait = (PCCMD_COMWAIT_T)pCmd;
4781 AssertCompile(sizeof(*pCmdComWait) == sizeof(*pCmd));
4782
4783 /* Validate reserved bits in the command. */
4784 if (!(pCmdComWait->au64[0] & ~IOMMU_CMD_COM_WAIT_QWORD_0_VALID_MASK))
4785 {
4786 /* If Completion Store is requested, write the StoreData to the specified address. */
4787 if (pCmdComWait->n.u1Store)
4788 {
4789 RTGCPHYS const GCPhysStore = RT_MAKE_U64(pCmdComWait->n.u29StoreAddrLo << 3, pCmdComWait->n.u20StoreAddrHi);
4790 uint64_t const u64Data = pCmdComWait->n.u64StoreData;
4791 int rc = PDMDevHlpPCIPhysWrite(pDevIns, GCPhysStore, &u64Data, sizeof(u64Data));
4792 if (RT_FAILURE(rc))
4793 {
4794 LogFunc(("Cmd(%#x): Failed to write StoreData (%#RX64) to %#RGp, rc=%Rrc\n", bCmd, u64Data,
4795 GCPhysStore, rc));
4796 iommuAmdCmdHwErrorEventInit(GCPhysStore, (PEVT_CMD_HW_ERR_T)pEvtError);
4797 return VERR_IOMMU_CMD_HW_ERROR;
4798 }
4799 }
4800
4801 /* If the command requests an interrupt and completion wait interrupts are enabled, raise it. */
4802 if (pCmdComWait->n.u1Interrupt)
4803 {
4804 IOMMU_LOCK(pDevIns, pThisR3);
4805 ASMAtomicOrU64(&pThis->Status.u64, IOMMU_STATUS_COMPLETION_WAIT_INTR);
4806 bool const fRaiseInt = pThis->Ctrl.n.u1CompWaitIntrEn;
4807 IOMMU_UNLOCK(pDevIns, pThisR3);
4808
4809 if (fRaiseInt)
4810 iommuAmdMsiInterruptRaise(pDevIns);
4811 }
4812 return VINF_SUCCESS;
4813 }
4814 iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
4815 return VERR_IOMMU_CMD_INVALID_FORMAT;
4816 }
4817
4818 case IOMMU_CMD_INV_DEV_TAB_ENTRY:
4819 {
4820 STAM_COUNTER_INC(&pThis->StatCmdInvDte);
4821 PCCMD_INV_DTE_T pCmdInvDte = (PCCMD_INV_DTE_T)pCmd;
4822 AssertCompile(sizeof(*pCmdInvDte) == sizeof(*pCmd));
4823
4824 /* Validate reserved bits in the command. */
4825 if ( !(pCmdInvDte->au64[0] & ~IOMMU_CMD_INV_DTE_QWORD_0_VALID_MASK)
4826 && !(pCmdInvDte->au64[1] & ~IOMMU_CMD_INV_DTE_QWORD_1_VALID_MASK))
4827 {
4828#ifdef IOMMU_WITH_DTE_CACHE
4829 iommuAmdDteCacheRemove(pDevIns, pCmdInvDte->n.u16DevId);
4830#endif
4831 return VINF_SUCCESS;
4832 }
4833 iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
4834 return VERR_IOMMU_CMD_INVALID_FORMAT;
4835 }
4836
4837 case IOMMU_CMD_INV_IOMMU_PAGES:
4838 {
4839 STAM_COUNTER_INC(&pThis->StatCmdInvIommuPages);
4840 PCCMD_INV_IOMMU_PAGES_T pCmdInvPages = (PCCMD_INV_IOMMU_PAGES_T)pCmd;
4841 AssertCompile(sizeof(*pCmdInvPages) == sizeof(*pCmd));
4842
4843 /* Validate reserved bits in the command. */
4844 if ( !(pCmdInvPages->au64[0] & ~IOMMU_CMD_INV_IOMMU_PAGES_QWORD_0_VALID_MASK)
4845 && !(pCmdInvPages->au64[1] & ~IOMMU_CMD_INV_IOMMU_PAGES_QWORD_1_VALID_MASK))
4846 {
4847#ifdef IOMMU_WITH_IOTLBE_CACHE
4848 uint64_t const uIova = RT_MAKE_U64(pCmdInvPages->n.u20AddrLo << X86_PAGE_4K_SHIFT, pCmdInvPages->n.u32AddrHi);
4849 uint16_t const uDomainId = pCmdInvPages->n.u16DomainId;
4850 bool const fFlushPde = pCmdInvPages->n.u1PageDirEntries;
4851 uint8_t cShift;
4852 if (!pCmdInvPages->n.u1Size)
4853 cShift = X86_PAGE_4K_SHIFT;
4854 else
4855 {
4856 /* Find the first clear bit starting from bit 12 to 64 of the I/O virtual address. */
4857 unsigned const uFirstZeroBit = ASMBitLastSetU64(~(uIova >> X86_PAGE_4K_SHIFT));
4858 cShift = X86_PAGE_4K_SHIFT + uFirstZeroBit;
4859
4860 /*
4861 * For the address 0x7ffffffffffff000, cShift would be 76 (12+64) and the code below
4862 * would do the right thing by clearing the entire cache for the specified domain ID.
4863 *
4864 * However, for the address 0xfffffffffffff000, cShift would be computed as 12.
4865 * IOMMU behavior is undefined in this case, so it's safe to invalidate just one page.
4866 * A debug-time assert is in place here to let us know if any software tries this.
4867 *
4868 * See AMD IOMMU spec. 2.4.3 "INVALIDATE_IOMMU_PAGES".
4869 * See AMD IOMMU spec. Table 14: "Example Page Size Encodings".
4870 */
4871 Assert(uIova != UINT64_C(0xfffffffffffff000));
4872 }
4873
4874 /*
4875 * Validate invalidation size.
4876 * See AMD IOMMU spec. 2.2.3 "I/O Page Tables for Host Translations".
4877 */
4878 if ( cShift == 12 /* 4K */ || cShift == 13 /* 8K */
4879 || cShift == 14 /* 16K */ || cShift == 20 /* 1M */
4880 || cShift == 22 /* 4M */ || cShift == 32 /* 4G */)
4881 {
4882 /* Remove the range of I/O virtual addresses requesting to be invalidated. */
4883 size_t const cbAccess = RT_BIT_64(cShift);
4884 iommuAmdIotlbRemoveRange(pDevIns, uDomainId, uIova, cbAccess);
4885 }
4886 else
4887 {
4888 /*
4889 * The guest provided size is invalid or exceeds the largest, meaningful page size.
4890 * In such situations we must remove all ranges for the specified domain ID.
4891 */
4892 iommuAmdIotlbRemoveDomainId(pDevIns, uDomainId);
4893 }
4894#endif
4895 return VINF_SUCCESS;
4896 }
4897 iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
4898 return VERR_IOMMU_CMD_INVALID_FORMAT;
4899 }
4900
4901 case IOMMU_CMD_INV_IOTLB_PAGES:
4902 {
4903 STAM_COUNTER_INC(&pThis->StatCmdInvIotlbPages);
4904
4905 uint32_t const uCapHdr = PDMPciDevGetDWord(pDevIns->apPciDevs[0], IOMMU_PCI_OFF_CAP_HDR);
4906 if (RT_BF_GET(uCapHdr, IOMMU_BF_CAPHDR_IOTLB_SUP))
4907 {
4908 /** @todo IOMMU: Implement remote IOTLB invalidation. */
4909 return VERR_NOT_IMPLEMENTED;
4910 }
4911 iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
4912 return VERR_IOMMU_CMD_NOT_SUPPORTED;
4913 }
4914
4915 case IOMMU_CMD_INV_INTR_TABLE:
4916 {
4917 STAM_COUNTER_INC(&pThis->StatCmdInvIntrTable);
4918
4919 PCCMD_INV_INTR_TABLE_T pCmdInvIntrTable = (PCCMD_INV_INTR_TABLE_T)pCmd;
4920 AssertCompile(sizeof(*pCmdInvIntrTable) == sizeof(*pCmd));
4921
4922 /* Validate reserved bits in the command. */
4923 if ( !(pCmdInvIntrTable->au64[0] & ~IOMMU_CMD_INV_INTR_TABLE_QWORD_0_VALID_MASK)
4924 && !(pCmdInvIntrTable->au64[1] & ~IOMMU_CMD_INV_INTR_TABLE_QWORD_1_VALID_MASK))
4925 {
4926#ifdef IOMMU_WITH_IRTE_CACHE
4927 iommuAmdIrteCacheRemove(pDevIns, pCmdInvIntrTable->u.u16DevId);
4928#endif
4929 return VINF_SUCCESS;
4930 }
4931 iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
4932 return VERR_IOMMU_CMD_INVALID_FORMAT;
4933 }
4934
4935 case IOMMU_CMD_PREFETCH_IOMMU_PAGES:
4936 {
4937 /* Linux doesn't use prefetching of IOMMU pages, so we don't bother for now. */
4938 STAM_COUNTER_INC(&pThis->StatCmdPrefIommuPages);
4939 Assert(!pThis->ExtFeat.n.u1PrefetchSup);
4940 iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
4941 return VERR_IOMMU_CMD_NOT_SUPPORTED;
4942 }
4943
4944 case IOMMU_CMD_COMPLETE_PPR_REQ:
4945 {
4946 STAM_COUNTER_INC(&pThis->StatCmdCompletePprReq);
4947
4948 /* We don't support PPR requests yet. */
4949 Assert(!pThis->ExtFeat.n.u1PprSup);
4950 iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
4951 return VERR_IOMMU_CMD_NOT_SUPPORTED;
4952 }
4953
4954 case IOMMU_CMD_INV_IOMMU_ALL:
4955 {
4956 STAM_COUNTER_INC(&pThis->StatCmdInvIommuAll);
4957 if (pThis->ExtFeat.n.u1InvAllSup)
4958 {
4959 PCCMD_INV_IOMMU_ALL_T pCmdInvAll = (PCCMD_INV_IOMMU_ALL_T)pCmd;
4960 AssertCompile(sizeof(*pCmdInvAll) == sizeof(*pCmd));
4961
4962 /* Validate reserved bits in the command. */
4963 if ( !(pCmdInvAll->au64[0] & ~IOMMU_CMD_INV_IOMMU_ALL_QWORD_0_VALID_MASK)
4964 && !(pCmdInvAll->au64[1] & ~IOMMU_CMD_INV_IOMMU_ALL_QWORD_1_VALID_MASK))
4965 {
4966#ifdef IOMMU_WITH_DTE_CACHE
4967 iommuAmdDteCacheRemoveAll(pDevIns);
4968#endif
4969#ifdef IOMMU_WITH_IOTLBE_CACHE
4970 iommuAmdIotlbRemoveAll(pDevIns);
4971#endif
4972 return VINF_SUCCESS;
4973 }
4974 iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
4975 return VERR_IOMMU_CMD_INVALID_FORMAT;
4976 }
4977 iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
4978 return VERR_IOMMU_CMD_NOT_SUPPORTED;
4979 }
4980 }
4981
4982 STAM_COUNTER_DEC(&pThis->StatCmd);
4983 LogFunc(("Cmd(%#x): Unrecognized\n", bCmd));
4984 iommuAmdIllegalCmdEventInit(GCPhysCmd, (PEVT_ILLEGAL_CMD_ERR_T)pEvtError);
4985 return VERR_IOMMU_CMD_NOT_SUPPORTED;
4986}
4987
4988
4989/**
4990 * The IOMMU command thread.
4991 *
4992 * @returns VBox status code.
4993 * @param pDevIns The IOMMU device instance.
4994 * @param pThread The command thread.
4995 */
4996static DECLCALLBACK(int) iommuAmdR3CmdThread(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
4997{
4998 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
4999 PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
5000
5001 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
5002 return VINF_SUCCESS;
5003
5004 /*
5005 * Pre-allocate the maximum command buffer size supported by the IOMMU.
5006 * This avoid trashing the heap as well as not wasting time allocating
5007 * and freeing buffers while processing commands.
5008 */
5009 size_t const cbMaxCmdBuf = sizeof(CMD_GENERIC_T) * iommuAmdGetBufMaxEntries(15);
5010 void *pvCmds = RTMemAllocZ(cbMaxCmdBuf);
5011 AssertPtrReturn(pvCmds, VERR_NO_MEMORY);
5012
5013 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
5014 {
5015 /*
5016 * Sleep perpetually until we are woken up to process commands.
5017 */
5018 bool const fSignaled = ASMAtomicXchgBool(&pThis->fCmdThreadSignaled, false);
5019 if (!fSignaled)
5020 {
5021 int rc = PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pThis->hEvtCmdThread, RT_INDEFINITE_WAIT);
5022 AssertLogRelMsgReturn(RT_SUCCESS(rc) || rc == VERR_INTERRUPTED, ("%Rrc\n", rc), rc);
5023 if (RT_UNLIKELY(pThread->enmState != PDMTHREADSTATE_RUNNING))
5024 break;
5025 Log4Func(("Woken up with rc=%Rrc\n", rc));
5026 ASMAtomicWriteBool(&pThis->fCmdThreadSignaled, false);
5027 }
5028
5029 /*
5030 * Fetch and process IOMMU commands.
5031 */
5032 /** @todo r=ramshankar: We currently copy all commands from guest memory into a
5033 * temporary host buffer before processing them as a batch. If we want to
5034 * save on host memory a bit, we could (once PGM has the necessary APIs)
5035 * lock the page mappings page mappings and access them directly. */
5036 IOMMU_LOCK(pDevIns, pThisR3);
5037
5038 if (pThis->Status.n.u1CmdBufRunning)
5039 {
5040 /* Get the offsets we need to read commands from memory (circular buffer offset). */
5041 uint32_t const cbCmdBuf = iommuAmdGetTotalBufLength(pThis->CmdBufBaseAddr.n.u4Len);
5042 uint32_t const offTail = pThis->CmdBufTailPtr.n.off;
5043 uint32_t offHead = pThis->CmdBufHeadPtr.n.off;
5044
5045 /* Validate. */
5046 Assert(!(offHead & ~IOMMU_CMD_BUF_HEAD_PTR_VALID_MASK));
5047 Assert(offHead < cbCmdBuf);
5048 Assert(cbCmdBuf <= cbMaxCmdBuf);
5049
5050 if (offHead != offTail)
5051 {
5052 /* Read the entire command buffer from memory (avoids multiple PGM calls). */
5053 RTGCPHYS const GCPhysCmdBufBase = pThis->CmdBufBaseAddr.n.u40Base << X86_PAGE_4K_SHIFT;
5054
5055 IOMMU_UNLOCK(pDevIns, pThisR3);
5056 int rc = PDMDevHlpPCIPhysRead(pDevIns, GCPhysCmdBufBase, pvCmds, cbCmdBuf);
5057 IOMMU_LOCK(pDevIns, pThisR3);
5058
5059 if (RT_SUCCESS(rc))
5060 {
5061 /* Indicate to software we've fetched all commands from the buffer. */
5062 pThis->CmdBufHeadPtr.n.off = offTail;
5063
5064 /* Allow IOMMU to do other work while we process commands. */
5065 IOMMU_UNLOCK(pDevIns, pThisR3);
5066
5067 /* Process the fetched commands. */
5068 EVT_GENERIC_T EvtError;
5069 do
5070 {
5071 PCCMD_GENERIC_T pCmd = (PCCMD_GENERIC_T)((uintptr_t)pvCmds + offHead);
5072 rc = iommuAmdR3CmdProcess(pDevIns, pCmd, GCPhysCmdBufBase + offHead, &EvtError);
5073 if (RT_FAILURE(rc))
5074 {
5075 if ( rc == VERR_IOMMU_CMD_NOT_SUPPORTED
5076 || rc == VERR_IOMMU_CMD_INVALID_FORMAT)
5077 {
5078 Assert(EvtError.n.u4EvtCode == IOMMU_EVT_ILLEGAL_CMD_ERROR);
5079 iommuAmdIllegalCmdEventRaise(pDevIns, (PCEVT_ILLEGAL_CMD_ERR_T)&EvtError);
5080 }
5081 else if (rc == VERR_IOMMU_CMD_HW_ERROR)
5082 {
5083 Assert(EvtError.n.u4EvtCode == IOMMU_EVT_COMMAND_HW_ERROR);
5084 LogFunc(("Raising command hardware error. Cmd=%#x -> COMMAND_HW_ERROR\n", pCmd->n.u4Opcode));
5085 iommuAmdCmdHwErrorEventRaise(pDevIns, (PCEVT_CMD_HW_ERR_T)&EvtError);
5086 }
5087 break;
5088 }
5089
5090 /* Move to the next command in the circular buffer. */
5091 offHead = (offHead + sizeof(CMD_GENERIC_T)) % cbCmdBuf;
5092 } while (offHead != offTail);
5093 }
5094 else
5095 {
5096 LogFunc(("Failed to read command at %#RGp. rc=%Rrc -> COMMAND_HW_ERROR\n", GCPhysCmdBufBase, rc));
5097 EVT_CMD_HW_ERR_T EvtCmdHwErr;
5098 iommuAmdCmdHwErrorEventInit(GCPhysCmdBufBase, &EvtCmdHwErr);
5099 iommuAmdCmdHwErrorEventRaise(pDevIns, &EvtCmdHwErr);
5100
5101 IOMMU_UNLOCK(pDevIns, pThisR3);
5102 }
5103 }
5104 else
5105 IOMMU_UNLOCK(pDevIns, pThisR3);
5106 }
5107 else
5108 IOMMU_UNLOCK(pDevIns, pThisR3);
5109 }
5110
5111 RTMemFree(pvCmds);
5112 LogFlowFunc(("Command thread terminating\n"));
5113 return VINF_SUCCESS;
5114}
5115
5116
5117/**
5118 * Wakes up the command thread so it can respond to a state change.
5119 *
5120 * @returns VBox status code.
5121 * @param pDevIns The IOMMU device instance.
5122 * @param pThread The command thread.
5123 */
5124static DECLCALLBACK(int) iommuAmdR3CmdThreadWakeUp(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
5125{
5126 RT_NOREF(pThread);
5127 LogFlowFunc(("\n"));
5128 PCIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
5129 return PDMDevHlpSUPSemEventSignal(pDevIns, pThis->hEvtCmdThread);
5130}
5131
5132
5133/**
5134 * @callback_method_impl{FNPCICONFIGREAD}
5135 */
5136static DECLCALLBACK(VBOXSTRICTRC) iommuAmdR3PciConfigRead(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t uAddress,
5137 unsigned cb, uint32_t *pu32Value)
5138{
5139 /** @todo IOMMU: PCI config read stat counter. */
5140 VBOXSTRICTRC rcStrict = PDMDevHlpPCIConfigRead(pDevIns, pPciDev, uAddress, cb, pu32Value);
5141 Log3Func(("uAddress=%#x (cb=%u) -> %#x. rc=%Rrc\n", uAddress, cb, *pu32Value, VBOXSTRICTRC_VAL(rcStrict)));
5142 return rcStrict;
5143}
5144
5145
5146/**
5147 * @callback_method_impl{FNPCICONFIGWRITE}
5148 */
5149static DECLCALLBACK(VBOXSTRICTRC) iommuAmdR3PciConfigWrite(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t uAddress,
5150 unsigned cb, uint32_t u32Value)
5151{
5152 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
5153
5154 /*
5155 * Discard writes to read-only registers that are specific to the IOMMU.
5156 * Other common PCI registers are handled by the generic code, see devpciR3IsConfigByteWritable().
5157 * See PCI spec. 6.1. "Configuration Space Organization".
5158 */
5159 switch (uAddress)
5160 {
5161 case IOMMU_PCI_OFF_CAP_HDR: /* All bits are read-only. */
5162 case IOMMU_PCI_OFF_RANGE_REG: /* We don't have any devices integrated with the IOMMU. */
5163 case IOMMU_PCI_OFF_MISCINFO_REG_0: /* We don't support MSI-X. */
5164 case IOMMU_PCI_OFF_MISCINFO_REG_1: /* We don't support guest-address translation. */
5165 {
5166 LogFunc(("PCI config write (%#RX32) to read-only register %#x -> Ignored\n", u32Value, uAddress));
5167 return VINF_SUCCESS;
5168 }
5169 }
5170
5171 PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
5172 IOMMU_LOCK(pDevIns, pThisR3);
5173
5174 VBOXSTRICTRC rcStrict = VERR_IOMMU_IPE_3;
5175 switch (uAddress)
5176 {
5177 case IOMMU_PCI_OFF_BASE_ADDR_REG_LO:
5178 {
5179 if (pThis->IommuBar.n.u1Enable)
5180 {
5181 rcStrict = VINF_SUCCESS;
5182 LogFunc(("Writing Base Address (Lo) when it's already enabled -> Ignored\n"));
5183 break;
5184 }
5185
5186 pThis->IommuBar.au32[0] = u32Value & IOMMU_BAR_VALID_MASK;
5187 if (pThis->IommuBar.n.u1Enable)
5188 {
5189 Assert(pThis->hMmio != NIL_IOMMMIOHANDLE); /* Paranoia. Ensure we have a valid IOM MMIO handle. */
5190 Assert(!pThis->ExtFeat.n.u1PerfCounterSup); /* Base is 16K aligned when performance counters aren't supported. */
5191 RTGCPHYS const GCPhysMmioBase = RT_MAKE_U64(pThis->IommuBar.au32[0] & 0xffffc000, pThis->IommuBar.au32[1]);
5192 RTGCPHYS const GCPhysMmioBasePrev = PDMDevHlpMmioGetMappingAddress(pDevIns, pThis->hMmio);
5193
5194 /* If the MMIO region is already mapped at the specified address, we're done. */
5195 Assert(GCPhysMmioBase != NIL_RTGCPHYS);
5196 if (GCPhysMmioBasePrev == GCPhysMmioBase)
5197 {
5198 rcStrict = VINF_SUCCESS;
5199 break;
5200 }
5201
5202 /* Unmap the previous MMIO region (which is at a different address). */
5203 if (GCPhysMmioBasePrev != NIL_RTGCPHYS)
5204 {
5205 LogFlowFunc(("Unmapping previous MMIO region at %#RGp\n", GCPhysMmioBasePrev));
5206 rcStrict = PDMDevHlpMmioUnmap(pDevIns, pThis->hMmio);
5207 if (RT_FAILURE(rcStrict))
5208 {
5209 LogFunc(("Failed to unmap MMIO region at %#RGp. rc=%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
5210 break;
5211 }
5212 }
5213
5214 /* Map the newly specified MMIO region. */
5215 LogFlowFunc(("Mapping MMIO region at %#RGp\n", GCPhysMmioBase));
5216 rcStrict = PDMDevHlpMmioMap(pDevIns, pThis->hMmio, GCPhysMmioBase);
5217 if (RT_FAILURE(rcStrict))
5218 {
5219 LogFunc(("Failed to unmap MMIO region at %#RGp. rc=%Rrc\n", VBOXSTRICTRC_VAL(rcStrict)));
5220 break;
5221 }
5222 }
5223 else
5224 rcStrict = VINF_SUCCESS;
5225 break;
5226 }
5227
5228 case IOMMU_PCI_OFF_BASE_ADDR_REG_HI:
5229 {
5230 if (!pThis->IommuBar.n.u1Enable)
5231 pThis->IommuBar.au32[1] = u32Value;
5232 else
5233 {
5234 rcStrict = VINF_SUCCESS;
5235 LogFunc(("Writing Base Address (Hi) when it's already enabled -> Ignored\n"));
5236 }
5237 break;
5238 }
5239
5240 case IOMMU_PCI_OFF_MSI_CAP_HDR:
5241 {
5242 u32Value |= RT_BIT(23); /* 64-bit MSI addressess must always be enabled for IOMMU. */
5243 RT_FALL_THRU();
5244 }
5245 default:
5246 {
5247 rcStrict = PDMDevHlpPCIConfigWrite(pDevIns, pPciDev, uAddress, cb, u32Value);
5248 break;
5249 }
5250 }
5251
5252 IOMMU_UNLOCK(pDevIns, pThisR3);
5253
5254 Log3Func(("uAddress=%#x (cb=%u) with %#x. rc=%Rrc\n", uAddress, cb, u32Value, VBOXSTRICTRC_VAL(rcStrict)));
5255 return rcStrict;
5256}
5257
5258
5259/**
5260 * @callback_method_impl{FNDBGFHANDLERDEV}
5261 */
5262static DECLCALLBACK(void) iommuAmdR3DbgInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5263{
5264 PCIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
5265 PCPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
5266 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
5267
5268 bool fVerbose;
5269 if ( pszArgs
5270 && !strncmp(pszArgs, RT_STR_TUPLE("verbose")))
5271 fVerbose = true;
5272 else
5273 fVerbose = false;
5274
5275 pHlp->pfnPrintf(pHlp, "AMD-IOMMU:\n");
5276 /* Device Table Base Addresses (all segments). */
5277 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aDevTabBaseAddrs); i++)
5278 {
5279 DEV_TAB_BAR_T const DevTabBar = pThis->aDevTabBaseAddrs[i];
5280 pHlp->pfnPrintf(pHlp, " Device Table BAR %u = %#RX64\n", i, DevTabBar.u64);
5281 if (fVerbose)
5282 {
5283 pHlp->pfnPrintf(pHlp, " Size = %#x (%u bytes)\n", DevTabBar.n.u9Size,
5284 IOMMU_GET_DEV_TAB_LEN(&DevTabBar));
5285 pHlp->pfnPrintf(pHlp, " Base address = %#RX64\n", DevTabBar.n.u40Base << X86_PAGE_4K_SHIFT);
5286 }
5287 }
5288 /* Command Buffer Base Address Register. */
5289 {
5290 CMD_BUF_BAR_T const CmdBufBar = pThis->CmdBufBaseAddr;
5291 uint8_t const uEncodedLen = CmdBufBar.n.u4Len;
5292 uint32_t const cEntries = iommuAmdGetBufMaxEntries(uEncodedLen);
5293 uint32_t const cbBuffer = iommuAmdGetTotalBufLength(uEncodedLen);
5294 pHlp->pfnPrintf(pHlp, " Command Buffer BAR = %#RX64\n", CmdBufBar.u64);
5295 if (fVerbose)
5296 {
5297 pHlp->pfnPrintf(pHlp, " Base address = %#RX64\n", CmdBufBar.n.u40Base << X86_PAGE_4K_SHIFT);
5298 pHlp->pfnPrintf(pHlp, " Length = %u (%u entries, %u bytes)\n", uEncodedLen,
5299 cEntries, cbBuffer);
5300 }
5301 }
5302 /* Event Log Base Address Register. */
5303 {
5304 EVT_LOG_BAR_T const EvtLogBar = pThis->EvtLogBaseAddr;
5305 uint8_t const uEncodedLen = EvtLogBar.n.u4Len;
5306 uint32_t const cEntries = iommuAmdGetBufMaxEntries(uEncodedLen);
5307 uint32_t const cbBuffer = iommuAmdGetTotalBufLength(uEncodedLen);
5308 pHlp->pfnPrintf(pHlp, " Event Log BAR = %#RX64\n", EvtLogBar.u64);
5309 if (fVerbose)
5310 {
5311 pHlp->pfnPrintf(pHlp, " Base address = %#RX64\n", EvtLogBar.n.u40Base << X86_PAGE_4K_SHIFT);
5312 pHlp->pfnPrintf(pHlp, " Length = %u (%u entries, %u bytes)\n", uEncodedLen,
5313 cEntries, cbBuffer);
5314 }
5315 }
5316 /* IOMMU Control Register. */
5317 {
5318 IOMMU_CTRL_T const Ctrl = pThis->Ctrl;
5319 pHlp->pfnPrintf(pHlp, " Control = %#RX64\n", Ctrl.u64);
5320 if (fVerbose)
5321 {
5322 pHlp->pfnPrintf(pHlp, " IOMMU enable = %RTbool\n", Ctrl.n.u1IommuEn);
5323 pHlp->pfnPrintf(pHlp, " HT Tunnel translation enable = %RTbool\n", Ctrl.n.u1HtTunEn);
5324 pHlp->pfnPrintf(pHlp, " Event log enable = %RTbool\n", Ctrl.n.u1EvtLogEn);
5325 pHlp->pfnPrintf(pHlp, " Event log interrupt enable = %RTbool\n", Ctrl.n.u1EvtIntrEn);
5326 pHlp->pfnPrintf(pHlp, " Completion wait interrupt enable = %RTbool\n", Ctrl.n.u1EvtIntrEn);
5327 pHlp->pfnPrintf(pHlp, " Invalidation timeout = %u\n", Ctrl.n.u3InvTimeOut);
5328 pHlp->pfnPrintf(pHlp, " Pass posted write = %RTbool\n", Ctrl.n.u1PassPW);
5329 pHlp->pfnPrintf(pHlp, " Respose Pass posted write = %RTbool\n", Ctrl.n.u1ResPassPW);
5330 pHlp->pfnPrintf(pHlp, " Coherent = %RTbool\n", Ctrl.n.u1Coherent);
5331 pHlp->pfnPrintf(pHlp, " Isochronous = %RTbool\n", Ctrl.n.u1Isoc);
5332 pHlp->pfnPrintf(pHlp, " Command buffer enable = %RTbool\n", Ctrl.n.u1CmdBufEn);
5333 pHlp->pfnPrintf(pHlp, " PPR log enable = %RTbool\n", Ctrl.n.u1PprLogEn);
5334 pHlp->pfnPrintf(pHlp, " PPR interrupt enable = %RTbool\n", Ctrl.n.u1PprIntrEn);
5335 pHlp->pfnPrintf(pHlp, " PPR enable = %RTbool\n", Ctrl.n.u1PprEn);
5336 pHlp->pfnPrintf(pHlp, " Guest translation eanble = %RTbool\n", Ctrl.n.u1GstTranslateEn);
5337 pHlp->pfnPrintf(pHlp, " Guest virtual-APIC enable = %RTbool\n", Ctrl.n.u1GstVirtApicEn);
5338 pHlp->pfnPrintf(pHlp, " CRW = %#x\n", Ctrl.n.u4Crw);
5339 pHlp->pfnPrintf(pHlp, " SMI filter enable = %RTbool\n", Ctrl.n.u1SmiFilterEn);
5340 pHlp->pfnPrintf(pHlp, " Self-writeback disable = %RTbool\n", Ctrl.n.u1SelfWriteBackDis);
5341 pHlp->pfnPrintf(pHlp, " SMI filter log enable = %RTbool\n", Ctrl.n.u1SmiFilterLogEn);
5342 pHlp->pfnPrintf(pHlp, " Guest virtual-APIC mode enable = %#x\n", Ctrl.n.u3GstVirtApicModeEn);
5343 pHlp->pfnPrintf(pHlp, " Guest virtual-APIC GA log enable = %RTbool\n", Ctrl.n.u1GstLogEn);
5344 pHlp->pfnPrintf(pHlp, " Guest virtual-APIC interrupt enable = %RTbool\n", Ctrl.n.u1GstIntrEn);
5345 pHlp->pfnPrintf(pHlp, " Dual PPR log enable = %#x\n", Ctrl.n.u2DualPprLogEn);
5346 pHlp->pfnPrintf(pHlp, " Dual event log enable = %#x\n", Ctrl.n.u2DualEvtLogEn);
5347 pHlp->pfnPrintf(pHlp, " Device table segmentation enable = %#x\n", Ctrl.n.u3DevTabSegEn);
5348 pHlp->pfnPrintf(pHlp, " Privilege abort enable = %#x\n", Ctrl.n.u2PrivAbortEn);
5349 pHlp->pfnPrintf(pHlp, " PPR auto response enable = %RTbool\n", Ctrl.n.u1PprAutoRespEn);
5350 pHlp->pfnPrintf(pHlp, " MARC enable = %RTbool\n", Ctrl.n.u1MarcEn);
5351 pHlp->pfnPrintf(pHlp, " Block StopMark enable = %RTbool\n", Ctrl.n.u1BlockStopMarkEn);
5352 pHlp->pfnPrintf(pHlp, " PPR auto response always-on enable = %RTbool\n", Ctrl.n.u1PprAutoRespAlwaysOnEn);
5353 pHlp->pfnPrintf(pHlp, " Domain IDPNE = %RTbool\n", Ctrl.n.u1DomainIDPNE);
5354 pHlp->pfnPrintf(pHlp, " Enhanced PPR handling = %RTbool\n", Ctrl.n.u1EnhancedPpr);
5355 pHlp->pfnPrintf(pHlp, " Host page table access/dirty bit update = %#x\n", Ctrl.n.u2HstAccDirtyBitUpdate);
5356 pHlp->pfnPrintf(pHlp, " Guest page table dirty bit disable = %RTbool\n", Ctrl.n.u1GstDirtyUpdateDis);
5357 pHlp->pfnPrintf(pHlp, " x2APIC enable = %RTbool\n", Ctrl.n.u1X2ApicEn);
5358 pHlp->pfnPrintf(pHlp, " x2APIC interrupt enable = %RTbool\n", Ctrl.n.u1X2ApicIntrGenEn);
5359 pHlp->pfnPrintf(pHlp, " Guest page table access bit update = %RTbool\n", Ctrl.n.u1GstAccessUpdateDis);
5360 }
5361 }
5362 /* Exclusion Base Address Register. */
5363 {
5364 IOMMU_EXCL_RANGE_BAR_T const ExclRangeBar = pThis->ExclRangeBaseAddr;
5365 pHlp->pfnPrintf(pHlp, " Exclusion BAR = %#RX64\n", ExclRangeBar.u64);
5366 if (fVerbose)
5367 {
5368 pHlp->pfnPrintf(pHlp, " Exclusion enable = %RTbool\n", ExclRangeBar.n.u1ExclEnable);
5369 pHlp->pfnPrintf(pHlp, " Allow all devices = %RTbool\n", ExclRangeBar.n.u1AllowAll);
5370 pHlp->pfnPrintf(pHlp, " Base address = %#RX64\n",
5371 ExclRangeBar.n.u40ExclRangeBase << X86_PAGE_4K_SHIFT);
5372 }
5373 }
5374 /* Exclusion Range Limit Register. */
5375 {
5376 IOMMU_EXCL_RANGE_LIMIT_T const ExclRangeLimit = pThis->ExclRangeLimit;
5377 pHlp->pfnPrintf(pHlp, " Exclusion Range Limit = %#RX64\n", ExclRangeLimit.u64);
5378 if (fVerbose)
5379 {
5380 pHlp->pfnPrintf(pHlp, " Range limit = %#RX64\n",
5381 (ExclRangeLimit.n.u40ExclRangeLimit << X86_PAGE_4K_SHIFT) | X86_PAGE_4K_OFFSET_MASK);
5382 }
5383 }
5384 /* Extended Feature Register. */
5385 {
5386 IOMMU_EXT_FEAT_T ExtFeat = pThis->ExtFeat;
5387 pHlp->pfnPrintf(pHlp, " Extended Feature Register = %#RX64\n", ExtFeat.u64);
5388 if (fVerbose)
5389 {
5390 pHlp->pfnPrintf(pHlp, " Prefetch support = %RTbool\n", ExtFeat.n.u1PrefetchSup);
5391 pHlp->pfnPrintf(pHlp, " PPR support = %RTbool\n", ExtFeat.n.u1PprSup);
5392 pHlp->pfnPrintf(pHlp, " x2APIC support = %RTbool\n", ExtFeat.n.u1X2ApicSup);
5393 pHlp->pfnPrintf(pHlp, " NX and privilege level support = %RTbool\n", ExtFeat.n.u1NoExecuteSup);
5394 pHlp->pfnPrintf(pHlp, " Guest translation support = %RTbool\n", ExtFeat.n.u1GstTranslateSup);
5395 pHlp->pfnPrintf(pHlp, " Invalidate-All command support = %RTbool\n", ExtFeat.n.u1InvAllSup);
5396 pHlp->pfnPrintf(pHlp, " Guest virtual-APIC support = %RTbool\n", ExtFeat.n.u1GstVirtApicSup);
5397 pHlp->pfnPrintf(pHlp, " Hardware error register support = %RTbool\n", ExtFeat.n.u1HwErrorSup);
5398 pHlp->pfnPrintf(pHlp, " Performance counters support = %RTbool\n", ExtFeat.n.u1PerfCounterSup);
5399 pHlp->pfnPrintf(pHlp, " Host address translation size = %#x\n", ExtFeat.n.u2HostAddrTranslateSize);
5400 pHlp->pfnPrintf(pHlp, " Guest address translation size = %#x\n", ExtFeat.n.u2GstAddrTranslateSize);
5401 pHlp->pfnPrintf(pHlp, " Guest CR3 root table level support = %#x\n", ExtFeat.n.u2GstCr3RootTblLevel);
5402 pHlp->pfnPrintf(pHlp, " SMI filter register support = %#x\n", ExtFeat.n.u2SmiFilterSup);
5403 pHlp->pfnPrintf(pHlp, " SMI filter register count = %#x\n", ExtFeat.n.u3SmiFilterCount);
5404 pHlp->pfnPrintf(pHlp, " Guest virtual-APIC modes support = %#x\n", ExtFeat.n.u3GstVirtApicModeSup);
5405 pHlp->pfnPrintf(pHlp, " Dual PPR log support = %#x\n", ExtFeat.n.u2DualPprLogSup);
5406 pHlp->pfnPrintf(pHlp, " Dual event log support = %#x\n", ExtFeat.n.u2DualEvtLogSup);
5407 pHlp->pfnPrintf(pHlp, " Maximum PASID = %#x\n", ExtFeat.n.u5MaxPasidSup);
5408 pHlp->pfnPrintf(pHlp, " User/supervisor page protection support = %RTbool\n", ExtFeat.n.u1UserSupervisorSup);
5409 pHlp->pfnPrintf(pHlp, " Device table segments supported = %#x (%u)\n", ExtFeat.n.u2DevTabSegSup,
5410 g_acDevTabSegs[ExtFeat.n.u2DevTabSegSup]);
5411 pHlp->pfnPrintf(pHlp, " PPR log overflow early warning support = %RTbool\n", ExtFeat.n.u1PprLogOverflowWarn);
5412 pHlp->pfnPrintf(pHlp, " PPR auto response support = %RTbool\n", ExtFeat.n.u1PprAutoRespSup);
5413 pHlp->pfnPrintf(pHlp, " MARC support = %#x\n", ExtFeat.n.u2MarcSup);
5414 pHlp->pfnPrintf(pHlp, " Block StopMark message support = %RTbool\n", ExtFeat.n.u1BlockStopMarkSup);
5415 pHlp->pfnPrintf(pHlp, " Performance optimization support = %RTbool\n", ExtFeat.n.u1PerfOptSup);
5416 pHlp->pfnPrintf(pHlp, " MSI capability MMIO access support = %RTbool\n", ExtFeat.n.u1MsiCapMmioSup);
5417 pHlp->pfnPrintf(pHlp, " Guest I/O protection support = %RTbool\n", ExtFeat.n.u1GstIoSup);
5418 pHlp->pfnPrintf(pHlp, " Host access support = %RTbool\n", ExtFeat.n.u1HostAccessSup);
5419 pHlp->pfnPrintf(pHlp, " Enhanced PPR handling support = %RTbool\n", ExtFeat.n.u1EnhancedPprSup);
5420 pHlp->pfnPrintf(pHlp, " Attribute forward supported = %RTbool\n", ExtFeat.n.u1AttrForwardSup);
5421 pHlp->pfnPrintf(pHlp, " Host dirty support = %RTbool\n", ExtFeat.n.u1HostDirtySup);
5422 pHlp->pfnPrintf(pHlp, " Invalidate IOTLB type support = %RTbool\n", ExtFeat.n.u1InvIoTlbTypeSup);
5423 pHlp->pfnPrintf(pHlp, " Guest page table access bit hw disable = %RTbool\n", ExtFeat.n.u1GstUpdateDisSup);
5424 pHlp->pfnPrintf(pHlp, " Force physical dest for remapped intr. = %RTbool\n", ExtFeat.n.u1ForcePhysDstSup);
5425 }
5426 }
5427 /* PPR Log Base Address Register. */
5428 {
5429 PPR_LOG_BAR_T PprLogBar = pThis->PprLogBaseAddr;
5430 uint8_t const uEncodedLen = PprLogBar.n.u4Len;
5431 uint32_t const cEntries = iommuAmdGetBufMaxEntries(uEncodedLen);
5432 uint32_t const cbBuffer = iommuAmdGetTotalBufLength(uEncodedLen);
5433 pHlp->pfnPrintf(pHlp, " PPR Log BAR = %#RX64\n", PprLogBar.u64);
5434 if (fVerbose)
5435 {
5436 pHlp->pfnPrintf(pHlp, " Base address = %#RX64\n", PprLogBar.n.u40Base << X86_PAGE_4K_SHIFT);
5437 pHlp->pfnPrintf(pHlp, " Length = %u (%u entries, %u bytes)\n", uEncodedLen,
5438 cEntries, cbBuffer);
5439 }
5440 }
5441 /* Hardware Event (Hi) Register. */
5442 {
5443 IOMMU_HW_EVT_HI_T HwEvtHi = pThis->HwEvtHi;
5444 pHlp->pfnPrintf(pHlp, " Hardware Event (Hi) = %#RX64\n", HwEvtHi.u64);
5445 if (fVerbose)
5446 {
5447 pHlp->pfnPrintf(pHlp, " First operand = %#RX64\n", HwEvtHi.n.u60FirstOperand);
5448 pHlp->pfnPrintf(pHlp, " Event code = %#RX8\n", HwEvtHi.n.u4EvtCode);
5449 }
5450 }
5451 /* Hardware Event (Lo) Register. */
5452 pHlp->pfnPrintf(pHlp, " Hardware Event (Lo) = %#RX64\n", pThis->HwEvtLo);
5453 /* Hardware Event Status. */
5454 {
5455 IOMMU_HW_EVT_STATUS_T HwEvtStatus = pThis->HwEvtStatus;
5456 pHlp->pfnPrintf(pHlp, " Hardware Event Status = %#RX64\n", HwEvtStatus.u64);
5457 if (fVerbose)
5458 {
5459 pHlp->pfnPrintf(pHlp, " Valid = %RTbool\n", HwEvtStatus.n.u1Valid);
5460 pHlp->pfnPrintf(pHlp, " Overflow = %RTbool\n", HwEvtStatus.n.u1Overflow);
5461 }
5462 }
5463 /* Guest Virtual-APIC Log Base Address Register. */
5464 {
5465 GALOG_BAR_T const GALogBar = pThis->GALogBaseAddr;
5466 uint8_t const uEncodedLen = GALogBar.n.u4Len;
5467 uint32_t const cEntries = iommuAmdGetBufMaxEntries(uEncodedLen);
5468 uint32_t const cbBuffer = iommuAmdGetTotalBufLength(uEncodedLen);
5469 pHlp->pfnPrintf(pHlp, " Guest Log BAR = %#RX64\n", GALogBar.u64);
5470 if (fVerbose)
5471 {
5472 pHlp->pfnPrintf(pHlp, " Base address = %#RX64\n", GALogBar.n.u40Base << X86_PAGE_4K_SHIFT);
5473 pHlp->pfnPrintf(pHlp, " Length = %u (%u entries, %u bytes)\n", uEncodedLen,
5474 cEntries, cbBuffer);
5475 }
5476 }
5477 /* Guest Virtual-APIC Log Tail Address Register. */
5478 {
5479 GALOG_TAIL_ADDR_T GALogTail = pThis->GALogTailAddr;
5480 pHlp->pfnPrintf(pHlp, " Guest Log Tail Address = %#RX64\n", GALogTail.u64);
5481 if (fVerbose)
5482 pHlp->pfnPrintf(pHlp, " Tail address = %#RX64\n", GALogTail.n.u40GALogTailAddr);
5483 }
5484 /* PPR Log B Base Address Register. */
5485 {
5486 PPR_LOG_B_BAR_T PprLogBBar = pThis->PprLogBBaseAddr;
5487 uint8_t const uEncodedLen = PprLogBBar.n.u4Len;
5488 uint32_t const cEntries = iommuAmdGetBufMaxEntries(uEncodedLen);
5489 uint32_t const cbBuffer = iommuAmdGetTotalBufLength(uEncodedLen);
5490 pHlp->pfnPrintf(pHlp, " PPR Log B BAR = %#RX64\n", PprLogBBar.u64);
5491 if (fVerbose)
5492 {
5493 pHlp->pfnPrintf(pHlp, " Base address = %#RX64\n", PprLogBBar.n.u40Base << X86_PAGE_4K_SHIFT);
5494 pHlp->pfnPrintf(pHlp, " Length = %u (%u entries, %u bytes)\n", uEncodedLen,
5495 cEntries, cbBuffer);
5496 }
5497 }
5498 /* Event Log B Base Address Register. */
5499 {
5500 EVT_LOG_B_BAR_T EvtLogBBar = pThis->EvtLogBBaseAddr;
5501 uint8_t const uEncodedLen = EvtLogBBar.n.u4Len;
5502 uint32_t const cEntries = iommuAmdGetBufMaxEntries(uEncodedLen);
5503 uint32_t const cbBuffer = iommuAmdGetTotalBufLength(uEncodedLen);
5504 pHlp->pfnPrintf(pHlp, " Event Log B BAR = %#RX64\n", EvtLogBBar.u64);
5505 if (fVerbose)
5506 {
5507 pHlp->pfnPrintf(pHlp, " Base address = %#RX64\n", EvtLogBBar.n.u40Base << X86_PAGE_4K_SHIFT);
5508 pHlp->pfnPrintf(pHlp, " Length = %u (%u entries, %u bytes)\n", uEncodedLen,
5509 cEntries, cbBuffer);
5510 }
5511 }
5512 /* Device-Specific Feature Extension Register. */
5513 {
5514 DEV_SPECIFIC_FEAT_T const DevSpecificFeat = pThis->DevSpecificFeat;
5515 pHlp->pfnPrintf(pHlp, " Device-specific Feature = %#RX64\n", DevSpecificFeat.u64);
5516 if (fVerbose)
5517 {
5518 pHlp->pfnPrintf(pHlp, " Feature = %#RX32\n", DevSpecificFeat.n.u24DevSpecFeat);
5519 pHlp->pfnPrintf(pHlp, " Minor revision ID = %#x\n", DevSpecificFeat.n.u4RevMinor);
5520 pHlp->pfnPrintf(pHlp, " Major revision ID = %#x\n", DevSpecificFeat.n.u4RevMajor);
5521 }
5522 }
5523 /* Device-Specific Control Extension Register. */
5524 {
5525 DEV_SPECIFIC_CTRL_T const DevSpecificCtrl = pThis->DevSpecificCtrl;
5526 pHlp->pfnPrintf(pHlp, " Device-specific Control = %#RX64\n", DevSpecificCtrl.u64);
5527 if (fVerbose)
5528 {
5529 pHlp->pfnPrintf(pHlp, " Control = %#RX32\n", DevSpecificCtrl.n.u24DevSpecCtrl);
5530 pHlp->pfnPrintf(pHlp, " Minor revision ID = %#x\n", DevSpecificCtrl.n.u4RevMinor);
5531 pHlp->pfnPrintf(pHlp, " Major revision ID = %#x\n", DevSpecificCtrl.n.u4RevMajor);
5532 }
5533 }
5534 /* Device-Specific Status Extension Register. */
5535 {
5536 DEV_SPECIFIC_STATUS_T const DevSpecificStatus = pThis->DevSpecificStatus;
5537 pHlp->pfnPrintf(pHlp, " Device-specific Status = %#RX64\n", DevSpecificStatus.u64);
5538 if (fVerbose)
5539 {
5540 pHlp->pfnPrintf(pHlp, " Status = %#RX32\n", DevSpecificStatus.n.u24DevSpecStatus);
5541 pHlp->pfnPrintf(pHlp, " Minor revision ID = %#x\n", DevSpecificStatus.n.u4RevMinor);
5542 pHlp->pfnPrintf(pHlp, " Major revision ID = %#x\n", DevSpecificStatus.n.u4RevMajor);
5543 }
5544 }
5545 /* Miscellaneous Information Register (Lo and Hi). */
5546 {
5547 MSI_MISC_INFO_T const MiscInfo = pThis->MiscInfo;
5548 pHlp->pfnPrintf(pHlp, " Misc. Info. Register = %#RX64\n", MiscInfo.u64);
5549 if (fVerbose)
5550 {
5551 pHlp->pfnPrintf(pHlp, " Event Log MSI number = %#x\n", MiscInfo.n.u5MsiNumEvtLog);
5552 pHlp->pfnPrintf(pHlp, " Guest Virtual-Address Size = %#x\n", MiscInfo.n.u3GstVirtAddrSize);
5553 pHlp->pfnPrintf(pHlp, " Physical Address Size = %#x\n", MiscInfo.n.u7PhysAddrSize);
5554 pHlp->pfnPrintf(pHlp, " Virtual-Address Size = %#x\n", MiscInfo.n.u7VirtAddrSize);
5555 pHlp->pfnPrintf(pHlp, " HT Transport ATS Range Reserved = %RTbool\n", MiscInfo.n.u1HtAtsResv);
5556 pHlp->pfnPrintf(pHlp, " PPR MSI number = %#x\n", MiscInfo.n.u5MsiNumPpr);
5557 pHlp->pfnPrintf(pHlp, " GA Log MSI number = %#x\n", MiscInfo.n.u5MsiNumGa);
5558 }
5559 }
5560 /* MSI Capability Header. */
5561 {
5562 MSI_CAP_HDR_T MsiCapHdr;
5563 MsiCapHdr.u32 = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_CAP_HDR);
5564 pHlp->pfnPrintf(pHlp, " MSI Capability Header = %#RX32\n", MsiCapHdr.u32);
5565 if (fVerbose)
5566 {
5567 pHlp->pfnPrintf(pHlp, " Capability ID = %#x\n", MsiCapHdr.n.u8MsiCapId);
5568 pHlp->pfnPrintf(pHlp, " Capability Ptr (PCI config offset) = %#x\n", MsiCapHdr.n.u8MsiCapPtr);
5569 pHlp->pfnPrintf(pHlp, " Enable = %RTbool\n", MsiCapHdr.n.u1MsiEnable);
5570 pHlp->pfnPrintf(pHlp, " Multi-message capability = %#x\n", MsiCapHdr.n.u3MsiMultiMessCap);
5571 pHlp->pfnPrintf(pHlp, " Multi-message enable = %#x\n", MsiCapHdr.n.u3MsiMultiMessEn);
5572 }
5573 }
5574 /* MSI Address Register (Lo and Hi). */
5575 {
5576 uint32_t const uMsiAddrLo = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_ADDR_LO);
5577 uint32_t const uMsiAddrHi = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_ADDR_HI);
5578 MSIADDR MsiAddr;
5579 MsiAddr.u64 = RT_MAKE_U64(uMsiAddrLo, uMsiAddrHi);
5580 pHlp->pfnPrintf(pHlp, " MSI Address = %#RX64\n", MsiAddr.u64);
5581 if (fVerbose)
5582 {
5583 pHlp->pfnPrintf(pHlp, " Destination mode = %#x\n", MsiAddr.n.u1DestMode);
5584 pHlp->pfnPrintf(pHlp, " Redirection hint = %#x\n", MsiAddr.n.u1RedirHint);
5585 pHlp->pfnPrintf(pHlp, " Destination Id = %#x\n", MsiAddr.n.u8DestId);
5586 pHlp->pfnPrintf(pHlp, " Address = %#RX32\n", MsiAddr.n.u12Addr);
5587 pHlp->pfnPrintf(pHlp, " Address (Hi) / Rsvd? = %#RX32\n", MsiAddr.n.u32Rsvd0);
5588 }
5589 }
5590 /* MSI Data. */
5591 {
5592 MSIDATA MsiData;
5593 MsiData.u32 = PDMPciDevGetDWord(pPciDev, IOMMU_PCI_OFF_MSI_DATA);
5594 pHlp->pfnPrintf(pHlp, " MSI Data = %#RX32\n", MsiData.u32);
5595 if (fVerbose)
5596 {
5597 pHlp->pfnPrintf(pHlp, " Vector = %#x (%u)\n", MsiData.n.u8Vector,
5598 MsiData.n.u8Vector);
5599 pHlp->pfnPrintf(pHlp, " Delivery mode = %#x\n", MsiData.n.u3DeliveryMode);
5600 pHlp->pfnPrintf(pHlp, " Level = %#x\n", MsiData.n.u1Level);
5601 pHlp->pfnPrintf(pHlp, " Trigger mode = %s\n", MsiData.n.u1TriggerMode ?
5602 "level" : "edge");
5603 }
5604 }
5605 /* MSI Mapping Capability Header (HyperTransport, reporting all 0s currently). */
5606 {
5607 MSI_MAP_CAP_HDR_T MsiMapCapHdr;
5608 MsiMapCapHdr.u32 = 0;
5609 pHlp->pfnPrintf(pHlp, " MSI Mapping Capability Header = %#RX32\n", MsiMapCapHdr.u32);
5610 if (fVerbose)
5611 {
5612 pHlp->pfnPrintf(pHlp, " Capability ID = %#x\n", MsiMapCapHdr.n.u8MsiMapCapId);
5613 pHlp->pfnPrintf(pHlp, " Map enable = %RTbool\n", MsiMapCapHdr.n.u1MsiMapEn);
5614 pHlp->pfnPrintf(pHlp, " Map fixed = %RTbool\n", MsiMapCapHdr.n.u1MsiMapFixed);
5615 pHlp->pfnPrintf(pHlp, " Map capability type = %#x\n", MsiMapCapHdr.n.u5MapCapType);
5616 }
5617 }
5618 /* Performance Optimization Control Register. */
5619 {
5620 IOMMU_PERF_OPT_CTRL_T const PerfOptCtrl = pThis->PerfOptCtrl;
5621 pHlp->pfnPrintf(pHlp, " Performance Optimization Control = %#RX32\n", PerfOptCtrl.u32);
5622 if (fVerbose)
5623 pHlp->pfnPrintf(pHlp, " Enable = %RTbool\n", PerfOptCtrl.n.u1PerfOptEn);
5624 }
5625 /* XT (x2APIC) General Interrupt Control Register. */
5626 {
5627 IOMMU_XT_GEN_INTR_CTRL_T const XtGenIntrCtrl = pThis->XtGenIntrCtrl;
5628 pHlp->pfnPrintf(pHlp, " XT General Interrupt Control = %#RX64\n", XtGenIntrCtrl.u64);
5629 if (fVerbose)
5630 {
5631 pHlp->pfnPrintf(pHlp, " Interrupt destination mode = %s\n",
5632 !XtGenIntrCtrl.n.u1X2ApicIntrDstMode ? "physical" : "logical");
5633 pHlp->pfnPrintf(pHlp, " Interrupt destination = %#RX64\n",
5634 RT_MAKE_U64(XtGenIntrCtrl.n.u24X2ApicIntrDstLo, XtGenIntrCtrl.n.u7X2ApicIntrDstHi));
5635 pHlp->pfnPrintf(pHlp, " Interrupt vector = %#x\n", XtGenIntrCtrl.n.u8X2ApicIntrVector);
5636 pHlp->pfnPrintf(pHlp, " Interrupt delivery mode = %s\n",
5637 !XtGenIntrCtrl.n.u8X2ApicIntrVector ? "fixed" : "arbitrated");
5638 }
5639 }
5640 /* XT (x2APIC) PPR Interrupt Control Register. */
5641 {
5642 IOMMU_XT_PPR_INTR_CTRL_T const XtPprIntrCtrl = pThis->XtPprIntrCtrl;
5643 pHlp->pfnPrintf(pHlp, " XT PPR Interrupt Control = %#RX64\n", XtPprIntrCtrl.u64);
5644 if (fVerbose)
5645 {
5646 pHlp->pfnPrintf(pHlp, " Interrupt destination mode = %s\n",
5647 !XtPprIntrCtrl.n.u1X2ApicIntrDstMode ? "physical" : "logical");
5648 pHlp->pfnPrintf(pHlp, " Interrupt destination = %#RX64\n",
5649 RT_MAKE_U64(XtPprIntrCtrl.n.u24X2ApicIntrDstLo, XtPprIntrCtrl.n.u7X2ApicIntrDstHi));
5650 pHlp->pfnPrintf(pHlp, " Interrupt vector = %#x\n", XtPprIntrCtrl.n.u8X2ApicIntrVector);
5651 pHlp->pfnPrintf(pHlp, " Interrupt delivery mode = %s\n",
5652 !XtPprIntrCtrl.n.u8X2ApicIntrVector ? "fixed" : "arbitrated");
5653 }
5654 }
5655 /* XT (X2APIC) GA Log Interrupt Control Register. */
5656 {
5657 IOMMU_XT_GALOG_INTR_CTRL_T const XtGALogIntrCtrl = pThis->XtGALogIntrCtrl;
5658 pHlp->pfnPrintf(pHlp, " XT PPR Interrupt Control = %#RX64\n", XtGALogIntrCtrl.u64);
5659 if (fVerbose)
5660 {
5661 pHlp->pfnPrintf(pHlp, " Interrupt destination mode = %s\n",
5662 !XtGALogIntrCtrl.n.u1X2ApicIntrDstMode ? "physical" : "logical");
5663 pHlp->pfnPrintf(pHlp, " Interrupt destination = %#RX64\n",
5664 RT_MAKE_U64(XtGALogIntrCtrl.n.u24X2ApicIntrDstLo, XtGALogIntrCtrl.n.u7X2ApicIntrDstHi));
5665 pHlp->pfnPrintf(pHlp, " Interrupt vector = %#x\n", XtGALogIntrCtrl.n.u8X2ApicIntrVector);
5666 pHlp->pfnPrintf(pHlp, " Interrupt delivery mode = %s\n",
5667 !XtGALogIntrCtrl.n.u8X2ApicIntrVector ? "fixed" : "arbitrated");
5668 }
5669 }
5670 /* MARC Registers. */
5671 {
5672 for (unsigned i = 0; i < RT_ELEMENTS(pThis->aMarcApers); i++)
5673 {
5674 pHlp->pfnPrintf(pHlp, " MARC Aperature %u:\n", i);
5675 MARC_APER_BAR_T const MarcAperBar = pThis->aMarcApers[i].Base;
5676 pHlp->pfnPrintf(pHlp, " Base = %#RX64\n", MarcAperBar.n.u40MarcBaseAddr << X86_PAGE_4K_SHIFT);
5677
5678 MARC_APER_RELOC_T const MarcAperReloc = pThis->aMarcApers[i].Reloc;
5679 pHlp->pfnPrintf(pHlp, " Reloc = %#RX64 (addr: %#RX64, read-only: %RTbool, enable: %RTbool)\n",
5680 MarcAperReloc.u64, MarcAperReloc.n.u40MarcRelocAddr << X86_PAGE_4K_SHIFT,
5681 MarcAperReloc.n.u1ReadOnly, MarcAperReloc.n.u1RelocEn);
5682
5683 MARC_APER_LEN_T const MarcAperLen = pThis->aMarcApers[i].Length;
5684 pHlp->pfnPrintf(pHlp, " Length = %u pages\n", MarcAperLen.n.u40MarcLength);
5685 }
5686 }
5687 /* Reserved Register. */
5688 pHlp->pfnPrintf(pHlp, " Reserved Register = %#RX64\n", pThis->RsvdReg);
5689 /* Command Buffer Head Pointer Register. */
5690 {
5691 CMD_BUF_HEAD_PTR_T const CmdBufHeadPtr = pThis->CmdBufHeadPtr;
5692 pHlp->pfnPrintf(pHlp, " Command Buffer Head Pointer = %#RX64 (off: %#x)\n", CmdBufHeadPtr.u64,
5693 CmdBufHeadPtr.n.off);
5694 }
5695 /* Command Buffer Tail Pointer Register. */
5696 {
5697 CMD_BUF_HEAD_PTR_T const CmdBufTailPtr = pThis->CmdBufTailPtr;
5698 pHlp->pfnPrintf(pHlp, " Command Buffer Tail Pointer = %#RX64 (off: %#x)\n", CmdBufTailPtr.u64,
5699 CmdBufTailPtr.n.off);
5700 }
5701 /* Event Log Head Pointer Register. */
5702 {
5703 EVT_LOG_HEAD_PTR_T const EvtLogHeadPtr = pThis->EvtLogHeadPtr;
5704 pHlp->pfnPrintf(pHlp, " Event Log Head Pointer = %#RX64 (off: %#x)\n", EvtLogHeadPtr.u64,
5705 EvtLogHeadPtr.n.off);
5706 }
5707 /* Event Log Tail Pointer Register. */
5708 {
5709 EVT_LOG_TAIL_PTR_T const EvtLogTailPtr = pThis->EvtLogTailPtr;
5710 pHlp->pfnPrintf(pHlp, " Event Log Head Pointer = %#RX64 (off: %#x)\n", EvtLogTailPtr.u64,
5711 EvtLogTailPtr.n.off);
5712 }
5713 /* Status Register. */
5714 {
5715 IOMMU_STATUS_T const Status = pThis->Status;
5716 pHlp->pfnPrintf(pHlp, " Status Register = %#RX64\n", Status.u64);
5717 if (fVerbose)
5718 {
5719 pHlp->pfnPrintf(pHlp, " Event log overflow = %RTbool\n", Status.n.u1EvtOverflow);
5720 pHlp->pfnPrintf(pHlp, " Event log interrupt = %RTbool\n", Status.n.u1EvtLogIntr);
5721 pHlp->pfnPrintf(pHlp, " Completion wait interrupt = %RTbool\n", Status.n.u1CompWaitIntr);
5722 pHlp->pfnPrintf(pHlp, " Event log running = %RTbool\n", Status.n.u1EvtLogRunning);
5723 pHlp->pfnPrintf(pHlp, " Command buffer running = %RTbool\n", Status.n.u1CmdBufRunning);
5724 pHlp->pfnPrintf(pHlp, " PPR overflow = %RTbool\n", Status.n.u1PprOverflow);
5725 pHlp->pfnPrintf(pHlp, " PPR interrupt = %RTbool\n", Status.n.u1PprIntr);
5726 pHlp->pfnPrintf(pHlp, " PPR log running = %RTbool\n", Status.n.u1PprLogRunning);
5727 pHlp->pfnPrintf(pHlp, " Guest log running = %RTbool\n", Status.n.u1GstLogRunning);
5728 pHlp->pfnPrintf(pHlp, " Guest log interrupt = %RTbool\n", Status.n.u1GstLogIntr);
5729 pHlp->pfnPrintf(pHlp, " PPR log B overflow = %RTbool\n", Status.n.u1PprOverflowB);
5730 pHlp->pfnPrintf(pHlp, " PPR log active = %RTbool\n", Status.n.u1PprLogActive);
5731 pHlp->pfnPrintf(pHlp, " Event log B overflow = %RTbool\n", Status.n.u1EvtOverflowB);
5732 pHlp->pfnPrintf(pHlp, " Event log active = %RTbool\n", Status.n.u1EvtLogActive);
5733 pHlp->pfnPrintf(pHlp, " PPR log B overflow early warning = %RTbool\n", Status.n.u1PprOverflowEarlyB);
5734 pHlp->pfnPrintf(pHlp, " PPR log overflow early warning = %RTbool\n", Status.n.u1PprOverflowEarly);
5735 }
5736 }
5737 /* PPR Log Head Pointer. */
5738 {
5739 PPR_LOG_HEAD_PTR_T const PprLogHeadPtr = pThis->PprLogHeadPtr;
5740 pHlp->pfnPrintf(pHlp, " PPR Log Head Pointer = %#RX64 (off: %#x)\n", PprLogHeadPtr.u64,
5741 PprLogHeadPtr.n.off);
5742 }
5743 /* PPR Log Tail Pointer. */
5744 {
5745 PPR_LOG_TAIL_PTR_T const PprLogTailPtr = pThis->PprLogTailPtr;
5746 pHlp->pfnPrintf(pHlp, " PPR Log Tail Pointer = %#RX64 (off: %#x)\n", PprLogTailPtr.u64,
5747 PprLogTailPtr.n.off);
5748 }
5749 /* Guest Virtual-APIC Log Head Pointer. */
5750 {
5751 GALOG_HEAD_PTR_T const GALogHeadPtr = pThis->GALogHeadPtr;
5752 pHlp->pfnPrintf(pHlp, " Guest Virtual-APIC Log Head Pointer = %#RX64 (off: %#x)\n", GALogHeadPtr.u64,
5753 GALogHeadPtr.n.u12GALogPtr);
5754 }
5755 /* Guest Virtual-APIC Log Tail Pointer. */
5756 {
5757 GALOG_HEAD_PTR_T const GALogTailPtr = pThis->GALogTailPtr;
5758 pHlp->pfnPrintf(pHlp, " Guest Virtual-APIC Log Tail Pointer = %#RX64 (off: %#x)\n", GALogTailPtr.u64,
5759 GALogTailPtr.n.u12GALogPtr);
5760 }
5761 /* PPR Log B Head Pointer. */
5762 {
5763 PPR_LOG_B_HEAD_PTR_T const PprLogBHeadPtr = pThis->PprLogBHeadPtr;
5764 pHlp->pfnPrintf(pHlp, " PPR Log B Head Pointer = %#RX64 (off: %#x)\n", PprLogBHeadPtr.u64,
5765 PprLogBHeadPtr.n.off);
5766 }
5767 /* PPR Log B Tail Pointer. */
5768 {
5769 PPR_LOG_B_TAIL_PTR_T const PprLogBTailPtr = pThis->PprLogBTailPtr;
5770 pHlp->pfnPrintf(pHlp, " PPR Log B Tail Pointer = %#RX64 (off: %#x)\n", PprLogBTailPtr.u64,
5771 PprLogBTailPtr.n.off);
5772 }
5773 /* Event Log B Head Pointer. */
5774 {
5775 EVT_LOG_B_HEAD_PTR_T const EvtLogBHeadPtr = pThis->EvtLogBHeadPtr;
5776 pHlp->pfnPrintf(pHlp, " Event Log B Head Pointer = %#RX64 (off: %#x)\n", EvtLogBHeadPtr.u64,
5777 EvtLogBHeadPtr.n.off);
5778 }
5779 /* Event Log B Tail Pointer. */
5780 {
5781 EVT_LOG_B_TAIL_PTR_T const EvtLogBTailPtr = pThis->EvtLogBTailPtr;
5782 pHlp->pfnPrintf(pHlp, " Event Log B Tail Pointer = %#RX64 (off: %#x)\n", EvtLogBTailPtr.u64,
5783 EvtLogBTailPtr.n.off);
5784 }
5785 /* PPR Log Auto Response Register. */
5786 {
5787 PPR_LOG_AUTO_RESP_T const PprLogAutoResp = pThis->PprLogAutoResp;
5788 pHlp->pfnPrintf(pHlp, " PPR Log Auto Response Register = %#RX64\n", PprLogAutoResp.u64);
5789 if (fVerbose)
5790 {
5791 pHlp->pfnPrintf(pHlp, " Code = %#x\n", PprLogAutoResp.n.u4AutoRespCode);
5792 pHlp->pfnPrintf(pHlp, " Mask Gen. = %RTbool\n", PprLogAutoResp.n.u1AutoRespMaskGen);
5793 }
5794 }
5795 /* PPR Log Overflow Early Warning Indicator Register. */
5796 {
5797 PPR_LOG_OVERFLOW_EARLY_T const PprLogOverflowEarly = pThis->PprLogOverflowEarly;
5798 pHlp->pfnPrintf(pHlp, " PPR Log overflow early warning = %#RX64\n", PprLogOverflowEarly.u64);
5799 if (fVerbose)
5800 {
5801 pHlp->pfnPrintf(pHlp, " Threshold = %#x\n", PprLogOverflowEarly.n.u15Threshold);
5802 pHlp->pfnPrintf(pHlp, " Interrupt enable = %RTbool\n", PprLogOverflowEarly.n.u1IntrEn);
5803 pHlp->pfnPrintf(pHlp, " Enable = %RTbool\n", PprLogOverflowEarly.n.u1Enable);
5804 }
5805 }
5806 /* PPR Log Overflow Early Warning Indicator Register. */
5807 {
5808 PPR_LOG_OVERFLOW_EARLY_T const PprLogBOverflowEarly = pThis->PprLogBOverflowEarly;
5809 pHlp->pfnPrintf(pHlp, " PPR Log B overflow early warning = %#RX64\n", PprLogBOverflowEarly.u64);
5810 if (fVerbose)
5811 {
5812 pHlp->pfnPrintf(pHlp, " Threshold = %#x\n", PprLogBOverflowEarly.n.u15Threshold);
5813 pHlp->pfnPrintf(pHlp, " Interrupt enable = %RTbool\n", PprLogBOverflowEarly.n.u1IntrEn);
5814 pHlp->pfnPrintf(pHlp, " Enable = %RTbool\n", PprLogBOverflowEarly.n.u1Enable);
5815 }
5816 }
5817}
5818
5819
5820/**
5821 * Dumps the DTE via the info callback helper.
5822 *
5823 * @param pHlp The info helper.
5824 * @param pDte The device table entry.
5825 * @param pszPrefix The string prefix.
5826 */
5827static void iommuAmdR3DbgInfoDteWorker(PCDBGFINFOHLP pHlp, PCDTE_T pDte, const char *pszPrefix)
5828{
5829 AssertReturnVoid(pHlp);
5830 AssertReturnVoid(pDte);
5831 AssertReturnVoid(pszPrefix);
5832
5833 pHlp->pfnPrintf(pHlp, "%sValid = %RTbool\n", pszPrefix, pDte->n.u1Valid);
5834 pHlp->pfnPrintf(pHlp, "%sTranslation Valid = %RTbool\n", pszPrefix, pDte->n.u1TranslationValid);
5835 pHlp->pfnPrintf(pHlp, "%sHost Access Dirty = %#x\n", pszPrefix, pDte->n.u2Had);
5836 pHlp->pfnPrintf(pHlp, "%sPaging Mode = %u\n", pszPrefix, pDte->n.u3Mode);
5837 pHlp->pfnPrintf(pHlp, "%sPage Table Root Ptr = %#RX64 (addr=%#RGp)\n", pszPrefix, pDte->n.u40PageTableRootPtrLo,
5838 pDte->n.u40PageTableRootPtrLo << 12);
5839 pHlp->pfnPrintf(pHlp, "%sPPR enable = %RTbool\n", pszPrefix, pDte->n.u1Ppr);
5840 pHlp->pfnPrintf(pHlp, "%sGuest PPR Resp w/ PASID = %RTbool\n", pszPrefix, pDte->n.u1GstPprRespPasid);
5841 pHlp->pfnPrintf(pHlp, "%sGuest I/O Prot Valid = %RTbool\n", pszPrefix, pDte->n.u1GstIoValid);
5842 pHlp->pfnPrintf(pHlp, "%sGuest Translation Valid = %RTbool\n", pszPrefix, pDte->n.u1GstTranslateValid);
5843 pHlp->pfnPrintf(pHlp, "%sGuest Levels Translated = %#x\n", pszPrefix, pDte->n.u2GstMode);
5844 pHlp->pfnPrintf(pHlp, "%sGuest Root Page Table Ptr = %#x %#x %#x (addr=%#RGp)\n", pszPrefix,
5845 pDte->n.u3GstCr3TableRootPtrLo, pDte->n.u16GstCr3TableRootPtrMid, pDte->n.u21GstCr3TableRootPtrHi,
5846 (pDte->n.u21GstCr3TableRootPtrHi << 31)
5847 | (pDte->n.u16GstCr3TableRootPtrMid << 15)
5848 | (pDte->n.u3GstCr3TableRootPtrLo << 12));
5849 pHlp->pfnPrintf(pHlp, "%sI/O Read = %s\n", pszPrefix, pDte->n.u1IoRead ? "allowed" : "denied");
5850 pHlp->pfnPrintf(pHlp, "%sI/O Write = %s\n", pszPrefix, pDte->n.u1IoWrite ? "allowed" : "denied");
5851 pHlp->pfnPrintf(pHlp, "%sReserved (MBZ) = %#x\n", pszPrefix, pDte->n.u1Rsvd0);
5852 pHlp->pfnPrintf(pHlp, "%sDomain ID = %u (%#x)\n", pszPrefix, pDte->n.u16DomainId, pDte->n.u16DomainId);
5853 pHlp->pfnPrintf(pHlp, "%sIOTLB Enable = %RTbool\n", pszPrefix, pDte->n.u1IoTlbEnable);
5854 pHlp->pfnPrintf(pHlp, "%sSuppress I/O PFs = %RTbool\n", pszPrefix, pDte->n.u1SuppressPfEvents);
5855 pHlp->pfnPrintf(pHlp, "%sSuppress all I/O PFs = %RTbool\n", pszPrefix, pDte->n.u1SuppressAllPfEvents);
5856 pHlp->pfnPrintf(pHlp, "%sPort I/O Control = %#x\n", pszPrefix, pDte->n.u2IoCtl);
5857 pHlp->pfnPrintf(pHlp, "%sIOTLB Cache Hint = %s\n", pszPrefix, pDte->n.u1Cache ? "no caching" : "cache");
5858 pHlp->pfnPrintf(pHlp, "%sSnoop Disable = %RTbool\n", pszPrefix, pDte->n.u1SnoopDisable);
5859 pHlp->pfnPrintf(pHlp, "%sAllow Exclusion = %RTbool\n", pszPrefix, pDte->n.u1AllowExclusion);
5860 pHlp->pfnPrintf(pHlp, "%sSysMgt Message Enable = %RTbool\n", pszPrefix, pDte->n.u2SysMgt);
5861 pHlp->pfnPrintf(pHlp, "%sInterrupt Map Valid = %RTbool\n", pszPrefix, pDte->n.u1IntrMapValid);
5862 uint8_t const uIntrTabLen = pDte->n.u4IntrTableLength;
5863 if (uIntrTabLen < IOMMU_DTE_INTR_TAB_LEN_MAX)
5864 {
5865 uint16_t const cEntries = IOMMU_DTE_GET_INTR_TAB_ENTRIES(pDte);
5866 uint16_t const cbIntrTable = IOMMU_DTE_GET_INTR_TAB_LEN(pDte);
5867 pHlp->pfnPrintf(pHlp, "%sInterrupt Table Length = %#x (%u entries, %u bytes)\n", pszPrefix, uIntrTabLen, cEntries,
5868 cbIntrTable);
5869 }
5870 else
5871 pHlp->pfnPrintf(pHlp, "%sInterrupt Table Length = %#x (invalid!)\n", pszPrefix, uIntrTabLen);
5872 pHlp->pfnPrintf(pHlp, "%sIgnore Unmapped Interrupts = %RTbool\n", pszPrefix, pDte->n.u1IgnoreUnmappedIntrs);
5873 pHlp->pfnPrintf(pHlp, "%sInterrupt Table Root Ptr = %#RX64 (addr=%#RGp)\n", pszPrefix,
5874 pDte->n.u46IntrTableRootPtr, pDte->au64[2] & IOMMU_DTE_IRTE_ROOT_PTR_MASK);
5875 pHlp->pfnPrintf(pHlp, "%sReserved (MBZ) = %#x\n", pszPrefix, pDte->n.u4Rsvd0);
5876 pHlp->pfnPrintf(pHlp, "%sINIT passthru = %RTbool\n", pszPrefix, pDte->n.u1InitPassthru);
5877 pHlp->pfnPrintf(pHlp, "%sExtInt passthru = %RTbool\n", pszPrefix, pDte->n.u1ExtIntPassthru);
5878 pHlp->pfnPrintf(pHlp, "%sNMI passthru = %RTbool\n", pszPrefix, pDte->n.u1NmiPassthru);
5879 pHlp->pfnPrintf(pHlp, "%sReserved (MBZ) = %#x\n", pszPrefix, pDte->n.u1Rsvd2);
5880 pHlp->pfnPrintf(pHlp, "%sInterrupt Control = %#x\n", pszPrefix, pDte->n.u2IntrCtrl);
5881 pHlp->pfnPrintf(pHlp, "%sLINT0 passthru = %RTbool\n", pszPrefix, pDte->n.u1Lint0Passthru);
5882 pHlp->pfnPrintf(pHlp, "%sLINT1 passthru = %RTbool\n", pszPrefix, pDte->n.u1Lint1Passthru);
5883 pHlp->pfnPrintf(pHlp, "%sReserved (MBZ) = %#x\n", pszPrefix, pDte->n.u32Rsvd0);
5884 pHlp->pfnPrintf(pHlp, "%sReserved (MBZ) = %#x\n", pszPrefix, pDte->n.u22Rsvd0);
5885 pHlp->pfnPrintf(pHlp, "%sAttribute Override Valid = %RTbool\n", pszPrefix, pDte->n.u1AttrOverride);
5886 pHlp->pfnPrintf(pHlp, "%sMode0FC = %#x\n", pszPrefix, pDte->n.u1Mode0FC);
5887 pHlp->pfnPrintf(pHlp, "%sSnoop Attribute = %#x\n", pszPrefix, pDte->n.u8SnoopAttr);
5888 pHlp->pfnPrintf(pHlp, "\n");
5889}
5890
5891
5892/**
5893 * @callback_method_impl{FNDBGFHANDLERDEV}
5894 */
5895static DECLCALLBACK(void) iommuAmdR3DbgInfoDte(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5896{
5897 if (pszArgs)
5898 {
5899 uint16_t uDevId = 0;
5900 int rc = RTStrToUInt16Full(pszArgs, 0 /* uBase */, &uDevId);
5901 if (RT_SUCCESS(rc))
5902 {
5903 DTE_T Dte;
5904 rc = iommuAmdDteRead(pDevIns, uDevId, IOMMUOP_TRANSLATE_REQ, &Dte);
5905 if (RT_SUCCESS(rc))
5906 {
5907 pHlp->pfnPrintf(pHlp, "DTE for device %#x\n", uDevId);
5908 iommuAmdR3DbgInfoDteWorker(pHlp, &Dte, " ");
5909 return;
5910 }
5911 pHlp->pfnPrintf(pHlp, "Failed to read DTE for device ID %u (%#x). rc=%Rrc\n", uDevId, uDevId, rc);
5912 }
5913 else
5914 pHlp->pfnPrintf(pHlp, "Failed to parse a valid 16-bit device ID. rc=%Rrc\n", rc);
5915 }
5916 else
5917 pHlp->pfnPrintf(pHlp, "Missing device ID.\n");
5918}
5919
5920
5921# ifdef IOMMU_WITH_DTE_CACHE
5922/**
5923 * @callback_method_impl{FNDBGFHANDLERDEV}
5924 */
5925static DECLCALLBACK(void) iommuAmdR3DbgInfoDteCache(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5926{
5927 RT_NOREF(pszArgs);
5928 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
5929 IOMMU_LOCK_CACHE_NORET(pDevIns, pThis);
5930
5931 uint16_t const cDteCache = RT_ELEMENTS(pThis->aDeviceIds);
5932 pHlp->pfnPrintf(pHlp, "DTE Cache: Capacity=%u entries\n", cDteCache);
5933 for (uint16_t i = 0; i < cDteCache; i++)
5934 {
5935 uint16_t const uDeviceId = pThis->aDeviceIds[i];
5936 if (uDeviceId)
5937 {
5938 pHlp->pfnPrintf(pHlp, " Entry[%u]: Device=%#x (BDF %02x:%02x.%d)\n", i, uDeviceId,
5939 (uDeviceId >> VBOX_PCI_BUS_SHIFT) & VBOX_PCI_BUS_MASK,
5940 (uDeviceId >> VBOX_PCI_DEVFN_DEV_SHIFT) & VBOX_PCI_DEVFN_DEV_MASK,
5941 uDeviceId & VBOX_PCI_DEVFN_FUN_MASK);
5942
5943 PCDTECACHE pDteCache = &pThis->aDteCache[i];
5944 pHlp->pfnPrintf(pHlp, " Flags = %#x\n", pDteCache->fFlags);
5945 pHlp->pfnPrintf(pHlp, " Domain Id = %u\n", pDteCache->uDomainId);
5946 pHlp->pfnPrintf(pHlp, "\n");
5947 }
5948 }
5949 IOMMU_UNLOCK_CACHE(pDevIns, pThis);
5950}
5951# endif /* IOMMU_WITH_DTE_CACHE */
5952
5953
5954# ifdef IOMMU_WITH_IOTLBE_CACHE
5955/**
5956 * @callback_method_impl{FNDBGFHANDLERDEV}
5957 */
5958static DECLCALLBACK(void) iommuAmdR3DbgInfoIotlb(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
5959{
5960 if (pszArgs)
5961 {
5962 uint16_t uDomainId = 0;
5963 int rc = RTStrToUInt16Full(pszArgs, 0 /* uBase */, &uDomainId);
5964 if (RT_SUCCESS(rc))
5965 {
5966 pHlp->pfnPrintf(pHlp, "IOTLBEs for domain %u (%#x):\n", uDomainId, uDomainId);
5967 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
5968 PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
5969 IOTLBEINFOARG Args;
5970 Args.pIommuR3 = pThisR3;
5971 Args.pHlp = pHlp;
5972 Args.uDomainId = uDomainId;
5973
5974 IOMMU_LOCK_CACHE_NORET(pDevIns, pThis);
5975 RTAvlU64DoWithAll(&pThisR3->TreeIotlbe, true /* fFromLeft */, iommuAmdR3IotlbEntryInfo, &Args);
5976 IOMMU_UNLOCK_CACHE(pDevIns, pThis);
5977 }
5978 else
5979 pHlp->pfnPrintf(pHlp, "Failed to parse a valid 16-bit domain ID. rc=%Rrc\n", rc);
5980 }
5981 else
5982 pHlp->pfnPrintf(pHlp, "Missing domain ID.\n");
5983}
5984# endif /* IOMMU_WITH_IOTLBE_CACHE */
5985
5986
5987# ifdef IOMMU_WITH_IRTE_CACHE
5988/**
5989 * Gets the interrupt type name for an interrupt type in the IRTE.
5990 *
5991 * @returns The interrupt type name.
5992 * @param uIntrType The interrupt type (as specified in the IRTE).
5993 */
5994static const char *iommuAmdIrteGetIntrTypeName(uint8_t uIntrType)
5995{
5996 switch (uIntrType)
5997 {
5998 case VBOX_MSI_DELIVERY_MODE_FIXED: return "Fixed";
5999 case VBOX_MSI_DELIVERY_MODE_LOWEST_PRIO: return "Arbitrated";
6000 default: return "<Reserved>";
6001 }
6002}
6003
6004/**
6005 * @callback_method_impl{FNDBGFHANDLERDEV}
6006 */
6007static DECLCALLBACK(void) iommuAmdR3DbgInfoIrteCache(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
6008{
6009 RT_NOREF(pszArgs);
6010
6011 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
6012 IOMMU_LOCK_CACHE_NORET(pDevIns, pThis);
6013
6014 uint16_t const cIrteCache = RT_ELEMENTS(pThis->aIrteCache);
6015 pHlp->pfnPrintf(pHlp, "IRTE Cache: Capacity=%u entries\n", cIrteCache);
6016 for (uint16_t idxIrte = 0; idxIrte < cIrteCache; idxIrte++)
6017 {
6018 PCIRTECACHE pIrteCache = &pThis->aIrteCache[idxIrte];
6019 uint32_t const uKey = pIrteCache->uKey;
6020 if (uKey != IOMMU_IRTE_CACHE_KEY_NIL)
6021 {
6022 uint16_t const uDeviceId = IOMMU_IRTE_CACHE_KEY_GET_DEVICE_ID(uKey);
6023 uint16_t const offIrte = IOMMU_IRTE_CACHE_KEY_GET_OFF(uKey);
6024 pHlp->pfnPrintf(pHlp, " Entry[%u]: Offset=%#x Device=%#x (BDF %02x:%02x.%d)\n",
6025 idxIrte, offIrte, uDeviceId,
6026 (uDeviceId >> VBOX_PCI_BUS_SHIFT) & VBOX_PCI_BUS_MASK,
6027 (uDeviceId >> VBOX_PCI_DEVFN_DEV_SHIFT) & VBOX_PCI_DEVFN_DEV_MASK,
6028 uDeviceId & VBOX_PCI_DEVFN_FUN_MASK);
6029
6030 PCIRTE_T pIrte = &pIrteCache->Irte;
6031 pHlp->pfnPrintf(pHlp, " Remap Enable = %RTbool\n", pIrte->n.u1RemapEnable);
6032 pHlp->pfnPrintf(pHlp, " Suppress IOPF = %RTbool\n", pIrte->n.u1SuppressIoPf);
6033 pHlp->pfnPrintf(pHlp, " Interrupt Type = %#x (%s)\n", pIrte->n.u3IntrType,
6034 iommuAmdIrteGetIntrTypeName(pIrte->n.u3IntrType));
6035 pHlp->pfnPrintf(pHlp, " Request EOI = %RTbool\n", pIrte->n.u1ReqEoi);
6036 pHlp->pfnPrintf(pHlp, " Destination mode = %s\n", pIrte->n.u1DestMode ? "Logical" : "Physical");
6037 pHlp->pfnPrintf(pHlp, " Destination Id = %u\n", pIrte->n.u8Dest);
6038 pHlp->pfnPrintf(pHlp, " Vector = %#x (%u)\n", pIrte->n.u8Vector, pIrte->n.u8Vector);
6039 pHlp->pfnPrintf(pHlp, "\n");
6040 }
6041 }
6042 IOMMU_UNLOCK_CACHE(pDevIns, pThis);
6043}
6044# endif /* IOMMU_WITH_IRTE_CACHE */
6045
6046
6047/**
6048 * @callback_method_impl{FNDBGFHANDLERDEV}
6049 */
6050static DECLCALLBACK(void) iommuAmdR3DbgInfoDevTabs(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
6051{
6052 RT_NOREF(pszArgs);
6053
6054 PCIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
6055 PCPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
6056 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
6057 NOREF(pPciDev);
6058
6059 uint8_t cSegments = 0;
6060 for (uint8_t i = 0; i < RT_ELEMENTS(pThis->aDevTabBaseAddrs); i++)
6061 {
6062 DEV_TAB_BAR_T const DevTabBar = pThis->aDevTabBaseAddrs[i];
6063 RTGCPHYS const GCPhysDevTab = DevTabBar.n.u40Base << X86_PAGE_4K_SHIFT;
6064 if (GCPhysDevTab)
6065 ++cSegments;
6066 }
6067
6068 pHlp->pfnPrintf(pHlp, "AMD-IOMMU device tables with address translations enabled:\n");
6069 pHlp->pfnPrintf(pHlp, " DTE Segments=%u\n", cSegments);
6070 if (!cSegments)
6071 return;
6072
6073 for (uint8_t i = 0; i < RT_ELEMENTS(pThis->aDevTabBaseAddrs); i++)
6074 {
6075 DEV_TAB_BAR_T const DevTabBar = pThis->aDevTabBaseAddrs[i];
6076 RTGCPHYS const GCPhysDevTab = DevTabBar.n.u40Base << X86_PAGE_4K_SHIFT;
6077 if (GCPhysDevTab)
6078 {
6079 uint32_t const cbDevTab = IOMMU_GET_DEV_TAB_LEN(&DevTabBar);
6080 uint32_t const cDtes = cbDevTab / sizeof(DTE_T);
6081
6082 void *pvDevTab = RTMemAllocZ(cbDevTab);
6083 if (RT_LIKELY(pvDevTab))
6084 {
6085 int rc = PDMDevHlpPCIPhysRead(pDevIns, GCPhysDevTab, pvDevTab, cbDevTab);
6086 if (RT_SUCCESS(rc))
6087 {
6088 for (uint32_t idxDte = 0; idxDte < cDtes; idxDte++)
6089 {
6090 PCDTE_T pDte = (PCDTE_T)((uintptr_t)pvDevTab + idxDte * sizeof(DTE_T));
6091 if ( pDte->n.u1Valid
6092 && pDte->n.u1TranslationValid
6093 && pDte->n.u3Mode != 0)
6094 {
6095 pHlp->pfnPrintf(pHlp, " DTE %u (BDF %02x:%02x.%d)\n", idxDte,
6096 (idxDte >> VBOX_PCI_BUS_SHIFT) & VBOX_PCI_BUS_MASK,
6097 (idxDte >> VBOX_PCI_DEVFN_DEV_SHIFT) & VBOX_PCI_DEVFN_DEV_MASK,
6098 idxDte & VBOX_PCI_DEVFN_FUN_MASK);
6099 iommuAmdR3DbgInfoDteWorker(pHlp, pDte, " ");
6100 pHlp->pfnPrintf(pHlp, "\n");
6101 }
6102 }
6103 pHlp->pfnPrintf(pHlp, "\n");
6104 }
6105 else
6106 {
6107 pHlp->pfnPrintf(pHlp, " Failed to read table at %#RGp of size %zu bytes. rc=%Rrc!\n", GCPhysDevTab,
6108 cbDevTab, rc);
6109 }
6110
6111 RTMemFree(pvDevTab);
6112 }
6113 else
6114 {
6115 pHlp->pfnPrintf(pHlp, " Allocating %zu bytes for reading the device table failed!\n", cbDevTab);
6116 return;
6117 }
6118 }
6119 }
6120}
6121
6122
6123/**
6124 * @callback_method_impl{FNSSMDEVLIVEEXEC}
6125 */
6126static DECLCALLBACK(int) iommuAmdR3LiveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uPass)
6127{
6128 PCIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
6129 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
6130 RT_NOREF(uPass);
6131 LogFlowFunc(("\n"));
6132
6133 /* Save registers that cannot be modified by the guest. */
6134 pHlp->pfnSSMPutU64(pSSM, pThis->ExtFeat.u64);
6135 pHlp->pfnSSMPutU64(pSSM, pThis->DevSpecificFeat.u64);
6136 pHlp->pfnSSMPutU64(pSSM, pThis->DevSpecificCtrl.u64);
6137 pHlp->pfnSSMPutU64(pSSM, pThis->DevSpecificStatus.u64);
6138 pHlp->pfnSSMPutU64(pSSM, pThis->MiscInfo.u64);
6139 pHlp->pfnSSMPutU64(pSSM, pThis->RsvdReg);
6140
6141 return VINF_SSM_DONT_CALL_AGAIN;
6142}
6143
6144
6145/**
6146 * @callback_method_impl{FNSSMDEVSAVEEXEC}
6147 */
6148static DECLCALLBACK(int) iommuAmdR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
6149{
6150 PCIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
6151 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
6152 LogFlowFunc(("\n"));
6153
6154 pHlp->pfnSSMPutU64(pSSM, pThis->IommuBar.u64);
6155
6156 uint8_t const cDevTabBaseAddrs = RT_ELEMENTS(pThis->aDevTabBaseAddrs);
6157 pHlp->pfnSSMPutU8(pSSM, cDevTabBaseAddrs);
6158 for (uint8_t i = 0; i < cDevTabBaseAddrs; i++)
6159 pHlp->pfnSSMPutU64(pSSM, pThis->aDevTabBaseAddrs[i].u64);
6160 pHlp->pfnSSMPutU64(pSSM, pThis->CmdBufBaseAddr.u64);
6161 pHlp->pfnSSMPutU64(pSSM, pThis->EvtLogBaseAddr.u64);
6162 pHlp->pfnSSMPutU64(pSSM, pThis->Ctrl.u64);
6163 pHlp->pfnSSMPutU64(pSSM, pThis->ExclRangeBaseAddr.u64);
6164 pHlp->pfnSSMPutU64(pSSM, pThis->ExclRangeLimit.u64);
6165#if 0
6166 pHlp->pfnSSMPutU64(pSSM, pThis->ExtFeat.u64); /* read-only, done in liveExec */
6167#endif
6168
6169 pHlp->pfnSSMPutU64(pSSM, pThis->PprLogBaseAddr.u64);
6170 pHlp->pfnSSMPutU64(pSSM, pThis->HwEvtHi.u64);
6171 pHlp->pfnSSMPutU64(pSSM, pThis->HwEvtLo);
6172 pHlp->pfnSSMPutU64(pSSM, pThis->HwEvtStatus.u64);
6173
6174 pHlp->pfnSSMPutU64(pSSM, pThis->GALogBaseAddr.u64);
6175 pHlp->pfnSSMPutU64(pSSM, pThis->GALogTailAddr.u64);
6176
6177 pHlp->pfnSSMPutU64(pSSM, pThis->PprLogBBaseAddr.u64);
6178 pHlp->pfnSSMPutU64(pSSM, pThis->EvtLogBBaseAddr.u64);
6179
6180#if 0
6181 pHlp->pfnSSMPutU64(pSSM, pThis->DevSpecificFeat.u64); /* read-only, done in liveExec */
6182 pHlp->pfnSSMPutU64(pSSM, pThis->DevSpecificCtrl.u64); /* read-only, done in liveExec */
6183 pHlp->pfnSSMPutU64(pSSM, pThis->DevSpecificStatus.u64); /* read-only, done in liveExec */
6184
6185 pHlp->pfnSSMPutU64(pSSM, pThis->MiscInfo.u64); /* read-only, done in liveExec */
6186#endif
6187 pHlp->pfnSSMPutU32(pSSM, pThis->PerfOptCtrl.u32);
6188
6189 pHlp->pfnSSMPutU64(pSSM, pThis->XtGenIntrCtrl.u64);
6190 pHlp->pfnSSMPutU64(pSSM, pThis->XtPprIntrCtrl.u64);
6191 pHlp->pfnSSMPutU64(pSSM, pThis->XtGALogIntrCtrl.u64);
6192
6193 size_t const cMarcApers = RT_ELEMENTS(pThis->aMarcApers);
6194 pHlp->pfnSSMPutU8(pSSM, cMarcApers);
6195 for (size_t i = 0; i < cMarcApers; i++)
6196 {
6197 pHlp->pfnSSMPutU64(pSSM, pThis->aMarcApers[i].Base.u64);
6198 pHlp->pfnSSMPutU64(pSSM, pThis->aMarcApers[i].Reloc.u64);
6199 pHlp->pfnSSMPutU64(pSSM, pThis->aMarcApers[i].Length.u64);
6200 }
6201
6202#if 0
6203 pHlp->pfnSSMPutU64(pSSM, pThis->RsvdReg); /* read-only, done in liveExec */
6204#endif
6205
6206 pHlp->pfnSSMPutU64(pSSM, pThis->CmdBufHeadPtr.u64);
6207 pHlp->pfnSSMPutU64(pSSM, pThis->CmdBufTailPtr.u64);
6208 pHlp->pfnSSMPutU64(pSSM, pThis->EvtLogHeadPtr.u64);
6209 pHlp->pfnSSMPutU64(pSSM, pThis->EvtLogTailPtr.u64);
6210
6211 pHlp->pfnSSMPutU64(pSSM, pThis->Status.u64);
6212
6213 pHlp->pfnSSMPutU64(pSSM, pThis->PprLogHeadPtr.u64);
6214 pHlp->pfnSSMPutU64(pSSM, pThis->PprLogTailPtr.u64);
6215
6216 pHlp->pfnSSMPutU64(pSSM, pThis->GALogHeadPtr.u64);
6217 pHlp->pfnSSMPutU64(pSSM, pThis->GALogTailPtr.u64);
6218
6219 pHlp->pfnSSMPutU64(pSSM, pThis->PprLogBHeadPtr.u64);
6220 pHlp->pfnSSMPutU64(pSSM, pThis->PprLogBTailPtr.u64);
6221
6222 pHlp->pfnSSMPutU64(pSSM, pThis->EvtLogBHeadPtr.u64);
6223 pHlp->pfnSSMPutU64(pSSM, pThis->EvtLogBTailPtr.u64);
6224
6225 pHlp->pfnSSMPutU64(pSSM, pThis->PprLogAutoResp.u64);
6226 pHlp->pfnSSMPutU64(pSSM, pThis->PprLogOverflowEarly.u64);
6227 pHlp->pfnSSMPutU64(pSSM, pThis->PprLogBOverflowEarly.u64);
6228
6229 return pHlp->pfnSSMPutU32(pSSM, UINT32_MAX);
6230}
6231
6232
6233/**
6234 * @callback_method_impl{FNSSMDEVLOADEXEC}
6235 */
6236static DECLCALLBACK(int) iommuAmdR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
6237{
6238 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
6239 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
6240 LogFlowFunc(("\n"));
6241
6242 /* Validate. */
6243 if (uPass != SSM_PASS_FINAL)
6244 return VINF_SUCCESS;
6245 if (uVersion != IOMMU_SAVED_STATE_VERSION)
6246 return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
6247 int const rcDataError = VERR_SSM_UNEXPECTED_DATA;
6248
6249 int rc = pHlp->pfnSSMGetU64(pSSM, &pThis->ExtFeat.u64);
6250 AssertRCReturn(rc, rc);
6251 AssertLogRelMsgReturn(pThis->ExtFeat.n.u2HostAddrTranslateSize < 0x3,
6252 ("ExtFeat invalid %#RX64\n", pThis->ExtFeat.u64), rcDataError);
6253
6254 pHlp->pfnSSMGetU64(pSSM, &pThis->DevSpecificFeat.u64);
6255 pHlp->pfnSSMGetU64(pSSM, &pThis->DevSpecificCtrl.u64);
6256 pHlp->pfnSSMGetU64(pSSM, &pThis->DevSpecificStatus.u64);
6257 pHlp->pfnSSMGetU64(pSSM, &pThis->MiscInfo.u64);
6258 pHlp->pfnSSMGetU64(pSSM, &pThis->RsvdReg);
6259
6260 /* Device table base address registers. */
6261 uint8_t cDevTabBaseAddrs;
6262 rc = pHlp->pfnSSMGetU8(pSSM, &cDevTabBaseAddrs);
6263 AssertRCReturn(rc, rc);
6264 AssertLogRelMsgReturn(cDevTabBaseAddrs <= RT_ELEMENTS(pThis->aDevTabBaseAddrs),
6265 ("Device table segment count invalid %#x\n", cDevTabBaseAddrs), rcDataError);
6266 for (uint8_t i = 0; i < cDevTabBaseAddrs; i++)
6267 {
6268 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->aDevTabBaseAddrs[i].u64);
6269 AssertRCReturn(rc, rc);
6270 pThis->aDevTabBaseAddrs[i].u64 &= IOMMU_DEV_TAB_BAR_VALID_MASK;
6271 AssertLogRelMsgReturn(pThis->aDevTabBaseAddrs[i].n.u9Size <= g_auDevTabSegMaxSizes[0],
6272 ("Device table segment size invalid %#x\n", pThis->aDevTabBaseAddrs[i].n.u9Size), rcDataError);
6273 }
6274
6275 /* Command buffer base address register. */
6276 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->CmdBufBaseAddr.u64);
6277 AssertRCReturn(rc, rc);
6278 pThis->CmdBufBaseAddr.u64 &= IOMMU_CMD_BUF_BAR_VALID_MASK;
6279 AssertLogRelMsgReturn(pThis->CmdBufBaseAddr.n.u4Len >= 8,
6280 ("Command buffer base address invalid %#RX64\n", pThis->CmdBufBaseAddr.u64), rcDataError);
6281
6282 /* Event log base address register. */
6283 pHlp->pfnSSMPutU64(pSSM, pThis->EvtLogBaseAddr.u64);
6284 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->EvtLogBaseAddr.u64);
6285 AssertRCReturn(rc, rc);
6286 pThis->EvtLogBaseAddr.u64 &= IOMMU_EVT_LOG_BAR_VALID_MASK;
6287 AssertLogRelMsgReturn(pThis->EvtLogBaseAddr.n.u4Len >= 8,
6288 ("Event log base address invalid %#RX64\n", pThis->EvtLogBaseAddr.u64), rcDataError);
6289
6290 /* Control register. */
6291 rc = pHlp->pfnSSMPutU64(pSSM, pThis->Ctrl.u64);
6292 AssertRCReturn(rc, rc);
6293 pThis->Ctrl.u64 &= IOMMU_CTRL_VALID_MASK;
6294 AssertLogRelMsgReturn(pThis->Ctrl.n.u3DevTabSegEn <= pThis->ExtFeat.n.u2DevTabSegSup,
6295 ("Control register invalid %#RX64\n", pThis->Ctrl.u64), rcDataError);
6296
6297 /* Exclusion range base address register. */
6298 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->ExclRangeBaseAddr.u64);
6299 AssertRCReturn(rc, rc);
6300 pThis->ExclRangeBaseAddr.u64 &= IOMMU_EXCL_RANGE_BAR_VALID_MASK;
6301
6302 /* Exclusion range limit register. */
6303 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->ExclRangeLimit.u64);
6304 AssertRCReturn(rc, rc);
6305 pThis->ExclRangeLimit.u64 &= IOMMU_EXCL_RANGE_LIMIT_VALID_MASK;
6306 pThis->ExclRangeLimit.u64 |= UINT64_C(0xfff);
6307
6308#if 0
6309 pHlp->pfnSSMGetU64(pSSM, &pThis->ExtFeat.u64); /* read-only, done already (above). */
6310#endif
6311
6312 /* PPR log base address register. */
6313 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogBaseAddr.u64);
6314 AssertRCReturn(rc, rc);
6315 Assert(!pThis->ExtFeat.n.u1PprSup);
6316
6317 /* Hardware event (Hi) register. */
6318 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->HwEvtHi.u64);
6319 AssertRCReturn(rc, rc);
6320
6321 /* Hardware event (Lo) register. */
6322 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->HwEvtLo);
6323 AssertRCReturn(rc, rc);
6324
6325 /* Hardware event status register. */
6326 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->HwEvtStatus.u64);
6327 AssertRCReturn(rc, rc);
6328 pThis->HwEvtStatus.u64 &= IOMMU_HW_EVT_STATUS_VALID_MASK;
6329
6330 /* Guest Virtual-APIC log base address register. */
6331 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->GALogBaseAddr.u64);
6332 AssertRCReturn(rc, rc);
6333 Assert(!pThis->ExtFeat.n.u1GstVirtApicSup);
6334
6335 /* Guest Virtual-APIC log tail address register. */
6336 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->GALogTailAddr.u64);
6337 AssertRCReturn(rc, rc);
6338 Assert(!pThis->ExtFeat.n.u1GstVirtApicSup);
6339
6340 /* PPR log-B base address register. */
6341 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogBBaseAddr.u64);
6342 AssertRCReturn(rc, rc);
6343 Assert(!pThis->ExtFeat.n.u1PprSup);
6344
6345 /* Event log-B base address register. */
6346 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->EvtLogBBaseAddr.u64);
6347 AssertRCReturn(rc, rc);
6348 Assert(!pThis->ExtFeat.n.u2DualPprLogSup);
6349
6350#if 0
6351 pHlp->pfnSSMGetU64(pSSM, &pThis->DevSpecificFeat.u64); /* read-only, done already (above). */
6352 pHlp->pfnSSMGetU64(pSSM, &pThis->DevSpecificCtrl.u64); /* read-only, done already (above). */
6353 pHlp->pfnSSMGetU64(pSSM, &pThis->DevSpecificStatus.u64); /* read-only, done already (above). */
6354
6355 pHlp->pfnSSMGetU64(pSSM, &pThis->MiscInfo.u64); /* read-only, done already (above). */
6356#endif
6357
6358 /* Performance optimization control register. */
6359 rc = pHlp->pfnSSMGetU32(pSSM, &pThis->PerfOptCtrl.u32);
6360 AssertRCReturn(rc, rc);
6361 Assert(!pThis->ExtFeat.n.u1PerfOptSup);
6362
6363 /* x2APIC registers. */
6364 {
6365 Assert(!pThis->ExtFeat.n.u1X2ApicSup);
6366
6367 /* x2APIC general interrupt control register. */
6368 pHlp->pfnSSMGetU64(pSSM, &pThis->XtGenIntrCtrl.u64);
6369 AssertRCReturn(rc, rc);
6370
6371 /* x2APIC PPR interrupt control register. */
6372 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->XtPprIntrCtrl.u64);
6373 AssertRCReturn(rc, rc);
6374
6375 /* x2APIC GA log interrupt control register. */
6376 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->XtGALogIntrCtrl.u64);
6377 AssertRCReturn(rc, rc);
6378 }
6379
6380 /* MARC (Memory access and routing) registers. */
6381 {
6382 uint8_t cMarcApers;
6383 rc = pHlp->pfnSSMGetU8(pSSM, &cMarcApers);
6384 AssertRCReturn(rc, rc);
6385 AssertLogRelMsgReturn(cMarcApers <= RT_ELEMENTS(pThis->aMarcApers),
6386 ("MARC register count invalid %#x\n", cMarcApers), rcDataError);
6387 for (uint8_t i = 0; i < cMarcApers; i++)
6388 {
6389 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->aMarcApers[i].Base.u64);
6390 AssertRCReturn(rc, rc);
6391
6392 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->aMarcApers[i].Reloc.u64);
6393 AssertRCReturn(rc, rc);
6394
6395 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->aMarcApers[i].Length.u64);
6396 AssertRCReturn(rc, rc);
6397 }
6398 Assert(!pThis->ExtFeat.n.u2MarcSup);
6399 }
6400
6401#if 0
6402 pHlp->pfnSSMGetU64(pSSM, &pThis->RsvdReg); /* read-only, done already (above). */
6403#endif
6404
6405 /* Command buffer head pointer register. */
6406 {
6407 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->CmdBufHeadPtr.u64);
6408 AssertRCReturn(rc, rc);
6409
6410 /*
6411 * IOMMU behavior is undefined when software writes a value outside the buffer length.
6412 * In our emulation, since we ignore the write entirely (see iommuAmdCmdBufHeadPtr_w)
6413 * we shouldn't see such values in the saved state.
6414 */
6415 uint32_t const offBuf = pThis->CmdBufHeadPtr.u64 & IOMMU_CMD_BUF_HEAD_PTR_VALID_MASK;
6416 uint32_t const cbBuf = iommuAmdGetTotalBufLength(pThis->CmdBufBaseAddr.n.u4Len);
6417 Assert(cbBuf <= _512K);
6418 AssertLogRelMsgReturn(offBuf < cbBuf,
6419 ("Command buffer head pointer invalid %#x\n", pThis->CmdBufHeadPtr.u64), rcDataError);
6420 }
6421
6422 /* Command buffer tail pointer register. */
6423 {
6424 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->CmdBufTailPtr.u64);
6425 AssertRCReturn(rc, rc);
6426
6427 uint32_t const offBuf = pThis->CmdBufTailPtr.u64 & IOMMU_CMD_BUF_TAIL_PTR_VALID_MASK;
6428 uint32_t const cbBuf = iommuAmdGetTotalBufLength(pThis->CmdBufBaseAddr.n.u4Len);
6429 Assert(cbBuf <= _512K);
6430 AssertLogRelMsgReturn(offBuf < cbBuf,
6431 ("Command buffer tail pointer invalid %#x\n", pThis->CmdBufTailPtr.u64), rcDataError);
6432 }
6433
6434 /* Event log head pointer register. */
6435 {
6436 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->EvtLogHeadPtr.u64);
6437 AssertRCReturn(rc, rc);
6438
6439 uint32_t const offBuf = pThis->EvtLogHeadPtr.u64 & IOMMU_EVT_LOG_HEAD_PTR_VALID_MASK;
6440 uint32_t const cbBuf = iommuAmdGetTotalBufLength(pThis->EvtLogBaseAddr.n.u4Len);
6441 Assert(cbBuf <= _512K);
6442 AssertLogRelMsgReturn(offBuf < cbBuf,
6443 ("Event log head pointer invalid %#x\n", pThis->EvtLogHeadPtr.u64), rcDataError);
6444 }
6445
6446 /* Event log tail pointer register. */
6447 {
6448 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->EvtLogTailPtr.u64);
6449 AssertRCReturn(rc, rc);
6450
6451 uint32_t const offBuf = pThis->EvtLogTailPtr.u64 & IOMMU_EVT_LOG_TAIL_PTR_VALID_MASK;
6452 uint32_t const cbBuf = iommuAmdGetTotalBufLength(pThis->EvtLogBaseAddr.n.u4Len);
6453 Assert(cbBuf <= _512K);
6454 AssertLogRelMsgReturn(offBuf < cbBuf,
6455 ("Event log tail pointer invalid %#x\n", pThis->EvtLogTailPtr.u64), rcDataError);
6456 }
6457
6458 /* Status register. */
6459 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->Status.u64);
6460 AssertRCReturn(rc, rc);
6461 pThis->Status.u64 &= IOMMU_STATUS_VALID_MASK;
6462
6463 /* PPR log head pointer register. */
6464 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogHeadPtr.u64);
6465 AssertRCReturn(rc, rc);
6466 Assert(!pThis->ExtFeat.n.u1PprSup);
6467
6468 /* PPR log tail pointer register. */
6469 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogTailPtr.u64);
6470 AssertRCReturn(rc, rc);
6471 Assert(!pThis->ExtFeat.n.u1PprSup);
6472
6473 /* Guest Virtual-APIC log head pointer register. */
6474 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->GALogHeadPtr.u64);
6475 AssertRCReturn(rc, rc);
6476 Assert(!pThis->ExtFeat.n.u1GstVirtApicSup);
6477
6478 /* Guest Virtual-APIC log tail pointer register. */
6479 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->GALogTailPtr.u64);
6480 AssertRCReturn(rc, rc);
6481 Assert(!pThis->ExtFeat.n.u1GstVirtApicSup);
6482
6483 /* PPR log-B head pointer register. */
6484 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogBHeadPtr.u64);
6485 AssertRCReturn(rc, rc);
6486 Assert(!pThis->ExtFeat.n.u1PprSup);
6487
6488 /* PPR log-B head pointer register. */
6489 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogBTailPtr.u64);
6490 AssertRCReturn(rc, rc);
6491 Assert(!pThis->ExtFeat.n.u1PprSup);
6492
6493 /* Event log-B head pointer register. */
6494 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->EvtLogBHeadPtr.u64);
6495 AssertRCReturn(rc, rc);
6496 Assert(!pThis->ExtFeat.n.u2DualEvtLogSup);
6497
6498 /* Event log-B tail pointer register. */
6499 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->EvtLogBTailPtr.u64);
6500 AssertRCReturn(rc, rc);
6501 Assert(!pThis->ExtFeat.n.u2DualEvtLogSup);
6502
6503 /* PPR log auto response register. */
6504 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogAutoResp.u64);
6505 AssertRCReturn(rc, rc);
6506 Assert(!pThis->ExtFeat.n.u1PprAutoRespSup);
6507
6508 /* PPR log overflow early indicator register. */
6509 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogOverflowEarly.u64);
6510 AssertRCReturn(rc, rc);
6511 Assert(!pThis->ExtFeat.n.u1PprLogOverflowWarn);
6512
6513 /* PPR log-B overflow early indicator register. */
6514 rc = pHlp->pfnSSMGetU64(pSSM, &pThis->PprLogBOverflowEarly.u64);
6515 AssertRCReturn(rc, rc);
6516 Assert(!pThis->ExtFeat.n.u1PprLogOverflowWarn);
6517
6518 /* End marker. */
6519 {
6520 uint32_t uEndMarker;
6521 rc = pHlp->pfnSSMGetU32(pSSM, &uEndMarker);
6522 AssertLogRelMsgRCReturn(rc, ("Failed to read end marker. rc=%Rrc\n", rc), VERR_SSM_DATA_UNIT_FORMAT_CHANGED);
6523 AssertLogRelMsgReturn(uEndMarker == UINT32_MAX, ("End marker invalid (%#x expected %#x)\n", uEndMarker, UINT32_MAX),
6524 rcDataError);
6525 }
6526
6527 /** @todo Kick the command thread, anything else to do on restore? */
6528
6529 return VERR_NOT_IMPLEMENTED;
6530}
6531
6532
6533/**
6534 * @interface_method_impl{PDMDEVREG,pfnReset}
6535 */
6536static DECLCALLBACK(void) iommuAmdR3Reset(PPDMDEVINS pDevIns)
6537{
6538 /*
6539 * Resets read-write portion of the IOMMU state.
6540 *
6541 * NOTE! State not initialized here is expected to be initialized during
6542 * device construction and remain read-only through the lifetime of the VM.
6543 */
6544 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
6545 PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
6546 PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
6547 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
6548 LogFlowFunc(("\n"));
6549
6550 IOMMU_LOCK_NORET(pDevIns, pThisR3);
6551
6552 RT_ZERO(pThis->aDevTabBaseAddrs);
6553
6554 pThis->CmdBufBaseAddr.u64 = 0;
6555 pThis->CmdBufBaseAddr.n.u4Len = 8;
6556
6557 pThis->EvtLogBaseAddr.u64 = 0;
6558 pThis->EvtLogBaseAddr.n.u4Len = 8;
6559
6560 pThis->Ctrl.u64 = 0;
6561 pThis->Ctrl.n.u1Coherent = 1;
6562 Assert(!pThis->ExtFeat.n.u1BlockStopMarkSup);
6563
6564 pThis->ExclRangeBaseAddr.u64 = 0;
6565 pThis->ExclRangeLimit.u64 = 0;
6566
6567 pThis->PprLogBaseAddr.u64 = 0;
6568 pThis->PprLogBaseAddr.n.u4Len = 8;
6569
6570 pThis->HwEvtHi.u64 = 0;
6571 pThis->HwEvtLo = 0;
6572 pThis->HwEvtStatus.u64 = 0;
6573
6574 pThis->GALogBaseAddr.u64 = 0;
6575 pThis->GALogBaseAddr.n.u4Len = 8;
6576 pThis->GALogTailAddr.u64 = 0;
6577
6578 pThis->PprLogBBaseAddr.u64 = 0;
6579 pThis->PprLogBBaseAddr.n.u4Len = 8;
6580
6581 pThis->EvtLogBBaseAddr.u64 = 0;
6582 pThis->EvtLogBBaseAddr.n.u4Len = 8;
6583
6584 pThis->PerfOptCtrl.u32 = 0;
6585
6586 pThis->XtGenIntrCtrl.u64 = 0;
6587 pThis->XtPprIntrCtrl.u64 = 0;
6588 pThis->XtGALogIntrCtrl.u64 = 0;
6589
6590 RT_ZERO(pThis->aMarcApers);
6591
6592 pThis->CmdBufHeadPtr.u64 = 0;
6593 pThis->CmdBufTailPtr.u64 = 0;
6594 pThis->EvtLogHeadPtr.u64 = 0;
6595 pThis->EvtLogTailPtr.u64 = 0;
6596
6597 pThis->Status.u64 = 0;
6598
6599 pThis->PprLogHeadPtr.u64 = 0;
6600 pThis->PprLogTailPtr.u64 = 0;
6601
6602 pThis->GALogHeadPtr.u64 = 0;
6603 pThis->GALogTailPtr.u64 = 0;
6604
6605 pThis->PprLogBHeadPtr.u64 = 0;
6606 pThis->PprLogBTailPtr.u64 = 0;
6607
6608 pThis->EvtLogBHeadPtr.u64 = 0;
6609 pThis->EvtLogBTailPtr.u64 = 0;
6610
6611 pThis->PprLogAutoResp.u64 = 0;
6612 pThis->PprLogOverflowEarly.u64 = 0;
6613 pThis->PprLogBOverflowEarly.u64 = 0;
6614
6615 pThis->IommuBar.u64 = 0;
6616 PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_BASE_ADDR_REG_LO, 0);
6617 PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_BASE_ADDR_REG_HI, 0);
6618
6619 PDMPciDevSetCommand(pPciDev, VBOX_PCI_COMMAND_MASTER);
6620
6621 IOMMU_UNLOCK(pDevIns, pThisR3);
6622
6623#ifdef IOMMU_WITH_DTE_CACHE
6624 iommuAmdDteCacheRemoveAll(pDevIns);
6625#endif
6626#ifdef IOMMU_WITH_IOTLBE_CACHE
6627 iommuAmdIotlbRemoveAll(pDevIns);
6628#endif
6629#ifdef IOMMU_WITH_IRTE_CACHE
6630 iommuAmdIrteCacheRemoveAll(pDevIns);
6631#endif
6632}
6633
6634
6635/**
6636 * @interface_method_impl{PDMDEVREG,pfnDestruct}
6637 */
6638static DECLCALLBACK(int) iommuAmdR3Destruct(PPDMDEVINS pDevIns)
6639{
6640 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
6641 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
6642 PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
6643 LogFlowFunc(("\n"));
6644
6645 IOMMU_LOCK_NORET(pDevIns, pThisR3);
6646
6647 /* Close the command thread semaphore. */
6648 if (pThis->hEvtCmdThread != NIL_SUPSEMEVENT)
6649 {
6650 PDMDevHlpSUPSemEventClose(pDevIns, pThis->hEvtCmdThread);
6651 pThis->hEvtCmdThread = NIL_SUPSEMEVENT;
6652 }
6653
6654#ifdef IOMMU_WITH_IOTLBE_CACHE
6655 /* Destroy the IOTLB cache. */
6656 if (pThisR3->paIotlbes)
6657 {
6658 PDMDevHlpMMHeapFree(pDevIns, pThisR3->paIotlbes);
6659 pThisR3->paIotlbes = NULL;
6660 pThisR3->idxUnusedIotlbe = 0;
6661 }
6662#endif
6663
6664 IOMMU_UNLOCK(pDevIns, pThisR3);
6665 return VINF_SUCCESS;
6666}
6667
6668
6669/**
6670 * @interface_method_impl{PDMDEVREG,pfnConstruct}
6671 */
6672static DECLCALLBACK(int) iommuAmdR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
6673{
6674 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
6675 RT_NOREF(pCfg);
6676
6677 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
6678 PIOMMUR3 pThisR3 = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUR3);
6679 pThis->u32Magic = IOMMU_MAGIC;
6680 pThisR3->pDevInsR3 = pDevIns;
6681
6682 LogFlowFunc(("iInstance=%d\n", iInstance));
6683
6684 /*
6685 * Register the IOMMU with PDM.
6686 */
6687 PDMIOMMUREGR3 IommuReg;
6688 RT_ZERO(IommuReg);
6689 IommuReg.u32Version = PDM_IOMMUREGCC_VERSION;
6690 IommuReg.pfnMemAccess = iommuAmdMemAccess;
6691 IommuReg.pfnMemBulkAccess = iommuAmdMemBulkAccess;
6692 IommuReg.pfnMsiRemap = iommuAmdMsiRemap;
6693 IommuReg.u32TheEnd = PDM_IOMMUREGCC_VERSION;
6694 int rc = PDMDevHlpIommuRegister(pDevIns, &IommuReg, &pThisR3->CTX_SUFF(pIommuHlp), &pThis->idxIommu);
6695 if (RT_FAILURE(rc))
6696 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to register ourselves as an IOMMU device"));
6697 if (pThisR3->CTX_SUFF(pIommuHlp)->u32Version != PDM_IOMMUHLPR3_VERSION)
6698 return PDMDevHlpVMSetError(pDevIns, VERR_VERSION_MISMATCH, RT_SRC_POS,
6699 N_("IOMMU helper version mismatch; got %#x expected %#x"),
6700 pThisR3->CTX_SUFF(pIommuHlp)->u32Version, PDM_IOMMUHLPR3_VERSION);
6701 if (pThisR3->CTX_SUFF(pIommuHlp)->u32TheEnd != PDM_IOMMUHLPR3_VERSION)
6702 return PDMDevHlpVMSetError(pDevIns, VERR_VERSION_MISMATCH, RT_SRC_POS,
6703 N_("IOMMU helper end-version mismatch; got %#x expected %#x"),
6704 pThisR3->CTX_SUFF(pIommuHlp)->u32TheEnd, PDM_IOMMUHLPR3_VERSION);
6705
6706 /*
6707 * We will use PDM's critical section (via helpers) for the IOMMU device.
6708 */
6709 rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
6710 AssertRCReturn(rc, rc);
6711
6712 /*
6713 * Initialize read-only PCI configuration space.
6714 */
6715 PPDMPCIDEV pPciDev = pDevIns->apPciDevs[0];
6716 PDMPCIDEV_ASSERT_VALID(pDevIns, pPciDev);
6717
6718 /* Header. */
6719 PDMPciDevSetVendorId(pPciDev, IOMMU_PCI_VENDOR_ID); /* AMD */
6720 PDMPciDevSetDeviceId(pPciDev, IOMMU_PCI_DEVICE_ID); /* VirtualBox IOMMU device */
6721 PDMPciDevSetCommand(pPciDev, VBOX_PCI_COMMAND_MASTER); /* Enable bus master (as we directly access main memory) */
6722 PDMPciDevSetStatus(pPciDev, VBOX_PCI_STATUS_CAP_LIST); /* Capability list supported */
6723 PDMPciDevSetRevisionId(pPciDev, IOMMU_PCI_REVISION_ID); /* VirtualBox specific device implementation revision */
6724 PDMPciDevSetClassBase(pPciDev, VBOX_PCI_CLASS_SYSTEM); /* System Base Peripheral */
6725 PDMPciDevSetClassSub(pPciDev, VBOX_PCI_SUB_SYSTEM_IOMMU); /* IOMMU */
6726 PDMPciDevSetClassProg(pPciDev, 0x0); /* IOMMU Programming interface */
6727 PDMPciDevSetHeaderType(pPciDev, 0x0); /* Single function, type 0 */
6728 PDMPciDevSetSubSystemId(pPciDev, IOMMU_PCI_DEVICE_ID); /* AMD */
6729 PDMPciDevSetSubSystemVendorId(pPciDev, IOMMU_PCI_VENDOR_ID); /* VirtualBox IOMMU device */
6730 PDMPciDevSetCapabilityList(pPciDev, IOMMU_PCI_OFF_CAP_HDR); /* Offset into capability registers */
6731 PDMPciDevSetInterruptPin(pPciDev, 0x1); /* INTA#. */
6732 PDMPciDevSetInterruptLine(pPciDev, 0x0); /* For software compatibility; no effect on hardware */
6733
6734 /* Capability Header. */
6735 /* NOTE! Fields (e.g, EFR) must match what we expose in the ACPI tables. */
6736 PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_CAP_HDR,
6737 RT_BF_MAKE(IOMMU_BF_CAPHDR_CAP_ID, 0xf) /* RO - Secure Device capability block */
6738 | RT_BF_MAKE(IOMMU_BF_CAPHDR_CAP_PTR, IOMMU_PCI_OFF_MSI_CAP_HDR) /* RO - Next capability offset */
6739 | RT_BF_MAKE(IOMMU_BF_CAPHDR_CAP_TYPE, 0x3) /* RO - IOMMU capability block */
6740 | RT_BF_MAKE(IOMMU_BF_CAPHDR_CAP_REV, 0x1) /* RO - IOMMU interface revision */
6741 | RT_BF_MAKE(IOMMU_BF_CAPHDR_IOTLB_SUP, 0x0) /* RO - Remote IOTLB support */
6742 | RT_BF_MAKE(IOMMU_BF_CAPHDR_HT_TUNNEL, 0x0) /* RO - HyperTransport Tunnel support */
6743 | RT_BF_MAKE(IOMMU_BF_CAPHDR_NP_CACHE, 0x0) /* RO - Cache NP page table entries */
6744 | RT_BF_MAKE(IOMMU_BF_CAPHDR_EFR_SUP, 0x1) /* RO - Extended Feature Register support */
6745 | RT_BF_MAKE(IOMMU_BF_CAPHDR_CAP_EXT, 0x1)); /* RO - Misc. Information Register support */
6746
6747 /* Base Address Register. */
6748 PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_BASE_ADDR_REG_LO, 0x0); /* RW - Base address (Lo) and enable bit */
6749 PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_BASE_ADDR_REG_HI, 0x0); /* RW - Base address (Hi) */
6750
6751 /* IOMMU Range Register. */
6752 PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_RANGE_REG, 0x0); /* RW - Range register (implemented as RO by us) */
6753
6754 /* Misc. Information Register. */
6755 /* NOTE! Fields (e.g, GVA size) must match what we expose in the ACPI tables. */
6756 uint32_t const uMiscInfoReg0 = RT_BF_MAKE(IOMMU_BF_MISCINFO_0_MSI_NUM, 0) /* RO - MSI number */
6757 | RT_BF_MAKE(IOMMU_BF_MISCINFO_0_GVA_SIZE, 2) /* RO - Guest Virt. Addr size (2=48 bits) */
6758 | RT_BF_MAKE(IOMMU_BF_MISCINFO_0_PA_SIZE, 48) /* RO - Physical Addr size (48 bits) */
6759 | RT_BF_MAKE(IOMMU_BF_MISCINFO_0_VA_SIZE, 64) /* RO - Virt. Addr size (64 bits) */
6760 | RT_BF_MAKE(IOMMU_BF_MISCINFO_0_HT_ATS_RESV, 0) /* RW - HT ATS reserved */
6761 | RT_BF_MAKE(IOMMU_BF_MISCINFO_0_MSI_NUM_PPR, 0); /* RW - PPR interrupt number */
6762 uint32_t const uMiscInfoReg1 = 0;
6763 PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MISCINFO_REG_0, uMiscInfoReg0);
6764 PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MISCINFO_REG_1, uMiscInfoReg1);
6765
6766 /* MSI Capability Header register. */
6767 PDMMSIREG MsiReg;
6768 RT_ZERO(MsiReg);
6769 MsiReg.cMsiVectors = 1;
6770 MsiReg.iMsiCapOffset = IOMMU_PCI_OFF_MSI_CAP_HDR;
6771 MsiReg.iMsiNextOffset = 0; /* IOMMU_PCI_OFF_MSI_MAP_CAP_HDR */
6772 MsiReg.fMsi64bit = 1; /* 64-bit addressing support is mandatory; See AMD IOMMU spec. 2.8 "IOMMU Interrupt Support". */
6773
6774 /* MSI Address (Lo, Hi) and MSI data are read-write PCI config registers handled by our generic PCI config space code. */
6775#if 0
6776 /* MSI Address Lo. */
6777 PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MSI_ADDR_LO, 0); /* RW - MSI message address (Lo) */
6778 /* MSI Address Hi. */
6779 PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MSI_ADDR_HI, 0); /* RW - MSI message address (Hi) */
6780 /* MSI Data. */
6781 PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MSI_DATA, 0); /* RW - MSI data */
6782#endif
6783
6784#if 0
6785 /** @todo IOMMU: I don't know if we need to support this, enable later if
6786 * required. */
6787 /* MSI Mapping Capability Header register. */
6788 PDMPciDevSetDWord(pPciDev, IOMMU_PCI_OFF_MSI_MAP_CAP_HDR,
6789 RT_BF_MAKE(IOMMU_BF_MSI_MAP_CAPHDR_CAP_ID, 0x8) /* RO - Capability ID */
6790 | RT_BF_MAKE(IOMMU_BF_MSI_MAP_CAPHDR_CAP_PTR, 0x0) /* RO - Offset to next capability (NULL) */
6791 | RT_BF_MAKE(IOMMU_BF_MSI_MAP_CAPHDR_EN, 0x1) /* RO - MSI mapping capability enable */
6792 | RT_BF_MAKE(IOMMU_BF_MSI_MAP_CAPHDR_FIXED, 0x1) /* RO - MSI mapping range is fixed */
6793 | RT_BF_MAKE(IOMMU_BF_MSI_MAP_CAPHDR_CAP_TYPE, 0x15)); /* RO - MSI mapping capability */
6794 /* When implementing don't forget to copy this to its MMIO shadow register (MsiMapCapHdr) in iommuAmdR3Init. */
6795#endif
6796
6797 /*
6798 * Register the PCI function with PDM.
6799 */
6800 rc = PDMDevHlpPCIRegister(pDevIns, pPciDev);
6801 AssertLogRelRCReturn(rc, rc);
6802
6803 /*
6804 * Register MSI support for the PCI device.
6805 * This must be done -after- register it as a PCI device!
6806 */
6807 rc = PDMDevHlpPCIRegisterMsi(pDevIns, &MsiReg);
6808 AssertRCReturn(rc, rc);
6809
6810 /*
6811 * Intercept PCI config. space accesses.
6812 */
6813 rc = PDMDevHlpPCIInterceptConfigAccesses(pDevIns, pPciDev, iommuAmdR3PciConfigRead, iommuAmdR3PciConfigWrite);
6814 AssertLogRelRCReturn(rc, rc);
6815
6816 /*
6817 * Create the MMIO region.
6818 * Mapping of the region is done when software configures it via PCI config space.
6819 */
6820 rc = PDMDevHlpMmioCreate(pDevIns, IOMMU_MMIO_REGION_SIZE, pPciDev, 0 /* iPciRegion */, iommuAmdMmioWrite, iommuAmdMmioRead,
6821 NULL /* pvUser */,
6822 IOMMMIO_FLAGS_READ_DWORD_QWORD
6823 | IOMMMIO_FLAGS_WRITE_DWORD_QWORD_READ_MISSING
6824 | IOMMMIO_FLAGS_DBGSTOP_ON_COMPLICATED_READ
6825 | IOMMMIO_FLAGS_DBGSTOP_ON_COMPLICATED_WRITE,
6826 "AMD-IOMMU", &pThis->hMmio);
6827 AssertLogRelRCReturn(rc, rc);
6828
6829 /*
6830 * Register saved state.
6831 */
6832 rc = PDMDevHlpSSMRegister3(pDevIns, IOMMU_SAVED_STATE_VERSION, sizeof(IOMMU), iommuAmdR3LiveExec, iommuAmdR3SaveExec,
6833 iommuAmdR3LoadExec);
6834 AssertLogRelRCReturn(rc, rc);
6835
6836 /*
6837 * Register debugger info items.
6838 */
6839 PDMDevHlpDBGFInfoRegister(pDevIns, "iommu", "Display IOMMU state.", iommuAmdR3DbgInfo);
6840 PDMDevHlpDBGFInfoRegister(pDevIns, "iommudte", "Display the DTE for a device (from memory). Arguments: DeviceID.", iommuAmdR3DbgInfoDte);
6841 PDMDevHlpDBGFInfoRegister(pDevIns, "iommudevtabs", "Display I/O device tables with translation enabled.", iommuAmdR3DbgInfoDevTabs);
6842#ifdef IOMMU_WITH_IOTLBE_CACHE
6843 PDMDevHlpDBGFInfoRegister(pDevIns, "iommutlb", "Display IOTLBs for a domain. Arguments: DomainID.", iommuAmdR3DbgInfoIotlb);
6844#endif
6845#ifdef IOMMU_WITH_DTE_CACHE
6846 PDMDevHlpDBGFInfoRegister(pDevIns, "iommudtecache", "Display the DTE cache.", iommuAmdR3DbgInfoDteCache);
6847#endif
6848#ifdef IOMMU_WITH_IRTE_CACHE
6849 PDMDevHlpDBGFInfoRegister(pDevIns, "iommuirtecache", "Display the IRTE cache.", iommuAmdR3DbgInfoIrteCache);
6850#endif
6851
6852# ifdef VBOX_WITH_STATISTICS
6853 /*
6854 * Statistics.
6855 */
6856 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioReadR3, STAMTYPE_COUNTER, "R3/MmioRead", STAMUNIT_OCCURENCES, "Number of MMIO reads in R3");
6857 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioReadRZ, STAMTYPE_COUNTER, "RZ/MmioRead", STAMUNIT_OCCURENCES, "Number of MMIO reads in RZ.");
6858
6859 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioWriteR3, STAMTYPE_COUNTER, "R3/MmioWrite", STAMUNIT_OCCURENCES, "Number of MMIO writes in R3.");
6860 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMmioWriteRZ, STAMTYPE_COUNTER, "RZ/MmioWrite", STAMUNIT_OCCURENCES, "Number of MMIO writes in RZ.");
6861
6862 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMsiRemapR3, STAMTYPE_COUNTER, "R3/MsiRemap", STAMUNIT_OCCURENCES, "Number of interrupt remap requests in R3.");
6863 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMsiRemapRZ, STAMTYPE_COUNTER, "RZ/MsiRemap", STAMUNIT_OCCURENCES, "Number of interrupt remap requests in RZ.");
6864
6865 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemReadR3, STAMTYPE_COUNTER, "R3/MemRead", STAMUNIT_OCCURENCES, "Number of memory read translation requests in R3.");
6866 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemReadRZ, STAMTYPE_COUNTER, "RZ/MemRead", STAMUNIT_OCCURENCES, "Number of memory read translation requests in RZ.");
6867
6868 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemWriteR3, STAMTYPE_COUNTER, "R3/MemWrite", STAMUNIT_OCCURENCES, "Number of memory write translation requests in R3.");
6869 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemWriteRZ, STAMTYPE_COUNTER, "RZ/MemWrite", STAMUNIT_OCCURENCES, "Number of memory write translation requests in RZ.");
6870
6871 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemBulkReadR3, STAMTYPE_COUNTER, "R3/MemBulkRead", STAMUNIT_OCCURENCES, "Number of memory bulk read translation requests in R3.");
6872 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemBulkReadRZ, STAMTYPE_COUNTER, "RZ/MemBulkRead", STAMUNIT_OCCURENCES, "Number of memory bulk read translation requests in RZ.");
6873
6874 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemBulkWriteR3, STAMTYPE_COUNTER, "R3/MemBulkWrite", STAMUNIT_OCCURENCES, "Number of memory bulk write translation requests in R3.");
6875 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatMemBulkWriteRZ, STAMTYPE_COUNTER, "RZ/MemBulkWrite", STAMUNIT_OCCURENCES, "Number of memory bulk write translation requests in RZ.");
6876
6877 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmd, STAMTYPE_COUNTER, "R3/Commands", STAMUNIT_OCCURENCES, "Number of commands processed (total).");
6878 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmdCompWait, STAMTYPE_COUNTER, "R3/Commands/CompWait", STAMUNIT_OCCURENCES, "Number of Completion Wait commands processed.");
6879 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmdInvDte, STAMTYPE_COUNTER, "R3/Commands/InvDte", STAMUNIT_OCCURENCES, "Number of Invalidate DTE commands processed.");
6880 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmdInvIommuPages, STAMTYPE_COUNTER, "R3/Commands/InvIommuPages", STAMUNIT_OCCURENCES, "Number of Invalidate IOMMU Pages commands processed.");
6881 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmdInvIotlbPages, STAMTYPE_COUNTER, "R3/Commands/InvIotlbPages", STAMUNIT_OCCURENCES, "Number of Invalidate IOTLB Pages commands processed.");
6882 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmdInvIntrTable, STAMTYPE_COUNTER, "R3/Commands/InvIntrTable", STAMUNIT_OCCURENCES, "Number of Invalidate Interrupt Table commands processed.");
6883 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmdPrefIommuPages, STAMTYPE_COUNTER, "R3/Commands/PrefIommuPages", STAMUNIT_OCCURENCES, "Number of Prefetch IOMMU Pages commands processed.");
6884 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmdCompletePprReq, STAMTYPE_COUNTER, "R3/Commands/CompletePprReq", STAMUNIT_OCCURENCES, "Number of Complete PPR Requests commands processed.");
6885 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatCmdInvIommuAll, STAMTYPE_COUNTER, "R3/Commands/InvIommuAll", STAMUNIT_OCCURENCES, "Number of Invalidate IOMMU All commands processed.");
6886
6887
6888 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIotlbeCached, STAMTYPE_COUNTER, "IOTLB/Cached", STAMUNIT_OCCURENCES, "Number of IOTLB entries in the cache.");
6889 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIotlbeLazyEvictReuse, STAMTYPE_COUNTER, "IOTLB/LazyEvictReuse", STAMUNIT_OCCURENCES, "Number of IOTLB entries reused after lazy eviction.");
6890
6891 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatProfDteLookup, STAMTYPE_PROFILE, "Profile/DteLookup", STAMUNIT_TICKS_PER_CALL, "Profiling DTE lookup.");
6892 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatProfIotlbeLookup, STAMTYPE_PROFILE, "Profile/IotlbeLookup", STAMUNIT_TICKS_PER_CALL, "Profiling IOTLBE lookup.");
6893
6894 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatProfIrteLookup, STAMTYPE_PROFILE, "Profile/IrteLookup", STAMUNIT_TICKS_PER_CALL, "Profiling IRTE lookup.");
6895 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatProfIrteCacheLookup, STAMTYPE_PROFILE, "Profile/IrteCacheLookup", STAMUNIT_TICKS_PER_CALL, "Profiling IRTE cache lookup.");
6896
6897 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatAccessCacheHit, STAMTYPE_COUNTER, "MemAccess/CacheHit", STAMUNIT_OCCURENCES, "Number of cache hits.");
6898 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatAccessCacheMiss, STAMTYPE_COUNTER, "MemAccess/CacheMiss", STAMUNIT_OCCURENCES, "Number of cache misses.");
6899 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatAccessCacheHitFull, STAMTYPE_COUNTER, "MemAccess/CacheHitFull", STAMUNIT_OCCURENCES, "Number of accesses that was entirely in the cache.");
6900 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatAccessCacheNonContig, STAMTYPE_COUNTER, "MemAccess/CacheNonContig", STAMUNIT_OCCURENCES, "Number of cache accesses that resulted in non-contiguous translated regions.");
6901 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatAccessCachePermDenied, STAMTYPE_COUNTER, "MemAccess/CacheAddrDenied", STAMUNIT_OCCURENCES, "Number of cache accesses that resulted in denied permissions.");
6902 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatAccessDteNonContig, STAMTYPE_COUNTER, "MemAccess/DteNonContig", STAMUNIT_OCCURENCES, "Number of DTE accesses that resulted in non-contiguous translated regions.");
6903 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatAccessDtePermDenied, STAMTYPE_COUNTER, "MemAccess/DtePermDenied", STAMUNIT_OCCURENCES, "Number of DTE accesses that resulted in denied permissions.");
6904
6905 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIntrCacheHit, STAMTYPE_COUNTER, "Interrupt/CacheHit", STAMUNIT_OCCURENCES, "Number of cache hits.");
6906 PDMDevHlpSTAMRegister(pDevIns, &pThis->StatIntrCacheMiss, STAMTYPE_COUNTER, "Interrupt/CacheMiss", STAMUNIT_OCCURENCES, "Number of cache misses.");
6907# endif
6908
6909 /*
6910 * Create the command thread and its event semaphore.
6911 */
6912 char szDevIommu[64];
6913 RT_ZERO(szDevIommu);
6914 RTStrPrintf(szDevIommu, sizeof(szDevIommu), "IOMMU-%u", iInstance);
6915 rc = PDMDevHlpThreadCreate(pDevIns, &pThisR3->pCmdThread, pThis, iommuAmdR3CmdThread, iommuAmdR3CmdThreadWakeUp,
6916 0 /* cbStack */, RTTHREADTYPE_IO, szDevIommu);
6917 AssertLogRelRCReturn(rc, rc);
6918
6919 rc = PDMDevHlpSUPSemEventCreate(pDevIns, &pThis->hEvtCmdThread);
6920 AssertLogRelRCReturn(rc, rc);
6921
6922#ifdef IOMMU_WITH_DTE_CACHE
6923 /*
6924 * Initialize the critsect of the cache.
6925 */
6926 rc = PDMDevHlpCritSectInit(pDevIns, &pThis->CritSectCache, RT_SRC_POS, "IOMMUCache-#%u", pDevIns->iInstance);
6927 AssertLogRelRCReturn(rc, rc);
6928
6929 /* Several places in this code relies on this basic assumption - assert it! */
6930 AssertCompile(RT_ELEMENTS(pThis->aDeviceIds) == RT_ELEMENTS(pThis->aDteCache));
6931#endif
6932
6933#ifdef IOMMU_WITH_IOTLBE_CACHE
6934 /*
6935 * Allocate IOTLB entries.
6936 * This is allocated upfront since we expect a relatively small number of entries,
6937 * is more cache-line efficient and easier to track least recently used entries for
6938 * eviction when the cache is full. This also avoids unpredictable behavior during
6939 * the lifetime of the VM if the hyperheap gets full.
6940 */
6941 size_t const cbIotlbes = sizeof(IOTLBE) * IOMMU_IOTLBE_MAX;
6942 pThisR3->paIotlbes = (PIOTLBE)PDMDevHlpMMHeapAllocZ(pDevIns, cbIotlbes);
6943 if (!pThisR3->paIotlbes)
6944 return PDMDevHlpVMSetError(pDevIns, VERR_NO_MEMORY, RT_SRC_POS,
6945 N_("Failed to allocate %zu bytes from the hyperheap for the IOTLB cache."), cbIotlbes);
6946 RTListInit(&pThisR3->LstLruIotlbe);
6947 LogRel(("%s: Allocated %zu bytes from the hyperheap for the IOTLB cache\n", IOMMU_LOG_PFX, cbIotlbes));
6948#endif
6949
6950 /*
6951 * Initialize read-only registers.
6952 * NOTE! Fields here must match their corresponding field in the ACPI tables.
6953 */
6954 /* Don't remove the commented lines below as it lets us see all features at a glance. */
6955 pThis->ExtFeat.u64 = 0;
6956 //pThis->ExtFeat.n.u1PrefetchSup = 0;
6957 //pThis->ExtFeat.n.u1PprSup = 0;
6958 //pThis->ExtFeat.n.u1X2ApicSup = 0;
6959 //pThis->ExtFeat.n.u1NoExecuteSup = 0;
6960 //pThis->ExtFeat.n.u1GstTranslateSup = 0;
6961 pThis->ExtFeat.n.u1InvAllSup = 1;
6962 //pThis->ExtFeat.n.u1GstVirtApicSup = 0;
6963 pThis->ExtFeat.n.u1HwErrorSup = 1;
6964 //pThis->ExtFeat.n.u1PerfCounterSup = 0;
6965 AssertCompile((IOMMU_MAX_HOST_PT_LEVEL & 0x3) < 3);
6966 pThis->ExtFeat.n.u2HostAddrTranslateSize = (IOMMU_MAX_HOST_PT_LEVEL & 0x3);
6967 //pThis->ExtFeat.n.u2GstAddrTranslateSize = 0; /* Requires GstTranslateSup */
6968 //pThis->ExtFeat.n.u2GstCr3RootTblLevel = 0; /* Requires GstTranslateSup */
6969 //pThis->ExtFeat.n.u2SmiFilterSup = 0;
6970 //pThis->ExtFeat.n.u3SmiFilterCount = 0;
6971 //pThis->ExtFeat.n.u3GstVirtApicModeSup = 0; /* Requires GstVirtApicSup */
6972 //pThis->ExtFeat.n.u2DualPprLogSup = 0;
6973 //pThis->ExtFeat.n.u2DualEvtLogSup = 0;
6974 //pThis->ExtFeat.n.u5MaxPasidSup = 0; /* Requires GstTranslateSup */
6975 //pThis->ExtFeat.n.u1UserSupervisorSup = 0;
6976 AssertCompile(IOMMU_MAX_DEV_TAB_SEGMENTS <= 3);
6977 pThis->ExtFeat.n.u2DevTabSegSup = IOMMU_MAX_DEV_TAB_SEGMENTS;
6978 //pThis->ExtFeat.n.u1PprLogOverflowWarn = 0;
6979 //pThis->ExtFeat.n.u1PprAutoRespSup = 0;
6980 //pThis->ExtFeat.n.u2MarcSup = 0;
6981 //pThis->ExtFeat.n.u1BlockStopMarkSup = 0;
6982 //pThis->ExtFeat.n.u1PerfOptSup = 0;
6983 pThis->ExtFeat.n.u1MsiCapMmioSup = 1;
6984 //pThis->ExtFeat.n.u1GstIoSup = 0;
6985 //pThis->ExtFeat.n.u1HostAccessSup = 0;
6986 //pThis->ExtFeat.n.u1EnhancedPprSup = 0;
6987 //pThis->ExtFeat.n.u1AttrForwardSup = 0;
6988 //pThis->ExtFeat.n.u1HostDirtySup = 0;
6989 //pThis->ExtFeat.n.u1InvIoTlbTypeSup = 0;
6990 //pThis->ExtFeat.n.u1GstUpdateDisSup = 0;
6991 //pThis->ExtFeat.n.u1ForcePhysDstSup = 0;
6992
6993 pThis->DevSpecificFeat.u64 = 0;
6994 pThis->DevSpecificFeat.n.u4RevMajor = IOMMU_DEVSPEC_FEAT_MAJOR_VERSION;
6995 pThis->DevSpecificFeat.n.u4RevMinor = IOMMU_DEVSPEC_FEAT_MINOR_VERSION;
6996
6997 pThis->DevSpecificCtrl.u64 = 0;
6998 pThis->DevSpecificCtrl.n.u4RevMajor = IOMMU_DEVSPEC_CTRL_MAJOR_VERSION;
6999 pThis->DevSpecificCtrl.n.u4RevMinor = IOMMU_DEVSPEC_CTRL_MINOR_VERSION;
7000
7001 pThis->DevSpecificStatus.u64 = 0;
7002 pThis->DevSpecificStatus.n.u4RevMajor = IOMMU_DEVSPEC_STATUS_MAJOR_VERSION;
7003 pThis->DevSpecificStatus.n.u4RevMinor = IOMMU_DEVSPEC_STATUS_MINOR_VERSION;
7004
7005 pThis->MiscInfo.u64 = RT_MAKE_U64(uMiscInfoReg0, uMiscInfoReg1);
7006
7007 pThis->RsvdReg = 0;
7008
7009 /*
7010 * Initialize parts of the IOMMU state as it would during reset.
7011 * Also initializes non-zero initial values like IRTE cache keys.
7012 * Must be called -after- initializing PCI config. space registers.
7013 */
7014 iommuAmdR3Reset(pDevIns);
7015
7016 LogRel(("%s: DSFX=%u.%u DSCX=%u.%u DSSX=%u.%u ExtFeat=%#RX64\n", IOMMU_LOG_PFX,
7017 pThis->DevSpecificFeat.n.u4RevMajor, pThis->DevSpecificFeat.n.u4RevMinor,
7018 pThis->DevSpecificCtrl.n.u4RevMajor, pThis->DevSpecificCtrl.n.u4RevMinor,
7019 pThis->DevSpecificStatus.n.u4RevMajor, pThis->DevSpecificStatus.n.u4RevMinor,
7020 pThis->ExtFeat.u64));
7021 return VINF_SUCCESS;
7022}
7023
7024#else
7025
7026/**
7027 * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
7028 */
7029static DECLCALLBACK(int) iommuAmdRZConstruct(PPDMDEVINS pDevIns)
7030{
7031 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
7032 PIOMMU pThis = PDMDEVINS_2_DATA(pDevIns, PIOMMU);
7033 PIOMMUCC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PIOMMUCC);
7034 pThisCC->CTX_SUFF(pDevIns) = pDevIns;
7035
7036 /* We will use PDM's critical section (via helpers) for the IOMMU device. */
7037 int rc = PDMDevHlpSetDeviceCritSect(pDevIns, PDMDevHlpCritSectGetNop(pDevIns));
7038 AssertRCReturn(rc, rc);
7039
7040 /* Set up the MMIO RZ handlers. */
7041 rc = PDMDevHlpMmioSetUpContext(pDevIns, pThis->hMmio, iommuAmdMmioWrite, iommuAmdMmioRead, NULL /* pvUser */);
7042 AssertRCReturn(rc, rc);
7043
7044 /* Set up the IOMMU RZ callbacks. */
7045 PDMIOMMUREGCC IommuReg;
7046 RT_ZERO(IommuReg);
7047 IommuReg.u32Version = PDM_IOMMUREGCC_VERSION;
7048 IommuReg.idxIommu = pThis->idxIommu;
7049 IommuReg.pfnMemAccess = iommuAmdMemAccess;
7050 IommuReg.pfnMemBulkAccess = iommuAmdMemBulkAccess;
7051 IommuReg.pfnMsiRemap = iommuAmdMsiRemap;
7052 IommuReg.u32TheEnd = PDM_IOMMUREGCC_VERSION;
7053 rc = PDMDevHlpIommuSetUpContext(pDevIns, &IommuReg, &pThisCC->CTX_SUFF(pIommuHlp));
7054 AssertRCReturn(rc, rc);
7055 AssertPtrReturn(pThisCC->CTX_SUFF(pIommuHlp), VERR_IOMMU_IPE_1);
7056 AssertReturn(pThisCC->CTX_SUFF(pIommuHlp)->u32Version == CTX_SUFF(PDM_IOMMUHLP)_VERSION, VERR_VERSION_MISMATCH);
7057 AssertReturn(pThisCC->CTX_SUFF(pIommuHlp)->u32TheEnd == CTX_SUFF(PDM_IOMMUHLP)_VERSION, VERR_VERSION_MISMATCH);
7058 AssertPtrReturn(pThisCC->CTX_SUFF(pIommuHlp)->pfnLock, VERR_INVALID_POINTER);
7059 AssertPtrReturn(pThisCC->CTX_SUFF(pIommuHlp)->pfnUnlock, VERR_INVALID_POINTER);
7060 return VINF_SUCCESS;
7061}
7062#endif
7063
7064
7065/**
7066 * The device registration structure.
7067 */
7068const PDMDEVREG g_DeviceIommuAmd =
7069{
7070 /* .u32Version = */ PDM_DEVREG_VERSION,
7071 /* .uReserved0 = */ 0,
7072 /* .szName = */ "iommu-amd",
7073 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE,
7074 /* .fClass = */ PDM_DEVREG_CLASS_PCI_BUILTIN,
7075 /* .cMaxInstances = */ ~0U,
7076 /* .uSharedVersion = */ 42,
7077 /* .cbInstanceShared = */ sizeof(IOMMU),
7078 /* .cbInstanceCC = */ sizeof(IOMMUCC),
7079 /* .cbInstanceRC = */ sizeof(IOMMURC),
7080 /* .cMaxPciDevices = */ 1,
7081 /* .cMaxMsixVectors = */ 0,
7082 /* .pszDescription = */ "IOMMU (AMD)",
7083#if defined(IN_RING3)
7084 /* .pszRCMod = */ "VBoxDDRC.rc",
7085 /* .pszR0Mod = */ "VBoxDDR0.r0",
7086 /* .pfnConstruct = */ iommuAmdR3Construct,
7087 /* .pfnDestruct = */ iommuAmdR3Destruct,
7088 /* .pfnRelocate = */ NULL,
7089 /* .pfnMemSetup = */ NULL,
7090 /* .pfnPowerOn = */ NULL,
7091 /* .pfnReset = */ iommuAmdR3Reset,
7092 /* .pfnSuspend = */ NULL,
7093 /* .pfnResume = */ NULL,
7094 /* .pfnAttach = */ NULL,
7095 /* .pfnDetach = */ NULL,
7096 /* .pfnQueryInterface = */ NULL,
7097 /* .pfnInitComplete = */ NULL,
7098 /* .pfnPowerOff = */ NULL,
7099 /* .pfnSoftReset = */ NULL,
7100 /* .pfnReserved0 = */ NULL,
7101 /* .pfnReserved1 = */ NULL,
7102 /* .pfnReserved2 = */ NULL,
7103 /* .pfnReserved3 = */ NULL,
7104 /* .pfnReserved4 = */ NULL,
7105 /* .pfnReserved5 = */ NULL,
7106 /* .pfnReserved6 = */ NULL,
7107 /* .pfnReserved7 = */ NULL,
7108#elif defined(IN_RING0)
7109 /* .pfnEarlyConstruct = */ NULL,
7110 /* .pfnConstruct = */ iommuAmdRZConstruct,
7111 /* .pfnDestruct = */ NULL,
7112 /* .pfnFinalDestruct = */ NULL,
7113 /* .pfnRequest = */ NULL,
7114 /* .pfnReserved0 = */ NULL,
7115 /* .pfnReserved1 = */ NULL,
7116 /* .pfnReserved2 = */ NULL,
7117 /* .pfnReserved3 = */ NULL,
7118 /* .pfnReserved4 = */ NULL,
7119 /* .pfnReserved5 = */ NULL,
7120 /* .pfnReserved6 = */ NULL,
7121 /* .pfnReserved7 = */ NULL,
7122#elif defined(IN_RC)
7123 /* .pfnConstruct = */ iommuAmdRZConstruct,
7124 /* .pfnReserved0 = */ NULL,
7125 /* .pfnReserved1 = */ NULL,
7126 /* .pfnReserved2 = */ NULL,
7127 /* .pfnReserved3 = */ NULL,
7128 /* .pfnReserved4 = */ NULL,
7129 /* .pfnReserved5 = */ NULL,
7130 /* .pfnReserved6 = */ NULL,
7131 /* .pfnReserved7 = */ NULL,
7132#else
7133# error "Not in IN_RING3, IN_RING0 or IN_RC!"
7134#endif
7135 /* .u32VersionEnd = */ PDM_DEVREG_VERSION
7136};
7137
7138#endif /* !VBOX_DEVICE_STRUCT_TESTCASE */
7139
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