longhaul.c 26 KB

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