Skip to content

Commit 202432a

Browse files
committed
Merge branch 'for-7.1/cxl-region-refactor' into cxl-for-next
Refactor CXL core/region code to make region code more manageable by splitting out DAX and PMEM code from RAM handling code. cxl/core: use cleanup.h for devm_cxl_add_dax_region cxl/core/region: move dax region device logic into region_dax.c cxl/core/region: move pmem region driver logic into region_pmem.c
2 parents 303d328 + 29990ab commit 202432a

7 files changed

Lines changed: 302 additions & 285 deletions

File tree

drivers/cxl/core/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cxl_core-y += hdm.o
1515
cxl_core-y += pmu.o
1616
cxl_core-y += cdat.o
1717
cxl_core-$(CONFIG_TRACING) += trace.o
18-
cxl_core-$(CONFIG_CXL_REGION) += region.o
18+
cxl_core-$(CONFIG_CXL_REGION) += region.o region_pmem.o region_dax.o
1919
cxl_core-$(CONFIG_CXL_MCE) += mce.o
2020
cxl_core-$(CONFIG_CXL_FEATURES) += features.o
2121
cxl_core-$(CONFIG_CXL_EDAC_MEM_FEATURES) += edac.o

drivers/cxl/core/core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ int cxl_get_poison_by_endpoint(struct cxl_port *port);
5050
struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa);
5151
u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd,
5252
u64 dpa);
53+
int devm_cxl_add_dax_region(struct cxl_region *cxlr);
54+
int devm_cxl_add_pmem_region(struct cxl_region *cxlr);
5355

5456
#else
5557
static inline u64 cxl_dpa_to_hpa(struct cxl_region *cxlr,

drivers/cxl/core/region.c

Lines changed: 0 additions & 283 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,46 +2850,6 @@ static ssize_t delete_region_store(struct device *dev,
28502850
}
28512851
DEVICE_ATTR_WO(delete_region);
28522852

2853-
static void cxl_pmem_region_release(struct device *dev)
2854-
{
2855-
struct cxl_pmem_region *cxlr_pmem = to_cxl_pmem_region(dev);
2856-
int i;
2857-
2858-
for (i = 0; i < cxlr_pmem->nr_mappings; i++) {
2859-
struct cxl_memdev *cxlmd = cxlr_pmem->mapping[i].cxlmd;
2860-
2861-
put_device(&cxlmd->dev);
2862-
}
2863-
2864-
kfree(cxlr_pmem);
2865-
}
2866-
2867-
static const struct attribute_group *cxl_pmem_region_attribute_groups[] = {
2868-
&cxl_base_attribute_group,
2869-
NULL,
2870-
};
2871-
2872-
const struct device_type cxl_pmem_region_type = {
2873-
.name = "cxl_pmem_region",
2874-
.release = cxl_pmem_region_release,
2875-
.groups = cxl_pmem_region_attribute_groups,
2876-
};
2877-
2878-
bool is_cxl_pmem_region(struct device *dev)
2879-
{
2880-
return dev->type == &cxl_pmem_region_type;
2881-
}
2882-
EXPORT_SYMBOL_NS_GPL(is_cxl_pmem_region, "CXL");
2883-
2884-
struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev)
2885-
{
2886-
if (dev_WARN_ONCE(dev, !is_cxl_pmem_region(dev),
2887-
"not a cxl_pmem_region device\n"))
2888-
return NULL;
2889-
return container_of(dev, struct cxl_pmem_region, dev);
2890-
}
2891-
EXPORT_SYMBOL_NS_GPL(to_cxl_pmem_region, "CXL");
2892-
28932853
struct cxl_poison_context {
28942854
struct cxl_port *port;
28952855
int part;
@@ -3545,249 +3505,6 @@ static int region_offset_to_dpa_result(struct cxl_region *cxlr, u64 offset,
35453505
return -ENXIO;
35463506
}
35473507

3548-
static struct lock_class_key cxl_pmem_region_key;
3549-
3550-
static int cxl_pmem_region_alloc(struct cxl_region *cxlr)
3551-
{
3552-
struct cxl_region_params *p = &cxlr->params;
3553-
struct cxl_nvdimm_bridge *cxl_nvb;
3554-
struct device *dev;
3555-
int i;
3556-
3557-
guard(rwsem_read)(&cxl_rwsem.region);
3558-
if (p->state != CXL_CONFIG_COMMIT)
3559-
return -ENXIO;
3560-
3561-
struct cxl_pmem_region *cxlr_pmem __free(kfree) =
3562-
kzalloc_flex(*cxlr_pmem, mapping, p->nr_targets);
3563-
if (!cxlr_pmem)
3564-
return -ENOMEM;
3565-
3566-
cxlr_pmem->hpa_range.start = p->res->start;
3567-
cxlr_pmem->hpa_range.end = p->res->end;
3568-
3569-
/* Snapshot the region configuration underneath the cxl_rwsem.region */
3570-
cxlr_pmem->nr_mappings = p->nr_targets;
3571-
for (i = 0; i < p->nr_targets; i++) {
3572-
struct cxl_endpoint_decoder *cxled = p->targets[i];
3573-
struct cxl_memdev *cxlmd = cxled_to_memdev(cxled);
3574-
struct cxl_pmem_region_mapping *m = &cxlr_pmem->mapping[i];
3575-
3576-
/*
3577-
* Regions never span CXL root devices, so by definition the
3578-
* bridge for one device is the same for all.
3579-
*/
3580-
if (i == 0) {
3581-
cxl_nvb = cxl_find_nvdimm_bridge(cxlmd->endpoint);
3582-
if (!cxl_nvb)
3583-
return -ENODEV;
3584-
cxlr->cxl_nvb = cxl_nvb;
3585-
}
3586-
m->cxlmd = cxlmd;
3587-
get_device(&cxlmd->dev);
3588-
m->start = cxled->dpa_res->start;
3589-
m->size = resource_size(cxled->dpa_res);
3590-
m->position = i;
3591-
}
3592-
3593-
dev = &cxlr_pmem->dev;
3594-
device_initialize(dev);
3595-
lockdep_set_class(&dev->mutex, &cxl_pmem_region_key);
3596-
device_set_pm_not_required(dev);
3597-
dev->parent = &cxlr->dev;
3598-
dev->bus = &cxl_bus_type;
3599-
dev->type = &cxl_pmem_region_type;
3600-
cxlr_pmem->cxlr = cxlr;
3601-
cxlr->cxlr_pmem = no_free_ptr(cxlr_pmem);
3602-
3603-
return 0;
3604-
}
3605-
3606-
static void cxl_dax_region_release(struct device *dev)
3607-
{
3608-
struct cxl_dax_region *cxlr_dax = to_cxl_dax_region(dev);
3609-
3610-
kfree(cxlr_dax);
3611-
}
3612-
3613-
static const struct attribute_group *cxl_dax_region_attribute_groups[] = {
3614-
&cxl_base_attribute_group,
3615-
NULL,
3616-
};
3617-
3618-
const struct device_type cxl_dax_region_type = {
3619-
.name = "cxl_dax_region",
3620-
.release = cxl_dax_region_release,
3621-
.groups = cxl_dax_region_attribute_groups,
3622-
};
3623-
3624-
static bool is_cxl_dax_region(struct device *dev)
3625-
{
3626-
return dev->type == &cxl_dax_region_type;
3627-
}
3628-
3629-
struct cxl_dax_region *to_cxl_dax_region(struct device *dev)
3630-
{
3631-
if (dev_WARN_ONCE(dev, !is_cxl_dax_region(dev),
3632-
"not a cxl_dax_region device\n"))
3633-
return NULL;
3634-
return container_of(dev, struct cxl_dax_region, dev);
3635-
}
3636-
EXPORT_SYMBOL_NS_GPL(to_cxl_dax_region, "CXL");
3637-
3638-
static struct lock_class_key cxl_dax_region_key;
3639-
3640-
static struct cxl_dax_region *cxl_dax_region_alloc(struct cxl_region *cxlr)
3641-
{
3642-
struct cxl_region_params *p = &cxlr->params;
3643-
struct cxl_dax_region *cxlr_dax;
3644-
struct device *dev;
3645-
3646-
guard(rwsem_read)(&cxl_rwsem.region);
3647-
if (p->state != CXL_CONFIG_COMMIT)
3648-
return ERR_PTR(-ENXIO);
3649-
3650-
cxlr_dax = kzalloc_obj(*cxlr_dax);
3651-
if (!cxlr_dax)
3652-
return ERR_PTR(-ENOMEM);
3653-
3654-
cxlr_dax->hpa_range.start = p->res->start;
3655-
cxlr_dax->hpa_range.end = p->res->end;
3656-
3657-
dev = &cxlr_dax->dev;
3658-
cxlr_dax->cxlr = cxlr;
3659-
device_initialize(dev);
3660-
lockdep_set_class(&dev->mutex, &cxl_dax_region_key);
3661-
device_set_pm_not_required(dev);
3662-
dev->parent = &cxlr->dev;
3663-
dev->bus = &cxl_bus_type;
3664-
dev->type = &cxl_dax_region_type;
3665-
3666-
return cxlr_dax;
3667-
}
3668-
3669-
static void cxlr_pmem_unregister(void *_cxlr_pmem)
3670-
{
3671-
struct cxl_pmem_region *cxlr_pmem = _cxlr_pmem;
3672-
struct cxl_region *cxlr = cxlr_pmem->cxlr;
3673-
struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb;
3674-
3675-
/*
3676-
* Either the bridge is in ->remove() context under the device_lock(),
3677-
* or cxlr_release_nvdimm() is cancelling the bridge's release action
3678-
* for @cxlr_pmem and doing it itself (while manually holding the bridge
3679-
* lock).
3680-
*/
3681-
device_lock_assert(&cxl_nvb->dev);
3682-
cxlr->cxlr_pmem = NULL;
3683-
cxlr_pmem->cxlr = NULL;
3684-
device_unregister(&cxlr_pmem->dev);
3685-
}
3686-
3687-
static void cxlr_release_nvdimm(void *_cxlr)
3688-
{
3689-
struct cxl_region *cxlr = _cxlr;
3690-
struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb;
3691-
3692-
scoped_guard(device, &cxl_nvb->dev) {
3693-
if (cxlr->cxlr_pmem)
3694-
devm_release_action(&cxl_nvb->dev, cxlr_pmem_unregister,
3695-
cxlr->cxlr_pmem);
3696-
}
3697-
cxlr->cxl_nvb = NULL;
3698-
put_device(&cxl_nvb->dev);
3699-
}
3700-
3701-
/**
3702-
* devm_cxl_add_pmem_region() - add a cxl_region-to-nd_region bridge
3703-
* @cxlr: parent CXL region for this pmem region bridge device
3704-
*
3705-
* Return: 0 on success negative error code on failure.
3706-
*/
3707-
static int devm_cxl_add_pmem_region(struct cxl_region *cxlr)
3708-
{
3709-
struct cxl_pmem_region *cxlr_pmem;
3710-
struct cxl_nvdimm_bridge *cxl_nvb;
3711-
struct device *dev;
3712-
int rc;
3713-
3714-
rc = cxl_pmem_region_alloc(cxlr);
3715-
if (rc)
3716-
return rc;
3717-
cxlr_pmem = cxlr->cxlr_pmem;
3718-
cxl_nvb = cxlr->cxl_nvb;
3719-
3720-
dev = &cxlr_pmem->dev;
3721-
rc = dev_set_name(dev, "pmem_region%d", cxlr->id);
3722-
if (rc)
3723-
goto err;
3724-
3725-
rc = device_add(dev);
3726-
if (rc)
3727-
goto err;
3728-
3729-
dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent),
3730-
dev_name(dev));
3731-
3732-
scoped_guard(device, &cxl_nvb->dev) {
3733-
if (cxl_nvb->dev.driver)
3734-
rc = devm_add_action_or_reset(&cxl_nvb->dev,
3735-
cxlr_pmem_unregister,
3736-
cxlr_pmem);
3737-
else
3738-
rc = -ENXIO;
3739-
}
3740-
3741-
if (rc)
3742-
goto err_bridge;
3743-
3744-
/* @cxlr carries a reference on @cxl_nvb until cxlr_release_nvdimm */
3745-
return devm_add_action_or_reset(&cxlr->dev, cxlr_release_nvdimm, cxlr);
3746-
3747-
err:
3748-
put_device(dev);
3749-
err_bridge:
3750-
put_device(&cxl_nvb->dev);
3751-
cxlr->cxl_nvb = NULL;
3752-
return rc;
3753-
}
3754-
3755-
static void cxlr_dax_unregister(void *_cxlr_dax)
3756-
{
3757-
struct cxl_dax_region *cxlr_dax = _cxlr_dax;
3758-
3759-
device_unregister(&cxlr_dax->dev);
3760-
}
3761-
3762-
static int devm_cxl_add_dax_region(struct cxl_region *cxlr)
3763-
{
3764-
struct cxl_dax_region *cxlr_dax;
3765-
struct device *dev;
3766-
int rc;
3767-
3768-
cxlr_dax = cxl_dax_region_alloc(cxlr);
3769-
if (IS_ERR(cxlr_dax))
3770-
return PTR_ERR(cxlr_dax);
3771-
3772-
dev = &cxlr_dax->dev;
3773-
rc = dev_set_name(dev, "dax_region%d", cxlr->id);
3774-
if (rc)
3775-
goto err;
3776-
3777-
rc = device_add(dev);
3778-
if (rc)
3779-
goto err;
3780-
3781-
dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent),
3782-
dev_name(dev));
3783-
3784-
return devm_add_action_or_reset(&cxlr->dev, cxlr_dax_unregister,
3785-
cxlr_dax);
3786-
err:
3787-
put_device(dev);
3788-
return rc;
3789-
}
3790-
37913508
static int match_root_decoder(struct device *dev, const void *data)
37923509
{
37933510
const struct range *r1, *r2 = data;

0 commit comments

Comments
 (0)