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>
42 #include <asm/processor.h>
43 #include <asm/cpufeature.h>
44 #include <asm/delay.h>
45 #include <asm/uaccess.h>
47 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)
49 MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
50 MODULE_DESCRIPTION("ACPI Processor P-States Driver");
51 MODULE_LICENSE("GPL");
54 struct acpi_cpufreq_data
{
55 struct acpi_processor_performance
*acpi_data
;
56 struct cpufreq_frequency_table
*freq_table
;
60 static struct acpi_cpufreq_data
*drv_data
[NR_CPUS
];
61 static struct acpi_processor_performance
*acpi_perf_data
[NR_CPUS
];
63 static struct cpufreq_driver acpi_cpufreq_driver
;
65 static unsigned int acpi_pstate_strict
;
67 static unsigned extract_freq(u32 value
, struct acpi_cpufreq_data
*data
)
69 struct acpi_processor_performance
*perf
;
72 perf
= data
->acpi_data
;
74 for (i
= 0; i
< perf
->state_count
; i
++) {
75 if (value
== perf
->states
[i
].status
)
76 return data
->freq_table
[i
].frequency
;
82 static void wrport(u16 port
, u8 bit_width
, u32 value
)
86 } else if (bit_width
<= 16) {
88 } else if (bit_width
<= 32) {
93 static void rdport(u16 port
, u8 bit_width
, u32
*ret
)
98 } else if (bit_width
<= 16) {
100 } else if (bit_width
<= 32) {
116 static void do_drv_read(struct drv_cmd
*cmd
)
118 rdport(cmd
->addr
.port
, cmd
->addr
.bit_width
, &cmd
->val
);
122 static void do_drv_write(struct drv_cmd
*cmd
)
124 wrport(cmd
->addr
.port
, cmd
->addr
.bit_width
, cmd
->val
);
128 static inline void drv_read(struct drv_cmd
*cmd
)
130 cpumask_t saved_mask
= current
->cpus_allowed
;
133 set_cpus_allowed(current
, cmd
->mask
);
135 set_cpus_allowed(current
, saved_mask
);
139 static void drv_write(struct drv_cmd
*cmd
)
141 cpumask_t saved_mask
= current
->cpus_allowed
;
144 for_each_cpu_mask(i
, cmd
->mask
) {
145 set_cpus_allowed(current
, cpumask_of_cpu(i
));
149 set_cpus_allowed(current
, saved_mask
);
153 static u32
get_cur_val(cpumask_t mask
)
155 struct acpi_processor_performance
*perf
;
158 if (unlikely(cpus_empty(mask
)))
161 perf
= drv_data
[first_cpu(mask
)]->acpi_data
;
162 cmd
.addr
.port
= perf
->control_register
.address
;
163 cmd
.addr
.bit_width
= perf
->control_register
.bit_width
;
168 dprintk("get_cur_val = %u\n", cmd
.val
);
173 static unsigned int get_cur_freq_on_cpu(unsigned int cpu
)
175 struct acpi_cpufreq_data
*data
= drv_data
[cpu
];
178 dprintk("get_cur_freq_on_cpu (%d)\n", cpu
);
180 if (unlikely(data
== NULL
||
181 data
->acpi_data
== NULL
||
182 data
->freq_table
== NULL
)) {
186 freq
= extract_freq(get_cur_val(cpumask_of_cpu(cpu
)), data
);
187 dprintk("cur freq = %u\n", freq
);
192 static unsigned int check_freqs(cpumask_t mask
, unsigned int freq
,
193 struct acpi_cpufreq_data
*data
)
195 unsigned int cur_freq
;
198 for (i
= 0; i
< 100; i
++) {
199 cur_freq
= extract_freq(get_cur_val(mask
), data
);
200 if (cur_freq
== freq
)
207 static int acpi_cpufreq_target(struct cpufreq_policy
*policy
,
208 unsigned int target_freq
,
209 unsigned int relation
)
211 struct acpi_cpufreq_data
*data
= drv_data
[policy
->cpu
];
212 struct acpi_processor_performance
*perf
;
213 struct cpufreq_freqs freqs
;
214 cpumask_t online_policy_cpus
;
216 unsigned int next_state
= 0;
217 unsigned int next_perf_state
= 0;
221 dprintk("acpi_cpufreq_target %d (%d)\n", target_freq
, policy
->cpu
);
223 if (unlikely(data
== NULL
||
224 data
->acpi_data
== NULL
||
225 data
->freq_table
== NULL
)) {
229 perf
= data
->acpi_data
;
230 result
= cpufreq_frequency_table_target(policy
,
235 if (unlikely(result
))
238 #ifdef CONFIG_HOTPLUG_CPU
239 /* cpufreq holds the hotplug lock, so we are safe from here on */
240 cpus_and(online_policy_cpus
, cpu_online_map
, policy
->cpus
);
242 online_policy_cpus
= policy
->cpus
;
245 cmd
.val
= get_cur_val(online_policy_cpus
);
246 freqs
.old
= extract_freq(cmd
.val
, data
);
247 freqs
.new = data
->freq_table
[next_state
].frequency
;
248 next_perf_state
= data
->freq_table
[next_state
].index
;
249 if (freqs
.new == freqs
.old
) {
250 if (unlikely(data
->resume
)) {
251 dprintk("Called after resume, resetting to P%d\n", next_perf_state
);
254 dprintk("Already at target state (P%d)\n", next_perf_state
);
259 cmd
.addr
.port
= perf
->control_register
.address
;
260 cmd
.addr
.bit_width
= perf
->control_register
.bit_width
;
261 cmd
.val
= (u32
) perf
->states
[next_perf_state
].control
;
263 cpus_clear(cmd
.mask
);
265 if (policy
->shared_type
!= CPUFREQ_SHARED_TYPE_ANY
)
266 cmd
.mask
= online_policy_cpus
;
268 cpu_set(policy
->cpu
, cmd
.mask
);
270 for_each_cpu_mask(i
, cmd
.mask
) {
272 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
277 if (acpi_pstate_strict
) {
278 if (!check_freqs(cmd
.mask
, freqs
.new, data
)) {
279 dprintk("acpi_cpufreq_target failed (%d)\n",
285 for_each_cpu_mask(i
, cmd
.mask
) {
287 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
289 perf
->state
= next_perf_state
;
296 acpi_cpufreq_verify (
297 struct cpufreq_policy
*policy
)
299 struct acpi_cpufreq_data
*data
= drv_data
[policy
->cpu
];
301 dprintk("acpi_cpufreq_verify\n");
303 return cpufreq_frequency_table_verify(policy
, data
->freq_table
);
308 acpi_cpufreq_guess_freq (
309 struct acpi_cpufreq_data
*data
,
312 struct acpi_processor_performance
*perf
= data
->acpi_data
;
315 /* search the closest match to cpu_khz */
318 unsigned long freqn
= perf
->states
[0].core_frequency
* 1000;
320 for (i
= 0; i
< (perf
->state_count
- 1); i
++) {
322 freqn
= perf
->states
[i
+1].core_frequency
* 1000;
323 if ((2 * cpu_khz
) > (freqn
+ freq
)) {
328 perf
->state
= perf
->state_count
- 1;
331 /* assume CPU is at P0... */
333 return perf
->states
[0].core_frequency
* 1000;
339 * acpi_cpufreq_early_init - initialize ACPI P-States library
341 * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
342 * in order to determine correct frequency and voltage pairings. We can
343 * do _PDC and _PSD and find out the processor dependency for the
344 * actual init that will happen later...
346 static int acpi_cpufreq_early_init(void)
348 struct acpi_processor_performance
*data
;
352 dprintk("acpi_cpufreq_early_init\n");
354 for_each_possible_cpu(i
) {
355 data
= kzalloc(sizeof(struct acpi_processor_performance
),
358 for_each_cpu_mask(j
, covered
) {
359 kfree(acpi_perf_data
[j
]);
360 acpi_perf_data
[j
] = NULL
;
364 acpi_perf_data
[i
] = data
;
368 /* Do initialization in ACPI core */
369 acpi_processor_preregister_performance(acpi_perf_data
);
374 * Some BIOSes do SW_ANY coordination internally, either set it up in hw
375 * or do it in BIOS firmware and won't inform about it to OS. If not
376 * detected, this has a side effect of making CPU run at a different speed
377 * than OS intended it to run at. Detect it and handle it cleanly.
379 static int bios_with_sw_any_bug
;
381 static int sw_any_bug_found(struct dmi_system_id
*d
)
383 bios_with_sw_any_bug
= 1;
387 static struct dmi_system_id sw_any_bug_dmi_table
[] = {
389 .callback
= sw_any_bug_found
,
390 .ident
= "Supermicro Server X6DLP",
392 DMI_MATCH(DMI_SYS_VENDOR
, "Supermicro"),
393 DMI_MATCH(DMI_BIOS_VERSION
, "080010"),
394 DMI_MATCH(DMI_PRODUCT_NAME
, "X6DLP"),
401 acpi_cpufreq_cpu_init (
402 struct cpufreq_policy
*policy
)
405 unsigned int valid_states
= 0;
406 unsigned int cpu
= policy
->cpu
;
407 struct acpi_cpufreq_data
*data
;
408 unsigned int result
= 0;
409 struct cpuinfo_x86
*c
= &cpu_data
[policy
->cpu
];
410 struct acpi_processor_performance
*perf
;
412 dprintk("acpi_cpufreq_cpu_init\n");
414 if (!acpi_perf_data
[cpu
])
417 data
= kzalloc(sizeof(struct acpi_cpufreq_data
), GFP_KERNEL
);
421 data
->acpi_data
= acpi_perf_data
[cpu
];
422 drv_data
[cpu
] = data
;
424 if (cpu_has(c
, X86_FEATURE_CONSTANT_TSC
)) {
425 acpi_cpufreq_driver
.flags
|= CPUFREQ_CONST_LOOPS
;
428 result
= acpi_processor_register_performance(data
->acpi_data
, cpu
);
432 perf
= data
->acpi_data
;
433 policy
->shared_type
= perf
->shared_type
;
435 * Will let policy->cpus know about dependency only when software
436 * coordination is required.
438 if (policy
->shared_type
== CPUFREQ_SHARED_TYPE_ALL
||
439 policy
->shared_type
== CPUFREQ_SHARED_TYPE_ANY
) {
440 policy
->cpus
= perf
->shared_cpu_map
;
444 dmi_check_system(sw_any_bug_dmi_table
);
445 if (bios_with_sw_any_bug
&& cpus_weight(policy
->cpus
) == 1) {
446 policy
->shared_type
= CPUFREQ_SHARED_TYPE_ALL
;
447 policy
->cpus
= cpu_core_map
[cpu
];
451 /* capability check */
452 if (perf
->state_count
<= 1) {
453 dprintk("No P-States\n");
458 if (perf
->control_register
.space_id
!= perf
->status_register
.space_id
) {
463 switch (perf
->control_register
.space_id
) {
464 case ACPI_ADR_SPACE_SYSTEM_IO
:
465 dprintk("SYSTEM IO addr space\n");
468 dprintk("Unknown addr space %d\n",
469 (u32
) (perf
->control_register
.space_id
));
474 data
->freq_table
= kmalloc(sizeof(struct cpufreq_frequency_table
) * (perf
->state_count
+ 1), GFP_KERNEL
);
475 if (!data
->freq_table
) {
480 /* detect transition latency */
481 policy
->cpuinfo
.transition_latency
= 0;
482 for (i
=0; i
<perf
->state_count
; i
++) {
483 if ((perf
->states
[i
].transition_latency
* 1000) > policy
->cpuinfo
.transition_latency
)
484 policy
->cpuinfo
.transition_latency
= perf
->states
[i
].transition_latency
* 1000;
486 policy
->governor
= CPUFREQ_DEFAULT_GOVERNOR
;
488 /* The current speed is unknown and not detectable by ACPI... */
489 policy
->cur
= acpi_cpufreq_guess_freq(data
, policy
->cpu
);
492 for (i
=0; i
<perf
->state_count
; i
++)
494 if ( i
> 0 && perf
->states
[i
].core_frequency
==
495 perf
->states
[i
- 1].core_frequency
)
498 data
->freq_table
[valid_states
].index
= i
;
499 data
->freq_table
[valid_states
].frequency
=
500 perf
->states
[i
].core_frequency
* 1000;
503 data
->freq_table
[perf
->state_count
].frequency
= CPUFREQ_TABLE_END
;
505 result
= cpufreq_frequency_table_cpuinfo(policy
, data
->freq_table
);
510 /* notify BIOS that we exist */
511 acpi_processor_notify_smm(THIS_MODULE
);
513 dprintk("CPU%u - ACPI performance management activated.\n", cpu
);
514 for (i
= 0; i
< perf
->state_count
; i
++)
515 dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
516 (i
== perf
->state
?'*':' '), i
,
517 (u32
) perf
->states
[i
].core_frequency
,
518 (u32
) perf
->states
[i
].power
,
519 (u32
) perf
->states
[i
].transition_latency
);
521 cpufreq_frequency_table_get_attr(data
->freq_table
, policy
->cpu
);
524 * the first call to ->target() should result in us actually
525 * writing something to the appropriate registers.
532 kfree(data
->freq_table
);
534 acpi_processor_unregister_performance(perf
, cpu
);
537 drv_data
[cpu
] = NULL
;
544 acpi_cpufreq_cpu_exit (
545 struct cpufreq_policy
*policy
)
547 struct acpi_cpufreq_data
*data
= drv_data
[policy
->cpu
];
550 dprintk("acpi_cpufreq_cpu_exit\n");
553 cpufreq_frequency_table_put_attr(policy
->cpu
);
554 drv_data
[policy
->cpu
] = NULL
;
555 acpi_processor_unregister_performance(data
->acpi_data
, policy
->cpu
);
563 acpi_cpufreq_resume (
564 struct cpufreq_policy
*policy
)
566 struct acpi_cpufreq_data
*data
= drv_data
[policy
->cpu
];
569 dprintk("acpi_cpufreq_resume\n");
577 static struct freq_attr
* acpi_cpufreq_attr
[] = {
578 &cpufreq_freq_attr_scaling_available_freqs
,
582 static struct cpufreq_driver acpi_cpufreq_driver
= {
583 .verify
= acpi_cpufreq_verify
,
584 .target
= acpi_cpufreq_target
,
585 .get
= get_cur_freq_on_cpu
,
586 .init
= acpi_cpufreq_cpu_init
,
587 .exit
= acpi_cpufreq_cpu_exit
,
588 .resume
= acpi_cpufreq_resume
,
589 .name
= "acpi-cpufreq",
590 .owner
= THIS_MODULE
,
591 .attr
= acpi_cpufreq_attr
,
596 acpi_cpufreq_init (void)
598 dprintk("acpi_cpufreq_init\n");
600 acpi_cpufreq_early_init();
602 return cpufreq_register_driver(&acpi_cpufreq_driver
);
607 acpi_cpufreq_exit (void)
610 dprintk("acpi_cpufreq_exit\n");
612 cpufreq_unregister_driver(&acpi_cpufreq_driver
);
614 for_each_possible_cpu(i
) {
615 kfree(acpi_perf_data
[i
]);
616 acpi_perf_data
[i
] = NULL
;
621 module_param(acpi_pstate_strict
, uint
, 0644);
622 MODULE_PARM_DESC(acpi_pstate_strict
, "value 0 or non-zero. non-zero -> strict ACPI checks are performed during frequency changes.");
624 late_initcall(acpi_cpufreq_init
);
625 module_exit(acpi_cpufreq_exit
);
627 MODULE_ALIAS("acpi");