speedstep-lib.c 12 KB

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