powernow-k6.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. static unsigned int param_busfreq = 0;
  25. static unsigned int param_max_multiplier = 0;
  26. module_param_named(max_multiplier, param_max_multiplier, uint, S_IRUGO);
  27. MODULE_PARM_DESC(max_multiplier, "Maximum multiplier (allowed values: 20 30 35 40 45 50 55 60)");
  28. module_param_named(bus_frequency, param_busfreq, uint, S_IRUGO);
  29. MODULE_PARM_DESC(bus_frequency, "Bus frequency in kHz");
  30. /* Clock ratio multiplied by 10 - see table 27 in AMD#23446 */
  31. static struct cpufreq_frequency_table clock_ratio[] = {
  32. {60, /* 110 -> 6.0x */ 0},
  33. {55, /* 011 -> 5.5x */ 0},
  34. {50, /* 001 -> 5.0x */ 0},
  35. {45, /* 000 -> 4.5x */ 0},
  36. {40, /* 010 -> 4.0x */ 0},
  37. {35, /* 111 -> 3.5x */ 0},
  38. {30, /* 101 -> 3.0x */ 0},
  39. {20, /* 100 -> 2.0x */ 0},
  40. {0, CPUFREQ_TABLE_END}
  41. };
  42. static const u8 index_to_register[8] = { 6, 3, 1, 0, 2, 7, 5, 4 };
  43. static const u8 register_to_index[8] = { 3, 2, 4, 1, 7, 6, 0, 5 };
  44. static const struct {
  45. unsigned freq;
  46. unsigned mult;
  47. } usual_frequency_table[] = {
  48. { 400000, 40 }, // 100 * 4
  49. { 450000, 45 }, // 100 * 4.5
  50. { 475000, 50 }, // 95 * 5
  51. { 500000, 50 }, // 100 * 5
  52. { 506250, 45 }, // 112.5 * 4.5
  53. { 533500, 55 }, // 97 * 5.5
  54. { 550000, 55 }, // 100 * 5.5
  55. { 562500, 50 }, // 112.5 * 5
  56. { 570000, 60 }, // 95 * 6
  57. { 600000, 60 }, // 100 * 6
  58. { 618750, 55 }, // 112.5 * 5.5
  59. { 660000, 55 }, // 120 * 5.5
  60. { 675000, 60 }, // 112.5 * 6
  61. { 720000, 60 }, // 120 * 6
  62. };
  63. #define FREQ_RANGE 3000
  64. /**
  65. * powernow_k6_get_cpu_multiplier - returns the current FSB multiplier
  66. *
  67. * Returns the current setting of the frequency multiplier. Core clock
  68. * speed is frequency of the Front-Side Bus multiplied with this value.
  69. */
  70. static int powernow_k6_get_cpu_multiplier(void)
  71. {
  72. unsigned long invalue = 0;
  73. u32 msrval;
  74. local_irq_disable();
  75. msrval = POWERNOW_IOPORT + 0x1;
  76. wrmsr(MSR_K6_EPMR, msrval, 0); /* enable the PowerNow port */
  77. invalue = inl(POWERNOW_IOPORT + 0x8);
  78. msrval = POWERNOW_IOPORT + 0x0;
  79. wrmsr(MSR_K6_EPMR, msrval, 0); /* disable it again */
  80. local_irq_enable();
  81. return clock_ratio[register_to_index[(invalue >> 5)&7]].index;
  82. }
  83. static void powernow_k6_set_cpu_multiplier(unsigned int best_i)
  84. {
  85. unsigned long outvalue, invalue;
  86. unsigned long msrval;
  87. unsigned long cr0;
  88. /* we now need to transform best_i to the BVC format, see AMD#23446 */
  89. /*
  90. * The processor doesn't respond to inquiry cycles while changing the
  91. * frequency, so we must disable cache.
  92. */
  93. local_irq_disable();
  94. cr0 = read_cr0();
  95. write_cr0(cr0 | X86_CR0_CD);
  96. wbinvd();
  97. outvalue = (1<<12) | (1<<10) | (1<<9) | (index_to_register[best_i]<<5);
  98. msrval = POWERNOW_IOPORT + 0x1;
  99. wrmsr(MSR_K6_EPMR, msrval, 0); /* enable the PowerNow port */
  100. invalue = inl(POWERNOW_IOPORT + 0x8);
  101. invalue = invalue & 0x1f;
  102. outvalue = outvalue | invalue;
  103. outl(outvalue, (POWERNOW_IOPORT + 0x8));
  104. msrval = POWERNOW_IOPORT + 0x0;
  105. wrmsr(MSR_K6_EPMR, msrval, 0); /* disable it again */
  106. write_cr0(cr0);
  107. local_irq_enable();
  108. }
  109. /**
  110. * powernow_k6_set_state - set the PowerNow! multiplier
  111. * @best_i: clock_ratio[best_i] is the target multiplier
  112. *
  113. * Tries to change the PowerNow! multiplier
  114. */
  115. static void powernow_k6_set_state(unsigned int best_i)
  116. {
  117. struct cpufreq_freqs freqs;
  118. if (clock_ratio[best_i].index > max_multiplier) {
  119. printk(KERN_ERR PFX "invalid target frequency\n");
  120. return;
  121. }
  122. freqs.old = busfreq * powernow_k6_get_cpu_multiplier();
  123. freqs.new = busfreq * clock_ratio[best_i].index;
  124. freqs.cpu = 0; /* powernow-k6.c is UP only driver */
  125. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  126. powernow_k6_set_cpu_multiplier(best_i);
  127. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  128. return;
  129. }
  130. /**
  131. * powernow_k6_verify - verifies a new CPUfreq policy
  132. * @policy: new policy
  133. *
  134. * Policy must be within lowest and highest possible CPU Frequency,
  135. * and at least one possible state must be within min and max.
  136. */
  137. static int powernow_k6_verify(struct cpufreq_policy *policy)
  138. {
  139. return cpufreq_frequency_table_verify(policy, &clock_ratio[0]);
  140. }
  141. /**
  142. * powernow_k6_setpolicy - sets a new CPUFreq policy
  143. * @policy: new policy
  144. * @target_freq: the target frequency
  145. * @relation: how that frequency relates to achieved frequency
  146. * (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
  147. *
  148. * sets a new CPUFreq policy
  149. */
  150. static int powernow_k6_target(struct cpufreq_policy *policy,
  151. unsigned int target_freq,
  152. unsigned int relation)
  153. {
  154. unsigned int newstate = 0;
  155. if (cpufreq_frequency_table_target(policy, &clock_ratio[0],
  156. target_freq, relation, &newstate))
  157. return -EINVAL;
  158. powernow_k6_set_state(newstate);
  159. return 0;
  160. }
  161. static int powernow_k6_cpu_init(struct cpufreq_policy *policy)
  162. {
  163. unsigned int i, f;
  164. int result;
  165. unsigned khz;
  166. if (policy->cpu != 0)
  167. return -ENODEV;
  168. max_multiplier = 0;
  169. khz = cpu_khz;
  170. for (i = 0; i < ARRAY_SIZE(usual_frequency_table); i++) {
  171. if (khz >= usual_frequency_table[i].freq - FREQ_RANGE &&
  172. khz <= usual_frequency_table[i].freq + FREQ_RANGE) {
  173. khz = usual_frequency_table[i].freq;
  174. max_multiplier = usual_frequency_table[i].mult;
  175. break;
  176. }
  177. }
  178. if (param_max_multiplier) {
  179. for (i = 0; (clock_ratio[i].frequency != CPUFREQ_TABLE_END); i++) {
  180. if (clock_ratio[i].index == param_max_multiplier) {
  181. max_multiplier = param_max_multiplier;
  182. goto have_max_multiplier;
  183. }
  184. }
  185. printk(KERN_ERR "powernow-k6: invalid max_multiplier parameter, valid parameters 20, 30, 35, 40, 45, 50, 55, 60\n");
  186. return -EINVAL;
  187. }
  188. if (!max_multiplier) {
  189. printk(KERN_WARNING "powernow-k6: unknown frequency %u, cannot determine current multiplier\n", khz);
  190. printk(KERN_WARNING "powernow-k6: use module parameters max_multiplier and bus_frequency\n");
  191. return -EOPNOTSUPP;
  192. }
  193. have_max_multiplier:
  194. param_max_multiplier = max_multiplier;
  195. if (param_busfreq) {
  196. if (param_busfreq >= 50000 && param_busfreq <= 150000) {
  197. busfreq = param_busfreq / 10;
  198. goto have_busfreq;
  199. }
  200. printk(KERN_ERR "powernow-k6: invalid bus_frequency parameter, allowed range 50000 - 150000 kHz\n");
  201. return -EINVAL;
  202. }
  203. busfreq = khz / max_multiplier;
  204. have_busfreq:
  205. param_busfreq = busfreq * 10;
  206. /* table init */
  207. for (i = 0; (clock_ratio[i].frequency != CPUFREQ_TABLE_END); i++) {
  208. f = clock_ratio[i].index;
  209. if (f > max_multiplier)
  210. clock_ratio[i].frequency = CPUFREQ_ENTRY_INVALID;
  211. else
  212. clock_ratio[i].frequency = busfreq * f;
  213. }
  214. /* cpuinfo and default policy values */
  215. policy->cpuinfo.transition_latency = 500000;
  216. policy->cur = busfreq * max_multiplier;
  217. result = cpufreq_frequency_table_cpuinfo(policy, clock_ratio);
  218. if (result)
  219. return result;
  220. cpufreq_frequency_table_get_attr(clock_ratio, policy->cpu);
  221. return 0;
  222. }
  223. static int powernow_k6_cpu_exit(struct cpufreq_policy *policy)
  224. {
  225. unsigned int i;
  226. for (i = 0; i < 8; i++) {
  227. if (i == max_multiplier)
  228. powernow_k6_set_state(i);
  229. }
  230. cpufreq_frequency_table_put_attr(policy->cpu);
  231. return 0;
  232. }
  233. static unsigned int powernow_k6_get(unsigned int cpu)
  234. {
  235. unsigned int ret;
  236. ret = (busfreq * powernow_k6_get_cpu_multiplier());
  237. return ret;
  238. }
  239. static struct freq_attr *powernow_k6_attr[] = {
  240. &cpufreq_freq_attr_scaling_available_freqs,
  241. NULL,
  242. };
  243. static struct cpufreq_driver powernow_k6_driver = {
  244. .verify = powernow_k6_verify,
  245. .target = powernow_k6_target,
  246. .init = powernow_k6_cpu_init,
  247. .exit = powernow_k6_cpu_exit,
  248. .get = powernow_k6_get,
  249. .name = "powernow-k6",
  250. .owner = THIS_MODULE,
  251. .attr = powernow_k6_attr,
  252. };
  253. static const struct x86_cpu_id powernow_k6_ids[] = {
  254. { X86_VENDOR_AMD, 5, 12 },
  255. { X86_VENDOR_AMD, 5, 13 },
  256. {}
  257. };
  258. MODULE_DEVICE_TABLE(x86cpu, powernow_k6_ids);
  259. /**
  260. * powernow_k6_init - initializes the k6 PowerNow! CPUFreq driver
  261. *
  262. * Initializes the K6 PowerNow! support. Returns -ENODEV on unsupported
  263. * devices, -EINVAL or -ENOMEM on problems during initiatization, and zero
  264. * on success.
  265. */
  266. static int __init powernow_k6_init(void)
  267. {
  268. if (!x86_match_cpu(powernow_k6_ids))
  269. return -ENODEV;
  270. if (!request_region(POWERNOW_IOPORT, 16, "PowerNow!")) {
  271. printk(KERN_INFO PFX "PowerNow IOPORT region already used.\n");
  272. return -EIO;
  273. }
  274. if (cpufreq_register_driver(&powernow_k6_driver)) {
  275. release_region(POWERNOW_IOPORT, 16);
  276. return -EINVAL;
  277. }
  278. return 0;
  279. }
  280. /**
  281. * powernow_k6_exit - unregisters AMD K6-2+/3+ PowerNow! support
  282. *
  283. * Unregisters AMD K6-2+ / K6-3+ PowerNow! support.
  284. */
  285. static void __exit powernow_k6_exit(void)
  286. {
  287. cpufreq_unregister_driver(&powernow_k6_driver);
  288. release_region(POWERNOW_IOPORT, 16);
  289. }
  290. MODULE_AUTHOR("Arjan van de Ven, Dave Jones <davej@redhat.com>, "
  291. "Dominik Brodowski <linux@brodo.de>");
  292. MODULE_DESCRIPTION("PowerNow! driver for AMD K6-2+ / K6-3+ processors.");
  293. MODULE_LICENSE("GPL");
  294. module_init(powernow_k6_init);
  295. module_exit(powernow_k6_exit);