From: Manish Honap Add the vfio-cxl module that vfio-pci-core loads on demand for a CXL Type-2 device, registering its callbacks at module_init. Its reset op runs the PCI core's reset methods until vfio-cxl serves the device memory. vfio-cxl is a feature of vfio-pci-core rather than a variant driver, so its Kconfig sources above the variant drivers and its objects link right after vfio-pci-core while still being loaded on demand: request_module("vfio-cxl") finds it by module name. Assisted-by: LLM Signed-off-by: Manish Honap --- MAINTAINERS | 7 +++ drivers/vfio/pci/Kconfig | 2 + drivers/vfio/pci/Makefile | 2 + drivers/vfio/pci/cxl/Kconfig | 11 ++++ drivers/vfio/pci/cxl/Makefile | 3 + drivers/vfio/pci/cxl/vfio_cxl_core.c | 82 ++++++++++++++++++++++++++++ 6 files changed, 107 insertions(+) create mode 100644 drivers/vfio/pci/cxl/Kconfig create mode 100644 drivers/vfio/pci/cxl/Makefile create mode 100644 drivers/vfio/pci/cxl/vfio_cxl_core.c diff --git a/MAINTAINERS b/MAINTAINERS index c3aef3dce68c..8ac0a91ce647 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -28636,6 +28636,13 @@ L: kvm@vger.kernel.org S: Maintained F: drivers/vfio/cdx/* +VFIO CXL DRIVER +M: Manish Honap +L: kvm@vger.kernel.org +L: linux-cxl@vger.kernel.org +S: Supported +F: drivers/vfio/pci/cxl/ + VFIO DRIVER M: Alex Williamson L: kvm@vger.kernel.org diff --git a/drivers/vfio/pci/Kconfig b/drivers/vfio/pci/Kconfig index 296bf01e185e..4cd6acd36053 100644 --- a/drivers/vfio/pci/Kconfig +++ b/drivers/vfio/pci/Kconfig @@ -58,6 +58,8 @@ config VFIO_PCI_ZDEV_KVM config VFIO_PCI_DMABUF def_bool y if VFIO_PCI_CORE && PCI_P2PDMA && DMA_SHARED_BUFFER +source "drivers/vfio/pci/cxl/Kconfig" + source "drivers/vfio/pci/mlx5/Kconfig" source "drivers/vfio/pci/ism/Kconfig" diff --git a/drivers/vfio/pci/Makefile b/drivers/vfio/pci/Makefile index 6138f1bf241d..b65117d8d463 100644 --- a/drivers/vfio/pci/Makefile +++ b/drivers/vfio/pci/Makefile @@ -5,6 +5,8 @@ vfio-pci-core-$(CONFIG_VFIO_PCI_ZDEV_KVM) += vfio_pci_zdev.o vfio-pci-core-$(CONFIG_VFIO_PCI_DMABUF) += vfio_pci_dmabuf.o obj-$(CONFIG_VFIO_PCI_CORE) += vfio-pci-core.o +obj-$(CONFIG_VFIO_CXL) += cxl/ + vfio-pci-y := vfio_pci.o vfio-pci-$(CONFIG_VFIO_PCI_IGD) += vfio_pci_igd.o obj-$(CONFIG_VFIO_PCI) += vfio-pci.o diff --git a/drivers/vfio/pci/cxl/Kconfig b/drivers/vfio/pci/cxl/Kconfig new file mode 100644 index 000000000000..1392fa4ed15d --- /dev/null +++ b/drivers/vfio/pci/cxl/Kconfig @@ -0,0 +1,11 @@ +# SPDX-License-Identifier: GPL-2.0-only +config VFIO_CXL + tristate "VFIO support for CXL Type-2 devices" + depends on CXL_BUS && CXL_MEM + select VFIO_PCI_CORE + help + Add CXL HDM handling to vfio-pci so a CXL Type-2 device, such as a + CXL-attached accelerator, can be assigned to a virtual machine. + vfio-pci loads this module on demand when it binds a CXL device. + + If you don't know what to do here, say N. diff --git a/drivers/vfio/pci/cxl/Makefile b/drivers/vfio/pci/cxl/Makefile new file mode 100644 index 000000000000..8501bf626108 --- /dev/null +++ b/drivers/vfio/pci/cxl/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_VFIO_CXL) += vfio-cxl.o +vfio-cxl-y := vfio_cxl_core.o diff --git a/drivers/vfio/pci/cxl/vfio_cxl_core.c b/drivers/vfio/pci/cxl/vfio_cxl_core.c new file mode 100644 index 000000000000..ee49d68d689c --- /dev/null +++ b/drivers/vfio/pci/cxl/vfio_cxl_core.c @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * VFIO support for CXL Type-2 devices. + * + * Copyright (c) 2026 NVIDIA Corporation & Affiliates + */ + +#include +#include +#include + +static int vfio_cxl_init_device(struct vfio_pci_core_device *vdev) +{ + return 0; +} + +static void vfio_cxl_release_device(struct vfio_pci_core_device *vdev) +{ +} + +static int vfio_cxl_open_device(struct vfio_pci_core_device *vdev) +{ + return 0; +} + +static void vfio_cxl_close_device(struct vfio_pci_core_device *vdev) +{ +} + +static void vfio_cxl_reset_prepare(struct vfio_pci_core_device *vdev) +{ +} + +static int vfio_cxl_reset_done(struct vfio_pci_core_device *vdev) +{ + return 0; +} + +/* + * Until vfio-cxl serves the device memory, reset through the PCI core's + * reset methods, which run CXL Reset ahead of FLR. Restore the PCI state + * here rather than through pci_reset_function_locked(): its reset_prepare + * and reset_done callbacks take memory_lock, which the caller already holds. + * The caller has saved the state, or on close loaded the open-time copy. + */ +static int vfio_cxl_reset(struct vfio_pci_core_device *vdev) +{ + struct pci_dev *pdev = vdev->pdev; + int ret; + + ret = __pci_reset_function_locked(pdev); + pci_restore_state(pdev); + + return ret; +} + +static const struct vfio_cxl_ops vfio_cxl_ops = { + .init = vfio_cxl_init_device, + .release = vfio_cxl_release_device, + .open_device = vfio_cxl_open_device, + .close_device = vfio_cxl_close_device, + .reset_prepare = vfio_cxl_reset_prepare, + .reset_done = vfio_cxl_reset_done, + .reset = vfio_cxl_reset, + .owner = THIS_MODULE, +}; + +static int __init vfio_cxl_init(void) +{ + return vfio_pci_core_register_cxl_ops(&vfio_cxl_ops); +} + +static void __exit vfio_cxl_exit(void) +{ + vfio_pci_core_unregister_cxl_ops(&vfio_cxl_ops); +} + +module_init(vfio_cxl_init); +module_exit(vfio_cxl_exit); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("VFIO support for CXL Type-2 devices"); -- 2.53.0