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/slab.h>
32 #include <linux/init.h>
33 #include <linux/sched.h>
34 #include <linux/cpufreq.h>
37 #include <asm/uaccess.h>
39 #include <acpi/acpi_bus.h>
40 #include <acpi/acpi_drivers.h>
41 #include <acpi/processor.h>
43 #define PREFIX "ACPI: "
45 #define ACPI_PROCESSOR_CLASS "processor"
46 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
47 ACPI_MODULE_NAME("processor_throttling");
50 * 0 -> acpi processor driver doesn't ignore _TPC values
51 * 1 -> acpi processor driver ignores _TPC values
53 static int ignore_tpc;
54 module_param(ignore_tpc, int, 0644);
55 MODULE_PARM_DESC(ignore_tpc, "Disable broken BIOS _TPC throttling support");
57 struct throttling_tstate {
58 unsigned int cpu; /* cpu nr */
59 int target_state; /* target T-state */
62 struct acpi_processor_throttling_arg {
63 struct acpi_processor *pr;
68 #define THROTTLING_PRECHANGE (1)
69 #define THROTTLING_POSTCHANGE (2)
71 static int acpi_processor_get_throttling(struct acpi_processor *pr);
72 int acpi_processor_set_throttling(struct acpi_processor *pr,
73 int state, bool force);
75 static int acpi_processor_update_tsd_coord(void)
77 int count, count_target;
80 cpumask_var_t covered_cpus;
81 struct acpi_processor *pr, *match_pr;
82 struct acpi_tsd_package *pdomain, *match_pdomain;
83 struct acpi_processor_throttling *pthrottling, *match_pthrottling;
85 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
89 * Now that we have _TSD data from all CPUs, lets setup T-state
90 * coordination between all CPUs.
92 for_each_possible_cpu(i) {
93 pr = per_cpu(processors, i);
97 /* Basic validity check for domain info */
98 pthrottling = &(pr->throttling);
101 * If tsd package for one cpu is invalid, the coordination
102 * among all CPUs is thought as invalid.
105 if (!pthrottling->tsd_valid_flag) {
113 for_each_possible_cpu(i) {
114 pr = per_cpu(processors, i);
118 if (cpumask_test_cpu(i, covered_cpus))
120 pthrottling = &pr->throttling;
122 pdomain = &(pthrottling->domain_info);
123 cpumask_set_cpu(i, pthrottling->shared_cpu_map);
124 cpumask_set_cpu(i, covered_cpus);
126 * If the number of processor in the TSD domain is 1, it is
127 * unnecessary to parse the coordination for this CPU.
129 if (pdomain->num_processors <= 1)
132 /* Validate the Domain info */
133 count_target = pdomain->num_processors;
136 for_each_possible_cpu(j) {
140 match_pr = per_cpu(processors, j);
144 match_pthrottling = &(match_pr->throttling);
145 match_pdomain = &(match_pthrottling->domain_info);
146 if (match_pdomain->domain != pdomain->domain)
149 /* Here i and j are in the same domain.
150 * If two TSD packages have the same domain, they
151 * should have the same num_porcessors and
152 * coordination type. Otherwise it will be regarded
155 if (match_pdomain->num_processors != count_target) {
160 if (pdomain->coord_type != match_pdomain->coord_type) {
165 cpumask_set_cpu(j, covered_cpus);
166 cpumask_set_cpu(j, pthrottling->shared_cpu_map);
169 for_each_possible_cpu(j) {
173 match_pr = per_cpu(processors, j);
177 match_pthrottling = &(match_pr->throttling);
178 match_pdomain = &(match_pthrottling->domain_info);
179 if (match_pdomain->domain != pdomain->domain)
183 * If some CPUS have the same domain, they
184 * will have the same shared_cpu_map.
186 cpumask_copy(match_pthrottling->shared_cpu_map,
187 pthrottling->shared_cpu_map);
192 free_cpumask_var(covered_cpus);
194 for_each_possible_cpu(i) {
195 pr = per_cpu(processors, i);
200 * Assume no coordination on any error parsing domain info.
201 * The coordination type will be forced as SW_ALL.
204 pthrottling = &(pr->throttling);
205 cpumask_clear(pthrottling->shared_cpu_map);
206 cpumask_set_cpu(i, pthrottling->shared_cpu_map);
207 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
215 * Update the T-state coordination after the _TSD
216 * data for all cpus is obtained.
218 void acpi_processor_throttling_init(void)
220 if (acpi_processor_update_tsd_coord()) {
221 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
222 "Assume no T-state coordination\n"));
228 static int acpi_processor_throttling_notifier(unsigned long event, void *data)
230 struct throttling_tstate *p_tstate = data;
231 struct acpi_processor *pr;
234 struct acpi_processor_limit *p_limit;
235 struct acpi_processor_throttling *p_throttling;
238 pr = per_cpu(processors, cpu);
240 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Invalid pr pointer\n"));
243 if (!pr->flags.throttling) {
244 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Throttling control is "
245 "unsupported on CPU %d\n", cpu));
248 target_state = p_tstate->target_state;
249 p_throttling = &(pr->throttling);
251 case THROTTLING_PRECHANGE:
253 * Prechange event is used to choose one proper t-state,
254 * which meets the limits of thermal, user and _TPC.
256 p_limit = &pr->limit;
257 if (p_limit->thermal.tx > target_state)
258 target_state = p_limit->thermal.tx;
259 if (p_limit->user.tx > target_state)
260 target_state = p_limit->user.tx;
261 if (pr->throttling_platform_limit > target_state)
262 target_state = pr->throttling_platform_limit;
263 if (target_state >= p_throttling->state_count) {
265 "Exceed the limit of T-state \n");
266 target_state = p_throttling->state_count - 1;
268 p_tstate->target_state = target_state;
269 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PreChange Event:"
270 "target T-state of CPU %d is T%d\n",
273 case THROTTLING_POSTCHANGE:
275 * Postchange event is only used to update the
276 * T-state flag of acpi_processor_throttling.
278 p_throttling->state = target_state;
279 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PostChange Event:"
280 "CPU %d is switched to T%d\n",
285 "Unsupported Throttling notifier event\n");
293 * _TPC - Throttling Present Capabilities
295 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
297 acpi_status status = 0;
298 unsigned long long tpc = 0;
306 status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);
307 if (ACPI_FAILURE(status)) {
308 if (status != AE_NOT_FOUND) {
309 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TPC"));
315 pr->throttling_platform_limit = (int)tpc;
319 int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
322 int throttling_limit;
324 struct acpi_processor_limit *limit;
330 result = acpi_processor_get_platform_limit(pr);
332 /* Throttling Limit is unsupported */
336 throttling_limit = pr->throttling_platform_limit;
337 if (throttling_limit >= pr->throttling.state_count) {
338 /* Uncorrect Throttling Limit */
342 current_state = pr->throttling.state;
343 if (current_state > throttling_limit) {
345 * The current state can meet the requirement of
346 * _TPC limit. But it is reasonable that OSPM changes
347 * t-states from high to low for better performance.
348 * Of course the limit condition of thermal
349 * and user should be considered.
352 target_state = throttling_limit;
353 if (limit->thermal.tx > target_state)
354 target_state = limit->thermal.tx;
355 if (limit->user.tx > target_state)
356 target_state = limit->user.tx;
357 } else if (current_state == throttling_limit) {
359 * Unnecessary to change the throttling state
364 * If the current state is lower than the limit of _TPC, it
365 * will be forced to switch to the throttling state defined
366 * by throttling_platfor_limit.
367 * Because the previous state meets with the limit condition
368 * of thermal and user, it is unnecessary to check it again.
370 target_state = throttling_limit;
372 return acpi_processor_set_throttling(pr, target_state, false);
376 * This function is used to reevaluate whether the T-state is valid
377 * after one CPU is onlined/offlined.
378 * It is noted that it won't reevaluate the following properties for
381 * 2. the number of supported T-state
384 void acpi_processor_reevaluate_tstate(struct acpi_processor *pr,
385 unsigned long action)
389 if (action == CPU_DEAD) {
390 /* When one CPU is offline, the T-state throttling
391 * will be invalidated.
393 pr->flags.throttling = 0;
396 /* the following is to recheck whether the T-state is valid for
399 if (!pr->throttling.state_count) {
400 /* If the number of T-state is invalid, it is
403 pr->flags.throttling = 0;
406 pr->flags.throttling = 1;
408 /* Disable throttling (if enabled). We'll let subsequent
409 * policy (e.g.thermal) decide to lower performance if it
410 * so chooses, but for now we'll crank up the speed.
413 result = acpi_processor_get_throttling(pr);
417 if (pr->throttling.state) {
418 result = acpi_processor_set_throttling(pr, 0, false);
425 pr->flags.throttling = 0;
428 * _PTC - Processor Throttling Control (and status) register location
430 static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
433 acpi_status status = 0;
434 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
435 union acpi_object *ptc = NULL;
436 union acpi_object obj = { 0 };
437 struct acpi_processor_throttling *throttling;
439 status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
440 if (ACPI_FAILURE(status)) {
441 if (status != AE_NOT_FOUND) {
442 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTC"));
447 ptc = (union acpi_object *)buffer.pointer;
448 if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)
449 || (ptc->package.count != 2)) {
450 printk(KERN_ERR PREFIX "Invalid _PTC data\n");
459 obj = ptc->package.elements[0];
461 if ((obj.type != ACPI_TYPE_BUFFER)
462 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
463 || (obj.buffer.pointer == NULL)) {
464 printk(KERN_ERR PREFIX
465 "Invalid _PTC data (control_register)\n");
469 memcpy(&pr->throttling.control_register, obj.buffer.pointer,
470 sizeof(struct acpi_ptc_register));
476 obj = ptc->package.elements[1];
478 if ((obj.type != ACPI_TYPE_BUFFER)
479 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
480 || (obj.buffer.pointer == NULL)) {
481 printk(KERN_ERR PREFIX "Invalid _PTC data (status_register)\n");
486 memcpy(&pr->throttling.status_register, obj.buffer.pointer,
487 sizeof(struct acpi_ptc_register));
489 throttling = &pr->throttling;
491 if ((throttling->control_register.bit_width +
492 throttling->control_register.bit_offset) > 32) {
493 printk(KERN_ERR PREFIX "Invalid _PTC control register\n");
498 if ((throttling->status_register.bit_width +
499 throttling->status_register.bit_offset) > 32) {
500 printk(KERN_ERR PREFIX "Invalid _PTC status register\n");
506 kfree(buffer.pointer);
512 * _TSS - Throttling Supported States
514 static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
517 acpi_status status = AE_OK;
518 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
519 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
520 struct acpi_buffer state = { 0, NULL };
521 union acpi_object *tss = NULL;
524 status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
525 if (ACPI_FAILURE(status)) {
526 if (status != AE_NOT_FOUND) {
527 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS"));
532 tss = buffer.pointer;
533 if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
534 printk(KERN_ERR PREFIX "Invalid _TSS data\n");
539 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
540 tss->package.count));
542 pr->throttling.state_count = tss->package.count;
543 pr->throttling.states_tss =
544 kmalloc(sizeof(struct acpi_processor_tx_tss) * tss->package.count,
546 if (!pr->throttling.states_tss) {
551 for (i = 0; i < pr->throttling.state_count; i++) {
553 struct acpi_processor_tx_tss *tx =
554 (struct acpi_processor_tx_tss *)&(pr->throttling.
557 state.length = sizeof(struct acpi_processor_tx_tss);
560 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
562 status = acpi_extract_package(&(tss->package.elements[i]),
564 if (ACPI_FAILURE(status)) {
565 ACPI_EXCEPTION((AE_INFO, status, "Invalid _TSS data"));
567 kfree(pr->throttling.states_tss);
571 if (!tx->freqpercentage) {
572 printk(KERN_ERR PREFIX
573 "Invalid _TSS data: freq is zero\n");
575 kfree(pr->throttling.states_tss);
581 kfree(buffer.pointer);
587 * _TSD - T-State Dependencies
589 static int acpi_processor_get_tsd(struct acpi_processor *pr)
592 acpi_status status = AE_OK;
593 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
594 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
595 struct acpi_buffer state = { 0, NULL };
596 union acpi_object *tsd = NULL;
597 struct acpi_tsd_package *pdomain;
598 struct acpi_processor_throttling *pthrottling;
600 pthrottling = &pr->throttling;
601 pthrottling->tsd_valid_flag = 0;
603 status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
604 if (ACPI_FAILURE(status)) {
605 if (status != AE_NOT_FOUND) {
606 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSD"));
611 tsd = buffer.pointer;
612 if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
613 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
618 if (tsd->package.count != 1) {
619 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
624 pdomain = &(pr->throttling.domain_info);
626 state.length = sizeof(struct acpi_tsd_package);
627 state.pointer = pdomain;
629 status = acpi_extract_package(&(tsd->package.elements[0]),
631 if (ACPI_FAILURE(status)) {
632 printk(KERN_ERR PREFIX "Invalid _TSD data\n");
637 if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
638 printk(KERN_ERR PREFIX "Unknown _TSD:num_entries\n");
643 if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
644 printk(KERN_ERR PREFIX "Unknown _TSD:revision\n");
649 pthrottling = &pr->throttling;
650 pthrottling->tsd_valid_flag = 1;
651 pthrottling->shared_type = pdomain->coord_type;
652 cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
654 * If the coordination type is not defined in ACPI spec,
655 * the tsd_valid_flag will be clear and coordination type
656 * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
658 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
659 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
660 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
661 pthrottling->tsd_valid_flag = 0;
662 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
666 kfree(buffer.pointer);
670 /* --------------------------------------------------------------------------
672 -------------------------------------------------------------------------- */
673 static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
683 if (!pr->flags.throttling)
686 pr->throttling.state = 0;
688 duty_mask = pr->throttling.state_count - 1;
690 duty_mask <<= pr->throttling.duty_offset;
694 value = inl(pr->throttling.address);
697 * Compute the current throttling state when throttling is enabled
701 duty_value = value & duty_mask;
702 duty_value >>= pr->throttling.duty_offset;
705 state = pr->throttling.state_count - duty_value;
708 pr->throttling.state = state;
712 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
713 "Throttling state is T%d (%d%% throttling applied)\n",
714 state, pr->throttling.states[state].performance));
720 static int acpi_throttling_rdmsr(u64 *value)
722 u64 msr_high, msr_low;
726 if ((this_cpu_read(cpu_info.x86_vendor) != X86_VENDOR_INTEL) ||
727 !this_cpu_has(X86_FEATURE_ACPI)) {
728 printk(KERN_ERR PREFIX
729 "HARDWARE addr space,NOT supported yet\n");
733 rdmsr_safe(MSR_IA32_THERM_CONTROL,
734 (u32 *)&msr_low , (u32 *) &msr_high);
735 msr = (msr_high << 32) | msr_low;
742 static int acpi_throttling_wrmsr(u64 value)
747 if ((this_cpu_read(cpu_info.x86_vendor) != X86_VENDOR_INTEL) ||
748 !this_cpu_has(X86_FEATURE_ACPI)) {
749 printk(KERN_ERR PREFIX
750 "HARDWARE addr space,NOT supported yet\n");
753 wrmsr_safe(MSR_IA32_THERM_CONTROL,
754 msr & 0xffffffff, msr >> 32);
760 static int acpi_throttling_rdmsr(u64 *value)
762 printk(KERN_ERR PREFIX
763 "HARDWARE addr space,NOT supported yet\n");
767 static int acpi_throttling_wrmsr(u64 value)
769 printk(KERN_ERR PREFIX
770 "HARDWARE addr space,NOT supported yet\n");
775 static int acpi_read_throttling_status(struct acpi_processor *pr,
778 u32 bit_width, bit_offset;
781 struct acpi_processor_throttling *throttling;
784 throttling = &pr->throttling;
785 switch (throttling->status_register.space_id) {
786 case ACPI_ADR_SPACE_SYSTEM_IO:
787 bit_width = throttling->status_register.bit_width;
788 bit_offset = throttling->status_register.bit_offset;
790 acpi_os_read_port((acpi_io_address) throttling->status_register.
792 (u32) (bit_width + bit_offset));
793 ptc_mask = (1 << bit_width) - 1;
794 *value = (u64) ((ptc_value >> bit_offset) & ptc_mask);
797 case ACPI_ADR_SPACE_FIXED_HARDWARE:
798 ret = acpi_throttling_rdmsr(value);
801 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
802 (u32) (throttling->status_register.space_id));
807 static int acpi_write_throttling_state(struct acpi_processor *pr,
810 u32 bit_width, bit_offset;
813 struct acpi_processor_throttling *throttling;
816 throttling = &pr->throttling;
817 switch (throttling->control_register.space_id) {
818 case ACPI_ADR_SPACE_SYSTEM_IO:
819 bit_width = throttling->control_register.bit_width;
820 bit_offset = throttling->control_register.bit_offset;
821 ptc_mask = (1 << bit_width) - 1;
822 ptc_value = value & ptc_mask;
824 acpi_os_write_port((acpi_io_address) throttling->
825 control_register.address,
826 (u32) (ptc_value << bit_offset),
827 (u32) (bit_width + bit_offset));
830 case ACPI_ADR_SPACE_FIXED_HARDWARE:
831 ret = acpi_throttling_wrmsr(value);
834 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
835 (u32) (throttling->control_register.space_id));
840 static int acpi_get_throttling_state(struct acpi_processor *pr,
845 for (i = 0; i < pr->throttling.state_count; i++) {
846 struct acpi_processor_tx_tss *tx =
847 (struct acpi_processor_tx_tss *)&(pr->throttling.
849 if (tx->control == value)
855 static int acpi_get_throttling_value(struct acpi_processor *pr,
856 int state, u64 *value)
860 if (state >= 0 && state <= pr->throttling.state_count) {
861 struct acpi_processor_tx_tss *tx =
862 (struct acpi_processor_tx_tss *)&(pr->throttling.
864 *value = tx->control;
870 static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
879 if (!pr->flags.throttling)
882 pr->throttling.state = 0;
885 ret = acpi_read_throttling_status(pr, &value);
887 state = acpi_get_throttling_state(pr, value);
889 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
890 "Invalid throttling state, reset\n"));
892 ret = acpi_processor_set_throttling(pr, state, true);
896 pr->throttling.state = state;
902 static int acpi_processor_get_throttling(struct acpi_processor *pr)
904 cpumask_var_t saved_mask;
910 if (!pr->flags.throttling)
913 if (!alloc_cpumask_var(&saved_mask, GFP_KERNEL))
917 * Migrate task to the cpu pointed by pr.
919 cpumask_copy(saved_mask, ¤t->cpus_allowed);
920 /* FIXME: use work_on_cpu() */
921 if (set_cpus_allowed_ptr(current, cpumask_of(pr->id))) {
922 /* Can't migrate to the target pr->id CPU. Exit */
923 free_cpumask_var(saved_mask);
926 ret = pr->throttling.acpi_processor_get_throttling(pr);
927 /* restore the previous state */
928 set_cpus_allowed_ptr(current, saved_mask);
929 free_cpumask_var(saved_mask);
934 static int acpi_processor_get_fadt_info(struct acpi_processor *pr)
938 if (!pr->throttling.address) {
939 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
941 } else if (!pr->throttling.duty_width) {
942 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
945 /* TBD: Support duty_cycle values that span bit 4. */
946 else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
947 printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");
951 pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
954 * Compute state values. Note that throttling displays a linear power
955 * performance relationship (at 50% performance the CPU will consume
956 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
959 step = (1000 / pr->throttling.state_count);
961 for (i = 0; i < pr->throttling.state_count; i++) {
962 pr->throttling.states[i].performance = 1000 - step * i;
963 pr->throttling.states[i].power = 1000 - step * i;
968 static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
969 int state, bool force)
978 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
981 if (!pr->flags.throttling)
984 if (!force && (state == pr->throttling.state))
987 if (state < pr->throttling_platform_limit)
990 * Calculate the duty_value and duty_mask.
993 duty_value = pr->throttling.state_count - state;
995 duty_value <<= pr->throttling.duty_offset;
997 /* Used to clear all duty_value bits */
998 duty_mask = pr->throttling.state_count - 1;
1000 duty_mask <<= acpi_gbl_FADT.duty_offset;
1001 duty_mask = ~duty_mask;
1004 local_irq_disable();
1007 * Disable throttling by writing a 0 to bit 4. Note that we must
1008 * turn it off before you can change the duty_value.
1010 value = inl(pr->throttling.address);
1012 value &= 0xFFFFFFEF;
1013 outl(value, pr->throttling.address);
1017 * Write the new duty_value and then enable throttling. Note
1018 * that a state value of 0 leaves throttling disabled.
1022 value |= duty_value;
1023 outl(value, pr->throttling.address);
1025 value |= 0x00000010;
1026 outl(value, pr->throttling.address);
1029 pr->throttling.state = state;
1033 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1034 "Throttling state set to T%d (%d%%)\n", state,
1035 (pr->throttling.states[state].performance ? pr->
1036 throttling.states[state].performance / 10 : 0)));
1041 static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
1042 int state, bool force)
1050 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1053 if (!pr->flags.throttling)
1056 if (!force && (state == pr->throttling.state))
1059 if (state < pr->throttling_platform_limit)
1063 ret = acpi_get_throttling_value(pr, state, &value);
1065 acpi_write_throttling_state(pr, value);
1066 pr->throttling.state = state;
1072 static long acpi_processor_throttling_fn(void *data)
1074 struct acpi_processor_throttling_arg *arg = data;
1075 struct acpi_processor *pr = arg->pr;
1077 return pr->throttling.acpi_processor_set_throttling(pr,
1078 arg->target_state, arg->force);
1081 int acpi_processor_set_throttling(struct acpi_processor *pr,
1082 int state, bool force)
1086 struct acpi_processor *match_pr;
1087 struct acpi_processor_throttling *p_throttling;
1088 struct acpi_processor_throttling_arg arg;
1089 struct throttling_tstate t_state;
1094 if (!pr->flags.throttling)
1097 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1100 if (cpu_is_offline(pr->id)) {
1102 * the cpu pointed by pr->id is offline. Unnecessary to change
1103 * the throttling state any more.
1108 t_state.target_state = state;
1109 p_throttling = &(pr->throttling);
1112 * The throttling notifier will be called for every
1113 * affected cpu in order to get one proper T-state.
1114 * The notifier event is THROTTLING_PRECHANGE.
1116 for_each_cpu_and(i, cpu_online_mask, p_throttling->shared_cpu_map) {
1118 acpi_processor_throttling_notifier(THROTTLING_PRECHANGE,
1122 * The function of acpi_processor_set_throttling will be called
1123 * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
1124 * it is necessary to call it for every affected cpu. Otherwise
1125 * it can be called only for the cpu pointed by pr.
1127 if (p_throttling->shared_type == DOMAIN_COORD_TYPE_SW_ANY) {
1129 arg.target_state = state;
1131 ret = work_on_cpu(pr->id, acpi_processor_throttling_fn, &arg);
1134 * When the T-state coordination is SW_ALL or HW_ALL,
1135 * it is necessary to set T-state for every affected
1138 for_each_cpu_and(i, cpu_online_mask,
1139 p_throttling->shared_cpu_map) {
1140 match_pr = per_cpu(processors, i);
1142 * If the pointer is invalid, we will report the
1143 * error message and continue.
1146 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1147 "Invalid Pointer for CPU %d\n", i));
1151 * If the throttling control is unsupported on CPU i,
1152 * we will report the error message and continue.
1154 if (!match_pr->flags.throttling) {
1155 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1156 "Throttling Control is unsupported "
1162 arg.target_state = state;
1164 ret = work_on_cpu(pr->id, acpi_processor_throttling_fn,
1169 * After the set_throttling is called, the
1170 * throttling notifier is called for every
1171 * affected cpu to update the T-states.
1172 * The notifier event is THROTTLING_POSTCHANGE
1174 for_each_cpu_and(i, cpu_online_mask, p_throttling->shared_cpu_map) {
1176 acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE,
1183 int acpi_processor_get_throttling_info(struct acpi_processor *pr)
1186 struct acpi_processor_throttling *pthrottling;
1188 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1189 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1190 pr->throttling.address,
1191 pr->throttling.duty_offset,
1192 pr->throttling.duty_width));
1195 * Evaluate _PTC, _TSS and _TPC
1196 * They must all be present or none of them can be used.
1198 if (acpi_processor_get_throttling_control(pr) ||
1199 acpi_processor_get_throttling_states(pr) ||
1200 acpi_processor_get_platform_limit(pr))
1202 pr->throttling.acpi_processor_get_throttling =
1203 &acpi_processor_get_throttling_fadt;
1204 pr->throttling.acpi_processor_set_throttling =
1205 &acpi_processor_set_throttling_fadt;
1206 if (acpi_processor_get_fadt_info(pr))
1209 pr->throttling.acpi_processor_get_throttling =
1210 &acpi_processor_get_throttling_ptc;
1211 pr->throttling.acpi_processor_set_throttling =
1212 &acpi_processor_set_throttling_ptc;
1216 * If TSD package for one CPU can't be parsed successfully, it means
1217 * that this CPU will have no coordination with other CPUs.
1219 if (acpi_processor_get_tsd(pr)) {
1220 pthrottling = &pr->throttling;
1221 pthrottling->tsd_valid_flag = 0;
1222 cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
1223 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
1227 * PIIX4 Errata: We don't support throttling on the original PIIX4.
1228 * This shouldn't be an issue as few (if any) mobile systems ever
1231 if (errata.piix4.throttle) {
1232 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1233 "Throttling not supported on PIIX4 A- or B-step\n"));
1237 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
1238 pr->throttling.state_count));
1240 pr->flags.throttling = 1;
1243 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
1244 * thermal) decide to lower performance if it so chooses, but for now
1245 * we'll crank up the speed.
1248 result = acpi_processor_get_throttling(pr);
1252 if (pr->throttling.state) {
1253 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1254 "Disabling throttling (was T%d)\n",
1255 pr->throttling.state));
1256 result = acpi_processor_set_throttling(pr, 0, false);
1263 pr->flags.throttling = 0;