powernow-k6.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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ä, Dominik Brodowski.
  4. *
  5. * Licensed under the terms of the GNU GPL License version 2.
  6. *
  7. * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/cpufreq.h>
  13. #include <linux/ioport.h>
  14. #include <linux/slab.h>
  15. #include <asm/msr.h>
  16. #include <asm/timex.h>
  17. #include <asm/io.h>
  18. #define POWERNOW_IOPORT 0xfff0 /* it doesn't matter where, as long
  19. as it is unused */
  20. static unsigned int busfreq; /* FSB, in 10 kHz */
  21. static unsigned int max_multiplier;
  22. /* Clock ratio multiplied by 10 - see table 27 in AMD#23446 */
  23. static struct cpufreq_frequency_table clock_ratio[] = {
  24. {45, /* 000 -> 4.5x */ 0},
  25. {50, /* 001 -> 5.0x */ 0},
  26. {40, /* 010 -> 4.0x */ 0},
  27. {55, /* 011 -> 5.5x */ 0},
  28. {20, /* 100 -> 2.0x */ 0},
  29. {30, /* 101 -> 3.0x */ 0},
  30. {60, /* 110 -> 6.0x */ 0},
  31. {35, /* 111 -> 3.5x */ 0},
  32. {0, CPUFREQ_TABLE_END}
  33. };
  34. /**
  35. * powernow_k6_get_cpu_multiplier - returns the current FSB multiplier
  36. *
  37. * Returns the current setting of the frequency multiplier. Core clock
  38. * speed is frequency of the Front-Side Bus multiplied with this value.
  39. */
  40. static int powernow_k6_get_cpu_multiplier(void)
  41. {
  42. u64 invalue = 0;
  43. u32 msrval;
  44. msrval = POWERNOW_IOPORT + 0x1;
  45. wrmsr(MSR_K6_EPMR, msrval, 0); /* enable the PowerNow port */
  46. invalue=inl(POWERNOW_IOPORT + 0x8);
  47. msrval = POWERNOW_IOPORT + 0x0;
  48. wrmsr(MSR_K6_EPMR, msrval, 0); /* disable it again */
  49. return clock_ratio[(invalue >> 5)&7].index;
  50. }
  51. /**
  52. * powernow_k6_set_state - set the PowerNow! multiplier
  53. * @best_i: clock_ratio[best_i] is the target multiplier
  54. *
  55. * Tries to change the PowerNow! multiplier
  56. */
  57. static void powernow_k6_set_state (unsigned int best_i)
  58. {
  59. unsigned long outvalue=0, invalue=0;
  60. unsigned long msrval;
  61. struct cpufreq_freqs freqs;
  62. if (clock_ratio[best_i].index > max_multiplier) {
  63. printk(KERN_ERR "cpufreq: invalid target frequency\n");
  64. return;
  65. }
  66. freqs.old = busfreq * powernow_k6_get_cpu_multiplier();
  67. freqs.new = busfreq * clock_ratio[best_i].index;
  68. freqs.cpu = 0; /* powernow-k6.c is UP only driver */
  69. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  70. /* we now need to transform best_i to the BVC format, see AMD#23446 */
  71. outvalue = (1<<12) | (1<<10) | (1<<9) | (best_i<<5);
  72. msrval = POWERNOW_IOPORT + 0x1;
  73. wrmsr(MSR_K6_EPMR, msrval, 0); /* enable the PowerNow port */
  74. invalue=inl(POWERNOW_IOPORT + 0x8);
  75. invalue = invalue & 0xf;
  76. outvalue = outvalue | invalue;
  77. outl(outvalue ,(POWERNOW_IOPORT + 0x8));
  78. msrval = POWERNOW_IOPORT + 0x0;
  79. wrmsr(MSR_K6_EPMR, msrval, 0); /* disable it again */
  80. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  81. return;
  82. }
  83. /**
  84. * powernow_k6_verify - verifies a new CPUfreq policy
  85. * @policy: new policy
  86. *
  87. * Policy must be within lowest and highest possible CPU Frequency,
  88. * and at least one possible state must be within min and max.
  89. */
  90. static int powernow_k6_verify(struct cpufreq_policy *policy)
  91. {
  92. return cpufreq_frequency_table_verify(policy, &clock_ratio[0]);
  93. }
  94. /**
  95. * powernow_k6_setpolicy - sets a new CPUFreq policy
  96. * @policy: new policy
  97. * @target_freq: the target frequency
  98. * @relation: how that frequency relates to achieved frequency (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
  99. *
  100. * sets a new CPUFreq policy
  101. */
  102. static int powernow_k6_target (struct cpufreq_policy *policy,
  103. unsigned int target_freq,
  104. unsigned int relation)
  105. {
  106. unsigned int newstate = 0;
  107. if (cpufreq_frequency_table_target(policy, &clock_ratio[0], target_freq, relation, &newstate))
  108. return -EINVAL;
  109. powernow_k6_set_state(newstate);
  110. return 0;
  111. }
  112. static int powernow_k6_cpu_init(struct cpufreq_policy *policy)
  113. {
  114. unsigned int i;
  115. int result;
  116. if (policy->cpu != 0)
  117. return -ENODEV;
  118. /* get frequencies */
  119. max_multiplier = powernow_k6_get_cpu_multiplier();
  120. busfreq = cpu_khz / max_multiplier;
  121. /* table init */
  122. for (i=0; (clock_ratio[i].frequency != CPUFREQ_TABLE_END); i++) {
  123. if (clock_ratio[i].index > max_multiplier)
  124. clock_ratio[i].frequency = CPUFREQ_ENTRY_INVALID;
  125. else
  126. clock_ratio[i].frequency = busfreq * clock_ratio[i].index;
  127. }
  128. /* cpuinfo and default policy values */
  129. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  130. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  131. policy->cur = busfreq * max_multiplier;
  132. result = cpufreq_frequency_table_cpuinfo(policy, clock_ratio);
  133. if (result)
  134. return (result);
  135. cpufreq_frequency_table_get_attr(clock_ratio, policy->cpu);
  136. return 0;
  137. }
  138. static int powernow_k6_cpu_exit(struct cpufreq_policy *policy)
  139. {
  140. unsigned int i;
  141. for (i=0; i<8; i++) {
  142. if (i==max_multiplier)
  143. powernow_k6_set_state(i);
  144. }
  145. cpufreq_frequency_table_put_attr(policy->cpu);
  146. return 0;
  147. }
  148. static unsigned int powernow_k6_get(unsigned int cpu)
  149. {
  150. return busfreq * powernow_k6_get_cpu_multiplier();
  151. }
  152. static struct freq_attr* powernow_k6_attr[] = {
  153. &cpufreq_freq_attr_scaling_available_freqs,
  154. NULL,
  155. };
  156. static struct cpufreq_driver powernow_k6_driver = {
  157. .verify = powernow_k6_verify,
  158. .target = powernow_k6_target,
  159. .init = powernow_k6_cpu_init,
  160. .exit = powernow_k6_cpu_exit,
  161. .get = powernow_k6_get,
  162. .name = "powernow-k6",
  163. .owner = THIS_MODULE,
  164. .attr = powernow_k6_attr,
  165. };
  166. /**
  167. * powernow_k6_init - initializes the k6 PowerNow! CPUFreq driver
  168. *
  169. * Initializes the K6 PowerNow! support. Returns -ENODEV on unsupported
  170. * devices, -EINVAL or -ENOMEM on problems during initiatization, and zero
  171. * on success.
  172. */
  173. static int __init powernow_k6_init(void)
  174. {
  175. struct cpuinfo_x86 *c = cpu_data;
  176. if ((c->x86_vendor != X86_VENDOR_AMD) || (c->x86 != 5) ||
  177. ((c->x86_model != 12) && (c->x86_model != 13)))
  178. return -ENODEV;
  179. if (!request_region(POWERNOW_IOPORT, 16, "PowerNow!")) {
  180. printk("cpufreq: PowerNow IOPORT region already used.\n");
  181. return -EIO;
  182. }
  183. if (cpufreq_register_driver(&powernow_k6_driver)) {
  184. release_region (POWERNOW_IOPORT, 16);
  185. return -EINVAL;
  186. }
  187. return 0;
  188. }
  189. /**
  190. * powernow_k6_exit - unregisters AMD K6-2+/3+ PowerNow! support
  191. *
  192. * Unregisters AMD K6-2+ / K6-3+ PowerNow! support.
  193. */
  194. static void __exit powernow_k6_exit(void)
  195. {
  196. cpufreq_unregister_driver(&powernow_k6_driver);
  197. release_region (POWERNOW_IOPORT, 16);
  198. }
  199. MODULE_AUTHOR ("Arjan van de Ven <arjanv@redhat.com>, Dave Jones <davej@codemonkey.org.uk>, Dominik Brodowski <linux@brodo.de>");
  200. MODULE_DESCRIPTION ("PowerNow! driver for AMD K6-2+ / K6-3+ processors.");
  201. MODULE_LICENSE ("GPL");
  202. module_init(powernow_k6_init);
  203. module_exit(powernow_k6_exit);