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 static unsigned int ignore_ppc = 0;
64 module_param(ignore_ppc, uint, 0644);
65 MODULE_PARM_DESC(ignore_ppc, "If the frequency of your machine gets wrongly" \
66 "limited by BIOS, this should help");
68 #define PPC_REGISTERED 1
71 static int acpi_processor_ppc_status = 0;
73 static int acpi_processor_ppc_notifier(struct notifier_block *nb,
74 unsigned long event, void *data)
76 struct cpufreq_policy *policy = data;
77 struct acpi_processor *pr;
83 mutex_lock(&performance_mutex);
85 if (event != CPUFREQ_INCOMPATIBLE)
88 pr = processors[policy->cpu];
89 if (!pr || !pr->performance)
92 ppc = (unsigned int)pr->performance_platform_limit;
94 if (ppc >= pr->performance->state_count)
97 cpufreq_verify_within_limits(policy, 0,
98 pr->performance->states[ppc].
99 core_frequency * 1000);
102 mutex_unlock(&performance_mutex);
107 static struct notifier_block acpi_ppc_notifier_block = {
108 .notifier_call = acpi_processor_ppc_notifier,
111 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
113 acpi_status status = 0;
114 unsigned long ppc = 0;
121 * _PPC indicates the maximum state currently supported by the platform
122 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
124 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
126 if (status != AE_NOT_FOUND)
127 acpi_processor_ppc_status |= PPC_IN_USE;
129 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
130 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
134 pr->performance_platform_limit = (int)ppc;
139 int acpi_processor_ppc_has_changed(struct acpi_processor *pr)
146 ret = acpi_processor_get_platform_limit(pr);
151 return cpufreq_update_policy(pr->id);
154 void acpi_processor_ppc_init(void)
156 if (!cpufreq_register_notifier
157 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
158 acpi_processor_ppc_status |= PPC_REGISTERED;
161 "Warning: Processor Platform Limit not supported.\n");
164 void acpi_processor_ppc_exit(void)
166 if (acpi_processor_ppc_status & PPC_REGISTERED)
167 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
168 CPUFREQ_POLICY_NOTIFIER);
170 acpi_processor_ppc_status &= ~PPC_REGISTERED;
173 static int acpi_processor_get_performance_control(struct acpi_processor *pr)
176 acpi_status status = 0;
177 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
178 union acpi_object *pct = NULL;
179 union acpi_object obj = { 0 };
182 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
183 if (ACPI_FAILURE(status)) {
184 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
188 pct = (union acpi_object *)buffer.pointer;
189 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
190 || (pct->package.count != 2)) {
191 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
200 obj = pct->package.elements[0];
202 if ((obj.type != ACPI_TYPE_BUFFER)
203 || (obj.buffer.length < sizeof(struct acpi_pct_register))
204 || (obj.buffer.pointer == NULL)) {
205 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
209 memcpy(&pr->performance->control_register, obj.buffer.pointer,
210 sizeof(struct acpi_pct_register));
216 obj = pct->package.elements[1];
218 if ((obj.type != ACPI_TYPE_BUFFER)
219 || (obj.buffer.length < sizeof(struct acpi_pct_register))
220 || (obj.buffer.pointer == NULL)) {
221 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
226 memcpy(&pr->performance->status_register, obj.buffer.pointer,
227 sizeof(struct acpi_pct_register));
230 kfree(buffer.pointer);
235 static int acpi_processor_get_performance_states(struct acpi_processor *pr)
238 acpi_status status = AE_OK;
239 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
240 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
241 struct acpi_buffer state = { 0, NULL };
242 union acpi_object *pss = NULL;
246 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
247 if (ACPI_FAILURE(status)) {
248 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
252 pss = buffer.pointer;
253 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
254 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
259 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
260 pss->package.count));
262 pr->performance->state_count = pss->package.count;
263 pr->performance->states =
264 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
266 if (!pr->performance->states) {
271 for (i = 0; i < pr->performance->state_count; i++) {
273 struct acpi_processor_px *px = &(pr->performance->states[i]);
275 state.length = sizeof(struct acpi_processor_px);
278 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
280 status = acpi_extract_package(&(pss->package.elements[i]),
282 if (ACPI_FAILURE(status)) {
283 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
285 kfree(pr->performance->states);
289 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
290 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
292 (u32) px->core_frequency,
294 (u32) px->transition_latency,
295 (u32) px->bus_master_latency,
296 (u32) px->control, (u32) px->status));
298 if (!px->core_frequency) {
299 printk(KERN_ERR PREFIX
300 "Invalid _PSS data: freq is zero\n");
302 kfree(pr->performance->states);
308 kfree(buffer.pointer);
313 static int acpi_processor_get_performance_info(struct acpi_processor *pr)
316 acpi_status status = AE_OK;
317 acpi_handle handle = NULL;
320 if (!pr || !pr->performance || !pr->handle)
323 status = acpi_get_handle(pr->handle, "_PCT", &handle);
324 if (ACPI_FAILURE(status)) {
325 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
326 "ACPI-based processor performance control unavailable\n"));
330 result = acpi_processor_get_performance_control(pr);
334 result = acpi_processor_get_performance_states(pr);
341 int acpi_processor_notify_smm(struct module *calling_module)
344 static int is_done = 0;
347 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
350 if (!try_module_get(calling_module))
353 /* is_done is set to negative if an error occured,
354 * and to postitive if _no_ error occured, but SMM
355 * was already notified. This avoids double notification
356 * which might lead to unexpected results...
359 module_put(calling_module);
361 } else if (is_done < 0) {
362 module_put(calling_module);
368 /* Can't write pstate_control to smi_command if either value is zero */
369 if ((!acpi_gbl_FADT.smi_command) || (!acpi_gbl_FADT.pstate_control)) {
370 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
371 module_put(calling_module);
375 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
376 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
377 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
379 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
380 (u32) acpi_gbl_FADT.pstate_control, 8);
381 if (ACPI_FAILURE(status)) {
382 ACPI_EXCEPTION((AE_INFO, status,
383 "Failed to write pstate_control [0x%x] to "
384 "smi_command [0x%x]", acpi_gbl_FADT.pstate_control,
385 acpi_gbl_FADT.smi_command));
386 module_put(calling_module);
390 /* Success. If there's no _PPC, we need to fear nothing, so
391 * we can allow the cpufreq driver to be rmmod'ed. */
394 if (!(acpi_processor_ppc_status & PPC_IN_USE))
395 module_put(calling_module);
400 EXPORT_SYMBOL(acpi_processor_notify_smm);
402 #ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
403 /* /proc/acpi/processor/../performance interface (DEPRECATED) */
405 static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file);
406 static struct file_operations acpi_processor_perf_fops = {
407 .open = acpi_processor_perf_open_fs,
410 .release = single_release,
413 static int acpi_processor_perf_seq_show(struct seq_file *seq, void *offset)
415 struct acpi_processor *pr = seq->private;
422 if (!pr->performance) {
423 seq_puts(seq, "<not supported>\n");
427 seq_printf(seq, "state count: %d\n"
428 "active state: P%d\n",
429 pr->performance->state_count, pr->performance->state);
431 seq_puts(seq, "states:\n");
432 for (i = 0; i < pr->performance->state_count; i++)
434 " %cP%d: %d MHz, %d mW, %d uS\n",
435 (i == pr->performance->state ? '*' : ' '), i,
436 (u32) pr->performance->states[i].core_frequency,
437 (u32) pr->performance->states[i].power,
438 (u32) pr->performance->states[i].transition_latency);
444 static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file)
446 return single_open(file, acpi_processor_perf_seq_show,
450 static void acpi_cpufreq_add_file(struct acpi_processor *pr)
452 struct proc_dir_entry *entry = NULL;
453 struct acpi_device *device = NULL;
456 if (acpi_bus_get_device(pr->handle, &device))
459 /* add file 'performance' [R/W] */
460 entry = create_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
462 acpi_device_dir(device));
464 entry->proc_fops = &acpi_processor_perf_fops;
465 entry->data = acpi_driver_data(device);
466 entry->owner = THIS_MODULE;
471 static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
473 struct acpi_device *device = NULL;
476 if (acpi_bus_get_device(pr->handle, &device))
479 /* remove file 'performance' */
480 remove_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
481 acpi_device_dir(device));
487 static void acpi_cpufreq_add_file(struct acpi_processor *pr)
491 static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
495 #endif /* CONFIG_X86_ACPI_CPUFREQ_PROC_INTF */
497 static int acpi_processor_get_psd(struct acpi_processor *pr)
500 acpi_status status = AE_OK;
501 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
502 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
503 struct acpi_buffer state = {0, NULL};
504 union acpi_object *psd = NULL;
505 struct acpi_psd_package *pdomain;
507 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
508 if (ACPI_FAILURE(status)) {
512 psd = buffer.pointer;
513 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
514 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
519 if (psd->package.count != 1) {
520 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
525 pdomain = &(pr->performance->domain_info);
527 state.length = sizeof(struct acpi_psd_package);
528 state.pointer = pdomain;
530 status = acpi_extract_package(&(psd->package.elements[0]),
532 if (ACPI_FAILURE(status)) {
533 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
538 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
539 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:num_entries\n"));
544 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
545 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:revision\n"));
551 kfree(buffer.pointer);
555 int acpi_processor_preregister_performance(
556 struct acpi_processor_performance *performance)
558 int count, count_target;
561 cpumask_t covered_cpus;
562 struct acpi_processor *pr;
563 struct acpi_psd_package *pdomain;
564 struct acpi_processor *match_pr;
565 struct acpi_psd_package *match_pdomain;
567 mutex_lock(&performance_mutex);
571 /* Call _PSD for all CPUs */
572 for_each_possible_cpu(i) {
575 /* Look only at processors in ACPI namespace */
579 if (pr->performance) {
584 if (!performance || !percpu_ptr(performance, i)) {
589 pr->performance = percpu_ptr(performance, i);
590 cpu_set(i, pr->performance->shared_cpu_map);
591 if (acpi_processor_get_psd(pr)) {
600 * Now that we have _PSD data from all CPUs, lets setup P-state
603 for_each_possible_cpu(i) {
608 /* Basic validity check for domain info */
609 pdomain = &(pr->performance->domain_info);
610 if ((pdomain->revision != ACPI_PSD_REV0_REVISION) ||
611 (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES)) {
615 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
616 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
617 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
623 cpus_clear(covered_cpus);
624 for_each_possible_cpu(i) {
629 if (cpu_isset(i, covered_cpus))
632 pdomain = &(pr->performance->domain_info);
633 cpu_set(i, pr->performance->shared_cpu_map);
634 cpu_set(i, covered_cpus);
635 if (pdomain->num_processors <= 1)
638 /* Validate the Domain info */
639 count_target = pdomain->num_processors;
641 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
642 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
643 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
644 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
645 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
646 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
648 for_each_possible_cpu(j) {
652 match_pr = processors[j];
656 match_pdomain = &(match_pr->performance->domain_info);
657 if (match_pdomain->domain != pdomain->domain)
660 /* Here i and j are in the same domain */
662 if (match_pdomain->num_processors != count_target) {
667 if (pdomain->coord_type != match_pdomain->coord_type) {
672 cpu_set(j, covered_cpus);
673 cpu_set(j, pr->performance->shared_cpu_map);
677 for_each_possible_cpu(j) {
681 match_pr = processors[j];
685 match_pdomain = &(match_pr->performance->domain_info);
686 if (match_pdomain->domain != pdomain->domain)
689 match_pr->performance->shared_type =
690 pr->performance->shared_type;
691 match_pr->performance->shared_cpu_map =
692 pr->performance->shared_cpu_map;
697 for_each_possible_cpu(i) {
699 if (!pr || !pr->performance)
702 /* Assume no coordination on any error parsing domain info */
704 cpus_clear(pr->performance->shared_cpu_map);
705 cpu_set(i, pr->performance->shared_cpu_map);
706 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
708 pr->performance = NULL; /* Will be set for real in register */
711 mutex_unlock(&performance_mutex);
714 EXPORT_SYMBOL(acpi_processor_preregister_performance);
718 acpi_processor_register_performance(struct acpi_processor_performance
719 *performance, unsigned int cpu)
721 struct acpi_processor *pr;
724 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
727 mutex_lock(&performance_mutex);
729 pr = processors[cpu];
731 mutex_unlock(&performance_mutex);
735 if (pr->performance) {
736 mutex_unlock(&performance_mutex);
740 WARN_ON(!performance);
742 pr->performance = performance;
744 if (acpi_processor_get_performance_info(pr)) {
745 pr->performance = NULL;
746 mutex_unlock(&performance_mutex);
750 acpi_cpufreq_add_file(pr);
752 mutex_unlock(&performance_mutex);
756 EXPORT_SYMBOL(acpi_processor_register_performance);
759 acpi_processor_unregister_performance(struct acpi_processor_performance
760 *performance, unsigned int cpu)
762 struct acpi_processor *pr;
765 mutex_lock(&performance_mutex);
767 pr = processors[cpu];
769 mutex_unlock(&performance_mutex);
774 kfree(pr->performance->states);
775 pr->performance = NULL;
777 acpi_cpufreq_remove_file(pr);
779 mutex_unlock(&performance_mutex);
784 EXPORT_SYMBOL(acpi_processor_unregister_performance);