Implement PCIIOMMUOPs [set|unset]_iommu_device() callbacks.
In set(), a VirtioHostIOMMUDevice is allocated which holds
a reference to the HostIOMMUDevice. This object is stored in a hash
table indexed by PCI BDF. The handle to the Host IOMMU device
will allow to retrieve information related to the physical IOMMU.
Signed-off-by: Eric Auger <eric.auger@redhat.com>
---
v2 -> v3:
- include host_iommu_device.h in virtio-iommu.h header
- introduce hiod_destroy() and fix UAF in
virtio_iommu_unset_iommu_device()
---
include/hw/virtio/virtio-iommu.h | 10 ++++
hw/virtio/virtio-iommu.c | 90 ++++++++++++++++++++++++++++++++
2 files changed, 100 insertions(+)
diff --git a/include/hw/virtio/virtio-iommu.h b/include/hw/virtio/virtio-iommu.h
index 83a52cc446..a5926de947 100644
--- a/include/hw/virtio/virtio-iommu.h
+++ b/include/hw/virtio/virtio-iommu.h
@@ -25,6 +25,7 @@
#include "hw/pci/pci.h"
#include "qom/object.h"
#include "qapi/qapi-types-virtio.h"
+#include "sysemu/host_iommu_device.h"
#define TYPE_VIRTIO_IOMMU "virtio-iommu-device"
#define TYPE_VIRTIO_IOMMU_PCI "virtio-iommu-pci"
@@ -45,6 +46,14 @@ typedef struct IOMMUDevice {
bool probe_done;
} IOMMUDevice;
+typedef struct VirtioHostIOMMUDevice {
+ void *viommu;
+ PCIBus *bus;
+ uint8_t devfn;
+ HostIOMMUDevice *dev;
+ QLIST_ENTRY(VirtioHostIOMMUDevice) next;
+} VirtioHostIOMMUDevice;
+
typedef struct IOMMUPciBus {
PCIBus *bus;
IOMMUDevice *pbdev[]; /* Parent array is sparse, so dynamically alloc */
@@ -57,6 +66,7 @@ struct VirtIOIOMMU {
struct virtio_iommu_config config;
uint64_t features;
GHashTable *as_by_busptr;
+ GHashTable *host_iommu_devices;
IOMMUPciBus *iommu_pcibus_by_bus_num[PCI_BUS_MAX];
PCIBus *primary_bus;
ReservedRegion *prop_resv_regions;
diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c
index 1326c6ec41..db842555c8 100644
--- a/hw/virtio/virtio-iommu.c
+++ b/hw/virtio/virtio-iommu.c
@@ -69,6 +69,11 @@ typedef struct VirtIOIOMMUMapping {
uint32_t flags;
} VirtIOIOMMUMapping;
+struct hiod_key {
+ PCIBus *bus;
+ uint8_t devfn;
+};
+
static inline uint16_t virtio_iommu_get_bdf(IOMMUDevice *dev)
{
return PCI_BUILD_BDF(pci_bus_num(dev->bus), dev->devfn);
@@ -462,8 +467,90 @@ static AddressSpace *virtio_iommu_find_add_as(PCIBus *bus, void *opaque,
return &sdev->as;
}
+static gboolean hiod_equal(gconstpointer v1, gconstpointer v2)
+{
+ const struct hiod_key *key1 = v1;
+ const struct hiod_key *key2 = v2;
+
+ return (key1->bus == key2->bus) && (key1->devfn == key2->devfn);
+}
+
+static guint hiod_hash(gconstpointer v)
+{
+ const struct hiod_key *key = v;
+ guint value = (guint)(uintptr_t)key->bus;
+
+ return (guint)(value << 8 | key->devfn);
+}
+
+static void hiod_destroy(gpointer v)
+{
+ object_unref(v);
+}
+
+static VirtioHostIOMMUDevice *
+get_host_iommu_device(VirtIOIOMMU *viommu, PCIBus *bus, int devfn) {
+ struct hiod_key key = {
+ .bus = bus,
+ .devfn = devfn,
+ };
+
+ return g_hash_table_lookup(viommu->host_iommu_devices, &key);
+}
+
+static bool virtio_iommu_set_iommu_device(PCIBus *bus, void *opaque, int devfn,
+ HostIOMMUDevice *hiod, Error **errp)
+{
+ VirtIOIOMMU *viommu = opaque;
+ VirtioHostIOMMUDevice *vhiod;
+ struct hiod_key *new_key;
+
+ assert(hiod);
+
+ vhiod = get_host_iommu_device(viommu, bus, devfn);
+ if (vhiod) {
+ error_setg(errp, "VirtioHostIOMMUDevice already exists");
+ return false;
+ }
+
+ vhiod = g_malloc0(sizeof(VirtioHostIOMMUDevice));
+ vhiod->bus = bus;
+ vhiod->devfn = (uint8_t)devfn;
+ vhiod->viommu = viommu;
+ vhiod->dev = hiod;
+
+ new_key = g_malloc(sizeof(*new_key));
+ new_key->bus = bus;
+ new_key->devfn = devfn;
+
+ object_ref(hiod);
+ g_hash_table_insert(viommu->host_iommu_devices, new_key, vhiod);
+
+ return true;
+}
+
+static void
+virtio_iommu_unset_iommu_device(PCIBus *bus, void *opaque, int devfn)
+{
+ VirtIOIOMMU *viommu = opaque;
+ VirtioHostIOMMUDevice *vhiod;
+ struct hiod_key key = {
+ .bus = bus,
+ .devfn = devfn,
+ };
+
+ vhiod = g_hash_table_lookup(viommu->host_iommu_devices, &key);
+ if (!vhiod) {
+ return;
+ }
+
+ g_hash_table_remove(viommu->host_iommu_devices, &key);
+}
+
static const PCIIOMMUOps virtio_iommu_ops = {
.get_address_space = virtio_iommu_find_add_as,
+ .set_iommu_device = virtio_iommu_set_iommu_device,
+ .unset_iommu_device = virtio_iommu_unset_iommu_device,
};
static int virtio_iommu_attach(VirtIOIOMMU *s,
@@ -1357,6 +1444,9 @@ static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)
s->as_by_busptr = g_hash_table_new_full(NULL, NULL, NULL, g_free);
+ s->host_iommu_devices = g_hash_table_new_full(hiod_hash, hiod_equal,
+ g_free, hiod_destroy);
+
if (s->primary_bus) {
pci_setup_iommu(s->primary_bus, &virtio_iommu_ops, s);
} else {
--
2.41.0
>-----Original Message----- >From: Eric Auger <eric.auger@redhat.com> >Subject: [PATCH v3 2/7] virtio-iommu: Implement >set|unset]_iommu_device() callbacks > >Implement PCIIOMMUOPs [set|unset]_iommu_device() callbacks. >In set(), a VirtioHostIOMMUDevice is allocated which holds >a reference to the HostIOMMUDevice. This object is stored in a hash >table indexed by PCI BDF. The handle to the Host IOMMU device >will allow to retrieve information related to the physical IOMMU. > >Signed-off-by: Eric Auger <eric.auger@redhat.com> > >--- > >v2 -> v3: >- include host_iommu_device.h in virtio-iommu.h header >- introduce hiod_destroy() and fix UAF in > virtio_iommu_unset_iommu_device() >--- > include/hw/virtio/virtio-iommu.h | 10 ++++ > hw/virtio/virtio-iommu.c | 90 >++++++++++++++++++++++++++++++++ > 2 files changed, 100 insertions(+) > >diff --git a/include/hw/virtio/virtio-iommu.h b/include/hw/virtio/virtio- >iommu.h >index 83a52cc446..a5926de947 100644 >--- a/include/hw/virtio/virtio-iommu.h >+++ b/include/hw/virtio/virtio-iommu.h >@@ -25,6 +25,7 @@ > #include "hw/pci/pci.h" > #include "qom/object.h" > #include "qapi/qapi-types-virtio.h" >+#include "sysemu/host_iommu_device.h" > > #define TYPE_VIRTIO_IOMMU "virtio-iommu-device" > #define TYPE_VIRTIO_IOMMU_PCI "virtio-iommu-pci" >@@ -45,6 +46,14 @@ typedef struct IOMMUDevice { > bool probe_done; > } IOMMUDevice; > >+typedef struct VirtioHostIOMMUDevice { >+ void *viommu; >+ PCIBus *bus; >+ uint8_t devfn; >+ HostIOMMUDevice *dev; >+ QLIST_ENTRY(VirtioHostIOMMUDevice) next; >+} VirtioHostIOMMUDevice; Do you have plan to use the elements in VirtioHostIOMMUDevice? >+ > typedef struct IOMMUPciBus { > PCIBus *bus; > IOMMUDevice *pbdev[]; /* Parent array is sparse, so dynamically alloc >*/ >@@ -57,6 +66,7 @@ struct VirtIOIOMMU { > struct virtio_iommu_config config; > uint64_t features; > GHashTable *as_by_busptr; >+ GHashTable *host_iommu_devices; > IOMMUPciBus *iommu_pcibus_by_bus_num[PCI_BUS_MAX]; > PCIBus *primary_bus; > ReservedRegion *prop_resv_regions; >diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c >index 1326c6ec41..db842555c8 100644 >--- a/hw/virtio/virtio-iommu.c >+++ b/hw/virtio/virtio-iommu.c >@@ -69,6 +69,11 @@ typedef struct VirtIOIOMMUMapping { > uint32_t flags; > } VirtIOIOMMUMapping; > >+struct hiod_key { >+ PCIBus *bus; >+ uint8_t devfn; >+}; >+ > static inline uint16_t virtio_iommu_get_bdf(IOMMUDevice *dev) > { > return PCI_BUILD_BDF(pci_bus_num(dev->bus), dev->devfn); >@@ -462,8 +467,90 @@ static AddressSpace >*virtio_iommu_find_add_as(PCIBus *bus, void *opaque, > return &sdev->as; > } > >+static gboolean hiod_equal(gconstpointer v1, gconstpointer v2) >+{ >+ const struct hiod_key *key1 = v1; >+ const struct hiod_key *key2 = v2; >+ >+ return (key1->bus == key2->bus) && (key1->devfn == key2->devfn); >+} >+ >+static guint hiod_hash(gconstpointer v) >+{ >+ const struct hiod_key *key = v; >+ guint value = (guint)(uintptr_t)key->bus; >+ >+ return (guint)(value << 8 | key->devfn); >+} >+ >+static void hiod_destroy(gpointer v) >+{ >+ object_unref(v); This is a bit different from intel_iommu, here v represents VirtioHostIOMMUDevice *. Thanks Zhenzhong >+} >+ >+static VirtioHostIOMMUDevice * >+get_host_iommu_device(VirtIOIOMMU *viommu, PCIBus *bus, int devfn) { >+ struct hiod_key key = { >+ .bus = bus, >+ .devfn = devfn, >+ }; >+ >+ return g_hash_table_lookup(viommu->host_iommu_devices, &key); >+} >+ >+static bool virtio_iommu_set_iommu_device(PCIBus *bus, void *opaque, >int devfn, >+ HostIOMMUDevice *hiod, Error **errp) >+{ >+ VirtIOIOMMU *viommu = opaque; >+ VirtioHostIOMMUDevice *vhiod; >+ struct hiod_key *new_key; >+ >+ assert(hiod); >+ >+ vhiod = get_host_iommu_device(viommu, bus, devfn); >+ if (vhiod) { >+ error_setg(errp, "VirtioHostIOMMUDevice already exists"); >+ return false; >+ } >+ >+ vhiod = g_malloc0(sizeof(VirtioHostIOMMUDevice)); >+ vhiod->bus = bus; >+ vhiod->devfn = (uint8_t)devfn; >+ vhiod->viommu = viommu; >+ vhiod->dev = hiod; >+ >+ new_key = g_malloc(sizeof(*new_key)); >+ new_key->bus = bus; >+ new_key->devfn = devfn; >+ >+ object_ref(hiod); >+ g_hash_table_insert(viommu->host_iommu_devices, new_key, vhiod); >+ >+ return true; >+} >+ >+static void >+virtio_iommu_unset_iommu_device(PCIBus *bus, void *opaque, int devfn) >+{ >+ VirtIOIOMMU *viommu = opaque; >+ VirtioHostIOMMUDevice *vhiod; >+ struct hiod_key key = { >+ .bus = bus, >+ .devfn = devfn, >+ }; >+ >+ vhiod = g_hash_table_lookup(viommu->host_iommu_devices, &key); >+ if (!vhiod) { >+ return; >+ } >+ >+ g_hash_table_remove(viommu->host_iommu_devices, &key); >+} >+ > static const PCIIOMMUOps virtio_iommu_ops = { > .get_address_space = virtio_iommu_find_add_as, >+ .set_iommu_device = virtio_iommu_set_iommu_device, >+ .unset_iommu_device = virtio_iommu_unset_iommu_device, > }; > > static int virtio_iommu_attach(VirtIOIOMMU *s, >@@ -1357,6 +1444,9 @@ static void >virtio_iommu_device_realize(DeviceState *dev, Error **errp) > > s->as_by_busptr = g_hash_table_new_full(NULL, NULL, NULL, g_free); > >+ s->host_iommu_devices = g_hash_table_new_full(hiod_hash, >hiod_equal, >+ g_free, hiod_destroy); >+ > if (s->primary_bus) { > pci_setup_iommu(s->primary_bus, &virtio_iommu_ops, s); > } else { >-- >2.41.0
Hi Zhenzhong, On 6/13/24 11:57, Duan, Zhenzhong wrote: > >> -----Original Message----- >> From: Eric Auger <eric.auger@redhat.com> >> Subject: [PATCH v3 2/7] virtio-iommu: Implement >> set|unset]_iommu_device() callbacks >> >> Implement PCIIOMMUOPs [set|unset]_iommu_device() callbacks. >> In set(), a VirtioHostIOMMUDevice is allocated which holds >> a reference to the HostIOMMUDevice. This object is stored in a hash >> table indexed by PCI BDF. The handle to the Host IOMMU device >> will allow to retrieve information related to the physical IOMMU. >> >> Signed-off-by: Eric Auger <eric.auger@redhat.com> >> >> --- >> >> v2 -> v3: >> - include host_iommu_device.h in virtio-iommu.h header >> - introduce hiod_destroy() and fix UAF in >> virtio_iommu_unset_iommu_device() >> --- >> include/hw/virtio/virtio-iommu.h | 10 ++++ >> hw/virtio/virtio-iommu.c | 90 >> ++++++++++++++++++++++++++++++++ >> 2 files changed, 100 insertions(+) >> >> diff --git a/include/hw/virtio/virtio-iommu.h b/include/hw/virtio/virtio- >> iommu.h >> index 83a52cc446..a5926de947 100644 >> --- a/include/hw/virtio/virtio-iommu.h >> +++ b/include/hw/virtio/virtio-iommu.h >> @@ -25,6 +25,7 @@ >> #include "hw/pci/pci.h" >> #include "qom/object.h" >> #include "qapi/qapi-types-virtio.h" >> +#include "sysemu/host_iommu_device.h" >> >> #define TYPE_VIRTIO_IOMMU "virtio-iommu-device" >> #define TYPE_VIRTIO_IOMMU_PCI "virtio-iommu-pci" >> @@ -45,6 +46,14 @@ typedef struct IOMMUDevice { >> bool probe_done; >> } IOMMUDevice; >> >> +typedef struct VirtioHostIOMMUDevice { >> + void *viommu; >> + PCIBus *bus; >> + uint8_t devfn; >> + HostIOMMUDevice *dev; >> + QLIST_ENTRY(VirtioHostIOMMUDevice) next; >> +} VirtioHostIOMMUDevice; > Do you have plan to use the elements in VirtioHostIOMMUDevice? As of now I get rid of this derived object and do something similar as what you did on vtd. > >> + >> typedef struct IOMMUPciBus { >> PCIBus *bus; >> IOMMUDevice *pbdev[]; /* Parent array is sparse, so dynamically alloc >> */ >> @@ -57,6 +66,7 @@ struct VirtIOIOMMU { >> struct virtio_iommu_config config; >> uint64_t features; >> GHashTable *as_by_busptr; >> + GHashTable *host_iommu_devices; >> IOMMUPciBus *iommu_pcibus_by_bus_num[PCI_BUS_MAX]; >> PCIBus *primary_bus; >> ReservedRegion *prop_resv_regions; >> diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c >> index 1326c6ec41..db842555c8 100644 >> --- a/hw/virtio/virtio-iommu.c >> +++ b/hw/virtio/virtio-iommu.c >> @@ -69,6 +69,11 @@ typedef struct VirtIOIOMMUMapping { >> uint32_t flags; >> } VirtIOIOMMUMapping; >> >> +struct hiod_key { >> + PCIBus *bus; >> + uint8_t devfn; >> +}; >> + >> static inline uint16_t virtio_iommu_get_bdf(IOMMUDevice *dev) >> { >> return PCI_BUILD_BDF(pci_bus_num(dev->bus), dev->devfn); >> @@ -462,8 +467,90 @@ static AddressSpace >> *virtio_iommu_find_add_as(PCIBus *bus, void *opaque, >> return &sdev->as; >> } >> >> +static gboolean hiod_equal(gconstpointer v1, gconstpointer v2) >> +{ >> + const struct hiod_key *key1 = v1; >> + const struct hiod_key *key2 = v2; >> + >> + return (key1->bus == key2->bus) && (key1->devfn == key2->devfn); >> +} >> + >> +static guint hiod_hash(gconstpointer v) >> +{ >> + const struct hiod_key *key = v; >> + guint value = (guint)(uintptr_t)key->bus; >> + >> + return (guint)(value << 8 | key->devfn); >> +} >> + >> +static void hiod_destroy(gpointer v) >> +{ >> + object_unref(v); > This is a bit different from intel_iommu, here v represents VirtioHostIOMMUDevice *. damned, you are totally right. Fixed in next version. Thank you very much! Eric > > Thanks > Zhenzhong > >> +} >> + >> +static VirtioHostIOMMUDevice * >> +get_host_iommu_device(VirtIOIOMMU *viommu, PCIBus *bus, int devfn) { >> + struct hiod_key key = { >> + .bus = bus, >> + .devfn = devfn, >> + }; >> + >> + return g_hash_table_lookup(viommu->host_iommu_devices, &key); >> +} >> + >> +static bool virtio_iommu_set_iommu_device(PCIBus *bus, void *opaque, >> int devfn, >> + HostIOMMUDevice *hiod, Error **errp) >> +{ >> + VirtIOIOMMU *viommu = opaque; >> + VirtioHostIOMMUDevice *vhiod; >> + struct hiod_key *new_key; >> + >> + assert(hiod); >> + >> + vhiod = get_host_iommu_device(viommu, bus, devfn); >> + if (vhiod) { >> + error_setg(errp, "VirtioHostIOMMUDevice already exists"); >> + return false; >> + } >> + >> + vhiod = g_malloc0(sizeof(VirtioHostIOMMUDevice)); >> + vhiod->bus = bus; >> + vhiod->devfn = (uint8_t)devfn; >> + vhiod->viommu = viommu; >> + vhiod->dev = hiod; >> + >> + new_key = g_malloc(sizeof(*new_key)); >> + new_key->bus = bus; >> + new_key->devfn = devfn; >> + >> + object_ref(hiod); >> + g_hash_table_insert(viommu->host_iommu_devices, new_key, vhiod); >> + >> + return true; >> +} >> + >> +static void >> +virtio_iommu_unset_iommu_device(PCIBus *bus, void *opaque, int devfn) >> +{ >> + VirtIOIOMMU *viommu = opaque; >> + VirtioHostIOMMUDevice *vhiod; >> + struct hiod_key key = { >> + .bus = bus, >> + .devfn = devfn, >> + }; >> + >> + vhiod = g_hash_table_lookup(viommu->host_iommu_devices, &key); >> + if (!vhiod) { >> + return; >> + } >> + >> + g_hash_table_remove(viommu->host_iommu_devices, &key); >> +} >> + >> static const PCIIOMMUOps virtio_iommu_ops = { >> .get_address_space = virtio_iommu_find_add_as, >> + .set_iommu_device = virtio_iommu_set_iommu_device, >> + .unset_iommu_device = virtio_iommu_unset_iommu_device, >> }; >> >> static int virtio_iommu_attach(VirtIOIOMMU *s, >> @@ -1357,6 +1444,9 @@ static void >> virtio_iommu_device_realize(DeviceState *dev, Error **errp) >> >> s->as_by_busptr = g_hash_table_new_full(NULL, NULL, NULL, g_free); >> >> + s->host_iommu_devices = g_hash_table_new_full(hiod_hash, >> hiod_equal, >> + g_free, hiod_destroy); >> + >> if (s->primary_bus) { >> pci_setup_iommu(s->primary_bus, &virtio_iommu_ops, s); >> } else { >> -- >> 2.41.0
On 6/13/24 11:57 AM, Duan, Zhenzhong wrote: > > >> -----Original Message----- >> From: Eric Auger <eric.auger@redhat.com> >> Subject: [PATCH v3 2/7] virtio-iommu: Implement >> set|unset]_iommu_device() callbacks >> >> Implement PCIIOMMUOPs [set|unset]_iommu_device() callbacks. >> In set(), a VirtioHostIOMMUDevice is allocated which holds >> a reference to the HostIOMMUDevice. This object is stored in a hash >> table indexed by PCI BDF. The handle to the Host IOMMU device >> will allow to retrieve information related to the physical IOMMU. >> >> Signed-off-by: Eric Auger <eric.auger@redhat.com> >> >> --- >> >> v2 -> v3: >> - include host_iommu_device.h in virtio-iommu.h header >> - introduce hiod_destroy() and fix UAF in >> virtio_iommu_unset_iommu_device() >> --- >> include/hw/virtio/virtio-iommu.h | 10 ++++ >> hw/virtio/virtio-iommu.c | 90 >> ++++++++++++++++++++++++++++++++ >> 2 files changed, 100 insertions(+) >> >> diff --git a/include/hw/virtio/virtio-iommu.h b/include/hw/virtio/virtio- >> iommu.h >> index 83a52cc446..a5926de947 100644 >> --- a/include/hw/virtio/virtio-iommu.h >> +++ b/include/hw/virtio/virtio-iommu.h >> @@ -25,6 +25,7 @@ >> #include "hw/pci/pci.h" >> #include "qom/object.h" >> #include "qapi/qapi-types-virtio.h" >> +#include "sysemu/host_iommu_device.h" >> >> #define TYPE_VIRTIO_IOMMU "virtio-iommu-device" >> #define TYPE_VIRTIO_IOMMU_PCI "virtio-iommu-pci" >> @@ -45,6 +46,14 @@ typedef struct IOMMUDevice { >> bool probe_done; >> } IOMMUDevice; >> >> +typedef struct VirtioHostIOMMUDevice { >> + void *viommu; >> + PCIBus *bus; >> + uint8_t devfn; >> + HostIOMMUDevice *dev; >> + QLIST_ENTRY(VirtioHostIOMMUDevice) next; >> +} VirtioHostIOMMUDevice; > > Do you have plan to use the elements in VirtioHostIOMMUDevice? Indeed. It would be better to avoid the unused struct definition. If removed, the new vtd and virtio un/set_iommu_device ops become very similar and there could be a common shared framework. Some of it could be handled in the PCI subsystem probably ? Thanks, C. > >> + >> typedef struct IOMMUPciBus { >> PCIBus *bus; >> IOMMUDevice *pbdev[]; /* Parent array is sparse, so dynamically alloc >> */ >> @@ -57,6 +66,7 @@ struct VirtIOIOMMU { >> struct virtio_iommu_config config; >> uint64_t features; >> GHashTable *as_by_busptr; >> + GHashTable *host_iommu_devices; >> IOMMUPciBus *iommu_pcibus_by_bus_num[PCI_BUS_MAX]; >> PCIBus *primary_bus; >> ReservedRegion *prop_resv_regions; >> diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c >> index 1326c6ec41..db842555c8 100644 >> --- a/hw/virtio/virtio-iommu.c >> +++ b/hw/virtio/virtio-iommu.c >> @@ -69,6 +69,11 @@ typedef struct VirtIOIOMMUMapping { >> uint32_t flags; >> } VirtIOIOMMUMapping; >> >> +struct hiod_key { >> + PCIBus *bus; >> + uint8_t devfn; >> +}; >> + >> static inline uint16_t virtio_iommu_get_bdf(IOMMUDevice *dev) >> { >> return PCI_BUILD_BDF(pci_bus_num(dev->bus), dev->devfn); >> @@ -462,8 +467,90 @@ static AddressSpace >> *virtio_iommu_find_add_as(PCIBus *bus, void *opaque, >> return &sdev->as; >> } >> >> +static gboolean hiod_equal(gconstpointer v1, gconstpointer v2) >> +{ >> + const struct hiod_key *key1 = v1; >> + const struct hiod_key *key2 = v2; >> + >> + return (key1->bus == key2->bus) && (key1->devfn == key2->devfn); >> +} >> + >> +static guint hiod_hash(gconstpointer v) >> +{ >> + const struct hiod_key *key = v; >> + guint value = (guint)(uintptr_t)key->bus; >> + >> + return (guint)(value << 8 | key->devfn); >> +} >> + >> +static void hiod_destroy(gpointer v) >> +{ >> + object_unref(v); > > This is a bit different from intel_iommu, here v represents VirtioHostIOMMUDevice *. > > Thanks > Zhenzhong > >> +} >> + >> +static VirtioHostIOMMUDevice * >> +get_host_iommu_device(VirtIOIOMMU *viommu, PCIBus *bus, int devfn) { >> + struct hiod_key key = { >> + .bus = bus, >> + .devfn = devfn, >> + }; >> + >> + return g_hash_table_lookup(viommu->host_iommu_devices, &key); >> +} >> + >> +static bool virtio_iommu_set_iommu_device(PCIBus *bus, void *opaque, >> int devfn, >> + HostIOMMUDevice *hiod, Error **errp) >> +{ >> + VirtIOIOMMU *viommu = opaque; >> + VirtioHostIOMMUDevice *vhiod; >> + struct hiod_key *new_key; >> + >> + assert(hiod); >> + >> + vhiod = get_host_iommu_device(viommu, bus, devfn); >> + if (vhiod) { >> + error_setg(errp, "VirtioHostIOMMUDevice already exists"); >> + return false; >> + } >> + >> + vhiod = g_malloc0(sizeof(VirtioHostIOMMUDevice)); >> + vhiod->bus = bus; >> + vhiod->devfn = (uint8_t)devfn; >> + vhiod->viommu = viommu; >> + vhiod->dev = hiod; >> + >> + new_key = g_malloc(sizeof(*new_key)); >> + new_key->bus = bus; >> + new_key->devfn = devfn; >> + >> + object_ref(hiod); >> + g_hash_table_insert(viommu->host_iommu_devices, new_key, vhiod); >> + >> + return true; >> +} >> + >> +static void >> +virtio_iommu_unset_iommu_device(PCIBus *bus, void *opaque, int devfn) >> +{ >> + VirtIOIOMMU *viommu = opaque; >> + VirtioHostIOMMUDevice *vhiod; >> + struct hiod_key key = { >> + .bus = bus, >> + .devfn = devfn, >> + }; >> + >> + vhiod = g_hash_table_lookup(viommu->host_iommu_devices, &key); >> + if (!vhiod) { >> + return; >> + } >> + >> + g_hash_table_remove(viommu->host_iommu_devices, &key); >> +} >> + >> static const PCIIOMMUOps virtio_iommu_ops = { >> .get_address_space = virtio_iommu_find_add_as, >> + .set_iommu_device = virtio_iommu_set_iommu_device, >> + .unset_iommu_device = virtio_iommu_unset_iommu_device, >> }; >> >> static int virtio_iommu_attach(VirtIOIOMMU *s, >> @@ -1357,6 +1444,9 @@ static void >> virtio_iommu_device_realize(DeviceState *dev, Error **errp) >> >> s->as_by_busptr = g_hash_table_new_full(NULL, NULL, NULL, g_free); >> >> + s->host_iommu_devices = g_hash_table_new_full(hiod_hash, >> hiod_equal, >> + g_free, hiod_destroy); >> + >> if (s->primary_bus) { >> pci_setup_iommu(s->primary_bus, &virtio_iommu_ops, s); >> } else { >> -- >> 2.41.0 >
© 2016 - 2024 Red Hat, Inc.