4 * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
5 * VA Linux Systems Japan K.K.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "sysemu/sysemu.h"
23 #include "qapi/qmp/qdict.h"
24 #include "monitor/monitor.h"
25 #include "hw/pci/pci_bridge.h"
26 #include "hw/pci/pcie.h"
27 #include "hw/pci/msix.h"
28 #include "hw/pci/msi.h"
29 #include "hw/pci/pci_bus.h"
30 #include "hw/pci/pcie_regs.h"
31 #include "qapi/error.h"
35 # define PCIE_DPRINTF(fmt, ...) \
36 fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__)
38 # define PCIE_DPRINTF(fmt, ...) do {} while (0)
40 #define PCIE_DEV_PRINTF(dev, fmt, ...) \
41 PCIE_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__)
43 #define PCI_ERR_SRC_COR_OFFS 0
44 #define PCI_ERR_SRC_UNCOR_OFFS 2
46 typedef struct PCIEErrorDetails {
53 /* From 6.2.7 Error Listing and Rules. Table 6-2, 6-3 and 6-4 */
54 static uint32_t pcie_aer_uncor_default_severity(uint32_t status)
57 case PCI_ERR_UNC_INTN:
60 case PCI_ERR_UNC_RX_OVER:
62 case PCI_ERR_UNC_MALF_TLP:
63 return PCI_ERR_ROOT_CMD_FATAL_EN;
64 case PCI_ERR_UNC_POISON_TLP:
65 case PCI_ERR_UNC_ECRC:
66 case PCI_ERR_UNC_UNSUP:
67 case PCI_ERR_UNC_COMP_TIME:
68 case PCI_ERR_UNC_COMP_ABORT:
69 case PCI_ERR_UNC_UNX_COMP:
70 case PCI_ERR_UNC_ACSV:
71 case PCI_ERR_UNC_MCBTLP:
72 case PCI_ERR_UNC_ATOP_EBLOCKED:
73 case PCI_ERR_UNC_TLP_PRF_BLOCKED:
74 return PCI_ERR_ROOT_CMD_NONFATAL_EN;
79 return PCI_ERR_ROOT_CMD_FATAL_EN;
82 static int aer_log_add_err(PCIEAERLog *aer_log, const PCIEAERErr *err)
84 if (aer_log->log_num == aer_log->log_max) {
87 memcpy(&aer_log->log[aer_log->log_num], err, sizeof *err);
92 static void aer_log_del_err(PCIEAERLog *aer_log, PCIEAERErr *err)
94 assert(aer_log->log_num);
95 *err = aer_log->log[0];
97 memmove(&aer_log->log[0], &aer_log->log[1],
98 aer_log->log_num * sizeof *err);
101 static void aer_log_clear_all_err(PCIEAERLog *aer_log)
103 aer_log->log_num = 0;
106 int pcie_aer_init(PCIDevice *dev, uint8_t cap_ver, uint16_t offset,
107 uint16_t size, Error **errp)
109 pcie_add_capability(dev, PCI_EXT_CAP_ID_ERR, cap_ver,
111 dev->exp.aer_cap = offset;
113 /* clip down the value to avoid unreasonable memory usage */
114 if (dev->exp.aer_log.log_max > PCIE_AER_LOG_MAX_LIMIT) {
115 error_setg(errp, "Invalid aer_log_max %d. The max number of aer log "
116 "is %d", dev->exp.aer_log.log_max, PCIE_AER_LOG_MAX_LIMIT);
119 dev->exp.aer_log.log = g_malloc0(sizeof dev->exp.aer_log.log[0] *
120 dev->exp.aer_log.log_max);
122 pci_set_long(dev->w1cmask + offset + PCI_ERR_UNCOR_STATUS,
123 PCI_ERR_UNC_SUPPORTED);
125 pci_set_long(dev->config + offset + PCI_ERR_UNCOR_SEVER,
126 PCI_ERR_UNC_SEVERITY_DEFAULT);
127 pci_set_long(dev->wmask + offset + PCI_ERR_UNCOR_SEVER,
128 PCI_ERR_UNC_SUPPORTED);
130 pci_long_test_and_set_mask(dev->w1cmask + offset + PCI_ERR_COR_STATUS,
131 PCI_ERR_COR_SUPPORTED);
133 pci_set_long(dev->config + offset + PCI_ERR_COR_MASK,
134 PCI_ERR_COR_MASK_DEFAULT);
135 pci_set_long(dev->wmask + offset + PCI_ERR_COR_MASK,
136 PCI_ERR_COR_SUPPORTED);
138 /* capabilities and control. multiple header logging is supported */
139 if (dev->exp.aer_log.log_max > 0) {
140 pci_set_long(dev->config + offset + PCI_ERR_CAP,
141 PCI_ERR_CAP_ECRC_GENC | PCI_ERR_CAP_ECRC_CHKC |
143 pci_set_long(dev->wmask + offset + PCI_ERR_CAP,
144 PCI_ERR_CAP_ECRC_GENE | PCI_ERR_CAP_ECRC_CHKE |
147 pci_set_long(dev->config + offset + PCI_ERR_CAP,
148 PCI_ERR_CAP_ECRC_GENC | PCI_ERR_CAP_ECRC_CHKC);
149 pci_set_long(dev->wmask + offset + PCI_ERR_CAP,
150 PCI_ERR_CAP_ECRC_GENE | PCI_ERR_CAP_ECRC_CHKE);
153 switch (pcie_cap_get_type(dev)) {
154 case PCI_EXP_TYPE_ROOT_PORT:
155 /* this case will be set by pcie_aer_root_init() */
157 case PCI_EXP_TYPE_DOWNSTREAM:
158 case PCI_EXP_TYPE_UPSTREAM:
159 pci_word_test_and_set_mask(dev->wmask + PCI_BRIDGE_CONTROL,
160 PCI_BRIDGE_CTL_SERR);
161 pci_long_test_and_set_mask(dev->w1cmask + PCI_STATUS,
162 PCI_SEC_STATUS_RCV_SYSTEM_ERROR);
171 void pcie_aer_exit(PCIDevice *dev)
173 g_free(dev->exp.aer_log.log);
176 static void pcie_aer_update_uncor_status(PCIDevice *dev)
178 uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
179 PCIEAERLog *aer_log = &dev->exp.aer_log;
182 for (i = 0; i < aer_log->log_num; i++) {
183 pci_long_test_and_set_mask(aer_cap + PCI_ERR_UNCOR_STATUS,
184 dev->exp.aer_log.log[i].status);
190 * true: error message needs to be sent up
191 * false: error message is masked
193 * 6.2.6 Error Message Control
195 * all pci express devices part
198 pcie_aer_msg_alldev(PCIDevice *dev, const PCIEAERMsg *msg)
200 if (!(pcie_aer_msg_is_uncor(msg) &&
201 (pci_get_word(dev->config + PCI_COMMAND) & PCI_COMMAND_SERR))) {
205 /* Signaled System Error
207 * 7.5.1.1 Command register
210 * When Set, this bit enables reporting of Non-fatal and Fatal
211 * errors detected by the Function to the Root Complex. Note that
212 * errors are reported if enabled either through this bit or through
213 * the PCI Express specific bits in the Device Control register (see
216 pci_word_test_and_set_mask(dev->config + PCI_STATUS,
217 PCI_STATUS_SIG_SYSTEM_ERROR);
219 if (!(msg->severity &
220 pci_get_word(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL))) {
224 /* send up error message */
230 * true: error message is sent up
231 * false: error message is masked
233 * 6.2.6 Error Message Control
235 * virtual pci bridge part
237 static bool pcie_aer_msg_vbridge(PCIDevice *dev, const PCIEAERMsg *msg)
239 uint16_t bridge_control = pci_get_word(dev->config + PCI_BRIDGE_CONTROL);
241 if (pcie_aer_msg_is_uncor(msg)) {
242 /* Received System Error */
243 pci_word_test_and_set_mask(dev->config + PCI_SEC_STATUS,
244 PCI_SEC_STATUS_RCV_SYSTEM_ERROR);
247 if (!(bridge_control & PCI_BRIDGE_CTL_SERR)) {
253 void pcie_aer_root_set_vector(PCIDevice *dev, unsigned int vector)
255 uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
256 assert(vector < PCI_ERR_ROOT_IRQ_MAX);
257 pci_long_test_and_clear_mask(aer_cap + PCI_ERR_ROOT_STATUS,
259 pci_long_test_and_set_mask(aer_cap + PCI_ERR_ROOT_STATUS,
260 vector << PCI_ERR_ROOT_IRQ_SHIFT);
263 static unsigned int pcie_aer_root_get_vector(PCIDevice *dev)
265 uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
266 uint32_t root_status = pci_get_long(aer_cap + PCI_ERR_ROOT_STATUS);
267 return (root_status & PCI_ERR_ROOT_IRQ) >> PCI_ERR_ROOT_IRQ_SHIFT;
270 /* Given a status register, get corresponding bits in the command register */
271 static uint32_t pcie_aer_status_to_cmd(uint32_t status)
274 if (status & PCI_ERR_ROOT_COR_RCV) {
275 cmd |= PCI_ERR_ROOT_CMD_COR_EN;
277 if (status & PCI_ERR_ROOT_NONFATAL_RCV) {
278 cmd |= PCI_ERR_ROOT_CMD_NONFATAL_EN;
280 if (status & PCI_ERR_ROOT_FATAL_RCV) {
281 cmd |= PCI_ERR_ROOT_CMD_FATAL_EN;
286 static void pcie_aer_root_notify(PCIDevice *dev)
288 if (msix_enabled(dev)) {
289 msix_notify(dev, pcie_aer_root_get_vector(dev));
290 } else if (msi_enabled(dev)) {
291 msi_notify(dev, pcie_aer_root_get_vector(dev));
298 * 6.2.6 Error Message Control
302 static void pcie_aer_msg_root_port(PCIDevice *dev, const PCIEAERMsg *msg)
307 uint32_t root_status, prev_status;
309 cmd = pci_get_word(dev->config + PCI_COMMAND);
310 aer_cap = dev->config + dev->exp.aer_cap;
311 root_cmd = pci_get_long(aer_cap + PCI_ERR_ROOT_COMMAND);
312 prev_status = root_status = pci_get_long(aer_cap + PCI_ERR_ROOT_STATUS);
314 if (cmd & PCI_COMMAND_SERR) {
317 * The way to report System Error is platform specific and
318 * it isn't implemented in qemu right now.
319 * So just discard the error for now.
320 * OS which cares of aer would receive errors via
321 * native aer mechanims, so this wouldn't matter.
325 /* Errro Message Received: Root Error Status register */
326 switch (msg->severity) {
327 case PCI_ERR_ROOT_CMD_COR_EN:
328 if (root_status & PCI_ERR_ROOT_COR_RCV) {
329 root_status |= PCI_ERR_ROOT_MULTI_COR_RCV;
331 pci_set_word(aer_cap + PCI_ERR_ROOT_ERR_SRC + PCI_ERR_SRC_COR_OFFS,
334 root_status |= PCI_ERR_ROOT_COR_RCV;
336 case PCI_ERR_ROOT_CMD_NONFATAL_EN:
337 root_status |= PCI_ERR_ROOT_NONFATAL_RCV;
339 case PCI_ERR_ROOT_CMD_FATAL_EN:
340 if (!(root_status & PCI_ERR_ROOT_UNCOR_RCV)) {
341 root_status |= PCI_ERR_ROOT_FIRST_FATAL;
343 root_status |= PCI_ERR_ROOT_FATAL_RCV;
349 if (pcie_aer_msg_is_uncor(msg)) {
350 if (root_status & PCI_ERR_ROOT_UNCOR_RCV) {
351 root_status |= PCI_ERR_ROOT_MULTI_UNCOR_RCV;
353 pci_set_word(aer_cap + PCI_ERR_ROOT_ERR_SRC +
354 PCI_ERR_SRC_UNCOR_OFFS, msg->source_id);
356 root_status |= PCI_ERR_ROOT_UNCOR_RCV;
358 pci_set_long(aer_cap + PCI_ERR_ROOT_STATUS, root_status);
360 /* 6.2.4.1.2 Interrupt Generation */
361 /* All the above did was set some bits in the status register.
362 * Specifically these that match message severity.
363 * The below code relies on this fact. */
364 if (!(root_cmd & msg->severity) ||
365 (pcie_aer_status_to_cmd(prev_status) & root_cmd)) {
366 /* Condition is not being set or was already true so nothing to do. */
370 pcie_aer_root_notify(dev);
374 * 6.2.6 Error Message Control Figure 6-3
376 * Walk up the bus tree from the device, propagate the error message.
378 static void pcie_aer_msg(PCIDevice *dev, const PCIEAERMsg *msg)
383 if (!pci_is_express(dev)) {
385 /* TODO: Shouldn't we set PCI_STATUS_SIG_SYSTEM_ERROR?
386 * Consider e.g. a PCI bridge above a PCI Express device. */
390 type = pcie_cap_get_type(dev);
391 if ((type == PCI_EXP_TYPE_ROOT_PORT ||
392 type == PCI_EXP_TYPE_UPSTREAM ||
393 type == PCI_EXP_TYPE_DOWNSTREAM) &&
394 !pcie_aer_msg_vbridge(dev, msg)) {
397 if (!pcie_aer_msg_alldev(dev, msg)) {
400 if (type == PCI_EXP_TYPE_ROOT_PORT) {
401 pcie_aer_msg_root_port(dev, msg);
402 /* Root port can notify system itself,
403 or send the error message to root complex event collector. */
405 * if root port is associated with an event collector,
406 * return the root complex event collector here.
407 * For now root complex event collector isn't supported.
411 dev = pci_bridge_get_device(pci_get_bus(dev));
415 static void pcie_aer_update_log(PCIDevice *dev, const PCIEAERErr *err)
417 uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
418 uint8_t first_bit = ctz32(err->status);
419 uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
423 assert(!(err->status & (err->status - 1)));
425 errcap &= ~(PCI_ERR_CAP_FEP_MASK | PCI_ERR_CAP_TLP);
426 errcap |= PCI_ERR_CAP_FEP(first_bit);
428 if (err->flags & PCIE_AER_ERR_HEADER_VALID) {
429 for (i = 0; i < ARRAY_SIZE(err->header); ++i) {
430 /* 7.10.8 Header Log Register */
431 uint8_t *header_log =
432 aer_cap + PCI_ERR_HEADER_LOG + i * sizeof err->header[0];
433 stl_be_p(header_log, err->header[i]);
436 assert(!(err->flags & PCIE_AER_ERR_TLP_PREFIX_PRESENT));
437 memset(aer_cap + PCI_ERR_HEADER_LOG, 0, PCI_ERR_HEADER_LOG_SIZE);
440 if ((err->flags & PCIE_AER_ERR_TLP_PREFIX_PRESENT) &&
441 (pci_get_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCAP2) &
442 PCI_EXP_DEVCAP2_EETLPP)) {
443 for (i = 0; i < ARRAY_SIZE(err->prefix); ++i) {
444 /* 7.10.12 tlp prefix log register */
445 uint8_t *prefix_log =
446 aer_cap + PCI_ERR_TLP_PREFIX_LOG + i * sizeof err->prefix[0];
447 stl_be_p(prefix_log, err->prefix[i]);
449 errcap |= PCI_ERR_CAP_TLP;
451 memset(aer_cap + PCI_ERR_TLP_PREFIX_LOG, 0,
452 PCI_ERR_TLP_PREFIX_LOG_SIZE);
454 pci_set_long(aer_cap + PCI_ERR_CAP, errcap);
457 static void pcie_aer_clear_log(PCIDevice *dev)
459 uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
461 pci_long_test_and_clear_mask(aer_cap + PCI_ERR_CAP,
462 PCI_ERR_CAP_FEP_MASK | PCI_ERR_CAP_TLP);
464 memset(aer_cap + PCI_ERR_HEADER_LOG, 0, PCI_ERR_HEADER_LOG_SIZE);
465 memset(aer_cap + PCI_ERR_TLP_PREFIX_LOG, 0, PCI_ERR_TLP_PREFIX_LOG_SIZE);
468 static void pcie_aer_clear_error(PCIDevice *dev)
470 uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
471 uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
472 PCIEAERLog *aer_log = &dev->exp.aer_log;
475 if (!(errcap & PCI_ERR_CAP_MHRE) || !aer_log->log_num) {
476 pcie_aer_clear_log(dev);
481 * If more errors are queued, set corresponding bits in uncorrectable
483 * We emulate uncorrectable error status register as W1CS.
484 * So set bit in uncorrectable error status here again for multiple
485 * error recording support.
487 * 6.2.4.2 Multiple Error Handling(Advanced Error Reporting Capability)
489 pcie_aer_update_uncor_status(dev);
491 aer_log_del_err(aer_log, &err);
492 pcie_aer_update_log(dev, &err);
495 static int pcie_aer_record_error(PCIDevice *dev,
496 const PCIEAERErr *err)
498 uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
499 uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
500 int fep = PCI_ERR_CAP_FEP(errcap);
503 assert(!(err->status & (err->status - 1)));
505 if (errcap & PCI_ERR_CAP_MHRE &&
506 (pci_get_long(aer_cap + PCI_ERR_UNCOR_STATUS) & (1U << fep))) {
507 /* Not first error. queue error */
508 if (aer_log_add_err(&dev->exp.aer_log, err) < 0) {
515 pcie_aer_update_log(dev, err);
519 typedef struct PCIEAERInject {
522 const PCIEAERErr *err;
525 uint32_t error_status;
526 bool unsupported_request;
531 static bool pcie_aer_inject_cor_error(PCIEAERInject *inj,
532 uint32_t uncor_status,
533 bool is_advisory_nonfatal)
535 PCIDevice *dev = inj->dev;
537 inj->devsta |= PCI_EXP_DEVSTA_CED;
538 if (inj->unsupported_request) {
539 inj->devsta |= PCI_EXP_DEVSTA_URD;
541 pci_set_word(dev->config + dev->exp.exp_cap + PCI_EXP_DEVSTA, inj->devsta);
545 pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_COR_STATUS,
547 mask = pci_get_long(inj->aer_cap + PCI_ERR_COR_MASK);
548 if (mask & inj->error_status) {
551 if (is_advisory_nonfatal) {
552 uint32_t uncor_mask =
553 pci_get_long(inj->aer_cap + PCI_ERR_UNCOR_MASK);
554 if (!(uncor_mask & uncor_status)) {
555 inj->log_overflow = !!pcie_aer_record_error(dev, inj->err);
557 pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_UNCOR_STATUS,
562 if (inj->unsupported_request && !(inj->devctl & PCI_EXP_DEVCTL_URRE)) {
565 if (!(inj->devctl & PCI_EXP_DEVCTL_CERE)) {
569 inj->msg.severity = PCI_ERR_ROOT_CMD_COR_EN;
573 static bool pcie_aer_inject_uncor_error(PCIEAERInject *inj, bool is_fatal)
575 PCIDevice *dev = inj->dev;
579 inj->devsta |= PCI_EXP_DEVSTA_FED;
581 inj->devsta |= PCI_EXP_DEVSTA_NFED;
583 if (inj->unsupported_request) {
584 inj->devsta |= PCI_EXP_DEVSTA_URD;
586 pci_set_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVSTA, inj->devsta);
589 uint32_t mask = pci_get_long(inj->aer_cap + PCI_ERR_UNCOR_MASK);
590 if (mask & inj->error_status) {
591 pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_UNCOR_STATUS,
596 inj->log_overflow = !!pcie_aer_record_error(dev, inj->err);
597 pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_UNCOR_STATUS,
601 cmd = pci_get_word(dev->config + PCI_COMMAND);
602 if (inj->unsupported_request &&
603 !(inj->devctl & PCI_EXP_DEVCTL_URRE) && !(cmd & PCI_COMMAND_SERR)) {
607 if (!((cmd & PCI_COMMAND_SERR) ||
608 (inj->devctl & PCI_EXP_DEVCTL_FERE))) {
611 inj->msg.severity = PCI_ERR_ROOT_CMD_FATAL_EN;
613 if (!((cmd & PCI_COMMAND_SERR) ||
614 (inj->devctl & PCI_EXP_DEVCTL_NFERE))) {
617 inj->msg.severity = PCI_ERR_ROOT_CMD_NONFATAL_EN;
623 * non-Function specific error must be recorded in all functions.
624 * It is the responsibility of the caller of this function.
625 * It is also caller's responsibility to determine which function should
628 * 6.2.4 Error Logging
629 * 6.2.5 Sequence of Device Error Signaling and Logging Operations
630 * Figure 6-2: Flowchart Showing Sequence of Device Error Signaling and Logging
633 static int pcie_aer_inject_error(PCIDevice *dev, const PCIEAERErr *err)
635 uint8_t *aer_cap = NULL;
638 uint32_t error_status = err->status;
641 if (!pci_is_express(dev)) {
645 if (err->flags & PCIE_AER_ERR_IS_CORRECTABLE) {
646 error_status &= PCI_ERR_COR_SUPPORTED;
648 error_status &= PCI_ERR_UNC_SUPPORTED;
651 /* invalid status bit. one and only one bit must be set */
652 if (!error_status || (error_status & (error_status - 1))) {
656 if (dev->exp.aer_cap) {
657 uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
658 aer_cap = dev->config + dev->exp.aer_cap;
659 devctl = pci_get_long(exp_cap + PCI_EXP_DEVCTL);
660 devsta = pci_get_long(exp_cap + PCI_EXP_DEVSTA);
664 inj.aer_cap = aer_cap;
668 inj.error_status = error_status;
669 inj.unsupported_request = !(err->flags & PCIE_AER_ERR_IS_CORRECTABLE) &&
670 err->status == PCI_ERR_UNC_UNSUP;
671 inj.log_overflow = false;
673 if (err->flags & PCIE_AER_ERR_IS_CORRECTABLE) {
674 if (!pcie_aer_inject_cor_error(&inj, 0, false)) {
679 pcie_aer_uncor_default_severity(error_status) ==
680 PCI_ERR_ROOT_CMD_FATAL_EN;
683 error_status & pci_get_long(aer_cap + PCI_ERR_UNCOR_SEVER);
685 if (!is_fatal && (err->flags & PCIE_AER_ERR_MAYBE_ADVISORY)) {
686 inj.error_status = PCI_ERR_COR_ADV_NONFATAL;
687 if (!pcie_aer_inject_cor_error(&inj, error_status, true)) {
691 if (!pcie_aer_inject_uncor_error(&inj, is_fatal)) {
697 /* send up error message */
698 inj.msg.source_id = err->source_id;
699 pcie_aer_msg(dev, &inj.msg);
701 if (inj.log_overflow) {
702 PCIEAERErr header_log_overflow = {
703 .status = PCI_ERR_COR_HL_OVERFLOW,
704 .flags = PCIE_AER_ERR_IS_CORRECTABLE,
706 int ret = pcie_aer_inject_error(dev, &header_log_overflow);
712 void pcie_aer_write_config(PCIDevice *dev,
713 uint32_t addr, uint32_t val, int len)
715 uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
716 uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
717 uint32_t first_error = 1U << PCI_ERR_CAP_FEP(errcap);
718 uint32_t uncorsta = pci_get_long(aer_cap + PCI_ERR_UNCOR_STATUS);
720 /* uncorrectable error */
721 if (!(uncorsta & first_error)) {
722 /* the bit that corresponds to the first error is cleared */
723 pcie_aer_clear_error(dev);
724 } else if (errcap & PCI_ERR_CAP_MHRE) {
725 /* When PCI_ERR_CAP_MHRE is enabled and the first error isn't cleared
726 * nothing should happen. So we have to revert the modification to
729 pcie_aer_update_uncor_status(dev);
731 /* capability & control
732 * PCI_ERR_CAP_MHRE might be cleared, so clear of header log.
734 aer_log_clear_all_err(&dev->exp.aer_log);
738 void pcie_aer_root_init(PCIDevice *dev)
740 uint16_t pos = dev->exp.aer_cap;
742 pci_set_long(dev->wmask + pos + PCI_ERR_ROOT_COMMAND,
743 PCI_ERR_ROOT_CMD_EN_MASK);
744 pci_set_long(dev->w1cmask + pos + PCI_ERR_ROOT_STATUS,
745 PCI_ERR_ROOT_STATUS_REPORT_MASK);
746 /* PCI_ERR_ROOT_IRQ is RO but devices change it using a
747 * device-specific method.
749 pci_set_long(dev->cmask + pos + PCI_ERR_ROOT_STATUS,
753 void pcie_aer_root_reset(PCIDevice *dev)
755 uint8_t* aer_cap = dev->config + dev->exp.aer_cap;
757 pci_set_long(aer_cap + PCI_ERR_ROOT_COMMAND, 0);
760 * Advanced Error Interrupt Message Number in Root Error Status Register
761 * must be updated by chip dependent code because it's chip dependent
762 * which number is used.
766 void pcie_aer_root_write_config(PCIDevice *dev,
767 uint32_t addr, uint32_t val, int len,
768 uint32_t root_cmd_prev)
770 uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
771 uint32_t root_status = pci_get_long(aer_cap + PCI_ERR_ROOT_STATUS);
772 uint32_t enabled_cmd = pcie_aer_status_to_cmd(root_status);
773 uint32_t root_cmd = pci_get_long(aer_cap + PCI_ERR_ROOT_COMMAND);
774 /* 6.2.4.1.2 Interrupt Generation */
775 if (!msix_enabled(dev) && !msi_enabled(dev)) {
776 pci_set_irq(dev, !!(root_cmd & enabled_cmd));
780 if ((root_cmd_prev & enabled_cmd) || !(root_cmd & enabled_cmd)) {
781 /* Send MSI on transition from false to true. */
785 pcie_aer_root_notify(dev);
788 static const VMStateDescription vmstate_pcie_aer_err = {
789 .name = "PCIE_AER_ERROR",
791 .minimum_version_id = 1,
792 .fields = (VMStateField[]) {
793 VMSTATE_UINT32(status, PCIEAERErr),
794 VMSTATE_UINT16(source_id, PCIEAERErr),
795 VMSTATE_UINT16(flags, PCIEAERErr),
796 VMSTATE_UINT32_ARRAY(header, PCIEAERErr, 4),
797 VMSTATE_UINT32_ARRAY(prefix, PCIEAERErr, 4),
798 VMSTATE_END_OF_LIST()
802 static bool pcie_aer_state_log_num_valid(void *opaque, int version_id)
804 PCIEAERLog *s = opaque;
806 return s->log_num <= s->log_max;
809 const VMStateDescription vmstate_pcie_aer_log = {
810 .name = "PCIE_AER_ERROR_LOG",
812 .minimum_version_id = 1,
813 .fields = (VMStateField[]) {
814 VMSTATE_UINT16(log_num, PCIEAERLog),
815 VMSTATE_UINT16_EQUAL(log_max, PCIEAERLog, NULL),
816 VMSTATE_VALIDATE("log_num <= log_max", pcie_aer_state_log_num_valid),
817 VMSTATE_STRUCT_VARRAY_POINTER_UINT16(log, PCIEAERLog, log_num,
818 vmstate_pcie_aer_err, PCIEAERErr),
819 VMSTATE_END_OF_LIST()
823 typedef struct PCIEAERErrorName {
830 * AER error name -> value conversion table
831 * This naming scheme is same to linux aer-injection tool.
833 static const struct PCIEAERErrorName pcie_aer_error_list[] = {
836 .val = PCI_ERR_UNC_DLP,
837 .correctable = false,
840 .val = PCI_ERR_UNC_SDN,
841 .correctable = false,
843 .name = "POISON_TLP",
844 .val = PCI_ERR_UNC_POISON_TLP,
845 .correctable = false,
848 .val = PCI_ERR_UNC_FCP,
849 .correctable = false,
852 .val = PCI_ERR_UNC_COMP_TIME,
853 .correctable = false,
855 .name = "COMP_ABORT",
856 .val = PCI_ERR_UNC_COMP_ABORT,
857 .correctable = false,
860 .val = PCI_ERR_UNC_UNX_COMP,
861 .correctable = false,
864 .val = PCI_ERR_UNC_RX_OVER,
865 .correctable = false,
868 .val = PCI_ERR_UNC_MALF_TLP,
869 .correctable = false,
872 .val = PCI_ERR_UNC_ECRC,
873 .correctable = false,
876 .val = PCI_ERR_UNC_UNSUP,
877 .correctable = false,
880 .val = PCI_ERR_UNC_ACSV,
881 .correctable = false,
884 .val = PCI_ERR_UNC_INTN,
885 .correctable = false,
888 .val = PCI_ERR_UNC_MCBTLP,
889 .correctable = false,
891 .name = "ATOP_EBLOCKED",
892 .val = PCI_ERR_UNC_ATOP_EBLOCKED,
893 .correctable = false,
895 .name = "TLP_PRF_BLOCKED",
896 .val = PCI_ERR_UNC_TLP_PRF_BLOCKED,
897 .correctable = false,
900 .val = PCI_ERR_COR_RCVR,
904 .val = PCI_ERR_COR_BAD_TLP,
908 .val = PCI_ERR_COR_BAD_DLLP,
912 .val = PCI_ERR_COR_REP_ROLL,
916 .val = PCI_ERR_COR_REP_TIMER,
919 .name = "ADV_NONFATAL",
920 .val = PCI_ERR_COR_ADV_NONFATAL,
924 .val = PCI_ERR_COR_INTERNAL,
927 .name = "HL_OVERFLOW",
928 .val = PCI_ERR_COR_HL_OVERFLOW,
933 static int pcie_aer_parse_error_string(const char *error_name,
934 uint32_t *status, bool *correctable)
938 for (i = 0; i < ARRAY_SIZE(pcie_aer_error_list); i++) {
939 const PCIEAERErrorName *e = &pcie_aer_error_list[i];
940 if (strcmp(error_name, e->name)) {
945 *correctable = e->correctable;
952 * Inject an error described by @qdict.
953 * On success, set @details to show where error was sent.
954 * Return negative errno if injection failed and a message was emitted.
956 static int do_pcie_aer_inject_error(Monitor *mon,
958 PCIEErrorDetails *details)
960 const char *id = qdict_get_str(qdict, "id");
961 const char *error_name;
962 uint32_t error_status;
968 ret = pci_qdev_find_device(id, &dev);
971 "id or pci device path is invalid or device not "
975 if (!pci_is_express(dev)) {
976 monitor_printf(mon, "the device doesn't support pci express. %s\n",
981 error_name = qdict_get_str(qdict, "error_status");
982 if (pcie_aer_parse_error_string(error_name, &error_status, &correctable)) {
984 error_status = strtoul(error_name, &e, 0);
985 correctable = qdict_get_try_bool(qdict, "correctable", false);
986 if (!e || *e != '\0') {
987 monitor_printf(mon, "invalid error status value. \"%s\"",
992 err.status = error_status;
993 err.source_id = pci_requester_id(dev);
997 err.flags |= PCIE_AER_ERR_IS_CORRECTABLE;
999 if (qdict_get_try_bool(qdict, "advisory_non_fatal", false)) {
1000 err.flags |= PCIE_AER_ERR_MAYBE_ADVISORY;
1002 if (qdict_haskey(qdict, "header0")) {
1003 err.flags |= PCIE_AER_ERR_HEADER_VALID;
1005 if (qdict_haskey(qdict, "prefix0")) {
1006 err.flags |= PCIE_AER_ERR_TLP_PREFIX_PRESENT;
1009 err.header[0] = qdict_get_try_int(qdict, "header0", 0);
1010 err.header[1] = qdict_get_try_int(qdict, "header1", 0);
1011 err.header[2] = qdict_get_try_int(qdict, "header2", 0);
1012 err.header[3] = qdict_get_try_int(qdict, "header3", 0);
1014 err.prefix[0] = qdict_get_try_int(qdict, "prefix0", 0);
1015 err.prefix[1] = qdict_get_try_int(qdict, "prefix1", 0);
1016 err.prefix[2] = qdict_get_try_int(qdict, "prefix2", 0);
1017 err.prefix[3] = qdict_get_try_int(qdict, "prefix3", 0);
1019 ret = pcie_aer_inject_error(dev, &err);
1021 monitor_printf(mon, "failed to inject error: %s\n",
1026 details->root_bus = pci_root_bus_path(dev);
1027 details->bus = pci_dev_bus_num(dev);
1028 details->devfn = dev->devfn;
1033 void hmp_pcie_aer_inject_error(Monitor *mon, const QDict *qdict)
1035 PCIEErrorDetails data;
1037 if (do_pcie_aer_inject_error(mon, qdict, &data) < 0) {
1041 monitor_printf(mon, "OK id: %s root bus: %s, bus: %x devfn: %x.%x\n",
1042 data.id, data.root_bus, data.bus,
1043 PCI_SLOT(data.devfn), PCI_FUNC(data.devfn));