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 / cpu / cpufreq / speedstep-lib.c
index 3305a84..4f46cac 100644 (file)
@@ -9,7 +9,7 @@
  */
 
 #include <linux/kernel.h>
-#include <linux/module.h> 
+#include <linux/module.h>
 #include <linux/moduleparam.h>
 #include <linux/init.h>
 #include <linux/cpufreq.h>
 #include <asm/msr.h>
 #include "speedstep-lib.h"
 
-
-/* DEBUG
- *   Define it if you want verbose debug output, e.g. for bug reporting
- */
-//#define SPEEDSTEP_DEBUG
-
-#ifdef SPEEDSTEP_DEBUG
-#define dprintk(msg...) printk(msg)
-#else
-#define dprintk(msg...) do { } while(0)
-#endif
+#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "speedstep-lib", msg)
 
 #ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK
 static int relaxed_check = 0;
@@ -46,8 +36,8 @@ static unsigned int pentium3_get_frequency (unsigned int processor)
         /* See table 14 of p3_ds.pdf and table 22 of 29834003.pdf */
        struct {
                unsigned int ratio;     /* Frequency Multiplier (x10) */
-               u8 bitmap;              /* power on configuration bits
-                                          [27, 25:22] (in MSR 0x2a) */
+               u8 bitmap;              /* power on configuration bits
+                                       [27, 25:22] (in MSR 0x2a) */
        } msr_decode_mult [] = {
                { 30, 0x01 },
                { 35, 0x05 },
@@ -68,9 +58,9 @@ static unsigned int pentium3_get_frequency (unsigned int processor)
 
        /* PIII(-M) FSB settings: see table b1-b of 24547206.pdf */
        struct {
-               unsigned int value;     /* Front Side Bus speed in MHz */
-               u8 bitmap;              /* power on configuration bits [18: 19]
-                                          (in MSR 0x2a) */
+               unsigned int value;     /* Front Side Bus speed in MHz */
+               u8 bitmap;              /* power on configuration bits [18: 19]
+                                       (in MSR 0x2a) */
        } msr_decode_fsb [] = {
                {  66, 0x0 },
                { 100, 0x2 },
@@ -78,12 +68,12 @@ static unsigned int pentium3_get_frequency (unsigned int processor)
                {   0, 0xff}
        };
 
-       u32     msr_lo, msr_tmp;
-       int     i = 0, j = 0;
+       u32 msr_lo, msr_tmp;
+       int i = 0, j = 0;
 
        /* read MSR 0x2a - we only need the low 32 bits */
        rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
-       dprintk(KERN_DEBUG "speedstep-lib: P3 - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
+       dprintk("P3 - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
        msr_tmp = msr_lo;
 
        /* decode the FSB */
@@ -96,9 +86,10 @@ static unsigned int pentium3_get_frequency (unsigned int processor)
        }
 
        /* decode the multiplier */
-       if (processor == SPEEDSTEP_PROCESSOR_PIII_C_EARLY)
+       if (processor == SPEEDSTEP_PROCESSOR_PIII_C_EARLY) {
+               dprintk("workaround for early PIIIs\n");
                msr_lo &= 0x03c00000;
-       else
+       else
                msr_lo &= 0x0bc00000;
        msr_lo >>= 22;
        while (msr_lo != msr_decode_mult[j].bitmap) {
@@ -107,16 +98,18 @@ static unsigned int pentium3_get_frequency (unsigned int processor)
                j++;
        }
 
+       dprintk("speed is %u\n", (msr_decode_mult[j].ratio * msr_decode_fsb[i].value * 100));
+
        return (msr_decode_mult[j].ratio * msr_decode_fsb[i].value * 100);
 }
 
 
 static unsigned int pentiumM_get_frequency(void)
 {
-       u32     msr_lo, msr_tmp;
+       u32 msr_lo, msr_tmp;
 
        rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
-       dprintk(KERN_DEBUG "speedstep-lib: PM - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
+       dprintk("PM - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
 
        /* see table B-2 of 24547212.pdf */
        if (msr_lo & 0x00040000) {
@@ -125,7 +118,7 @@ static unsigned int pentiumM_get_frequency(void)
        }
 
        msr_tmp = (msr_lo >> 22) & 0x1f;
-       dprintk(KERN_DEBUG "speedstep-lib: bits 22-26 are 0x%x\n", msr_tmp);
+       dprintk("bits 22-26 are 0x%x, speed is %u\n", msr_tmp, (msr_tmp * 100 * 1000));
 
        return (msr_tmp * 100 * 1000);
 }
@@ -139,9 +132,9 @@ static unsigned int pentium4_get_frequency(void)
 
        rdmsr(0x2c, msr_lo, msr_hi);
 
-       dprintk(KERN_DEBUG "speedstep-lib: P4 - MSR_EBC_FREQUENCY_ID: 0x%x 0x%x\n", msr_lo, msr_hi);
+       dprintk("P4 - MSR_EBC_FREQUENCY_ID: 0x%x 0x%x\n", msr_lo, msr_hi);
 
-       /* decode the FSB: see IA-32 Intel (C) Architecture Software 
+       /* decode the FSB: see IA-32 Intel (C) Architecture Software
         * Developer's Manual, Volume 3: System Prgramming Guide,
         * revision #12 in Table B-1: MSRs in the Pentium 4 and
         * Intel Xeon Processors, on page B-4 and B-5.
@@ -167,14 +160,17 @@ static unsigned int pentium4_get_frequency(void)
                printk(KERN_DEBUG "speedstep-lib: couldn't detect FSB speed. Please send an e-mail to <linux@brodo.de>\n");
 
        /* Multiplier. */
-       mult = msr_lo >> 24;
+       if (c->x86_model < 2)
+               mult = msr_lo >> 27;
+       else
+               mult = msr_lo >> 24;
 
-       dprintk(KERN_DEBUG "speedstep-lib: P4 - FSB %u kHz; Multiplier %u\n", fsb, mult);
+       dprintk("P4 - FSB %u kHz; Multiplier %u; Speed %u kHz\n", fsb, mult, (fsb * mult));
 
        return (fsb * mult);
 }
 
+
 unsigned int speedstep_get_processor_frequency(unsigned int processor)
 {
        switch (processor) {
@@ -202,9 +198,11 @@ EXPORT_SYMBOL_GPL(speedstep_get_processor_frequency);
 unsigned int speedstep_detect_processor (void)
 {
        struct cpuinfo_x86 *c = cpu_data;
-       u32                     ebx, msr_lo, msr_hi;
+       u32 ebx, msr_lo, msr_hi;
+
+       dprintk("x86: %x, model: %x\n", c->x86, c->x86_model);
 
-       if ((c->x86_vendor != X86_VENDOR_INTEL) || 
+       if ((c->x86_vendor != X86_VENDOR_INTEL) ||
            ((c->x86 != 6) && (c->x86 != 0xF)))
                return 0;
 
@@ -217,18 +215,18 @@ unsigned int speedstep_detect_processor (void)
                ebx = cpuid_ebx(0x00000001);
                ebx &= 0x000000FF;
 
-               dprintk(KERN_INFO "ebx value is %x, x86_mask is %x\n", ebx, c->x86_mask);
+               dprintk("ebx value is %x, x86_mask is %x\n", ebx, c->x86_mask);
 
                switch (c->x86_mask) {
-               case 4: 
+               case 4:
                        /*
-                        * B-stepping [M-P4-M] 
+                        * B-stepping [M-P4-M]
                         * sample has ebx = 0x0f, production has 0x0e.
                         */
                        if ((ebx == 0x0e) || (ebx == 0x0f))
                                return SPEEDSTEP_PROCESSOR_P4M;
                        break;
-               case 7: 
+               case 7:
                        /*
                         * C-stepping [M-P4-M]
                         * needs to have ebx=0x0e, else it's a celeron:
@@ -252,11 +250,10 @@ unsigned int speedstep_detect_processor (void)
                         * specific.
                         * M-P4-Ms may have either ebx=0xe or 0xf [see above]
                         * M-P4/533 have either ebx=0xe or 0xf. [25317607.pdf]
-                        * So, how to distinguish all those processors with
-                        * ebx=0xf? I don't know. Sort them out, and wait
-                        * for someone to complain.
+                        * also, M-P4M HTs have ebx=0x8, too
+                        * For now, they are distinguished by the model_id string
                         */
-                       if (ebx == 0x0e)
+                       if ((ebx == 0x0e) || (strstr(c->x86_model_id,"Mobile Intel(R) Pentium(R) 4") != NULL))
                                return SPEEDSTEP_PROCESSOR_P4M;
                        break;
                default:
@@ -267,9 +264,9 @@ unsigned int speedstep_detect_processor (void)
 
        switch (c->x86_model) {
        case 0x0B: /* Intel PIII [Tualatin] */
-               /* cpuid_ebx(1) is 0x04 for desktop PIII, 
-                                  0x06 for mobile PIII-M */
+               /* cpuid_ebx(1) is 0x04 for desktop PIII, 0x06 for mobile PIII-M */
                ebx = cpuid_ebx(0x00000001);
+               dprintk("ebx is %x\n", ebx);
 
                ebx &= 0x000000FF;
 
@@ -277,9 +274,8 @@ unsigned int speedstep_detect_processor (void)
                        return 0;
 
                /* So far all PIII-M processors support SpeedStep. See
-                * Intel's 24540640.pdf of June 2003 
+                * Intel's 24540640.pdf of June 2003
                 */
-
                return SPEEDSTEP_PROCESSOR_PIII_T;
 
        case 0x08: /* Intel PIII [Coppermine] */
@@ -287,7 +283,7 @@ unsigned int speedstep_detect_processor (void)
                /* all mobile PIII Coppermines have FSB 100 MHz
                 * ==> sort out a few desktop PIIIs. */
                rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_hi);
-               dprintk(KERN_DEBUG "cpufreq: Coppermine: MSR_IA32_EBL_CR_POWERON is 0x%x, 0x%x\n", msr_lo, msr_hi);
+               dprintk("Coppermine: MSR_IA32_EBL_CR_POWERON is 0x%x, 0x%x\n", msr_lo, msr_hi);
                msr_lo &= 0x00c0000;
                if (msr_lo != 0x0080000)
                        return 0;
@@ -299,11 +295,12 @@ unsigned int speedstep_detect_processor (void)
                 * bit 56 or 57 is set
                 */
                rdmsr(MSR_IA32_PLATFORM_ID, msr_lo, msr_hi);
-               dprintk(KERN_DEBUG "cpufreq: Coppermine: MSR_IA32_PLATFORM ID is 0x%x, 0x%x\n", msr_lo, msr_hi);
+               dprintk("Coppermine: MSR_IA32_PLATFORM ID is 0x%x, 0x%x\n", msr_lo, msr_hi);
                if ((msr_hi & (1<<18)) && (relaxed_check ? 1 : (msr_hi & (3<<24)))) {
-                       if (c->x86_mask == 0x01)
+                       if (c->x86_mask == 0x01) {
+                               dprintk("early PIII version\n");
                                return SPEEDSTEP_PROCESSOR_PIII_C_EARLY;
-                       else
+                       else
                                return SPEEDSTEP_PROCESSOR_PIII_C;
                }
 
@@ -321,40 +318,57 @@ EXPORT_SYMBOL_GPL(speedstep_detect_processor);
 unsigned int speedstep_get_freqs(unsigned int processor,
                                  unsigned int *low_speed,
                                  unsigned int *high_speed,
-                                 void (*set_state) (unsigned int state,
-                                                    unsigned int notify)
-                                )
+                                 unsigned int *transition_latency,
+                                 void (*set_state) (unsigned int state))
 {
        unsigned int prev_speed;
        unsigned int ret = 0;
        unsigned long flags;
+       struct timeval tv1, tv2;
 
        if ((!processor) || (!low_speed) || (!high_speed) || (!set_state))
                return -EINVAL;
 
+       dprintk("trying to determine both speeds\n");
+
        /* get current speed */
        prev_speed = speedstep_get_processor_frequency(processor);
        if (!prev_speed)
                return -EIO;
-       
+
+       dprintk("previous speed is %u\n", prev_speed);
+
        local_irq_save(flags);
 
        /* switch to low state */
-       set_state(SPEEDSTEP_LOW, 0);
+       set_state(SPEEDSTEP_LOW);
        *low_speed = speedstep_get_processor_frequency(processor);
        if (!*low_speed) {
                ret = -EIO;
                goto out;
        }
 
+       dprintk("low speed is %u\n", *low_speed);
+
+       /* start latency measurement */
+       if (transition_latency)
+               do_gettimeofday(&tv1);
+
        /* switch to high state */
-       set_state(SPEEDSTEP_HIGH, 0);
+       set_state(SPEEDSTEP_HIGH);
+
+       /* end latency measurement */
+       if (transition_latency)
+               do_gettimeofday(&tv2);
+
        *high_speed = speedstep_get_processor_frequency(processor);
        if (!*high_speed) {
                ret = -EIO;
                goto out;
        }
 
+       dprintk("high speed is %u\n", *high_speed);
+
        if (*low_speed == *high_speed) {
                ret = -ENODEV;
                goto out;
@@ -362,9 +376,28 @@ unsigned int speedstep_get_freqs(unsigned int processor,
 
        /* switch to previous state, if necessary */
        if (*high_speed != prev_speed)
-               set_state(SPEEDSTEP_LOW, 0);
+               set_state(SPEEDSTEP_LOW);
+
+       if (transition_latency) {
+               *transition_latency = (tv2.tv_sec - tv1.tv_sec) * USEC_PER_SEC +
+                       tv2.tv_usec - tv1.tv_usec;
+               dprintk("transition latency is %u uSec\n", *transition_latency);
+
+               /* convert uSec to nSec and add 20% for safety reasons */
+               *transition_latency *= 1200;
+
+               /* check if the latency measurement is too high or too low
+                * and set it to a safe value (500uSec) in that case
+                */
+               if (*transition_latency > 10000000 || *transition_latency < 50000) {
+                       printk (KERN_WARNING "speedstep: frequency transition measured seems out of "
+                                       "range (%u nSec), falling back to a safe one of %u nSec.\n",
+                                       *transition_latency, 500000);
+                       *transition_latency = 500000;
+               }
+       }
 
- out:
+out:
        local_irq_restore(flags);
        return (ret);
 }