speedstep-lib.c 11 KB

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