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*
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>
20 #include "speedstep-lib.h"
22 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "speedstep-lib", msg)
24 #ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK
25 static int relaxed_check = 0;
27 #define relaxed_check 0
30 /*********************************************************************
31 * GET PROCESSOR CORE SPEED IN KHZ *
32 *********************************************************************/
34 static unsigned int pentium3_get_frequency (unsigned int processor)
36 /* See table 14 of p3_ds.pdf and table 22 of 29834003.pdf */
38 unsigned int ratio; /* Frequency Multiplier (x10) */
39 u8 bitmap; /* power on configuration bits
40 [27, 25:22] (in MSR 0x2a) */
41 } msr_decode_mult [] = {
56 { 0, 0xff } /* error or unknown value */
59 /* PIII(-M) FSB settings: see table b1-b of 24547206.pdf */
61 unsigned int value; /* Front Side Bus speed in MHz */
62 u8 bitmap; /* power on configuration bits [18: 19]
64 } msr_decode_fsb [] = {
74 /* read MSR 0x2a - we only need the low 32 bits */
75 rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
76 dprintk("P3 - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
82 while (msr_tmp != msr_decode_fsb[i].bitmap) {
83 if (msr_decode_fsb[i].bitmap == 0xff)
88 /* decode the multiplier */
89 if (processor == SPEEDSTEP_PROCESSOR_PIII_C_EARLY) {
90 dprintk("workaround for early PIIIs\n");
95 while (msr_lo != msr_decode_mult[j].bitmap) {
96 if (msr_decode_mult[j].bitmap == 0xff)
101 dprintk("speed is %u\n", (msr_decode_mult[j].ratio * msr_decode_fsb[i].value * 100));
103 return (msr_decode_mult[j].ratio * msr_decode_fsb[i].value * 100);
107 static unsigned int pentiumM_get_frequency(void)
111 rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
112 dprintk("PM - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
114 /* see table B-2 of 24547212.pdf */
115 if (msr_lo & 0x00040000) {
116 printk(KERN_DEBUG "speedstep-lib: PM - invalid FSB: 0x%x 0x%x\n", msr_lo, msr_tmp);
120 msr_tmp = (msr_lo >> 22) & 0x1f;
121 dprintk("bits 22-26 are 0x%x, speed is %u\n", msr_tmp, (msr_tmp * 100 * 1000));
123 return (msr_tmp * 100 * 1000);
127 static unsigned int pentium4_get_frequency(void)
129 struct cpuinfo_x86 *c = &boot_cpu_data;
130 u32 msr_lo, msr_hi, mult;
131 unsigned int fsb = 0;
133 rdmsr(0x2c, msr_lo, msr_hi);
135 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.
142 if (c->x86_model < 2)
145 u8 fsb_code = (msr_lo >> 16) & 0x7;
160 printk(KERN_DEBUG "speedstep-lib: couldn't detect FSB speed. Please send an e-mail to <linux@brodo.de>\n");
165 dprintk("P4 - FSB %u kHz; Multiplier %u; Speed %u kHz\n", fsb, mult, (fsb * mult));
171 unsigned int speedstep_get_processor_frequency(unsigned int processor)
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);
188 EXPORT_SYMBOL_GPL(speedstep_get_processor_frequency);
191 /*********************************************************************
192 * DETECT SPEEDSTEP-CAPABLE PROCESSOR *
193 *********************************************************************/
195 unsigned int speedstep_detect_processor (void)
197 struct cpuinfo_x86 *c = cpu_data;
198 u32 ebx, msr_lo, msr_hi;
200 dprintk("x86: %x, model: %x\n", c->x86, c->x86_model);
202 if ((c->x86_vendor != X86_VENDOR_INTEL) ||
203 ((c->x86 != 6) && (c->x86 != 0xF)))
207 /* Intel Mobile Pentium 4-M
208 * or Intel Mobile Pentium 4 with 533 MHz FSB */
209 if (c->x86_model != 2)
212 ebx = cpuid_ebx(0x00000001);
215 dprintk("ebx value is %x, x86_mask is %x\n", ebx, c->x86_mask);
217 switch (c->x86_mask) {
220 * B-stepping [M-P4-M]
221 * sample has ebx = 0x0f, production has 0x0e.
223 if ((ebx == 0x0e) || (ebx == 0x0f))
224 return SPEEDSTEP_PROCESSOR_P4M;
228 * C-stepping [M-P4-M]
229 * needs to have ebx=0x0e, else it's a celeron:
230 * cf. 25130917.pdf / page 7, footnote 5 even
231 * though 25072120.pdf / page 7 doesn't say
232 * samples are only of B-stepping...
235 return SPEEDSTEP_PROCESSOR_P4M;
239 * D-stepping [M-P4-M or M-P4/533]
241 * this is totally strange: CPUID 0x0F29 is
242 * used by M-P4-M, M-P4/533 and(!) Celeron CPUs.
243 * The latter need to be sorted out as they don't
245 * Celerons with CPUID 0x0F29 may have either
246 * ebx=0x8 or 0xf -- 25130917.pdf doesn't say anything
248 * M-P4-Ms may have either ebx=0xe or 0xf [see above]
249 * M-P4/533 have either ebx=0xe or 0xf. [25317607.pdf]
250 * also, M-P4M HTs have ebx=0x8, too
251 * For now, they are distinguished by the model_id string
253 if ((ebx == 0x0e) || (strstr(c->x86_model_id,"Mobile Intel(R) Pentium(R) 4") != NULL))
254 return SPEEDSTEP_PROCESSOR_P4M;
262 switch (c->x86_model) {
263 case 0x0B: /* Intel PIII [Tualatin] */
264 /* cpuid_ebx(1) is 0x04 for desktop PIII,
265 0x06 for mobile PIII-M */
266 ebx = cpuid_ebx(0x00000001);
267 dprintk("ebx is %x\n", ebx);
274 /* So far all PIII-M processors support SpeedStep. See
275 * Intel's 24540640.pdf of June 2003
278 return SPEEDSTEP_PROCESSOR_PIII_T;
280 case 0x08: /* Intel PIII [Coppermine] */
282 /* all mobile PIII Coppermines have FSB 100 MHz
283 * ==> sort out a few desktop PIIIs. */
284 rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_hi);
285 dprintk("Coppermine: MSR_IA32_EBL_CR_POWERON is 0x%x, 0x%x\n", msr_lo, msr_hi);
287 if (msr_lo != 0x0080000)
291 * If the processor is a mobile version,
292 * platform ID has bit 50 set
293 * it has SpeedStep technology if either
294 * bit 56 or 57 is set
296 rdmsr(MSR_IA32_PLATFORM_ID, msr_lo, msr_hi);
297 dprintk("Coppermine: MSR_IA32_PLATFORM ID is 0x%x, 0x%x\n", msr_lo, msr_hi);
298 if ((msr_hi & (1<<18)) && (relaxed_check ? 1 : (msr_hi & (3<<24)))) {
299 if (c->x86_mask == 0x01) {
300 dprintk("early PIII version\n");
301 return SPEEDSTEP_PROCESSOR_PIII_C_EARLY;
303 return SPEEDSTEP_PROCESSOR_PIII_C;
310 EXPORT_SYMBOL_GPL(speedstep_detect_processor);
313 /*********************************************************************
314 * DETECT SPEEDSTEP SPEEDS *
315 *********************************************************************/
317 unsigned int speedstep_get_freqs(unsigned int processor,
318 unsigned int *low_speed,
319 unsigned int *high_speed,
320 void (*set_state) (unsigned int state))
322 unsigned int prev_speed;
323 unsigned int ret = 0;
326 if ((!processor) || (!low_speed) || (!high_speed) || (!set_state))
329 dprintk("trying to determine both speeds\n");
331 /* get current speed */
332 prev_speed = speedstep_get_processor_frequency(processor);
336 dprintk("previous seped is %u\n", prev_speed);
338 local_irq_save(flags);
340 /* switch to low state */
341 set_state(SPEEDSTEP_LOW);
342 *low_speed = speedstep_get_processor_frequency(processor);
348 dprintk("low seped is %u\n", *low_speed);
350 /* switch to high state */
351 set_state(SPEEDSTEP_HIGH);
352 *high_speed = speedstep_get_processor_frequency(processor);
358 dprintk("high seped is %u\n", *high_speed);
360 if (*low_speed == *high_speed) {
365 /* switch to previous state, if necessary */
366 if (*high_speed != prev_speed)
367 set_state(SPEEDSTEP_LOW);
370 local_irq_restore(flags);
373 EXPORT_SYMBOL_GPL(speedstep_get_freqs);
375 #ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK
376 module_param(relaxed_check, int, 0444);
377 MODULE_PARM_DESC(relaxed_check, "Don't do all checks for speedstep capability.");
380 MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>");
381 MODULE_DESCRIPTION ("Library for Intel SpeedStep 1 or 2 cpufreq drivers.");
382 MODULE_LICENSE ("GPL");