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/acpi_drivers.h>
42 #include <acpi/processor.h>
44 #define ACPI_PROCESSOR_CLASS "processor"
45 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
46 ACPI_MODULE_NAME("processor_throttling");
49 * 0 -> acpi processor driver doesn't ignore _TPC values
50 * 1 -> acpi processor driver ignores _TPC values
52 static int ignore_tpc
;
53 module_param(ignore_tpc
, int, 0644);
54 MODULE_PARM_DESC(ignore_tpc
, "Disable broken BIOS _TPC throttling support");
56 struct throttling_tstate
{
57 unsigned int cpu
; /* cpu nr */
58 int target_state
; /* target T-state */
61 #define THROTTLING_PRECHANGE (1)
62 #define THROTTLING_POSTCHANGE (2)
64 static int acpi_processor_get_throttling(struct acpi_processor
*pr
);
65 int acpi_processor_set_throttling(struct acpi_processor
*pr
,
66 int state
, bool force
);
68 static int acpi_processor_update_tsd_coord(void)
70 int count
, count_target
;
73 cpumask_var_t covered_cpus
;
74 struct acpi_processor
*pr
, *match_pr
;
75 struct acpi_tsd_package
*pdomain
, *match_pdomain
;
76 struct acpi_processor_throttling
*pthrottling
, *match_pthrottling
;
78 if (!alloc_cpumask_var(&covered_cpus
, GFP_KERNEL
))
82 * Now that we have _TSD data from all CPUs, lets setup T-state
83 * coordination between all CPUs.
85 for_each_possible_cpu(i
) {
86 pr
= per_cpu(processors
, i
);
90 /* Basic validity check for domain info */
91 pthrottling
= &(pr
->throttling
);
94 * If tsd package for one cpu is invalid, the coordination
95 * among all CPUs is thought as invalid.
98 if (!pthrottling
->tsd_valid_flag
) {
106 cpumask_clear(covered_cpus
);
107 for_each_possible_cpu(i
) {
108 pr
= per_cpu(processors
, i
);
112 if (cpumask_test_cpu(i
, covered_cpus
))
114 pthrottling
= &pr
->throttling
;
116 pdomain
= &(pthrottling
->domain_info
);
117 cpumask_set_cpu(i
, pthrottling
->shared_cpu_map
);
118 cpumask_set_cpu(i
, covered_cpus
);
120 * If the number of processor in the TSD domain is 1, it is
121 * unnecessary to parse the coordination for this CPU.
123 if (pdomain
->num_processors
<= 1)
126 /* Validate the Domain info */
127 count_target
= pdomain
->num_processors
;
130 for_each_possible_cpu(j
) {
134 match_pr
= per_cpu(processors
, j
);
138 match_pthrottling
= &(match_pr
->throttling
);
139 match_pdomain
= &(match_pthrottling
->domain_info
);
140 if (match_pdomain
->domain
!= pdomain
->domain
)
143 /* Here i and j are in the same domain.
144 * If two TSD packages have the same domain, they
145 * should have the same num_porcessors and
146 * coordination type. Otherwise it will be regarded
149 if (match_pdomain
->num_processors
!= count_target
) {
154 if (pdomain
->coord_type
!= match_pdomain
->coord_type
) {
159 cpumask_set_cpu(j
, covered_cpus
);
160 cpumask_set_cpu(j
, pthrottling
->shared_cpu_map
);
163 for_each_possible_cpu(j
) {
167 match_pr
= per_cpu(processors
, j
);
171 match_pthrottling
= &(match_pr
->throttling
);
172 match_pdomain
= &(match_pthrottling
->domain_info
);
173 if (match_pdomain
->domain
!= pdomain
->domain
)
177 * If some CPUS have the same domain, they
178 * will have the same shared_cpu_map.
180 cpumask_copy(match_pthrottling
->shared_cpu_map
,
181 pthrottling
->shared_cpu_map
);
186 free_cpumask_var(covered_cpus
);
188 for_each_possible_cpu(i
) {
189 pr
= per_cpu(processors
, i
);
194 * Assume no coordination on any error parsing domain info.
195 * The coordination type will be forced as SW_ALL.
198 pthrottling
= &(pr
->throttling
);
199 cpumask_clear(pthrottling
->shared_cpu_map
);
200 cpumask_set_cpu(i
, pthrottling
->shared_cpu_map
);
201 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
209 * Update the T-state coordination after the _TSD
210 * data for all cpus is obtained.
212 void acpi_processor_throttling_init(void)
214 if (acpi_processor_update_tsd_coord())
215 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
216 "Assume no T-state coordination\n"));
221 static int acpi_processor_throttling_notifier(unsigned long event
, void *data
)
223 struct throttling_tstate
*p_tstate
= data
;
224 struct acpi_processor
*pr
;
227 struct acpi_processor_limit
*p_limit
;
228 struct acpi_processor_throttling
*p_throttling
;
231 pr
= per_cpu(processors
, cpu
);
233 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Invalid pr pointer\n"));
236 if (!pr
->flags
.throttling
) {
237 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Throttling control is "
238 "unsupported on CPU %d\n", cpu
));
241 target_state
= p_tstate
->target_state
;
242 p_throttling
= &(pr
->throttling
);
244 case THROTTLING_PRECHANGE
:
246 * Prechange event is used to choose one proper t-state,
247 * which meets the limits of thermal, user and _TPC.
249 p_limit
= &pr
->limit
;
250 if (p_limit
->thermal
.tx
> target_state
)
251 target_state
= p_limit
->thermal
.tx
;
252 if (p_limit
->user
.tx
> target_state
)
253 target_state
= p_limit
->user
.tx
;
254 if (pr
->throttling_platform_limit
> target_state
)
255 target_state
= pr
->throttling_platform_limit
;
256 if (target_state
>= p_throttling
->state_count
) {
258 "Exceed the limit of T-state \n");
259 target_state
= p_throttling
->state_count
- 1;
261 p_tstate
->target_state
= target_state
;
262 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "PreChange Event:"
263 "target T-state of CPU %d is T%d\n",
266 case THROTTLING_POSTCHANGE
:
268 * Postchange event is only used to update the
269 * T-state flag of acpi_processor_throttling.
271 p_throttling
->state
= target_state
;
272 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "PostChange Event:"
273 "CPU %d is switched to T%d\n",
278 "Unsupported Throttling notifier event\n");
286 * _TPC - Throttling Present Capabilities
288 static int acpi_processor_get_platform_limit(struct acpi_processor
*pr
)
290 acpi_status status
= 0;
291 unsigned long long tpc
= 0;
299 status
= acpi_evaluate_integer(pr
->handle
, "_TPC", NULL
, &tpc
);
300 if (ACPI_FAILURE(status
)) {
301 if (status
!= AE_NOT_FOUND
) {
302 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TPC"));
308 pr
->throttling_platform_limit
= (int)tpc
;
312 int acpi_processor_tstate_has_changed(struct acpi_processor
*pr
)
315 int throttling_limit
;
317 struct acpi_processor_limit
*limit
;
323 result
= acpi_processor_get_platform_limit(pr
);
325 /* Throttling Limit is unsupported */
329 throttling_limit
= pr
->throttling_platform_limit
;
330 if (throttling_limit
>= pr
->throttling
.state_count
) {
331 /* Uncorrect Throttling Limit */
335 current_state
= pr
->throttling
.state
;
336 if (current_state
> throttling_limit
) {
338 * The current state can meet the requirement of
339 * _TPC limit. But it is reasonable that OSPM changes
340 * t-states from high to low for better performance.
341 * Of course the limit condition of thermal
342 * and user should be considered.
345 target_state
= throttling_limit
;
346 if (limit
->thermal
.tx
> target_state
)
347 target_state
= limit
->thermal
.tx
;
348 if (limit
->user
.tx
> target_state
)
349 target_state
= limit
->user
.tx
;
350 } else if (current_state
== throttling_limit
) {
352 * Unnecessary to change the throttling state
357 * If the current state is lower than the limit of _TPC, it
358 * will be forced to switch to the throttling state defined
359 * by throttling_platfor_limit.
360 * Because the previous state meets with the limit condition
361 * of thermal and user, it is unnecessary to check it again.
363 target_state
= throttling_limit
;
365 return acpi_processor_set_throttling(pr
, target_state
, false);
369 * _PTC - Processor Throttling Control (and status) register location
371 static int acpi_processor_get_throttling_control(struct acpi_processor
*pr
)
374 acpi_status status
= 0;
375 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
376 union acpi_object
*ptc
= NULL
;
377 union acpi_object obj
= { 0 };
378 struct acpi_processor_throttling
*throttling
;
380 status
= acpi_evaluate_object(pr
->handle
, "_PTC", NULL
, &buffer
);
381 if (ACPI_FAILURE(status
)) {
382 if (status
!= AE_NOT_FOUND
) {
383 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PTC"));
388 ptc
= (union acpi_object
*)buffer
.pointer
;
389 if (!ptc
|| (ptc
->type
!= ACPI_TYPE_PACKAGE
)
390 || (ptc
->package
.count
!= 2)) {
391 printk(KERN_ERR PREFIX
"Invalid _PTC data\n");
400 obj
= ptc
->package
.elements
[0];
402 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
403 || (obj
.buffer
.length
< sizeof(struct acpi_ptc_register
))
404 || (obj
.buffer
.pointer
== NULL
)) {
405 printk(KERN_ERR PREFIX
406 "Invalid _PTC data (control_register)\n");
410 memcpy(&pr
->throttling
.control_register
, obj
.buffer
.pointer
,
411 sizeof(struct acpi_ptc_register
));
417 obj
= ptc
->package
.elements
[1];
419 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
420 || (obj
.buffer
.length
< sizeof(struct acpi_ptc_register
))
421 || (obj
.buffer
.pointer
== NULL
)) {
422 printk(KERN_ERR PREFIX
"Invalid _PTC data (status_register)\n");
427 memcpy(&pr
->throttling
.status_register
, obj
.buffer
.pointer
,
428 sizeof(struct acpi_ptc_register
));
430 throttling
= &pr
->throttling
;
432 if ((throttling
->control_register
.bit_width
+
433 throttling
->control_register
.bit_offset
) > 32) {
434 printk(KERN_ERR PREFIX
"Invalid _PTC control register\n");
439 if ((throttling
->status_register
.bit_width
+
440 throttling
->status_register
.bit_offset
) > 32) {
441 printk(KERN_ERR PREFIX
"Invalid _PTC status register\n");
447 kfree(buffer
.pointer
);
453 * _TSS - Throttling Supported States
455 static int acpi_processor_get_throttling_states(struct acpi_processor
*pr
)
458 acpi_status status
= AE_OK
;
459 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
460 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
461 struct acpi_buffer state
= { 0, NULL
};
462 union acpi_object
*tss
= NULL
;
465 status
= acpi_evaluate_object(pr
->handle
, "_TSS", NULL
, &buffer
);
466 if (ACPI_FAILURE(status
)) {
467 if (status
!= AE_NOT_FOUND
) {
468 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TSS"));
473 tss
= buffer
.pointer
;
474 if (!tss
|| (tss
->type
!= ACPI_TYPE_PACKAGE
)) {
475 printk(KERN_ERR PREFIX
"Invalid _TSS data\n");
480 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d throttling states\n",
481 tss
->package
.count
));
483 pr
->throttling
.state_count
= tss
->package
.count
;
484 pr
->throttling
.states_tss
=
485 kmalloc(sizeof(struct acpi_processor_tx_tss
) * tss
->package
.count
,
487 if (!pr
->throttling
.states_tss
) {
492 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
494 struct acpi_processor_tx_tss
*tx
=
495 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
498 state
.length
= sizeof(struct acpi_processor_tx_tss
);
501 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Extracting state %d\n", i
));
503 status
= acpi_extract_package(&(tss
->package
.elements
[i
]),
505 if (ACPI_FAILURE(status
)) {
506 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _TSS data"));
508 kfree(pr
->throttling
.states_tss
);
512 if (!tx
->freqpercentage
) {
513 printk(KERN_ERR PREFIX
514 "Invalid _TSS data: freq is zero\n");
516 kfree(pr
->throttling
.states_tss
);
522 kfree(buffer
.pointer
);
528 * _TSD - T-State Dependencies
530 static int acpi_processor_get_tsd(struct acpi_processor
*pr
)
533 acpi_status status
= AE_OK
;
534 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
535 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
536 struct acpi_buffer state
= { 0, NULL
};
537 union acpi_object
*tsd
= NULL
;
538 struct acpi_tsd_package
*pdomain
;
539 struct acpi_processor_throttling
*pthrottling
;
541 pthrottling
= &pr
->throttling
;
542 pthrottling
->tsd_valid_flag
= 0;
544 status
= acpi_evaluate_object(pr
->handle
, "_TSD", NULL
, &buffer
);
545 if (ACPI_FAILURE(status
)) {
546 if (status
!= AE_NOT_FOUND
) {
547 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TSD"));
552 tsd
= buffer
.pointer
;
553 if (!tsd
|| (tsd
->type
!= ACPI_TYPE_PACKAGE
)) {
554 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
559 if (tsd
->package
.count
!= 1) {
560 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
565 pdomain
= &(pr
->throttling
.domain_info
);
567 state
.length
= sizeof(struct acpi_tsd_package
);
568 state
.pointer
= pdomain
;
570 status
= acpi_extract_package(&(tsd
->package
.elements
[0]),
572 if (ACPI_FAILURE(status
)) {
573 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
578 if (pdomain
->num_entries
!= ACPI_TSD_REV0_ENTRIES
) {
579 printk(KERN_ERR PREFIX
"Unknown _TSD:num_entries\n");
584 if (pdomain
->revision
!= ACPI_TSD_REV0_REVISION
) {
585 printk(KERN_ERR PREFIX
"Unknown _TSD:revision\n");
590 pthrottling
= &pr
->throttling
;
591 pthrottling
->tsd_valid_flag
= 1;
592 pthrottling
->shared_type
= pdomain
->coord_type
;
593 cpumask_set_cpu(pr
->id
, pthrottling
->shared_cpu_map
);
595 * If the coordination type is not defined in ACPI spec,
596 * the tsd_valid_flag will be clear and coordination type
597 * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
599 if (pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ALL
&&
600 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ANY
&&
601 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_HW_ALL
) {
602 pthrottling
->tsd_valid_flag
= 0;
603 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
607 kfree(buffer
.pointer
);
611 /* --------------------------------------------------------------------------
613 -------------------------------------------------------------------------- */
614 static int acpi_processor_get_throttling_fadt(struct acpi_processor
*pr
)
624 if (!pr
->flags
.throttling
)
627 pr
->throttling
.state
= 0;
629 duty_mask
= pr
->throttling
.state_count
- 1;
631 duty_mask
<<= pr
->throttling
.duty_offset
;
635 value
= inl(pr
->throttling
.address
);
638 * Compute the current throttling state when throttling is enabled
642 duty_value
= value
& duty_mask
;
643 duty_value
>>= pr
->throttling
.duty_offset
;
646 state
= pr
->throttling
.state_count
- duty_value
;
649 pr
->throttling
.state
= state
;
653 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
654 "Throttling state is T%d (%d%% throttling applied)\n",
655 state
, pr
->throttling
.states
[state
].performance
));
661 static int acpi_throttling_rdmsr(struct acpi_processor
*pr
,
662 acpi_integer
* value
)
664 struct cpuinfo_x86
*c
;
665 u64 msr_high
, msr_low
;
673 if ((c
->x86_vendor
!= X86_VENDOR_INTEL
) ||
674 !cpu_has(c
, X86_FEATURE_ACPI
)) {
675 printk(KERN_ERR PREFIX
676 "HARDWARE addr space,NOT supported yet\n");
680 rdmsr_safe(MSR_IA32_THERM_CONTROL
,
681 (u32
*)&msr_low
, (u32
*) &msr_high
);
682 msr
= (msr_high
<< 32) | msr_low
;
683 *value
= (acpi_integer
) msr
;
689 static int acpi_throttling_wrmsr(struct acpi_processor
*pr
, acpi_integer value
)
691 struct cpuinfo_x86
*c
;
699 if ((c
->x86_vendor
!= X86_VENDOR_INTEL
) ||
700 !cpu_has(c
, X86_FEATURE_ACPI
)) {
701 printk(KERN_ERR PREFIX
702 "HARDWARE addr space,NOT supported yet\n");
705 wrmsr_safe(MSR_IA32_THERM_CONTROL
,
706 msr
& 0xffffffff, msr
>> 32);
712 static int acpi_throttling_rdmsr(struct acpi_processor
*pr
,
713 acpi_integer
* value
)
715 printk(KERN_ERR PREFIX
716 "HARDWARE addr space,NOT supported yet\n");
720 static int acpi_throttling_wrmsr(struct acpi_processor
*pr
, acpi_integer value
)
722 printk(KERN_ERR PREFIX
723 "HARDWARE addr space,NOT supported yet\n");
728 static int acpi_read_throttling_status(struct acpi_processor
*pr
,
731 u32 bit_width
, bit_offset
;
734 struct acpi_processor_throttling
*throttling
;
737 throttling
= &pr
->throttling
;
738 switch (throttling
->status_register
.space_id
) {
739 case ACPI_ADR_SPACE_SYSTEM_IO
:
741 bit_width
= throttling
->status_register
.bit_width
;
742 bit_offset
= throttling
->status_register
.bit_offset
;
744 acpi_os_read_port((acpi_io_address
) throttling
->status_register
.
745 address
, (u32
*) &ptc_value
,
746 (u32
) (bit_width
+ bit_offset
));
747 ptc_mask
= (1 << bit_width
) - 1;
748 *value
= (acpi_integer
) ((ptc_value
>> bit_offset
) & ptc_mask
);
751 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
752 ret
= acpi_throttling_rdmsr(pr
, value
);
755 printk(KERN_ERR PREFIX
"Unknown addr space %d\n",
756 (u32
) (throttling
->status_register
.space_id
));
761 static int acpi_write_throttling_state(struct acpi_processor
*pr
,
764 u32 bit_width
, bit_offset
;
767 struct acpi_processor_throttling
*throttling
;
770 throttling
= &pr
->throttling
;
771 switch (throttling
->control_register
.space_id
) {
772 case ACPI_ADR_SPACE_SYSTEM_IO
:
773 bit_width
= throttling
->control_register
.bit_width
;
774 bit_offset
= throttling
->control_register
.bit_offset
;
775 ptc_mask
= (1 << bit_width
) - 1;
776 ptc_value
= value
& ptc_mask
;
778 acpi_os_write_port((acpi_io_address
) throttling
->
779 control_register
.address
,
780 (u32
) (ptc_value
<< bit_offset
),
781 (u32
) (bit_width
+ bit_offset
));
784 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
785 ret
= acpi_throttling_wrmsr(pr
, value
);
788 printk(KERN_ERR PREFIX
"Unknown addr space %d\n",
789 (u32
) (throttling
->control_register
.space_id
));
794 static int acpi_get_throttling_state(struct acpi_processor
*pr
,
799 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
800 struct acpi_processor_tx_tss
*tx
=
801 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
803 if (tx
->control
== value
)
809 static int acpi_get_throttling_value(struct acpi_processor
*pr
,
810 int state
, acpi_integer
*value
)
814 if (state
>= 0 && state
<= pr
->throttling
.state_count
) {
815 struct acpi_processor_tx_tss
*tx
=
816 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
818 *value
= tx
->control
;
824 static int acpi_processor_get_throttling_ptc(struct acpi_processor
*pr
)
833 if (!pr
->flags
.throttling
)
836 pr
->throttling
.state
= 0;
839 ret
= acpi_read_throttling_status(pr
, &value
);
841 state
= acpi_get_throttling_state(pr
, value
);
843 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
844 "Invalid throttling state, reset\n"));
846 ret
= acpi_processor_set_throttling(pr
, state
, true);
850 pr
->throttling
.state
= state
;
856 static int acpi_processor_get_throttling(struct acpi_processor
*pr
)
858 cpumask_var_t saved_mask
;
864 if (!pr
->flags
.throttling
)
867 if (!alloc_cpumask_var(&saved_mask
, GFP_KERNEL
))
871 * Migrate task to the cpu pointed by pr.
873 cpumask_copy(saved_mask
, ¤t
->cpus_allowed
);
874 /* FIXME: use work_on_cpu() */
875 set_cpus_allowed_ptr(current
, cpumask_of(pr
->id
));
876 ret
= pr
->throttling
.acpi_processor_get_throttling(pr
);
877 /* restore the previous state */
878 set_cpus_allowed_ptr(current
, saved_mask
);
879 free_cpumask_var(saved_mask
);
884 static int acpi_processor_get_fadt_info(struct acpi_processor
*pr
)
888 if (!pr
->throttling
.address
) {
889 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No throttling register\n"));
891 } else if (!pr
->throttling
.duty_width
) {
892 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No throttling states\n"));
895 /* TBD: Support duty_cycle values that span bit 4. */
896 else if ((pr
->throttling
.duty_offset
+ pr
->throttling
.duty_width
) > 4) {
897 printk(KERN_WARNING PREFIX
"duty_cycle spans bit 4\n");
901 pr
->throttling
.state_count
= 1 << acpi_gbl_FADT
.duty_width
;
904 * Compute state values. Note that throttling displays a linear power
905 * performance relationship (at 50% performance the CPU will consume
906 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
909 step
= (1000 / pr
->throttling
.state_count
);
911 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
912 pr
->throttling
.states
[i
].performance
= 1000 - step
* i
;
913 pr
->throttling
.states
[i
].power
= 1000 - step
* i
;
918 static int acpi_processor_set_throttling_fadt(struct acpi_processor
*pr
,
919 int state
, bool force
)
928 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
931 if (!pr
->flags
.throttling
)
934 if (!force
&& (state
== pr
->throttling
.state
))
937 if (state
< pr
->throttling_platform_limit
)
940 * Calculate the duty_value and duty_mask.
943 duty_value
= pr
->throttling
.state_count
- state
;
945 duty_value
<<= pr
->throttling
.duty_offset
;
947 /* Used to clear all duty_value bits */
948 duty_mask
= pr
->throttling
.state_count
- 1;
950 duty_mask
<<= acpi_gbl_FADT
.duty_offset
;
951 duty_mask
= ~duty_mask
;
957 * Disable throttling by writing a 0 to bit 4. Note that we must
958 * turn it off before you can change the duty_value.
960 value
= inl(pr
->throttling
.address
);
963 outl(value
, pr
->throttling
.address
);
967 * Write the new duty_value and then enable throttling. Note
968 * that a state value of 0 leaves throttling disabled.
973 outl(value
, pr
->throttling
.address
);
976 outl(value
, pr
->throttling
.address
);
979 pr
->throttling
.state
= state
;
983 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
984 "Throttling state set to T%d (%d%%)\n", state
,
985 (pr
->throttling
.states
[state
].performance
? pr
->
986 throttling
.states
[state
].performance
/ 10 : 0)));
991 static int acpi_processor_set_throttling_ptc(struct acpi_processor
*pr
,
992 int state
, bool force
)
1000 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
1003 if (!pr
->flags
.throttling
)
1006 if (!force
&& (state
== pr
->throttling
.state
))
1009 if (state
< pr
->throttling_platform_limit
)
1013 ret
= acpi_get_throttling_value(pr
, state
, &value
);
1015 acpi_write_throttling_state(pr
, value
);
1016 pr
->throttling
.state
= state
;
1022 int acpi_processor_set_throttling(struct acpi_processor
*pr
,
1023 int state
, bool force
)
1025 cpumask_var_t saved_mask
;
1028 struct acpi_processor
*match_pr
;
1029 struct acpi_processor_throttling
*p_throttling
;
1030 struct throttling_tstate t_state
;
1031 cpumask_var_t online_throttling_cpus
;
1036 if (!pr
->flags
.throttling
)
1039 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
1042 if (!alloc_cpumask_var(&saved_mask
, GFP_KERNEL
))
1045 if (!alloc_cpumask_var(&online_throttling_cpus
, GFP_KERNEL
)) {
1046 free_cpumask_var(saved_mask
);
1050 cpumask_copy(saved_mask
, ¤t
->cpus_allowed
);
1051 t_state
.target_state
= state
;
1052 p_throttling
= &(pr
->throttling
);
1053 cpumask_and(online_throttling_cpus
, cpu_online_mask
,
1054 p_throttling
->shared_cpu_map
);
1056 * The throttling notifier will be called for every
1057 * affected cpu in order to get one proper T-state.
1058 * The notifier event is THROTTLING_PRECHANGE.
1060 for_each_cpu(i
, online_throttling_cpus
) {
1062 acpi_processor_throttling_notifier(THROTTLING_PRECHANGE
,
1066 * The function of acpi_processor_set_throttling will be called
1067 * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
1068 * it is necessary to call it for every affected cpu. Otherwise
1069 * it can be called only for the cpu pointed by pr.
1071 if (p_throttling
->shared_type
== DOMAIN_COORD_TYPE_SW_ANY
) {
1072 /* FIXME: use work_on_cpu() */
1073 set_cpus_allowed_ptr(current
, cpumask_of(pr
->id
));
1074 ret
= p_throttling
->acpi_processor_set_throttling(pr
,
1075 t_state
.target_state
, force
);
1078 * When the T-state coordination is SW_ALL or HW_ALL,
1079 * it is necessary to set T-state for every affected
1082 for_each_cpu(i
, online_throttling_cpus
) {
1083 match_pr
= per_cpu(processors
, i
);
1085 * If the pointer is invalid, we will report the
1086 * error message and continue.
1089 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1090 "Invalid Pointer for CPU %d\n", i
));
1094 * If the throttling control is unsupported on CPU i,
1095 * we will report the error message and continue.
1097 if (!match_pr
->flags
.throttling
) {
1098 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1099 "Throttling Controll is unsupported "
1104 /* FIXME: use work_on_cpu() */
1105 set_cpus_allowed_ptr(current
, cpumask_of(i
));
1106 ret
= match_pr
->throttling
.
1107 acpi_processor_set_throttling(
1108 match_pr
, t_state
.target_state
, force
);
1112 * After the set_throttling is called, the
1113 * throttling notifier is called for every
1114 * affected cpu to update the T-states.
1115 * The notifier event is THROTTLING_POSTCHANGE
1117 for_each_cpu(i
, online_throttling_cpus
) {
1119 acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE
,
1122 /* restore the previous state */
1123 /* FIXME: use work_on_cpu() */
1124 set_cpus_allowed_ptr(current
, saved_mask
);
1125 free_cpumask_var(online_throttling_cpus
);
1126 free_cpumask_var(saved_mask
);
1130 int acpi_processor_get_throttling_info(struct acpi_processor
*pr
)
1133 struct acpi_processor_throttling
*pthrottling
;
1135 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1136 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1137 pr
->throttling
.address
,
1138 pr
->throttling
.duty_offset
,
1139 pr
->throttling
.duty_width
));
1145 * Evaluate _PTC, _TSS and _TPC
1146 * They must all be present or none of them can be used.
1148 if (acpi_processor_get_throttling_control(pr
) ||
1149 acpi_processor_get_throttling_states(pr
) ||
1150 acpi_processor_get_platform_limit(pr
))
1152 pr
->throttling
.acpi_processor_get_throttling
=
1153 &acpi_processor_get_throttling_fadt
;
1154 pr
->throttling
.acpi_processor_set_throttling
=
1155 &acpi_processor_set_throttling_fadt
;
1156 if (acpi_processor_get_fadt_info(pr
))
1159 pr
->throttling
.acpi_processor_get_throttling
=
1160 &acpi_processor_get_throttling_ptc
;
1161 pr
->throttling
.acpi_processor_set_throttling
=
1162 &acpi_processor_set_throttling_ptc
;
1166 * If TSD package for one CPU can't be parsed successfully, it means
1167 * that this CPU will have no coordination with other CPUs.
1169 if (acpi_processor_get_tsd(pr
)) {
1170 pthrottling
= &pr
->throttling
;
1171 pthrottling
->tsd_valid_flag
= 0;
1172 cpumask_set_cpu(pr
->id
, pthrottling
->shared_cpu_map
);
1173 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
1177 * PIIX4 Errata: We don't support throttling on the original PIIX4.
1178 * This shouldn't be an issue as few (if any) mobile systems ever
1181 if (errata
.piix4
.throttle
) {
1182 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1183 "Throttling not supported on PIIX4 A- or B-step\n"));
1187 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d throttling states\n",
1188 pr
->throttling
.state_count
));
1190 pr
->flags
.throttling
= 1;
1193 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
1194 * thermal) decide to lower performance if it so chooses, but for now
1195 * we'll crank up the speed.
1198 result
= acpi_processor_get_throttling(pr
);
1202 if (pr
->throttling
.state
) {
1203 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1204 "Disabling throttling (was T%d)\n",
1205 pr
->throttling
.state
));
1206 result
= acpi_processor_set_throttling(pr
, 0, false);
1213 pr
->flags
.throttling
= 0;
1218 /* proc interface */
1220 static int acpi_processor_throttling_seq_show(struct seq_file
*seq
,
1223 struct acpi_processor
*pr
= seq
->private;
1230 if (!(pr
->throttling
.state_count
> 0)) {
1231 seq_puts(seq
, "<not supported>\n");
1235 result
= acpi_processor_get_throttling(pr
);
1239 "Could not determine current throttling state.\n");
1243 seq_printf(seq
, "state count: %d\n"
1244 "active state: T%d\n"
1245 "state available: T%d to T%d\n",
1246 pr
->throttling
.state_count
, pr
->throttling
.state
,
1247 pr
->throttling_platform_limit
,
1248 pr
->throttling
.state_count
- 1);
1250 seq_puts(seq
, "states:\n");
1251 if (pr
->throttling
.acpi_processor_get_throttling
==
1252 acpi_processor_get_throttling_fadt
) {
1253 for (i
= 0; i
< pr
->throttling
.state_count
; i
++)
1254 seq_printf(seq
, " %cT%d: %02d%%\n",
1255 (i
== pr
->throttling
.state
? '*' : ' '), i
,
1256 (pr
->throttling
.states
[i
].performance
? pr
->
1257 throttling
.states
[i
].performance
/ 10 : 0));
1259 for (i
= 0; i
< pr
->throttling
.state_count
; i
++)
1260 seq_printf(seq
, " %cT%d: %02d%%\n",
1261 (i
== pr
->throttling
.state
? '*' : ' '), i
,
1262 (int)pr
->throttling
.states_tss
[i
].
1270 static int acpi_processor_throttling_open_fs(struct inode
*inode
,
1273 return single_open(file
, acpi_processor_throttling_seq_show
,
1277 static ssize_t
acpi_processor_write_throttling(struct file
*file
,
1278 const char __user
* buffer
,
1279 size_t count
, loff_t
* data
)
1282 struct seq_file
*m
= file
->private_data
;
1283 struct acpi_processor
*pr
= m
->private;
1284 char state_string
[5] = "";
1286 size_t state_val
= 0;
1287 char tmpbuf
[5] = "";
1289 if (!pr
|| (count
> sizeof(state_string
) - 1))
1292 if (copy_from_user(state_string
, buffer
, count
))
1295 state_string
[count
] = '\0';
1296 if ((count
> 0) && (state_string
[count
-1] == '\n'))
1297 state_string
[count
-1] = '\0';
1299 charp
= state_string
;
1300 if ((state_string
[0] == 't') || (state_string
[0] == 'T'))
1303 state_val
= simple_strtoul(charp
, NULL
, 0);
1304 if (state_val
>= pr
->throttling
.state_count
)
1307 snprintf(tmpbuf
, 5, "%zu", state_val
);
1309 if (strcmp(tmpbuf
, charp
) != 0)
1312 result
= acpi_processor_set_throttling(pr
, state_val
, false);
1319 const struct file_operations acpi_processor_throttling_fops
= {
1320 .owner
= THIS_MODULE
,
1321 .open
= acpi_processor_throttling_open_fs
,
1323 .write
= acpi_processor_write_throttling
,
1324 .llseek
= seq_lseek
,
1325 .release
= single_release
,