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/pci.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
25 #include "speedstep-lib.h"
27 #define PFX "speedstep-smi: "
29 /* speedstep system management interface port/command.
31 * These parameters are got from IST-SMI BIOS call.
32 * If user gives it, these are used.
35 static int smi_port = 0;
36 static int smi_cmd = 0;
37 static unsigned int smi_sig = 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? */
60 * Define it if you want verbose debug output, e.g. for bug reporting
62 //#define SPEEDSTEP_DEBUG
64 #ifdef SPEEDSTEP_DEBUG
65 #define dprintk(msg...) printk(msg)
67 #define dprintk(msg...) do { } while(0)
71 * speedstep_smi_ownership
73 static int speedstep_smi_ownership (void)
75 u32 command, result, magic;
76 u32 function = GET_SPEEDSTEP_OWNER;
77 unsigned char magic_data[] = "Copyright (c) 1999 Intel Corporation";
79 command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
80 magic = virt_to_phys(magic_data);
85 : "a" (command), "b" (function), "c" (0), "d" (smi_port), "D" (0), "S" (magic)
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, edi, high_mhz, low_mhz;
104 u32 function = GET_SPEEDSTEP_FREQS;
106 if (!(ist_info.event & 0xFFFF))
109 command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
111 __asm__ __volatile__("movl $0, %%edi\n"
113 : "=a" (result), "=b" (high_mhz), "=c" (low_mhz), "=d" (state), "=D" (edi)
114 : "a" (command), "b" (function), "c" (state), "d" (smi_port), "S" (0)
116 *high = high_mhz * 1000;
117 *low = low_mhz * 1000;
123 * speedstep_get_state - set the SpeedStep state
124 * @state: processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
127 static int speedstep_get_state (void)
129 u32 function=GET_SPEEDSTEP_STATE;
130 u32 result, state, edi, command;
132 command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
134 __asm__ __volatile__("movl $0, %%edi\n"
136 : "=a" (result), "=b" (state), "=D" (edi)
137 : "a" (command), "b" (function), "c" (0), "d" (smi_port), "S" (0)
144 * speedstep_set_state - set the SpeedStep state
145 * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
146 * @notify: whether to call cpufreq_notify_transition
149 static void speedstep_set_state (unsigned int state, unsigned int notify)
151 unsigned int old_state, result = 0, command, new_state;
153 struct cpufreq_freqs freqs;
154 unsigned int function=SET_SPEEDSTEP_STATE;
155 unsigned int retry = 0;
160 old_state = speedstep_get_state();
161 freqs.old = speedstep_freqs[old_state].frequency;
162 freqs.new = speedstep_freqs[state].frequency;
163 freqs.cpu = 0; /* speedstep.c is UP only driver */
165 if (old_state == state)
169 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
172 local_irq_save(flags);
174 command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff);
178 dprintk(KERN_INFO "cpufreq: retry %u, previous result %u\n", retry, result);
182 __asm__ __volatile__(
185 : "=b" (new_state), "=D" (result)
186 : "a" (command), "b" (function), "c" (state), "d" (smi_port), "S" (0)
188 } while ((new_state != state) && (retry <= SMI_TRIES));
191 local_irq_restore(flags);
193 if (new_state == state) {
194 dprintk(KERN_INFO "cpufreq: change to %u MHz succeeded after %u tries with result %u\n", (freqs.new / 1000), retry, result);
196 printk(KERN_ERR "cpufreq: change failed with new_state %u and result %u\n", new_state, result);
200 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
207 * speedstep_target - set a new CPUFreq policy
208 * @policy: new policy
209 * @target_freq: new freq
212 * Sets a new CPUFreq policy/freq.
214 static int speedstep_target (struct cpufreq_policy *policy,
215 unsigned int target_freq, unsigned int relation)
217 unsigned int newstate = 0;
219 if (cpufreq_frequency_table_target(policy, &speedstep_freqs[0], target_freq, relation, &newstate))
222 speedstep_set_state(newstate, 1);
229 * speedstep_verify - verifies a new CPUFreq policy
230 * @policy: new policy
232 * Limit must be within speedstep_low_freq and speedstep_high_freq, with
233 * at least one border included.
235 static int speedstep_verify (struct cpufreq_policy *policy)
237 return cpufreq_frequency_table_verify(policy, &speedstep_freqs[0]);
241 static int speedstep_cpu_init(struct cpufreq_policy *policy)
244 unsigned int speed,state;
246 /* capability check */
247 if (policy->cpu != 0)
250 result = speedstep_smi_ownership();
252 dprintk(KERN_INFO "cpufreq: fails an aquiring ownership of a SMI interface.\n");
256 /* detect low and high frequency */
257 result = speedstep_smi_get_freqs(&speedstep_freqs[SPEEDSTEP_LOW].frequency,
258 &speedstep_freqs[SPEEDSTEP_HIGH].frequency);
260 /* fall back to speedstep_lib.c dection mechanism: try both states out */
261 unsigned int speedstep_processor = speedstep_detect_processor();
263 dprintk(KERN_INFO PFX "could not detect low and high frequencies by SMI call.\n");
264 if (!speedstep_processor)
267 result = speedstep_get_freqs(speedstep_processor,
268 &speedstep_freqs[SPEEDSTEP_LOW].frequency,
269 &speedstep_freqs[SPEEDSTEP_HIGH].frequency,
270 &speedstep_set_state);
273 dprintk(KERN_INFO PFX "could not detect two different speeds -- aborting.\n");
276 dprintk(KERN_INFO PFX "workaround worked.\n");
279 /* get current speed setting */
280 state = speedstep_get_state();
281 speed = speedstep_freqs[state].frequency;
283 dprintk(KERN_INFO "cpufreq: currently at %s speed setting - %i MHz\n",
284 (speed == speedstep_freqs[SPEEDSTEP_LOW].frequency) ? "low" : "high",
287 /* cpuinfo and default policy values */
288 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
289 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
292 result = cpufreq_frequency_table_cpuinfo(policy, speedstep_freqs);
296 cpufreq_frequency_table_get_attr(speedstep_freqs, policy->cpu);
302 static int speedstep_cpu_exit(struct cpufreq_policy *policy)
304 cpufreq_frequency_table_put_attr(policy->cpu);
309 static int speedstep_resume(struct cpufreq_policy *policy)
311 int result = speedstep_smi_ownership();
314 dprintk(KERN_INFO "cpufreq: fails an aquiring ownership of a SMI interface.\n");
319 static struct freq_attr* speedstep_attr[] = {
320 &cpufreq_freq_attr_scaling_available_freqs,
324 static struct cpufreq_driver speedstep_driver = {
325 .name = "speedstep-smi",
326 .verify = speedstep_verify,
327 .target = speedstep_target,
328 .init = speedstep_cpu_init,
329 .exit = speedstep_cpu_exit,
330 .resume = speedstep_resume,
331 .owner = THIS_MODULE,
332 .attr = speedstep_attr,
336 * speedstep_init - initializes the SpeedStep CPUFreq driver
338 * Initializes the SpeedStep support. Returns -ENODEV on unsupported
339 * BIOS, -EINVAL on problems during initiatization, and zero on
342 static int __init speedstep_init(void)
344 struct cpuinfo_x86 *c = cpu_data;
346 if (c->x86_vendor != X86_VENDOR_INTEL) {
347 printk (KERN_INFO PFX "No Intel CPU detected.\n");
351 dprintk(KERN_DEBUG PFX "signature:0x%.8lx, command:0x%.8lx, event:0x%.8lx, perf_level:0x%.8lx.\n",
352 ist_info.signature, ist_info.command, ist_info.event, ist_info.perf_level);
355 /* Error if no IST-SMI BIOS or no PARM
356 sig= 'ISGE' aka 'Intel Speedstep Gate E' */
357 if ((ist_info.signature != 0x47534943) && (
358 (smi_port == 0) || (smi_cmd == 0)))
362 smi_sig = 0x47534943;
364 smi_sig = ist_info.signature;
366 /* setup smi_port from MODLULE_PARM or BIOS */
367 if ((smi_port > 0xff) || (smi_port < 0)) {
369 } else if (smi_port == 0) {
370 smi_port = ist_info.command & 0xff;
373 if ((smi_cmd > 0xff) || (smi_cmd < 0)) {
375 } else if (smi_cmd == 0) {
376 smi_cmd = (ist_info.command >> 16) & 0xff;
379 return cpufreq_register_driver(&speedstep_driver);
384 * speedstep_exit - unregisters SpeedStep support
386 * Unregisters SpeedStep support.
388 static void __exit speedstep_exit(void)
390 cpufreq_unregister_driver(&speedstep_driver);
393 module_param(smi_port, int, 0444);
394 module_param(smi_cmd, int, 0444);
395 module_param(smi_sig, uint, 0444);
397 MODULE_PARM_DESC(smi_port, "Override the BIOS-given IST port with this value -- Intel's default setting is 0xb2");
398 MODULE_PARM_DESC(smi_cmd, "Override the BIOS-given IST command with this value -- Intel's default setting is 0x82");
399 MODULE_PARM_DESC(smi_sig, "Set to 1 to fake the IST signature when using the SMI interface.");
401 MODULE_AUTHOR ("Hiroshi Miura");
402 MODULE_DESCRIPTION ("Speedstep driver for IST applet SMI interface.");
403 MODULE_LICENSE ("GPL");
405 module_init(speedstep_init);
406 module_exit(speedstep_exit);