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
106 "Invalid _PTC data (control_register)\n");
110 memcpy(&pr->throttling.control_register, obj.buffer.pointer,
111 sizeof(struct acpi_ptc_register));
117 obj = ptc->package.elements[1];
119 if ((obj.type != ACPI_TYPE_BUFFER)
120 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
121 || (obj.buffer.pointer == NULL)) {
122 printk(KERN_ERR PREFIX "Invalid _PTC data (status_register)\n");
127 memcpy(&pr->throttling.status_register, obj.buffer.pointer,
128 sizeof(struct acpi_ptc_register));
131 kfree(buffer.pointer);
135 static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
138 acpi_status status = AE_OK;
139 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
140 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
141 struct acpi_buffer state = { 0, NULL };
142 union acpi_object *tss = NULL;
145 status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
146 if (ACPI_FAILURE(status)) {
147 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS"));
151 tss = buffer.pointer;
152 if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
153 printk(KERN_ERR PREFIX "Invalid _TSS data\n");
158 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
159 tss->package.count));
161 pr->throttling.state_count = tss->package.count;
162 pr->throttling.states_tss =
163 kmalloc(sizeof(struct acpi_processor_tx_tss) * tss->package.count,
165 if (!pr->throttling.states_tss) {
170 for (i = 0; i < pr->throttling.state_count; i++) {
172 struct acpi_processor_tx_tss *tx =
173 (struct acpi_processor_tx_tss *)&(pr->throttling.
176 state.length = sizeof(struct acpi_processor_tx_tss);
179 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
181 status = acpi_extract_package(&(tss->package.elements[i]),
183 if (ACPI_FAILURE(status)) {
184 ACPI_EXCEPTION((AE_INFO, status, "Invalid _TSS data"));
186 kfree(pr->throttling.states_tss);
190 if (!tx->freqpercentage) {
191 printk(KERN_ERR PREFIX
192 "Invalid _TSS data: freq is zero\n");
194 kfree(pr->throttling.states_tss);
200 kfree(buffer.pointer);
204 static int acpi_processor_get_tsd(struct acpi_processor *pr)
207 acpi_status status = AE_OK;
208 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
209 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
210 struct acpi_buffer state = { 0, NULL };
211 union acpi_object *tsd = NULL;
212 struct acpi_tsd_package *pdomain;
214 status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
215 if (ACPI_FAILURE(status)) {
219 tsd = buffer.pointer;
220 if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
221 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
226 if (tsd->package.count != 1) {
227 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
232 pdomain = &(pr->throttling.domain_info);
234 state.length = sizeof(struct acpi_tsd_package);
235 state.pointer = pdomain;
237 status = acpi_extract_package(&(tsd->package.elements[0]),
239 if (ACPI_FAILURE(status)) {
240 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
245 if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
246 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:num_entries\n"));
251 if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
252 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:revision\n"));
258 kfree(buffer.pointer);
262 /* --------------------------------------------------------------------------
264 -------------------------------------------------------------------------- */
265 static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
275 if (!pr->flags.throttling)
278 pr->throttling.state = 0;
280 duty_mask = pr->throttling.state_count - 1;
282 duty_mask <<= pr->throttling.duty_offset;
286 value = inl(pr->throttling.address);
289 * Compute the current throttling state when throttling is enabled
293 duty_value = value & duty_mask;
294 duty_value >>= pr->throttling.duty_offset;
297 state = pr->throttling.state_count - duty_value;
300 pr->throttling.state = state;
304 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
305 "Throttling state is T%d (%d%% throttling applied)\n",
306 state, pr->throttling.states[state].performance));
311 static int acpi_read_throttling_status(struct acpi_processor_throttling
315 switch (throttling->status_register.space_id) {
316 case ACPI_ADR_SPACE_SYSTEM_IO:
317 acpi_os_read_port((acpi_io_address) throttling->status_register.
319 (u32) throttling->status_register.bit_width *
322 case ACPI_ADR_SPACE_FIXED_HARDWARE:
323 printk(KERN_ERR PREFIX
324 "HARDWARE addr space,NOT supported yet\n");
327 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
328 (u32) (throttling->status_register.space_id));
333 static int acpi_write_throttling_state(struct acpi_processor_throttling
334 *throttling, int value)
338 switch (throttling->control_register.space_id) {
339 case ACPI_ADR_SPACE_SYSTEM_IO:
340 acpi_os_write_port((acpi_io_address) throttling->
341 control_register.address, value,
342 (u32) throttling->control_register.
346 case ACPI_ADR_SPACE_FIXED_HARDWARE:
347 printk(KERN_ERR PREFIX
348 "HARDWARE addr space,NOT supported yet\n");
351 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
352 (u32) (throttling->control_register.space_id));
357 static int acpi_get_throttling_state(struct acpi_processor *pr, int value)
361 for (i = 0; i < pr->throttling.state_count; i++) {
362 struct acpi_processor_tx_tss *tx =
363 (struct acpi_processor_tx_tss *)&(pr->throttling.
365 if (tx->control == value)
368 if (i > pr->throttling.state_count)
373 static int acpi_get_throttling_value(struct acpi_processor *pr, int state)
376 if (state >= 0 && state <= pr->throttling.state_count) {
377 struct acpi_processor_tx_tss *tx =
378 (struct acpi_processor_tx_tss *)&(pr->throttling.
385 static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
393 if (!pr->flags.throttling)
396 pr->throttling.state = 0;
398 value = acpi_read_throttling_status(&pr->throttling);
400 state = acpi_get_throttling_state(pr, value);
401 pr->throttling.state = state;
408 static int acpi_processor_get_throttling(struct acpi_processor *pr)
410 return pr->throttling.acpi_processor_get_throttling(pr);
413 static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
423 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
426 if (!pr->flags.throttling)
429 if (state == pr->throttling.state)
432 if (state < pr->throttling_platform_limit)
435 * Calculate the duty_value and duty_mask.
438 duty_value = pr->throttling.state_count - state;
440 duty_value <<= pr->throttling.duty_offset;
442 /* Used to clear all duty_value bits */
443 duty_mask = pr->throttling.state_count - 1;
445 duty_mask <<= acpi_gbl_FADT.duty_offset;
446 duty_mask = ~duty_mask;
452 * Disable throttling by writing a 0 to bit 4. Note that we must
453 * turn it off before you can change the duty_value.
455 value = inl(pr->throttling.address);
458 outl(value, pr->throttling.address);
462 * Write the new duty_value and then enable throttling. Note
463 * that a state value of 0 leaves throttling disabled.
468 outl(value, pr->throttling.address);
471 outl(value, pr->throttling.address);
474 pr->throttling.state = state;
478 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
479 "Throttling state set to T%d (%d%%)\n", state,
480 (pr->throttling.states[state].performance ? pr->
481 throttling.states[state].performance / 10 : 0)));
486 static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
494 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
497 if (!pr->flags.throttling)
500 if (state == pr->throttling.state)
503 if (state < pr->throttling_platform_limit)
508 value = acpi_get_throttling_value(pr, state);
510 acpi_write_throttling_state(&pr->throttling, value);
511 pr->throttling.state = state;
518 int acpi_processor_set_throttling(struct acpi_processor *pr, int state)
520 return pr->throttling.acpi_processor_set_throttling(pr, state);
523 int acpi_processor_get_throttling_info(struct acpi_processor *pr)
532 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
533 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
534 pr->throttling.address,
535 pr->throttling.duty_offset,
536 pr->throttling.duty_width));
541 /* TBD: Support ACPI 2.0 objects */
542 no_ptc = acpi_processor_get_throttling_control(pr);
543 no_tss = acpi_processor_get_throttling_states(pr);
544 no_tsd = acpi_processor_get_tsd(pr);
546 if (no_ptc || no_tss) {
547 pr->throttling.acpi_processor_get_throttling =
548 &acpi_processor_get_throttling_fadt;
549 pr->throttling.acpi_processor_set_throttling =
550 &acpi_processor_set_throttling_fadt;
552 pr->throttling.acpi_processor_get_throttling =
553 &acpi_processor_get_throttling_ptc;
554 pr->throttling.acpi_processor_set_throttling =
555 &acpi_processor_set_throttling_ptc;
558 if (!pr->throttling.address) {
559 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
561 } else if (!pr->throttling.duty_width) {
562 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
565 /* TBD: Support duty_cycle values that span bit 4. */
566 else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
567 printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");
572 * PIIX4 Errata: We don't support throttling on the original PIIX4.
573 * This shouldn't be an issue as few (if any) mobile systems ever
576 if (errata.piix4.throttle) {
577 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
578 "Throttling not supported on PIIX4 A- or B-step\n"));
582 pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
585 * Compute state values. Note that throttling displays a linear power/
586 * performance relationship (at 50% performance the CPU will consume
587 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
590 step = (1000 / pr->throttling.state_count);
592 for (i = 0; i < pr->throttling.state_count; i++) {
593 pr->throttling.states[i].performance = step * i;
594 pr->throttling.states[i].power = step * i;
597 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
598 pr->throttling.state_count));
600 pr->flags.throttling = 1;
603 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
604 * thermal) decide to lower performance if it so chooses, but for now
605 * we'll crank up the speed.
608 result = acpi_processor_get_throttling(pr);
612 if (pr->throttling.state) {
613 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
614 "Disabling throttling (was T%d)\n",
615 pr->throttling.state));
616 result = acpi_processor_set_throttling(pr, 0);
623 pr->flags.throttling = 0;
630 static int acpi_processor_throttling_seq_show(struct seq_file *seq,
633 struct acpi_processor *pr = seq->private;
640 if (!(pr->throttling.state_count > 0)) {
641 seq_puts(seq, "<not supported>\n");
645 result = acpi_processor_get_throttling(pr);
649 "Could not determine current throttling state.\n");
653 seq_printf(seq, "state count: %d\n"
654 "active state: T%d\n"
655 "state available: T%d to T%d\n",
656 pr->throttling.state_count, pr->throttling.state,
657 pr->throttling_platform_limit,
658 pr->throttling.state_count - 1);
660 seq_puts(seq, "states:\n");
661 if (acpi_processor_get_throttling == acpi_processor_get_throttling_fadt)
662 for (i = 0; i < pr->throttling.state_count; i++)
663 seq_printf(seq, " %cT%d: %02d%%\n",
664 (i == pr->throttling.state ? '*' : ' '), i,
665 (pr->throttling.states[i].performance ? pr->
666 throttling.states[i].performance / 10 : 0));
668 for (i = 0; i < pr->throttling.state_count; i++)
669 seq_printf(seq, " %cT%d: %02d%%\n",
670 (i == pr->throttling.state ? '*' : ' '), i,
671 (int)pr->throttling.states_tss[i].
678 static int acpi_processor_throttling_open_fs(struct inode *inode,
681 return single_open(file, acpi_processor_throttling_seq_show,
685 static ssize_t acpi_processor_write_throttling(struct file *file,
686 const char __user * buffer,
687 size_t count, loff_t * data)
690 struct seq_file *m = file->private_data;
691 struct acpi_processor *pr = m->private;
692 char state_string[12] = { '\0' };
694 if (!pr || (count > sizeof(state_string) - 1))
697 if (copy_from_user(state_string, buffer, count))
700 state_string[count] = '\0';
702 result = acpi_processor_set_throttling(pr,
703 simple_strtoul(state_string,
711 struct file_operations acpi_processor_throttling_fops = {
712 .open = acpi_processor_throttling_open_fs,
714 .write = acpi_processor_write_throttling,
716 .release = single_release,