[RFC v2 08/15] vfio/cxl: discover precommitted CXL region

mhonap
2025-12-09T16:52:19Z

From: Zhi Wang <zhiw@nvidia.com>

A type-2 device can have precommitted CXL region that is configured by
BIOS. Before letting a VFIO CXL variant driver create a new CXL region,
the VFIO CXL core first needs to discover the precommited CXL region.

Discover the precommited CXL region when enabling CXL devices.

Signed-off-by: Zhi Wang <zhiw@nvidia.com>
Signed-off-by: Manish Honap <mhonap@nvidia.com>
---
drivers/vfio/pci/vfio_cxl_core.c | 29 +++++++++++++++++++++++++++--
include/linux/vfio_pci_core.h | 1 +
2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/drivers/vfio/pci/vfio_cxl_core.c b/drivers/vfio/pci/vfio_cxl_core.c
index 35d95de47fa8..099d35866a39 100644
--- a/drivers/vfio/pci/vfio_cxl_core.c
+++ b/drivers/vfio/pci/vfio_cxl_core.c
@@ -82,11 +82,16 @@ static void disable_cxl(struct vfio_cxl_core_device *cxl)
{
struct vfio_cxl *cxl_core = cxl->cxl_core;

- WARN_ON(cxl_core->region.region);
-
if (!cxl->hdm_count)
return;

+ if (cxl_core->region.precommitted) {
+ kfree(cxl_core->region.region);
+ cxl_core->region.region = NULL;
+ }
+
+ WARN_ON(cxl_core->region.region);
+
if (cxl_core->cxled) {
cxl_decoder_detach(NULL, cxl_core->cxled, 0, DETACH_INVALIDATE);
cxl_dpa_free(cxl_core->cxled);
@@ -96,6 +101,24 @@ static void disable_cxl(struct vfio_cxl_core_device *cxl)
cxl_put_root_decoder(cxl_core->cxlrd);
}

+static void discover_precommitted_region(struct vfio_cxl_core_device *cxl)
+{
+ struct cxl_region **cxlrs = NULL;
+ struct vfio_cxl *cxl_core = cxl->cxl_core;
+ int num, ret;
+
+ ret = cxl_get_committed_regions(cxl_core->cxlmd, &cxlrs, &num);
+ if (ret || !cxlrs) {
+ kfree(cxlrs);
+ return;
+ }
+
+ WARN_ON(num > 1);
+
+ cxl_core->region.region = cxlrs[0];
+ cxl_core->region.precommitted = true;
+}
+
int vfio_cxl_core_enable(struct vfio_cxl_core_device *cxl,
struct vfio_cxl_dev_info *info)
{
@@ -126,6 +149,8 @@ int vfio_cxl_core_enable(struct vfio_cxl_core_device *cxl,
if (ret)
goto err;

+ discover_precommitted_region(cxl);
+
return 0;

err:
diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
index 3474835f5d65..7237fcaecbb6 100644
--- a/include/linux/vfio_pci_core.h
+++ b/include/linux/vfio_pci_core.h
@@ -104,6 +104,7 @@ struct vfio_cxl_region {
u64 addr;
void *vaddr;
bool noncached;
+ bool precommitted;
};

struct vfio_cxl {
--
2.25.1