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>
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 unsigned int speedstep_processor = 0;
45 * There are only two frequency states for each processor. Values
46 * are in kHz for the time being.
48 static struct cpufreq_frequency_table speedstep_freqs[] = {
51 {0, CPUFREQ_TABLE_END},
56 * Define it if you want verbose debug output, e.g. for bug reporting
58 //#define SPEEDSTEP_DEBUG
60 #ifdef SPEEDSTEP_DEBUG
61 #define dprintk(msg...) printk(msg)
63 #define dprintk(msg...) do { } while(0)
68 * speedstep_set_state - set the SpeedStep state
69 * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
71 * Tries to change the SpeedStep state.
73 static void speedstep_set_state (unsigned int state)
80 if (!speedstep_chipset_dev || (state > 0x1))
84 pci_read_config_dword(speedstep_chipset_dev, 0x40, &pmbase);
85 if (!(pmbase & 0x01)) {
86 printk(KERN_ERR "cpufreq: could not find speedstep register\n");
92 printk(KERN_ERR "cpufreq: could not find speedstep register\n");
97 local_irq_save(flags);
100 value = inb(pmbase + 0x50);
102 dprintk(KERN_DEBUG "cpufreq: read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
104 /* write new state */
108 dprintk(KERN_DEBUG "cpufreq: writing 0x%x to pmbase 0x%x + 0x50\n", value, pmbase);
110 /* Disable bus master arbitration */
111 pm2_blk = inb(pmbase + 0x20);
113 outb(pm2_blk, (pmbase + 0x20));
115 /* Actual transition */
116 outb(value, (pmbase + 0x50));
118 /* Restore bus master arbitration */
120 outb(pm2_blk, (pmbase + 0x20));
122 /* check if transition was successful */
123 value = inb(pmbase + 0x50);
126 local_irq_restore(flags);
128 dprintk(KERN_DEBUG "cpufreq: read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
130 if (state == (value & 0x1)) {
131 dprintk (KERN_INFO "cpufreq: change to %u MHz succeeded\n", (speedstep_get_processor_frequency(speedstep_processor) / 1000));
133 printk (KERN_ERR "cpufreq: change failed - I/O error\n");
141 * speedstep_activate - activate SpeedStep control in the chipset
143 * Tries to activate the SpeedStep status and control registers.
144 * Returns -EINVAL on an unsupported chipset, and zero on success.
146 static int speedstep_activate (void)
150 if (!speedstep_chipset_dev)
153 pci_read_config_word(speedstep_chipset_dev, 0x00A0, &value);
154 if (!(value & 0x08)) {
156 dprintk(KERN_DEBUG "cpufreq: activating SpeedStep (TM) registers\n");
157 pci_write_config_word(speedstep_chipset_dev, 0x00A0, value);
165 * speedstep_detect_chipset - detect the Southbridge which contains SpeedStep logic
167 * Detects ICH2-M, ICH3-M and ICH4-M so far. The pci_dev points to
168 * the LPC bridge / PM module which contains all power-management
169 * functions. Returns the SPEEDSTEP_CHIPSET_-number for the detected
170 * chipset, or zero on failure.
172 static unsigned int speedstep_detect_chipset (void)
174 speedstep_chipset_dev = pci_find_subsys(PCI_VENDOR_ID_INTEL,
175 PCI_DEVICE_ID_INTEL_82801DB_12,
179 if (speedstep_chipset_dev)
182 speedstep_chipset_dev = pci_find_subsys(PCI_VENDOR_ID_INTEL,
183 PCI_DEVICE_ID_INTEL_82801CA_12,
187 if (speedstep_chipset_dev)
191 speedstep_chipset_dev = pci_find_subsys(PCI_VENDOR_ID_INTEL,
192 PCI_DEVICE_ID_INTEL_82801BA_10,
196 if (speedstep_chipset_dev) {
197 /* speedstep.c causes lockups on Dell Inspirons 8000 and
198 * 8100 which use a pretty old revision of the 82815
199 * host brige. Abort on these systems.
201 static struct pci_dev *hostbridge;
204 hostbridge = pci_find_subsys(PCI_VENDOR_ID_INTEL,
205 PCI_DEVICE_ID_INTEL_82815_MC,
213 pci_read_config_byte(hostbridge, PCI_REVISION_ID, &rev);
215 dprintk(KERN_INFO "cpufreq: hostbridge does not support speedstep\n");
216 speedstep_chipset_dev = NULL;
226 static unsigned int speedstep_get(unsigned int cpu)
229 cpumask_t cpus_allowed,affected_cpu_map;
231 /* only run on CPU to be set, or on its sibling */
232 cpus_allowed = current->cpus_allowed;
234 affected_cpu_map = cpu_sibling_map[cpu];
236 affected_cpu_map = cpumask_of_cpu(cpu);
238 set_cpus_allowed(current, affected_cpu_map);
239 speed=speedstep_get_processor_frequency(speedstep_processor);
240 set_cpus_allowed(current, cpus_allowed);
245 * speedstep_target - set a new CPUFreq policy
246 * @policy: new policy
247 * @target_freq: the target frequency
248 * @relation: how that frequency relates to achieved frequency (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
250 * Sets a new CPUFreq policy.
252 static int speedstep_target (struct cpufreq_policy *policy,
253 unsigned int target_freq,
254 unsigned int relation)
256 unsigned int newstate = 0;
257 struct cpufreq_freqs freqs;
258 cpumask_t cpus_allowed, affected_cpu_map;
261 if (cpufreq_frequency_table_target(policy, &speedstep_freqs[0], target_freq, relation, &newstate))
264 freqs.old = speedstep_get(policy->cpu);
265 freqs.new = speedstep_freqs[newstate].frequency;
266 freqs.cpu = policy->cpu;
268 /* no transition necessary */
269 if (freqs.old == freqs.new)
272 cpus_allowed = current->cpus_allowed;
274 /* only run on CPU to be set, or on its sibling */
276 affected_cpu_map = cpu_sibling_map[policy->cpu];
278 affected_cpu_map = cpumask_of_cpu(policy->cpu);
281 for_each_cpu_mask(i, affected_cpu_map) {
283 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
286 /* switch to physical CPU where state is to be changed */
287 set_cpus_allowed(current, affected_cpu_map);
289 speedstep_set_state(newstate);
291 /* allow to be run on all CPUs */
292 set_cpus_allowed(current, cpus_allowed);
294 for_each_cpu_mask(i, affected_cpu_map) {
296 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
304 * speedstep_verify - verifies a new CPUFreq policy
305 * @policy: new policy
307 * Limit must be within speedstep_low_freq and speedstep_high_freq, with
308 * at least one border included.
310 static int speedstep_verify (struct cpufreq_policy *policy)
312 return cpufreq_frequency_table_verify(policy, &speedstep_freqs[0]);
316 static int speedstep_cpu_init(struct cpufreq_policy *policy)
320 cpumask_t cpus_allowed,affected_cpu_map;
323 /* capability check */
324 if (policy->cpu != 0) /* FIXME: better support for SMT in cpufreq core. Up until then, it's better to register only one CPU */
327 /* only run on CPU to be set, or on its sibling */
328 cpus_allowed = current->cpus_allowed;
330 affected_cpu_map = cpu_sibling_map[policy->cpu];
332 affected_cpu_map = cpumask_of_cpu(policy->cpu);
334 set_cpus_allowed(current, affected_cpu_map);
336 /* detect low and high frequency */
337 result = speedstep_get_freqs(speedstep_processor,
338 &speedstep_freqs[SPEEDSTEP_LOW].frequency,
339 &speedstep_freqs[SPEEDSTEP_HIGH].frequency,
340 &speedstep_set_state);
341 set_cpus_allowed(current, cpus_allowed);
345 /* get current speed setting */
346 speed = speedstep_get(policy->cpu);
350 dprintk(KERN_INFO "cpufreq: currently at %s speed setting - %i MHz\n",
351 (speed == speedstep_freqs[SPEEDSTEP_LOW].frequency) ? "low" : "high",
354 /* cpuinfo and default policy values */
355 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
356 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
359 result = cpufreq_frequency_table_cpuinfo(policy, speedstep_freqs);
363 cpufreq_frequency_table_get_attr(speedstep_freqs, policy->cpu);
369 static int speedstep_cpu_exit(struct cpufreq_policy *policy)
371 cpufreq_frequency_table_put_attr(policy->cpu);
375 static struct freq_attr* speedstep_attr[] = {
376 &cpufreq_freq_attr_scaling_available_freqs,
381 static struct cpufreq_driver speedstep_driver = {
382 .name = "speedstep-ich",
383 .verify = speedstep_verify,
384 .target = speedstep_target,
385 .init = speedstep_cpu_init,
386 .exit = speedstep_cpu_exit,
387 .get = speedstep_get,
388 .owner = THIS_MODULE,
389 .attr = speedstep_attr,
394 * speedstep_init - initializes the SpeedStep CPUFreq driver
396 * Initializes the SpeedStep support. Returns -ENODEV on unsupported
397 * devices, -EINVAL on problems during initiatization, and zero on
400 static int __init speedstep_init(void)
402 /* detect processor */
403 speedstep_processor = speedstep_detect_processor();
404 if (!speedstep_processor)
408 if (!speedstep_detect_chipset()) {
409 printk(KERN_INFO "cpufreq: Intel(R) SpeedStep(TM) for this chipset not (yet) available.\n");
413 /* activate speedstep support */
414 if (speedstep_activate())
417 return cpufreq_register_driver(&speedstep_driver);
422 * speedstep_exit - unregisters SpeedStep support
424 * Unregisters SpeedStep support.
426 static void __exit speedstep_exit(void)
428 cpufreq_unregister_driver(&speedstep_driver);
432 MODULE_AUTHOR ("Dave Jones <davej@codemonkey.org.uk>, Dominik Brodowski <linux@brodo.de>");
433 MODULE_DESCRIPTION ("Speedstep driver for Intel mobile processors on chipsets with ICH-M southbridges.");
434 MODULE_LICENSE ("GPL");
436 module_init(speedstep_init);
437 module_exit(speedstep_exit);