2 * processor_throttling.c - Throttling submodule of the ACPI processor driver
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
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/cpufreq.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
37 #include <asm/uaccess.h>
39 #include <acpi/acpi_bus.h>
40 #include <acpi/processor.h>
42 #define ACPI_PROCESSOR_COMPONENT 0x01000000
43 #define ACPI_PROCESSOR_CLASS "processor"
44 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
45 ACPI_MODULE_NAME("processor_throttling");
47 static int acpi_processor_get_throttling(struct acpi_processor
*pr
);
48 int acpi_processor_set_throttling(struct acpi_processor
*pr
, int state
);
51 * _TPC - Throttling Present Capabilities
53 static int acpi_processor_get_platform_limit(struct acpi_processor
*pr
)
55 acpi_status status
= 0;
56 unsigned long tpc
= 0;
60 status
= acpi_evaluate_integer(pr
->handle
, "_TPC", NULL
, &tpc
);
61 if (ACPI_FAILURE(status
)) {
62 if (status
!= AE_NOT_FOUND
) {
63 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TPC"));
67 pr
->throttling_platform_limit
= (int)tpc
;
71 int acpi_processor_tstate_has_changed(struct acpi_processor
*pr
)
73 return acpi_processor_get_platform_limit(pr
);
77 * _PTC - Processor Throttling Control (and status) register location
79 static int acpi_processor_get_throttling_control(struct acpi_processor
*pr
)
82 acpi_status status
= 0;
83 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
84 union acpi_object
*ptc
= NULL
;
85 union acpi_object obj
= { 0 };
87 status
= acpi_evaluate_object(pr
->handle
, "_PTC", NULL
, &buffer
);
88 if (ACPI_FAILURE(status
)) {
89 if (status
!= AE_NOT_FOUND
) {
90 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PTC"));
95 ptc
= (union acpi_object
*)buffer
.pointer
;
96 if (!ptc
|| (ptc
->type
!= ACPI_TYPE_PACKAGE
)
97 || (ptc
->package
.count
!= 2)) {
98 printk(KERN_ERR PREFIX
"Invalid _PTC data\n");
107 obj
= ptc
->package
.elements
[0];
109 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
110 || (obj
.buffer
.length
< sizeof(struct acpi_ptc_register
))
111 || (obj
.buffer
.pointer
== NULL
)) {
112 printk(KERN_ERR PREFIX
113 "Invalid _PTC data (control_register)\n");
117 memcpy(&pr
->throttling
.control_register
, obj
.buffer
.pointer
,
118 sizeof(struct acpi_ptc_register
));
124 obj
= ptc
->package
.elements
[1];
126 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
127 || (obj
.buffer
.length
< sizeof(struct acpi_ptc_register
))
128 || (obj
.buffer
.pointer
== NULL
)) {
129 printk(KERN_ERR PREFIX
"Invalid _PTC data (status_register)\n");
134 memcpy(&pr
->throttling
.status_register
, obj
.buffer
.pointer
,
135 sizeof(struct acpi_ptc_register
));
138 kfree(buffer
.pointer
);
144 * _TSS - Throttling Supported States
146 static int acpi_processor_get_throttling_states(struct acpi_processor
*pr
)
149 acpi_status status
= AE_OK
;
150 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
151 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
152 struct acpi_buffer state
= { 0, NULL
};
153 union acpi_object
*tss
= NULL
;
156 status
= acpi_evaluate_object(pr
->handle
, "_TSS", NULL
, &buffer
);
157 if (ACPI_FAILURE(status
)) {
158 if (status
!= AE_NOT_FOUND
) {
159 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TSS"));
164 tss
= buffer
.pointer
;
165 if (!tss
|| (tss
->type
!= ACPI_TYPE_PACKAGE
)) {
166 printk(KERN_ERR PREFIX
"Invalid _TSS data\n");
171 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d throttling states\n",
172 tss
->package
.count
));
174 pr
->throttling
.state_count
= tss
->package
.count
;
175 pr
->throttling
.states_tss
=
176 kmalloc(sizeof(struct acpi_processor_tx_tss
) * tss
->package
.count
,
178 if (!pr
->throttling
.states_tss
) {
183 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
185 struct acpi_processor_tx_tss
*tx
=
186 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
189 state
.length
= sizeof(struct acpi_processor_tx_tss
);
192 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Extracting state %d\n", i
));
194 status
= acpi_extract_package(&(tss
->package
.elements
[i
]),
196 if (ACPI_FAILURE(status
)) {
197 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _TSS data"));
199 kfree(pr
->throttling
.states_tss
);
203 if (!tx
->freqpercentage
) {
204 printk(KERN_ERR PREFIX
205 "Invalid _TSS data: freq is zero\n");
207 kfree(pr
->throttling
.states_tss
);
213 kfree(buffer
.pointer
);
219 * _TSD - T-State Dependencies
221 static int acpi_processor_get_tsd(struct acpi_processor
*pr
)
224 acpi_status status
= AE_OK
;
225 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
226 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
227 struct acpi_buffer state
= { 0, NULL
};
228 union acpi_object
*tsd
= NULL
;
229 struct acpi_tsd_package
*pdomain
;
231 status
= acpi_evaluate_object(pr
->handle
, "_TSD", NULL
, &buffer
);
232 if (ACPI_FAILURE(status
)) {
233 if (status
!= AE_NOT_FOUND
) {
234 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TSD"));
239 tsd
= buffer
.pointer
;
240 if (!tsd
|| (tsd
->type
!= ACPI_TYPE_PACKAGE
)) {
241 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid _TSD data\n"));
246 if (tsd
->package
.count
!= 1) {
247 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid _TSD data\n"));
252 pdomain
= &(pr
->throttling
.domain_info
);
254 state
.length
= sizeof(struct acpi_tsd_package
);
255 state
.pointer
= pdomain
;
257 status
= acpi_extract_package(&(tsd
->package
.elements
[0]),
259 if (ACPI_FAILURE(status
)) {
260 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Invalid _TSD data\n"));
265 if (pdomain
->num_entries
!= ACPI_TSD_REV0_ENTRIES
) {
266 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Unknown _TSD:num_entries\n"));
271 if (pdomain
->revision
!= ACPI_TSD_REV0_REVISION
) {
272 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Unknown _TSD:revision\n"));
278 kfree(buffer
.pointer
);
282 /* --------------------------------------------------------------------------
284 -------------------------------------------------------------------------- */
285 static int acpi_processor_get_throttling_fadt(struct acpi_processor
*pr
)
295 if (!pr
->flags
.throttling
)
298 pr
->throttling
.state
= 0;
300 duty_mask
= pr
->throttling
.state_count
- 1;
302 duty_mask
<<= pr
->throttling
.duty_offset
;
306 value
= inl(pr
->throttling
.address
);
309 * Compute the current throttling state when throttling is enabled
313 duty_value
= value
& duty_mask
;
314 duty_value
>>= pr
->throttling
.duty_offset
;
317 state
= pr
->throttling
.state_count
- duty_value
;
320 pr
->throttling
.state
= state
;
324 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
325 "Throttling state is T%d (%d%% throttling applied)\n",
326 state
, pr
->throttling
.states
[state
].performance
));
331 static int acpi_read_throttling_status(struct acpi_processor_throttling
335 switch (throttling
->status_register
.space_id
) {
336 case ACPI_ADR_SPACE_SYSTEM_IO
:
337 acpi_os_read_port((acpi_io_address
) throttling
->status_register
.
339 (u32
) throttling
->status_register
.bit_width
*
342 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
343 printk(KERN_ERR PREFIX
344 "HARDWARE addr space,NOT supported yet\n");
347 printk(KERN_ERR PREFIX
"Unknown addr space %d\n",
348 (u32
) (throttling
->status_register
.space_id
));
353 static int acpi_write_throttling_state(struct acpi_processor_throttling
354 *throttling
, int value
)
358 switch (throttling
->control_register
.space_id
) {
359 case ACPI_ADR_SPACE_SYSTEM_IO
:
360 acpi_os_write_port((acpi_io_address
) throttling
->
361 control_register
.address
, value
,
362 (u32
) throttling
->control_register
.
366 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
367 printk(KERN_ERR PREFIX
368 "HARDWARE addr space,NOT supported yet\n");
371 printk(KERN_ERR PREFIX
"Unknown addr space %d\n",
372 (u32
) (throttling
->control_register
.space_id
));
377 static int acpi_get_throttling_state(struct acpi_processor
*pr
, int value
)
381 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
382 struct acpi_processor_tx_tss
*tx
=
383 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
385 if (tx
->control
== value
)
388 if (i
> pr
->throttling
.state_count
)
393 static int acpi_get_throttling_value(struct acpi_processor
*pr
, int state
)
396 if (state
>= 0 && state
<= pr
->throttling
.state_count
) {
397 struct acpi_processor_tx_tss
*tx
=
398 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
405 static int acpi_processor_get_throttling_ptc(struct acpi_processor
*pr
)
413 if (!pr
->flags
.throttling
)
416 pr
->throttling
.state
= 0;
418 value
= acpi_read_throttling_status(&pr
->throttling
);
420 state
= acpi_get_throttling_state(pr
, value
);
421 pr
->throttling
.state
= state
;
428 static int acpi_processor_get_throttling(struct acpi_processor
*pr
)
430 return pr
->throttling
.acpi_processor_get_throttling(pr
);
433 static int acpi_processor_set_throttling_fadt(struct acpi_processor
*pr
,
443 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
446 if (!pr
->flags
.throttling
)
449 if (state
== pr
->throttling
.state
)
452 if (state
< pr
->throttling_platform_limit
)
455 * Calculate the duty_value and duty_mask.
458 duty_value
= pr
->throttling
.state_count
- state
;
460 duty_value
<<= pr
->throttling
.duty_offset
;
462 /* Used to clear all duty_value bits */
463 duty_mask
= pr
->throttling
.state_count
- 1;
465 duty_mask
<<= acpi_gbl_FADT
.duty_offset
;
466 duty_mask
= ~duty_mask
;
472 * Disable throttling by writing a 0 to bit 4. Note that we must
473 * turn it off before you can change the duty_value.
475 value
= inl(pr
->throttling
.address
);
478 outl(value
, pr
->throttling
.address
);
482 * Write the new duty_value and then enable throttling. Note
483 * that a state value of 0 leaves throttling disabled.
488 outl(value
, pr
->throttling
.address
);
491 outl(value
, pr
->throttling
.address
);
494 pr
->throttling
.state
= state
;
498 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
499 "Throttling state set to T%d (%d%%)\n", state
,
500 (pr
->throttling
.states
[state
].performance
? pr
->
501 throttling
.states
[state
].performance
/ 10 : 0)));
506 static int acpi_processor_set_throttling_ptc(struct acpi_processor
*pr
,
514 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
517 if (!pr
->flags
.throttling
)
520 if (state
== pr
->throttling
.state
)
523 if (state
< pr
->throttling_platform_limit
)
528 value
= acpi_get_throttling_value(pr
, state
);
530 acpi_write_throttling_state(&pr
->throttling
, value
);
531 pr
->throttling
.state
= state
;
538 int acpi_processor_set_throttling(struct acpi_processor
*pr
, int state
)
540 return pr
->throttling
.acpi_processor_set_throttling(pr
, state
);
543 int acpi_processor_get_throttling_info(struct acpi_processor
*pr
)
549 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
550 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
551 pr
->throttling
.address
,
552 pr
->throttling
.duty_offset
,
553 pr
->throttling
.duty_width
));
559 * Evaluate _PTC, _TSS and _TPC
560 * They must all be present or none of them can be used.
562 if (acpi_processor_get_throttling_control(pr
) ||
563 acpi_processor_get_throttling_states(pr
) ||
564 acpi_processor_get_platform_limit(pr
))
566 pr
->throttling
.acpi_processor_get_throttling
=
567 &acpi_processor_get_throttling_fadt
;
568 pr
->throttling
.acpi_processor_set_throttling
=
569 &acpi_processor_set_throttling_fadt
;
571 pr
->throttling
.acpi_processor_get_throttling
=
572 &acpi_processor_get_throttling_ptc
;
573 pr
->throttling
.acpi_processor_set_throttling
=
574 &acpi_processor_set_throttling_ptc
;
577 acpi_processor_get_tsd(pr
);
579 if (!pr
->throttling
.address
) {
580 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No throttling register\n"));
582 } else if (!pr
->throttling
.duty_width
) {
583 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No throttling states\n"));
586 /* TBD: Support duty_cycle values that span bit 4. */
587 else if ((pr
->throttling
.duty_offset
+ pr
->throttling
.duty_width
) > 4) {
588 printk(KERN_WARNING PREFIX
"duty_cycle spans bit 4\n");
593 * PIIX4 Errata: We don't support throttling on the original PIIX4.
594 * This shouldn't be an issue as few (if any) mobile systems ever
597 if (errata
.piix4
.throttle
) {
598 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
599 "Throttling not supported on PIIX4 A- or B-step\n"));
603 pr
->throttling
.state_count
= 1 << acpi_gbl_FADT
.duty_width
;
606 * Compute state values. Note that throttling displays a linear power/
607 * performance relationship (at 50% performance the CPU will consume
608 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
611 step
= (1000 / pr
->throttling
.state_count
);
613 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
614 pr
->throttling
.states
[i
].performance
= step
* i
;
615 pr
->throttling
.states
[i
].power
= step
* i
;
618 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d throttling states\n",
619 pr
->throttling
.state_count
));
621 pr
->flags
.throttling
= 1;
624 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
625 * thermal) decide to lower performance if it so chooses, but for now
626 * we'll crank up the speed.
629 result
= acpi_processor_get_throttling(pr
);
633 if (pr
->throttling
.state
) {
634 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
635 "Disabling throttling (was T%d)\n",
636 pr
->throttling
.state
));
637 result
= acpi_processor_set_throttling(pr
, 0);
644 pr
->flags
.throttling
= 0;
651 static int acpi_processor_throttling_seq_show(struct seq_file
*seq
,
654 struct acpi_processor
*pr
= seq
->private;
661 if (!(pr
->throttling
.state_count
> 0)) {
662 seq_puts(seq
, "<not supported>\n");
666 result
= acpi_processor_get_throttling(pr
);
670 "Could not determine current throttling state.\n");
674 seq_printf(seq
, "state count: %d\n"
675 "active state: T%d\n"
676 "state available: T%d to T%d\n",
677 pr
->throttling
.state_count
, pr
->throttling
.state
,
678 pr
->throttling_platform_limit
,
679 pr
->throttling
.state_count
- 1);
681 seq_puts(seq
, "states:\n");
682 if (pr
->throttling
.acpi_processor_get_throttling
==
683 acpi_processor_get_throttling_fadt
) {
684 for (i
= 0; i
< pr
->throttling
.state_count
; i
++)
685 seq_printf(seq
, " %cT%d: %02d%%\n",
686 (i
== pr
->throttling
.state
? '*' : ' '), i
,
687 (pr
->throttling
.states
[i
].performance
? pr
->
688 throttling
.states
[i
].performance
/ 10 : 0));
690 for (i
= 0; i
< pr
->throttling
.state_count
; i
++)
691 seq_printf(seq
, " %cT%d: %02d%%\n",
692 (i
== pr
->throttling
.state
? '*' : ' '), i
,
693 (int)pr
->throttling
.states_tss
[i
].
701 static int acpi_processor_throttling_open_fs(struct inode
*inode
,
704 return single_open(file
, acpi_processor_throttling_seq_show
,
708 static ssize_t
acpi_processor_write_throttling(struct file
*file
,
709 const char __user
* buffer
,
710 size_t count
, loff_t
* data
)
713 struct seq_file
*m
= file
->private_data
;
714 struct acpi_processor
*pr
= m
->private;
715 char state_string
[12] = { '\0' };
717 if (!pr
|| (count
> sizeof(state_string
) - 1))
720 if (copy_from_user(state_string
, buffer
, count
))
723 state_string
[count
] = '\0';
725 result
= acpi_processor_set_throttling(pr
,
726 simple_strtoul(state_string
,
734 struct file_operations acpi_processor_throttling_fops
= {
735 .open
= acpi_processor_throttling_open_fs
,
737 .write
= acpi_processor_write_throttling
,
739 .release
= single_release
,