Skip to contentSkip to conversation

Add a host IOMMU device abstraction

PATCH Superseded Zhenzhong Duan 3 participants 37 messages Apr 8, 2024 – Apr 17, 2024 Arm/Amlogic Meson SoC support

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add a host IOMMU device abstraction

Based on Joao's suggestion, the iommufd nesting prerequisite series [1] is further splitted to host IOMMU device abstract part and vIOMMU check part. This series implements the 1st part.

This split also faciliates the dirty tracking series [2] and virtio-iommu series [3] to depend on 1st part.

The major change in this version is to use QOM, the class tree is as below:

                        HostIOMMUDevice
                               | .get_host_iommu_info()
                               |
                               |
            .------------------------------------.
            |                  |                 |
      HIODLegacyVFIO    [HIODLegacyVDPA]    HIODIOMMUFD
            | .vdev            | [.vdev]         | .iommufd
                                                 | .devid
                                                 | [.ioas_id]
                                                 | [.attach_hwpt()]
                                                 | [.detach_hwpt()]
                                                 |
                                    .----------------------.
                                    |                      |
                           HIODIOMMUFDVFIO         [HIODIOMMUFDVDPA]
                                    | .vdev                | [.vdev]

* The classes in [] will be implemented in future.
* .ioas_id, .attach/detach_hwpt() will be implemented in nesting series.
* .vdev in different class points to different agent device,
* i.e., for VFIO it points to VFIODevice.

PATCH1-4: Introduce HostIOMMUDevice and its sub classes
PATCH5-7: Implement get_host_iommu_info() callback
PATCH8-10: Create HostIOMMUDevice instance and pass to vIOMMU

Qemu code can be found at:
https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_nesting_preq_part1_v2

[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://lore.kernel.org/qemu-devel/[email protected]/
[3] https://lore.kernel.org/qemu-devel/[email protected]/

Thanks
Zhenzhong

Changelog:
v2:
- use QOM to abstract host IOMMU device and its sub-classes (Cédric)
- move host IOMMU device creation in attach_device() (Cédric)
- refine pci_device_set/unset_iommu_device doc futher (Eric)
- define host IOMMU info format of different backend
- implement get_host_iommu_info() for different backend (Cédric)

v1:
- use HostIOMMUDevice handle instead of union in VFIODevice (Eric)
- change host_iommu_device_init to host_iommu_device_create
- allocate HostIOMMUDevice in host_iommu_device_create callback and set the VFIODevice base_hdev handle (Eric)
- refine pci_device_set/unset_iommu_device doc (Eric)
- use HostIOMMUDevice handle instead of union in VTDHostIOMMUDevice (Eric)

rfcv2:
- introduce common abstract HostIOMMUDevice and sub struct for different BEs (Eric, Cédric)
- remove iommufd_device.[ch] (Cédric)
- remove duplicate iommufd/devid define from VFIODevice (Eric)
- drop the p in aliased_pbus and aliased_pdevfn (Eric)
- assert devfn and iommu_bus in pci_device_get_iommu_bus_devfn (Cédric, Eric)
- use errp in iommufd_device_get_info (Eric)
- split and simplify cap/ecap check/sync code in intel_iommu.c (Cédric)
- move VTDHostIOMMUDevice declaration to intel_iommu_internal.h (Cédric)
- make '(vtd->cap_reg >> 16) & 0x3fULL' a MACRO and add missed '+1' (Cédric)
- block migration if vIOMMU cap/ecap updated based on host IOMMU cap/ecap
- add R-B

Yi Liu (1):
hw/pci: Introduce pci_device_set/unset_iommu_device()

Zhenzhong Duan (9):
backends: Introduce abstract HostIOMMUDevice
vfio: Introduce HIODLegacyVFIO device backends/iommufd: Introduce abstract HIODIOMMUFD device vfio/iommufd: Introduce HIODIOMMUFDVFIO device
vfio: Implement get_host_iommu_info() callback backends/iommufd: Introduce helper function
iommufd_backend_get_device_info()
backends/iommufd: Implement get_host_iommu_info() callback
vfio: Create host IOMMU device instance
vfio: Pass HostIOMMUDevice to vIOMMU

 MAINTAINERS                        |  2 +
 include/hw/pci/pci.h               | 40 +++++++++++++-
 include/hw/vfio/vfio-common.h      | 23 ++++++++
 include/sysemu/host_iommu_device.h | 29 ++++++++++
 include/sysemu/iommufd.h           | 33 ++++++++++++
 backends/host_iommu_device.c       | 19 +++++++
 backends/iommufd.c                 | 85 ++++++++++++++++++++++++------
 hw/pci/pci.c                       | 75 ++++++++++++++++++++++++--
 hw/vfio/container.c                | 40 +++++++++++++-
 hw/vfio/iommufd.c                  | 19 ++++++-
 hw/vfio/pci.c                      | 20 +++++--
 backends/Kconfig                   |  5 ++
 backends/meson.build               |  1 +
 13 files changed, 364 insertions(+), 27 deletions(-)
 create mode 100644 include/sysemu/host_iommu_device.h
 create mode 100644 backends/host_iommu_device.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/10 backends: Introduce abstract HostIOMMUDevice

Introduce HostIOMMUDevice as an abstraction of host IOMMU device.

get_host_iommu_info() is used to get host IOMMU info, different backends can have different implementations and result format.

Introduce a macro CONFIG_HOST_IOMMU_DEVICE to define the usage for VFIO, and VDPA in the future.

---
 MAINTAINERS                        |  2 ++
 include/sysemu/host_iommu_device.h | 19 +++++++++++++++++++
 backends/host_iommu_device.c       | 19 +++++++++++++++++++
 backends/Kconfig                   |  5 +++++
 backends/meson.build               |  1 +
 5 files changed, 46 insertions(+)
 create mode 100644 include/sysemu/host_iommu_device.h
 create mode 100644 backends/host_iommu_device.c
MAINTAINERS +2 -0
index e71183eef9..22f71cbe02 100644
@@ -2202,6 +2202,8 @@ M: Zhenzhong Duan <[email protected]>
 S: Supported
 F: backends/iommufd.c
 F: include/sysemu/iommufd.h
+F: backends/host_iommu_device.c
+F: include/sysemu/host_iommu_device.h
 F: include/qemu/chardev_open.h
 F: util/chardev_open.c
 F: docs/devel/vfio-iommufd.rst
include/sysemu/host_iommu_device.h new file +19 -0
new file mode 100644
index 0000000000..22ccbe3a5d
@@ -0,0 +1,19 @@
+#ifndef HOST_IOMMU_DEVICE_H
+#define HOST_IOMMU_DEVICE_H
+
+#include "qom/object.h"
+
+#define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
+OBJECT_DECLARE_TYPE(HostIOMMUDevice, HostIOMMUDeviceClass, HOST_IOMMU_DEVICE)
+
+struct HostIOMMUDevice {
+    Object parent;
+};
+
+struct HostIOMMUDeviceClass {
+    ObjectClass parent_class;
+
+    int (*get_host_iommu_info)(HostIOMMUDevice *hiod, void *data, uint32_t len,
+                               Error **errp);
+};
+#endif
backends/host_iommu_device.c new file +19 -0
new file mode 100644
index 0000000000..6cb6007d8c
@@ -0,0 +1,19 @@
+#include "qemu/osdep.h"
+#include "sysemu/host_iommu_device.h"
+
+OBJECT_DEFINE_ABSTRACT_TYPE(HostIOMMUDevice,
+                            host_iommu_device,
+                            HOST_IOMMU_DEVICE,
+                            OBJECT)
+
+static void host_iommu_device_class_init(ObjectClass *oc, void *data)
+{
+}
+
+static void host_iommu_device_init(Object *obj)
+{
+}
+
+static void host_iommu_device_finalize(Object *obj)
+{
+}
backends/Kconfig +5 -0
index 2cb23f62fa..34ab29e994 100644
@@ -3,3 +3,8 @@ source tpm/Kconfig
 config IOMMUFD
     bool
     depends on VFIO
+
+config HOST_IOMMU_DEVICE
+    bool
+    default y
+    depends on VFIO
backends/meson.build +1 -0
index 8b2b111497..2e975d641e 100644
@@ -25,6 +25,7 @@ if have_vhost_user
 endif
 system_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('cryptodev-vhost.c'))
 system_ss.add(when: 'CONFIG_IOMMUFD', if_true: files('iommufd.c'))
+system_ss.add(when: 'CONFIG_HOST_IOMMU_DEVICE', if_true: files('host_iommu_device.c'))
 if have_vhost_user_crypto
   system_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('cryptodev-vhost-user.c'))
 endif
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v2 01/10] backends: Introduce abstract HostIOMMUDevice

Quoted 10 lines · Zhenzhong Duan

Introduce HostIOMMUDevice as an abstraction of host IOMMU device.

get_host_iommu_info() is used to get host IOMMU info, different backends can have different implementations and result format.

Introduce a macro CONFIG_HOST_IOMMU_DEVICE to define the usage for VFIO, and VDPA in the future.

LGTM,

Quoted 33 lines
---
  MAINTAINERS                        |  2 ++
  include/sysemu/host_iommu_device.h | 19 +++++++++++++++++++
  backends/host_iommu_device.c       | 19 +++++++++++++++++++
  backends/Kconfig                   |  5 +++++
  backends/meson.build               |  1 +
  5 files changed, 46 insertions(+)

create mode 100644 include/sysemu/host_iommu_device.h create mode 100644 backends/host_iommu_device.c

MAINTAINERS +2 -0
index e71183eef9..22f71cbe02 100644
@@ -2202,6 +2202,8 @@ M: Zhenzhong Duan <[email protected]>
  S: Supported
  F: backends/iommufd.c
  F: include/sysemu/iommufd.h
+F: backends/host_iommu_device.c
+F: include/sysemu/host_iommu_device.h
  F: include/qemu/chardev_open.h
  F: util/chardev_open.c
  F: docs/devel/vfio-iommufd.rst
include/sysemu/host_iommu_device.h new file +4 -0
new file mode 100644
index 0000000000..22ccbe3a5d
@@ -0,0 +1,19 @@
+#ifndef HOST_IOMMU_DEVICE_H
+#define HOST_IOMMU_DEVICE_H
+
+#include "qom/object.h"

include/sysemu/host_iommu_device.h line 14

Quoted 10 lines
include/sysemu/host_iommu_device.h new file +10 -0
+
+#define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
+OBJECT_DECLARE_TYPE(HostIOMMUDevice, HostIOMMUDeviceClass, HOST_IOMMU_DEVICE)
+
+struct HostIOMMUDevice {
+    Object parent;
+};
+
+struct HostIOMMUDeviceClass {
+    ObjectClass parent_class;

Could you please document the struct and its handlers ? This is more for the future reader to understand the VFIO concepts than for the generated docs. Anyhow, it could be useful for the docs also. Overall, the QEMU VFIO susbsytem suffers from a lack of documentation and we should try to improve that in the next cycle.

Thanks,

C.

Quoted 54 lines
include/sysemu/host_iommu_device.h new file +4 -0
+    int (*get_host_iommu_info)(HostIOMMUDevice *hiod, void *data, uint32_t len,
+                               Error **errp);
+};
+#endif
backends/host_iommu_device.c new file +19 -0
new file mode 100644
index 0000000000..6cb6007d8c
@@ -0,0 +1,19 @@
+#include "qemu/osdep.h"
+#include "sysemu/host_iommu_device.h"
+
+OBJECT_DEFINE_ABSTRACT_TYPE(HostIOMMUDevice,
+                            host_iommu_device,
+                            HOST_IOMMU_DEVICE,
+                            OBJECT)
+
+static void host_iommu_device_class_init(ObjectClass *oc, void *data)
+{
+}
+
+static void host_iommu_device_init(Object *obj)
+{
+}
+
+static void host_iommu_device_finalize(Object *obj)
+{
+}
backends/Kconfig +5 -0
index 2cb23f62fa..34ab29e994 100644
@@ -3,3 +3,8 @@ source tpm/Kconfig
  config IOMMUFD
      bool
      depends on VFIO
+
+config HOST_IOMMU_DEVICE
+    bool
+    default y
+    depends on VFIO
backends/meson.build +1 -0
index 8b2b111497..2e975d641e 100644
@@ -25,6 +25,7 @@ if have_vhost_user
  endif
  system_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('cryptodev-vhost.c'))
  system_ss.add(when: 'CONFIG_IOMMUFD', if_true: files('iommufd.c'))
+system_ss.add(when: 'CONFIG_HOST_IOMMU_DEVICE', if_true: files('host_iommu_device.c'))
  if have_vhost_user_crypto
    system_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('cryptodev-vhost-user.c'))
  endif
#3 lore raw

RE: [PATCH v2 01/10] backends: Introduce abstract HostIOMMUDevice

Quoted 60 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v2 01/10] backends: Introduce abstract HostIOMMUDevice

Quoted 10 lines · Zhenzhong Duan

Introduce HostIOMMUDevice as an abstraction of host IOMMU device.

get_host_iommu_info() is used to get host IOMMU info, different backends can have different implementations and result format.

Introduce a macro CONFIG_HOST_IOMMU_DEVICE to define the usage for VFIO, and VDPA in the future.

LGTM,

MAINTAINERS M: Zhenzhong Duan

Quoted 15 lines
---
  MAINTAINERS                        |  2 ++
  include/sysemu/host_iommu_device.h | 19 +++++++++++++++++++
  backends/host_iommu_device.c       | 19 +++++++++++++++++++
  backends/Kconfig                   |  5 +++++
  backends/meson.build               |  1 +
  5 files changed, 46 insertions(+)

create mode 100644 include/sysemu/host_iommu_device.h create mode 100644 backends/host_iommu_device.c

MAINTAINERS +0 -0
index e71183eef9..22f71cbe02 100644
@@ -2202,6 +2202,8 @@ M: Zhenzhong Duan

<[email protected]>

Review

Quoted 9 lines
MAINTAINERS +2 -0
  S: Supported
  F: backends/iommufd.c
  F: include/sysemu/iommufd.h
+F: backends/host_iommu_device.c
+F: include/sysemu/host_iommu_device.h
  F: include/qemu/chardev_open.h
  F: util/chardev_open.c
  F: docs/devel/vfio-iommufd.rst
(quoted diff) +0 -0

b/include/sysemu/host_iommu_device.h

include/sysemu/host_iommu_device.h line 7

Quoted 12 lines

new file mode 100644
index 0000000000..22ccbe3a5d

include/sysemu/host_iommu_device.h new file +7 -0
@@ -0,0 +1,19 @@
+#ifndef HOST_IOMMU_DEVICE_H
+#define HOST_IOMMU_DEVICE_H
+
+#include "qom/object.h"
+
+#define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
+OBJECT_DECLARE_TYPE(HostIOMMUDevice, HostIOMMUDeviceClass,

HOST_IOMMU_DEVICE)

Quoted 13 lines

include/sysemu/host_iommu_device.h line 14

Quoted 7 lines
include/sysemu/host_iommu_device.h new file +7 -0
+
+struct HostIOMMUDevice {
+    Object parent;
+};
+
+struct HostIOMMUDeviceClass {
+    ObjectClass parent_class;

Could you please document the struct and its handlers ? This is more for the future reader to understand the VFIO concepts than for the generated docs. Anyhow, it could be useful for the docs also. Overall, the QEMU VFIO susbsytem suffers from a lack of documentation and we should try to improve that in the next cycle.

Sure, will doc struct and handlers in v3.

Thanks
Zhenzhong

Quoted 66 lines

Thanks,

C.

include/sysemu/host_iommu_device.h line 15

Quoted 1 line
include/sysemu/host_iommu_device.h new file +1 -0
+    int (*get_host_iommu_info)(HostIOMMUDevice *hiod, void *data,

uint32_t len,

Review

Quoted 4 lines
include/sysemu/host_iommu_device.h new file +3 -0
+                               Error **errp);
+};
+#endif
(quoted diff) +0 -0

b/backends/host_iommu_device.c

Quoted 42 lines

new file mode 100644
index 0000000000..6cb6007d8c

backends/host_iommu_device.c new file +19 -0
@@ -0,0 +1,19 @@
+#include "qemu/osdep.h"
+#include "sysemu/host_iommu_device.h"
+
+OBJECT_DEFINE_ABSTRACT_TYPE(HostIOMMUDevice,
+                            host_iommu_device,
+                            HOST_IOMMU_DEVICE,
+                            OBJECT)
+
+static void host_iommu_device_class_init(ObjectClass *oc, void *data)
+{
+}
+
+static void host_iommu_device_init(Object *obj)
+{
+}
+
+static void host_iommu_device_finalize(Object *obj)
+{
+}
backends/Kconfig +5 -0
index 2cb23f62fa..34ab29e994 100644
@@ -3,3 +3,8 @@ source tpm/Kconfig
  config IOMMUFD
      bool
      depends on VFIO
+
+config HOST_IOMMU_DEVICE
+    bool
+    default y
+    depends on VFIO
backends/meson.build +0 -0
index 8b2b111497..2e975d641e 100644

backends/meson.build if have_vhost_user · line 26

Quoted 2 lines
backends/meson.build +0 -0
@@ -25,6 +25,7 @@ if have_vhost_user
  endif
  system_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('cryptodev-

vhost.c'))

backends/meson.build if have_vhost_user · line 28

Quoted 2 lines
backends/meson.build +1 -0
  system_ss.add(when: 'CONFIG_IOMMUFD', if_true: files('iommufd.c'))
+system_ss.add(when: 'CONFIG_HOST_IOMMU_DEVICE', if_true:

files('host_iommu_device.c'))

backends/meson.build if have_vhost_user · line 30

Quoted 2 lines
backends/meson.build +0 -0
  if have_vhost_user_crypto
    system_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true:

files('cryptodev-vhost-user.c'))

Quoted 1 line
backends/meson.build +0 -0
  endif
#4 lore raw

Re: [PATCH v2 01/10] backends: Introduce abstract HostIOMMUDevice

In reply to Zhenzhong Duan

Hi Zhenzhong,

Quoted 34 lines · Zhenzhong Duan

Introduce HostIOMMUDevice as an abstraction of host IOMMU device.

get_host_iommu_info() is used to get host IOMMU info, different backends can have different implementations and result format.

Introduce a macro CONFIG_HOST_IOMMU_DEVICE to define the usage for VFIO, and VDPA in the future.

---
  MAINTAINERS                        |  2 ++
  include/sysemu/host_iommu_device.h | 19 +++++++++++++++++++
  backends/host_iommu_device.c       | 19 +++++++++++++++++++
  backends/Kconfig                   |  5 +++++
  backends/meson.build               |  1 +
  5 files changed, 46 insertions(+)

create mode 100644 include/sysemu/host_iommu_device.h create mode 100644 backends/host_iommu_device.c

include/sysemu/host_iommu_device.h new file +7 -0
new file mode 100644
index 0000000000..22ccbe3a5d
@@ -0,0 +1,19 @@
+#ifndef HOST_IOMMU_DEVICE_H
+#define HOST_IOMMU_DEVICE_H
+
+#include "qom/object.h"
+
+#define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
+OBJECT_DECLARE_TYPE(HostIOMMUDevice, HostIOMMUDeviceClass, HOST_IOMMU_DEVICE)

include/sysemu/host_iommu_device.h line 17

Quoted 10 lines
include/sysemu/host_iommu_device.h new file +10 -0
+
+struct HostIOMMUDevice {
+    Object parent;
+};
+
+struct HostIOMMUDeviceClass {
+    ObjectClass parent_class;
+
+    int (*get_host_iommu_info)(HostIOMMUDevice *hiod, void *data, uint32_t len,
+                               Error **errp);

Please document this new method (in particular return value and @data).

Since @len is sizeof(data), can we use the size_t type?

Thanks,

Phil.

Quoted 2 lines
include/sysemu/host_iommu_device.h new file +2 -0
+};
+#endif
#5 lore raw

RE: [PATCH v2 01/10] backends: Introduce abstract HostIOMMUDevice

Quoted 56 lines

-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Subject: Re: [PATCH v2 01/10] backends: Introduce abstract HostIOMMUDevice

Hi Zhenzhong,

Quoted 17 lines · Zhenzhong Duan

Introduce HostIOMMUDevice as an abstraction of host IOMMU device.

get_host_iommu_info() is used to get host IOMMU info, different backends can have different implementations and result format.

Introduce a macro CONFIG_HOST_IOMMU_DEVICE to define the usage for VFIO, and VDPA in the future.

---
  MAINTAINERS                        |  2 ++
  include/sysemu/host_iommu_device.h | 19 +++++++++++++++++++
  backends/host_iommu_device.c       | 19 +++++++++++++++++++
  backends/Kconfig                   |  5 +++++
  backends/meson.build               |  1 +
  5 files changed, 46 insertions(+)

Review

Quoted 5 lines

create mode 100644 include/sysemu/host_iommu_device.h create mode 100644 backends/host_iommu_device.c

(quoted diff) +0 -0

b/include/sysemu/host_iommu_device.h

include/sysemu/host_iommu_device.h line 7

Quoted 12 lines

new file mode 100644
index 0000000000..22ccbe3a5d

include/sysemu/host_iommu_device.h new file +7 -0
@@ -0,0 +1,19 @@
+#ifndef HOST_IOMMU_DEVICE_H
+#define HOST_IOMMU_DEVICE_H
+
+#include "qom/object.h"
+
+#define TYPE_HOST_IOMMU_DEVICE "host-iommu-device"
+OBJECT_DECLARE_TYPE(HostIOMMUDevice, HostIOMMUDeviceClass,

HOST_IOMMU_DEVICE)

include/sysemu/host_iommu_device.h

Quoted 9 lines
include/sysemu/host_iommu_device.h new file +9 -0
+
+struct HostIOMMUDevice {
+    Object parent;
+};
+
+struct HostIOMMUDeviceClass {
+    ObjectClass parent_class;
+
+    int (*get_host_iommu_info)(HostIOMMUDevice *hiod, void *data,

uint32_t len,

Quoted 5 lines

include/sysemu/host_iommu_device.h line 17

Quoted 1 line
include/sysemu/host_iommu_device.h new file +1 -0
+                               Error **errp);

Please document this new method (in particular return value and @data).

Since @len is sizeof(data), can we use the size_t type?

Sure, will do.

Thanks
Zhenzhong

#6 lore raw

PATCH v2 2/10 vfio: Introduce HIODLegacyVFIO device

In reply to Zhenzhong Duan

HIODLegacyVFIO represents a host IOMMU device under VFIO legacy container backend.

It includes a link to VFIODevice.

---
 include/hw/vfio/vfio-common.h | 11 +++++++++++
 hw/vfio/container.c           | 11 ++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)
include/hw/vfio/vfio-common.h +11 -0
index b9da6c08ef..f30772f534 100644
@@ -31,6 +31,7 @@
 #endif
 #include "sysemu/sysemu.h"
 #include "hw/vfio/vfio-container-base.h"
+#include "sysemu/host_iommu_device.h"
 
 #define VFIO_MSG_PREFIX "vfio %s: "
 
@@ -147,6 +148,16 @@ typedef struct VFIOGroup {
     bool ram_block_discard_allowed;
 } VFIOGroup;
 
+#define TYPE_HIOD_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio"
+OBJECT_DECLARE_SIMPLE_TYPE(HIODLegacyVFIO, HIOD_LEGACY_VFIO)
+
+/* Abstraction of VFIO legacy host IOMMU device */
+struct HIODLegacyVFIO {
+    /*< private >*/
+    HostIOMMUDevice parent;
+    VFIODevice *vdev;
+};
+
 typedef struct VFIODMABuf {
     QemuDmaBuf buf;
     uint32_t pos_x, pos_y, pos_updates;
hw/vfio/container.c +10 -1
index 77bdec276e..44018ef085 100644
@@ -1143,12 +1143,21 @@ static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data)
     vioc->pci_hot_reset = vfio_legacy_pci_hot_reset;
 };
 
+static void hiod_legacy_vfio_class_init(ObjectClass *oc, void *data)
+{
+};
+
 static const TypeInfo types[] = {
     {
         .name = TYPE_VFIO_IOMMU_LEGACY,
         .parent = TYPE_VFIO_IOMMU,
         .class_init = vfio_iommu_legacy_class_init,
-    },
+    }, {
+        .name = TYPE_HIOD_LEGACY_VFIO,
+        .parent = TYPE_HOST_IOMMU_DEVICE,
+        .instance_size = sizeof(HIODLegacyVFIO),
+        .class_init = hiod_legacy_vfio_class_init,
+    }
 };
 
 DEFINE_TYPES(types)
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device

Quoted 26 lines · Zhenzhong Duan

HIODLegacyVFIO represents a host IOMMU device under VFIO legacy container backend.

It includes a link to VFIODevice.

---
  include/hw/vfio/vfio-common.h | 11 +++++++++++
  hw/vfio/container.c           | 11 ++++++++++-
  2 files changed, 21 insertions(+), 1 deletion(-)
include/hw/vfio/vfio-common.h +1 -0
index b9da6c08ef..f30772f534 100644
@@ -31,6 +31,7 @@
  #endif
  #include "sysemu/sysemu.h"
  #include "hw/vfio/vfio-container-base.h"
+#include "sysemu/host_iommu_device.h"
 
  #define VFIO_MSG_PREFIX "vfio %s: "
 

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 156

Quoted 9 lines
include/hw/vfio/vfio-common.h +6 -0
@@ -147,6 +148,16 @@ typedef struct VFIOGroup {
      bool ram_block_discard_allowed;
  } VFIOGroup;
 
+#define TYPE_HIOD_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio"
+OBJECT_DECLARE_SIMPLE_TYPE(HIODLegacyVFIO, HIOD_LEGACY_VFIO)
+
+/* Abstraction of VFIO legacy host IOMMU device */
+struct HIODLegacyVFIO {
+    /*< private >*/

Please drop this comment.

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 157

Quoted 1 line
include/hw/vfio/vfio-common.h +1 -0
+    HostIOMMUDevice parent;

Please name 'parent_obj'.

Quoted 2 lines
include/hw/vfio/vfio-common.h +2 -0
+    VFIODevice *vdev;
+};
#8 lore raw

RE: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device

Hi Philippe,

Quoted 45 lines

-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Sent: Monday, April 15, 2024 5:20 PM
To: Duan, Zhenzhong <[email protected]>; qemu- [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Tian, Kevin <[email protected]>; Liu, Yi L <[email protected]>; Peng, Chao P <[email protected]>
Subject: Re: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device

include/hw/vfio/vfio-

Quoted 14 lines · Zhenzhong Duan

HIODLegacyVFIO represents a host IOMMU device under VFIO legacy container backend.

It includes a link to VFIODevice.

---
  include/hw/vfio/vfio-common.h | 11 +++++++++++
  hw/vfio/container.c           | 11 ++++++++++-
  2 files changed, 21 insertions(+), 1 deletion(-)
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-common.h +0 -0

common.h

Quoted 12 lines

index b9da6c08ef..f30772f534 100644

include/hw/vfio/vfio-common.h +1 -0
@@ -31,6 +31,7 @@
  #endif
  #include "sysemu/sysemu.h"
  #include "hw/vfio/vfio-container-base.h"
+#include "sysemu/host_iommu_device.h"
 
  #define VFIO_MSG_PREFIX "vfio %s: "
 

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 151

Quoted 4 lines
include/hw/vfio/vfio-common.h +1 -0
@@ -147,6 +148,16 @@ typedef struct VFIOGroup {
      bool ram_block_discard_allowed;
  } VFIOGroup;
 
+#define TYPE_HIOD_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-

vfio"

Quoted 7 lines

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 156

Quoted 5 lines
include/hw/vfio/vfio-common.h +5 -0
+OBJECT_DECLARE_SIMPLE_TYPE(HIODLegacyVFIO, HIOD_LEGACY_VFIO)
+
+/* Abstraction of VFIO legacy host IOMMU device */
+struct HIODLegacyVFIO {
+    /*< private >*/

Please drop this comment.

Will do. But may I ask the rules when to use that comment and when not? I see some QOM use that comment to mark private vs. public, for example:

struct AccelState {
    /*< private >*/
    Object parent_obj;
};
typedef struct AccelClass {
    /*< private >*/
    ObjectClass parent_class;
    /*< public >*/
Quoted 4 lines

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 157

Quoted 2 lines
include/hw/vfio/vfio-common.h +1 -0
+    HostIOMMUDevice parent;

Please name 'parent_obj'.

Will do.

Thanks
Zhenzhong

Quoted 3 lines
Quoted 3 lines
include/hw/vfio/vfio-common.h +2 -0
+    VFIODevice *vdev;
+};
#9 lore raw

Re: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device

Quoted 55 lines · Zhenzhong

Hi Philippe,

Quoted 45 lines

-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Sent: Monday, April 15, 2024 5:20 PM
To: Duan, Zhenzhong <[email protected]>; qemu- [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Tian, Kevin <[email protected]>; Liu, Yi L <[email protected]>; Peng, Chao P <[email protected]>
Subject: Re: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device

include/hw/vfio/vfio-

Quoted 14 lines · Zhenzhong Duan

HIODLegacyVFIO represents a host IOMMU device under VFIO legacy container backend.

It includes a link to VFIODevice.

---
   include/hw/vfio/vfio-common.h | 11 +++++++++++
   hw/vfio/container.c           | 11 ++++++++++-
   2 files changed, 21 insertions(+), 1 deletion(-)
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-common.h +0 -0

common.h

Quoted 12 lines

index b9da6c08ef..f30772f534 100644

include/hw/vfio/vfio-common.h +1 -0
@@ -31,6 +31,7 @@
   #endif
   #include "sysemu/sysemu.h"
   #include "hw/vfio/vfio-container-base.h"
+#include "sysemu/host_iommu_device.h"
 
   #define VFIO_MSG_PREFIX "vfio %s: "
 

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 151

Quoted 4 lines
include/hw/vfio/vfio-common.h +1 -0
@@ -147,6 +148,16 @@ typedef struct VFIOGroup {
       bool ram_block_discard_allowed;
   } VFIOGroup;
 
+#define TYPE_HIOD_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-

vfio"

Quoted 7 lines

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 156

Quoted 5 lines
include/hw/vfio/vfio-common.h +5 -0
+OBJECT_DECLARE_SIMPLE_TYPE(HIODLegacyVFIO, HIOD_LEGACY_VFIO)
+
+/* Abstraction of VFIO legacy host IOMMU device */
+struct HIODLegacyVFIO {
+    /*< private >*/

Please drop this comment.

Quoted 1 line

Will do. But may I ask the rules when to use that comment and when not?

Sure, see https://www.qemu.org/docs/master/devel/style.html#qemu-object-model-declarations

Quoted 5 lines

I see some QOM use that comment to mark private vs. public, for example:

struct AccelState {
     /*< private >*/
     Object parent_obj;

This is old style which might be cleaned some day...

Quoted 8 lines

};

typedef struct AccelClass {
     /*< private >*/
     ObjectClass parent_class;
     /*< public >*/
Quoted 1 line
Quoted 5 lines
Quoted 3 lines

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 157

Quoted 1 line
include/hw/vfio/vfio-common.h +1 -0
+    HostIOMMUDevice parent;

Please name 'parent_obj'.

Will do.

Thanks,

Phil.

Quoted 8 lines

Thanks
Zhenzhong

Quoted 3 lines
Quoted 3 lines
include/hw/vfio/vfio-common.h +2 -0
+    VFIODevice *vdev;
+};
#10 lore raw

RE: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device

Quoted 64 lines

-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Subject: Re: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device

Quoted 8 lines · Zhenzhong

Hi Philippe,

Quoted 6 lines

-----Original Message-----
From: Philippe Mathieu-Daudé <[email protected]>
Sent: Monday, April 15, 2024 5:20 PM
To: Duan, Zhenzhong <[email protected]>; qemu- [email protected]
Cc: [email protected]; [email protected];

[email protected];

Quoted 31 lines
Quoted 31 lines

[email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Tian, Kevin <[email protected]>; Liu, Yi L <[email protected]>; Peng, Chao P <[email protected]>
Subject: Re: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device

include/hw/vfio/vfio-

Quoted 14 lines · Zhenzhong Duan

HIODLegacyVFIO represents a host IOMMU device under VFIO legacy container backend.

It includes a link to VFIODevice.

---
   include/hw/vfio/vfio-common.h | 11 +++++++++++
   hw/vfio/container.c           | 11 ++++++++++-
   2 files changed, 21 insertions(+), 1 deletion(-)
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-common.h +0 -0

common.h

Quoted 9 lines

index b9da6c08ef..f30772f534 100644

include/hw/vfio/vfio-common.h +1 -0
@@ -31,6 +31,7 @@
   #endif
   #include "sysemu/sysemu.h"
   #include "hw/vfio/vfio-container-base.h"
+#include "sysemu/host_iommu_device.h"
 
Quoted 7 lines
Quoted 7 lines

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 151

Quoted 7 lines
include/hw/vfio/vfio-common.h +1 -0
   #define VFIO_MSG_PREFIX "vfio %s: "
 
@@ -147,6 +148,16 @@ typedef struct VFIOGroup {
       bool ram_block_discard_allowed;
   } VFIOGroup;
 
+#define TYPE_HIOD_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-

legacy-

Quoted 9 lines
Quoted 8 lines

vfio"

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 156

Quoted 5 lines
include/hw/vfio/vfio-common.h +5 -0
+OBJECT_DECLARE_SIMPLE_TYPE(HIODLegacyVFIO, HIOD_LEGACY_VFIO)
+
+/* Abstraction of VFIO legacy host IOMMU device */
+struct HIODLegacyVFIO {
+    /*< private >*/

Please drop this comment.

Quoted 5 lines
Quoted 1 line

Will do. But may I ask the rules when to use that comment and when not?

Sure, see
https://www.qemu.org/docs/master/devel/style.html#qemu-object-model-
declarations

Learned, thanks Philippe.

BRs.
Zhenzhong

#11 lore raw

Re: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device

In reply to Zhenzhong Duan
Quoted 26 lines · Zhenzhong Duan

HIODLegacyVFIO represents a host IOMMU device under VFIO legacy container backend.

It includes a link to VFIODevice.

---
  include/hw/vfio/vfio-common.h | 11 +++++++++++
  hw/vfio/container.c           | 11 ++++++++++-
  2 files changed, 21 insertions(+), 1 deletion(-)
include/hw/vfio/vfio-common.h +1 -0
index b9da6c08ef..f30772f534 100644
@@ -31,6 +31,7 @@
  #endif
  #include "sysemu/sysemu.h"
  #include "hw/vfio/vfio-container-base.h"
+#include "sysemu/host_iommu_device.h"
 
  #define VFIO_MSG_PREFIX "vfio %s: "
 

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 151

Quoted 4 lines
include/hw/vfio/vfio-common.h +1 -0
@@ -147,6 +148,16 @@ typedef struct VFIOGroup {
      bool ram_block_discard_allowed;
  } VFIOGroup;
 
+#define TYPE_HIOD_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-vfio"

I would prefer to keep the prefix TYPE_HOST_IOMMU_DEVICE.

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 155

Quoted 4 lines
include/hw/vfio/vfio-common.h +4 -0
+OBJECT_DECLARE_SIMPLE_TYPE(HIODLegacyVFIO, HIOD_LEGACY_VFIO)
+
+/* Abstraction of VFIO legacy host IOMMU device */
+struct HIODLegacyVFIO {

same here

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 158

Quoted 3 lines
include/hw/vfio/vfio-common.h +3 -0
+    /*< private >*/
+    HostIOMMUDevice parent;
+    VFIODevice *vdev;

It seems to me that the back pointer should be on the container instead. Looks more correct conceptually.

Quoted 10 lines
include/hw/vfio/vfio-common.h +2 -0
+};
+
  typedef struct VFIODMABuf {
      QemuDmaBuf buf;
      uint32_t pos_x, pos_y, pos_updates;
hw/vfio/container.c +0 -0
index 77bdec276e..44018ef085 100644

hw/vfio/container.c static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data) · line 1148

Quoted 6 lines
hw/vfio/container.c +3 -0
@@ -1143,12 +1143,21 @@ static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data)
      vioc->pci_hot_reset = vfio_legacy_pci_hot_reset;
  };
 
+static void hiod_legacy_vfio_class_init(ObjectClass *oc, void *data)
+{
+};

Is it preferable to introduce routines when they are actually useful. Please drop the .class_init definition.

Thanks,

C.

Quoted 16 lines
hw/vfio/container.c +7 -1
+
  static const TypeInfo types[] = {
      {
          .name = TYPE_VFIO_IOMMU_LEGACY,
          .parent = TYPE_VFIO_IOMMU,
          .class_init = vfio_iommu_legacy_class_init,
-    },
+    }, {
+        .name = TYPE_HIOD_LEGACY_VFIO,
+        .parent = TYPE_HOST_IOMMU_DEVICE,
+        .instance_size = sizeof(HIODLegacyVFIO),
+        .class_init = hiod_legacy_vfio_class_init,
+    }
  };
 
  DEFINE_TYPES(types)
#12 lore raw

RE: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device

Hi Cédric,

Quoted 31 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device

include/hw/vfio/vfio-

Quoted 14 lines · Zhenzhong Duan

HIODLegacyVFIO represents a host IOMMU device under VFIO legacy container backend.

It includes a link to VFIODevice.

---
  include/hw/vfio/vfio-common.h | 11 +++++++++++
  hw/vfio/container.c           | 11 ++++++++++-
  2 files changed, 21 insertions(+), 1 deletion(-)
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-common.h +0 -0

common.h

Quoted 11 lines

index b9da6c08ef..f30772f534 100644

include/hw/vfio/vfio-common.h +1 -0
@@ -31,6 +31,7 @@
  #endif
  #include "sysemu/sysemu.h"
  #include "hw/vfio/vfio-container-base.h"
+#include "sysemu/host_iommu_device.h"
 
  #define VFIO_MSG_PREFIX "vfio %s: "
 
Quoted 8 lines

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 151

Quoted 5 lines
include/hw/vfio/vfio-common.h +1 -0
@@ -147,6 +148,16 @@ typedef struct VFIOGroup {
      bool ram_block_discard_allowed;
  } VFIOGroup;
 
+#define TYPE_HIOD_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-

vfio"

I would prefer to keep the prefix TYPE_HOST_IOMMU_DEVICE.

Will do.

Quoted 7 lines

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 155

Quoted 5 lines
include/hw/vfio/vfio-common.h +4 -0
+OBJECT_DECLARE_SIMPLE_TYPE(HIODLegacyVFIO, HIOD_LEGACY_VFIO)
+
+/* Abstraction of VFIO legacy host IOMMU device */
+struct HIODLegacyVFIO {

same here

Should I do the same for all the HostIOMMUDevice and HostIOMMUDeviceClass sub-structures?

The reason I used 'HIOD' abbreviation is some function names become extremely long and exceed 80 characters. E.g.:

@@ -1148,9 +1148,9 @@ static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data)

     vioc->pci_hot_reset = vfio_legacy_pci_hot_reset;

};

-static int hiod_legacy_vfio_get_host_iommu_info(HostIOMMUDevice *hiod,
- void *data, uint32_t len,
- Error **errp)
+static int host_iommu_device_legacy_vfio_get_host_iommu_info(HostIOMMUDevice *hiod,
+ void *data, uint32_t len,
+ Error **errp)

 {
     VFIODevice *vbasedev = HIOD_LEGACY_VFIO(hiod)->vdev;
     /* iova_ranges is a sorted list */
@@ -1173,7 +1173,7 @@ static void hiod_legacy_vfio_class_init(ObjectClass *oc, void *data)
 {
     HostIOMMUDeviceClass *hioc = HOST_IOMMU_DEVICE_CLASS(oc);

- hioc->get_host_iommu_info = hiod_legacy_vfio_get_host_iommu_info;
+ hioc->get_host_iommu_info = host_iommu_device_legacy_vfio_get_host_iommu_info;
};

I didn't find other way to make it meet the 80 chars limitation. Any suggestions on this?

Quoted 7 lines

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 158

Quoted 4 lines
include/hw/vfio/vfio-common.h +3 -0
+    /*< private >*/
+    HostIOMMUDevice parent;
+    VFIODevice *vdev;

It seems to me that the back pointer should be on the container instead. Looks more correct conceptually.

Yes, that makes sense for legacy VFIO, as iova_ranges, pgsizes etc are all saved in bcontainer.

Quoted 16 lines

hw/vfio/container.c static void

Quoted 12 lines
include/hw/vfio/vfio-common.h +2 -0
+};
+
  typedef struct VFIODMABuf {
      QemuDmaBuf buf;
      uint32_t pos_x, pos_y, pos_updates;
hw/vfio/container.c +0 -0
index 77bdec276e..44018ef085 100644
@@ -1143,12 +1143,21 @@ static void

vfio_iommu_legacy_class_init(ObjectClass *klass, void *data)

Quoted 3 lines
hw/vfio/container.c +0 -0
      vioc->pci_hot_reset = vfio_legacy_pci_hot_reset;
  };
 
Quoted 6 lines

hw/vfio/container.c static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data) · line 1148

Quoted 3 lines
hw/vfio/container.c +3 -0
+static void hiod_legacy_vfio_class_init(ObjectClass *oc, void *data)
+{
+};

Is it preferable to introduce routines when they are actually useful. Please drop the .class_init definition.

Sure.

Thanks
Zhenzhong

Quoted 22 lines

Thanks,

C.

Quoted 16 lines
hw/vfio/container.c +7 -1
+
  static const TypeInfo types[] = {
      {
          .name = TYPE_VFIO_IOMMU_LEGACY,
          .parent = TYPE_VFIO_IOMMU,
          .class_init = vfio_iommu_legacy_class_init,
-    },
+    }, {
+        .name = TYPE_HIOD_LEGACY_VFIO,
+        .parent = TYPE_HOST_IOMMU_DEVICE,
+        .instance_size = sizeof(HIODLegacyVFIO),
+        .class_init = hiod_legacy_vfio_class_init,
+    }
  };
 
  DEFINE_TYPES(types)
#13 lore raw

Re: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device

Hello,

Quoted 45 lines · Zhenzhong

Hi Cédric,

Quoted 31 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device

include/hw/vfio/vfio-

Quoted 14 lines · Zhenzhong Duan

HIODLegacyVFIO represents a host IOMMU device under VFIO legacy container backend.

It includes a link to VFIODevice.

---
   include/hw/vfio/vfio-common.h | 11 +++++++++++
   hw/vfio/container.c           | 11 ++++++++++-
   2 files changed, 21 insertions(+), 1 deletion(-)
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-common.h +0 -0

common.h

Quoted 11 lines

index b9da6c08ef..f30772f534 100644

include/hw/vfio/vfio-common.h +1 -0
@@ -31,6 +31,7 @@
   #endif
   #include "sysemu/sysemu.h"
   #include "hw/vfio/vfio-container-base.h"
+#include "sysemu/host_iommu_device.h"
 
   #define VFIO_MSG_PREFIX "vfio %s: "
 
Quoted 8 lines

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 151

Quoted 5 lines
include/hw/vfio/vfio-common.h +1 -0
@@ -147,6 +148,16 @@ typedef struct VFIOGroup {
       bool ram_block_discard_allowed;
   } VFIOGroup;
 
+#define TYPE_HIOD_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-legacy-

vfio"

I would prefer to keep the prefix TYPE_HOST_IOMMU_DEVICE.

Will do.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 155

Quoted 4 lines
include/hw/vfio/vfio-common.h +4 -0
+OBJECT_DECLARE_SIMPLE_TYPE(HIODLegacyVFIO, HIOD_LEGACY_VFIO)
+
+/* Abstraction of VFIO legacy host IOMMU device */
+struct HIODLegacyVFIO {

same here

Should I do the same for all the HostIOMMUDevice and HostIOMMUDeviceClass sub-structures?

I would for type names. The main reason is for naming consistency, which is useful for grep and code analysis.

Quoted 25 lines

The reason I used 'HIOD' abbreviation is some function names become extremely long and exceed 80 characters. E.g.:

(quoted diff) +4 -4
@@ -1148,9 +1148,9 @@ static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data)
      vioc->pci_hot_reset = vfio_legacy_pci_hot_reset;
  };
 
-static int hiod_legacy_vfio_get_host_iommu_info(HostIOMMUDevice *hiod,
-                                                void *data, uint32_t len,
-                                                Error **errp)
+static int host_iommu_device_legacy_vfio_get_host_iommu_info(HostIOMMUDevice *hiod,
+                                                             void *data, uint32_t len,
+                                                             Error **errp)
  {
      VFIODevice *vbasedev = HIOD_LEGACY_VFIO(hiod)->vdev;
      /* iova_ranges is a sorted list */
@@ -1173,7 +1173,7 @@ static void hiod_legacy_vfio_class_init(ObjectClass *oc, void *data)
  {
      HostIOMMUDeviceClass *hioc = HOST_IOMMU_DEVICE_CLASS(oc);
 
-    hioc->get_host_iommu_info = hiod_legacy_vfio_get_host_iommu_info;
+    hioc->get_host_iommu_info = host_iommu_device_legacy_vfio_get_host_iommu_info;
  };
 
Quoted 1 line

I didn't find other way to make it meet the 80 chars limitation. Any suggestions on this?

Try :

@@ -1177,7 +1177,8 @@ static void hiod_legacy_vfio_class_init(

 {
     HostIOMMUDeviceClass *hioc = HOST_IOMMU_DEVICE_CLASS(oc);

- hioc->get_host_iommu_info = hiod_legacy_vfio_get_host_iommu_info;
+ hioc->get_host_iommu_info =
+ host_iommu_device_legacy_vfio_get_host_iommu_info;
};

static const TypeInfo types[] = {

That said, I agree that 'host_iommu_device_legacy_vfio' routine prefix could be shortened to 'hiod_legacy_vfio'.

Thanks,

C.

Quoted 62 lines
Quoted 8 lines

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 158

Quoted 5 lines
include/hw/vfio/vfio-common.h +3 -0
+    /*< private >*/
+    HostIOMMUDevice parent;
+    VFIODevice *vdev;

It seems to me that the back pointer should be on the container instead. Looks more correct conceptually.

Yes, that makes sense for legacy VFIO, as iova_ranges, pgsizes etc are all saved in bcontainer.

Quoted 16 lines

hw/vfio/container.c static void

Quoted 12 lines
include/hw/vfio/vfio-common.h +2 -0
+};
+
   typedef struct VFIODMABuf {
       QemuDmaBuf buf;
       uint32_t pos_x, pos_y, pos_updates;
hw/vfio/container.c +0 -0
index 77bdec276e..44018ef085 100644
@@ -1143,12 +1143,21 @@ static void

vfio_iommu_legacy_class_init(ObjectClass *klass, void *data)

Quoted 3 lines
hw/vfio/container.c +0 -0
       vioc->pci_hot_reset = vfio_legacy_pci_hot_reset;
   };
 
Quoted 6 lines

hw/vfio/container.c static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data) · line 1148

Quoted 3 lines
hw/vfio/container.c +3 -0
+static void hiod_legacy_vfio_class_init(ObjectClass *oc, void *data)
+{
+};

Is it preferable to introduce routines when they are actually useful. Please drop the .class_init definition.

Sure.

Thanks
Zhenzhong

Quoted 22 lines

Thanks,

C.

Quoted 16 lines
hw/vfio/container.c +7 -1
+
   static const TypeInfo types[] = {
       {
           .name = TYPE_VFIO_IOMMU_LEGACY,
           .parent = TYPE_VFIO_IOMMU,
           .class_init = vfio_iommu_legacy_class_init,
-    },
+    }, {
+        .name = TYPE_HIOD_LEGACY_VFIO,
+        .parent = TYPE_HOST_IOMMU_DEVICE,
+        .instance_size = sizeof(HIODLegacyVFIO),
+        .class_init = hiod_legacy_vfio_class_init,
+    }
   };
 
   DEFINE_TYPES(types)
#14 lore raw

RE: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device

Quoted 62 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device

Hello,

Quoted 31 lines · Zhenzhong

Hi Cédric,

Quoted 29 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v2 02/10] vfio: Introduce HIODLegacyVFIO device

include/hw/vfio/vfio-

Quoted 14 lines · Zhenzhong Duan

HIODLegacyVFIO represents a host IOMMU device under VFIO legacy container backend.

It includes a link to VFIODevice.

---
   include/hw/vfio/vfio-common.h | 11 +++++++++++
   hw/vfio/container.c           | 11 ++++++++++-
   2 files changed, 21 insertions(+), 1 deletion(-)
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-common.h +0 -0

common.h

Quoted 9 lines

index b9da6c08ef..f30772f534 100644

include/hw/vfio/vfio-common.h +1 -0
@@ -31,6 +31,7 @@
   #endif
   #include "sysemu/sysemu.h"
   #include "hw/vfio/vfio-container-base.h"
+#include "sysemu/host_iommu_device.h"
 
Quoted 7 lines
Quoted 7 lines

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 151

Quoted 7 lines
include/hw/vfio/vfio-common.h +1 -0
   #define VFIO_MSG_PREFIX "vfio %s: "
 
@@ -147,6 +148,16 @@ typedef struct VFIOGroup {
       bool ram_block_discard_allowed;
   } VFIOGroup;
 
+#define TYPE_HIOD_LEGACY_VFIO TYPE_HOST_IOMMU_DEVICE "-

legacy-

Quoted 14 lines
Quoted 3 lines

vfio"

I would prefer to keep the prefix TYPE_HOST_IOMMU_DEVICE.

Will do.

Quoted 7 lines

include/hw/vfio/vfio-common.h typedef struct VFIOGroup { · line 155

Quoted 5 lines
include/hw/vfio/vfio-common.h +4 -0
+OBJECT_DECLARE_SIMPLE_TYPE(HIODLegacyVFIO, HIOD_LEGACY_VFIO)
+
+/* Abstraction of VFIO legacy host IOMMU device */
+struct HIODLegacyVFIO {

same here

Quoted 5 lines
Quoted 1 line

Should I do the same for all the HostIOMMUDevice and

HostIOMMUDeviceClass sub-structures?

I would for type names. The main reason is for naming consistency, which is useful for grep and code analysis.

Got it.

Quoted 49 lines
Quoted 3 lines

The reason I used 'HIOD' abbreviation is some function names become

extremely long

Review static void

Quoted 3 lines

and exceed 80 characters. E.g.:

(quoted diff) +0 -0
@@ -1148,9 +1148,9 @@ static void

vfio_iommu_legacy_class_init(ObjectClass *klass, void *data)

hw/vfio/container.c static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data)

Quoted 4 lines
hw/vfio/container.c +0 -1
      vioc->pci_hot_reset = vfio_legacy_pci_hot_reset;
  };
 
-static int hiod_legacy_vfio_get_host_iommu_info(HostIOMMUDevice

*hiod,

Review static void · line 1151

Quoted 3 lines
(quoted diff) +1 -2
-                                                void *data, uint32_t len,
-                                                Error **errp)
+static int

host_iommu_device_legacy_vfio_get_host_iommu_info(HostIOMMUDevice *hiod,

Review static void

Quoted 6 lines
(quoted diff) +2 -0
+                                                             void *data, uint32_t len,
+                                                             Error **errp)
  {
      VFIODevice *vbasedev = HIOD_LEGACY_VFIO(hiod)->vdev;
      /* iova_ranges is a sorted list */
@@ -1173,7 +1173,7 @@ static void

hiod_legacy_vfio_class_init(ObjectClass *oc, void *data)

Review static void · line 1176

Quoted 5 lines
(quoted diff) +1 -1
  {
      HostIOMMUDeviceClass *hioc = HOST_IOMMU_DEVICE_CLASS(oc);
 
-    hioc->get_host_iommu_info = hiod_legacy_vfio_get_host_iommu_info;
+    hioc->get_host_iommu_info =

host_iommu_device_legacy_vfio_get_host_iommu_info;

Quoted 3 lines
(quoted diff) +0 -0
  };
 

I didn't find other way to make it meet the 80 chars limitation. Any

suggestions on this?

Try :

(quoted diff) +2 -1
@@ -1177,7 +1177,8 @@ static void hiod_legacy_vfio_class_init(
 {
     HostIOMMUDeviceClass *hioc = HOST_IOMMU_DEVICE_CLASS(oc);
 
-    hioc->get_host_iommu_info = hiod_legacy_vfio_get_host_iommu_info;
+    hioc->get_host_iommu_info =
+        host_iommu_device_legacy_vfio_get_host_iommu_info;
 };
 
 static const TypeInfo types[] = {
Quoted 2 lines

That said, I agree that 'host_iommu_device_legacy_vfio' routine prefix could be shortened to 'hiod_legacy_vfio'.

Got it.

Thanks
Zhenzhong

#15 lore raw

PATCH v2 3/10 backends/iommufd: Introduce abstract HIODIOMMUFD device

In reply to Zhenzhong Duan

HIODIOMMUFD represents a host IOMMU device under iommufd backend.

Currently it includes only public iommufd handle and device id. which could be used to get hw IOMMU information.

When nested translation is supported in future, vIOMMU is going to have iommufd related operations like attaching/detaching hwpt, So IOMMUFDDevice interface will be further extended at that time.

VFIO and VDPA device have different way of attaching/detaching hwpt. So HIODIOMMUFD is still an abstract class which will be inherited by VFIO and VDPA device.

Introduce a helper hiod_iommufd_init() to initialize HIODIOMMUFD device.

---
 include/sysemu/iommufd.h | 22 +++++++++++++++++++
 backends/iommufd.c       | 47 ++++++++++++++++++++++++++--------------
 2 files changed, 53 insertions(+), 16 deletions(-)
include/sysemu/iommufd.h +22 -0
index 9af27ebd6c..71c53cbb45 100644
@@ -4,6 +4,7 @@
 #include "qom/object.h"
 #include "exec/hwaddr.h"
 #include "exec/cpu-common.h"
+#include "sysemu/host_iommu_device.h"
 
 #define TYPE_IOMMUFD_BACKEND "iommufd"
 OBJECT_DECLARE_TYPE(IOMMUFDBackend, IOMMUFDBackendClass, IOMMUFD_BACKEND)
@@ -33,4 +34,25 @@ int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova,
                             ram_addr_t size, void *vaddr, bool readonly);
 int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,
                               hwaddr iova, ram_addr_t size);
+
+#define TYPE_HIOD_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"
+OBJECT_DECLARE_TYPE(HIODIOMMUFD, HIODIOMMUFDClass, HIOD_IOMMUFD)
+
+struct HIODIOMMUFD {
+    /*< private >*/
+    HostIOMMUDevice parent;
+    void *opaque;
+
+    /*< public >*/
+    IOMMUFDBackend *iommufd;
+    uint32_t devid;
+};
+
+struct HIODIOMMUFDClass {
+    /*< private >*/
+    HostIOMMUDeviceClass parent_class;
+};
+
+void hiod_iommufd_init(HIODIOMMUFD *idev, IOMMUFDBackend *iommufd,
+                       uint32_t devid);
 #endif
backends/iommufd.c +31 -16

54 lines not on this page · raw message

Signature · 1 line
2.34.1
#16 lore raw

RE: [PATCH v2 03/10] backends/iommufd: Introduce abstract HIODIOMMUFD device

Hi All,

Quoted 53 lines

-----Original Message-----
From: Duan, Zhenzhong <[email protected]>
Subject: [PATCH v2 03/10] backends/iommufd: Introduce abstract HIODIOMMUFD device

HIODIOMMUFD represents a host IOMMU device under iommufd backend.

Currently it includes only public iommufd handle and device id. which could be used to get hw IOMMU information.

When nested translation is supported in future, vIOMMU is going to have iommufd related operations like attaching/detaching hwpt, So IOMMUFDDevice interface will be further extended at that time.

VFIO and VDPA device have different way of attaching/detaching hwpt. So HIODIOMMUFD is still an abstract class which will be inherited by VFIO and VDPA device.

Introduce a helper hiod_iommufd_init() to initialize HIODIOMMUFD device.

---
include/sysemu/iommufd.h | 22 +++++++++++++++++++
backends/iommufd.c | 47 ++++++++++++++++++++++++++--------------

2 files changed, 53 insertions(+), 16 deletions(-)
include/sysemu/iommufd.h +1 -0
index 9af27ebd6c..71c53cbb45 100644
@@ -4,6 +4,7 @@
 #include "qom/object.h"
 #include "exec/hwaddr.h"
 #include "exec/cpu-common.h"
+#include "sysemu/host_iommu_device.h"
 
 #define TYPE_IOMMUFD_BACKEND "iommufd"
 OBJECT_DECLARE_TYPE(IOMMUFDBackend, IOMMUFDBackendClass,

IOMMUFD_BACKEND)

include/sysemu/iommufd.h +0 -0
@@ -33,4 +34,25 @@ int iommufd_backend_map_dma(IOMMUFDBackend
 *be, uint32_t ioas_id, hwaddr iova,
                            ram_addr_t size, void *vaddr, bool readonly);

int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,

                              hwaddr iova, ram_addr_t size);
include/sysemu/iommufd.h +3 -0
+
+#define TYPE_HIOD_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"
+OBJECT_DECLARE_TYPE(HIODIOMMUFD, HIODIOMMUFDClass,

HIOD_IOMMUFD)

include/sysemu/iommufd.h int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova, · line 44

Quoted 5 lines
include/sysemu/iommufd.h +5 -0
+
+struct HIODIOMMUFD {
+    /*< private >*/
+    HostIOMMUDevice parent;
+    void *opaque;

Please ignore above line "void *opaque;", it's totally useless, I forgot to remove it. Sorry for noise.

Thanks
Zhenzhong

#17 lore raw

Re: [PATCH v2 03/10] backends/iommufd: Introduce abstract HIODIOMMUFD device

In reply to Zhenzhong Duan
Quoted 38 lines · Zhenzhong Duan

HIODIOMMUFD represents a host IOMMU device under iommufd backend.

Currently it includes only public iommufd handle and device id. which could be used to get hw IOMMU information.

When nested translation is supported in future, vIOMMU is going to have iommufd related operations like attaching/detaching hwpt, So IOMMUFDDevice interface will be further extended at that time.

VFIO and VDPA device have different way of attaching/detaching hwpt. So HIODIOMMUFD is still an abstract class which will be inherited by VFIO and VDPA device.

Introduce a helper hiod_iommufd_init() to initialize HIODIOMMUFD device.

---
  include/sysemu/iommufd.h | 22 +++++++++++++++++++
  backends/iommufd.c       | 47 ++++++++++++++++++++++++++--------------
  2 files changed, 53 insertions(+), 16 deletions(-)
include/sysemu/iommufd.h +1 -0
index 9af27ebd6c..71c53cbb45 100644
@@ -4,6 +4,7 @@
  #include "qom/object.h"
  #include "exec/hwaddr.h"
  #include "exec/cpu-common.h"
+#include "sysemu/host_iommu_device.h"
 
  #define TYPE_IOMMUFD_BACKEND "iommufd"
  OBJECT_DECLARE_TYPE(IOMMUFDBackend, IOMMUFDBackendClass, IOMMUFD_BACKEND)

include/sysemu/iommufd.h int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova, · line 38

Quoted 5 lines
include/sysemu/iommufd.h +2 -0
@@ -33,4 +34,25 @@ int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova,
                              ram_addr_t size, void *vaddr, bool readonly);
  int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,
                                hwaddr iova, ram_addr_t size);
+
+#define TYPE_HIOD_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"

Please keep TYPE_HOST_IOMMU_DEVICE

Quoted 6 lines
include/sysemu/iommufd.h +6 -0
+OBJECT_DECLARE_TYPE(HIODIOMMUFD, HIODIOMMUFDClass, HIOD_IOMMUFD)
+
+struct HIODIOMMUFD {
+    /*< private >*/
+    HostIOMMUDevice parent;
+    void *opaque;

include/sysemu/iommufd.h int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova, · line 54

Quoted 10 lines
include/sysemu/iommufd.h +10 -0
+
+    /*< public >*/
+    IOMMUFDBackend *iommufd;
+    uint32_t devid;
+};
+
+struct HIODIOMMUFDClass {
+    /*< private >*/
+    HostIOMMUDeviceClass parent_class;
+};

This new class doesn't seem useful. Do you have plans for handlers ?

Quoted 21 lines
include/sysemu/iommufd.h +3 -0
+
+void hiod_iommufd_init(HIODIOMMUFD *idev, IOMMUFDBackend *iommufd,
+                       uint32_t devid);
  #endif
backends/iommufd.c +0 -9
index 62a79fa6b0..ef8b3a808b 100644
@@ -212,23 +212,38 @@ int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,
      return ret;
  }
 
-static const TypeInfo iommufd_backend_info = {
-    .name = TYPE_IOMMUFD_BACKEND,
-    .parent = TYPE_OBJECT,
-    .instance_size = sizeof(IOMMUFDBackend),
-    .instance_init = iommufd_backend_init,
-    .instance_finalize = iommufd_backend_finalize,
-    .class_size = sizeof(IOMMUFDBackendClass),
-    .class_init = iommufd_backend_class_init,
-    .interfaces = (InterfaceInfo[]) {

backends/iommufd.c int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id, · line 220

Quoted 10 lines
backends/iommufd.c +6 -4
-        { TYPE_USER_CREATABLE },
-        { }
-    }
-};
+void hiod_iommufd_init(HIODIOMMUFD *idev, IOMMUFDBackend *iommufd,
+                       uint32_t devid)
+{
+    idev->iommufd = iommufd;
+    idev->devid = devid;
+}

This routine doesn't seem useful. I wonder if we shouldn't introduce properties. I'm not sure this is useful either.

Quoted 31 lines
backends/iommufd.c +25 -3
-static void register_types(void)
+static void hiod_iommufd_class_init(ObjectClass *oc, void *data)
  {
-    type_register_static(&iommufd_backend_info);
  }
 
-type_init(register_types);
+static const TypeInfo types[] = {
+    {
+        .name = TYPE_IOMMUFD_BACKEND,
+        .parent = TYPE_OBJECT,
+        .instance_size = sizeof(IOMMUFDBackend),
+        .instance_init = iommufd_backend_init,
+        .instance_finalize = iommufd_backend_finalize,
+        .class_size = sizeof(IOMMUFDBackendClass),
+        .class_init = iommufd_backend_class_init,
+        .interfaces = (InterfaceInfo[]) {
+            { TYPE_USER_CREATABLE },
+            { }
+        }
+    }, {
+        .name = TYPE_HIOD_IOMMUFD,
+        .parent = TYPE_HOST_IOMMU_DEVICE,
+        .instance_size = sizeof(HIODIOMMUFD),
+        .class_size = sizeof(HIODIOMMUFDClass),
+        .class_init = hiod_iommufd_class_init,
+        .abstract = true,
+    }
+};
+
+DEFINE_TYPES(types)
#18 lore raw

RE: [PATCH v2 03/10] backends/iommufd: Introduce abstract HIODIOMMUFD device

Quoted 49 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v2 03/10] backends/iommufd: Introduce abstract HIODIOMMUFD device

Quoted 30 lines · Zhenzhong Duan

HIODIOMMUFD represents a host IOMMU device under iommufd backend.

Currently it includes only public iommufd handle and device id. which could be used to get hw IOMMU information.

When nested translation is supported in future, vIOMMU is going to have iommufd related operations like attaching/detaching hwpt, So IOMMUFDDevice interface will be further extended at that time.

VFIO and VDPA device have different way of attaching/detaching hwpt. So HIODIOMMUFD is still an abstract class which will be inherited by VFIO and VDPA device.

Introduce a helper hiod_iommufd_init() to initialize HIODIOMMUFD device.

---
  include/sysemu/iommufd.h | 22 +++++++++++++++++++
  backends/iommufd.c       | 47 ++++++++++++++++++++++++++--------------
  2 files changed, 53 insertions(+), 16 deletions(-)
include/sysemu/iommufd.h +0 -0
index 9af27ebd6c..71c53cbb45 100644

include/sysemu/iommufd.h line 10

Quoted 7 lines
include/sysemu/iommufd.h +1 -0
@@ -4,6 +4,7 @@
  #include "qom/object.h"
  #include "exec/hwaddr.h"
  #include "exec/cpu-common.h"
+#include "sysemu/host_iommu_device.h"
 
  #define TYPE_IOMMUFD_BACKEND "iommufd"
  OBJECT_DECLARE_TYPE(IOMMUFDBackend, IOMMUFDBackendClass,

IOMMUFD_BACKEND)

include/sysemu/iommufd.h int

Quoted 1 line
include/sysemu/iommufd.h +0 -0
@@ -33,4 +34,25 @@ int

iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova,

Quoted 8 lines

include/sysemu/iommufd.h int · line 35

Quoted 2 lines
include/sysemu/iommufd.h +0 -0
                              ram_addr_t size, void *vaddr, bool readonly);
  int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t

ioas_id,

include/sysemu/iommufd.h int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova, · line 38

Quoted 3 lines
include/sysemu/iommufd.h +2 -0
                                hwaddr iova, ram_addr_t size);
+
+#define TYPE_HIOD_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"

Please keep TYPE_HOST_IOMMU_DEVICE

Sure.

Quoted 12 lines

include/sysemu/iommufd.h int · line 39

Quoted 2 lines
include/sysemu/iommufd.h +1 -0
+OBJECT_DECLARE_TYPE(HIODIOMMUFD, HIODIOMMUFDClass,

HIOD_IOMMUFD)

Quoted 9 lines
include/sysemu/iommufd.h +9 -0
+
+struct HIODIOMMUFD {
+    /*< private >*/
+    HostIOMMUDevice parent;
+    void *opaque;
+
+    /*< public >*/
+    IOMMUFDBackend *iommufd;
+    uint32_t devid;
Quoted 8 lines

include/sysemu/iommufd.h int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova, · line 54

Quoted 6 lines
include/sysemu/iommufd.h +6 -0
+};
+
+struct HIODIOMMUFDClass {
+    /*< private >*/
+    HostIOMMUDeviceClass parent_class;
+};

This new class doesn't seem useful. Do you have plans for handlers ?

Yes, In nesting series https://github.com/yiliu1765/qemu/commits/zhenzhong/iommufd_nesting_rfcv2/
This commit https://github.com/yiliu1765/qemu/commit/581fc900aa296988eaa48abee6d68d3670faf8c9
implement [at|de]tach_hwpt handlers.

So I add an extra layer of abstract HIODIOMMUFDClass.

Quoted 30 lines

include/sysemu/iommufd.h int · line 56

Quoted 3 lines
include/sysemu/iommufd.h +2 -0
+
+void hiod_iommufd_init(HIODIOMMUFD *idev, IOMMUFDBackend

*iommufd,

backends/iommufd.c int

Quoted 7 lines
include/sysemu/iommufd.h +1 -0
+                       uint32_t devid);
  #endif
backends/iommufd.c +0 -0
index 62a79fa6b0..ef8b3a808b 100644
@@ -212,23 +212,38 @@ int

iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,

Quoted 7 lines
backends/iommufd.c +0 -4
      return ret;
  }
 
-static const TypeInfo iommufd_backend_info = {
-    .name = TYPE_IOMMUFD_BACKEND,
-    .parent = TYPE_OBJECT,
-    .instance_size = sizeof(IOMMUFDBackend),

backends/iommufd.c int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,

Quoted 10 lines
backends/iommufd.c +1 -9
-    .instance_init = iommufd_backend_init,
-    .instance_finalize = iommufd_backend_finalize,
-    .class_size = sizeof(IOMMUFDBackendClass),
-    .class_init = iommufd_backend_class_init,
-    .interfaces = (InterfaceInfo[]) {
-        { TYPE_USER_CREATABLE },
-        { }
-    }
-};
+void hiod_iommufd_init(HIODIOMMUFD *idev, IOMMUFDBackend

*iommufd,

Quoted 8 lines

backends/iommufd.c int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id, · line 220

Quoted 5 lines
backends/iommufd.c +5 -0
+                       uint32_t devid)
+{
+    idev->iommufd = iommufd;
+    idev->devid = devid;
+}

This routine doesn't seem useful. I wonder if we shouldn't introduce properties. I'm not sure this is useful either.

This routine is called in patch8 to initialize iommu, devid and ioas(in future nesting series). I didn't choose properties as HIODIOMMUFD is not user creatable, property is a bit heavy here. But I'm fine to use it if you prefer.

Thanks
Zhenzhong

Quoted 33 lines
Quoted 33 lines
backends/iommufd.c +25 -3
-static void register_types(void)
+static void hiod_iommufd_class_init(ObjectClass *oc, void *data)
  {
-    type_register_static(&iommufd_backend_info);
  }
 
-type_init(register_types);
+static const TypeInfo types[] = {
+    {
+        .name = TYPE_IOMMUFD_BACKEND,
+        .parent = TYPE_OBJECT,
+        .instance_size = sizeof(IOMMUFDBackend),
+        .instance_init = iommufd_backend_init,
+        .instance_finalize = iommufd_backend_finalize,
+        .class_size = sizeof(IOMMUFDBackendClass),
+        .class_init = iommufd_backend_class_init,
+        .interfaces = (InterfaceInfo[]) {
+            { TYPE_USER_CREATABLE },
+            { }
+        }
+    }, {
+        .name = TYPE_HIOD_IOMMUFD,
+        .parent = TYPE_HOST_IOMMU_DEVICE,
+        .instance_size = sizeof(HIODIOMMUFD),
+        .class_size = sizeof(HIODIOMMUFDClass),
+        .class_init = hiod_iommufd_class_init,
+        .abstract = true,
+    }
+};
+
+DEFINE_TYPES(types)
#19 lore raw

PATCH v2 4/10 vfio/iommufd: Introduce HIODIOMMUFDVFIO device

In reply to Zhenzhong Duan

HIODIOMMUFDVFIO represents a host IOMMU device under VFIO iommufd backend. It will be created during VFIO device attaching and passed to vIOMMU.

It includes a link to VFIODevice so that we can do VFIO device specific hwpt attaching/detaching.

---
 include/hw/vfio/vfio-common.h | 11 +++++++++++
 hw/vfio/iommufd.c             | 11 ++++++++++-
 2 files changed, 21 insertions(+), 1 deletion(-)
include/hw/vfio/vfio-common.h +11 -0
index f30772f534..d382b12ec1 100644
@@ -32,6 +32,7 @@
 #include "sysemu/sysemu.h"
 #include "hw/vfio/vfio-container-base.h"
 #include "sysemu/host_iommu_device.h"
+#include "sysemu/iommufd.h"
 
 #define VFIO_MSG_PREFIX "vfio %s: "
 
@@ -158,6 +159,16 @@ struct HIODLegacyVFIO {
     VFIODevice *vdev;
 };
 
+#define TYPE_HIOD_IOMMUFD_VFIO TYPE_HIOD_IOMMUFD "-vfio"
+OBJECT_DECLARE_SIMPLE_TYPE(HIODIOMMUFDVFIO, HIOD_IOMMUFD_VFIO)
+
+/* Abstraction of VFIO IOMMUFD host IOMMU device */
+struct HIODIOMMUFDVFIO {
+    /*< private >*/
+    HIODIOMMUFD parent;
+    VFIODevice *vdev;
+};
+
 typedef struct VFIODMABuf {
     QemuDmaBuf buf;
     uint32_t pos_x, pos_y, pos_updates;
hw/vfio/iommufd.c +10 -1
index 8827ffe636..115b9f8e7f 100644
@@ -634,12 +634,21 @@ static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)
     vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset;
 };
 
+static void hiod_iommufd_vfio_class_init(ObjectClass *oc, void *data)
+{
+};
+
 static const TypeInfo types[] = {
     {
         .name = TYPE_VFIO_IOMMU_IOMMUFD,
         .parent = TYPE_VFIO_IOMMU,
         .class_init = vfio_iommu_iommufd_class_init,
-    },
+    }, {
+        .name = TYPE_HIOD_IOMMUFD_VFIO,
+        .parent = TYPE_HIOD_IOMMUFD,
+        .instance_size = sizeof(HIODIOMMUFDVFIO),
+        .class_init = hiod_iommufd_vfio_class_init,
+    }
 };
 
 DEFINE_TYPES(types)
Signature · 1 line
2.34.1
#20 lore raw

PATCH v2 5/10 vfio: Implement get_host_iommu_info() callback

In reply to Zhenzhong Duan

Utilize iova_ranges to calculate host IOMMU address width and package it in HIOD_LEGACY_INFO for vIOMMU usage.

HIOD_LEGACY_INFO will be used by both VFIO and VDPA so declare it in host_iommu_device.h.

---
 include/sysemu/host_iommu_device.h | 10 ++++++++++
 hw/vfio/container.c                | 24 ++++++++++++++++++++++++
 2 files changed, 34 insertions(+)
include/sysemu/host_iommu_device.h +10 -0
index 22ccbe3a5d..beb8be8231 100644
@@ -16,4 +16,14 @@ struct HostIOMMUDeviceClass {
     int (*get_host_iommu_info)(HostIOMMUDevice *hiod, void *data, uint32_t len,
                                Error **errp);
 };
+
+/*
+ * Define the format of host IOMMU related info that current VFIO
+ * or VDPA can privode to vIOMMU.
+ *
+ * @aw_bits: Host IOMMU address width. 0xff if no limitation.
+ */
+typedef struct HIOD_LEGACY_INFO {
+    uint8_t aw_bits;
+} HIOD_LEGACY_INFO;
 #endif
hw/vfio/container.c +24 -0
index 44018ef085..ba0ad4a41b 100644
@@ -1143,8 +1143,32 @@ static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data)
     vioc->pci_hot_reset = vfio_legacy_pci_hot_reset;
 };
 
+static int hiod_legacy_vfio_get_host_iommu_info(HostIOMMUDevice *hiod,
+                                                void *data, uint32_t len,
+                                                Error **errp)
+{
+    VFIODevice *vbasedev = HIOD_LEGACY_VFIO(hiod)->vdev;
+    /* iova_ranges is a sorted list */
+    GList *l = g_list_last(vbasedev->bcontainer->iova_ranges);
+    HIOD_LEGACY_INFO *info = data;
+
+    assert(sizeof(HIOD_LEGACY_INFO) <= len);
+
+    if (l) {
+        Range *range = l->data;
+        info->aw_bits = find_last_bit(&range->upb, BITS_PER_LONG) + 1;
+    } else {
+        info->aw_bits = 0xff;
+    }
+
+    return 0;
+}
+
 static void hiod_legacy_vfio_class_init(ObjectClass *oc, void *data)
 {
+    HostIOMMUDeviceClass *hioc = HOST_IOMMU_DEVICE_CLASS(oc);
+
+    hioc->get_host_iommu_info = hiod_legacy_vfio_get_host_iommu_info;
 };
 
 static const TypeInfo types[] = {
Signature · 1 line
2.34.1
#21 lore raw

Re: [PATCH v2 05/10] vfio: Implement get_host_iommu_info() callback

Quoted 18 lines · Zhenzhong Duan

Utilize iova_ranges to calculate host IOMMU address width and package it in HIOD_LEGACY_INFO for vIOMMU usage.

HIOD_LEGACY_INFO will be used by both VFIO and VDPA so declare it in host_iommu_device.h.

---
  include/sysemu/host_iommu_device.h | 10 ++++++++++
  hw/vfio/container.c                | 24 ++++++++++++++++++++++++
  2 files changed, 34 insertions(+)
include/sysemu/host_iommu_device.h +0 -0
index 22ccbe3a5d..beb8be8231 100644
@@ -16,4 +16,14 @@ struct HostIOMMUDeviceClass {
      int (*get_host_iommu_info)(HostIOMMUDevice *hiod, void *data, uint32_t len,

include/sysemu/host_iommu_device.h struct HostIOMMUDeviceClass { · line 26

Quoted 10 lines
include/sysemu/host_iommu_device.h +8 -0
                                 Error **errp);
  };
+
+/*
+ * Define the format of host IOMMU related info that current VFIO
+ * or VDPA can privode to vIOMMU.
+ *
+ * @aw_bits: Host IOMMU address width. 0xff if no limitation.
+ */
+typedef struct HIOD_LEGACY_INFO {

Please use CamelCase names.

Quoted 15 lines
include/sysemu/host_iommu_device.h +2 -0
+    uint8_t aw_bits;
+} HIOD_LEGACY_INFO;
  #endif
hw/vfio/container.c +4 -0
index 44018ef085..ba0ad4a41b 100644
@@ -1143,8 +1143,32 @@ static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data)
      vioc->pci_hot_reset = vfio_legacy_pci_hot_reset;
  };
 
+static int hiod_legacy_vfio_get_host_iommu_info(HostIOMMUDevice *hiod,
+                                                void *data, uint32_t len,
+                                                Error **errp)
+{

hw/vfio/container.c static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data) · line 1159

Quoted 10 lines
hw/vfio/container.c +10 -0
+    VFIODevice *vbasedev = HIOD_LEGACY_VFIO(hiod)->vdev;
+    /* iova_ranges is a sorted list */
+    GList *l = g_list_last(vbasedev->bcontainer->iova_ranges);
+    HIOD_LEGACY_INFO *info = data;
+
+    assert(sizeof(HIOD_LEGACY_INFO) <= len);
+
+    if (l) {
+        Range *range = l->data;
+        info->aw_bits = find_last_bit(&range->upb, BITS_PER_LONG) + 1;

There is a comment in range.h saying:

/*
* Do not access members directly, use the functions!

Please introduce a new helper.

Thanks,

C.

Quoted 15 lines
hw/vfio/container.c +10 -0
+    } else {
+        info->aw_bits = 0xff;
+    }
+
+    return 0;
+}
+
  static void hiod_legacy_vfio_class_init(ObjectClass *oc, void *data)
  {
+    HostIOMMUDeviceClass *hioc = HOST_IOMMU_DEVICE_CLASS(oc);
+
+    hioc->get_host_iommu_info = hiod_legacy_vfio_get_host_iommu_info;
  };
 
  static const TypeInfo types[] = {
#22 lore raw

RE: [PATCH v2 05/10] vfio: Implement get_host_iommu_info() callback

Quoted 28 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v2 05/10] vfio: Implement get_host_iommu_info() callback

Quoted 6 lines · Zhenzhong Duan

Utilize iova_ranges to calculate host IOMMU address width and package it in HIOD_LEGACY_INFO for vIOMMU usage.

HIOD_LEGACY_INFO will be used by both VFIO and VDPA so declare it in host_iommu_device.h.

Review

Quoted 7 lines
---
  include/sysemu/host_iommu_device.h | 10 ++++++++++
  hw/vfio/container.c                | 24 ++++++++++++++++++++++++
  2 files changed, 34 insertions(+)
(quoted diff) +0 -0

b/include/sysemu/host_iommu_device.h

include/sysemu/host_iommu_device.h struct HostIOMMUDeviceClass { · line 16

Quoted 5 lines

index 22ccbe3a5d..beb8be8231 100644

include/sysemu/host_iommu_device.h +0 -0
@@ -16,4 +16,14 @@ struct HostIOMMUDeviceClass {
      int (*get_host_iommu_info)(HostIOMMUDevice *hiod, void *data,

uint32_t len,

Quoted 12 lines

include/sysemu/host_iommu_device.h struct HostIOMMUDeviceClass { · line 26

Quoted 10 lines
include/sysemu/host_iommu_device.h +8 -0
                                 Error **errp);
  };
+
+/*
+ * Define the format of host IOMMU related info that current VFIO
+ * or VDPA can privode to vIOMMU.
+ *
+ * @aw_bits: Host IOMMU address width. 0xff if no limitation.
+ */
+typedef struct HIOD_LEGACY_INFO {

Please use CamelCase names.

Sure.

Quoted 31 lines

hw/vfio/container.c static void

Quoted 9 lines
include/sysemu/host_iommu_device.h +2 -0
+    uint8_t aw_bits;
+} HIOD_LEGACY_INFO;
  #endif
hw/vfio/container.c +0 -0
index 44018ef085..ba0ad4a41b 100644
@@ -1143,8 +1143,32 @@ static void

vfio_iommu_legacy_class_init(ObjectClass *klass, void *data)

hw/vfio/container.c static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data)

Quoted 4 lines
hw/vfio/container.c +1 -0
      vioc->pci_hot_reset = vfio_legacy_pci_hot_reset;
  };
 
+static int hiod_legacy_vfio_get_host_iommu_info(HostIOMMUDevice

*hiod,

hw/vfio/container.c static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data) · line 1159

Quoted 13 lines
hw/vfio/container.c +13 -0
+                                                void *data, uint32_t len,
+                                                Error **errp)
+{
+    VFIODevice *vbasedev = HIOD_LEGACY_VFIO(hiod)->vdev;
+    /* iova_ranges is a sorted list */
+    GList *l = g_list_last(vbasedev->bcontainer->iova_ranges);
+    HIOD_LEGACY_INFO *info = data;
+
+    assert(sizeof(HIOD_LEGACY_INFO) <= len);
+
+    if (l) {
+        Range *range = l->data;
+        info->aw_bits = find_last_bit(&range->upb, BITS_PER_LONG) + 1;

There is a comment in range.h saying:

Quoted 4 lines

/*
* Do not access members directly, use the functions!

Please introduce a new helper.

Sure, thanks for point out.

BRs.
Zhenzhong

Quoted 24 lines

Thanks,

C.

hw/vfio/container.c static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data)

Quoted 12 lines
hw/vfio/container.c +10 -0
+    } else {
+        info->aw_bits = 0xff;
+    }
+
+    return 0;
+}
+
  static void hiod_legacy_vfio_class_init(ObjectClass *oc, void *data)
  {
+    HostIOMMUDeviceClass *hioc = HOST_IOMMU_DEVICE_CLASS(oc);
+
+    hioc->get_host_iommu_info =

hiod_legacy_vfio_get_host_iommu_info;

Quoted 3 lines
hw/vfio/container.c +0 -0
  };
 
  static const TypeInfo types[] = {
#23 lore raw

PATCH v2 6/10 backends/iommufd: Introduce helper function iommufd_backend_get_device_info()

In reply to Zhenzhong Duan

Introduce a helper function iommufd_backend_get_device_info() to get host IOMMU related information through iommufd uAPI.

---
 include/sysemu/iommufd.h |  4 ++++
 backends/iommufd.c       | 23 ++++++++++++++++++++++-
 2 files changed, 26 insertions(+), 1 deletion(-)
include/sysemu/iommufd.h +4 -0
index 71c53cbb45..fa1a866237 100644
@@ -4,6 +4,7 @@
 #include "qom/object.h"
 #include "exec/hwaddr.h"
 #include "exec/cpu-common.h"
+#include <linux/iommufd.h>
 #include "sysemu/host_iommu_device.h"
 
 #define TYPE_IOMMUFD_BACKEND "iommufd"
@@ -34,6 +35,9 @@ int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova,
                             ram_addr_t size, void *vaddr, bool readonly);
 int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,
                               hwaddr iova, ram_addr_t size);
+int iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
+                                    enum iommu_hw_info_type *type,
+                                    void *data, uint32_t len, Error **errp);
 
 #define TYPE_HIOD_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"
 OBJECT_DECLARE_TYPE(HIODIOMMUFD, HIODIOMMUFDClass, HIOD_IOMMUFD)
backends/iommufd.c +22 -1
index ef8b3a808b..559affa9ec 100644
@@ -20,7 +20,6 @@
 #include "monitor/monitor.h"
 #include "trace.h"
 #include <sys/ioctl.h>
-#include <linux/iommufd.h>
 
 static void iommufd_backend_init(Object *obj)
 {
@@ -212,6 +211,28 @@ int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,
     return ret;
 }
 
+int iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
+                                    enum iommu_hw_info_type *type,
+                                    void *data, uint32_t len, Error **errp)
+{
+    struct iommu_hw_info info = {
+        .size = sizeof(info),
+        .dev_id = devid,
+        .data_len = len,
+        .data_uptr = (uintptr_t)data,
+    };
+    int ret;
+
+    ret = ioctl(be->fd, IOMMU_GET_HW_INFO, &info);
+    if (ret) {
+        error_setg_errno(errp, errno, "Failed to get hardware info");
+    } else {
+        *type = info.out_data_type;
+    }
+
+    return ret;
+}
+
 void hiod_iommufd_init(HIODIOMMUFD *idev, IOMMUFDBackend *iommufd,
                        uint32_t devid)
 {
Signature · 1 line
2.34.1
#24 lore raw

Re: [PATCH v2 06/10] backends/iommufd: Introduce helper function iommufd_backend_get_device_info()

Quoted 56 lines · Zhenzhong Duan

Introduce a helper function iommufd_backend_get_device_info() to get host IOMMU related information through iommufd uAPI.

---
  include/sysemu/iommufd.h |  4 ++++
  backends/iommufd.c       | 23 ++++++++++++++++++++++-
  2 files changed, 26 insertions(+), 1 deletion(-)
include/sysemu/iommufd.h +4 -0
index 71c53cbb45..fa1a866237 100644
@@ -4,6 +4,7 @@
  #include "qom/object.h"
  #include "exec/hwaddr.h"
  #include "exec/cpu-common.h"
+#include <linux/iommufd.h>
  #include "sysemu/host_iommu_device.h"
 
  #define TYPE_IOMMUFD_BACKEND "iommufd"
@@ -34,6 +35,9 @@ int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova,
                              ram_addr_t size, void *vaddr, bool readonly);
  int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,
                                hwaddr iova, ram_addr_t size);
+int iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
+                                    enum iommu_hw_info_type *type,
+                                    void *data, uint32_t len, Error **errp);
 
  #define TYPE_HIOD_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"
  OBJECT_DECLARE_TYPE(HIODIOMMUFD, HIODIOMMUFDClass, HIOD_IOMMUFD)
backends/iommufd.c +7 -1
index ef8b3a808b..559affa9ec 100644
@@ -20,7 +20,6 @@
  #include "monitor/monitor.h"
  #include "trace.h"
  #include <sys/ioctl.h>
-#include <linux/iommufd.h>
 
  static void iommufd_backend_init(Object *obj)
  {
@@ -212,6 +211,28 @@ int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,
      return ret;
  }
 
+int iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
+                                    enum iommu_hw_info_type *type,
+                                    void *data, uint32_t len, Error **errp)
+{
+    struct iommu_hw_info info = {
+        .size = sizeof(info),
+        .dev_id = devid,

backends/iommufd.c int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id, · line 230

Quoted 10 lines
backends/iommufd.c +10 -0
+        .data_len = len,
+        .data_uptr = (uintptr_t)data,
+    };
+    int ret;
+
+    ret = ioctl(be->fd, IOMMU_GET_HW_INFO, &info);
+    if (ret) {
+        error_setg_errno(errp, errno, "Failed to get hardware info");
+    } else {
+        *type = info.out_data_type;

type should not be NULL.

Quoted 8 lines
backends/iommufd.c +5 -0
+    }
+
+    return ret;
+}
+
  void hiod_iommufd_init(HIODIOMMUFD *idev, IOMMUFDBackend *iommufd,
                         uint32_t devid)
  {
#25 lore raw

RE: [PATCH v2 06/10] backends/iommufd: Introduce helper function iommufd_backend_get_device_info()

Quoted 74 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v2 06/10] backends/iommufd: Introduce helper function iommufd_backend_get_device_info()

include/sysemu/iommufd.h int iommufd_backend_map_dma(IOMMUFDBackend

Quoted 24 lines · Zhenzhong Duan

Introduce a helper function iommufd_backend_get_device_info() to get host IOMMU related information through iommufd uAPI.

---
  include/sysemu/iommufd.h |  4 ++++
  backends/iommufd.c       | 23 ++++++++++++++++++++++-
  2 files changed, 26 insertions(+), 1 deletion(-)
include/sysemu/iommufd.h +1 -0
index 71c53cbb45..fa1a866237 100644
@@ -4,6 +4,7 @@
  #include "qom/object.h"
  #include "exec/hwaddr.h"
  #include "exec/cpu-common.h"
+#include <linux/iommufd.h>
  #include "sysemu/host_iommu_device.h"
 
  #define TYPE_IOMMUFD_BACKEND "iommufd"
@@ -34,6 +35,9 @@ int iommufd_backend_map_dma(IOMMUFDBackend

*be, uint32_t ioas_id, hwaddr iova,

include/sysemu/iommufd.h int iommufd_backend_map_dma(IOMMUFDBackend · line 36

Quoted 2 lines
include/sysemu/iommufd.h +0 -0
                              ram_addr_t size, void *vaddr, bool readonly);
  int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t

ioas_id,

include/sysemu/iommufd.h int iommufd_backend_map_dma(IOMMUFDBackend · line 38

Quoted 2 lines
include/sysemu/iommufd.h +1 -0
                                hwaddr iova, ram_addr_t size);
+int iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t

devid,

include/sysemu/iommufd.h int iommufd_backend_map_dma(IOMMUFDBackend *be, uint32_t ioas_id, hwaddr iova,

Quoted 5 lines
include/sysemu/iommufd.h +2 -0
+                                    enum iommu_hw_info_type *type,
+                                    void *data, uint32_t len, Error **errp);
 
  #define TYPE_HIOD_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"
  OBJECT_DECLARE_TYPE(HIODIOMMUFD, HIODIOMMUFDClass,

HIOD_IOMMUFD)

backends/iommufd.c int

Quoted 13 lines
backends/iommufd.c +0 -1
index ef8b3a808b..559affa9ec 100644
@@ -20,7 +20,6 @@
  #include "monitor/monitor.h"
  #include "trace.h"
  #include <sys/ioctl.h>
-#include <linux/iommufd.h>
 
  static void iommufd_backend_init(Object *obj)
  {
@@ -212,6 +211,28 @@ int

iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,

backends/iommufd.c int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id,

Quoted 4 lines
backends/iommufd.c +1 -0
      return ret;
  }
 
+int iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t

devid,

Quoted 10 lines
backends/iommufd.c +10 -0
+                                    enum iommu_hw_info_type *type,
+                                    void *data, uint32_t len, Error **errp)
+{
+    struct iommu_hw_info info = {
+        .size = sizeof(info),
+        .dev_id = devid,
+        .data_len = len,
+        .data_uptr = (uintptr_t)data,
+    };
+    int ret;
Quoted 8 lines

backends/iommufd.c int iommufd_backend_unmap_dma(IOMMUFDBackend *be, uint32_t ioas_id, · line 230

Quoted 6 lines
backends/iommufd.c +6 -0
+
+    ret = ioctl(be->fd, IOMMU_GET_HW_INFO, &info);
+    if (ret) {
+        error_setg_errno(errp, errno, "Failed to get hardware info");
+    } else {
+        *type = info.out_data_type;

type should not be NULL.

Yes, will add g_assert(type);

Thanks
Zhenzhong

#26 lore raw

PATCH v2 7/10 backends/iommufd: Implement get_host_iommu_info() callback

In reply to Zhenzhong Duan

It calls iommufd_backend_get_device_info() to get host IOMMU related information.

Define a common structure HIOD_IOMMUFD_INFO to describe the info returned from kernel. Currently only vtd, but easy to add arm smmu when kernel supports.

---
 include/sysemu/iommufd.h |  7 +++++++
 backends/iommufd.c       | 17 +++++++++++++++++
 2 files changed, 24 insertions(+)
include/sysemu/iommufd.h +7 -0
index fa1a866237..44ec1335b2 100644
@@ -39,6 +39,13 @@ int iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
                                     enum iommu_hw_info_type *type,
                                     void *data, uint32_t len, Error **errp);
 
+typedef struct HIOD_IOMMUFD_INFO {
+    enum iommu_hw_info_type type;
+    union {
+        struct iommu_hw_info_vtd vtd;
+    } data;
+} HIOD_IOMMUFD_INFO;
+
 #define TYPE_HIOD_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"
 OBJECT_DECLARE_TYPE(HIODIOMMUFD, HIODIOMMUFDClass, HIOD_IOMMUFD)
 
backends/iommufd.c +17 -0
index 559affa9ec..1e9c469e65 100644
@@ -240,8 +240,25 @@ void hiod_iommufd_init(HIODIOMMUFD *idev, IOMMUFDBackend *iommufd,
     idev->devid = devid;
 }
 
+static int hiod_iommufd_get_host_iommu_info(HostIOMMUDevice *hiod,
+                                            void *data, uint32_t len,
+                                            Error **errp)
+{
+    HIODIOMMUFD *idev = HIOD_IOMMUFD(hiod);
+    HIOD_IOMMUFD_INFO *info = data;
+
+    assert(sizeof(HIOD_IOMMUFD_INFO) <= len);
+
+    return iommufd_backend_get_device_info(idev->iommufd, idev->devid,
+                                           &info->type, &info->data,
+                                           sizeof(info->data), errp);
+}
+
 static void hiod_iommufd_class_init(ObjectClass *oc, void *data)
 {
+    HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
+
+    hiodc->get_host_iommu_info = hiod_iommufd_get_host_iommu_info;
 }
 
 static const TypeInfo types[] = {
Signature · 1 line
2.34.1
#27 lore raw

Re: [PATCH v2 07/10] backends/iommufd: Implement get_host_iommu_info() callback

Quoted 6 lines · Zhenzhong Duan

It calls iommufd_backend_get_device_info() to get host IOMMU related information.

Define a common structure HIOD_IOMMUFD_INFO to describe the info returned from kernel. Currently only vtd, but easy to add arm smmu when kernel supports.

I think you can merge the previous patch and this one.

include/sysemu/iommufd.h

Quoted 10 lines
---
  include/sysemu/iommufd.h |  7 +++++++
  backends/iommufd.c       | 17 +++++++++++++++++
  2 files changed, 24 insertions(+)
include/sysemu/iommufd.h +0 -0
index fa1a866237..44ec1335b2 100644

I just noticed that include/sysemu/iommufd.h lacks a header. Could you fix that please ?

include/sysemu/iommufd.h int iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid, · line 42

Quoted 5 lines
include/sysemu/iommufd.h +1 -0
@@ -39,6 +39,13 @@ int iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,
                                      enum iommu_hw_info_type *type,
                                      void *data, uint32_t len, Error **errp);
 
+typedef struct HIOD_IOMMUFD_INFO {

Please use CamelCase names.

Thanks,

C.

Quoted 39 lines
include/sysemu/iommufd.h +6 -0
+    enum iommu_hw_info_type type;
+    union {
+        struct iommu_hw_info_vtd vtd;
+    } data;
+} HIOD_IOMMUFD_INFO;
+
  #define TYPE_HIOD_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"
  OBJECT_DECLARE_TYPE(HIODIOMMUFD, HIODIOMMUFDClass, HIOD_IOMMUFD)
 
backends/iommufd.c +17 -0
index 559affa9ec..1e9c469e65 100644
@@ -240,8 +240,25 @@ void hiod_iommufd_init(HIODIOMMUFD *idev, IOMMUFDBackend *iommufd,
      idev->devid = devid;
  }
 
+static int hiod_iommufd_get_host_iommu_info(HostIOMMUDevice *hiod,
+                                            void *data, uint32_t len,
+                                            Error **errp)
+{
+    HIODIOMMUFD *idev = HIOD_IOMMUFD(hiod);
+    HIOD_IOMMUFD_INFO *info = data;
+
+    assert(sizeof(HIOD_IOMMUFD_INFO) <= len);
+
+    return iommufd_backend_get_device_info(idev->iommufd, idev->devid,
+                                           &info->type, &info->data,
+                                           sizeof(info->data), errp);
+}
+
  static void hiod_iommufd_class_init(ObjectClass *oc, void *data)
  {
+    HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
+
+    hiodc->get_host_iommu_info = hiod_iommufd_get_host_iommu_info;
  }
 
  static const TypeInfo types[] = {
#28 lore raw

RE: [PATCH v2 07/10] backends/iommufd: Implement get_host_iommu_info() callback

Quoted 8 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v2 07/10] backends/iommufd: Implement get_host_iommu_info() callback

On 4/8/24 10:12, Zhenzhong Duan wrote:

Quoted 8 lines
Quoted 6 lines

It calls iommufd_backend_get_device_info() to get host IOMMU related information.

Define a common structure HIOD_IOMMUFD_INFO to describe the info returned from kernel. Currently only vtd, but easy to add arm smmu when kernel supports.

I think you can merge the previous patch and this one.

Sure.

Quoted 8 lines
Quoted 8 lines
---
  include/sysemu/iommufd.h |  7 +++++++
  backends/iommufd.c       | 17 +++++++++++++++++
  2 files changed, 24 insertions(+)
Quoted 7 lines

include/sysemu/iommufd.h

Quoted 4 lines
include/sysemu/iommufd.h +0 -0
index fa1a866237..44ec1335b2 100644

I just noticed that include/sysemu/iommufd.h lacks a header. Could you fix that please ?

Sure. Presume you means the copyright header. Fix me if you mean others.

Quoted 9 lines

include/sysemu/iommufd.h int

Quoted 2 lines
include/sysemu/iommufd.h +0 -0
@@ -39,6 +39,13 @@ int

iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,

include/sysemu/iommufd.h int iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid, · line 42

Quoted 4 lines
include/sysemu/iommufd.h +1 -0
                                      enum iommu_hw_info_type *type,
                                      void *data, uint32_t len, Error **errp);
 
+typedef struct HIOD_IOMMUFD_INFO {

Please use CamelCase names.

Sure.

Thanks
Zhenzhong

Quoted 50 lines

Thanks,

C.

include/sysemu/iommufd.h int iommufd_backend_get_device_info(IOMMUFDBackend *be, uint32_t devid,

Quoted 8 lines
include/sysemu/iommufd.h +6 -0
+    enum iommu_hw_info_type type;
+    union {
+        struct iommu_hw_info_vtd vtd;
+    } data;
+} HIOD_IOMMUFD_INFO;
+
  #define TYPE_HIOD_IOMMUFD TYPE_HOST_IOMMU_DEVICE "-iommufd"
  OBJECT_DECLARE_TYPE(HIODIOMMUFD, HIODIOMMUFDClass,

HIOD_IOMMUFD)

backends/iommufd.c void hiod_iommufd_init(HIODIOMMUFD *idev,

Quoted 6 lines
backends/iommufd.c +0 -0
index 559affa9ec..1e9c469e65 100644
@@ -240,8 +240,25 @@ void hiod_iommufd_init(HIODIOMMUFD *idev,

IOMMUFDBackend *iommufd,

backends/iommufd.c void hiod_iommufd_init(HIODIOMMUFD *idev, IOMMUFDBackend *iommufd,

Quoted 4 lines
backends/iommufd.c +1 -0
      idev->devid = devid;
  }
 
+static int hiod_iommufd_get_host_iommu_info(HostIOMMUDevice

*hiod,

Quoted 22 lines
backends/iommufd.c +16 -0
+                                            void *data, uint32_t len,
+                                            Error **errp)
+{
+    HIODIOMMUFD *idev = HIOD_IOMMUFD(hiod);
+    HIOD_IOMMUFD_INFO *info = data;
+
+    assert(sizeof(HIOD_IOMMUFD_INFO) <= len);
+
+    return iommufd_backend_get_device_info(idev->iommufd, idev-
 devid,
+                                           &info->type, &info->data,
+                                           sizeof(info->data), errp);
+}
+
  static void hiod_iommufd_class_init(ObjectClass *oc, void *data)
  {
+    HostIOMMUDeviceClass *hiodc = HOST_IOMMU_DEVICE_CLASS(oc);
+
+    hiodc->get_host_iommu_info = hiod_iommufd_get_host_iommu_info;
  }
 
  static const TypeInfo types[] = {
#29 lore raw

PATCH v2 8/10 vfio: Create host IOMMU device instance

In reply to Zhenzhong Duan

Create host IOMMU device instance and initialize it based on backend.

---
 include/hw/vfio/vfio-common.h | 1 +
 hw/vfio/container.c           | 5 +++++
 hw/vfio/iommufd.c             | 8 ++++++++
 3 files changed, 14 insertions(+)
include/hw/vfio/vfio-common.h +1 -0
index d382b12ec1..4fbba85018 100644
@@ -126,6 +126,7 @@ typedef struct VFIODevice {
     OnOffAuto pre_copy_dirty_page_tracking;
     bool dirty_pages_supported;
     bool dirty_tracking;
+    HostIOMMUDevice *hiod;
     int devid;
     IOMMUFDBackend *iommufd;
 } VFIODevice;
hw/vfio/container.c +5 -0
index ba0ad4a41b..fc0c027501 100644
@@ -915,6 +915,7 @@ static int vfio_legacy_attach_device(const char *name, VFIODevice *vbasedev,
     VFIODevice *vbasedev_iter;
     VFIOGroup *group;
     VFIOContainerBase *bcontainer;
+    HIODLegacyVFIO *hiod_vfio;
     int ret;
 
     if (groupid < 0) {
@@ -945,6 +946,9 @@ static int vfio_legacy_attach_device(const char *name, VFIODevice *vbasedev,
     vbasedev->bcontainer = bcontainer;
     QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
     QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
+    hiod_vfio = HIOD_LEGACY_VFIO(object_new(TYPE_HIOD_LEGACY_VFIO));
+    hiod_vfio->vdev = vbasedev;
+    vbasedev->hiod = HOST_IOMMU_DEVICE(hiod_vfio);
 
     return ret;
 }
@@ -959,6 +963,7 @@ static void vfio_legacy_detach_device(VFIODevice *vbasedev)
     trace_vfio_detach_device(vbasedev->name, group->groupid);
     vfio_put_base_device(vbasedev);
     vfio_put_group(group);
+    object_unref(vbasedev->hiod);
 }
 
 static int vfio_legacy_pci_hot_reset(VFIODevice *vbasedev, bool single)
hw/vfio/iommufd.c +8 -0
index 115b9f8e7f..b6d058339b 100644
@@ -308,6 +308,7 @@ static int iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
     VFIOIOMMUFDContainer *container;
     VFIOAddressSpace *space;
     struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
+    HIODIOMMUFDVFIO *hiod_vfio;
     int ret, devfd;
     uint32_t ioas_id;
     Error *err = NULL;
@@ -431,6 +432,12 @@ found_container:
     QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
     QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
 
+    hiod_vfio = HIOD_IOMMUFD_VFIO(object_new(TYPE_HIOD_IOMMUFD_VFIO));
+    hiod_iommufd_init(HIOD_IOMMUFD(hiod_vfio), vbasedev->iommufd,
+                      vbasedev->devid);
+    hiod_vfio->vdev = vbasedev;
+    vbasedev->hiod = HOST_IOMMU_DEVICE(hiod_vfio);
+
     trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs,
                                    vbasedev->num_regions, vbasedev->flags);
     return 0;
@@ -468,6 +475,7 @@ static void iommufd_cdev_detach(VFIODevice *vbasedev)
     iommufd_cdev_detach_container(vbasedev, container);
     iommufd_cdev_container_destroy(container);
     vfio_put_address_space(space);
+    object_unref(vbasedev->hiod);
 
     iommufd_cdev_unbind_and_disconnect(vbasedev);
     close(vbasedev->fd);
Signature · 1 line
2.34.1
#30 lore raw

Re: [PATCH v2 08/10] vfio: Create host IOMMU device instance

Quoted 26 lines · Zhenzhong Duan

Create host IOMMU device instance and initialize it based on backend.

---
  include/hw/vfio/vfio-common.h | 1 +
  hw/vfio/container.c           | 5 +++++
  hw/vfio/iommufd.c             | 8 ++++++++
  3 files changed, 14 insertions(+)
include/hw/vfio/vfio-common.h +1 -0
index d382b12ec1..4fbba85018 100644
@@ -126,6 +126,7 @@ typedef struct VFIODevice {
      OnOffAuto pre_copy_dirty_page_tracking;
      bool dirty_pages_supported;
      bool dirty_tracking;
+    HostIOMMUDevice *hiod;
      int devid;
      IOMMUFDBackend *iommufd;
  } VFIODevice;
hw/vfio/container.c +0 -0
index ba0ad4a41b..fc0c027501 100644

hw/vfio/container.c static int vfio_legacy_attach_device(const char *name, VFIODevice *vbasedev, · line 918

Quoted 4 lines
hw/vfio/container.c +1 -0
@@ -915,6 +915,7 @@ static int vfio_legacy_attach_device(const char *name, VFIODevice *vbasedev,
      VFIODevice *vbasedev_iter;
      VFIOGroup *group;
      VFIOContainerBase *bcontainer;
+    HIODLegacyVFIO *hiod_vfio;

s/hiod_vfio/hiod/ please. Same below.

Thanks,

C.

Quoted 54 lines
hw/vfio/container.c +4 -0
      int ret;
 
      if (groupid < 0) {
@@ -945,6 +946,9 @@ static int vfio_legacy_attach_device(const char *name, VFIODevice *vbasedev,
      vbasedev->bcontainer = bcontainer;
      QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
      QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
+    hiod_vfio = HIOD_LEGACY_VFIO(object_new(TYPE_HIOD_LEGACY_VFIO));
+    hiod_vfio->vdev = vbasedev;
+    vbasedev->hiod = HOST_IOMMU_DEVICE(hiod_vfio);
 
      return ret;
  }
@@ -959,6 +963,7 @@ static void vfio_legacy_detach_device(VFIODevice *vbasedev)
      trace_vfio_detach_device(vbasedev->name, group->groupid);
      vfio_put_base_device(vbasedev);
      vfio_put_group(group);
+    object_unref(vbasedev->hiod);
  }
 
  static int vfio_legacy_pci_hot_reset(VFIODevice *vbasedev, bool single)
hw/vfio/iommufd.c +8 -0
index 115b9f8e7f..b6d058339b 100644
@@ -308,6 +308,7 @@ static int iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
      VFIOIOMMUFDContainer *container;
      VFIOAddressSpace *space;
      struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
+    HIODIOMMUFDVFIO *hiod_vfio;
      int ret, devfd;
      uint32_t ioas_id;
      Error *err = NULL;
@@ -431,6 +432,12 @@ found_container:
      QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
      QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
 
+    hiod_vfio = HIOD_IOMMUFD_VFIO(object_new(TYPE_HIOD_IOMMUFD_VFIO));
+    hiod_iommufd_init(HIOD_IOMMUFD(hiod_vfio), vbasedev->iommufd,
+                      vbasedev->devid);
+    hiod_vfio->vdev = vbasedev;
+    vbasedev->hiod = HOST_IOMMU_DEVICE(hiod_vfio);
+
      trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs,
                                     vbasedev->num_regions, vbasedev->flags);
      return 0;
@@ -468,6 +475,7 @@ static void iommufd_cdev_detach(VFIODevice *vbasedev)
      iommufd_cdev_detach_container(vbasedev, container);
      iommufd_cdev_container_destroy(container);
      vfio_put_address_space(space);
+    object_unref(vbasedev->hiod);
 
      iommufd_cdev_unbind_and_disconnect(vbasedev);
      close(vbasedev->fd);
#31 lore raw

RE: [PATCH v2 08/10] vfio: Create host IOMMU device instance

Quoted 35 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v2 08/10] vfio: Create host IOMMU device instance

include/hw/vfio/vfio-

Quoted 10 lines · Zhenzhong Duan

Create host IOMMU device instance and initialize it based on backend.

---
  include/hw/vfio/vfio-common.h | 1 +
  hw/vfio/container.c           | 5 +++++
  hw/vfio/iommufd.c             | 8 ++++++++
  3 files changed, 14 insertions(+)
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-common.h +0 -0

common.h

hw/vfio/container.c static int vfio_legacy_attach_device(const char

Quoted 16 lines

index d382b12ec1..4fbba85018 100644

include/hw/vfio/vfio-common.h +1 -0
@@ -126,6 +126,7 @@ typedef struct VFIODevice {
      OnOffAuto pre_copy_dirty_page_tracking;
      bool dirty_pages_supported;
      bool dirty_tracking;
+    HostIOMMUDevice *hiod;
      int devid;
      IOMMUFDBackend *iommufd;
  } VFIODevice;
hw/vfio/container.c +0 -0
index ba0ad4a41b..fc0c027501 100644
@@ -915,6 +915,7 @@ static int vfio_legacy_attach_device(const char

*name, VFIODevice *vbasedev,

Quoted 6 lines

hw/vfio/container.c static int vfio_legacy_attach_device(const char *name, VFIODevice *vbasedev, · line 918

Quoted 4 lines
hw/vfio/container.c +1 -0
      VFIODevice *vbasedev_iter;
      VFIOGroup *group;
      VFIOContainerBase *bcontainer;
+    HIODLegacyVFIO *hiod_vfio;

s/hiod_vfio/hiod/ please. Same below.

Will do.

Thanks
Zhenzhong

Quoted 71 lines

Thanks,

C.

hw/vfio/container.c static int vfio_legacy_attach_device(const char

Quoted 4 lines
hw/vfio/container.c +0 -0
      int ret;
 
      if (groupid < 0) {
@@ -945,6 +946,9 @@ static int vfio_legacy_attach_device(const char

*name, VFIODevice *vbasedev,

hw/vfio/container.c static int vfio_legacy_attach_device(const char · line 947

Quoted 2 lines
hw/vfio/container.c +0 -0
      vbasedev->bcontainer = bcontainer;
      QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev,

container_next);

hw/vfio/container.c static int vfio_legacy_attach_device(const char · line 949

Quoted 2 lines
hw/vfio/container.c +1 -0
      QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
+    hiod_vfio =

HIOD_LEGACY_VFIO(object_new(TYPE_HIOD_LEGACY_VFIO));

hw/vfio/container.c static void vfio_legacy_detach_device(VFIODevice

Quoted 6 lines
hw/vfio/container.c +2 -0
+    hiod_vfio->vdev = vbasedev;
+    vbasedev->hiod = HOST_IOMMU_DEVICE(hiod_vfio);
 
      return ret;
  }
@@ -959,6 +963,7 @@ static void vfio_legacy_detach_device(VFIODevice

*vbasedev)

hw/vfio/iommufd.c static int iommufd_cdev_attach(const char *name,

Quoted 12 lines
hw/vfio/container.c +1 -0
      trace_vfio_detach_device(vbasedev->name, group->groupid);
      vfio_put_base_device(vbasedev);
      vfio_put_group(group);
+    object_unref(vbasedev->hiod);
  }
 
  static int vfio_legacy_pci_hot_reset(VFIODevice *vbasedev, bool single)
hw/vfio/iommufd.c +0 -0
index 115b9f8e7f..b6d058339b 100644
@@ -308,6 +308,7 @@ static int iommufd_cdev_attach(const char *name,

VFIODevice *vbasedev,

hw/vfio/iommufd.c found_container: · line 432

Quoted 9 lines
hw/vfio/iommufd.c +1 -0
      VFIOIOMMUFDContainer *container;
      VFIOAddressSpace *space;
      struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
+    HIODIOMMUFDVFIO *hiod_vfio;
      int ret, devfd;
      uint32_t ioas_id;
      Error *err = NULL;
@@ -431,6 +432,12 @@ found_container:
      QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev,

container_next);

hw/vfio/iommufd.c found_container: · line 435

Quoted 3 lines
hw/vfio/iommufd.c +1 -0
      QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
 
+    hiod_vfio =

HIOD_IOMMUFD_VFIO(object_new(TYPE_HIOD_IOMMUFD_VFIO));

hw/vfio/iommufd.c static void iommufd_cdev_detach(VFIODevice

Quoted 10 lines
hw/vfio/iommufd.c +5 -0
+    hiod_iommufd_init(HIOD_IOMMUFD(hiod_vfio), vbasedev->iommufd,
+                      vbasedev->devid);
+    hiod_vfio->vdev = vbasedev;
+    vbasedev->hiod = HOST_IOMMU_DEVICE(hiod_vfio);
+
      trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev-
 num_irqs,
                                     vbasedev->num_regions, vbasedev->flags);
      return 0;
hw/vfio/iommufd.c +0 -0
@@ -468,6 +475,7 @@ static void iommufd_cdev_detach(VFIODevice

*vbasedev)

Quoted 7 lines
hw/vfio/iommufd.c +1 -0
      iommufd_cdev_detach_container(vbasedev, container);
      iommufd_cdev_container_destroy(container);
      vfio_put_address_space(space);
+    object_unref(vbasedev->hiod);
 
      iommufd_cdev_unbind_and_disconnect(vbasedev);
      close(vbasedev->fd);
#32 lore raw

PATCH v2 9/10 hw/pci: Introduce pci_device_set/unset_iommu_device()

In reply to Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds pci_device_set/unset_iommu_device() to set/unset HostIOMMUDevice for a given PCI device. Caller of set should fail if set operation fails.

Extract out pci_device_get_iommu_bus_devfn() to facilitate implementation of pci_device_set/unset_iommu_device().

---
 include/hw/pci/pci.h | 40 ++++++++++++++++++++++-
 hw/pci/pci.c         | 75 ++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 111 insertions(+), 4 deletions(-)
include/hw/pci/pci.h +39 -1
index eaa3fc99d8..4ae7fe6f3f 100644
@@ -3,6 +3,7 @@
 
 #include "exec/memory.h"
 #include "sysemu/dma.h"
+#include "sysemu/host_iommu_device.h"
 
 /* PCI includes legacy ISA access.  */
 #include "hw/isa/isa.h"
@@ -383,10 +384,47 @@ typedef struct PCIIOMMUOps {
      *
      * @devfn: device and function number
      */
-   AddressSpace * (*get_address_space)(PCIBus *bus, void *opaque, int devfn);
+    AddressSpace * (*get_address_space)(PCIBus *bus, void *opaque, int devfn);
+    /**
+     * @set_iommu_device: attach a HostIOMMUDevice to a vIOMMU
+     *
+     * Optional callback, if not implemented in vIOMMU, then vIOMMU can't
+     * retrieve host information from the associated HostIOMMUDevice.
+     *
+     * Return true if HostIOMMUDevice is attached, or else return false
+     * with errp set.
+     *
+     * @bus: the #PCIBus of the PCI device.
+     *
+     * @opaque: the data passed to pci_setup_iommu().
+     *
+     * @devfn: device and function number of the PCI device.
+     *
+     * @dev: the data structure representing host IOMMU device.
+     *
+     * @errp: pass an Error out only when return false
+     *
+     */
+    int (*set_iommu_device)(PCIBus *bus, void *opaque, int devfn,
+                            HostIOMMUDevice *dev, Error **errp);
+    /**
+     * @unset_iommu_device: detach a HostIOMMUDevice from a vIOMMU
+     *
+     * Optional callback.
+     *
+     * @bus: the #PCIBus of the PCI device.
+     *
+     * @opaque: the data passed to pci_setup_iommu().
+     *
+     * @devfn: device and function number of the PCI device.
+     */
+    void (*unset_iommu_device)(PCIBus *bus, void *opaque, int devfn);
 } PCIIOMMUOps;
 
 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
+int pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod,
+                                Error **errp);
+void pci_device_unset_iommu_device(PCIDevice *dev);
 
 /**
  * pci_setup_iommu: Initialize specific IOMMU handlers for a PCIBus
hw/pci/pci.c +72 -3

96 lines not on this page · raw message

Signature · 1 line
2.34.1
#33 lore raw

Re: [PATCH v2 09/10] hw/pci: Introduce pci_device_set/unset_iommu_device()

Quoted 7 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds pci_device_set/unset_iommu_device() to set/unset HostIOMMUDevice for a given PCI device. Caller of set should fail if set operation fails.

Extract out pci_device_get_iommu_bus_devfn() to facilitate

I would separate this change in a prereq patch.

Thanks,

C.

Quoted 174 lines

implementation of pci_device_set/unset_iommu_device().

---
  include/hw/pci/pci.h | 40 ++++++++++++++++++++++-
  hw/pci/pci.c         | 75 ++++++++++++++++++++++++++++++++++++++++++--
  2 files changed, 111 insertions(+), 4 deletions(-)
include/hw/pci/pci.h +39 -1
index eaa3fc99d8..4ae7fe6f3f 100644
@@ -3,6 +3,7 @@
 
  #include "exec/memory.h"
  #include "sysemu/dma.h"
+#include "sysemu/host_iommu_device.h"
 
  /* PCI includes legacy ISA access.  */
  #include "hw/isa/isa.h"
@@ -383,10 +384,47 @@ typedef struct PCIIOMMUOps {
       *
       * @devfn: device and function number
       */
-   AddressSpace * (*get_address_space)(PCIBus *bus, void *opaque, int devfn);
+    AddressSpace * (*get_address_space)(PCIBus *bus, void *opaque, int devfn);
+    /**
+     * @set_iommu_device: attach a HostIOMMUDevice to a vIOMMU
+     *
+     * Optional callback, if not implemented in vIOMMU, then vIOMMU can't
+     * retrieve host information from the associated HostIOMMUDevice.
+     *
+     * Return true if HostIOMMUDevice is attached, or else return false
+     * with errp set.
+     *
+     * @bus: the #PCIBus of the PCI device.
+     *
+     * @opaque: the data passed to pci_setup_iommu().
+     *
+     * @devfn: device and function number of the PCI device.
+     *
+     * @dev: the data structure representing host IOMMU device.
+     *
+     * @errp: pass an Error out only when return false
+     *
+     */
+    int (*set_iommu_device)(PCIBus *bus, void *opaque, int devfn,
+                            HostIOMMUDevice *dev, Error **errp);
+    /**
+     * @unset_iommu_device: detach a HostIOMMUDevice from a vIOMMU
+     *
+     * Optional callback.
+     *
+     * @bus: the #PCIBus of the PCI device.
+     *
+     * @opaque: the data passed to pci_setup_iommu().
+     *
+     * @devfn: device and function number of the PCI device.
+     */
+    void (*unset_iommu_device)(PCIBus *bus, void *opaque, int devfn);
  } PCIIOMMUOps;
 
  AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
+int pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod,
+                                Error **errp);
+void pci_device_unset_iommu_device(PCIDevice *dev);
 
  /**
   * pci_setup_iommu: Initialize specific IOMMU handlers for a PCIBus
hw/pci/pci.c +72 -3
index e7a39cb203..8ece617673 100644
@@ -2648,11 +2648,27 @@ static void pci_device_class_base_init(ObjectClass *klass, void *data)
      }
  }
 
-AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+/*
+ * Get IOMMU root bus, aliased bus and devfn of a PCI device
+ *
+ * IOMMU root bus is needed by all call sites to call into iommu_ops.
+ * For call sites which don't need aliased BDF, passing NULL to
+ * aliased_[bus/devfn] is allowed.
+ *
+ * @piommu_bus: return root #PCIBus backed by an IOMMU for the PCI device.
+ *
+ * @aliased_bus: return aliased #PCIBus of the PCI device, optional.
+ *
+ * @aliased_devfn: return aliased devfn of the PCI device, optional.
+ */
+static void pci_device_get_iommu_bus_devfn(PCIDevice *dev,
+                                           PCIBus **piommu_bus,
+                                           PCIBus **aliased_bus,
+                                           int *aliased_devfn)
  {
      PCIBus *bus = pci_get_bus(dev);
      PCIBus *iommu_bus = bus;
-    uint8_t devfn = dev->devfn;
+    int devfn = dev->devfn;
 
      while (iommu_bus && !iommu_bus->iommu_ops && iommu_bus->parent_dev) {
          PCIBus *parent_bus = pci_get_bus(iommu_bus->parent_dev);
@@ -2693,13 +2709,66 @@ AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
 
          iommu_bus = parent_bus;
      }
-    if (!pci_bus_bypass_iommu(bus) && iommu_bus->iommu_ops) {
+
+    assert(0 <= devfn && devfn < PCI_DEVFN_MAX);
+    assert(iommu_bus);
+
+    if (pci_bus_bypass_iommu(bus) || !iommu_bus->iommu_ops) {
+        iommu_bus = NULL;
+    }
+
+    *piommu_bus = iommu_bus;
+
+    if (aliased_bus) {
+        *aliased_bus = bus;
+    }
+
+    if (aliased_devfn) {
+        *aliased_devfn = devfn;
+    }
+}
+
+AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    int devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &iommu_bus, &bus, &devfn);
+    if (iommu_bus) {
          return iommu_bus->iommu_ops->get_address_space(bus,
                                   iommu_bus->iommu_opaque, devfn);
      }
      return &address_space_memory;
  }
 
+int pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *hiod,
+                                Error **errp)
+{
+    PCIBus *iommu_bus;
+
+    /* set_iommu_device requires device's direct BDF instead of aliased BDF */
+    pci_device_get_iommu_bus_devfn(dev, &iommu_bus, NULL, NULL);
+    if (iommu_bus && iommu_bus->iommu_ops->set_iommu_device) {
+        return iommu_bus->iommu_ops->set_iommu_device(pci_get_bus(dev),
+                                                      iommu_bus->iommu_opaque,
+                                                      dev->devfn, hiod, errp);
+    }
+    return 0;
+}
+
+void pci_device_unset_iommu_device(PCIDevice *dev)
+{
+    PCIBus *iommu_bus;
+
+    pci_device_get_iommu_bus_devfn(dev, &iommu_bus, NULL, NULL);
+    if (iommu_bus && iommu_bus->iommu_ops->unset_iommu_device) {
+        return iommu_bus->iommu_ops->unset_iommu_device(pci_get_bus(dev),
+                                                        iommu_bus->iommu_opaque,
+                                                        dev->devfn);
+    }
+}
+
  void pci_setup_iommu(PCIBus *bus, const PCIIOMMUOps *ops, void *opaque)
  {
      /*
#34 lore raw

RE: [PATCH v2 09/10] hw/pci: Introduce pci_device_set/unset_iommu_device()

Quoted 10 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v2 09/10] hw/pci: Introduce
pci_device_set/unset_iommu_device()

Quoted 2 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

Quoted 7 lines
Quoted 5 lines

This adds pci_device_set/unset_iommu_device() to set/unset HostIOMMUDevice for a given PCI device. Caller of set should fail if set operation fails.

Extract out pci_device_get_iommu_bus_devfn() to facilitate

I would separate this change in a prereq patch.

Will do.

Thanks
Zhenzhong

#35 lore raw

PATCH v2 10/10 vfio: Pass HostIOMMUDevice to vIOMMU

In reply to Zhenzhong Duan

With HostIOMMUDevice passed, vIOMMU can check compatibility with host IOMMU, call into IOMMUFD specific methods, etc.

---
 hw/vfio/pci.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
hw/vfio/pci.c +15 -5
index 64780d1b79..224501a86e 100644
@@ -3111,11 +3111,17 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
 
     vfio_bars_register(vdev);
 
-    ret = vfio_add_capabilities(vdev, errp);
+    ret = pci_device_set_iommu_device(pdev, vbasedev->hiod, errp);
     if (ret) {
+        error_prepend(errp, "Failed to set iommu_device: ");
         goto out_teardown;
     }
 
+    ret = vfio_add_capabilities(vdev, errp);
+    if (ret) {
+        goto out_unset_idev;
+    }
+
     if (vdev->vga) {
         vfio_vga_quirk_setup(vdev);
     }
@@ -3132,7 +3138,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
             error_setg(errp,
                        "cannot support IGD OpRegion feature on hotplugged "
                        "device");
-            goto out_teardown;
+            goto out_unset_idev;
         }
 
         ret = vfio_get_dev_region_info(vbasedev,
@@ -3141,13 +3147,13 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
         if (ret) {
             error_setg_errno(errp, -ret,
                              "does not support requested IGD OpRegion feature");
-            goto out_teardown;
+            goto out_unset_idev;
         }
 
         ret = vfio_pci_igd_opregion_init(vdev, opregion, errp);
         g_free(opregion);
         if (ret) {
-            goto out_teardown;
+            goto out_unset_idev;
         }
     }
 
@@ -3233,6 +3239,8 @@ out_deregister:
     if (vdev->intx.mmap_timer) {
         timer_free(vdev->intx.mmap_timer);
     }
+out_unset_idev:
+    pci_device_unset_iommu_device(pdev);
 out_teardown:
     vfio_teardown_msi(vdev);
     vfio_bars_exit(vdev);
@@ -3261,6 +3269,7 @@ static void vfio_instance_finalize(Object *obj)
 static void vfio_exitfn(PCIDevice *pdev)
 {
     VFIOPCIDevice *vdev = VFIO_PCI(pdev);
+    VFIODevice *vbasedev = &vdev->vbasedev;
 
     vfio_unregister_req_notifier(vdev);
     vfio_unregister_err_notifier(vdev);
@@ -3275,7 +3284,8 @@ static void vfio_exitfn(PCIDevice *pdev)
     vfio_teardown_msi(vdev);
     vfio_pci_disable_rp_atomics(vdev);
     vfio_bars_exit(vdev);
-    vfio_migration_exit(&vdev->vbasedev);
+    vfio_migration_exit(vbasedev);
+    pci_device_unset_iommu_device(pdev);
 }
 
 static void vfio_pci_reset(DeviceState *dev)
Signature · 1 line
2.34.1
#36 lore raw

Re: [PATCH v2 10/10] vfio: Pass HostIOMMUDevice to vIOMMU

Quoted 7 lines · Zhenzhong Duan

With HostIOMMUDevice passed, vIOMMU can check compatibility with host IOMMU, call into IOMMUFD specific methods, etc.

LGTM, waiting v3.

Thanks,

C.

Quoted 79 lines
---
  hw/vfio/pci.c | 20 +++++++++++++++-----
  1 file changed, 15 insertions(+), 5 deletions(-)
hw/vfio/pci.c +15 -5
index 64780d1b79..224501a86e 100644
@@ -3111,11 +3111,17 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
 
      vfio_bars_register(vdev);
 
-    ret = vfio_add_capabilities(vdev, errp);
+    ret = pci_device_set_iommu_device(pdev, vbasedev->hiod, errp);
      if (ret) {
+        error_prepend(errp, "Failed to set iommu_device: ");
          goto out_teardown;
      }
 
+    ret = vfio_add_capabilities(vdev, errp);
+    if (ret) {
+        goto out_unset_idev;
+    }
+
      if (vdev->vga) {
          vfio_vga_quirk_setup(vdev);
      }
@@ -3132,7 +3138,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
              error_setg(errp,
                         "cannot support IGD OpRegion feature on hotplugged "
                         "device");
-            goto out_teardown;
+            goto out_unset_idev;
          }
 
          ret = vfio_get_dev_region_info(vbasedev,
@@ -3141,13 +3147,13 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
          if (ret) {
              error_setg_errno(errp, -ret,
                               "does not support requested IGD OpRegion feature");
-            goto out_teardown;
+            goto out_unset_idev;
          }
 
          ret = vfio_pci_igd_opregion_init(vdev, opregion, errp);
          g_free(opregion);
          if (ret) {
-            goto out_teardown;
+            goto out_unset_idev;
          }
      }
 
@@ -3233,6 +3239,8 @@ out_deregister:
      if (vdev->intx.mmap_timer) {
          timer_free(vdev->intx.mmap_timer);
      }
+out_unset_idev:
+    pci_device_unset_iommu_device(pdev);
  out_teardown:
      vfio_teardown_msi(vdev);
      vfio_bars_exit(vdev);
@@ -3261,6 +3269,7 @@ static void vfio_instance_finalize(Object *obj)
  static void vfio_exitfn(PCIDevice *pdev)
  {
      VFIOPCIDevice *vdev = VFIO_PCI(pdev);
+    VFIODevice *vbasedev = &vdev->vbasedev;
 
      vfio_unregister_req_notifier(vdev);
      vfio_unregister_err_notifier(vdev);
@@ -3275,7 +3284,8 @@ static void vfio_exitfn(PCIDevice *pdev)
      vfio_teardown_msi(vdev);
      vfio_pci_disable_rp_atomics(vdev);
      vfio_bars_exit(vdev);
-    vfio_migration_exit(&vdev->vbasedev);
+    vfio_migration_exit(vbasedev);
+    pci_device_unset_iommu_device(pdev);
  }
 
  static void vfio_pci_reset(DeviceState *dev)