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)
76 struct acpi_processor_limit *limit;
79 result = acpi_processor_get_platform_limit(pr);
81 /* Throttling Limit is unsupported */
85 throttling_limit = pr->throttling_platform_limit;
86 if (throttling_limit >= pr->throttling.state_count) {
87 /* Uncorrect Throttling Limit */
91 current_state = pr->throttling.state;
92 if (current_state > throttling_limit) {
94 * The current state can meet the requirement of
95 * _TPC limit. But it is reasonable that OSPM changes
96 * t-states from high to low for better performance.
97 * Of course the limit condition of thermal
98 * and user should be considered.
101 target_state = throttling_limit;
102 if (limit->thermal.tx > target_state)
103 target_state = limit->thermal.tx;
104 if (limit->user.tx > target_state)
105 target_state = limit->user.tx;
106 } else if (current_state == throttling_limit) {
108 * Unnecessary to change the throttling state
113 * If the current state is lower than the limit of _TPC, it
114 * will be forced to switch to the throttling state defined
115 * by throttling_platfor_limit.
116 * Because the previous state meets with the limit condition
117 * of thermal and user, it is unnecessary to check it again.
119 target_state = throttling_limit;
121 return acpi_processor_set_throttling(pr, target_state);
125 * _PTC - Processor Throttling Control (and status) register location
127 static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
130 acpi_status status = 0;
131 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
132 union acpi_object *ptc = NULL;
133 union acpi_object obj = { 0 };
135 status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
136 if (ACPI_FAILURE(status)) {
137 if (status != AE_NOT_FOUND) {
138 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTC"));
143 ptc = (union acpi_object *)buffer.pointer;
144 if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)
145 || (ptc->package.count != 2)) {
146 printk(KERN_ERR PREFIX "Invalid _PTC data\n");
155 obj = ptc->package.elements[0];
157 if ((obj.type != ACPI_TYPE_BUFFER)
158 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
159 || (obj.buffer.pointer == NULL)) {
160 printk(KERN_ERR PREFIX
161 "Invalid _PTC data (control_register)\n");
165 memcpy(&pr->throttling.control_register, obj.buffer.pointer,
166 sizeof(struct acpi_ptc_register));
172 obj = ptc->package.elements[1];
174 if ((obj.type != ACPI_TYPE_BUFFER)
175 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
176 || (obj.buffer.pointer == NULL)) {
177 printk(KERN_ERR PREFIX "Invalid _PTC data (status_register)\n");
182 memcpy(&pr->throttling.status_register, obj.buffer.pointer,
183 sizeof(struct acpi_ptc_register));
186 kfree(buffer.pointer);
192 * _TSS - Throttling Supported States
194 static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
197 acpi_status status = AE_OK;
198 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
199 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
200 struct acpi_buffer state = { 0, NULL };
201 union acpi_object *tss = NULL;
204 status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
205 if (ACPI_FAILURE(status)) {
206 if (status != AE_NOT_FOUND) {
207 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS"));
212 tss = buffer.pointer;
213 if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
214 printk(KERN_ERR PREFIX "Invalid _TSS data\n");
219 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
220 tss->package.count));
222 pr->throttling.state_count = tss->package.count;
223 pr->throttling.states_tss =
224 kmalloc(sizeof(struct acpi_processor_tx_tss) * tss->package.count,
226 if (!pr->throttling.states_tss) {
231 for (i = 0; i < pr->throttling.state_count; i++) {
233 struct acpi_processor_tx_tss *tx =
234 (struct acpi_processor_tx_tss *)&(pr->throttling.
237 state.length = sizeof(struct acpi_processor_tx_tss);
240 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
242 status = acpi_extract_package(&(tss->package.elements[i]),
244 if (ACPI_FAILURE(status)) {
245 ACPI_EXCEPTION((AE_INFO, status, "Invalid _TSS data"));
247 kfree(pr->throttling.states_tss);
251 if (!tx->freqpercentage) {
252 printk(KERN_ERR PREFIX
253 "Invalid _TSS data: freq is zero\n");
255 kfree(pr->throttling.states_tss);
261 kfree(buffer.pointer);
267 * _TSD - T-State Dependencies
269 static int acpi_processor_get_tsd(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("NNNNN"), "NNNNN" };
275 struct acpi_buffer state = { 0, NULL };
276 union acpi_object *tsd = NULL;
277 struct acpi_tsd_package *pdomain;
279 status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
280 if (ACPI_FAILURE(status)) {
281 if (status != AE_NOT_FOUND) {
282 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSD"));
287 tsd = buffer.pointer;
288 if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
289 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
294 if (tsd->package.count != 1) {
295 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
300 pdomain = &(pr->throttling.domain_info);
302 state.length = sizeof(struct acpi_tsd_package);
303 state.pointer = pdomain;
305 status = acpi_extract_package(&(tsd->package.elements[0]),
307 if (ACPI_FAILURE(status)) {
308 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
313 if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
314 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:num_entries\n"));
319 if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
320 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:revision\n"));
326 kfree(buffer.pointer);
330 /* --------------------------------------------------------------------------
332 -------------------------------------------------------------------------- */
333 static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
343 if (!pr->flags.throttling)
346 pr->throttling.state = 0;
348 duty_mask = pr->throttling.state_count - 1;
350 duty_mask <<= pr->throttling.duty_offset;
354 value = inl(pr->throttling.address);
357 * Compute the current throttling state when throttling is enabled
361 duty_value = value & duty_mask;
362 duty_value >>= pr->throttling.duty_offset;
365 state = pr->throttling.state_count - duty_value;
368 pr->throttling.state = state;
372 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
373 "Throttling state is T%d (%d%% throttling applied)\n",
374 state, pr->throttling.states[state].performance));
379 static int acpi_read_throttling_status(struct acpi_processor_throttling
383 switch (throttling->status_register.space_id) {
384 case ACPI_ADR_SPACE_SYSTEM_IO:
385 acpi_os_read_port((acpi_io_address) throttling->status_register.
387 (u32) throttling->status_register.bit_width *
390 case ACPI_ADR_SPACE_FIXED_HARDWARE:
391 printk(KERN_ERR PREFIX
392 "HARDWARE addr space,NOT supported yet\n");
395 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
396 (u32) (throttling->status_register.space_id));
401 static int acpi_write_throttling_state(struct acpi_processor_throttling
402 *throttling, int value)
406 switch (throttling->control_register.space_id) {
407 case ACPI_ADR_SPACE_SYSTEM_IO:
408 acpi_os_write_port((acpi_io_address) throttling->
409 control_register.address, value,
410 (u32) throttling->control_register.
414 case ACPI_ADR_SPACE_FIXED_HARDWARE:
415 printk(KERN_ERR PREFIX
416 "HARDWARE addr space,NOT supported yet\n");
419 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
420 (u32) (throttling->control_register.space_id));
425 static int acpi_get_throttling_state(struct acpi_processor *pr, int value)
429 for (i = 0; i < pr->throttling.state_count; i++) {
430 struct acpi_processor_tx_tss *tx =
431 (struct acpi_processor_tx_tss *)&(pr->throttling.
433 if (tx->control == value)
436 if (i > pr->throttling.state_count)
441 static int acpi_get_throttling_value(struct acpi_processor *pr, int state)
444 if (state >= 0 && state <= pr->throttling.state_count) {
445 struct acpi_processor_tx_tss *tx =
446 (struct acpi_processor_tx_tss *)&(pr->throttling.
453 static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
461 if (!pr->flags.throttling)
464 pr->throttling.state = 0;
466 value = acpi_read_throttling_status(&pr->throttling);
468 state = acpi_get_throttling_state(pr, value);
469 pr->throttling.state = state;
476 static int acpi_processor_get_throttling(struct acpi_processor *pr)
478 return pr->throttling.acpi_processor_get_throttling(pr);
481 static int acpi_processor_get_fadt_info(struct acpi_processor *pr)
485 if (!pr->throttling.address) {
486 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
488 } else if (!pr->throttling.duty_width) {
489 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
492 /* TBD: Support duty_cycle values that span bit 4. */
493 else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
494 printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");
498 pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
501 * Compute state values. Note that throttling displays a linear power
502 * performance relationship (at 50% performance the CPU will consume
503 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
506 step = (1000 / pr->throttling.state_count);
508 for (i = 0; i < pr->throttling.state_count; i++) {
509 pr->throttling.states[i].performance = 1000 - step * i;
510 pr->throttling.states[i].power = 1000 - step * i;
515 static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
525 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
528 if (!pr->flags.throttling)
531 if (state == pr->throttling.state)
534 if (state < pr->throttling_platform_limit)
537 * Calculate the duty_value and duty_mask.
540 duty_value = pr->throttling.state_count - state;
542 duty_value <<= pr->throttling.duty_offset;
544 /* Used to clear all duty_value bits */
545 duty_mask = pr->throttling.state_count - 1;
547 duty_mask <<= acpi_gbl_FADT.duty_offset;
548 duty_mask = ~duty_mask;
554 * Disable throttling by writing a 0 to bit 4. Note that we must
555 * turn it off before you can change the duty_value.
557 value = inl(pr->throttling.address);
560 outl(value, pr->throttling.address);
564 * Write the new duty_value and then enable throttling. Note
565 * that a state value of 0 leaves throttling disabled.
570 outl(value, pr->throttling.address);
573 outl(value, pr->throttling.address);
576 pr->throttling.state = state;
580 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
581 "Throttling state set to T%d (%d%%)\n", state,
582 (pr->throttling.states[state].performance ? pr->
583 throttling.states[state].performance / 10 : 0)));
588 static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
596 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
599 if (!pr->flags.throttling)
602 if (state == pr->throttling.state)
605 if (state < pr->throttling_platform_limit)
610 value = acpi_get_throttling_value(pr, state);
612 acpi_write_throttling_state(&pr->throttling, value);
613 pr->throttling.state = state;
620 int acpi_processor_set_throttling(struct acpi_processor *pr, int state)
622 return pr->throttling.acpi_processor_set_throttling(pr, state);
625 int acpi_processor_get_throttling_info(struct acpi_processor *pr)
629 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
630 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
631 pr->throttling.address,
632 pr->throttling.duty_offset,
633 pr->throttling.duty_width));
639 * Evaluate _PTC, _TSS and _TPC
640 * They must all be present or none of them can be used.
642 if (acpi_processor_get_throttling_control(pr) ||
643 acpi_processor_get_throttling_states(pr) ||
644 acpi_processor_get_platform_limit(pr))
646 if (acpi_processor_get_fadt_info(pr))
648 pr->throttling.acpi_processor_get_throttling =
649 &acpi_processor_get_throttling_fadt;
650 pr->throttling.acpi_processor_set_throttling =
651 &acpi_processor_set_throttling_fadt;
653 pr->throttling.acpi_processor_get_throttling =
654 &acpi_processor_get_throttling_ptc;
655 pr->throttling.acpi_processor_set_throttling =
656 &acpi_processor_set_throttling_ptc;
659 acpi_processor_get_tsd(pr);
662 * PIIX4 Errata: We don't support throttling on the original PIIX4.
663 * This shouldn't be an issue as few (if any) mobile systems ever
666 if (errata.piix4.throttle) {
667 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
668 "Throttling not supported on PIIX4 A- or B-step\n"));
672 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
673 pr->throttling.state_count));
675 pr->flags.throttling = 1;
678 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
679 * thermal) decide to lower performance if it so chooses, but for now
680 * we'll crank up the speed.
683 result = acpi_processor_get_throttling(pr);
687 if (pr->throttling.state) {
688 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
689 "Disabling throttling (was T%d)\n",
690 pr->throttling.state));
691 result = acpi_processor_set_throttling(pr, 0);
698 pr->flags.throttling = 0;
705 static int acpi_processor_throttling_seq_show(struct seq_file *seq,
708 struct acpi_processor *pr = seq->private;
715 if (!(pr->throttling.state_count > 0)) {
716 seq_puts(seq, "<not supported>\n");
720 result = acpi_processor_get_throttling(pr);
724 "Could not determine current throttling state.\n");
728 seq_printf(seq, "state count: %d\n"
729 "active state: T%d\n"
730 "state available: T%d to T%d\n",
731 pr->throttling.state_count, pr->throttling.state,
732 pr->throttling_platform_limit,
733 pr->throttling.state_count - 1);
735 seq_puts(seq, "states:\n");
736 if (pr->throttling.acpi_processor_get_throttling ==
737 acpi_processor_get_throttling_fadt) {
738 for (i = 0; i < pr->throttling.state_count; i++)
739 seq_printf(seq, " %cT%d: %02d%%\n",
740 (i == pr->throttling.state ? '*' : ' '), i,
741 (pr->throttling.states[i].performance ? pr->
742 throttling.states[i].performance / 10 : 0));
744 for (i = 0; i < pr->throttling.state_count; i++)
745 seq_printf(seq, " %cT%d: %02d%%\n",
746 (i == pr->throttling.state ? '*' : ' '), i,
747 (int)pr->throttling.states_tss[i].
755 static int acpi_processor_throttling_open_fs(struct inode *inode,
758 return single_open(file, acpi_processor_throttling_seq_show,
762 static ssize_t acpi_processor_write_throttling(struct file *file,
763 const char __user * buffer,
764 size_t count, loff_t * data)
767 struct seq_file *m = file->private_data;
768 struct acpi_processor *pr = m->private;
769 char state_string[12] = { '\0' };
771 if (!pr || (count > sizeof(state_string) - 1))
774 if (copy_from_user(state_string, buffer, count))
777 state_string[count] = '\0';
779 result = acpi_processor_set_throttling(pr,
780 simple_strtoul(state_string,
788 struct file_operations acpi_processor_throttling_fops = {
789 .open = acpi_processor_throttling_open_fs,
791 .write = acpi_processor_write_throttling,
793 .release = single_release,