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>
7 #include <linux/pci-doe.h>
17 * Compute Express Link protocols are layered on top of PCIe. CXL core provides
18 * a set of helpers for CXL interactions which occur via PCIe.
21 static unsigned short media_ready_timeout = 60;
22 module_param(media_ready_timeout, ushort, 0644);
23 MODULE_PARM_DESC(media_ready_timeout, "seconds to wait for media ready");
25 struct cxl_walk_context {
27 struct cxl_port *port;
33 static int match_add_dports(struct pci_dev *pdev, void *data)
35 struct cxl_walk_context *ctx = data;
36 struct cxl_port *port = ctx->port;
37 int type = pci_pcie_type(pdev);
38 struct cxl_register_map map;
39 struct cxl_dport *dport;
43 if (pdev->bus != ctx->bus)
45 if (!pci_is_pcie(pdev))
47 if (type != ctx->type)
49 if (pci_read_config_dword(pdev, pci_pcie_cap(pdev) + PCI_EXP_LNKCAP,
53 rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
55 dev_dbg(&port->dev, "failed to find component registers\n");
57 port_num = FIELD_GET(PCI_EXP_LNKCAP_PN, lnkcap);
58 dport = devm_cxl_add_dport(port, &pdev->dev, port_num, map.resource);
60 ctx->error = PTR_ERR(dport);
61 return PTR_ERR(dport);
69 * devm_cxl_port_enumerate_dports - enumerate downstream ports of the upstream port
70 * @port: cxl_port whose ->uport_dev is the upstream of dports to be enumerated
72 * Returns a positive number of dports enumerated or a negative error
75 int devm_cxl_port_enumerate_dports(struct cxl_port *port)
77 struct pci_bus *bus = cxl_port_to_pci_bus(port);
78 struct cxl_walk_context ctx;
84 if (pci_is_root_bus(bus))
85 type = PCI_EXP_TYPE_ROOT_PORT;
87 type = PCI_EXP_TYPE_DOWNSTREAM;
89 ctx = (struct cxl_walk_context) {
94 pci_walk_bus(bus, match_add_dports, &ctx);
102 EXPORT_SYMBOL_NS_GPL(devm_cxl_port_enumerate_dports, CXL);
104 static int cxl_dvsec_mem_range_valid(struct cxl_dev_state *cxlds, int id)
106 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
107 int d = cxlds->cxl_dvsec;
112 if (id > CXL_DVSEC_RANGE_MAX)
115 /* Check MEM INFO VALID bit first, give up after 1s */
118 rc = pci_read_config_dword(pdev,
119 d + CXL_DVSEC_RANGE_SIZE_LOW(id),
124 valid = FIELD_GET(CXL_DVSEC_MEM_INFO_VALID, temp);
132 "Timeout awaiting memory range %d valid after 1s.\n",
140 static int cxl_dvsec_mem_range_active(struct cxl_dev_state *cxlds, int id)
142 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
143 int d = cxlds->cxl_dvsec;
148 if (id > CXL_DVSEC_RANGE_MAX)
151 /* Check MEM ACTIVE bit, up to 60s timeout by default */
152 for (i = media_ready_timeout; i; i--) {
153 rc = pci_read_config_dword(
154 pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(id), &temp);
158 active = FIELD_GET(CXL_DVSEC_MEM_ACTIVE, temp);
166 "timeout awaiting memory active after %d seconds\n",
167 media_ready_timeout);
175 * Wait up to @media_ready_timeout for the device to report memory
178 int cxl_await_media_ready(struct cxl_dev_state *cxlds)
180 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
181 int d = cxlds->cxl_dvsec;
182 int rc, i, hdm_count;
186 rc = pci_read_config_word(pdev,
187 d + CXL_DVSEC_CAP_OFFSET, &cap);
191 hdm_count = FIELD_GET(CXL_DVSEC_HDM_COUNT_MASK, cap);
192 for (i = 0; i < hdm_count; i++) {
193 rc = cxl_dvsec_mem_range_valid(cxlds, i);
198 for (i = 0; i < hdm_count; i++) {
199 rc = cxl_dvsec_mem_range_active(cxlds, i);
204 md_status = readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
205 if (!CXLMDEV_READY(md_status))
210 EXPORT_SYMBOL_NS_GPL(cxl_await_media_ready, CXL);
212 static int wait_for_valid(struct pci_dev *pdev, int d)
218 * Memory_Info_Valid: When set, indicates that the CXL Range 1 Size high
219 * and Size Low registers are valid. Must be set within 1 second of
220 * deassertion of reset to CXL device. Likely it is already set by the
221 * time this runs, but otherwise give a 1.5 second timeout in case of
224 rc = pci_read_config_dword(pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(0), &val);
228 if (val & CXL_DVSEC_MEM_INFO_VALID)
233 rc = pci_read_config_dword(pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(0), &val);
237 if (val & CXL_DVSEC_MEM_INFO_VALID)
243 static int cxl_set_mem_enable(struct cxl_dev_state *cxlds, u16 val)
245 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
246 int d = cxlds->cxl_dvsec;
250 rc = pci_read_config_word(pdev, d + CXL_DVSEC_CTRL_OFFSET, &ctrl);
254 if ((ctrl & CXL_DVSEC_MEM_ENABLE) == val)
256 ctrl &= ~CXL_DVSEC_MEM_ENABLE;
259 rc = pci_write_config_word(pdev, d + CXL_DVSEC_CTRL_OFFSET, ctrl);
266 static void clear_mem_enable(void *cxlds)
268 cxl_set_mem_enable(cxlds, 0);
271 static int devm_cxl_enable_mem(struct device *host, struct cxl_dev_state *cxlds)
275 rc = cxl_set_mem_enable(cxlds, CXL_DVSEC_MEM_ENABLE);
280 return devm_add_action_or_reset(host, clear_mem_enable, cxlds);
283 /* require dvsec ranges to be covered by a locked platform window */
284 static int dvsec_range_allowed(struct device *dev, void *arg)
286 struct range *dev_range = arg;
287 struct cxl_decoder *cxld;
289 if (!is_root_decoder(dev))
292 cxld = to_cxl_decoder(dev);
294 if (!(cxld->flags & CXL_DECODER_F_RAM))
297 return range_contains(&cxld->hpa_range, dev_range);
300 static void disable_hdm(void *_cxlhdm)
303 struct cxl_hdm *cxlhdm = _cxlhdm;
304 void __iomem *hdm = cxlhdm->regs.hdm_decoder;
306 global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
307 writel(global_ctrl & ~CXL_HDM_DECODER_ENABLE,
308 hdm + CXL_HDM_DECODER_CTRL_OFFSET);
311 static int devm_cxl_enable_hdm(struct device *host, struct cxl_hdm *cxlhdm)
313 void __iomem *hdm = cxlhdm->regs.hdm_decoder;
316 global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
317 writel(global_ctrl | CXL_HDM_DECODER_ENABLE,
318 hdm + CXL_HDM_DECODER_CTRL_OFFSET);
320 return devm_add_action_or_reset(host, disable_hdm, cxlhdm);
323 int cxl_dvsec_rr_decode(struct device *dev, int d,
324 struct cxl_endpoint_dvsec_info *info)
326 struct pci_dev *pdev = to_pci_dev(dev);
327 int hdm_count, rc, i, ranges = 0;
331 dev_dbg(dev, "No DVSEC Capability\n");
335 rc = pci_read_config_word(pdev, d + CXL_DVSEC_CAP_OFFSET, &cap);
339 rc = pci_read_config_word(pdev, d + CXL_DVSEC_CTRL_OFFSET, &ctrl);
343 if (!(cap & CXL_DVSEC_MEM_CAPABLE)) {
344 dev_dbg(dev, "Not MEM Capable\n");
349 * It is not allowed by spec for MEM.capable to be set and have 0 legacy
350 * HDM decoders (values > 2 are also undefined as of CXL 2.0). As this
351 * driver is for a spec defined class code which must be CXL.mem
352 * capable, there is no point in continuing to enable CXL.mem.
354 hdm_count = FIELD_GET(CXL_DVSEC_HDM_COUNT_MASK, cap);
355 if (!hdm_count || hdm_count > 2)
358 rc = wait_for_valid(pdev, d);
360 dev_dbg(dev, "Failure awaiting MEM_INFO_VALID (%d)\n", rc);
365 * The current DVSEC values are moot if the memory capability is
366 * disabled, and they will remain moot after the HDM Decoder
367 * capability is enabled.
369 info->mem_enabled = FIELD_GET(CXL_DVSEC_MEM_ENABLE, ctrl);
370 if (!info->mem_enabled)
373 for (i = 0; i < hdm_count; i++) {
377 rc = pci_read_config_dword(
378 pdev, d + CXL_DVSEC_RANGE_SIZE_HIGH(i), &temp);
382 size = (u64)temp << 32;
384 rc = pci_read_config_dword(
385 pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(i), &temp);
389 size |= temp & CXL_DVSEC_MEM_SIZE_LOW_MASK;
391 info->dvsec_range[i] = (struct range) {
393 .end = CXL_RESOURCE_NONE,
398 rc = pci_read_config_dword(
399 pdev, d + CXL_DVSEC_RANGE_BASE_HIGH(i), &temp);
403 base = (u64)temp << 32;
405 rc = pci_read_config_dword(
406 pdev, d + CXL_DVSEC_RANGE_BASE_LOW(i), &temp);
410 base |= temp & CXL_DVSEC_MEM_BASE_LOW_MASK;
412 info->dvsec_range[i] = (struct range) {
414 .end = base + size - 1
420 info->ranges = ranges;
424 EXPORT_SYMBOL_NS_GPL(cxl_dvsec_rr_decode, CXL);
427 * cxl_hdm_decode_init() - Setup HDM decoding for the endpoint
428 * @cxlds: Device state
429 * @cxlhdm: Mapped HDM decoder Capability
430 * @info: Cached DVSEC range registers info
432 * Try to enable the endpoint's HDM Decoder Capability
434 int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
435 struct cxl_endpoint_dvsec_info *info)
437 void __iomem *hdm = cxlhdm->regs.hdm_decoder;
438 struct cxl_port *port = cxlhdm->port;
439 struct device *dev = cxlds->dev;
440 struct cxl_port *root;
445 global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
448 * If the HDM Decoder Capability is already enabled then assume
449 * that some other agent like platform firmware set it up.
451 if (global_ctrl & CXL_HDM_DECODER_ENABLE || (!hdm && info->mem_enabled))
452 return devm_cxl_enable_mem(&port->dev, cxlds);
456 root = to_cxl_port(port->dev.parent);
457 while (!is_cxl_root(root) && is_cxl_port(root->dev.parent))
458 root = to_cxl_port(root->dev.parent);
459 if (!is_cxl_root(root)) {
460 dev_err(dev, "Failed to acquire root port for HDM enable\n");
464 for (i = 0, allowed = 0; info->mem_enabled && i < info->ranges; i++) {
465 struct device *cxld_dev;
467 cxld_dev = device_find_child(&root->dev, &info->dvsec_range[i],
468 dvsec_range_allowed);
470 dev_dbg(dev, "DVSEC Range%d denied by platform\n", i);
473 dev_dbg(dev, "DVSEC Range%d allowed by platform\n", i);
474 put_device(cxld_dev);
479 cxl_set_mem_enable(cxlds, 0);
480 info->mem_enabled = 0;
484 * Per CXL 2.0 Section 8.1.3.8.3 and 8.1.3.8.4 DVSEC CXL Range 1 Base
485 * [High,Low] when HDM operation is enabled the range register values
486 * are ignored by the device, but the spec also recommends matching the
487 * DVSEC Range 1,2 to HDM Decoder Range 0,1. So, non-zero info->ranges
488 * are expected even though Linux does not require or maintain that
489 * match. If at least one DVSEC range is enabled and allowed, skip HDM
490 * Decoder Capability Enable.
492 if (info->mem_enabled)
495 rc = devm_cxl_enable_hdm(&port->dev, cxlhdm);
499 return devm_cxl_enable_mem(&port->dev, cxlds);
501 EXPORT_SYMBOL_NS_GPL(cxl_hdm_decode_init, CXL);
503 #define CXL_DOE_TABLE_ACCESS_REQ_CODE 0x000000ff
504 #define CXL_DOE_TABLE_ACCESS_REQ_CODE_READ 0
505 #define CXL_DOE_TABLE_ACCESS_TABLE_TYPE 0x0000ff00
506 #define CXL_DOE_TABLE_ACCESS_TABLE_TYPE_CDATA 0
507 #define CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE 0xffff0000
508 #define CXL_DOE_TABLE_ACCESS_LAST_ENTRY 0xffff
509 #define CXL_DOE_PROTOCOL_TABLE_ACCESS 2
511 #define CDAT_DOE_REQ(entry_handle) cpu_to_le32 \
512 (FIELD_PREP(CXL_DOE_TABLE_ACCESS_REQ_CODE, \
513 CXL_DOE_TABLE_ACCESS_REQ_CODE_READ) | \
514 FIELD_PREP(CXL_DOE_TABLE_ACCESS_TABLE_TYPE, \
515 CXL_DOE_TABLE_ACCESS_TABLE_TYPE_CDATA) | \
516 FIELD_PREP(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE, (entry_handle)))
518 static int cxl_cdat_get_length(struct device *dev,
519 struct pci_doe_mb *cdat_doe,
522 __le32 request = CDAT_DOE_REQ(0);
526 rc = pci_doe(cdat_doe, PCI_DVSEC_VENDOR_ID_CXL,
527 CXL_DOE_PROTOCOL_TABLE_ACCESS,
528 &request, sizeof(request),
529 &response, sizeof(response));
531 dev_err(dev, "DOE failed: %d", rc);
534 if (rc < sizeof(response))
537 *length = le32_to_cpu(response[1]);
538 dev_dbg(dev, "CDAT length %zu\n", *length);
543 static int cxl_cdat_read_table(struct device *dev,
544 struct pci_doe_mb *cdat_doe,
545 void *cdat_table, size_t *cdat_length)
547 size_t length = *cdat_length + sizeof(__le32);
548 __le32 *data = cdat_table;
549 int entry_handle = 0;
553 __le32 request = CDAT_DOE_REQ(entry_handle);
554 struct cdat_entry_header *entry;
558 rc = pci_doe(cdat_doe, PCI_DVSEC_VENDOR_ID_CXL,
559 CXL_DOE_PROTOCOL_TABLE_ACCESS,
560 &request, sizeof(request),
563 dev_err(dev, "DOE failed: %d", rc);
567 /* 1 DW Table Access Response Header + CDAT entry */
568 entry = (struct cdat_entry_header *)(data + 1);
569 if ((entry_handle == 0 &&
570 rc != sizeof(__le32) + sizeof(struct cdat_header)) ||
572 (rc < sizeof(__le32) + sizeof(*entry) ||
573 rc != sizeof(__le32) + le16_to_cpu(entry->length))))
576 /* Get the CXL table access header entry handle */
577 entry_handle = FIELD_GET(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE,
578 le32_to_cpu(data[0]));
579 entry_dw = rc / sizeof(__le32);
583 * Table Access Response Header overwrote the last DW of
584 * previous entry, so restore that DW
587 length -= entry_dw * sizeof(__le32);
590 } while (entry_handle != CXL_DOE_TABLE_ACCESS_LAST_ENTRY);
592 /* Length in CDAT header may exceed concatenation of CDAT entries */
593 *cdat_length -= length - sizeof(__le32);
599 * read_cdat_data - Read the CDAT data on this port
600 * @port: Port to read data from
602 * This call will sleep waiting for responses from the DOE mailbox.
604 void read_cdat_data(struct cxl_port *port)
606 struct cxl_memdev *cxlmd = to_cxl_memdev(port->uport_dev);
607 struct device *host = cxlmd->dev.parent;
608 struct device *dev = &port->dev;
609 struct pci_doe_mb *cdat_doe;
614 if (!dev_is_pci(host))
616 cdat_doe = pci_find_doe_mailbox(to_pci_dev(host),
617 PCI_DVSEC_VENDOR_ID_CXL,
618 CXL_DOE_PROTOCOL_TABLE_ACCESS);
620 dev_dbg(dev, "No CDAT mailbox\n");
624 port->cdat_available = true;
626 if (cxl_cdat_get_length(dev, cdat_doe, &cdat_length)) {
627 dev_dbg(dev, "No CDAT length\n");
631 cdat_table = devm_kzalloc(dev, cdat_length + sizeof(__le32),
636 rc = cxl_cdat_read_table(dev, cdat_doe, cdat_table, &cdat_length);
638 /* Don't leave table data allocated on error */
639 devm_kfree(dev, cdat_table);
640 dev_err(dev, "CDAT data read error\n");
644 port->cdat.table = cdat_table + sizeof(__le32);
645 port->cdat.length = cdat_length;
647 EXPORT_SYMBOL_NS_GPL(read_cdat_data, CXL);
649 void cxl_cor_error_detected(struct pci_dev *pdev)
651 struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
655 if (!cxlds->regs.ras)
658 addr = cxlds->regs.ras + CXL_RAS_CORRECTABLE_STATUS_OFFSET;
659 status = readl(addr);
660 if (status & CXL_RAS_CORRECTABLE_STATUS_MASK) {
661 writel(status & CXL_RAS_CORRECTABLE_STATUS_MASK, addr);
662 trace_cxl_aer_correctable_error(cxlds->cxlmd, status);
665 EXPORT_SYMBOL_NS_GPL(cxl_cor_error_detected, CXL);
667 /* CXL spec rev3.0 8.2.4.16.1 */
668 static void header_log_copy(struct cxl_dev_state *cxlds, u32 *log)
672 int i, log_u32_size = CXL_HEADERLOG_SIZE / sizeof(u32);
674 addr = cxlds->regs.ras + CXL_RAS_HEADER_LOG_OFFSET;
677 for (i = 0; i < log_u32_size; i++) {
678 *log_addr = readl(addr);
685 * Log the state of the RAS status registers and prepare them to log the
686 * next error status. Return 1 if reset needed.
688 static bool cxl_report_and_clear(struct cxl_dev_state *cxlds)
690 u32 hl[CXL_HEADERLOG_SIZE_U32];
695 if (!cxlds->regs.ras)
698 addr = cxlds->regs.ras + CXL_RAS_UNCORRECTABLE_STATUS_OFFSET;
699 status = readl(addr);
700 if (!(status & CXL_RAS_UNCORRECTABLE_STATUS_MASK))
703 /* If multiple errors, log header points to first error from ctrl reg */
704 if (hweight32(status) > 1) {
705 void __iomem *rcc_addr =
706 cxlds->regs.ras + CXL_RAS_CAP_CONTROL_OFFSET;
708 fe = BIT(FIELD_GET(CXL_RAS_CAP_CONTROL_FE_MASK,
714 header_log_copy(cxlds, hl);
715 trace_cxl_aer_uncorrectable_error(cxlds->cxlmd, status, fe, hl);
716 writel(status & CXL_RAS_UNCORRECTABLE_STATUS_MASK, addr);
721 pci_ers_result_t cxl_error_detected(struct pci_dev *pdev,
722 pci_channel_state_t state)
724 struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
725 struct cxl_memdev *cxlmd = cxlds->cxlmd;
726 struct device *dev = &cxlmd->dev;
730 * A frozen channel indicates an impending reset which is fatal to
731 * CXL.mem operation, and will likely crash the system. On the off
732 * chance the situation is recoverable dump the status of the RAS
733 * capability registers and bounce the active state of the memdev.
735 ue = cxl_report_and_clear(cxlds);
738 case pci_channel_io_normal:
740 device_release_driver(dev);
741 return PCI_ERS_RESULT_NEED_RESET;
743 return PCI_ERS_RESULT_CAN_RECOVER;
744 case pci_channel_io_frozen:
746 "%s: frozen state error detected, disable CXL.mem\n",
748 device_release_driver(dev);
749 return PCI_ERS_RESULT_NEED_RESET;
750 case pci_channel_io_perm_failure:
752 "failure state error detected, request disconnect\n");
753 return PCI_ERS_RESULT_DISCONNECT;
755 return PCI_ERS_RESULT_NEED_RESET;
757 EXPORT_SYMBOL_NS_GPL(cxl_error_detected, CXL);