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 ibm_set_slot_reset == RTAS_UNKNOWN_SERVICE ||
93 (ibm_read_slot_reset_state2 == RTAS_UNKNOWN_SERVICE &&
94 ibm_read_slot_reset_state == RTAS_UNKNOWN_SERVICE) ||
95 ibm_slot_error_detail == RTAS_UNKNOWN_SERVICE ||
96 (ibm_configure_pe == RTAS_UNKNOWN_SERVICE &&
97 ibm_configure_bridge == RTAS_UNKNOWN_SERVICE)) {
98 pr_info("EEH functionality not supported\n");
102 /* Initialize error log lock and size */
103 spin_lock_init(&slot_errbuf_lock);
104 eeh_error_buf_size = rtas_token("rtas-error-log-max");
105 if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
106 pr_info("%s: unknown EEH error log size\n",
108 eeh_error_buf_size = 1024;
109 } else if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
110 pr_info("%s: EEH error log size %d exceeds the maximal %d\n",
111 __func__, eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
112 eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
115 /* Set EEH probe mode */
116 eeh_add_flag(EEH_PROBE_MODE_DEVTREE | EEH_ENABLE_IO_FOR_LOG);
121 static int pseries_eeh_cap_start(struct device_node *dn)
123 struct pci_dn *pdn = PCI_DN(dn);
129 rtas_read_config(pdn, PCI_STATUS, 2, &status);
130 if (!(status & PCI_STATUS_CAP_LIST))
133 return PCI_CAPABILITY_LIST;
137 static int pseries_eeh_find_cap(struct device_node *dn, int cap)
139 struct pci_dn *pdn = PCI_DN(dn);
140 int pos = pseries_eeh_cap_start(dn);
141 int cnt = 48; /* Maximal number of capabilities */
148 rtas_read_config(pdn, pos, 1, &pos);
152 rtas_read_config(pdn, pos + PCI_CAP_LIST_ID, 1, &id);
157 pos += PCI_CAP_LIST_NEXT;
163 static int pseries_eeh_find_ecap(struct device_node *dn, int cap)
165 struct pci_dn *pdn = PCI_DN(dn);
166 struct eeh_dev *edev = of_node_to_eeh_dev(dn);
169 int ttl = (4096 - 256) / 8;
171 if (!edev || !edev->pcie_cap)
173 if (rtas_read_config(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
179 if (PCI_EXT_CAP_ID(header) == cap && pos)
182 pos = PCI_EXT_CAP_NEXT(header);
186 if (rtas_read_config(pdn, pos, 4, &header) != PCIBIOS_SUCCESSFUL)
194 * pseries_eeh_of_probe - EEH probe on the given device
198 * When EEH module is installed during system boot, all PCI devices
199 * are checked one by one to see if it supports EEH. The function
200 * is introduced for the purpose.
202 static void *pseries_eeh_of_probe(struct device_node *dn, void *flag)
204 struct eeh_dev *edev;
206 struct pci_dn *pdn = PCI_DN(dn);
207 const __be32 *classp, *vendorp, *devicep;
214 /* Retrieve OF node and eeh device */
215 edev = of_node_to_eeh_dev(dn);
216 if (edev->pe || !of_device_is_available(dn))
219 /* Retrieve class/vendor/device IDs */
220 classp = of_get_property(dn, "class-code", NULL);
221 vendorp = of_get_property(dn, "vendor-id", NULL);
222 devicep = of_get_property(dn, "device-id", NULL);
224 /* Skip for bad OF node or PCI-ISA bridge */
225 if (!classp || !vendorp || !devicep)
227 if (dn->type && !strcmp(dn->type, "isa"))
230 class_code = of_read_number(classp, 1);
233 * Update class code and mode of eeh device. We need
234 * correctly reflects that current device is root port
235 * or PCIe switch downstream port.
237 edev->class_code = class_code;
238 edev->pcix_cap = pseries_eeh_find_cap(dn, PCI_CAP_ID_PCIX);
239 edev->pcie_cap = pseries_eeh_find_cap(dn, PCI_CAP_ID_EXP);
240 edev->aer_cap = pseries_eeh_find_ecap(dn, PCI_EXT_CAP_ID_ERR);
241 edev->mode &= 0xFFFFFF00;
242 if ((edev->class_code >> 8) == PCI_CLASS_BRIDGE_PCI) {
243 edev->mode |= EEH_DEV_BRIDGE;
244 if (edev->pcie_cap) {
245 rtas_read_config(pdn, edev->pcie_cap + PCI_EXP_FLAGS,
247 pcie_flags = (pcie_flags & PCI_EXP_FLAGS_TYPE) >> 4;
248 if (pcie_flags == PCI_EXP_TYPE_ROOT_PORT)
249 edev->mode |= EEH_DEV_ROOT_PORT;
250 else if (pcie_flags == PCI_EXP_TYPE_DOWNSTREAM)
251 edev->mode |= EEH_DEV_DS_PORT;
255 /* Retrieve the device address */
256 regs = of_get_property(dn, "reg", NULL);
258 pr_warn("%s: OF node property %s::reg not found\n",
259 __func__, dn->full_name);
263 /* Initialize the fake PE */
264 memset(&pe, 0, sizeof(struct eeh_pe));
266 pe.config_addr = of_read_number(regs, 1);
268 /* Enable EEH on the device */
269 ret = eeh_ops->set_option(&pe, EEH_OPT_ENABLE);
271 edev->config_addr = of_read_number(regs, 1);
272 /* Retrieve PE address */
273 edev->pe_config_addr = eeh_ops->get_pe_addr(&pe);
274 pe.addr = edev->pe_config_addr;
276 /* Some older systems (Power4) allow the ibm,set-eeh-option
277 * call to succeed even on nodes where EEH is not supported.
278 * Verify support explicitly.
280 ret = eeh_ops->get_state(&pe, NULL);
281 if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
285 eeh_add_flag(EEH_ENABLED);
286 eeh_add_to_parent_pe(edev);
288 pr_debug("%s: EEH enabled on %s PHB#%d-PE#%x, config addr#%x\n",
289 __func__, dn->full_name, pe.phb->global_number,
290 pe.addr, pe.config_addr);
291 } else if (dn->parent && of_node_to_eeh_dev(dn->parent) &&
292 (of_node_to_eeh_dev(dn->parent))->pe) {
293 /* This device doesn't support EEH, but it may have an
294 * EEH parent, in which case we mark it as supported.
296 edev->config_addr = of_node_to_eeh_dev(dn->parent)->config_addr;
297 edev->pe_config_addr = of_node_to_eeh_dev(dn->parent)->pe_config_addr;
298 eeh_add_to_parent_pe(edev);
302 /* Save memory bars */
309 * pseries_eeh_set_option - Initialize EEH or MMIO/DMA reenable
311 * @option: operation to be issued
313 * The function is used to control the EEH functionality globally.
314 * Currently, following options are support according to PAPR:
315 * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
317 static int pseries_eeh_set_option(struct eeh_pe *pe, int option)
323 * When we're enabling or disabling EEH functioality on
324 * the particular PE, the PE config address is possibly
325 * unavailable. Therefore, we have to figure it out from
329 case EEH_OPT_DISABLE:
331 case EEH_OPT_THAW_MMIO:
332 case EEH_OPT_THAW_DMA:
333 config_addr = pe->config_addr;
335 config_addr = pe->addr;
337 case EEH_OPT_FREEZE_PE:
341 pr_err("%s: Invalid option %d\n",
346 ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
347 config_addr, BUID_HI(pe->phb->buid),
348 BUID_LO(pe->phb->buid), option);
354 * pseries_eeh_get_pe_addr - Retrieve PE address
357 * Retrieve the assocated PE address. Actually, there're 2 RTAS
358 * function calls dedicated for the purpose. We need implement
359 * it through the new function and then the old one. Besides,
360 * you should make sure the config address is figured out from
361 * FDT node before calling the function.
363 * It's notable that zero'ed return value means invalid PE config
366 static int pseries_eeh_get_pe_addr(struct eeh_pe *pe)
371 if (ibm_get_config_addr_info2 != RTAS_UNKNOWN_SERVICE) {
373 * First of all, we need to make sure there has one PE
374 * associated with the device. Otherwise, PE address is
377 ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
378 pe->config_addr, BUID_HI(pe->phb->buid),
379 BUID_LO(pe->phb->buid), 1);
380 if (ret || (rets[0] == 0))
383 /* Retrieve the associated PE config address */
384 ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
385 pe->config_addr, BUID_HI(pe->phb->buid),
386 BUID_LO(pe->phb->buid), 0);
388 pr_warn("%s: Failed to get address for PHB#%d-PE#%x\n",
389 __func__, pe->phb->global_number, pe->config_addr);
396 if (ibm_get_config_addr_info != RTAS_UNKNOWN_SERVICE) {
397 ret = rtas_call(ibm_get_config_addr_info, 4, 2, rets,
398 pe->config_addr, BUID_HI(pe->phb->buid),
399 BUID_LO(pe->phb->buid), 0);
401 pr_warn("%s: Failed to get address for PHB#%d-PE#%x\n",
402 __func__, pe->phb->global_number, pe->config_addr);
413 * pseries_eeh_get_state - Retrieve PE state
415 * @state: return value
417 * Retrieve the state of the specified PE. On RTAS compliant
418 * pseries platform, there already has one dedicated RTAS function
419 * for the purpose. It's notable that the associated PE config address
420 * might be ready when calling the function. Therefore, endeavour to
421 * use the PE config address if possible. Further more, there're 2
422 * RTAS calls for the purpose, we need to try the new one and back
423 * to the old one if the new one couldn't work properly.
425 static int pseries_eeh_get_state(struct eeh_pe *pe, int *state)
432 /* Figure out PE config address if possible */
433 config_addr = pe->config_addr;
435 config_addr = pe->addr;
437 if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
438 ret = rtas_call(ibm_read_slot_reset_state2, 3, 4, rets,
439 config_addr, BUID_HI(pe->phb->buid),
440 BUID_LO(pe->phb->buid));
441 } else if (ibm_read_slot_reset_state != RTAS_UNKNOWN_SERVICE) {
442 /* Fake PE unavailable info */
444 ret = rtas_call(ibm_read_slot_reset_state, 3, 3, rets,
445 config_addr, BUID_HI(pe->phb->buid),
446 BUID_LO(pe->phb->buid));
448 return EEH_STATE_NOT_SUPPORT;
454 /* Parse the result out */
459 result &= ~EEH_STATE_RESET_ACTIVE;
460 result |= EEH_STATE_MMIO_ACTIVE;
461 result |= EEH_STATE_DMA_ACTIVE;
464 result |= EEH_STATE_RESET_ACTIVE;
465 result |= EEH_STATE_MMIO_ACTIVE;
466 result |= EEH_STATE_DMA_ACTIVE;
469 result &= ~EEH_STATE_RESET_ACTIVE;
470 result &= ~EEH_STATE_MMIO_ACTIVE;
471 result &= ~EEH_STATE_DMA_ACTIVE;
474 result &= ~EEH_STATE_RESET_ACTIVE;
475 result &= ~EEH_STATE_MMIO_ACTIVE;
476 result &= ~EEH_STATE_DMA_ACTIVE;
477 result |= EEH_STATE_MMIO_ENABLED;
481 if (state) *state = rets[2];
482 result = EEH_STATE_UNAVAILABLE;
484 result = EEH_STATE_NOT_SUPPORT;
488 result = EEH_STATE_NOT_SUPPORT;
491 result = EEH_STATE_NOT_SUPPORT;
498 * pseries_eeh_reset - Reset the specified PE
500 * @option: reset option
502 * Reset the specified PE
504 static int pseries_eeh_reset(struct eeh_pe *pe, int option)
509 /* Figure out PE address */
510 config_addr = pe->config_addr;
512 config_addr = pe->addr;
514 /* Reset PE through RTAS call */
515 ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
516 config_addr, BUID_HI(pe->phb->buid),
517 BUID_LO(pe->phb->buid), option);
519 /* If fundamental-reset not supported, try hot-reset */
520 if (option == EEH_RESET_FUNDAMENTAL &&
522 option = EEH_RESET_HOT;
523 ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
524 config_addr, BUID_HI(pe->phb->buid),
525 BUID_LO(pe->phb->buid), option);
528 /* We need reset hold or settlement delay */
529 if (option == EEH_RESET_FUNDAMENTAL ||
530 option == EEH_RESET_HOT)
531 msleep(EEH_PE_RST_HOLD_TIME);
533 msleep(EEH_PE_RST_SETTLE_TIME);
539 * pseries_eeh_wait_state - Wait for PE state
541 * @max_wait: maximal period in microsecond
543 * Wait for the state of associated PE. It might take some time
544 * to retrieve the PE's state.
546 static int pseries_eeh_wait_state(struct eeh_pe *pe, int max_wait)
552 * According to PAPR, the state of PE might be temporarily
553 * unavailable. Under the circumstance, we have to wait
554 * for indicated time determined by firmware. The maximal
555 * wait time is 5 minutes, which is acquired from the original
556 * EEH implementation. Also, the original implementation
557 * also defined the minimal wait time as 1 second.
559 #define EEH_STATE_MIN_WAIT_TIME (1000)
560 #define EEH_STATE_MAX_WAIT_TIME (300 * 1000)
563 ret = pseries_eeh_get_state(pe, &mwait);
566 * If the PE's state is temporarily unavailable,
567 * we have to wait for the specified time. Otherwise,
568 * the PE's state will be returned immediately.
570 if (ret != EEH_STATE_UNAVAILABLE)
574 pr_warn("%s: Timeout when getting PE's state (%d)\n",
576 return EEH_STATE_NOT_SUPPORT;
580 pr_warn("%s: Firmware returned bad wait value %d\n",
582 mwait = EEH_STATE_MIN_WAIT_TIME;
583 } else if (mwait > EEH_STATE_MAX_WAIT_TIME) {
584 pr_warn("%s: Firmware returned too long wait value %d\n",
586 mwait = EEH_STATE_MAX_WAIT_TIME;
593 return EEH_STATE_NOT_SUPPORT;
597 * pseries_eeh_get_log - Retrieve error log
599 * @severity: temporary or permanent error log
600 * @drv_log: driver log to be combined with retrieved error log
601 * @len: length of driver log
603 * Retrieve the temporary or permanent error from the PE.
604 * Actually, the error will be retrieved through the dedicated
607 static int pseries_eeh_get_log(struct eeh_pe *pe, int severity, char *drv_log, unsigned long len)
613 spin_lock_irqsave(&slot_errbuf_lock, flags);
614 memset(slot_errbuf, 0, eeh_error_buf_size);
616 /* Figure out the PE address */
617 config_addr = pe->config_addr;
619 config_addr = pe->addr;
621 ret = rtas_call(ibm_slot_error_detail, 8, 1, NULL, config_addr,
622 BUID_HI(pe->phb->buid), BUID_LO(pe->phb->buid),
623 virt_to_phys(drv_log), len,
624 virt_to_phys(slot_errbuf), eeh_error_buf_size,
627 log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
628 spin_unlock_irqrestore(&slot_errbuf_lock, flags);
634 * pseries_eeh_configure_bridge - Configure PCI bridges in the indicated PE
637 * The function will be called to reconfigure the bridges included
638 * in the specified PE so that the mulfunctional PE would be recovered
641 static int pseries_eeh_configure_bridge(struct eeh_pe *pe)
646 /* Figure out the PE address */
647 config_addr = pe->config_addr;
649 config_addr = pe->addr;
651 /* Use new configure-pe function, if supported */
652 if (ibm_configure_pe != RTAS_UNKNOWN_SERVICE) {
653 ret = rtas_call(ibm_configure_pe, 3, 1, NULL,
654 config_addr, BUID_HI(pe->phb->buid),
655 BUID_LO(pe->phb->buid));
656 } else if (ibm_configure_bridge != RTAS_UNKNOWN_SERVICE) {
657 ret = rtas_call(ibm_configure_bridge, 3, 1, NULL,
658 config_addr, BUID_HI(pe->phb->buid),
659 BUID_LO(pe->phb->buid));
665 pr_warn("%s: Unable to configure bridge PHB#%d-PE#%x (%d)\n",
666 __func__, pe->phb->global_number, pe->addr, ret);
672 * pseries_eeh_read_config - Read PCI config space
674 * @where: PCI address
675 * @size: size to read
678 * Read config space from the speicifed device
680 static int pseries_eeh_read_config(struct device_node *dn, int where, int size, u32 *val)
686 return rtas_read_config(pdn, where, size, val);
690 * pseries_eeh_write_config - Write PCI config space
692 * @where: PCI address
693 * @size: size to write
694 * @val: value to be written
696 * Write config space to the specified device
698 static int pseries_eeh_write_config(struct device_node *dn, int where, int size, u32 val)
704 return rtas_write_config(pdn, where, size, val);
707 static struct eeh_ops pseries_eeh_ops = {
709 .init = pseries_eeh_init,
710 .of_probe = pseries_eeh_of_probe,
712 .set_option = pseries_eeh_set_option,
713 .get_pe_addr = pseries_eeh_get_pe_addr,
714 .get_state = pseries_eeh_get_state,
715 .reset = pseries_eeh_reset,
716 .wait_state = pseries_eeh_wait_state,
717 .get_log = pseries_eeh_get_log,
718 .configure_bridge = pseries_eeh_configure_bridge,
720 .read_config = pseries_eeh_read_config,
721 .write_config = pseries_eeh_write_config,
723 .restore_config = NULL
727 * eeh_pseries_init - Register platform dependent EEH operations
729 * EEH initialization on pseries platform. This function should be
730 * called before any EEH related functions.
732 static int __init eeh_pseries_init(void)
736 ret = eeh_ops_register(&pseries_eeh_ops);
738 pr_info("EEH: pSeries platform initialized\n");
740 pr_info("EEH: pSeries platform initialization failure (%d)\n",
745 machine_early_initcall(pseries, eeh_pseries_init);