2 * processor_throttling.c - Throttling submodule of the ACPI processor driver
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 * Copyright (C) 2004 Dominik Brodowski <linux@brodo.de>
7 * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
8 * - Added processor hotplug support
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/init.h>
33 #include <linux/sched.h>
34 #include <linux/cpufreq.h>
37 #include <asm/uaccess.h>
39 #include <acpi/acpi_bus.h>
40 #include <acpi/acpi_drivers.h>
41 #include <acpi/processor.h>
43 #define PREFIX "ACPI: "
45 #define ACPI_PROCESSOR_CLASS "processor"
46 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
47 ACPI_MODULE_NAME("processor_throttling");
50 * 0 -> acpi processor driver doesn't ignore _TPC values
51 * 1 -> acpi processor driver ignores _TPC values
53 static int ignore_tpc
;
54 module_param(ignore_tpc
, int, 0644);
55 MODULE_PARM_DESC(ignore_tpc
, "Disable broken BIOS _TPC throttling support");
57 struct throttling_tstate
{
58 unsigned int cpu
; /* cpu nr */
59 int target_state
; /* target T-state */
62 struct acpi_processor_throttling_arg
{
63 struct acpi_processor
*pr
;
68 #define THROTTLING_PRECHANGE (1)
69 #define THROTTLING_POSTCHANGE (2)
71 static int acpi_processor_get_throttling(struct acpi_processor
*pr
);
72 int acpi_processor_set_throttling(struct acpi_processor
*pr
,
73 int state
, bool force
);
75 static int acpi_processor_update_tsd_coord(void)
77 int count
, count_target
;
80 cpumask_var_t covered_cpus
;
81 struct acpi_processor
*pr
, *match_pr
;
82 struct acpi_tsd_package
*pdomain
, *match_pdomain
;
83 struct acpi_processor_throttling
*pthrottling
, *match_pthrottling
;
85 if (!zalloc_cpumask_var(&covered_cpus
, GFP_KERNEL
))
89 * Now that we have _TSD data from all CPUs, lets setup T-state
90 * coordination between all CPUs.
92 for_each_possible_cpu(i
) {
93 pr
= per_cpu(processors
, i
);
97 /* Basic validity check for domain info */
98 pthrottling
= &(pr
->throttling
);
101 * If tsd package for one cpu is invalid, the coordination
102 * among all CPUs is thought as invalid.
105 if (!pthrottling
->tsd_valid_flag
) {
113 for_each_possible_cpu(i
) {
114 pr
= per_cpu(processors
, i
);
118 if (cpumask_test_cpu(i
, covered_cpus
))
120 pthrottling
= &pr
->throttling
;
122 pdomain
= &(pthrottling
->domain_info
);
123 cpumask_set_cpu(i
, pthrottling
->shared_cpu_map
);
124 cpumask_set_cpu(i
, covered_cpus
);
126 * If the number of processor in the TSD domain is 1, it is
127 * unnecessary to parse the coordination for this CPU.
129 if (pdomain
->num_processors
<= 1)
132 /* Validate the Domain info */
133 count_target
= pdomain
->num_processors
;
136 for_each_possible_cpu(j
) {
140 match_pr
= per_cpu(processors
, j
);
144 match_pthrottling
= &(match_pr
->throttling
);
145 match_pdomain
= &(match_pthrottling
->domain_info
);
146 if (match_pdomain
->domain
!= pdomain
->domain
)
149 /* Here i and j are in the same domain.
150 * If two TSD packages have the same domain, they
151 * should have the same num_porcessors and
152 * coordination type. Otherwise it will be regarded
155 if (match_pdomain
->num_processors
!= count_target
) {
160 if (pdomain
->coord_type
!= match_pdomain
->coord_type
) {
165 cpumask_set_cpu(j
, covered_cpus
);
166 cpumask_set_cpu(j
, pthrottling
->shared_cpu_map
);
169 for_each_possible_cpu(j
) {
173 match_pr
= per_cpu(processors
, j
);
177 match_pthrottling
= &(match_pr
->throttling
);
178 match_pdomain
= &(match_pthrottling
->domain_info
);
179 if (match_pdomain
->domain
!= pdomain
->domain
)
183 * If some CPUS have the same domain, they
184 * will have the same shared_cpu_map.
186 cpumask_copy(match_pthrottling
->shared_cpu_map
,
187 pthrottling
->shared_cpu_map
);
192 free_cpumask_var(covered_cpus
);
194 for_each_possible_cpu(i
) {
195 pr
= per_cpu(processors
, i
);
200 * Assume no coordination on any error parsing domain info.
201 * The coordination type will be forced as SW_ALL.
204 pthrottling
= &(pr
->throttling
);
205 cpumask_clear(pthrottling
->shared_cpu_map
);
206 cpumask_set_cpu(i
, pthrottling
->shared_cpu_map
);
207 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
215 * Update the T-state coordination after the _TSD
216 * data for all cpus is obtained.
218 void acpi_processor_throttling_init(void)
220 if (acpi_processor_update_tsd_coord())
221 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
222 "Assume no T-state coordination\n"));
227 static int acpi_processor_throttling_notifier(unsigned long event
, void *data
)
229 struct throttling_tstate
*p_tstate
= data
;
230 struct acpi_processor
*pr
;
233 struct acpi_processor_limit
*p_limit
;
234 struct acpi_processor_throttling
*p_throttling
;
237 pr
= per_cpu(processors
, cpu
);
239 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Invalid pr pointer\n"));
242 if (!pr
->flags
.throttling
) {
243 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Throttling control is "
244 "unsupported on CPU %d\n", cpu
));
247 target_state
= p_tstate
->target_state
;
248 p_throttling
= &(pr
->throttling
);
250 case THROTTLING_PRECHANGE
:
252 * Prechange event is used to choose one proper t-state,
253 * which meets the limits of thermal, user and _TPC.
255 p_limit
= &pr
->limit
;
256 if (p_limit
->thermal
.tx
> target_state
)
257 target_state
= p_limit
->thermal
.tx
;
258 if (p_limit
->user
.tx
> target_state
)
259 target_state
= p_limit
->user
.tx
;
260 if (pr
->throttling_platform_limit
> target_state
)
261 target_state
= pr
->throttling_platform_limit
;
262 if (target_state
>= p_throttling
->state_count
) {
264 "Exceed the limit of T-state \n");
265 target_state
= p_throttling
->state_count
- 1;
267 p_tstate
->target_state
= target_state
;
268 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "PreChange Event:"
269 "target T-state of CPU %d is T%d\n",
272 case THROTTLING_POSTCHANGE
:
274 * Postchange event is only used to update the
275 * T-state flag of acpi_processor_throttling.
277 p_throttling
->state
= target_state
;
278 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "PostChange Event:"
279 "CPU %d is switched to T%d\n",
284 "Unsupported Throttling notifier event\n");
292 * _TPC - Throttling Present Capabilities
294 static int acpi_processor_get_platform_limit(struct acpi_processor
*pr
)
296 acpi_status status
= 0;
297 unsigned long long tpc
= 0;
305 status
= acpi_evaluate_integer(pr
->handle
, "_TPC", NULL
, &tpc
);
306 if (ACPI_FAILURE(status
)) {
307 if (status
!= AE_NOT_FOUND
) {
308 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TPC"));
314 pr
->throttling_platform_limit
= (int)tpc
;
318 int acpi_processor_tstate_has_changed(struct acpi_processor
*pr
)
321 int throttling_limit
;
323 struct acpi_processor_limit
*limit
;
329 result
= acpi_processor_get_platform_limit(pr
);
331 /* Throttling Limit is unsupported */
335 throttling_limit
= pr
->throttling_platform_limit
;
336 if (throttling_limit
>= pr
->throttling
.state_count
) {
337 /* Uncorrect Throttling Limit */
341 current_state
= pr
->throttling
.state
;
342 if (current_state
> throttling_limit
) {
344 * The current state can meet the requirement of
345 * _TPC limit. But it is reasonable that OSPM changes
346 * t-states from high to low for better performance.
347 * Of course the limit condition of thermal
348 * and user should be considered.
351 target_state
= throttling_limit
;
352 if (limit
->thermal
.tx
> target_state
)
353 target_state
= limit
->thermal
.tx
;
354 if (limit
->user
.tx
> target_state
)
355 target_state
= limit
->user
.tx
;
356 } else if (current_state
== throttling_limit
) {
358 * Unnecessary to change the throttling state
363 * If the current state is lower than the limit of _TPC, it
364 * will be forced to switch to the throttling state defined
365 * by throttling_platfor_limit.
366 * Because the previous state meets with the limit condition
367 * of thermal and user, it is unnecessary to check it again.
369 target_state
= throttling_limit
;
371 return acpi_processor_set_throttling(pr
, target_state
, false);
375 * This function is used to reevaluate whether the T-state is valid
376 * after one CPU is onlined/offlined.
377 * It is noted that it won't reevaluate the following properties for
380 * 2. the number of supported T-state
383 void acpi_processor_reevaluate_tstate(struct acpi_processor
*pr
,
384 unsigned long action
)
388 if (action
== CPU_DEAD
) {
389 /* When one CPU is offline, the T-state throttling
390 * will be invalidated.
392 pr
->flags
.throttling
= 0;
395 /* the following is to recheck whether the T-state is valid for
398 if (!pr
->throttling
.state_count
) {
399 /* If the number of T-state is invalid, it is
402 pr
->flags
.throttling
= 0;
405 pr
->flags
.throttling
= 1;
407 /* Disable throttling (if enabled). We'll let subsequent
408 * policy (e.g.thermal) decide to lower performance if it
409 * so chooses, but for now we'll crank up the speed.
412 result
= acpi_processor_get_throttling(pr
);
416 if (pr
->throttling
.state
) {
417 result
= acpi_processor_set_throttling(pr
, 0, false);
424 pr
->flags
.throttling
= 0;
427 * _PTC - Processor Throttling Control (and status) register location
429 static int acpi_processor_get_throttling_control(struct acpi_processor
*pr
)
432 acpi_status status
= 0;
433 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
434 union acpi_object
*ptc
= NULL
;
435 union acpi_object obj
= { 0 };
436 struct acpi_processor_throttling
*throttling
;
438 status
= acpi_evaluate_object(pr
->handle
, "_PTC", NULL
, &buffer
);
439 if (ACPI_FAILURE(status
)) {
440 if (status
!= AE_NOT_FOUND
) {
441 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PTC"));
446 ptc
= (union acpi_object
*)buffer
.pointer
;
447 if (!ptc
|| (ptc
->type
!= ACPI_TYPE_PACKAGE
)
448 || (ptc
->package
.count
!= 2)) {
449 printk(KERN_ERR PREFIX
"Invalid _PTC data\n");
458 obj
= ptc
->package
.elements
[0];
460 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
461 || (obj
.buffer
.length
< sizeof(struct acpi_ptc_register
))
462 || (obj
.buffer
.pointer
== NULL
)) {
463 printk(KERN_ERR PREFIX
464 "Invalid _PTC data (control_register)\n");
468 memcpy(&pr
->throttling
.control_register
, obj
.buffer
.pointer
,
469 sizeof(struct acpi_ptc_register
));
475 obj
= ptc
->package
.elements
[1];
477 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
478 || (obj
.buffer
.length
< sizeof(struct acpi_ptc_register
))
479 || (obj
.buffer
.pointer
== NULL
)) {
480 printk(KERN_ERR PREFIX
"Invalid _PTC data (status_register)\n");
485 memcpy(&pr
->throttling
.status_register
, obj
.buffer
.pointer
,
486 sizeof(struct acpi_ptc_register
));
488 throttling
= &pr
->throttling
;
490 if ((throttling
->control_register
.bit_width
+
491 throttling
->control_register
.bit_offset
) > 32) {
492 printk(KERN_ERR PREFIX
"Invalid _PTC control register\n");
497 if ((throttling
->status_register
.bit_width
+
498 throttling
->status_register
.bit_offset
) > 32) {
499 printk(KERN_ERR PREFIX
"Invalid _PTC status register\n");
505 kfree(buffer
.pointer
);
511 * _TSS - Throttling Supported States
513 static int acpi_processor_get_throttling_states(struct acpi_processor
*pr
)
516 acpi_status status
= AE_OK
;
517 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
518 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
519 struct acpi_buffer state
= { 0, NULL
};
520 union acpi_object
*tss
= NULL
;
523 status
= acpi_evaluate_object(pr
->handle
, "_TSS", NULL
, &buffer
);
524 if (ACPI_FAILURE(status
)) {
525 if (status
!= AE_NOT_FOUND
) {
526 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TSS"));
531 tss
= buffer
.pointer
;
532 if (!tss
|| (tss
->type
!= ACPI_TYPE_PACKAGE
)) {
533 printk(KERN_ERR PREFIX
"Invalid _TSS data\n");
538 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d throttling states\n",
539 tss
->package
.count
));
541 pr
->throttling
.state_count
= tss
->package
.count
;
542 pr
->throttling
.states_tss
=
543 kmalloc(sizeof(struct acpi_processor_tx_tss
) * tss
->package
.count
,
545 if (!pr
->throttling
.states_tss
) {
550 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
552 struct acpi_processor_tx_tss
*tx
=
553 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
556 state
.length
= sizeof(struct acpi_processor_tx_tss
);
559 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Extracting state %d\n", i
));
561 status
= acpi_extract_package(&(tss
->package
.elements
[i
]),
563 if (ACPI_FAILURE(status
)) {
564 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _TSS data"));
566 kfree(pr
->throttling
.states_tss
);
570 if (!tx
->freqpercentage
) {
571 printk(KERN_ERR PREFIX
572 "Invalid _TSS data: freq is zero\n");
574 kfree(pr
->throttling
.states_tss
);
580 kfree(buffer
.pointer
);
586 * _TSD - T-State Dependencies
588 static int acpi_processor_get_tsd(struct acpi_processor
*pr
)
591 acpi_status status
= AE_OK
;
592 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
593 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
594 struct acpi_buffer state
= { 0, NULL
};
595 union acpi_object
*tsd
= NULL
;
596 struct acpi_tsd_package
*pdomain
;
597 struct acpi_processor_throttling
*pthrottling
;
599 pthrottling
= &pr
->throttling
;
600 pthrottling
->tsd_valid_flag
= 0;
602 status
= acpi_evaluate_object(pr
->handle
, "_TSD", NULL
, &buffer
);
603 if (ACPI_FAILURE(status
)) {
604 if (status
!= AE_NOT_FOUND
) {
605 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TSD"));
610 tsd
= buffer
.pointer
;
611 if (!tsd
|| (tsd
->type
!= ACPI_TYPE_PACKAGE
)) {
612 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
617 if (tsd
->package
.count
!= 1) {
618 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
623 pdomain
= &(pr
->throttling
.domain_info
);
625 state
.length
= sizeof(struct acpi_tsd_package
);
626 state
.pointer
= pdomain
;
628 status
= acpi_extract_package(&(tsd
->package
.elements
[0]),
630 if (ACPI_FAILURE(status
)) {
631 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
636 if (pdomain
->num_entries
!= ACPI_TSD_REV0_ENTRIES
) {
637 printk(KERN_ERR PREFIX
"Unknown _TSD:num_entries\n");
642 if (pdomain
->revision
!= ACPI_TSD_REV0_REVISION
) {
643 printk(KERN_ERR PREFIX
"Unknown _TSD:revision\n");
648 pthrottling
= &pr
->throttling
;
649 pthrottling
->tsd_valid_flag
= 1;
650 pthrottling
->shared_type
= pdomain
->coord_type
;
651 cpumask_set_cpu(pr
->id
, pthrottling
->shared_cpu_map
);
653 * If the coordination type is not defined in ACPI spec,
654 * the tsd_valid_flag will be clear and coordination type
655 * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
657 if (pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ALL
&&
658 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ANY
&&
659 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_HW_ALL
) {
660 pthrottling
->tsd_valid_flag
= 0;
661 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
665 kfree(buffer
.pointer
);
669 /* --------------------------------------------------------------------------
671 -------------------------------------------------------------------------- */
672 static int acpi_processor_get_throttling_fadt(struct acpi_processor
*pr
)
682 if (!pr
->flags
.throttling
)
685 pr
->throttling
.state
= 0;
687 duty_mask
= pr
->throttling
.state_count
- 1;
689 duty_mask
<<= pr
->throttling
.duty_offset
;
693 value
= inl(pr
->throttling
.address
);
696 * Compute the current throttling state when throttling is enabled
700 duty_value
= value
& duty_mask
;
701 duty_value
>>= pr
->throttling
.duty_offset
;
704 state
= pr
->throttling
.state_count
- duty_value
;
707 pr
->throttling
.state
= state
;
711 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
712 "Throttling state is T%d (%d%% throttling applied)\n",
713 state
, pr
->throttling
.states
[state
].performance
));
719 static int acpi_throttling_rdmsr(u64
*value
)
721 u64 msr_high
, msr_low
;
725 if ((this_cpu_read(cpu_info
.x86_vendor
) != X86_VENDOR_INTEL
) ||
726 !this_cpu_has(X86_FEATURE_ACPI
)) {
727 printk(KERN_ERR PREFIX
728 "HARDWARE addr space,NOT supported yet\n");
732 rdmsr_safe(MSR_IA32_THERM_CONTROL
,
733 (u32
*)&msr_low
, (u32
*) &msr_high
);
734 msr
= (msr_high
<< 32) | msr_low
;
741 static int acpi_throttling_wrmsr(u64 value
)
746 if ((this_cpu_read(cpu_info
.x86_vendor
) != X86_VENDOR_INTEL
) ||
747 !this_cpu_has(X86_FEATURE_ACPI
)) {
748 printk(KERN_ERR PREFIX
749 "HARDWARE addr space,NOT supported yet\n");
752 wrmsr_safe(MSR_IA32_THERM_CONTROL
,
753 msr
& 0xffffffff, msr
>> 32);
759 static int acpi_throttling_rdmsr(u64
*value
)
761 printk(KERN_ERR PREFIX
762 "HARDWARE addr space,NOT supported yet\n");
766 static int acpi_throttling_wrmsr(u64 value
)
768 printk(KERN_ERR PREFIX
769 "HARDWARE addr space,NOT supported yet\n");
774 static int acpi_read_throttling_status(struct acpi_processor
*pr
,
777 u32 bit_width
, bit_offset
;
780 struct acpi_processor_throttling
*throttling
;
783 throttling
= &pr
->throttling
;
784 switch (throttling
->status_register
.space_id
) {
785 case ACPI_ADR_SPACE_SYSTEM_IO
:
786 bit_width
= throttling
->status_register
.bit_width
;
787 bit_offset
= throttling
->status_register
.bit_offset
;
789 acpi_os_read_port((acpi_io_address
) throttling
->status_register
.
791 (u32
) (bit_width
+ bit_offset
));
792 ptc_mask
= (1 << bit_width
) - 1;
793 *value
= (u64
) ((ptc_value
>> bit_offset
) & ptc_mask
);
796 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
797 ret
= acpi_throttling_rdmsr(value
);
800 printk(KERN_ERR PREFIX
"Unknown addr space %d\n",
801 (u32
) (throttling
->status_register
.space_id
));
806 static int acpi_write_throttling_state(struct acpi_processor
*pr
,
809 u32 bit_width
, bit_offset
;
812 struct acpi_processor_throttling
*throttling
;
815 throttling
= &pr
->throttling
;
816 switch (throttling
->control_register
.space_id
) {
817 case ACPI_ADR_SPACE_SYSTEM_IO
:
818 bit_width
= throttling
->control_register
.bit_width
;
819 bit_offset
= throttling
->control_register
.bit_offset
;
820 ptc_mask
= (1 << bit_width
) - 1;
821 ptc_value
= value
& ptc_mask
;
823 acpi_os_write_port((acpi_io_address
) throttling
->
824 control_register
.address
,
825 (u32
) (ptc_value
<< bit_offset
),
826 (u32
) (bit_width
+ bit_offset
));
829 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
830 ret
= acpi_throttling_wrmsr(value
);
833 printk(KERN_ERR PREFIX
"Unknown addr space %d\n",
834 (u32
) (throttling
->control_register
.space_id
));
839 static int acpi_get_throttling_state(struct acpi_processor
*pr
,
844 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
845 struct acpi_processor_tx_tss
*tx
=
846 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
848 if (tx
->control
== value
)
854 static int acpi_get_throttling_value(struct acpi_processor
*pr
,
855 int state
, u64
*value
)
859 if (state
>= 0 && state
<= pr
->throttling
.state_count
) {
860 struct acpi_processor_tx_tss
*tx
=
861 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
863 *value
= tx
->control
;
869 static int acpi_processor_get_throttling_ptc(struct acpi_processor
*pr
)
878 if (!pr
->flags
.throttling
)
881 pr
->throttling
.state
= 0;
884 ret
= acpi_read_throttling_status(pr
, &value
);
886 state
= acpi_get_throttling_state(pr
, value
);
888 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
889 "Invalid throttling state, reset\n"));
891 ret
= acpi_processor_set_throttling(pr
, state
, true);
895 pr
->throttling
.state
= state
;
901 static int acpi_processor_get_throttling(struct acpi_processor
*pr
)
903 cpumask_var_t saved_mask
;
909 if (!pr
->flags
.throttling
)
912 if (!alloc_cpumask_var(&saved_mask
, GFP_KERNEL
))
916 * Migrate task to the cpu pointed by pr.
918 cpumask_copy(saved_mask
, ¤t
->cpus_allowed
);
919 /* FIXME: use work_on_cpu() */
920 if (set_cpus_allowed_ptr(current
, cpumask_of(pr
->id
))) {
921 /* Can't migrate to the target pr->id CPU. Exit */
922 free_cpumask_var(saved_mask
);
925 ret
= pr
->throttling
.acpi_processor_get_throttling(pr
);
926 /* restore the previous state */
927 set_cpus_allowed_ptr(current
, saved_mask
);
928 free_cpumask_var(saved_mask
);
933 static int acpi_processor_get_fadt_info(struct acpi_processor
*pr
)
937 if (!pr
->throttling
.address
) {
938 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No throttling register\n"));
940 } else if (!pr
->throttling
.duty_width
) {
941 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No throttling states\n"));
944 /* TBD: Support duty_cycle values that span bit 4. */
945 else if ((pr
->throttling
.duty_offset
+ pr
->throttling
.duty_width
) > 4) {
946 printk(KERN_WARNING PREFIX
"duty_cycle spans bit 4\n");
950 pr
->throttling
.state_count
= 1 << acpi_gbl_FADT
.duty_width
;
953 * Compute state values. Note that throttling displays a linear power
954 * performance relationship (at 50% performance the CPU will consume
955 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
958 step
= (1000 / pr
->throttling
.state_count
);
960 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
961 pr
->throttling
.states
[i
].performance
= 1000 - step
* i
;
962 pr
->throttling
.states
[i
].power
= 1000 - step
* i
;
967 static int acpi_processor_set_throttling_fadt(struct acpi_processor
*pr
,
968 int state
, bool force
)
977 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
980 if (!pr
->flags
.throttling
)
983 if (!force
&& (state
== pr
->throttling
.state
))
986 if (state
< pr
->throttling_platform_limit
)
989 * Calculate the duty_value and duty_mask.
992 duty_value
= pr
->throttling
.state_count
- state
;
994 duty_value
<<= pr
->throttling
.duty_offset
;
996 /* Used to clear all duty_value bits */
997 duty_mask
= pr
->throttling
.state_count
- 1;
999 duty_mask
<<= acpi_gbl_FADT
.duty_offset
;
1000 duty_mask
= ~duty_mask
;
1003 local_irq_disable();
1006 * Disable throttling by writing a 0 to bit 4. Note that we must
1007 * turn it off before you can change the duty_value.
1009 value
= inl(pr
->throttling
.address
);
1011 value
&= 0xFFFFFFEF;
1012 outl(value
, pr
->throttling
.address
);
1016 * Write the new duty_value and then enable throttling. Note
1017 * that a state value of 0 leaves throttling disabled.
1021 value
|= duty_value
;
1022 outl(value
, pr
->throttling
.address
);
1024 value
|= 0x00000010;
1025 outl(value
, pr
->throttling
.address
);
1028 pr
->throttling
.state
= state
;
1032 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1033 "Throttling state set to T%d (%d%%)\n", state
,
1034 (pr
->throttling
.states
[state
].performance
? pr
->
1035 throttling
.states
[state
].performance
/ 10 : 0)));
1040 static int acpi_processor_set_throttling_ptc(struct acpi_processor
*pr
,
1041 int state
, bool force
)
1049 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
1052 if (!pr
->flags
.throttling
)
1055 if (!force
&& (state
== pr
->throttling
.state
))
1058 if (state
< pr
->throttling_platform_limit
)
1062 ret
= acpi_get_throttling_value(pr
, state
, &value
);
1064 acpi_write_throttling_state(pr
, value
);
1065 pr
->throttling
.state
= state
;
1071 static long acpi_processor_throttling_fn(void *data
)
1073 struct acpi_processor_throttling_arg
*arg
= data
;
1074 struct acpi_processor
*pr
= arg
->pr
;
1076 return pr
->throttling
.acpi_processor_set_throttling(pr
,
1077 arg
->target_state
, arg
->force
);
1080 int acpi_processor_set_throttling(struct acpi_processor
*pr
,
1081 int state
, bool force
)
1085 struct acpi_processor
*match_pr
;
1086 struct acpi_processor_throttling
*p_throttling
;
1087 struct acpi_processor_throttling_arg arg
;
1088 struct throttling_tstate t_state
;
1093 if (!pr
->flags
.throttling
)
1096 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
1099 if (cpu_is_offline(pr
->id
)) {
1101 * the cpu pointed by pr->id is offline. Unnecessary to change
1102 * the throttling state any more.
1107 t_state
.target_state
= state
;
1108 p_throttling
= &(pr
->throttling
);
1111 * The throttling notifier will be called for every
1112 * affected cpu in order to get one proper T-state.
1113 * The notifier event is THROTTLING_PRECHANGE.
1115 for_each_cpu_and(i
, cpu_online_mask
, p_throttling
->shared_cpu_map
) {
1117 acpi_processor_throttling_notifier(THROTTLING_PRECHANGE
,
1121 * The function of acpi_processor_set_throttling will be called
1122 * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
1123 * it is necessary to call it for every affected cpu. Otherwise
1124 * it can be called only for the cpu pointed by pr.
1126 if (p_throttling
->shared_type
== DOMAIN_COORD_TYPE_SW_ANY
) {
1128 arg
.target_state
= state
;
1130 ret
= work_on_cpu(pr
->id
, acpi_processor_throttling_fn
, &arg
);
1133 * When the T-state coordination is SW_ALL or HW_ALL,
1134 * it is necessary to set T-state for every affected
1137 for_each_cpu_and(i
, cpu_online_mask
,
1138 p_throttling
->shared_cpu_map
) {
1139 match_pr
= per_cpu(processors
, i
);
1141 * If the pointer is invalid, we will report the
1142 * error message and continue.
1145 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1146 "Invalid Pointer for CPU %d\n", i
));
1150 * If the throttling control is unsupported on CPU i,
1151 * we will report the error message and continue.
1153 if (!match_pr
->flags
.throttling
) {
1154 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1155 "Throttling Control is unsupported "
1161 arg
.target_state
= state
;
1163 ret
= work_on_cpu(pr
->id
, acpi_processor_throttling_fn
,
1168 * After the set_throttling is called, the
1169 * throttling notifier is called for every
1170 * affected cpu to update the T-states.
1171 * The notifier event is THROTTLING_POSTCHANGE
1173 for_each_cpu_and(i
, cpu_online_mask
, p_throttling
->shared_cpu_map
) {
1175 acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE
,
1182 int acpi_processor_get_throttling_info(struct acpi_processor
*pr
)
1185 struct acpi_processor_throttling
*pthrottling
;
1187 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1188 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1189 pr
->throttling
.address
,
1190 pr
->throttling
.duty_offset
,
1191 pr
->throttling
.duty_width
));
1194 * Evaluate _PTC, _TSS and _TPC
1195 * They must all be present or none of them can be used.
1197 if (acpi_processor_get_throttling_control(pr
) ||
1198 acpi_processor_get_throttling_states(pr
) ||
1199 acpi_processor_get_platform_limit(pr
))
1201 pr
->throttling
.acpi_processor_get_throttling
=
1202 &acpi_processor_get_throttling_fadt
;
1203 pr
->throttling
.acpi_processor_set_throttling
=
1204 &acpi_processor_set_throttling_fadt
;
1205 if (acpi_processor_get_fadt_info(pr
))
1208 pr
->throttling
.acpi_processor_get_throttling
=
1209 &acpi_processor_get_throttling_ptc
;
1210 pr
->throttling
.acpi_processor_set_throttling
=
1211 &acpi_processor_set_throttling_ptc
;
1215 * If TSD package for one CPU can't be parsed successfully, it means
1216 * that this CPU will have no coordination with other CPUs.
1218 if (acpi_processor_get_tsd(pr
)) {
1219 pthrottling
= &pr
->throttling
;
1220 pthrottling
->tsd_valid_flag
= 0;
1221 cpumask_set_cpu(pr
->id
, pthrottling
->shared_cpu_map
);
1222 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
1226 * PIIX4 Errata: We don't support throttling on the original PIIX4.
1227 * This shouldn't be an issue as few (if any) mobile systems ever
1230 if (errata
.piix4
.throttle
) {
1231 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1232 "Throttling not supported on PIIX4 A- or B-step\n"));
1236 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d throttling states\n",
1237 pr
->throttling
.state_count
));
1239 pr
->flags
.throttling
= 1;
1242 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
1243 * thermal) decide to lower performance if it so chooses, but for now
1244 * we'll crank up the speed.
1247 result
= acpi_processor_get_throttling(pr
);
1251 if (pr
->throttling
.state
) {
1252 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1253 "Disabling throttling (was T%d)\n",
1254 pr
->throttling
.state
));
1255 result
= acpi_processor_set_throttling(pr
, 0, false);
1262 pr
->flags
.throttling
= 0;