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>
35 #include <asm/cpufeature.h>
38 #include <acpi/acpi_bus.h>
39 #include <acpi/acpi_drivers.h>
40 #include <acpi/processor.h>
42 #define ACPI_PROCESSOR_CLASS "processor"
43 #define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
44 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
45 ACPI_MODULE_NAME("processor_perflib");
47 static DEFINE_MUTEX(performance_mutex);
49 /* Use cpufreq debug layer for _PPC changes. */
50 #define cpufreq_printk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
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.
64 * -1 -> cpufreq low level drivers not initialized -> _PSS, etc. not called yet
66 * 0 -> cpufreq low level drivers initialized -> consider _PPC values
67 * 1 -> ignore _PPC totally -> forced by user through boot param
69 static int ignore_ppc = -1;
70 module_param(ignore_ppc, int, 0644);
71 MODULE_PARM_DESC(ignore_ppc, "If the frequency of your machine gets wrongly" \
72 "limited by BIOS, this should help");
74 #define PPC_REGISTERED 1
77 static int acpi_processor_ppc_status;
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,
122 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
124 acpi_status status = 0;
125 unsigned long long ppc = 0;
132 * _PPC indicates the maximum state currently supported by the platform
133 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
135 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
137 if (status != AE_NOT_FOUND)
138 acpi_processor_ppc_status |= PPC_IN_USE;
140 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
141 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
145 cpufreq_printk("CPU %d: _PPC is %d - frequency %s limited\n", pr->id,
146 (int)ppc, ppc ? "" : "not");
148 pr->performance_platform_limit = (int)ppc;
153 int acpi_processor_ppc_has_changed(struct acpi_processor *pr)
160 ret = acpi_processor_get_platform_limit(pr);
165 return cpufreq_update_policy(pr->id);
168 void acpi_processor_ppc_init(void)
170 if (!cpufreq_register_notifier
171 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
172 acpi_processor_ppc_status |= PPC_REGISTERED;
175 "Warning: Processor Platform Limit not supported.\n");
178 void acpi_processor_ppc_exit(void)
180 if (acpi_processor_ppc_status & PPC_REGISTERED)
181 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
182 CPUFREQ_POLICY_NOTIFIER);
184 acpi_processor_ppc_status &= ~PPC_REGISTERED;
187 static int acpi_processor_get_performance_control(struct acpi_processor *pr)
190 acpi_status status = 0;
191 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
192 union acpi_object *pct = NULL;
193 union acpi_object obj = { 0 };
196 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
197 if (ACPI_FAILURE(status)) {
198 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
202 pct = (union acpi_object *)buffer.pointer;
203 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
204 || (pct->package.count != 2)) {
205 printk(KERN_ERR PREFIX "Invalid _PCT data\n");
214 obj = pct->package.elements[0];
216 if ((obj.type != ACPI_TYPE_BUFFER)
217 || (obj.buffer.length < sizeof(struct acpi_pct_register))
218 || (obj.buffer.pointer == NULL)) {
219 printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
223 memcpy(&pr->performance->control_register, obj.buffer.pointer,
224 sizeof(struct acpi_pct_register));
230 obj = pct->package.elements[1];
232 if ((obj.type != ACPI_TYPE_BUFFER)
233 || (obj.buffer.length < sizeof(struct acpi_pct_register))
234 || (obj.buffer.pointer == NULL)) {
235 printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
240 memcpy(&pr->performance->status_register, obj.buffer.pointer,
241 sizeof(struct acpi_pct_register));
244 kfree(buffer.pointer);
249 static int acpi_processor_get_performance_states(struct acpi_processor *pr)
252 acpi_status status = AE_OK;
253 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
254 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
255 struct acpi_buffer state = { 0, NULL };
256 union acpi_object *pss = NULL;
260 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
261 if (ACPI_FAILURE(status)) {
262 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
266 pss = buffer.pointer;
267 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
268 printk(KERN_ERR PREFIX "Invalid _PSS data\n");
273 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
274 pss->package.count));
276 pr->performance->state_count = pss->package.count;
277 pr->performance->states =
278 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
280 if (!pr->performance->states) {
285 for (i = 0; i < pr->performance->state_count; i++) {
287 struct acpi_processor_px *px = &(pr->performance->states[i]);
289 state.length = sizeof(struct acpi_processor_px);
292 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
294 status = acpi_extract_package(&(pss->package.elements[i]),
296 if (ACPI_FAILURE(status)) {
297 ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
299 kfree(pr->performance->states);
303 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
304 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
306 (u32) px->core_frequency,
308 (u32) px->transition_latency,
309 (u32) px->bus_master_latency,
310 (u32) px->control, (u32) px->status));
313 * Check that ACPI's u64 MHz will be valid as u32 KHz in cpufreq
315 if (!px->core_frequency ||
316 ((u32)(px->core_frequency * 1000) !=
317 (px->core_frequency * 1000))) {
318 printk(KERN_ERR FW_BUG PREFIX
319 "Invalid BIOS _PSS frequency: 0x%llx MHz\n",
322 kfree(pr->performance->states);
328 kfree(buffer.pointer);
333 static int acpi_processor_get_performance_info(struct acpi_processor *pr)
336 acpi_status status = AE_OK;
337 acpi_handle handle = NULL;
339 if (!pr || !pr->performance || !pr->handle)
342 status = acpi_get_handle(pr->handle, "_PCT", &handle);
343 if (ACPI_FAILURE(status)) {
344 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
345 "ACPI-based processor performance control unavailable\n"));
349 result = acpi_processor_get_performance_control(pr);
353 result = acpi_processor_get_performance_states(pr);
360 * Having _PPC but missing frequencies (_PSS, _PCT) is a very good hint that
361 * the BIOS is older than the CPU and does not know its frequencies
365 if (ACPI_SUCCESS(acpi_get_handle(pr->handle, "_PPC", &handle))){
366 if(boot_cpu_has(X86_FEATURE_EST))
367 printk(KERN_WARNING FW_BUG "BIOS needs update for CPU "
368 "frequency support\n");
374 int acpi_processor_notify_smm(struct module *calling_module)
377 static int is_done = 0;
380 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
383 if (!try_module_get(calling_module))
386 /* is_done is set to negative if an error occured,
387 * and to postitive if _no_ error occured, but SMM
388 * was already notified. This avoids double notification
389 * which might lead to unexpected results...
392 module_put(calling_module);
394 } else if (is_done < 0) {
395 module_put(calling_module);
401 /* Can't write pstate_control to smi_command if either value is zero */
402 if ((!acpi_gbl_FADT.smi_command) || (!acpi_gbl_FADT.pstate_control)) {
403 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
404 module_put(calling_module);
408 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
409 "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
410 acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
412 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
413 (u32) acpi_gbl_FADT.pstate_control, 8);
414 if (ACPI_FAILURE(status)) {
415 ACPI_EXCEPTION((AE_INFO, status,
416 "Failed to write pstate_control [0x%x] to "
417 "smi_command [0x%x]", acpi_gbl_FADT.pstate_control,
418 acpi_gbl_FADT.smi_command));
419 module_put(calling_module);
423 /* Success. If there's no _PPC, we need to fear nothing, so
424 * we can allow the cpufreq driver to be rmmod'ed. */
427 if (!(acpi_processor_ppc_status & PPC_IN_USE))
428 module_put(calling_module);
433 EXPORT_SYMBOL(acpi_processor_notify_smm);
435 static int acpi_processor_get_psd(struct acpi_processor *pr)
438 acpi_status status = AE_OK;
439 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
440 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
441 struct acpi_buffer state = {0, NULL};
442 union acpi_object *psd = NULL;
443 struct acpi_psd_package *pdomain;
445 status = acpi_evaluate_object(pr->handle, "_PSD", NULL, &buffer);
446 if (ACPI_FAILURE(status)) {
450 psd = buffer.pointer;
451 if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
452 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
457 if (psd->package.count != 1) {
458 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
463 pdomain = &(pr->performance->domain_info);
465 state.length = sizeof(struct acpi_psd_package);
466 state.pointer = pdomain;
468 status = acpi_extract_package(&(psd->package.elements[0]),
470 if (ACPI_FAILURE(status)) {
471 printk(KERN_ERR PREFIX "Invalid _PSD data\n");
476 if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
477 printk(KERN_ERR PREFIX "Unknown _PSD:num_entries\n");
482 if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
483 printk(KERN_ERR PREFIX "Unknown _PSD:revision\n");
488 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
489 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
490 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
491 printk(KERN_ERR PREFIX "Invalid _PSD:coord_type\n");
496 kfree(buffer.pointer);
500 int acpi_processor_preregister_performance(
501 struct acpi_processor_performance *performance)
503 int count, count_target;
506 cpumask_var_t covered_cpus;
507 struct acpi_processor *pr;
508 struct acpi_psd_package *pdomain;
509 struct acpi_processor *match_pr;
510 struct acpi_psd_package *match_pdomain;
512 if (!alloc_cpumask_var(&covered_cpus, GFP_KERNEL))
515 mutex_lock(&performance_mutex);
518 * Check if another driver has already registered, and abort before
519 * changing pr->performance if it has. Check input data as well.
521 for_each_possible_cpu(i) {
522 pr = per_cpu(processors, i);
524 /* Look only at processors in ACPI namespace */
528 if (pr->performance) {
533 if (!performance || !per_cpu_ptr(performance, i)) {
539 /* Call _PSD for all CPUs */
540 for_each_possible_cpu(i) {
541 pr = per_cpu(processors, i);
545 pr->performance = per_cpu_ptr(performance, i);
546 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
547 if (acpi_processor_get_psd(pr)) {
556 * Now that we have _PSD data from all CPUs, lets setup P-state
559 cpumask_clear(covered_cpus);
560 for_each_possible_cpu(i) {
561 pr = per_cpu(processors, i);
565 if (cpumask_test_cpu(i, covered_cpus))
568 pdomain = &(pr->performance->domain_info);
569 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
570 cpumask_set_cpu(i, covered_cpus);
571 if (pdomain->num_processors <= 1)
574 /* Validate the Domain info */
575 count_target = pdomain->num_processors;
577 if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
578 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
579 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
580 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
581 else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
582 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
584 for_each_possible_cpu(j) {
588 match_pr = per_cpu(processors, j);
592 match_pdomain = &(match_pr->performance->domain_info);
593 if (match_pdomain->domain != pdomain->domain)
596 /* Here i and j are in the same domain */
598 if (match_pdomain->num_processors != count_target) {
603 if (pdomain->coord_type != match_pdomain->coord_type) {
608 cpumask_set_cpu(j, covered_cpus);
609 cpumask_set_cpu(j, pr->performance->shared_cpu_map);
613 for_each_possible_cpu(j) {
617 match_pr = per_cpu(processors, j);
621 match_pdomain = &(match_pr->performance->domain_info);
622 if (match_pdomain->domain != pdomain->domain)
625 match_pr->performance->shared_type =
626 pr->performance->shared_type;
627 cpumask_copy(match_pr->performance->shared_cpu_map,
628 pr->performance->shared_cpu_map);
633 for_each_possible_cpu(i) {
634 pr = per_cpu(processors, i);
635 if (!pr || !pr->performance)
638 /* Assume no coordination on any error parsing domain info */
640 cpumask_clear(pr->performance->shared_cpu_map);
641 cpumask_set_cpu(i, pr->performance->shared_cpu_map);
642 pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
644 pr->performance = NULL; /* Will be set for real in register */
648 mutex_unlock(&performance_mutex);
649 free_cpumask_var(covered_cpus);
652 EXPORT_SYMBOL(acpi_processor_preregister_performance);
655 acpi_processor_register_performance(struct acpi_processor_performance
656 *performance, unsigned int cpu)
658 struct acpi_processor *pr;
660 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
663 mutex_lock(&performance_mutex);
665 pr = per_cpu(processors, cpu);
667 mutex_unlock(&performance_mutex);
671 if (pr->performance) {
672 mutex_unlock(&performance_mutex);
676 WARN_ON(!performance);
678 pr->performance = performance;
680 if (acpi_processor_get_performance_info(pr)) {
681 pr->performance = NULL;
682 mutex_unlock(&performance_mutex);
686 mutex_unlock(&performance_mutex);
690 EXPORT_SYMBOL(acpi_processor_register_performance);
693 acpi_processor_unregister_performance(struct acpi_processor_performance
694 *performance, unsigned int cpu)
696 struct acpi_processor *pr;
698 mutex_lock(&performance_mutex);
700 pr = per_cpu(processors, cpu);
702 mutex_unlock(&performance_mutex);
707 kfree(pr->performance->states);
708 pr->performance = NULL;
710 mutex_unlock(&performance_mutex);
715 EXPORT_SYMBOL(acpi_processor_unregister_performance);