Merge master.kernel.org:/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[pandora-kernel.git] / arch / i386 / kernel / cpu / cpufreq / longhaul.c
index 98fbe28..b59878a 100644 (file)
@@ -8,12 +8,11 @@
  *  VIA have currently 3 different versions of Longhaul.
  *  Version 1 (Longhaul) uses the BCR2 MSR at 0x1147.
  *   It is present only in Samuel 1 (C5A), Samuel 2 (C5B) stepping 0.
- *  Version 2 of longhaul is the same as v1, but adds voltage scaling.
- *   Present in Samuel 2 (steppings 1-7 only) (C5B), and Ezra (C5C)
- *   voltage scaling support has currently been disabled in this driver
- *   until we have code that gets it right.
+ *  Version 2 of longhaul is backward compatible with v1, but adds
+ *   LONGHAUL MSR for purpose of both frequency and voltage scaling.
+ *   Present in Samuel 2 (steppings 1-7 only) (C5B), and Ezra (C5C).
  *  Version 3 of longhaul got renamed to Powersaver and redesigned
- *   to use the POWERSAVER MSR at 0x110a.
+ *   to use only the POWERSAVER MSR at 0x110a.
  *   It is present in Ezra-T (C5M), Nehemiah (C5X) and above.
  *   It's pretty much the same feature wise to longhaul v2, though
  *   there is provision for scaling FSB too, but this doesn't work
@@ -65,7 +64,8 @@ static unsigned int fsb;
 static struct mV_pos *vrm_mV_table;
 static unsigned char *mV_vrm_table;
 struct f_msr {
-       unsigned char vrm;
+       u8 vrm;
+       u8 pos;
 };
 static struct f_msr f_msr_table[32];
 
@@ -75,6 +75,7 @@ static int can_scale_voltage;
 static struct acpi_processor *pr = NULL;
 static struct acpi_processor_cx *cx = NULL;
 static u8 longhaul_flags;
+static u8 longhaul_pos;
 
 /* Module parameters */
 static int scale_voltage;
@@ -165,41 +166,79 @@ static void do_longhaul1(unsigned int clock_ratio_index)
 static void do_powersaver(int cx_address, unsigned int clock_ratio_index)
 {
        union msr_longhaul longhaul;
+       u8 dest_pos;
        u32 t;
 
+       dest_pos = f_msr_table[clock_ratio_index].pos;
+
        rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
+       /* Setup new frequency */
        longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
        longhaul.bits.SoftBusRatio = clock_ratio_index & 0xf;
        longhaul.bits.SoftBusRatio4 = (clock_ratio_index & 0x10) >> 4;
-       longhaul.bits.EnableSoftBusRatio = 1;
-
-       if (can_scale_voltage) {
+       /* Setup new voltage */
+       if (can_scale_voltage)
                longhaul.bits.SoftVID = f_msr_table[clock_ratio_index].vrm;
+       /* Sync to timer tick */
+       safe_halt();
+       /* Raise voltage if necessary */
+       if (can_scale_voltage && longhaul_pos < dest_pos) {
                longhaul.bits.EnableSoftVID = 1;
+               wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
+               /* Change voltage */
+               if (!cx_address) {
+                       ACPI_FLUSH_CPU_CACHE();
+                       halt();
+               } else {
+                       ACPI_FLUSH_CPU_CACHE();
+                       /* Invoke C3 */
+                       inb(cx_address);
+                       /* Dummy op - must do something useless after P_LVL3
+                        * read */
+                       t = inl(acpi_gbl_FADT.xpm_timer_block.address);
+               }
+               longhaul.bits.EnableSoftVID = 0;
+               wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
+               longhaul_pos = dest_pos;
        }
 
-       /* Sync to timer tick */
-       safe_halt();
        /* Change frequency on next halt or sleep */
+       longhaul.bits.EnableSoftBusRatio = 1;
        wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
        if (!cx_address) {
                ACPI_FLUSH_CPU_CACHE();
-               /* Invoke C1 */
                halt();
        } else {
                ACPI_FLUSH_CPU_CACHE();
                /* Invoke C3 */
                inb(cx_address);
                /* Dummy op - must do something useless after P_LVL3 read */
-               t = inl(acpi_fadt.xpm_tmr_blk.address);
+               t = inl(acpi_gbl_FADT.xpm_timer_block.address);
        }
        /* Disable bus ratio bit */
-       local_irq_disable();
-       longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
        longhaul.bits.EnableSoftBusRatio = 0;
-       longhaul.bits.EnableSoftBSEL = 0;
-       longhaul.bits.EnableSoftVID = 0;
        wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
+
+       /* Reduce voltage if necessary */
+       if (can_scale_voltage && longhaul_pos > dest_pos) {
+               longhaul.bits.EnableSoftVID = 1;
+               wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
+               /* Change voltage */
+               if (!cx_address) {
+                       ACPI_FLUSH_CPU_CACHE();
+                       halt();
+               } else {
+                       ACPI_FLUSH_CPU_CACHE();
+                       /* Invoke C3 */
+                       inb(cx_address);
+                       /* Dummy op - must do something useless after P_LVL3
+                        * read */
+                       t = inl(acpi_gbl_FADT.xpm_timer_block.address);
+               }
+               longhaul.bits.EnableSoftVID = 0;
+               wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
+               longhaul_pos = dest_pos;
+       }
 }
 
 /**
@@ -251,39 +290,30 @@ static void longhaul_setstate(unsigned int clock_ratio_index)
                outb(3, 0x22);
        } else if ((pr != NULL) && pr->flags.bm_control) {
                /* Disable bus master arbitration */
-               acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1,
-                                 ACPI_MTX_DO_NOT_LOCK);
+               acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
        }
        switch (longhaul_version) {
 
        /*
         * Longhaul v1. (Samuel[C5A] and Samuel2 stepping 0[C5B])
         * Software controlled multipliers only.
-        *
-        * *NB* Until we get voltage scaling working v1 & v2 are the same code.
-        * Longhaul v2 appears in Samuel2 Steppings 1->7 [C5b] and Ezra [C5C]
         */
        case TYPE_LONGHAUL_V1:
-       case TYPE_LONGHAUL_V2:
                do_longhaul1(clock_ratio_index);
                break;
 
        /*
+        * Longhaul v2 appears in Samuel2 Steppings 1->7 [C5B] and Ezra [C5C]
+        *
         * Longhaul v3 (aka Powersaver). (Ezra-T [C5M] & Nehemiah [C5N])
-        * We can scale voltage with this too, but that's currently
-        * disabled until we come up with a decent 'match freq to voltage'
-        * algorithm.
-        * When we add voltage scaling, we will also need to do the
-        * voltage/freq setting in order depending on the direction
-        * of scaling (like we do in powernow-k7.c)
         * Nehemiah can do FSB scaling too, but this has never been proven
         * to work in practice.
         */
+       case TYPE_LONGHAUL_V2:
        case TYPE_POWERSAVER:
                if (longhaul_flags & USE_ACPI_C3) {
                        /* Don't allow wakeup */
-                       acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0,
-                                         ACPI_MTX_DO_NOT_LOCK);
+                       acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
                        do_powersaver(cx->address, clock_ratio_index);
                } else {
                        do_powersaver(0, clock_ratio_index);
@@ -296,8 +326,7 @@ static void longhaul_setstate(unsigned int clock_ratio_index)
                outb(0, 0x22);
        } else if ((pr != NULL) && pr->flags.bm_control) {
                /* Enable bus master arbitration */
-               acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0,
-                                 ACPI_MTX_DO_NOT_LOCK);
+               acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
        }
        outb(pic2_mask,0xA1);   /* restore mask */
        outb(pic1_mask,0x21);
@@ -305,6 +334,7 @@ static void longhaul_setstate(unsigned int clock_ratio_index)
        local_irq_restore(flags);
        preempt_enable();
 
+       freqs.new = calc_speed(longhaul_get_cpu_mult());
        cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
 }
 
@@ -358,22 +388,15 @@ static int __init longhaul_get_ranges(void)
         * C3 is booting at max anyway. */
        maxmult = mult;
        /* Get min multiplier */
-       switch (longhaul_version) {
-       case TYPE_LONGHAUL_V1:
-       case TYPE_LONGHAUL_V2:
-               minmult = 30;
+       switch (cpu_model) {
+       case CPU_NEHEMIAH:
+               minmult = 50;
                break;
-
-       case TYPE_POWERSAVER:
-               /* Ezra-T */
-               if (cpu_model == CPU_EZRA_T)
-                       minmult = 30;
-               /* Nehemiah */
-               else if (cpu_model == CPU_NEHEMIAH)
-                       minmult = 50;
-               /* Nehemiah C */
-               else if (cpu_model == CPU_NEHEMIAH_C)
-                       minmult = 40;
+       case CPU_NEHEMIAH_C:
+               minmult = 40;
+               break;
+       default:
+               minmult = 30;
                break;
        }
 
@@ -383,7 +406,7 @@ static int __init longhaul_get_ranges(void)
        highest_speed = calc_speed(maxmult);
        lowest_speed = calc_speed(minmult);
        dprintk ("FSB:%dMHz  Lowest speed: %s   Highest speed:%s\n", fsb,
-                print_speed(lowest_speed/1000), 
+                print_speed(lowest_speed/1000),
                 print_speed(highest_speed/1000));
 
        if (lowest_speed == highest_speed) {
@@ -427,6 +450,7 @@ static void __init longhaul_setup_voltagescaling(void)
        union msr_longhaul longhaul;
        struct mV_pos minvid, maxvid;
        unsigned int j, speed, pos, kHz_step, numvscales;
+       int min_vid_speed;
 
        rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
        if (!(longhaul.bits.RevisionID & 1)) {
@@ -440,14 +464,14 @@ static void __init longhaul_setup_voltagescaling(void)
                mV_vrm_table = &mV_vrm85[0];
        } else {
                printk (KERN_INFO PFX "Mobile VRM\n");
+               if (cpu_model < CPU_NEHEMIAH)
+                       return;
                vrm_mV_table = &mobilevrm_mV[0];
                mV_vrm_table = &mV_mobilevrm[0];
        }
 
        minvid = vrm_mV_table[longhaul.bits.MinimumVID];
        maxvid = vrm_mV_table[longhaul.bits.MaximumVID];
-       numvscales = maxvid.pos - minvid.pos + 1;
-       kHz_step = (highest_speed - lowest_speed) / numvscales;
 
        if (minvid.mV == 0 || maxvid.mV == 0 || minvid.mV > maxvid.mV) {
                printk (KERN_INFO PFX "Bogus values Min:%d.%03d Max:%d.%03d. "
@@ -463,20 +487,59 @@ static void __init longhaul_setup_voltagescaling(void)
                return;
        }
 
-       printk(KERN_INFO PFX "Max VID=%d.%03d  Min VID=%d.%03d, %d possible voltage scales\n",
+       /* How many voltage steps */
+       numvscales = maxvid.pos - minvid.pos + 1;
+       printk(KERN_INFO PFX
+               "Max VID=%d.%03d  "
+               "Min VID=%d.%03d, "
+               "%d possible voltage scales\n",
                maxvid.mV/1000, maxvid.mV%1000,
                minvid.mV/1000, minvid.mV%1000,
                numvscales);
-       
+
+       /* Calculate max frequency at min voltage */
+       j = longhaul.bits.MinMHzBR;
+       if (longhaul.bits.MinMHzBR4)
+               j += 16;
+       min_vid_speed = eblcr_table[j];
+       if (min_vid_speed == -1)
+               return;
+       switch (longhaul.bits.MinMHzFSB) {
+       case 0:
+               min_vid_speed *= 13333;
+               break;
+       case 1:
+               min_vid_speed *= 10000;
+               break;
+       case 3:
+               min_vid_speed *= 6666;
+               break;
+       default:
+               return;
+               break;
+       }
+       if (min_vid_speed >= highest_speed)
+               return;
+       /* Calculate kHz for one voltage step */
+       kHz_step = (highest_speed - min_vid_speed) / numvscales;
+
+
        j = 0;
        while (longhaul_table[j].frequency != CPUFREQ_TABLE_END) {
                speed = longhaul_table[j].frequency;
-               pos = (speed - lowest_speed) / kHz_step + minvid.pos;
+               if (speed > min_vid_speed)
+                       pos = (speed - min_vid_speed) / kHz_step + minvid.pos;
+               else
+                       pos = minvid.pos;
                f_msr_table[longhaul_table[j].index].vrm = mV_vrm_table[pos];
+               f_msr_table[longhaul_table[j].index].pos = pos;
                j++;
        }
 
+       longhaul_pos = maxvid.pos;
        can_scale_voltage = 1;
+       printk(KERN_INFO PFX "Voltage scaling enabled. "
+               "Use of \"conservative\" governor is highly recommended.\n");
 }
 
 
@@ -588,6 +651,7 @@ static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
        struct cpuinfo_x86 *c = cpu_data;
        char *cpuname=NULL;
        int ret;
+       u32 lo, hi;
        int vt8235_present;
 
        /* Check what we have on this motherboard */
@@ -601,16 +665,20 @@ static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
                break;
 
        case 7:
-               longhaul_version = TYPE_LONGHAUL_V1;
                switch (c->x86_mask) {
                case 0:
+                       longhaul_version = TYPE_LONGHAUL_V1;
                        cpu_model = CPU_SAMUEL2;
                        cpuname = "C3 'Samuel 2' [C5B]";
-                       /* Note, this is not a typo, early Samuel2's had Samuel1 ratios. */
-                       memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
-                       memcpy (eblcr_table, samuel2_eblcr, sizeof(samuel2_eblcr));
+                       /* Note, this is not a typo, early Samuel2's had
+                        * Samuel1 ratios. */
+                       memcpy(clock_ratio, samuel1_clock_ratio,
+                               sizeof(samuel1_clock_ratio));
+                       memcpy(eblcr_table, samuel2_eblcr,
+                               sizeof(samuel2_eblcr));
                        break;
                case 1 ... 15:
+                       longhaul_version = TYPE_LONGHAUL_V2;
                        if (c->x86_mask < 8) {
                                cpu_model = CPU_SAMUEL2;
                                cpuname = "C3 'Samuel 2' [C5B]";
@@ -618,8 +686,10 @@ static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
                                cpu_model = CPU_EZRA;
                                cpuname = "C3 'Ezra' [C5C]";
                        }
-                       memcpy (clock_ratio, ezra_clock_ratio, sizeof(ezra_clock_ratio));
-                       memcpy (eblcr_table, ezra_eblcr, sizeof(ezra_eblcr));
+                       memcpy(clock_ratio, ezra_clock_ratio,
+                               sizeof(ezra_clock_ratio));
+                       memcpy(eblcr_table, ezra_eblcr,
+                               sizeof(ezra_eblcr));
                        break;
                }
                break;
@@ -643,15 +713,15 @@ static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
                switch (c->x86_mask) {
                case 0 ... 1:
                        cpu_model = CPU_NEHEMIAH;
-                       cpuname = "C3 'Nehemiah A' [C5N]";
+                       cpuname = "C3 'Nehemiah A' [C5XLOE]";
                        break;
                case 2 ... 4:
                        cpu_model = CPU_NEHEMIAH;
-                       cpuname = "C3 'Nehemiah B' [C5N]";
+                       cpuname = "C3 'Nehemiah B' [C5XLOH]";
                        break;
                case 5 ... 15:
                        cpu_model = CPU_NEHEMIAH_C;
-                       cpuname = "C3 'Nehemiah C' [C5N]";
+                       cpuname = "C3 'Nehemiah C' [C5P]";
                        break;
                }
                break;
@@ -660,6 +730,13 @@ static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
                cpuname = "Unknown";
                break;
        }
+       /* Check Longhaul ver. 2 */
+       if (longhaul_version == TYPE_LONGHAUL_V2) {
+               rdmsr(MSR_VIA_LONGHAUL, lo, hi);
+               if (lo == 0 && hi == 0)
+                       /* Looks like MSR isn't present */
+                       longhaul_version = TYPE_LONGHAUL_V1;
+       }
 
        printk (KERN_INFO PFX "VIA %s CPU detected.  ", cpuname);
        switch (longhaul_version) {
@@ -681,7 +758,7 @@ static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
                                NULL, (void *)&pr);
 
        /* Check ACPI support for C3 state */
-       if (pr != NULL && longhaul_version == TYPE_POWERSAVER) {
+       if (pr != NULL && longhaul_version != TYPE_LONGHAUL_V1) {
                cx = &pr->power.states[ACPI_STATE_C3];
                if (cx->address > 0 && cx->latency <= 1000) {
                        longhaul_flags |= USE_ACPI_C3;