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);
137 * pseries_eeh_of_probe - EEH probe on the given device
141 * When EEH module is installed during system boot, all PCI devices
142 * are checked one by one to see if it supports EEH. The function
143 * is introduced for the purpose.
145 static void *pseries_eeh_of_probe(struct device_node *dn, void *flag)
147 struct eeh_dev *edev;
149 const u32 *class_code, *vendor_id, *device_id;
154 /* Retrieve OF node and eeh device */
155 edev = of_node_to_eeh_dev(dn);
156 if (!of_device_is_available(dn))
159 /* Retrieve class/vendor/device IDs */
160 class_code = of_get_property(dn, "class-code", NULL);
161 vendor_id = of_get_property(dn, "vendor-id", NULL);
162 device_id = of_get_property(dn, "device-id", NULL);
164 /* Skip for bad OF node or PCI-ISA bridge */
165 if (!class_code || !vendor_id || !device_id)
167 if (dn->type && !strcmp(dn->type, "isa"))
170 /* Update class code and mode of eeh device */
171 edev->class_code = *class_code;
174 /* Retrieve the device address */
175 regs = of_get_property(dn, "reg", NULL);
177 pr_warning("%s: OF node property %s::reg not found\n",
178 __func__, dn->full_name);
182 /* Initialize the fake PE */
183 memset(&pe, 0, sizeof(struct eeh_pe));
185 pe.config_addr = regs[0];
187 /* Enable EEH on the device */
188 ret = eeh_ops->set_option(&pe, EEH_OPT_ENABLE);
190 edev->config_addr = regs[0];
191 /* Retrieve PE address */
192 edev->pe_config_addr = eeh_ops->get_pe_addr(&pe);
193 pe.addr = edev->pe_config_addr;
195 /* Some older systems (Power4) allow the ibm,set-eeh-option
196 * call to succeed even on nodes where EEH is not supported.
197 * Verify support explicitly.
199 ret = eeh_ops->get_state(&pe, NULL);
200 if (ret > 0 && ret != EEH_STATE_NOT_SUPPORT)
204 eeh_subsystem_enabled = 1;
205 eeh_add_to_parent_pe(edev);
207 pr_debug("%s: EEH enabled on %s PHB#%d-PE#%x, config addr#%x\n",
208 __func__, dn->full_name, pe.phb->global_number,
209 pe.addr, pe.config_addr);
210 } else if (dn->parent && of_node_to_eeh_dev(dn->parent) &&
211 (of_node_to_eeh_dev(dn->parent))->pe) {
212 /* This device doesn't support EEH, but it may have an
213 * EEH parent, in which case we mark it as supported.
215 edev->config_addr = of_node_to_eeh_dev(dn->parent)->config_addr;
216 edev->pe_config_addr = of_node_to_eeh_dev(dn->parent)->pe_config_addr;
217 eeh_add_to_parent_pe(edev);
221 /* Save memory bars */
228 * pseries_eeh_set_option - Initialize EEH or MMIO/DMA reenable
230 * @option: operation to be issued
232 * The function is used to control the EEH functionality globally.
233 * Currently, following options are support according to PAPR:
234 * Enable EEH, Disable EEH, Enable MMIO and Enable DMA
236 static int pseries_eeh_set_option(struct eeh_pe *pe, int option)
242 * When we're enabling or disabling EEH functioality on
243 * the particular PE, the PE config address is possibly
244 * unavailable. Therefore, we have to figure it out from
248 case EEH_OPT_DISABLE:
250 case EEH_OPT_THAW_MMIO:
251 case EEH_OPT_THAW_DMA:
252 config_addr = pe->config_addr;
254 config_addr = pe->addr;
258 pr_err("%s: Invalid option %d\n",
263 ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
264 config_addr, BUID_HI(pe->phb->buid),
265 BUID_LO(pe->phb->buid), option);
271 * pseries_eeh_get_pe_addr - Retrieve PE address
274 * Retrieve the assocated PE address. Actually, there're 2 RTAS
275 * function calls dedicated for the purpose. We need implement
276 * it through the new function and then the old one. Besides,
277 * you should make sure the config address is figured out from
278 * FDT node before calling the function.
280 * It's notable that zero'ed return value means invalid PE config
283 static int pseries_eeh_get_pe_addr(struct eeh_pe *pe)
288 if (ibm_get_config_addr_info2 != RTAS_UNKNOWN_SERVICE) {
290 * First of all, we need to make sure there has one PE
291 * associated with the device. Otherwise, PE address is
294 ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
295 pe->config_addr, BUID_HI(pe->phb->buid),
296 BUID_LO(pe->phb->buid), 1);
297 if (ret || (rets[0] == 0))
300 /* Retrieve the associated PE config address */
301 ret = rtas_call(ibm_get_config_addr_info2, 4, 2, rets,
302 pe->config_addr, BUID_HI(pe->phb->buid),
303 BUID_LO(pe->phb->buid), 0);
305 pr_warning("%s: Failed to get address for PHB#%d-PE#%x\n",
306 __func__, pe->phb->global_number, pe->config_addr);
313 if (ibm_get_config_addr_info != RTAS_UNKNOWN_SERVICE) {
314 ret = rtas_call(ibm_get_config_addr_info, 4, 2, rets,
315 pe->config_addr, BUID_HI(pe->phb->buid),
316 BUID_LO(pe->phb->buid), 0);
318 pr_warning("%s: Failed to get address for PHB#%d-PE#%x\n",
319 __func__, pe->phb->global_number, pe->config_addr);
330 * pseries_eeh_get_state - Retrieve PE state
332 * @state: return value
334 * Retrieve the state of the specified PE. On RTAS compliant
335 * pseries platform, there already has one dedicated RTAS function
336 * for the purpose. It's notable that the associated PE config address
337 * might be ready when calling the function. Therefore, endeavour to
338 * use the PE config address if possible. Further more, there're 2
339 * RTAS calls for the purpose, we need to try the new one and back
340 * to the old one if the new one couldn't work properly.
342 static int pseries_eeh_get_state(struct eeh_pe *pe, int *state)
349 /* Figure out PE config address if possible */
350 config_addr = pe->config_addr;
352 config_addr = pe->addr;
354 if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
355 ret = rtas_call(ibm_read_slot_reset_state2, 3, 4, rets,
356 config_addr, BUID_HI(pe->phb->buid),
357 BUID_LO(pe->phb->buid));
358 } else if (ibm_read_slot_reset_state != RTAS_UNKNOWN_SERVICE) {
359 /* Fake PE unavailable info */
361 ret = rtas_call(ibm_read_slot_reset_state, 3, 3, rets,
362 config_addr, BUID_HI(pe->phb->buid),
363 BUID_LO(pe->phb->buid));
365 return EEH_STATE_NOT_SUPPORT;
371 /* Parse the result out */
376 result &= ~EEH_STATE_RESET_ACTIVE;
377 result |= EEH_STATE_MMIO_ACTIVE;
378 result |= EEH_STATE_DMA_ACTIVE;
381 result |= EEH_STATE_RESET_ACTIVE;
382 result |= EEH_STATE_MMIO_ACTIVE;
383 result |= EEH_STATE_DMA_ACTIVE;
386 result &= ~EEH_STATE_RESET_ACTIVE;
387 result &= ~EEH_STATE_MMIO_ACTIVE;
388 result &= ~EEH_STATE_DMA_ACTIVE;
391 result &= ~EEH_STATE_RESET_ACTIVE;
392 result &= ~EEH_STATE_MMIO_ACTIVE;
393 result &= ~EEH_STATE_DMA_ACTIVE;
394 result |= EEH_STATE_MMIO_ENABLED;
398 if (state) *state = rets[2];
399 result = EEH_STATE_UNAVAILABLE;
401 result = EEH_STATE_NOT_SUPPORT;
405 result = EEH_STATE_NOT_SUPPORT;
408 result = EEH_STATE_NOT_SUPPORT;
415 * pseries_eeh_reset - Reset the specified PE
417 * @option: reset option
419 * Reset the specified PE
421 static int pseries_eeh_reset(struct eeh_pe *pe, int option)
426 /* Figure out PE address */
427 config_addr = pe->config_addr;
429 config_addr = pe->addr;
431 /* Reset PE through RTAS call */
432 ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
433 config_addr, BUID_HI(pe->phb->buid),
434 BUID_LO(pe->phb->buid), option);
436 /* If fundamental-reset not supported, try hot-reset */
437 if (option == EEH_RESET_FUNDAMENTAL &&
439 ret = rtas_call(ibm_set_slot_reset, 4, 1, NULL,
440 config_addr, BUID_HI(pe->phb->buid),
441 BUID_LO(pe->phb->buid), EEH_RESET_HOT);
448 * pseries_eeh_wait_state - Wait for PE state
450 * @max_wait: maximal period in microsecond
452 * Wait for the state of associated PE. It might take some time
453 * to retrieve the PE's state.
455 static int pseries_eeh_wait_state(struct eeh_pe *pe, int max_wait)
461 * According to PAPR, the state of PE might be temporarily
462 * unavailable. Under the circumstance, we have to wait
463 * for indicated time determined by firmware. The maximal
464 * wait time is 5 minutes, which is acquired from the original
465 * EEH implementation. Also, the original implementation
466 * also defined the minimal wait time as 1 second.
468 #define EEH_STATE_MIN_WAIT_TIME (1000)
469 #define EEH_STATE_MAX_WAIT_TIME (300 * 1000)
472 ret = pseries_eeh_get_state(pe, &mwait);
475 * If the PE's state is temporarily unavailable,
476 * we have to wait for the specified time. Otherwise,
477 * the PE's state will be returned immediately.
479 if (ret != EEH_STATE_UNAVAILABLE)
483 pr_warning("%s: Timeout when getting PE's state (%d)\n",
485 return EEH_STATE_NOT_SUPPORT;
489 pr_warning("%s: Firmware returned bad wait value %d\n",
491 mwait = EEH_STATE_MIN_WAIT_TIME;
492 } else if (mwait > EEH_STATE_MAX_WAIT_TIME) {
493 pr_warning("%s: Firmware returned too long wait value %d\n",
495 mwait = EEH_STATE_MAX_WAIT_TIME;
502 return EEH_STATE_NOT_SUPPORT;
506 * pseries_eeh_get_log - Retrieve error log
508 * @severity: temporary or permanent error log
509 * @drv_log: driver log to be combined with retrieved error log
510 * @len: length of driver log
512 * Retrieve the temporary or permanent error from the PE.
513 * Actually, the error will be retrieved through the dedicated
516 static int pseries_eeh_get_log(struct eeh_pe *pe, int severity, char *drv_log, unsigned long len)
522 spin_lock_irqsave(&slot_errbuf_lock, flags);
523 memset(slot_errbuf, 0, eeh_error_buf_size);
525 /* Figure out the PE address */
526 config_addr = pe->config_addr;
528 config_addr = pe->addr;
530 ret = rtas_call(ibm_slot_error_detail, 8, 1, NULL, config_addr,
531 BUID_HI(pe->phb->buid), BUID_LO(pe->phb->buid),
532 virt_to_phys(drv_log), len,
533 virt_to_phys(slot_errbuf), eeh_error_buf_size,
536 log_error(slot_errbuf, ERR_TYPE_RTAS_LOG, 0);
537 spin_unlock_irqrestore(&slot_errbuf_lock, flags);
543 * pseries_eeh_configure_bridge - Configure PCI bridges in the indicated PE
546 * The function will be called to reconfigure the bridges included
547 * in the specified PE so that the mulfunctional PE would be recovered
550 static int pseries_eeh_configure_bridge(struct eeh_pe *pe)
555 /* Figure out the PE address */
556 config_addr = pe->config_addr;
558 config_addr = pe->addr;
560 /* Use new configure-pe function, if supported */
561 if (ibm_configure_pe != RTAS_UNKNOWN_SERVICE) {
562 ret = rtas_call(ibm_configure_pe, 3, 1, NULL,
563 config_addr, BUID_HI(pe->phb->buid),
564 BUID_LO(pe->phb->buid));
565 } else if (ibm_configure_bridge != RTAS_UNKNOWN_SERVICE) {
566 ret = rtas_call(ibm_configure_bridge, 3, 1, NULL,
567 config_addr, BUID_HI(pe->phb->buid),
568 BUID_LO(pe->phb->buid));
574 pr_warning("%s: Unable to configure bridge PHB#%d-PE#%x (%d)\n",
575 __func__, pe->phb->global_number, pe->addr, ret);
581 * pseries_eeh_read_config - Read PCI config space
583 * @where: PCI address
584 * @size: size to read
587 * Read config space from the speicifed device
589 static int pseries_eeh_read_config(struct device_node *dn, int where, int size, u32 *val)
595 return rtas_read_config(pdn, where, size, val);
599 * pseries_eeh_write_config - Write PCI config space
601 * @where: PCI address
602 * @size: size to write
603 * @val: value to be written
605 * Write config space to the specified device
607 static int pseries_eeh_write_config(struct device_node *dn, int where, int size, u32 val)
613 return rtas_write_config(pdn, where, size, val);
616 static struct eeh_ops pseries_eeh_ops = {
618 .init = pseries_eeh_init,
619 .of_probe = pseries_eeh_of_probe,
621 .set_option = pseries_eeh_set_option,
622 .get_pe_addr = pseries_eeh_get_pe_addr,
623 .get_state = pseries_eeh_get_state,
624 .reset = pseries_eeh_reset,
625 .wait_state = pseries_eeh_wait_state,
626 .get_log = pseries_eeh_get_log,
627 .configure_bridge = pseries_eeh_configure_bridge,
628 .read_config = pseries_eeh_read_config,
629 .write_config = pseries_eeh_write_config
633 * eeh_pseries_init - Register platform dependent EEH operations
635 * EEH initialization on pseries platform. This function should be
636 * called before any EEH related functions.
638 static int __init eeh_pseries_init(void)
642 if (!machine_is(pseries))
645 ret = eeh_ops_register(&pseries_eeh_ops);
647 pr_info("EEH: pSeries platform initialized\n");
649 pr_info("EEH: pSeries platform initialization failure (%d)\n",
655 early_initcall(eeh_pseries_init);