longhaul.c 24 KB

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