longhaul.c 26 KB

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