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