powernow-k6.c 5.5 KB

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