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