blob: 3afdee8f334a41f7f0839f207e1bca8f6f12e9d8 [file] [log] [blame]
Dan Williams4812be92021-06-09 09:01:35 -07001// 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>
Dan Williams3b94ce72021-06-09 09:01:51 -07008#include <linux/pci.h>
Dan Williams4812be92021-06-09 09:01:35 -07009#include "cxl.h"
10
Alison Schofieldda6aafe2021-06-17 16:12:15 -070011static struct acpi_table_header *acpi_cedt;
12
13static struct acpi_cedt_chbs *cxl_acpi_match_chbs(struct device *dev, u32 uid)
14{
15 struct acpi_cedt_chbs *chbs, *chbs_match = NULL;
16 acpi_size len, cur = 0;
17 void *cedt_subtable;
18
19 len = acpi_cedt->length - sizeof(*acpi_cedt);
20 cedt_subtable = acpi_cedt + 1;
21
22 while (cur < len) {
23 struct acpi_cedt_header *c = cedt_subtable + cur;
24
25 if (c->type != ACPI_CEDT_TYPE_CHBS) {
26 cur += c->length;
27 continue;
28 }
29
30 chbs = cedt_subtable + cur;
31
32 if (chbs->header.length < sizeof(*chbs)) {
33 dev_warn_once(dev,
34 "CHBS entry skipped: invalid length:%u\n",
35 chbs->header.length);
36 cur += c->length;
37 continue;
38 }
39
40 if (chbs->uid != uid) {
41 cur += c->length;
42 continue;
43 }
44
45 if (chbs_match) {
46 dev_warn_once(dev,
47 "CHBS entry skipped: duplicate UID:%u\n",
48 uid);
49 cur += c->length;
50 continue;
51 }
52
53 chbs_match = chbs;
54 cur += c->length;
55 }
56
57 return chbs_match ? chbs_match : ERR_PTR(-ENODEV);
58}
59
60static resource_size_t get_chbcr(struct acpi_cedt_chbs *chbs)
61{
62 return IS_ERR(chbs) ? CXL_RESOURCE_NONE : chbs->base;
63}
64
Dan Williams3b94ce72021-06-09 09:01:51 -070065struct cxl_walk_context {
66 struct device *dev;
67 struct pci_bus *root;
68 struct cxl_port *port;
69 int error;
70 int count;
71};
72
73static int match_add_root_ports(struct pci_dev *pdev, void *data)
74{
75 struct cxl_walk_context *ctx = data;
76 struct pci_bus *root_bus = ctx->root;
77 struct cxl_port *port = ctx->port;
78 int type = pci_pcie_type(pdev);
79 struct device *dev = ctx->dev;
80 u32 lnkcap, port_num;
81 int rc;
82
83 if (pdev->bus != root_bus)
84 return 0;
85 if (!pci_is_pcie(pdev))
86 return 0;
87 if (type != PCI_EXP_TYPE_ROOT_PORT)
88 return 0;
89 if (pci_read_config_dword(pdev, pci_pcie_cap(pdev) + PCI_EXP_LNKCAP,
90 &lnkcap) != PCIBIOS_SUCCESSFUL)
91 return 0;
92
93 /* TODO walk DVSEC to find component register base */
94 port_num = FIELD_GET(PCI_EXP_LNKCAP_PN, lnkcap);
95 rc = cxl_add_dport(port, &pdev->dev, port_num, CXL_RESOURCE_NONE);
96 if (rc) {
97 ctx->error = rc;
98 return rc;
99 }
100 ctx->count++;
101
102 dev_dbg(dev, "add dport%d: %s\n", port_num, dev_name(&pdev->dev));
103
104 return 0;
105}
106
Alison Schofieldda6aafe2021-06-17 16:12:15 -0700107static struct cxl_dport *find_dport_by_dev(struct cxl_port *port, struct device *dev)
108{
109 struct cxl_dport *dport;
110
111 device_lock(&port->dev);
112 list_for_each_entry(dport, &port->dports, list)
113 if (dport->dport == dev) {
114 device_unlock(&port->dev);
115 return dport;
116 }
117
118 device_unlock(&port->dev);
119 return NULL;
120}
121
Dan Williams7d4b5ca2021-06-09 09:01:46 -0700122static struct acpi_device *to_cxl_host_bridge(struct device *dev)
123{
124 struct acpi_device *adev = to_acpi_device(dev);
125
126 if (strcmp(acpi_device_hid(adev), "ACPI0016") == 0)
127 return adev;
128 return NULL;
129}
130
Dan Williams3b94ce72021-06-09 09:01:51 -0700131/*
132 * A host bridge is a dport to a CFMWS decode and it is a uport to the
133 * dport (PCIe Root Ports) in the host bridge.
134 */
135static int add_host_bridge_uport(struct device *match, void *arg)
136{
137 struct acpi_device *bridge = to_cxl_host_bridge(match);
138 struct cxl_port *root_port = arg;
139 struct device *host = root_port->dev.parent;
140 struct acpi_pci_root *pci_root;
141 struct cxl_walk_context ctx;
Dan Williams40ba17a2021-06-09 09:43:29 -0700142 struct cxl_decoder *cxld;
Alison Schofieldda6aafe2021-06-17 16:12:15 -0700143 struct cxl_dport *dport;
Dan Williams3b94ce72021-06-09 09:01:51 -0700144 struct cxl_port *port;
145
146 if (!bridge)
147 return 0;
148
149 pci_root = acpi_pci_find_root(bridge->handle);
150 if (!pci_root)
151 return -ENXIO;
152
Alison Schofieldda6aafe2021-06-17 16:12:15 -0700153 dport = find_dport_by_dev(root_port, match);
154 if (!dport) {
155 dev_dbg(host, "host bridge expected and not found\n");
156 return -ENODEV;
157 }
158
159 port = devm_cxl_add_port(host, match, dport->component_reg_phys,
160 root_port);
Dan Williams3b94ce72021-06-09 09:01:51 -0700161 if (IS_ERR(port))
162 return PTR_ERR(port);
163 dev_dbg(host, "%s: add: %s\n", dev_name(match), dev_name(&port->dev));
164
165 ctx = (struct cxl_walk_context){
166 .dev = host,
167 .root = pci_root->bus,
168 .port = port,
169 };
170 pci_walk_bus(pci_root->bus, match_add_root_ports, &ctx);
171
172 if (ctx.count == 0)
173 return -ENODEV;
Dan Williams40ba17a2021-06-09 09:43:29 -0700174 if (ctx.error)
175 return ctx.error;
176
177 /* TODO: Scan CHBCR for HDM Decoder resources */
178
179 /*
180 * In the single-port host-bridge case there are no HDM decoders
181 * in the CHBCR and a 1:1 passthrough decode is implied.
182 */
183 if (ctx.count == 1) {
184 cxld = devm_cxl_add_passthrough_decoder(host, port);
185 if (IS_ERR(cxld))
186 return PTR_ERR(cxld);
187
188 dev_dbg(host, "add: %s\n", dev_name(&cxld->dev));
189 }
190
191 return 0;
Dan Williams3b94ce72021-06-09 09:01:51 -0700192}
193
Dan Williams7d4b5ca2021-06-09 09:01:46 -0700194static int add_host_bridge_dport(struct device *match, void *arg)
195{
196 int rc;
197 acpi_status status;
198 unsigned long long uid;
Alison Schofieldda6aafe2021-06-17 16:12:15 -0700199 struct acpi_cedt_chbs *chbs;
Dan Williams7d4b5ca2021-06-09 09:01:46 -0700200 struct cxl_port *root_port = arg;
201 struct device *host = root_port->dev.parent;
202 struct acpi_device *bridge = to_cxl_host_bridge(match);
203
204 if (!bridge)
205 return 0;
206
207 status = acpi_evaluate_integer(bridge->handle, METHOD_NAME__UID, NULL,
208 &uid);
209 if (status != AE_OK) {
210 dev_err(host, "unable to retrieve _UID of %s\n",
211 dev_name(match));
212 return -ENODEV;
213 }
214
Alison Schofieldda6aafe2021-06-17 16:12:15 -0700215 chbs = cxl_acpi_match_chbs(host, uid);
216 if (IS_ERR(chbs))
217 dev_dbg(host, "No CHBS found for Host Bridge: %s\n",
218 dev_name(match));
219
220 rc = cxl_add_dport(root_port, match, uid, get_chbcr(chbs));
Dan Williams7d4b5ca2021-06-09 09:01:46 -0700221 if (rc) {
222 dev_err(host, "failed to add downstream port: %s\n",
223 dev_name(match));
224 return rc;
225 }
226 dev_dbg(host, "add dport%llu: %s\n", uid, dev_name(match));
227 return 0;
228}
229
Dan Williams8fdcb172021-06-15 16:18:17 -0700230static int add_root_nvdimm_bridge(struct device *match, void *data)
231{
232 struct cxl_decoder *cxld;
233 struct cxl_port *root_port = data;
234 struct cxl_nvdimm_bridge *cxl_nvb;
235 struct device *host = root_port->dev.parent;
236
237 if (!is_root_decoder(match))
238 return 0;
239
240 cxld = to_cxl_decoder(match);
241 if (!(cxld->flags & CXL_DECODER_F_PMEM))
242 return 0;
243
244 cxl_nvb = devm_cxl_add_nvdimm_bridge(host, root_port);
245 if (IS_ERR(cxl_nvb)) {
246 dev_dbg(host, "failed to register pmem\n");
247 return PTR_ERR(cxl_nvb);
248 }
249 dev_dbg(host, "%s: add: %s\n", dev_name(&root_port->dev),
250 dev_name(&cxl_nvb->dev));
251 return 1;
252}
253
Dan Williams4812be92021-06-09 09:01:35 -0700254static int cxl_acpi_probe(struct platform_device *pdev)
255{
Dan Williams3b94ce72021-06-09 09:01:51 -0700256 int rc;
Alison Schofieldda6aafe2021-06-17 16:12:15 -0700257 acpi_status status;
Dan Williams4812be92021-06-09 09:01:35 -0700258 struct cxl_port *root_port;
259 struct device *host = &pdev->dev;
Dan Williams7d4b5ca2021-06-09 09:01:46 -0700260 struct acpi_device *adev = ACPI_COMPANION(host);
Dan Williams4812be92021-06-09 09:01:35 -0700261
262 root_port = devm_cxl_add_port(host, host, CXL_RESOURCE_NONE, NULL);
263 if (IS_ERR(root_port))
264 return PTR_ERR(root_port);
265 dev_dbg(host, "add: %s\n", dev_name(&root_port->dev));
266
Alison Schofieldda6aafe2021-06-17 16:12:15 -0700267 status = acpi_get_table(ACPI_SIG_CEDT, 0, &acpi_cedt);
268 if (ACPI_FAILURE(status))
269 return -ENXIO;
270
Dan Williams3b94ce72021-06-09 09:01:51 -0700271 rc = bus_for_each_dev(adev->dev.bus, NULL, root_port,
272 add_host_bridge_dport);
273 if (rc)
Alison Schofieldda6aafe2021-06-17 16:12:15 -0700274 goto out;
Dan Williams3b94ce72021-06-09 09:01:51 -0700275
276 /*
277 * Root level scanned with host-bridge as dports, now scan host-bridges
278 * for their role as CXL uports to their CXL-capable PCIe Root Ports.
279 */
Dan Williams8fdcb172021-06-15 16:18:17 -0700280 rc = bus_for_each_dev(adev->dev.bus, NULL, root_port,
281 add_host_bridge_uport);
282 if (rc)
Alison Schofieldda6aafe2021-06-17 16:12:15 -0700283 goto out;
Dan Williams8fdcb172021-06-15 16:18:17 -0700284
285 if (IS_ENABLED(CONFIG_CXL_PMEM))
286 rc = device_for_each_child(&root_port->dev, root_port,
287 add_root_nvdimm_bridge);
Alison Schofieldda6aafe2021-06-17 16:12:15 -0700288
289out:
290 acpi_put_table(acpi_cedt);
Dan Williams8fdcb172021-06-15 16:18:17 -0700291 if (rc < 0)
292 return rc;
293 return 0;
Dan Williams4812be92021-06-09 09:01:35 -0700294}
295
296static const struct acpi_device_id cxl_acpi_ids[] = {
297 { "ACPI0017", 0 },
298 { "", 0 },
299};
300MODULE_DEVICE_TABLE(acpi, cxl_acpi_ids);
301
302static struct platform_driver cxl_acpi_driver = {
303 .probe = cxl_acpi_probe,
304 .driver = {
305 .name = KBUILD_MODNAME,
306 .acpi_match_table = cxl_acpi_ids,
307 },
308};
309
310module_platform_driver(cxl_acpi_driver);
311MODULE_LICENSE("GPL v2");
312MODULE_IMPORT_NS(CXL);