initial commit with v2.6.9
[linux-2.6.9-moxart.git] / arch / i386 / kernel / cpu / cpufreq / speedstep-lib.c
blob82f7c022085ba31719d8072d7fd5eedfe1212601
1 /*
2 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
4 * Licensed under the terms of the GNU GPL License version 2.
6 * Library for common functions for Intel SpeedStep v.1 and v.2 support
8 * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
9 */
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/cpufreq.h>
16 #include <linux/pci.h>
17 #include <linux/slab.h>
19 #include <asm/msr.h>
20 #include "speedstep-lib.h"
23 /* DEBUG
24 * Define it if you want verbose debug output, e.g. for bug reporting
26 //#define SPEEDSTEP_DEBUG
28 #ifdef SPEEDSTEP_DEBUG
29 #define dprintk(msg...) printk(msg)
30 #else
31 #define dprintk(msg...) do { } while(0)
32 #endif
34 #ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK
35 static int relaxed_check = 0;
36 #else
37 #define relaxed_check 0
38 #endif
40 /*********************************************************************
41 * GET PROCESSOR CORE SPEED IN KHZ *
42 *********************************************************************/
44 static unsigned int pentium3_get_frequency (unsigned int processor)
46 /* See table 14 of p3_ds.pdf and table 22 of 29834003.pdf */
47 struct {
48 unsigned int ratio; /* Frequency Multiplier (x10) */
49 u8 bitmap; /* power on configuration bits
50 [27, 25:22] (in MSR 0x2a) */
51 } msr_decode_mult [] = {
52 { 30, 0x01 },
53 { 35, 0x05 },
54 { 40, 0x02 },
55 { 45, 0x06 },
56 { 50, 0x00 },
57 { 55, 0x04 },
58 { 60, 0x0b },
59 { 65, 0x0f },
60 { 70, 0x09 },
61 { 75, 0x0d },
62 { 80, 0x0a },
63 { 85, 0x26 },
64 { 90, 0x20 },
65 { 100, 0x2b },
66 { 0, 0xff } /* error or unknown value */
69 /* PIII(-M) FSB settings: see table b1-b of 24547206.pdf */
70 struct {
71 unsigned int value; /* Front Side Bus speed in MHz */
72 u8 bitmap; /* power on configuration bits [18: 19]
73 (in MSR 0x2a) */
74 } msr_decode_fsb [] = {
75 { 66, 0x0 },
76 { 100, 0x2 },
77 { 133, 0x1 },
78 { 0, 0xff}
81 u32 msr_lo, msr_tmp;
82 int i = 0, j = 0;
84 /* read MSR 0x2a - we only need the low 32 bits */
85 rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
86 dprintk(KERN_DEBUG "speedstep-lib: P3 - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
87 msr_tmp = msr_lo;
89 /* decode the FSB */
90 msr_tmp &= 0x00c0000;
91 msr_tmp >>= 18;
92 while (msr_tmp != msr_decode_fsb[i].bitmap) {
93 if (msr_decode_fsb[i].bitmap == 0xff)
94 return 0;
95 i++;
98 /* decode the multiplier */
99 if (processor == SPEEDSTEP_PROCESSOR_PIII_C_EARLY)
100 msr_lo &= 0x03c00000;
101 else
102 msr_lo &= 0x0bc00000;
103 msr_lo >>= 22;
104 while (msr_lo != msr_decode_mult[j].bitmap) {
105 if (msr_decode_mult[j].bitmap == 0xff)
106 return 0;
107 j++;
110 return (msr_decode_mult[j].ratio * msr_decode_fsb[i].value * 100);
114 static unsigned int pentiumM_get_frequency(void)
116 u32 msr_lo, msr_tmp;
118 rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
119 dprintk(KERN_DEBUG "speedstep-lib: PM - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
121 /* see table B-2 of 24547212.pdf */
122 if (msr_lo & 0x00040000) {
123 printk(KERN_DEBUG "speedstep-lib: PM - invalid FSB: 0x%x 0x%x\n", msr_lo, msr_tmp);
124 return 0;
127 msr_tmp = (msr_lo >> 22) & 0x1f;
128 dprintk(KERN_DEBUG "speedstep-lib: bits 22-26 are 0x%x\n", msr_tmp);
130 return (msr_tmp * 100 * 1000);
134 static unsigned int pentium4_get_frequency(void)
136 struct cpuinfo_x86 *c = &boot_cpu_data;
137 u32 msr_lo, msr_hi, mult;
138 unsigned int fsb = 0;
140 rdmsr(0x2c, msr_lo, msr_hi);
142 dprintk(KERN_DEBUG "speedstep-lib: P4 - MSR_EBC_FREQUENCY_ID: 0x%x 0x%x\n", msr_lo, msr_hi);
144 /* decode the FSB: see IA-32 Intel (C) Architecture Software
145 * Developer's Manual, Volume 3: System Prgramming Guide,
146 * revision #12 in Table B-1: MSRs in the Pentium 4 and
147 * Intel Xeon Processors, on page B-4 and B-5.
149 if (c->x86_model < 2)
150 fsb = 100 * 1000;
151 else {
152 u8 fsb_code = (msr_lo >> 16) & 0x7;
153 switch (fsb_code) {
154 case 0:
155 fsb = 100 * 1000;
156 break;
157 case 1:
158 fsb = 13333 * 10;
159 break;
160 case 2:
161 fsb = 200 * 1000;
162 break;
166 if (!fsb)
167 printk(KERN_DEBUG "speedstep-lib: couldn't detect FSB speed. Please send an e-mail to <linux@brodo.de>\n");
169 /* Multiplier. */
170 mult = msr_lo >> 24;
172 dprintk(KERN_DEBUG "speedstep-lib: P4 - FSB %u kHz; Multiplier %u\n", fsb, mult);
174 return (fsb * mult);
178 unsigned int speedstep_get_processor_frequency(unsigned int processor)
180 switch (processor) {
181 case SPEEDSTEP_PROCESSOR_PM:
182 return pentiumM_get_frequency();
183 case SPEEDSTEP_PROCESSOR_P4D:
184 case SPEEDSTEP_PROCESSOR_P4M:
185 return pentium4_get_frequency();
186 case SPEEDSTEP_PROCESSOR_PIII_T:
187 case SPEEDSTEP_PROCESSOR_PIII_C:
188 case SPEEDSTEP_PROCESSOR_PIII_C_EARLY:
189 return pentium3_get_frequency(processor);
190 default:
191 return 0;
193 return 0;
195 EXPORT_SYMBOL_GPL(speedstep_get_processor_frequency);
198 /*********************************************************************
199 * DETECT SPEEDSTEP-CAPABLE PROCESSOR *
200 *********************************************************************/
202 unsigned int speedstep_detect_processor (void)
204 struct cpuinfo_x86 *c = cpu_data;
205 u32 ebx, msr_lo, msr_hi;
207 if ((c->x86_vendor != X86_VENDOR_INTEL) ||
208 ((c->x86 != 6) && (c->x86 != 0xF)))
209 return 0;
211 if (c->x86 == 0xF) {
212 /* Intel Mobile Pentium 4-M
213 * or Intel Mobile Pentium 4 with 533 MHz FSB */
214 if (c->x86_model != 2)
215 return 0;
217 ebx = cpuid_ebx(0x00000001);
218 ebx &= 0x000000FF;
220 dprintk(KERN_INFO "ebx value is %x, x86_mask is %x\n", ebx, c->x86_mask);
222 switch (c->x86_mask) {
223 case 4:
225 * B-stepping [M-P4-M]
226 * sample has ebx = 0x0f, production has 0x0e.
228 if ((ebx == 0x0e) || (ebx == 0x0f))
229 return SPEEDSTEP_PROCESSOR_P4M;
230 break;
231 case 7:
233 * C-stepping [M-P4-M]
234 * needs to have ebx=0x0e, else it's a celeron:
235 * cf. 25130917.pdf / page 7, footnote 5 even
236 * though 25072120.pdf / page 7 doesn't say
237 * samples are only of B-stepping...
239 if (ebx == 0x0e)
240 return SPEEDSTEP_PROCESSOR_P4M;
241 break;
242 case 9:
244 * D-stepping [M-P4-M or M-P4/533]
246 * this is totally strange: CPUID 0x0F29 is
247 * used by M-P4-M, M-P4/533 and(!) Celeron CPUs.
248 * The latter need to be sorted out as they don't
249 * support speedstep.
250 * Celerons with CPUID 0x0F29 may have either
251 * ebx=0x8 or 0xf -- 25130917.pdf doesn't say anything
252 * specific.
253 * M-P4-Ms may have either ebx=0xe or 0xf [see above]
254 * M-P4/533 have either ebx=0xe or 0xf. [25317607.pdf]
255 * also, M-P4M HTs have ebx=0x8, too
256 * For now, they are distinguished by the model_id string
258 if ((ebx == 0x0e) || (strstr(c->x86_model_id,"Mobile Intel(R) Pentium(R) 4") != NULL))
259 return SPEEDSTEP_PROCESSOR_P4M;
260 break;
261 default:
262 break;
264 return 0;
267 switch (c->x86_model) {
268 case 0x0B: /* Intel PIII [Tualatin] */
269 /* cpuid_ebx(1) is 0x04 for desktop PIII,
270 0x06 for mobile PIII-M */
271 ebx = cpuid_ebx(0x00000001);
273 ebx &= 0x000000FF;
275 if (ebx != 0x06)
276 return 0;
278 /* So far all PIII-M processors support SpeedStep. See
279 * Intel's 24540640.pdf of June 2003
282 return SPEEDSTEP_PROCESSOR_PIII_T;
284 case 0x08: /* Intel PIII [Coppermine] */
286 /* all mobile PIII Coppermines have FSB 100 MHz
287 * ==> sort out a few desktop PIIIs. */
288 rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_hi);
289 dprintk(KERN_DEBUG "cpufreq: Coppermine: MSR_IA32_EBL_CR_POWERON is 0x%x, 0x%x\n", msr_lo, msr_hi);
290 msr_lo &= 0x00c0000;
291 if (msr_lo != 0x0080000)
292 return 0;
295 * If the processor is a mobile version,
296 * platform ID has bit 50 set
297 * it has SpeedStep technology if either
298 * bit 56 or 57 is set
300 rdmsr(MSR_IA32_PLATFORM_ID, msr_lo, msr_hi);
301 dprintk(KERN_DEBUG "cpufreq: Coppermine: MSR_IA32_PLATFORM ID is 0x%x, 0x%x\n", msr_lo, msr_hi);
302 if ((msr_hi & (1<<18)) && (relaxed_check ? 1 : (msr_hi & (3<<24)))) {
303 if (c->x86_mask == 0x01)
304 return SPEEDSTEP_PROCESSOR_PIII_C_EARLY;
305 else
306 return SPEEDSTEP_PROCESSOR_PIII_C;
309 default:
310 return 0;
313 EXPORT_SYMBOL_GPL(speedstep_detect_processor);
316 /*********************************************************************
317 * DETECT SPEEDSTEP SPEEDS *
318 *********************************************************************/
320 unsigned int speedstep_get_freqs(unsigned int processor,
321 unsigned int *low_speed,
322 unsigned int *high_speed,
323 void (*set_state) (unsigned int state))
325 unsigned int prev_speed;
326 unsigned int ret = 0;
327 unsigned long flags;
329 if ((!processor) || (!low_speed) || (!high_speed) || (!set_state))
330 return -EINVAL;
332 /* get current speed */
333 prev_speed = speedstep_get_processor_frequency(processor);
334 if (!prev_speed)
335 return -EIO;
337 local_irq_save(flags);
339 /* switch to low state */
340 set_state(SPEEDSTEP_LOW);
341 *low_speed = speedstep_get_processor_frequency(processor);
342 if (!*low_speed) {
343 ret = -EIO;
344 goto out;
347 /* switch to high state */
348 set_state(SPEEDSTEP_HIGH);
349 *high_speed = speedstep_get_processor_frequency(processor);
350 if (!*high_speed) {
351 ret = -EIO;
352 goto out;
355 if (*low_speed == *high_speed) {
356 ret = -ENODEV;
357 goto out;
360 /* switch to previous state, if necessary */
361 if (*high_speed != prev_speed)
362 set_state(SPEEDSTEP_LOW);
364 out:
365 local_irq_restore(flags);
366 return (ret);
368 EXPORT_SYMBOL_GPL(speedstep_get_freqs);
370 #ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK
371 module_param(relaxed_check, int, 0444);
372 MODULE_PARM_DESC(relaxed_check, "Don't do all checks for speedstep capability.");
373 #endif
375 MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>");
376 MODULE_DESCRIPTION ("Library for Intel SpeedStep 1 or 2 cpufreq drivers.");
377 MODULE_LICENSE ("GPL");