patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / i386 / kernel / cpu / cpufreq / speedstep-centrino.c
1 /*
2  * cpufreq driver for Enhanced SpeedStep, as found in Intel's Pentium
3  * M (part of the Centrino chipset).
4  *
5  * Despite the "SpeedStep" in the name, this is almost entirely unlike
6  * traditional SpeedStep.
7  *
8  * Modelled on speedstep.c
9  *
10  * Copyright (C) 2003 Jeremy Fitzhardinge <jeremy@goop.org>
11  *
12  * WARNING WARNING WARNING
13  * 
14  * This driver manipulates the PERF_CTL MSR, which is only somewhat
15  * documented.  While it seems to work on my laptop, it has not been
16  * tested anywhere else, and it may not work for you, do strange
17  * things or simply crash.
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/cpufreq.h>
24 #include <linux/config.h>
25
26 #include <asm/msr.h>
27 #include <asm/processor.h>
28 #include <asm/cpufeature.h>
29
30 #define PFX             "speedstep-centrino: "
31 #define MAINTAINER      "Jeremy Fitzhardinge <jeremy@goop.org>"
32
33 /*#define CENTRINO_DEBUG*/
34
35 #ifdef CENTRINO_DEBUG
36 #define dprintk(msg...) printk(msg)
37 #else
38 #define dprintk(msg...) do { } while(0)
39 #endif
40
41 struct cpu_id
42 {
43         __u8    x86;            /* CPU family */
44         __u8    x86_vendor;     /* CPU vendor */
45         __u8    x86_model;      /* model */
46         __u8    x86_mask;       /* stepping */
47 };
48
49 static const struct cpu_id cpu_id_banias = {
50         .x86_vendor = X86_VENDOR_INTEL,
51         .x86 = 6,
52         .x86_model = 9,
53         .x86_mask = 5,
54 };
55
56 static const struct cpu_id cpu_id_dothan_a1 = {
57         .x86_vendor = X86_VENDOR_INTEL,
58         .x86 = 6,
59         .x86_model = 13,
60         .x86_mask = 1,
61 };
62
63 struct cpu_model
64 {
65         const struct cpu_id *cpu_id;
66         const char      *model_name;
67         unsigned        max_freq; /* max clock in kHz */
68
69         struct cpufreq_frequency_table *op_points; /* clock/voltage pairs */
70 };
71 static int centrino_verify_cpu_id(struct cpuinfo_x86 *c, const struct cpu_id *x);
72
73 /* Operating points for current CPU */
74 static struct cpu_model *centrino_model;
75
76 #ifdef CONFIG_X86_SPEEDSTEP_CENTRINO_TABLE
77
78 /* Computes the correct form for IA32_PERF_CTL MSR for a particular
79    frequency/voltage operating point; frequency in MHz, volts in mV.
80    This is stored as "index" in the structure. */
81 #define OP(mhz, mv)                                                     \
82         {                                                               \
83                 .frequency = (mhz) * 1000,                              \
84                 .index = (((mhz)/100) << 8) | ((mv - 700) / 16)         \
85         }
86
87 /* 
88  * These voltage tables were derived from the Intel Pentium M
89  * datasheet, document 25261202.pdf, Table 5.  I have verified they
90  * are consistent with my IBM ThinkPad X31, which has a 1.3GHz Pentium
91  * M.
92  */
93
94 /* Ultra Low Voltage Intel Pentium M processor 900MHz (Banias) */
95 static struct cpufreq_frequency_table banias_900[] =
96 {
97         OP(600,  844),
98         OP(800,  988),
99         OP(900, 1004),
100         { .frequency = CPUFREQ_TABLE_END }
101 };
102
103 /* Ultra Low Voltage Intel Pentium M processor 1000MHz (Banias) */
104 static struct cpufreq_frequency_table banias_1000[] =
105 {
106         OP(600,  844),
107         OP(800,  972),
108         OP(900,  988),
109         OP(1000, 1004),
110         { .frequency = CPUFREQ_TABLE_END }
111 };
112
113 /* Low Voltage Intel Pentium M processor 1.10GHz (Banias) */
114 static struct cpufreq_frequency_table banias_1100[] =
115 {
116         OP( 600,  956),
117         OP( 800, 1020),
118         OP( 900, 1100),
119         OP(1000, 1164),
120         OP(1100, 1180),
121         { .frequency = CPUFREQ_TABLE_END }
122 };
123
124
125 /* Low Voltage Intel Pentium M processor 1.20GHz (Banias) */
126 static struct cpufreq_frequency_table banias_1200[] =
127 {
128         OP( 600,  956),
129         OP( 800, 1004),
130         OP( 900, 1020),
131         OP(1000, 1100),
132         OP(1100, 1164),
133         OP(1200, 1180),
134         { .frequency = CPUFREQ_TABLE_END }
135 };
136
137 /* Intel Pentium M processor 1.30GHz (Banias) */
138 static struct cpufreq_frequency_table banias_1300[] = 
139 {
140         OP( 600,  956),
141         OP( 800, 1260),
142         OP(1000, 1292),
143         OP(1200, 1356),
144         OP(1300, 1388),
145         { .frequency = CPUFREQ_TABLE_END }
146 };
147
148 /* Intel Pentium M processor 1.40GHz (Banias) */
149 static struct cpufreq_frequency_table banias_1400[] = 
150 {
151         OP( 600,  956),
152         OP( 800, 1180),
153         OP(1000, 1308),
154         OP(1200, 1436),
155         OP(1400, 1484),
156         { .frequency = CPUFREQ_TABLE_END }
157 };
158
159 /* Intel Pentium M processor 1.50GHz (Banias) */
160 static struct cpufreq_frequency_table banias_1500[] = 
161 {
162         OP( 600,  956),
163         OP( 800, 1116),
164         OP(1000, 1228),
165         OP(1200, 1356),
166         OP(1400, 1452),
167         OP(1500, 1484),
168         { .frequency = CPUFREQ_TABLE_END }
169 };
170
171 /* Intel Pentium M processor 1.60GHz (Banias) */
172 static struct cpufreq_frequency_table banias_1600[] = 
173 {
174         OP( 600,  956),
175         OP( 800, 1036),
176         OP(1000, 1164),
177         OP(1200, 1276),
178         OP(1400, 1420),
179         OP(1600, 1484),
180         { .frequency = CPUFREQ_TABLE_END }
181 };
182
183 /* Intel Pentium M processor 1.70GHz (Banias) */
184 static struct cpufreq_frequency_table banias_1700[] =
185 {
186         OP( 600,  956),
187         OP( 800, 1004),
188         OP(1000, 1116),
189         OP(1200, 1228),
190         OP(1400, 1308),
191         OP(1700, 1484),
192         { .frequency = CPUFREQ_TABLE_END }
193 };
194 #undef OP
195
196 #define _BANIAS(cpuid, max, name)       \
197 {       .cpu_id         = cpuid,        \
198         .model_name     = "Intel(R) Pentium(R) M processor " name "MHz", \
199         .max_freq       = (max)*1000,   \
200         .op_points      = banias_##max, \
201 }
202 #define BANIAS(max)     _BANIAS(&cpu_id_banias, max, #max)
203
204 /* CPU models, their operating frequency range, and freq/voltage
205    operating points */
206 static struct cpu_model models[] = 
207 {
208         _BANIAS(&cpu_id_banias, 900, " 900"),
209         BANIAS(1000),
210         BANIAS(1100),
211         BANIAS(1200),
212         BANIAS(1300),
213         BANIAS(1400),
214         BANIAS(1500),
215         BANIAS(1600),
216         BANIAS(1700),
217         { 0, }
218 };
219 #undef _BANIAS
220 #undef BANIAS
221
222 static int centrino_cpu_init_table(struct cpufreq_policy *policy)
223 {
224         struct cpuinfo_x86 *cpu = &cpu_data[policy->cpu];
225         struct cpu_model *model;
226
227         for(model = models; model->model_name != NULL; model++)
228                 if ((strcmp(cpu->x86_model_id, model->model_name) == 0) &&
229                     (!centrino_verify_cpu_id(cpu, model->cpu_id)))
230                         break;
231         if (model->model_name == NULL) {
232                 printk(KERN_INFO PFX "no support for CPU model \"%s\": "
233                        "send /proc/cpuinfo to " MAINTAINER "\n",
234                        cpu->x86_model_id);
235                 return -ENOENT;
236         }
237
238         centrino_model = model;
239                 
240         printk(KERN_INFO PFX "found \"%s\": max frequency: %dkHz\n",
241                model->model_name, model->max_freq);
242
243         return 0;
244 }
245
246 #else
247 static inline int centrino_cpu_init_table(struct cpufreq_policy *policy) { return -ENODEV; }
248 #endif /* CONFIG_X86_SPEEDSTEP_CENTRINO_TABLE */
249
250 static int centrino_verify_cpu_id(struct cpuinfo_x86 *c, const struct cpu_id *x)
251 {
252         if ((c->x86 == x->x86) &&
253             (c->x86_vendor == x->x86_vendor) &&
254             (c->x86_model == x->x86_model) &&
255             (c->x86_mask == x->x86_mask))
256                 return 0;
257         return -ENODEV;
258 }
259
260 /* Extract clock in kHz from PERF_CTL value */
261 static unsigned extract_clock(unsigned msr)
262 {
263         msr = (msr >> 8) & 0xff;
264         return msr * 100000;
265 }
266
267 /* Return the current CPU frequency in kHz */
268 static unsigned int get_cur_freq(unsigned int cpu)
269 {
270         unsigned l, h;
271         if (cpu)
272                 return 0;
273
274         rdmsr(MSR_IA32_PERF_STATUS, l, h);
275         return extract_clock(l);
276 }
277
278
279 #ifdef CONFIG_X86_SPEEDSTEP_CENTRINO_ACPI
280
281 static struct acpi_processor_performance p;
282
283 #include <linux/acpi.h>
284 #include <acpi/processor.h>
285
286 #define ACPI_PDC_CAPABILITY_ENHANCED_SPEEDSTEP 0x1
287
288 /*
289  * centrino_cpu_init_acpi - register with ACPI P-States library 
290  *
291  * Register with the ACPI P-States library (part of drivers/acpi/processor.c)
292  * in order to determine correct frequency and voltage pairings by reading
293  * the _PSS of the ACPI DSDT or SSDT tables.
294  */
295 static int centrino_cpu_init_acpi(struct cpufreq_policy *policy)
296 {
297         union acpi_object               arg0 = {ACPI_TYPE_BUFFER};
298         u32                             arg0_buf[3];
299         struct acpi_object_list         arg_list = {1, &arg0};
300         unsigned long                   cur_freq;
301         int                             result = 0, i;
302
303         /* _PDC settings */
304         arg0.buffer.length = 12;
305         arg0.buffer.pointer = (u8 *) arg0_buf;
306         arg0_buf[0] = ACPI_PDC_REVISION_ID;
307         arg0_buf[1] = 1;
308         arg0_buf[2] = ACPI_PDC_CAPABILITY_ENHANCED_SPEEDSTEP;
309
310         p.pdc = &arg_list;
311
312         /* register with ACPI core */
313         if (acpi_processor_register_performance(&p, 0))
314                 return -EIO;
315
316         /* verify the acpi_data */
317         if (p.state_count <= 1) {
318                 printk(KERN_DEBUG "No P-States\n");
319                 result = -ENODEV;
320                 goto err_unreg;
321         }
322
323         if ((p.control_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) ||
324             (p.status_register.space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)) {
325                 printk(KERN_DEBUG "Invalid control/status registers\n");
326                 result = -EIO;
327                 goto err_unreg;
328         }
329
330         for (i=0; i<p.state_count; i++) {
331                 if (p.states[i].control != p.states[i].status) {
332                         printk(KERN_DEBUG "Different control and status values\n");
333                         result = -EINVAL;
334                         goto err_unreg;
335                 }
336
337                 if (!p.states[i].core_frequency) {
338                         printk(KERN_DEBUG "Zero core frequency\n");
339                         result = -EINVAL;
340                         goto err_unreg;
341                 }
342
343                 if (extract_clock(p.states[i].control) != 
344                     (p.states[i].core_frequency * 1000)) {
345                         printk(KERN_DEBUG "Invalid encoded frequency\n");
346                         result = -EINVAL;
347                         goto err_unreg;
348                 }
349         }
350
351         centrino_model = kmalloc(sizeof(struct cpu_model), GFP_KERNEL);
352         if (!centrino_model) {
353                 result = -ENOMEM;
354                 goto err_unreg;
355         }
356         memset(centrino_model, 0, sizeof(struct cpu_model));
357
358         centrino_model->model_name=NULL;
359         centrino_model->max_freq = p.states[0].core_frequency * 1000;
360         centrino_model->op_points =  kmalloc(sizeof(struct cpufreq_frequency_table) * 
361                                              (p.state_count + 1), GFP_KERNEL);
362         if (!centrino_model->op_points) {
363                 result = -ENOMEM;
364                 goto err_kfree;
365         }
366
367         cur_freq = get_cur_freq(0);
368
369         for (i=0; i<p.state_count; i++) {
370                 centrino_model->op_points[i].index = p.states[i].control;
371                 centrino_model->op_points[i].frequency = p.states[i].core_frequency * 1000;
372                 if (cur_freq == centrino_model->op_points[i].frequency)
373                         p.state = i;
374         }
375         centrino_model->op_points[p.state_count].frequency = CPUFREQ_TABLE_END;
376
377         return 0;
378
379  err_kfree:
380         kfree(centrino_model);
381  err_unreg:
382         acpi_processor_unregister_performance(&p, 0);
383         return (result);
384 }
385 #else
386 static inline int centrino_cpu_init_acpi(struct cpufreq_policy *policy) { return -ENODEV; }
387 #endif
388
389 static int centrino_cpu_init(struct cpufreq_policy *policy)
390 {
391         struct cpuinfo_x86 *cpu = &cpu_data[policy->cpu];
392         unsigned freq;
393         unsigned l, h;
394         int ret;
395
396         if (policy->cpu != 0)
397                 return -ENODEV;
398
399         if (!cpu_has(cpu, X86_FEATURE_EST))
400                 return -ENODEV;
401
402         if ((centrino_verify_cpu_id(cpu, &cpu_id_banias)) &&
403             (centrino_verify_cpu_id(cpu, &cpu_id_dothan_a1))) {
404                 printk(KERN_INFO PFX "found unsupported CPU with Enhanced SpeedStep: "
405                        "send /proc/cpuinfo to " MAINTAINER "\n");
406                 return -ENODEV;
407         }
408
409         if (centrino_cpu_init_acpi(policy)) {
410                 if (centrino_cpu_init_table(policy)) {
411                         return -ENODEV;
412                 }
413         }
414
415         /* Check to see if Enhanced SpeedStep is enabled, and try to
416            enable it if not. */
417         rdmsr(MSR_IA32_MISC_ENABLE, l, h);
418                 
419         if (!(l & (1<<16))) {
420                 l |= (1<<16);
421                 wrmsr(MSR_IA32_MISC_ENABLE, l, h);
422                 
423                 /* check to see if it stuck */
424                 rdmsr(MSR_IA32_MISC_ENABLE, l, h);
425                 if (!(l & (1<<16))) {
426                         printk(KERN_INFO PFX "couldn't enable Enhanced SpeedStep\n");
427                         return -ENODEV;
428                 }
429         }
430
431         freq = get_cur_freq(0);
432
433         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
434         policy->cpuinfo.transition_latency = 10000; /* 10uS transition latency */
435         policy->cur = freq;
436
437         dprintk(KERN_INFO PFX "centrino_cpu_init: policy=%d cur=%dkHz\n",
438                 policy->policy, policy->cur);
439         
440         ret = cpufreq_frequency_table_cpuinfo(policy, centrino_model->op_points);
441         if (ret)
442                 return (ret);
443
444         cpufreq_frequency_table_get_attr(centrino_model->op_points, policy->cpu);
445
446         return 0;
447 }
448
449 static int centrino_cpu_exit(struct cpufreq_policy *policy)
450 {
451         if (!centrino_model)
452                 return -ENODEV;
453
454         cpufreq_frequency_table_put_attr(policy->cpu);
455
456 #ifdef CONFIG_X86_SPEEDSTEP_CENTRINO_ACPI
457         if (!centrino_model->model_name) {
458                 acpi_processor_unregister_performance(&p, 0);
459                 kfree(centrino_model->op_points);
460                 kfree(centrino_model);
461         }
462 #endif
463
464         centrino_model = NULL;
465
466         return 0;
467 }
468
469 /**
470  * centrino_verify - verifies a new CPUFreq policy
471  * @policy: new policy
472  *
473  * Limit must be within this model's frequency range at least one
474  * border included.
475  */
476 static int centrino_verify (struct cpufreq_policy *policy)
477 {
478         return cpufreq_frequency_table_verify(policy, centrino_model->op_points);
479 }
480
481 /**
482  * centrino_setpolicy - set a new CPUFreq policy
483  * @policy: new policy
484  * @target_freq: the target frequency
485  * @relation: how that frequency relates to achieved frequency (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
486  *
487  * Sets a new CPUFreq policy.
488  */
489 static int centrino_target (struct cpufreq_policy *policy,
490                             unsigned int target_freq,
491                             unsigned int relation)
492 {
493         unsigned int    newstate = 0;
494         unsigned int    msr, oldmsr, h;
495         struct cpufreq_freqs    freqs;
496
497         if (centrino_model == NULL)
498                 return -ENODEV;
499
500         if (cpufreq_frequency_table_target(policy, centrino_model->op_points, target_freq,
501                                            relation, &newstate))
502                 return -EINVAL;
503
504         msr = centrino_model->op_points[newstate].index;
505         rdmsr(MSR_IA32_PERF_CTL, oldmsr, h);
506
507         if (msr == (oldmsr & 0xffff))
508                 return 0;
509
510         /* Hm, old frequency can either be the last value we put in
511            PERF_CTL, or whatever it is now. The trouble is that TM2
512            can change it behind our back, which means we never get to
513            see the speed change.  Reading back the current speed would
514            tell us something happened, but it may leave the things on
515            the notifier chain confused; we therefore stick to using
516            the last programmed speed rather than the current speed for
517            "old". 
518
519            TODO: work out how the TCC interrupts work, and try to
520            catch the CPU changing things under us.
521         */
522         freqs.cpu = 0;
523         freqs.old = extract_clock(oldmsr);
524         freqs.new = extract_clock(msr);
525         
526         dprintk(KERN_INFO PFX "target=%dkHz old=%d new=%d msr=%04x\n",
527                 target_freq, freqs.old, freqs.new, msr);
528
529         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);   
530
531         /* all but 16 LSB are "reserved", so treat them with
532            care */
533         oldmsr &= ~0xffff;
534         msr &= 0xffff;
535         oldmsr |= msr;
536         
537         wrmsr(MSR_IA32_PERF_CTL, oldmsr, h);
538
539         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
540
541         return 0;
542 }
543
544 static struct freq_attr* centrino_attr[] = {
545         &cpufreq_freq_attr_scaling_available_freqs,
546         NULL,
547 };
548
549 static struct cpufreq_driver centrino_driver = {
550         .name           = "centrino", /* should be speedstep-centrino, 
551                                          but there's a 16 char limit */
552         .init           = centrino_cpu_init,
553         .exit           = centrino_cpu_exit,
554         .verify         = centrino_verify,
555         .target         = centrino_target,
556         .get            = get_cur_freq,
557         .attr           = centrino_attr,
558         .owner          = THIS_MODULE,
559 };
560
561
562 /**
563  * centrino_init - initializes the Enhanced SpeedStep CPUFreq driver
564  *
565  * Initializes the Enhanced SpeedStep support. Returns -ENODEV on
566  * unsupported devices, -ENOENT if there's no voltage table for this
567  * particular CPU model, -EINVAL on problems during initiatization,
568  * and zero on success.
569  *
570  * This is quite picky.  Not only does the CPU have to advertise the
571  * "est" flag in the cpuid capability flags, we look for a specific
572  * CPU model and stepping, and we need to have the exact model name in
573  * our voltage tables.  That is, be paranoid about not releasing
574  * someone's valuable magic smoke.
575  */
576 static int __init centrino_init(void)
577 {
578         struct cpuinfo_x86 *cpu = cpu_data;
579
580         if (!cpu_has(cpu, X86_FEATURE_EST))
581                 return -ENODEV;
582
583         return cpufreq_register_driver(&centrino_driver);
584 }
585
586 static void __exit centrino_exit(void)
587 {
588         cpufreq_unregister_driver(&centrino_driver);
589 }
590
591 MODULE_AUTHOR ("Jeremy Fitzhardinge <jeremy@goop.org>");
592 MODULE_DESCRIPTION ("Enhanced SpeedStep driver for Intel Pentium M processors.");
593 MODULE_LICENSE ("GPL");
594
595 late_initcall(centrino_init);
596 module_exit(centrino_exit);