]> Git Repo - J-linux.git/blob - drivers/cxl/core/pci.c
cxl/port: Reuse 'struct cxl_hdm' context for hdm init
[J-linux.git] / drivers / cxl / core / pci.c
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>
6 #include <linux/pci.h>
7 #include <cxlpci.h>
8 #include <cxlmem.h>
9 #include <cxl.h>
10 #include "core.h"
11
12 /**
13  * DOC: cxl core pci
14  *
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.
17  */
18
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");
22
23 struct cxl_walk_context {
24         struct pci_bus *bus;
25         struct cxl_port *port;
26         int type;
27         int error;
28         int count;
29 };
30
31 static int match_add_dports(struct pci_dev *pdev, void *data)
32 {
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;
38         u32 lnkcap, port_num;
39         int rc;
40
41         if (pdev->bus != ctx->bus)
42                 return 0;
43         if (!pci_is_pcie(pdev))
44                 return 0;
45         if (type != ctx->type)
46                 return 0;
47         if (pci_read_config_dword(pdev, pci_pcie_cap(pdev) + PCI_EXP_LNKCAP,
48                                   &lnkcap))
49                 return 0;
50
51         rc = cxl_find_regblock(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
52         if (rc)
53                 dev_dbg(&port->dev, "failed to find component registers\n");
54
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));
58         if (IS_ERR(dport)) {
59                 ctx->error = PTR_ERR(dport);
60                 return PTR_ERR(dport);
61         }
62         ctx->count++;
63
64         dev_dbg(&port->dev, "add dport%d: %s\n", port_num, dev_name(&pdev->dev));
65
66         return 0;
67 }
68
69 /**
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
72  *
73  * Returns a positive number of dports enumerated or a negative error
74  * code.
75  */
76 int devm_cxl_port_enumerate_dports(struct cxl_port *port)
77 {
78         struct pci_bus *bus = cxl_port_to_pci_bus(port);
79         struct cxl_walk_context ctx;
80         int type;
81
82         if (!bus)
83                 return -ENXIO;
84
85         if (pci_is_root_bus(bus))
86                 type = PCI_EXP_TYPE_ROOT_PORT;
87         else
88                 type = PCI_EXP_TYPE_DOWNSTREAM;
89
90         ctx = (struct cxl_walk_context) {
91                 .port = port,
92                 .bus = bus,
93                 .type = type,
94         };
95         pci_walk_bus(bus, match_add_dports, &ctx);
96
97         if (ctx.count == 0)
98                 return -ENODEV;
99         if (ctx.error)
100                 return ctx.error;
101         return ctx.count;
102 }
103 EXPORT_SYMBOL_NS_GPL(devm_cxl_port_enumerate_dports, CXL);
104
105 /*
106  * Wait up to @media_ready_timeout for the device to report memory
107  * active.
108  */
109 int cxl_await_media_ready(struct cxl_dev_state *cxlds)
110 {
111         struct pci_dev *pdev = to_pci_dev(cxlds->dev);
112         int d = cxlds->cxl_dvsec;
113         bool active = false;
114         u64 md_status;
115         int rc, i;
116
117         for (i = media_ready_timeout; i; i--) {
118                 u32 temp;
119
120                 rc = pci_read_config_dword(
121                         pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(0), &temp);
122                 if (rc)
123                         return rc;
124
125                 active = FIELD_GET(CXL_DVSEC_MEM_ACTIVE, temp);
126                 if (active)
127                         break;
128                 msleep(1000);
129         }
130
131         if (!active) {
132                 dev_err(&pdev->dev,
133                         "timeout awaiting memory active after %d seconds\n",
134                         media_ready_timeout);
135                 return -ETIMEDOUT;
136         }
137
138         md_status = readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
139         if (!CXLMDEV_READY(md_status))
140                 return -EIO;
141
142         return 0;
143 }
144 EXPORT_SYMBOL_NS_GPL(cxl_await_media_ready, CXL);
145
146 static int wait_for_valid(struct cxl_dev_state *cxlds)
147 {
148         struct pci_dev *pdev = to_pci_dev(cxlds->dev);
149         int d = cxlds->cxl_dvsec, rc;
150         u32 val;
151
152         /*
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
157          * clock skew.
158          */
159         rc = pci_read_config_dword(pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(0), &val);
160         if (rc)
161                 return rc;
162
163         if (val & CXL_DVSEC_MEM_INFO_VALID)
164                 return 0;
165
166         msleep(1500);
167
168         rc = pci_read_config_dword(pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(0), &val);
169         if (rc)
170                 return rc;
171
172         if (val & CXL_DVSEC_MEM_INFO_VALID)
173                 return 0;
174
175         return -ETIMEDOUT;
176 }
177
178 static bool __cxl_hdm_decode_init(struct cxl_dev_state *cxlds,
179                                   struct cxl_hdm *cxlhdm,
180                                   struct cxl_endpoint_dvsec_info *info)
181 {
182         void __iomem *hdm = cxlhdm->regs.hdm_decoder;
183         bool global_enable;
184         u32 global_ctrl;
185
186         global_ctrl = readl(hdm + CXL_HDM_DECODER_CTRL_OFFSET);
187         global_enable = global_ctrl & CXL_HDM_DECODER_ENABLE;
188
189         /*
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
195          * match.
196          */
197         if (!global_enable && info->mem_enabled && info->ranges)
198                 return false;
199
200         /*
201          * Permanently (for this boot at least) opt the device into HDM
202          * operation. Individual HDM decoders still need to be enabled after
203          * this point.
204          */
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);
209         }
210
211         return true;
212 }
213
214 /**
215  * cxl_hdm_decode_init() - Setup HDM decoding for the endpoint
216  * @cxlds: Device state
217  * @cxlhdm: Mapped HDM decoder Capability
218  *
219  * Try to enable the endpoint's HDM Decoder Capability
220  */
221 int cxl_hdm_decode_init(struct cxl_dev_state *cxlds, struct cxl_hdm *cxlhdm)
222 {
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;
228         u16 cap, ctrl;
229
230         if (!d) {
231                 dev_dbg(dev, "No DVSEC Capability\n");
232                 return -ENXIO;
233         }
234
235         rc = pci_read_config_word(pdev, d + CXL_DVSEC_CAP_OFFSET, &cap);
236         if (rc)
237                 return rc;
238
239         rc = pci_read_config_word(pdev, d + CXL_DVSEC_CTRL_OFFSET, &ctrl);
240         if (rc)
241                 return rc;
242
243         if (!(cap & CXL_DVSEC_MEM_CAPABLE)) {
244                 dev_dbg(dev, "Not MEM Capable\n");
245                 return -ENXIO;
246         }
247
248         /*
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.
253          */
254         hdm_count = FIELD_GET(CXL_DVSEC_HDM_COUNT_MASK, cap);
255         if (!hdm_count || hdm_count > 2)
256                 return -EINVAL;
257
258         rc = wait_for_valid(cxlds);
259         if (rc) {
260                 dev_dbg(dev, "Failure awaiting MEM_INFO_VALID (%d)\n", rc);
261                 return rc;
262         }
263
264         info.mem_enabled = FIELD_GET(CXL_DVSEC_MEM_ENABLE, ctrl);
265         if (!info.mem_enabled)
266                 return 0;
267
268         for (i = 0; i < hdm_count; i++) {
269                 u64 base, size;
270                 u32 temp;
271
272                 rc = pci_read_config_dword(
273                         pdev, d + CXL_DVSEC_RANGE_SIZE_HIGH(i), &temp);
274                 if (rc)
275                         return rc;
276
277                 size = (u64)temp << 32;
278
279                 rc = pci_read_config_dword(
280                         pdev, d + CXL_DVSEC_RANGE_SIZE_LOW(i), &temp);
281                 if (rc)
282                         return rc;
283
284                 size |= temp & CXL_DVSEC_MEM_SIZE_LOW_MASK;
285
286                 rc = pci_read_config_dword(
287                         pdev, d + CXL_DVSEC_RANGE_BASE_HIGH(i), &temp);
288                 if (rc)
289                         return rc;
290
291                 base = (u64)temp << 32;
292
293                 rc = pci_read_config_dword(
294                         pdev, d + CXL_DVSEC_RANGE_BASE_LOW(i), &temp);
295                 if (rc)
296                         return rc;
297
298                 base |= temp & CXL_DVSEC_MEM_BASE_LOW_MASK;
299
300                 info.dvsec_range[i] = (struct range) {
301                         .start = base,
302                         .end = base + size - 1
303                 };
304
305                 if (size)
306                         ranges++;
307         }
308
309         info.ranges = ranges;
310
311         /*
312          * If DVSEC ranges are being used instead of HDM decoder registers there
313          * is no use in trying to manage those.
314          */
315         if (!__cxl_hdm_decode_init(cxlds, cxlhdm, &info)) {
316                 dev_err(dev,
317                         "Legacy range registers configuration prevents HDM operation.\n");
318                 return -EBUSY;
319         }
320
321         return 0;
322 }
323 EXPORT_SYMBOL_NS_GPL(cxl_hdm_decode_init, CXL);
This page took 0.04666 seconds and 4 git commands to generate.