PageRenderTime 66ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/cpufreq/powernow-k6.c

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