P-state software coordination for acpi-cpufreq
[linux-2.6/mini2440.git] / arch / i386 / kernel / cpu / cpufreq / acpi-cpufreq.c
blob4c7c6e089e87b7b7d8ff374be512385c863f3cac
1 /*
2 * acpi-cpufreq.c - ACPI Processor P-States Driver ($Revision: 1.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>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 #include <linux/config.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/cpufreq.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
34 #include <linux/compiler.h>
35 #include <linux/sched.h> /* current */
36 #include <asm/io.h>
37 #include <asm/delay.h>
38 #include <asm/uaccess.h>
40 #include <linux/acpi.h>
41 #include <acpi/processor.h>
43 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)
45 MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
46 MODULE_DESCRIPTION("ACPI Processor P-States Driver");
47 MODULE_LICENSE("GPL");
50 struct cpufreq_acpi_io {
51 struct acpi_processor_performance *acpi_data;
52 struct cpufreq_frequency_table *freq_table;
53 unsigned int resume;
56 static struct cpufreq_acpi_io *acpi_io_data[NR_CPUS];
57 static struct acpi_processor_performance *acpi_perf_data[NR_CPUS];
59 static struct cpufreq_driver acpi_cpufreq_driver;
61 static unsigned int acpi_pstate_strict;
63 static int
64 acpi_processor_write_port(
65 u16 port,
66 u8 bit_width,
67 u32 value)
69 if (bit_width <= 8) {
70 outb(value, port);
71 } else if (bit_width <= 16) {
72 outw(value, port);
73 } else if (bit_width <= 32) {
74 outl(value, port);
75 } else {
76 return -ENODEV;
78 return 0;
81 static int
82 acpi_processor_read_port(
83 u16 port,
84 u8 bit_width,
85 u32 *ret)
87 *ret = 0;
88 if (bit_width <= 8) {
89 *ret = inb(port);
90 } else if (bit_width <= 16) {
91 *ret = inw(port);
92 } else if (bit_width <= 32) {
93 *ret = inl(port);
94 } else {
95 return -ENODEV;
97 return 0;
100 static int
101 acpi_processor_set_performance (
102 struct cpufreq_acpi_io *data,
103 unsigned int cpu,
104 int state)
106 u16 port = 0;
107 u8 bit_width = 0;
108 int i = 0;
109 int ret = 0;
110 u32 value = 0;
111 int retval;
112 struct acpi_processor_performance *perf;
114 dprintk("acpi_processor_set_performance\n");
116 retval = 0;
117 perf = data->acpi_data;
118 if (state == perf->state) {
119 if (unlikely(data->resume)) {
120 dprintk("Called after resume, resetting to P%d\n", state);
121 data->resume = 0;
122 } else {
123 dprintk("Already at target state (P%d)\n", state);
124 return (retval);
128 dprintk("Transitioning from P%d to P%d\n", perf->state, state);
131 * First we write the target state's 'control' value to the
132 * control_register.
135 port = perf->control_register.address;
136 bit_width = perf->control_register.bit_width;
137 value = (u32) perf->states[state].control;
139 dprintk("Writing 0x%08x to port 0x%04x\n", value, port);
141 ret = acpi_processor_write_port(port, bit_width, value);
142 if (ret) {
143 dprintk("Invalid port width 0x%04x\n", bit_width);
144 return (ret);
148 * Assume the write went through when acpi_pstate_strict is not used.
149 * As read status_register is an expensive operation and there
150 * are no specific error cases where an IO port write will fail.
152 if (acpi_pstate_strict) {
153 /* Then we read the 'status_register' and compare the value
154 * with the target state's 'status' to make sure the
155 * transition was successful.
156 * Note that we'll poll for up to 1ms (100 cycles of 10us)
157 * before giving up.
160 port = perf->status_register.address;
161 bit_width = perf->status_register.bit_width;
163 dprintk("Looking for 0x%08x from port 0x%04x\n",
164 (u32) perf->states[state].status, port);
166 for (i = 0; i < 100; i++) {
167 ret = acpi_processor_read_port(port, bit_width, &value);
168 if (ret) {
169 dprintk("Invalid port width 0x%04x\n", bit_width);
170 return (ret);
172 if (value == (u32) perf->states[state].status)
173 break;
174 udelay(10);
176 } else {
177 i = 0;
178 value = (u32) perf->states[state].status;
181 if (unlikely(value != (u32) perf->states[state].status)) {
182 printk(KERN_WARNING "acpi-cpufreq: Transition failed\n");
183 retval = -ENODEV;
184 return (retval);
187 dprintk("Transition successful after %d microseconds\n", i * 10);
189 perf->state = state;
190 return (retval);
194 static int
195 acpi_cpufreq_target (
196 struct cpufreq_policy *policy,
197 unsigned int target_freq,
198 unsigned int relation)
200 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
201 struct acpi_processor_performance *perf;
202 struct cpufreq_freqs freqs;
203 cpumask_t online_policy_cpus;
204 cpumask_t saved_mask;
205 cpumask_t set_mask;
206 cpumask_t covered_cpus;
207 unsigned int cur_state = 0;
208 unsigned int next_state = 0;
209 unsigned int result = 0;
210 unsigned int j;
211 unsigned int tmp;
213 dprintk("acpi_cpufreq_setpolicy\n");
215 result = cpufreq_frequency_table_target(policy,
216 data->freq_table,
217 target_freq,
218 relation,
219 &next_state);
220 if (unlikely(result))
221 return (result);
223 perf = data->acpi_data;
224 cur_state = perf->state;
225 freqs.old = data->freq_table[cur_state].frequency;
226 freqs.new = data->freq_table[next_state].frequency;
228 /* cpufreq holds the hotplug lock, so we are safe from here on */
229 cpus_and(online_policy_cpus, cpu_online_map, policy->cpus);
231 for_each_cpu_mask(j, online_policy_cpus) {
232 freqs.cpu = j;
233 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
237 * We need to call driver->target() on all or any CPU in
238 * policy->cpus, depending on policy->shared_type.
240 saved_mask = current->cpus_allowed;
241 cpus_clear(covered_cpus);
242 for_each_cpu_mask(j, online_policy_cpus) {
244 * Support for SMP systems.
245 * Make sure we are running on CPU that wants to change freq
247 cpus_clear(set_mask);
248 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
249 cpus_or(set_mask, set_mask, online_policy_cpus);
250 else
251 cpu_set(j, set_mask);
253 set_cpus_allowed(current, set_mask);
254 if (unlikely(!cpu_isset(smp_processor_id(), set_mask))) {
255 dprintk("couldn't limit to CPUs in this domain\n");
256 result = -EAGAIN;
257 break;
260 result = acpi_processor_set_performance (data, j, next_state);
261 if (result) {
262 result = -EAGAIN;
263 break;
266 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
267 break;
269 cpu_set(j, covered_cpus);
272 for_each_cpu_mask(j, online_policy_cpus) {
273 freqs.cpu = j;
274 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
277 if (unlikely(result)) {
279 * We have failed halfway through the frequency change.
280 * We have sent callbacks to online_policy_cpus and
281 * acpi_processor_set_performance() has been called on
282 * coverd_cpus. Best effort undo..
285 if (!cpus_empty(covered_cpus)) {
286 for_each_cpu_mask(j, covered_cpus) {
287 policy->cpu = j;
288 acpi_processor_set_performance (data,
290 cur_state);
294 tmp = freqs.new;
295 freqs.new = freqs.old;
296 freqs.old = tmp;
297 for_each_cpu_mask(j, online_policy_cpus) {
298 freqs.cpu = j;
299 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
300 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
304 set_cpus_allowed(current, saved_mask);
305 return (result);
309 static int
310 acpi_cpufreq_verify (
311 struct cpufreq_policy *policy)
313 unsigned int result = 0;
314 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
316 dprintk("acpi_cpufreq_verify\n");
318 result = cpufreq_frequency_table_verify(policy,
319 data->freq_table);
321 return (result);
325 static unsigned long
326 acpi_cpufreq_guess_freq (
327 struct cpufreq_acpi_io *data,
328 unsigned int cpu)
330 struct acpi_processor_performance *perf = data->acpi_data;
332 if (cpu_khz) {
333 /* search the closest match to cpu_khz */
334 unsigned int i;
335 unsigned long freq;
336 unsigned long freqn = perf->states[0].core_frequency * 1000;
338 for (i = 0; i < (perf->state_count - 1); i++) {
339 freq = freqn;
340 freqn = perf->states[i+1].core_frequency * 1000;
341 if ((2 * cpu_khz) > (freqn + freq)) {
342 perf->state = i;
343 return (freq);
346 perf->state = perf->state_count - 1;
347 return (freqn);
348 } else {
349 /* assume CPU is at P0... */
350 perf->state = 0;
351 return perf->states[0].core_frequency * 1000;
357 * acpi_cpufreq_early_init - initialize ACPI P-States library
359 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
360 * in order to determine correct frequency and voltage pairings. We can
361 * do _PDC and _PSD and find out the processor dependency for the
362 * actual init that will happen later...
364 static int acpi_cpufreq_early_init_acpi(void)
366 struct acpi_processor_performance *data;
367 unsigned int i, j;
369 dprintk("acpi_cpufreq_early_init\n");
371 for_each_cpu(i) {
372 data = kzalloc(sizeof(struct acpi_processor_performance),
373 GFP_KERNEL);
374 if (!data) {
375 for_each_cpu(j) {
376 kfree(acpi_perf_data[j]);
377 acpi_perf_data[j] = NULL;
379 return (-ENOMEM);
381 acpi_perf_data[i] = data;
384 /* Do initialization in ACPI core */
385 acpi_processor_preregister_performance(acpi_perf_data);
386 return 0;
389 static int
390 acpi_cpufreq_cpu_init (
391 struct cpufreq_policy *policy)
393 unsigned int i;
394 unsigned int cpu = policy->cpu;
395 struct cpufreq_acpi_io *data;
396 unsigned int result = 0;
397 struct cpuinfo_x86 *c = &cpu_data[policy->cpu];
398 struct acpi_processor_performance *perf;
400 dprintk("acpi_cpufreq_cpu_init\n");
402 if (!acpi_perf_data[cpu])
403 return (-ENODEV);
405 data = kzalloc(sizeof(struct cpufreq_acpi_io), GFP_KERNEL);
406 if (!data)
407 return (-ENOMEM);
409 data->acpi_data = acpi_perf_data[cpu];
410 acpi_io_data[cpu] = data;
412 result = acpi_processor_register_performance(data->acpi_data, cpu);
414 if (result)
415 goto err_free;
417 perf = data->acpi_data;
418 policy->cpus = perf->shared_cpu_map;
419 policy->shared_type = perf->shared_type;
421 if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
422 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
425 /* capability check */
426 if (perf->state_count <= 1) {
427 dprintk("No P-States\n");
428 result = -ENODEV;
429 goto err_unreg;
432 if ((perf->control_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO) ||
433 (perf->status_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
434 dprintk("Unsupported address space [%d, %d]\n",
435 (u32) (perf->control_register.space_id),
436 (u32) (perf->status_register.space_id));
437 result = -ENODEV;
438 goto err_unreg;
441 /* alloc freq_table */
442 data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) * (perf->state_count + 1), GFP_KERNEL);
443 if (!data->freq_table) {
444 result = -ENOMEM;
445 goto err_unreg;
448 /* detect transition latency */
449 policy->cpuinfo.transition_latency = 0;
450 for (i=0; i<perf->state_count; i++) {
451 if ((perf->states[i].transition_latency * 1000) > policy->cpuinfo.transition_latency)
452 policy->cpuinfo.transition_latency = perf->states[i].transition_latency * 1000;
454 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
456 /* The current speed is unknown and not detectable by ACPI... */
457 policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
459 /* table init */
460 for (i=0; i<=perf->state_count; i++)
462 data->freq_table[i].index = i;
463 if (i<perf->state_count)
464 data->freq_table[i].frequency = perf->states[i].core_frequency * 1000;
465 else
466 data->freq_table[i].frequency = CPUFREQ_TABLE_END;
469 result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
470 if (result) {
471 goto err_freqfree;
474 /* notify BIOS that we exist */
475 acpi_processor_notify_smm(THIS_MODULE);
477 printk(KERN_INFO "acpi-cpufreq: CPU%u - ACPI performance management activated.\n",
478 cpu);
479 for (i = 0; i < perf->state_count; i++)
480 dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
481 (i == perf->state?'*':' '), i,
482 (u32) perf->states[i].core_frequency,
483 (u32) perf->states[i].power,
484 (u32) perf->states[i].transition_latency);
486 cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
489 * the first call to ->target() should result in us actually
490 * writing something to the appropriate registers.
492 data->resume = 1;
494 return (result);
496 err_freqfree:
497 kfree(data->freq_table);
498 err_unreg:
499 acpi_processor_unregister_performance(perf, cpu);
500 err_free:
501 kfree(data);
502 acpi_io_data[cpu] = NULL;
504 return (result);
508 static int
509 acpi_cpufreq_cpu_exit (
510 struct cpufreq_policy *policy)
512 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
515 dprintk("acpi_cpufreq_cpu_exit\n");
517 if (data) {
518 cpufreq_frequency_table_put_attr(policy->cpu);
519 acpi_io_data[policy->cpu] = NULL;
520 acpi_processor_unregister_performance(data->acpi_data, policy->cpu);
521 kfree(data);
524 return (0);
527 static int
528 acpi_cpufreq_resume (
529 struct cpufreq_policy *policy)
531 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
534 dprintk("acpi_cpufreq_resume\n");
536 data->resume = 1;
538 return (0);
542 static struct freq_attr* acpi_cpufreq_attr[] = {
543 &cpufreq_freq_attr_scaling_available_freqs,
544 NULL,
547 static struct cpufreq_driver acpi_cpufreq_driver = {
548 .verify = acpi_cpufreq_verify,
549 .target = acpi_cpufreq_target,
550 .init = acpi_cpufreq_cpu_init,
551 .exit = acpi_cpufreq_cpu_exit,
552 .resume = acpi_cpufreq_resume,
553 .name = "acpi-cpufreq",
554 .owner = THIS_MODULE,
555 .attr = acpi_cpufreq_attr,
559 static int __init
560 acpi_cpufreq_init (void)
562 int result = 0;
564 dprintk("acpi_cpufreq_init\n");
566 result = acpi_cpufreq_early_init_acpi();
568 if (!result)
569 result = cpufreq_register_driver(&acpi_cpufreq_driver);
571 return (result);
575 static void __exit
576 acpi_cpufreq_exit (void)
578 unsigned int i;
579 dprintk("acpi_cpufreq_exit\n");
581 cpufreq_unregister_driver(&acpi_cpufreq_driver);
583 for_each_cpu(i) {
584 kfree(acpi_perf_data[i]);
585 acpi_perf_data[i] = NULL;
587 return;
590 module_param(acpi_pstate_strict, uint, 0644);
591 MODULE_PARM_DESC(acpi_pstate_strict, "value 0 or non-zero. non-zero -> strict ACPI checks are performed during frequency changes.");
593 late_initcall(acpi_cpufreq_init);
594 module_exit(acpi_cpufreq_exit);
596 MODULE_ALIAS("acpi");