powernow-k6.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * This file was based upon code in Powertweak Linux (http://powertweak.sf.net)
  3. * (C) 2000-2003 Dave Jones, Arjan van de Ven, Janne Pänkälä,
  4. * Dominik Brodowski.
  5. *
  6. * Licensed under the terms of the GNU GPL License version 2.
  7. *
  8. * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/cpufreq.h>
  14. #include <linux/ioport.h>
  15. #include <linux/timex.h>
  16. #include <linux/io.h>
  17. #include <asm/cpu_device_id.h>
  18. #include <asm/msr.h>
  19. #define POWERNOW_IOPORT 0xfff0 /* it doesn't matter where, as long
  20. as it is unused */
  21. #define PFX "powernow-k6: "
  22. static unsigned int busfreq; /* FSB, in 10 kHz */
  23. static unsigned int max_multiplier;
  24. /* Clock ratio multiplied by 10 - see table 27 in AMD#23446 */
  25. static struct cpufreq_frequency_table clock_ratio[] = {
  26. {45, /* 000 -> 4.5x */ 0},
  27. {50, /* 001 -> 5.0x */ 0},
  28. {40, /* 010 -> 4.0x */ 0},
  29. {55, /* 011 -> 5.5x */ 0},
  30. {20, /* 100 -> 2.0x */ 0},
  31. {30, /* 101 -> 3.0x */ 0},
  32. {60, /* 110 -> 6.0x */ 0},
  33. {35, /* 111 -> 3.5x */ 0},
  34. {0, CPUFREQ_TABLE_END}
  35. };
  36. /**
  37. * powernow_k6_get_cpu_multiplier - returns the current FSB multiplier
  38. *
  39. * Returns the current setting of the frequency multiplier. Core clock
  40. * speed is frequency of the Front-Side Bus multiplied with this value.
  41. */
  42. static int powernow_k6_get_cpu_multiplier(void)
  43. {
  44. u64 invalue = 0;
  45. u32 msrval;
  46. msrval = POWERNOW_IOPORT + 0x1;
  47. wrmsr(MSR_K6_EPMR, msrval, 0); /* enable the PowerNow port */
  48. invalue = inl(POWERNOW_IOPORT + 0x8);
  49. msrval = POWERNOW_IOPORT + 0x0;
  50. wrmsr(MSR_K6_EPMR, msrval, 0); /* disable it again */
  51. return clock_ratio[(invalue >> 5)&7].driver_data;
  52. }
  53. /**
  54. * powernow_k6_set_state - set the PowerNow! multiplier
  55. * @best_i: clock_ratio[best_i] is the target multiplier
  56. *
  57. * Tries to change the PowerNow! multiplier
  58. */
  59. static void powernow_k6_set_state(struct cpufreq_policy *policy,
  60. unsigned int best_i)
  61. {
  62. unsigned long outvalue = 0, invalue = 0;
  63. unsigned long msrval;
  64. struct cpufreq_freqs freqs;
  65. if (clock_ratio[best_i].driver_data > max_multiplier) {
  66. printk(KERN_ERR PFX "invalid target frequency\n");
  67. return;
  68. }
  69. freqs.old = busfreq * powernow_k6_get_cpu_multiplier();
  70. freqs.new = busfreq * clock_ratio[best_i].driver_data;
  71. cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
  72. /* we now need to transform best_i to the BVC format, see AMD#23446 */
  73. outvalue = (1<<12) | (1<<10) | (1<<9) | (best_i<<5);
  74. msrval = POWERNOW_IOPORT + 0x1;
  75. wrmsr(MSR_K6_EPMR, msrval, 0); /* enable the PowerNow port */
  76. invalue = inl(POWERNOW_IOPORT + 0x8);
  77. invalue = invalue & 0xf;
  78. outvalue = outvalue | invalue;
  79. outl(outvalue , (POWERNOW_IOPORT + 0x8));
  80. msrval = POWERNOW_IOPORT + 0x0;
  81. wrmsr(MSR_K6_EPMR, msrval, 0); /* disable it again */
  82. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  83. return;
  84. }
  85. /**
  86. * powernow_k6_verify - verifies a new CPUfreq policy
  87. * @policy: new policy
  88. *
  89. * Policy must be within lowest and highest possible CPU Frequency,
  90. * and at least one possible state must be within min and max.
  91. */
  92. static int powernow_k6_verify(struct cpufreq_policy *policy)
  93. {
  94. return cpufreq_frequency_table_verify(policy, &clock_ratio[0]);
  95. }
  96. /**
  97. * powernow_k6_setpolicy - sets a new CPUFreq policy
  98. * @policy: new policy
  99. * @target_freq: the target frequency
  100. * @relation: how that frequency relates to achieved frequency
  101. * (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
  102. *
  103. * sets a new CPUFreq policy
  104. */
  105. static int powernow_k6_target(struct cpufreq_policy *policy,
  106. unsigned int target_freq,
  107. unsigned int relation)
  108. {
  109. unsigned int newstate = 0;
  110. if (cpufreq_frequency_table_target(policy, &clock_ratio[0],
  111. target_freq, relation, &newstate))
  112. return -EINVAL;
  113. powernow_k6_set_state(policy, newstate);
  114. return 0;
  115. }
  116. static int powernow_k6_cpu_init(struct cpufreq_policy *policy)
  117. {
  118. unsigned int i, f;
  119. int result;
  120. if (policy->cpu != 0)
  121. return -ENODEV;
  122. /* get frequencies */
  123. max_multiplier = powernow_k6_get_cpu_multiplier();
  124. busfreq = cpu_khz / max_multiplier;
  125. /* table init */
  126. for (i = 0; (clock_ratio[i].frequency != CPUFREQ_TABLE_END); i++) {
  127. f = clock_ratio[i].driver_data;
  128. if (f > max_multiplier)
  129. clock_ratio[i].frequency = CPUFREQ_ENTRY_INVALID;
  130. else
  131. clock_ratio[i].frequency = busfreq * f;
  132. }
  133. /* cpuinfo and default policy values */
  134. policy->cpuinfo.transition_latency = 200000;
  135. policy->cur = busfreq * max_multiplier;
  136. result = cpufreq_frequency_table_cpuinfo(policy, clock_ratio);
  137. if (result)
  138. return result;
  139. cpufreq_frequency_table_get_attr(clock_ratio, policy->cpu);
  140. return 0;
  141. }
  142. static int powernow_k6_cpu_exit(struct cpufreq_policy *policy)
  143. {
  144. unsigned int i;
  145. for (i = 0; i < 8; i++) {
  146. if (i == max_multiplier)
  147. powernow_k6_set_state(policy, i);
  148. }
  149. cpufreq_frequency_table_put_attr(policy->cpu);
  150. return 0;
  151. }
  152. static unsigned int powernow_k6_get(unsigned int cpu)
  153. {
  154. unsigned int ret;
  155. ret = (busfreq * powernow_k6_get_cpu_multiplier());
  156. return ret;
  157. }
  158. static struct freq_attr *powernow_k6_attr[] = {
  159. &cpufreq_freq_attr_scaling_available_freqs,
  160. NULL,
  161. };
  162. static struct cpufreq_driver powernow_k6_driver = {
  163. .verify = powernow_k6_verify,
  164. .target = powernow_k6_target,
  165. .init = powernow_k6_cpu_init,
  166. .exit = powernow_k6_cpu_exit,
  167. .get = powernow_k6_get,
  168. .name = "powernow-k6",
  169. .attr = powernow_k6_attr,
  170. };
  171. static const struct x86_cpu_id powernow_k6_ids[] = {
  172. { X86_VENDOR_AMD, 5, 12 },
  173. { X86_VENDOR_AMD, 5, 13 },
  174. {}
  175. };
  176. MODULE_DEVICE_TABLE(x86cpu, powernow_k6_ids);
  177. /**
  178. * powernow_k6_init - initializes the k6 PowerNow! CPUFreq driver
  179. *
  180. * Initializes the K6 PowerNow! support. Returns -ENODEV on unsupported
  181. * devices, -EINVAL or -ENOMEM on problems during initiatization, and zero
  182. * on success.
  183. */
  184. static int __init powernow_k6_init(void)
  185. {
  186. if (!x86_match_cpu(powernow_k6_ids))
  187. return -ENODEV;
  188. if (!request_region(POWERNOW_IOPORT, 16, "PowerNow!")) {
  189. printk(KERN_INFO PFX "PowerNow IOPORT region already used.\n");
  190. return -EIO;
  191. }
  192. if (cpufreq_register_driver(&powernow_k6_driver)) {
  193. release_region(POWERNOW_IOPORT, 16);
  194. return -EINVAL;
  195. }
  196. return 0;
  197. }
  198. /**
  199. * powernow_k6_exit - unregisters AMD K6-2+/3+ PowerNow! support
  200. *
  201. * Unregisters AMD K6-2+ / K6-3+ PowerNow! support.
  202. */
  203. static void __exit powernow_k6_exit(void)
  204. {
  205. cpufreq_unregister_driver(&powernow_k6_driver);
  206. release_region(POWERNOW_IOPORT, 16);
  207. }
  208. MODULE_AUTHOR("Arjan van de Ven, Dave Jones <davej@redhat.com>, "
  209. "Dominik Brodowski <linux@brodo.de>");
  210. MODULE_DESCRIPTION("PowerNow! driver for AMD K6-2+ / K6-3+ processors.");
  211. MODULE_LICENSE("GPL");
  212. module_init(powernow_k6_init);
  213. module_exit(powernow_k6_exit);