Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Sairaj Kodilkar <sarunkod@amd.com>
To: "Clément Mathieu--Drif" <clement.mathieu--drif@bull.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Alejandro Jimenez" <alejandro.j.jimenez@oracle.com>,
	"Ani Sinha" <anisinha@redhat.com>,
	"Igor Mammedov" <imammedo@redhat.com>,
	"Jason Wang" <jasowangio@gmail.com>,
	"Marcelo Tosatti" <mtosatti@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Richard Henderson" <richard.henderson@linaro.org>,
	"Sairaj Kodilkar" <sarunkod@amd.com>,
	"Yi Liu" <yi.l.liu@intel.com>,
	kvm@vger.kernel.org, qemu-devel@nongnu.org
Cc: <Vasant.Hegde@amd.com>, <Suravee.Suthikulpanit@amd.com>
Subject: [PATCH 5/6] amd_iommu: Introduce AMDIVRSInfo for shared IVRS construction
Date: Mon, 30 Nov 2026 15:42:36 +0530	[thread overview]
Message-ID: <20261130101237.1416-6-sarunkod@amd.com> (raw)
In-Reply-To: <20261130101237.1416-1-sarunkod@amd.com>

IVRS construction will be shared by the both emulated IOMMU and upcoming
hardware vIOMMU. The hardware model does not use AMDVIState and will
introduce new struct "AMDVIOMMUState", so there is need to have a common API
which IVRS builder can use independently.

Add AMDIVRSInfo for that. Each model supplies the IOMMU PCI device, the
root bus, the MMIO base address, and a extended_feature. acpi-build reads
those fields when it emits IVHD type 10h and 11h entries. The struct
also includes dma_translation because upcoming AMD HW-VIOMMU model
doesn't support V1 page tables, and must always set the HATDis=1
(dma_translation=0).

The emulated model fills AMDIVRSInfo from AMDVIState. Drop the exported
helper and amd_iommu-stub.c, which existed only to provide that helper
when CONFIG_AMD_IOMMU is off.

Signed-off-by: Sairaj Kodilkar <sarunkod@amd.com>
---
 hw/i386/acpi-build.c     | 26 +++++++++++++++++---------
 hw/i386/amd_iommu-stub.c | 26 --------------------------
 hw/i386/amd_iommu.c      | 16 +++++++++++++---
 hw/i386/amd_iommu.h      | 12 ++++++++++--
 hw/i386/meson.build      |  3 +--
 5 files changed, 41 insertions(+), 42 deletions(-)
 delete mode 100644 hw/i386/amd_iommu-stub.c

diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c
index 188c97f0e916..d4be50ade22f 100644
--- a/hw/i386/acpi-build.c
+++ b/hw/i386/acpi-build.c
@@ -1918,11 +1918,19 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
         X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(entry->dev);
         AMDVIState *s = AMD_IOMMU_DEVICE(entry->dev);
         GArray *ivhd_blob = g_array_new(false, true, 1);
-        uint16_t iommu_devid = pci_get_bdf(&s->pci->dev);
-        uint64_t extended_feature = amdvi_extended_feature_register(s);
+        uint16_t iommu_devid;
+        uint64_t extended_feature;
+        AMDIVRSInfo ivrs_info;
 
-        assert(s->root_bus);
-        add_ivhd_entries_from_bus(s->root_bus, ivhd_blob);
+        s->get_ivrs_info(s, &ivrs_info);
+
+        assert(ivrs_info.pci);
+        assert(ivrs_info.root_bus);
+
+        add_ivhd_entries_from_bus(ivrs_info.root_bus, ivhd_blob);
+
+        iommu_devid = pci_get_bdf(&ivrs_info.pci->dev);
+        extended_feature = ivrs_info.extended_feature;
 
         if (!ivhd_blob->len) {
             /*
@@ -1964,9 +1972,9 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
         /* DeviceID */
         build_append_int_noprefix(table_data, iommu_devid, 2);
         /* Capability offset */
-        build_append_int_noprefix(table_data, s->pci->capab_offset, 2);
+        build_append_int_noprefix(table_data, ivrs_info.pci->capab_offset, 2);
         /* IOMMU base address */
-        build_append_int_noprefix(table_data, s->mr_mmio.addr, 8);
+        build_append_int_noprefix(table_data, ivrs_info.base_addr, 8);
         /* PCI Segment Group */
         build_append_int_noprefix(table_data, 0, 2);
         /* IOMMU info */
@@ -1993,15 +2001,15 @@ build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
         /* DeviceID */
         build_append_int_noprefix(table_data, iommu_devid, 2);
         /* Capability offset */
-        build_append_int_noprefix(table_data, s->pci->capab_offset, 2);
+        build_append_int_noprefix(table_data, ivrs_info.pci->capab_offset, 2);
         /* IOMMU base address */
-        build_append_int_noprefix(table_data, s->mr_mmio.addr, 8);
+        build_append_int_noprefix(table_data, ivrs_info.base_addr, 8);
         /* PCI Segment Group */
         build_append_int_noprefix(table_data, 0, 2);
         /* IOMMU info */
         build_append_int_noprefix(table_data, 0, 2);
         /* IOMMU Attributes */
-        if (!s->iommu.dma_translation) {
+        if (!ivrs_info.dma_translation) {
             build_append_int_noprefix(table_data, (1UL << 0) /* HATDis */, 4);
         } else {
             build_append_int_noprefix(table_data, 0, 4);
diff --git a/hw/i386/amd_iommu-stub.c b/hw/i386/amd_iommu-stub.c
deleted file mode 100644
index fb36dee903ee..000000000000
--- a/hw/i386/amd_iommu-stub.c
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * Stubs for AMD IOMMU emulation
- *
- * Copyright (C) 2023 Bui Quang Minh <minhquangbui99@gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
-
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
-
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, see <http://www.gnu.org/licenses/>.
- */
-
-#include "qemu/osdep.h"
-#include "amd_iommu.h"
-
-uint64_t amdvi_extended_feature_register(AMDVIState *s)
-{
-    return 0;
-}
diff --git a/hw/i386/amd_iommu.c b/hw/i386/amd_iommu.c
index 79d3fcf9fd9b..64009f4908d2 100644
--- a/hw/i386/amd_iommu.c
+++ b/hw/i386/amd_iommu.c
@@ -133,7 +133,7 @@ FIELD(AMDVI_IRTE_GA_LO, DESTINATION, 8, 24)
 FIELD(AMDVI_IRTE_GA_HI, VECTOR, 0, 8)
 FIELD(AMDVI_IRTE_GA_HI, DESTINATION_HI, 56, 8)
 
-uint64_t amdvi_extended_feature_register(AMDVIState *s)
+static uint64_t amdvi_extended_feature_register(AMDVIState *s)
 {
     uint64_t feature = AMDVI_DEFAULT_EXT_FEATURES;
     if (s->xtsup) {
@@ -2538,6 +2538,15 @@ static int amdvi_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
     return 0;
 }
 
+static void amdvi_get_ivrs_info(AMDVIState *s, AMDIVRSInfo *ivrs_info)
+{
+    ivrs_info->pci = s->pci;
+    ivrs_info->root_bus = s->root_bus;
+    ivrs_info->base_addr = AMDVI_GET_BASE_ADDR(s->iommu.index);
+    ivrs_info->extended_feature = s->extended_feature;
+    ivrs_info->dma_translation = s->iommu.dma_translation;
+}
+
 static void amdvi_init(AMDVIState *s)
 {
     amdvi_iotlb_reset(s);
@@ -2555,12 +2564,13 @@ static void amdvi_init(AMDVIState *s)
     s->cmdbuf_enabled = false;
     s->xten = false;
     s->intcapxten = false;
+    s->extended_feature = amdvi_extended_feature_register(s);
+    s->get_ivrs_info = amdvi_get_ivrs_info;
 
     /* reset MMIO */
     memset(s->mmior, 0, AMDVI_MMIO_SIZE);
     amdvi_set_quad(s, AMDVI_MMIO_EXT_FEATURES,
-                   amdvi_extended_feature_register(s),
-                   0xffffffffffffffef, 0);
+                   s->extended_feature, 0xffffffffffffffef, 0);
     amdvi_set_quad(s, AMDVI_MMIO_STATUS, 0, 0x98, 0x67);
 }
 
diff --git a/hw/i386/amd_iommu.h b/hw/i386/amd_iommu.h
index 383605c38e6c..38ab7aa97c47 100644
--- a/hw/i386/amd_iommu.h
+++ b/hw/i386/amd_iommu.h
@@ -326,6 +326,14 @@ struct AMDVIPCIState {
     uint32_t capab_offset;       /* capability offset pointer    */
 };
 
+typedef struct AMDIVRSInfo {
+    AMDVIPCIState *pci;         /* IOMMU PCI device               */
+    PCIBus *root_bus;           /* root bus to register IOMMU ops */
+    uint64_t base_addr;         /* MMIO region                    */
+    uint64_t extended_feature;  /* Extended feature bits          */
+    bool dma_translation;       /* Host address translation enabled */
+} AMDIVRSInfo;
+
 struct AMDVIState {
     X86IOMMUState iommu;        /* IOMMU bus device             */
     AMDVIPCIState *pci;         /* IOMMU PCI device             */
@@ -395,8 +403,8 @@ struct AMDVIState {
 
     /* root bus to register IOMMU ops */
     PCIBus *root_bus;
+    uint64_t extended_feature;
+    void (*get_ivrs_info)(AMDVIState *s, AMDIVRSInfo *ivrs_info);
 };
 
-uint64_t amdvi_extended_feature_register(AMDVIState *s);
-
 #endif
diff --git a/hw/i386/meson.build b/hw/i386/meson.build
index 6bb73b8731cc..39a2886e7292 100644
--- a/hw/i386/meson.build
+++ b/hw/i386/meson.build
@@ -12,8 +12,7 @@ i386_ss.add(files(
 i386_ss.add(when: 'CONFIG_APIC', if_true: files('vapic.c'))
 i386_ss.add(when: 'CONFIG_X86_IOMMU', if_true: files('x86-iommu.c'),
                                       if_false: files('x86-iommu-stub.c'))
-i386_ss.add(when: 'CONFIG_AMD_IOMMU', if_true: files('amd_iommu.c'),
-                                      if_false: files('amd_iommu-stub.c'))
+i386_ss.add(when: 'CONFIG_AMD_IOMMU', if_true: files('amd_iommu.c'))
 i386_ss.add(when: 'CONFIG_I440FX', if_true: files('pc_piix.c'))
 i386_ss.add(when: 'CONFIG_ISAPC', if_true: files('isapc.c'))
 i386_ss.add(when: 'CONFIG_MICROVM', if_true: files('x86-common.c', 'microvm.c', 'acpi-microvm.c', 'microvm-dt.c'))
-- 
2.34.1


  parent reply	other threads:[~2026-11-30 10:17 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 10:12 [PATCH 0/6] amd_iommu: Add support for multiple AMD vIOMMUs Sairaj Kodilkar
2026-11-30 10:12 ` [PATCH 1/6] hw/i386: Track the IOMMU to which IOAPIC is attached Sairaj Kodilkar
2026-11-30 10:12 ` [PATCH 2/6] i386/kvm: x86_iommu: Introduce per-iommu notify_list_inited flag Sairaj Kodilkar
2026-11-30 10:12 ` [PATCH 3/6] acpi-build: i386/pc: Track vIOMMUs in a list Sairaj Kodilkar
2026-11-30 10:12 ` [PATCH 4/6] amd_iommu: acpi-build: Add support for multiple AMD-IOMMUs Sairaj Kodilkar
2026-11-30 10:12 ` Sairaj Kodilkar [this message]
2026-11-30 10:12 ` [PATCH 6/6] x86-iommu: Allow multiple AMD vIOMMU devices Sairaj Kodilkar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130101237.1416-6-sarunkod@amd.com \
    --to=sarunkod@amd.com \
    --cc=Suravee.Suthikulpanit@amd.com \
    --cc=Vasant.Hegde@amd.com \
    --cc=alejandro.j.jimenez@oracle.com \
    --cc=anisinha@redhat.com \
    --cc=clement.mathieu--drif@bull.com \
    --cc=imammedo@redhat.com \
    --cc=jasowangio@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=yi.l.liu@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox