longhaul.c 24 KB

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