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/sched.h>
33 #include <linux/cpufreq.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
38 #include <asm/uaccess.h>
40 #include <acpi/acpi_bus.h>
41 #include <acpi/processor.h>
43 #define ACPI_PROCESSOR_COMPONENT 0x01000000
44 #define ACPI_PROCESSOR_CLASS "processor"
45 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
46 ACPI_MODULE_NAME("processor_throttling");
48 static int acpi_processor_get_throttling(struct acpi_processor *pr);
49 int acpi_processor_set_throttling(struct acpi_processor *pr, int state);
52 * _TPC - Throttling Present Capabilities
54 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
56 acpi_status status = 0;
57 unsigned long tpc = 0;
61 status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);
62 if (ACPI_FAILURE(status)) {
63 if (status != AE_NOT_FOUND) {
64 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TPC"));
68 pr->throttling_platform_limit = (int)tpc;
72 int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
77 struct acpi_processor_limit *limit;
80 result = acpi_processor_get_platform_limit(pr);
82 /* Throttling Limit is unsupported */
86 throttling_limit = pr->throttling_platform_limit;
87 if (throttling_limit >= pr->throttling.state_count) {
88 /* Uncorrect Throttling Limit */
92 current_state = pr->throttling.state;
93 if (current_state > throttling_limit) {
95 * The current state can meet the requirement of
96 * _TPC limit. But it is reasonable that OSPM changes
97 * t-states from high to low for better performance.
98 * Of course the limit condition of thermal
99 * and user should be considered.
102 target_state = throttling_limit;
103 if (limit->thermal.tx > target_state)
104 target_state = limit->thermal.tx;
105 if (limit->user.tx > target_state)
106 target_state = limit->user.tx;
107 } else if (current_state == throttling_limit) {
109 * Unnecessary to change the throttling state
114 * If the current state is lower than the limit of _TPC, it
115 * will be forced to switch to the throttling state defined
116 * by throttling_platfor_limit.
117 * Because the previous state meets with the limit condition
118 * of thermal and user, it is unnecessary to check it again.
120 target_state = throttling_limit;
122 return acpi_processor_set_throttling(pr, target_state);
126 * _PTC - Processor Throttling Control (and status) register location
128 static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
131 acpi_status status = 0;
132 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
133 union acpi_object *ptc = NULL;
134 union acpi_object obj = { 0 };
135 struct acpi_processor_throttling *throttling;
137 status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
138 if (ACPI_FAILURE(status)) {
139 if (status != AE_NOT_FOUND) {
140 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTC"));
145 ptc = (union acpi_object *)buffer.pointer;
146 if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)
147 || (ptc->package.count != 2)) {
148 printk(KERN_ERR PREFIX "Invalid _PTC data\n");
157 obj = ptc->package.elements[0];
159 if ((obj.type != ACPI_TYPE_BUFFER)
160 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
161 || (obj.buffer.pointer == NULL)) {
162 printk(KERN_ERR PREFIX
163 "Invalid _PTC data (control_register)\n");
167 memcpy(&pr->throttling.control_register, obj.buffer.pointer,
168 sizeof(struct acpi_ptc_register));
174 obj = ptc->package.elements[1];
176 if ((obj.type != ACPI_TYPE_BUFFER)
177 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
178 || (obj.buffer.pointer == NULL)) {
179 printk(KERN_ERR PREFIX "Invalid _PTC data (status_register)\n");
184 memcpy(&pr->throttling.status_register, obj.buffer.pointer,
185 sizeof(struct acpi_ptc_register));
187 throttling = &pr->throttling;
189 if ((throttling->control_register.bit_width +
190 throttling->control_register.bit_offset) > 32) {
191 printk(KERN_ERR PREFIX "Invalid _PTC control register\n");
196 if ((throttling->status_register.bit_width +
197 throttling->status_register.bit_offset) > 32) {
198 printk(KERN_ERR PREFIX "Invalid _PTC status register\n");
204 kfree(buffer.pointer);
210 * _TSS - Throttling Supported States
212 static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
215 acpi_status status = AE_OK;
216 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
217 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
218 struct acpi_buffer state = { 0, NULL };
219 union acpi_object *tss = NULL;
222 status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
223 if (ACPI_FAILURE(status)) {
224 if (status != AE_NOT_FOUND) {
225 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS"));
230 tss = buffer.pointer;
231 if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
232 printk(KERN_ERR PREFIX "Invalid _TSS data\n");
237 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
238 tss->package.count));
240 pr->throttling.state_count = tss->package.count;
241 pr->throttling.states_tss =
242 kmalloc(sizeof(struct acpi_processor_tx_tss) * tss->package.count,
244 if (!pr->throttling.states_tss) {
249 for (i = 0; i < pr->throttling.state_count; i++) {
251 struct acpi_processor_tx_tss *tx =
252 (struct acpi_processor_tx_tss *)&(pr->throttling.
255 state.length = sizeof(struct acpi_processor_tx_tss);
258 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
260 status = acpi_extract_package(&(tss->package.elements[i]),
262 if (ACPI_FAILURE(status)) {
263 ACPI_EXCEPTION((AE_INFO, status, "Invalid _TSS data"));
265 kfree(pr->throttling.states_tss);
269 if (!tx->freqpercentage) {
270 printk(KERN_ERR PREFIX
271 "Invalid _TSS data: freq is zero\n");
273 kfree(pr->throttling.states_tss);
279 kfree(buffer.pointer);
285 * _TSD - T-State Dependencies
287 static int acpi_processor_get_tsd(struct acpi_processor *pr)
290 acpi_status status = AE_OK;
291 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
292 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
293 struct acpi_buffer state = { 0, NULL };
294 union acpi_object *tsd = NULL;
295 struct acpi_tsd_package *pdomain;
297 status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
298 if (ACPI_FAILURE(status)) {
299 if (status != AE_NOT_FOUND) {
300 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSD"));
305 tsd = buffer.pointer;
306 if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
307 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
312 if (tsd->package.count != 1) {
313 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
318 pdomain = &(pr->throttling.domain_info);
320 state.length = sizeof(struct acpi_tsd_package);
321 state.pointer = pdomain;
323 status = acpi_extract_package(&(tsd->package.elements[0]),
325 if (ACPI_FAILURE(status)) {
326 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
331 if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
332 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:num_entries\n"));
337 if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
338 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:revision\n"));
344 kfree(buffer.pointer);
348 /* --------------------------------------------------------------------------
350 -------------------------------------------------------------------------- */
351 static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
361 if (!pr->flags.throttling)
364 pr->throttling.state = 0;
366 duty_mask = pr->throttling.state_count - 1;
368 duty_mask <<= pr->throttling.duty_offset;
372 value = inl(pr->throttling.address);
375 * Compute the current throttling state when throttling is enabled
379 duty_value = value & duty_mask;
380 duty_value >>= pr->throttling.duty_offset;
383 state = pr->throttling.state_count - duty_value;
386 pr->throttling.state = state;
390 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
391 "Throttling state is T%d (%d%% throttling applied)\n",
392 state, pr->throttling.states[state].performance));
398 static int acpi_throttling_rdmsr(struct acpi_processor *pr,
399 acpi_integer * value)
401 struct cpuinfo_x86 *c;
402 u64 msr_high, msr_low;
410 if ((c->x86_vendor != X86_VENDOR_INTEL) ||
411 !cpu_has(c, X86_FEATURE_ACPI)) {
412 printk(KERN_ERR PREFIX
413 "HARDWARE addr space,NOT supported yet\n");
417 rdmsr_safe(MSR_IA32_THERM_CONTROL,
418 (u32 *)&msr_low , (u32 *) &msr_high);
419 msr = (msr_high << 32) | msr_low;
420 *value = (acpi_integer) msr;
426 static int acpi_throttling_wrmsr(struct acpi_processor *pr, acpi_integer value)
428 struct cpuinfo_x86 *c;
436 if ((c->x86_vendor != X86_VENDOR_INTEL) ||
437 !cpu_has(c, X86_FEATURE_ACPI)) {
438 printk(KERN_ERR PREFIX
439 "HARDWARE addr space,NOT supported yet\n");
442 wrmsr_safe(MSR_IA32_THERM_CONTROL,
443 msr & 0xffffffff, msr >> 32);
449 static int acpi_throttling_rdmsr(struct acpi_processor *pr,
450 acpi_integer * value)
452 printk(KERN_ERR PREFIX
453 "HARDWARE addr space,NOT supported yet\n");
457 static int acpi_throttling_wrmsr(struct acpi_processor *pr, acpi_integer value)
459 printk(KERN_ERR PREFIX
460 "HARDWARE addr space,NOT supported yet\n");
465 static int acpi_read_throttling_status(struct acpi_processor *pr,
468 u32 bit_width, bit_offset;
471 struct acpi_processor_throttling *throttling;
474 throttling = &pr->throttling;
475 switch (throttling->status_register.space_id) {
476 case ACPI_ADR_SPACE_SYSTEM_IO:
478 bit_width = throttling->status_register.bit_width;
479 bit_offset = throttling->status_register.bit_offset;
481 acpi_os_read_port((acpi_io_address) throttling->status_register.
482 address, (u32 *) &ptc_value,
483 (u32) (bit_width + bit_offset));
484 ptc_mask = (1 << bit_width) - 1;
485 *value = (acpi_integer) ((ptc_value >> bit_offset) & ptc_mask);
488 case ACPI_ADR_SPACE_FIXED_HARDWARE:
489 ret = acpi_throttling_rdmsr(pr, value);
492 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
493 (u32) (throttling->status_register.space_id));
498 static int acpi_write_throttling_state(struct acpi_processor *pr,
501 u32 bit_width, bit_offset;
504 struct acpi_processor_throttling *throttling;
507 throttling = &pr->throttling;
508 switch (throttling->control_register.space_id) {
509 case ACPI_ADR_SPACE_SYSTEM_IO:
510 bit_width = throttling->control_register.bit_width;
511 bit_offset = throttling->control_register.bit_offset;
512 ptc_mask = (1 << bit_width) - 1;
513 ptc_value = value & ptc_mask;
515 acpi_os_write_port((acpi_io_address) throttling->
516 control_register.address,
517 (u32) (ptc_value << bit_offset),
518 (u32) (bit_width + bit_offset));
521 case ACPI_ADR_SPACE_FIXED_HARDWARE:
522 ret = acpi_throttling_wrmsr(pr, value);
525 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
526 (u32) (throttling->control_register.space_id));
531 static int acpi_get_throttling_state(struct acpi_processor *pr,
536 for (i = 0; i < pr->throttling.state_count; i++) {
537 struct acpi_processor_tx_tss *tx =
538 (struct acpi_processor_tx_tss *)&(pr->throttling.
540 if (tx->control == value)
543 if (i > pr->throttling.state_count)
548 static int acpi_get_throttling_value(struct acpi_processor *pr,
549 int state, acpi_integer *value)
553 if (state >= 0 && state <= pr->throttling.state_count) {
554 struct acpi_processor_tx_tss *tx =
555 (struct acpi_processor_tx_tss *)&(pr->throttling.
557 *value = tx->control;
563 static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
572 if (!pr->flags.throttling)
575 pr->throttling.state = 0;
578 ret = acpi_read_throttling_status(pr, &value);
580 state = acpi_get_throttling_state(pr, value);
581 pr->throttling.state = state;
587 static int acpi_processor_get_throttling(struct acpi_processor *pr)
589 cpumask_t saved_mask;
593 * Migrate task to the cpu pointed by pr.
595 saved_mask = current->cpus_allowed;
596 set_cpus_allowed(current, cpumask_of_cpu(pr->id));
597 ret = pr->throttling.acpi_processor_get_throttling(pr);
598 /* restore the previous state */
599 set_cpus_allowed(current, saved_mask);
604 static int acpi_processor_get_fadt_info(struct acpi_processor *pr)
608 if (!pr->throttling.address) {
609 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
611 } else if (!pr->throttling.duty_width) {
612 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
615 /* TBD: Support duty_cycle values that span bit 4. */
616 else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
617 printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");
621 pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
624 * Compute state values. Note that throttling displays a linear power
625 * performance relationship (at 50% performance the CPU will consume
626 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
629 step = (1000 / pr->throttling.state_count);
631 for (i = 0; i < pr->throttling.state_count; i++) {
632 pr->throttling.states[i].performance = 1000 - step * i;
633 pr->throttling.states[i].power = 1000 - step * i;
638 static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
648 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
651 if (!pr->flags.throttling)
654 if (state == pr->throttling.state)
657 if (state < pr->throttling_platform_limit)
660 * Calculate the duty_value and duty_mask.
663 duty_value = pr->throttling.state_count - state;
665 duty_value <<= pr->throttling.duty_offset;
667 /* Used to clear all duty_value bits */
668 duty_mask = pr->throttling.state_count - 1;
670 duty_mask <<= acpi_gbl_FADT.duty_offset;
671 duty_mask = ~duty_mask;
677 * Disable throttling by writing a 0 to bit 4. Note that we must
678 * turn it off before you can change the duty_value.
680 value = inl(pr->throttling.address);
683 outl(value, pr->throttling.address);
687 * Write the new duty_value and then enable throttling. Note
688 * that a state value of 0 leaves throttling disabled.
693 outl(value, pr->throttling.address);
696 outl(value, pr->throttling.address);
699 pr->throttling.state = state;
703 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
704 "Throttling state set to T%d (%d%%)\n", state,
705 (pr->throttling.states[state].performance ? pr->
706 throttling.states[state].performance / 10 : 0)));
711 static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
720 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
723 if (!pr->flags.throttling)
726 if (state == pr->throttling.state)
729 if (state < pr->throttling_platform_limit)
733 ret = acpi_get_throttling_value(pr, state, &value);
735 acpi_write_throttling_state(pr, value);
736 pr->throttling.state = state;
742 int acpi_processor_set_throttling(struct acpi_processor *pr, int state)
744 cpumask_t saved_mask;
747 * Migrate task to the cpu pointed by pr.
749 saved_mask = current->cpus_allowed;
750 set_cpus_allowed(current, cpumask_of_cpu(pr->id));
751 ret = pr->throttling.acpi_processor_set_throttling(pr, state);
752 /* restore the previous state */
753 set_cpus_allowed(current, saved_mask);
757 int acpi_processor_get_throttling_info(struct acpi_processor *pr)
761 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
762 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
763 pr->throttling.address,
764 pr->throttling.duty_offset,
765 pr->throttling.duty_width));
771 * Evaluate _PTC, _TSS and _TPC
772 * They must all be present or none of them can be used.
774 if (acpi_processor_get_throttling_control(pr) ||
775 acpi_processor_get_throttling_states(pr) ||
776 acpi_processor_get_platform_limit(pr))
778 pr->throttling.acpi_processor_get_throttling =
779 &acpi_processor_get_throttling_fadt;
780 pr->throttling.acpi_processor_set_throttling =
781 &acpi_processor_set_throttling_fadt;
782 if (acpi_processor_get_fadt_info(pr))
785 pr->throttling.acpi_processor_get_throttling =
786 &acpi_processor_get_throttling_ptc;
787 pr->throttling.acpi_processor_set_throttling =
788 &acpi_processor_set_throttling_ptc;
791 acpi_processor_get_tsd(pr);
794 * PIIX4 Errata: We don't support throttling on the original PIIX4.
795 * This shouldn't be an issue as few (if any) mobile systems ever
798 if (errata.piix4.throttle) {
799 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
800 "Throttling not supported on PIIX4 A- or B-step\n"));
804 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
805 pr->throttling.state_count));
807 pr->flags.throttling = 1;
810 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
811 * thermal) decide to lower performance if it so chooses, but for now
812 * we'll crank up the speed.
815 result = acpi_processor_get_throttling(pr);
819 if (pr->throttling.state) {
820 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
821 "Disabling throttling (was T%d)\n",
822 pr->throttling.state));
823 result = acpi_processor_set_throttling(pr, 0);
830 pr->flags.throttling = 0;
837 static int acpi_processor_throttling_seq_show(struct seq_file *seq,
840 struct acpi_processor *pr = seq->private;
847 if (!(pr->throttling.state_count > 0)) {
848 seq_puts(seq, "<not supported>\n");
852 result = acpi_processor_get_throttling(pr);
856 "Could not determine current throttling state.\n");
860 seq_printf(seq, "state count: %d\n"
861 "active state: T%d\n"
862 "state available: T%d to T%d\n",
863 pr->throttling.state_count, pr->throttling.state,
864 pr->throttling_platform_limit,
865 pr->throttling.state_count - 1);
867 seq_puts(seq, "states:\n");
868 if (pr->throttling.acpi_processor_get_throttling ==
869 acpi_processor_get_throttling_fadt) {
870 for (i = 0; i < pr->throttling.state_count; i++)
871 seq_printf(seq, " %cT%d: %02d%%\n",
872 (i == pr->throttling.state ? '*' : ' '), i,
873 (pr->throttling.states[i].performance ? pr->
874 throttling.states[i].performance / 10 : 0));
876 for (i = 0; i < pr->throttling.state_count; i++)
877 seq_printf(seq, " %cT%d: %02d%%\n",
878 (i == pr->throttling.state ? '*' : ' '), i,
879 (int)pr->throttling.states_tss[i].
887 static int acpi_processor_throttling_open_fs(struct inode *inode,
890 return single_open(file, acpi_processor_throttling_seq_show,
894 static ssize_t acpi_processor_write_throttling(struct file *file,
895 const char __user * buffer,
896 size_t count, loff_t * data)
899 struct seq_file *m = file->private_data;
900 struct acpi_processor *pr = m->private;
901 char state_string[12] = { '\0' };
903 if (!pr || (count > sizeof(state_string) - 1))
906 if (copy_from_user(state_string, buffer, count))
909 state_string[count] = '\0';
911 result = acpi_processor_set_throttling(pr,
912 simple_strtoul(state_string,
920 struct file_operations acpi_processor_throttling_fops = {
921 .open = acpi_processor_throttling_open_fs,
923 .write = acpi_processor_write_throttling,
925 .release = single_release,