Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / arch / i386 / kernel / timers / timer_pm.c
index 7aa63c9..144e94a 100644 (file)
 #include <linux/module.h>
 #include <linux/device.h>
 #include <linux/init.h>
+#include <linux/pci.h>
 #include <asm/types.h>
 #include <asm/timer.h>
 #include <asm/smp.h>
 #include <asm/io.h>
 #include <asm/arch_hooks.h>
 
+#include <linux/timex.h>
+#include "mach_timer.h"
+
+/* Number of PMTMR ticks expected during calibration run */
+#define PMTMR_TICKS_PER_SEC 3579545
+#define PMTMR_EXPECTED_RATE \
+  ((CALIBRATE_LATCH * (PMTMR_TICKS_PER_SEC >> 10)) / (CLOCK_TICK_RATE>>10))
+
 
 /* The I/O port the PMTMR resides at.
  * The location is detected during setup_arch(),
@@ -37,26 +46,60 @@ static seqlock_t monotonic_lock = SEQLOCK_UNLOCKED;
 
 #define ACPI_PM_MASK 0xFFFFFF /* limit it to 24 bits */
 
+static int pmtmr_need_workaround __read_mostly = 1;
+
 /*helper function to safely read acpi pm timesource*/
 static inline u32 read_pmtmr(void)
 {
-       u32 v1=0,v2=0,v3=0;
-       /* It has been reported that because of various broken
-        * chipsets (ICH4, PIIX4 and PIIX4E) where the ACPI PM time
-        * source is not latched, so you must read it multiple
-        * times to insure a safe value is read.
-        */
-       do {
-               v1 = inl(pmtmr_ioport);
-               v2 = inl(pmtmr_ioport);
-               v3 = inl(pmtmr_ioport);
-       } while ((v1 > v2 && v1 < v3) || (v2 > v3 && v2 < v1)
-                       || (v3 > v1 && v3 < v2));
-
-       /* mask the output to 24 bits */
-       return v2 & ACPI_PM_MASK;
+       if (pmtmr_need_workaround) {
+               u32 v1, v2, v3;
+
+               /* It has been reported that because of various broken
+                * chipsets (ICH4, PIIX4 and PIIX4E) where the ACPI PM time
+                * source is not latched, so you must read it multiple
+                * times to insure a safe value is read.
+                */
+               do {
+                       v1 = inl(pmtmr_ioport);
+                       v2 = inl(pmtmr_ioport);
+                       v3 = inl(pmtmr_ioport);
+               } while ((v1 > v2 && v1 < v3) || (v2 > v3 && v2 < v1)
+                        || (v3 > v1 && v3 < v2));
+
+               /* mask the output to 24 bits */
+               return v2 & ACPI_PM_MASK;
+       }
+
+       return inl(pmtmr_ioport) & ACPI_PM_MASK;
+}
+
+
+/*
+ * Some boards have the PMTMR running way too fast. We check
+ * the PMTMR rate against PIT channel 2 to catch these cases.
+ */
+static int verify_pmtmr_rate(void)
+{
+       u32 value1, value2;
+       unsigned long count, delta;
+
+       mach_prepare_counter();
+       value1 = read_pmtmr();
+       mach_countup(&count);
+       value2 = read_pmtmr();
+       delta = (value2 - value1) & ACPI_PM_MASK;
+
+       /* Check that the PMTMR delta is within 5% of what we expect */
+       if (delta < (PMTMR_EXPECTED_RATE * 19) / 20 ||
+           delta > (PMTMR_EXPECTED_RATE * 21) / 20) {
+               printk(KERN_INFO "PM-Timer running at invalid rate: %lu%% of normal - aborting.\n", 100UL * delta / PMTMR_EXPECTED_RATE);
+               return -1;
+       }
+
+       return 0;
 }
 
+
 static int init_pmtmr(char* override)
 {
        u32 value1, value2;
@@ -89,6 +132,9 @@ static int init_pmtmr(char* override)
        return -ENODEV;
 
 pm_good:
+       if (verify_pmtmr_rate() != 0)
+               return -ENODEV;
+
        init_cpu_khz();
        return 0;
 }
@@ -148,6 +194,14 @@ static void mark_offset_pmtmr(void)
        }
 }
 
+static int pmtmr_resume(void)
+{
+       write_seqlock(&monotonic_lock);
+       /* Assume this is the last mark offset time */
+       offset_tick = read_pmtmr();
+       write_sequnlock(&monotonic_lock);
+       return 0;
+}
 
 static unsigned long long monotonic_clock_pmtmr(void)
 {
@@ -202,15 +256,86 @@ static unsigned long get_offset_pmtmr(void)
 
 
 /* acpi timer_opts struct */
-struct timer_opts timer_pmtmr = {
+static struct timer_opts timer_pmtmr = {
        .name                   = "pmtmr",
-       .init                   = init_pmtmr,
        .mark_offset            = mark_offset_pmtmr,
        .get_offset             = get_offset_pmtmr,
        .monotonic_clock        = monotonic_clock_pmtmr,
        .delay                  = delay_pmtmr,
+       .read_timer             = read_timer_tsc,
+       .resume                 = pmtmr_resume,
 };
 
+struct init_timer_opts __initdata timer_pmtmr_init = {
+       .init = init_pmtmr,
+       .opts = &timer_pmtmr,
+};
+
+#ifdef CONFIG_PCI
+/*
+ * PIIX4 Errata:
+ *
+ * The power management timer may return improper results when read.
+ * Although the timer value settles properly after incrementing,
+ * while incrementing there is a 3 ns window every 69.8 ns where the
+ * timer value is indeterminate (a 4.2% chance that the data will be
+ * incorrect when read). As a result, the ACPI free running count up
+ * timer specification is violated due to erroneous reads.
+ */
+static int __init pmtmr_bug_check(void)
+{
+       static struct pci_device_id gray_list[] __initdata = {
+               /* these chipsets may have bug. */
+               { PCI_DEVICE(PCI_VENDOR_ID_INTEL,
+                               PCI_DEVICE_ID_INTEL_82801DB_0) },
+               { },
+       };
+       struct pci_dev *dev;
+       int pmtmr_has_bug = 0;
+       u8 rev;
+
+       if (cur_timer != &timer_pmtmr || !pmtmr_need_workaround)
+               return 0;
+
+       dev = pci_get_device(PCI_VENDOR_ID_INTEL,
+                            PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
+       if (dev) {
+               pci_read_config_byte(dev, PCI_REVISION_ID, &rev);
+               /* the bug has been fixed in PIIX4M */
+               if (rev < 3) {
+                       printk(KERN_WARNING "* Found PM-Timer Bug on this "
+                               "chipset. Due to workarounds for a bug,\n"
+                               "* this time source is slow.  Consider trying "
+                               "other time sources (clock=)\n");
+                       pmtmr_has_bug = 1;
+               }
+               pci_dev_put(dev);
+       }
+
+       if (pci_dev_present(gray_list)) {
+               printk(KERN_WARNING "* This chipset may have PM-Timer Bug.  Due"
+                       " to workarounds for a bug,\n"
+                       "* this time source is slow. If you are sure your timer"
+                       " does not have\n"
+                       "* this bug, please use \"pmtmr_good\" to disable the "
+                       "workaround\n");
+               pmtmr_has_bug = 1;
+       }
+
+       if (!pmtmr_has_bug)
+               pmtmr_need_workaround = 0;
+
+       return 0;
+}
+device_initcall(pmtmr_bug_check);
+#endif
+
+static int __init pmtr_good_setup(char *__str)
+{
+       pmtmr_need_workaround = 0;
+       return 1;
+}
+__setup("pmtmr_good", pmtr_good_setup);
 
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");