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>
38 #include <asm/uaccess.h>
41 #include <acpi/acpi_bus.h>
42 #include <acpi/processor.h>
44 #define ACPI_PROCESSOR_COMPONENT 0x01000000
45 #define ACPI_PROCESSOR_CLASS "processor"
46 #define ACPI_PROCESSOR_DRIVER_NAME "ACPI Processor Driver"
47 #define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
48 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
49 ACPI_MODULE_NAME("acpi_processor")
51 static DECLARE_MUTEX(performance_sem);
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 down(&performance_sem);
77 if (event != CPUFREQ_INCOMPATIBLE)
80 pr = processors[policy->cpu];
81 if (!pr || !pr->performance)
84 ppc = (unsigned int)pr->performance_platform_limit;
88 if (ppc > pr->performance->state_count)
91 cpufreq_verify_within_limits(policy, 0,
92 pr->performance->states[ppc].
93 core_frequency * 1000);
101 static struct notifier_block acpi_ppc_notifier_block = {
102 .notifier_call = acpi_processor_ppc_notifier,
105 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
107 acpi_status status = 0;
108 unsigned long ppc = 0;
110 ACPI_FUNCTION_TRACE("acpi_processor_get_platform_limit");
113 return_VALUE(-EINVAL);
116 * _PPC indicates the maximum state currently supported by the platform
117 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
119 status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
121 if (status != AE_NOT_FOUND)
122 acpi_processor_ppc_status |= PPC_IN_USE;
124 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
125 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PPC\n"));
126 return_VALUE(-ENODEV);
129 pr->performance_platform_limit = (int)ppc;
134 int acpi_processor_ppc_has_changed(struct acpi_processor *pr)
136 int ret = acpi_processor_get_platform_limit(pr);
140 return cpufreq_update_policy(pr->id);
143 void acpi_processor_ppc_init(void)
145 if (!cpufreq_register_notifier
146 (&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
147 acpi_processor_ppc_status |= PPC_REGISTERED;
150 "Warning: Processor Platform Limit not supported.\n");
153 void acpi_processor_ppc_exit(void)
155 if (acpi_processor_ppc_status & PPC_REGISTERED)
156 cpufreq_unregister_notifier(&acpi_ppc_notifier_block,
157 CPUFREQ_POLICY_NOTIFIER);
159 acpi_processor_ppc_status &= ~PPC_REGISTERED;
162 static int acpi_processor_get_performance_control(struct acpi_processor *pr)
165 acpi_status status = 0;
166 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
167 union acpi_object *pct = NULL;
168 union acpi_object obj = { 0 };
170 ACPI_FUNCTION_TRACE("acpi_processor_get_performance_control");
172 status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
173 if (ACPI_FAILURE(status)) {
174 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PCT\n"));
175 return_VALUE(-ENODEV);
178 pct = (union acpi_object *)buffer.pointer;
179 if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
180 || (pct->package.count != 2)) {
181 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PCT data\n"));
190 obj = pct->package.elements[0];
192 if ((obj.type != ACPI_TYPE_BUFFER)
193 || (obj.buffer.length < sizeof(struct acpi_pct_register))
194 || (obj.buffer.pointer == NULL)) {
195 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
196 "Invalid _PCT data (control_register)\n"));
200 memcpy(&pr->performance->control_register, obj.buffer.pointer,
201 sizeof(struct acpi_pct_register));
207 obj = pct->package.elements[1];
209 if ((obj.type != ACPI_TYPE_BUFFER)
210 || (obj.buffer.length < sizeof(struct acpi_pct_register))
211 || (obj.buffer.pointer == NULL)) {
212 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
213 "Invalid _PCT data (status_register)\n"));
218 memcpy(&pr->performance->status_register, obj.buffer.pointer,
219 sizeof(struct acpi_pct_register));
222 acpi_os_free(buffer.pointer);
224 return_VALUE(result);
227 static int acpi_processor_get_performance_states(struct acpi_processor *pr)
230 acpi_status status = AE_OK;
231 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
232 struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
233 struct acpi_buffer state = { 0, NULL };
234 union acpi_object *pss = NULL;
237 ACPI_FUNCTION_TRACE("acpi_processor_get_performance_states");
239 status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
240 if (ACPI_FAILURE(status)) {
241 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PSS\n"));
242 return_VALUE(-ENODEV);
245 pss = (union acpi_object *)buffer.pointer;
246 if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
247 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSS data\n"));
252 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
253 pss->package.count));
255 pr->performance->state_count = pss->package.count;
256 pr->performance->states =
257 kmalloc(sizeof(struct acpi_processor_px) * pss->package.count,
259 if (!pr->performance->states) {
264 for (i = 0; i < pr->performance->state_count; i++) {
266 struct acpi_processor_px *px = &(pr->performance->states[i]);
268 state.length = sizeof(struct acpi_processor_px);
271 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
273 status = acpi_extract_package(&(pss->package.elements[i]),
275 if (ACPI_FAILURE(status)) {
276 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
277 "Invalid _PSS data\n"));
279 kfree(pr->performance->states);
283 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
284 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
286 (u32) px->core_frequency,
288 (u32) px->transition_latency,
289 (u32) px->bus_master_latency,
290 (u32) px->control, (u32) px->status));
292 if (!px->core_frequency) {
293 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
294 "Invalid _PSS data: freq is zero\n"));
296 kfree(pr->performance->states);
302 acpi_os_free(buffer.pointer);
304 return_VALUE(result);
307 static int acpi_processor_get_performance_info(struct acpi_processor *pr)
310 acpi_status status = AE_OK;
311 acpi_handle handle = NULL;
313 ACPI_FUNCTION_TRACE("acpi_processor_get_performance_info");
315 if (!pr || !pr->performance || !pr->handle)
316 return_VALUE(-EINVAL);
318 status = acpi_get_handle(pr->handle, "_PCT", &handle);
319 if (ACPI_FAILURE(status)) {
320 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
321 "ACPI-based processor performance control unavailable\n"));
322 return_VALUE(-ENODEV);
325 result = acpi_processor_get_performance_control(pr);
327 return_VALUE(result);
329 result = acpi_processor_get_performance_states(pr);
331 return_VALUE(result);
333 result = acpi_processor_get_platform_limit(pr);
335 return_VALUE(result);
340 int acpi_processor_notify_smm(struct module *calling_module)
343 static int is_done = 0;
345 ACPI_FUNCTION_TRACE("acpi_processor_notify_smm");
347 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
348 return_VALUE(-EBUSY);
350 if (!try_module_get(calling_module))
351 return_VALUE(-EINVAL);
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);
363 return_VALUE(is_done);
368 /* Can't write pstate_cnt to smi_cmd if either value is zero */
369 if ((!acpi_fadt.smi_cmd) || (!acpi_fadt.pstate_cnt)) {
370 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_cnt\n"));
371 module_put(calling_module);
375 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
376 "Writing pstate_cnt [0x%x] to smi_cmd [0x%x]\n",
377 acpi_fadt.pstate_cnt, acpi_fadt.smi_cmd));
379 /* FADT v1 doesn't support pstate_cnt, many BIOS vendors use
380 * it anyway, so we need to support it... */
381 if (acpi_fadt_is_v1) {
382 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
383 "Using v1.0 FADT reserved value for pstate_cnt\n"));
386 status = acpi_os_write_port(acpi_fadt.smi_cmd,
387 (u32) acpi_fadt.pstate_cnt, 8);
388 if (ACPI_FAILURE(status)) {
389 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
390 "Failed to write pstate_cnt [0x%x] to "
391 "smi_cmd [0x%x]\n", acpi_fadt.pstate_cnt,
393 module_put(calling_module);
394 return_VALUE(status);
397 /* Success. If there's no _PPC, we need to fear nothing, so
398 * we can allow the cpufreq driver to be rmmod'ed. */
401 if (!(acpi_processor_ppc_status & PPC_IN_USE))
402 module_put(calling_module);
407 EXPORT_SYMBOL(acpi_processor_notify_smm);
409 #ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
410 /* /proc/acpi/processor/../performance interface (DEPRECATED) */
412 static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file);
413 static struct file_operations acpi_processor_perf_fops = {
414 .open = acpi_processor_perf_open_fs,
417 .release = single_release,
420 static int acpi_processor_perf_seq_show(struct seq_file *seq, void *offset)
422 struct acpi_processor *pr = (struct acpi_processor *)seq->private;
425 ACPI_FUNCTION_TRACE("acpi_processor_perf_seq_show");
430 if (!pr->performance) {
431 seq_puts(seq, "<not supported>\n");
435 seq_printf(seq, "state count: %d\n"
436 "active state: P%d\n",
437 pr->performance->state_count, pr->performance->state);
439 seq_puts(seq, "states:\n");
440 for (i = 0; i < pr->performance->state_count; i++)
442 " %cP%d: %d MHz, %d mW, %d uS\n",
443 (i == pr->performance->state ? '*' : ' '), i,
444 (u32) pr->performance->states[i].core_frequency,
445 (u32) pr->performance->states[i].power,
446 (u32) pr->performance->states[i].transition_latency);
452 static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file)
454 return single_open(file, acpi_processor_perf_seq_show,
459 acpi_processor_write_performance(struct file *file,
460 const char __user * buffer,
461 size_t count, loff_t * data)
464 struct seq_file *m = (struct seq_file *)file->private_data;
465 struct acpi_processor *pr = (struct acpi_processor *)m->private;
466 struct acpi_processor_performance *perf;
467 char state_string[12] = { '\0' };
468 unsigned int new_state = 0;
469 struct cpufreq_policy policy;
471 ACPI_FUNCTION_TRACE("acpi_processor_write_performance");
473 if (!pr || (count > sizeof(state_string) - 1))
474 return_VALUE(-EINVAL);
476 perf = pr->performance;
478 return_VALUE(-EINVAL);
480 if (copy_from_user(state_string, buffer, count))
481 return_VALUE(-EFAULT);
483 state_string[count] = '\0';
484 new_state = simple_strtoul(state_string, NULL, 0);
486 if (new_state >= perf->state_count)
487 return_VALUE(-EINVAL);
489 cpufreq_get_policy(&policy, pr->id);
492 policy.min = perf->states[new_state].core_frequency * 1000;
493 policy.max = perf->states[new_state].core_frequency * 1000;
495 result = cpufreq_set_policy(&policy);
497 return_VALUE(result);
502 static void acpi_cpufreq_add_file(struct acpi_processor *pr)
504 struct proc_dir_entry *entry = NULL;
505 struct acpi_device *device = NULL;
507 ACPI_FUNCTION_TRACE("acpi_cpufreq_addfile");
509 if (acpi_bus_get_device(pr->handle, &device))
512 /* add file 'performance' [R/W] */
513 entry = create_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
514 S_IFREG | S_IRUGO | S_IWUSR,
515 acpi_device_dir(device));
517 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
518 "Unable to create '%s' fs entry\n",
519 ACPI_PROCESSOR_FILE_PERFORMANCE));
521 acpi_processor_perf_fops.write = acpi_processor_write_performance;
522 entry->proc_fops = &acpi_processor_perf_fops;
523 entry->data = acpi_driver_data(device);
524 entry->owner = THIS_MODULE;
529 static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
531 struct acpi_device *device = NULL;
533 ACPI_FUNCTION_TRACE("acpi_cpufreq_addfile");
535 if (acpi_bus_get_device(pr->handle, &device))
538 /* remove file 'performance' */
539 remove_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
540 acpi_device_dir(device));
546 static void acpi_cpufreq_add_file(struct acpi_processor *pr)
550 static void acpi_cpufreq_remove_file(struct acpi_processor *pr)
554 #endif /* CONFIG_X86_ACPI_CPUFREQ_PROC_INTF */
557 acpi_processor_register_performance(struct acpi_processor_performance
558 *performance, unsigned int cpu)
560 struct acpi_processor *pr;
562 ACPI_FUNCTION_TRACE("acpi_processor_register_performance");
564 if (!(acpi_processor_ppc_status & PPC_REGISTERED))
565 return_VALUE(-EINVAL);
567 down(&performance_sem);
569 pr = processors[cpu];
571 up(&performance_sem);
572 return_VALUE(-ENODEV);
575 if (pr->performance) {
576 up(&performance_sem);
577 return_VALUE(-EBUSY);
580 pr->performance = performance;
582 if (acpi_processor_get_performance_info(pr)) {
583 pr->performance = NULL;
584 up(&performance_sem);
588 acpi_cpufreq_add_file(pr);
590 up(&performance_sem);
594 EXPORT_SYMBOL(acpi_processor_register_performance);
597 acpi_processor_unregister_performance(struct acpi_processor_performance
598 *performance, unsigned int cpu)
600 struct acpi_processor *pr;
602 ACPI_FUNCTION_TRACE("acpi_processor_unregister_performance");
604 down(&performance_sem);
606 pr = processors[cpu];
608 up(&performance_sem);
612 kfree(pr->performance->states);
613 pr->performance = NULL;
615 acpi_cpufreq_remove_file(pr);
617 up(&performance_sem);
622 EXPORT_SYMBOL(acpi_processor_unregister_performance);