Move and rename vhost_backend_update_device_iotlb(), vhost_backend_invalidate_device_iotlb(), and vhost_backend_handle_iotlb_msg() from vhost-backend.c to vhost.c. vhost-backend.c is actually about vhost-kernel backend. But these functions are shared with vhost-user, so let's move them into generic place. Moreover, two of three functions becomes static as they are used only in vhost.c.
Signed-off-by: Vladimir Sementsov-Ogievskiy <[email protected]> --- hw/virtio/vhost-backend.c | 82 +---------------------------- hw/virtio/vhost-user.c | 2 +- hw/virtio/vhost.c | 85 +++++++++++++++++++++++++++++-- include/hw/virtio/vhost-backend.h | 11 ---- include/hw/virtio/vhost.h | 2 + 5 files changed, 86 insertions(+), 96 deletions(-) diff --git a/hw/virtio/vhost-backend.c b/hw/virtio/vhost-backend.c index 4367db0d95..fea88afc39 100644 --- a/hw/virtio/vhost-backend.c +++ b/hw/virtio/vhost-backend.c @@ -298,7 +298,7 @@ static void vhost_kernel_iotlb_read(void *opaque) break; } - vhost_backend_handle_iotlb_msg(dev, &msg.iotlb); + vhost_handle_iotlb_msg(dev, &msg.iotlb); } } else { struct vhost_msg msg; @@ -313,7 +313,7 @@ static void vhost_kernel_iotlb_read(void *opaque) break; } - vhost_backend_handle_iotlb_msg(dev, &msg.iotlb); + vhost_handle_iotlb_msg(dev, &msg.iotlb); } } } @@ -392,81 +392,3 @@ const VhostOps kernel_ops = { .vhost_send_device_iotlb_msg = vhost_kernel_send_device_iotlb_msg, }; #endif - -int vhost_backend_update_device_iotlb(struct vhost_dev *dev, - uint64_t iova, uint64_t uaddr, - uint64_t len, - IOMMUAccessFlags perm) -{ - struct vhost_iotlb_msg imsg; - - imsg.iova = iova; - imsg.uaddr = uaddr; - imsg.size = len; - imsg.type = VHOST_IOTLB_UPDATE; - - switch (perm) { - case IOMMU_RO: - imsg.perm = VHOST_ACCESS_RO; - break; - case IOMMU_WO: - imsg.perm = VHOST_ACCESS_WO; - break; - case IOMMU_RW: - imsg.perm = VHOST_ACCESS_RW; - break; - default: - return -EINVAL; - } - - if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg) - return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg); - - return -ENODEV; -} - -int vhost_backend_invalidate_device_iotlb(struct vhost_dev *dev, - uint64_t iova, uint64_t len) -{ - struct vhost_iotlb_msg imsg; - - imsg.iova = iova; - imsg.size = len; - imsg.type = VHOST_IOTLB_INVALIDATE; - - if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg) - return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg); - - return -ENODEV; -} - -int vhost_backend_handle_iotlb_msg(struct vhost_dev *dev, - struct vhost_iotlb_msg *imsg) -{ - int ret = 0; - - if (unlikely(!dev->vdev)) { - error_report("Unexpected IOTLB message when virtio device is stopped"); - return -EINVAL; - } - - switch (imsg->type) { - case VHOST_IOTLB_MISS: - ret = vhost_device_iotlb_miss(dev, imsg->iova, - imsg->perm != VHOST_ACCESS_RO); - break; - case VHOST_IOTLB_ACCESS_FAIL: - /* FIXME: report device iotlb error */ - error_report("Access failure IOTLB message type not supported"); - ret = -ENOTSUP; - break; - case VHOST_IOTLB_UPDATE: - case VHOST_IOTLB_INVALIDATE: - default: - error_report("Unexpected IOTLB message type"); - ret = -EINVAL; - break; - } - - return ret; -} diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index e820c5cb93..c750b32617 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -1906,7 +1906,7 @@ static gboolean backend_read(QIOChannel *ioc, GIOCondition condition, switch (hdr.request) { case VHOST_USER_BACKEND_IOTLB_MSG: - ret = vhost_backend_handle_iotlb_msg(dev, &payload.iotlb); + ret = vhost_handle_iotlb_msg(dev, &payload.iotlb); break; case VHOST_USER_BACKEND_CONFIG_CHANGE_MSG: ret = vhost_user_backend_handle_config_change(dev); diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index 9bf932eaf9..3ee353dce0 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -916,14 +916,92 @@ static void vhost_region_addnop(MemoryListener *listener, vhost_region_add_section(dev, section); } +static int vhost_update_device_iotlb(struct vhost_dev *dev, + uint64_t iova, uint64_t uaddr, + uint64_t len, + IOMMUAccessFlags perm) +{ + struct vhost_iotlb_msg imsg; + + imsg.iova = iova; + imsg.uaddr = uaddr; + imsg.size = len; + imsg.type = VHOST_IOTLB_UPDATE; + + switch (perm) { + case IOMMU_RO: + imsg.perm = VHOST_ACCESS_RO; + break; + case IOMMU_WO: + imsg.perm = VHOST_ACCESS_WO; + break; + case IOMMU_RW: + imsg.perm = VHOST_ACCESS_RW; + break; + default: + return -EINVAL; + } + + if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg) { + return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg); + } + + return -ENODEV; +} + +static int vhost_invalidate_device_iotlb(struct vhost_dev *dev, + uint64_t iova, uint64_t len) +{ + struct vhost_iotlb_msg imsg; + + imsg.iova = iova; + imsg.size = len; + imsg.type = VHOST_IOTLB_INVALIDATE; + + if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg) { + return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg); + } + + return -ENODEV; +} + +int vhost_handle_iotlb_msg(struct vhost_dev *dev, struct vhost_iotlb_msg *imsg) +{ + int ret = 0; + + if (unlikely(!dev->vdev)) { + error_report("Unexpected IOTLB message when virtio device is stopped"); + return -EINVAL; + } + + switch (imsg->type) { + case VHOST_IOTLB_MISS: + ret = vhost_device_iotlb_miss(dev, imsg->iova, + imsg->perm != VHOST_ACCESS_RO); + break; + case VHOST_IOTLB_ACCESS_FAIL: + /* FIXME: report device iotlb error */ + error_report("Access failure IOTLB message type not supported"); + ret = -ENOTSUP; + break; + case VHOST_IOTLB_UPDATE: + case VHOST_IOTLB_INVALIDATE: + default: + error_report("Unexpected IOTLB message type"); + ret = -EINVAL; + break; + } + + return ret; +} + static void vhost_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) { struct vhost_iommu *iommu = container_of(n, struct vhost_iommu, n); struct vhost_dev *hdev = iommu->hdev; hwaddr iova = iotlb->iova + iommu->iommu_offset; - if (vhost_backend_invalidate_device_iotlb(hdev, iova, - iotlb->addr_mask + 1)) { + if (vhost_invalidate_device_iotlb(hdev, iova, iotlb->addr_mask + 1)) { error_report("Fail to invalidate device iotlb"); } } @@ -1306,8 +1384,7 @@ int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write) len = MIN(iotlb.addr_mask + 1, len); iova = iova & ~iotlb.addr_mask; - ret = vhost_backend_update_device_iotlb(dev, iova, uaddr, - len, iotlb.perm); + ret = vhost_update_device_iotlb(dev, iova, uaddr, len, iotlb.perm); if (ret) { trace_vhost_iotlb_miss(dev, 4); error_report("Fail to update device iotlb"); diff --git a/include/hw/virtio/vhost-backend.h b/include/hw/virtio/vhost-backend.h index ff94fa1734..57497e197a 100644 --- a/include/hw/virtio/vhost-backend.h +++ b/include/hw/virtio/vhost-backend.h @@ -222,17 +222,6 @@ typedef struct VhostOps { vhost_check_device_state_op vhost_check_device_state; } VhostOps; -int vhost_backend_update_device_iotlb(struct vhost_dev *dev, - uint64_t iova, uint64_t uaddr, - uint64_t len, - IOMMUAccessFlags perm); - -int vhost_backend_invalidate_device_iotlb(struct vhost_dev *dev, - uint64_t iova, uint64_t len); - -int vhost_backend_handle_iotlb_msg(struct vhost_dev *dev, - struct vhost_iotlb_msg *imsg); - int vhost_user_gpu_set_socket(struct vhost_dev *dev, int fd); int vhost_user_get_shared_object(struct vhost_dev *dev, unsigned char *uuid, diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h index 54862f9b44..b292e9f0fe 100644 --- a/include/hw/virtio/vhost.h +++ b/include/hw/virtio/vhost.h @@ -403,6 +403,8 @@ int vhost_dev_set_inflight(struct vhost_dev *dev, int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, struct vhost_inflight *inflight); bool vhost_dev_has_iommu(struct vhost_dev *dev); +int vhost_handle_iotlb_msg(struct vhost_dev *dev, struct vhost_iotlb_msg *imsg); + static inline bool vhost_dev_has_feature(struct vhost_dev *dev, uint64_t feature) -- 2.52.0
