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