1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright(c) 2021 Intel Corporation. All rights reserved. */ 3 #include <linux/platform_device.h> 4 #include <linux/module.h> 5 #include <linux/device.h> 6 #include <linux/kernel.h> 7 #include <linux/acpi.h> 8 #include <linux/pci.h> 9 #include <linux/node.h> 10 #include <asm/div64.h> 11 #include "cxlpci.h" 12 #include "cxl.h" 13 14 static const guid_t acpi_cxl_qtg_id_guid = 15 GUID_INIT(0xF365F9A6, 0xA7DE, 0x4071, 16 0xA6, 0x6A, 0xB4, 0x0C, 0x0B, 0x4F, 0x8E, 0x52); 17 18 #define HBIW_TO_NR_MAPS_SIZE (CXL_DECODER_MAX_INTERLEAVE + 1) 19 static const int hbiw_to_nr_maps[HBIW_TO_NR_MAPS_SIZE] = { 20 [1] = 0, [2] = 1, [3] = 0, [4] = 2, [6] = 1, [8] = 3, [12] = 2, [16] = 4 21 }; 22 23 static const int valid_hbiw[] = { 1, 2, 3, 4, 6, 8, 12, 16 }; 24 25 u64 cxl_do_xormap_calc(struct cxl_cxims_data *cximsd, u64 addr, int hbiw) 26 { 27 int nr_maps_to_apply = -1; 28 u64 val; 29 int pos; 30 31 /* 32 * Strictly validate hbiw since this function is used for testing and 33 * that nullifies any expectation of trusted parameters from the CXL 34 * Region Driver. 35 */ 36 for (int i = 0; i < ARRAY_SIZE(valid_hbiw); i++) { 37 if (valid_hbiw[i] == hbiw) { 38 nr_maps_to_apply = hbiw_to_nr_maps[hbiw]; 39 break; 40 } 41 } 42 if (nr_maps_to_apply == -1 || nr_maps_to_apply > cximsd->nr_maps) 43 return ULLONG_MAX; 44 45 /* 46 * In regions using XOR interleave arithmetic the CXL HPA may not 47 * be the same as the SPA. This helper performs the SPA->CXL HPA 48 * or the CXL HPA->SPA translation. Since XOR is self-inverting, 49 * so is this function. 50 * 51 * For root decoders using xormaps (hbiw: 2,4,6,8,12,16) applying the 52 * xormaps will toggle a position bit. 53 * 54 * pos is the lowest set bit in an XORMAP 55 * val is the XORALLBITS(addr & XORMAP) 56 * 57 * XORALLBITS: The CXL spec (3.1 Table 9-22) defines XORALLBITS 58 * as an operation that outputs a single bit by XORing all the 59 * bits in the input (addr & xormap). Implement XORALLBITS using 60 * hweight64(). If the hamming weight is even the XOR of those 61 * bits results in val==0, if odd the XOR result is val==1. 62 */ 63 64 for (int i = 0; i < cximsd->nr_maps; i++) { 65 if (!cximsd->xormaps[i]) 66 continue; 67 pos = __ffs(cximsd->xormaps[i]); 68 val = (hweight64(addr & cximsd->xormaps[i]) & 1); 69 addr = (addr & ~(1ULL << pos)) | (val << pos); 70 } 71 72 return addr; 73 } 74 EXPORT_SYMBOL_FOR_MODULES(cxl_do_xormap_calc, "cxl_translate"); 75 76 static u64 cxl_apply_xor_maps(struct cxl_root_decoder *cxlrd, u64 addr) 77 { 78 int hbiw = cxlrd->cxlsd.nr_targets; 79 struct cxl_cxims_data *cximsd; 80 81 /* No xormaps for host bridge interleave ways of 1 or 3 */ 82 if (hbiw == 1 || hbiw == 3) 83 return addr; 84 85 cximsd = cxlrd->platform_data; 86 87 return cxl_do_xormap_calc(cximsd, addr, hbiw); 88 } 89 90 struct cxl_cxims_context { 91 struct device *dev; 92 struct cxl_root_decoder *cxlrd; 93 }; 94 95 static int cxl_parse_cxims(union acpi_subtable_headers *header, void *arg, 96 const unsigned long end) 97 { 98 struct acpi_cedt_cxims *cxims = (struct acpi_cedt_cxims *)header; 99 struct cxl_cxims_context *ctx = arg; 100 struct cxl_root_decoder *cxlrd = ctx->cxlrd; 101 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld; 102 struct device *dev = ctx->dev; 103 struct cxl_cxims_data *cximsd; 104 unsigned int nr_maps; 105 int hbig, rc; 106 107 rc = eig_to_granularity(cxims->hbig, &hbig); 108 if (rc) 109 return rc; 110 111 /* Does this CXIMS entry apply to the given CXL Window? */ 112 if (hbig != cxld->interleave_granularity) 113 return 0; 114 115 /* IW 1,3 do not use xormaps and skip this parsing entirely */ 116 if (is_power_of_2(cxld->interleave_ways)) 117 /* 2, 4, 8, 16 way */ 118 nr_maps = ilog2(cxld->interleave_ways); 119 else 120 /* 6, 12 way */ 121 nr_maps = ilog2(cxld->interleave_ways / 3); 122 123 if (cxims->nr_xormaps < nr_maps) { 124 dev_dbg(dev, "CXIMS nr_xormaps[%d] expected[%d]\n", 125 cxims->nr_xormaps, nr_maps); 126 return -ENXIO; 127 } 128 129 cximsd = devm_kzalloc(dev, struct_size(cximsd, xormaps, nr_maps), 130 GFP_KERNEL); 131 if (!cximsd) 132 return -ENOMEM; 133 cximsd->nr_maps = nr_maps; 134 memcpy(cximsd->xormaps, cxims->xormap_list, 135 nr_maps * sizeof(*cximsd->xormaps)); 136 cxlrd->platform_data = cximsd; 137 138 return 0; 139 } 140 141 static unsigned long cfmws_to_decoder_flags(int restrictions) 142 { 143 unsigned long flags = CXL_DECODER_F_ENABLE; 144 145 if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_DEVMEM) 146 flags |= CXL_DECODER_F_TYPE2; 147 if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_HOSTONLYMEM) 148 flags |= CXL_DECODER_F_TYPE3; 149 if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_VOLATILE) 150 flags |= CXL_DECODER_F_RAM; 151 if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_PMEM) 152 flags |= CXL_DECODER_F_PMEM; 153 if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_FIXED) 154 flags |= CXL_DECODER_F_LOCK; 155 156 return flags; 157 } 158 159 static int cxl_acpi_cfmws_verify(struct device *dev, 160 struct acpi_cedt_cfmws *cfmws) 161 { 162 int rc, expected_len; 163 int ways; 164 165 if (cfmws->interleave_arithmetic != ACPI_CEDT_CFMWS_ARITHMETIC_MODULO && 166 cfmws->interleave_arithmetic != ACPI_CEDT_CFMWS_ARITHMETIC_XOR) { 167 dev_err(dev, "CFMWS Unknown Interleave Arithmetic: %d\n", 168 cfmws->interleave_arithmetic); 169 return -EINVAL; 170 } 171 172 if (!IS_ALIGNED(cfmws->base_hpa, SZ_256M)) { 173 dev_err(dev, "CFMWS Base HPA not 256MB aligned\n"); 174 return -EINVAL; 175 } 176 177 if (!IS_ALIGNED(cfmws->window_size, SZ_256M)) { 178 dev_err(dev, "CFMWS Window Size not 256MB aligned\n"); 179 return -EINVAL; 180 } 181 182 rc = eiw_to_ways(cfmws->interleave_ways, &ways); 183 if (rc) { 184 dev_err(dev, "CFMWS Interleave Ways (%d) invalid\n", 185 cfmws->interleave_ways); 186 return -EINVAL; 187 } 188 189 expected_len = struct_size(cfmws, interleave_targets, ways); 190 191 if (cfmws->header.length < expected_len) { 192 dev_err(dev, "CFMWS length %d less than expected %d\n", 193 cfmws->header.length, expected_len); 194 return -EINVAL; 195 } 196 197 if (cfmws->header.length > expected_len) 198 dev_dbg(dev, "CFMWS length %d greater than expected %d\n", 199 cfmws->header.length, expected_len); 200 201 return 0; 202 } 203 204 /* 205 * Note, @dev must be the first member, see 'struct cxl_chbs_context' 206 * and mock_acpi_table_parse_cedt() 207 */ 208 struct cxl_cfmws_context { 209 struct device *dev; 210 struct cxl_port *root_port; 211 struct resource *cxl_res; 212 int id; 213 }; 214 215 /** 216 * cxl_acpi_evaluate_qtg_dsm - Retrieve QTG ids via ACPI _DSM 217 * @handle: ACPI handle 218 * @coord: performance access coordinates 219 * @entries: number of QTG IDs to return 220 * @qos_class: int array provided by caller to return QTG IDs 221 * 222 * Return: number of QTG IDs returned, or -errno for errors 223 * 224 * Issue QTG _DSM with accompanied bandwidth and latency data in order to get 225 * the QTG IDs that are suitable for the performance point in order of most 226 * suitable to least suitable. Write back array of QTG IDs and return the 227 * actual number of QTG IDs written back. 228 */ 229 static int 230 cxl_acpi_evaluate_qtg_dsm(acpi_handle handle, struct access_coordinate *coord, 231 int entries, int *qos_class) 232 { 233 union acpi_object *out_obj, *out_buf, *obj; 234 /* QTG _DSM expects latencies in picoseconds. */ 235 union acpi_object in_array[4] = { 236 [0].integer = { ACPI_TYPE_INTEGER, coord->read_latency * 1000ULL }, 237 [1].integer = { ACPI_TYPE_INTEGER, coord->write_latency * 1000ULL }, 238 [2].integer = { ACPI_TYPE_INTEGER, coord->read_bandwidth }, 239 [3].integer = { ACPI_TYPE_INTEGER, coord->write_bandwidth }, 240 }; 241 union acpi_object in_obj = { 242 .package = { 243 .type = ACPI_TYPE_PACKAGE, 244 .count = 4, 245 .elements = in_array, 246 }, 247 }; 248 int count, pkg_entries, i; 249 u16 max_qtg; 250 int rc; 251 252 if (!entries) 253 return -EINVAL; 254 255 /* Latency inputs must fit in the DWORD fields defined by the spec. */ 256 if (coord->read_latency > U32_MAX / 1000 || 257 coord->write_latency > U32_MAX / 1000) 258 return -ERANGE; 259 260 out_obj = acpi_evaluate_dsm(handle, &acpi_cxl_qtg_id_guid, 1, 1, &in_obj); 261 if (!out_obj) 262 return -ENXIO; 263 264 if (out_obj->type != ACPI_TYPE_PACKAGE) { 265 rc = -ENXIO; 266 goto out; 267 } 268 269 /* Check Max QTG ID */ 270 obj = &out_obj->package.elements[0]; 271 if (obj->type != ACPI_TYPE_INTEGER) { 272 rc = -ENXIO; 273 goto out; 274 } 275 276 max_qtg = obj->integer.value; 277 278 /* It's legal to have 0 QTG entries */ 279 pkg_entries = out_obj->package.count; 280 if (pkg_entries <= 1) { 281 rc = 0; 282 goto out; 283 } 284 285 /* Retrieve QTG IDs package */ 286 obj = &out_obj->package.elements[1]; 287 if (obj->type != ACPI_TYPE_PACKAGE) { 288 rc = -ENXIO; 289 goto out; 290 } 291 292 pkg_entries = obj->package.count; 293 count = min(entries, pkg_entries); 294 for (i = 0; i < count; i++) { 295 u16 qtg_id; 296 297 out_buf = &obj->package.elements[i]; 298 if (out_buf->type != ACPI_TYPE_INTEGER) { 299 rc = -ENXIO; 300 goto out; 301 } 302 303 qtg_id = out_buf->integer.value; 304 if (qtg_id > max_qtg) 305 pr_warn("QTG ID %u greater than MAX %u\n", 306 qtg_id, max_qtg); 307 308 qos_class[i] = qtg_id; 309 } 310 rc = count; 311 312 out: 313 ACPI_FREE(out_obj); 314 return rc; 315 } 316 317 static int cxl_acpi_qos_class(struct cxl_root *cxl_root, 318 struct access_coordinate *coord, int entries, 319 int *qos_class) 320 { 321 struct device *dev = cxl_root->port.uport_dev; 322 acpi_handle handle; 323 324 if (!dev_is_platform(dev)) 325 return -ENODEV; 326 327 handle = ACPI_HANDLE(dev); 328 if (!handle) 329 return -ENODEV; 330 331 return cxl_acpi_evaluate_qtg_dsm(handle, coord, entries, qos_class); 332 } 333 334 static void del_cxl_resource(struct resource *res) 335 { 336 if (!res) 337 return; 338 kfree(res->name); 339 kfree(res); 340 } 341 342 static struct resource *alloc_cxl_resource(resource_size_t base, 343 resource_size_t n, int id) 344 { 345 struct resource *res __free(kfree) = kzalloc_obj(*res); 346 347 if (!res) 348 return NULL; 349 350 res->start = base; 351 res->end = base + n - 1; 352 res->flags = IORESOURCE_MEM; 353 res->name = kasprintf(GFP_KERNEL, "CXL Window %d", id); 354 if (!res->name) 355 return NULL; 356 357 return no_free_ptr(res); 358 } 359 360 static int add_or_reset_cxl_resource(struct resource *parent, struct resource *res) 361 { 362 int rc = insert_resource(parent, res); 363 364 if (rc) 365 del_cxl_resource(res); 366 return rc; 367 } 368 369 static void cxl_setup_extended_linear_cache(struct cxl_root_decoder *cxlrd) 370 { 371 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld; 372 struct range *hpa = &cxld->hpa_range; 373 resource_size_t size = range_len(hpa); 374 resource_size_t start = hpa->start; 375 resource_size_t cache_size; 376 struct resource res; 377 int nid, rc; 378 379 /* Explicitly initialize cache size to 0 at the beginning */ 380 cxlrd->cache_size = 0; 381 res = DEFINE_RES_MEM(start, size); 382 nid = phys_to_target_node(start); 383 384 rc = hmat_get_extended_linear_cache_size(&res, nid, &cache_size); 385 if (rc) 386 return; 387 388 /* 389 * The cache range is expected to be within the CFMWS. 390 * Currently there is only support cache_size == cxl_size. CXL 391 * size is then half of the total CFMWS window size. 392 */ 393 size = size >> 1; 394 if (cache_size && size != cache_size) { 395 dev_warn(&cxld->dev, 396 "Extended Linear Cache size %pa != CXL size %pa. No Support!", 397 &cache_size, &size); 398 return; 399 } 400 401 cxlrd->cache_size = cache_size; 402 } 403 404 DEFINE_FREE(put_cxlrd, struct cxl_root_decoder *, 405 if (!IS_ERR_OR_NULL(_T)) put_device(&_T->cxlsd.cxld.dev)) 406 DEFINE_FREE(del_cxl_resource, struct resource *, if (_T) del_cxl_resource(_T)) 407 static int __cxl_parse_cfmws(struct acpi_cedt_cfmws *cfmws, 408 struct cxl_cfmws_context *ctx) 409 { 410 struct cxl_port *root_port = ctx->root_port; 411 struct cxl_cxims_context cxims_ctx; 412 struct device *dev = ctx->dev; 413 struct cxl_decoder *cxld; 414 int ways, i, ig; 415 int rc; 416 417 rc = cxl_acpi_cfmws_verify(dev, cfmws); 418 if (rc) 419 return rc; 420 421 rc = eiw_to_ways(cfmws->interleave_ways, &ways); 422 if (rc) 423 return rc; 424 rc = eig_to_granularity(cfmws->granularity, &ig); 425 if (rc) 426 return rc; 427 428 struct resource *res __free(del_cxl_resource) = alloc_cxl_resource( 429 cfmws->base_hpa, cfmws->window_size, ctx->id++); 430 if (!res) 431 return -ENOMEM; 432 433 /* add to the local resource tracking to establish a sort order */ 434 rc = add_or_reset_cxl_resource(ctx->cxl_res, no_free_ptr(res)); 435 if (rc) 436 return rc; 437 438 struct cxl_root_decoder *cxlrd __free(put_cxlrd) = 439 cxl_root_decoder_alloc(root_port, ways); 440 441 if (IS_ERR(cxlrd)) 442 return PTR_ERR(cxlrd); 443 444 cxld = &cxlrd->cxlsd.cxld; 445 cxld->flags = cfmws_to_decoder_flags(cfmws->restrictions); 446 cxld->target_type = CXL_DECODER_HOSTONLYMEM; 447 cxld->hpa_range = (struct range) { 448 .start = cfmws->base_hpa, 449 .end = cfmws->base_hpa + cfmws->window_size - 1, 450 }; 451 cxld->interleave_ways = ways; 452 for (i = 0; i < ways; i++) 453 cxld->target_map[i] = cfmws->interleave_targets[i]; 454 /* 455 * Minimize the x1 granularity to advertise support for any 456 * valid region granularity 457 */ 458 if (ways == 1) 459 ig = CXL_DECODER_MIN_GRANULARITY; 460 cxld->interleave_granularity = ig; 461 462 if (cfmws->interleave_arithmetic == ACPI_CEDT_CFMWS_ARITHMETIC_XOR) { 463 if (ways != 1 && ways != 3) { 464 cxims_ctx = (struct cxl_cxims_context) { 465 .dev = dev, 466 .cxlrd = cxlrd, 467 }; 468 rc = acpi_table_parse_cedt(ACPI_CEDT_TYPE_CXIMS, 469 cxl_parse_cxims, &cxims_ctx); 470 if (rc < 0) 471 return rc; 472 if (!cxlrd->platform_data) { 473 dev_err(dev, "No CXIMS for HBIG %d\n", ig); 474 return -EINVAL; 475 } 476 } 477 cxlrd->ops.hpa_to_spa = cxl_apply_xor_maps; 478 cxlrd->ops.spa_to_hpa = cxl_apply_xor_maps; 479 } 480 481 cxl_setup_extended_linear_cache(cxlrd); 482 483 cxlrd->qos_class = cfmws->qtg_id; 484 485 rc = cxl_decoder_add(cxld); 486 if (rc) 487 return rc; 488 489 rc = cxl_root_decoder_autoremove(dev, no_free_ptr(cxlrd)); 490 if (rc) 491 return rc; 492 493 dev_dbg(root_port->dev.parent, "%s added to %s\n", 494 dev_name(&cxld->dev), dev_name(&root_port->dev)); 495 496 return 0; 497 } 498 499 static int cxl_parse_cfmws(union acpi_subtable_headers *header, void *arg, 500 const unsigned long end) 501 { 502 struct acpi_cedt_cfmws *cfmws = (struct acpi_cedt_cfmws *)header; 503 struct cxl_cfmws_context *ctx = arg; 504 struct device *dev = ctx->dev; 505 int rc; 506 507 rc = __cxl_parse_cfmws(cfmws, ctx); 508 if (rc) 509 dev_err(dev, 510 "Failed to add decode range: [%#llx - %#llx] (%d)\n", 511 cfmws->base_hpa, 512 cfmws->base_hpa + cfmws->window_size - 1, rc); 513 else 514 dev_dbg(dev, "decode range: node: %d range [%#llx - %#llx]\n", 515 phys_to_target_node(cfmws->base_hpa), cfmws->base_hpa, 516 cfmws->base_hpa + cfmws->window_size - 1); 517 518 /* never fail cxl_acpi load for a single window failure */ 519 return 0; 520 } 521 522 __mock struct acpi_device *to_cxl_host_bridge(struct device *host, 523 struct device *dev) 524 { 525 struct acpi_device *adev = to_acpi_device(dev); 526 527 if (!acpi_pci_find_root(adev->handle)) 528 return NULL; 529 530 if (strcmp(acpi_device_hid(adev), "ACPI0016") == 0) 531 return adev; 532 return NULL; 533 } 534 535 /* Note, @dev is used by mock_acpi_table_parse_cedt() */ 536 struct cxl_chbs_context { 537 struct device *dev; 538 unsigned long long uid; 539 resource_size_t base; 540 u32 cxl_version; 541 int nr_versions; 542 u32 saved_version; 543 }; 544 545 static int cxl_get_chbs_iter(union acpi_subtable_headers *header, void *arg, 546 const unsigned long end) 547 { 548 struct cxl_chbs_context *ctx = arg; 549 struct acpi_cedt_chbs *chbs; 550 551 chbs = (struct acpi_cedt_chbs *) header; 552 553 if (chbs->cxl_version == ACPI_CEDT_CHBS_VERSION_CXL11 && 554 chbs->length != ACPI_CEDT_CHBS_LENGTH_CXL11) 555 return 0; 556 557 if (chbs->cxl_version == ACPI_CEDT_CHBS_VERSION_CXL20 && 558 chbs->length != ACPI_CEDT_CHBS_LENGTH_CXL20) 559 return 0; 560 561 if (!chbs->base) 562 return 0; 563 564 if (ctx->saved_version != chbs->cxl_version) { 565 /* 566 * cxl_version cannot be overwritten before the next two 567 * checks, then use saved_version 568 */ 569 ctx->saved_version = chbs->cxl_version; 570 ctx->nr_versions++; 571 } 572 573 if (ctx->base != CXL_RESOURCE_NONE) 574 return 0; 575 576 if (ctx->uid != chbs->uid) 577 return 0; 578 579 ctx->cxl_version = chbs->cxl_version; 580 ctx->base = chbs->base; 581 582 return 0; 583 } 584 585 static int cxl_get_chbs(struct device *dev, struct acpi_device *hb, 586 struct cxl_chbs_context *ctx) 587 { 588 unsigned long long uid; 589 int rc; 590 591 rc = acpi_evaluate_integer(hb->handle, METHOD_NAME__UID, NULL, &uid); 592 if (rc != AE_OK) { 593 dev_err(dev, "unable to retrieve _UID\n"); 594 return -ENOENT; 595 } 596 597 dev_dbg(dev, "UID found: %lld\n", uid); 598 *ctx = (struct cxl_chbs_context) { 599 .dev = dev, 600 .uid = uid, 601 .base = CXL_RESOURCE_NONE, 602 .cxl_version = UINT_MAX, 603 .saved_version = UINT_MAX, 604 }; 605 606 acpi_table_parse_cedt(ACPI_CEDT_TYPE_CHBS, cxl_get_chbs_iter, ctx); 607 608 if (ctx->nr_versions > 1) { 609 /* 610 * Disclaim eRCD support given some component register may 611 * only be found via CHBCR 612 */ 613 dev_info(dev, "Unsupported platform config, mixed Virtual Host and Restricted CXL Host hierarchy."); 614 } 615 616 return 0; 617 } 618 619 static int get_genport_coordinates(struct device *dev, struct cxl_dport *dport) 620 { 621 struct acpi_device *hb = to_cxl_host_bridge(NULL, dev); 622 u32 uid; 623 624 if (kstrtou32(acpi_device_uid(hb), 0, &uid)) 625 return -EINVAL; 626 627 return acpi_get_genport_coordinates(uid, dport->coord); 628 } 629 630 static int add_host_bridge_dport(struct device *match, void *arg) 631 { 632 int ret; 633 acpi_status rc; 634 struct device *bridge; 635 struct cxl_dport *dport; 636 struct cxl_chbs_context ctx; 637 struct acpi_pci_root *pci_root; 638 struct cxl_port *root_port = arg; 639 struct device *host = root_port->dev.parent; 640 struct acpi_device *hb = to_cxl_host_bridge(host, match); 641 642 if (!hb) 643 return 0; 644 645 rc = cxl_get_chbs(match, hb, &ctx); 646 if (rc) 647 return rc; 648 649 if (ctx.cxl_version == UINT_MAX) { 650 dev_warn(match, "No CHBS found for Host Bridge (UID %lld)\n", 651 ctx.uid); 652 return 0; 653 } 654 655 if (ctx.base == CXL_RESOURCE_NONE) { 656 dev_warn(match, "CHBS invalid for Host Bridge (UID %lld)\n", 657 ctx.uid); 658 return 0; 659 } 660 661 pci_root = acpi_pci_find_root(hb->handle); 662 bridge = pci_root->bus->bridge; 663 664 /* 665 * In RCH mode, bind the component regs base to the dport. In 666 * VH mode it will be bound to the CXL host bridge's port 667 * object later in add_host_bridge_uport(). 668 */ 669 if (ctx.cxl_version == ACPI_CEDT_CHBS_VERSION_CXL11) { 670 dev_dbg(match, "RCRB found for UID %lld: %pa\n", ctx.uid, 671 &ctx.base); 672 dport = devm_cxl_add_rch_dport(root_port, bridge, ctx.uid, 673 ctx.base); 674 } else { 675 dport = devm_cxl_add_dport(root_port, bridge, ctx.uid, 676 CXL_RESOURCE_NONE); 677 } 678 679 if (IS_ERR(dport)) 680 return PTR_ERR(dport); 681 682 ret = get_genport_coordinates(match, dport); 683 if (ret) 684 dev_dbg(match, "Failed to get generic port perf coordinates.\n"); 685 686 return 0; 687 } 688 689 /* 690 * A host bridge is a dport to a CFMWS decode and it is a uport to the 691 * dport (PCIe Root Ports) in the host bridge. 692 */ 693 static int add_host_bridge_uport(struct device *match, void *arg) 694 { 695 struct cxl_port *root_port = arg; 696 struct device *host = root_port->dev.parent; 697 struct acpi_device *hb = to_cxl_host_bridge(host, match); 698 struct acpi_pci_root *pci_root; 699 struct cxl_dport *dport; 700 struct cxl_port *port; 701 struct device *bridge; 702 struct cxl_chbs_context ctx; 703 resource_size_t component_reg_phys; 704 int rc; 705 706 if (!hb) 707 return 0; 708 709 pci_root = acpi_pci_find_root(hb->handle); 710 bridge = pci_root->bus->bridge; 711 dport = cxl_find_dport_by_dev(root_port, bridge); 712 if (!dport) { 713 dev_dbg(host, "host bridge expected and not found\n"); 714 return 0; 715 } 716 717 if (dport->rch) { 718 dev_info(bridge, "host supports CXL (restricted)\n"); 719 return 0; 720 } 721 722 rc = cxl_get_chbs(match, hb, &ctx); 723 if (rc) 724 return rc; 725 726 if (ctx.cxl_version == ACPI_CEDT_CHBS_VERSION_CXL11) { 727 dev_warn(bridge, 728 "CXL CHBS version mismatch, skip port registration\n"); 729 return 0; 730 } 731 732 component_reg_phys = ctx.base; 733 if (component_reg_phys != CXL_RESOURCE_NONE) 734 dev_dbg(match, "CHBCR found for UID %lld: %pa\n", 735 ctx.uid, &component_reg_phys); 736 737 rc = devm_cxl_register_pci_bus(host, bridge, pci_root->bus); 738 if (rc) 739 return rc; 740 741 port = devm_cxl_add_port(host, bridge, component_reg_phys, dport); 742 if (IS_ERR(port)) 743 return PTR_ERR(port); 744 745 dev_info(bridge, "host supports CXL\n"); 746 747 return 0; 748 } 749 750 static int add_root_nvdimm_bridge(struct device *match, void *data) 751 { 752 struct cxl_decoder *cxld; 753 struct cxl_port *root_port = data; 754 struct cxl_nvdimm_bridge *cxl_nvb; 755 struct device *host = root_port->dev.parent; 756 757 if (!is_root_decoder(match)) 758 return 0; 759 760 cxld = to_cxl_decoder(match); 761 if (!(cxld->flags & CXL_DECODER_F_PMEM)) 762 return 0; 763 764 cxl_nvb = devm_cxl_add_nvdimm_bridge(host, root_port); 765 if (IS_ERR(cxl_nvb)) { 766 dev_dbg(host, "failed to register pmem\n"); 767 return PTR_ERR(cxl_nvb); 768 } 769 dev_dbg(host, "%s: add: %s\n", dev_name(&root_port->dev), 770 dev_name(&cxl_nvb->dev)); 771 return 1; 772 } 773 774 static struct lock_class_key cxl_root_key; 775 776 static void cxl_acpi_lock_reset_class(void *dev) 777 { 778 device_lock_reset_class(dev); 779 } 780 781 static void cxl_set_public_resource(struct resource *priv, struct resource *pub) 782 { 783 priv->desc = (unsigned long) pub; 784 } 785 786 static struct resource *cxl_get_public_resource(struct resource *priv) 787 { 788 return (struct resource *) priv->desc; 789 } 790 791 static void remove_cxl_resources(void *data) 792 { 793 struct resource *res, *next, *cxl = data; 794 795 for (res = cxl->child; res; res = next) { 796 struct resource *victim = cxl_get_public_resource(res); 797 798 next = res->sibling; 799 remove_resource(res); 800 801 if (victim) { 802 remove_resource(victim); 803 kfree(victim); 804 } 805 806 del_cxl_resource(res); 807 } 808 } 809 810 /** 811 * add_cxl_resources() - reflect CXL fixed memory windows in iomem_resource 812 * @cxl_res: A standalone resource tree where each CXL window is a sibling 813 * 814 * Walk each CXL window in @cxl_res and add it to iomem_resource potentially 815 * expanding its boundaries to ensure that any conflicting resources become 816 * children. If a window is expanded it may then conflict with a another window 817 * entry and require the window to be truncated or trimmed. Consider this 818 * situation:: 819 * 820 * |-- "CXL Window 0" --||----- "CXL Window 1" -----| 821 * |--------------- "System RAM" -------------| 822 * 823 * ...where platform firmware has established as System RAM resource across 2 824 * windows, but has left some portion of window 1 for dynamic CXL region 825 * provisioning. In this case "Window 0" will span the entirety of the "System 826 * RAM" span, and "CXL Window 1" is truncated to the remaining tail past the end 827 * of that "System RAM" resource. 828 */ 829 static int add_cxl_resources(struct resource *cxl_res) 830 { 831 struct resource *res, *new, *next; 832 833 for (res = cxl_res->child; res; res = next) { 834 new = kzalloc_obj(*new); 835 if (!new) 836 return -ENOMEM; 837 new->name = res->name; 838 new->start = res->start; 839 new->end = res->end; 840 new->flags = IORESOURCE_MEM; 841 new->desc = IORES_DESC_CXL; 842 843 /* 844 * Record the public resource in the private cxl_res tree for 845 * later removal. 846 */ 847 cxl_set_public_resource(res, new); 848 849 insert_resource_expand_to_fit(&iomem_resource, new); 850 851 next = res->sibling; 852 while (next && resource_overlaps(new, next)) { 853 if (resource_contains(new, next)) { 854 struct resource *_next = next->sibling; 855 856 remove_resource(next); 857 del_cxl_resource(next); 858 next = _next; 859 } else 860 next->start = new->end + 1; 861 } 862 } 863 return 0; 864 } 865 866 static int pair_cxl_resource(struct device *dev, void *data) 867 { 868 struct resource *cxl_res = data; 869 struct resource *p; 870 871 if (!is_root_decoder(dev)) 872 return 0; 873 874 for (p = cxl_res->child; p; p = p->sibling) { 875 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev); 876 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld; 877 struct resource res = { 878 .start = cxld->hpa_range.start, 879 .end = cxld->hpa_range.end, 880 .flags = IORESOURCE_MEM, 881 }; 882 883 if (resource_contains(p, &res)) { 884 cxlrd->res = cxl_get_public_resource(p); 885 break; 886 } 887 } 888 889 return 0; 890 } 891 892 static int cxl_acpi_probe(struct platform_device *pdev) 893 { 894 struct cxl_cfmws_context ctx; 895 struct acpi_device *adev; 896 struct resource *cxl_res; 897 struct cxl_root *cxl_root; 898 struct cxl_port *root_port; 899 struct device *host = &pdev->dev; 900 int rc; 901 902 adev = ACPI_COMPANION(host); 903 if (!adev) 904 return -ENODEV; 905 906 device_lock_set_class(&pdev->dev, &cxl_root_key); 907 rc = devm_add_action_or_reset(&pdev->dev, cxl_acpi_lock_reset_class, 908 &pdev->dev); 909 if (rc) 910 return rc; 911 912 cxl_res = devm_kzalloc(host, sizeof(*cxl_res), GFP_KERNEL); 913 if (!cxl_res) 914 return -ENOMEM; 915 cxl_res->name = "CXL mem"; 916 cxl_res->start = 0; 917 cxl_res->end = -1; 918 cxl_res->flags = IORESOURCE_MEM; 919 920 cxl_root = devm_cxl_add_root(host); 921 if (IS_ERR(cxl_root)) 922 return PTR_ERR(cxl_root); 923 cxl_root->ops.qos_class = cxl_acpi_qos_class; 924 root_port = &cxl_root->port; 925 926 cxl_setup_prm_address_translation(cxl_root); 927 928 rc = bus_for_each_dev(adev->dev.bus, NULL, root_port, 929 add_host_bridge_dport); 930 if (rc < 0) 931 return rc; 932 933 rc = devm_add_action_or_reset(host, remove_cxl_resources, cxl_res); 934 if (rc) 935 return rc; 936 937 ctx = (struct cxl_cfmws_context) { 938 .dev = host, 939 .root_port = root_port, 940 .cxl_res = cxl_res, 941 }; 942 rc = acpi_table_parse_cedt(ACPI_CEDT_TYPE_CFMWS, cxl_parse_cfmws, &ctx); 943 if (rc < 0) 944 return -ENXIO; 945 946 rc = add_cxl_resources(cxl_res); 947 if (rc) 948 return rc; 949 950 /* 951 * Populate the root decoders with their related iomem resource, 952 * if present 953 */ 954 device_for_each_child(&root_port->dev, cxl_res, pair_cxl_resource); 955 956 /* 957 * Root level scanned with host-bridge as dports, now scan host-bridges 958 * for their role as CXL uports to their CXL-capable PCIe Root Ports. 959 */ 960 rc = bus_for_each_dev(adev->dev.bus, NULL, root_port, 961 add_host_bridge_uport); 962 if (rc < 0) 963 return rc; 964 965 if (IS_ENABLED(CONFIG_CXL_PMEM)) 966 rc = device_for_each_child(&root_port->dev, root_port, 967 add_root_nvdimm_bridge); 968 if (rc < 0) 969 return rc; 970 971 /* In case PCI is scanned before ACPI re-trigger memdev attach */ 972 cxl_bus_rescan(); 973 return 0; 974 } 975 976 static const struct acpi_device_id cxl_acpi_ids[] = { 977 { "ACPI0017" }, 978 { }, 979 }; 980 MODULE_DEVICE_TABLE(acpi, cxl_acpi_ids); 981 982 static const struct platform_device_id cxl_test_ids[] = { 983 { "cxl_acpi" }, 984 { }, 985 }; 986 MODULE_DEVICE_TABLE(platform, cxl_test_ids); 987 988 static struct platform_driver cxl_acpi_driver = { 989 .probe = cxl_acpi_probe, 990 .driver = { 991 .name = KBUILD_MODNAME, 992 .acpi_match_table = cxl_acpi_ids, 993 }, 994 .id_table = cxl_test_ids, 995 }; 996 997 static int __init cxl_acpi_init(void) 998 { 999 return platform_driver_register(&cxl_acpi_driver); 1000 } 1001 1002 static void __exit cxl_acpi_exit(void) 1003 { 1004 platform_driver_unregister(&cxl_acpi_driver); 1005 cxl_bus_drain(); 1006 } 1007 1008 /* 1009 * Load before dax_hmem sees 'Soft Reserved' CXL ranges. Use 1010 * subsys_initcall_sync() since there is an order dependency with 1011 * subsys_initcall(efisubsys_init), which must run first. 1012 */ 1013 subsys_initcall_sync(cxl_acpi_init); 1014 1015 /* 1016 * Arrange for host-bridge ports to be active synchronous with 1017 * cxl_acpi_probe() exit. 1018 */ 1019 MODULE_SOFTDEP("pre: cxl_port"); 1020 1021 module_exit(cxl_acpi_exit); 1022 MODULE_DESCRIPTION("CXL ACPI: Platform Support"); 1023 MODULE_LICENSE("GPL v2"); 1024 MODULE_IMPORT_NS("CXL"); 1025 MODULE_IMPORT_NS("ACPI"); 1026