Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2001-2004 Dave Jones. <davej@codemonkey.org.uk> |
| 3 | * (C) 2002 Padraig Brady. <padraig@antefacto.com> |
| 4 | * |
| 5 | * Licensed under the terms of the GNU GPL License version 2. |
| 6 | * Based upon datasheets & sample CPUs kindly provided by VIA. |
| 7 | * |
| 8 | * VIA have currently 3 different versions of Longhaul. |
| 9 | * Version 1 (Longhaul) uses the BCR2 MSR at 0x1147. |
| 10 | * It is present only in Samuel 1 (C5A), Samuel 2 (C5B) stepping 0. |
| 11 | * Version 2 of longhaul is the same as v1, but adds voltage scaling. |
| 12 | * Present in Samuel 2 (steppings 1-7 only) (C5B), and Ezra (C5C) |
| 13 | * voltage scaling support has currently been disabled in this driver |
| 14 | * until we have code that gets it right. |
| 15 | * Version 3 of longhaul got renamed to Powersaver and redesigned |
| 16 | * to use the POWERSAVER MSR at 0x110a. |
| 17 | * It is present in Ezra-T (C5M), Nehemiah (C5X) and above. |
| 18 | * It's pretty much the same feature wise to longhaul v2, though |
| 19 | * there is provision for scaling FSB too, but this doesn't work |
| 20 | * too well in practice so we don't even try to use this. |
| 21 | * |
| 22 | * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous* |
| 23 | */ |
| 24 | |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/moduleparam.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/cpufreq.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/string.h> |
| 32 | |
| 33 | #include <asm/msr.h> |
| 34 | #include <asm/timex.h> |
| 35 | #include <asm/io.h> |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 36 | #include <asm/acpi.h> |
| 37 | #include <linux/acpi.h> |
| 38 | #include <acpi/processor.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #include "longhaul.h" |
| 41 | |
| 42 | #define PFX "longhaul: " |
| 43 | |
| 44 | #define TYPE_LONGHAUL_V1 1 |
| 45 | #define TYPE_LONGHAUL_V2 2 |
| 46 | #define TYPE_POWERSAVER 3 |
| 47 | |
| 48 | #define CPU_SAMUEL 1 |
| 49 | #define CPU_SAMUEL2 2 |
| 50 | #define CPU_EZRA 3 |
| 51 | #define CPU_EZRA_T 4 |
| 52 | #define CPU_NEHEMIAH 5 |
| 53 | |
| 54 | static int cpu_model; |
| 55 | static unsigned int numscales=16, numvscales; |
| 56 | static unsigned int fsb; |
| 57 | static int minvid, maxvid; |
| 58 | static unsigned int minmult, maxmult; |
| 59 | static int can_scale_voltage; |
| 60 | static int vrmrev; |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 61 | static struct acpi_processor *pr = NULL; |
| 62 | static struct acpi_processor_cx *cx = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | /* Module parameters */ |
| 65 | static int dont_scale_voltage; |
| 66 | |
| 67 | |
| 68 | #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "longhaul", msg) |
| 69 | |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | /* Clock ratios multiplied by 10 */ |
| 72 | static int clock_ratio[32]; |
| 73 | static int eblcr_table[32]; |
| 74 | static int voltage_table[32]; |
| 75 | static unsigned int highest_speed, lowest_speed; /* kHz */ |
| 76 | static int longhaul_version; |
| 77 | static struct cpufreq_frequency_table *longhaul_table; |
| 78 | |
| 79 | #ifdef CONFIG_CPU_FREQ_DEBUG |
| 80 | static char speedbuffer[8]; |
| 81 | |
| 82 | static char *print_speed(int speed) |
| 83 | { |
Dave Jones | e2aa873 | 2006-05-30 17:37:15 -0400 | [diff] [blame] | 84 | if (speed < 1000) { |
| 85 | snprintf(speedbuffer, sizeof(speedbuffer),"%dMHz", speed); |
| 86 | return speedbuffer; |
| 87 | } |
| 88 | |
| 89 | if (speed%1000 == 0) |
| 90 | snprintf(speedbuffer, sizeof(speedbuffer), |
| 91 | "%dGHz", speed/1000); |
| 92 | else |
| 93 | snprintf(speedbuffer, sizeof(speedbuffer), |
| 94 | "%d.%dGHz", speed/1000, (speed%1000)/100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | return speedbuffer; |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | |
| 101 | static unsigned int calc_speed(int mult) |
| 102 | { |
| 103 | int khz; |
| 104 | khz = (mult/10)*fsb; |
| 105 | if (mult%10) |
| 106 | khz += fsb/2; |
| 107 | khz *= 1000; |
| 108 | return khz; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | static int longhaul_get_cpu_mult(void) |
| 113 | { |
| 114 | unsigned long invalue=0,lo, hi; |
| 115 | |
| 116 | rdmsr (MSR_IA32_EBL_CR_POWERON, lo, hi); |
| 117 | invalue = (lo & (1<<22|1<<23|1<<24|1<<25)) >>22; |
| 118 | if (longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) { |
| 119 | if (lo & (1<<27)) |
| 120 | invalue+=16; |
| 121 | } |
| 122 | return eblcr_table[invalue]; |
| 123 | } |
| 124 | |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 125 | /* For processor with BCR2 MSR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 127 | static void do_longhaul1(int cx_address, unsigned int clock_ratio_index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 129 | union msr_bcr2 bcr2; |
| 130 | u32 t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 132 | rdmsrl(MSR_VIA_BCR2, bcr2.val); |
| 133 | /* Enable software clock multiplier */ |
| 134 | bcr2.bits.ESOFTBF = 1; |
| 135 | bcr2.bits.CLOCKMUL = clock_ratio_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 137 | /* Sync to timer tick */ |
Zachary Amsden | 4bb0d3e | 2005-09-03 15:56:36 -0700 | [diff] [blame] | 138 | safe_halt(); |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 139 | ACPI_FLUSH_CPU_CACHE(); |
| 140 | /* Change frequency on next halt or sleep */ |
| 141 | wrmsrl(MSR_VIA_BCR2, bcr2.val); |
| 142 | /* Invoke C3 */ |
| 143 | inb(cx_address); |
| 144 | /* Dummy op - must do something useless after P_LVL3 read */ |
| 145 | t = inl(acpi_fadt.xpm_tmr_blk.address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 147 | /* Disable software clock multiplier */ |
Dave Jones | 3be6a48 | 2005-05-31 19:03:51 -0700 | [diff] [blame] | 148 | local_irq_disable(); |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 149 | rdmsrl(MSR_VIA_BCR2, bcr2.val); |
| 150 | bcr2.bits.ESOFTBF = 0; |
| 151 | wrmsrl(MSR_VIA_BCR2, bcr2.val); |
| 152 | } |
Dave Jones | 3be6a48 | 2005-05-31 19:03:51 -0700 | [diff] [blame] | 153 | |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 154 | /* For processor with Longhaul MSR */ |
Dave Jones | 1174631 | 2005-05-31 19:03:51 -0700 | [diff] [blame] | 155 | |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 156 | static void do_powersaver(int cx_address, unsigned int clock_ratio_index) |
| 157 | { |
| 158 | union msr_longhaul longhaul; |
| 159 | u32 t; |
Dave Jones | 3be6a48 | 2005-05-31 19:03:51 -0700 | [diff] [blame] | 160 | |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 161 | rdmsrl(MSR_VIA_LONGHAUL, longhaul.val); |
| 162 | longhaul.bits.RevisionKey = longhaul.bits.RevisionID; |
| 163 | longhaul.bits.SoftBusRatio = clock_ratio_index & 0xf; |
| 164 | longhaul.bits.SoftBusRatio4 = (clock_ratio_index & 0x10) >> 4; |
Rafa³ Bilski | eb23c75 | 2006-07-09 21:47:04 +0200 | [diff] [blame^] | 165 | longhaul.bits.EnableSoftBusRatio = 1; |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 166 | |
| 167 | /* Sync to timer tick */ |
| 168 | safe_halt(); |
| 169 | ACPI_FLUSH_CPU_CACHE(); |
| 170 | /* Change frequency on next halt or sleep */ |
| 171 | wrmsrl(MSR_VIA_LONGHAUL, longhaul.val); |
| 172 | /* Invoke C3 */ |
| 173 | inb(cx_address); |
| 174 | /* Dummy op - must do something useless after P_LVL3 read */ |
| 175 | t = inl(acpi_fadt.xpm_tmr_blk.address); |
| 176 | |
| 177 | /* Disable bus ratio bit */ |
| 178 | local_irq_disable(); |
| 179 | longhaul.bits.RevisionKey = longhaul.bits.RevisionID; |
| 180 | longhaul.bits.EnableSoftBusRatio = 0; |
| 181 | longhaul.bits.EnableSoftBSEL = 0; |
| 182 | longhaul.bits.EnableSoftVID = 0; |
| 183 | wrmsrl(MSR_VIA_LONGHAUL, longhaul.val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
| 187 | * longhaul_set_cpu_frequency() |
| 188 | * @clock_ratio_index : bitpattern of the new multiplier. |
| 189 | * |
| 190 | * Sets a new clock ratio. |
| 191 | */ |
| 192 | |
| 193 | static void longhaul_setstate(unsigned int clock_ratio_index) |
| 194 | { |
| 195 | int speed, mult; |
| 196 | struct cpufreq_freqs freqs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | static unsigned int old_ratio=-1; |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 198 | unsigned long flags; |
| 199 | unsigned int pic1_mask, pic2_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | if (old_ratio == clock_ratio_index) |
| 202 | return; |
| 203 | old_ratio = clock_ratio_index; |
| 204 | |
| 205 | mult = clock_ratio[clock_ratio_index]; |
| 206 | if (mult == -1) |
| 207 | return; |
| 208 | |
| 209 | speed = calc_speed(mult); |
| 210 | if ((speed > highest_speed) || (speed < lowest_speed)) |
| 211 | return; |
| 212 | |
| 213 | freqs.old = calc_speed(longhaul_get_cpu_mult()); |
| 214 | freqs.new = speed; |
| 215 | freqs.cpu = 0; /* longhaul.c is UP only driver */ |
| 216 | |
| 217 | cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); |
| 218 | |
| 219 | dprintk ("Setting to FSB:%dMHz Mult:%d.%dx (%s)\n", |
| 220 | fsb, mult/10, mult%10, print_speed(speed/1000)); |
| 221 | |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 222 | preempt_disable(); |
| 223 | local_irq_save(flags); |
| 224 | |
| 225 | pic2_mask = inb(0xA1); |
| 226 | pic1_mask = inb(0x21); /* works on C3. save mask. */ |
| 227 | outb(0xFF,0xA1); /* Overkill */ |
| 228 | outb(0xFE,0x21); /* TMR0 only */ |
| 229 | |
| 230 | /* Disable bus master arbitration */ |
| 231 | if (pr->flags.bm_check) { |
| 232 | acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1, |
| 233 | ACPI_MTX_DO_NOT_LOCK); |
| 234 | } |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | switch (longhaul_version) { |
| 237 | |
| 238 | /* |
| 239 | * Longhaul v1. (Samuel[C5A] and Samuel2 stepping 0[C5B]) |
| 240 | * Software controlled multipliers only. |
| 241 | * |
| 242 | * *NB* Until we get voltage scaling working v1 & v2 are the same code. |
| 243 | * Longhaul v2 appears in Samuel2 Steppings 1->7 [C5b] and Ezra [C5C] |
| 244 | */ |
| 245 | case TYPE_LONGHAUL_V1: |
| 246 | case TYPE_LONGHAUL_V2: |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 247 | do_longhaul1(cx->address, clock_ratio_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | break; |
| 249 | |
| 250 | /* |
| 251 | * Longhaul v3 (aka Powersaver). (Ezra-T [C5M] & Nehemiah [C5N]) |
| 252 | * We can scale voltage with this too, but that's currently |
| 253 | * disabled until we come up with a decent 'match freq to voltage' |
| 254 | * algorithm. |
| 255 | * When we add voltage scaling, we will also need to do the |
| 256 | * voltage/freq setting in order depending on the direction |
| 257 | * of scaling (like we do in powernow-k7.c) |
| 258 | * Nehemiah can do FSB scaling too, but this has never been proven |
| 259 | * to work in practice. |
| 260 | */ |
| 261 | case TYPE_POWERSAVER: |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 262 | do_powersaver(cx->address, clock_ratio_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 266 | /* Enable bus master arbitration */ |
| 267 | if (pr->flags.bm_check) { |
| 268 | acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0, |
| 269 | ACPI_MTX_DO_NOT_LOCK); |
| 270 | } |
| 271 | |
| 272 | outb(pic2_mask,0xA1); /* restore mask */ |
| 273 | outb(pic1_mask,0x21); |
| 274 | |
| 275 | local_irq_restore(flags); |
| 276 | preempt_enable(); |
| 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Centaur decided to make life a little more tricky. |
| 283 | * Only longhaul v1 is allowed to read EBLCR BSEL[0:1]. |
| 284 | * Samuel2 and above have to try and guess what the FSB is. |
| 285 | * We do this by assuming we booted at maximum multiplier, and interpolate |
| 286 | * between that value multiplied by possible FSBs and cpu_mhz which |
| 287 | * was calculated at boot time. Really ugly, but no other way to do this. |
| 288 | */ |
| 289 | |
| 290 | #define ROUNDING 0xf |
| 291 | |
| 292 | static int _guess(int guess) |
| 293 | { |
| 294 | int target; |
| 295 | |
| 296 | target = ((maxmult/10)*guess); |
| 297 | if (maxmult%10 != 0) |
| 298 | target += (guess/2); |
| 299 | target += ROUNDING/2; |
| 300 | target &= ~ROUNDING; |
| 301 | return target; |
| 302 | } |
| 303 | |
| 304 | |
| 305 | static int guess_fsb(void) |
| 306 | { |
| 307 | int speed = (cpu_khz/1000); |
| 308 | int i; |
| 309 | int speeds[3] = { 66, 100, 133 }; |
| 310 | |
| 311 | speed += ROUNDING/2; |
| 312 | speed &= ~ROUNDING; |
| 313 | |
| 314 | for (i=0; i<3; i++) { |
| 315 | if (_guess(speeds[i]) == speed) |
| 316 | return speeds[i]; |
| 317 | } |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | static int __init longhaul_get_ranges(void) |
| 323 | { |
| 324 | unsigned long invalue; |
| 325 | unsigned int multipliers[32]= { |
| 326 | 50,30,40,100,55,35,45,95,90,70,80,60,120,75,85,65, |
| 327 | -1,110,120,-1,135,115,125,105,130,150,160,140,-1,155,-1,145 }; |
| 328 | unsigned int j, k = 0; |
| 329 | union msr_longhaul longhaul; |
| 330 | unsigned long lo, hi; |
| 331 | unsigned int eblcr_fsb_table_v1[] = { 66, 133, 100, -1 }; |
| 332 | unsigned int eblcr_fsb_table_v2[] = { 133, 100, -1, 66 }; |
| 333 | |
| 334 | switch (longhaul_version) { |
| 335 | case TYPE_LONGHAUL_V1: |
| 336 | case TYPE_LONGHAUL_V2: |
| 337 | /* Ugh, Longhaul v1 didn't have the min/max MSRs. |
| 338 | Assume min=3.0x & max = whatever we booted at. */ |
| 339 | minmult = 30; |
| 340 | maxmult = longhaul_get_cpu_mult(); |
| 341 | rdmsr (MSR_IA32_EBL_CR_POWERON, lo, hi); |
| 342 | invalue = (lo & (1<<18|1<<19)) >>18; |
| 343 | if (cpu_model==CPU_SAMUEL || cpu_model==CPU_SAMUEL2) |
| 344 | fsb = eblcr_fsb_table_v1[invalue]; |
| 345 | else |
| 346 | fsb = guess_fsb(); |
| 347 | break; |
| 348 | |
| 349 | case TYPE_POWERSAVER: |
| 350 | /* Ezra-T */ |
| 351 | if (cpu_model==CPU_EZRA_T) { |
| 352 | rdmsrl (MSR_VIA_LONGHAUL, longhaul.val); |
| 353 | invalue = longhaul.bits.MaxMHzBR; |
| 354 | if (longhaul.bits.MaxMHzBR4) |
| 355 | invalue += 16; |
| 356 | maxmult=multipliers[invalue]; |
| 357 | |
| 358 | invalue = longhaul.bits.MinMHzBR; |
| 359 | if (longhaul.bits.MinMHzBR4 == 1) |
| 360 | minmult = 30; |
| 361 | else |
| 362 | minmult = multipliers[invalue]; |
| 363 | fsb = eblcr_fsb_table_v2[longhaul.bits.MaxMHzFSB]; |
| 364 | break; |
| 365 | } |
| 366 | |
| 367 | /* Nehemiah */ |
| 368 | if (cpu_model==CPU_NEHEMIAH) { |
| 369 | rdmsrl (MSR_VIA_LONGHAUL, longhaul.val); |
| 370 | |
| 371 | /* |
| 372 | * TODO: This code works, but raises a lot of questions. |
| 373 | * - Some Nehemiah's seem to have broken Min/MaxMHzBR's. |
| 374 | * We get around this by using a hardcoded multiplier of 4.0x |
| 375 | * for the minimimum speed, and the speed we booted up at for the max. |
| 376 | * This is done in longhaul_get_cpu_mult() by reading the EBLCR register. |
| 377 | * - According to some VIA documentation EBLCR is only |
| 378 | * in pre-Nehemiah C3s. How this still works is a mystery. |
| 379 | * We're possibly using something undocumented and unsupported, |
| 380 | * But it works, so we don't grumble. |
| 381 | */ |
| 382 | minmult=40; |
| 383 | maxmult=longhaul_get_cpu_mult(); |
| 384 | |
| 385 | /* Starting with the 1.2GHz parts, theres a 200MHz bus. */ |
| 386 | if ((cpu_khz/1000) > 1200) |
| 387 | fsb = 200; |
| 388 | else |
| 389 | fsb = eblcr_fsb_table_v2[longhaul.bits.MaxMHzFSB]; |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | dprintk ("MinMult:%d.%dx MaxMult:%d.%dx\n", |
| 395 | minmult/10, minmult%10, maxmult/10, maxmult%10); |
| 396 | |
| 397 | if (fsb == -1) { |
| 398 | printk (KERN_INFO PFX "Invalid (reserved) FSB!\n"); |
| 399 | return -EINVAL; |
| 400 | } |
| 401 | |
| 402 | highest_speed = calc_speed(maxmult); |
| 403 | lowest_speed = calc_speed(minmult); |
| 404 | dprintk ("FSB:%dMHz Lowest speed: %s Highest speed:%s\n", fsb, |
| 405 | print_speed(lowest_speed/1000), |
| 406 | print_speed(highest_speed/1000)); |
| 407 | |
| 408 | if (lowest_speed == highest_speed) { |
| 409 | printk (KERN_INFO PFX "highestspeed == lowest, aborting.\n"); |
| 410 | return -EINVAL; |
| 411 | } |
| 412 | if (lowest_speed > highest_speed) { |
| 413 | printk (KERN_INFO PFX "nonsense! lowest (%d > %d) !\n", |
| 414 | lowest_speed, highest_speed); |
| 415 | return -EINVAL; |
| 416 | } |
| 417 | |
| 418 | longhaul_table = kmalloc((numscales + 1) * sizeof(struct cpufreq_frequency_table), GFP_KERNEL); |
| 419 | if(!longhaul_table) |
| 420 | return -ENOMEM; |
| 421 | |
| 422 | for (j=0; j < numscales; j++) { |
| 423 | unsigned int ratio; |
| 424 | ratio = clock_ratio[j]; |
| 425 | if (ratio == -1) |
| 426 | continue; |
| 427 | if (ratio > maxmult || ratio < minmult) |
| 428 | continue; |
| 429 | longhaul_table[k].frequency = calc_speed(ratio); |
| 430 | longhaul_table[k].index = j; |
| 431 | k++; |
| 432 | } |
| 433 | |
| 434 | longhaul_table[k].frequency = CPUFREQ_TABLE_END; |
| 435 | if (!k) { |
| 436 | kfree (longhaul_table); |
| 437 | return -EINVAL; |
| 438 | } |
| 439 | |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | |
| 444 | static void __init longhaul_setup_voltagescaling(void) |
| 445 | { |
| 446 | union msr_longhaul longhaul; |
| 447 | |
| 448 | rdmsrl (MSR_VIA_LONGHAUL, longhaul.val); |
| 449 | |
| 450 | if (!(longhaul.bits.RevisionID & 1)) |
| 451 | return; |
| 452 | |
| 453 | minvid = longhaul.bits.MinimumVID; |
| 454 | maxvid = longhaul.bits.MaximumVID; |
| 455 | vrmrev = longhaul.bits.VRMRev; |
| 456 | |
| 457 | if (minvid == 0 || maxvid == 0) { |
| 458 | printk (KERN_INFO PFX "Bogus values Min:%d.%03d Max:%d.%03d. " |
| 459 | "Voltage scaling disabled.\n", |
| 460 | minvid/1000, minvid%1000, maxvid/1000, maxvid%1000); |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | if (minvid == maxvid) { |
| 465 | printk (KERN_INFO PFX "Claims to support voltage scaling but min & max are " |
| 466 | "both %d.%03d. Voltage scaling disabled\n", |
| 467 | maxvid/1000, maxvid%1000); |
| 468 | return; |
| 469 | } |
| 470 | |
| 471 | if (vrmrev==0) { |
Dave Jones | 52c18fd | 2005-09-01 11:01:02 -0700 | [diff] [blame] | 472 | dprintk ("VRM 8.5\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | memcpy (voltage_table, vrm85scales, sizeof(voltage_table)); |
| 474 | numvscales = (voltage_table[maxvid]-voltage_table[minvid])/25; |
| 475 | } else { |
Dave Jones | 52c18fd | 2005-09-01 11:01:02 -0700 | [diff] [blame] | 476 | dprintk ("Mobile VRM\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | memcpy (voltage_table, mobilevrmscales, sizeof(voltage_table)); |
| 478 | numvscales = (voltage_table[maxvid]-voltage_table[minvid])/5; |
| 479 | } |
| 480 | |
| 481 | /* Current voltage isn't readable at first, so we need to |
| 482 | set it to a known value. The spec says to use maxvid */ |
| 483 | longhaul.bits.RevisionKey = longhaul.bits.RevisionID; /* FIXME: This is bad. */ |
| 484 | longhaul.bits.EnableSoftVID = 1; |
| 485 | longhaul.bits.SoftVID = maxvid; |
| 486 | wrmsrl (MSR_VIA_LONGHAUL, longhaul.val); |
| 487 | |
| 488 | minvid = voltage_table[minvid]; |
| 489 | maxvid = voltage_table[maxvid]; |
| 490 | |
| 491 | dprintk ("Min VID=%d.%03d Max VID=%d.%03d, %d possible voltage scales\n", |
| 492 | maxvid/1000, maxvid%1000, minvid/1000, minvid%1000, numvscales); |
| 493 | |
| 494 | can_scale_voltage = 1; |
| 495 | } |
| 496 | |
| 497 | |
| 498 | static int longhaul_verify(struct cpufreq_policy *policy) |
| 499 | { |
| 500 | return cpufreq_frequency_table_verify(policy, longhaul_table); |
| 501 | } |
| 502 | |
| 503 | |
| 504 | static int longhaul_target(struct cpufreq_policy *policy, |
| 505 | unsigned int target_freq, unsigned int relation) |
| 506 | { |
| 507 | unsigned int table_index = 0; |
| 508 | unsigned int new_clock_ratio = 0; |
| 509 | |
| 510 | if (cpufreq_frequency_table_target(policy, longhaul_table, target_freq, relation, &table_index)) |
| 511 | return -EINVAL; |
| 512 | |
| 513 | new_clock_ratio = longhaul_table[table_index].index & 0xFF; |
| 514 | |
| 515 | longhaul_setstate(new_clock_ratio); |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | |
| 521 | static unsigned int longhaul_get(unsigned int cpu) |
| 522 | { |
| 523 | if (cpu) |
| 524 | return 0; |
| 525 | return calc_speed(longhaul_get_cpu_mult()); |
| 526 | } |
| 527 | |
Adrian Bunk | c4a96c1 | 2006-07-09 19:53:08 +0200 | [diff] [blame] | 528 | static acpi_status longhaul_walk_callback(acpi_handle obj_handle, |
| 529 | u32 nesting_level, |
| 530 | void *context, void **return_value) |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 531 | { |
| 532 | struct acpi_device *d; |
| 533 | |
| 534 | if ( acpi_bus_get_device(obj_handle, &d) ) { |
| 535 | return 0; |
| 536 | } |
| 537 | *return_value = (void *)acpi_driver_data(d); |
| 538 | return 1; |
| 539 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
| 541 | static int __init longhaul_cpu_init(struct cpufreq_policy *policy) |
| 542 | { |
| 543 | struct cpuinfo_x86 *c = cpu_data; |
| 544 | char *cpuname=NULL; |
| 545 | int ret; |
| 546 | |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 547 | /* Check ACPI support for C3 state */ |
| 548 | acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX, |
| 549 | &longhaul_walk_callback, NULL, (void *)&pr); |
| 550 | if (pr == NULL) goto err_acpi; |
| 551 | |
| 552 | cx = &pr->power.states[ACPI_STATE_C3]; |
| 553 | if (cx == NULL || cx->latency > 1000) goto err_acpi; |
| 554 | |
| 555 | /* Now check what we have on this motherboard */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | switch (c->x86_model) { |
| 557 | case 6: |
| 558 | cpu_model = CPU_SAMUEL; |
| 559 | cpuname = "C3 'Samuel' [C5A]"; |
| 560 | longhaul_version = TYPE_LONGHAUL_V1; |
| 561 | memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio)); |
| 562 | memcpy (eblcr_table, samuel1_eblcr, sizeof(samuel1_eblcr)); |
| 563 | break; |
| 564 | |
| 565 | case 7: |
| 566 | longhaul_version = TYPE_LONGHAUL_V1; |
| 567 | switch (c->x86_mask) { |
| 568 | case 0: |
| 569 | cpu_model = CPU_SAMUEL2; |
| 570 | cpuname = "C3 'Samuel 2' [C5B]"; |
| 571 | /* Note, this is not a typo, early Samuel2's had Samuel1 ratios. */ |
| 572 | memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio)); |
| 573 | memcpy (eblcr_table, samuel2_eblcr, sizeof(samuel2_eblcr)); |
| 574 | break; |
| 575 | case 1 ... 15: |
| 576 | if (c->x86_mask < 8) { |
| 577 | cpu_model = CPU_SAMUEL2; |
| 578 | cpuname = "C3 'Samuel 2' [C5B]"; |
| 579 | } else { |
| 580 | cpu_model = CPU_EZRA; |
| 581 | cpuname = "C3 'Ezra' [C5C]"; |
| 582 | } |
| 583 | memcpy (clock_ratio, ezra_clock_ratio, sizeof(ezra_clock_ratio)); |
| 584 | memcpy (eblcr_table, ezra_eblcr, sizeof(ezra_eblcr)); |
| 585 | break; |
| 586 | } |
| 587 | break; |
| 588 | |
| 589 | case 8: |
| 590 | cpu_model = CPU_EZRA_T; |
| 591 | cpuname = "C3 'Ezra-T' [C5M]"; |
| 592 | longhaul_version = TYPE_POWERSAVER; |
| 593 | numscales=32; |
| 594 | memcpy (clock_ratio, ezrat_clock_ratio, sizeof(ezrat_clock_ratio)); |
| 595 | memcpy (eblcr_table, ezrat_eblcr, sizeof(ezrat_eblcr)); |
| 596 | break; |
| 597 | |
| 598 | case 9: |
| 599 | cpu_model = CPU_NEHEMIAH; |
| 600 | longhaul_version = TYPE_POWERSAVER; |
| 601 | numscales=32; |
| 602 | switch (c->x86_mask) { |
| 603 | case 0 ... 1: |
| 604 | cpuname = "C3 'Nehemiah A' [C5N]"; |
| 605 | memcpy (clock_ratio, nehemiah_a_clock_ratio, sizeof(nehemiah_a_clock_ratio)); |
| 606 | memcpy (eblcr_table, nehemiah_a_eblcr, sizeof(nehemiah_a_eblcr)); |
| 607 | break; |
| 608 | case 2 ... 4: |
| 609 | cpuname = "C3 'Nehemiah B' [C5N]"; |
| 610 | memcpy (clock_ratio, nehemiah_b_clock_ratio, sizeof(nehemiah_b_clock_ratio)); |
| 611 | memcpy (eblcr_table, nehemiah_b_eblcr, sizeof(nehemiah_b_eblcr)); |
| 612 | break; |
| 613 | case 5 ... 15: |
| 614 | cpuname = "C3 'Nehemiah C' [C5N]"; |
| 615 | memcpy (clock_ratio, nehemiah_c_clock_ratio, sizeof(nehemiah_c_clock_ratio)); |
| 616 | memcpy (eblcr_table, nehemiah_c_eblcr, sizeof(nehemiah_c_eblcr)); |
| 617 | break; |
| 618 | } |
| 619 | break; |
| 620 | |
| 621 | default: |
| 622 | cpuname = "Unknown"; |
| 623 | break; |
| 624 | } |
| 625 | |
| 626 | printk (KERN_INFO PFX "VIA %s CPU detected. ", cpuname); |
| 627 | switch (longhaul_version) { |
| 628 | case TYPE_LONGHAUL_V1: |
| 629 | case TYPE_LONGHAUL_V2: |
| 630 | printk ("Longhaul v%d supported.\n", longhaul_version); |
| 631 | break; |
| 632 | case TYPE_POWERSAVER: |
| 633 | printk ("Powersaver supported.\n"); |
| 634 | break; |
| 635 | }; |
| 636 | |
| 637 | ret = longhaul_get_ranges(); |
| 638 | if (ret != 0) |
| 639 | return ret; |
| 640 | |
| 641 | if ((longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) && |
| 642 | (dont_scale_voltage==0)) |
| 643 | longhaul_setup_voltagescaling(); |
| 644 | |
| 645 | policy->governor = CPUFREQ_DEFAULT_GOVERNOR; |
Dave Jones | 6778bae | 2005-05-31 19:03:51 -0700 | [diff] [blame] | 646 | policy->cpuinfo.transition_latency = 200000; /* nsec */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | policy->cur = calc_speed(longhaul_get_cpu_mult()); |
| 648 | |
| 649 | ret = cpufreq_frequency_table_cpuinfo(policy, longhaul_table); |
| 650 | if (ret) |
| 651 | return ret; |
| 652 | |
| 653 | cpufreq_frequency_table_get_attr(longhaul_table, policy->cpu); |
| 654 | |
| 655 | return 0; |
Rafa³ Bilski | dadb49d | 2006-07-03 07:19:05 +0200 | [diff] [blame] | 656 | |
| 657 | err_acpi: |
| 658 | printk(KERN_ERR PFX "No ACPI support for CPU frequency changes.\n"); |
| 659 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static int __devexit longhaul_cpu_exit(struct cpufreq_policy *policy) |
| 663 | { |
| 664 | cpufreq_frequency_table_put_attr(policy->cpu); |
| 665 | return 0; |
| 666 | } |
| 667 | |
| 668 | static struct freq_attr* longhaul_attr[] = { |
| 669 | &cpufreq_freq_attr_scaling_available_freqs, |
| 670 | NULL, |
| 671 | }; |
| 672 | |
| 673 | static struct cpufreq_driver longhaul_driver = { |
| 674 | .verify = longhaul_verify, |
| 675 | .target = longhaul_target, |
| 676 | .get = longhaul_get, |
| 677 | .init = longhaul_cpu_init, |
| 678 | .exit = __devexit_p(longhaul_cpu_exit), |
| 679 | .name = "longhaul", |
| 680 | .owner = THIS_MODULE, |
| 681 | .attr = longhaul_attr, |
| 682 | }; |
| 683 | |
| 684 | |
| 685 | static int __init longhaul_init(void) |
| 686 | { |
| 687 | struct cpuinfo_x86 *c = cpu_data; |
| 688 | |
| 689 | if (c->x86_vendor != X86_VENDOR_CENTAUR || c->x86 != 6) |
| 690 | return -ENODEV; |
| 691 | |
Rafa³ Bilski | 48b7bde | 2006-07-04 17:50:57 +0200 | [diff] [blame] | 692 | #ifdef CONFIG_SMP |
| 693 | if (num_online_cpus() > 1) { |
| 694 | return -ENODEV; |
| 695 | printk(KERN_ERR PFX "More than 1 CPU detected, longhaul disabled.\n"); |
| 696 | } |
| 697 | #endif |
| 698 | #ifdef CONFIG_X86_IO_APIC |
| 699 | if (cpu_has_apic) { |
| 700 | printk(KERN_ERR PFX "APIC detected. Longhaul is currently broken in this configuration.\n"); |
| 701 | return -ENODEV; |
| 702 | } |
| 703 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | switch (c->x86_model) { |
| 705 | case 6 ... 9: |
| 706 | return cpufreq_register_driver(&longhaul_driver); |
| 707 | default: |
| 708 | printk (KERN_INFO PFX "Unknown VIA CPU. Contact davej@codemonkey.org.uk\n"); |
| 709 | } |
| 710 | |
| 711 | return -ENODEV; |
| 712 | } |
| 713 | |
| 714 | |
| 715 | static void __exit longhaul_exit(void) |
| 716 | { |
Dave Jones | 8eebf1a | 2006-05-30 17:40:16 -0400 | [diff] [blame] | 717 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | |
| 719 | for (i=0; i < numscales; i++) { |
| 720 | if (clock_ratio[i] == maxmult) { |
| 721 | longhaul_setstate(i); |
| 722 | break; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | cpufreq_unregister_driver(&longhaul_driver); |
| 727 | kfree(longhaul_table); |
| 728 | } |
| 729 | |
| 730 | module_param (dont_scale_voltage, int, 0644); |
| 731 | MODULE_PARM_DESC(dont_scale_voltage, "Don't scale voltage of processor"); |
| 732 | |
| 733 | MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>"); |
| 734 | MODULE_DESCRIPTION ("Longhaul driver for VIA Cyrix processors."); |
| 735 | MODULE_LICENSE ("GPL"); |
| 736 | |
Rafa³ Bilski | 0d6daba | 2006-07-07 08:48:26 +0200 | [diff] [blame] | 737 | late_initcall(longhaul_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | module_exit(longhaul_exit); |
| 739 | |