speedstep-lib.c 11 KB

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