134e65944SIsaku Yamahata /*
234e65944SIsaku Yamahata * pcie_aer.c
334e65944SIsaku Yamahata *
434e65944SIsaku Yamahata * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
534e65944SIsaku Yamahata * VA Linux Systems Japan K.K.
634e65944SIsaku Yamahata *
734e65944SIsaku Yamahata * This program is free software; you can redistribute it and/or modify
834e65944SIsaku Yamahata * it under the terms of the GNU General Public License as published by
934e65944SIsaku Yamahata * the Free Software Foundation; either version 2 of the License, or
1034e65944SIsaku Yamahata * (at your option) any later version.
1134e65944SIsaku Yamahata *
1234e65944SIsaku Yamahata * This program is distributed in the hope that it will be useful,
1334e65944SIsaku Yamahata * but WITHOUT ANY WARRANTY; without even the implied warranty of
1434e65944SIsaku Yamahata * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1534e65944SIsaku Yamahata * GNU General Public License for more details.
1634e65944SIsaku Yamahata *
1734e65944SIsaku Yamahata * You should have received a copy of the GNU General Public License along
1834e65944SIsaku Yamahata * with this program; if not, see <http://www.gnu.org/licenses/>.
1934e65944SIsaku Yamahata */
2034e65944SIsaku Yamahata
2197d5408fSPeter Maydell #include "qemu/osdep.h"
22d6454270SMarkus Armbruster #include "migration/vmstate.h"
23c759b24fSMichael S. Tsirkin #include "hw/pci/pci_bridge.h"
24c759b24fSMichael S. Tsirkin #include "hw/pci/pcie.h"
25c759b24fSMichael S. Tsirkin #include "hw/pci/msix.h"
26c759b24fSMichael S. Tsirkin #include "hw/pci/msi.h"
2706aac7bdSMichael S. Tsirkin #include "hw/pci/pci_bus.h"
28c759b24fSMichael S. Tsirkin #include "hw/pci/pcie_regs.h"
29d0e67298SMarkus Armbruster #include "pci-internal.h"
3034e65944SIsaku Yamahata
3134e65944SIsaku Yamahata //#define DEBUG_PCIE
3234e65944SIsaku Yamahata #ifdef DEBUG_PCIE
3334e65944SIsaku Yamahata # define PCIE_DPRINTF(fmt, ...) \
3434e65944SIsaku Yamahata fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__)
3534e65944SIsaku Yamahata #else
3634e65944SIsaku Yamahata # define PCIE_DPRINTF(fmt, ...) do {} while (0)
3734e65944SIsaku Yamahata #endif
3834e65944SIsaku Yamahata #define PCIE_DEV_PRINTF(dev, fmt, ...) \
3934e65944SIsaku Yamahata PCIE_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__)
4034e65944SIsaku Yamahata
4181486b55SJan Kiszka #define PCI_ERR_SRC_COR_OFFS 0
4281486b55SJan Kiszka #define PCI_ERR_SRC_UNCOR_OFFS 2
4381486b55SJan Kiszka
4434e65944SIsaku Yamahata /* From 6.2.7 Error Listing and Rules. Table 6-2, 6-3 and 6-4 */
pcie_aer_uncor_default_severity(uint32_t status)4534e65944SIsaku Yamahata static uint32_t pcie_aer_uncor_default_severity(uint32_t status)
4634e65944SIsaku Yamahata {
4734e65944SIsaku Yamahata switch (status) {
4834e65944SIsaku Yamahata case PCI_ERR_UNC_INTN:
4934e65944SIsaku Yamahata case PCI_ERR_UNC_DLP:
5034e65944SIsaku Yamahata case PCI_ERR_UNC_SDN:
5134e65944SIsaku Yamahata case PCI_ERR_UNC_RX_OVER:
5234e65944SIsaku Yamahata case PCI_ERR_UNC_FCP:
5334e65944SIsaku Yamahata case PCI_ERR_UNC_MALF_TLP:
5434e65944SIsaku Yamahata return PCI_ERR_ROOT_CMD_FATAL_EN;
5534e65944SIsaku Yamahata case PCI_ERR_UNC_POISON_TLP:
5634e65944SIsaku Yamahata case PCI_ERR_UNC_ECRC:
5734e65944SIsaku Yamahata case PCI_ERR_UNC_UNSUP:
5834e65944SIsaku Yamahata case PCI_ERR_UNC_COMP_TIME:
5934e65944SIsaku Yamahata case PCI_ERR_UNC_COMP_ABORT:
6034e65944SIsaku Yamahata case PCI_ERR_UNC_UNX_COMP:
6134e65944SIsaku Yamahata case PCI_ERR_UNC_ACSV:
6234e65944SIsaku Yamahata case PCI_ERR_UNC_MCBTLP:
6334e65944SIsaku Yamahata case PCI_ERR_UNC_ATOP_EBLOCKED:
6434e65944SIsaku Yamahata case PCI_ERR_UNC_TLP_PRF_BLOCKED:
6534e65944SIsaku Yamahata return PCI_ERR_ROOT_CMD_NONFATAL_EN;
6634e65944SIsaku Yamahata default:
6734e65944SIsaku Yamahata abort();
6834e65944SIsaku Yamahata break;
6934e65944SIsaku Yamahata }
7034e65944SIsaku Yamahata return PCI_ERR_ROOT_CMD_FATAL_EN;
7134e65944SIsaku Yamahata }
7234e65944SIsaku Yamahata
aer_log_add_err(PCIEAERLog * aer_log,const PCIEAERErr * err)7334e65944SIsaku Yamahata static int aer_log_add_err(PCIEAERLog *aer_log, const PCIEAERErr *err)
7434e65944SIsaku Yamahata {
7534e65944SIsaku Yamahata if (aer_log->log_num == aer_log->log_max) {
7634e65944SIsaku Yamahata return -1;
7734e65944SIsaku Yamahata }
7834e65944SIsaku Yamahata memcpy(&aer_log->log[aer_log->log_num], err, sizeof *err);
7934e65944SIsaku Yamahata aer_log->log_num++;
8034e65944SIsaku Yamahata return 0;
8134e65944SIsaku Yamahata }
8234e65944SIsaku Yamahata
aer_log_del_err(PCIEAERLog * aer_log,PCIEAERErr * err)8334e65944SIsaku Yamahata static void aer_log_del_err(PCIEAERLog *aer_log, PCIEAERErr *err)
8434e65944SIsaku Yamahata {
8534e65944SIsaku Yamahata assert(aer_log->log_num);
8634e65944SIsaku Yamahata *err = aer_log->log[0];
8734e65944SIsaku Yamahata aer_log->log_num--;
8834e65944SIsaku Yamahata memmove(&aer_log->log[0], &aer_log->log[1],
8934e65944SIsaku Yamahata aer_log->log_num * sizeof *err);
9034e65944SIsaku Yamahata }
9134e65944SIsaku Yamahata
aer_log_clear_all_err(PCIEAERLog * aer_log)9234e65944SIsaku Yamahata static void aer_log_clear_all_err(PCIEAERLog *aer_log)
9334e65944SIsaku Yamahata {
9434e65944SIsaku Yamahata aer_log->log_num = 0;
9534e65944SIsaku Yamahata }
9634e65944SIsaku Yamahata
pcie_aer_init(PCIDevice * dev,uint8_t cap_ver,uint16_t offset,uint16_t size,Error ** errp)97f18c697bSDou Liyang int pcie_aer_init(PCIDevice *dev, uint8_t cap_ver, uint16_t offset,
98f18c697bSDou Liyang uint16_t size, Error **errp)
9934e65944SIsaku Yamahata {
100f18c697bSDou Liyang pcie_add_capability(dev, PCI_EXT_CAP_ID_ERR, cap_ver,
1018d86ada2SChen Fan offset, size);
10233848ceeSCao jin dev->exp.aer_cap = offset;
10334e65944SIsaku Yamahata
10433848ceeSCao jin /* clip down the value to avoid unreasonable memory usage */
10534e65944SIsaku Yamahata if (dev->exp.aer_log.log_max > PCIE_AER_LOG_MAX_LIMIT) {
10633848ceeSCao jin error_setg(errp, "Invalid aer_log_max %d. The max number of aer log "
10733848ceeSCao jin "is %d", dev->exp.aer_log.log_max, PCIE_AER_LOG_MAX_LIMIT);
10834e65944SIsaku Yamahata return -EINVAL;
10934e65944SIsaku Yamahata }
1107267c094SAnthony Liguori dev->exp.aer_log.log = g_malloc0(sizeof dev->exp.aer_log.log[0] *
11134e65944SIsaku Yamahata dev->exp.aer_log.log_max);
11234e65944SIsaku Yamahata
11334e65944SIsaku Yamahata pci_set_long(dev->w1cmask + offset + PCI_ERR_UNCOR_STATUS,
11434e65944SIsaku Yamahata PCI_ERR_UNC_SUPPORTED);
1155ed3dabeSLeonardo Bras
1165ed3dabeSLeonardo Bras if (dev->cap_present & QEMU_PCIE_ERR_UNC_MASK) {
117010746aeSJonathan Cameron pci_set_long(dev->config + offset + PCI_ERR_UNCOR_MASK,
118010746aeSJonathan Cameron PCI_ERR_UNC_MASK_DEFAULT);
119010746aeSJonathan Cameron pci_set_long(dev->wmask + offset + PCI_ERR_UNCOR_MASK,
120010746aeSJonathan Cameron PCI_ERR_UNC_SUPPORTED);
1215ed3dabeSLeonardo Bras }
12234e65944SIsaku Yamahata
12334e65944SIsaku Yamahata pci_set_long(dev->config + offset + PCI_ERR_UNCOR_SEVER,
12434e65944SIsaku Yamahata PCI_ERR_UNC_SEVERITY_DEFAULT);
12534e65944SIsaku Yamahata pci_set_long(dev->wmask + offset + PCI_ERR_UNCOR_SEVER,
12634e65944SIsaku Yamahata PCI_ERR_UNC_SUPPORTED);
12734e65944SIsaku Yamahata
12834e65944SIsaku Yamahata pci_long_test_and_set_mask(dev->w1cmask + offset + PCI_ERR_COR_STATUS,
129310e91f7SChen Fan PCI_ERR_COR_SUPPORTED);
13034e65944SIsaku Yamahata
13134e65944SIsaku Yamahata pci_set_long(dev->config + offset + PCI_ERR_COR_MASK,
13234e65944SIsaku Yamahata PCI_ERR_COR_MASK_DEFAULT);
13334e65944SIsaku Yamahata pci_set_long(dev->wmask + offset + PCI_ERR_COR_MASK,
13434e65944SIsaku Yamahata PCI_ERR_COR_SUPPORTED);
13534e65944SIsaku Yamahata
13634e65944SIsaku Yamahata /* capabilities and control. multiple header logging is supported */
13734e65944SIsaku Yamahata if (dev->exp.aer_log.log_max > 0) {
13834e65944SIsaku Yamahata pci_set_long(dev->config + offset + PCI_ERR_CAP,
13934e65944SIsaku Yamahata PCI_ERR_CAP_ECRC_GENC | PCI_ERR_CAP_ECRC_CHKC |
14034e65944SIsaku Yamahata PCI_ERR_CAP_MHRC);
14134e65944SIsaku Yamahata pci_set_long(dev->wmask + offset + PCI_ERR_CAP,
14234e65944SIsaku Yamahata PCI_ERR_CAP_ECRC_GENE | PCI_ERR_CAP_ECRC_CHKE |
14334e65944SIsaku Yamahata PCI_ERR_CAP_MHRE);
14434e65944SIsaku Yamahata } else {
14534e65944SIsaku Yamahata pci_set_long(dev->config + offset + PCI_ERR_CAP,
14634e65944SIsaku Yamahata PCI_ERR_CAP_ECRC_GENC | PCI_ERR_CAP_ECRC_CHKC);
14734e65944SIsaku Yamahata pci_set_long(dev->wmask + offset + PCI_ERR_CAP,
14834e65944SIsaku Yamahata PCI_ERR_CAP_ECRC_GENE | PCI_ERR_CAP_ECRC_CHKE);
14934e65944SIsaku Yamahata }
15034e65944SIsaku Yamahata
15134e65944SIsaku Yamahata switch (pcie_cap_get_type(dev)) {
15234e65944SIsaku Yamahata case PCI_EXP_TYPE_ROOT_PORT:
15334e65944SIsaku Yamahata /* this case will be set by pcie_aer_root_init() */
15434e65944SIsaku Yamahata /* fallthrough */
15534e65944SIsaku Yamahata case PCI_EXP_TYPE_DOWNSTREAM:
15634e65944SIsaku Yamahata case PCI_EXP_TYPE_UPSTREAM:
15734e65944SIsaku Yamahata pci_word_test_and_set_mask(dev->wmask + PCI_BRIDGE_CONTROL,
15834e65944SIsaku Yamahata PCI_BRIDGE_CTL_SERR);
15934e65944SIsaku Yamahata pci_long_test_and_set_mask(dev->w1cmask + PCI_STATUS,
16034e65944SIsaku Yamahata PCI_SEC_STATUS_RCV_SYSTEM_ERROR);
16134e65944SIsaku Yamahata break;
16234e65944SIsaku Yamahata default:
16334e65944SIsaku Yamahata /* nothing */
16434e65944SIsaku Yamahata break;
16534e65944SIsaku Yamahata }
16634e65944SIsaku Yamahata return 0;
16734e65944SIsaku Yamahata }
16834e65944SIsaku Yamahata
pcie_aer_exit(PCIDevice * dev)16934e65944SIsaku Yamahata void pcie_aer_exit(PCIDevice *dev)
17034e65944SIsaku Yamahata {
1717267c094SAnthony Liguori g_free(dev->exp.aer_log.log);
17234e65944SIsaku Yamahata }
17334e65944SIsaku Yamahata
pcie_aer_update_uncor_status(PCIDevice * dev)17434e65944SIsaku Yamahata static void pcie_aer_update_uncor_status(PCIDevice *dev)
17534e65944SIsaku Yamahata {
17634e65944SIsaku Yamahata uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
17734e65944SIsaku Yamahata PCIEAERLog *aer_log = &dev->exp.aer_log;
17834e65944SIsaku Yamahata
17934e65944SIsaku Yamahata uint16_t i;
18034e65944SIsaku Yamahata for (i = 0; i < aer_log->log_num; i++) {
18134e65944SIsaku Yamahata pci_long_test_and_set_mask(aer_cap + PCI_ERR_UNCOR_STATUS,
18234e65944SIsaku Yamahata dev->exp.aer_log.log[i].status);
18334e65944SIsaku Yamahata }
18434e65944SIsaku Yamahata }
18534e65944SIsaku Yamahata
18634e65944SIsaku Yamahata /*
18734e65944SIsaku Yamahata * return value:
188247c97f3SMichael S. Tsirkin * true: error message needs to be sent up
18934e65944SIsaku Yamahata * false: error message is masked
19034e65944SIsaku Yamahata *
19134e65944SIsaku Yamahata * 6.2.6 Error Message Control
19234e65944SIsaku Yamahata * Figure 6-3
19334e65944SIsaku Yamahata * all pci express devices part
19434e65944SIsaku Yamahata */
19534e65944SIsaku Yamahata static bool
pcie_aer_msg_alldev(PCIDevice * dev,const PCIEAERMsg * msg)19634e65944SIsaku Yamahata pcie_aer_msg_alldev(PCIDevice *dev, const PCIEAERMsg *msg)
19734e65944SIsaku Yamahata {
1989a6ef182SJonathan Cameron uint16_t devctl = pci_get_word(dev->config + dev->exp.exp_cap +
1999a6ef182SJonathan Cameron PCI_EXP_DEVCTL);
20034e65944SIsaku Yamahata if (!(pcie_aer_msg_is_uncor(msg) &&
2019a6ef182SJonathan Cameron (pci_get_word(dev->config + PCI_COMMAND) & PCI_COMMAND_SERR)) &&
2029a6ef182SJonathan Cameron !((msg->severity == PCI_ERR_ROOT_CMD_NONFATAL_EN) &&
2039a6ef182SJonathan Cameron (devctl & PCI_EXP_DEVCTL_NFERE)) &&
2049a6ef182SJonathan Cameron !((msg->severity == PCI_ERR_ROOT_CMD_COR_EN) &&
2059a6ef182SJonathan Cameron (devctl & PCI_EXP_DEVCTL_CERE)) &&
2069a6ef182SJonathan Cameron !((msg->severity == PCI_ERR_ROOT_CMD_FATAL_EN) &&
2079a6ef182SJonathan Cameron (devctl & PCI_EXP_DEVCTL_FERE))) {
20834e65944SIsaku Yamahata return false;
20934e65944SIsaku Yamahata }
21034e65944SIsaku Yamahata
21134e65944SIsaku Yamahata /* Signaled System Error
21234e65944SIsaku Yamahata *
21334e65944SIsaku Yamahata * 7.5.1.1 Command register
21434e65944SIsaku Yamahata * Bit 8 SERR# Enable
21534e65944SIsaku Yamahata *
21634e65944SIsaku Yamahata * When Set, this bit enables reporting of Non-fatal and Fatal
21734e65944SIsaku Yamahata * errors detected by the Function to the Root Complex. Note that
21834e65944SIsaku Yamahata * errors are reported if enabled either through this bit or through
21934e65944SIsaku Yamahata * the PCI Express specific bits in the Device Control register (see
22034e65944SIsaku Yamahata * Section 7.8.4).
22134e65944SIsaku Yamahata */
22234e65944SIsaku Yamahata pci_word_test_and_set_mask(dev->config + PCI_STATUS,
22334e65944SIsaku Yamahata PCI_STATUS_SIG_SYSTEM_ERROR);
22434e65944SIsaku Yamahata
22534e65944SIsaku Yamahata if (!(msg->severity &
22634e65944SIsaku Yamahata pci_get_word(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL))) {
22734e65944SIsaku Yamahata return false;
22834e65944SIsaku Yamahata }
22934e65944SIsaku Yamahata
23034e65944SIsaku Yamahata /* send up error message */
231247c97f3SMichael S. Tsirkin return true;
232247c97f3SMichael S. Tsirkin }
233247c97f3SMichael S. Tsirkin
23434e65944SIsaku Yamahata /*
23534e65944SIsaku Yamahata * return value:
23634e65944SIsaku Yamahata * true: error message is sent up
23734e65944SIsaku Yamahata * false: error message is masked
23834e65944SIsaku Yamahata *
23934e65944SIsaku Yamahata * 6.2.6 Error Message Control
24034e65944SIsaku Yamahata * Figure 6-3
24134e65944SIsaku Yamahata * virtual pci bridge part
24234e65944SIsaku Yamahata */
pcie_aer_msg_vbridge(PCIDevice * dev,const PCIEAERMsg * msg)24334e65944SIsaku Yamahata static bool pcie_aer_msg_vbridge(PCIDevice *dev, const PCIEAERMsg *msg)
24434e65944SIsaku Yamahata {
24534e65944SIsaku Yamahata uint16_t bridge_control = pci_get_word(dev->config + PCI_BRIDGE_CONTROL);
24634e65944SIsaku Yamahata
24734e65944SIsaku Yamahata if (pcie_aer_msg_is_uncor(msg)) {
24834e65944SIsaku Yamahata /* Received System Error */
24934e65944SIsaku Yamahata pci_word_test_and_set_mask(dev->config + PCI_SEC_STATUS,
25034e65944SIsaku Yamahata PCI_SEC_STATUS_RCV_SYSTEM_ERROR);
25134e65944SIsaku Yamahata }
25234e65944SIsaku Yamahata
25334e65944SIsaku Yamahata if (!(bridge_control & PCI_BRIDGE_CTL_SERR)) {
25434e65944SIsaku Yamahata return false;
25534e65944SIsaku Yamahata }
25634e65944SIsaku Yamahata return true;
25734e65944SIsaku Yamahata }
25834e65944SIsaku Yamahata
pcie_aer_root_set_vector(PCIDevice * dev,unsigned int vector)25934e65944SIsaku Yamahata void pcie_aer_root_set_vector(PCIDevice *dev, unsigned int vector)
26034e65944SIsaku Yamahata {
26134e65944SIsaku Yamahata uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
26234e65944SIsaku Yamahata assert(vector < PCI_ERR_ROOT_IRQ_MAX);
26334e65944SIsaku Yamahata pci_long_test_and_clear_mask(aer_cap + PCI_ERR_ROOT_STATUS,
26434e65944SIsaku Yamahata PCI_ERR_ROOT_IRQ);
26534e65944SIsaku Yamahata pci_long_test_and_set_mask(aer_cap + PCI_ERR_ROOT_STATUS,
26634e65944SIsaku Yamahata vector << PCI_ERR_ROOT_IRQ_SHIFT);
26734e65944SIsaku Yamahata }
26834e65944SIsaku Yamahata
pcie_aer_root_get_vector(PCIDevice * dev)26934e65944SIsaku Yamahata static unsigned int pcie_aer_root_get_vector(PCIDevice *dev)
27034e65944SIsaku Yamahata {
27134e65944SIsaku Yamahata uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
27234e65944SIsaku Yamahata uint32_t root_status = pci_get_long(aer_cap + PCI_ERR_ROOT_STATUS);
27334e65944SIsaku Yamahata return (root_status & PCI_ERR_ROOT_IRQ) >> PCI_ERR_ROOT_IRQ_SHIFT;
27434e65944SIsaku Yamahata }
27534e65944SIsaku Yamahata
276c3f33667SMichael S. Tsirkin /* Given a status register, get corresponding bits in the command register */
pcie_aer_status_to_cmd(uint32_t status)277c3f33667SMichael S. Tsirkin static uint32_t pcie_aer_status_to_cmd(uint32_t status)
278c3f33667SMichael S. Tsirkin {
279c3f33667SMichael S. Tsirkin uint32_t cmd = 0;
280c3f33667SMichael S. Tsirkin if (status & PCI_ERR_ROOT_COR_RCV) {
281c3f33667SMichael S. Tsirkin cmd |= PCI_ERR_ROOT_CMD_COR_EN;
282c3f33667SMichael S. Tsirkin }
283c3f33667SMichael S. Tsirkin if (status & PCI_ERR_ROOT_NONFATAL_RCV) {
284c3f33667SMichael S. Tsirkin cmd |= PCI_ERR_ROOT_CMD_NONFATAL_EN;
285c3f33667SMichael S. Tsirkin }
286c3f33667SMichael S. Tsirkin if (status & PCI_ERR_ROOT_FATAL_RCV) {
287c3f33667SMichael S. Tsirkin cmd |= PCI_ERR_ROOT_CMD_FATAL_EN;
288c3f33667SMichael S. Tsirkin }
289c3f33667SMichael S. Tsirkin return cmd;
290c3f33667SMichael S. Tsirkin }
291c3f33667SMichael S. Tsirkin
pcie_aer_root_notify(PCIDevice * dev)292513691b7SMichael S. Tsirkin static void pcie_aer_root_notify(PCIDevice *dev)
293513691b7SMichael S. Tsirkin {
294513691b7SMichael S. Tsirkin if (msix_enabled(dev)) {
295513691b7SMichael S. Tsirkin msix_notify(dev, pcie_aer_root_get_vector(dev));
296513691b7SMichael S. Tsirkin } else if (msi_enabled(dev)) {
297513691b7SMichael S. Tsirkin msi_notify(dev, pcie_aer_root_get_vector(dev));
2982e865671SFrederic Barrat } else if (pci_intx(dev) != -1) {
2995a03e708SMarcel Apfelbaum pci_irq_assert(dev);
300513691b7SMichael S. Tsirkin }
301513691b7SMichael S. Tsirkin }
302513691b7SMichael S. Tsirkin
30334e65944SIsaku Yamahata /*
30434e65944SIsaku Yamahata * 6.2.6 Error Message Control
30534e65944SIsaku Yamahata * Figure 6-3
30634e65944SIsaku Yamahata * root port part
30734e65944SIsaku Yamahata */
pcie_aer_msg_root_port(PCIDevice * dev,const PCIEAERMsg * msg)3085f47c187SMichael S. Tsirkin static void pcie_aer_msg_root_port(PCIDevice *dev, const PCIEAERMsg *msg)
30934e65944SIsaku Yamahata {
31034e65944SIsaku Yamahata uint16_t cmd;
31134e65944SIsaku Yamahata uint8_t *aer_cap;
31234e65944SIsaku Yamahata uint32_t root_cmd;
313c3f33667SMichael S. Tsirkin uint32_t root_status, prev_status;
31434e65944SIsaku Yamahata
31534e65944SIsaku Yamahata cmd = pci_get_word(dev->config + PCI_COMMAND);
31634e65944SIsaku Yamahata aer_cap = dev->config + dev->exp.aer_cap;
31734e65944SIsaku Yamahata root_cmd = pci_get_long(aer_cap + PCI_ERR_ROOT_COMMAND);
318c3f33667SMichael S. Tsirkin prev_status = root_status = pci_get_long(aer_cap + PCI_ERR_ROOT_STATUS);
31934e65944SIsaku Yamahata
32034e65944SIsaku Yamahata if (cmd & PCI_COMMAND_SERR) {
32134e65944SIsaku Yamahata /* System Error.
32234e65944SIsaku Yamahata *
32334e65944SIsaku Yamahata * The way to report System Error is platform specific and
32434e65944SIsaku Yamahata * it isn't implemented in qemu right now.
32534e65944SIsaku Yamahata * So just discard the error for now.
32634e65944SIsaku Yamahata * OS which cares of aer would receive errors via
327f1c0cff8SMichael Tokarev * native aer mechanisms, so this wouldn't matter.
32834e65944SIsaku Yamahata */
32934e65944SIsaku Yamahata }
33034e65944SIsaku Yamahata
331118d4ed0SDr. David Alan Gilbert /* Error Message Received: Root Error Status register */
33234e65944SIsaku Yamahata switch (msg->severity) {
33334e65944SIsaku Yamahata case PCI_ERR_ROOT_CMD_COR_EN:
33434e65944SIsaku Yamahata if (root_status & PCI_ERR_ROOT_COR_RCV) {
33534e65944SIsaku Yamahata root_status |= PCI_ERR_ROOT_MULTI_COR_RCV;
33634e65944SIsaku Yamahata } else {
33781486b55SJan Kiszka pci_set_word(aer_cap + PCI_ERR_ROOT_ERR_SRC + PCI_ERR_SRC_COR_OFFS,
33881486b55SJan Kiszka msg->source_id);
33934e65944SIsaku Yamahata }
34034e65944SIsaku Yamahata root_status |= PCI_ERR_ROOT_COR_RCV;
34134e65944SIsaku Yamahata break;
34234e65944SIsaku Yamahata case PCI_ERR_ROOT_CMD_NONFATAL_EN:
34334e65944SIsaku Yamahata root_status |= PCI_ERR_ROOT_NONFATAL_RCV;
34434e65944SIsaku Yamahata break;
34534e65944SIsaku Yamahata case PCI_ERR_ROOT_CMD_FATAL_EN:
34634e65944SIsaku Yamahata if (!(root_status & PCI_ERR_ROOT_UNCOR_RCV)) {
34734e65944SIsaku Yamahata root_status |= PCI_ERR_ROOT_FIRST_FATAL;
34834e65944SIsaku Yamahata }
34934e65944SIsaku Yamahata root_status |= PCI_ERR_ROOT_FATAL_RCV;
35034e65944SIsaku Yamahata break;
35134e65944SIsaku Yamahata default:
35234e65944SIsaku Yamahata abort();
35334e65944SIsaku Yamahata break;
35434e65944SIsaku Yamahata }
35534e65944SIsaku Yamahata if (pcie_aer_msg_is_uncor(msg)) {
35634e65944SIsaku Yamahata if (root_status & PCI_ERR_ROOT_UNCOR_RCV) {
35734e65944SIsaku Yamahata root_status |= PCI_ERR_ROOT_MULTI_UNCOR_RCV;
35834e65944SIsaku Yamahata } else {
35981486b55SJan Kiszka pci_set_word(aer_cap + PCI_ERR_ROOT_ERR_SRC +
36081486b55SJan Kiszka PCI_ERR_SRC_UNCOR_OFFS, msg->source_id);
36134e65944SIsaku Yamahata }
36234e65944SIsaku Yamahata root_status |= PCI_ERR_ROOT_UNCOR_RCV;
36334e65944SIsaku Yamahata }
36434e65944SIsaku Yamahata pci_set_long(aer_cap + PCI_ERR_ROOT_STATUS, root_status);
36534e65944SIsaku Yamahata
36634e65944SIsaku Yamahata /* 6.2.4.1.2 Interrupt Generation */
367c3f33667SMichael S. Tsirkin /* All the above did was set some bits in the status register.
368c3f33667SMichael S. Tsirkin * Specifically these that match message severity.
369c3f33667SMichael S. Tsirkin * The below code relies on this fact. */
370c3f33667SMichael S. Tsirkin if (!(root_cmd & msg->severity) ||
371c3f33667SMichael S. Tsirkin (pcie_aer_status_to_cmd(prev_status) & root_cmd)) {
372c3f33667SMichael S. Tsirkin /* Condition is not being set or was already true so nothing to do. */
3735f47c187SMichael S. Tsirkin return;
374c3f33667SMichael S. Tsirkin }
375c3f33667SMichael S. Tsirkin
376513691b7SMichael S. Tsirkin pcie_aer_root_notify(dev);
37734e65944SIsaku Yamahata }
37834e65944SIsaku Yamahata
37934e65944SIsaku Yamahata /*
38034e65944SIsaku Yamahata * 6.2.6 Error Message Control Figure 6-3
381247c97f3SMichael S. Tsirkin *
382d33d9156SMichael S. Tsirkin * Walk up the bus tree from the device, propagate the error message.
38334e65944SIsaku Yamahata */
pcie_aer_msg(PCIDevice * dev,const PCIEAERMsg * msg)3848f16de18SEric Blake static void pcie_aer_msg(PCIDevice *dev, const PCIEAERMsg *msg)
385247c97f3SMichael S. Tsirkin {
386d33d9156SMichael S. Tsirkin uint8_t type;
387d33d9156SMichael S. Tsirkin
388247c97f3SMichael S. Tsirkin while (dev) {
389d33d9156SMichael S. Tsirkin if (!pci_is_express(dev)) {
390d33d9156SMichael S. Tsirkin /* just ignore it */
391d33d9156SMichael S. Tsirkin /* TODO: Shouldn't we set PCI_STATUS_SIG_SYSTEM_ERROR?
392d33d9156SMichael S. Tsirkin * Consider e.g. a PCI bridge above a PCI Express device. */
393247c97f3SMichael S. Tsirkin return;
394247c97f3SMichael S. Tsirkin }
395d33d9156SMichael S. Tsirkin
396d33d9156SMichael S. Tsirkin type = pcie_cap_get_type(dev);
397d33d9156SMichael S. Tsirkin if ((type == PCI_EXP_TYPE_ROOT_PORT ||
398d33d9156SMichael S. Tsirkin type == PCI_EXP_TYPE_UPSTREAM ||
399d33d9156SMichael S. Tsirkin type == PCI_EXP_TYPE_DOWNSTREAM) &&
400d33d9156SMichael S. Tsirkin !pcie_aer_msg_vbridge(dev, msg)) {
401d33d9156SMichael S. Tsirkin return;
402d33d9156SMichael S. Tsirkin }
403d33d9156SMichael S. Tsirkin if (!pcie_aer_msg_alldev(dev, msg)) {
404d33d9156SMichael S. Tsirkin return;
405d33d9156SMichael S. Tsirkin }
406d33d9156SMichael S. Tsirkin if (type == PCI_EXP_TYPE_ROOT_PORT) {
407d33d9156SMichael S. Tsirkin pcie_aer_msg_root_port(dev, msg);
408d33d9156SMichael S. Tsirkin /* Root port can notify system itself,
409d33d9156SMichael S. Tsirkin or send the error message to root complex event collector. */
410d33d9156SMichael S. Tsirkin /*
411d33d9156SMichael S. Tsirkin * if root port is associated with an event collector,
412d33d9156SMichael S. Tsirkin * return the root complex event collector here.
413d33d9156SMichael S. Tsirkin * For now root complex event collector isn't supported.
414d33d9156SMichael S. Tsirkin */
415d33d9156SMichael S. Tsirkin return;
416d33d9156SMichael S. Tsirkin }
417fd56e061SDavid Gibson dev = pci_bridge_get_device(pci_get_bus(dev));
418247c97f3SMichael S. Tsirkin }
41934e65944SIsaku Yamahata }
42034e65944SIsaku Yamahata
pcie_aer_update_log(PCIDevice * dev,const PCIEAERErr * err)42134e65944SIsaku Yamahata static void pcie_aer_update_log(PCIDevice *dev, const PCIEAERErr *err)
42234e65944SIsaku Yamahata {
42334e65944SIsaku Yamahata uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
424786a4ea8SStefan Hajnoczi uint8_t first_bit = ctz32(err->status);
42534e65944SIsaku Yamahata uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
42634e65944SIsaku Yamahata int i;
42734e65944SIsaku Yamahata
42834e65944SIsaku Yamahata assert(err->status);
42974d63b65SIsaku Yamahata assert(!(err->status & (err->status - 1)));
43034e65944SIsaku Yamahata
43134e65944SIsaku Yamahata errcap &= ~(PCI_ERR_CAP_FEP_MASK | PCI_ERR_CAP_TLP);
43234e65944SIsaku Yamahata errcap |= PCI_ERR_CAP_FEP(first_bit);
43334e65944SIsaku Yamahata
43434e65944SIsaku Yamahata if (err->flags & PCIE_AER_ERR_HEADER_VALID) {
43534e65944SIsaku Yamahata for (i = 0; i < ARRAY_SIZE(err->header); ++i) {
43634e65944SIsaku Yamahata /* 7.10.8 Header Log Register */
43734e65944SIsaku Yamahata uint8_t *header_log =
43834e65944SIsaku Yamahata aer_cap + PCI_ERR_HEADER_LOG + i * sizeof err->header[0];
4396bd194abSPeter Maydell stl_be_p(header_log, err->header[i]);
44034e65944SIsaku Yamahata }
44134e65944SIsaku Yamahata } else {
44234e65944SIsaku Yamahata assert(!(err->flags & PCIE_AER_ERR_TLP_PREFIX_PRESENT));
44334e65944SIsaku Yamahata memset(aer_cap + PCI_ERR_HEADER_LOG, 0, PCI_ERR_HEADER_LOG_SIZE);
44434e65944SIsaku Yamahata }
44534e65944SIsaku Yamahata
44634e65944SIsaku Yamahata if ((err->flags & PCIE_AER_ERR_TLP_PREFIX_PRESENT) &&
44777a3c1d7SChen Fan (pci_get_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCAP2) &
44834e65944SIsaku Yamahata PCI_EXP_DEVCAP2_EETLPP)) {
44934e65944SIsaku Yamahata for (i = 0; i < ARRAY_SIZE(err->prefix); ++i) {
45034e65944SIsaku Yamahata /* 7.10.12 tlp prefix log register */
45134e65944SIsaku Yamahata uint8_t *prefix_log =
45234e65944SIsaku Yamahata aer_cap + PCI_ERR_TLP_PREFIX_LOG + i * sizeof err->prefix[0];
4536bd194abSPeter Maydell stl_be_p(prefix_log, err->prefix[i]);
45434e65944SIsaku Yamahata }
45534e65944SIsaku Yamahata errcap |= PCI_ERR_CAP_TLP;
45634e65944SIsaku Yamahata } else {
45734e65944SIsaku Yamahata memset(aer_cap + PCI_ERR_TLP_PREFIX_LOG, 0,
45834e65944SIsaku Yamahata PCI_ERR_TLP_PREFIX_LOG_SIZE);
45934e65944SIsaku Yamahata }
46034e65944SIsaku Yamahata pci_set_long(aer_cap + PCI_ERR_CAP, errcap);
46134e65944SIsaku Yamahata }
46234e65944SIsaku Yamahata
pcie_aer_clear_log(PCIDevice * dev)46334e65944SIsaku Yamahata static void pcie_aer_clear_log(PCIDevice *dev)
46434e65944SIsaku Yamahata {
46534e65944SIsaku Yamahata uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
46634e65944SIsaku Yamahata
46734e65944SIsaku Yamahata pci_long_test_and_clear_mask(aer_cap + PCI_ERR_CAP,
46834e65944SIsaku Yamahata PCI_ERR_CAP_FEP_MASK | PCI_ERR_CAP_TLP);
46934e65944SIsaku Yamahata
47034e65944SIsaku Yamahata memset(aer_cap + PCI_ERR_HEADER_LOG, 0, PCI_ERR_HEADER_LOG_SIZE);
47134e65944SIsaku Yamahata memset(aer_cap + PCI_ERR_TLP_PREFIX_LOG, 0, PCI_ERR_TLP_PREFIX_LOG_SIZE);
47234e65944SIsaku Yamahata }
47334e65944SIsaku Yamahata
pcie_aer_clear_error(PCIDevice * dev)47434e65944SIsaku Yamahata static void pcie_aer_clear_error(PCIDevice *dev)
47534e65944SIsaku Yamahata {
47634e65944SIsaku Yamahata uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
47734e65944SIsaku Yamahata uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
47834e65944SIsaku Yamahata PCIEAERLog *aer_log = &dev->exp.aer_log;
47934e65944SIsaku Yamahata PCIEAERErr err;
48034e65944SIsaku Yamahata
48134e65944SIsaku Yamahata if (!(errcap & PCI_ERR_CAP_MHRE) || !aer_log->log_num) {
48234e65944SIsaku Yamahata pcie_aer_clear_log(dev);
48334e65944SIsaku Yamahata return;
48434e65944SIsaku Yamahata }
48534e65944SIsaku Yamahata
48634e65944SIsaku Yamahata /*
48734e65944SIsaku Yamahata * If more errors are queued, set corresponding bits in uncorrectable
48834e65944SIsaku Yamahata * error status.
48934e65944SIsaku Yamahata * We emulate uncorrectable error status register as W1CS.
49034e65944SIsaku Yamahata * So set bit in uncorrectable error status here again for multiple
49134e65944SIsaku Yamahata * error recording support.
49234e65944SIsaku Yamahata *
49334e65944SIsaku Yamahata * 6.2.4.2 Multiple Error Handling(Advanced Error Reporting Capability)
49434e65944SIsaku Yamahata */
49534e65944SIsaku Yamahata pcie_aer_update_uncor_status(dev);
49634e65944SIsaku Yamahata
49734e65944SIsaku Yamahata aer_log_del_err(aer_log, &err);
49834e65944SIsaku Yamahata pcie_aer_update_log(dev, &err);
49934e65944SIsaku Yamahata }
50034e65944SIsaku Yamahata
pcie_aer_record_error(PCIDevice * dev,const PCIEAERErr * err)50134e65944SIsaku Yamahata static int pcie_aer_record_error(PCIDevice *dev,
50234e65944SIsaku Yamahata const PCIEAERErr *err)
50334e65944SIsaku Yamahata {
50434e65944SIsaku Yamahata uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
50534e65944SIsaku Yamahata uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
50634e65944SIsaku Yamahata int fep = PCI_ERR_CAP_FEP(errcap);
50734e65944SIsaku Yamahata
50834e65944SIsaku Yamahata assert(err->status);
50974d63b65SIsaku Yamahata assert(!(err->status & (err->status - 1)));
51034e65944SIsaku Yamahata
51134e65944SIsaku Yamahata if (errcap & PCI_ERR_CAP_MHRE &&
51234e65944SIsaku Yamahata (pci_get_long(aer_cap + PCI_ERR_UNCOR_STATUS) & (1U << fep))) {
51334e65944SIsaku Yamahata /* Not first error. queue error */
51434e65944SIsaku Yamahata if (aer_log_add_err(&dev->exp.aer_log, err) < 0) {
51534e65944SIsaku Yamahata /* overflow */
51634e65944SIsaku Yamahata return -1;
51734e65944SIsaku Yamahata }
51834e65944SIsaku Yamahata return 0;
51934e65944SIsaku Yamahata }
52034e65944SIsaku Yamahata
52134e65944SIsaku Yamahata pcie_aer_update_log(dev, err);
52234e65944SIsaku Yamahata return 0;
52334e65944SIsaku Yamahata }
52434e65944SIsaku Yamahata
52534e65944SIsaku Yamahata typedef struct PCIEAERInject {
52634e65944SIsaku Yamahata PCIDevice *dev;
52734e65944SIsaku Yamahata uint8_t *aer_cap;
52834e65944SIsaku Yamahata const PCIEAERErr *err;
52934e65944SIsaku Yamahata uint16_t devctl;
53034e65944SIsaku Yamahata uint16_t devsta;
53134e65944SIsaku Yamahata uint32_t error_status;
53234e65944SIsaku Yamahata bool unsupported_request;
53334e65944SIsaku Yamahata bool log_overflow;
53434e65944SIsaku Yamahata PCIEAERMsg msg;
53534e65944SIsaku Yamahata } PCIEAERInject;
53634e65944SIsaku Yamahata
pcie_aer_inject_cor_error(PCIEAERInject * inj,uint32_t uncor_status,bool is_advisory_nonfatal)53734e65944SIsaku Yamahata static bool pcie_aer_inject_cor_error(PCIEAERInject *inj,
53834e65944SIsaku Yamahata uint32_t uncor_status,
53934e65944SIsaku Yamahata bool is_advisory_nonfatal)
54034e65944SIsaku Yamahata {
54134e65944SIsaku Yamahata PCIDevice *dev = inj->dev;
54234e65944SIsaku Yamahata
54334e65944SIsaku Yamahata inj->devsta |= PCI_EXP_DEVSTA_CED;
54434e65944SIsaku Yamahata if (inj->unsupported_request) {
54534e65944SIsaku Yamahata inj->devsta |= PCI_EXP_DEVSTA_URD;
54634e65944SIsaku Yamahata }
54734e65944SIsaku Yamahata pci_set_word(dev->config + dev->exp.exp_cap + PCI_EXP_DEVSTA, inj->devsta);
54834e65944SIsaku Yamahata
54934e65944SIsaku Yamahata if (inj->aer_cap) {
55034e65944SIsaku Yamahata uint32_t mask;
55134e65944SIsaku Yamahata pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_COR_STATUS,
55234e65944SIsaku Yamahata inj->error_status);
55334e65944SIsaku Yamahata mask = pci_get_long(inj->aer_cap + PCI_ERR_COR_MASK);
55434e65944SIsaku Yamahata if (mask & inj->error_status) {
55534e65944SIsaku Yamahata return false;
55634e65944SIsaku Yamahata }
55734e65944SIsaku Yamahata if (is_advisory_nonfatal) {
55834e65944SIsaku Yamahata uint32_t uncor_mask =
55934e65944SIsaku Yamahata pci_get_long(inj->aer_cap + PCI_ERR_UNCOR_MASK);
56034e65944SIsaku Yamahata if (!(uncor_mask & uncor_status)) {
56134e65944SIsaku Yamahata inj->log_overflow = !!pcie_aer_record_error(dev, inj->err);
56234e65944SIsaku Yamahata }
56334e65944SIsaku Yamahata pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_UNCOR_STATUS,
56434e65944SIsaku Yamahata uncor_status);
56534e65944SIsaku Yamahata }
56634e65944SIsaku Yamahata }
56734e65944SIsaku Yamahata
56834e65944SIsaku Yamahata if (inj->unsupported_request && !(inj->devctl & PCI_EXP_DEVCTL_URRE)) {
56934e65944SIsaku Yamahata return false;
57034e65944SIsaku Yamahata }
57134e65944SIsaku Yamahata if (!(inj->devctl & PCI_EXP_DEVCTL_CERE)) {
57234e65944SIsaku Yamahata return false;
57334e65944SIsaku Yamahata }
57434e65944SIsaku Yamahata
57534e65944SIsaku Yamahata inj->msg.severity = PCI_ERR_ROOT_CMD_COR_EN;
57634e65944SIsaku Yamahata return true;
57734e65944SIsaku Yamahata }
57834e65944SIsaku Yamahata
pcie_aer_inject_uncor_error(PCIEAERInject * inj,bool is_fatal)57934e65944SIsaku Yamahata static bool pcie_aer_inject_uncor_error(PCIEAERInject *inj, bool is_fatal)
58034e65944SIsaku Yamahata {
58134e65944SIsaku Yamahata PCIDevice *dev = inj->dev;
58234e65944SIsaku Yamahata uint16_t cmd;
58334e65944SIsaku Yamahata
58434e65944SIsaku Yamahata if (is_fatal) {
58534e65944SIsaku Yamahata inj->devsta |= PCI_EXP_DEVSTA_FED;
58634e65944SIsaku Yamahata } else {
58734e65944SIsaku Yamahata inj->devsta |= PCI_EXP_DEVSTA_NFED;
58834e65944SIsaku Yamahata }
58934e65944SIsaku Yamahata if (inj->unsupported_request) {
59034e65944SIsaku Yamahata inj->devsta |= PCI_EXP_DEVSTA_URD;
59134e65944SIsaku Yamahata }
59234e65944SIsaku Yamahata pci_set_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVSTA, inj->devsta);
59334e65944SIsaku Yamahata
59434e65944SIsaku Yamahata if (inj->aer_cap) {
59534e65944SIsaku Yamahata uint32_t mask = pci_get_long(inj->aer_cap + PCI_ERR_UNCOR_MASK);
59634e65944SIsaku Yamahata if (mask & inj->error_status) {
59734e65944SIsaku Yamahata pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_UNCOR_STATUS,
59834e65944SIsaku Yamahata inj->error_status);
59934e65944SIsaku Yamahata return false;
60034e65944SIsaku Yamahata }
60134e65944SIsaku Yamahata
60234e65944SIsaku Yamahata inj->log_overflow = !!pcie_aer_record_error(dev, inj->err);
60334e65944SIsaku Yamahata pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_UNCOR_STATUS,
60434e65944SIsaku Yamahata inj->error_status);
60534e65944SIsaku Yamahata }
60634e65944SIsaku Yamahata
60734e65944SIsaku Yamahata cmd = pci_get_word(dev->config + PCI_COMMAND);
60834e65944SIsaku Yamahata if (inj->unsupported_request &&
60934e65944SIsaku Yamahata !(inj->devctl & PCI_EXP_DEVCTL_URRE) && !(cmd & PCI_COMMAND_SERR)) {
61034e65944SIsaku Yamahata return false;
61134e65944SIsaku Yamahata }
61234e65944SIsaku Yamahata if (is_fatal) {
61334e65944SIsaku Yamahata if (!((cmd & PCI_COMMAND_SERR) ||
61434e65944SIsaku Yamahata (inj->devctl & PCI_EXP_DEVCTL_FERE))) {
61534e65944SIsaku Yamahata return false;
61634e65944SIsaku Yamahata }
61734e65944SIsaku Yamahata inj->msg.severity = PCI_ERR_ROOT_CMD_FATAL_EN;
61834e65944SIsaku Yamahata } else {
61934e65944SIsaku Yamahata if (!((cmd & PCI_COMMAND_SERR) ||
62034e65944SIsaku Yamahata (inj->devctl & PCI_EXP_DEVCTL_NFERE))) {
62134e65944SIsaku Yamahata return false;
62234e65944SIsaku Yamahata }
62334e65944SIsaku Yamahata inj->msg.severity = PCI_ERR_ROOT_CMD_NONFATAL_EN;
62434e65944SIsaku Yamahata }
62534e65944SIsaku Yamahata return true;
62634e65944SIsaku Yamahata }
62734e65944SIsaku Yamahata
62834e65944SIsaku Yamahata /*
62934e65944SIsaku Yamahata * non-Function specific error must be recorded in all functions.
63034e65944SIsaku Yamahata * It is the responsibility of the caller of this function.
631e8e3bb2fSStefan Weil * It is also caller's responsibility to determine which function should
632b01738c2SChen Fan * report the error.
63334e65944SIsaku Yamahata *
63434e65944SIsaku Yamahata * 6.2.4 Error Logging
635b01738c2SChen Fan * 6.2.5 Sequence of Device Error Signaling and Logging Operations
636ce394947SMichael S. Tsirkin * Figure 6-2: Flowchart Showing Sequence of Device Error Signaling and Logging
63734e65944SIsaku Yamahata * Operations
63834e65944SIsaku Yamahata */
pcie_aer_inject_error(PCIDevice * dev,const PCIEAERErr * err)639d0e67298SMarkus Armbruster int pcie_aer_inject_error(PCIDevice *dev, const PCIEAERErr *err)
64034e65944SIsaku Yamahata {
64134e65944SIsaku Yamahata uint8_t *aer_cap = NULL;
64234e65944SIsaku Yamahata uint16_t devctl = 0;
64334e65944SIsaku Yamahata uint16_t devsta = 0;
64434e65944SIsaku Yamahata uint32_t error_status = err->status;
64534e65944SIsaku Yamahata PCIEAERInject inj;
64634e65944SIsaku Yamahata
64734e65944SIsaku Yamahata if (!pci_is_express(dev)) {
64834e65944SIsaku Yamahata return -ENOSYS;
64934e65944SIsaku Yamahata }
65034e65944SIsaku Yamahata
65134e65944SIsaku Yamahata if (err->flags & PCIE_AER_ERR_IS_CORRECTABLE) {
65234e65944SIsaku Yamahata error_status &= PCI_ERR_COR_SUPPORTED;
65334e65944SIsaku Yamahata } else {
65434e65944SIsaku Yamahata error_status &= PCI_ERR_UNC_SUPPORTED;
65534e65944SIsaku Yamahata }
65634e65944SIsaku Yamahata
65734e65944SIsaku Yamahata /* invalid status bit. one and only one bit must be set */
65834e65944SIsaku Yamahata if (!error_status || (error_status & (error_status - 1))) {
65934e65944SIsaku Yamahata return -EINVAL;
66034e65944SIsaku Yamahata }
66134e65944SIsaku Yamahata
66234e65944SIsaku Yamahata if (dev->exp.aer_cap) {
66334e65944SIsaku Yamahata uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
66434e65944SIsaku Yamahata aer_cap = dev->config + dev->exp.aer_cap;
66534e65944SIsaku Yamahata devctl = pci_get_long(exp_cap + PCI_EXP_DEVCTL);
66634e65944SIsaku Yamahata devsta = pci_get_long(exp_cap + PCI_EXP_DEVSTA);
66734e65944SIsaku Yamahata }
66834e65944SIsaku Yamahata
66934e65944SIsaku Yamahata inj.dev = dev;
67034e65944SIsaku Yamahata inj.aer_cap = aer_cap;
67134e65944SIsaku Yamahata inj.err = err;
67234e65944SIsaku Yamahata inj.devctl = devctl;
67334e65944SIsaku Yamahata inj.devsta = devsta;
67434e65944SIsaku Yamahata inj.error_status = error_status;
67534e65944SIsaku Yamahata inj.unsupported_request = !(err->flags & PCIE_AER_ERR_IS_CORRECTABLE) &&
67634e65944SIsaku Yamahata err->status == PCI_ERR_UNC_UNSUP;
67734e65944SIsaku Yamahata inj.log_overflow = false;
67834e65944SIsaku Yamahata
67934e65944SIsaku Yamahata if (err->flags & PCIE_AER_ERR_IS_CORRECTABLE) {
68034e65944SIsaku Yamahata if (!pcie_aer_inject_cor_error(&inj, 0, false)) {
68134e65944SIsaku Yamahata return 0;
68234e65944SIsaku Yamahata }
68334e65944SIsaku Yamahata } else {
68434e65944SIsaku Yamahata bool is_fatal =
68534e65944SIsaku Yamahata pcie_aer_uncor_default_severity(error_status) ==
68634e65944SIsaku Yamahata PCI_ERR_ROOT_CMD_FATAL_EN;
68734e65944SIsaku Yamahata if (aer_cap) {
68834e65944SIsaku Yamahata is_fatal =
68934e65944SIsaku Yamahata error_status & pci_get_long(aer_cap + PCI_ERR_UNCOR_SEVER);
69034e65944SIsaku Yamahata }
69134e65944SIsaku Yamahata if (!is_fatal && (err->flags & PCIE_AER_ERR_MAYBE_ADVISORY)) {
69234e65944SIsaku Yamahata inj.error_status = PCI_ERR_COR_ADV_NONFATAL;
69334e65944SIsaku Yamahata if (!pcie_aer_inject_cor_error(&inj, error_status, true)) {
69434e65944SIsaku Yamahata return 0;
69534e65944SIsaku Yamahata }
69634e65944SIsaku Yamahata } else {
69734e65944SIsaku Yamahata if (!pcie_aer_inject_uncor_error(&inj, is_fatal)) {
69834e65944SIsaku Yamahata return 0;
69934e65944SIsaku Yamahata }
70034e65944SIsaku Yamahata }
70134e65944SIsaku Yamahata }
70234e65944SIsaku Yamahata
70334e65944SIsaku Yamahata /* send up error message */
70434e65944SIsaku Yamahata inj.msg.source_id = err->source_id;
70534e65944SIsaku Yamahata pcie_aer_msg(dev, &inj.msg);
70634e65944SIsaku Yamahata
70734e65944SIsaku Yamahata if (inj.log_overflow) {
70834e65944SIsaku Yamahata PCIEAERErr header_log_overflow = {
70934e65944SIsaku Yamahata .status = PCI_ERR_COR_HL_OVERFLOW,
71034e65944SIsaku Yamahata .flags = PCIE_AER_ERR_IS_CORRECTABLE,
71134e65944SIsaku Yamahata };
71234e65944SIsaku Yamahata int ret = pcie_aer_inject_error(dev, &header_log_overflow);
71334e65944SIsaku Yamahata assert(!ret);
71434e65944SIsaku Yamahata }
71534e65944SIsaku Yamahata return 0;
71634e65944SIsaku Yamahata }
71734e65944SIsaku Yamahata
pcie_aer_write_config(PCIDevice * dev,uint32_t addr,uint32_t val,int len)71834e65944SIsaku Yamahata void pcie_aer_write_config(PCIDevice *dev,
71934e65944SIsaku Yamahata uint32_t addr, uint32_t val, int len)
72034e65944SIsaku Yamahata {
72134e65944SIsaku Yamahata uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
72234e65944SIsaku Yamahata uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
72334e65944SIsaku Yamahata uint32_t first_error = 1U << PCI_ERR_CAP_FEP(errcap);
72434e65944SIsaku Yamahata uint32_t uncorsta = pci_get_long(aer_cap + PCI_ERR_UNCOR_STATUS);
72534e65944SIsaku Yamahata
72634e65944SIsaku Yamahata /* uncorrectable error */
72734e65944SIsaku Yamahata if (!(uncorsta & first_error)) {
72834e65944SIsaku Yamahata /* the bit that corresponds to the first error is cleared */
72934e65944SIsaku Yamahata pcie_aer_clear_error(dev);
73034e65944SIsaku Yamahata } else if (errcap & PCI_ERR_CAP_MHRE) {
73134e65944SIsaku Yamahata /* When PCI_ERR_CAP_MHRE is enabled and the first error isn't cleared
73234e65944SIsaku Yamahata * nothing should happen. So we have to revert the modification to
73334e65944SIsaku Yamahata * the register.
73434e65944SIsaku Yamahata */
73534e65944SIsaku Yamahata pcie_aer_update_uncor_status(dev);
73634e65944SIsaku Yamahata } else {
73734e65944SIsaku Yamahata /* capability & control
73834e65944SIsaku Yamahata * PCI_ERR_CAP_MHRE might be cleared, so clear of header log.
73934e65944SIsaku Yamahata */
74034e65944SIsaku Yamahata aer_log_clear_all_err(&dev->exp.aer_log);
74134e65944SIsaku Yamahata }
74234e65944SIsaku Yamahata }
74334e65944SIsaku Yamahata
pcie_aer_root_init(PCIDevice * dev)74434e65944SIsaku Yamahata void pcie_aer_root_init(PCIDevice *dev)
74534e65944SIsaku Yamahata {
74634e65944SIsaku Yamahata uint16_t pos = dev->exp.aer_cap;
74734e65944SIsaku Yamahata
74834e65944SIsaku Yamahata pci_set_long(dev->wmask + pos + PCI_ERR_ROOT_COMMAND,
74934e65944SIsaku Yamahata PCI_ERR_ROOT_CMD_EN_MASK);
75034e65944SIsaku Yamahata pci_set_long(dev->w1cmask + pos + PCI_ERR_ROOT_STATUS,
75134e65944SIsaku Yamahata PCI_ERR_ROOT_STATUS_REPORT_MASK);
7520e180d9cSJason Baron /* PCI_ERR_ROOT_IRQ is RO but devices change it using a
7530e180d9cSJason Baron * device-specific method.
7540e180d9cSJason Baron */
7550e180d9cSJason Baron pci_set_long(dev->cmask + pos + PCI_ERR_ROOT_STATUS,
7560e180d9cSJason Baron ~PCI_ERR_ROOT_IRQ);
75734e65944SIsaku Yamahata }
75834e65944SIsaku Yamahata
pcie_aer_root_reset(PCIDevice * dev)75934e65944SIsaku Yamahata void pcie_aer_root_reset(PCIDevice *dev)
76034e65944SIsaku Yamahata {
76134e65944SIsaku Yamahata uint8_t* aer_cap = dev->config + dev->exp.aer_cap;
76234e65944SIsaku Yamahata
76334e65944SIsaku Yamahata pci_set_long(aer_cap + PCI_ERR_ROOT_COMMAND, 0);
76434e65944SIsaku Yamahata
76534e65944SIsaku Yamahata /*
76634e65944SIsaku Yamahata * Advanced Error Interrupt Message Number in Root Error Status Register
76734e65944SIsaku Yamahata * must be updated by chip dependent code because it's chip dependent
76834e65944SIsaku Yamahata * which number is used.
76934e65944SIsaku Yamahata */
77034e65944SIsaku Yamahata }
77134e65944SIsaku Yamahata
pcie_aer_root_write_config(PCIDevice * dev,uint32_t addr,uint32_t val,int len,uint32_t root_cmd_prev)77234e65944SIsaku Yamahata void pcie_aer_root_write_config(PCIDevice *dev,
77334e65944SIsaku Yamahata uint32_t addr, uint32_t val, int len,
77434e65944SIsaku Yamahata uint32_t root_cmd_prev)
77534e65944SIsaku Yamahata {
77634e65944SIsaku Yamahata uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
77734e65944SIsaku Yamahata uint32_t root_status = pci_get_long(aer_cap + PCI_ERR_ROOT_STATUS);
7782b3cb353SMichael S. Tsirkin uint32_t enabled_cmd = pcie_aer_status_to_cmd(root_status);
7792b3cb353SMichael S. Tsirkin uint32_t root_cmd = pci_get_long(aer_cap + PCI_ERR_ROOT_COMMAND);
7802b3cb353SMichael S. Tsirkin /* 6.2.4.1.2 Interrupt Generation */
7812b3cb353SMichael S. Tsirkin if (!msix_enabled(dev) && !msi_enabled(dev)) {
78220766514SFrederic Barrat if (pci_intx(dev) != -1) {
7835a03e708SMarcel Apfelbaum pci_set_irq(dev, !!(root_cmd & enabled_cmd));
78420766514SFrederic Barrat }
7852b3cb353SMichael S. Tsirkin return;
7862b3cb353SMichael S. Tsirkin }
7872b3cb353SMichael S. Tsirkin
7882b3cb353SMichael S. Tsirkin if ((root_cmd_prev & enabled_cmd) || !(root_cmd & enabled_cmd)) {
7892b3cb353SMichael S. Tsirkin /* Send MSI on transition from false to true. */
7902b3cb353SMichael S. Tsirkin return;
7912b3cb353SMichael S. Tsirkin }
79234e65944SIsaku Yamahata
793513691b7SMichael S. Tsirkin pcie_aer_root_notify(dev);
79434e65944SIsaku Yamahata }
79534e65944SIsaku Yamahata
79634e65944SIsaku Yamahata static const VMStateDescription vmstate_pcie_aer_err = {
79734e65944SIsaku Yamahata .name = "PCIE_AER_ERROR",
79834e65944SIsaku Yamahata .version_id = 1,
79934e65944SIsaku Yamahata .minimum_version_id = 1,
800*8e5e0890SRichard Henderson .fields = (const VMStateField[]) {
80134e65944SIsaku Yamahata VMSTATE_UINT32(status, PCIEAERErr),
80234e65944SIsaku Yamahata VMSTATE_UINT16(source_id, PCIEAERErr),
80334e65944SIsaku Yamahata VMSTATE_UINT16(flags, PCIEAERErr),
80434e65944SIsaku Yamahata VMSTATE_UINT32_ARRAY(header, PCIEAERErr, 4),
80534e65944SIsaku Yamahata VMSTATE_UINT32_ARRAY(prefix, PCIEAERErr, 4),
80634e65944SIsaku Yamahata VMSTATE_END_OF_LIST()
80734e65944SIsaku Yamahata }
80834e65944SIsaku Yamahata };
80934e65944SIsaku Yamahata
pcie_aer_state_log_num_valid(void * opaque,int version_id)8105f691ff9SMichael S. Tsirkin static bool pcie_aer_state_log_num_valid(void *opaque, int version_id)
8115f691ff9SMichael S. Tsirkin {
8125f691ff9SMichael S. Tsirkin PCIEAERLog *s = opaque;
8135f691ff9SMichael S. Tsirkin
8145f691ff9SMichael S. Tsirkin return s->log_num <= s->log_max;
8155f691ff9SMichael S. Tsirkin }
8165f691ff9SMichael S. Tsirkin
81734e65944SIsaku Yamahata const VMStateDescription vmstate_pcie_aer_log = {
81834e65944SIsaku Yamahata .name = "PCIE_AER_ERROR_LOG",
81934e65944SIsaku Yamahata .version_id = 1,
82034e65944SIsaku Yamahata .minimum_version_id = 1,
821*8e5e0890SRichard Henderson .fields = (const VMStateField[]) {
82234e65944SIsaku Yamahata VMSTATE_UINT16(log_num, PCIEAERLog),
823d2164ad3SHalil Pasic VMSTATE_UINT16_EQUAL(log_max, PCIEAERLog, NULL),
8245f691ff9SMichael S. Tsirkin VMSTATE_VALIDATE("log_num <= log_max", pcie_aer_state_log_num_valid),
82547188700SDmitry Eremin-Solenikov VMSTATE_STRUCT_VARRAY_POINTER_UINT16(log, PCIEAERLog, log_num,
82634e65944SIsaku Yamahata vmstate_pcie_aer_err, PCIEAERErr),
82734e65944SIsaku Yamahata VMSTATE_END_OF_LIST()
82834e65944SIsaku Yamahata }
82934e65944SIsaku Yamahata };
8302ae63bdaSIsaku Yamahata
8312ae63bdaSIsaku Yamahata typedef struct PCIEAERErrorName {
8322ae63bdaSIsaku Yamahata const char *name;
8332ae63bdaSIsaku Yamahata uint32_t val;
8342ae63bdaSIsaku Yamahata bool correctable;
8352ae63bdaSIsaku Yamahata } PCIEAERErrorName;
8362ae63bdaSIsaku Yamahata
8372ae63bdaSIsaku Yamahata /*
83866a0a2cbSDong Xu Wang * AER error name -> value conversion table
8392ae63bdaSIsaku Yamahata * This naming scheme is same to linux aer-injection tool.
8402ae63bdaSIsaku Yamahata */
8412ae63bdaSIsaku Yamahata static const struct PCIEAERErrorName pcie_aer_error_list[] = {
8422ae63bdaSIsaku Yamahata {
8432ae63bdaSIsaku Yamahata .name = "DLP",
8442ae63bdaSIsaku Yamahata .val = PCI_ERR_UNC_DLP,
8452ae63bdaSIsaku Yamahata .correctable = false,
8462ae63bdaSIsaku Yamahata }, {
8472ae63bdaSIsaku Yamahata .name = "SDN",
8482ae63bdaSIsaku Yamahata .val = PCI_ERR_UNC_SDN,
8492ae63bdaSIsaku Yamahata .correctable = false,
8502ae63bdaSIsaku Yamahata }, {
8512ae63bdaSIsaku Yamahata .name = "POISON_TLP",
8522ae63bdaSIsaku Yamahata .val = PCI_ERR_UNC_POISON_TLP,
8532ae63bdaSIsaku Yamahata .correctable = false,
8542ae63bdaSIsaku Yamahata }, {
8552ae63bdaSIsaku Yamahata .name = "FCP",
8562ae63bdaSIsaku Yamahata .val = PCI_ERR_UNC_FCP,
8572ae63bdaSIsaku Yamahata .correctable = false,
8582ae63bdaSIsaku Yamahata }, {
8592ae63bdaSIsaku Yamahata .name = "COMP_TIME",
8602ae63bdaSIsaku Yamahata .val = PCI_ERR_UNC_COMP_TIME,
8612ae63bdaSIsaku Yamahata .correctable = false,
8622ae63bdaSIsaku Yamahata }, {
8632ae63bdaSIsaku Yamahata .name = "COMP_ABORT",
8642ae63bdaSIsaku Yamahata .val = PCI_ERR_UNC_COMP_ABORT,
8652ae63bdaSIsaku Yamahata .correctable = false,
8662ae63bdaSIsaku Yamahata }, {
8672ae63bdaSIsaku Yamahata .name = "UNX_COMP",
8682ae63bdaSIsaku Yamahata .val = PCI_ERR_UNC_UNX_COMP,
8692ae63bdaSIsaku Yamahata .correctable = false,
8702ae63bdaSIsaku Yamahata }, {
8712ae63bdaSIsaku Yamahata .name = "RX_OVER",
8722ae63bdaSIsaku Yamahata .val = PCI_ERR_UNC_RX_OVER,
8732ae63bdaSIsaku Yamahata .correctable = false,
8742ae63bdaSIsaku Yamahata }, {
8752ae63bdaSIsaku Yamahata .name = "MALF_TLP",
8762ae63bdaSIsaku Yamahata .val = PCI_ERR_UNC_MALF_TLP,
8772ae63bdaSIsaku Yamahata .correctable = false,
8782ae63bdaSIsaku Yamahata }, {
8792ae63bdaSIsaku Yamahata .name = "ECRC",
8802ae63bdaSIsaku Yamahata .val = PCI_ERR_UNC_ECRC,
8812ae63bdaSIsaku Yamahata .correctable = false,
8822ae63bdaSIsaku Yamahata }, {
8832ae63bdaSIsaku Yamahata .name = "UNSUP",
8842ae63bdaSIsaku Yamahata .val = PCI_ERR_UNC_UNSUP,
8852ae63bdaSIsaku Yamahata .correctable = false,
8862ae63bdaSIsaku Yamahata }, {
8872ae63bdaSIsaku Yamahata .name = "ACSV",
8882ae63bdaSIsaku Yamahata .val = PCI_ERR_UNC_ACSV,
8892ae63bdaSIsaku Yamahata .correctable = false,
8902ae63bdaSIsaku Yamahata }, {
8912ae63bdaSIsaku Yamahata .name = "INTN",
8922ae63bdaSIsaku Yamahata .val = PCI_ERR_UNC_INTN,
8932ae63bdaSIsaku Yamahata .correctable = false,
8942ae63bdaSIsaku Yamahata }, {
8952ae63bdaSIsaku Yamahata .name = "MCBTLP",
8962ae63bdaSIsaku Yamahata .val = PCI_ERR_UNC_MCBTLP,
8972ae63bdaSIsaku Yamahata .correctable = false,
8982ae63bdaSIsaku Yamahata }, {
8992ae63bdaSIsaku Yamahata .name = "ATOP_EBLOCKED",
9002ae63bdaSIsaku Yamahata .val = PCI_ERR_UNC_ATOP_EBLOCKED,
9012ae63bdaSIsaku Yamahata .correctable = false,
9022ae63bdaSIsaku Yamahata }, {
9032ae63bdaSIsaku Yamahata .name = "TLP_PRF_BLOCKED",
9042ae63bdaSIsaku Yamahata .val = PCI_ERR_UNC_TLP_PRF_BLOCKED,
9052ae63bdaSIsaku Yamahata .correctable = false,
9062ae63bdaSIsaku Yamahata }, {
9072ae63bdaSIsaku Yamahata .name = "RCVR",
9082ae63bdaSIsaku Yamahata .val = PCI_ERR_COR_RCVR,
9092ae63bdaSIsaku Yamahata .correctable = true,
9102ae63bdaSIsaku Yamahata }, {
9112ae63bdaSIsaku Yamahata .name = "BAD_TLP",
9122ae63bdaSIsaku Yamahata .val = PCI_ERR_COR_BAD_TLP,
9132ae63bdaSIsaku Yamahata .correctable = true,
9142ae63bdaSIsaku Yamahata }, {
9152ae63bdaSIsaku Yamahata .name = "BAD_DLLP",
9162ae63bdaSIsaku Yamahata .val = PCI_ERR_COR_BAD_DLLP,
9172ae63bdaSIsaku Yamahata .correctable = true,
9182ae63bdaSIsaku Yamahata }, {
9192ae63bdaSIsaku Yamahata .name = "REP_ROLL",
9202ae63bdaSIsaku Yamahata .val = PCI_ERR_COR_REP_ROLL,
9212ae63bdaSIsaku Yamahata .correctable = true,
9222ae63bdaSIsaku Yamahata }, {
9232ae63bdaSIsaku Yamahata .name = "REP_TIMER",
9242ae63bdaSIsaku Yamahata .val = PCI_ERR_COR_REP_TIMER,
9252ae63bdaSIsaku Yamahata .correctable = true,
9262ae63bdaSIsaku Yamahata }, {
9272ae63bdaSIsaku Yamahata .name = "ADV_NONFATAL",
9282ae63bdaSIsaku Yamahata .val = PCI_ERR_COR_ADV_NONFATAL,
9292ae63bdaSIsaku Yamahata .correctable = true,
9302ae63bdaSIsaku Yamahata }, {
9312ae63bdaSIsaku Yamahata .name = "INTERNAL",
9322ae63bdaSIsaku Yamahata .val = PCI_ERR_COR_INTERNAL,
9332ae63bdaSIsaku Yamahata .correctable = true,
9342ae63bdaSIsaku Yamahata }, {
9352ae63bdaSIsaku Yamahata .name = "HL_OVERFLOW",
9362ae63bdaSIsaku Yamahata .val = PCI_ERR_COR_HL_OVERFLOW,
9372ae63bdaSIsaku Yamahata .correctable = true,
9382ae63bdaSIsaku Yamahata },
9392ae63bdaSIsaku Yamahata };
9402ae63bdaSIsaku Yamahata
pcie_aer_parse_error_string(const char * error_name,uint32_t * status,bool * correctable)941d0e67298SMarkus Armbruster int pcie_aer_parse_error_string(const char *error_name,
9422ae63bdaSIsaku Yamahata uint32_t *status, bool *correctable)
9432ae63bdaSIsaku Yamahata {
9442ae63bdaSIsaku Yamahata int i;
9452ae63bdaSIsaku Yamahata
9462ae63bdaSIsaku Yamahata for (i = 0; i < ARRAY_SIZE(pcie_aer_error_list); i++) {
9472ae63bdaSIsaku Yamahata const PCIEAERErrorName *e = &pcie_aer_error_list[i];
9482ae63bdaSIsaku Yamahata if (strcmp(error_name, e->name)) {
9492ae63bdaSIsaku Yamahata continue;
9502ae63bdaSIsaku Yamahata }
9512ae63bdaSIsaku Yamahata
9522ae63bdaSIsaku Yamahata *status = e->val;
9532ae63bdaSIsaku Yamahata *correctable = e->correctable;
9542ae63bdaSIsaku Yamahata return 0;
9552ae63bdaSIsaku Yamahata }
9562ae63bdaSIsaku Yamahata return -EINVAL;
9572ae63bdaSIsaku Yamahata }
958