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/>.
22 #include "qemu-objects.h"
24 #include "pci_bridge.h"
28 #include "pci_internals.h"
29 #include "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 /* From 6.2.7 Error Listing and Rules. Table 6-2, 6-3 and 6-4 */
42 static uint32_t pcie_aer_uncor_default_severity(uint32_t status
)
45 case PCI_ERR_UNC_INTN
:
48 case PCI_ERR_UNC_RX_OVER
:
50 case PCI_ERR_UNC_MALF_TLP
:
51 return PCI_ERR_ROOT_CMD_FATAL_EN
;
52 case PCI_ERR_UNC_POISON_TLP
:
53 case PCI_ERR_UNC_ECRC
:
54 case PCI_ERR_UNC_UNSUP
:
55 case PCI_ERR_UNC_COMP_TIME
:
56 case PCI_ERR_UNC_COMP_ABORT
:
57 case PCI_ERR_UNC_UNX_COMP
:
58 case PCI_ERR_UNC_ACSV
:
59 case PCI_ERR_UNC_MCBTLP
:
60 case PCI_ERR_UNC_ATOP_EBLOCKED
:
61 case PCI_ERR_UNC_TLP_PRF_BLOCKED
:
62 return PCI_ERR_ROOT_CMD_NONFATAL_EN
;
67 return PCI_ERR_ROOT_CMD_FATAL_EN
;
70 static int aer_log_add_err(PCIEAERLog
*aer_log
, const PCIEAERErr
*err
)
72 if (aer_log
->log_num
== aer_log
->log_max
) {
75 memcpy(&aer_log
->log
[aer_log
->log_num
], err
, sizeof *err
);
80 static void aer_log_del_err(PCIEAERLog
*aer_log
, PCIEAERErr
*err
)
82 assert(aer_log
->log_num
);
83 *err
= aer_log
->log
[0];
85 memmove(&aer_log
->log
[0], &aer_log
->log
[1],
86 aer_log
->log_num
* sizeof *err
);
89 static void aer_log_clear_all_err(PCIEAERLog
*aer_log
)
94 int pcie_aer_init(PCIDevice
*dev
, uint16_t offset
)
96 PCIExpressDevice
*exp
;
98 pcie_add_capability(dev
, PCI_EXT_CAP_ID_ERR
, PCI_ERR_VER
,
99 offset
, PCI_ERR_SIZEOF
);
101 exp
->aer_cap
= offset
;
103 /* log_max is property */
104 if (dev
->exp
.aer_log
.log_max
== PCIE_AER_LOG_MAX_UNSET
) {
105 dev
->exp
.aer_log
.log_max
= PCIE_AER_LOG_MAX_DEFAULT
;
107 /* clip down the value to avoid unreasobale memory usage */
108 if (dev
->exp
.aer_log
.log_max
> PCIE_AER_LOG_MAX_LIMIT
) {
111 dev
->exp
.aer_log
.log
= qemu_mallocz(sizeof dev
->exp
.aer_log
.log
[0] *
112 dev
->exp
.aer_log
.log_max
);
114 pci_set_long(dev
->w1cmask
+ offset
+ PCI_ERR_UNCOR_STATUS
,
115 PCI_ERR_UNC_SUPPORTED
);
117 pci_set_long(dev
->config
+ offset
+ PCI_ERR_UNCOR_SEVER
,
118 PCI_ERR_UNC_SEVERITY_DEFAULT
);
119 pci_set_long(dev
->wmask
+ offset
+ PCI_ERR_UNCOR_SEVER
,
120 PCI_ERR_UNC_SUPPORTED
);
122 pci_long_test_and_set_mask(dev
->w1cmask
+ offset
+ PCI_ERR_COR_STATUS
,
125 pci_set_long(dev
->config
+ offset
+ PCI_ERR_COR_MASK
,
126 PCI_ERR_COR_MASK_DEFAULT
);
127 pci_set_long(dev
->wmask
+ offset
+ PCI_ERR_COR_MASK
,
128 PCI_ERR_COR_SUPPORTED
);
130 /* capabilities and control. multiple header logging is supported */
131 if (dev
->exp
.aer_log
.log_max
> 0) {
132 pci_set_long(dev
->config
+ offset
+ PCI_ERR_CAP
,
133 PCI_ERR_CAP_ECRC_GENC
| PCI_ERR_CAP_ECRC_CHKC
|
135 pci_set_long(dev
->wmask
+ offset
+ PCI_ERR_CAP
,
136 PCI_ERR_CAP_ECRC_GENE
| PCI_ERR_CAP_ECRC_CHKE
|
139 pci_set_long(dev
->config
+ offset
+ PCI_ERR_CAP
,
140 PCI_ERR_CAP_ECRC_GENC
| PCI_ERR_CAP_ECRC_CHKC
);
141 pci_set_long(dev
->wmask
+ offset
+ PCI_ERR_CAP
,
142 PCI_ERR_CAP_ECRC_GENE
| PCI_ERR_CAP_ECRC_CHKE
);
145 switch (pcie_cap_get_type(dev
)) {
146 case PCI_EXP_TYPE_ROOT_PORT
:
147 /* this case will be set by pcie_aer_root_init() */
149 case PCI_EXP_TYPE_DOWNSTREAM
:
150 case PCI_EXP_TYPE_UPSTREAM
:
151 pci_word_test_and_set_mask(dev
->wmask
+ PCI_BRIDGE_CONTROL
,
152 PCI_BRIDGE_CTL_SERR
);
153 pci_long_test_and_set_mask(dev
->w1cmask
+ PCI_STATUS
,
154 PCI_SEC_STATUS_RCV_SYSTEM_ERROR
);
163 void pcie_aer_exit(PCIDevice
*dev
)
165 qemu_free(dev
->exp
.aer_log
.log
);
168 static void pcie_aer_update_uncor_status(PCIDevice
*dev
)
170 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
171 PCIEAERLog
*aer_log
= &dev
->exp
.aer_log
;
174 for (i
= 0; i
< aer_log
->log_num
; i
++) {
175 pci_long_test_and_set_mask(aer_cap
+ PCI_ERR_UNCOR_STATUS
,
176 dev
->exp
.aer_log
.log
[i
].status
);
182 * true: error message needs to be sent up
183 * false: error message is masked
185 * 6.2.6 Error Message Control
187 * all pci express devices part
190 pcie_aer_msg_alldev(PCIDevice
*dev
, const PCIEAERMsg
*msg
)
192 if (!(pcie_aer_msg_is_uncor(msg
) &&
193 (pci_get_word(dev
->config
+ PCI_COMMAND
) & PCI_COMMAND_SERR
))) {
197 /* Signaled System Error
199 * 7.5.1.1 Command register
202 * When Set, this bit enables reporting of Non-fatal and Fatal
203 * errors detected by the Function to the Root Complex. Note that
204 * errors are reported if enabled either through this bit or through
205 * the PCI Express specific bits in the Device Control register (see
208 pci_word_test_and_set_mask(dev
->config
+ PCI_STATUS
,
209 PCI_STATUS_SIG_SYSTEM_ERROR
);
211 if (!(msg
->severity
&
212 pci_get_word(dev
->config
+ dev
->exp
.exp_cap
+ PCI_EXP_DEVCTL
))) {
216 /* send up error message */
222 * true: error message is sent up
223 * false: error message is masked
225 * 6.2.6 Error Message Control
227 * virtual pci bridge part
229 static bool pcie_aer_msg_vbridge(PCIDevice
*dev
, const PCIEAERMsg
*msg
)
231 uint16_t bridge_control
= pci_get_word(dev
->config
+ PCI_BRIDGE_CONTROL
);
233 if (pcie_aer_msg_is_uncor(msg
)) {
234 /* Received System Error */
235 pci_word_test_and_set_mask(dev
->config
+ PCI_SEC_STATUS
,
236 PCI_SEC_STATUS_RCV_SYSTEM_ERROR
);
239 if (!(bridge_control
& PCI_BRIDGE_CTL_SERR
)) {
245 void pcie_aer_root_set_vector(PCIDevice
*dev
, unsigned int vector
)
247 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
248 assert(vector
< PCI_ERR_ROOT_IRQ_MAX
);
249 pci_long_test_and_clear_mask(aer_cap
+ PCI_ERR_ROOT_STATUS
,
251 pci_long_test_and_set_mask(aer_cap
+ PCI_ERR_ROOT_STATUS
,
252 vector
<< PCI_ERR_ROOT_IRQ_SHIFT
);
255 static unsigned int pcie_aer_root_get_vector(PCIDevice
*dev
)
257 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
258 uint32_t root_status
= pci_get_long(aer_cap
+ PCI_ERR_ROOT_STATUS
);
259 return (root_status
& PCI_ERR_ROOT_IRQ
) >> PCI_ERR_ROOT_IRQ_SHIFT
;
262 /* Given a status register, get corresponding bits in the command register */
263 static uint32_t pcie_aer_status_to_cmd(uint32_t status
)
266 if (status
& PCI_ERR_ROOT_COR_RCV
) {
267 cmd
|= PCI_ERR_ROOT_CMD_COR_EN
;
269 if (status
& PCI_ERR_ROOT_NONFATAL_RCV
) {
270 cmd
|= PCI_ERR_ROOT_CMD_NONFATAL_EN
;
272 if (status
& PCI_ERR_ROOT_FATAL_RCV
) {
273 cmd
|= PCI_ERR_ROOT_CMD_FATAL_EN
;
278 static void pcie_aer_root_notify(PCIDevice
*dev
)
280 if (msix_enabled(dev
)) {
281 msix_notify(dev
, pcie_aer_root_get_vector(dev
));
282 } else if (msi_enabled(dev
)) {
283 msi_notify(dev
, pcie_aer_root_get_vector(dev
));
285 qemu_set_irq(dev
->irq
[dev
->exp
.aer_intx
], 1);
290 * 6.2.6 Error Message Control
294 static void pcie_aer_msg_root_port(PCIDevice
*dev
, const PCIEAERMsg
*msg
)
299 uint32_t root_status
, prev_status
;
301 cmd
= pci_get_word(dev
->config
+ PCI_COMMAND
);
302 aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
303 root_cmd
= pci_get_long(aer_cap
+ PCI_ERR_ROOT_COMMAND
);
304 prev_status
= root_status
= pci_get_long(aer_cap
+ PCI_ERR_ROOT_STATUS
);
306 if (cmd
& PCI_COMMAND_SERR
) {
309 * The way to report System Error is platform specific and
310 * it isn't implemented in qemu right now.
311 * So just discard the error for now.
312 * OS which cares of aer would receive errors via
313 * native aer mechanims, so this wouldn't matter.
317 /* Errro Message Received: Root Error Status register */
318 switch (msg
->severity
) {
319 case PCI_ERR_ROOT_CMD_COR_EN
:
320 if (root_status
& PCI_ERR_ROOT_COR_RCV
) {
321 root_status
|= PCI_ERR_ROOT_MULTI_COR_RCV
;
323 pci_set_word(aer_cap
+ PCI_ERR_ROOT_COR_SRC
, msg
->source_id
);
325 root_status
|= PCI_ERR_ROOT_COR_RCV
;
327 case PCI_ERR_ROOT_CMD_NONFATAL_EN
:
328 root_status
|= PCI_ERR_ROOT_NONFATAL_RCV
;
330 case PCI_ERR_ROOT_CMD_FATAL_EN
:
331 if (!(root_status
& PCI_ERR_ROOT_UNCOR_RCV
)) {
332 root_status
|= PCI_ERR_ROOT_FIRST_FATAL
;
334 root_status
|= PCI_ERR_ROOT_FATAL_RCV
;
340 if (pcie_aer_msg_is_uncor(msg
)) {
341 if (root_status
& PCI_ERR_ROOT_UNCOR_RCV
) {
342 root_status
|= PCI_ERR_ROOT_MULTI_UNCOR_RCV
;
344 pci_set_word(aer_cap
+ PCI_ERR_ROOT_SRC
, msg
->source_id
);
346 root_status
|= PCI_ERR_ROOT_UNCOR_RCV
;
348 pci_set_long(aer_cap
+ PCI_ERR_ROOT_STATUS
, root_status
);
350 /* 6.2.4.1.2 Interrupt Generation */
351 /* All the above did was set some bits in the status register.
352 * Specifically these that match message severity.
353 * The below code relies on this fact. */
354 if (!(root_cmd
& msg
->severity
) ||
355 (pcie_aer_status_to_cmd(prev_status
) & root_cmd
)) {
356 /* Condition is not being set or was already true so nothing to do. */
360 pcie_aer_root_notify(dev
);
364 * 6.2.6 Error Message Control Figure 6-3
366 * Walk up the bus tree from the device, propagate the error message.
368 static void pcie_aer_msg(PCIDevice
*dev
, const PCIEAERMsg
*msg
)
373 if (!pci_is_express(dev
)) {
375 /* TODO: Shouldn't we set PCI_STATUS_SIG_SYSTEM_ERROR?
376 * Consider e.g. a PCI bridge above a PCI Express device. */
380 type
= pcie_cap_get_type(dev
);
381 if ((type
== PCI_EXP_TYPE_ROOT_PORT
||
382 type
== PCI_EXP_TYPE_UPSTREAM
||
383 type
== PCI_EXP_TYPE_DOWNSTREAM
) &&
384 !pcie_aer_msg_vbridge(dev
, msg
)) {
387 if (!pcie_aer_msg_alldev(dev
, msg
)) {
390 if (type
== PCI_EXP_TYPE_ROOT_PORT
) {
391 pcie_aer_msg_root_port(dev
, msg
);
392 /* Root port can notify system itself,
393 or send the error message to root complex event collector. */
395 * if root port is associated with an event collector,
396 * return the root complex event collector here.
397 * For now root complex event collector isn't supported.
401 dev
= pci_bridge_get_device(dev
->bus
);
405 static void pcie_aer_update_log(PCIDevice
*dev
, const PCIEAERErr
*err
)
407 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
408 uint8_t first_bit
= ffs(err
->status
) - 1;
409 uint32_t errcap
= pci_get_long(aer_cap
+ PCI_ERR_CAP
);
413 assert(err
->status
& (err
->status
- 1));
415 errcap
&= ~(PCI_ERR_CAP_FEP_MASK
| PCI_ERR_CAP_TLP
);
416 errcap
|= PCI_ERR_CAP_FEP(first_bit
);
418 if (err
->flags
& PCIE_AER_ERR_HEADER_VALID
) {
419 for (i
= 0; i
< ARRAY_SIZE(err
->header
); ++i
) {
420 /* 7.10.8 Header Log Register */
421 uint8_t *header_log
=
422 aer_cap
+ PCI_ERR_HEADER_LOG
+ i
* sizeof err
->header
[0];
423 cpu_to_be32wu((uint32_t*)header_log
, err
->header
[i
]);
426 assert(!(err
->flags
& PCIE_AER_ERR_TLP_PREFIX_PRESENT
));
427 memset(aer_cap
+ PCI_ERR_HEADER_LOG
, 0, PCI_ERR_HEADER_LOG_SIZE
);
430 if ((err
->flags
& PCIE_AER_ERR_TLP_PREFIX_PRESENT
) &&
431 (pci_get_long(dev
->config
+ dev
->exp
.exp_cap
+ PCI_EXP_DEVCTL2
) &
432 PCI_EXP_DEVCAP2_EETLPP
)) {
433 for (i
= 0; i
< ARRAY_SIZE(err
->prefix
); ++i
) {
434 /* 7.10.12 tlp prefix log register */
435 uint8_t *prefix_log
=
436 aer_cap
+ PCI_ERR_TLP_PREFIX_LOG
+ i
* sizeof err
->prefix
[0];
437 cpu_to_be32wu((uint32_t*)prefix_log
, err
->prefix
[i
]);
439 errcap
|= PCI_ERR_CAP_TLP
;
441 memset(aer_cap
+ PCI_ERR_TLP_PREFIX_LOG
, 0,
442 PCI_ERR_TLP_PREFIX_LOG_SIZE
);
444 pci_set_long(aer_cap
+ PCI_ERR_CAP
, errcap
);
447 static void pcie_aer_clear_log(PCIDevice
*dev
)
449 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
451 pci_long_test_and_clear_mask(aer_cap
+ PCI_ERR_CAP
,
452 PCI_ERR_CAP_FEP_MASK
| PCI_ERR_CAP_TLP
);
454 memset(aer_cap
+ PCI_ERR_HEADER_LOG
, 0, PCI_ERR_HEADER_LOG_SIZE
);
455 memset(aer_cap
+ PCI_ERR_TLP_PREFIX_LOG
, 0, PCI_ERR_TLP_PREFIX_LOG_SIZE
);
458 static void pcie_aer_clear_error(PCIDevice
*dev
)
460 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
461 uint32_t errcap
= pci_get_long(aer_cap
+ PCI_ERR_CAP
);
462 PCIEAERLog
*aer_log
= &dev
->exp
.aer_log
;
465 if (!(errcap
& PCI_ERR_CAP_MHRE
) || !aer_log
->log_num
) {
466 pcie_aer_clear_log(dev
);
471 * If more errors are queued, set corresponding bits in uncorrectable
473 * We emulate uncorrectable error status register as W1CS.
474 * So set bit in uncorrectable error status here again for multiple
475 * error recording support.
477 * 6.2.4.2 Multiple Error Handling(Advanced Error Reporting Capability)
479 pcie_aer_update_uncor_status(dev
);
481 aer_log_del_err(aer_log
, &err
);
482 pcie_aer_update_log(dev
, &err
);
485 static int pcie_aer_record_error(PCIDevice
*dev
,
486 const PCIEAERErr
*err
)
488 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
489 uint32_t errcap
= pci_get_long(aer_cap
+ PCI_ERR_CAP
);
490 int fep
= PCI_ERR_CAP_FEP(errcap
);
493 assert(err
->status
& (err
->status
- 1));
495 if (errcap
& PCI_ERR_CAP_MHRE
&&
496 (pci_get_long(aer_cap
+ PCI_ERR_UNCOR_STATUS
) & (1U << fep
))) {
497 /* Not first error. queue error */
498 if (aer_log_add_err(&dev
->exp
.aer_log
, err
) < 0) {
505 pcie_aer_update_log(dev
, err
);
509 typedef struct PCIEAERInject
{
512 const PCIEAERErr
*err
;
515 uint32_t error_status
;
516 bool unsupported_request
;
521 static bool pcie_aer_inject_cor_error(PCIEAERInject
*inj
,
522 uint32_t uncor_status
,
523 bool is_advisory_nonfatal
)
525 PCIDevice
*dev
= inj
->dev
;
527 inj
->devsta
|= PCI_EXP_DEVSTA_CED
;
528 if (inj
->unsupported_request
) {
529 inj
->devsta
|= PCI_EXP_DEVSTA_URD
;
531 pci_set_word(dev
->config
+ dev
->exp
.exp_cap
+ PCI_EXP_DEVSTA
, inj
->devsta
);
535 pci_long_test_and_set_mask(inj
->aer_cap
+ PCI_ERR_COR_STATUS
,
537 mask
= pci_get_long(inj
->aer_cap
+ PCI_ERR_COR_MASK
);
538 if (mask
& inj
->error_status
) {
541 if (is_advisory_nonfatal
) {
542 uint32_t uncor_mask
=
543 pci_get_long(inj
->aer_cap
+ PCI_ERR_UNCOR_MASK
);
544 if (!(uncor_mask
& uncor_status
)) {
545 inj
->log_overflow
= !!pcie_aer_record_error(dev
, inj
->err
);
547 pci_long_test_and_set_mask(inj
->aer_cap
+ PCI_ERR_UNCOR_STATUS
,
552 if (inj
->unsupported_request
&& !(inj
->devctl
& PCI_EXP_DEVCTL_URRE
)) {
555 if (!(inj
->devctl
& PCI_EXP_DEVCTL_CERE
)) {
559 inj
->msg
.severity
= PCI_ERR_ROOT_CMD_COR_EN
;
563 static bool pcie_aer_inject_uncor_error(PCIEAERInject
*inj
, bool is_fatal
)
565 PCIDevice
*dev
= inj
->dev
;
569 inj
->devsta
|= PCI_EXP_DEVSTA_FED
;
571 inj
->devsta
|= PCI_EXP_DEVSTA_NFED
;
573 if (inj
->unsupported_request
) {
574 inj
->devsta
|= PCI_EXP_DEVSTA_URD
;
576 pci_set_long(dev
->config
+ dev
->exp
.exp_cap
+ PCI_EXP_DEVSTA
, inj
->devsta
);
579 uint32_t mask
= pci_get_long(inj
->aer_cap
+ PCI_ERR_UNCOR_MASK
);
580 if (mask
& inj
->error_status
) {
581 pci_long_test_and_set_mask(inj
->aer_cap
+ PCI_ERR_UNCOR_STATUS
,
586 inj
->log_overflow
= !!pcie_aer_record_error(dev
, inj
->err
);
587 pci_long_test_and_set_mask(inj
->aer_cap
+ PCI_ERR_UNCOR_STATUS
,
591 cmd
= pci_get_word(dev
->config
+ PCI_COMMAND
);
592 if (inj
->unsupported_request
&&
593 !(inj
->devctl
& PCI_EXP_DEVCTL_URRE
) && !(cmd
& PCI_COMMAND_SERR
)) {
597 if (!((cmd
& PCI_COMMAND_SERR
) ||
598 (inj
->devctl
& PCI_EXP_DEVCTL_FERE
))) {
601 inj
->msg
.severity
= PCI_ERR_ROOT_CMD_FATAL_EN
;
603 if (!((cmd
& PCI_COMMAND_SERR
) ||
604 (inj
->devctl
& PCI_EXP_DEVCTL_NFERE
))) {
607 inj
->msg
.severity
= PCI_ERR_ROOT_CMD_NONFATAL_EN
;
613 * non-Function specific error must be recorded in all functions.
614 * It is the responsibility of the caller of this function.
615 * It is also caller's responsiblity to determine which function should
618 * 6.2.4 Error Logging
619 * 6.2.5 Sqeunce of Device Error Signaling and Logging Operations
620 * table 6-2: Flowchard Showing Sequence of Device Error Signaling and Logging
623 int pcie_aer_inject_error(PCIDevice
*dev
, const PCIEAERErr
*err
)
625 uint8_t *aer_cap
= NULL
;
628 uint32_t error_status
= err
->status
;
631 if (!pci_is_express(dev
)) {
635 if (err
->flags
& PCIE_AER_ERR_IS_CORRECTABLE
) {
636 error_status
&= PCI_ERR_COR_SUPPORTED
;
638 error_status
&= PCI_ERR_UNC_SUPPORTED
;
641 /* invalid status bit. one and only one bit must be set */
642 if (!error_status
|| (error_status
& (error_status
- 1))) {
646 if (dev
->exp
.aer_cap
) {
647 uint8_t *exp_cap
= dev
->config
+ dev
->exp
.exp_cap
;
648 aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
649 devctl
= pci_get_long(exp_cap
+ PCI_EXP_DEVCTL
);
650 devsta
= pci_get_long(exp_cap
+ PCI_EXP_DEVSTA
);
654 inj
.aer_cap
= aer_cap
;
658 inj
.error_status
= error_status
;
659 inj
.unsupported_request
= !(err
->flags
& PCIE_AER_ERR_IS_CORRECTABLE
) &&
660 err
->status
== PCI_ERR_UNC_UNSUP
;
661 inj
.log_overflow
= false;
663 if (err
->flags
& PCIE_AER_ERR_IS_CORRECTABLE
) {
664 if (!pcie_aer_inject_cor_error(&inj
, 0, false)) {
669 pcie_aer_uncor_default_severity(error_status
) ==
670 PCI_ERR_ROOT_CMD_FATAL_EN
;
673 error_status
& pci_get_long(aer_cap
+ PCI_ERR_UNCOR_SEVER
);
675 if (!is_fatal
&& (err
->flags
& PCIE_AER_ERR_MAYBE_ADVISORY
)) {
676 inj
.error_status
= PCI_ERR_COR_ADV_NONFATAL
;
677 if (!pcie_aer_inject_cor_error(&inj
, error_status
, true)) {
681 if (!pcie_aer_inject_uncor_error(&inj
, is_fatal
)) {
687 /* send up error message */
688 inj
.msg
.source_id
= err
->source_id
;
689 pcie_aer_msg(dev
, &inj
.msg
);
691 if (inj
.log_overflow
) {
692 PCIEAERErr header_log_overflow
= {
693 .status
= PCI_ERR_COR_HL_OVERFLOW
,
694 .flags
= PCIE_AER_ERR_IS_CORRECTABLE
,
696 int ret
= pcie_aer_inject_error(dev
, &header_log_overflow
);
702 void pcie_aer_write_config(PCIDevice
*dev
,
703 uint32_t addr
, uint32_t val
, int len
)
705 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
706 uint32_t errcap
= pci_get_long(aer_cap
+ PCI_ERR_CAP
);
707 uint32_t first_error
= 1U << PCI_ERR_CAP_FEP(errcap
);
708 uint32_t uncorsta
= pci_get_long(aer_cap
+ PCI_ERR_UNCOR_STATUS
);
710 /* uncorrectable error */
711 if (!(uncorsta
& first_error
)) {
712 /* the bit that corresponds to the first error is cleared */
713 pcie_aer_clear_error(dev
);
714 } else if (errcap
& PCI_ERR_CAP_MHRE
) {
715 /* When PCI_ERR_CAP_MHRE is enabled and the first error isn't cleared
716 * nothing should happen. So we have to revert the modification to
719 pcie_aer_update_uncor_status(dev
);
721 /* capability & control
722 * PCI_ERR_CAP_MHRE might be cleared, so clear of header log.
724 aer_log_clear_all_err(&dev
->exp
.aer_log
);
728 void pcie_aer_root_init(PCIDevice
*dev
)
730 uint16_t pos
= dev
->exp
.aer_cap
;
732 pci_set_long(dev
->wmask
+ pos
+ PCI_ERR_ROOT_COMMAND
,
733 PCI_ERR_ROOT_CMD_EN_MASK
);
734 pci_set_long(dev
->w1cmask
+ pos
+ PCI_ERR_ROOT_STATUS
,
735 PCI_ERR_ROOT_STATUS_REPORT_MASK
);
738 void pcie_aer_root_reset(PCIDevice
*dev
)
740 uint8_t* aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
742 pci_set_long(aer_cap
+ PCI_ERR_ROOT_COMMAND
, 0);
745 * Advanced Error Interrupt Message Number in Root Error Status Register
746 * must be updated by chip dependent code because it's chip dependent
747 * which number is used.
751 void pcie_aer_root_write_config(PCIDevice
*dev
,
752 uint32_t addr
, uint32_t val
, int len
,
753 uint32_t root_cmd_prev
)
755 uint8_t *aer_cap
= dev
->config
+ dev
->exp
.aer_cap
;
756 uint32_t root_status
= pci_get_long(aer_cap
+ PCI_ERR_ROOT_STATUS
);
757 uint32_t enabled_cmd
= pcie_aer_status_to_cmd(root_status
);
758 uint32_t root_cmd
= pci_get_long(aer_cap
+ PCI_ERR_ROOT_COMMAND
);
759 /* 6.2.4.1.2 Interrupt Generation */
760 if (!msix_enabled(dev
) && !msi_enabled(dev
)) {
761 qemu_set_irq(dev
->irq
[dev
->exp
.aer_intx
], !!(root_cmd
& enabled_cmd
));
765 if ((root_cmd_prev
& enabled_cmd
) || !(root_cmd
& enabled_cmd
)) {
766 /* Send MSI on transition from false to true. */
770 pcie_aer_root_notify(dev
);
773 static const VMStateDescription vmstate_pcie_aer_err
= {
774 .name
= "PCIE_AER_ERROR",
776 .minimum_version_id
= 1,
777 .minimum_version_id_old
= 1,
778 .fields
= (VMStateField
[]) {
779 VMSTATE_UINT32(status
, PCIEAERErr
),
780 VMSTATE_UINT16(source_id
, PCIEAERErr
),
781 VMSTATE_UINT16(flags
, PCIEAERErr
),
782 VMSTATE_UINT32_ARRAY(header
, PCIEAERErr
, 4),
783 VMSTATE_UINT32_ARRAY(prefix
, PCIEAERErr
, 4),
784 VMSTATE_END_OF_LIST()
788 #define VMSTATE_PCIE_AER_ERRS(_field, _state, _field_num, _vmsd, _type) { \
789 .name = (stringify(_field)), \
791 .num_offset = vmstate_offset_value(_state, _field_num, uint16_t), \
792 .size = sizeof(_type), \
794 .flags = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT, \
795 .offset = vmstate_offset_pointer(_state, _field, _type), \
798 const VMStateDescription vmstate_pcie_aer_log
= {
799 .name
= "PCIE_AER_ERROR_LOG",
801 .minimum_version_id
= 1,
802 .minimum_version_id_old
= 1,
803 .fields
= (VMStateField
[]) {
804 VMSTATE_UINT16(log_num
, PCIEAERLog
),
805 VMSTATE_UINT16(log_max
, PCIEAERLog
),
806 VMSTATE_PCIE_AER_ERRS(log
, PCIEAERLog
, log_num
,
807 vmstate_pcie_aer_err
, PCIEAERErr
),
808 VMSTATE_END_OF_LIST()
812 void pcie_aer_inject_error_print(Monitor
*mon
, const QObject
*data
)
816 assert(qobject_type(data
) == QTYPE_QDICT
);
817 qdict
= qobject_to_qdict(data
);
819 devfn
= (int)qdict_get_int(qdict
, "devfn");
820 monitor_printf(mon
, "OK id: %s domain: %x, bus: %x devfn: %x.%x\n",
821 qdict_get_str(qdict
, "id"),
822 (int) qdict_get_int(qdict
, "domain"),
823 (int) qdict_get_int(qdict
, "bus"),
824 PCI_SLOT(devfn
), PCI_FUNC(devfn
));
827 typedef struct PCIEAERErrorName
{
834 * AER error name -> value convertion table
835 * This naming scheme is same to linux aer-injection tool.
837 static const struct PCIEAERErrorName pcie_aer_error_list
[] = {
840 .val
= PCI_ERR_UNC_TRAIN
,
841 .correctable
= false,
844 .val
= PCI_ERR_UNC_DLP
,
845 .correctable
= false,
848 .val
= PCI_ERR_UNC_SDN
,
849 .correctable
= false,
851 .name
= "POISON_TLP",
852 .val
= PCI_ERR_UNC_POISON_TLP
,
853 .correctable
= false,
856 .val
= PCI_ERR_UNC_FCP
,
857 .correctable
= false,
860 .val
= PCI_ERR_UNC_COMP_TIME
,
861 .correctable
= false,
863 .name
= "COMP_ABORT",
864 .val
= PCI_ERR_UNC_COMP_ABORT
,
865 .correctable
= false,
868 .val
= PCI_ERR_UNC_UNX_COMP
,
869 .correctable
= false,
872 .val
= PCI_ERR_UNC_RX_OVER
,
873 .correctable
= false,
876 .val
= PCI_ERR_UNC_MALF_TLP
,
877 .correctable
= false,
880 .val
= PCI_ERR_UNC_ECRC
,
881 .correctable
= false,
884 .val
= PCI_ERR_UNC_UNSUP
,
885 .correctable
= false,
888 .val
= PCI_ERR_UNC_ACSV
,
889 .correctable
= false,
892 .val
= PCI_ERR_UNC_INTN
,
893 .correctable
= false,
896 .val
= PCI_ERR_UNC_MCBTLP
,
897 .correctable
= false,
899 .name
= "ATOP_EBLOCKED",
900 .val
= PCI_ERR_UNC_ATOP_EBLOCKED
,
901 .correctable
= false,
903 .name
= "TLP_PRF_BLOCKED",
904 .val
= PCI_ERR_UNC_TLP_PRF_BLOCKED
,
905 .correctable
= false,
908 .val
= PCI_ERR_COR_RCVR
,
912 .val
= PCI_ERR_COR_BAD_TLP
,
916 .val
= PCI_ERR_COR_BAD_DLLP
,
920 .val
= PCI_ERR_COR_REP_ROLL
,
924 .val
= PCI_ERR_COR_REP_TIMER
,
927 .name
= "ADV_NONFATAL",
928 .val
= PCI_ERR_COR_ADV_NONFATAL
,
932 .val
= PCI_ERR_COR_INTERNAL
,
935 .name
= "HL_OVERFLOW",
936 .val
= PCI_ERR_COR_HL_OVERFLOW
,
941 static int pcie_aer_parse_error_string(const char *error_name
,
942 uint32_t *status
, bool *correctable
)
946 for (i
= 0; i
< ARRAY_SIZE(pcie_aer_error_list
); i
++) {
947 const PCIEAERErrorName
*e
= &pcie_aer_error_list
[i
];
948 if (strcmp(error_name
, e
->name
)) {
953 *correctable
= e
->correctable
;
959 int do_pcie_aer_inejct_error(Monitor
*mon
,
960 const QDict
*qdict
, QObject
**ret_data
)
962 const char *id
= qdict_get_str(qdict
, "id");
963 const char *error_name
;
964 uint32_t error_status
;
970 ret
= pci_qdev_find_device(id
, &dev
);
973 "id or pci device path is invalid or device not "
977 if (!pci_is_express(dev
)) {
978 monitor_printf(mon
, "the device doesn't support pci express. %s\n",
983 error_name
= qdict_get_str(qdict
, "error_status");
984 if (pcie_aer_parse_error_string(error_name
, &error_status
, &correctable
)) {
986 error_status
= strtoul(error_name
, &e
, 0);
987 correctable
= !!qdict_get_int(qdict
, "correctable");
988 if (!e
|| *e
!= '\0') {
989 monitor_printf(mon
, "invalid error status value. \"%s\"",
994 err
.source_id
= (pci_bus_num(dev
->bus
) << 8) | dev
->devfn
;
998 err
.flags
|= PCIE_AER_ERR_IS_CORRECTABLE
;
1000 if (qdict_get_int(qdict
, "advisory_non_fatal")) {
1001 err
.flags
|= PCIE_AER_ERR_MAYBE_ADVISORY
;
1003 if (qdict_haskey(qdict
, "header0")) {
1004 err
.flags
|= PCIE_AER_ERR_HEADER_VALID
;
1006 if (qdict_haskey(qdict
, "prefix0")) {
1007 err
.flags
|= PCIE_AER_ERR_TLP_PREFIX_PRESENT
;
1010 err
.header
[0] = qdict_get_try_int(qdict
, "header0", 0);
1011 err
.header
[1] = qdict_get_try_int(qdict
, "header1", 0);
1012 err
.header
[2] = qdict_get_try_int(qdict
, "header2", 0);
1013 err
.header
[3] = qdict_get_try_int(qdict
, "header3", 0);
1015 err
.prefix
[0] = qdict_get_try_int(qdict
, "prefix0", 0);
1016 err
.prefix
[1] = qdict_get_try_int(qdict
, "prefix1", 0);
1017 err
.prefix
[2] = qdict_get_try_int(qdict
, "prefix2", 0);
1018 err
.prefix
[3] = qdict_get_try_int(qdict
, "prefix3", 0);
1020 ret
= pcie_aer_inject_error(dev
, &err
);
1021 *ret_data
= qobject_from_jsonf("{'id': %s, "
1022 "'domain': %d, 'bus': %d, 'devfn': %d, "
1025 pci_find_domain(dev
->bus
),
1026 pci_bus_num(dev
->bus
), dev
->devfn
,