Skip to content

Commit 9a775c0

Browse files
aluceropdavejiang
authored andcommitted
cxl: support Type2 when initializing cxl_dev_state
In preparation for type2 drivers add function and macro for differentiating CXL memory expanders (type 3) from CXL device accelerators (type 2) helping drivers built from public headers to embed struct cxl_dev_state inside a private struct. Update type3 driver for using this same initialization. Signed-off-by: Alejandro Lucero <alucerop@amd.com> Reviewed-by: Dave Jiang <dave.jiang@intel.com> Reviewed-by: Alison Schofield <alison.schofield@intel.com> Reviewed-by: Gregory Price <gourry@gourry.net> Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com> Link: https://patch.msgid.link/20260306164741.3796372-2-alejandro.lucero-palau@amd.com Signed-off-by: Dave Jiang <dave.jiang@intel.com>
1 parent f338e77 commit 9a775c0

5 files changed

Lines changed: 70 additions & 17 deletions

File tree

drivers/cxl/core/mbox.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,23 +1521,21 @@ int cxl_mailbox_init(struct cxl_mailbox *cxl_mbox, struct device *host)
15211521
}
15221522
EXPORT_SYMBOL_NS_GPL(cxl_mailbox_init, "CXL");
15231523

1524-
struct cxl_memdev_state *cxl_memdev_state_create(struct device *dev)
1524+
struct cxl_memdev_state *cxl_memdev_state_create(struct device *dev, u64 serial,
1525+
u16 dvsec)
15251526
{
15261527
struct cxl_memdev_state *mds;
15271528
int rc;
15281529

1529-
mds = devm_kzalloc(dev, sizeof(*mds), GFP_KERNEL);
1530+
mds = devm_cxl_dev_state_create(dev, CXL_DEVTYPE_CLASSMEM, serial,
1531+
dvsec, struct cxl_memdev_state, cxlds,
1532+
true);
15301533
if (!mds) {
15311534
dev_err(dev, "No memory available\n");
15321535
return ERR_PTR(-ENOMEM);
15331536
}
15341537

15351538
mutex_init(&mds->event.log_lock);
1536-
mds->cxlds.dev = dev;
1537-
mds->cxlds.reg_map.host = dev;
1538-
mds->cxlds.cxl_mbox.host = dev;
1539-
mds->cxlds.reg_map.resource = CXL_RESOURCE_NONE;
1540-
mds->cxlds.type = CXL_DEVTYPE_CLASSMEM;
15411539

15421540
rc = devm_cxl_register_mce_notifier(dev, &mds->mce_notifier);
15431541
if (rc == -EOPNOTSUPP)

drivers/cxl/core/memdev.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,30 @@ static void detach_memdev(struct work_struct *work)
656656

657657
static struct lock_class_key cxl_memdev_key;
658658

659+
struct cxl_dev_state *_devm_cxl_dev_state_create(struct device *dev,
660+
enum cxl_devtype type,
661+
u64 serial, u16 dvsec,
662+
size_t size, bool has_mbox)
663+
{
664+
struct cxl_dev_state *cxlds = devm_kzalloc(dev, size, GFP_KERNEL);
665+
666+
if (!cxlds)
667+
return NULL;
668+
669+
cxlds->dev = dev;
670+
cxlds->type = type;
671+
cxlds->serial = serial;
672+
cxlds->cxl_dvsec = dvsec;
673+
cxlds->reg_map.host = dev;
674+
cxlds->reg_map.resource = CXL_RESOURCE_NONE;
675+
676+
if (has_mbox)
677+
cxlds->cxl_mbox.host = dev;
678+
679+
return cxlds;
680+
}
681+
EXPORT_SYMBOL_NS_GPL(_devm_cxl_dev_state_create, "CXL");
682+
659683
static struct cxl_memdev *cxl_memdev_alloc(struct cxl_dev_state *cxlds,
660684
const struct file_operations *fops,
661685
const struct cxl_memdev_attach *attach)

drivers/cxl/cxlmem.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,37 @@ to_cxl_memdev_state(struct cxl_dev_state *cxlds)
523523
return container_of(cxlds, struct cxl_memdev_state, cxlds);
524524
}
525525

526+
struct cxl_dev_state *_devm_cxl_dev_state_create(struct device *dev,
527+
enum cxl_devtype type,
528+
u64 serial, u16 dvsec,
529+
size_t size, bool has_mbox);
530+
531+
/**
532+
* cxl_dev_state_create - safely create and cast a cxl dev state embedded in a
533+
* driver specific struct.
534+
*
535+
* @parent: device behind the request
536+
* @type: CXL device type
537+
* @serial: device identification
538+
* @dvsec: dvsec capability offset
539+
* @drv_struct: driver struct embedding a cxl_dev_state struct
540+
* @member: name of the struct cxl_dev_state member in drv_struct
541+
* @mbox: true if mailbox supported
542+
*
543+
* Returns a pointer to the drv_struct allocated and embedding a cxl_dev_state
544+
* struct initialized.
545+
*
546+
* Introduced for Type2 driver support.
547+
*/
548+
#define devm_cxl_dev_state_create(parent, type, serial, dvsec, drv_struct, member, mbox) \
549+
({ \
550+
static_assert(__same_type(struct cxl_dev_state, \
551+
((drv_struct *)NULL)->member)); \
552+
static_assert(offsetof(drv_struct, member) == 0); \
553+
(drv_struct *)_devm_cxl_dev_state_create(parent, type, serial, dvsec, \
554+
sizeof(drv_struct), mbox); \
555+
})
556+
526557
enum cxl_opcode {
527558
CXL_MBOX_OP_INVALID = 0x0000,
528559
CXL_MBOX_OP_RAW = CXL_MBOX_OP_INVALID,
@@ -858,7 +889,8 @@ int cxl_dev_state_identify(struct cxl_memdev_state *mds);
858889
int cxl_await_media_ready(struct cxl_dev_state *cxlds);
859890
int cxl_enumerate_cmds(struct cxl_memdev_state *mds);
860891
int cxl_mem_dpa_fetch(struct cxl_memdev_state *mds, struct cxl_dpa_info *info);
861-
struct cxl_memdev_state *cxl_memdev_state_create(struct device *dev);
892+
struct cxl_memdev_state *cxl_memdev_state_create(struct device *dev, u64 serial,
893+
u16 dvsec);
862894
void set_exclusive_cxl_commands(struct cxl_memdev_state *mds,
863895
unsigned long *cmds);
864896
void clear_exclusive_cxl_commands(struct cxl_memdev_state *mds,

drivers/cxl/pci.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -865,25 +865,25 @@ static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
865865
int rc, pmu_count;
866866
unsigned int i;
867867
bool irq_avail;
868+
u16 dvsec;
868869

869870
rc = pcim_enable_device(pdev);
870871
if (rc)
871872
return rc;
872873
pci_set_master(pdev);
873874

874-
mds = cxl_memdev_state_create(&pdev->dev);
875+
dvsec = pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_CXL,
876+
PCI_DVSEC_CXL_DEVICE);
877+
if (!dvsec)
878+
pci_warn(pdev, "Device DVSEC not present, skip CXL.mem init\n");
879+
880+
mds = cxl_memdev_state_create(&pdev->dev, pci_get_dsn(pdev), dvsec);
875881
if (IS_ERR(mds))
876882
return PTR_ERR(mds);
877883
cxlds = &mds->cxlds;
878884
pci_set_drvdata(pdev, cxlds);
879885

880886
cxlds->rcd = is_cxl_restricted(pdev);
881-
cxlds->serial = pci_get_dsn(pdev);
882-
cxlds->cxl_dvsec = pci_find_dvsec_capability(
883-
pdev, PCI_VENDOR_ID_CXL, PCI_DVSEC_CXL_DEVICE);
884-
if (!cxlds->cxl_dvsec)
885-
dev_warn(&pdev->dev,
886-
"Device DVSEC not present, skip CXL.mem init\n");
887887

888888
rc = cxl_pci_setup_regs(pdev, CXL_REGLOC_RBI_MEMDEV, &map);
889889
if (rc)

tools/testing/cxl/test/mem.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ static int cxl_mock_mem_probe(struct platform_device *pdev)
17161716
if (rc)
17171717
return rc;
17181718

1719-
mds = cxl_memdev_state_create(dev);
1719+
mds = cxl_memdev_state_create(dev, pdev->id + 1, 0);
17201720
if (IS_ERR(mds))
17211721
return PTR_ERR(mds);
17221722

@@ -1732,7 +1732,6 @@ static int cxl_mock_mem_probe(struct platform_device *pdev)
17321732
mds->event.buf = (struct cxl_get_event_payload *) mdata->event_buf;
17331733
INIT_DELAYED_WORK(&mds->security.poll_dwork, cxl_mockmem_sanitize_work);
17341734

1735-
cxlds->serial = pdev->id + 1;
17361735
if (is_rcd(pdev))
17371736
cxlds->rcd = true;
17381737

0 commit comments

Comments
 (0)