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_DRIVER_NAME "ACPI Processor Driver"
48 #define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
49 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
50 ACPI_MODULE_NAME("acpi_processor")
52 static DEFINE_MUTEX(performance_mutex);
55 * _PPC support is implemented as a CPUfreq policy notifier:
56 * This means each time a CPUfreq driver registered also with
57 * the ACPI core is asked to change the speed policy, the maximum
58 * value is adjusted so that it is within the platform limit.
60 * Also, when a new platform limit value is detected, the CPUfreq
61 * policy is adjusted accordingly.
64 #define PPC_REGISTERED 1
67 static int acpi_processor_ppc_status = 0;
69 static int acpi_processor_ppc_notifier(struct notifier_block *nb,
70 unsigned long event, void *data)
72 struct cpufreq_policy *policy = data;
73 struct acpi_processor *pr;
76 mutex_lock(&performance_mutex);
78 if (event != CPUFREQ_INCOMPATIBLE)
81 pr = processors[policy->cpu];
82 if (!pr || !pr->performance)
85 ppc = (unsigned int)pr->performance_platform_limit;
87 if (ppc >= pr->performance->state_count)
90 cpufreq_verify_within_limits(policy, 0,
91 pr->performance->states[ppc].
92 core_frequency * 1000);
95 mutex_unlock(&performance_mutex);
100 static struct notifier_block acpi_ppc_notifier_block = {
101 .notifier_call = acpi_processor_ppc_notifier,
104 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
106 acpi_status status = 0;
107 unsigned long ppc = 0;
114 * _PPC indicates the maximum state currently supported by the platform
115 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
117 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
119 if (status != AE_NOT_FOUND)
120 acpi_processor_ppc_status |= PPC_IN_USE;
122 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
123 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
127 pr->performance_platform_limit = (int)ppc;
132 int acpi_processor_ppc_has_changed(struct acpi_processor *pr)
134 int ret = acpi_processor_get_platform_limit(pr);
138 return cpufreq_update_policy(pr->id);
141 void acpi_processor_ppc_init(void)
143 if (!cpufreq_register_notifier
144 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
145 acpi_processor_ppc_status |= PPC_REGISTERED;
148 "Warning: Processor Platform Limit not supported.\n");
151 void acpi_processor_ppc_exit(void)
153 if (acpi_processor_ppc_status & PPC_REGISTERED)
154 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
155 CPUFREQ_POLICY_NOTIFIER);
157 acpi_processor_ppc_status &= ~PPC_REGISTERED;
160 static int acpi_processor_get_performance_control(struct acpi_processor *pr)
163 acpi_status status = 0;
164 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
165 union acpi_object *pct = NULL;
166 union acpi_object obj = { 0 };
169 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
170 if (ACPI_FAILURE(status)) {
171 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
175 pct = (union acpi_object *)buffer.pointer;
176 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
177 || (pct->package.count != 2)) {
178 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
187 obj = pct->package.elements[0];
189 if ((obj.type != ACPI_TYPE_BUFFER)
190 || (obj.buffer.length < sizeof(struct acpi_pct_register))
191 || (obj.buffer.pointer == NULL)) {
192 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
196 memcpy(&pr->performance->control_register, obj.buffer.pointer,
197 sizeof(struct acpi_pct_register));
203 obj = pct->package.elements[1];
205 if ((obj.type != ACPI_TYPE_BUFFER)
206 || (obj.buffer.length < sizeof(struct acpi_pct_register))
207 || (obj.buffer.pointer == NULL)) {
208 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
213 memcpy(&pr->performance->status_register, obj.buffer.pointer,
214 sizeof(struct acpi_pct_register));
217 kfree(buffer.pointer);
222 static int acpi_processor_get_performance_states(struct acpi_processor *pr)
225 acpi_status status = AE_OK;
226 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
227 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
228 struct acpi_buffer state = { 0, NULL };
229 union acpi_object *pss = NULL;
233 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
234 if (ACPI_FAILURE(status)) {
235 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
239 pss = (union acpi_object *)buffer.pointer;
240 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
241 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
246 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
247 pss->package.count));
249 pr->performance->state_count = pss->package.count;
250 pr->performance->states =
251 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
253 if (!pr->performance->states) {
258 for (i = 0; i < pr->performance->state_count; i++) {
260 struct acpi_processor_px *px = &(pr->performance->states[i]);
262 state.length = sizeof(struct acpi_processor_px);
265 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
267 status = acpi_extract_package(&(pss->package.elements[i]),
269 if (ACPI_FAILURE(status)) {
270 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
272 kfree(pr->performance->states);
276 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
277 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
279 (u32) px->core_frequency,
281 (u32) px->transition_latency,
282 (u32) px->bus_master_latency,
283 (u32) px->control, (u32) px->status));
285 if (!px->core_frequency) {
286 printk(KERN_ERR PREFIX
287 "Invalid _PSS data: freq is zero\n");
289 kfree(pr->performance->states);
295 kfree(buffer.pointer);
300 static int acpi_processor_get_performance_info(struct acpi_processor *pr)
303 acpi_status status = AE_OK;
304 acpi_handle handle = NULL;
307 if (!pr || !pr->performance || !pr->handle)
310 status = acpi_get_handle(pr->handle, "_PCT", &handle);
311 if (ACPI_FAILURE(status)) {
312 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
313 "ACPI-based processor performance control unavailable\n"));
317 result = acpi_processor_get_performance_control(pr);
321 result = acpi_processor_get_performance_states(pr);
325 result = acpi_processor_get_platform_limit(pr);
332 int acpi_processor_notify_smm(struct module *calling_module)
335 static int is_done = 0;
338 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
341 if (!try_module_get(calling_module))
344 /* is_done is set to negative if an error occured,
345 * and to postitive if _no_ error occured, but SMM
346 * was already notified. This avoids double notification
347 * which might lead to unexpected results...
350 module_put(calling_module);
352 } else if (is_done < 0) {
353 module_put(calling_module);
359 /* Can't write pstate_cnt to smi_cmd if either value is zero */
360 if ((!acpi_fadt.smi_cmd) || (!acpi_fadt.pstate_cnt)) {
361 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_cnt\n"));
362 module_put(calling_module);
366 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
367 "Writing pstate_cnt [0x%x] to smi_cmd [0x%x]\n",
368 acpi_fadt.pstate_cnt, acpi_fadt.smi_cmd));
370 /* FADT v1 doesn't support pstate_cnt, many BIOS vendors use
371 * it anyway, so we need to support it... */
372 if (acpi_fadt_is_v1) {
373 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
374 "Using v1.0 FADT reserved value for pstate_cnt\n"));
377 status = acpi_os_write_port(acpi_fadt.smi_cmd,
378 (u32) acpi_fadt.pstate_cnt, 8);
379 if (ACPI_FAILURE(status)) {
380 ACPI_EXCEPTION((AE_INFO, status,
381 "Failed to write pstate_cnt [0x%x] to "
382 "smi_cmd [0x%x]", acpi_fadt.pstate_cnt,
384 module_put(calling_module);
388 /* Success. If there's no _PPC, we need to fear nothing, so
389 * we can allow the cpufreq driver to be rmmod'ed. */
392 if (!(acpi_processor_ppc_status & PPC_IN_USE))
393 module_put(calling_module);
398 EXPORT_SYMBOL(acpi_processor_notify_smm);
400 #ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
401 /* /proc/acpi/processor/../performance interface (DEPRECATED) */
403 static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file);
404 static struct file_operations acpi_processor_perf_fops = {
405 .open = acpi_processor_perf_open_fs,
408 .release = single_release,
411 static int acpi_processor_perf_seq_show(struct seq_file *seq, void *offset)
413 struct acpi_processor *pr = (struct acpi_processor *)seq->private;
420 if (!pr->performance) {
421 seq_puts(seq, "<not supported>\n");
425 seq_printf(seq, "state count: %d\n"
426 "active state: P%d\n",
427 pr->performance->state_count, pr->performance->state);
429 seq_puts(seq, "states:\n");
430 for (i = 0; i < pr->performance->state_count; i++)
432 " %cP%d: %d MHz, %d mW, %d uS\n",
433 (i == pr->performance->state ? '*' : ' '), i,
434 (u32) pr->performance->states[i].core_frequency,
435 (u32) pr->performance->states[i].power,
436 (u32) pr->performance->states[i].transition_latency);
442 static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file)
444 return single_open(file, acpi_processor_perf_seq_show,
449 acpi_processor_write_performance(struct file *file,
450 const char __user * buffer,
451 size_t count, loff_t * data)
454 struct seq_file *m = (struct seq_file *)file->private_data;
455 struct acpi_processor *pr = (struct acpi_processor *)m->private;
456 struct acpi_processor_performance *perf;
457 char state_string[12] = { '\0' };
458 unsigned int new_state = 0;
459 struct cpufreq_policy policy;
462 if (!pr || (count > sizeof(state_string) - 1))
465 perf = pr->performance;
469 if (copy_from_user(state_string, buffer, count))
472 state_string[count] = '\0';
473 new_state = simple_strtoul(state_string, NULL, 0);
475 if (new_state >= perf->state_count)
478 cpufreq_get_policy(&policy, pr->id);
481 policy.min = perf->states[new_state].core_frequency * 1000;
482 policy.max = perf->states[new_state].core_frequency * 1000;
484 result = cpufreq_set_policy(&policy);
491 static void acpi_cpufreq_add_file(struct acpi_processor *pr)
493 struct proc_dir_entry *entry = NULL;
494 struct acpi_device *device = NULL;
497 if (acpi_bus_get_device(pr->handle, &device))
500 /* add file 'performance' [R/W] */
501 entry = create_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
502 S_IFREG | S_IRUGO | S_IWUSR,
503 acpi_device_dir(device));
505 acpi_processor_perf_fops.write = acpi_processor_write_performance;
506 entry->proc_fops = &acpi_processor_perf_fops;
507 entry->data = acpi_driver_data(device);
508 entry->owner = THIS_MODULE;
513 static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
515 struct acpi_device *device = NULL;
518 if (acpi_bus_get_device(pr->handle, &device))
521 /* remove file 'performance' */
522 remove_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
523 acpi_device_dir(device));
529 static void acpi_cpufreq_add_file(struct acpi_processor *pr)
533 static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
537 #endif /* CONFIG_X86_ACPI_CPUFREQ_PROC_INTF */
539 static int acpi_processor_get_psd(struct acpi_processor *pr)
542 acpi_status status = AE_OK;
543 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
544 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
545 struct acpi_buffer state = {0, NULL};
546 union acpi_object *psd = NULL;
547 struct acpi_psd_package *pdomain;
549 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
550 if (ACPI_FAILURE(status)) {
554 psd = (union acpi_object *) buffer.pointer;
555 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
556 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
561 if (psd->package.count != 1) {
562 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
567 pdomain = &(pr->performance->domain_info);
569 state.length = sizeof(struct acpi_psd_package);
570 state.pointer = pdomain;
572 status = acpi_extract_package(&(psd->package.elements[0]),
574 if (ACPI_FAILURE(status)) {
575 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
580 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
581 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:num_entries\n"));
586 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
587 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:revision\n"));
593 kfree(buffer.pointer);
597 int acpi_processor_preregister_performance(
598 struct acpi_processor_performance **performance)
600 int count, count_target;
603 cpumask_t covered_cpus;
604 struct acpi_processor *pr;
605 struct acpi_psd_package *pdomain;
606 struct acpi_processor *match_pr;
607 struct acpi_psd_package *match_pdomain;
609 mutex_lock(&performance_mutex);
613 /* Call _PSD for all CPUs */
614 for_each_possible_cpu(i) {
617 /* Look only at processors in ACPI namespace */
621 if (pr->performance) {
626 if (!performance || !performance[i]) {
631 pr->performance = performance[i];
632 cpu_set(i, pr->performance->shared_cpu_map);
633 if (acpi_processor_get_psd(pr)) {
642 * Now that we have _PSD data from all CPUs, lets setup P-state
645 for_each_possible_cpu(i) {
650 /* Basic validity check for domain info */
651 pdomain = &(pr->performance->domain_info);
652 if ((pdomain->revision != ACPI_PSD_REV0_REVISION) ||
653 (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES)) {
657 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
658 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
659 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
665 cpus_clear(covered_cpus);
666 for_each_possible_cpu(i) {
671 if (cpu_isset(i, covered_cpus))
674 pdomain = &(pr->performance->domain_info);
675 cpu_set(i, pr->performance->shared_cpu_map);
676 cpu_set(i, covered_cpus);
677 if (pdomain->num_processors <= 1)
680 /* Validate the Domain info */
681 count_target = pdomain->num_processors;
683 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
684 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
685 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
686 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
687 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
688 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
690 for_each_possible_cpu(j) {
694 match_pr = processors[j];
698 match_pdomain = &(match_pr->performance->domain_info);
699 if (match_pdomain->domain != pdomain->domain)
702 /* Here i and j are in the same domain */
704 if (match_pdomain->num_processors != count_target) {
709 if (pdomain->coord_type != match_pdomain->coord_type) {
714 cpu_set(j, covered_cpus);
715 cpu_set(j, pr->performance->shared_cpu_map);
719 for_each_possible_cpu(j) {
723 match_pr = processors[j];
727 match_pdomain = &(match_pr->performance->domain_info);
728 if (match_pdomain->domain != pdomain->domain)
731 match_pr->performance->shared_type =
732 pr->performance->shared_type;
733 match_pr->performance->shared_cpu_map =
734 pr->performance->shared_cpu_map;
740 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error while parsing _PSD domain information. Assuming no coordination\n"));
743 for_each_possible_cpu(i) {
745 if (!pr || !pr->performance)
748 /* Assume no coordination on any error parsing domain info */
750 cpus_clear(pr->performance->shared_cpu_map);
751 cpu_set(i, pr->performance->shared_cpu_map);
752 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
754 pr->performance = NULL; /* Will be set for real in register */
757 mutex_unlock(&performance_mutex);
760 EXPORT_SYMBOL(acpi_processor_preregister_performance);
764 acpi_processor_register_performance(struct acpi_processor_performance
765 *performance, unsigned int cpu)
767 struct acpi_processor *pr;
770 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
773 mutex_lock(&performance_mutex);
775 pr = processors[cpu];
777 mutex_unlock(&performance_mutex);
781 if (pr->performance) {
782 mutex_unlock(&performance_mutex);
786 WARN_ON(!performance);
788 pr->performance = performance;
790 if (acpi_processor_get_performance_info(pr)) {
791 pr->performance = NULL;
792 mutex_unlock(&performance_mutex);
796 acpi_cpufreq_add_file(pr);
798 mutex_unlock(&performance_mutex);
802 EXPORT_SYMBOL(acpi_processor_register_performance);
805 acpi_processor_unregister_performance(struct acpi_processor_performance
806 *performance, unsigned int cpu)
808 struct acpi_processor *pr;
811 mutex_lock(&performance_mutex);
813 pr = processors[cpu];
815 mutex_unlock(&performance_mutex);
820 kfree(pr->performance->states);
821 pr->performance = NULL;
823 acpi_cpufreq_remove_file(pr);
825 mutex_unlock(&performance_mutex);
830 EXPORT_SYMBOL(acpi_processor_unregister_performance);