This adds the pasid attach/detach uAPIs for userspace to attach/detach a PASID of a device to/from a given ioas/hwpt. Only vfio-pci driver is enabled in this series. After this series, PASID-capable devices bound with vfio-pci can report PASID capability to userspace and VM to enable PASID usages like Shared Virtual Addressing (SVA). This series first adds the helpers for pasid attach in vfio core and then adds the device cdev ioctls for pasid attach/detach, finally exposing the device PASID capability to the user. It depends on the iommufd pasid attach/detach series [1]. A userspace VMM is supposed to get the details of the device's PASID capability and assemble a virtual PASID capability in a proper offset in the virtual PCI configuration space. While it is still an open on how to get the available offsets. Devices may have hidden bits that are not in the PCI cap chain. For now, there are two options to get the available offsets.[2] - Report the available offsets via ioctl. This requires device-specific logic to provide available offsets. e.g., vfio-pci variant driver. Or may the device provide the available offset by DVSEC. - Store the available offsets in a static table in userspace VMM. VMM gets the empty offsets from this table. Since there was no clear answer to it, I have not made much change on this in this version. Wish we could have more discussions on this. The completed code can be found at [3], tested with a hacky Qemu branch [4]. [1] https://lore.kernel.org/linux-iommu/20240412081516.31168-1-yi.l.liu@xxxxxxxxx/ [2] https://lore.kernel.org/kvm/b3e07591-8ebc-4924-85fe-29a46fc73d78@xxxxxxxxx/ [3] https://github.com/yiliu1765/iommufd/tree/iommufd_pasid [4] https://github.com/yiliu1765/qemu/tree/wip/zhenzhong/iommufd_nesting_rfcv2-test-pasid Change log: v2: - Use IDA to track if PASID is attached or not in VFIO. (Jason) - Fix the issue of calling pasid_at[de]tach_ioas callback unconditionally (Alex) - Fix the wrong data copy in vfio_df_ioctl_pasid_detach_pt() (Zhenzhong) - Minor tweaks in comments (Kevin) v1: https://lore.kernel.org/kvm/20231127063909.129153-1-yi.l.liu@xxxxxxxxx/ - Report PASID capability via VFIO_DEVICE_FEATURE (Alex) rfc: https://lore.kernel.org/linux-iommu/20230926093121.18676-1-yi.l.liu@xxxxxxxxx/ Regards, Yi Liu Yi Liu (4): ida: Add ida_get_lowest() vfio-iommufd: Support pasid [at|de]tach for physical VFIO devices vfio: Add VFIO_DEVICE_PASID_[AT|DE]TACH_IOMMUFD_PT vfio: Report PASID capability via VFIO_DEVICE_FEATURE ioctl drivers/vfio/device_cdev.c | 51 +++++++++++++++++++++++ drivers/vfio/iommufd.c | 60 +++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci.c | 2 + drivers/vfio/pci/vfio_pci_core.c | 50 +++++++++++++++++++++++ drivers/vfio/vfio.h | 4 ++ drivers/vfio/vfio_main.c | 8 ++++ include/linux/idr.h | 1 + include/linux/vfio.h | 11 +++++ include/uapi/linux/vfio.h | 69 ++++++++++++++++++++++++++++++++ lib/idr.c | 67 +++++++++++++++++++++++++++++++ 10 files changed, 323 insertions(+) -- 2.34.1