ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / i386 / kernel / cpu / cpufreq / speedstep-smi.c
1 /*
2  * Intel SpeedStep SMI driver.
3  *
4  * (C) 2003  Hiroshi Miura <miura@da-cha.org>
5  *
6  *  Licensed under the terms of the GNU GPL License version 2.
7  *
8  */
9
10
11 /*********************************************************************
12  *                        SPEEDSTEP - DEFINITIONS                    *
13  *********************************************************************/
14
15 #include <linux/kernel.h>
16 #include <linux/module.h> 
17 #include <linux/moduleparam.h> 
18 #include <linux/init.h>
19 #include <linux/cpufreq.h>
20 #include <linux/pci.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <asm/ist.h>
24
25 #include "speedstep-lib.h"
26
27 #define PFX "speedstep-smi: "
28
29 /* speedstep system management interface port/command.
30  *
31  * These parameters are got from IST-SMI BIOS call.
32  * If user gives it, these are used.
33  * 
34  */
35 static int              smi_port        = 0;
36 static int              smi_cmd         = 0;
37 static unsigned int     smi_sig         = 0;
38
39
40 /* 
41  *   There are only two frequency states for each processor. Values
42  * are in kHz for the time being.
43  */
44 static struct cpufreq_frequency_table speedstep_freqs[] = {
45         {SPEEDSTEP_HIGH,        0},
46         {SPEEDSTEP_LOW,         0},
47         {0,                     CPUFREQ_TABLE_END},
48 };
49
50 #define GET_SPEEDSTEP_OWNER 0
51 #define GET_SPEEDSTEP_STATE 1
52 #define SET_SPEEDSTEP_STATE 2
53 #define GET_SPEEDSTEP_FREQS 4
54
55 /* how often shall the SMI call be tried if it failed, e.g. because
56  * of DMA activity going on? */
57 #define SMI_TRIES 5
58
59 /* DEBUG
60  *   Define it if you want verbose debug output, e.g. for bug reporting
61  */
62 //#define SPEEDSTEP_DEBUG
63
64 #ifdef SPEEDSTEP_DEBUG
65 #define dprintk(msg...) printk(msg)
66 #else
67 #define dprintk(msg...) do { } while(0)
68 #endif
69
70 /**
71  * speedstep_smi_ownership
72  */
73 static int speedstep_smi_ownership (void)
74 {
75         u32 command, result, magic;
76         u32 function = GET_SPEEDSTEP_OWNER;
77         unsigned char magic_data[] = "Copyright (c) 1999 Intel Corporation";
78
79         command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
80         magic = virt_to_phys(magic_data);
81
82         __asm__ __volatile__(
83                 "out %%al, (%%dx)\n"
84                 : "=D" (result)
85                 : "a" (command), "b" (function), "c" (0), "d" (smi_port), "D" (0), "S" (magic)
86         );
87
88         return result;
89 }
90
91 /**
92  * speedstep_smi_get_freqs - get SpeedStep preferred & current freq.
93  * @low: the low frequency value is placed here
94  * @high: the high frequency value is placed here
95  *
96  * Only available on later SpeedStep-enabled systems, returns false results or
97  * even hangs [cf. bugme.osdl.org # 1422] on earlier systems. Empirical testing
98  * shows that the latter occurs if !(ist_info.event & 0xFFFF).
99  */
100 static int speedstep_smi_get_freqs (unsigned int *low, unsigned int *high)
101 {
102         u32 command, result, edi, high_mhz, low_mhz;
103         u32 state=0;
104         u32 function = GET_SPEEDSTEP_FREQS;
105
106         if (!(ist_info.event & 0xFFFF))
107                 return -ENODEV;
108
109         command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
110
111         __asm__ __volatile__("movl $0, %%edi\n"
112                 "out %%al, (%%dx)\n"
113                 : "=a" (result), "=b" (high_mhz), "=c" (low_mhz), "=d" (state), "=D" (edi)
114                 : "a" (command), "b" (function), "c" (state), "d" (smi_port), "S" (0)
115         );
116         *high = high_mhz * 1000;
117         *low  = low_mhz  * 1000;
118
119         return result;
120
121
122 /**
123  * speedstep_get_state - set the SpeedStep state
124  * @state: processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
125  *
126  */
127 static int speedstep_get_state (void)
128 {
129         u32 function=GET_SPEEDSTEP_STATE;
130         u32 result, state, edi, command;
131
132         command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
133
134         __asm__ __volatile__("movl $0, %%edi\n"
135                 "out %%al, (%%dx)\n"
136                 : "=a" (result), "=b" (state), "=D" (edi)
137                 : "a" (command), "b" (function), "c" (0), "d" (smi_port), "S" (0)
138         );
139
140         return state;
141 }
142
143 /**
144  * speedstep_set_state - set the SpeedStep state
145  * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
146  * @notify: whether to call cpufreq_notify_transition
147  *
148  */
149 static void speedstep_set_state (unsigned int state, unsigned int notify)
150 {
151         unsigned int old_state, result = 0, command, new_state;
152         unsigned long flags;
153         struct cpufreq_freqs freqs;
154         unsigned int function=SET_SPEEDSTEP_STATE;
155         unsigned int retry = 0;
156
157         if (state > 0x1)
158                 return;
159
160         old_state = speedstep_get_state();
161         freqs.old = speedstep_freqs[old_state].frequency;
162         freqs.new = speedstep_freqs[state].frequency;
163         freqs.cpu = 0; /* speedstep.c is UP only driver */
164
165         if (old_state == state)
166                 return;
167
168         if (notify)
169                 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
170
171         /* Disable IRQs */
172         local_irq_save(flags);
173
174         command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
175
176         do {
177                 if (retry) {
178                         dprintk(KERN_INFO "cpufreq: retry %u, previous result %u\n", retry, result);
179                         mdelay(retry * 50);
180                 }
181                 retry++;
182                 __asm__ __volatile__(
183                         "movl $0, %%edi\n"
184                         "out %%al, (%%dx)\n"
185                         : "=b" (new_state), "=D" (result)
186                         : "a" (command), "b" (function), "c" (state), "d" (smi_port), "S" (0)
187                         );
188         } while ((new_state != state) && (retry <= SMI_TRIES));
189
190         /* enable IRQs */
191         local_irq_restore(flags);
192
193         if (new_state == state) {
194                 dprintk(KERN_INFO "cpufreq: change to %u MHz succeeded after %u tries with result %u\n", (freqs.new / 1000), retry, result);
195         } else {
196                 printk(KERN_ERR "cpufreq: change failed with new_state %u and result %u\n", new_state, result);
197         }
198
199         if (notify)
200                 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
201
202         return;
203 }
204
205
206 /**
207  * speedstep_target - set a new CPUFreq policy
208  * @policy: new policy
209  * @target_freq: new freq
210  * @relation: 
211  *
212  * Sets a new CPUFreq policy/freq.
213  */
214 static int speedstep_target (struct cpufreq_policy *policy,
215                         unsigned int target_freq, unsigned int relation)
216 {
217         unsigned int newstate = 0;
218
219         if (cpufreq_frequency_table_target(policy, &speedstep_freqs[0], target_freq, relation, &newstate))
220                 return -EINVAL;
221
222         speedstep_set_state(newstate, 1);
223
224         return 0;
225 }
226
227
228 /**
229  * speedstep_verify - verifies a new CPUFreq policy
230  * @policy: new policy
231  *
232  * Limit must be within speedstep_low_freq and speedstep_high_freq, with
233  * at least one border included.
234  */
235 static int speedstep_verify (struct cpufreq_policy *policy)
236 {
237         return cpufreq_frequency_table_verify(policy, &speedstep_freqs[0]);
238 }
239
240
241 static int speedstep_cpu_init(struct cpufreq_policy *policy)
242 {
243         int result;
244         unsigned int speed,state;
245
246         /* capability check */
247         if (policy->cpu != 0)
248                 return -ENODEV;
249
250         result = speedstep_smi_ownership();
251         if (result) {
252                 dprintk(KERN_INFO "cpufreq: fails an aquiring ownership of a SMI interface.\n");
253                 return -EINVAL;
254         }
255
256         /* detect low and high frequency */
257         result = speedstep_smi_get_freqs(&speedstep_freqs[SPEEDSTEP_LOW].frequency,
258                                 &speedstep_freqs[SPEEDSTEP_HIGH].frequency);
259         if (result) {
260                 /* fall back to speedstep_lib.c dection mechanism: try both states out */
261                 unsigned int speedstep_processor = speedstep_detect_processor();
262
263                 dprintk(KERN_INFO PFX "could not detect low and high frequencies by SMI call.\n");
264                 if (!speedstep_processor)
265                         return -ENODEV;
266
267                 result = speedstep_get_freqs(speedstep_processor,
268                                 &speedstep_freqs[SPEEDSTEP_LOW].frequency,
269                                 &speedstep_freqs[SPEEDSTEP_HIGH].frequency,
270                                 &speedstep_set_state);
271
272                 if (result) {
273                         dprintk(KERN_INFO PFX "could not detect two different speeds -- aborting.\n");
274                         return result;
275                 } else
276                         dprintk(KERN_INFO PFX "workaround worked.\n");
277         }
278
279         /* get current speed setting */
280         state = speedstep_get_state();
281         speed = speedstep_freqs[state].frequency;
282
283         dprintk(KERN_INFO "cpufreq: currently at %s speed setting - %i MHz\n", 
284                 (speed == speedstep_freqs[SPEEDSTEP_LOW].frequency) ? "low" : "high",
285                 (speed / 1000));
286
287         /* cpuinfo and default policy values */
288         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
289         policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
290         policy->cur = speed;
291
292         result = cpufreq_frequency_table_cpuinfo(policy, speedstep_freqs);
293         if (result)
294                 return (result);
295
296         cpufreq_frequency_table_get_attr(speedstep_freqs, policy->cpu);
297
298         return 0;
299 }
300
301
302 static int speedstep_cpu_exit(struct cpufreq_policy *policy)
303 {
304         cpufreq_frequency_table_put_attr(policy->cpu);
305         return 0;
306 }
307
308
309 static int speedstep_resume(struct cpufreq_policy *policy)
310 {
311         int result = speedstep_smi_ownership();
312
313         if (result)
314                 dprintk(KERN_INFO "cpufreq: fails an aquiring ownership of a SMI interface.\n");
315
316         return result;
317 }
318
319 static struct freq_attr* speedstep_attr[] = {
320         &cpufreq_freq_attr_scaling_available_freqs,
321         NULL,
322 };
323
324 static struct cpufreq_driver speedstep_driver = {
325         .name           = "speedstep-smi",
326         .verify         = speedstep_verify,
327         .target         = speedstep_target,
328         .init           = speedstep_cpu_init,
329         .exit           = speedstep_cpu_exit,
330         .resume         = speedstep_resume,
331         .owner          = THIS_MODULE,
332         .attr           = speedstep_attr,
333 };
334
335 /**
336  * speedstep_init - initializes the SpeedStep CPUFreq driver
337  *
338  *   Initializes the SpeedStep support. Returns -ENODEV on unsupported
339  * BIOS, -EINVAL on problems during initiatization, and zero on
340  * success.
341  */
342 static int __init speedstep_init(void)
343 {
344     struct cpuinfo_x86 *c = cpu_data;
345
346     if (c->x86_vendor != X86_VENDOR_INTEL) {
347                 printk (KERN_INFO PFX "No Intel CPU detected.\n");
348                 return -ENODEV;
349         }
350
351         dprintk(KERN_DEBUG PFX "signature:0x%.8lx, command:0x%.8lx, event:0x%.8lx, perf_level:0x%.8lx.\n", 
352                 ist_info.signature, ist_info.command, ist_info.event, ist_info.perf_level);
353
354
355         /* Error if no IST-SMI BIOS or no PARM 
356                  sig= 'ISGE' aka 'Intel Speedstep Gate E' */
357         if ((ist_info.signature !=  0x47534943) && ( 
358             (smi_port == 0) || (smi_cmd == 0)))
359                 return -ENODEV;
360
361         if (smi_sig == 1)
362                 smi_sig = 0x47534943;
363         else
364                 smi_sig = ist_info.signature;
365
366         /* setup smi_port from MODLULE_PARM or BIOS */
367         if ((smi_port > 0xff) || (smi_port < 0)) {
368                 return -EINVAL;
369         } else if (smi_port == 0) {
370                 smi_port = ist_info.command & 0xff;
371         }
372
373         if ((smi_cmd > 0xff) || (smi_cmd < 0)) {
374                 return -EINVAL;
375         } else if (smi_cmd == 0) {
376                 smi_cmd = (ist_info.command >> 16) & 0xff;
377         }
378
379         return cpufreq_register_driver(&speedstep_driver);
380 }
381
382
383 /**
384  * speedstep_exit - unregisters SpeedStep support
385  *
386  *   Unregisters SpeedStep support.
387  */
388 static void __exit speedstep_exit(void)
389 {
390         cpufreq_unregister_driver(&speedstep_driver);
391 }
392
393 module_param(smi_port,  int, 0444);
394 module_param(smi_cmd,   int, 0444);
395 module_param(smi_sig,  uint, 0444);
396
397 MODULE_PARM_DESC(smi_port, "Override the BIOS-given IST port with this value -- Intel's default setting is 0xb2");
398 MODULE_PARM_DESC(smi_cmd, "Override the BIOS-given IST command with this value -- Intel's default setting is 0x82");
399 MODULE_PARM_DESC(smi_sig, "Set to 1 to fake the IST signature when using the SMI interface.");
400
401 MODULE_AUTHOR ("Hiroshi Miura");
402 MODULE_DESCRIPTION ("Speedstep driver for IST applet SMI interface.");
403 MODULE_LICENSE ("GPL");
404
405 module_init(speedstep_init);
406 module_exit(speedstep_exit);