speedstep-lib.c 11 KB

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