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>
13 #define CXL_RCRB_SIZE SZ_8K
15 struct cxl_cxims_data {
21 * Find a targets entry (n) in the host bridge interleave list.
22 * CXL Specification 3.0 Table 9-22
24 static int cxl_xor_calc_n(u64 hpa, struct cxl_cxims_data *cximsd, int iw,
30 /* IW: 2,4,6,8,12,16 begin building 'n' using xormaps */
32 for (i = 0; i < cximsd->nr_maps; i++)
33 n |= (hweight64(hpa & cximsd->xormaps[i]) & 1) << i;
35 /* IW: 3,6,12 add a modulo calculation to 'n' */
36 if (!is_power_of_2(iw)) {
37 if (ways_to_eiw(iw, &eiw))
39 hpa &= GENMASK_ULL(51, eiw + ig);
40 n |= do_div(hpa, 3) << i;
45 static struct cxl_dport *cxl_hb_xor(struct cxl_root_decoder *cxlrd, int pos)
47 struct cxl_cxims_data *cximsd = cxlrd->platform_data;
48 struct cxl_switch_decoder *cxlsd = &cxlrd->cxlsd;
49 struct cxl_decoder *cxld = &cxlsd->cxld;
50 int ig = cxld->interleave_granularity;
51 int iw = cxld->interleave_ways;
55 if (dev_WARN_ONCE(&cxld->dev,
56 cxld->interleave_ways != cxlsd->nr_targets,
57 "misconfigured root decoder\n"))
60 hpa = cxlrd->res->start + pos * ig;
62 /* Entry (n) is 0 for no interleave (iw == 1) */
64 n = cxl_xor_calc_n(hpa, cximsd, iw, ig);
69 return cxlrd->cxlsd.target[n];
72 struct cxl_cxims_context {
74 struct cxl_root_decoder *cxlrd;
77 static int cxl_parse_cxims(union acpi_subtable_headers *header, void *arg,
78 const unsigned long end)
80 struct acpi_cedt_cxims *cxims = (struct acpi_cedt_cxims *)header;
81 struct cxl_cxims_context *ctx = arg;
82 struct cxl_root_decoder *cxlrd = ctx->cxlrd;
83 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
84 struct device *dev = ctx->dev;
85 struct cxl_cxims_data *cximsd;
86 unsigned int hbig, nr_maps;
89 rc = eig_to_granularity(cxims->hbig, &hbig);
93 /* Does this CXIMS entry apply to the given CXL Window? */
94 if (hbig != cxld->interleave_granularity)
97 /* IW 1,3 do not use xormaps and skip this parsing entirely */
98 if (is_power_of_2(cxld->interleave_ways))
100 nr_maps = ilog2(cxld->interleave_ways);
103 nr_maps = ilog2(cxld->interleave_ways / 3);
105 if (cxims->nr_xormaps < nr_maps) {
106 dev_dbg(dev, "CXIMS nr_xormaps[%d] expected[%d]\n",
107 cxims->nr_xormaps, nr_maps);
111 cximsd = devm_kzalloc(dev, struct_size(cximsd, xormaps, nr_maps),
115 memcpy(cximsd->xormaps, cxims->xormap_list,
116 nr_maps * sizeof(*cximsd->xormaps));
117 cximsd->nr_maps = nr_maps;
118 cxlrd->platform_data = cximsd;
123 static unsigned long cfmws_to_decoder_flags(int restrictions)
125 unsigned long flags = CXL_DECODER_F_ENABLE;
127 if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_TYPE2)
128 flags |= CXL_DECODER_F_TYPE2;
129 if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_TYPE3)
130 flags |= CXL_DECODER_F_TYPE3;
131 if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_VOLATILE)
132 flags |= CXL_DECODER_F_RAM;
133 if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_PMEM)
134 flags |= CXL_DECODER_F_PMEM;
135 if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_FIXED)
136 flags |= CXL_DECODER_F_LOCK;
141 static int cxl_acpi_cfmws_verify(struct device *dev,
142 struct acpi_cedt_cfmws *cfmws)
144 int rc, expected_len;
147 if (cfmws->interleave_arithmetic != ACPI_CEDT_CFMWS_ARITHMETIC_MODULO &&
148 cfmws->interleave_arithmetic != ACPI_CEDT_CFMWS_ARITHMETIC_XOR) {
149 dev_err(dev, "CFMWS Unknown Interleave Arithmetic: %d\n",
150 cfmws->interleave_arithmetic);
154 if (!IS_ALIGNED(cfmws->base_hpa, SZ_256M)) {
155 dev_err(dev, "CFMWS Base HPA not 256MB aligned\n");
159 if (!IS_ALIGNED(cfmws->window_size, SZ_256M)) {
160 dev_err(dev, "CFMWS Window Size not 256MB aligned\n");
164 rc = eiw_to_ways(cfmws->interleave_ways, &ways);
166 dev_err(dev, "CFMWS Interleave Ways (%d) invalid\n",
167 cfmws->interleave_ways);
171 expected_len = struct_size(cfmws, interleave_targets, ways);
173 if (cfmws->header.length < expected_len) {
174 dev_err(dev, "CFMWS length %d less than expected %d\n",
175 cfmws->header.length, expected_len);
179 if (cfmws->header.length > expected_len)
180 dev_dbg(dev, "CFMWS length %d greater than expected %d\n",
181 cfmws->header.length, expected_len);
187 * Note, @dev must be the first member, see 'struct cxl_chbs_context'
188 * and mock_acpi_table_parse_cedt()
190 struct cxl_cfmws_context {
192 struct cxl_port *root_port;
193 struct resource *cxl_res;
197 static int cxl_parse_cfmws(union acpi_subtable_headers *header, void *arg,
198 const unsigned long end)
200 int target_map[CXL_DECODER_MAX_INTERLEAVE];
201 struct cxl_cfmws_context *ctx = arg;
202 struct cxl_port *root_port = ctx->root_port;
203 struct resource *cxl_res = ctx->cxl_res;
204 struct cxl_cxims_context cxims_ctx;
205 struct cxl_root_decoder *cxlrd;
206 struct device *dev = ctx->dev;
207 struct acpi_cedt_cfmws *cfmws;
208 cxl_calc_hb_fn cxl_calc_hb;
209 struct cxl_decoder *cxld;
210 unsigned int ways, i, ig;
211 struct resource *res;
214 cfmws = (struct acpi_cedt_cfmws *) header;
216 rc = cxl_acpi_cfmws_verify(dev, cfmws);
218 dev_err(dev, "CFMWS range %#llx-%#llx not registered\n",
220 cfmws->base_hpa + cfmws->window_size - 1);
224 rc = eiw_to_ways(cfmws->interleave_ways, &ways);
227 rc = eig_to_granularity(cfmws->granularity, &ig);
230 for (i = 0; i < ways; i++)
231 target_map[i] = cfmws->interleave_targets[i];
233 res = kzalloc(sizeof(*res), GFP_KERNEL);
237 res->name = kasprintf(GFP_KERNEL, "CXL Window %d", ctx->id++);
241 res->start = cfmws->base_hpa;
242 res->end = cfmws->base_hpa + cfmws->window_size - 1;
243 res->flags = IORESOURCE_MEM;
245 /* add to the local resource tracking to establish a sort order */
246 rc = insert_resource(cxl_res, res);
250 if (cfmws->interleave_arithmetic == ACPI_CEDT_CFMWS_ARITHMETIC_MODULO)
251 cxl_calc_hb = cxl_hb_modulo;
253 cxl_calc_hb = cxl_hb_xor;
255 cxlrd = cxl_root_decoder_alloc(root_port, ways, cxl_calc_hb);
259 cxld = &cxlrd->cxlsd.cxld;
260 cxld->flags = cfmws_to_decoder_flags(cfmws->restrictions);
261 cxld->target_type = CXL_DECODER_EXPANDER;
262 cxld->hpa_range = (struct range) {
266 cxld->interleave_ways = ways;
268 * Minimize the x1 granularity to advertise support for any
269 * valid region granularity
272 ig = CXL_DECODER_MIN_GRANULARITY;
273 cxld->interleave_granularity = ig;
275 if (cfmws->interleave_arithmetic == ACPI_CEDT_CFMWS_ARITHMETIC_XOR) {
276 if (ways != 1 && ways != 3) {
277 cxims_ctx = (struct cxl_cxims_context) {
281 rc = acpi_table_parse_cedt(ACPI_CEDT_TYPE_CXIMS,
282 cxl_parse_cxims, &cxims_ctx);
285 if (!cxlrd->platform_data) {
286 dev_err(dev, "No CXIMS for HBIG %u\n", ig);
292 rc = cxl_decoder_add(cxld, target_map);
295 put_device(&cxld->dev);
297 rc = cxl_decoder_autoremove(dev, cxld);
299 dev_err(dev, "Failed to add decode range [%#llx - %#llx]\n",
300 cxld->hpa_range.start, cxld->hpa_range.end);
303 dev_dbg(dev, "add: %s node: %d range [%#llx - %#llx]\n",
304 dev_name(&cxld->dev),
305 phys_to_target_node(cxld->hpa_range.start),
306 cxld->hpa_range.start, cxld->hpa_range.end);
317 __mock struct acpi_device *to_cxl_host_bridge(struct device *host,
320 struct acpi_device *adev = to_acpi_device(dev);
322 if (!acpi_pci_find_root(adev->handle))
325 if (strcmp(acpi_device_hid(adev), "ACPI0016") == 0)
331 * A host bridge is a dport to a CFMWS decode and it is a uport to the
332 * dport (PCIe Root Ports) in the host bridge.
334 static int add_host_bridge_uport(struct device *match, void *arg)
336 struct cxl_port *root_port = arg;
337 struct device *host = root_port->dev.parent;
338 struct acpi_device *hb = to_cxl_host_bridge(host, match);
339 struct acpi_pci_root *pci_root;
340 struct cxl_dport *dport;
341 struct cxl_port *port;
342 struct device *bridge;
348 pci_root = acpi_pci_find_root(hb->handle);
349 bridge = pci_root->bus->bridge;
350 dport = cxl_find_dport_by_dev(root_port, bridge);
352 dev_dbg(host, "host bridge expected and not found\n");
357 dev_info(bridge, "host supports CXL (restricted)\n");
361 rc = devm_cxl_register_pci_bus(host, bridge, pci_root->bus);
365 port = devm_cxl_add_port(host, bridge, dport->component_reg_phys,
368 return PTR_ERR(port);
370 dev_info(bridge, "host supports CXL\n");
375 struct cxl_chbs_context {
377 unsigned long long uid;
378 resource_size_t rcrb;
379 resource_size_t chbcr;
383 static int cxl_get_chbcr(union acpi_subtable_headers *header, void *arg,
384 const unsigned long end)
386 struct cxl_chbs_context *ctx = arg;
387 struct acpi_cedt_chbs *chbs;
392 chbs = (struct acpi_cedt_chbs *) header;
394 if (ctx->uid != chbs->uid)
397 ctx->cxl_version = chbs->cxl_version;
398 ctx->rcrb = CXL_RESOURCE_NONE;
399 ctx->chbcr = CXL_RESOURCE_NONE;
404 if (chbs->cxl_version != ACPI_CEDT_CHBS_VERSION_CXL11) {
405 ctx->chbcr = chbs->base;
409 if (chbs->length != CXL_RCRB_SIZE)
412 ctx->rcrb = chbs->base;
413 ctx->chbcr = cxl_rcrb_to_component(ctx->dev, chbs->base,
414 CXL_RCRB_DOWNSTREAM);
419 static int add_host_bridge_dport(struct device *match, void *arg)
422 struct device *bridge;
423 unsigned long long uid;
424 struct cxl_dport *dport;
425 struct cxl_chbs_context ctx;
426 struct acpi_pci_root *pci_root;
427 struct cxl_port *root_port = arg;
428 struct device *host = root_port->dev.parent;
429 struct acpi_device *hb = to_cxl_host_bridge(host, match);
434 rc = acpi_evaluate_integer(hb->handle, METHOD_NAME__UID, NULL, &uid);
436 dev_err(match, "unable to retrieve _UID\n");
440 dev_dbg(match, "UID found: %lld\n", uid);
442 ctx = (struct cxl_chbs_context) {
446 acpi_table_parse_cedt(ACPI_CEDT_TYPE_CHBS, cxl_get_chbcr, &ctx);
449 dev_warn(match, "No CHBS found for Host Bridge (UID %lld)\n",
454 if (ctx.rcrb != CXL_RESOURCE_NONE)
455 dev_dbg(match, "RCRB found for UID %lld: %pa\n", uid, &ctx.rcrb);
457 if (ctx.chbcr == CXL_RESOURCE_NONE) {
458 dev_warn(match, "CHBCR invalid for Host Bridge (UID %lld)\n",
463 dev_dbg(match, "CHBCR found: %pa\n", &ctx.chbcr);
465 pci_root = acpi_pci_find_root(hb->handle);
466 bridge = pci_root->bus->bridge;
467 if (ctx.cxl_version == ACPI_CEDT_CHBS_VERSION_CXL11)
468 dport = devm_cxl_add_rch_dport(root_port, bridge, uid,
469 ctx.chbcr, ctx.rcrb);
471 dport = devm_cxl_add_dport(root_port, bridge, uid,
474 return PTR_ERR(dport);
479 static int add_root_nvdimm_bridge(struct device *match, void *data)
481 struct cxl_decoder *cxld;
482 struct cxl_port *root_port = data;
483 struct cxl_nvdimm_bridge *cxl_nvb;
484 struct device *host = root_port->dev.parent;
486 if (!is_root_decoder(match))
489 cxld = to_cxl_decoder(match);
490 if (!(cxld->flags & CXL_DECODER_F_PMEM))
493 cxl_nvb = devm_cxl_add_nvdimm_bridge(host, root_port);
494 if (IS_ERR(cxl_nvb)) {
495 dev_dbg(host, "failed to register pmem\n");
496 return PTR_ERR(cxl_nvb);
498 dev_dbg(host, "%s: add: %s\n", dev_name(&root_port->dev),
499 dev_name(&cxl_nvb->dev));
503 static struct lock_class_key cxl_root_key;
505 static void cxl_acpi_lock_reset_class(void *dev)
507 device_lock_reset_class(dev);
510 static void del_cxl_resource(struct resource *res)
516 static void cxl_set_public_resource(struct resource *priv, struct resource *pub)
518 priv->desc = (unsigned long) pub;
521 static struct resource *cxl_get_public_resource(struct resource *priv)
523 return (struct resource *) priv->desc;
526 static void remove_cxl_resources(void *data)
528 struct resource *res, *next, *cxl = data;
530 for (res = cxl->child; res; res = next) {
531 struct resource *victim = cxl_get_public_resource(res);
534 remove_resource(res);
537 remove_resource(victim);
541 del_cxl_resource(res);
546 * add_cxl_resources() - reflect CXL fixed memory windows in iomem_resource
547 * @cxl_res: A standalone resource tree where each CXL window is a sibling
549 * Walk each CXL window in @cxl_res and add it to iomem_resource potentially
550 * expanding its boundaries to ensure that any conflicting resources become
551 * children. If a window is expanded it may then conflict with a another window
552 * entry and require the window to be truncated or trimmed. Consider this
555 * |-- "CXL Window 0" --||----- "CXL Window 1" -----|
556 * |--------------- "System RAM" -------------|
558 * ...where platform firmware has established as System RAM resource across 2
559 * windows, but has left some portion of window 1 for dynamic CXL region
560 * provisioning. In this case "Window 0" will span the entirety of the "System
561 * RAM" span, and "CXL Window 1" is truncated to the remaining tail past the end
562 * of that "System RAM" resource.
564 static int add_cxl_resources(struct resource *cxl_res)
566 struct resource *res, *new, *next;
568 for (res = cxl_res->child; res; res = next) {
569 new = kzalloc(sizeof(*new), GFP_KERNEL);
572 new->name = res->name;
573 new->start = res->start;
575 new->flags = IORESOURCE_MEM;
576 new->desc = IORES_DESC_CXL;
579 * Record the public resource in the private cxl_res tree for
582 cxl_set_public_resource(res, new);
584 insert_resource_expand_to_fit(&iomem_resource, new);
587 while (next && resource_overlaps(new, next)) {
588 if (resource_contains(new, next)) {
589 struct resource *_next = next->sibling;
591 remove_resource(next);
592 del_cxl_resource(next);
595 next->start = new->end + 1;
601 static int pair_cxl_resource(struct device *dev, void *data)
603 struct resource *cxl_res = data;
606 if (!is_root_decoder(dev))
609 for (p = cxl_res->child; p; p = p->sibling) {
610 struct cxl_root_decoder *cxlrd = to_cxl_root_decoder(dev);
611 struct cxl_decoder *cxld = &cxlrd->cxlsd.cxld;
612 struct resource res = {
613 .start = cxld->hpa_range.start,
614 .end = cxld->hpa_range.end,
615 .flags = IORESOURCE_MEM,
618 if (resource_contains(p, &res)) {
619 cxlrd->res = cxl_get_public_resource(p);
627 static int cxl_acpi_probe(struct platform_device *pdev)
630 struct resource *cxl_res;
631 struct cxl_port *root_port;
632 struct device *host = &pdev->dev;
633 struct acpi_device *adev = ACPI_COMPANION(host);
634 struct cxl_cfmws_context ctx;
636 device_lock_set_class(&pdev->dev, &cxl_root_key);
637 rc = devm_add_action_or_reset(&pdev->dev, cxl_acpi_lock_reset_class,
642 cxl_res = devm_kzalloc(host, sizeof(*cxl_res), GFP_KERNEL);
645 cxl_res->name = "CXL mem";
648 cxl_res->flags = IORESOURCE_MEM;
650 root_port = devm_cxl_add_port(host, host, CXL_RESOURCE_NONE, NULL);
651 if (IS_ERR(root_port))
652 return PTR_ERR(root_port);
654 rc = bus_for_each_dev(adev->dev.bus, NULL, root_port,
655 add_host_bridge_dport);
659 rc = devm_add_action_or_reset(host, remove_cxl_resources, cxl_res);
663 ctx = (struct cxl_cfmws_context) {
665 .root_port = root_port,
668 rc = acpi_table_parse_cedt(ACPI_CEDT_TYPE_CFMWS, cxl_parse_cfmws, &ctx);
672 rc = add_cxl_resources(cxl_res);
677 * Populate the root decoders with their related iomem resource,
680 device_for_each_child(&root_port->dev, cxl_res, pair_cxl_resource);
683 * Root level scanned with host-bridge as dports, now scan host-bridges
684 * for their role as CXL uports to their CXL-capable PCIe Root Ports.
686 rc = bus_for_each_dev(adev->dev.bus, NULL, root_port,
687 add_host_bridge_uport);
691 if (IS_ENABLED(CONFIG_CXL_PMEM))
692 rc = device_for_each_child(&root_port->dev, root_port,
693 add_root_nvdimm_bridge);
697 /* In case PCI is scanned before ACPI re-trigger memdev attach */
702 static const struct acpi_device_id cxl_acpi_ids[] = {
706 MODULE_DEVICE_TABLE(acpi, cxl_acpi_ids);
708 static const struct platform_device_id cxl_test_ids[] = {
712 MODULE_DEVICE_TABLE(platform, cxl_test_ids);
714 static struct platform_driver cxl_acpi_driver = {
715 .probe = cxl_acpi_probe,
717 .name = KBUILD_MODNAME,
718 .acpi_match_table = cxl_acpi_ids,
720 .id_table = cxl_test_ids,
723 static int __init cxl_acpi_init(void)
725 return platform_driver_register(&cxl_acpi_driver);
728 static void __exit cxl_acpi_exit(void)
730 platform_driver_unregister(&cxl_acpi_driver);
734 /* load before dax_hmem sees 'Soft Reserved' CXL ranges */
735 subsys_initcall(cxl_acpi_init);
736 module_exit(cxl_acpi_exit);
737 MODULE_LICENSE("GPL v2");
738 MODULE_IMPORT_NS(CXL);
739 MODULE_IMPORT_NS(ACPI);