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);
54 * _PPC support is implemented as a CPUfreq policy notifier:
55 * This means each time a CPUfreq driver registered also with
56 * the ACPI core is asked to change the speed policy, the maximum
57 * value is adjusted so that it is within the platform limit.
59 * Also, when a new platform limit value is detected, the CPUfreq
60 * policy is adjusted accordingly.
63 #define PPC_REGISTERED 1
66 static int acpi_processor_ppc_status = 0;
68 static int acpi_processor_ppc_notifier(struct notifier_block *nb,
69 unsigned long event, void *data)
71 struct cpufreq_policy *policy = data;
72 struct acpi_processor *pr;
75 mutex_lock(&performance_mutex);
77 if (event != CPUFREQ_INCOMPATIBLE)
80 pr = processors[policy->cpu];
81 if (!pr || !pr->performance)
84 ppc = (unsigned int)pr->performance_platform_limit;
86 if (ppc >= pr->performance->state_count)
89 cpufreq_verify_within_limits(policy, 0,
90 pr->performance->states[ppc].
91 core_frequency * 1000);
94 mutex_unlock(&performance_mutex);
99 static struct notifier_block acpi_ppc_notifier_block = {
100 .notifier_call = acpi_processor_ppc_notifier,
103 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
105 acpi_status status = 0;
106 unsigned long ppc = 0;
113 * _PPC indicates the maximum state currently supported by the platform
114 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
116 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
118 if (status != AE_NOT_FOUND)
119 acpi_processor_ppc_status |= PPC_IN_USE;
121 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
122 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
126 pr->performance_platform_limit = (int)ppc;
131 int acpi_processor_ppc_has_changed(struct acpi_processor *pr)
133 int ret = acpi_processor_get_platform_limit(pr);
137 return cpufreq_update_policy(pr->id);
140 void acpi_processor_ppc_init(void)
142 if (!cpufreq_register_notifier
143 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
144 acpi_processor_ppc_status |= PPC_REGISTERED;
147 "Warning: Processor Platform Limit not supported.\n");
150 void acpi_processor_ppc_exit(void)
152 if (acpi_processor_ppc_status & PPC_REGISTERED)
153 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
154 CPUFREQ_POLICY_NOTIFIER);
156 acpi_processor_ppc_status &= ~PPC_REGISTERED;
159 static int acpi_processor_get_performance_control(struct acpi_processor *pr)
162 acpi_status status = 0;
163 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
164 union acpi_object *pct = NULL;
165 union acpi_object obj = { 0 };
168 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
169 if (ACPI_FAILURE(status)) {
170 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
174 pct = (union acpi_object *)buffer.pointer;
175 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
176 || (pct->package.count != 2)) {
177 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
186 obj = pct->package.elements[0];
188 if ((obj.type != ACPI_TYPE_BUFFER)
189 || (obj.buffer.length < sizeof(struct acpi_pct_register))
190 || (obj.buffer.pointer == NULL)) {
191 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
195 memcpy(&pr->performance->control_register, obj.buffer.pointer,
196 sizeof(struct acpi_pct_register));
202 obj = pct->package.elements[1];
204 if ((obj.type != ACPI_TYPE_BUFFER)
205 || (obj.buffer.length < sizeof(struct acpi_pct_register))
206 || (obj.buffer.pointer == NULL)) {
207 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
212 memcpy(&pr->performance->status_register, obj.buffer.pointer,
213 sizeof(struct acpi_pct_register));
216 kfree(buffer.pointer);
221 static int acpi_processor_get_performance_states(struct acpi_processor *pr)
224 acpi_status status = AE_OK;
225 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
226 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
227 struct acpi_buffer state = { 0, NULL };
228 union acpi_object *pss = NULL;
232 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
233 if (ACPI_FAILURE(status)) {
234 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
238 pss = buffer.pointer;
239 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
240 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
245 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
246 pss->package.count));
248 pr->performance->state_count = pss->package.count;
249 pr->performance->states =
250 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
252 if (!pr->performance->states) {
257 for (i = 0; i < pr->performance->state_count; i++) {
259 struct acpi_processor_px *px = &(pr->performance->states[i]);
261 state.length = sizeof(struct acpi_processor_px);
264 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
266 status = acpi_extract_package(&(pss->package.elements[i]),
268 if (ACPI_FAILURE(status)) {
269 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
271 kfree(pr->performance->states);
275 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
276 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
278 (u32) px->core_frequency,
280 (u32) px->transition_latency,
281 (u32) px->bus_master_latency,
282 (u32) px->control, (u32) px->status));
284 if (!px->core_frequency) {
285 printk(KERN_ERR PREFIX
286 "Invalid _PSS data: freq is zero\n");
288 kfree(pr->performance->states);
294 kfree(buffer.pointer);
299 static int acpi_processor_get_performance_info(struct acpi_processor *pr)
302 acpi_status status = AE_OK;
303 acpi_handle handle = NULL;
306 if (!pr || !pr->performance || !pr->handle)
309 status = acpi_get_handle(pr->handle, "_PCT", &handle);
310 if (ACPI_FAILURE(status)) {
311 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
312 "ACPI-based processor performance control unavailable\n"));
316 result = acpi_processor_get_performance_control(pr);
320 result = acpi_processor_get_performance_states(pr);
327 int acpi_processor_notify_smm(struct module *calling_module)
330 static int is_done = 0;
333 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
336 if (!try_module_get(calling_module))
339 /* is_done is set to negative if an error occured,
340 * and to postitive if _no_ error occured, but SMM
341 * was already notified. This avoids double notification
342 * which might lead to unexpected results...
345 module_put(calling_module);
347 } else if (is_done < 0) {
348 module_put(calling_module);
354 /* Can't write pstate_control to smi_command if either value is zero */
355 if ((!acpi_gbl_FADT.smi_command) || (!acpi_gbl_FADT.pstate_control)) {
356 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
357 module_put(calling_module);
361 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
362 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
363 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
365 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
366 (u32) acpi_gbl_FADT.pstate_control, 8);
367 if (ACPI_FAILURE(status)) {
368 ACPI_EXCEPTION((AE_INFO, status,
369 "Failed to write pstate_control [0x%x] to "
370 "smi_command [0x%x]", acpi_gbl_FADT.pstate_control,
371 acpi_gbl_FADT.smi_command));
372 module_put(calling_module);
376 /* Success. If there's no _PPC, we need to fear nothing, so
377 * we can allow the cpufreq driver to be rmmod'ed. */
380 if (!(acpi_processor_ppc_status & PPC_IN_USE))
381 module_put(calling_module);
386 EXPORT_SYMBOL(acpi_processor_notify_smm);
388 #ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
389 /* /proc/acpi/processor/../performance interface (DEPRECATED) */
391 static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file);
392 static struct file_operations acpi_processor_perf_fops = {
393 .open = acpi_processor_perf_open_fs,
396 .release = single_release,
399 static int acpi_processor_perf_seq_show(struct seq_file *seq, void *offset)
401 struct acpi_processor *pr = seq->private;
408 if (!pr->performance) {
409 seq_puts(seq, "<not supported>\n");
413 seq_printf(seq, "state count: %d\n"
414 "active state: P%d\n",
415 pr->performance->state_count, pr->performance->state);
417 seq_puts(seq, "states:\n");
418 for (i = 0; i < pr->performance->state_count; i++)
420 " %cP%d: %d MHz, %d mW, %d uS\n",
421 (i == pr->performance->state ? '*' : ' '), i,
422 (u32) pr->performance->states[i].core_frequency,
423 (u32) pr->performance->states[i].power,
424 (u32) pr->performance->states[i].transition_latency);
430 static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file)
432 return single_open(file, acpi_processor_perf_seq_show,
437 acpi_processor_write_performance(struct file *file,
438 const char __user * buffer,
439 size_t count, loff_t * data)
442 struct seq_file *m = file->private_data;
443 struct acpi_processor *pr = m->private;
444 struct acpi_processor_performance *perf;
445 char state_string[12] = { '\0' };
446 unsigned int new_state = 0;
447 struct cpufreq_policy policy;
450 if (!pr || (count > sizeof(state_string) - 1))
453 perf = pr->performance;
457 if (copy_from_user(state_string, buffer, count))
460 state_string[count] = '\0';
461 new_state = simple_strtoul(state_string, NULL, 0);
463 if (new_state >= perf->state_count)
466 cpufreq_get_policy(&policy, pr->id);
469 policy.min = perf->states[new_state].core_frequency * 1000;
470 policy.max = perf->states[new_state].core_frequency * 1000;
472 result = cpufreq_set_policy(&policy);
479 static void acpi_cpufreq_add_file(struct acpi_processor *pr)
481 struct proc_dir_entry *entry = NULL;
482 struct acpi_device *device = NULL;
485 if (acpi_bus_get_device(pr->handle, &device))
488 /* add file 'performance' [R/W] */
489 entry = create_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
490 S_IFREG | S_IRUGO | S_IWUSR,
491 acpi_device_dir(device));
493 acpi_processor_perf_fops.write = acpi_processor_write_performance;
494 entry->proc_fops = &acpi_processor_perf_fops;
495 entry->data = acpi_driver_data(device);
496 entry->owner = THIS_MODULE;
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 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
549 if (psd->package.count != 1) {
550 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "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 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
568 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
569 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:num_entries\n"));
574 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
575 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "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) {
605 /* Look only at processors in ACPI namespace */
609 if (pr->performance) {
614 if (!performance || !performance[i]) {
619 pr->performance = 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) {
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) {
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 = 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 = 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) {
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 = 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 = 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);