blob: 4ab7a215667249326d4b1b81b2144b56767b0dcd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
11#include <linux/kernel.h>
Dave Jones32ee8c32006-02-28 00:43:23 -050012#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/moduleparam.h>
14#include <linux/init.h>
15#include <linux/cpufreq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <asm/msr.h>
Matthias-Christian Ott199785e2009-02-20 20:52:17 -050018#include <asm/tsc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "speedstep-lib.h"
20
Dave Jonesbbfebd62009-01-17 23:55:22 -050021#define PFX "speedstep-lib: "
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK
Dave Jonesbbfebd62009-01-17 23:55:22 -050024static int relaxed_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#else
26#define relaxed_check 0
27#endif
28
29/*********************************************************************
30 * GET PROCESSOR CORE SPEED IN KHZ *
31 *********************************************************************/
32
Rusty Russell1cce76c2009-11-17 14:39:53 -080033static unsigned int pentium3_get_frequency(enum speedstep_processor processor)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Dave Jonesbbfebd62009-01-17 23:55:22 -050035 /* See table 14 of p3_ds.pdf and table 22 of 29834003.pdf */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 struct {
37 unsigned int ratio; /* Frequency Multiplier (x10) */
Dave Jones32ee8c32006-02-28 00:43:23 -050038 u8 bitmap; /* power on configuration bits
39 [27, 25:22] (in MSR 0x2a) */
Dave Jonesbbfebd62009-01-17 23:55:22 -050040 } msr_decode_mult[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 { 30, 0x01 },
42 { 35, 0x05 },
43 { 40, 0x02 },
44 { 45, 0x06 },
45 { 50, 0x00 },
46 { 55, 0x04 },
47 { 60, 0x0b },
48 { 65, 0x0f },
49 { 70, 0x09 },
50 { 75, 0x0d },
51 { 80, 0x0a },
52 { 85, 0x26 },
53 { 90, 0x20 },
54 { 100, 0x2b },
Dave Jonesbbfebd62009-01-17 23:55:22 -050055 { 0, 0xff } /* error or unknown value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 };
57
58 /* PIII(-M) FSB settings: see table b1-b of 24547206.pdf */
59 struct {
Dave Jones32ee8c32006-02-28 00:43:23 -050060 unsigned int value; /* Front Side Bus speed in MHz */
61 u8 bitmap; /* power on configuration bits [18: 19]
62 (in MSR 0x2a) */
Dave Jonesbbfebd62009-01-17 23:55:22 -050063 } msr_decode_fsb[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 { 66, 0x0 },
65 { 100, 0x2 },
66 { 133, 0x1 },
67 { 0, 0xff}
68 };
69
Dave Jones32ee8c32006-02-28 00:43:23 -050070 u32 msr_lo, msr_tmp;
71 int i = 0, j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 /* read MSR 0x2a - we only need the low 32 bits */
74 rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020075 pr_debug("P3 - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 msr_tmp = msr_lo;
77
78 /* decode the FSB */
79 msr_tmp &= 0x00c0000;
80 msr_tmp >>= 18;
81 while (msr_tmp != msr_decode_fsb[i].bitmap) {
82 if (msr_decode_fsb[i].bitmap == 0xff)
83 return 0;
84 i++;
85 }
86
87 /* decode the multiplier */
Dave Jonesbbfebd62009-01-17 23:55:22 -050088 if (processor == SPEEDSTEP_CPU_PIII_C_EARLY) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020089 pr_debug("workaround for early PIIIs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 msr_lo &= 0x03c00000;
91 } else
92 msr_lo &= 0x0bc00000;
93 msr_lo >>= 22;
94 while (msr_lo != msr_decode_mult[j].bitmap) {
95 if (msr_decode_mult[j].bitmap == 0xff)
96 return 0;
97 j++;
98 }
99
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200100 pr_debug("speed is %u\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500101 (msr_decode_mult[j].ratio * msr_decode_fsb[i].value * 100));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Dave Jonesbbfebd62009-01-17 23:55:22 -0500103 return msr_decode_mult[j].ratio * msr_decode_fsb[i].value * 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106
107static unsigned int pentiumM_get_frequency(void)
108{
Dave Jones32ee8c32006-02-28 00:43:23 -0500109 u32 msr_lo, msr_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200112 pr_debug("PM - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 /* see table B-2 of 24547212.pdf */
115 if (msr_lo & 0x00040000) {
Dave Jonesbbfebd62009-01-17 23:55:22 -0500116 printk(KERN_DEBUG PFX "PM - invalid FSB: 0x%x 0x%x\n",
117 msr_lo, msr_tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return 0;
119 }
120
121 msr_tmp = (msr_lo >> 22) & 0x1f;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200122 pr_debug("bits 22-26 are 0x%x, speed is %u\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500123 msr_tmp, (msr_tmp * 100 * 1000));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Dave Jonesbbfebd62009-01-17 23:55:22 -0500125 return msr_tmp * 100 * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Dominik Brodowski4e746632006-10-31 12:44:08 -0500128static unsigned int pentium_core_get_frequency(void)
129{
130 u32 fsb = 0;
131 u32 msr_lo, msr_tmp;
Dave Jonesbbfebd62009-01-17 23:55:22 -0500132 int ret;
Dominik Brodowski4e746632006-10-31 12:44:08 -0500133
134 rdmsr(MSR_FSB_FREQ, msr_lo, msr_tmp);
Dominik Brodowskie11952b2006-12-04 20:39:16 -0500135 /* see table B-2 of 25366920.pdf */
Dominik Brodowski4e746632006-10-31 12:44:08 -0500136 switch (msr_lo & 0x07) {
137 case 5:
Dominik Brodowskie11952b2006-12-04 20:39:16 -0500138 fsb = 100000;
Dominik Brodowski4e746632006-10-31 12:44:08 -0500139 break;
140 case 1:
Dominik Brodowskie11952b2006-12-04 20:39:16 -0500141 fsb = 133333;
Dominik Brodowski4e746632006-10-31 12:44:08 -0500142 break;
143 case 3:
Dominik Brodowskie11952b2006-12-04 20:39:16 -0500144 fsb = 166667;
Dominik Brodowski4e746632006-10-31 12:44:08 -0500145 break;
Herton Ronaldo Krzesinskic60e19e2008-11-15 17:02:46 -0200146 case 2:
147 fsb = 200000;
148 break;
149 case 0:
150 fsb = 266667;
151 break;
152 case 4:
153 fsb = 333333;
154 break;
Dominik Brodowski4e746632006-10-31 12:44:08 -0500155 default:
156 printk(KERN_ERR "PCORE - MSR_FSB_FREQ undefined value");
157 }
158
159 rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200160 pr_debug("PCORE - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500161 msr_lo, msr_tmp);
Dominik Brodowski4e746632006-10-31 12:44:08 -0500162
163 msr_tmp = (msr_lo >> 22) & 0x1f;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200164 pr_debug("bits 22-26 are 0x%x, speed is %u\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500165 msr_tmp, (msr_tmp * fsb));
Dominik Brodowski4e746632006-10-31 12:44:08 -0500166
Dave Jonesbbfebd62009-01-17 23:55:22 -0500167 ret = (msr_tmp * fsb);
168 return ret;
Dominik Brodowski4e746632006-10-31 12:44:08 -0500169}
Dominik Brodowskie11952b2006-12-04 20:39:16 -0500170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172static unsigned int pentium4_get_frequency(void)
173{
174 struct cpuinfo_x86 *c = &boot_cpu_data;
175 u32 msr_lo, msr_hi, mult;
176 unsigned int fsb = 0;
Dave Jonesbbfebd62009-01-17 23:55:22 -0500177 unsigned int ret;
Matthias-Christian Ott199785e2009-02-20 20:52:17 -0500178 u8 fsb_code;
179
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.
184 */
185 if (c->x86_model < 2)
186 return cpu_khz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 rdmsr(0x2c, msr_lo, msr_hi);
189
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200190 pr_debug("P4 - MSR_EBC_FREQUENCY_ID: 0x%x 0x%x\n", msr_lo, msr_hi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Dave Jones32ee8c32006-02-28 00:43:23 -0500192 /* decode the FSB: see IA-32 Intel (C) Architecture Software
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * 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.
196 */
Matthias-Christian Ott199785e2009-02-20 20:52:17 -0500197 fsb_code = (msr_lo >> 16) & 0x7;
198 switch (fsb_code) {
199 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 fsb = 100 * 1000;
Matthias-Christian Ott199785e2009-02-20 20:52:17 -0500201 break;
202 case 1:
203 fsb = 13333 * 10;
204 break;
205 case 2:
206 fsb = 200 * 1000;
207 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209
210 if (!fsb)
Dave Jonesbbfebd62009-01-17 23:55:22 -0500211 printk(KERN_DEBUG PFX "couldn't detect FSB speed. "
212 "Please send an e-mail to <linux@brodo.de>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* Multiplier. */
Zhao Yakuied9cbcd2007-11-20 14:20:21 -0500215 mult = msr_lo >> 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200217 pr_debug("P4 - FSB %u kHz; Multiplier %u; Speed %u kHz\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500218 fsb, mult, (fsb * mult));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Dave Jonesbbfebd62009-01-17 23:55:22 -0500220 ret = (fsb * mult);
221 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Dave Jones32ee8c32006-02-28 00:43:23 -0500224
Rusty Russell394122a2009-06-11 22:59:58 +0930225/* Warning: may get called from smp_call_function_single. */
Rusty Russell1cce76c2009-11-17 14:39:53 -0800226unsigned int speedstep_get_frequency(enum speedstep_processor processor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 switch (processor) {
Dave Jonesbbfebd62009-01-17 23:55:22 -0500229 case SPEEDSTEP_CPU_PCORE:
Dominik Brodowski4e746632006-10-31 12:44:08 -0500230 return pentium_core_get_frequency();
Dave Jonesbbfebd62009-01-17 23:55:22 -0500231 case SPEEDSTEP_CPU_PM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return pentiumM_get_frequency();
Dave Jonesbbfebd62009-01-17 23:55:22 -0500233 case SPEEDSTEP_CPU_P4D:
234 case SPEEDSTEP_CPU_P4M:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return pentium4_get_frequency();
Dave Jonesbbfebd62009-01-17 23:55:22 -0500236 case SPEEDSTEP_CPU_PIII_T:
237 case SPEEDSTEP_CPU_PIII_C:
238 case SPEEDSTEP_CPU_PIII_C_EARLY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return pentium3_get_frequency(processor);
240 default:
241 return 0;
242 };
243 return 0;
244}
Dave Jonesbbfebd62009-01-17 23:55:22 -0500245EXPORT_SYMBOL_GPL(speedstep_get_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247
248/*********************************************************************
249 * DETECT SPEEDSTEP-CAPABLE PROCESSOR *
250 *********************************************************************/
251
Andi Kleenfa8031a2012-01-26 00:09:12 +0100252/* Keep in sync with the x86_cpu_id tables in the different modules */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500253unsigned int speedstep_detect_processor(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Mike Travis92cb7612007-10-19 20:35:04 +0200255 struct cpuinfo_x86 *c = &cpu_data(0);
Dave Jones32ee8c32006-02-28 00:43:23 -0500256 u32 ebx, msr_lo, msr_hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200258 pr_debug("x86: %x, model: %x\n", c->x86, c->x86_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Dave Jones32ee8c32006-02-28 00:43:23 -0500260 if ((c->x86_vendor != X86_VENDOR_INTEL) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 ((c->x86 != 6) && (c->x86 != 0xF)))
262 return 0;
263
264 if (c->x86 == 0xF) {
265 /* Intel Mobile Pentium 4-M
266 * or Intel Mobile Pentium 4 with 533 MHz FSB */
267 if (c->x86_model != 2)
268 return 0;
269
270 ebx = cpuid_ebx(0x00000001);
271 ebx &= 0x000000FF;
272
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200273 pr_debug("ebx value is %x, x86_mask is %x\n", ebx, c->x86_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 switch (c->x86_mask) {
Dave Jones32ee8c32006-02-28 00:43:23 -0500276 case 4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /*
Dave Jones32ee8c32006-02-28 00:43:23 -0500278 * B-stepping [M-P4-M]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * sample has ebx = 0x0f, production has 0x0e.
280 */
281 if ((ebx == 0x0e) || (ebx == 0x0f))
Dave Jonesbbfebd62009-01-17 23:55:22 -0500282 return SPEEDSTEP_CPU_P4M;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 break;
Dave Jones32ee8c32006-02-28 00:43:23 -0500284 case 7:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /*
286 * C-stepping [M-P4-M]
287 * needs to have ebx=0x0e, else it's a celeron:
288 * cf. 25130917.pdf / page 7, footnote 5 even
289 * though 25072120.pdf / page 7 doesn't say
290 * samples are only of B-stepping...
291 */
292 if (ebx == 0x0e)
Dave Jonesbbfebd62009-01-17 23:55:22 -0500293 return SPEEDSTEP_CPU_P4M;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 break;
295 case 9:
296 /*
297 * D-stepping [M-P4-M or M-P4/533]
298 *
299 * this is totally strange: CPUID 0x0F29 is
300 * used by M-P4-M, M-P4/533 and(!) Celeron CPUs.
301 * The latter need to be sorted out as they don't
302 * support speedstep.
303 * Celerons with CPUID 0x0F29 may have either
304 * ebx=0x8 or 0xf -- 25130917.pdf doesn't say anything
305 * specific.
306 * M-P4-Ms may have either ebx=0xe or 0xf [see above]
307 * M-P4/533 have either ebx=0xe or 0xf. [25317607.pdf]
308 * also, M-P4M HTs have ebx=0x8, too
Dave Jonesbbfebd62009-01-17 23:55:22 -0500309 * For now, they are distinguished by the model_id
310 * string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500312 if ((ebx == 0x0e) ||
313 (strstr(c->x86_model_id,
314 "Mobile Intel(R) Pentium(R) 4") != NULL))
315 return SPEEDSTEP_CPU_P4M;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 break;
317 default:
318 break;
319 }
320 return 0;
321 }
322
323 switch (c->x86_model) {
324 case 0x0B: /* Intel PIII [Tualatin] */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500325 /* cpuid_ebx(1) is 0x04 for desktop PIII,
326 * 0x06 for mobile PIII-M */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 ebx = cpuid_ebx(0x00000001);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200328 pr_debug("ebx is %x\n", ebx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 ebx &= 0x000000FF;
331
332 if (ebx != 0x06)
333 return 0;
334
335 /* So far all PIII-M processors support SpeedStep. See
Dave Jones32ee8c32006-02-28 00:43:23 -0500336 * Intel's 24540640.pdf of June 2003
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500338 return SPEEDSTEP_CPU_PIII_T;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 case 0x08: /* Intel PIII [Coppermine] */
341
342 /* all mobile PIII Coppermines have FSB 100 MHz
343 * ==> sort out a few desktop PIIIs. */
344 rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_hi);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200345 pr_debug("Coppermine: MSR_IA32_EBL_CR_POWERON is 0x%x, 0x%x\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500346 msr_lo, msr_hi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 msr_lo &= 0x00c0000;
348 if (msr_lo != 0x0080000)
349 return 0;
350
351 /*
352 * If the processor is a mobile version,
353 * platform ID has bit 50 set
354 * it has SpeedStep technology if either
355 * bit 56 or 57 is set
356 */
357 rdmsr(MSR_IA32_PLATFORM_ID, msr_lo, msr_hi);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200358 pr_debug("Coppermine: MSR_IA32_PLATFORM ID is 0x%x, 0x%x\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500359 msr_lo, msr_hi);
360 if ((msr_hi & (1<<18)) &&
361 (relaxed_check ? 1 : (msr_hi & (3<<24)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (c->x86_mask == 0x01) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200363 pr_debug("early PIII version\n");
Dave Jonesbbfebd62009-01-17 23:55:22 -0500364 return SPEEDSTEP_CPU_PIII_C_EARLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 } else
Dave Jonesbbfebd62009-01-17 23:55:22 -0500366 return SPEEDSTEP_CPU_PIII_C;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
369 default:
370 return 0;
371 }
372}
373EXPORT_SYMBOL_GPL(speedstep_detect_processor);
374
375
376/*********************************************************************
377 * DETECT SPEEDSTEP SPEEDS *
378 *********************************************************************/
379
Rusty Russell1cce76c2009-11-17 14:39:53 -0800380unsigned int speedstep_get_freqs(enum speedstep_processor processor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 unsigned int *low_speed,
382 unsigned int *high_speed,
Mattia Dongili1a107602005-12-02 21:59:41 +0100383 unsigned int *transition_latency,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 void (*set_state) (unsigned int state))
385{
386 unsigned int prev_speed;
387 unsigned int ret = 0;
388 unsigned long flags;
Mattia Dongili1a107602005-12-02 21:59:41 +0100389 struct timeval tv1, tv2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if ((!processor) || (!low_speed) || (!high_speed) || (!set_state))
392 return -EINVAL;
393
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200394 pr_debug("trying to determine both speeds\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /* get current speed */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500397 prev_speed = speedstep_get_frequency(processor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (!prev_speed)
399 return -EIO;
400
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200401 pr_debug("previous speed is %u\n", prev_speed);
Mattia Dongili1a107602005-12-02 21:59:41 +0100402
Mikulas Patockad4d4eda2015-02-09 13:38:17 -0500403 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 local_irq_save(flags);
405
406 /* switch to low state */
407 set_state(SPEEDSTEP_LOW);
Dave Jonesbbfebd62009-01-17 23:55:22 -0500408 *low_speed = speedstep_get_frequency(processor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (!*low_speed) {
410 ret = -EIO;
411 goto out;
412 }
413
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200414 pr_debug("low speed is %u\n", *low_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Mattia Dongili1a107602005-12-02 21:59:41 +0100416 /* start latency measurement */
417 if (transition_latency)
418 do_gettimeofday(&tv1);
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* switch to high state */
421 set_state(SPEEDSTEP_HIGH);
Mattia Dongili1a107602005-12-02 21:59:41 +0100422
423 /* end latency measurement */
424 if (transition_latency)
425 do_gettimeofday(&tv2);
426
Dave Jonesbbfebd62009-01-17 23:55:22 -0500427 *high_speed = speedstep_get_frequency(processor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (!*high_speed) {
429 ret = -EIO;
430 goto out;
431 }
432
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200433 pr_debug("high speed is %u\n", *high_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 if (*low_speed == *high_speed) {
436 ret = -ENODEV;
437 goto out;
438 }
439
440 /* switch to previous state, if necessary */
441 if (*high_speed != prev_speed)
442 set_state(SPEEDSTEP_LOW);
443
Mattia Dongili1a107602005-12-02 21:59:41 +0100444 if (transition_latency) {
445 *transition_latency = (tv2.tv_sec - tv1.tv_sec) * USEC_PER_SEC +
446 tv2.tv_usec - tv1.tv_usec;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200447 pr_debug("transition latency is %u uSec\n", *transition_latency);
Mattia Dongili1a107602005-12-02 21:59:41 +0100448
449 /* convert uSec to nSec and add 20% for safety reasons */
450 *transition_latency *= 1200;
451
452 /* check if the latency measurement is too high or too low
453 * and set it to a safe value (500uSec) in that case
454 */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500455 if (*transition_latency > 10000000 ||
456 *transition_latency < 50000) {
457 printk(KERN_WARNING PFX "frequency transition "
458 "measured seems out of range (%u "
459 "nSec), falling back to a safe one of"
460 "%u nSec.\n",
Mattia Dongili1a107602005-12-02 21:59:41 +0100461 *transition_latency, 500000);
462 *transition_latency = 500000;
463 }
464 }
465
Dave Jones32ee8c32006-02-28 00:43:23 -0500466out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 local_irq_restore(flags);
Mikulas Patockad4d4eda2015-02-09 13:38:17 -0500468 preempt_enable();
469
Dave Jonesbbfebd62009-01-17 23:55:22 -0500470 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472EXPORT_SYMBOL_GPL(speedstep_get_freqs);
473
474#ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK
475module_param(relaxed_check, int, 0444);
Dave Jonesbbfebd62009-01-17 23:55:22 -0500476MODULE_PARM_DESC(relaxed_check,
477 "Don't do all checks for speedstep capability.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478#endif
479
Dave Jonesbbfebd62009-01-17 23:55:22 -0500480MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
481MODULE_DESCRIPTION("Library for Intel SpeedStep 1 or 2 cpufreq drivers.");
482MODULE_LICENSE("GPL");