longhaul.c 24 KB

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