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 "sysemu/sysemu.h"
22 #include "qapi/qmp/types.h"
23 #include "monitor/monitor.h"
24 #include "hw/pci/pci_bridge.h"
25 #include "hw/pci/pcie.h"
26 #include "hw/pci/msix.h"
27 #include "hw/pci/msi.h"
28 #include "hw/pci/pci_bus.h"
29 #include "hw/pci/pcie_regs.h"
33 # define PCIE_DPRINTF(fmt, ...) \
34 fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__)
36 # define PCIE_DPRINTF(fmt, ...) do {} while (0)
38 #define PCIE_DEV_PRINTF(dev, fmt, ...) \
39 PCIE_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__)
41 #define PCI_ERR_SRC_COR_OFFS 0
42 #define PCI_ERR_SRC_UNCOR_OFFS 2
44 /* From 6.2.7 Error Listing and Rules. Table 6-2, 6-3 and 6-4 */
45 static uint32_t pcie_aer_uncor_default_severity(uint32_t status
)
48 case PCI_ERR_UNC_INTN
:
51 case PCI_ERR_UNC_RX_OVER
:
53 case PCI_ERR_UNC_MALF_TLP
:
54 return PCI_ERR_ROOT_CMD_FATAL_EN
;
55 case PCI_ERR_UNC_POISON_TLP
:
56 case PCI_ERR_UNC_ECRC
:
57 case PCI_ERR_UNC_UNSUP
:
58 case PCI_ERR_UNC_COMP_TIME
:
59 case PCI_ERR_UNC_COMP_ABORT
:
60 case PCI_ERR_UNC_UNX_COMP
:
61 case PCI_ERR_UNC_ACSV
:
62 case PCI_ERR_UNC_MCBTLP
:
63 case PCI_ERR_UNC_ATOP_EBLOCKED
:
64 case PCI_ERR_UNC_TLP_PRF_BLOCKED
:
65 return PCI_ERR_ROOT_CMD_NONFATAL_EN
;
70 return PCI_ERR_ROOT_CMD_FATAL_EN
;
73 static int aer_log_add_err(PCIEAERLog
*aer_log
, const PCIEAERErr
*err
)
75 if (aer_log
->log_num
== aer_log
->log_max
) {
78 memcpy(&aer_log
->log
[aer_log
->log_num
], err
, sizeof *err
);
83 static void aer_log_del_err(PCIEAERLog
*aer_log
, PCIEAERErr
*err
)
85 assert(aer_log
->log_num
);
86 *err
= aer_log
->log
[0];
88 memmove(&aer_log
->log
[0], &aer_log
->log
[1],
89 aer_log
->log_num
* sizeof *err
);
92 static void aer_log_clear_all_err(PCIEAERLog
*aer_log
)
97 int pcie_aer_init(PCIDevice
*dev
, uint16_t offset
)
99 PCIExpressDevice
*exp
;
101 pcie_add_capability(dev
, PCI_EXT_CAP_ID_ERR
, PCI_ERR_VER
,
102 offset
, PCI_ERR_SIZEOF
);
104 exp
->aer_cap
= offset
;
106 /* log_max is property */
107 if (dev
->exp
.aer_log
.log_max
== PCIE_AER_LOG_MAX_UNSET
) {
108 dev
->exp
.aer_log
.log_max
= PCIE_AER_LOG_MAX_DEFAULT
;
110 /* clip down the value to avoid unreasobale memory usage */
111 if (dev
->exp
.aer_log
.log_max
> PCIE_AER_LOG_MAX_LIMIT
) {
114 dev
->exp
.aer_log
.log
= g_malloc0(sizeof dev
->exp
.aer_log
.log
[0] *
115 dev
->exp
.aer_log
.log_max
);
117 pci_set_long(dev
->w1cmask
+ offset
+ PCI_ERR_UNCOR_STATUS
,
118 PCI_ERR_UNC_SUPPORTED
);
120 pci_set_long(dev
->config
+ offset
+ PCI_ERR_UNCOR_SEVER
,
121 PCI_ERR_UNC_SEVERITY_DEFAULT
);
122 pci_set_long(dev
->wmask
+ offset
+ PCI_ERR_UNCOR_SEVER
,
123 PCI_ERR_UNC_SUPPORTED
);
125 pci_long_test_and_set_mask(dev
->w1cmask
+ offset
+ PCI_ERR_COR_STATUS
,
126 PCI_ERR_COR_SUPPORTED
);
128 pci_set_long(dev
->config
+ offset
+ PCI_ERR_COR_MASK
,
129 PCI_ERR_COR_MASK_DEFAULT
);
130 pci_set_long(dev
->wmask
+ offset
+ PCI_ERR_COR_MASK
,
131 PCI_ERR_COR_SUPPORTED
);
133 /* capabilities and control. multiple header logging is supported */
134 if (dev
->exp
.aer_log
.log_max
> 0) {
135 pci_set_long(dev
->config
+ offset
+ PCI_ERR_CAP
,
136 PCI_ERR_CAP_ECRC_GENC
| PCI_ERR_CAP_ECRC_CHKC
|
138 pci_set_long(dev
->wmask
+ offset
+ PCI_ERR_CAP
,
139 PCI_ERR_CAP_ECRC_GENE
| PCI_ERR_CAP_ECRC_CHKE
|
142 pci_set_long(dev
->config
+ offset
+ PCI_ERR_CAP
,
143 PCI_ERR_CAP_ECRC_GENC
| PCI_ERR_CAP_ECRC_CHKC
);
144 pci_set_long(dev
->wmask
+ offset
+ PCI_ERR_CAP
,
145 PCI_ERR_CAP_ECRC_GENE
| PCI_ERR_CAP_ECRC_CHKE
);
148 switch (pcie_cap_get_type(dev
)) {
149 case PCI_EXP_TYPE_ROOT_PORT
:
150 /* this case will be set by pcie_aer_root_init() */
152 case PCI_EXP_TYPE_DOWNSTREAM
:
153 case PCI_EXP_TYPE_UPSTREAM
:
154 pci_word_test_and_set_mask(dev
->wmask
+ PCI_BRIDGE_CONTROL
,
155 PCI_BRIDGE_CTL_SERR
);
156 pci_long_test_and_set_mask(dev
->w1cmask
+ PCI_STATUS
,
157 PCI_SEC_STATUS_RCV_SYSTEM_ERROR
);
166 void pcie_aer_exit(PCIDevice
*dev
)
168 g_free(dev
->exp
.aer_log
.log
);
171 static void pcie_aer_update_uncor_status(PCIDevice
*dev
)
173 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
174 PCIEAERLog
*aer_log
= &dev
->exp
.aer_log
;
177 for (i
= 0; i
< aer_log
->log_num
; i
++) {
178 pci_long_test_and_set_mask(aer_cap
+ PCI_ERR_UNCOR_STATUS
,
179 dev
->exp
.aer_log
.log
[i
].status
);
185 * true: error message needs to be sent up
186 * false: error message is masked
188 * 6.2.6 Error Message Control
190 * all pci express devices part
193 pcie_aer_msg_alldev(PCIDevice
*dev
, const PCIEAERMsg
*msg
)
195 if (!(pcie_aer_msg_is_uncor(msg
) &&
196 (pci_get_word(dev
->config
+ PCI_COMMAND
) & PCI_COMMAND_SERR
))) {
200 /* Signaled System Error
202 * 7.5.1.1 Command register
205 * When Set, this bit enables reporting of Non-fatal and Fatal
206 * errors detected by the Function to the Root Complex. Note that
207 * errors are reported if enabled either through this bit or through
208 * the PCI Express specific bits in the Device Control register (see
211 pci_word_test_and_set_mask(dev
->config
+ PCI_STATUS
,
212 PCI_STATUS_SIG_SYSTEM_ERROR
);
214 if (!(msg
->severity
&
215 pci_get_word(dev
->config
+ dev
->exp
.exp_cap
+ PCI_EXP_DEVCTL
))) {
219 /* send up error message */
225 * true: error message is sent up
226 * false: error message is masked
228 * 6.2.6 Error Message Control
230 * virtual pci bridge part
232 static bool pcie_aer_msg_vbridge(PCIDevice
*dev
, const PCIEAERMsg
*msg
)
234 uint16_t bridge_control
= pci_get_word(dev
->config
+ PCI_BRIDGE_CONTROL
);
236 if (pcie_aer_msg_is_uncor(msg
)) {
237 /* Received System Error */
238 pci_word_test_and_set_mask(dev
->config
+ PCI_SEC_STATUS
,
239 PCI_SEC_STATUS_RCV_SYSTEM_ERROR
);
242 if (!(bridge_control
& PCI_BRIDGE_CTL_SERR
)) {
248 void pcie_aer_root_set_vector(PCIDevice
*dev
, unsigned int vector
)
250 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
251 assert(vector
< PCI_ERR_ROOT_IRQ_MAX
);
252 pci_long_test_and_clear_mask(aer_cap
+ PCI_ERR_ROOT_STATUS
,
254 pci_long_test_and_set_mask(aer_cap
+ PCI_ERR_ROOT_STATUS
,
255 vector
<< PCI_ERR_ROOT_IRQ_SHIFT
);
258 static unsigned int pcie_aer_root_get_vector(PCIDevice
*dev
)
260 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
261 uint32_t root_status
= pci_get_long(aer_cap
+ PCI_ERR_ROOT_STATUS
);
262 return (root_status
& PCI_ERR_ROOT_IRQ
) >> PCI_ERR_ROOT_IRQ_SHIFT
;
265 /* Given a status register, get corresponding bits in the command register */
266 static uint32_t pcie_aer_status_to_cmd(uint32_t status
)
269 if (status
& PCI_ERR_ROOT_COR_RCV
) {
270 cmd
|= PCI_ERR_ROOT_CMD_COR_EN
;
272 if (status
& PCI_ERR_ROOT_NONFATAL_RCV
) {
273 cmd
|= PCI_ERR_ROOT_CMD_NONFATAL_EN
;
275 if (status
& PCI_ERR_ROOT_FATAL_RCV
) {
276 cmd
|= PCI_ERR_ROOT_CMD_FATAL_EN
;
281 static void pcie_aer_root_notify(PCIDevice
*dev
)
283 if (msix_enabled(dev
)) {
284 msix_notify(dev
, pcie_aer_root_get_vector(dev
));
285 } else if (msi_enabled(dev
)) {
286 msi_notify(dev
, pcie_aer_root_get_vector(dev
));
293 * 6.2.6 Error Message Control
297 static void pcie_aer_msg_root_port(PCIDevice
*dev
, const PCIEAERMsg
*msg
)
302 uint32_t root_status
, prev_status
;
304 cmd
= pci_get_word(dev
->config
+ PCI_COMMAND
);
305 aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
306 root_cmd
= pci_get_long(aer_cap
+ PCI_ERR_ROOT_COMMAND
);
307 prev_status
= root_status
= pci_get_long(aer_cap
+ PCI_ERR_ROOT_STATUS
);
309 if (cmd
& PCI_COMMAND_SERR
) {
312 * The way to report System Error is platform specific and
313 * it isn't implemented in qemu right now.
314 * So just discard the error for now.
315 * OS which cares of aer would receive errors via
316 * native aer mechanims, so this wouldn't matter.
320 /* Errro Message Received: Root Error Status register */
321 switch (msg
->severity
) {
322 case PCI_ERR_ROOT_CMD_COR_EN
:
323 if (root_status
& PCI_ERR_ROOT_COR_RCV
) {
324 root_status
|= PCI_ERR_ROOT_MULTI_COR_RCV
;
326 pci_set_word(aer_cap
+ PCI_ERR_ROOT_ERR_SRC
+ PCI_ERR_SRC_COR_OFFS
,
329 root_status
|= PCI_ERR_ROOT_COR_RCV
;
331 case PCI_ERR_ROOT_CMD_NONFATAL_EN
:
332 root_status
|= PCI_ERR_ROOT_NONFATAL_RCV
;
334 case PCI_ERR_ROOT_CMD_FATAL_EN
:
335 if (!(root_status
& PCI_ERR_ROOT_UNCOR_RCV
)) {
336 root_status
|= PCI_ERR_ROOT_FIRST_FATAL
;
338 root_status
|= PCI_ERR_ROOT_FATAL_RCV
;
344 if (pcie_aer_msg_is_uncor(msg
)) {
345 if (root_status
& PCI_ERR_ROOT_UNCOR_RCV
) {
346 root_status
|= PCI_ERR_ROOT_MULTI_UNCOR_RCV
;
348 pci_set_word(aer_cap
+ PCI_ERR_ROOT_ERR_SRC
+
349 PCI_ERR_SRC_UNCOR_OFFS
, msg
->source_id
);
351 root_status
|= PCI_ERR_ROOT_UNCOR_RCV
;
353 pci_set_long(aer_cap
+ PCI_ERR_ROOT_STATUS
, root_status
);
355 /* 6.2.4.1.2 Interrupt Generation */
356 /* All the above did was set some bits in the status register.
357 * Specifically these that match message severity.
358 * The below code relies on this fact. */
359 if (!(root_cmd
& msg
->severity
) ||
360 (pcie_aer_status_to_cmd(prev_status
) & root_cmd
)) {
361 /* Condition is not being set or was already true so nothing to do. */
365 pcie_aer_root_notify(dev
);
369 * 6.2.6 Error Message Control Figure 6-3
371 * Walk up the bus tree from the device, propagate the error message.
373 static void pcie_aer_msg(PCIDevice
*dev
, const PCIEAERMsg
*msg
)
378 if (!pci_is_express(dev
)) {
380 /* TODO: Shouldn't we set PCI_STATUS_SIG_SYSTEM_ERROR?
381 * Consider e.g. a PCI bridge above a PCI Express device. */
385 type
= pcie_cap_get_type(dev
);
386 if ((type
== PCI_EXP_TYPE_ROOT_PORT
||
387 type
== PCI_EXP_TYPE_UPSTREAM
||
388 type
== PCI_EXP_TYPE_DOWNSTREAM
) &&
389 !pcie_aer_msg_vbridge(dev
, msg
)) {
392 if (!pcie_aer_msg_alldev(dev
, msg
)) {
395 if (type
== PCI_EXP_TYPE_ROOT_PORT
) {
396 pcie_aer_msg_root_port(dev
, msg
);
397 /* Root port can notify system itself,
398 or send the error message to root complex event collector. */
400 * if root port is associated with an event collector,
401 * return the root complex event collector here.
402 * For now root complex event collector isn't supported.
406 dev
= pci_bridge_get_device(dev
->bus
);
410 static void pcie_aer_update_log(PCIDevice
*dev
, const PCIEAERErr
*err
)
412 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
413 uint8_t first_bit
= ctz32(err
->status
);
414 uint32_t errcap
= pci_get_long(aer_cap
+ PCI_ERR_CAP
);
418 assert(!(err
->status
& (err
->status
- 1)));
420 errcap
&= ~(PCI_ERR_CAP_FEP_MASK
| PCI_ERR_CAP_TLP
);
421 errcap
|= PCI_ERR_CAP_FEP(first_bit
);
423 if (err
->flags
& PCIE_AER_ERR_HEADER_VALID
) {
424 for (i
= 0; i
< ARRAY_SIZE(err
->header
); ++i
) {
425 /* 7.10.8 Header Log Register */
426 uint8_t *header_log
=
427 aer_cap
+ PCI_ERR_HEADER_LOG
+ i
* sizeof err
->header
[0];
428 stl_be_p(header_log
, err
->header
[i
]);
431 assert(!(err
->flags
& PCIE_AER_ERR_TLP_PREFIX_PRESENT
));
432 memset(aer_cap
+ PCI_ERR_HEADER_LOG
, 0, PCI_ERR_HEADER_LOG_SIZE
);
435 if ((err
->flags
& PCIE_AER_ERR_TLP_PREFIX_PRESENT
) &&
436 (pci_get_long(dev
->config
+ dev
->exp
.exp_cap
+ PCI_EXP_DEVCAP2
) &
437 PCI_EXP_DEVCAP2_EETLPP
)) {
438 for (i
= 0; i
< ARRAY_SIZE(err
->prefix
); ++i
) {
439 /* 7.10.12 tlp prefix log register */
440 uint8_t *prefix_log
=
441 aer_cap
+ PCI_ERR_TLP_PREFIX_LOG
+ i
* sizeof err
->prefix
[0];
442 stl_be_p(prefix_log
, err
->prefix
[i
]);
444 errcap
|= PCI_ERR_CAP_TLP
;
446 memset(aer_cap
+ PCI_ERR_TLP_PREFIX_LOG
, 0,
447 PCI_ERR_TLP_PREFIX_LOG_SIZE
);
449 pci_set_long(aer_cap
+ PCI_ERR_CAP
, errcap
);
452 static void pcie_aer_clear_log(PCIDevice
*dev
)
454 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
456 pci_long_test_and_clear_mask(aer_cap
+ PCI_ERR_CAP
,
457 PCI_ERR_CAP_FEP_MASK
| PCI_ERR_CAP_TLP
);
459 memset(aer_cap
+ PCI_ERR_HEADER_LOG
, 0, PCI_ERR_HEADER_LOG_SIZE
);
460 memset(aer_cap
+ PCI_ERR_TLP_PREFIX_LOG
, 0, PCI_ERR_TLP_PREFIX_LOG_SIZE
);
463 static void pcie_aer_clear_error(PCIDevice
*dev
)
465 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
466 uint32_t errcap
= pci_get_long(aer_cap
+ PCI_ERR_CAP
);
467 PCIEAERLog
*aer_log
= &dev
->exp
.aer_log
;
470 if (!(errcap
& PCI_ERR_CAP_MHRE
) || !aer_log
->log_num
) {
471 pcie_aer_clear_log(dev
);
476 * If more errors are queued, set corresponding bits in uncorrectable
478 * We emulate uncorrectable error status register as W1CS.
479 * So set bit in uncorrectable error status here again for multiple
480 * error recording support.
482 * 6.2.4.2 Multiple Error Handling(Advanced Error Reporting Capability)
484 pcie_aer_update_uncor_status(dev
);
486 aer_log_del_err(aer_log
, &err
);
487 pcie_aer_update_log(dev
, &err
);
490 static int pcie_aer_record_error(PCIDevice
*dev
,
491 const PCIEAERErr
*err
)
493 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
494 uint32_t errcap
= pci_get_long(aer_cap
+ PCI_ERR_CAP
);
495 int fep
= PCI_ERR_CAP_FEP(errcap
);
498 assert(!(err
->status
& (err
->status
- 1)));
500 if (errcap
& PCI_ERR_CAP_MHRE
&&
501 (pci_get_long(aer_cap
+ PCI_ERR_UNCOR_STATUS
) & (1U << fep
))) {
502 /* Not first error. queue error */
503 if (aer_log_add_err(&dev
->exp
.aer_log
, err
) < 0) {
510 pcie_aer_update_log(dev
, err
);
514 typedef struct PCIEAERInject
{
517 const PCIEAERErr
*err
;
520 uint32_t error_status
;
521 bool unsupported_request
;
526 static bool pcie_aer_inject_cor_error(PCIEAERInject
*inj
,
527 uint32_t uncor_status
,
528 bool is_advisory_nonfatal
)
530 PCIDevice
*dev
= inj
->dev
;
532 inj
->devsta
|= PCI_EXP_DEVSTA_CED
;
533 if (inj
->unsupported_request
) {
534 inj
->devsta
|= PCI_EXP_DEVSTA_URD
;
536 pci_set_word(dev
->config
+ dev
->exp
.exp_cap
+ PCI_EXP_DEVSTA
, inj
->devsta
);
540 pci_long_test_and_set_mask(inj
->aer_cap
+ PCI_ERR_COR_STATUS
,
542 mask
= pci_get_long(inj
->aer_cap
+ PCI_ERR_COR_MASK
);
543 if (mask
& inj
->error_status
) {
546 if (is_advisory_nonfatal
) {
547 uint32_t uncor_mask
=
548 pci_get_long(inj
->aer_cap
+ PCI_ERR_UNCOR_MASK
);
549 if (!(uncor_mask
& uncor_status
)) {
550 inj
->log_overflow
= !!pcie_aer_record_error(dev
, inj
->err
);
552 pci_long_test_and_set_mask(inj
->aer_cap
+ PCI_ERR_UNCOR_STATUS
,
557 if (inj
->unsupported_request
&& !(inj
->devctl
& PCI_EXP_DEVCTL_URRE
)) {
560 if (!(inj
->devctl
& PCI_EXP_DEVCTL_CERE
)) {
564 inj
->msg
.severity
= PCI_ERR_ROOT_CMD_COR_EN
;
568 static bool pcie_aer_inject_uncor_error(PCIEAERInject
*inj
, bool is_fatal
)
570 PCIDevice
*dev
= inj
->dev
;
574 inj
->devsta
|= PCI_EXP_DEVSTA_FED
;
576 inj
->devsta
|= PCI_EXP_DEVSTA_NFED
;
578 if (inj
->unsupported_request
) {
579 inj
->devsta
|= PCI_EXP_DEVSTA_URD
;
581 pci_set_long(dev
->config
+ dev
->exp
.exp_cap
+ PCI_EXP_DEVSTA
, inj
->devsta
);
584 uint32_t mask
= pci_get_long(inj
->aer_cap
+ PCI_ERR_UNCOR_MASK
);
585 if (mask
& inj
->error_status
) {
586 pci_long_test_and_set_mask(inj
->aer_cap
+ PCI_ERR_UNCOR_STATUS
,
591 inj
->log_overflow
= !!pcie_aer_record_error(dev
, inj
->err
);
592 pci_long_test_and_set_mask(inj
->aer_cap
+ PCI_ERR_UNCOR_STATUS
,
596 cmd
= pci_get_word(dev
->config
+ PCI_COMMAND
);
597 if (inj
->unsupported_request
&&
598 !(inj
->devctl
& PCI_EXP_DEVCTL_URRE
) && !(cmd
& PCI_COMMAND_SERR
)) {
602 if (!((cmd
& PCI_COMMAND_SERR
) ||
603 (inj
->devctl
& PCI_EXP_DEVCTL_FERE
))) {
606 inj
->msg
.severity
= PCI_ERR_ROOT_CMD_FATAL_EN
;
608 if (!((cmd
& PCI_COMMAND_SERR
) ||
609 (inj
->devctl
& PCI_EXP_DEVCTL_NFERE
))) {
612 inj
->msg
.severity
= PCI_ERR_ROOT_CMD_NONFATAL_EN
;
618 * non-Function specific error must be recorded in all functions.
619 * It is the responsibility of the caller of this function.
620 * It is also caller's responsibility to determine which function should
623 * 6.2.4 Error Logging
624 * 6.2.5 Sequence of Device Error Signaling and Logging Operations
625 * Figure 6-2: Flowchart Showing Sequence of Device Error Signaling and Logging
628 int pcie_aer_inject_error(PCIDevice
*dev
, const PCIEAERErr
*err
)
630 uint8_t *aer_cap
= NULL
;
633 uint32_t error_status
= err
->status
;
636 if (!pci_is_express(dev
)) {
640 if (err
->flags
& PCIE_AER_ERR_IS_CORRECTABLE
) {
641 error_status
&= PCI_ERR_COR_SUPPORTED
;
643 error_status
&= PCI_ERR_UNC_SUPPORTED
;
646 /* invalid status bit. one and only one bit must be set */
647 if (!error_status
|| (error_status
& (error_status
- 1))) {
651 if (dev
->exp
.aer_cap
) {
652 uint8_t *exp_cap
= dev
->config
+ dev
->exp
.exp_cap
;
653 aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
654 devctl
= pci_get_long(exp_cap
+ PCI_EXP_DEVCTL
);
655 devsta
= pci_get_long(exp_cap
+ PCI_EXP_DEVSTA
);
659 inj
.aer_cap
= aer_cap
;
663 inj
.error_status
= error_status
;
664 inj
.unsupported_request
= !(err
->flags
& PCIE_AER_ERR_IS_CORRECTABLE
) &&
665 err
->status
== PCI_ERR_UNC_UNSUP
;
666 inj
.log_overflow
= false;
668 if (err
->flags
& PCIE_AER_ERR_IS_CORRECTABLE
) {
669 if (!pcie_aer_inject_cor_error(&inj
, 0, false)) {
674 pcie_aer_uncor_default_severity(error_status
) ==
675 PCI_ERR_ROOT_CMD_FATAL_EN
;
678 error_status
& pci_get_long(aer_cap
+ PCI_ERR_UNCOR_SEVER
);
680 if (!is_fatal
&& (err
->flags
& PCIE_AER_ERR_MAYBE_ADVISORY
)) {
681 inj
.error_status
= PCI_ERR_COR_ADV_NONFATAL
;
682 if (!pcie_aer_inject_cor_error(&inj
, error_status
, true)) {
686 if (!pcie_aer_inject_uncor_error(&inj
, is_fatal
)) {
692 /* send up error message */
693 inj
.msg
.source_id
= err
->source_id
;
694 pcie_aer_msg(dev
, &inj
.msg
);
696 if (inj
.log_overflow
) {
697 PCIEAERErr header_log_overflow
= {
698 .status
= PCI_ERR_COR_HL_OVERFLOW
,
699 .flags
= PCIE_AER_ERR_IS_CORRECTABLE
,
701 int ret
= pcie_aer_inject_error(dev
, &header_log_overflow
);
707 void pcie_aer_write_config(PCIDevice
*dev
,
708 uint32_t addr
, uint32_t val
, int len
)
710 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
711 uint32_t errcap
= pci_get_long(aer_cap
+ PCI_ERR_CAP
);
712 uint32_t first_error
= 1U << PCI_ERR_CAP_FEP(errcap
);
713 uint32_t uncorsta
= pci_get_long(aer_cap
+ PCI_ERR_UNCOR_STATUS
);
715 /* uncorrectable error */
716 if (!(uncorsta
& first_error
)) {
717 /* the bit that corresponds to the first error is cleared */
718 pcie_aer_clear_error(dev
);
719 } else if (errcap
& PCI_ERR_CAP_MHRE
) {
720 /* When PCI_ERR_CAP_MHRE is enabled and the first error isn't cleared
721 * nothing should happen. So we have to revert the modification to
724 pcie_aer_update_uncor_status(dev
);
726 /* capability & control
727 * PCI_ERR_CAP_MHRE might be cleared, so clear of header log.
729 aer_log_clear_all_err(&dev
->exp
.aer_log
);
733 void pcie_aer_root_init(PCIDevice
*dev
)
735 uint16_t pos
= dev
->exp
.aer_cap
;
737 pci_set_long(dev
->wmask
+ pos
+ PCI_ERR_ROOT_COMMAND
,
738 PCI_ERR_ROOT_CMD_EN_MASK
);
739 pci_set_long(dev
->w1cmask
+ pos
+ PCI_ERR_ROOT_STATUS
,
740 PCI_ERR_ROOT_STATUS_REPORT_MASK
);
741 /* PCI_ERR_ROOT_IRQ is RO but devices change it using a
742 * device-specific method.
744 pci_set_long(dev
->cmask
+ pos
+ PCI_ERR_ROOT_STATUS
,
748 void pcie_aer_root_reset(PCIDevice
*dev
)
750 uint8_t* aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
752 pci_set_long(aer_cap
+ PCI_ERR_ROOT_COMMAND
, 0);
755 * Advanced Error Interrupt Message Number in Root Error Status Register
756 * must be updated by chip dependent code because it's chip dependent
757 * which number is used.
761 void pcie_aer_root_write_config(PCIDevice
*dev
,
762 uint32_t addr
, uint32_t val
, int len
,
763 uint32_t root_cmd_prev
)
765 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
766 uint32_t root_status
= pci_get_long(aer_cap
+ PCI_ERR_ROOT_STATUS
);
767 uint32_t enabled_cmd
= pcie_aer_status_to_cmd(root_status
);
768 uint32_t root_cmd
= pci_get_long(aer_cap
+ PCI_ERR_ROOT_COMMAND
);
769 /* 6.2.4.1.2 Interrupt Generation */
770 if (!msix_enabled(dev
) && !msi_enabled(dev
)) {
771 pci_set_irq(dev
, !!(root_cmd
& enabled_cmd
));
775 if ((root_cmd_prev
& enabled_cmd
) || !(root_cmd
& enabled_cmd
)) {
776 /* Send MSI on transition from false to true. */
780 pcie_aer_root_notify(dev
);
783 static const VMStateDescription vmstate_pcie_aer_err
= {
784 .name
= "PCIE_AER_ERROR",
786 .minimum_version_id
= 1,
787 .fields
= (VMStateField
[]) {
788 VMSTATE_UINT32(status
, PCIEAERErr
),
789 VMSTATE_UINT16(source_id
, PCIEAERErr
),
790 VMSTATE_UINT16(flags
, PCIEAERErr
),
791 VMSTATE_UINT32_ARRAY(header
, PCIEAERErr
, 4),
792 VMSTATE_UINT32_ARRAY(prefix
, PCIEAERErr
, 4),
793 VMSTATE_END_OF_LIST()
797 static bool pcie_aer_state_log_num_valid(void *opaque
, int version_id
)
799 PCIEAERLog
*s
= opaque
;
801 return s
->log_num
<= s
->log_max
;
804 const VMStateDescription vmstate_pcie_aer_log
= {
805 .name
= "PCIE_AER_ERROR_LOG",
807 .minimum_version_id
= 1,
808 .fields
= (VMStateField
[]) {
809 VMSTATE_UINT16(log_num
, PCIEAERLog
),
810 VMSTATE_UINT16_EQUAL(log_max
, PCIEAERLog
),
811 VMSTATE_VALIDATE("log_num <= log_max", pcie_aer_state_log_num_valid
),
812 VMSTATE_STRUCT_VARRAY_POINTER_UINT16(log
, PCIEAERLog
, log_num
,
813 vmstate_pcie_aer_err
, PCIEAERErr
),
814 VMSTATE_END_OF_LIST()
818 typedef struct PCIEAERErrorName
{
825 * AER error name -> value conversion table
826 * This naming scheme is same to linux aer-injection tool.
828 static const struct PCIEAERErrorName pcie_aer_error_list
[] = {
831 .val
= PCI_ERR_UNC_DLP
,
832 .correctable
= false,
835 .val
= PCI_ERR_UNC_SDN
,
836 .correctable
= false,
838 .name
= "POISON_TLP",
839 .val
= PCI_ERR_UNC_POISON_TLP
,
840 .correctable
= false,
843 .val
= PCI_ERR_UNC_FCP
,
844 .correctable
= false,
847 .val
= PCI_ERR_UNC_COMP_TIME
,
848 .correctable
= false,
850 .name
= "COMP_ABORT",
851 .val
= PCI_ERR_UNC_COMP_ABORT
,
852 .correctable
= false,
855 .val
= PCI_ERR_UNC_UNX_COMP
,
856 .correctable
= false,
859 .val
= PCI_ERR_UNC_RX_OVER
,
860 .correctable
= false,
863 .val
= PCI_ERR_UNC_MALF_TLP
,
864 .correctable
= false,
867 .val
= PCI_ERR_UNC_ECRC
,
868 .correctable
= false,
871 .val
= PCI_ERR_UNC_UNSUP
,
872 .correctable
= false,
875 .val
= PCI_ERR_UNC_ACSV
,
876 .correctable
= false,
879 .val
= PCI_ERR_UNC_INTN
,
880 .correctable
= false,
883 .val
= PCI_ERR_UNC_MCBTLP
,
884 .correctable
= false,
886 .name
= "ATOP_EBLOCKED",
887 .val
= PCI_ERR_UNC_ATOP_EBLOCKED
,
888 .correctable
= false,
890 .name
= "TLP_PRF_BLOCKED",
891 .val
= PCI_ERR_UNC_TLP_PRF_BLOCKED
,
892 .correctable
= false,
895 .val
= PCI_ERR_COR_RCVR
,
899 .val
= PCI_ERR_COR_BAD_TLP
,
903 .val
= PCI_ERR_COR_BAD_DLLP
,
907 .val
= PCI_ERR_COR_REP_ROLL
,
911 .val
= PCI_ERR_COR_REP_TIMER
,
914 .name
= "ADV_NONFATAL",
915 .val
= PCI_ERR_COR_ADV_NONFATAL
,
919 .val
= PCI_ERR_COR_INTERNAL
,
922 .name
= "HL_OVERFLOW",
923 .val
= PCI_ERR_COR_HL_OVERFLOW
,
928 static int pcie_aer_parse_error_string(const char *error_name
,
929 uint32_t *status
, bool *correctable
)
933 for (i
= 0; i
< ARRAY_SIZE(pcie_aer_error_list
); i
++) {
934 const PCIEAERErrorName
*e
= &pcie_aer_error_list
[i
];
935 if (strcmp(error_name
, e
->name
)) {
940 *correctable
= e
->correctable
;
946 static int do_pcie_aer_inject_error(Monitor
*mon
,
947 const QDict
*qdict
, QObject
**ret_data
)
949 const char *id
= qdict_get_str(qdict
, "id");
950 const char *error_name
;
951 uint32_t error_status
;
957 ret
= pci_qdev_find_device(id
, &dev
);
960 "id or pci device path is invalid or device not "
964 if (!pci_is_express(dev
)) {
965 monitor_printf(mon
, "the device doesn't support pci express. %s\n",
970 error_name
= qdict_get_str(qdict
, "error_status");
971 if (pcie_aer_parse_error_string(error_name
, &error_status
, &correctable
)) {
973 error_status
= strtoul(error_name
, &e
, 0);
974 correctable
= qdict_get_try_bool(qdict
, "correctable", false);
975 if (!e
|| *e
!= '\0') {
976 monitor_printf(mon
, "invalid error status value. \"%s\"",
981 err
.status
= error_status
;
982 err
.source_id
= (pci_bus_num(dev
->bus
) << 8) | dev
->devfn
;
986 err
.flags
|= PCIE_AER_ERR_IS_CORRECTABLE
;
988 if (qdict_get_try_bool(qdict
, "advisory_non_fatal", false)) {
989 err
.flags
|= PCIE_AER_ERR_MAYBE_ADVISORY
;
991 if (qdict_haskey(qdict
, "header0")) {
992 err
.flags
|= PCIE_AER_ERR_HEADER_VALID
;
994 if (qdict_haskey(qdict
, "prefix0")) {
995 err
.flags
|= PCIE_AER_ERR_TLP_PREFIX_PRESENT
;
998 err
.header
[0] = qdict_get_try_int(qdict
, "header0", 0);
999 err
.header
[1] = qdict_get_try_int(qdict
, "header1", 0);
1000 err
.header
[2] = qdict_get_try_int(qdict
, "header2", 0);
1001 err
.header
[3] = qdict_get_try_int(qdict
, "header3", 0);
1003 err
.prefix
[0] = qdict_get_try_int(qdict
, "prefix0", 0);
1004 err
.prefix
[1] = qdict_get_try_int(qdict
, "prefix1", 0);
1005 err
.prefix
[2] = qdict_get_try_int(qdict
, "prefix2", 0);
1006 err
.prefix
[3] = qdict_get_try_int(qdict
, "prefix3", 0);
1008 ret
= pcie_aer_inject_error(dev
, &err
);
1009 *ret_data
= qobject_from_jsonf("{'id': %s, "
1010 "'root_bus': %s, 'bus': %d, 'devfn': %d, "
1012 id
, pci_root_bus_path(dev
),
1013 pci_bus_num(dev
->bus
), dev
->devfn
,
1020 void hmp_pcie_aer_inject_error(Monitor
*mon
, const QDict
*qdict
)
1025 if (do_pcie_aer_inject_error(mon
, qdict
, &data
) < 0) {
1029 assert(qobject_type(data
) == QTYPE_QDICT
);
1030 qdict
= qobject_to_qdict(data
);
1032 devfn
= (int)qdict_get_int(qdict
, "devfn");
1033 monitor_printf(mon
, "OK id: %s root bus: %s, bus: %x devfn: %x.%x\n",
1034 qdict_get_str(qdict
, "id"),
1035 qdict_get_str(qdict
, "root_bus"),
1036 (int) qdict_get_int(qdict
, "bus"),
1037 PCI_SLOT(devfn
), PCI_FUNC(devfn
));