speedstep-lib.c 11 KB

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