longrun.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 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_cpu_limits(policy);
  113. if ((policy->policy != CPUFREQ_POLICY_POWERSAVE) &&
  114. (policy->policy != CPUFREQ_POLICY_PERFORMANCE))
  115. return -EINVAL;
  116. return 0;
  117. }
  118. static unsigned int longrun_get(unsigned int cpu)
  119. {
  120. u32 eax, ebx, ecx, edx;
  121. if (cpu)
  122. return 0;
  123. cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
  124. pr_debug("cpuid eax is %u\n", eax);
  125. return eax * 1000;
  126. }
  127. /**
  128. * longrun_determine_freqs - determines the lowest and highest possible core frequency
  129. * @low_freq: an int to put the lowest frequency into
  130. * @high_freq: an int to put the highest frequency into
  131. *
  132. * Determines the lowest and highest possible core frequencies on this CPU.
  133. * This is necessary to calculate the performance percentage according to
  134. * TMTA rules:
  135. * performance_pctg = (target_freq - low_freq)/(high_freq - low_freq)
  136. */
  137. static int longrun_determine_freqs(unsigned int *low_freq,
  138. unsigned int *high_freq)
  139. {
  140. u32 msr_lo, msr_hi;
  141. u32 save_lo, save_hi;
  142. u32 eax, ebx, ecx, edx;
  143. u32 try_hi;
  144. struct cpuinfo_x86 *c = &cpu_data(0);
  145. if (!low_freq || !high_freq)
  146. return -EINVAL;
  147. if (cpu_has(c, X86_FEATURE_LRTI)) {
  148. /* if the LongRun Table Interface is present, the
  149. * detection is a bit easier:
  150. * For minimum frequency, read out the maximum
  151. * level (msr_hi), write that into "currently
  152. * selected level", and read out the frequency.
  153. * For maximum frequency, read out level zero.
  154. */
  155. /* minimum */
  156. rdmsr(MSR_TMTA_LRTI_READOUT, msr_lo, msr_hi);
  157. wrmsr(MSR_TMTA_LRTI_READOUT, msr_hi, msr_hi);
  158. rdmsr(MSR_TMTA_LRTI_VOLT_MHZ, msr_lo, msr_hi);
  159. *low_freq = msr_lo * 1000; /* to kHz */
  160. /* maximum */
  161. wrmsr(MSR_TMTA_LRTI_READOUT, 0, msr_hi);
  162. rdmsr(MSR_TMTA_LRTI_VOLT_MHZ, msr_lo, msr_hi);
  163. *high_freq = msr_lo * 1000; /* to kHz */
  164. pr_debug("longrun table interface told %u - %u kHz\n",
  165. *low_freq, *high_freq);
  166. if (*low_freq > *high_freq)
  167. *low_freq = *high_freq;
  168. return 0;
  169. }
  170. /* set the upper border to the value determined during TSC init */
  171. *high_freq = (cpu_khz / 1000);
  172. *high_freq = *high_freq * 1000;
  173. pr_debug("high frequency is %u kHz\n", *high_freq);
  174. /* get current borders */
  175. rdmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
  176. save_lo = msr_lo & 0x0000007F;
  177. save_hi = msr_hi & 0x0000007F;
  178. /* if current perf_pctg is larger than 90%, we need to decrease the
  179. * upper limit to make the calculation more accurate.
  180. */
  181. cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
  182. /* try decreasing in 10% steps, some processors react only
  183. * on some barrier values */
  184. for (try_hi = 80; try_hi > 0 && ecx > 90; try_hi -= 10) {
  185. /* set to 0 to try_hi perf_pctg */
  186. msr_lo &= 0xFFFFFF80;
  187. msr_hi &= 0xFFFFFF80;
  188. msr_hi |= try_hi;
  189. wrmsr(MSR_TMTA_LONGRUN_CTRL, msr_lo, msr_hi);
  190. /* read out current core MHz and current perf_pctg */
  191. cpuid(0x80860007, &eax, &ebx, &ecx, &edx);
  192. /* restore values */
  193. wrmsr(MSR_TMTA_LONGRUN_CTRL, save_lo, save_hi);
  194. }
  195. pr_debug("percentage is %u %%, freq is %u MHz\n", ecx, eax);
  196. /* performance_pctg = (current_freq - low_freq)/(high_freq - low_freq)
  197. * eqals
  198. * low_freq * (1 - perf_pctg) = (cur_freq - high_freq * perf_pctg)
  199. *
  200. * high_freq * perf_pctg is stored tempoarily into "ebx".
  201. */
  202. ebx = (((cpu_khz / 1000) * ecx) / 100); /* to MHz */
  203. if ((ecx > 95) || (ecx == 0) || (eax < ebx))
  204. return -EIO;
  205. edx = ((eax - ebx) * 100) / (100 - ecx);
  206. *low_freq = edx * 1000; /* back to kHz */
  207. pr_debug("low frequency is %u kHz\n", *low_freq);
  208. if (*low_freq > *high_freq)
  209. *low_freq = *high_freq;
  210. return 0;
  211. }
  212. static int longrun_cpu_init(struct cpufreq_policy *policy)
  213. {
  214. int result = 0;
  215. /* capability check */
  216. if (policy->cpu != 0)
  217. return -ENODEV;
  218. /* detect low and high frequency */
  219. result = longrun_determine_freqs(&longrun_low_freq, &longrun_high_freq);
  220. if (result)
  221. return result;
  222. /* cpuinfo and default policy values */
  223. policy->cpuinfo.min_freq = longrun_low_freq;
  224. policy->cpuinfo.max_freq = longrun_high_freq;
  225. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  226. longrun_get_policy(policy);
  227. return 0;
  228. }
  229. static struct cpufreq_driver longrun_driver = {
  230. .flags = CPUFREQ_CONST_LOOPS,
  231. .verify = longrun_verify_policy,
  232. .setpolicy = longrun_set_policy,
  233. .get = longrun_get,
  234. .init = longrun_cpu_init,
  235. .name = "longrun",
  236. };
  237. static const struct x86_cpu_id longrun_ids[] = {
  238. { X86_VENDOR_TRANSMETA, X86_FAMILY_ANY, X86_MODEL_ANY,
  239. X86_FEATURE_LONGRUN },
  240. {}
  241. };
  242. MODULE_DEVICE_TABLE(x86cpu, longrun_ids);
  243. /**
  244. * longrun_init - initializes the Transmeta Crusoe LongRun CPUFreq driver
  245. *
  246. * Initializes the LongRun support.
  247. */
  248. static int __init longrun_init(void)
  249. {
  250. if (!x86_match_cpu(longrun_ids))
  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 "
  263. "Efficeon processors.");
  264. MODULE_LICENSE("GPL");
  265. module_init(longrun_init);
  266. module_exit(longrun_exit);