speedstep-lib.c 11 KB

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