2 * processor_perflib.c - ACPI Processor P-States Library ($Revision: 71 $)
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) 2004 Dominik Brodowski <linux@brodo.de>
7 * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
8 * - Added processor hotplug support
11 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or (at
16 * your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/cpufreq.h>
35 #include <asm/cpufeature.h>
38 #include <acpi/acpi_bus.h>
39 #include <acpi/acpi_drivers.h>
40 #include <acpi/processor.h>
42 #define PREFIX "ACPI: "
44 #define ACPI_PROCESSOR_CLASS "processor"
45 #define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
46 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
47 ACPI_MODULE_NAME("processor_perflib");
49 static DEFINE_MUTEX(performance_mutex
);
51 /* Use cpufreq debug layer for _PPC changes. */
52 #define cpufreq_printk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
56 * _PPC support is implemented as a CPUfreq policy notifier:
57 * This means each time a CPUfreq driver registered also with
58 * the ACPI core is asked to change the speed policy, the maximum
59 * value is adjusted so that it is within the platform limit.
61 * Also, when a new platform limit value is detected, the CPUfreq
62 * policy is adjusted accordingly.
66 * -1 -> cpufreq low level drivers not initialized -> _PSS, etc. not called yet
68 * 0 -> cpufreq low level drivers initialized -> consider _PPC values
69 * 1 -> ignore _PPC totally -> forced by user through boot param
71 static int ignore_ppc
= -1;
72 module_param(ignore_ppc
, int, 0644);
73 MODULE_PARM_DESC(ignore_ppc
, "If the frequency of your machine gets wrongly" \
74 "limited by BIOS, this should help");
76 #define PPC_REGISTERED 1
79 static int acpi_processor_ppc_status
;
81 static int acpi_processor_ppc_notifier(struct notifier_block
*nb
,
82 unsigned long event
, void *data
)
84 struct cpufreq_policy
*policy
= data
;
85 struct acpi_processor
*pr
;
88 if (event
== CPUFREQ_START
&& ignore_ppc
<= 0) {
96 if (event
!= CPUFREQ_INCOMPATIBLE
)
99 mutex_lock(&performance_mutex
);
101 pr
= per_cpu(processors
, policy
->cpu
);
102 if (!pr
|| !pr
->performance
)
105 ppc
= (unsigned int)pr
->performance_platform_limit
;
107 if (ppc
>= pr
->performance
->state_count
)
110 cpufreq_verify_within_limits(policy
, 0,
111 pr
->performance
->states
[ppc
].
112 core_frequency
* 1000);
115 mutex_unlock(&performance_mutex
);
120 static struct notifier_block acpi_ppc_notifier_block
= {
121 .notifier_call
= acpi_processor_ppc_notifier
,
124 static int acpi_processor_get_platform_limit(struct acpi_processor
*pr
)
126 acpi_status status
= 0;
127 unsigned long long ppc
= 0;
134 * _PPC indicates the maximum state currently supported by the platform
135 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
137 status
= acpi_evaluate_integer(pr
->handle
, "_PPC", NULL
, &ppc
);
139 if (status
!= AE_NOT_FOUND
)
140 acpi_processor_ppc_status
|= PPC_IN_USE
;
142 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
143 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PPC"));
147 cpufreq_printk("CPU %d: _PPC is %d - frequency %s limited\n", pr
->id
,
148 (int)ppc
, ppc
? "" : "not");
150 pr
->performance_platform_limit
= (int)ppc
;
155 int acpi_processor_ppc_has_changed(struct acpi_processor
*pr
)
162 ret
= acpi_processor_get_platform_limit(pr
);
167 return cpufreq_update_policy(pr
->id
);
170 void acpi_processor_ppc_init(void)
172 if (!cpufreq_register_notifier
173 (&acpi_ppc_notifier_block
, CPUFREQ_POLICY_NOTIFIER
))
174 acpi_processor_ppc_status
|= PPC_REGISTERED
;
177 "Warning: Processor Platform Limit not supported.\n");
180 void acpi_processor_ppc_exit(void)
182 if (acpi_processor_ppc_status
& PPC_REGISTERED
)
183 cpufreq_unregister_notifier(&acpi_ppc_notifier_block
,
184 CPUFREQ_POLICY_NOTIFIER
);
186 acpi_processor_ppc_status
&= ~PPC_REGISTERED
;
189 static int acpi_processor_get_performance_control(struct acpi_processor
*pr
)
192 acpi_status status
= 0;
193 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
194 union acpi_object
*pct
= NULL
;
195 union acpi_object obj
= { 0 };
198 status
= acpi_evaluate_object(pr
->handle
, "_PCT", NULL
, &buffer
);
199 if (ACPI_FAILURE(status
)) {
200 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PCT"));
204 pct
= (union acpi_object
*)buffer
.pointer
;
205 if (!pct
|| (pct
->type
!= ACPI_TYPE_PACKAGE
)
206 || (pct
->package
.count
!= 2)) {
207 printk(KERN_ERR PREFIX
"Invalid _PCT data\n");
216 obj
= pct
->package
.elements
[0];
218 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
219 || (obj
.buffer
.length
< sizeof(struct acpi_pct_register
))
220 || (obj
.buffer
.pointer
== NULL
)) {
221 printk(KERN_ERR PREFIX
"Invalid _PCT data (control_register)\n");
225 memcpy(&pr
->performance
->control_register
, obj
.buffer
.pointer
,
226 sizeof(struct acpi_pct_register
));
232 obj
= pct
->package
.elements
[1];
234 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
235 || (obj
.buffer
.length
< sizeof(struct acpi_pct_register
))
236 || (obj
.buffer
.pointer
== NULL
)) {
237 printk(KERN_ERR PREFIX
"Invalid _PCT data (status_register)\n");
242 memcpy(&pr
->performance
->status_register
, obj
.buffer
.pointer
,
243 sizeof(struct acpi_pct_register
));
246 kfree(buffer
.pointer
);
251 static int acpi_processor_get_performance_states(struct acpi_processor
*pr
)
254 acpi_status status
= AE_OK
;
255 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
256 struct acpi_buffer format
= { sizeof("NNNNNN"), "NNNNNN" };
257 struct acpi_buffer state
= { 0, NULL
};
258 union acpi_object
*pss
= NULL
;
262 status
= acpi_evaluate_object(pr
->handle
, "_PSS", NULL
, &buffer
);
263 if (ACPI_FAILURE(status
)) {
264 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PSS"));
268 pss
= buffer
.pointer
;
269 if (!pss
|| (pss
->type
!= ACPI_TYPE_PACKAGE
)) {
270 printk(KERN_ERR PREFIX
"Invalid _PSS data\n");
275 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d performance states\n",
276 pss
->package
.count
));
278 pr
->performance
->state_count
= pss
->package
.count
;
279 pr
->performance
->states
=
280 kmalloc(sizeof(struct acpi_processor_px
) * pss
->package
.count
,
282 if (!pr
->performance
->states
) {
287 for (i
= 0; i
< pr
->performance
->state_count
; i
++) {
289 struct acpi_processor_px
*px
= &(pr
->performance
->states
[i
]);
291 state
.length
= sizeof(struct acpi_processor_px
);
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Extracting state %d\n", i
));
296 status
= acpi_extract_package(&(pss
->package
.elements
[i
]),
298 if (ACPI_FAILURE(status
)) {
299 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _PSS data"));
301 kfree(pr
->performance
->states
);
305 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
306 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
308 (u32
) px
->core_frequency
,
310 (u32
) px
->transition_latency
,
311 (u32
) px
->bus_master_latency
,
312 (u32
) px
->control
, (u32
) px
->status
));
315 * Check that ACPI's u64 MHz will be valid as u32 KHz in cpufreq
317 if (!px
->core_frequency
||
318 ((u32
)(px
->core_frequency
* 1000) !=
319 (px
->core_frequency
* 1000))) {
320 printk(KERN_ERR FW_BUG PREFIX
321 "Invalid BIOS _PSS frequency: 0x%llx MHz\n",
324 kfree(pr
->performance
->states
);
330 kfree(buffer
.pointer
);
335 static int acpi_processor_get_performance_info(struct acpi_processor
*pr
)
338 acpi_status status
= AE_OK
;
339 acpi_handle handle
= NULL
;
341 if (!pr
|| !pr
->performance
|| !pr
->handle
)
344 status
= acpi_get_handle(pr
->handle
, "_PCT", &handle
);
345 if (ACPI_FAILURE(status
)) {
346 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
347 "ACPI-based processor performance control unavailable\n"));
351 result
= acpi_processor_get_performance_control(pr
);
355 result
= acpi_processor_get_performance_states(pr
);
362 * Having _PPC but missing frequencies (_PSS, _PCT) is a very good hint that
363 * the BIOS is older than the CPU and does not know its frequencies
367 if (ACPI_SUCCESS(acpi_get_handle(pr
->handle
, "_PPC", &handle
))){
368 if(boot_cpu_has(X86_FEATURE_EST
))
369 printk(KERN_WARNING FW_BUG
"BIOS needs update for CPU "
370 "frequency support\n");
376 int acpi_processor_notify_smm(struct module
*calling_module
)
379 static int is_done
= 0;
382 if (!(acpi_processor_ppc_status
& PPC_REGISTERED
))
385 if (!try_module_get(calling_module
))
388 /* is_done is set to negative if an error occured,
389 * and to postitive if _no_ error occured, but SMM
390 * was already notified. This avoids double notification
391 * which might lead to unexpected results...
394 module_put(calling_module
);
396 } else if (is_done
< 0) {
397 module_put(calling_module
);
403 /* Can't write pstate_control to smi_command if either value is zero */
404 if ((!acpi_gbl_FADT
.smi_command
) || (!acpi_gbl_FADT
.pstate_control
)) {
405 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No SMI port or pstate_control\n"));
406 module_put(calling_module
);
410 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
411 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
412 acpi_gbl_FADT
.pstate_control
, acpi_gbl_FADT
.smi_command
));
414 status
= acpi_os_write_port(acpi_gbl_FADT
.smi_command
,
415 (u32
) acpi_gbl_FADT
.pstate_control
, 8);
416 if (ACPI_FAILURE(status
)) {
417 ACPI_EXCEPTION((AE_INFO
, status
,
418 "Failed to write pstate_control [0x%x] to "
419 "smi_command [0x%x]", acpi_gbl_FADT
.pstate_control
,
420 acpi_gbl_FADT
.smi_command
));
421 module_put(calling_module
);
425 /* Success. If there's no _PPC, we need to fear nothing, so
426 * we can allow the cpufreq driver to be rmmod'ed. */
429 if (!(acpi_processor_ppc_status
& PPC_IN_USE
))
430 module_put(calling_module
);
435 EXPORT_SYMBOL(acpi_processor_notify_smm
);
437 static int acpi_processor_get_psd(struct acpi_processor
*pr
)
440 acpi_status status
= AE_OK
;
441 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
442 struct acpi_buffer format
= {sizeof("NNNNN"), "NNNNN"};
443 struct acpi_buffer state
= {0, NULL
};
444 union acpi_object
*psd
= NULL
;
445 struct acpi_psd_package
*pdomain
;
447 status
= acpi_evaluate_object(pr
->handle
, "_PSD", NULL
, &buffer
);
448 if (ACPI_FAILURE(status
)) {
452 psd
= buffer
.pointer
;
453 if (!psd
|| (psd
->type
!= ACPI_TYPE_PACKAGE
)) {
454 printk(KERN_ERR PREFIX
"Invalid _PSD data\n");
459 if (psd
->package
.count
!= 1) {
460 printk(KERN_ERR PREFIX
"Invalid _PSD data\n");
465 pdomain
= &(pr
->performance
->domain_info
);
467 state
.length
= sizeof(struct acpi_psd_package
);
468 state
.pointer
= pdomain
;
470 status
= acpi_extract_package(&(psd
->package
.elements
[0]),
472 if (ACPI_FAILURE(status
)) {
473 printk(KERN_ERR PREFIX
"Invalid _PSD data\n");
478 if (pdomain
->num_entries
!= ACPI_PSD_REV0_ENTRIES
) {
479 printk(KERN_ERR PREFIX
"Unknown _PSD:num_entries\n");
484 if (pdomain
->revision
!= ACPI_PSD_REV0_REVISION
) {
485 printk(KERN_ERR PREFIX
"Unknown _PSD:revision\n");
490 if (pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ALL
&&
491 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ANY
&&
492 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_HW_ALL
) {
493 printk(KERN_ERR PREFIX
"Invalid _PSD:coord_type\n");
498 kfree(buffer
.pointer
);
502 int acpi_processor_preregister_performance(
503 struct acpi_processor_performance
*performance
)
505 int count
, count_target
;
508 cpumask_var_t covered_cpus
;
509 struct acpi_processor
*pr
;
510 struct acpi_psd_package
*pdomain
;
511 struct acpi_processor
*match_pr
;
512 struct acpi_psd_package
*match_pdomain
;
514 if (!zalloc_cpumask_var(&covered_cpus
, GFP_KERNEL
))
517 mutex_lock(&performance_mutex
);
520 * Check if another driver has already registered, and abort before
521 * changing pr->performance if it has. Check input data as well.
523 for_each_possible_cpu(i
) {
524 pr
= per_cpu(processors
, i
);
526 /* Look only at processors in ACPI namespace */
530 if (pr
->performance
) {
535 if (!performance
|| !per_cpu_ptr(performance
, i
)) {
541 /* Call _PSD for all CPUs */
542 for_each_possible_cpu(i
) {
543 pr
= per_cpu(processors
, i
);
547 pr
->performance
= per_cpu_ptr(performance
, i
);
548 cpumask_set_cpu(i
, pr
->performance
->shared_cpu_map
);
549 if (acpi_processor_get_psd(pr
)) {
558 * Now that we have _PSD data from all CPUs, lets setup P-state
561 for_each_possible_cpu(i
) {
562 pr
= per_cpu(processors
, i
);
566 if (cpumask_test_cpu(i
, covered_cpus
))
569 pdomain
= &(pr
->performance
->domain_info
);
570 cpumask_set_cpu(i
, pr
->performance
->shared_cpu_map
);
571 cpumask_set_cpu(i
, covered_cpus
);
572 if (pdomain
->num_processors
<= 1)
575 /* Validate the Domain info */
576 count_target
= pdomain
->num_processors
;
578 if (pdomain
->coord_type
== DOMAIN_COORD_TYPE_SW_ALL
)
579 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_ALL
;
580 else if (pdomain
->coord_type
== DOMAIN_COORD_TYPE_HW_ALL
)
581 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_HW
;
582 else if (pdomain
->coord_type
== DOMAIN_COORD_TYPE_SW_ANY
)
583 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_ANY
;
585 for_each_possible_cpu(j
) {
589 match_pr
= per_cpu(processors
, j
);
593 match_pdomain
= &(match_pr
->performance
->domain_info
);
594 if (match_pdomain
->domain
!= pdomain
->domain
)
597 /* Here i and j are in the same domain */
599 if (match_pdomain
->num_processors
!= count_target
) {
604 if (pdomain
->coord_type
!= match_pdomain
->coord_type
) {
609 cpumask_set_cpu(j
, covered_cpus
);
610 cpumask_set_cpu(j
, pr
->performance
->shared_cpu_map
);
614 for_each_possible_cpu(j
) {
618 match_pr
= per_cpu(processors
, j
);
622 match_pdomain
= &(match_pr
->performance
->domain_info
);
623 if (match_pdomain
->domain
!= pdomain
->domain
)
626 match_pr
->performance
->shared_type
=
627 pr
->performance
->shared_type
;
628 cpumask_copy(match_pr
->performance
->shared_cpu_map
,
629 pr
->performance
->shared_cpu_map
);
634 for_each_possible_cpu(i
) {
635 pr
= per_cpu(processors
, i
);
636 if (!pr
|| !pr
->performance
)
639 /* Assume no coordination on any error parsing domain info */
641 cpumask_clear(pr
->performance
->shared_cpu_map
);
642 cpumask_set_cpu(i
, pr
->performance
->shared_cpu_map
);
643 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_ALL
;
645 pr
->performance
= NULL
; /* Will be set for real in register */
649 mutex_unlock(&performance_mutex
);
650 free_cpumask_var(covered_cpus
);
653 EXPORT_SYMBOL(acpi_processor_preregister_performance
);
656 acpi_processor_register_performance(struct acpi_processor_performance
657 *performance
, unsigned int cpu
)
659 struct acpi_processor
*pr
;
661 if (!(acpi_processor_ppc_status
& PPC_REGISTERED
))
664 mutex_lock(&performance_mutex
);
666 pr
= per_cpu(processors
, cpu
);
668 mutex_unlock(&performance_mutex
);
672 if (pr
->performance
) {
673 mutex_unlock(&performance_mutex
);
677 WARN_ON(!performance
);
679 pr
->performance
= performance
;
681 if (acpi_processor_get_performance_info(pr
)) {
682 pr
->performance
= NULL
;
683 mutex_unlock(&performance_mutex
);
687 mutex_unlock(&performance_mutex
);
691 EXPORT_SYMBOL(acpi_processor_register_performance
);
694 acpi_processor_unregister_performance(struct acpi_processor_performance
695 *performance
, unsigned int cpu
)
697 struct acpi_processor
*pr
;
699 mutex_lock(&performance_mutex
);
701 pr
= per_cpu(processors
, cpu
);
703 mutex_unlock(&performance_mutex
);
708 kfree(pr
->performance
->states
);
709 pr
->performance
= NULL
;
711 mutex_unlock(&performance_mutex
);
716 EXPORT_SYMBOL(acpi_processor_unregister_performance
);