Skip to contentSkip to conversation

Check and sync host IOMMU cap/ecap with vIOMMU

RFC PATCH Superseded Zhenzhong Duan 5 participants 46 messages Jan 15, 2024 – Jan 24, 2024 Arm/Amlogic Meson SoC support

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Check and sync host IOMMU cap/ecap with vIOMMU

Hi,

This introduces a framework for vIOMMU to get hw IOMMU cap/ecap information through IOMMUFD interface and check or sync with vIOMMU's own cap/ecap config.

This framework works by having device side, i.e. VFIO, register a IOMMUFDDevice to vIOMMU, IOMMUFDDevice includes necessary data to archive that. Currently only VFIO device is supported, but it could also be used for other devices, i.e., VDPA.

This is also a prerequisite for incoming iommufd nesting series:
'intel_iommu: Enable stage-1 translation'.

PATCH1-4: initialize IOMMUFDDevice and pass to vIOMMU
PATCH5-6: cap/ecap sync mechanism between host IOMMU and vIOMMU

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

Thanks
Zhenzhong

Yi Liu (3):
hw/pci: introduce pci_device_set/unset_iommu_device()
intel_iommu: add set/unset_iommu_device callback
intel_iommu: add a framework to check and sync host IOMMU cap/ecap

Zhenzhong Duan (3):
backends/iommufd_device: introduce IOMMUFDDevice
vfio: initialize IOMMUFDDevice and pass to vIOMMU
intel_iommu: extract out vtd_cap_init to initialize cap/ecap

 MAINTAINERS                     |   4 +-
 include/hw/i386/intel_iommu.h   |  14 ++
 include/hw/pci/pci.h            |  39 +++++-
 include/hw/vfio/vfio-common.h   |   2 +
 include/sysemu/iommufd_device.h |  31 +++++
 backends/iommufd_device.c       |  50 +++++++
 hw/i386/intel_iommu.c           | 239 ++++++++++++++++++++++++++------
 hw/pci/pci.c                    |  49 ++++++-
 hw/vfio/iommufd.c               |   2 +
 hw/vfio/pci.c                   |  24 +++-
 backends/meson.build            |   2 +-
 11 files changed, 402 insertions(+), 54 deletions(-)
 create mode 100644 include/sysemu/iommufd_device.h
 create mode 100644 backends/iommufd_device.c
Signature · 1 line
2.34.1
#1 lore raw

RFC PATCH 1/6 backends/iommufd_device: introduce IOMMUFDDevice

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 iommufd handler and device id information which could be used by vIOMMU to get hw IOMMU information.

In future nested translation support, vIOMMU is going to have more iommufd related operations like allocate hwpt for a device, attach/detach hwpt, etc. So IOMMUFDDevice will be further expanded.

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.

---
 MAINTAINERS                     |  4 +--
 include/sysemu/iommufd_device.h | 31 ++++++++++++++++++++
 backends/iommufd_device.c       | 50 +++++++++++++++++++++++++++++++++
 backends/meson.build            |  2 +-
 4 files changed, 84 insertions(+), 3 deletions(-)
 create mode 100644 include/sysemu/iommufd_device.h
 create mode 100644 backends/iommufd_device.c
MAINTAINERS +2 -2
index 00ec1f7eca..606dfeb2b1 100644
@@ -2171,8 +2171,8 @@ M: Yi Liu <[email protected]>
 M: Eric Auger <[email protected]>
 M: Zhenzhong Duan <[email protected]>
 S: Supported
-F: backends/iommufd.c
-F: include/sysemu/iommufd.h
+F: backends/iommufd*.c
+F: include/sysemu/iommufd*.h
 F: include/qemu/chardev_open.h
 F: util/chardev_open.c
 F: docs/devel/vfio-iommufd.rst
include/sysemu/iommufd_device.h new file +31 -0
new file mode 100644
index 0000000000..795630324b
@@ -0,0 +1,31 @@
+/*
+ * IOMMUFD Device
+ *
+ * Copyright (C) 2024 Intel Corporation.
+ *
+ * Authors: Yi Liu <[email protected]>
+ *          Zhenzhong Duan <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef SYSEMU_IOMMUFD_DEVICE_H
+#define SYSEMU_IOMMUFD_DEVICE_H
+
+#include <linux/iommufd.h>
+#include "sysemu/iommufd.h"
+
+typedef struct IOMMUFDDevice IOMMUFDDevice;
+
+/* This is an abstraction of host IOMMUFD device */
+struct IOMMUFDDevice {
+    IOMMUFDBackend *iommufd;
+    uint32_t dev_id;
+};
+
+int iommufd_device_get_info(IOMMUFDDevice *idev,
+                            enum iommu_hw_info_type *type,
+                            uint32_t len, void *data);
+void iommufd_device_init(void *_idev, size_t instance_size,
+                         IOMMUFDBackend *iommufd, uint32_t dev_id);
+#endif
backends/iommufd_device.c new file +50 -0

50 lines not on this page · raw message

backends/meson.build +1 -1
index 8b2b111497..c437cdb363 100644
@@ -24,7 +24,7 @@ if have_vhost_user
   system_ss.add(when: 'CONFIG_VIRTIO', if_true: files('vhost-user.c'))
 endif
 system_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('cryptodev-vhost.c'))
-system_ss.add(when: 'CONFIG_IOMMUFD', if_true: files('iommufd.c'))
+system_ss.add(when: 'CONFIG_IOMMUFD', if_true: files('iommufd.c', 'iommufd_device.c'))
 if have_vhost_user_crypto
   system_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('cryptodev-vhost-user.c'))
 endif
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH rfcv1 1/6] backends/iommufd_device: introduce IOMMUFDDevice

Hi Zhenzhong,

Quoted 5 lines · 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 iommufd handler and device id information

iommufd handle

Quoted 10 lines

which could be used by vIOMMU to get hw IOMMU information.

In future nested translation support, vIOMMU is going to have more iommufd related operations like allocate hwpt for a device, attach/detach hwpt, etc. So IOMMUFDDevice will be further expanded.

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.

+  iommufd_device_get_info helper

Quoted 71 lines
---
 MAINTAINERS                     |  4 +--
 include/sysemu/iommufd_device.h | 31 ++++++++++++++++++++
 backends/iommufd_device.c       | 50 +++++++++++++++++++++++++++++++++
 backends/meson.build            |  2 +-
 4 files changed, 84 insertions(+), 3 deletions(-)
 create mode 100644 include/sysemu/iommufd_device.h
 create mode 100644 backends/iommufd_device.c
MAINTAINERS +2 -2
index 00ec1f7eca..606dfeb2b1 100644
@@ -2171,8 +2171,8 @@ M: Yi Liu <[email protected]>
 M: Eric Auger <[email protected]>
 M: Zhenzhong Duan <[email protected]>
 S: Supported
-F: backends/iommufd.c
-F: include/sysemu/iommufd.h
+F: backends/iommufd*.c
+F: include/sysemu/iommufd*.h
 F: include/qemu/chardev_open.h
 F: util/chardev_open.c
 F: docs/devel/vfio-iommufd.rst
include/sysemu/iommufd_device.h new file +31 -0
new file mode 100644
index 0000000000..795630324b
@@ -0,0 +1,31 @@
+/*
+ * IOMMUFD Device
+ *
+ * Copyright (C) 2024 Intel Corporation.
+ *
+ * Authors: Yi Liu <[email protected]>
+ *          Zhenzhong Duan <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef SYSEMU_IOMMUFD_DEVICE_H
+#define SYSEMU_IOMMUFD_DEVICE_H
+
+#include <linux/iommufd.h>
+#include "sysemu/iommufd.h"
+
+typedef struct IOMMUFDDevice IOMMUFDDevice;
+
+/* This is an abstraction of host IOMMUFD device */
+struct IOMMUFDDevice {
+    IOMMUFDBackend *iommufd;
+    uint32_t dev_id;
+};
+
+int iommufd_device_get_info(IOMMUFDDevice *idev,
+                            enum iommu_hw_info_type *type,
+                            uint32_t len, void *data);
+void iommufd_device_init(void *_idev, size_t instance_size,
+                         IOMMUFDBackend *iommufd, uint32_t dev_id);
+#endif
backends/iommufd_device.c new file +0 -0
new file mode 100644
index 0000000000..f6e7ca1dbf

backends/iommufd_device.c line 2

Quoted 2 lines
backends/iommufd_device.c new file +2 -0
@@ -0,0 +1,50 @@
+/*
+ * QEMU abstract of Host IOMMU

it is the abstraction of the IOMMU or of any assigned device?

Quoted 21 lines
backends/iommufd_device.c new file +21 -0
+ *
+ * Copyright (C) 2024 Intel Corporation.
+ *
+ * Authors: Yi Liu <[email protected]>
+ *          Zhenzhong Duan <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <sys/ioctl.h>
+#include "qemu/osdep.h"
+#include "qemu/error-report.h"
+#include "sysemu/iommufd_device.h"
+
+int (IOMMUFDDevice *idev,
+                            enum iommu_hw_info_type *type,
+                            uint32_t len, void *data)
+{
+    struct iommu_hw_info info = {
+        .size = sizeof(info),
+        .flags = 0,

backends/iommufd_device.c line 33

Quoted 10 lines
backends/iommufd_device.c new file +10 -0
+        .dev_id = idev->dev_id,
+        .data_len = len,
+        .__reserved = 0,
+        .data_uptr = (uintptr_t)data,
+    };
+    int ret;
+
+    ret = ioctl(idev->iommufd->fd, IOMMU_GET_HW_INFO, &info);
+    if (ret) {
+        error_report("Failed to get info %m");

you may prefer using errp instead of hard traces.

backends/iommufd_device.c line 41

Quoted 8 lines
backends/iommufd_device.c new file +8 -0
+    } else {
+        *type = info.out_data_type;
+    }
+
+    return ret;
+}
+
+void iommufd_device_init(void *_idev, size_t instance_size,

nit: why the "_"

backends/iommufd_device.c line 46

Quoted 5 lines
backends/iommufd_device.c new file +5 -0
+                         IOMMUFDBackend *iommufd, uint32_t dev_id)
+{
+    IOMMUFDDevice *idev = (IOMMUFDDevice *)_idev;
+
+    g_assert(sizeof(IOMMUFDDevice) <= instance_size);

at this stage of the reading it is not clear why you input the instance_size. worth to be clarified/documented.

Quoted 9 lines
backends/iommufd_device.c new file +4 -0
+
+    idev->iommufd = iommufd;
+    idev->dev_id = dev_id;
+}
backends/meson.build +0 -0
index 8b2b111497..c437cdb363 100644

backends/meson.build if have_vhost_user · line 30

Quoted 8 lines
backends/meson.build +1 -1
@@ -24,7 +24,7 @@ if have_vhost_user
   system_ss.add(when: 'CONFIG_VIRTIO', if_true: files('vhost-user.c'))
 endif
 system_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('cryptodev-vhost.c'))
-system_ss.add(when: 'CONFIG_IOMMUFD', if_true: files('iommufd.c'))
+system_ss.add(when: 'CONFIG_IOMMUFD', if_true: files('iommufd.c', 'iommufd_device.c'))
 if have_vhost_user_crypto
   system_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('cryptodev-vhost-user.c'))
 endif

Thanks

Eric

#3 lore raw

RE: [PATCH rfcv1 1/6] backends/iommufd_device: introduce IOMMUFDDevice

Quoted 22 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 1/6] backends/iommufd_device: introduce IOMMUFDDevice

Hi Zhenzhong,

Quoted 5 lines · 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 iommufd handler and device id information

iommufd handle

Quoted 6 lines

which could be used by vIOMMU to get hw IOMMU information.

In future nested translation support, vIOMMU is going to have more iommufd related operations like allocate hwpt for a device, attach/detach hwpt, etc. So IOMMUFDDevice will be further expanded.

Quoted 6 lines
Quoted 4 lines

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.

+  iommufd_device_get_info helper

Will do.

Quoted 68 lines
Quoted 8 lines
---
 MAINTAINERS                     |  4 +--
 include/sysemu/iommufd_device.h | 31 ++++++++++++++++++++
 backends/iommufd_device.c       | 50

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

Review

Quoted 21 lines
 backends/meson.build            |  2 +-
 4 files changed, 84 insertions(+), 3 deletions(-)
 create mode 100644 include/sysemu/iommufd_device.h
 create mode 100644 backends/iommufd_device.c
MAINTAINERS +2 -2
index 00ec1f7eca..606dfeb2b1 100644
@@ -2171,8 +2171,8 @@ M: Yi Liu <[email protected]>
 M: Eric Auger <[email protected]>
 M: Zhenzhong Duan <[email protected]>
 S: Supported
-F: backends/iommufd.c
-F: include/sysemu/iommufd.h
+F: backends/iommufd*.c
+F: include/sysemu/iommufd*.h
 F: include/qemu/chardev_open.h
 F: util/chardev_open.c
 F: docs/devel/vfio-iommufd.rst
(quoted diff) +0 -0

b/include/sysemu/iommufd_device.h

Quoted 37 lines

new file mode 100644
index 0000000000..795630324b

include/sysemu/iommufd_device.h new file +31 -0
@@ -0,0 +1,31 @@
+/*
+ * IOMMUFD Device
+ *
+ * Copyright (C) 2024 Intel Corporation.
+ *
+ * Authors: Yi Liu <[email protected]>
+ *          Zhenzhong Duan <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef SYSEMU_IOMMUFD_DEVICE_H
+#define SYSEMU_IOMMUFD_DEVICE_H
+
+#include <linux/iommufd.h>
+#include "sysemu/iommufd.h"
+
+typedef struct IOMMUFDDevice IOMMUFDDevice;
+
+/* This is an abstraction of host IOMMUFD device */
+struct IOMMUFDDevice {
+    IOMMUFDBackend *iommufd;
+    uint32_t dev_id;
+};
+
+int iommufd_device_get_info(IOMMUFDDevice *idev,
+                            enum iommu_hw_info_type *type,
+                            uint32_t len, void *data);
+void iommufd_device_init(void *_idev, size_t instance_size,
+                         IOMMUFDBackend *iommufd, uint32_t dev_id);
+#endif
backends/iommufd_device.c +0 -0
Quoted 8 lines

backends/iommufd_device.c line 2

Quoted 7 lines

new file mode 100644
index 0000000000..f6e7ca1dbf

backends/iommufd_device.c new file +2 -0
@@ -0,0 +1,50 @@
+/*
+ * QEMU abstract of Host IOMMU

it is the abstraction of the IOMMU or of any assigned device?

' QEMU abstract of Host IOMMUFD device' may be better.

Quoted 24 lines
Quoted 24 lines
backends/iommufd_device.c new file +24 -0
+ *
+ * Copyright (C) 2024 Intel Corporation.
+ *
+ * Authors: Yi Liu <[email protected]>
+ *          Zhenzhong Duan <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <sys/ioctl.h>
+#include "qemu/osdep.h"
+#include "qemu/error-report.h"
+#include "sysemu/iommufd_device.h"
+
+int (IOMMUFDDevice *idev,
+                            enum iommu_hw_info_type *type,
+                            uint32_t len, void *data)
+{
+    struct iommu_hw_info info = {
+        .size = sizeof(info),
+        .flags = 0,
+        .dev_id = idev->dev_id,
+        .data_len = len,
+        .__reserved = 0,
Quoted 8 lines

backends/iommufd_device.c line 33

Quoted 7 lines
backends/iommufd_device.c new file +7 -0
+        .data_uptr = (uintptr_t)data,
+    };
+    int ret;
+
+    ret = ioctl(idev->iommufd->fd, IOMMU_GET_HW_INFO, &info);
+    if (ret) {
+        error_report("Failed to get info %m");

you may prefer using errp instead of hard traces.

Good suggestion, will do.

Quoted 9 lines

backends/iommufd_device.c line 41

Quoted 8 lines
backends/iommufd_device.c new file +8 -0
+    } else {
+        *type = info.out_data_type;
+    }
+
+    return ret;
+}
+
+void iommufd_device_init(void *_idev, size_t instance_size,

nit: why the "_"

To distinguish with local idev.

Quoted 7 lines

backends/iommufd_device.c line 46

Quoted 5 lines
backends/iommufd_device.c new file +5 -0
+                         IOMMUFDBackend *iommufd, uint32_t dev_id)
+{
+    IOMMUFDDevice *idev = (IOMMUFDDevice *)_idev;
+
+    g_assert(sizeof(IOMMUFDDevice) <= instance_size);

at this stage of the reading it is not clear why you input the instance_size. worth to be clarified/documented.

VFIO or VDPA may have IOMMUFD related attributes for its own usages. It looks VFIO doesn't need this for now. I'll remove it, then _idev can be removed too.

Thanks
Zhenzhong

#4 lore raw

Re: [PATCH rfcv1 1/6] backends/iommufd_device: introduce IOMMUFDDevice

In reply to Zhenzhong Duan
Quoted 16 lines · 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 iommufd handler and device id information which could be used by vIOMMU to get hw IOMMU information.

In future nested translation support, vIOMMU is going to have more iommufd related operations like allocate hwpt for a device, attach/detach hwpt, etc. So IOMMUFDDevice will be further expanded.

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.

Quoted 7 lines
---
 MAINTAINERS                     |  4 +--
 include/sysemu/iommufd_device.h | 31 ++++++++++++++++++++
 backends/iommufd_device.c       | 50 +++++++++++++++++++++++++++++++++

Maybe it is still time to move the iommufd files in a sepate dir, under hw at the same level as vfio.

Thoughts?

Eric

Quoted 126 lines
 backends/meson.build            |  2 +-
 4 files changed, 84 insertions(+), 3 deletions(-)
 create mode 100644 include/sysemu/iommufd_device.h
 create mode 100644 backends/iommufd_device.c
MAINTAINERS +2 -2
index 00ec1f7eca..606dfeb2b1 100644
@@ -2171,8 +2171,8 @@ M: Yi Liu <[email protected]>
 M: Eric Auger <[email protected]>
 M: Zhenzhong Duan <[email protected]>
 S: Supported
-F: backends/iommufd.c
-F: include/sysemu/iommufd.h
+F: backends/iommufd*.c
+F: include/sysemu/iommufd*.h
 F: include/qemu/chardev_open.h
 F: util/chardev_open.c
 F: docs/devel/vfio-iommufd.rst
include/sysemu/iommufd_device.h new file +31 -0
new file mode 100644
index 0000000000..795630324b
@@ -0,0 +1,31 @@
+/*
+ * IOMMUFD Device
+ *
+ * Copyright (C) 2024 Intel Corporation.
+ *
+ * Authors: Yi Liu <[email protected]>
+ *          Zhenzhong Duan <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef SYSEMU_IOMMUFD_DEVICE_H
+#define SYSEMU_IOMMUFD_DEVICE_H
+
+#include <linux/iommufd.h>
+#include "sysemu/iommufd.h"
+
+typedef struct IOMMUFDDevice IOMMUFDDevice;
+
+/* This is an abstraction of host IOMMUFD device */
+struct IOMMUFDDevice {
+    IOMMUFDBackend *iommufd;
+    uint32_t dev_id;
+};
+
+int iommufd_device_get_info(IOMMUFDDevice *idev,
+                            enum iommu_hw_info_type *type,
+                            uint32_t len, void *data);
+void iommufd_device_init(void *_idev, size_t instance_size,
+                         IOMMUFDBackend *iommufd, uint32_t dev_id);
+#endif
backends/iommufd_device.c new file +50 -0
new file mode 100644
index 0000000000..f6e7ca1dbf
@@ -0,0 +1,50 @@
+/*
+ * QEMU abstract of Host IOMMU
+ *
+ * Copyright (C) 2024 Intel Corporation.
+ *
+ * Authors: Yi Liu <[email protected]>
+ *          Zhenzhong Duan <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <sys/ioctl.h>
+#include "qemu/osdep.h"
+#include "qemu/error-report.h"
+#include "sysemu/iommufd_device.h"
+
+int iommufd_device_get_info(IOMMUFDDevice *idev,
+                            enum iommu_hw_info_type *type,
+                            uint32_t len, void *data)
+{
+    struct iommu_hw_info info = {
+        .size = sizeof(info),
+        .flags = 0,
+        .dev_id = idev->dev_id,
+        .data_len = len,
+        .__reserved = 0,
+        .data_uptr = (uintptr_t)data,
+    };
+    int ret;
+
+    ret = ioctl(idev->iommufd->fd, IOMMU_GET_HW_INFO, &info);
+    if (ret) {
+        error_report("Failed to get info %m");
+    } else {
+        *type = info.out_data_type;
+    }
+
+    return ret;
+}
+
+void iommufd_device_init(void *_idev, size_t instance_size,
+                         IOMMUFDBackend *iommufd, uint32_t dev_id)
+{
+    IOMMUFDDevice *idev = (IOMMUFDDevice *)_idev;
+
+    g_assert(sizeof(IOMMUFDDevice) <= instance_size);
+
+    idev->iommufd = iommufd;
+    idev->dev_id = dev_id;
+}
backends/meson.build +1 -1
index 8b2b111497..c437cdb363 100644
@@ -24,7 +24,7 @@ if have_vhost_user
   system_ss.add(when: 'CONFIG_VIRTIO', if_true: files('vhost-user.c'))
 endif
 system_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('cryptodev-vhost.c'))
-system_ss.add(when: 'CONFIG_IOMMUFD', if_true: files('iommufd.c'))
+system_ss.add(when: 'CONFIG_IOMMUFD', if_true: files('iommufd.c', 'iommufd_device.c'))
 if have_vhost_user_crypto
   system_ss.add(when: 'CONFIG_VIRTIO_CRYPTO', if_true: files('cryptodev-vhost-user.c'))
 endif
#5 lore raw

RE: [PATCH rfcv1 1/6] backends/iommufd_device: introduce IOMMUFDDevice

Quoted 33 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 1/6] backends/iommufd_device: introduce IOMMUFDDevice

Quoted 16 lines · 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 iommufd handler and device id information which could be used by vIOMMU to get hw IOMMU information.

In future nested translation support, vIOMMU is going to have more iommufd related operations like allocate hwpt for a device, attach/detach hwpt, etc. So IOMMUFDDevice will be further expanded.

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.

Quoted 7 lines
---
 MAINTAINERS                     |  4 +--
 include/sysemu/iommufd_device.h | 31 ++++++++++++++++++++
 backends/iommufd_device.c       | 50
Quoted 5 lines

+++++++++++++++++++++++++++++++++
Maybe it is still time to move the iommufd files in a sepate dir, under hw at the same level as vfio.

Thoughts?

Any reason for the movement? Hw dir contains entries to emulate different Devices. Iommufd is not a real device. It's more a backend.

Thanks
Zhenzhong

#6 lore raw

Re: [PATCH rfcv1 1/6] backends/iommufd_device: introduce IOMMUFDDevice

Quoted 33 lines · Zhenzhong
Quoted 33 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 1/6] backends/iommufd_device: introduce IOMMUFDDevice

Quoted 23 lines · 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 iommufd handler and device id information which could be used by vIOMMU to get hw IOMMU information.

In future nested translation support, vIOMMU is going to have more iommufd related operations like allocate hwpt for a device, attach/detach hwpt, etc. So IOMMUFDDevice will be further expanded.

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.

---
  MAINTAINERS                     |  4 +--
  include/sysemu/iommufd_device.h | 31 ++++++++++++++++++++
  backends/iommufd_device.c       | 50
Quoted 8 lines
Quoted 5 lines

+++++++++++++++++++++++++++++++++
Maybe it is still time to move the iommufd files in a sepate dir, under hw at the same level as vfio.

Thoughts?

Any reason for the movement? Hw dir contains entries to emulate different Devices. Iommufd is not a real device. It's more a backend.

I would include the new services in the existing iommufd .[ch] files. No need for a new file since the changes are all related to the IOMMUFD device usage.

Thanks,

C.

#7 lore raw

RE: [PATCH rfcv1 1/6] backends/iommufd_device: introduce IOMMUFDDevice

Quoted 47 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv1 1/6] backends/iommufd_device: introduce IOMMUFDDevice

Quoted 39 lines · Zhenzhong
Quoted 38 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 1/6] backends/iommufd_device: introduce IOMMUFDDevice

Quoted 16 lines · 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 iommufd handler and device id information which could be used by vIOMMU to get hw IOMMU information.

In future nested translation support, vIOMMU is going to have more iommufd related operations like allocate hwpt for a device, attach/detach hwpt, etc. So IOMMUFDDevice will be further expanded.

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.

Quoted 7 lines
---
  MAINTAINERS                     |  4 +--
  include/sysemu/iommufd_device.h | 31 ++++++++++++++++++++
  backends/iommufd_device.c       | 50

+++++++++++++++++++++++++++++++++
Maybe it is still time to move the iommufd files in a sepate dir, under hw at the same level as vfio.

Thoughts?

Quoted 7 lines
Quoted 1 line

Any reason for the movement? Hw dir contains entries to emulate

different

Quoted 1 line

Devices. Iommufd is not a real device. It's more a backend.

I would include the new services in the existing iommufd .[ch] files. No need for a new file since the changes are all related to the IOMMUFD device usage.

Make sense, will do.

Thanks
Zhenzhong

#8 lore raw

Re: [PATCH rfcv1 1/6] backends/iommufd_device: introduce IOMMUFDDevice

In reply to Duan, Zhenzhong
Quoted 32 lines · Zhenzhong
Quoted 32 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 1/6] backends/iommufd_device: introduce IOMMUFDDevice

Quoted 23 lines · 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 iommufd handler and device id information which could be used by vIOMMU to get hw IOMMU information.

In future nested translation support, vIOMMU is going to have more iommufd related operations like allocate hwpt for a device, attach/detach hwpt, etc. So IOMMUFDDevice will be further expanded.

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.

---
 MAINTAINERS                     |  4 +--
 include/sysemu/iommufd_device.h | 31 ++++++++++++++++++++
 backends/iommufd_device.c       | 50
Quoted 7 lines
Quoted 5 lines

+++++++++++++++++++++++++++++++++
Maybe it is still time to move the iommufd files in a sepate dir, under hw at the same level as vfio.

Thoughts?

Any reason for the movement? Hw dir contains entries to emulate different Devices. Iommufd is not a real device. It's more a backend.

Well I was thinking it would become bigger and bigger and since you created a new .c file with new abstraction (devices) the backend dir flat layout may not be well adapted. But as suggested by Cédric we may use the existing files.

Thanks

Eric

Quoted 3 lines

Thanks
Zhenzhong

#9 lore raw

RFC PATCH 2/6 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 IOMMUFDDevice 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 | 39 ++++++++++++++++++++++++++++++++++-
 hw/pci/pci.c         | 49 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 86 insertions(+), 2 deletions(-)
include/hw/pci/pci.h +38 -1
index fa6313aabc..a810c0ec74 100644
@@ -7,6 +7,8 @@
 /* PCI includes legacy ISA access.  */
 #include "hw/isa/isa.h"
 
+#include "sysemu/iommufd_device.h"
+
 extern bool pci_available;
 
 /* PCI bus */
@@ -384,10 +386,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: set iommufd device for a PCI device to vIOMMU
+     *
+     * Optional callback, if not implemented in vIOMMU, then vIOMMU can't
+     * utilize iommufd specific features.
+     *
+     * Return true if iommufd device is accepted, 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.
+     *
+     * @idev: the data structure representing iommufd device.
+     *
+     */
+    int (*set_iommu_device)(PCIBus *bus, void *opaque, int32_t devfn,
+                            IOMMUFDDevice *idev, Error **errp);
+    /**
+     * @unset_iommu_device: unset iommufd device for a PCI device from 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, int32_t devfn);
 } PCIIOMMUOps;
 
 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice *idev,
+                                Error **errp);
+void pci_device_unset_iommu_device(PCIDevice *dev);
 
 /**
  * pci_setup_iommu: Initialize specific IOMMU handlers for a PCIBus
hw/pci/pci.c +48 -1

67 lines not on this page · raw message

Signature · 1 line
2.34.1
#10 lore raw

Re: [PATCH rfcv1 2/6] hw/pci: introduce pci_device_set/unset_iommu_device()

Hi Zhenzhong,

Quoted 83 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds pci_device_set/unset_iommu_device() to set/unset IOMMUFDDevice 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 | 39 ++++++++++++++++++++++++++++++++++-
 hw/pci/pci.c         | 49 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 86 insertions(+), 2 deletions(-)
include/hw/pci/pci.h +38 -1
index fa6313aabc..a810c0ec74 100644
@@ -7,6 +7,8 @@
 /* PCI includes legacy ISA access.  */
 #include "hw/isa/isa.h"
 
+#include "sysemu/iommufd_device.h"
+
 extern bool pci_available;
 
 /* PCI bus */
@@ -384,10 +386,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: set iommufd device for a PCI device to vIOMMU
+     *
+     * Optional callback, if not implemented in vIOMMU, then vIOMMU can't
+     * utilize iommufd specific features.
+     *
+     * Return true if iommufd device is accepted, 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.
+     *
+     * @idev: the data structure representing iommufd device.
+     *
+     */
+    int (*set_iommu_device)(PCIBus *bus, void *opaque, int32_t devfn,
+                            IOMMUFDDevice *idev, Error **errp);
+    /**
+     * @unset_iommu_device: unset iommufd device for a PCI device from 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, int32_t devfn);
 } PCIIOMMUOps;
 
 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice *idev,
+                                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..3848662f95 100644

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

Quoted 8 lines
hw/pci/pci.c +4 -1
@@ -2672,7 +2672,10 @@ 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_pbus,
+                                           PCIBus **piommu_bus,
+                                           uint8_t *aliased_pdevfn)

nit: I would drop the p in aliased_pbus andaliased_pdevfn. Maybe you should allow the caller to pass NUL for aliased_pbus and aliased_pdevfn as it is the case for pci_device_set_iommu_device() I may resue that helper in [RFC 2/7] hw/pci: Introduce pci_device_iommu_bus

Quoted 51 lines
hw/pci/pci.c +37 -0
 {
     PCIBus *bus = pci_get_bus(dev);
     PCIBus *iommu_bus = bus;
@@ -2717,6 +2720,18 @@ AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
 
         iommu_bus = parent_bus;
     }
+    *aliased_pbus = bus;
+    *piommu_bus = iommu_bus;
+    *aliased_pdevfn = devfn;
+}
+
+AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus, &devfn);
     if (!pci_bus_bypass_iommu(bus) && iommu_bus->iommu_ops) {
         return iommu_bus->iommu_ops->get_address_space(bus,
                                  iommu_bus->iommu_opaque, devfn);
@@ -2724,6 +2739,38 @@ AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
     return &address_space_memory;
 }
 
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice *idev,
+                                Error **errp)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus, &devfn);
+    if (!pci_bus_bypass_iommu(bus) && iommu_bus &&
+        iommu_bus->iommu_ops && 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, idev, errp);
+    }
+    return 0;
+}
+
+void pci_device_unset_iommu_device(PCIDevice *dev)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus, &devfn);
+    if (!pci_bus_bypass_iommu(bus) && iommu_bus &&

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

Quoted 10 lines
hw/pci/pci.c +7 -0
+        iommu_bus->iommu_ops && 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

#11 lore raw

RE: [PATCH rfcv1 2/6] hw/pci: introduce pci_device_set/unset_iommu_device()

Quoted 105 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 2/6] 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 IOMMUFDDevice 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 | 39 ++++++++++++++++++++++++++++++++++-
 hw/pci/pci.c         | 49

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

Quoted 16 lines
 2 files changed, 86 insertions(+), 2 deletions(-)
include/hw/pci/pci.h +2 -0
index fa6313aabc..a810c0ec74 100644
@@ -7,6 +7,8 @@
 /* PCI includes legacy ISA access.  */
 #include "hw/isa/isa.h"
 
+#include "sysemu/iommufd_device.h"
+
 extern bool pci_available;
 
 /* PCI bus */

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

Quoted 4 lines
include/hw/pci/pci.h +0 -1
@@ -384,10 +386,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 389

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 { · line 391

Quoted 2 lines
include/hw/pci/pci.h +2 -0
+    /**
+     * @set_iommu_device: set iommufd device for a PCI device to

vIOMMU

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

Quoted 2 lines
include/hw/pci/pci.h +2 -0
+     *
+     * Optional callback, if not implemented in vIOMMU, then vIOMMU

can't

Quoted 8 lines
include/hw/pci/pci.h +8 -0
+     * utilize iommufd specific features.
+     *
+     * Return true if iommufd device is accepted, or else return false with
+     * errp set.
+     *
+     * @bus: the #PCIBus of the PCI device.
+     *
+     * @opaque: the data passed to pci_setup_iommu().

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

Quoted 10 lines
include/hw/pci/pci.h +10 -0
+     *
+     * @devfn: device and function number of the PCI device.
+     *
+     * @idev: the data structure representing iommufd device.
+     *
+     */
+    int (*set_iommu_device)(PCIBus *bus, void *opaque, int32_t devfn,
+                            IOMMUFDDevice *idev, Error **errp);
+    /**
+     * @unset_iommu_device: unset iommufd device for a PCI device from

vIOMMU

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

Quoted 10 lines
include/hw/pci/pci.h +10 -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, int32_t

devfn);

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

Quoted 4 lines
include/hw/pci/pci.h +1 -0
 } PCIIOMMUOps;
 
 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice

*idev,

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..3848662f95 100644
@@ -2672,7 +2672,10 @@ static void

pci_device_class_base_init(ObjectClass *klass, void *data)

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

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

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

nit: I would drop the p in aliased_pbus andaliased_pdevfn. Maybe you should allow the caller to pass NUL for aliased_pbus and aliased_pdevfn as it is the case for pci_device_set_iommu_device() I may resue that helper in [RFC 2/7] hw/pci: Introduce pci_device_iommu_bus

Good suggestion, will do.

Thanks
Zhenzhong

Quoted 72 lines

hw/pci/pci.c AddressSpace

Quoted 4 lines
hw/pci/pci.c +0 -0
 {
     PCIBus *bus = pci_get_bus(dev);
     PCIBus *iommu_bus = bus;
@@ -2717,6 +2720,18 @@ AddressSpace

*pci_device_iommu_address_space(PCIDevice *dev)

hw/pci/pci.c AddressSpace

Quoted 19 lines
hw/pci/pci.c +12 -0
         iommu_bus = parent_bus;
     }
+    *aliased_pbus = bus;
+    *piommu_bus = iommu_bus;
+    *aliased_pdevfn = devfn;
+}
+
+AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus, &devfn);
     if (!pci_bus_bypass_iommu(bus) && iommu_bus->iommu_ops) {
         return iommu_bus->iommu_ops->get_address_space(bus,
                                  iommu_bus->iommu_opaque, devfn);
@@ -2724,6 +2739,38 @@ AddressSpace

*pci_device_iommu_address_space(PCIDevice *dev)

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

Quoted 4 lines
hw/pci/pci.c +1 -0
     return &address_space_memory;
 }
 
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice

*idev,

Quoted 30 lines
hw/pci/pci.c +26 -0
+                                Error **errp)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus, &devfn);
+    if (!pci_bus_bypass_iommu(bus) && iommu_bus &&
+        iommu_bus->iommu_ops && 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, idev, errp);
+    }
+    return 0;
+}
+
+void pci_device_unset_iommu_device(PCIDevice *dev)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus, &devfn);
+    if (!pci_bus_bypass_iommu(bus) && iommu_bus &&
+        iommu_bus->iommu_ops && iommu_bus->iommu_ops-
 unset_iommu_device) {
+        return iommu_bus->iommu_ops-

unset_iommu_device(pci_get_bus(dev),

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

Quoted 6 lines
hw/pci/pci.c +5 -0
+                                                        iommu_bus->iommu_opaque,
+                                                        dev->devfn);
+    }
+}
+
 void pci_setup_iommu(PCIBus *bus, const PCIIOMMUOps *ops, void

*opaque)

Quoted 2 lines
 {
     /*

Thanks

Eric

#12 lore raw

Re: [PATCH rfcv1 2/6] hw/pci: introduce pci_device_set/unset_iommu_device()

In reply to Zhenzhong Duan
Quoted 116 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds pci_device_set/unset_iommu_device() to set/unset IOMMUFDDevice 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 | 39 ++++++++++++++++++++++++++++++++++-
  hw/pci/pci.c         | 49 +++++++++++++++++++++++++++++++++++++++++++-
  2 files changed, 86 insertions(+), 2 deletions(-)
include/hw/pci/pci.h +38 -1
index fa6313aabc..a810c0ec74 100644
@@ -7,6 +7,8 @@
  /* PCI includes legacy ISA access.  */
  #include "hw/isa/isa.h"
 
+#include "sysemu/iommufd_device.h"
+
  extern bool pci_available;
 
  /* PCI bus */
@@ -384,10 +386,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: set iommufd device for a PCI device to vIOMMU
+     *
+     * Optional callback, if not implemented in vIOMMU, then vIOMMU can't
+     * utilize iommufd specific features.
+     *
+     * Return true if iommufd device is accepted, 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.
+     *
+     * @idev: the data structure representing iommufd device.
+     *
+     */
+    int (*set_iommu_device)(PCIBus *bus, void *opaque, int32_t devfn,
+                            IOMMUFDDevice *idev, Error **errp);
+    /**
+     * @unset_iommu_device: unset iommufd device for a PCI device from 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, int32_t devfn);
  } PCIIOMMUOps;
 
  AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice *idev,
+                                Error **errp);
+void pci_device_unset_iommu_device(PCIDevice *dev);
 
  /**
   * pci_setup_iommu: Initialize specific IOMMU handlers for a PCIBus
hw/pci/pci.c +16 -1
index 76080af580..3848662f95 100644
@@ -2672,7 +2672,10 @@ 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_pbus,
+                                           PCIBus **piommu_bus,
+                                           uint8_t *aliased_pdevfn)
  {
      PCIBus *bus = pci_get_bus(dev);
      PCIBus *iommu_bus = bus;
@@ -2717,6 +2720,18 @@ AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
 
          iommu_bus = parent_bus;
      }
+    *aliased_pbus = bus;
+    *piommu_bus = iommu_bus;
+    *aliased_pdevfn = devfn;
+}
+
+AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus, &devfn);
      if (!pci_bus_bypass_iommu(bus) && iommu_bus->iommu_ops) {
          return iommu_bus->iommu_ops->get_address_space(bus,
                                   iommu_bus->iommu_opaque, devfn);
@@ -2724,6 +2739,38 @@ AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
      return &address_space_memory;
  }

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

Quoted 10 lines
hw/pci/pci.c +9 -0
 
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice *idev,
+                                Error **errp)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus, &devfn);
+    if (!pci_bus_bypass_iommu(bus) && iommu_bus &&

Why do we test iommu_bus in pci_device_un/set_iommu_device routines and not in pci_device_iommu_address_space() ?

Thanks,

C.

Quoted 26 lines
hw/pci/pci.c +23 -0
+        iommu_bus->iommu_ops && 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, idev, errp);
+    }
+    return 0;
+}
+
+void pci_device_unset_iommu_device(PCIDevice *dev)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus, &devfn);
+    if (!pci_bus_bypass_iommu(bus) && iommu_bus &&
+        iommu_bus->iommu_ops && 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)
  {
      /*
#13 lore raw

RE: [PATCH rfcv1 2/6] hw/pci: introduce pci_device_set/unset_iommu_device()

Quoted 138 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv1 2/6] hw/pci: introduce
pci_device_set/unset_iommu_device()

Quoted 9 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds pci_device_set/unset_iommu_device() to set/unset IOMMUFDDevice 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 | 39 ++++++++++++++++++++++++++++++++++-
  hw/pci/pci.c         | 49

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

Quoted 16 lines
  2 files changed, 86 insertions(+), 2 deletions(-)
include/hw/pci/pci.h +2 -0
index fa6313aabc..a810c0ec74 100644
@@ -7,6 +7,8 @@
  /* PCI includes legacy ISA access.  */
  #include "hw/isa/isa.h"
 
+#include "sysemu/iommufd_device.h"
+
  extern bool pci_available;
 
  /* PCI bus */

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

Quoted 4 lines
include/hw/pci/pci.h +0 -1
@@ -384,10 +386,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 389

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 { · line 391

Quoted 2 lines
include/hw/pci/pci.h +2 -0
+    /**
+     * @set_iommu_device: set iommufd device for a PCI device to

vIOMMU

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

Quoted 2 lines
include/hw/pci/pci.h +2 -0
+     *
+     * Optional callback, if not implemented in vIOMMU, then vIOMMU

can't

Quoted 8 lines
include/hw/pci/pci.h +8 -0
+     * utilize iommufd specific features.
+     *
+     * Return true if iommufd device is accepted, or else return false with
+     * errp set.
+     *
+     * @bus: the #PCIBus of the PCI device.
+     *
+     * @opaque: the data passed to pci_setup_iommu().

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

Quoted 10 lines
include/hw/pci/pci.h +10 -0
+     *
+     * @devfn: device and function number of the PCI device.
+     *
+     * @idev: the data structure representing iommufd device.
+     *
+     */
+    int (*set_iommu_device)(PCIBus *bus, void *opaque, int32_t devfn,
+                            IOMMUFDDevice *idev, Error **errp);
+    /**
+     * @unset_iommu_device: unset iommufd device for a PCI device from

vIOMMU

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

Quoted 10 lines
include/hw/pci/pci.h +10 -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, int32_t

devfn);

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

Quoted 4 lines
include/hw/pci/pci.h +1 -0
  } PCIIOMMUOps;
 
  AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice

*idev,

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..3848662f95 100644
@@ -2672,7 +2672,10 @@ static void

pci_device_class_base_init(ObjectClass *klass, void *data)

hw/pci/pci.c AddressSpace

Quoted 12 lines
hw/pci/pci.c +4 -1
      }
  }
 
-AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+static void pci_device_get_iommu_bus_devfn(PCIDevice *dev,
+                                           PCIBus **aliased_pbus,
+                                           PCIBus **piommu_bus,
+                                           uint8_t *aliased_pdevfn)
  {
      PCIBus *bus = pci_get_bus(dev);
      PCIBus *iommu_bus = bus;
@@ -2717,6 +2720,18 @@ AddressSpace

*pci_device_iommu_address_space(PCIDevice *dev)

hw/pci/pci.c AddressSpace

Quoted 19 lines
hw/pci/pci.c +12 -0
          iommu_bus = parent_bus;
      }
+    *aliased_pbus = bus;
+    *piommu_bus = iommu_bus;
+    *aliased_pdevfn = devfn;
+}
+
+AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus, &devfn);
      if (!pci_bus_bypass_iommu(bus) && iommu_bus->iommu_ops) {
          return iommu_bus->iommu_ops->get_address_space(bus,
                                   iommu_bus->iommu_opaque, devfn);
@@ -2724,6 +2739,38 @@ AddressSpace

*pci_device_iommu_address_space(PCIDevice *dev)

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

Quoted 4 lines
hw/pci/pci.c +1 -0
      return &address_space_memory;
  }
 
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice

*idev,

Quoted 12 lines

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

Quoted 8 lines
hw/pci/pci.c +8 -0
+                                Error **errp)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus, &devfn);
+    if (!pci_bus_bypass_iommu(bus) && iommu_bus &&

Why do we test iommu_bus in pci_device_un/set_iommu_device routines and
not in pci_device_iommu_address_space() ?

iommu_bus check in pci_device_iommu_address_space() is dropped in below commit, I didn't find related discussion in mail history, maybe by accident? I can add it back if it's not intentional.

ba7d12eb8c hw/pci: modify pci_setup_iommu() to set PCIIOMMUOps

Thanks
Zhenzhong

#14 lore raw

Re: [PATCH rfcv1 2/6] hw/pci: introduce pci_device_set/unset_iommu_device()

Quoted 151 lines · Zhenzhong
Quoted 138 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv1 2/6] hw/pci: introduce
pci_device_set/unset_iommu_device()

Quoted 9 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds pci_device_set/unset_iommu_device() to set/unset IOMMUFDDevice 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 | 39 ++++++++++++++++++++++++++++++++++-
   hw/pci/pci.c         | 49

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

Quoted 16 lines
   2 files changed, 86 insertions(+), 2 deletions(-)
include/hw/pci/pci.h +2 -0
index fa6313aabc..a810c0ec74 100644
@@ -7,6 +7,8 @@
   /* PCI includes legacy ISA access.  */
   #include "hw/isa/isa.h"
 
+#include "sysemu/iommufd_device.h"
+
   extern bool pci_available;
 
   /* PCI bus */

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

Quoted 4 lines
include/hw/pci/pci.h +0 -1
@@ -384,10 +386,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 389

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 { · line 391

Quoted 2 lines
include/hw/pci/pci.h +2 -0
+    /**
+     * @set_iommu_device: set iommufd device for a PCI device to

vIOMMU

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

Quoted 2 lines
include/hw/pci/pci.h +2 -0
+     *
+     * Optional callback, if not implemented in vIOMMU, then vIOMMU

can't

Quoted 8 lines
include/hw/pci/pci.h +8 -0
+     * utilize iommufd specific features.
+     *
+     * Return true if iommufd device is accepted, or else return false with
+     * errp set.
+     *
+     * @bus: the #PCIBus of the PCI device.
+     *
+     * @opaque: the data passed to pci_setup_iommu().

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

Quoted 10 lines
include/hw/pci/pci.h +10 -0
+     *
+     * @devfn: device and function number of the PCI device.
+     *
+     * @idev: the data structure representing iommufd device.
+     *
+     */
+    int (*set_iommu_device)(PCIBus *bus, void *opaque, int32_t devfn,
+                            IOMMUFDDevice *idev, Error **errp);
+    /**
+     * @unset_iommu_device: unset iommufd device for a PCI device from

vIOMMU

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

Quoted 10 lines
include/hw/pci/pci.h +10 -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, int32_t

devfn);

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

Quoted 4 lines
include/hw/pci/pci.h +1 -0
   } PCIIOMMUOps;
 
   AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice

*idev,

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..3848662f95 100644
@@ -2672,7 +2672,10 @@ static void

pci_device_class_base_init(ObjectClass *klass, void *data)

hw/pci/pci.c AddressSpace

Quoted 12 lines
hw/pci/pci.c +4 -1
       }
   }
 
-AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+static void pci_device_get_iommu_bus_devfn(PCIDevice *dev,
+                                           PCIBus **aliased_pbus,
+                                           PCIBus **piommu_bus,
+                                           uint8_t *aliased_pdevfn)
   {
       PCIBus *bus = pci_get_bus(dev);
       PCIBus *iommu_bus = bus;
@@ -2717,6 +2720,18 @@ AddressSpace

*pci_device_iommu_address_space(PCIDevice *dev)

hw/pci/pci.c AddressSpace

Quoted 19 lines
hw/pci/pci.c +12 -0
           iommu_bus = parent_bus;
       }
+    *aliased_pbus = bus;
+    *piommu_bus = iommu_bus;
+    *aliased_pdevfn = devfn;
+}
+
+AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus, &devfn);
       if (!pci_bus_bypass_iommu(bus) && iommu_bus->iommu_ops) {
           return iommu_bus->iommu_ops->get_address_space(bus,
                                    iommu_bus->iommu_opaque, devfn);
@@ -2724,6 +2739,38 @@ AddressSpace

*pci_device_iommu_address_space(PCIDevice *dev)

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

Quoted 4 lines
hw/pci/pci.c +1 -0
       return &address_space_memory;
   }
 
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice

*idev,

Quoted 12 lines

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

Quoted 8 lines
hw/pci/pci.c +8 -0
+                                Error **errp)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus, &devfn);
+    if (!pci_bus_bypass_iommu(bus) && iommu_bus &&

Why do we test iommu_bus in pci_device_un/set_iommu_device routines and
not in pci_device_iommu_address_space() ?

Quoted 3 lines

iommu_bus check in pci_device_iommu_address_space() is dropped in below commit, I didn't find related discussion in mail history, maybe by accident? I can add it back if it's not intentional.

Can iommu_bus be NULL or should we add an assert ?

C.

Quoted 5 lines

ba7d12eb8c hw/pci: modify pci_setup_iommu() to set PCIIOMMUOps

Thanks
Zhenzhong

#15 lore raw

RE: [PATCH rfcv1 2/6] hw/pci: introduce pci_device_set/unset_iommu_device()

Quoted 162 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv1 2/6] hw/pci: introduce
pci_device_set/unset_iommu_device()

Quoted 17 lines · Zhenzhong
Quoted 17 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv1 2/6] hw/pci: introduce
pci_device_set/unset_iommu_device()

Quoted 9 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds pci_device_set/unset_iommu_device() to set/unset IOMMUFDDevice 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 6 lines
Quoted 6 lines
Quoted 6 lines
---
   include/hw/pci/pci.h | 39

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

Quoted 16 lines
Quoted 16 lines
Quoted 1 line
   hw/pci/pci.c         | 49

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

Quoted 14 lines
   2 files changed, 86 insertions(+), 2 deletions(-)
include/hw/pci/pci.h +2 -0
index fa6313aabc..a810c0ec74 100644
@@ -7,6 +7,8 @@
   /* PCI includes legacy ISA access.  */
   #include "hw/isa/isa.h"
 
+#include "sysemu/iommufd_device.h"
+
   extern bool pci_available;
 
Quoted 8 lines
Quoted 8 lines

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

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

devfn);

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

int

Quoted 17 lines
Quoted 17 lines

devfn);

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

Quoted 2 lines
include/hw/pci/pci.h +2 -0
+    /**
+     * @set_iommu_device: set iommufd device for a PCI device to

vIOMMU

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

Quoted 2 lines
include/hw/pci/pci.h +2 -0
+     *
+     * Optional callback, if not implemented in vIOMMU, then vIOMMU

can't

Quoted 10 lines
include/hw/pci/pci.h +10 -0
+     * utilize iommufd specific features.
+     *
+     * Return true if iommufd device is accepted, 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.
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
include/hw/pci/pci.h +8 -0
+     *
+     * @idev: the data structure representing iommufd device.
+     *
+     */
+    int (*set_iommu_device)(PCIBus *bus, void *opaque, int32_t devfn,
+                            IOMMUFDDevice *idev, Error **errp);
+    /**
+     * @unset_iommu_device: unset iommufd device for a PCI device

from

Quoted 42 lines
Quoted 42 lines

vIOMMU

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

Quoted 10 lines
include/hw/pci/pci.h +10 -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, int32_t

devfn);

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

Quoted 4 lines
include/hw/pci/pci.h +1 -0
   } PCIIOMMUOps;
 
   AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice

*idev,

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..3848662f95 100644
@@ -2672,7 +2672,10 @@ static void

pci_device_class_base_init(ObjectClass *klass, void *data)

hw/pci/pci.c AddressSpace

Quoted 12 lines
hw/pci/pci.c +4 -1
       }
   }
 
-AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+static void pci_device_get_iommu_bus_devfn(PCIDevice *dev,
+                                           PCIBus **aliased_pbus,
+                                           PCIBus **piommu_bus,
+                                           uint8_t *aliased_pdevfn)
   {
       PCIBus *bus = pci_get_bus(dev);
       PCIBus *iommu_bus = bus;
@@ -2717,6 +2720,18 @@ AddressSpace

*pci_device_iommu_address_space(PCIDevice *dev)

Quoted 1 line
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
           iommu_bus = parent_bus;
       }
hw/pci/pci.c +4 -0
+    *aliased_pbus = bus;
+    *piommu_bus = iommu_bus;
+    *aliased_pdevfn = devfn;
+}
Quoted 8 lines

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

Quoted 8 lines
hw/pci/pci.c +8 -0
+
+AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus,

&devfn);

Quoted 9 lines
Quoted 9 lines

hw/pci/pci.c AddressSpace

Quoted 4 lines
       if (!pci_bus_bypass_iommu(bus) && iommu_bus->iommu_ops) {
           return iommu_bus->iommu_ops->get_address_space(bus,
                                    iommu_bus->iommu_opaque, devfn);
hw/pci/pci.c +0 -0
@@ -2724,6 +2739,38 @@ AddressSpace

*pci_device_iommu_address_space(PCIDevice *dev)

Quoted 4 lines
hw/pci/pci.c +1 -0
       return &address_space_memory;
   }
 
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice
Quoted 8 lines
Quoted 8 lines

*idev,

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

Quoted 7 lines
hw/pci/pci.c +7 -0
+                                Error **errp)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus,

&devfn);

Quoted 3 lines
Quoted 3 lines

hw/pci/pci.c AddressSpace · line 2750

Quoted 1 line
hw/pci/pci.c +1 -0
+    if (!pci_bus_bypass_iommu(bus) && iommu_bus &&

Why do we test iommu_bus in pci_device_un/set_iommu_device

routines

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

and
not in pci_device_iommu_address_space() ?

iommu_bus check in pci_device_iommu_address_space() is dropped in below commit, I didn't find related discussion in mail history, maybe by accident? I can add it back if it's not intentional.

Can iommu_bus be NULL or should we add an assert ?

I dig into the history changes of pci_device_iommu_address_space() and below commit added iommu_bus check.

5af2ae230514 pci: Fix pci_device_iommu_address_space() bus propagation

In theory, !iommu_bus->parent_dev take precedency over !iommu_bus, So we never see iommu_bus NULL, assert may be better.

Thanks
Zhenzhong

#16 lore raw

Re: [PATCH rfcv1 2/6] hw/pci: introduce pci_device_set/unset_iommu_device()

Quoted 163 lines · Zhenzhong
Quoted 163 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv1 2/6] hw/pci: introduce
pci_device_set/unset_iommu_device()

Quoted 16 lines · Zhenzhong
Quoted 16 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv1 2/6] hw/pci: introduce
pci_device_set/unset_iommu_device()

Quoted 9 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds pci_device_set/unset_iommu_device() to set/unset IOMMUFDDevice 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 6 lines
Quoted 6 lines
Quoted 6 lines
---
   include/hw/pci/pci.h | 39

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

Quoted 16 lines
Quoted 16 lines
Quoted 1 line
   hw/pci/pci.c         | 49

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

Quoted 14 lines
   2 files changed, 86 insertions(+), 2 deletions(-)
include/hw/pci/pci.h +2 -0
index fa6313aabc..a810c0ec74 100644
@@ -7,6 +7,8 @@
   /* PCI includes legacy ISA access.  */
   #include "hw/isa/isa.h"
 
+#include "sysemu/iommufd_device.h"
+
   extern bool pci_available;
 
Quoted 8 lines
Quoted 8 lines

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

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

devfn);

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

int

Quoted 17 lines
Quoted 17 lines

devfn);

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

Quoted 2 lines
include/hw/pci/pci.h +2 -0
+    /**
+     * @set_iommu_device: set iommufd device for a PCI device to

vIOMMU

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

Quoted 2 lines
include/hw/pci/pci.h +2 -0
+     *
+     * Optional callback, if not implemented in vIOMMU, then vIOMMU

can't

Quoted 10 lines
include/hw/pci/pci.h +10 -0
+     * utilize iommufd specific features.
+     *
+     * Return true if iommufd device is accepted, 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.
Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
include/hw/pci/pci.h +8 -0
+     *
+     * @idev: the data structure representing iommufd device.
+     *
+     */
+    int (*set_iommu_device)(PCIBus *bus, void *opaque, int32_t devfn,
+                            IOMMUFDDevice *idev, Error **errp);
+    /**
+     * @unset_iommu_device: unset iommufd device for a PCI device

from

Quoted 41 lines
Quoted 41 lines

vIOMMU

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

Quoted 10 lines
include/hw/pci/pci.h +10 -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, int32_t

devfn);

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

Quoted 4 lines
include/hw/pci/pci.h +1 -0
   } PCIIOMMUOps;
 
   AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice

*idev,

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..3848662f95 100644
@@ -2672,7 +2672,10 @@ static void

pci_device_class_base_init(ObjectClass *klass, void *data)

hw/pci/pci.c AddressSpace

Quoted 12 lines
hw/pci/pci.c +4 -1
       }
   }
 
-AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+static void pci_device_get_iommu_bus_devfn(PCIDevice *dev,
+                                           PCIBus **aliased_pbus,
+                                           PCIBus **piommu_bus,
+                                           uint8_t *aliased_pdevfn)
   {
       PCIBus *bus = pci_get_bus(dev);
       PCIBus *iommu_bus = bus;
@@ -2717,6 +2720,18 @@ AddressSpace

*pci_device_iommu_address_space(PCIDevice *dev)

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
hw/pci/pci.c +4 -0
           iommu_bus = parent_bus;
       }
+    *aliased_pbus = bus;
+    *piommu_bus = iommu_bus;
+    *aliased_pdevfn = devfn;
+}
Quoted 8 lines

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

Quoted 8 lines
hw/pci/pci.c +8 -0
+
+AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus,

&devfn);

Quoted 9 lines
Quoted 9 lines

hw/pci/pci.c AddressSpace

Quoted 4 lines
hw/pci/pci.c +0 -0
       if (!pci_bus_bypass_iommu(bus) && iommu_bus->iommu_ops) {
           return iommu_bus->iommu_ops->get_address_space(bus,
                                    iommu_bus->iommu_opaque, devfn);
@@ -2724,6 +2739,38 @@ AddressSpace

*pci_device_iommu_address_space(PCIDevice *dev)

Quoted 4 lines
hw/pci/pci.c +1 -0
       return &address_space_memory;
   }
 
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice
Quoted 8 lines
Quoted 8 lines

*idev,

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

Quoted 7 lines
hw/pci/pci.c +7 -0
+                                Error **errp)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus,

&devfn);

Quoted 2 lines
Quoted 2 lines

hw/pci/pci.c AddressSpace · line 2750

Quoted 1 line
hw/pci/pci.c +1 -0
+    if (!pci_bus_bypass_iommu(bus) && iommu_bus &&

Why do we test iommu_bus in pci_device_un/set_iommu_device

routines

Quoted 5 lines
Quoted 2 lines

and
not in pci_device_iommu_address_space() ?

iommu_bus check in pci_device_iommu_address_space() is dropped in below commit, I didn't find related discussion in mail history, maybe by accident? I can add it back if it's not intentional.

Quoted 8 lines
Quoted 1 line

Can iommu_bus be NULL or should we add an assert ?

I dig into the history changes of pci_device_iommu_address_space() and below commit added iommu_bus check.

5af2ae230514 pci: Fix pci_device_iommu_address_space() bus propagation

In theory, !iommu_bus->parent_dev take precedency over !iommu_bus, So we never see iommu_bus NULL, assert may be better.

I think we had such a discussion in
https://www.mail-archive.com/[email protected]/msg994766.html
But maybe this was related to a different call place. I remember I challenged the check at some point

Eric

Quoted 4 lines

Thanks
Zhenzhong

#17 lore raw

RE: [PATCH rfcv1 2/6] hw/pci: introduce pci_device_set/unset_iommu_device()

Quoted 185 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 2/6] hw/pci: introduce
pci_device_set/unset_iommu_device()

Quoted 46 lines · Zhenzhong
Quoted 46 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv1 2/6] hw/pci: introduce
pci_device_set/unset_iommu_device()

Quoted 16 lines · Zhenzhong
Quoted 16 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv1 2/6] hw/pci: introduce
pci_device_set/unset_iommu_device()

Quoted 9 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds pci_device_set/unset_iommu_device() to set/unset IOMMUFDDevice 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 6 lines
Quoted 6 lines
Quoted 6 lines
---
   include/hw/pci/pci.h | 39

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

Quoted 16 lines
Quoted 16 lines
Quoted 1 line
   hw/pci/pci.c         | 49

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

Quoted 14 lines
   2 files changed, 86 insertions(+), 2 deletions(-)
include/hw/pci/pci.h +2 -0
index fa6313aabc..a810c0ec74 100644
@@ -7,6 +7,8 @@
   /* PCI includes legacy ISA access.  */
   #include "hw/isa/isa.h"
 
+#include "sysemu/iommufd_device.h"
+
   extern bool pci_available;
 
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

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

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

int

Quoted 9 lines
Quoted 9 lines
Quoted 2 lines
Quoted 2 lines

devfn);

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

int

Quoted 6 lines
Quoted 6 lines

devfn);

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

Quoted 2 lines
include/hw/pci/pci.h +2 -0
+    /**
+     * @set_iommu_device: set iommufd device for a PCI device to

vIOMMU

Quoted 2 lines
include/hw/pci/pci.h +2 -0
+     *
+     * Optional callback, if not implemented in vIOMMU, then

vIOMMU

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

can't

Quoted 3 lines
include/hw/pci/pci.h +3 -0
+     * utilize iommufd specific features.
+     *
+     * Return true if iommufd device is accepted, or else return false

with

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

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

Quoted 8 lines
include/hw/pci/pci.h +8 -0
+     * @opaque: the data passed to pci_setup_iommu().
+     *
+     * @devfn: device and function number of the PCI device.
+     *
+     * @idev: the data structure representing iommufd device.
+     *
+     */
+    int (*set_iommu_device)(PCIBus *bus, void *opaque, int32_t

devfn,

Quoted 18 lines
Quoted 18 lines
Quoted 3 lines
Quoted 3 lines

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

Quoted 3 lines
include/hw/pci/pci.h +3 -0
+                            IOMMUFDDevice *idev, Error **errp);
+    /**
+     * @unset_iommu_device: unset iommufd device for a PCI device

from

Quoted 14 lines
Quoted 14 lines

vIOMMU

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

Quoted 10 lines
include/hw/pci/pci.h +10 -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, int32_t

devfn);

Quoted 2 lines
include/hw/pci/pci.h +0 -0
   } PCIIOMMUOps;
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

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

Quoted 2 lines
include/hw/pci/pci.h +1 -0
   AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice

*idev,

Quoted 5 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

Quoted 57 lines
Quoted 57 lines
Quoted 19 lines
Quoted 19 lines

hw/pci/pci.c static void

Quoted 5 lines
hw/pci/pci.c +0 -0
index 76080af580..3848662f95 100644
@@ -2672,7 +2672,10 @@ static void

pci_device_class_base_init(ObjectClass *klass, void *data)

hw/pci/pci.c AddressSpace

Quoted 12 lines
hw/pci/pci.c +4 -1
       }
   }
 
-AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+static void pci_device_get_iommu_bus_devfn(PCIDevice *dev,
+                                           PCIBus **aliased_pbus,
+                                           PCIBus **piommu_bus,
+                                           uint8_t *aliased_pdevfn)
   {
       PCIBus *bus = pci_get_bus(dev);
       PCIBus *iommu_bus = bus;
@@ -2717,6 +2720,18 @@ AddressSpace

*pci_device_iommu_address_space(PCIDevice *dev)

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
hw/pci/pci.c +4 -0
           iommu_bus = parent_bus;
       }
+    *aliased_pbus = bus;
+    *piommu_bus = iommu_bus;
+    *aliased_pdevfn = devfn;
+}
Quoted 8 lines

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

Quoted 8 lines
hw/pci/pci.c +8 -0
+
+AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus,

&devfn);

Quoted 9 lines
Quoted 9 lines

hw/pci/pci.c AddressSpace

Quoted 4 lines
hw/pci/pci.c +0 -0
       if (!pci_bus_bypass_iommu(bus) && iommu_bus->iommu_ops) {
           return iommu_bus->iommu_ops->get_address_space(bus,
                                    iommu_bus->iommu_opaque, devfn);
@@ -2724,6 +2739,38 @@ AddressSpace

*pci_device_iommu_address_space(PCIDevice *dev)

Quoted 4 lines
hw/pci/pci.c +1 -0
       return &address_space_memory;
   }
 
+int pci_device_set_iommu_device(PCIDevice *dev, IOMMUFDDevice
Quoted 8 lines
Quoted 8 lines

*idev,

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

Quoted 7 lines
hw/pci/pci.c +7 -0
+                                Error **errp)
+{
+    PCIBus *bus;
+    PCIBus *iommu_bus;
+    uint8_t devfn;
+
+    pci_device_get_iommu_bus_devfn(dev, &bus, &iommu_bus,

&devfn);

Quoted 2 lines
Quoted 2 lines

hw/pci/pci.c AddressSpace · line 2750

Quoted 1 line
hw/pci/pci.c +1 -0
+    if (!pci_bus_bypass_iommu(bus) && iommu_bus &&

Why do we test iommu_bus in pci_device_un/set_iommu_device

routines

Quoted 2 lines
Quoted 2 lines

and
not in pci_device_iommu_address_space() ?

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

iommu_bus check in pci_device_iommu_address_space() is dropped in below commit, I didn't find related discussion in mail history, maybe by accident? I can add it back if it's not intentional.

Can iommu_bus be NULL or should we add an assert ?

I dig into the history changes of pci_device_iommu_address_space() and below commit added iommu_bus check.

5af2ae230514 pci: Fix pci_device_iommu_address_space() bus

propagation

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

In theory, !iommu_bus->parent_dev take precedency over !iommu_bus, So we never see iommu_bus NULL, assert may be better.

I think we had such a discussion in
https://www.mail-archive.com/[email protected]/msg994766.html
But maybe this was related to a different call place. I remember I challenged the check at some point

It seems this question is not discussed further in that thread. Per my code inspection, PCI root bus's parent_dev should be NULL, so we get either root bus or sub bus, neither a NULL.
Also tested with PXB bridge which is suspicious scenarios, same.

Thanks
Zhenzhong

#18 lore raw

RFC PATCH 3/6 intel_iommu: add set/unset_iommu_device callback

In reply to Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds set/unset_iommu_device() implementation in Intel vIOMMU. In set call, IOMMUFDDevice is recorded in hash table indexed by PCI BDF.

---
 include/hw/i386/intel_iommu.h | 10 +++++
 hw/i386/intel_iommu.c         | 79 +++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)
include/hw/i386/intel_iommu.h +10 -0
index 7fa0a695c8..c65fdde56f 100644
@@ -62,6 +62,7 @@ typedef union VTD_IR_TableEntry VTD_IR_TableEntry;
 typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress;
 typedef struct VTDPASIDDirEntry VTDPASIDDirEntry;
 typedef struct VTDPASIDEntry VTDPASIDEntry;
+typedef struct VTDIOMMUFDDevice VTDIOMMUFDDevice;
 
 /* Context-Entry */
 struct VTDContextEntry {
@@ -148,6 +149,13 @@ struct VTDAddressSpace {
     IOVATree *iova_tree;
 };
 
+struct VTDIOMMUFDDevice {
+    PCIBus *bus;
+    uint8_t devfn;
+    IOMMUFDDevice *idev;
+    IntelIOMMUState *iommu_state;
+};
+
 struct VTDIOTLBEntry {
     uint64_t gfn;
     uint16_t domain_id;
@@ -292,6 +300,8 @@ struct IntelIOMMUState {
     /* list of registered notifiers */
     QLIST_HEAD(, VTDAddressSpace) vtd_as_with_notifiers;
 
+    GHashTable *vtd_iommufd_dev;             /* VTDIOMMUFDDevice */
+
     /* interrupt remapping */
     bool intr_enabled;              /* Whether guest enabled IR */
     dma_addr_t intr_root;           /* Interrupt remapping table pointer */
hw/i386/intel_iommu.c +79 -0

103 lines not on this page · raw message

Signature · 1 line
2.34.1
#19 lore raw

Re: [PATCH rfcv1 3/6] intel_iommu: add set/unset_iommu_device callback

Hi Zhenzhong,

Quoted 27 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds set/unset_iommu_device() implementation in Intel vIOMMU. In set call, IOMMUFDDevice is recorded in hash table indexed by PCI BDF.

---
 include/hw/i386/intel_iommu.h | 10 +++++
 hw/i386/intel_iommu.c         | 79 +++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)
include/hw/i386/intel_iommu.h +1 -0
index 7fa0a695c8..c65fdde56f 100644
@@ -62,6 +62,7 @@ typedef union VTD_IR_TableEntry VTD_IR_TableEntry;
 typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress;
 typedef struct VTDPASIDDirEntry VTDPASIDDirEntry;
 typedef struct VTDPASIDEntry VTDPASIDEntry;
+typedef struct VTDIOMMUFDDevice VTDIOMMUFDDevice;
 
 /* Context-Entry */
 struct VTDContextEntry {

include/hw/i386/intel_iommu.h struct VTDAddressSpace { · line 158

Quoted 10 lines
include/hw/i386/intel_iommu.h +7 -0
@@ -148,6 +149,13 @@ struct VTDAddressSpace {
     IOVATree *iova_tree;
 };
 
+struct VTDIOMMUFDDevice {
+    PCIBus *bus;
+    uint8_t devfn;
+    IOMMUFDDevice *idev;
+    IntelIOMMUState *iommu_state;
+};
+

Just wondering whether we shouldn't reuse the VTDAddressSpace to store the idev, if any. How have you made your choice. What will it become when PASID gets added?

Quoted 115 lines
include/hw/i386/intel_iommu.h +2 -0
 struct VTDIOTLBEntry {
     uint64_t gfn;
     uint16_t domain_id;
@@ -292,6 +300,8 @@ struct IntelIOMMUState {
     /* list of registered notifiers */
     QLIST_HEAD(, VTDAddressSpace) vtd_as_with_notifiers;
 
+    GHashTable *vtd_iommufd_dev;             /* VTDIOMMUFDDevice */
+
     /* interrupt remapping */
     bool intr_enabled;              /* Whether guest enabled IR */
     dma_addr_t intr_root;           /* Interrupt remapping table pointer */
hw/i386/intel_iommu.c +77 -0
index ed5677c0ae..95faf697eb 100644
@@ -237,6 +237,13 @@ static gboolean vtd_as_equal(gconstpointer v1, gconstpointer v2)
            (key1->pasid == key2->pasid);
 }
 
+static gboolean vtd_as_idev_equal(gconstpointer v1, gconstpointer v2)
+{
+    const struct vtd_as_key *key1 = v1;
+    const struct vtd_as_key *key2 = v2;
+
+    return (key1->bus == key2->bus) && (key1->devfn == key2->devfn);
+}
 /*
  * Note that we use pointer to PCIBus as the key, so hashing/shifting
  * based on the pointer value is intended. Note that we deal with
@@ -3812,6 +3819,74 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
     return vtd_dev_as;
 }
 
+static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int32_t devfn,
+                                    IOMMUFDDevice *idev, Error **errp)
+{
+    IntelIOMMUState *s = opaque;
+    VTDIOMMUFDDevice *vtd_idev;
+    struct vtd_as_key key = {
+        .bus = bus,
+        .devfn = devfn,
+    };
+    struct vtd_as_key *new_key;
+
+    assert(0 <= devfn && devfn < PCI_DEVFN_MAX);
+
+    /* None IOMMUFD case */
+    if (!idev) {
+        return 0;
+    }
+
+    vtd_iommu_lock(s);
+
+    vtd_idev = g_hash_table_lookup(s->vtd_iommufd_dev, &key);
+
+    if (vtd_idev) {
+        error_setg(errp, "IOMMUFD device already exist");
+        return -1;
+    }
+
+    new_key = g_malloc(sizeof(*new_key));
+    new_key->bus = bus;
+    new_key->devfn = devfn;
+
+    vtd_idev = g_malloc0(sizeof(VTDIOMMUFDDevice));
+    vtd_idev->bus = bus;
+    vtd_idev->devfn = (uint8_t)devfn;
+    vtd_idev->iommu_state = s;
+    vtd_idev->idev = idev;
+
+    g_hash_table_insert(s->vtd_iommufd_dev, new_key, vtd_idev);
+
+    vtd_iommu_unlock(s);
+
+    return 0;
+}
+
+static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int32_t devfn)
+{
+    IntelIOMMUState *s = opaque;
+    VTDIOMMUFDDevice *vtd_idev;
+    struct vtd_as_key key = {
+        .bus = bus,
+        .devfn = devfn,
+    };
+
+    assert(0 <= devfn && devfn < PCI_DEVFN_MAX);
+
+    vtd_iommu_lock(s);
+
+    vtd_idev = g_hash_table_lookup(s->vtd_iommufd_dev, &key);
+    if (!vtd_idev) {
+        vtd_iommu_unlock(s);
+        return;
+    }
+
+    g_hash_table_remove(s->vtd_iommufd_dev, &key);
+
+    vtd_iommu_unlock(s);
+}
+
 /* Unmap the whole range in the notifier's scope. */
 static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
 {
@@ -4107,6 +4182,8 @@ static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)
 
 static PCIIOMMUOps vtd_iommu_ops = {
     .get_address_space = vtd_host_dma_iommu,
+    .set_iommu_device = vtd_dev_set_iommu_device,
+    .unset_iommu_device = vtd_dev_unset_iommu_device,
 };
 
 static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)

hw/i386/intel_iommu.c static void vtd_realize(DeviceState *dev, Error **errp) · line 4314

Quoted 8 lines
hw/i386/intel_iommu.c +2 -0
@@ -4230,6 +4307,8 @@ static void vtd_realize(DeviceState *dev, Error **errp)
                                      g_free, g_free);
     s->vtd_address_spaces = g_hash_table_new_full(vtd_as_hash, vtd_as_equal,
                                       g_free, g_free);
+    s->vtd_iommufd_dev = g_hash_table_new_full(vtd_as_hash, vtd_as_idev_equal,
+                                               g_free, g_free);
     vtd_init(s);
     pci_setup_iommu(bus, &vtd_iommu_ops, dev);
     /* Pseudo address space under root PCI bus. */

Thanks

Eric

#20 lore raw

RE: [PATCH rfcv1 3/6] intel_iommu: add set/unset_iommu_device callback

Quoted 42 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 3/6] intel_iommu: add set/unset_iommu_device callback

Hi Zhenzhong,

Quoted 6 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds set/unset_iommu_device() implementation in Intel vIOMMU. In set call, IOMMUFDDevice is recorded in hash table indexed by PCI BDF.

Quoted 6 lines
---
 include/hw/i386/intel_iommu.h | 10 +++++
 hw/i386/intel_iommu.c         | 79

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

Review

Quoted 3 lines
 2 files changed, 89 insertions(+)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 15 lines

index 7fa0a695c8..c65fdde56f 100644

include/hw/i386/intel_iommu.h +1 -0
@@ -62,6 +62,7 @@ typedef union VTD_IR_TableEntry VTD_IR_TableEntry;
 typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress;
 typedef struct VTDPASIDDirEntry VTDPASIDDirEntry;
 typedef struct VTDPASIDEntry VTDPASIDEntry;
+typedef struct VTDIOMMUFDDevice VTDIOMMUFDDevice;
 
 /* Context-Entry */
 struct VTDContextEntry {
@@ -148,6 +149,13 @@ struct VTDAddressSpace {
     IOVATree *iova_tree;
 };
 
Quoted 10 lines

include/hw/i386/intel_iommu.h struct VTDAddressSpace { · line 158

Quoted 7 lines
include/hw/i386/intel_iommu.h +7 -0
+struct VTDIOMMUFDDevice {
+    PCIBus *bus;
+    uint8_t devfn;
+    IOMMUFDDevice *idev;
+    IntelIOMMUState *iommu_state;
+};
+

Just wondering whether we shouldn't reuse the VTDAddressSpace to store the idev, if any. How have you made your choice. What will it become when PASID gets added?

VTDAddressSpace is indexed by aliased BDF, but VTDIOMMUFDDevice is indexed by device's BDF. So we can't just store VTDIOMMUFDDevice as a pointer in VTDAddressSpace, may need a list in case more than one device in same address space. Then a global VTDIOMMUFDDevice list is better for lookup.

For PASID in modern mode which support stage-1 page table, we have VTDPASIDAddressSpace indexed by device's BDF+PASID, We didn't use VTDAddressSpace which is for stage-2 page table.

Thanks
Zhenzhong

Quoted 135 lines

hw/i386/intel_iommu.c static gboolean vtd_as_equal(gconstpointer v1,

Quoted 17 lines
include/hw/i386/intel_iommu.h +2 -0
 struct VTDIOTLBEntry {
     uint64_t gfn;
     uint16_t domain_id;
@@ -292,6 +300,8 @@ struct IntelIOMMUState {
     /* list of registered notifiers */
     QLIST_HEAD(, VTDAddressSpace) vtd_as_with_notifiers;
 
+    GHashTable *vtd_iommufd_dev;             /* VTDIOMMUFDDevice */
+
     /* interrupt remapping */
     bool intr_enabled;              /* Whether guest enabled IR */
     dma_addr_t intr_root;           /* Interrupt remapping table pointer */
hw/i386/intel_iommu.c +0 -0
index ed5677c0ae..95faf697eb 100644
@@ -237,6 +237,13 @@ static gboolean vtd_as_equal(gconstpointer v1,

gconstpointer v2)

hw/i386/intel_iommu.c VTDAddressSpace

Quoted 14 lines
hw/i386/intel_iommu.c +7 -0
            (key1->pasid == key2->pasid);
 }
 
+static gboolean vtd_as_idev_equal(gconstpointer v1, gconstpointer v2)
+{
+    const struct vtd_as_key *key1 = v1;
+    const struct vtd_as_key *key2 = v2;
+
+    return (key1->bus == key2->bus) && (key1->devfn == key2->devfn);
+}
 /*
  * Note that we use pointer to PCIBus as the key, so hashing/shifting
  * based on the pointer value is intended. Note that we deal with
@@ -3812,6 +3819,74 @@ VTDAddressSpace

*vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
     return vtd_dev_as;
 }
 
+static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque,

int32_t devfn,

Quoted 34 lines
hw/i386/intel_iommu.c +34 -0
+                                    IOMMUFDDevice *idev, Error **errp)
+{
+    IntelIOMMUState *s = opaque;
+    VTDIOMMUFDDevice *vtd_idev;
+    struct vtd_as_key key = {
+        .bus = bus,
+        .devfn = devfn,
+    };
+    struct vtd_as_key *new_key;
+
+    assert(0 <= devfn && devfn < PCI_DEVFN_MAX);
+
+    /* None IOMMUFD case */
+    if (!idev) {
+        return 0;
+    }
+
+    vtd_iommu_lock(s);
+
+    vtd_idev = g_hash_table_lookup(s->vtd_iommufd_dev, &key);
+
+    if (vtd_idev) {
+        error_setg(errp, "IOMMUFD device already exist");
+        return -1;
+    }
+
+    new_key = g_malloc(sizeof(*new_key));
+    new_key->bus = bus;
+    new_key->devfn = devfn;
+
+    vtd_idev = g_malloc0(sizeof(VTDIOMMUFDDevice));
+    vtd_idev->bus = bus;
+    vtd_idev->devfn = (uint8_t)devfn;
+    vtd_idev->iommu_state = s;

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    vtd_idev->idev = idev;
+
+    g_hash_table_insert(s->vtd_iommufd_dev, new_key, vtd_idev);
+
+    vtd_iommu_unlock(s);
+
+    return 0;
+}
+
+static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque,

int32_t devfn)

Quoted 15 lines
hw/i386/intel_iommu.c +15 -0
+{
+    IntelIOMMUState *s = opaque;
+    VTDIOMMUFDDevice *vtd_idev;
+    struct vtd_as_key key = {
+        .bus = bus,
+        .devfn = devfn,
+    };
+
+    assert(0 <= devfn && devfn < PCI_DEVFN_MAX);
+
+    vtd_iommu_lock(s);
+
+    vtd_idev = g_hash_table_lookup(s->vtd_iommufd_dev, &key);
+    if (!vtd_idev) {
+        vtd_iommu_unlock(s);

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 10 lines
hw/i386/intel_iommu.c +8 -0
+        return;
+    }
+
+    g_hash_table_remove(s->vtd_iommufd_dev, &key);
+
+    vtd_iommu_unlock(s);
+}
+
 /* Unmap the whole range in the notifier's scope. */
 static void vtd_address_space_unmap(VTDAddressSpace *as,

IOMMUNotifier *n)

hw/i386/intel_iommu.c static AddressSpace

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
 {
@@ -4107,6 +4182,8 @@ static AddressSpace

*vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)

hw/i386/intel_iommu.c static void vtd_realize(DeviceState *dev, Error

Quoted 9 lines
hw/i386/intel_iommu.c +2 -0
 static PCIIOMMUOps vtd_iommu_ops = {
     .get_address_space = vtd_host_dma_iommu,
+    .set_iommu_device = vtd_dev_set_iommu_device,
+    .unset_iommu_device = vtd_dev_unset_iommu_device,
 };
 
 static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
@@ -4230,6 +4307,8 @@ static void vtd_realize(DeviceState *dev, Error

**errp)

hw/i386/intel_iommu.c static void vtd_realize(DeviceState *dev, Error · line 4308

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
                                      g_free, g_free);
     s->vtd_address_spaces = g_hash_table_new_full(vtd_as_hash,

vtd_as_equal,

hw/i386/intel_iommu.c static void vtd_realize(DeviceState *dev, Error · line 4310

Quoted 2 lines
hw/i386/intel_iommu.c +1 -0
                                       g_free, g_free);
+    s->vtd_iommufd_dev = g_hash_table_new_full(vtd_as_hash,

vtd_as_idev_equal,

hw/i386/intel_iommu.c static void vtd_realize(DeviceState *dev, Error **errp) · line 4314

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
+                                               g_free, g_free);
     vtd_init(s);
     pci_setup_iommu(bus, &vtd_iommu_ops, dev);
     /* Pseudo address space under root PCI bus. */

Thanks

Eric

#21 lore raw

Re: [PATCH rfcv1 3/6] intel_iommu: add set/unset_iommu_device callback

Quoted 51 lines · Zhenzhong
Quoted 41 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 3/6] intel_iommu: add set/unset_iommu_device callback

Hi Zhenzhong,

Quoted 6 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds set/unset_iommu_device() implementation in Intel vIOMMU. In set call, IOMMUFDDevice is recorded in hash table indexed by PCI BDF.

Quoted 6 lines
---
 include/hw/i386/intel_iommu.h | 10 +++++
 hw/i386/intel_iommu.c         | 79

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

Review

Quoted 3 lines
 2 files changed, 89 insertions(+)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 15 lines

index 7fa0a695c8..c65fdde56f 100644

include/hw/i386/intel_iommu.h +1 -0
@@ -62,6 +62,7 @@ typedef union VTD_IR_TableEntry VTD_IR_TableEntry;
 typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress;
 typedef struct VTDPASIDDirEntry VTDPASIDDirEntry;
 typedef struct VTDPASIDEntry VTDPASIDEntry;
+typedef struct VTDIOMMUFDDevice VTDIOMMUFDDevice;
 
 /* Context-Entry */
 struct VTDContextEntry {
@@ -148,6 +149,13 @@ struct VTDAddressSpace {
     IOVATree *iova_tree;
 };
 
Quoted 10 lines

include/hw/i386/intel_iommu.h struct VTDAddressSpace { · line 158

Quoted 7 lines
include/hw/i386/intel_iommu.h +7 -0
+struct VTDIOMMUFDDevice {
+    PCIBus *bus;
+    uint8_t devfn;
+    IOMMUFDDevice *idev;
+    IntelIOMMUState *iommu_state;
+};
+

Just wondering whether we shouldn't reuse the VTDAddressSpace to store the idev, if any. How have you made your choice. What will it become when PASID gets added?

Quoted 4 lines

VTDAddressSpace is indexed by aliased BDF, but VTDIOMMUFDDevice is indexed by device's BDF. So we can't just store VTDIOMMUFDDevice as a pointer in VTDAddressSpace, may need a list in case more than one device in same address space. Then a global VTDIOMMUFDDevice list is better for lookup.

OK but if several devices are hidden under an aliased BDF, can't they share the host properties (DMAR ecap/cap)?

Quoted 4 lines

For PASID in modern mode which support stage-1 page table, we have VTDPASIDAddressSpace indexed by device's BDF+PASID, We didn't use VTDAddressSpace which is for stage-2 page table.

OK

Thanks

Eric

Quoted 138 lines

Thanks
Zhenzhong

Quoted 134 lines

hw/i386/intel_iommu.c static gboolean vtd_as_equal(gconstpointer v1,

Quoted 17 lines
include/hw/i386/intel_iommu.h +2 -0
 struct VTDIOTLBEntry {
     uint64_t gfn;
     uint16_t domain_id;
@@ -292,6 +300,8 @@ struct IntelIOMMUState {
     /* list of registered notifiers */
     QLIST_HEAD(, VTDAddressSpace) vtd_as_with_notifiers;
 
+    GHashTable *vtd_iommufd_dev;             /* VTDIOMMUFDDevice */
+
     /* interrupt remapping */
     bool intr_enabled;              /* Whether guest enabled IR */
     dma_addr_t intr_root;           /* Interrupt remapping table pointer */
hw/i386/intel_iommu.c +0 -0
index ed5677c0ae..95faf697eb 100644
@@ -237,6 +237,13 @@ static gboolean vtd_as_equal(gconstpointer v1,

gconstpointer v2)

hw/i386/intel_iommu.c VTDAddressSpace

Quoted 14 lines
hw/i386/intel_iommu.c +7 -0
            (key1->pasid == key2->pasid);
 }
 
+static gboolean vtd_as_idev_equal(gconstpointer v1, gconstpointer v2)
+{
+    const struct vtd_as_key *key1 = v1;
+    const struct vtd_as_key *key2 = v2;
+
+    return (key1->bus == key2->bus) && (key1->devfn == key2->devfn);
+}
 /*
  * Note that we use pointer to PCIBus as the key, so hashing/shifting
  * based on the pointer value is intended. Note that we deal with
@@ -3812,6 +3819,74 @@ VTDAddressSpace

*vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
     return vtd_dev_as;
 }
 
+static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque,

int32_t devfn,

Quoted 34 lines
hw/i386/intel_iommu.c +34 -0
+                                    IOMMUFDDevice *idev, Error **errp)
+{
+    IntelIOMMUState *s = opaque;
+    VTDIOMMUFDDevice *vtd_idev;
+    struct vtd_as_key key = {
+        .bus = bus,
+        .devfn = devfn,
+    };
+    struct vtd_as_key *new_key;
+
+    assert(0 <= devfn && devfn < PCI_DEVFN_MAX);
+
+    /* None IOMMUFD case */
+    if (!idev) {
+        return 0;
+    }
+
+    vtd_iommu_lock(s);
+
+    vtd_idev = g_hash_table_lookup(s->vtd_iommufd_dev, &key);
+
+    if (vtd_idev) {
+        error_setg(errp, "IOMMUFD device already exist");
+        return -1;
+    }
+
+    new_key = g_malloc(sizeof(*new_key));
+    new_key->bus = bus;
+    new_key->devfn = devfn;
+
+    vtd_idev = g_malloc0(sizeof(VTDIOMMUFDDevice));
+    vtd_idev->bus = bus;
+    vtd_idev->devfn = (uint8_t)devfn;
+    vtd_idev->iommu_state = s;

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+    vtd_idev->idev = idev;
+
+    g_hash_table_insert(s->vtd_iommufd_dev, new_key, vtd_idev);
+
+    vtd_iommu_unlock(s);
+
+    return 0;
+}
+
+static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque,

int32_t devfn)

Quoted 15 lines
hw/i386/intel_iommu.c +15 -0
+{
+    IntelIOMMUState *s = opaque;
+    VTDIOMMUFDDevice *vtd_idev;
+    struct vtd_as_key key = {
+        .bus = bus,
+        .devfn = devfn,
+    };
+
+    assert(0 <= devfn && devfn < PCI_DEVFN_MAX);
+
+    vtd_iommu_lock(s);
+
+    vtd_idev = g_hash_table_lookup(s->vtd_iommufd_dev, &key);
+    if (!vtd_idev) {
+        vtd_iommu_unlock(s);

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 10 lines
hw/i386/intel_iommu.c +8 -0
+        return;
+    }
+
+    g_hash_table_remove(s->vtd_iommufd_dev, &key);
+
+    vtd_iommu_unlock(s);
+}
+
 /* Unmap the whole range in the notifier's scope. */
 static void vtd_address_space_unmap(VTDAddressSpace *as,

IOMMUNotifier *n)

hw/i386/intel_iommu.c static AddressSpace

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
 {
@@ -4107,6 +4182,8 @@ static AddressSpace

*vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)

hw/i386/intel_iommu.c static void vtd_realize(DeviceState *dev, Error

Quoted 8 lines
hw/i386/intel_iommu.c +2 -0
 static PCIIOMMUOps vtd_iommu_ops = {
     .get_address_space = vtd_host_dma_iommu,
+    .set_iommu_device = vtd_dev_set_iommu_device,
+    .unset_iommu_device = vtd_dev_unset_iommu_device,
 };
 
 static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
@@ -4230,6 +4307,8 @@ static void vtd_realize(DeviceState *dev, Error

**errp)

hw/i386/intel_iommu.c static void vtd_realize(DeviceState *dev, Error · line 4308

Quoted 2 lines
hw/i386/intel_iommu.c +0 -0
                                      g_free, g_free);
     s->vtd_address_spaces = g_hash_table_new_full(vtd_as_hash,

vtd_as_equal,

hw/i386/intel_iommu.c static void vtd_realize(DeviceState *dev, Error · line 4310

Quoted 2 lines
hw/i386/intel_iommu.c +1 -0
                                       g_free, g_free);
+    s->vtd_iommufd_dev = g_hash_table_new_full(vtd_as_hash,

vtd_as_idev_equal,

hw/i386/intel_iommu.c static void vtd_realize(DeviceState *dev, Error **errp) · line 4314

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
+                                               g_free, g_free);
     vtd_init(s);
     pci_setup_iommu(bus, &vtd_iommu_ops, dev);
     /* Pseudo address space under root PCI bus. */

Thanks

Eric

#22 lore raw

RE: [PATCH rfcv1 3/6] intel_iommu: add set/unset_iommu_device callback

Quoted 65 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 3/6] intel_iommu: add set/unset_iommu_device callback

Quoted 4 lines · Zhenzhong
Quoted 4 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 3/6] intel_iommu: add

set/unset_iommu_device

Quoted 18 lines
Quoted 18 lines

callback

Hi Zhenzhong,

Quoted 6 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds set/unset_iommu_device() implementation in Intel vIOMMU. In set call, IOMMUFDDevice is recorded in hash table indexed by PCI BDF.

Quoted 6 lines
---
 include/hw/i386/intel_iommu.h | 10 +++++
 hw/i386/intel_iommu.c         | 79

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

Quoted 8 lines
Quoted 8 lines

Review

Quoted 3 lines
 2 files changed, 89 insertions(+)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

include/hw/i386/intel_iommu.h typedef union VTD_IR_TableEntry

Quoted 4 lines

index 7fa0a695c8..c65fdde56f 100644

include/hw/i386/intel_iommu.h +0 -0
@@ -62,6 +62,7 @@ typedef union VTD_IR_TableEntry

VTD_IR_TableEntry;

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
include/hw/i386/intel_iommu.h +1 -0
 typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress;
 typedef struct VTDPASIDDirEntry VTDPASIDDirEntry;
 typedef struct VTDPASIDEntry VTDPASIDEntry;
+typedef struct VTDIOMMUFDDevice VTDIOMMUFDDevice;
 
 /* Context-Entry */
 struct VTDContextEntry {
@@ -148,6 +149,13 @@ struct VTDAddressSpace {
     IOVATree *iova_tree;
 };
 
Quoted 8 lines
Quoted 8 lines

include/hw/i386/intel_iommu.h struct VTDAddressSpace { · line 158

Quoted 7 lines
include/hw/i386/intel_iommu.h +7 -0
+struct VTDIOMMUFDDevice {
+    PCIBus *bus;
+    uint8_t devfn;
+    IOMMUFDDevice *idev;
+    IntelIOMMUState *iommu_state;
+};
+

Just wondering whether we shouldn't reuse the VTDAddressSpace to

store

Quoted 3 lines
Quoted 2 lines

the idev, if any. How have you made your choice. What will it become when PASID gets added?

VTDAddressSpace is indexed by aliased BDF, but VTDIOMMUFDDevice is

Quoted 8 lines

indexed

Quoted 2 lines

by device's BDF. So we can't just store VTDIOMMUFDDevice as a pointer in VTDAddressSpace, may need a list in case more than one device in same

address

Quoted 1 line

space. Then a global VTDIOMMUFDDevice list is better for lookup.

OK but if several devices are hidden under an aliased BDF, can't they share the host properties (DMAR ecap/cap)?

It depends on that if the vfio devices are under same aliased BDF in host. If vfio devices are configured under same aliased BDF in qemu but they are not in same aliased BDF in host, their host cap/ecap may be different.

Thanks
Zhenzhong

#23 lore raw

Re: [PATCH rfcv1 3/6] intel_iommu: add set/unset_iommu_device callback

In reply to Zhenzhong Duan
Quoted 27 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds set/unset_iommu_device() implementation in Intel vIOMMU. In set call, IOMMUFDDevice is recorded in hash table indexed by PCI BDF.

---
  include/hw/i386/intel_iommu.h | 10 +++++
  hw/i386/intel_iommu.c         | 79 +++++++++++++++++++++++++++++++++++
  2 files changed, 89 insertions(+)
include/hw/i386/intel_iommu.h +1 -0
index 7fa0a695c8..c65fdde56f 100644
@@ -62,6 +62,7 @@ typedef union VTD_IR_TableEntry VTD_IR_TableEntry;
  typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress;
  typedef struct VTDPASIDDirEntry VTDPASIDDirEntry;
  typedef struct VTDPASIDEntry VTDPASIDEntry;
+typedef struct VTDIOMMUFDDevice VTDIOMMUFDDevice;
 
  /* Context-Entry */
  struct VTDContextEntry {

include/hw/i386/intel_iommu.h struct VTDAddressSpace { · line 157

Quoted 9 lines
include/hw/i386/intel_iommu.h +6 -0
@@ -148,6 +149,13 @@ struct VTDAddressSpace {
      IOVATree *iova_tree;
  };
 
+struct VTDIOMMUFDDevice {
+    PCIBus *bus;
+    uint8_t devfn;
+    IOMMUFDDevice *idev;
+    IntelIOMMUState *iommu_state;
+};

Does the VTDIOMMUFDDevice definition need to be public ?

Quoted 36 lines
include/hw/i386/intel_iommu.h +2 -0
  struct VTDIOTLBEntry {
      uint64_t gfn;
      uint16_t domain_id;
@@ -292,6 +300,8 @@ struct IntelIOMMUState {
      /* list of registered notifiers */
      QLIST_HEAD(, VTDAddressSpace) vtd_as_with_notifiers;
 
+    GHashTable *vtd_iommufd_dev;             /* VTDIOMMUFDDevice */
+
      /* interrupt remapping */
      bool intr_enabled;              /* Whether guest enabled IR */
      dma_addr_t intr_root;           /* Interrupt remapping table pointer */
hw/i386/intel_iommu.c +9 -0
index ed5677c0ae..95faf697eb 100644
@@ -237,6 +237,13 @@ static gboolean vtd_as_equal(gconstpointer v1, gconstpointer v2)
             (key1->pasid == key2->pasid);
  }
 
+static gboolean vtd_as_idev_equal(gconstpointer v1, gconstpointer v2)
+{
+    const struct vtd_as_key *key1 = v1;
+    const struct vtd_as_key *key2 = v2;
+
+    return (key1->bus == key2->bus) && (key1->devfn == key2->devfn);
+}
  /*
   * Note that we use pointer to PCIBus as the key, so hashing/shifting
   * based on the pointer value is intended. Note that we deal with
@@ -3812,6 +3819,74 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
      return vtd_dev_as;
  }
 
+static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int32_t devfn,
+                                    IOMMUFDDevice *idev, Error **errp)

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, · line 3833

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+{
+    IntelIOMMUState *s = opaque;
+    VTDIOMMUFDDevice *vtd_idev;
+    struct vtd_as_key key = {
+        .bus = bus,
+        .devfn = devfn,
+    };
+    struct vtd_as_key *new_key;
+
+    assert(0 <= devfn && devfn < PCI_DEVFN_MAX);

Can we move the assert earlier in the call stack ? pci_device_get_iommu_bus_devfn() looks like a good place.

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, · line 3838

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+
+    /* None IOMMUFD case */
+    if (!idev) {
+        return 0;
+    }

Can we move this test in the helper ? (Looks like an error to me).

Thanks,

C.

Quoted 72 lines
hw/i386/intel_iommu.c +55 -0
+
+    vtd_iommu_lock(s);
+
+    vtd_idev = g_hash_table_lookup(s->vtd_iommufd_dev, &key);
+
+    if (vtd_idev) {
+        error_setg(errp, "IOMMUFD device already exist");
+        return -1;
+    }
+
+    new_key = g_malloc(sizeof(*new_key));
+    new_key->bus = bus;
+    new_key->devfn = devfn;
+
+    vtd_idev = g_malloc0(sizeof(VTDIOMMUFDDevice));
+    vtd_idev->bus = bus;
+    vtd_idev->devfn = (uint8_t)devfn;
+    vtd_idev->iommu_state = s;
+    vtd_idev->idev = idev;
+
+    g_hash_table_insert(s->vtd_iommufd_dev, new_key, vtd_idev);
+
+    vtd_iommu_unlock(s);
+
+    return 0;
+}
+
+static void vtd_dev_unset_iommu_device(PCIBus *bus, void *opaque, int32_t devfn)
+{
+    IntelIOMMUState *s = opaque;
+    VTDIOMMUFDDevice *vtd_idev;
+    struct vtd_as_key key = {
+        .bus = bus,
+        .devfn = devfn,
+    };
+
+    assert(0 <= devfn && devfn < PCI_DEVFN_MAX);
+
+    vtd_iommu_lock(s);
+
+    vtd_idev = g_hash_table_lookup(s->vtd_iommufd_dev, &key);
+    if (!vtd_idev) {
+        vtd_iommu_unlock(s);
+        return;
+    }
+
+    g_hash_table_remove(s->vtd_iommufd_dev, &key);
+
+    vtd_iommu_unlock(s);
+}
+
  /* Unmap the whole range in the notifier's scope. */
  static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
  {
@@ -4107,6 +4182,8 @@ static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)
 
  static PCIIOMMUOps vtd_iommu_ops = {
      .get_address_space = vtd_host_dma_iommu,
+    .set_iommu_device = vtd_dev_set_iommu_device,
+    .unset_iommu_device = vtd_dev_unset_iommu_device,
  };
 
  static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
@@ -4230,6 +4307,8 @@ static void vtd_realize(DeviceState *dev, Error **errp)
                                       g_free, g_free);
      s->vtd_address_spaces = g_hash_table_new_full(vtd_as_hash, vtd_as_equal,
                                        g_free, g_free);
+    s->vtd_iommufd_dev = g_hash_table_new_full(vtd_as_hash, vtd_as_idev_equal,
+                                               g_free, g_free);
      vtd_init(s);
      pci_setup_iommu(bus, &vtd_iommu_ops, dev);
      /* Pseudo address space under root PCI bus. */
#24 lore raw

RE: [PATCH rfcv1 3/6] intel_iommu: add set/unset_iommu_device callback

Quoted 40 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv1 3/6] intel_iommu: add set/unset_iommu_device callback

Quoted 6 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

This adds set/unset_iommu_device() implementation in Intel vIOMMU. In set call, IOMMUFDDevice is recorded in hash table indexed by PCI BDF.

Quoted 6 lines
---
  include/hw/i386/intel_iommu.h | 10 +++++
  hw/i386/intel_iommu.c         | 79

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

Review

Quoted 3 lines
  2 files changed, 89 insertions(+)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 15 lines

index 7fa0a695c8..c65fdde56f 100644

include/hw/i386/intel_iommu.h +1 -0
@@ -62,6 +62,7 @@ typedef union VTD_IR_TableEntry VTD_IR_TableEntry;
  typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress;
  typedef struct VTDPASIDDirEntry VTDPASIDDirEntry;
  typedef struct VTDPASIDEntry VTDPASIDEntry;
+typedef struct VTDIOMMUFDDevice VTDIOMMUFDDevice;
 
  /* Context-Entry */
  struct VTDContextEntry {
@@ -148,6 +149,13 @@ struct VTDAddressSpace {
      IOVATree *iova_tree;
  };
 
Quoted 8 lines

include/hw/i386/intel_iommu.h struct VTDAddressSpace { · line 157

Quoted 6 lines
include/hw/i386/intel_iommu.h +6 -0
+struct VTDIOMMUFDDevice {
+    PCIBus *bus;
+    uint8_t devfn;
+    IOMMUFDDevice *idev;
+    IntelIOMMUState *iommu_state;
+};

Does the VTDIOMMUFDDevice definition need to be public ?

No need, will move it to hw/i386/intel_iommu_internal.h
It looks I need to do the same for other definitions in nesting series.

Quoted 39 lines

hw/i386/intel_iommu.c static gboolean vtd_as_equal(gconstpointer v1,

Quoted 18 lines
include/hw/i386/intel_iommu.h +2 -0
  struct VTDIOTLBEntry {
      uint64_t gfn;
      uint16_t domain_id;
@@ -292,6 +300,8 @@ struct IntelIOMMUState {
      /* list of registered notifiers */
      QLIST_HEAD(, VTDAddressSpace) vtd_as_with_notifiers;
 
+    GHashTable *vtd_iommufd_dev;             /* VTDIOMMUFDDevice */
+
      /* interrupt remapping */
      bool intr_enabled;              /* Whether guest enabled IR */
      dma_addr_t intr_root;           /* Interrupt remapping table pointer */
hw/i386/intel_iommu.c +0 -0
index ed5677c0ae..95faf697eb 100644
@@ -237,6 +237,13 @@ static gboolean vtd_as_equal(gconstpointer v1,

gconstpointer v2)

hw/i386/intel_iommu.c VTDAddressSpace

Quoted 14 lines
hw/i386/intel_iommu.c +7 -0
             (key1->pasid == key2->pasid);
  }
 
+static gboolean vtd_as_idev_equal(gconstpointer v1, gconstpointer v2)
+{
+    const struct vtd_as_key *key1 = v1;
+    const struct vtd_as_key *key2 = v2;
+
+    return (key1->bus == key2->bus) && (key1->devfn == key2->devfn);
+}
  /*
   * Note that we use pointer to PCIBus as the key, so hashing/shifting
   * based on the pointer value is intended. Note that we deal with
@@ -3812,6 +3819,74 @@ VTDAddressSpace

*vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
      return vtd_dev_as;
  }
 
+static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque,

int32_t devfn,

Quoted 14 lines

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, · line 3833

Quoted 11 lines
hw/i386/intel_iommu.c +11 -0
+                                    IOMMUFDDevice *idev, Error **errp)
+{
+    IntelIOMMUState *s = opaque;
+    VTDIOMMUFDDevice *vtd_idev;
+    struct vtd_as_key key = {
+        .bus = bus,
+        .devfn = devfn,
+    };
+    struct vtd_as_key *new_key;
+
+    assert(0 <= devfn && devfn < PCI_DEVFN_MAX);

Can we move the assert earlier in the call stack ? pci_device_get_iommu_bus_devfn() looks like a good place.

Sure.

Quoted 8 lines

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, · line 3838

Quoted 6 lines
hw/i386/intel_iommu.c +5 -0
+
+    /* None IOMMUFD case */
+    if (!idev) {
+        return 0;
+    }

Can we move this test in the helper ? (Looks like an error to me).

We need to pass in NULL idev to do further check in nesting series.
See https://github.com/yiliu1765/qemu/commit/7f0bb59575bb5cf38618ae950f68a8661676e881

Thanks
Zhenzhong

#25 lore raw

RFC PATCH 4/6 vfio: initialize IOMMUFDDevice and pass to vIOMMU

In reply to Zhenzhong Duan

Initialize IOMMUFDDevice in vfio and pass to vIOMMU, so that vIOMMU could get hw IOMMU information.

In VFIO legacy backend mode, we still pass a NULL IOMMUFDDevice to vIOMMU, in case vIOMMU needs some processing for VFIO legacy backend mode.

---
 include/hw/vfio/vfio-common.h |  2 ++
 hw/vfio/iommufd.c             |  2 ++
 hw/vfio/pci.c                 | 24 +++++++++++++++++++-----
 3 files changed, 23 insertions(+), 5 deletions(-)
include/hw/vfio/vfio-common.h +2 -0
index 9b7ef7d02b..fde0d0ca60 100644
@@ -31,6 +31,7 @@
 #endif
 #include "sysemu/sysemu.h"
 #include "hw/vfio/vfio-container-base.h"
+#include "sysemu/iommufd_device.h"
 
 #define VFIO_MSG_PREFIX "vfio %s: "
 
@@ -126,6 +127,7 @@ typedef struct VFIODevice {
     bool dirty_tracking;
     int devid;
     IOMMUFDBackend *iommufd;
+    IOMMUFDDevice idev;
 } VFIODevice;
 
 struct VFIODeviceOps {
hw/vfio/iommufd.c +2 -0
index 9bfddc1360..cbd035f148 100644
@@ -309,6 +309,7 @@ static int iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
     VFIOContainerBase *bcontainer;
     VFIOIOMMUFDContainer *container;
     VFIOAddressSpace *space;
+    IOMMUFDDevice *idev = &vbasedev->idev;
     struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
     int ret, devfd;
     uint32_t ioas_id;
@@ -428,6 +429,7 @@ found_container:
     QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
     QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
 
+    iommufd_device_init(idev, sizeof(*idev), container->be, vbasedev->devid);
     trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs,
                                    vbasedev->num_regions, vbasedev->flags);
     return 0;
hw/vfio/pci.c +19 -5
index d7fe06715c..2c3a5d267b 100644
@@ -3107,11 +3107,21 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
 
     vfio_bars_register(vdev);
 
-    ret = vfio_add_capabilities(vdev, errp);
+    if (vbasedev->iommufd) {
+        ret = pci_device_set_iommu_device(pdev, &vbasedev->idev, errp);
+    } else {
+        ret = pci_device_set_iommu_device(pdev, 0, 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);
     }
@@ -3128,7 +3138,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
             error_setg(errp,
                        "cannot support IGD OpRegion feature on hotplugged "
                        "device");
-            goto out_teardown;
+            goto out_unset_idev;
         }
 
         ret = vfio_get_dev_region_info(vbasedev,
@@ -3137,13 +3147,13 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
         if (ret) {
             error_setg_errno(errp, -ret,
                              "does not support requested IGD OpRegion feature");
-            goto out_teardown;
+            goto out_unset_idev;
         }
 
         ret = vfio_pci_igd_opregion_init(vdev, opregion, errp);
         g_free(opregion);
         if (ret) {
-            goto out_teardown;
+            goto out_unset_idev;
         }
     }
 
@@ -3229,6 +3239,8 @@ out_deregister:
     if (vdev->intx.mmap_timer) {
         timer_free(vdev->intx.mmap_timer);
     }
+out_unset_idev:
+    pci_device_unset_iommu_device(pdev);
 out_teardown:
     vfio_teardown_msi(vdev);
     vfio_bars_exit(vdev);
@@ -3257,6 +3269,7 @@ static void vfio_instance_finalize(Object *obj)
 static void vfio_exitfn(PCIDevice *pdev)
 {
     VFIOPCIDevice *vdev = VFIO_PCI(pdev);
+    VFIODevice *vbasedev = &vdev->vbasedev;
 
     vfio_unregister_req_notifier(vdev);
     vfio_unregister_err_notifier(vdev);
@@ -3271,7 +3284,8 @@ static void vfio_exitfn(PCIDevice *pdev)
     vfio_teardown_msi(vdev);
     vfio_pci_disable_rp_atomics(vdev);
     vfio_bars_exit(vdev);
-    vfio_migration_exit(&vdev->vbasedev);
+    vfio_migration_exit(vbasedev);
+    pci_device_unset_iommu_device(pdev);
 }
 
 static void vfio_pci_reset(DeviceState *dev)
Signature · 1 line
2.34.1
#26 lore raw

Re: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

Quoted 13 lines · Zhenzhong Duan
hw/vfio/iommufd.c +1 -0
index 9bfddc1360..cbd035f148 100644
@@ -309,6 +309,7 @@ static int iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
     VFIOContainerBase *bcontainer;
     VFIOIOMMUFDContainer *container;
     VFIOAddressSpace *space;
+    IOMMUFDDevice *idev = &vbasedev->idev;
     struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
     int ret, devfd;
     uint32_t ioas_id;

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

Quoted 7 lines
hw/vfio/iommufd.c +1 -0
@@ -428,6 +429,7 @@ found_container:
     QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
     QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
 
+    iommufd_device_init(idev, sizeof(*idev), container->be, vbasedev->devid);
     trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs,
                                    vbasedev->num_regions, vbasedev->flags);
     return 0;

In the dirty tracking series, I'll need to fetch out_capabilities from device and do a bunch of stuff that is used when allocating hwpt to ask for dirty tracking. And this means having iommufd_device_init() be called before we call iommufd_cdev_attach_container().

Here's what it looks based on an earlier version of your patch:

https://github.com/jpemartins/qemu/commit/433f97a05e0cdd8e3b8563aa20e4f22d107219b5

I can move the call earlier in my series, unless there's something specifically when you call it here?

Joao

#27 lore raw

RE: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

Quoted 39 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

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

Quoted 5 lines · Zhenzhong Duan
hw/vfio/iommufd.c +0 -0
index 9bfddc1360..cbd035f148 100644
@@ -309,6 +309,7 @@ static int iommufd_cdev_attach(const char *name,

VFIODevice *vbasedev,

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

Quoted 9 lines
hw/vfio/iommufd.c +1 -0
     VFIOContainerBase *bcontainer;
     VFIOIOMMUFDContainer *container;
     VFIOAddressSpace *space;
+    IOMMUFDDevice *idev = &vbasedev->idev;
     struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
     int ret, devfd;
     uint32_t ioas_id;
@@ -428,6 +429,7 @@ found_container:
     QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev,

container_next);

Quoted 8 lines
hw/vfio/iommufd.c +1 -0
     QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
 
+    iommufd_device_init(idev, sizeof(*idev), container->be, vbasedev-
 devid);
     trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev-
 num_irqs,
                                    vbasedev->num_regions, vbasedev->flags);
     return 0;

In the dirty tracking series, I'll need to fetch out_capabilities from device and do a bunch of stuff that is used when allocating hwpt to ask for dirty tracking. And this means having iommufd_device_init() be called before we call
iommufd_cdev_attach_container().

Quoted 7 lines

Here's what it looks based on an earlier version of your patch:

https://github.com/jpemartins/qemu/commit/433f97a05e0cdd8e3b8563a
a20e4f22d107219b5

I can move the call earlier in my series, unless there's something specifically when you call it here?

I think it's safe to move it earlier, just remember to do the same for existing container.

Thanks
Zhenzhong

#28 lore raw

Re: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

Quoted 41 lines · Zhenzhong
Quoted 41 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

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

Quoted 5 lines · Zhenzhong Duan
hw/vfio/iommufd.c +0 -0
index 9bfddc1360..cbd035f148 100644
@@ -309,6 +309,7 @@ static int iommufd_cdev_attach(const char *name,

VFIODevice *vbasedev,

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

Quoted 9 lines
hw/vfio/iommufd.c +1 -0
      VFIOContainerBase *bcontainer;
      VFIOIOMMUFDContainer *container;
      VFIOAddressSpace *space;
+    IOMMUFDDevice *idev = &vbasedev->idev;
      struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
      int ret, devfd;
      uint32_t ioas_id;
@@ -428,6 +429,7 @@ found_container:
      QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev,

container_next);

Quoted 8 lines
hw/vfio/iommufd.c +1 -0
      QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
 
+    iommufd_device_init(idev, sizeof(*idev), container->be, vbasedev-
 devid);
      trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev-
 num_irqs,
                                     vbasedev->num_regions, vbasedev->flags);
      return 0;

In the dirty tracking series, I'll need to fetch out_capabilities from device and do a bunch of stuff that is used when allocating hwpt to ask for dirty tracking. And this means having iommufd_device_init() be called before we call
iommufd_cdev_attach_container().

Here's what it looks based on an earlier version of your patch:

Quoted 8 lines
Quoted 5 lines

https://github.com/jpemartins/qemu/commit/433f97a05e0cdd8e3b8563a
a20e4f22d107219b5

I can move the call earlier in my series, unless there's something specifically when you call it here?

I think it's safe to move it earlier, just remember to do the same for existing container.

yes, as long as the input of iommufd_device_init() are available in the new place. And remember to destroy it if the code failed after initializing iommufd_device.

Signature · 2 lines
Regards,
Yi Liu
#29 lore raw

Re: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

Quoted 48 lines · Yi Liu
Quoted 47 lines · Zhenzhong
Quoted 46 lines

-----Original Message-----
From: Joao Martins <[email protected]>
Subject: Re: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

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

Quoted 5 lines · Zhenzhong Duan
hw/vfio/iommufd.c +0 -0
index 9bfddc1360..cbd035f148 100644
@@ -309,6 +309,7 @@ static int iommufd_cdev_attach(const char *name,

VFIODevice *vbasedev,

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

Quoted 9 lines
hw/vfio/iommufd.c +1 -0
       VFIOContainerBase *bcontainer;
       VFIOIOMMUFDContainer *container;
       VFIOAddressSpace *space;
+    IOMMUFDDevice *idev = &vbasedev->idev;
       struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
       int ret, devfd;
       uint32_t ioas_id;
@@ -428,6 +429,7 @@ found_container:
       QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev,

container_next);

Quoted 8 lines
hw/vfio/iommufd.c +1 -0
       QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
 
+    iommufd_device_init(idev, sizeof(*idev), container->be, vbasedev-
 devid);

      trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev- num_irqs,
                                     vbasedev->num_regions, vbasedev->flags); return 0;

In the dirty tracking series, I'll need to fetch out_capabilities from device and do a bunch of stuff that is used when allocating hwpt to ask for dirty tracking. And this means having iommufd_device_init() be called before we call
iommufd_cdev_attach_container().

Here's what it looks based on an earlier version of your patch:

https://github.com/jpemartins/qemu/commit/433f97a05e0cdd8e3b8563a
a20e4f22d107219b5

I can move the call earlier in my series, unless there's something specifically when you call it here?

Quoted 7 lines
Quoted 2 lines

I think it's safe to move it earlier, just remember to do the same for existing container.

yes, as long as the input of iommufd_device_init() are available in the new place. And remember to destroy it if the code failed after initializing iommufd_device.

In the way I am using I don't think there's any teardown as no new resources or things are done. We essentially just initialize @idev and fetch some capabilities thus nothing needs teardown. But I am not sure is the nesting needs; perhaps destruction of resources is related to something to that?

#30 lore raw

Re: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

In reply to Zhenzhong Duan

Hi Zhenzhong,

Quoted 29 lines · Zhenzhong Duan

Initialize IOMMUFDDevice in vfio and pass to vIOMMU, so that vIOMMU could get hw IOMMU information.

In VFIO legacy backend mode, we still pass a NULL IOMMUFDDevice to vIOMMU, in case vIOMMU needs some processing for VFIO legacy backend mode.

---
 include/hw/vfio/vfio-common.h |  2 ++
 hw/vfio/iommufd.c             |  2 ++
 hw/vfio/pci.c                 | 24 +++++++++++++++++++-----
 3 files changed, 23 insertions(+), 5 deletions(-)
include/hw/vfio/vfio-common.h +1 -0
index 9b7ef7d02b..fde0d0ca60 100644
@@ -31,6 +31,7 @@
 #endif
 #include "sysemu/sysemu.h"
 #include "hw/vfio/vfio-container-base.h"
+#include "sysemu/iommufd_device.h"
 
 #define VFIO_MSG_PREFIX "vfio %s: "
 

include/hw/vfio/vfio-common.h typedef struct VFIODevice { · line 130

Quoted 4 lines
include/hw/vfio/vfio-common.h +1 -0
@@ -126,6 +127,7 @@ typedef struct VFIODevice {
     bool dirty_tracking;
     int devid;
     IOMMUFDBackend *iommufd;
+    IOMMUFDDevice idev;

This looks duplicate of existing fields:
idev.dev_id is same as above devid. by the way let's try to use the same devid everywhere.
idev.iommufd is same as above iommufd if != NULL.
So we should at least rationalize.

Quoted 29 lines
include/hw/vfio/vfio-common.h +0 -0
 } VFIODevice;
 
 struct VFIODeviceOps {
hw/vfio/iommufd.c +2 -0
index 9bfddc1360..cbd035f148 100644
@@ -309,6 +309,7 @@ static int iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
     VFIOContainerBase *bcontainer;
     VFIOIOMMUFDContainer *container;
     VFIOAddressSpace *space;
+    IOMMUFDDevice *idev = &vbasedev->idev;
     struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
     int ret, devfd;
     uint32_t ioas_id;
@@ -428,6 +429,7 @@ found_container:
     QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
     QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
 
+    iommufd_device_init(idev, sizeof(*idev), container->be, vbasedev->devid);
     trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs,
                                    vbasedev->num_regions, vbasedev->flags);
     return 0;
hw/vfio/pci.c +0 -0
index d7fe06715c..2c3a5d267b 100644
@@ -3107,11 +3107,21 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
 

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

Quoted 10 lines
hw/vfio/pci.c +6 -1
     vfio_bars_register(vdev);
 
-    ret = vfio_add_capabilities(vdev, errp);
+    if (vbasedev->iommufd) {
+        ret = pci_device_set_iommu_device(pdev, &vbasedev->idev, errp);
+    } else {
+        ret = pci_device_set_iommu_device(pdev, 0, errp);
+    }
     if (ret) {
+        error_prepend(errp, "Failed to set iommu_device: ");

at the moment it is rather an IOMMUFD device.

Quoted 54 lines
hw/vfio/pci.c +11 -3
         goto out_teardown;
     }
 
+    ret = vfio_add_capabilities(vdev, errp);
+    if (ret) {
+        goto out_unset_idev;
+    }
+
     if (vdev->vga) {
         vfio_vga_quirk_setup(vdev);
     }
@@ -3128,7 +3138,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
             error_setg(errp,
                        "cannot support IGD OpRegion feature on hotplugged "
                        "device");
-            goto out_teardown;
+            goto out_unset_idev;
         }
 
         ret = vfio_get_dev_region_info(vbasedev,
@@ -3137,13 +3147,13 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
         if (ret) {
             error_setg_errno(errp, -ret,
                              "does not support requested IGD OpRegion feature");
-            goto out_teardown;
+            goto out_unset_idev;
         }
 
         ret = vfio_pci_igd_opregion_init(vdev, opregion, errp);
         g_free(opregion);
         if (ret) {
-            goto out_teardown;
+            goto out_unset_idev;
         }
     }
 
@@ -3229,6 +3239,8 @@ out_deregister:
     if (vdev->intx.mmap_timer) {
         timer_free(vdev->intx.mmap_timer);
     }
+out_unset_idev:
+    pci_device_unset_iommu_device(pdev);
 out_teardown:
     vfio_teardown_msi(vdev);
     vfio_bars_exit(vdev);
@@ -3257,6 +3269,7 @@ static void vfio_instance_finalize(Object *obj)
 static void vfio_exitfn(PCIDevice *pdev)
 {
     VFIOPCIDevice *vdev = VFIO_PCI(pdev);
+    VFIODevice *vbasedev = &vdev->vbasedev;
 
     vfio_unregister_req_notifier(vdev);
     vfio_unregister_err_notifier(vdev);

hw/vfio/pci.c static void vfio_exitfn(PCIDevice *pdev) · line 3291

Quoted 9 lines
hw/vfio/pci.c +2 -1
@@ -3271,7 +3284,8 @@ static void vfio_exitfn(PCIDevice *pdev)
     vfio_teardown_msi(vdev);
     vfio_pci_disable_rp_atomics(vdev);
     vfio_bars_exit(vdev);
-    vfio_migration_exit(&vdev->vbasedev);
+    vfio_migration_exit(vbasedev);
+    pci_device_unset_iommu_device(pdev);
 }
 
 static void vfio_pci_reset(DeviceState *dev)

Thanks

Eric

#31 lore raw

RE: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

Quoted 40 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

Hi Zhenzhong,

Quoted 4 lines · Zhenzhong Duan

Initialize IOMMUFDDevice in vfio and pass to vIOMMU, so that vIOMMU could get hw IOMMU information.

In VFIO legacy backend mode, we still pass a NULL IOMMUFDDevice to

vIOMMU,

include/hw/vfio/vfio-

Quoted 13 lines

in case vIOMMU needs some processing for VFIO legacy backend mode.

---
 include/hw/vfio/vfio-common.h |  2 ++
 hw/vfio/iommufd.c             |  2 ++
 hw/vfio/pci.c                 | 24 +++++++++++++++++++-----
 3 files changed, 23 insertions(+), 5 deletions(-)
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-common.h +0 -0

common.h

Quoted 11 lines

index 9b7ef7d02b..fde0d0ca60 100644

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

include/hw/vfio/vfio-common.h typedef struct VFIODevice { · line 130

Quoted 5 lines
include/hw/vfio/vfio-common.h +1 -0
@@ -126,6 +127,7 @@ typedef struct VFIODevice {
     bool dirty_tracking;
     int devid;
     IOMMUFDBackend *iommufd;
+    IOMMUFDDevice idev;

This looks duplicate of existing fields:
idev.dev_id is same as above devid. by the way let's try to use the same devid everywhere.
idev.iommufd is same as above iommufd if != NULL.
So we should at least rationalize.

Indeed, I'll remove devid and *iommufd. Thanks for suggestion.

Quoted 44 lines

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

Quoted 8 lines
include/hw/vfio/vfio-common.h +0 -0
 } VFIODevice;
 
 struct VFIODeviceOps {
hw/vfio/iommufd.c +0 -0
index 9bfddc1360..cbd035f148 100644
@@ -309,6 +309,7 @@ static int iommufd_cdev_attach(const char *name,

VFIODevice *vbasedev,

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

Quoted 9 lines
hw/vfio/iommufd.c +1 -0
     VFIOContainerBase *bcontainer;
     VFIOIOMMUFDContainer *container;
     VFIOAddressSpace *space;
+    IOMMUFDDevice *idev = &vbasedev->idev;
     struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
     int ret, devfd;
     uint32_t ioas_id;
@@ -428,6 +429,7 @@ found_container:
     QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev,

container_next);

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

Quoted 13 lines
hw/vfio/iommufd.c +1 -0
     QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
 
+    iommufd_device_init(idev, sizeof(*idev), container->be, vbasedev-
 devid);
     trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev-
 num_irqs,
                                    vbasedev->num_regions, vbasedev->flags);
     return 0;
hw/vfio/pci.c +0 -0
index d7fe06715c..2c3a5d267b 100644
@@ -3107,11 +3107,21 @@ static void vfio_realize(PCIDevice *pdev,

Error **errp)

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

Quoted 11 lines
hw/vfio/pci.c +6 -1
     vfio_bars_register(vdev);
 
-    ret = vfio_add_capabilities(vdev, errp);
+    if (vbasedev->iommufd) {
+        ret = pci_device_set_iommu_device(pdev, &vbasedev->idev, errp);
+    } else {
+        ret = pci_device_set_iommu_device(pdev, 0, errp);
+    }
     if (ret) {
+        error_prepend(errp, "Failed to set iommu_device: ");
Quoted 1 line

at the moment it is rather an IOMMUFD device.

Will use "Failed to set IOMMUFD device: "

Thanks
Zhenzhong

Quoted 68 lines

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

Quoted 12 lines
hw/vfio/pci.c +5 -0
         goto out_teardown;
     }
 
+    ret = vfio_add_capabilities(vdev, errp);
+    if (ret) {
+        goto out_unset_idev;
+    }
+
     if (vdev->vga) {
         vfio_vga_quirk_setup(vdev);
     }
@@ -3128,7 +3138,7 @@ static void vfio_realize(PCIDevice *pdev, Error

**errp)

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

Quoted 9 lines
hw/vfio/pci.c +1 -1
             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,
@@ -3137,13 +3147,13 @@ static void vfio_realize(PCIDevice *pdev,

Error **errp)

Quoted 33 lines
hw/vfio/pci.c +5 -2
         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;
         }
     }
 
@@ -3229,6 +3239,8 @@ out_deregister:
     if (vdev->intx.mmap_timer) {
         timer_free(vdev->intx.mmap_timer);
     }
+out_unset_idev:
+    pci_device_unset_iommu_device(pdev);
 out_teardown:
     vfio_teardown_msi(vdev);
     vfio_bars_exit(vdev);
@@ -3257,6 +3269,7 @@ static void vfio_instance_finalize(Object *obj)
 static void vfio_exitfn(PCIDevice *pdev)
 {
     VFIOPCIDevice *vdev = VFIO_PCI(pdev);
+    VFIODevice *vbasedev = &vdev->vbasedev;
 
     vfio_unregister_req_notifier(vdev);
     vfio_unregister_err_notifier(vdev);

hw/vfio/pci.c static void vfio_exitfn(PCIDevice *pdev) · line 3291

Quoted 9 lines
hw/vfio/pci.c +2 -1
@@ -3271,7 +3284,8 @@ static void vfio_exitfn(PCIDevice *pdev)
     vfio_teardown_msi(vdev);
     vfio_pci_disable_rp_atomics(vdev);
     vfio_bars_exit(vdev);
-    vfio_migration_exit(&vdev->vbasedev);
+    vfio_migration_exit(vbasedev);
+    pci_device_unset_iommu_device(pdev);
 }
 
 static void vfio_pci_reset(DeviceState *dev)

Thanks

Eric

#32 lore raw

Re: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

In reply to Zhenzhong Duan
Quoted 61 lines · Zhenzhong Duan

Initialize IOMMUFDDevice in vfio and pass to vIOMMU, so that vIOMMU could get hw IOMMU information.

In VFIO legacy backend mode, we still pass a NULL IOMMUFDDevice to vIOMMU, in case vIOMMU needs some processing for VFIO legacy backend mode.

---
  include/hw/vfio/vfio-common.h |  2 ++
  hw/vfio/iommufd.c             |  2 ++
  hw/vfio/pci.c                 | 24 +++++++++++++++++++-----
  3 files changed, 23 insertions(+), 5 deletions(-)
include/hw/vfio/vfio-common.h +2 -0
index 9b7ef7d02b..fde0d0ca60 100644
@@ -31,6 +31,7 @@
  #endif
  #include "sysemu/sysemu.h"
  #include "hw/vfio/vfio-container-base.h"
+#include "sysemu/iommufd_device.h"
 
  #define VFIO_MSG_PREFIX "vfio %s: "
 
@@ -126,6 +127,7 @@ typedef struct VFIODevice {
      bool dirty_tracking;
      int devid;
      IOMMUFDBackend *iommufd;
+    IOMMUFDDevice idev;
  } VFIODevice;
 
  struct VFIODeviceOps {
hw/vfio/iommufd.c +2 -0
index 9bfddc1360..cbd035f148 100644
@@ -309,6 +309,7 @@ static int iommufd_cdev_attach(const char *name, VFIODevice *vbasedev,
      VFIOContainerBase *bcontainer;
      VFIOIOMMUFDContainer *container;
      VFIOAddressSpace *space;
+    IOMMUFDDevice *idev = &vbasedev->idev;
      struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
      int ret, devfd;
      uint32_t ioas_id;
@@ -428,6 +429,7 @@ found_container:
      QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev, container_next);
      QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
 
+    iommufd_device_init(idev, sizeof(*idev), container->be, vbasedev->devid);
      trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev->num_irqs,
                                     vbasedev->num_regions, vbasedev->flags);
      return 0;
hw/vfio/pci.c +0 -0
index d7fe06715c..2c3a5d267b 100644

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

Quoted 8 lines
hw/vfio/pci.c +4 -1
@@ -3107,11 +3107,21 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
 
      vfio_bars_register(vdev);
 
-    ret = vfio_add_capabilities(vdev, errp);
+    if (vbasedev->iommufd) {
+        ret = pci_device_set_iommu_device(pdev, &vbasedev->idev, errp);
+    } else {
+        ret = pci_device_set_iommu_device(pdev, 0, errp);

AFAICT, pci_device_set_iommu_device() with a NULL IOMMUFDDevice will do nothing. Why call it ?

Thanks,

C.

Quoted 66 lines
hw/vfio/pci.c +15 -4
+    }
      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);
      }
@@ -3128,7 +3138,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
              error_setg(errp,
                         "cannot support IGD OpRegion feature on hotplugged "
                         "device");
-            goto out_teardown;
+            goto out_unset_idev;
          }
 
          ret = vfio_get_dev_region_info(vbasedev,
@@ -3137,13 +3147,13 @@ static void vfio_realize(PCIDevice *pdev, Error **errp)
          if (ret) {
              error_setg_errno(errp, -ret,
                               "does not support requested IGD OpRegion feature");
-            goto out_teardown;
+            goto out_unset_idev;
          }
 
          ret = vfio_pci_igd_opregion_init(vdev, opregion, errp);
          g_free(opregion);
          if (ret) {
-            goto out_teardown;
+            goto out_unset_idev;
          }
      }
 
@@ -3229,6 +3239,8 @@ out_deregister:
      if (vdev->intx.mmap_timer) {
          timer_free(vdev->intx.mmap_timer);
      }
+out_unset_idev:
+    pci_device_unset_iommu_device(pdev);
  out_teardown:
      vfio_teardown_msi(vdev);
      vfio_bars_exit(vdev);
@@ -3257,6 +3269,7 @@ static void vfio_instance_finalize(Object *obj)
  static void vfio_exitfn(PCIDevice *pdev)
  {
      VFIOPCIDevice *vdev = VFIO_PCI(pdev);
+    VFIODevice *vbasedev = &vdev->vbasedev;
 
      vfio_unregister_req_notifier(vdev);
      vfio_unregister_err_notifier(vdev);
@@ -3271,7 +3284,8 @@ static void vfio_exitfn(PCIDevice *pdev)
      vfio_teardown_msi(vdev);
      vfio_pci_disable_rp_atomics(vdev);
      vfio_bars_exit(vdev);
-    vfio_migration_exit(&vdev->vbasedev);
+    vfio_migration_exit(vbasedev);
+    pci_device_unset_iommu_device(pdev);
  }
 
  static void vfio_pci_reset(DeviceState *dev)
#33 lore raw

RE: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

Quoted 86 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

Quoted 4 lines · Zhenzhong Duan

Initialize IOMMUFDDevice in vfio and pass to vIOMMU, so that vIOMMU could get hw IOMMU information.

In VFIO legacy backend mode, we still pass a NULL IOMMUFDDevice to

vIOMMU,

include/hw/vfio/vfio-

Quoted 13 lines

in case vIOMMU needs some processing for VFIO legacy backend mode.

---
  include/hw/vfio/vfio-common.h |  2 ++
  hw/vfio/iommufd.c             |  2 ++
  hw/vfio/pci.c                 | 24 +++++++++++++++++++-----
  3 files changed, 23 insertions(+), 5 deletions(-)
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-common.h +0 -0

common.h

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

Quoted 24 lines

index 9b7ef7d02b..fde0d0ca60 100644

include/hw/vfio/vfio-common.h +2 -0
@@ -31,6 +31,7 @@
  #endif
  #include "sysemu/sysemu.h"
  #include "hw/vfio/vfio-container-base.h"
+#include "sysemu/iommufd_device.h"
 
  #define VFIO_MSG_PREFIX "vfio %s: "
 
@@ -126,6 +127,7 @@ typedef struct VFIODevice {
      bool dirty_tracking;
      int devid;
      IOMMUFDBackend *iommufd;
+    IOMMUFDDevice idev;
  } VFIODevice;
 
  struct VFIODeviceOps {
hw/vfio/iommufd.c +0 -0
index 9bfddc1360..cbd035f148 100644
@@ -309,6 +309,7 @@ static int iommufd_cdev_attach(const char *name,

VFIODevice *vbasedev,

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

Quoted 9 lines
hw/vfio/iommufd.c +1 -0
      VFIOContainerBase *bcontainer;
      VFIOIOMMUFDContainer *container;
      VFIOAddressSpace *space;
+    IOMMUFDDevice *idev = &vbasedev->idev;
      struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
      int ret, devfd;
      uint32_t ioas_id;
@@ -428,6 +429,7 @@ found_container:
      QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev,

container_next);

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

Quoted 13 lines
hw/vfio/iommufd.c +1 -0
      QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
 
+    iommufd_device_init(idev, sizeof(*idev), container->be, vbasedev-
 devid);
      trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev-
 num_irqs,
                                     vbasedev->num_regions, vbasedev->flags);
      return 0;
hw/vfio/pci.c +0 -0
index d7fe06715c..2c3a5d267b 100644
@@ -3107,11 +3107,21 @@ static void vfio_realize(PCIDevice *pdev,

Error **errp)

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

Quoted 8 lines
hw/vfio/pci.c +4 -1
      vfio_bars_register(vdev);
 
-    ret = vfio_add_capabilities(vdev, errp);
+    if (vbasedev->iommufd) {
+        ret = pci_device_set_iommu_device(pdev, &vbasedev->idev, errp);
+    } else {
+        ret = pci_device_set_iommu_device(pdev, 0, errp);
Quoted 3 lines

AFAICT, pci_device_set_iommu_device() with a NULL IOMMUFDDevice will do
nothing. Why call it ?

We will do something in nesting series, see https://github.com/yiliu1765/qemu/commit/7f0bb59575bb5cf38618ae950f68a8661676e881

Another choice is to call pci_device_set_iommu_device() no matter which backend is used and check idev->iommufd in vtd_dev_set_iommu_device(). Is this better for you?

Thanks
Zhenzhong

#34 lore raw

Re: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

Quoted 86 lines · Zhenzhong
Quoted 86 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

Quoted 4 lines · Zhenzhong Duan

Initialize IOMMUFDDevice in vfio and pass to vIOMMU, so that vIOMMU could get hw IOMMU information.

In VFIO legacy backend mode, we still pass a NULL IOMMUFDDevice to

vIOMMU,

include/hw/vfio/vfio-

Quoted 13 lines

in case vIOMMU needs some processing for VFIO legacy backend mode.

---
   include/hw/vfio/vfio-common.h |  2 ++
   hw/vfio/iommufd.c             |  2 ++
   hw/vfio/pci.c                 | 24 +++++++++++++++++++-----
   3 files changed, 23 insertions(+), 5 deletions(-)
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-common.h +0 -0

common.h

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

Quoted 24 lines

index 9b7ef7d02b..fde0d0ca60 100644

include/hw/vfio/vfio-common.h +2 -0
@@ -31,6 +31,7 @@
   #endif
   #include "sysemu/sysemu.h"
   #include "hw/vfio/vfio-container-base.h"
+#include "sysemu/iommufd_device.h"
 
   #define VFIO_MSG_PREFIX "vfio %s: "
 
@@ -126,6 +127,7 @@ typedef struct VFIODevice {
       bool dirty_tracking;
       int devid;
       IOMMUFDBackend *iommufd;
+    IOMMUFDDevice idev;
   } VFIODevice;
 
   struct VFIODeviceOps {
hw/vfio/iommufd.c +0 -0
index 9bfddc1360..cbd035f148 100644
@@ -309,6 +309,7 @@ static int iommufd_cdev_attach(const char *name,

VFIODevice *vbasedev,

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

Quoted 9 lines
hw/vfio/iommufd.c +1 -0
       VFIOContainerBase *bcontainer;
       VFIOIOMMUFDContainer *container;
       VFIOAddressSpace *space;
+    IOMMUFDDevice *idev = &vbasedev->idev;
       struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
       int ret, devfd;
       uint32_t ioas_id;
@@ -428,6 +429,7 @@ found_container:
       QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev,

container_next);

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

Quoted 13 lines
hw/vfio/iommufd.c +1 -0
       QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
 
+    iommufd_device_init(idev, sizeof(*idev), container->be, vbasedev-
 devid);
       trace_iommufd_cdev_device_info(vbasedev->name, devfd, vbasedev-
 num_irqs,
                                      vbasedev->num_regions, vbasedev->flags);
       return 0;
hw/vfio/pci.c +0 -0
index d7fe06715c..2c3a5d267b 100644
@@ -3107,11 +3107,21 @@ static void vfio_realize(PCIDevice *pdev,

Error **errp)

Quoted 8 lines
hw/vfio/pci.c +4 -1
       vfio_bars_register(vdev);
 
-    ret = vfio_add_capabilities(vdev, errp);
+    if (vbasedev->iommufd) {
+        ret = pci_device_set_iommu_device(pdev, &vbasedev->idev, errp);
+    } else {
+        ret = pci_device_set_iommu_device(pdev, 0, errp);
Quoted 5 lines
Quoted 3 lines

AFAICT, pci_device_set_iommu_device() with a NULL IOMMUFDDevice will do
nothing. Why call it ?

We will do something in nesting series, see https://github.com/yiliu1765/qemu/commit/7f0bb59575bb5cf38618ae950f68a8661676e881

ok, that's not much. idev is used as a capability bool and later on to pass the /dev/iommu fd. We don't need to support the legacy mode ?

Quoted 3 lines

Another choice is to call pci_device_set_iommu_device() no matter which backend is used and check idev->iommufd in vtd_dev_set_iommu_device(). Is this better for you?

yes. Should be fine. There is more to it though.

IIUC, what will determine most of the requirements, is the legacy mode. We also need the host iommu info in that case. As said Eric, ideally, we should introduce a common abstract "host-iommu-info" struct and sub structs associated with the iommu backends (iommufd + legacy) which would be allocated accordingly.

So, IOMMUFDDevice usage should be limited to the iommufd files. All PCI files should use the common abstract type. We should define these data structures first. They could be simple C struct for now. We will see if QOM applies after.

Will take a look at Eric's patchset next.

Thanks,

C.

#35 lore raw

RE: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

Quoted 102 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass to vIOMMU

Quoted 5 lines · Zhenzhong
Quoted 5 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv1 4/6] vfio: initialize IOMMUFDDevice and pass

to

Quoted 40 lines
Quoted 40 lines

vIOMMU

Quoted 4 lines · Zhenzhong Duan

Initialize IOMMUFDDevice in vfio and pass to vIOMMU, so that vIOMMU could get hw IOMMU information.

In VFIO legacy backend mode, we still pass a NULL IOMMUFDDevice to

vIOMMU,

include/hw/vfio/vfio-

Quoted 13 lines

in case vIOMMU needs some processing for VFIO legacy backend mode.

---
   include/hw/vfio/vfio-common.h |  2 ++
   hw/vfio/iommufd.c             |  2 ++
   hw/vfio/pci.c                 | 24 +++++++++++++++++++-----
   3 files changed, 23 insertions(+), 5 deletions(-)
include/hw/vfio/vfio- renamed from include/hw/vfio/vfio-common.h +0 -0

common.h

Quoted 18 lines

index 9b7ef7d02b..fde0d0ca60 100644

include/hw/vfio/vfio-common.h +2 -0
@@ -31,6 +31,7 @@
   #endif
   #include "sysemu/sysemu.h"
   #include "hw/vfio/vfio-container-base.h"
+#include "sysemu/iommufd_device.h"
 
   #define VFIO_MSG_PREFIX "vfio %s: "
 
@@ -126,6 +127,7 @@ typedef struct VFIODevice {
       bool dirty_tracking;
       int devid;
       IOMMUFDBackend *iommufd;
+    IOMMUFDDevice idev;
   } VFIODevice;
 
Quoted 6 lines
Quoted 6 lines

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

Quoted 6 lines
include/hw/vfio/vfio-common.h +0 -0
   struct VFIODeviceOps {
hw/vfio/iommufd.c +0 -0
index 9bfddc1360..cbd035f148 100644
@@ -309,6 +309,7 @@ static int iommufd_cdev_attach(const char

*name,

Quoted 11 lines
Quoted 11 lines

VFIODevice *vbasedev,

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

Quoted 9 lines
hw/vfio/iommufd.c +1 -0
       VFIOContainerBase *bcontainer;
       VFIOIOMMUFDContainer *container;
       VFIOAddressSpace *space;
+    IOMMUFDDevice *idev = &vbasedev->idev;
       struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };
       int ret, devfd;
       uint32_t ioas_id;
@@ -428,6 +429,7 @@ found_container:
       QLIST_INSERT_HEAD(&bcontainer->device_list, vbasedev,

container_next);

Quoted 5 lines
Quoted 5 lines

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

Quoted 5 lines
hw/vfio/iommufd.c +1 -0
       QLIST_INSERT_HEAD(&vfio_device_list, vbasedev, global_next);
 
+    iommufd_device_init(idev, sizeof(*idev), container->be, vbasedev-
 devid);
       trace_iommufd_cdev_device_info(vbasedev->name, devfd,

vbasedev-

Quoted 12 lines
Quoted 12 lines

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

Quoted 8 lines
hw/vfio/iommufd.c +0 -0
 num_irqs,
                                      vbasedev->num_regions, vbasedev->flags);
       return 0;
hw/vfio/pci.c +0 -0
index d7fe06715c..2c3a5d267b 100644
@@ -3107,11 +3107,21 @@ static void vfio_realize(PCIDevice *pdev,

Error **errp)

Quoted 3 lines
hw/vfio/pci.c +0 -0
       vfio_bars_register(vdev);
 
Quoted 8 lines
Quoted 8 lines

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

Quoted 5 lines
hw/vfio/pci.c +4 -1
-    ret = vfio_add_capabilities(vdev, errp);
+    if (vbasedev->iommufd) {
+        ret = pci_device_set_iommu_device(pdev, &vbasedev->idev, errp);
+    } else {
+        ret = pci_device_set_iommu_device(pdev, 0, errp);

AFAICT, pci_device_set_iommu_device() with a NULL IOMMUFDDevice

will

Quoted 3 lines
Quoted 2 lines

do
nothing. Why call it ?

Quoted 6 lines
Quoted 1 line

We will do something in nesting series, see

https://github.com/yiliu1765/qemu/commit/7f0bb59575bb5cf38618ae950
f68a8661676e881

ok, that's not much. idev is used as a capability bool and later on to pass the /dev/iommu fd. We don't need to support the legacy mode ?

It's better to have for legacy mode. Especially when we support address width 57 to QEMU Intel_iommu in future.

Quoted 7 lines
Quoted 2 lines

Another choice is to call pci_device_set_iommu_device() no matter which

backend

Quoted 1 line

is used and check idev->iommufd in vtd_dev_set_iommu_device(). Is this

better

Quoted 1 line

for you?

Quoted 7 lines

yes. Should be fine. There is more to it though.

IIUC, what will determine most of the requirements, is the legacy mode. We also need the host iommu info in that case. As said Eric, ideally, we should introduce a common abstract "host-iommu-info" struct and sub structs associated with the iommu backends (iommufd + legacy) which would be allocated accordingly.

I see, I'll make a rfcv2 as you and Eric suggested and discuss further with Eric what elements he needs in legacy sub structs.

Quoted 5 lines

So, IOMMUFDDevice usage should be limited to the iommufd files. All PCI files should use the common abstract type. We should define these data structures first. They could be simple C struct for now. We will see if QOM applies after.

Got it.

Thanks
Zhenzhong

#36 lore raw

RFC PATCH 5/6 intel_iommu: extract out vtd_cap_init to initialize cap/ecap

In reply to Zhenzhong Duan

This is a prerequisite for host cap/ecap sync.

No functional change intended.

---
 hw/i386/intel_iommu.c | 92 +++++++++++++++++++++++--------------------
 1 file changed, 50 insertions(+), 42 deletions(-)
hw/i386/intel_iommu.c +50 -42
index 95faf697eb..4c1d058ebd 100644
@@ -4009,30 +4009,10 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
     return;
 }
 
-/* Do the initialization. It will also be called when reset, so pay
- * attention when adding new initialization stuff.
- */
-static void vtd_init(IntelIOMMUState *s)
+static void vtd_cap_init(IntelIOMMUState *s)
 {
     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
-    memset(s->csr, 0, DMAR_REG_SIZE);
-    memset(s->wmask, 0, DMAR_REG_SIZE);
-    memset(s->w1cmask, 0, DMAR_REG_SIZE);
-    memset(s->womask, 0, DMAR_REG_SIZE);
-
-    s->root = 0;
-    s->root_scalable = false;
-    s->dmar_enabled = false;
-    s->intr_enabled = false;
-    s->iq_head = 0;
-    s->iq_tail = 0;
-    s->iq = 0;
-    s->iq_size = 0;
-    s->qi_enabled = false;
-    s->iq_last_desc_type = VTD_INV_DESC_NONE;
-    s->iq_dw = false;
-    s->next_frcd_reg = 0;
     s->cap = VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND |
              VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS |
              VTD_CAP_MGAW(s->aw_bits);
@@ -4049,27 +4029,6 @@ static void vtd_init(IntelIOMMUState *s)
     }
     s->ecap = VTD_ECAP_QI | VTD_ECAP_IRO;
 
-    /*
-     * Rsvd field masks for spte
-     */
-    vtd_spte_rsvd[0] = ~0ULL;
-    vtd_spte_rsvd[1] = VTD_SPTE_PAGE_L1_RSVD_MASK(s->aw_bits,
-                                                  x86_iommu->dt_supported);
-    vtd_spte_rsvd[2] = VTD_SPTE_PAGE_L2_RSVD_MASK(s->aw_bits);
-    vtd_spte_rsvd[3] = VTD_SPTE_PAGE_L3_RSVD_MASK(s->aw_bits);
-    vtd_spte_rsvd[4] = VTD_SPTE_PAGE_L4_RSVD_MASK(s->aw_bits);
-
-    vtd_spte_rsvd_large[2] = VTD_SPTE_LPAGE_L2_RSVD_MASK(s->aw_bits,
-                                                         x86_iommu->dt_supported);
-    vtd_spte_rsvd_large[3] = VTD_SPTE_LPAGE_L3_RSVD_MASK(s->aw_bits,
-                                                         x86_iommu->dt_supported);
-
-    if (s->scalable_mode || s->snoop_control) {
-        vtd_spte_rsvd[1] &= ~VTD_SPTE_SNP;
-        vtd_spte_rsvd_large[2] &= ~VTD_SPTE_SNP;
-        vtd_spte_rsvd_large[3] &= ~VTD_SPTE_SNP;
-    }
-
     if (x86_iommu_ir_supported(x86_iommu)) {
         s->ecap |= VTD_ECAP_IR | VTD_ECAP_MHMV;
         if (s->intr_eim == ON_OFF_AUTO_ON) {
@@ -4102,7 +4061,56 @@ static void vtd_init(IntelIOMMUState *s)
     if (s->pasid) {
         s->ecap |= VTD_ECAP_PASID;
     }
+}
+
+/*
+ * Do the initialization. It will also be called when reset, so pay
+ * attention when adding new initialization stuff.
+ */
+static void vtd_init(IntelIOMMUState *s)
+{
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
+
+    memset(s->csr, 0, DMAR_REG_SIZE);
+    memset(s->wmask, 0, DMAR_REG_SIZE);
+    memset(s->w1cmask, 0, DMAR_REG_SIZE);
+    memset(s->womask, 0, DMAR_REG_SIZE);
+
+    s->root = 0;
+    s->root_scalable = false;
+    s->dmar_enabled = false;
+    s->intr_enabled = false;
+    s->iq_head = 0;
+    s->iq_tail = 0;
+    s->iq = 0;
+    s->iq_size = 0;
+    s->qi_enabled = false;
+    s->iq_last_desc_type = VTD_INV_DESC_NONE;
+    s->iq_dw = false;
+    s->next_frcd_reg = 0;
+
+    /*
+     * Rsvd field masks for spte
+     */
+    vtd_spte_rsvd[0] = ~0ULL;
+    vtd_spte_rsvd[1] = VTD_SPTE_PAGE_L1_RSVD_MASK(s->aw_bits,
+                                                  x86_iommu->dt_supported);
+    vtd_spte_rsvd[2] = VTD_SPTE_PAGE_L2_RSVD_MASK(s->aw_bits);
+    vtd_spte_rsvd[3] = VTD_SPTE_PAGE_L3_RSVD_MASK(s->aw_bits);
+    vtd_spte_rsvd[4] = VTD_SPTE_PAGE_L4_RSVD_MASK(s->aw_bits);
+
+    vtd_spte_rsvd_large[2] = VTD_SPTE_LPAGE_L2_RSVD_MASK(s->aw_bits,
+                                                    x86_iommu->dt_supported);
+    vtd_spte_rsvd_large[3] = VTD_SPTE_LPAGE_L3_RSVD_MASK(s->aw_bits,
+                                                    x86_iommu->dt_supported);
+
+    if (s->scalable_mode || s->snoop_control) {
+        vtd_spte_rsvd[1] &= ~VTD_SPTE_SNP;
+        vtd_spte_rsvd_large[2] &= ~VTD_SPTE_SNP;
+        vtd_spte_rsvd_large[3] &= ~VTD_SPTE_SNP;
+    }
 
+    vtd_cap_init(s);
     vtd_reset_caches(s);
 
     /* Define registers with default values and bit semantics */
Signature · 1 line
2.34.1
Reviewed-by on v1 5/6
#37 lore raw

Re: [PATCH rfcv1 5/6] intel_iommu: extract out vtd_cap_init to initialize cap/ecap

Quoted 5 lines · Zhenzhong Duan

This is a prerequisite for host cap/ecap sync.

No functional change intended.

Looks good to me

Eric

Quoted 125 lines
---
 hw/i386/intel_iommu.c | 92 +++++++++++++++++++++++--------------------
 1 file changed, 50 insertions(+), 42 deletions(-)
hw/i386/intel_iommu.c +50 -42
index 95faf697eb..4c1d058ebd 100644
@@ -4009,30 +4009,10 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
     return;
 }
 
-/* Do the initialization. It will also be called when reset, so pay
- * attention when adding new initialization stuff.
- */
-static void vtd_init(IntelIOMMUState *s)
+static void vtd_cap_init(IntelIOMMUState *s)
 {
     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
-    memset(s->csr, 0, DMAR_REG_SIZE);
-    memset(s->wmask, 0, DMAR_REG_SIZE);
-    memset(s->w1cmask, 0, DMAR_REG_SIZE);
-    memset(s->womask, 0, DMAR_REG_SIZE);
-
-    s->root = 0;
-    s->root_scalable = false;
-    s->dmar_enabled = false;
-    s->intr_enabled = false;
-    s->iq_head = 0;
-    s->iq_tail = 0;
-    s->iq = 0;
-    s->iq_size = 0;
-    s->qi_enabled = false;
-    s->iq_last_desc_type = VTD_INV_DESC_NONE;
-    s->iq_dw = false;
-    s->next_frcd_reg = 0;
     s->cap = VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND |
              VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS |
              VTD_CAP_MGAW(s->aw_bits);
@@ -4049,27 +4029,6 @@ static void vtd_init(IntelIOMMUState *s)
     }
     s->ecap = VTD_ECAP_QI | VTD_ECAP_IRO;
 
-    /*
-     * Rsvd field masks for spte
-     */
-    vtd_spte_rsvd[0] = ~0ULL;
-    vtd_spte_rsvd[1] = VTD_SPTE_PAGE_L1_RSVD_MASK(s->aw_bits,
-                                                  x86_iommu->dt_supported);
-    vtd_spte_rsvd[2] = VTD_SPTE_PAGE_L2_RSVD_MASK(s->aw_bits);
-    vtd_spte_rsvd[3] = VTD_SPTE_PAGE_L3_RSVD_MASK(s->aw_bits);
-    vtd_spte_rsvd[4] = VTD_SPTE_PAGE_L4_RSVD_MASK(s->aw_bits);
-
-    vtd_spte_rsvd_large[2] = VTD_SPTE_LPAGE_L2_RSVD_MASK(s->aw_bits,
-                                                         x86_iommu->dt_supported);
-    vtd_spte_rsvd_large[3] = VTD_SPTE_LPAGE_L3_RSVD_MASK(s->aw_bits,
-                                                         x86_iommu->dt_supported);
-
-    if (s->scalable_mode || s->snoop_control) {
-        vtd_spte_rsvd[1] &= ~VTD_SPTE_SNP;
-        vtd_spte_rsvd_large[2] &= ~VTD_SPTE_SNP;
-        vtd_spte_rsvd_large[3] &= ~VTD_SPTE_SNP;
-    }
-
     if (x86_iommu_ir_supported(x86_iommu)) {
         s->ecap |= VTD_ECAP_IR | VTD_ECAP_MHMV;
         if (s->intr_eim == ON_OFF_AUTO_ON) {
@@ -4102,7 +4061,56 @@ static void vtd_init(IntelIOMMUState *s)
     if (s->pasid) {
         s->ecap |= VTD_ECAP_PASID;
     }
+}
+
+/*
+ * Do the initialization. It will also be called when reset, so pay
+ * attention when adding new initialization stuff.
+ */
+static void vtd_init(IntelIOMMUState *s)
+{
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
+
+    memset(s->csr, 0, DMAR_REG_SIZE);
+    memset(s->wmask, 0, DMAR_REG_SIZE);
+    memset(s->w1cmask, 0, DMAR_REG_SIZE);
+    memset(s->womask, 0, DMAR_REG_SIZE);
+
+    s->root = 0;
+    s->root_scalable = false;
+    s->dmar_enabled = false;
+    s->intr_enabled = false;
+    s->iq_head = 0;
+    s->iq_tail = 0;
+    s->iq = 0;
+    s->iq_size = 0;
+    s->qi_enabled = false;
+    s->iq_last_desc_type = VTD_INV_DESC_NONE;
+    s->iq_dw = false;
+    s->next_frcd_reg = 0;
+
+    /*
+     * Rsvd field masks for spte
+     */
+    vtd_spte_rsvd[0] = ~0ULL;
+    vtd_spte_rsvd[1] = VTD_SPTE_PAGE_L1_RSVD_MASK(s->aw_bits,
+                                                  x86_iommu->dt_supported);
+    vtd_spte_rsvd[2] = VTD_SPTE_PAGE_L2_RSVD_MASK(s->aw_bits);
+    vtd_spte_rsvd[3] = VTD_SPTE_PAGE_L3_RSVD_MASK(s->aw_bits);
+    vtd_spte_rsvd[4] = VTD_SPTE_PAGE_L4_RSVD_MASK(s->aw_bits);
+
+    vtd_spte_rsvd_large[2] = VTD_SPTE_LPAGE_L2_RSVD_MASK(s->aw_bits,
+                                                    x86_iommu->dt_supported);
+    vtd_spte_rsvd_large[3] = VTD_SPTE_LPAGE_L3_RSVD_MASK(s->aw_bits,
+                                                    x86_iommu->dt_supported);
+
+    if (s->scalable_mode || s->snoop_control) {
+        vtd_spte_rsvd[1] &= ~VTD_SPTE_SNP;
+        vtd_spte_rsvd_large[2] &= ~VTD_SPTE_SNP;
+        vtd_spte_rsvd_large[3] &= ~VTD_SPTE_SNP;
+    }
 
+    vtd_cap_init(s);
     vtd_reset_caches(s);
 
     /* Define registers with default values and bit semantics */
#38 lore raw

RFC PATCH 6/6 intel_iommu: add a framework to check and sync host IOMMU cap/ecap

In reply to Zhenzhong Duan

From: Yi Liu <[email protected]>

Add a framework to check and synchronize host IOMMU cap/ecap with vIOMMU cap/ecap.

Currently only stage-2 translation is supported which is backed by shadow page table on host side. So we don't need exact matching of each bit of cap/ecap between vIOMMU and host. However, we can still utilize this framework to ensure compatibility of host and vIOMMU's address width at least, i.e., vIOMMU's aw_bits <= host aw_bits, which is missed before.

When stage-1 translation is supported in future, a.k.a. scalable modern mode, we need to ensure compatibility of each bits. Some bits are user controllable, they should be checked with host side to ensure compatibility. Other bits are not, they should be synced into vIOMMU cap/ecap for compatibility.

The sequence will be:

vtd_cap_init() initializes iommu->cap/ecap. ---- vtd_cap_init() iommu->host_cap/ecap is initialized as iommu->cap/ecap. ---- vtd_init() iommu->host_cap/ecap is checked and updated some bits with host cap/ecap. ---- vtd_sync_hw_info() iommu->cap/ecap is finalized as iommu->host_cap/ecap. ---- vtd_machine_done_hook()

iommu->host_cap/ecap is a temporary storage to hold intermediate value when synthesize host cap/ecap and vIOMMU's initial configured cap/ecap.

---
 include/hw/i386/intel_iommu.h |  4 ++
 hw/i386/intel_iommu.c         | 78 +++++++++++++++++++++++++++++++----
 2 files changed, 75 insertions(+), 7 deletions(-)
include/hw/i386/intel_iommu.h +4 -0
index c65fdde56f..b8abbcce12 100644
@@ -292,6 +292,9 @@ struct IntelIOMMUState {
     uint64_t cap;                   /* The value of capability reg */
     uint64_t ecap;                  /* The value of extended capability reg */
 
+    uint64_t host_cap;              /* The value of host capability reg */
+    uint64_t host_ecap;             /* The value of host ext-capability reg */
+
     uint32_t context_cache_gen;     /* Should be in [1,MAX] */
     GHashTable *iotlb;              /* IOTLB */
 
@@ -314,6 +317,7 @@ struct IntelIOMMUState {
     bool dma_translation;           /* Whether DMA translation supported */
     bool pasid;                     /* Whether to support PASID */
 
+    bool cap_finalized;             /* Whether VTD capability finalized */
     /*
      * Protects IOMMU states in general.  Currently it protects the
      * per-IOMMU IOTLB cache, and context entry cache in VTDAddressSpace.
hw/i386/intel_iommu.c +71 -7

124 lines not on this page · raw message

Signature · 1 line
2.34.1
#39 lore raw

Re: [PATCH rfcv1 6/6] intel_iommu: add a framework to check and sync host IOMMU cap/ecap

Hi Zhenzhong,

Quoted 111 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

Add a framework to check and synchronize host IOMMU cap/ecap with vIOMMU cap/ecap.

Currently only stage-2 translation is supported which is backed by shadow page table on host side. So we don't need exact matching of each bit of cap/ecap between vIOMMU and host. However, we can still utilize this framework to ensure compatibility of host and vIOMMU's address width at least, i.e., vIOMMU's aw_bits <= host aw_bits, which is missed before.

When stage-1 translation is supported in future, a.k.a. scalable modern mode, we need to ensure compatibility of each bits. Some bits are user controllable, they should be checked with host side to ensure compatibility. Other bits are not, they should be synced into vIOMMU cap/ecap for compatibility.

The sequence will be:

vtd_cap_init() initializes iommu->cap/ecap. ---- vtd_cap_init() iommu->host_cap/ecap is initialized as iommu->cap/ecap. ---- vtd_init() iommu->host_cap/ecap is checked and updated some bits with host cap/ecap. ---- vtd_sync_hw_info() iommu->cap/ecap is finalized as iommu->host_cap/ecap. ---- vtd_machine_done_hook()

iommu->host_cap/ecap is a temporary storage to hold intermediate value when synthesize host cap/ecap and vIOMMU's initial configured cap/ecap.

---
 include/hw/i386/intel_iommu.h |  4 ++
 hw/i386/intel_iommu.c         | 78 +++++++++++++++++++++++++++++++----
 2 files changed, 75 insertions(+), 7 deletions(-)
include/hw/i386/intel_iommu.h +4 -0
index c65fdde56f..b8abbcce12 100644
@@ -292,6 +292,9 @@ struct IntelIOMMUState {
     uint64_t cap;                   /* The value of capability reg */
     uint64_t ecap;                  /* The value of extended capability reg */
 
+    uint64_t host_cap;              /* The value of host capability reg */
+    uint64_t host_ecap;             /* The value of host ext-capability reg */
+
     uint32_t context_cache_gen;     /* Should be in [1,MAX] */
     GHashTable *iotlb;              /* IOTLB */
 
@@ -314,6 +317,7 @@ struct IntelIOMMUState {
     bool dma_translation;           /* Whether DMA translation supported */
     bool pasid;                     /* Whether to support PASID */
 
+    bool cap_finalized;             /* Whether VTD capability finalized */
     /*
      * Protects IOMMU states in general.  Currently it protects the
      * per-IOMMU IOTLB cache, and context entry cache in VTDAddressSpace.
hw/i386/intel_iommu.c +41 -0
index 4c1d058ebd..be03fcbf52 100644
@@ -3819,6 +3819,47 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
     return vtd_dev_as;
 }
 
+static bool vtd_sync_hw_info(IntelIOMMUState *s, struct iommu_hw_info_vtd *vtd,
+                             Error **errp)
+{
+    uint64_t addr_width;
+
+    addr_width = (vtd->cap_reg >> 16) & 0x3fULL;
+    if (s->aw_bits > addr_width) {
+        error_setg(errp, "User aw-bits: %u > host address width: %lu",
+                   s->aw_bits, addr_width);
+        return false;
+    }
+
+    /* TODO: check and sync host cap/ecap into vIOMMU cap/ecap */
+
+    return true;
+}
+
+/*
+ * virtual VT-d which wants nested needs to check the host IOMMU
+ * nesting cap info behind the assigned devices. Thus that vIOMMU
+ * could bind guest page table to host.
+ */
+static bool vtd_check_idev(IntelIOMMUState *s, IOMMUFDDevice *idev,
+                           Error **errp)
+{
+    struct iommu_hw_info_vtd vtd;
+    enum iommu_hw_info_type type = IOMMU_HW_INFO_TYPE_INTEL_VTD;
+
+    if (iommufd_device_get_info(idev, &type, sizeof(vtd), &vtd)) {
+        error_setg(errp, "Failed to get IOMMU capability!!!");
+        return false;
+    }
+
+    if (type != IOMMU_HW_INFO_TYPE_INTEL_VTD) {
+        error_setg(errp, "IOMMU hardware is not compatible!!!");
+        return false;
+    }
+
+    return vtd_sync_hw_info(s, &vtd, errp);
+}
+
 static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int32_t devfn,
                                     IOMMUFDDevice *idev, Error **errp)
 {

hw/i386/intel_iommu.c static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int32_t devfn, · line 3881

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
@@ -3837,6 +3878,10 @@ static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int32_t devfn,
         return 0;
     }
 
+    if (!vtd_check_idev(s, idev, errp)) {In

In
[RFC 0/7] VIRTIO-IOMMU/VFIO: Fix host iommu geometry handling for hotplugged devices
https://lore.kernel.org/all/[email protected]/

I also attempt to pass host iommu info to the virtio-iommu but with legacy BE. In my case I want to pass the reserved memory regions which also model the aw.
So this is a pretty similar use case.

Why don't we pass the pointer to an opaque iommu_hw_info instead, through the PCIIOMMUOps?

Quoted 9 lines
hw/i386/intel_iommu.c +3 -0
+        return -1;
+    }
+
     vtd_iommu_lock(s);
 
     vtd_idev = g_hash_table_lookup(s->vtd_iommufd_dev, &key);
@@ -4071,10 +4116,11 @@ static void vtd_init(IntelIOMMUState *s)
 {
     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4123

Quoted 10 lines
hw/i386/intel_iommu.c +5 -4
 
-    memset(s->csr, 0, DMAR_REG_SIZE);
-    memset(s->wmask, 0, DMAR_REG_SIZE);
-    memset(s->w1cmask, 0, DMAR_REG_SIZE);
-    memset(s->womask, 0, DMAR_REG_SIZE);
+    /* CAP/ECAP are initialized in machine create done stage */
+    memset(s->csr + DMAR_GCMD_REG, 0, DMAR_REG_SIZE - DMAR_GCMD_REG);
+    memset(s->wmask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE - DMAR_GCMD_REG);
+    memset(s->w1cmask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE - DMAR_GCMD_REG);
+    memset(s->womask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE - DMAR_GCMD_REG);

This change is not documented in the commit msg.
Sorry I don't get why this is needed?

Quoted 37 lines
hw/i386/intel_iommu.c +12 -3
     s->root = 0;
     s->root_scalable = false;
@@ -4110,13 +4156,16 @@ static void vtd_init(IntelIOMMUState *s)
         vtd_spte_rsvd_large[3] &= ~VTD_SPTE_SNP;
     }
 
-    vtd_cap_init(s);
+    if (!s->cap_finalized) {
+        vtd_cap_init(s);
+        s->host_cap = s->cap;
+        s->host_ecap = s->ecap;
+    }
+
     vtd_reset_caches(s);
 
     /* Define registers with default values and bit semantics */
     vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0);
-    vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
-    vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
     vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0);
     vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL);
     vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0);
@@ -4241,6 +4290,12 @@ static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
     return true;
 }
 
+static void vtd_setup_capability_reg(IntelIOMMUState *s)
+{
+    vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
+    vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
+}
+
 static int vtd_machine_done_notify_one(Object *child, void *unused)
 {
     IntelIOMMUState *iommu = INTEL_IOMMU_DEVICE(x86_iommu_get_default());

hw/i386/intel_iommu.c static int vtd_machine_done_notify_one(Object *child, void *unused) · line 4321

Quoted 8 lines
hw/i386/intel_iommu.c +5 -0
@@ -4259,6 +4314,14 @@ static int vtd_machine_done_notify_one(Object *child, void *unused)
 
 static void vtd_machine_done_hook(Notifier *notifier, void *unused)
 {
+    IntelIOMMUState *iommu = INTEL_IOMMU_DEVICE(x86_iommu_get_default());
+
+    iommu->cap = iommu->host_cap;
+    iommu->ecap = iommu->host_ecap;
+    iommu->cap_finalized = true;

I don't think you can change the defaults like this without taking care of compats (migration).

Thanks

Eric

Quoted 14 lines
hw/i386/intel_iommu.c +4 -0
+
+    vtd_setup_capability_reg(iommu);
+
     object_child_foreach_recursive(object_get_root(),
                                    vtd_machine_done_notify_one, NULL);
 }
@@ -4292,6 +4355,7 @@ static void vtd_realize(DeviceState *dev, Error **errp)
 
     QLIST_INIT(&s->vtd_as_with_notifiers);
     qemu_mutex_init(&s->iommu_lock);
+    s->cap_finalized = false;
     memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
                           "intel_iommu", DMAR_REG_SIZE);
     memory_region_add_subregion(get_system_memory(),
#40 lore raw

RE: [PATCH rfcv1 6/6] intel_iommu: add a framework to check and sync host IOMMU cap/ecap

Hi Eric,

Quoted 133 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 6/6] intel_iommu: add a framework to check and sync host IOMMU cap/ecap

Hi Zhenzhong,

Quoted 18 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

Add a framework to check and synchronize host IOMMU cap/ecap with vIOMMU cap/ecap.

Currently only stage-2 translation is supported which is backed by shadow page table on host side. So we don't need exact matching of each bit of cap/ecap between vIOMMU and host. However, we can still utilize this framework to ensure compatibility of host and vIOMMU's address width at least, i.e., vIOMMU's aw_bits <= host aw_bits, which is missed before.

When stage-1 translation is supported in future, a.k.a. scalable modern mode, we need to ensure compatibility of each bits. Some bits are user controllable, they should be checked with host side to ensure compatibility. Other bits are not, they should be synced into vIOMMU cap/ecap for compatibility.

Quoted 5 lines

The sequence will be:

vtd_cap_init() initializes iommu->cap/ecap. ---- vtd_cap_init() iommu->host_cap/ecap is initialized as iommu->cap/ecap. ---- vtd_init() iommu->host_cap/ecap is checked and updated some bits with host

cap/ecap. ---- vtd_sync_hw_info()

Quoted 1 line

iommu->cap/ecap is finalized as iommu->host_cap/ecap. ----

vtd_machine_done_hook()

Quoted 10 lines

iommu->host_cap/ecap is a temporary storage to hold intermediate value when synthesize host cap/ecap and vIOMMU's initial configured cap/ecap.

---
 include/hw/i386/intel_iommu.h |  4 ++
 hw/i386/intel_iommu.c         | 78

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

Review

Quoted 3 lines
 2 files changed, 75 insertions(+), 7 deletions(-)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 14 lines

index c65fdde56f..b8abbcce12 100644

include/hw/i386/intel_iommu.h +3 -0
@@ -292,6 +292,9 @@ struct IntelIOMMUState {
     uint64_t cap;                   /* The value of capability reg */
     uint64_t ecap;                  /* The value of extended capability reg */
 
+    uint64_t host_cap;              /* The value of host capability reg */
+    uint64_t host_ecap;             /* The value of host ext-capability reg */
+
     uint32_t context_cache_gen;     /* Should be in [1,MAX] */
     GHashTable *iotlb;              /* IOTLB */
 

include/hw/i386/intel_iommu.h struct IntelIOMMUState { · line 323

Quoted 7 lines
include/hw/i386/intel_iommu.h +1 -0
@@ -314,6 +317,7 @@ struct IntelIOMMUState {
     bool dma_translation;           /* Whether DMA translation supported */
     bool pasid;                     /* Whether to support PASID */
 
+    bool cap_finalized;             /* Whether VTD capability finalized */
     /*
      * Protects IOMMU states in general.  Currently it protects the
      * per-IOMMU IOTLB cache, and context entry cache in

VTDAddressSpace.

hw/i386/intel_iommu.c VTDAddressSpace

Quoted 5 lines
hw/i386/intel_iommu.c +0 -0
index 4c1d058ebd..be03fcbf52 100644
@@ -3819,6 +3819,47 @@ VTDAddressSpace

*vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
     return vtd_dev_as;
 }
 
+static bool vtd_sync_hw_info(IntelIOMMUState *s, struct

iommu_hw_info_vtd *vtd,

Quoted 16 lines
hw/i386/intel_iommu.c +16 -0
+                             Error **errp)
+{
+    uint64_t addr_width;
+
+    addr_width = (vtd->cap_reg >> 16) & 0x3fULL;
+    if (s->aw_bits > addr_width) {
+        error_setg(errp, "User aw-bits: %u > host address width: %lu",
+                   s->aw_bits, addr_width);
+        return false;
+    }
+
+    /* TODO: check and sync host cap/ecap into vIOMMU cap/ecap */
+
+    return true;
+}
+

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+/*
+ * virtual VT-d which wants nested needs to check the host IOMMU
+ * nesting cap info behind the assigned devices. Thus that vIOMMU
+ * could bind guest page table to host.
+ */
+static bool vtd_check_idev(IntelIOMMUState *s, IOMMUFDDevice *idev,
+                           Error **errp)
+{
+    struct iommu_hw_info_vtd vtd;
+    enum iommu_hw_info_type type =

IOMMU_HW_INFO_TYPE_INTEL_VTD;

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+
+    if (iommufd_device_get_info(idev, &type, sizeof(vtd), &vtd)) {
+        error_setg(errp, "Failed to get IOMMU capability!!!");
+        return false;
+    }

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 10 lines
hw/i386/intel_iommu.c +9 -0
+
+    if (type != IOMMU_HW_INFO_TYPE_INTEL_VTD) {
+        error_setg(errp, "IOMMU hardware is not compatible!!!");
+        return false;
+    }
+
+    return vtd_sync_hw_info(s, &vtd, errp);
+}
+
 static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int32_t

devfn,

hw/i386/intel_iommu.c static int vtd_dev_set_iommu_device(PCIBus

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
                                     IOMMUFDDevice *idev, Error **errp)
 {
@@ -3837,6 +3878,10 @@ static int vtd_dev_set_iommu_device(PCIBus

*bus, void *opaque, int32_t devfn,

hw/i386/intel_iommu.c static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int32_t devfn,

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
         return 0;
     }
 
+    if (!vtd_check_idev(s, idev, errp)) {In
Quoted 8 lines

In
[RFC 0/7] VIRTIO-IOMMU/VFIO: Fix host iommu geometry handling for hotplugged devices
https://lore.kernel.org/all/20240117080414.316890-1-
[email protected]/

I also attempt to pass host iommu info to the virtio-iommu but with legacy BE.

I think your patch works with iommufd BE too😊 Because iommufd BE also fills bcontainer->iova_ranges in iommufd_cdev_get_info_iova_range().

Quoted 3 lines

In my case I want to pass the reserved memory regions which also model the aw.
So this is a pretty similar use case.

Yes.

Quoted 3 lines

Why don't we pass the pointer to an opaque iommu_hw_info instead, through the PCIIOMMUOps?

Passing iommu_hw_info is ok for this series, but we want more from IOMMUFDDevice in nesting series. I.e., allocate/free ioas/hwpt, attach/detach from hwpt, get dirty bitmap, etc. It's more flexible to let vIOMMU get what it want itself.

Quoted 20 lines
Quoted 10 lines
hw/i386/intel_iommu.c +3 -0
+        return -1;
+    }
+
     vtd_iommu_lock(s);
 
     vtd_idev = g_hash_table_lookup(s->vtd_iommufd_dev, &key);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4120

Quoted 9 lines
hw/i386/intel_iommu.c +2 -4
@@ -4071,10 +4116,11 @@ static void vtd_init(IntelIOMMUState *s)
 {
     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
-    memset(s->csr, 0, DMAR_REG_SIZE);
-    memset(s->wmask, 0, DMAR_REG_SIZE);
-    memset(s->w1cmask, 0, DMAR_REG_SIZE);
-    memset(s->womask, 0, DMAR_REG_SIZE);
+    /* CAP/ECAP are initialized in machine create done stage */
+    memset(s->csr + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);

Quoted 8 lines

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4121

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    memset(s->wmask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4122

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    memset(s->w1cmask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4123

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    memset(s->womask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);
This change is not documented in the commit msg.
Sorry I don't get why this is needed?

I'll doc it. Above we have one line to explain when cap/ecap are initialized. vtd_init() is called in qemu init and guest reset. In qemu init, Cap/ecap is finalized, after that we don't want cap/ecap to be changed. So we bypass change to cap/ecap here.

Quoted 44 lines

hw/i386/intel_iommu.c static bool

Quoted 24 lines
hw/i386/intel_iommu.c +6 -3
     s->root = 0;
     s->root_scalable = false;
@@ -4110,13 +4156,16 @@ static void vtd_init(IntelIOMMUState *s)
         vtd_spte_rsvd_large[3] &= ~VTD_SPTE_SNP;
     }
 
-    vtd_cap_init(s);
+    if (!s->cap_finalized) {
+        vtd_cap_init(s);
+        s->host_cap = s->cap;
+        s->host_ecap = s->ecap;
+    }
+
     vtd_reset_caches(s);
 
     /* Define registers with default values and bit semantics */
     vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0);
-    vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
-    vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
     vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0);
     vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL);
     vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0);
@@ -4241,6 +4290,12 @@ static bool

vtd_decide_config(IntelIOMMUState *s, Error **errp)

hw/i386/intel_iommu.c static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)

Quoted 12 lines
hw/i386/intel_iommu.c +6 -0
     return true;
 }
 
+static void vtd_setup_capability_reg(IntelIOMMUState *s)
+{
+    vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
+    vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
+}
+
 static int vtd_machine_done_notify_one(Object *child, void *unused)
 {
     IntelIOMMUState *iommu =

INTEL_IOMMU_DEVICE(x86_iommu_get_default());

hw/i386/intel_iommu.c static int

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
@@ -4259,6 +4314,14 @@ static int

vtd_machine_done_notify_one(Object *child, void *unused)

hw/i386/intel_iommu.c static int vtd_machine_done_notify_one(Object *child, void *unused)

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
 static void vtd_machine_done_hook(Notifier *notifier, void *unused)
 {
+    IntelIOMMUState *iommu =
Quoted 7 lines

INTEL_IOMMU_DEVICE(x86_iommu_get_default());

hw/i386/intel_iommu.c static int vtd_machine_done_notify_one(Object *child, void *unused) · line 4321

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+
+    iommu->cap = iommu->host_cap;
+    iommu->ecap = iommu->host_ecap;
+    iommu->cap_finalized = true;

I don't think you can change the defaults like this without taking care of compats (migration).

Will bump vtd_vmstate .version_id works?

Thanks
Zhenzhong

Quoted 19 lines

Thanks

Eric

hw/i386/intel_iommu.c static void vtd_realize(DeviceState *dev, Error

Quoted 7 lines
hw/i386/intel_iommu.c +3 -0
+
+    vtd_setup_capability_reg(iommu);
+
     object_child_foreach_recursive(object_get_root(),
                                    vtd_machine_done_notify_one, NULL);
 }
@@ -4292,6 +4355,7 @@ static void vtd_realize(DeviceState *dev, Error

**errp)

Quoted 7 lines
hw/i386/intel_iommu.c +1 -0
     QLIST_INIT(&s->vtd_as_with_notifiers);
     qemu_mutex_init(&s->iommu_lock);
+    s->cap_finalized = false;
     memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
                           "intel_iommu", DMAR_REG_SIZE);
     memory_region_add_subregion(get_system_memory(),
#41 lore raw

Re: [PATCH rfcv1 6/6] intel_iommu: add a framework to check and sync host IOMMU cap/ecap

Quoted 140 lines · Zhenzhong

Hi Eric,

Quoted 138 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 6/6] intel_iommu: add a framework to check and sync host IOMMU cap/ecap

Hi Zhenzhong,

Quoted 18 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

Add a framework to check and synchronize host IOMMU cap/ecap with vIOMMU cap/ecap.

Currently only stage-2 translation is supported which is backed by shadow page table on host side. So we don't need exact matching of each bit of cap/ecap between vIOMMU and host. However, we can still utilize this framework to ensure compatibility of host and vIOMMU's address width at least, i.e., vIOMMU's aw_bits <= host aw_bits, which is missed before.

When stage-1 translation is supported in future, a.k.a. scalable modern mode, we need to ensure compatibility of each bits. Some bits are user controllable, they should be checked with host side to ensure compatibility. Other bits are not, they should be synced into vIOMMU cap/ecap for compatibility.

Quoted 5 lines

The sequence will be:

vtd_cap_init() initializes iommu->cap/ecap. ---- vtd_cap_init() iommu->host_cap/ecap is initialized as iommu->cap/ecap. ---- vtd_init() iommu->host_cap/ecap is checked and updated some bits with host

cap/ecap. ---- vtd_sync_hw_info()

Quoted 1 line

iommu->cap/ecap is finalized as iommu->host_cap/ecap. ----

vtd_machine_done_hook()

Quoted 9 lines

iommu->host_cap/ecap is a temporary storage to hold intermediate value when synthesize host cap/ecap and vIOMMU's initial configured cap/ecap.

---
 include/hw/i386/intel_iommu.h |  4 ++
 hw/i386/intel_iommu.c         | 78

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

Review

Quoted 3 lines
 2 files changed, 75 insertions(+), 7 deletions(-)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 14 lines

index c65fdde56f..b8abbcce12 100644

include/hw/i386/intel_iommu.h +3 -0
@@ -292,6 +292,9 @@ struct IntelIOMMUState {
     uint64_t cap;                   /* The value of capability reg */
     uint64_t ecap;                  /* The value of extended capability reg */
 
+    uint64_t host_cap;              /* The value of host capability reg */
+    uint64_t host_ecap;             /* The value of host ext-capability reg */
+
     uint32_t context_cache_gen;     /* Should be in [1,MAX] */
     GHashTable *iotlb;              /* IOTLB */
 

include/hw/i386/intel_iommu.h struct IntelIOMMUState { · line 323

Quoted 7 lines
include/hw/i386/intel_iommu.h +1 -0
@@ -314,6 +317,7 @@ struct IntelIOMMUState {
     bool dma_translation;           /* Whether DMA translation supported */
     bool pasid;                     /* Whether to support PASID */
 
+    bool cap_finalized;             /* Whether VTD capability finalized */
     /*
      * Protects IOMMU states in general.  Currently it protects the
      * per-IOMMU IOTLB cache, and context entry cache in

VTDAddressSpace.

hw/i386/intel_iommu.c VTDAddressSpace

Quoted 5 lines
hw/i386/intel_iommu.c +0 -0
index 4c1d058ebd..be03fcbf52 100644
@@ -3819,6 +3819,47 @@ VTDAddressSpace

*vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
     return vtd_dev_as;
 }
 
+static bool vtd_sync_hw_info(IntelIOMMUState *s, struct

iommu_hw_info_vtd *vtd,

Quoted 16 lines
hw/i386/intel_iommu.c +16 -0
+                             Error **errp)
+{
+    uint64_t addr_width;
+
+    addr_width = (vtd->cap_reg >> 16) & 0x3fULL;
+    if (s->aw_bits > addr_width) {
+        error_setg(errp, "User aw-bits: %u > host address width: %lu",
+                   s->aw_bits, addr_width);
+        return false;
+    }
+
+    /* TODO: check and sync host cap/ecap into vIOMMU cap/ecap */
+
+    return true;
+}
+

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+/*
+ * virtual VT-d which wants nested needs to check the host IOMMU
+ * nesting cap info behind the assigned devices. Thus that vIOMMU
+ * could bind guest page table to host.
+ */
+static bool vtd_check_idev(IntelIOMMUState *s, IOMMUFDDevice *idev,
+                           Error **errp)
+{
+    struct iommu_hw_info_vtd vtd;
+    enum iommu_hw_info_type type =

IOMMU_HW_INFO_TYPE_INTEL_VTD;

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+
+    if (iommufd_device_get_info(idev, &type, sizeof(vtd), &vtd)) {
+        error_setg(errp, "Failed to get IOMMU capability!!!");
+        return false;
+    }

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 10 lines
hw/i386/intel_iommu.c +9 -0
+
+    if (type != IOMMU_HW_INFO_TYPE_INTEL_VTD) {
+        error_setg(errp, "IOMMU hardware is not compatible!!!");
+        return false;
+    }
+
+    return vtd_sync_hw_info(s, &vtd, errp);
+}
+
 static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int32_t

devfn,

hw/i386/intel_iommu.c static int vtd_dev_set_iommu_device(PCIBus

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
                                     IOMMUFDDevice *idev, Error **errp)
 {
@@ -3837,6 +3878,10 @@ static int vtd_dev_set_iommu_device(PCIBus

*bus, void *opaque, int32_t devfn,

hw/i386/intel_iommu.c static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int32_t devfn,

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
         return 0;
     }
 
+    if (!vtd_check_idev(s, idev, errp)) {In

In
[RFC 0/7] VIRTIO-IOMMU/VFIO: Fix host iommu geometry handling for hotplugged devices
https://lore.kernel.org/all/20240117080414.316890-1-
[email protected]/

Quoted 4 lines
Quoted 2 lines

I also attempt to pass host iommu info to the virtio-iommu but with legacy BE.

I think your patch works with iommufd BE too😊 Because iommufd BE also fills bcontainer->iova_ranges in iommufd_cdev_get_info_iova_range().

correct. I wanted to emphasize that we also have the need to pass host iommu info in legacy mode for instance. In this series you introduce an object that works with the iommufd backed but I think if we go this way we would need another one for the legacy device. So maybe introducing a base object derived into 2 ones may be the most appropriate? Maybe, given the assumption that we will use iommufd for new use cases this legacy object will implement much fewer interfaces but still.

Quoted 4 lines
Quoted 4 lines

In my case I want to pass the reserved memory regions which also model the aw.
So this is a pretty similar use case.

Quoted 8 lines

Yes.

Quoted 2 lines

Why don't we pass the pointer to an opaque iommu_hw_info instead, through the PCIIOMMUOps?

Passing iommu_hw_info is ok for this series, but we want more from IOMMUFDDevice in nesting series. I.e., allocate/free ioas/hwpt, attach/detach from hwpt, get dirty bitmap, etc. It's more flexible to let vIOMMU get what it want itself.

OK, would be interesting to define the class for this object. Worth to be introduced either in the cover letter or in the 1st patch

Eric

Quoted 104 lines
Quoted 20 lines
Quoted 10 lines
hw/i386/intel_iommu.c +3 -0
+        return -1;
+    }
+
     vtd_iommu_lock(s);
 
     vtd_idev = g_hash_table_lookup(s->vtd_iommufd_dev, &key);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4120

Quoted 9 lines
hw/i386/intel_iommu.c +2 -4
@@ -4071,10 +4116,11 @@ static void vtd_init(IntelIOMMUState *s)
 {
     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
-    memset(s->csr, 0, DMAR_REG_SIZE);
-    memset(s->wmask, 0, DMAR_REG_SIZE);
-    memset(s->w1cmask, 0, DMAR_REG_SIZE);
-    memset(s->womask, 0, DMAR_REG_SIZE);
+    /* CAP/ECAP are initialized in machine create done stage */
+    memset(s->csr + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);

Quoted 8 lines

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4121

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    memset(s->wmask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4122

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    memset(s->w1cmask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4123

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    memset(s->womask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);
This change is not documented in the commit msg.
Sorry I don't get why this is needed?

I'll doc it. Above we have one line to explain when cap/ecap are initialized. vtd_init() is called in qemu init and guest reset. In qemu init, Cap/ecap is finalized, after that we don't want cap/ecap to be changed. So we bypass change to cap/ecap here.

Quoted 42 lines

hw/i386/intel_iommu.c static bool

Quoted 23 lines
hw/i386/intel_iommu.c +6 -3
     s->root = 0;
     s->root_scalable = false;
@@ -4110,13 +4156,16 @@ static void vtd_init(IntelIOMMUState *s)
         vtd_spte_rsvd_large[3] &= ~VTD_SPTE_SNP;
     }
 
-    vtd_cap_init(s);
+    if (!s->cap_finalized) {
+        vtd_cap_init(s);
+        s->host_cap = s->cap;
+        s->host_ecap = s->ecap;
+    }
+
     vtd_reset_caches(s);
 
     /* Define registers with default values and bit semantics */
     vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0);
-    vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
-    vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
     vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0);
     vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL);
     vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0);
@@ -4241,6 +4290,12 @@ static bool

vtd_decide_config(IntelIOMMUState *s, Error **errp)

hw/i386/intel_iommu.c static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)

Quoted 12 lines
hw/i386/intel_iommu.c +6 -0
     return true;
 }
 
+static void vtd_setup_capability_reg(IntelIOMMUState *s)
+{
+    vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
+    vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
+}
+
 static int vtd_machine_done_notify_one(Object *child, void *unused)
 {
     IntelIOMMUState *iommu =

INTEL_IOMMU_DEVICE(x86_iommu_get_default());

hw/i386/intel_iommu.c static int

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
@@ -4259,6 +4314,14 @@ static int

vtd_machine_done_notify_one(Object *child, void *unused)

hw/i386/intel_iommu.c static int vtd_machine_done_notify_one(Object *child, void *unused)

Quoted 3 lines
hw/i386/intel_iommu.c +1 -0
 static void vtd_machine_done_hook(Notifier *notifier, void *unused)
 {
+    IntelIOMMUState *iommu =
Quoted 7 lines

INTEL_IOMMU_DEVICE(x86_iommu_get_default());

hw/i386/intel_iommu.c static int vtd_machine_done_notify_one(Object *child, void *unused) · line 4321

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+
+    iommu->cap = iommu->host_cap;
+    iommu->ecap = iommu->host_ecap;
+    iommu->cap_finalized = true;

I don't think you can change the defaults like this without taking care of compats (migration).

Will bump vtd_vmstate .version_id works?

Thanks
Zhenzhong

Quoted 17 lines

Thanks

Eric

hw/i386/intel_iommu.c static void vtd_realize(DeviceState *dev, Error

Quoted 7 lines
hw/i386/intel_iommu.c +3 -0
+
+    vtd_setup_capability_reg(iommu);
+
     object_child_foreach_recursive(object_get_root(),
                                    vtd_machine_done_notify_one, NULL);
 }
@@ -4292,6 +4355,7 @@ static void vtd_realize(DeviceState *dev, Error

**errp)

Quoted 6 lines
hw/i386/intel_iommu.c +1 -0
     QLIST_INIT(&s->vtd_as_with_notifiers);
     qemu_mutex_init(&s->iommu_lock);
+    s->cap_finalized = false;
     memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
                           "intel_iommu", DMAR_REG_SIZE);
     memory_region_add_subregion(get_system_memory(),
#42 lore raw

RE: [PATCH rfcv1 6/6] intel_iommu: add a framework to check and sync host IOMMU cap/ecap

Quoted 161 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 6/6] intel_iommu: add a framework to check and sync host IOMMU cap/ecap

Quoted 5 lines · Zhenzhong

Hi Eric,

Quoted 3 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 6/6] intel_iommu: add a framework to check

and

Quoted 25 lines
Quoted 25 lines

sync host IOMMU cap/ecap

Hi Zhenzhong,

Quoted 20 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

Add a framework to check and synchronize host IOMMU cap/ecap with vIOMMU cap/ecap.

Currently only stage-2 translation is supported which is backed by shadow page table on host side. So we don't need exact matching of each bit of cap/ecap between vIOMMU and host. However, we can still utilize this framework to ensure compatibility of host and vIOMMU's address width at least, i.e., vIOMMU's aw_bits <= host aw_bits, which is missed before.

When stage-1 translation is supported in future, a.k.a. scalable modern mode, we need to ensure compatibility of each bits. Some bits are user controllable, they should be checked with host side to ensure compatibility. Other bits are not, they should be synced into vIOMMU cap/ecap for compatibility.

The sequence will be:

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

vtd_cap_init() initializes iommu->cap/ecap. ---- vtd_cap_init() iommu->host_cap/ecap is initialized as iommu->cap/ecap. ---- vtd_init() iommu->host_cap/ecap is checked and updated some bits with host

cap/ecap. ---- vtd_sync_hw_info()

Quoted 1 line

iommu->cap/ecap is finalized as iommu->host_cap/ecap. ----

vtd_machine_done_hook()

Quoted 1 line

iommu->host_cap/ecap is a temporary storage to hold intermediate

value

Quoted 1 line
Quoted 1 line
Quoted 1 line

when synthesize host cap/ecap and vIOMMU's initial configured

cap/ecap.

Quoted 19 lines
Quoted 19 lines
Quoted 7 lines
---
 include/hw/i386/intel_iommu.h |  4 ++
 hw/i386/intel_iommu.c         | 78

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

Review

Quoted 3 lines
 2 files changed, 75 insertions(+), 7 deletions(-)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 7 lines

index c65fdde56f..b8abbcce12 100644

include/hw/i386/intel_iommu.h +0 -0
@@ -292,6 +292,9 @@ struct IntelIOMMUState {
     uint64_t cap;                   /* The value of capability reg */
     uint64_t ecap;                  /* The value of extended capability reg */
 
Quoted 8 lines
Quoted 8 lines

include/hw/i386/intel_iommu.h struct IntelIOMMUState { · line 317

Quoted 8 lines
include/hw/i386/intel_iommu.h +3 -0
+    uint64_t host_cap;              /* The value of host capability reg */
+    uint64_t host_ecap;             /* The value of host ext-capability reg */
+
     uint32_t context_cache_gen;     /* Should be in [1,MAX] */
     GHashTable *iotlb;              /* IOTLB */
 
@@ -314,6 +317,7 @@ struct IntelIOMMUState {
     bool dma_translation;           /* Whether DMA translation supported

*/

Quoted 32 lines
Quoted 32 lines

include/hw/i386/intel_iommu.h struct IntelIOMMUState {

Quoted 6 lines
include/hw/i386/intel_iommu.h +1 -0
     bool pasid;                     /* Whether to support PASID */
 
+    bool cap_finalized;             /* Whether VTD capability finalized */
     /*
      * Protects IOMMU states in general.  Currently it protects the
      * per-IOMMU IOTLB cache, and context entry cache in

VTDAddressSpace.

hw/i386/intel_iommu.c VTDAddressSpace

Quoted 5 lines
hw/i386/intel_iommu.c +0 -0
index 4c1d058ebd..be03fcbf52 100644
@@ -3819,6 +3819,47 @@ VTDAddressSpace

*vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
     return vtd_dev_as;
 }
 
+static bool vtd_sync_hw_info(IntelIOMMUState *s, struct

iommu_hw_info_vtd *vtd,

Quoted 14 lines
hw/i386/intel_iommu.c +14 -0
+                             Error **errp)
+{
+    uint64_t addr_width;
+
+    addr_width = (vtd->cap_reg >> 16) & 0x3fULL;
+    if (s->aw_bits > addr_width) {
+        error_setg(errp, "User aw-bits: %u > host address width: %lu",
+                   s->aw_bits, addr_width);
+        return false;
+    }
+
+    /* TODO: check and sync host cap/ecap into vIOMMU cap/ecap */
+
+    return true;
Quoted 8 lines
Quoted 8 lines

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 8 lines
hw/i386/intel_iommu.c +8 -0
+}
+
+/*
+ * virtual VT-d which wants nested needs to check the host IOMMU
+ * nesting cap info behind the assigned devices. Thus that vIOMMU
+ * could bind guest page table to host.
+ */
+static bool vtd_check_idev(IntelIOMMUState *s, IOMMUFDDevice

*idev,

Quoted 5 lines
Quoted 5 lines

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+                           Error **errp)
+{
+    struct iommu_hw_info_vtd vtd;
+    enum iommu_hw_info_type type =

IOMMU_HW_INFO_TYPE_INTEL_VTD;

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
hw/i386/intel_iommu.c +7 -0
+
+    if (iommufd_device_get_info(idev, &type, sizeof(vtd), &vtd)) {
+        error_setg(errp, "Failed to get IOMMU capability!!!");
+        return false;
+    }
+
+    if (type != IOMMU_HW_INFO_TYPE_INTEL_VTD) {
Quoted 8 lines

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 8 lines
hw/i386/intel_iommu.c +7 -0
+        error_setg(errp, "IOMMU hardware is not compatible!!!");
+        return false;
+    }
+
+    return vtd_sync_hw_info(s, &vtd, errp);
+}
+
 static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque,

int32_t

Quoted 4 lines
Quoted 4 lines

devfn,

hw/i386/intel_iommu.c static int

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
                                     IOMMUFDDevice *idev, Error **errp)
 {
@@ -3837,6 +3878,10 @@ static int

vtd_dev_set_iommu_device(PCIBus

Quoted 11 lines
Quoted 11 lines

*bus, void *opaque, int32_t devfn,

hw/i386/intel_iommu.c static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int32_t devfn,

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
         return 0;
     }
 
+    if (!vtd_check_idev(s, idev, errp)) {In

In
[RFC 0/7] VIRTIO-IOMMU/VFIO: Fix host iommu geometry handling for hotplugged devices
https://lore.kernel.org/all/20240117080414.316890-1-
[email protected]/

Quoted 4 lines
Quoted 2 lines

I also attempt to pass host iommu info to the virtio-iommu but with legacy BE.

I think your patch works with iommufd BE too😊 Because iommufd BE also fills bcontainer->iova_ranges in iommufd_cdev_get_info_iova_range().

Quoted 7 lines

correct. I wanted to emphasize that we also have the need to pass host iommu info in legacy mode for instance. In this series you introduce an object that works with the iommufd backed but I think if we go this way we would need another one for the legacy device. So maybe introducing a base object derived into 2 ones may be the most appropriate? Maybe, given the assumption that we will use iommufd for new use cases this legacy object will implement much fewer interfaces but still.

How about this:

enum IOMMU_LEGACY_DEVICE_TYPE {
IOMMU_LEGACY_VFIO_DEVICE,
IOMMU_LEGACY_VDPA_DEVICE,
}

typedef struct IOMMULegacyDevice {
    enum IOMMU_LEGACY_DEVICE_TYPE type;

    /* common field */

    union {
        ....
    }

} IOMMULegacyDevice;

typedef struct IOMMUFDDevice {
    IOMMUFDBackend *iommufd;
    uint32_t dev_id;
    uint32_t ioas_id;
} IOMMUFDDevice;

enum IOMMUDEVICE_TYPE {
IOMMUFD_DEVICE,
IOMMU_LEGACY_DEVICE,
}

struct IOMMUDevice {
    enum IOMMU_DEVICE_TYPE type;

    /* common field */
    GList *iova_ranges;

    union {
        IOMMULegacyDevice legacy_dev;
        IOMMUFDDevice idev;
    }
}
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

In my case I want to pass the reserved memory regions which also model the aw.
So this is a pretty similar use case.

Yes.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Why don't we pass the pointer to an opaque iommu_hw_info instead, through the PCIIOMMUOps?

Passing iommu_hw_info is ok for this series, but we want more from IOMMUFDDevice in nesting series. I.e., allocate/free ioas/hwpt, attach/detach from hwpt, get dirty bitmap, etc. It's more flexible to let vIOMMU get what it want itself.

OK, would be interesting to define the class for this object. Worth to be introduced either in the cover letter or in the 1st patch

Not a QOM class because we don't want it showed out through query-qmp-schema.

Thanks
Zhenzhong

Quoted 107 lines

Eric

Quoted 89 lines
Quoted 20 lines
Quoted 10 lines
hw/i386/intel_iommu.c +3 -0
+        return -1;
+    }
+
     vtd_iommu_lock(s);
 
     vtd_idev = g_hash_table_lookup(s->vtd_iommufd_dev, &key);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4120

Quoted 9 lines
hw/i386/intel_iommu.c +2 -4
@@ -4071,10 +4116,11 @@ static void vtd_init(IntelIOMMUState *s)
 {
     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
-    memset(s->csr, 0, DMAR_REG_SIZE);
-    memset(s->wmask, 0, DMAR_REG_SIZE);
-    memset(s->w1cmask, 0, DMAR_REG_SIZE);
-    memset(s->womask, 0, DMAR_REG_SIZE);
+    /* CAP/ECAP are initialized in machine create done stage */
+    memset(s->csr + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);

Quoted 8 lines

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4121

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    memset(s->wmask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4122

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    memset(s->w1cmask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4123

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    memset(s->womask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);
This change is not documented in the commit msg.
Sorry I don't get why this is needed?

I'll doc it. Above we have one line to explain when cap/ecap are initialized. vtd_init() is called in qemu init and guest reset. In qemu init, Cap/ecap is finalized, after that we don't want cap/ecap to be changed. So we bypass change to cap/ecap here.

Quoted 42 lines

hw/i386/intel_iommu.c static bool

Quoted 23 lines
hw/i386/intel_iommu.c +6 -3
     s->root = 0;
     s->root_scalable = false;
@@ -4110,13 +4156,16 @@ static void vtd_init(IntelIOMMUState *s)
         vtd_spte_rsvd_large[3] &= ~VTD_SPTE_SNP;
     }
 
-    vtd_cap_init(s);
+    if (!s->cap_finalized) {
+        vtd_cap_init(s);
+        s->host_cap = s->cap;
+        s->host_ecap = s->ecap;
+    }
+
     vtd_reset_caches(s);
 
     /* Define registers with default values and bit semantics */
     vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0);
-    vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
-    vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
     vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0);
     vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL);
     vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0);
@@ -4241,6 +4290,12 @@ static bool

vtd_decide_config(IntelIOMMUState *s, Error **errp)

hw/i386/intel_iommu.c static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)

Quoted 12 lines
hw/i386/intel_iommu.c +6 -0
     return true;
 }
 
+static void vtd_setup_capability_reg(IntelIOMMUState *s)
+{
+    vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
+    vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
+}
+
 static int vtd_machine_done_notify_one(Object *child, void *unused)
 {
     IntelIOMMUState *iommu =

INTEL_IOMMU_DEVICE(x86_iommu_get_default());

hw/i386/intel_iommu.c static int

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
@@ -4259,6 +4314,14 @@ static int

vtd_machine_done_notify_one(Object *child, void *unused)

hw/i386/intel_iommu.c static int vtd_machine_done_notify_one(Object *child, void *unused)

Quoted 3 lines
hw/i386/intel_iommu.c +1 -0
 static void vtd_machine_done_hook(Notifier *notifier, void *unused)
 {
+    IntelIOMMUState *iommu =
Quoted 7 lines

INTEL_IOMMU_DEVICE(x86_iommu_get_default());

hw/i386/intel_iommu.c static int vtd_machine_done_notify_one(Object *child, void *unused) · line 4321

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+
+    iommu->cap = iommu->host_cap;
+    iommu->ecap = iommu->host_ecap;
+    iommu->cap_finalized = true;

I don't think you can change the defaults like this without taking care of compats (migration).

Will bump vtd_vmstate .version_id works?

Thanks
Zhenzhong

Quoted 2 lines

Thanks

Quoted 8 lines
Quoted 8 lines

Eric

hw/i386/intel_iommu.c static void vtd_realize(DeviceState *dev,

Quoted 7 lines
hw/i386/intel_iommu.c +3 -0
+
+    vtd_setup_capability_reg(iommu);
+
     object_child_foreach_recursive(object_get_root(),
                                    vtd_machine_done_notify_one, NULL);
 }
@@ -4292,6 +4355,7 @@ static void vtd_realize(DeviceState *dev,

Error

Quoted 7 lines
Quoted 7 lines

**errp)

Quoted 6 lines
hw/i386/intel_iommu.c +1 -0
     QLIST_INIT(&s->vtd_as_with_notifiers);
     qemu_mutex_init(&s->iommu_lock);
+    s->cap_finalized = false;
     memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
                           "intel_iommu", DMAR_REG_SIZE);
     memory_region_add_subregion(get_system_memory(),
#43 lore raw

Re: [PATCH rfcv1 6/6] intel_iommu: add a framework to check and sync host IOMMU cap/ecap

Quoted 195 lines · Zhenzhong
Quoted 159 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 6/6] intel_iommu: add a framework to check and sync host IOMMU cap/ecap

Quoted 5 lines · Zhenzhong

Hi Eric,

Quoted 3 lines

-----Original Message-----
From: Eric Auger <[email protected]>
Subject: Re: [PATCH rfcv1 6/6] intel_iommu: add a framework to check

and

Quoted 25 lines
Quoted 25 lines

sync host IOMMU cap/ecap

Hi Zhenzhong,

Quoted 20 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

Add a framework to check and synchronize host IOMMU cap/ecap with vIOMMU cap/ecap.

Currently only stage-2 translation is supported which is backed by shadow page table on host side. So we don't need exact matching of each bit of cap/ecap between vIOMMU and host. However, we can still utilize this framework to ensure compatibility of host and vIOMMU's address width at least, i.e., vIOMMU's aw_bits <= host aw_bits, which is missed before.

When stage-1 translation is supported in future, a.k.a. scalable modern mode, we need to ensure compatibility of each bits. Some bits are user controllable, they should be checked with host side to ensure compatibility. Other bits are not, they should be synced into vIOMMU cap/ecap for compatibility.

The sequence will be:

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

vtd_cap_init() initializes iommu->cap/ecap. ---- vtd_cap_init() iommu->host_cap/ecap is initialized as iommu->cap/ecap. ---- vtd_init() iommu->host_cap/ecap is checked and updated some bits with host

cap/ecap. ---- vtd_sync_hw_info()

Quoted 1 line

iommu->cap/ecap is finalized as iommu->host_cap/ecap. ----

vtd_machine_done_hook()

Quoted 1 line

iommu->host_cap/ecap is a temporary storage to hold intermediate

value

Quoted 1 line
Quoted 1 line
Quoted 1 line

when synthesize host cap/ecap and vIOMMU's initial configured

cap/ecap.

Quoted 18 lines
Quoted 18 lines
Quoted 6 lines
---
 include/hw/i386/intel_iommu.h |  4 ++
 hw/i386/intel_iommu.c         | 78

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

Review

Quoted 3 lines
 2 files changed, 75 insertions(+), 7 deletions(-)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 7 lines

index c65fdde56f..b8abbcce12 100644

include/hw/i386/intel_iommu.h +0 -0
@@ -292,6 +292,9 @@ struct IntelIOMMUState {
     uint64_t cap;                   /* The value of capability reg */
     uint64_t ecap;                  /* The value of extended capability reg */
 
Quoted 8 lines
Quoted 8 lines

include/hw/i386/intel_iommu.h struct IntelIOMMUState { · line 317

Quoted 8 lines
include/hw/i386/intel_iommu.h +3 -0
+    uint64_t host_cap;              /* The value of host capability reg */
+    uint64_t host_ecap;             /* The value of host ext-capability reg */
+
     uint32_t context_cache_gen;     /* Should be in [1,MAX] */
     GHashTable *iotlb;              /* IOTLB */
 
@@ -314,6 +317,7 @@ struct IntelIOMMUState {
     bool dma_translation;           /* Whether DMA translation supported

*/

Quoted 32 lines
Quoted 32 lines

include/hw/i386/intel_iommu.h struct IntelIOMMUState {

Quoted 6 lines
include/hw/i386/intel_iommu.h +1 -0
     bool pasid;                     /* Whether to support PASID */
 
+    bool cap_finalized;             /* Whether VTD capability finalized */
     /*
      * Protects IOMMU states in general.  Currently it protects the
      * per-IOMMU IOTLB cache, and context entry cache in

VTDAddressSpace.

hw/i386/intel_iommu.c VTDAddressSpace

Quoted 5 lines
hw/i386/intel_iommu.c +0 -0
index 4c1d058ebd..be03fcbf52 100644
@@ -3819,6 +3819,47 @@ VTDAddressSpace

*vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
     return vtd_dev_as;
 }
 
+static bool vtd_sync_hw_info(IntelIOMMUState *s, struct

iommu_hw_info_vtd *vtd,

Quoted 14 lines
hw/i386/intel_iommu.c +14 -0
+                             Error **errp)
+{
+    uint64_t addr_width;
+
+    addr_width = (vtd->cap_reg >> 16) & 0x3fULL;
+    if (s->aw_bits > addr_width) {
+        error_setg(errp, "User aw-bits: %u > host address width: %lu",
+                   s->aw_bits, addr_width);
+        return false;
+    }
+
+    /* TODO: check and sync host cap/ecap into vIOMMU cap/ecap */
+
+    return true;
Quoted 8 lines
Quoted 8 lines

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 8 lines
hw/i386/intel_iommu.c +8 -0
+}
+
+/*
+ * virtual VT-d which wants nested needs to check the host IOMMU
+ * nesting cap info behind the assigned devices. Thus that vIOMMU
+ * could bind guest page table to host.
+ */
+static bool vtd_check_idev(IntelIOMMUState *s, IOMMUFDDevice

*idev,

Quoted 5 lines
Quoted 5 lines

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+                           Error **errp)
+{
+    struct iommu_hw_info_vtd vtd;
+    enum iommu_hw_info_type type =

IOMMU_HW_INFO_TYPE_INTEL_VTD;

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
hw/i386/intel_iommu.c +7 -0
+
+    if (iommufd_device_get_info(idev, &type, sizeof(vtd), &vtd)) {
+        error_setg(errp, "Failed to get IOMMU capability!!!");
+        return false;
+    }
+
+    if (type != IOMMU_HW_INFO_TYPE_INTEL_VTD) {
Quoted 8 lines

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 8 lines
hw/i386/intel_iommu.c +7 -0
+        error_setg(errp, "IOMMU hardware is not compatible!!!");
+        return false;
+    }
+
+    return vtd_sync_hw_info(s, &vtd, errp);
+}
+
 static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque,

int32_t

Quoted 4 lines
Quoted 4 lines

devfn,

hw/i386/intel_iommu.c static int

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
                                     IOMMUFDDevice *idev, Error **errp)
 {
@@ -3837,6 +3878,10 @@ static int

vtd_dev_set_iommu_device(PCIBus

Quoted 11 lines
Quoted 11 lines

*bus, void *opaque, int32_t devfn,

hw/i386/intel_iommu.c static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int32_t devfn,

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
         return 0;
     }
 
+    if (!vtd_check_idev(s, idev, errp)) {In

In
[RFC 0/7] VIRTIO-IOMMU/VFIO: Fix host iommu geometry handling for hotplugged devices
https://lore.kernel.org/all/20240117080414.316890-1-
[email protected]/

Quoted 4 lines
Quoted 2 lines

I also attempt to pass host iommu info to the virtio-iommu but with legacy BE.

I think your patch works with iommufd BE too😊 Because iommufd BE also fills bcontainer->iova_ranges in iommufd_cdev_get_info_iova_range().

Quoted 7 lines

correct. I wanted to emphasize that we also have the need to pass host iommu info in legacy mode for instance. In this series you introduce an object that works with the iommufd backed but I think if we go this way we would need another one for the legacy device. So maybe introducing a base object derived into 2 ones may be the most appropriate? Maybe, given the assumption that we will use iommufd for new use cases this legacy object will implement much fewer interfaces but still.

How about this:

enum IOMMU_LEGACY_DEVICE_TYPE {
IOMMU_LEGACY_VFIO_DEVICE,
IOMMU_LEGACY_VDPA_DEVICE,
}

typedef struct IOMMULegacyDevice {
    enum IOMMU_LEGACY_DEVICE_TYPE type;

    /* common field */

    union {
        ....
    }

} IOMMULegacyDevice;

typedef struct IOMMUFDDevice {
    IOMMUFDBackend *iommufd;
    uint32_t dev_id;
    uint32_t ioas_id;
} IOMMUFDDevice;

enum IOMMUDEVICE_TYPE {
IOMMUFD_DEVICE,
IOMMU_LEGACY_DEVICE,
}

Quoted 8 lines
struct IOMMUDevice {
    enum IOMMU_DEVICE_TYPE type;

    /* common field */
    GList *iova_ranges;

    union {
        IOMMULegacyDevice legacy_dev;

yeah but that's not very nice to have this LegacyDevice def in an iommufd.c file

Either we define an abstract HostAssignedDevice and derived objects for both legacy and IOMMUFD or we consider using a different API for legacy use cases (retrieving resv regions, page size mask, ...).

Eric

+int iommufd_device_get_info(IOMMUFDDevice *idev,
+ enum iommu_hw_info_type *type,
+ uint32_t len, void *data);
+void iommufd_device_init(void *_idev, size_t instance_size,
+ IOMMUFDBackend *iommufd, uint32_t dev_id);

Quoted 127 lines
        IOMMUFDDevice idev;
    }
}
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

In my case I want to pass the reserved memory regions which also model the aw.
So this is a pretty similar use case.

Yes.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Why don't we pass the pointer to an opaque iommu_hw_info instead, through the PCIIOMMUOps?

Passing iommu_hw_info is ok for this series, but we want more from IOMMUFDDevice in nesting series. I.e., allocate/free ioas/hwpt, attach/detach from hwpt, get dirty bitmap, etc. It's more flexible to let vIOMMU get what it want itself.

OK, would be interesting to define the class for this object. Worth to be introduced either in the cover letter or in the 1st patch

Not a QOM class because we don't want it showed out through query-qmp-schema.

Thanks
Zhenzhong

Quoted 104 lines

Eric

Quoted 87 lines
Quoted 18 lines
Quoted 8 lines
hw/i386/intel_iommu.c +3 -0
+        return -1;
+    }
+
     vtd_iommu_lock(s);
 
     vtd_idev = g_hash_table_lookup(s->vtd_iommufd_dev, &key);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4120

Quoted 9 lines
hw/i386/intel_iommu.c +2 -4
@@ -4071,10 +4116,11 @@ static void vtd_init(IntelIOMMUState *s)
 {
     X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
-    memset(s->csr, 0, DMAR_REG_SIZE);
-    memset(s->wmask, 0, DMAR_REG_SIZE);
-    memset(s->w1cmask, 0, DMAR_REG_SIZE);
-    memset(s->womask, 0, DMAR_REG_SIZE);
+    /* CAP/ECAP are initialized in machine create done stage */
+    memset(s->csr + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);

Quoted 8 lines

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4121

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    memset(s->wmask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4122

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    memset(s->w1cmask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4123

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    memset(s->womask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);
This change is not documented in the commit msg.
Sorry I don't get why this is needed?

I'll doc it. Above we have one line to explain when cap/ecap are initialized. vtd_init() is called in qemu init and guest reset. In qemu init, Cap/ecap is finalized, after that we don't want cap/ecap to be changed. So we bypass change to cap/ecap here.

Quoted 42 lines

hw/i386/intel_iommu.c static bool

Quoted 23 lines
hw/i386/intel_iommu.c +6 -3
     s->root = 0;
     s->root_scalable = false;
@@ -4110,13 +4156,16 @@ static void vtd_init(IntelIOMMUState *s)
         vtd_spte_rsvd_large[3] &= ~VTD_SPTE_SNP;
     }
 
-    vtd_cap_init(s);
+    if (!s->cap_finalized) {
+        vtd_cap_init(s);
+        s->host_cap = s->cap;
+        s->host_ecap = s->ecap;
+    }
+
     vtd_reset_caches(s);
 
     /* Define registers with default values and bit semantics */
     vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0);
-    vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
-    vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
     vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0);
     vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL);
     vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0);
@@ -4241,6 +4290,12 @@ static bool

vtd_decide_config(IntelIOMMUState *s, Error **errp)

hw/i386/intel_iommu.c static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)

Quoted 12 lines
hw/i386/intel_iommu.c +6 -0
     return true;
 }
 
+static void vtd_setup_capability_reg(IntelIOMMUState *s)
+{
+    vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
+    vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
+}
+
 static int vtd_machine_done_notify_one(Object *child, void *unused)
 {
     IntelIOMMUState *iommu =

INTEL_IOMMU_DEVICE(x86_iommu_get_default());

hw/i386/intel_iommu.c static int

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
@@ -4259,6 +4314,14 @@ static int

vtd_machine_done_notify_one(Object *child, void *unused)

hw/i386/intel_iommu.c static int vtd_machine_done_notify_one(Object *child, void *unused)

Quoted 3 lines
hw/i386/intel_iommu.c +1 -0
 static void vtd_machine_done_hook(Notifier *notifier, void *unused)
 {
+    IntelIOMMUState *iommu =
Quoted 7 lines

INTEL_IOMMU_DEVICE(x86_iommu_get_default());

hw/i386/intel_iommu.c static int vtd_machine_done_notify_one(Object *child, void *unused) · line 4321

Quoted 4 lines
hw/i386/intel_iommu.c +4 -0
+
+    iommu->cap = iommu->host_cap;
+    iommu->ecap = iommu->host_ecap;
+    iommu->cap_finalized = true;

I don't think you can change the defaults like this without taking care of compats (migration).

Will bump vtd_vmstate .version_id works?

Thanks
Zhenzhong

Quoted 2 lines

Thanks

Quoted 8 lines
Quoted 8 lines

Eric

hw/i386/intel_iommu.c static void vtd_realize(DeviceState *dev,

Quoted 7 lines
hw/i386/intel_iommu.c +3 -0
+
+    vtd_setup_capability_reg(iommu);
+
     object_child_foreach_recursive(object_get_root(),
                                    vtd_machine_done_notify_one, NULL);
 }
@@ -4292,6 +4355,7 @@ static void vtd_realize(DeviceState *dev,

Error

Quoted 7 lines
Quoted 7 lines

**errp)

Quoted 6 lines
hw/i386/intel_iommu.c +1 -0
     QLIST_INIT(&s->vtd_as_with_notifiers);
     qemu_mutex_init(&s->iommu_lock);
+    s->cap_finalized = false;
     memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
                           "intel_iommu", DMAR_REG_SIZE);
     memory_region_add_subregion(get_system_memory(),
#44 lore raw

Re: [PATCH rfcv1 6/6] intel_iommu: add a framework to check and sync host IOMMU cap/ecap

In reply to Zhenzhong Duan
Quoted 20 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

Add a framework to check and synchronize host IOMMU cap/ecap with vIOMMU cap/ecap.

Currently only stage-2 translation is supported which is backed by shadow page table on host side. So we don't need exact matching of each bit of cap/ecap between vIOMMU and host. However, we can still utilize this framework to ensure compatibility of host and vIOMMU's address width at least, i.e., vIOMMU's aw_bits <= host aw_bits, which is missed before.

When stage-1 translation is supported in future, a.k.a. scalable modern mode, we need to ensure compatibility of each bits. Some bits are user controllable, they should be checked with host side to ensure compatibility. Other bits are not, they should be synced into vIOMMU cap/ecap for compatibility.

The sequence will be:

Quoted 7 lines

vtd_cap_init() initializes iommu->cap/ecap. ---- vtd_cap_init() iommu->host_cap/ecap is initialized as iommu->cap/ecap. ---- vtd_init() iommu->host_cap/ecap is checked and updated some bits with host cap/ecap. ---- vtd_sync_hw_info() iommu->cap/ecap is finalized as iommu->host_cap/ecap. ---- vtd_machine_done_hook()

iommu->host_cap/ecap is a temporary storage to hold intermediate value when synthesize host cap/ecap and vIOMMU's initial configured cap/ecap.

The above "sequence" paragraph is not very clear. The patch may need to be split further.

Quoted 35 lines
---
  include/hw/i386/intel_iommu.h |  4 ++
  hw/i386/intel_iommu.c         | 78 +++++++++++++++++++++++++++++++----
  2 files changed, 75 insertions(+), 7 deletions(-)
include/hw/i386/intel_iommu.h +4 -0
index c65fdde56f..b8abbcce12 100644
@@ -292,6 +292,9 @@ struct IntelIOMMUState {
      uint64_t cap;                   /* The value of capability reg */
      uint64_t ecap;                  /* The value of extended capability reg */
 
+    uint64_t host_cap;              /* The value of host capability reg */
+    uint64_t host_ecap;             /* The value of host ext-capability reg */
+
      uint32_t context_cache_gen;     /* Should be in [1,MAX] */
      GHashTable *iotlb;              /* IOTLB */
 
@@ -314,6 +317,7 @@ struct IntelIOMMUState {
      bool dma_translation;           /* Whether DMA translation supported */
      bool pasid;                     /* Whether to support PASID */
 
+    bool cap_finalized;             /* Whether VTD capability finalized */
      /*
       * Protects IOMMU states in general.  Currently it protects the
       * per-IOMMU IOTLB cache, and context entry cache in VTDAddressSpace.
hw/i386/intel_iommu.c +0 -0
index 4c1d058ebd..be03fcbf52 100644

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, · line 3827

Quoted 9 lines
hw/i386/intel_iommu.c +6 -0
@@ -3819,6 +3819,47 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,
      return vtd_dev_as;
  }
 
+static bool vtd_sync_hw_info(IntelIOMMUState *s, struct iommu_hw_info_vtd *vtd,
+                             Error **errp)
+{
+    uint64_t addr_width;
+
+    addr_width = (vtd->cap_reg >> 16) & 0x3fULL;

Virek uses the same kind of macro in :

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

What about the + 1 ? Looks like it's missing here, according to 11.4.2 Capability Register.

Could we introduce a common macro in intel_iommu_internal.h ?

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, · line 3829

Quoted 2 lines
hw/i386/intel_iommu.c +2 -0
+    if (s->aw_bits > addr_width) {
+        error_setg(errp, "User aw-bits: %u > host address width: %lu",

I think %lu should be PRId64. This is a general comment. You should avoid %llx, %lx, etc. in the code.

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s)

Quoted 64 lines
hw/i386/intel_iommu.c +42 -4
+                   s->aw_bits, addr_width);
+        return false;
+    }
+
+    /* TODO: check and sync host cap/ecap into vIOMMU cap/ecap */
+
+    return true;
+}
+
+/*
+ * virtual VT-d which wants nested needs to check the host IOMMU
+ * nesting cap info behind the assigned devices. Thus that vIOMMU
+ * could bind guest page table to host.
+ */
+static bool vtd_check_idev(IntelIOMMUState *s, IOMMUFDDevice *idev,
+                           Error **errp)
+{
+    struct iommu_hw_info_vtd vtd;
+    enum iommu_hw_info_type type = IOMMU_HW_INFO_TYPE_INTEL_VTD;
+
+    if (iommufd_device_get_info(idev, &type, sizeof(vtd), &vtd)) {
+        error_setg(errp, "Failed to get IOMMU capability!!!");
+        return false;
+    }
+
+    if (type != IOMMU_HW_INFO_TYPE_INTEL_VTD) {
+        error_setg(errp, "IOMMU hardware is not compatible!!!");
+        return false;
+    }
+
+    return vtd_sync_hw_info(s, &vtd, errp);
+}
+
  static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int32_t devfn,
                                      IOMMUFDDevice *idev, Error **errp)
  {
@@ -3837,6 +3878,10 @@ static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque, int32_t devfn,
          return 0;
      }
 
+    if (!vtd_check_idev(s, idev, errp)) {
+        return -1;
+    }
+
      vtd_iommu_lock(s);
 
      vtd_idev = g_hash_table_lookup(s->vtd_iommufd_dev, &key);
@@ -4071,10 +4116,11 @@ static void vtd_init(IntelIOMMUState *s)
  {
      X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
-    memset(s->csr, 0, DMAR_REG_SIZE);
-    memset(s->wmask, 0, DMAR_REG_SIZE);
-    memset(s->w1cmask, 0, DMAR_REG_SIZE);
-    memset(s->womask, 0, DMAR_REG_SIZE);
+    /* CAP/ECAP are initialized in machine create done stage */
+    memset(s->csr + DMAR_GCMD_REG, 0, DMAR_REG_SIZE - DMAR_GCMD_REG);
+    memset(s->wmask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE - DMAR_GCMD_REG);
+    memset(s->w1cmask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE - DMAR_GCMD_REG);
+    memset(s->womask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE - DMAR_GCMD_REG);
 
      s->root = 0;
      s->root_scalable = false;
@@ -4110,13 +4156,16 @@ static void vtd_init(IntelIOMMUState *s)

vtd_init() is called from reset and from realize. This is redundant. reset should be enough.

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4159

Quoted 5 lines
hw/i386/intel_iommu.c +1 -1
          vtd_spte_rsvd_large[3] &= ~VTD_SPTE_SNP;
      }
 
-    vtd_cap_init(s);
+    if (!s->cap_finalized) {

ok. so this can only be done in reset.

Quoted 29 lines
hw/i386/intel_iommu.c +11 -2
+        vtd_cap_init(s);
+        s->host_cap = s->cap;
+        s->host_ecap = s->ecap;
+    }
+
      vtd_reset_caches(s);
 
      /* Define registers with default values and bit semantics */
      vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0);
-    vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
-    vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
      vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0);
      vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL);
      vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0);
@@ -4241,6 +4290,12 @@ static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
      return true;
  }
 
+static void vtd_setup_capability_reg(IntelIOMMUState *s)
+{
+    vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
+    vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
+}
+
  static int vtd_machine_done_notify_one(Object *child, void *unused)
  {
      IntelIOMMUState *iommu = INTEL_IOMMU_DEVICE(x86_iommu_get_default());
@@ -4259,6 +4314,14 @@ static int vtd_machine_done_notify_one(Object *child, void *unused)
 

hw/i386/intel_iommu.c static int vtd_machine_done_notify_one(Object *child, void *unused) · line 4324

Quoted 10 lines
hw/i386/intel_iommu.c +8 -0
  static void vtd_machine_done_hook(Notifier *notifier, void *unused)
  {
+    IntelIOMMUState *iommu = INTEL_IOMMU_DEVICE(x86_iommu_get_default());
+
+    iommu->cap = iommu->host_cap;
+    iommu->ecap = iommu->host_ecap;
+    iommu->cap_finalized = true;
+
+    vtd_setup_capability_reg(iommu);
+

This is confusing. Please split the patch better to reflect the ordering of the e/cap register settings.

Thanks,

C.

Quoted 11 lines
hw/i386/intel_iommu.c +1 -0
      object_child_foreach_recursive(object_get_root(),
                                     vtd_machine_done_notify_one, NULL);
  }
@@ -4292,6 +4355,7 @@ static void vtd_realize(DeviceState *dev, Error **errp)
 
      QLIST_INIT(&s->vtd_as_with_notifiers);
      qemu_mutex_init(&s->iommu_lock);
+    s->cap_finalized = false;
      memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
                            "intel_iommu", DMAR_REG_SIZE);
      memory_region_add_subregion(get_system_memory(),
#45 lore raw

RE: [PATCH rfcv1 6/6] intel_iommu: add a framework to check and sync host IOMMU cap/ecap

Quoted 33 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Subject: Re: [PATCH rfcv1 6/6] intel_iommu: add a framework to check and sync host IOMMU cap/ecap

Quoted 18 lines · Zhenzhong Duan

From: Yi Liu <[email protected]>

Add a framework to check and synchronize host IOMMU cap/ecap with vIOMMU cap/ecap.

Currently only stage-2 translation is supported which is backed by shadow page table on host side. So we don't need exact matching of each bit of cap/ecap between vIOMMU and host. However, we can still utilize this framework to ensure compatibility of host and vIOMMU's address width at least, i.e., vIOMMU's aw_bits <= host aw_bits, which is missed before.

When stage-1 translation is supported in future, a.k.a. scalable modern mode, we need to ensure compatibility of each bits. Some bits are user controllable, they should be checked with host side to ensure compatibility. Other bits are not, they should be synced into vIOMMU cap/ecap for compatibility.

Quoted 5 lines

The sequence will be:

vtd_cap_init() initializes iommu->cap/ecap. ---- vtd_cap_init() iommu->host_cap/ecap is initialized as iommu->cap/ecap. ---- vtd_init() iommu->host_cap/ecap is checked and updated some bits with host

cap/ecap. ---- vtd_sync_hw_info()

Quoted 1 line

iommu->cap/ecap is finalized as iommu->host_cap/ecap. ----

Quoted 8 lines

vtd_machine_done_hook()

Quoted 3 lines

iommu->host_cap/ecap is a temporary storage to hold intermediate value when synthesize host cap/ecap and vIOMMU's initial configured cap/ecap.

The above "sequence" paragraph is not very clear. The patch may need to be split further.

OK, will do.

Quoted 55 lines
Quoted 9 lines
---
  include/hw/i386/intel_iommu.h |  4 ++
  hw/i386/intel_iommu.c         | 78

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

Review

Quoted 3 lines
  2 files changed, 75 insertions(+), 7 deletions(-)
(quoted diff) +0 -0

b/include/hw/i386/intel_iommu.h

Quoted 14 lines

index c65fdde56f..b8abbcce12 100644

include/hw/i386/intel_iommu.h +3 -0
@@ -292,6 +292,9 @@ struct IntelIOMMUState {
      uint64_t cap;                   /* The value of capability reg */
      uint64_t ecap;                  /* The value of extended capability reg */
 
+    uint64_t host_cap;              /* The value of host capability reg */
+    uint64_t host_ecap;             /* The value of host ext-capability reg */
+
      uint32_t context_cache_gen;     /* Should be in [1,MAX] */
      GHashTable *iotlb;              /* IOTLB */
 

include/hw/i386/intel_iommu.h struct IntelIOMMUState { · line 323

Quoted 7 lines
include/hw/i386/intel_iommu.h +1 -0
@@ -314,6 +317,7 @@ struct IntelIOMMUState {
      bool dma_translation;           /* Whether DMA translation supported */
      bool pasid;                     /* Whether to support PASID */
 
+    bool cap_finalized;             /* Whether VTD capability finalized */
      /*
       * Protects IOMMU states in general.  Currently it protects the
       * per-IOMMU IOTLB cache, and context entry cache in

VTDAddressSpace.

hw/i386/intel_iommu.c VTDAddressSpace

Quoted 5 lines
hw/i386/intel_iommu.c +0 -0
index 4c1d058ebd..be03fcbf52 100644
@@ -3819,6 +3819,47 @@ VTDAddressSpace

*vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
      return vtd_dev_as;
  }
 
+static bool vtd_sync_hw_info(IntelIOMMUState *s, struct

iommu_hw_info_vtd *vtd,

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, · line 3827

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+                             Error **errp)
+{
+    uint64_t addr_width;
+
+    addr_width = (vtd->cap_reg >> 16) & 0x3fULL;

Virek uses the same kind of macro in :

Quoted 7 lines

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

What about the + 1 ? Looks like it's missing here, according to 11.4.2 Capability Register.

Could we introduce a common macro in intel_iommu_internal.h ?

Sure.

Quoted 7 lines

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, · line 3829

Quoted 4 lines
hw/i386/intel_iommu.c +2 -0
+    if (s->aw_bits > addr_width) {
+        error_setg(errp, "User aw-bits: %u > host address width: %lu",

I think %lu should be PRId64. This is a general comment. You should avoid %llx, %lx, etc. in the code.

Got it.

Quoted 67 lines
Quoted 10 lines
hw/i386/intel_iommu.c +9 -0
+                   s->aw_bits, addr_width);
+        return false;
+    }
+
+    /* TODO: check and sync host cap/ecap into vIOMMU cap/ecap */
+
+    return true;
+}
+

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 10 lines
hw/i386/intel_iommu.c +10 -0
+/*
+ * virtual VT-d which wants nested needs to check the host IOMMU
+ * nesting cap info behind the assigned devices. Thus that vIOMMU
+ * could bind guest page table to host.
+ */
+static bool vtd_check_idev(IntelIOMMUState *s, IOMMUFDDevice *idev,
+                           Error **errp)
+{
+    struct iommu_hw_info_vtd vtd;
+    enum iommu_hw_info_type type =

IOMMU_HW_INFO_TYPE_INTEL_VTD;

Quoted 5 lines
hw/i386/intel_iommu.c +5 -0
+
+    if (iommufd_device_get_info(idev, &type, sizeof(vtd), &vtd)) {
+        error_setg(errp, "Failed to get IOMMU capability!!!");
+        return false;
+    }

hw/i386/intel_iommu.c VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus,

Quoted 10 lines
hw/i386/intel_iommu.c +9 -0
+
+    if (type != IOMMU_HW_INFO_TYPE_INTEL_VTD) {
+        error_setg(errp, "IOMMU hardware is not compatible!!!");
+        return false;
+    }
+
+    return vtd_sync_hw_info(s, &vtd, errp);
+}
+
  static int vtd_dev_set_iommu_device(PCIBus *bus, void *opaque,

int32_t devfn,

hw/i386/intel_iommu.c static int vtd_dev_set_iommu_device(PCIBus

Quoted 3 lines
hw/i386/intel_iommu.c +0 -0
                                      IOMMUFDDevice *idev, Error **errp)
  {
@@ -3837,6 +3878,10 @@ static int vtd_dev_set_iommu_device(PCIBus

*bus, void *opaque, int32_t devfn,

Quoted 11 lines
hw/i386/intel_iommu.c +4 -0
          return 0;
      }
 
+    if (!vtd_check_idev(s, idev, errp)) {
+        return -1;
+    }
+
      vtd_iommu_lock(s);
 
      vtd_idev = g_hash_table_lookup(s->vtd_iommufd_dev, &key);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4120

Quoted 9 lines
hw/i386/intel_iommu.c +2 -4
@@ -4071,10 +4116,11 @@ static void vtd_init(IntelIOMMUState *s)
  {
      X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
-    memset(s->csr, 0, DMAR_REG_SIZE);
-    memset(s->wmask, 0, DMAR_REG_SIZE);
-    memset(s->w1cmask, 0, DMAR_REG_SIZE);
-    memset(s->womask, 0, DMAR_REG_SIZE);
+    /* CAP/ECAP are initialized in machine create done stage */
+    memset(s->csr + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4121

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    memset(s->wmask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4122

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    memset(s->w1cmask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -

DMAR_GCMD_REG);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4123

Quoted 1 line
hw/i386/intel_iommu.c +1 -0
+    memset(s->womask + DMAR_GCMD_REG, 0, DMAR_REG_SIZE -
Quoted 8 lines

DMAR_GCMD_REG);

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s)

Quoted 4 lines
hw/i386/intel_iommu.c +0 -0
      s->root = 0;
      s->root_scalable = false;
@@ -4110,13 +4156,16 @@ static void vtd_init(IntelIOMMUState *s)

vtd_init() is called from reset and from realize. This is redundant. reset should be enough.

Looks so. I'll try to see if it break anything.

Quoted 9 lines

hw/i386/intel_iommu.c static void vtd_init(IntelIOMMUState *s) · line 4159

Quoted 7 lines
hw/i386/intel_iommu.c +1 -1
          vtd_spte_rsvd_large[3] &= ~VTD_SPTE_SNP;
      }
 
-    vtd_cap_init(s);
+    if (!s->cap_finalized) {

ok. so this can only be done in reset.

Not quite get, vtd_init can be called multiple times harmlessly before machine create done. But once is enough, i.e., in reset. The call in realize looks redundant.

Quoted 37 lines

hw/i386/intel_iommu.c static bool

Quoted 16 lines
hw/i386/intel_iommu.c +5 -2
+        vtd_cap_init(s);
+        s->host_cap = s->cap;
+        s->host_ecap = s->ecap;
+    }
+
      vtd_reset_caches(s);
 
      /* Define registers with default values and bit semantics */
      vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0);
-    vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
-    vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
      vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0);
      vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL);
      vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0);
@@ -4241,6 +4290,12 @@ static bool

vtd_decide_config(IntelIOMMUState *s, Error **errp)

hw/i386/intel_iommu.c static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)

Quoted 12 lines
hw/i386/intel_iommu.c +6 -0
      return true;
  }
 
+static void vtd_setup_capability_reg(IntelIOMMUState *s)
+{
+    vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
+    vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
+}
+
  static int vtd_machine_done_notify_one(Object *child, void *unused)
  {
      IntelIOMMUState *iommu =

INTEL_IOMMU_DEVICE(x86_iommu_get_default());

hw/i386/intel_iommu.c static int

Quoted 1 line
hw/i386/intel_iommu.c +0 -0
@@ -4259,6 +4314,14 @@ static int

vtd_machine_done_notify_one(Object *child, void *unused)

hw/i386/intel_iommu.c static int vtd_machine_done_notify_one(Object *child, void *unused)

Quoted 4 lines
hw/i386/intel_iommu.c +1 -0
  static void vtd_machine_done_hook(Notifier *notifier, void *unused)
  {
+    IntelIOMMUState *iommu =

INTEL_IOMMU_DEVICE(x86_iommu_get_default());

Quoted 10 lines

hw/i386/intel_iommu.c static int vtd_machine_done_notify_one(Object *child, void *unused) · line 4324

Quoted 7 lines
hw/i386/intel_iommu.c +7 -0
+
+    iommu->cap = iommu->host_cap;
+    iommu->ecap = iommu->host_ecap;
+    iommu->cap_finalized = true;
+
+    vtd_setup_capability_reg(iommu);
+

This is confusing. Please split the patch better to reflect the ordering of the e/cap register settings.

Will do.

Thanks
Zhenzhong