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>
11 static struct acpi_table_header *acpi_cedt;
13 /* Encode defined in CXL 2.0 8.2.5.12.7 HDM Decoder Control Register */
14 #define CFMWS_INTERLEAVE_WAYS(x) (1 << (x)->interleave_ways)
15 #define CFMWS_INTERLEAVE_GRANULARITY(x) ((x)->granularity + 8)
17 static unsigned long cfmws_to_decoder_flags(int restrictions)
19 unsigned long flags = 0;
21 if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_TYPE2)
22 flags |= CXL_DECODER_F_TYPE2;
23 if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_TYPE3)
24 flags |= CXL_DECODER_F_TYPE3;
25 if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_VOLATILE)
26 flags |= CXL_DECODER_F_RAM;
27 if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_PMEM)
28 flags |= CXL_DECODER_F_PMEM;
29 if (restrictions & ACPI_CEDT_CFMWS_RESTRICT_FIXED)
30 flags |= CXL_DECODER_F_LOCK;
35 static int cxl_acpi_cfmws_verify(struct device *dev,
36 struct acpi_cedt_cfmws *cfmws)
40 if (cfmws->interleave_arithmetic != ACPI_CEDT_CFMWS_ARITHMETIC_MODULO) {
41 dev_err(dev, "CFMWS Unsupported Interleave Arithmetic\n");
45 if (!IS_ALIGNED(cfmws->base_hpa, SZ_256M)) {
46 dev_err(dev, "CFMWS Base HPA not 256MB aligned\n");
50 if (!IS_ALIGNED(cfmws->window_size, SZ_256M)) {
51 dev_err(dev, "CFMWS Window Size not 256MB aligned\n");
55 if (CFMWS_INTERLEAVE_WAYS(cfmws) > CXL_DECODER_MAX_INTERLEAVE) {
56 dev_err(dev, "CFMWS Interleave Ways (%d) too large\n",
57 CFMWS_INTERLEAVE_WAYS(cfmws));
61 expected_len = struct_size((cfmws), interleave_targets,
62 CFMWS_INTERLEAVE_WAYS(cfmws));
64 if (cfmws->header.length < expected_len) {
65 dev_err(dev, "CFMWS length %d less than expected %d\n",
66 cfmws->header.length, expected_len);
70 if (cfmws->header.length > expected_len)
71 dev_dbg(dev, "CFMWS length %d greater than expected %d\n",
72 cfmws->header.length, expected_len);
77 static void cxl_add_cfmws_decoders(struct device *dev,
78 struct cxl_port *root_port)
80 int target_map[CXL_DECODER_MAX_INTERLEAVE];
81 struct acpi_cedt_cfmws *cfmws;
82 struct cxl_decoder *cxld;
83 acpi_size len, cur = 0;
87 len = acpi_cedt->length - sizeof(*acpi_cedt);
88 cedt_subtable = acpi_cedt + 1;
91 struct acpi_cedt_header *c = cedt_subtable + cur;
94 if (c->type != ACPI_CEDT_TYPE_CFMWS) {
99 cfmws = cedt_subtable + cur;
101 if (cfmws->header.length < sizeof(*cfmws)) {
103 "CFMWS entry skipped:invalid length:%u\n",
104 cfmws->header.length);
109 rc = cxl_acpi_cfmws_verify(dev, cfmws);
111 dev_err(dev, "CFMWS range %#llx-%#llx not registered\n",
112 cfmws->base_hpa, cfmws->base_hpa +
113 cfmws->window_size - 1);
118 for (i = 0; i < CFMWS_INTERLEAVE_WAYS(cfmws); i++)
119 target_map[i] = cfmws->interleave_targets[i];
121 cxld = cxl_decoder_alloc(root_port,
122 CFMWS_INTERLEAVE_WAYS(cfmws));
126 cxld->flags = cfmws_to_decoder_flags(cfmws->restrictions);
127 cxld->target_type = CXL_DECODER_EXPANDER;
128 cxld->range = (struct range) {
129 .start = cfmws->base_hpa,
130 .end = cfmws->base_hpa + cfmws->window_size - 1,
132 cxld->interleave_ways = CFMWS_INTERLEAVE_WAYS(cfmws);
133 cxld->interleave_granularity =
134 CFMWS_INTERLEAVE_GRANULARITY(cfmws);
136 rc = cxl_decoder_add(cxld, target_map);
138 put_device(&cxld->dev);
140 rc = cxl_decoder_autoremove(dev, cxld);
142 dev_err(dev, "Failed to add decoder for %#llx-%#llx\n",
143 cfmws->base_hpa, cfmws->base_hpa +
144 cfmws->window_size - 1);
147 dev_dbg(dev, "add: %s range %#llx-%#llx\n",
148 dev_name(&cxld->dev), cfmws->base_hpa,
149 cfmws->base_hpa + cfmws->window_size - 1);
155 static struct acpi_cedt_chbs *cxl_acpi_match_chbs(struct device *dev, u32 uid)
157 struct acpi_cedt_chbs *chbs, *chbs_match = NULL;
158 acpi_size len, cur = 0;
161 len = acpi_cedt->length - sizeof(*acpi_cedt);
162 cedt_subtable = acpi_cedt + 1;
165 struct acpi_cedt_header *c = cedt_subtable + cur;
167 if (c->type != ACPI_CEDT_TYPE_CHBS) {
172 chbs = cedt_subtable + cur;
174 if (chbs->header.length < sizeof(*chbs)) {
176 "CHBS entry skipped: invalid length:%u\n",
177 chbs->header.length);
182 if (chbs->uid != uid) {
189 "CHBS entry skipped: duplicate UID:%u\n",
199 return chbs_match ? chbs_match : ERR_PTR(-ENODEV);
202 static resource_size_t get_chbcr(struct acpi_cedt_chbs *chbs)
204 return IS_ERR(chbs) ? CXL_RESOURCE_NONE : chbs->base;
207 __mock int match_add_root_ports(struct pci_dev *pdev, void *data)
209 struct cxl_walk_context *ctx = data;
210 struct pci_bus *root_bus = ctx->root;
211 struct cxl_port *port = ctx->port;
212 int type = pci_pcie_type(pdev);
213 struct device *dev = ctx->dev;
214 u32 lnkcap, port_num;
217 if (pdev->bus != root_bus)
219 if (!pci_is_pcie(pdev))
221 if (type != PCI_EXP_TYPE_ROOT_PORT)
223 if (pci_read_config_dword(pdev, pci_pcie_cap(pdev) + PCI_EXP_LNKCAP,
224 &lnkcap) != PCIBIOS_SUCCESSFUL)
227 /* TODO walk DVSEC to find component register base */
228 port_num = FIELD_GET(PCI_EXP_LNKCAP_PN, lnkcap);
229 rc = cxl_add_dport(port, &pdev->dev, port_num, CXL_RESOURCE_NONE);
236 dev_dbg(dev, "add dport%d: %s\n", port_num, dev_name(&pdev->dev));
241 static struct cxl_dport *find_dport_by_dev(struct cxl_port *port, struct device *dev)
243 struct cxl_dport *dport;
245 device_lock(&port->dev);
246 list_for_each_entry(dport, &port->dports, list)
247 if (dport->dport == dev) {
248 device_unlock(&port->dev);
252 device_unlock(&port->dev);
256 __mock struct acpi_device *to_cxl_host_bridge(struct device *host,
259 struct acpi_device *adev = to_acpi_device(dev);
261 if (!acpi_pci_find_root(adev->handle))
264 if (strcmp(acpi_device_hid(adev), "ACPI0016") == 0)
270 * A host bridge is a dport to a CFMWS decode and it is a uport to the
271 * dport (PCIe Root Ports) in the host bridge.
273 static int add_host_bridge_uport(struct device *match, void *arg)
275 struct cxl_port *root_port = arg;
276 struct device *host = root_port->dev.parent;
277 struct acpi_device *bridge = to_cxl_host_bridge(host, match);
278 struct acpi_pci_root *pci_root;
279 struct cxl_walk_context ctx;
280 int single_port_map[1], rc;
281 struct cxl_decoder *cxld;
282 struct cxl_dport *dport;
283 struct cxl_port *port;
288 dport = find_dport_by_dev(root_port, match);
290 dev_dbg(host, "host bridge expected and not found\n");
294 port = devm_cxl_add_port(host, match, dport->component_reg_phys,
297 return PTR_ERR(port);
298 dev_dbg(host, "%s: add: %s\n", dev_name(match), dev_name(&port->dev));
301 * Note that this lookup already succeeded in
302 * to_cxl_host_bridge(), so no need to check for failure here
304 pci_root = acpi_pci_find_root(bridge->handle);
305 ctx = (struct cxl_walk_context){
307 .root = pci_root->bus,
310 pci_walk_bus(pci_root->bus, match_add_root_ports, &ctx);
319 /* TODO: Scan CHBCR for HDM Decoder resources */
322 * Per the CXL specification (8.2.5.12 CXL HDM Decoder Capability
323 * Structure) single ported host-bridges need not publish a decoder
324 * capability when a passthrough decode can be assumed, i.e. all
325 * transactions that the uport sees are claimed and passed to the single
326 * dport. Disable the range until the first CXL region is enumerated /
329 cxld = cxl_decoder_alloc(port, 1);
331 return PTR_ERR(cxld);
333 cxld->interleave_ways = 1;
334 cxld->interleave_granularity = PAGE_SIZE;
335 cxld->target_type = CXL_DECODER_EXPANDER;
336 cxld->range = (struct range) {
341 device_lock(&port->dev);
342 dport = list_first_entry(&port->dports, typeof(*dport), list);
343 device_unlock(&port->dev);
345 single_port_map[0] = dport->port_id;
347 rc = cxl_decoder_add(cxld, single_port_map);
349 put_device(&cxld->dev);
351 rc = cxl_decoder_autoremove(host, cxld);
354 dev_dbg(host, "add: %s\n", dev_name(&cxld->dev));
358 static int add_host_bridge_dport(struct device *match, void *arg)
362 unsigned long long uid;
363 struct acpi_cedt_chbs *chbs;
364 struct cxl_port *root_port = arg;
365 struct device *host = root_port->dev.parent;
366 struct acpi_device *bridge = to_cxl_host_bridge(host, match);
371 status = acpi_evaluate_integer(bridge->handle, METHOD_NAME__UID, NULL,
373 if (status != AE_OK) {
374 dev_err(host, "unable to retrieve _UID of %s\n",
379 chbs = cxl_acpi_match_chbs(host, uid);
381 dev_warn(host, "No CHBS found for Host Bridge: %s\n",
386 rc = cxl_add_dport(root_port, match, uid, get_chbcr(chbs));
388 dev_err(host, "failed to add downstream port: %s\n",
392 dev_dbg(host, "add dport%llu: %s\n", uid, dev_name(match));
396 static int add_root_nvdimm_bridge(struct device *match, void *data)
398 struct cxl_decoder *cxld;
399 struct cxl_port *root_port = data;
400 struct cxl_nvdimm_bridge *cxl_nvb;
401 struct device *host = root_port->dev.parent;
403 if (!is_root_decoder(match))
406 cxld = to_cxl_decoder(match);
407 if (!(cxld->flags & CXL_DECODER_F_PMEM))
410 cxl_nvb = devm_cxl_add_nvdimm_bridge(host, root_port);
411 if (IS_ERR(cxl_nvb)) {
412 dev_dbg(host, "failed to register pmem\n");
413 return PTR_ERR(cxl_nvb);
415 dev_dbg(host, "%s: add: %s\n", dev_name(&root_port->dev),
416 dev_name(&cxl_nvb->dev));
420 static u32 cedt_instance(struct platform_device *pdev)
422 const bool *native_acpi0017 = acpi_device_get_match_data(&pdev->dev);
424 if (native_acpi0017 && *native_acpi0017)
427 /* for cxl_test request a non-canonical instance */
431 static int cxl_acpi_probe(struct platform_device *pdev)
435 struct cxl_port *root_port;
436 struct device *host = &pdev->dev;
437 struct acpi_device *adev = ACPI_COMPANION(host);
439 root_port = devm_cxl_add_port(host, host, CXL_RESOURCE_NONE, NULL);
440 if (IS_ERR(root_port))
441 return PTR_ERR(root_port);
442 dev_dbg(host, "add: %s\n", dev_name(&root_port->dev));
444 status = acpi_get_table(ACPI_SIG_CEDT, cedt_instance(pdev), &acpi_cedt);
445 if (ACPI_FAILURE(status))
448 rc = bus_for_each_dev(adev->dev.bus, NULL, root_port,
449 add_host_bridge_dport);
453 cxl_add_cfmws_decoders(host, root_port);
456 * Root level scanned with host-bridge as dports, now scan host-bridges
457 * for their role as CXL uports to their CXL-capable PCIe Root Ports.
459 rc = bus_for_each_dev(adev->dev.bus, NULL, root_port,
460 add_host_bridge_uport);
464 if (IS_ENABLED(CONFIG_CXL_PMEM))
465 rc = device_for_each_child(&root_port->dev, root_port,
466 add_root_nvdimm_bridge);
469 acpi_put_table(acpi_cedt);
475 static bool native_acpi0017 = true;
477 static const struct acpi_device_id cxl_acpi_ids[] = {
478 { "ACPI0017", (unsigned long) &native_acpi0017 },
481 MODULE_DEVICE_TABLE(acpi, cxl_acpi_ids);
483 static struct platform_driver cxl_acpi_driver = {
484 .probe = cxl_acpi_probe,
486 .name = KBUILD_MODNAME,
487 .acpi_match_table = cxl_acpi_ids,
491 module_platform_driver(cxl_acpi_driver);
492 MODULE_LICENSE("GPL v2");
493 MODULE_IMPORT_NS(CXL);