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 #include <linux/proc_fs.h>
36 #include <linux/seq_file.h>
39 #include <asm/uaccess.h>
41 #include <acpi/acpi_bus.h>
42 #include <acpi/acpi_drivers.h>
43 #include <acpi/processor.h>
45 #define PREFIX "ACPI: "
47 #define ACPI_PROCESSOR_CLASS "processor"
48 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
49 ACPI_MODULE_NAME("processor_throttling");
52 * 0 -> acpi processor driver doesn't ignore _TPC values
53 * 1 -> acpi processor driver ignores _TPC values
55 static int ignore_tpc
;
56 module_param(ignore_tpc
, int, 0644);
57 MODULE_PARM_DESC(ignore_tpc
, "Disable broken BIOS _TPC throttling support");
59 struct throttling_tstate
{
60 unsigned int cpu
; /* cpu nr */
61 int target_state
; /* target T-state */
64 #define THROTTLING_PRECHANGE (1)
65 #define THROTTLING_POSTCHANGE (2)
67 static int acpi_processor_get_throttling(struct acpi_processor
*pr
);
68 int acpi_processor_set_throttling(struct acpi_processor
*pr
,
69 int state
, bool force
);
71 static int acpi_processor_update_tsd_coord(void)
73 int count
, count_target
;
76 cpumask_var_t covered_cpus
;
77 struct acpi_processor
*pr
, *match_pr
;
78 struct acpi_tsd_package
*pdomain
, *match_pdomain
;
79 struct acpi_processor_throttling
*pthrottling
, *match_pthrottling
;
81 if (!zalloc_cpumask_var(&covered_cpus
, GFP_KERNEL
))
85 * Now that we have _TSD data from all CPUs, lets setup T-state
86 * coordination between all CPUs.
88 for_each_possible_cpu(i
) {
89 pr
= per_cpu(processors
, i
);
93 /* Basic validity check for domain info */
94 pthrottling
= &(pr
->throttling
);
97 * If tsd package for one cpu is invalid, the coordination
98 * among all CPUs is thought as invalid.
101 if (!pthrottling
->tsd_valid_flag
) {
109 for_each_possible_cpu(i
) {
110 pr
= per_cpu(processors
, i
);
114 if (cpumask_test_cpu(i
, covered_cpus
))
116 pthrottling
= &pr
->throttling
;
118 pdomain
= &(pthrottling
->domain_info
);
119 cpumask_set_cpu(i
, pthrottling
->shared_cpu_map
);
120 cpumask_set_cpu(i
, covered_cpus
);
122 * If the number of processor in the TSD domain is 1, it is
123 * unnecessary to parse the coordination for this CPU.
125 if (pdomain
->num_processors
<= 1)
128 /* Validate the Domain info */
129 count_target
= pdomain
->num_processors
;
132 for_each_possible_cpu(j
) {
136 match_pr
= per_cpu(processors
, j
);
140 match_pthrottling
= &(match_pr
->throttling
);
141 match_pdomain
= &(match_pthrottling
->domain_info
);
142 if (match_pdomain
->domain
!= pdomain
->domain
)
145 /* Here i and j are in the same domain.
146 * If two TSD packages have the same domain, they
147 * should have the same num_porcessors and
148 * coordination type. Otherwise it will be regarded
151 if (match_pdomain
->num_processors
!= count_target
) {
156 if (pdomain
->coord_type
!= match_pdomain
->coord_type
) {
161 cpumask_set_cpu(j
, covered_cpus
);
162 cpumask_set_cpu(j
, pthrottling
->shared_cpu_map
);
165 for_each_possible_cpu(j
) {
169 match_pr
= per_cpu(processors
, j
);
173 match_pthrottling
= &(match_pr
->throttling
);
174 match_pdomain
= &(match_pthrottling
->domain_info
);
175 if (match_pdomain
->domain
!= pdomain
->domain
)
179 * If some CPUS have the same domain, they
180 * will have the same shared_cpu_map.
182 cpumask_copy(match_pthrottling
->shared_cpu_map
,
183 pthrottling
->shared_cpu_map
);
188 free_cpumask_var(covered_cpus
);
190 for_each_possible_cpu(i
) {
191 pr
= per_cpu(processors
, i
);
196 * Assume no coordination on any error parsing domain info.
197 * The coordination type will be forced as SW_ALL.
200 pthrottling
= &(pr
->throttling
);
201 cpumask_clear(pthrottling
->shared_cpu_map
);
202 cpumask_set_cpu(i
, pthrottling
->shared_cpu_map
);
203 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
211 * Update the T-state coordination after the _TSD
212 * data for all cpus is obtained.
214 void acpi_processor_throttling_init(void)
216 if (acpi_processor_update_tsd_coord())
217 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
218 "Assume no T-state coordination\n"));
223 static int acpi_processor_throttling_notifier(unsigned long event
, void *data
)
225 struct throttling_tstate
*p_tstate
= data
;
226 struct acpi_processor
*pr
;
229 struct acpi_processor_limit
*p_limit
;
230 struct acpi_processor_throttling
*p_throttling
;
233 pr
= per_cpu(processors
, cpu
);
235 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Invalid pr pointer\n"));
238 if (!pr
->flags
.throttling
) {
239 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Throttling control is "
240 "unsupported on CPU %d\n", cpu
));
243 target_state
= p_tstate
->target_state
;
244 p_throttling
= &(pr
->throttling
);
246 case THROTTLING_PRECHANGE
:
248 * Prechange event is used to choose one proper t-state,
249 * which meets the limits of thermal, user and _TPC.
251 p_limit
= &pr
->limit
;
252 if (p_limit
->thermal
.tx
> target_state
)
253 target_state
= p_limit
->thermal
.tx
;
254 if (p_limit
->user
.tx
> target_state
)
255 target_state
= p_limit
->user
.tx
;
256 if (pr
->throttling_platform_limit
> target_state
)
257 target_state
= pr
->throttling_platform_limit
;
258 if (target_state
>= p_throttling
->state_count
) {
260 "Exceed the limit of T-state \n");
261 target_state
= p_throttling
->state_count
- 1;
263 p_tstate
->target_state
= target_state
;
264 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "PreChange Event:"
265 "target T-state of CPU %d is T%d\n",
268 case THROTTLING_POSTCHANGE
:
270 * Postchange event is only used to update the
271 * T-state flag of acpi_processor_throttling.
273 p_throttling
->state
= target_state
;
274 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "PostChange Event:"
275 "CPU %d is switched to T%d\n",
280 "Unsupported Throttling notifier event\n");
288 * _TPC - Throttling Present Capabilities
290 static int acpi_processor_get_platform_limit(struct acpi_processor
*pr
)
292 acpi_status status
= 0;
293 unsigned long long tpc
= 0;
301 status
= acpi_evaluate_integer(pr
->handle
, "_TPC", NULL
, &tpc
);
302 if (ACPI_FAILURE(status
)) {
303 if (status
!= AE_NOT_FOUND
) {
304 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TPC"));
310 pr
->throttling_platform_limit
= (int)tpc
;
314 int acpi_processor_tstate_has_changed(struct acpi_processor
*pr
)
317 int throttling_limit
;
319 struct acpi_processor_limit
*limit
;
325 result
= acpi_processor_get_platform_limit(pr
);
327 /* Throttling Limit is unsupported */
331 throttling_limit
= pr
->throttling_platform_limit
;
332 if (throttling_limit
>= pr
->throttling
.state_count
) {
333 /* Uncorrect Throttling Limit */
337 current_state
= pr
->throttling
.state
;
338 if (current_state
> throttling_limit
) {
340 * The current state can meet the requirement of
341 * _TPC limit. But it is reasonable that OSPM changes
342 * t-states from high to low for better performance.
343 * Of course the limit condition of thermal
344 * and user should be considered.
347 target_state
= throttling_limit
;
348 if (limit
->thermal
.tx
> target_state
)
349 target_state
= limit
->thermal
.tx
;
350 if (limit
->user
.tx
> target_state
)
351 target_state
= limit
->user
.tx
;
352 } else if (current_state
== throttling_limit
) {
354 * Unnecessary to change the throttling state
359 * If the current state is lower than the limit of _TPC, it
360 * will be forced to switch to the throttling state defined
361 * by throttling_platfor_limit.
362 * Because the previous state meets with the limit condition
363 * of thermal and user, it is unnecessary to check it again.
365 target_state
= throttling_limit
;
367 return acpi_processor_set_throttling(pr
, target_state
, false);
371 * _PTC - Processor Throttling Control (and status) register location
373 static int acpi_processor_get_throttling_control(struct acpi_processor
*pr
)
376 acpi_status status
= 0;
377 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
378 union acpi_object
*ptc
= NULL
;
379 union acpi_object obj
= { 0 };
380 struct acpi_processor_throttling
*throttling
;
382 status
= acpi_evaluate_object(pr
->handle
, "_PTC", NULL
, &buffer
);
383 if (ACPI_FAILURE(status
)) {
384 if (status
!= AE_NOT_FOUND
) {
385 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PTC"));
390 ptc
= (union acpi_object
*)buffer
.pointer
;
391 if (!ptc
|| (ptc
->type
!= ACPI_TYPE_PACKAGE
)
392 || (ptc
->package
.count
!= 2)) {
393 printk(KERN_ERR PREFIX
"Invalid _PTC data\n");
402 obj
= ptc
->package
.elements
[0];
404 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
405 || (obj
.buffer
.length
< sizeof(struct acpi_ptc_register
))
406 || (obj
.buffer
.pointer
== NULL
)) {
407 printk(KERN_ERR PREFIX
408 "Invalid _PTC data (control_register)\n");
412 memcpy(&pr
->throttling
.control_register
, obj
.buffer
.pointer
,
413 sizeof(struct acpi_ptc_register
));
419 obj
= ptc
->package
.elements
[1];
421 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
422 || (obj
.buffer
.length
< sizeof(struct acpi_ptc_register
))
423 || (obj
.buffer
.pointer
== NULL
)) {
424 printk(KERN_ERR PREFIX
"Invalid _PTC data (status_register)\n");
429 memcpy(&pr
->throttling
.status_register
, obj
.buffer
.pointer
,
430 sizeof(struct acpi_ptc_register
));
432 throttling
= &pr
->throttling
;
434 if ((throttling
->control_register
.bit_width
+
435 throttling
->control_register
.bit_offset
) > 32) {
436 printk(KERN_ERR PREFIX
"Invalid _PTC control register\n");
441 if ((throttling
->status_register
.bit_width
+
442 throttling
->status_register
.bit_offset
) > 32) {
443 printk(KERN_ERR PREFIX
"Invalid _PTC status register\n");
449 kfree(buffer
.pointer
);
455 * _TSS - Throttling Supported States
457 static int acpi_processor_get_throttling_states(struct acpi_processor
*pr
)
460 acpi_status status
= AE_OK
;
461 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
462 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
463 struct acpi_buffer state
= { 0, NULL
};
464 union acpi_object
*tss
= NULL
;
467 status
= acpi_evaluate_object(pr
->handle
, "_TSS", NULL
, &buffer
);
468 if (ACPI_FAILURE(status
)) {
469 if (status
!= AE_NOT_FOUND
) {
470 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TSS"));
475 tss
= buffer
.pointer
;
476 if (!tss
|| (tss
->type
!= ACPI_TYPE_PACKAGE
)) {
477 printk(KERN_ERR PREFIX
"Invalid _TSS data\n");
482 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d throttling states\n",
483 tss
->package
.count
));
485 pr
->throttling
.state_count
= tss
->package
.count
;
486 pr
->throttling
.states_tss
=
487 kmalloc(sizeof(struct acpi_processor_tx_tss
) * tss
->package
.count
,
489 if (!pr
->throttling
.states_tss
) {
494 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
496 struct acpi_processor_tx_tss
*tx
=
497 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
500 state
.length
= sizeof(struct acpi_processor_tx_tss
);
503 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Extracting state %d\n", i
));
505 status
= acpi_extract_package(&(tss
->package
.elements
[i
]),
507 if (ACPI_FAILURE(status
)) {
508 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _TSS data"));
510 kfree(pr
->throttling
.states_tss
);
514 if (!tx
->freqpercentage
) {
515 printk(KERN_ERR PREFIX
516 "Invalid _TSS data: freq is zero\n");
518 kfree(pr
->throttling
.states_tss
);
524 kfree(buffer
.pointer
);
530 * _TSD - T-State Dependencies
532 static int acpi_processor_get_tsd(struct acpi_processor
*pr
)
535 acpi_status status
= AE_OK
;
536 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
537 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
538 struct acpi_buffer state
= { 0, NULL
};
539 union acpi_object
*tsd
= NULL
;
540 struct acpi_tsd_package
*pdomain
;
541 struct acpi_processor_throttling
*pthrottling
;
543 pthrottling
= &pr
->throttling
;
544 pthrottling
->tsd_valid_flag
= 0;
546 status
= acpi_evaluate_object(pr
->handle
, "_TSD", NULL
, &buffer
);
547 if (ACPI_FAILURE(status
)) {
548 if (status
!= AE_NOT_FOUND
) {
549 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TSD"));
554 tsd
= buffer
.pointer
;
555 if (!tsd
|| (tsd
->type
!= ACPI_TYPE_PACKAGE
)) {
556 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
561 if (tsd
->package
.count
!= 1) {
562 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
567 pdomain
= &(pr
->throttling
.domain_info
);
569 state
.length
= sizeof(struct acpi_tsd_package
);
570 state
.pointer
= pdomain
;
572 status
= acpi_extract_package(&(tsd
->package
.elements
[0]),
574 if (ACPI_FAILURE(status
)) {
575 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
580 if (pdomain
->num_entries
!= ACPI_TSD_REV0_ENTRIES
) {
581 printk(KERN_ERR PREFIX
"Unknown _TSD:num_entries\n");
586 if (pdomain
->revision
!= ACPI_TSD_REV0_REVISION
) {
587 printk(KERN_ERR PREFIX
"Unknown _TSD:revision\n");
592 pthrottling
= &pr
->throttling
;
593 pthrottling
->tsd_valid_flag
= 1;
594 pthrottling
->shared_type
= pdomain
->coord_type
;
595 cpumask_set_cpu(pr
->id
, pthrottling
->shared_cpu_map
);
597 * If the coordination type is not defined in ACPI spec,
598 * the tsd_valid_flag will be clear and coordination type
599 * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
601 if (pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ALL
&&
602 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ANY
&&
603 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_HW_ALL
) {
604 pthrottling
->tsd_valid_flag
= 0;
605 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
609 kfree(buffer
.pointer
);
613 /* --------------------------------------------------------------------------
615 -------------------------------------------------------------------------- */
616 static int acpi_processor_get_throttling_fadt(struct acpi_processor
*pr
)
626 if (!pr
->flags
.throttling
)
629 pr
->throttling
.state
= 0;
631 duty_mask
= pr
->throttling
.state_count
- 1;
633 duty_mask
<<= pr
->throttling
.duty_offset
;
637 value
= inl(pr
->throttling
.address
);
640 * Compute the current throttling state when throttling is enabled
644 duty_value
= value
& duty_mask
;
645 duty_value
>>= pr
->throttling
.duty_offset
;
648 state
= pr
->throttling
.state_count
- duty_value
;
651 pr
->throttling
.state
= state
;
655 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
656 "Throttling state is T%d (%d%% throttling applied)\n",
657 state
, pr
->throttling
.states
[state
].performance
));
663 static int acpi_throttling_rdmsr(struct acpi_processor
*pr
,
666 struct cpuinfo_x86
*c
;
667 u64 msr_high
, msr_low
;
675 if ((c
->x86_vendor
!= X86_VENDOR_INTEL
) ||
676 !cpu_has(c
, X86_FEATURE_ACPI
)) {
677 printk(KERN_ERR PREFIX
678 "HARDWARE addr space,NOT supported yet\n");
682 rdmsr_safe(MSR_IA32_THERM_CONTROL
,
683 (u32
*)&msr_low
, (u32
*) &msr_high
);
684 msr
= (msr_high
<< 32) | msr_low
;
691 static int acpi_throttling_wrmsr(struct acpi_processor
*pr
, u64 value
)
693 struct cpuinfo_x86
*c
;
701 if ((c
->x86_vendor
!= X86_VENDOR_INTEL
) ||
702 !cpu_has(c
, X86_FEATURE_ACPI
)) {
703 printk(KERN_ERR PREFIX
704 "HARDWARE addr space,NOT supported yet\n");
707 wrmsr_safe(MSR_IA32_THERM_CONTROL
,
708 msr
& 0xffffffff, msr
>> 32);
714 static int acpi_throttling_rdmsr(struct acpi_processor
*pr
,
717 printk(KERN_ERR PREFIX
718 "HARDWARE addr space,NOT supported yet\n");
722 static int acpi_throttling_wrmsr(struct acpi_processor
*pr
, u64 value
)
724 printk(KERN_ERR PREFIX
725 "HARDWARE addr space,NOT supported yet\n");
730 static int acpi_read_throttling_status(struct acpi_processor
*pr
,
733 u32 bit_width
, bit_offset
;
736 struct acpi_processor_throttling
*throttling
;
739 throttling
= &pr
->throttling
;
740 switch (throttling
->status_register
.space_id
) {
741 case ACPI_ADR_SPACE_SYSTEM_IO
:
743 bit_width
= throttling
->status_register
.bit_width
;
744 bit_offset
= throttling
->status_register
.bit_offset
;
746 acpi_os_read_port((acpi_io_address
) throttling
->status_register
.
747 address
, (u32
*) &ptc_value
,
748 (u32
) (bit_width
+ bit_offset
));
749 ptc_mask
= (1 << bit_width
) - 1;
750 *value
= (u64
) ((ptc_value
>> bit_offset
) & ptc_mask
);
753 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
754 ret
= acpi_throttling_rdmsr(pr
, value
);
757 printk(KERN_ERR PREFIX
"Unknown addr space %d\n",
758 (u32
) (throttling
->status_register
.space_id
));
763 static int acpi_write_throttling_state(struct acpi_processor
*pr
,
766 u32 bit_width
, bit_offset
;
769 struct acpi_processor_throttling
*throttling
;
772 throttling
= &pr
->throttling
;
773 switch (throttling
->control_register
.space_id
) {
774 case ACPI_ADR_SPACE_SYSTEM_IO
:
775 bit_width
= throttling
->control_register
.bit_width
;
776 bit_offset
= throttling
->control_register
.bit_offset
;
777 ptc_mask
= (1 << bit_width
) - 1;
778 ptc_value
= value
& ptc_mask
;
780 acpi_os_write_port((acpi_io_address
) throttling
->
781 control_register
.address
,
782 (u32
) (ptc_value
<< bit_offset
),
783 (u32
) (bit_width
+ bit_offset
));
786 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
787 ret
= acpi_throttling_wrmsr(pr
, value
);
790 printk(KERN_ERR PREFIX
"Unknown addr space %d\n",
791 (u32
) (throttling
->control_register
.space_id
));
796 static int acpi_get_throttling_state(struct acpi_processor
*pr
,
801 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
802 struct acpi_processor_tx_tss
*tx
=
803 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
805 if (tx
->control
== value
)
811 static int acpi_get_throttling_value(struct acpi_processor
*pr
,
812 int state
, u64
*value
)
816 if (state
>= 0 && state
<= pr
->throttling
.state_count
) {
817 struct acpi_processor_tx_tss
*tx
=
818 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
820 *value
= tx
->control
;
826 static int acpi_processor_get_throttling_ptc(struct acpi_processor
*pr
)
835 if (!pr
->flags
.throttling
)
838 pr
->throttling
.state
= 0;
841 ret
= acpi_read_throttling_status(pr
, &value
);
843 state
= acpi_get_throttling_state(pr
, value
);
845 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
846 "Invalid throttling state, reset\n"));
848 ret
= acpi_processor_set_throttling(pr
, state
, true);
852 pr
->throttling
.state
= state
;
858 static int acpi_processor_get_throttling(struct acpi_processor
*pr
)
860 cpumask_var_t saved_mask
;
866 if (!pr
->flags
.throttling
)
869 if (!alloc_cpumask_var(&saved_mask
, GFP_KERNEL
))
873 * Migrate task to the cpu pointed by pr.
875 cpumask_copy(saved_mask
, ¤t
->cpus_allowed
);
876 /* FIXME: use work_on_cpu() */
877 set_cpus_allowed_ptr(current
, cpumask_of(pr
->id
));
878 ret
= pr
->throttling
.acpi_processor_get_throttling(pr
);
879 /* restore the previous state */
880 set_cpus_allowed_ptr(current
, saved_mask
);
881 free_cpumask_var(saved_mask
);
886 static int acpi_processor_get_fadt_info(struct acpi_processor
*pr
)
890 if (!pr
->throttling
.address
) {
891 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No throttling register\n"));
893 } else if (!pr
->throttling
.duty_width
) {
894 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No throttling states\n"));
897 /* TBD: Support duty_cycle values that span bit 4. */
898 else if ((pr
->throttling
.duty_offset
+ pr
->throttling
.duty_width
) > 4) {
899 printk(KERN_WARNING PREFIX
"duty_cycle spans bit 4\n");
903 pr
->throttling
.state_count
= 1 << acpi_gbl_FADT
.duty_width
;
906 * Compute state values. Note that throttling displays a linear power
907 * performance relationship (at 50% performance the CPU will consume
908 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
911 step
= (1000 / pr
->throttling
.state_count
);
913 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
914 pr
->throttling
.states
[i
].performance
= 1000 - step
* i
;
915 pr
->throttling
.states
[i
].power
= 1000 - step
* i
;
920 static int acpi_processor_set_throttling_fadt(struct acpi_processor
*pr
,
921 int state
, bool force
)
930 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
933 if (!pr
->flags
.throttling
)
936 if (!force
&& (state
== pr
->throttling
.state
))
939 if (state
< pr
->throttling_platform_limit
)
942 * Calculate the duty_value and duty_mask.
945 duty_value
= pr
->throttling
.state_count
- state
;
947 duty_value
<<= pr
->throttling
.duty_offset
;
949 /* Used to clear all duty_value bits */
950 duty_mask
= pr
->throttling
.state_count
- 1;
952 duty_mask
<<= acpi_gbl_FADT
.duty_offset
;
953 duty_mask
= ~duty_mask
;
959 * Disable throttling by writing a 0 to bit 4. Note that we must
960 * turn it off before you can change the duty_value.
962 value
= inl(pr
->throttling
.address
);
965 outl(value
, pr
->throttling
.address
);
969 * Write the new duty_value and then enable throttling. Note
970 * that a state value of 0 leaves throttling disabled.
975 outl(value
, pr
->throttling
.address
);
978 outl(value
, pr
->throttling
.address
);
981 pr
->throttling
.state
= state
;
985 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
986 "Throttling state set to T%d (%d%%)\n", state
,
987 (pr
->throttling
.states
[state
].performance
? pr
->
988 throttling
.states
[state
].performance
/ 10 : 0)));
993 static int acpi_processor_set_throttling_ptc(struct acpi_processor
*pr
,
994 int state
, bool force
)
1002 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
1005 if (!pr
->flags
.throttling
)
1008 if (!force
&& (state
== pr
->throttling
.state
))
1011 if (state
< pr
->throttling_platform_limit
)
1015 ret
= acpi_get_throttling_value(pr
, state
, &value
);
1017 acpi_write_throttling_state(pr
, value
);
1018 pr
->throttling
.state
= state
;
1024 int acpi_processor_set_throttling(struct acpi_processor
*pr
,
1025 int state
, bool force
)
1027 cpumask_var_t saved_mask
;
1030 struct acpi_processor
*match_pr
;
1031 struct acpi_processor_throttling
*p_throttling
;
1032 struct throttling_tstate t_state
;
1033 cpumask_var_t online_throttling_cpus
;
1038 if (!pr
->flags
.throttling
)
1041 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
1044 if (!alloc_cpumask_var(&saved_mask
, GFP_KERNEL
))
1047 if (!alloc_cpumask_var(&online_throttling_cpus
, GFP_KERNEL
)) {
1048 free_cpumask_var(saved_mask
);
1052 cpumask_copy(saved_mask
, ¤t
->cpus_allowed
);
1053 t_state
.target_state
= state
;
1054 p_throttling
= &(pr
->throttling
);
1055 cpumask_and(online_throttling_cpus
, cpu_online_mask
,
1056 p_throttling
->shared_cpu_map
);
1058 * The throttling notifier will be called for every
1059 * affected cpu in order to get one proper T-state.
1060 * The notifier event is THROTTLING_PRECHANGE.
1062 for_each_cpu(i
, online_throttling_cpus
) {
1064 acpi_processor_throttling_notifier(THROTTLING_PRECHANGE
,
1068 * The function of acpi_processor_set_throttling will be called
1069 * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
1070 * it is necessary to call it for every affected cpu. Otherwise
1071 * it can be called only for the cpu pointed by pr.
1073 if (p_throttling
->shared_type
== DOMAIN_COORD_TYPE_SW_ANY
) {
1074 /* FIXME: use work_on_cpu() */
1075 set_cpus_allowed_ptr(current
, cpumask_of(pr
->id
));
1076 ret
= p_throttling
->acpi_processor_set_throttling(pr
,
1077 t_state
.target_state
, force
);
1080 * When the T-state coordination is SW_ALL or HW_ALL,
1081 * it is necessary to set T-state for every affected
1084 for_each_cpu(i
, online_throttling_cpus
) {
1085 match_pr
= per_cpu(processors
, i
);
1087 * If the pointer is invalid, we will report the
1088 * error message and continue.
1091 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1092 "Invalid Pointer for CPU %d\n", i
));
1096 * If the throttling control is unsupported on CPU i,
1097 * we will report the error message and continue.
1099 if (!match_pr
->flags
.throttling
) {
1100 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1101 "Throttling Controll is unsupported "
1106 /* FIXME: use work_on_cpu() */
1107 set_cpus_allowed_ptr(current
, cpumask_of(i
));
1108 ret
= match_pr
->throttling
.
1109 acpi_processor_set_throttling(
1110 match_pr
, t_state
.target_state
, force
);
1114 * After the set_throttling is called, the
1115 * throttling notifier is called for every
1116 * affected cpu to update the T-states.
1117 * The notifier event is THROTTLING_POSTCHANGE
1119 for_each_cpu(i
, online_throttling_cpus
) {
1121 acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE
,
1124 /* restore the previous state */
1125 /* FIXME: use work_on_cpu() */
1126 set_cpus_allowed_ptr(current
, saved_mask
);
1127 free_cpumask_var(online_throttling_cpus
);
1128 free_cpumask_var(saved_mask
);
1132 int acpi_processor_get_throttling_info(struct acpi_processor
*pr
)
1135 struct acpi_processor_throttling
*pthrottling
;
1137 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1138 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1139 pr
->throttling
.address
,
1140 pr
->throttling
.duty_offset
,
1141 pr
->throttling
.duty_width
));
1144 * Evaluate _PTC, _TSS and _TPC
1145 * They must all be present or none of them can be used.
1147 if (acpi_processor_get_throttling_control(pr
) ||
1148 acpi_processor_get_throttling_states(pr
) ||
1149 acpi_processor_get_platform_limit(pr
))
1151 pr
->throttling
.acpi_processor_get_throttling
=
1152 &acpi_processor_get_throttling_fadt
;
1153 pr
->throttling
.acpi_processor_set_throttling
=
1154 &acpi_processor_set_throttling_fadt
;
1155 if (acpi_processor_get_fadt_info(pr
))
1158 pr
->throttling
.acpi_processor_get_throttling
=
1159 &acpi_processor_get_throttling_ptc
;
1160 pr
->throttling
.acpi_processor_set_throttling
=
1161 &acpi_processor_set_throttling_ptc
;
1165 * If TSD package for one CPU can't be parsed successfully, it means
1166 * that this CPU will have no coordination with other CPUs.
1168 if (acpi_processor_get_tsd(pr
)) {
1169 pthrottling
= &pr
->throttling
;
1170 pthrottling
->tsd_valid_flag
= 0;
1171 cpumask_set_cpu(pr
->id
, pthrottling
->shared_cpu_map
);
1172 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
1176 * PIIX4 Errata: We don't support throttling on the original PIIX4.
1177 * This shouldn't be an issue as few (if any) mobile systems ever
1180 if (errata
.piix4
.throttle
) {
1181 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1182 "Throttling not supported on PIIX4 A- or B-step\n"));
1186 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d throttling states\n",
1187 pr
->throttling
.state_count
));
1189 pr
->flags
.throttling
= 1;
1192 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
1193 * thermal) decide to lower performance if it so chooses, but for now
1194 * we'll crank up the speed.
1197 result
= acpi_processor_get_throttling(pr
);
1201 if (pr
->throttling
.state
) {
1202 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1203 "Disabling throttling (was T%d)\n",
1204 pr
->throttling
.state
));
1205 result
= acpi_processor_set_throttling(pr
, 0, false);
1212 pr
->flags
.throttling
= 0;
1217 /* proc interface */
1218 #ifdef CONFIG_ACPI_PROCFS
1219 static int acpi_processor_throttling_seq_show(struct seq_file
*seq
,
1222 struct acpi_processor
*pr
= seq
->private;
1229 if (!(pr
->throttling
.state_count
> 0)) {
1230 seq_puts(seq
, "<not supported>\n");
1234 result
= acpi_processor_get_throttling(pr
);
1238 "Could not determine current throttling state.\n");
1242 seq_printf(seq
, "state count: %d\n"
1243 "active state: T%d\n"
1244 "state available: T%d to T%d\n",
1245 pr
->throttling
.state_count
, pr
->throttling
.state
,
1246 pr
->throttling_platform_limit
,
1247 pr
->throttling
.state_count
- 1);
1249 seq_puts(seq
, "states:\n");
1250 if (pr
->throttling
.acpi_processor_get_throttling
==
1251 acpi_processor_get_throttling_fadt
) {
1252 for (i
= 0; i
< pr
->throttling
.state_count
; i
++)
1253 seq_printf(seq
, " %cT%d: %02d%%\n",
1254 (i
== pr
->throttling
.state
? '*' : ' '), i
,
1255 (pr
->throttling
.states
[i
].performance
? pr
->
1256 throttling
.states
[i
].performance
/ 10 : 0));
1258 for (i
= 0; i
< pr
->throttling
.state_count
; i
++)
1259 seq_printf(seq
, " %cT%d: %02d%%\n",
1260 (i
== pr
->throttling
.state
? '*' : ' '), i
,
1261 (int)pr
->throttling
.states_tss
[i
].
1269 static int acpi_processor_throttling_open_fs(struct inode
*inode
,
1272 return single_open(file
, acpi_processor_throttling_seq_show
,
1276 static ssize_t
acpi_processor_write_throttling(struct file
*file
,
1277 const char __user
* buffer
,
1278 size_t count
, loff_t
* data
)
1281 struct seq_file
*m
= file
->private_data
;
1282 struct acpi_processor
*pr
= m
->private;
1283 char state_string
[5] = "";
1285 size_t state_val
= 0;
1286 char tmpbuf
[5] = "";
1288 if (!pr
|| (count
> sizeof(state_string
) - 1))
1291 if (copy_from_user(state_string
, buffer
, count
))
1294 state_string
[count
] = '\0';
1295 if ((count
> 0) && (state_string
[count
-1] == '\n'))
1296 state_string
[count
-1] = '\0';
1298 charp
= state_string
;
1299 if ((state_string
[0] == 't') || (state_string
[0] == 'T'))
1302 state_val
= simple_strtoul(charp
, NULL
, 0);
1303 if (state_val
>= pr
->throttling
.state_count
)
1306 snprintf(tmpbuf
, 5, "%zu", state_val
);
1308 if (strcmp(tmpbuf
, charp
) != 0)
1311 result
= acpi_processor_set_throttling(pr
, state_val
, false);
1318 const struct file_operations acpi_processor_throttling_fops
= {
1319 .owner
= THIS_MODULE
,
1320 .open
= acpi_processor_throttling_open_fs
,
1322 .write
= acpi_processor_write_throttling
,
1323 .llseek
= seq_lseek
,
1324 .release
= single_release
,