powernow-k6.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/slab.h>
  16. #include <linux/timex.h>
  17. #include <linux/io.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].index;
  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(unsigned int best_i)
  60. {
  61. unsigned long outvalue = 0, invalue = 0;
  62. unsigned long msrval;
  63. struct cpufreq_freqs freqs;
  64. if (clock_ratio[best_i].index > max_multiplier) {
  65. printk(KERN_ERR PFX "invalid target frequency\n");
  66. return;
  67. }
  68. freqs.old = busfreq * powernow_k6_get_cpu_multiplier();
  69. freqs.new = busfreq * clock_ratio[best_i].index;
  70. freqs.cpu = 0; /* powernow-k6.c is UP only driver */
  71. cpufreq_notify_transition(&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(&freqs, CPUFREQ_POSTCHANGE);
  83. return;
  84. }
  85. /**
  86. * powernow_k6_verify - verifies a new CPUfreq policy
  87. * @policy: new policy
  88. *
  89. * Policy must be within lowest and highest possible CPU Frequency,
  90. * and at least one possible state must be within min and max.
  91. */
  92. static int powernow_k6_verify(struct cpufreq_policy *policy)
  93. {
  94. return cpufreq_frequency_table_verify(policy, &clock_ratio[0]);
  95. }
  96. /**
  97. * powernow_k6_setpolicy - sets a new CPUFreq policy
  98. * @policy: new policy
  99. * @target_freq: the target frequency
  100. * @relation: how that frequency relates to achieved frequency
  101. * (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
  102. *
  103. * sets a new CPUFreq policy
  104. */
  105. static int powernow_k6_target(struct cpufreq_policy *policy,
  106. unsigned int target_freq,
  107. unsigned int relation)
  108. {
  109. unsigned int newstate = 0;
  110. if (cpufreq_frequency_table_target(policy, &clock_ratio[0],
  111. target_freq, relation, &newstate))
  112. return -EINVAL;
  113. powernow_k6_set_state(newstate);
  114. return 0;
  115. }
  116. static int powernow_k6_cpu_init(struct cpufreq_policy *policy)
  117. {
  118. unsigned int i, f;
  119. int result;
  120. if (policy->cpu != 0)
  121. return -ENODEV;
  122. /* get frequencies */
  123. max_multiplier = powernow_k6_get_cpu_multiplier();
  124. busfreq = cpu_khz / max_multiplier;
  125. /* table init */
  126. for (i = 0; (clock_ratio[i].frequency != CPUFREQ_TABLE_END); i++) {
  127. f = clock_ratio[i].index;
  128. if (f > max_multiplier)
  129. clock_ratio[i].frequency = CPUFREQ_ENTRY_INVALID;
  130. else
  131. clock_ratio[i].frequency = busfreq * f;
  132. }
  133. /* cpuinfo and default policy values */
  134. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  135. policy->cur = busfreq * max_multiplier;
  136. result = cpufreq_frequency_table_cpuinfo(policy, clock_ratio);
  137. if (result)
  138. return result;
  139. cpufreq_frequency_table_get_attr(clock_ratio, policy->cpu);
  140. return 0;
  141. }
  142. static int powernow_k6_cpu_exit(struct cpufreq_policy *policy)
  143. {
  144. unsigned int i;
  145. for (i = 0; i < 8; i++) {
  146. if (i == max_multiplier)
  147. powernow_k6_set_state(i);
  148. }
  149. cpufreq_frequency_table_put_attr(policy->cpu);
  150. return 0;
  151. }
  152. static unsigned int powernow_k6_get(unsigned int cpu)
  153. {
  154. unsigned int ret;
  155. ret = (busfreq * powernow_k6_get_cpu_multiplier());
  156. return ret;
  157. }
  158. static struct freq_attr *powernow_k6_attr[] = {
  159. &cpufreq_freq_attr_scaling_available_freqs,
  160. NULL,
  161. };
  162. static struct cpufreq_driver powernow_k6_driver = {
  163. .verify = powernow_k6_verify,
  164. .target = powernow_k6_target,
  165. .init = powernow_k6_cpu_init,
  166. .exit = powernow_k6_cpu_exit,
  167. .get = powernow_k6_get,
  168. .name = "powernow-k6",
  169. .owner = THIS_MODULE,
  170. .attr = powernow_k6_attr,
  171. };
  172. /**
  173. * powernow_k6_init - initializes the k6 PowerNow! CPUFreq driver
  174. *
  175. * Initializes the K6 PowerNow! support. Returns -ENODEV on unsupported
  176. * devices, -EINVAL or -ENOMEM on problems during initiatization, and zero
  177. * on success.
  178. */
  179. static int __init powernow_k6_init(void)
  180. {
  181. struct cpuinfo_x86 *c = &cpu_data(0);
  182. if ((c->x86_vendor != X86_VENDOR_AMD) || (c->x86 != 5) ||
  183. ((c->x86_model != 12) && (c->x86_model != 13)))
  184. return -ENODEV;
  185. if (!request_region(POWERNOW_IOPORT, 16, "PowerNow!")) {
  186. printk(KERN_INFO PFX "PowerNow IOPORT region already used.\n");
  187. return -EIO;
  188. }
  189. if (cpufreq_register_driver(&powernow_k6_driver)) {
  190. release_region(POWERNOW_IOPORT, 16);
  191. return -EINVAL;
  192. }
  193. return 0;
  194. }
  195. /**
  196. * powernow_k6_exit - unregisters AMD K6-2+/3+ PowerNow! support
  197. *
  198. * Unregisters AMD K6-2+ / K6-3+ PowerNow! support.
  199. */
  200. static void __exit powernow_k6_exit(void)
  201. {
  202. cpufreq_unregister_driver(&powernow_k6_driver);
  203. release_region(POWERNOW_IOPORT, 16);
  204. }
  205. MODULE_AUTHOR("Arjan van de Ven, Dave Jones <davej@redhat.com>, "
  206. "Dominik Brodowski <linux@brodo.de>");
  207. MODULE_DESCRIPTION("PowerNow! driver for AMD K6-2+ / K6-3+ processors.");
  208. MODULE_LICENSE("GPL");
  209. module_init(powernow_k6_init);
  210. module_exit(powernow_k6_exit);