2 new class functions are pasid_[at|de]tach_hwpt(). They are used to attach/detach pasid to/from hwpt. VFIO and VDPA can have different implementions, so implementation will be in sub-class instead of HostIOMMUDeviceIOMMUFD, e.g., in HostIOMMUDeviceIOMMUFDVFIO.
Signed-off-by: Yi Liu <[email protected]> Signed-off-by: Zhenzhong Duan <[email protected]> --- hw/vfio/iommufd.c | 47 ++++++++++++++++++++++++++++++++++++++++++++ hw/vfio/trace-events | 2 ++ 2 files changed, 49 insertions(+) diff --git a/hw/vfio/iommufd.c b/hw/vfio/iommufd.c index 131612eb83..96735f1fc8 100644 --- a/hw/vfio/iommufd.c +++ b/hw/vfio/iommufd.c @@ -931,6 +931,51 @@ host_iommu_device_iommufd_vfio_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev, return iommufd_cdev_detach_ioas_hwpt(vbasedev, errp); } +static bool +host_iommu_device_iommufd_vfio_pasid_attach_hwpt(HostIOMMUDeviceIOMMUFD *idev, + uint32_t pasid, + uint32_t hwpt_id, + Error **errp) +{ + VFIODevice *vbasedev = HOST_IOMMU_DEVICE(idev)->agent; + struct vfio_device_attach_iommufd_pt attach = { + .argsz = sizeof(attach), + .flags = VFIO_DEVICE_ATTACH_PASID, + .pasid = pasid, + .pt_id = hwpt_id, + }; + + if (ioctl(vbasedev->fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach)) { + error_setg_errno(errp, errno, "error attach %s pasid %d to id=%d", + vbasedev->name, pasid, hwpt_id); + return false; + } + + trace_hiod_iommufd_vfio_pasid_attach_hwpt(vbasedev->name, pasid, hwpt_id); + return true; +} + +static bool +host_iommu_device_iommufd_vfio_pasid_detach_hwpt(HostIOMMUDeviceIOMMUFD *idev, + uint32_t pasid, Error **errp) +{ + VFIODevice *vbasedev = HOST_IOMMU_DEVICE(idev)->agent; + struct vfio_device_detach_iommufd_pt detach = { + .argsz = sizeof(detach), + .pasid = pasid, + .flags = VFIO_DEVICE_DETACH_PASID, + }; + + if (ioctl(vbasedev->fd, VFIO_DEVICE_DETACH_IOMMUFD_PT, &detach)) { + error_setg_errno(errp, errno, "detach %s pasid %d failed", + vbasedev->name, pasid); + return false; + } + + trace_hiod_iommufd_vfio_pasid_detach_hwpt(vbasedev->name, pasid); + return true; +} + static bool hiod_iommufd_vfio_realize(HostIOMMUDevice *hiod, void *opaque, Error **errp) { @@ -993,6 +1038,8 @@ static void hiod_iommufd_vfio_class_init(ObjectClass *oc, const void *data) idevc->attach_hwpt = host_iommu_device_iommufd_vfio_attach_hwpt; idevc->detach_hwpt = host_iommu_device_iommufd_vfio_detach_hwpt; + idevc->pasid_attach_hwpt = host_iommu_device_iommufd_vfio_pasid_attach_hwpt; + idevc->pasid_detach_hwpt = host_iommu_device_iommufd_vfio_pasid_detach_hwpt; }; static const TypeInfo types[] = { diff --git a/hw/vfio/trace-events b/hw/vfio/trace-events index 846e3625c5..2af6f46014 100644 --- a/hw/vfio/trace-events +++ b/hw/vfio/trace-events @@ -188,6 +188,8 @@ iommufd_cdev_fail_attach_existing_container(const char *msg) " %s" iommufd_cdev_alloc_ioas(int iommufd, int ioas_id) " [iommufd=%d] new IOMMUFD container with ioasid=%d" iommufd_cdev_device_info(char *name, int devfd, int num_irqs, int num_regions, int flags) " %s (%d) num_irqs=%d num_regions=%d flags=%d" iommufd_cdev_pci_hot_reset_dep_devices(int domain, int bus, int slot, int function, int dev_id) "\t%04x:%02x:%02x.%x devid %d" +hiod_iommufd_vfio_pasid_attach_hwpt(const char *name, uint32_t paisd, uint32_t hwpt_id) " Successfully attached device %s pasid %d to id=%d" +hiod_iommufd_vfio_pasid_detach_hwpt(const char *name, uint32_t paisd) " Successfully detached device %s pasid %d" # cpr-iommufd.c vfio_cpr_find_device(uint32_t ioas_id, int devid, uint32_t hwpt_id) "ioas_id %u, devid %d, hwpt_id %u" -- 2.47.3
