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>
33 #include <linux/slab.h>
36 #include <asm/cpufeature.h>
39 #include <acpi/acpi_bus.h>
40 #include <acpi/acpi_drivers.h>
41 #include <acpi/processor.h>
43 #define PREFIX "ACPI: "
45 #define ACPI_PROCESSOR_CLASS "processor"
46 #define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
47 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
48 ACPI_MODULE_NAME("processor_perflib");
50 static DEFINE_MUTEX(performance_mutex);
53 * _PPC support is implemented as a CPUfreq policy notifier:
54 * This means each time a CPUfreq driver registered also with
55 * the ACPI core is asked to change the speed policy, the maximum
56 * value is adjusted so that it is within the platform limit.
58 * Also, when a new platform limit value is detected, the CPUfreq
59 * policy is adjusted accordingly.
63 * -1 -> cpufreq low level drivers not initialized -> _PSS, etc. not called yet
65 * 0 -> cpufreq low level drivers initialized -> consider _PPC values
66 * 1 -> ignore _PPC totally -> forced by user through boot param
68 static int ignore_ppc = -1;
69 module_param(ignore_ppc, int, 0644);
70 MODULE_PARM_DESC(ignore_ppc, "If the frequency of your machine gets wrongly" \
71 "limited by BIOS, this should help");
73 #define PPC_REGISTERED 1
76 static int acpi_processor_ppc_status;
78 #ifdef CONFIG_CPU_FREQ
79 static int acpi_processor_ppc_notifier(struct notifier_block *nb,
80 unsigned long event, void *data)
82 struct cpufreq_policy *policy = data;
83 struct acpi_processor *pr;
86 if (event == CPUFREQ_START && ignore_ppc <= 0) {
94 if (event != CPUFREQ_INCOMPATIBLE)
97 mutex_lock(&performance_mutex);
99 pr = per_cpu(processors, policy->cpu);
100 if (!pr || !pr->performance)
103 ppc = (unsigned int)pr->performance_platform_limit;
105 if (ppc >= pr->performance->state_count)
108 cpufreq_verify_within_limits(policy, 0,
109 pr->performance->states[ppc].
110 core_frequency * 1000);
113 mutex_unlock(&performance_mutex);
118 static struct notifier_block acpi_ppc_notifier_block = {
119 .notifier_call = acpi_processor_ppc_notifier,
121 #endif /* CONFIG_CPU_FREQ */
123 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
125 acpi_status status = 0;
126 unsigned long long ppc = 0;
133 * _PPC indicates the maximum state currently supported by the platform
134 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
136 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
138 if (status != AE_NOT_FOUND)
139 acpi_processor_ppc_status |= PPC_IN_USE;
141 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
142 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
146 pr_debug("CPU %d: _PPC is %d - frequency %s limited\n", pr->id,
147 (int)ppc, ppc ? "" : "not");
149 pr->performance_platform_limit = (int)ppc;
154 #define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
156 * acpi_processor_ppc_ost: Notify firmware the _PPC evaluation status
157 * @handle: ACPI processor handle
158 * @status: the status code of _PPC evaluation
159 * 0: success. OSPM is now using the performance state specificed.
160 * 1: failure. OSPM has not changed the number of P-states in use
162 static void acpi_processor_ppc_ost(acpi_handle handle, int status)
164 union acpi_object params[2] = {
165 {.type = ACPI_TYPE_INTEGER,},
166 {.type = ACPI_TYPE_INTEGER,},
168 struct acpi_object_list arg_list = {2, params};
171 params[0].integer.value = ACPI_PROCESSOR_NOTIFY_PERFORMANCE;
172 params[1].integer.value = status;
174 /* when there is no _OST , skip it */
175 if (ACPI_FAILURE(acpi_get_handle(handle, "_OST", &temp)))
178 acpi_evaluate_object(handle, "_OST", &arg_list, NULL);
182 int acpi_processor_ppc_has_changed(struct acpi_processor *pr, int event_flag)
186 #ifdef CONFIG_PROCESSOR_EXTERNAL_CONTROL
187 /* Xen hypervisor can handle cpufreq _PPC event */
188 if (ignore_ppc < 0 && processor_pmperf_external())
194 * Only when it is notification event, the _OST object
195 * will be evaluated. Otherwise it is skipped.
198 acpi_processor_ppc_ost(pr->handle, 1);
202 ret = acpi_processor_get_platform_limit(pr);
204 * Only when it is notification event, the _OST object
205 * will be evaluated. Otherwise it is skipped.
209 acpi_processor_ppc_ost(pr->handle, 1);
211 acpi_processor_ppc_ost(pr->handle, 0);
216 #ifdef CONFIG_CPU_FREQ
217 return cpufreq_update_policy(pr->id);
218 #elif defined(CONFIG_PROCESSOR_EXTERNAL_CONTROL)
219 return processor_notify_external(pr,
220 PROCESSOR_PM_CHANGE, PM_TYPE_PERF);
224 int acpi_processor_get_bios_limit(int cpu, unsigned int *limit)
226 struct acpi_processor *pr;
228 pr = per_cpu(processors, cpu);
229 if (!pr || !pr->performance || !pr->performance->state_count)
231 *limit = pr->performance->states[pr->performance_platform_limit].
232 core_frequency * 1000;
235 EXPORT_SYMBOL(acpi_processor_get_bios_limit);
237 #ifdef CONFIG_CPU_FREQ
238 void acpi_processor_ppc_init(void)
240 if (!cpufreq_register_notifier
241 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
242 acpi_processor_ppc_status |= PPC_REGISTERED;
245 "Warning: Processor Platform Limit not supported.\n");
248 void acpi_processor_ppc_exit(void)
250 if (acpi_processor_ppc_status & PPC_REGISTERED)
251 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
252 CPUFREQ_POLICY_NOTIFIER);
254 acpi_processor_ppc_status &= ~PPC_REGISTERED;
258 * Do a quick check if the systems looks like it should use ACPI
259 * cpufreq. We look at a _PCT method being available, but don't
260 * do a whole lot of sanity checks.
262 void acpi_processor_load_module(struct acpi_processor *pr)
264 static int requested;
265 acpi_status status = 0;
266 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
268 if (!arch_has_acpi_pdc() || requested)
270 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
271 if (!ACPI_FAILURE(status)) {
272 printk(KERN_INFO PREFIX "Requesting acpi_cpufreq\n");
273 request_module_nowait("acpi_cpufreq");
276 kfree(buffer.pointer);
278 #endif /* CONFIG_CPU_FREQ */
280 static int acpi_processor_get_performance_control(struct acpi_processor *pr)
283 acpi_status status = 0;
284 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
285 union acpi_object *pct = NULL;
286 union acpi_object obj = { 0 };
289 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
290 if (ACPI_FAILURE(status)) {
291 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
295 pct = (union acpi_object *)buffer.pointer;
296 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
297 || (pct->package.count != 2)) {
298 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
307 obj = pct->package.elements[0];
309 if ((obj.type != ACPI_TYPE_BUFFER)
310 || (obj.buffer.length < sizeof(struct acpi_pct_register))
311 || (obj.buffer.pointer == NULL)) {
312 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
316 memcpy(&pr->performance->control_register, obj.buffer.pointer,
317 sizeof(struct acpi_pct_register));
323 obj = pct->package.elements[1];
325 if ((obj.type != ACPI_TYPE_BUFFER)
326 || (obj.buffer.length < sizeof(struct acpi_pct_register))
327 || (obj.buffer.pointer == NULL)) {
328 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
333 memcpy(&pr->performance->status_register, obj.buffer.pointer,
334 sizeof(struct acpi_pct_register));
337 kfree(buffer.pointer);
342 static int acpi_processor_get_performance_states(struct acpi_processor *pr)
345 acpi_status status = AE_OK;
346 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
347 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
348 struct acpi_buffer state = { 0, NULL };
349 union acpi_object *pss = NULL;
353 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
354 if (ACPI_FAILURE(status)) {
355 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
359 pss = buffer.pointer;
360 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
361 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
366 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
367 pss->package.count));
369 pr->performance->state_count = pss->package.count;
370 pr->performance->states =
371 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
373 if (!pr->performance->states) {
378 for (i = 0; i < pr->performance->state_count; i++) {
380 struct acpi_processor_px *px = &(pr->performance->states[i]);
382 state.length = sizeof(struct acpi_processor_px);
385 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
387 status = acpi_extract_package(&(pss->package.elements[i]),
389 if (ACPI_FAILURE(status)) {
390 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
392 kfree(pr->performance->states);
396 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
397 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
399 (u32) px->core_frequency,
401 (u32) px->transition_latency,
402 (u32) px->bus_master_latency,
403 (u32) px->control, (u32) px->status));
406 * Check that ACPI's u64 MHz will be valid as u32 KHz in cpufreq
408 if (!px->core_frequency ||
409 ((u32)(px->core_frequency * 1000) !=
410 (px->core_frequency * 1000))) {
411 printk(KERN_ERR FW_BUG PREFIX
412 "Invalid BIOS _PSS frequency: 0x%llx MHz\n",
415 kfree(pr->performance->states);
421 kfree(buffer.pointer);
426 #ifndef CONFIG_PROCESSOR_EXTERNAL_CONTROL
429 int acpi_processor_get_performance_info(struct acpi_processor *pr)
432 acpi_status status = AE_OK;
433 acpi_handle handle = NULL;
435 if (!pr || !pr->performance || !pr->handle)
438 status = acpi_get_handle(pr->handle, "_PCT", &handle);
439 if (ACPI_FAILURE(status)) {
440 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
441 "ACPI-based processor performance control unavailable\n"));
445 result = acpi_processor_get_performance_control(pr);
449 result = acpi_processor_get_performance_states(pr);
453 /* We need to call _PPC once when cpufreq starts */
455 result = acpi_processor_get_platform_limit(pr);
460 * Having _PPC but missing frequencies (_PSS, _PCT) is a very good hint that
461 * the BIOS is older than the CPU and does not know its frequencies
465 if (ACPI_SUCCESS(acpi_get_handle(pr->handle, "_PPC", &handle))){
466 if(boot_cpu_has(X86_FEATURE_EST))
467 printk(KERN_WARNING FW_BUG "BIOS needs update for CPU "
468 "frequency support\n");
474 #ifdef CONFIG_CPU_FREQ
475 int acpi_processor_notify_smm(struct module *calling_module)
478 static int is_done = 0;
481 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
484 if (!try_module_get(calling_module))
487 /* is_done is set to negative if an error occurred,
488 * and to postitive if _no_ error occurred, but SMM
489 * was already notified. This avoids double notification
490 * which might lead to unexpected results...
493 module_put(calling_module);
495 } else if (is_done < 0) {
496 module_put(calling_module);
502 /* Can't write pstate_control to smi_command if either value is zero */
503 if ((!acpi_gbl_FADT.smi_command) || (!acpi_gbl_FADT.pstate_control)) {
504 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
505 module_put(calling_module);
509 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
510 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
511 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
513 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
514 (u32) acpi_gbl_FADT.pstate_control, 8);
515 if (ACPI_FAILURE(status)) {
516 ACPI_EXCEPTION((AE_INFO, status,
517 "Failed to write pstate_control [0x%x] to "
518 "smi_command [0x%x]", acpi_gbl_FADT.pstate_control,
519 acpi_gbl_FADT.smi_command));
520 module_put(calling_module);
524 /* Success. If there's no _PPC, we need to fear nothing, so
525 * we can allow the cpufreq driver to be rmmod'ed. */
528 if (!(acpi_processor_ppc_status & PPC_IN_USE))
529 module_put(calling_module);
534 EXPORT_SYMBOL(acpi_processor_notify_smm);
535 #endif /* CONFIG_CPU_FREQ */
537 #ifndef CONFIG_PROCESSOR_EXTERNAL_CONTROL
540 int acpi_processor_get_psd(struct acpi_processor *pr)
543 acpi_status status = AE_OK;
544 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
545 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
546 struct acpi_buffer state = {0, NULL};
547 union acpi_object *psd = NULL;
548 struct acpi_psd_package *pdomain;
550 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
551 if (ACPI_FAILURE(status)) {
555 psd = buffer.pointer;
556 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
557 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
562 if (psd->package.count != 1) {
563 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
568 pdomain = &(pr->performance->domain_info);
570 state.length = sizeof(struct acpi_psd_package);
571 state.pointer = pdomain;
573 status = acpi_extract_package(&(psd->package.elements[0]),
575 if (ACPI_FAILURE(status)) {
576 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
581 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
582 printk(KERN_ERR PREFIX "Unknown _PSD:num_entries\n");
587 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
588 printk(KERN_ERR PREFIX "Unknown _PSD:revision\n");
593 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
594 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
595 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
596 printk(KERN_ERR PREFIX "Invalid _PSD:coord_type\n");
601 kfree(buffer.pointer);
605 #ifndef CONFIG_PROCESSOR_EXTERNAL_CONTROL
607 int acpi_processor_preregister_performance(
608 struct acpi_processor_performance __percpu *performance)
610 int count, count_target;
613 cpumask_var_t covered_cpus;
614 struct acpi_processor *pr;
615 struct acpi_psd_package *pdomain;
616 struct acpi_processor *match_pr;
617 struct acpi_psd_package *match_pdomain;
619 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
622 mutex_lock(&performance_mutex);
625 * Check if another driver has already registered, and abort before
626 * changing pr->performance if it has. Check input data as well.
628 for_each_possible_cpu(i) {
629 pr = per_cpu(processors, i);
631 /* Look only at processors in ACPI namespace */
635 if (pr->performance) {
640 if (!performance || !per_cpu_ptr(performance, i)) {
646 /* Call _PSD for all CPUs */
647 for_each_possible_cpu(i) {
648 pr = per_cpu(processors, i);
652 pr->performance = per_cpu_ptr(performance, i);
653 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
654 if (acpi_processor_get_psd(pr)) {
663 * Now that we have _PSD data from all CPUs, lets setup P-state
666 for_each_possible_cpu(i) {
667 pr = per_cpu(processors, i);
671 if (cpumask_test_cpu(i, covered_cpus))
674 pdomain = &(pr->performance->domain_info);
675 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
676 cpumask_set_cpu(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 = per_cpu(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 cpumask_set_cpu(j, covered_cpus);
715 cpumask_set_cpu(j, pr->performance->shared_cpu_map);
719 for_each_possible_cpu(j) {
723 match_pr = per_cpu(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 cpumask_copy(match_pr->performance->shared_cpu_map,
734 pr->performance->shared_cpu_map);
739 for_each_possible_cpu(i) {
740 pr = per_cpu(processors, i);
741 if (!pr || !pr->performance)
744 /* Assume no coordination on any error parsing domain info */
746 cpumask_clear(pr->performance->shared_cpu_map);
747 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
748 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
750 pr->performance = NULL; /* Will be set for real in register */
754 mutex_unlock(&performance_mutex);
755 free_cpumask_var(covered_cpus);
758 EXPORT_SYMBOL(acpi_processor_preregister_performance);
761 acpi_processor_register_performance(struct acpi_processor_performance
762 *performance, unsigned int cpu)
764 struct acpi_processor *pr;
766 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
769 mutex_lock(&performance_mutex);
771 pr = per_cpu(processors, cpu);
773 mutex_unlock(&performance_mutex);
777 if (pr->performance) {
778 mutex_unlock(&performance_mutex);
782 WARN_ON(!performance);
784 pr->performance = performance;
786 if (acpi_processor_get_performance_info(pr)) {
787 pr->performance = NULL;
788 mutex_unlock(&performance_mutex);
792 mutex_unlock(&performance_mutex);
796 EXPORT_SYMBOL(acpi_processor_register_performance);
799 acpi_processor_unregister_performance(struct acpi_processor_performance
800 *performance, unsigned int cpu)
802 struct acpi_processor *pr;
804 mutex_lock(&performance_mutex);
806 pr = per_cpu(processors, cpu);
808 mutex_unlock(&performance_mutex);
813 kfree(pr->performance->states);
814 pr->performance = NULL;
816 mutex_unlock(&performance_mutex);
821 EXPORT_SYMBOL(acpi_processor_unregister_performance);
823 #endif /* !CONFIG_PROCESSOR_EXTERNAL_CONTROL */