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/slab.h>
27 #include <linux/sched.h>
29 #include "speedstep-lib.h"
33 * It is necessary to know which chipset is used. As accesses to
34 * this device occur at various places in this module, we need a
35 * static struct pci_dev * pointing to that device.
37 static struct pci_dev *speedstep_chipset_dev;
40 /* speedstep_processor
42 static unsigned int speedstep_processor;
47 * There are only two frequency states for each processor. Values
48 * are in kHz for the time being.
50 static struct cpufreq_frequency_table speedstep_freqs[] = {
53 {0, CPUFREQ_TABLE_END},
57 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, \
62 * speedstep_find_register - read the PMBASE address
64 * Returns: -ENODEV if no register could be found
66 static int speedstep_find_register(void)
68 if (!speedstep_chipset_dev)
72 pci_read_config_dword(speedstep_chipset_dev, 0x40, &pmbase);
73 if (!(pmbase & 0x01)) {
74 printk(KERN_ERR "speedstep-ich: could not find speedstep register\n");
80 printk(KERN_ERR "speedstep-ich: could not find speedstep register\n");
84 dprintk("pmbase is 0x%x\n", pmbase);
89 * speedstep_set_state - set the SpeedStep state
90 * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
92 * Tries to change the SpeedStep state. Can be called from
93 * smp_call_function_single.
95 static void speedstep_set_state(unsigned int state)
105 local_irq_save(flags);
108 value = inb(pmbase + 0x50);
110 dprintk("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
112 /* write new state */
116 dprintk("writing 0x%x to pmbase 0x%x + 0x50\n", value, pmbase);
118 /* Disable bus master arbitration */
119 pm2_blk = inb(pmbase + 0x20);
121 outb(pm2_blk, (pmbase + 0x20));
123 /* Actual transition */
124 outb(value, (pmbase + 0x50));
126 /* Restore bus master arbitration */
128 outb(pm2_blk, (pmbase + 0x20));
130 /* check if transition was successful */
131 value = inb(pmbase + 0x50);
134 local_irq_restore(flags);
136 dprintk("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
138 if (state == (value & 0x1))
139 dprintk("change to %u MHz succeeded\n",
140 speedstep_get_frequency(speedstep_processor) / 1000);
142 printk(KERN_ERR "cpufreq: change failed - I/O error\n");
147 /* Wrapper for smp_call_function_single. */
148 static void _speedstep_set_state(void *_state)
150 speedstep_set_state(*(unsigned int *)_state);
154 * speedstep_activate - activate SpeedStep control in the chipset
156 * Tries to activate the SpeedStep status and control registers.
157 * Returns -EINVAL on an unsupported chipset, and zero on success.
159 static int speedstep_activate(void)
163 if (!speedstep_chipset_dev)
166 pci_read_config_word(speedstep_chipset_dev, 0x00A0, &value);
167 if (!(value & 0x08)) {
169 dprintk("activating SpeedStep (TM) registers\n");
170 pci_write_config_word(speedstep_chipset_dev, 0x00A0, value);
178 * speedstep_detect_chipset - detect the Southbridge which contains SpeedStep logic
180 * Detects ICH2-M, ICH3-M and ICH4-M so far. The pci_dev points to
181 * the LPC bridge / PM module which contains all power-management
182 * functions. Returns the SPEEDSTEP_CHIPSET_-number for the detected
183 * chipset, or zero on failure.
185 static unsigned int speedstep_detect_chipset(void)
187 speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
188 PCI_DEVICE_ID_INTEL_82801DB_12,
189 PCI_ANY_ID, PCI_ANY_ID,
191 if (speedstep_chipset_dev)
194 speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
195 PCI_DEVICE_ID_INTEL_82801CA_12,
196 PCI_ANY_ID, PCI_ANY_ID,
198 if (speedstep_chipset_dev)
202 speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
203 PCI_DEVICE_ID_INTEL_82801BA_10,
204 PCI_ANY_ID, PCI_ANY_ID,
206 if (speedstep_chipset_dev) {
207 /* speedstep.c causes lockups on Dell Inspirons 8000 and
208 * 8100 which use a pretty old revision of the 82815
209 * host brige. Abort on these systems.
211 static struct pci_dev *hostbridge;
213 hostbridge = pci_get_subsys(PCI_VENDOR_ID_INTEL,
214 PCI_DEVICE_ID_INTEL_82815_MC,
215 PCI_ANY_ID, PCI_ANY_ID,
221 if (hostbridge->revision < 5) {
222 dprintk("hostbridge does not support speedstep\n");
223 speedstep_chipset_dev = NULL;
224 pci_dev_put(hostbridge);
228 pci_dev_put(hostbridge);
235 struct get_freq_data {
237 unsigned int processor;
240 static void get_freq_data(void *_data)
242 struct get_freq_data *data = _data;
244 data->speed = speedstep_get_frequency(data->processor);
247 static unsigned int speedstep_get(unsigned int cpu)
249 struct get_freq_data data = { .processor = cpu };
251 /* You're supposed to ensure CPU is online. */
252 if (smp_call_function_single(cpu, get_freq_data, &data, 1) != 0)
255 dprintk("detected %u kHz as current frequency\n", data.speed);
260 * speedstep_target - set a new CPUFreq policy
261 * @policy: new policy
262 * @target_freq: the target frequency
263 * @relation: how that frequency relates to achieved frequency
264 * (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
266 * Sets a new CPUFreq policy.
268 static int speedstep_target(struct cpufreq_policy *policy,
269 unsigned int target_freq,
270 unsigned int relation)
272 unsigned int newstate = 0, policy_cpu;
273 struct cpufreq_freqs freqs;
276 if (cpufreq_frequency_table_target(policy, &speedstep_freqs[0],
277 target_freq, relation, &newstate))
280 policy_cpu = cpumask_any_and(policy->cpus, cpu_online_mask);
281 freqs.old = speedstep_get(policy_cpu);
282 freqs.new = speedstep_freqs[newstate].frequency;
283 freqs.cpu = policy->cpu;
285 dprintk("transiting from %u to %u kHz\n", freqs.old, freqs.new);
287 /* no transition necessary */
288 if (freqs.old == freqs.new)
291 for_each_cpu(i, policy->cpus) {
293 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
296 smp_call_function_single(policy_cpu, _speedstep_set_state, &newstate,
299 for_each_cpu(i, policy->cpus) {
301 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
309 * speedstep_verify - verifies a new CPUFreq policy
310 * @policy: new policy
312 * Limit must be within speedstep_low_freq and speedstep_high_freq, with
313 * at least one border included.
315 static int speedstep_verify(struct cpufreq_policy *policy)
317 return cpufreq_frequency_table_verify(policy, &speedstep_freqs[0]);
321 struct cpufreq_policy *policy;
325 static void get_freqs_on_cpu(void *_get_freqs)
327 struct get_freqs *get_freqs = _get_freqs;
330 speedstep_get_freqs(speedstep_processor,
331 &speedstep_freqs[SPEEDSTEP_LOW].frequency,
332 &speedstep_freqs[SPEEDSTEP_HIGH].frequency,
333 &get_freqs->policy->cpuinfo.transition_latency,
334 &speedstep_set_state);
337 static int speedstep_cpu_init(struct cpufreq_policy *policy)
340 unsigned int policy_cpu, speed;
343 /* only run on CPU to be set, or on its sibling */
345 cpumask_copy(policy->cpus, cpu_sibling_mask(policy->cpu));
347 policy_cpu = cpumask_any_and(policy->cpus, cpu_online_mask);
349 /* detect low and high frequency and transition latency */
351 smp_call_function_single(policy_cpu, get_freqs_on_cpu, &gf, 1);
355 /* get current speed setting */
356 speed = speedstep_get(policy_cpu);
360 dprintk("currently at %s speed setting - %i MHz\n",
361 (speed == speedstep_freqs[SPEEDSTEP_LOW].frequency)
365 /* cpuinfo and default policy values */
368 result = cpufreq_frequency_table_cpuinfo(policy, speedstep_freqs);
372 cpufreq_frequency_table_get_attr(speedstep_freqs, policy->cpu);
378 static int speedstep_cpu_exit(struct cpufreq_policy *policy)
380 cpufreq_frequency_table_put_attr(policy->cpu);
384 static struct freq_attr *speedstep_attr[] = {
385 &cpufreq_freq_attr_scaling_available_freqs,
390 static struct cpufreq_driver speedstep_driver = {
391 .name = "speedstep-ich",
392 .verify = speedstep_verify,
393 .target = speedstep_target,
394 .init = speedstep_cpu_init,
395 .exit = speedstep_cpu_exit,
396 .get = speedstep_get,
397 .owner = THIS_MODULE,
398 .attr = speedstep_attr,
403 * speedstep_init - initializes the SpeedStep CPUFreq driver
405 * Initializes the SpeedStep support. Returns -ENODEV on unsupported
406 * devices, -EINVAL on problems during initiatization, and zero on
409 static int __init speedstep_init(void)
411 /* detect processor */
412 speedstep_processor = speedstep_detect_processor();
413 if (!speedstep_processor) {
414 dprintk("Intel(R) SpeedStep(TM) capable processor "
420 if (!speedstep_detect_chipset()) {
421 dprintk("Intel(R) SpeedStep(TM) for this chipset not "
422 "(yet) available.\n");
426 /* activate speedstep support */
427 if (speedstep_activate()) {
428 pci_dev_put(speedstep_chipset_dev);
432 if (speedstep_find_register())
435 return cpufreq_register_driver(&speedstep_driver);
440 * speedstep_exit - unregisters SpeedStep support
442 * Unregisters SpeedStep support.
444 static void __exit speedstep_exit(void)
446 pci_dev_put(speedstep_chipset_dev);
447 cpufreq_unregister_driver(&speedstep_driver);
451 MODULE_AUTHOR("Dave Jones <davej@redhat.com>, "
452 "Dominik Brodowski <linux@brodo.de>");
453 MODULE_DESCRIPTION("Speedstep driver for Intel mobile processors on chipsets "
454 "with ICH-M southbridges.");
455 MODULE_LICENSE("GPL");
457 module_init(speedstep_init);
458 module_exit(speedstep_exit);