1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright(c) 2022 Intel Corporation. All rights reserved. */ 3 #include <linux/memregion.h> 4 #include <linux/genalloc.h> 5 #include <linux/debugfs.h> 6 #include <linux/device.h> 7 #include <linux/module.h> 8 #include <linux/memory.h> 9 #include <linux/slab.h> 10 #include <linux/uuid.h> 11 #include <linux/sort.h> 12 #include <linux/idr.h> 13 #include <linux/memory-tiers.h> 14 #include <cxlmem.h> 15 #include <cxl.h> 16 #include "core.h" 17 18 /** 19 * DOC: cxl core region 20 * 21 * CXL Regions represent mapped memory capacity in system physical address 22 * space. Whereas the CXL Root Decoders identify the bounds of potential CXL 23 * Memory ranges, Regions represent the active mapped capacity by the HDM 24 * Decoder Capability structures throughout the Host Bridges, Switches, and 25 * Endpoints in the topology. 26 * 27 * Region configuration has ordering constraints. UUID may be set at any time 28 * but is only visible for persistent regions. 29 * 1. Interleave granularity 30 * 2. Interleave size 31 * 3. Decoder targets 32 */ 33 34 static struct cxl_region *to_cxl_region(struct device *dev); 35 36 #define __ACCESS_ATTR_RO(_level, _name) { \ 37 .attr = { .name = __stringify(_name), .mode = 0444 }, \ 38 .show = _name##_access##_level##_show, \ 39 } 40 41 #define ACCESS_DEVICE_ATTR_RO(level, name) \ 42 struct device_attribute dev_attr_access##level##_##name = __ACCESS_ATTR_RO(level, name) 43 44 #define ACCESS_ATTR_RO(level, attrib) \ 45 static ssize_t attrib##_access##level##_show(struct device *dev, \ 46 struct device_attribute *attr, \ 47 char *buf) \ 48 { \ 49 struct cxl_region *cxlr = to_cxl_region(dev); \ 50 \ 51 if (cxlr->coord[level].attrib == 0) \ 52 return -ENOENT; \ 53 \ 54 return sysfs_emit(buf, "%u\n", cxlr->coord[level].attrib); \ 55 } \ 56 static ACCESS_DEVICE_ATTR_RO(level, attrib) 57 58 ACCESS_ATTR_RO(0, read_bandwidth); 59 ACCESS_ATTR_RO(0, read_latency); 60 ACCESS_ATTR_RO(0, write_bandwidth); 61 ACCESS_ATTR_RO(0, write_latency); 62 63 #define ACCESS_ATTR_DECLARE(level, attrib) \ 64 (&dev_attr_access##level##_##attrib.attr) 65 66 static struct attribute *access0_coordinate_attrs[] = { 67 ACCESS_ATTR_DECLARE(0, read_bandwidth), 68 ACCESS_ATTR_DECLARE(0, write_bandwidth), 69 ACCESS_ATTR_DECLARE(0, read_latency), 70 ACCESS_ATTR_DECLARE(0, write_latency), 71 NULL 72 }; 73 74 ACCESS_ATTR_RO(1, read_bandwidth); 75 ACCESS_ATTR_RO(1, read_latency); 76 ACCESS_ATTR_RO(1, write_bandwidth); 77 ACCESS_ATTR_RO(1, write_latency); 78 79 static struct attribute *access1_coordinate_attrs[] = { 80 ACCESS_ATTR_DECLARE(1, read_bandwidth), 81 ACCESS_ATTR_DECLARE(1, write_bandwidth), 82 ACCESS_ATTR_DECLARE(1, read_latency), 83 ACCESS_ATTR_DECLARE(1, write_latency), 84 NULL 85 }; 86 87 #define ACCESS_VISIBLE(level) \ 88 static umode_t cxl_region_access##level##_coordinate_visible( \ 89 struct kobject *kobj, struct attribute *a, int n) \ 90 { \ 91 struct device *dev = kobj_to_dev(kobj); \ 92 struct cxl_region *cxlr = to_cxl_region(dev); \ 93 \ 94 if (a == &dev_attr_access##level##_read_latency.attr && \ 95 cxlr->coord[level].read_latency == 0) \ 96 return 0; \ 97 \ 98 if (a == &dev_attr_access##level##_write_latency.attr && \ 99 cxlr->coord[level].write_latency == 0) \ 100 return 0; \ 101 \ 102 if (a == &dev_attr_access##level##_read_bandwidth.attr && \ 103 cxlr->coord[level].read_bandwidth == 0) \ 104 return 0; \ 105 \ 106 if (a == &dev_attr_access##level##_write_bandwidth.attr && \ 107 cxlr->coord[level].write_bandwidth == 0) \ 108 return 0; \ 109 \ 110 return a->mode; \ 111 } 112 113 ACCESS_VISIBLE(0); 114 ACCESS_VISIBLE(1); 115 116 static const struct attribute_group cxl_region_access0_coordinate_group = { 117 .name = "access0", 118 .attrs = access0_coordinate_attrs, 119 .is_visible = cxl_region_access0_coordinate_visible, 120 }; 121 122 static const struct attribute_group *get_cxl_region_access0_group(void) 123 { 124 return &cxl_region_access0_coordinate_group; 125 } 126 127 static const struct attribute_group cxl_region_access1_coordinate_group = { 128 .name = "access1", 129 .attrs = access1_coordinate_attrs, 130 .is_visible = cxl_region_access1_coordinate_visible, 131 }; 132 133 static const struct attribute_group *get_cxl_region_access1_group(void) 134 { 135 return &cxl_region_access1_coordinate_group; 136 } 137 138 static ssize_t uuid_show(struct device *dev, struct device_attribute *attr, 139 char *buf) 140 { 141 struct cxl_region *cxlr = to_cxl_region(dev); 142 struct cxl_region_params *p = &cxlr->params; 143 ssize_t rc; 144 145 ACQUIRE(rwsem_read_intr, region_rwsem)(&cxl_rwsem.region); 146 if ((rc = ACQUIRE_ERR(rwsem_read_intr, ®ion_rwsem))) 147 return rc; 148 if (cxlr->mode != CXL_PARTMODE_PMEM) 149 return sysfs_emit(buf, "\n"); 150 return sysfs_emit(buf, "%pUb\n", &p->uuid); 151 } 152 153 static int is_dup(struct device *match, void *data) 154 { 155 struct cxl_region_params *p; 156 struct cxl_region *cxlr; 157 uuid_t *uuid = data; 158 159 if (!is_cxl_region(match)) 160 return 0; 161 162 lockdep_assert_held(&cxl_rwsem.region); 163 cxlr = to_cxl_region(match); 164 p = &cxlr->params; 165 166 if (uuid_equal(&p->uuid, uuid)) { 167 dev_dbg(match, "already has uuid: %pUb\n", uuid); 168 return -EBUSY; 169 } 170 171 return 0; 172 } 173 174 static ssize_t uuid_store(struct device *dev, struct device_attribute *attr, 175 const char *buf, size_t len) 176 { 177 struct cxl_region *cxlr = to_cxl_region(dev); 178 struct cxl_region_params *p = &cxlr->params; 179 uuid_t temp; 180 ssize_t rc; 181 182 if (len != UUID_STRING_LEN + 1) 183 return -EINVAL; 184 185 rc = uuid_parse(buf, &temp); 186 if (rc) 187 return rc; 188 189 if (uuid_is_null(&temp)) 190 return -EINVAL; 191 192 ACQUIRE(rwsem_write_kill, region_rwsem)(&cxl_rwsem.region); 193 if ((rc = ACQUIRE_ERR(rwsem_write_kill, ®ion_rwsem))) 194 return rc; 195 196 if (uuid_equal(&p->uuid, &temp)) 197 return len; 198 199 if (p->state >= CXL_CONFIG_ACTIVE) 200 return -EBUSY; 201 202 rc = bus_for_each_dev(&cxl_bus_type, NULL, &temp, is_dup); 203 if (rc < 0) 204 return rc; 205 206 uuid_copy(&p->uuid, &temp); 207 208 return len; 209 } 210 static DEVICE_ATTR_RW(uuid); 211 212 static struct cxl_region_ref *cxl_rr_load(struct cxl_port *port, 213 struct cxl_region *cxlr) 214 { 215 return xa_load(&port->regions, (unsigned long)cxlr); 216 } 217 218 static int cxl_region_invalidate_memregion(struct cxl_region *cxlr) 219 { 220 if (!cpu_cache_has_invalidate_memregion()) { 221 if (IS_ENABLED(CONFIG_CXL_REGION_INVALIDATION_TEST)) { 222 dev_info_once( 223 &cxlr->dev, 224 "Bypassing cpu_cache_invalidate_memregion() for testing!\n"); 225 return 0; 226 } 227 dev_WARN(&cxlr->dev, 228 "Failed to synchronize CPU cache state\n"); 229 return -ENXIO; 230 } 231 232 cpu_cache_invalidate_memregion(IORES_DESC_CXL); 233 return 0; 234 } 235 236 static void cxl_region_decode_reset(struct cxl_region *cxlr, int count) 237 { 238 struct cxl_region_params *p = &cxlr->params; 239 int i; 240 241 /* 242 * Before region teardown attempt to flush, evict any data cached for 243 * this region, or scream loudly about missing arch / platform support 244 * for CXL teardown. 245 */ 246 cxl_region_invalidate_memregion(cxlr); 247 248 for (i = count - 1; i >= 0; i--) { 249 struct cxl_endpoint_decoder *cxled = p->targets[i]; 250 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 251 struct cxl_port *iter = cxled_to_port(cxled); 252 struct cxl_dev_state *cxlds = cxlmd->cxlds; 253 struct cxl_ep *ep; 254 255 if (cxlds->rcd) 256 goto endpoint_reset; 257 258 while (!is_cxl_root(to_cxl_port(iter->dev.parent))) 259 iter = to_cxl_port(iter->dev.parent); 260 261 for (ep = cxl_ep_load(iter, cxlmd); iter; 262 iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) { 263 struct cxl_region_ref *cxl_rr; 264 struct cxl_decoder *cxld; 265 266 cxl_rr = cxl_rr_load(iter, cxlr); 267 cxld = cxl_rr->decoder; 268 if (cxld->reset) 269 cxld->reset(cxld); 270 set_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags); 271 } 272 273 endpoint_reset: 274 cxled->cxld.reset(&cxled->cxld); 275 set_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags); 276 } 277 278 /* all decoders associated with this region have been torn down */ 279 clear_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags); 280 } 281 282 static int commit_decoder(struct cxl_decoder *cxld) 283 { 284 struct cxl_switch_decoder *cxlsd = NULL; 285 286 if (cxld->commit) 287 return cxld->commit(cxld); 288 289 if (is_switch_decoder(&cxld->dev)) 290 cxlsd = to_cxl_switch_decoder(&cxld->dev); 291 292 if (dev_WARN_ONCE(&cxld->dev, !cxlsd || cxlsd->nr_targets > 1, 293 "->commit() is required\n")) 294 return -ENXIO; 295 return 0; 296 } 297 298 static int cxl_region_decode_commit(struct cxl_region *cxlr) 299 { 300 struct cxl_region_params *p = &cxlr->params; 301 int i, rc = 0; 302 303 for (i = 0; i < p->nr_targets; i++) { 304 struct cxl_endpoint_decoder *cxled = p->targets[i]; 305 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 306 struct cxl_region_ref *cxl_rr; 307 struct cxl_decoder *cxld; 308 struct cxl_port *iter; 309 struct cxl_ep *ep; 310 311 /* commit bottom up */ 312 for (iter = cxled_to_port(cxled); !is_cxl_root(iter); 313 iter = to_cxl_port(iter->dev.parent)) { 314 cxl_rr = cxl_rr_load(iter, cxlr); 315 cxld = cxl_rr->decoder; 316 rc = commit_decoder(cxld); 317 if (rc) 318 break; 319 } 320 321 if (rc) { 322 /* programming @iter failed, teardown */ 323 for (ep = cxl_ep_load(iter, cxlmd); ep && iter; 324 iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) { 325 cxl_rr = cxl_rr_load(iter, cxlr); 326 cxld = cxl_rr->decoder; 327 if (cxld->reset) 328 cxld->reset(cxld); 329 } 330 331 cxled->cxld.reset(&cxled->cxld); 332 goto err; 333 } 334 } 335 336 return 0; 337 338 err: 339 /* undo the targets that were successfully committed */ 340 cxl_region_decode_reset(cxlr, i); 341 return rc; 342 } 343 344 static int queue_reset(struct cxl_region *cxlr) 345 { 346 struct cxl_region_params *p = &cxlr->params; 347 int rc; 348 349 ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region); 350 if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem))) 351 return rc; 352 353 /* Already in the requested state? */ 354 if (p->state < CXL_CONFIG_COMMIT) 355 return 0; 356 357 p->state = CXL_CONFIG_RESET_PENDING; 358 359 return 0; 360 } 361 362 static int __commit(struct cxl_region *cxlr) 363 { 364 struct cxl_region_params *p = &cxlr->params; 365 int rc; 366 367 ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region); 368 if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem))) 369 return rc; 370 371 /* Already in the requested state? */ 372 if (p->state >= CXL_CONFIG_COMMIT) 373 return 0; 374 375 /* Not ready to commit? */ 376 if (p->state < CXL_CONFIG_ACTIVE) 377 return -ENXIO; 378 379 /* 380 * Invalidate caches before region setup to drop any speculative 381 * consumption of this address space 382 */ 383 rc = cxl_region_invalidate_memregion(cxlr); 384 if (rc) 385 return rc; 386 387 rc = cxl_region_decode_commit(cxlr); 388 if (rc) 389 return rc; 390 391 p->state = CXL_CONFIG_COMMIT; 392 393 return 0; 394 } 395 396 static ssize_t commit_store(struct device *dev, struct device_attribute *attr, 397 const char *buf, size_t len) 398 { 399 struct cxl_region *cxlr = to_cxl_region(dev); 400 struct cxl_region_params *p = &cxlr->params; 401 bool commit; 402 ssize_t rc; 403 404 rc = kstrtobool(buf, &commit); 405 if (rc) 406 return rc; 407 408 if (commit) { 409 rc = __commit(cxlr); 410 if (rc) 411 return rc; 412 return len; 413 } 414 415 rc = queue_reset(cxlr); 416 if (rc) 417 return rc; 418 419 /* 420 * Unmap the region and depend the reset-pending state to ensure 421 * it does not go active again until post reset 422 */ 423 device_release_driver(&cxlr->dev); 424 425 /* 426 * With the reset pending take cxl_rwsem.region unconditionally 427 * to ensure the reset gets handled before returning. 428 */ 429 guard(rwsem_write)(&cxl_rwsem.region); 430 431 /* 432 * Revalidate that the reset is still pending in case another 433 * thread already handled this reset. 434 */ 435 if (p->state == CXL_CONFIG_RESET_PENDING) { 436 cxl_region_decode_reset(cxlr, p->interleave_ways); 437 p->state = CXL_CONFIG_ACTIVE; 438 } 439 440 return len; 441 } 442 443 static ssize_t commit_show(struct device *dev, struct device_attribute *attr, 444 char *buf) 445 { 446 struct cxl_region *cxlr = to_cxl_region(dev); 447 struct cxl_region_params *p = &cxlr->params; 448 ssize_t rc; 449 450 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region); 451 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem))) 452 return rc; 453 return sysfs_emit(buf, "%d\n", p->state >= CXL_CONFIG_COMMIT); 454 } 455 static DEVICE_ATTR_RW(commit); 456 457 static umode_t cxl_region_visible(struct kobject *kobj, struct attribute *a, 458 int n) 459 { 460 struct device *dev = kobj_to_dev(kobj); 461 struct cxl_region *cxlr = to_cxl_region(dev); 462 463 /* 464 * Support tooling that expects to find a 'uuid' attribute for all 465 * regions regardless of mode. 466 */ 467 if (a == &dev_attr_uuid.attr && cxlr->mode != CXL_PARTMODE_PMEM) 468 return 0444; 469 return a->mode; 470 } 471 472 static ssize_t interleave_ways_show(struct device *dev, 473 struct device_attribute *attr, char *buf) 474 { 475 struct cxl_region *cxlr = to_cxl_region(dev); 476 struct cxl_region_params *p = &cxlr->params; 477 int rc; 478 479 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region); 480 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem))) 481 return rc; 482 return sysfs_emit(buf, "%d\n", p->interleave_ways); 483 } 484 485 static const struct attribute_group *get_cxl_region_target_group(void); 486 487 static ssize_t interleave_ways_store(struct device *dev, 488 struct device_attribute *attr, 489 const char *buf, size_t len) 490 { 491 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent); 492 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld; 493 struct cxl_region *cxlr = to_cxl_region(dev); 494 struct cxl_region_params *p = &cxlr->params; 495 unsigned int val, save; 496 int rc; 497 u8 iw; 498 499 rc = kstrtouint(buf, 0, &val); 500 if (rc) 501 return rc; 502 503 rc = ways_to_eiw(val, &iw); 504 if (rc) 505 return rc; 506 507 /* 508 * Even for x3, x6, and x12 interleaves the region interleave must be a 509 * power of 2 multiple of the host bridge interleave. 510 */ 511 if (!is_power_of_2(val / cxld->interleave_ways) || 512 (val % cxld->interleave_ways)) { 513 dev_dbg(&cxlr->dev, "invalid interleave: %d\n", val); 514 return -EINVAL; 515 } 516 517 ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region); 518 if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem))) 519 return rc; 520 521 if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) 522 return -EBUSY; 523 524 save = p->interleave_ways; 525 p->interleave_ways = val; 526 rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group()); 527 if (rc) { 528 p->interleave_ways = save; 529 return rc; 530 } 531 532 return len; 533 } 534 static DEVICE_ATTR_RW(interleave_ways); 535 536 static ssize_t interleave_granularity_show(struct device *dev, 537 struct device_attribute *attr, 538 char *buf) 539 { 540 struct cxl_region *cxlr = to_cxl_region(dev); 541 struct cxl_region_params *p = &cxlr->params; 542 int rc; 543 544 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region); 545 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem))) 546 return rc; 547 return sysfs_emit(buf, "%d\n", p->interleave_granularity); 548 } 549 550 static ssize_t interleave_granularity_store(struct device *dev, 551 struct device_attribute *attr, 552 const char *buf, size_t len) 553 { 554 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent); 555 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld; 556 struct cxl_region *cxlr = to_cxl_region(dev); 557 struct cxl_region_params *p = &cxlr->params; 558 int rc, val; 559 u16 ig; 560 561 rc = kstrtoint(buf, 0, &val); 562 if (rc) 563 return rc; 564 565 rc = granularity_to_eig(val, &ig); 566 if (rc) 567 return rc; 568 569 /* 570 * When the host-bridge is interleaved, disallow region granularity != 571 * root granularity. Regions with a granularity less than the root 572 * interleave result in needing multiple endpoints to support a single 573 * slot in the interleave (possible to support in the future). Regions 574 * with a granularity greater than the root interleave result in invalid 575 * DPA translations (invalid to support). 576 */ 577 if (cxld->interleave_ways > 1 && val != cxld->interleave_granularity) 578 return -EINVAL; 579 580 ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region); 581 if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem))) 582 return rc; 583 584 if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) 585 return -EBUSY; 586 587 p->interleave_granularity = val; 588 589 return len; 590 } 591 static DEVICE_ATTR_RW(interleave_granularity); 592 593 static ssize_t resource_show(struct device *dev, struct device_attribute *attr, 594 char *buf) 595 { 596 struct cxl_region *cxlr = to_cxl_region(dev); 597 struct cxl_region_params *p = &cxlr->params; 598 u64 resource = -1ULL; 599 int rc; 600 601 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region); 602 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem))) 603 return rc; 604 605 if (p->res) 606 resource = p->res->start; 607 return sysfs_emit(buf, "%#llx\n", resource); 608 } 609 static DEVICE_ATTR_RO(resource); 610 611 static ssize_t mode_show(struct device *dev, struct device_attribute *attr, 612 char *buf) 613 { 614 struct cxl_region *cxlr = to_cxl_region(dev); 615 const char *desc; 616 617 if (cxlr->mode == CXL_PARTMODE_RAM) 618 desc = "ram"; 619 else if (cxlr->mode == CXL_PARTMODE_PMEM) 620 desc = "pmem"; 621 else 622 desc = ""; 623 624 return sysfs_emit(buf, "%s\n", desc); 625 } 626 static DEVICE_ATTR_RO(mode); 627 628 static int alloc_hpa(struct cxl_region *cxlr, resource_size_t size) 629 { 630 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent); 631 struct cxl_region_params *p = &cxlr->params; 632 struct resource *res; 633 u64 remainder = 0; 634 635 lockdep_assert_held_write(&cxl_rwsem.region); 636 637 /* Nothing to do... */ 638 if (p->res && resource_size(p->res) == size) 639 return 0; 640 641 /* To change size the old size must be freed first */ 642 if (p->res) 643 return -EBUSY; 644 645 if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) 646 return -EBUSY; 647 648 /* ways, granularity and uuid (if PMEM) need to be set before HPA */ 649 if (!p->interleave_ways || !p->interleave_granularity || 650 (cxlr->mode == CXL_PARTMODE_PMEM && uuid_is_null(&p->uuid))) 651 return -ENXIO; 652 653 div64_u64_rem(size, (u64)SZ_256M * p->interleave_ways, &remainder); 654 if (remainder) 655 return -EINVAL; 656 657 res = alloc_free_mem_region(cxlrd->res, size, SZ_256M, 658 dev_name(&cxlr->dev)); 659 if (IS_ERR(res)) { 660 dev_dbg(&cxlr->dev, 661 "HPA allocation error (%ld) for size:%pap in %s %pr\n", 662 PTR_ERR(res), &size, cxlrd->res->name, cxlrd->res); 663 return PTR_ERR(res); 664 } 665 666 p->res = res; 667 p->state = CXL_CONFIG_INTERLEAVE_ACTIVE; 668 669 return 0; 670 } 671 672 static void cxl_region_iomem_release(struct cxl_region *cxlr) 673 { 674 struct cxl_region_params *p = &cxlr->params; 675 676 if (device_is_registered(&cxlr->dev)) 677 lockdep_assert_held_write(&cxl_rwsem.region); 678 if (p->res) { 679 /* 680 * Autodiscovered regions may not have been able to insert their 681 * resource. 682 */ 683 if (p->res->parent) 684 remove_resource(p->res); 685 kfree(p->res); 686 p->res = NULL; 687 } 688 } 689 690 static int free_hpa(struct cxl_region *cxlr) 691 { 692 struct cxl_region_params *p = &cxlr->params; 693 694 lockdep_assert_held_write(&cxl_rwsem.region); 695 696 if (!p->res) 697 return 0; 698 699 if (p->state >= CXL_CONFIG_ACTIVE) 700 return -EBUSY; 701 702 cxl_region_iomem_release(cxlr); 703 p->state = CXL_CONFIG_IDLE; 704 return 0; 705 } 706 707 static ssize_t size_store(struct device *dev, struct device_attribute *attr, 708 const char *buf, size_t len) 709 { 710 struct cxl_region *cxlr = to_cxl_region(dev); 711 u64 val; 712 int rc; 713 714 rc = kstrtou64(buf, 0, &val); 715 if (rc) 716 return rc; 717 718 ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region); 719 if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem))) 720 return rc; 721 722 if (val) 723 rc = alloc_hpa(cxlr, val); 724 else 725 rc = free_hpa(cxlr); 726 727 if (rc) 728 return rc; 729 730 return len; 731 } 732 733 static ssize_t size_show(struct device *dev, struct device_attribute *attr, 734 char *buf) 735 { 736 struct cxl_region *cxlr = to_cxl_region(dev); 737 struct cxl_region_params *p = &cxlr->params; 738 u64 size = 0; 739 ssize_t rc; 740 741 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region); 742 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem))) 743 return rc; 744 if (p->res) 745 size = resource_size(p->res); 746 return sysfs_emit(buf, "%#llx\n", size); 747 } 748 static DEVICE_ATTR_RW(size); 749 750 static struct attribute *cxl_region_attrs[] = { 751 &dev_attr_uuid.attr, 752 &dev_attr_commit.attr, 753 &dev_attr_interleave_ways.attr, 754 &dev_attr_interleave_granularity.attr, 755 &dev_attr_resource.attr, 756 &dev_attr_size.attr, 757 &dev_attr_mode.attr, 758 NULL, 759 }; 760 761 static const struct attribute_group cxl_region_group = { 762 .attrs = cxl_region_attrs, 763 .is_visible = cxl_region_visible, 764 }; 765 766 static size_t show_targetN(struct cxl_region *cxlr, char *buf, int pos) 767 { 768 struct cxl_region_params *p = &cxlr->params; 769 struct cxl_endpoint_decoder *cxled; 770 int rc; 771 772 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region); 773 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem))) 774 return rc; 775 776 if (pos >= p->interleave_ways) { 777 dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos, 778 p->interleave_ways); 779 return -ENXIO; 780 } 781 782 cxled = p->targets[pos]; 783 if (!cxled) 784 return sysfs_emit(buf, "\n"); 785 return sysfs_emit(buf, "%s\n", dev_name(&cxled->cxld.dev)); 786 } 787 788 static int check_commit_order(struct device *dev, void *data) 789 { 790 struct cxl_decoder *cxld = to_cxl_decoder(dev); 791 792 /* 793 * if port->commit_end is not the only free decoder, then out of 794 * order shutdown has occurred, block further allocations until 795 * that is resolved 796 */ 797 if (((cxld->flags & CXL_DECODER_F_ENABLE) == 0)) 798 return -EBUSY; 799 return 0; 800 } 801 802 static int match_free_decoder(struct device *dev, const void *data) 803 { 804 struct cxl_port *port = to_cxl_port(dev->parent); 805 struct cxl_decoder *cxld; 806 int rc; 807 808 if (!is_switch_decoder(dev)) 809 return 0; 810 811 cxld = to_cxl_decoder(dev); 812 813 if (cxld->id != port->commit_end + 1) 814 return 0; 815 816 if (cxld->region) { 817 dev_dbg(dev->parent, 818 "next decoder to commit (%s) is already reserved (%s)\n", 819 dev_name(dev), dev_name(&cxld->region->dev)); 820 return 0; 821 } 822 823 rc = device_for_each_child_reverse_from(dev->parent, dev, NULL, 824 check_commit_order); 825 if (rc) { 826 dev_dbg(dev->parent, 827 "unable to allocate %s due to out of order shutdown\n", 828 dev_name(dev)); 829 return 0; 830 } 831 return 1; 832 } 833 834 static bool region_res_match_cxl_range(const struct cxl_region_params *p, 835 struct range *range) 836 { 837 if (!p->res) 838 return false; 839 840 /* 841 * If an extended linear cache region then the CXL range is assumed 842 * to be fronted by the DRAM range in current known implementation. 843 * This assumption will be made until a variant implementation exists. 844 */ 845 return p->res->start + p->cache_size == range->start && 846 p->res->end == range->end; 847 } 848 849 static int match_auto_decoder(struct device *dev, const void *data) 850 { 851 const struct cxl_region_params *p = data; 852 struct cxl_decoder *cxld; 853 struct range *r; 854 855 if (!is_switch_decoder(dev)) 856 return 0; 857 858 cxld = to_cxl_decoder(dev); 859 r = &cxld->hpa_range; 860 861 if (region_res_match_cxl_range(p, r)) 862 return 1; 863 864 return 0; 865 } 866 867 /** 868 * cxl_port_pick_region_decoder() - assign or lookup a decoder for a region 869 * @port: a port in the ancestry of the endpoint implied by @cxled 870 * @cxled: endpoint decoder to be, or currently, mapped by @port 871 * @cxlr: region to establish, or validate, decode @port 872 * 873 * In the region creation path cxl_port_pick_region_decoder() is an 874 * allocator to find a free port. In the region assembly path, it is 875 * recalling the decoder that platform firmware picked for validation 876 * purposes. 877 * 878 * The result is recorded in a 'struct cxl_region_ref' in @port. 879 */ 880 static struct cxl_decoder * 881 cxl_port_pick_region_decoder(struct cxl_port *port, 882 struct cxl_endpoint_decoder *cxled, 883 struct cxl_region *cxlr) 884 { 885 struct device *dev; 886 887 if (port == cxled_to_port(cxled)) 888 return &cxled->cxld; 889 890 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) 891 dev = device_find_child(&port->dev, &cxlr->params, 892 match_auto_decoder); 893 else 894 dev = device_find_child(&port->dev, NULL, match_free_decoder); 895 if (!dev) 896 return NULL; 897 /* 898 * This decoder is pinned registered as long as the endpoint decoder is 899 * registered, and endpoint decoder unregistration holds the 900 * cxl_rwsem.region over unregister events, so no need to hold on to 901 * this extra reference. 902 */ 903 put_device(dev); 904 return to_cxl_decoder(dev); 905 } 906 907 static bool auto_order_ok(struct cxl_port *port, struct cxl_region *cxlr_iter, 908 struct cxl_decoder *cxld) 909 { 910 struct cxl_region_ref *rr = cxl_rr_load(port, cxlr_iter); 911 struct cxl_decoder *cxld_iter = rr->decoder; 912 913 /* 914 * Allow the out of order assembly of auto-discovered regions. 915 * Per CXL Spec 3.1 8.2.4.20.12 software must commit decoders 916 * in HPA order. Confirm that the decoder with the lesser HPA 917 * starting address has the lesser id. 918 */ 919 dev_dbg(&cxld->dev, "check for HPA violation %s:%d < %s:%d\n", 920 dev_name(&cxld->dev), cxld->id, 921 dev_name(&cxld_iter->dev), cxld_iter->id); 922 923 if (cxld_iter->id > cxld->id) 924 return true; 925 926 return false; 927 } 928 929 static struct cxl_region_ref * 930 alloc_region_ref(struct cxl_port *port, struct cxl_region *cxlr, 931 struct cxl_endpoint_decoder *cxled, 932 struct cxl_decoder *cxld) 933 { 934 struct cxl_region_params *p = &cxlr->params; 935 struct cxl_region_ref *cxl_rr, *iter; 936 unsigned long index; 937 int rc; 938 939 xa_for_each(&port->regions, index, iter) { 940 struct cxl_region_params *ip = &iter->region->params; 941 942 if (!ip->res || ip->res->start < p->res->start) 943 continue; 944 945 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) { 946 if (auto_order_ok(port, iter->region, cxld)) 947 continue; 948 } 949 dev_dbg(&cxlr->dev, "%s: HPA order violation %s:%pr vs %pr\n", 950 dev_name(&port->dev), 951 dev_name(&iter->region->dev), ip->res, p->res); 952 953 return ERR_PTR(-EBUSY); 954 } 955 956 cxl_rr = kzalloc(sizeof(*cxl_rr), GFP_KERNEL); 957 if (!cxl_rr) 958 return ERR_PTR(-ENOMEM); 959 cxl_rr->port = port; 960 cxl_rr->region = cxlr; 961 cxl_rr->nr_targets = 1; 962 xa_init(&cxl_rr->endpoints); 963 964 rc = xa_insert(&port->regions, (unsigned long)cxlr, cxl_rr, GFP_KERNEL); 965 if (rc) { 966 dev_dbg(&cxlr->dev, 967 "%s: failed to track region reference: %d\n", 968 dev_name(&port->dev), rc); 969 kfree(cxl_rr); 970 return ERR_PTR(rc); 971 } 972 973 return cxl_rr; 974 } 975 976 static void cxl_rr_free_decoder(struct cxl_region_ref *cxl_rr) 977 { 978 struct cxl_region *cxlr = cxl_rr->region; 979 struct cxl_decoder *cxld = cxl_rr->decoder; 980 981 if (!cxld) 982 return; 983 984 dev_WARN_ONCE(&cxlr->dev, cxld->region != cxlr, "region mismatch\n"); 985 if (cxld->region == cxlr) { 986 cxld->region = NULL; 987 put_device(&cxlr->dev); 988 } 989 } 990 991 static void free_region_ref(struct cxl_region_ref *cxl_rr) 992 { 993 struct cxl_port *port = cxl_rr->port; 994 struct cxl_region *cxlr = cxl_rr->region; 995 996 cxl_rr_free_decoder(cxl_rr); 997 xa_erase(&port->regions, (unsigned long)cxlr); 998 xa_destroy(&cxl_rr->endpoints); 999 kfree(cxl_rr); 1000 } 1001 1002 static int cxl_rr_ep_add(struct cxl_region_ref *cxl_rr, 1003 struct cxl_endpoint_decoder *cxled) 1004 { 1005 int rc; 1006 struct cxl_port *port = cxl_rr->port; 1007 struct cxl_region *cxlr = cxl_rr->region; 1008 struct cxl_decoder *cxld = cxl_rr->decoder; 1009 struct cxl_ep *ep = cxl_ep_load(port, cxled_to_memdev(cxled)); 1010 1011 if (ep) { 1012 rc = xa_insert(&cxl_rr->endpoints, (unsigned long)cxled, ep, 1013 GFP_KERNEL); 1014 if (rc) 1015 return rc; 1016 } 1017 cxl_rr->nr_eps++; 1018 1019 if (!cxld->region) { 1020 cxld->region = cxlr; 1021 get_device(&cxlr->dev); 1022 } 1023 1024 return 0; 1025 } 1026 1027 static int cxl_rr_assign_decoder(struct cxl_port *port, struct cxl_region *cxlr, 1028 struct cxl_endpoint_decoder *cxled, 1029 struct cxl_region_ref *cxl_rr, 1030 struct cxl_decoder *cxld) 1031 { 1032 if (cxld->region) { 1033 dev_dbg(&cxlr->dev, "%s: %s already attached to %s\n", 1034 dev_name(&port->dev), dev_name(&cxld->dev), 1035 dev_name(&cxld->region->dev)); 1036 return -EBUSY; 1037 } 1038 1039 /* 1040 * Endpoints should already match the region type, but backstop that 1041 * assumption with an assertion. Switch-decoders change mapping-type 1042 * based on what is mapped when they are assigned to a region. 1043 */ 1044 dev_WARN_ONCE(&cxlr->dev, 1045 port == cxled_to_port(cxled) && 1046 cxld->target_type != cxlr->type, 1047 "%s:%s mismatch decoder type %d -> %d\n", 1048 dev_name(&cxled_to_memdev(cxled)->dev), 1049 dev_name(&cxld->dev), cxld->target_type, cxlr->type); 1050 cxld->target_type = cxlr->type; 1051 cxl_rr->decoder = cxld; 1052 return 0; 1053 } 1054 1055 /** 1056 * cxl_port_attach_region() - track a region's interest in a port by endpoint 1057 * @port: port to add a new region reference 'struct cxl_region_ref' 1058 * @cxlr: region to attach to @port 1059 * @cxled: endpoint decoder used to create or further pin a region reference 1060 * @pos: interleave position of @cxled in @cxlr 1061 * 1062 * The attach event is an opportunity to validate CXL decode setup 1063 * constraints and record metadata needed for programming HDM decoders, 1064 * in particular decoder target lists. 1065 * 1066 * The steps are: 1067 * 1068 * - validate that there are no other regions with a higher HPA already 1069 * associated with @port 1070 * - establish a region reference if one is not already present 1071 * 1072 * - additionally allocate a decoder instance that will host @cxlr on 1073 * @port 1074 * 1075 * - pin the region reference by the endpoint 1076 * - account for how many entries in @port's target list are needed to 1077 * cover all of the added endpoints. 1078 */ 1079 static int cxl_port_attach_region(struct cxl_port *port, 1080 struct cxl_region *cxlr, 1081 struct cxl_endpoint_decoder *cxled, int pos) 1082 { 1083 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 1084 struct cxl_ep *ep = cxl_ep_load(port, cxlmd); 1085 struct cxl_region_ref *cxl_rr; 1086 bool nr_targets_inc = false; 1087 struct cxl_decoder *cxld; 1088 unsigned long index; 1089 int rc = -EBUSY; 1090 1091 lockdep_assert_held_write(&cxl_rwsem.region); 1092 1093 cxl_rr = cxl_rr_load(port, cxlr); 1094 if (cxl_rr) { 1095 struct cxl_ep *ep_iter; 1096 int found = 0; 1097 1098 /* 1099 * Walk the existing endpoints that have been attached to 1100 * @cxlr at @port and see if they share the same 'next' port 1101 * in the downstream direction. I.e. endpoints that share common 1102 * upstream switch. 1103 */ 1104 xa_for_each(&cxl_rr->endpoints, index, ep_iter) { 1105 if (ep_iter == ep) 1106 continue; 1107 if (ep_iter->next == ep->next) { 1108 found++; 1109 break; 1110 } 1111 } 1112 1113 /* 1114 * New target port, or @port is an endpoint port that always 1115 * accounts its own local decode as a target. 1116 */ 1117 if (!found || !ep->next) { 1118 cxl_rr->nr_targets++; 1119 nr_targets_inc = true; 1120 } 1121 } else { 1122 struct cxl_decoder *cxld; 1123 1124 cxld = cxl_port_pick_region_decoder(port, cxled, cxlr); 1125 if (!cxld) { 1126 dev_dbg(&cxlr->dev, "%s: no decoder available\n", 1127 dev_name(&port->dev)); 1128 return -EBUSY; 1129 } 1130 1131 cxl_rr = alloc_region_ref(port, cxlr, cxled, cxld); 1132 if (IS_ERR(cxl_rr)) { 1133 dev_dbg(&cxlr->dev, 1134 "%s: failed to allocate region reference\n", 1135 dev_name(&port->dev)); 1136 return PTR_ERR(cxl_rr); 1137 } 1138 nr_targets_inc = true; 1139 1140 rc = cxl_rr_assign_decoder(port, cxlr, cxled, cxl_rr, cxld); 1141 if (rc) 1142 goto out_erase; 1143 } 1144 cxld = cxl_rr->decoder; 1145 1146 /* 1147 * the number of targets should not exceed the target_count 1148 * of the decoder 1149 */ 1150 if (is_switch_decoder(&cxld->dev)) { 1151 struct cxl_switch_decoder *cxlsd; 1152 1153 cxlsd = to_cxl_switch_decoder(&cxld->dev); 1154 if (cxl_rr->nr_targets > cxlsd->nr_targets) { 1155 dev_dbg(&cxlr->dev, 1156 "%s:%s %s add: %s:%s @ %d overflows targets: %d\n", 1157 dev_name(port->uport_dev), dev_name(&port->dev), 1158 dev_name(&cxld->dev), dev_name(&cxlmd->dev), 1159 dev_name(&cxled->cxld.dev), pos, 1160 cxlsd->nr_targets); 1161 rc = -ENXIO; 1162 goto out_erase; 1163 } 1164 } 1165 1166 rc = cxl_rr_ep_add(cxl_rr, cxled); 1167 if (rc) { 1168 dev_dbg(&cxlr->dev, 1169 "%s: failed to track endpoint %s:%s reference\n", 1170 dev_name(&port->dev), dev_name(&cxlmd->dev), 1171 dev_name(&cxld->dev)); 1172 goto out_erase; 1173 } 1174 1175 dev_dbg(&cxlr->dev, 1176 "%s:%s %s add: %s:%s @ %d next: %s nr_eps: %d nr_targets: %d\n", 1177 dev_name(port->uport_dev), dev_name(&port->dev), 1178 dev_name(&cxld->dev), dev_name(&cxlmd->dev), 1179 dev_name(&cxled->cxld.dev), pos, 1180 ep ? ep->next ? dev_name(ep->next->uport_dev) : 1181 dev_name(&cxlmd->dev) : 1182 "none", 1183 cxl_rr->nr_eps, cxl_rr->nr_targets); 1184 1185 return 0; 1186 out_erase: 1187 if (nr_targets_inc) 1188 cxl_rr->nr_targets--; 1189 if (cxl_rr->nr_eps == 0) 1190 free_region_ref(cxl_rr); 1191 return rc; 1192 } 1193 1194 static void cxl_port_detach_region(struct cxl_port *port, 1195 struct cxl_region *cxlr, 1196 struct cxl_endpoint_decoder *cxled) 1197 { 1198 struct cxl_region_ref *cxl_rr; 1199 struct cxl_ep *ep = NULL; 1200 1201 lockdep_assert_held_write(&cxl_rwsem.region); 1202 1203 cxl_rr = cxl_rr_load(port, cxlr); 1204 if (!cxl_rr) 1205 return; 1206 1207 /* 1208 * Endpoint ports do not carry cxl_ep references, and they 1209 * never target more than one endpoint by definition 1210 */ 1211 if (cxl_rr->decoder == &cxled->cxld) 1212 cxl_rr->nr_eps--; 1213 else 1214 ep = xa_erase(&cxl_rr->endpoints, (unsigned long)cxled); 1215 if (ep) { 1216 struct cxl_ep *ep_iter; 1217 unsigned long index; 1218 int found = 0; 1219 1220 cxl_rr->nr_eps--; 1221 xa_for_each(&cxl_rr->endpoints, index, ep_iter) { 1222 if (ep_iter->next == ep->next) { 1223 found++; 1224 break; 1225 } 1226 } 1227 if (!found) 1228 cxl_rr->nr_targets--; 1229 } 1230 1231 if (cxl_rr->nr_eps == 0) 1232 free_region_ref(cxl_rr); 1233 } 1234 1235 static int check_last_peer(struct cxl_endpoint_decoder *cxled, 1236 struct cxl_ep *ep, struct cxl_region_ref *cxl_rr, 1237 int distance) 1238 { 1239 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 1240 struct cxl_region *cxlr = cxl_rr->region; 1241 struct cxl_region_params *p = &cxlr->params; 1242 struct cxl_endpoint_decoder *cxled_peer; 1243 struct cxl_port *port = cxl_rr->port; 1244 struct cxl_memdev *cxlmd_peer; 1245 struct cxl_ep *ep_peer; 1246 int pos = cxled->pos; 1247 1248 /* 1249 * If this position wants to share a dport with the last endpoint mapped 1250 * then that endpoint, at index 'position - distance', must also be 1251 * mapped by this dport. 1252 */ 1253 if (pos < distance) { 1254 dev_dbg(&cxlr->dev, "%s:%s: cannot host %s:%s at %d\n", 1255 dev_name(port->uport_dev), dev_name(&port->dev), 1256 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos); 1257 return -ENXIO; 1258 } 1259 cxled_peer = p->targets[pos - distance]; 1260 cxlmd_peer = cxled_to_memdev(cxled_peer); 1261 ep_peer = cxl_ep_load(port, cxlmd_peer); 1262 if (ep->dport != ep_peer->dport) { 1263 dev_dbg(&cxlr->dev, 1264 "%s:%s: %s:%s pos %d mismatched peer %s:%s\n", 1265 dev_name(port->uport_dev), dev_name(&port->dev), 1266 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos, 1267 dev_name(&cxlmd_peer->dev), 1268 dev_name(&cxled_peer->cxld.dev)); 1269 return -ENXIO; 1270 } 1271 1272 return 0; 1273 } 1274 1275 static int check_interleave_cap(struct cxl_decoder *cxld, int iw, int ig) 1276 { 1277 struct cxl_port *port = to_cxl_port(cxld->dev.parent); 1278 struct cxl_hdm *cxlhdm = dev_get_drvdata(&port->dev); 1279 unsigned int interleave_mask; 1280 u8 eiw; 1281 u16 eig; 1282 int high_pos, low_pos; 1283 1284 if (!test_bit(iw, &cxlhdm->iw_cap_mask)) 1285 return -ENXIO; 1286 /* 1287 * Per CXL specification r3.1(8.2.4.20.13 Decoder Protection), 1288 * if eiw < 8: 1289 * DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + 8 + eiw] 1290 * DPAOFFSET[eig + 7: 0] = HPAOFFSET[eig + 7: 0] 1291 * 1292 * when the eiw is 0, all the bits of HPAOFFSET[51: 0] are used, the 1293 * interleave bits are none. 1294 * 1295 * if eiw >= 8: 1296 * DPAOFFSET[51: eig + 8] = HPAOFFSET[51: eig + eiw] / 3 1297 * DPAOFFSET[eig + 7: 0] = HPAOFFSET[eig + 7: 0] 1298 * 1299 * when the eiw is 8, all the bits of HPAOFFSET[51: 0] are used, the 1300 * interleave bits are none. 1301 */ 1302 ways_to_eiw(iw, &eiw); 1303 if (eiw == 0 || eiw == 8) 1304 return 0; 1305 1306 granularity_to_eig(ig, &eig); 1307 if (eiw > 8) 1308 high_pos = eiw + eig - 1; 1309 else 1310 high_pos = eiw + eig + 7; 1311 low_pos = eig + 8; 1312 interleave_mask = GENMASK(high_pos, low_pos); 1313 if (interleave_mask & ~cxlhdm->interleave_mask) 1314 return -ENXIO; 1315 1316 return 0; 1317 } 1318 1319 static int cxl_port_setup_targets(struct cxl_port *port, 1320 struct cxl_region *cxlr, 1321 struct cxl_endpoint_decoder *cxled) 1322 { 1323 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent); 1324 int parent_iw, parent_ig, ig, iw, rc, inc = 0, pos = cxled->pos; 1325 struct cxl_port *parent_port = to_cxl_port(port->dev.parent); 1326 struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr); 1327 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 1328 struct cxl_ep *ep = cxl_ep_load(port, cxlmd); 1329 struct cxl_region_params *p = &cxlr->params; 1330 struct cxl_decoder *cxld = cxl_rr->decoder; 1331 struct cxl_switch_decoder *cxlsd; 1332 struct cxl_port *iter = port; 1333 u16 eig, peig; 1334 u8 eiw, peiw; 1335 1336 /* 1337 * While root level decoders support x3, x6, x12, switch level 1338 * decoders only support powers of 2 up to x16. 1339 */ 1340 if (!is_power_of_2(cxl_rr->nr_targets)) { 1341 dev_dbg(&cxlr->dev, "%s:%s: invalid target count %d\n", 1342 dev_name(port->uport_dev), dev_name(&port->dev), 1343 cxl_rr->nr_targets); 1344 return -EINVAL; 1345 } 1346 1347 cxlsd = to_cxl_switch_decoder(&cxld->dev); 1348 if (cxl_rr->nr_targets_set) { 1349 int i, distance = 1; 1350 struct cxl_region_ref *cxl_rr_iter; 1351 1352 /* 1353 * The "distance" between peer downstream ports represents which 1354 * endpoint positions in the region interleave a given port can 1355 * host. 1356 * 1357 * For example, at the root of a hierarchy the distance is 1358 * always 1 as every index targets a different host-bridge. At 1359 * each subsequent switch level those ports map every Nth region 1360 * position where N is the width of the switch == distance. 1361 */ 1362 do { 1363 cxl_rr_iter = cxl_rr_load(iter, cxlr); 1364 distance *= cxl_rr_iter->nr_targets; 1365 iter = to_cxl_port(iter->dev.parent); 1366 } while (!is_cxl_root(iter)); 1367 distance *= cxlrd->cxlsd.cxld.interleave_ways; 1368 1369 for (i = 0; i < cxl_rr->nr_targets_set; i++) 1370 if (ep->dport == cxlsd->target[i]) { 1371 rc = check_last_peer(cxled, ep, cxl_rr, 1372 distance); 1373 if (rc) 1374 return rc; 1375 goto out_target_set; 1376 } 1377 goto add_target; 1378 } 1379 1380 if (is_cxl_root(parent_port)) { 1381 /* 1382 * Root decoder IG is always set to value in CFMWS which 1383 * may be different than this region's IG. We can use the 1384 * region's IG here since interleave_granularity_store() 1385 * does not allow interleaved host-bridges with 1386 * root IG != region IG. 1387 */ 1388 parent_ig = p->interleave_granularity; 1389 parent_iw = cxlrd->cxlsd.cxld.interleave_ways; 1390 /* 1391 * For purposes of address bit routing, use power-of-2 math for 1392 * switch ports. 1393 */ 1394 if (!is_power_of_2(parent_iw)) 1395 parent_iw /= 3; 1396 } else { 1397 struct cxl_region_ref *parent_rr; 1398 struct cxl_decoder *parent_cxld; 1399 1400 parent_rr = cxl_rr_load(parent_port, cxlr); 1401 parent_cxld = parent_rr->decoder; 1402 parent_ig = parent_cxld->interleave_granularity; 1403 parent_iw = parent_cxld->interleave_ways; 1404 } 1405 1406 rc = granularity_to_eig(parent_ig, &peig); 1407 if (rc) { 1408 dev_dbg(&cxlr->dev, "%s:%s: invalid parent granularity: %d\n", 1409 dev_name(parent_port->uport_dev), 1410 dev_name(&parent_port->dev), parent_ig); 1411 return rc; 1412 } 1413 1414 rc = ways_to_eiw(parent_iw, &peiw); 1415 if (rc) { 1416 dev_dbg(&cxlr->dev, "%s:%s: invalid parent interleave: %d\n", 1417 dev_name(parent_port->uport_dev), 1418 dev_name(&parent_port->dev), parent_iw); 1419 return rc; 1420 } 1421 1422 iw = cxl_rr->nr_targets; 1423 rc = ways_to_eiw(iw, &eiw); 1424 if (rc) { 1425 dev_dbg(&cxlr->dev, "%s:%s: invalid port interleave: %d\n", 1426 dev_name(port->uport_dev), dev_name(&port->dev), iw); 1427 return rc; 1428 } 1429 1430 /* 1431 * Interleave granularity is a multiple of @parent_port granularity. 1432 * Multiplier is the parent port interleave ways. 1433 */ 1434 rc = granularity_to_eig(parent_ig * parent_iw, &eig); 1435 if (rc) { 1436 dev_dbg(&cxlr->dev, 1437 "%s: invalid granularity calculation (%d * %d)\n", 1438 dev_name(&parent_port->dev), parent_ig, parent_iw); 1439 return rc; 1440 } 1441 1442 rc = eig_to_granularity(eig, &ig); 1443 if (rc) { 1444 dev_dbg(&cxlr->dev, "%s:%s: invalid interleave: %d\n", 1445 dev_name(port->uport_dev), dev_name(&port->dev), 1446 256 << eig); 1447 return rc; 1448 } 1449 1450 if (iw > 8 || iw > cxlsd->nr_targets) { 1451 dev_dbg(&cxlr->dev, 1452 "%s:%s:%s: ways: %d overflows targets: %d\n", 1453 dev_name(port->uport_dev), dev_name(&port->dev), 1454 dev_name(&cxld->dev), iw, cxlsd->nr_targets); 1455 return -ENXIO; 1456 } 1457 1458 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) { 1459 if (cxld->interleave_ways != iw || 1460 (iw > 1 && cxld->interleave_granularity != ig) || 1461 !region_res_match_cxl_range(p, &cxld->hpa_range) || 1462 ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)) { 1463 dev_err(&cxlr->dev, 1464 "%s:%s %s expected iw: %d ig: %d %pr\n", 1465 dev_name(port->uport_dev), dev_name(&port->dev), 1466 __func__, iw, ig, p->res); 1467 dev_err(&cxlr->dev, 1468 "%s:%s %s got iw: %d ig: %d state: %s %#llx:%#llx\n", 1469 dev_name(port->uport_dev), dev_name(&port->dev), 1470 __func__, cxld->interleave_ways, 1471 cxld->interleave_granularity, 1472 (cxld->flags & CXL_DECODER_F_ENABLE) ? 1473 "enabled" : 1474 "disabled", 1475 cxld->hpa_range.start, cxld->hpa_range.end); 1476 return -ENXIO; 1477 } 1478 } else { 1479 rc = check_interleave_cap(cxld, iw, ig); 1480 if (rc) { 1481 dev_dbg(&cxlr->dev, 1482 "%s:%s iw: %d ig: %d is not supported\n", 1483 dev_name(port->uport_dev), 1484 dev_name(&port->dev), iw, ig); 1485 return rc; 1486 } 1487 1488 cxld->interleave_ways = iw; 1489 cxld->interleave_granularity = ig; 1490 cxld->hpa_range = (struct range) { 1491 .start = p->res->start, 1492 .end = p->res->end, 1493 }; 1494 } 1495 dev_dbg(&cxlr->dev, "%s:%s iw: %d ig: %d\n", dev_name(port->uport_dev), 1496 dev_name(&port->dev), iw, ig); 1497 add_target: 1498 if (cxl_rr->nr_targets_set == cxl_rr->nr_targets) { 1499 dev_dbg(&cxlr->dev, 1500 "%s:%s: targets full trying to add %s:%s at %d\n", 1501 dev_name(port->uport_dev), dev_name(&port->dev), 1502 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos); 1503 return -ENXIO; 1504 } 1505 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) { 1506 if (cxlsd->target[cxl_rr->nr_targets_set] != ep->dport) { 1507 dev_dbg(&cxlr->dev, "%s:%s: %s expected %s at %d\n", 1508 dev_name(port->uport_dev), dev_name(&port->dev), 1509 dev_name(&cxlsd->cxld.dev), 1510 dev_name(ep->dport->dport_dev), 1511 cxl_rr->nr_targets_set); 1512 return -ENXIO; 1513 } 1514 } else 1515 cxlsd->target[cxl_rr->nr_targets_set] = ep->dport; 1516 inc = 1; 1517 out_target_set: 1518 cxl_rr->nr_targets_set += inc; 1519 dev_dbg(&cxlr->dev, "%s:%s target[%d] = %s for %s:%s @ %d\n", 1520 dev_name(port->uport_dev), dev_name(&port->dev), 1521 cxl_rr->nr_targets_set - 1, dev_name(ep->dport->dport_dev), 1522 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), pos); 1523 1524 return 0; 1525 } 1526 1527 static void cxl_port_reset_targets(struct cxl_port *port, 1528 struct cxl_region *cxlr) 1529 { 1530 struct cxl_region_ref *cxl_rr = cxl_rr_load(port, cxlr); 1531 struct cxl_decoder *cxld; 1532 1533 /* 1534 * After the last endpoint has been detached the entire cxl_rr may now 1535 * be gone. 1536 */ 1537 if (!cxl_rr) 1538 return; 1539 cxl_rr->nr_targets_set = 0; 1540 1541 cxld = cxl_rr->decoder; 1542 cxld->hpa_range = (struct range) { 1543 .start = 0, 1544 .end = -1, 1545 }; 1546 } 1547 1548 static void cxl_region_teardown_targets(struct cxl_region *cxlr) 1549 { 1550 struct cxl_region_params *p = &cxlr->params; 1551 struct cxl_endpoint_decoder *cxled; 1552 struct cxl_dev_state *cxlds; 1553 struct cxl_memdev *cxlmd; 1554 struct cxl_port *iter; 1555 struct cxl_ep *ep; 1556 int i; 1557 1558 /* 1559 * In the auto-discovery case skip automatic teardown since the 1560 * address space is already active 1561 */ 1562 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) 1563 return; 1564 1565 for (i = 0; i < p->nr_targets; i++) { 1566 cxled = p->targets[i]; 1567 cxlmd = cxled_to_memdev(cxled); 1568 cxlds = cxlmd->cxlds; 1569 1570 if (cxlds->rcd) 1571 continue; 1572 1573 iter = cxled_to_port(cxled); 1574 while (!is_cxl_root(to_cxl_port(iter->dev.parent))) 1575 iter = to_cxl_port(iter->dev.parent); 1576 1577 for (ep = cxl_ep_load(iter, cxlmd); iter; 1578 iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) 1579 cxl_port_reset_targets(iter, cxlr); 1580 } 1581 } 1582 1583 static int cxl_region_setup_targets(struct cxl_region *cxlr) 1584 { 1585 struct cxl_region_params *p = &cxlr->params; 1586 struct cxl_endpoint_decoder *cxled; 1587 struct cxl_dev_state *cxlds; 1588 int i, rc, rch = 0, vh = 0; 1589 struct cxl_memdev *cxlmd; 1590 struct cxl_port *iter; 1591 struct cxl_ep *ep; 1592 1593 for (i = 0; i < p->nr_targets; i++) { 1594 cxled = p->targets[i]; 1595 cxlmd = cxled_to_memdev(cxled); 1596 cxlds = cxlmd->cxlds; 1597 1598 /* validate that all targets agree on topology */ 1599 if (!cxlds->rcd) { 1600 vh++; 1601 } else { 1602 rch++; 1603 continue; 1604 } 1605 1606 iter = cxled_to_port(cxled); 1607 while (!is_cxl_root(to_cxl_port(iter->dev.parent))) 1608 iter = to_cxl_port(iter->dev.parent); 1609 1610 /* 1611 * Descend the topology tree programming / validating 1612 * targets while looking for conflicts. 1613 */ 1614 for (ep = cxl_ep_load(iter, cxlmd); iter; 1615 iter = ep->next, ep = cxl_ep_load(iter, cxlmd)) { 1616 rc = cxl_port_setup_targets(iter, cxlr, cxled); 1617 if (rc) { 1618 cxl_region_teardown_targets(cxlr); 1619 return rc; 1620 } 1621 } 1622 } 1623 1624 if (rch && vh) { 1625 dev_err(&cxlr->dev, "mismatched CXL topologies detected\n"); 1626 cxl_region_teardown_targets(cxlr); 1627 return -ENXIO; 1628 } 1629 1630 return 0; 1631 } 1632 1633 static int cxl_region_validate_position(struct cxl_region *cxlr, 1634 struct cxl_endpoint_decoder *cxled, 1635 int pos) 1636 { 1637 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 1638 struct cxl_region_params *p = &cxlr->params; 1639 int i; 1640 1641 if (pos < 0 || pos >= p->interleave_ways) { 1642 dev_dbg(&cxlr->dev, "position %d out of range %d\n", pos, 1643 p->interleave_ways); 1644 return -ENXIO; 1645 } 1646 1647 if (p->targets[pos] == cxled) 1648 return 0; 1649 1650 if (p->targets[pos]) { 1651 struct cxl_endpoint_decoder *cxled_target = p->targets[pos]; 1652 struct cxl_memdev *cxlmd_target = cxled_to_memdev(cxled_target); 1653 1654 dev_dbg(&cxlr->dev, "position %d already assigned to %s:%s\n", 1655 pos, dev_name(&cxlmd_target->dev), 1656 dev_name(&cxled_target->cxld.dev)); 1657 return -EBUSY; 1658 } 1659 1660 for (i = 0; i < p->interleave_ways; i++) { 1661 struct cxl_endpoint_decoder *cxled_target; 1662 struct cxl_memdev *cxlmd_target; 1663 1664 cxled_target = p->targets[i]; 1665 if (!cxled_target) 1666 continue; 1667 1668 cxlmd_target = cxled_to_memdev(cxled_target); 1669 if (cxlmd_target == cxlmd) { 1670 dev_dbg(&cxlr->dev, 1671 "%s already specified at position %d via: %s\n", 1672 dev_name(&cxlmd->dev), pos, 1673 dev_name(&cxled_target->cxld.dev)); 1674 return -EBUSY; 1675 } 1676 } 1677 1678 return 0; 1679 } 1680 1681 static int cxl_region_attach_position(struct cxl_region *cxlr, 1682 struct cxl_root_decoder *cxlrd, 1683 struct cxl_endpoint_decoder *cxled, 1684 const struct cxl_dport *dport, int pos) 1685 { 1686 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 1687 struct cxl_switch_decoder *cxlsd = &cxlrd->cxlsd; 1688 struct cxl_decoder *cxld = &cxlsd->cxld; 1689 int iw = cxld->interleave_ways; 1690 struct cxl_port *iter; 1691 int rc; 1692 1693 if (dport != cxlrd->cxlsd.target[pos % iw]) { 1694 dev_dbg(&cxlr->dev, "%s:%s invalid target position for %s\n", 1695 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), 1696 dev_name(&cxlrd->cxlsd.cxld.dev)); 1697 return -ENXIO; 1698 } 1699 1700 for (iter = cxled_to_port(cxled); !is_cxl_root(iter); 1701 iter = to_cxl_port(iter->dev.parent)) { 1702 rc = cxl_port_attach_region(iter, cxlr, cxled, pos); 1703 if (rc) 1704 goto err; 1705 } 1706 1707 return 0; 1708 1709 err: 1710 for (iter = cxled_to_port(cxled); !is_cxl_root(iter); 1711 iter = to_cxl_port(iter->dev.parent)) 1712 cxl_port_detach_region(iter, cxlr, cxled); 1713 return rc; 1714 } 1715 1716 static int cxl_region_attach_auto(struct cxl_region *cxlr, 1717 struct cxl_endpoint_decoder *cxled, int pos) 1718 { 1719 struct cxl_region_params *p = &cxlr->params; 1720 1721 if (cxled->state != CXL_DECODER_STATE_AUTO) { 1722 dev_err(&cxlr->dev, 1723 "%s: unable to add decoder to autodetected region\n", 1724 dev_name(&cxled->cxld.dev)); 1725 return -EINVAL; 1726 } 1727 1728 if (pos >= 0) { 1729 dev_dbg(&cxlr->dev, "%s: expected auto position, not %d\n", 1730 dev_name(&cxled->cxld.dev), pos); 1731 return -EINVAL; 1732 } 1733 1734 if (p->nr_targets >= p->interleave_ways) { 1735 dev_err(&cxlr->dev, "%s: no more target slots available\n", 1736 dev_name(&cxled->cxld.dev)); 1737 return -ENXIO; 1738 } 1739 1740 /* 1741 * Temporarily record the endpoint decoder into the target array. Yes, 1742 * this means that userspace can view devices in the wrong position 1743 * before the region activates, and must be careful to understand when 1744 * it might be racing region autodiscovery. 1745 */ 1746 pos = p->nr_targets; 1747 p->targets[pos] = cxled; 1748 cxled->pos = pos; 1749 p->nr_targets++; 1750 1751 return 0; 1752 } 1753 1754 static int cmp_interleave_pos(const void *a, const void *b) 1755 { 1756 struct cxl_endpoint_decoder *cxled_a = *(typeof(cxled_a) *)a; 1757 struct cxl_endpoint_decoder *cxled_b = *(typeof(cxled_b) *)b; 1758 1759 return cxled_a->pos - cxled_b->pos; 1760 } 1761 1762 static int match_switch_decoder_by_range(struct device *dev, 1763 const void *data) 1764 { 1765 struct cxl_switch_decoder *cxlsd; 1766 const struct range *r1, *r2 = data; 1767 1768 1769 if (!is_switch_decoder(dev)) 1770 return 0; 1771 1772 cxlsd = to_cxl_switch_decoder(dev); 1773 r1 = &cxlsd->cxld.hpa_range; 1774 1775 if (is_root_decoder(dev)) 1776 return range_contains(r1, r2); 1777 return (r1->start == r2->start && r1->end == r2->end); 1778 } 1779 1780 static int find_pos_and_ways(struct cxl_port *port, struct range *range, 1781 int *pos, int *ways) 1782 { 1783 struct cxl_switch_decoder *cxlsd; 1784 struct cxl_port *parent; 1785 struct device *dev; 1786 int rc = -ENXIO; 1787 1788 parent = parent_port_of(port); 1789 if (!parent) 1790 return rc; 1791 1792 dev = device_find_child(&parent->dev, range, 1793 match_switch_decoder_by_range); 1794 if (!dev) { 1795 dev_err(port->uport_dev, 1796 "failed to find decoder mapping %#llx-%#llx\n", 1797 range->start, range->end); 1798 return rc; 1799 } 1800 cxlsd = to_cxl_switch_decoder(dev); 1801 *ways = cxlsd->cxld.interleave_ways; 1802 1803 for (int i = 0; i < *ways; i++) { 1804 if (cxlsd->target[i] == port->parent_dport) { 1805 *pos = i; 1806 rc = 0; 1807 break; 1808 } 1809 } 1810 put_device(dev); 1811 1812 if (rc) 1813 dev_err(port->uport_dev, 1814 "failed to find %s:%s in target list of %s\n", 1815 dev_name(&port->dev), 1816 dev_name(port->parent_dport->dport_dev), 1817 dev_name(&cxlsd->cxld.dev)); 1818 1819 return rc; 1820 } 1821 1822 /** 1823 * cxl_calc_interleave_pos() - calculate an endpoint position in a region 1824 * @cxled: endpoint decoder member of given region 1825 * 1826 * The endpoint position is calculated by traversing the topology from 1827 * the endpoint to the root decoder and iteratively applying this 1828 * calculation: 1829 * 1830 * position = position * parent_ways + parent_pos; 1831 * 1832 * ...where @position is inferred from switch and root decoder target lists. 1833 * 1834 * Return: position >= 0 on success 1835 * -ENXIO on failure 1836 */ 1837 static int cxl_calc_interleave_pos(struct cxl_endpoint_decoder *cxled) 1838 { 1839 struct cxl_port *iter, *port = cxled_to_port(cxled); 1840 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 1841 struct range *range = &cxled->cxld.hpa_range; 1842 int parent_ways = 0, parent_pos = 0, pos = 0; 1843 int rc; 1844 1845 /* 1846 * Example: the expected interleave order of the 4-way region shown 1847 * below is: mem0, mem2, mem1, mem3 1848 * 1849 * root_port 1850 * / \ 1851 * host_bridge_0 host_bridge_1 1852 * | | | | 1853 * mem0 mem1 mem2 mem3 1854 * 1855 * In the example the calculator will iterate twice. The first iteration 1856 * uses the mem position in the host-bridge and the ways of the host- 1857 * bridge to generate the first, or local, position. The second 1858 * iteration uses the host-bridge position in the root_port and the ways 1859 * of the root_port to refine the position. 1860 * 1861 * A trace of the calculation per endpoint looks like this: 1862 * mem0: pos = 0 * 2 + 0 mem2: pos = 0 * 2 + 0 1863 * pos = 0 * 2 + 0 pos = 0 * 2 + 1 1864 * pos: 0 pos: 1 1865 * 1866 * mem1: pos = 0 * 2 + 1 mem3: pos = 0 * 2 + 1 1867 * pos = 1 * 2 + 0 pos = 1 * 2 + 1 1868 * pos: 2 pos = 3 1869 * 1870 * Note that while this example is simple, the method applies to more 1871 * complex topologies, including those with switches. 1872 */ 1873 1874 /* Iterate from endpoint to root_port refining the position */ 1875 for (iter = port; iter; iter = parent_port_of(iter)) { 1876 if (is_cxl_root(iter)) 1877 break; 1878 1879 rc = find_pos_and_ways(iter, range, &parent_pos, &parent_ways); 1880 if (rc) 1881 return rc; 1882 1883 pos = pos * parent_ways + parent_pos; 1884 } 1885 1886 dev_dbg(&cxlmd->dev, 1887 "decoder:%s parent:%s port:%s range:%#llx-%#llx pos:%d\n", 1888 dev_name(&cxled->cxld.dev), dev_name(cxlmd->dev.parent), 1889 dev_name(&port->dev), range->start, range->end, pos); 1890 1891 return pos; 1892 } 1893 1894 static int cxl_region_sort_targets(struct cxl_region *cxlr) 1895 { 1896 struct cxl_region_params *p = &cxlr->params; 1897 int i, rc = 0; 1898 1899 for (i = 0; i < p->nr_targets; i++) { 1900 struct cxl_endpoint_decoder *cxled = p->targets[i]; 1901 1902 cxled->pos = cxl_calc_interleave_pos(cxled); 1903 /* 1904 * Record that sorting failed, but still continue to calc 1905 * cxled->pos so that follow-on code paths can reliably 1906 * do p->targets[cxled->pos] to self-reference their entry. 1907 */ 1908 if (cxled->pos < 0) 1909 rc = -ENXIO; 1910 } 1911 /* Keep the cxlr target list in interleave position order */ 1912 sort(p->targets, p->nr_targets, sizeof(p->targets[0]), 1913 cmp_interleave_pos, NULL); 1914 1915 dev_dbg(&cxlr->dev, "region sort %s\n", rc ? "failed" : "successful"); 1916 return rc; 1917 } 1918 1919 static int cxl_region_attach(struct cxl_region *cxlr, 1920 struct cxl_endpoint_decoder *cxled, int pos) 1921 { 1922 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent); 1923 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 1924 struct cxl_dev_state *cxlds = cxlmd->cxlds; 1925 struct cxl_region_params *p = &cxlr->params; 1926 struct cxl_port *ep_port, *root_port; 1927 struct cxl_dport *dport; 1928 int rc = -ENXIO; 1929 1930 rc = check_interleave_cap(&cxled->cxld, p->interleave_ways, 1931 p->interleave_granularity); 1932 if (rc) { 1933 dev_dbg(&cxlr->dev, "%s iw: %d ig: %d is not supported\n", 1934 dev_name(&cxled->cxld.dev), p->interleave_ways, 1935 p->interleave_granularity); 1936 return rc; 1937 } 1938 1939 if (cxled->part < 0) { 1940 dev_dbg(&cxlr->dev, "%s dead\n", dev_name(&cxled->cxld.dev)); 1941 return -ENODEV; 1942 } 1943 1944 if (cxlds->part[cxled->part].mode != cxlr->mode) { 1945 dev_dbg(&cxlr->dev, "%s region mode: %d mismatch\n", 1946 dev_name(&cxled->cxld.dev), cxlr->mode); 1947 return -EINVAL; 1948 } 1949 1950 /* all full of members, or interleave config not established? */ 1951 if (p->state > CXL_CONFIG_INTERLEAVE_ACTIVE) { 1952 dev_dbg(&cxlr->dev, "region already active\n"); 1953 return -EBUSY; 1954 } 1955 1956 if (p->state < CXL_CONFIG_INTERLEAVE_ACTIVE) { 1957 dev_dbg(&cxlr->dev, "interleave config missing\n"); 1958 return -ENXIO; 1959 } 1960 1961 if (p->nr_targets >= p->interleave_ways) { 1962 dev_dbg(&cxlr->dev, "region already has %d endpoints\n", 1963 p->nr_targets); 1964 return -EINVAL; 1965 } 1966 1967 ep_port = cxled_to_port(cxled); 1968 root_port = cxlrd_to_port(cxlrd); 1969 dport = cxl_find_dport_by_dev(root_port, ep_port->host_bridge); 1970 if (!dport) { 1971 dev_dbg(&cxlr->dev, "%s:%s invalid target for %s\n", 1972 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), 1973 dev_name(cxlr->dev.parent)); 1974 return -ENXIO; 1975 } 1976 1977 if (cxled->cxld.target_type != cxlr->type) { 1978 dev_dbg(&cxlr->dev, "%s:%s type mismatch: %d vs %d\n", 1979 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), 1980 cxled->cxld.target_type, cxlr->type); 1981 return -ENXIO; 1982 } 1983 1984 if (!cxled->dpa_res) { 1985 dev_dbg(&cxlr->dev, "%s:%s: missing DPA allocation.\n", 1986 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev)); 1987 return -ENXIO; 1988 } 1989 1990 if (resource_size(cxled->dpa_res) * p->interleave_ways + p->cache_size != 1991 resource_size(p->res)) { 1992 dev_dbg(&cxlr->dev, 1993 "%s:%s-size-%#llx * ways-%d + cache-%#llx != region-size-%#llx\n", 1994 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), 1995 (u64)resource_size(cxled->dpa_res), p->interleave_ways, 1996 (u64)p->cache_size, (u64)resource_size(p->res)); 1997 return -EINVAL; 1998 } 1999 2000 cxl_region_perf_data_calculate(cxlr, cxled); 2001 2002 if (test_bit(CXL_REGION_F_AUTO, &cxlr->flags)) { 2003 int i; 2004 2005 rc = cxl_region_attach_auto(cxlr, cxled, pos); 2006 if (rc) 2007 return rc; 2008 2009 /* await more targets to arrive... */ 2010 if (p->nr_targets < p->interleave_ways) 2011 return 0; 2012 2013 /* 2014 * All targets are here, which implies all PCI enumeration that 2015 * affects this region has been completed. Walk the topology to 2016 * sort the devices into their relative region decode position. 2017 */ 2018 rc = cxl_region_sort_targets(cxlr); 2019 if (rc) 2020 return rc; 2021 2022 for (i = 0; i < p->nr_targets; i++) { 2023 cxled = p->targets[i]; 2024 ep_port = cxled_to_port(cxled); 2025 dport = cxl_find_dport_by_dev(root_port, 2026 ep_port->host_bridge); 2027 rc = cxl_region_attach_position(cxlr, cxlrd, cxled, 2028 dport, i); 2029 if (rc) 2030 return rc; 2031 } 2032 2033 rc = cxl_region_setup_targets(cxlr); 2034 if (rc) 2035 return rc; 2036 2037 /* 2038 * If target setup succeeds in the autodiscovery case 2039 * then the region is already committed. 2040 */ 2041 p->state = CXL_CONFIG_COMMIT; 2042 cxl_region_shared_upstream_bandwidth_update(cxlr); 2043 2044 return 0; 2045 } 2046 2047 rc = cxl_region_validate_position(cxlr, cxled, pos); 2048 if (rc) 2049 return rc; 2050 2051 rc = cxl_region_attach_position(cxlr, cxlrd, cxled, dport, pos); 2052 if (rc) 2053 return rc; 2054 2055 p->targets[pos] = cxled; 2056 cxled->pos = pos; 2057 p->nr_targets++; 2058 2059 if (p->nr_targets == p->interleave_ways) { 2060 rc = cxl_region_setup_targets(cxlr); 2061 if (rc) 2062 return rc; 2063 p->state = CXL_CONFIG_ACTIVE; 2064 cxl_region_shared_upstream_bandwidth_update(cxlr); 2065 } 2066 2067 cxled->cxld.interleave_ways = p->interleave_ways; 2068 cxled->cxld.interleave_granularity = p->interleave_granularity; 2069 cxled->cxld.hpa_range = (struct range) { 2070 .start = p->res->start, 2071 .end = p->res->end, 2072 }; 2073 2074 if (p->nr_targets != p->interleave_ways) 2075 return 0; 2076 2077 /* 2078 * Test the auto-discovery position calculator function 2079 * against this successfully created user-defined region. 2080 * A fail message here means that this interleave config 2081 * will fail when presented as CXL_REGION_F_AUTO. 2082 */ 2083 for (int i = 0; i < p->nr_targets; i++) { 2084 struct cxl_endpoint_decoder *cxled = p->targets[i]; 2085 int test_pos; 2086 2087 test_pos = cxl_calc_interleave_pos(cxled); 2088 dev_dbg(&cxled->cxld.dev, 2089 "Test cxl_calc_interleave_pos(): %s test_pos:%d cxled->pos:%d\n", 2090 (test_pos == cxled->pos) ? "success" : "fail", 2091 test_pos, cxled->pos); 2092 } 2093 2094 return 0; 2095 } 2096 2097 static struct cxl_region * 2098 __cxl_decoder_detach(struct cxl_region *cxlr, 2099 struct cxl_endpoint_decoder *cxled, int pos, 2100 enum cxl_detach_mode mode) 2101 { 2102 struct cxl_region_params *p; 2103 2104 lockdep_assert_held_write(&cxl_rwsem.region); 2105 2106 if (!cxled) { 2107 p = &cxlr->params; 2108 2109 if (pos >= p->interleave_ways) { 2110 dev_dbg(&cxlr->dev, "position %d out of range %d\n", 2111 pos, p->interleave_ways); 2112 return NULL; 2113 } 2114 2115 if (!p->targets[pos]) 2116 return NULL; 2117 cxled = p->targets[pos]; 2118 } else { 2119 cxlr = cxled->cxld.region; 2120 if (!cxlr) 2121 return NULL; 2122 p = &cxlr->params; 2123 } 2124 2125 if (mode == DETACH_INVALIDATE) 2126 cxled->part = -1; 2127 2128 if (p->state > CXL_CONFIG_ACTIVE) { 2129 cxl_region_decode_reset(cxlr, p->interleave_ways); 2130 p->state = CXL_CONFIG_ACTIVE; 2131 } 2132 2133 for (struct cxl_port *iter = cxled_to_port(cxled); !is_cxl_root(iter); 2134 iter = to_cxl_port(iter->dev.parent)) 2135 cxl_port_detach_region(iter, cxlr, cxled); 2136 2137 if (cxled->pos < 0 || cxled->pos >= p->interleave_ways || 2138 p->targets[cxled->pos] != cxled) { 2139 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 2140 2141 dev_WARN_ONCE(&cxlr->dev, 1, "expected %s:%s at position %d\n", 2142 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), 2143 cxled->pos); 2144 return NULL; 2145 } 2146 2147 if (p->state == CXL_CONFIG_ACTIVE) { 2148 p->state = CXL_CONFIG_INTERLEAVE_ACTIVE; 2149 cxl_region_teardown_targets(cxlr); 2150 } 2151 p->targets[cxled->pos] = NULL; 2152 p->nr_targets--; 2153 cxled->cxld.hpa_range = (struct range) { 2154 .start = 0, 2155 .end = -1, 2156 }; 2157 2158 get_device(&cxlr->dev); 2159 return cxlr; 2160 } 2161 2162 /* 2163 * Cleanup a decoder's interest in a region. There are 2 cases to 2164 * handle, removing an unknown @cxled from a known position in a region 2165 * (detach_target()) or removing a known @cxled from an unknown @cxlr 2166 * (cxld_unregister()) 2167 * 2168 * When the detachment finds a region release the region driver. 2169 */ 2170 int cxl_decoder_detach(struct cxl_region *cxlr, 2171 struct cxl_endpoint_decoder *cxled, int pos, 2172 enum cxl_detach_mode mode) 2173 { 2174 struct cxl_region *detach; 2175 2176 /* when the decoder is being destroyed lock unconditionally */ 2177 if (mode == DETACH_INVALIDATE) { 2178 guard(rwsem_write)(&cxl_rwsem.region); 2179 detach = __cxl_decoder_detach(cxlr, cxled, pos, mode); 2180 } else { 2181 int rc; 2182 2183 ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region); 2184 if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem))) 2185 return rc; 2186 detach = __cxl_decoder_detach(cxlr, cxled, pos, mode); 2187 } 2188 2189 if (detach) { 2190 device_release_driver(&detach->dev); 2191 put_device(&detach->dev); 2192 } 2193 return 0; 2194 } 2195 2196 static int __attach_target(struct cxl_region *cxlr, 2197 struct cxl_endpoint_decoder *cxled, int pos, 2198 unsigned int state) 2199 { 2200 int rc; 2201 2202 if (state == TASK_INTERRUPTIBLE) { 2203 ACQUIRE(rwsem_write_kill, rwsem)(&cxl_rwsem.region); 2204 if ((rc = ACQUIRE_ERR(rwsem_write_kill, &rwsem))) 2205 return rc; 2206 guard(rwsem_read)(&cxl_rwsem.dpa); 2207 return cxl_region_attach(cxlr, cxled, pos); 2208 } 2209 guard(rwsem_write)(&cxl_rwsem.region); 2210 guard(rwsem_read)(&cxl_rwsem.dpa); 2211 return cxl_region_attach(cxlr, cxled, pos); 2212 } 2213 2214 static int attach_target(struct cxl_region *cxlr, 2215 struct cxl_endpoint_decoder *cxled, int pos, 2216 unsigned int state) 2217 { 2218 int rc = __attach_target(cxlr, cxled, pos, state); 2219 2220 if (rc == 0) 2221 return 0; 2222 2223 dev_warn(cxled->cxld.dev.parent, "failed to attach %s to %s: %d\n", 2224 dev_name(&cxled->cxld.dev), dev_name(&cxlr->dev), rc); 2225 return rc; 2226 } 2227 2228 static int detach_target(struct cxl_region *cxlr, int pos) 2229 { 2230 return cxl_decoder_detach(cxlr, NULL, pos, DETACH_ONLY); 2231 } 2232 2233 static size_t store_targetN(struct cxl_region *cxlr, const char *buf, int pos, 2234 size_t len) 2235 { 2236 int rc; 2237 2238 if (sysfs_streq(buf, "\n")) 2239 rc = detach_target(cxlr, pos); 2240 else { 2241 struct device *dev; 2242 2243 dev = bus_find_device_by_name(&cxl_bus_type, NULL, buf); 2244 if (!dev) 2245 return -ENODEV; 2246 2247 if (!is_endpoint_decoder(dev)) { 2248 rc = -EINVAL; 2249 goto out; 2250 } 2251 2252 rc = attach_target(cxlr, to_cxl_endpoint_decoder(dev), pos, 2253 TASK_INTERRUPTIBLE); 2254 out: 2255 put_device(dev); 2256 } 2257 2258 if (rc < 0) 2259 return rc; 2260 return len; 2261 } 2262 2263 #define TARGET_ATTR_RW(n) \ 2264 static ssize_t target##n##_show( \ 2265 struct device *dev, struct device_attribute *attr, char *buf) \ 2266 { \ 2267 return show_targetN(to_cxl_region(dev), buf, (n)); \ 2268 } \ 2269 static ssize_t target##n##_store(struct device *dev, \ 2270 struct device_attribute *attr, \ 2271 const char *buf, size_t len) \ 2272 { \ 2273 return store_targetN(to_cxl_region(dev), buf, (n), len); \ 2274 } \ 2275 static DEVICE_ATTR_RW(target##n) 2276 2277 TARGET_ATTR_RW(0); 2278 TARGET_ATTR_RW(1); 2279 TARGET_ATTR_RW(2); 2280 TARGET_ATTR_RW(3); 2281 TARGET_ATTR_RW(4); 2282 TARGET_ATTR_RW(5); 2283 TARGET_ATTR_RW(6); 2284 TARGET_ATTR_RW(7); 2285 TARGET_ATTR_RW(8); 2286 TARGET_ATTR_RW(9); 2287 TARGET_ATTR_RW(10); 2288 TARGET_ATTR_RW(11); 2289 TARGET_ATTR_RW(12); 2290 TARGET_ATTR_RW(13); 2291 TARGET_ATTR_RW(14); 2292 TARGET_ATTR_RW(15); 2293 2294 static struct attribute *target_attrs[] = { 2295 &dev_attr_target0.attr, 2296 &dev_attr_target1.attr, 2297 &dev_attr_target2.attr, 2298 &dev_attr_target3.attr, 2299 &dev_attr_target4.attr, 2300 &dev_attr_target5.attr, 2301 &dev_attr_target6.attr, 2302 &dev_attr_target7.attr, 2303 &dev_attr_target8.attr, 2304 &dev_attr_target9.attr, 2305 &dev_attr_target10.attr, 2306 &dev_attr_target11.attr, 2307 &dev_attr_target12.attr, 2308 &dev_attr_target13.attr, 2309 &dev_attr_target14.attr, 2310 &dev_attr_target15.attr, 2311 NULL, 2312 }; 2313 2314 static umode_t cxl_region_target_visible(struct kobject *kobj, 2315 struct attribute *a, int n) 2316 { 2317 struct device *dev = kobj_to_dev(kobj); 2318 struct cxl_region *cxlr = to_cxl_region(dev); 2319 struct cxl_region_params *p = &cxlr->params; 2320 2321 if (n < p->interleave_ways) 2322 return a->mode; 2323 return 0; 2324 } 2325 2326 static const struct attribute_group cxl_region_target_group = { 2327 .attrs = target_attrs, 2328 .is_visible = cxl_region_target_visible, 2329 }; 2330 2331 static const struct attribute_group *get_cxl_region_target_group(void) 2332 { 2333 return &cxl_region_target_group; 2334 } 2335 2336 static const struct attribute_group *region_groups[] = { 2337 &cxl_base_attribute_group, 2338 &cxl_region_group, 2339 &cxl_region_target_group, 2340 &cxl_region_access0_coordinate_group, 2341 &cxl_region_access1_coordinate_group, 2342 NULL, 2343 }; 2344 2345 static void cxl_region_release(struct device *dev) 2346 { 2347 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev->parent); 2348 struct cxl_region *cxlr = to_cxl_region(dev); 2349 int id = atomic_read(&cxlrd->region_id); 2350 2351 /* 2352 * Try to reuse the recently idled id rather than the cached 2353 * next id to prevent the region id space from increasing 2354 * unnecessarily. 2355 */ 2356 if (cxlr->id < id) 2357 if (atomic_try_cmpxchg(&cxlrd->region_id, &id, cxlr->id)) { 2358 memregion_free(id); 2359 goto out; 2360 } 2361 2362 memregion_free(cxlr->id); 2363 out: 2364 put_device(dev->parent); 2365 kfree(cxlr); 2366 } 2367 2368 const struct device_type cxl_region_type = { 2369 .name = "cxl_region", 2370 .release = cxl_region_release, 2371 .groups = region_groups 2372 }; 2373 2374 bool is_cxl_region(struct device *dev) 2375 { 2376 return dev->type == &cxl_region_type; 2377 } 2378 EXPORT_SYMBOL_NS_GPL(is_cxl_region, "CXL"); 2379 2380 static struct cxl_region *to_cxl_region(struct device *dev) 2381 { 2382 if (dev_WARN_ONCE(dev, dev->type != &cxl_region_type, 2383 "not a cxl_region device\n")) 2384 return NULL; 2385 2386 return container_of(dev, struct cxl_region, dev); 2387 } 2388 2389 static void unregister_region(void *_cxlr) 2390 { 2391 struct cxl_region *cxlr = _cxlr; 2392 struct cxl_region_params *p = &cxlr->params; 2393 int i; 2394 2395 device_del(&cxlr->dev); 2396 2397 /* 2398 * Now that region sysfs is shutdown, the parameter block is now 2399 * read-only, so no need to hold the region rwsem to access the 2400 * region parameters. 2401 */ 2402 for (i = 0; i < p->interleave_ways; i++) 2403 detach_target(cxlr, i); 2404 2405 cxl_region_iomem_release(cxlr); 2406 put_device(&cxlr->dev); 2407 } 2408 2409 static struct lock_class_key cxl_region_key; 2410 2411 static struct cxl_region *cxl_region_alloc(struct cxl_root_decoder *cxlrd, int id) 2412 { 2413 struct cxl_region *cxlr; 2414 struct device *dev; 2415 2416 cxlr = kzalloc(sizeof(*cxlr), GFP_KERNEL); 2417 if (!cxlr) { 2418 memregion_free(id); 2419 return ERR_PTR(-ENOMEM); 2420 } 2421 2422 dev = &cxlr->dev; 2423 device_initialize(dev); 2424 lockdep_set_class(&dev->mutex, &cxl_region_key); 2425 dev->parent = &cxlrd->cxlsd.cxld.dev; 2426 /* 2427 * Keep root decoder pinned through cxl_region_release to fixup 2428 * region id allocations 2429 */ 2430 get_device(dev->parent); 2431 device_set_pm_not_required(dev); 2432 dev->bus = &cxl_bus_type; 2433 dev->type = &cxl_region_type; 2434 cxlr->id = id; 2435 2436 return cxlr; 2437 } 2438 2439 static bool cxl_region_update_coordinates(struct cxl_region *cxlr, int nid) 2440 { 2441 int cset = 0; 2442 int rc; 2443 2444 for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) { 2445 if (cxlr->coord[i].read_bandwidth) { 2446 rc = 0; 2447 if (cxl_need_node_perf_attrs_update(nid)) 2448 node_set_perf_attrs(nid, &cxlr->coord[i], i); 2449 else 2450 rc = cxl_update_hmat_access_coordinates(nid, cxlr, i); 2451 2452 if (rc == 0) 2453 cset++; 2454 } 2455 } 2456 2457 if (!cset) 2458 return false; 2459 2460 rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_access0_group()); 2461 if (rc) 2462 dev_dbg(&cxlr->dev, "Failed to update access0 group\n"); 2463 2464 rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_access1_group()); 2465 if (rc) 2466 dev_dbg(&cxlr->dev, "Failed to update access1 group\n"); 2467 2468 return true; 2469 } 2470 2471 static int cxl_region_perf_attrs_callback(struct notifier_block *nb, 2472 unsigned long action, void *arg) 2473 { 2474 struct cxl_region *cxlr = container_of(nb, struct cxl_region, 2475 node_notifier); 2476 struct node_notify *nn = arg; 2477 int nid = nn->nid; 2478 int region_nid; 2479 2480 if (action != NODE_ADDED_FIRST_MEMORY) 2481 return NOTIFY_DONE; 2482 2483 /* 2484 * No need to hold cxl_rwsem.region; region parameters are stable 2485 * within the cxl_region driver. 2486 */ 2487 region_nid = phys_to_target_node(cxlr->params.res->start); 2488 if (nid != region_nid) 2489 return NOTIFY_DONE; 2490 2491 if (!cxl_region_update_coordinates(cxlr, nid)) 2492 return NOTIFY_DONE; 2493 2494 return NOTIFY_OK; 2495 } 2496 2497 static int cxl_region_calculate_adistance(struct notifier_block *nb, 2498 unsigned long nid, void *data) 2499 { 2500 struct cxl_region *cxlr = container_of(nb, struct cxl_region, 2501 adist_notifier); 2502 struct access_coordinate *perf; 2503 int *adist = data; 2504 int region_nid; 2505 2506 /* 2507 * No need to hold cxl_rwsem.region; region parameters are stable 2508 * within the cxl_region driver. 2509 */ 2510 region_nid = phys_to_target_node(cxlr->params.res->start); 2511 if (nid != region_nid) 2512 return NOTIFY_OK; 2513 2514 perf = &cxlr->coord[ACCESS_COORDINATE_CPU]; 2515 2516 if (mt_perf_to_adistance(perf, adist)) 2517 return NOTIFY_OK; 2518 2519 return NOTIFY_STOP; 2520 } 2521 2522 /** 2523 * devm_cxl_add_region - Adds a region to a decoder 2524 * @cxlrd: root decoder 2525 * @id: memregion id to create, or memregion_free() on failure 2526 * @mode: mode for the endpoint decoders of this region 2527 * @type: select whether this is an expander or accelerator (type-2 or type-3) 2528 * 2529 * This is the second step of region initialization. Regions exist within an 2530 * address space which is mapped by a @cxlrd. 2531 * 2532 * Return: 0 if the region was added to the @cxlrd, else returns negative error 2533 * code. The region will be named "regionZ" where Z is the unique region number. 2534 */ 2535 static struct cxl_region *devm_cxl_add_region(struct cxl_root_decoder *cxlrd, 2536 int id, 2537 enum cxl_partition_mode mode, 2538 enum cxl_decoder_type type) 2539 { 2540 struct cxl_port *port = to_cxl_port(cxlrd->cxlsd.cxld.dev.parent); 2541 struct cxl_region *cxlr; 2542 struct device *dev; 2543 int rc; 2544 2545 cxlr = cxl_region_alloc(cxlrd, id); 2546 if (IS_ERR(cxlr)) 2547 return cxlr; 2548 cxlr->mode = mode; 2549 cxlr->type = type; 2550 2551 dev = &cxlr->dev; 2552 rc = dev_set_name(dev, "region%d", id); 2553 if (rc) 2554 goto err; 2555 2556 rc = device_add(dev); 2557 if (rc) 2558 goto err; 2559 2560 rc = devm_add_action_or_reset(port->uport_dev, unregister_region, cxlr); 2561 if (rc) 2562 return ERR_PTR(rc); 2563 2564 dev_dbg(port->uport_dev, "%s: created %s\n", 2565 dev_name(&cxlrd->cxlsd.cxld.dev), dev_name(dev)); 2566 return cxlr; 2567 2568 err: 2569 put_device(dev); 2570 return ERR_PTR(rc); 2571 } 2572 2573 static ssize_t __create_region_show(struct cxl_root_decoder *cxlrd, char *buf) 2574 { 2575 return sysfs_emit(buf, "region%u\n", atomic_read(&cxlrd->region_id)); 2576 } 2577 2578 static ssize_t create_pmem_region_show(struct device *dev, 2579 struct device_attribute *attr, char *buf) 2580 { 2581 return __create_region_show(to_cxl_root_decoder(dev), buf); 2582 } 2583 2584 static ssize_t create_ram_region_show(struct device *dev, 2585 struct device_attribute *attr, char *buf) 2586 { 2587 return __create_region_show(to_cxl_root_decoder(dev), buf); 2588 } 2589 2590 static struct cxl_region *__create_region(struct cxl_root_decoder *cxlrd, 2591 enum cxl_partition_mode mode, int id) 2592 { 2593 int rc; 2594 2595 switch (mode) { 2596 case CXL_PARTMODE_RAM: 2597 case CXL_PARTMODE_PMEM: 2598 break; 2599 default: 2600 dev_err(&cxlrd->cxlsd.cxld.dev, "unsupported mode %d\n", mode); 2601 return ERR_PTR(-EINVAL); 2602 } 2603 2604 rc = memregion_alloc(GFP_KERNEL); 2605 if (rc < 0) 2606 return ERR_PTR(rc); 2607 2608 if (atomic_cmpxchg(&cxlrd->region_id, id, rc) != id) { 2609 memregion_free(rc); 2610 return ERR_PTR(-EBUSY); 2611 } 2612 2613 return devm_cxl_add_region(cxlrd, id, mode, CXL_DECODER_HOSTONLYMEM); 2614 } 2615 2616 static ssize_t create_region_store(struct device *dev, const char *buf, 2617 size_t len, enum cxl_partition_mode mode) 2618 { 2619 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev); 2620 struct cxl_region *cxlr; 2621 int rc, id; 2622 2623 rc = sscanf(buf, "region%d\n", &id); 2624 if (rc != 1) 2625 return -EINVAL; 2626 2627 cxlr = __create_region(cxlrd, mode, id); 2628 if (IS_ERR(cxlr)) 2629 return PTR_ERR(cxlr); 2630 2631 return len; 2632 } 2633 2634 static ssize_t create_pmem_region_store(struct device *dev, 2635 struct device_attribute *attr, 2636 const char *buf, size_t len) 2637 { 2638 return create_region_store(dev, buf, len, CXL_PARTMODE_PMEM); 2639 } 2640 DEVICE_ATTR_RW(create_pmem_region); 2641 2642 static ssize_t create_ram_region_store(struct device *dev, 2643 struct device_attribute *attr, 2644 const char *buf, size_t len) 2645 { 2646 return create_region_store(dev, buf, len, CXL_PARTMODE_RAM); 2647 } 2648 DEVICE_ATTR_RW(create_ram_region); 2649 2650 static ssize_t region_show(struct device *dev, struct device_attribute *attr, 2651 char *buf) 2652 { 2653 struct cxl_decoder *cxld = to_cxl_decoder(dev); 2654 ssize_t rc; 2655 2656 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region); 2657 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem))) 2658 return rc; 2659 2660 if (cxld->region) 2661 return sysfs_emit(buf, "%s\n", dev_name(&cxld->region->dev)); 2662 return sysfs_emit(buf, "\n"); 2663 } 2664 DEVICE_ATTR_RO(region); 2665 2666 static struct cxl_region * 2667 cxl_find_region_by_name(struct cxl_root_decoder *cxlrd, const char *name) 2668 { 2669 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld; 2670 struct device *region_dev; 2671 2672 region_dev = device_find_child_by_name(&cxld->dev, name); 2673 if (!region_dev) 2674 return ERR_PTR(-ENODEV); 2675 2676 return to_cxl_region(region_dev); 2677 } 2678 2679 static ssize_t delete_region_store(struct device *dev, 2680 struct device_attribute *attr, 2681 const char *buf, size_t len) 2682 { 2683 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev); 2684 struct cxl_port *port = to_cxl_port(dev->parent); 2685 struct cxl_region *cxlr; 2686 2687 cxlr = cxl_find_region_by_name(cxlrd, buf); 2688 if (IS_ERR(cxlr)) 2689 return PTR_ERR(cxlr); 2690 2691 devm_release_action(port->uport_dev, unregister_region, cxlr); 2692 put_device(&cxlr->dev); 2693 2694 return len; 2695 } 2696 DEVICE_ATTR_WO(delete_region); 2697 2698 static void cxl_pmem_region_release(struct device *dev) 2699 { 2700 struct cxl_pmem_region *cxlr_pmem = to_cxl_pmem_region(dev); 2701 int i; 2702 2703 for (i = 0; i < cxlr_pmem->nr_mappings; i++) { 2704 struct cxl_memdev *cxlmd = cxlr_pmem->mapping[i].cxlmd; 2705 2706 put_device(&cxlmd->dev); 2707 } 2708 2709 kfree(cxlr_pmem); 2710 } 2711 2712 static const struct attribute_group *cxl_pmem_region_attribute_groups[] = { 2713 &cxl_base_attribute_group, 2714 NULL, 2715 }; 2716 2717 const struct device_type cxl_pmem_region_type = { 2718 .name = "cxl_pmem_region", 2719 .release = cxl_pmem_region_release, 2720 .groups = cxl_pmem_region_attribute_groups, 2721 }; 2722 2723 bool is_cxl_pmem_region(struct device *dev) 2724 { 2725 return dev->type == &cxl_pmem_region_type; 2726 } 2727 EXPORT_SYMBOL_NS_GPL(is_cxl_pmem_region, "CXL"); 2728 2729 struct cxl_pmem_region *to_cxl_pmem_region(struct device *dev) 2730 { 2731 if (dev_WARN_ONCE(dev, !is_cxl_pmem_region(dev), 2732 "not a cxl_pmem_region device\n")) 2733 return NULL; 2734 return container_of(dev, struct cxl_pmem_region, dev); 2735 } 2736 EXPORT_SYMBOL_NS_GPL(to_cxl_pmem_region, "CXL"); 2737 2738 struct cxl_poison_context { 2739 struct cxl_port *port; 2740 int part; 2741 u64 offset; 2742 }; 2743 2744 static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd, 2745 struct cxl_poison_context *ctx) 2746 { 2747 struct cxl_dev_state *cxlds = cxlmd->cxlds; 2748 const struct resource *res; 2749 struct resource *p, *last; 2750 u64 offset, length; 2751 int rc = 0; 2752 2753 if (ctx->part < 0) 2754 return 0; 2755 2756 /* 2757 * Collect poison for the remaining unmapped resources after 2758 * poison is collected by committed endpoints decoders. 2759 */ 2760 for (int i = ctx->part; i < cxlds->nr_partitions; i++) { 2761 res = &cxlds->part[i].res; 2762 for (p = res->child, last = NULL; p; p = p->sibling) 2763 last = p; 2764 if (last) 2765 offset = last->end + 1; 2766 else 2767 offset = res->start; 2768 length = res->end - offset + 1; 2769 if (!length) 2770 break; 2771 rc = cxl_mem_get_poison(cxlmd, offset, length, NULL); 2772 if (rc == -EFAULT && cxlds->part[i].mode == CXL_PARTMODE_RAM) 2773 continue; 2774 if (rc) 2775 break; 2776 } 2777 2778 return rc; 2779 } 2780 2781 static int poison_by_decoder(struct device *dev, void *arg) 2782 { 2783 struct cxl_poison_context *ctx = arg; 2784 struct cxl_endpoint_decoder *cxled; 2785 enum cxl_partition_mode mode; 2786 struct cxl_dev_state *cxlds; 2787 struct cxl_memdev *cxlmd; 2788 u64 offset, length; 2789 int rc = 0; 2790 2791 if (!is_endpoint_decoder(dev)) 2792 return rc; 2793 2794 cxled = to_cxl_endpoint_decoder(dev); 2795 if (!cxled->dpa_res) 2796 return rc; 2797 2798 cxlmd = cxled_to_memdev(cxled); 2799 cxlds = cxlmd->cxlds; 2800 mode = cxlds->part[cxled->part].mode; 2801 2802 if (cxled->skip) { 2803 offset = cxled->dpa_res->start - cxled->skip; 2804 length = cxled->skip; 2805 rc = cxl_mem_get_poison(cxlmd, offset, length, NULL); 2806 if (rc == -EFAULT && mode == CXL_PARTMODE_RAM) 2807 rc = 0; 2808 if (rc) 2809 return rc; 2810 } 2811 2812 offset = cxled->dpa_res->start; 2813 length = cxled->dpa_res->end - offset + 1; 2814 rc = cxl_mem_get_poison(cxlmd, offset, length, cxled->cxld.region); 2815 if (rc == -EFAULT && mode == CXL_PARTMODE_RAM) 2816 rc = 0; 2817 if (rc) 2818 return rc; 2819 2820 /* Iterate until commit_end is reached */ 2821 if (cxled->cxld.id == ctx->port->commit_end) { 2822 ctx->offset = cxled->dpa_res->end + 1; 2823 ctx->part = cxled->part; 2824 return 1; 2825 } 2826 2827 return 0; 2828 } 2829 2830 int cxl_get_poison_by_endpoint(struct cxl_port *port) 2831 { 2832 struct cxl_poison_context ctx; 2833 int rc = 0; 2834 2835 ctx = (struct cxl_poison_context) { 2836 .port = port, 2837 .part = -1, 2838 }; 2839 2840 rc = device_for_each_child(&port->dev, &ctx, poison_by_decoder); 2841 if (rc == 1) 2842 rc = cxl_get_poison_unmapped(to_cxl_memdev(port->uport_dev), 2843 &ctx); 2844 2845 return rc; 2846 } 2847 2848 struct cxl_dpa_to_region_context { 2849 struct cxl_region *cxlr; 2850 u64 dpa; 2851 }; 2852 2853 static int __cxl_dpa_to_region(struct device *dev, void *arg) 2854 { 2855 struct cxl_dpa_to_region_context *ctx = arg; 2856 struct cxl_endpoint_decoder *cxled; 2857 struct cxl_region *cxlr; 2858 u64 dpa = ctx->dpa; 2859 2860 if (!is_endpoint_decoder(dev)) 2861 return 0; 2862 2863 cxled = to_cxl_endpoint_decoder(dev); 2864 if (!cxled || !cxled->dpa_res || !resource_size(cxled->dpa_res)) 2865 return 0; 2866 2867 if (!cxl_resource_contains_addr(cxled->dpa_res, dpa)) 2868 return 0; 2869 2870 /* 2871 * Stop the region search (return 1) when an endpoint mapping is 2872 * found. The region may not be fully constructed so offering 2873 * the cxlr in the context structure is not guaranteed. 2874 */ 2875 cxlr = cxled->cxld.region; 2876 if (cxlr) 2877 dev_dbg(dev, "dpa:0x%llx mapped in region:%s\n", dpa, 2878 dev_name(&cxlr->dev)); 2879 else 2880 dev_dbg(dev, "dpa:0x%llx mapped in endpoint:%s\n", dpa, 2881 dev_name(dev)); 2882 2883 ctx->cxlr = cxlr; 2884 2885 return 1; 2886 } 2887 2888 struct cxl_region *cxl_dpa_to_region(const struct cxl_memdev *cxlmd, u64 dpa) 2889 { 2890 struct cxl_dpa_to_region_context ctx; 2891 struct cxl_port *port; 2892 2893 ctx = (struct cxl_dpa_to_region_context) { 2894 .dpa = dpa, 2895 }; 2896 port = cxlmd->endpoint; 2897 if (port && is_cxl_endpoint(port) && cxl_num_decoders_committed(port)) 2898 device_for_each_child(&port->dev, &ctx, __cxl_dpa_to_region); 2899 2900 return ctx.cxlr; 2901 } 2902 2903 static bool cxl_is_hpa_in_chunk(u64 hpa, struct cxl_region *cxlr, int pos) 2904 { 2905 struct cxl_region_params *p = &cxlr->params; 2906 int gran = p->interleave_granularity; 2907 int ways = p->interleave_ways; 2908 u64 offset; 2909 2910 /* Is the hpa in an expected chunk for its pos(-ition) */ 2911 offset = hpa - p->res->start; 2912 offset = do_div(offset, gran * ways); 2913 if ((offset >= pos * gran) && (offset < (pos + 1) * gran)) 2914 return true; 2915 2916 dev_dbg(&cxlr->dev, 2917 "Addr trans fail: hpa 0x%llx not in expected chunk\n", hpa); 2918 2919 return false; 2920 } 2921 2922 static bool has_hpa_to_spa(struct cxl_root_decoder *cxlrd) 2923 { 2924 return cxlrd->ops && cxlrd->ops->hpa_to_spa; 2925 } 2926 2927 static bool has_spa_to_hpa(struct cxl_root_decoder *cxlrd) 2928 { 2929 return cxlrd->ops && cxlrd->ops->spa_to_hpa; 2930 } 2931 2932 u64 cxl_dpa_to_hpa(struct cxl_region *cxlr, const struct cxl_memdev *cxlmd, 2933 u64 dpa) 2934 { 2935 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent); 2936 u64 dpa_offset, hpa_offset, bits_upper, mask_upper, hpa; 2937 struct cxl_region_params *p = &cxlr->params; 2938 struct cxl_endpoint_decoder *cxled = NULL; 2939 u16 eig = 0; 2940 u8 eiw = 0; 2941 int pos; 2942 2943 for (int i = 0; i < p->nr_targets; i++) { 2944 cxled = p->targets[i]; 2945 if (cxlmd == cxled_to_memdev(cxled)) 2946 break; 2947 } 2948 if (!cxled || cxlmd != cxled_to_memdev(cxled)) 2949 return ULLONG_MAX; 2950 2951 pos = cxled->pos; 2952 ways_to_eiw(p->interleave_ways, &eiw); 2953 granularity_to_eig(p->interleave_granularity, &eig); 2954 2955 /* 2956 * The device position in the region interleave set was removed 2957 * from the offset at HPA->DPA translation. To reconstruct the 2958 * HPA, place the 'pos' in the offset. 2959 * 2960 * The placement of 'pos' in the HPA is determined by interleave 2961 * ways and granularity and is defined in the CXL Spec 3.0 Section 2962 * 8.2.4.19.13 Implementation Note: Device Decode Logic 2963 */ 2964 2965 /* Remove the dpa base */ 2966 dpa_offset = dpa - cxl_dpa_resource_start(cxled); 2967 2968 mask_upper = GENMASK_ULL(51, eig + 8); 2969 2970 if (eiw < 8) { 2971 hpa_offset = (dpa_offset & mask_upper) << eiw; 2972 hpa_offset |= pos << (eig + 8); 2973 } else { 2974 bits_upper = (dpa_offset & mask_upper) >> (eig + 8); 2975 bits_upper = bits_upper * 3; 2976 hpa_offset = ((bits_upper << (eiw - 8)) + pos) << (eig + 8); 2977 } 2978 2979 /* The lower bits remain unchanged */ 2980 hpa_offset |= dpa_offset & GENMASK_ULL(eig + 7, 0); 2981 2982 /* Apply the hpa_offset to the region base address */ 2983 hpa = hpa_offset + p->res->start + p->cache_size; 2984 2985 /* Root decoder translation overrides typical modulo decode */ 2986 if (has_hpa_to_spa(cxlrd)) 2987 hpa = cxlrd->ops->hpa_to_spa(cxlrd, hpa); 2988 2989 if (!cxl_resource_contains_addr(p->res, hpa)) { 2990 dev_dbg(&cxlr->dev, 2991 "Addr trans fail: hpa 0x%llx not in region\n", hpa); 2992 return ULLONG_MAX; 2993 } 2994 2995 /* Simple chunk check, by pos & gran, only applies to modulo decodes */ 2996 if (!has_hpa_to_spa(cxlrd) && (!cxl_is_hpa_in_chunk(hpa, cxlr, pos))) 2997 return ULLONG_MAX; 2998 2999 return hpa; 3000 } 3001 3002 struct dpa_result { 3003 struct cxl_memdev *cxlmd; 3004 u64 dpa; 3005 }; 3006 3007 static int region_offset_to_dpa_result(struct cxl_region *cxlr, u64 offset, 3008 struct dpa_result *result) 3009 { 3010 struct cxl_region_params *p = &cxlr->params; 3011 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent); 3012 struct cxl_endpoint_decoder *cxled; 3013 u64 hpa, hpa_offset, dpa_offset; 3014 u64 bits_upper, bits_lower; 3015 u64 shifted, rem, temp; 3016 u16 eig = 0; 3017 u8 eiw = 0; 3018 int pos; 3019 3020 lockdep_assert_held(&cxl_rwsem.region); 3021 lockdep_assert_held(&cxl_rwsem.dpa); 3022 3023 /* Input validation ensures valid ways and gran */ 3024 granularity_to_eig(p->interleave_granularity, &eig); 3025 ways_to_eiw(p->interleave_ways, &eiw); 3026 3027 /* 3028 * If the root decoder has SPA to CXL HPA callback, use it. Otherwise 3029 * CXL HPA is assumed to equal SPA. 3030 */ 3031 if (has_spa_to_hpa(cxlrd)) { 3032 hpa = cxlrd->ops->spa_to_hpa(cxlrd, p->res->start + offset); 3033 hpa_offset = hpa - p->res->start; 3034 } else { 3035 hpa_offset = offset; 3036 } 3037 /* 3038 * Interleave position: CXL Spec 3.2 Section 8.2.4.20.13 3039 * eiw < 8 3040 * Position is in the IW bits at HPA_OFFSET[IG+8+IW-1:IG+8]. 3041 * Per spec "remove IW bits starting with bit position IG+8" 3042 * eiw >= 8 3043 * Position is not explicitly stored in HPA_OFFSET bits. It is 3044 * derived from the modulo operation of the upper bits using 3045 * the total number of interleave ways. 3046 */ 3047 if (eiw < 8) { 3048 pos = (hpa_offset >> (eig + 8)) & GENMASK(eiw - 1, 0); 3049 } else { 3050 shifted = hpa_offset >> (eig + 8); 3051 div64_u64_rem(shifted, p->interleave_ways, &rem); 3052 pos = rem; 3053 } 3054 if (pos < 0 || pos >= p->nr_targets) { 3055 dev_dbg(&cxlr->dev, "Invalid position %d for %d targets\n", 3056 pos, p->nr_targets); 3057 return -ENXIO; 3058 } 3059 3060 /* 3061 * DPA offset: CXL Spec 3.2 Section 8.2.4.20.13 3062 * Lower bits [IG+7:0] pass through unchanged 3063 * (eiw < 8) 3064 * Per spec: DPAOffset[51:IG+8] = (HPAOffset[51:IG+IW+8] >> IW) 3065 * Clear the position bits to isolate upper section, then 3066 * reverse the left shift by eiw that occurred during DPA->HPA 3067 * (eiw >= 8) 3068 * Per spec: DPAOffset[51:IG+8] = HPAOffset[51:IG+IW] / 3 3069 * Extract upper bits from the correct bit range and divide by 3 3070 * to recover the original DPA upper bits 3071 */ 3072 bits_lower = hpa_offset & GENMASK_ULL(eig + 7, 0); 3073 if (eiw < 8) { 3074 temp = hpa_offset &= ~((u64)GENMASK(eig + eiw + 8 - 1, 0)); 3075 dpa_offset = temp >> eiw; 3076 } else { 3077 bits_upper = div64_u64(hpa_offset >> (eig + eiw), 3); 3078 dpa_offset = bits_upper << (eig + 8); 3079 } 3080 dpa_offset |= bits_lower; 3081 3082 /* Look-up and return the result: a memdev and a DPA */ 3083 for (int i = 0; i < p->nr_targets; i++) { 3084 cxled = p->targets[i]; 3085 if (cxled->pos != pos) 3086 continue; 3087 result->cxlmd = cxled_to_memdev(cxled); 3088 result->dpa = cxl_dpa_resource_start(cxled) + dpa_offset; 3089 3090 return 0; 3091 } 3092 dev_err(&cxlr->dev, "No device found for position %d\n", pos); 3093 3094 return -ENXIO; 3095 } 3096 3097 static struct lock_class_key cxl_pmem_region_key; 3098 3099 static int cxl_pmem_region_alloc(struct cxl_region *cxlr) 3100 { 3101 struct cxl_region_params *p = &cxlr->params; 3102 struct cxl_nvdimm_bridge *cxl_nvb; 3103 struct device *dev; 3104 int i; 3105 3106 guard(rwsem_read)(&cxl_rwsem.region); 3107 if (p->state != CXL_CONFIG_COMMIT) 3108 return -ENXIO; 3109 3110 struct cxl_pmem_region *cxlr_pmem __free(kfree) = 3111 kzalloc(struct_size(cxlr_pmem, mapping, p->nr_targets), GFP_KERNEL); 3112 if (!cxlr_pmem) 3113 return -ENOMEM; 3114 3115 cxlr_pmem->hpa_range.start = p->res->start; 3116 cxlr_pmem->hpa_range.end = p->res->end; 3117 3118 /* Snapshot the region configuration underneath the cxl_rwsem.region */ 3119 cxlr_pmem->nr_mappings = p->nr_targets; 3120 for (i = 0; i < p->nr_targets; i++) { 3121 struct cxl_endpoint_decoder *cxled = p->targets[i]; 3122 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 3123 struct cxl_pmem_region_mapping *m = &cxlr_pmem->mapping[i]; 3124 3125 /* 3126 * Regions never span CXL root devices, so by definition the 3127 * bridge for one device is the same for all. 3128 */ 3129 if (i == 0) { 3130 cxl_nvb = cxl_find_nvdimm_bridge(cxlmd->endpoint); 3131 if (!cxl_nvb) 3132 return -ENODEV; 3133 cxlr->cxl_nvb = cxl_nvb; 3134 } 3135 m->cxlmd = cxlmd; 3136 get_device(&cxlmd->dev); 3137 m->start = cxled->dpa_res->start; 3138 m->size = resource_size(cxled->dpa_res); 3139 m->position = i; 3140 } 3141 3142 dev = &cxlr_pmem->dev; 3143 device_initialize(dev); 3144 lockdep_set_class(&dev->mutex, &cxl_pmem_region_key); 3145 device_set_pm_not_required(dev); 3146 dev->parent = &cxlr->dev; 3147 dev->bus = &cxl_bus_type; 3148 dev->type = &cxl_pmem_region_type; 3149 cxlr_pmem->cxlr = cxlr; 3150 cxlr->cxlr_pmem = no_free_ptr(cxlr_pmem); 3151 3152 return 0; 3153 } 3154 3155 static void cxl_dax_region_release(struct device *dev) 3156 { 3157 struct cxl_dax_region *cxlr_dax = to_cxl_dax_region(dev); 3158 3159 kfree(cxlr_dax); 3160 } 3161 3162 static const struct attribute_group *cxl_dax_region_attribute_groups[] = { 3163 &cxl_base_attribute_group, 3164 NULL, 3165 }; 3166 3167 const struct device_type cxl_dax_region_type = { 3168 .name = "cxl_dax_region", 3169 .release = cxl_dax_region_release, 3170 .groups = cxl_dax_region_attribute_groups, 3171 }; 3172 3173 static bool is_cxl_dax_region(struct device *dev) 3174 { 3175 return dev->type == &cxl_dax_region_type; 3176 } 3177 3178 struct cxl_dax_region *to_cxl_dax_region(struct device *dev) 3179 { 3180 if (dev_WARN_ONCE(dev, !is_cxl_dax_region(dev), 3181 "not a cxl_dax_region device\n")) 3182 return NULL; 3183 return container_of(dev, struct cxl_dax_region, dev); 3184 } 3185 EXPORT_SYMBOL_NS_GPL(to_cxl_dax_region, "CXL"); 3186 3187 static struct lock_class_key cxl_dax_region_key; 3188 3189 static struct cxl_dax_region *cxl_dax_region_alloc(struct cxl_region *cxlr) 3190 { 3191 struct cxl_region_params *p = &cxlr->params; 3192 struct cxl_dax_region *cxlr_dax; 3193 struct device *dev; 3194 3195 guard(rwsem_read)(&cxl_rwsem.region); 3196 if (p->state != CXL_CONFIG_COMMIT) 3197 return ERR_PTR(-ENXIO); 3198 3199 cxlr_dax = kzalloc(sizeof(*cxlr_dax), GFP_KERNEL); 3200 if (!cxlr_dax) 3201 return ERR_PTR(-ENOMEM); 3202 3203 cxlr_dax->hpa_range.start = p->res->start; 3204 cxlr_dax->hpa_range.end = p->res->end; 3205 3206 dev = &cxlr_dax->dev; 3207 cxlr_dax->cxlr = cxlr; 3208 device_initialize(dev); 3209 lockdep_set_class(&dev->mutex, &cxl_dax_region_key); 3210 device_set_pm_not_required(dev); 3211 dev->parent = &cxlr->dev; 3212 dev->bus = &cxl_bus_type; 3213 dev->type = &cxl_dax_region_type; 3214 3215 return cxlr_dax; 3216 } 3217 3218 static void cxlr_pmem_unregister(void *_cxlr_pmem) 3219 { 3220 struct cxl_pmem_region *cxlr_pmem = _cxlr_pmem; 3221 struct cxl_region *cxlr = cxlr_pmem->cxlr; 3222 struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb; 3223 3224 /* 3225 * Either the bridge is in ->remove() context under the device_lock(), 3226 * or cxlr_release_nvdimm() is cancelling the bridge's release action 3227 * for @cxlr_pmem and doing it itself (while manually holding the bridge 3228 * lock). 3229 */ 3230 device_lock_assert(&cxl_nvb->dev); 3231 cxlr->cxlr_pmem = NULL; 3232 cxlr_pmem->cxlr = NULL; 3233 device_unregister(&cxlr_pmem->dev); 3234 } 3235 3236 static void cxlr_release_nvdimm(void *_cxlr) 3237 { 3238 struct cxl_region *cxlr = _cxlr; 3239 struct cxl_nvdimm_bridge *cxl_nvb = cxlr->cxl_nvb; 3240 3241 scoped_guard(device, &cxl_nvb->dev) { 3242 if (cxlr->cxlr_pmem) 3243 devm_release_action(&cxl_nvb->dev, cxlr_pmem_unregister, 3244 cxlr->cxlr_pmem); 3245 } 3246 cxlr->cxl_nvb = NULL; 3247 put_device(&cxl_nvb->dev); 3248 } 3249 3250 /** 3251 * devm_cxl_add_pmem_region() - add a cxl_region-to-nd_region bridge 3252 * @cxlr: parent CXL region for this pmem region bridge device 3253 * 3254 * Return: 0 on success negative error code on failure. 3255 */ 3256 static int devm_cxl_add_pmem_region(struct cxl_region *cxlr) 3257 { 3258 struct cxl_pmem_region *cxlr_pmem; 3259 struct cxl_nvdimm_bridge *cxl_nvb; 3260 struct device *dev; 3261 int rc; 3262 3263 rc = cxl_pmem_region_alloc(cxlr); 3264 if (rc) 3265 return rc; 3266 cxlr_pmem = cxlr->cxlr_pmem; 3267 cxl_nvb = cxlr->cxl_nvb; 3268 3269 dev = &cxlr_pmem->dev; 3270 rc = dev_set_name(dev, "pmem_region%d", cxlr->id); 3271 if (rc) 3272 goto err; 3273 3274 rc = device_add(dev); 3275 if (rc) 3276 goto err; 3277 3278 dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent), 3279 dev_name(dev)); 3280 3281 scoped_guard(device, &cxl_nvb->dev) { 3282 if (cxl_nvb->dev.driver) 3283 rc = devm_add_action_or_reset(&cxl_nvb->dev, 3284 cxlr_pmem_unregister, 3285 cxlr_pmem); 3286 else 3287 rc = -ENXIO; 3288 } 3289 3290 if (rc) 3291 goto err_bridge; 3292 3293 /* @cxlr carries a reference on @cxl_nvb until cxlr_release_nvdimm */ 3294 return devm_add_action_or_reset(&cxlr->dev, cxlr_release_nvdimm, cxlr); 3295 3296 err: 3297 put_device(dev); 3298 err_bridge: 3299 put_device(&cxl_nvb->dev); 3300 cxlr->cxl_nvb = NULL; 3301 return rc; 3302 } 3303 3304 static void cxlr_dax_unregister(void *_cxlr_dax) 3305 { 3306 struct cxl_dax_region *cxlr_dax = _cxlr_dax; 3307 3308 device_unregister(&cxlr_dax->dev); 3309 } 3310 3311 static int devm_cxl_add_dax_region(struct cxl_region *cxlr) 3312 { 3313 struct cxl_dax_region *cxlr_dax; 3314 struct device *dev; 3315 int rc; 3316 3317 cxlr_dax = cxl_dax_region_alloc(cxlr); 3318 if (IS_ERR(cxlr_dax)) 3319 return PTR_ERR(cxlr_dax); 3320 3321 dev = &cxlr_dax->dev; 3322 rc = dev_set_name(dev, "dax_region%d", cxlr->id); 3323 if (rc) 3324 goto err; 3325 3326 rc = device_add(dev); 3327 if (rc) 3328 goto err; 3329 3330 dev_dbg(&cxlr->dev, "%s: register %s\n", dev_name(dev->parent), 3331 dev_name(dev)); 3332 3333 return devm_add_action_or_reset(&cxlr->dev, cxlr_dax_unregister, 3334 cxlr_dax); 3335 err: 3336 put_device(dev); 3337 return rc; 3338 } 3339 3340 static int match_decoder_by_range(struct device *dev, const void *data) 3341 { 3342 const struct range *r1, *r2 = data; 3343 struct cxl_decoder *cxld; 3344 3345 if (!is_switch_decoder(dev)) 3346 return 0; 3347 3348 cxld = to_cxl_decoder(dev); 3349 r1 = &cxld->hpa_range; 3350 return range_contains(r1, r2); 3351 } 3352 3353 static struct cxl_decoder * 3354 cxl_port_find_switch_decoder(struct cxl_port *port, struct range *hpa) 3355 { 3356 struct device *cxld_dev = device_find_child(&port->dev, hpa, 3357 match_decoder_by_range); 3358 3359 return cxld_dev ? to_cxl_decoder(cxld_dev) : NULL; 3360 } 3361 3362 static struct cxl_root_decoder * 3363 cxl_find_root_decoder(struct cxl_endpoint_decoder *cxled) 3364 { 3365 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 3366 struct cxl_port *port = cxled_to_port(cxled); 3367 struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port); 3368 struct cxl_decoder *root, *cxld = &cxled->cxld; 3369 struct range *hpa = &cxld->hpa_range; 3370 3371 root = cxl_port_find_switch_decoder(&cxl_root->port, hpa); 3372 if (!root) { 3373 dev_err(cxlmd->dev.parent, 3374 "%s:%s no CXL window for range %#llx:%#llx\n", 3375 dev_name(&cxlmd->dev), dev_name(&cxld->dev), 3376 cxld->hpa_range.start, cxld->hpa_range.end); 3377 return NULL; 3378 } 3379 3380 return to_cxl_root_decoder(&root->dev); 3381 } 3382 3383 static int match_region_by_range(struct device *dev, const void *data) 3384 { 3385 struct cxl_region_params *p; 3386 struct cxl_region *cxlr; 3387 const struct range *r = data; 3388 3389 if (!is_cxl_region(dev)) 3390 return 0; 3391 3392 cxlr = to_cxl_region(dev); 3393 p = &cxlr->params; 3394 3395 guard(rwsem_read)(&cxl_rwsem.region); 3396 if (p->res && p->res->start == r->start && p->res->end == r->end) 3397 return 1; 3398 3399 return 0; 3400 } 3401 3402 static int cxl_extended_linear_cache_resize(struct cxl_region *cxlr, 3403 struct resource *res) 3404 { 3405 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(cxlr->dev.parent); 3406 struct cxl_region_params *p = &cxlr->params; 3407 resource_size_t size = resource_size(res); 3408 resource_size_t cache_size, start; 3409 3410 cache_size = cxlrd->cache_size; 3411 if (!cache_size) 3412 return 0; 3413 3414 if (size != cache_size) { 3415 dev_warn(&cxlr->dev, 3416 "Extended Linear Cache size %pa != CXL size %pa. No Support!", 3417 &cache_size, &size); 3418 return -ENXIO; 3419 } 3420 3421 /* 3422 * Move the start of the range to where the cache range starts. The 3423 * implementation assumes that the cache range is in front of the 3424 * CXL range. This is not dictated by the HMAT spec but is how the 3425 * current known implementation is configured. 3426 * 3427 * The cache range is expected to be within the CFMWS. The adjusted 3428 * res->start should not be less than cxlrd->res->start. 3429 */ 3430 start = res->start - cache_size; 3431 if (start < cxlrd->res->start) 3432 return -ENXIO; 3433 3434 res->start = start; 3435 p->cache_size = cache_size; 3436 3437 return 0; 3438 } 3439 3440 static int __construct_region(struct cxl_region *cxlr, 3441 struct cxl_root_decoder *cxlrd, 3442 struct cxl_endpoint_decoder *cxled) 3443 { 3444 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 3445 struct range *hpa = &cxled->cxld.hpa_range; 3446 struct cxl_region_params *p; 3447 struct resource *res; 3448 int rc; 3449 3450 guard(rwsem_write)(&cxl_rwsem.region); 3451 p = &cxlr->params; 3452 if (p->state >= CXL_CONFIG_INTERLEAVE_ACTIVE) { 3453 dev_err(cxlmd->dev.parent, 3454 "%s:%s: %s autodiscovery interrupted\n", 3455 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), 3456 __func__); 3457 return -EBUSY; 3458 } 3459 3460 set_bit(CXL_REGION_F_AUTO, &cxlr->flags); 3461 3462 res = kmalloc(sizeof(*res), GFP_KERNEL); 3463 if (!res) 3464 return -ENOMEM; 3465 3466 *res = DEFINE_RES_MEM_NAMED(hpa->start, range_len(hpa), 3467 dev_name(&cxlr->dev)); 3468 3469 rc = cxl_extended_linear_cache_resize(cxlr, res); 3470 if (rc && rc != -EOPNOTSUPP) { 3471 /* 3472 * Failing to support extended linear cache region resize does not 3473 * prevent the region from functioning. Only causes cxl list showing 3474 * incorrect region size. 3475 */ 3476 dev_warn(cxlmd->dev.parent, 3477 "Extended linear cache calculation failed rc:%d\n", rc); 3478 } 3479 3480 rc = insert_resource(cxlrd->res, res); 3481 if (rc) { 3482 /* 3483 * Platform-firmware may not have split resources like "System 3484 * RAM" on CXL window boundaries see cxl_region_iomem_release() 3485 */ 3486 dev_warn(cxlmd->dev.parent, 3487 "%s:%s: %s %s cannot insert resource\n", 3488 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), 3489 __func__, dev_name(&cxlr->dev)); 3490 } 3491 3492 p->res = res; 3493 p->interleave_ways = cxled->cxld.interleave_ways; 3494 p->interleave_granularity = cxled->cxld.interleave_granularity; 3495 p->state = CXL_CONFIG_INTERLEAVE_ACTIVE; 3496 3497 rc = sysfs_update_group(&cxlr->dev.kobj, get_cxl_region_target_group()); 3498 if (rc) 3499 return rc; 3500 3501 dev_dbg(cxlmd->dev.parent, "%s:%s: %s %s res: %pr iw: %d ig: %d\n", 3502 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), __func__, 3503 dev_name(&cxlr->dev), p->res, p->interleave_ways, 3504 p->interleave_granularity); 3505 3506 /* ...to match put_device() in cxl_add_to_region() */ 3507 get_device(&cxlr->dev); 3508 3509 return 0; 3510 } 3511 3512 /* Establish an empty region covering the given HPA range */ 3513 static struct cxl_region *construct_region(struct cxl_root_decoder *cxlrd, 3514 struct cxl_endpoint_decoder *cxled) 3515 { 3516 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 3517 struct cxl_port *port = cxlrd_to_port(cxlrd); 3518 struct cxl_dev_state *cxlds = cxlmd->cxlds; 3519 int rc, part = READ_ONCE(cxled->part); 3520 struct cxl_region *cxlr; 3521 3522 do { 3523 cxlr = __create_region(cxlrd, cxlds->part[part].mode, 3524 atomic_read(&cxlrd->region_id)); 3525 } while (IS_ERR(cxlr) && PTR_ERR(cxlr) == -EBUSY); 3526 3527 if (IS_ERR(cxlr)) { 3528 dev_err(cxlmd->dev.parent, 3529 "%s:%s: %s failed assign region: %ld\n", 3530 dev_name(&cxlmd->dev), dev_name(&cxled->cxld.dev), 3531 __func__, PTR_ERR(cxlr)); 3532 return cxlr; 3533 } 3534 3535 rc = __construct_region(cxlr, cxlrd, cxled); 3536 if (rc) { 3537 devm_release_action(port->uport_dev, unregister_region, cxlr); 3538 return ERR_PTR(rc); 3539 } 3540 3541 return cxlr; 3542 } 3543 3544 static struct cxl_region * 3545 cxl_find_region_by_range(struct cxl_root_decoder *cxlrd, struct range *hpa) 3546 { 3547 struct device *region_dev; 3548 3549 region_dev = device_find_child(&cxlrd->cxlsd.cxld.dev, hpa, 3550 match_region_by_range); 3551 if (!region_dev) 3552 return NULL; 3553 3554 return to_cxl_region(region_dev); 3555 } 3556 3557 int cxl_add_to_region(struct cxl_endpoint_decoder *cxled) 3558 { 3559 struct range *hpa = &cxled->cxld.hpa_range; 3560 struct cxl_region_params *p; 3561 bool attach = false; 3562 int rc; 3563 3564 struct cxl_root_decoder *cxlrd __free(put_cxl_root_decoder) = 3565 cxl_find_root_decoder(cxled); 3566 if (!cxlrd) 3567 return -ENXIO; 3568 3569 /* 3570 * Ensure that if multiple threads race to construct_region() for @hpa 3571 * one does the construction and the others add to that. 3572 */ 3573 mutex_lock(&cxlrd->range_lock); 3574 struct cxl_region *cxlr __free(put_cxl_region) = 3575 cxl_find_region_by_range(cxlrd, hpa); 3576 if (!cxlr) 3577 cxlr = construct_region(cxlrd, cxled); 3578 mutex_unlock(&cxlrd->range_lock); 3579 3580 rc = PTR_ERR_OR_ZERO(cxlr); 3581 if (rc) 3582 return rc; 3583 3584 attach_target(cxlr, cxled, -1, TASK_UNINTERRUPTIBLE); 3585 3586 scoped_guard(rwsem_read, &cxl_rwsem.region) { 3587 p = &cxlr->params; 3588 attach = p->state == CXL_CONFIG_COMMIT; 3589 } 3590 3591 if (attach) { 3592 /* 3593 * If device_attach() fails the range may still be active via 3594 * the platform-firmware memory map, otherwise the driver for 3595 * regions is local to this file, so driver matching can't fail. 3596 */ 3597 if (device_attach(&cxlr->dev) < 0) 3598 dev_err(&cxlr->dev, "failed to enable, range: %pr\n", 3599 p->res); 3600 } 3601 3602 return rc; 3603 } 3604 EXPORT_SYMBOL_NS_GPL(cxl_add_to_region, "CXL"); 3605 3606 u64 cxl_port_get_spa_cache_alias(struct cxl_port *endpoint, u64 spa) 3607 { 3608 struct cxl_region_ref *iter; 3609 unsigned long index; 3610 3611 if (!endpoint) 3612 return ~0ULL; 3613 3614 guard(rwsem_write)(&cxl_rwsem.region); 3615 3616 xa_for_each(&endpoint->regions, index, iter) { 3617 struct cxl_region_params *p = &iter->region->params; 3618 3619 if (cxl_resource_contains_addr(p->res, spa)) { 3620 if (!p->cache_size) 3621 return ~0ULL; 3622 3623 if (spa >= p->res->start + p->cache_size) 3624 return spa - p->cache_size; 3625 3626 return spa + p->cache_size; 3627 } 3628 } 3629 3630 return ~0ULL; 3631 } 3632 EXPORT_SYMBOL_NS_GPL(cxl_port_get_spa_cache_alias, "CXL"); 3633 3634 static int is_system_ram(struct resource *res, void *arg) 3635 { 3636 struct cxl_region *cxlr = arg; 3637 struct cxl_region_params *p = &cxlr->params; 3638 3639 dev_dbg(&cxlr->dev, "%pr has System RAM: %pr\n", p->res, res); 3640 return 1; 3641 } 3642 3643 static void shutdown_notifiers(void *_cxlr) 3644 { 3645 struct cxl_region *cxlr = _cxlr; 3646 3647 unregister_node_notifier(&cxlr->node_notifier); 3648 unregister_mt_adistance_algorithm(&cxlr->adist_notifier); 3649 } 3650 3651 static void remove_debugfs(void *dentry) 3652 { 3653 debugfs_remove_recursive(dentry); 3654 } 3655 3656 static int validate_region_offset(struct cxl_region *cxlr, u64 offset) 3657 { 3658 struct cxl_region_params *p = &cxlr->params; 3659 resource_size_t region_size; 3660 u64 hpa; 3661 3662 if (offset < p->cache_size) { 3663 dev_err(&cxlr->dev, 3664 "Offset %#llx is within extended linear cache %#llx\n", 3665 offset, p->cache_size); 3666 return -EINVAL; 3667 } 3668 3669 region_size = resource_size(p->res); 3670 if (offset >= region_size) { 3671 dev_err(&cxlr->dev, "Offset %#llx exceeds region size %#llx\n", 3672 offset, region_size); 3673 return -EINVAL; 3674 } 3675 3676 hpa = p->res->start + offset; 3677 if (hpa < p->res->start || hpa > p->res->end) { 3678 dev_err(&cxlr->dev, "HPA %#llx not in region %pr\n", hpa, 3679 p->res); 3680 return -EINVAL; 3681 } 3682 3683 return 0; 3684 } 3685 3686 static int cxl_region_debugfs_poison_inject(void *data, u64 offset) 3687 { 3688 struct dpa_result result = { .dpa = ULLONG_MAX, .cxlmd = NULL }; 3689 struct cxl_region *cxlr = data; 3690 int rc; 3691 3692 ACQUIRE(rwsem_read_intr, region_rwsem)(&cxl_rwsem.region); 3693 if ((rc = ACQUIRE_ERR(rwsem_read_intr, ®ion_rwsem))) 3694 return rc; 3695 3696 ACQUIRE(rwsem_read_intr, dpa_rwsem)(&cxl_rwsem.dpa); 3697 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &dpa_rwsem))) 3698 return rc; 3699 3700 if (validate_region_offset(cxlr, offset)) 3701 return -EINVAL; 3702 3703 rc = region_offset_to_dpa_result(cxlr, offset, &result); 3704 if (rc || !result.cxlmd || result.dpa == ULLONG_MAX) { 3705 dev_dbg(&cxlr->dev, 3706 "Failed to resolve DPA for region offset %#llx rc %d\n", 3707 offset, rc); 3708 3709 return rc ? rc : -EINVAL; 3710 } 3711 3712 return cxl_inject_poison_locked(result.cxlmd, result.dpa); 3713 } 3714 3715 DEFINE_DEBUGFS_ATTRIBUTE(cxl_poison_inject_fops, NULL, 3716 cxl_region_debugfs_poison_inject, "%llx\n"); 3717 3718 static int cxl_region_debugfs_poison_clear(void *data, u64 offset) 3719 { 3720 struct dpa_result result = { .dpa = ULLONG_MAX, .cxlmd = NULL }; 3721 struct cxl_region *cxlr = data; 3722 int rc; 3723 3724 ACQUIRE(rwsem_read_intr, region_rwsem)(&cxl_rwsem.region); 3725 if ((rc = ACQUIRE_ERR(rwsem_read_intr, ®ion_rwsem))) 3726 return rc; 3727 3728 ACQUIRE(rwsem_read_intr, dpa_rwsem)(&cxl_rwsem.dpa); 3729 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &dpa_rwsem))) 3730 return rc; 3731 3732 if (validate_region_offset(cxlr, offset)) 3733 return -EINVAL; 3734 3735 rc = region_offset_to_dpa_result(cxlr, offset, &result); 3736 if (rc || !result.cxlmd || result.dpa == ULLONG_MAX) { 3737 dev_dbg(&cxlr->dev, 3738 "Failed to resolve DPA for region offset %#llx rc %d\n", 3739 offset, rc); 3740 3741 return rc ? rc : -EINVAL; 3742 } 3743 3744 return cxl_clear_poison_locked(result.cxlmd, result.dpa); 3745 } 3746 3747 DEFINE_DEBUGFS_ATTRIBUTE(cxl_poison_clear_fops, NULL, 3748 cxl_region_debugfs_poison_clear, "%llx\n"); 3749 3750 static int cxl_region_can_probe(struct cxl_region *cxlr) 3751 { 3752 struct cxl_region_params *p = &cxlr->params; 3753 int rc; 3754 3755 ACQUIRE(rwsem_read_intr, rwsem)(&cxl_rwsem.region); 3756 if ((rc = ACQUIRE_ERR(rwsem_read_intr, &rwsem))) { 3757 dev_dbg(&cxlr->dev, "probe interrupted\n"); 3758 return rc; 3759 } 3760 3761 if (p->state < CXL_CONFIG_COMMIT) { 3762 dev_dbg(&cxlr->dev, "config state: %d\n", p->state); 3763 return -ENXIO; 3764 } 3765 3766 if (test_bit(CXL_REGION_F_NEEDS_RESET, &cxlr->flags)) { 3767 dev_err(&cxlr->dev, 3768 "failed to activate, re-commit region and retry\n"); 3769 return -ENXIO; 3770 } 3771 3772 return 0; 3773 } 3774 3775 static int cxl_region_probe(struct device *dev) 3776 { 3777 struct cxl_region *cxlr = to_cxl_region(dev); 3778 struct cxl_region_params *p = &cxlr->params; 3779 bool poison_supported = true; 3780 int rc; 3781 3782 rc = cxl_region_can_probe(cxlr); 3783 if (rc) 3784 return rc; 3785 3786 /* 3787 * From this point on any path that changes the region's state away from 3788 * CXL_CONFIG_COMMIT is also responsible for releasing the driver. 3789 */ 3790 3791 cxlr->node_notifier.notifier_call = cxl_region_perf_attrs_callback; 3792 cxlr->node_notifier.priority = CXL_CALLBACK_PRI; 3793 register_node_notifier(&cxlr->node_notifier); 3794 3795 cxlr->adist_notifier.notifier_call = cxl_region_calculate_adistance; 3796 cxlr->adist_notifier.priority = 100; 3797 register_mt_adistance_algorithm(&cxlr->adist_notifier); 3798 3799 rc = devm_add_action_or_reset(&cxlr->dev, shutdown_notifiers, cxlr); 3800 if (rc) 3801 return rc; 3802 3803 /* Create poison attributes if all memdevs support the capabilities */ 3804 for (int i = 0; i < p->nr_targets; i++) { 3805 struct cxl_endpoint_decoder *cxled = p->targets[i]; 3806 struct cxl_memdev *cxlmd = cxled_to_memdev(cxled); 3807 3808 if (!cxl_memdev_has_poison_cmd(cxlmd, CXL_POISON_ENABLED_INJECT) || 3809 !cxl_memdev_has_poison_cmd(cxlmd, CXL_POISON_ENABLED_CLEAR)) { 3810 poison_supported = false; 3811 break; 3812 } 3813 } 3814 3815 if (poison_supported) { 3816 struct dentry *dentry; 3817 3818 dentry = cxl_debugfs_create_dir(dev_name(dev)); 3819 debugfs_create_file("inject_poison", 0200, dentry, cxlr, 3820 &cxl_poison_inject_fops); 3821 debugfs_create_file("clear_poison", 0200, dentry, cxlr, 3822 &cxl_poison_clear_fops); 3823 rc = devm_add_action_or_reset(dev, remove_debugfs, dentry); 3824 if (rc) 3825 return rc; 3826 } 3827 3828 switch (cxlr->mode) { 3829 case CXL_PARTMODE_PMEM: 3830 rc = devm_cxl_region_edac_register(cxlr); 3831 if (rc) 3832 dev_dbg(&cxlr->dev, "CXL EDAC registration for region_id=%d failed\n", 3833 cxlr->id); 3834 3835 return devm_cxl_add_pmem_region(cxlr); 3836 case CXL_PARTMODE_RAM: 3837 rc = devm_cxl_region_edac_register(cxlr); 3838 if (rc) 3839 dev_dbg(&cxlr->dev, "CXL EDAC registration for region_id=%d failed\n", 3840 cxlr->id); 3841 3842 /* 3843 * The region can not be manged by CXL if any portion of 3844 * it is already online as 'System RAM' 3845 */ 3846 if (walk_iomem_res_desc(IORES_DESC_NONE, 3847 IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY, 3848 p->res->start, p->res->end, cxlr, 3849 is_system_ram) > 0) 3850 return 0; 3851 return devm_cxl_add_dax_region(cxlr); 3852 default: 3853 dev_dbg(&cxlr->dev, "unsupported region mode: %d\n", 3854 cxlr->mode); 3855 return -ENXIO; 3856 } 3857 } 3858 3859 static struct cxl_driver cxl_region_driver = { 3860 .name = "cxl_region", 3861 .probe = cxl_region_probe, 3862 .id = CXL_DEVICE_REGION, 3863 }; 3864 3865 int cxl_region_init(void) 3866 { 3867 return cxl_driver_register(&cxl_region_driver); 3868 } 3869 3870 void cxl_region_exit(void) 3871 { 3872 cxl_driver_unregister(&cxl_region_driver); 3873 } 3874 3875 MODULE_IMPORT_NS("CXL"); 3876 MODULE_IMPORT_NS("DEVMEM"); 3877 MODULE_ALIAS_CXL(CXL_DEVICE_REGION); 3878