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);
50 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
52 acpi_status status = 0;
53 unsigned long tpc = 0;
57 status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);
58 if(ACPI_FAILURE(status) && status != AE_NOT_FOUND){
59 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TPC"));
62 pr->throttling_platform_limit = (int)tpc;
66 int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
68 return acpi_processor_get_platform_limit(pr);
71 /* --------------------------------------------------------------------------
72 _PTC, _TSS, _TSD support
73 -------------------------------------------------------------------------- */
74 static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
77 acpi_status status = 0;
78 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
79 union acpi_object *ptc = NULL;
80 union acpi_object obj = { 0 };
82 status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
83 if (ACPI_FAILURE(status)) {
84 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTC"));
88 ptc = (union acpi_object *)buffer.pointer;
89 if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)
90 || (ptc->package.count != 2)) {
91 printk(KERN_ERR PREFIX "Invalid _PTC data\n");
100 obj = ptc->package.elements[0];
102 if ((obj.type != ACPI_TYPE_BUFFER)
103 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
104 || (obj.buffer.pointer == NULL)) {
105 printk(KERN_ERR PREFIX "Invalid _PTC data (control_register)\n");
109 memcpy(&pr->throttling.control_register, obj.buffer.pointer,
110 sizeof(struct acpi_ptc_register));
116 obj = ptc->package.elements[1];
118 if ((obj.type != ACPI_TYPE_BUFFER)
119 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
120 || (obj.buffer.pointer == NULL)) {
121 printk(KERN_ERR PREFIX "Invalid _PTC data (status_register)\n");
126 memcpy(&pr->throttling.status_register, obj.buffer.pointer,
127 sizeof(struct acpi_ptc_register));
130 kfree(buffer.pointer);
134 static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
137 acpi_status status = AE_OK;
138 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
139 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
140 struct acpi_buffer state = { 0, NULL };
141 union acpi_object *tss = NULL;
144 status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
145 if (ACPI_FAILURE(status)) {
146 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS"));
150 tss = buffer.pointer;
151 if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
152 printk(KERN_ERR PREFIX "Invalid _TSS data\n");
157 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
158 tss->package.count));
160 pr->throttling.state_count = tss->package.count;
161 pr->throttling.states_tss =
162 kmalloc(sizeof(struct acpi_processor_tx_tss) * tss->package.count,
164 if (!pr->throttling.states_tss) {
169 for (i = 0; i < pr->throttling.state_count; i++) {
171 struct acpi_processor_tx_tss *tx = (struct acpi_processor_tx_tss *) &(pr->throttling.states_tss[i]);
173 state.length = sizeof(struct acpi_processor_tx_tss);
176 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
178 status = acpi_extract_package(&(tss->package.elements[i]),
180 if (ACPI_FAILURE(status)) {
181 ACPI_EXCEPTION((AE_INFO, status, "Invalid _TSS data"));
183 kfree(pr->throttling.states_tss);
187 if (!tx->freqpercentage) {
188 printk(KERN_ERR PREFIX
189 "Invalid _TSS data: freq is zero\n");
191 kfree(pr->throttling.states_tss);
197 kfree(buffer.pointer);
201 static int acpi_processor_get_tsd(struct acpi_processor *pr)
204 acpi_status status = AE_OK;
205 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
206 struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
207 struct acpi_buffer state = {0, NULL};
208 union acpi_object *tsd = NULL;
209 struct acpi_tsd_package *pdomain;
211 status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
212 if (ACPI_FAILURE(status)) {
216 tsd = buffer.pointer;
217 if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
218 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
223 if (tsd->package.count != 1) {
224 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
229 pdomain = &(pr->throttling.domain_info);
231 state.length = sizeof(struct acpi_tsd_package);
232 state.pointer = pdomain;
234 status = acpi_extract_package(&(tsd->package.elements[0]),
236 if (ACPI_FAILURE(status)) {
237 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
242 if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
243 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:num_entries\n"));
248 if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
249 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:revision\n"));
255 kfree(buffer.pointer);
259 /* --------------------------------------------------------------------------
261 -------------------------------------------------------------------------- */
262 static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
273 if (!pr->flags.throttling)
276 pr->throttling.state = 0;
278 duty_mask = pr->throttling.state_count - 1;
280 duty_mask <<= pr->throttling.duty_offset;
284 value = inl(pr->throttling.address);
287 * Compute the current throttling state when throttling is enabled
291 duty_value = value & duty_mask;
292 duty_value >>= pr->throttling.duty_offset;
295 state = pr->throttling.state_count - duty_value;
298 pr->throttling.state = state;
302 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
303 "Throttling state is T%d (%d%% throttling applied)\n",
304 state, pr->throttling.states[state].performance));
309 static int acpi_read_throttling_status(struct acpi_processor_throttling *throttling)
312 switch (throttling->status_register.space_id) {
313 case ACPI_ADR_SPACE_SYSTEM_IO:
314 acpi_os_read_port((acpi_io_address)throttling->status_register.address,
316 (u32)throttling->status_register.bit_width*8);
318 case ACPI_ADR_SPACE_FIXED_HARDWARE:
319 printk(KERN_ERR PREFIX "HARDWARE addr space,NOT supported yet\n");
322 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
323 (u32) (throttling->status_register.space_id));
328 static int acpi_write_throttling_state(struct acpi_processor_throttling *throttling,int value)
332 switch (throttling->control_register.space_id) {
333 case ACPI_ADR_SPACE_SYSTEM_IO:
334 acpi_os_write_port((acpi_io_address)throttling->control_register.address,
336 (u32)throttling->control_register.bit_width*8);
339 case ACPI_ADR_SPACE_FIXED_HARDWARE:
340 printk(KERN_ERR PREFIX "HARDWARE addr space,NOT supported yet\n");
343 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
344 (u32) (throttling->control_register.space_id));
349 static int acpi_get_throttling_state(struct acpi_processor *pr,int value)
353 for (i = 0; i < pr->throttling.state_count; i++) {
354 struct acpi_processor_tx_tss *tx = (struct acpi_processor_tx_tss *) &(pr->throttling.states_tss[i]);
355 if(tx->control == value)
358 if(i > pr->throttling.state_count)
363 static int acpi_get_throttling_value(struct acpi_processor *pr,int state)
366 if(state >=0 && state <= pr->throttling.state_count){
367 struct acpi_processor_tx_tss *tx = (struct acpi_processor_tx_tss *) &(pr->throttling.states_tss[state]);
373 static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
382 if (!pr->flags.throttling)
385 pr->throttling.state = 0;
387 value = acpi_read_throttling_status(&pr->throttling);
389 state = acpi_get_throttling_state(pr,value);
390 pr->throttling.state = state;
398 static int acpi_processor_get_throttling(struct acpi_processor *pr)
400 return pr->throttling.acpi_processor_get_throttling(pr);
403 int acpi_processor_set_throttling_fadt(struct acpi_processor *pr, int state)
413 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
416 if (!pr->flags.throttling)
419 if (state == pr->throttling.state)
422 if (state < pr->throttling_platform_limit)
425 * Calculate the duty_value and duty_mask.
428 duty_value = pr->throttling.state_count - state;
430 duty_value <<= pr->throttling.duty_offset;
432 /* Used to clear all duty_value bits */
433 duty_mask = pr->throttling.state_count - 1;
435 duty_mask <<= acpi_gbl_FADT.duty_offset;
436 duty_mask = ~duty_mask;
442 * Disable throttling by writing a 0 to bit 4. Note that we must
443 * turn it off before you can change the duty_value.
445 value = inl(pr->throttling.address);
448 outl(value, pr->throttling.address);
452 * Write the new duty_value and then enable throttling. Note
453 * that a state value of 0 leaves throttling disabled.
458 outl(value, pr->throttling.address);
461 outl(value, pr->throttling.address);
464 pr->throttling.state = state;
468 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
469 "Throttling state set to T%d (%d%%)\n", state,
470 (pr->throttling.states[state].performance ? pr->
471 throttling.states[state].performance / 10 : 0)));
476 int acpi_processor_set_throttling_ptc(struct acpi_processor *pr, int state)
483 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
486 if (!pr->flags.throttling)
489 if (state == pr->throttling.state)
492 if (state < pr->throttling_platform_limit)
497 value = acpi_get_throttling_value(pr,state);
499 acpi_write_throttling_state(&pr->throttling,value);
500 pr->throttling.state = state;
507 int acpi_processor_set_throttling(struct acpi_processor *pr, int state)
509 return pr->throttling.acpi_processor_set_throttling(pr,state);
512 int acpi_processor_get_throttling_info(struct acpi_processor *pr)
522 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
523 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
524 pr->throttling.address,
525 pr->throttling.duty_offset,
526 pr->throttling.duty_width));
531 /* TBD: Support ACPI 2.0 objects */
532 no_ptc = acpi_processor_get_throttling_control(pr);
533 no_tss = acpi_processor_get_throttling_states(pr);
534 no_tsd = acpi_processor_get_tsd(pr);
536 if(no_ptc || no_tss) {
537 pr->throttling.acpi_processor_get_throttling = &acpi_processor_get_throttling_fadt;
538 pr->throttling.acpi_processor_set_throttling = &acpi_processor_set_throttling_fadt;
540 pr->throttling.acpi_processor_get_throttling = &acpi_processor_get_throttling_ptc;
541 pr->throttling.acpi_processor_set_throttling = &acpi_processor_set_throttling_ptc;
544 if (!pr->throttling.address) {
545 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
547 } else if (!pr->throttling.duty_width) {
548 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
551 /* TBD: Support duty_cycle values that span bit 4. */
552 else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
553 printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");
558 * PIIX4 Errata: We don't support throttling on the original PIIX4.
559 * This shouldn't be an issue as few (if any) mobile systems ever
562 if (errata.piix4.throttle) {
563 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
564 "Throttling not supported on PIIX4 A- or B-step\n"));
568 pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
571 * Compute state values. Note that throttling displays a linear power/
572 * performance relationship (at 50% performance the CPU will consume
573 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
576 step = (1000 / pr->throttling.state_count);
578 for (i = 0; i < pr->throttling.state_count; i++) {
579 pr->throttling.states[i].performance = step * i;
580 pr->throttling.states[i].power = step * i;
583 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
584 pr->throttling.state_count));
586 pr->flags.throttling = 1;
589 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
590 * thermal) decide to lower performance if it so chooses, but for now
591 * we'll crank up the speed.
594 result = acpi_processor_get_throttling(pr);
598 if (pr->throttling.state) {
599 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
600 "Disabling throttling (was T%d)\n",
601 pr->throttling.state));
602 result = acpi_processor_set_throttling(pr, 0);
609 pr->flags.throttling = 0;
616 static int acpi_processor_throttling_seq_show(struct seq_file *seq,
619 struct acpi_processor *pr = seq->private;
627 if (!(pr->throttling.state_count > 0)) {
628 seq_puts(seq, "<not supported>\n");
632 result = acpi_processor_get_throttling(pr);
636 "Could not determine current throttling state.\n");
640 seq_printf(seq, "state count: %d\n"
641 "active state: T%d\n"
642 "state available: T%d to T%d\n",
643 pr->throttling.state_count, pr->throttling.state,
644 pr->throttling_platform_limit,
645 pr->throttling.state_count-1);
647 seq_puts(seq, "states:\n");
648 if(acpi_processor_get_throttling == acpi_processor_get_throttling_fadt)
649 for (i = 0; i < pr->throttling.state_count; i++)
650 seq_printf(seq, " %cT%d: %02d%%\n",
651 (i == pr->throttling.state ? '*' : ' '), i,
652 (pr->throttling.states[i].performance ? pr->
653 throttling.states[i].performance / 10 : 0));
655 for (i = 0; i < pr->throttling.state_count; i++)
656 seq_printf(seq, " %cT%d: %02d%%\n",
657 (i == pr->throttling.state ? '*' : ' '), i,
658 (int)pr->throttling.states_tss[i].freqpercentage);
665 static int acpi_processor_throttling_open_fs(struct inode *inode,
668 return single_open(file, acpi_processor_throttling_seq_show,
672 static ssize_t acpi_processor_write_throttling(struct file * file,
673 const char __user * buffer,
674 size_t count, loff_t * data)
677 struct seq_file *m = file->private_data;
678 struct acpi_processor *pr = m->private;
679 char state_string[12] = { '\0' };
682 if (!pr || (count > sizeof(state_string) - 1))
685 if (copy_from_user(state_string, buffer, count))
688 state_string[count] = '\0';
690 result = acpi_processor_set_throttling(pr,
691 simple_strtoul(state_string,
699 struct file_operations acpi_processor_throttling_fops = {
700 .open = acpi_processor_throttling_open_fs,
702 .write = acpi_processor_write_throttling,
704 .release = single_release,