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 const VMStateDescription vmstate_pcie_aer_log
= {
789 .name
= "PCIE_AER_ERROR_LOG",
791 .minimum_version_id
= 1,
792 .minimum_version_id_old
= 1,
793 .fields
= (VMStateField
[]) {
794 VMSTATE_UINT16(log_num
, PCIEAERLog
),
795 VMSTATE_UINT16(log_max
, PCIEAERLog
),
796 VMSTATE_STRUCT_VARRAY_POINTER_UINT16(log
, PCIEAERLog
, log_num
,
797 vmstate_pcie_aer_err
, PCIEAERErr
),
798 VMSTATE_END_OF_LIST()
802 void pcie_aer_inject_error_print(Monitor
*mon
, const QObject
*data
)
806 assert(qobject_type(data
) == QTYPE_QDICT
);
807 qdict
= qobject_to_qdict(data
);
809 devfn
= (int)qdict_get_int(qdict
, "devfn");
810 monitor_printf(mon
, "OK id: %s domain: %x, bus: %x devfn: %x.%x\n",
811 qdict_get_str(qdict
, "id"),
812 (int) qdict_get_int(qdict
, "domain"),
813 (int) qdict_get_int(qdict
, "bus"),
814 PCI_SLOT(devfn
), PCI_FUNC(devfn
));
817 typedef struct PCIEAERErrorName
{
824 * AER error name -> value convertion table
825 * This naming scheme is same to linux aer-injection tool.
827 static const struct PCIEAERErrorName pcie_aer_error_list
[] = {
830 .val
= PCI_ERR_UNC_TRAIN
,
831 .correctable
= false,
834 .val
= PCI_ERR_UNC_DLP
,
835 .correctable
= false,
838 .val
= PCI_ERR_UNC_SDN
,
839 .correctable
= false,
841 .name
= "POISON_TLP",
842 .val
= PCI_ERR_UNC_POISON_TLP
,
843 .correctable
= false,
846 .val
= PCI_ERR_UNC_FCP
,
847 .correctable
= false,
850 .val
= PCI_ERR_UNC_COMP_TIME
,
851 .correctable
= false,
853 .name
= "COMP_ABORT",
854 .val
= PCI_ERR_UNC_COMP_ABORT
,
855 .correctable
= false,
858 .val
= PCI_ERR_UNC_UNX_COMP
,
859 .correctable
= false,
862 .val
= PCI_ERR_UNC_RX_OVER
,
863 .correctable
= false,
866 .val
= PCI_ERR_UNC_MALF_TLP
,
867 .correctable
= false,
870 .val
= PCI_ERR_UNC_ECRC
,
871 .correctable
= false,
874 .val
= PCI_ERR_UNC_UNSUP
,
875 .correctable
= false,
878 .val
= PCI_ERR_UNC_ACSV
,
879 .correctable
= false,
882 .val
= PCI_ERR_UNC_INTN
,
883 .correctable
= false,
886 .val
= PCI_ERR_UNC_MCBTLP
,
887 .correctable
= false,
889 .name
= "ATOP_EBLOCKED",
890 .val
= PCI_ERR_UNC_ATOP_EBLOCKED
,
891 .correctable
= false,
893 .name
= "TLP_PRF_BLOCKED",
894 .val
= PCI_ERR_UNC_TLP_PRF_BLOCKED
,
895 .correctable
= false,
898 .val
= PCI_ERR_COR_RCVR
,
902 .val
= PCI_ERR_COR_BAD_TLP
,
906 .val
= PCI_ERR_COR_BAD_DLLP
,
910 .val
= PCI_ERR_COR_REP_ROLL
,
914 .val
= PCI_ERR_COR_REP_TIMER
,
917 .name
= "ADV_NONFATAL",
918 .val
= PCI_ERR_COR_ADV_NONFATAL
,
922 .val
= PCI_ERR_COR_INTERNAL
,
925 .name
= "HL_OVERFLOW",
926 .val
= PCI_ERR_COR_HL_OVERFLOW
,
931 static int pcie_aer_parse_error_string(const char *error_name
,
932 uint32_t *status
, bool *correctable
)
936 for (i
= 0; i
< ARRAY_SIZE(pcie_aer_error_list
); i
++) {
937 const PCIEAERErrorName
*e
= &pcie_aer_error_list
[i
];
938 if (strcmp(error_name
, e
->name
)) {
943 *correctable
= e
->correctable
;
949 int do_pcie_aer_inejct_error(Monitor
*mon
,
950 const QDict
*qdict
, QObject
**ret_data
)
952 const char *id
= qdict_get_str(qdict
, "id");
953 const char *error_name
;
954 uint32_t error_status
;
960 ret
= pci_qdev_find_device(id
, &dev
);
963 "id or pci device path is invalid or device not "
967 if (!pci_is_express(dev
)) {
968 monitor_printf(mon
, "the device doesn't support pci express. %s\n",
973 error_name
= qdict_get_str(qdict
, "error_status");
974 if (pcie_aer_parse_error_string(error_name
, &error_status
, &correctable
)) {
976 error_status
= strtoul(error_name
, &e
, 0);
977 correctable
= !!qdict_get_int(qdict
, "correctable");
978 if (!e
|| *e
!= '\0') {
979 monitor_printf(mon
, "invalid error status value. \"%s\"",
984 err
.source_id
= (pci_bus_num(dev
->bus
) << 8) | dev
->devfn
;
988 err
.flags
|= PCIE_AER_ERR_IS_CORRECTABLE
;
990 if (qdict_get_int(qdict
, "advisory_non_fatal")) {
991 err
.flags
|= PCIE_AER_ERR_MAYBE_ADVISORY
;
993 if (qdict_haskey(qdict
, "header0")) {
994 err
.flags
|= PCIE_AER_ERR_HEADER_VALID
;
996 if (qdict_haskey(qdict
, "prefix0")) {
997 err
.flags
|= PCIE_AER_ERR_TLP_PREFIX_PRESENT
;
1000 err
.header
[0] = qdict_get_try_int(qdict
, "header0", 0);
1001 err
.header
[1] = qdict_get_try_int(qdict
, "header1", 0);
1002 err
.header
[2] = qdict_get_try_int(qdict
, "header2", 0);
1003 err
.header
[3] = qdict_get_try_int(qdict
, "header3", 0);
1005 err
.prefix
[0] = qdict_get_try_int(qdict
, "prefix0", 0);
1006 err
.prefix
[1] = qdict_get_try_int(qdict
, "prefix1", 0);
1007 err
.prefix
[2] = qdict_get_try_int(qdict
, "prefix2", 0);
1008 err
.prefix
[3] = qdict_get_try_int(qdict
, "prefix3", 0);
1010 ret
= pcie_aer_inject_error(dev
, &err
);
1011 *ret_data
= qobject_from_jsonf("{'id': %s, "
1012 "'domain': %d, 'bus': %d, 'devfn': %d, "
1015 pci_find_domain(dev
->bus
),
1016 pci_bus_num(dev
->bus
), dev
->devfn
,