]> Git Repo - qemu.git/blob - hw/pcie_aer.c
pci/aer: remove dead code
[qemu.git] / hw / pcie_aer.c
1 /*
2  * pcie_aer.c
3  *
4  * Copyright (c) 2010 Isaku Yamahata <yamahata at valinux co jp>
5  *                    VA Linux Systems Japan K.K.
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 #include "sysemu.h"
22 #include "pci_bridge.h"
23 #include "pcie.h"
24 #include "msix.h"
25 #include "msi.h"
26 #include "pci_internals.h"
27 #include "pcie_regs.h"
28
29 //#define DEBUG_PCIE
30 #ifdef DEBUG_PCIE
31 # define PCIE_DPRINTF(fmt, ...)                                         \
32     fprintf(stderr, "%s:%d " fmt, __func__, __LINE__, ## __VA_ARGS__)
33 #else
34 # define PCIE_DPRINTF(fmt, ...) do {} while (0)
35 #endif
36 #define PCIE_DEV_PRINTF(dev, fmt, ...)                                  \
37     PCIE_DPRINTF("%s:%x "fmt, (dev)->name, (dev)->devfn, ## __VA_ARGS__)
38
39 /* From 6.2.7 Error Listing and Rules. Table 6-2, 6-3 and 6-4 */
40 static uint32_t pcie_aer_uncor_default_severity(uint32_t status)
41 {
42     switch (status) {
43     case PCI_ERR_UNC_INTN:
44     case PCI_ERR_UNC_DLP:
45     case PCI_ERR_UNC_SDN:
46     case PCI_ERR_UNC_RX_OVER:
47     case PCI_ERR_UNC_FCP:
48     case PCI_ERR_UNC_MALF_TLP:
49         return PCI_ERR_ROOT_CMD_FATAL_EN;
50     case PCI_ERR_UNC_POISON_TLP:
51     case PCI_ERR_UNC_ECRC:
52     case PCI_ERR_UNC_UNSUP:
53     case PCI_ERR_UNC_COMP_TIME:
54     case PCI_ERR_UNC_COMP_ABORT:
55     case PCI_ERR_UNC_UNX_COMP:
56     case PCI_ERR_UNC_ACSV:
57     case PCI_ERR_UNC_MCBTLP:
58     case PCI_ERR_UNC_ATOP_EBLOCKED:
59     case PCI_ERR_UNC_TLP_PRF_BLOCKED:
60         return PCI_ERR_ROOT_CMD_NONFATAL_EN;
61     default:
62         abort();
63         break;
64     }
65     return PCI_ERR_ROOT_CMD_FATAL_EN;
66 }
67
68 static int aer_log_add_err(PCIEAERLog *aer_log, const PCIEAERErr *err)
69 {
70     if (aer_log->log_num == aer_log->log_max) {
71         return -1;
72     }
73     memcpy(&aer_log->log[aer_log->log_num], err, sizeof *err);
74     aer_log->log_num++;
75     return 0;
76 }
77
78 static void aer_log_del_err(PCIEAERLog *aer_log, PCIEAERErr *err)
79 {
80     assert(aer_log->log_num);
81     *err = aer_log->log[0];
82     aer_log->log_num--;
83     memmove(&aer_log->log[0], &aer_log->log[1],
84             aer_log->log_num * sizeof *err);
85 }
86
87 static void aer_log_clear_all_err(PCIEAERLog *aer_log)
88 {
89     aer_log->log_num = 0;
90 }
91
92 int pcie_aer_init(PCIDevice *dev, uint16_t offset)
93 {
94     PCIExpressDevice *exp;
95
96     pcie_add_capability(dev, PCI_EXT_CAP_ID_ERR, PCI_ERR_VER,
97                         offset, PCI_ERR_SIZEOF);
98     exp = &dev->exp;
99     exp->aer_cap = offset;
100
101     /* log_max is property */
102     if (dev->exp.aer_log.log_max == PCIE_AER_LOG_MAX_UNSET) {
103         dev->exp.aer_log.log_max = PCIE_AER_LOG_MAX_DEFAULT;
104     }
105     /* clip down the value to avoid unreasobale memory usage */
106     if (dev->exp.aer_log.log_max > PCIE_AER_LOG_MAX_LIMIT) {
107         return -EINVAL;
108     }
109     dev->exp.aer_log.log = qemu_mallocz(sizeof dev->exp.aer_log.log[0] *
110                                         dev->exp.aer_log.log_max);
111
112     pci_set_long(dev->w1cmask + offset + PCI_ERR_UNCOR_STATUS,
113                  PCI_ERR_UNC_SUPPORTED);
114
115     pci_set_long(dev->config + offset + PCI_ERR_UNCOR_SEVER,
116                  PCI_ERR_UNC_SEVERITY_DEFAULT);
117     pci_set_long(dev->wmask + offset + PCI_ERR_UNCOR_SEVER,
118                  PCI_ERR_UNC_SUPPORTED);
119
120     pci_long_test_and_set_mask(dev->w1cmask + offset + PCI_ERR_COR_STATUS,
121                                PCI_ERR_COR_STATUS);
122
123     pci_set_long(dev->config + offset + PCI_ERR_COR_MASK,
124                  PCI_ERR_COR_MASK_DEFAULT);
125     pci_set_long(dev->wmask + offset + PCI_ERR_COR_MASK,
126                  PCI_ERR_COR_SUPPORTED);
127
128     /* capabilities and control. multiple header logging is supported */
129     if (dev->exp.aer_log.log_max > 0) {
130         pci_set_long(dev->config + offset + PCI_ERR_CAP,
131                      PCI_ERR_CAP_ECRC_GENC | PCI_ERR_CAP_ECRC_CHKC |
132                      PCI_ERR_CAP_MHRC);
133         pci_set_long(dev->wmask + offset + PCI_ERR_CAP,
134                      PCI_ERR_CAP_ECRC_GENE | PCI_ERR_CAP_ECRC_CHKE |
135                      PCI_ERR_CAP_MHRE);
136     } else {
137         pci_set_long(dev->config + offset + PCI_ERR_CAP,
138                      PCI_ERR_CAP_ECRC_GENC | PCI_ERR_CAP_ECRC_CHKC);
139         pci_set_long(dev->wmask + offset + PCI_ERR_CAP,
140                      PCI_ERR_CAP_ECRC_GENE | PCI_ERR_CAP_ECRC_CHKE);
141     }
142
143     switch (pcie_cap_get_type(dev)) {
144     case PCI_EXP_TYPE_ROOT_PORT:
145         /* this case will be set by pcie_aer_root_init() */
146         /* fallthrough */
147     case PCI_EXP_TYPE_DOWNSTREAM:
148     case PCI_EXP_TYPE_UPSTREAM:
149         pci_word_test_and_set_mask(dev->wmask + PCI_BRIDGE_CONTROL,
150                                    PCI_BRIDGE_CTL_SERR);
151         pci_long_test_and_set_mask(dev->w1cmask + PCI_STATUS,
152                                    PCI_SEC_STATUS_RCV_SYSTEM_ERROR);
153         break;
154     default:
155         /* nothing */
156         break;
157     }
158     return 0;
159 }
160
161 void pcie_aer_exit(PCIDevice *dev)
162 {
163     qemu_free(dev->exp.aer_log.log);
164 }
165
166 static void pcie_aer_update_uncor_status(PCIDevice *dev)
167 {
168     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
169     PCIEAERLog *aer_log = &dev->exp.aer_log;
170
171     uint16_t i;
172     for (i = 0; i < aer_log->log_num; i++) {
173         pci_long_test_and_set_mask(aer_cap + PCI_ERR_UNCOR_STATUS,
174                                    dev->exp.aer_log.log[i].status);
175     }
176 }
177
178 /*
179  * return value:
180  * true: error message needs to be sent up
181  * false: error message is masked
182  *
183  * 6.2.6 Error Message Control
184  * Figure 6-3
185  * all pci express devices part
186  */
187 static bool
188 pcie_aer_msg_alldev(PCIDevice *dev, const PCIEAERMsg *msg)
189 {
190     if (!(pcie_aer_msg_is_uncor(msg) &&
191           (pci_get_word(dev->config + PCI_COMMAND) & PCI_COMMAND_SERR))) {
192         return false;
193     }
194
195     /* Signaled System Error
196      *
197      * 7.5.1.1 Command register
198      * Bit 8 SERR# Enable
199      *
200      * When Set, this bit enables reporting of Non-fatal and Fatal
201      * errors detected by the Function to the Root Complex. Note that
202      * errors are reported if enabled either through this bit or through
203      * the PCI Express specific bits in the Device Control register (see
204      * Section 7.8.4).
205      */
206     pci_word_test_and_set_mask(dev->config + PCI_STATUS,
207                                PCI_STATUS_SIG_SYSTEM_ERROR);
208
209     if (!(msg->severity &
210           pci_get_word(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL))) {
211         return false;
212     }
213
214     /* send up error message */
215     return true;
216 }
217
218 /*
219  * return value:
220  * true: error message is sent up
221  * false: error message is masked
222  *
223  * 6.2.6 Error Message Control
224  * Figure 6-3
225  * virtual pci bridge part
226  */
227 static bool pcie_aer_msg_vbridge(PCIDevice *dev, const PCIEAERMsg *msg)
228 {
229     uint16_t bridge_control = pci_get_word(dev->config + PCI_BRIDGE_CONTROL);
230
231     if (pcie_aer_msg_is_uncor(msg)) {
232         /* Received System Error */
233         pci_word_test_and_set_mask(dev->config + PCI_SEC_STATUS,
234                                    PCI_SEC_STATUS_RCV_SYSTEM_ERROR);
235     }
236
237     if (!(bridge_control & PCI_BRIDGE_CTL_SERR)) {
238         return false;
239     }
240     return true;
241 }
242
243 void pcie_aer_root_set_vector(PCIDevice *dev, unsigned int vector)
244 {
245     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
246     assert(vector < PCI_ERR_ROOT_IRQ_MAX);
247     pci_long_test_and_clear_mask(aer_cap + PCI_ERR_ROOT_STATUS,
248                                  PCI_ERR_ROOT_IRQ);
249     pci_long_test_and_set_mask(aer_cap + PCI_ERR_ROOT_STATUS,
250                                vector << PCI_ERR_ROOT_IRQ_SHIFT);
251 }
252
253 static unsigned int pcie_aer_root_get_vector(PCIDevice *dev)
254 {
255     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
256     uint32_t root_status = pci_get_long(aer_cap + PCI_ERR_ROOT_STATUS);
257     return (root_status & PCI_ERR_ROOT_IRQ) >> PCI_ERR_ROOT_IRQ_SHIFT;
258 }
259
260 /* Given a status register, get corresponding bits in the command register */
261 static uint32_t pcie_aer_status_to_cmd(uint32_t status)
262 {
263     uint32_t cmd = 0;
264     if (status & PCI_ERR_ROOT_COR_RCV) {
265         cmd |= PCI_ERR_ROOT_CMD_COR_EN;
266     }
267     if (status & PCI_ERR_ROOT_NONFATAL_RCV) {
268         cmd |= PCI_ERR_ROOT_CMD_NONFATAL_EN;
269     }
270     if (status & PCI_ERR_ROOT_FATAL_RCV) {
271         cmd |= PCI_ERR_ROOT_CMD_FATAL_EN;
272     }
273     return cmd;
274 }
275
276 /*
277  * 6.2.6 Error Message Control
278  * Figure 6-3
279  * root port part
280  */
281 static void pcie_aer_msg_root_port(PCIDevice *dev, const PCIEAERMsg *msg)
282 {
283     uint16_t cmd;
284     uint8_t *aer_cap;
285     uint32_t root_cmd;
286     uint32_t root_status, prev_status;
287
288     cmd = pci_get_word(dev->config + PCI_COMMAND);
289     aer_cap = dev->config + dev->exp.aer_cap;
290     root_cmd = pci_get_long(aer_cap + PCI_ERR_ROOT_COMMAND);
291     prev_status = root_status = pci_get_long(aer_cap + PCI_ERR_ROOT_STATUS);
292
293     if (cmd & PCI_COMMAND_SERR) {
294         /* System Error.
295          *
296          * The way to report System Error is platform specific and
297          * it isn't implemented in qemu right now.
298          * So just discard the error for now.
299          * OS which cares of aer would receive errors via
300          * native aer mechanims, so this wouldn't matter.
301          */
302     }
303
304     /* Errro Message Received: Root Error Status register */
305     switch (msg->severity) {
306     case PCI_ERR_ROOT_CMD_COR_EN:
307         if (root_status & PCI_ERR_ROOT_COR_RCV) {
308             root_status |= PCI_ERR_ROOT_MULTI_COR_RCV;
309         } else {
310             pci_set_word(aer_cap + PCI_ERR_ROOT_COR_SRC, msg->source_id);
311         }
312         root_status |= PCI_ERR_ROOT_COR_RCV;
313         break;
314     case PCI_ERR_ROOT_CMD_NONFATAL_EN:
315         root_status |= PCI_ERR_ROOT_NONFATAL_RCV;
316         break;
317     case PCI_ERR_ROOT_CMD_FATAL_EN:
318         if (!(root_status & PCI_ERR_ROOT_UNCOR_RCV)) {
319             root_status |= PCI_ERR_ROOT_FIRST_FATAL;
320         }
321         root_status |= PCI_ERR_ROOT_FATAL_RCV;
322         break;
323     default:
324         abort();
325         break;
326     }
327     if (pcie_aer_msg_is_uncor(msg)) {
328         if (root_status & PCI_ERR_ROOT_UNCOR_RCV) {
329             root_status |= PCI_ERR_ROOT_MULTI_UNCOR_RCV;
330         } else {
331             pci_set_word(aer_cap + PCI_ERR_ROOT_SRC, msg->source_id);
332         }
333         root_status |= PCI_ERR_ROOT_UNCOR_RCV;
334     }
335     pci_set_long(aer_cap + PCI_ERR_ROOT_STATUS, root_status);
336
337     /* 6.2.4.1.2 Interrupt Generation */
338     /* All the above did was set some bits in the status register.
339      * Specifically these that match message severity.
340      * The below code relies on this fact. */
341     if (!(root_cmd & msg->severity) ||
342         (pcie_aer_status_to_cmd(prev_status) & root_cmd)) {
343         /* Condition is not being set or was already true so nothing to do. */
344         return;
345     }
346
347     if (msix_enabled(dev)) {
348         msix_notify(dev, pcie_aer_root_get_vector(dev));
349     } else if (msi_enabled(dev)) {
350         msi_notify(dev, pcie_aer_root_get_vector(dev));
351     } else {
352         qemu_set_irq(dev->irq[dev->exp.aer_intx], 1);
353     }
354 }
355
356 /*
357  * 6.2.6 Error Message Control Figure 6-3
358  *
359  * Walk up the bus tree from the device, propagate the error message.
360  */
361 static void pcie_aer_msg(PCIDevice *dev, const PCIEAERMsg *msg)
362 {
363     uint8_t type;
364
365     while (dev) {
366         if (!pci_is_express(dev)) {
367             /* just ignore it */
368             /* TODO: Shouldn't we set PCI_STATUS_SIG_SYSTEM_ERROR?
369              * Consider e.g. a PCI bridge above a PCI Express device. */
370             return;
371         }
372
373         type = pcie_cap_get_type(dev);
374         if ((type == PCI_EXP_TYPE_ROOT_PORT ||
375             type == PCI_EXP_TYPE_UPSTREAM ||
376             type == PCI_EXP_TYPE_DOWNSTREAM) &&
377             !pcie_aer_msg_vbridge(dev, msg)) {
378                 return;
379         }
380         if (!pcie_aer_msg_alldev(dev, msg)) {
381             return;
382         }
383         if (type == PCI_EXP_TYPE_ROOT_PORT) {
384             pcie_aer_msg_root_port(dev, msg);
385             /* Root port can notify system itself,
386                or send the error message to root complex event collector. */
387             /*
388              * if root port is associated with an event collector,
389              * return the root complex event collector here.
390              * For now root complex event collector isn't supported.
391              */
392             return;
393         }
394         dev = pci_bridge_get_device(dev->bus);
395     }
396 }
397
398 static void pcie_aer_update_log(PCIDevice *dev, const PCIEAERErr *err)
399 {
400     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
401     uint8_t first_bit = ffs(err->status) - 1;
402     uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
403     int i;
404
405     assert(err->status);
406     assert(err->status & (err->status - 1));
407
408     errcap &= ~(PCI_ERR_CAP_FEP_MASK | PCI_ERR_CAP_TLP);
409     errcap |= PCI_ERR_CAP_FEP(first_bit);
410
411     if (err->flags & PCIE_AER_ERR_HEADER_VALID) {
412         for (i = 0; i < ARRAY_SIZE(err->header); ++i) {
413             /* 7.10.8 Header Log Register */
414             uint8_t *header_log =
415                 aer_cap + PCI_ERR_HEADER_LOG + i * sizeof err->header[0];
416             cpu_to_be32wu((uint32_t*)header_log, err->header[i]);
417         }
418     } else {
419         assert(!(err->flags & PCIE_AER_ERR_TLP_PREFIX_PRESENT));
420         memset(aer_cap + PCI_ERR_HEADER_LOG, 0, PCI_ERR_HEADER_LOG_SIZE);
421     }
422
423     if ((err->flags & PCIE_AER_ERR_TLP_PREFIX_PRESENT) &&
424         (pci_get_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVCTL2) &
425          PCI_EXP_DEVCAP2_EETLPP)) {
426         for (i = 0; i < ARRAY_SIZE(err->prefix); ++i) {
427             /* 7.10.12 tlp prefix log register */
428             uint8_t *prefix_log =
429                 aer_cap + PCI_ERR_TLP_PREFIX_LOG + i * sizeof err->prefix[0];
430             cpu_to_be32wu((uint32_t*)prefix_log, err->prefix[i]);
431         }
432         errcap |= PCI_ERR_CAP_TLP;
433     } else {
434         memset(aer_cap + PCI_ERR_TLP_PREFIX_LOG, 0,
435                PCI_ERR_TLP_PREFIX_LOG_SIZE);
436     }
437     pci_set_long(aer_cap + PCI_ERR_CAP, errcap);
438 }
439
440 static void pcie_aer_clear_log(PCIDevice *dev)
441 {
442     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
443
444     pci_long_test_and_clear_mask(aer_cap + PCI_ERR_CAP,
445                                  PCI_ERR_CAP_FEP_MASK | PCI_ERR_CAP_TLP);
446
447     memset(aer_cap + PCI_ERR_HEADER_LOG, 0, PCI_ERR_HEADER_LOG_SIZE);
448     memset(aer_cap + PCI_ERR_TLP_PREFIX_LOG, 0, PCI_ERR_TLP_PREFIX_LOG_SIZE);
449 }
450
451 static void pcie_aer_clear_error(PCIDevice *dev)
452 {
453     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
454     uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
455     PCIEAERLog *aer_log = &dev->exp.aer_log;
456     PCIEAERErr err;
457
458     if (!(errcap & PCI_ERR_CAP_MHRE) || !aer_log->log_num) {
459         pcie_aer_clear_log(dev);
460         return;
461     }
462
463     /*
464      * If more errors are queued, set corresponding bits in uncorrectable
465      * error status.
466      * We emulate uncorrectable error status register as W1CS.
467      * So set bit in uncorrectable error status here again for multiple
468      * error recording support.
469      *
470      * 6.2.4.2 Multiple Error Handling(Advanced Error Reporting Capability)
471      */
472     pcie_aer_update_uncor_status(dev);
473
474     aer_log_del_err(aer_log, &err);
475     pcie_aer_update_log(dev, &err);
476 }
477
478 static int pcie_aer_record_error(PCIDevice *dev,
479                                  const PCIEAERErr *err)
480 {
481     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
482     uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
483     int fep = PCI_ERR_CAP_FEP(errcap);
484
485     assert(err->status);
486     assert(err->status & (err->status - 1));
487
488     if (errcap & PCI_ERR_CAP_MHRE &&
489         (pci_get_long(aer_cap + PCI_ERR_UNCOR_STATUS) & (1U << fep))) {
490         /*  Not first error. queue error */
491         if (aer_log_add_err(&dev->exp.aer_log, err) < 0) {
492             /* overflow */
493             return -1;
494         }
495         return 0;
496     }
497
498     pcie_aer_update_log(dev, err);
499     return 0;
500 }
501
502 typedef struct PCIEAERInject {
503     PCIDevice *dev;
504     uint8_t *aer_cap;
505     const PCIEAERErr *err;
506     uint16_t devctl;
507     uint16_t devsta;
508     uint32_t error_status;
509     bool unsupported_request;
510     bool log_overflow;
511     PCIEAERMsg msg;
512 } PCIEAERInject;
513
514 static bool pcie_aer_inject_cor_error(PCIEAERInject *inj,
515                                       uint32_t uncor_status,
516                                       bool is_advisory_nonfatal)
517 {
518     PCIDevice *dev = inj->dev;
519
520     inj->devsta |= PCI_EXP_DEVSTA_CED;
521     if (inj->unsupported_request) {
522         inj->devsta |= PCI_EXP_DEVSTA_URD;
523     }
524     pci_set_word(dev->config + dev->exp.exp_cap + PCI_EXP_DEVSTA, inj->devsta);
525
526     if (inj->aer_cap) {
527         uint32_t mask;
528         pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_COR_STATUS,
529                                    inj->error_status);
530         mask = pci_get_long(inj->aer_cap + PCI_ERR_COR_MASK);
531         if (mask & inj->error_status) {
532             return false;
533         }
534         if (is_advisory_nonfatal) {
535             uint32_t uncor_mask =
536                 pci_get_long(inj->aer_cap + PCI_ERR_UNCOR_MASK);
537             if (!(uncor_mask & uncor_status)) {
538                 inj->log_overflow = !!pcie_aer_record_error(dev, inj->err);
539             }
540             pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_UNCOR_STATUS,
541                                        uncor_status);
542         }
543     }
544
545     if (inj->unsupported_request && !(inj->devctl & PCI_EXP_DEVCTL_URRE)) {
546         return false;
547     }
548     if (!(inj->devctl & PCI_EXP_DEVCTL_CERE)) {
549         return false;
550     }
551
552     inj->msg.severity = PCI_ERR_ROOT_CMD_COR_EN;
553     return true;
554 }
555
556 static bool pcie_aer_inject_uncor_error(PCIEAERInject *inj, bool is_fatal)
557 {
558     PCIDevice *dev = inj->dev;
559     uint16_t cmd;
560
561     if (is_fatal) {
562         inj->devsta |= PCI_EXP_DEVSTA_FED;
563     } else {
564         inj->devsta |= PCI_EXP_DEVSTA_NFED;
565     }
566     if (inj->unsupported_request) {
567         inj->devsta |= PCI_EXP_DEVSTA_URD;
568     }
569     pci_set_long(dev->config + dev->exp.exp_cap + PCI_EXP_DEVSTA, inj->devsta);
570
571     if (inj->aer_cap) {
572         uint32_t mask = pci_get_long(inj->aer_cap + PCI_ERR_UNCOR_MASK);
573         if (mask & inj->error_status) {
574             pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_UNCOR_STATUS,
575                                        inj->error_status);
576             return false;
577         }
578
579         inj->log_overflow = !!pcie_aer_record_error(dev, inj->err);
580         pci_long_test_and_set_mask(inj->aer_cap + PCI_ERR_UNCOR_STATUS,
581                                    inj->error_status);
582     }
583
584     cmd = pci_get_word(dev->config + PCI_COMMAND);
585     if (inj->unsupported_request &&
586         !(inj->devctl & PCI_EXP_DEVCTL_URRE) && !(cmd & PCI_COMMAND_SERR)) {
587         return false;
588     }
589     if (is_fatal) {
590         if (!((cmd & PCI_COMMAND_SERR) ||
591               (inj->devctl & PCI_EXP_DEVCTL_FERE))) {
592             return false;
593         }
594         inj->msg.severity = PCI_ERR_ROOT_CMD_FATAL_EN;
595     } else {
596         if (!((cmd & PCI_COMMAND_SERR) ||
597               (inj->devctl & PCI_EXP_DEVCTL_NFERE))) {
598             return false;
599         }
600         inj->msg.severity = PCI_ERR_ROOT_CMD_NONFATAL_EN;
601     }
602     return true;
603 }
604
605 /*
606  * non-Function specific error must be recorded in all functions.
607  * It is the responsibility of the caller of this function.
608  * It is also caller's responsiblity to determine which function should
609  * report the rerror.
610  *
611  * 6.2.4 Error Logging
612  * 6.2.5 Sqeunce of Device Error Signaling and Logging Operations
613  * table 6-2: Flowchard Showing Sequence of Device Error Signaling and Logging
614  *            Operations
615  */
616 int pcie_aer_inject_error(PCIDevice *dev, const PCIEAERErr *err)
617 {
618     uint8_t *aer_cap = NULL;
619     uint16_t devctl = 0;
620     uint16_t devsta = 0;
621     uint32_t error_status = err->status;
622     PCIEAERInject inj;
623
624     if (!pci_is_express(dev)) {
625         return -ENOSYS;
626     }
627
628     if (err->flags & PCIE_AER_ERR_IS_CORRECTABLE) {
629         error_status &= PCI_ERR_COR_SUPPORTED;
630     } else {
631         error_status &= PCI_ERR_UNC_SUPPORTED;
632     }
633
634     /* invalid status bit. one and only one bit must be set */
635     if (!error_status || (error_status & (error_status - 1))) {
636         return -EINVAL;
637     }
638
639     if (dev->exp.aer_cap) {
640         uint8_t *exp_cap = dev->config + dev->exp.exp_cap;
641         aer_cap = dev->config + dev->exp.aer_cap;
642         devctl = pci_get_long(exp_cap + PCI_EXP_DEVCTL);
643         devsta = pci_get_long(exp_cap + PCI_EXP_DEVSTA);
644     }
645
646     inj.dev = dev;
647     inj.aer_cap = aer_cap;
648     inj.err = err;
649     inj.devctl = devctl;
650     inj.devsta = devsta;
651     inj.error_status = error_status;
652     inj.unsupported_request = !(err->flags & PCIE_AER_ERR_IS_CORRECTABLE) &&
653         err->status == PCI_ERR_UNC_UNSUP;
654     inj.log_overflow = false;
655
656     if (err->flags & PCIE_AER_ERR_IS_CORRECTABLE) {
657         if (!pcie_aer_inject_cor_error(&inj, 0, false)) {
658             return 0;
659         }
660     } else {
661         bool is_fatal =
662             pcie_aer_uncor_default_severity(error_status) ==
663             PCI_ERR_ROOT_CMD_FATAL_EN;
664         if (aer_cap) {
665             is_fatal =
666                 error_status & pci_get_long(aer_cap + PCI_ERR_UNCOR_SEVER);
667         }
668         if (!is_fatal && (err->flags & PCIE_AER_ERR_MAYBE_ADVISORY)) {
669             inj.error_status = PCI_ERR_COR_ADV_NONFATAL;
670             if (!pcie_aer_inject_cor_error(&inj, error_status, true)) {
671                 return 0;
672             }
673         } else {
674             if (!pcie_aer_inject_uncor_error(&inj, is_fatal)) {
675                 return 0;
676             }
677         }
678     }
679
680     /* send up error message */
681     inj.msg.source_id = err->source_id;
682     pcie_aer_msg(dev, &inj.msg);
683
684     if (inj.log_overflow) {
685         PCIEAERErr header_log_overflow = {
686             .status = PCI_ERR_COR_HL_OVERFLOW,
687             .flags = PCIE_AER_ERR_IS_CORRECTABLE,
688         };
689         int ret = pcie_aer_inject_error(dev, &header_log_overflow);
690         assert(!ret);
691     }
692     return 0;
693 }
694
695 void pcie_aer_write_config(PCIDevice *dev,
696                            uint32_t addr, uint32_t val, int len)
697 {
698     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
699     uint32_t errcap = pci_get_long(aer_cap + PCI_ERR_CAP);
700     uint32_t first_error = 1U << PCI_ERR_CAP_FEP(errcap);
701     uint32_t uncorsta = pci_get_long(aer_cap + PCI_ERR_UNCOR_STATUS);
702
703     /* uncorrectable error */
704     if (!(uncorsta & first_error)) {
705         /* the bit that corresponds to the first error is cleared */
706         pcie_aer_clear_error(dev);
707     } else if (errcap & PCI_ERR_CAP_MHRE) {
708         /* When PCI_ERR_CAP_MHRE is enabled and the first error isn't cleared
709          * nothing should happen. So we have to revert the modification to
710          * the register.
711          */
712         pcie_aer_update_uncor_status(dev);
713     } else {
714         /* capability & control
715          * PCI_ERR_CAP_MHRE might be cleared, so clear of header log.
716          */
717         aer_log_clear_all_err(&dev->exp.aer_log);
718     }
719 }
720
721 void pcie_aer_root_init(PCIDevice *dev)
722 {
723     uint16_t pos = dev->exp.aer_cap;
724
725     pci_set_long(dev->wmask + pos + PCI_ERR_ROOT_COMMAND,
726                  PCI_ERR_ROOT_CMD_EN_MASK);
727     pci_set_long(dev->w1cmask + pos + PCI_ERR_ROOT_STATUS,
728                  PCI_ERR_ROOT_STATUS_REPORT_MASK);
729 }
730
731 void pcie_aer_root_reset(PCIDevice *dev)
732 {
733     uint8_t* aer_cap = dev->config + dev->exp.aer_cap;
734
735     pci_set_long(aer_cap + PCI_ERR_ROOT_COMMAND, 0);
736
737     /*
738      * Advanced Error Interrupt Message Number in Root Error Status Register
739      * must be updated by chip dependent code because it's chip dependent
740      * which number is used.
741      */
742 }
743
744 void pcie_aer_root_write_config(PCIDevice *dev,
745                                 uint32_t addr, uint32_t val, int len,
746                                 uint32_t root_cmd_prev)
747 {
748     uint8_t *aer_cap = dev->config + dev->exp.aer_cap;
749     uint32_t root_status = pci_get_long(aer_cap + PCI_ERR_ROOT_STATUS);
750     uint32_t enabled_cmd = pcie_aer_status_to_cmd(root_status);
751     uint32_t root_cmd = pci_get_long(aer_cap + PCI_ERR_ROOT_COMMAND);
752     /* 6.2.4.1.2 Interrupt Generation */
753     if (!msix_enabled(dev) && !msi_enabled(dev)) {
754         qemu_set_irq(dev->irq[dev->exp.aer_intx], !!(root_cmd & enabled_cmd));
755         return;
756     }
757
758     if ((root_cmd_prev & enabled_cmd) || !(root_cmd & enabled_cmd)) {
759         /* Send MSI on transition from false to true. */
760         return;
761     }
762
763     if (msix_enabled(dev)) {
764         msix_notify(dev, pcie_aer_root_get_vector(dev));
765     } else if (msi_enabled(dev)) {
766         msi_notify(dev, pcie_aer_root_get_vector(dev));
767     } else {
768         abort();
769     }
770 }
771
772 static const VMStateDescription vmstate_pcie_aer_err = {
773     .name = "PCIE_AER_ERROR",
774     .version_id = 1,
775     .minimum_version_id = 1,
776     .minimum_version_id_old = 1,
777     .fields     = (VMStateField[]) {
778         VMSTATE_UINT32(status, PCIEAERErr),
779         VMSTATE_UINT16(source_id, PCIEAERErr),
780         VMSTATE_UINT16(flags, PCIEAERErr),
781         VMSTATE_UINT32_ARRAY(header, PCIEAERErr, 4),
782         VMSTATE_UINT32_ARRAY(prefix, PCIEAERErr, 4),
783         VMSTATE_END_OF_LIST()
784     }
785 };
786
787 #define VMSTATE_PCIE_AER_ERRS(_field, _state, _field_num, _vmsd, _type) { \
788     .name       = (stringify(_field)),                                    \
789     .version_id = 0,                                                      \
790     .num_offset = vmstate_offset_value(_state, _field_num, uint16_t),     \
791     .size       = sizeof(_type),                                          \
792     .vmsd       = &(_vmsd),                                               \
793     .flags      = VMS_POINTER | VMS_VARRAY_UINT16 | VMS_STRUCT,           \
794     .offset     = vmstate_offset_pointer(_state, _field, _type),          \
795 }
796
797 const VMStateDescription vmstate_pcie_aer_log = {
798     .name = "PCIE_AER_ERROR_LOG",
799     .version_id = 1,
800     .minimum_version_id = 1,
801     .minimum_version_id_old = 1,
802     .fields     = (VMStateField[]) {
803         VMSTATE_UINT16(log_num, PCIEAERLog),
804         VMSTATE_UINT16(log_max, PCIEAERLog),
805         VMSTATE_PCIE_AER_ERRS(log, PCIEAERLog, log_num,
806                               vmstate_pcie_aer_err, PCIEAERErr),
807         VMSTATE_END_OF_LIST()
808     }
809 };
This page took 0.072481 seconds and 4 git commands to generate.