Index: /trunk/src/VBox/VMM/VMMAll/PDMAllIommu.cpp
===================================================================
--- /trunk/src/VBox/VMM/VMMAll/PDMAllIommu.cpp	(revision 87477)
+++ /trunk/src/VBox/VMM/VMMAll/PDMAllIommu.cpp	(revision 87478)
@@ -27,4 +27,17 @@
 #ifdef IN_RING3
 # include <iprt/mem.h>
+#endif
+
+
+/*********************************************************************************************************************************
+*   Defined Constants And Macros                                                                                                 *
+*********************************************************************************************************************************/
+/**
+ * Gets the PDM IOMMU for the current context from the PDM device instance.
+ */
+#ifdef IN_RING0
+#define PDMDEVINS_TO_IOMMU(a_pDevIns)   &(a_pDevIns)->Internal.s.pGVM->pdmr0.s.aIommus[0];
+#else
+#define PDMDEVINS_TO_IOMMU(a_pDevIns)   &(a_pDevIns)->Internal.s.pVMR3->pdm.s.aIommus[0];
 #endif
 
@@ -53,20 +66,4 @@
 
 
-#if 0
-/* Hmpf. We have no PPDMIOMMUR3 ... */
-DECL_FORCE_INLINE(PPDMDEVINS) pdmIommuGetInstance(PPDMDEVINS pDevIns)
-{
-#if defined(IN_RING0)
-    PGVM        pGVM   = pDevIns->Internal.s.pGVM;
-    PPDMIOMMUR0 pIommu = &pGVM->pdmr0.s.aIommus[0];
-#else
-    PVM        pVM     = pDevIns->Internal.s.pVMR3;
-    PPDMIOMMU  pIommu  = &pVM->pdm.s.aIommus[0];
-#endif
-    return pIommu->CTX_SUFF(pDevIns);
-}
-#endif
-
-
 /**
  * Bus master physical memory read after translating the physical address using the
@@ -87,9 +84,6 @@
 int pdmIommuMemAccessRead(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, RTGCPHYS GCPhys, void *pvBuf, size_t cbRead, uint32_t fFlags)
 {
-    /** @todo IOMMU: Optimize/re-organize things here later. */
-#if defined(IN_RING0)
-    PGVM        pGVM         = pDevIns->Internal.s.pGVM;
-    PPDMIOMMUR0 pIommu       = &pGVM->pdmr0.s.aIommus[0];
-    PPDMDEVINS  pDevInsIommu = pIommu->CTX_SUFF(pDevIns);
+    PPDMIOMMU  pIommu       = PDMDEVINS_TO_IOMMU(pDevIns);
+    PPDMDEVINS pDevInsIommu = pIommu->CTX_SUFF(pDevIns);
     if (   pDevInsIommu
         && pDevInsIommu != pDevIns)
@@ -105,5 +99,5 @@
             {
                 /** @todo Handle strict return codes from PGMPhysRead. */
-                rc = pDevIns->pHlpR0->pfnPhysRead(pDevIns, GCPhysOut, pvBuf, cbRead, fFlags);
+                rc = pDevIns->CTX_SUFF(pHlp)->pfnPhysRead(pDevIns, GCPhysOut, pvBuf, cbRead, fFlags);
                 if (RT_SUCCESS(rc))
                 {
@@ -117,6 +111,5 @@
             else
             {
-                LogFunc(("R0: IOMMU memory read failed. uDeviceId=%#x GCPhys=%#RGp cb=%u rc=%Rrc\n", uDeviceId, GCPhys, cbRead,
-                         rc));
+                LogFunc(("IOMMU memory read failed. uDeviceId=%#x GCPhys=%#RGp cb=%zu rc=%Rrc\n", uDeviceId, GCPhys, cbRead, rc));
                 break;
             }
@@ -124,47 +117,5 @@
         return rc;
     }
-
-    return VERR_IOMMU_NOT_PRESENT;
-#elif defined(IN_RING3)
-    PVM        pVM          = pDevIns->Internal.s.pVMR3;
-    PPDMIOMMU  pIommu       = &pVM->pdm.s.aIommus[0];
-    PPDMDEVINS pDevInsIommu = pIommu->CTX_SUFF(pDevIns);
-    if (   pDevInsIommu
-        && pDevInsIommu != pDevIns)
-    {
-        uint16_t const uDeviceId = pdmIommuGetPciDeviceId(pDevIns, pPciDev);
-        int rc = VINF_SUCCESS;
-        while (cbRead > 0)
-        {
-            RTGCPHYS GCPhysOut;
-            size_t   cbContig;
-            rc = pIommu->pfnMemAccess(pDevInsIommu, uDeviceId, GCPhys, cbRead, PDMIOMMU_MEM_F_READ, &GCPhysOut, &cbContig);
-            if (RT_SUCCESS(rc))
-            {
-                /** @todo Handle strict return codes from PGMPhysRead. */
-                rc = pDevIns->pHlpR3->pfnPhysRead(pDevIns, GCPhysOut, pvBuf, cbRead, fFlags);
-                if (RT_SUCCESS(rc))
-                {
-                    cbRead -= cbContig;
-                    pvBuf   = (void *)((uintptr_t)pvBuf + cbContig);
-                    GCPhys += cbContig;
-                }
-                else
-                    break;
-            }
-            else
-            {
-                LogFunc(("R3: IOMMU memory read failed. uDeviceId=%#x GCPhys=%#RGp cb=%u rc=%Rrc\n", uDeviceId, GCPhys, cbRead,
-                         rc));
-                break;
-            }
-        }
-        return rc;
-    }
-
-    return VERR_IOMMU_NOT_PRESENT;
-#else
-# error "Implement me."
-#endif
+    return VERR_IOMMU_NOT_PRESENT;
 }
 
@@ -189,9 +140,6 @@
                            uint32_t fFlags)
 {
-    /** @todo IOMMU: Optimize/re-organize things here later. */
-#if defined(IN_RING0)
-    PGVM        pGVM         = pDevIns->Internal.s.pGVM;
-    PPDMIOMMUR0 pIommu       = &pGVM->pdmr0.s.aIommus[0];
-    PPDMDEVINS  pDevInsIommu = pIommu->CTX_SUFF(pDevIns);
+    PPDMIOMMU  pIommu       = PDMDEVINS_TO_IOMMU(pDevIns);
+    PPDMDEVINS pDevInsIommu = pIommu->CTX_SUFF(pDevIns);
     if (   pDevInsIommu
         && pDevInsIommu != pDevIns)
@@ -207,5 +155,5 @@
             {
                 /** @todo Handle strict return codes from PGMPhysWrite. */
-                rc = pDevIns->pHlpR0->pfnPhysWrite(pDevIns, GCPhysOut, pvBuf, cbWrite, fFlags);
+                rc = pDevIns->CTX_SUFF(pHlp)->pfnPhysWrite(pDevIns, GCPhysOut, pvBuf, cbWrite, fFlags);
                 if (RT_SUCCESS(rc))
                 {
@@ -219,5 +167,5 @@
             else
             {
-                LogFunc(("R0: IOMMU memory write failed. uDeviceId=%#x GCPhys=%#RGp cb=%u rc=%Rrc\n", uDeviceId, GCPhys, cbWrite,
+                LogFunc(("IOMMU memory write failed. uDeviceId=%#x GCPhys=%#RGp cb=%zu rc=%Rrc\n", uDeviceId, GCPhys, cbWrite,
                          rc));
                 break;
@@ -226,47 +174,5 @@
         return rc;
     }
-
-    return VERR_IOMMU_NOT_PRESENT;
-#elif defined(IN_RING3)
-    PVM        pVM          = pDevIns->Internal.s.pVMR3;
-    PPDMIOMMU  pIommu       = &pVM->pdm.s.aIommus[0];
-    PPDMDEVINS pDevInsIommu = pIommu->CTX_SUFF(pDevIns);
-    if (   pDevInsIommu
-        && pDevInsIommu != pDevIns)
-    {
-        uint16_t const uDeviceId = pdmIommuGetPciDeviceId(pDevIns, pPciDev);
-        int rc = VINF_SUCCESS;
-        while (cbWrite > 0)
-        {
-            RTGCPHYS GCPhysOut;
-            size_t   cbContig;
-            rc = pIommu->pfnMemAccess(pDevInsIommu, uDeviceId, GCPhys, cbWrite, PDMIOMMU_MEM_F_WRITE, &GCPhysOut, &cbContig);
-            if (RT_SUCCESS(rc))
-            {
-                /** @todo Handle strict return codes from PGMPhysWrite. */
-                rc = pDevIns->pHlpR3->pfnPhysWrite(pDevIns, GCPhysOut, pvBuf, cbContig, fFlags);
-                if (RT_SUCCESS(rc))
-                {
-                    cbWrite -= cbContig;
-                    pvBuf    = (const void *)((uintptr_t)pvBuf + cbContig);
-                    GCPhys  += cbContig;
-                }
-                else
-                    break;
-            }
-            else
-            {
-                LogFunc(("R3: IOMMU memory write failed. uDeviceId=%#x GCPhys=%#RGp cb=%u rc=%Rrc\n", uDeviceId, GCPhys, cbWrite,
-                         rc));
-                break;
-            }
-        }
-        return rc;
-    }
-
-    return VERR_IOMMU_NOT_PRESENT;
-#else
-# error "Implement me."
-#endif
+    return VERR_IOMMU_NOT_PRESENT;
 }
 
@@ -294,6 +200,5 @@
 {
 #if defined(IN_RING3)
-    PVM        pVM          = pDevIns->Internal.s.pVMR3;
-    PPDMIOMMU  pIommu       = &pVM->pdm.s.aIommus[0];
+    PPDMIOMMU  pIommu       = PDMDEVINS_TO_IOMMU(pDevIns);
     PPDMDEVINS pDevInsIommu = pIommu->CTX_SUFF(pDevIns);
     if (   pDevInsIommu
@@ -315,5 +220,4 @@
         return rc;
     }
-
     return VERR_IOMMU_NOT_PRESENT;
 #else
@@ -346,6 +250,5 @@
 {
 #if defined(IN_RING3)
-    PVM        pVM          = pDevIns->Internal.s.pVMR3;
-    PPDMIOMMU  pIommu       = &pVM->pdm.s.aIommus[0];
+    PPDMIOMMU  pIommu       = PDMDEVINS_TO_IOMMU(pDevIns);
     PPDMDEVINS pDevInsIommu = pIommu->CTX_SUFF(pDevIns);
     if (   pDevInsIommu
@@ -367,5 +270,4 @@
         return rc;
     }
-
     return VERR_IOMMU_NOT_PRESENT;
 #else
@@ -401,6 +303,5 @@
 {
 #ifdef IN_RING3
-    PVM        pVM          = pDevIns->Internal.s.pVMR3;
-    PPDMIOMMU  pIommu       = &pVM->pdm.s.aIommus[0];
+    PPDMIOMMU  pIommu       = PDMDEVINS_TO_IOMMU(pDevIns);
     PPDMDEVINS pDevInsIommu = pIommu->CTX_SUFF(pDevIns);
     if (   pDevInsIommu
@@ -437,5 +338,4 @@
         return rc;
     }
-
     return VERR_IOMMU_NOT_PRESENT;
 #else
@@ -471,6 +371,5 @@
 {
 #ifdef IN_RING3
-    PVM        pVM          = pDevIns->Internal.s.pVMR3;
-    PPDMIOMMU  pIommu       = &pVM->pdm.s.aIommus[0];
+    PPDMIOMMU  pIommu       = PDMDEVINS_TO_IOMMU(pDevIns);
     PPDMDEVINS pDevInsIommu = pIommu->CTX_SUFF(pDevIns);
     if (   pDevInsIommu
@@ -507,5 +406,4 @@
         return rc;
     }
-
     return VERR_IOMMU_NOT_PRESENT;
 #else
@@ -519,8 +417,6 @@
 int pdmIommuMsiRemap(PPDMDEVINS pDevIns, uint16_t uDeviceId, PCMSIMSG pMsiIn, PMSIMSG pMsiOut)
 {
-#if defined(IN_RING0)
-    PGVM        pGVM         = pDevIns->Internal.s.pGVM;
-    PPDMIOMMUR0 pIommu       = &pGVM->pdmr0.s.aIommus[0];
-    PPDMDEVINS  pDevInsIommu = pIommu->CTX_SUFF(pDevIns);
+    PPDMIOMMU  pIommu       = PDMDEVINS_TO_IOMMU(pDevIns);
+    PPDMDEVINS pDevInsIommu = pIommu->CTX_SUFF(pDevIns);
     if (   pDevInsIommu
         && pDevInsIommu != pDevIns)
@@ -529,31 +425,10 @@
         if (RT_FAILURE(rc))
         {
-            LogFunc(("R0: MSI remap failed. uDeviceId=%#x pMsiIn=(%#RX64, %#RU32) rc=%Rrc\n", uDeviceId, pMsiIn->Addr.u64,
+            LogFunc(("MSI remap failed. uDeviceId=%#x pMsiIn=(%#RX64, %#RU32) rc=%Rrc\n", uDeviceId, pMsiIn->Addr.u64,
                      pMsiIn->Data.u32, rc));
         }
         return rc;
     }
-
-    return VERR_IOMMU_NOT_PRESENT;
-#elif defined(IN_RING3)
-    PVM        pVM          = pDevIns->Internal.s.pVMR3;
-    PPDMIOMMU  pIommu       = &pVM->pdm.s.aIommus[0];
-    PPDMDEVINS pDevInsIommu = pIommu->CTX_SUFF(pDevIns);
-    if (   pDevInsIommu
-        && pDevInsIommu != pDevIns)
-    {
-        int rc = pIommu->pfnMsiRemap(pDevInsIommu, uDeviceId, pMsiIn, pMsiOut);
-        if (RT_FAILURE(rc))
-        {
-            LogFunc(("R3: MSI remap failed. uDeviceId=%#x pMsiIn=(%#RX64, %#RU32) rc=%Rrc\n", uDeviceId, pMsiIn->Addr.u64,
-                     pMsiIn->Data.u32, rc));
-        }
-        return rc;
-    }
-
-    return VERR_IOMMU_NOT_PRESENT;
-#else
-# error "Implement me."
-#endif
-}
-
+    return VERR_IOMMU_NOT_PRESENT;
+}
+
Index: /trunk/src/VBox/VMM/VMMR0/PDMR0DevHlp.cpp
===================================================================
--- /trunk/src/VBox/VMM/VMMR0/PDMR0DevHlp.cpp	(revision 87477)
+++ /trunk/src/VBox/VMM/VMMR0/PDMR0DevHlp.cpp	(revision 87478)
@@ -1028,5 +1028,5 @@
     ASMCompilerBarrier();
     AssertLogRelMsgReturn(idxIommu < RT_ELEMENTS(pGVM->pdm.s.aIommus), ("idxIommu=%#x\n", idxIommu), VERR_OUT_OF_RANGE);
-    PPDMIOMMU pIommuShared = &pGVM->pdm.s.aIommus[idxIommu];
+    PPDMIOMMUR3 pIommuShared = &pGVM->pdm.s.aIommus[idxIommu];
     AssertLogRelMsgReturn(pIommuShared->idxIommu == idxIommu, ("%u vs %u\n", pIommuShared->idxIommu, idxIommu), VERR_INVALID_PARAMETER);
     AssertLogRelMsgReturn(pIommuShared->pDevInsR3 == pDevIns->pDevInsForR3,
Index: /trunk/src/VBox/VMM/VMMR3/PDMDevHlp.cpp
===================================================================
--- /trunk/src/VBox/VMM/VMMR3/PDMDevHlp.cpp	(revision 87477)
+++ /trunk/src/VBox/VMM/VMMR3/PDMDevHlp.cpp	(revision 87478)
@@ -1461,6 +1461,6 @@
 #ifdef VBOX_WITH_IOMMU_AMD
         /** @todo IOMMU: Restrict this to the AMD flavor of IOMMU only at runtime. */
-        PPDMIOMMU  pIommu       = &pVM->pdm.s.aIommus[0];
-        PPDMDEVINS pDevInsIommu = pIommu->CTX_SUFF(pDevIns);
+        PPDMIOMMUR3 pIommu       = &pVM->pdm.s.aIommus[0];
+        PPDMDEVINS  pDevInsIommu = pIommu->CTX_SUFF(pDevIns);
         if (pDevInsIommu)
         {
@@ -3474,5 +3474,5 @@
                     VERR_ALREADY_EXISTS);
 #endif
-    PPDMIOMMU pIommu = &pVM->pdm.s.aIommus[idxIommu];
+    PPDMIOMMUR3 pIommu = &pVM->pdm.s.aIommus[idxIommu];
 
     /*
Index: /trunk/src/VBox/VMM/include/PDMInternal.h
===================================================================
--- /trunk/src/VBox/VMM/include/PDMInternal.h	(revision 87477)
+++ /trunk/src/VBox/VMM/include/PDMInternal.h	(revision 87478)
@@ -684,7 +684,7 @@
 
 /**
- * PDM registered IOMMU device.
- */
-typedef struct PDMIOMMU
+ * PDM IOMMU, shared ring-3.
+ */
+typedef struct PDMIOMMUR3
 {
     /** IOMMU index. */
@@ -702,13 +702,13 @@
     /** @copydoc PDMIOMMUREGR3::pfnMsiRemap */
     DECLR3CALLBACKMEMBER(int,   pfnMsiRemap,(PPDMDEVINS pDevIns, uint16_t uDevId, PCMSIMSG pMsiIn, PMSIMSG pMsiOut));
-} PDMIOMMU;
+} PDMIOMMUR3;
 /** Pointer to a PDM IOMMU instance. */
-typedef PDMIOMMU *PPDMIOMMU;
+typedef PDMIOMMUR3 *PPDMIOMMUR3;
 /** Pointer to a const PDM IOMMU instance. */
-typedef const PDMIOMMU *PCPDMIOMMU;
-
-
-/**
- * Ring-0 PDM IOMMU instance data.
+typedef const PDMIOMMUR3 *PCPDMIOMMUR3;
+
+
+/**
+ * PDM IOMMU, ring-0.
  */
 typedef struct PDMIOMMUR0
@@ -733,4 +733,11 @@
 /** Pointer to a const ring-0 IOMMU data. */
 typedef const PDMIOMMUR0 *PCPDMIOMMUR0;
+
+/** Pointer to a PDM IOMMU for the current context. */
+#ifdef IN_RING3
+typedef PPDMIOMMUR3     PPDMIOMMU;
+#else
+typedef PPDMIOMMUR0     PPDMIOMMU;
+#endif
 
 
@@ -1356,5 +1363,5 @@
     PDMPCIBUS                       aPciBuses[PDM_PCI_BUSSES_MAX];
     /** IOMMU devices. */
-    PDMIOMMU                        aIommus[PDM_IOMMUS_MAX];
+    PDMIOMMUR3                      aIommus[PDM_IOMMUS_MAX];
     /** The register PIC device. */
     PDMPIC                          Pic;
