powernow-k6.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/cpufreq.h>
  15. #include <linux/ioport.h>
  16. #include <linux/timex.h>
  17. #include <linux/io.h>
  18. #include <asm/cpu_device_id.h>
  19. #include <asm/msr.h>
  20. #define POWERNOW_IOPORT 0xfff0 /* it doesn't matter where, as long
  21. as it is unused */
  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. pr_err("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. pr_err("invalid max_multiplier parameter, valid parameters 20, 30, 35, 40, 45, 50, 55, 60\n");
  150. return -EINVAL;
  151. }
  152. if (!max_multiplier) {
  153. pr_warn("unknown frequency %u, cannot determine current multiplier\n",
  154. khz);
  155. pr_warn("use module parameters max_multiplier and bus_frequency\n");
  156. return -EOPNOTSUPP;
  157. }
  158. have_max_multiplier:
  159. param_max_multiplier = max_multiplier;
  160. if (param_busfreq) {
  161. if (param_busfreq >= 50000 && param_busfreq <= 150000) {
  162. busfreq = param_busfreq / 10;
  163. goto have_busfreq;
  164. }
  165. pr_err("invalid bus_frequency parameter, allowed range 50000 - 150000 kHz\n");
  166. return -EINVAL;
  167. }
  168. busfreq = khz / max_multiplier;
  169. have_busfreq:
  170. param_busfreq = busfreq * 10;
  171. /* table init */
  172. cpufreq_for_each_entry(pos, clock_ratio) {
  173. f = pos->driver_data;
  174. if (f > max_multiplier)
  175. pos->frequency = CPUFREQ_ENTRY_INVALID;
  176. else
  177. pos->frequency = busfreq * f;
  178. }
  179. /* cpuinfo and default policy values */
  180. policy->cpuinfo.transition_latency = 500000;
  181. return cpufreq_table_validate_and_show(policy, clock_ratio);
  182. }
  183. static int powernow_k6_cpu_exit(struct cpufreq_policy *policy)
  184. {
  185. unsigned int i;
  186. for (i = 0; (clock_ratio[i].frequency != CPUFREQ_TABLE_END); i++) {
  187. if (clock_ratio[i].driver_data == max_multiplier) {
  188. struct cpufreq_freqs freqs;
  189. freqs.old = policy->cur;
  190. freqs.new = clock_ratio[i].frequency;
  191. freqs.flags = 0;
  192. cpufreq_freq_transition_begin(policy, &freqs);
  193. powernow_k6_target(policy, i);
  194. cpufreq_freq_transition_end(policy, &freqs, 0);
  195. break;
  196. }
  197. }
  198. return 0;
  199. }
  200. static unsigned int powernow_k6_get(unsigned int cpu)
  201. {
  202. unsigned int ret;
  203. ret = (busfreq * powernow_k6_get_cpu_multiplier());
  204. return ret;
  205. }
  206. static struct cpufreq_driver powernow_k6_driver = {
  207. .verify = cpufreq_generic_frequency_table_verify,
  208. .target_index = powernow_k6_target,
  209. .init = powernow_k6_cpu_init,
  210. .exit = powernow_k6_cpu_exit,
  211. .get = powernow_k6_get,
  212. .name = "powernow-k6",
  213. .attr = cpufreq_generic_attr,
  214. };
  215. static const struct x86_cpu_id powernow_k6_ids[] = {
  216. { X86_VENDOR_AMD, 5, 12 },
  217. { X86_VENDOR_AMD, 5, 13 },
  218. {}
  219. };
  220. MODULE_DEVICE_TABLE(x86cpu, powernow_k6_ids);
  221. /**
  222. * powernow_k6_init - initializes the k6 PowerNow! CPUFreq driver
  223. *
  224. * Initializes the K6 PowerNow! support. Returns -ENODEV on unsupported
  225. * devices, -EINVAL or -ENOMEM on problems during initiatization, and zero
  226. * on success.
  227. */
  228. static int __init powernow_k6_init(void)
  229. {
  230. if (!x86_match_cpu(powernow_k6_ids))
  231. return -ENODEV;
  232. if (!request_region(POWERNOW_IOPORT, 16, "PowerNow!")) {
  233. pr_info("PowerNow IOPORT region already used\n");
  234. return -EIO;
  235. }
  236. if (cpufreq_register_driver(&powernow_k6_driver)) {
  237. release_region(POWERNOW_IOPORT, 16);
  238. return -EINVAL;
  239. }
  240. return 0;
  241. }
  242. /**
  243. * powernow_k6_exit - unregisters AMD K6-2+/3+ PowerNow! support
  244. *
  245. * Unregisters AMD K6-2+ / K6-3+ PowerNow! support.
  246. */
  247. static void __exit powernow_k6_exit(void)
  248. {
  249. cpufreq_unregister_driver(&powernow_k6_driver);
  250. release_region(POWERNOW_IOPORT, 16);
  251. }
  252. MODULE_AUTHOR("Arjan van de Ven, Dave Jones, "
  253. "Dominik Brodowski <linux@brodo.de>");
  254. MODULE_DESCRIPTION("PowerNow! driver for AMD K6-2+ / K6-3+ processors.");
  255. MODULE_LICENSE("GPL");
  256. module_init(powernow_k6_init);
  257. module_exit(powernow_k6_exit);