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>
19
#include "speedstep-lib.h"
21
#define PFX "speedstep-lib: "
23
#ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK
24
static int relaxed_check;
26
#define relaxed_check 0
29
/*********************************************************************
30
* GET PROCESSOR CORE SPEED IN KHZ *
31
*********************************************************************/
33
static unsigned int pentium3_get_frequency(enum speedstep_processor processor)
35
/* See table 14 of p3_ds.pdf and table 22 of 29834003.pdf */
37
unsigned int ratio; /* Frequency Multiplier (x10) */
38
u8 bitmap; /* power on configuration bits
39
[27, 25:22] (in MSR 0x2a) */
40
} msr_decode_mult[] = {
55
{ 0, 0xff } /* error or unknown value */
58
/* PIII(-M) FSB settings: see table b1-b of 24547206.pdf */
60
unsigned int value; /* Front Side Bus speed in MHz */
61
u8 bitmap; /* power on configuration bits [18: 19]
63
} msr_decode_fsb[] = {
73
/* read MSR 0x2a - we only need the low 32 bits */
74
rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
75
pr_debug("P3 - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
81
while (msr_tmp != msr_decode_fsb[i].bitmap) {
82
if (msr_decode_fsb[i].bitmap == 0xff)
87
/* decode the multiplier */
88
if (processor == SPEEDSTEP_CPU_PIII_C_EARLY) {
89
pr_debug("workaround for early PIIIs\n");
94
while (msr_lo != msr_decode_mult[j].bitmap) {
95
if (msr_decode_mult[j].bitmap == 0xff)
100
pr_debug("speed is %u\n",
101
(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
pr_debug("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 PFX "PM - invalid FSB: 0x%x 0x%x\n",
121
msr_tmp = (msr_lo >> 22) & 0x1f;
122
pr_debug("bits 22-26 are 0x%x, speed is %u\n",
123
msr_tmp, (msr_tmp * 100 * 1000));
125
return msr_tmp * 100 * 1000;
128
static unsigned int pentium_core_get_frequency(void)
134
rdmsr(MSR_FSB_FREQ, msr_lo, msr_tmp);
135
/* see table B-2 of 25366920.pdf */
136
switch (msr_lo & 0x07) {
156
printk(KERN_ERR "PCORE - MSR_FSB_FREQ undefined value");
159
rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
160
pr_debug("PCORE - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n",
163
msr_tmp = (msr_lo >> 22) & 0x1f;
164
pr_debug("bits 22-26 are 0x%x, speed is %u\n",
165
msr_tmp, (msr_tmp * fsb));
167
ret = (msr_tmp * fsb);
172
static unsigned int pentium4_get_frequency(void)
174
struct cpuinfo_x86 *c = &boot_cpu_data;
175
u32 msr_lo, msr_hi, mult;
176
unsigned int fsb = 0;
180
/* Pentium 4 Model 0 and 1 do not have the Core Clock Frequency
181
* to System Bus Frequency Ratio Field in the Processor Frequency
182
* Configuration Register of the MSR. Therefore the current
183
* frequency cannot be calculated and has to be measured.
185
if (c->x86_model < 2)
188
rdmsr(0x2c, msr_lo, msr_hi);
190
pr_debug("P4 - MSR_EBC_FREQUENCY_ID: 0x%x 0x%x\n", msr_lo, msr_hi);
192
/* decode the FSB: see IA-32 Intel (C) Architecture Software
193
* Developer's Manual, Volume 3: System Prgramming Guide,
194
* revision #12 in Table B-1: MSRs in the Pentium 4 and
195
* Intel Xeon Processors, on page B-4 and B-5.
197
fsb_code = (msr_lo >> 16) & 0x7;
211
printk(KERN_DEBUG PFX "couldn't detect FSB speed. "
212
"Please send an e-mail to <linux@brodo.de>\n");
217
pr_debug("P4 - FSB %u kHz; Multiplier %u; Speed %u kHz\n",
218
fsb, mult, (fsb * mult));
225
/* Warning: may get called from smp_call_function_single. */
226
unsigned int speedstep_get_frequency(enum speedstep_processor processor)
229
case SPEEDSTEP_CPU_PCORE:
230
return pentium_core_get_frequency();
231
case SPEEDSTEP_CPU_PM:
232
return pentiumM_get_frequency();
233
case SPEEDSTEP_CPU_P4D:
234
case SPEEDSTEP_CPU_P4M:
235
return pentium4_get_frequency();
236
case SPEEDSTEP_CPU_PIII_T:
237
case SPEEDSTEP_CPU_PIII_C:
238
case SPEEDSTEP_CPU_PIII_C_EARLY:
239
return pentium3_get_frequency(processor);
245
EXPORT_SYMBOL_GPL(speedstep_get_frequency);
248
/*********************************************************************
249
* DETECT SPEEDSTEP-CAPABLE PROCESSOR *
250
*********************************************************************/
252
unsigned int speedstep_detect_processor(void)
254
struct cpuinfo_x86 *c = &cpu_data(0);
255
u32 ebx, msr_lo, msr_hi;
257
pr_debug("x86: %x, model: %x\n", c->x86, c->x86_model);
259
if ((c->x86_vendor != X86_VENDOR_INTEL) ||
260
((c->x86 != 6) && (c->x86 != 0xF)))
264
/* Intel Mobile Pentium 4-M
265
* or Intel Mobile Pentium 4 with 533 MHz FSB */
266
if (c->x86_model != 2)
269
ebx = cpuid_ebx(0x00000001);
272
pr_debug("ebx value is %x, x86_mask is %x\n", ebx, c->x86_mask);
274
switch (c->x86_mask) {
277
* B-stepping [M-P4-M]
278
* sample has ebx = 0x0f, production has 0x0e.
280
if ((ebx == 0x0e) || (ebx == 0x0f))
281
return SPEEDSTEP_CPU_P4M;
285
* C-stepping [M-P4-M]
286
* needs to have ebx=0x0e, else it's a celeron:
287
* cf. 25130917.pdf / page 7, footnote 5 even
288
* though 25072120.pdf / page 7 doesn't say
289
* samples are only of B-stepping...
292
return SPEEDSTEP_CPU_P4M;
296
* D-stepping [M-P4-M or M-P4/533]
298
* this is totally strange: CPUID 0x0F29 is
299
* used by M-P4-M, M-P4/533 and(!) Celeron CPUs.
300
* The latter need to be sorted out as they don't
302
* Celerons with CPUID 0x0F29 may have either
303
* ebx=0x8 or 0xf -- 25130917.pdf doesn't say anything
305
* M-P4-Ms may have either ebx=0xe or 0xf [see above]
306
* M-P4/533 have either ebx=0xe or 0xf. [25317607.pdf]
307
* also, M-P4M HTs have ebx=0x8, too
308
* For now, they are distinguished by the model_id
312
(strstr(c->x86_model_id,
313
"Mobile Intel(R) Pentium(R) 4") != NULL))
314
return SPEEDSTEP_CPU_P4M;
322
switch (c->x86_model) {
323
case 0x0B: /* Intel PIII [Tualatin] */
324
/* cpuid_ebx(1) is 0x04 for desktop PIII,
325
* 0x06 for mobile PIII-M */
326
ebx = cpuid_ebx(0x00000001);
327
pr_debug("ebx is %x\n", ebx);
334
/* So far all PIII-M processors support SpeedStep. See
335
* Intel's 24540640.pdf of June 2003
337
return SPEEDSTEP_CPU_PIII_T;
339
case 0x08: /* Intel PIII [Coppermine] */
341
/* all mobile PIII Coppermines have FSB 100 MHz
342
* ==> sort out a few desktop PIIIs. */
343
rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_hi);
344
pr_debug("Coppermine: MSR_IA32_EBL_CR_POWERON is 0x%x, 0x%x\n",
347
if (msr_lo != 0x0080000)
351
* If the processor is a mobile version,
352
* platform ID has bit 50 set
353
* it has SpeedStep technology if either
354
* bit 56 or 57 is set
356
rdmsr(MSR_IA32_PLATFORM_ID, msr_lo, msr_hi);
357
pr_debug("Coppermine: MSR_IA32_PLATFORM ID is 0x%x, 0x%x\n",
359
if ((msr_hi & (1<<18)) &&
360
(relaxed_check ? 1 : (msr_hi & (3<<24)))) {
361
if (c->x86_mask == 0x01) {
362
pr_debug("early PIII version\n");
363
return SPEEDSTEP_CPU_PIII_C_EARLY;
365
return SPEEDSTEP_CPU_PIII_C;
372
EXPORT_SYMBOL_GPL(speedstep_detect_processor);
375
/*********************************************************************
376
* DETECT SPEEDSTEP SPEEDS *
377
*********************************************************************/
379
unsigned int speedstep_get_freqs(enum speedstep_processor processor,
380
unsigned int *low_speed,
381
unsigned int *high_speed,
382
unsigned int *transition_latency,
383
void (*set_state) (unsigned int state))
385
unsigned int prev_speed;
386
unsigned int ret = 0;
388
struct timeval tv1, tv2;
390
if ((!processor) || (!low_speed) || (!high_speed) || (!set_state))
393
pr_debug("trying to determine both speeds\n");
395
/* get current speed */
396
prev_speed = speedstep_get_frequency(processor);
400
pr_debug("previous speed is %u\n", prev_speed);
402
local_irq_save(flags);
404
/* switch to low state */
405
set_state(SPEEDSTEP_LOW);
406
*low_speed = speedstep_get_frequency(processor);
412
pr_debug("low speed is %u\n", *low_speed);
414
/* start latency measurement */
415
if (transition_latency)
416
do_gettimeofday(&tv1);
418
/* switch to high state */
419
set_state(SPEEDSTEP_HIGH);
421
/* end latency measurement */
422
if (transition_latency)
423
do_gettimeofday(&tv2);
425
*high_speed = speedstep_get_frequency(processor);
431
pr_debug("high speed is %u\n", *high_speed);
433
if (*low_speed == *high_speed) {
438
/* switch to previous state, if necessary */
439
if (*high_speed != prev_speed)
440
set_state(SPEEDSTEP_LOW);
442
if (transition_latency) {
443
*transition_latency = (tv2.tv_sec - tv1.tv_sec) * USEC_PER_SEC +
444
tv2.tv_usec - tv1.tv_usec;
445
pr_debug("transition latency is %u uSec\n", *transition_latency);
447
/* convert uSec to nSec and add 20% for safety reasons */
448
*transition_latency *= 1200;
450
/* check if the latency measurement is too high or too low
451
* and set it to a safe value (500uSec) in that case
453
if (*transition_latency > 10000000 ||
454
*transition_latency < 50000) {
455
printk(KERN_WARNING PFX "frequency transition "
456
"measured seems out of range (%u "
457
"nSec), falling back to a safe one of"
459
*transition_latency, 500000);
460
*transition_latency = 500000;
465
local_irq_restore(flags);
468
EXPORT_SYMBOL_GPL(speedstep_get_freqs);
470
#ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK
471
module_param(relaxed_check, int, 0444);
472
MODULE_PARM_DESC(relaxed_check,
473
"Don't do all checks for speedstep capability.");
476
MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
477
MODULE_DESCRIPTION("Library for Intel SpeedStep 1 or 2 cpufreq drivers.");
478
MODULE_LICENSE("GPL");