This commit was manufactured by cvs2svn to create tag
[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         *high = high_mhz * 1000;
119         *low  = low_mhz  * 1000;
120
121         return result;
122
123
124 /**
125  * speedstep_get_state - set the SpeedStep state
126  * @state: processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
127  *
128  */
129 static int speedstep_get_state (void)
130 {
131         u32 function=GET_SPEEDSTEP_STATE;
132         u32 result, state, edi, command;
133
134         command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
135
136         __asm__ __volatile__("movl $0, %%edi\n"
137                 "out %%al, (%%dx)\n"
138                 : "=a" (result), "=b" (state), "=D" (edi)
139                 : "a" (command), "b" (function), "c" (0), "d" (smi_port), "S" (0)
140         );
141
142         return state;
143 }
144
145 /**
146  * speedstep_set_state - set the SpeedStep state
147  * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
148  * @notify: whether to call cpufreq_notify_transition
149  *
150  */
151 static void speedstep_set_state (unsigned int state, unsigned int notify)
152 {
153         unsigned int old_state, result = 0, command, new_state;
154         unsigned long flags;
155         struct cpufreq_freqs freqs;
156         unsigned int function=SET_SPEEDSTEP_STATE;
157         unsigned int retry = 0;
158
159         if (state > 0x1)
160                 return;
161
162         old_state = speedstep_get_state();
163         freqs.old = speedstep_freqs[old_state].frequency;
164         freqs.new = speedstep_freqs[state].frequency;
165         freqs.cpu = 0; /* speedstep.c is UP only driver */
166
167         if (old_state == state)
168                 return;
169
170         if (notify)
171                 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
172
173         /* Disable IRQs */
174         local_irq_save(flags);
175
176         command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
177
178         do {
179                 if (retry) {
180                         dprintk(KERN_INFO "cpufreq: retry %u, previous result %u\n", retry, result);
181                         mdelay(retry * 50);
182                 }
183                 retry++;
184                 __asm__ __volatile__(
185                         "movl $0, %%edi\n"
186                         "out %%al, (%%dx)\n"
187                         : "=b" (new_state), "=D" (result)
188                         : "a" (command), "b" (function), "c" (state), "d" (smi_port), "S" (0)
189                         );
190         } while ((new_state != state) && (retry <= SMI_TRIES));
191
192         /* enable IRQs */
193         local_irq_restore(flags);
194
195         if (new_state == state) {
196                 dprintk(KERN_INFO "cpufreq: change to %u MHz succeeded after %u tries with result %u\n", (freqs.new / 1000), retry, result);
197         } else {
198                 printk(KERN_ERR "cpufreq: change failed with new_state %u and result %u\n", new_state, result);
199         }
200
201         if (notify)
202                 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
203
204         return;
205 }
206
207
208 /**
209  * speedstep_target - set a new CPUFreq policy
210  * @policy: new policy
211  * @target_freq: new freq
212  * @relation: 
213  *
214  * Sets a new CPUFreq policy/freq.
215  */
216 static int speedstep_target (struct cpufreq_policy *policy,
217                         unsigned int target_freq, unsigned int relation)
218 {
219         unsigned int newstate = 0;
220
221         if (cpufreq_frequency_table_target(policy, &speedstep_freqs[0], target_freq, relation, &newstate))
222                 return -EINVAL;
223
224         speedstep_set_state(newstate, 1);
225
226         return 0;
227 }
228
229
230 /**
231  * speedstep_verify - verifies a new CPUFreq policy
232  * @policy: new policy
233  *
234  * Limit must be within speedstep_low_freq and speedstep_high_freq, with
235  * at least one border included.
236  */
237 static int speedstep_verify (struct cpufreq_policy *policy)
238 {
239         return cpufreq_frequency_table_verify(policy, &speedstep_freqs[0]);
240 }
241
242
243 static int speedstep_cpu_init(struct cpufreq_policy *policy)
244 {
245         int result;
246         unsigned int speed,state;
247
248         /* capability check */
249         if (policy->cpu != 0)
250                 return -ENODEV;
251
252         result = speedstep_smi_ownership();
253         if (result) {
254                 dprintk(KERN_INFO "cpufreq: fails an aquiring ownership of a SMI interface.\n");
255                 return -EINVAL;
256         }
257
258         /* detect low and high frequency */
259         result = speedstep_smi_get_freqs(&speedstep_freqs[SPEEDSTEP_LOW].frequency,
260                                 &speedstep_freqs[SPEEDSTEP_HIGH].frequency);
261         if (result) {
262                 /* fall back to speedstep_lib.c dection mechanism: try both states out */
263                 dprintk(KERN_INFO PFX "could not detect low and high frequencies by SMI call.\n");
264                 if (!speedstep_processor)
265                         speedstep_processor = speedstep_detect_processor();
266
267                 if (!speedstep_processor)
268                         return -ENODEV;
269
270                 result = speedstep_get_freqs(speedstep_processor,
271                                 &speedstep_freqs[SPEEDSTEP_LOW].frequency,
272                                 &speedstep_freqs[SPEEDSTEP_HIGH].frequency,
273                                 &speedstep_set_state);
274
275                 if (result) {
276                         dprintk(KERN_INFO PFX "could not detect two different speeds -- aborting.\n");
277                         return result;
278                 } else
279                         dprintk(KERN_INFO PFX "workaround worked.\n");
280         }
281
282         /* get current speed setting */
283         state = speedstep_get_state();
284         speed = speedstep_freqs[state].frequency;
285
286         dprintk(KERN_INFO "cpufreq: currently at %s speed setting - %i MHz\n", 
287                 (speed == speedstep_freqs[SPEEDSTEP_LOW].frequency) ? "low" : "high",
288                 (speed / 1000));
289
290         /* cpuinfo and default policy values */
291         policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
292         policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
293         policy->cur = speed;
294
295         result = cpufreq_frequency_table_cpuinfo(policy, speedstep_freqs);
296         if (result)
297                 return (result);
298
299         cpufreq_frequency_table_get_attr(speedstep_freqs, policy->cpu);
300
301         return 0;
302 }
303
304 static int speedstep_cpu_exit(struct cpufreq_policy *policy)
305 {
306         cpufreq_frequency_table_put_attr(policy->cpu);
307         return 0;
308 }
309
310 static unsigned int speedstep_get(unsigned int cpu)
311 {
312         if (cpu)
313                 return -ENODEV;
314         if (!speedstep_processor)
315                 speedstep_processor = speedstep_detect_processor();
316         if (!speedstep_processor)
317                 return 0;
318         return speedstep_get_processor_frequency(speedstep_processor);
319 }
320
321
322 static int speedstep_resume(struct cpufreq_policy *policy)
323 {
324         int result = speedstep_smi_ownership();
325
326         if (result)
327                 dprintk(KERN_INFO "cpufreq: fails an aquiring ownership of a SMI interface.\n");
328
329         return result;
330 }
331
332 static struct freq_attr* speedstep_attr[] = {
333         &cpufreq_freq_attr_scaling_available_freqs,
334         NULL,
335 };
336
337 static struct cpufreq_driver speedstep_driver = {
338         .name           = "speedstep-smi",
339         .verify         = speedstep_verify,
340         .target         = speedstep_target,
341         .init           = speedstep_cpu_init,
342         .exit           = speedstep_cpu_exit,
343         .get            = speedstep_get,
344         .resume         = speedstep_resume,
345         .owner          = THIS_MODULE,
346         .attr           = speedstep_attr,
347 };
348
349 /**
350  * speedstep_init - initializes the SpeedStep CPUFreq driver
351  *
352  *   Initializes the SpeedStep support. Returns -ENODEV on unsupported
353  * BIOS, -EINVAL on problems during initiatization, and zero on
354  * success.
355  */
356 static int __init speedstep_init(void)
357 {
358     struct cpuinfo_x86 *c = cpu_data;
359
360     if (c->x86_vendor != X86_VENDOR_INTEL) {
361                 printk (KERN_INFO PFX "No Intel CPU detected.\n");
362                 return -ENODEV;
363         }
364
365         dprintk(KERN_DEBUG PFX "signature:0x%.8lx, command:0x%.8lx, event:0x%.8lx, perf_level:0x%.8lx.\n", 
366                 ist_info.signature, ist_info.command, ist_info.event, ist_info.perf_level);
367
368
369         /* Error if no IST-SMI BIOS or no PARM 
370                  sig= 'ISGE' aka 'Intel Speedstep Gate E' */
371         if ((ist_info.signature !=  0x47534943) && ( 
372             (smi_port == 0) || (smi_cmd == 0)))
373                 return -ENODEV;
374
375         if (smi_sig == 1)
376                 smi_sig = 0x47534943;
377         else
378                 smi_sig = ist_info.signature;
379
380         /* setup smi_port from MODLULE_PARM or BIOS */
381         if ((smi_port > 0xff) || (smi_port < 0)) {
382                 return -EINVAL;
383         } else if (smi_port == 0) {
384                 smi_port = ist_info.command & 0xff;
385         }
386
387         if ((smi_cmd > 0xff) || (smi_cmd < 0)) {
388                 return -EINVAL;
389         } else if (smi_cmd == 0) {
390                 smi_cmd = (ist_info.command >> 16) & 0xff;
391         }
392
393         return cpufreq_register_driver(&speedstep_driver);
394 }
395
396
397 /**
398  * speedstep_exit - unregisters SpeedStep support
399  *
400  *   Unregisters SpeedStep support.
401  */
402 static void __exit speedstep_exit(void)
403 {
404         cpufreq_unregister_driver(&speedstep_driver);
405 }
406
407 module_param(smi_port,  int, 0444);
408 module_param(smi_cmd,   int, 0444);
409 module_param(smi_sig,  uint, 0444);
410
411 MODULE_PARM_DESC(smi_port, "Override the BIOS-given IST port with this value -- Intel's default setting is 0xb2");
412 MODULE_PARM_DESC(smi_cmd, "Override the BIOS-given IST command with this value -- Intel's default setting is 0x82");
413 MODULE_PARM_DESC(smi_sig, "Set to 1 to fake the IST signature when using the SMI interface.");
414
415 MODULE_AUTHOR ("Hiroshi Miura");
416 MODULE_DESCRIPTION ("Speedstep driver for IST applet SMI interface.");
417 MODULE_LICENSE ("GPL");
418
419 module_init(speedstep_init);
420 module_exit(speedstep_exit);