speedstep-lib.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. * Library for common functions for Intel SpeedStep v.1 and v.2 support
  7. *
  8. * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/cpufreq.h>
  16. #include <asm/msr.h>
  17. #include <asm/tsc.h>
  18. #include "speedstep-lib.h"
  19. #define PFX "speedstep-lib: "
  20. #ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK
  21. static int relaxed_check;
  22. #else
  23. #define relaxed_check 0
  24. #endif
  25. /*********************************************************************
  26. * GET PROCESSOR CORE SPEED IN KHZ *
  27. *********************************************************************/
  28. static unsigned int pentium3_get_frequency(enum speedstep_processor processor)
  29. {
  30. /* See table 14 of p3_ds.pdf and table 22 of 29834003.pdf */
  31. struct {
  32. unsigned int ratio; /* Frequency Multiplier (x10) */
  33. u8 bitmap; /* power on configuration bits
  34. [27, 25:22] (in MSR 0x2a) */
  35. } msr_decode_mult[] = {
  36. { 30, 0x01 },
  37. { 35, 0x05 },
  38. { 40, 0x02 },
  39. { 45, 0x06 },
  40. { 50, 0x00 },
  41. { 55, 0x04 },
  42. { 60, 0x0b },
  43. { 65, 0x0f },
  44. { 70, 0x09 },
  45. { 75, 0x0d },
  46. { 80, 0x0a },
  47. { 85, 0x26 },
  48. { 90, 0x20 },
  49. { 100, 0x2b },
  50. { 0, 0xff } /* error or unknown value */
  51. };
  52. /* PIII(-M) FSB settings: see table b1-b of 24547206.pdf */
  53. struct {
  54. unsigned int value; /* Front Side Bus speed in MHz */
  55. u8 bitmap; /* power on configuration bits [18: 19]
  56. (in MSR 0x2a) */
  57. } msr_decode_fsb[] = {
  58. { 66, 0x0 },
  59. { 100, 0x2 },
  60. { 133, 0x1 },
  61. { 0, 0xff}
  62. };
  63. u32 msr_lo, msr_tmp;
  64. int i = 0, j = 0;
  65. /* read MSR 0x2a - we only need the low 32 bits */
  66. rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
  67. pr_debug("P3 - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
  68. msr_tmp = msr_lo;
  69. /* decode the FSB */
  70. msr_tmp &= 0x00c0000;
  71. msr_tmp >>= 18;
  72. while (msr_tmp != msr_decode_fsb[i].bitmap) {
  73. if (msr_decode_fsb[i].bitmap == 0xff)
  74. return 0;
  75. i++;
  76. }
  77. /* decode the multiplier */
  78. if (processor == SPEEDSTEP_CPU_PIII_C_EARLY) {
  79. pr_debug("workaround for early PIIIs\n");
  80. msr_lo &= 0x03c00000;
  81. } else
  82. msr_lo &= 0x0bc00000;
  83. msr_lo >>= 22;
  84. while (msr_lo != msr_decode_mult[j].bitmap) {
  85. if (msr_decode_mult[j].bitmap == 0xff)
  86. return 0;
  87. j++;
  88. }
  89. pr_debug("speed is %u\n",
  90. (msr_decode_mult[j].ratio * msr_decode_fsb[i].value * 100));
  91. return msr_decode_mult[j].ratio * msr_decode_fsb[i].value * 100;
  92. }
  93. static unsigned int pentiumM_get_frequency(void)
  94. {
  95. u32 msr_lo, msr_tmp;
  96. rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
  97. pr_debug("PM - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
  98. /* see table B-2 of 24547212.pdf */
  99. if (msr_lo & 0x00040000) {
  100. printk(KERN_DEBUG PFX "PM - invalid FSB: 0x%x 0x%x\n",
  101. msr_lo, msr_tmp);
  102. return 0;
  103. }
  104. msr_tmp = (msr_lo >> 22) & 0x1f;
  105. pr_debug("bits 22-26 are 0x%x, speed is %u\n",
  106. msr_tmp, (msr_tmp * 100 * 1000));
  107. return msr_tmp * 100 * 1000;
  108. }
  109. static unsigned int pentium_core_get_frequency(void)
  110. {
  111. u32 fsb = 0;
  112. u32 msr_lo, msr_tmp;
  113. int ret;
  114. rdmsr(MSR_FSB_FREQ, msr_lo, msr_tmp);
  115. /* see table B-2 of 25366920.pdf */
  116. switch (msr_lo & 0x07) {
  117. case 5:
  118. fsb = 100000;
  119. break;
  120. case 1:
  121. fsb = 133333;
  122. break;
  123. case 3:
  124. fsb = 166667;
  125. break;
  126. case 2:
  127. fsb = 200000;
  128. break;
  129. case 0:
  130. fsb = 266667;
  131. break;
  132. case 4:
  133. fsb = 333333;
  134. break;
  135. default:
  136. pr_err("PCORE - MSR_FSB_FREQ undefined value\n");
  137. }
  138. rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
  139. pr_debug("PCORE - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n",
  140. msr_lo, msr_tmp);
  141. msr_tmp = (msr_lo >> 22) & 0x1f;
  142. pr_debug("bits 22-26 are 0x%x, speed is %u\n",
  143. msr_tmp, (msr_tmp * fsb));
  144. ret = (msr_tmp * fsb);
  145. return ret;
  146. }
  147. static unsigned int pentium4_get_frequency(void)
  148. {
  149. struct cpuinfo_x86 *c = &boot_cpu_data;
  150. u32 msr_lo, msr_hi, mult;
  151. unsigned int fsb = 0;
  152. unsigned int ret;
  153. u8 fsb_code;
  154. /* Pentium 4 Model 0 and 1 do not have the Core Clock Frequency
  155. * to System Bus Frequency Ratio Field in the Processor Frequency
  156. * Configuration Register of the MSR. Therefore the current
  157. * frequency cannot be calculated and has to be measured.
  158. */
  159. if (c->x86_model < 2)
  160. return cpu_khz;
  161. rdmsr(0x2c, msr_lo, msr_hi);
  162. pr_debug("P4 - MSR_EBC_FREQUENCY_ID: 0x%x 0x%x\n", msr_lo, msr_hi);
  163. /* decode the FSB: see IA-32 Intel (C) Architecture Software
  164. * Developer's Manual, Volume 3: System Prgramming Guide,
  165. * revision #12 in Table B-1: MSRs in the Pentium 4 and
  166. * Intel Xeon Processors, on page B-4 and B-5.
  167. */
  168. fsb_code = (msr_lo >> 16) & 0x7;
  169. switch (fsb_code) {
  170. case 0:
  171. fsb = 100 * 1000;
  172. break;
  173. case 1:
  174. fsb = 13333 * 10;
  175. break;
  176. case 2:
  177. fsb = 200 * 1000;
  178. break;
  179. }
  180. if (!fsb)
  181. printk(KERN_DEBUG PFX "couldn't detect FSB speed. "
  182. "Please send an e-mail to <linux@brodo.de>\n");
  183. /* Multiplier. */
  184. mult = msr_lo >> 24;
  185. pr_debug("P4 - FSB %u kHz; Multiplier %u; Speed %u kHz\n",
  186. fsb, mult, (fsb * mult));
  187. ret = (fsb * mult);
  188. return ret;
  189. }
  190. /* Warning: may get called from smp_call_function_single. */
  191. unsigned int speedstep_get_frequency(enum speedstep_processor processor)
  192. {
  193. switch (processor) {
  194. case SPEEDSTEP_CPU_PCORE:
  195. return pentium_core_get_frequency();
  196. case SPEEDSTEP_CPU_PM:
  197. return pentiumM_get_frequency();
  198. case SPEEDSTEP_CPU_P4D:
  199. case SPEEDSTEP_CPU_P4M:
  200. return pentium4_get_frequency();
  201. case SPEEDSTEP_CPU_PIII_T:
  202. case SPEEDSTEP_CPU_PIII_C:
  203. case SPEEDSTEP_CPU_PIII_C_EARLY:
  204. return pentium3_get_frequency(processor);
  205. default:
  206. return 0;
  207. };
  208. return 0;
  209. }
  210. EXPORT_SYMBOL_GPL(speedstep_get_frequency);
  211. /*********************************************************************
  212. * DETECT SPEEDSTEP-CAPABLE PROCESSOR *
  213. *********************************************************************/
  214. /* Keep in sync with the x86_cpu_id tables in the different modules */
  215. unsigned int speedstep_detect_processor(void)
  216. {
  217. struct cpuinfo_x86 *c = &cpu_data(0);
  218. u32 ebx, msr_lo, msr_hi;
  219. pr_debug("x86: %x, model: %x\n", c->x86, c->x86_model);
  220. if ((c->x86_vendor != X86_VENDOR_INTEL) ||
  221. ((c->x86 != 6) && (c->x86 != 0xF)))
  222. return 0;
  223. if (c->x86 == 0xF) {
  224. /* Intel Mobile Pentium 4-M
  225. * or Intel Mobile Pentium 4 with 533 MHz FSB */
  226. if (c->x86_model != 2)
  227. return 0;
  228. ebx = cpuid_ebx(0x00000001);
  229. ebx &= 0x000000FF;
  230. pr_debug("ebx value is %x, x86_stepping is %x\n", ebx, c->x86_stepping);
  231. switch (c->x86_stepping) {
  232. case 4:
  233. /*
  234. * B-stepping [M-P4-M]
  235. * sample has ebx = 0x0f, production has 0x0e.
  236. */
  237. if ((ebx == 0x0e) || (ebx == 0x0f))
  238. return SPEEDSTEP_CPU_P4M;
  239. break;
  240. case 7:
  241. /*
  242. * C-stepping [M-P4-M]
  243. * needs to have ebx=0x0e, else it's a celeron:
  244. * cf. 25130917.pdf / page 7, footnote 5 even
  245. * though 25072120.pdf / page 7 doesn't say
  246. * samples are only of B-stepping...
  247. */
  248. if (ebx == 0x0e)
  249. return SPEEDSTEP_CPU_P4M;
  250. break;
  251. case 9:
  252. /*
  253. * D-stepping [M-P4-M or M-P4/533]
  254. *
  255. * this is totally strange: CPUID 0x0F29 is
  256. * used by M-P4-M, M-P4/533 and(!) Celeron CPUs.
  257. * The latter need to be sorted out as they don't
  258. * support speedstep.
  259. * Celerons with CPUID 0x0F29 may have either
  260. * ebx=0x8 or 0xf -- 25130917.pdf doesn't say anything
  261. * specific.
  262. * M-P4-Ms may have either ebx=0xe or 0xf [see above]
  263. * M-P4/533 have either ebx=0xe or 0xf. [25317607.pdf]
  264. * also, M-P4M HTs have ebx=0x8, too
  265. * For now, they are distinguished by the model_id
  266. * string
  267. */
  268. if ((ebx == 0x0e) ||
  269. (strstr(c->x86_model_id,
  270. "Mobile Intel(R) Pentium(R) 4") != NULL))
  271. return SPEEDSTEP_CPU_P4M;
  272. break;
  273. default:
  274. break;
  275. }
  276. return 0;
  277. }
  278. switch (c->x86_model) {
  279. case 0x0B: /* Intel PIII [Tualatin] */
  280. /* cpuid_ebx(1) is 0x04 for desktop PIII,
  281. * 0x06 for mobile PIII-M */
  282. ebx = cpuid_ebx(0x00000001);
  283. pr_debug("ebx is %x\n", ebx);
  284. ebx &= 0x000000FF;
  285. if (ebx != 0x06)
  286. return 0;
  287. /* So far all PIII-M processors support SpeedStep. See
  288. * Intel's 24540640.pdf of June 2003
  289. */
  290. return SPEEDSTEP_CPU_PIII_T;
  291. case 0x08: /* Intel PIII [Coppermine] */
  292. /* all mobile PIII Coppermines have FSB 100 MHz
  293. * ==> sort out a few desktop PIIIs. */
  294. rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_hi);
  295. pr_debug("Coppermine: MSR_IA32_EBL_CR_POWERON is 0x%x, 0x%x\n",
  296. msr_lo, msr_hi);
  297. msr_lo &= 0x00c0000;
  298. if (msr_lo != 0x0080000)
  299. return 0;
  300. /*
  301. * If the processor is a mobile version,
  302. * platform ID has bit 50 set
  303. * it has SpeedStep technology if either
  304. * bit 56 or 57 is set
  305. */
  306. rdmsr(MSR_IA32_PLATFORM_ID, msr_lo, msr_hi);
  307. pr_debug("Coppermine: MSR_IA32_PLATFORM ID is 0x%x, 0x%x\n",
  308. msr_lo, msr_hi);
  309. if ((msr_hi & (1<<18)) &&
  310. (relaxed_check ? 1 : (msr_hi & (3<<24)))) {
  311. if (c->x86_stepping == 0x01) {
  312. pr_debug("early PIII version\n");
  313. return SPEEDSTEP_CPU_PIII_C_EARLY;
  314. } else
  315. return SPEEDSTEP_CPU_PIII_C;
  316. }
  317. default:
  318. return 0;
  319. }
  320. }
  321. EXPORT_SYMBOL_GPL(speedstep_detect_processor);
  322. /*********************************************************************
  323. * DETECT SPEEDSTEP SPEEDS *
  324. *********************************************************************/
  325. unsigned int speedstep_get_freqs(enum speedstep_processor processor,
  326. unsigned int *low_speed,
  327. unsigned int *high_speed,
  328. unsigned int *transition_latency,
  329. void (*set_state) (unsigned int state))
  330. {
  331. unsigned int prev_speed;
  332. unsigned int ret = 0;
  333. unsigned long flags;
  334. ktime_t tv1, tv2;
  335. if ((!processor) || (!low_speed) || (!high_speed) || (!set_state))
  336. return -EINVAL;
  337. pr_debug("trying to determine both speeds\n");
  338. /* get current speed */
  339. prev_speed = speedstep_get_frequency(processor);
  340. if (!prev_speed)
  341. return -EIO;
  342. pr_debug("previous speed is %u\n", prev_speed);
  343. preempt_disable();
  344. local_irq_save(flags);
  345. /* switch to low state */
  346. set_state(SPEEDSTEP_LOW);
  347. *low_speed = speedstep_get_frequency(processor);
  348. if (!*low_speed) {
  349. ret = -EIO;
  350. goto out;
  351. }
  352. pr_debug("low speed is %u\n", *low_speed);
  353. /* start latency measurement */
  354. if (transition_latency)
  355. tv1 = ktime_get();
  356. /* switch to high state */
  357. set_state(SPEEDSTEP_HIGH);
  358. /* end latency measurement */
  359. if (transition_latency)
  360. tv2 = ktime_get();
  361. *high_speed = speedstep_get_frequency(processor);
  362. if (!*high_speed) {
  363. ret = -EIO;
  364. goto out;
  365. }
  366. pr_debug("high speed is %u\n", *high_speed);
  367. if (*low_speed == *high_speed) {
  368. ret = -ENODEV;
  369. goto out;
  370. }
  371. /* switch to previous state, if necessary */
  372. if (*high_speed != prev_speed)
  373. set_state(SPEEDSTEP_LOW);
  374. if (transition_latency) {
  375. *transition_latency = ktime_to_us(ktime_sub(tv2, tv1));
  376. pr_debug("transition latency is %u uSec\n", *transition_latency);
  377. /* convert uSec to nSec and add 20% for safety reasons */
  378. *transition_latency *= 1200;
  379. /* check if the latency measurement is too high or too low
  380. * and set it to a safe value (500uSec) in that case
  381. */
  382. if (*transition_latency > 10000000 ||
  383. *transition_latency < 50000) {
  384. pr_warn("frequency transition measured seems out of range (%u nSec), falling back to a safe one of %u nSec\n",
  385. *transition_latency, 500000);
  386. *transition_latency = 500000;
  387. }
  388. }
  389. out:
  390. local_irq_restore(flags);
  391. preempt_enable();
  392. return ret;
  393. }
  394. EXPORT_SYMBOL_GPL(speedstep_get_freqs);
  395. #ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK
  396. module_param(relaxed_check, int, 0444);
  397. MODULE_PARM_DESC(relaxed_check,
  398. "Don't do all checks for speedstep capability.");
  399. #endif
  400. MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
  401. MODULE_DESCRIPTION("Library for Intel SpeedStep 1 or 2 cpufreq drivers.");
  402. MODULE_LICENSE("GPL");