blob: fbbcb88db061652a35a573538e8cd51fabe6fa6b [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
Joe Perches1c5864e2016-04-05 13:28:25 -070021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/cpufreq.h>
27#include <linux/pci.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040028#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Andi Kleenfa8031a2012-01-26 00:09:12 +010030#include <asm/cpu_device_id.h>
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "speedstep-lib.h"
33
34
35/* speedstep_chipset:
36 * It is necessary to know which chipset is used. As accesses to
37 * this device occur at various places in this module, we need a
38 * static struct pci_dev * pointing to that device.
39 */
40static struct pci_dev *speedstep_chipset_dev;
41
42
43/* speedstep_processor
44 */
Rusty Russell1cce76c2009-11-17 14:39:53 -080045static enum speedstep_processor speedstep_processor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Mattia Dongili9a7d82a2005-11-30 22:00:59 +010047static u32 pmbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
50 * There are only two frequency states for each processor. Values
51 * are in kHz for the time being.
52 */
53static struct cpufreq_frequency_table speedstep_freqs[] = {
Viresh Kumar7f4b0462014-03-28 19:11:47 +053054 {0, SPEEDSTEP_HIGH, 0},
55 {0, SPEEDSTEP_LOW, 0},
56 {0, 0, CPUFREQ_TABLE_END},
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/**
Mattia Dongili9a7d82a2005-11-30 22:00:59 +010061 * speedstep_find_register - read the PMBASE address
62 *
63 * Returns: -ENODEV if no register could be found
64 */
Dave Jonesbbfebd62009-01-17 23:55:22 -050065static int speedstep_find_register(void)
Mattia Dongili9a7d82a2005-11-30 22:00:59 +010066{
67 if (!speedstep_chipset_dev)
68 return -ENODEV;
69
70 /* get PMBASE */
71 pci_read_config_dword(speedstep_chipset_dev, 0x40, &pmbase);
72 if (!(pmbase & 0x01)) {
Joe Perches1c5864e2016-04-05 13:28:25 -070073 pr_err("could not find speedstep register\n");
Mattia Dongili9a7d82a2005-11-30 22:00:59 +010074 return -ENODEV;
75 }
76
77 pmbase &= 0xFFFFFFFE;
78 if (!pmbase) {
Joe Perches1c5864e2016-04-05 13:28:25 -070079 pr_err("could not find speedstep register\n");
Mattia Dongili9a7d82a2005-11-30 22:00:59 +010080 return -ENODEV;
81 }
82
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +020083 pr_debug("pmbase is 0x%x\n", pmbase);
Mattia Dongili9a7d82a2005-11-30 22:00:59 +010084 return 0;
85}
86
87/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * speedstep_set_state - set the SpeedStep state
89 * @state: new processor frequency state (SPEEDSTEP_LOW or SPEEDSTEP_HIGH)
90 *
Rusty Russell394122a2009-06-11 22:59:58 +093091 * Tries to change the SpeedStep state. Can be called from
92 * smp_call_function_single.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
Dave Jonesbbfebd62009-01-17 23:55:22 -050094static void speedstep_set_state(unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 u8 pm2_blk;
97 u8 value;
98 unsigned long flags;
99
Mattia Dongili9a7d82a2005-11-30 22:00:59 +0100100 if (state > 0x1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return;
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 /* Disable IRQs */
104 local_irq_save(flags);
105
106 /* read state */
107 value = inb(pmbase + 0x50);
108
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200109 pr_debug("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 /* write new state */
112 value &= 0xFE;
113 value |= state;
114
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200115 pr_debug("writing 0x%x to pmbase 0x%x + 0x50\n", value, pmbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
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
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200135 pr_debug("read at pmbase 0x%x + 0x50 returned 0x%x\n", pmbase, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Dave Jonesbbfebd62009-01-17 23:55:22 -0500137 if (state == (value & 0x1))
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200138 pr_debug("change to %u MHz succeeded\n",
Dave Jonesbbfebd62009-01-17 23:55:22 -0500139 speedstep_get_frequency(speedstep_processor) / 1000);
140 else
Joe Perches1c5864e2016-04-05 13:28:25 -0700141 pr_err("change failed - I/O error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 return;
144}
145
Rusty Russell394122a2009-06-11 22:59:58 +0930146/* Wrapper for smp_call_function_single. */
147static void _speedstep_set_state(void *_state)
148{
149 speedstep_set_state(*(unsigned int *)_state);
150}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152/**
153 * speedstep_activate - activate SpeedStep control in the chipset
154 *
155 * Tries to activate the SpeedStep status and control registers.
156 * Returns -EINVAL on an unsupported chipset, and zero on success.
157 */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500158static int speedstep_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 u16 value = 0;
161
162 if (!speedstep_chipset_dev)
163 return -EINVAL;
164
165 pci_read_config_word(speedstep_chipset_dev, 0x00A0, &value);
166 if (!(value & 0x08)) {
167 value |= 0x08;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200168 pr_debug("activating SpeedStep (TM) registers\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 pci_write_config_word(speedstep_chipset_dev, 0x00A0, value);
170 }
171
172 return 0;
173}
174
175
176/**
177 * speedstep_detect_chipset - detect the Southbridge which contains SpeedStep logic
178 *
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.
183 */
Dave Jonesbbfebd62009-01-17 23:55:22 -0500184static unsigned int speedstep_detect_chipset(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
187 PCI_DEVICE_ID_INTEL_82801DB_12,
Dave Jonesbbfebd62009-01-17 23:55:22 -0500188 PCI_ANY_ID, PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 NULL);
190 if (speedstep_chipset_dev)
191 return 4; /* 4-M */
192
193 speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
194 PCI_DEVICE_ID_INTEL_82801CA_12,
Dave Jonesbbfebd62009-01-17 23:55:22 -0500195 PCI_ANY_ID, PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 NULL);
197 if (speedstep_chipset_dev)
198 return 3; /* 3-M */
199
200
201 speedstep_chipset_dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
202 PCI_DEVICE_ID_INTEL_82801BA_10,
Dave Jonesbbfebd62009-01-17 23:55:22 -0500203 PCI_ANY_ID, PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 NULL);
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
Masanari Iidac03c3012012-07-18 09:09:27 +0900208 * host bridge. Abort on these systems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
Gustavo A. R. Silvac95a05f2017-07-04 22:09:21 -0500210 struct pci_dev *hostbridge;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 hostbridge = pci_get_subsys(PCI_VENDOR_ID_INTEL,
213 PCI_DEVICE_ID_INTEL_82815_MC,
Dave Jonesbbfebd62009-01-17 23:55:22 -0500214 PCI_ANY_ID, PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 NULL);
216
217 if (!hostbridge)
218 return 2; /* 2-M */
219
Auke Kok44c10132007-06-08 15:46:36 -0700220 if (hostbridge->revision < 5) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200221 pr_debug("hostbridge does not support speedstep\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 speedstep_chipset_dev = NULL;
223 pci_dev_put(hostbridge);
224 return 0;
225 }
226
227 pci_dev_put(hostbridge);
228 return 2; /* 2-M */
229 }
230
231 return 0;
232}
233
Rusty Russell8dca15e2009-11-02 23:35:30 -0800234static void get_freq_data(void *_speed)
Rusty Russell394122a2009-06-11 22:59:58 +0930235{
Rusty Russell8dca15e2009-11-02 23:35:30 -0800236 unsigned int *speed = _speed;
Rusty Russell394122a2009-06-11 22:59:58 +0930237
Rusty Russell8dca15e2009-11-02 23:35:30 -0800238 *speed = speedstep_get_frequency(speedstep_processor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241static unsigned int speedstep_get(unsigned int cpu)
242{
Rusty Russell8dca15e2009-11-02 23:35:30 -0800243 unsigned int speed;
Rusty Russell394122a2009-06-11 22:59:58 +0930244
245 /* You're supposed to ensure CPU is online. */
Rusty Russell8dca15e2009-11-02 23:35:30 -0800246 if (smp_call_function_single(cpu, get_freq_data, &speed, 1) != 0)
Rusty Russell394122a2009-06-11 22:59:58 +0930247 BUG();
248
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200249 pr_debug("detected %u kHz as current frequency\n", speed);
Rusty Russell8dca15e2009-11-02 23:35:30 -0800250 return speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/**
254 * speedstep_target - set a new CPUFreq policy
255 * @policy: new policy
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530256 * @index: index of target frequency
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 *
258 * Sets a new CPUFreq policy.
259 */
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530260static int speedstep_target(struct cpufreq_policy *policy, unsigned int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530262 unsigned int policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Rusty Russell394122a2009-06-11 22:59:58 +0930264 policy_cpu = cpumask_any_and(policy->cpus, cpu_online_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530266 smp_call_function_single(policy_cpu, _speedstep_set_state, &index,
Rusty Russell394122a2009-06-11 22:59:58 +0930267 true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return 0;
270}
271
272
Rusty Russell394122a2009-06-11 22:59:58 +0930273struct get_freqs {
274 struct cpufreq_policy *policy;
275 int ret;
276};
277
278static void get_freqs_on_cpu(void *_get_freqs)
279{
280 struct get_freqs *get_freqs = _get_freqs;
281
282 get_freqs->ret =
283 speedstep_get_freqs(speedstep_processor,
284 &speedstep_freqs[SPEEDSTEP_LOW].frequency,
285 &speedstep_freqs[SPEEDSTEP_HIGH].frequency,
286 &get_freqs->policy->cpuinfo.transition_latency,
287 &speedstep_set_state);
288}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290static int speedstep_cpu_init(struct cpufreq_policy *policy)
291{
Viresh Kumar4b157682013-10-03 20:29:04 +0530292 unsigned int policy_cpu;
Rusty Russell394122a2009-06-11 22:59:58 +0930293 struct get_freqs gf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 /* only run on CPU to be set, or on its sibling */
296#ifdef CONFIG_SMP
Bartosz Golaszewski265ea622015-05-26 15:11:34 +0200297 cpumask_copy(policy->cpus, topology_sibling_cpumask(policy->cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298#endif
Rusty Russell394122a2009-06-11 22:59:58 +0930299 policy_cpu = cpumask_any_and(policy->cpus, cpu_online_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Mattia Dongili1a107602005-12-02 21:59:41 +0100301 /* detect low and high frequency and transition latency */
Rusty Russell394122a2009-06-11 22:59:58 +0930302 gf.policy = policy;
303 smp_call_function_single(policy_cpu, get_freqs_on_cpu, &gf, 1);
304 if (gf.ret)
305 return gf.ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Viresh Kumar2d28b032018-02-26 10:39:08 +0530307 policy->freq_table = speedstep_freqs;
308
309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312
Linus Torvalds221dee22007-02-26 14:55:48 -0800313static struct cpufreq_driver speedstep_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 .name = "speedstep-ich",
Viresh Kumar3be13942013-10-03 20:28:28 +0530315 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530316 .target_index = speedstep_target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 .init = speedstep_cpu_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 .get = speedstep_get,
Viresh Kumar3be13942013-10-03 20:28:28 +0530319 .attr = cpufreq_generic_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320};
321
Andi Kleenfa8031a2012-01-26 00:09:12 +0100322static const struct x86_cpu_id ss_smi_ids[] = {
323 { X86_VENDOR_INTEL, 6, 0xb, },
324 { X86_VENDOR_INTEL, 6, 0x8, },
325 { X86_VENDOR_INTEL, 15, 2 },
326 {}
327};
328#if 0
329/* Autoload or not? Do not for now. */
330MODULE_DEVICE_TABLE(x86cpu, ss_smi_ids);
331#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333/**
334 * speedstep_init - initializes the SpeedStep CPUFreq driver
335 *
336 * Initializes the SpeedStep support. Returns -ENODEV on unsupported
337 * devices, -EINVAL on problems during initiatization, and zero on
338 * success.
339 */
340static int __init speedstep_init(void)
341{
Andi Kleenfa8031a2012-01-26 00:09:12 +0100342 if (!x86_match_cpu(ss_smi_ids))
343 return -ENODEV;
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /* detect processor */
346 speedstep_processor = speedstep_detect_processor();
347 if (!speedstep_processor) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200348 pr_debug("Intel(R) SpeedStep(TM) capable processor "
Dave Jonesbbfebd62009-01-17 23:55:22 -0500349 "not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return -ENODEV;
351 }
352
353 /* detect chipset */
354 if (!speedstep_detect_chipset()) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200355 pr_debug("Intel(R) SpeedStep(TM) for this chipset not "
Dave Jonesbbfebd62009-01-17 23:55:22 -0500356 "(yet) available.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -ENODEV;
358 }
359
360 /* activate speedstep support */
361 if (speedstep_activate()) {
362 pci_dev_put(speedstep_chipset_dev);
363 return -EINVAL;
364 }
365
Mattia Dongili9a7d82a2005-11-30 22:00:59 +0100366 if (speedstep_find_register())
367 return -ENODEV;
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return cpufreq_register_driver(&speedstep_driver);
370}
371
372
373/**
374 * speedstep_exit - unregisters SpeedStep support
375 *
376 * Unregisters SpeedStep support.
377 */
378static void __exit speedstep_exit(void)
379{
380 pci_dev_put(speedstep_chipset_dev);
381 cpufreq_unregister_driver(&speedstep_driver);
382}
383
384
Dave Jonesd5e80b42014-12-19 11:20:43 -0500385MODULE_AUTHOR("Dave Jones, Dominik Brodowski <linux@brodo.de>");
Dave Jonesbbfebd62009-01-17 23:55:22 -0500386MODULE_DESCRIPTION("Speedstep driver for Intel mobile processors on chipsets "
387 "with ICH-M southbridges.");
388MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390module_init(speedstep_init);
391module_exit(speedstep_exit);