longhaul.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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 backward compatible with v1, but adds
  12. * LONGHAUL MSR for purpose of both frequency and voltage scaling.
  13. * Present in Samuel 2 (steppings 1-7 only) (C5B), and Ezra (C5C).
  14. * Version 3 of longhaul got renamed to Powersaver and redesigned
  15. * to use only the POWERSAVER MSR at 0x110a.
  16. * It is present in Ezra-T (C5M), Nehemiah (C5X) and above.
  17. * It's pretty much the same feature wise to longhaul v2, though
  18. * there is provision for scaling FSB too, but this doesn't work
  19. * too well in practice so we don't even try to use this.
  20. *
  21. * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/init.h>
  27. #include <linux/cpufreq.h>
  28. #include <linux/pci.h>
  29. #include <linux/slab.h>
  30. #include <linux/string.h>
  31. #include <linux/delay.h>
  32. #include <asm/msr.h>
  33. #include <asm/timex.h>
  34. #include <asm/io.h>
  35. #include <asm/acpi.h>
  36. #include <linux/acpi.h>
  37. #include <acpi/processor.h>
  38. #include "longhaul.h"
  39. #define PFX "longhaul: "
  40. #define TYPE_LONGHAUL_V1 1
  41. #define TYPE_LONGHAUL_V2 2
  42. #define TYPE_POWERSAVER 3
  43. #define CPU_SAMUEL 1
  44. #define CPU_SAMUEL2 2
  45. #define CPU_EZRA 3
  46. #define CPU_EZRA_T 4
  47. #define CPU_NEHEMIAH 5
  48. #define CPU_NEHEMIAH_C 6
  49. /* Flags */
  50. #define USE_ACPI_C3 (1 << 1)
  51. #define USE_NORTHBRIDGE (1 << 2)
  52. static int cpu_model;
  53. static unsigned int numscales=16;
  54. static unsigned int fsb;
  55. static const struct mV_pos *vrm_mV_table;
  56. static const unsigned char *mV_vrm_table;
  57. static unsigned int highest_speed, lowest_speed; /* kHz */
  58. static unsigned int minmult, maxmult;
  59. static int can_scale_voltage;
  60. static struct acpi_processor *pr = NULL;
  61. static struct acpi_processor_cx *cx = NULL;
  62. static u32 acpi_regs_addr;
  63. static u8 longhaul_flags;
  64. static unsigned int longhaul_index;
  65. /* Module parameters */
  66. static int scale_voltage;
  67. static int disable_acpi_c3;
  68. static int revid_errata;
  69. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "longhaul", msg)
  70. /* Clock ratios multiplied by 10 */
  71. static int clock_ratio[32];
  72. static int eblcr_table[32];
  73. static int longhaul_version;
  74. static struct cpufreq_frequency_table *longhaul_table;
  75. #ifdef CONFIG_CPU_FREQ_DEBUG
  76. static char speedbuffer[8];
  77. static char *print_speed(int speed)
  78. {
  79. if (speed < 1000) {
  80. snprintf(speedbuffer, sizeof(speedbuffer),"%dMHz", speed);
  81. return speedbuffer;
  82. }
  83. if (speed%1000 == 0)
  84. snprintf(speedbuffer, sizeof(speedbuffer),
  85. "%dGHz", speed/1000);
  86. else
  87. snprintf(speedbuffer, sizeof(speedbuffer),
  88. "%d.%dGHz", speed/1000, (speed%1000)/100);
  89. return speedbuffer;
  90. }
  91. #endif
  92. static unsigned int calc_speed(int mult)
  93. {
  94. int khz;
  95. khz = (mult/10)*fsb;
  96. if (mult%10)
  97. khz += fsb/2;
  98. khz *= 1000;
  99. return khz;
  100. }
  101. static int longhaul_get_cpu_mult(void)
  102. {
  103. unsigned long invalue=0,lo, hi;
  104. rdmsr (MSR_IA32_EBL_CR_POWERON, lo, hi);
  105. invalue = (lo & (1<<22|1<<23|1<<24|1<<25)) >>22;
  106. if (longhaul_version==TYPE_LONGHAUL_V2 || longhaul_version==TYPE_POWERSAVER) {
  107. if (lo & (1<<27))
  108. invalue+=16;
  109. }
  110. return eblcr_table[invalue];
  111. }
  112. /* For processor with BCR2 MSR */
  113. static void do_longhaul1(unsigned int clock_ratio_index)
  114. {
  115. union msr_bcr2 bcr2;
  116. rdmsrl(MSR_VIA_BCR2, bcr2.val);
  117. /* Enable software clock multiplier */
  118. bcr2.bits.ESOFTBF = 1;
  119. bcr2.bits.CLOCKMUL = clock_ratio_index & 0xff;
  120. /* Sync to timer tick */
  121. safe_halt();
  122. /* Change frequency on next halt or sleep */
  123. wrmsrl(MSR_VIA_BCR2, bcr2.val);
  124. /* Invoke transition */
  125. ACPI_FLUSH_CPU_CACHE();
  126. halt();
  127. /* Disable software clock multiplier */
  128. local_irq_disable();
  129. rdmsrl(MSR_VIA_BCR2, bcr2.val);
  130. bcr2.bits.ESOFTBF = 0;
  131. wrmsrl(MSR_VIA_BCR2, bcr2.val);
  132. }
  133. /* For processor with Longhaul MSR */
  134. static void do_powersaver(int cx_address, unsigned int clock_ratio_index,
  135. unsigned int dir)
  136. {
  137. union msr_longhaul longhaul;
  138. u32 t;
  139. rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
  140. /* Setup new frequency */
  141. if (!revid_errata)
  142. longhaul.bits.RevisionKey = longhaul.bits.RevisionID;
  143. else
  144. longhaul.bits.RevisionKey = 0;
  145. longhaul.bits.SoftBusRatio = clock_ratio_index & 0xf;
  146. longhaul.bits.SoftBusRatio4 = (clock_ratio_index & 0x10) >> 4;
  147. /* Setup new voltage */
  148. if (can_scale_voltage)
  149. longhaul.bits.SoftVID = (clock_ratio_index >> 8) & 0x1f;
  150. /* Sync to timer tick */
  151. safe_halt();
  152. /* Raise voltage if necessary */
  153. if (can_scale_voltage && dir) {
  154. longhaul.bits.EnableSoftVID = 1;
  155. wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
  156. /* Change voltage */
  157. if (!cx_address) {
  158. ACPI_FLUSH_CPU_CACHE();
  159. halt();
  160. } else {
  161. ACPI_FLUSH_CPU_CACHE();
  162. /* Invoke C3 */
  163. inb(cx_address);
  164. /* Dummy op - must do something useless after P_LVL3
  165. * read */
  166. t = inl(acpi_gbl_FADT.xpm_timer_block.address);
  167. }
  168. longhaul.bits.EnableSoftVID = 0;
  169. wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
  170. }
  171. /* Change frequency on next halt or sleep */
  172. longhaul.bits.EnableSoftBusRatio = 1;
  173. wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
  174. if (!cx_address) {
  175. ACPI_FLUSH_CPU_CACHE();
  176. halt();
  177. } else {
  178. ACPI_FLUSH_CPU_CACHE();
  179. /* Invoke C3 */
  180. inb(cx_address);
  181. /* Dummy op - must do something useless after P_LVL3 read */
  182. t = inl(acpi_gbl_FADT.xpm_timer_block.address);
  183. }
  184. /* Disable bus ratio bit */
  185. longhaul.bits.EnableSoftBusRatio = 0;
  186. wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
  187. /* Reduce voltage if necessary */
  188. if (can_scale_voltage && !dir) {
  189. longhaul.bits.EnableSoftVID = 1;
  190. wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
  191. /* Change voltage */
  192. if (!cx_address) {
  193. ACPI_FLUSH_CPU_CACHE();
  194. halt();
  195. } else {
  196. ACPI_FLUSH_CPU_CACHE();
  197. /* Invoke C3 */
  198. inb(cx_address);
  199. /* Dummy op - must do something useless after P_LVL3
  200. * read */
  201. t = inl(acpi_gbl_FADT.xpm_timer_block.address);
  202. }
  203. longhaul.bits.EnableSoftVID = 0;
  204. wrmsrl(MSR_VIA_LONGHAUL, longhaul.val);
  205. }
  206. }
  207. /**
  208. * longhaul_set_cpu_frequency()
  209. * @clock_ratio_index : bitpattern of the new multiplier.
  210. *
  211. * Sets a new clock ratio.
  212. */
  213. static void longhaul_setstate(unsigned int table_index)
  214. {
  215. unsigned int clock_ratio_index;
  216. int speed, mult;
  217. struct cpufreq_freqs freqs;
  218. unsigned long flags;
  219. unsigned int pic1_mask, pic2_mask;
  220. u16 bm_status = 0;
  221. u32 bm_timeout = 1000;
  222. unsigned int dir = 0;
  223. clock_ratio_index = longhaul_table[table_index].index;
  224. /* Safety precautions */
  225. mult = clock_ratio[clock_ratio_index & 0x1f];
  226. if (mult == -1)
  227. return;
  228. speed = calc_speed(mult);
  229. if ((speed > highest_speed) || (speed < lowest_speed))
  230. return;
  231. /* Voltage transition before frequency transition? */
  232. if (can_scale_voltage && longhaul_index < table_index)
  233. dir = 1;
  234. freqs.old = calc_speed(longhaul_get_cpu_mult());
  235. freqs.new = speed;
  236. freqs.cpu = 0; /* longhaul.c is UP only driver */
  237. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  238. dprintk ("Setting to FSB:%dMHz Mult:%d.%dx (%s)\n",
  239. fsb, mult/10, mult%10, print_speed(speed/1000));
  240. retry_loop:
  241. preempt_disable();
  242. local_irq_save(flags);
  243. pic2_mask = inb(0xA1);
  244. pic1_mask = inb(0x21); /* works on C3. save mask. */
  245. outb(0xFF,0xA1); /* Overkill */
  246. outb(0xFE,0x21); /* TMR0 only */
  247. /* Wait while PCI bus is busy. */
  248. if (acpi_regs_addr && (longhaul_flags & USE_NORTHBRIDGE
  249. || ((pr != NULL) && pr->flags.bm_control))) {
  250. bm_status = inw(acpi_regs_addr);
  251. bm_status &= 1 << 4;
  252. while (bm_status && bm_timeout) {
  253. outw(1 << 4, acpi_regs_addr);
  254. bm_timeout--;
  255. bm_status = inw(acpi_regs_addr);
  256. bm_status &= 1 << 4;
  257. }
  258. }
  259. if (longhaul_flags & USE_NORTHBRIDGE) {
  260. /* Disable AGP and PCI arbiters */
  261. outb(3, 0x22);
  262. } else if ((pr != NULL) && pr->flags.bm_control) {
  263. /* Disable bus master arbitration */
  264. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
  265. }
  266. switch (longhaul_version) {
  267. /*
  268. * Longhaul v1. (Samuel[C5A] and Samuel2 stepping 0[C5B])
  269. * Software controlled multipliers only.
  270. */
  271. case TYPE_LONGHAUL_V1:
  272. do_longhaul1(clock_ratio_index);
  273. break;
  274. /*
  275. * Longhaul v2 appears in Samuel2 Steppings 1->7 [C5B] and Ezra [C5C]
  276. *
  277. * Longhaul v3 (aka Powersaver). (Ezra-T [C5M] & Nehemiah [C5N])
  278. * Nehemiah can do FSB scaling too, but this has never been proven
  279. * to work in practice.
  280. */
  281. case TYPE_LONGHAUL_V2:
  282. case TYPE_POWERSAVER:
  283. if (longhaul_flags & USE_ACPI_C3) {
  284. /* Don't allow wakeup */
  285. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
  286. do_powersaver(cx->address, clock_ratio_index, dir);
  287. } else {
  288. do_powersaver(0, clock_ratio_index, dir);
  289. }
  290. break;
  291. }
  292. if (longhaul_flags & USE_NORTHBRIDGE) {
  293. /* Enable arbiters */
  294. outb(0, 0x22);
  295. } else if ((pr != NULL) && pr->flags.bm_control) {
  296. /* Enable bus master arbitration */
  297. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
  298. }
  299. outb(pic2_mask,0xA1); /* restore mask */
  300. outb(pic1_mask,0x21);
  301. local_irq_restore(flags);
  302. preempt_enable();
  303. freqs.new = calc_speed(longhaul_get_cpu_mult());
  304. /* Check if requested frequency is set. */
  305. if (unlikely(freqs.new != speed)) {
  306. printk(KERN_INFO PFX "Failed to set requested frequency!\n");
  307. /* Revision ID = 1 but processor is expecting revision key
  308. * equal to 0. Jumpers at the bottom of processor will change
  309. * multiplier and FSB, but will not change bits in Longhaul
  310. * MSR nor enable voltage scaling. */
  311. if (!revid_errata) {
  312. printk(KERN_INFO PFX "Enabling \"Ignore Revision ID\" "
  313. "option.\n");
  314. revid_errata = 1;
  315. msleep(200);
  316. goto retry_loop;
  317. }
  318. /* Why ACPI C3 sometimes doesn't work is a mystery for me.
  319. * But it does happen. Processor is entering ACPI C3 state,
  320. * but it doesn't change frequency. I tried poking various
  321. * bits in northbridge registers, but without success. */
  322. if (longhaul_flags & USE_ACPI_C3) {
  323. printk(KERN_INFO PFX "Disabling ACPI C3 support.\n");
  324. longhaul_flags &= ~USE_ACPI_C3;
  325. if (revid_errata) {
  326. printk(KERN_INFO PFX "Disabling \"Ignore "
  327. "Revision ID\" option.\n");
  328. revid_errata = 0;
  329. }
  330. msleep(200);
  331. goto retry_loop;
  332. }
  333. /* This shouldn't happen. Longhaul ver. 2 was reported not
  334. * working on processors without voltage scaling, but with
  335. * RevID = 1. RevID errata will make things right. Just
  336. * to be 100% sure. */
  337. if (longhaul_version == TYPE_LONGHAUL_V2) {
  338. printk(KERN_INFO PFX "Switching to Longhaul ver. 1\n");
  339. longhaul_version = TYPE_LONGHAUL_V1;
  340. msleep(200);
  341. goto retry_loop;
  342. }
  343. }
  344. /* Report true CPU frequency */
  345. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  346. if (!bm_timeout)
  347. printk(KERN_INFO PFX "Warning: Timeout while waiting for idle PCI bus.\n");
  348. }
  349. /*
  350. * Centaur decided to make life a little more tricky.
  351. * Only longhaul v1 is allowed to read EBLCR BSEL[0:1].
  352. * Samuel2 and above have to try and guess what the FSB is.
  353. * We do this by assuming we booted at maximum multiplier, and interpolate
  354. * between that value multiplied by possible FSBs and cpu_mhz which
  355. * was calculated at boot time. Really ugly, but no other way to do this.
  356. */
  357. #define ROUNDING 0xf
  358. static int guess_fsb(int mult)
  359. {
  360. int speed = cpu_khz / 1000;
  361. int i;
  362. int speeds[] = { 666, 1000, 1333, 2000 };
  363. int f_max, f_min;
  364. for (i = 0; i < 4; i++) {
  365. f_max = ((speeds[i] * mult) + 50) / 100;
  366. f_max += (ROUNDING / 2);
  367. f_min = f_max - ROUNDING;
  368. if ((speed <= f_max) && (speed >= f_min))
  369. return speeds[i] / 10;
  370. }
  371. return 0;
  372. }
  373. static int __init longhaul_get_ranges(void)
  374. {
  375. unsigned int i, j, k = 0;
  376. unsigned int ratio;
  377. int mult;
  378. /* Get current frequency */
  379. mult = longhaul_get_cpu_mult();
  380. if (mult == -1) {
  381. printk(KERN_INFO PFX "Invalid (reserved) multiplier!\n");
  382. return -EINVAL;
  383. }
  384. fsb = guess_fsb(mult);
  385. if (fsb == 0) {
  386. printk(KERN_INFO PFX "Invalid (reserved) FSB!\n");
  387. return -EINVAL;
  388. }
  389. /* Get max multiplier - as we always did.
  390. * Longhaul MSR is usefull only when voltage scaling is enabled.
  391. * C3 is booting at max anyway. */
  392. maxmult = mult;
  393. /* Get min multiplier */
  394. switch (cpu_model) {
  395. case CPU_NEHEMIAH:
  396. minmult = 50;
  397. break;
  398. case CPU_NEHEMIAH_C:
  399. minmult = 40;
  400. break;
  401. default:
  402. minmult = 30;
  403. break;
  404. }
  405. dprintk ("MinMult:%d.%dx MaxMult:%d.%dx\n",
  406. minmult/10, minmult%10, maxmult/10, maxmult%10);
  407. highest_speed = calc_speed(maxmult);
  408. lowest_speed = calc_speed(minmult);
  409. dprintk ("FSB:%dMHz Lowest speed: %s Highest speed:%s\n", fsb,
  410. print_speed(lowest_speed/1000),
  411. print_speed(highest_speed/1000));
  412. if (lowest_speed == highest_speed) {
  413. printk (KERN_INFO PFX "highestspeed == lowest, aborting.\n");
  414. return -EINVAL;
  415. }
  416. if (lowest_speed > highest_speed) {
  417. printk (KERN_INFO PFX "nonsense! lowest (%d > %d) !\n",
  418. lowest_speed, highest_speed);
  419. return -EINVAL;
  420. }
  421. longhaul_table = kmalloc((numscales + 1) * sizeof(struct cpufreq_frequency_table), GFP_KERNEL);
  422. if(!longhaul_table)
  423. return -ENOMEM;
  424. for (j = 0; j < numscales; j++) {
  425. ratio = clock_ratio[j];
  426. if (ratio == -1)
  427. continue;
  428. if (ratio > maxmult || ratio < minmult)
  429. continue;
  430. longhaul_table[k].frequency = calc_speed(ratio);
  431. longhaul_table[k].index = j;
  432. k++;
  433. }
  434. if (k <= 1) {
  435. kfree(longhaul_table);
  436. return -ENODEV;
  437. }
  438. /* Sort */
  439. for (j = 0; j < k - 1; j++) {
  440. unsigned int min_f, min_i;
  441. min_f = longhaul_table[j].frequency;
  442. min_i = j;
  443. for (i = j + 1; i < k; i++) {
  444. if (longhaul_table[i].frequency < min_f) {
  445. min_f = longhaul_table[i].frequency;
  446. min_i = i;
  447. }
  448. }
  449. if (min_i != j) {
  450. unsigned int temp;
  451. temp = longhaul_table[j].frequency;
  452. longhaul_table[j].frequency = longhaul_table[min_i].frequency;
  453. longhaul_table[min_i].frequency = temp;
  454. temp = longhaul_table[j].index;
  455. longhaul_table[j].index = longhaul_table[min_i].index;
  456. longhaul_table[min_i].index = temp;
  457. }
  458. }
  459. longhaul_table[k].frequency = CPUFREQ_TABLE_END;
  460. /* Find index we are running on */
  461. for (j = 0; j < k; j++) {
  462. if (clock_ratio[longhaul_table[j].index & 0x1f] == mult) {
  463. longhaul_index = j;
  464. break;
  465. }
  466. }
  467. return 0;
  468. }
  469. static void __init longhaul_setup_voltagescaling(void)
  470. {
  471. union msr_longhaul longhaul;
  472. struct mV_pos minvid, maxvid, vid;
  473. unsigned int j, speed, pos, kHz_step, numvscales;
  474. int min_vid_speed;
  475. rdmsrl(MSR_VIA_LONGHAUL, longhaul.val);
  476. if (!(longhaul.bits.RevisionID & 1)) {
  477. printk(KERN_INFO PFX "Voltage scaling not supported by CPU.\n");
  478. return;
  479. }
  480. if (!longhaul.bits.VRMRev) {
  481. printk(KERN_INFO PFX "VRM 8.5\n");
  482. vrm_mV_table = &vrm85_mV[0];
  483. mV_vrm_table = &mV_vrm85[0];
  484. } else {
  485. printk(KERN_INFO PFX "Mobile VRM\n");
  486. if (cpu_model < CPU_NEHEMIAH)
  487. return;
  488. vrm_mV_table = &mobilevrm_mV[0];
  489. mV_vrm_table = &mV_mobilevrm[0];
  490. }
  491. minvid = vrm_mV_table[longhaul.bits.MinimumVID];
  492. maxvid = vrm_mV_table[longhaul.bits.MaximumVID];
  493. if (minvid.mV == 0 || maxvid.mV == 0 || minvid.mV > maxvid.mV) {
  494. printk (KERN_INFO PFX "Bogus values Min:%d.%03d Max:%d.%03d. "
  495. "Voltage scaling disabled.\n",
  496. minvid.mV/1000, minvid.mV%1000, maxvid.mV/1000, maxvid.mV%1000);
  497. return;
  498. }
  499. if (minvid.mV == maxvid.mV) {
  500. printk (KERN_INFO PFX "Claims to support voltage scaling but min & max are "
  501. "both %d.%03d. Voltage scaling disabled\n",
  502. maxvid.mV/1000, maxvid.mV%1000);
  503. return;
  504. }
  505. /* How many voltage steps */
  506. numvscales = maxvid.pos - minvid.pos + 1;
  507. printk(KERN_INFO PFX
  508. "Max VID=%d.%03d "
  509. "Min VID=%d.%03d, "
  510. "%d possible voltage scales\n",
  511. maxvid.mV/1000, maxvid.mV%1000,
  512. minvid.mV/1000, minvid.mV%1000,
  513. numvscales);
  514. /* Calculate max frequency at min voltage */
  515. j = longhaul.bits.MinMHzBR;
  516. if (longhaul.bits.MinMHzBR4)
  517. j += 16;
  518. min_vid_speed = eblcr_table[j];
  519. if (min_vid_speed == -1)
  520. return;
  521. switch (longhaul.bits.MinMHzFSB) {
  522. case 0:
  523. min_vid_speed *= 13333;
  524. break;
  525. case 1:
  526. min_vid_speed *= 10000;
  527. break;
  528. case 3:
  529. min_vid_speed *= 6666;
  530. break;
  531. default:
  532. return;
  533. break;
  534. }
  535. if (min_vid_speed >= highest_speed)
  536. return;
  537. /* Calculate kHz for one voltage step */
  538. kHz_step = (highest_speed - min_vid_speed) / numvscales;
  539. j = 0;
  540. while (longhaul_table[j].frequency != CPUFREQ_TABLE_END) {
  541. speed = longhaul_table[j].frequency;
  542. if (speed > min_vid_speed)
  543. pos = (speed - min_vid_speed) / kHz_step + minvid.pos;
  544. else
  545. pos = minvid.pos;
  546. longhaul_table[j].index |= mV_vrm_table[pos] << 8;
  547. vid = vrm_mV_table[mV_vrm_table[pos]];
  548. printk(KERN_INFO PFX "f: %d kHz, index: %d, vid: %d mV\n", speed, j, vid.mV);
  549. j++;
  550. }
  551. can_scale_voltage = 1;
  552. printk(KERN_INFO PFX "Voltage scaling enabled.\n");
  553. }
  554. static int longhaul_verify(struct cpufreq_policy *policy)
  555. {
  556. return cpufreq_frequency_table_verify(policy, longhaul_table);
  557. }
  558. static int longhaul_target(struct cpufreq_policy *policy,
  559. unsigned int target_freq, unsigned int relation)
  560. {
  561. unsigned int table_index = 0;
  562. unsigned int i;
  563. unsigned int dir = 0;
  564. u8 vid, current_vid;
  565. if (cpufreq_frequency_table_target(policy, longhaul_table, target_freq, relation, &table_index))
  566. return -EINVAL;
  567. /* Don't set same frequency again */
  568. if (longhaul_index == table_index)
  569. return 0;
  570. if (!can_scale_voltage)
  571. longhaul_setstate(table_index);
  572. else {
  573. /* On test system voltage transitions exceeding single
  574. * step up or down were turning motherboard off. Both
  575. * "ondemand" and "userspace" are unsafe. C7 is doing
  576. * this in hardware, C3 is old and we need to do this
  577. * in software. */
  578. i = longhaul_index;
  579. current_vid = (longhaul_table[longhaul_index].index >> 8) & 0x1f;
  580. if (table_index > longhaul_index)
  581. dir = 1;
  582. while (i != table_index) {
  583. vid = (longhaul_table[i].index >> 8) & 0x1f;
  584. if (vid != current_vid) {
  585. longhaul_setstate(i);
  586. current_vid = vid;
  587. msleep(200);
  588. }
  589. if (dir)
  590. i++;
  591. else
  592. i--;
  593. }
  594. longhaul_setstate(table_index);
  595. }
  596. longhaul_index = table_index;
  597. return 0;
  598. }
  599. static unsigned int longhaul_get(unsigned int cpu)
  600. {
  601. if (cpu)
  602. return 0;
  603. return calc_speed(longhaul_get_cpu_mult());
  604. }
  605. static acpi_status longhaul_walk_callback(acpi_handle obj_handle,
  606. u32 nesting_level,
  607. void *context, void **return_value)
  608. {
  609. struct acpi_device *d;
  610. if ( acpi_bus_get_device(obj_handle, &d) ) {
  611. return 0;
  612. }
  613. *return_value = (void *)acpi_driver_data(d);
  614. return 1;
  615. }
  616. /* VIA don't support PM2 reg, but have something similar */
  617. static int enable_arbiter_disable(void)
  618. {
  619. struct pci_dev *dev;
  620. int status = 1;
  621. int reg;
  622. u8 pci_cmd;
  623. /* Find PLE133 host bridge */
  624. reg = 0x78;
  625. dev = pci_get_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8601_0,
  626. NULL);
  627. /* Find PM133/VT8605 host bridge */
  628. if (dev == NULL)
  629. dev = pci_get_device(PCI_VENDOR_ID_VIA,
  630. PCI_DEVICE_ID_VIA_8605_0, NULL);
  631. /* Find CLE266 host bridge */
  632. if (dev == NULL) {
  633. reg = 0x76;
  634. dev = pci_get_device(PCI_VENDOR_ID_VIA,
  635. PCI_DEVICE_ID_VIA_862X_0, NULL);
  636. /* Find CN400 V-Link host bridge */
  637. if (dev == NULL)
  638. dev = pci_get_device(PCI_VENDOR_ID_VIA, 0x7259, NULL);
  639. }
  640. if (dev != NULL) {
  641. /* Enable access to port 0x22 */
  642. pci_read_config_byte(dev, reg, &pci_cmd);
  643. if (!(pci_cmd & 1<<7)) {
  644. pci_cmd |= 1<<7;
  645. pci_write_config_byte(dev, reg, pci_cmd);
  646. pci_read_config_byte(dev, reg, &pci_cmd);
  647. if (!(pci_cmd & 1<<7)) {
  648. printk(KERN_ERR PFX
  649. "Can't enable access to port 0x22.\n");
  650. status = 0;
  651. }
  652. }
  653. pci_dev_put(dev);
  654. return status;
  655. }
  656. return 0;
  657. }
  658. static int longhaul_setup_southbridge(void)
  659. {
  660. struct pci_dev *dev;
  661. u8 pci_cmd;
  662. /* Find VT8235 southbridge */
  663. dev = pci_get_device(PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_8235, NULL);
  664. if (dev == NULL)
  665. /* Find VT8237 southbridge */
  666. dev = pci_get_device(PCI_VENDOR_ID_VIA,
  667. PCI_DEVICE_ID_VIA_8237, NULL);
  668. if (dev != NULL) {
  669. /* Set transition time to max */
  670. pci_read_config_byte(dev, 0xec, &pci_cmd);
  671. pci_cmd &= ~(1 << 2);
  672. pci_write_config_byte(dev, 0xec, pci_cmd);
  673. pci_read_config_byte(dev, 0xe4, &pci_cmd);
  674. pci_cmd &= ~(1 << 7);
  675. pci_write_config_byte(dev, 0xe4, pci_cmd);
  676. pci_read_config_byte(dev, 0xe5, &pci_cmd);
  677. pci_cmd |= 1 << 7;
  678. pci_write_config_byte(dev, 0xe5, pci_cmd);
  679. /* Get address of ACPI registers block*/
  680. pci_read_config_byte(dev, 0x81, &pci_cmd);
  681. if (pci_cmd & 1 << 7) {
  682. pci_read_config_dword(dev, 0x88, &acpi_regs_addr);
  683. acpi_regs_addr &= 0xff00;
  684. printk(KERN_INFO PFX "ACPI I/O at 0x%x\n", acpi_regs_addr);
  685. }
  686. pci_dev_put(dev);
  687. return 1;
  688. }
  689. return 0;
  690. }
  691. static int __init longhaul_cpu_init(struct cpufreq_policy *policy)
  692. {
  693. struct cpuinfo_x86 *c = cpu_data;
  694. char *cpuname=NULL;
  695. int ret;
  696. u32 lo, hi;
  697. /* Check what we have on this motherboard */
  698. switch (c->x86_model) {
  699. case 6:
  700. cpu_model = CPU_SAMUEL;
  701. cpuname = "C3 'Samuel' [C5A]";
  702. longhaul_version = TYPE_LONGHAUL_V1;
  703. memcpy (clock_ratio, samuel1_clock_ratio, sizeof(samuel1_clock_ratio));
  704. memcpy (eblcr_table, samuel1_eblcr, sizeof(samuel1_eblcr));
  705. break;
  706. case 7:
  707. switch (c->x86_mask) {
  708. case 0:
  709. longhaul_version = TYPE_LONGHAUL_V1;
  710. cpu_model = CPU_SAMUEL2;
  711. cpuname = "C3 'Samuel 2' [C5B]";
  712. /* Note, this is not a typo, early Samuel2's had
  713. * Samuel1 ratios. */
  714. memcpy(clock_ratio, samuel1_clock_ratio,
  715. sizeof(samuel1_clock_ratio));
  716. memcpy(eblcr_table, samuel2_eblcr,
  717. sizeof(samuel2_eblcr));
  718. break;
  719. case 1 ... 15:
  720. longhaul_version = TYPE_LONGHAUL_V1;
  721. if (c->x86_mask < 8) {
  722. cpu_model = CPU_SAMUEL2;
  723. cpuname = "C3 'Samuel 2' [C5B]";
  724. } else {
  725. cpu_model = CPU_EZRA;
  726. cpuname = "C3 'Ezra' [C5C]";
  727. }
  728. memcpy(clock_ratio, ezra_clock_ratio,
  729. sizeof(ezra_clock_ratio));
  730. memcpy(eblcr_table, ezra_eblcr,
  731. sizeof(ezra_eblcr));
  732. break;
  733. }
  734. break;
  735. case 8:
  736. cpu_model = CPU_EZRA_T;
  737. cpuname = "C3 'Ezra-T' [C5M]";
  738. longhaul_version = TYPE_POWERSAVER;
  739. numscales=32;
  740. memcpy (clock_ratio, ezrat_clock_ratio, sizeof(ezrat_clock_ratio));
  741. memcpy (eblcr_table, ezrat_eblcr, sizeof(ezrat_eblcr));
  742. break;
  743. case 9:
  744. longhaul_version = TYPE_POWERSAVER;
  745. numscales = 32;
  746. memcpy(clock_ratio,
  747. nehemiah_clock_ratio,
  748. sizeof(nehemiah_clock_ratio));
  749. memcpy(eblcr_table, nehemiah_eblcr, sizeof(nehemiah_eblcr));
  750. switch (c->x86_mask) {
  751. case 0 ... 1:
  752. cpu_model = CPU_NEHEMIAH;
  753. cpuname = "C3 'Nehemiah A' [C5XLOE]";
  754. break;
  755. case 2 ... 4:
  756. cpu_model = CPU_NEHEMIAH;
  757. cpuname = "C3 'Nehemiah B' [C5XLOH]";
  758. break;
  759. case 5 ... 15:
  760. cpu_model = CPU_NEHEMIAH_C;
  761. cpuname = "C3 'Nehemiah C' [C5P]";
  762. break;
  763. }
  764. break;
  765. default:
  766. cpuname = "Unknown";
  767. break;
  768. }
  769. /* Check Longhaul ver. 2 */
  770. if (longhaul_version == TYPE_LONGHAUL_V2) {
  771. rdmsr(MSR_VIA_LONGHAUL, lo, hi);
  772. if (lo == 0 && hi == 0)
  773. /* Looks like MSR isn't present */
  774. longhaul_version = TYPE_LONGHAUL_V1;
  775. }
  776. printk (KERN_INFO PFX "VIA %s CPU detected. ", cpuname);
  777. switch (longhaul_version) {
  778. case TYPE_LONGHAUL_V1:
  779. case TYPE_LONGHAUL_V2:
  780. printk ("Longhaul v%d supported.\n", longhaul_version);
  781. break;
  782. case TYPE_POWERSAVER:
  783. printk ("Powersaver supported.\n");
  784. break;
  785. };
  786. /* Doesn't hurt */
  787. longhaul_setup_southbridge();
  788. /* Find ACPI data for processor */
  789. acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
  790. ACPI_UINT32_MAX, &longhaul_walk_callback,
  791. NULL, (void *)&pr);
  792. /* Check ACPI support for C3 state */
  793. if (pr != NULL && longhaul_version == TYPE_POWERSAVER) {
  794. cx = &pr->power.states[ACPI_STATE_C3];
  795. if (cx->address > 0 && cx->latency <= 1000)
  796. longhaul_flags |= USE_ACPI_C3;
  797. }
  798. /* Disable if it isn't working */
  799. if (disable_acpi_c3)
  800. longhaul_flags &= ~USE_ACPI_C3;
  801. /* Check if northbridge is friendly */
  802. if (enable_arbiter_disable())
  803. longhaul_flags |= USE_NORTHBRIDGE;
  804. /* Check ACPI support for bus master arbiter disable */
  805. if (!(longhaul_flags & USE_ACPI_C3
  806. || longhaul_flags & USE_NORTHBRIDGE)
  807. && ((pr == NULL) || !(pr->flags.bm_control))) {
  808. printk(KERN_ERR PFX
  809. "No ACPI support. Unsupported northbridge.\n");
  810. return -ENODEV;
  811. }
  812. if (longhaul_flags & USE_NORTHBRIDGE)
  813. printk(KERN_INFO PFX "Using northbridge support.\n");
  814. if (longhaul_flags & USE_ACPI_C3)
  815. printk(KERN_INFO PFX "Using ACPI support.\n");
  816. ret = longhaul_get_ranges();
  817. if (ret != 0)
  818. return ret;
  819. if ((longhaul_version != TYPE_LONGHAUL_V1) && (scale_voltage != 0))
  820. longhaul_setup_voltagescaling();
  821. policy->cpuinfo.transition_latency = 200000; /* nsec */
  822. policy->cur = calc_speed(longhaul_get_cpu_mult());
  823. ret = cpufreq_frequency_table_cpuinfo(policy, longhaul_table);
  824. if (ret)
  825. return ret;
  826. cpufreq_frequency_table_get_attr(longhaul_table, policy->cpu);
  827. return 0;
  828. }
  829. static int __devexit longhaul_cpu_exit(struct cpufreq_policy *policy)
  830. {
  831. cpufreq_frequency_table_put_attr(policy->cpu);
  832. return 0;
  833. }
  834. static struct freq_attr* longhaul_attr[] = {
  835. &cpufreq_freq_attr_scaling_available_freqs,
  836. NULL,
  837. };
  838. static struct cpufreq_driver longhaul_driver = {
  839. .verify = longhaul_verify,
  840. .target = longhaul_target,
  841. .get = longhaul_get,
  842. .init = longhaul_cpu_init,
  843. .exit = __devexit_p(longhaul_cpu_exit),
  844. .name = "longhaul",
  845. .owner = THIS_MODULE,
  846. .attr = longhaul_attr,
  847. };
  848. static int __init longhaul_init(void)
  849. {
  850. struct cpuinfo_x86 *c = cpu_data;
  851. if (c->x86_vendor != X86_VENDOR_CENTAUR || c->x86 != 6)
  852. return -ENODEV;
  853. #ifdef CONFIG_SMP
  854. if (num_online_cpus() > 1) {
  855. printk(KERN_ERR PFX "More than 1 CPU detected, longhaul disabled.\n");
  856. return -ENODEV;
  857. }
  858. #endif
  859. #ifdef CONFIG_X86_IO_APIC
  860. if (cpu_has_apic) {
  861. printk(KERN_ERR PFX "APIC detected. Longhaul is currently broken in this configuration.\n");
  862. return -ENODEV;
  863. }
  864. #endif
  865. switch (c->x86_model) {
  866. case 6 ... 9:
  867. return cpufreq_register_driver(&longhaul_driver);
  868. case 10:
  869. printk(KERN_ERR PFX "Use acpi-cpufreq driver for VIA C7\n");
  870. default:
  871. ;;
  872. }
  873. return -ENODEV;
  874. }
  875. static void __exit longhaul_exit(void)
  876. {
  877. int i;
  878. for (i=0; i < numscales; i++) {
  879. if (clock_ratio[i] == maxmult) {
  880. longhaul_setstate(i);
  881. break;
  882. }
  883. }
  884. cpufreq_unregister_driver(&longhaul_driver);
  885. kfree(longhaul_table);
  886. }
  887. /* Even if BIOS is exporting ACPI C3 state, and it is used
  888. * with success when CPU is idle, this state doesn't
  889. * trigger frequency transition in some cases. */
  890. module_param (disable_acpi_c3, int, 0644);
  891. MODULE_PARM_DESC(disable_acpi_c3, "Don't use ACPI C3 support");
  892. /* Change CPU voltage with frequency. Very usefull to save
  893. * power, but most VIA C3 processors aren't supporting it. */
  894. module_param (scale_voltage, int, 0644);
  895. MODULE_PARM_DESC(scale_voltage, "Scale voltage of processor");
  896. /* Force revision key to 0 for processors which doesn't
  897. * support voltage scaling, but are introducing itself as
  898. * such. */
  899. module_param(revid_errata, int, 0644);
  900. MODULE_PARM_DESC(revid_errata, "Ignore CPU Revision ID");
  901. MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>");
  902. MODULE_DESCRIPTION ("Longhaul driver for VIA Cyrix processors.");
  903. MODULE_LICENSE ("GPL");
  904. late_initcall(longhaul_init);
  905. module_exit(longhaul_exit);