[PATCH v9 08/27] cxl: add functions for resource request/release by a driver

alejandro.lucero-palau
2024-12-30T21:45:14Z

From: Alejandro Lucero <alucerop@amd.com>

Create accessors for an accel driver requesting and releasing a resource.

Signed-off-by: Alejandro Lucero <alucerop@amd.com>
Reviewed-by: Ben Cheatham <benjamin.cheatham@amd.com>
Reviewed-by: Zhi Wang <zhi@nvidia.com>
Reviewed-by: Fan Ni <fan.ni@samsung.com>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
drivers/cxl/core/memdev.c | 39 +++++++++++++++++++++++++++++++++++++++
include/cxl/cxl.h | 2 ++
2 files changed, 41 insertions(+)

diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
index 99f533caae1e..b104af6761cf 100644
--- a/drivers/cxl/core/memdev.c
+++ b/drivers/cxl/core/memdev.c
@@ -744,6 +744,45 @@ int cxl_set_resource(struct cxl_dev_state *cxlds, struct resource res,
}
EXPORT_SYMBOL_NS_GPL(cxl_set_resource, "CXL");

+int cxl_request_resource(struct cxl_dev_state *cxlds, enum cxl_resource type)
+{
+ switch (type) {
+ case CXL_RES_RAM:
+ if (!resource_size(&cxlds->ram_res)) {
+ dev_err(cxlds->dev,
+ "resource request for ram with size 0\n");
+ return -EINVAL;
+ }
+
+ return request_resource(&cxlds->dpa_res, &cxlds->ram_res);
+ case CXL_RES_PMEM:
+ if (!resource_size(&cxlds->pmem_res)) {
+ dev_err(cxlds->dev,
+ "resource request for pmem with size 0\n");
+ return -EINVAL;
+ }
+ return request_resource(&cxlds->dpa_res, &cxlds->pmem_res);
+ default:
+ dev_err(cxlds->dev, "unsupported resource type (%u)\n", type);
+ return -EINVAL;
+ }
+}
+EXPORT_SYMBOL_NS_GPL(cxl_request_resource, "CXL");
+
+int cxl_release_resource(struct cxl_dev_state *cxlds, enum cxl_resource type)
+{
+ switch (type) {
+ case CXL_RES_RAM:
+ return release_resource(&cxlds->ram_res);
+ case CXL_RES_PMEM:
+ return release_resource(&cxlds->pmem_res);
+ default:
+ dev_err(cxlds->dev, "unknown resource type (%u)\n", type);
+ return -EINVAL;
+ }
+}
+EXPORT_SYMBOL_NS_GPL(cxl_release_resource, "CXL");
+
static int cxl_memdev_release_file(struct inode *inode, struct file *file)
{
struct cxl_memdev *cxlmd =
diff --git a/include/cxl/cxl.h b/include/cxl/cxl.h
index c1bb0359476c..87b192095fe3 100644
--- a/include/cxl/cxl.h
+++ b/include/cxl/cxl.h
@@ -45,4 +45,6 @@ bool cxl_pci_check_caps(struct cxl_dev_state *cxlds,
unsigned long *current_caps);
struct pci_dev;
int cxl_pci_accel_setup_regs(struct pci_dev *pdev, struct cxl_dev_state *cxlds);
+int cxl_request_resource(struct cxl_dev_state *cxlds, enum cxl_resource type);
+int cxl_release_resource(struct cxl_dev_state *cxlds, enum cxl_resource type);
#endif
--
2.17.1