2 * Intel SpeedStep SMI driver.
4 * (C) 2003 Hiroshi Miura <miura@da-cha.org>
6 * Licensed under the terms of the GNU GPL License version 2.
11 /*********************************************************************
12 * SPEEDSTEP - DEFINITIONS *
13 *********************************************************************/
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>
25 #include "speedstep-lib.h"
27 /* speedstep system management interface port/command.
29 * These parameters are got from IST-SMI BIOS call.
30 * If user gives it, these are used.
33 static int smi_port
= 0;
34 static int smi_cmd
= 0;
35 static unsigned int smi_sig
= 0;
37 /* info about the processor */
38 static unsigned int speedstep_processor
= 0;
41 * There are only two frequency states for each processor. Values
42 * are in kHz for the time being.
44 static struct cpufreq_frequency_table speedstep_freqs
[] = {
47 {0, CPUFREQ_TABLE_END
},
50 #define GET_SPEEDSTEP_OWNER 0
51 #define GET_SPEEDSTEP_STATE 1
52 #define SET_SPEEDSTEP_STATE 2
53 #define GET_SPEEDSTEP_FREQS 4
55 /* how often shall the SMI call be tried if it failed, e.g. because
56 * of DMA activity going on? */
59 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "speedstep-smi", msg)
62 * speedstep_smi_ownership
64 static int speedstep_smi_ownership (void)
66 u32 command
, result
, magic
, dummy
;
67 u32 function
= GET_SPEEDSTEP_OWNER
;
68 unsigned char magic_data
[] = "Copyright (c) 1999 Intel Corporation";
70 command
= (smi_sig
& 0xffffff00) | (smi_cmd
& 0xff);
71 magic
= virt_to_phys(magic_data
);
73 dprintk("trying to obtain ownership with command %x at port %x\n", command
, smi_port
);
79 : "=D" (result
), "=a" (dummy
), "=b" (dummy
), "=c" (dummy
), "=d" (dummy
),
81 : "a" (command
), "b" (function
), "c" (0), "d" (smi_port
),
86 dprintk("result is %x\n", result
);
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
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).
100 static int speedstep_smi_get_freqs (unsigned int *low
, unsigned int *high
)
102 u32 command
, result
= 0, edi
, high_mhz
, low_mhz
, dummy
;
104 u32 function
= GET_SPEEDSTEP_FREQS
;
106 if (!(ist_info
.event
& 0xFFFF)) {
107 dprintk("bug #1422 -- can't read freqs from BIOS\n");
111 command
= (smi_sig
& 0xffffff00) | (smi_cmd
& 0xff);
113 dprintk("trying to determine frequencies with command %x at port %x\n", command
, smi_port
);
115 __asm__
__volatile__(
119 : "=a" (result
), "=b" (high_mhz
), "=c" (low_mhz
), "=d" (state
), "=D" (edi
), "=S" (dummy
)
120 : "a" (command
), "b" (function
), "c" (state
), "d" (smi_port
), "S" (0), "D" (0)
123 dprintk("result %x, low_freq %u, high_freq %u\n", result
, low_mhz
, high_mhz
);
125 /* abort if results are obviously incorrect... */
126 if ((high_mhz
+ low_mhz
) < 600)
129 *high
= high_mhz
* 1000;
130 *low
= low_mhz
* 1000;
136 * speedstep_get_state - set the SpeedStep state
137 * @state: processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
140 static int speedstep_get_state (void)
142 u32 function
=GET_SPEEDSTEP_STATE
;
143 u32 result
, state
, edi
, command
, dummy
;
145 command
= (smi_sig
& 0xffffff00) | (smi_cmd
& 0xff);
147 dprintk("trying to determine current setting with command %x at port %x\n", command
, smi_port
);
149 __asm__
__volatile__(
153 : "=a" (result
), "=b" (state
), "=D" (edi
), "=c" (dummy
), "=d" (dummy
), "=S" (dummy
)
154 : "a" (command
), "b" (function
), "c" (0), "d" (smi_port
), "S" (0), "D" (0)
157 dprintk("state is %x, result is %x\n", state
, result
);
164 * speedstep_set_state - set the SpeedStep state
165 * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
168 static void speedstep_set_state (unsigned int state
)
170 unsigned int result
= 0, command
, new_state
, dummy
;
172 unsigned int function
=SET_SPEEDSTEP_STATE
;
173 unsigned int retry
= 0;
179 local_irq_save(flags
);
181 command
= (smi_sig
& 0xffffff00) | (smi_cmd
& 0xff);
183 dprintk("trying to set frequency to state %u with command %x at port %x\n", state
, command
, smi_port
);
187 dprintk("retry %u, previous result %u, waiting...\n", retry
, result
);
191 __asm__
__volatile__(
195 : "=b" (new_state
), "=D" (result
), "=c" (dummy
), "=a" (dummy
),
196 "=d" (dummy
), "=S" (dummy
)
197 : "a" (command
), "b" (function
), "c" (state
), "d" (smi_port
), "S" (0), "D" (0)
199 } while ((new_state
!= state
) && (retry
<= SMI_TRIES
));
202 local_irq_restore(flags
);
204 if (new_state
== state
) {
205 dprintk("change to %u MHz succeeded after %u tries with result %u\n", (speedstep_freqs
[new_state
].frequency
/ 1000), retry
, result
);
207 printk(KERN_ERR
"cpufreq: change to state %u failed with new_state %u and result %u\n", state
, new_state
, result
);
215 * speedstep_target - set a new CPUFreq policy
216 * @policy: new policy
217 * @target_freq: new freq
220 * Sets a new CPUFreq policy/freq.
222 static int speedstep_target (struct cpufreq_policy
*policy
,
223 unsigned int target_freq
, unsigned int relation
)
225 unsigned int newstate
= 0;
226 struct cpufreq_freqs freqs
;
228 if (cpufreq_frequency_table_target(policy
, &speedstep_freqs
[0], target_freq
, relation
, &newstate
))
231 freqs
.old
= speedstep_freqs
[speedstep_get_state()].frequency
;
232 freqs
.new = speedstep_freqs
[newstate
].frequency
;
233 freqs
.cpu
= 0; /* speedstep.c is UP only driver */
235 if (freqs
.old
== freqs
.new)
238 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
239 speedstep_set_state(newstate
);
240 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
247 * speedstep_verify - verifies a new CPUFreq policy
248 * @policy: new policy
250 * Limit must be within speedstep_low_freq and speedstep_high_freq, with
251 * at least one border included.
253 static int speedstep_verify (struct cpufreq_policy
*policy
)
255 return cpufreq_frequency_table_verify(policy
, &speedstep_freqs
[0]);
259 static int speedstep_cpu_init(struct cpufreq_policy
*policy
)
262 unsigned int speed
,state
;
264 /* capability check */
265 if (policy
->cpu
!= 0)
268 result
= speedstep_smi_ownership();
270 dprintk("fails in aquiring ownership of a SMI interface.\n");
274 /* detect low and high frequency */
275 result
= speedstep_smi_get_freqs(&speedstep_freqs
[SPEEDSTEP_LOW
].frequency
,
276 &speedstep_freqs
[SPEEDSTEP_HIGH
].frequency
);
278 /* fall back to speedstep_lib.c dection mechanism: try both states out */
279 dprintk("could not detect low and high frequencies by SMI call.\n");
280 result
= speedstep_get_freqs(speedstep_processor
,
281 &speedstep_freqs
[SPEEDSTEP_LOW
].frequency
,
282 &speedstep_freqs
[SPEEDSTEP_HIGH
].frequency
,
284 &speedstep_set_state
);
287 dprintk("could not detect two different speeds -- aborting.\n");
290 dprintk("workaround worked.\n");
293 /* get current speed setting */
294 state
= speedstep_get_state();
295 speed
= speedstep_freqs
[state
].frequency
;
297 dprintk("currently at %s speed setting - %i MHz\n",
298 (speed
== speedstep_freqs
[SPEEDSTEP_LOW
].frequency
) ? "low" : "high",
301 /* cpuinfo and default policy values */
302 policy
->cpuinfo
.transition_latency
= CPUFREQ_ETERNAL
;
305 result
= cpufreq_frequency_table_cpuinfo(policy
, speedstep_freqs
);
309 cpufreq_frequency_table_get_attr(speedstep_freqs
, policy
->cpu
);
314 static int speedstep_cpu_exit(struct cpufreq_policy
*policy
)
316 cpufreq_frequency_table_put_attr(policy
->cpu
);
320 static unsigned int speedstep_get(unsigned int cpu
)
324 return speedstep_get_processor_frequency(speedstep_processor
);
328 static int speedstep_resume(struct cpufreq_policy
*policy
)
330 int result
= speedstep_smi_ownership();
333 dprintk("fails in re-aquiring ownership of a SMI interface.\n");
338 static struct freq_attr
* speedstep_attr
[] = {
339 &cpufreq_freq_attr_scaling_available_freqs
,
343 static struct cpufreq_driver speedstep_driver
= {
344 .name
= "speedstep-smi",
345 .verify
= speedstep_verify
,
346 .target
= speedstep_target
,
347 .init
= speedstep_cpu_init
,
348 .exit
= speedstep_cpu_exit
,
349 .get
= speedstep_get
,
350 .resume
= speedstep_resume
,
351 .owner
= THIS_MODULE
,
352 .attr
= speedstep_attr
,
356 * speedstep_init - initializes the SpeedStep CPUFreq driver
358 * Initializes the SpeedStep support. Returns -ENODEV on unsupported
359 * BIOS, -EINVAL on problems during initiatization, and zero on
362 static int __init
speedstep_init(void)
364 speedstep_processor
= speedstep_detect_processor();
366 switch (speedstep_processor
) {
367 case SPEEDSTEP_PROCESSOR_PIII_T
:
368 case SPEEDSTEP_PROCESSOR_PIII_C
:
369 case SPEEDSTEP_PROCESSOR_PIII_C_EARLY
:
372 speedstep_processor
= 0;
375 if (!speedstep_processor
) {
376 dprintk ("No supported Intel CPU detected.\n");
380 dprintk("signature:0x%.8lx, command:0x%.8lx, event:0x%.8lx, perf_level:0x%.8lx.\n",
381 ist_info
.signature
, ist_info
.command
, ist_info
.event
, ist_info
.perf_level
);
383 /* Error if no IST-SMI BIOS or no PARM
384 sig= 'ISGE' aka 'Intel Speedstep Gate E' */
385 if ((ist_info
.signature
!= 0x47534943) && (
386 (smi_port
== 0) || (smi_cmd
== 0)))
390 smi_sig
= 0x47534943;
392 smi_sig
= ist_info
.signature
;
394 /* setup smi_port from MODLULE_PARM or BIOS */
395 if ((smi_port
> 0xff) || (smi_port
< 0))
397 else if (smi_port
== 0)
398 smi_port
= ist_info
.command
& 0xff;
400 if ((smi_cmd
> 0xff) || (smi_cmd
< 0))
402 else if (smi_cmd
== 0)
403 smi_cmd
= (ist_info
.command
>> 16) & 0xff;
405 return cpufreq_register_driver(&speedstep_driver
);
410 * speedstep_exit - unregisters SpeedStep support
412 * Unregisters SpeedStep support.
414 static void __exit
speedstep_exit(void)
416 cpufreq_unregister_driver(&speedstep_driver
);
419 module_param(smi_port
, int, 0444);
420 module_param(smi_cmd
, int, 0444);
421 module_param(smi_sig
, uint
, 0444);
423 MODULE_PARM_DESC(smi_port
, "Override the BIOS-given IST port with this value -- Intel's default setting is 0xb2");
424 MODULE_PARM_DESC(smi_cmd
, "Override the BIOS-given IST command with this value -- Intel's default setting is 0x82");
425 MODULE_PARM_DESC(smi_sig
, "Set to 1 to fake the IST signature when using the SMI interface.");
427 MODULE_AUTHOR ("Hiroshi Miura");
428 MODULE_DESCRIPTION ("Speedstep driver for IST applet SMI interface.");
429 MODULE_LICENSE ("GPL");
431 module_init(speedstep_init
);
432 module_exit(speedstep_exit
);