powernow-k6.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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_setpolicy - sets a new CPUFreq policy
  87. * @policy: new policy
  88. * @target_freq: the target frequency
  89. * @relation: how that frequency relates to achieved frequency
  90. * (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
  91. *
  92. * sets a new CPUFreq policy
  93. */
  94. static int powernow_k6_target(struct cpufreq_policy *policy,
  95. unsigned int target_freq,
  96. unsigned int relation)
  97. {
  98. unsigned int newstate = 0;
  99. if (cpufreq_frequency_table_target(policy, &clock_ratio[0],
  100. target_freq, relation, &newstate))
  101. return -EINVAL;
  102. powernow_k6_set_state(policy, newstate);
  103. return 0;
  104. }
  105. static int powernow_k6_cpu_init(struct cpufreq_policy *policy)
  106. {
  107. unsigned int i, f;
  108. if (policy->cpu != 0)
  109. return -ENODEV;
  110. /* get frequencies */
  111. max_multiplier = powernow_k6_get_cpu_multiplier();
  112. busfreq = cpu_khz / max_multiplier;
  113. /* table init */
  114. for (i = 0; (clock_ratio[i].frequency != CPUFREQ_TABLE_END); i++) {
  115. f = clock_ratio[i].driver_data;
  116. if (f > max_multiplier)
  117. clock_ratio[i].frequency = CPUFREQ_ENTRY_INVALID;
  118. else
  119. clock_ratio[i].frequency = busfreq * f;
  120. }
  121. /* cpuinfo and default policy values */
  122. policy->cpuinfo.transition_latency = 200000;
  123. return cpufreq_table_validate_and_show(policy, clock_ratio);
  124. }
  125. static int powernow_k6_cpu_exit(struct cpufreq_policy *policy)
  126. {
  127. unsigned int i;
  128. for (i = 0; i < 8; i++) {
  129. if (i == max_multiplier)
  130. powernow_k6_set_state(policy, i);
  131. }
  132. cpufreq_frequency_table_put_attr(policy->cpu);
  133. return 0;
  134. }
  135. static unsigned int powernow_k6_get(unsigned int cpu)
  136. {
  137. unsigned int ret;
  138. ret = (busfreq * powernow_k6_get_cpu_multiplier());
  139. return ret;
  140. }
  141. static struct cpufreq_driver powernow_k6_driver = {
  142. .verify = cpufreq_generic_frequency_table_verify,
  143. .target = powernow_k6_target,
  144. .init = powernow_k6_cpu_init,
  145. .exit = powernow_k6_cpu_exit,
  146. .get = powernow_k6_get,
  147. .name = "powernow-k6",
  148. .attr = cpufreq_generic_attr,
  149. };
  150. static const struct x86_cpu_id powernow_k6_ids[] = {
  151. { X86_VENDOR_AMD, 5, 12 },
  152. { X86_VENDOR_AMD, 5, 13 },
  153. {}
  154. };
  155. MODULE_DEVICE_TABLE(x86cpu, powernow_k6_ids);
  156. /**
  157. * powernow_k6_init - initializes the k6 PowerNow! CPUFreq driver
  158. *
  159. * Initializes the K6 PowerNow! support. Returns -ENODEV on unsupported
  160. * devices, -EINVAL or -ENOMEM on problems during initiatization, and zero
  161. * on success.
  162. */
  163. static int __init powernow_k6_init(void)
  164. {
  165. if (!x86_match_cpu(powernow_k6_ids))
  166. return -ENODEV;
  167. if (!request_region(POWERNOW_IOPORT, 16, "PowerNow!")) {
  168. printk(KERN_INFO PFX "PowerNow IOPORT region already used.\n");
  169. return -EIO;
  170. }
  171. if (cpufreq_register_driver(&powernow_k6_driver)) {
  172. release_region(POWERNOW_IOPORT, 16);
  173. return -EINVAL;
  174. }
  175. return 0;
  176. }
  177. /**
  178. * powernow_k6_exit - unregisters AMD K6-2+/3+ PowerNow! support
  179. *
  180. * Unregisters AMD K6-2+ / K6-3+ PowerNow! support.
  181. */
  182. static void __exit powernow_k6_exit(void)
  183. {
  184. cpufreq_unregister_driver(&powernow_k6_driver);
  185. release_region(POWERNOW_IOPORT, 16);
  186. }
  187. MODULE_AUTHOR("Arjan van de Ven, Dave Jones <davej@redhat.com>, "
  188. "Dominik Brodowski <linux@brodo.de>");
  189. MODULE_DESCRIPTION("PowerNow! driver for AMD K6-2+ / K6-3+ processors.");
  190. MODULE_LICENSE("GPL");
  191. module_init(powernow_k6_init);
  192. module_exit(powernow_k6_exit);