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>
35 #ifdef CONFIG_ACPI_PROCFS
36 #include <linux/proc_fs.h>
37 #include <linux/seq_file.h>
41 #include <asm/uaccess.h>
43 #include <acpi/acpi_bus.h>
44 #include <acpi/acpi_drivers.h>
45 #include <acpi/processor.h>
47 #define PREFIX "ACPI: "
49 #define ACPI_PROCESSOR_CLASS "processor"
50 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
51 ACPI_MODULE_NAME("processor_throttling");
54 * 0 -> acpi processor driver doesn't ignore _TPC values
55 * 1 -> acpi processor driver ignores _TPC values
57 static int ignore_tpc
;
58 module_param(ignore_tpc
, int, 0644);
59 MODULE_PARM_DESC(ignore_tpc
, "Disable broken BIOS _TPC throttling support");
61 struct throttling_tstate
{
62 unsigned int cpu
; /* cpu nr */
63 int target_state
; /* target T-state */
66 #define THROTTLING_PRECHANGE (1)
67 #define THROTTLING_POSTCHANGE (2)
69 static int acpi_processor_get_throttling(struct acpi_processor
*pr
);
70 int acpi_processor_set_throttling(struct acpi_processor
*pr
,
71 int state
, bool force
);
73 static int acpi_processor_update_tsd_coord(void)
75 int count
, count_target
;
78 cpumask_var_t covered_cpus
;
79 struct acpi_processor
*pr
, *match_pr
;
80 struct acpi_tsd_package
*pdomain
, *match_pdomain
;
81 struct acpi_processor_throttling
*pthrottling
, *match_pthrottling
;
83 if (!zalloc_cpumask_var(&covered_cpus
, GFP_KERNEL
))
87 * Now that we have _TSD data from all CPUs, lets setup T-state
88 * coordination between all CPUs.
90 for_each_possible_cpu(i
) {
91 pr
= per_cpu(processors
, i
);
95 /* Basic validity check for domain info */
96 pthrottling
= &(pr
->throttling
);
99 * If tsd package for one cpu is invalid, the coordination
100 * among all CPUs is thought as invalid.
103 if (!pthrottling
->tsd_valid_flag
) {
111 for_each_possible_cpu(i
) {
112 pr
= per_cpu(processors
, i
);
116 if (cpumask_test_cpu(i
, covered_cpus
))
118 pthrottling
= &pr
->throttling
;
120 pdomain
= &(pthrottling
->domain_info
);
121 cpumask_set_cpu(i
, pthrottling
->shared_cpu_map
);
122 cpumask_set_cpu(i
, covered_cpus
);
124 * If the number of processor in the TSD domain is 1, it is
125 * unnecessary to parse the coordination for this CPU.
127 if (pdomain
->num_processors
<= 1)
130 /* Validate the Domain info */
131 count_target
= pdomain
->num_processors
;
134 for_each_possible_cpu(j
) {
138 match_pr
= per_cpu(processors
, j
);
142 match_pthrottling
= &(match_pr
->throttling
);
143 match_pdomain
= &(match_pthrottling
->domain_info
);
144 if (match_pdomain
->domain
!= pdomain
->domain
)
147 /* Here i and j are in the same domain.
148 * If two TSD packages have the same domain, they
149 * should have the same num_porcessors and
150 * coordination type. Otherwise it will be regarded
153 if (match_pdomain
->num_processors
!= count_target
) {
158 if (pdomain
->coord_type
!= match_pdomain
->coord_type
) {
163 cpumask_set_cpu(j
, covered_cpus
);
164 cpumask_set_cpu(j
, pthrottling
->shared_cpu_map
);
167 for_each_possible_cpu(j
) {
171 match_pr
= per_cpu(processors
, j
);
175 match_pthrottling
= &(match_pr
->throttling
);
176 match_pdomain
= &(match_pthrottling
->domain_info
);
177 if (match_pdomain
->domain
!= pdomain
->domain
)
181 * If some CPUS have the same domain, they
182 * will have the same shared_cpu_map.
184 cpumask_copy(match_pthrottling
->shared_cpu_map
,
185 pthrottling
->shared_cpu_map
);
190 free_cpumask_var(covered_cpus
);
192 for_each_possible_cpu(i
) {
193 pr
= per_cpu(processors
, i
);
198 * Assume no coordination on any error parsing domain info.
199 * The coordination type will be forced as SW_ALL.
202 pthrottling
= &(pr
->throttling
);
203 cpumask_clear(pthrottling
->shared_cpu_map
);
204 cpumask_set_cpu(i
, pthrottling
->shared_cpu_map
);
205 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
213 * Update the T-state coordination after the _TSD
214 * data for all cpus is obtained.
216 void acpi_processor_throttling_init(void)
218 if (acpi_processor_update_tsd_coord())
219 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
220 "Assume no T-state coordination\n"));
225 static int acpi_processor_throttling_notifier(unsigned long event
, void *data
)
227 struct throttling_tstate
*p_tstate
= data
;
228 struct acpi_processor
*pr
;
231 struct acpi_processor_limit
*p_limit
;
232 struct acpi_processor_throttling
*p_throttling
;
235 pr
= per_cpu(processors
, cpu
);
237 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Invalid pr pointer\n"));
240 if (!pr
->flags
.throttling
) {
241 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Throttling control is "
242 "unsupported on CPU %d\n", cpu
));
245 target_state
= p_tstate
->target_state
;
246 p_throttling
= &(pr
->throttling
);
248 case THROTTLING_PRECHANGE
:
250 * Prechange event is used to choose one proper t-state,
251 * which meets the limits of thermal, user and _TPC.
253 p_limit
= &pr
->limit
;
254 if (p_limit
->thermal
.tx
> target_state
)
255 target_state
= p_limit
->thermal
.tx
;
256 if (p_limit
->user
.tx
> target_state
)
257 target_state
= p_limit
->user
.tx
;
258 if (pr
->throttling_platform_limit
> target_state
)
259 target_state
= pr
->throttling_platform_limit
;
260 if (target_state
>= p_throttling
->state_count
) {
262 "Exceed the limit of T-state \n");
263 target_state
= p_throttling
->state_count
- 1;
265 p_tstate
->target_state
= target_state
;
266 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "PreChange Event:"
267 "target T-state of CPU %d is T%d\n",
270 case THROTTLING_POSTCHANGE
:
272 * Postchange event is only used to update the
273 * T-state flag of acpi_processor_throttling.
275 p_throttling
->state
= target_state
;
276 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "PostChange Event:"
277 "CPU %d is switched to T%d\n",
282 "Unsupported Throttling notifier event\n");
290 * _TPC - Throttling Present Capabilities
292 static int acpi_processor_get_platform_limit(struct acpi_processor
*pr
)
294 acpi_status status
= 0;
295 unsigned long long tpc
= 0;
303 status
= acpi_evaluate_integer(pr
->handle
, "_TPC", NULL
, &tpc
);
304 if (ACPI_FAILURE(status
)) {
305 if (status
!= AE_NOT_FOUND
) {
306 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TPC"));
312 pr
->throttling_platform_limit
= (int)tpc
;
316 int acpi_processor_tstate_has_changed(struct acpi_processor
*pr
)
319 int throttling_limit
;
321 struct acpi_processor_limit
*limit
;
327 result
= acpi_processor_get_platform_limit(pr
);
329 /* Throttling Limit is unsupported */
333 throttling_limit
= pr
->throttling_platform_limit
;
334 if (throttling_limit
>= pr
->throttling
.state_count
) {
335 /* Uncorrect Throttling Limit */
339 current_state
= pr
->throttling
.state
;
340 if (current_state
> throttling_limit
) {
342 * The current state can meet the requirement of
343 * _TPC limit. But it is reasonable that OSPM changes
344 * t-states from high to low for better performance.
345 * Of course the limit condition of thermal
346 * and user should be considered.
349 target_state
= throttling_limit
;
350 if (limit
->thermal
.tx
> target_state
)
351 target_state
= limit
->thermal
.tx
;
352 if (limit
->user
.tx
> target_state
)
353 target_state
= limit
->user
.tx
;
354 } else if (current_state
== throttling_limit
) {
356 * Unnecessary to change the throttling state
361 * If the current state is lower than the limit of _TPC, it
362 * will be forced to switch to the throttling state defined
363 * by throttling_platfor_limit.
364 * Because the previous state meets with the limit condition
365 * of thermal and user, it is unnecessary to check it again.
367 target_state
= throttling_limit
;
369 return acpi_processor_set_throttling(pr
, target_state
, false);
373 * _PTC - Processor Throttling Control (and status) register location
375 static int acpi_processor_get_throttling_control(struct acpi_processor
*pr
)
378 acpi_status status
= 0;
379 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
380 union acpi_object
*ptc
= NULL
;
381 union acpi_object obj
= { 0 };
382 struct acpi_processor_throttling
*throttling
;
384 status
= acpi_evaluate_object(pr
->handle
, "_PTC", NULL
, &buffer
);
385 if (ACPI_FAILURE(status
)) {
386 if (status
!= AE_NOT_FOUND
) {
387 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PTC"));
392 ptc
= (union acpi_object
*)buffer
.pointer
;
393 if (!ptc
|| (ptc
->type
!= ACPI_TYPE_PACKAGE
)
394 || (ptc
->package
.count
!= 2)) {
395 printk(KERN_ERR PREFIX
"Invalid _PTC data\n");
404 obj
= ptc
->package
.elements
[0];
406 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
407 || (obj
.buffer
.length
< sizeof(struct acpi_ptc_register
))
408 || (obj
.buffer
.pointer
== NULL
)) {
409 printk(KERN_ERR PREFIX
410 "Invalid _PTC data (control_register)\n");
414 memcpy(&pr
->throttling
.control_register
, obj
.buffer
.pointer
,
415 sizeof(struct acpi_ptc_register
));
421 obj
= ptc
->package
.elements
[1];
423 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
424 || (obj
.buffer
.length
< sizeof(struct acpi_ptc_register
))
425 || (obj
.buffer
.pointer
== NULL
)) {
426 printk(KERN_ERR PREFIX
"Invalid _PTC data (status_register)\n");
431 memcpy(&pr
->throttling
.status_register
, obj
.buffer
.pointer
,
432 sizeof(struct acpi_ptc_register
));
434 throttling
= &pr
->throttling
;
436 if ((throttling
->control_register
.bit_width
+
437 throttling
->control_register
.bit_offset
) > 32) {
438 printk(KERN_ERR PREFIX
"Invalid _PTC control register\n");
443 if ((throttling
->status_register
.bit_width
+
444 throttling
->status_register
.bit_offset
) > 32) {
445 printk(KERN_ERR PREFIX
"Invalid _PTC status register\n");
451 kfree(buffer
.pointer
);
457 * _TSS - Throttling Supported States
459 static int acpi_processor_get_throttling_states(struct acpi_processor
*pr
)
462 acpi_status status
= AE_OK
;
463 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
464 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
465 struct acpi_buffer state
= { 0, NULL
};
466 union acpi_object
*tss
= NULL
;
469 status
= acpi_evaluate_object(pr
->handle
, "_TSS", NULL
, &buffer
);
470 if (ACPI_FAILURE(status
)) {
471 if (status
!= AE_NOT_FOUND
) {
472 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TSS"));
477 tss
= buffer
.pointer
;
478 if (!tss
|| (tss
->type
!= ACPI_TYPE_PACKAGE
)) {
479 printk(KERN_ERR PREFIX
"Invalid _TSS data\n");
484 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d throttling states\n",
485 tss
->package
.count
));
487 pr
->throttling
.state_count
= tss
->package
.count
;
488 pr
->throttling
.states_tss
=
489 kmalloc(sizeof(struct acpi_processor_tx_tss
) * tss
->package
.count
,
491 if (!pr
->throttling
.states_tss
) {
496 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
498 struct acpi_processor_tx_tss
*tx
=
499 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
502 state
.length
= sizeof(struct acpi_processor_tx_tss
);
505 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Extracting state %d\n", i
));
507 status
= acpi_extract_package(&(tss
->package
.elements
[i
]),
509 if (ACPI_FAILURE(status
)) {
510 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _TSS data"));
512 kfree(pr
->throttling
.states_tss
);
516 if (!tx
->freqpercentage
) {
517 printk(KERN_ERR PREFIX
518 "Invalid _TSS data: freq is zero\n");
520 kfree(pr
->throttling
.states_tss
);
526 kfree(buffer
.pointer
);
532 * _TSD - T-State Dependencies
534 static int acpi_processor_get_tsd(struct acpi_processor
*pr
)
537 acpi_status status
= AE_OK
;
538 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
539 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
540 struct acpi_buffer state
= { 0, NULL
};
541 union acpi_object
*tsd
= NULL
;
542 struct acpi_tsd_package
*pdomain
;
543 struct acpi_processor_throttling
*pthrottling
;
545 pthrottling
= &pr
->throttling
;
546 pthrottling
->tsd_valid_flag
= 0;
548 status
= acpi_evaluate_object(pr
->handle
, "_TSD", NULL
, &buffer
);
549 if (ACPI_FAILURE(status
)) {
550 if (status
!= AE_NOT_FOUND
) {
551 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TSD"));
556 tsd
= buffer
.pointer
;
557 if (!tsd
|| (tsd
->type
!= ACPI_TYPE_PACKAGE
)) {
558 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
563 if (tsd
->package
.count
!= 1) {
564 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
569 pdomain
= &(pr
->throttling
.domain_info
);
571 state
.length
= sizeof(struct acpi_tsd_package
);
572 state
.pointer
= pdomain
;
574 status
= acpi_extract_package(&(tsd
->package
.elements
[0]),
576 if (ACPI_FAILURE(status
)) {
577 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
582 if (pdomain
->num_entries
!= ACPI_TSD_REV0_ENTRIES
) {
583 printk(KERN_ERR PREFIX
"Unknown _TSD:num_entries\n");
588 if (pdomain
->revision
!= ACPI_TSD_REV0_REVISION
) {
589 printk(KERN_ERR PREFIX
"Unknown _TSD:revision\n");
594 pthrottling
= &pr
->throttling
;
595 pthrottling
->tsd_valid_flag
= 1;
596 pthrottling
->shared_type
= pdomain
->coord_type
;
597 cpumask_set_cpu(pr
->id
, pthrottling
->shared_cpu_map
);
599 * If the coordination type is not defined in ACPI spec,
600 * the tsd_valid_flag will be clear and coordination type
601 * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
603 if (pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ALL
&&
604 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ANY
&&
605 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_HW_ALL
) {
606 pthrottling
->tsd_valid_flag
= 0;
607 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
611 kfree(buffer
.pointer
);
615 /* --------------------------------------------------------------------------
617 -------------------------------------------------------------------------- */
618 static int acpi_processor_get_throttling_fadt(struct acpi_processor
*pr
)
628 if (!pr
->flags
.throttling
)
631 pr
->throttling
.state
= 0;
633 duty_mask
= pr
->throttling
.state_count
- 1;
635 duty_mask
<<= pr
->throttling
.duty_offset
;
639 value
= inl(pr
->throttling
.address
);
642 * Compute the current throttling state when throttling is enabled
646 duty_value
= value
& duty_mask
;
647 duty_value
>>= pr
->throttling
.duty_offset
;
650 state
= pr
->throttling
.state_count
- duty_value
;
653 pr
->throttling
.state
= state
;
657 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
658 "Throttling state is T%d (%d%% throttling applied)\n",
659 state
, pr
->throttling
.states
[state
].performance
));
665 static int acpi_throttling_rdmsr(struct acpi_processor
*pr
,
668 struct cpuinfo_x86
*c
;
669 u64 msr_high
, msr_low
;
677 if ((c
->x86_vendor
!= X86_VENDOR_INTEL
) ||
678 !cpu_has(c
, X86_FEATURE_ACPI
)) {
679 printk(KERN_ERR PREFIX
680 "HARDWARE addr space,NOT supported yet\n");
684 rdmsr_safe(MSR_IA32_THERM_CONTROL
,
685 (u32
*)&msr_low
, (u32
*) &msr_high
);
686 msr
= (msr_high
<< 32) | msr_low
;
693 static int acpi_throttling_wrmsr(struct acpi_processor
*pr
, u64 value
)
695 struct cpuinfo_x86
*c
;
703 if ((c
->x86_vendor
!= X86_VENDOR_INTEL
) ||
704 !cpu_has(c
, X86_FEATURE_ACPI
)) {
705 printk(KERN_ERR PREFIX
706 "HARDWARE addr space,NOT supported yet\n");
709 wrmsr_safe(MSR_IA32_THERM_CONTROL
,
710 msr
& 0xffffffff, msr
>> 32);
716 static int acpi_throttling_rdmsr(struct acpi_processor
*pr
,
719 printk(KERN_ERR PREFIX
720 "HARDWARE addr space,NOT supported yet\n");
724 static int acpi_throttling_wrmsr(struct acpi_processor
*pr
, u64 value
)
726 printk(KERN_ERR PREFIX
727 "HARDWARE addr space,NOT supported yet\n");
732 static int acpi_read_throttling_status(struct acpi_processor
*pr
,
735 u32 bit_width
, bit_offset
;
738 struct acpi_processor_throttling
*throttling
;
741 throttling
= &pr
->throttling
;
742 switch (throttling
->status_register
.space_id
) {
743 case ACPI_ADR_SPACE_SYSTEM_IO
:
745 bit_width
= throttling
->status_register
.bit_width
;
746 bit_offset
= throttling
->status_register
.bit_offset
;
748 acpi_os_read_port((acpi_io_address
) throttling
->status_register
.
749 address
, (u32
*) &ptc_value
,
750 (u32
) (bit_width
+ bit_offset
));
751 ptc_mask
= (1 << bit_width
) - 1;
752 *value
= (u64
) ((ptc_value
>> bit_offset
) & ptc_mask
);
755 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
756 ret
= acpi_throttling_rdmsr(pr
, value
);
759 printk(KERN_ERR PREFIX
"Unknown addr space %d\n",
760 (u32
) (throttling
->status_register
.space_id
));
765 static int acpi_write_throttling_state(struct acpi_processor
*pr
,
768 u32 bit_width
, bit_offset
;
771 struct acpi_processor_throttling
*throttling
;
774 throttling
= &pr
->throttling
;
775 switch (throttling
->control_register
.space_id
) {
776 case ACPI_ADR_SPACE_SYSTEM_IO
:
777 bit_width
= throttling
->control_register
.bit_width
;
778 bit_offset
= throttling
->control_register
.bit_offset
;
779 ptc_mask
= (1 << bit_width
) - 1;
780 ptc_value
= value
& ptc_mask
;
782 acpi_os_write_port((acpi_io_address
) throttling
->
783 control_register
.address
,
784 (u32
) (ptc_value
<< bit_offset
),
785 (u32
) (bit_width
+ bit_offset
));
788 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
789 ret
= acpi_throttling_wrmsr(pr
, value
);
792 printk(KERN_ERR PREFIX
"Unknown addr space %d\n",
793 (u32
) (throttling
->control_register
.space_id
));
798 static int acpi_get_throttling_state(struct acpi_processor
*pr
,
803 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
804 struct acpi_processor_tx_tss
*tx
=
805 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
807 if (tx
->control
== value
)
813 static int acpi_get_throttling_value(struct acpi_processor
*pr
,
814 int state
, u64
*value
)
818 if (state
>= 0 && state
<= pr
->throttling
.state_count
) {
819 struct acpi_processor_tx_tss
*tx
=
820 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
822 *value
= tx
->control
;
828 static int acpi_processor_get_throttling_ptc(struct acpi_processor
*pr
)
837 if (!pr
->flags
.throttling
)
840 pr
->throttling
.state
= 0;
843 ret
= acpi_read_throttling_status(pr
, &value
);
845 state
= acpi_get_throttling_state(pr
, value
);
847 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
848 "Invalid throttling state, reset\n"));
850 ret
= acpi_processor_set_throttling(pr
, state
, true);
854 pr
->throttling
.state
= state
;
860 static int acpi_processor_get_throttling(struct acpi_processor
*pr
)
862 cpumask_var_t saved_mask
;
868 if (!pr
->flags
.throttling
)
871 if (!alloc_cpumask_var(&saved_mask
, GFP_KERNEL
))
875 * Migrate task to the cpu pointed by pr.
877 cpumask_copy(saved_mask
, ¤t
->cpus_allowed
);
878 /* FIXME: use work_on_cpu() */
879 set_cpus_allowed_ptr(current
, cpumask_of(pr
->id
));
880 ret
= pr
->throttling
.acpi_processor_get_throttling(pr
);
881 /* restore the previous state */
882 set_cpus_allowed_ptr(current
, saved_mask
);
883 free_cpumask_var(saved_mask
);
888 static int acpi_processor_get_fadt_info(struct acpi_processor
*pr
)
892 if (!pr
->throttling
.address
) {
893 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No throttling register\n"));
895 } else if (!pr
->throttling
.duty_width
) {
896 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No throttling states\n"));
899 /* TBD: Support duty_cycle values that span bit 4. */
900 else if ((pr
->throttling
.duty_offset
+ pr
->throttling
.duty_width
) > 4) {
901 printk(KERN_WARNING PREFIX
"duty_cycle spans bit 4\n");
905 pr
->throttling
.state_count
= 1 << acpi_gbl_FADT
.duty_width
;
908 * Compute state values. Note that throttling displays a linear power
909 * performance relationship (at 50% performance the CPU will consume
910 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
913 step
= (1000 / pr
->throttling
.state_count
);
915 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
916 pr
->throttling
.states
[i
].performance
= 1000 - step
* i
;
917 pr
->throttling
.states
[i
].power
= 1000 - step
* i
;
922 static int acpi_processor_set_throttling_fadt(struct acpi_processor
*pr
,
923 int state
, bool force
)
932 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
935 if (!pr
->flags
.throttling
)
938 if (!force
&& (state
== pr
->throttling
.state
))
941 if (state
< pr
->throttling_platform_limit
)
944 * Calculate the duty_value and duty_mask.
947 duty_value
= pr
->throttling
.state_count
- state
;
949 duty_value
<<= pr
->throttling
.duty_offset
;
951 /* Used to clear all duty_value bits */
952 duty_mask
= pr
->throttling
.state_count
- 1;
954 duty_mask
<<= acpi_gbl_FADT
.duty_offset
;
955 duty_mask
= ~duty_mask
;
961 * Disable throttling by writing a 0 to bit 4. Note that we must
962 * turn it off before you can change the duty_value.
964 value
= inl(pr
->throttling
.address
);
967 outl(value
, pr
->throttling
.address
);
971 * Write the new duty_value and then enable throttling. Note
972 * that a state value of 0 leaves throttling disabled.
977 outl(value
, pr
->throttling
.address
);
980 outl(value
, pr
->throttling
.address
);
983 pr
->throttling
.state
= state
;
987 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
988 "Throttling state set to T%d (%d%%)\n", state
,
989 (pr
->throttling
.states
[state
].performance
? pr
->
990 throttling
.states
[state
].performance
/ 10 : 0)));
995 static int acpi_processor_set_throttling_ptc(struct acpi_processor
*pr
,
996 int state
, bool force
)
1004 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
1007 if (!pr
->flags
.throttling
)
1010 if (!force
&& (state
== pr
->throttling
.state
))
1013 if (state
< pr
->throttling_platform_limit
)
1017 ret
= acpi_get_throttling_value(pr
, state
, &value
);
1019 acpi_write_throttling_state(pr
, value
);
1020 pr
->throttling
.state
= state
;
1026 int acpi_processor_set_throttling(struct acpi_processor
*pr
,
1027 int state
, bool force
)
1029 cpumask_var_t saved_mask
;
1032 struct acpi_processor
*match_pr
;
1033 struct acpi_processor_throttling
*p_throttling
;
1034 struct throttling_tstate t_state
;
1035 cpumask_var_t online_throttling_cpus
;
1040 if (!pr
->flags
.throttling
)
1043 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
1046 if (!alloc_cpumask_var(&saved_mask
, GFP_KERNEL
))
1049 if (!alloc_cpumask_var(&online_throttling_cpus
, GFP_KERNEL
)) {
1050 free_cpumask_var(saved_mask
);
1054 cpumask_copy(saved_mask
, ¤t
->cpus_allowed
);
1055 t_state
.target_state
= state
;
1056 p_throttling
= &(pr
->throttling
);
1057 cpumask_and(online_throttling_cpus
, cpu_online_mask
,
1058 p_throttling
->shared_cpu_map
);
1060 * The throttling notifier will be called for every
1061 * affected cpu in order to get one proper T-state.
1062 * The notifier event is THROTTLING_PRECHANGE.
1064 for_each_cpu(i
, online_throttling_cpus
) {
1066 acpi_processor_throttling_notifier(THROTTLING_PRECHANGE
,
1070 * The function of acpi_processor_set_throttling will be called
1071 * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
1072 * it is necessary to call it for every affected cpu. Otherwise
1073 * it can be called only for the cpu pointed by pr.
1075 if (p_throttling
->shared_type
== DOMAIN_COORD_TYPE_SW_ANY
) {
1076 /* FIXME: use work_on_cpu() */
1077 set_cpus_allowed_ptr(current
, cpumask_of(pr
->id
));
1078 ret
= p_throttling
->acpi_processor_set_throttling(pr
,
1079 t_state
.target_state
, force
);
1082 * When the T-state coordination is SW_ALL or HW_ALL,
1083 * it is necessary to set T-state for every affected
1086 for_each_cpu(i
, online_throttling_cpus
) {
1087 match_pr
= per_cpu(processors
, i
);
1089 * If the pointer is invalid, we will report the
1090 * error message and continue.
1093 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1094 "Invalid Pointer for CPU %d\n", i
));
1098 * If the throttling control is unsupported on CPU i,
1099 * we will report the error message and continue.
1101 if (!match_pr
->flags
.throttling
) {
1102 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1103 "Throttling Controll is unsupported "
1108 /* FIXME: use work_on_cpu() */
1109 set_cpus_allowed_ptr(current
, cpumask_of(i
));
1110 ret
= match_pr
->throttling
.
1111 acpi_processor_set_throttling(
1112 match_pr
, t_state
.target_state
, force
);
1116 * After the set_throttling is called, the
1117 * throttling notifier is called for every
1118 * affected cpu to update the T-states.
1119 * The notifier event is THROTTLING_POSTCHANGE
1121 for_each_cpu(i
, online_throttling_cpus
) {
1123 acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE
,
1126 /* restore the previous state */
1127 /* FIXME: use work_on_cpu() */
1128 set_cpus_allowed_ptr(current
, saved_mask
);
1129 free_cpumask_var(online_throttling_cpus
);
1130 free_cpumask_var(saved_mask
);
1134 int acpi_processor_get_throttling_info(struct acpi_processor
*pr
)
1137 struct acpi_processor_throttling
*pthrottling
;
1139 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1140 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1141 pr
->throttling
.address
,
1142 pr
->throttling
.duty_offset
,
1143 pr
->throttling
.duty_width
));
1146 * Evaluate _PTC, _TSS and _TPC
1147 * They must all be present or none of them can be used.
1149 if (acpi_processor_get_throttling_control(pr
) ||
1150 acpi_processor_get_throttling_states(pr
) ||
1151 acpi_processor_get_platform_limit(pr
))
1153 pr
->throttling
.acpi_processor_get_throttling
=
1154 &acpi_processor_get_throttling_fadt
;
1155 pr
->throttling
.acpi_processor_set_throttling
=
1156 &acpi_processor_set_throttling_fadt
;
1157 if (acpi_processor_get_fadt_info(pr
))
1160 pr
->throttling
.acpi_processor_get_throttling
=
1161 &acpi_processor_get_throttling_ptc
;
1162 pr
->throttling
.acpi_processor_set_throttling
=
1163 &acpi_processor_set_throttling_ptc
;
1167 * If TSD package for one CPU can't be parsed successfully, it means
1168 * that this CPU will have no coordination with other CPUs.
1170 if (acpi_processor_get_tsd(pr
)) {
1171 pthrottling
= &pr
->throttling
;
1172 pthrottling
->tsd_valid_flag
= 0;
1173 cpumask_set_cpu(pr
->id
, pthrottling
->shared_cpu_map
);
1174 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
1178 * PIIX4 Errata: We don't support throttling on the original PIIX4.
1179 * This shouldn't be an issue as few (if any) mobile systems ever
1182 if (errata
.piix4
.throttle
) {
1183 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1184 "Throttling not supported on PIIX4 A- or B-step\n"));
1188 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d throttling states\n",
1189 pr
->throttling
.state_count
));
1191 pr
->flags
.throttling
= 1;
1194 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
1195 * thermal) decide to lower performance if it so chooses, but for now
1196 * we'll crank up the speed.
1199 result
= acpi_processor_get_throttling(pr
);
1203 if (pr
->throttling
.state
) {
1204 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1205 "Disabling throttling (was T%d)\n",
1206 pr
->throttling
.state
));
1207 result
= acpi_processor_set_throttling(pr
, 0, false);
1214 pr
->flags
.throttling
= 0;
1219 #ifdef CONFIG_ACPI_PROCFS
1220 /* proc interface */
1221 static int acpi_processor_throttling_seq_show(struct seq_file
*seq
,
1224 struct acpi_processor
*pr
= seq
->private;
1231 if (!(pr
->throttling
.state_count
> 0)) {
1232 seq_puts(seq
, "<not supported>\n");
1236 result
= acpi_processor_get_throttling(pr
);
1240 "Could not determine current throttling state.\n");
1244 seq_printf(seq
, "state count: %d\n"
1245 "active state: T%d\n"
1246 "state available: T%d to T%d\n",
1247 pr
->throttling
.state_count
, pr
->throttling
.state
,
1248 pr
->throttling_platform_limit
,
1249 pr
->throttling
.state_count
- 1);
1251 seq_puts(seq
, "states:\n");
1252 if (pr
->throttling
.acpi_processor_get_throttling
==
1253 acpi_processor_get_throttling_fadt
) {
1254 for (i
= 0; i
< pr
->throttling
.state_count
; i
++)
1255 seq_printf(seq
, " %cT%d: %02d%%\n",
1256 (i
== pr
->throttling
.state
? '*' : ' '), i
,
1257 (pr
->throttling
.states
[i
].performance
? pr
->
1258 throttling
.states
[i
].performance
/ 10 : 0));
1260 for (i
= 0; i
< pr
->throttling
.state_count
; i
++)
1261 seq_printf(seq
, " %cT%d: %02d%%\n",
1262 (i
== pr
->throttling
.state
? '*' : ' '), i
,
1263 (int)pr
->throttling
.states_tss
[i
].
1271 static int acpi_processor_throttling_open_fs(struct inode
*inode
,
1274 return single_open(file
, acpi_processor_throttling_seq_show
,
1278 static ssize_t
acpi_processor_write_throttling(struct file
*file
,
1279 const char __user
* buffer
,
1280 size_t count
, loff_t
* data
)
1283 struct seq_file
*m
= file
->private_data
;
1284 struct acpi_processor
*pr
= m
->private;
1285 char state_string
[5] = "";
1287 size_t state_val
= 0;
1288 char tmpbuf
[5] = "";
1290 if (!pr
|| (count
> sizeof(state_string
) - 1))
1293 if (copy_from_user(state_string
, buffer
, count
))
1296 state_string
[count
] = '\0';
1297 if ((count
> 0) && (state_string
[count
-1] == '\n'))
1298 state_string
[count
-1] = '\0';
1300 charp
= state_string
;
1301 if ((state_string
[0] == 't') || (state_string
[0] == 'T'))
1304 state_val
= simple_strtoul(charp
, NULL
, 0);
1305 if (state_val
>= pr
->throttling
.state_count
)
1308 snprintf(tmpbuf
, 5, "%zu", state_val
);
1310 if (strcmp(tmpbuf
, charp
) != 0)
1313 result
= acpi_processor_set_throttling(pr
, state_val
, false);
1320 const struct file_operations acpi_processor_throttling_fops
= {
1321 .owner
= THIS_MODULE
,
1322 .open
= acpi_processor_throttling_open_fs
,
1324 .write
= acpi_processor_write_throttling
,
1325 .llseek
= seq_lseek
,
1326 .release
= single_release
,