powernow-k6.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file was based upon code in Powertweak Linux (http://powertweak.sf.net)
  4. * (C) 2000-2003 Dave Jones, Arjan van de Ven, Janne Pänkälä,
  5. * Dominik Brodowski.
  6. *
  7. * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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. static unsigned int busfreq; /* FSB, in 10 kHz */
  22. static unsigned int max_multiplier;
  23. static unsigned int param_busfreq = 0;
  24. static unsigned int param_max_multiplier = 0;
  25. module_param_named(max_multiplier, param_max_multiplier, uint, S_IRUGO);
  26. MODULE_PARM_DESC(max_multiplier, "Maximum multiplier (allowed values: 20 30 35 40 45 50 55 60)");
  27. module_param_named(bus_frequency, param_busfreq, uint, S_IRUGO);
  28. MODULE_PARM_DESC(bus_frequency, "Bus frequency in kHz");
  29. /* Clock ratio multiplied by 10 - see table 27 in AMD#23446 */
  30. static struct cpufreq_frequency_table clock_ratio[] = {
  31. {0, 60, /* 110 -> 6.0x */ 0},
  32. {0, 55, /* 011 -> 5.5x */ 0},
  33. {0, 50, /* 001 -> 5.0x */ 0},
  34. {0, 45, /* 000 -> 4.5x */ 0},
  35. {0, 40, /* 010 -> 4.0x */ 0},
  36. {0, 35, /* 111 -> 3.5x */ 0},
  37. {0, 30, /* 101 -> 3.0x */ 0},
  38. {0, 20, /* 100 -> 2.0x */ 0},
  39. {0, 0, CPUFREQ_TABLE_END}
  40. };
  41. static const u8 index_to_register[8] = { 6, 3, 1, 0, 2, 7, 5, 4 };
  42. static const u8 register_to_index[8] = { 3, 2, 4, 1, 7, 6, 0, 5 };
  43. static const struct {
  44. unsigned freq;
  45. unsigned mult;
  46. } usual_frequency_table[] = {
  47. { 350000, 35 }, // 100 * 3.5
  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]].driver_data;
  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_target - 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 int powernow_k6_target(struct cpufreq_policy *policy,
  116. unsigned int best_i)
  117. {
  118. if (clock_ratio[best_i].driver_data > max_multiplier) {
  119. pr_err("invalid target frequency\n");
  120. return -EINVAL;
  121. }
  122. powernow_k6_set_cpu_multiplier(best_i);
  123. return 0;
  124. }
  125. static int powernow_k6_cpu_init(struct cpufreq_policy *policy)
  126. {
  127. struct cpufreq_frequency_table *pos;
  128. unsigned int i, f;
  129. unsigned khz;
  130. if (policy->cpu != 0)
  131. return -ENODEV;
  132. max_multiplier = 0;
  133. khz = cpu_khz;
  134. for (i = 0; i < ARRAY_SIZE(usual_frequency_table); i++) {
  135. if (khz >= usual_frequency_table[i].freq - FREQ_RANGE &&
  136. khz <= usual_frequency_table[i].freq + FREQ_RANGE) {
  137. khz = usual_frequency_table[i].freq;
  138. max_multiplier = usual_frequency_table[i].mult;
  139. break;
  140. }
  141. }
  142. if (param_max_multiplier) {
  143. cpufreq_for_each_entry(pos, clock_ratio)
  144. if (pos->driver_data == param_max_multiplier) {
  145. max_multiplier = param_max_multiplier;
  146. goto have_max_multiplier;
  147. }
  148. pr_err("invalid max_multiplier parameter, valid parameters 20, 30, 35, 40, 45, 50, 55, 60\n");
  149. return -EINVAL;
  150. }
  151. if (!max_multiplier) {
  152. pr_warn("unknown frequency %u, cannot determine current multiplier\n",
  153. khz);
  154. pr_warn("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. pr_err("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. policy->freq_table = clock_ratio;
  181. return 0;
  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_MATCH_VENDOR_FAM_MODEL(AMD, 5, 12, NULL),
  217. X86_MATCH_VENDOR_FAM_MODEL(AMD, 5, 13, NULL),
  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);