Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / x86 / kernel / cpu / cpufreq / speedstep-lib.c
1 /*
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>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/init.h>
15 #include <linux/cpufreq.h>
16 #include <linux/slab.h>
17
18 #include <asm/msr.h>
19 #include "speedstep-lib.h"
20
21 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "speedstep-lib", msg)
22
23 #ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK
24 static int relaxed_check = 0;
25 #else
26 #define relaxed_check 0
27 #endif
28
29 /*********************************************************************
30  *                   GET PROCESSOR CORE SPEED IN KHZ                 *
31  *********************************************************************/
32
33 static unsigned int pentium3_get_frequency (unsigned int processor)
34 {
35         /* See table 14 of p3_ds.pdf and table 22 of 29834003.pdf */
36         struct {
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 [] = {
41                 { 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 },
55                 { 0, 0xff }     /* error or unknown value */
56         };
57
58         /* PIII(-M) FSB settings: see table b1-b of 24547206.pdf */
59         struct {
60                 unsigned int value;     /* Front Side Bus speed in MHz */
61                 u8 bitmap;              /* power on configuration bits [18: 19]
62                                         (in MSR 0x2a) */
63         } msr_decode_fsb [] = {
64                 {  66, 0x0 },
65                 { 100, 0x2 },
66                 { 133, 0x1 },
67                 {   0, 0xff}
68         };
69
70         u32 msr_lo, msr_tmp;
71         int i = 0, j = 0;
72
73         /* read MSR 0x2a - we only need the low 32 bits */
74         rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
75         dprintk("P3 - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
76         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 */
88         if (processor == SPEEDSTEP_PROCESSOR_PIII_C_EARLY) {
89                 dprintk("workaround for early PIIIs\n");
90                 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
100         dprintk("speed is %u\n", (msr_decode_mult[j].ratio * msr_decode_fsb[i].value * 100));
101
102         return (msr_decode_mult[j].ratio * msr_decode_fsb[i].value * 100);
103 }
104
105
106 static unsigned int pentiumM_get_frequency(void)
107 {
108         u32 msr_lo, msr_tmp;
109
110         rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
111         dprintk("PM - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
112
113         /* see table B-2 of 24547212.pdf */
114         if (msr_lo & 0x00040000) {
115                 printk(KERN_DEBUG "speedstep-lib: PM - invalid FSB: 0x%x 0x%x\n", msr_lo, msr_tmp);
116                 return 0;
117         }
118
119         msr_tmp = (msr_lo >> 22) & 0x1f;
120         dprintk("bits 22-26 are 0x%x, speed is %u\n", msr_tmp, (msr_tmp * 100 * 1000));
121
122         return (msr_tmp * 100 * 1000);
123 }
124
125 static unsigned int pentium_core_get_frequency(void)
126 {
127         u32 fsb = 0;
128         u32 msr_lo, msr_tmp;
129
130         rdmsr(MSR_FSB_FREQ, msr_lo, msr_tmp);
131         /* see table B-2 of 25366920.pdf */
132         switch (msr_lo & 0x07) {
133         case 5:
134                 fsb = 100000;
135                 break;
136         case 1:
137                 fsb = 133333;
138                 break;
139         case 3:
140                 fsb = 166667;
141                 break;
142         case 2:
143                 fsb = 200000;
144                 break;
145         case 0:
146                 fsb = 266667;
147                 break;
148         case 4:
149                 fsb = 333333;
150                 break;
151         default:
152                 printk(KERN_ERR "PCORE - MSR_FSB_FREQ undefined value");
153         }
154
155         rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_tmp);
156         dprintk("PCORE - MSR_IA32_EBL_CR_POWERON: 0x%x 0x%x\n", msr_lo, msr_tmp);
157
158         msr_tmp = (msr_lo >> 22) & 0x1f;
159         dprintk("bits 22-26 are 0x%x, speed is %u\n", msr_tmp, (msr_tmp * fsb));
160
161         return (msr_tmp * fsb);
162 }
163
164
165 static unsigned int pentium4_get_frequency(void)
166 {
167         struct cpuinfo_x86 *c = &boot_cpu_data;
168         u32 msr_lo, msr_hi, mult;
169         unsigned int fsb = 0;
170
171         rdmsr(0x2c, msr_lo, msr_hi);
172
173         dprintk("P4 - MSR_EBC_FREQUENCY_ID: 0x%x 0x%x\n", msr_lo, msr_hi);
174
175         /* decode the FSB: see IA-32 Intel (C) Architecture Software
176          * Developer's Manual, Volume 3: System Prgramming Guide,
177          * revision #12 in Table B-1: MSRs in the Pentium 4 and
178          * Intel Xeon Processors, on page B-4 and B-5.
179          */
180         if (c->x86_model < 2)
181                 fsb = 100 * 1000;
182         else {
183                 u8 fsb_code = (msr_lo >> 16) & 0x7;
184                 switch (fsb_code) {
185                 case 0:
186                         fsb = 100 * 1000;
187                         break;
188                 case 1:
189                         fsb = 13333 * 10;
190                         break;
191                 case 2:
192                         fsb = 200 * 1000;
193                         break;
194                 }
195         }
196
197         if (!fsb)
198                 printk(KERN_DEBUG "speedstep-lib: couldn't detect FSB speed. Please send an e-mail to <linux@brodo.de>\n");
199
200         /* Multiplier. */
201         mult = msr_lo >> 24;
202
203         dprintk("P4 - FSB %u kHz; Multiplier %u; Speed %u kHz\n", fsb, mult, (fsb * mult));
204
205         return (fsb * mult);
206 }
207
208
209 unsigned int speedstep_get_processor_frequency(unsigned int processor)
210 {
211         switch (processor) {
212         case SPEEDSTEP_PROCESSOR_PCORE:
213                 return pentium_core_get_frequency();
214         case SPEEDSTEP_PROCESSOR_PM:
215                 return pentiumM_get_frequency();
216         case SPEEDSTEP_PROCESSOR_P4D:
217         case SPEEDSTEP_PROCESSOR_P4M:
218                 return pentium4_get_frequency();
219         case SPEEDSTEP_PROCESSOR_PIII_T:
220         case SPEEDSTEP_PROCESSOR_PIII_C:
221         case SPEEDSTEP_PROCESSOR_PIII_C_EARLY:
222                 return pentium3_get_frequency(processor);
223         default:
224                 return 0;
225         };
226         return 0;
227 }
228 EXPORT_SYMBOL_GPL(speedstep_get_processor_frequency);
229
230
231 /*********************************************************************
232  *                 DETECT SPEEDSTEP-CAPABLE PROCESSOR                *
233  *********************************************************************/
234
235 unsigned int speedstep_detect_processor (void)
236 {
237         struct cpuinfo_x86 *c = &cpu_data(0);
238         u32 ebx, msr_lo, msr_hi;
239
240         dprintk("x86: %x, model: %x\n", c->x86, c->x86_model);
241
242         if ((c->x86_vendor != X86_VENDOR_INTEL) ||
243             ((c->x86 != 6) && (c->x86 != 0xF)))
244                 return 0;
245
246         if (c->x86 == 0xF) {
247                 /* Intel Mobile Pentium 4-M
248                  * or Intel Mobile Pentium 4 with 533 MHz FSB */
249                 if (c->x86_model != 2)
250                         return 0;
251
252                 ebx = cpuid_ebx(0x00000001);
253                 ebx &= 0x000000FF;
254
255                 dprintk("ebx value is %x, x86_mask is %x\n", ebx, c->x86_mask);
256
257                 switch (c->x86_mask) {
258                 case 4:
259                         /*
260                          * B-stepping [M-P4-M]
261                          * sample has ebx = 0x0f, production has 0x0e.
262                          */
263                         if ((ebx == 0x0e) || (ebx == 0x0f))
264                                 return SPEEDSTEP_PROCESSOR_P4M;
265                         break;
266                 case 7:
267                         /*
268                          * C-stepping [M-P4-M]
269                          * needs to have ebx=0x0e, else it's a celeron:
270                          * cf. 25130917.pdf / page 7, footnote 5 even
271                          * though 25072120.pdf / page 7 doesn't say
272                          * samples are only of B-stepping...
273                          */
274                         if (ebx == 0x0e)
275                                 return SPEEDSTEP_PROCESSOR_P4M;
276                         break;
277                 case 9:
278                         /*
279                          * D-stepping [M-P4-M or M-P4/533]
280                          *
281                          * this is totally strange: CPUID 0x0F29 is
282                          * used by M-P4-M, M-P4/533 and(!) Celeron CPUs.
283                          * The latter need to be sorted out as they don't
284                          * support speedstep.
285                          * Celerons with CPUID 0x0F29 may have either
286                          * ebx=0x8 or 0xf -- 25130917.pdf doesn't say anything
287                          * specific.
288                          * M-P4-Ms may have either ebx=0xe or 0xf [see above]
289                          * M-P4/533 have either ebx=0xe or 0xf. [25317607.pdf]
290                          * also, M-P4M HTs have ebx=0x8, too
291                          * For now, they are distinguished by the model_id string
292                          */
293                         if ((ebx == 0x0e) || (strstr(c->x86_model_id,"Mobile Intel(R) Pentium(R) 4") != NULL))
294                                 return SPEEDSTEP_PROCESSOR_P4M;
295                         break;
296                 default:
297                         break;
298                 }
299                 return 0;
300         }
301
302         switch (c->x86_model) {
303         case 0x0B: /* Intel PIII [Tualatin] */
304                 /* cpuid_ebx(1) is 0x04 for desktop PIII, 0x06 for mobile PIII-M */
305                 ebx = cpuid_ebx(0x00000001);
306                 dprintk("ebx is %x\n", ebx);
307
308                 ebx &= 0x000000FF;
309
310                 if (ebx != 0x06)
311                         return 0;
312
313                 /* So far all PIII-M processors support SpeedStep. See
314                  * Intel's 24540640.pdf of June 2003
315                  */
316                 return SPEEDSTEP_PROCESSOR_PIII_T;
317
318         case 0x08: /* Intel PIII [Coppermine] */
319
320                 /* all mobile PIII Coppermines have FSB 100 MHz
321                  * ==> sort out a few desktop PIIIs. */
322                 rdmsr(MSR_IA32_EBL_CR_POWERON, msr_lo, msr_hi);
323                 dprintk("Coppermine: MSR_IA32_EBL_CR_POWERON is 0x%x, 0x%x\n", msr_lo, msr_hi);
324                 msr_lo &= 0x00c0000;
325                 if (msr_lo != 0x0080000)
326                         return 0;
327
328                 /*
329                  * If the processor is a mobile version,
330                  * platform ID has bit 50 set
331                  * it has SpeedStep technology if either
332                  * bit 56 or 57 is set
333                  */
334                 rdmsr(MSR_IA32_PLATFORM_ID, msr_lo, msr_hi);
335                 dprintk("Coppermine: MSR_IA32_PLATFORM ID is 0x%x, 0x%x\n", msr_lo, msr_hi);
336                 if ((msr_hi & (1<<18)) && (relaxed_check ? 1 : (msr_hi & (3<<24)))) {
337                         if (c->x86_mask == 0x01) {
338                                 dprintk("early PIII version\n");
339                                 return SPEEDSTEP_PROCESSOR_PIII_C_EARLY;
340                         } else
341                                 return SPEEDSTEP_PROCESSOR_PIII_C;
342                 }
343
344         default:
345                 return 0;
346         }
347 }
348 EXPORT_SYMBOL_GPL(speedstep_detect_processor);
349
350
351 /*********************************************************************
352  *                     DETECT SPEEDSTEP SPEEDS                       *
353  *********************************************************************/
354
355 unsigned int speedstep_get_freqs(unsigned int processor,
356                                   unsigned int *low_speed,
357                                   unsigned int *high_speed,
358                                   unsigned int *transition_latency,
359                                   void (*set_state) (unsigned int state))
360 {
361         unsigned int prev_speed;
362         unsigned int ret = 0;
363         unsigned long flags;
364         struct timeval tv1, tv2;
365
366         if ((!processor) || (!low_speed) || (!high_speed) || (!set_state))
367                 return -EINVAL;
368
369         dprintk("trying to determine both speeds\n");
370
371         /* get current speed */
372         prev_speed = speedstep_get_processor_frequency(processor);
373         if (!prev_speed)
374                 return -EIO;
375
376         dprintk("previous speed is %u\n", prev_speed);
377
378         local_irq_save(flags);
379
380         /* switch to low state */
381         set_state(SPEEDSTEP_LOW);
382         *low_speed = speedstep_get_processor_frequency(processor);
383         if (!*low_speed) {
384                 ret = -EIO;
385                 goto out;
386         }
387
388         dprintk("low speed is %u\n", *low_speed);
389
390         /* start latency measurement */
391         if (transition_latency)
392                 do_gettimeofday(&tv1);
393
394         /* switch to high state */
395         set_state(SPEEDSTEP_HIGH);
396
397         /* end latency measurement */
398         if (transition_latency)
399                 do_gettimeofday(&tv2);
400
401         *high_speed = speedstep_get_processor_frequency(processor);
402         if (!*high_speed) {
403                 ret = -EIO;
404                 goto out;
405         }
406
407         dprintk("high speed is %u\n", *high_speed);
408
409         if (*low_speed == *high_speed) {
410                 ret = -ENODEV;
411                 goto out;
412         }
413
414         /* switch to previous state, if necessary */
415         if (*high_speed != prev_speed)
416                 set_state(SPEEDSTEP_LOW);
417
418         if (transition_latency) {
419                 *transition_latency = (tv2.tv_sec - tv1.tv_sec) * USEC_PER_SEC +
420                         tv2.tv_usec - tv1.tv_usec;
421                 dprintk("transition latency is %u uSec\n", *transition_latency);
422
423                 /* convert uSec to nSec and add 20% for safety reasons */
424                 *transition_latency *= 1200;
425
426                 /* check if the latency measurement is too high or too low
427                  * and set it to a safe value (500uSec) in that case
428                  */
429                 if (*transition_latency > 10000000 || *transition_latency < 50000) {
430                         printk (KERN_WARNING "speedstep: frequency transition measured seems out of "
431                                         "range (%u nSec), falling back to a safe one of %u nSec.\n",
432                                         *transition_latency, 500000);
433                         *transition_latency = 500000;
434                 }
435         }
436
437 out:
438         local_irq_restore(flags);
439         return (ret);
440 }
441 EXPORT_SYMBOL_GPL(speedstep_get_freqs);
442
443 #ifdef CONFIG_X86_SPEEDSTEP_RELAXED_CAP_CHECK
444 module_param(relaxed_check, int, 0444);
445 MODULE_PARM_DESC(relaxed_check, "Don't do all checks for speedstep capability.");
446 #endif
447
448 MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>");
449 MODULE_DESCRIPTION ("Library for Intel SpeedStep 1 or 2 cpufreq drivers.");
450 MODULE_LICENSE ("GPL");