/arch/i386/kernel/cpu/cpufreq/powernow-k7.c

https://bitbucket.org/evzijst/gittest · C · 690 lines · 502 code · 145 blank · 43 comment · 81 complexity · 2028e4ee1113e8c5851a8d4128407d4d MD5 · raw file

  1. /*
  2. * AMD K7 Powernow driver.
  3. * (C) 2003 Dave Jones <davej@codemonkey.org.uk> on behalf of SuSE Labs.
  4. * (C) 2003-2004 Dave Jones <davej@redhat.com>
  5. *
  6. * Licensed under the terms of the GNU GPL License version 2.
  7. * Based upon datasheets & sample CPUs kindly provided by AMD.
  8. *
  9. * Errata 5: Processor may fail to execute a FID/VID change in presence of interrupt.
  10. * - We cli/sti on stepping A0 CPUs around the FID/VID transition.
  11. * Errata 15: Processors with half frequency multipliers may hang upon wakeup from disconnect.
  12. * - We disable half multipliers if ACPI is used on A0 stepping CPUs.
  13. */
  14. #include <linux/config.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/init.h>
  19. #include <linux/cpufreq.h>
  20. #include <linux/slab.h>
  21. #include <linux/string.h>
  22. #include <linux/dmi.h>
  23. #include <asm/msr.h>
  24. #include <asm/timex.h>
  25. #include <asm/io.h>
  26. #include <asm/system.h>
  27. #ifdef CONFIG_X86_POWERNOW_K7_ACPI
  28. #include <linux/acpi.h>
  29. #include <acpi/processor.h>
  30. #endif
  31. #include "powernow-k7.h"
  32. #define PFX "powernow: "
  33. struct psb_s {
  34. u8 signature[10];
  35. u8 tableversion;
  36. u8 flags;
  37. u16 settlingtime;
  38. u8 reserved1;
  39. u8 numpst;
  40. };
  41. struct pst_s {
  42. u32 cpuid;
  43. u8 fsbspeed;
  44. u8 maxfid;
  45. u8 startvid;
  46. u8 numpstates;
  47. };
  48. #ifdef CONFIG_X86_POWERNOW_K7_ACPI
  49. union powernow_acpi_control_t {
  50. struct {
  51. unsigned long fid:5,
  52. vid:5,
  53. sgtc:20,
  54. res1:2;
  55. } bits;
  56. unsigned long val;
  57. };
  58. #endif
  59. #ifdef CONFIG_CPU_FREQ_DEBUG
  60. /* divide by 1000 to get VCore voltage in V. */
  61. static int mobile_vid_table[32] = {
  62. 2000, 1950, 1900, 1850, 1800, 1750, 1700, 1650,
  63. 1600, 1550, 1500, 1450, 1400, 1350, 1300, 0,
  64. 1275, 1250, 1225, 1200, 1175, 1150, 1125, 1100,
  65. 1075, 1050, 1025, 1000, 975, 950, 925, 0,
  66. };
  67. #endif
  68. /* divide by 10 to get FID. */
  69. static int fid_codes[32] = {
  70. 110, 115, 120, 125, 50, 55, 60, 65,
  71. 70, 75, 80, 85, 90, 95, 100, 105,
  72. 30, 190, 40, 200, 130, 135, 140, 210,
  73. 150, 225, 160, 165, 170, 180, -1, -1,
  74. };
  75. /* This parameter is used in order to force ACPI instead of legacy method for
  76. * configuration purpose.
  77. */
  78. static int acpi_force;
  79. static struct cpufreq_frequency_table *powernow_table;
  80. static unsigned int can_scale_bus;
  81. static unsigned int can_scale_vid;
  82. static unsigned int minimum_speed=-1;
  83. static unsigned int maximum_speed;
  84. static unsigned int number_scales;
  85. static unsigned int fsb;
  86. static unsigned int latency;
  87. static char have_a0;
  88. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "powernow-k7", msg)
  89. static int check_fsb(unsigned int fsbspeed)
  90. {
  91. int delta;
  92. unsigned int f = fsb / 1000;
  93. delta = (fsbspeed > f) ? fsbspeed - f : f - fsbspeed;
  94. return (delta < 5);
  95. }
  96. static int check_powernow(void)
  97. {
  98. struct cpuinfo_x86 *c = cpu_data;
  99. unsigned int maxei, eax, ebx, ecx, edx;
  100. if ((c->x86_vendor != X86_VENDOR_AMD) || (c->x86 !=6)) {
  101. #ifdef MODULE
  102. printk (KERN_INFO PFX "This module only works with AMD K7 CPUs\n");
  103. #endif
  104. return 0;
  105. }
  106. /* Get maximum capabilities */
  107. maxei = cpuid_eax (0x80000000);
  108. if (maxei < 0x80000007) { /* Any powernow info ? */
  109. #ifdef MODULE
  110. printk (KERN_INFO PFX "No powernow capabilities detected\n");
  111. #endif
  112. return 0;
  113. }
  114. if ((c->x86_model == 6) && (c->x86_mask == 0)) {
  115. printk (KERN_INFO PFX "K7 660[A0] core detected, enabling errata workarounds\n");
  116. have_a0 = 1;
  117. }
  118. cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
  119. /* Check we can actually do something before we say anything.*/
  120. if (!(edx & (1 << 1 | 1 << 2)))
  121. return 0;
  122. printk (KERN_INFO PFX "PowerNOW! Technology present. Can scale: ");
  123. if (edx & 1 << 1) {
  124. printk ("frequency");
  125. can_scale_bus=1;
  126. }
  127. if ((edx & (1 << 1 | 1 << 2)) == 0x6)
  128. printk (" and ");
  129. if (edx & 1 << 2) {
  130. printk ("voltage");
  131. can_scale_vid=1;
  132. }
  133. printk (".\n");
  134. return 1;
  135. }
  136. static int get_ranges (unsigned char *pst)
  137. {
  138. unsigned int j;
  139. unsigned int speed;
  140. u8 fid, vid;
  141. powernow_table = kmalloc((sizeof(struct cpufreq_frequency_table) * (number_scales + 1)), GFP_KERNEL);
  142. if (!powernow_table)
  143. return -ENOMEM;
  144. memset(powernow_table, 0, (sizeof(struct cpufreq_frequency_table) * (number_scales + 1)));
  145. for (j=0 ; j < number_scales; j++) {
  146. fid = *pst++;
  147. powernow_table[j].frequency = (fsb * fid_codes[fid]) / 10;
  148. powernow_table[j].index = fid; /* lower 8 bits */
  149. speed = powernow_table[j].frequency;
  150. if ((fid_codes[fid] % 10)==5) {
  151. #ifdef CONFIG_X86_POWERNOW_K7_ACPI
  152. if (have_a0 == 1)
  153. powernow_table[j].frequency = CPUFREQ_ENTRY_INVALID;
  154. #endif
  155. }
  156. if (speed < minimum_speed)
  157. minimum_speed = speed;
  158. if (speed > maximum_speed)
  159. maximum_speed = speed;
  160. vid = *pst++;
  161. powernow_table[j].index |= (vid << 8); /* upper 8 bits */
  162. dprintk (" FID: 0x%x (%d.%dx [%dMHz]) "
  163. "VID: 0x%x (%d.%03dV)\n", fid, fid_codes[fid] / 10,
  164. fid_codes[fid] % 10, speed/1000, vid,
  165. mobile_vid_table[vid]/1000,
  166. mobile_vid_table[vid]%1000);
  167. }
  168. powernow_table[number_scales].frequency = CPUFREQ_TABLE_END;
  169. powernow_table[number_scales].index = 0;
  170. return 0;
  171. }
  172. static void change_FID(int fid)
  173. {
  174. union msr_fidvidctl fidvidctl;
  175. rdmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
  176. if (fidvidctl.bits.FID != fid) {
  177. fidvidctl.bits.SGTC = latency;
  178. fidvidctl.bits.FID = fid;
  179. fidvidctl.bits.VIDC = 0;
  180. fidvidctl.bits.FIDC = 1;
  181. wrmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
  182. }
  183. }
  184. static void change_VID(int vid)
  185. {
  186. union msr_fidvidctl fidvidctl;
  187. rdmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
  188. if (fidvidctl.bits.VID != vid) {
  189. fidvidctl.bits.SGTC = latency;
  190. fidvidctl.bits.VID = vid;
  191. fidvidctl.bits.FIDC = 0;
  192. fidvidctl.bits.VIDC = 1;
  193. wrmsrl (MSR_K7_FID_VID_CTL, fidvidctl.val);
  194. }
  195. }
  196. static void change_speed (unsigned int index)
  197. {
  198. u8 fid, vid;
  199. struct cpufreq_freqs freqs;
  200. union msr_fidvidstatus fidvidstatus;
  201. int cfid;
  202. /* fid are the lower 8 bits of the index we stored into
  203. * the cpufreq frequency table in powernow_decode_bios,
  204. * vid are the upper 8 bits.
  205. */
  206. fid = powernow_table[index].index & 0xFF;
  207. vid = (powernow_table[index].index & 0xFF00) >> 8;
  208. freqs.cpu = 0;
  209. rdmsrl (MSR_K7_FID_VID_STATUS, fidvidstatus.val);
  210. cfid = fidvidstatus.bits.CFID;
  211. freqs.old = fsb * fid_codes[cfid] / 10;
  212. freqs.new = powernow_table[index].frequency;
  213. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  214. /* Now do the magic poking into the MSRs. */
  215. if (have_a0 == 1) /* A0 errata 5 */
  216. local_irq_disable();
  217. if (freqs.old > freqs.new) {
  218. /* Going down, so change FID first */
  219. change_FID(fid);
  220. change_VID(vid);
  221. } else {
  222. /* Going up, so change VID first */
  223. change_VID(vid);
  224. change_FID(fid);
  225. }
  226. if (have_a0 == 1)
  227. local_irq_enable();
  228. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  229. }
  230. #ifdef CONFIG_X86_POWERNOW_K7_ACPI
  231. static struct acpi_processor_performance *acpi_processor_perf;
  232. static int powernow_acpi_init(void)
  233. {
  234. int i;
  235. int retval = 0;
  236. union powernow_acpi_control_t pc;
  237. if (acpi_processor_perf != NULL && powernow_table != NULL) {
  238. retval = -EINVAL;
  239. goto err0;
  240. }
  241. acpi_processor_perf = kmalloc(sizeof(struct acpi_processor_performance),
  242. GFP_KERNEL);
  243. if (!acpi_processor_perf) {
  244. retval = -ENOMEM;
  245. goto err0;
  246. }
  247. memset(acpi_processor_perf, 0, sizeof(struct acpi_processor_performance));
  248. if (acpi_processor_register_performance(acpi_processor_perf, 0)) {
  249. retval = -EIO;
  250. goto err1;
  251. }
  252. if (acpi_processor_perf->control_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) {
  253. retval = -ENODEV;
  254. goto err2;
  255. }
  256. if (acpi_processor_perf->status_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) {
  257. retval = -ENODEV;
  258. goto err2;
  259. }
  260. number_scales = acpi_processor_perf->state_count;
  261. if (number_scales < 2) {
  262. retval = -ENODEV;
  263. goto err2;
  264. }
  265. powernow_table = kmalloc((number_scales + 1) * (sizeof(struct cpufreq_frequency_table)), GFP_KERNEL);
  266. if (!powernow_table) {
  267. retval = -ENOMEM;
  268. goto err2;
  269. }
  270. memset(powernow_table, 0, ((number_scales + 1) * sizeof(struct cpufreq_frequency_table)));
  271. pc.val = (unsigned long) acpi_processor_perf->states[0].control;
  272. for (i = 0; i < number_scales; i++) {
  273. u8 fid, vid;
  274. unsigned int speed;
  275. pc.val = (unsigned long) acpi_processor_perf->states[i].control;
  276. dprintk ("acpi: P%d: %d MHz %d mW %d uS control %08x SGTC %d\n",
  277. i,
  278. (u32) acpi_processor_perf->states[i].core_frequency,
  279. (u32) acpi_processor_perf->states[i].power,
  280. (u32) acpi_processor_perf->states[i].transition_latency,
  281. (u32) acpi_processor_perf->states[i].control,
  282. pc.bits.sgtc);
  283. vid = pc.bits.vid;
  284. fid = pc.bits.fid;
  285. powernow_table[i].frequency = fsb * fid_codes[fid] / 10;
  286. powernow_table[i].index = fid; /* lower 8 bits */
  287. powernow_table[i].index |= (vid << 8); /* upper 8 bits */
  288. speed = powernow_table[i].frequency;
  289. if ((fid_codes[fid] % 10)==5) {
  290. if (have_a0 == 1)
  291. powernow_table[i].frequency = CPUFREQ_ENTRY_INVALID;
  292. }
  293. dprintk (" FID: 0x%x (%d.%dx [%dMHz]) "
  294. "VID: 0x%x (%d.%03dV)\n", fid, fid_codes[fid] / 10,
  295. fid_codes[fid] % 10, speed/1000, vid,
  296. mobile_vid_table[vid]/1000,
  297. mobile_vid_table[vid]%1000);
  298. if (latency < pc.bits.sgtc)
  299. latency = pc.bits.sgtc;
  300. if (speed < minimum_speed)
  301. minimum_speed = speed;
  302. if (speed > maximum_speed)
  303. maximum_speed = speed;
  304. }
  305. powernow_table[i].frequency = CPUFREQ_TABLE_END;
  306. powernow_table[i].index = 0;
  307. /* notify BIOS that we exist */
  308. acpi_processor_notify_smm(THIS_MODULE);
  309. return 0;
  310. err2:
  311. acpi_processor_unregister_performance(acpi_processor_perf, 0);
  312. err1:
  313. kfree(acpi_processor_perf);
  314. err0:
  315. printk(KERN_WARNING PFX "ACPI perflib can not be used in this platform\n");
  316. acpi_processor_perf = NULL;
  317. return retval;
  318. }
  319. #else
  320. static int powernow_acpi_init(void)
  321. {
  322. printk(KERN_INFO PFX "no support for ACPI processor found."
  323. " Please recompile your kernel with ACPI processor\n");
  324. return -EINVAL;
  325. }
  326. #endif
  327. static int powernow_decode_bios (int maxfid, int startvid)
  328. {
  329. struct psb_s *psb;
  330. struct pst_s *pst;
  331. unsigned int i, j;
  332. unsigned char *p;
  333. unsigned int etuple;
  334. unsigned int ret;
  335. etuple = cpuid_eax(0x80000001);
  336. for (i=0xC0000; i < 0xffff0 ; i+=16) {
  337. p = phys_to_virt(i);
  338. if (memcmp(p, "AMDK7PNOW!", 10) == 0){
  339. dprintk ("Found PSB header at %p\n", p);
  340. psb = (struct psb_s *) p;
  341. dprintk ("Table version: 0x%x\n", psb->tableversion);
  342. if (psb->tableversion != 0x12) {
  343. printk (KERN_INFO PFX "Sorry, only v1.2 tables supported right now\n");
  344. return -ENODEV;
  345. }
  346. dprintk ("Flags: 0x%x\n", psb->flags);
  347. if ((psb->flags & 1)==0) {
  348. dprintk ("Mobile voltage regulator\n");
  349. } else {
  350. dprintk ("Desktop voltage regulator\n");
  351. }
  352. latency = psb->settlingtime;
  353. if (latency < 100) {
  354. printk (KERN_INFO PFX "BIOS set settling time to %d microseconds."
  355. "Should be at least 100. Correcting.\n", latency);
  356. latency = 100;
  357. }
  358. dprintk ("Settling Time: %d microseconds.\n", psb->settlingtime);
  359. dprintk ("Has %d PST tables. (Only dumping ones relevant to this CPU).\n", psb->numpst);
  360. p += sizeof (struct psb_s);
  361. pst = (struct pst_s *) p;
  362. for (i = 0 ; i <psb->numpst; i++) {
  363. pst = (struct pst_s *) p;
  364. number_scales = pst->numpstates;
  365. if ((etuple == pst->cpuid) && check_fsb(pst->fsbspeed) &&
  366. (maxfid==pst->maxfid) && (startvid==pst->startvid))
  367. {
  368. dprintk ("PST:%d (@%p)\n", i, pst);
  369. dprintk (" cpuid: 0x%x fsb: %d maxFID: 0x%x startvid: 0x%x\n",
  370. pst->cpuid, pst->fsbspeed, pst->maxfid, pst->startvid);
  371. ret = get_ranges ((char *) pst + sizeof (struct pst_s));
  372. return ret;
  373. } else {
  374. p = (char *) pst + sizeof (struct pst_s);
  375. for (j=0 ; j < number_scales; j++)
  376. p+=2;
  377. }
  378. }
  379. printk (KERN_INFO PFX "No PST tables match this cpuid (0x%x)\n", etuple);
  380. printk (KERN_INFO PFX "This is indicative of a broken BIOS.\n");
  381. return -EINVAL;
  382. }
  383. p++;
  384. }
  385. return -ENODEV;
  386. }
  387. static int powernow_target (struct cpufreq_policy *policy,
  388. unsigned int target_freq,
  389. unsigned int relation)
  390. {
  391. unsigned int newstate;
  392. if (cpufreq_frequency_table_target(policy, powernow_table, target_freq, relation, &newstate))
  393. return -EINVAL;
  394. change_speed(newstate);
  395. return 0;
  396. }
  397. static int powernow_verify (struct cpufreq_policy *policy)
  398. {
  399. return cpufreq_frequency_table_verify(policy, powernow_table);
  400. }
  401. /*
  402. * We use the fact that the bus frequency is somehow
  403. * a multiple of 100000/3 khz, then we compute sgtc according
  404. * to this multiple.
  405. * That way, we match more how AMD thinks all of that work.
  406. * We will then get the same kind of behaviour already tested under
  407. * the "well-known" other OS.
  408. */
  409. static int __init fixup_sgtc(void)
  410. {
  411. unsigned int sgtc;
  412. unsigned int m;
  413. m = fsb / 3333;
  414. if ((m % 10) >= 5)
  415. m += 5;
  416. m /= 10;
  417. sgtc = 100 * m * latency;
  418. sgtc = sgtc / 3;
  419. if (sgtc > 0xfffff) {
  420. printk(KERN_WARNING PFX "SGTC too large %d\n", sgtc);
  421. sgtc = 0xfffff;
  422. }
  423. return sgtc;
  424. }
  425. static unsigned int powernow_get(unsigned int cpu)
  426. {
  427. union msr_fidvidstatus fidvidstatus;
  428. unsigned int cfid;
  429. if (cpu)
  430. return 0;
  431. rdmsrl (MSR_K7_FID_VID_STATUS, fidvidstatus.val);
  432. cfid = fidvidstatus.bits.CFID;
  433. return (fsb * fid_codes[cfid] / 10);
  434. }
  435. static int __init acer_cpufreq_pst(struct dmi_system_id *d)
  436. {
  437. printk(KERN_WARNING "%s laptop with broken PST tables in BIOS detected.\n", d->ident);
  438. printk(KERN_WARNING "You need to downgrade to 3A21 (09/09/2002), or try a newer BIOS than 3A71 (01/20/2003)\n");
  439. printk(KERN_WARNING "cpufreq scaling has been disabled as a result of this.\n");
  440. return 0;
  441. }
  442. /*
  443. * Some Athlon laptops have really fucked PST tables.
  444. * A BIOS update is all that can save them.
  445. * Mention this, and disable cpufreq.
  446. */
  447. static struct dmi_system_id __initdata powernow_dmi_table[] = {
  448. {
  449. .callback = acer_cpufreq_pst,
  450. .ident = "Acer Aspire",
  451. .matches = {
  452. DMI_MATCH(DMI_SYS_VENDOR, "Insyde Software"),
  453. DMI_MATCH(DMI_BIOS_VERSION, "3A71"),
  454. },
  455. },
  456. { }
  457. };
  458. static int __init powernow_cpu_init (struct cpufreq_policy *policy)
  459. {
  460. union msr_fidvidstatus fidvidstatus;
  461. int result;
  462. if (policy->cpu != 0)
  463. return -ENODEV;
  464. rdmsrl (MSR_K7_FID_VID_STATUS, fidvidstatus.val);
  465. /* A K7 with powernow technology is set to max frequency by BIOS */
  466. fsb = (10 * cpu_khz) / fid_codes[fidvidstatus.bits.MFID];
  467. if (!fsb) {
  468. printk(KERN_WARNING PFX "can not determine bus frequency\n");
  469. return -EINVAL;
  470. }
  471. dprintk("FSB: %3d.%03d MHz\n", fsb/1000, fsb%1000);
  472. if (dmi_check_system(powernow_dmi_table) || acpi_force) {
  473. printk (KERN_INFO PFX "PSB/PST known to be broken. Trying ACPI instead\n");
  474. result = powernow_acpi_init();
  475. } else {
  476. result = powernow_decode_bios(fidvidstatus.bits.MFID, fidvidstatus.bits.SVID);
  477. if (result) {
  478. printk (KERN_INFO PFX "Trying ACPI perflib\n");
  479. maximum_speed = 0;
  480. minimum_speed = -1;
  481. latency = 0;
  482. result = powernow_acpi_init();
  483. if (result) {
  484. printk (KERN_INFO PFX "ACPI and legacy methods failed\n");
  485. printk (KERN_INFO PFX "See http://www.codemonkey.org.uk/projects/cpufreq/powernow-k7.shtml\n");
  486. }
  487. } else {
  488. /* SGTC use the bus clock as timer */
  489. latency = fixup_sgtc();
  490. printk(KERN_INFO PFX "SGTC: %d\n", latency);
  491. }
  492. }
  493. if (result)
  494. return result;
  495. printk (KERN_INFO PFX "Minimum speed %d MHz. Maximum speed %d MHz.\n",
  496. minimum_speed/1000, maximum_speed/1000);
  497. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  498. policy->cpuinfo.transition_latency = cpufreq_scale(2000000UL, fsb, latency);
  499. policy->cur = powernow_get(0);
  500. cpufreq_frequency_table_get_attr(powernow_table, policy->cpu);
  501. return cpufreq_frequency_table_cpuinfo(policy, powernow_table);
  502. }
  503. static int powernow_cpu_exit (struct cpufreq_policy *policy) {
  504. cpufreq_frequency_table_put_attr(policy->cpu);
  505. #ifdef CONFIG_X86_POWERNOW_K7_ACPI
  506. if (acpi_processor_perf) {
  507. acpi_processor_unregister_performance(acpi_processor_perf, 0);
  508. kfree(acpi_processor_perf);
  509. }
  510. #endif
  511. if (powernow_table)
  512. kfree(powernow_table);
  513. return 0;
  514. }
  515. static struct freq_attr* powernow_table_attr[] = {
  516. &cpufreq_freq_attr_scaling_available_freqs,
  517. NULL,
  518. };
  519. static struct cpufreq_driver powernow_driver = {
  520. .verify = powernow_verify,
  521. .target = powernow_target,
  522. .get = powernow_get,
  523. .init = powernow_cpu_init,
  524. .exit = powernow_cpu_exit,
  525. .name = "powernow-k7",
  526. .owner = THIS_MODULE,
  527. .attr = powernow_table_attr,
  528. };
  529. static int __init powernow_init (void)
  530. {
  531. if (check_powernow()==0)
  532. return -ENODEV;
  533. return cpufreq_register_driver(&powernow_driver);
  534. }
  535. static void __exit powernow_exit (void)
  536. {
  537. cpufreq_unregister_driver(&powernow_driver);
  538. }
  539. module_param(acpi_force, int, 0444);
  540. MODULE_PARM_DESC(acpi_force, "Force ACPI to be used.");
  541. MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>");
  542. MODULE_DESCRIPTION ("Powernow driver for AMD K7 processors.");
  543. MODULE_LICENSE ("GPL");
  544. late_initcall(powernow_init);
  545. module_exit(powernow_exit);