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/delay.h>
24 #include "speedstep-lib.h"
26 /* speedstep system management interface port/command.
28 * These parameters are got from IST-SMI BIOS call.
29 * If user gives it, these are used.
34 static unsigned int smi_sig
;
36 /* info about the processor */
37 static enum speedstep_processor speedstep_processor
;
40 * There are only two frequency states for each processor. Values
41 * are in kHz for the time being.
43 static struct cpufreq_frequency_table speedstep_freqs
[] = {
46 {0, CPUFREQ_TABLE_END
},
49 #define GET_SPEEDSTEP_OWNER 0
50 #define GET_SPEEDSTEP_STATE 1
51 #define SET_SPEEDSTEP_STATE 2
52 #define GET_SPEEDSTEP_FREQS 4
54 /* how often shall the SMI call be tried if it failed, e.g. because
55 * of DMA activity going on? */
58 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, \
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",
81 "=a" (dummy
), "=b" (dummy
), "=c" (dummy
), "=d" (dummy
),
83 : "a" (command
), "b" (function
), "c" (0), "d" (smi_port
),
88 dprintk("result is %x\n", result
);
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
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).
102 static int speedstep_smi_get_freqs(unsigned int *low
, unsigned int *high
)
104 u32 command
, result
= 0, edi
, high_mhz
, low_mhz
, dummy
;
106 u32 function
= GET_SPEEDSTEP_FREQS
;
108 if (!(ist_info
.event
& 0xFFFF)) {
109 dprintk("bug #1422 -- can't read freqs from BIOS\n");
113 command
= (smi_sig
& 0xffffff00) | (smi_cmd
& 0xff);
115 dprintk("trying to determine frequencies with command %x at port %x\n",
118 __asm__
__volatile__(
125 "=d" (state
), "=D" (edi
), "=S" (dummy
)
129 "d" (smi_port
), "S" (0), "D" (0)
132 dprintk("result %x, low_freq %u, high_freq %u\n",
133 result
, low_mhz
, high_mhz
);
135 /* abort if results are obviously incorrect... */
136 if ((high_mhz
+ low_mhz
) < 600)
139 *high
= high_mhz
* 1000;
140 *low
= low_mhz
* 1000;
146 * speedstep_get_state - set the SpeedStep state
147 * @state: processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
150 static int speedstep_get_state(void)
152 u32 function
= GET_SPEEDSTEP_STATE
;
153 u32 result
, state
, edi
, command
, dummy
;
155 command
= (smi_sig
& 0xffffff00) | (smi_cmd
& 0xff);
157 dprintk("trying to determine current setting with command %x "
158 "at port %x\n", command
, smi_port
);
160 __asm__
__volatile__(
165 "=b" (state
), "=D" (edi
),
166 "=c" (dummy
), "=d" (dummy
), "=S" (dummy
)
167 : "a" (command
), "b" (function
), "c" (0),
168 "d" (smi_port
), "S" (0), "D" (0)
171 dprintk("state is %x, result is %x\n", state
, result
);
178 * speedstep_set_state - set the SpeedStep state
179 * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
182 static void speedstep_set_state(unsigned int state
)
184 unsigned int result
= 0, command
, new_state
, dummy
;
186 unsigned int function
= SET_SPEEDSTEP_STATE
;
187 unsigned int retry
= 0;
193 local_irq_save(flags
);
195 command
= (smi_sig
& 0xffffff00) | (smi_cmd
& 0xff);
197 dprintk("trying to set frequency to state %u "
198 "with command %x at port %x\n",
199 state
, command
, smi_port
);
203 dprintk("retry %u, previous result %u, waiting...\n",
208 __asm__
__volatile__(
212 : "=b" (new_state
), "=D" (result
),
213 "=c" (dummy
), "=a" (dummy
),
214 "=d" (dummy
), "=S" (dummy
)
215 : "a" (command
), "b" (function
), "c" (state
),
216 "d" (smi_port
), "S" (0), "D" (0)
218 } while ((new_state
!= state
) && (retry
<= SMI_TRIES
));
221 local_irq_restore(flags
);
223 if (new_state
== state
)
224 dprintk("change to %u MHz succeeded after %u tries "
226 (speedstep_freqs
[new_state
].frequency
/ 1000),
229 printk(KERN_ERR
"cpufreq: change to state %u "
230 "failed with new_state %u and result %u\n",
231 state
, new_state
, result
);
238 * speedstep_target - set a new CPUFreq policy
239 * @policy: new policy
240 * @target_freq: new freq
243 * Sets a new CPUFreq policy/freq.
245 static int speedstep_target(struct cpufreq_policy
*policy
,
246 unsigned int target_freq
, unsigned int relation
)
248 unsigned int newstate
= 0;
249 struct cpufreq_freqs freqs
;
251 if (cpufreq_frequency_table_target(policy
, &speedstep_freqs
[0],
252 target_freq
, relation
, &newstate
))
255 freqs
.old
= speedstep_freqs
[speedstep_get_state()].frequency
;
256 freqs
.new = speedstep_freqs
[newstate
].frequency
;
257 freqs
.cpu
= 0; /* speedstep.c is UP only driver */
259 if (freqs
.old
== freqs
.new)
262 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
263 speedstep_set_state(newstate
);
264 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
271 * speedstep_verify - verifies a new CPUFreq policy
272 * @policy: new policy
274 * Limit must be within speedstep_low_freq and speedstep_high_freq, with
275 * at least one border included.
277 static int speedstep_verify(struct cpufreq_policy
*policy
)
279 return cpufreq_frequency_table_verify(policy
, &speedstep_freqs
[0]);
283 static int speedstep_cpu_init(struct cpufreq_policy
*policy
)
286 unsigned int speed
, state
;
287 unsigned int *low
, *high
;
289 /* capability check */
290 if (policy
->cpu
!= 0)
293 result
= speedstep_smi_ownership();
295 dprintk("fails in aquiring ownership of a SMI interface.\n");
299 /* detect low and high frequency */
300 low
= &speedstep_freqs
[SPEEDSTEP_LOW
].frequency
;
301 high
= &speedstep_freqs
[SPEEDSTEP_HIGH
].frequency
;
303 result
= speedstep_smi_get_freqs(low
, high
);
305 /* fall back to speedstep_lib.c dection mechanism:
306 * try both states out */
307 dprintk("could not detect low and high frequencies "
309 result
= speedstep_get_freqs(speedstep_processor
,
312 &speedstep_set_state
);
315 dprintk("could not detect two different speeds"
319 dprintk("workaround worked.\n");
322 /* get current speed setting */
323 state
= speedstep_get_state();
324 speed
= speedstep_freqs
[state
].frequency
;
326 dprintk("currently at %s speed setting - %i MHz\n",
327 (speed
== speedstep_freqs
[SPEEDSTEP_LOW
].frequency
)
331 /* cpuinfo and default policy values */
332 policy
->cpuinfo
.transition_latency
= CPUFREQ_ETERNAL
;
335 result
= cpufreq_frequency_table_cpuinfo(policy
, speedstep_freqs
);
339 cpufreq_frequency_table_get_attr(speedstep_freqs
, policy
->cpu
);
344 static int speedstep_cpu_exit(struct cpufreq_policy
*policy
)
346 cpufreq_frequency_table_put_attr(policy
->cpu
);
350 static unsigned int speedstep_get(unsigned int cpu
)
354 return speedstep_get_frequency(speedstep_processor
);
358 static int speedstep_resume(struct cpufreq_policy
*policy
)
360 int result
= speedstep_smi_ownership();
363 dprintk("fails in re-aquiring ownership of a SMI interface.\n");
368 static struct freq_attr
*speedstep_attr
[] = {
369 &cpufreq_freq_attr_scaling_available_freqs
,
373 static struct cpufreq_driver speedstep_driver
= {
374 .name
= "speedstep-smi",
375 .verify
= speedstep_verify
,
376 .target
= speedstep_target
,
377 .init
= speedstep_cpu_init
,
378 .exit
= speedstep_cpu_exit
,
379 .get
= speedstep_get
,
380 .resume
= speedstep_resume
,
381 .owner
= THIS_MODULE
,
382 .attr
= speedstep_attr
,
386 * speedstep_init - initializes the SpeedStep CPUFreq driver
388 * Initializes the SpeedStep support. Returns -ENODEV on unsupported
389 * BIOS, -EINVAL on problems during initiatization, and zero on
392 static int __init
speedstep_init(void)
394 speedstep_processor
= speedstep_detect_processor();
396 switch (speedstep_processor
) {
397 case SPEEDSTEP_CPU_PIII_T
:
398 case SPEEDSTEP_CPU_PIII_C
:
399 case SPEEDSTEP_CPU_PIII_C_EARLY
:
402 speedstep_processor
= 0;
405 if (!speedstep_processor
) {
406 dprintk("No supported Intel CPU detected.\n");
410 dprintk("signature:0x%.8lx, command:0x%.8lx, "
411 "event:0x%.8lx, perf_level:0x%.8lx.\n",
412 ist_info
.signature
, ist_info
.command
,
413 ist_info
.event
, ist_info
.perf_level
);
415 /* Error if no IST-SMI BIOS or no PARM
416 sig= 'ISGE' aka 'Intel Speedstep Gate E' */
417 if ((ist_info
.signature
!= 0x47534943) && (
418 (smi_port
== 0) || (smi_cmd
== 0)))
422 smi_sig
= 0x47534943;
424 smi_sig
= ist_info
.signature
;
426 /* setup smi_port from MODLULE_PARM or BIOS */
427 if ((smi_port
> 0xff) || (smi_port
< 0))
429 else if (smi_port
== 0)
430 smi_port
= ist_info
.command
& 0xff;
432 if ((smi_cmd
> 0xff) || (smi_cmd
< 0))
434 else if (smi_cmd
== 0)
435 smi_cmd
= (ist_info
.command
>> 16) & 0xff;
437 return cpufreq_register_driver(&speedstep_driver
);
442 * speedstep_exit - unregisters SpeedStep support
444 * Unregisters SpeedStep support.
446 static void __exit
speedstep_exit(void)
448 cpufreq_unregister_driver(&speedstep_driver
);
451 module_param(smi_port
, int, 0444);
452 module_param(smi_cmd
, int, 0444);
453 module_param(smi_sig
, uint
, 0444);
455 MODULE_PARM_DESC(smi_port
, "Override the BIOS-given IST port with this value "
456 "-- Intel's default setting is 0xb2");
457 MODULE_PARM_DESC(smi_cmd
, "Override the BIOS-given IST command with this value "
458 "-- Intel's default setting is 0x82");
459 MODULE_PARM_DESC(smi_sig
, "Set to 1 to fake the IST signature when using the "
462 MODULE_AUTHOR("Hiroshi Miura");
463 MODULE_DESCRIPTION("Speedstep driver for IST applet SMI interface.");
464 MODULE_LICENSE("GPL");
466 module_init(speedstep_init
);
467 module_exit(speedstep_exit
);