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/acpi.h>
36 #include <acpi/processor.h>
38 #include <asm/uaccess.h>
40 #define PREFIX "ACPI: "
42 #define ACPI_PROCESSOR_CLASS "processor"
43 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
44 ACPI_MODULE_NAME("processor_throttling");
47 * 0 -> acpi processor driver doesn't ignore _TPC values
48 * 1 -> acpi processor driver ignores _TPC values
50 static int ignore_tpc
;
51 module_param(ignore_tpc
, int, 0644);
52 MODULE_PARM_DESC(ignore_tpc
, "Disable broken BIOS _TPC throttling support");
54 struct throttling_tstate
{
55 unsigned int cpu
; /* cpu nr */
56 int target_state
; /* target T-state */
59 #define THROTTLING_PRECHANGE (1)
60 #define THROTTLING_POSTCHANGE (2)
62 static int acpi_processor_get_throttling(struct acpi_processor
*pr
);
63 int acpi_processor_set_throttling(struct acpi_processor
*pr
,
64 int state
, bool force
);
66 static int acpi_processor_update_tsd_coord(void)
68 int count
, count_target
;
71 cpumask_var_t covered_cpus
;
72 struct acpi_processor
*pr
, *match_pr
;
73 struct acpi_tsd_package
*pdomain
, *match_pdomain
;
74 struct acpi_processor_throttling
*pthrottling
, *match_pthrottling
;
76 if (!zalloc_cpumask_var(&covered_cpus
, GFP_KERNEL
))
80 * Now that we have _TSD data from all CPUs, lets setup T-state
81 * coordination between all CPUs.
83 for_each_possible_cpu(i
) {
84 pr
= per_cpu(processors
, i
);
88 /* Basic validity check for domain info */
89 pthrottling
= &(pr
->throttling
);
92 * If tsd package for one cpu is invalid, the coordination
93 * among all CPUs is thought as invalid.
96 if (!pthrottling
->tsd_valid_flag
) {
104 for_each_possible_cpu(i
) {
105 pr
= per_cpu(processors
, i
);
109 if (cpumask_test_cpu(i
, covered_cpus
))
111 pthrottling
= &pr
->throttling
;
113 pdomain
= &(pthrottling
->domain_info
);
114 cpumask_set_cpu(i
, pthrottling
->shared_cpu_map
);
115 cpumask_set_cpu(i
, covered_cpus
);
117 * If the number of processor in the TSD domain is 1, it is
118 * unnecessary to parse the coordination for this CPU.
120 if (pdomain
->num_processors
<= 1)
123 /* Validate the Domain info */
124 count_target
= pdomain
->num_processors
;
127 for_each_possible_cpu(j
) {
131 match_pr
= per_cpu(processors
, j
);
135 match_pthrottling
= &(match_pr
->throttling
);
136 match_pdomain
= &(match_pthrottling
->domain_info
);
137 if (match_pdomain
->domain
!= pdomain
->domain
)
140 /* Here i and j are in the same domain.
141 * If two TSD packages have the same domain, they
142 * should have the same num_porcessors and
143 * coordination type. Otherwise it will be regarded
146 if (match_pdomain
->num_processors
!= count_target
) {
151 if (pdomain
->coord_type
!= match_pdomain
->coord_type
) {
156 cpumask_set_cpu(j
, covered_cpus
);
157 cpumask_set_cpu(j
, pthrottling
->shared_cpu_map
);
160 for_each_possible_cpu(j
) {
164 match_pr
= per_cpu(processors
, j
);
168 match_pthrottling
= &(match_pr
->throttling
);
169 match_pdomain
= &(match_pthrottling
->domain_info
);
170 if (match_pdomain
->domain
!= pdomain
->domain
)
174 * If some CPUS have the same domain, they
175 * will have the same shared_cpu_map.
177 cpumask_copy(match_pthrottling
->shared_cpu_map
,
178 pthrottling
->shared_cpu_map
);
183 free_cpumask_var(covered_cpus
);
185 for_each_possible_cpu(i
) {
186 pr
= per_cpu(processors
, i
);
191 * Assume no coordination on any error parsing domain info.
192 * The coordination type will be forced as SW_ALL.
195 pthrottling
= &(pr
->throttling
);
196 cpumask_clear(pthrottling
->shared_cpu_map
);
197 cpumask_set_cpu(i
, pthrottling
->shared_cpu_map
);
198 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
206 * Update the T-state coordination after the _TSD
207 * data for all cpus is obtained.
209 void acpi_processor_throttling_init(void)
211 if (acpi_processor_update_tsd_coord()) {
212 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
213 "Assume no T-state coordination\n"));
219 static int acpi_processor_throttling_notifier(unsigned long event
, void *data
)
221 struct throttling_tstate
*p_tstate
= data
;
222 struct acpi_processor
*pr
;
225 struct acpi_processor_limit
*p_limit
;
226 struct acpi_processor_throttling
*p_throttling
;
229 pr
= per_cpu(processors
, cpu
);
231 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Invalid pr pointer\n"));
234 if (!pr
->flags
.throttling
) {
235 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Throttling control is "
236 "unsupported on CPU %d\n", cpu
));
239 target_state
= p_tstate
->target_state
;
240 p_throttling
= &(pr
->throttling
);
242 case THROTTLING_PRECHANGE
:
244 * Prechange event is used to choose one proper t-state,
245 * which meets the limits of thermal, user and _TPC.
247 p_limit
= &pr
->limit
;
248 if (p_limit
->thermal
.tx
> target_state
)
249 target_state
= p_limit
->thermal
.tx
;
250 if (p_limit
->user
.tx
> target_state
)
251 target_state
= p_limit
->user
.tx
;
252 if (pr
->throttling_platform_limit
> target_state
)
253 target_state
= pr
->throttling_platform_limit
;
254 if (target_state
>= p_throttling
->state_count
) {
256 "Exceed the limit of T-state \n");
257 target_state
= p_throttling
->state_count
- 1;
259 p_tstate
->target_state
= target_state
;
260 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "PreChange Event:"
261 "target T-state of CPU %d is T%d\n",
264 case THROTTLING_POSTCHANGE
:
266 * Postchange event is only used to update the
267 * T-state flag of acpi_processor_throttling.
269 p_throttling
->state
= target_state
;
270 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "PostChange Event:"
271 "CPU %d is switched to T%d\n",
276 "Unsupported Throttling notifier event\n");
284 * _TPC - Throttling Present Capabilities
286 static int acpi_processor_get_platform_limit(struct acpi_processor
*pr
)
288 acpi_status status
= 0;
289 unsigned long long tpc
= 0;
297 status
= acpi_evaluate_integer(pr
->handle
, "_TPC", NULL
, &tpc
);
298 if (ACPI_FAILURE(status
)) {
299 if (status
!= AE_NOT_FOUND
) {
300 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TPC"));
306 pr
->throttling_platform_limit
= (int)tpc
;
310 int acpi_processor_tstate_has_changed(struct acpi_processor
*pr
)
313 int throttling_limit
;
315 struct acpi_processor_limit
*limit
;
321 result
= acpi_processor_get_platform_limit(pr
);
323 /* Throttling Limit is unsupported */
327 throttling_limit
= pr
->throttling_platform_limit
;
328 if (throttling_limit
>= pr
->throttling
.state_count
) {
329 /* Uncorrect Throttling Limit */
333 current_state
= pr
->throttling
.state
;
334 if (current_state
> throttling_limit
) {
336 * The current state can meet the requirement of
337 * _TPC limit. But it is reasonable that OSPM changes
338 * t-states from high to low for better performance.
339 * Of course the limit condition of thermal
340 * and user should be considered.
343 target_state
= throttling_limit
;
344 if (limit
->thermal
.tx
> target_state
)
345 target_state
= limit
->thermal
.tx
;
346 if (limit
->user
.tx
> target_state
)
347 target_state
= limit
->user
.tx
;
348 } else if (current_state
== throttling_limit
) {
350 * Unnecessary to change the throttling state
355 * If the current state is lower than the limit of _TPC, it
356 * will be forced to switch to the throttling state defined
357 * by throttling_platfor_limit.
358 * Because the previous state meets with the limit condition
359 * of thermal and user, it is unnecessary to check it again.
361 target_state
= throttling_limit
;
363 return acpi_processor_set_throttling(pr
, target_state
, false);
367 * This function is used to reevaluate whether the T-state is valid
368 * after one CPU is onlined/offlined.
369 * It is noted that it won't reevaluate the following properties for
372 * 2. the number of supported T-state
375 void acpi_processor_reevaluate_tstate(struct acpi_processor
*pr
,
376 unsigned long action
)
380 if (action
== CPU_DEAD
) {
381 /* When one CPU is offline, the T-state throttling
382 * will be invalidated.
384 pr
->flags
.throttling
= 0;
387 /* the following is to recheck whether the T-state is valid for
390 if (!pr
->throttling
.state_count
) {
391 /* If the number of T-state is invalid, it is
394 pr
->flags
.throttling
= 0;
397 pr
->flags
.throttling
= 1;
399 /* Disable throttling (if enabled). We'll let subsequent
400 * policy (e.g.thermal) decide to lower performance if it
401 * so chooses, but for now we'll crank up the speed.
404 result
= acpi_processor_get_throttling(pr
);
408 if (pr
->throttling
.state
) {
409 result
= acpi_processor_set_throttling(pr
, 0, false);
416 pr
->flags
.throttling
= 0;
419 * _PTC - Processor Throttling Control (and status) register location
421 static int acpi_processor_get_throttling_control(struct acpi_processor
*pr
)
424 acpi_status status
= 0;
425 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
426 union acpi_object
*ptc
= NULL
;
427 union acpi_object obj
= { 0 };
428 struct acpi_processor_throttling
*throttling
;
430 status
= acpi_evaluate_object(pr
->handle
, "_PTC", NULL
, &buffer
);
431 if (ACPI_FAILURE(status
)) {
432 if (status
!= AE_NOT_FOUND
) {
433 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PTC"));
438 ptc
= (union acpi_object
*)buffer
.pointer
;
439 if (!ptc
|| (ptc
->type
!= ACPI_TYPE_PACKAGE
)
440 || (ptc
->package
.count
!= 2)) {
441 printk(KERN_ERR PREFIX
"Invalid _PTC data\n");
450 obj
= ptc
->package
.elements
[0];
452 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
453 || (obj
.buffer
.length
< sizeof(struct acpi_ptc_register
))
454 || (obj
.buffer
.pointer
== NULL
)) {
455 printk(KERN_ERR PREFIX
456 "Invalid _PTC data (control_register)\n");
460 memcpy(&pr
->throttling
.control_register
, obj
.buffer
.pointer
,
461 sizeof(struct acpi_ptc_register
));
467 obj
= ptc
->package
.elements
[1];
469 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
470 || (obj
.buffer
.length
< sizeof(struct acpi_ptc_register
))
471 || (obj
.buffer
.pointer
== NULL
)) {
472 printk(KERN_ERR PREFIX
"Invalid _PTC data (status_register)\n");
477 memcpy(&pr
->throttling
.status_register
, obj
.buffer
.pointer
,
478 sizeof(struct acpi_ptc_register
));
480 throttling
= &pr
->throttling
;
482 if ((throttling
->control_register
.bit_width
+
483 throttling
->control_register
.bit_offset
) > 32) {
484 printk(KERN_ERR PREFIX
"Invalid _PTC control register\n");
489 if ((throttling
->status_register
.bit_width
+
490 throttling
->status_register
.bit_offset
) > 32) {
491 printk(KERN_ERR PREFIX
"Invalid _PTC status register\n");
497 kfree(buffer
.pointer
);
503 * _TSS - Throttling Supported States
505 static int acpi_processor_get_throttling_states(struct acpi_processor
*pr
)
508 acpi_status status
= AE_OK
;
509 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
510 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
511 struct acpi_buffer state
= { 0, NULL
};
512 union acpi_object
*tss
= NULL
;
515 status
= acpi_evaluate_object(pr
->handle
, "_TSS", NULL
, &buffer
);
516 if (ACPI_FAILURE(status
)) {
517 if (status
!= AE_NOT_FOUND
) {
518 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TSS"));
523 tss
= buffer
.pointer
;
524 if (!tss
|| (tss
->type
!= ACPI_TYPE_PACKAGE
)) {
525 printk(KERN_ERR PREFIX
"Invalid _TSS data\n");
530 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d throttling states\n",
531 tss
->package
.count
));
533 pr
->throttling
.state_count
= tss
->package
.count
;
534 pr
->throttling
.states_tss
=
535 kmalloc(sizeof(struct acpi_processor_tx_tss
) * tss
->package
.count
,
537 if (!pr
->throttling
.states_tss
) {
542 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
544 struct acpi_processor_tx_tss
*tx
=
545 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
548 state
.length
= sizeof(struct acpi_processor_tx_tss
);
551 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Extracting state %d\n", i
));
553 status
= acpi_extract_package(&(tss
->package
.elements
[i
]),
555 if (ACPI_FAILURE(status
)) {
556 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _TSS data"));
558 kfree(pr
->throttling
.states_tss
);
562 if (!tx
->freqpercentage
) {
563 printk(KERN_ERR PREFIX
564 "Invalid _TSS data: freq is zero\n");
566 kfree(pr
->throttling
.states_tss
);
572 kfree(buffer
.pointer
);
578 * _TSD - T-State Dependencies
580 static int acpi_processor_get_tsd(struct acpi_processor
*pr
)
583 acpi_status status
= AE_OK
;
584 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
585 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
586 struct acpi_buffer state
= { 0, NULL
};
587 union acpi_object
*tsd
= NULL
;
588 struct acpi_tsd_package
*pdomain
;
589 struct acpi_processor_throttling
*pthrottling
;
591 pthrottling
= &pr
->throttling
;
592 pthrottling
->tsd_valid_flag
= 0;
594 status
= acpi_evaluate_object(pr
->handle
, "_TSD", NULL
, &buffer
);
595 if (ACPI_FAILURE(status
)) {
596 if (status
!= AE_NOT_FOUND
) {
597 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TSD"));
602 tsd
= buffer
.pointer
;
603 if (!tsd
|| (tsd
->type
!= ACPI_TYPE_PACKAGE
)) {
604 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
609 if (tsd
->package
.count
!= 1) {
610 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
615 pdomain
= &(pr
->throttling
.domain_info
);
617 state
.length
= sizeof(struct acpi_tsd_package
);
618 state
.pointer
= pdomain
;
620 status
= acpi_extract_package(&(tsd
->package
.elements
[0]),
622 if (ACPI_FAILURE(status
)) {
623 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
628 if (pdomain
->num_entries
!= ACPI_TSD_REV0_ENTRIES
) {
629 printk(KERN_ERR PREFIX
"Unknown _TSD:num_entries\n");
634 if (pdomain
->revision
!= ACPI_TSD_REV0_REVISION
) {
635 printk(KERN_ERR PREFIX
"Unknown _TSD:revision\n");
640 pthrottling
= &pr
->throttling
;
641 pthrottling
->tsd_valid_flag
= 1;
642 pthrottling
->shared_type
= pdomain
->coord_type
;
643 cpumask_set_cpu(pr
->id
, pthrottling
->shared_cpu_map
);
645 * If the coordination type is not defined in ACPI spec,
646 * the tsd_valid_flag will be clear and coordination type
647 * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
649 if (pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ALL
&&
650 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ANY
&&
651 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_HW_ALL
) {
652 pthrottling
->tsd_valid_flag
= 0;
653 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
657 kfree(buffer
.pointer
);
661 /* --------------------------------------------------------------------------
663 -------------------------------------------------------------------------- */
664 static int acpi_processor_get_throttling_fadt(struct acpi_processor
*pr
)
674 if (!pr
->flags
.throttling
)
677 pr
->throttling
.state
= 0;
679 duty_mask
= pr
->throttling
.state_count
- 1;
681 duty_mask
<<= pr
->throttling
.duty_offset
;
685 value
= inl(pr
->throttling
.address
);
688 * Compute the current throttling state when throttling is enabled
692 duty_value
= value
& duty_mask
;
693 duty_value
>>= pr
->throttling
.duty_offset
;
696 state
= pr
->throttling
.state_count
- duty_value
;
699 pr
->throttling
.state
= state
;
703 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
704 "Throttling state is T%d (%d%% throttling applied)\n",
705 state
, pr
->throttling
.states
[state
].performance
));
711 static int acpi_throttling_rdmsr(u64
*value
)
713 u64 msr_high
, msr_low
;
717 if ((this_cpu_read(cpu_info
.x86_vendor
) != X86_VENDOR_INTEL
) ||
718 !this_cpu_has(X86_FEATURE_ACPI
)) {
719 printk(KERN_ERR PREFIX
720 "HARDWARE addr space,NOT supported yet\n");
724 rdmsr_safe(MSR_IA32_THERM_CONTROL
,
725 (u32
*)&msr_low
, (u32
*) &msr_high
);
726 msr
= (msr_high
<< 32) | msr_low
;
733 static int acpi_throttling_wrmsr(u64 value
)
738 if ((this_cpu_read(cpu_info
.x86_vendor
) != X86_VENDOR_INTEL
) ||
739 !this_cpu_has(X86_FEATURE_ACPI
)) {
740 printk(KERN_ERR PREFIX
741 "HARDWARE addr space,NOT supported yet\n");
744 wrmsr_safe(MSR_IA32_THERM_CONTROL
,
745 msr
& 0xffffffff, msr
>> 32);
751 static int acpi_throttling_rdmsr(u64
*value
)
753 printk(KERN_ERR PREFIX
754 "HARDWARE addr space,NOT supported yet\n");
758 static int acpi_throttling_wrmsr(u64 value
)
760 printk(KERN_ERR PREFIX
761 "HARDWARE addr space,NOT supported yet\n");
766 static int acpi_read_throttling_status(struct acpi_processor
*pr
,
769 u32 bit_width
, bit_offset
;
772 struct acpi_processor_throttling
*throttling
;
775 throttling
= &pr
->throttling
;
776 switch (throttling
->status_register
.space_id
) {
777 case ACPI_ADR_SPACE_SYSTEM_IO
:
778 bit_width
= throttling
->status_register
.bit_width
;
779 bit_offset
= throttling
->status_register
.bit_offset
;
781 acpi_os_read_port((acpi_io_address
) throttling
->status_register
.
783 (u32
) (bit_width
+ bit_offset
));
784 ptc_mask
= (1 << bit_width
) - 1;
785 *value
= (u64
) ((ptc_value
>> bit_offset
) & ptc_mask
);
788 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
789 ret
= acpi_throttling_rdmsr(value
);
792 printk(KERN_ERR PREFIX
"Unknown addr space %d\n",
793 (u32
) (throttling
->status_register
.space_id
));
798 static int acpi_write_throttling_state(struct acpi_processor
*pr
,
801 u32 bit_width
, bit_offset
;
804 struct acpi_processor_throttling
*throttling
;
807 throttling
= &pr
->throttling
;
808 switch (throttling
->control_register
.space_id
) {
809 case ACPI_ADR_SPACE_SYSTEM_IO
:
810 bit_width
= throttling
->control_register
.bit_width
;
811 bit_offset
= throttling
->control_register
.bit_offset
;
812 ptc_mask
= (1 << bit_width
) - 1;
813 ptc_value
= value
& ptc_mask
;
815 acpi_os_write_port((acpi_io_address
) throttling
->
816 control_register
.address
,
817 (u32
) (ptc_value
<< bit_offset
),
818 (u32
) (bit_width
+ bit_offset
));
821 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
822 ret
= acpi_throttling_wrmsr(value
);
825 printk(KERN_ERR PREFIX
"Unknown addr space %d\n",
826 (u32
) (throttling
->control_register
.space_id
));
831 static int acpi_get_throttling_state(struct acpi_processor
*pr
,
836 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
837 struct acpi_processor_tx_tss
*tx
=
838 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
840 if (tx
->control
== value
)
846 static int acpi_get_throttling_value(struct acpi_processor
*pr
,
847 int state
, u64
*value
)
851 if (state
>= 0 && state
<= pr
->throttling
.state_count
) {
852 struct acpi_processor_tx_tss
*tx
=
853 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
855 *value
= tx
->control
;
861 static int acpi_processor_get_throttling_ptc(struct acpi_processor
*pr
)
870 if (!pr
->flags
.throttling
)
873 pr
->throttling
.state
= 0;
876 ret
= acpi_read_throttling_status(pr
, &value
);
878 state
= acpi_get_throttling_state(pr
, value
);
880 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
881 "Invalid throttling state, reset\n"));
883 ret
= acpi_processor_set_throttling(pr
, state
, true);
887 pr
->throttling
.state
= state
;
893 static int acpi_processor_get_throttling(struct acpi_processor
*pr
)
895 cpumask_var_t saved_mask
;
901 if (!pr
->flags
.throttling
)
904 if (!alloc_cpumask_var(&saved_mask
, GFP_KERNEL
))
908 * Migrate task to the cpu pointed by pr.
910 cpumask_copy(saved_mask
, ¤t
->cpus_allowed
);
911 /* FIXME: use work_on_cpu() */
912 if (set_cpus_allowed_ptr(current
, cpumask_of(pr
->id
))) {
913 /* Can't migrate to the target pr->id CPU. Exit */
914 free_cpumask_var(saved_mask
);
917 ret
= pr
->throttling
.acpi_processor_get_throttling(pr
);
918 /* restore the previous state */
919 set_cpus_allowed_ptr(current
, saved_mask
);
920 free_cpumask_var(saved_mask
);
925 static int acpi_processor_get_fadt_info(struct acpi_processor
*pr
)
929 if (!pr
->throttling
.address
) {
930 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No throttling register\n"));
932 } else if (!pr
->throttling
.duty_width
) {
933 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No throttling states\n"));
936 /* TBD: Support duty_cycle values that span bit 4. */
937 else if ((pr
->throttling
.duty_offset
+ pr
->throttling
.duty_width
) > 4) {
938 printk(KERN_WARNING PREFIX
"duty_cycle spans bit 4\n");
942 pr
->throttling
.state_count
= 1 << acpi_gbl_FADT
.duty_width
;
945 * Compute state values. Note that throttling displays a linear power
946 * performance relationship (at 50% performance the CPU will consume
947 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
950 step
= (1000 / pr
->throttling
.state_count
);
952 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
953 pr
->throttling
.states
[i
].performance
= 1000 - step
* i
;
954 pr
->throttling
.states
[i
].power
= 1000 - step
* i
;
959 static int acpi_processor_set_throttling_fadt(struct acpi_processor
*pr
,
960 int state
, bool force
)
969 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
972 if (!pr
->flags
.throttling
)
975 if (!force
&& (state
== pr
->throttling
.state
))
978 if (state
< pr
->throttling_platform_limit
)
981 * Calculate the duty_value and duty_mask.
984 duty_value
= pr
->throttling
.state_count
- state
;
986 duty_value
<<= pr
->throttling
.duty_offset
;
988 /* Used to clear all duty_value bits */
989 duty_mask
= pr
->throttling
.state_count
- 1;
991 duty_mask
<<= acpi_gbl_FADT
.duty_offset
;
992 duty_mask
= ~duty_mask
;
998 * Disable throttling by writing a 0 to bit 4. Note that we must
999 * turn it off before you can change the duty_value.
1001 value
= inl(pr
->throttling
.address
);
1003 value
&= 0xFFFFFFEF;
1004 outl(value
, pr
->throttling
.address
);
1008 * Write the new duty_value and then enable throttling. Note
1009 * that a state value of 0 leaves throttling disabled.
1013 value
|= duty_value
;
1014 outl(value
, pr
->throttling
.address
);
1016 value
|= 0x00000010;
1017 outl(value
, pr
->throttling
.address
);
1020 pr
->throttling
.state
= state
;
1024 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1025 "Throttling state set to T%d (%d%%)\n", state
,
1026 (pr
->throttling
.states
[state
].performance
? pr
->
1027 throttling
.states
[state
].performance
/ 10 : 0)));
1032 static int acpi_processor_set_throttling_ptc(struct acpi_processor
*pr
,
1033 int state
, bool force
)
1041 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
1044 if (!pr
->flags
.throttling
)
1047 if (!force
&& (state
== pr
->throttling
.state
))
1050 if (state
< pr
->throttling_platform_limit
)
1054 ret
= acpi_get_throttling_value(pr
, state
, &value
);
1056 acpi_write_throttling_state(pr
, value
);
1057 pr
->throttling
.state
= state
;
1063 int acpi_processor_set_throttling(struct acpi_processor
*pr
,
1064 int state
, bool force
)
1066 cpumask_var_t saved_mask
;
1069 struct acpi_processor
*match_pr
;
1070 struct acpi_processor_throttling
*p_throttling
;
1071 struct throttling_tstate t_state
;
1072 cpumask_var_t online_throttling_cpus
;
1077 if (!pr
->flags
.throttling
)
1080 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
1083 if (!alloc_cpumask_var(&saved_mask
, GFP_KERNEL
))
1086 if (!alloc_cpumask_var(&online_throttling_cpus
, GFP_KERNEL
)) {
1087 free_cpumask_var(saved_mask
);
1091 if (cpu_is_offline(pr
->id
)) {
1093 * the cpu pointed by pr->id is offline. Unnecessary to change
1094 * the throttling state any more.
1099 cpumask_copy(saved_mask
, ¤t
->cpus_allowed
);
1100 t_state
.target_state
= state
;
1101 p_throttling
= &(pr
->throttling
);
1102 cpumask_and(online_throttling_cpus
, cpu_online_mask
,
1103 p_throttling
->shared_cpu_map
);
1105 * The throttling notifier will be called for every
1106 * affected cpu in order to get one proper T-state.
1107 * The notifier event is THROTTLING_PRECHANGE.
1109 for_each_cpu(i
, online_throttling_cpus
) {
1111 acpi_processor_throttling_notifier(THROTTLING_PRECHANGE
,
1115 * The function of acpi_processor_set_throttling will be called
1116 * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
1117 * it is necessary to call it for every affected cpu. Otherwise
1118 * it can be called only for the cpu pointed by pr.
1120 if (p_throttling
->shared_type
== DOMAIN_COORD_TYPE_SW_ANY
) {
1121 /* FIXME: use work_on_cpu() */
1122 if (set_cpus_allowed_ptr(current
, cpumask_of(pr
->id
))) {
1123 /* Can't migrate to the pr->id CPU. Exit */
1127 ret
= p_throttling
->acpi_processor_set_throttling(pr
,
1128 t_state
.target_state
, force
);
1131 * When the T-state coordination is SW_ALL or HW_ALL,
1132 * it is necessary to set T-state for every affected
1135 for_each_cpu(i
, online_throttling_cpus
) {
1136 match_pr
= per_cpu(processors
, i
);
1138 * If the pointer is invalid, we will report the
1139 * error message and continue.
1142 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1143 "Invalid Pointer for CPU %d\n", i
));
1147 * If the throttling control is unsupported on CPU i,
1148 * we will report the error message and continue.
1150 if (!match_pr
->flags
.throttling
) {
1151 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1152 "Throttling Control is unsupported "
1157 /* FIXME: use work_on_cpu() */
1158 if (set_cpus_allowed_ptr(current
, cpumask_of(i
)))
1160 ret
= match_pr
->throttling
.
1161 acpi_processor_set_throttling(
1162 match_pr
, t_state
.target_state
, force
);
1166 * After the set_throttling is called, the
1167 * throttling notifier is called for every
1168 * affected cpu to update the T-states.
1169 * The notifier event is THROTTLING_POSTCHANGE
1171 for_each_cpu(i
, online_throttling_cpus
) {
1173 acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE
,
1176 /* restore the previous state */
1177 /* FIXME: use work_on_cpu() */
1178 set_cpus_allowed_ptr(current
, saved_mask
);
1180 free_cpumask_var(online_throttling_cpus
);
1181 free_cpumask_var(saved_mask
);
1185 int acpi_processor_get_throttling_info(struct acpi_processor
*pr
)
1188 struct acpi_processor_throttling
*pthrottling
;
1190 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1191 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1192 pr
->throttling
.address
,
1193 pr
->throttling
.duty_offset
,
1194 pr
->throttling
.duty_width
));
1197 * Evaluate _PTC, _TSS and _TPC
1198 * They must all be present or none of them can be used.
1200 if (acpi_processor_get_throttling_control(pr
) ||
1201 acpi_processor_get_throttling_states(pr
) ||
1202 acpi_processor_get_platform_limit(pr
))
1204 pr
->throttling
.acpi_processor_get_throttling
=
1205 &acpi_processor_get_throttling_fadt
;
1206 pr
->throttling
.acpi_processor_set_throttling
=
1207 &acpi_processor_set_throttling_fadt
;
1208 if (acpi_processor_get_fadt_info(pr
))
1211 pr
->throttling
.acpi_processor_get_throttling
=
1212 &acpi_processor_get_throttling_ptc
;
1213 pr
->throttling
.acpi_processor_set_throttling
=
1214 &acpi_processor_set_throttling_ptc
;
1218 * If TSD package for one CPU can't be parsed successfully, it means
1219 * that this CPU will have no coordination with other CPUs.
1221 if (acpi_processor_get_tsd(pr
)) {
1222 pthrottling
= &pr
->throttling
;
1223 pthrottling
->tsd_valid_flag
= 0;
1224 cpumask_set_cpu(pr
->id
, pthrottling
->shared_cpu_map
);
1225 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
1229 * PIIX4 Errata: We don't support throttling on the original PIIX4.
1230 * This shouldn't be an issue as few (if any) mobile systems ever
1233 if (errata
.piix4
.throttle
) {
1234 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1235 "Throttling not supported on PIIX4 A- or B-step\n"));
1239 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d throttling states\n",
1240 pr
->throttling
.state_count
));
1242 pr
->flags
.throttling
= 1;
1245 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
1246 * thermal) decide to lower performance if it so chooses, but for now
1247 * we'll crank up the speed.
1250 result
= acpi_processor_get_throttling(pr
);
1254 if (pr
->throttling
.state
) {
1255 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1256 "Disabling throttling (was T%d)\n",
1257 pr
->throttling
.state
));
1258 result
= acpi_processor_set_throttling(pr
, 0, false);
1265 pr
->flags
.throttling
= 0;