longhaul.c 26 KB

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