longrun.c 8.4 KB

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