]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/i386/kernel/cpu/cpufreq/acpi-cpufreq.c
[CPUFREQ] Longhaul - Add voltage scaling to driver
[mirror_ubuntu-bionic-kernel.git] / arch / i386 / kernel / cpu / cpufreq / acpi-cpufreq.c
CommitLineData
1da177e4
LT
1/*
2 * acpi-cpufreq.c - ACPI Processor P-States Driver ($Revision: 1.3 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 * Copyright (C) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
1da177e4
LT
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/cpufreq.h>
31#include <linux/proc_fs.h>
32#include <linux/seq_file.h>
d395bf12 33#include <linux/compiler.h>
4e57b681 34#include <linux/sched.h> /* current */
1da177e4
LT
35#include <asm/io.h>
36#include <asm/delay.h>
37#include <asm/uaccess.h>
38
39#include <linux/acpi.h>
40#include <acpi/processor.h>
41
1da177e4
LT
42#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)
43
44MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
45MODULE_DESCRIPTION("ACPI Processor P-States Driver");
46MODULE_LICENSE("GPL");
47
48
49struct cpufreq_acpi_io {
09b4d1ee 50 struct acpi_processor_performance *acpi_data;
1da177e4
LT
51 struct cpufreq_frequency_table *freq_table;
52 unsigned int resume;
53};
54
55static struct cpufreq_acpi_io *acpi_io_data[NR_CPUS];
09b4d1ee 56static struct acpi_processor_performance *acpi_perf_data[NR_CPUS];
1da177e4
LT
57
58static struct cpufreq_driver acpi_cpufreq_driver;
59
d395bf12
VP
60static unsigned int acpi_pstate_strict;
61
1da177e4
LT
62static int
63acpi_processor_write_port(
64 u16 port,
65 u8 bit_width,
66 u32 value)
67{
68 if (bit_width <= 8) {
69 outb(value, port);
70 } else if (bit_width <= 16) {
71 outw(value, port);
72 } else if (bit_width <= 32) {
73 outl(value, port);
74 } else {
75 return -ENODEV;
76 }
77 return 0;
78}
79
80static int
81acpi_processor_read_port(
82 u16 port,
83 u8 bit_width,
84 u32 *ret)
85{
86 *ret = 0;
87 if (bit_width <= 8) {
88 *ret = inb(port);
89 } else if (bit_width <= 16) {
90 *ret = inw(port);
91 } else if (bit_width <= 32) {
92 *ret = inl(port);
93 } else {
94 return -ENODEV;
95 }
96 return 0;
97}
98
99static int
100acpi_processor_set_performance (
101 struct cpufreq_acpi_io *data,
102 unsigned int cpu,
103 int state)
104{
105 u16 port = 0;
106 u8 bit_width = 0;
1da177e4 107 int i = 0;
1da177e4
LT
108 int ret = 0;
109 u32 value = 0;
1da177e4 110 int retval;
09b4d1ee 111 struct acpi_processor_performance *perf;
1da177e4
LT
112
113 dprintk("acpi_processor_set_performance\n");
114
09b4d1ee
VP
115 retval = 0;
116 perf = data->acpi_data;
117 if (state == perf->state) {
1da177e4
LT
118 if (unlikely(data->resume)) {
119 dprintk("Called after resume, resetting to P%d\n", state);
120 data->resume = 0;
121 } else {
122 dprintk("Already at target state (P%d)\n", state);
09b4d1ee 123 return (retval);
1da177e4
LT
124 }
125 }
126
09b4d1ee 127 dprintk("Transitioning from P%d to P%d\n", perf->state, state);
1da177e4
LT
128
129 /*
130 * First we write the target state's 'control' value to the
131 * control_register.
132 */
133
09b4d1ee
VP
134 port = perf->control_register.address;
135 bit_width = perf->control_register.bit_width;
136 value = (u32) perf->states[state].control;
1da177e4
LT
137
138 dprintk("Writing 0x%08x to port 0x%04x\n", value, port);
139
140 ret = acpi_processor_write_port(port, bit_width, value);
141 if (ret) {
142 dprintk("Invalid port width 0x%04x\n", bit_width);
09b4d1ee 143 return (ret);
1da177e4
LT
144 }
145
146 /*
d395bf12
VP
147 * Assume the write went through when acpi_pstate_strict is not used.
148 * As read status_register is an expensive operation and there
149 * are no specific error cases where an IO port write will fail.
1da177e4 150 */
d395bf12
VP
151 if (acpi_pstate_strict) {
152 /* Then we read the 'status_register' and compare the value
153 * with the target state's 'status' to make sure the
154 * transition was successful.
155 * Note that we'll poll for up to 1ms (100 cycles of 10us)
156 * before giving up.
157 */
158
09b4d1ee
VP
159 port = perf->status_register.address;
160 bit_width = perf->status_register.bit_width;
d395bf12
VP
161
162 dprintk("Looking for 0x%08x from port 0x%04x\n",
09b4d1ee 163 (u32) perf->states[state].status, port);
d395bf12 164
09b4d1ee 165 for (i = 0; i < 100; i++) {
d395bf12
VP
166 ret = acpi_processor_read_port(port, bit_width, &value);
167 if (ret) {
168 dprintk("Invalid port width 0x%04x\n", bit_width);
09b4d1ee 169 return (ret);
d395bf12 170 }
09b4d1ee 171 if (value == (u32) perf->states[state].status)
d395bf12
VP
172 break;
173 udelay(10);
1da177e4 174 }
d395bf12 175 } else {
09b4d1ee 176 value = (u32) perf->states[state].status;
1da177e4
LT
177 }
178
09b4d1ee 179 if (unlikely(value != (u32) perf->states[state].status)) {
1da177e4
LT
180 printk(KERN_WARNING "acpi-cpufreq: Transition failed\n");
181 retval = -ENODEV;
09b4d1ee 182 return (retval);
1da177e4
LT
183 }
184
185 dprintk("Transition successful after %d microseconds\n", i * 10);
186
09b4d1ee 187 perf->state = state;
1da177e4
LT
188 return (retval);
189}
190
191
192static int
193acpi_cpufreq_target (
194 struct cpufreq_policy *policy,
195 unsigned int target_freq,
196 unsigned int relation)
197{
198 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
09b4d1ee
VP
199 struct acpi_processor_performance *perf;
200 struct cpufreq_freqs freqs;
201 cpumask_t online_policy_cpus;
202 cpumask_t saved_mask;
203 cpumask_t set_mask;
204 cpumask_t covered_cpus;
205 unsigned int cur_state = 0;
1da177e4
LT
206 unsigned int next_state = 0;
207 unsigned int result = 0;
09b4d1ee
VP
208 unsigned int j;
209 unsigned int tmp;
1da177e4
LT
210
211 dprintk("acpi_cpufreq_setpolicy\n");
212
213 result = cpufreq_frequency_table_target(policy,
214 data->freq_table,
215 target_freq,
216 relation,
217 &next_state);
09b4d1ee 218 if (unlikely(result))
1da177e4
LT
219 return (result);
220
09b4d1ee
VP
221 perf = data->acpi_data;
222 cur_state = perf->state;
223 freqs.old = data->freq_table[cur_state].frequency;
224 freqs.new = data->freq_table[next_state].frequency;
225
7e1f19e5 226#ifdef CONFIG_HOTPLUG_CPU
09b4d1ee
VP
227 /* cpufreq holds the hotplug lock, so we are safe from here on */
228 cpus_and(online_policy_cpus, cpu_online_map, policy->cpus);
7e1f19e5
AM
229#else
230 online_policy_cpus = policy->cpus;
231#endif
1da177e4 232
09b4d1ee
VP
233 for_each_cpu_mask(j, online_policy_cpus) {
234 freqs.cpu = j;
235 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
236 }
237
238 /*
239 * We need to call driver->target() on all or any CPU in
240 * policy->cpus, depending on policy->shared_type.
241 */
242 saved_mask = current->cpus_allowed;
243 cpus_clear(covered_cpus);
244 for_each_cpu_mask(j, online_policy_cpus) {
245 /*
246 * Support for SMP systems.
247 * Make sure we are running on CPU that wants to change freq
248 */
249 cpus_clear(set_mask);
250 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
251 cpus_or(set_mask, set_mask, online_policy_cpus);
252 else
253 cpu_set(j, set_mask);
254
255 set_cpus_allowed(current, set_mask);
256 if (unlikely(!cpu_isset(smp_processor_id(), set_mask))) {
257 dprintk("couldn't limit to CPUs in this domain\n");
258 result = -EAGAIN;
259 break;
260 }
261
262 result = acpi_processor_set_performance (data, j, next_state);
263 if (result) {
264 result = -EAGAIN;
265 break;
266 }
267
268 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
269 break;
270
271 cpu_set(j, covered_cpus);
272 }
273
274 for_each_cpu_mask(j, online_policy_cpus) {
275 freqs.cpu = j;
276 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
277 }
1da177e4 278
09b4d1ee
VP
279 if (unlikely(result)) {
280 /*
281 * We have failed halfway through the frequency change.
282 * We have sent callbacks to online_policy_cpus and
283 * acpi_processor_set_performance() has been called on
284 * coverd_cpus. Best effort undo..
285 */
286
287 if (!cpus_empty(covered_cpus)) {
288 for_each_cpu_mask(j, covered_cpus) {
289 policy->cpu = j;
290 acpi_processor_set_performance (data,
291 j,
292 cur_state);
293 }
294 }
295
296 tmp = freqs.new;
297 freqs.new = freqs.old;
298 freqs.old = tmp;
299 for_each_cpu_mask(j, online_policy_cpus) {
300 freqs.cpu = j;
301 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
302 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
303 }
304 }
305
306 set_cpus_allowed(current, saved_mask);
1da177e4
LT
307 return (result);
308}
309
310
311static int
312acpi_cpufreq_verify (
313 struct cpufreq_policy *policy)
314{
315 unsigned int result = 0;
316 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
317
318 dprintk("acpi_cpufreq_verify\n");
319
320 result = cpufreq_frequency_table_verify(policy,
321 data->freq_table);
322
323 return (result);
324}
325
326
327static unsigned long
328acpi_cpufreq_guess_freq (
329 struct cpufreq_acpi_io *data,
330 unsigned int cpu)
331{
09b4d1ee
VP
332 struct acpi_processor_performance *perf = data->acpi_data;
333
1da177e4
LT
334 if (cpu_khz) {
335 /* search the closest match to cpu_khz */
336 unsigned int i;
337 unsigned long freq;
09b4d1ee 338 unsigned long freqn = perf->states[0].core_frequency * 1000;
1da177e4 339
09b4d1ee 340 for (i = 0; i < (perf->state_count - 1); i++) {
1da177e4 341 freq = freqn;
09b4d1ee 342 freqn = perf->states[i+1].core_frequency * 1000;
1da177e4 343 if ((2 * cpu_khz) > (freqn + freq)) {
09b4d1ee 344 perf->state = i;
1da177e4
LT
345 return (freq);
346 }
347 }
09b4d1ee 348 perf->state = perf->state_count - 1;
1da177e4 349 return (freqn);
09b4d1ee 350 } else {
1da177e4 351 /* assume CPU is at P0... */
09b4d1ee
VP
352 perf->state = 0;
353 return perf->states[0].core_frequency * 1000;
354 }
1da177e4
LT
355}
356
357
09b4d1ee
VP
358/*
359 * acpi_cpufreq_early_init - initialize ACPI P-States library
360 *
361 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
362 * in order to determine correct frequency and voltage pairings. We can
363 * do _PDC and _PSD and find out the processor dependency for the
364 * actual init that will happen later...
365 */
366static int acpi_cpufreq_early_init_acpi(void)
367{
368 struct acpi_processor_performance *data;
369 unsigned int i, j;
370
371 dprintk("acpi_cpufreq_early_init\n");
372
fb1bb34d 373 for_each_possible_cpu(i) {
09b4d1ee
VP
374 data = kzalloc(sizeof(struct acpi_processor_performance),
375 GFP_KERNEL);
376 if (!data) {
fb1bb34d 377 for_each_possible_cpu(j) {
09b4d1ee
VP
378 kfree(acpi_perf_data[j]);
379 acpi_perf_data[j] = NULL;
380 }
381 return (-ENOMEM);
382 }
383 acpi_perf_data[i] = data;
384 }
385
386 /* Do initialization in ACPI core */
12e704db 387 return acpi_processor_preregister_performance(acpi_perf_data);
09b4d1ee
VP
388}
389
1da177e4
LT
390static int
391acpi_cpufreq_cpu_init (
392 struct cpufreq_policy *policy)
393{
394 unsigned int i;
395 unsigned int cpu = policy->cpu;
396 struct cpufreq_acpi_io *data;
397 unsigned int result = 0;
152bf8c5 398 struct cpuinfo_x86 *c = &cpu_data[policy->cpu];
09b4d1ee 399 struct acpi_processor_performance *perf;
1da177e4 400
1da177e4 401 dprintk("acpi_cpufreq_cpu_init\n");
1da177e4 402
09b4d1ee
VP
403 if (!acpi_perf_data[cpu])
404 return (-ENODEV);
405
bfdc708d 406 data = kzalloc(sizeof(struct cpufreq_acpi_io), GFP_KERNEL);
1da177e4
LT
407 if (!data)
408 return (-ENOMEM);
1da177e4 409
09b4d1ee 410 data->acpi_data = acpi_perf_data[cpu];
1da177e4
LT
411 acpi_io_data[cpu] = data;
412
09b4d1ee 413 result = acpi_processor_register_performance(data->acpi_data, cpu);
1da177e4
LT
414
415 if (result)
416 goto err_free;
417
09b4d1ee 418 perf = data->acpi_data;
09b4d1ee 419 policy->shared_type = perf->shared_type;
46f18e3a
VP
420 /*
421 * Will let policy->cpus know about dependency only when software
422 * coordination is required.
423 */
424 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
425 policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
426 policy->cpus = perf->shared_cpu_map;
09b4d1ee 427
152bf8c5 428 if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
1da177e4
LT
429 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
430 }
431
432 /* capability check */
09b4d1ee 433 if (perf->state_count <= 1) {
1da177e4
LT
434 dprintk("No P-States\n");
435 result = -ENODEV;
436 goto err_unreg;
437 }
09b4d1ee
VP
438
439 if ((perf->control_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO) ||
440 (perf->status_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
1da177e4 441 dprintk("Unsupported address space [%d, %d]\n",
09b4d1ee
VP
442 (u32) (perf->control_register.space_id),
443 (u32) (perf->status_register.space_id));
1da177e4
LT
444 result = -ENODEV;
445 goto err_unreg;
446 }
447
448 /* alloc freq_table */
09b4d1ee 449 data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) * (perf->state_count + 1), GFP_KERNEL);
1da177e4
LT
450 if (!data->freq_table) {
451 result = -ENOMEM;
452 goto err_unreg;
453 }
454
455 /* detect transition latency */
456 policy->cpuinfo.transition_latency = 0;
09b4d1ee
VP
457 for (i=0; i<perf->state_count; i++) {
458 if ((perf->states[i].transition_latency * 1000) > policy->cpuinfo.transition_latency)
459 policy->cpuinfo.transition_latency = perf->states[i].transition_latency * 1000;
1da177e4
LT
460 }
461 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
462
463 /* The current speed is unknown and not detectable by ACPI... */
464 policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
465
466 /* table init */
09b4d1ee 467 for (i=0; i<=perf->state_count; i++)
1da177e4
LT
468 {
469 data->freq_table[i].index = i;
09b4d1ee
VP
470 if (i<perf->state_count)
471 data->freq_table[i].frequency = perf->states[i].core_frequency * 1000;
1da177e4
LT
472 else
473 data->freq_table[i].frequency = CPUFREQ_TABLE_END;
474 }
475
476 result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
477 if (result) {
478 goto err_freqfree;
479 }
480
481 /* notify BIOS that we exist */
482 acpi_processor_notify_smm(THIS_MODULE);
483
484 printk(KERN_INFO "acpi-cpufreq: CPU%u - ACPI performance management activated.\n",
485 cpu);
09b4d1ee 486 for (i = 0; i < perf->state_count; i++)
1da177e4 487 dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
09b4d1ee
VP
488 (i == perf->state?'*':' '), i,
489 (u32) perf->states[i].core_frequency,
490 (u32) perf->states[i].power,
491 (u32) perf->states[i].transition_latency);
1da177e4
LT
492
493 cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
4b31e774
DB
494
495 /*
496 * the first call to ->target() should result in us actually
497 * writing something to the appropriate registers.
498 */
499 data->resume = 1;
500
1da177e4
LT
501 return (result);
502
503 err_freqfree:
504 kfree(data->freq_table);
505 err_unreg:
09b4d1ee 506 acpi_processor_unregister_performance(perf, cpu);
1da177e4
LT
507 err_free:
508 kfree(data);
509 acpi_io_data[cpu] = NULL;
510
511 return (result);
512}
513
514
515static int
516acpi_cpufreq_cpu_exit (
517 struct cpufreq_policy *policy)
518{
519 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
520
521
522 dprintk("acpi_cpufreq_cpu_exit\n");
523
524 if (data) {
525 cpufreq_frequency_table_put_attr(policy->cpu);
526 acpi_io_data[policy->cpu] = NULL;
09b4d1ee 527 acpi_processor_unregister_performance(data->acpi_data, policy->cpu);
1da177e4
LT
528 kfree(data);
529 }
530
531 return (0);
532}
533
534static int
535acpi_cpufreq_resume (
536 struct cpufreq_policy *policy)
537{
538 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
539
540
541 dprintk("acpi_cpufreq_resume\n");
542
543 data->resume = 1;
544
545 return (0);
546}
547
548
549static struct freq_attr* acpi_cpufreq_attr[] = {
550 &cpufreq_freq_attr_scaling_available_freqs,
551 NULL,
552};
553
554static struct cpufreq_driver acpi_cpufreq_driver = {
911cb74b
DJ
555 .verify = acpi_cpufreq_verify,
556 .target = acpi_cpufreq_target,
557 .init = acpi_cpufreq_cpu_init,
558 .exit = acpi_cpufreq_cpu_exit,
559 .resume = acpi_cpufreq_resume,
560 .name = "acpi-cpufreq",
561 .owner = THIS_MODULE,
562 .attr = acpi_cpufreq_attr,
563 .flags = CPUFREQ_STICKY,
1da177e4
LT
564};
565
566
567static int __init
568acpi_cpufreq_init (void)
569{
1da177e4
LT
570 dprintk("acpi_cpufreq_init\n");
571
a0cc621f 572 acpi_cpufreq_early_init_acpi();
09b4d1ee 573
a0cc621f 574 return cpufreq_register_driver(&acpi_cpufreq_driver);
1da177e4
LT
575}
576
577
578static void __exit
579acpi_cpufreq_exit (void)
580{
09b4d1ee 581 unsigned int i;
1da177e4
LT
582 dprintk("acpi_cpufreq_exit\n");
583
584 cpufreq_unregister_driver(&acpi_cpufreq_driver);
585
fb1bb34d 586 for_each_possible_cpu(i) {
09b4d1ee
VP
587 kfree(acpi_perf_data[i]);
588 acpi_perf_data[i] = NULL;
589 }
1da177e4
LT
590 return;
591}
592
d395bf12
VP
593module_param(acpi_pstate_strict, uint, 0644);
594MODULE_PARM_DESC(acpi_pstate_strict, "value 0 or non-zero. non-zero -> strict ACPI checks are performed during frequency changes.");
1da177e4
LT
595
596late_initcall(acpi_cpufreq_init);
597module_exit(acpi_cpufreq_exit);
598
599MODULE_ALIAS("acpi");