blob: 8dc91fd3396a398f1dc102a0f1cc27e4ac2424fb [file] [log] [blame]
Dan Williams4cdadfd2021-02-16 20:09:50 -08001// SPDX-License-Identifier: GPL-2.0-only
2/* Copyright(c) 2020 Intel Corporation. All rights reserved. */
Dan Williams4faf31b2021-09-08 22:12:32 -07003#include <linux/io-64-nonatomic-lo-hi.h>
Dan Williams4cdadfd2021-02-16 20:09:50 -08004#include <linux/module.h>
Dan Williamsfae88172021-04-16 17:43:30 -07005#include <linux/sizes.h>
Dan Williamsb39cb102021-02-16 20:09:52 -08006#include <linux/mutex.h>
Ira Weiny30af9722021-06-03 17:50:36 -07007#include <linux/list.h>
Dan Williams4cdadfd2021-02-16 20:09:50 -08008#include <linux/pci.h>
9#include <linux/io.h>
Ben Widawsky5161a552021-08-02 10:29:38 -070010#include "cxlmem.h"
Dan Williams4cdadfd2021-02-16 20:09:50 -080011#include "pci.h"
Ben Widawsky8adaf742021-02-16 20:09:51 -080012#include "cxl.h"
13
14/**
Ben Widawsky21e9f762021-05-26 10:44:13 -070015 * DOC: cxl pci
Ben Widawsky8adaf742021-02-16 20:09:51 -080016 *
Ben Widawsky21e9f762021-05-26 10:44:13 -070017 * This implements the PCI exclusive functionality for a CXL device as it is
18 * defined by the Compute Express Link specification. CXL devices may surface
Ben Widawskyed97afb2021-09-13 09:33:24 -070019 * certain functionality even if it isn't CXL enabled. While this driver is
20 * focused around the PCI specific aspects of a CXL device, it binds to the
21 * specific CXL memory device class code, and therefore the implementation of
22 * cxl_pci is focused around CXL memory devices.
Ben Widawsky8adaf742021-02-16 20:09:51 -080023 *
24 * The driver has several responsibilities, mainly:
25 * - Create the memX device and register on the CXL bus.
26 * - Enumerate device's register interface and map them.
Ben Widawskyed97afb2021-09-13 09:33:24 -070027 * - Registers nvdimm bridge device with cxl_core.
28 * - Registers a CXL mailbox with cxl_core.
Ben Widawsky8adaf742021-02-16 20:09:51 -080029 */
30
Ira Weiny5e2411a2021-11-02 13:29:01 -070031#define cxl_doorbell_busy(cxlds) \
32 (readl((cxlds)->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET) & \
Ben Widawsky8adaf742021-02-16 20:09:51 -080033 CXLDEV_MBOX_CTRL_DOORBELL)
34
35/* CXL 2.0 - 8.2.8.4 */
36#define CXL_MAILBOX_TIMEOUT_MS (2 * HZ)
37
Ira Weiny5e2411a2021-11-02 13:29:01 -070038static int cxl_pci_mbox_wait_for_doorbell(struct cxl_dev_state *cxlds)
Ben Widawsky8adaf742021-02-16 20:09:51 -080039{
40 const unsigned long start = jiffies;
41 unsigned long end = start;
42
Ira Weiny5e2411a2021-11-02 13:29:01 -070043 while (cxl_doorbell_busy(cxlds)) {
Ben Widawsky8adaf742021-02-16 20:09:51 -080044 end = jiffies;
45
46 if (time_after(end, start + CXL_MAILBOX_TIMEOUT_MS)) {
47 /* Check again in case preempted before timeout test */
Ira Weiny5e2411a2021-11-02 13:29:01 -070048 if (!cxl_doorbell_busy(cxlds))
Ben Widawsky8adaf742021-02-16 20:09:51 -080049 break;
50 return -ETIMEDOUT;
51 }
52 cpu_relax();
53 }
54
Ira Weiny5e2411a2021-11-02 13:29:01 -070055 dev_dbg(cxlds->dev, "Doorbell wait took %dms",
Ben Widawsky8adaf742021-02-16 20:09:51 -080056 jiffies_to_msecs(end) - jiffies_to_msecs(start));
57 return 0;
58}
59
Ira Weiny5e2411a2021-11-02 13:29:01 -070060static void cxl_pci_mbox_timeout(struct cxl_dev_state *cxlds,
Dan Williamsb64955a2021-09-08 22:12:21 -070061 struct cxl_mbox_cmd *mbox_cmd)
Ben Widawsky8adaf742021-02-16 20:09:51 -080062{
Ira Weiny5e2411a2021-11-02 13:29:01 -070063 struct device *dev = cxlds->dev;
Ben Widawsky8adaf742021-02-16 20:09:51 -080064
65 dev_dbg(dev, "Mailbox command (opcode: %#x size: %zub) timed out\n",
66 mbox_cmd->opcode, mbox_cmd->size_in);
67}
68
69/**
Ben Widawskyed97afb2021-09-13 09:33:24 -070070 * __cxl_pci_mbox_send_cmd() - Execute a mailbox command
Ira Weiny5e2411a2021-11-02 13:29:01 -070071 * @cxlds: The device state to communicate with.
Ben Widawsky8adaf742021-02-16 20:09:51 -080072 * @mbox_cmd: Command to send to the memory device.
73 *
74 * Context: Any context. Expects mbox_mutex to be held.
75 * Return: -ETIMEDOUT if timeout occurred waiting for completion. 0 on success.
76 * Caller should check the return code in @mbox_cmd to make sure it
77 * succeeded.
78 *
79 * This is a generic form of the CXL mailbox send command thus only using the
80 * registers defined by the mailbox capability ID - CXL 2.0 8.2.8.4. Memory
81 * devices, and perhaps other types of CXL devices may have further information
82 * available upon error conditions. Driver facilities wishing to send mailbox
83 * commands should use the wrapper command.
84 *
85 * The CXL spec allows for up to two mailboxes. The intention is for the primary
86 * mailbox to be OS controlled and the secondary mailbox to be used by system
87 * firmware. This allows the OS and firmware to communicate with the device and
88 * not need to coordinate with each other. The driver only uses the primary
89 * mailbox.
90 */
Ira Weiny5e2411a2021-11-02 13:29:01 -070091static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds,
Dan Williamsb64955a2021-09-08 22:12:21 -070092 struct cxl_mbox_cmd *mbox_cmd)
Ben Widawsky8adaf742021-02-16 20:09:51 -080093{
Ira Weiny5e2411a2021-11-02 13:29:01 -070094 void __iomem *payload = cxlds->regs.mbox + CXLDEV_MBOX_PAYLOAD_OFFSET;
95 struct device *dev = cxlds->dev;
Ben Widawsky8adaf742021-02-16 20:09:51 -080096 u64 cmd_reg, status_reg;
97 size_t out_len;
98 int rc;
99
Ira Weiny5e2411a2021-11-02 13:29:01 -0700100 lockdep_assert_held(&cxlds->mbox_mutex);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800101
102 /*
103 * Here are the steps from 8.2.8.4 of the CXL 2.0 spec.
104 * 1. Caller reads MB Control Register to verify doorbell is clear
105 * 2. Caller writes Command Register
106 * 3. Caller writes Command Payload Registers if input payload is non-empty
107 * 4. Caller writes MB Control Register to set doorbell
108 * 5. Caller either polls for doorbell to be clear or waits for interrupt if configured
109 * 6. Caller reads MB Status Register to fetch Return code
110 * 7. If command successful, Caller reads Command Register to get Payload Length
111 * 8. If output payload is non-empty, host reads Command Payload Registers
112 *
113 * Hardware is free to do whatever it wants before the doorbell is rung,
114 * and isn't allowed to change anything after it clears the doorbell. As
115 * such, steps 2 and 3 can happen in any order, and steps 6, 7, 8 can
116 * also happen in any order (though some orders might not make sense).
117 */
118
119 /* #1 */
Ira Weiny5e2411a2021-11-02 13:29:01 -0700120 if (cxl_doorbell_busy(cxlds)) {
Dan Williams99e222a2021-09-08 22:12:09 -0700121 dev_err_ratelimited(dev, "Mailbox re-busy after acquiring\n");
Ben Widawsky8adaf742021-02-16 20:09:51 -0800122 return -EBUSY;
123 }
124
125 cmd_reg = FIELD_PREP(CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK,
126 mbox_cmd->opcode);
127 if (mbox_cmd->size_in) {
128 if (WARN_ON(!mbox_cmd->payload_in))
129 return -EINVAL;
130
131 cmd_reg |= FIELD_PREP(CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK,
132 mbox_cmd->size_in);
133 memcpy_toio(payload, mbox_cmd->payload_in, mbox_cmd->size_in);
134 }
135
136 /* #2, #3 */
Ira Weiny5e2411a2021-11-02 13:29:01 -0700137 writeq(cmd_reg, cxlds->regs.mbox + CXLDEV_MBOX_CMD_OFFSET);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800138
139 /* #4 */
Dan Williams99e222a2021-09-08 22:12:09 -0700140 dev_dbg(dev, "Sending command\n");
Ben Widawsky8adaf742021-02-16 20:09:51 -0800141 writel(CXLDEV_MBOX_CTRL_DOORBELL,
Ira Weiny5e2411a2021-11-02 13:29:01 -0700142 cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800143
144 /* #5 */
Ira Weiny5e2411a2021-11-02 13:29:01 -0700145 rc = cxl_pci_mbox_wait_for_doorbell(cxlds);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800146 if (rc == -ETIMEDOUT) {
Ira Weiny5e2411a2021-11-02 13:29:01 -0700147 cxl_pci_mbox_timeout(cxlds, mbox_cmd);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800148 return rc;
149 }
150
151 /* #6 */
Ira Weiny5e2411a2021-11-02 13:29:01 -0700152 status_reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_STATUS_OFFSET);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800153 mbox_cmd->return_code =
154 FIELD_GET(CXLDEV_MBOX_STATUS_RET_CODE_MASK, status_reg);
155
156 if (mbox_cmd->return_code != 0) {
Dan Williams99e222a2021-09-08 22:12:09 -0700157 dev_dbg(dev, "Mailbox operation had an error\n");
Ben Widawsky8adaf742021-02-16 20:09:51 -0800158 return 0;
159 }
160
161 /* #7 */
Ira Weiny5e2411a2021-11-02 13:29:01 -0700162 cmd_reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_CMD_OFFSET);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800163 out_len = FIELD_GET(CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK, cmd_reg);
164
165 /* #8 */
166 if (out_len && mbox_cmd->payload_out) {
167 /*
168 * Sanitize the copy. If hardware misbehaves, out_len per the
169 * spec can actually be greater than the max allowed size (21
170 * bits available but spec defined 1M max). The caller also may
171 * have requested less data than the hardware supplied even
172 * within spec.
173 */
Ira Weiny5e2411a2021-11-02 13:29:01 -0700174 size_t n = min3(mbox_cmd->size_out, cxlds->payload_size, out_len);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800175
176 memcpy_fromio(mbox_cmd->payload_out, payload, n);
177 mbox_cmd->size_out = n;
178 } else {
179 mbox_cmd->size_out = 0;
180 }
181
182 return 0;
183}
184
185/**
Ben Widawskyed97afb2021-09-13 09:33:24 -0700186 * cxl_pci_mbox_get() - Acquire exclusive access to the mailbox.
Ira Weiny5e2411a2021-11-02 13:29:01 -0700187 * @cxlds: The device state to gain access to.
Ben Widawsky8adaf742021-02-16 20:09:51 -0800188 *
189 * Context: Any context. Takes the mbox_mutex.
190 * Return: 0 if exclusive access was acquired.
191 */
Ira Weiny5e2411a2021-11-02 13:29:01 -0700192static int cxl_pci_mbox_get(struct cxl_dev_state *cxlds)
Ben Widawsky8adaf742021-02-16 20:09:51 -0800193{
Ira Weiny5e2411a2021-11-02 13:29:01 -0700194 struct device *dev = cxlds->dev;
Ben Widawsky8adaf742021-02-16 20:09:51 -0800195 u64 md_status;
196 int rc;
197
Ira Weiny5e2411a2021-11-02 13:29:01 -0700198 mutex_lock_io(&cxlds->mbox_mutex);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800199
200 /*
201 * XXX: There is some amount of ambiguity in the 2.0 version of the spec
202 * around the mailbox interface ready (8.2.8.5.1.1). The purpose of the
203 * bit is to allow firmware running on the device to notify the driver
204 * that it's ready to receive commands. It is unclear if the bit needs
205 * to be read for each transaction mailbox, ie. the firmware can switch
206 * it on and off as needed. Second, there is no defined timeout for
207 * mailbox ready, like there is for the doorbell interface.
208 *
209 * Assumptions:
210 * 1. The firmware might toggle the Mailbox Interface Ready bit, check
211 * it for every command.
212 *
213 * 2. If the doorbell is clear, the firmware should have first set the
214 * Mailbox Interface Ready bit. Therefore, waiting for the doorbell
215 * to be ready is sufficient.
216 */
Ira Weiny5e2411a2021-11-02 13:29:01 -0700217 rc = cxl_pci_mbox_wait_for_doorbell(cxlds);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800218 if (rc) {
219 dev_warn(dev, "Mailbox interface not ready\n");
220 goto out;
221 }
222
Ira Weiny5e2411a2021-11-02 13:29:01 -0700223 md_status = readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800224 if (!(md_status & CXLMDEV_MBOX_IF_READY && CXLMDEV_READY(md_status))) {
225 dev_err(dev, "mbox: reported doorbell ready, but not mbox ready\n");
226 rc = -EBUSY;
227 goto out;
228 }
229
230 /*
231 * Hardware shouldn't allow a ready status but also have failure bits
232 * set. Spit out an error, this should be a bug report
233 */
234 rc = -EFAULT;
235 if (md_status & CXLMDEV_DEV_FATAL) {
236 dev_err(dev, "mbox: reported ready, but fatal\n");
237 goto out;
238 }
239 if (md_status & CXLMDEV_FW_HALT) {
240 dev_err(dev, "mbox: reported ready, but halted\n");
241 goto out;
242 }
243 if (CXLMDEV_RESET_NEEDED(md_status)) {
244 dev_err(dev, "mbox: reported ready, but reset needed\n");
245 goto out;
246 }
247
248 /* with lock held */
249 return 0;
250
251out:
Ira Weiny5e2411a2021-11-02 13:29:01 -0700252 mutex_unlock(&cxlds->mbox_mutex);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800253 return rc;
254}
255
256/**
Ben Widawskyed97afb2021-09-13 09:33:24 -0700257 * cxl_pci_mbox_put() - Release exclusive access to the mailbox.
Ira Weiny5e2411a2021-11-02 13:29:01 -0700258 * @cxlds: The device state to communicate with.
Ben Widawsky8adaf742021-02-16 20:09:51 -0800259 *
260 * Context: Any context. Expects mbox_mutex to be held.
261 */
Ira Weiny5e2411a2021-11-02 13:29:01 -0700262static void cxl_pci_mbox_put(struct cxl_dev_state *cxlds)
Ben Widawsky8adaf742021-02-16 20:09:51 -0800263{
Ira Weiny5e2411a2021-11-02 13:29:01 -0700264 mutex_unlock(&cxlds->mbox_mutex);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800265}
266
Ira Weiny5e2411a2021-11-02 13:29:01 -0700267static int cxl_pci_mbox_send(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd)
Dan Williamsb64955a2021-09-08 22:12:21 -0700268{
269 int rc;
270
Ira Weiny5e2411a2021-11-02 13:29:01 -0700271 rc = cxl_pci_mbox_get(cxlds);
Dan Williamsb64955a2021-09-08 22:12:21 -0700272 if (rc)
273 return rc;
274
Ira Weiny5e2411a2021-11-02 13:29:01 -0700275 rc = __cxl_pci_mbox_send_cmd(cxlds, cmd);
276 cxl_pci_mbox_put(cxlds);
Dan Williamsb64955a2021-09-08 22:12:21 -0700277
278 return rc;
279}
280
Ira Weiny5e2411a2021-11-02 13:29:01 -0700281static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds)
Ben Widawsky8adaf742021-02-16 20:09:51 -0800282{
Ira Weiny5e2411a2021-11-02 13:29:01 -0700283 const int cap = readl(cxlds->regs.mbox + CXLDEV_MBOX_CAPS_OFFSET);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800284
Ira Weiny5e2411a2021-11-02 13:29:01 -0700285 cxlds->mbox_send = cxl_pci_mbox_send;
286 cxlds->payload_size =
Ben Widawsky8adaf742021-02-16 20:09:51 -0800287 1 << FIELD_GET(CXLDEV_MBOX_CAP_PAYLOAD_SIZE_MASK, cap);
288
289 /*
290 * CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register
291 *
292 * If the size is too small, mandatory commands will not work and so
293 * there's no point in going forward. If the size is too large, there's
294 * no harm is soft limiting it.
295 */
Ira Weiny5e2411a2021-11-02 13:29:01 -0700296 cxlds->payload_size = min_t(size_t, cxlds->payload_size, SZ_1M);
297 if (cxlds->payload_size < 256) {
298 dev_err(cxlds->dev, "Mailbox is too small (%zub)",
299 cxlds->payload_size);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800300 return -ENXIO;
301 }
302
Ira Weiny5e2411a2021-11-02 13:29:01 -0700303 dev_dbg(cxlds->dev, "Mailbox payload sized %zu",
304 cxlds->payload_size);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800305
306 return 0;
307}
308
Dan Williamsa261e9a2021-10-15 14:57:27 -0700309static int cxl_map_regblock(struct pci_dev *pdev, struct cxl_register_map *map)
Ben Widawsky1b0a1a22021-04-07 15:26:20 -0700310{
Ira Weinyf8a7e8c2021-05-27 17:49:19 -0700311 void __iomem *addr;
Ben Widawsky7dc7a642021-10-13 16:53:29 -0700312 int bar = map->barno;
313 struct device *dev = &pdev->dev;
314 resource_size_t offset = map->block_offset;
Ben Widawsky1b0a1a22021-04-07 15:26:20 -0700315
Ben Widawsky8adaf742021-02-16 20:09:51 -0800316 /* Basic sanity check that BAR is big enough */
317 if (pci_resource_len(pdev, bar) < offset) {
Ben Widawsky7dc7a642021-10-13 16:53:29 -0700318 dev_err(dev, "BAR%d: %pr: too small (offset: %pa)\n", bar,
319 &pdev->resource[bar], &offset);
Dan Williamsa261e9a2021-10-15 14:57:27 -0700320 return -ENXIO;
Ben Widawsky8adaf742021-02-16 20:09:51 -0800321 }
322
Ira Weiny30af9722021-06-03 17:50:36 -0700323 addr = pci_iomap(pdev, bar, 0);
Ira Weinyf8a7e8c2021-05-27 17:49:19 -0700324 if (!addr) {
Ben Widawsky8adaf742021-02-16 20:09:51 -0800325 dev_err(dev, "failed to map registers\n");
Dan Williamsa261e9a2021-10-15 14:57:27 -0700326 return -ENOMEM;
Ben Widawsky8adaf742021-02-16 20:09:51 -0800327 }
Ben Widawsky8adaf742021-02-16 20:09:51 -0800328
Ben Widawsky7dc7a642021-10-13 16:53:29 -0700329 dev_dbg(dev, "Mapped CXL Memory Device resource bar %u @ %pa\n",
330 bar, &offset);
Ben Widawsky6630d312021-05-20 14:29:53 -0700331
Dan Williamsa261e9a2021-10-15 14:57:27 -0700332 map->base = addr + map->block_offset;
333 return 0;
Ira Weiny30af9722021-06-03 17:50:36 -0700334}
335
Dan Williamsa261e9a2021-10-15 14:57:27 -0700336static void cxl_unmap_regblock(struct pci_dev *pdev,
337 struct cxl_register_map *map)
Ira Weiny30af9722021-06-03 17:50:36 -0700338{
Dan Williamsa261e9a2021-10-15 14:57:27 -0700339 pci_iounmap(pdev, map->base - map->block_offset);
340 map->base = NULL;
Ben Widawsky8adaf742021-02-16 20:09:51 -0800341}
Dan Williams4cdadfd2021-02-16 20:09:50 -0800342
Dan Williamsa261e9a2021-10-15 14:57:27 -0700343static int cxl_probe_regs(struct pci_dev *pdev, struct cxl_register_map *map)
Ira Weiny30af9722021-06-03 17:50:36 -0700344{
Ben Widawsky08422372021-05-27 17:49:22 -0700345 struct cxl_component_reg_map *comp_map;
Ira Weiny30af9722021-06-03 17:50:36 -0700346 struct cxl_device_reg_map *dev_map;
Ben Widawsky7dc7a642021-10-13 16:53:29 -0700347 struct device *dev = &pdev->dev;
Dan Williamsa261e9a2021-10-15 14:57:27 -0700348 void __iomem *base = map->base;
Ira Weiny30af9722021-06-03 17:50:36 -0700349
350 switch (map->reg_type) {
Ben Widawsky08422372021-05-27 17:49:22 -0700351 case CXL_REGLOC_RBI_COMPONENT:
352 comp_map = &map->component_map;
353 cxl_probe_component_regs(dev, base, comp_map);
354 if (!comp_map->hdm_decoder.valid) {
355 dev_err(dev, "HDM decoder registers not found\n");
356 return -ENXIO;
357 }
358
359 dev_dbg(dev, "Set up component registers\n");
360 break;
Ira Weiny30af9722021-06-03 17:50:36 -0700361 case CXL_REGLOC_RBI_MEMDEV:
362 dev_map = &map->device_map;
363 cxl_probe_device_regs(dev, base, dev_map);
364 if (!dev_map->status.valid || !dev_map->mbox.valid ||
365 !dev_map->memdev.valid) {
366 dev_err(dev, "registers not found: %s%s%s\n",
367 !dev_map->status.valid ? "status " : "",
Li Qiang (Johnny Li)da582aa2021-09-03 19:20:50 -0700368 !dev_map->mbox.valid ? "mbox " : "",
369 !dev_map->memdev.valid ? "memdev " : "");
Ira Weiny30af9722021-06-03 17:50:36 -0700370 return -ENXIO;
371 }
372
373 dev_dbg(dev, "Probing device registers...\n");
374 break;
375 default:
376 break;
377 }
378
379 return 0;
380}
381
Ira Weiny5e2411a2021-11-02 13:29:01 -0700382static int cxl_map_regs(struct cxl_dev_state *cxlds, struct cxl_register_map *map)
Ira Weiny30af9722021-06-03 17:50:36 -0700383{
Ira Weiny5e2411a2021-11-02 13:29:01 -0700384 struct device *dev = cxlds->dev;
Dan Williams99e222a2021-09-08 22:12:09 -0700385 struct pci_dev *pdev = to_pci_dev(dev);
Ira Weiny30af9722021-06-03 17:50:36 -0700386
387 switch (map->reg_type) {
Ben Widawsky08422372021-05-27 17:49:22 -0700388 case CXL_REGLOC_RBI_COMPONENT:
Ira Weiny5e2411a2021-11-02 13:29:01 -0700389 cxl_map_component_regs(pdev, &cxlds->regs.component, map);
Ben Widawsky08422372021-05-27 17:49:22 -0700390 dev_dbg(dev, "Mapping component registers...\n");
391 break;
Ira Weiny30af9722021-06-03 17:50:36 -0700392 case CXL_REGLOC_RBI_MEMDEV:
Ira Weiny5e2411a2021-11-02 13:29:01 -0700393 cxl_map_device_regs(pdev, &cxlds->regs.device_regs, map);
Ira Weiny30af9722021-06-03 17:50:36 -0700394 dev_dbg(dev, "Probing device registers...\n");
395 break;
396 default:
397 break;
398 }
399
400 return 0;
401}
402
Ben Widawsky7dc7a642021-10-13 16:53:29 -0700403static void cxl_decode_regblock(u32 reg_lo, u32 reg_hi,
404 struct cxl_register_map *map)
Ira Weiny07d62ea2021-05-27 17:49:18 -0700405{
Ben Widawsky7dc7a642021-10-13 16:53:29 -0700406 map->block_offset =
407 ((u64)reg_hi << 32) | (reg_lo & CXL_REGLOC_ADDR_MASK);
408 map->barno = FIELD_GET(CXL_REGLOC_BIR_MASK, reg_lo);
409 map->reg_type = FIELD_GET(CXL_REGLOC_RBI_MASK, reg_lo);
Ira Weiny07d62ea2021-05-27 17:49:18 -0700410}
411
Ben Widawsky1d5a4152021-04-07 15:26:21 -0700412/**
Ben Widawsky85afc312021-10-15 16:30:42 -0700413 * cxl_find_regblock() - Locate register blocks by type
414 * @pdev: The CXL PCI device to enumerate.
415 * @type: Register Block Indicator id
416 * @map: Enumeration output, clobbered on error
Ben Widawsky1d5a4152021-04-07 15:26:21 -0700417 *
Ben Widawsky85afc312021-10-15 16:30:42 -0700418 * Return: 0 if register block enumerated, negative error code otherwise
Ben Widawsky1d5a4152021-04-07 15:26:21 -0700419 *
Ben Widawsky85afc312021-10-15 16:30:42 -0700420 * A CXL DVSEC may point to one or more register blocks, search for them
421 * by @type.
Ben Widawsky1d5a4152021-04-07 15:26:21 -0700422 */
Ben Widawsky85afc312021-10-15 16:30:42 -0700423static int cxl_find_regblock(struct pci_dev *pdev, enum cxl_regloc_type type,
424 struct cxl_register_map *map)
Ben Widawsky1d5a4152021-04-07 15:26:21 -0700425{
Dan Williams99e222a2021-09-08 22:12:09 -0700426 u32 regloc_size, regblocks;
Ben Widawsky85afc312021-10-15 16:30:42 -0700427 int regloc, i;
Ben Widawsky1d5a4152021-04-07 15:26:21 -0700428
Ben Widawsky55006a2c2021-10-09 09:44:45 -0700429 regloc = pci_find_dvsec_capability(pdev, PCI_DVSEC_VENDOR_ID_CXL,
430 PCI_DVSEC_ID_CXL_REGLOC_DVSEC_ID);
Ben Widawsky85afc312021-10-15 16:30:42 -0700431 if (!regloc)
Ben Widawsky1d5a4152021-04-07 15:26:21 -0700432 return -ENXIO;
Ben Widawsky1d5a4152021-04-07 15:26:21 -0700433
Ben Widawsky1d5a4152021-04-07 15:26:21 -0700434 pci_read_config_dword(pdev, regloc + PCI_DVSEC_HEADER1, &regloc_size);
435 regloc_size = FIELD_GET(PCI_DVSEC_HEADER1_LENGTH_MASK, regloc_size);
436
437 regloc += PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET;
438 regblocks = (regloc_size - PCI_DVSEC_ID_CXL_REGLOC_BLOCK1_OFFSET) / 8;
439
Ben Widawsky85afc312021-10-15 16:30:42 -0700440 for (i = 0; i < regblocks; i++, regloc += 8) {
Ben Widawsky1d5a4152021-04-07 15:26:21 -0700441 u32 reg_lo, reg_hi;
Ben Widawsky1d5a4152021-04-07 15:26:21 -0700442
Ben Widawsky1d5a4152021-04-07 15:26:21 -0700443 pci_read_config_dword(pdev, regloc, &reg_lo);
444 pci_read_config_dword(pdev, regloc + 4, &reg_hi);
445
Ben Widawsky7dc7a642021-10-13 16:53:29 -0700446 cxl_decode_regblock(reg_lo, reg_hi, map);
Ira Weiny07d62ea2021-05-27 17:49:18 -0700447
Ben Widawsky85afc312021-10-15 16:30:42 -0700448 if (map->reg_type == type)
449 return 0;
Ben Widawsky1d5a4152021-04-07 15:26:21 -0700450 }
451
Ben Widawsky85afc312021-10-15 16:30:42 -0700452 return -ENODEV;
453}
Ben Widawsky1d5a4152021-04-07 15:26:21 -0700454
Ben Widawsky85afc312021-10-15 16:30:42 -0700455static int cxl_setup_regs(struct pci_dev *pdev, enum cxl_regloc_type type,
456 struct cxl_register_map *map)
457{
458 int rc;
459
460 rc = cxl_find_regblock(pdev, type, map);
461 if (rc)
462 return rc;
463
464 rc = cxl_map_regblock(pdev, map);
465 if (rc)
466 return rc;
467
468 rc = cxl_probe_regs(pdev, map);
469 cxl_unmap_regblock(pdev, map);
470
471 return rc;
Ben Widawsky1d5a4152021-04-07 15:26:21 -0700472}
473
Ben Widawskyed97afb2021-09-13 09:33:24 -0700474static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Dan Williams4cdadfd2021-02-16 20:09:50 -0800475{
Ben Widawsky85afc312021-10-15 16:30:42 -0700476 struct cxl_register_map map;
Dan Williams21083f52021-06-15 16:36:31 -0700477 struct cxl_memdev *cxlmd;
Ira Weiny5e2411a2021-11-02 13:29:01 -0700478 struct cxl_dev_state *cxlds;
Ben Widawsky1d5a4152021-04-07 15:26:21 -0700479 int rc;
Ben Widawsky8adaf742021-02-16 20:09:51 -0800480
Dan Williams5a2328f2021-09-08 22:12:38 -0700481 /*
482 * Double check the anonymous union trickery in struct cxl_regs
483 * FIXME switch to struct_group()
484 */
485 BUILD_BUG_ON(offsetof(struct cxl_regs, memdev) !=
486 offsetof(struct cxl_regs, device_regs.memdev));
487
Ben Widawsky8adaf742021-02-16 20:09:51 -0800488 rc = pcim_enable_device(pdev);
489 if (rc)
490 return rc;
Dan Williams4cdadfd2021-02-16 20:09:50 -0800491
Ira Weiny5e2411a2021-11-02 13:29:01 -0700492 cxlds = cxl_dev_state_create(&pdev->dev);
493 if (IS_ERR(cxlds))
494 return PTR_ERR(cxlds);
Ben Widawsky1b0a1a22021-04-07 15:26:20 -0700495
Ben Widawsky85afc312021-10-15 16:30:42 -0700496 rc = cxl_setup_regs(pdev, CXL_REGLOC_RBI_MEMDEV, &map);
497 if (rc)
498 return rc;
499
Ira Weiny5e2411a2021-11-02 13:29:01 -0700500 rc = cxl_map_regs(cxlds, &map);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800501 if (rc)
502 return rc;
503
Ira Weiny5e2411a2021-11-02 13:29:01 -0700504 rc = cxl_pci_setup_mailbox(cxlds);
Ben Widawsky8adaf742021-02-16 20:09:51 -0800505 if (rc)
506 return rc;
507
Ira Weiny5e2411a2021-11-02 13:29:01 -0700508 rc = cxl_enumerate_cmds(cxlds);
Ben Widawsky472b1ce2021-02-16 20:09:55 -0800509 if (rc)
510 return rc;
511
Ira Weiny5e2411a2021-11-02 13:29:01 -0700512 rc = cxl_dev_state_identify(cxlds);
Dan Williamsb39cb102021-02-16 20:09:52 -0800513 if (rc)
514 return rc;
515
Ira Weiny5e2411a2021-11-02 13:29:01 -0700516 rc = cxl_mem_create_range_info(cxlds);
Ira Weinyf8475022021-08-10 11:57:59 -0700517 if (rc)
518 return rc;
519
Ira Weiny5e2411a2021-11-02 13:29:01 -0700520 cxlmd = devm_cxl_add_memdev(cxlds);
Dan Williams21083f52021-06-15 16:36:31 -0700521 if (IS_ERR(cxlmd))
522 return PTR_ERR(cxlmd);
523
Ira Weiny5e2411a2021-11-02 13:29:01 -0700524 if (range_len(&cxlds->pmem_range) && IS_ENABLED(CONFIG_CXL_PMEM))
Dan Williams21083f52021-06-15 16:36:31 -0700525 rc = devm_cxl_add_nvdimm(&pdev->dev, cxlmd);
526
527 return rc;
Dan Williams4cdadfd2021-02-16 20:09:50 -0800528}
529
530static const struct pci_device_id cxl_mem_pci_tbl[] = {
531 /* PCI class code for CXL.mem Type-3 Devices */
532 { PCI_DEVICE_CLASS((PCI_CLASS_MEMORY_CXL << 8 | CXL_MEMORY_PROGIF), ~0)},
533 { /* terminate list */ },
534};
535MODULE_DEVICE_TABLE(pci, cxl_mem_pci_tbl);
536
Ben Widawskyed97afb2021-09-13 09:33:24 -0700537static struct pci_driver cxl_pci_driver = {
Dan Williams4cdadfd2021-02-16 20:09:50 -0800538 .name = KBUILD_MODNAME,
539 .id_table = cxl_mem_pci_tbl,
Ben Widawskyed97afb2021-09-13 09:33:24 -0700540 .probe = cxl_pci_probe,
Dan Williams4cdadfd2021-02-16 20:09:50 -0800541 .driver = {
542 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
543 },
544};
545
546MODULE_LICENSE("GPL v2");
Ben Widawskyed97afb2021-09-13 09:33:24 -0700547module_pci_driver(cxl_pci_driver);
Dan Williamsb39cb102021-02-16 20:09:52 -0800548MODULE_IMPORT_NS(CXL);