blob: ade98a219cc13bb177e95e216a6c7f4cc65a0d82 [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
Joe Perches1c5864e2016-04-05 13:28:25 -070011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
Dave Jones32ee8c32006-02-28 00:43:23 -050014#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/moduleparam.h>
16#include <linux/init.h>
17#include <linux/cpufreq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/msr.h>
Matthias-Christian Ott199785e2009-02-20 20:52:17 -050020#include <asm/tsc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "speedstep-lib.h"
22
Dave Jonesbbfebd62009-01-17 23:55:22 -050023#define PFX "speedstep-lib: "
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK
Dave Jonesbbfebd62009-01-17 23:55:22 -050026static int relaxed_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#else
28#define relaxed_check 0
29#endif
30
31/*********************************************************************
32 * GET PROCESSOR CORE SPEED IN KHZ *
33 *********************************************************************/
34
Rusty Russell1cce76c2009-11-17 14:39:53 -080035static unsigned int pentium3_get_frequency(enum speedstep_processor processor)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Dave Jonesbbfebd62009-01-17 23:55:22 -050037 /* See table 14 of p3_ds.pdf and table 22 of 29834003.pdf */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct {
39 unsigned int ratio; /* Frequency Multiplier (x10) */
Dave Jones32ee8c32006-02-28 00:43:23 -050040 u8 bitmap; /* power on configuration bits
41 [27, 25:22] (in MSR 0x2a) */
Dave Jonesbbfebd62009-01-17 23:55:22 -050042 } msr_decode_mult[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 { 30, 0x01 },
44 { 35, 0x05 },
45 { 40, 0x02 },
46 { 45, 0x06 },
47 { 50, 0x00 },
48 { 55, 0x04 },
49 { 60, 0x0b },
50 { 65, 0x0f },
51 { 70, 0x09 },
52 { 75, 0x0d },
53 { 80, 0x0a },
54 { 85, 0x26 },
55 { 90, 0x20 },
56 { 100, 0x2b },
Dave Jonesbbfebd62009-01-17 23:55:22 -050057 { 0, 0xff } /* error or unknown value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 };
59
60 /* PIII(-M) FSB settings: see table b1-b of 24547206.pdf */
61 struct {
Dave Jones32ee8c32006-02-28 00:43:23 -050062 unsigned int value; /* Front Side Bus speed in MHz */
63 u8 bitmap; /* power on configuration bits [18: 19]
64 (in MSR 0x2a) */
Dave Jonesbbfebd62009-01-17 23:55:22 -050065 } msr_decode_fsb[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 { 66, 0x0 },
67 { 100, 0x2 },
68 { 133, 0x1 },
69 { 0, 0xff}
70 };
71
Dave Jones32ee8c32006-02-28 00:43:23 -050072 u32 msr_lo, msr_tmp;
73 int i = 0, j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 /* read MSR 0x2a - we only need the low 32 bits */
76 rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020077 pr_debug("P3 - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 msr_tmp = msr_lo;
79
80 /* decode the FSB */
81 msr_tmp &= 0x00c0000;
82 msr_tmp >>= 18;
83 while (msr_tmp != msr_decode_fsb[i].bitmap) {
84 if (msr_decode_fsb[i].bitmap == 0xff)
85 return 0;
86 i++;
87 }
88
89 /* decode the multiplier */
Dave Jonesbbfebd62009-01-17 23:55:22 -050090 if (processor == SPEEDSTEP_CPU_PIII_C_EARLY) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020091 pr_debug("workaround for early PIIIs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 msr_lo &= 0x03c00000;
93 } else
94 msr_lo &= 0x0bc00000;
95 msr_lo >>= 22;
96 while (msr_lo != msr_decode_mult[j].bitmap) {
97 if (msr_decode_mult[j].bitmap == 0xff)
98 return 0;
99 j++;
100 }
101
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200102 pr_debug("speed is %u\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500103 (msr_decode_mult[j].ratio * msr_decode_fsb[i].value * 100));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Dave Jonesbbfebd62009-01-17 23:55:22 -0500105 return msr_decode_mult[j].ratio * msr_decode_fsb[i].value * 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108
109static unsigned int pentiumM_get_frequency(void)
110{
Dave Jones32ee8c32006-02-28 00:43:23 -0500111 u32 msr_lo, msr_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200114 pr_debug("PM - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 /* see table B-2 of 24547212.pdf */
117 if (msr_lo & 0x00040000) {
Dave Jonesbbfebd62009-01-17 23:55:22 -0500118 printk(KERN_DEBUG PFX "PM - invalid FSB: 0x%x 0x%x\n",
119 msr_lo, msr_tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return 0;
121 }
122
123 msr_tmp = (msr_lo >> 22) & 0x1f;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200124 pr_debug("bits 22-26 are 0x%x, speed is %u\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500125 msr_tmp, (msr_tmp * 100 * 1000));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Dave Jonesbbfebd62009-01-17 23:55:22 -0500127 return msr_tmp * 100 * 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Dominik Brodowski4e746632006-10-31 12:44:08 -0500130static unsigned int pentium_core_get_frequency(void)
131{
132 u32 fsb = 0;
133 u32 msr_lo, msr_tmp;
Dave Jonesbbfebd62009-01-17 23:55:22 -0500134 int ret;
Dominik Brodowski4e746632006-10-31 12:44:08 -0500135
136 rdmsr(MSR_FSB_FREQ, msr_lo, msr_tmp);
Dominik Brodowskie11952b2006-12-04 20:39:16 -0500137 /* see table B-2 of 25366920.pdf */
Dominik Brodowski4e746632006-10-31 12:44:08 -0500138 switch (msr_lo & 0x07) {
139 case 5:
Dominik Brodowskie11952b2006-12-04 20:39:16 -0500140 fsb = 100000;
Dominik Brodowski4e746632006-10-31 12:44:08 -0500141 break;
142 case 1:
Dominik Brodowskie11952b2006-12-04 20:39:16 -0500143 fsb = 133333;
Dominik Brodowski4e746632006-10-31 12:44:08 -0500144 break;
145 case 3:
Dominik Brodowskie11952b2006-12-04 20:39:16 -0500146 fsb = 166667;
Dominik Brodowski4e746632006-10-31 12:44:08 -0500147 break;
Herton Ronaldo Krzesinskic60e19e2008-11-15 17:02:46 -0200148 case 2:
149 fsb = 200000;
150 break;
151 case 0:
152 fsb = 266667;
153 break;
154 case 4:
155 fsb = 333333;
156 break;
Dominik Brodowski4e746632006-10-31 12:44:08 -0500157 default:
Joe Perchesb49c22a2016-04-05 13:28:24 -0700158 pr_err("PCORE - MSR_FSB_FREQ undefined value\n");
Dominik Brodowski4e746632006-10-31 12:44:08 -0500159 }
160
161 rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200162 pr_debug("PCORE - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500163 msr_lo, msr_tmp);
Dominik Brodowski4e746632006-10-31 12:44:08 -0500164
165 msr_tmp = (msr_lo >> 22) & 0x1f;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200166 pr_debug("bits 22-26 are 0x%x, speed is %u\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500167 msr_tmp, (msr_tmp * fsb));
Dominik Brodowski4e746632006-10-31 12:44:08 -0500168
Dave Jonesbbfebd62009-01-17 23:55:22 -0500169 ret = (msr_tmp * fsb);
170 return ret;
Dominik Brodowski4e746632006-10-31 12:44:08 -0500171}
Dominik Brodowskie11952b2006-12-04 20:39:16 -0500172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174static unsigned int pentium4_get_frequency(void)
175{
176 struct cpuinfo_x86 *c = &boot_cpu_data;
177 u32 msr_lo, msr_hi, mult;
178 unsigned int fsb = 0;
Dave Jonesbbfebd62009-01-17 23:55:22 -0500179 unsigned int ret;
Matthias-Christian Ott199785e2009-02-20 20:52:17 -0500180 u8 fsb_code;
181
182 /* Pentium 4 Model 0 and 1 do not have the Core Clock Frequency
183 * to System Bus Frequency Ratio Field in the Processor Frequency
184 * Configuration Register of the MSR. Therefore the current
185 * frequency cannot be calculated and has to be measured.
186 */
187 if (c->x86_model < 2)
188 return cpu_khz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 rdmsr(0x2c, msr_lo, msr_hi);
191
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200192 pr_debug("P4 - MSR_EBC_FREQUENCY_ID: 0x%x 0x%x\n", msr_lo, msr_hi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Dave Jones32ee8c32006-02-28 00:43:23 -0500194 /* decode the FSB: see IA-32 Intel (C) Architecture Software
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * Developer's Manual, Volume 3: System Prgramming Guide,
196 * revision #12 in Table B-1: MSRs in the Pentium 4 and
197 * Intel Xeon Processors, on page B-4 and B-5.
198 */
Matthias-Christian Ott199785e2009-02-20 20:52:17 -0500199 fsb_code = (msr_lo >> 16) & 0x7;
200 switch (fsb_code) {
201 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 fsb = 100 * 1000;
Matthias-Christian Ott199785e2009-02-20 20:52:17 -0500203 break;
204 case 1:
205 fsb = 13333 * 10;
206 break;
207 case 2:
208 fsb = 200 * 1000;
209 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
212 if (!fsb)
Dave Jonesbbfebd62009-01-17 23:55:22 -0500213 printk(KERN_DEBUG PFX "couldn't detect FSB speed. "
214 "Please send an e-mail to <linux@brodo.de>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 /* Multiplier. */
Zhao Yakuied9cbcd2007-11-20 14:20:21 -0500217 mult = msr_lo >> 24;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200219 pr_debug("P4 - FSB %u kHz; Multiplier %u; Speed %u kHz\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500220 fsb, mult, (fsb * mult));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Dave Jonesbbfebd62009-01-17 23:55:22 -0500222 ret = (fsb * mult);
223 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Dave Jones32ee8c32006-02-28 00:43:23 -0500226
Rusty Russell394122a2009-06-11 22:59:58 +0930227/* Warning: may get called from smp_call_function_single. */
Rusty Russell1cce76c2009-11-17 14:39:53 -0800228unsigned int speedstep_get_frequency(enum speedstep_processor processor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 switch (processor) {
Dave Jonesbbfebd62009-01-17 23:55:22 -0500231 case SPEEDSTEP_CPU_PCORE:
Dominik Brodowski4e746632006-10-31 12:44:08 -0500232 return pentium_core_get_frequency();
Dave Jonesbbfebd62009-01-17 23:55:22 -0500233 case SPEEDSTEP_CPU_PM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return pentiumM_get_frequency();
Dave Jonesbbfebd62009-01-17 23:55:22 -0500235 case SPEEDSTEP_CPU_P4D:
236 case SPEEDSTEP_CPU_P4M:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return pentium4_get_frequency();
Dave Jonesbbfebd62009-01-17 23:55:22 -0500238 case SPEEDSTEP_CPU_PIII_T:
239 case SPEEDSTEP_CPU_PIII_C:
240 case SPEEDSTEP_CPU_PIII_C_EARLY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return pentium3_get_frequency(processor);
242 default:
243 return 0;
244 };
245 return 0;
246}
Dave Jonesbbfebd62009-01-17 23:55:22 -0500247EXPORT_SYMBOL_GPL(speedstep_get_frequency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249
250/*********************************************************************
251 * DETECT SPEEDSTEP-CAPABLE PROCESSOR *
252 *********************************************************************/
253
Andi Kleenfa8031a2012-01-26 00:09:12 +0100254/* Keep in sync with the x86_cpu_id tables in the different modules */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500255unsigned int speedstep_detect_processor(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Mike Travis92cb7612007-10-19 20:35:04 +0200257 struct cpuinfo_x86 *c = &cpu_data(0);
Dave Jones32ee8c32006-02-28 00:43:23 -0500258 u32 ebx, msr_lo, msr_hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200260 pr_debug("x86: %x, model: %x\n", c->x86, c->x86_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Dave Jones32ee8c32006-02-28 00:43:23 -0500262 if ((c->x86_vendor != X86_VENDOR_INTEL) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 ((c->x86 != 6) && (c->x86 != 0xF)))
264 return 0;
265
266 if (c->x86 == 0xF) {
267 /* Intel Mobile Pentium 4-M
268 * or Intel Mobile Pentium 4 with 533 MHz FSB */
269 if (c->x86_model != 2)
270 return 0;
271
272 ebx = cpuid_ebx(0x00000001);
273 ebx &= 0x000000FF;
274
Jia Zhang06be0072018-01-01 09:52:10 +0800275 pr_debug("ebx value is %x, x86_stepping is %x\n", ebx, c->x86_stepping);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Jia Zhang06be0072018-01-01 09:52:10 +0800277 switch (c->x86_stepping) {
Dave Jones32ee8c32006-02-28 00:43:23 -0500278 case 4:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /*
Dave Jones32ee8c32006-02-28 00:43:23 -0500280 * B-stepping [M-P4-M]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * sample has ebx = 0x0f, production has 0x0e.
282 */
283 if ((ebx == 0x0e) || (ebx == 0x0f))
Dave Jonesbbfebd62009-01-17 23:55:22 -0500284 return SPEEDSTEP_CPU_P4M;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 break;
Dave Jones32ee8c32006-02-28 00:43:23 -0500286 case 7:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /*
288 * C-stepping [M-P4-M]
289 * needs to have ebx=0x0e, else it's a celeron:
290 * cf. 25130917.pdf / page 7, footnote 5 even
291 * though 25072120.pdf / page 7 doesn't say
292 * samples are only of B-stepping...
293 */
294 if (ebx == 0x0e)
Dave Jonesbbfebd62009-01-17 23:55:22 -0500295 return SPEEDSTEP_CPU_P4M;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 break;
297 case 9:
298 /*
299 * D-stepping [M-P4-M or M-P4/533]
300 *
301 * this is totally strange: CPUID 0x0F29 is
302 * used by M-P4-M, M-P4/533 and(!) Celeron CPUs.
303 * The latter need to be sorted out as they don't
304 * support speedstep.
305 * Celerons with CPUID 0x0F29 may have either
306 * ebx=0x8 or 0xf -- 25130917.pdf doesn't say anything
307 * specific.
308 * M-P4-Ms may have either ebx=0xe or 0xf [see above]
309 * M-P4/533 have either ebx=0xe or 0xf. [25317607.pdf]
310 * also, M-P4M HTs have ebx=0x8, too
Dave Jonesbbfebd62009-01-17 23:55:22 -0500311 * For now, they are distinguished by the model_id
312 * string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500314 if ((ebx == 0x0e) ||
315 (strstr(c->x86_model_id,
316 "Mobile Intel(R) Pentium(R) 4") != NULL))
317 return SPEEDSTEP_CPU_P4M;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 break;
319 default:
320 break;
321 }
322 return 0;
323 }
324
325 switch (c->x86_model) {
326 case 0x0B: /* Intel PIII [Tualatin] */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500327 /* cpuid_ebx(1) is 0x04 for desktop PIII,
328 * 0x06 for mobile PIII-M */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 ebx = cpuid_ebx(0x00000001);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200330 pr_debug("ebx is %x\n", ebx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 ebx &= 0x000000FF;
333
334 if (ebx != 0x06)
335 return 0;
336
337 /* So far all PIII-M processors support SpeedStep. See
Dave Jones32ee8c32006-02-28 00:43:23 -0500338 * Intel's 24540640.pdf of June 2003
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500340 return SPEEDSTEP_CPU_PIII_T;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 case 0x08: /* Intel PIII [Coppermine] */
343
344 /* all mobile PIII Coppermines have FSB 100 MHz
345 * ==> sort out a few desktop PIIIs. */
346 rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_hi);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200347 pr_debug("Coppermine: MSR_IA32_EBL_CR_POWERON is 0x%x, 0x%x\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500348 msr_lo, msr_hi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 msr_lo &= 0x00c0000;
350 if (msr_lo != 0x0080000)
351 return 0;
352
353 /*
354 * If the processor is a mobile version,
355 * platform ID has bit 50 set
356 * it has SpeedStep technology if either
357 * bit 56 or 57 is set
358 */
359 rdmsr(MSR_IA32_PLATFORM_ID, msr_lo, msr_hi);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200360 pr_debug("Coppermine: MSR_IA32_PLATFORM ID is 0x%x, 0x%x\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500361 msr_lo, msr_hi);
362 if ((msr_hi & (1<<18)) &&
363 (relaxed_check ? 1 : (msr_hi & (3<<24)))) {
Jia Zhang06be0072018-01-01 09:52:10 +0800364 if (c->x86_stepping == 0x01) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200365 pr_debug("early PIII version\n");
Dave Jonesbbfebd62009-01-17 23:55:22 -0500366 return SPEEDSTEP_CPU_PIII_C_EARLY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 } else
Dave Jonesbbfebd62009-01-17 23:55:22 -0500368 return SPEEDSTEP_CPU_PIII_C;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370
371 default:
372 return 0;
373 }
374}
375EXPORT_SYMBOL_GPL(speedstep_detect_processor);
376
377
378/*********************************************************************
379 * DETECT SPEEDSTEP SPEEDS *
380 *********************************************************************/
381
Rusty Russell1cce76c2009-11-17 14:39:53 -0800382unsigned int speedstep_get_freqs(enum speedstep_processor processor,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 unsigned int *low_speed,
384 unsigned int *high_speed,
Mattia Dongili1a107602005-12-02 21:59:41 +0100385 unsigned int *transition_latency,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 void (*set_state) (unsigned int state))
387{
388 unsigned int prev_speed;
389 unsigned int ret = 0;
390 unsigned long flags;
Abhilash Jindal72e624d2015-08-11 12:01:22 -0400391 ktime_t tv1, tv2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 if ((!processor) || (!low_speed) || (!high_speed) || (!set_state))
394 return -EINVAL;
395
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200396 pr_debug("trying to determine both speeds\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 /* get current speed */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500399 prev_speed = speedstep_get_frequency(processor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (!prev_speed)
401 return -EIO;
402
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200403 pr_debug("previous speed is %u\n", prev_speed);
Mattia Dongili1a107602005-12-02 21:59:41 +0100404
Mikulas Patockad4d4eda2015-02-09 13:38:17 -0500405 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 local_irq_save(flags);
407
408 /* switch to low state */
409 set_state(SPEEDSTEP_LOW);
Dave Jonesbbfebd62009-01-17 23:55:22 -0500410 *low_speed = speedstep_get_frequency(processor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (!*low_speed) {
412 ret = -EIO;
413 goto out;
414 }
415
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200416 pr_debug("low speed is %u\n", *low_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Mattia Dongili1a107602005-12-02 21:59:41 +0100418 /* start latency measurement */
419 if (transition_latency)
Abhilash Jindal72e624d2015-08-11 12:01:22 -0400420 tv1 = ktime_get();
Mattia Dongili1a107602005-12-02 21:59:41 +0100421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /* switch to high state */
423 set_state(SPEEDSTEP_HIGH);
Mattia Dongili1a107602005-12-02 21:59:41 +0100424
425 /* end latency measurement */
426 if (transition_latency)
Abhilash Jindal72e624d2015-08-11 12:01:22 -0400427 tv2 = ktime_get();
Mattia Dongili1a107602005-12-02 21:59:41 +0100428
Dave Jonesbbfebd62009-01-17 23:55:22 -0500429 *high_speed = speedstep_get_frequency(processor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (!*high_speed) {
431 ret = -EIO;
432 goto out;
433 }
434
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200435 pr_debug("high speed is %u\n", *high_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 if (*low_speed == *high_speed) {
438 ret = -ENODEV;
439 goto out;
440 }
441
442 /* switch to previous state, if necessary */
443 if (*high_speed != prev_speed)
444 set_state(SPEEDSTEP_LOW);
445
Mattia Dongili1a107602005-12-02 21:59:41 +0100446 if (transition_latency) {
Abhilash Jindal72e624d2015-08-11 12:01:22 -0400447 *transition_latency = ktime_to_us(ktime_sub(tv2, tv1));
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200448 pr_debug("transition latency is %u uSec\n", *transition_latency);
Mattia Dongili1a107602005-12-02 21:59:41 +0100449
450 /* convert uSec to nSec and add 20% for safety reasons */
451 *transition_latency *= 1200;
452
453 /* check if the latency measurement is too high or too low
454 * and set it to a safe value (500uSec) in that case
455 */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500456 if (*transition_latency > 10000000 ||
457 *transition_latency < 50000) {
Joe Perches1c5864e2016-04-05 13:28:25 -0700458 pr_warn("frequency transition measured seems out of range (%u nSec), falling back to a safe one of %u nSec\n",
Joe Perchesb49c22a2016-04-05 13:28:24 -0700459 *transition_latency, 500000);
Mattia Dongili1a107602005-12-02 21:59:41 +0100460 *transition_latency = 500000;
461 }
462 }
463
Dave Jones32ee8c32006-02-28 00:43:23 -0500464out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 local_irq_restore(flags);
Mikulas Patockad4d4eda2015-02-09 13:38:17 -0500466 preempt_enable();
467
Dave Jonesbbfebd62009-01-17 23:55:22 -0500468 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470EXPORT_SYMBOL_GPL(speedstep_get_freqs);
471
472#ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK
473module_param(relaxed_check, int, 0444);
Dave Jonesbbfebd62009-01-17 23:55:22 -0500474MODULE_PARM_DESC(relaxed_check,
475 "Don't do all checks for speedstep capability.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#endif
477
Dave Jonesbbfebd62009-01-17 23:55:22 -0500478MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
479MODULE_DESCRIPTION("Library for Intel SpeedStep 1 or 2 cpufreq drivers.");
480MODULE_LICENSE("GPL");