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 #define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
157 * acpi_processor_ppc_ost: Notify firmware the _PPC evaluation status
158 * @handle: ACPI processor handle
159 * @status: the status code of _PPC evaluation
160 * 0: success. OSPM is now using the performance state specificed.
161 * 1: failure. OSPM has not changed the number of P-states in use
163 static void acpi_processor_ppc_ost(acpi_handle handle
, int status
)
165 union acpi_object params
[2] = {
166 {.type
= ACPI_TYPE_INTEGER
,},
167 {.type
= ACPI_TYPE_INTEGER
,},
169 struct acpi_object_list arg_list
= {2, params
};
172 params
[0].integer
.value
= ACPI_PROCESSOR_NOTIFY_PERFORMANCE
;
173 params
[1].integer
.value
= status
;
175 /* when there is no _OST , skip it */
176 if (ACPI_FAILURE(acpi_get_handle(handle
, "_OST", &temp
)))
179 acpi_evaluate_object(handle
, "_OST", &arg_list
, NULL
);
183 int acpi_processor_ppc_has_changed(struct acpi_processor
*pr
, int event_flag
)
189 * Only when it is notification event, the _OST object
190 * will be evaluated. Otherwise it is skipped.
193 acpi_processor_ppc_ost(pr
->handle
, 1);
197 ret
= acpi_processor_get_platform_limit(pr
);
199 * Only when it is notification event, the _OST object
200 * will be evaluated. Otherwise it is skipped.
204 acpi_processor_ppc_ost(pr
->handle
, 1);
206 acpi_processor_ppc_ost(pr
->handle
, 0);
211 return cpufreq_update_policy(pr
->id
);
214 int acpi_processor_get_bios_limit(int cpu
, unsigned int *limit
)
216 struct acpi_processor
*pr
;
218 pr
= per_cpu(processors
, cpu
);
219 if (!pr
|| !pr
->performance
|| !pr
->performance
->state_count
)
221 *limit
= pr
->performance
->states
[pr
->performance_platform_limit
].
222 core_frequency
* 1000;
225 EXPORT_SYMBOL(acpi_processor_get_bios_limit
);
227 void acpi_processor_ppc_init(void)
229 if (!cpufreq_register_notifier
230 (&acpi_ppc_notifier_block
, CPUFREQ_POLICY_NOTIFIER
))
231 acpi_processor_ppc_status
|= PPC_REGISTERED
;
234 "Warning: Processor Platform Limit not supported.\n");
237 void acpi_processor_ppc_exit(void)
239 if (acpi_processor_ppc_status
& PPC_REGISTERED
)
240 cpufreq_unregister_notifier(&acpi_ppc_notifier_block
,
241 CPUFREQ_POLICY_NOTIFIER
);
243 acpi_processor_ppc_status
&= ~PPC_REGISTERED
;
246 static int acpi_processor_get_performance_control(struct acpi_processor
*pr
)
249 acpi_status status
= 0;
250 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
251 union acpi_object
*pct
= NULL
;
252 union acpi_object obj
= { 0 };
255 status
= acpi_evaluate_object(pr
->handle
, "_PCT", NULL
, &buffer
);
256 if (ACPI_FAILURE(status
)) {
257 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PCT"));
261 pct
= (union acpi_object
*)buffer
.pointer
;
262 if (!pct
|| (pct
->type
!= ACPI_TYPE_PACKAGE
)
263 || (pct
->package
.count
!= 2)) {
264 printk(KERN_ERR PREFIX
"Invalid _PCT data\n");
273 obj
= pct
->package
.elements
[0];
275 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
276 || (obj
.buffer
.length
< sizeof(struct acpi_pct_register
))
277 || (obj
.buffer
.pointer
== NULL
)) {
278 printk(KERN_ERR PREFIX
"Invalid _PCT data (control_register)\n");
282 memcpy(&pr
->performance
->control_register
, obj
.buffer
.pointer
,
283 sizeof(struct acpi_pct_register
));
289 obj
= pct
->package
.elements
[1];
291 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
292 || (obj
.buffer
.length
< sizeof(struct acpi_pct_register
))
293 || (obj
.buffer
.pointer
== NULL
)) {
294 printk(KERN_ERR PREFIX
"Invalid _PCT data (status_register)\n");
299 memcpy(&pr
->performance
->status_register
, obj
.buffer
.pointer
,
300 sizeof(struct acpi_pct_register
));
303 kfree(buffer
.pointer
);
308 static int acpi_processor_get_performance_states(struct acpi_processor
*pr
)
311 acpi_status status
= AE_OK
;
312 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
313 struct acpi_buffer format
= { sizeof("NNNNNN"), "NNNNNN" };
314 struct acpi_buffer state
= { 0, NULL
};
315 union acpi_object
*pss
= NULL
;
319 status
= acpi_evaluate_object(pr
->handle
, "_PSS", NULL
, &buffer
);
320 if (ACPI_FAILURE(status
)) {
321 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PSS"));
325 pss
= buffer
.pointer
;
326 if (!pss
|| (pss
->type
!= ACPI_TYPE_PACKAGE
)) {
327 printk(KERN_ERR PREFIX
"Invalid _PSS data\n");
332 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d performance states\n",
333 pss
->package
.count
));
335 pr
->performance
->state_count
= pss
->package
.count
;
336 pr
->performance
->states
=
337 kmalloc(sizeof(struct acpi_processor_px
) * pss
->package
.count
,
339 if (!pr
->performance
->states
) {
344 for (i
= 0; i
< pr
->performance
->state_count
; i
++) {
346 struct acpi_processor_px
*px
= &(pr
->performance
->states
[i
]);
348 state
.length
= sizeof(struct acpi_processor_px
);
351 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Extracting state %d\n", i
));
353 status
= acpi_extract_package(&(pss
->package
.elements
[i
]),
355 if (ACPI_FAILURE(status
)) {
356 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _PSS data"));
358 kfree(pr
->performance
->states
);
362 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
363 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
365 (u32
) px
->core_frequency
,
367 (u32
) px
->transition_latency
,
368 (u32
) px
->bus_master_latency
,
369 (u32
) px
->control
, (u32
) px
->status
));
372 * Check that ACPI's u64 MHz will be valid as u32 KHz in cpufreq
374 if (!px
->core_frequency
||
375 ((u32
)(px
->core_frequency
* 1000) !=
376 (px
->core_frequency
* 1000))) {
377 printk(KERN_ERR FW_BUG PREFIX
378 "Invalid BIOS _PSS frequency: 0x%llx MHz\n",
381 kfree(pr
->performance
->states
);
387 kfree(buffer
.pointer
);
392 static int acpi_processor_get_performance_info(struct acpi_processor
*pr
)
395 acpi_status status
= AE_OK
;
396 acpi_handle handle
= NULL
;
398 if (!pr
|| !pr
->performance
|| !pr
->handle
)
401 status
= acpi_get_handle(pr
->handle
, "_PCT", &handle
);
402 if (ACPI_FAILURE(status
)) {
403 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
404 "ACPI-based processor performance control unavailable\n"));
408 result
= acpi_processor_get_performance_control(pr
);
412 result
= acpi_processor_get_performance_states(pr
);
416 /* We need to call _PPC once when cpufreq starts */
418 result
= acpi_processor_get_platform_limit(pr
);
423 * Having _PPC but missing frequencies (_PSS, _PCT) is a very good hint that
424 * the BIOS is older than the CPU and does not know its frequencies
428 if (ACPI_SUCCESS(acpi_get_handle(pr
->handle
, "_PPC", &handle
))){
429 if(boot_cpu_has(X86_FEATURE_EST
))
430 printk(KERN_WARNING FW_BUG
"BIOS needs update for CPU "
431 "frequency support\n");
437 int acpi_processor_notify_smm(struct module
*calling_module
)
440 static int is_done
= 0;
443 if (!(acpi_processor_ppc_status
& PPC_REGISTERED
))
446 if (!try_module_get(calling_module
))
449 /* is_done is set to negative if an error occured,
450 * and to postitive if _no_ error occured, but SMM
451 * was already notified. This avoids double notification
452 * which might lead to unexpected results...
455 module_put(calling_module
);
457 } else if (is_done
< 0) {
458 module_put(calling_module
);
464 /* Can't write pstate_control to smi_command if either value is zero */
465 if ((!acpi_gbl_FADT
.smi_command
) || (!acpi_gbl_FADT
.pstate_control
)) {
466 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No SMI port or pstate_control\n"));
467 module_put(calling_module
);
471 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
472 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
473 acpi_gbl_FADT
.pstate_control
, acpi_gbl_FADT
.smi_command
));
475 status
= acpi_os_write_port(acpi_gbl_FADT
.smi_command
,
476 (u32
) acpi_gbl_FADT
.pstate_control
, 8);
477 if (ACPI_FAILURE(status
)) {
478 ACPI_EXCEPTION((AE_INFO
, status
,
479 "Failed to write pstate_control [0x%x] to "
480 "smi_command [0x%x]", acpi_gbl_FADT
.pstate_control
,
481 acpi_gbl_FADT
.smi_command
));
482 module_put(calling_module
);
486 /* Success. If there's no _PPC, we need to fear nothing, so
487 * we can allow the cpufreq driver to be rmmod'ed. */
490 if (!(acpi_processor_ppc_status
& PPC_IN_USE
))
491 module_put(calling_module
);
496 EXPORT_SYMBOL(acpi_processor_notify_smm
);
498 static int acpi_processor_get_psd(struct acpi_processor
*pr
)
501 acpi_status status
= AE_OK
;
502 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
503 struct acpi_buffer format
= {sizeof("NNNNN"), "NNNNN"};
504 struct acpi_buffer state
= {0, NULL
};
505 union acpi_object
*psd
= NULL
;
506 struct acpi_psd_package
*pdomain
;
508 status
= acpi_evaluate_object(pr
->handle
, "_PSD", NULL
, &buffer
);
509 if (ACPI_FAILURE(status
)) {
513 psd
= buffer
.pointer
;
514 if (!psd
|| (psd
->type
!= ACPI_TYPE_PACKAGE
)) {
515 printk(KERN_ERR PREFIX
"Invalid _PSD data\n");
520 if (psd
->package
.count
!= 1) {
521 printk(KERN_ERR PREFIX
"Invalid _PSD data\n");
526 pdomain
= &(pr
->performance
->domain_info
);
528 state
.length
= sizeof(struct acpi_psd_package
);
529 state
.pointer
= pdomain
;
531 status
= acpi_extract_package(&(psd
->package
.elements
[0]),
533 if (ACPI_FAILURE(status
)) {
534 printk(KERN_ERR PREFIX
"Invalid _PSD data\n");
539 if (pdomain
->num_entries
!= ACPI_PSD_REV0_ENTRIES
) {
540 printk(KERN_ERR PREFIX
"Unknown _PSD:num_entries\n");
545 if (pdomain
->revision
!= ACPI_PSD_REV0_REVISION
) {
546 printk(KERN_ERR PREFIX
"Unknown _PSD:revision\n");
551 if (pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ALL
&&
552 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ANY
&&
553 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_HW_ALL
) {
554 printk(KERN_ERR PREFIX
"Invalid _PSD:coord_type\n");
559 kfree(buffer
.pointer
);
563 int acpi_processor_preregister_performance(
564 struct acpi_processor_performance __percpu
*performance
)
566 int count
, count_target
;
569 cpumask_var_t covered_cpus
;
570 struct acpi_processor
*pr
;
571 struct acpi_psd_package
*pdomain
;
572 struct acpi_processor
*match_pr
;
573 struct acpi_psd_package
*match_pdomain
;
575 if (!zalloc_cpumask_var(&covered_cpus
, GFP_KERNEL
))
578 mutex_lock(&performance_mutex
);
581 * Check if another driver has already registered, and abort before
582 * changing pr->performance if it has. Check input data as well.
584 for_each_possible_cpu(i
) {
585 pr
= per_cpu(processors
, i
);
587 /* Look only at processors in ACPI namespace */
591 if (pr
->performance
) {
596 if (!performance
|| !per_cpu_ptr(performance
, i
)) {
602 /* Call _PSD for all CPUs */
603 for_each_possible_cpu(i
) {
604 pr
= per_cpu(processors
, i
);
608 pr
->performance
= per_cpu_ptr(performance
, i
);
609 cpumask_set_cpu(i
, pr
->performance
->shared_cpu_map
);
610 if (acpi_processor_get_psd(pr
)) {
619 * Now that we have _PSD data from all CPUs, lets setup P-state
622 for_each_possible_cpu(i
) {
623 pr
= per_cpu(processors
, i
);
627 if (cpumask_test_cpu(i
, covered_cpus
))
630 pdomain
= &(pr
->performance
->domain_info
);
631 cpumask_set_cpu(i
, pr
->performance
->shared_cpu_map
);
632 cpumask_set_cpu(i
, covered_cpus
);
633 if (pdomain
->num_processors
<= 1)
636 /* Validate the Domain info */
637 count_target
= pdomain
->num_processors
;
639 if (pdomain
->coord_type
== DOMAIN_COORD_TYPE_SW_ALL
)
640 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_ALL
;
641 else if (pdomain
->coord_type
== DOMAIN_COORD_TYPE_HW_ALL
)
642 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_HW
;
643 else if (pdomain
->coord_type
== DOMAIN_COORD_TYPE_SW_ANY
)
644 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_ANY
;
646 for_each_possible_cpu(j
) {
650 match_pr
= per_cpu(processors
, j
);
654 match_pdomain
= &(match_pr
->performance
->domain_info
);
655 if (match_pdomain
->domain
!= pdomain
->domain
)
658 /* Here i and j are in the same domain */
660 if (match_pdomain
->num_processors
!= count_target
) {
665 if (pdomain
->coord_type
!= match_pdomain
->coord_type
) {
670 cpumask_set_cpu(j
, covered_cpus
);
671 cpumask_set_cpu(j
, pr
->performance
->shared_cpu_map
);
675 for_each_possible_cpu(j
) {
679 match_pr
= per_cpu(processors
, j
);
683 match_pdomain
= &(match_pr
->performance
->domain_info
);
684 if (match_pdomain
->domain
!= pdomain
->domain
)
687 match_pr
->performance
->shared_type
=
688 pr
->performance
->shared_type
;
689 cpumask_copy(match_pr
->performance
->shared_cpu_map
,
690 pr
->performance
->shared_cpu_map
);
695 for_each_possible_cpu(i
) {
696 pr
= per_cpu(processors
, i
);
697 if (!pr
|| !pr
->performance
)
700 /* Assume no coordination on any error parsing domain info */
702 cpumask_clear(pr
->performance
->shared_cpu_map
);
703 cpumask_set_cpu(i
, pr
->performance
->shared_cpu_map
);
704 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_ALL
;
706 pr
->performance
= NULL
; /* Will be set for real in register */
710 mutex_unlock(&performance_mutex
);
711 free_cpumask_var(covered_cpus
);
714 EXPORT_SYMBOL(acpi_processor_preregister_performance
);
717 acpi_processor_register_performance(struct acpi_processor_performance
718 *performance
, unsigned int cpu
)
720 struct acpi_processor
*pr
;
722 if (!(acpi_processor_ppc_status
& PPC_REGISTERED
))
725 mutex_lock(&performance_mutex
);
727 pr
= per_cpu(processors
, cpu
);
729 mutex_unlock(&performance_mutex
);
733 if (pr
->performance
) {
734 mutex_unlock(&performance_mutex
);
738 WARN_ON(!performance
);
740 pr
->performance
= performance
;
742 if (acpi_processor_get_performance_info(pr
)) {
743 pr
->performance
= NULL
;
744 mutex_unlock(&performance_mutex
);
748 mutex_unlock(&performance_mutex
);
752 EXPORT_SYMBOL(acpi_processor_register_performance
);
755 acpi_processor_unregister_performance(struct acpi_processor_performance
756 *performance
, unsigned int cpu
)
758 struct acpi_processor
*pr
;
760 mutex_lock(&performance_mutex
);
762 pr
= per_cpu(processors
, cpu
);
764 mutex_unlock(&performance_mutex
);
769 kfree(pr
->performance
->states
);
770 pr
->performance
= NULL
;
772 mutex_unlock(&performance_mutex
);
777 EXPORT_SYMBOL(acpi_processor_unregister_performance
);