From: Wu Fei <[email protected]>

Add an optional accel device for the RISC-V IOMMU. Its behaviour
matches the default path so far, leaving room for upcoming
accelerated (nested) translation. When the accel device is enabled,
each device's address space is embedded in RISCVIOMMUAccelDevice, a
per-device struct that will store per-device data for nested
translation.

Signed-off-by: Wu Fei <[email protected]>
Signed-off-by: Gong Shuai <[email protected]>
---
 hw/riscv/Kconfig             |  5 ++++
 hw/riscv/meson.build         |  1 +
 hw/riscv/riscv-iommu-accel.c | 49 ++++++++++++++++++++++++++++++++++++
 hw/riscv/riscv-iommu-accel.h | 25 ++++++++++++++++++
 hw/riscv/riscv-iommu.c       |  5 ++++
 hw/riscv/riscv-iommu.h       |  3 +++
 6 files changed, 88 insertions(+)
 create mode 100644 hw/riscv/riscv-iommu-accel.c
 create mode 100644 hw/riscv/riscv-iommu-accel.h

diff --git a/hw/riscv/Kconfig b/hw/riscv/Kconfig
index a423fafca1..bed2e713f3 100644
--- a/hw/riscv/Kconfig
+++ b/hw/riscv/Kconfig
@@ -1,5 +1,10 @@
+config RISCV_IOMMU_ACCEL
+    bool
+    depends on RISCV_IOMMU
+
 config RISCV_IOMMU
     bool
+    select RISCV_IOMMU_ACCEL if IOMMUFD
 
 config RISCV_NUMA
     bool
diff --git a/hw/riscv/meson.build b/hw/riscv/meson.build
index 6bdabb9544..b1f3b3a2d2 100644
--- a/hw/riscv/meson.build
+++ b/hw/riscv/meson.build
@@ -17,6 +17,7 @@ riscv_ss.add(when: 'CONFIG_TENSTORRENT', if_true: 
files('tt_atlantis.c'))
 riscv_ss.add(when: 'CONFIG_ACPI', if_true: files('virt-acpi-build.c'))
 riscv_ss.add(when: 'CONFIG_RISCV_IOMMU', if_true: files(
        'riscv-iommu.c', 'riscv-iommu-pci.c', 'riscv-iommu-sys.c', 
'riscv-iommu-hpm.c'))
+riscv_ss.add(when: 'CONFIG_RISCV_IOMMU_ACCEL', if_true: 
files('riscv-iommu-accel.c'))
 riscv_ss.add(when: 'CONFIG_MICROBLAZE_V', if_true: 
files('microblaze-v-generic.c'))
 riscv_ss.add(when: 'CONFIG_XIANGSHAN_KUNMINGHU', if_true: 
files('xiangshan_kmh.c'))
 riscv_ss.add(when: 'CONFIG_RISCV_MIPS_CPS', if_true: files('cps.c'))
diff --git a/hw/riscv/riscv-iommu-accel.c b/hw/riscv/riscv-iommu-accel.c
new file mode 100644
index 0000000000..41e4d03be6
--- /dev/null
+++ b/hw/riscv/riscv-iommu-accel.c
@@ -0,0 +1,49 @@
+/*
+ * RISC-V IOMMU - accelerated (nested) translation support
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+
+#include "riscv-iommu.h"
+#include "riscv-iommu-accel.h"
+
+static RISCVIOMMUAccelDevice *riscv_iommu_accel_get_dev(RISCVIOMMUState *s,
+                                                        PCIBus *bus, int devfn)
+{
+    RISCVIOMMUSpace *as;
+    RISCVIOMMUAccelDevice *accel_dev;
+
+    /* FIXME: for simplicity, assume only one iommu right now */
+    as = riscv_iommu_get_space(s, bus, devfn);
+    if (as) {
+        return container_of(as, RISCVIOMMUAccelDevice, as);
+    }
+
+    accel_dev = g_new0(RISCVIOMMUAccelDevice, 1);
+    as = &accel_dev->as;
+    riscv_iommu_init_space(s, as, bus, devfn);
+
+    return accel_dev;
+}
+
+static AddressSpace *riscv_iommu_accel_find_as(PCIBus *bus, void *opaque,
+                                               int devfn)
+{
+    RISCVIOMMUState *s = (RISCVIOMMUState *) opaque;
+
+    RISCVIOMMUAccelDevice *accel_dev = riscv_iommu_accel_get_dev(s, bus, 
devfn);
+    RISCVIOMMUSpace *as = &accel_dev->as;
+
+    return &as->iova_as;
+}
+
+static const PCIIOMMUOps riscv_iommu_accel_ops = {
+    .get_address_space = riscv_iommu_accel_find_as,
+};
+
+void riscv_iommu_accel_init(RISCVIOMMUState *s)
+{
+    s->iommu_ops = &riscv_iommu_accel_ops;
+}
diff --git a/hw/riscv/riscv-iommu-accel.h b/hw/riscv/riscv-iommu-accel.h
new file mode 100644
index 0000000000..c69fea87dd
--- /dev/null
+++ b/hw/riscv/riscv-iommu-accel.h
@@ -0,0 +1,25 @@
+/*
+ * RISC-V IOMMU - accelerated (nested) translation support
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef HW_RISCV_RISCV_IOMMU_ACCEL_H
+#define HW_RISCV_RISCV_IOMMU_ACCEL_H
+
+#include "hw/riscv/riscv-iommu.h"
+#include CONFIG_DEVICES
+
+typedef struct RISCVIOMMUAccelDevice {
+    RISCVIOMMUSpace as;
+} RISCVIOMMUAccelDevice;
+
+#ifdef CONFIG_RISCV_IOMMU_ACCEL
+void riscv_iommu_accel_init(RISCVIOMMUState *s);
+#else
+static inline void riscv_iommu_accel_init(RISCVIOMMUState *s)
+{
+}
+#endif
+
+#endif /* HW_RISCV_RISCV_IOMMU_ACCEL_H */
diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c
index 10f81f8bdf..762691c8ed 100644
--- a/hw/riscv/riscv-iommu.c
+++ b/hw/riscv/riscv-iommu.c
@@ -31,6 +31,7 @@
 
 #include "cpu_bits.h"
 #include "riscv-iommu.h"
+#include "riscv-iommu-accel.h"
 #include "riscv-iommu-bits.h"
 #include "riscv-iommu-hpm.h"
 #include "trace.h"
@@ -2707,6 +2708,10 @@ static void riscv_iommu_realize(DeviceState *dev, Error 
**errp)
 {
     RISCVIOMMUState *s = RISCV_IOMMU(dev);
 
+    if (s->accel) {
+        riscv_iommu_accel_init(s);
+    }
+
     /* Report QEMU target physical address space limits. */
     s->cap = set_field(s->cap, RISCV_IOMMU_CAP_PAS, s->pas_bits);
 
diff --git a/hw/riscv/riscv-iommu.h b/hw/riscv/riscv-iommu.h
index 3e10647975..0bc096ba60 100644
--- a/hw/riscv/riscv-iommu.h
+++ b/hw/riscv/riscv-iommu.h
@@ -111,6 +111,9 @@ struct RISCVIOMMUState {
     uint8_t hpm_cntrs;
 
     const PCIIOMMUOps *iommu_ops;
+
+    /* nested iommu support */
+    bool accel;
 };
 
 void riscv_iommu_pci_setup_iommu(RISCVIOMMUState *iommu, PCIBus *bus,
-- 
2.53.0


Reply via email to