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.
68 * -1 -> cpufreq low level drivers not initialized -> _PSS, etc. not called yet
70 * 0 -> cpufreq low level drivers initialized -> consider _PPC values
71 * 1 -> ignore _PPC totally -> forced by user through boot param
73 static unsigned int ignore_ppc = -1;
74 module_param(ignore_ppc, uint, 0644);
75 MODULE_PARM_DESC(ignore_ppc, "If the frequency of your machine gets wrongly" \
76 "limited by BIOS, this should help");
78 #define PPC_REGISTERED 1
81 static int acpi_processor_ppc_status;
83 static int acpi_processor_ppc_notifier(struct notifier_block *nb,
84 unsigned long event, void *data)
86 struct cpufreq_policy *policy = data;
87 struct acpi_processor *pr;
90 if (event == CPUFREQ_START && ignore_ppc <= 0) {
98 if (event != CPUFREQ_INCOMPATIBLE)
101 mutex_lock(&performance_mutex);
103 pr = per_cpu(processors, policy->cpu);
104 if (!pr || !pr->performance)
107 ppc = (unsigned int)pr->performance_platform_limit;
109 if (ppc >= pr->performance->state_count)
112 cpufreq_verify_within_limits(policy, 0,
113 pr->performance->states[ppc].
114 core_frequency * 1000);
117 mutex_unlock(&performance_mutex);
122 static struct notifier_block acpi_ppc_notifier_block = {
123 .notifier_call = acpi_processor_ppc_notifier,
126 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
128 acpi_status status = 0;
129 unsigned long ppc = 0;
136 * _PPC indicates the maximum state currently supported by the platform
137 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
139 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
141 if (status != AE_NOT_FOUND)
142 acpi_processor_ppc_status |= PPC_IN_USE;
144 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
145 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
149 cpufreq_printk("CPU %d: _PPC is %d - frequency %s limited\n", pr->id,
150 (int)ppc, ppc ? "" : "not");
152 pr->performance_platform_limit = (int)ppc;
157 int acpi_processor_ppc_has_changed(struct acpi_processor *pr)
164 ret = acpi_processor_get_platform_limit(pr);
169 return cpufreq_update_policy(pr->id);
172 void acpi_processor_ppc_init(void)
174 if (!cpufreq_register_notifier
175 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
176 acpi_processor_ppc_status |= PPC_REGISTERED;
179 "Warning: Processor Platform Limit not supported.\n");
182 void acpi_processor_ppc_exit(void)
184 if (acpi_processor_ppc_status & PPC_REGISTERED)
185 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
186 CPUFREQ_POLICY_NOTIFIER);
188 acpi_processor_ppc_status &= ~PPC_REGISTERED;
191 static int acpi_processor_get_performance_control(struct acpi_processor *pr)
194 acpi_status status = 0;
195 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
196 union acpi_object *pct = NULL;
197 union acpi_object obj = { 0 };
200 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
201 if (ACPI_FAILURE(status)) {
202 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
206 pct = (union acpi_object *)buffer.pointer;
207 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
208 || (pct->package.count != 2)) {
209 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
218 obj = pct->package.elements[0];
220 if ((obj.type != ACPI_TYPE_BUFFER)
221 || (obj.buffer.length < sizeof(struct acpi_pct_register))
222 || (obj.buffer.pointer == NULL)) {
223 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
227 memcpy(&pr->performance->control_register, obj.buffer.pointer,
228 sizeof(struct acpi_pct_register));
234 obj = pct->package.elements[1];
236 if ((obj.type != ACPI_TYPE_BUFFER)
237 || (obj.buffer.length < sizeof(struct acpi_pct_register))
238 || (obj.buffer.pointer == NULL)) {
239 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
244 memcpy(&pr->performance->status_register, obj.buffer.pointer,
245 sizeof(struct acpi_pct_register));
248 kfree(buffer.pointer);
253 static int acpi_processor_get_performance_states(struct acpi_processor *pr)
256 acpi_status status = AE_OK;
257 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
258 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
259 struct acpi_buffer state = { 0, NULL };
260 union acpi_object *pss = NULL;
264 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
265 if (ACPI_FAILURE(status)) {
266 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
270 pss = buffer.pointer;
271 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
272 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
277 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
278 pss->package.count));
280 pr->performance->state_count = pss->package.count;
281 pr->performance->states =
282 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
284 if (!pr->performance->states) {
289 for (i = 0; i < pr->performance->state_count; i++) {
291 struct acpi_processor_px *px = &(pr->performance->states[i]);
293 state.length = sizeof(struct acpi_processor_px);
296 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
298 status = acpi_extract_package(&(pss->package.elements[i]),
300 if (ACPI_FAILURE(status)) {
301 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
303 kfree(pr->performance->states);
307 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
308 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
310 (u32) px->core_frequency,
312 (u32) px->transition_latency,
313 (u32) px->bus_master_latency,
314 (u32) px->control, (u32) px->status));
316 if (!px->core_frequency) {
317 printk(KERN_ERR PREFIX
318 "Invalid _PSS data: freq is zero\n");
320 kfree(pr->performance->states);
326 kfree(buffer.pointer);
331 static int acpi_processor_get_performance_info(struct acpi_processor *pr)
334 acpi_status status = AE_OK;
335 acpi_handle handle = NULL;
338 if (!pr || !pr->performance || !pr->handle)
341 status = acpi_get_handle(pr->handle, "_PCT", &handle);
342 if (ACPI_FAILURE(status)) {
343 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
344 "ACPI-based processor performance control unavailable\n"));
348 result = acpi_processor_get_performance_control(pr);
352 result = acpi_processor_get_performance_states(pr);
359 int acpi_processor_notify_smm(struct module *calling_module)
362 static int is_done = 0;
365 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
368 if (!try_module_get(calling_module))
371 /* is_done is set to negative if an error occured,
372 * and to postitive if _no_ error occured, but SMM
373 * was already notified. This avoids double notification
374 * which might lead to unexpected results...
377 module_put(calling_module);
379 } else if (is_done < 0) {
380 module_put(calling_module);
386 /* Can't write pstate_control to smi_command if either value is zero */
387 if ((!acpi_gbl_FADT.smi_command) || (!acpi_gbl_FADT.pstate_control)) {
388 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
389 module_put(calling_module);
393 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
394 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
395 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
397 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
398 (u32) acpi_gbl_FADT.pstate_control, 8);
399 if (ACPI_FAILURE(status)) {
400 ACPI_EXCEPTION((AE_INFO, status,
401 "Failed to write pstate_control [0x%x] to "
402 "smi_command [0x%x]", acpi_gbl_FADT.pstate_control,
403 acpi_gbl_FADT.smi_command));
404 module_put(calling_module);
408 /* Success. If there's no _PPC, we need to fear nothing, so
409 * we can allow the cpufreq driver to be rmmod'ed. */
412 if (!(acpi_processor_ppc_status & PPC_IN_USE))
413 module_put(calling_module);
418 EXPORT_SYMBOL(acpi_processor_notify_smm);
420 #ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
421 /* /proc/acpi/processor/../performance interface (DEPRECATED) */
423 static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file);
424 static struct file_operations acpi_processor_perf_fops = {
425 .owner = THIS_MODULE,
426 .open = acpi_processor_perf_open_fs,
429 .release = single_release,
432 static int acpi_processor_perf_seq_show(struct seq_file *seq, void *offset)
434 struct acpi_processor *pr = seq->private;
441 if (!pr->performance) {
442 seq_puts(seq, "<not supported>\n");
446 seq_printf(seq, "state count: %d\n"
447 "active state: P%d\n",
448 pr->performance->state_count, pr->performance->state);
450 seq_puts(seq, "states:\n");
451 for (i = 0; i < pr->performance->state_count; i++)
453 " %cP%d: %d MHz, %d mW, %d uS\n",
454 (i == pr->performance->state ? '*' : ' '), i,
455 (u32) pr->performance->states[i].core_frequency,
456 (u32) pr->performance->states[i].power,
457 (u32) pr->performance->states[i].transition_latency);
463 static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file)
465 return single_open(file, acpi_processor_perf_seq_show,
469 static void acpi_cpufreq_add_file(struct acpi_processor *pr)
471 struct acpi_device *device = NULL;
474 if (acpi_bus_get_device(pr->handle, &device))
477 /* add file 'performance' [R/W] */
478 proc_create_data(ACPI_PROCESSOR_FILE_PERFORMANCE, S_IFREG | S_IRUGO,
479 acpi_device_dir(device),
480 &acpi_processor_perf_fops, acpi_driver_data(device));
484 static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
486 struct acpi_device *device = NULL;
489 if (acpi_bus_get_device(pr->handle, &device))
492 /* remove file 'performance' */
493 remove_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
494 acpi_device_dir(device));
500 static void acpi_cpufreq_add_file(struct acpi_processor *pr)
504 static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
508 #endif /* CONFIG_X86_ACPI_CPUFREQ_PROC_INTF */
510 static int acpi_processor_get_psd(struct acpi_processor *pr)
513 acpi_status status = AE_OK;
514 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
515 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
516 struct acpi_buffer state = {0, NULL};
517 union acpi_object *psd = NULL;
518 struct acpi_psd_package *pdomain;
520 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
521 if (ACPI_FAILURE(status)) {
525 psd = buffer.pointer;
526 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
527 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
532 if (psd->package.count != 1) {
533 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
538 pdomain = &(pr->performance->domain_info);
540 state.length = sizeof(struct acpi_psd_package);
541 state.pointer = pdomain;
543 status = acpi_extract_package(&(psd->package.elements[0]),
545 if (ACPI_FAILURE(status)) {
546 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSD data\n"));
551 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
552 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:num_entries\n"));
557 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
558 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _PSD:revision\n"));
564 kfree(buffer.pointer);
568 int acpi_processor_preregister_performance(
569 struct acpi_processor_performance *performance)
571 int count, count_target;
574 cpumask_t covered_cpus;
575 struct acpi_processor *pr;
576 struct acpi_psd_package *pdomain;
577 struct acpi_processor *match_pr;
578 struct acpi_psd_package *match_pdomain;
580 mutex_lock(&performance_mutex);
584 /* Call _PSD for all CPUs */
585 for_each_possible_cpu(i) {
586 pr = per_cpu(processors, i);
588 /* Look only at processors in ACPI namespace */
592 if (pr->performance) {
597 if (!performance || !percpu_ptr(performance, i)) {
602 pr->performance = percpu_ptr(performance, i);
603 cpu_set(i, pr->performance->shared_cpu_map);
604 if (acpi_processor_get_psd(pr)) {
613 * Now that we have _PSD data from all CPUs, lets setup P-state
616 for_each_possible_cpu(i) {
617 pr = per_cpu(processors, i);
621 /* Basic validity check for domain info */
622 pdomain = &(pr->performance->domain_info);
623 if ((pdomain->revision != ACPI_PSD_REV0_REVISION) ||
624 (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES)) {
628 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
629 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
630 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
636 cpus_clear(covered_cpus);
637 for_each_possible_cpu(i) {
638 pr = per_cpu(processors, i);
642 if (cpu_isset(i, covered_cpus))
645 pdomain = &(pr->performance->domain_info);
646 cpu_set(i, pr->performance->shared_cpu_map);
647 cpu_set(i, covered_cpus);
648 if (pdomain->num_processors <= 1)
651 /* Validate the Domain info */
652 count_target = pdomain->num_processors;
654 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
655 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
656 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
657 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
658 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
659 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
661 for_each_possible_cpu(j) {
665 match_pr = per_cpu(processors, j);
669 match_pdomain = &(match_pr->performance->domain_info);
670 if (match_pdomain->domain != pdomain->domain)
673 /* Here i and j are in the same domain */
675 if (match_pdomain->num_processors != count_target) {
680 if (pdomain->coord_type != match_pdomain->coord_type) {
685 cpu_set(j, covered_cpus);
686 cpu_set(j, pr->performance->shared_cpu_map);
690 for_each_possible_cpu(j) {
694 match_pr = per_cpu(processors, j);
698 match_pdomain = &(match_pr->performance->domain_info);
699 if (match_pdomain->domain != pdomain->domain)
702 match_pr->performance->shared_type =
703 pr->performance->shared_type;
704 match_pr->performance->shared_cpu_map =
705 pr->performance->shared_cpu_map;
710 for_each_possible_cpu(i) {
711 pr = per_cpu(processors, i);
712 if (!pr || !pr->performance)
715 /* Assume no coordination on any error parsing domain info */
717 cpus_clear(pr->performance->shared_cpu_map);
718 cpu_set(i, pr->performance->shared_cpu_map);
719 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
721 pr->performance = NULL; /* Will be set for real in register */
724 mutex_unlock(&performance_mutex);
727 EXPORT_SYMBOL(acpi_processor_preregister_performance);
731 acpi_processor_register_performance(struct acpi_processor_performance
732 *performance, unsigned int cpu)
734 struct acpi_processor *pr;
737 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
740 mutex_lock(&performance_mutex);
742 pr = per_cpu(processors, cpu);
744 mutex_unlock(&performance_mutex);
748 if (pr->performance) {
749 mutex_unlock(&performance_mutex);
753 WARN_ON(!performance);
755 pr->performance = performance;
757 if (acpi_processor_get_performance_info(pr)) {
758 pr->performance = NULL;
759 mutex_unlock(&performance_mutex);
763 acpi_cpufreq_add_file(pr);
765 mutex_unlock(&performance_mutex);
769 EXPORT_SYMBOL(acpi_processor_register_performance);
772 acpi_processor_unregister_performance(struct acpi_processor_performance
773 *performance, unsigned int cpu)
775 struct acpi_processor *pr;
778 mutex_lock(&performance_mutex);
780 pr = per_cpu(processors, cpu);
782 mutex_unlock(&performance_mutex);
787 kfree(pr->performance->states);
788 pr->performance = NULL;
790 acpi_cpufreq_remove_file(pr);
792 mutex_unlock(&performance_mutex);
797 EXPORT_SYMBOL(acpi_processor_unregister_performance);