xref: /openbmc/linux/drivers/cxl/pci.c (revision 1ad3f701c3999904d0c6cdea299df16c6cd9878d)
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>
12 #include <linux/io.h>
13 #include "cxlmem.h"
14 #include "cxlpci.h"
15 #include "cxl.h"
16 #include "pmu.h"
17 
18 /**
19  * DOC: cxl pci
20  *
21  * This implements the PCI exclusive functionality for a CXL device as it is
22  * defined by the Compute Express Link specification. CXL devices may surface
23  * certain functionality even if it isn't CXL enabled. While this driver is
24  * focused around the PCI specific aspects of a CXL device, it binds to the
25  * specific CXL memory device class code, and therefore the implementation of
26  * cxl_pci is focused around CXL memory devices.
27  *
28  * The driver has several responsibilities, mainly:
29  *  - Create the memX device and register on the CXL bus.
30  *  - Enumerate device's register interface and map them.
31  *  - Registers nvdimm bridge device with cxl_core.
32  *  - Registers a CXL mailbox with cxl_core.
33  */
34 
35 #define cxl_doorbell_busy(cxlds)                                                \
36 	(readl((cxlds)->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET) &                  \
37 	 CXLDEV_MBOX_CTRL_DOORBELL)
38 
39 /* CXL 2.0 - 8.2.8.4 */
40 #define CXL_MAILBOX_TIMEOUT_MS (2 * HZ)
41 
42 /*
43  * CXL 2.0 ECN "Add Mailbox Ready Time" defines a capability field to
44  * dictate how long to wait for the mailbox to become ready. The new
45  * field allows the device to tell software the amount of time to wait
46  * before mailbox ready. This field per the spec theoretically allows
47  * for up to 255 seconds. 255 seconds is unreasonably long, its longer
48  * than the maximum SATA port link recovery wait. Default to 60 seconds
49  * until someone builds a CXL device that needs more time in practice.
50  */
51 static unsigned short mbox_ready_timeout = 60;
52 module_param(mbox_ready_timeout, ushort, 0644);
53 MODULE_PARM_DESC(mbox_ready_timeout, "seconds to wait for mailbox ready");
54 
55 static int cxl_pci_mbox_wait_for_doorbell(struct cxl_dev_state *cxlds)
56 {
57 	const unsigned long start = jiffies;
58 	unsigned long end = start;
59 
60 	while (cxl_doorbell_busy(cxlds)) {
61 		end = jiffies;
62 
63 		if (time_after(end, start + CXL_MAILBOX_TIMEOUT_MS)) {
64 			/* Check again in case preempted before timeout test */
65 			if (!cxl_doorbell_busy(cxlds))
66 				break;
67 			return -ETIMEDOUT;
68 		}
69 		cpu_relax();
70 	}
71 
72 	dev_dbg(cxlds->dev, "Doorbell wait took %dms",
73 		jiffies_to_msecs(end) - jiffies_to_msecs(start));
74 	return 0;
75 }
76 
77 #define cxl_err(dev, status, msg)                                        \
78 	dev_err_ratelimited(dev, msg ", device state %s%s\n",                  \
79 			    status & CXLMDEV_DEV_FATAL ? " fatal" : "",        \
80 			    status & CXLMDEV_FW_HALT ? " firmware-halt" : "")
81 
82 #define cxl_cmd_err(dev, cmd, status, msg)                               \
83 	dev_err_ratelimited(dev, msg " (opcode: %#x), device state %s%s\n",    \
84 			    (cmd)->opcode,                                     \
85 			    status & CXLMDEV_DEV_FATAL ? " fatal" : "",        \
86 			    status & CXLMDEV_FW_HALT ? " firmware-halt" : "")
87 
88 /**
89  * __cxl_pci_mbox_send_cmd() - Execute a mailbox command
90  * @cxlds: The device state to communicate with.
91  * @mbox_cmd: Command to send to the memory device.
92  *
93  * Context: Any context. Expects mbox_mutex to be held.
94  * Return: -ETIMEDOUT if timeout occurred waiting for completion. 0 on success.
95  *         Caller should check the return code in @mbox_cmd to make sure it
96  *         succeeded.
97  *
98  * This is a generic form of the CXL mailbox send command thus only using the
99  * registers defined by the mailbox capability ID - CXL 2.0 8.2.8.4. Memory
100  * devices, and perhaps other types of CXL devices may have further information
101  * available upon error conditions. Driver facilities wishing to send mailbox
102  * commands should use the wrapper command.
103  *
104  * The CXL spec allows for up to two mailboxes. The intention is for the primary
105  * mailbox to be OS controlled and the secondary mailbox to be used by system
106  * firmware. This allows the OS and firmware to communicate with the device and
107  * not need to coordinate with each other. The driver only uses the primary
108  * mailbox.
109  */
110 static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds,
111 				   struct cxl_mbox_cmd *mbox_cmd)
112 {
113 	void __iomem *payload = cxlds->regs.mbox + CXLDEV_MBOX_PAYLOAD_OFFSET;
114 	struct device *dev = cxlds->dev;
115 	u64 cmd_reg, status_reg;
116 	size_t out_len;
117 	int rc;
118 
119 	lockdep_assert_held(&cxlds->mbox_mutex);
120 
121 	/*
122 	 * Here are the steps from 8.2.8.4 of the CXL 2.0 spec.
123 	 *   1. Caller reads MB Control Register to verify doorbell is clear
124 	 *   2. Caller writes Command Register
125 	 *   3. Caller writes Command Payload Registers if input payload is non-empty
126 	 *   4. Caller writes MB Control Register to set doorbell
127 	 *   5. Caller either polls for doorbell to be clear or waits for interrupt if configured
128 	 *   6. Caller reads MB Status Register to fetch Return code
129 	 *   7. If command successful, Caller reads Command Register to get Payload Length
130 	 *   8. If output payload is non-empty, host reads Command Payload Registers
131 	 *
132 	 * Hardware is free to do whatever it wants before the doorbell is rung,
133 	 * and isn't allowed to change anything after it clears the doorbell. As
134 	 * such, steps 2 and 3 can happen in any order, and steps 6, 7, 8 can
135 	 * also happen in any order (though some orders might not make sense).
136 	 */
137 
138 	/* #1 */
139 	if (cxl_doorbell_busy(cxlds)) {
140 		u64 md_status =
141 			readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
142 
143 		cxl_cmd_err(cxlds->dev, mbox_cmd, md_status,
144 			    "mailbox queue busy");
145 		return -EBUSY;
146 	}
147 
148 	cmd_reg = FIELD_PREP(CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK,
149 			     mbox_cmd->opcode);
150 	if (mbox_cmd->size_in) {
151 		if (WARN_ON(!mbox_cmd->payload_in))
152 			return -EINVAL;
153 
154 		cmd_reg |= FIELD_PREP(CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK,
155 				      mbox_cmd->size_in);
156 		memcpy_toio(payload, mbox_cmd->payload_in, mbox_cmd->size_in);
157 	}
158 
159 	/* #2, #3 */
160 	writeq(cmd_reg, cxlds->regs.mbox + CXLDEV_MBOX_CMD_OFFSET);
161 
162 	/* #4 */
163 	dev_dbg(dev, "Sending command: 0x%04x\n", mbox_cmd->opcode);
164 	writel(CXLDEV_MBOX_CTRL_DOORBELL,
165 	       cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET);
166 
167 	/* #5 */
168 	rc = cxl_pci_mbox_wait_for_doorbell(cxlds);
169 	if (rc == -ETIMEDOUT) {
170 		u64 md_status = readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
171 
172 		cxl_cmd_err(cxlds->dev, mbox_cmd, md_status, "mailbox timeout");
173 		return rc;
174 	}
175 
176 	/* #6 */
177 	status_reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_STATUS_OFFSET);
178 	mbox_cmd->return_code =
179 		FIELD_GET(CXLDEV_MBOX_STATUS_RET_CODE_MASK, status_reg);
180 
181 	if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS) {
182 		dev_dbg(dev, "Mailbox operation had an error: %s\n",
183 			cxl_mbox_cmd_rc2str(mbox_cmd));
184 		return 0; /* completed but caller must check return_code */
185 	}
186 
187 	/* #7 */
188 	cmd_reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_CMD_OFFSET);
189 	out_len = FIELD_GET(CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK, cmd_reg);
190 
191 	/* #8 */
192 	if (out_len && mbox_cmd->payload_out) {
193 		/*
194 		 * Sanitize the copy. If hardware misbehaves, out_len per the
195 		 * spec can actually be greater than the max allowed size (21
196 		 * bits available but spec defined 1M max). The caller also may
197 		 * have requested less data than the hardware supplied even
198 		 * within spec.
199 		 */
200 		size_t n = min3(mbox_cmd->size_out, cxlds->payload_size, out_len);
201 
202 		memcpy_fromio(mbox_cmd->payload_out, payload, n);
203 		mbox_cmd->size_out = n;
204 	} else {
205 		mbox_cmd->size_out = 0;
206 	}
207 
208 	return 0;
209 }
210 
211 static int cxl_pci_mbox_send(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd)
212 {
213 	int rc;
214 
215 	mutex_lock_io(&cxlds->mbox_mutex);
216 	rc = __cxl_pci_mbox_send_cmd(cxlds, cmd);
217 	mutex_unlock(&cxlds->mbox_mutex);
218 
219 	return rc;
220 }
221 
222 static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds)
223 {
224 	const int cap = readl(cxlds->regs.mbox + CXLDEV_MBOX_CAPS_OFFSET);
225 	unsigned long timeout;
226 	u64 md_status;
227 
228 	timeout = jiffies + mbox_ready_timeout * HZ;
229 	do {
230 		md_status = readq(cxlds->regs.memdev + CXLMDEV_STATUS_OFFSET);
231 		if (md_status & CXLMDEV_MBOX_IF_READY)
232 			break;
233 		if (msleep_interruptible(100))
234 			break;
235 	} while (!time_after(jiffies, timeout));
236 
237 	if (!(md_status & CXLMDEV_MBOX_IF_READY)) {
238 		cxl_err(cxlds->dev, md_status,
239 			"timeout awaiting mailbox ready");
240 		return -ETIMEDOUT;
241 	}
242 
243 	/*
244 	 * A command may be in flight from a previous driver instance,
245 	 * think kexec, do one doorbell wait so that
246 	 * __cxl_pci_mbox_send_cmd() can assume that it is the only
247 	 * source for future doorbell busy events.
248 	 */
249 	if (cxl_pci_mbox_wait_for_doorbell(cxlds) != 0) {
250 		cxl_err(cxlds->dev, md_status, "timeout awaiting mailbox idle");
251 		return -ETIMEDOUT;
252 	}
253 
254 	cxlds->mbox_send = cxl_pci_mbox_send;
255 	cxlds->payload_size =
256 		1 << FIELD_GET(CXLDEV_MBOX_CAP_PAYLOAD_SIZE_MASK, cap);
257 
258 	/*
259 	 * CXL 2.0 8.2.8.4.3 Mailbox Capabilities Register
260 	 *
261 	 * If the size is too small, mandatory commands will not work and so
262 	 * there's no point in going forward. If the size is too large, there's
263 	 * no harm is soft limiting it.
264 	 */
265 	cxlds->payload_size = min_t(size_t, cxlds->payload_size, SZ_1M);
266 	if (cxlds->payload_size < 256) {
267 		dev_err(cxlds->dev, "Mailbox is too small (%zub)",
268 			cxlds->payload_size);
269 		return -ENXIO;
270 	}
271 
272 	dev_dbg(cxlds->dev, "Mailbox payload sized %zu",
273 		cxlds->payload_size);
274 
275 	return 0;
276 }
277 
278 static int cxl_map_regblock(struct pci_dev *pdev, struct cxl_register_map *map)
279 {
280 	struct device *dev = &pdev->dev;
281 
282 	map->base = ioremap(map->resource, map->max_size);
283 	if (!map->base) {
284 		dev_err(dev, "failed to map registers\n");
285 		return -ENOMEM;
286 	}
287 
288 	dev_dbg(dev, "Mapped CXL Memory Device resource %pa\n", &map->resource);
289 	return 0;
290 }
291 
292 static void cxl_unmap_regblock(struct pci_dev *pdev,
293 			       struct cxl_register_map *map)
294 {
295 	iounmap(map->base);
296 	map->base = NULL;
297 }
298 
299 static int cxl_probe_regs(struct pci_dev *pdev, struct cxl_register_map *map)
300 {
301 	struct cxl_component_reg_map *comp_map;
302 	struct cxl_device_reg_map *dev_map;
303 	struct device *dev = &pdev->dev;
304 	void __iomem *base = map->base;
305 
306 	switch (map->reg_type) {
307 	case CXL_REGLOC_RBI_COMPONENT:
308 		comp_map = &map->component_map;
309 		cxl_probe_component_regs(dev, base, comp_map);
310 		if (!comp_map->hdm_decoder.valid) {
311 			dev_err(dev, "HDM decoder registers not found\n");
312 			return -ENXIO;
313 		}
314 
315 		if (!comp_map->ras.valid)
316 			dev_dbg(dev, "RAS registers not found\n");
317 
318 		dev_dbg(dev, "Set up component registers\n");
319 		break;
320 	case CXL_REGLOC_RBI_MEMDEV:
321 		dev_map = &map->device_map;
322 		cxl_probe_device_regs(dev, base, dev_map);
323 		if (!dev_map->status.valid || !dev_map->mbox.valid ||
324 		    !dev_map->memdev.valid) {
325 			dev_err(dev, "registers not found: %s%s%s\n",
326 				!dev_map->status.valid ? "status " : "",
327 				!dev_map->mbox.valid ? "mbox " : "",
328 				!dev_map->memdev.valid ? "memdev " : "");
329 			return -ENXIO;
330 		}
331 
332 		dev_dbg(dev, "Probing device registers...\n");
333 		break;
334 	default:
335 		break;
336 	}
337 
338 	return 0;
339 }
340 
341 static int cxl_setup_regs(struct pci_dev *pdev, enum cxl_regloc_type type,
342 			  struct cxl_register_map *map)
343 {
344 	int rc;
345 
346 	rc = cxl_find_regblock(pdev, type, map);
347 	if (rc)
348 		return rc;
349 
350 	rc = cxl_map_regblock(pdev, map);
351 	if (rc)
352 		return rc;
353 
354 	rc = cxl_probe_regs(pdev, map);
355 	cxl_unmap_regblock(pdev, map);
356 
357 	return rc;
358 }
359 
360 /*
361  * Assume that any RCIEP that emits the CXL memory expander class code
362  * is an RCD
363  */
364 static bool is_cxl_restricted(struct pci_dev *pdev)
365 {
366 	return pci_pcie_type(pdev) == PCI_EXP_TYPE_RC_END;
367 }
368 
369 /*
370  * CXL v3.0 6.2.3 Table 6-4
371  * The table indicates that if PCIe Flit Mode is set, then CXL is in 256B flits
372  * mode, otherwise it's 68B flits mode.
373  */
374 static bool cxl_pci_flit_256(struct pci_dev *pdev)
375 {
376 	u16 lnksta2;
377 
378 	pcie_capability_read_word(pdev, PCI_EXP_LNKSTA2, &lnksta2);
379 	return lnksta2 & PCI_EXP_LNKSTA2_FLIT;
380 }
381 
382 static int cxl_pci_ras_unmask(struct pci_dev *pdev)
383 {
384 	struct pci_host_bridge *host_bridge = pci_find_host_bridge(pdev->bus);
385 	struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
386 	void __iomem *addr;
387 	u32 orig_val, val, mask;
388 	u16 cap;
389 	int rc;
390 
391 	if (!cxlds->regs.ras) {
392 		dev_dbg(&pdev->dev, "No RAS registers.\n");
393 		return 0;
394 	}
395 
396 	/* BIOS has CXL error control */
397 	if (!host_bridge->native_cxl_error)
398 		return -ENXIO;
399 
400 	rc = pcie_capability_read_word(pdev, PCI_EXP_DEVCTL, &cap);
401 	if (rc)
402 		return rc;
403 
404 	if (cap & PCI_EXP_DEVCTL_URRE) {
405 		addr = cxlds->regs.ras + CXL_RAS_UNCORRECTABLE_MASK_OFFSET;
406 		orig_val = readl(addr);
407 
408 		mask = CXL_RAS_UNCORRECTABLE_MASK_MASK;
409 		if (!cxl_pci_flit_256(pdev))
410 			mask &= ~CXL_RAS_UNCORRECTABLE_MASK_F256B_MASK;
411 		val = orig_val & ~mask;
412 		writel(val, addr);
413 		dev_dbg(&pdev->dev,
414 			"Uncorrectable RAS Errors Mask: %#x -> %#x\n",
415 			orig_val, val);
416 	}
417 
418 	if (cap & PCI_EXP_DEVCTL_CERE) {
419 		addr = cxlds->regs.ras + CXL_RAS_CORRECTABLE_MASK_OFFSET;
420 		orig_val = readl(addr);
421 		val = orig_val & ~CXL_RAS_CORRECTABLE_MASK_MASK;
422 		writel(val, addr);
423 		dev_dbg(&pdev->dev, "Correctable RAS Errors Mask: %#x -> %#x\n",
424 			orig_val, val);
425 	}
426 
427 	return 0;
428 }
429 
430 static void free_event_buf(void *buf)
431 {
432 	kvfree(buf);
433 }
434 
435 /*
436  * There is a single buffer for reading event logs from the mailbox.  All logs
437  * share this buffer protected by the cxlds->event_log_lock.
438  */
439 static int cxl_mem_alloc_event_buf(struct cxl_dev_state *cxlds)
440 {
441 	struct cxl_get_event_payload *buf;
442 
443 	buf = kvmalloc(cxlds->payload_size, GFP_KERNEL);
444 	if (!buf)
445 		return -ENOMEM;
446 	cxlds->event.buf = buf;
447 
448 	return devm_add_action_or_reset(cxlds->dev, free_event_buf, buf);
449 }
450 
451 static int cxl_alloc_irq_vectors(struct pci_dev *pdev)
452 {
453 	int nvecs;
454 
455 	/*
456 	 * Per CXL 3.0 3.1.1 CXL.io Endpoint a function on a CXL device must
457 	 * not generate INTx messages if that function participates in
458 	 * CXL.cache or CXL.mem.
459 	 *
460 	 * Additionally pci_alloc_irq_vectors() handles calling
461 	 * pci_free_irq_vectors() automatically despite not being called
462 	 * pcim_*.  See pci_setup_msi_context().
463 	 */
464 	nvecs = pci_alloc_irq_vectors(pdev, 1, CXL_PCI_DEFAULT_MAX_VECTORS,
465 				      PCI_IRQ_MSIX | PCI_IRQ_MSI);
466 	if (nvecs < 1) {
467 		dev_dbg(&pdev->dev, "Failed to alloc irq vectors: %d\n", nvecs);
468 		return -ENXIO;
469 	}
470 	return 0;
471 }
472 
473 struct cxl_dev_id {
474 	struct cxl_dev_state *cxlds;
475 };
476 
477 static irqreturn_t cxl_event_thread(int irq, void *id)
478 {
479 	struct cxl_dev_id *dev_id = id;
480 	struct cxl_dev_state *cxlds = dev_id->cxlds;
481 	u32 status;
482 
483 	do {
484 		/*
485 		 * CXL 3.0 8.2.8.3.1: The lower 32 bits are the status;
486 		 * ignore the reserved upper 32 bits
487 		 */
488 		status = readl(cxlds->regs.status + CXLDEV_DEV_EVENT_STATUS_OFFSET);
489 		/* Ignore logs unknown to the driver */
490 		status &= CXLDEV_EVENT_STATUS_ALL;
491 		if (!status)
492 			break;
493 		cxl_mem_get_event_records(cxlds, status);
494 		cond_resched();
495 	} while (status);
496 
497 	return IRQ_HANDLED;
498 }
499 
500 static int cxl_event_req_irq(struct cxl_dev_state *cxlds, u8 setting)
501 {
502 	struct device *dev = cxlds->dev;
503 	struct pci_dev *pdev = to_pci_dev(dev);
504 	struct cxl_dev_id *dev_id;
505 	int irq;
506 
507 	if (FIELD_GET(CXLDEV_EVENT_INT_MODE_MASK, setting) != CXL_INT_MSI_MSIX)
508 		return -ENXIO;
509 
510 	/* dev_id must be globally unique and must contain the cxlds */
511 	dev_id = devm_kzalloc(dev, sizeof(*dev_id), GFP_KERNEL);
512 	if (!dev_id)
513 		return -ENOMEM;
514 	dev_id->cxlds = cxlds;
515 
516 	irq =  pci_irq_vector(pdev,
517 			      FIELD_GET(CXLDEV_EVENT_INT_MSGNUM_MASK, setting));
518 	if (irq < 0)
519 		return irq;
520 
521 	return devm_request_threaded_irq(dev, irq, NULL, cxl_event_thread,
522 					 IRQF_SHARED | IRQF_ONESHOT, NULL,
523 					 dev_id);
524 }
525 
526 static int cxl_event_get_int_policy(struct cxl_dev_state *cxlds,
527 				    struct cxl_event_interrupt_policy *policy)
528 {
529 	struct cxl_mbox_cmd mbox_cmd = {
530 		.opcode = CXL_MBOX_OP_GET_EVT_INT_POLICY,
531 		.payload_out = policy,
532 		.size_out = sizeof(*policy),
533 	};
534 	int rc;
535 
536 	rc = cxl_internal_send_cmd(cxlds, &mbox_cmd);
537 	if (rc < 0)
538 		dev_err(cxlds->dev, "Failed to get event interrupt policy : %d",
539 			rc);
540 
541 	return rc;
542 }
543 
544 static int cxl_event_config_msgnums(struct cxl_dev_state *cxlds,
545 				    struct cxl_event_interrupt_policy *policy)
546 {
547 	struct cxl_mbox_cmd mbox_cmd;
548 	int rc;
549 
550 	*policy = (struct cxl_event_interrupt_policy) {
551 		.info_settings = CXL_INT_MSI_MSIX,
552 		.warn_settings = CXL_INT_MSI_MSIX,
553 		.failure_settings = CXL_INT_MSI_MSIX,
554 		.fatal_settings = CXL_INT_MSI_MSIX,
555 	};
556 
557 	mbox_cmd = (struct cxl_mbox_cmd) {
558 		.opcode = CXL_MBOX_OP_SET_EVT_INT_POLICY,
559 		.payload_in = policy,
560 		.size_in = sizeof(*policy),
561 	};
562 
563 	rc = cxl_internal_send_cmd(cxlds, &mbox_cmd);
564 	if (rc < 0) {
565 		dev_err(cxlds->dev, "Failed to set event interrupt policy : %d",
566 			rc);
567 		return rc;
568 	}
569 
570 	/* Retrieve final interrupt settings */
571 	return cxl_event_get_int_policy(cxlds, policy);
572 }
573 
574 static int cxl_event_irqsetup(struct cxl_dev_state *cxlds)
575 {
576 	struct cxl_event_interrupt_policy policy;
577 	int rc;
578 
579 	rc = cxl_event_config_msgnums(cxlds, &policy);
580 	if (rc)
581 		return rc;
582 
583 	rc = cxl_event_req_irq(cxlds, policy.info_settings);
584 	if (rc) {
585 		dev_err(cxlds->dev, "Failed to get interrupt for event Info log\n");
586 		return rc;
587 	}
588 
589 	rc = cxl_event_req_irq(cxlds, policy.warn_settings);
590 	if (rc) {
591 		dev_err(cxlds->dev, "Failed to get interrupt for event Warn log\n");
592 		return rc;
593 	}
594 
595 	rc = cxl_event_req_irq(cxlds, policy.failure_settings);
596 	if (rc) {
597 		dev_err(cxlds->dev, "Failed to get interrupt for event Failure log\n");
598 		return rc;
599 	}
600 
601 	rc = cxl_event_req_irq(cxlds, policy.fatal_settings);
602 	if (rc) {
603 		dev_err(cxlds->dev, "Failed to get interrupt for event Fatal log\n");
604 		return rc;
605 	}
606 
607 	return 0;
608 }
609 
610 static bool cxl_event_int_is_fw(u8 setting)
611 {
612 	u8 mode = FIELD_GET(CXLDEV_EVENT_INT_MODE_MASK, setting);
613 
614 	return mode == CXL_INT_FW;
615 }
616 
617 static int cxl_event_config(struct pci_host_bridge *host_bridge,
618 			    struct cxl_dev_state *cxlds)
619 {
620 	struct cxl_event_interrupt_policy policy;
621 	int rc;
622 
623 	/*
624 	 * When BIOS maintains CXL error reporting control, it will process
625 	 * event records.  Only one agent can do so.
626 	 */
627 	if (!host_bridge->native_cxl_error)
628 		return 0;
629 
630 	rc = cxl_mem_alloc_event_buf(cxlds);
631 	if (rc)
632 		return rc;
633 
634 	rc = cxl_event_get_int_policy(cxlds, &policy);
635 	if (rc)
636 		return rc;
637 
638 	if (cxl_event_int_is_fw(policy.info_settings) ||
639 	    cxl_event_int_is_fw(policy.warn_settings) ||
640 	    cxl_event_int_is_fw(policy.failure_settings) ||
641 	    cxl_event_int_is_fw(policy.fatal_settings)) {
642 		dev_err(cxlds->dev, "FW still in control of Event Logs despite _OSC settings\n");
643 		return -EBUSY;
644 	}
645 
646 	rc = cxl_event_irqsetup(cxlds);
647 	if (rc)
648 		return rc;
649 
650 	cxl_mem_get_event_records(cxlds, CXLDEV_EVENT_STATUS_ALL);
651 
652 	return 0;
653 }
654 
655 static int cxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
656 {
657 	struct pci_host_bridge *host_bridge = pci_find_host_bridge(pdev->bus);
658 	struct cxl_register_map map;
659 	struct cxl_memdev *cxlmd;
660 	struct cxl_dev_state *cxlds;
661 	int i, rc, pmu_count;
662 
663 	/*
664 	 * Double check the anonymous union trickery in struct cxl_regs
665 	 * FIXME switch to struct_group()
666 	 */
667 	BUILD_BUG_ON(offsetof(struct cxl_regs, memdev) !=
668 		     offsetof(struct cxl_regs, device_regs.memdev));
669 
670 	rc = pcim_enable_device(pdev);
671 	if (rc)
672 		return rc;
673 	pci_set_master(pdev);
674 
675 	cxlds = cxl_dev_state_create(&pdev->dev);
676 	if (IS_ERR(cxlds))
677 		return PTR_ERR(cxlds);
678 	pci_set_drvdata(pdev, cxlds);
679 
680 	cxlds->rcd = is_cxl_restricted(pdev);
681 	cxlds->serial = pci_get_dsn(pdev);
682 	cxlds->cxl_dvsec = pci_find_dvsec_capability(
683 		pdev, PCI_DVSEC_VENDOR_ID_CXL, CXL_DVSEC_PCIE_DEVICE);
684 	if (!cxlds->cxl_dvsec)
685 		dev_warn(&pdev->dev,
686 			 "Device DVSEC not present, skip CXL.mem init\n");
687 
688 	rc = cxl_setup_regs(pdev, CXL_REGLOC_RBI_MEMDEV, &map);
689 	if (rc)
690 		return rc;
691 
692 	rc = cxl_map_device_regs(&pdev->dev, &cxlds->regs.device_regs, &map);
693 	if (rc)
694 		return rc;
695 
696 	/*
697 	 * If the component registers can't be found, the cxl_pci driver may
698 	 * still be useful for management functions so don't return an error.
699 	 */
700 	cxlds->component_reg_phys = CXL_RESOURCE_NONE;
701 	rc = cxl_setup_regs(pdev, CXL_REGLOC_RBI_COMPONENT, &map);
702 	if (rc)
703 		dev_warn(&pdev->dev, "No component registers (%d)\n", rc);
704 
705 	cxlds->component_reg_phys = map.resource;
706 
707 	rc = cxl_map_component_regs(&pdev->dev, &cxlds->regs.component,
708 				    &map, BIT(CXL_CM_CAP_CAP_ID_RAS));
709 	if (rc)
710 		dev_dbg(&pdev->dev, "Failed to map RAS capability.\n");
711 
712 	rc = cxl_await_media_ready(cxlds);
713 	if (rc == 0)
714 		cxlds->media_ready = true;
715 	else
716 		dev_warn(&pdev->dev, "Media not active (%d)\n", rc);
717 
718 	rc = cxl_pci_setup_mailbox(cxlds);
719 	if (rc)
720 		return rc;
721 
722 	rc = cxl_enumerate_cmds(cxlds);
723 	if (rc)
724 		return rc;
725 
726 	rc = cxl_set_timestamp(cxlds);
727 	if (rc)
728 		return rc;
729 
730 	rc = cxl_poison_state_init(cxlds);
731 	if (rc)
732 		return rc;
733 
734 	rc = cxl_dev_state_identify(cxlds);
735 	if (rc)
736 		return rc;
737 
738 	rc = cxl_mem_create_range_info(cxlds);
739 	if (rc)
740 		return rc;
741 
742 	rc = cxl_alloc_irq_vectors(pdev);
743 	if (rc)
744 		return rc;
745 
746 	cxlmd = devm_cxl_add_memdev(cxlds);
747 	if (IS_ERR(cxlmd))
748 		return PTR_ERR(cxlmd);
749 
750 	pmu_count = cxl_count_regblock(pdev, CXL_REGLOC_RBI_PMU);
751 	for (i = 0; i < pmu_count; i++) {
752 		struct cxl_pmu_regs pmu_regs;
753 
754 		rc = cxl_find_regblock_instance(pdev, CXL_REGLOC_RBI_PMU, &map, i);
755 		if (rc) {
756 			dev_dbg(&pdev->dev, "Could not find PMU regblock\n");
757 			break;
758 		}
759 
760 		rc = cxl_map_pmu_regs(pdev, &pmu_regs, &map);
761 		if (rc) {
762 			dev_dbg(&pdev->dev, "Could not map PMU regs\n");
763 			break;
764 		}
765 
766 		rc = devm_cxl_pmu_add(cxlds->dev, &pmu_regs, cxlmd->id, i, CXL_PMU_MEMDEV);
767 		if (rc) {
768 			dev_dbg(&pdev->dev, "Could not add PMU instance\n");
769 			break;
770 		}
771 	}
772 
773 	rc = cxl_event_config(host_bridge, cxlds);
774 	if (rc)
775 		return rc;
776 
777 	rc = cxl_pci_ras_unmask(pdev);
778 	if (rc)
779 		dev_dbg(&pdev->dev, "No RAS reporting unmasked\n");
780 
781 	pci_save_state(pdev);
782 
783 	return rc;
784 }
785 
786 static const struct pci_device_id cxl_mem_pci_tbl[] = {
787 	/* PCI class code for CXL.mem Type-3 Devices */
788 	{ PCI_DEVICE_CLASS((PCI_CLASS_MEMORY_CXL << 8 | CXL_MEMORY_PROGIF), ~0)},
789 	{ /* terminate list */ },
790 };
791 MODULE_DEVICE_TABLE(pci, cxl_mem_pci_tbl);
792 
793 static pci_ers_result_t cxl_slot_reset(struct pci_dev *pdev)
794 {
795 	struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
796 	struct cxl_memdev *cxlmd = cxlds->cxlmd;
797 	struct device *dev = &cxlmd->dev;
798 
799 	dev_info(&pdev->dev, "%s: restart CXL.mem after slot reset\n",
800 		 dev_name(dev));
801 	pci_restore_state(pdev);
802 	if (device_attach(dev) <= 0)
803 		return PCI_ERS_RESULT_DISCONNECT;
804 	return PCI_ERS_RESULT_RECOVERED;
805 }
806 
807 static void cxl_error_resume(struct pci_dev *pdev)
808 {
809 	struct cxl_dev_state *cxlds = pci_get_drvdata(pdev);
810 	struct cxl_memdev *cxlmd = cxlds->cxlmd;
811 	struct device *dev = &cxlmd->dev;
812 
813 	dev_info(&pdev->dev, "%s: error resume %s\n", dev_name(dev),
814 		 dev->driver ? "successful" : "failed");
815 }
816 
817 static const struct pci_error_handlers cxl_error_handlers = {
818 	.error_detected	= cxl_error_detected,
819 	.slot_reset	= cxl_slot_reset,
820 	.resume		= cxl_error_resume,
821 	.cor_error_detected	= cxl_cor_error_detected,
822 };
823 
824 static struct pci_driver cxl_pci_driver = {
825 	.name			= KBUILD_MODNAME,
826 	.id_table		= cxl_mem_pci_tbl,
827 	.probe			= cxl_pci_probe,
828 	.err_handler		= &cxl_error_handlers,
829 	.driver	= {
830 		.probe_type	= PROBE_PREFER_ASYNCHRONOUS,
831 	},
832 };
833 
834 MODULE_LICENSE("GPL v2");
835 module_pci_driver(cxl_pci_driver);
836 MODULE_IMPORT_NS(CXL);
837