powernow-k6.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. {0, 60, /* 110 -> 6.0x */ 0},
  33. {0, 55, /* 011 -> 5.5x */ 0},
  34. {0, 50, /* 001 -> 5.0x */ 0},
  35. {0, 45, /* 000 -> 4.5x */ 0},
  36. {0, 40, /* 010 -> 4.0x */ 0},
  37. {0, 35, /* 111 -> 3.5x */ 0},
  38. {0, 30, /* 101 -> 3.0x */ 0},
  39. {0, 20, /* 100 -> 2.0x */ 0},
  40. {0, 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. { 350000, 35 }, // 100 * 3.5
  49. { 400000, 40 }, // 100 * 4
  50. { 450000, 45 }, // 100 * 4.5
  51. { 475000, 50 }, // 95 * 5
  52. { 500000, 50 }, // 100 * 5
  53. { 506250, 45 }, // 112.5 * 4.5
  54. { 533500, 55 }, // 97 * 5.5
  55. { 550000, 55 }, // 100 * 5.5
  56. { 562500, 50 }, // 112.5 * 5
  57. { 570000, 60 }, // 95 * 6
  58. { 600000, 60 }, // 100 * 6
  59. { 618750, 55 }, // 112.5 * 5.5
  60. { 660000, 55 }, // 120 * 5.5
  61. { 675000, 60 }, // 112.5 * 6
  62. { 720000, 60 }, // 120 * 6
  63. };
  64. #define FREQ_RANGE 3000
  65. /**
  66. * powernow_k6_get_cpu_multiplier - returns the current FSB multiplier
  67. *
  68. * Returns the current setting of the frequency multiplier. Core clock
  69. * speed is frequency of the Front-Side Bus multiplied with this value.
  70. */
  71. static int powernow_k6_get_cpu_multiplier(void)
  72. {
  73. unsigned long invalue = 0;
  74. u32 msrval;
  75. local_irq_disable();
  76. msrval = POWERNOW_IOPORT + 0x1;
  77. wrmsr(MSR_K6_EPMR, msrval, 0); /* enable the PowerNow port */
  78. invalue = inl(POWERNOW_IOPORT + 0x8);
  79. msrval = POWERNOW_IOPORT + 0x0;
  80. wrmsr(MSR_K6_EPMR, msrval, 0); /* disable it again */
  81. local_irq_enable();
  82. return clock_ratio[register_to_index[(invalue >> 5)&7]].driver_data;
  83. }
  84. static void powernow_k6_set_cpu_multiplier(unsigned int best_i)
  85. {
  86. unsigned long outvalue, invalue;
  87. unsigned long msrval;
  88. unsigned long cr0;
  89. /* we now need to transform best_i to the BVC format, see AMD#23446 */
  90. /*
  91. * The processor doesn't respond to inquiry cycles while changing the
  92. * frequency, so we must disable cache.
  93. */
  94. local_irq_disable();
  95. cr0 = read_cr0();
  96. write_cr0(cr0 | X86_CR0_CD);
  97. wbinvd();
  98. outvalue = (1<<12) | (1<<10) | (1<<9) | (index_to_register[best_i]<<5);
  99. msrval = POWERNOW_IOPORT + 0x1;
  100. wrmsr(MSR_K6_EPMR, msrval, 0); /* enable the PowerNow port */
  101. invalue = inl(POWERNOW_IOPORT + 0x8);
  102. invalue = invalue & 0x1f;
  103. outvalue = outvalue | invalue;
  104. outl(outvalue, (POWERNOW_IOPORT + 0x8));
  105. msrval = POWERNOW_IOPORT + 0x0;
  106. wrmsr(MSR_K6_EPMR, msrval, 0); /* disable it again */
  107. write_cr0(cr0);
  108. local_irq_enable();
  109. }
  110. /**
  111. * powernow_k6_target - set the PowerNow! multiplier
  112. * @best_i: clock_ratio[best_i] is the target multiplier
  113. *
  114. * Tries to change the PowerNow! multiplier
  115. */
  116. static int powernow_k6_target(struct cpufreq_policy *policy,
  117. unsigned int best_i)
  118. {
  119. if (clock_ratio[best_i].driver_data > max_multiplier) {
  120. printk(KERN_ERR PFX "invalid target frequency\n");
  121. return -EINVAL;
  122. }
  123. powernow_k6_set_cpu_multiplier(best_i);
  124. return 0;
  125. }
  126. static int powernow_k6_cpu_init(struct cpufreq_policy *policy)
  127. {
  128. struct cpufreq_frequency_table *pos;
  129. unsigned int i, f;
  130. unsigned khz;
  131. if (policy->cpu != 0)
  132. return -ENODEV;
  133. max_multiplier = 0;
  134. khz = cpu_khz;
  135. for (i = 0; i < ARRAY_SIZE(usual_frequency_table); i++) {
  136. if (khz >= usual_frequency_table[i].freq - FREQ_RANGE &&
  137. khz <= usual_frequency_table[i].freq + FREQ_RANGE) {
  138. khz = usual_frequency_table[i].freq;
  139. max_multiplier = usual_frequency_table[i].mult;
  140. break;
  141. }
  142. }
  143. if (param_max_multiplier) {
  144. cpufreq_for_each_entry(pos, clock_ratio)
  145. if (pos->driver_data == param_max_multiplier) {
  146. max_multiplier = param_max_multiplier;
  147. goto have_max_multiplier;
  148. }
  149. printk(KERN_ERR "powernow-k6: invalid max_multiplier parameter, valid parameters 20, 30, 35, 40, 45, 50, 55, 60\n");
  150. return -EINVAL;
  151. }
  152. if (!max_multiplier) {
  153. printk(KERN_WARNING "powernow-k6: unknown frequency %u, cannot determine current multiplier\n", khz);
  154. printk(KERN_WARNING "powernow-k6: use module parameters max_multiplier and bus_frequency\n");
  155. return -EOPNOTSUPP;
  156. }
  157. have_max_multiplier:
  158. param_max_multiplier = max_multiplier;
  159. if (param_busfreq) {
  160. if (param_busfreq >= 50000 && param_busfreq <= 150000) {
  161. busfreq = param_busfreq / 10;
  162. goto have_busfreq;
  163. }
  164. printk(KERN_ERR "powernow-k6: invalid bus_frequency parameter, allowed range 50000 - 150000 kHz\n");
  165. return -EINVAL;
  166. }
  167. busfreq = khz / max_multiplier;
  168. have_busfreq:
  169. param_busfreq = busfreq * 10;
  170. /* table init */
  171. cpufreq_for_each_entry(pos, clock_ratio) {
  172. f = pos->driver_data;
  173. if (f > max_multiplier)
  174. pos->frequency = CPUFREQ_ENTRY_INVALID;
  175. else
  176. pos->frequency = busfreq * f;
  177. }
  178. /* cpuinfo and default policy values */
  179. policy->cpuinfo.transition_latency = 500000;
  180. return cpufreq_table_validate_and_show(policy, clock_ratio);
  181. }
  182. static int powernow_k6_cpu_exit(struct cpufreq_policy *policy)
  183. {
  184. unsigned int i;
  185. for (i = 0; (clock_ratio[i].frequency != CPUFREQ_TABLE_END); i++) {
  186. if (clock_ratio[i].driver_data == max_multiplier) {
  187. struct cpufreq_freqs freqs;
  188. freqs.old = policy->cur;
  189. freqs.new = clock_ratio[i].frequency;
  190. freqs.flags = 0;
  191. cpufreq_freq_transition_begin(policy, &freqs);
  192. powernow_k6_target(policy, i);
  193. cpufreq_freq_transition_end(policy, &freqs, 0);
  194. break;
  195. }
  196. }
  197. return 0;
  198. }
  199. static unsigned int powernow_k6_get(unsigned int cpu)
  200. {
  201. unsigned int ret;
  202. ret = (busfreq * powernow_k6_get_cpu_multiplier());
  203. return ret;
  204. }
  205. static struct cpufreq_driver powernow_k6_driver = {
  206. .verify = cpufreq_generic_frequency_table_verify,
  207. .target_index = powernow_k6_target,
  208. .init = powernow_k6_cpu_init,
  209. .exit = powernow_k6_cpu_exit,
  210. .get = powernow_k6_get,
  211. .name = "powernow-k6",
  212. .attr = cpufreq_generic_attr,
  213. };
  214. static const struct x86_cpu_id powernow_k6_ids[] = {
  215. { X86_VENDOR_AMD, 5, 12 },
  216. { X86_VENDOR_AMD, 5, 13 },
  217. {}
  218. };
  219. MODULE_DEVICE_TABLE(x86cpu, powernow_k6_ids);
  220. /**
  221. * powernow_k6_init - initializes the k6 PowerNow! CPUFreq driver
  222. *
  223. * Initializes the K6 PowerNow! support. Returns -ENODEV on unsupported
  224. * devices, -EINVAL or -ENOMEM on problems during initiatization, and zero
  225. * on success.
  226. */
  227. static int __init powernow_k6_init(void)
  228. {
  229. if (!x86_match_cpu(powernow_k6_ids))
  230. return -ENODEV;
  231. if (!request_region(POWERNOW_IOPORT, 16, "PowerNow!")) {
  232. printk(KERN_INFO PFX "PowerNow IOPORT region already used.\n");
  233. return -EIO;
  234. }
  235. if (cpufreq_register_driver(&powernow_k6_driver)) {
  236. release_region(POWERNOW_IOPORT, 16);
  237. return -EINVAL;
  238. }
  239. return 0;
  240. }
  241. /**
  242. * powernow_k6_exit - unregisters AMD K6-2+/3+ PowerNow! support
  243. *
  244. * Unregisters AMD K6-2+ / K6-3+ PowerNow! support.
  245. */
  246. static void __exit powernow_k6_exit(void)
  247. {
  248. cpufreq_unregister_driver(&powernow_k6_driver);
  249. release_region(POWERNOW_IOPORT, 16);
  250. }
  251. MODULE_AUTHOR("Arjan van de Ven, Dave Jones, "
  252. "Dominik Brodowski <linux@brodo.de>");
  253. MODULE_DESCRIPTION("PowerNow! driver for AMD K6-2+ / K6-3+ processors.");
  254. MODULE_LICENSE("GPL");
  255. module_init(powernow_k6_init);
  256. module_exit(powernow_k6_exit);