1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2021 Intel Corporation. All rights reserved. */
3 #include <linux/io-64-nonatomic-lo-hi.h>
4 #include <linux/device.h>
5 #include <linux/delay.h>
15 * Compute Express Link protocols are layered on top of PCIe. CXL core provides
16 * a set of helpers for CXL interactions which occur via PCIe.
19 static unsigned short media_ready_timeout = 60;
20 module_param(media_ready_timeout, ushort, 0644);
21 MODULE_PARM_DESC(media_ready_timeout, "seconds to wait for media ready");
23 struct cxl_walk_context {
25 struct cxl_port *port;
31 static int match_add_dports(struct pci_dev *pdev, void *data)
33 struct cxl_walk_context *ctx = data;
34 struct cxl_port *port = ctx->port;
35 int type = pci_pcie_type(pdev);
36 struct cxl_register_map map;
37 struct cxl_dport *dport;
41 if (pdev->bus != ctx->bus)
43 if (!pci_is_pcie(pdev))
45 if (type != ctx->type)
47 if (pci_read_config_dword(pdev, pci_pcie_cap(pdev) + PCI_EXP_LNKCAP,
51 rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
53 dev_dbg(&port->dev, "failed to find component registers\n");
55 port_num = FIELD_GET(PCI_EXP_LNKCAP_PN, lnkcap);
56 dport = devm_cxl_add_dport(port, &pdev->dev, port_num,
57 cxl_regmap_to_base(pdev, &map));
59 ctx->error = PTR_ERR(dport);
60 return PTR_ERR(dport);
64 dev_dbg(&port->dev, "add dport%d: %s\n", port_num, dev_name(&pdev->dev));
70 * devm_cxl_port_enumerate_dports - enumerate downstream ports of the upstream port
71 * @port: cxl_port whose ->uport is the upstream of dports to be enumerated
73 * Returns a positive number of dports enumerated or a negative error
76 int devm_cxl_port_enumerate_dports(struct cxl_port *port)
78 struct pci_bus *bus = cxl_port_to_pci_bus(port);
79 struct cxl_walk_context ctx;
85 if (pci_is_root_bus(bus))
86 type = PCI_EXP_TYPE_ROOT_PORT;
88 type = PCI_EXP_TYPE_DOWNSTREAM;
90 ctx = (struct cxl_walk_context) {
95 pci_walk_bus(bus, match_add_dports, &ctx);
103 EXPORT_SYMBOL_NS_GPL(devm_cxl_port_enumerate_dports, CXL);
106 * Wait up to @media_ready_timeout for the device to report memory
109 int cxl_await_media_ready(struct cxl_dev_state *cxlds)
111 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
112 int d = cxlds->cxl_dvsec;
117 for (i = media_ready_timeout; i; i--) {
120 rc = pci_read_config_dword(
121 pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(0), &temp);
125 active = FIELD_GET(CXL_DVSEC_MEM_ACTIVE, temp);
133 "timeout awaiting memory active after %d seconds\n",
134 media_ready_timeout);
138 md_status = readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
139 if (!CXLMDEV_READY(md_status))
144 EXPORT_SYMBOL_NS_GPL(cxl_await_media_ready, CXL);
146 static int wait_for_valid(struct cxl_dev_state *cxlds)
148 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
149 int d = cxlds->cxl_dvsec, rc;
153 * Memory_Info_Valid: When set, indicates that the CXL Range 1 Size high
154 * and Size Low registers are valid. Must be set within 1 second of
155 * deassertion of reset to CXL device. Likely it is already set by the
156 * time this runs, but otherwise give a 1.5 second timeout in case of
159 rc = pci_read_config_dword(pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(0), &val);
163 if (val & CXL_DVSEC_MEM_INFO_VALID)
168 rc = pci_read_config_dword(pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(0), &val);
172 if (val & CXL_DVSEC_MEM_INFO_VALID)
178 static int cxl_set_mem_enable(struct cxl_dev_state *cxlds, u16 val)
180 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
181 int d = cxlds->cxl_dvsec;
185 rc = pci_read_config_word(pdev, d + CXL_DVSEC_CTRL_OFFSET, &ctrl);
189 if ((ctrl & CXL_DVSEC_MEM_ENABLE) == val)
191 ctrl &= ~CXL_DVSEC_MEM_ENABLE;
194 rc = pci_write_config_word(pdev, d + CXL_DVSEC_CTRL_OFFSET, ctrl);
201 static void clear_mem_enable(void *cxlds)
203 cxl_set_mem_enable(cxlds, 0);
206 static int devm_cxl_enable_mem(struct device *host, struct cxl_dev_state *cxlds)
210 rc = cxl_set_mem_enable(cxlds, CXL_DVSEC_MEM_ENABLE);
215 return devm_add_action_or_reset(host, clear_mem_enable, cxlds);
218 static bool range_contains(struct range *r1, struct range *r2)
220 return r1->start <= r2->start && r1->end >= r2->end;
223 /* require dvsec ranges to be covered by a locked platform window */
224 static int dvsec_range_allowed(struct device *dev, void *arg)
226 struct range *dev_range = arg;
227 struct cxl_decoder *cxld;
228 struct range root_range;
230 if (!is_root_decoder(dev))
233 cxld = to_cxl_decoder(dev);
235 if (!(cxld->flags & CXL_DECODER_F_LOCK))
237 if (!(cxld->flags & CXL_DECODER_F_RAM))
240 root_range = (struct range) {
241 .start = cxld->platform_res.start,
242 .end = cxld->platform_res.end,
245 return range_contains(&root_range, dev_range);
248 static void disable_hdm(void *_cxlhdm)
251 struct cxl_hdm *cxlhdm = _cxlhdm;
252 void __iomem *hdm = cxlhdm->regs.hdm_decoder;
254 global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
255 writel(global_ctrl & ~CXL_HDM_DECODER_ENABLE,
256 hdm + CXL_HDM_DECODER_CTRL_OFFSET);
259 static int devm_cxl_enable_hdm(struct device *host, struct cxl_hdm *cxlhdm)
261 void __iomem *hdm = cxlhdm->regs.hdm_decoder;
264 global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
265 writel(global_ctrl | CXL_HDM_DECODER_ENABLE,
266 hdm + CXL_HDM_DECODER_CTRL_OFFSET);
268 return devm_add_action_or_reset(host, disable_hdm, cxlhdm);
271 static bool __cxl_hdm_decode_init(struct cxl_dev_state *cxlds,
272 struct cxl_hdm *cxlhdm,
273 struct cxl_endpoint_dvsec_info *info)
275 void __iomem *hdm = cxlhdm->regs.hdm_decoder;
276 struct cxl_port *port = cxlhdm->port;
277 struct device *dev = cxlds->dev;
278 struct cxl_port *root;
282 global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
285 * If the HDM Decoder Capability is already enabled then assume
286 * that some other agent like platform firmware set it up.
288 if (global_ctrl & CXL_HDM_DECODER_ENABLE) {
289 rc = devm_cxl_enable_mem(&port->dev, cxlds);
295 root = to_cxl_port(port->dev.parent);
296 while (!is_cxl_root(root) && is_cxl_port(root->dev.parent))
297 root = to_cxl_port(root->dev.parent);
298 if (!is_cxl_root(root)) {
299 dev_err(dev, "Failed to acquire root port for HDM enable\n");
303 for (i = 0, allowed = 0; info->mem_enabled && i < info->ranges; i++) {
304 struct device *cxld_dev;
306 cxld_dev = device_find_child(&root->dev, &info->dvsec_range[i],
307 dvsec_range_allowed);
309 dev_dbg(dev, "DVSEC Range%d denied by platform\n", i);
312 dev_dbg(dev, "DVSEC Range%d allowed by platform\n", i);
313 put_device(cxld_dev);
318 cxl_set_mem_enable(cxlds, 0);
319 info->mem_enabled = 0;
323 * Per CXL 2.0 Section 8.1.3.8.3 and 8.1.3.8.4 DVSEC CXL Range 1 Base
324 * [High,Low] when HDM operation is enabled the range register values
325 * are ignored by the device, but the spec also recommends matching the
326 * DVSEC Range 1,2 to HDM Decoder Range 0,1. So, non-zero info->ranges
327 * are expected even though Linux does not require or maintain that
328 * match. If at least one DVSEC range is enabled and allowed, skip HDM
329 * Decoder Capability Enable.
331 if (info->mem_enabled)
334 rc = devm_cxl_enable_hdm(&port->dev, cxlhdm);
338 rc = devm_cxl_enable_mem(&port->dev, cxlds);
346 * cxl_hdm_decode_init() - Setup HDM decoding for the endpoint
347 * @cxlds: Device state
348 * @cxlhdm: Mapped HDM decoder Capability
350 * Try to enable the endpoint's HDM Decoder Capability
352 int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm)
354 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
355 struct cxl_endpoint_dvsec_info info = { 0 };
356 int hdm_count, rc, i, ranges = 0;
357 struct device *dev = &pdev->dev;
358 int d = cxlds->cxl_dvsec;
362 dev_dbg(dev, "No DVSEC Capability\n");
366 rc = pci_read_config_word(pdev, d + CXL_DVSEC_CAP_OFFSET, &cap);
370 rc = pci_read_config_word(pdev, d + CXL_DVSEC_CTRL_OFFSET, &ctrl);
374 if (!(cap & CXL_DVSEC_MEM_CAPABLE)) {
375 dev_dbg(dev, "Not MEM Capable\n");
380 * It is not allowed by spec for MEM.capable to be set and have 0 legacy
381 * HDM decoders (values > 2 are also undefined as of CXL 2.0). As this
382 * driver is for a spec defined class code which must be CXL.mem
383 * capable, there is no point in continuing to enable CXL.mem.
385 hdm_count = FIELD_GET(CXL_DVSEC_HDM_COUNT_MASK, cap);
386 if (!hdm_count || hdm_count > 2)
389 rc = wait_for_valid(cxlds);
391 dev_dbg(dev, "Failure awaiting MEM_INFO_VALID (%d)\n", rc);
396 * The current DVSEC values are moot if the memory capability is
397 * disabled, and they will remain moot after the HDM Decoder
398 * capability is enabled.
400 info.mem_enabled = FIELD_GET(CXL_DVSEC_MEM_ENABLE, ctrl);
401 if (!info.mem_enabled)
404 for (i = 0; i < hdm_count; i++) {
408 rc = pci_read_config_dword(
409 pdev, d + CXL_DVSEC_RANGE_SIZE_HIGH(i), &temp);
413 size = (u64)temp << 32;
415 rc = pci_read_config_dword(
416 pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(i), &temp);
420 size |= temp & CXL_DVSEC_MEM_SIZE_LOW_MASK;
422 rc = pci_read_config_dword(
423 pdev, d + CXL_DVSEC_RANGE_BASE_HIGH(i), &temp);
427 base = (u64)temp << 32;
429 rc = pci_read_config_dword(
430 pdev, d + CXL_DVSEC_RANGE_BASE_LOW(i), &temp);
434 base |= temp & CXL_DVSEC_MEM_BASE_LOW_MASK;
436 info.dvsec_range[i] = (struct range) {
438 .end = base + size - 1
445 info.ranges = ranges;
448 * If DVSEC ranges are being used instead of HDM decoder registers there
449 * is no use in trying to manage those.
452 if (!__cxl_hdm_decode_init(cxlds, cxlhdm, &info)) {
454 "Legacy range registers configuration prevents HDM operation.\n");
460 EXPORT_SYMBOL_NS_GPL(cxl_hdm_decode_init, CXL);