Merge branch 'drm-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[linux-2.6] / arch / x86 / kernel / cpu / cpufreq / speedstep-ich.c
1 /*
2  * (C) 2001  Dave Jones, Arjan van de ven.
3  * (C) 2002 - 2003  Dominik Brodowski <linux@brodo.de>
4  *
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.
8  *
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.
12  *
13  *  BIG FAT DISCLAIMER: Work in progress code. Possibly *dangerous*
14  */
15
16
17 /*********************************************************************
18  *                        SPEEDSTEP - DEFINITIONS                    *
19  *********************************************************************/
20
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>
28
29 #include "speedstep-lib.h"
30
31
32 /* speedstep_chipset:
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.
36  */
37 static struct pci_dev *speedstep_chipset_dev;
38
39
40 /* speedstep_processor
41  */
42 static unsigned int speedstep_processor;
43
44 static u32 pmbase;
45
46 /*
47  *   There are only two frequency states for each processor. Values
48  * are in kHz for the time being.
49  */
50 static struct cpufreq_frequency_table speedstep_freqs[] = {
51         {SPEEDSTEP_HIGH,        0},
52         {SPEEDSTEP_LOW,         0},
53         {0,                     CPUFREQ_TABLE_END},
54 };
55
56
57 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, \
58                 "speedstep-ich", msg)
59
60
61 /**
62  * speedstep_find_register - read the PMBASE address
63  *
64  * Returns: -ENODEV if no register could be found
65  */
66 static int speedstep_find_register(void)
67 {
68         if (!speedstep_chipset_dev)
69                 return -ENODEV;
70
71         /* get PMBASE */
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");
75                 return -ENODEV;
76         }
77
78         pmbase &= 0xFFFFFFFE;
79         if (!pmbase) {
80                 printk(KERN_ERR "speedstep-ich: could not find speedstep register\n");
81                 return -ENODEV;
82         }
83
84         dprintk("pmbase is 0x%x\n", pmbase);
85         return 0;
86 }
87
88 /**
89  * speedstep_set_state - set the SpeedStep state
90  * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
91  *
92  *   Tries to change the SpeedStep state.
93  */
94 static void speedstep_set_state(unsigned int state)
95 {
96         u8 pm2_blk;
97         u8 value;
98         unsigned long flags;
99
100         if (state > 0x1)
101                 return;
102
103         /* Disable IRQs */
104         local_irq_save(flags);
105
106         /* read state */
107         value = inb(pmbase + 0x50);
108
109         dprintk("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
110
111         /* write new state */
112         value &= 0xFE;
113         value |= state;
114
115         dprintk("writing 0x%x to pmbase 0x%x + 0x50\n", value, pmbase);
116
117         /* Disable bus master arbitration */
118         pm2_blk = inb(pmbase + 0x20);
119         pm2_blk |= 0x01;
120         outb(pm2_blk, (pmbase + 0x20));
121
122         /* Actual transition */
123         outb(value, (pmbase + 0x50));
124
125         /* Restore bus master arbitration */
126         pm2_blk &= 0xfe;
127         outb(pm2_blk, (pmbase + 0x20));
128
129         /* check if transition was successful */
130         value = inb(pmbase + 0x50);
131
132         /* Enable IRQs */
133         local_irq_restore(flags);
134
135         dprintk("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
136
137         if (state == (value & 0x1))
138                 dprintk("change to %u MHz succeeded\n",
139                         speedstep_get_frequency(speedstep_processor) / 1000);
140         else
141                 printk(KERN_ERR "cpufreq: change failed - I/O error\n");
142
143         return;
144 }
145
146
147 /**
148  * speedstep_activate - activate SpeedStep control in the chipset
149  *
150  *   Tries to activate the SpeedStep status and control registers.
151  * Returns -EINVAL on an unsupported chipset, and zero on success.
152  */
153 static int speedstep_activate(void)
154 {
155         u16 value = 0;
156
157         if (!speedstep_chipset_dev)
158                 return -EINVAL;
159
160         pci_read_config_word(speedstep_chipset_dev, 0x00A0, &value);
161         if (!(value & 0x08)) {
162                 value |= 0x08;
163                 dprintk("activating SpeedStep (TM) registers\n");
164                 pci_write_config_word(speedstep_chipset_dev, 0x00A0, value);
165         }
166
167         return 0;
168 }
169
170
171 /**
172  * speedstep_detect_chipset - detect the Southbridge which contains SpeedStep logic
173  *
174  *   Detects ICH2-M, ICH3-M and ICH4-M so far. The pci_dev points to
175  * the LPC bridge / PM module which contains all power-management
176  * functions. Returns the SPEEDSTEP_CHIPSET_-number for the detected
177  * chipset, or zero on failure.
178  */
179 static unsigned int speedstep_detect_chipset(void)
180 {
181         speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
182                               PCI_DEVICE_ID_INTEL_82801DB_12,
183                               PCI_ANY_ID, PCI_ANY_ID,
184                               NULL);
185         if (speedstep_chipset_dev)
186                 return 4; /* 4-M */
187
188         speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
189                               PCI_DEVICE_ID_INTEL_82801CA_12,
190                               PCI_ANY_ID, PCI_ANY_ID,
191                               NULL);
192         if (speedstep_chipset_dev)
193                 return 3; /* 3-M */
194
195
196         speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
197                               PCI_DEVICE_ID_INTEL_82801BA_10,
198                               PCI_ANY_ID, PCI_ANY_ID,
199                               NULL);
200         if (speedstep_chipset_dev) {
201                 /* speedstep.c causes lockups on Dell Inspirons 8000 and
202                  * 8100 which use a pretty old revision of the 82815
203                  * host brige. Abort on these systems.
204                  */
205                 static struct pci_dev *hostbridge;
206
207                 hostbridge  = pci_get_subsys(PCI_VENDOR_ID_INTEL,
208                               PCI_DEVICE_ID_INTEL_82815_MC,
209                               PCI_ANY_ID, PCI_ANY_ID,
210                               NULL);
211
212                 if (!hostbridge)
213                         return 2; /* 2-M */
214
215                 if (hostbridge->revision < 5) {
216                         dprintk("hostbridge does not support speedstep\n");
217                         speedstep_chipset_dev = NULL;
218                         pci_dev_put(hostbridge);
219                         return 0;
220                 }
221
222                 pci_dev_put(hostbridge);
223                 return 2; /* 2-M */
224         }
225
226         return 0;
227 }
228
229 static unsigned int _speedstep_get(const struct cpumask *cpus)
230 {
231         unsigned int speed;
232         cpumask_t cpus_allowed;
233
234         cpus_allowed = current->cpus_allowed;
235         set_cpus_allowed_ptr(current, cpus);
236         speed = speedstep_get_frequency(speedstep_processor);
237         set_cpus_allowed_ptr(current, &cpus_allowed);
238         dprintk("detected %u kHz as current frequency\n", speed);
239         return speed;
240 }
241
242 static unsigned int speedstep_get(unsigned int cpu)
243 {
244         return _speedstep_get(cpumask_of(cpu));
245 }
246
247 /**
248  * speedstep_target - set a new CPUFreq policy
249  * @policy: new policy
250  * @target_freq: the target frequency
251  * @relation: how that frequency relates to achieved frequency
252  *      (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
253  *
254  * Sets a new CPUFreq policy.
255  */
256 static int speedstep_target(struct cpufreq_policy *policy,
257                              unsigned int target_freq,
258                              unsigned int relation)
259 {
260         unsigned int newstate = 0;
261         struct cpufreq_freqs freqs;
262         cpumask_t cpus_allowed;
263         int i;
264
265         if (cpufreq_frequency_table_target(policy, &speedstep_freqs[0],
266                                 target_freq, relation, &newstate))
267                 return -EINVAL;
268
269         freqs.old = _speedstep_get(policy->cpus);
270         freqs.new = speedstep_freqs[newstate].frequency;
271         freqs.cpu = policy->cpu;
272
273         dprintk("transiting from %u to %u kHz\n", freqs.old, freqs.new);
274
275         /* no transition necessary */
276         if (freqs.old == freqs.new)
277                 return 0;
278
279         cpus_allowed = current->cpus_allowed;
280
281         for_each_cpu(i, policy->cpus) {
282                 freqs.cpu = i;
283                 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
284         }
285
286         /* switch to physical CPU where state is to be changed */
287         set_cpus_allowed_ptr(current, policy->cpus);
288
289         speedstep_set_state(newstate);
290
291         /* allow to be run on all CPUs */
292         set_cpus_allowed_ptr(current, &cpus_allowed);
293
294         for_each_cpu(i, policy->cpus) {
295                 freqs.cpu = i;
296                 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
297         }
298
299         return 0;
300 }
301
302
303 /**
304  * speedstep_verify - verifies a new CPUFreq policy
305  * @policy: new policy
306  *
307  * Limit must be within speedstep_low_freq and speedstep_high_freq, with
308  * at least one border included.
309  */
310 static int speedstep_verify(struct cpufreq_policy *policy)
311 {
312         return cpufreq_frequency_table_verify(policy, &speedstep_freqs[0]);
313 }
314
315
316 static int speedstep_cpu_init(struct cpufreq_policy *policy)
317 {
318         int result = 0;
319         unsigned int speed;
320         cpumask_t cpus_allowed;
321
322         /* only run on CPU to be set, or on its sibling */
323 #ifdef CONFIG_SMP
324         cpumask_copy(policy->cpus, cpu_sibling_mask(policy->cpu));
325 #endif
326
327         cpus_allowed = current->cpus_allowed;
328         set_cpus_allowed_ptr(current, policy->cpus);
329
330         /* detect low and high frequency and transition latency */
331         result = speedstep_get_freqs(speedstep_processor,
332                                      &speedstep_freqs[SPEEDSTEP_LOW].frequency,
333                                      &speedstep_freqs[SPEEDSTEP_HIGH].frequency,
334                                      &policy->cpuinfo.transition_latency,
335                                      &speedstep_set_state);
336         set_cpus_allowed_ptr(current, &cpus_allowed);
337         if (result)
338                 return result;
339
340         /* get current speed setting */
341         speed = _speedstep_get(policy->cpus);
342         if (!speed)
343                 return -EIO;
344
345         dprintk("currently at %s speed setting - %i MHz\n",
346                 (speed == speedstep_freqs[SPEEDSTEP_LOW].frequency)
347                 ? "low" : "high",
348                 (speed / 1000));
349
350         /* cpuinfo and default policy values */
351         policy->cur = speed;
352
353         result = cpufreq_frequency_table_cpuinfo(policy, speedstep_freqs);
354         if (result)
355                 return result;
356
357         cpufreq_frequency_table_get_attr(speedstep_freqs, policy->cpu);
358
359         return 0;
360 }
361
362
363 static int speedstep_cpu_exit(struct cpufreq_policy *policy)
364 {
365         cpufreq_frequency_table_put_attr(policy->cpu);
366         return 0;
367 }
368
369 static struct freq_attr *speedstep_attr[] = {
370         &cpufreq_freq_attr_scaling_available_freqs,
371         NULL,
372 };
373
374
375 static struct cpufreq_driver speedstep_driver = {
376         .name   = "speedstep-ich",
377         .verify = speedstep_verify,
378         .target = speedstep_target,
379         .init   = speedstep_cpu_init,
380         .exit   = speedstep_cpu_exit,
381         .get    = speedstep_get,
382         .owner  = THIS_MODULE,
383         .attr   = speedstep_attr,
384 };
385
386
387 /**
388  * speedstep_init - initializes the SpeedStep CPUFreq driver
389  *
390  *   Initializes the SpeedStep support. Returns -ENODEV on unsupported
391  * devices, -EINVAL on problems during initiatization, and zero on
392  * success.
393  */
394 static int __init speedstep_init(void)
395 {
396         /* detect processor */
397         speedstep_processor = speedstep_detect_processor();
398         if (!speedstep_processor) {
399                 dprintk("Intel(R) SpeedStep(TM) capable processor "
400                                 "not found\n");
401                 return -ENODEV;
402         }
403
404         /* detect chipset */
405         if (!speedstep_detect_chipset()) {
406                 dprintk("Intel(R) SpeedStep(TM) for this chipset not "
407                                 "(yet) available.\n");
408                 return -ENODEV;
409         }
410
411         /* activate speedstep support */
412         if (speedstep_activate()) {
413                 pci_dev_put(speedstep_chipset_dev);
414                 return -EINVAL;
415         }
416
417         if (speedstep_find_register())
418                 return -ENODEV;
419
420         return cpufreq_register_driver(&speedstep_driver);
421 }
422
423
424 /**
425  * speedstep_exit - unregisters SpeedStep support
426  *
427  *   Unregisters SpeedStep support.
428  */
429 static void __exit speedstep_exit(void)
430 {
431         pci_dev_put(speedstep_chipset_dev);
432         cpufreq_unregister_driver(&speedstep_driver);
433 }
434
435
436 MODULE_AUTHOR("Dave Jones <davej@redhat.com>, "
437                 "Dominik Brodowski <linux@brodo.de>");
438 MODULE_DESCRIPTION("Speedstep driver for Intel mobile processors on chipsets "
439                 "with ICH-M southbridges.");
440 MODULE_LICENSE("GPL");
441
442 module_init(speedstep_init);
443 module_exit(speedstep_exit);