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>
34 #ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
35 #include <linux/proc_fs.h>
36 #include <linux/seq_file.h>
37 #include <linux/mutex.h>
39 #include <asm/uaccess.h>
43 #include <asm/cpufeature.h>
46 #include <acpi/acpi_bus.h>
47 #include <acpi/processor.h>
49 #define ACPI_PROCESSOR_COMPONENT 0x01000000
50 #define ACPI_PROCESSOR_CLASS "processor"
51 #define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
52 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
53 ACPI_MODULE_NAME("processor_perflib");
55 static DEFINE_MUTEX(performance_mutex
);
57 /* Use cpufreq debug layer for _PPC changes. */
58 #define cpufreq_printk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
62 * _PPC support is implemented as a CPUfreq policy notifier:
63 * This means each time a CPUfreq driver registered also with
64 * the ACPI core is asked to change the speed policy, the maximum
65 * value is adjusted so that it is within the platform limit.
67 * Also, when a new platform limit value is detected, the CPUfreq
68 * policy is adjusted accordingly.
72 * -1 -> cpufreq low level drivers not initialized -> _PSS, etc. not called yet
74 * 0 -> cpufreq low level drivers initialized -> consider _PPC values
75 * 1 -> ignore _PPC totally -> forced by user through boot param
77 static int ignore_ppc
= -1;
78 module_param(ignore_ppc
, int, 0644);
79 MODULE_PARM_DESC(ignore_ppc
, "If the frequency of your machine gets wrongly" \
80 "limited by BIOS, this should help");
82 #define PPC_REGISTERED 1
85 static int acpi_processor_ppc_status
;
87 static int acpi_processor_ppc_notifier(struct notifier_block
*nb
,
88 unsigned long event
, void *data
)
90 struct cpufreq_policy
*policy
= data
;
91 struct acpi_processor
*pr
;
94 if (event
== CPUFREQ_START
&& ignore_ppc
<= 0) {
102 if (event
!= CPUFREQ_INCOMPATIBLE
)
105 mutex_lock(&performance_mutex
);
107 pr
= per_cpu(processors
, policy
->cpu
);
108 if (!pr
|| !pr
->performance
)
111 ppc
= (unsigned int)pr
->performance_platform_limit
;
113 if (ppc
>= pr
->performance
->state_count
)
116 cpufreq_verify_within_limits(policy
, 0,
117 pr
->performance
->states
[ppc
].
118 core_frequency
* 1000);
121 mutex_unlock(&performance_mutex
);
126 static struct notifier_block acpi_ppc_notifier_block
= {
127 .notifier_call
= acpi_processor_ppc_notifier
,
130 static int acpi_processor_get_platform_limit(struct acpi_processor
*pr
)
132 acpi_status status
= 0;
133 unsigned long long ppc
= 0;
140 * _PPC indicates the maximum state currently supported by the platform
141 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
143 status
= acpi_evaluate_integer(pr
->handle
, "_PPC", NULL
, &ppc
);
145 if (status
!= AE_NOT_FOUND
)
146 acpi_processor_ppc_status
|= PPC_IN_USE
;
148 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
149 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PPC"));
153 cpufreq_printk("CPU %d: _PPC is %d - frequency %s limited\n", pr
->id
,
154 (int)ppc
, ppc
? "" : "not");
156 pr
->performance_platform_limit
= (int)ppc
;
161 int acpi_processor_ppc_has_changed(struct acpi_processor
*pr
)
168 ret
= acpi_processor_get_platform_limit(pr
);
173 return cpufreq_update_policy(pr
->id
);
176 void acpi_processor_ppc_init(void)
178 if (!cpufreq_register_notifier
179 (&acpi_ppc_notifier_block
, CPUFREQ_POLICY_NOTIFIER
))
180 acpi_processor_ppc_status
|= PPC_REGISTERED
;
183 "Warning: Processor Platform Limit not supported.\n");
186 void acpi_processor_ppc_exit(void)
188 if (acpi_processor_ppc_status
& PPC_REGISTERED
)
189 cpufreq_unregister_notifier(&acpi_ppc_notifier_block
,
190 CPUFREQ_POLICY_NOTIFIER
);
192 acpi_processor_ppc_status
&= ~PPC_REGISTERED
;
195 static int acpi_processor_get_performance_control(struct acpi_processor
*pr
)
198 acpi_status status
= 0;
199 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
200 union acpi_object
*pct
= NULL
;
201 union acpi_object obj
= { 0 };
204 status
= acpi_evaluate_object(pr
->handle
, "_PCT", NULL
, &buffer
);
205 if (ACPI_FAILURE(status
)) {
206 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PCT"));
210 pct
= (union acpi_object
*)buffer
.pointer
;
211 if (!pct
|| (pct
->type
!= ACPI_TYPE_PACKAGE
)
212 || (pct
->package
.count
!= 2)) {
213 printk(KERN_ERR PREFIX
"Invalid _PCT data\n");
222 obj
= pct
->package
.elements
[0];
224 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
225 || (obj
.buffer
.length
< sizeof(struct acpi_pct_register
))
226 || (obj
.buffer
.pointer
== NULL
)) {
227 printk(KERN_ERR PREFIX
"Invalid _PCT data (control_register)\n");
231 memcpy(&pr
->performance
->control_register
, obj
.buffer
.pointer
,
232 sizeof(struct acpi_pct_register
));
238 obj
= pct
->package
.elements
[1];
240 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
241 || (obj
.buffer
.length
< sizeof(struct acpi_pct_register
))
242 || (obj
.buffer
.pointer
== NULL
)) {
243 printk(KERN_ERR PREFIX
"Invalid _PCT data (status_register)\n");
248 memcpy(&pr
->performance
->status_register
, obj
.buffer
.pointer
,
249 sizeof(struct acpi_pct_register
));
252 kfree(buffer
.pointer
);
257 static int acpi_processor_get_performance_states(struct acpi_processor
*pr
)
260 acpi_status status
= AE_OK
;
261 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
262 struct acpi_buffer format
= { sizeof("NNNNNN"), "NNNNNN" };
263 struct acpi_buffer state
= { 0, NULL
};
264 union acpi_object
*pss
= NULL
;
268 status
= acpi_evaluate_object(pr
->handle
, "_PSS", NULL
, &buffer
);
269 if (ACPI_FAILURE(status
)) {
270 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PSS"));
274 pss
= buffer
.pointer
;
275 if (!pss
|| (pss
->type
!= ACPI_TYPE_PACKAGE
)) {
276 printk(KERN_ERR PREFIX
"Invalid _PSS data\n");
281 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d performance states\n",
282 pss
->package
.count
));
284 pr
->performance
->state_count
= pss
->package
.count
;
285 pr
->performance
->states
=
286 kmalloc(sizeof(struct acpi_processor_px
) * pss
->package
.count
,
288 if (!pr
->performance
->states
) {
293 for (i
= 0; i
< pr
->performance
->state_count
; i
++) {
295 struct acpi_processor_px
*px
= &(pr
->performance
->states
[i
]);
297 state
.length
= sizeof(struct acpi_processor_px
);
300 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Extracting state %d\n", i
));
302 status
= acpi_extract_package(&(pss
->package
.elements
[i
]),
304 if (ACPI_FAILURE(status
)) {
305 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _PSS data"));
307 kfree(pr
->performance
->states
);
311 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
312 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
314 (u32
) px
->core_frequency
,
316 (u32
) px
->transition_latency
,
317 (u32
) px
->bus_master_latency
,
318 (u32
) px
->control
, (u32
) px
->status
));
320 if (!px
->core_frequency
) {
321 printk(KERN_ERR PREFIX
322 "Invalid _PSS data: freq is zero\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 #ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
438 /* /proc/acpi/processor/../performance interface (DEPRECATED) */
440 static int acpi_processor_perf_open_fs(struct inode
*inode
, struct file
*file
);
441 static struct file_operations acpi_processor_perf_fops
= {
442 .owner
= THIS_MODULE
,
443 .open
= acpi_processor_perf_open_fs
,
446 .release
= single_release
,
449 static int acpi_processor_perf_seq_show(struct seq_file
*seq
, void *offset
)
451 struct acpi_processor
*pr
= seq
->private;
458 if (!pr
->performance
) {
459 seq_puts(seq
, "<not supported>\n");
463 seq_printf(seq
, "state count: %d\n"
464 "active state: P%d\n",
465 pr
->performance
->state_count
, pr
->performance
->state
);
467 seq_puts(seq
, "states:\n");
468 for (i
= 0; i
< pr
->performance
->state_count
; i
++)
470 " %cP%d: %d MHz, %d mW, %d uS\n",
471 (i
== pr
->performance
->state
? '*' : ' '), i
,
472 (u32
) pr
->performance
->states
[i
].core_frequency
,
473 (u32
) pr
->performance
->states
[i
].power
,
474 (u32
) pr
->performance
->states
[i
].transition_latency
);
480 static int acpi_processor_perf_open_fs(struct inode
*inode
, struct file
*file
)
482 return single_open(file
, acpi_processor_perf_seq_show
,
486 static void acpi_cpufreq_add_file(struct acpi_processor
*pr
)
488 struct acpi_device
*device
= NULL
;
491 if (acpi_bus_get_device(pr
->handle
, &device
))
494 /* add file 'performance' [R/W] */
495 proc_create_data(ACPI_PROCESSOR_FILE_PERFORMANCE
, S_IFREG
| S_IRUGO
,
496 acpi_device_dir(device
),
497 &acpi_processor_perf_fops
, acpi_driver_data(device
));
501 static void acpi_cpufreq_remove_file(struct acpi_processor
*pr
)
503 struct acpi_device
*device
= NULL
;
506 if (acpi_bus_get_device(pr
->handle
, &device
))
509 /* remove file 'performance' */
510 remove_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE
,
511 acpi_device_dir(device
));
517 static void acpi_cpufreq_add_file(struct acpi_processor
*pr
)
521 static void acpi_cpufreq_remove_file(struct acpi_processor
*pr
)
525 #endif /* CONFIG_X86_ACPI_CPUFREQ_PROC_INTF */
527 static int acpi_processor_get_psd(struct acpi_processor
*pr
)
530 acpi_status status
= AE_OK
;
531 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
532 struct acpi_buffer format
= {sizeof("NNNNN"), "NNNNN"};
533 struct acpi_buffer state
= {0, NULL
};
534 union acpi_object
*psd
= NULL
;
535 struct acpi_psd_package
*pdomain
;
537 status
= acpi_evaluate_object(pr
->handle
, "_PSD", NULL
, &buffer
);
538 if (ACPI_FAILURE(status
)) {
542 psd
= buffer
.pointer
;
543 if (!psd
|| (psd
->type
!= ACPI_TYPE_PACKAGE
)) {
544 printk(KERN_ERR PREFIX
"Invalid _PSD data\n");
549 if (psd
->package
.count
!= 1) {
550 printk(KERN_ERR PREFIX
"Invalid _PSD data\n");
555 pdomain
= &(pr
->performance
->domain_info
);
557 state
.length
= sizeof(struct acpi_psd_package
);
558 state
.pointer
= pdomain
;
560 status
= acpi_extract_package(&(psd
->package
.elements
[0]),
562 if (ACPI_FAILURE(status
)) {
563 printk(KERN_ERR PREFIX
"Invalid _PSD data\n");
568 if (pdomain
->num_entries
!= ACPI_PSD_REV0_ENTRIES
) {
569 printk(KERN_ERR PREFIX
"Unknown _PSD:num_entries\n");
574 if (pdomain
->revision
!= ACPI_PSD_REV0_REVISION
) {
575 printk(KERN_ERR PREFIX
"Unknown _PSD:revision\n");
581 kfree(buffer
.pointer
);
585 int acpi_processor_preregister_performance(
586 struct acpi_processor_performance
*performance
)
588 int count
, count_target
;
591 cpumask_t covered_cpus
;
592 struct acpi_processor
*pr
;
593 struct acpi_psd_package
*pdomain
;
594 struct acpi_processor
*match_pr
;
595 struct acpi_psd_package
*match_pdomain
;
597 mutex_lock(&performance_mutex
);
601 /* Call _PSD for all CPUs */
602 for_each_possible_cpu(i
) {
603 pr
= per_cpu(processors
, i
);
605 /* Look only at processors in ACPI namespace */
609 if (pr
->performance
) {
614 if (!performance
|| !percpu_ptr(performance
, i
)) {
619 pr
->performance
= percpu_ptr(performance
, i
);
620 cpu_set(i
, pr
->performance
->shared_cpu_map
);
621 if (acpi_processor_get_psd(pr
)) {
630 * Now that we have _PSD data from all CPUs, lets setup P-state
633 for_each_possible_cpu(i
) {
634 pr
= per_cpu(processors
, i
);
638 /* Basic validity check for domain info */
639 pdomain
= &(pr
->performance
->domain_info
);
640 if ((pdomain
->revision
!= ACPI_PSD_REV0_REVISION
) ||
641 (pdomain
->num_entries
!= ACPI_PSD_REV0_ENTRIES
)) {
645 if (pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ALL
&&
646 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ANY
&&
647 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_HW_ALL
) {
653 cpus_clear(covered_cpus
);
654 for_each_possible_cpu(i
) {
655 pr
= per_cpu(processors
, i
);
659 if (cpu_isset(i
, covered_cpus
))
662 pdomain
= &(pr
->performance
->domain_info
);
663 cpu_set(i
, pr
->performance
->shared_cpu_map
);
664 cpu_set(i
, covered_cpus
);
665 if (pdomain
->num_processors
<= 1)
668 /* Validate the Domain info */
669 count_target
= pdomain
->num_processors
;
671 if (pdomain
->coord_type
== DOMAIN_COORD_TYPE_SW_ALL
)
672 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_ALL
;
673 else if (pdomain
->coord_type
== DOMAIN_COORD_TYPE_HW_ALL
)
674 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_HW
;
675 else if (pdomain
->coord_type
== DOMAIN_COORD_TYPE_SW_ANY
)
676 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_ANY
;
678 for_each_possible_cpu(j
) {
682 match_pr
= per_cpu(processors
, j
);
686 match_pdomain
= &(match_pr
->performance
->domain_info
);
687 if (match_pdomain
->domain
!= pdomain
->domain
)
690 /* Here i and j are in the same domain */
692 if (match_pdomain
->num_processors
!= count_target
) {
697 if (pdomain
->coord_type
!= match_pdomain
->coord_type
) {
702 cpu_set(j
, covered_cpus
);
703 cpu_set(j
, pr
->performance
->shared_cpu_map
);
707 for_each_possible_cpu(j
) {
711 match_pr
= per_cpu(processors
, j
);
715 match_pdomain
= &(match_pr
->performance
->domain_info
);
716 if (match_pdomain
->domain
!= pdomain
->domain
)
719 match_pr
->performance
->shared_type
=
720 pr
->performance
->shared_type
;
721 match_pr
->performance
->shared_cpu_map
=
722 pr
->performance
->shared_cpu_map
;
727 for_each_possible_cpu(i
) {
728 pr
= per_cpu(processors
, i
);
729 if (!pr
|| !pr
->performance
)
732 /* Assume no coordination on any error parsing domain info */
734 cpus_clear(pr
->performance
->shared_cpu_map
);
735 cpu_set(i
, pr
->performance
->shared_cpu_map
);
736 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_ALL
;
738 pr
->performance
= NULL
; /* Will be set for real in register */
741 mutex_unlock(&performance_mutex
);
744 EXPORT_SYMBOL(acpi_processor_preregister_performance
);
748 acpi_processor_register_performance(struct acpi_processor_performance
749 *performance
, unsigned int cpu
)
751 struct acpi_processor
*pr
;
754 if (!(acpi_processor_ppc_status
& PPC_REGISTERED
))
757 mutex_lock(&performance_mutex
);
759 pr
= per_cpu(processors
, cpu
);
761 mutex_unlock(&performance_mutex
);
765 if (pr
->performance
) {
766 mutex_unlock(&performance_mutex
);
770 WARN_ON(!performance
);
772 pr
->performance
= performance
;
774 if (acpi_processor_get_performance_info(pr
)) {
775 pr
->performance
= NULL
;
776 mutex_unlock(&performance_mutex
);
780 acpi_cpufreq_add_file(pr
);
782 mutex_unlock(&performance_mutex
);
786 EXPORT_SYMBOL(acpi_processor_register_performance
);
789 acpi_processor_unregister_performance(struct acpi_processor_performance
790 *performance
, unsigned int cpu
)
792 struct acpi_processor
*pr
;
795 mutex_lock(&performance_mutex
);
797 pr
= per_cpu(processors
, cpu
);
799 mutex_unlock(&performance_mutex
);
804 kfree(pr
->performance
->states
);
805 pr
->performance
= NULL
;
807 acpi_cpufreq_remove_file(pr
);
809 mutex_unlock(&performance_mutex
);
814 EXPORT_SYMBOL(acpi_processor_unregister_performance
);