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 PREFIX "ACPI: "
46 #define ACPI_PROCESSOR_CLASS "processor"
47 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
48 ACPI_MODULE_NAME("processor_throttling");
51 * 0 -> acpi processor driver doesn't ignore _TPC values
52 * 1 -> acpi processor driver ignores _TPC values
54 static int ignore_tpc
;
55 module_param(ignore_tpc
, int, 0644);
56 MODULE_PARM_DESC(ignore_tpc
, "Disable broken BIOS _TPC throttling support");
58 struct throttling_tstate
{
59 unsigned int cpu
; /* cpu nr */
60 int target_state
; /* target T-state */
63 #define THROTTLING_PRECHANGE (1)
64 #define THROTTLING_POSTCHANGE (2)
66 static int acpi_processor_get_throttling(struct acpi_processor
*pr
);
67 int acpi_processor_set_throttling(struct acpi_processor
*pr
,
68 int state
, bool force
);
70 static int acpi_processor_update_tsd_coord(void)
72 int count
, count_target
;
75 cpumask_var_t covered_cpus
;
76 struct acpi_processor
*pr
, *match_pr
;
77 struct acpi_tsd_package
*pdomain
, *match_pdomain
;
78 struct acpi_processor_throttling
*pthrottling
, *match_pthrottling
;
80 if (!zalloc_cpumask_var(&covered_cpus
, GFP_KERNEL
))
84 * Now that we have _TSD data from all CPUs, lets setup T-state
85 * coordination between all CPUs.
87 for_each_possible_cpu(i
) {
88 pr
= per_cpu(processors
, i
);
92 /* Basic validity check for domain info */
93 pthrottling
= &(pr
->throttling
);
96 * If tsd package for one cpu is invalid, the coordination
97 * among all CPUs is thought as invalid.
100 if (!pthrottling
->tsd_valid_flag
) {
108 for_each_possible_cpu(i
) {
109 pr
= per_cpu(processors
, i
);
113 if (cpumask_test_cpu(i
, covered_cpus
))
115 pthrottling
= &pr
->throttling
;
117 pdomain
= &(pthrottling
->domain_info
);
118 cpumask_set_cpu(i
, pthrottling
->shared_cpu_map
);
119 cpumask_set_cpu(i
, covered_cpus
);
121 * If the number of processor in the TSD domain is 1, it is
122 * unnecessary to parse the coordination for this CPU.
124 if (pdomain
->num_processors
<= 1)
127 /* Validate the Domain info */
128 count_target
= pdomain
->num_processors
;
131 for_each_possible_cpu(j
) {
135 match_pr
= per_cpu(processors
, j
);
139 match_pthrottling
= &(match_pr
->throttling
);
140 match_pdomain
= &(match_pthrottling
->domain_info
);
141 if (match_pdomain
->domain
!= pdomain
->domain
)
144 /* Here i and j are in the same domain.
145 * If two TSD packages have the same domain, they
146 * should have the same num_porcessors and
147 * coordination type. Otherwise it will be regarded
150 if (match_pdomain
->num_processors
!= count_target
) {
155 if (pdomain
->coord_type
!= match_pdomain
->coord_type
) {
160 cpumask_set_cpu(j
, covered_cpus
);
161 cpumask_set_cpu(j
, pthrottling
->shared_cpu_map
);
164 for_each_possible_cpu(j
) {
168 match_pr
= per_cpu(processors
, j
);
172 match_pthrottling
= &(match_pr
->throttling
);
173 match_pdomain
= &(match_pthrottling
->domain_info
);
174 if (match_pdomain
->domain
!= pdomain
->domain
)
178 * If some CPUS have the same domain, they
179 * will have the same shared_cpu_map.
181 cpumask_copy(match_pthrottling
->shared_cpu_map
,
182 pthrottling
->shared_cpu_map
);
187 free_cpumask_var(covered_cpus
);
189 for_each_possible_cpu(i
) {
190 pr
= per_cpu(processors
, i
);
195 * Assume no coordination on any error parsing domain info.
196 * The coordination type will be forced as SW_ALL.
199 pthrottling
= &(pr
->throttling
);
200 cpumask_clear(pthrottling
->shared_cpu_map
);
201 cpumask_set_cpu(i
, pthrottling
->shared_cpu_map
);
202 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
210 * Update the T-state coordination after the _TSD
211 * data for all cpus is obtained.
213 void acpi_processor_throttling_init(void)
215 if (acpi_processor_update_tsd_coord())
216 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
217 "Assume no T-state coordination\n"));
222 static int acpi_processor_throttling_notifier(unsigned long event
, void *data
)
224 struct throttling_tstate
*p_tstate
= data
;
225 struct acpi_processor
*pr
;
228 struct acpi_processor_limit
*p_limit
;
229 struct acpi_processor_throttling
*p_throttling
;
232 pr
= per_cpu(processors
, cpu
);
234 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Invalid pr pointer\n"));
237 if (!pr
->flags
.throttling
) {
238 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Throttling control is "
239 "unsupported on CPU %d\n", cpu
));
242 target_state
= p_tstate
->target_state
;
243 p_throttling
= &(pr
->throttling
);
245 case THROTTLING_PRECHANGE
:
247 * Prechange event is used to choose one proper t-state,
248 * which meets the limits of thermal, user and _TPC.
250 p_limit
= &pr
->limit
;
251 if (p_limit
->thermal
.tx
> target_state
)
252 target_state
= p_limit
->thermal
.tx
;
253 if (p_limit
->user
.tx
> target_state
)
254 target_state
= p_limit
->user
.tx
;
255 if (pr
->throttling_platform_limit
> target_state
)
256 target_state
= pr
->throttling_platform_limit
;
257 if (target_state
>= p_throttling
->state_count
) {
259 "Exceed the limit of T-state \n");
260 target_state
= p_throttling
->state_count
- 1;
262 p_tstate
->target_state
= target_state
;
263 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "PreChange Event:"
264 "target T-state of CPU %d is T%d\n",
267 case THROTTLING_POSTCHANGE
:
269 * Postchange event is only used to update the
270 * T-state flag of acpi_processor_throttling.
272 p_throttling
->state
= target_state
;
273 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "PostChange Event:"
274 "CPU %d is switched to T%d\n",
279 "Unsupported Throttling notifier event\n");
287 * _TPC - Throttling Present Capabilities
289 static int acpi_processor_get_platform_limit(struct acpi_processor
*pr
)
291 acpi_status status
= 0;
292 unsigned long long tpc
= 0;
300 status
= acpi_evaluate_integer(pr
->handle
, "_TPC", NULL
, &tpc
);
301 if (ACPI_FAILURE(status
)) {
302 if (status
!= AE_NOT_FOUND
) {
303 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TPC"));
309 pr
->throttling_platform_limit
= (int)tpc
;
313 int acpi_processor_tstate_has_changed(struct acpi_processor
*pr
)
316 int throttling_limit
;
318 struct acpi_processor_limit
*limit
;
324 result
= acpi_processor_get_platform_limit(pr
);
326 /* Throttling Limit is unsupported */
330 throttling_limit
= pr
->throttling_platform_limit
;
331 if (throttling_limit
>= pr
->throttling
.state_count
) {
332 /* Uncorrect Throttling Limit */
336 current_state
= pr
->throttling
.state
;
337 if (current_state
> throttling_limit
) {
339 * The current state can meet the requirement of
340 * _TPC limit. But it is reasonable that OSPM changes
341 * t-states from high to low for better performance.
342 * Of course the limit condition of thermal
343 * and user should be considered.
346 target_state
= throttling_limit
;
347 if (limit
->thermal
.tx
> target_state
)
348 target_state
= limit
->thermal
.tx
;
349 if (limit
->user
.tx
> target_state
)
350 target_state
= limit
->user
.tx
;
351 } else if (current_state
== throttling_limit
) {
353 * Unnecessary to change the throttling state
358 * If the current state is lower than the limit of _TPC, it
359 * will be forced to switch to the throttling state defined
360 * by throttling_platfor_limit.
361 * Because the previous state meets with the limit condition
362 * of thermal and user, it is unnecessary to check it again.
364 target_state
= throttling_limit
;
366 return acpi_processor_set_throttling(pr
, target_state
, false);
370 * _PTC - Processor Throttling Control (and status) register location
372 static int acpi_processor_get_throttling_control(struct acpi_processor
*pr
)
375 acpi_status status
= 0;
376 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
377 union acpi_object
*ptc
= NULL
;
378 union acpi_object obj
= { 0 };
379 struct acpi_processor_throttling
*throttling
;
381 status
= acpi_evaluate_object(pr
->handle
, "_PTC", NULL
, &buffer
);
382 if (ACPI_FAILURE(status
)) {
383 if (status
!= AE_NOT_FOUND
) {
384 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PTC"));
389 ptc
= (union acpi_object
*)buffer
.pointer
;
390 if (!ptc
|| (ptc
->type
!= ACPI_TYPE_PACKAGE
)
391 || (ptc
->package
.count
!= 2)) {
392 printk(KERN_ERR PREFIX
"Invalid _PTC data\n");
401 obj
= ptc
->package
.elements
[0];
403 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
404 || (obj
.buffer
.length
< sizeof(struct acpi_ptc_register
))
405 || (obj
.buffer
.pointer
== NULL
)) {
406 printk(KERN_ERR PREFIX
407 "Invalid _PTC data (control_register)\n");
411 memcpy(&pr
->throttling
.control_register
, obj
.buffer
.pointer
,
412 sizeof(struct acpi_ptc_register
));
418 obj
= ptc
->package
.elements
[1];
420 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
421 || (obj
.buffer
.length
< sizeof(struct acpi_ptc_register
))
422 || (obj
.buffer
.pointer
== NULL
)) {
423 printk(KERN_ERR PREFIX
"Invalid _PTC data (status_register)\n");
428 memcpy(&pr
->throttling
.status_register
, obj
.buffer
.pointer
,
429 sizeof(struct acpi_ptc_register
));
431 throttling
= &pr
->throttling
;
433 if ((throttling
->control_register
.bit_width
+
434 throttling
->control_register
.bit_offset
) > 32) {
435 printk(KERN_ERR PREFIX
"Invalid _PTC control register\n");
440 if ((throttling
->status_register
.bit_width
+
441 throttling
->status_register
.bit_offset
) > 32) {
442 printk(KERN_ERR PREFIX
"Invalid _PTC status register\n");
448 kfree(buffer
.pointer
);
454 * _TSS - Throttling Supported States
456 static int acpi_processor_get_throttling_states(struct acpi_processor
*pr
)
459 acpi_status status
= AE_OK
;
460 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
461 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
462 struct acpi_buffer state
= { 0, NULL
};
463 union acpi_object
*tss
= NULL
;
466 status
= acpi_evaluate_object(pr
->handle
, "_TSS", NULL
, &buffer
);
467 if (ACPI_FAILURE(status
)) {
468 if (status
!= AE_NOT_FOUND
) {
469 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TSS"));
474 tss
= buffer
.pointer
;
475 if (!tss
|| (tss
->type
!= ACPI_TYPE_PACKAGE
)) {
476 printk(KERN_ERR PREFIX
"Invalid _TSS data\n");
481 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d throttling states\n",
482 tss
->package
.count
));
484 pr
->throttling
.state_count
= tss
->package
.count
;
485 pr
->throttling
.states_tss
=
486 kmalloc(sizeof(struct acpi_processor_tx_tss
) * tss
->package
.count
,
488 if (!pr
->throttling
.states_tss
) {
493 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
495 struct acpi_processor_tx_tss
*tx
=
496 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
499 state
.length
= sizeof(struct acpi_processor_tx_tss
);
502 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Extracting state %d\n", i
));
504 status
= acpi_extract_package(&(tss
->package
.elements
[i
]),
506 if (ACPI_FAILURE(status
)) {
507 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _TSS data"));
509 kfree(pr
->throttling
.states_tss
);
513 if (!tx
->freqpercentage
) {
514 printk(KERN_ERR PREFIX
515 "Invalid _TSS data: freq is zero\n");
517 kfree(pr
->throttling
.states_tss
);
523 kfree(buffer
.pointer
);
529 * _TSD - T-State Dependencies
531 static int acpi_processor_get_tsd(struct acpi_processor
*pr
)
534 acpi_status status
= AE_OK
;
535 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
536 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
537 struct acpi_buffer state
= { 0, NULL
};
538 union acpi_object
*tsd
= NULL
;
539 struct acpi_tsd_package
*pdomain
;
540 struct acpi_processor_throttling
*pthrottling
;
542 pthrottling
= &pr
->throttling
;
543 pthrottling
->tsd_valid_flag
= 0;
545 status
= acpi_evaluate_object(pr
->handle
, "_TSD", NULL
, &buffer
);
546 if (ACPI_FAILURE(status
)) {
547 if (status
!= AE_NOT_FOUND
) {
548 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TSD"));
553 tsd
= buffer
.pointer
;
554 if (!tsd
|| (tsd
->type
!= ACPI_TYPE_PACKAGE
)) {
555 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
560 if (tsd
->package
.count
!= 1) {
561 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
566 pdomain
= &(pr
->throttling
.domain_info
);
568 state
.length
= sizeof(struct acpi_tsd_package
);
569 state
.pointer
= pdomain
;
571 status
= acpi_extract_package(&(tsd
->package
.elements
[0]),
573 if (ACPI_FAILURE(status
)) {
574 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
579 if (pdomain
->num_entries
!= ACPI_TSD_REV0_ENTRIES
) {
580 printk(KERN_ERR PREFIX
"Unknown _TSD:num_entries\n");
585 if (pdomain
->revision
!= ACPI_TSD_REV0_REVISION
) {
586 printk(KERN_ERR PREFIX
"Unknown _TSD:revision\n");
591 pthrottling
= &pr
->throttling
;
592 pthrottling
->tsd_valid_flag
= 1;
593 pthrottling
->shared_type
= pdomain
->coord_type
;
594 cpumask_set_cpu(pr
->id
, pthrottling
->shared_cpu_map
);
596 * If the coordination type is not defined in ACPI spec,
597 * the tsd_valid_flag will be clear and coordination type
598 * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
600 if (pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ALL
&&
601 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ANY
&&
602 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_HW_ALL
) {
603 pthrottling
->tsd_valid_flag
= 0;
604 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
608 kfree(buffer
.pointer
);
612 /* --------------------------------------------------------------------------
614 -------------------------------------------------------------------------- */
615 static int acpi_processor_get_throttling_fadt(struct acpi_processor
*pr
)
625 if (!pr
->flags
.throttling
)
628 pr
->throttling
.state
= 0;
630 duty_mask
= pr
->throttling
.state_count
- 1;
632 duty_mask
<<= pr
->throttling
.duty_offset
;
636 value
= inl(pr
->throttling
.address
);
639 * Compute the current throttling state when throttling is enabled
643 duty_value
= value
& duty_mask
;
644 duty_value
>>= pr
->throttling
.duty_offset
;
647 state
= pr
->throttling
.state_count
- duty_value
;
650 pr
->throttling
.state
= state
;
654 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
655 "Throttling state is T%d (%d%% throttling applied)\n",
656 state
, pr
->throttling
.states
[state
].performance
));
662 static int acpi_throttling_rdmsr(struct acpi_processor
*pr
,
665 struct cpuinfo_x86
*c
;
666 u64 msr_high
, msr_low
;
674 if ((c
->x86_vendor
!= X86_VENDOR_INTEL
) ||
675 !cpu_has(c
, X86_FEATURE_ACPI
)) {
676 printk(KERN_ERR PREFIX
677 "HARDWARE addr space,NOT supported yet\n");
681 rdmsr_safe(MSR_IA32_THERM_CONTROL
,
682 (u32
*)&msr_low
, (u32
*) &msr_high
);
683 msr
= (msr_high
<< 32) | msr_low
;
690 static int acpi_throttling_wrmsr(struct acpi_processor
*pr
, u64 value
)
692 struct cpuinfo_x86
*c
;
700 if ((c
->x86_vendor
!= X86_VENDOR_INTEL
) ||
701 !cpu_has(c
, X86_FEATURE_ACPI
)) {
702 printk(KERN_ERR PREFIX
703 "HARDWARE addr space,NOT supported yet\n");
706 wrmsr_safe(MSR_IA32_THERM_CONTROL
,
707 msr
& 0xffffffff, msr
>> 32);
713 static int acpi_throttling_rdmsr(struct acpi_processor
*pr
,
716 printk(KERN_ERR PREFIX
717 "HARDWARE addr space,NOT supported yet\n");
721 static int acpi_throttling_wrmsr(struct acpi_processor
*pr
, u64 value
)
723 printk(KERN_ERR PREFIX
724 "HARDWARE addr space,NOT supported yet\n");
729 static int acpi_read_throttling_status(struct acpi_processor
*pr
,
732 u32 bit_width
, bit_offset
;
735 struct acpi_processor_throttling
*throttling
;
738 throttling
= &pr
->throttling
;
739 switch (throttling
->status_register
.space_id
) {
740 case ACPI_ADR_SPACE_SYSTEM_IO
:
742 bit_width
= throttling
->status_register
.bit_width
;
743 bit_offset
= throttling
->status_register
.bit_offset
;
745 acpi_os_read_port((acpi_io_address
) throttling
->status_register
.
746 address
, (u32
*) &ptc_value
,
747 (u32
) (bit_width
+ bit_offset
));
748 ptc_mask
= (1 << bit_width
) - 1;
749 *value
= (u64
) ((ptc_value
>> bit_offset
) & ptc_mask
);
752 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
753 ret
= acpi_throttling_rdmsr(pr
, value
);
756 printk(KERN_ERR PREFIX
"Unknown addr space %d\n",
757 (u32
) (throttling
->status_register
.space_id
));
762 static int acpi_write_throttling_state(struct acpi_processor
*pr
,
765 u32 bit_width
, bit_offset
;
768 struct acpi_processor_throttling
*throttling
;
771 throttling
= &pr
->throttling
;
772 switch (throttling
->control_register
.space_id
) {
773 case ACPI_ADR_SPACE_SYSTEM_IO
:
774 bit_width
= throttling
->control_register
.bit_width
;
775 bit_offset
= throttling
->control_register
.bit_offset
;
776 ptc_mask
= (1 << bit_width
) - 1;
777 ptc_value
= value
& ptc_mask
;
779 acpi_os_write_port((acpi_io_address
) throttling
->
780 control_register
.address
,
781 (u32
) (ptc_value
<< bit_offset
),
782 (u32
) (bit_width
+ bit_offset
));
785 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
786 ret
= acpi_throttling_wrmsr(pr
, value
);
789 printk(KERN_ERR PREFIX
"Unknown addr space %d\n",
790 (u32
) (throttling
->control_register
.space_id
));
795 static int acpi_get_throttling_state(struct acpi_processor
*pr
,
800 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
801 struct acpi_processor_tx_tss
*tx
=
802 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
804 if (tx
->control
== value
)
810 static int acpi_get_throttling_value(struct acpi_processor
*pr
,
811 int state
, u64
*value
)
815 if (state
>= 0 && state
<= pr
->throttling
.state_count
) {
816 struct acpi_processor_tx_tss
*tx
=
817 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
819 *value
= tx
->control
;
825 static int acpi_processor_get_throttling_ptc(struct acpi_processor
*pr
)
834 if (!pr
->flags
.throttling
)
837 pr
->throttling
.state
= 0;
840 ret
= acpi_read_throttling_status(pr
, &value
);
842 state
= acpi_get_throttling_state(pr
, value
);
844 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
845 "Invalid throttling state, reset\n"));
847 ret
= acpi_processor_set_throttling(pr
, state
, true);
851 pr
->throttling
.state
= state
;
857 static int acpi_processor_get_throttling(struct acpi_processor
*pr
)
859 cpumask_var_t saved_mask
;
865 if (!pr
->flags
.throttling
)
868 if (!alloc_cpumask_var(&saved_mask
, GFP_KERNEL
))
872 * Migrate task to the cpu pointed by pr.
874 cpumask_copy(saved_mask
, ¤t
->cpus_allowed
);
875 /* FIXME: use work_on_cpu() */
876 set_cpus_allowed_ptr(current
, cpumask_of(pr
->id
));
877 ret
= pr
->throttling
.acpi_processor_get_throttling(pr
);
878 /* restore the previous state */
879 set_cpus_allowed_ptr(current
, saved_mask
);
880 free_cpumask_var(saved_mask
);
885 static int acpi_processor_get_fadt_info(struct acpi_processor
*pr
)
889 if (!pr
->throttling
.address
) {
890 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No throttling register\n"));
892 } else if (!pr
->throttling
.duty_width
) {
893 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No throttling states\n"));
896 /* TBD: Support duty_cycle values that span bit 4. */
897 else if ((pr
->throttling
.duty_offset
+ pr
->throttling
.duty_width
) > 4) {
898 printk(KERN_WARNING PREFIX
"duty_cycle spans bit 4\n");
902 pr
->throttling
.state_count
= 1 << acpi_gbl_FADT
.duty_width
;
905 * Compute state values. Note that throttling displays a linear power
906 * performance relationship (at 50% performance the CPU will consume
907 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
910 step
= (1000 / pr
->throttling
.state_count
);
912 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
913 pr
->throttling
.states
[i
].performance
= 1000 - step
* i
;
914 pr
->throttling
.states
[i
].power
= 1000 - step
* i
;
919 static int acpi_processor_set_throttling_fadt(struct acpi_processor
*pr
,
920 int state
, bool force
)
929 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
932 if (!pr
->flags
.throttling
)
935 if (!force
&& (state
== pr
->throttling
.state
))
938 if (state
< pr
->throttling_platform_limit
)
941 * Calculate the duty_value and duty_mask.
944 duty_value
= pr
->throttling
.state_count
- state
;
946 duty_value
<<= pr
->throttling
.duty_offset
;
948 /* Used to clear all duty_value bits */
949 duty_mask
= pr
->throttling
.state_count
- 1;
951 duty_mask
<<= acpi_gbl_FADT
.duty_offset
;
952 duty_mask
= ~duty_mask
;
958 * Disable throttling by writing a 0 to bit 4. Note that we must
959 * turn it off before you can change the duty_value.
961 value
= inl(pr
->throttling
.address
);
964 outl(value
, pr
->throttling
.address
);
968 * Write the new duty_value and then enable throttling. Note
969 * that a state value of 0 leaves throttling disabled.
974 outl(value
, pr
->throttling
.address
);
977 outl(value
, pr
->throttling
.address
);
980 pr
->throttling
.state
= state
;
984 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
985 "Throttling state set to T%d (%d%%)\n", state
,
986 (pr
->throttling
.states
[state
].performance
? pr
->
987 throttling
.states
[state
].performance
/ 10 : 0)));
992 static int acpi_processor_set_throttling_ptc(struct acpi_processor
*pr
,
993 int state
, bool force
)
1001 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
1004 if (!pr
->flags
.throttling
)
1007 if (!force
&& (state
== pr
->throttling
.state
))
1010 if (state
< pr
->throttling_platform_limit
)
1014 ret
= acpi_get_throttling_value(pr
, state
, &value
);
1016 acpi_write_throttling_state(pr
, value
);
1017 pr
->throttling
.state
= state
;
1023 int acpi_processor_set_throttling(struct acpi_processor
*pr
,
1024 int state
, bool force
)
1026 cpumask_var_t saved_mask
;
1029 struct acpi_processor
*match_pr
;
1030 struct acpi_processor_throttling
*p_throttling
;
1031 struct throttling_tstate t_state
;
1032 cpumask_var_t online_throttling_cpus
;
1037 if (!pr
->flags
.throttling
)
1040 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
1043 if (!alloc_cpumask_var(&saved_mask
, GFP_KERNEL
))
1046 if (!alloc_cpumask_var(&online_throttling_cpus
, GFP_KERNEL
)) {
1047 free_cpumask_var(saved_mask
);
1051 cpumask_copy(saved_mask
, ¤t
->cpus_allowed
);
1052 t_state
.target_state
= state
;
1053 p_throttling
= &(pr
->throttling
);
1054 cpumask_and(online_throttling_cpus
, cpu_online_mask
,
1055 p_throttling
->shared_cpu_map
);
1057 * The throttling notifier will be called for every
1058 * affected cpu in order to get one proper T-state.
1059 * The notifier event is THROTTLING_PRECHANGE.
1061 for_each_cpu(i
, online_throttling_cpus
) {
1063 acpi_processor_throttling_notifier(THROTTLING_PRECHANGE
,
1067 * The function of acpi_processor_set_throttling will be called
1068 * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
1069 * it is necessary to call it for every affected cpu. Otherwise
1070 * it can be called only for the cpu pointed by pr.
1072 if (p_throttling
->shared_type
== DOMAIN_COORD_TYPE_SW_ANY
) {
1073 /* FIXME: use work_on_cpu() */
1074 set_cpus_allowed_ptr(current
, cpumask_of(pr
->id
));
1075 ret
= p_throttling
->acpi_processor_set_throttling(pr
,
1076 t_state
.target_state
, force
);
1079 * When the T-state coordination is SW_ALL or HW_ALL,
1080 * it is necessary to set T-state for every affected
1083 for_each_cpu(i
, online_throttling_cpus
) {
1084 match_pr
= per_cpu(processors
, i
);
1086 * If the pointer is invalid, we will report the
1087 * error message and continue.
1090 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1091 "Invalid Pointer for CPU %d\n", i
));
1095 * If the throttling control is unsupported on CPU i,
1096 * we will report the error message and continue.
1098 if (!match_pr
->flags
.throttling
) {
1099 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1100 "Throttling Controll is unsupported "
1105 /* FIXME: use work_on_cpu() */
1106 set_cpus_allowed_ptr(current
, cpumask_of(i
));
1107 ret
= match_pr
->throttling
.
1108 acpi_processor_set_throttling(
1109 match_pr
, t_state
.target_state
, force
);
1113 * After the set_throttling is called, the
1114 * throttling notifier is called for every
1115 * affected cpu to update the T-states.
1116 * The notifier event is THROTTLING_POSTCHANGE
1118 for_each_cpu(i
, online_throttling_cpus
) {
1120 acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE
,
1123 /* restore the previous state */
1124 /* FIXME: use work_on_cpu() */
1125 set_cpus_allowed_ptr(current
, saved_mask
);
1126 free_cpumask_var(online_throttling_cpus
);
1127 free_cpumask_var(saved_mask
);
1131 int acpi_processor_get_throttling_info(struct acpi_processor
*pr
)
1134 struct acpi_processor_throttling
*pthrottling
;
1136 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1137 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1138 pr
->throttling
.address
,
1139 pr
->throttling
.duty_offset
,
1140 pr
->throttling
.duty_width
));
1143 * Evaluate _PTC, _TSS and _TPC
1144 * They must all be present or none of them can be used.
1146 if (acpi_processor_get_throttling_control(pr
) ||
1147 acpi_processor_get_throttling_states(pr
) ||
1148 acpi_processor_get_platform_limit(pr
))
1150 pr
->throttling
.acpi_processor_get_throttling
=
1151 &acpi_processor_get_throttling_fadt
;
1152 pr
->throttling
.acpi_processor_set_throttling
=
1153 &acpi_processor_set_throttling_fadt
;
1154 if (acpi_processor_get_fadt_info(pr
))
1157 pr
->throttling
.acpi_processor_get_throttling
=
1158 &acpi_processor_get_throttling_ptc
;
1159 pr
->throttling
.acpi_processor_set_throttling
=
1160 &acpi_processor_set_throttling_ptc
;
1164 * If TSD package for one CPU can't be parsed successfully, it means
1165 * that this CPU will have no coordination with other CPUs.
1167 if (acpi_processor_get_tsd(pr
)) {
1168 pthrottling
= &pr
->throttling
;
1169 pthrottling
->tsd_valid_flag
= 0;
1170 cpumask_set_cpu(pr
->id
, pthrottling
->shared_cpu_map
);
1171 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
1175 * PIIX4 Errata: We don't support throttling on the original PIIX4.
1176 * This shouldn't be an issue as few (if any) mobile systems ever
1179 if (errata
.piix4
.throttle
) {
1180 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1181 "Throttling not supported on PIIX4 A- or B-step\n"));
1185 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d throttling states\n",
1186 pr
->throttling
.state_count
));
1188 pr
->flags
.throttling
= 1;
1191 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
1192 * thermal) decide to lower performance if it so chooses, but for now
1193 * we'll crank up the speed.
1196 result
= acpi_processor_get_throttling(pr
);
1200 if (pr
->throttling
.state
) {
1201 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1202 "Disabling throttling (was T%d)\n",
1203 pr
->throttling
.state
));
1204 result
= acpi_processor_set_throttling(pr
, 0, false);
1211 pr
->flags
.throttling
= 0;
1216 /* proc interface */
1217 #ifdef CONFIG_ACPI_PROCFS
1218 static int acpi_processor_throttling_seq_show(struct seq_file
*seq
,
1221 struct acpi_processor
*pr
= seq
->private;
1228 if (!(pr
->throttling
.state_count
> 0)) {
1229 seq_puts(seq
, "<not supported>\n");
1233 result
= acpi_processor_get_throttling(pr
);
1237 "Could not determine current throttling state.\n");
1241 seq_printf(seq
, "state count: %d\n"
1242 "active state: T%d\n"
1243 "state available: T%d to T%d\n",
1244 pr
->throttling
.state_count
, pr
->throttling
.state
,
1245 pr
->throttling_platform_limit
,
1246 pr
->throttling
.state_count
- 1);
1248 seq_puts(seq
, "states:\n");
1249 if (pr
->throttling
.acpi_processor_get_throttling
==
1250 acpi_processor_get_throttling_fadt
) {
1251 for (i
= 0; i
< pr
->throttling
.state_count
; i
++)
1252 seq_printf(seq
, " %cT%d: %02d%%\n",
1253 (i
== pr
->throttling
.state
? '*' : ' '), i
,
1254 (pr
->throttling
.states
[i
].performance
? pr
->
1255 throttling
.states
[i
].performance
/ 10 : 0));
1257 for (i
= 0; i
< pr
->throttling
.state_count
; i
++)
1258 seq_printf(seq
, " %cT%d: %02d%%\n",
1259 (i
== pr
->throttling
.state
? '*' : ' '), i
,
1260 (int)pr
->throttling
.states_tss
[i
].
1268 static int acpi_processor_throttling_open_fs(struct inode
*inode
,
1271 return single_open(file
, acpi_processor_throttling_seq_show
,
1275 static ssize_t
acpi_processor_write_throttling(struct file
*file
,
1276 const char __user
* buffer
,
1277 size_t count
, loff_t
* data
)
1280 struct seq_file
*m
= file
->private_data
;
1281 struct acpi_processor
*pr
= m
->private;
1282 char state_string
[5] = "";
1284 size_t state_val
= 0;
1285 char tmpbuf
[5] = "";
1287 if (!pr
|| (count
> sizeof(state_string
) - 1))
1290 if (copy_from_user(state_string
, buffer
, count
))
1293 state_string
[count
] = '\0';
1294 if ((count
> 0) && (state_string
[count
-1] == '\n'))
1295 state_string
[count
-1] = '\0';
1297 charp
= state_string
;
1298 if ((state_string
[0] == 't') || (state_string
[0] == 'T'))
1301 state_val
= simple_strtoul(charp
, NULL
, 0);
1302 if (state_val
>= pr
->throttling
.state_count
)
1305 snprintf(tmpbuf
, 5, "%zu", state_val
);
1307 if (strcmp(tmpbuf
, charp
) != 0)
1310 result
= acpi_processor_set_throttling(pr
, state_val
, false);
1317 const struct file_operations acpi_processor_throttling_fops
= {
1318 .owner
= THIS_MODULE
,
1319 .open
= acpi_processor_throttling_open_fs
,
1321 .write
= acpi_processor_write_throttling
,
1322 .llseek
= seq_lseek
,
1323 .release
= single_release
,