1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright(c) 2020 Intel Corporation. All rights reserved. */
3 #include <linux/io-64-nonatomic-lo-hi.h>
4 #include <linux/moduleparam.h>
5 #include <linux/module.h>
6 #include <linux/delay.h>
7 #include <linux/sizes.h>
8 #include <linux/mutex.h>
9 #include <linux/list.h>
10 #include <linux/pci.h>
11 #include <linux/aer.h>
20 * This implements the PCI exclusive functionality for a CXL device as it is
21 * defined by the Compute Express Link specification. CXL devices may surface
22 * certain functionality even if it isn't CXL enabled. While this driver is
23 * focused around the PCI specific aspects of a CXL device, it binds to the
24 * specific CXL memory device class code, and therefore the implementation of
25 * cxl_pci is focused around CXL memory devices.
27 * The driver has several responsibilities, mainly:
28 * - Create the memX device and register on the CXL bus.
29 * - Enumerate device's register interface and map them.
30 * - Registers nvdimm bridge device with cxl_core.
31 * - Registers a CXL mailbox with cxl_core.
34 #define cxl_doorbell_busy(cxlds) \
35 (readl((cxlds)->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET) & \
36 CXLDEV_MBOX_CTRL_DOORBELL)
38 /* CXL 2.0 - 8.2.8.4 */
39 #define CXL_MAILBOX_TIMEOUT_MS (2 * HZ)
42 * CXL 2.0 ECN "Add Mailbox Ready Time" defines a capability field to
43 * dictate how long to wait for the mailbox to become ready. The new
44 * field allows the device to tell software the amount of time to wait
45 * before mailbox ready. This field per the spec theoretically allows
46 * for up to 255 seconds. 255 seconds is unreasonably long, its longer
47 * than the maximum SATA port link recovery wait. Default to 60 seconds
48 * until someone builds a CXL device that needs more time in practice.
50 static unsigned short mbox_ready_timeout = 60;
51 module_param(mbox_ready_timeout, ushort, 0644);
52 MODULE_PARM_DESC(mbox_ready_timeout, "seconds to wait for mailbox ready");
54 static int cxl_pci_mbox_wait_for_doorbell(struct cxl_dev_state *cxlds)
56 const unsigned long start = jiffies;
57 unsigned long end = start;
59 while (cxl_doorbell_busy(cxlds)) {
62 if (time_after(end, start + CXL_MAILBOX_TIMEOUT_MS)) {
63 /* Check again in case preempted before timeout test */
64 if (!cxl_doorbell_busy(cxlds))
71 dev_dbg(cxlds->dev, "Doorbell wait took %dms",
72 jiffies_to_msecs(end) - jiffies_to_msecs(start));
76 #define cxl_err(dev, status, msg) \
77 dev_err_ratelimited(dev, msg ", device state %s%s\n", \
78 status & CXLMDEV_DEV_FATAL ? " fatal" : "", \
79 status & CXLMDEV_FW_HALT ? " firmware-halt" : "")
81 #define cxl_cmd_err(dev, cmd, status, msg) \
82 dev_err_ratelimited(dev, msg " (opcode: %#x), device state %s%s\n", \
84 status & CXLMDEV_DEV_FATAL ? " fatal" : "", \
85 status & CXLMDEV_FW_HALT ? " firmware-halt" : "")
88 struct cxl_dev_state *cxlds;
91 static int cxl_request_irq(struct cxl_dev_state *cxlds, int irq,
92 irq_handler_t handler, irq_handler_t thread_fn)
94 struct device *dev = cxlds->dev;
95 struct cxl_dev_id *dev_id;
97 /* dev_id must be globally unique and must contain the cxlds */
98 dev_id = devm_kzalloc(dev, sizeof(*dev_id), GFP_KERNEL);
101 dev_id->cxlds = cxlds;
103 return devm_request_threaded_irq(dev, irq, handler, thread_fn,
104 IRQF_SHARED | IRQF_ONESHOT,
108 static bool cxl_mbox_background_complete(struct cxl_dev_state *cxlds)
112 reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_BG_CMD_STATUS_OFFSET);
113 return FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_PCT_MASK, reg) == 100;
116 static irqreturn_t cxl_pci_mbox_irq(int irq, void *id)
120 struct cxl_dev_id *dev_id = id;
121 struct cxl_dev_state *cxlds = dev_id->cxlds;
122 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds);
124 if (!cxl_mbox_background_complete(cxlds))
127 reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_BG_CMD_STATUS_OFFSET);
128 opcode = FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_OPCODE_MASK, reg);
129 if (opcode == CXL_MBOX_OP_SANITIZE) {
130 if (mds->security.sanitize_node)
131 sysfs_notify_dirent(mds->security.sanitize_node);
133 dev_dbg(cxlds->dev, "Sanitization operation ended\n");
135 /* short-circuit the wait in __cxl_pci_mbox_send_cmd() */
136 rcuwait_wake_up(&mds->mbox_wait);
143 * Sanitization operation polling mode.
145 static void cxl_mbox_sanitize_work(struct work_struct *work)
147 struct cxl_memdev_state *mds =
148 container_of(work, typeof(*mds), security.poll_dwork.work);
149 struct cxl_dev_state *cxlds = &mds->cxlds;
151 mutex_lock(&mds->mbox_mutex);
152 if (cxl_mbox_background_complete(cxlds)) {
153 mds->security.poll_tmo_secs = 0;
154 put_device(cxlds->dev);
156 if (mds->security.sanitize_node)
157 sysfs_notify_dirent(mds->security.sanitize_node);
159 dev_dbg(cxlds->dev, "Sanitization operation ended\n");
161 int timeout = mds->security.poll_tmo_secs + 10;
163 mds->security.poll_tmo_secs = min(15 * 60, timeout);
164 queue_delayed_work(system_wq, &mds->security.poll_dwork,
167 mutex_unlock(&mds->mbox_mutex);
171 * __cxl_pci_mbox_send_cmd() - Execute a mailbox command
172 * @mds: The memory device driver data
173 * @mbox_cmd: Command to send to the memory device.
175 * Context: Any context. Expects mbox_mutex to be held.
176 * Return: -ETIMEDOUT if timeout occurred waiting for completion. 0 on success.
177 * Caller should check the return code in @mbox_cmd to make sure it
180 * This is a generic form of the CXL mailbox send command thus only using the
181 * registers defined by the mailbox capability ID - CXL 2.0 8.2.8.4. Memory
182 * devices, and perhaps other types of CXL devices may have further information
183 * available upon error conditions. Driver facilities wishing to send mailbox
184 * commands should use the wrapper command.
186 * The CXL spec allows for up to two mailboxes. The intention is for the primary
187 * mailbox to be OS controlled and the secondary mailbox to be used by system
188 * firmware. This allows the OS and firmware to communicate with the device and
189 * not need to coordinate with each other. The driver only uses the primary
192 static int __cxl_pci_mbox_send_cmd(struct cxl_memdev_state *mds,
193 struct cxl_mbox_cmd *mbox_cmd)
195 struct cxl_dev_state *cxlds = &mds->cxlds;
196 void __iomem *payload = cxlds->regs.mbox + CXLDEV_MBOX_PAYLOAD_OFFSET;
197 struct device *dev = cxlds->dev;
198 u64 cmd_reg, status_reg;
202 lockdep_assert_held(&mds->mbox_mutex);
205 * Here are the steps from 8.2.8.4 of the CXL 2.0 spec.
206 * 1. Caller reads MB Control Register to verify doorbell is clear
207 * 2. Caller writes Command Register
208 * 3. Caller writes Command Payload Registers if input payload is non-empty
209 * 4. Caller writes MB Control Register to set doorbell
210 * 5. Caller either polls for doorbell to be clear or waits for interrupt if configured
211 * 6. Caller reads MB Status Register to fetch Return code
212 * 7. If command successful, Caller reads Command Register to get Payload Length
213 * 8. If output payload is non-empty, host reads Command Payload Registers
215 * Hardware is free to do whatever it wants before the doorbell is rung,
216 * and isn't allowed to change anything after it clears the doorbell. As
217 * such, steps 2 and 3 can happen in any order, and steps 6, 7, 8 can
218 * also happen in any order (though some orders might not make sense).
222 if (cxl_doorbell_busy(cxlds)) {
224 readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
226 cxl_cmd_err(cxlds->dev, mbox_cmd, md_status,
227 "mailbox queue busy");
232 * With sanitize polling, hardware might be done and the poller still
233 * not be in sync. Ensure no new command comes in until so. Keep the
234 * hardware semantics and only allow device health status.
236 if (mds->security.poll_tmo_secs > 0) {
237 if (mbox_cmd->opcode != CXL_MBOX_OP_GET_HEALTH_INFO)
241 cmd_reg = FIELD_PREP(CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK,
243 if (mbox_cmd->size_in) {
244 if (WARN_ON(!mbox_cmd->payload_in))
247 cmd_reg |= FIELD_PREP(CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK,
249 memcpy_toio(payload, mbox_cmd->payload_in, mbox_cmd->size_in);
253 writeq(cmd_reg, cxlds->regs.mbox + CXLDEV_MBOX_CMD_OFFSET);
256 dev_dbg(dev, "Sending command: 0x%04x\n", mbox_cmd->opcode);
257 writel(CXLDEV_MBOX_CTRL_DOORBELL,
258 cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET);
261 rc = cxl_pci_mbox_wait_for_doorbell(cxlds);
262 if (rc == -ETIMEDOUT) {
263 u64 md_status = readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
265 cxl_cmd_err(cxlds->dev, mbox_cmd, md_status, "mailbox timeout");
270 status_reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_STATUS_OFFSET);
271 mbox_cmd->return_code =
272 FIELD_GET(CXLDEV_MBOX_STATUS_RET_CODE_MASK, status_reg);
275 * Handle the background command in a synchronous manner.
277 * All other mailbox commands will serialize/queue on the mbox_mutex,
278 * which we currently hold. Furthermore this also guarantees that
279 * cxl_mbox_background_complete() checks are safe amongst each other,
280 * in that no new bg operation can occur in between.
282 * Background operations are timesliced in accordance with the nature
283 * of the command. In the event of timeout, the mailbox state is
284 * indeterminate until the next successful command submission and the
285 * driver can get back in sync with the hardware state.
287 if (mbox_cmd->return_code == CXL_MBOX_CMD_RC_BACKGROUND) {
292 * Sanitization is a special case which monopolizes the device
293 * and cannot be timesliced. Handle asynchronously instead,
294 * and allow userspace to poll(2) for completion.
296 if (mbox_cmd->opcode == CXL_MBOX_OP_SANITIZE) {
297 if (mds->security.poll_tmo_secs != -1) {
298 /* hold the device throughout */
299 get_device(cxlds->dev);
301 /* give first timeout a second */
303 mds->security.poll_tmo_secs = timeout;
304 queue_delayed_work(system_wq,
305 &mds->security.poll_dwork,
309 dev_dbg(dev, "Sanitization operation started\n");
313 dev_dbg(dev, "Mailbox background operation (0x%04x) started\n",
316 timeout = mbox_cmd->poll_interval_ms;
317 for (i = 0; i < mbox_cmd->poll_count; i++) {
318 if (rcuwait_wait_event_timeout(&mds->mbox_wait,
319 cxl_mbox_background_complete(cxlds),
320 TASK_UNINTERRUPTIBLE,
321 msecs_to_jiffies(timeout)) > 0)
325 if (!cxl_mbox_background_complete(cxlds)) {
326 dev_err(dev, "timeout waiting for background (%d ms)\n",
327 timeout * mbox_cmd->poll_count);
331 bg_status_reg = readq(cxlds->regs.mbox +
332 CXLDEV_MBOX_BG_CMD_STATUS_OFFSET);
333 mbox_cmd->return_code =
334 FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_RC_MASK,
337 "Mailbox background operation (0x%04x) completed\n",
341 if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS) {
342 dev_dbg(dev, "Mailbox operation had an error: %s\n",
343 cxl_mbox_cmd_rc2str(mbox_cmd));
344 return 0; /* completed but caller must check return_code */
349 cmd_reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_CMD_OFFSET);
350 out_len = FIELD_GET(CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK, cmd_reg);
353 if (out_len && mbox_cmd->payload_out) {
355 * Sanitize the copy. If hardware misbehaves, out_len per the
356 * spec can actually be greater than the max allowed size (21
357 * bits available but spec defined 1M max). The caller also may
358 * have requested less data than the hardware supplied even
363 n = min3(mbox_cmd->size_out, mds->payload_size, out_len);
364 memcpy_fromio(mbox_cmd->payload_out, payload, n);
365 mbox_cmd->size_out = n;
367 mbox_cmd->size_out = 0;
373 static int cxl_pci_mbox_send(struct cxl_memdev_state *mds,
374 struct cxl_mbox_cmd *cmd)
378 mutex_lock_io(&mds->mbox_mutex);
379 rc = __cxl_pci_mbox_send_cmd(mds, cmd);
380 mutex_unlock(&mds->mbox_mutex);
385 static int cxl_pci_setup_mailbox(struct cxl_memdev_state *mds)
387 struct cxl_dev_state *cxlds = &mds->cxlds;
388 const int cap = readl(cxlds->regs.mbox + CXLDEV_MBOX_CAPS_OFFSET);
389 struct device *dev = cxlds->dev;
390 unsigned long timeout;
393 timeout = jiffies + mbox_ready_timeout * HZ;
395 md_status = readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
396 if (md_status & CXLMDEV_MBOX_IF_READY)
398 if (msleep_interruptible(100))
400 } while (!time_after(jiffies, timeout));
402 if (!(md_status & CXLMDEV_MBOX_IF_READY)) {
403 cxl_err(dev, md_status, "timeout awaiting mailbox ready");
408 * A command may be in flight from a previous driver instance,
409 * think kexec, do one doorbell wait so that
410 * __cxl_pci_mbox_send_cmd() can assume that it is the only
411 * source for future doorbell busy events.
413 if (cxl_pci_mbox_wait_for_doorbell(cxlds) != 0) {
414 cxl_err(dev, md_status, "timeout awaiting mailbox idle");
418 mds->mbox_send = cxl_pci_mbox_send;
420 1 << FIELD_GET(CXLDEV_MBOX_CAP_PAYLOAD_SIZE_MASK, cap);
423 * CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register
425 * If the size is too small, mandatory commands will not work and so
426 * there's no point in going forward. If the size is too large, there's
427 * no harm is soft limiting it.
429 mds->payload_size = min_t(size_t, mds->payload_size, SZ_1M);
430 if (mds->payload_size < 256) {
431 dev_err(dev, "Mailbox is too small (%zub)",
436 dev_dbg(dev, "Mailbox payload sized %zu", mds->payload_size);
438 rcuwait_init(&mds->mbox_wait);
440 if (cap & CXLDEV_MBOX_CAP_BG_CMD_IRQ) {
443 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
445 msgnum = FIELD_GET(CXLDEV_MBOX_CAP_IRQ_MSGNUM_MASK, cap);
446 irq = pci_irq_vector(pdev, msgnum);
450 if (cxl_request_irq(cxlds, irq, cxl_pci_mbox_irq, NULL))
453 /* enable background command mbox irq support */
454 ctrl = readl(cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET);
455 ctrl |= CXLDEV_MBOX_CTRL_BG_CMD_IRQ;
456 writel(ctrl, cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET);
462 mds->security.poll = true;
463 INIT_DELAYED_WORK(&mds->security.poll_dwork, cxl_mbox_sanitize_work);
465 dev_dbg(cxlds->dev, "Mailbox interrupts are unsupported");
469 static int cxl_map_regblock(struct pci_dev *pdev, struct cxl_register_map *map)
471 struct device *dev = &pdev->dev;
473 map->base = ioremap(map->resource, map->max_size);
475 dev_err(dev, "failed to map registers\n");
479 dev_dbg(dev, "Mapped CXL Memory Device resource %pa\n", &map->resource);
483 static void cxl_unmap_regblock(struct pci_dev *pdev,
484 struct cxl_register_map *map)
490 static int cxl_probe_regs(struct pci_dev *pdev, struct cxl_register_map *map)
492 struct cxl_component_reg_map *comp_map;
493 struct cxl_device_reg_map *dev_map;
494 struct device *dev = &pdev->dev;
495 void __iomem *base = map->base;
497 switch (map->reg_type) {
498 case CXL_REGLOC_RBI_COMPONENT:
499 comp_map = &map->component_map;
500 cxl_probe_component_regs(dev, base, comp_map);
501 if (!comp_map->hdm_decoder.valid) {
502 dev_err(dev, "HDM decoder registers not found\n");
506 if (!comp_map->ras.valid)
507 dev_dbg(dev, "RAS registers not found\n");
509 dev_dbg(dev, "Set up component registers\n");
511 case CXL_REGLOC_RBI_MEMDEV:
512 dev_map = &map->device_map;
513 cxl_probe_device_regs(dev, base, dev_map);
514 if (!dev_map->status.valid || !dev_map->mbox.valid ||
515 !dev_map->memdev.valid) {
516 dev_err(dev, "registers not found: %s%s%s\n",
517 !dev_map->status.valid ? "status " : "",
518 !dev_map->mbox.valid ? "mbox " : "",
519 !dev_map->memdev.valid ? "memdev " : "");
523 dev_dbg(dev, "Probing device registers...\n");
532 static int cxl_setup_regs(struct pci_dev *pdev, enum cxl_regloc_type type,
533 struct cxl_register_map *map)
537 rc = cxl_find_regblock(pdev, type, map);
541 rc = cxl_map_regblock(pdev, map);
545 rc = cxl_probe_regs(pdev, map);
546 cxl_unmap_regblock(pdev, map);
552 * Assume that any RCIEP that emits the CXL memory expander class code
555 static bool is_cxl_restricted(struct pci_dev *pdev)
557 return pci_pcie_type(pdev) == PCI_EXP_TYPE_RC_END;
560 static int cxl_pci_ras_unmask(struct pci_dev *pdev)
562 struct pci_host_bridge *host_bridge = pci_find_host_bridge(pdev->bus);
563 struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
565 u32 orig_val, val, mask;
569 if (!cxlds->regs.ras) {
570 dev_dbg(&pdev->dev, "No RAS registers.\n");
574 /* BIOS has CXL error control */
575 if (!host_bridge->native_cxl_error)
578 rc = pcie_capability_read_word(pdev, PCI_EXP_DEVCTL, &cap);
582 if (cap & PCI_EXP_DEVCTL_URRE) {
583 addr = cxlds->regs.ras + CXL_RAS_UNCORRECTABLE_MASK_OFFSET;
584 orig_val = readl(addr);
586 mask = CXL_RAS_UNCORRECTABLE_MASK_MASK |
587 CXL_RAS_UNCORRECTABLE_MASK_F256B_MASK;
588 val = orig_val & ~mask;
591 "Uncorrectable RAS Errors Mask: %#x -> %#x\n",
595 if (cap & PCI_EXP_DEVCTL_CERE) {
596 addr = cxlds->regs.ras + CXL_RAS_CORRECTABLE_MASK_OFFSET;
597 orig_val = readl(addr);
598 val = orig_val & ~CXL_RAS_CORRECTABLE_MASK_MASK;
600 dev_dbg(&pdev->dev, "Correctable RAS Errors Mask: %#x -> %#x\n",
607 static void free_event_buf(void *buf)
613 * There is a single buffer for reading event logs from the mailbox. All logs
614 * share this buffer protected by the mds->event_log_lock.
616 static int cxl_mem_alloc_event_buf(struct cxl_memdev_state *mds)
618 struct cxl_get_event_payload *buf;
620 buf = kvmalloc(mds->payload_size, GFP_KERNEL);
623 mds->event.buf = buf;
625 return devm_add_action_or_reset(mds->cxlds.dev, free_event_buf, buf);
628 static int cxl_alloc_irq_vectors(struct pci_dev *pdev)
633 * Per CXL 3.0 3.1.1 CXL.io Endpoint a function on a CXL device must
634 * not generate INTx messages if that function participates in
635 * CXL.cache or CXL.mem.
637 * Additionally pci_alloc_irq_vectors() handles calling
638 * pci_free_irq_vectors() automatically despite not being called
639 * pcim_*. See pci_setup_msi_context().
641 nvecs = pci_alloc_irq_vectors(pdev, 1, CXL_PCI_DEFAULT_MAX_VECTORS,
642 PCI_IRQ_MSIX | PCI_IRQ_MSI);
644 dev_dbg(&pdev->dev, "Failed to alloc irq vectors: %d\n", nvecs);
650 static irqreturn_t cxl_event_thread(int irq, void *id)
652 struct cxl_dev_id *dev_id = id;
653 struct cxl_dev_state *cxlds = dev_id->cxlds;
654 struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds);
659 * CXL 3.0 8.2.8.3.1: The lower 32 bits are the status;
660 * ignore the reserved upper 32 bits
662 status = readl(cxlds->regs.status + CXLDEV_DEV_EVENT_STATUS_OFFSET);
663 /* Ignore logs unknown to the driver */
664 status &= CXLDEV_EVENT_STATUS_ALL;
667 cxl_mem_get_event_records(mds, status);
674 static int cxl_event_req_irq(struct cxl_dev_state *cxlds, u8 setting)
676 struct pci_dev *pdev = to_pci_dev(cxlds->dev);
679 if (FIELD_GET(CXLDEV_EVENT_INT_MODE_MASK, setting) != CXL_INT_MSI_MSIX)
682 irq = pci_irq_vector(pdev,
683 FIELD_GET(CXLDEV_EVENT_INT_MSGNUM_MASK, setting));
687 return cxl_request_irq(cxlds, irq, NULL, cxl_event_thread);
690 static int cxl_event_get_int_policy(struct cxl_memdev_state *mds,
691 struct cxl_event_interrupt_policy *policy)
693 struct cxl_mbox_cmd mbox_cmd = {
694 .opcode = CXL_MBOX_OP_GET_EVT_INT_POLICY,
695 .payload_out = policy,
696 .size_out = sizeof(*policy),
700 rc = cxl_internal_send_cmd(mds, &mbox_cmd);
702 dev_err(mds->cxlds.dev,
703 "Failed to get event interrupt policy : %d", rc);
708 static int cxl_event_config_msgnums(struct cxl_memdev_state *mds,
709 struct cxl_event_interrupt_policy *policy)
711 struct cxl_mbox_cmd mbox_cmd;
714 *policy = (struct cxl_event_interrupt_policy) {
715 .info_settings = CXL_INT_MSI_MSIX,
716 .warn_settings = CXL_INT_MSI_MSIX,
717 .failure_settings = CXL_INT_MSI_MSIX,
718 .fatal_settings = CXL_INT_MSI_MSIX,
721 mbox_cmd = (struct cxl_mbox_cmd) {
722 .opcode = CXL_MBOX_OP_SET_EVT_INT_POLICY,
723 .payload_in = policy,
724 .size_in = sizeof(*policy),
727 rc = cxl_internal_send_cmd(mds, &mbox_cmd);
729 dev_err(mds->cxlds.dev, "Failed to set event interrupt policy : %d",
734 /* Retrieve final interrupt settings */
735 return cxl_event_get_int_policy(mds, policy);
738 static int cxl_event_irqsetup(struct cxl_memdev_state *mds)
740 struct cxl_dev_state *cxlds = &mds->cxlds;
741 struct cxl_event_interrupt_policy policy;
744 rc = cxl_event_config_msgnums(mds, &policy);
748 rc = cxl_event_req_irq(cxlds, policy.info_settings);
750 dev_err(cxlds->dev, "Failed to get interrupt for event Info log\n");
754 rc = cxl_event_req_irq(cxlds, policy.warn_settings);
756 dev_err(cxlds->dev, "Failed to get interrupt for event Warn log\n");
760 rc = cxl_event_req_irq(cxlds, policy.failure_settings);
762 dev_err(cxlds->dev, "Failed to get interrupt for event Failure log\n");
766 rc = cxl_event_req_irq(cxlds, policy.fatal_settings);
768 dev_err(cxlds->dev, "Failed to get interrupt for event Fatal log\n");
775 static bool cxl_event_int_is_fw(u8 setting)
777 u8 mode = FIELD_GET(CXLDEV_EVENT_INT_MODE_MASK, setting);
779 return mode == CXL_INT_FW;
782 static int cxl_event_config(struct pci_host_bridge *host_bridge,
783 struct cxl_memdev_state *mds)
785 struct cxl_event_interrupt_policy policy;
789 * When BIOS maintains CXL error reporting control, it will process
790 * event records. Only one agent can do so.
792 if (!host_bridge->native_cxl_error)
795 rc = cxl_mem_alloc_event_buf(mds);
799 rc = cxl_event_get_int_policy(mds, &policy);
803 if (cxl_event_int_is_fw(policy.info_settings) ||
804 cxl_event_int_is_fw(policy.warn_settings) ||
805 cxl_event_int_is_fw(policy.failure_settings) ||
806 cxl_event_int_is_fw(policy.fatal_settings)) {
807 dev_err(mds->cxlds.dev,
808 "FW still in control of Event Logs despite _OSC settings\n");
812 rc = cxl_event_irqsetup(mds);
816 cxl_mem_get_event_records(mds, CXLDEV_EVENT_STATUS_ALL);
821 static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
823 struct pci_host_bridge *host_bridge = pci_find_host_bridge(pdev->bus);
824 struct cxl_memdev_state *mds;
825 struct cxl_dev_state *cxlds;
826 struct cxl_register_map map;
827 struct cxl_memdev *cxlmd;
831 * Double check the anonymous union trickery in struct cxl_regs
832 * FIXME switch to struct_group()
834 BUILD_BUG_ON(offsetof(struct cxl_regs, memdev) !=
835 offsetof(struct cxl_regs, device_regs.memdev));
837 rc = pcim_enable_device(pdev);
840 pci_set_master(pdev);
842 mds = cxl_memdev_state_create(&pdev->dev);
846 pci_set_drvdata(pdev, cxlds);
848 cxlds->rcd = is_cxl_restricted(pdev);
849 cxlds->serial = pci_get_dsn(pdev);
850 cxlds->cxl_dvsec = pci_find_dvsec_capability(
851 pdev, PCI_DVSEC_VENDOR_ID_CXL, CXL_DVSEC_PCIE_DEVICE);
852 if (!cxlds->cxl_dvsec)
854 "Device DVSEC not present, skip CXL.mem init\n");
856 rc = cxl_setup_regs(pdev, CXL_REGLOC_RBI_MEMDEV, &map);
860 rc = cxl_map_device_regs(&pdev->dev, &cxlds->regs.device_regs, &map);
865 * If the component registers can't be found, the cxl_pci driver may
866 * still be useful for management functions so don't return an error.
868 cxlds->component_reg_phys = CXL_RESOURCE_NONE;
869 rc = cxl_setup_regs(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
871 dev_warn(&pdev->dev, "No component registers (%d)\n", rc);
873 cxlds->component_reg_phys = map.resource;
875 rc = cxl_map_component_regs(&pdev->dev, &cxlds->regs.component,
876 &map, BIT(CXL_CM_CAP_CAP_ID_RAS));
878 dev_dbg(&pdev->dev, "Failed to map RAS capability.\n");
880 rc = cxl_await_media_ready(cxlds);
882 cxlds->media_ready = true;
884 dev_warn(&pdev->dev, "Media not active (%d)\n", rc);
886 rc = cxl_alloc_irq_vectors(pdev);
890 rc = cxl_pci_setup_mailbox(mds);
894 rc = cxl_enumerate_cmds(mds);
898 rc = cxl_set_timestamp(mds);
902 rc = cxl_poison_state_init(mds);
906 rc = cxl_dev_state_identify(mds);
910 rc = cxl_mem_create_range_info(mds);
914 cxlmd = devm_cxl_add_memdev(cxlds);
916 return PTR_ERR(cxlmd);
918 rc = cxl_memdev_setup_fw_upload(mds);
922 rc = cxl_event_config(host_bridge, mds);
926 rc = cxl_pci_ras_unmask(pdev);
928 dev_dbg(&pdev->dev, "No RAS reporting unmasked\n");
930 pci_save_state(pdev);
935 static const struct pci_device_id cxl_mem_pci_tbl[] = {
936 /* PCI class code for CXL.mem Type-3 Devices */
937 { PCI_DEVICE_CLASS((PCI_CLASS_MEMORY_CXL << 8 | CXL_MEMORY_PROGIF), ~0)},
938 { /* terminate list */ },
940 MODULE_DEVICE_TABLE(pci, cxl_mem_pci_tbl);
942 static pci_ers_result_t cxl_slot_reset(struct pci_dev *pdev)
944 struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
945 struct cxl_memdev *cxlmd = cxlds->cxlmd;
946 struct device *dev = &cxlmd->dev;
948 dev_info(&pdev->dev, "%s: restart CXL.mem after slot reset\n",
950 pci_restore_state(pdev);
951 if (device_attach(dev) <= 0)
952 return PCI_ERS_RESULT_DISCONNECT;
953 return PCI_ERS_RESULT_RECOVERED;
956 static void cxl_error_resume(struct pci_dev *pdev)
958 struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
959 struct cxl_memdev *cxlmd = cxlds->cxlmd;
960 struct device *dev = &cxlmd->dev;
962 dev_info(&pdev->dev, "%s: error resume %s\n", dev_name(dev),
963 dev->driver ? "successful" : "failed");
966 static const struct pci_error_handlers cxl_error_handlers = {
967 .error_detected = cxl_error_detected,
968 .slot_reset = cxl_slot_reset,
969 .resume = cxl_error_resume,
970 .cor_error_detected = cxl_cor_error_detected,
973 static struct pci_driver cxl_pci_driver = {
974 .name = KBUILD_MODNAME,
975 .id_table = cxl_mem_pci_tbl,
976 .probe = cxl_pci_probe,
977 .err_handler = &cxl_error_handlers,
979 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
983 MODULE_LICENSE("GPL v2");
984 module_pci_driver(cxl_pci_driver);
985 MODULE_IMPORT_NS(CXL);