longhaul.c 26 KB

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