longrun.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
  3. *
  4. * Licensed under the terms of the GNU GPL License version 2.
  5. *
  6. * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/cpufreq.h>
  12. #include <linux/timex.h>
  13. #include <asm/msr.h>
  14. #include <asm/processor.h>
  15. static struct cpufreq_driver longrun_driver;
  16. /**
  17. * longrun_{low,high}_freq is needed for the conversion of cpufreq kHz
  18. * values into per cent values. In TMTA microcode, the following is valid:
  19. * performance_pctg = (current_freq - low_freq)/(high_freq - low_freq)
  20. */
  21. static unsigned int longrun_low_freq, longrun_high_freq;
  22. /**
  23. * longrun_get_policy - get the current LongRun policy
  24. * @policy: struct cpufreq_policy where current policy is written into
  25. *
  26. * Reads the current LongRun policy by access to MSR_TMTA_LONGRUN_FLAGS
  27. * and MSR_TMTA_LONGRUN_CTRL
  28. */
  29. static void __cpuinit longrun_get_policy(struct cpufreq_policy *policy)
  30. {
  31. u32 msr_lo, msr_hi;
  32. rdmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
  33. pr_debug("longrun flags are %x - %x\n", msr_lo, msr_hi);
  34. if (msr_lo & 0x01)
  35. policy->policy = CPUFREQ_POLICY_PERFORMANCE;
  36. else
  37. policy->policy = CPUFREQ_POLICY_POWERSAVE;
  38. rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
  39. pr_debug("longrun ctrl is %x - %x\n", msr_lo, msr_hi);
  40. msr_lo &= 0x0000007F;
  41. msr_hi &= 0x0000007F;
  42. if (longrun_high_freq <= longrun_low_freq) {
  43. /* Assume degenerate Longrun table */
  44. policy->min = policy->max = longrun_high_freq;
  45. } else {
  46. policy->min = longrun_low_freq + msr_lo *
  47. ((longrun_high_freq - longrun_low_freq) / 100);
  48. policy->max = longrun_low_freq + msr_hi *
  49. ((longrun_high_freq - longrun_low_freq) / 100);
  50. }
  51. policy->cpu = 0;
  52. }
  53. /**
  54. * longrun_set_policy - sets a new CPUFreq policy
  55. * @policy: new policy
  56. *
  57. * Sets a new CPUFreq policy on LongRun-capable processors. This function
  58. * has to be called with cpufreq_driver locked.
  59. */
  60. static int longrun_set_policy(struct cpufreq_policy *policy)
  61. {
  62. u32 msr_lo, msr_hi;
  63. u32 pctg_lo, pctg_hi;
  64. if (!policy)
  65. return -EINVAL;
  66. if (longrun_high_freq <= longrun_low_freq) {
  67. /* Assume degenerate Longrun table */
  68. pctg_lo = pctg_hi = 100;
  69. } else {
  70. pctg_lo = (policy->min - longrun_low_freq) /
  71. ((longrun_high_freq - longrun_low_freq) / 100);
  72. pctg_hi = (policy->max - longrun_low_freq) /
  73. ((longrun_high_freq - longrun_low_freq) / 100);
  74. }
  75. if (pctg_hi > 100)
  76. pctg_hi = 100;
  77. if (pctg_lo > pctg_hi)
  78. pctg_lo = pctg_hi;
  79. /* performance or economy mode */
  80. rdmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
  81. msr_lo &= 0xFFFFFFFE;
  82. switch (policy->policy) {
  83. case CPUFREQ_POLICY_PERFORMANCE:
  84. msr_lo |= 0x00000001;
  85. break;
  86. case CPUFREQ_POLICY_POWERSAVE:
  87. break;
  88. }
  89. wrmsr(MSR_TMTA_LONGRUN_FLAGS, msr_lo, msr_hi);
  90. /* lower and upper boundary */
  91. rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
  92. msr_lo &= 0xFFFFFF80;
  93. msr_hi &= 0xFFFFFF80;
  94. msr_lo |= pctg_lo;
  95. msr_hi |= pctg_hi;
  96. wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
  97. return 0;
  98. }
  99. /**
  100. * longrun_verify_poliy - verifies a new CPUFreq policy
  101. * @policy: the policy to verify
  102. *
  103. * Validates a new CPUFreq policy. This function has to be called with
  104. * cpufreq_driver locked.
  105. */
  106. static int longrun_verify_policy(struct cpufreq_policy *policy)
  107. {
  108. if (!policy)
  109. return -EINVAL;
  110. policy->cpu = 0;
  111. cpufreq_verify_within_limits(policy,
  112. policy->cpuinfo.min_freq,
  113. policy->cpuinfo.max_freq);
  114. if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) &&
  115. (policy->policy != CPUFREQ_POLICY_PERFORMANCE))
  116. return -EINVAL;
  117. return 0;
  118. }
  119. static unsigned int longrun_get(unsigned int cpu)
  120. {
  121. u32 eax, ebx, ecx, edx;
  122. if (cpu)
  123. return 0;
  124. cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
  125. pr_debug("cpuid eax is %u\n", eax);
  126. return eax * 1000;
  127. }
  128. /**
  129. * longrun_determine_freqs - determines the lowest and highest possible core frequency
  130. * @low_freq: an int to put the lowest frequency into
  131. * @high_freq: an int to put the highest frequency into
  132. *
  133. * Determines the lowest and highest possible core frequencies on this CPU.
  134. * This is necessary to calculate the performance percentage according to
  135. * TMTA rules:
  136. * performance_pctg = (target_freq - low_freq)/(high_freq - low_freq)
  137. */
  138. static int __cpuinit longrun_determine_freqs(unsigned int *low_freq,
  139. unsigned int *high_freq)
  140. {
  141. u32 msr_lo, msr_hi;
  142. u32 save_lo, save_hi;
  143. u32 eax, ebx, ecx, edx;
  144. u32 try_hi;
  145. struct cpuinfo_x86 *c = &cpu_data(0);
  146. if (!low_freq || !high_freq)
  147. return -EINVAL;
  148. if (cpu_has(c, X86_FEATURE_LRTI)) {
  149. /* if the LongRun Table Interface is present, the
  150. * detection is a bit easier:
  151. * For minimum frequency, read out the maximum
  152. * level (msr_hi), write that into "currently
  153. * selected level", and read out the frequency.
  154. * For maximum frequency, read out level zero.
  155. */
  156. /* minimum */
  157. rdmsr(MSR_TMTA_LRTI_READOUT, msr_lo, msr_hi);
  158. wrmsr(MSR_TMTA_LRTI_READOUT, msr_hi, msr_hi);
  159. rdmsr(MSR_TMTA_LRTI_VOLT_MHZ, msr_lo, msr_hi);
  160. *low_freq = msr_lo * 1000; /* to kHz */
  161. /* maximum */
  162. wrmsr(MSR_TMTA_LRTI_READOUT, 0, msr_hi);
  163. rdmsr(MSR_TMTA_LRTI_VOLT_MHZ, msr_lo, msr_hi);
  164. *high_freq = msr_lo * 1000; /* to kHz */
  165. pr_debug("longrun table interface told %u - %u kHz\n",
  166. *low_freq, *high_freq);
  167. if (*low_freq > *high_freq)
  168. *low_freq = *high_freq;
  169. return 0;
  170. }
  171. /* set the upper border to the value determined during TSC init */
  172. *high_freq = (cpu_khz / 1000);
  173. *high_freq = *high_freq * 1000;
  174. pr_debug("high frequency is %u kHz\n", *high_freq);
  175. /* get current borders */
  176. rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
  177. save_lo = msr_lo & 0x0000007F;
  178. save_hi = msr_hi & 0x0000007F;
  179. /* if current perf_pctg is larger than 90%, we need to decrease the
  180. * upper limit to make the calculation more accurate.
  181. */
  182. cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
  183. /* try decreasing in 10% steps, some processors react only
  184. * on some barrier values */
  185. for (try_hi = 80; try_hi > 0 && ecx > 90; try_hi -= 10) {
  186. /* set to 0 to try_hi perf_pctg */
  187. msr_lo &= 0xFFFFFF80;
  188. msr_hi &= 0xFFFFFF80;
  189. msr_hi |= try_hi;
  190. wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
  191. /* read out current core MHz and current perf_pctg */
  192. cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
  193. /* restore values */
  194. wrmsr(MSR_TMTA_LONGRUN_CTRL, save_lo, save_hi);
  195. }
  196. pr_debug("percentage is %u %%, freq is %u MHz\n", ecx, eax);
  197. /* performance_pctg = (current_freq - low_freq)/(high_freq - low_freq)
  198. * eqals
  199. * low_freq * (1 - perf_pctg) = (cur_freq - high_freq * perf_pctg)
  200. *
  201. * high_freq * perf_pctg is stored tempoarily into "ebx".
  202. */
  203. ebx = (((cpu_khz / 1000) * ecx) / 100); /* to MHz */
  204. if ((ecx > 95) || (ecx == 0) || (eax < ebx))
  205. return -EIO;
  206. edx = ((eax - ebx) * 100) / (100 - ecx);
  207. *low_freq = edx * 1000; /* back to kHz */
  208. pr_debug("low frequency is %u kHz\n", *low_freq);
  209. if (*low_freq > *high_freq)
  210. *low_freq = *high_freq;
  211. return 0;
  212. }
  213. static int __cpuinit longrun_cpu_init(struct cpufreq_policy *policy)
  214. {
  215. int result = 0;
  216. /* capability check */
  217. if (policy->cpu != 0)
  218. return -ENODEV;
  219. /* detect low and high frequency */
  220. result = longrun_determine_freqs(&longrun_low_freq, &longrun_high_freq);
  221. if (result)
  222. return result;
  223. /* cpuinfo and default policy values */
  224. policy->cpuinfo.min_freq = longrun_low_freq;
  225. policy->cpuinfo.max_freq = longrun_high_freq;
  226. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  227. longrun_get_policy(policy);
  228. return 0;
  229. }
  230. static struct cpufreq_driver longrun_driver = {
  231. .flags = CPUFREQ_CONST_LOOPS,
  232. .verify = longrun_verify_policy,
  233. .setpolicy = longrun_set_policy,
  234. .get = longrun_get,
  235. .init = longrun_cpu_init,
  236. .name = "longrun",
  237. .owner = THIS_MODULE,
  238. };
  239. /**
  240. * longrun_init - initializes the Transmeta Crusoe LongRun CPUFreq driver
  241. *
  242. * Initializes the LongRun support.
  243. */
  244. static int __init longrun_init(void)
  245. {
  246. struct cpuinfo_x86 *c = &cpu_data(0);
  247. if (c->x86_vendor != X86_VENDOR_TRANSMETA ||
  248. !cpu_has(c, X86_FEATURE_LONGRUN))
  249. return -ENODEV;
  250. return cpufreq_register_driver(&longrun_driver);
  251. }
  252. /**
  253. * longrun_exit - unregisters LongRun support
  254. */
  255. static void __exit longrun_exit(void)
  256. {
  257. cpufreq_unregister_driver(&longrun_driver);
  258. }
  259. MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
  260. MODULE_DESCRIPTION("LongRun driver for Transmeta Crusoe and "
  261. "Efficeon processors.");
  262. MODULE_LICENSE("GPL");
  263. module_init(longrun_init);
  264. module_exit(longrun_exit);