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>
42 #include <acpi/acpi_bus.h>
43 #include <acpi/processor.h>
45 #define ACPI_PROCESSOR_COMPONENT 0x01000000
46 #define ACPI_PROCESSOR_CLASS "processor"
47 #define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
48 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
49 ACPI_MODULE_NAME("processor_perflib");
51 static DEFINE_MUTEX(performance_mutex
);
53 /* Use cpufreq debug layer for _PPC changes. */
54 #define cpufreq_printk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
58 * _PPC support is implemented as a CPUfreq policy notifier:
59 * This means each time a CPUfreq driver registered also with
60 * the ACPI core is asked to change the speed policy, the maximum
61 * value is adjusted so that it is within the platform limit.
63 * Also, when a new platform limit value is detected, the CPUfreq
64 * policy is adjusted accordingly.
67 static unsigned int ignore_ppc
= 0;
68 module_param(ignore_ppc
, uint
, 0644);
69 MODULE_PARM_DESC(ignore_ppc
, "If the frequency of your machine gets wrongly" \
70 "limited by BIOS, this should help");
72 #define PPC_REGISTERED 1
75 static int acpi_processor_ppc_status
= 0;
77 static int acpi_processor_ppc_notifier(struct notifier_block
*nb
,
78 unsigned long event
, void *data
)
80 struct cpufreq_policy
*policy
= data
;
81 struct acpi_processor
*pr
;
87 mutex_lock(&performance_mutex
);
89 if (event
!= CPUFREQ_INCOMPATIBLE
)
92 pr
= processors
[policy
->cpu
];
93 if (!pr
|| !pr
->performance
)
96 ppc
= (unsigned int)pr
->performance_platform_limit
;
98 if (ppc
>= pr
->performance
->state_count
)
101 cpufreq_verify_within_limits(policy
, 0,
102 pr
->performance
->states
[ppc
].
103 core_frequency
* 1000);
106 mutex_unlock(&performance_mutex
);
111 static struct notifier_block acpi_ppc_notifier_block
= {
112 .notifier_call
= acpi_processor_ppc_notifier
,
115 static int acpi_processor_get_platform_limit(struct acpi_processor
*pr
)
117 acpi_status status
= 0;
118 unsigned long ppc
= 0;
125 * _PPC indicates the maximum state currently supported by the platform
126 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
128 status
= acpi_evaluate_integer(pr
->handle
, "_PPC", NULL
, &ppc
);
130 if (status
!= AE_NOT_FOUND
)
131 acpi_processor_ppc_status
|= PPC_IN_USE
;
133 if (ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
134 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PPC"));
138 cpufreq_printk("CPU %d: _PPC is %d - frequency %s limited\n", pr
->id
,
139 (int)ppc
, ppc
? "" : "not");
141 pr
->performance_platform_limit
= (int)ppc
;
146 int acpi_processor_ppc_has_changed(struct acpi_processor
*pr
)
153 ret
= acpi_processor_get_platform_limit(pr
);
158 return cpufreq_update_policy(pr
->id
);
161 void acpi_processor_ppc_init(void)
163 if (!cpufreq_register_notifier
164 (&acpi_ppc_notifier_block
, CPUFREQ_POLICY_NOTIFIER
))
165 acpi_processor_ppc_status
|= PPC_REGISTERED
;
168 "Warning: Processor Platform Limit not supported.\n");
171 void acpi_processor_ppc_exit(void)
173 if (acpi_processor_ppc_status
& PPC_REGISTERED
)
174 cpufreq_unregister_notifier(&acpi_ppc_notifier_block
,
175 CPUFREQ_POLICY_NOTIFIER
);
177 acpi_processor_ppc_status
&= ~PPC_REGISTERED
;
180 static int acpi_processor_get_performance_control(struct acpi_processor
*pr
)
183 acpi_status status
= 0;
184 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
185 union acpi_object
*pct
= NULL
;
186 union acpi_object obj
= { 0 };
189 status
= acpi_evaluate_object(pr
->handle
, "_PCT", NULL
, &buffer
);
190 if (ACPI_FAILURE(status
)) {
191 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PCT"));
195 pct
= (union acpi_object
*)buffer
.pointer
;
196 if (!pct
|| (pct
->type
!= ACPI_TYPE_PACKAGE
)
197 || (pct
->package
.count
!= 2)) {
198 printk(KERN_ERR PREFIX
"Invalid _PCT data\n");
207 obj
= pct
->package
.elements
[0];
209 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
210 || (obj
.buffer
.length
< sizeof(struct acpi_pct_register
))
211 || (obj
.buffer
.pointer
== NULL
)) {
212 printk(KERN_ERR PREFIX
"Invalid _PCT data (control_register)\n");
216 memcpy(&pr
->performance
->control_register
, obj
.buffer
.pointer
,
217 sizeof(struct acpi_pct_register
));
223 obj
= pct
->package
.elements
[1];
225 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
226 || (obj
.buffer
.length
< sizeof(struct acpi_pct_register
))
227 || (obj
.buffer
.pointer
== NULL
)) {
228 printk(KERN_ERR PREFIX
"Invalid _PCT data (status_register)\n");
233 memcpy(&pr
->performance
->status_register
, obj
.buffer
.pointer
,
234 sizeof(struct acpi_pct_register
));
237 kfree(buffer
.pointer
);
242 static int acpi_processor_get_performance_states(struct acpi_processor
*pr
)
245 acpi_status status
= AE_OK
;
246 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
247 struct acpi_buffer format
= { sizeof("NNNNNN"), "NNNNNN" };
248 struct acpi_buffer state
= { 0, NULL
};
249 union acpi_object
*pss
= NULL
;
253 status
= acpi_evaluate_object(pr
->handle
, "_PSS", NULL
, &buffer
);
254 if (ACPI_FAILURE(status
)) {
255 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PSS"));
259 pss
= buffer
.pointer
;
260 if (!pss
|| (pss
->type
!= ACPI_TYPE_PACKAGE
)) {
261 printk(KERN_ERR PREFIX
"Invalid _PSS data\n");
266 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d performance states\n",
267 pss
->package
.count
));
269 pr
->performance
->state_count
= pss
->package
.count
;
270 pr
->performance
->states
=
271 kmalloc(sizeof(struct acpi_processor_px
) * pss
->package
.count
,
273 if (!pr
->performance
->states
) {
278 for (i
= 0; i
< pr
->performance
->state_count
; i
++) {
280 struct acpi_processor_px
*px
= &(pr
->performance
->states
[i
]);
282 state
.length
= sizeof(struct acpi_processor_px
);
285 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Extracting state %d\n", i
));
287 status
= acpi_extract_package(&(pss
->package
.elements
[i
]),
289 if (ACPI_FAILURE(status
)) {
290 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _PSS data"));
292 kfree(pr
->performance
->states
);
296 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
297 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
299 (u32
) px
->core_frequency
,
301 (u32
) px
->transition_latency
,
302 (u32
) px
->bus_master_latency
,
303 (u32
) px
->control
, (u32
) px
->status
));
305 if (!px
->core_frequency
) {
306 printk(KERN_ERR PREFIX
307 "Invalid _PSS data: freq is zero\n");
309 kfree(pr
->performance
->states
);
315 kfree(buffer
.pointer
);
320 static int acpi_processor_get_performance_info(struct acpi_processor
*pr
)
323 acpi_status status
= AE_OK
;
324 acpi_handle handle
= NULL
;
327 if (!pr
|| !pr
->performance
|| !pr
->handle
)
330 status
= acpi_get_handle(pr
->handle
, "_PCT", &handle
);
331 if (ACPI_FAILURE(status
)) {
332 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
333 "ACPI-based processor performance control unavailable\n"));
337 result
= acpi_processor_get_performance_control(pr
);
341 result
= acpi_processor_get_performance_states(pr
);
348 int acpi_processor_notify_smm(struct module
*calling_module
)
351 static int is_done
= 0;
354 if (!(acpi_processor_ppc_status
& PPC_REGISTERED
))
357 if (!try_module_get(calling_module
))
360 /* is_done is set to negative if an error occured,
361 * and to postitive if _no_ error occured, but SMM
362 * was already notified. This avoids double notification
363 * which might lead to unexpected results...
366 module_put(calling_module
);
368 } else if (is_done
< 0) {
369 module_put(calling_module
);
375 /* Can't write pstate_control to smi_command if either value is zero */
376 if ((!acpi_gbl_FADT
.smi_command
) || (!acpi_gbl_FADT
.pstate_control
)) {
377 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No SMI port or pstate_control\n"));
378 module_put(calling_module
);
382 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
383 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
384 acpi_gbl_FADT
.pstate_control
, acpi_gbl_FADT
.smi_command
));
386 status
= acpi_os_write_port(acpi_gbl_FADT
.smi_command
,
387 (u32
) acpi_gbl_FADT
.pstate_control
, 8);
388 if (ACPI_FAILURE(status
)) {
389 ACPI_EXCEPTION((AE_INFO
, status
,
390 "Failed to write pstate_control [0x%x] to "
391 "smi_command [0x%x]", acpi_gbl_FADT
.pstate_control
,
392 acpi_gbl_FADT
.smi_command
));
393 module_put(calling_module
);
397 /* Success. If there's no _PPC, we need to fear nothing, so
398 * we can allow the cpufreq driver to be rmmod'ed. */
401 if (!(acpi_processor_ppc_status
& PPC_IN_USE
))
402 module_put(calling_module
);
407 EXPORT_SYMBOL(acpi_processor_notify_smm
);
409 #ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
410 /* /proc/acpi/processor/../performance interface (DEPRECATED) */
412 static int acpi_processor_perf_open_fs(struct inode
*inode
, struct file
*file
);
413 static struct file_operations acpi_processor_perf_fops
= {
414 .open
= acpi_processor_perf_open_fs
,
417 .release
= single_release
,
420 static int acpi_processor_perf_seq_show(struct seq_file
*seq
, void *offset
)
422 struct acpi_processor
*pr
= seq
->private;
429 if (!pr
->performance
) {
430 seq_puts(seq
, "<not supported>\n");
434 seq_printf(seq
, "state count: %d\n"
435 "active state: P%d\n",
436 pr
->performance
->state_count
, pr
->performance
->state
);
438 seq_puts(seq
, "states:\n");
439 for (i
= 0; i
< pr
->performance
->state_count
; i
++)
441 " %cP%d: %d MHz, %d mW, %d uS\n",
442 (i
== pr
->performance
->state
? '*' : ' '), i
,
443 (u32
) pr
->performance
->states
[i
].core_frequency
,
444 (u32
) pr
->performance
->states
[i
].power
,
445 (u32
) pr
->performance
->states
[i
].transition_latency
);
451 static int acpi_processor_perf_open_fs(struct inode
*inode
, struct file
*file
)
453 return single_open(file
, acpi_processor_perf_seq_show
,
457 static void acpi_cpufreq_add_file(struct acpi_processor
*pr
)
459 struct proc_dir_entry
*entry
= NULL
;
460 struct acpi_device
*device
= NULL
;
463 if (acpi_bus_get_device(pr
->handle
, &device
))
466 /* add file 'performance' [R/W] */
467 entry
= create_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE
,
469 acpi_device_dir(device
));
471 entry
->proc_fops
= &acpi_processor_perf_fops
;
472 entry
->data
= acpi_driver_data(device
);
473 entry
->owner
= THIS_MODULE
;
478 static void acpi_cpufreq_remove_file(struct acpi_processor
*pr
)
480 struct acpi_device
*device
= NULL
;
483 if (acpi_bus_get_device(pr
->handle
, &device
))
486 /* remove file 'performance' */
487 remove_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE
,
488 acpi_device_dir(device
));
494 static void acpi_cpufreq_add_file(struct acpi_processor
*pr
)
498 static void acpi_cpufreq_remove_file(struct acpi_processor
*pr
)
502 #endif /* CONFIG_X86_ACPI_CPUFREQ_PROC_INTF */
504 static int acpi_processor_get_psd(struct acpi_processor
*pr
)
507 acpi_status status
= AE_OK
;
508 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
509 struct acpi_buffer format
= {sizeof("NNNNN"), "NNNNN"};
510 struct acpi_buffer state
= {0, NULL
};
511 union acpi_object
*psd
= NULL
;
512 struct acpi_psd_package
*pdomain
;
514 status
= acpi_evaluate_object(pr
->handle
, "_PSD", NULL
, &buffer
);
515 if (ACPI_FAILURE(status
)) {
519 psd
= buffer
.pointer
;
520 if (!psd
|| (psd
->type
!= ACPI_TYPE_PACKAGE
)) {
521 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid _PSD data\n"));
526 if (psd
->package
.count
!= 1) {
527 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid _PSD data\n"));
532 pdomain
= &(pr
->performance
->domain_info
);
534 state
.length
= sizeof(struct acpi_psd_package
);
535 state
.pointer
= pdomain
;
537 status
= acpi_extract_package(&(psd
->package
.elements
[0]),
539 if (ACPI_FAILURE(status
)) {
540 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid _PSD data\n"));
545 if (pdomain
->num_entries
!= ACPI_PSD_REV0_ENTRIES
) {
546 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Unknown _PSD:num_entries\n"));
551 if (pdomain
->revision
!= ACPI_PSD_REV0_REVISION
) {
552 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Unknown _PSD:revision\n"));
558 kfree(buffer
.pointer
);
562 int acpi_processor_preregister_performance(
563 struct acpi_processor_performance
*performance
)
565 int count
, count_target
;
568 cpumask_t covered_cpus
;
569 struct acpi_processor
*pr
;
570 struct acpi_psd_package
*pdomain
;
571 struct acpi_processor
*match_pr
;
572 struct acpi_psd_package
*match_pdomain
;
574 mutex_lock(&performance_mutex
);
578 /* Call _PSD for all CPUs */
579 for_each_possible_cpu(i
) {
582 /* Look only at processors in ACPI namespace */
586 if (pr
->performance
) {
591 if (!performance
|| !percpu_ptr(performance
, i
)) {
596 pr
->performance
= percpu_ptr(performance
, i
);
597 cpu_set(i
, pr
->performance
->shared_cpu_map
);
598 if (acpi_processor_get_psd(pr
)) {
607 * Now that we have _PSD data from all CPUs, lets setup P-state
610 for_each_possible_cpu(i
) {
615 /* Basic validity check for domain info */
616 pdomain
= &(pr
->performance
->domain_info
);
617 if ((pdomain
->revision
!= ACPI_PSD_REV0_REVISION
) ||
618 (pdomain
->num_entries
!= ACPI_PSD_REV0_ENTRIES
)) {
622 if (pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ALL
&&
623 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ANY
&&
624 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_HW_ALL
) {
630 cpus_clear(covered_cpus
);
631 for_each_possible_cpu(i
) {
636 if (cpu_isset(i
, covered_cpus
))
639 pdomain
= &(pr
->performance
->domain_info
);
640 cpu_set(i
, pr
->performance
->shared_cpu_map
);
641 cpu_set(i
, covered_cpus
);
642 if (pdomain
->num_processors
<= 1)
645 /* Validate the Domain info */
646 count_target
= pdomain
->num_processors
;
648 if (pdomain
->coord_type
== DOMAIN_COORD_TYPE_SW_ALL
)
649 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_ALL
;
650 else if (pdomain
->coord_type
== DOMAIN_COORD_TYPE_HW_ALL
)
651 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_HW
;
652 else if (pdomain
->coord_type
== DOMAIN_COORD_TYPE_SW_ANY
)
653 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_ANY
;
655 for_each_possible_cpu(j
) {
659 match_pr
= processors
[j
];
663 match_pdomain
= &(match_pr
->performance
->domain_info
);
664 if (match_pdomain
->domain
!= pdomain
->domain
)
667 /* Here i and j are in the same domain */
669 if (match_pdomain
->num_processors
!= count_target
) {
674 if (pdomain
->coord_type
!= match_pdomain
->coord_type
) {
679 cpu_set(j
, covered_cpus
);
680 cpu_set(j
, pr
->performance
->shared_cpu_map
);
684 for_each_possible_cpu(j
) {
688 match_pr
= processors
[j
];
692 match_pdomain
= &(match_pr
->performance
->domain_info
);
693 if (match_pdomain
->domain
!= pdomain
->domain
)
696 match_pr
->performance
->shared_type
=
697 pr
->performance
->shared_type
;
698 match_pr
->performance
->shared_cpu_map
=
699 pr
->performance
->shared_cpu_map
;
704 for_each_possible_cpu(i
) {
706 if (!pr
|| !pr
->performance
)
709 /* Assume no coordination on any error parsing domain info */
711 cpus_clear(pr
->performance
->shared_cpu_map
);
712 cpu_set(i
, pr
->performance
->shared_cpu_map
);
713 pr
->performance
->shared_type
= CPUFREQ_SHARED_TYPE_ALL
;
715 pr
->performance
= NULL
; /* Will be set for real in register */
718 mutex_unlock(&performance_mutex
);
721 EXPORT_SYMBOL(acpi_processor_preregister_performance
);
725 acpi_processor_register_performance(struct acpi_processor_performance
726 *performance
, unsigned int cpu
)
728 struct acpi_processor
*pr
;
731 if (!(acpi_processor_ppc_status
& PPC_REGISTERED
))
734 mutex_lock(&performance_mutex
);
736 pr
= processors
[cpu
];
738 mutex_unlock(&performance_mutex
);
742 if (pr
->performance
) {
743 mutex_unlock(&performance_mutex
);
747 WARN_ON(!performance
);
749 pr
->performance
= performance
;
751 if (acpi_processor_get_performance_info(pr
)) {
752 pr
->performance
= NULL
;
753 mutex_unlock(&performance_mutex
);
757 acpi_cpufreq_add_file(pr
);
759 mutex_unlock(&performance_mutex
);
763 EXPORT_SYMBOL(acpi_processor_register_performance
);
766 acpi_processor_unregister_performance(struct acpi_processor_performance
767 *performance
, unsigned int cpu
)
769 struct acpi_processor
*pr
;
772 mutex_lock(&performance_mutex
);
774 pr
= processors
[cpu
];
776 mutex_unlock(&performance_mutex
);
781 kfree(pr
->performance
->states
);
782 pr
->performance
= NULL
;
784 acpi_cpufreq_remove_file(pr
);
786 mutex_unlock(&performance_mutex
);
791 EXPORT_SYMBOL(acpi_processor_unregister_performance
);