From: Cindy Lu <lulu@redhat.com>
To: lulu@redhat.com, mst@redhat.com, jasowang@redhat.com,
qemu-devel@nongnu.org
Subject: [RFC 5/7] vhost-vdpa: Add the iommufd support in the map/unmap function
Date: Wed, 3 May 2023 17:13:35 +0800 [thread overview]
Message-ID: <20230503091337.2130631-6-lulu@redhat.com> (raw)
In-Reply-To: <20230503091337.2130631-1-lulu@redhat.com>
1.Change the map/umap function to legacy_map/unmap
2. Add the check for iommufd support,
a>If support iommufd, call the iommufd-related function
b>In order to use kernel's iotlb process. Still need to call
the legacy mode iotlb message, Kernel will check and
skip the legacy iotlb message if iommufd enable
Signed-off-by: Cindy Lu <lulu@redhat.com>
---
hw/virtio/vhost-vdpa.c | 56 ++++++++++++++++++++++++++++++----
include/hw/virtio/vhost-vdpa.h | 24 +++++++++++++++
2 files changed, 74 insertions(+), 6 deletions(-)
diff --git a/hw/virtio/vhost-vdpa.c b/hw/virtio/vhost-vdpa.c
index 542e003101..85240926b2 100644
--- a/hw/virtio/vhost-vdpa.c
+++ b/hw/virtio/vhost-vdpa.c
@@ -26,6 +26,7 @@
#include "cpu.h"
#include "trace.h"
#include "qapi/error.h"
+#include "sysemu/iommufd.h"
/*
* Return one past the end of the end of section. Be careful with uint64_t
@@ -76,8 +77,9 @@ static bool vhost_vdpa_listener_skipped_section(MemoryRegionSection *section,
* The caller must set asid = 0 if the device does not support asid.
* This is not an ABI break since it is set to 0 by the initializer anyway.
*/
-int vhost_vdpa_dma_map(struct vhost_vdpa *v, uint32_t asid, hwaddr iova,
- hwaddr size, void *vaddr, bool readonly)
+
+int vhost_vdpa_leagcy_dma_map(struct vhost_vdpa *v, uint32_t asid, hwaddr iova,
+ hwaddr size, void *vaddr, bool readonly)
{
struct vhost_msg_v2 msg = {};
int fd = v->device_fd;
@@ -103,13 +105,32 @@ int vhost_vdpa_dma_map(struct vhost_vdpa *v, uint32_t asid, hwaddr iova,
return ret;
}
+int vhost_vdpa_dma_map(struct vhost_vdpa *v, uint32_t asid, hwaddr iova,
+ hwaddr size, void *vaddr, bool readonly)
+{
+ struct vhost_dev *dev = v->dev;
+
+ if ((v->enable_iommufd) && (v->ops == NULL)) {
+ vdpa_backend_iommufd_ops_class_init(v);
+ }
+
+ struct vdpa_iommu_backend_ops *ops = v->ops;
+ /* inoder to reuse the iotlb prcess to in kernel, still need to call leagcy
+ mode mapping but in kernel , the leagcy mode mapping was replace by
+ iommufd*/
+ if (v->enable_iommufd) {
+ ops->dma_map(dev, asid, iova, size, vaddr, readonly);
+ }
+ return vhost_vdpa_leagcy_dma_map(v, asid, iova, size, vaddr, readonly);
+}
/*
* The caller must set asid = 0 if the device does not support asid.
* This is not an ABI break since it is set to 0 by the initializer anyway.
*/
-int vhost_vdpa_dma_unmap(struct vhost_vdpa *v, uint32_t asid, hwaddr iova,
- hwaddr size)
+
+int vhost_vdpa_leagcy_dma_unmap(struct vhost_vdpa *v, uint32_t asid,
+ hwaddr iova, hwaddr size)
{
struct vhost_msg_v2 msg = {};
int fd = v->device_fd;
@@ -132,6 +153,26 @@ int vhost_vdpa_dma_unmap(struct vhost_vdpa *v, uint32_t asid, hwaddr iova,
return ret;
}
+int vhost_vdpa_dma_unmap(struct vhost_vdpa *v, uint32_t asid, hwaddr iova,
+ hwaddr size)
+{
+ struct vhost_dev *dev = v->dev;
+
+ if ((v->enable_iommufd) && (v->ops == NULL)) {
+ vdpa_backend_iommufd_ops_class_init(v);
+ }
+
+
+ /* inoder to reuse the iotlb prcess to in kernel, still need to call leagcy
+ mode mapping but in kernel , the leagcy mode mapping was replace by
+ iommufd*/
+ if (v->enable_iommufd) {
+ struct vdpa_iommu_backend_ops *ops = v->ops;
+
+ ops->dma_unmap(dev, asid, iova, size);
+ }
+ return vhost_vdpa_leagcy_dma_unmap(v, asid, iova, size);
+}
static void vhost_vdpa_listener_begin_batch(struct vhost_vdpa *v)
{
@@ -423,13 +464,14 @@ static void vhost_vdpa_init_svq(struct vhost_dev *hdev, struct vhost_vdpa *v)
v->shadow_vqs = g_steal_pointer(&shadow_vqs);
}
-
+int g_iommufd;
static int vhost_vdpa_init(struct vhost_dev *dev, void *opaque, Error **errp)
{
struct vhost_vdpa *v;
assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_VDPA);
trace_vhost_vdpa_init(dev, opaque);
int ret;
+ printf("[%s] %d called\n", __func__, __LINE__);
/*
* Similar to VFIO, we end up pinning all guest memory and have to
@@ -580,7 +622,9 @@ static int vhost_vdpa_cleanup(struct vhost_dev *dev)
vhost_vdpa_host_notifiers_uninit(dev, dev->nvqs);
memory_listener_unregister(&v->listener);
vhost_vdpa_svq_cleanup(dev);
-
+ if (vhost_vdpa_first_dev(dev)) {
+ v->ops->detach_device(v);
+ }
dev->opaque = NULL;
ram_block_discard_disable(false);
diff --git a/include/hw/virtio/vhost-vdpa.h b/include/hw/virtio/vhost-vdpa.h
index 309d4ffc70..aa0e3ed65b 100644
--- a/include/hw/virtio/vhost-vdpa.h
+++ b/include/hw/virtio/vhost-vdpa.h
@@ -55,6 +55,10 @@ typedef struct vhost_vdpa {
void *shadow_vq_ops_opaque;
struct vhost_dev *dev;
VhostVDPAHostNotifier notifier[VIRTIO_QUEUE_MAX];
+ /*iommufd related*/
+ struct vdpa_iommu_backend_ops *ops;
+ bool enable_iommufd;
+
} VhostVDPA;
@@ -76,9 +80,29 @@ typedef struct vdpa_iommufd {
int vhost_vdpa_get_iova_range(int fd, struct vhost_vdpa_iova_range *iova_range);
+int vhost_vdpa_leagcy_dma_map(struct vhost_vdpa *v, uint32_t asid, hwaddr iova,
+ hwaddr size, void *vaddr, bool readonly);
+int vhost_vdpa_leagcy_dma_unmap(struct vhost_vdpa *v, uint32_t asid,
+ hwaddr iova, hwaddr size);
+
int vhost_vdpa_dma_map(struct vhost_vdpa *v, uint32_t asid, hwaddr iova,
hwaddr size, void *vaddr, bool readonly);
int vhost_vdpa_dma_unmap(struct vhost_vdpa *v, uint32_t asid, hwaddr iova,
hwaddr size);
+struct vdpa_iommu_backend_ops {
+ /*< private >*/
+ ObjectClass parent_class;
+ int (*dma_map)(struct vhost_dev *dev, uint32_t asid, hwaddr iova,
+ hwaddr size, void *vaddr, bool readonly);
+ int (*dma_unmap)(struct vhost_dev *dev, uint32_t asid, hwaddr iova,
+ hwaddr size);
+ int (*attach_device)(struct vhost_vdpa *dev, AddressSpace *as,
+ Error **errp);
+ void (*detach_device)(struct vhost_vdpa *dev);
+ int (*reset)(VDPAIOMMUFDState *vdpa_iommufd);
+};
+
+void vdpa_backend_iommufd_ops_class_init(struct vhost_vdpa *v);
+
#endif
--
2.34.3
next prev parent reply other threads:[~2023-05-03 9:17 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-03 9:13 [RFC 0/7] vhost-vdpa: add support for iommufd Cindy Lu
2023-05-03 9:13 ` [RFC 1/7] vhost: introduce new UAPI to support IOMMUFD Cindy Lu
2023-05-03 9:13 ` [RFC 2/7] qapi: support iommufd in vdpa Cindy Lu
2023-05-03 9:13 ` [RFC 3/7] virtio : add a ptr for vdpa_iommufd in VirtIODevice Cindy Lu
2023-05-03 9:13 ` [RFC 4/7] net/vhost-vdpa: Add the check for iommufd Cindy Lu
2023-05-03 9:13 ` Cindy Lu [this message]
2023-05-03 9:13 ` [RFC 6/7] vhost-vdpa: init iommufd function in vhost_vdpa start Cindy Lu
2023-05-03 9:13 ` [RFC 7/7] vhost-vdpa-iommufd: Add iommufd support for vdpa Cindy Lu
2023-05-05 3:29 ` [RFC 0/7] vhost-vdpa: add support for iommufd Jason Wang
2023-05-05 6:29 ` Cindy Lu
2023-06-05 5:41 ` Michael S. Tsirkin
2023-06-05 8:04 ` Cindy Lu
2023-09-13 13:31 ` Michael S. Tsirkin
2023-09-14 5:44 ` Cindy Lu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230503091337.2130631-6-lulu@redhat.com \
--to=lulu@redhat.com \
--cc=jasowang@redhat.com \
--cc=mst@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).