blob: e2e5aa97145275a0b860ba119d736cc444a3fd2f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040026#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Andi Kleenfa8031a2012-01-26 00:09:12 +010028#include <asm/cpu_device_id.h>
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "speedstep-lib.h"
31
32
33/* speedstep_chipset:
34 * It is necessary to know which chipset is used. As accesses to
35 * this device occur at various places in this module, we need a
36 * static struct pci_dev * pointing to that device.
37 */
38static struct pci_dev *speedstep_chipset_dev;
39
40
41/* speedstep_processor
42 */
Rusty Russell1cce76c2009-11-17 14:39:53 -080043static enum speedstep_processor speedstep_processor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Mattia Dongili9a7d82a2005-11-30 22:00:59 +010045static u32 pmbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/*
48 * There are only two frequency states for each processor. Values
49 * are in kHz for the time being.
50 */
51static struct cpufreq_frequency_table speedstep_freqs[] = {
52 {SPEEDSTEP_HIGH, 0},
53 {SPEEDSTEP_LOW, 0},
54 {0, CPUFREQ_TABLE_END},
55};
56
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/**
Mattia Dongili9a7d82a2005-11-30 22:00:59 +010059 * speedstep_find_register - read the PMBASE address
60 *
61 * Returns: -ENODEV if no register could be found
62 */
Dave Jonesbbfebd62009-01-17 23:55:22 -050063static int speedstep_find_register(void)
Mattia Dongili9a7d82a2005-11-30 22:00:59 +010064{
65 if (!speedstep_chipset_dev)
66 return -ENODEV;
67
68 /* get PMBASE */
69 pci_read_config_dword(speedstep_chipset_dev, 0x40, &pmbase);
70 if (!(pmbase & 0x01)) {
71 printk(KERN_ERR "speedstep-ich: could not find speedstep register\n");
72 return -ENODEV;
73 }
74
75 pmbase &= 0xFFFFFFFE;
76 if (!pmbase) {
77 printk(KERN_ERR "speedstep-ich: could not find speedstep register\n");
78 return -ENODEV;
79 }
80
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020081 pr_debug("pmbase is 0x%x\n", pmbase);
Mattia Dongili9a7d82a2005-11-30 22:00:59 +010082 return 0;
83}
84
85/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * speedstep_set_state - set the SpeedStep state
87 * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
88 *
Rusty Russell394122a2009-06-11 22:59:58 +093089 * Tries to change the SpeedStep state. Can be called from
90 * smp_call_function_single.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
Dave Jonesbbfebd62009-01-17 23:55:22 -050092static void speedstep_set_state(unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 u8 pm2_blk;
95 u8 value;
96 unsigned long flags;
97
Mattia Dongili9a7d82a2005-11-30 22:00:59 +010098 if (state > 0x1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return;
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 /* Disable IRQs */
102 local_irq_save(flags);
103
104 /* read state */
105 value = inb(pmbase + 0x50);
106
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200107 pr_debug("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 /* write new state */
110 value &= 0xFE;
111 value |= state;
112
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200113 pr_debug("writing 0x%x to pmbase 0x%x + 0x50\n", value, pmbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 /* Disable bus master arbitration */
116 pm2_blk = inb(pmbase + 0x20);
117 pm2_blk |= 0x01;
118 outb(pm2_blk, (pmbase + 0x20));
119
120 /* Actual transition */
121 outb(value, (pmbase + 0x50));
122
123 /* Restore bus master arbitration */
124 pm2_blk &= 0xfe;
125 outb(pm2_blk, (pmbase + 0x20));
126
127 /* check if transition was successful */
128 value = inb(pmbase + 0x50);
129
130 /* Enable IRQs */
131 local_irq_restore(flags);
132
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200133 pr_debug("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Dave Jonesbbfebd62009-01-17 23:55:22 -0500135 if (state == (value & 0x1))
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200136 pr_debug("change to %u MHz succeeded\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500137 speedstep_get_frequency(speedstep_processor) / 1000);
138 else
139 printk(KERN_ERR "cpufreq: change failed - I/O error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 return;
142}
143
Rusty Russell394122a2009-06-11 22:59:58 +0930144/* Wrapper for smp_call_function_single. */
145static void _speedstep_set_state(void *_state)
146{
147 speedstep_set_state(*(unsigned int *)_state);
148}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150/**
151 * speedstep_activate - activate SpeedStep control in the chipset
152 *
153 * Tries to activate the SpeedStep status and control registers.
154 * Returns -EINVAL on an unsupported chipset, and zero on success.
155 */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500156static int speedstep_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 u16 value = 0;
159
160 if (!speedstep_chipset_dev)
161 return -EINVAL;
162
163 pci_read_config_word(speedstep_chipset_dev, 0x00A0, &value);
164 if (!(value & 0x08)) {
165 value |= 0x08;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200166 pr_debug("activating SpeedStep (TM) registers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 pci_write_config_word(speedstep_chipset_dev, 0x00A0, value);
168 }
169
170 return 0;
171}
172
173
174/**
175 * speedstep_detect_chipset - detect the Southbridge which contains SpeedStep logic
176 *
177 * Detects ICH2-M, ICH3-M and ICH4-M so far. The pci_dev points to
178 * the LPC bridge / PM module which contains all power-management
179 * functions. Returns the SPEEDSTEP_CHIPSET_-number for the detected
180 * chipset, or zero on failure.
181 */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500182static unsigned int speedstep_detect_chipset(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
185 PCI_DEVICE_ID_INTEL_82801DB_12,
Dave Jonesbbfebd62009-01-17 23:55:22 -0500186 PCI_ANY_ID, PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 NULL);
188 if (speedstep_chipset_dev)
189 return 4; /* 4-M */
190
191 speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
192 PCI_DEVICE_ID_INTEL_82801CA_12,
Dave Jonesbbfebd62009-01-17 23:55:22 -0500193 PCI_ANY_ID, PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 NULL);
195 if (speedstep_chipset_dev)
196 return 3; /* 3-M */
197
198
199 speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
200 PCI_DEVICE_ID_INTEL_82801BA_10,
Dave Jonesbbfebd62009-01-17 23:55:22 -0500201 PCI_ANY_ID, PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 NULL);
203 if (speedstep_chipset_dev) {
204 /* speedstep.c causes lockups on Dell Inspirons 8000 and
205 * 8100 which use a pretty old revision of the 82815
Masanari Iidac03c3012012-07-18 09:09:27 +0900206 * host bridge. Abort on these systems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 */
208 static struct pci_dev *hostbridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 hostbridge = pci_get_subsys(PCI_VENDOR_ID_INTEL,
211 PCI_DEVICE_ID_INTEL_82815_MC,
Dave Jonesbbfebd62009-01-17 23:55:22 -0500212 PCI_ANY_ID, PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 NULL);
214
215 if (!hostbridge)
216 return 2; /* 2-M */
217
Auke Kok44c10132007-06-08 15:46:36 -0700218 if (hostbridge->revision < 5) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200219 pr_debug("hostbridge does not support speedstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 speedstep_chipset_dev = NULL;
221 pci_dev_put(hostbridge);
222 return 0;
223 }
224
225 pci_dev_put(hostbridge);
226 return 2; /* 2-M */
227 }
228
229 return 0;
230}
231
Rusty Russell8dca15e2009-11-02 23:35:30 -0800232static void get_freq_data(void *_speed)
Rusty Russell394122a2009-06-11 22:59:58 +0930233{
Rusty Russell8dca15e2009-11-02 23:35:30 -0800234 unsigned int *speed = _speed;
Rusty Russell394122a2009-06-11 22:59:58 +0930235
Rusty Russell8dca15e2009-11-02 23:35:30 -0800236 *speed = speedstep_get_frequency(speedstep_processor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239static unsigned int speedstep_get(unsigned int cpu)
240{
Rusty Russell8dca15e2009-11-02 23:35:30 -0800241 unsigned int speed;
Rusty Russell394122a2009-06-11 22:59:58 +0930242
243 /* You're supposed to ensure CPU is online. */
Rusty Russell8dca15e2009-11-02 23:35:30 -0800244 if (smp_call_function_single(cpu, get_freq_data, &speed, 1) != 0)
Rusty Russell394122a2009-06-11 22:59:58 +0930245 BUG();
246
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200247 pr_debug("detected %u kHz as current frequency\n", speed);
Rusty Russell8dca15e2009-11-02 23:35:30 -0800248 return speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
251/**
252 * speedstep_target - set a new CPUFreq policy
253 * @policy: new policy
254 * @target_freq: the target frequency
Dave Jonesbbfebd62009-01-17 23:55:22 -0500255 * @relation: how that frequency relates to achieved frequency
256 * (CPUFREQ_RELATION_L or CPUFREQ_RELATION_H)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 *
258 * Sets a new CPUFreq policy.
259 */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500260static int speedstep_target(struct cpufreq_policy *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 unsigned int target_freq,
262 unsigned int relation)
263{
Rusty Russell394122a2009-06-11 22:59:58 +0930264 unsigned int newstate = 0, policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 struct cpufreq_freqs freqs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Dave Jonesbbfebd62009-01-17 23:55:22 -0500267 if (cpufreq_frequency_table_target(policy, &speedstep_freqs[0],
268 target_freq, relation, &newstate))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return -EINVAL;
270
Rusty Russell394122a2009-06-11 22:59:58 +0930271 policy_cpu = cpumask_any_and(policy->cpus, cpu_online_mask);
272 freqs.old = speedstep_get(policy_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 freqs.new = speedstep_freqs[newstate].frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200275 pr_debug("transiting from %u to %u kHz\n", freqs.old, freqs.new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 /* no transition necessary */
278 if (freqs.old == freqs.new)
279 return 0;
280
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530281 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Rusty Russell394122a2009-06-11 22:59:58 +0930283 smp_call_function_single(policy_cpu, _speedstep_set_state, &newstate,
284 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530286 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 return 0;
289}
290
291
292/**
293 * speedstep_verify - verifies a new CPUFreq policy
294 * @policy: new policy
295 *
296 * Limit must be within speedstep_low_freq and speedstep_high_freq, with
297 * at least one border included.
298 */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500299static int speedstep_verify(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 return cpufreq_frequency_table_verify(policy, &speedstep_freqs[0]);
302}
303
Rusty Russell394122a2009-06-11 22:59:58 +0930304struct get_freqs {
305 struct cpufreq_policy *policy;
306 int ret;
307};
308
309static void get_freqs_on_cpu(void *_get_freqs)
310{
311 struct get_freqs *get_freqs = _get_freqs;
312
313 get_freqs->ret =
314 speedstep_get_freqs(speedstep_processor,
315 &speedstep_freqs[SPEEDSTEP_LOW].frequency,
316 &speedstep_freqs[SPEEDSTEP_HIGH].frequency,
317 &get_freqs->policy->cpuinfo.transition_latency,
318 &speedstep_set_state);
319}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321static int speedstep_cpu_init(struct cpufreq_policy *policy)
322{
Rusty Russell394122a2009-06-11 22:59:58 +0930323 int result;
324 unsigned int policy_cpu, speed;
325 struct get_freqs gf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* only run on CPU to be set, or on its sibling */
328#ifdef CONFIG_SMP
Rusty Russell7ad728f2009-03-13 14:49:50 +1030329 cpumask_copy(policy->cpus, cpu_sibling_mask(policy->cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330#endif
Rusty Russell394122a2009-06-11 22:59:58 +0930331 policy_cpu = cpumask_any_and(policy->cpus, cpu_online_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Mattia Dongili1a107602005-12-02 21:59:41 +0100333 /* detect low and high frequency and transition latency */
Rusty Russell394122a2009-06-11 22:59:58 +0930334 gf.policy = policy;
335 smp_call_function_single(policy_cpu, get_freqs_on_cpu, &gf, 1);
336 if (gf.ret)
337 return gf.ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* get current speed setting */
Rusty Russell394122a2009-06-11 22:59:58 +0930340 speed = speedstep_get(policy_cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (!speed)
342 return -EIO;
343
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200344 pr_debug("currently at %s speed setting - %i MHz\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500345 (speed == speedstep_freqs[SPEEDSTEP_LOW].frequency)
346 ? "low" : "high",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 (speed / 1000));
348
349 /* cpuinfo and default policy values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 policy->cur = speed;
351
352 result = cpufreq_frequency_table_cpuinfo(policy, speedstep_freqs);
353 if (result)
Dave Jonesbbfebd62009-01-17 23:55:22 -0500354 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Dave Jonesbbfebd62009-01-17 23:55:22 -0500356 cpufreq_frequency_table_get_attr(speedstep_freqs, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 return 0;
359}
360
361
362static int speedstep_cpu_exit(struct cpufreq_policy *policy)
363{
364 cpufreq_frequency_table_put_attr(policy->cpu);
365 return 0;
366}
367
Dave Jonesbbfebd62009-01-17 23:55:22 -0500368static struct freq_attr *speedstep_attr[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 &cpufreq_freq_attr_scaling_available_freqs,
370 NULL,
371};
372
373
Linus Torvalds221dee22007-02-26 14:55:48 -0800374static struct cpufreq_driver speedstep_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 .name = "speedstep-ich",
376 .verify = speedstep_verify,
377 .target = speedstep_target,
378 .init = speedstep_cpu_init,
379 .exit = speedstep_cpu_exit,
380 .get = speedstep_get,
381 .owner = THIS_MODULE,
382 .attr = speedstep_attr,
383};
384
Andi Kleenfa8031a2012-01-26 00:09:12 +0100385static const struct x86_cpu_id ss_smi_ids[] = {
386 { X86_VENDOR_INTEL, 6, 0xb, },
387 { X86_VENDOR_INTEL, 6, 0x8, },
388 { X86_VENDOR_INTEL, 15, 2 },
389 {}
390};
391#if 0
392/* Autoload or not? Do not for now. */
393MODULE_DEVICE_TABLE(x86cpu, ss_smi_ids);
394#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396/**
397 * speedstep_init - initializes the SpeedStep CPUFreq driver
398 *
399 * Initializes the SpeedStep support. Returns -ENODEV on unsupported
400 * devices, -EINVAL on problems during initiatization, and zero on
401 * success.
402 */
403static int __init speedstep_init(void)
404{
Andi Kleenfa8031a2012-01-26 00:09:12 +0100405 if (!x86_match_cpu(ss_smi_ids))
406 return -ENODEV;
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /* detect processor */
409 speedstep_processor = speedstep_detect_processor();
410 if (!speedstep_processor) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200411 pr_debug("Intel(R) SpeedStep(TM) capable processor "
Dave Jonesbbfebd62009-01-17 23:55:22 -0500412 "not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return -ENODEV;
414 }
415
416 /* detect chipset */
417 if (!speedstep_detect_chipset()) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200418 pr_debug("Intel(R) SpeedStep(TM) for this chipset not "
Dave Jonesbbfebd62009-01-17 23:55:22 -0500419 "(yet) available.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return -ENODEV;
421 }
422
423 /* activate speedstep support */
424 if (speedstep_activate()) {
425 pci_dev_put(speedstep_chipset_dev);
426 return -EINVAL;
427 }
428
Mattia Dongili9a7d82a2005-11-30 22:00:59 +0100429 if (speedstep_find_register())
430 return -ENODEV;
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return cpufreq_register_driver(&speedstep_driver);
433}
434
435
436/**
437 * speedstep_exit - unregisters SpeedStep support
438 *
439 * Unregisters SpeedStep support.
440 */
441static void __exit speedstep_exit(void)
442{
443 pci_dev_put(speedstep_chipset_dev);
444 cpufreq_unregister_driver(&speedstep_driver);
445}
446
447
Dave Jonesbbfebd62009-01-17 23:55:22 -0500448MODULE_AUTHOR("Dave Jones <davej@redhat.com>, "
449 "Dominik Brodowski <linux@brodo.de>");
450MODULE_DESCRIPTION("Speedstep driver for Intel mobile processors on chipsets "
451 "with ICH-M southbridges.");
452MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454module_init(speedstep_init);
455module_exit(speedstep_exit);