longrun.c 8.3 KB

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