longhaul.c 25 KB

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