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/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>
33 #include <linux/compiler.h>
34 #include <linux/sched.h> /* current */
36 #include <asm/delay.h>
37 #include <asm/uaccess.h>
39 #include <linux/acpi.h>
40 #include <acpi/processor.h>
42 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)
44 MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
45 MODULE_DESCRIPTION("ACPI Processor P-States Driver");
46 MODULE_LICENSE("GPL");
49 struct cpufreq_acpi_io
{
50 struct acpi_processor_performance
*acpi_data
;
51 struct cpufreq_frequency_table
*freq_table
;
55 static struct cpufreq_acpi_io
*acpi_io_data
[NR_CPUS
];
56 static struct acpi_processor_performance
*acpi_perf_data
[NR_CPUS
];
58 static struct cpufreq_driver acpi_cpufreq_driver
;
60 static unsigned int acpi_pstate_strict
;
63 acpi_processor_write_port(
70 } else if (bit_width
<= 16) {
72 } else if (bit_width
<= 32) {
81 acpi_processor_read_port(
89 } else if (bit_width
<= 16) {
91 } else if (bit_width
<= 32) {
100 acpi_processor_set_performance (
101 struct cpufreq_acpi_io
*data
,
111 struct acpi_processor_performance
*perf
;
113 dprintk("acpi_processor_set_performance\n");
116 perf
= data
->acpi_data
;
117 if (state
== perf
->state
) {
118 if (unlikely(data
->resume
)) {
119 dprintk("Called after resume, resetting to P%d\n", state
);
122 dprintk("Already at target state (P%d)\n", state
);
127 dprintk("Transitioning from P%d to P%d\n", perf
->state
, state
);
130 * First we write the target state's 'control' value to the
134 port
= perf
->control_register
.address
;
135 bit_width
= perf
->control_register
.bit_width
;
136 value
= (u32
) perf
->states
[state
].control
;
138 dprintk("Writing 0x%08x to port 0x%04x\n", value
, port
);
140 ret
= acpi_processor_write_port(port
, bit_width
, value
);
142 dprintk("Invalid port width 0x%04x\n", bit_width
);
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.
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)
159 port
= perf
->status_register
.address
;
160 bit_width
= perf
->status_register
.bit_width
;
162 dprintk("Looking for 0x%08x from port 0x%04x\n",
163 (u32
) perf
->states
[state
].status
, port
);
165 for (i
= 0; i
< 100; i
++) {
166 ret
= acpi_processor_read_port(port
, bit_width
, &value
);
168 dprintk("Invalid port width 0x%04x\n", bit_width
);
171 if (value
== (u32
) perf
->states
[state
].status
)
176 value
= (u32
) perf
->states
[state
].status
;
179 if (unlikely(value
!= (u32
) perf
->states
[state
].status
)) {
180 printk(KERN_WARNING
"acpi-cpufreq: Transition failed\n");
185 dprintk("Transition successful after %d microseconds\n", i
* 10);
193 acpi_cpufreq_target (
194 struct cpufreq_policy
*policy
,
195 unsigned int target_freq
,
196 unsigned int relation
)
198 struct cpufreq_acpi_io
*data
= acpi_io_data
[policy
->cpu
];
199 struct acpi_processor_performance
*perf
;
200 struct cpufreq_freqs freqs
;
201 cpumask_t online_policy_cpus
;
202 cpumask_t saved_mask
;
204 cpumask_t covered_cpus
;
205 unsigned int cur_state
= 0;
206 unsigned int next_state
= 0;
207 unsigned int result
= 0;
211 dprintk("acpi_cpufreq_setpolicy\n");
213 result
= cpufreq_frequency_table_target(policy
,
218 if (unlikely(result
))
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
;
226 #ifdef CONFIG_HOTPLUG_CPU
227 /* cpufreq holds the hotplug lock, so we are safe from here on */
228 cpus_and(online_policy_cpus
, cpu_online_map
, policy
->cpus
);
230 online_policy_cpus
= policy
->cpus
;
233 for_each_cpu_mask(j
, online_policy_cpus
) {
235 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
239 * We need to call driver->target() on all or any CPU in
240 * policy->cpus, depending on policy->shared_type.
242 saved_mask
= current
->cpus_allowed
;
243 cpus_clear(covered_cpus
);
244 for_each_cpu_mask(j
, online_policy_cpus
) {
246 * Support for SMP systems.
247 * Make sure we are running on CPU that wants to change freq
249 cpus_clear(set_mask
);
250 if (policy
->shared_type
== CPUFREQ_SHARED_TYPE_ANY
)
251 cpus_or(set_mask
, set_mask
, online_policy_cpus
);
253 cpu_set(j
, set_mask
);
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");
262 result
= acpi_processor_set_performance (data
, j
, next_state
);
268 if (policy
->shared_type
== CPUFREQ_SHARED_TYPE_ANY
)
271 cpu_set(j
, covered_cpus
);
274 for_each_cpu_mask(j
, online_policy_cpus
) {
276 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
279 if (unlikely(result
)) {
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..
287 if (!cpus_empty(covered_cpus
)) {
288 for_each_cpu_mask(j
, covered_cpus
) {
290 acpi_processor_set_performance (data
,
297 freqs
.new = freqs
.old
;
299 for_each_cpu_mask(j
, online_policy_cpus
) {
301 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
302 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
306 set_cpus_allowed(current
, saved_mask
);
312 acpi_cpufreq_verify (
313 struct cpufreq_policy
*policy
)
315 unsigned int result
= 0;
316 struct cpufreq_acpi_io
*data
= acpi_io_data
[policy
->cpu
];
318 dprintk("acpi_cpufreq_verify\n");
320 result
= cpufreq_frequency_table_verify(policy
,
328 acpi_cpufreq_guess_freq (
329 struct cpufreq_acpi_io
*data
,
332 struct acpi_processor_performance
*perf
= data
->acpi_data
;
335 /* search the closest match to cpu_khz */
338 unsigned long freqn
= perf
->states
[0].core_frequency
* 1000;
340 for (i
= 0; i
< (perf
->state_count
- 1); i
++) {
342 freqn
= perf
->states
[i
+1].core_frequency
* 1000;
343 if ((2 * cpu_khz
) > (freqn
+ freq
)) {
348 perf
->state
= perf
->state_count
- 1;
351 /* assume CPU is at P0... */
353 return perf
->states
[0].core_frequency
* 1000;
359 * acpi_cpufreq_early_init - initialize ACPI P-States library
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...
366 static int acpi_cpufreq_early_init_acpi(void)
368 struct acpi_processor_performance
*data
;
371 dprintk("acpi_cpufreq_early_init\n");
373 for_each_possible_cpu(i
) {
374 data
= kzalloc(sizeof(struct acpi_processor_performance
),
377 for_each_possible_cpu(j
) {
378 kfree(acpi_perf_data
[j
]);
379 acpi_perf_data
[j
] = NULL
;
383 acpi_perf_data
[i
] = data
;
386 /* Do initialization in ACPI core */
387 acpi_processor_preregister_performance(acpi_perf_data
);
392 acpi_cpufreq_cpu_init (
393 struct cpufreq_policy
*policy
)
396 unsigned int cpu
= policy
->cpu
;
397 struct cpufreq_acpi_io
*data
;
398 unsigned int result
= 0;
399 struct cpuinfo_x86
*c
= &cpu_data
[policy
->cpu
];
400 struct acpi_processor_performance
*perf
;
402 dprintk("acpi_cpufreq_cpu_init\n");
404 if (!acpi_perf_data
[cpu
])
407 data
= kzalloc(sizeof(struct cpufreq_acpi_io
), GFP_KERNEL
);
411 data
->acpi_data
= acpi_perf_data
[cpu
];
412 acpi_io_data
[cpu
] = data
;
414 result
= acpi_processor_register_performance(data
->acpi_data
, cpu
);
419 perf
= data
->acpi_data
;
420 policy
->shared_type
= perf
->shared_type
;
422 * Will let policy->cpus know about dependency only when software
423 * coordination is required.
425 if (policy
->shared_type
== CPUFREQ_SHARED_TYPE_ALL
||
426 policy
->shared_type
== CPUFREQ_SHARED_TYPE_ANY
)
427 policy
->cpus
= perf
->shared_cpu_map
;
429 if (cpu_has(c
, X86_FEATURE_CONSTANT_TSC
)) {
430 acpi_cpufreq_driver
.flags
|= CPUFREQ_CONST_LOOPS
;
433 /* capability check */
434 if (perf
->state_count
<= 1) {
435 dprintk("No P-States\n");
440 if ((perf
->control_register
.space_id
!= ACPI_ADR_SPACE_SYSTEM_IO
) ||
441 (perf
->status_register
.space_id
!= ACPI_ADR_SPACE_SYSTEM_IO
)) {
442 dprintk("Unsupported address space [%d, %d]\n",
443 (u32
) (perf
->control_register
.space_id
),
444 (u32
) (perf
->status_register
.space_id
));
449 /* alloc freq_table */
450 data
->freq_table
= kmalloc(sizeof(struct cpufreq_frequency_table
) * (perf
->state_count
+ 1), GFP_KERNEL
);
451 if (!data
->freq_table
) {
456 /* detect transition latency */
457 policy
->cpuinfo
.transition_latency
= 0;
458 for (i
=0; i
<perf
->state_count
; i
++) {
459 if ((perf
->states
[i
].transition_latency
* 1000) > policy
->cpuinfo
.transition_latency
)
460 policy
->cpuinfo
.transition_latency
= perf
->states
[i
].transition_latency
* 1000;
462 policy
->governor
= CPUFREQ_DEFAULT_GOVERNOR
;
464 /* The current speed is unknown and not detectable by ACPI... */
465 policy
->cur
= acpi_cpufreq_guess_freq(data
, policy
->cpu
);
468 for (i
=0; i
<=perf
->state_count
; i
++)
470 data
->freq_table
[i
].index
= i
;
471 if (i
<perf
->state_count
)
472 data
->freq_table
[i
].frequency
= perf
->states
[i
].core_frequency
* 1000;
474 data
->freq_table
[i
].frequency
= CPUFREQ_TABLE_END
;
477 result
= cpufreq_frequency_table_cpuinfo(policy
, data
->freq_table
);
482 /* notify BIOS that we exist */
483 acpi_processor_notify_smm(THIS_MODULE
);
485 printk(KERN_INFO
"acpi-cpufreq: CPU%u - ACPI performance management activated.\n",
487 for (i
= 0; i
< perf
->state_count
; i
++)
488 dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
489 (i
== perf
->state
?'*':' '), i
,
490 (u32
) perf
->states
[i
].core_frequency
,
491 (u32
) perf
->states
[i
].power
,
492 (u32
) perf
->states
[i
].transition_latency
);
494 cpufreq_frequency_table_get_attr(data
->freq_table
, policy
->cpu
);
497 * the first call to ->target() should result in us actually
498 * writing something to the appropriate registers.
505 kfree(data
->freq_table
);
507 acpi_processor_unregister_performance(perf
, cpu
);
510 acpi_io_data
[cpu
] = NULL
;
517 acpi_cpufreq_cpu_exit (
518 struct cpufreq_policy
*policy
)
520 struct cpufreq_acpi_io
*data
= acpi_io_data
[policy
->cpu
];
523 dprintk("acpi_cpufreq_cpu_exit\n");
526 cpufreq_frequency_table_put_attr(policy
->cpu
);
527 acpi_io_data
[policy
->cpu
] = NULL
;
528 acpi_processor_unregister_performance(data
->acpi_data
, policy
->cpu
);
536 acpi_cpufreq_resume (
537 struct cpufreq_policy
*policy
)
539 struct cpufreq_acpi_io
*data
= acpi_io_data
[policy
->cpu
];
542 dprintk("acpi_cpufreq_resume\n");
550 static struct freq_attr
* acpi_cpufreq_attr
[] = {
551 &cpufreq_freq_attr_scaling_available_freqs
,
555 static struct cpufreq_driver acpi_cpufreq_driver
= {
556 .verify
= acpi_cpufreq_verify
,
557 .target
= acpi_cpufreq_target
,
558 .init
= acpi_cpufreq_cpu_init
,
559 .exit
= acpi_cpufreq_cpu_exit
,
560 .resume
= acpi_cpufreq_resume
,
561 .name
= "acpi-cpufreq",
562 .owner
= THIS_MODULE
,
563 .attr
= acpi_cpufreq_attr
,
564 .flags
= CPUFREQ_STICKY
,
569 acpi_cpufreq_init (void)
573 dprintk("acpi_cpufreq_init\n");
575 result
= acpi_cpufreq_early_init_acpi();
578 result
= cpufreq_register_driver(&acpi_cpufreq_driver
);
585 acpi_cpufreq_exit (void)
588 dprintk("acpi_cpufreq_exit\n");
590 cpufreq_unregister_driver(&acpi_cpufreq_driver
);
592 for_each_possible_cpu(i
) {
593 kfree(acpi_perf_data
[i
]);
594 acpi_perf_data
[i
] = NULL
;
599 module_param(acpi_pstate_strict
, uint
, 0644);
600 MODULE_PARM_DESC(acpi_pstate_strict
, "value 0 or non-zero. non-zero -> strict ACPI checks are performed during frequency changes.");
602 late_initcall(acpi_cpufreq_init
);
603 module_exit(acpi_cpufreq_exit
);
605 MODULE_ALIAS("acpi");