1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2021 Intel Corporation. All rights reserved. */
3 #include <linux/units.h>
4 #include <linux/io-64-nonatomic-lo-hi.h>
5 #include <linux/device.h>
6 #include <linux/delay.h>
8 #include <linux/pci-doe.h>
19 * Compute Express Link protocols are layered on top of PCIe. CXL core provides
20 * a set of helpers for CXL interactions which occur via PCIe.
23 static unsigned short media_ready_timeout = 60;
24 module_param(media_ready_timeout, ushort, 0644);
25 MODULE_PARM_DESC(media_ready_timeout, "seconds to wait for media ready");
27 struct cxl_walk_context {
29 struct cxl_port *port;
35 static int match_add_dports(struct pci_dev *pdev, void *data)
37 struct cxl_walk_context *ctx = data;
38 struct cxl_port *port = ctx->port;
39 int type = pci_pcie_type(pdev);
40 struct cxl_register_map map;
41 struct cxl_dport *dport;
45 if (pdev->bus != ctx->bus)
47 if (!pci_is_pcie(pdev))
49 if (type != ctx->type)
51 if (pci_read_config_dword(pdev, pci_pcie_cap(pdev) + PCI_EXP_LNKCAP,
55 rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
57 dev_dbg(&port->dev, "failed to find component registers\n");
59 port_num = FIELD_GET(PCI_EXP_LNKCAP_PN, lnkcap);
60 dport = devm_cxl_add_dport(port, &pdev->dev, port_num, map.resource);
62 ctx->error = PTR_ERR(dport);
63 return PTR_ERR(dport);
71 * devm_cxl_port_enumerate_dports - enumerate downstream ports of the upstream port
72 * @port: cxl_port whose ->uport_dev is the upstream of dports to be enumerated
74 * Returns a positive number of dports enumerated or a negative error
77 int devm_cxl_port_enumerate_dports(struct cxl_port *port)
79 struct pci_bus *bus = cxl_port_to_pci_bus(port);
80 struct cxl_walk_context ctx;
86 if (pci_is_root_bus(bus))
87 type = PCI_EXP_TYPE_ROOT_PORT;
89 type = PCI_EXP_TYPE_DOWNSTREAM;
91 ctx = (struct cxl_walk_context) {
96 pci_walk_bus(bus, match_add_dports, &ctx);
104 EXPORT_SYMBOL_NS_GPL(devm_cxl_port_enumerate_dports, "CXL");
106 static int cxl_dvsec_mem_range_valid(struct cxl_dev_state *cxlds, int id)
108 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
109 int d = cxlds->cxl_dvsec;
114 if (id > CXL_DVSEC_RANGE_MAX)
117 /* Check MEM INFO VALID bit first, give up after 1s */
120 rc = pci_read_config_dword(pdev,
121 d + CXL_DVSEC_RANGE_SIZE_LOW(id),
126 valid = FIELD_GET(CXL_DVSEC_MEM_INFO_VALID, temp);
134 "Timeout awaiting memory range %d valid after 1s.\n",
142 static int cxl_dvsec_mem_range_active(struct cxl_dev_state *cxlds, int id)
144 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
145 int d = cxlds->cxl_dvsec;
150 if (id > CXL_DVSEC_RANGE_MAX)
153 /* Check MEM ACTIVE bit, up to 60s timeout by default */
154 for (i = media_ready_timeout; i; i--) {
155 rc = pci_read_config_dword(
156 pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(id), &temp);
160 active = FIELD_GET(CXL_DVSEC_MEM_ACTIVE, temp);
168 "timeout awaiting memory active after %d seconds\n",
169 media_ready_timeout);
177 * Wait up to @media_ready_timeout for the device to report memory
180 int cxl_await_media_ready(struct cxl_dev_state *cxlds)
182 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
183 int d = cxlds->cxl_dvsec;
184 int rc, i, hdm_count;
188 rc = pci_read_config_word(pdev,
189 d + CXL_DVSEC_CAP_OFFSET, &cap);
193 hdm_count = FIELD_GET(CXL_DVSEC_HDM_COUNT_MASK, cap);
194 for (i = 0; i < hdm_count; i++) {
195 rc = cxl_dvsec_mem_range_valid(cxlds, i);
200 for (i = 0; i < hdm_count; i++) {
201 rc = cxl_dvsec_mem_range_active(cxlds, i);
206 md_status = readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
207 if (!CXLMDEV_READY(md_status))
212 EXPORT_SYMBOL_NS_GPL(cxl_await_media_ready, "CXL");
214 static int cxl_set_mem_enable(struct cxl_dev_state *cxlds, u16 val)
216 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
217 int d = cxlds->cxl_dvsec;
221 rc = pci_read_config_word(pdev, d + CXL_DVSEC_CTRL_OFFSET, &ctrl);
225 if ((ctrl & CXL_DVSEC_MEM_ENABLE) == val)
227 ctrl &= ~CXL_DVSEC_MEM_ENABLE;
230 rc = pci_write_config_word(pdev, d + CXL_DVSEC_CTRL_OFFSET, ctrl);
237 static void clear_mem_enable(void *cxlds)
239 cxl_set_mem_enable(cxlds, 0);
242 static int devm_cxl_enable_mem(struct device *host, struct cxl_dev_state *cxlds)
246 rc = cxl_set_mem_enable(cxlds, CXL_DVSEC_MEM_ENABLE);
251 return devm_add_action_or_reset(host, clear_mem_enable, cxlds);
254 /* require dvsec ranges to be covered by a locked platform window */
255 static int dvsec_range_allowed(struct device *dev, const void *arg)
257 const struct range *dev_range = arg;
258 struct cxl_decoder *cxld;
260 if (!is_root_decoder(dev))
263 cxld = to_cxl_decoder(dev);
265 if (!(cxld->flags & CXL_DECODER_F_RAM))
268 return range_contains(&cxld->hpa_range, dev_range);
271 static void disable_hdm(void *_cxlhdm)
274 struct cxl_hdm *cxlhdm = _cxlhdm;
275 void __iomem *hdm = cxlhdm->regs.hdm_decoder;
277 global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
278 writel(global_ctrl & ~CXL_HDM_DECODER_ENABLE,
279 hdm + CXL_HDM_DECODER_CTRL_OFFSET);
282 static int devm_cxl_enable_hdm(struct device *host, struct cxl_hdm *cxlhdm)
284 void __iomem *hdm = cxlhdm->regs.hdm_decoder;
287 global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
288 writel(global_ctrl | CXL_HDM_DECODER_ENABLE,
289 hdm + CXL_HDM_DECODER_CTRL_OFFSET);
291 return devm_add_action_or_reset(host, disable_hdm, cxlhdm);
294 int cxl_dvsec_rr_decode(struct cxl_dev_state *cxlds,
295 struct cxl_endpoint_dvsec_info *info)
297 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
298 struct device *dev = cxlds->dev;
299 int hdm_count, rc, i, ranges = 0;
300 int d = cxlds->cxl_dvsec;
304 dev_dbg(dev, "No DVSEC Capability\n");
308 rc = pci_read_config_word(pdev, d + CXL_DVSEC_CAP_OFFSET, &cap);
312 if (!(cap & CXL_DVSEC_MEM_CAPABLE)) {
313 dev_dbg(dev, "Not MEM Capable\n");
318 * It is not allowed by spec for MEM.capable to be set and have 0 legacy
319 * HDM decoders (values > 2 are also undefined as of CXL 2.0). As this
320 * driver is for a spec defined class code which must be CXL.mem
321 * capable, there is no point in continuing to enable CXL.mem.
323 hdm_count = FIELD_GET(CXL_DVSEC_HDM_COUNT_MASK, cap);
324 if (!hdm_count || hdm_count > 2)
328 * The current DVSEC values are moot if the memory capability is
329 * disabled, and they will remain moot after the HDM Decoder
330 * capability is enabled.
332 rc = pci_read_config_word(pdev, d + CXL_DVSEC_CTRL_OFFSET, &ctrl);
336 info->mem_enabled = FIELD_GET(CXL_DVSEC_MEM_ENABLE, ctrl);
337 if (!info->mem_enabled)
340 for (i = 0; i < hdm_count; i++) {
344 rc = cxl_dvsec_mem_range_valid(cxlds, i);
348 rc = pci_read_config_dword(
349 pdev, d + CXL_DVSEC_RANGE_SIZE_HIGH(i), &temp);
353 size = (u64)temp << 32;
355 rc = pci_read_config_dword(
356 pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(i), &temp);
360 size |= temp & CXL_DVSEC_MEM_SIZE_LOW_MASK;
365 rc = pci_read_config_dword(
366 pdev, d + CXL_DVSEC_RANGE_BASE_HIGH(i), &temp);
370 base = (u64)temp << 32;
372 rc = pci_read_config_dword(
373 pdev, d + CXL_DVSEC_RANGE_BASE_LOW(i), &temp);
377 base |= temp & CXL_DVSEC_MEM_BASE_LOW_MASK;
379 info->dvsec_range[ranges++] = (struct range) {
381 .end = base + size - 1
385 info->ranges = ranges;
389 EXPORT_SYMBOL_NS_GPL(cxl_dvsec_rr_decode, "CXL");
392 * cxl_hdm_decode_init() - Setup HDM decoding for the endpoint
393 * @cxlds: Device state
394 * @cxlhdm: Mapped HDM decoder Capability
395 * @info: Cached DVSEC range registers info
397 * Try to enable the endpoint's HDM Decoder Capability
399 int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm,
400 struct cxl_endpoint_dvsec_info *info)
402 void __iomem *hdm = cxlhdm->regs.hdm_decoder;
403 struct cxl_port *port = cxlhdm->port;
404 struct device *dev = cxlds->dev;
405 struct cxl_port *root;
410 global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
413 * If the HDM Decoder Capability is already enabled then assume
414 * that some other agent like platform firmware set it up.
416 if (global_ctrl & CXL_HDM_DECODER_ENABLE || (!hdm && info->mem_enabled))
417 return devm_cxl_enable_mem(&port->dev, cxlds);
421 root = to_cxl_port(port->dev.parent);
422 while (!is_cxl_root(root) && is_cxl_port(root->dev.parent))
423 root = to_cxl_port(root->dev.parent);
424 if (!is_cxl_root(root)) {
425 dev_err(dev, "Failed to acquire root port for HDM enable\n");
429 if (!info->mem_enabled) {
430 rc = devm_cxl_enable_hdm(&port->dev, cxlhdm);
434 return devm_cxl_enable_mem(&port->dev, cxlds);
437 for (i = 0, allowed = 0; i < info->ranges; i++) {
438 struct device *cxld_dev;
440 cxld_dev = device_find_child(&root->dev, &info->dvsec_range[i],
441 dvsec_range_allowed);
443 dev_dbg(dev, "DVSEC Range%d denied by platform\n", i);
446 dev_dbg(dev, "DVSEC Range%d allowed by platform\n", i);
447 put_device(cxld_dev);
452 dev_err(dev, "Range register decodes outside platform defined CXL ranges.\n");
457 * Per CXL 2.0 Section 8.1.3.8.3 and 8.1.3.8.4 DVSEC CXL Range 1 Base
458 * [High,Low] when HDM operation is enabled the range register values
459 * are ignored by the device, but the spec also recommends matching the
460 * DVSEC Range 1,2 to HDM Decoder Range 0,1. So, non-zero info->ranges
461 * are expected even though Linux does not require or maintain that
462 * match. If at least one DVSEC range is enabled and allowed, skip HDM
463 * Decoder Capability Enable.
467 EXPORT_SYMBOL_NS_GPL(cxl_hdm_decode_init, "CXL");
469 #define CXL_DOE_TABLE_ACCESS_REQ_CODE 0x000000ff
470 #define CXL_DOE_TABLE_ACCESS_REQ_CODE_READ 0
471 #define CXL_DOE_TABLE_ACCESS_TABLE_TYPE 0x0000ff00
472 #define CXL_DOE_TABLE_ACCESS_TABLE_TYPE_CDATA 0
473 #define CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE 0xffff0000
474 #define CXL_DOE_TABLE_ACCESS_LAST_ENTRY 0xffff
475 #define CXL_DOE_PROTOCOL_TABLE_ACCESS 2
477 #define CDAT_DOE_REQ(entry_handle) cpu_to_le32 \
478 (FIELD_PREP(CXL_DOE_TABLE_ACCESS_REQ_CODE, \
479 CXL_DOE_TABLE_ACCESS_REQ_CODE_READ) | \
480 FIELD_PREP(CXL_DOE_TABLE_ACCESS_TABLE_TYPE, \
481 CXL_DOE_TABLE_ACCESS_TABLE_TYPE_CDATA) | \
482 FIELD_PREP(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE, (entry_handle)))
484 static int cxl_cdat_get_length(struct device *dev,
485 struct pci_doe_mb *doe_mb,
488 __le32 request = CDAT_DOE_REQ(0);
492 rc = pci_doe(doe_mb, PCI_VENDOR_ID_CXL,
493 CXL_DOE_PROTOCOL_TABLE_ACCESS,
494 &request, sizeof(request),
495 &response, sizeof(response));
497 dev_err(dev, "DOE failed: %d", rc);
500 if (rc < sizeof(response))
503 *length = le32_to_cpu(response[1]);
504 dev_dbg(dev, "CDAT length %zu\n", *length);
509 static int cxl_cdat_read_table(struct device *dev,
510 struct pci_doe_mb *doe_mb,
511 struct cdat_doe_rsp *rsp, size_t *length)
513 size_t received, remaining = *length;
514 unsigned int entry_handle = 0;
515 union cdat_data *data;
519 __le32 request = CDAT_DOE_REQ(entry_handle);
522 rc = pci_doe(doe_mb, PCI_VENDOR_ID_CXL,
523 CXL_DOE_PROTOCOL_TABLE_ACCESS,
524 &request, sizeof(request),
525 rsp, sizeof(*rsp) + remaining);
527 dev_err(dev, "DOE failed: %d", rc);
531 if (rc < sizeof(*rsp))
534 data = (union cdat_data *)rsp->data;
535 received = rc - sizeof(*rsp);
537 if (entry_handle == 0) {
538 if (received != sizeof(data->header))
541 if (received < sizeof(data->entry) ||
542 received != le16_to_cpu(data->entry.length))
546 /* Get the CXL table access header entry handle */
547 entry_handle = FIELD_GET(CXL_DOE_TABLE_ACCESS_ENTRY_HANDLE,
548 le32_to_cpu(rsp->doe_header));
551 * Table Access Response Header overwrote the last DW of
552 * previous entry, so restore that DW
554 rsp->doe_header = saved_dw;
555 remaining -= received;
556 rsp = (void *)rsp + received;
557 saved_dw = rsp->doe_header;
558 } while (entry_handle != CXL_DOE_TABLE_ACCESS_LAST_ENTRY);
560 /* Length in CDAT header may exceed concatenation of CDAT entries */
561 *length -= remaining;
566 static unsigned char cdat_checksum(void *buf, size_t size)
568 unsigned char sum, *data = buf;
571 for (sum = 0, i = 0; i < size; i++)
577 * read_cdat_data - Read the CDAT data on this port
578 * @port: Port to read data from
580 * This call will sleep waiting for responses from the DOE mailbox.
582 void read_cdat_data(struct cxl_port *port)
584 struct device *uport = port->uport_dev;
585 struct device *dev = &port->dev;
586 struct pci_doe_mb *doe_mb;
587 struct pci_dev *pdev = NULL;
588 struct cxl_memdev *cxlmd;
589 struct cdat_doe_rsp *buf;
590 size_t table_length, length;
593 if (is_cxl_memdev(uport)) {
596 cxlmd = to_cxl_memdev(uport);
597 host = cxlmd->dev.parent;
598 if (dev_is_pci(host))
599 pdev = to_pci_dev(host);
600 } else if (dev_is_pci(uport)) {
601 pdev = to_pci_dev(uport);
607 doe_mb = pci_find_doe_mailbox(pdev, PCI_VENDOR_ID_CXL,
608 CXL_DOE_PROTOCOL_TABLE_ACCESS);
610 dev_dbg(dev, "No CDAT mailbox\n");
614 port->cdat_available = true;
616 if (cxl_cdat_get_length(dev, doe_mb, &length)) {
617 dev_dbg(dev, "No CDAT length\n");
622 * The begin of the CDAT buffer needs space for additional 4
623 * bytes for the DOE header. Table data starts afterwards.
625 buf = devm_kzalloc(dev, sizeof(*buf) + length, GFP_KERNEL);
629 table_length = length;
631 rc = cxl_cdat_read_table(dev, doe_mb, buf, &length);
635 if (table_length != length)
636 dev_warn(dev, "Malformed CDAT table length (%zu:%zu), discarding trailing data\n",
637 table_length, length);
639 if (cdat_checksum(buf->data, length))
642 port->cdat.table = buf->data;
643 port->cdat.length = length;
647 /* Don't leave table data allocated on error */
648 devm_kfree(dev, buf);
649 dev_err(dev, "Failed to read/validate CDAT.\n");
651 EXPORT_SYMBOL_NS_GPL(read_cdat_data, "CXL");
653 static void __cxl_handle_cor_ras(struct cxl_dev_state *cxlds,
654 void __iomem *ras_base)
662 addr = ras_base + CXL_RAS_CORRECTABLE_STATUS_OFFSET;
663 status = readl(addr);
664 if (status & CXL_RAS_CORRECTABLE_STATUS_MASK) {
665 writel(status & CXL_RAS_CORRECTABLE_STATUS_MASK, addr);
666 trace_cxl_aer_correctable_error(cxlds->cxlmd, status);
670 static void cxl_handle_endpoint_cor_ras(struct cxl_dev_state *cxlds)
672 return __cxl_handle_cor_ras(cxlds, cxlds->regs.ras);
675 /* CXL spec rev3.0 8.2.4.16.1 */
676 static void header_log_copy(void __iomem *ras_base, u32 *log)
680 int i, log_u32_size = CXL_HEADERLOG_SIZE / sizeof(u32);
682 addr = ras_base + CXL_RAS_HEADER_LOG_OFFSET;
685 for (i = 0; i < log_u32_size; i++) {
686 *log_addr = readl(addr);
693 * Log the state of the RAS status registers and prepare them to log the
694 * next error status. Return 1 if reset needed.
696 static bool __cxl_handle_ras(struct cxl_dev_state *cxlds,
697 void __iomem *ras_base)
699 u32 hl[CXL_HEADERLOG_SIZE_U32];
707 addr = ras_base + CXL_RAS_UNCORRECTABLE_STATUS_OFFSET;
708 status = readl(addr);
709 if (!(status & CXL_RAS_UNCORRECTABLE_STATUS_MASK))
712 /* If multiple errors, log header points to first error from ctrl reg */
713 if (hweight32(status) > 1) {
714 void __iomem *rcc_addr =
715 ras_base + CXL_RAS_CAP_CONTROL_OFFSET;
717 fe = BIT(FIELD_GET(CXL_RAS_CAP_CONTROL_FE_MASK,
723 header_log_copy(ras_base, hl);
724 trace_cxl_aer_uncorrectable_error(cxlds->cxlmd, status, fe, hl);
725 writel(status & CXL_RAS_UNCORRECTABLE_STATUS_MASK, addr);
730 static bool cxl_handle_endpoint_ras(struct cxl_dev_state *cxlds)
732 return __cxl_handle_ras(cxlds, cxlds->regs.ras);
735 #ifdef CONFIG_PCIEAER_CXL
737 static void cxl_dport_map_rch_aer(struct cxl_dport *dport)
739 resource_size_t aer_phys;
743 aer_cap = cxl_rcrb_to_aer(dport->dport_dev, dport->rcrb.base);
745 host = dport->reg_map.host;
746 aer_phys = aer_cap + dport->rcrb.base;
747 dport->regs.dport_aer = devm_cxl_iomap_block(host, aer_phys,
748 sizeof(struct aer_capability_regs));
752 static void cxl_dport_map_ras(struct cxl_dport *dport)
754 struct cxl_register_map *map = &dport->reg_map;
755 struct device *dev = dport->dport_dev;
757 if (!map->component_map.ras.valid)
758 dev_dbg(dev, "RAS registers not found\n");
759 else if (cxl_map_component_regs(map, &dport->regs.component,
760 BIT(CXL_CM_CAP_CAP_ID_RAS)))
761 dev_dbg(dev, "Failed to map RAS capability.\n");
764 static void cxl_disable_rch_root_ints(struct cxl_dport *dport)
766 void __iomem *aer_base = dport->regs.dport_aer;
767 u32 aer_cmd_mask, aer_cmd;
773 * Disable RCH root port command interrupts.
774 * CXL 3.0 12.2.1.1 - RCH Downstream Port-detected Errors
776 * This sequence may not be necessary. CXL spec states disabling
777 * the root cmd register's interrupts is required. But, PCI spec
778 * shows these are disabled by default on reset.
780 aer_cmd_mask = (PCI_ERR_ROOT_CMD_COR_EN |
781 PCI_ERR_ROOT_CMD_NONFATAL_EN |
782 PCI_ERR_ROOT_CMD_FATAL_EN);
783 aer_cmd = readl(aer_base + PCI_ERR_ROOT_COMMAND);
784 aer_cmd &= ~aer_cmd_mask;
785 writel(aer_cmd, aer_base + PCI_ERR_ROOT_COMMAND);
789 * cxl_dport_init_ras_reporting - Setup CXL RAS report on this dport
790 * @dport: the cxl_dport that needs to be initialized
791 * @host: host device for devm operations
793 void cxl_dport_init_ras_reporting(struct cxl_dport *dport, struct device *host)
795 dport->reg_map.host = host;
796 cxl_dport_map_ras(dport);
799 struct pci_host_bridge *host_bridge = to_pci_host_bridge(dport->dport_dev);
801 if (!host_bridge->native_aer)
804 cxl_dport_map_rch_aer(dport);
805 cxl_disable_rch_root_ints(dport);
808 EXPORT_SYMBOL_NS_GPL(cxl_dport_init_ras_reporting, "CXL");
810 static void cxl_handle_rdport_cor_ras(struct cxl_dev_state *cxlds,
811 struct cxl_dport *dport)
813 return __cxl_handle_cor_ras(cxlds, dport->regs.ras);
816 static bool cxl_handle_rdport_ras(struct cxl_dev_state *cxlds,
817 struct cxl_dport *dport)
819 return __cxl_handle_ras(cxlds, dport->regs.ras);
823 * Copy the AER capability registers using 32 bit read accesses.
824 * This is necessary because RCRB AER capability is MMIO mapped. Clear the
825 * status after copying.
827 * @aer_base: base address of AER capability block in RCRB
828 * @aer_regs: destination for copying AER capability
830 static bool cxl_rch_get_aer_info(void __iomem *aer_base,
831 struct aer_capability_regs *aer_regs)
833 int read_cnt = sizeof(struct aer_capability_regs) / sizeof(u32);
834 u32 *aer_regs_buf = (u32 *)aer_regs;
840 /* Use readl() to guarantee 32-bit accesses */
841 for (n = 0; n < read_cnt; n++)
842 aer_regs_buf[n] = readl(aer_base + n * sizeof(u32));
844 writel(aer_regs->uncor_status, aer_base + PCI_ERR_UNCOR_STATUS);
845 writel(aer_regs->cor_status, aer_base + PCI_ERR_COR_STATUS);
850 /* Get AER severity. Return false if there is no error. */
851 static bool cxl_rch_get_aer_severity(struct aer_capability_regs *aer_regs,
854 if (aer_regs->uncor_status & ~aer_regs->uncor_mask) {
855 if (aer_regs->uncor_status & PCI_ERR_ROOT_FATAL_RCV)
856 *severity = AER_FATAL;
858 *severity = AER_NONFATAL;
862 if (aer_regs->cor_status & ~aer_regs->cor_mask) {
863 *severity = AER_CORRECTABLE;
870 static void cxl_handle_rdport_errors(struct cxl_dev_state *cxlds)
872 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
873 struct aer_capability_regs aer_regs;
874 struct cxl_dport *dport;
877 struct cxl_port *port __free(put_cxl_port) =
878 cxl_pci_find_port(pdev, &dport);
882 if (!cxl_rch_get_aer_info(dport->regs.dport_aer, &aer_regs))
885 if (!cxl_rch_get_aer_severity(&aer_regs, &severity))
888 pci_print_aer(pdev, severity, &aer_regs);
890 if (severity == AER_CORRECTABLE)
891 cxl_handle_rdport_cor_ras(cxlds, dport);
893 cxl_handle_rdport_ras(cxlds, dport);
897 static void cxl_handle_rdport_errors(struct cxl_dev_state *cxlds) { }
900 void cxl_cor_error_detected(struct pci_dev *pdev)
902 struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
903 struct device *dev = &cxlds->cxlmd->dev;
905 scoped_guard(device, dev) {
908 "%s: memdev disabled, abort error handling\n",
914 cxl_handle_rdport_errors(cxlds);
916 cxl_handle_endpoint_cor_ras(cxlds);
919 EXPORT_SYMBOL_NS_GPL(cxl_cor_error_detected, "CXL");
921 pci_ers_result_t cxl_error_detected(struct pci_dev *pdev,
922 pci_channel_state_t state)
924 struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
925 struct cxl_memdev *cxlmd = cxlds->cxlmd;
926 struct device *dev = &cxlmd->dev;
929 scoped_guard(device, dev) {
932 "%s: memdev disabled, abort error handling\n",
934 return PCI_ERS_RESULT_DISCONNECT;
938 cxl_handle_rdport_errors(cxlds);
940 * A frozen channel indicates an impending reset which is fatal to
941 * CXL.mem operation, and will likely crash the system. On the off
942 * chance the situation is recoverable dump the status of the RAS
943 * capability registers and bounce the active state of the memdev.
945 ue = cxl_handle_endpoint_ras(cxlds);
950 case pci_channel_io_normal:
952 device_release_driver(dev);
953 return PCI_ERS_RESULT_NEED_RESET;
955 return PCI_ERS_RESULT_CAN_RECOVER;
956 case pci_channel_io_frozen:
958 "%s: frozen state error detected, disable CXL.mem\n",
960 device_release_driver(dev);
961 return PCI_ERS_RESULT_NEED_RESET;
962 case pci_channel_io_perm_failure:
964 "failure state error detected, request disconnect\n");
965 return PCI_ERS_RESULT_DISCONNECT;
967 return PCI_ERS_RESULT_NEED_RESET;
969 EXPORT_SYMBOL_NS_GPL(cxl_error_detected, "CXL");
971 static int cxl_flit_size(struct pci_dev *pdev)
973 if (cxl_pci_flit_256(pdev))
980 * cxl_pci_get_latency - calculate the link latency for the PCIe link
983 * return: calculated latency or 0 for no latency
985 * CXL Memory Device SW Guide v1.0 2.11.4 Link latency calculation
986 * Link latency = LinkPropagationLatency + FlitLatency + RetimerLatency
987 * LinkProgationLatency is negligible, so 0 will be used
988 * RetimerLatency is assumed to be negligible and 0 will be used
989 * FlitLatency = FlitSize / LinkBandwidth
990 * FlitSize is defined by spec. CXL rev3.0 4.2.1.
991 * 68B flit is used up to 32GT/s. >32GT/s, 256B flit size is used.
992 * The FlitLatency is converted to picoseconds.
994 long cxl_pci_get_latency(struct pci_dev *pdev)
998 bw = pcie_link_speed_mbps(pdev);
1001 bw /= BITS_PER_BYTE;
1003 return cxl_flit_size(pdev) * MEGA / bw;
1006 static int __cxl_endpoint_decoder_reset_detected(struct device *dev, void *data)
1008 struct cxl_port *port = data;
1009 struct cxl_decoder *cxld;
1010 struct cxl_hdm *cxlhdm;
1014 if (!is_endpoint_decoder(dev))
1017 cxld = to_cxl_decoder(dev);
1018 if ((cxld->flags & CXL_DECODER_F_ENABLE) == 0)
1021 cxlhdm = dev_get_drvdata(&port->dev);
1022 hdm = cxlhdm->regs.hdm_decoder;
1023 ctrl = readl(hdm + CXL_HDM_DECODER0_CTRL_OFFSET(cxld->id));
1025 return !FIELD_GET(CXL_HDM_DECODER0_CTRL_COMMITTED, ctrl);
1028 bool cxl_endpoint_decoder_reset_detected(struct cxl_port *port)
1030 return device_for_each_child(&port->dev, port,
1031 __cxl_endpoint_decoder_reset_detected);
1033 EXPORT_SYMBOL_NS_GPL(cxl_endpoint_decoder_reset_detected, "CXL");
1035 int cxl_pci_get_bandwidth(struct pci_dev *pdev, struct access_coordinate *c)
1041 speed = pcie_link_speed_mbps(pdev);
1044 speed /= BITS_PER_BYTE;
1046 pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnksta);
1047 width = FIELD_GET(PCI_EXP_LNKSTA_NLW, lnksta);
1050 for (int i = 0; i < ACCESS_COORDINATE_MAX; i++) {
1051 c[i].read_bandwidth = bw;
1052 c[i].write_bandwidth = bw;