2 * The file intends to implement the platform dependent EEH operations on pseries.
3 * Actually, the pseries platform is built based on RTAS heavily. That means the
4 * pseries platform dependent EEH operations will be built on RTAS calls. The functions
5 * are devired from arch/powerpc/platforms/pseries/eeh.c and necessary cleanup has
8 * Copyright Benjamin Herrenschmidt & Gavin Shan, IBM Corporation 2011.
9 * Copyright IBM Corporation 2001, 2005, 2006
10 * Copyright Dave Engebretsen & Todd Inglett 2001
11 * Copyright Linas Vepstas 2005, 2006
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <linux/atomic.h>
29 #include <linux/delay.h>
30 #include <linux/export.h>
31 #include <linux/init.h>
32 #include <linux/list.h>
34 #include <linux/pci.h>
35 #include <linux/proc_fs.h>
36 #include <linux/rbtree.h>
37 #include <linux/sched.h>
38 #include <linux/seq_file.h>
39 #include <linux/spinlock.h>
42 #include <asm/eeh_event.h>
44 #include <asm/machdep.h>
45 #include <asm/ppc-pci.h>
49 static int ibm_set_eeh_option;
50 static int ibm_set_slot_reset;
51 static int ibm_read_slot_reset_state;
52 static int ibm_read_slot_reset_state2;
53 static int ibm_slot_error_detail;
54 static int ibm_get_config_addr_info;
55 static int ibm_get_config_addr_info2;
56 static int ibm_configure_bridge;
57 static int ibm_configure_pe;
60 * Buffer for reporting slot-error-detail rtas calls. Its here
61 * in BSS, and not dynamically alloced, so that it ends up in
62 * RMO where RTAS can access it.
64 static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
65 static DEFINE_SPINLOCK(slot_errbuf_lock);
66 static int eeh_error_buf_size;
69 * pseries_eeh_init - EEH platform dependent initialization
71 * EEH platform dependent initialization on pseries.
73 static int pseries_eeh_init(void)
75 /* figure out EEH RTAS function call tokens */
76 ibm_set_eeh_option = rtas_token("ibm,set-eeh-option");
77 ibm_set_slot_reset = rtas_token("ibm,set-slot-reset");
78 ibm_read_slot_reset_state2 = rtas_token("ibm,read-slot-reset-state2");
79 ibm_read_slot_reset_state = rtas_token("ibm,read-slot-reset-state");
80 ibm_slot_error_detail = rtas_token("ibm,slot-error-detail");
81 ibm_get_config_addr_info2 = rtas_token("ibm,get-config-addr-info2");
82 ibm_get_config_addr_info = rtas_token("ibm,get-config-addr-info");
83 ibm_configure_pe = rtas_token("ibm,configure-pe");
84 ibm_configure_bridge = rtas_token("ibm,configure-bridge");
87 * Necessary sanity check. We needn't check "get-config-addr-info"
88 * and its variant since the old firmware probably support address
89 * of domain/bus/slot/function for EEH RTAS operations.
91 if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE) {
92 pr_warning("%s: RTAS service <ibm,set-eeh-option> invalid\n",
95 } else if (ibm_set_slot_reset == RTAS_UNKNOWN_SERVICE) {
96 pr_warning("%s: RTAS service <ibm,set-slot-reset> invalid\n",
99 } else if (ibm_read_slot_reset_state2 == RTAS_UNKNOWN_SERVICE &&
100 ibm_read_slot_reset_state == RTAS_UNKNOWN_SERVICE) {
101 pr_warning("%s: RTAS service <ibm,read-slot-reset-state2> and "
102 "<ibm,read-slot-reset-state> invalid\n",
105 } else if (ibm_slot_error_detail == RTAS_UNKNOWN_SERVICE) {
106 pr_warning("%s: RTAS service <ibm,slot-error-detail> invalid\n",
109 } else if (ibm_configure_pe == RTAS_UNKNOWN_SERVICE &&
110 ibm_configure_bridge == RTAS_UNKNOWN_SERVICE) {
111 pr_warning("%s: RTAS service <ibm,configure-pe> and "
112 "<ibm,configure-bridge> invalid\n",
117 /* Initialize error log lock and size */
118 spin_lock_init(&slot_errbuf_lock);
119 eeh_error_buf_size = rtas_token("rtas-error-log-max");
120 if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
121 pr_warning("%s: unknown EEH error log size\n",
123 eeh_error_buf_size = 1024;
124 } else if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
125 pr_warning("%s: EEH error log size %d exceeds the maximal %d\n",
126 __func__, eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
127 eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
130 /* Set EEH probe mode */
131 eeh_probe_mode_set(EEH_PROBE_MODE_DEVTREE);
136 static int pseries_eeh_cap_start(struct device_node *dn)
138 struct pci_dn *pdn = PCI_DN(dn);
144 rtas_read_config(pdn, PCI_STATUS, 2, &status);
145 if (!(status & PCI_STATUS_CAP_LIST))
148 return PCI_CAPABILITY_LIST;
152 static int pseries_eeh_find_cap(struct device_node *dn, int cap)
154 struct pci_dn *pdn = PCI_DN(dn);
155 int pos = pseries_eeh_cap_start(dn);
156 int cnt = 48; /* Maximal number of capabilities */
163 rtas_read_config(pdn, pos, 1, &pos);
167 rtas_read_config(pdn, pos + PCI_CAP_LIST_ID, 1, &id);
172 pos += PCI_CAP_LIST_NEXT;
178 static int pseries_eeh_find_ecap(struct device_node *dn, int cap)
180 struct pci_dn *pdn = PCI_DN(dn);
181 struct eeh_dev *edev = of_node_to_eeh_dev(dn);
184 int ttl = (4096 - 256) / 8;
186 if (!edev || !edev->pcie_cap)
188 if (rtas_read_config(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
194 if (PCI_EXT_CAP_ID(header) == cap && pos)
197 pos = PCI_EXT_CAP_NEXT(header);
201 if (rtas_read_config(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
209 * pseries_eeh_of_probe - EEH probe on the given device
213 * When EEH module is installed during system boot, all PCI devices
214 * are checked one by one to see if it supports EEH. The function
215 * is introduced for the purpose.
217 static void *pseries_eeh_of_probe(struct device_node *dn, void *flag)
219 struct eeh_dev *edev;
221 struct pci_dn *pdn = PCI_DN(dn);
222 const __be32 *classp, *vendorp, *devicep;
229 /* Retrieve OF node and eeh device */
230 edev = of_node_to_eeh_dev(dn);
231 if (edev->pe || !of_device_is_available(dn))
234 /* Retrieve class/vendor/device IDs */
235 classp = of_get_property(dn, "class-code", NULL);
236 vendorp = of_get_property(dn, "vendor-id", NULL);
237 devicep = of_get_property(dn, "device-id", NULL);
239 /* Skip for bad OF node or PCI-ISA bridge */
240 if (!classp || !vendorp || !devicep)
242 if (dn->type && !strcmp(dn->type, "isa"))
245 class_code = of_read_number(classp, 1);
248 * Update class code and mode of eeh device. We need
249 * correctly reflects that current device is root port
250 * or PCIe switch downstream port.
252 edev->class_code = class_code;
253 edev->pcix_cap = pseries_eeh_find_cap(dn, PCI_CAP_ID_PCIX);
254 edev->pcie_cap = pseries_eeh_find_cap(dn, PCI_CAP_ID_EXP);
255 edev->aer_cap = pseries_eeh_find_ecap(dn, PCI_EXT_CAP_ID_ERR);
256 edev->mode &= 0xFFFFFF00;
257 if ((edev->class_code >> 8) == PCI_CLASS_BRIDGE_PCI) {
258 edev->mode |= EEH_DEV_BRIDGE;
259 if (edev->pcie_cap) {
260 rtas_read_config(pdn, edev->pcie_cap + PCI_EXP_FLAGS,
262 pcie_flags = (pcie_flags & PCI_EXP_FLAGS_TYPE) >> 4;
263 if (pcie_flags == PCI_EXP_TYPE_ROOT_PORT)
264 edev->mode |= EEH_DEV_ROOT_PORT;
265 else if (pcie_flags == PCI_EXP_TYPE_DOWNSTREAM)
266 edev->mode |= EEH_DEV_DS_PORT;
270 /* Retrieve the device address */
271 regs = of_get_property(dn, "reg", NULL);
273 pr_warning("%s: OF node property %s::reg not found\n",
274 __func__, dn->full_name);
278 /* Initialize the fake PE */
279 memset(&pe, 0, sizeof(struct eeh_pe));
281 pe.config_addr = of_read_number(regs, 1);
283 /* Enable EEH on the device */
284 ret = eeh_ops->set_option(&pe, EEH_OPT_ENABLE);
286 edev->config_addr = of_read_number(regs, 1);
287 /* Retrieve PE address */
288 edev->pe_config_addr = eeh_ops->get_pe_addr(&pe);
289 pe.addr = edev->pe_config_addr;
291 /* Some older systems (Power4) allow the ibm,set-eeh-option
292 * call to succeed even on nodes where EEH is not supported.
293 * Verify support explicitly.
295 ret = eeh_ops->get_state(&pe, NULL);
296 if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
300 eeh_set_enable(true);
301 eeh_add_to_parent_pe(edev);
303 pr_debug("%s: EEH enabled on %s PHB#%d-PE#%x, config addr#%x\n",
304 __func__, dn->full_name, pe.phb->global_number,
305 pe.addr, pe.config_addr);
306 } else if (dn->parent && of_node_to_eeh_dev(dn->parent) &&
307 (of_node_to_eeh_dev(dn->parent))->pe) {
308 /* This device doesn't support EEH, but it may have an
309 * EEH parent, in which case we mark it as supported.
311 edev->config_addr = of_node_to_eeh_dev(dn->parent)->config_addr;
312 edev->pe_config_addr = of_node_to_eeh_dev(dn->parent)->pe_config_addr;
313 eeh_add_to_parent_pe(edev);
317 /* Save memory bars */
324 * pseries_eeh_set_option - Initialize EEH or MMIO/DMA reenable
326 * @option: operation to be issued
328 * The function is used to control the EEH functionality globally.
329 * Currently, following options are support according to PAPR:
330 * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
332 static int pseries_eeh_set_option(struct eeh_pe *pe, int option)
338 * When we're enabling or disabling EEH functioality on
339 * the particular PE, the PE config address is possibly
340 * unavailable. Therefore, we have to figure it out from
344 case EEH_OPT_DISABLE:
346 case EEH_OPT_THAW_MMIO:
347 case EEH_OPT_THAW_DMA:
348 config_addr = pe->config_addr;
350 config_addr = pe->addr;
354 pr_err("%s: Invalid option %d\n",
359 ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
360 config_addr, BUID_HI(pe->phb->buid),
361 BUID_LO(pe->phb->buid), option);
367 * pseries_eeh_get_pe_addr - Retrieve PE address
370 * Retrieve the assocated PE address. Actually, there're 2 RTAS
371 * function calls dedicated for the purpose. We need implement
372 * it through the new function and then the old one. Besides,
373 * you should make sure the config address is figured out from
374 * FDT node before calling the function.
376 * It's notable that zero'ed return value means invalid PE config
379 static int pseries_eeh_get_pe_addr(struct eeh_pe *pe)
384 if (ibm_get_config_addr_info2 != RTAS_UNKNOWN_SERVICE) {
386 * First of all, we need to make sure there has one PE
387 * associated with the device. Otherwise, PE address is
390 ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
391 pe->config_addr, BUID_HI(pe->phb->buid),
392 BUID_LO(pe->phb->buid), 1);
393 if (ret || (rets[0] == 0))
396 /* Retrieve the associated PE config address */
397 ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
398 pe->config_addr, BUID_HI(pe->phb->buid),
399 BUID_LO(pe->phb->buid), 0);
401 pr_warning("%s: Failed to get address for PHB#%d-PE#%x\n",
402 __func__, pe->phb->global_number, pe->config_addr);
409 if (ibm_get_config_addr_info != RTAS_UNKNOWN_SERVICE) {
410 ret = rtas_call(ibm_get_config_addr_info, 4, 2, rets,
411 pe->config_addr, BUID_HI(pe->phb->buid),
412 BUID_LO(pe->phb->buid), 0);
414 pr_warning("%s: Failed to get address for PHB#%d-PE#%x\n",
415 __func__, pe->phb->global_number, pe->config_addr);
426 * pseries_eeh_get_state - Retrieve PE state
428 * @state: return value
430 * Retrieve the state of the specified PE. On RTAS compliant
431 * pseries platform, there already has one dedicated RTAS function
432 * for the purpose. It's notable that the associated PE config address
433 * might be ready when calling the function. Therefore, endeavour to
434 * use the PE config address if possible. Further more, there're 2
435 * RTAS calls for the purpose, we need to try the new one and back
436 * to the old one if the new one couldn't work properly.
438 static int pseries_eeh_get_state(struct eeh_pe *pe, int *state)
445 /* Figure out PE config address if possible */
446 config_addr = pe->config_addr;
448 config_addr = pe->addr;
450 if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
451 ret = rtas_call(ibm_read_slot_reset_state2, 3, 4, rets,
452 config_addr, BUID_HI(pe->phb->buid),
453 BUID_LO(pe->phb->buid));
454 } else if (ibm_read_slot_reset_state != RTAS_UNKNOWN_SERVICE) {
455 /* Fake PE unavailable info */
457 ret = rtas_call(ibm_read_slot_reset_state, 3, 3, rets,
458 config_addr, BUID_HI(pe->phb->buid),
459 BUID_LO(pe->phb->buid));
461 return EEH_STATE_NOT_SUPPORT;
467 /* Parse the result out */
472 result &= ~EEH_STATE_RESET_ACTIVE;
473 result |= EEH_STATE_MMIO_ACTIVE;
474 result |= EEH_STATE_DMA_ACTIVE;
477 result |= EEH_STATE_RESET_ACTIVE;
478 result |= EEH_STATE_MMIO_ACTIVE;
479 result |= EEH_STATE_DMA_ACTIVE;
482 result &= ~EEH_STATE_RESET_ACTIVE;
483 result &= ~EEH_STATE_MMIO_ACTIVE;
484 result &= ~EEH_STATE_DMA_ACTIVE;
487 result &= ~EEH_STATE_RESET_ACTIVE;
488 result &= ~EEH_STATE_MMIO_ACTIVE;
489 result &= ~EEH_STATE_DMA_ACTIVE;
490 result |= EEH_STATE_MMIO_ENABLED;
494 if (state) *state = rets[2];
495 result = EEH_STATE_UNAVAILABLE;
497 result = EEH_STATE_NOT_SUPPORT;
501 result = EEH_STATE_NOT_SUPPORT;
504 result = EEH_STATE_NOT_SUPPORT;
511 * pseries_eeh_reset - Reset the specified PE
513 * @option: reset option
515 * Reset the specified PE
517 static int pseries_eeh_reset(struct eeh_pe *pe, int option)
522 /* Figure out PE address */
523 config_addr = pe->config_addr;
525 config_addr = pe->addr;
527 /* Reset PE through RTAS call */
528 ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
529 config_addr, BUID_HI(pe->phb->buid),
530 BUID_LO(pe->phb->buid), option);
532 /* If fundamental-reset not supported, try hot-reset */
533 if (option == EEH_RESET_FUNDAMENTAL &&
535 option = EEH_RESET_HOT;
536 ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
537 config_addr, BUID_HI(pe->phb->buid),
538 BUID_LO(pe->phb->buid), option);
541 /* We need reset hold or settlement delay */
542 if (option == EEH_RESET_FUNDAMENTAL ||
543 option == EEH_RESET_HOT)
544 msleep(EEH_PE_RST_HOLD_TIME);
546 msleep(EEH_PE_RST_SETTLE_TIME);
552 * pseries_eeh_wait_state - Wait for PE state
554 * @max_wait: maximal period in microsecond
556 * Wait for the state of associated PE. It might take some time
557 * to retrieve the PE's state.
559 static int pseries_eeh_wait_state(struct eeh_pe *pe, int max_wait)
565 * According to PAPR, the state of PE might be temporarily
566 * unavailable. Under the circumstance, we have to wait
567 * for indicated time determined by firmware. The maximal
568 * wait time is 5 minutes, which is acquired from the original
569 * EEH implementation. Also, the original implementation
570 * also defined the minimal wait time as 1 second.
572 #define EEH_STATE_MIN_WAIT_TIME (1000)
573 #define EEH_STATE_MAX_WAIT_TIME (300 * 1000)
576 ret = pseries_eeh_get_state(pe, &mwait);
579 * If the PE's state is temporarily unavailable,
580 * we have to wait for the specified time. Otherwise,
581 * the PE's state will be returned immediately.
583 if (ret != EEH_STATE_UNAVAILABLE)
587 pr_warning("%s: Timeout when getting PE's state (%d)\n",
589 return EEH_STATE_NOT_SUPPORT;
593 pr_warning("%s: Firmware returned bad wait value %d\n",
595 mwait = EEH_STATE_MIN_WAIT_TIME;
596 } else if (mwait > EEH_STATE_MAX_WAIT_TIME) {
597 pr_warning("%s: Firmware returned too long wait value %d\n",
599 mwait = EEH_STATE_MAX_WAIT_TIME;
606 return EEH_STATE_NOT_SUPPORT;
610 * pseries_eeh_get_log - Retrieve error log
612 * @severity: temporary or permanent error log
613 * @drv_log: driver log to be combined with retrieved error log
614 * @len: length of driver log
616 * Retrieve the temporary or permanent error from the PE.
617 * Actually, the error will be retrieved through the dedicated
620 static int pseries_eeh_get_log(struct eeh_pe *pe, int severity, char *drv_log, unsigned long len)
626 spin_lock_irqsave(&slot_errbuf_lock, flags);
627 memset(slot_errbuf, 0, eeh_error_buf_size);
629 /* Figure out the PE address */
630 config_addr = pe->config_addr;
632 config_addr = pe->addr;
634 ret = rtas_call(ibm_slot_error_detail, 8, 1, NULL, config_addr,
635 BUID_HI(pe->phb->buid), BUID_LO(pe->phb->buid),
636 virt_to_phys(drv_log), len,
637 virt_to_phys(slot_errbuf), eeh_error_buf_size,
640 log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
641 spin_unlock_irqrestore(&slot_errbuf_lock, flags);
647 * pseries_eeh_configure_bridge - Configure PCI bridges in the indicated PE
650 * The function will be called to reconfigure the bridges included
651 * in the specified PE so that the mulfunctional PE would be recovered
654 static int pseries_eeh_configure_bridge(struct eeh_pe *pe)
659 /* Figure out the PE address */
660 config_addr = pe->config_addr;
662 config_addr = pe->addr;
664 /* Use new configure-pe function, if supported */
665 if (ibm_configure_pe != RTAS_UNKNOWN_SERVICE) {
666 ret = rtas_call(ibm_configure_pe, 3, 1, NULL,
667 config_addr, BUID_HI(pe->phb->buid),
668 BUID_LO(pe->phb->buid));
669 } else if (ibm_configure_bridge != RTAS_UNKNOWN_SERVICE) {
670 ret = rtas_call(ibm_configure_bridge, 3, 1, NULL,
671 config_addr, BUID_HI(pe->phb->buid),
672 BUID_LO(pe->phb->buid));
678 pr_warning("%s: Unable to configure bridge PHB#%d-PE#%x (%d)\n",
679 __func__, pe->phb->global_number, pe->addr, ret);
685 * pseries_eeh_read_config - Read PCI config space
687 * @where: PCI address
688 * @size: size to read
691 * Read config space from the speicifed device
693 static int pseries_eeh_read_config(struct device_node *dn, int where, int size, u32 *val)
699 return rtas_read_config(pdn, where, size, val);
703 * pseries_eeh_write_config - Write PCI config space
705 * @where: PCI address
706 * @size: size to write
707 * @val: value to be written
709 * Write config space to the specified device
711 static int pseries_eeh_write_config(struct device_node *dn, int where, int size, u32 val)
717 return rtas_write_config(pdn, where, size, val);
720 static struct eeh_ops pseries_eeh_ops = {
722 .init = pseries_eeh_init,
723 .of_probe = pseries_eeh_of_probe,
725 .set_option = pseries_eeh_set_option,
726 .get_pe_addr = pseries_eeh_get_pe_addr,
727 .get_state = pseries_eeh_get_state,
728 .reset = pseries_eeh_reset,
729 .wait_state = pseries_eeh_wait_state,
730 .get_log = pseries_eeh_get_log,
731 .configure_bridge = pseries_eeh_configure_bridge,
732 .read_config = pseries_eeh_read_config,
733 .write_config = pseries_eeh_write_config,
735 .restore_config = NULL
739 * eeh_pseries_init - Register platform dependent EEH operations
741 * EEH initialization on pseries platform. This function should be
742 * called before any EEH related functions.
744 static int __init eeh_pseries_init(void)
748 if (!machine_is(pseries))
751 ret = eeh_ops_register(&pseries_eeh_ops);
753 pr_info("EEH: pSeries platform initialized\n");
755 pr_info("EEH: pSeries platform initialization failure (%d)\n",
761 early_initcall(eeh_pseries_init);