speedstep-lib.c 9.7 KB

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