121e9f767SBen Widawsky // SPDX-License-Identifier: GPL-2.0-only 221e9f767SBen Widawsky /* Copyright(c) 2020 Intel Corporation. All rights reserved. */ 34faf31b4SDan Williams #include <linux/io-64-nonatomic-lo-hi.h> 4229e8828SBen Widawsky #include <linux/moduleparam.h> 521e9f767SBen Widawsky #include <linux/module.h> 6229e8828SBen Widawsky #include <linux/delay.h> 721e9f767SBen Widawsky #include <linux/sizes.h> 821e9f767SBen Widawsky #include <linux/mutex.h> 930af9729SIra Weiny #include <linux/list.h> 1021e9f767SBen Widawsky #include <linux/pci.h> 112905cb52SDan Williams #include <linux/aer.h> 1221e9f767SBen Widawsky #include <linux/io.h> 135161a55cSBen Widawsky #include "cxlmem.h" 14af9cae9fSDan Williams #include "cxlpci.h" 1521e9f767SBen Widawsky #include "cxl.h" 1621e9f767SBen Widawsky 1721e9f767SBen Widawsky /** 1821e9f767SBen Widawsky * DOC: cxl pci 1921e9f767SBen Widawsky * 2021e9f767SBen Widawsky * This implements the PCI exclusive functionality for a CXL device as it is 2121e9f767SBen Widawsky * defined by the Compute Express Link specification. CXL devices may surface 22ed97afb5SBen Widawsky * certain functionality even if it isn't CXL enabled. While this driver is 23ed97afb5SBen Widawsky * focused around the PCI specific aspects of a CXL device, it binds to the 24ed97afb5SBen Widawsky * specific CXL memory device class code, and therefore the implementation of 25ed97afb5SBen Widawsky * cxl_pci is focused around CXL memory devices. 2621e9f767SBen Widawsky * 2721e9f767SBen Widawsky * The driver has several responsibilities, mainly: 2821e9f767SBen Widawsky * - Create the memX device and register on the CXL bus. 2921e9f767SBen Widawsky * - Enumerate device's register interface and map them. 30ed97afb5SBen Widawsky * - Registers nvdimm bridge device with cxl_core. 31ed97afb5SBen Widawsky * - Registers a CXL mailbox with cxl_core. 3221e9f767SBen Widawsky */ 3321e9f767SBen Widawsky 345e2411aeSIra Weiny #define cxl_doorbell_busy(cxlds) \ 355e2411aeSIra Weiny (readl((cxlds)->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET) & \ 3621e9f767SBen Widawsky CXLDEV_MBOX_CTRL_DOORBELL) 3721e9f767SBen Widawsky 3821e9f767SBen Widawsky /* CXL 2.0 - 8.2.8.4 */ 3921e9f767SBen Widawsky #define CXL_MAILBOX_TIMEOUT_MS (2 * HZ) 4021e9f767SBen Widawsky 41229e8828SBen Widawsky /* 42229e8828SBen Widawsky * CXL 2.0 ECN "Add Mailbox Ready Time" defines a capability field to 43229e8828SBen Widawsky * dictate how long to wait for the mailbox to become ready. The new 44229e8828SBen Widawsky * field allows the device to tell software the amount of time to wait 45229e8828SBen Widawsky * before mailbox ready. This field per the spec theoretically allows 46229e8828SBen Widawsky * for up to 255 seconds. 255 seconds is unreasonably long, its longer 47229e8828SBen Widawsky * than the maximum SATA port link recovery wait. Default to 60 seconds 48229e8828SBen Widawsky * until someone builds a CXL device that needs more time in practice. 49229e8828SBen Widawsky */ 50229e8828SBen Widawsky static unsigned short mbox_ready_timeout = 60; 51229e8828SBen Widawsky module_param(mbox_ready_timeout, ushort, 0644); 522e4ba0ecSDan Williams MODULE_PARM_DESC(mbox_ready_timeout, "seconds to wait for mailbox ready"); 53229e8828SBen Widawsky 545e2411aeSIra Weiny static int cxl_pci_mbox_wait_for_doorbell(struct cxl_dev_state *cxlds) 5521e9f767SBen Widawsky { 5621e9f767SBen Widawsky const unsigned long start = jiffies; 5721e9f767SBen Widawsky unsigned long end = start; 5821e9f767SBen Widawsky 595e2411aeSIra Weiny while (cxl_doorbell_busy(cxlds)) { 6021e9f767SBen Widawsky end = jiffies; 6121e9f767SBen Widawsky 6221e9f767SBen Widawsky if (time_after(end, start + CXL_MAILBOX_TIMEOUT_MS)) { 6321e9f767SBen Widawsky /* Check again in case preempted before timeout test */ 645e2411aeSIra Weiny if (!cxl_doorbell_busy(cxlds)) 6521e9f767SBen Widawsky break; 6621e9f767SBen Widawsky return -ETIMEDOUT; 6721e9f767SBen Widawsky } 6821e9f767SBen Widawsky cpu_relax(); 6921e9f767SBen Widawsky } 7021e9f767SBen Widawsky 715e2411aeSIra Weiny dev_dbg(cxlds->dev, "Doorbell wait took %dms", 7221e9f767SBen Widawsky jiffies_to_msecs(end) - jiffies_to_msecs(start)); 7321e9f767SBen Widawsky return 0; 7421e9f767SBen Widawsky } 7521e9f767SBen Widawsky 764f195ee7SDan Williams #define cxl_err(dev, status, msg) \ 774f195ee7SDan Williams dev_err_ratelimited(dev, msg ", device state %s%s\n", \ 784f195ee7SDan Williams status & CXLMDEV_DEV_FATAL ? " fatal" : "", \ 794f195ee7SDan Williams status & CXLMDEV_FW_HALT ? " firmware-halt" : "") 8021e9f767SBen Widawsky 814f195ee7SDan Williams #define cxl_cmd_err(dev, cmd, status, msg) \ 824f195ee7SDan Williams dev_err_ratelimited(dev, msg " (opcode: %#x), device state %s%s\n", \ 834f195ee7SDan Williams (cmd)->opcode, \ 844f195ee7SDan Williams status & CXLMDEV_DEV_FATAL ? " fatal" : "", \ 854f195ee7SDan Williams status & CXLMDEV_FW_HALT ? " firmware-halt" : "") 8621e9f767SBen Widawsky 879f7a320dSDavidlohr Bueso struct cxl_dev_id { 889f7a320dSDavidlohr Bueso struct cxl_dev_state *cxlds; 899f7a320dSDavidlohr Bueso }; 909f7a320dSDavidlohr Bueso 919f7a320dSDavidlohr Bueso static int cxl_request_irq(struct cxl_dev_state *cxlds, int irq, 929f7a320dSDavidlohr Bueso irq_handler_t handler, irq_handler_t thread_fn) 939f7a320dSDavidlohr Bueso { 949f7a320dSDavidlohr Bueso struct device *dev = cxlds->dev; 959f7a320dSDavidlohr Bueso struct cxl_dev_id *dev_id; 969f7a320dSDavidlohr Bueso 979f7a320dSDavidlohr Bueso /* dev_id must be globally unique and must contain the cxlds */ 989f7a320dSDavidlohr Bueso dev_id = devm_kzalloc(dev, sizeof(*dev_id), GFP_KERNEL); 999f7a320dSDavidlohr Bueso if (!dev_id) 1009f7a320dSDavidlohr Bueso return -ENOMEM; 1019f7a320dSDavidlohr Bueso dev_id->cxlds = cxlds; 1029f7a320dSDavidlohr Bueso 1039f7a320dSDavidlohr Bueso return devm_request_threaded_irq(dev, irq, handler, thread_fn, 1049f7a320dSDavidlohr Bueso IRQF_SHARED | IRQF_ONESHOT, 1059f7a320dSDavidlohr Bueso NULL, dev_id); 1069f7a320dSDavidlohr Bueso } 1079f7a320dSDavidlohr Bueso 108ccadf131SDavidlohr Bueso static bool cxl_mbox_background_complete(struct cxl_dev_state *cxlds) 109ccadf131SDavidlohr Bueso { 110ccadf131SDavidlohr Bueso u64 reg; 111ccadf131SDavidlohr Bueso 112ccadf131SDavidlohr Bueso reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_BG_CMD_STATUS_OFFSET); 113ccadf131SDavidlohr Bueso return FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_PCT_MASK, reg) == 100; 114ccadf131SDavidlohr Bueso } 115ccadf131SDavidlohr Bueso 116ccadf131SDavidlohr Bueso static irqreturn_t cxl_pci_mbox_irq(int irq, void *id) 117ccadf131SDavidlohr Bueso { 118*0c36b6adSDavidlohr Bueso u64 reg; 119*0c36b6adSDavidlohr Bueso u16 opcode; 120ccadf131SDavidlohr Bueso struct cxl_dev_id *dev_id = id; 121ccadf131SDavidlohr Bueso struct cxl_dev_state *cxlds = dev_id->cxlds; 122ccadf131SDavidlohr Bueso 1238ea9c33dSDavidlohr Bueso if (!cxl_mbox_background_complete(cxlds)) 1248ea9c33dSDavidlohr Bueso return IRQ_NONE; 1258ea9c33dSDavidlohr Bueso 126*0c36b6adSDavidlohr Bueso reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_BG_CMD_STATUS_OFFSET); 127*0c36b6adSDavidlohr Bueso opcode = FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_OPCODE_MASK, reg); 128*0c36b6adSDavidlohr Bueso if (opcode == CXL_MBOX_OP_SANITIZE) { 129*0c36b6adSDavidlohr Bueso dev_dbg(cxlds->dev, "Sanitization operation ended\n"); 130*0c36b6adSDavidlohr Bueso } else { 131ccadf131SDavidlohr Bueso /* short-circuit the wait in __cxl_pci_mbox_send_cmd() */ 132ccadf131SDavidlohr Bueso rcuwait_wake_up(&cxlds->mbox_wait); 133*0c36b6adSDavidlohr Bueso } 134ccadf131SDavidlohr Bueso 135ccadf131SDavidlohr Bueso return IRQ_HANDLED; 136ccadf131SDavidlohr Bueso } 137ccadf131SDavidlohr Bueso 138*0c36b6adSDavidlohr Bueso /* 139*0c36b6adSDavidlohr Bueso * Sanitization operation polling mode. 140*0c36b6adSDavidlohr Bueso */ 141*0c36b6adSDavidlohr Bueso static void cxl_mbox_sanitize_work(struct work_struct *work) 142*0c36b6adSDavidlohr Bueso { 143*0c36b6adSDavidlohr Bueso struct cxl_dev_state *cxlds; 144*0c36b6adSDavidlohr Bueso 145*0c36b6adSDavidlohr Bueso cxlds = container_of(work, 146*0c36b6adSDavidlohr Bueso struct cxl_dev_state, security.poll_dwork.work); 147*0c36b6adSDavidlohr Bueso 148*0c36b6adSDavidlohr Bueso mutex_lock(&cxlds->mbox_mutex); 149*0c36b6adSDavidlohr Bueso if (cxl_mbox_background_complete(cxlds)) { 150*0c36b6adSDavidlohr Bueso cxlds->security.poll_tmo_secs = 0; 151*0c36b6adSDavidlohr Bueso put_device(cxlds->dev); 152*0c36b6adSDavidlohr Bueso 153*0c36b6adSDavidlohr Bueso dev_dbg(cxlds->dev, "Sanitization operation ended\n"); 154*0c36b6adSDavidlohr Bueso } else { 155*0c36b6adSDavidlohr Bueso int timeout = cxlds->security.poll_tmo_secs + 10; 156*0c36b6adSDavidlohr Bueso 157*0c36b6adSDavidlohr Bueso cxlds->security.poll_tmo_secs = min(15 * 60, timeout); 158*0c36b6adSDavidlohr Bueso queue_delayed_work(system_wq, &cxlds->security.poll_dwork, 159*0c36b6adSDavidlohr Bueso timeout * HZ); 160*0c36b6adSDavidlohr Bueso } 161*0c36b6adSDavidlohr Bueso mutex_unlock(&cxlds->mbox_mutex); 162*0c36b6adSDavidlohr Bueso } 163*0c36b6adSDavidlohr Bueso 16421e9f767SBen Widawsky /** 165ed97afb5SBen Widawsky * __cxl_pci_mbox_send_cmd() - Execute a mailbox command 1665e2411aeSIra Weiny * @cxlds: The device state to communicate with. 16721e9f767SBen Widawsky * @mbox_cmd: Command to send to the memory device. 16821e9f767SBen Widawsky * 16921e9f767SBen Widawsky * Context: Any context. Expects mbox_mutex to be held. 17021e9f767SBen Widawsky * Return: -ETIMEDOUT if timeout occurred waiting for completion. 0 on success. 17121e9f767SBen Widawsky * Caller should check the return code in @mbox_cmd to make sure it 17221e9f767SBen Widawsky * succeeded. 17321e9f767SBen Widawsky * 17421e9f767SBen Widawsky * This is a generic form of the CXL mailbox send command thus only using the 17521e9f767SBen Widawsky * registers defined by the mailbox capability ID - CXL 2.0 8.2.8.4. Memory 17621e9f767SBen Widawsky * devices, and perhaps other types of CXL devices may have further information 17721e9f767SBen Widawsky * available upon error conditions. Driver facilities wishing to send mailbox 17821e9f767SBen Widawsky * commands should use the wrapper command. 17921e9f767SBen Widawsky * 18021e9f767SBen Widawsky * The CXL spec allows for up to two mailboxes. The intention is for the primary 18121e9f767SBen Widawsky * mailbox to be OS controlled and the secondary mailbox to be used by system 18221e9f767SBen Widawsky * firmware. This allows the OS and firmware to communicate with the device and 18321e9f767SBen Widawsky * not need to coordinate with each other. The driver only uses the primary 18421e9f767SBen Widawsky * mailbox. 18521e9f767SBen Widawsky */ 1865e2411aeSIra Weiny static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds, 187b64955a9SDan Williams struct cxl_mbox_cmd *mbox_cmd) 18821e9f767SBen Widawsky { 1895e2411aeSIra Weiny void __iomem *payload = cxlds->regs.mbox + CXLDEV_MBOX_PAYLOAD_OFFSET; 1905e2411aeSIra Weiny struct device *dev = cxlds->dev; 19121e9f767SBen Widawsky u64 cmd_reg, status_reg; 19221e9f767SBen Widawsky size_t out_len; 19321e9f767SBen Widawsky int rc; 19421e9f767SBen Widawsky 1955e2411aeSIra Weiny lockdep_assert_held(&cxlds->mbox_mutex); 19621e9f767SBen Widawsky 19721e9f767SBen Widawsky /* 19821e9f767SBen Widawsky * Here are the steps from 8.2.8.4 of the CXL 2.0 spec. 19921e9f767SBen Widawsky * 1. Caller reads MB Control Register to verify doorbell is clear 20021e9f767SBen Widawsky * 2. Caller writes Command Register 20121e9f767SBen Widawsky * 3. Caller writes Command Payload Registers if input payload is non-empty 20221e9f767SBen Widawsky * 4. Caller writes MB Control Register to set doorbell 20321e9f767SBen Widawsky * 5. Caller either polls for doorbell to be clear or waits for interrupt if configured 20421e9f767SBen Widawsky * 6. Caller reads MB Status Register to fetch Return code 20521e9f767SBen Widawsky * 7. If command successful, Caller reads Command Register to get Payload Length 20621e9f767SBen Widawsky * 8. If output payload is non-empty, host reads Command Payload Registers 20721e9f767SBen Widawsky * 20821e9f767SBen Widawsky * Hardware is free to do whatever it wants before the doorbell is rung, 20921e9f767SBen Widawsky * and isn't allowed to change anything after it clears the doorbell. As 21021e9f767SBen Widawsky * such, steps 2 and 3 can happen in any order, and steps 6, 7, 8 can 21121e9f767SBen Widawsky * also happen in any order (though some orders might not make sense). 21221e9f767SBen Widawsky */ 21321e9f767SBen Widawsky 21421e9f767SBen Widawsky /* #1 */ 2155e2411aeSIra Weiny if (cxl_doorbell_busy(cxlds)) { 2164f195ee7SDan Williams u64 md_status = 2174f195ee7SDan Williams readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET); 2184f195ee7SDan Williams 2194f195ee7SDan Williams cxl_cmd_err(cxlds->dev, mbox_cmd, md_status, 2204f195ee7SDan Williams "mailbox queue busy"); 22121e9f767SBen Widawsky return -EBUSY; 22221e9f767SBen Widawsky } 22321e9f767SBen Widawsky 224*0c36b6adSDavidlohr Bueso /* 225*0c36b6adSDavidlohr Bueso * With sanitize polling, hardware might be done and the poller still 226*0c36b6adSDavidlohr Bueso * not be in sync. Ensure no new command comes in until so. Keep the 227*0c36b6adSDavidlohr Bueso * hardware semantics and only allow device health status. 228*0c36b6adSDavidlohr Bueso */ 229*0c36b6adSDavidlohr Bueso if (cxlds->security.poll_tmo_secs > 0) { 230*0c36b6adSDavidlohr Bueso if (mbox_cmd->opcode != CXL_MBOX_OP_GET_HEALTH_INFO) 231*0c36b6adSDavidlohr Bueso return -EBUSY; 232*0c36b6adSDavidlohr Bueso } 233*0c36b6adSDavidlohr Bueso 23421e9f767SBen Widawsky cmd_reg = FIELD_PREP(CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK, 23521e9f767SBen Widawsky mbox_cmd->opcode); 23621e9f767SBen Widawsky if (mbox_cmd->size_in) { 23721e9f767SBen Widawsky if (WARN_ON(!mbox_cmd->payload_in)) 23821e9f767SBen Widawsky return -EINVAL; 23921e9f767SBen Widawsky 24021e9f767SBen Widawsky cmd_reg |= FIELD_PREP(CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK, 24121e9f767SBen Widawsky mbox_cmd->size_in); 24221e9f767SBen Widawsky memcpy_toio(payload, mbox_cmd->payload_in, mbox_cmd->size_in); 24321e9f767SBen Widawsky } 24421e9f767SBen Widawsky 24521e9f767SBen Widawsky /* #2, #3 */ 2465e2411aeSIra Weiny writeq(cmd_reg, cxlds->regs.mbox + CXLDEV_MBOX_CMD_OFFSET); 24721e9f767SBen Widawsky 24821e9f767SBen Widawsky /* #4 */ 249852db33cSRobert Richter dev_dbg(dev, "Sending command: 0x%04x\n", mbox_cmd->opcode); 25021e9f767SBen Widawsky writel(CXLDEV_MBOX_CTRL_DOORBELL, 2515e2411aeSIra Weiny cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET); 25221e9f767SBen Widawsky 25321e9f767SBen Widawsky /* #5 */ 2545e2411aeSIra Weiny rc = cxl_pci_mbox_wait_for_doorbell(cxlds); 25521e9f767SBen Widawsky if (rc == -ETIMEDOUT) { 2564f195ee7SDan Williams u64 md_status = readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET); 2574f195ee7SDan Williams 2584f195ee7SDan Williams cxl_cmd_err(cxlds->dev, mbox_cmd, md_status, "mailbox timeout"); 25921e9f767SBen Widawsky return rc; 26021e9f767SBen Widawsky } 26121e9f767SBen Widawsky 26221e9f767SBen Widawsky /* #6 */ 2635e2411aeSIra Weiny status_reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_STATUS_OFFSET); 26421e9f767SBen Widawsky mbox_cmd->return_code = 26521e9f767SBen Widawsky FIELD_GET(CXLDEV_MBOX_STATUS_RET_CODE_MASK, status_reg); 26621e9f767SBen Widawsky 267ccadf131SDavidlohr Bueso /* 268ccadf131SDavidlohr Bueso * Handle the background command in a synchronous manner. 269ccadf131SDavidlohr Bueso * 270ccadf131SDavidlohr Bueso * All other mailbox commands will serialize/queue on the mbox_mutex, 271ccadf131SDavidlohr Bueso * which we currently hold. Furthermore this also guarantees that 272ccadf131SDavidlohr Bueso * cxl_mbox_background_complete() checks are safe amongst each other, 273ccadf131SDavidlohr Bueso * in that no new bg operation can occur in between. 274ccadf131SDavidlohr Bueso * 275ccadf131SDavidlohr Bueso * Background operations are timesliced in accordance with the nature 276ccadf131SDavidlohr Bueso * of the command. In the event of timeout, the mailbox state is 277ccadf131SDavidlohr Bueso * indeterminate until the next successful command submission and the 278ccadf131SDavidlohr Bueso * driver can get back in sync with the hardware state. 279ccadf131SDavidlohr Bueso */ 280ccadf131SDavidlohr Bueso if (mbox_cmd->return_code == CXL_MBOX_CMD_RC_BACKGROUND) { 281ccadf131SDavidlohr Bueso u64 bg_status_reg; 282*0c36b6adSDavidlohr Bueso int i, timeout; 283*0c36b6adSDavidlohr Bueso 284*0c36b6adSDavidlohr Bueso /* 285*0c36b6adSDavidlohr Bueso * Sanitization is a special case which monopolizes the device 286*0c36b6adSDavidlohr Bueso * and cannot be timesliced. Handle asynchronously instead, 287*0c36b6adSDavidlohr Bueso * and allow userspace to poll(2) for completion. 288*0c36b6adSDavidlohr Bueso */ 289*0c36b6adSDavidlohr Bueso if (mbox_cmd->opcode == CXL_MBOX_OP_SANITIZE) { 290*0c36b6adSDavidlohr Bueso if (cxlds->security.poll_tmo_secs != -1) { 291*0c36b6adSDavidlohr Bueso /* hold the device throughout */ 292*0c36b6adSDavidlohr Bueso get_device(cxlds->dev); 293*0c36b6adSDavidlohr Bueso 294*0c36b6adSDavidlohr Bueso /* give first timeout a second */ 295*0c36b6adSDavidlohr Bueso timeout = 1; 296*0c36b6adSDavidlohr Bueso cxlds->security.poll_tmo_secs = timeout; 297*0c36b6adSDavidlohr Bueso queue_delayed_work(system_wq, 298*0c36b6adSDavidlohr Bueso &cxlds->security.poll_dwork, 299*0c36b6adSDavidlohr Bueso timeout * HZ); 300*0c36b6adSDavidlohr Bueso } 301*0c36b6adSDavidlohr Bueso 302*0c36b6adSDavidlohr Bueso dev_dbg(dev, "Sanitization operation started\n"); 303*0c36b6adSDavidlohr Bueso goto success; 304*0c36b6adSDavidlohr Bueso } 305ccadf131SDavidlohr Bueso 306ccadf131SDavidlohr Bueso dev_dbg(dev, "Mailbox background operation (0x%04x) started\n", 307ccadf131SDavidlohr Bueso mbox_cmd->opcode); 308ccadf131SDavidlohr Bueso 309*0c36b6adSDavidlohr Bueso timeout = mbox_cmd->poll_interval_ms; 310ccadf131SDavidlohr Bueso for (i = 0; i < mbox_cmd->poll_count; i++) { 311ccadf131SDavidlohr Bueso if (rcuwait_wait_event_timeout(&cxlds->mbox_wait, 312ccadf131SDavidlohr Bueso cxl_mbox_background_complete(cxlds), 313ccadf131SDavidlohr Bueso TASK_UNINTERRUPTIBLE, 314ccadf131SDavidlohr Bueso msecs_to_jiffies(timeout)) > 0) 315ccadf131SDavidlohr Bueso break; 316ccadf131SDavidlohr Bueso } 317ccadf131SDavidlohr Bueso 318ccadf131SDavidlohr Bueso if (!cxl_mbox_background_complete(cxlds)) { 319ccadf131SDavidlohr Bueso dev_err(dev, "timeout waiting for background (%d ms)\n", 320ccadf131SDavidlohr Bueso timeout * mbox_cmd->poll_count); 321ccadf131SDavidlohr Bueso return -ETIMEDOUT; 322ccadf131SDavidlohr Bueso } 323ccadf131SDavidlohr Bueso 324ccadf131SDavidlohr Bueso bg_status_reg = readq(cxlds->regs.mbox + 325ccadf131SDavidlohr Bueso CXLDEV_MBOX_BG_CMD_STATUS_OFFSET); 326ccadf131SDavidlohr Bueso mbox_cmd->return_code = 327ccadf131SDavidlohr Bueso FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_RC_MASK, 328ccadf131SDavidlohr Bueso bg_status_reg); 329ccadf131SDavidlohr Bueso dev_dbg(dev, 330ccadf131SDavidlohr Bueso "Mailbox background operation (0x%04x) completed\n", 331ccadf131SDavidlohr Bueso mbox_cmd->opcode); 332ccadf131SDavidlohr Bueso } 333ccadf131SDavidlohr Bueso 33492fcc1abSDavidlohr Bueso if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS) { 335c43e036dSDavidlohr Bueso dev_dbg(dev, "Mailbox operation had an error: %s\n", 336c43e036dSDavidlohr Bueso cxl_mbox_cmd_rc2str(mbox_cmd)); 337cbe83a20SDavidlohr Bueso return 0; /* completed but caller must check return_code */ 33821e9f767SBen Widawsky } 33921e9f767SBen Widawsky 340*0c36b6adSDavidlohr Bueso success: 34121e9f767SBen Widawsky /* #7 */ 3425e2411aeSIra Weiny cmd_reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_CMD_OFFSET); 34321e9f767SBen Widawsky out_len = FIELD_GET(CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK, cmd_reg); 34421e9f767SBen Widawsky 34521e9f767SBen Widawsky /* #8 */ 34621e9f767SBen Widawsky if (out_len && mbox_cmd->payload_out) { 34721e9f767SBen Widawsky /* 34821e9f767SBen Widawsky * Sanitize the copy. If hardware misbehaves, out_len per the 34921e9f767SBen Widawsky * spec can actually be greater than the max allowed size (21 35021e9f767SBen Widawsky * bits available but spec defined 1M max). The caller also may 35121e9f767SBen Widawsky * have requested less data than the hardware supplied even 35221e9f767SBen Widawsky * within spec. 35321e9f767SBen Widawsky */ 3545e2411aeSIra Weiny size_t n = min3(mbox_cmd->size_out, cxlds->payload_size, out_len); 35521e9f767SBen Widawsky 35621e9f767SBen Widawsky memcpy_fromio(mbox_cmd->payload_out, payload, n); 35721e9f767SBen Widawsky mbox_cmd->size_out = n; 35821e9f767SBen Widawsky } else { 35921e9f767SBen Widawsky mbox_cmd->size_out = 0; 36021e9f767SBen Widawsky } 36121e9f767SBen Widawsky 36221e9f767SBen Widawsky return 0; 36321e9f767SBen Widawsky } 36421e9f767SBen Widawsky 3655e2411aeSIra Weiny static int cxl_pci_mbox_send(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd) 366b64955a9SDan Williams { 367b64955a9SDan Williams int rc; 368b64955a9SDan Williams 3694f195ee7SDan Williams mutex_lock_io(&cxlds->mbox_mutex); 3705e2411aeSIra Weiny rc = __cxl_pci_mbox_send_cmd(cxlds, cmd); 3714f195ee7SDan Williams mutex_unlock(&cxlds->mbox_mutex); 372b64955a9SDan Williams 373b64955a9SDan Williams return rc; 374b64955a9SDan Williams } 375b64955a9SDan Williams 3765e2411aeSIra Weiny static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds) 37721e9f767SBen Widawsky { 3785e2411aeSIra Weiny const int cap = readl(cxlds->regs.mbox + CXLDEV_MBOX_CAPS_OFFSET); 379229e8828SBen Widawsky unsigned long timeout; 380229e8828SBen Widawsky u64 md_status; 381229e8828SBen Widawsky 382229e8828SBen Widawsky timeout = jiffies + mbox_ready_timeout * HZ; 383229e8828SBen Widawsky do { 384229e8828SBen Widawsky md_status = readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET); 385229e8828SBen Widawsky if (md_status & CXLMDEV_MBOX_IF_READY) 386229e8828SBen Widawsky break; 387229e8828SBen Widawsky if (msleep_interruptible(100)) 388229e8828SBen Widawsky break; 389229e8828SBen Widawsky } while (!time_after(jiffies, timeout)); 390229e8828SBen Widawsky 391229e8828SBen Widawsky if (!(md_status & CXLMDEV_MBOX_IF_READY)) { 3924f195ee7SDan Williams cxl_err(cxlds->dev, md_status, 3934f195ee7SDan Williams "timeout awaiting mailbox ready"); 3944f195ee7SDan Williams return -ETIMEDOUT; 3954f195ee7SDan Williams } 3964f195ee7SDan Williams 3974f195ee7SDan Williams /* 3984f195ee7SDan Williams * A command may be in flight from a previous driver instance, 3994f195ee7SDan Williams * think kexec, do one doorbell wait so that 4004f195ee7SDan Williams * __cxl_pci_mbox_send_cmd() can assume that it is the only 4014f195ee7SDan Williams * source for future doorbell busy events. 4024f195ee7SDan Williams */ 4034f195ee7SDan Williams if (cxl_pci_mbox_wait_for_doorbell(cxlds) != 0) { 4044f195ee7SDan Williams cxl_err(cxlds->dev, md_status, "timeout awaiting mailbox idle"); 4054f195ee7SDan Williams return -ETIMEDOUT; 406229e8828SBen Widawsky } 40721e9f767SBen Widawsky 4085e2411aeSIra Weiny cxlds->mbox_send = cxl_pci_mbox_send; 4095e2411aeSIra Weiny cxlds->payload_size = 41021e9f767SBen Widawsky 1 << FIELD_GET(CXLDEV_MBOX_CAP_PAYLOAD_SIZE_MASK, cap); 41121e9f767SBen Widawsky 41221e9f767SBen Widawsky /* 41321e9f767SBen Widawsky * CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register 41421e9f767SBen Widawsky * 41521e9f767SBen Widawsky * If the size is too small, mandatory commands will not work and so 41621e9f767SBen Widawsky * there's no point in going forward. If the size is too large, there's 41721e9f767SBen Widawsky * no harm is soft limiting it. 41821e9f767SBen Widawsky */ 4195e2411aeSIra Weiny cxlds->payload_size = min_t(size_t, cxlds->payload_size, SZ_1M); 4205e2411aeSIra Weiny if (cxlds->payload_size < 256) { 4215e2411aeSIra Weiny dev_err(cxlds->dev, "Mailbox is too small (%zub)", 4225e2411aeSIra Weiny cxlds->payload_size); 42321e9f767SBen Widawsky return -ENXIO; 42421e9f767SBen Widawsky } 42521e9f767SBen Widawsky 4265e2411aeSIra Weiny dev_dbg(cxlds->dev, "Mailbox payload sized %zu", 4275e2411aeSIra Weiny cxlds->payload_size); 42821e9f767SBen Widawsky 429ccadf131SDavidlohr Bueso rcuwait_init(&cxlds->mbox_wait); 430ccadf131SDavidlohr Bueso 431ccadf131SDavidlohr Bueso if (cap & CXLDEV_MBOX_CAP_BG_CMD_IRQ) { 432ccadf131SDavidlohr Bueso u32 ctrl; 433ccadf131SDavidlohr Bueso int irq, msgnum; 434ccadf131SDavidlohr Bueso struct pci_dev *pdev = to_pci_dev(cxlds->dev); 435ccadf131SDavidlohr Bueso 436ccadf131SDavidlohr Bueso msgnum = FIELD_GET(CXLDEV_MBOX_CAP_IRQ_MSGNUM_MASK, cap); 437ccadf131SDavidlohr Bueso irq = pci_irq_vector(pdev, msgnum); 438ccadf131SDavidlohr Bueso if (irq < 0) 439ccadf131SDavidlohr Bueso goto mbox_poll; 440ccadf131SDavidlohr Bueso 441ccadf131SDavidlohr Bueso if (cxl_request_irq(cxlds, irq, cxl_pci_mbox_irq, NULL)) 442ccadf131SDavidlohr Bueso goto mbox_poll; 443ccadf131SDavidlohr Bueso 444ccadf131SDavidlohr Bueso /* enable background command mbox irq support */ 445ccadf131SDavidlohr Bueso ctrl = readl(cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET); 446ccadf131SDavidlohr Bueso ctrl |= CXLDEV_MBOX_CTRL_BG_CMD_IRQ; 447ccadf131SDavidlohr Bueso writel(ctrl, cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET); 448ccadf131SDavidlohr Bueso 449ccadf131SDavidlohr Bueso return 0; 450ccadf131SDavidlohr Bueso } 451ccadf131SDavidlohr Bueso 452ccadf131SDavidlohr Bueso mbox_poll: 453*0c36b6adSDavidlohr Bueso cxlds->security.poll = true; 454*0c36b6adSDavidlohr Bueso INIT_DELAYED_WORK(&cxlds->security.poll_dwork, cxl_mbox_sanitize_work); 455*0c36b6adSDavidlohr Bueso 456ccadf131SDavidlohr Bueso dev_dbg(cxlds->dev, "Mailbox interrupts are unsupported"); 45721e9f767SBen Widawsky return 0; 45821e9f767SBen Widawsky } 45921e9f767SBen Widawsky 460a261e9a1SDan Williams static int cxl_map_regblock(struct pci_dev *pdev, struct cxl_register_map *map) 4611b0a1a2aSBen Widawsky { 4627dc7a64dSBen Widawsky struct device *dev = &pdev->dev; 4631b0a1a2aSBen Widawsky 4646c7f4f1eSDan Williams map->base = ioremap(map->resource, map->max_size); 4656c7f4f1eSDan Williams if (!map->base) { 46621e9f767SBen Widawsky dev_err(dev, "failed to map registers\n"); 467a261e9a1SDan Williams return -ENOMEM; 46821e9f767SBen Widawsky } 46921e9f767SBen Widawsky 4706c7f4f1eSDan Williams dev_dbg(dev, "Mapped CXL Memory Device resource %pa\n", &map->resource); 471a261e9a1SDan Williams return 0; 47230af9729SIra Weiny } 47330af9729SIra Weiny 474a261e9a1SDan Williams static void cxl_unmap_regblock(struct pci_dev *pdev, 475a261e9a1SDan Williams struct cxl_register_map *map) 47630af9729SIra Weiny { 4776c7f4f1eSDan Williams iounmap(map->base); 478a261e9a1SDan Williams map->base = NULL; 47921e9f767SBen Widawsky } 48021e9f767SBen Widawsky 481a261e9a1SDan Williams static int cxl_probe_regs(struct pci_dev *pdev, struct cxl_register_map *map) 48230af9729SIra Weiny { 48308422378SBen Widawsky struct cxl_component_reg_map *comp_map; 48430af9729SIra Weiny struct cxl_device_reg_map *dev_map; 4857dc7a64dSBen Widawsky struct device *dev = &pdev->dev; 486a261e9a1SDan Williams void __iomem *base = map->base; 48730af9729SIra Weiny 48830af9729SIra Weiny switch (map->reg_type) { 48908422378SBen Widawsky case CXL_REGLOC_RBI_COMPONENT: 49008422378SBen Widawsky comp_map = &map->component_map; 49108422378SBen Widawsky cxl_probe_component_regs(dev, base, comp_map); 49208422378SBen Widawsky if (!comp_map->hdm_decoder.valid) { 49308422378SBen Widawsky dev_err(dev, "HDM decoder registers not found\n"); 49408422378SBen Widawsky return -ENXIO; 49508422378SBen Widawsky } 49608422378SBen Widawsky 497bd09626bSDan Williams if (!comp_map->ras.valid) 498bd09626bSDan Williams dev_dbg(dev, "RAS registers not found\n"); 499bd09626bSDan Williams 50008422378SBen Widawsky dev_dbg(dev, "Set up component registers\n"); 50108422378SBen Widawsky break; 50230af9729SIra Weiny case CXL_REGLOC_RBI_MEMDEV: 50330af9729SIra Weiny dev_map = &map->device_map; 50430af9729SIra Weiny cxl_probe_device_regs(dev, base, dev_map); 50530af9729SIra Weiny if (!dev_map->status.valid || !dev_map->mbox.valid || 50630af9729SIra Weiny !dev_map->memdev.valid) { 50730af9729SIra Weiny dev_err(dev, "registers not found: %s%s%s\n", 50830af9729SIra Weiny !dev_map->status.valid ? "status " : "", 509da582aa5SLi Qiang (Johnny Li) !dev_map->mbox.valid ? "mbox " : "", 510da582aa5SLi Qiang (Johnny Li) !dev_map->memdev.valid ? "memdev " : ""); 51130af9729SIra Weiny return -ENXIO; 51230af9729SIra Weiny } 51330af9729SIra Weiny 51430af9729SIra Weiny dev_dbg(dev, "Probing device registers...\n"); 51530af9729SIra Weiny break; 51630af9729SIra Weiny default: 51730af9729SIra Weiny break; 51830af9729SIra Weiny } 51930af9729SIra Weiny 52030af9729SIra Weiny return 0; 52130af9729SIra Weiny } 52230af9729SIra Weiny 52385afc317SBen Widawsky static int cxl_setup_regs(struct pci_dev *pdev, enum cxl_regloc_type type, 52485afc317SBen Widawsky struct cxl_register_map *map) 52585afc317SBen Widawsky { 52685afc317SBen Widawsky int rc; 52785afc317SBen Widawsky 52885afc317SBen Widawsky rc = cxl_find_regblock(pdev, type, map); 52985afc317SBen Widawsky if (rc) 53085afc317SBen Widawsky return rc; 53185afc317SBen Widawsky 53285afc317SBen Widawsky rc = cxl_map_regblock(pdev, map); 53385afc317SBen Widawsky if (rc) 53485afc317SBen Widawsky return rc; 53585afc317SBen Widawsky 53685afc317SBen Widawsky rc = cxl_probe_regs(pdev, map); 537a261e9a1SDan Williams cxl_unmap_regblock(pdev, map); 5385b68705dSBen Widawsky 53985afc317SBen Widawsky return rc; 5401d5a4159SBen Widawsky } 5411d5a4159SBen Widawsky 5420a19bfc8SDan Williams /* 5430a19bfc8SDan Williams * Assume that any RCIEP that emits the CXL memory expander class code 5440a19bfc8SDan Williams * is an RCD 5450a19bfc8SDan Williams */ 5460a19bfc8SDan Williams static bool is_cxl_restricted(struct pci_dev *pdev) 5470a19bfc8SDan Williams { 5480a19bfc8SDan Williams return pci_pcie_type(pdev) == PCI_EXP_TYPE_RC_END; 5490a19bfc8SDan Williams } 5500a19bfc8SDan Williams 551248529edSDave Jiang /* 552248529edSDave Jiang * CXL v3.0 6.2.3 Table 6-4 553248529edSDave Jiang * The table indicates that if PCIe Flit Mode is set, then CXL is in 256B flits 554248529edSDave Jiang * mode, otherwise it's 68B flits mode. 555248529edSDave Jiang */ 556248529edSDave Jiang static bool cxl_pci_flit_256(struct pci_dev *pdev) 5572905cb52SDan Williams { 558248529edSDave Jiang u16 lnksta2; 559248529edSDave Jiang 560248529edSDave Jiang pcie_capability_read_word(pdev, PCI_EXP_LNKSTA2, &lnksta2); 561248529edSDave Jiang return lnksta2 & PCI_EXP_LNKSTA2_FLIT; 562248529edSDave Jiang } 563248529edSDave Jiang 564248529edSDave Jiang static int cxl_pci_ras_unmask(struct pci_dev *pdev) 565248529edSDave Jiang { 566248529edSDave Jiang struct pci_host_bridge *host_bridge = pci_find_host_bridge(pdev->bus); 567248529edSDave Jiang struct cxl_dev_state *cxlds = pci_get_drvdata(pdev); 568248529edSDave Jiang void __iomem *addr; 569248529edSDave Jiang u32 orig_val, val, mask; 570248529edSDave Jiang u16 cap; 571248529edSDave Jiang int rc; 572248529edSDave Jiang 573248529edSDave Jiang if (!cxlds->regs.ras) { 574248529edSDave Jiang dev_dbg(&pdev->dev, "No RAS registers.\n"); 575248529edSDave Jiang return 0; 576248529edSDave Jiang } 577248529edSDave Jiang 578248529edSDave Jiang /* BIOS has CXL error control */ 579248529edSDave Jiang if (!host_bridge->native_cxl_error) 580248529edSDave Jiang return -ENXIO; 581248529edSDave Jiang 582248529edSDave Jiang rc = pcie_capability_read_word(pdev, PCI_EXP_DEVCTL, &cap); 583248529edSDave Jiang if (rc) 584248529edSDave Jiang return rc; 585248529edSDave Jiang 586248529edSDave Jiang if (cap & PCI_EXP_DEVCTL_URRE) { 587248529edSDave Jiang addr = cxlds->regs.ras + CXL_RAS_UNCORRECTABLE_MASK_OFFSET; 588248529edSDave Jiang orig_val = readl(addr); 589248529edSDave Jiang 590248529edSDave Jiang mask = CXL_RAS_UNCORRECTABLE_MASK_MASK; 591248529edSDave Jiang if (!cxl_pci_flit_256(pdev)) 592248529edSDave Jiang mask &= ~CXL_RAS_UNCORRECTABLE_MASK_F256B_MASK; 593248529edSDave Jiang val = orig_val & ~mask; 594248529edSDave Jiang writel(val, addr); 595248529edSDave Jiang dev_dbg(&pdev->dev, 596248529edSDave Jiang "Uncorrectable RAS Errors Mask: %#x -> %#x\n", 597248529edSDave Jiang orig_val, val); 598248529edSDave Jiang } 599248529edSDave Jiang 600248529edSDave Jiang if (cap & PCI_EXP_DEVCTL_CERE) { 601248529edSDave Jiang addr = cxlds->regs.ras + CXL_RAS_CORRECTABLE_MASK_OFFSET; 602248529edSDave Jiang orig_val = readl(addr); 603248529edSDave Jiang val = orig_val & ~CXL_RAS_CORRECTABLE_MASK_MASK; 604248529edSDave Jiang writel(val, addr); 605248529edSDave Jiang dev_dbg(&pdev->dev, "Correctable RAS Errors Mask: %#x -> %#x\n", 606248529edSDave Jiang orig_val, val); 607248529edSDave Jiang } 608248529edSDave Jiang 609248529edSDave Jiang return 0; 6102905cb52SDan Williams } 6112905cb52SDan Williams 6126ebe28f9SIra Weiny static void free_event_buf(void *buf) 6136ebe28f9SIra Weiny { 6146ebe28f9SIra Weiny kvfree(buf); 6156ebe28f9SIra Weiny } 6166ebe28f9SIra Weiny 6176ebe28f9SIra Weiny /* 6186ebe28f9SIra Weiny * There is a single buffer for reading event logs from the mailbox. All logs 6196ebe28f9SIra Weiny * share this buffer protected by the cxlds->event_log_lock. 6206ebe28f9SIra Weiny */ 6216ebe28f9SIra Weiny static int cxl_mem_alloc_event_buf(struct cxl_dev_state *cxlds) 6226ebe28f9SIra Weiny { 6236ebe28f9SIra Weiny struct cxl_get_event_payload *buf; 6246ebe28f9SIra Weiny 6256ebe28f9SIra Weiny buf = kvmalloc(cxlds->payload_size, GFP_KERNEL); 6266ebe28f9SIra Weiny if (!buf) 6276ebe28f9SIra Weiny return -ENOMEM; 6286ebe28f9SIra Weiny cxlds->event.buf = buf; 6296ebe28f9SIra Weiny 6306ebe28f9SIra Weiny return devm_add_action_or_reset(cxlds->dev, free_event_buf, buf); 6316ebe28f9SIra Weiny } 6326ebe28f9SIra Weiny 633a49aa814SDavidlohr Bueso static int cxl_alloc_irq_vectors(struct pci_dev *pdev) 634a49aa814SDavidlohr Bueso { 635a49aa814SDavidlohr Bueso int nvecs; 636a49aa814SDavidlohr Bueso 637a49aa814SDavidlohr Bueso /* 638a49aa814SDavidlohr Bueso * Per CXL 3.0 3.1.1 CXL.io Endpoint a function on a CXL device must 639a49aa814SDavidlohr Bueso * not generate INTx messages if that function participates in 640a49aa814SDavidlohr Bueso * CXL.cache or CXL.mem. 641a49aa814SDavidlohr Bueso * 642a49aa814SDavidlohr Bueso * Additionally pci_alloc_irq_vectors() handles calling 643a49aa814SDavidlohr Bueso * pci_free_irq_vectors() automatically despite not being called 644a49aa814SDavidlohr Bueso * pcim_*. See pci_setup_msi_context(). 645a49aa814SDavidlohr Bueso */ 646a49aa814SDavidlohr Bueso nvecs = pci_alloc_irq_vectors(pdev, 1, CXL_PCI_DEFAULT_MAX_VECTORS, 647a49aa814SDavidlohr Bueso PCI_IRQ_MSIX | PCI_IRQ_MSI); 648a49aa814SDavidlohr Bueso if (nvecs < 1) { 649a49aa814SDavidlohr Bueso dev_dbg(&pdev->dev, "Failed to alloc irq vectors: %d\n", nvecs); 650a49aa814SDavidlohr Bueso return -ENXIO; 651a49aa814SDavidlohr Bueso } 652a49aa814SDavidlohr Bueso return 0; 653a49aa814SDavidlohr Bueso } 654a49aa814SDavidlohr Bueso 655a49aa814SDavidlohr Bueso static irqreturn_t cxl_event_thread(int irq, void *id) 656a49aa814SDavidlohr Bueso { 657a49aa814SDavidlohr Bueso struct cxl_dev_id *dev_id = id; 658a49aa814SDavidlohr Bueso struct cxl_dev_state *cxlds = dev_id->cxlds; 659a49aa814SDavidlohr Bueso u32 status; 660a49aa814SDavidlohr Bueso 661a49aa814SDavidlohr Bueso do { 662a49aa814SDavidlohr Bueso /* 663a49aa814SDavidlohr Bueso * CXL 3.0 8.2.8.3.1: The lower 32 bits are the status; 664a49aa814SDavidlohr Bueso * ignore the reserved upper 32 bits 665a49aa814SDavidlohr Bueso */ 666a49aa814SDavidlohr Bueso status = readl(cxlds->regs.status + CXLDEV_DEV_EVENT_STATUS_OFFSET); 667a49aa814SDavidlohr Bueso /* Ignore logs unknown to the driver */ 668a49aa814SDavidlohr Bueso status &= CXLDEV_EVENT_STATUS_ALL; 669a49aa814SDavidlohr Bueso if (!status) 670a49aa814SDavidlohr Bueso break; 671a49aa814SDavidlohr Bueso cxl_mem_get_event_records(cxlds, status); 672a49aa814SDavidlohr Bueso cond_resched(); 673a49aa814SDavidlohr Bueso } while (status); 674a49aa814SDavidlohr Bueso 675a49aa814SDavidlohr Bueso return IRQ_HANDLED; 676a49aa814SDavidlohr Bueso } 677a49aa814SDavidlohr Bueso 678a49aa814SDavidlohr Bueso static int cxl_event_req_irq(struct cxl_dev_state *cxlds, u8 setting) 679a49aa814SDavidlohr Bueso { 6809f7a320dSDavidlohr Bueso struct pci_dev *pdev = to_pci_dev(cxlds->dev); 681a49aa814SDavidlohr Bueso int irq; 682a49aa814SDavidlohr Bueso 683a49aa814SDavidlohr Bueso if (FIELD_GET(CXLDEV_EVENT_INT_MODE_MASK, setting) != CXL_INT_MSI_MSIX) 684a49aa814SDavidlohr Bueso return -ENXIO; 685a49aa814SDavidlohr Bueso 686a49aa814SDavidlohr Bueso irq = pci_irq_vector(pdev, 687a49aa814SDavidlohr Bueso FIELD_GET(CXLDEV_EVENT_INT_MSGNUM_MASK, setting)); 688a49aa814SDavidlohr Bueso if (irq < 0) 689a49aa814SDavidlohr Bueso return irq; 690a49aa814SDavidlohr Bueso 6919f7a320dSDavidlohr Bueso return cxl_request_irq(cxlds, irq, NULL, cxl_event_thread); 692a49aa814SDavidlohr Bueso } 693a49aa814SDavidlohr Bueso 694a49aa814SDavidlohr Bueso static int cxl_event_get_int_policy(struct cxl_dev_state *cxlds, 695a49aa814SDavidlohr Bueso struct cxl_event_interrupt_policy *policy) 696a49aa814SDavidlohr Bueso { 697a49aa814SDavidlohr Bueso struct cxl_mbox_cmd mbox_cmd = { 698a49aa814SDavidlohr Bueso .opcode = CXL_MBOX_OP_GET_EVT_INT_POLICY, 699a49aa814SDavidlohr Bueso .payload_out = policy, 700a49aa814SDavidlohr Bueso .size_out = sizeof(*policy), 701a49aa814SDavidlohr Bueso }; 702a49aa814SDavidlohr Bueso int rc; 703a49aa814SDavidlohr Bueso 704a49aa814SDavidlohr Bueso rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 705a49aa814SDavidlohr Bueso if (rc < 0) 706a49aa814SDavidlohr Bueso dev_err(cxlds->dev, "Failed to get event interrupt policy : %d", 707a49aa814SDavidlohr Bueso rc); 708a49aa814SDavidlohr Bueso 709a49aa814SDavidlohr Bueso return rc; 710a49aa814SDavidlohr Bueso } 711a49aa814SDavidlohr Bueso 712a49aa814SDavidlohr Bueso static int cxl_event_config_msgnums(struct cxl_dev_state *cxlds, 713a49aa814SDavidlohr Bueso struct cxl_event_interrupt_policy *policy) 714a49aa814SDavidlohr Bueso { 715a49aa814SDavidlohr Bueso struct cxl_mbox_cmd mbox_cmd; 716a49aa814SDavidlohr Bueso int rc; 717a49aa814SDavidlohr Bueso 718a49aa814SDavidlohr Bueso *policy = (struct cxl_event_interrupt_policy) { 719a49aa814SDavidlohr Bueso .info_settings = CXL_INT_MSI_MSIX, 720a49aa814SDavidlohr Bueso .warn_settings = CXL_INT_MSI_MSIX, 721a49aa814SDavidlohr Bueso .failure_settings = CXL_INT_MSI_MSIX, 722a49aa814SDavidlohr Bueso .fatal_settings = CXL_INT_MSI_MSIX, 723a49aa814SDavidlohr Bueso }; 724a49aa814SDavidlohr Bueso 725a49aa814SDavidlohr Bueso mbox_cmd = (struct cxl_mbox_cmd) { 726a49aa814SDavidlohr Bueso .opcode = CXL_MBOX_OP_SET_EVT_INT_POLICY, 727a49aa814SDavidlohr Bueso .payload_in = policy, 728a49aa814SDavidlohr Bueso .size_in = sizeof(*policy), 729a49aa814SDavidlohr Bueso }; 730a49aa814SDavidlohr Bueso 731a49aa814SDavidlohr Bueso rc = cxl_internal_send_cmd(cxlds, &mbox_cmd); 732a49aa814SDavidlohr Bueso if (rc < 0) { 733a49aa814SDavidlohr Bueso dev_err(cxlds->dev, "Failed to set event interrupt policy : %d", 734a49aa814SDavidlohr Bueso rc); 735a49aa814SDavidlohr Bueso return rc; 736a49aa814SDavidlohr Bueso } 737a49aa814SDavidlohr Bueso 738a49aa814SDavidlohr Bueso /* Retrieve final interrupt settings */ 739a49aa814SDavidlohr Bueso return cxl_event_get_int_policy(cxlds, policy); 740a49aa814SDavidlohr Bueso } 741a49aa814SDavidlohr Bueso 742a49aa814SDavidlohr Bueso static int cxl_event_irqsetup(struct cxl_dev_state *cxlds) 743a49aa814SDavidlohr Bueso { 744a49aa814SDavidlohr Bueso struct cxl_event_interrupt_policy policy; 745a49aa814SDavidlohr Bueso int rc; 746a49aa814SDavidlohr Bueso 747a49aa814SDavidlohr Bueso rc = cxl_event_config_msgnums(cxlds, &policy); 748a49aa814SDavidlohr Bueso if (rc) 749a49aa814SDavidlohr Bueso return rc; 750a49aa814SDavidlohr Bueso 751a49aa814SDavidlohr Bueso rc = cxl_event_req_irq(cxlds, policy.info_settings); 752a49aa814SDavidlohr Bueso if (rc) { 753a49aa814SDavidlohr Bueso dev_err(cxlds->dev, "Failed to get interrupt for event Info log\n"); 754a49aa814SDavidlohr Bueso return rc; 755a49aa814SDavidlohr Bueso } 756a49aa814SDavidlohr Bueso 757a49aa814SDavidlohr Bueso rc = cxl_event_req_irq(cxlds, policy.warn_settings); 758a49aa814SDavidlohr Bueso if (rc) { 759a49aa814SDavidlohr Bueso dev_err(cxlds->dev, "Failed to get interrupt for event Warn log\n"); 760a49aa814SDavidlohr Bueso return rc; 761a49aa814SDavidlohr Bueso } 762a49aa814SDavidlohr Bueso 763a49aa814SDavidlohr Bueso rc = cxl_event_req_irq(cxlds, policy.failure_settings); 764a49aa814SDavidlohr Bueso if (rc) { 765a49aa814SDavidlohr Bueso dev_err(cxlds->dev, "Failed to get interrupt for event Failure log\n"); 766a49aa814SDavidlohr Bueso return rc; 767a49aa814SDavidlohr Bueso } 768a49aa814SDavidlohr Bueso 769a49aa814SDavidlohr Bueso rc = cxl_event_req_irq(cxlds, policy.fatal_settings); 770a49aa814SDavidlohr Bueso if (rc) { 771a49aa814SDavidlohr Bueso dev_err(cxlds->dev, "Failed to get interrupt for event Fatal log\n"); 772a49aa814SDavidlohr Bueso return rc; 773a49aa814SDavidlohr Bueso } 774a49aa814SDavidlohr Bueso 775a49aa814SDavidlohr Bueso return 0; 776a49aa814SDavidlohr Bueso } 777a49aa814SDavidlohr Bueso 778a49aa814SDavidlohr Bueso static bool cxl_event_int_is_fw(u8 setting) 779a49aa814SDavidlohr Bueso { 780a49aa814SDavidlohr Bueso u8 mode = FIELD_GET(CXLDEV_EVENT_INT_MODE_MASK, setting); 781a49aa814SDavidlohr Bueso 782a49aa814SDavidlohr Bueso return mode == CXL_INT_FW; 783a49aa814SDavidlohr Bueso } 784a49aa814SDavidlohr Bueso 785a49aa814SDavidlohr Bueso static int cxl_event_config(struct pci_host_bridge *host_bridge, 786a49aa814SDavidlohr Bueso struct cxl_dev_state *cxlds) 787a49aa814SDavidlohr Bueso { 788a49aa814SDavidlohr Bueso struct cxl_event_interrupt_policy policy; 789a49aa814SDavidlohr Bueso int rc; 790a49aa814SDavidlohr Bueso 791a49aa814SDavidlohr Bueso /* 792a49aa814SDavidlohr Bueso * When BIOS maintains CXL error reporting control, it will process 793a49aa814SDavidlohr Bueso * event records. Only one agent can do so. 794a49aa814SDavidlohr Bueso */ 795a49aa814SDavidlohr Bueso if (!host_bridge->native_cxl_error) 796a49aa814SDavidlohr Bueso return 0; 797a49aa814SDavidlohr Bueso 798a49aa814SDavidlohr Bueso rc = cxl_mem_alloc_event_buf(cxlds); 799a49aa814SDavidlohr Bueso if (rc) 800a49aa814SDavidlohr Bueso return rc; 801a49aa814SDavidlohr Bueso 802a49aa814SDavidlohr Bueso rc = cxl_event_get_int_policy(cxlds, &policy); 803a49aa814SDavidlohr Bueso if (rc) 804a49aa814SDavidlohr Bueso return rc; 805a49aa814SDavidlohr Bueso 806a49aa814SDavidlohr Bueso if (cxl_event_int_is_fw(policy.info_settings) || 807a49aa814SDavidlohr Bueso cxl_event_int_is_fw(policy.warn_settings) || 808a49aa814SDavidlohr Bueso cxl_event_int_is_fw(policy.failure_settings) || 809a49aa814SDavidlohr Bueso cxl_event_int_is_fw(policy.fatal_settings)) { 810a49aa814SDavidlohr Bueso dev_err(cxlds->dev, "FW still in control of Event Logs despite _OSC settings\n"); 811a49aa814SDavidlohr Bueso return -EBUSY; 812a49aa814SDavidlohr Bueso } 813a49aa814SDavidlohr Bueso 814a49aa814SDavidlohr Bueso rc = cxl_event_irqsetup(cxlds); 815a49aa814SDavidlohr Bueso if (rc) 816a49aa814SDavidlohr Bueso return rc; 817a49aa814SDavidlohr Bueso 818a49aa814SDavidlohr Bueso cxl_mem_get_event_records(cxlds, CXLDEV_EVENT_STATUS_ALL); 819a49aa814SDavidlohr Bueso 820a49aa814SDavidlohr Bueso return 0; 821a49aa814SDavidlohr Bueso } 822a49aa814SDavidlohr Bueso 823ed97afb5SBen Widawsky static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) 82421e9f767SBen Widawsky { 8256ebe28f9SIra Weiny struct pci_host_bridge *host_bridge = pci_find_host_bridge(pdev->bus); 82685afc317SBen Widawsky struct cxl_register_map map; 82721083f51SDan Williams struct cxl_memdev *cxlmd; 8285e2411aeSIra Weiny struct cxl_dev_state *cxlds; 8291d5a4159SBen Widawsky int rc; 83021e9f767SBen Widawsky 8315a2328f4SDan Williams /* 8325a2328f4SDan Williams * Double check the anonymous union trickery in struct cxl_regs 8335a2328f4SDan Williams * FIXME switch to struct_group() 8345a2328f4SDan Williams */ 8355a2328f4SDan Williams BUILD_BUG_ON(offsetof(struct cxl_regs, memdev) != 8365a2328f4SDan Williams offsetof(struct cxl_regs, device_regs.memdev)); 8375a2328f4SDan Williams 83821e9f767SBen Widawsky rc = pcim_enable_device(pdev); 83921e9f767SBen Widawsky if (rc) 84021e9f767SBen Widawsky return rc; 841a49aa814SDavidlohr Bueso pci_set_master(pdev); 84221e9f767SBen Widawsky 8435e2411aeSIra Weiny cxlds = cxl_dev_state_create(&pdev->dev); 8445e2411aeSIra Weiny if (IS_ERR(cxlds)) 8455e2411aeSIra Weiny return PTR_ERR(cxlds); 8462905cb52SDan Williams pci_set_drvdata(pdev, cxlds); 8471b0a1a2aSBen Widawsky 8480a19bfc8SDan Williams cxlds->rcd = is_cxl_restricted(pdev); 849bcc79ea3SDan Williams cxlds->serial = pci_get_dsn(pdev); 85006e279e5SBen Widawsky cxlds->cxl_dvsec = pci_find_dvsec_capability( 85106e279e5SBen Widawsky pdev, PCI_DVSEC_VENDOR_ID_CXL, CXL_DVSEC_PCIE_DEVICE); 85206e279e5SBen Widawsky if (!cxlds->cxl_dvsec) 85306e279e5SBen Widawsky dev_warn(&pdev->dev, 85406e279e5SBen Widawsky "Device DVSEC not present, skip CXL.mem init\n"); 85506e279e5SBen Widawsky 85685afc317SBen Widawsky rc = cxl_setup_regs(pdev, CXL_REGLOC_RBI_MEMDEV, &map); 85785afc317SBen Widawsky if (rc) 85885afc317SBen Widawsky return rc; 85985afc317SBen Widawsky 8606c7f4f1eSDan Williams rc = cxl_map_device_regs(&pdev->dev, &cxlds->regs.device_regs, &map); 86121e9f767SBen Widawsky if (rc) 86221e9f767SBen Widawsky return rc; 86321e9f767SBen Widawsky 8644112a08dSBen Widawsky /* 8654112a08dSBen Widawsky * If the component registers can't be found, the cxl_pci driver may 8664112a08dSBen Widawsky * still be useful for management functions so don't return an error. 8674112a08dSBen Widawsky */ 8684112a08dSBen Widawsky cxlds->component_reg_phys = CXL_RESOURCE_NONE; 8694112a08dSBen Widawsky rc = cxl_setup_regs(pdev, CXL_REGLOC_RBI_COMPONENT, &map); 8704112a08dSBen Widawsky if (rc) 8714112a08dSBen Widawsky dev_warn(&pdev->dev, "No component registers (%d)\n", rc); 8724112a08dSBen Widawsky 8736c7f4f1eSDan Williams cxlds->component_reg_phys = map.resource; 8744112a08dSBen Widawsky 875bd09626bSDan Williams rc = cxl_map_component_regs(&pdev->dev, &cxlds->regs.component, 876bd09626bSDan Williams &map, BIT(CXL_CM_CAP_CAP_ID_RAS)); 877bd09626bSDan Williams if (rc) 878bd09626bSDan Williams dev_dbg(&pdev->dev, "Failed to map RAS capability.\n"); 879bd09626bSDan Williams 880e764f122SDave Jiang rc = cxl_await_media_ready(cxlds); 881e764f122SDave Jiang if (rc == 0) 882e764f122SDave Jiang cxlds->media_ready = true; 883e764f122SDave Jiang else 884e764f122SDave Jiang dev_warn(&pdev->dev, "Media not active (%d)\n", rc); 885e764f122SDave Jiang 886f279d0bcSDavidlohr Bueso rc = cxl_alloc_irq_vectors(pdev); 887f279d0bcSDavidlohr Bueso if (rc) 888f279d0bcSDavidlohr Bueso return rc; 889f279d0bcSDavidlohr Bueso 8905e2411aeSIra Weiny rc = cxl_pci_setup_mailbox(cxlds); 89121e9f767SBen Widawsky if (rc) 89221e9f767SBen Widawsky return rc; 89321e9f767SBen Widawsky 8945e2411aeSIra Weiny rc = cxl_enumerate_cmds(cxlds); 89521e9f767SBen Widawsky if (rc) 89621e9f767SBen Widawsky return rc; 89721e9f767SBen Widawsky 898fa884345SJonathan Cameron rc = cxl_set_timestamp(cxlds); 899fa884345SJonathan Cameron if (rc) 900fa884345SJonathan Cameron return rc; 901fa884345SJonathan Cameron 902d0abf578SAlison Schofield rc = cxl_poison_state_init(cxlds); 903d0abf578SAlison Schofield if (rc) 904d0abf578SAlison Schofield return rc; 905d0abf578SAlison Schofield 9065e2411aeSIra Weiny rc = cxl_dev_state_identify(cxlds); 90721e9f767SBen Widawsky if (rc) 90821e9f767SBen Widawsky return rc; 90921e9f767SBen Widawsky 9105e2411aeSIra Weiny rc = cxl_mem_create_range_info(cxlds); 911f847502aSIra Weiny if (rc) 912f847502aSIra Weiny return rc; 913f847502aSIra Weiny 9145e2411aeSIra Weiny cxlmd = devm_cxl_add_memdev(cxlds); 91521083f51SDan Williams if (IS_ERR(cxlmd)) 91621083f51SDan Williams return PTR_ERR(cxlmd); 91721083f51SDan Williams 918a49aa814SDavidlohr Bueso rc = cxl_event_config(host_bridge, cxlds); 9196ebe28f9SIra Weiny if (rc) 9206ebe28f9SIra Weiny return rc; 9216ebe28f9SIra Weiny 922248529edSDave Jiang rc = cxl_pci_ras_unmask(pdev); 9232905cb52SDan Williams if (rc) 924248529edSDave Jiang dev_dbg(&pdev->dev, "No RAS reporting unmasked\n"); 925248529edSDave Jiang 9262905cb52SDan Williams pci_save_state(pdev); 9272905cb52SDan Williams 92821083f51SDan Williams return rc; 92921e9f767SBen Widawsky } 93021e9f767SBen Widawsky 93121e9f767SBen Widawsky static const struct pci_device_id cxl_mem_pci_tbl[] = { 93221e9f767SBen Widawsky /* PCI class code for CXL.mem Type-3 Devices */ 93321e9f767SBen Widawsky { PCI_DEVICE_CLASS((PCI_CLASS_MEMORY_CXL << 8 | CXL_MEMORY_PROGIF), ~0)}, 93421e9f767SBen Widawsky { /* terminate list */ }, 93521e9f767SBen Widawsky }; 93621e9f767SBen Widawsky MODULE_DEVICE_TABLE(pci, cxl_mem_pci_tbl); 93721e9f767SBen Widawsky 9382905cb52SDan Williams static pci_ers_result_t cxl_slot_reset(struct pci_dev *pdev) 9392905cb52SDan Williams { 9402905cb52SDan Williams struct cxl_dev_state *cxlds = pci_get_drvdata(pdev); 9412905cb52SDan Williams struct cxl_memdev *cxlmd = cxlds->cxlmd; 9422905cb52SDan Williams struct device *dev = &cxlmd->dev; 9432905cb52SDan Williams 9442905cb52SDan Williams dev_info(&pdev->dev, "%s: restart CXL.mem after slot reset\n", 9452905cb52SDan Williams dev_name(dev)); 9462905cb52SDan Williams pci_restore_state(pdev); 9472905cb52SDan Williams if (device_attach(dev) <= 0) 9482905cb52SDan Williams return PCI_ERS_RESULT_DISCONNECT; 9492905cb52SDan Williams return PCI_ERS_RESULT_RECOVERED; 9502905cb52SDan Williams } 9512905cb52SDan Williams 9522905cb52SDan Williams static void cxl_error_resume(struct pci_dev *pdev) 9532905cb52SDan Williams { 9542905cb52SDan Williams struct cxl_dev_state *cxlds = pci_get_drvdata(pdev); 9552905cb52SDan Williams struct cxl_memdev *cxlmd = cxlds->cxlmd; 9562905cb52SDan Williams struct device *dev = &cxlmd->dev; 9572905cb52SDan Williams 9582905cb52SDan Williams dev_info(&pdev->dev, "%s: error resume %s\n", dev_name(dev), 9592905cb52SDan Williams dev->driver ? "successful" : "failed"); 9602905cb52SDan Williams } 9612905cb52SDan Williams 9622905cb52SDan Williams static const struct pci_error_handlers cxl_error_handlers = { 9632905cb52SDan Williams .error_detected = cxl_error_detected, 9642905cb52SDan Williams .slot_reset = cxl_slot_reset, 9652905cb52SDan Williams .resume = cxl_error_resume, 9666155ccc9SDave Jiang .cor_error_detected = cxl_cor_error_detected, 9672905cb52SDan Williams }; 9682905cb52SDan Williams 969ed97afb5SBen Widawsky static struct pci_driver cxl_pci_driver = { 97021e9f767SBen Widawsky .name = KBUILD_MODNAME, 97121e9f767SBen Widawsky .id_table = cxl_mem_pci_tbl, 972ed97afb5SBen Widawsky .probe = cxl_pci_probe, 9732905cb52SDan Williams .err_handler = &cxl_error_handlers, 97421e9f767SBen Widawsky .driver = { 97521e9f767SBen Widawsky .probe_type = PROBE_PREFER_ASYNCHRONOUS, 97621e9f767SBen Widawsky }, 97721e9f767SBen Widawsky }; 97821e9f767SBen Widawsky 97921e9f767SBen Widawsky MODULE_LICENSE("GPL v2"); 980ed97afb5SBen Widawsky module_pci_driver(cxl_pci_driver); 98121e9f767SBen Widawsky MODULE_IMPORT_NS(CXL); 982