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