3 * Copyright (C) 2001 Dave Engebretsen & Todd Inglett IBM Corporation
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/init.h>
21 #include <linux/pci.h>
22 #include <linux/proc_fs.h>
23 #include <linux/bootmem.h>
25 #include <linux/rbtree.h>
26 #include <linux/spinlock.h>
27 #include <linux/seq_file.h>
29 #include <asm/processor.h>
32 #include <asm/machdep.h>
33 #include <asm/pgtable.h>
39 #define BUID_HI(buid) ((buid) >> 32)
40 #define BUID_LO(buid) ((buid) & 0xffffffff)
41 #define CONFIG_ADDR(busno, devfn) \
42 (((((busno) & 0xff) << 8) | ((devfn) & 0xf8)) << 8)
45 static int ibm_set_eeh_option;
46 static int ibm_set_slot_reset;
47 static int ibm_read_slot_reset_state;
48 static int ibm_read_slot_reset_state2;
49 static int ibm_slot_error_detail;
51 static int eeh_subsystem_enabled;
53 /* Buffer for reporting slot-error-detail rtas calls */
54 static unsigned char slot_errbuf[RTAS_ERROR_LOG_MAX];
55 static spinlock_t slot_errbuf_lock = SPIN_LOCK_UNLOCKED;
56 static int eeh_error_buf_size;
58 /* System monitoring statistics */
59 static DEFINE_PER_CPU(unsigned long, total_mmio_ffs);
60 static DEFINE_PER_CPU(unsigned long, false_positives);
61 static DEFINE_PER_CPU(unsigned long, ignored_failures);
64 * The pci address cache subsystem. This subsystem places
65 * PCI device address resources into a red-black tree, sorted
66 * according to the address range, so that given only an i/o
67 * address, the corresponding PCI device can be **quickly**
70 * Currently, the only customer of this code is the EEH subsystem;
71 * thus, this code has been somewhat tailored to suit EEH better.
72 * In particular, the cache does *not* hold the addresses of devices
73 * for which EEH is not enabled.
75 * (Implementation Note: The RB tree seems to be better/faster
76 * than any hash algo I could think of for this problem, even
77 * with the penalty of slow pointer chases for d-cache misses).
79 struct pci_io_addr_range
81 struct rb_node rb_node;
82 unsigned long addr_lo;
83 unsigned long addr_hi;
84 struct pci_dev *pcidev;
88 static struct pci_io_addr_cache
90 struct rb_root rb_root;
92 } pci_io_addr_cache_root;
94 static inline struct pci_dev *__pci_get_device_by_addr(unsigned long addr)
96 struct rb_node *n = pci_io_addr_cache_root.rb_root.rb_node;
99 struct pci_io_addr_range *piar;
100 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
102 if (addr < piar->addr_lo) {
105 if (addr > piar->addr_hi) {
108 pci_dev_get(piar->pcidev);
118 * pci_get_device_by_addr - Get device, given only address
119 * @addr: mmio (PIO) phys address or i/o port number
121 * Given an mmio phys address, or a port number, find a pci device
122 * that implements this address. Be sure to pci_dev_put the device
123 * when finished. I/O port numbers are assumed to be offset
124 * from zero (that is, they do *not* have pci_io_addr added in).
125 * It is safe to call this function within an interrupt.
127 static struct pci_dev *pci_get_device_by_addr(unsigned long addr)
132 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
133 dev = __pci_get_device_by_addr(addr);
134 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
140 * Handy-dandy debug print routine, does nothing more
141 * than print out the contents of our addr cache.
143 static void pci_addr_cache_print(struct pci_io_addr_cache *cache)
148 n = rb_first(&cache->rb_root);
150 struct pci_io_addr_range *piar;
151 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
152 printk(KERN_DEBUG "PCI: %s addr range %d [%lx-%lx]: %s %s\n",
153 (piar->flags & IORESOURCE_IO) ? "i/o" : "mem", cnt,
154 piar->addr_lo, piar->addr_hi, pci_name(piar->pcidev),
155 pci_pretty_name(piar->pcidev));
162 /* Insert address range into the rb tree. */
163 static struct pci_io_addr_range *
164 pci_addr_cache_insert(struct pci_dev *dev, unsigned long alo,
165 unsigned long ahi, unsigned int flags)
167 struct rb_node **p = &pci_io_addr_cache_root.rb_root.rb_node;
168 struct rb_node *parent = NULL;
169 struct pci_io_addr_range *piar;
171 /* Walk tree, find a place to insert into tree */
174 piar = rb_entry(parent, struct pci_io_addr_range, rb_node);
175 if (alo < piar->addr_lo) {
176 p = &parent->rb_left;
177 } else if (ahi > piar->addr_hi) {
178 p = &parent->rb_right;
180 if (dev != piar->pcidev ||
181 alo != piar->addr_lo || ahi != piar->addr_hi) {
182 printk(KERN_WARNING "PIAR: overlapping address range\n");
187 piar = (struct pci_io_addr_range *)kmalloc(sizeof(struct pci_io_addr_range), GFP_ATOMIC);
196 rb_link_node(&piar->rb_node, parent, p);
197 rb_insert_color(&piar->rb_node, &pci_io_addr_cache_root.rb_root);
202 static void __pci_addr_cache_insert_device(struct pci_dev *dev)
204 struct device_node *dn;
208 dn = pci_device_to_OF_node(dev);
210 printk(KERN_WARNING "PCI: no pci dn found for dev=%s %s\n",
211 pci_name(dev), pci_pretty_name(dev));
215 /* Skip any devices for which EEH is not enabled. */
216 if (!(dn->eeh_mode & EEH_MODE_SUPPORTED) ||
217 dn->eeh_mode & EEH_MODE_NOCHECK) {
219 printk(KERN_INFO "PCI: skip building address cache for=%s %s\n",
220 pci_name(dev), pci_pretty_name(dev));
225 /* The cache holds a reference to the device... */
228 /* Walk resources on this device, poke them into the tree */
229 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
230 unsigned long start = pci_resource_start(dev,i);
231 unsigned long end = pci_resource_end(dev,i);
232 unsigned int flags = pci_resource_flags(dev,i);
234 /* We are interested only bus addresses, not dma or other stuff */
235 if (0 == (flags & (IORESOURCE_IO | IORESOURCE_MEM)))
237 if (start == 0 || ~start == 0 || end == 0 || ~end == 0)
239 pci_addr_cache_insert(dev, start, end, flags);
243 /* If there was nothing to add, the cache has no reference... */
249 * pci_addr_cache_insert_device - Add a device to the address cache
250 * @dev: PCI device whose I/O addresses we are interested in.
252 * In order to support the fast lookup of devices based on addresses,
253 * we maintain a cache of devices that can be quickly searched.
254 * This routine adds a device to that cache.
256 void pci_addr_cache_insert_device(struct pci_dev *dev)
260 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
261 __pci_addr_cache_insert_device(dev);
262 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
265 static inline void __pci_addr_cache_remove_device(struct pci_dev *dev)
271 n = rb_first(&pci_io_addr_cache_root.rb_root);
273 struct pci_io_addr_range *piar;
274 piar = rb_entry(n, struct pci_io_addr_range, rb_node);
276 if (piar->pcidev == dev) {
277 rb_erase(n, &pci_io_addr_cache_root.rb_root);
285 /* The cache no longer holds its reference to this device... */
291 * pci_addr_cache_remove_device - remove pci device from addr cache
292 * @dev: device to remove
294 * Remove a device from the addr-cache tree.
295 * This is potentially expensive, since it will walk
296 * the tree multiple times (once per resource).
297 * But so what; device removal doesn't need to be that fast.
299 void pci_addr_cache_remove_device(struct pci_dev *dev)
303 spin_lock_irqsave(&pci_io_addr_cache_root.piar_lock, flags);
304 __pci_addr_cache_remove_device(dev);
305 spin_unlock_irqrestore(&pci_io_addr_cache_root.piar_lock, flags);
309 * pci_addr_cache_build - Build a cache of I/O addresses
311 * Build a cache of pci i/o addresses. This cache will be used to
312 * find the pci device that corresponds to a given address.
313 * This routine scans all pci busses to build the cache.
314 * Must be run late in boot process, after the pci controllers
315 * have been scaned for devices (after all device resources are known).
317 void __init pci_addr_cache_build(void)
319 struct pci_dev *dev = NULL;
321 spin_lock_init(&pci_io_addr_cache_root.piar_lock);
323 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
324 /* Ignore PCI bridges ( XXX why ??) */
325 if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE) {
328 pci_addr_cache_insert_device(dev);
332 /* Verify tree built up above, echo back the list of addrs. */
333 pci_addr_cache_print(&pci_io_addr_cache_root);
338 * eeh_token_to_phys - convert EEH address token to phys address
339 * @token i/o token, should be address in the form 0xE....
341 static inline unsigned long eeh_token_to_phys(unsigned long token)
346 ptep = find_linux_pte(ioremap_mm.pgd, token);
349 pa = pte_pfn(*ptep) << PAGE_SHIFT;
351 return pa | (token & (PAGE_SIZE-1));
355 * eeh_dn_check_failure - check if all 1's data is due to EEH slot freeze
357 * @dev pci device, if known
359 * Check for an EEH failure for the given device node. Call this
360 * routine if the result of a read was all 0xff's and you want to
361 * find out if this is due to an EEH slot freeze event. This routine
362 * will query firmware for the EEH status.
364 * Returns 0 if there has not been an EEH error; otherwise returns
367 * It is safe to call this routine in an interrupt context.
369 int eeh_dn_check_failure(struct device_node *dn, struct pci_dev *dev)
375 __get_cpu_var(total_mmio_ffs)++;
377 if (!eeh_subsystem_enabled)
383 /* Access to IO BARs might get this far and still not want checking. */
384 if (!(dn->eeh_mode & EEH_MODE_SUPPORTED) ||
385 dn->eeh_mode & EEH_MODE_NOCHECK) {
389 if (!dn->eeh_config_addr) {
394 * Now test for an EEH failure. This is VERY expensive.
395 * Note that the eeh_config_addr may be a parent device
396 * in the case of a device behind a bridge, or it may be
397 * function zero of a multi-function device.
398 * In any case they must share a common PHB.
400 ret = read_slot_reset_state(dn, rets);
402 if (ret == 0 && rets[1] == 1 && (rets[0] == 2 || rets[0] == 4)) {
405 spin_lock_irqsave(&slot_errbuf_lock, flags);
406 memset(slot_errbuf, 0, eeh_error_buf_size);
408 log_event = rtas_call(ibm_slot_error_detail,
409 8, 1, NULL, dn->eeh_config_addr,
410 BUID_HI(dn->phb->buid),
411 BUID_LO(dn->phb->buid), NULL, 0,
412 virt_to_phys(slot_errbuf),
414 1 /* Temporary Error */);
417 log_error(slot_errbuf, ERR_TYPE_RTAS_LOG,
420 spin_unlock_irqrestore(&slot_errbuf_lock, flags);
422 printk(KERN_INFO "EEH: MMIO failure (%d) on device: %s %s\n",
423 rets[0], dn->name, dn->full_name);
427 * XXX We should create a separate sysctl for this.
429 * Since the panic_on_oops sysctl is used to halt
430 * the system in light of potential corruption, we
434 panic("EEH: MMIO failure (%d) on device: %s %s\n",
435 rets[0], dn->name, dn->full_name);
437 __get_cpu_var(ignored_failures)++;
440 __get_cpu_var(false_positives)++;
446 EXPORT_SYMBOL(eeh_dn_check_failure);
449 * read_slot_reset_state - get the current state of a slot for a
452 * @dn device node for the slot to check
453 * @rets array to return results in
455 static int read_slot_reset_state(struct device_node *dn, unsigned long rets[])
459 if (ibm_read_slot_reset_state2 != RTAS_UNKNOWN_SERVICE) {
460 token = ibm_read_slot_reset_state2;
463 token = ibm_read_slot_reset_state;
467 return rtas_call(token, 3, outputs, rets, dn->eeh_config_addr,
468 BUID_HI(dn->phb->buid), BUID_LO(dn->phb->buid));
472 * eeh_check_failure - check if all 1's data is due to EEH slot freeze
473 * @token i/o token, should be address in the form 0xA....
474 * @val value, should be all 1's (XXX why do we need this arg??)
476 * Check for an eeh failure at the given token address.
477 * Check for an EEH failure at the given token address. Call this
478 * routine if the result of a read was all 0xff's and you want to
479 * find out if this is due to an EEH slot freeze event. This routine
480 * will query firmware for the EEH status.
482 * Note this routine is safe to call in an interrupt context.
484 unsigned long eeh_check_failure(const volatile void __iomem *token, unsigned long val)
488 struct device_node *dn;
490 /* Finding the phys addr + pci device; this is pretty quick. */
491 addr = eeh_token_to_phys((unsigned long __force) token);
492 dev = pci_get_device_by_addr(addr);
496 dn = pci_device_to_OF_node(dev);
497 eeh_dn_check_failure (dn, dev);
503 EXPORT_SYMBOL(eeh_check_failure);
505 struct eeh_early_enable_info {
506 unsigned int buid_hi;
507 unsigned int buid_lo;
510 /* Enable eeh for the given device node. */
511 static void *early_enable_eeh(struct device_node *dn, void *data)
513 struct eeh_early_enable_info *info = data;
515 char *status = get_property(dn, "status", NULL);
516 u32 *class_code = (u32 *)get_property(dn, "class-code", NULL);
517 u32 *vendor_id = (u32 *)get_property(dn, "vendor-id", NULL);
518 u32 *device_id = (u32 *)get_property(dn, "device-id", NULL);
524 if (status && strcmp(status, "ok") != 0)
525 return NULL; /* ignore devices with bad status */
527 /* Ignore bad nodes. */
528 if (!class_code || !vendor_id || !device_id)
531 /* There is nothing to check on PCI to ISA bridges */
532 if (dn->type && !strcmp(dn->type, "isa")) {
533 dn->eeh_mode |= EEH_MODE_NOCHECK;
538 * Now decide if we are going to "Disable" EEH checking
539 * for this device. We still run with the EEH hardware active,
540 * but we won't be checking for ff's. This means a driver
541 * could return bad data (very bad!), an interrupt handler could
542 * hang waiting on status bits that won't change, etc.
543 * But there are a few cases like display devices that make sense.
545 enable = 1; /* i.e. we will do checking */
546 if ((*class_code >> 16) == PCI_BASE_CLASS_DISPLAY)
550 dn->eeh_mode |= EEH_MODE_NOCHECK;
552 /* Ok... see if this device supports EEH. Some do, some don't,
553 * and the only way to find out is to check each and every one. */
554 regs = (u32 *)get_property(dn, "reg", NULL);
556 /* First register entry is addr (00BBSS00) */
557 /* Try to enable eeh */
558 ret = rtas_call(ibm_set_eeh_option, 4, 1, NULL,
559 regs[0], info->buid_hi, info->buid_lo,
562 eeh_subsystem_enabled = 1;
563 dn->eeh_mode |= EEH_MODE_SUPPORTED;
564 dn->eeh_config_addr = regs[0];
566 printk(KERN_DEBUG "EEH: %s: eeh enabled\n", dn->full_name);
570 /* This device doesn't support EEH, but it may have an
571 * EEH parent, in which case we mark it as supported. */
572 if (dn->parent && (dn->parent->eeh_mode & EEH_MODE_SUPPORTED)) {
573 /* Parent supports EEH. */
574 dn->eeh_mode |= EEH_MODE_SUPPORTED;
575 dn->eeh_config_addr = dn->parent->eeh_config_addr;
580 printk(KERN_WARNING "EEH: %s: unable to get reg property.\n",
588 * Initialize EEH by trying to enable it for all of the adapters in the system.
589 * As a side effect we can determine here if eeh is supported at all.
590 * Note that we leave EEH on so failed config cycles won't cause a machine
591 * check. If a user turns off EEH for a particular adapter they are really
592 * telling Linux to ignore errors. Some hardware (e.g. POWER5) won't
593 * grant access to a slot if EEH isn't enabled, and so we always enable
594 * EEH for all slots/all devices.
596 * The eeh-force-off option disables EEH checking globally, for all slots.
597 * Even if force-off is set, the EEH hardware is still enabled, so that
598 * newer systems can boot.
600 void __init eeh_init(void)
602 struct device_node *phb, *np;
603 struct eeh_early_enable_info info;
605 init_pci_config_tokens();
607 np = of_find_node_by_path("/rtas");
611 ibm_set_eeh_option = rtas_token("ibm,set-eeh-option");
612 ibm_set_slot_reset = rtas_token("ibm,set-slot-reset");
613 ibm_read_slot_reset_state = rtas_token("ibm,read-slot-reset-state");
614 ibm_read_slot_reset_state2 = rtas_token("ibm,read-slot-reset-state2");
615 ibm_slot_error_detail = rtas_token("ibm,slot-error-detail");
617 if (ibm_set_eeh_option == RTAS_UNKNOWN_SERVICE)
620 eeh_error_buf_size = rtas_token("rtas-error-log-max");
621 if (eeh_error_buf_size == RTAS_UNKNOWN_SERVICE) {
622 eeh_error_buf_size = 1024;
624 if (eeh_error_buf_size > RTAS_ERROR_LOG_MAX) {
625 printk(KERN_WARNING "EEH: rtas-error-log-max is bigger than allocated "
626 "buffer ! (%d vs %d)", eeh_error_buf_size, RTAS_ERROR_LOG_MAX);
627 eeh_error_buf_size = RTAS_ERROR_LOG_MAX;
630 /* Enable EEH for all adapters. Note that eeh requires buid's */
631 for (phb = of_find_node_by_name(NULL, "pci"); phb;
632 phb = of_find_node_by_name(phb, "pci")) {
635 buid = get_phb_buid(phb);
639 info.buid_lo = BUID_LO(buid);
640 info.buid_hi = BUID_HI(buid);
641 traverse_pci_devices(phb, early_enable_eeh, &info);
644 if (eeh_subsystem_enabled)
645 printk(KERN_INFO "EEH: PCI Enhanced I/O Error Handling Enabled\n");
647 printk(KERN_WARNING "EEH: No capable adapters found\n");
651 * eeh_add_device_early - enable EEH for the indicated device_node
652 * @dn: device node for which to set up EEH
654 * This routine must be used to perform EEH initialization for PCI
655 * devices that were added after system boot (e.g. hotplug, dlpar).
656 * This routine must be called before any i/o is performed to the
657 * adapter (inluding any config-space i/o).
658 * Whether this actually enables EEH or not for this device depends
659 * on the CEC architecture, type of the device, on earlier boot
660 * command-line arguments & etc.
662 void eeh_add_device_early(struct device_node *dn)
664 struct pci_controller *phb;
665 struct eeh_early_enable_info info;
667 if (!dn || !eeh_subsystem_enabled)
670 if (NULL == phb || 0 == phb->buid) {
671 printk(KERN_WARNING "EEH: Expected buid but found none\n");
675 info.buid_hi = BUID_HI(phb->buid);
676 info.buid_lo = BUID_LO(phb->buid);
677 early_enable_eeh(dn, &info);
679 EXPORT_SYMBOL(eeh_add_device_early);
682 * eeh_add_device_late - perform EEH initialization for the indicated pci device
683 * @dev: pci device for which to set up EEH
685 * This routine must be used to complete EEH initialization for PCI
686 * devices that were added after system boot (e.g. hotplug, dlpar).
688 void eeh_add_device_late(struct pci_dev *dev)
690 if (!dev || !eeh_subsystem_enabled)
694 printk(KERN_DEBUG "EEH: adding device %s %s\n", pci_name(dev),
695 pci_pretty_name(dev));
698 pci_addr_cache_insert_device (dev);
700 EXPORT_SYMBOL(eeh_add_device_late);
703 * eeh_remove_device - undo EEH setup for the indicated pci device
704 * @dev: pci device to be removed
706 * This routine should be when a device is removed from a running
707 * system (e.g. by hotplug or dlpar).
709 void eeh_remove_device(struct pci_dev *dev)
711 if (!dev || !eeh_subsystem_enabled)
714 /* Unregister the device with the EEH/PCI address search system */
716 printk(KERN_DEBUG "EEH: remove device %s %s\n", pci_name(dev),
717 pci_pretty_name(dev));
719 pci_addr_cache_remove_device(dev);
721 EXPORT_SYMBOL(eeh_remove_device);
723 static int proc_eeh_show(struct seq_file *m, void *v)
726 unsigned long ffs = 0, positives = 0, failures = 0;
729 ffs += per_cpu(total_mmio_ffs, cpu);
730 positives += per_cpu(false_positives, cpu);
731 failures += per_cpu(ignored_failures, cpu);
734 if (0 == eeh_subsystem_enabled) {
735 seq_printf(m, "EEH Subsystem is globally disabled\n");
736 seq_printf(m, "eeh_total_mmio_ffs=%ld\n", ffs);
738 seq_printf(m, "EEH Subsystem is enabled\n");
739 seq_printf(m, "eeh_total_mmio_ffs=%ld\n"
740 "eeh_false_positives=%ld\n"
741 "eeh_ignored_failures=%ld\n",
742 ffs, positives, failures);
748 static int proc_eeh_open(struct inode *inode, struct file *file)
750 return single_open(file, proc_eeh_show, NULL);
753 static struct file_operations proc_eeh_operations = {
754 .open = proc_eeh_open,
757 .release = single_release,
760 static int __init eeh_init_proc(void)
762 struct proc_dir_entry *e;
764 if (systemcfg->platform & PLATFORM_PSERIES) {
765 e = create_proc_entry("ppc64/eeh", 0, NULL);
767 e->proc_fops = &proc_eeh_operations;
772 __initcall(eeh_init_proc);