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