Skip to contentSkip to conversation

Add a host IOMMU device abstraction

PATCH Superseded Zhenzhong Duan 5 participants 42 messages Feb 28, 2024 – Apr 1, 2024

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

cover Add a host IOMMU device abstraction

Hi,

Based on Joao's suggestion, the iommufd nesting prerequisite series [1] is further splitted to host IOMMU device abstract part and vIOMMU check/sync 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.

PATCH1-3: Introduce HostIOMMUDevice and two sub class
PATCH4: Define HostIOMMUDevice handle in VFIODevice
PATCH5-8: Introdcue host_iommu_device_create callback to allocate and intialize HostIOMMUDevice
PATCH9-10: Introdcue set/unset_iommu_device to pass HostIOMMUDevice to vIOMMU
PATCH11: a helper to get host IOMMU info

Because it's becoming clear on community's suggestion, I'd like to remove rfc tag from this version.

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

[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:
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 (10):
Introduce a common abstract struct HostIOMMUDevice backends/iommufd: Introduce IOMMUFDDevice
vfio: Introduce IOMMULegacyDevice
vfio: Add HostIOMMUDevice handle into VFIODevice
vfio: Introduce host_iommu_device_create callback vfio/container: Implement host_iommu_device_create callback in legacy
mode
vfio/iommufd: Implement host_iommu_device_create callback in iommufd
mode
vfio/pci: Allocate and initialize HostIOMMUDevice after attachment
vfio: Pass HostIOMMUDevice to vIOMMU
backends/iommufd: Introduce helper function iommufd_device_get_info()

 include/hw/pci/pci.h                  | 38 +++++++++++++++-
 include/hw/vfio/vfio-common.h         |  8 ++++
 include/hw/vfio/vfio-container-base.h |  1 +
 include/sysemu/host_iommu_device.h    | 22 ++++++++++
 include/sysemu/iommufd.h              | 19 ++++++++
 backends/iommufd.c                    | 32 +++++++++++++-
 hw/pci/pci.c                          | 62 +++++++++++++++++++++++++--
 hw/vfio/common.c                      |  8 ++++
 hw/vfio/container.c                   |  9 ++++
 hw/vfio/iommufd.c                     | 10 +++++
 hw/vfio/pci.c                         | 24 ++++++++---
 11 files changed, 223 insertions(+), 10 deletions(-)
 create mode 100644 include/sysemu/host_iommu_device.h
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/11 Introduce a common abstract struct HostIOMMUDevice

HostIOMMUDevice will be inherited by two sub classes, legacy and iommufd currently.

Introduce a helper function host_iommu_base_device_init to initialize it.

---
 include/sysemu/host_iommu_device.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
 create mode 100644 include/sysemu/host_iommu_device.h
include/sysemu/host_iommu_device.h new file +22 -0
new file mode 100644
index 0000000000..fe80ab25fb
@@ -0,0 +1,22 @@
+#ifndef HOST_IOMMU_DEVICE_H
+#define HOST_IOMMU_DEVICE_H
+
+typedef enum HostIOMMUDevice_Type {
+    HID_LEGACY,
+    HID_IOMMUFD,
+    HID_MAX,
+} HostIOMMUDevice_Type;
+
+typedef struct HostIOMMUDevice {
+    HostIOMMUDevice_Type type;
+    size_t size;
+} HostIOMMUDevice;
+
+static inline void host_iommu_base_device_init(HostIOMMUDevice *dev,
+                                               HostIOMMUDevice_Type type,
+                                               size_t size)
+{
+    dev->type = type;
+    dev->size = size;
+}
+#endif
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Hi Zhenzhong,

Quoted 2 lines · Zhenzhong Duan

HostIOMMUDevice will be inherited by two sub classes, legacy and iommufd currently.

As this patch introduces the object, you describe what the object is meant for and used for. Maybe reuse text from the cover letter

Thanks

Eric

Quoted 38 lines

Introduce a helper function host_iommu_base_device_init to initialize it.

---
 include/sysemu/host_iommu_device.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
 create mode 100644 include/sysemu/host_iommu_device.h
include/sysemu/host_iommu_device.h new file +22 -0
new file mode 100644
index 0000000000..fe80ab25fb
@@ -0,0 +1,22 @@
+#ifndef HOST_IOMMU_DEVICE_H
+#define HOST_IOMMU_DEVICE_H
+
+typedef enum HostIOMMUDevice_Type {
+    HID_LEGACY,
+    HID_IOMMUFD,
+    HID_MAX,
+} HostIOMMUDevice_Type;
+
+typedef struct HostIOMMUDevice {
+    HostIOMMUDevice_Type type;
+    size_t size;
+} HostIOMMUDevice;
+
+static inline void host_iommu_base_device_init(HostIOMMUDevice *dev,
+                                               HostIOMMUDevice_Type type,
+                                               size_t size)
+{
+    dev->type = type;
+    dev->size = size;
+}
+#endif
#3 lore raw

RE: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Quoted 7 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Quoted 6 lines

Hi Zhenzhong,

Quoted 2 lines · Zhenzhong Duan

HostIOMMUDevice will be inherited by two sub classes, legacy and iommufd currently.

As this patch introduces the object, you describe what the object is meant for and used for. Maybe reuse text from the cover letter

Sure, will do.

Thanks
Zhenzhong

Quoted 43 lines

Thanks

Eric

Review

Quoted 11 lines

Introduce a helper function host_iommu_base_device_init to initialize it.

---
 include/sysemu/host_iommu_device.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
 create mode 100644 include/sysemu/host_iommu_device.h
(quoted diff) +0 -0

b/include/sysemu/host_iommu_device.h

Quoted 27 lines

new file mode 100644
index 0000000000..fe80ab25fb

include/sysemu/host_iommu_device.h new file +22 -0
@@ -0,0 +1,22 @@
+#ifndef HOST_IOMMU_DEVICE_H
+#define HOST_IOMMU_DEVICE_H
+
+typedef enum HostIOMMUDevice_Type {
+    HID_LEGACY,
+    HID_IOMMUFD,
+    HID_MAX,
+} HostIOMMUDevice_Type;
+
+typedef struct HostIOMMUDevice {
+    HostIOMMUDevice_Type type;
+    size_t size;
+} HostIOMMUDevice;
+
+static inline void host_iommu_base_device_init(HostIOMMUDevice *dev,
+                                               HostIOMMUDevice_Type type,
+                                               size_t size)
+{
+    dev->type = type;
+    dev->size = size;
+}
+#endif
#4 lore raw

Re: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

In reply to Zhenzhong Duan

Hello Zhenzhong,

Quoted 19 lines · Zhenzhong Duan

HostIOMMUDevice will be inherited by two sub classes, legacy and iommufd currently.

Introduce a helper function host_iommu_base_device_init to initialize it.

---
  include/sysemu/host_iommu_device.h | 22 ++++++++++++++++++++++
  1 file changed, 22 insertions(+)

create mode 100644 include/sysemu/host_iommu_device.h

include/sysemu/host_iommu_device.h new file +1 -0
new file mode 100644
index 0000000000..fe80ab25fb
@@ -0,0 +1,22 @@
+#ifndef HOST_IOMMU_DEVICE_H

include/sysemu/host_iommu_device.h line 11

Quoted 10 lines
include/sysemu/host_iommu_device.h new file +10 -0
+#define HOST_IOMMU_DEVICE_H
+
+typedef enum HostIOMMUDevice_Type {
+    HID_LEGACY,
+    HID_IOMMUFD,
+    HID_MAX,
+} HostIOMMUDevice_Type;
+
+typedef struct HostIOMMUDevice {
+    HostIOMMUDevice_Type type;

A type field is not a good sign and that's where QOM is useful.

Is vtd_check_hdev() the only use of this field ? If so, can we simplify with a QOM interface in any way ?

Thanks,

C.

Quoted 11 lines
include/sysemu/host_iommu_device.h new file +11 -0
+    size_t size;
+} HostIOMMUDevice;
+
+static inline void host_iommu_base_device_init(HostIOMMUDevice *dev,
+                                               HostIOMMUDevice_Type type,
+                                               size_t size)
+{
+    dev->type = type;
+    dev->size = size;
+}
+#endif
#5 lore raw

RE: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Hi Cédric,

Quoted 40 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, March 19, 2024 4:17 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]; Tian, Kevin <[email protected]>; Liu, Yi L <[email protected]>; Sun, Yi Y <[email protected]>; Peng, Chao P <[email protected]>
Subject: Re: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Hello Zhenzhong,

Quoted 5 lines · Zhenzhong Duan

HostIOMMUDevice will be inherited by two sub classes, legacy and iommufd currently.

Introduce a helper function host_iommu_base_device_init to initialize it.

Review

Quoted 8 lines
---
  include/sysemu/host_iommu_device.h | 22 ++++++++++++++++++++++
  1 file changed, 22 insertions(+)

create mode 100644 include/sysemu/host_iommu_device.h

(quoted diff) +0 -0

b/include/sysemu/host_iommu_device.h

Quoted 10 lines

new file mode 100644
index 0000000000..fe80ab25fb

include/sysemu/host_iommu_device.h new file +5 -0
@@ -0,0 +1,22 @@
+#ifndef HOST_IOMMU_DEVICE_H
+#define HOST_IOMMU_DEVICE_H
+
+typedef enum HostIOMMUDevice_Type {
+    HID_LEGACY,
Quoted 8 lines

include/sysemu/host_iommu_device.h line 11

Quoted 6 lines
include/sysemu/host_iommu_device.h new file +6 -0
+    HID_IOMMUFD,
+    HID_MAX,
+} HostIOMMUDevice_Type;
+
+typedef struct HostIOMMUDevice {
+    HostIOMMUDevice_Type type;

A type field is not a good sign and that's where QOM is useful.

Yes, agree.
I didn't choose QOM because in iommufd-cdev series, VFIOContainer chooses not using QOM model. See the discussion: https://lore.kernel.org/all/YmuFv2s5TPuw7K%2Fu@yekko/
I thought HostIOMMUDevice need to follow same rule.

But after further digging into this, I think it may be ok to use QOM model as long as we don't expose HostIOMMUDevice in qapi/qom.json and not use USER_CREATABLE interface. Your thoughts?

Quoted 2 lines

Is vtd_check_hdev() the only use of this field ?

Currently yes. virtio-iommu may have similar usage.

Quoted 1 line

If so, can we simplify with a QOM interface in any way ?

QOM interface is a set of callbacks, guess you mean QOM class, saying HostIOMMUDevice class, IOMMULegacyDevice class and IOMMUFDDevice class?

Thanks
Zhenzhong

Quoted 19 lines

Thanks,

C.

Quoted 11 lines
include/sysemu/host_iommu_device.h new file +11 -0
+    size_t size;
+} HostIOMMUDevice;
+
+static inline void host_iommu_base_device_init(HostIOMMUDevice *dev,
+                                               HostIOMMUDevice_Type type,
+                                               size_t size)
+{
+    dev->type = type;
+    dev->size = size;
+}
+#endif
#6 lore raw

Re: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Hello Zhenzhong,

Quoted 51 lines · Zhenzhong

Hi Cédric,

Quoted 40 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, March 19, 2024 4:17 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]; Tian, Kevin <[email protected]>; Liu, Yi L <[email protected]>; Sun, Yi Y <[email protected]>; Peng, Chao P <[email protected]>
Subject: Re: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Hello Zhenzhong,

Quoted 5 lines · Zhenzhong Duan

HostIOMMUDevice will be inherited by two sub classes, legacy and iommufd currently.

Introduce a helper function host_iommu_base_device_init to initialize it.

Review

Quoted 8 lines
---
   include/sysemu/host_iommu_device.h | 22 ++++++++++++++++++++++
   1 file changed, 22 insertions(+)

create mode 100644 include/sysemu/host_iommu_device.h

(quoted diff) +0 -0

b/include/sysemu/host_iommu_device.h

Quoted 10 lines

new file mode 100644
index 0000000000..fe80ab25fb

include/sysemu/host_iommu_device.h new file +5 -0
@@ -0,0 +1,22 @@
+#ifndef HOST_IOMMU_DEVICE_H
+#define HOST_IOMMU_DEVICE_H
+
+typedef enum HostIOMMUDevice_Type {
+    HID_LEGACY,
Quoted 8 lines

include/sysemu/host_iommu_device.h line 11

Quoted 6 lines
include/sysemu/host_iommu_device.h new file +6 -0
+    HID_IOMMUFD,
+    HID_MAX,
+} HostIOMMUDevice_Type;
+
+typedef struct HostIOMMUDevice {
+    HostIOMMUDevice_Type type;

A type field is not a good sign and that's where QOM is useful.

Quoted 7 lines

Yes, agree.
I didn't choose QOM because in iommufd-cdev series, VFIOContainer chooses not using QOM model. See the discussion: https://lore.kernel.org/all/YmuFv2s5TPuw7K%2Fu@yekko/
I thought HostIOMMUDevice need to follow same rule.

But after further digging into this, I think it may be ok to use QOM model as long as we don't expose HostIOMMUDevice in qapi/qom.json and not use USER_CREATABLE interface. Your thoughts?

yes. Can we change a bit this series to use QOM ? something like :

    typedef struct HostIOMMUDevice {
        Object parent;
    } HostIOMMUDevice;
    
    #define TYPE_HOST_IOMMU "host.iommu"
    OBJECT_DECLARE_TYPE(HostIOMMUDevice, HostIOMMUClass, HOST_IOMMU)
    
    struct HostIOMMUClass {
        ObjectClass parent_class;
    
        int (*get_type)(HostIOMMUDevice *hiod, uint64_t *type, Error **errp);
        int (*get_cap)(HostIOMMUDevice *hiod, uint64_t *cap, Error **errp);
    };

Inherited objects would be TYPE_HOST_IOMMU_IOMMUFD and TYPE_HOST_IOMMU_LEGACY. Each class implementing the handlers or not (legacy mode).

The class handlers are introduced for the intel-iommu helper vtd_check_hdev() in order to avoid using iommufd routines directly. HostIOMMUDevice is supposed to abstract the Host IOMMU device, so we need to abstract also all the interfaces to this object.

The .host_iommu_device_create() handler could be merged in .attach_device() possibly. Anyhow, please use now object_new() and object_unref() instead. host_iommu_base_device_init() is useless IMHO.

Quoted 10 lines
Quoted 3 lines

Is vtd_check_hdev() the only use of this field ?

Currently yes. virtio-iommu may have similar usage.

Quoted 1 line

If so, can we simplify with a QOM interface in any way ?

QOM interface is a set of callbacks, guess you mean QOM class, saying HostIOMMUDevice class, IOMMULegacyDevice class and IOMMUFDDevice class?

See above proposal. it should work fine.

Also, I think it is better to use a IOMMUFDBackend* parameter for iommufd_device_get_info() to be consistent with the other routines.

Then It would interesting to see how this applies to Eric's series.

Thanks,

C.

#7 lore raw

RE: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Hi Cédric,

Quoted 89 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Hello Zhenzhong,

Quoted 27 lines · Zhenzhong

Hi Cédric,

Quoted 25 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, March 19, 2024 4:17 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]; Tian, Kevin <[email protected]>; Liu, Yi L <[email protected]>; Sun, Yi Y <[email protected]>; Peng, Chao P <[email protected]>
Subject: Re: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Hello Zhenzhong,

Quoted 9 lines · Zhenzhong Duan

HostIOMMUDevice will be inherited by two sub classes, legacy and iommufd currently.

Introduce a helper function host_iommu_base_device_init to initialize it.

---
   include/sysemu/host_iommu_device.h | 22

++++++++++++++++++++++

Quoted 24 lines
Quoted 15 lines

Review

Quoted 4 lines
   1 file changed, 22 insertions(+)

create mode 100644 include/sysemu/host_iommu_device.h

(quoted diff) +0 -0

b/include/sysemu/host_iommu_device.h

Quoted 10 lines

new file mode 100644
index 0000000000..fe80ab25fb

include/sysemu/host_iommu_device.h new file +5 -0
@@ -0,0 +1,22 @@
+#ifndef HOST_IOMMU_DEVICE_H
+#define HOST_IOMMU_DEVICE_H
+
+typedef enum HostIOMMUDevice_Type {
+    HID_LEGACY,
Quoted 8 lines

include/sysemu/host_iommu_device.h line 11

Quoted 6 lines
include/sysemu/host_iommu_device.h new file +6 -0
+    HID_IOMMUFD,
+    HID_MAX,
+} HostIOMMUDevice_Type;
+
+typedef struct HostIOMMUDevice {
+    HostIOMMUDevice_Type type;

A type field is not a good sign and that's where QOM is useful.

Quoted 2 lines

Yes, agree.
I didn't choose QOM because in iommufd-cdev series, VFIOContainer

chooses not using QOM model.

Quoted 1 line

See the discussion:

https://lore.kernel.org/all/YmuFv2s5TPuw7K%2Fu@yekko/

Quoted 3 lines

I thought HostIOMMUDevice need to follow same rule.

But after further digging into this, I think it may be ok to use QOM model

as long as we don't expose

Quoted 1 line

HostIOMMUDevice in qapi/qom.json and not use USER_CREATABLE

interface. Your thoughts?

yes. Can we change a bit this series to use QOM ? something like :

    typedef struct HostIOMMUDevice {
        Object parent;
    } HostIOMMUDevice;

    #define TYPE_HOST_IOMMU "host.iommu"
    OBJECT_DECLARE_TYPE(HostIOMMUDevice, HostIOMMUClass,

HOST_IOMMU)

    struct HostIOMMUClass {
        ObjectClass parent_class;

        int (*get_type)(HostIOMMUDevice *hiod, uint64_t *type, Error **errp);
        int (*get_cap)(HostIOMMUDevice *hiod, uint64_t *cap, Error **errp);
    };
Quoted 3 lines

Inherited objects would be TYPE_HOST_IOMMU_IOMMUFD and TYPE_HOST_IOMMU_LEGACY.
Each class implementing the handlers or not (legacy mode).

Understood, thanks for your guide.

Quoted 7 lines

The class handlers are introduced for the intel-iommu helper vtd_check_hdev()
in order to avoid using iommufd routines directly. HostIOMMUDevice is supposed
to abstract the Host IOMMU device, so we need to abstract also all the interfaces to this object.

I'd like to have a minimal adjustment to class handers. Just let me know if you have strong preference.

Cap/ecap is intel_iommu specific, I'd like to make it a bit generic also for arm smmu usage, and merge get_type and get_cap into one function as they both calls ioctl(IOMMU_GET_HW_INFO), something like:
get_info(HostIOMMUDevice *hiod, enum iommu_hw_info_type *type, void **data, void **len, Error **errp);

and let iommu emulater to extract content of *data. For intel_iommu, it's:

struct iommu_hw_info_vtd {
        __u32 flags;
        __u32 __reserved;
        __aligned_u64 cap_reg;
        __aligned_u64 ecap_reg;
};
Quoted 5 lines

The .host_iommu_device_create() handler could be merged
in .attach_device()
possibly. Anyhow, please use now object_new() and object_unref() instead. host_iommu_base_device_init() is useless IMHO.

Good idea, will do.

Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

Is vtd_check_hdev() the only use of this field ?

Currently yes. virtio-iommu may have similar usage.

Quoted 1 line

If so, can we simplify with a QOM interface in any way ?

Quoted 8 lines
Quoted 2 lines

QOM interface is a set of callbacks, guess you mean QOM class, saying HostIOMMUDevice class, IOMMULegacyDevice class and

IOMMUFDDevice class?

See above proposal. it should work fine.

Also, I think it is better to use a IOMMUFDBackend* parameter for iommufd_device_get_info() to be consistent with the other routines.

Sure, then I'd like to also rename it to iommufd_backend_get_device_info().

Thanks
Zhenzhong

Quoted 8 lines

Then It would interesting to see how this applies to Eric's series.

Thanks,

C.

#8 lore raw

Re: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Hello Zhenzhong,

Quoted 105 lines · Zhenzhong

Hi Cédric,

Quoted 89 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Hello Zhenzhong,

Quoted 27 lines · Zhenzhong

Hi Cédric,

Quoted 25 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, March 19, 2024 4:17 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]; Tian, Kevin <[email protected]>; Liu, Yi L <[email protected]>; Sun, Yi Y <[email protected]>; Peng, Chao P <[email protected]>
Subject: Re: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Hello Zhenzhong,

Quoted 9 lines · Zhenzhong Duan

HostIOMMUDevice will be inherited by two sub classes, legacy and iommufd currently.

Introduce a helper function host_iommu_base_device_init to initialize it.

---
    include/sysemu/host_iommu_device.h | 22

++++++++++++++++++++++

Quoted 24 lines
Quoted 15 lines

Review

Quoted 4 lines
    1 file changed, 22 insertions(+)

create mode 100644 include/sysemu/host_iommu_device.h

(quoted diff) +0 -0

b/include/sysemu/host_iommu_device.h

Quoted 10 lines

new file mode 100644
index 0000000000..fe80ab25fb

include/sysemu/host_iommu_device.h new file +5 -0
@@ -0,0 +1,22 @@
+#ifndef HOST_IOMMU_DEVICE_H
+#define HOST_IOMMU_DEVICE_H
+
+typedef enum HostIOMMUDevice_Type {
+    HID_LEGACY,
Quoted 8 lines

include/sysemu/host_iommu_device.h line 11

Quoted 6 lines
include/sysemu/host_iommu_device.h new file +6 -0
+    HID_IOMMUFD,
+    HID_MAX,
+} HostIOMMUDevice_Type;
+
+typedef struct HostIOMMUDevice {
+    HostIOMMUDevice_Type type;

A type field is not a good sign and that's where QOM is useful.

Quoted 2 lines

Yes, agree.
I didn't choose QOM because in iommufd-cdev series, VFIOContainer

chooses not using QOM model.

Quoted 1 line

See the discussion:

https://lore.kernel.org/all/YmuFv2s5TPuw7K%2Fu@yekko/

Quoted 3 lines

I thought HostIOMMUDevice need to follow same rule.

But after further digging into this, I think it may be ok to use QOM model

as long as we don't expose

Quoted 1 line

HostIOMMUDevice in qapi/qom.json and not use USER_CREATABLE

interface. Your thoughts?

yes. Can we change a bit this series to use QOM ? something like :

     typedef struct HostIOMMUDevice {
         Object parent;
     } HostIOMMUDevice;

     #define TYPE_HOST_IOMMU "host.iommu"
     OBJECT_DECLARE_TYPE(HostIOMMUDevice, HostIOMMUClass,

HOST_IOMMU)

     struct HostIOMMUClass {
         ObjectClass parent_class;

         int (*get_type)(HostIOMMUDevice *hiod, uint64_t *type, Error **errp);
         int (*get_cap)(HostIOMMUDevice *hiod, uint64_t *cap, Error **errp);
     };
Quoted 3 lines

Inherited objects would be TYPE_HOST_IOMMU_IOMMUFD and TYPE_HOST_IOMMU_LEGACY.
Each class implementing the handlers or not (legacy mode).

Understood, thanks for your guide.

Quoted 7 lines

The class handlers are introduced for the intel-iommu helper vtd_check_hdev()
in order to avoid using iommufd routines directly. HostIOMMUDevice is supposed
to abstract the Host IOMMU device, so we need to abstract also all the interfaces to this object.

Quoted 7 lines

I'd like to have a minimal adjustment to class handers. Just let me know if you have strong preference.

Cap/ecap is intel_iommu specific, I'd like to make it a bit generic also for arm smmu usage, and merge get_type and get_cap into one function as they both calls ioctl(IOMMU_GET_HW_INFO), something like:
get_info(HostIOMMUDevice *hiod, enum iommu_hw_info_type *type, void **data, void **len, Error **errp);

OK. Let's see how it goes. Having more users of this new object Host IOMMU device is important to get a better feeling of the interface. As of today, it doesn't have not much value. The iommufd object could be QOM linked to the vIOMMU when available and we could get the bind devid in some other ways I suppose. Anyhow, please keep it simple and let's explore.

Thanks,

C.

Quoted 50 lines

and let iommu emulater to extract content of *data. For intel_iommu, it's:

struct iommu_hw_info_vtd {
         __u32 flags;
         __u32 __reserved;
         __aligned_u64 cap_reg;
         __aligned_u64 ecap_reg;
};
Quoted 5 lines

The .host_iommu_device_create() handler could be merged
in .attach_device()
possibly. Anyhow, please use now object_new() and object_unref() instead. host_iommu_base_device_init() is useless IMHO.

Good idea, will do.

Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

Is vtd_check_hdev() the only use of this field ?

Currently yes. virtio-iommu may have similar usage.

Quoted 1 line

If so, can we simplify with a QOM interface in any way ?

Quoted 8 lines
Quoted 2 lines

QOM interface is a set of callbacks, guess you mean QOM class, saying HostIOMMUDevice class, IOMMULegacyDevice class and

IOMMUFDDevice class?

See above proposal. it should work fine.

Also, I think it is better to use a IOMMUFDBackend* parameter for iommufd_device_get_info() to be consistent with the other routines.

Sure, then I'd like to also rename it to iommufd_backend_get_device_info().

Thanks
Zhenzhong

Quoted 8 lines

Then It would interesting to see how this applies to Eric's series.

Thanks,

C.

#9 lore raw

RE: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Quoted 130 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Hello Zhenzhong,

Quoted 20 lines · Zhenzhong

Hi Cédric,

Quoted 18 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Hello Zhenzhong,

Quoted 10 lines · Zhenzhong

Hi Cédric,

Quoted 8 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, March 19, 2024 4:17 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];

Tian,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Kevin <[email protected]>; Liu, Yi L <[email protected]>; Sun, Yi Y <[email protected]>; Peng, Chao P <[email protected]>
Subject: Re: [PATCH v1 01/11] Introduce a common abstract struct HostIOMMUDevice

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Hello Zhenzhong,

Quoted 4 lines · Zhenzhong Duan

HostIOMMUDevice will be inherited by two sub classes, legacy and iommufd currently.

Introduce a helper function host_iommu_base_device_init to

initialize it.

Quoted 30 lines
Quoted 30 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
---
    include/sysemu/host_iommu_device.h | 22

++++++++++++++++++++++

Quoted 24 lines
Quoted 23 lines

Review

Quoted 4 lines
    1 file changed, 22 insertions(+)

create mode 100644 include/sysemu/host_iommu_device.h

(quoted diff) +0 -0

b/include/sysemu/host_iommu_device.h

Quoted 6 lines

new file mode 100644
index 0000000000..fe80ab25fb

include/sysemu/host_iommu_device.h new file +1 -0
@@ -0,0 +1,22 @@
+#ifndef HOST_IOMMU_DEVICE_H

include/sysemu/host_iommu_device.h line 11

Quoted 10 lines
include/sysemu/host_iommu_device.h new file +10 -0
+#define HOST_IOMMU_DEVICE_H
+
+typedef enum HostIOMMUDevice_Type {
+    HID_LEGACY,
+    HID_IOMMUFD,
+    HID_MAX,
+} HostIOMMUDevice_Type;
+
+typedef struct HostIOMMUDevice {
+    HostIOMMUDevice_Type type;

A type field is not a good sign and that's where QOM is useful.

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

Yes, agree.
I didn't choose QOM because in iommufd-cdev series, VFIOContainer

chooses not using QOM model.

Quoted 1 line

See the discussion:

https://lore.kernel.org/all/YmuFv2s5TPuw7K%2Fu@yekko/

Quoted 3 lines

I thought HostIOMMUDevice need to follow same rule.

But after further digging into this, I think it may be ok to use QOM

model

Quoted 10 lines
Quoted 10 lines

as long as we don't expose

Quoted 1 line

HostIOMMUDevice in qapi/qom.json and not use USER_CREATABLE

interface. Your thoughts?

yes. Can we change a bit this series to use QOM ? something like :

     typedef struct HostIOMMUDevice {
         Object parent;
     } HostIOMMUDevice;
Quoted 8 lines
Quoted 8 lines
     #define TYPE_HOST_IOMMU "host.iommu"
     OBJECT_DECLARE_TYPE(HostIOMMUDevice, HostIOMMUClass,

HOST_IOMMU)

     struct HostIOMMUClass {
         ObjectClass parent_class;

         int (*get_type)(HostIOMMUDevice *hiod, uint64_t *type, Error

**errp);

Quoted 1 line
Quoted 1 line

int (*get_cap)(HostIOMMUDevice *hiod, uint64_t *cap, Error

**errp);

Quoted 9 lines
Quoted 5 lines
     };

Inherited objects would be TYPE_HOST_IOMMU_IOMMUFD and TYPE_HOST_IOMMU_LEGACY.
Each class implementing the handlers or not (legacy mode).

Understood, thanks for your guide.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines

The class handlers are introduced for the intel-iommu helper vtd_check_hdev()
in order to avoid using iommufd routines directly. HostIOMMUDevice is supposed
to abstract the Host IOMMU device, so we need to abstract also all the interfaces to this object.

I'd like to have a minimal adjustment to class handers. Just let me know if

you have strong

Quoted 3 lines

preference.

Cap/ecap is intel_iommu specific, I'd like to make it a bit generic also for

arm smmu usage,

Quoted 1 line

and merge get_type and get_cap into one function as they both calls

ioctl(IOMMU_GET_HW_INFO),

Quoted 2 lines

something like:
get_info(HostIOMMUDevice *hiod, enum iommu_hw_info_type *type,

Quoted 8 lines

void **data, void **len, Error **errp);

OK. Let's see how it goes. Having more users of this new object Host IOMMU device is important to get a better feeling of the interface. As of today, it doesn't have not much value. The iommufd object could be QOM linked to the vIOMMU when available and we could get the bind devid in some other ways I suppose. Anyhow, please keep it simple and let's explore.

Got it, thanks Cédric!

BRs.
Zhenzhong

#10 lore raw

PATCH 2/11 backends/iommufd: Introduce IOMMUFDDevice

In reply to Zhenzhong Duan

IOMMUFDDevice represents a device in iommufd and can be used as a communication interface between devices (i.e., VFIO, VDPA) and vIOMMU.

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

There will also be some elements in private field in future, i.e., capability bits for dirty tracking; when nested translation is supported in future, vIOMMU is going to have more iommufd related operations like allocate hwpt for a device, attach/detach hwpt, etc. So IOMMUFDDevice will be further extended with those needs.

IOMMUFDDevice is willingly not a QOM object because we don't want it to be visible from the user interface.

Introduce a helper iommufd_device_init to initialize IOMMUFDDevice.

---
 include/sysemu/iommufd.h | 15 +++++++++++++++
 backends/iommufd.c       |  9 +++++++++
 2 files changed, 24 insertions(+)
include/sysemu/iommufd.h +15 -0
index 9af27ebd6c..d509ff88ef 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,18 @@ 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);
+
+
+/* Abstraction of host IOMMUFD device */
+typedef struct IOMMUFDDevice {
+    /* private: */
+    HostIOMMUDevice base;
+
+    /* public: */
+    IOMMUFDBackend *iommufd;
+    uint32_t devid;
+} IOMMUFDDevice;
+
+void iommufd_device_init(IOMMUFDDevice *idev,
+                         IOMMUFDBackend *iommufd, int devid);
 #endif
backends/iommufd.c +9 -0
index 1ef683c7b0..6d280e4aea 100644
@@ -231,3 +231,12 @@ static void register_types(void)
 }
 
 type_init(register_types);
+
+void iommufd_device_init(IOMMUFDDevice *idev,
+                         IOMMUFDBackend *iommufd, int devid)
+{
+    host_iommu_base_device_init(&idev->base, HID_IOMMUFD,
+                                sizeof(IOMMUFDDevice));
+    idev->iommufd = iommufd;
+    idev->devid = devid;
+}
Signature · 1 line
2.34.1
#11 lore raw

PATCH 3/11 vfio: Introduce IOMMULegacyDevice

In reply to Zhenzhong Duan

Similar as IOMMUFDDevice, IOMMULegacyDevice represents a device in legacy mode and can be used as a communication interface between devices (i.e., VFIO, VDPA) and vIOMMU.

Currently it includes nothing legacy specific, but could be extended with any wanted info of legacy mode when necessary.

IOMMULegacyDevice is willingly not a QOM object because we don't want it to be visible from the user interface.

---
 include/hw/vfio/vfio-common.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/hw/vfio/vfio-common.h +6 -0
index 9b7ef7d02b..8bfb9cbe94 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: "
 
@@ -97,6 +98,11 @@ typedef struct VFIOIOMMUFDContainer {
     uint32_t ioas_id;
 } VFIOIOMMUFDContainer;
 
+/* Abstraction of host IOMMU legacy device */
+typedef struct IOMMULegacyDevice {
+    HostIOMMUDevice base;
+} IOMMULegacyDevice;
+
 typedef struct VFIODeviceOps VFIODeviceOps;
 
 typedef struct VFIODevice {
Signature · 1 line
2.34.1
#12 lore raw

PATCH 4/11 vfio: Add HostIOMMUDevice handle into VFIODevice

In reply to Zhenzhong Duan

This handle points to either IOMMULegacyDevice or IOMMUFDDevice variant, neither both.

---
 include/hw/vfio/vfio-common.h | 1 +
 1 file changed, 1 insertion(+)
include/hw/vfio/vfio-common.h +1 -0
index 8bfb9cbe94..b6676c9f79 100644
@@ -130,6 +130,7 @@ typedef struct VFIODevice {
     OnOffAuto pre_copy_dirty_page_tracking;
     bool dirty_pages_supported;
     bool dirty_tracking;
+    HostIOMMUDevice *base_hdev;
     int devid;
     IOMMUFDBackend *iommufd;
 } VFIODevice;
Signature · 1 line
2.34.1
#13 lore raw

Re: [PATCH v1 04/11] vfio: Add HostIOMMUDevice handle into VFIODevice

Quoted 2 lines · Zhenzhong Duan

This handle points to either IOMMULegacyDevice or IOMMUFDDevice variant, neither both.

I would reword into:
store an handle to the HostIOMMUDevice the VFIODevice is associated with . Its actual nature depends on the backend in use (VFIO or IOMMUFD).

Thanks

Eric

Quoted 18 lines
---
 include/hw/vfio/vfio-common.h | 1 +
 1 file changed, 1 insertion(+)
include/hw/vfio/vfio-common.h +1 -0
index 8bfb9cbe94..b6676c9f79 100644
@@ -130,6 +130,7 @@ typedef struct VFIODevice {
     OnOffAuto pre_copy_dirty_page_tracking;
     bool dirty_pages_supported;
     bool dirty_tracking;
+    HostIOMMUDevice *base_hdev;
     int devid;
     IOMMUFDBackend *iommufd;
 } VFIODevice;
#14 lore raw

RE: [PATCH v1 04/11] vfio: Add HostIOMMUDevice handle into VFIODevice

Quoted 9 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v1 04/11] vfio: Add HostIOMMUDevice handle into VFIODevice

Quoted 7 lines
Quoted 1 line · Zhenzhong Duan

This handle points to either IOMMULegacyDevice or IOMMUFDDevice

variant,

Quoted 1 line

neither both.

I would reword into:
store an handle to the HostIOMMUDevice the VFIODevice is associated with . Its actual nature depends on the backend in use (VFIO or IOMMUFD).

More clear, thanks Eric, will use it.

Zhenzhong

Quoted 23 lines

Thanks

Eric

include/hw/vfio/vfio-

Quoted 7 lines
---
 include/hw/vfio/vfio-common.h | 1 +
 1 file changed, 1 insertion(+)
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-common.h +0 -0

common.h

Quoted 11 lines

index 8bfb9cbe94..b6676c9f79 100644

include/hw/vfio/vfio-common.h +1 -0
@@ -130,6 +130,7 @@ typedef struct VFIODevice {
     OnOffAuto pre_copy_dirty_page_tracking;
     bool dirty_pages_supported;
     bool dirty_tracking;
+    HostIOMMUDevice *base_hdev;
     int devid;
     IOMMUFDBackend *iommufd;
 } VFIODevice;
#15 lore raw

PATCH 5/11 vfio: Introduce host_iommu_device_create callback

In reply to Zhenzhong Duan

Introduce host_iommu_device_create callback and a wrapper for it.

This callback is used to allocate a host iommu device instance and initialize it based on type.

---
 include/hw/vfio/vfio-common.h         | 1 +
 include/hw/vfio/vfio-container-base.h | 1 +
 hw/vfio/common.c                      | 8 ++++++++
 3 files changed, 10 insertions(+)
include/hw/vfio/vfio-common.h +1 -0
index b6676c9f79..9fefea4b89 100644
@@ -208,6 +208,7 @@ struct vfio_device_info *vfio_get_device_info(int fd);
 int vfio_attach_device(char *name, VFIODevice *vbasedev,
                        AddressSpace *as, Error **errp);
 void vfio_detach_device(VFIODevice *vbasedev);
+void host_iommu_device_create(VFIODevice *vbasedev);
 
 int vfio_kvm_device_add_fd(int fd, Error **errp);
 int vfio_kvm_device_del_fd(int fd, Error **errp);
include/hw/vfio/vfio-container-base.h +1 -0
index b2813b0c11..dc003f6eb2 100644
@@ -120,6 +120,7 @@ struct VFIOIOMMUClass {
     int (*attach_device)(const char *name, VFIODevice *vbasedev,
                          AddressSpace *as, Error **errp);
     void (*detach_device)(VFIODevice *vbasedev);
+    void (*host_iommu_device_create)(VFIODevice *vbasedev);
     /* migration feature */
     int (*set_dirty_page_tracking)(const VFIOContainerBase *bcontainer,
                                    bool start);
hw/vfio/common.c +8 -0
index 059bfdc07a..41e9031c59 100644
@@ -1521,3 +1521,11 @@ void vfio_detach_device(VFIODevice *vbasedev)
     }
     vbasedev->bcontainer->ops->detach_device(vbasedev);
 }
+
+void host_iommu_device_create(VFIODevice *vbasedev)
+{
+    const VFIOIOMMUClass *ops = vbasedev->bcontainer->ops;
+
+    assert(ops->host_iommu_device_create);
+    ops->host_iommu_device_create(vbasedev);
+}
Signature · 1 line
2.34.1
#16 lore raw

Re: [PATCH v1 05/11] vfio: Introduce host_iommu_device_create callback

Hi ZHenzhong,

Quoted 41 lines · Zhenzhong Duan

Introduce host_iommu_device_create callback and a wrapper for it.

This callback is used to allocate a host iommu device instance and initialize it based on type.

---
 include/hw/vfio/vfio-common.h         | 1 +
 include/hw/vfio/vfio-container-base.h | 1 +
 hw/vfio/common.c                      | 8 ++++++++
 3 files changed, 10 insertions(+)
include/hw/vfio/vfio-common.h +1 -0
index b6676c9f79..9fefea4b89 100644
@@ -208,6 +208,7 @@ struct vfio_device_info *vfio_get_device_info(int fd);
 int vfio_attach_device(char *name, VFIODevice *vbasedev,
                        AddressSpace *as, Error **errp);
 void vfio_detach_device(VFIODevice *vbasedev);
+void host_iommu_device_create(VFIODevice *vbasedev);
 
 int vfio_kvm_device_add_fd(int fd, Error **errp);
 int vfio_kvm_device_del_fd(int fd, Error **errp);
include/hw/vfio/vfio-container-base.h +1 -0
index b2813b0c11..dc003f6eb2 100644
@@ -120,6 +120,7 @@ struct VFIOIOMMUClass {
     int (*attach_device)(const char *name, VFIODevice *vbasedev,
                          AddressSpace *as, Error **errp);
     void (*detach_device)(VFIODevice *vbasedev);
+    void (*host_iommu_device_create)(VFIODevice *vbasedev);
     /* migration feature */
     int (*set_dirty_page_tracking)(const VFIOContainerBase *bcontainer,
                                    bool start);
hw/vfio/common.c +0 -0
index 059bfdc07a..41e9031c59 100644

hw/vfio/common.c void vfio_detach_device(VFIODevice *vbasedev) · line 1529

Quoted 9 lines
hw/vfio/common.c +6 -0
@@ -1521,3 +1521,11 @@ void vfio_detach_device(VFIODevice *vbasedev)
     }
     vbasedev->bcontainer->ops->detach_device(vbasedev);
 }
+
+void host_iommu_device_create(VFIODevice *vbasedev)
+{
+    const VFIOIOMMUClass *ops = vbasedev->bcontainer->ops;
+
+    assert(ops->host_iommu_device_create);

at this stage ops actual implementation do not exist yet so this will break the bisection

Eric

Quoted 2 lines
hw/vfio/common.c +2 -0
+    ops->host_iommu_device_create(vbasedev);
+}
#17 lore raw

Re: [PATCH v1 05/11] vfio: Introduce host_iommu_device_create callback

Quoted 47 lines · Eric Auger

Hi ZHenzhong,

Quoted 44 lines · Zhenzhong Duan

Introduce host_iommu_device_create callback and a wrapper for it.

This callback is used to allocate a host iommu device instance and initialize it based on type.

---
 include/hw/vfio/vfio-common.h         | 1 +
 include/hw/vfio/vfio-container-base.h | 1 +
 hw/vfio/common.c                      | 8 ++++++++
 3 files changed, 10 insertions(+)
include/hw/vfio/vfio-common.h +1 -0
index b6676c9f79..9fefea4b89 100644
@@ -208,6 +208,7 @@ struct vfio_device_info *vfio_get_device_info(int fd);
 int vfio_attach_device(char *name, VFIODevice *vbasedev,
                        AddressSpace *as, Error **errp);
 void vfio_detach_device(VFIODevice *vbasedev);
+void host_iommu_device_create(VFIODevice *vbasedev);
 
 int vfio_kvm_device_add_fd(int fd, Error **errp);
 int vfio_kvm_device_del_fd(int fd, Error **errp);
include/hw/vfio/vfio-container-base.h +1 -0
index b2813b0c11..dc003f6eb2 100644
@@ -120,6 +120,7 @@ struct VFIOIOMMUClass {
     int (*attach_device)(const char *name, VFIODevice *vbasedev,
                          AddressSpace *as, Error **errp);
     void (*detach_device)(VFIODevice *vbasedev);
+    void (*host_iommu_device_create)(VFIODevice *vbasedev);
     /* migration feature */
     int (*set_dirty_page_tracking)(const VFIOContainerBase *bcontainer,
                                    bool start);
hw/vfio/common.c +0 -0
index 059bfdc07a..41e9031c59 100644
@@ -1521,3 +1521,11 @@ void vfio_detach_device(VFIODevice *vbasedev)
     }
     vbasedev->bcontainer->ops->detach_device(vbasedev);
 }
Quoted 8 lines

hw/vfio/common.c void vfio_detach_device(VFIODevice *vbasedev) · line 1529

Quoted 6 lines
hw/vfio/common.c +6 -0
+
+void host_iommu_device_create(VFIODevice *vbasedev)
+{
+    const VFIOIOMMUClass *ops = vbasedev->bcontainer->ops;
+
+    assert(ops->host_iommu_device_create);

at this stage ops actual implementation do not exist yet so this will break the bisection

Sorry it is OK at the function only is called in
[PATCH v1 08/11] vfio/pci: Allocate and initialize HostIOMMUDevice after attachment

Sorry for the noise

Eric

Quoted 5 lines

Eric

Quoted 2 lines
hw/vfio/common.c +2 -0
+    ops->host_iommu_device_create(vbasedev);
+}
#18 lore raw

RE: [PATCH v1 05/11] vfio: Introduce host_iommu_device_create callback

Quoted 70 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v1 05/11] vfio: Introduce host_iommu_device_create callback

Quoted 8 lines · Eric Auger

Hi ZHenzhong,

Quoted 5 lines · Zhenzhong Duan

Introduce host_iommu_device_create callback and a wrapper for it.

This callback is used to allocate a host iommu device instance and initialize it based on type.

Quoted 8 lines

include/hw/vfio/vfio-

Quoted 8 lines
---
 include/hw/vfio/vfio-common.h         | 1 +
 include/hw/vfio/vfio-container-base.h | 1 +
 hw/vfio/common.c                      | 8 ++++++++
 3 files changed, 10 insertions(+)
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-common.h +0 -0

common.h

Quoted 4 lines

include/hw/vfio/vfio-common.h struct vfio_device_info *vfio_get_device_info(int

Quoted 4 lines

index b6676c9f79..9fefea4b89 100644

include/hw/vfio/vfio-common.h +0 -0
@@ -208,6 +208,7 @@ struct vfio_device_info *vfio_get_device_info(int

fd);

Quoted 8 lines

include/hw/vfio/vfio-

Quoted 8 lines
include/hw/vfio/vfio-common.h +1 -0
 int vfio_attach_device(char *name, VFIODevice *vbasedev,
                        AddressSpace *as, Error **errp);
 void vfio_detach_device(VFIODevice *vbasedev);
+void host_iommu_device_create(VFIODevice *vbasedev);
 
 int vfio_kvm_device_add_fd(int fd, Error **errp);
 int vfio_kvm_device_del_fd(int fd, Error **errp);
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-container-base.h +0 -0

container-base.h

Quoted 8 lines
Quoted 8 lines

index b2813b0c11..dc003f6eb2 100644

include/hw/vfio/vfio-container-base.h +1 -0
@@ -120,6 +120,7 @@ struct VFIOIOMMUClass {
     int (*attach_device)(const char *name, VFIODevice *vbasedev,
                          AddressSpace *as, Error **errp);
     void (*detach_device)(VFIODevice *vbasedev);
+    void (*host_iommu_device_create)(VFIODevice *vbasedev);
Quoted 8 lines

hw/vfio/common.c void vfio_detach_device(VFIODevice

Quoted 8 lines
include/hw/vfio/vfio-container-base.h +0 -0
     /* migration feature */
     int (*set_dirty_page_tracking)(const VFIOContainerBase *bcontainer,
                                    bool start);
hw/vfio/common.c +0 -0
index 059bfdc07a..41e9031c59 100644
@@ -1521,3 +1521,11 @@ void vfio_detach_device(VFIODevice

*vbasedev)

Quoted 11 lines

hw/vfio/common.c void vfio_detach_device(VFIODevice *vbasedev) · line 1529

Quoted 9 lines
hw/vfio/common.c +6 -0
     }
     vbasedev->bcontainer->ops->detach_device(vbasedev);
 }
+
+void host_iommu_device_create(VFIODevice *vbasedev)
+{
+    const VFIOIOMMUClass *ops = vbasedev->bcontainer->ops;
+
+    assert(ops->host_iommu_device_create);

at this stage ops actual implementation do not exist yet so this will break the bisection

Quoted 5 lines

Sorry it is OK at the function only is called in
[PATCH v1 08/11] vfio/pci: Allocate and initialize HostIOMMUDevice after attachment

Sorry for the noise

Ah, send too quickly. No problem.

Thanks
Zhenzhong

#19 lore raw

RE: [PATCH v1 05/11] vfio: Introduce host_iommu_device_create callback

In reply to Eric Auger

Hi Eric,

Quoted 53 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v1 05/11] vfio: Introduce host_iommu_device_create callback

Hi ZHenzhong,

include/hw/vfio/vfio-

Quoted 13 lines · Zhenzhong Duan

Introduce host_iommu_device_create callback and a wrapper for it.

This callback is used to allocate a host iommu device instance and initialize it based on type.

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

common.h

include/hw/vfio/vfio-common.h struct vfio_device_info *vfio_get_device_info(int

Quoted 4 lines

index b6676c9f79..9fefea4b89 100644

include/hw/vfio/vfio-common.h +0 -0
@@ -208,6 +208,7 @@ struct vfio_device_info *vfio_get_device_info(int

fd);

include/hw/vfio/vfio-

Quoted 8 lines
include/hw/vfio/vfio-common.h +1 -0
 int vfio_attach_device(char *name, VFIODevice *vbasedev,
                        AddressSpace *as, Error **errp);
 void vfio_detach_device(VFIODevice *vbasedev);
+void host_iommu_device_create(VFIODevice *vbasedev);
 
 int vfio_kvm_device_add_fd(int fd, Error **errp);
 int vfio_kvm_device_del_fd(int fd, Error **errp);
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-container-base.h +0 -0

container-base.h

hw/vfio/common.c void vfio_detach_device(VFIODevice

Quoted 16 lines

index b2813b0c11..dc003f6eb2 100644

include/hw/vfio/vfio-container-base.h +1 -0
@@ -120,6 +120,7 @@ struct VFIOIOMMUClass {
     int (*attach_device)(const char *name, VFIODevice *vbasedev,
                          AddressSpace *as, Error **errp);
     void (*detach_device)(VFIODevice *vbasedev);
+    void (*host_iommu_device_create)(VFIODevice *vbasedev);
     /* migration feature */
     int (*set_dirty_page_tracking)(const VFIOContainerBase *bcontainer,
                                    bool start);
hw/vfio/common.c +0 -0
index 059bfdc07a..41e9031c59 100644
@@ -1521,3 +1521,11 @@ void vfio_detach_device(VFIODevice

*vbasedev)

Quoted 11 lines

hw/vfio/common.c void vfio_detach_device(VFIODevice *vbasedev) · line 1529

Quoted 9 lines
hw/vfio/common.c +6 -0
     }
     vbasedev->bcontainer->ops->detach_device(vbasedev);
 }
+
+void host_iommu_device_create(VFIODevice *vbasedev)
+{
+    const VFIOIOMMUClass *ops = vbasedev->bcontainer->ops;
+
+    assert(ops->host_iommu_device_create);

at this stage ops actual implementation do not exist yet so this will break the bisection

This patch only introcudes host_iommu_device_create but no one call into it. Patch6-7 implement callback for different backend, patch8 call host_iommu_device_create(), so I think the order is ok. Let me know if I missed your points.

Thanks
Zhenzhong

Quoted 4 lines

Eric

Quoted 2 lines
hw/vfio/common.c +2 -0
+    ops->host_iommu_device_create(vbasedev);
+}
#20 lore raw

Re: [PATCH v1 05/11] vfio: Introduce host_iommu_device_create callback

In reply to Zhenzhong Duan
Quoted 29 lines · Zhenzhong Duan

Introduce host_iommu_device_create callback and a wrapper for it.

This callback is used to allocate a host iommu device instance and initialize it based on type.

---
 include/hw/vfio/vfio-common.h         | 1 +
 include/hw/vfio/vfio-container-base.h | 1 +
 hw/vfio/common.c                      | 8 ++++++++
 3 files changed, 10 insertions(+)
include/hw/vfio/vfio-common.h +1 -0
index b6676c9f79..9fefea4b89 100644
@@ -208,6 +208,7 @@ struct vfio_device_info *vfio_get_device_info(int fd);
 int vfio_attach_device(char *name, VFIODevice *vbasedev,
                        AddressSpace *as, Error **errp);
 void vfio_detach_device(VFIODevice *vbasedev);
+void host_iommu_device_create(VFIODevice *vbasedev);
 
 int vfio_kvm_device_add_fd(int fd, Error **errp);
 int vfio_kvm_device_del_fd(int fd, Error **errp);
include/hw/vfio/vfio-container-base.h +0 -0
index b2813b0c11..dc003f6eb2 100644

include/hw/vfio/vfio-container-base.h struct VFIOIOMMUClass { · line 123

Quoted 4 lines
include/hw/vfio/vfio-container-base.h +1 -0
@@ -120,6 +120,7 @@ struct VFIOIOMMUClass {
     int (*attach_device)(const char *name, VFIODevice *vbasedev,
                          AddressSpace *as, Error **errp);
     void (*detach_device)(VFIODevice *vbasedev);
+    void (*host_iommu_device_create)(VFIODevice *vbasedev);

Maybe return an int instead. It is common the allocation can fail and the deallocation cannot. While at it I would also pass an errp in case it fails

Eric

Quoted 19 lines
include/hw/vfio/vfio-container-base.h +0 -0
     /* migration feature */
     int (*set_dirty_page_tracking)(const VFIOContainerBase *bcontainer,
                                    bool start);
hw/vfio/common.c +8 -0
index 059bfdc07a..41e9031c59 100644
@@ -1521,3 +1521,11 @@ void vfio_detach_device(VFIODevice *vbasedev)
     }
     vbasedev->bcontainer->ops->detach_device(vbasedev);
 }
+
+void host_iommu_device_create(VFIODevice *vbasedev)
+{
+    const VFIOIOMMUClass *ops = vbasedev->bcontainer->ops;
+
+    assert(ops->host_iommu_device_create);
+    ops->host_iommu_device_create(vbasedev);
+}
#21 lore raw

RE: [PATCH v1 05/11] vfio: Introduce host_iommu_device_create callback

Quoted 38 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v1 05/11] vfio: Introduce host_iommu_device_create callback

include/hw/vfio/vfio-

Quoted 13 lines · Zhenzhong Duan

Introduce host_iommu_device_create callback and a wrapper for it.

This callback is used to allocate a host iommu device instance and initialize it based on type.

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

common.h

include/hw/vfio/vfio-common.h struct vfio_device_info *vfio_get_device_info(int

Quoted 4 lines

index b6676c9f79..9fefea4b89 100644

include/hw/vfio/vfio-common.h +0 -0
@@ -208,6 +208,7 @@ struct vfio_device_info *vfio_get_device_info(int

fd);

include/hw/vfio/vfio-

Quoted 8 lines
include/hw/vfio/vfio-common.h +1 -0
 int vfio_attach_device(char *name, VFIODevice *vbasedev,
                        AddressSpace *as, Error **errp);
 void vfio_detach_device(VFIODevice *vbasedev);
+void host_iommu_device_create(VFIODevice *vbasedev);
 
 int vfio_kvm_device_add_fd(int fd, Error **errp);
 int vfio_kvm_device_del_fd(int fd, Error **errp);
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-container-base.h +0 -0

container-base.h

Quoted 11 lines

include/hw/vfio/vfio-container-base.h struct VFIOIOMMUClass { · line 123

Quoted 8 lines

index b2813b0c11..dc003f6eb2 100644

include/hw/vfio/vfio-container-base.h +1 -0
@@ -120,6 +120,7 @@ struct VFIOIOMMUClass {
     int (*attach_device)(const char *name, VFIODevice *vbasedev,
                          AddressSpace *as, Error **errp);
     void (*detach_device)(VFIODevice *vbasedev);
+    void (*host_iommu_device_create)(VFIODevice *vbasedev);

Maybe return an int instead. It is common the allocation can fail and the deallocation cannot. While at it I would also pass an errp in case it fails

Currently host_iommu_device_create implementation only calls g_malloc0, so never fails, so I returned void.

I'm fine to return an int, will be like below, take iommufd for example:

hw/vfio/iommufd.c +3 -1
@@ -651,7 +651,7 @@ static IOMMUFDDeviceOps vfio_iommufd_device_ops = {
     .detach_hwpt = vfio_iommufd_device_detach_hwpt,
 };
 
-static void vfio_cdev_host_iommu_device_create(VFIODevice *vbasedev)
+static int vfio_cdev_host_iommu_device_create(VFIODevice *vbasedev, Error **errp)
 {
     IOMMUFDDevice *idev = g_malloc0(sizeof(IOMMUFDDevice));
     VFIOIOMMUFDContainer *container = container_of(vbasedev->bcontainer,
@@ -661,6 +661,8 @@ static void vfio_cdev_host_iommu_device_create(VFIODevice *vbasedev)
 
     iommufd_device_init(idev, vbasedev->iommufd, vbasedev->devid,
                         container->ioas_id, vbasedev, &vfio_iommufd_device_ops);
+
+    return 0;
 }
 

Thanks
Zhenzhong

Quoted 22 lines

Eric

hw/vfio/common.c void vfio_detach_device(VFIODevice

Quoted 8 lines
include/hw/vfio/vfio-container-base.h +0 -0
     /* migration feature */
     int (*set_dirty_page_tracking)(const VFIOContainerBase *bcontainer,
                                    bool start);
hw/vfio/common.c +0 -0
index 059bfdc07a..41e9031c59 100644
@@ -1521,3 +1521,11 @@ void vfio_detach_device(VFIODevice

*vbasedev)

Quoted 11 lines
hw/vfio/common.c +8 -0
     }
     vbasedev->bcontainer->ops->detach_device(vbasedev);
 }
+
+void host_iommu_device_create(VFIODevice *vbasedev)
+{
+    const VFIOIOMMUClass *ops = vbasedev->bcontainer->ops;
+
+    assert(ops->host_iommu_device_create);
+    ops->host_iommu_device_create(vbasedev);
+}
#22 lore raw

Re: [PATCH v1 05/11] vfio: Introduce host_iommu_device_create callback

Hi Zhenzhong,

Quoted 65 lines · Zhenzhong
Quoted 37 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v1 05/11] vfio: Introduce host_iommu_device_create callback

include/hw/vfio/vfio-

Quoted 13 lines · Zhenzhong Duan

Introduce host_iommu_device_create callback and a wrapper for it.

This callback is used to allocate a host iommu device instance and initialize it based on type.

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

common.h

include/hw/vfio/vfio-common.h struct vfio_device_info *vfio_get_device_info(int

Quoted 4 lines

index b6676c9f79..9fefea4b89 100644

include/hw/vfio/vfio-common.h +0 -0
@@ -208,6 +208,7 @@ struct vfio_device_info *vfio_get_device_info(int

fd);

include/hw/vfio/vfio-

Quoted 8 lines
include/hw/vfio/vfio-common.h +1 -0
 int vfio_attach_device(char *name, VFIODevice *vbasedev,
                        AddressSpace *as, Error **errp);
 void vfio_detach_device(VFIODevice *vbasedev);
+void host_iommu_device_create(VFIODevice *vbasedev);
 
 int vfio_kvm_device_add_fd(int fd, Error **errp);
 int vfio_kvm_device_del_fd(int fd, Error **errp);
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-container-base.h +0 -0

container-base.h

Quoted 11 lines

include/hw/vfio/vfio-container-base.h struct VFIOIOMMUClass { · line 123

Quoted 8 lines

index b2813b0c11..dc003f6eb2 100644

include/hw/vfio/vfio-container-base.h +1 -0
@@ -120,6 +120,7 @@ struct VFIOIOMMUClass {
     int (*attach_device)(const char *name, VFIODevice *vbasedev,
                          AddressSpace *as, Error **errp);
     void (*detach_device)(VFIODevice *vbasedev);
+    void (*host_iommu_device_create)(VFIODevice *vbasedev);

Maybe return an int instead. It is common the allocation can fail and the deallocation cannot. While at it I would also pass an errp in case it fails

Currently host_iommu_device_create implementation only calls g_malloc0, so never fails, so I returned void.

I'm fine to return an int, will be like below, take iommufd for example:

hw/vfio/iommufd.c +1 -1
@@ -651,7 +651,7 @@ static IOMMUFDDeviceOps vfio_iommufd_device_ops = {
     .detach_hwpt = vfio_iommufd_device_detach_hwpt,
 };
 
-static void vfio_cdev_host_iommu_device_create(VFIODevice *vbasedev)
+static int vfio_cdev_host_iommu_device_create(VFIODevice *vbasedev, Error **errp)
 {
     IOMMUFDDevice *idev = g_malloc0(sizeof(IOMMUFDDevice));
     VFIOIOMMUFDContainer *container = container_of(vbasedev->bcontainer,

hw/vfio/iommufd.c static void vfio_cdev_host_iommu_device_create(VFIODevice *vbasedev) · line 666

Quoted 6 lines
hw/vfio/iommufd.c +2 -0
@@ -661,6 +661,8 @@ static void vfio_cdev_host_iommu_device_create(VFIODevice *vbasedev)
 
     iommufd_device_init(idev, vbasedev->iommufd, vbasedev->devid,
                         container->ioas_id, vbasedev, &vfio_iommufd_device_ops);
+
+    return 0;
 }

I think the rationale behind introducing an error handle is to propagate it down to the callbacks. This is were I anticipated errors to occur. But if you don't foresee any maybe we can just wait and postpone this addition.

Thanks

Eric

Quoted 27 lines

Thanks
Zhenzhong

Quoted 21 lines

Eric

hw/vfio/common.c void vfio_detach_device(VFIODevice

Quoted 8 lines
include/hw/vfio/vfio-container-base.h +0 -0
     /* migration feature */
     int (*set_dirty_page_tracking)(const VFIOContainerBase *bcontainer,
                                    bool start);
hw/vfio/common.c +0 -0
index 059bfdc07a..41e9031c59 100644
@@ -1521,3 +1521,11 @@ void vfio_detach_device(VFIODevice

*vbasedev)

Quoted 11 lines
hw/vfio/common.c +8 -0
     }
     vbasedev->bcontainer->ops->detach_device(vbasedev);
 }
+
+void host_iommu_device_create(VFIODevice *vbasedev)
+{
+    const VFIOIOMMUClass *ops = vbasedev->bcontainer->ops;
+
+    assert(ops->host_iommu_device_create);
+    ops->host_iommu_device_create(vbasedev);
+}
#23 lore raw

PATCH 6/11 vfio/container: Implement host_iommu_device_create callback in legacy mode

In reply to Zhenzhong Duan

This callback will be used to initialize base and public elements in IOMMULegacyDevice.

---
 hw/vfio/container.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/vfio/container.c +9 -0
index bd25b9fbad..2e8ff32284 100644
@@ -1120,6 +1120,14 @@ out_single:
     return ret;
 }
 
+static void vfio_legacy_host_iommu_device_create(VFIODevice *vbasedev)
+{
+    vbasedev->base_hdev = g_malloc0(sizeof(IOMMULegacyDevice));
+
+    host_iommu_base_device_init(vbasedev->base_hdev, HID_LEGACY,
+                                sizeof(IOMMULegacyDevice));
+}
+
 static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data)
 {
     VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
@@ -1132,6 +1140,7 @@ static void vfio_iommu_legacy_class_init(ObjectClass *klass, void *data)
     vioc->set_dirty_page_tracking = vfio_legacy_set_dirty_page_tracking;
     vioc->query_dirty_bitmap = vfio_legacy_query_dirty_bitmap;
     vioc->pci_hot_reset = vfio_legacy_pci_hot_reset;
+    vioc->host_iommu_device_create = vfio_legacy_host_iommu_device_create;
 };
 
 static const TypeInfo types[] = {
Signature · 1 line
2.34.1
#24 lore raw

PATCH 7/11 vfio/iommufd: Implement host_iommu_device_create callback in iommufd mode

In reply to Zhenzhong Duan

This callback will be used to initialize base and public elements in IOMMUFDDevice.

---
 hw/vfio/iommufd.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
hw/vfio/iommufd.c +10 -0
index 9bfddc1360..1c2f5da0d0 100644
@@ -619,6 +619,15 @@ out_single:
     return ret;
 }
 
+static void vfio_cdev_host_iommu_device_create(VFIODevice *vbasedev)
+{
+    IOMMUFDDevice *idev = g_malloc0(sizeof(IOMMUFDDevice));
+
+    vbasedev->base_hdev = &idev->base;
+
+    iommufd_device_init(idev, vbasedev->iommufd, vbasedev->devid);
+}
+
 static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)
 {
     VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass);
@@ -628,6 +637,7 @@ static void vfio_iommu_iommufd_class_init(ObjectClass *klass, void *data)
     vioc->attach_device = iommufd_cdev_attach;
     vioc->detach_device = iommufd_cdev_detach;
     vioc->pci_hot_reset = iommufd_cdev_pci_hot_reset;
+    vioc->host_iommu_device_create = vfio_cdev_host_iommu_device_create;
 };
 
 static const TypeInfo types[] = {
Signature · 1 line
2.34.1
#25 lore raw

PATCH 8/11 vfio/pci: Allocate and initialize HostIOMMUDevice after attachment

In reply to Zhenzhong Duan
---
 hw/vfio/pci.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/vfio/pci.c +4 -0
index 4fa387f043..6cc7de5d10 100644
@@ -3006,6 +3006,9 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
         goto error;
     }
 
+    /* Allocate and initialize HostIOMMUDevice after attachment succeed */
+    host_iommu_device_create(vbasedev);
+
     vfio_populate_device(vdev, &err);
     if (err) {
         error_propagate(errp, err);
@@ -3244,6 +3247,7 @@ static void vfio_instance_finalize(Object *obj)
 
     vfio_display_finalize(vdev);
     vfio_bars_finalize(vdev);
+    g_free(vdev->vbasedev.base_hdev);
     g_free(vdev->emulated_config_bits);
     g_free(vdev->rom);
     /*
Signature · 1 line
2.34.1
#26 lore raw

Re: [PATCH v1 08/11] vfio/pci: Allocate and initialize HostIOMMUDevice after attachment

Quoted 10 lines · Zhenzhong Duan
---
 hw/vfio/pci.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/vfio/pci.c +0 -0
index 4fa387f043..6cc7de5d10 100644

hw/vfio/pci.c static void vfio_realize(PCIDevice *pdev, Error **errp) · line 3009

Quoted 4 lines
hw/vfio/pci.c +1 -0
@@ -3006,6 +3006,9 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
         goto error;
     }
 
+    /* Allocate and initialize HostIOMMUDevice after attachment succeed */

after successful attachment?

hw/vfio/pci.c static void vfio_realize(PCIDevice *pdev, Error **errp) · line 3011

Quoted 2 lines
hw/vfio/pci.c +2 -0
+    host_iommu_device_create(vbasedev);
+

you shall free on error: as well

Eric

Quoted 11 lines
hw/vfio/pci.c +1 -0
     vfio_populate_device(vdev, &err);
     if (err) {
         error_propagate(errp, err);
@@ -3244,6 +3247,7 @@ static void vfio_instance_finalize(Object *obj)
 
     vfio_display_finalize(vdev);
     vfio_bars_finalize(vdev);
+    g_free(vdev->vbasedev.base_hdev);
     g_free(vdev->emulated_config_bits);
     g_free(vdev->rom);
     /*
#27 lore raw

RE: [PATCH v1 08/11] vfio/pci: Allocate and initialize HostIOMMUDevice after attachment

Quoted 24 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v1 08/11] vfio/pci: Allocate and initialize HostIOMMUDevice after attachment

hw/vfio/pci.c static void vfio_realize(PCIDevice *pdev, Error

Quoted 10 lines · Zhenzhong Duan
---
 hw/vfio/pci.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/vfio/pci.c +0 -0
index 4fa387f043..6cc7de5d10 100644
@@ -3006,6 +3006,9 @@ static void vfio_realize(PCIDevice *pdev, Error

**errp)

Quoted 3 lines
hw/vfio/pci.c +0 -0
         goto error;
     }
 
Quoted 6 lines

hw/vfio/pci.c static void vfio_realize(PCIDevice *pdev, Error · line 3009

Quoted 1 line
hw/vfio/pci.c +1 -0
+    /* Allocate and initialize HostIOMMUDevice after attachment succeed

*/
after successful attachment?

hw/vfio/pci.c static void vfio_realize(PCIDevice *pdev, Error · line 3011

Quoted 2 lines
hw/vfio/pci.c +2 -0
+    host_iommu_device_create(vbasedev);
+

you shall free on error: as well

I free it in vfio_instance_finalize().
Vfio-pci's design is special, it didn't free all allocated resources in realize's error path,
They are freed in _finalize(). e.g., vdev->emulated_config_bits, vdev->rom, devices and group resources(vfio_detach_device). I'm following the same way. I'm fine to free it as you suggested something like below:

hw/vfio/pci.c +2 -0
@@ -3246,6 +3246,7 @@ out_teardown:
     vfio_teardown_msi(vdev);
     vfio_bars_exit(vdev);
 error:
+    g_free(vdev->vbasedev.base_hdev);
     error_prepend(errp, VFIO_MSG_PREFIX, vbasedev->name);
 }
 
@@ -3288,6 +3289,7 @@ static void vfio_exitfn(PCIDevice *pdev)
     vfio_bars_exit(vdev);
     vfio_migration_exit(vbasedev);
     pci_device_unset_iommu_device(pdev);
+    g_free(vdev->vbasedev.base_hdev);
 }
 
Quoted 10 lines

Eric

hw/vfio/pci.c static void vfio_instance_finalize(Object *obj) · line 3250

Quoted 8 lines
hw/vfio/pci.c +1 -0
     vfio_populate_device(vdev, &err);
     if (err) {
         error_propagate(errp, err);
@@ -3244,6 +3247,7 @@ static void vfio_instance_finalize(Object *obj)
 
     vfio_display_finalize(vdev);
     vfio_bars_finalize(vdev);
+    g_free(vdev->vbasedev.base_hdev);

I free it here.

Thanks
Zhenzhong

Quoted 3 lines
Quoted 3 lines
hw/vfio/pci.c +0 -0
     g_free(vdev->emulated_config_bits);
     g_free(vdev->rom);
     /*
#28 lore raw

Re: [PATCH v1 08/11] vfio/pci: Allocate and initialize HostIOMMUDevice after attachment

Quoted 26 lines · Zhenzhong
Quoted 26 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v1 08/11] vfio/pci: Allocate and initialize HostIOMMUDevice after attachment

hw/vfio/pci.c static void vfio_realize(PCIDevice *pdev, Error

Quoted 10 lines · Zhenzhong Duan
---
 hw/vfio/pci.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/vfio/pci.c +0 -0
index 4fa387f043..6cc7de5d10 100644
@@ -3006,6 +3006,9 @@ static void vfio_realize(PCIDevice *pdev, Error

**errp)

hw/vfio/pci.c static void vfio_realize(PCIDevice *pdev, Error · line 3009

Quoted 4 lines
hw/vfio/pci.c +1 -0
         goto error;
     }
 
+    /* Allocate and initialize HostIOMMUDevice after attachment succeed

*/
after successful attachment?

Quoted 7 lines
Quoted 3 lines

hw/vfio/pci.c static void vfio_realize(PCIDevice *pdev, Error · line 3011

Quoted 2 lines
hw/vfio/pci.c +2 -0
+    host_iommu_device_create(vbasedev);
+

you shall free on error: as well

I free it in vfio_instance_finalize().
Vfio-pci's design is special, it didn't free all allocated resources in realize's error path,
They are freed in _finalize(). e.g., vdev->emulated_config_bits, vdev->rom, devices and group resources(vfio_detach_device). I'm following the same way. I'm fine to free it as you suggested something like below:

Oh yes I remember now. I had exactly the same question some months ago :-/ So that's fine then

Eric

Quoted 35 lines
hw/vfio/pci.c +2 -0
@@ -3246,6 +3246,7 @@ out_teardown:
     vfio_teardown_msi(vdev);
     vfio_bars_exit(vdev);
 error:
+    g_free(vdev->vbasedev.base_hdev);
     error_prepend(errp, VFIO_MSG_PREFIX, vbasedev->name);
 }
 
@@ -3288,6 +3289,7 @@ static void vfio_exitfn(PCIDevice *pdev)
     vfio_bars_exit(vdev);
     vfio_migration_exit(vbasedev);
     pci_device_unset_iommu_device(pdev);
+    g_free(vdev->vbasedev.base_hdev);
 }
 
Quoted 9 lines

Eric

hw/vfio/pci.c static void vfio_instance_finalize(Object *obj) · line 3250

Quoted 8 lines
hw/vfio/pci.c +1 -0
     vfio_populate_device(vdev, &err);
     if (err) {
         error_propagate(errp, err);
@@ -3244,6 +3247,7 @@ static void vfio_instance_finalize(Object *obj)
 
     vfio_display_finalize(vdev);
     vfio_bars_finalize(vdev);
+    g_free(vdev->vbasedev.base_hdev);

I free it here.

Thanks
Zhenzhong

Quoted 3 lines
Quoted 3 lines
hw/vfio/pci.c +0 -0
     g_free(vdev->emulated_config_bits);
     g_free(vdev->rom);
     /*
#29 lore raw

PATCH 9/11 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 PCIe 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 | 38 ++++++++++++++++++++++++++-
 hw/pci/pci.c         | 62 +++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 96 insertions(+), 4 deletions(-)
include/hw/pci/pci.h +37 -1
index fa6313aabc..8fe6f746d7 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"
@@ -384,10 +385,45 @@ 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.
+     *
+     */
+    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 *base_dev,
+                                Error **errp);
+void pci_device_unset_iommu_device(PCIDevice *dev);
 
 /**
  * pci_setup_iommu: Initialize specific IOMMU handlers for a PCIBus
hw/pci/pci.c +59 -3

83 lines not on this page · raw message

Signature · 1 line
2.34.1
#30 lore raw

Re: [PATCH v1 09/11] hw/pci: Introduce pci_device_set/unset_iommu_device()

Quoted 59 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds pci_device_set/unset_iommu_device() to set/unset HostIOMMUDevice for a given PCIe 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 | 38 ++++++++++++++++++++++++++-
 hw/pci/pci.c         | 62 +++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 96 insertions(+), 4 deletions(-)
include/hw/pci/pci.h +25 -1
index fa6313aabc..8fe6f746d7 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"
@@ -384,10 +385,45 @@ 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.
+     *
+     */
+    int (*set_iommu_device)(PCIBus *bus, void *opaque, int devfn,
+                            HostIOMMUDevice *dev, Error **errp);
+    /**
+     * @unset_iommu_device: detach a HostIOMMUDevice from a vIOMMU
+     *

include/hw/pci/pci.h typedef struct PCIIOMMUOps { · line 421

Quoted 10 lines
include/hw/pci/pci.h +9 -0
+     * 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;

So I expected someone to implement these new callbacks but I see no implementation in this patchset.

Is this just infrastructure that will be used later? A bit hard to judge without a user ...

Quoted 98 lines
include/hw/pci/pci.h +3 -0
 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
+int pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *base_dev,
+                                Error **errp);
+void pci_device_unset_iommu_device(PCIDevice *dev);
 
 /**
  * pci_setup_iommu: Initialize specific IOMMU handlers for a PCIBus
hw/pci/pci.c +59 -3
index 76080af580..8078307963 100644
@@ -2672,11 +2672,14 @@ static void pci_device_class_base_init(ObjectClass *klass, void *data)
     }
 }
 
-AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+static void pci_device_get_iommu_bus_devfn(PCIDevice *dev,
+                                           PCIBus **aliased_bus,
+                                           PCIBus **piommu_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);
@@ -2717,13 +2720,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, &bus, &iommu_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 *base_dev,
+                                Error **errp)
+{
+    PCIBus *iommu_bus;
+
+    pci_device_get_iommu_bus_devfn(dev, NULL, &iommu_bus, 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, base_dev,
+                                                      errp);
+    }
+    return 0;
+}
+
+void pci_device_unset_iommu_device(PCIDevice *dev)
+{
+    PCIBus *iommu_bus;
+
+    pci_device_get_iommu_bus_devfn(dev, NULL, &iommu_bus, 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)
 {
     /*
Signature · 1 line
2.34.1
#31 lore raw

Re: [PATCH v1 09/11] hw/pci: Introduce pci_device_set/unset_iommu_device()

Quoted 75 lines · Michael S. Tsirkin
Quoted 59 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds pci_device_set/unset_iommu_device() to set/unset HostIOMMUDevice for a given PCIe 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 | 38 ++++++++++++++++++++++++++-
 hw/pci/pci.c         | 62 +++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 96 insertions(+), 4 deletions(-)
include/hw/pci/pci.h +25 -1
index fa6313aabc..8fe6f746d7 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"
@@ -384,10 +385,45 @@ 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.
+     *
+     */
+    int (*set_iommu_device)(PCIBus *bus, void *opaque, int devfn,
+                            HostIOMMUDevice *dev, Error **errp);
+    /**
+     * @unset_iommu_device: detach a HostIOMMUDevice from a vIOMMU
+     *

include/hw/pci/pci.h typedef struct PCIIOMMUOps { · line 421

Quoted 10 lines
include/hw/pci/pci.h +9 -0
+     * 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;

So I expected someone to implement these new callbacks but I see no implementation in this patchset.

Quoted 3 lines

Is this just infrastructure that will be used later? A bit hard to judge without a user ...

Looked at the second part of the patchset now (dealing with VTD). Let's continue the discussion there.

Quoted 99 lines
Quoted 99 lines
include/hw/pci/pci.h +3 -0
 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
+int pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *base_dev,
+                                Error **errp);
+void pci_device_unset_iommu_device(PCIDevice *dev);
 
 /**
  * pci_setup_iommu: Initialize specific IOMMU handlers for a PCIBus
hw/pci/pci.c +59 -3
index 76080af580..8078307963 100644
@@ -2672,11 +2672,14 @@ static void pci_device_class_base_init(ObjectClass *klass, void *data)
     }
 }
 
-AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+static void pci_device_get_iommu_bus_devfn(PCIDevice *dev,
+                                           PCIBus **aliased_bus,
+                                           PCIBus **piommu_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);
@@ -2717,13 +2720,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, &bus, &iommu_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 *base_dev,
+                                Error **errp)
+{
+    PCIBus *iommu_bus;
+
+    pci_device_get_iommu_bus_devfn(dev, NULL, &iommu_bus, 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, base_dev,
+                                                      errp);
+    }
+    return 0;
+}
+
+void pci_device_unset_iommu_device(PCIDevice *dev)
+{
+    PCIBus *iommu_bus;
+
+    pci_device_get_iommu_bus_devfn(dev, NULL, &iommu_bus, 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)
 {
     /*
Signature · 1 line
2.34.1
#32 lore raw

Re: [PATCH v1 09/11] hw/pci: Introduce pci_device_set/unset_iommu_device()

In reply to Zhenzhong Duan

Hi Zhenzhong,

Quoted 42 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds pci_device_set/unset_iommu_device() to set/unset HostIOMMUDevice for a given PCIe 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 | 38 ++++++++++++++++++++++++++-
 hw/pci/pci.c         | 62 +++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 96 insertions(+), 4 deletions(-)
include/hw/pci/pci.h +8 -1
index fa6313aabc..8fe6f746d7 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"
@@ -384,10 +385,45 @@ 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.
+     *

include/hw/pci/pci.h typedef struct PCIIOMMUOps { · line 404

Quoted 10 lines
include/hw/pci/pci.h +10 -0
+     * 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 is missing

Quoted 30 lines
include/hw/pci/pci.h +19 -0
+     *
+     */
+    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 *base_dev,
+                                Error **errp);
+void pci_device_unset_iommu_device(PCIDevice *dev);
 
 /**
  * pci_setup_iommu: Initialize specific IOMMU handlers for a PCIBus
hw/pci/pci.c +0 -0
index 76080af580..8078307963 100644

hw/pci/pci.c static void pci_device_class_base_init(ObjectClass *klass, void *data) · line 2674

Quoted 3 lines
hw/pci/pci.c +0 -0
@@ -2672,11 +2672,14 @@ static void pci_device_class_base_init(ObjectClass *klass, void *data)
     }
 }
 

I would write some comments describing the output params and also explicitly saying some are optional

hw/pci/pci.c static void pci_device_class_base_init(ObjectClass *klass, void *data) · line 2677

Quoted 4 lines
hw/pci/pci.c +3 -1
-AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+static void pci_device_get_iommu_bus_devfn(PCIDevice *dev,
+                                           PCIBus **aliased_bus,
+                                           PCIBus **piommu_bus,

piommu_bus is not an optional parameter. I would put it before aliased_bus.

Quoted 43 lines
hw/pci/pci.c +29 -2
+                                           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);
@@ -2717,13 +2720,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, &bus, &iommu_bus, &devfn);
+    if (iommu_bus) {
         return iommu_bus->iommu_ops->get_address_space(bus,
                                  iommu_bus->iommu_opaque, devfn);

hw/pci/pci.c AddressSpace *pci_device_iommu_address_space(PCIDevice *dev) · line 2761

Quoted 10 lines
hw/pci/pci.c +6 -0
     }
     return &address_space_memory;
 }
 
+int pci_device_set_iommu_device(PCIDevice *dev, HostIOMMUDevice *base_dev,
+                                Error **errp)
+{
+    PCIBus *iommu_bus;
+
+    pci_device_get_iommu_bus_devfn(dev, NULL, &iommu_bus, NULL);

I would add a comment explaining why you don't care about aliased bus and devfn

Quoted 14 lines
hw/pci/pci.c +14 -0
+    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, base_dev,
+                                                      errp);
+    }
+    return 0;
+}
+
+void pci_device_unset_iommu_device(PCIDevice *dev)
+{
+    PCIBus *iommu_bus;
+
+    pci_device_get_iommu_bus_devfn(dev, NULL, &iommu_bus, NULL);

hw/pci/pci.c AddressSpace *pci_device_iommu_address_space(PCIDevice *dev) · line 2785

Quoted 10 lines
hw/pci/pci.c +7 -0
+    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)
 {
     /*

Thanks

Eric

#33 lore raw

RE: [PATCH v1 09/11] hw/pci: Introduce pci_device_set/unset_iommu_device()

Quoted 66 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v1 09/11] hw/pci: Introduce
pci_device_set/unset_iommu_device()

Hi Zhenzhong,

Quoted 9 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds pci_device_set/unset_iommu_device() to set/unset HostIOMMUDevice for a given PCIe 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().

Quoted 7 lines
---
 include/hw/pci/pci.h | 38 ++++++++++++++++++++++++++-
 hw/pci/pci.c         | 62

+++++++++++++++++++++++++++++++++++++++++---

Quoted 15 lines
 2 files changed, 96 insertions(+), 4 deletions(-)
include/hw/pci/pci.h +1 -0
index fa6313aabc..8fe6f746d7 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"

include/hw/pci/pci.h typedef struct PCIIOMMUOps { · line 387

Quoted 4 lines
include/hw/pci/pci.h +0 -1
@@ -384,10 +385,45 @@ typedef struct PCIIOMMUOps {
      *
      * @devfn: device and function number
      */
-   AddressSpace * (*get_address_space)(PCIBus *bus, void *opaque, int

devfn);

include/hw/pci/pci.h typedef struct PCIIOMMUOps { · line 388

Quoted 1 line
include/hw/pci/pci.h +1 -0
+    AddressSpace * (*get_address_space)(PCIBus *bus, void *opaque, int

devfn);

include/hw/pci/pci.h typedef struct PCIIOMMUOps {

Quoted 4 lines
include/hw/pci/pci.h +4 -0
+    /**
+     * @set_iommu_device: attach a HostIOMMUDevice to a vIOMMU
+     *
+     * Optional callback, if not implemented in vIOMMU, then vIOMMU

can't

include/hw/pci/pci.h typedef struct PCIIOMMUOps { · line 404

Quoted 12 lines
include/hw/pci/pci.h +12 -0
+     * 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.
Quoted 1 line

@errp is missing

Will add.

Quoted 33 lines

include/hw/pci/pci.h typedef struct PCIIOMMUOps {

Quoted 6 lines
include/hw/pci/pci.h +6 -0
+     *
+     */
+    int (*set_iommu_device)(PCIBus *bus, void *opaque, int devfn,
+                            HostIOMMUDevice *dev, Error **errp);
+    /**
+     * @unset_iommu_device: detach a HostIOMMUDevice from a

vIOMMU

Quoted 4 lines
include/hw/pci/pci.h +4 -0
+     *
+     * Optional callback.
+     *
+     * @bus: the #PCIBus of the PCI device.

include/hw/pci/pci.h typedef struct PCIIOMMUOps {

Quoted 10 lines
include/hw/pci/pci.h +7 -0
+     *
+     * @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

*base_dev,

hw/pci/pci.c static void

Quoted 10 lines
include/hw/pci/pci.h +2 -0
+                                Error **errp);
+void pci_device_unset_iommu_device(PCIDevice *dev);
 
 /**
  * pci_setup_iommu: Initialize specific IOMMU handlers for a PCIBus
hw/pci/pci.c +0 -0
index 76080af580..8078307963 100644
@@ -2672,11 +2672,14 @@ static void

pci_device_class_base_init(ObjectClass *klass, void *data)

Quoted 5 lines

hw/pci/pci.c static void · line 2674

Quoted 3 lines
hw/pci/pci.c +0 -0
     }
 }
 

I would write some comments describing the output params and also explicitly saying some are optional

Sure.

Quoted 6 lines

hw/pci/pci.c static void pci_device_class_base_init(ObjectClass *klass, void *data) · line 2677

Quoted 4 lines
hw/pci/pci.c +3 -1
-AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+static void pci_device_get_iommu_bus_devfn(PCIDevice *dev,
+                                           PCIBus **aliased_bus,
+                                           PCIBus **piommu_bus,

piommu_bus is not an optional parameter. I would put it before aliased_bus.

Good suggestion, will do.

Quoted 52 lines

hw/pci/pci.c AddressSpace

Quoted 12 lines
hw/pci/pci.c +2 -1
+                                           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);
hw/pci/pci.c +0 -0
@@ -2717,13 +2720,66 @@ AddressSpace

*pci_device_iommu_address_space(PCIDevice *dev)

Quoted 28 lines
hw/pci/pci.c +24 -1
         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;

hw/pci/pci.c AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)

Quoted 10 lines
hw/pci/pci.c +4 -0
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_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

*base_dev,

Quoted 7 lines

hw/pci/pci.c AddressSpace *pci_device_iommu_address_space(PCIDevice *dev) · line 2761

Quoted 5 lines
hw/pci/pci.c +5 -0
+                                Error **errp)
+{
+    PCIBus *iommu_bus;
+
+    pci_device_get_iommu_bus_devfn(dev, NULL, &iommu_bus, NULL);

I would add a comment explaining why you don't care about aliased bus and devfn

Sure.

Thanks
Zhenzhong

Quoted 30 lines
Quoted 14 lines
hw/pci/pci.c +13 -0
+    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, base_dev,
+                                                      errp);
+    }
+    return 0;
+}
+
+void pci_device_unset_iommu_device(PCIDevice *dev)
+{
+    PCIBus *iommu_bus;
+

hw/pci/pci.c AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)

Quoted 10 lines
hw/pci/pci.c +8 -0
+    pci_device_get_iommu_bus_devfn(dev, NULL, &iommu_bus, 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)

Quoted 2 lines
hw/pci/pci.c +0 -0
 {
     /*

Thanks

Eric

#34 lore raw

PATCH 10/11 vfio: Pass HostIOMMUDevice to vIOMMU

In reply to Zhenzhong Duan

Support both iommufd and legacy backend.

---
 hw/vfio/pci.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
hw/vfio/pci.c +15 -5
index 6cc7de5d10..ed9f386fde 100644
@@ -3112,11 +3112,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->base_hdev, 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);
     }
@@ -3133,7 +3139,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,
@@ -3142,13 +3148,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;
         }
     }
 
@@ -3234,6 +3240,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);
@@ -3263,6 +3271,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);
@@ -3277,7 +3286,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
#35 lore raw

PATCH 11/11 backends/iommufd: Introduce helper function iommufd_device_get_info()

In reply to Zhenzhong Duan

Introduce a helper function iommufd_device_get_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 d509ff88ef..518b97bfed 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"
@@ -48,4 +49,7 @@ typedef struct IOMMUFDDevice {
 
 void iommufd_device_init(IOMMUFDDevice *idev,
                          IOMMUFDBackend *iommufd, int devid);
+int iommufd_device_get_info(IOMMUFDDevice *idev,
+                            enum iommu_hw_info_type *type,
+                            uint32_t len, void *data, Error **errp);
 #endif
backends/iommufd.c +22 -1
index 6d280e4aea..69f3f75ea5 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)
 {
@@ -240,3 +239,25 @@ void iommufd_device_init(IOMMUFDDevice *idev,
     idev->iommufd = iommufd;
     idev->devid = devid;
 }
+
+int iommufd_device_get_info(IOMMUFDDevice *idev,
+                            enum iommu_hw_info_type *type,
+                            uint32_t len, void *data, Error **errp)
+{
+    struct iommu_hw_info info = {
+        .size = sizeof(info),
+        .dev_id = idev->devid,
+        .data_len = len,
+        .data_uptr = (uintptr_t)data,
+    };
+    int ret;
+
+    ret = ioctl(idev->iommufd->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;
+}
Signature · 1 line
2.34.1
#36 lore raw

Re: [PATCH v1 11/11] backends/iommufd: Introduce helper function iommufd_device_get_info()

Hi Zhenzhong,

Quoted 2 lines · Zhenzhong Duan

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

Looks strange to have this patch in this series. I Would rather put it in your second series alongs with its user.

Eric

Quoted 67 lines
---
 include/sysemu/iommufd.h |  4 ++++
 backends/iommufd.c       | 23 ++++++++++++++++++++++-
 2 files changed, 26 insertions(+), 1 deletion(-)
include/sysemu/iommufd.h +4 -0
index d509ff88ef..518b97bfed 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"
@@ -48,4 +49,7 @@ typedef struct IOMMUFDDevice {
 
 void iommufd_device_init(IOMMUFDDevice *idev,
                          IOMMUFDBackend *iommufd, int devid);
+int iommufd_device_get_info(IOMMUFDDevice *idev,
+                            enum iommu_hw_info_type *type,
+                            uint32_t len, void *data, Error **errp);
 #endif
backends/iommufd.c +22 -1
index 6d280e4aea..69f3f75ea5 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)
 {
@@ -240,3 +239,25 @@ void iommufd_device_init(IOMMUFDDevice *idev,
     idev->iommufd = iommufd;
     idev->devid = devid;
 }
+
+int iommufd_device_get_info(IOMMUFDDevice *idev,
+                            enum iommu_hw_info_type *type,
+                            uint32_t len, void *data, Error **errp)
+{
+    struct iommu_hw_info info = {
+        .size = sizeof(info),
+        .dev_id = idev->devid,
+        .data_len = len,
+        .data_uptr = (uintptr_t)data,
+    };
+    int ret;
+
+    ret = ioctl(idev->iommufd->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;
+}
#37 lore raw

Re: [PATCH v1 11/11] backends/iommufd: Introduce helper function iommufd_device_get_info()

Quoted 8 lines · Eric Auger

Hi Zhenzhong,

Quoted 2 lines · Zhenzhong Duan

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

Looks strange to have this patch in this series. I Would rather put it in your second series alongs with its user.

The reason it was here was to use this helper for this patch:

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

Instead of me having my own alternate helper.

Though at the same time, Zhenzhong will also make use of it in his second series.

Quoted 69 lines

Eric

Quoted 67 lines
---
 include/sysemu/iommufd.h |  4 ++++
 backends/iommufd.c       | 23 ++++++++++++++++++++++-
 2 files changed, 26 insertions(+), 1 deletion(-)
include/sysemu/iommufd.h +4 -0
index d509ff88ef..518b97bfed 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"
@@ -48,4 +49,7 @@ typedef struct IOMMUFDDevice {
 
 void iommufd_device_init(IOMMUFDDevice *idev,
                          IOMMUFDBackend *iommufd, int devid);
+int iommufd_device_get_info(IOMMUFDDevice *idev,
+                            enum iommu_hw_info_type *type,
+                            uint32_t len, void *data, Error **errp);
 #endif
backends/iommufd.c +22 -1
index 6d280e4aea..69f3f75ea5 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)
 {
@@ -240,3 +239,25 @@ void iommufd_device_init(IOMMUFDDevice *idev,
     idev->iommufd = iommufd;
     idev->devid = devid;
 }
+
+int iommufd_device_get_info(IOMMUFDDevice *idev,
+                            enum iommu_hw_info_type *type,
+                            uint32_t len, void *data, Error **errp)
+{
+    struct iommu_hw_info info = {
+        .size = sizeof(info),
+        .dev_id = idev->devid,
+        .data_len = len,
+        .data_uptr = (uintptr_t)data,
+    };
+    int ret;
+
+    ret = ioctl(idev->iommufd->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;
+}
#38 lore raw

Re: [PATCH v1 11/11] backends/iommufd: Introduce helper function iommufd_device_get_info()

Hi Joao,

Quoted 9 lines · Joao Martins
Quoted 8 lines · Eric Auger

Hi Zhenzhong,

Quoted 2 lines · Zhenzhong Duan

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

Looks strange to have this patch in this series. I Would rather put it in your second series alongs with its user.

Quoted 7 lines

The reason it was here was to use this helper for this patch:

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

Instead of me having my own alternate helper.

Though at the same time, Zhenzhong will also make use of it in his second series.

OK I understand now. Maybe with extra comment in the coverletter then

Thanks

Eric

Quoted 68 lines
Quoted 68 lines

Eric

Quoted 66 lines
---
 include/sysemu/iommufd.h |  4 ++++
 backends/iommufd.c       | 23 ++++++++++++++++++++++-
 2 files changed, 26 insertions(+), 1 deletion(-)
include/sysemu/iommufd.h +4 -0
index d509ff88ef..518b97bfed 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"
@@ -48,4 +49,7 @@ typedef struct IOMMUFDDevice {
 
 void iommufd_device_init(IOMMUFDDevice *idev,
                          IOMMUFDBackend *iommufd, int devid);
+int iommufd_device_get_info(IOMMUFDDevice *idev,
+                            enum iommu_hw_info_type *type,
+                            uint32_t len, void *data, Error **errp);
 #endif
backends/iommufd.c +22 -1
index 6d280e4aea..69f3f75ea5 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)
 {
@@ -240,3 +239,25 @@ void iommufd_device_init(IOMMUFDDevice *idev,
     idev->iommufd = iommufd;
     idev->devid = devid;
 }
+
+int iommufd_device_get_info(IOMMUFDDevice *idev,
+                            enum iommu_hw_info_type *type,
+                            uint32_t len, void *data, Error **errp)
+{
+    struct iommu_hw_info info = {
+        .size = sizeof(info),
+        .dev_id = idev->devid,
+        .data_len = len,
+        .data_uptr = (uintptr_t)data,
+    };
+    int ret;
+
+    ret = ioctl(idev->iommufd->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;
+}
#39 lore raw

RE: [PATCH v1 11/11] backends/iommufd: Introduce helper function iommufd_device_get_info()

Quoted 21 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v1 11/11] backends/iommufd: Introduce helper function iommufd_device_get_info()

Hi Joao,

Quoted 11 lines · Joao Martins
Quoted 8 lines · Eric Auger

Hi Zhenzhong,

Quoted 2 lines · Zhenzhong Duan

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

Looks strange to have this patch in this series. I Would rather put it in your second series alongs with its user.

The reason it was here was to use this helper for this patch:

Quoted 8 lines
Quoted 1 line

https://lore.kernel.org/qemu-devel/20240212135643.5858-2-

[email protected]/

Quoted 4 lines

Instead of me having my own alternate helper.

Though at the same time, Zhenzhong will also make use of it in his second

series.
OK I understand now. Maybe with extra comment in the coverletter then

Will add.

Thanks
Zhenzhong

#40 lore raw
In reply to Zhenzhong Duan
Quoted 6 lines · Zhenzhong Duan

Hi,

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

Quoted 6 lines

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

PATCH1-3: Introduce HostIOMMUDevice and two sub class
PATCH4: Define HostIOMMUDevice handle in VFIODevice
PATCH5-8: Introdcue host_iommu_device_create callback to allocate and intialize HostIOMMUDevice

Introduce, here and below

Eric

Quoted 70 lines

PATCH9-10: Introdcue set/unset_iommu_device to pass HostIOMMUDevice to vIOMMU
PATCH11: a helper to get host IOMMU info

Because it's becoming clear on community's suggestion, I'd like to remove rfc tag from this version.

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

[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:
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 (10):
Introduce a common abstract struct HostIOMMUDevice backends/iommufd: Introduce IOMMUFDDevice
vfio: Introduce IOMMULegacyDevice
vfio: Add HostIOMMUDevice handle into VFIODevice
vfio: Introduce host_iommu_device_create callback vfio/container: Implement host_iommu_device_create callback in legacy
mode
vfio/iommufd: Implement host_iommu_device_create callback in iommufd
mode
vfio/pci: Allocate and initialize HostIOMMUDevice after attachment
vfio: Pass HostIOMMUDevice to vIOMMU
backends/iommufd: Introduce helper function iommufd_device_get_info()

 include/hw/pci/pci.h                  | 38 +++++++++++++++-
 include/hw/vfio/vfio-common.h         |  8 ++++
 include/hw/vfio/vfio-container-base.h |  1 +
 include/sysemu/host_iommu_device.h    | 22 ++++++++++
 include/sysemu/iommufd.h              | 19 ++++++++
 backends/iommufd.c                    | 32 +++++++++++++-
 hw/pci/pci.c                          | 62 +++++++++++++++++++++++++--
 hw/vfio/common.c                      |  8 ++++
 hw/vfio/container.c                   |  9 ++++
 hw/vfio/iommufd.c                     | 10 +++++
 hw/vfio/pci.c                         | 24 ++++++++---
 11 files changed, 223 insertions(+), 10 deletions(-)
 create mode 100644 include/sysemu/host_iommu_device.h
#41 lore raw
Quoted 15 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH v1 00/11] Add a host IOMMU device abstraction

Quoted 6 lines · Zhenzhong Duan

Hi,

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

Quoted 8 lines
Quoted 6 lines

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

PATCH1-3: Introduce HostIOMMUDevice and two sub class
PATCH4: Define HostIOMMUDevice handle in VFIODevice
PATCH5-8: Introdcue host_iommu_device_create callback to allocate and

intialize HostIOMMUDevice
Introduce, here and below

Good catch, will fix.

Thanks
Zhenzhong

Quoted 85 lines

Eric

Quoted 1 line

PATCH9-10: Introdcue set/unset_iommu_device to pass

HostIOMMUDevice to vIOMMU

Quoted 7 lines

PATCH11: a helper to get host IOMMU info

Because it's becoming clear on community's suggestion, I'd like to remove rfc tag from this version.

Qemu code can be found at:

https://github.com/yiliu1765/qemu/tree/zhenzhong/iommufd_nesting_pre
q_part1_v1

Quoted 2 lines

[1] https://lore.kernel.org/qemu-devel/20240201072818.327930-1-

[email protected]/

Quoted 1 line

[2] https://lore.kernel.org/qemu-devel/20240212135643.5858-1-

[email protected]/

Quoted 1 line

[3] https://lore.kernel.org/qemu-devel/20240117080414.316890-1-

[email protected]/

Quoted 4 lines

Thanks
Zhenzhong

Quoted 8 lines

Changelog:
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)

Quoted 3 lines

rfcv2:
- introduce common abstract HostIOMMUDevice and sub struct for

different BEs (Eric, Cédric)

Quoted 4 lines

- 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)

Quoted 3 lines

- 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)

Quoted 1 line

- make '(vtd->cap_reg >> 16) & 0x3fULL' a MACRO and add missed '+1'

(Cédric)

Quoted 1 line

- block migration if vIOMMU cap/ecap updated based on host IOMMU

cap/ecap

Quoted 6 lines

- add R-B

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

Quoted 9 lines

Zhenzhong Duan (10):
Introduce a common abstract struct HostIOMMUDevice backends/iommufd: Introduce IOMMUFDDevice
vfio: Introduce IOMMULegacyDevice
vfio: Add HostIOMMUDevice handle into VFIODevice
vfio: Introduce host_iommu_device_create callback vfio/container: Implement host_iommu_device_create callback in legacy
mode
vfio/iommufd: Implement host_iommu_device_create callback in

iommufd

Quoted 19 lines

mode
vfio/pci: Allocate and initialize HostIOMMUDevice after attachment
vfio: Pass HostIOMMUDevice to vIOMMU
backends/iommufd: Introduce helper function iommufd_device_get_info()

 include/hw/pci/pci.h                  | 38 +++++++++++++++-
 include/hw/vfio/vfio-common.h         |  8 ++++
 include/hw/vfio/vfio-container-base.h |  1 +
 include/sysemu/host_iommu_device.h    | 22 ++++++++++
 include/sysemu/iommufd.h              | 19 ++++++++
 backends/iommufd.c                    | 32 +++++++++++++-
 hw/pci/pci.c                          | 62 +++++++++++++++++++++++++--
 hw/vfio/common.c                      |  8 ++++
 hw/vfio/container.c                   |  9 ++++
 hw/vfio/iommufd.c                     | 10 +++++
 hw/vfio/pci.c                         | 24 ++++++++---
 11 files changed, 223 insertions(+), 10 deletions(-)
 create mode 100644 include/sysemu/host_iommu_device.h