1779dd20cSBen Widawsky // SPDX-License-Identifier: GPL-2.0-only 2779dd20cSBen Widawsky /* Copyright(c) 2022 Intel Corporation. All rights reserved. */ 3779dd20cSBen Widawsky #include <linux/memregion.h> 4779dd20cSBen Widawsky #include <linux/genalloc.h> 5779dd20cSBen Widawsky #include <linux/device.h> 6779dd20cSBen Widawsky #include <linux/module.h> 7779dd20cSBen Widawsky #include <linux/slab.h> 8dd5ba0ebSBen Widawsky #include <linux/uuid.h> 9779dd20cSBen Widawsky #include <linux/idr.h> 1080d10a6cSBen Widawsky #include <cxlmem.h> 11779dd20cSBen Widawsky #include <cxl.h> 12779dd20cSBen Widawsky #include "core.h" 13779dd20cSBen Widawsky 14779dd20cSBen Widawsky /** 15779dd20cSBen Widawsky * DOC: cxl core region 16779dd20cSBen Widawsky * 17779dd20cSBen Widawsky * CXL Regions represent mapped memory capacity in system physical address 18779dd20cSBen Widawsky * space. Whereas the CXL Root Decoders identify the bounds of potential CXL 19779dd20cSBen Widawsky * Memory ranges, Regions represent the active mapped capacity by the HDM 20779dd20cSBen Widawsky * Decoder Capability structures throughout the Host Bridges, Switches, and 21779dd20cSBen Widawsky * Endpoints in the topology. 22dd5ba0ebSBen Widawsky * 23dd5ba0ebSBen Widawsky * Region configuration has ordering constraints. UUID may be set at any time 24dd5ba0ebSBen Widawsky * but is only visible for persistent regions. 2580d10a6cSBen Widawsky * 1. Interleave granularity 2680d10a6cSBen Widawsky * 2. Interleave size 27b9686e8cSDan Williams * 3. Decoder targets 28779dd20cSBen Widawsky */ 29779dd20cSBen Widawsky 30dd5ba0ebSBen Widawsky /* 31dd5ba0ebSBen Widawsky * All changes to the interleave configuration occur with this lock held 32dd5ba0ebSBen Widawsky * for write. 33dd5ba0ebSBen Widawsky */ 34dd5ba0ebSBen Widawsky static DECLARE_RWSEM(cxl_region_rwsem); 35dd5ba0ebSBen Widawsky 36779dd20cSBen Widawsky static struct cxl_region *to_cxl_region(struct device *dev); 37779dd20cSBen Widawsky 38dd5ba0ebSBen Widawsky static ssize_t uuid_show(struct device *dev, struct device_attribute *attr, 39dd5ba0ebSBen Widawsky char *buf) 40dd5ba0ebSBen Widawsky { 41dd5ba0ebSBen Widawsky struct cxl_region *cxlr = to_cxl_region(dev); 42dd5ba0ebSBen Widawsky struct cxl_region_params *p = &cxlr->params; 43dd5ba0ebSBen Widawsky ssize_t rc; 44dd5ba0ebSBen Widawsky 45dd5ba0ebSBen Widawsky rc = down_read_interruptible(&cxl_region_rwsem); 46dd5ba0ebSBen Widawsky if (rc) 47dd5ba0ebSBen Widawsky return rc; 48dd5ba0ebSBen Widawsky rc = sysfs_emit(buf, "%pUb\n", &p->uuid); 49dd5ba0ebSBen Widawsky up_read(&cxl_region_rwsem); 50dd5ba0ebSBen Widawsky 51dd5ba0ebSBen Widawsky return rc; 52dd5ba0ebSBen Widawsky } 53dd5ba0ebSBen Widawsky 54dd5ba0ebSBen Widawsky static int is_dup(struct device *match, void *data) 55dd5ba0ebSBen Widawsky { 56dd5ba0ebSBen Widawsky struct cxl_region_params *p; 57dd5ba0ebSBen Widawsky struct cxl_region *cxlr; 58dd5ba0ebSBen Widawsky uuid_t *uuid = data; 59dd5ba0ebSBen Widawsky 60dd5ba0ebSBen Widawsky if (!is_cxl_region(match)) 61dd5ba0ebSBen Widawsky return 0; 62dd5ba0ebSBen Widawsky 63dd5ba0ebSBen Widawsky lockdep_assert_held(&cxl_region_rwsem); 64dd5ba0ebSBen Widawsky cxlr = to_cxl_region(match); 65dd5ba0ebSBen Widawsky p = &cxlr->params; 66dd5ba0ebSBen Widawsky 67dd5ba0ebSBen Widawsky if (uuid_equal(&p->uuid, uuid)) { 68dd5ba0ebSBen Widawsky dev_dbg(match, "already has uuid: %pUb\n", uuid); 69dd5ba0ebSBen Widawsky return -EBUSY; 70dd5ba0ebSBen Widawsky } 71dd5ba0ebSBen Widawsky 72dd5ba0ebSBen Widawsky return 0; 73dd5ba0ebSBen Widawsky } 74dd5ba0ebSBen Widawsky 75dd5ba0ebSBen Widawsky static ssize_t uuid_store(struct device *dev, struct device_attribute *attr, 76dd5ba0ebSBen Widawsky const char *buf, size_t len) 77dd5ba0ebSBen Widawsky { 78dd5ba0ebSBen Widawsky struct cxl_region *cxlr = to_cxl_region(dev); 79dd5ba0ebSBen Widawsky struct cxl_region_params *p = &cxlr->params; 80dd5ba0ebSBen Widawsky uuid_t temp; 81dd5ba0ebSBen Widawsky ssize_t rc; 82dd5ba0ebSBen Widawsky 83dd5ba0ebSBen Widawsky if (len != UUID_STRING_LEN + 1) 84dd5ba0ebSBen Widawsky return -EINVAL; 85dd5ba0ebSBen Widawsky 86dd5ba0ebSBen Widawsky rc = uuid_parse(buf, &temp); 87dd5ba0ebSBen Widawsky if (rc) 88dd5ba0ebSBen Widawsky return rc; 89dd5ba0ebSBen Widawsky 90dd5ba0ebSBen Widawsky if (uuid_is_null(&temp)) 91dd5ba0ebSBen Widawsky return -EINVAL; 92dd5ba0ebSBen Widawsky 93dd5ba0ebSBen Widawsky rc = down_write_killable(&cxl_region_rwsem); 94dd5ba0ebSBen Widawsky if (rc) 95dd5ba0ebSBen Widawsky return rc; 96dd5ba0ebSBen Widawsky 97dd5ba0ebSBen Widawsky if (uuid_equal(&p->uuid, &temp)) 98dd5ba0ebSBen Widawsky goto out; 99dd5ba0ebSBen Widawsky 100dd5ba0ebSBen Widawsky rc = -EBUSY; 101dd5ba0ebSBen Widawsky if (p->state >= CXL_CONFIG_ACTIVE) 102dd5ba0ebSBen Widawsky goto out; 103dd5ba0ebSBen Widawsky 104dd5ba0ebSBen Widawsky rc = bus_for_each_dev(&cxl_bus_type, NULL, &temp, is_dup); 105dd5ba0ebSBen Widawsky if (rc < 0) 106dd5ba0ebSBen Widawsky goto out; 107dd5ba0ebSBen Widawsky 108dd5ba0ebSBen Widawsky uuid_copy(&p->uuid, &temp); 109dd5ba0ebSBen Widawsky out: 110dd5ba0ebSBen Widawsky up_write(&cxl_region_rwsem); 111dd5ba0ebSBen Widawsky 112dd5ba0ebSBen Widawsky if (rc) 113dd5ba0ebSBen Widawsky return rc; 114dd5ba0ebSBen Widawsky return len; 115dd5ba0ebSBen Widawsky } 116dd5ba0ebSBen Widawsky static DEVICE_ATTR_RW(uuid); 117dd5ba0ebSBen Widawsky 118176baefbSDan Williams static struct cxl_region_ref *cxl_rr_load(struct cxl_port *port, 119176baefbSDan Williams struct cxl_region *cxlr) 120176baefbSDan Williams { 121176baefbSDan Williams return xa_load(&port->regions, (unsigned long)cxlr); 122176baefbSDan Williams } 123176baefbSDan Williams 124176baefbSDan Williams static int cxl_region_decode_reset(struct cxl_region *cxlr, int count) 125176baefbSDan Williams { 126176baefbSDan Williams struct cxl_region_params *p = &cxlr->params; 127176baefbSDan Williams int i; 128176baefbSDan Williams 129176baefbSDan Williams for (i = count - 1; i >= 0; i--) { 130176baefbSDan Williams struct cxl_endpoint_decoder *cxled = p->targets[i]; 131176baefbSDan Williams struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 132176baefbSDan Williams struct cxl_port *iter = cxled_to_port(cxled); 133176baefbSDan Williams struct cxl_ep *ep; 134176baefbSDan Williams int rc; 135176baefbSDan Williams 136176baefbSDan Williams while (!is_cxl_root(to_cxl_port(iter->dev.parent))) 137176baefbSDan Williams iter = to_cxl_port(iter->dev.parent); 138176baefbSDan Williams 139176baefbSDan Williams for (ep = cxl_ep_load(iter, cxlmd); iter; 140176baefbSDan Williams iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) { 141176baefbSDan Williams struct cxl_region_ref *cxl_rr; 142176baefbSDan Williams struct cxl_decoder *cxld; 143176baefbSDan Williams 144176baefbSDan Williams cxl_rr = cxl_rr_load(iter, cxlr); 145176baefbSDan Williams cxld = cxl_rr->decoder; 146176baefbSDan Williams rc = cxld->reset(cxld); 147176baefbSDan Williams if (rc) 148176baefbSDan Williams return rc; 149176baefbSDan Williams } 150176baefbSDan Williams 151176baefbSDan Williams rc = cxled->cxld.reset(&cxled->cxld); 152176baefbSDan Williams if (rc) 153176baefbSDan Williams return rc; 154176baefbSDan Williams } 155176baefbSDan Williams 156176baefbSDan Williams return 0; 157176baefbSDan Williams } 158176baefbSDan Williams 159176baefbSDan Williams static int cxl_region_decode_commit(struct cxl_region *cxlr) 160176baefbSDan Williams { 161176baefbSDan Williams struct cxl_region_params *p = &cxlr->params; 16269c99613SDan Williams int i, rc = 0; 163176baefbSDan Williams 164176baefbSDan Williams for (i = 0; i < p->nr_targets; i++) { 165176baefbSDan Williams struct cxl_endpoint_decoder *cxled = p->targets[i]; 166176baefbSDan Williams struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 167176baefbSDan Williams struct cxl_region_ref *cxl_rr; 168176baefbSDan Williams struct cxl_decoder *cxld; 169176baefbSDan Williams struct cxl_port *iter; 170176baefbSDan Williams struct cxl_ep *ep; 171176baefbSDan Williams 172176baefbSDan Williams /* commit bottom up */ 173176baefbSDan Williams for (iter = cxled_to_port(cxled); !is_cxl_root(iter); 174176baefbSDan Williams iter = to_cxl_port(iter->dev.parent)) { 175176baefbSDan Williams cxl_rr = cxl_rr_load(iter, cxlr); 176176baefbSDan Williams cxld = cxl_rr->decoder; 177176baefbSDan Williams rc = cxld->commit(cxld); 178176baefbSDan Williams if (rc) 179176baefbSDan Williams break; 180176baefbSDan Williams } 181176baefbSDan Williams 18269c99613SDan Williams if (rc) { 183176baefbSDan Williams /* programming @iter failed, teardown */ 184176baefbSDan Williams for (ep = cxl_ep_load(iter, cxlmd); ep && iter; 185176baefbSDan Williams iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) { 186176baefbSDan Williams cxl_rr = cxl_rr_load(iter, cxlr); 187176baefbSDan Williams cxld = cxl_rr->decoder; 188176baefbSDan Williams cxld->reset(cxld); 189176baefbSDan Williams } 190176baefbSDan Williams 191176baefbSDan Williams cxled->cxld.reset(&cxled->cxld); 19269c99613SDan Williams goto err; 19369c99613SDan Williams } 194176baefbSDan Williams } 195176baefbSDan Williams 196176baefbSDan Williams return 0; 197176baefbSDan Williams 19869c99613SDan Williams err: 199176baefbSDan Williams /* undo the targets that were successfully committed */ 200176baefbSDan Williams cxl_region_decode_reset(cxlr, i); 201176baefbSDan Williams return rc; 202176baefbSDan Williams } 203176baefbSDan Williams 204176baefbSDan Williams static ssize_t commit_store(struct device *dev, struct device_attribute *attr, 205176baefbSDan Williams const char *buf, size_t len) 206176baefbSDan Williams { 207176baefbSDan Williams struct cxl_region *cxlr = to_cxl_region(dev); 208176baefbSDan Williams struct cxl_region_params *p = &cxlr->params; 209176baefbSDan Williams bool commit; 210176baefbSDan Williams ssize_t rc; 211176baefbSDan Williams 212176baefbSDan Williams rc = kstrtobool(buf, &commit); 213176baefbSDan Williams if (rc) 214176baefbSDan Williams return rc; 215176baefbSDan Williams 216176baefbSDan Williams rc = down_write_killable(&cxl_region_rwsem); 217176baefbSDan Williams if (rc) 218176baefbSDan Williams return rc; 219176baefbSDan Williams 220176baefbSDan Williams /* Already in the requested state? */ 221176baefbSDan Williams if (commit && p->state >= CXL_CONFIG_COMMIT) 222176baefbSDan Williams goto out; 223176baefbSDan Williams if (!commit && p->state < CXL_CONFIG_COMMIT) 224176baefbSDan Williams goto out; 225176baefbSDan Williams 226176baefbSDan Williams /* Not ready to commit? */ 227176baefbSDan Williams if (commit && p->state < CXL_CONFIG_ACTIVE) { 228176baefbSDan Williams rc = -ENXIO; 229176baefbSDan Williams goto out; 230176baefbSDan Williams } 231176baefbSDan Williams 232176baefbSDan Williams if (commit) 233176baefbSDan Williams rc = cxl_region_decode_commit(cxlr); 234176baefbSDan Williams else { 235176baefbSDan Williams p->state = CXL_CONFIG_RESET_PENDING; 236176baefbSDan Williams up_write(&cxl_region_rwsem); 237176baefbSDan Williams device_release_driver(&cxlr->dev); 238176baefbSDan Williams down_write(&cxl_region_rwsem); 239176baefbSDan Williams 240176baefbSDan Williams /* 241176baefbSDan Williams * The lock was dropped, so need to revalidate that the reset is 242176baefbSDan Williams * still pending. 243176baefbSDan Williams */ 244176baefbSDan Williams if (p->state == CXL_CONFIG_RESET_PENDING) 245176baefbSDan Williams rc = cxl_region_decode_reset(cxlr, p->interleave_ways); 246176baefbSDan Williams } 247176baefbSDan Williams 248176baefbSDan Williams if (rc) 249176baefbSDan Williams goto out; 250176baefbSDan Williams 251176baefbSDan Williams if (commit) 252176baefbSDan Williams p->state = CXL_CONFIG_COMMIT; 253176baefbSDan Williams else if (p->state == CXL_CONFIG_RESET_PENDING) 254176baefbSDan Williams p->state = CXL_CONFIG_ACTIVE; 255176baefbSDan Williams 256176baefbSDan Williams out: 257176baefbSDan Williams up_write(&cxl_region_rwsem); 258176baefbSDan Williams 259176baefbSDan Williams if (rc) 260176baefbSDan Williams return rc; 261176baefbSDan Williams return len; 262176baefbSDan Williams } 263176baefbSDan Williams 264176baefbSDan Williams static ssize_t commit_show(struct device *dev, struct device_attribute *attr, 265176baefbSDan Williams char *buf) 266176baefbSDan Williams { 267176baefbSDan Williams struct cxl_region *cxlr = to_cxl_region(dev); 268176baefbSDan Williams struct cxl_region_params *p = &cxlr->params; 269176baefbSDan Williams ssize_t rc; 270176baefbSDan Williams 271176baefbSDan Williams rc = down_read_interruptible(&cxl_region_rwsem); 272176baefbSDan Williams if (rc) 273176baefbSDan Williams return rc; 274176baefbSDan Williams rc = sysfs_emit(buf, "%d\n", p->state >= CXL_CONFIG_COMMIT); 275176baefbSDan Williams up_read(&cxl_region_rwsem); 276176baefbSDan Williams 277176baefbSDan Williams return rc; 278176baefbSDan Williams } 279176baefbSDan Williams static DEVICE_ATTR_RW(commit); 280176baefbSDan Williams 281dd5ba0ebSBen Widawsky static umode_t cxl_region_visible(struct kobject *kobj, struct attribute *a, 282dd5ba0ebSBen Widawsky int n) 283dd5ba0ebSBen Widawsky { 284dd5ba0ebSBen Widawsky struct device *dev = kobj_to_dev(kobj); 285dd5ba0ebSBen Widawsky struct cxl_region *cxlr = to_cxl_region(dev); 286dd5ba0ebSBen Widawsky 287dd5ba0ebSBen Widawsky if (a == &dev_attr_uuid.attr && cxlr->mode != CXL_DECODER_PMEM) 288dd5ba0ebSBen Widawsky return 0; 289dd5ba0ebSBen Widawsky return a->mode; 290dd5ba0ebSBen Widawsky } 291dd5ba0ebSBen Widawsky 29280d10a6cSBen Widawsky static ssize_t interleave_ways_show(struct device *dev, 29380d10a6cSBen Widawsky struct device_attribute *attr, char *buf) 29480d10a6cSBen Widawsky { 29580d10a6cSBen Widawsky struct cxl_region *cxlr = to_cxl_region(dev); 29680d10a6cSBen Widawsky struct cxl_region_params *p = &cxlr->params; 29780d10a6cSBen Widawsky ssize_t rc; 29880d10a6cSBen Widawsky 29980d10a6cSBen Widawsky rc = down_read_interruptible(&cxl_region_rwsem); 30080d10a6cSBen Widawsky if (rc) 30180d10a6cSBen Widawsky return rc; 30280d10a6cSBen Widawsky rc = sysfs_emit(buf, "%d\n", p->interleave_ways); 30380d10a6cSBen Widawsky up_read(&cxl_region_rwsem); 30480d10a6cSBen Widawsky 30580d10a6cSBen Widawsky return rc; 30680d10a6cSBen Widawsky } 30780d10a6cSBen Widawsky 308b9686e8cSDan Williams static const struct attribute_group *get_cxl_region_target_group(void); 309b9686e8cSDan Williams 31080d10a6cSBen Widawsky static ssize_t interleave_ways_store(struct device *dev, 31180d10a6cSBen Widawsky struct device_attribute *attr, 31280d10a6cSBen Widawsky const char *buf, size_t len) 31380d10a6cSBen Widawsky { 31480d10a6cSBen Widawsky struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent); 31580d10a6cSBen Widawsky struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld; 31680d10a6cSBen Widawsky struct cxl_region *cxlr = to_cxl_region(dev); 31780d10a6cSBen Widawsky struct cxl_region_params *p = &cxlr->params; 318c7e3548cSDan Carpenter unsigned int val, save; 319c7e3548cSDan Carpenter int rc; 32080d10a6cSBen Widawsky u8 iw; 32180d10a6cSBen Widawsky 322c7e3548cSDan Carpenter rc = kstrtouint(buf, 0, &val); 32380d10a6cSBen Widawsky if (rc) 32480d10a6cSBen Widawsky return rc; 32580d10a6cSBen Widawsky 32680d10a6cSBen Widawsky rc = ways_to_cxl(val, &iw); 32780d10a6cSBen Widawsky if (rc) 32880d10a6cSBen Widawsky return rc; 32980d10a6cSBen Widawsky 33080d10a6cSBen Widawsky /* 33180d10a6cSBen Widawsky * Even for x3, x9, and x12 interleaves the region interleave must be a 33280d10a6cSBen Widawsky * power of 2 multiple of the host bridge interleave. 33380d10a6cSBen Widawsky */ 33480d10a6cSBen Widawsky if (!is_power_of_2(val / cxld->interleave_ways) || 33580d10a6cSBen Widawsky (val % cxld->interleave_ways)) { 33680d10a6cSBen Widawsky dev_dbg(&cxlr->dev, "invalid interleave: %d\n", val); 33780d10a6cSBen Widawsky return -EINVAL; 33880d10a6cSBen Widawsky } 33980d10a6cSBen Widawsky 34080d10a6cSBen Widawsky rc = down_write_killable(&cxl_region_rwsem); 34180d10a6cSBen Widawsky if (rc) 34280d10a6cSBen Widawsky return rc; 34380d10a6cSBen Widawsky if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) { 34480d10a6cSBen Widawsky rc = -EBUSY; 34580d10a6cSBen Widawsky goto out; 34680d10a6cSBen Widawsky } 34780d10a6cSBen Widawsky 348b9686e8cSDan Williams save = p->interleave_ways; 34980d10a6cSBen Widawsky p->interleave_ways = val; 350b9686e8cSDan Williams rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group()); 351b9686e8cSDan Williams if (rc) 352b9686e8cSDan Williams p->interleave_ways = save; 35380d10a6cSBen Widawsky out: 35480d10a6cSBen Widawsky up_write(&cxl_region_rwsem); 35580d10a6cSBen Widawsky if (rc) 35680d10a6cSBen Widawsky return rc; 35780d10a6cSBen Widawsky return len; 35880d10a6cSBen Widawsky } 35980d10a6cSBen Widawsky static DEVICE_ATTR_RW(interleave_ways); 36080d10a6cSBen Widawsky 36180d10a6cSBen Widawsky static ssize_t interleave_granularity_show(struct device *dev, 36280d10a6cSBen Widawsky struct device_attribute *attr, 36380d10a6cSBen Widawsky char *buf) 36480d10a6cSBen Widawsky { 36580d10a6cSBen Widawsky struct cxl_region *cxlr = to_cxl_region(dev); 36680d10a6cSBen Widawsky struct cxl_region_params *p = &cxlr->params; 36780d10a6cSBen Widawsky ssize_t rc; 36880d10a6cSBen Widawsky 36980d10a6cSBen Widawsky rc = down_read_interruptible(&cxl_region_rwsem); 37080d10a6cSBen Widawsky if (rc) 37180d10a6cSBen Widawsky return rc; 37280d10a6cSBen Widawsky rc = sysfs_emit(buf, "%d\n", p->interleave_granularity); 37380d10a6cSBen Widawsky up_read(&cxl_region_rwsem); 37480d10a6cSBen Widawsky 37580d10a6cSBen Widawsky return rc; 37680d10a6cSBen Widawsky } 37780d10a6cSBen Widawsky 37880d10a6cSBen Widawsky static ssize_t interleave_granularity_store(struct device *dev, 37980d10a6cSBen Widawsky struct device_attribute *attr, 38080d10a6cSBen Widawsky const char *buf, size_t len) 38180d10a6cSBen Widawsky { 38280d10a6cSBen Widawsky struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent); 38380d10a6cSBen Widawsky struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld; 38480d10a6cSBen Widawsky struct cxl_region *cxlr = to_cxl_region(dev); 38580d10a6cSBen Widawsky struct cxl_region_params *p = &cxlr->params; 38680d10a6cSBen Widawsky int rc, val; 38780d10a6cSBen Widawsky u16 ig; 38880d10a6cSBen Widawsky 38980d10a6cSBen Widawsky rc = kstrtoint(buf, 0, &val); 39080d10a6cSBen Widawsky if (rc) 39180d10a6cSBen Widawsky return rc; 39280d10a6cSBen Widawsky 39380d10a6cSBen Widawsky rc = granularity_to_cxl(val, &ig); 39480d10a6cSBen Widawsky if (rc) 39580d10a6cSBen Widawsky return rc; 39680d10a6cSBen Widawsky 39780d10a6cSBen Widawsky /* 39880d10a6cSBen Widawsky * Disallow region granularity less than root granularity to 39980d10a6cSBen Widawsky * simplify the implementation. Otherwise, region's with a 40080d10a6cSBen Widawsky * granularity less than the root interleave result in needing 40180d10a6cSBen Widawsky * multiple endpoints to support a single slot in the 40280d10a6cSBen Widawsky * interleave. 40380d10a6cSBen Widawsky */ 40480d10a6cSBen Widawsky if (val < cxld->interleave_granularity) 40580d10a6cSBen Widawsky return -EINVAL; 40680d10a6cSBen Widawsky 40780d10a6cSBen Widawsky rc = down_write_killable(&cxl_region_rwsem); 40880d10a6cSBen Widawsky if (rc) 40980d10a6cSBen Widawsky return rc; 41080d10a6cSBen Widawsky if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) { 41180d10a6cSBen Widawsky rc = -EBUSY; 41280d10a6cSBen Widawsky goto out; 41380d10a6cSBen Widawsky } 41480d10a6cSBen Widawsky 41580d10a6cSBen Widawsky p->interleave_granularity = val; 41680d10a6cSBen Widawsky out: 41780d10a6cSBen Widawsky up_write(&cxl_region_rwsem); 41880d10a6cSBen Widawsky if (rc) 41980d10a6cSBen Widawsky return rc; 42080d10a6cSBen Widawsky return len; 42180d10a6cSBen Widawsky } 42280d10a6cSBen Widawsky static DEVICE_ATTR_RW(interleave_granularity); 42380d10a6cSBen Widawsky 42423a22cd1SDan Williams static ssize_t resource_show(struct device *dev, struct device_attribute *attr, 42523a22cd1SDan Williams char *buf) 42623a22cd1SDan Williams { 42723a22cd1SDan Williams struct cxl_region *cxlr = to_cxl_region(dev); 42823a22cd1SDan Williams struct cxl_region_params *p = &cxlr->params; 42923a22cd1SDan Williams u64 resource = -1ULL; 43023a22cd1SDan Williams ssize_t rc; 43123a22cd1SDan Williams 43223a22cd1SDan Williams rc = down_read_interruptible(&cxl_region_rwsem); 43323a22cd1SDan Williams if (rc) 43423a22cd1SDan Williams return rc; 43523a22cd1SDan Williams if (p->res) 43623a22cd1SDan Williams resource = p->res->start; 43723a22cd1SDan Williams rc = sysfs_emit(buf, "%#llx\n", resource); 43823a22cd1SDan Williams up_read(&cxl_region_rwsem); 43923a22cd1SDan Williams 44023a22cd1SDan Williams return rc; 44123a22cd1SDan Williams } 44223a22cd1SDan Williams static DEVICE_ATTR_RO(resource); 44323a22cd1SDan Williams 44423a22cd1SDan Williams static int alloc_hpa(struct cxl_region *cxlr, resource_size_t size) 44523a22cd1SDan Williams { 44623a22cd1SDan Williams struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent); 44723a22cd1SDan Williams struct cxl_region_params *p = &cxlr->params; 44823a22cd1SDan Williams struct resource *res; 44923a22cd1SDan Williams u32 remainder = 0; 45023a22cd1SDan Williams 45123a22cd1SDan Williams lockdep_assert_held_write(&cxl_region_rwsem); 45223a22cd1SDan Williams 45323a22cd1SDan Williams /* Nothing to do... */ 45488ab1ddeSDan Carpenter if (p->res && resource_size(p->res) == size) 45523a22cd1SDan Williams return 0; 45623a22cd1SDan Williams 45723a22cd1SDan Williams /* To change size the old size must be freed first */ 45823a22cd1SDan Williams if (p->res) 45923a22cd1SDan Williams return -EBUSY; 46023a22cd1SDan Williams 46123a22cd1SDan Williams if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) 46223a22cd1SDan Williams return -EBUSY; 46323a22cd1SDan Williams 46423a22cd1SDan Williams /* ways, granularity and uuid (if PMEM) need to be set before HPA */ 46523a22cd1SDan Williams if (!p->interleave_ways || !p->interleave_granularity || 46623a22cd1SDan Williams (cxlr->mode == CXL_DECODER_PMEM && uuid_is_null(&p->uuid))) 46723a22cd1SDan Williams return -ENXIO; 46823a22cd1SDan Williams 46923a22cd1SDan Williams div_u64_rem(size, SZ_256M * p->interleave_ways, &remainder); 47023a22cd1SDan Williams if (remainder) 47123a22cd1SDan Williams return -EINVAL; 47223a22cd1SDan Williams 47323a22cd1SDan Williams res = alloc_free_mem_region(cxlrd->res, size, SZ_256M, 47423a22cd1SDan Williams dev_name(&cxlr->dev)); 47523a22cd1SDan Williams if (IS_ERR(res)) { 47623a22cd1SDan Williams dev_dbg(&cxlr->dev, "failed to allocate HPA: %ld\n", 47723a22cd1SDan Williams PTR_ERR(res)); 47823a22cd1SDan Williams return PTR_ERR(res); 47923a22cd1SDan Williams } 48023a22cd1SDan Williams 48123a22cd1SDan Williams p->res = res; 48223a22cd1SDan Williams p->state = CXL_CONFIG_INTERLEAVE_ACTIVE; 48323a22cd1SDan Williams 48423a22cd1SDan Williams return 0; 48523a22cd1SDan Williams } 48623a22cd1SDan Williams 48723a22cd1SDan Williams static void cxl_region_iomem_release(struct cxl_region *cxlr) 48823a22cd1SDan Williams { 48923a22cd1SDan Williams struct cxl_region_params *p = &cxlr->params; 49023a22cd1SDan Williams 49123a22cd1SDan Williams if (device_is_registered(&cxlr->dev)) 49223a22cd1SDan Williams lockdep_assert_held_write(&cxl_region_rwsem); 49323a22cd1SDan Williams if (p->res) { 49423a22cd1SDan Williams remove_resource(p->res); 49523a22cd1SDan Williams kfree(p->res); 49623a22cd1SDan Williams p->res = NULL; 49723a22cd1SDan Williams } 49823a22cd1SDan Williams } 49923a22cd1SDan Williams 50023a22cd1SDan Williams static int free_hpa(struct cxl_region *cxlr) 50123a22cd1SDan Williams { 50223a22cd1SDan Williams struct cxl_region_params *p = &cxlr->params; 50323a22cd1SDan Williams 50423a22cd1SDan Williams lockdep_assert_held_write(&cxl_region_rwsem); 50523a22cd1SDan Williams 50623a22cd1SDan Williams if (!p->res) 50723a22cd1SDan Williams return 0; 50823a22cd1SDan Williams 50923a22cd1SDan Williams if (p->state >= CXL_CONFIG_ACTIVE) 51023a22cd1SDan Williams return -EBUSY; 51123a22cd1SDan Williams 51223a22cd1SDan Williams cxl_region_iomem_release(cxlr); 51323a22cd1SDan Williams p->state = CXL_CONFIG_IDLE; 51423a22cd1SDan Williams return 0; 51523a22cd1SDan Williams } 51623a22cd1SDan Williams 51723a22cd1SDan Williams static ssize_t size_store(struct device *dev, struct device_attribute *attr, 51823a22cd1SDan Williams const char *buf, size_t len) 51923a22cd1SDan Williams { 52023a22cd1SDan Williams struct cxl_region *cxlr = to_cxl_region(dev); 52123a22cd1SDan Williams u64 val; 52223a22cd1SDan Williams int rc; 52323a22cd1SDan Williams 52423a22cd1SDan Williams rc = kstrtou64(buf, 0, &val); 52523a22cd1SDan Williams if (rc) 52623a22cd1SDan Williams return rc; 52723a22cd1SDan Williams 52823a22cd1SDan Williams rc = down_write_killable(&cxl_region_rwsem); 52923a22cd1SDan Williams if (rc) 53023a22cd1SDan Williams return rc; 53123a22cd1SDan Williams 53223a22cd1SDan Williams if (val) 53323a22cd1SDan Williams rc = alloc_hpa(cxlr, val); 53423a22cd1SDan Williams else 53523a22cd1SDan Williams rc = free_hpa(cxlr); 53623a22cd1SDan Williams up_write(&cxl_region_rwsem); 53723a22cd1SDan Williams 53823a22cd1SDan Williams if (rc) 53923a22cd1SDan Williams return rc; 54023a22cd1SDan Williams 54123a22cd1SDan Williams return len; 54223a22cd1SDan Williams } 54323a22cd1SDan Williams 54423a22cd1SDan Williams static ssize_t size_show(struct device *dev, struct device_attribute *attr, 54523a22cd1SDan Williams char *buf) 54623a22cd1SDan Williams { 54723a22cd1SDan Williams struct cxl_region *cxlr = to_cxl_region(dev); 54823a22cd1SDan Williams struct cxl_region_params *p = &cxlr->params; 54923a22cd1SDan Williams u64 size = 0; 55023a22cd1SDan Williams ssize_t rc; 55123a22cd1SDan Williams 55223a22cd1SDan Williams rc = down_read_interruptible(&cxl_region_rwsem); 55323a22cd1SDan Williams if (rc) 55423a22cd1SDan Williams return rc; 55523a22cd1SDan Williams if (p->res) 55623a22cd1SDan Williams size = resource_size(p->res); 55723a22cd1SDan Williams rc = sysfs_emit(buf, "%#llx\n", size); 55823a22cd1SDan Williams up_read(&cxl_region_rwsem); 55923a22cd1SDan Williams 56023a22cd1SDan Williams return rc; 56123a22cd1SDan Williams } 56223a22cd1SDan Williams static DEVICE_ATTR_RW(size); 56323a22cd1SDan Williams 564dd5ba0ebSBen Widawsky static struct attribute *cxl_region_attrs[] = { 565dd5ba0ebSBen Widawsky &dev_attr_uuid.attr, 566176baefbSDan Williams &dev_attr_commit.attr, 56780d10a6cSBen Widawsky &dev_attr_interleave_ways.attr, 56880d10a6cSBen Widawsky &dev_attr_interleave_granularity.attr, 56923a22cd1SDan Williams &dev_attr_resource.attr, 57023a22cd1SDan Williams &dev_attr_size.attr, 571dd5ba0ebSBen Widawsky NULL, 572dd5ba0ebSBen Widawsky }; 573dd5ba0ebSBen Widawsky 574dd5ba0ebSBen Widawsky static const struct attribute_group cxl_region_group = { 575dd5ba0ebSBen Widawsky .attrs = cxl_region_attrs, 576dd5ba0ebSBen Widawsky .is_visible = cxl_region_visible, 577dd5ba0ebSBen Widawsky }; 578dd5ba0ebSBen Widawsky 579b9686e8cSDan Williams static size_t show_targetN(struct cxl_region *cxlr, char *buf, int pos) 580b9686e8cSDan Williams { 581b9686e8cSDan Williams struct cxl_region_params *p = &cxlr->params; 582b9686e8cSDan Williams struct cxl_endpoint_decoder *cxled; 583b9686e8cSDan Williams int rc; 584b9686e8cSDan Williams 585b9686e8cSDan Williams rc = down_read_interruptible(&cxl_region_rwsem); 586b9686e8cSDan Williams if (rc) 587b9686e8cSDan Williams return rc; 588b9686e8cSDan Williams 589b9686e8cSDan Williams if (pos >= p->interleave_ways) { 590b9686e8cSDan Williams dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos, 591b9686e8cSDan Williams p->interleave_ways); 592b9686e8cSDan Williams rc = -ENXIO; 593b9686e8cSDan Williams goto out; 594b9686e8cSDan Williams } 595b9686e8cSDan Williams 596b9686e8cSDan Williams cxled = p->targets[pos]; 597b9686e8cSDan Williams if (!cxled) 598b9686e8cSDan Williams rc = sysfs_emit(buf, "\n"); 599b9686e8cSDan Williams else 600b9686e8cSDan Williams rc = sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev)); 601b9686e8cSDan Williams out: 602b9686e8cSDan Williams up_read(&cxl_region_rwsem); 603b9686e8cSDan Williams 604b9686e8cSDan Williams return rc; 605b9686e8cSDan Williams } 606b9686e8cSDan Williams 607384e624bSDan Williams static int match_free_decoder(struct device *dev, void *data) 608384e624bSDan Williams { 609384e624bSDan Williams struct cxl_decoder *cxld; 610384e624bSDan Williams int *id = data; 611384e624bSDan Williams 612384e624bSDan Williams if (!is_switch_decoder(dev)) 613384e624bSDan Williams return 0; 614384e624bSDan Williams 615384e624bSDan Williams cxld = to_cxl_decoder(dev); 616384e624bSDan Williams 617384e624bSDan Williams /* enforce ordered allocation */ 618384e624bSDan Williams if (cxld->id != *id) 619384e624bSDan Williams return 0; 620384e624bSDan Williams 621384e624bSDan Williams if (!cxld->region) 622384e624bSDan Williams return 1; 623384e624bSDan Williams 624384e624bSDan Williams (*id)++; 625384e624bSDan Williams 626384e624bSDan Williams return 0; 627384e624bSDan Williams } 628384e624bSDan Williams 629384e624bSDan Williams static struct cxl_decoder *cxl_region_find_decoder(struct cxl_port *port, 630384e624bSDan Williams struct cxl_region *cxlr) 631384e624bSDan Williams { 632384e624bSDan Williams struct device *dev; 633384e624bSDan Williams int id = 0; 634384e624bSDan Williams 635384e624bSDan Williams dev = device_find_child(&port->dev, &id, match_free_decoder); 636384e624bSDan Williams if (!dev) 637384e624bSDan Williams return NULL; 638b9686e8cSDan Williams /* 639384e624bSDan Williams * This decoder is pinned registered as long as the endpoint decoder is 640384e624bSDan Williams * registered, and endpoint decoder unregistration holds the 641384e624bSDan Williams * cxl_region_rwsem over unregister events, so no need to hold on to 642384e624bSDan Williams * this extra reference. 643b9686e8cSDan Williams */ 644384e624bSDan Williams put_device(dev); 645384e624bSDan Williams return to_cxl_decoder(dev); 646384e624bSDan Williams } 647384e624bSDan Williams 648384e624bSDan Williams static struct cxl_region_ref *alloc_region_ref(struct cxl_port *port, 649384e624bSDan Williams struct cxl_region *cxlr) 650384e624bSDan Williams { 651e29a8995SDan Williams struct cxl_region_params *p = &cxlr->params; 652e29a8995SDan Williams struct cxl_region_ref *cxl_rr, *iter; 653e29a8995SDan Williams unsigned long index; 654384e624bSDan Williams int rc; 655384e624bSDan Williams 656e29a8995SDan Williams xa_for_each(&port->regions, index, iter) { 657e29a8995SDan Williams struct cxl_region_params *ip = &iter->region->params; 658e29a8995SDan Williams 659e29a8995SDan Williams if (ip->res->start > p->res->start) { 660e29a8995SDan Williams dev_dbg(&cxlr->dev, 661e29a8995SDan Williams "%s: HPA order violation %s:%pr vs %pr\n", 662e29a8995SDan Williams dev_name(&port->dev), 663e29a8995SDan Williams dev_name(&iter->region->dev), ip->res, p->res); 664e29a8995SDan Williams return ERR_PTR(-EBUSY); 665e29a8995SDan Williams } 666e29a8995SDan Williams } 667e29a8995SDan Williams 668384e624bSDan Williams cxl_rr = kzalloc(sizeof(*cxl_rr), GFP_KERNEL); 669384e624bSDan Williams if (!cxl_rr) 670e29a8995SDan Williams return ERR_PTR(-ENOMEM); 671384e624bSDan Williams cxl_rr->port = port; 672384e624bSDan Williams cxl_rr->region = cxlr; 67327b3f8d1SDan Williams cxl_rr->nr_targets = 1; 674384e624bSDan Williams xa_init(&cxl_rr->endpoints); 675384e624bSDan Williams 676384e624bSDan Williams rc = xa_insert(&port->regions, (unsigned long)cxlr, cxl_rr, GFP_KERNEL); 677384e624bSDan Williams if (rc) { 678384e624bSDan Williams dev_dbg(&cxlr->dev, 679384e624bSDan Williams "%s: failed to track region reference: %d\n", 680384e624bSDan Williams dev_name(&port->dev), rc); 681384e624bSDan Williams kfree(cxl_rr); 682e29a8995SDan Williams return ERR_PTR(rc); 683384e624bSDan Williams } 684384e624bSDan Williams 685384e624bSDan Williams return cxl_rr; 686384e624bSDan Williams } 687384e624bSDan Williams 688384e624bSDan Williams static void free_region_ref(struct cxl_region_ref *cxl_rr) 689384e624bSDan Williams { 690384e624bSDan Williams struct cxl_port *port = cxl_rr->port; 691384e624bSDan Williams struct cxl_region *cxlr = cxl_rr->region; 692384e624bSDan Williams struct cxl_decoder *cxld = cxl_rr->decoder; 693384e624bSDan Williams 694384e624bSDan Williams dev_WARN_ONCE(&cxlr->dev, cxld->region != cxlr, "region mismatch\n"); 695384e624bSDan Williams if (cxld->region == cxlr) { 696384e624bSDan Williams cxld->region = NULL; 697384e624bSDan Williams put_device(&cxlr->dev); 698384e624bSDan Williams } 699384e624bSDan Williams 700384e624bSDan Williams xa_erase(&port->regions, (unsigned long)cxlr); 701384e624bSDan Williams xa_destroy(&cxl_rr->endpoints); 702384e624bSDan Williams kfree(cxl_rr); 703384e624bSDan Williams } 704384e624bSDan Williams 705384e624bSDan Williams static int cxl_rr_ep_add(struct cxl_region_ref *cxl_rr, 706384e624bSDan Williams struct cxl_endpoint_decoder *cxled) 707384e624bSDan Williams { 708384e624bSDan Williams int rc; 709384e624bSDan Williams struct cxl_port *port = cxl_rr->port; 710384e624bSDan Williams struct cxl_region *cxlr = cxl_rr->region; 711384e624bSDan Williams struct cxl_decoder *cxld = cxl_rr->decoder; 712384e624bSDan Williams struct cxl_ep *ep = cxl_ep_load(port, cxled_to_memdev(cxled)); 713384e624bSDan Williams 71427b3f8d1SDan Williams if (ep) { 715384e624bSDan Williams rc = xa_insert(&cxl_rr->endpoints, (unsigned long)cxled, ep, 716384e624bSDan Williams GFP_KERNEL); 717384e624bSDan Williams if (rc) 718384e624bSDan Williams return rc; 71927b3f8d1SDan Williams } 720384e624bSDan Williams cxl_rr->nr_eps++; 721384e624bSDan Williams 722384e624bSDan Williams if (!cxld->region) { 723384e624bSDan Williams cxld->region = cxlr; 724384e624bSDan Williams get_device(&cxlr->dev); 725384e624bSDan Williams } 726384e624bSDan Williams 727384e624bSDan Williams return 0; 728384e624bSDan Williams } 729384e624bSDan Williams 730384e624bSDan Williams /** 731384e624bSDan Williams * cxl_port_attach_region() - track a region's interest in a port by endpoint 732384e624bSDan Williams * @port: port to add a new region reference 'struct cxl_region_ref' 733384e624bSDan Williams * @cxlr: region to attach to @port 734384e624bSDan Williams * @cxled: endpoint decoder used to create or further pin a region reference 735384e624bSDan Williams * @pos: interleave position of @cxled in @cxlr 736384e624bSDan Williams * 737384e624bSDan Williams * The attach event is an opportunity to validate CXL decode setup 738384e624bSDan Williams * constraints and record metadata needed for programming HDM decoders, 739384e624bSDan Williams * in particular decoder target lists. 740384e624bSDan Williams * 741384e624bSDan Williams * The steps are: 742*f13da0d9SBagas Sanjaya * 743384e624bSDan Williams * - validate that there are no other regions with a higher HPA already 744384e624bSDan Williams * associated with @port 745384e624bSDan Williams * - establish a region reference if one is not already present 746*f13da0d9SBagas Sanjaya * 747384e624bSDan Williams * - additionally allocate a decoder instance that will host @cxlr on 748384e624bSDan Williams * @port 749*f13da0d9SBagas Sanjaya * 750384e624bSDan Williams * - pin the region reference by the endpoint 751384e624bSDan Williams * - account for how many entries in @port's target list are needed to 752384e624bSDan Williams * cover all of the added endpoints. 753384e624bSDan Williams */ 754384e624bSDan Williams static int cxl_port_attach_region(struct cxl_port *port, 755384e624bSDan Williams struct cxl_region *cxlr, 756384e624bSDan Williams struct cxl_endpoint_decoder *cxled, int pos) 757384e624bSDan Williams { 758384e624bSDan Williams struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 759384e624bSDan Williams struct cxl_ep *ep = cxl_ep_load(port, cxlmd); 760e29a8995SDan Williams struct cxl_region_ref *cxl_rr; 761e29a8995SDan Williams bool nr_targets_inc = false; 762e29a8995SDan Williams struct cxl_decoder *cxld; 763384e624bSDan Williams unsigned long index; 764384e624bSDan Williams int rc = -EBUSY; 765384e624bSDan Williams 766384e624bSDan Williams lockdep_assert_held_write(&cxl_region_rwsem); 767384e624bSDan Williams 768e29a8995SDan Williams cxl_rr = cxl_rr_load(port, cxlr); 769384e624bSDan Williams if (cxl_rr) { 770384e624bSDan Williams struct cxl_ep *ep_iter; 771384e624bSDan Williams int found = 0; 772384e624bSDan Williams 773e29a8995SDan Williams /* 774e29a8995SDan Williams * Walk the existing endpoints that have been attached to 775e29a8995SDan Williams * @cxlr at @port and see if they share the same 'next' port 776e29a8995SDan Williams * in the downstream direction. I.e. endpoints that share common 777e29a8995SDan Williams * upstream switch. 778e29a8995SDan Williams */ 779384e624bSDan Williams xa_for_each(&cxl_rr->endpoints, index, ep_iter) { 780384e624bSDan Williams if (ep_iter == ep) 781384e624bSDan Williams continue; 782384e624bSDan Williams if (ep_iter->next == ep->next) { 783384e624bSDan Williams found++; 784384e624bSDan Williams break; 785384e624bSDan Williams } 786384e624bSDan Williams } 787384e624bSDan Williams 788384e624bSDan Williams /* 789e29a8995SDan Williams * New target port, or @port is an endpoint port that always 790e29a8995SDan Williams * accounts its own local decode as a target. 791384e624bSDan Williams */ 792e29a8995SDan Williams if (!found || !ep->next) { 793384e624bSDan Williams cxl_rr->nr_targets++; 794e29a8995SDan Williams nr_targets_inc = true; 795e29a8995SDan Williams } 796e29a8995SDan Williams 797e29a8995SDan Williams /* 798e29a8995SDan Williams * The decoder for @cxlr was allocated when the region was first 799e29a8995SDan Williams * attached to @port. 800e29a8995SDan Williams */ 801e29a8995SDan Williams cxld = cxl_rr->decoder; 802384e624bSDan Williams } else { 803384e624bSDan Williams cxl_rr = alloc_region_ref(port, cxlr); 804e29a8995SDan Williams if (IS_ERR(cxl_rr)) { 805384e624bSDan Williams dev_dbg(&cxlr->dev, 806384e624bSDan Williams "%s: failed to allocate region reference\n", 807384e624bSDan Williams dev_name(&port->dev)); 808e29a8995SDan Williams return PTR_ERR(cxl_rr); 809384e624bSDan Williams } 810e29a8995SDan Williams nr_targets_inc = true; 811384e624bSDan Williams 812384e624bSDan Williams if (port == cxled_to_port(cxled)) 813384e624bSDan Williams cxld = &cxled->cxld; 814384e624bSDan Williams else 815384e624bSDan Williams cxld = cxl_region_find_decoder(port, cxlr); 816384e624bSDan Williams if (!cxld) { 817384e624bSDan Williams dev_dbg(&cxlr->dev, "%s: no decoder available\n", 818384e624bSDan Williams dev_name(&port->dev)); 819384e624bSDan Williams goto out_erase; 820384e624bSDan Williams } 821384e624bSDan Williams 822384e624bSDan Williams if (cxld->region) { 823384e624bSDan Williams dev_dbg(&cxlr->dev, "%s: %s already attached to %s\n", 824384e624bSDan Williams dev_name(&port->dev), dev_name(&cxld->dev), 825384e624bSDan Williams dev_name(&cxld->region->dev)); 826384e624bSDan Williams rc = -EBUSY; 827384e624bSDan Williams goto out_erase; 828384e624bSDan Williams } 829384e624bSDan Williams 830384e624bSDan Williams cxl_rr->decoder = cxld; 831384e624bSDan Williams } 832384e624bSDan Williams 833384e624bSDan Williams rc = cxl_rr_ep_add(cxl_rr, cxled); 834384e624bSDan Williams if (rc) { 835384e624bSDan Williams dev_dbg(&cxlr->dev, 836384e624bSDan Williams "%s: failed to track endpoint %s:%s reference\n", 837384e624bSDan Williams dev_name(&port->dev), dev_name(&cxlmd->dev), 838384e624bSDan Williams dev_name(&cxld->dev)); 839384e624bSDan Williams goto out_erase; 840384e624bSDan Williams } 841384e624bSDan Williams 84227b3f8d1SDan Williams dev_dbg(&cxlr->dev, 84327b3f8d1SDan Williams "%s:%s %s add: %s:%s @ %d next: %s nr_eps: %d nr_targets: %d\n", 84427b3f8d1SDan Williams dev_name(port->uport), dev_name(&port->dev), 84527b3f8d1SDan Williams dev_name(&cxld->dev), dev_name(&cxlmd->dev), 84627b3f8d1SDan Williams dev_name(&cxled->cxld.dev), pos, 84727b3f8d1SDan Williams ep ? ep->next ? dev_name(ep->next->uport) : 84827b3f8d1SDan Williams dev_name(&cxlmd->dev) : 84927b3f8d1SDan Williams "none", 85027b3f8d1SDan Williams cxl_rr->nr_eps, cxl_rr->nr_targets); 85127b3f8d1SDan Williams 852384e624bSDan Williams return 0; 853384e624bSDan Williams out_erase: 854e29a8995SDan Williams if (nr_targets_inc) 855e29a8995SDan Williams cxl_rr->nr_targets--; 856384e624bSDan Williams if (cxl_rr->nr_eps == 0) 857384e624bSDan Williams free_region_ref(cxl_rr); 858384e624bSDan Williams return rc; 859384e624bSDan Williams } 860384e624bSDan Williams 861384e624bSDan Williams static void cxl_port_detach_region(struct cxl_port *port, 862384e624bSDan Williams struct cxl_region *cxlr, 863384e624bSDan Williams struct cxl_endpoint_decoder *cxled) 864384e624bSDan Williams { 865384e624bSDan Williams struct cxl_region_ref *cxl_rr; 86627b3f8d1SDan Williams struct cxl_ep *ep = NULL; 867384e624bSDan Williams 868384e624bSDan Williams lockdep_assert_held_write(&cxl_region_rwsem); 869384e624bSDan Williams 870384e624bSDan Williams cxl_rr = cxl_rr_load(port, cxlr); 871384e624bSDan Williams if (!cxl_rr) 872384e624bSDan Williams return; 873384e624bSDan Williams 87427b3f8d1SDan Williams /* 87527b3f8d1SDan Williams * Endpoint ports do not carry cxl_ep references, and they 87627b3f8d1SDan Williams * never target more than one endpoint by definition 87727b3f8d1SDan Williams */ 87827b3f8d1SDan Williams if (cxl_rr->decoder == &cxled->cxld) 87927b3f8d1SDan Williams cxl_rr->nr_eps--; 88027b3f8d1SDan Williams else 881384e624bSDan Williams ep = xa_erase(&cxl_rr->endpoints, (unsigned long)cxled); 882384e624bSDan Williams if (ep) { 883384e624bSDan Williams struct cxl_ep *ep_iter; 884384e624bSDan Williams unsigned long index; 885384e624bSDan Williams int found = 0; 886384e624bSDan Williams 887384e624bSDan Williams cxl_rr->nr_eps--; 888384e624bSDan Williams xa_for_each(&cxl_rr->endpoints, index, ep_iter) { 889384e624bSDan Williams if (ep_iter->next == ep->next) { 890384e624bSDan Williams found++; 891384e624bSDan Williams break; 892384e624bSDan Williams } 893384e624bSDan Williams } 894384e624bSDan Williams if (!found) 895384e624bSDan Williams cxl_rr->nr_targets--; 896384e624bSDan Williams } 897384e624bSDan Williams 898384e624bSDan Williams if (cxl_rr->nr_eps == 0) 899384e624bSDan Williams free_region_ref(cxl_rr); 900384e624bSDan Williams } 901384e624bSDan Williams 90227b3f8d1SDan Williams static int check_last_peer(struct cxl_endpoint_decoder *cxled, 90327b3f8d1SDan Williams struct cxl_ep *ep, struct cxl_region_ref *cxl_rr, 90427b3f8d1SDan Williams int distance) 90527b3f8d1SDan Williams { 90627b3f8d1SDan Williams struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 90727b3f8d1SDan Williams struct cxl_region *cxlr = cxl_rr->region; 90827b3f8d1SDan Williams struct cxl_region_params *p = &cxlr->params; 90927b3f8d1SDan Williams struct cxl_endpoint_decoder *cxled_peer; 91027b3f8d1SDan Williams struct cxl_port *port = cxl_rr->port; 91127b3f8d1SDan Williams struct cxl_memdev *cxlmd_peer; 91227b3f8d1SDan Williams struct cxl_ep *ep_peer; 91327b3f8d1SDan Williams int pos = cxled->pos; 91427b3f8d1SDan Williams 91527b3f8d1SDan Williams /* 91627b3f8d1SDan Williams * If this position wants to share a dport with the last endpoint mapped 91727b3f8d1SDan Williams * then that endpoint, at index 'position - distance', must also be 91827b3f8d1SDan Williams * mapped by this dport. 91927b3f8d1SDan Williams */ 92027b3f8d1SDan Williams if (pos < distance) { 92127b3f8d1SDan Williams dev_dbg(&cxlr->dev, "%s:%s: cannot host %s:%s at %d\n", 92227b3f8d1SDan Williams dev_name(port->uport), dev_name(&port->dev), 92327b3f8d1SDan Williams dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos); 92427b3f8d1SDan Williams return -ENXIO; 92527b3f8d1SDan Williams } 92627b3f8d1SDan Williams cxled_peer = p->targets[pos - distance]; 92727b3f8d1SDan Williams cxlmd_peer = cxled_to_memdev(cxled_peer); 92827b3f8d1SDan Williams ep_peer = cxl_ep_load(port, cxlmd_peer); 92927b3f8d1SDan Williams if (ep->dport != ep_peer->dport) { 93027b3f8d1SDan Williams dev_dbg(&cxlr->dev, 93127b3f8d1SDan Williams "%s:%s: %s:%s pos %d mismatched peer %s:%s\n", 93227b3f8d1SDan Williams dev_name(port->uport), dev_name(&port->dev), 93327b3f8d1SDan Williams dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos, 93427b3f8d1SDan Williams dev_name(&cxlmd_peer->dev), 93527b3f8d1SDan Williams dev_name(&cxled_peer->cxld.dev)); 93627b3f8d1SDan Williams return -ENXIO; 93727b3f8d1SDan Williams } 93827b3f8d1SDan Williams 93927b3f8d1SDan Williams return 0; 94027b3f8d1SDan Williams } 94127b3f8d1SDan Williams 94227b3f8d1SDan Williams static int cxl_port_setup_targets(struct cxl_port *port, 94327b3f8d1SDan Williams struct cxl_region *cxlr, 94427b3f8d1SDan Williams struct cxl_endpoint_decoder *cxled) 94527b3f8d1SDan Williams { 94627b3f8d1SDan Williams struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent); 94727b3f8d1SDan Williams int parent_iw, parent_ig, ig, iw, rc, inc = 0, pos = cxled->pos; 94827b3f8d1SDan Williams struct cxl_port *parent_port = to_cxl_port(port->dev.parent); 94927b3f8d1SDan Williams struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr); 95027b3f8d1SDan Williams struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 95127b3f8d1SDan Williams struct cxl_ep *ep = cxl_ep_load(port, cxlmd); 95227b3f8d1SDan Williams struct cxl_region_params *p = &cxlr->params; 95327b3f8d1SDan Williams struct cxl_decoder *cxld = cxl_rr->decoder; 95427b3f8d1SDan Williams struct cxl_switch_decoder *cxlsd; 95527b3f8d1SDan Williams u16 eig, peig; 95627b3f8d1SDan Williams u8 eiw, peiw; 95727b3f8d1SDan Williams 95827b3f8d1SDan Williams /* 95927b3f8d1SDan Williams * While root level decoders support x3, x6, x12, switch level 96027b3f8d1SDan Williams * decoders only support powers of 2 up to x16. 96127b3f8d1SDan Williams */ 96227b3f8d1SDan Williams if (!is_power_of_2(cxl_rr->nr_targets)) { 96327b3f8d1SDan Williams dev_dbg(&cxlr->dev, "%s:%s: invalid target count %d\n", 96427b3f8d1SDan Williams dev_name(port->uport), dev_name(&port->dev), 96527b3f8d1SDan Williams cxl_rr->nr_targets); 96627b3f8d1SDan Williams return -EINVAL; 96727b3f8d1SDan Williams } 96827b3f8d1SDan Williams 96927b3f8d1SDan Williams cxlsd = to_cxl_switch_decoder(&cxld->dev); 97027b3f8d1SDan Williams if (cxl_rr->nr_targets_set) { 97127b3f8d1SDan Williams int i, distance; 97227b3f8d1SDan Williams 97327b3f8d1SDan Williams distance = p->nr_targets / cxl_rr->nr_targets; 97427b3f8d1SDan Williams for (i = 0; i < cxl_rr->nr_targets_set; i++) 97527b3f8d1SDan Williams if (ep->dport == cxlsd->target[i]) { 97627b3f8d1SDan Williams rc = check_last_peer(cxled, ep, cxl_rr, 97727b3f8d1SDan Williams distance); 97827b3f8d1SDan Williams if (rc) 97927b3f8d1SDan Williams return rc; 98027b3f8d1SDan Williams goto out_target_set; 98127b3f8d1SDan Williams } 98227b3f8d1SDan Williams goto add_target; 98327b3f8d1SDan Williams } 98427b3f8d1SDan Williams 98527b3f8d1SDan Williams if (is_cxl_root(parent_port)) { 98627b3f8d1SDan Williams parent_ig = cxlrd->cxlsd.cxld.interleave_granularity; 98727b3f8d1SDan Williams parent_iw = cxlrd->cxlsd.cxld.interleave_ways; 98827b3f8d1SDan Williams /* 98927b3f8d1SDan Williams * For purposes of address bit routing, use power-of-2 math for 99027b3f8d1SDan Williams * switch ports. 99127b3f8d1SDan Williams */ 99227b3f8d1SDan Williams if (!is_power_of_2(parent_iw)) 99327b3f8d1SDan Williams parent_iw /= 3; 99427b3f8d1SDan Williams } else { 99527b3f8d1SDan Williams struct cxl_region_ref *parent_rr; 99627b3f8d1SDan Williams struct cxl_decoder *parent_cxld; 99727b3f8d1SDan Williams 99827b3f8d1SDan Williams parent_rr = cxl_rr_load(parent_port, cxlr); 99927b3f8d1SDan Williams parent_cxld = parent_rr->decoder; 100027b3f8d1SDan Williams parent_ig = parent_cxld->interleave_granularity; 100127b3f8d1SDan Williams parent_iw = parent_cxld->interleave_ways; 100227b3f8d1SDan Williams } 100327b3f8d1SDan Williams 10048d428542SDan Williams rc = granularity_to_cxl(parent_ig, &peig); 10058d428542SDan Williams if (rc) { 10068d428542SDan Williams dev_dbg(&cxlr->dev, "%s:%s: invalid parent granularity: %d\n", 10078d428542SDan Williams dev_name(parent_port->uport), 10088d428542SDan Williams dev_name(&parent_port->dev), parent_ig); 10098d428542SDan Williams return rc; 10108d428542SDan Williams } 10118d428542SDan Williams 10128d428542SDan Williams rc = ways_to_cxl(parent_iw, &peiw); 10138d428542SDan Williams if (rc) { 10148d428542SDan Williams dev_dbg(&cxlr->dev, "%s:%s: invalid parent interleave: %d\n", 10158d428542SDan Williams dev_name(parent_port->uport), 10168d428542SDan Williams dev_name(&parent_port->dev), parent_iw); 10178d428542SDan Williams return rc; 10188d428542SDan Williams } 101927b3f8d1SDan Williams 102027b3f8d1SDan Williams iw = cxl_rr->nr_targets; 10218d428542SDan Williams rc = ways_to_cxl(iw, &eiw); 10228d428542SDan Williams if (rc) { 10238d428542SDan Williams dev_dbg(&cxlr->dev, "%s:%s: invalid port interleave: %d\n", 10248d428542SDan Williams dev_name(port->uport), dev_name(&port->dev), iw); 10258d428542SDan Williams return rc; 10268d428542SDan Williams } 10278d428542SDan Williams 102827b3f8d1SDan Williams if (cxl_rr->nr_targets > 1) { 102927b3f8d1SDan Williams u32 address_bit = max(peig + peiw, eiw + peig); 103027b3f8d1SDan Williams 103127b3f8d1SDan Williams eig = address_bit - eiw + 1; 103227b3f8d1SDan Williams } else { 103327b3f8d1SDan Williams eiw = peiw; 103427b3f8d1SDan Williams eig = peig; 103527b3f8d1SDan Williams } 103627b3f8d1SDan Williams 103727b3f8d1SDan Williams rc = cxl_to_granularity(eig, &ig); 103827b3f8d1SDan Williams if (rc) { 103927b3f8d1SDan Williams dev_dbg(&cxlr->dev, "%s:%s: invalid interleave: %d\n", 104027b3f8d1SDan Williams dev_name(port->uport), dev_name(&port->dev), 104127b3f8d1SDan Williams 256 << eig); 104227b3f8d1SDan Williams return rc; 104327b3f8d1SDan Williams } 104427b3f8d1SDan Williams 104527b3f8d1SDan Williams cxld->interleave_ways = iw; 104627b3f8d1SDan Williams cxld->interleave_granularity = ig; 104727b3f8d1SDan Williams dev_dbg(&cxlr->dev, "%s:%s iw: %d ig: %d\n", dev_name(port->uport), 104827b3f8d1SDan Williams dev_name(&port->dev), iw, ig); 104927b3f8d1SDan Williams add_target: 105027b3f8d1SDan Williams if (cxl_rr->nr_targets_set == cxl_rr->nr_targets) { 105127b3f8d1SDan Williams dev_dbg(&cxlr->dev, 105227b3f8d1SDan Williams "%s:%s: targets full trying to add %s:%s at %d\n", 105327b3f8d1SDan Williams dev_name(port->uport), dev_name(&port->dev), 105427b3f8d1SDan Williams dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos); 105527b3f8d1SDan Williams return -ENXIO; 105627b3f8d1SDan Williams } 105727b3f8d1SDan Williams cxlsd->target[cxl_rr->nr_targets_set] = ep->dport; 105827b3f8d1SDan Williams inc = 1; 105927b3f8d1SDan Williams out_target_set: 106027b3f8d1SDan Williams cxl_rr->nr_targets_set += inc; 106127b3f8d1SDan Williams dev_dbg(&cxlr->dev, "%s:%s target[%d] = %s for %s:%s @ %d\n", 106227b3f8d1SDan Williams dev_name(port->uport), dev_name(&port->dev), 106327b3f8d1SDan Williams cxl_rr->nr_targets_set - 1, dev_name(ep->dport->dport), 106427b3f8d1SDan Williams dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos); 106527b3f8d1SDan Williams 106627b3f8d1SDan Williams return 0; 106727b3f8d1SDan Williams } 106827b3f8d1SDan Williams 106927b3f8d1SDan Williams static void cxl_port_reset_targets(struct cxl_port *port, 107027b3f8d1SDan Williams struct cxl_region *cxlr) 107127b3f8d1SDan Williams { 107227b3f8d1SDan Williams struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr); 107327b3f8d1SDan Williams 107427b3f8d1SDan Williams /* 107527b3f8d1SDan Williams * After the last endpoint has been detached the entire cxl_rr may now 107627b3f8d1SDan Williams * be gone. 107727b3f8d1SDan Williams */ 107827b3f8d1SDan Williams if (cxl_rr) 107927b3f8d1SDan Williams cxl_rr->nr_targets_set = 0; 108027b3f8d1SDan Williams } 108127b3f8d1SDan Williams 108227b3f8d1SDan Williams static void cxl_region_teardown_targets(struct cxl_region *cxlr) 108327b3f8d1SDan Williams { 108427b3f8d1SDan Williams struct cxl_region_params *p = &cxlr->params; 108527b3f8d1SDan Williams struct cxl_endpoint_decoder *cxled; 108627b3f8d1SDan Williams struct cxl_memdev *cxlmd; 108727b3f8d1SDan Williams struct cxl_port *iter; 108827b3f8d1SDan Williams struct cxl_ep *ep; 108927b3f8d1SDan Williams int i; 109027b3f8d1SDan Williams 109127b3f8d1SDan Williams for (i = 0; i < p->nr_targets; i++) { 109227b3f8d1SDan Williams cxled = p->targets[i]; 109327b3f8d1SDan Williams cxlmd = cxled_to_memdev(cxled); 109427b3f8d1SDan Williams 109527b3f8d1SDan Williams iter = cxled_to_port(cxled); 109627b3f8d1SDan Williams while (!is_cxl_root(to_cxl_port(iter->dev.parent))) 109727b3f8d1SDan Williams iter = to_cxl_port(iter->dev.parent); 109827b3f8d1SDan Williams 109927b3f8d1SDan Williams for (ep = cxl_ep_load(iter, cxlmd); iter; 110027b3f8d1SDan Williams iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) 110127b3f8d1SDan Williams cxl_port_reset_targets(iter, cxlr); 110227b3f8d1SDan Williams } 110327b3f8d1SDan Williams } 110427b3f8d1SDan Williams 110527b3f8d1SDan Williams static int cxl_region_setup_targets(struct cxl_region *cxlr) 110627b3f8d1SDan Williams { 110727b3f8d1SDan Williams struct cxl_region_params *p = &cxlr->params; 110827b3f8d1SDan Williams struct cxl_endpoint_decoder *cxled; 110927b3f8d1SDan Williams struct cxl_memdev *cxlmd; 111027b3f8d1SDan Williams struct cxl_port *iter; 111127b3f8d1SDan Williams struct cxl_ep *ep; 111227b3f8d1SDan Williams int i, rc; 111327b3f8d1SDan Williams 111427b3f8d1SDan Williams for (i = 0; i < p->nr_targets; i++) { 111527b3f8d1SDan Williams cxled = p->targets[i]; 111627b3f8d1SDan Williams cxlmd = cxled_to_memdev(cxled); 111727b3f8d1SDan Williams 111827b3f8d1SDan Williams iter = cxled_to_port(cxled); 111927b3f8d1SDan Williams while (!is_cxl_root(to_cxl_port(iter->dev.parent))) 112027b3f8d1SDan Williams iter = to_cxl_port(iter->dev.parent); 112127b3f8d1SDan Williams 112227b3f8d1SDan Williams /* 112327b3f8d1SDan Williams * Descend the topology tree programming targets while 112427b3f8d1SDan Williams * looking for conflicts. 112527b3f8d1SDan Williams */ 112627b3f8d1SDan Williams for (ep = cxl_ep_load(iter, cxlmd); iter; 112727b3f8d1SDan Williams iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) { 112827b3f8d1SDan Williams rc = cxl_port_setup_targets(iter, cxlr, cxled); 112927b3f8d1SDan Williams if (rc) { 113027b3f8d1SDan Williams cxl_region_teardown_targets(cxlr); 113127b3f8d1SDan Williams return rc; 113227b3f8d1SDan Williams } 113327b3f8d1SDan Williams } 113427b3f8d1SDan Williams } 113527b3f8d1SDan Williams 113627b3f8d1SDan Williams return 0; 113727b3f8d1SDan Williams } 113827b3f8d1SDan Williams 1139b9686e8cSDan Williams static int cxl_region_attach(struct cxl_region *cxlr, 1140b9686e8cSDan Williams struct cxl_endpoint_decoder *cxled, int pos) 1141b9686e8cSDan Williams { 1142384e624bSDan Williams struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent); 1143384e624bSDan Williams struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 1144384e624bSDan Williams struct cxl_port *ep_port, *root_port, *iter; 1145b9686e8cSDan Williams struct cxl_region_params *p = &cxlr->params; 1146384e624bSDan Williams struct cxl_dport *dport; 1147384e624bSDan Williams int i, rc = -ENXIO; 1148b9686e8cSDan Williams 1149b9686e8cSDan Williams if (cxled->mode == CXL_DECODER_DEAD) { 1150b9686e8cSDan Williams dev_dbg(&cxlr->dev, "%s dead\n", dev_name(&cxled->cxld.dev)); 1151b9686e8cSDan Williams return -ENODEV; 1152b9686e8cSDan Williams } 1153b9686e8cSDan Williams 1154384e624bSDan Williams /* all full of members, or interleave config not established? */ 1155384e624bSDan Williams if (p->state > CXL_CONFIG_INTERLEAVE_ACTIVE) { 1156384e624bSDan Williams dev_dbg(&cxlr->dev, "region already active\n"); 1157384e624bSDan Williams return -EBUSY; 1158384e624bSDan Williams } else if (p->state < CXL_CONFIG_INTERLEAVE_ACTIVE) { 1159384e624bSDan Williams dev_dbg(&cxlr->dev, "interleave config missing\n"); 1160384e624bSDan Williams return -ENXIO; 1161384e624bSDan Williams } 1162384e624bSDan Williams 1163384e624bSDan Williams if (pos < 0 || pos >= p->interleave_ways) { 1164b9686e8cSDan Williams dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos, 1165b9686e8cSDan Williams p->interleave_ways); 1166b9686e8cSDan Williams return -ENXIO; 1167b9686e8cSDan Williams } 1168b9686e8cSDan Williams 1169b9686e8cSDan Williams if (p->targets[pos] == cxled) 1170b9686e8cSDan Williams return 0; 1171b9686e8cSDan Williams 1172b9686e8cSDan Williams if (p->targets[pos]) { 1173b9686e8cSDan Williams struct cxl_endpoint_decoder *cxled_target = p->targets[pos]; 1174b9686e8cSDan Williams struct cxl_memdev *cxlmd_target = cxled_to_memdev(cxled_target); 1175b9686e8cSDan Williams 1176b9686e8cSDan Williams dev_dbg(&cxlr->dev, "position %d already assigned to %s:%s\n", 1177b9686e8cSDan Williams pos, dev_name(&cxlmd_target->dev), 1178b9686e8cSDan Williams dev_name(&cxled_target->cxld.dev)); 1179b9686e8cSDan Williams return -EBUSY; 1180b9686e8cSDan Williams } 1181b9686e8cSDan Williams 1182384e624bSDan Williams for (i = 0; i < p->interleave_ways; i++) { 1183384e624bSDan Williams struct cxl_endpoint_decoder *cxled_target; 1184384e624bSDan Williams struct cxl_memdev *cxlmd_target; 1185384e624bSDan Williams 1186384e624bSDan Williams cxled_target = p->targets[pos]; 1187384e624bSDan Williams if (!cxled_target) 1188384e624bSDan Williams continue; 1189384e624bSDan Williams 1190384e624bSDan Williams cxlmd_target = cxled_to_memdev(cxled_target); 1191384e624bSDan Williams if (cxlmd_target == cxlmd) { 1192384e624bSDan Williams dev_dbg(&cxlr->dev, 1193384e624bSDan Williams "%s already specified at position %d via: %s\n", 1194384e624bSDan Williams dev_name(&cxlmd->dev), pos, 1195384e624bSDan Williams dev_name(&cxled_target->cxld.dev)); 1196384e624bSDan Williams return -EBUSY; 1197384e624bSDan Williams } 1198384e624bSDan Williams } 1199384e624bSDan Williams 1200384e624bSDan Williams ep_port = cxled_to_port(cxled); 1201384e624bSDan Williams root_port = cxlrd_to_port(cxlrd); 1202384e624bSDan Williams dport = cxl_find_dport_by_dev(root_port, ep_port->host_bridge); 1203384e624bSDan Williams if (!dport) { 1204384e624bSDan Williams dev_dbg(&cxlr->dev, "%s:%s invalid target for %s\n", 1205384e624bSDan Williams dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), 1206384e624bSDan Williams dev_name(cxlr->dev.parent)); 1207384e624bSDan Williams return -ENXIO; 1208384e624bSDan Williams } 1209384e624bSDan Williams 1210384e624bSDan Williams if (cxlrd->calc_hb(cxlrd, pos) != dport) { 1211384e624bSDan Williams dev_dbg(&cxlr->dev, "%s:%s invalid target position for %s\n", 1212384e624bSDan Williams dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), 1213384e624bSDan Williams dev_name(&cxlrd->cxlsd.cxld.dev)); 1214384e624bSDan Williams return -ENXIO; 1215384e624bSDan Williams } 1216384e624bSDan Williams 1217384e624bSDan Williams if (cxled->cxld.target_type != cxlr->type) { 1218384e624bSDan Williams dev_dbg(&cxlr->dev, "%s:%s type mismatch: %d vs %d\n", 1219384e624bSDan Williams dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), 1220384e624bSDan Williams cxled->cxld.target_type, cxlr->type); 1221384e624bSDan Williams return -ENXIO; 1222384e624bSDan Williams } 1223384e624bSDan Williams 1224384e624bSDan Williams if (!cxled->dpa_res) { 1225384e624bSDan Williams dev_dbg(&cxlr->dev, "%s:%s: missing DPA allocation.\n", 1226384e624bSDan Williams dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev)); 1227384e624bSDan Williams return -ENXIO; 1228384e624bSDan Williams } 1229384e624bSDan Williams 1230384e624bSDan Williams if (resource_size(cxled->dpa_res) * p->interleave_ways != 1231384e624bSDan Williams resource_size(p->res)) { 1232384e624bSDan Williams dev_dbg(&cxlr->dev, 1233384e624bSDan Williams "%s:%s: decoder-size-%#llx * ways-%d != region-size-%#llx\n", 1234384e624bSDan Williams dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), 1235384e624bSDan Williams (u64)resource_size(cxled->dpa_res), p->interleave_ways, 1236384e624bSDan Williams (u64)resource_size(p->res)); 1237384e624bSDan Williams return -EINVAL; 1238384e624bSDan Williams } 1239384e624bSDan Williams 1240384e624bSDan Williams for (iter = ep_port; !is_cxl_root(iter); 1241384e624bSDan Williams iter = to_cxl_port(iter->dev.parent)) { 1242384e624bSDan Williams rc = cxl_port_attach_region(iter, cxlr, cxled, pos); 1243384e624bSDan Williams if (rc) 1244384e624bSDan Williams goto err; 1245384e624bSDan Williams } 1246384e624bSDan Williams 1247b9686e8cSDan Williams p->targets[pos] = cxled; 1248b9686e8cSDan Williams cxled->pos = pos; 1249b9686e8cSDan Williams p->nr_targets++; 1250b9686e8cSDan Williams 125127b3f8d1SDan Williams if (p->nr_targets == p->interleave_ways) { 125227b3f8d1SDan Williams rc = cxl_region_setup_targets(cxlr); 125327b3f8d1SDan Williams if (rc) 12545e42bcbcSDan Carpenter goto err_decrement; 1255384e624bSDan Williams p->state = CXL_CONFIG_ACTIVE; 125627b3f8d1SDan Williams } 1257384e624bSDan Williams 1258b9686e8cSDan Williams return 0; 1259384e624bSDan Williams 12605e42bcbcSDan Carpenter err_decrement: 12615e42bcbcSDan Carpenter p->nr_targets--; 1262384e624bSDan Williams err: 1263384e624bSDan Williams for (iter = ep_port; !is_cxl_root(iter); 1264384e624bSDan Williams iter = to_cxl_port(iter->dev.parent)) 1265384e624bSDan Williams cxl_port_detach_region(iter, cxlr, cxled); 1266384e624bSDan Williams return rc; 1267b9686e8cSDan Williams } 1268b9686e8cSDan Williams 1269176baefbSDan Williams static int cxl_region_detach(struct cxl_endpoint_decoder *cxled) 1270b9686e8cSDan Williams { 1271384e624bSDan Williams struct cxl_port *iter, *ep_port = cxled_to_port(cxled); 1272b9686e8cSDan Williams struct cxl_region *cxlr = cxled->cxld.region; 1273b9686e8cSDan Williams struct cxl_region_params *p; 1274176baefbSDan Williams int rc = 0; 1275b9686e8cSDan Williams 1276b9686e8cSDan Williams lockdep_assert_held_write(&cxl_region_rwsem); 1277b9686e8cSDan Williams 1278b9686e8cSDan Williams if (!cxlr) 1279176baefbSDan Williams return 0; 1280b9686e8cSDan Williams 1281b9686e8cSDan Williams p = &cxlr->params; 1282b9686e8cSDan Williams get_device(&cxlr->dev); 1283b9686e8cSDan Williams 1284176baefbSDan Williams if (p->state > CXL_CONFIG_ACTIVE) { 1285176baefbSDan Williams /* 1286176baefbSDan Williams * TODO: tear down all impacted regions if a device is 1287176baefbSDan Williams * removed out of order 1288176baefbSDan Williams */ 1289176baefbSDan Williams rc = cxl_region_decode_reset(cxlr, p->interleave_ways); 1290176baefbSDan Williams if (rc) 1291176baefbSDan Williams goto out; 1292176baefbSDan Williams p->state = CXL_CONFIG_ACTIVE; 1293176baefbSDan Williams } 1294176baefbSDan Williams 1295384e624bSDan Williams for (iter = ep_port; !is_cxl_root(iter); 1296384e624bSDan Williams iter = to_cxl_port(iter->dev.parent)) 1297384e624bSDan Williams cxl_port_detach_region(iter, cxlr, cxled); 1298384e624bSDan Williams 1299b9686e8cSDan Williams if (cxled->pos < 0 || cxled->pos >= p->interleave_ways || 1300b9686e8cSDan Williams p->targets[cxled->pos] != cxled) { 1301b9686e8cSDan Williams struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 1302b9686e8cSDan Williams 1303b9686e8cSDan Williams dev_WARN_ONCE(&cxlr->dev, 1, "expected %s:%s at position %d\n", 1304b9686e8cSDan Williams dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), 1305b9686e8cSDan Williams cxled->pos); 1306b9686e8cSDan Williams goto out; 1307b9686e8cSDan Williams } 1308b9686e8cSDan Williams 130927b3f8d1SDan Williams if (p->state == CXL_CONFIG_ACTIVE) { 1310384e624bSDan Williams p->state = CXL_CONFIG_INTERLEAVE_ACTIVE; 131127b3f8d1SDan Williams cxl_region_teardown_targets(cxlr); 131227b3f8d1SDan Williams } 1313b9686e8cSDan Williams p->targets[cxled->pos] = NULL; 1314b9686e8cSDan Williams p->nr_targets--; 1315b9686e8cSDan Williams 1316384e624bSDan Williams /* notify the region driver that one of its targets has departed */ 1317b9686e8cSDan Williams up_write(&cxl_region_rwsem); 1318b9686e8cSDan Williams device_release_driver(&cxlr->dev); 1319b9686e8cSDan Williams down_write(&cxl_region_rwsem); 1320b9686e8cSDan Williams out: 1321b9686e8cSDan Williams put_device(&cxlr->dev); 1322176baefbSDan Williams return rc; 1323b9686e8cSDan Williams } 1324b9686e8cSDan Williams 1325b9686e8cSDan Williams void cxl_decoder_kill_region(struct cxl_endpoint_decoder *cxled) 1326b9686e8cSDan Williams { 1327b9686e8cSDan Williams down_write(&cxl_region_rwsem); 1328b9686e8cSDan Williams cxled->mode = CXL_DECODER_DEAD; 1329b9686e8cSDan Williams cxl_region_detach(cxled); 1330b9686e8cSDan Williams up_write(&cxl_region_rwsem); 1331b9686e8cSDan Williams } 1332b9686e8cSDan Williams 1333b9686e8cSDan Williams static int attach_target(struct cxl_region *cxlr, const char *decoder, int pos) 1334b9686e8cSDan Williams { 1335b9686e8cSDan Williams struct device *dev; 1336b9686e8cSDan Williams int rc; 1337b9686e8cSDan Williams 1338b9686e8cSDan Williams dev = bus_find_device_by_name(&cxl_bus_type, NULL, decoder); 1339b9686e8cSDan Williams if (!dev) 1340b9686e8cSDan Williams return -ENODEV; 1341b9686e8cSDan Williams 1342b9686e8cSDan Williams if (!is_endpoint_decoder(dev)) { 1343b9686e8cSDan Williams put_device(dev); 1344b9686e8cSDan Williams return -EINVAL; 1345b9686e8cSDan Williams } 1346b9686e8cSDan Williams 1347b9686e8cSDan Williams rc = down_write_killable(&cxl_region_rwsem); 1348b9686e8cSDan Williams if (rc) 1349b9686e8cSDan Williams goto out; 1350b9686e8cSDan Williams down_read(&cxl_dpa_rwsem); 1351b9686e8cSDan Williams rc = cxl_region_attach(cxlr, to_cxl_endpoint_decoder(dev), pos); 1352b9686e8cSDan Williams up_read(&cxl_dpa_rwsem); 1353b9686e8cSDan Williams up_write(&cxl_region_rwsem); 1354b9686e8cSDan Williams out: 1355b9686e8cSDan Williams put_device(dev); 1356b9686e8cSDan Williams return rc; 1357b9686e8cSDan Williams } 1358b9686e8cSDan Williams 1359b9686e8cSDan Williams static int detach_target(struct cxl_region *cxlr, int pos) 1360b9686e8cSDan Williams { 1361b9686e8cSDan Williams struct cxl_region_params *p = &cxlr->params; 1362b9686e8cSDan Williams int rc; 1363b9686e8cSDan Williams 1364b9686e8cSDan Williams rc = down_write_killable(&cxl_region_rwsem); 1365b9686e8cSDan Williams if (rc) 1366b9686e8cSDan Williams return rc; 1367b9686e8cSDan Williams 1368b9686e8cSDan Williams if (pos >= p->interleave_ways) { 1369b9686e8cSDan Williams dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos, 1370b9686e8cSDan Williams p->interleave_ways); 1371b9686e8cSDan Williams rc = -ENXIO; 1372b9686e8cSDan Williams goto out; 1373b9686e8cSDan Williams } 1374b9686e8cSDan Williams 1375b9686e8cSDan Williams if (!p->targets[pos]) { 1376b9686e8cSDan Williams rc = 0; 1377b9686e8cSDan Williams goto out; 1378b9686e8cSDan Williams } 1379b9686e8cSDan Williams 1380176baefbSDan Williams rc = cxl_region_detach(p->targets[pos]); 1381b9686e8cSDan Williams out: 1382b9686e8cSDan Williams up_write(&cxl_region_rwsem); 1383b9686e8cSDan Williams return rc; 1384b9686e8cSDan Williams } 1385b9686e8cSDan Williams 1386b9686e8cSDan Williams static size_t store_targetN(struct cxl_region *cxlr, const char *buf, int pos, 1387b9686e8cSDan Williams size_t len) 1388b9686e8cSDan Williams { 1389b9686e8cSDan Williams int rc; 1390b9686e8cSDan Williams 1391b9686e8cSDan Williams if (sysfs_streq(buf, "\n")) 1392b9686e8cSDan Williams rc = detach_target(cxlr, pos); 1393b9686e8cSDan Williams else 1394b9686e8cSDan Williams rc = attach_target(cxlr, buf, pos); 1395b9686e8cSDan Williams 1396b9686e8cSDan Williams if (rc < 0) 1397b9686e8cSDan Williams return rc; 1398b9686e8cSDan Williams return len; 1399b9686e8cSDan Williams } 1400b9686e8cSDan Williams 1401b9686e8cSDan Williams #define TARGET_ATTR_RW(n) \ 1402b9686e8cSDan Williams static ssize_t target##n##_show( \ 1403b9686e8cSDan Williams struct device *dev, struct device_attribute *attr, char *buf) \ 1404b9686e8cSDan Williams { \ 1405b9686e8cSDan Williams return show_targetN(to_cxl_region(dev), buf, (n)); \ 1406b9686e8cSDan Williams } \ 1407b9686e8cSDan Williams static ssize_t target##n##_store(struct device *dev, \ 1408b9686e8cSDan Williams struct device_attribute *attr, \ 1409b9686e8cSDan Williams const char *buf, size_t len) \ 1410b9686e8cSDan Williams { \ 1411b9686e8cSDan Williams return store_targetN(to_cxl_region(dev), buf, (n), len); \ 1412b9686e8cSDan Williams } \ 1413b9686e8cSDan Williams static DEVICE_ATTR_RW(target##n) 1414b9686e8cSDan Williams 1415b9686e8cSDan Williams TARGET_ATTR_RW(0); 1416b9686e8cSDan Williams TARGET_ATTR_RW(1); 1417b9686e8cSDan Williams TARGET_ATTR_RW(2); 1418b9686e8cSDan Williams TARGET_ATTR_RW(3); 1419b9686e8cSDan Williams TARGET_ATTR_RW(4); 1420b9686e8cSDan Williams TARGET_ATTR_RW(5); 1421b9686e8cSDan Williams TARGET_ATTR_RW(6); 1422b9686e8cSDan Williams TARGET_ATTR_RW(7); 1423b9686e8cSDan Williams TARGET_ATTR_RW(8); 1424b9686e8cSDan Williams TARGET_ATTR_RW(9); 1425b9686e8cSDan Williams TARGET_ATTR_RW(10); 1426b9686e8cSDan Williams TARGET_ATTR_RW(11); 1427b9686e8cSDan Williams TARGET_ATTR_RW(12); 1428b9686e8cSDan Williams TARGET_ATTR_RW(13); 1429b9686e8cSDan Williams TARGET_ATTR_RW(14); 1430b9686e8cSDan Williams TARGET_ATTR_RW(15); 1431b9686e8cSDan Williams 1432b9686e8cSDan Williams static struct attribute *target_attrs[] = { 1433b9686e8cSDan Williams &dev_attr_target0.attr, 1434b9686e8cSDan Williams &dev_attr_target1.attr, 1435b9686e8cSDan Williams &dev_attr_target2.attr, 1436b9686e8cSDan Williams &dev_attr_target3.attr, 1437b9686e8cSDan Williams &dev_attr_target4.attr, 1438b9686e8cSDan Williams &dev_attr_target5.attr, 1439b9686e8cSDan Williams &dev_attr_target6.attr, 1440b9686e8cSDan Williams &dev_attr_target7.attr, 1441b9686e8cSDan Williams &dev_attr_target8.attr, 1442b9686e8cSDan Williams &dev_attr_target9.attr, 1443b9686e8cSDan Williams &dev_attr_target10.attr, 1444b9686e8cSDan Williams &dev_attr_target11.attr, 1445b9686e8cSDan Williams &dev_attr_target12.attr, 1446b9686e8cSDan Williams &dev_attr_target13.attr, 1447b9686e8cSDan Williams &dev_attr_target14.attr, 1448b9686e8cSDan Williams &dev_attr_target15.attr, 1449b9686e8cSDan Williams NULL, 1450b9686e8cSDan Williams }; 1451b9686e8cSDan Williams 1452b9686e8cSDan Williams static umode_t cxl_region_target_visible(struct kobject *kobj, 1453b9686e8cSDan Williams struct attribute *a, int n) 1454b9686e8cSDan Williams { 1455b9686e8cSDan Williams struct device *dev = kobj_to_dev(kobj); 1456b9686e8cSDan Williams struct cxl_region *cxlr = to_cxl_region(dev); 1457b9686e8cSDan Williams struct cxl_region_params *p = &cxlr->params; 1458b9686e8cSDan Williams 1459b9686e8cSDan Williams if (n < p->interleave_ways) 1460b9686e8cSDan Williams return a->mode; 1461b9686e8cSDan Williams return 0; 1462b9686e8cSDan Williams } 1463b9686e8cSDan Williams 1464b9686e8cSDan Williams static const struct attribute_group cxl_region_target_group = { 1465b9686e8cSDan Williams .attrs = target_attrs, 1466b9686e8cSDan Williams .is_visible = cxl_region_target_visible, 1467b9686e8cSDan Williams }; 1468b9686e8cSDan Williams 1469b9686e8cSDan Williams static const struct attribute_group *get_cxl_region_target_group(void) 1470b9686e8cSDan Williams { 1471b9686e8cSDan Williams return &cxl_region_target_group; 1472b9686e8cSDan Williams } 1473b9686e8cSDan Williams 1474dd5ba0ebSBen Widawsky static const struct attribute_group *region_groups[] = { 1475dd5ba0ebSBen Widawsky &cxl_base_attribute_group, 1476dd5ba0ebSBen Widawsky &cxl_region_group, 1477b9686e8cSDan Williams &cxl_region_target_group, 1478dd5ba0ebSBen Widawsky NULL, 1479dd5ba0ebSBen Widawsky }; 1480dd5ba0ebSBen Widawsky 1481779dd20cSBen Widawsky static void cxl_region_release(struct device *dev) 1482779dd20cSBen Widawsky { 1483779dd20cSBen Widawsky struct cxl_region *cxlr = to_cxl_region(dev); 1484779dd20cSBen Widawsky 1485779dd20cSBen Widawsky memregion_free(cxlr->id); 1486779dd20cSBen Widawsky kfree(cxlr); 1487779dd20cSBen Widawsky } 1488779dd20cSBen Widawsky 14898d48817dSDan Williams const struct device_type cxl_region_type = { 1490779dd20cSBen Widawsky .name = "cxl_region", 1491779dd20cSBen Widawsky .release = cxl_region_release, 1492dd5ba0ebSBen Widawsky .groups = region_groups 1493779dd20cSBen Widawsky }; 1494779dd20cSBen Widawsky 1495779dd20cSBen Widawsky bool is_cxl_region(struct device *dev) 1496779dd20cSBen Widawsky { 1497779dd20cSBen Widawsky return dev->type == &cxl_region_type; 1498779dd20cSBen Widawsky } 1499779dd20cSBen Widawsky EXPORT_SYMBOL_NS_GPL(is_cxl_region, CXL); 1500779dd20cSBen Widawsky 1501779dd20cSBen Widawsky static struct cxl_region *to_cxl_region(struct device *dev) 1502779dd20cSBen Widawsky { 1503779dd20cSBen Widawsky if (dev_WARN_ONCE(dev, dev->type != &cxl_region_type, 1504779dd20cSBen Widawsky "not a cxl_region device\n")) 1505779dd20cSBen Widawsky return NULL; 1506779dd20cSBen Widawsky 1507779dd20cSBen Widawsky return container_of(dev, struct cxl_region, dev); 1508779dd20cSBen Widawsky } 1509779dd20cSBen Widawsky 1510779dd20cSBen Widawsky static void unregister_region(void *dev) 1511779dd20cSBen Widawsky { 151223a22cd1SDan Williams struct cxl_region *cxlr = to_cxl_region(dev); 151323a22cd1SDan Williams 151423a22cd1SDan Williams device_del(dev); 151523a22cd1SDan Williams cxl_region_iomem_release(cxlr); 151623a22cd1SDan Williams put_device(dev); 1517779dd20cSBen Widawsky } 1518779dd20cSBen Widawsky 1519779dd20cSBen Widawsky static struct lock_class_key cxl_region_key; 1520779dd20cSBen Widawsky 1521779dd20cSBen Widawsky static struct cxl_region *cxl_region_alloc(struct cxl_root_decoder *cxlrd, int id) 1522779dd20cSBen Widawsky { 1523779dd20cSBen Widawsky struct cxl_region *cxlr; 1524779dd20cSBen Widawsky struct device *dev; 1525779dd20cSBen Widawsky 1526779dd20cSBen Widawsky cxlr = kzalloc(sizeof(*cxlr), GFP_KERNEL); 1527779dd20cSBen Widawsky if (!cxlr) { 1528779dd20cSBen Widawsky memregion_free(id); 1529779dd20cSBen Widawsky return ERR_PTR(-ENOMEM); 1530779dd20cSBen Widawsky } 1531779dd20cSBen Widawsky 1532779dd20cSBen Widawsky dev = &cxlr->dev; 1533779dd20cSBen Widawsky device_initialize(dev); 1534779dd20cSBen Widawsky lockdep_set_class(&dev->mutex, &cxl_region_key); 1535779dd20cSBen Widawsky dev->parent = &cxlrd->cxlsd.cxld.dev; 1536779dd20cSBen Widawsky device_set_pm_not_required(dev); 1537779dd20cSBen Widawsky dev->bus = &cxl_bus_type; 1538779dd20cSBen Widawsky dev->type = &cxl_region_type; 1539779dd20cSBen Widawsky cxlr->id = id; 1540779dd20cSBen Widawsky 1541779dd20cSBen Widawsky return cxlr; 1542779dd20cSBen Widawsky } 1543779dd20cSBen Widawsky 1544779dd20cSBen Widawsky /** 1545779dd20cSBen Widawsky * devm_cxl_add_region - Adds a region to a decoder 1546779dd20cSBen Widawsky * @cxlrd: root decoder 1547779dd20cSBen Widawsky * @id: memregion id to create, or memregion_free() on failure 1548779dd20cSBen Widawsky * @mode: mode for the endpoint decoders of this region 1549779dd20cSBen Widawsky * @type: select whether this is an expander or accelerator (type-2 or type-3) 1550779dd20cSBen Widawsky * 1551779dd20cSBen Widawsky * This is the second step of region initialization. Regions exist within an 1552779dd20cSBen Widawsky * address space which is mapped by a @cxlrd. 1553779dd20cSBen Widawsky * 1554779dd20cSBen Widawsky * Return: 0 if the region was added to the @cxlrd, else returns negative error 1555779dd20cSBen Widawsky * code. The region will be named "regionZ" where Z is the unique region number. 1556779dd20cSBen Widawsky */ 1557779dd20cSBen Widawsky static struct cxl_region *devm_cxl_add_region(struct cxl_root_decoder *cxlrd, 1558779dd20cSBen Widawsky int id, 1559779dd20cSBen Widawsky enum cxl_decoder_mode mode, 1560779dd20cSBen Widawsky enum cxl_decoder_type type) 1561779dd20cSBen Widawsky { 1562779dd20cSBen Widawsky struct cxl_port *port = to_cxl_port(cxlrd->cxlsd.cxld.dev.parent); 1563779dd20cSBen Widawsky struct cxl_region *cxlr; 1564779dd20cSBen Widawsky struct device *dev; 1565779dd20cSBen Widawsky int rc; 1566779dd20cSBen Widawsky 1567779dd20cSBen Widawsky cxlr = cxl_region_alloc(cxlrd, id); 1568779dd20cSBen Widawsky if (IS_ERR(cxlr)) 1569779dd20cSBen Widawsky return cxlr; 1570779dd20cSBen Widawsky cxlr->mode = mode; 1571779dd20cSBen Widawsky cxlr->type = type; 1572779dd20cSBen Widawsky 1573779dd20cSBen Widawsky dev = &cxlr->dev; 1574779dd20cSBen Widawsky rc = dev_set_name(dev, "region%d", id); 1575779dd20cSBen Widawsky if (rc) 1576779dd20cSBen Widawsky goto err; 1577779dd20cSBen Widawsky 1578779dd20cSBen Widawsky rc = device_add(dev); 1579779dd20cSBen Widawsky if (rc) 1580779dd20cSBen Widawsky goto err; 1581779dd20cSBen Widawsky 1582779dd20cSBen Widawsky rc = devm_add_action_or_reset(port->uport, unregister_region, cxlr); 1583779dd20cSBen Widawsky if (rc) 1584779dd20cSBen Widawsky return ERR_PTR(rc); 1585779dd20cSBen Widawsky 1586779dd20cSBen Widawsky dev_dbg(port->uport, "%s: created %s\n", 1587779dd20cSBen Widawsky dev_name(&cxlrd->cxlsd.cxld.dev), dev_name(dev)); 1588779dd20cSBen Widawsky return cxlr; 1589779dd20cSBen Widawsky 1590779dd20cSBen Widawsky err: 1591779dd20cSBen Widawsky put_device(dev); 1592779dd20cSBen Widawsky return ERR_PTR(rc); 1593779dd20cSBen Widawsky } 1594779dd20cSBen Widawsky 1595779dd20cSBen Widawsky static ssize_t create_pmem_region_show(struct device *dev, 1596779dd20cSBen Widawsky struct device_attribute *attr, char *buf) 1597779dd20cSBen Widawsky { 1598779dd20cSBen Widawsky struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev); 1599779dd20cSBen Widawsky 1600779dd20cSBen Widawsky return sysfs_emit(buf, "region%u\n", atomic_read(&cxlrd->region_id)); 1601779dd20cSBen Widawsky } 1602779dd20cSBen Widawsky 1603779dd20cSBen Widawsky static ssize_t create_pmem_region_store(struct device *dev, 1604779dd20cSBen Widawsky struct device_attribute *attr, 1605779dd20cSBen Widawsky const char *buf, size_t len) 1606779dd20cSBen Widawsky { 1607779dd20cSBen Widawsky struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev); 1608779dd20cSBen Widawsky struct cxl_region *cxlr; 1609779dd20cSBen Widawsky int id, rc; 1610779dd20cSBen Widawsky 1611779dd20cSBen Widawsky rc = sscanf(buf, "region%d\n", &id); 1612779dd20cSBen Widawsky if (rc != 1) 1613779dd20cSBen Widawsky return -EINVAL; 1614779dd20cSBen Widawsky 1615779dd20cSBen Widawsky rc = memregion_alloc(GFP_KERNEL); 1616779dd20cSBen Widawsky if (rc < 0) 1617779dd20cSBen Widawsky return rc; 1618779dd20cSBen Widawsky 1619779dd20cSBen Widawsky if (atomic_cmpxchg(&cxlrd->region_id, id, rc) != id) { 1620779dd20cSBen Widawsky memregion_free(rc); 1621779dd20cSBen Widawsky return -EBUSY; 1622779dd20cSBen Widawsky } 1623779dd20cSBen Widawsky 1624779dd20cSBen Widawsky cxlr = devm_cxl_add_region(cxlrd, id, CXL_DECODER_PMEM, 1625779dd20cSBen Widawsky CXL_DECODER_EXPANDER); 1626779dd20cSBen Widawsky if (IS_ERR(cxlr)) 1627779dd20cSBen Widawsky return PTR_ERR(cxlr); 1628779dd20cSBen Widawsky 1629779dd20cSBen Widawsky return len; 1630779dd20cSBen Widawsky } 1631779dd20cSBen Widawsky DEVICE_ATTR_RW(create_pmem_region); 1632779dd20cSBen Widawsky 1633b9686e8cSDan Williams static ssize_t region_show(struct device *dev, struct device_attribute *attr, 1634b9686e8cSDan Williams char *buf) 1635b9686e8cSDan Williams { 1636b9686e8cSDan Williams struct cxl_decoder *cxld = to_cxl_decoder(dev); 1637b9686e8cSDan Williams ssize_t rc; 1638b9686e8cSDan Williams 1639b9686e8cSDan Williams rc = down_read_interruptible(&cxl_region_rwsem); 1640b9686e8cSDan Williams if (rc) 1641b9686e8cSDan Williams return rc; 1642b9686e8cSDan Williams 1643b9686e8cSDan Williams if (cxld->region) 1644b9686e8cSDan Williams rc = sysfs_emit(buf, "%s\n", dev_name(&cxld->region->dev)); 1645b9686e8cSDan Williams else 1646b9686e8cSDan Williams rc = sysfs_emit(buf, "\n"); 1647b9686e8cSDan Williams up_read(&cxl_region_rwsem); 1648b9686e8cSDan Williams 1649b9686e8cSDan Williams return rc; 1650b9686e8cSDan Williams } 1651b9686e8cSDan Williams DEVICE_ATTR_RO(region); 1652b9686e8cSDan Williams 1653779dd20cSBen Widawsky static struct cxl_region * 1654779dd20cSBen Widawsky cxl_find_region_by_name(struct cxl_root_decoder *cxlrd, const char *name) 1655779dd20cSBen Widawsky { 1656779dd20cSBen Widawsky struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld; 1657779dd20cSBen Widawsky struct device *region_dev; 1658779dd20cSBen Widawsky 1659779dd20cSBen Widawsky region_dev = device_find_child_by_name(&cxld->dev, name); 1660779dd20cSBen Widawsky if (!region_dev) 1661779dd20cSBen Widawsky return ERR_PTR(-ENODEV); 1662779dd20cSBen Widawsky 1663779dd20cSBen Widawsky return to_cxl_region(region_dev); 1664779dd20cSBen Widawsky } 1665779dd20cSBen Widawsky 1666779dd20cSBen Widawsky static ssize_t delete_region_store(struct device *dev, 1667779dd20cSBen Widawsky struct device_attribute *attr, 1668779dd20cSBen Widawsky const char *buf, size_t len) 1669779dd20cSBen Widawsky { 1670779dd20cSBen Widawsky struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev); 1671779dd20cSBen Widawsky struct cxl_port *port = to_cxl_port(dev->parent); 1672779dd20cSBen Widawsky struct cxl_region *cxlr; 1673779dd20cSBen Widawsky 1674779dd20cSBen Widawsky cxlr = cxl_find_region_by_name(cxlrd, buf); 1675779dd20cSBen Widawsky if (IS_ERR(cxlr)) 1676779dd20cSBen Widawsky return PTR_ERR(cxlr); 1677779dd20cSBen Widawsky 1678779dd20cSBen Widawsky devm_release_action(port->uport, unregister_region, cxlr); 1679779dd20cSBen Widawsky put_device(&cxlr->dev); 1680779dd20cSBen Widawsky 1681779dd20cSBen Widawsky return len; 1682779dd20cSBen Widawsky } 1683779dd20cSBen Widawsky DEVICE_ATTR_WO(delete_region); 168423a22cd1SDan Williams 168504ad63f0SDan Williams static void cxl_pmem_region_release(struct device *dev) 168604ad63f0SDan Williams { 168704ad63f0SDan Williams struct cxl_pmem_region *cxlr_pmem = to_cxl_pmem_region(dev); 168804ad63f0SDan Williams int i; 168904ad63f0SDan Williams 169004ad63f0SDan Williams for (i = 0; i < cxlr_pmem->nr_mappings; i++) { 169104ad63f0SDan Williams struct cxl_memdev *cxlmd = cxlr_pmem->mapping[i].cxlmd; 169204ad63f0SDan Williams 169304ad63f0SDan Williams put_device(&cxlmd->dev); 169404ad63f0SDan Williams } 169504ad63f0SDan Williams 169604ad63f0SDan Williams kfree(cxlr_pmem); 169704ad63f0SDan Williams } 169804ad63f0SDan Williams 169904ad63f0SDan Williams static const struct attribute_group *cxl_pmem_region_attribute_groups[] = { 170004ad63f0SDan Williams &cxl_base_attribute_group, 170104ad63f0SDan Williams NULL, 170204ad63f0SDan Williams }; 170304ad63f0SDan Williams 170404ad63f0SDan Williams const struct device_type cxl_pmem_region_type = { 170504ad63f0SDan Williams .name = "cxl_pmem_region", 170604ad63f0SDan Williams .release = cxl_pmem_region_release, 170704ad63f0SDan Williams .groups = cxl_pmem_region_attribute_groups, 170804ad63f0SDan Williams }; 170904ad63f0SDan Williams 171004ad63f0SDan Williams bool is_cxl_pmem_region(struct device *dev) 171104ad63f0SDan Williams { 171204ad63f0SDan Williams return dev->type == &cxl_pmem_region_type; 171304ad63f0SDan Williams } 171404ad63f0SDan Williams EXPORT_SYMBOL_NS_GPL(is_cxl_pmem_region, CXL); 171504ad63f0SDan Williams 171604ad63f0SDan Williams struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev) 171704ad63f0SDan Williams { 171804ad63f0SDan Williams if (dev_WARN_ONCE(dev, !is_cxl_pmem_region(dev), 171904ad63f0SDan Williams "not a cxl_pmem_region device\n")) 172004ad63f0SDan Williams return NULL; 172104ad63f0SDan Williams return container_of(dev, struct cxl_pmem_region, dev); 172204ad63f0SDan Williams } 172304ad63f0SDan Williams EXPORT_SYMBOL_NS_GPL(to_cxl_pmem_region, CXL); 172404ad63f0SDan Williams 172504ad63f0SDan Williams static struct lock_class_key cxl_pmem_region_key; 172604ad63f0SDan Williams 172704ad63f0SDan Williams static struct cxl_pmem_region *cxl_pmem_region_alloc(struct cxl_region *cxlr) 172804ad63f0SDan Williams { 172904ad63f0SDan Williams struct cxl_region_params *p = &cxlr->params; 173004ad63f0SDan Williams struct cxl_pmem_region *cxlr_pmem; 173104ad63f0SDan Williams struct device *dev; 173204ad63f0SDan Williams int i; 173304ad63f0SDan Williams 173404ad63f0SDan Williams down_read(&cxl_region_rwsem); 173504ad63f0SDan Williams if (p->state != CXL_CONFIG_COMMIT) { 173604ad63f0SDan Williams cxlr_pmem = ERR_PTR(-ENXIO); 173704ad63f0SDan Williams goto out; 173804ad63f0SDan Williams } 173904ad63f0SDan Williams 174004ad63f0SDan Williams cxlr_pmem = kzalloc(struct_size(cxlr_pmem, mapping, p->nr_targets), 174104ad63f0SDan Williams GFP_KERNEL); 174204ad63f0SDan Williams if (!cxlr_pmem) { 174304ad63f0SDan Williams cxlr_pmem = ERR_PTR(-ENOMEM); 174404ad63f0SDan Williams goto out; 174504ad63f0SDan Williams } 174604ad63f0SDan Williams 174704ad63f0SDan Williams cxlr_pmem->hpa_range.start = p->res->start; 174804ad63f0SDan Williams cxlr_pmem->hpa_range.end = p->res->end; 174904ad63f0SDan Williams 175004ad63f0SDan Williams /* Snapshot the region configuration underneath the cxl_region_rwsem */ 175104ad63f0SDan Williams cxlr_pmem->nr_mappings = p->nr_targets; 175204ad63f0SDan Williams for (i = 0; i < p->nr_targets; i++) { 175304ad63f0SDan Williams struct cxl_endpoint_decoder *cxled = p->targets[i]; 175404ad63f0SDan Williams struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 175504ad63f0SDan Williams struct cxl_pmem_region_mapping *m = &cxlr_pmem->mapping[i]; 175604ad63f0SDan Williams 175704ad63f0SDan Williams m->cxlmd = cxlmd; 175804ad63f0SDan Williams get_device(&cxlmd->dev); 175904ad63f0SDan Williams m->start = cxled->dpa_res->start; 176004ad63f0SDan Williams m->size = resource_size(cxled->dpa_res); 176104ad63f0SDan Williams m->position = i; 176204ad63f0SDan Williams } 176304ad63f0SDan Williams 176404ad63f0SDan Williams dev = &cxlr_pmem->dev; 176504ad63f0SDan Williams cxlr_pmem->cxlr = cxlr; 176604ad63f0SDan Williams device_initialize(dev); 176704ad63f0SDan Williams lockdep_set_class(&dev->mutex, &cxl_pmem_region_key); 176804ad63f0SDan Williams device_set_pm_not_required(dev); 176904ad63f0SDan Williams dev->parent = &cxlr->dev; 177004ad63f0SDan Williams dev->bus = &cxl_bus_type; 177104ad63f0SDan Williams dev->type = &cxl_pmem_region_type; 177204ad63f0SDan Williams out: 177304ad63f0SDan Williams up_read(&cxl_region_rwsem); 177404ad63f0SDan Williams 177504ad63f0SDan Williams return cxlr_pmem; 177604ad63f0SDan Williams } 177704ad63f0SDan Williams 177804ad63f0SDan Williams static void cxlr_pmem_unregister(void *dev) 177904ad63f0SDan Williams { 178004ad63f0SDan Williams device_unregister(dev); 178104ad63f0SDan Williams } 178204ad63f0SDan Williams 178304ad63f0SDan Williams /** 178404ad63f0SDan Williams * devm_cxl_add_pmem_region() - add a cxl_region-to-nd_region bridge 178504ad63f0SDan Williams * @cxlr: parent CXL region for this pmem region bridge device 178604ad63f0SDan Williams * 178704ad63f0SDan Williams * Return: 0 on success negative error code on failure. 178804ad63f0SDan Williams */ 178904ad63f0SDan Williams static int devm_cxl_add_pmem_region(struct cxl_region *cxlr) 179004ad63f0SDan Williams { 179104ad63f0SDan Williams struct cxl_pmem_region *cxlr_pmem; 179204ad63f0SDan Williams struct device *dev; 179304ad63f0SDan Williams int rc; 179404ad63f0SDan Williams 179504ad63f0SDan Williams cxlr_pmem = cxl_pmem_region_alloc(cxlr); 179604ad63f0SDan Williams if (IS_ERR(cxlr_pmem)) 179704ad63f0SDan Williams return PTR_ERR(cxlr_pmem); 179804ad63f0SDan Williams 179904ad63f0SDan Williams dev = &cxlr_pmem->dev; 180004ad63f0SDan Williams rc = dev_set_name(dev, "pmem_region%d", cxlr->id); 180104ad63f0SDan Williams if (rc) 180204ad63f0SDan Williams goto err; 180304ad63f0SDan Williams 180404ad63f0SDan Williams rc = device_add(dev); 180504ad63f0SDan Williams if (rc) 180604ad63f0SDan Williams goto err; 180704ad63f0SDan Williams 180804ad63f0SDan Williams dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent), 180904ad63f0SDan Williams dev_name(dev)); 181004ad63f0SDan Williams 181104ad63f0SDan Williams return devm_add_action_or_reset(&cxlr->dev, cxlr_pmem_unregister, dev); 181204ad63f0SDan Williams 181304ad63f0SDan Williams err: 181404ad63f0SDan Williams put_device(dev); 181504ad63f0SDan Williams return rc; 181604ad63f0SDan Williams } 181704ad63f0SDan Williams 18188d48817dSDan Williams static int cxl_region_probe(struct device *dev) 18198d48817dSDan Williams { 18208d48817dSDan Williams struct cxl_region *cxlr = to_cxl_region(dev); 18218d48817dSDan Williams struct cxl_region_params *p = &cxlr->params; 18228d48817dSDan Williams int rc; 18238d48817dSDan Williams 18248d48817dSDan Williams rc = down_read_interruptible(&cxl_region_rwsem); 18258d48817dSDan Williams if (rc) { 18268d48817dSDan Williams dev_dbg(&cxlr->dev, "probe interrupted\n"); 18278d48817dSDan Williams return rc; 18288d48817dSDan Williams } 18298d48817dSDan Williams 18308d48817dSDan Williams if (p->state < CXL_CONFIG_COMMIT) { 18318d48817dSDan Williams dev_dbg(&cxlr->dev, "config state: %d\n", p->state); 18328d48817dSDan Williams rc = -ENXIO; 18338d48817dSDan Williams } 18348d48817dSDan Williams 18358d48817dSDan Williams /* 18368d48817dSDan Williams * From this point on any path that changes the region's state away from 18378d48817dSDan Williams * CXL_CONFIG_COMMIT is also responsible for releasing the driver. 18388d48817dSDan Williams */ 18398d48817dSDan Williams up_read(&cxl_region_rwsem); 18408d48817dSDan Williams 184104ad63f0SDan Williams switch (cxlr->mode) { 184204ad63f0SDan Williams case CXL_DECODER_PMEM: 184304ad63f0SDan Williams return devm_cxl_add_pmem_region(cxlr); 184404ad63f0SDan Williams default: 184504ad63f0SDan Williams dev_dbg(&cxlr->dev, "unsupported region mode: %d\n", 184604ad63f0SDan Williams cxlr->mode); 184704ad63f0SDan Williams return -ENXIO; 184804ad63f0SDan Williams } 18498d48817dSDan Williams } 18508d48817dSDan Williams 18518d48817dSDan Williams static struct cxl_driver cxl_region_driver = { 18528d48817dSDan Williams .name = "cxl_region", 18538d48817dSDan Williams .probe = cxl_region_probe, 18548d48817dSDan Williams .id = CXL_DEVICE_REGION, 18558d48817dSDan Williams }; 18568d48817dSDan Williams 18578d48817dSDan Williams int cxl_region_init(void) 18588d48817dSDan Williams { 18598d48817dSDan Williams return cxl_driver_register(&cxl_region_driver); 18608d48817dSDan Williams } 18618d48817dSDan Williams 18628d48817dSDan Williams void cxl_region_exit(void) 18638d48817dSDan Williams { 18648d48817dSDan Williams cxl_driver_unregister(&cxl_region_driver); 18658d48817dSDan Williams } 18668d48817dSDan Williams 186723a22cd1SDan Williams MODULE_IMPORT_NS(CXL); 18688d48817dSDan Williams MODULE_ALIAS_CXL(CXL_DEVICE_REGION); 1869