speedstep-lib.c 11 KB

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