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
, int state
);
67 static int acpi_processor_update_tsd_coord(void)
69 int count
, count_target
;
72 cpumask_var_t covered_cpus
;
73 struct acpi_processor
*pr
, *match_pr
;
74 struct acpi_tsd_package
*pdomain
, *match_pdomain
;
75 struct acpi_processor_throttling
*pthrottling
, *match_pthrottling
;
77 if (!alloc_cpumask_var(&covered_cpus
, GFP_KERNEL
))
81 * Now that we have _TSD data from all CPUs, lets setup T-state
82 * coordination between all CPUs.
84 for_each_possible_cpu(i
) {
85 pr
= per_cpu(processors
, i
);
89 /* Basic validity check for domain info */
90 pthrottling
= &(pr
->throttling
);
93 * If tsd package for one cpu is invalid, the coordination
94 * among all CPUs is thought as invalid.
97 if (!pthrottling
->tsd_valid_flag
) {
105 cpumask_clear(covered_cpus
);
106 for_each_possible_cpu(i
) {
107 pr
= per_cpu(processors
, i
);
111 if (cpumask_test_cpu(i
, covered_cpus
))
113 pthrottling
= &pr
->throttling
;
115 pdomain
= &(pthrottling
->domain_info
);
116 cpumask_set_cpu(i
, pthrottling
->shared_cpu_map
);
117 cpumask_set_cpu(i
, covered_cpus
);
119 * If the number of processor in the TSD domain is 1, it is
120 * unnecessary to parse the coordination for this CPU.
122 if (pdomain
->num_processors
<= 1)
125 /* Validate the Domain info */
126 count_target
= pdomain
->num_processors
;
129 for_each_possible_cpu(j
) {
133 match_pr
= per_cpu(processors
, j
);
137 match_pthrottling
= &(match_pr
->throttling
);
138 match_pdomain
= &(match_pthrottling
->domain_info
);
139 if (match_pdomain
->domain
!= pdomain
->domain
)
142 /* Here i and j are in the same domain.
143 * If two TSD packages have the same domain, they
144 * should have the same num_porcessors and
145 * coordination type. Otherwise it will be regarded
148 if (match_pdomain
->num_processors
!= count_target
) {
153 if (pdomain
->coord_type
!= match_pdomain
->coord_type
) {
158 cpumask_set_cpu(j
, covered_cpus
);
159 cpumask_set_cpu(j
, pthrottling
->shared_cpu_map
);
162 for_each_possible_cpu(j
) {
166 match_pr
= per_cpu(processors
, j
);
170 match_pthrottling
= &(match_pr
->throttling
);
171 match_pdomain
= &(match_pthrottling
->domain_info
);
172 if (match_pdomain
->domain
!= pdomain
->domain
)
176 * If some CPUS have the same domain, they
177 * will have the same shared_cpu_map.
179 cpumask_copy(match_pthrottling
->shared_cpu_map
,
180 pthrottling
->shared_cpu_map
);
185 free_cpumask_var(covered_cpus
);
187 for_each_possible_cpu(i
) {
188 pr
= per_cpu(processors
, i
);
193 * Assume no coordination on any error parsing domain info.
194 * The coordination type will be forced as SW_ALL.
197 pthrottling
= &(pr
->throttling
);
198 cpumask_clear(pthrottling
->shared_cpu_map
);
199 cpumask_set_cpu(i
, pthrottling
->shared_cpu_map
);
200 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
208 * Update the T-state coordination after the _TSD
209 * data for all cpus is obtained.
211 void acpi_processor_throttling_init(void)
213 if (acpi_processor_update_tsd_coord())
214 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
215 "Assume no T-state coordination\n"));
220 static int acpi_processor_throttling_notifier(unsigned long event
, void *data
)
222 struct throttling_tstate
*p_tstate
= data
;
223 struct acpi_processor
*pr
;
226 struct acpi_processor_limit
*p_limit
;
227 struct acpi_processor_throttling
*p_throttling
;
230 pr
= per_cpu(processors
, cpu
);
232 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Invalid pr pointer\n"));
235 if (!pr
->flags
.throttling
) {
236 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Throttling control is "
237 "unsupported on CPU %d\n", cpu
));
240 target_state
= p_tstate
->target_state
;
241 p_throttling
= &(pr
->throttling
);
243 case THROTTLING_PRECHANGE
:
245 * Prechange event is used to choose one proper t-state,
246 * which meets the limits of thermal, user and _TPC.
248 p_limit
= &pr
->limit
;
249 if (p_limit
->thermal
.tx
> target_state
)
250 target_state
= p_limit
->thermal
.tx
;
251 if (p_limit
->user
.tx
> target_state
)
252 target_state
= p_limit
->user
.tx
;
253 if (pr
->throttling_platform_limit
> target_state
)
254 target_state
= pr
->throttling_platform_limit
;
255 if (target_state
>= p_throttling
->state_count
) {
257 "Exceed the limit of T-state \n");
258 target_state
= p_throttling
->state_count
- 1;
260 p_tstate
->target_state
= target_state
;
261 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "PreChange Event:"
262 "target T-state of CPU %d is T%d\n",
265 case THROTTLING_POSTCHANGE
:
267 * Postchange event is only used to update the
268 * T-state flag of acpi_processor_throttling.
270 p_throttling
->state
= target_state
;
271 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "PostChange Event:"
272 "CPU %d is switched to T%d\n",
277 "Unsupported Throttling notifier event\n");
285 * _TPC - Throttling Present Capabilities
287 static int acpi_processor_get_platform_limit(struct acpi_processor
*pr
)
289 acpi_status status
= 0;
290 unsigned long long tpc
= 0;
298 status
= acpi_evaluate_integer(pr
->handle
, "_TPC", NULL
, &tpc
);
299 if (ACPI_FAILURE(status
)) {
300 if (status
!= AE_NOT_FOUND
) {
301 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TPC"));
307 pr
->throttling_platform_limit
= (int)tpc
;
311 int acpi_processor_tstate_has_changed(struct acpi_processor
*pr
)
314 int throttling_limit
;
316 struct acpi_processor_limit
*limit
;
322 result
= acpi_processor_get_platform_limit(pr
);
324 /* Throttling Limit is unsupported */
328 throttling_limit
= pr
->throttling_platform_limit
;
329 if (throttling_limit
>= pr
->throttling
.state_count
) {
330 /* Uncorrect Throttling Limit */
334 current_state
= pr
->throttling
.state
;
335 if (current_state
> throttling_limit
) {
337 * The current state can meet the requirement of
338 * _TPC limit. But it is reasonable that OSPM changes
339 * t-states from high to low for better performance.
340 * Of course the limit condition of thermal
341 * and user should be considered.
344 target_state
= throttling_limit
;
345 if (limit
->thermal
.tx
> target_state
)
346 target_state
= limit
->thermal
.tx
;
347 if (limit
->user
.tx
> target_state
)
348 target_state
= limit
->user
.tx
;
349 } else if (current_state
== throttling_limit
) {
351 * Unnecessary to change the throttling state
356 * If the current state is lower than the limit of _TPC, it
357 * will be forced to switch to the throttling state defined
358 * by throttling_platfor_limit.
359 * Because the previous state meets with the limit condition
360 * of thermal and user, it is unnecessary to check it again.
362 target_state
= throttling_limit
;
364 return acpi_processor_set_throttling(pr
, target_state
);
368 * _PTC - Processor Throttling Control (and status) register location
370 static int acpi_processor_get_throttling_control(struct acpi_processor
*pr
)
373 acpi_status status
= 0;
374 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
375 union acpi_object
*ptc
= NULL
;
376 union acpi_object obj
= { 0 };
377 struct acpi_processor_throttling
*throttling
;
379 status
= acpi_evaluate_object(pr
->handle
, "_PTC", NULL
, &buffer
);
380 if (ACPI_FAILURE(status
)) {
381 if (status
!= AE_NOT_FOUND
) {
382 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _PTC"));
387 ptc
= (union acpi_object
*)buffer
.pointer
;
388 if (!ptc
|| (ptc
->type
!= ACPI_TYPE_PACKAGE
)
389 || (ptc
->package
.count
!= 2)) {
390 printk(KERN_ERR PREFIX
"Invalid _PTC data\n");
399 obj
= ptc
->package
.elements
[0];
401 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
402 || (obj
.buffer
.length
< sizeof(struct acpi_ptc_register
))
403 || (obj
.buffer
.pointer
== NULL
)) {
404 printk(KERN_ERR PREFIX
405 "Invalid _PTC data (control_register)\n");
409 memcpy(&pr
->throttling
.control_register
, obj
.buffer
.pointer
,
410 sizeof(struct acpi_ptc_register
));
416 obj
= ptc
->package
.elements
[1];
418 if ((obj
.type
!= ACPI_TYPE_BUFFER
)
419 || (obj
.buffer
.length
< sizeof(struct acpi_ptc_register
))
420 || (obj
.buffer
.pointer
== NULL
)) {
421 printk(KERN_ERR PREFIX
"Invalid _PTC data (status_register)\n");
426 memcpy(&pr
->throttling
.status_register
, obj
.buffer
.pointer
,
427 sizeof(struct acpi_ptc_register
));
429 throttling
= &pr
->throttling
;
431 if ((throttling
->control_register
.bit_width
+
432 throttling
->control_register
.bit_offset
) > 32) {
433 printk(KERN_ERR PREFIX
"Invalid _PTC control register\n");
438 if ((throttling
->status_register
.bit_width
+
439 throttling
->status_register
.bit_offset
) > 32) {
440 printk(KERN_ERR PREFIX
"Invalid _PTC status register\n");
446 kfree(buffer
.pointer
);
452 * _TSS - Throttling Supported States
454 static int acpi_processor_get_throttling_states(struct acpi_processor
*pr
)
457 acpi_status status
= AE_OK
;
458 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
459 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
460 struct acpi_buffer state
= { 0, NULL
};
461 union acpi_object
*tss
= NULL
;
464 status
= acpi_evaluate_object(pr
->handle
, "_TSS", NULL
, &buffer
);
465 if (ACPI_FAILURE(status
)) {
466 if (status
!= AE_NOT_FOUND
) {
467 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TSS"));
472 tss
= buffer
.pointer
;
473 if (!tss
|| (tss
->type
!= ACPI_TYPE_PACKAGE
)) {
474 printk(KERN_ERR PREFIX
"Invalid _TSS data\n");
479 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d throttling states\n",
480 tss
->package
.count
));
482 pr
->throttling
.state_count
= tss
->package
.count
;
483 pr
->throttling
.states_tss
=
484 kmalloc(sizeof(struct acpi_processor_tx_tss
) * tss
->package
.count
,
486 if (!pr
->throttling
.states_tss
) {
491 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
493 struct acpi_processor_tx_tss
*tx
=
494 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
497 state
.length
= sizeof(struct acpi_processor_tx_tss
);
500 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Extracting state %d\n", i
));
502 status
= acpi_extract_package(&(tss
->package
.elements
[i
]),
504 if (ACPI_FAILURE(status
)) {
505 ACPI_EXCEPTION((AE_INFO
, status
, "Invalid _TSS data"));
507 kfree(pr
->throttling
.states_tss
);
511 if (!tx
->freqpercentage
) {
512 printk(KERN_ERR PREFIX
513 "Invalid _TSS data: freq is zero\n");
515 kfree(pr
->throttling
.states_tss
);
521 kfree(buffer
.pointer
);
527 * _TSD - T-State Dependencies
529 static int acpi_processor_get_tsd(struct acpi_processor
*pr
)
532 acpi_status status
= AE_OK
;
533 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
534 struct acpi_buffer format
= { sizeof("NNNNN"), "NNNNN" };
535 struct acpi_buffer state
= { 0, NULL
};
536 union acpi_object
*tsd
= NULL
;
537 struct acpi_tsd_package
*pdomain
;
538 struct acpi_processor_throttling
*pthrottling
;
540 pthrottling
= &pr
->throttling
;
541 pthrottling
->tsd_valid_flag
= 0;
543 status
= acpi_evaluate_object(pr
->handle
, "_TSD", NULL
, &buffer
);
544 if (ACPI_FAILURE(status
)) {
545 if (status
!= AE_NOT_FOUND
) {
546 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _TSD"));
551 tsd
= buffer
.pointer
;
552 if (!tsd
|| (tsd
->type
!= ACPI_TYPE_PACKAGE
)) {
553 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
558 if (tsd
->package
.count
!= 1) {
559 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
564 pdomain
= &(pr
->throttling
.domain_info
);
566 state
.length
= sizeof(struct acpi_tsd_package
);
567 state
.pointer
= pdomain
;
569 status
= acpi_extract_package(&(tsd
->package
.elements
[0]),
571 if (ACPI_FAILURE(status
)) {
572 printk(KERN_ERR PREFIX
"Invalid _TSD data\n");
577 if (pdomain
->num_entries
!= ACPI_TSD_REV0_ENTRIES
) {
578 printk(KERN_ERR PREFIX
"Unknown _TSD:num_entries\n");
583 if (pdomain
->revision
!= ACPI_TSD_REV0_REVISION
) {
584 printk(KERN_ERR PREFIX
"Unknown _TSD:revision\n");
589 pthrottling
= &pr
->throttling
;
590 pthrottling
->tsd_valid_flag
= 1;
591 pthrottling
->shared_type
= pdomain
->coord_type
;
592 cpumask_set_cpu(pr
->id
, pthrottling
->shared_cpu_map
);
594 * If the coordination type is not defined in ACPI spec,
595 * the tsd_valid_flag will be clear and coordination type
596 * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
598 if (pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ALL
&&
599 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_SW_ANY
&&
600 pdomain
->coord_type
!= DOMAIN_COORD_TYPE_HW_ALL
) {
601 pthrottling
->tsd_valid_flag
= 0;
602 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
606 kfree(buffer
.pointer
);
610 /* --------------------------------------------------------------------------
612 -------------------------------------------------------------------------- */
613 static int acpi_processor_get_throttling_fadt(struct acpi_processor
*pr
)
623 if (!pr
->flags
.throttling
)
626 pr
->throttling
.state
= 0;
628 duty_mask
= pr
->throttling
.state_count
- 1;
630 duty_mask
<<= pr
->throttling
.duty_offset
;
634 value
= inl(pr
->throttling
.address
);
637 * Compute the current throttling state when throttling is enabled
641 duty_value
= value
& duty_mask
;
642 duty_value
>>= pr
->throttling
.duty_offset
;
645 state
= pr
->throttling
.state_count
- duty_value
;
648 pr
->throttling
.state
= state
;
652 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
653 "Throttling state is T%d (%d%% throttling applied)\n",
654 state
, pr
->throttling
.states
[state
].performance
));
660 static int acpi_throttling_rdmsr(struct acpi_processor
*pr
,
661 acpi_integer
* value
)
663 struct cpuinfo_x86
*c
;
664 u64 msr_high
, msr_low
;
672 if ((c
->x86_vendor
!= X86_VENDOR_INTEL
) ||
673 !cpu_has(c
, X86_FEATURE_ACPI
)) {
674 printk(KERN_ERR PREFIX
675 "HARDWARE addr space,NOT supported yet\n");
679 rdmsr_safe(MSR_IA32_THERM_CONTROL
,
680 (u32
*)&msr_low
, (u32
*) &msr_high
);
681 msr
= (msr_high
<< 32) | msr_low
;
682 *value
= (acpi_integer
) msr
;
688 static int acpi_throttling_wrmsr(struct acpi_processor
*pr
, acpi_integer value
)
690 struct cpuinfo_x86
*c
;
698 if ((c
->x86_vendor
!= X86_VENDOR_INTEL
) ||
699 !cpu_has(c
, X86_FEATURE_ACPI
)) {
700 printk(KERN_ERR PREFIX
701 "HARDWARE addr space,NOT supported yet\n");
704 wrmsr_safe(MSR_IA32_THERM_CONTROL
,
705 msr
& 0xffffffff, msr
>> 32);
711 static int acpi_throttling_rdmsr(struct acpi_processor
*pr
,
712 acpi_integer
* value
)
714 printk(KERN_ERR PREFIX
715 "HARDWARE addr space,NOT supported yet\n");
719 static int acpi_throttling_wrmsr(struct acpi_processor
*pr
, acpi_integer value
)
721 printk(KERN_ERR PREFIX
722 "HARDWARE addr space,NOT supported yet\n");
727 static int acpi_read_throttling_status(struct acpi_processor
*pr
,
730 u32 bit_width
, bit_offset
;
733 struct acpi_processor_throttling
*throttling
;
736 throttling
= &pr
->throttling
;
737 switch (throttling
->status_register
.space_id
) {
738 case ACPI_ADR_SPACE_SYSTEM_IO
:
740 bit_width
= throttling
->status_register
.bit_width
;
741 bit_offset
= throttling
->status_register
.bit_offset
;
743 acpi_os_read_port((acpi_io_address
) throttling
->status_register
.
744 address
, (u32
*) &ptc_value
,
745 (u32
) (bit_width
+ bit_offset
));
746 ptc_mask
= (1 << bit_width
) - 1;
747 *value
= (acpi_integer
) ((ptc_value
>> bit_offset
) & ptc_mask
);
750 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
751 ret
= acpi_throttling_rdmsr(pr
, value
);
754 printk(KERN_ERR PREFIX
"Unknown addr space %d\n",
755 (u32
) (throttling
->status_register
.space_id
));
760 static int acpi_write_throttling_state(struct acpi_processor
*pr
,
763 u32 bit_width
, bit_offset
;
766 struct acpi_processor_throttling
*throttling
;
769 throttling
= &pr
->throttling
;
770 switch (throttling
->control_register
.space_id
) {
771 case ACPI_ADR_SPACE_SYSTEM_IO
:
772 bit_width
= throttling
->control_register
.bit_width
;
773 bit_offset
= throttling
->control_register
.bit_offset
;
774 ptc_mask
= (1 << bit_width
) - 1;
775 ptc_value
= value
& ptc_mask
;
777 acpi_os_write_port((acpi_io_address
) throttling
->
778 control_register
.address
,
779 (u32
) (ptc_value
<< bit_offset
),
780 (u32
) (bit_width
+ bit_offset
));
783 case ACPI_ADR_SPACE_FIXED_HARDWARE
:
784 ret
= acpi_throttling_wrmsr(pr
, value
);
787 printk(KERN_ERR PREFIX
"Unknown addr space %d\n",
788 (u32
) (throttling
->control_register
.space_id
));
793 static int acpi_get_throttling_state(struct acpi_processor
*pr
,
798 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
799 struct acpi_processor_tx_tss
*tx
=
800 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
802 if (tx
->control
== value
)
808 static int acpi_get_throttling_value(struct acpi_processor
*pr
,
809 int state
, acpi_integer
*value
)
813 if (state
>= 0 && state
<= pr
->throttling
.state_count
) {
814 struct acpi_processor_tx_tss
*tx
=
815 (struct acpi_processor_tx_tss
*)&(pr
->throttling
.
817 *value
= tx
->control
;
823 static int acpi_processor_get_throttling_ptc(struct acpi_processor
*pr
)
832 if (!pr
->flags
.throttling
)
835 pr
->throttling
.state
= 0;
838 ret
= acpi_read_throttling_status(pr
, &value
);
840 state
= acpi_get_throttling_state(pr
, value
);
841 pr
->throttling
.state
= state
;
847 static int acpi_processor_get_throttling(struct acpi_processor
*pr
)
849 cpumask_var_t saved_mask
;
855 if (!pr
->flags
.throttling
)
858 if (!alloc_cpumask_var(&saved_mask
, GFP_KERNEL
))
862 * Migrate task to the cpu pointed by pr.
864 cpumask_copy(saved_mask
, ¤t
->cpus_allowed
);
865 /* FIXME: use work_on_cpu() */
866 set_cpus_allowed_ptr(current
, cpumask_of(pr
->id
));
867 ret
= pr
->throttling
.acpi_processor_get_throttling(pr
);
868 /* restore the previous state */
869 set_cpus_allowed_ptr(current
, saved_mask
);
870 free_cpumask_var(saved_mask
);
875 static int acpi_processor_get_fadt_info(struct acpi_processor
*pr
)
879 if (!pr
->throttling
.address
) {
880 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No throttling register\n"));
882 } else if (!pr
->throttling
.duty_width
) {
883 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "No throttling states\n"));
886 /* TBD: Support duty_cycle values that span bit 4. */
887 else if ((pr
->throttling
.duty_offset
+ pr
->throttling
.duty_width
) > 4) {
888 printk(KERN_WARNING PREFIX
"duty_cycle spans bit 4\n");
892 pr
->throttling
.state_count
= 1 << acpi_gbl_FADT
.duty_width
;
895 * Compute state values. Note that throttling displays a linear power
896 * performance relationship (at 50% performance the CPU will consume
897 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
900 step
= (1000 / pr
->throttling
.state_count
);
902 for (i
= 0; i
< pr
->throttling
.state_count
; i
++) {
903 pr
->throttling
.states
[i
].performance
= 1000 - step
* i
;
904 pr
->throttling
.states
[i
].power
= 1000 - step
* i
;
909 static int acpi_processor_set_throttling_fadt(struct acpi_processor
*pr
,
919 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
922 if (!pr
->flags
.throttling
)
925 if (state
== pr
->throttling
.state
)
928 if (state
< pr
->throttling_platform_limit
)
931 * Calculate the duty_value and duty_mask.
934 duty_value
= pr
->throttling
.state_count
- state
;
936 duty_value
<<= pr
->throttling
.duty_offset
;
938 /* Used to clear all duty_value bits */
939 duty_mask
= pr
->throttling
.state_count
- 1;
941 duty_mask
<<= acpi_gbl_FADT
.duty_offset
;
942 duty_mask
= ~duty_mask
;
948 * Disable throttling by writing a 0 to bit 4. Note that we must
949 * turn it off before you can change the duty_value.
951 value
= inl(pr
->throttling
.address
);
954 outl(value
, pr
->throttling
.address
);
958 * Write the new duty_value and then enable throttling. Note
959 * that a state value of 0 leaves throttling disabled.
964 outl(value
, pr
->throttling
.address
);
967 outl(value
, pr
->throttling
.address
);
970 pr
->throttling
.state
= state
;
974 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
975 "Throttling state set to T%d (%d%%)\n", state
,
976 (pr
->throttling
.states
[state
].performance
? pr
->
977 throttling
.states
[state
].performance
/ 10 : 0)));
982 static int acpi_processor_set_throttling_ptc(struct acpi_processor
*pr
,
991 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
994 if (!pr
->flags
.throttling
)
997 if (state
== pr
->throttling
.state
)
1000 if (state
< pr
->throttling_platform_limit
)
1004 ret
= acpi_get_throttling_value(pr
, state
, &value
);
1006 acpi_write_throttling_state(pr
, value
);
1007 pr
->throttling
.state
= state
;
1013 int acpi_processor_set_throttling(struct acpi_processor
*pr
, int state
)
1015 cpumask_var_t saved_mask
;
1018 struct acpi_processor
*match_pr
;
1019 struct acpi_processor_throttling
*p_throttling
;
1020 struct throttling_tstate t_state
;
1021 cpumask_var_t online_throttling_cpus
;
1026 if (!pr
->flags
.throttling
)
1029 if ((state
< 0) || (state
> (pr
->throttling
.state_count
- 1)))
1032 if (!alloc_cpumask_var(&saved_mask
, GFP_KERNEL
))
1035 if (!alloc_cpumask_var(&online_throttling_cpus
, GFP_KERNEL
)) {
1036 free_cpumask_var(saved_mask
);
1040 cpumask_copy(saved_mask
, ¤t
->cpus_allowed
);
1041 t_state
.target_state
= state
;
1042 p_throttling
= &(pr
->throttling
);
1043 cpumask_and(online_throttling_cpus
, cpu_online_mask
,
1044 p_throttling
->shared_cpu_map
);
1046 * The throttling notifier will be called for every
1047 * affected cpu in order to get one proper T-state.
1048 * The notifier event is THROTTLING_PRECHANGE.
1050 for_each_cpu(i
, online_throttling_cpus
) {
1052 acpi_processor_throttling_notifier(THROTTLING_PRECHANGE
,
1056 * The function of acpi_processor_set_throttling will be called
1057 * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
1058 * it is necessary to call it for every affected cpu. Otherwise
1059 * it can be called only for the cpu pointed by pr.
1061 if (p_throttling
->shared_type
== DOMAIN_COORD_TYPE_SW_ANY
) {
1062 /* FIXME: use work_on_cpu() */
1063 set_cpus_allowed_ptr(current
, cpumask_of(pr
->id
));
1064 ret
= p_throttling
->acpi_processor_set_throttling(pr
,
1065 t_state
.target_state
);
1068 * When the T-state coordination is SW_ALL or HW_ALL,
1069 * it is necessary to set T-state for every affected
1072 for_each_cpu(i
, online_throttling_cpus
) {
1073 match_pr
= per_cpu(processors
, i
);
1075 * If the pointer is invalid, we will report the
1076 * error message and continue.
1079 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1080 "Invalid Pointer for CPU %d\n", i
));
1084 * If the throttling control is unsupported on CPU i,
1085 * we will report the error message and continue.
1087 if (!match_pr
->flags
.throttling
) {
1088 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1089 "Throttling Controll is unsupported "
1094 /* FIXME: use work_on_cpu() */
1095 set_cpus_allowed_ptr(current
, cpumask_of(i
));
1096 ret
= match_pr
->throttling
.
1097 acpi_processor_set_throttling(
1098 match_pr
, t_state
.target_state
);
1102 * After the set_throttling is called, the
1103 * throttling notifier is called for every
1104 * affected cpu to update the T-states.
1105 * The notifier event is THROTTLING_POSTCHANGE
1107 for_each_cpu(i
, online_throttling_cpus
) {
1109 acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE
,
1112 /* restore the previous state */
1113 /* FIXME: use work_on_cpu() */
1114 set_cpus_allowed_ptr(current
, saved_mask
);
1115 free_cpumask_var(online_throttling_cpus
);
1116 free_cpumask_var(saved_mask
);
1120 int acpi_processor_get_throttling_info(struct acpi_processor
*pr
)
1123 struct acpi_processor_throttling
*pthrottling
;
1125 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1126 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1127 pr
->throttling
.address
,
1128 pr
->throttling
.duty_offset
,
1129 pr
->throttling
.duty_width
));
1135 * Evaluate _PTC, _TSS and _TPC
1136 * They must all be present or none of them can be used.
1138 if (acpi_processor_get_throttling_control(pr
) ||
1139 acpi_processor_get_throttling_states(pr
) ||
1140 acpi_processor_get_platform_limit(pr
))
1142 pr
->throttling
.acpi_processor_get_throttling
=
1143 &acpi_processor_get_throttling_fadt
;
1144 pr
->throttling
.acpi_processor_set_throttling
=
1145 &acpi_processor_set_throttling_fadt
;
1146 if (acpi_processor_get_fadt_info(pr
))
1149 pr
->throttling
.acpi_processor_get_throttling
=
1150 &acpi_processor_get_throttling_ptc
;
1151 pr
->throttling
.acpi_processor_set_throttling
=
1152 &acpi_processor_set_throttling_ptc
;
1156 * If TSD package for one CPU can't be parsed successfully, it means
1157 * that this CPU will have no coordination with other CPUs.
1159 if (acpi_processor_get_tsd(pr
)) {
1160 pthrottling
= &pr
->throttling
;
1161 pthrottling
->tsd_valid_flag
= 0;
1162 cpumask_set_cpu(pr
->id
, pthrottling
->shared_cpu_map
);
1163 pthrottling
->shared_type
= DOMAIN_COORD_TYPE_SW_ALL
;
1167 * PIIX4 Errata: We don't support throttling on the original PIIX4.
1168 * This shouldn't be an issue as few (if any) mobile systems ever
1171 if (errata
.piix4
.throttle
) {
1172 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1173 "Throttling not supported on PIIX4 A- or B-step\n"));
1177 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Found %d throttling states\n",
1178 pr
->throttling
.state_count
));
1180 pr
->flags
.throttling
= 1;
1183 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
1184 * thermal) decide to lower performance if it so chooses, but for now
1185 * we'll crank up the speed.
1188 result
= acpi_processor_get_throttling(pr
);
1192 if (pr
->throttling
.state
) {
1193 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
1194 "Disabling throttling (was T%d)\n",
1195 pr
->throttling
.state
));
1196 result
= acpi_processor_set_throttling(pr
, 0);
1203 pr
->flags
.throttling
= 0;
1208 /* proc interface */
1210 static int acpi_processor_throttling_seq_show(struct seq_file
*seq
,
1213 struct acpi_processor
*pr
= seq
->private;
1220 if (!(pr
->throttling
.state_count
> 0)) {
1221 seq_puts(seq
, "<not supported>\n");
1225 result
= acpi_processor_get_throttling(pr
);
1229 "Could not determine current throttling state.\n");
1233 seq_printf(seq
, "state count: %d\n"
1234 "active state: T%d\n"
1235 "state available: T%d to T%d\n",
1236 pr
->throttling
.state_count
, pr
->throttling
.state
,
1237 pr
->throttling_platform_limit
,
1238 pr
->throttling
.state_count
- 1);
1240 seq_puts(seq
, "states:\n");
1241 if (pr
->throttling
.acpi_processor_get_throttling
==
1242 acpi_processor_get_throttling_fadt
) {
1243 for (i
= 0; i
< pr
->throttling
.state_count
; i
++)
1244 seq_printf(seq
, " %cT%d: %02d%%\n",
1245 (i
== pr
->throttling
.state
? '*' : ' '), i
,
1246 (pr
->throttling
.states
[i
].performance
? pr
->
1247 throttling
.states
[i
].performance
/ 10 : 0));
1249 for (i
= 0; i
< pr
->throttling
.state_count
; i
++)
1250 seq_printf(seq
, " %cT%d: %02d%%\n",
1251 (i
== pr
->throttling
.state
? '*' : ' '), i
,
1252 (int)pr
->throttling
.states_tss
[i
].
1260 static int acpi_processor_throttling_open_fs(struct inode
*inode
,
1263 return single_open(file
, acpi_processor_throttling_seq_show
,
1267 static ssize_t
acpi_processor_write_throttling(struct file
*file
,
1268 const char __user
* buffer
,
1269 size_t count
, loff_t
* data
)
1272 struct seq_file
*m
= file
->private_data
;
1273 struct acpi_processor
*pr
= m
->private;
1274 char state_string
[5] = "";
1276 size_t state_val
= 0;
1277 char tmpbuf
[5] = "";
1279 if (!pr
|| (count
> sizeof(state_string
) - 1))
1282 if (copy_from_user(state_string
, buffer
, count
))
1285 state_string
[count
] = '\0';
1286 if ((count
> 0) && (state_string
[count
-1] == '\n'))
1287 state_string
[count
-1] = '\0';
1289 charp
= state_string
;
1290 if ((state_string
[0] == 't') || (state_string
[0] == 'T'))
1293 state_val
= simple_strtoul(charp
, NULL
, 0);
1294 if (state_val
>= pr
->throttling
.state_count
)
1297 snprintf(tmpbuf
, 5, "%zu", state_val
);
1299 if (strcmp(tmpbuf
, charp
) != 0)
1302 result
= acpi_processor_set_throttling(pr
, state_val
);
1309 const struct file_operations acpi_processor_throttling_fops
= {
1310 .owner
= THIS_MODULE
,
1311 .open
= acpi_processor_throttling_open_fs
,
1313 .write
= acpi_processor_write_throttling
,
1314 .llseek
= seq_lseek
,
1315 .release
= single_release
,