2 * acpi-cpufreq.c - ACPI Processor P-States Driver ($Revision: 1.4 $)
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 * Copyright (C) 2006 Denis Sadykov <denis.m.sadykov@intel.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/smp.h>
32 #include <linux/sched.h>
33 #include <linux/cpufreq.h>
34 #include <linux/compiler.h>
35 #include <linux/sched.h> /* current */
36 #include <linux/dmi.h>
38 #include <linux/acpi.h>
39 #include <acpi/processor.h>
43 #include <asm/processor.h>
44 #include <asm/cpufeature.h>
45 #include <asm/delay.h>
46 #include <asm/uaccess.h>
48 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)
50 MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
51 MODULE_DESCRIPTION("ACPI Processor P-States Driver");
52 MODULE_LICENSE("GPL");
55 UNDEFINED_CAPABLE
= 0,
56 SYSTEM_INTEL_MSR_CAPABLE
,
60 #define INTEL_MSR_RANGE (0xffff)
62 struct acpi_cpufreq_data
{
63 struct acpi_processor_performance
*acpi_data
;
64 struct cpufreq_frequency_table
*freq_table
;
66 unsigned int cpu_feature
;
69 static struct acpi_cpufreq_data
*drv_data
[NR_CPUS
];
70 static struct acpi_processor_performance
*acpi_perf_data
[NR_CPUS
];
72 static struct cpufreq_driver acpi_cpufreq_driver
;
74 static unsigned int acpi_pstate_strict
;
76 static int check_est_cpu(unsigned int cpuid
)
78 struct cpuinfo_x86
*cpu
= &cpu_data
[cpuid
];
80 if (cpu
->x86_vendor
!= X86_VENDOR_INTEL
||
81 !cpu_has(cpu
, X86_FEATURE_EST
))
87 static unsigned extract_io(u32 value
, struct acpi_cpufreq_data
*data
)
89 struct acpi_processor_performance
*perf
;
92 perf
= data
->acpi_data
;
94 for (i
= 0; i
< perf
->state_count
; i
++) {
95 if (value
== perf
->states
[i
].status
)
96 return data
->freq_table
[i
].frequency
;
101 static unsigned extract_msr(u32 msr
, struct acpi_cpufreq_data
*data
)
105 msr
&= INTEL_MSR_RANGE
;
106 for (i
= 0; data
->freq_table
[i
].frequency
!= CPUFREQ_TABLE_END
; i
++) {
107 if (msr
== data
->freq_table
[i
].index
)
108 return data
->freq_table
[i
].frequency
;
110 return data
->freq_table
[0].frequency
;
113 static unsigned extract_freq(u32 val
, struct acpi_cpufreq_data
*data
)
115 switch (data
->cpu_feature
) {
116 case SYSTEM_INTEL_MSR_CAPABLE
:
117 return extract_msr(val
, data
);
118 case SYSTEM_IO_CAPABLE
:
119 return extract_io(val
, data
);
125 static void wrport(u16 port
, u8 bit_width
, u32 value
)
127 if (bit_width
<= 8) {
129 } else if (bit_width
<= 16) {
131 } else if (bit_width
<= 32) {
136 static void rdport(u16 port
, u8 bit_width
, u32
* ret
)
139 if (bit_width
<= 8) {
141 } else if (bit_width
<= 16) {
143 } else if (bit_width
<= 32) {
169 static void do_drv_read(struct drv_cmd
*cmd
)
174 case SYSTEM_INTEL_MSR_CAPABLE
:
175 rdmsr(cmd
->addr
.msr
.reg
, cmd
->val
, h
);
177 case SYSTEM_IO_CAPABLE
:
178 rdport(cmd
->addr
.io
.port
, cmd
->addr
.io
.bit_width
, &cmd
->val
);
185 static void do_drv_write(struct drv_cmd
*cmd
)
190 case SYSTEM_INTEL_MSR_CAPABLE
:
191 wrmsr(cmd
->addr
.msr
.reg
, cmd
->val
, h
);
193 case SYSTEM_IO_CAPABLE
:
194 wrport(cmd
->addr
.io
.port
, cmd
->addr
.io
.bit_width
, cmd
->val
);
201 static inline void drv_read(struct drv_cmd
*cmd
)
203 cpumask_t saved_mask
= current
->cpus_allowed
;
206 set_cpus_allowed(current
, cmd
->mask
);
208 set_cpus_allowed(current
, saved_mask
);
212 static void drv_write(struct drv_cmd
*cmd
)
214 cpumask_t saved_mask
= current
->cpus_allowed
;
217 for_each_cpu_mask(i
, cmd
->mask
) {
218 set_cpus_allowed(current
, cpumask_of_cpu(i
));
222 set_cpus_allowed(current
, saved_mask
);
226 static u32
get_cur_val(cpumask_t mask
)
228 struct acpi_processor_performance
*perf
;
231 if (unlikely(cpus_empty(mask
)))
234 switch (drv_data
[first_cpu(mask
)]->cpu_feature
) {
235 case SYSTEM_INTEL_MSR_CAPABLE
:
236 cmd
.type
= SYSTEM_INTEL_MSR_CAPABLE
;
237 cmd
.addr
.msr
.reg
= MSR_IA32_PERF_STATUS
;
239 case SYSTEM_IO_CAPABLE
:
240 cmd
.type
= SYSTEM_IO_CAPABLE
;
241 perf
= drv_data
[first_cpu(mask
)]->acpi_data
;
242 cmd
.addr
.io
.port
= perf
->control_register
.address
;
243 cmd
.addr
.io
.bit_width
= perf
->control_register
.bit_width
;
253 dprintk("get_cur_val = %u\n", cmd
.val
);
258 static unsigned int get_cur_freq_on_cpu(unsigned int cpu
)
260 struct acpi_cpufreq_data
*data
= drv_data
[cpu
];
263 dprintk("get_cur_freq_on_cpu (%d)\n", cpu
);
265 if (unlikely(data
== NULL
||
266 data
->acpi_data
== NULL
|| data
->freq_table
== NULL
)) {
270 freq
= extract_freq(get_cur_val(cpumask_of_cpu(cpu
)), data
);
271 dprintk("cur freq = %u\n", freq
);
276 static unsigned int check_freqs(cpumask_t mask
, unsigned int freq
,
277 struct acpi_cpufreq_data
*data
)
279 unsigned int cur_freq
;
282 for (i
= 0; i
< 100; i
++) {
283 cur_freq
= extract_freq(get_cur_val(mask
), data
);
284 if (cur_freq
== freq
)
291 static int acpi_cpufreq_target(struct cpufreq_policy
*policy
,
292 unsigned int target_freq
, unsigned int relation
)
294 struct acpi_cpufreq_data
*data
= drv_data
[policy
->cpu
];
295 struct acpi_processor_performance
*perf
;
296 struct cpufreq_freqs freqs
;
297 cpumask_t online_policy_cpus
;
300 unsigned int next_state
= 0;
301 unsigned int next_perf_state
= 0;
305 dprintk("acpi_cpufreq_target %d (%d)\n", target_freq
, policy
->cpu
);
307 if (unlikely(data
== NULL
||
308 data
->acpi_data
== NULL
|| data
->freq_table
== NULL
)) {
312 perf
= data
->acpi_data
;
313 result
= cpufreq_frequency_table_target(policy
,
316 relation
, &next_state
);
317 if (unlikely(result
))
320 #ifdef CONFIG_HOTPLUG_CPU
321 /* cpufreq holds the hotplug lock, so we are safe from here on */
322 cpus_and(online_policy_cpus
, cpu_online_map
, policy
->cpus
);
324 online_policy_cpus
= policy
->cpus
;
327 cmd
.val
= get_cur_val(online_policy_cpus
);
328 freqs
.old
= extract_freq(cmd
.val
, data
);
329 freqs
.new = data
->freq_table
[next_state
].frequency
;
330 next_perf_state
= data
->freq_table
[next_state
].index
;
331 if (freqs
.new == freqs
.old
) {
332 if (unlikely(data
->resume
)) {
333 dprintk("Called after resume, resetting to P%d\n",
337 dprintk("Already at target state (P%d)\n",
343 switch (data
->cpu_feature
) {
344 case SYSTEM_INTEL_MSR_CAPABLE
:
345 cmd
.type
= SYSTEM_INTEL_MSR_CAPABLE
;
346 cmd
.addr
.msr
.reg
= MSR_IA32_PERF_CTL
;
348 (u32
) perf
->states
[next_perf_state
].
349 control
& INTEL_MSR_RANGE
;
350 cmd
.val
= (cmd
.val
& ~INTEL_MSR_RANGE
) | msr
;
352 case SYSTEM_IO_CAPABLE
:
353 cmd
.type
= SYSTEM_IO_CAPABLE
;
354 cmd
.addr
.io
.port
= perf
->control_register
.address
;
355 cmd
.addr
.io
.bit_width
= perf
->control_register
.bit_width
;
356 cmd
.val
= (u32
) perf
->states
[next_perf_state
].control
;
362 cpus_clear(cmd
.mask
);
364 if (policy
->shared_type
!= CPUFREQ_SHARED_TYPE_ANY
)
365 cmd
.mask
= online_policy_cpus
;
367 cpu_set(policy
->cpu
, cmd
.mask
);
369 for_each_cpu_mask(i
, cmd
.mask
) {
371 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
376 if (acpi_pstate_strict
) {
377 if (!check_freqs(cmd
.mask
, freqs
.new, data
)) {
378 dprintk("acpi_cpufreq_target failed (%d)\n",
384 for_each_cpu_mask(i
, cmd
.mask
) {
386 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
388 perf
->state
= next_perf_state
;
393 static int acpi_cpufreq_verify(struct cpufreq_policy
*policy
)
395 struct acpi_cpufreq_data
*data
= drv_data
[policy
->cpu
];
397 dprintk("acpi_cpufreq_verify\n");
399 return cpufreq_frequency_table_verify(policy
, data
->freq_table
);
403 acpi_cpufreq_guess_freq(struct acpi_cpufreq_data
*data
, unsigned int cpu
)
405 struct acpi_processor_performance
*perf
= data
->acpi_data
;
408 /* search the closest match to cpu_khz */
411 unsigned long freqn
= perf
->states
[0].core_frequency
* 1000;
413 for (i
= 0; i
< (perf
->state_count
- 1); i
++) {
415 freqn
= perf
->states
[i
+ 1].core_frequency
* 1000;
416 if ((2 * cpu_khz
) > (freqn
+ freq
)) {
421 perf
->state
= perf
->state_count
- 1;
424 /* assume CPU is at P0... */
426 return perf
->states
[0].core_frequency
* 1000;
431 * acpi_cpufreq_early_init - initialize ACPI P-States library
433 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
434 * in order to determine correct frequency and voltage pairings. We can
435 * do _PDC and _PSD and find out the processor dependency for the
436 * actual init that will happen later...
438 static int acpi_cpufreq_early_init(void)
440 struct acpi_processor_performance
*data
;
444 dprintk("acpi_cpufreq_early_init\n");
446 for_each_possible_cpu(i
) {
447 data
= kzalloc(sizeof(struct acpi_processor_performance
),
450 for_each_cpu_mask(j
, covered
) {
451 kfree(acpi_perf_data
[j
]);
452 acpi_perf_data
[j
] = NULL
;
456 acpi_perf_data
[i
] = data
;
460 /* Do initialization in ACPI core */
461 acpi_processor_preregister_performance(acpi_perf_data
);
466 * Some BIOSes do SW_ANY coordination internally, either set it up in hw
467 * or do it in BIOS firmware and won't inform about it to OS. If not
468 * detected, this has a side effect of making CPU run at a different speed
469 * than OS intended it to run at. Detect it and handle it cleanly.
471 static int bios_with_sw_any_bug
;
473 static int sw_any_bug_found(struct dmi_system_id
*d
)
475 bios_with_sw_any_bug
= 1;
479 static struct dmi_system_id sw_any_bug_dmi_table
[] = {
481 .callback
= sw_any_bug_found
,
482 .ident
= "Supermicro Server X6DLP",
484 DMI_MATCH(DMI_SYS_VENDOR
, "Supermicro"),
485 DMI_MATCH(DMI_BIOS_VERSION
, "080010"),
486 DMI_MATCH(DMI_PRODUCT_NAME
, "X6DLP"),
492 static int acpi_cpufreq_cpu_init(struct cpufreq_policy
*policy
)
495 unsigned int valid_states
= 0;
496 unsigned int cpu
= policy
->cpu
;
497 struct acpi_cpufreq_data
*data
;
499 unsigned int result
= 0;
500 struct cpuinfo_x86
*c
= &cpu_data
[policy
->cpu
];
501 struct acpi_processor_performance
*perf
;
503 dprintk("acpi_cpufreq_cpu_init\n");
505 if (!acpi_perf_data
[cpu
])
508 data
= kzalloc(sizeof(struct acpi_cpufreq_data
), GFP_KERNEL
);
512 data
->acpi_data
= acpi_perf_data
[cpu
];
513 drv_data
[cpu
] = data
;
515 if (cpu_has(c
, X86_FEATURE_CONSTANT_TSC
)) {
516 acpi_cpufreq_driver
.flags
|= CPUFREQ_CONST_LOOPS
;
519 result
= acpi_processor_register_performance(data
->acpi_data
, cpu
);
523 perf
= data
->acpi_data
;
524 policy
->shared_type
= perf
->shared_type
;
526 * Will let policy->cpus know about dependency only when software
527 * coordination is required.
529 if (policy
->shared_type
== CPUFREQ_SHARED_TYPE_ALL
||
530 policy
->shared_type
== CPUFREQ_SHARED_TYPE_ANY
) {
531 policy
->cpus
= perf
->shared_cpu_map
;
535 dmi_check_system(sw_any_bug_dmi_table
);
536 if (bios_with_sw_any_bug
&& cpus_weight(policy
->cpus
) == 1) {
537 policy
->shared_type
= CPUFREQ_SHARED_TYPE_ALL
;
538 policy
->cpus
= cpu_core_map
[cpu
];
542 /* capability check */
543 if (perf
->state_count
<= 1) {
544 dprintk("No P-States\n");
549 if (perf
->control_register
.space_id
!= perf
->status_register
.space_id
) {
554 switch (perf
->control_register
.space_id
) {
555 case ACPI_ADR_SPACE_SYSTEM_IO
:
556 dprintk("SYSTEM IO addr space\n");
557 data
->cpu_feature
= SYSTEM_IO_CAPABLE
;
559 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
560 dprintk("HARDWARE addr space\n");
561 if (!check_est_cpu(cpu
)) {
565 data
->cpu_feature
= SYSTEM_INTEL_MSR_CAPABLE
;
568 dprintk("Unknown addr space %d\n",
569 (u32
) (perf
->control_register
.space_id
));
575 kmalloc(sizeof(struct cpufreq_frequency_table
) *
576 (perf
->state_count
+ 1), GFP_KERNEL
);
577 if (!data
->freq_table
) {
582 /* detect transition latency */
583 policy
->cpuinfo
.transition_latency
= 0;
584 for (i
= 0; i
< perf
->state_count
; i
++) {
585 if ((perf
->states
[i
].transition_latency
* 1000) >
586 policy
->cpuinfo
.transition_latency
)
587 policy
->cpuinfo
.transition_latency
=
588 perf
->states
[i
].transition_latency
* 1000;
590 policy
->governor
= CPUFREQ_DEFAULT_GOVERNOR
;
593 for (i
= 0; i
< perf
->state_count
; i
++) {
594 if (i
> 0 && perf
->states
[i
].core_frequency
==
595 perf
->states
[i
- 1].core_frequency
)
598 data
->freq_table
[valid_states
].index
= i
;
599 data
->freq_table
[valid_states
].frequency
=
600 perf
->states
[i
].core_frequency
* 1000;
603 data
->freq_table
[perf
->state_count
].frequency
= CPUFREQ_TABLE_END
;
605 result
= cpufreq_frequency_table_cpuinfo(policy
, data
->freq_table
);
610 switch (data
->cpu_feature
) {
611 case ACPI_ADR_SPACE_SYSTEM_IO
:
612 /* Current speed is unknown and not detectable by IO port */
613 policy
->cur
= acpi_cpufreq_guess_freq(data
, policy
->cpu
);
615 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
616 get_cur_freq_on_cpu(cpu
);
622 /* notify BIOS that we exist */
623 acpi_processor_notify_smm(THIS_MODULE
);
625 dprintk("CPU%u - ACPI performance management activated.\n", cpu
);
626 for (i
= 0; i
< perf
->state_count
; i
++)
627 dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
628 (i
== perf
->state
? '*' : ' '), i
,
629 (u32
) perf
->states
[i
].core_frequency
,
630 (u32
) perf
->states
[i
].power
,
631 (u32
) perf
->states
[i
].transition_latency
);
633 cpufreq_frequency_table_get_attr(data
->freq_table
, policy
->cpu
);
636 * the first call to ->target() should result in us actually
637 * writing something to the appropriate registers.
644 kfree(data
->freq_table
);
646 acpi_processor_unregister_performance(perf
, cpu
);
649 drv_data
[cpu
] = NULL
;
654 static int acpi_cpufreq_cpu_exit(struct cpufreq_policy
*policy
)
656 struct acpi_cpufreq_data
*data
= drv_data
[policy
->cpu
];
658 dprintk("acpi_cpufreq_cpu_exit\n");
661 cpufreq_frequency_table_put_attr(policy
->cpu
);
662 drv_data
[policy
->cpu
] = NULL
;
663 acpi_processor_unregister_performance(data
->acpi_data
,
671 static int acpi_cpufreq_resume(struct cpufreq_policy
*policy
)
673 struct acpi_cpufreq_data
*data
= drv_data
[policy
->cpu
];
675 dprintk("acpi_cpufreq_resume\n");
682 static struct freq_attr
*acpi_cpufreq_attr
[] = {
683 &cpufreq_freq_attr_scaling_available_freqs
,
687 static struct cpufreq_driver acpi_cpufreq_driver
= {
688 .verify
= acpi_cpufreq_verify
,
689 .target
= acpi_cpufreq_target
,
690 .get
= get_cur_freq_on_cpu
,
691 .init
= acpi_cpufreq_cpu_init
,
692 .exit
= acpi_cpufreq_cpu_exit
,
693 .resume
= acpi_cpufreq_resume
,
694 .name
= "acpi-cpufreq",
695 .owner
= THIS_MODULE
,
696 .attr
= acpi_cpufreq_attr
,
699 static int __init
acpi_cpufreq_init(void)
701 dprintk("acpi_cpufreq_init\n");
703 acpi_cpufreq_early_init();
705 return cpufreq_register_driver(&acpi_cpufreq_driver
);
708 static void __exit
acpi_cpufreq_exit(void)
711 dprintk("acpi_cpufreq_exit\n");
713 cpufreq_unregister_driver(&acpi_cpufreq_driver
);
715 for_each_possible_cpu(i
) {
716 kfree(acpi_perf_data
[i
]);
717 acpi_perf_data
[i
] = NULL
;
722 module_param(acpi_pstate_strict
, uint
, 0644);
723 MODULE_PARM_DESC(acpi_pstate_strict
,
724 "value 0 or non-zero. non-zero -> strict ACPI checks are performed during frequency changes.");
726 late_initcall(acpi_cpufreq_init
);
727 module_exit(acpi_cpufreq_exit
);
729 MODULE_ALIAS("acpi");