]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
32ee8c3e DJ |
16 | #include <linux/module.h> |
17 | #include <linux/moduleparam.h> | |
1da177e4 LT |
18 | #include <linux/init.h> |
19 | #include <linux/cpufreq.h> | |
1da177e4 | 20 | #include <linux/delay.h> |
bbfebd66 | 21 | #include <linux/io.h> |
1da177e4 LT |
22 | #include <asm/ist.h> |
23 | ||
24 | #include "speedstep-lib.h" | |
25 | ||
26 | /* speedstep system management interface port/command. | |
27 | * | |
28 | * These parameters are got from IST-SMI BIOS call. | |
29 | * If user gives it, these are used. | |
32ee8c3e | 30 | * |
1da177e4 | 31 | */ |
bbfebd66 DJ |
32 | static int smi_port; |
33 | static int smi_cmd; | |
34 | static unsigned int smi_sig; | |
1da177e4 LT |
35 | |
36 | /* info about the processor */ | |
1cce76c2 | 37 | static enum speedstep_processor speedstep_processor; |
1da177e4 | 38 | |
32ee8c3e DJ |
39 | /* |
40 | * There are only two frequency states for each processor. Values | |
1da177e4 LT |
41 | * are in kHz for the time being. |
42 | */ | |
43 | static struct cpufreq_frequency_table speedstep_freqs[] = { | |
32ee8c3e | 44 | {SPEEDSTEP_HIGH, 0}, |
1da177e4 LT |
45 | {SPEEDSTEP_LOW, 0}, |
46 | {0, CPUFREQ_TABLE_END}, | |
47 | }; | |
48 | ||
49 | #define GET_SPEEDSTEP_OWNER 0 | |
50 | #define GET_SPEEDSTEP_STATE 1 | |
51 | #define SET_SPEEDSTEP_STATE 2 | |
52 | #define GET_SPEEDSTEP_FREQS 4 | |
53 | ||
54 | /* how often shall the SMI call be tried if it failed, e.g. because | |
55 | * of DMA activity going on? */ | |
56 | #define SMI_TRIES 5 | |
57 | ||
bbfebd66 DJ |
58 | #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, \ |
59 | "speedstep-smi", msg) | |
1da177e4 LT |
60 | |
61 | /** | |
62 | * speedstep_smi_ownership | |
63 | */ | |
bbfebd66 | 64 | static int speedstep_smi_ownership(void) |
1da177e4 | 65 | { |
c6e8256a | 66 | u32 command, result, magic, dummy; |
1da177e4 LT |
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 | ||
bbfebd66 DJ |
73 | dprintk("trying to obtain ownership with command %x at port %x\n", |
74 | command, smi_port); | |
1da177e4 LT |
75 | |
76 | __asm__ __volatile__( | |
c6e8256a | 77 | "push %%ebp\n" |
1da177e4 | 78 | "out %%al, (%%dx)\n" |
c6e8256a | 79 | "pop %%ebp\n" |
bbfebd66 DJ |
80 | : "=D" (result), |
81 | "=a" (dummy), "=b" (dummy), "=c" (dummy), "=d" (dummy), | |
82 | "=S" (dummy) | |
f081a529 | 83 | : "a" (command), "b" (function), "c" (0), "d" (smi_port), |
bbfebd66 | 84 | "D" (0), "S" (magic) |
f081a529 | 85 | : "memory" |
1da177e4 LT |
86 | ); |
87 | ||
88 | dprintk("result is %x\n", result); | |
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 | */ | |
bbfebd66 | 102 | static int speedstep_smi_get_freqs(unsigned int *low, unsigned int *high) |
1da177e4 | 103 | { |
c6e8256a | 104 | u32 command, result = 0, edi, high_mhz, low_mhz, dummy; |
bbfebd66 | 105 | u32 state = 0; |
1da177e4 LT |
106 | u32 function = GET_SPEEDSTEP_FREQS; |
107 | ||
108 | if (!(ist_info.event & 0xFFFF)) { | |
ce38b51e | 109 | dprintk("bug #1422 -- can't read freqs from BIOS\n"); |
1da177e4 LT |
110 | return -ENODEV; |
111 | } | |
112 | ||
113 | command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff); | |
114 | ||
bbfebd66 DJ |
115 | dprintk("trying to determine frequencies with command %x at port %x\n", |
116 | command, smi_port); | |
1da177e4 | 117 | |
c6e8256a SD |
118 | __asm__ __volatile__( |
119 | "push %%ebp\n" | |
1da177e4 | 120 | "out %%al, (%%dx)\n" |
c6e8256a | 121 | "pop %%ebp" |
bbfebd66 DJ |
122 | : "=a" (result), |
123 | "=b" (high_mhz), | |
124 | "=c" (low_mhz), | |
125 | "=d" (state), "=D" (edi), "=S" (dummy) | |
126 | : "a" (command), | |
127 | "b" (function), | |
128 | "c" (state), | |
129 | "d" (smi_port), "S" (0), "D" (0) | |
1da177e4 LT |
130 | ); |
131 | ||
bbfebd66 DJ |
132 | dprintk("result %x, low_freq %u, high_freq %u\n", |
133 | result, low_mhz, high_mhz); | |
1da177e4 LT |
134 | |
135 | /* abort if results are obviously incorrect... */ | |
136 | if ((high_mhz + low_mhz) < 600) | |
137 | return -EINVAL; | |
138 | ||
139 | *high = high_mhz * 1000; | |
140 | *low = low_mhz * 1000; | |
141 | ||
142 | return result; | |
32ee8c3e | 143 | } |
1da177e4 LT |
144 | |
145 | /** | |
146 | * speedstep_get_state - set the SpeedStep state | |
147 | * @state: processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH) | |
148 | * | |
149 | */ | |
bbfebd66 | 150 | static int speedstep_get_state(void) |
1da177e4 | 151 | { |
bbfebd66 | 152 | u32 function = GET_SPEEDSTEP_STATE; |
c6e8256a | 153 | u32 result, state, edi, command, dummy; |
1da177e4 LT |
154 | |
155 | command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff); | |
156 | ||
bbfebd66 DJ |
157 | dprintk("trying to determine current setting with command %x " |
158 | "at port %x\n", command, smi_port); | |
1da177e4 | 159 | |
c6e8256a SD |
160 | __asm__ __volatile__( |
161 | "push %%ebp\n" | |
1da177e4 | 162 | "out %%al, (%%dx)\n" |
c6e8256a | 163 | "pop %%ebp\n" |
bbfebd66 DJ |
164 | : "=a" (result), |
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) | |
1da177e4 LT |
169 | ); |
170 | ||
171 | dprintk("state is %x, result is %x\n", state, result); | |
172 | ||
bbfebd66 | 173 | return state & 1; |
1da177e4 LT |
174 | } |
175 | ||
176 | ||
177 | /** | |
178 | * speedstep_set_state - set the SpeedStep state | |
179 | * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH) | |
180 | * | |
181 | */ | |
bbfebd66 | 182 | static void speedstep_set_state(unsigned int state) |
1da177e4 | 183 | { |
c6e8256a | 184 | unsigned int result = 0, command, new_state, dummy; |
1da177e4 | 185 | unsigned long flags; |
bbfebd66 | 186 | unsigned int function = SET_SPEEDSTEP_STATE; |
1da177e4 LT |
187 | unsigned int retry = 0; |
188 | ||
189 | if (state > 0x1) | |
190 | return; | |
191 | ||
192 | /* Disable IRQs */ | |
193 | local_irq_save(flags); | |
194 | ||
195 | command = (smi_sig & 0xffffff00) | (smi_cmd & 0xff); | |
196 | ||
bbfebd66 DJ |
197 | dprintk("trying to set frequency to state %u " |
198 | "with command %x at port %x\n", | |
199 | state, command, smi_port); | |
1da177e4 LT |
200 | |
201 | do { | |
202 | if (retry) { | |
bbfebd66 DJ |
203 | dprintk("retry %u, previous result %u, waiting...\n", |
204 | retry, result); | |
1da177e4 LT |
205 | mdelay(retry * 50); |
206 | } | |
207 | retry++; | |
208 | __asm__ __volatile__( | |
c6e8256a | 209 | "push %%ebp\n" |
1da177e4 | 210 | "out %%al, (%%dx)\n" |
c6e8256a | 211 | "pop %%ebp" |
bbfebd66 DJ |
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) | |
1da177e4 LT |
217 | ); |
218 | } while ((new_state != state) && (retry <= SMI_TRIES)); | |
219 | ||
220 | /* enable IRQs */ | |
221 | local_irq_restore(flags); | |
222 | ||
bbfebd66 DJ |
223 | if (new_state == state) |
224 | dprintk("change to %u MHz succeeded after %u tries " | |
225 | "with result %u\n", | |
226 | (speedstep_freqs[new_state].frequency / 1000), | |
227 | retry, result); | |
228 | else | |
229 | printk(KERN_ERR "cpufreq: change to state %u " | |
230 | "failed with new_state %u and result %u\n", | |
231 | state, new_state, result); | |
1da177e4 LT |
232 | |
233 | return; | |
234 | } | |
235 | ||
236 | ||
237 | /** | |
238 | * speedstep_target - set a new CPUFreq policy | |
239 | * @policy: new policy | |
240 | * @target_freq: new freq | |
32ee8c3e | 241 | * @relation: |
1da177e4 LT |
242 | * |
243 | * Sets a new CPUFreq policy/freq. | |
244 | */ | |
bbfebd66 | 245 | static int speedstep_target(struct cpufreq_policy *policy, |
1da177e4 LT |
246 | unsigned int target_freq, unsigned int relation) |
247 | { | |
248 | unsigned int newstate = 0; | |
249 | struct cpufreq_freqs freqs; | |
250 | ||
bbfebd66 DJ |
251 | if (cpufreq_frequency_table_target(policy, &speedstep_freqs[0], |
252 | target_freq, relation, &newstate)) | |
1da177e4 LT |
253 | return -EINVAL; |
254 | ||
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 */ | |
258 | ||
259 | if (freqs.old == freqs.new) | |
260 | return 0; | |
261 | ||
262 | cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); | |
263 | speedstep_set_state(newstate); | |
264 | cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); | |
265 | ||
266 | return 0; | |
267 | } | |
268 | ||
269 | ||
270 | /** | |
271 | * speedstep_verify - verifies a new CPUFreq policy | |
272 | * @policy: new policy | |
273 | * | |
274 | * Limit must be within speedstep_low_freq and speedstep_high_freq, with | |
275 | * at least one border included. | |
276 | */ | |
bbfebd66 | 277 | static int speedstep_verify(struct cpufreq_policy *policy) |
1da177e4 LT |
278 | { |
279 | return cpufreq_frequency_table_verify(policy, &speedstep_freqs[0]); | |
280 | } | |
281 | ||
282 | ||
283 | static int speedstep_cpu_init(struct cpufreq_policy *policy) | |
284 | { | |
285 | int result; | |
bbfebd66 DJ |
286 | unsigned int speed, state; |
287 | unsigned int *low, *high; | |
1da177e4 LT |
288 | |
289 | /* capability check */ | |
290 | if (policy->cpu != 0) | |
291 | return -ENODEV; | |
292 | ||
293 | result = speedstep_smi_ownership(); | |
294 | if (result) { | |
295 | dprintk("fails in aquiring ownership of a SMI interface.\n"); | |
296 | return -EINVAL; | |
297 | } | |
298 | ||
299 | /* detect low and high frequency */ | |
bbfebd66 DJ |
300 | low = &speedstep_freqs[SPEEDSTEP_LOW].frequency; |
301 | high = &speedstep_freqs[SPEEDSTEP_HIGH].frequency; | |
302 | ||
303 | result = speedstep_smi_get_freqs(low, high); | |
1da177e4 | 304 | if (result) { |
bbfebd66 DJ |
305 | /* fall back to speedstep_lib.c dection mechanism: |
306 | * try both states out */ | |
307 | dprintk("could not detect low and high frequencies " | |
308 | "by SMI call.\n"); | |
1da177e4 | 309 | result = speedstep_get_freqs(speedstep_processor, |
bbfebd66 | 310 | low, high, |
1a10760c | 311 | NULL, |
1da177e4 LT |
312 | &speedstep_set_state); |
313 | ||
314 | if (result) { | |
bbfebd66 DJ |
315 | dprintk("could not detect two different speeds" |
316 | " -- aborting.\n"); | |
1da177e4 LT |
317 | return result; |
318 | } else | |
319 | dprintk("workaround worked.\n"); | |
320 | } | |
321 | ||
322 | /* get current speed setting */ | |
323 | state = speedstep_get_state(); | |
324 | speed = speedstep_freqs[state].frequency; | |
325 | ||
32ee8c3e | 326 | dprintk("currently at %s speed setting - %i MHz\n", |
bbfebd66 DJ |
327 | (speed == speedstep_freqs[SPEEDSTEP_LOW].frequency) |
328 | ? "low" : "high", | |
1da177e4 LT |
329 | (speed / 1000)); |
330 | ||
331 | /* cpuinfo and default policy values */ | |
1da177e4 LT |
332 | policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL; |
333 | policy->cur = speed; | |
334 | ||
335 | result = cpufreq_frequency_table_cpuinfo(policy, speedstep_freqs); | |
336 | if (result) | |
bbfebd66 | 337 | return result; |
1da177e4 | 338 | |
32ee8c3e | 339 | cpufreq_frequency_table_get_attr(speedstep_freqs, policy->cpu); |
1da177e4 LT |
340 | |
341 | return 0; | |
342 | } | |
343 | ||
344 | static int speedstep_cpu_exit(struct cpufreq_policy *policy) | |
345 | { | |
346 | cpufreq_frequency_table_put_attr(policy->cpu); | |
347 | return 0; | |
348 | } | |
349 | ||
350 | static unsigned int speedstep_get(unsigned int cpu) | |
351 | { | |
352 | if (cpu) | |
353 | return -ENODEV; | |
bbfebd66 | 354 | return speedstep_get_frequency(speedstep_processor); |
1da177e4 LT |
355 | } |
356 | ||
357 | ||
358 | static int speedstep_resume(struct cpufreq_policy *policy) | |
359 | { | |
360 | int result = speedstep_smi_ownership(); | |
361 | ||
362 | if (result) | |
363 | dprintk("fails in re-aquiring ownership of a SMI interface.\n"); | |
364 | ||
365 | return result; | |
366 | } | |
367 | ||
bbfebd66 | 368 | static struct freq_attr *speedstep_attr[] = { |
1da177e4 LT |
369 | &cpufreq_freq_attr_scaling_available_freqs, |
370 | NULL, | |
371 | }; | |
372 | ||
221dee28 | 373 | static struct cpufreq_driver speedstep_driver = { |
1da177e4 | 374 | .name = "speedstep-smi", |
32ee8c3e DJ |
375 | .verify = speedstep_verify, |
376 | .target = speedstep_target, | |
1da177e4 LT |
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, | |
383 | }; | |
384 | ||
385 | /** | |
386 | * speedstep_init - initializes the SpeedStep CPUFreq driver | |
387 | * | |
388 | * Initializes the SpeedStep support. Returns -ENODEV on unsupported | |
389 | * BIOS, -EINVAL on problems during initiatization, and zero on | |
390 | * success. | |
391 | */ | |
392 | static int __init speedstep_init(void) | |
393 | { | |
394 | speedstep_processor = speedstep_detect_processor(); | |
395 | ||
396 | switch (speedstep_processor) { | |
bbfebd66 DJ |
397 | case SPEEDSTEP_CPU_PIII_T: |
398 | case SPEEDSTEP_CPU_PIII_C: | |
399 | case SPEEDSTEP_CPU_PIII_C_EARLY: | |
1da177e4 LT |
400 | break; |
401 | default: | |
402 | speedstep_processor = 0; | |
403 | } | |
404 | ||
405 | if (!speedstep_processor) { | |
bbfebd66 | 406 | dprintk("No supported Intel CPU detected.\n"); |
1da177e4 LT |
407 | return -ENODEV; |
408 | } | |
409 | ||
bbfebd66 DJ |
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); | |
1da177e4 | 414 | |
32ee8c3e | 415 | /* Error if no IST-SMI BIOS or no PARM |
1da177e4 | 416 | sig= 'ISGE' aka 'Intel Speedstep Gate E' */ |
32ee8c3e | 417 | if ((ist_info.signature != 0x47534943) && ( |
1da177e4 LT |
418 | (smi_port == 0) || (smi_cmd == 0))) |
419 | return -ENODEV; | |
420 | ||
421 | if (smi_sig == 1) | |
422 | smi_sig = 0x47534943; | |
423 | else | |
424 | smi_sig = ist_info.signature; | |
425 | ||
426 | /* setup smi_port from MODLULE_PARM or BIOS */ | |
32ee8c3e | 427 | if ((smi_port > 0xff) || (smi_port < 0)) |
1da177e4 | 428 | return -EINVAL; |
32ee8c3e | 429 | else if (smi_port == 0) |
1da177e4 | 430 | smi_port = ist_info.command & 0xff; |
1da177e4 | 431 | |
32ee8c3e | 432 | if ((smi_cmd > 0xff) || (smi_cmd < 0)) |
1da177e4 | 433 | return -EINVAL; |
32ee8c3e | 434 | else if (smi_cmd == 0) |
1da177e4 | 435 | smi_cmd = (ist_info.command >> 16) & 0xff; |
1da177e4 LT |
436 | |
437 | return cpufreq_register_driver(&speedstep_driver); | |
438 | } | |
439 | ||
440 | ||
441 | /** | |
442 | * speedstep_exit - unregisters SpeedStep support | |
443 | * | |
444 | * Unregisters SpeedStep support. | |
445 | */ | |
446 | static void __exit speedstep_exit(void) | |
447 | { | |
448 | cpufreq_unregister_driver(&speedstep_driver); | |
449 | } | |
450 | ||
bbfebd66 DJ |
451 | module_param(smi_port, int, 0444); |
452 | module_param(smi_cmd, int, 0444); | |
453 | module_param(smi_sig, uint, 0444); | |
1da177e4 | 454 | |
bbfebd66 DJ |
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 " | |
460 | "SMI interface."); | |
1da177e4 | 461 | |
bbfebd66 DJ |
462 | MODULE_AUTHOR("Hiroshi Miura"); |
463 | MODULE_DESCRIPTION("Speedstep driver for IST applet SMI interface."); | |
464 | MODULE_LICENSE("GPL"); | |
1da177e4 LT |
465 | |
466 | module_init(speedstep_init); | |
467 | module_exit(speedstep_exit); |