2 * (C) 2001 Dave Jones, Arjan van de ven.
3 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
5 * Licensed under the terms of the GNU GPL License version 2.
6 * Based upon reverse engineered information, and on Intel documentation
7 * for chipsets ICH2-M and ICH3-M.
9 * Many thanks to Ducrot Bruno for finding and fixing the last
10 * "missing link" for ICH2-M/ICH3-M support, and to Thomas Winkler
11 * for extensive testing.
13 * BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
17 /*********************************************************************
18 * SPEEDSTEP - DEFINITIONS *
19 *********************************************************************/
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/cpufreq.h>
25 #include <linux/pci.h>
26 #include <linux/sched.h>
28 #include "speedstep-lib.h"
32 * It is necessary to know which chipset is used. As accesses to
33 * this device occur at various places in this module, we need a
34 * static struct pci_dev * pointing to that device.
36 static struct pci_dev
*speedstep_chipset_dev
;
39 /* speedstep_processor
41 static enum speedstep_processor speedstep_processor
;
46 * There are only two frequency states for each processor. Values
47 * are in kHz for the time being.
49 static struct cpufreq_frequency_table speedstep_freqs
[] = {
52 {0, CPUFREQ_TABLE_END
},
56 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, \
61 * speedstep_find_register - read the PMBASE address
63 * Returns: -ENODEV if no register could be found
65 static int speedstep_find_register(void)
67 if (!speedstep_chipset_dev
)
71 pci_read_config_dword(speedstep_chipset_dev
, 0x40, &pmbase
);
72 if (!(pmbase
& 0x01)) {
73 printk(KERN_ERR
"speedstep-ich: could not find speedstep register\n");
79 printk(KERN_ERR
"speedstep-ich: could not find speedstep register\n");
83 dprintk("pmbase is 0x%x\n", pmbase
);
88 * speedstep_set_state - set the SpeedStep state
89 * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
91 * Tries to change the SpeedStep state. Can be called from
92 * smp_call_function_single.
94 static void speedstep_set_state(unsigned int state
)
104 local_irq_save(flags
);
107 value
= inb(pmbase
+ 0x50);
109 dprintk("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase
, value
);
111 /* write new state */
115 dprintk("writing 0x%x to pmbase 0x%x + 0x50\n", value
, pmbase
);
117 /* Disable bus master arbitration */
118 pm2_blk
= inb(pmbase
+ 0x20);
120 outb(pm2_blk
, (pmbase
+ 0x20));
122 /* Actual transition */
123 outb(value
, (pmbase
+ 0x50));
125 /* Restore bus master arbitration */
127 outb(pm2_blk
, (pmbase
+ 0x20));
129 /* check if transition was successful */
130 value
= inb(pmbase
+ 0x50);
133 local_irq_restore(flags
);
135 dprintk("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase
, value
);
137 if (state
== (value
& 0x1))
138 dprintk("change to %u MHz succeeded\n",
139 speedstep_get_frequency(speedstep_processor
) / 1000);
141 printk(KERN_ERR
"cpufreq: change failed - I/O error\n");
146 /* Wrapper for smp_call_function_single. */
147 static void _speedstep_set_state(void *_state
)
149 speedstep_set_state(*(unsigned int *)_state
);
153 * speedstep_activate - activate SpeedStep control in the chipset
155 * Tries to activate the SpeedStep status and control registers.
156 * Returns -EINVAL on an unsupported chipset, and zero on success.
158 static int speedstep_activate(void)
162 if (!speedstep_chipset_dev
)
165 pci_read_config_word(speedstep_chipset_dev
, 0x00A0, &value
);
166 if (!(value
& 0x08)) {
168 dprintk("activating SpeedStep (TM) registers\n");
169 pci_write_config_word(speedstep_chipset_dev
, 0x00A0, value
);
177 * speedstep_detect_chipset - detect the Southbridge which contains SpeedStep logic
179 * Detects ICH2-M, ICH3-M and ICH4-M so far. The pci_dev points to
180 * the LPC bridge / PM module which contains all power-management
181 * functions. Returns the SPEEDSTEP_CHIPSET_-number for the detected
182 * chipset, or zero on failure.
184 static unsigned int speedstep_detect_chipset(void)
186 speedstep_chipset_dev
= pci_get_subsys(PCI_VENDOR_ID_INTEL
,
187 PCI_DEVICE_ID_INTEL_82801DB_12
,
188 PCI_ANY_ID
, PCI_ANY_ID
,
190 if (speedstep_chipset_dev
)
193 speedstep_chipset_dev
= pci_get_subsys(PCI_VENDOR_ID_INTEL
,
194 PCI_DEVICE_ID_INTEL_82801CA_12
,
195 PCI_ANY_ID
, PCI_ANY_ID
,
197 if (speedstep_chipset_dev
)
201 speedstep_chipset_dev
= pci_get_subsys(PCI_VENDOR_ID_INTEL
,
202 PCI_DEVICE_ID_INTEL_82801BA_10
,
203 PCI_ANY_ID
, PCI_ANY_ID
,
205 if (speedstep_chipset_dev
) {
206 /* speedstep.c causes lockups on Dell Inspirons 8000 and
207 * 8100 which use a pretty old revision of the 82815
208 * host brige. Abort on these systems.
210 static struct pci_dev
*hostbridge
;
212 hostbridge
= pci_get_subsys(PCI_VENDOR_ID_INTEL
,
213 PCI_DEVICE_ID_INTEL_82815_MC
,
214 PCI_ANY_ID
, PCI_ANY_ID
,
220 if (hostbridge
->revision
< 5) {
221 dprintk("hostbridge does not support speedstep\n");
222 speedstep_chipset_dev
= NULL
;
223 pci_dev_put(hostbridge
);
227 pci_dev_put(hostbridge
);
234 static void get_freq_data(void *_speed
)
236 unsigned int *speed
= _speed
;
238 *speed
= speedstep_get_frequency(speedstep_processor
);
241 static unsigned int speedstep_get(unsigned int cpu
)
245 /* You're supposed to ensure CPU is online. */
246 if (smp_call_function_single(cpu
, get_freq_data
, &speed
, 1) != 0)
249 dprintk("detected %u kHz as current frequency\n", speed
);
254 * speedstep_target - set a new CPUFreq policy
255 * @policy: new policy
256 * @target_freq: the target frequency
257 * @relation: how that frequency relates to achieved frequency
258 * (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
260 * Sets a new CPUFreq policy.
262 static int speedstep_target(struct cpufreq_policy
*policy
,
263 unsigned int target_freq
,
264 unsigned int relation
)
266 unsigned int newstate
= 0, policy_cpu
;
267 struct cpufreq_freqs freqs
;
270 if (cpufreq_frequency_table_target(policy
, &speedstep_freqs
[0],
271 target_freq
, relation
, &newstate
))
274 policy_cpu
= cpumask_any_and(policy
->cpus
, cpu_online_mask
);
275 freqs
.old
= speedstep_get(policy_cpu
);
276 freqs
.new = speedstep_freqs
[newstate
].frequency
;
277 freqs
.cpu
= policy
->cpu
;
279 dprintk("transiting from %u to %u kHz\n", freqs
.old
, freqs
.new);
281 /* no transition necessary */
282 if (freqs
.old
== freqs
.new)
285 for_each_cpu(i
, policy
->cpus
) {
287 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
290 smp_call_function_single(policy_cpu
, _speedstep_set_state
, &newstate
,
293 for_each_cpu(i
, policy
->cpus
) {
295 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
303 * speedstep_verify - verifies a new CPUFreq policy
304 * @policy: new policy
306 * Limit must be within speedstep_low_freq and speedstep_high_freq, with
307 * at least one border included.
309 static int speedstep_verify(struct cpufreq_policy
*policy
)
311 return cpufreq_frequency_table_verify(policy
, &speedstep_freqs
[0]);
315 struct cpufreq_policy
*policy
;
319 static void get_freqs_on_cpu(void *_get_freqs
)
321 struct get_freqs
*get_freqs
= _get_freqs
;
324 speedstep_get_freqs(speedstep_processor
,
325 &speedstep_freqs
[SPEEDSTEP_LOW
].frequency
,
326 &speedstep_freqs
[SPEEDSTEP_HIGH
].frequency
,
327 &get_freqs
->policy
->cpuinfo
.transition_latency
,
328 &speedstep_set_state
);
331 static int speedstep_cpu_init(struct cpufreq_policy
*policy
)
334 unsigned int policy_cpu
, speed
;
337 /* only run on CPU to be set, or on its sibling */
339 cpumask_copy(policy
->cpus
, cpu_sibling_mask(policy
->cpu
));
341 policy_cpu
= cpumask_any_and(policy
->cpus
, cpu_online_mask
);
343 /* detect low and high frequency and transition latency */
345 smp_call_function_single(policy_cpu
, get_freqs_on_cpu
, &gf
, 1);
349 /* get current speed setting */
350 speed
= speedstep_get(policy_cpu
);
354 dprintk("currently at %s speed setting - %i MHz\n",
355 (speed
== speedstep_freqs
[SPEEDSTEP_LOW
].frequency
)
359 /* cpuinfo and default policy values */
362 result
= cpufreq_frequency_table_cpuinfo(policy
, speedstep_freqs
);
366 cpufreq_frequency_table_get_attr(speedstep_freqs
, policy
->cpu
);
372 static int speedstep_cpu_exit(struct cpufreq_policy
*policy
)
374 cpufreq_frequency_table_put_attr(policy
->cpu
);
378 static struct freq_attr
*speedstep_attr
[] = {
379 &cpufreq_freq_attr_scaling_available_freqs
,
384 static struct cpufreq_driver speedstep_driver
= {
385 .name
= "speedstep-ich",
386 .verify
= speedstep_verify
,
387 .target
= speedstep_target
,
388 .init
= speedstep_cpu_init
,
389 .exit
= speedstep_cpu_exit
,
390 .get
= speedstep_get
,
391 .owner
= THIS_MODULE
,
392 .attr
= speedstep_attr
,
397 * speedstep_init - initializes the SpeedStep CPUFreq driver
399 * Initializes the SpeedStep support. Returns -ENODEV on unsupported
400 * devices, -EINVAL on problems during initiatization, and zero on
403 static int __init
speedstep_init(void)
405 /* detect processor */
406 speedstep_processor
= speedstep_detect_processor();
407 if (!speedstep_processor
) {
408 dprintk("Intel(R) SpeedStep(TM) capable processor "
414 if (!speedstep_detect_chipset()) {
415 dprintk("Intel(R) SpeedStep(TM) for this chipset not "
416 "(yet) available.\n");
420 /* activate speedstep support */
421 if (speedstep_activate()) {
422 pci_dev_put(speedstep_chipset_dev
);
426 if (speedstep_find_register())
429 return cpufreq_register_driver(&speedstep_driver
);
434 * speedstep_exit - unregisters SpeedStep support
436 * Unregisters SpeedStep support.
438 static void __exit
speedstep_exit(void)
440 pci_dev_put(speedstep_chipset_dev
);
441 cpufreq_unregister_driver(&speedstep_driver
);
445 MODULE_AUTHOR("Dave Jones <davej@redhat.com>, "
446 "Dominik Brodowski <linux@brodo.de>");
447 MODULE_DESCRIPTION("Speedstep driver for Intel mobile processors on chipsets "
448 "with ICH-M southbridges.");
449 MODULE_LICENSE("GPL");
451 module_init(speedstep_init
);
452 module_exit(speedstep_exit
);