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.
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/cpufreq.h>
35 #ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
36 #include <linux/proc_fs.h>
37 #include <linux/seq_file.h>
39 #include <asm/uaccess.h>
42 #include <acpi/acpi_bus.h>
43 #include <acpi/processor.h>
46 #define ACPI_PROCESSOR_COMPONENT 0x01000000
47 #define ACPI_PROCESSOR_CLASS "processor"
48 #define ACPI_PROCESSOR_DRIVER_NAME "ACPI Processor Driver"
49 #define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
50 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
51 ACPI_MODULE_NAME ("acpi_processor")
54 static DECLARE_MUTEX(performance_sem
);
57 * _PPC support is implemented as a CPUfreq policy notifier:
58 * This means each time a CPUfreq driver registered also with
59 * the ACPI core is asked to change the speed policy, the maximum
60 * value is adjusted so that it is within the platform limit.
62 * Also, when a new platform limit value is detected, the CPUfreq
63 * policy is adjusted accordingly.
66 #define PPC_REGISTERED 1
69 static int acpi_processor_ppc_status
= 0;
71 static int acpi_processor_ppc_notifier(struct notifier_block
*nb
,
75 struct cpufreq_policy
*policy
= data
;
76 struct acpi_processor
*pr
;
79 down(&performance_sem
);
81 if (event
!= CPUFREQ_INCOMPATIBLE
)
84 pr
= processors
[policy
->cpu
];
85 if (!pr
|| !pr
->performance
)
88 ppc
= (unsigned int) pr
->performance_platform_limit
;
92 if (ppc
> pr
->performance
->state_count
)
95 cpufreq_verify_within_limits(policy
, 0,
96 pr
->performance
->states
[ppc
].core_frequency
* 1000);
105 static struct notifier_block acpi_ppc_notifier_block
= {
106 .notifier_call
= acpi_processor_ppc_notifier
,
111 acpi_processor_get_platform_limit (
112 struct acpi_processor
* pr
)
114 acpi_status status
= 0;
115 unsigned long ppc
= 0;
117 ACPI_FUNCTION_TRACE("acpi_processor_get_platform_limit");
120 return_VALUE(-EINVAL
);
123 * _PPC indicates the maximum state currently supported by the platform
124 * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
126 status
= acpi_evaluate_integer(pr
->handle
, "_PPC", NULL
, &ppc
);
128 if (status
!= AE_NOT_FOUND
)
129 acpi_processor_ppc_status
|= PPC_IN_USE
;
131 if(ACPI_FAILURE(status
) && status
!= AE_NOT_FOUND
) {
132 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Error evaluating _PPC\n"));
133 return_VALUE(-ENODEV
);
136 pr
->performance_platform_limit
= (int) ppc
;
142 int acpi_processor_ppc_has_changed(
143 struct acpi_processor
*pr
)
145 int ret
= acpi_processor_get_platform_limit(pr
);
149 return cpufreq_update_policy(pr
->id
);
153 void acpi_processor_ppc_init(void) {
154 if (!cpufreq_register_notifier(&acpi_ppc_notifier_block
, CPUFREQ_POLICY_NOTIFIER
))
155 acpi_processor_ppc_status
|= PPC_REGISTERED
;
157 printk(KERN_DEBUG
"Warning: Processor Platform Limit not supported.\n");
161 void acpi_processor_ppc_exit(void) {
162 if (acpi_processor_ppc_status
& PPC_REGISTERED
)
163 cpufreq_unregister_notifier(&acpi_ppc_notifier_block
, CPUFREQ_POLICY_NOTIFIER
);
165 acpi_processor_ppc_status
&= ~PPC_REGISTERED
;
169 * when registering a cpufreq driver with this ACPI processor driver, the
170 * _PCT and _PSS structures are read out and written into struct
171 * acpi_processor_performance.
173 static int acpi_processor_set_pdc (struct acpi_processor
*pr
)
175 acpi_status status
= AE_OK
;
177 union acpi_object arg0
= {ACPI_TYPE_BUFFER
};
178 struct acpi_object_list no_object
= {1, &arg0
};
179 struct acpi_object_list
*pdc
;
181 ACPI_FUNCTION_TRACE("acpi_processor_set_pdc");
183 arg0
.buffer
.length
= 12;
184 arg0
.buffer
.pointer
= (u8
*) arg0_buf
;
185 arg0_buf
[0] = ACPI_PDC_REVISION_ID
;
189 pdc
= (pr
->performance
->pdc
) ? pr
->performance
->pdc
: &no_object
;
191 status
= acpi_evaluate_object(pr
->handle
, "_PDC", pdc
, NULL
);
193 if ((ACPI_FAILURE(status
)) && (pr
->performance
->pdc
))
194 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Error evaluating _PDC, using legacy perf. control...\n"));
196 return_VALUE(status
);
201 acpi_processor_get_performance_control (
202 struct acpi_processor
*pr
)
205 acpi_status status
= 0;
206 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
207 union acpi_object
*pct
= NULL
;
208 union acpi_object obj
= {0};
210 ACPI_FUNCTION_TRACE("acpi_processor_get_performance_control");
212 status
= acpi_evaluate_object(pr
->handle
, "_PCT", NULL
, &buffer
);
213 if(ACPI_FAILURE(status
)) {
214 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Error evaluating _PCT\n"));
215 return_VALUE(-ENODEV
);
218 pct
= (union acpi_object
*) buffer
.pointer
;
219 if (!pct
|| (pct
->type
!= ACPI_TYPE_PACKAGE
)
220 || (pct
->package
.count
!= 2)) {
221 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid _PCT data\n"));
230 obj
= pct
->package
.elements
[0];
232 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
233 || (obj
.buffer
.length
< sizeof(struct acpi_pct_register
))
234 || (obj
.buffer
.pointer
== NULL
)) {
235 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
236 "Invalid _PCT data (control_register)\n"));
240 memcpy(&pr
->performance
->control_register
, obj
.buffer
.pointer
, sizeof(struct acpi_pct_register
));
247 obj
= pct
->package
.elements
[1];
249 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
250 || (obj
.buffer
.length
< sizeof(struct acpi_pct_register
))
251 || (obj
.buffer
.pointer
== NULL
)) {
252 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
253 "Invalid _PCT data (status_register)\n"));
258 memcpy(&pr
->performance
->status_register
, obj
.buffer
.pointer
, sizeof(struct acpi_pct_register
));
261 acpi_os_free(buffer
.pointer
);
263 return_VALUE(result
);
268 acpi_processor_get_performance_states (
269 struct acpi_processor
*pr
)
272 acpi_status status
= AE_OK
;
273 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
274 struct acpi_buffer format
= {sizeof("NNNNNN"), "NNNNNN"};
275 struct acpi_buffer state
= {0, NULL
};
276 union acpi_object
*pss
= NULL
;
279 ACPI_FUNCTION_TRACE("acpi_processor_get_performance_states");
281 status
= acpi_evaluate_object(pr
->handle
, "_PSS", NULL
, &buffer
);
282 if(ACPI_FAILURE(status
)) {
283 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Error evaluating _PSS\n"));
284 return_VALUE(-ENODEV
);
287 pss
= (union acpi_object
*) buffer
.pointer
;
288 if (!pss
|| (pss
->type
!= ACPI_TYPE_PACKAGE
)) {
289 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid _PSS data\n"));
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d performance states\n",
295 pss
->package
.count
));
297 pr
->performance
->state_count
= pss
->package
.count
;
298 pr
->performance
->states
= kmalloc(sizeof(struct acpi_processor_px
) * pss
->package
.count
, GFP_KERNEL
);
299 if (!pr
->performance
->states
) {
304 for (i
= 0; i
< pr
->performance
->state_count
; i
++) {
306 struct acpi_processor_px
*px
= &(pr
->performance
->states
[i
]);
308 state
.length
= sizeof(struct acpi_processor_px
);
311 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Extracting state %d\n", i
));
313 status
= acpi_extract_package(&(pss
->package
.elements
[i
]),
315 if (ACPI_FAILURE(status
)) {
316 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid _PSS data\n"));
318 kfree(pr
->performance
->states
);
322 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
323 "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
325 (u32
) px
->core_frequency
,
327 (u32
) px
->transition_latency
,
328 (u32
) px
->bus_master_latency
,
332 if (!px
->core_frequency
) {
333 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid _PSS data: freq is zero\n"));
335 kfree(pr
->performance
->states
);
341 acpi_os_free(buffer
.pointer
);
343 return_VALUE(result
);
348 acpi_processor_get_performance_info (
349 struct acpi_processor
*pr
)
352 acpi_status status
= AE_OK
;
353 acpi_handle handle
= NULL
;
355 ACPI_FUNCTION_TRACE("acpi_processor_get_performance_info");
357 if (!pr
|| !pr
->performance
|| !pr
->handle
)
358 return_VALUE(-EINVAL
);
360 acpi_processor_set_pdc(pr
);
362 status
= acpi_get_handle(pr
->handle
, "_PCT", &handle
);
363 if (ACPI_FAILURE(status
)) {
364 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
365 "ACPI-based processor performance control unavailable\n"));
366 return_VALUE(-ENODEV
);
369 result
= acpi_processor_get_performance_control(pr
);
371 return_VALUE(result
);
373 result
= acpi_processor_get_performance_states(pr
);
375 return_VALUE(result
);
377 result
= acpi_processor_get_platform_limit(pr
);
379 return_VALUE(result
);
385 int acpi_processor_notify_smm(struct module
*calling_module
) {
387 static int is_done
= 0;
389 ACPI_FUNCTION_TRACE("acpi_processor_notify_smm");
391 if (!(acpi_processor_ppc_status
& PPC_REGISTERED
))
392 return_VALUE(-EBUSY
);
394 if (!try_module_get(calling_module
))
395 return_VALUE(-EINVAL
);
397 /* is_done is set to negative if an error occured,
398 * and to postitive if _no_ error occured, but SMM
399 * was already notified. This avoids double notification
400 * which might lead to unexpected results...
403 module_put(calling_module
);
406 else if (is_done
< 0) {
407 module_put(calling_module
);
408 return_VALUE(is_done
);
413 /* Can't write pstate_cnt to smi_cmd if either value is zero */
414 if ((!acpi_fadt
.smi_cmd
) ||
415 (!acpi_fadt
.pstate_cnt
)) {
416 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
417 "No SMI port or pstate_cnt\n"));
418 module_put(calling_module
);
422 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Writing pstate_cnt [0x%x] to smi_cmd [0x%x]\n", acpi_fadt
.pstate_cnt
, acpi_fadt
.smi_cmd
));
424 /* FADT v1 doesn't support pstate_cnt, many BIOS vendors use
425 * it anyway, so we need to support it... */
426 if (acpi_fadt_is_v1
) {
427 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Using v1.0 FADT reserved value for pstate_cnt\n"));
430 status
= acpi_os_write_port (acpi_fadt
.smi_cmd
,
431 (u32
) acpi_fadt
.pstate_cnt
, 8);
432 if (ACPI_FAILURE (status
)) {
433 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
434 "Failed to write pstate_cnt [0x%x] to "
435 "smi_cmd [0x%x]\n", acpi_fadt
.pstate_cnt
, acpi_fadt
.smi_cmd
));
436 module_put(calling_module
);
437 return_VALUE(status
);
440 /* Success. If there's no _PPC, we need to fear nothing, so
441 * we can allow the cpufreq driver to be rmmod'ed. */
444 if (!(acpi_processor_ppc_status
& PPC_IN_USE
))
445 module_put(calling_module
);
449 EXPORT_SYMBOL(acpi_processor_notify_smm
);
452 #ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
453 /* /proc/acpi/processor/../performance interface (DEPRECATED) */
455 static int acpi_processor_perf_open_fs(struct inode
*inode
, struct file
*file
);
456 static struct file_operations acpi_processor_perf_fops
= {
457 .open
= acpi_processor_perf_open_fs
,
460 .release
= single_release
,
463 static int acpi_processor_perf_seq_show(struct seq_file
*seq
, void *offset
)
465 struct acpi_processor
*pr
= (struct acpi_processor
*)seq
->private;
468 ACPI_FUNCTION_TRACE("acpi_processor_perf_seq_show");
473 if (!pr
->performance
) {
474 seq_puts(seq
, "<not supported>\n");
478 seq_printf(seq
, "state count: %d\n"
479 "active state: P%d\n",
480 pr
->performance
->state_count
,
481 pr
->performance
->state
);
483 seq_puts(seq
, "states:\n");
484 for (i
= 0; i
< pr
->performance
->state_count
; i
++)
485 seq_printf(seq
, " %cP%d: %d MHz, %d mW, %d uS\n",
486 (i
== pr
->performance
->state
?'*':' '), i
,
487 (u32
) pr
->performance
->states
[i
].core_frequency
,
488 (u32
) pr
->performance
->states
[i
].power
,
489 (u32
) pr
->performance
->states
[i
].transition_latency
);
495 static int acpi_processor_perf_open_fs(struct inode
*inode
, struct file
*file
)
497 return single_open(file
, acpi_processor_perf_seq_show
,
502 acpi_processor_write_performance (
504 const char __user
*buffer
,
509 struct seq_file
*m
= (struct seq_file
*) file
->private_data
;
510 struct acpi_processor
*pr
= (struct acpi_processor
*) m
->private;
511 struct acpi_processor_performance
*perf
;
512 char state_string
[12] = {'\0'};
513 unsigned int new_state
= 0;
514 struct cpufreq_policy policy
;
516 ACPI_FUNCTION_TRACE("acpi_processor_write_performance");
518 if (!pr
|| (count
> sizeof(state_string
) - 1))
519 return_VALUE(-EINVAL
);
521 perf
= pr
->performance
;
523 return_VALUE(-EINVAL
);
525 if (copy_from_user(state_string
, buffer
, count
))
526 return_VALUE(-EFAULT
);
528 state_string
[count
] = '\0';
529 new_state
= simple_strtoul(state_string
, NULL
, 0);
531 if (new_state
>= perf
->state_count
)
532 return_VALUE(-EINVAL
);
534 cpufreq_get_policy(&policy
, pr
->id
);
537 policy
.min
= perf
->states
[new_state
].core_frequency
* 1000;
538 policy
.max
= perf
->states
[new_state
].core_frequency
* 1000;
540 result
= cpufreq_set_policy(&policy
);
542 return_VALUE(result
);
548 acpi_cpufreq_add_file (
549 struct acpi_processor
*pr
)
551 struct proc_dir_entry
*entry
= NULL
;
552 struct acpi_device
*device
= NULL
;
554 ACPI_FUNCTION_TRACE("acpi_cpufreq_addfile");
556 if (acpi_bus_get_device(pr
->handle
, &device
))
559 /* add file 'performance' [R/W] */
560 entry
= create_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE
,
561 S_IFREG
|S_IRUGO
|S_IWUSR
, acpi_device_dir(device
));
563 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
564 "Unable to create '%s' fs entry\n",
565 ACPI_PROCESSOR_FILE_PERFORMANCE
));
567 entry
->proc_fops
= &acpi_processor_perf_fops
;
568 entry
->proc_fops
->write
= acpi_processor_write_performance
;
569 entry
->data
= acpi_driver_data(device
);
570 entry
->owner
= THIS_MODULE
;
576 acpi_cpufreq_remove_file (
577 struct acpi_processor
*pr
)
579 struct acpi_device
*device
= NULL
;
581 ACPI_FUNCTION_TRACE("acpi_cpufreq_addfile");
583 if (acpi_bus_get_device(pr
->handle
, &device
))
586 /* remove file 'performance' */
587 remove_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE
,
588 acpi_device_dir(device
));
594 static void acpi_cpufreq_add_file (struct acpi_processor
*pr
) { return; }
595 static void acpi_cpufreq_remove_file (struct acpi_processor
*pr
) { return; }
596 #endif /* CONFIG_X86_ACPI_CPUFREQ_PROC_INTF */
600 acpi_processor_register_performance (
601 struct acpi_processor_performance
* performance
,
604 struct acpi_processor
*pr
;
606 ACPI_FUNCTION_TRACE("acpi_processor_register_performance");
608 if (!(acpi_processor_ppc_status
& PPC_REGISTERED
))
609 return_VALUE(-EINVAL
);
611 down(&performance_sem
);
613 pr
= processors
[cpu
];
615 up(&performance_sem
);
616 return_VALUE(-ENODEV
);
619 if (pr
->performance
) {
620 up(&performance_sem
);
621 return_VALUE(-EBUSY
);
624 pr
->performance
= performance
;
626 if (acpi_processor_get_performance_info(pr
)) {
627 pr
->performance
= NULL
;
628 up(&performance_sem
);
632 acpi_cpufreq_add_file(pr
);
634 up(&performance_sem
);
637 EXPORT_SYMBOL(acpi_processor_register_performance
);
641 acpi_processor_unregister_performance (
642 struct acpi_processor_performance
* performance
,
645 struct acpi_processor
*pr
;
647 ACPI_FUNCTION_TRACE("acpi_processor_unregister_performance");
649 down(&performance_sem
);
651 pr
= processors
[cpu
];
653 up(&performance_sem
);
657 kfree(pr
->performance
->states
);
658 pr
->performance
= NULL
;
660 acpi_cpufreq_remove_file(pr
);
662 up(&performance_sem
);
666 EXPORT_SYMBOL(acpi_processor_unregister_performance
);