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 */
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;
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;
64 acpi_processor_write_port(
71 } else if (bit_width <= 16) {
73 } else if (bit_width <= 32) {
82 acpi_processor_read_port(
90 } else if (bit_width <= 16) {
92 } else if (bit_width <= 32) {
101 acpi_processor_set_performance (
102 struct cpufreq_acpi_io *data,
112 struct acpi_processor_performance *perf;
114 dprintk("acpi_processor_set_performance\n");
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);
123 dprintk("Already at target state (P%d)\n", state);
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
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);
143 dprintk("Invalid port width 0x%04x\n", bit_width);
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)
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);
169 dprintk("Invalid port width 0x%04x\n", bit_width);
172 if (value == (u32) perf->states[state].status)
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");
187 dprintk("Transition successful after %d microseconds\n", i * 10);
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;
206 cpumask_t covered_cpus;
207 unsigned int cur_state = 0;
208 unsigned int next_state = 0;
209 unsigned int result = 0;
213 dprintk("acpi_cpufreq_setpolicy\n");
215 result = cpufreq_frequency_table_target(policy,
220 if (unlikely(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 #ifdef CONFIG_HOTPLUG_CPU
229 /* cpufreq holds the hotplug lock, so we are safe from here on */
230 cpus_and(online_policy_cpus, cpu_online_map, policy->cpus);
232 online_policy_cpus = policy->cpus;
235 for_each_cpu_mask(j, online_policy_cpus) {
237 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
241 * We need to call driver->target() on all or any CPU in
242 * policy->cpus, depending on policy->shared_type.
244 saved_mask = current->cpus_allowed;
245 cpus_clear(covered_cpus);
246 for_each_cpu_mask(j, online_policy_cpus) {
248 * Support for SMP systems.
249 * Make sure we are running on CPU that wants to change freq
251 cpus_clear(set_mask);
252 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
253 cpus_or(set_mask, set_mask, online_policy_cpus);
255 cpu_set(j, set_mask);
257 set_cpus_allowed(current, set_mask);
258 if (unlikely(!cpu_isset(smp_processor_id(), set_mask))) {
259 dprintk("couldn't limit to CPUs in this domain\n");
264 result = acpi_processor_set_performance (data, j, next_state);
270 if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
273 cpu_set(j, covered_cpus);
276 for_each_cpu_mask(j, online_policy_cpus) {
278 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
281 if (unlikely(result)) {
283 * We have failed halfway through the frequency change.
284 * We have sent callbacks to online_policy_cpus and
285 * acpi_processor_set_performance() has been called on
286 * coverd_cpus. Best effort undo..
289 if (!cpus_empty(covered_cpus)) {
290 for_each_cpu_mask(j, covered_cpus) {
292 acpi_processor_set_performance (data,
299 freqs.new = freqs.old;
301 for_each_cpu_mask(j, online_policy_cpus) {
303 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
304 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
308 set_cpus_allowed(current, saved_mask);
314 acpi_cpufreq_verify (
315 struct cpufreq_policy *policy)
317 unsigned int result = 0;
318 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
320 dprintk("acpi_cpufreq_verify\n");
322 result = cpufreq_frequency_table_verify(policy,
330 acpi_cpufreq_guess_freq (
331 struct cpufreq_acpi_io *data,
334 struct acpi_processor_performance *perf = data->acpi_data;
337 /* search the closest match to cpu_khz */
340 unsigned long freqn = perf->states[0].core_frequency * 1000;
342 for (i = 0; i < (perf->state_count - 1); i++) {
344 freqn = perf->states[i+1].core_frequency * 1000;
345 if ((2 * cpu_khz) > (freqn + freq)) {
350 perf->state = perf->state_count - 1;
353 /* assume CPU is at P0... */
355 return perf->states[0].core_frequency * 1000;
361 * acpi_cpufreq_early_init - initialize ACPI P-States library
363 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
364 * in order to determine correct frequency and voltage pairings. We can
365 * do _PDC and _PSD and find out the processor dependency for the
366 * actual init that will happen later...
368 static int acpi_cpufreq_early_init_acpi(void)
370 struct acpi_processor_performance *data;
373 dprintk("acpi_cpufreq_early_init\n");
376 data = kzalloc(sizeof(struct acpi_processor_performance),
380 kfree(acpi_perf_data[j]);
381 acpi_perf_data[j] = NULL;
385 acpi_perf_data[i] = data;
388 /* Do initialization in ACPI core */
389 acpi_processor_preregister_performance(acpi_perf_data);
394 acpi_cpufreq_cpu_init (
395 struct cpufreq_policy *policy)
398 unsigned int cpu = policy->cpu;
399 struct cpufreq_acpi_io *data;
400 unsigned int result = 0;
401 struct cpuinfo_x86 *c = &cpu_data[policy->cpu];
402 struct acpi_processor_performance *perf;
404 dprintk("acpi_cpufreq_cpu_init\n");
406 if (!acpi_perf_data[cpu])
409 data = kzalloc(sizeof(struct cpufreq_acpi_io), GFP_KERNEL);
413 data->acpi_data = acpi_perf_data[cpu];
414 acpi_io_data[cpu] = data;
416 result = acpi_processor_register_performance(data->acpi_data, cpu);
421 perf = data->acpi_data;
422 policy->cpus = perf->shared_cpu_map;
423 policy->shared_type = perf->shared_type;
425 if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
426 acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
429 /* capability check */
430 if (perf->state_count <= 1) {
431 dprintk("No P-States\n");
436 if ((perf->control_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO) ||
437 (perf->status_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
438 dprintk("Unsupported address space [%d, %d]\n",
439 (u32) (perf->control_register.space_id),
440 (u32) (perf->status_register.space_id));
445 /* alloc freq_table */
446 data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) * (perf->state_count + 1), GFP_KERNEL);
447 if (!data->freq_table) {
452 /* detect transition latency */
453 policy->cpuinfo.transition_latency = 0;
454 for (i=0; i<perf->state_count; i++) {
455 if ((perf->states[i].transition_latency * 1000) > policy->cpuinfo.transition_latency)
456 policy->cpuinfo.transition_latency = perf->states[i].transition_latency * 1000;
458 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
460 /* The current speed is unknown and not detectable by ACPI... */
461 policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
464 for (i=0; i<=perf->state_count; i++)
466 data->freq_table[i].index = i;
467 if (i<perf->state_count)
468 data->freq_table[i].frequency = perf->states[i].core_frequency * 1000;
470 data->freq_table[i].frequency = CPUFREQ_TABLE_END;
473 result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
478 /* notify BIOS that we exist */
479 acpi_processor_notify_smm(THIS_MODULE);
481 printk(KERN_INFO "acpi-cpufreq: CPU%u - ACPI performance management activated.\n",
483 for (i = 0; i < perf->state_count; i++)
484 dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
485 (i == perf->state?'*':' '), i,
486 (u32) perf->states[i].core_frequency,
487 (u32) perf->states[i].power,
488 (u32) perf->states[i].transition_latency);
490 cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
493 * the first call to ->target() should result in us actually
494 * writing something to the appropriate registers.
501 kfree(data->freq_table);
503 acpi_processor_unregister_performance(perf, cpu);
506 acpi_io_data[cpu] = NULL;
513 acpi_cpufreq_cpu_exit (
514 struct cpufreq_policy *policy)
516 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
519 dprintk("acpi_cpufreq_cpu_exit\n");
522 cpufreq_frequency_table_put_attr(policy->cpu);
523 acpi_io_data[policy->cpu] = NULL;
524 acpi_processor_unregister_performance(data->acpi_data, policy->cpu);
532 acpi_cpufreq_resume (
533 struct cpufreq_policy *policy)
535 struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
538 dprintk("acpi_cpufreq_resume\n");
546 static struct freq_attr* acpi_cpufreq_attr[] = {
547 &cpufreq_freq_attr_scaling_available_freqs,
551 static struct cpufreq_driver acpi_cpufreq_driver = {
552 .verify = acpi_cpufreq_verify,
553 .target = acpi_cpufreq_target,
554 .init = acpi_cpufreq_cpu_init,
555 .exit = acpi_cpufreq_cpu_exit,
556 .resume = acpi_cpufreq_resume,
557 .name = "acpi-cpufreq",
558 .owner = THIS_MODULE,
559 .attr = acpi_cpufreq_attr,
564 acpi_cpufreq_init (void)
568 dprintk("acpi_cpufreq_init\n");
570 result = acpi_cpufreq_early_init_acpi();
573 result = cpufreq_register_driver(&acpi_cpufreq_driver);
580 acpi_cpufreq_exit (void)
583 dprintk("acpi_cpufreq_exit\n");
585 cpufreq_unregister_driver(&acpi_cpufreq_driver);
588 kfree(acpi_perf_data[i]);
589 acpi_perf_data[i] = NULL;
594 module_param(acpi_pstate_strict, uint, 0644);
595 MODULE_PARM_DESC(acpi_pstate_strict, "value 0 or non-zero. non-zero -> strict ACPI checks are performed during frequency changes.");
597 late_initcall(acpi_cpufreq_init);
598 module_exit(acpi_cpufreq_exit);
600 MODULE_ALIAS("acpi");