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 bool __cxl_hdm_decode_init(struct cxl_dev_state *cxlds,
179 struct cxl_hdm *cxlhdm,
180 struct cxl_endpoint_dvsec_info *info)
182 void __iomem *hdm = cxlhdm->regs.hdm_decoder;
186 global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
187 global_enable = global_ctrl & CXL_HDM_DECODER_ENABLE;
190 * Per CXL 2.0 Section 8.1.3.8.3 and 8.1.3.8.4 DVSEC CXL Range 1 Base
191 * [High,Low] when HDM operation is enabled the range register values
192 * are ignored by the device, but the spec also recommends matching the
193 * DVSEC Range 1,2 to HDM Decoder Range 0,1. So, non-zero info->ranges
194 * are expected even though Linux does not require or maintain that
197 if (!global_enable && info->mem_enabled && info->ranges)
201 * Permanently (for this boot at least) opt the device into HDM
202 * operation. Individual HDM decoders still need to be enabled after
205 if (!global_enable) {
206 dev_dbg(cxlds->dev, "Enabling HDM decode\n");
207 writel(global_ctrl | CXL_HDM_DECODER_ENABLE,
208 hdm + CXL_HDM_DECODER_CTRL_OFFSET);
215 * cxl_hdm_decode_init() - Setup HDM decoding for the endpoint
216 * @cxlds: Device state
217 * @cxlhdm: Mapped HDM decoder Capability
219 * Try to enable the endpoint's HDM Decoder Capability
221 int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm)
223 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
224 struct cxl_endpoint_dvsec_info info = { 0 };
225 int hdm_count, rc, i, ranges = 0;
226 struct device *dev = &pdev->dev;
227 int d = cxlds->cxl_dvsec;
231 dev_dbg(dev, "No DVSEC Capability\n");
235 rc = pci_read_config_word(pdev, d + CXL_DVSEC_CAP_OFFSET, &cap);
239 rc = pci_read_config_word(pdev, d + CXL_DVSEC_CTRL_OFFSET, &ctrl);
243 if (!(cap & CXL_DVSEC_MEM_CAPABLE)) {
244 dev_dbg(dev, "Not MEM Capable\n");
249 * It is not allowed by spec for MEM.capable to be set and have 0 legacy
250 * HDM decoders (values > 2 are also undefined as of CXL 2.0). As this
251 * driver is for a spec defined class code which must be CXL.mem
252 * capable, there is no point in continuing to enable CXL.mem.
254 hdm_count = FIELD_GET(CXL_DVSEC_HDM_COUNT_MASK, cap);
255 if (!hdm_count || hdm_count > 2)
258 rc = wait_for_valid(cxlds);
260 dev_dbg(dev, "Failure awaiting MEM_INFO_VALID (%d)\n", rc);
264 info.mem_enabled = FIELD_GET(CXL_DVSEC_MEM_ENABLE, ctrl);
265 if (!info.mem_enabled)
268 for (i = 0; i < hdm_count; i++) {
272 rc = pci_read_config_dword(
273 pdev, d + CXL_DVSEC_RANGE_SIZE_HIGH(i), &temp);
277 size = (u64)temp << 32;
279 rc = pci_read_config_dword(
280 pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(i), &temp);
284 size |= temp & CXL_DVSEC_MEM_SIZE_LOW_MASK;
286 rc = pci_read_config_dword(
287 pdev, d + CXL_DVSEC_RANGE_BASE_HIGH(i), &temp);
291 base = (u64)temp << 32;
293 rc = pci_read_config_dword(
294 pdev, d + CXL_DVSEC_RANGE_BASE_LOW(i), &temp);
298 base |= temp & CXL_DVSEC_MEM_BASE_LOW_MASK;
300 info.dvsec_range[i] = (struct range) {
302 .end = base + size - 1
309 info.ranges = ranges;
312 * If DVSEC ranges are being used instead of HDM decoder registers there
313 * is no use in trying to manage those.
315 if (!__cxl_hdm_decode_init(cxlds, cxlhdm, &info)) {
317 "Legacy range registers configuration prevents HDM operation.\n");
323 EXPORT_SYMBOL_NS_GPL(cxl_hdm_decode_init, CXL);