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/processor.h>
43 #define ACPI_PROCESSOR_COMPONENT 0x01000000
44 #define ACPI_PROCESSOR_CLASS "processor"
45 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
46 ACPI_MODULE_NAME("processor_throttling");
48 struct throttling_tstate {
49 unsigned int cpu; /* cpu nr */
50 int target_state; /* target T-state */
53 #define THROTTLING_PRECHANGE (1)
54 #define THROTTLING_POSTCHANGE (2)
56 static int acpi_processor_get_throttling(struct acpi_processor *pr);
57 int acpi_processor_set_throttling(struct acpi_processor *pr, int state);
59 static int acpi_processor_update_tsd_coord(void)
61 int count, count_target;
64 cpumask_t covered_cpus;
65 struct acpi_processor *pr, *match_pr;
66 struct acpi_tsd_package *pdomain, *match_pdomain;
67 struct acpi_processor_throttling *pthrottling, *match_pthrottling;
70 * Now that we have _TSD data from all CPUs, lets setup T-state
71 * coordination between all CPUs.
73 for_each_possible_cpu(i) {
74 pr = per_cpu(processors, i);
78 /* Basic validity check for domain info */
79 pthrottling = &(pr->throttling);
82 * If tsd package for one cpu is invalid, the coordination
83 * among all CPUs is thought as invalid.
86 if (!pthrottling->tsd_valid_flag) {
94 cpus_clear(covered_cpus);
95 for_each_possible_cpu(i) {
96 pr = per_cpu(processors, i);
100 if (cpu_isset(i, covered_cpus))
102 pthrottling = &pr->throttling;
104 pdomain = &(pthrottling->domain_info);
105 cpu_set(i, pthrottling->shared_cpu_map);
106 cpu_set(i, covered_cpus);
108 * If the number of processor in the TSD domain is 1, it is
109 * unnecessary to parse the coordination for this CPU.
111 if (pdomain->num_processors <= 1)
114 /* Validate the Domain info */
115 count_target = pdomain->num_processors;
118 for_each_possible_cpu(j) {
122 match_pr = per_cpu(processors, j);
126 match_pthrottling = &(match_pr->throttling);
127 match_pdomain = &(match_pthrottling->domain_info);
128 if (match_pdomain->domain != pdomain->domain)
131 /* Here i and j are in the same domain.
132 * If two TSD packages have the same domain, they
133 * should have the same num_porcessors and
134 * coordination type. Otherwise it will be regarded
137 if (match_pdomain->num_processors != count_target) {
142 if (pdomain->coord_type != match_pdomain->coord_type) {
147 cpu_set(j, covered_cpus);
148 cpu_set(j, pthrottling->shared_cpu_map);
151 for_each_possible_cpu(j) {
155 match_pr = per_cpu(processors, j);
159 match_pthrottling = &(match_pr->throttling);
160 match_pdomain = &(match_pthrottling->domain_info);
161 if (match_pdomain->domain != pdomain->domain)
165 * If some CPUS have the same domain, they
166 * will have the same shared_cpu_map.
168 match_pthrottling->shared_cpu_map =
169 pthrottling->shared_cpu_map;
174 for_each_possible_cpu(i) {
175 pr = per_cpu(processors, i);
180 * Assume no coordination on any error parsing domain info.
181 * The coordination type will be forced as SW_ALL.
184 pthrottling = &(pr->throttling);
185 cpus_clear(pthrottling->shared_cpu_map);
186 cpu_set(i, pthrottling->shared_cpu_map);
187 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
195 * Update the T-state coordination after the _TSD
196 * data for all cpus is obtained.
198 void acpi_processor_throttling_init(void)
200 if (acpi_processor_update_tsd_coord())
201 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
202 "Assume no T-state coordination\n"));
207 static int acpi_processor_throttling_notifier(unsigned long event, void *data)
209 struct throttling_tstate *p_tstate = data;
210 struct acpi_processor *pr;
213 struct acpi_processor_limit *p_limit;
214 struct acpi_processor_throttling *p_throttling;
217 pr = per_cpu(processors, cpu);
219 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Invalid pr pointer\n"));
222 if (!pr->flags.throttling) {
223 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Throttling control is "
224 "unsupported on CPU %d\n", cpu));
227 target_state = p_tstate->target_state;
228 p_throttling = &(pr->throttling);
230 case THROTTLING_PRECHANGE:
232 * Prechange event is used to choose one proper t-state,
233 * which meets the limits of thermal, user and _TPC.
235 p_limit = &pr->limit;
236 if (p_limit->thermal.tx > target_state)
237 target_state = p_limit->thermal.tx;
238 if (p_limit->user.tx > target_state)
239 target_state = p_limit->user.tx;
240 if (pr->throttling_platform_limit > target_state)
241 target_state = pr->throttling_platform_limit;
242 if (target_state >= p_throttling->state_count) {
244 "Exceed the limit of T-state \n");
245 target_state = p_throttling->state_count - 1;
247 p_tstate->target_state = target_state;
248 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PreChange Event:"
249 "target T-state of CPU %d is T%d\n",
252 case THROTTLING_POSTCHANGE:
254 * Postchange event is only used to update the
255 * T-state flag of acpi_processor_throttling.
257 p_throttling->state = target_state;
258 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PostChange Event:"
259 "CPU %d is switched to T%d\n",
264 "Unsupported Throttling notifier event\n");
272 * _TPC - Throttling Present Capabilities
274 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
276 acpi_status status = 0;
277 unsigned long tpc = 0;
281 status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);
282 if (ACPI_FAILURE(status)) {
283 if (status != AE_NOT_FOUND) {
284 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TPC"));
288 pr->throttling_platform_limit = (int)tpc;
292 int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
295 int throttling_limit;
297 struct acpi_processor_limit *limit;
300 result = acpi_processor_get_platform_limit(pr);
302 /* Throttling Limit is unsupported */
306 throttling_limit = pr->throttling_platform_limit;
307 if (throttling_limit >= pr->throttling.state_count) {
308 /* Uncorrect Throttling Limit */
312 current_state = pr->throttling.state;
313 if (current_state > throttling_limit) {
315 * The current state can meet the requirement of
316 * _TPC limit. But it is reasonable that OSPM changes
317 * t-states from high to low for better performance.
318 * Of course the limit condition of thermal
319 * and user should be considered.
322 target_state = throttling_limit;
323 if (limit->thermal.tx > target_state)
324 target_state = limit->thermal.tx;
325 if (limit->user.tx > target_state)
326 target_state = limit->user.tx;
327 } else if (current_state == throttling_limit) {
329 * Unnecessary to change the throttling state
334 * If the current state is lower than the limit of _TPC, it
335 * will be forced to switch to the throttling state defined
336 * by throttling_platfor_limit.
337 * Because the previous state meets with the limit condition
338 * of thermal and user, it is unnecessary to check it again.
340 target_state = throttling_limit;
342 return acpi_processor_set_throttling(pr, target_state);
346 * _PTC - Processor Throttling Control (and status) register location
348 static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
351 acpi_status status = 0;
352 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
353 union acpi_object *ptc = NULL;
354 union acpi_object obj = { 0 };
355 struct acpi_processor_throttling *throttling;
357 status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
358 if (ACPI_FAILURE(status)) {
359 if (status != AE_NOT_FOUND) {
360 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTC"));
365 ptc = (union acpi_object *)buffer.pointer;
366 if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)
367 || (ptc->package.count != 2)) {
368 printk(KERN_ERR PREFIX "Invalid _PTC data\n");
377 obj = ptc->package.elements[0];
379 if ((obj.type != ACPI_TYPE_BUFFER)
380 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
381 || (obj.buffer.pointer == NULL)) {
382 printk(KERN_ERR PREFIX
383 "Invalid _PTC data (control_register)\n");
387 memcpy(&pr->throttling.control_register, obj.buffer.pointer,
388 sizeof(struct acpi_ptc_register));
394 obj = ptc->package.elements[1];
396 if ((obj.type != ACPI_TYPE_BUFFER)
397 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
398 || (obj.buffer.pointer == NULL)) {
399 printk(KERN_ERR PREFIX "Invalid _PTC data (status_register)\n");
404 memcpy(&pr->throttling.status_register, obj.buffer.pointer,
405 sizeof(struct acpi_ptc_register));
407 throttling = &pr->throttling;
409 if ((throttling->control_register.bit_width +
410 throttling->control_register.bit_offset) > 32) {
411 printk(KERN_ERR PREFIX "Invalid _PTC control register\n");
416 if ((throttling->status_register.bit_width +
417 throttling->status_register.bit_offset) > 32) {
418 printk(KERN_ERR PREFIX "Invalid _PTC status register\n");
424 kfree(buffer.pointer);
430 * _TSS - Throttling Supported States
432 static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
435 acpi_status status = AE_OK;
436 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
437 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
438 struct acpi_buffer state = { 0, NULL };
439 union acpi_object *tss = NULL;
442 status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
443 if (ACPI_FAILURE(status)) {
444 if (status != AE_NOT_FOUND) {
445 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS"));
450 tss = buffer.pointer;
451 if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
452 printk(KERN_ERR PREFIX "Invalid _TSS data\n");
457 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
458 tss->package.count));
460 pr->throttling.state_count = tss->package.count;
461 pr->throttling.states_tss =
462 kmalloc(sizeof(struct acpi_processor_tx_tss) * tss->package.count,
464 if (!pr->throttling.states_tss) {
469 for (i = 0; i < pr->throttling.state_count; i++) {
471 struct acpi_processor_tx_tss *tx =
472 (struct acpi_processor_tx_tss *)&(pr->throttling.
475 state.length = sizeof(struct acpi_processor_tx_tss);
478 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
480 status = acpi_extract_package(&(tss->package.elements[i]),
482 if (ACPI_FAILURE(status)) {
483 ACPI_EXCEPTION((AE_INFO, status, "Invalid _TSS data"));
485 kfree(pr->throttling.states_tss);
489 if (!tx->freqpercentage) {
490 printk(KERN_ERR PREFIX
491 "Invalid _TSS data: freq is zero\n");
493 kfree(pr->throttling.states_tss);
499 kfree(buffer.pointer);
505 * _TSD - T-State Dependencies
507 static int acpi_processor_get_tsd(struct acpi_processor *pr)
510 acpi_status status = AE_OK;
511 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
512 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
513 struct acpi_buffer state = { 0, NULL };
514 union acpi_object *tsd = NULL;
515 struct acpi_tsd_package *pdomain;
516 struct acpi_processor_throttling *pthrottling;
518 pthrottling = &pr->throttling;
519 pthrottling->tsd_valid_flag = 0;
521 status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
522 if (ACPI_FAILURE(status)) {
523 if (status != AE_NOT_FOUND) {
524 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSD"));
529 tsd = buffer.pointer;
530 if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
531 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
536 if (tsd->package.count != 1) {
537 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
542 pdomain = &(pr->throttling.domain_info);
544 state.length = sizeof(struct acpi_tsd_package);
545 state.pointer = pdomain;
547 status = acpi_extract_package(&(tsd->package.elements[0]),
549 if (ACPI_FAILURE(status)) {
550 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
555 if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
556 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:num_entries\n"));
561 if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
562 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:revision\n"));
567 pthrottling = &pr->throttling;
568 pthrottling->tsd_valid_flag = 1;
569 pthrottling->shared_type = pdomain->coord_type;
570 cpu_set(pr->id, pthrottling->shared_cpu_map);
572 * If the coordination type is not defined in ACPI spec,
573 * the tsd_valid_flag will be clear and coordination type
574 * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
576 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
577 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
578 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
579 pthrottling->tsd_valid_flag = 0;
580 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
584 kfree(buffer.pointer);
588 /* --------------------------------------------------------------------------
590 -------------------------------------------------------------------------- */
591 static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
601 if (!pr->flags.throttling)
604 pr->throttling.state = 0;
606 duty_mask = pr->throttling.state_count - 1;
608 duty_mask <<= pr->throttling.duty_offset;
612 value = inl(pr->throttling.address);
615 * Compute the current throttling state when throttling is enabled
619 duty_value = value & duty_mask;
620 duty_value >>= pr->throttling.duty_offset;
623 state = pr->throttling.state_count - duty_value;
626 pr->throttling.state = state;
630 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
631 "Throttling state is T%d (%d%% throttling applied)\n",
632 state, pr->throttling.states[state].performance));
638 static int acpi_throttling_rdmsr(struct acpi_processor *pr,
639 acpi_integer * value)
641 struct cpuinfo_x86 *c;
642 u64 msr_high, msr_low;
650 if ((c->x86_vendor != X86_VENDOR_INTEL) ||
651 !cpu_has(c, X86_FEATURE_ACPI)) {
652 printk(KERN_ERR PREFIX
653 "HARDWARE addr space,NOT supported yet\n");
657 rdmsr_safe(MSR_IA32_THERM_CONTROL,
658 (u32 *)&msr_low , (u32 *) &msr_high);
659 msr = (msr_high << 32) | msr_low;
660 *value = (acpi_integer) msr;
666 static int acpi_throttling_wrmsr(struct acpi_processor *pr, acpi_integer value)
668 struct cpuinfo_x86 *c;
676 if ((c->x86_vendor != X86_VENDOR_INTEL) ||
677 !cpu_has(c, X86_FEATURE_ACPI)) {
678 printk(KERN_ERR PREFIX
679 "HARDWARE addr space,NOT supported yet\n");
682 wrmsr_safe(MSR_IA32_THERM_CONTROL,
683 msr & 0xffffffff, msr >> 32);
689 static int acpi_throttling_rdmsr(struct acpi_processor *pr,
690 acpi_integer * value)
692 printk(KERN_ERR PREFIX
693 "HARDWARE addr space,NOT supported yet\n");
697 static int acpi_throttling_wrmsr(struct acpi_processor *pr, acpi_integer value)
699 printk(KERN_ERR PREFIX
700 "HARDWARE addr space,NOT supported yet\n");
705 static int acpi_read_throttling_status(struct acpi_processor *pr,
708 u32 bit_width, bit_offset;
711 struct acpi_processor_throttling *throttling;
714 throttling = &pr->throttling;
715 switch (throttling->status_register.space_id) {
716 case ACPI_ADR_SPACE_SYSTEM_IO:
718 bit_width = throttling->status_register.bit_width;
719 bit_offset = throttling->status_register.bit_offset;
721 acpi_os_read_port((acpi_io_address) throttling->status_register.
722 address, (u32 *) &ptc_value,
723 (u32) (bit_width + bit_offset));
724 ptc_mask = (1 << bit_width) - 1;
725 *value = (acpi_integer) ((ptc_value >> bit_offset) & ptc_mask);
728 case ACPI_ADR_SPACE_FIXED_HARDWARE:
729 ret = acpi_throttling_rdmsr(pr, value);
732 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
733 (u32) (throttling->status_register.space_id));
738 static int acpi_write_throttling_state(struct acpi_processor *pr,
741 u32 bit_width, bit_offset;
744 struct acpi_processor_throttling *throttling;
747 throttling = &pr->throttling;
748 switch (throttling->control_register.space_id) {
749 case ACPI_ADR_SPACE_SYSTEM_IO:
750 bit_width = throttling->control_register.bit_width;
751 bit_offset = throttling->control_register.bit_offset;
752 ptc_mask = (1 << bit_width) - 1;
753 ptc_value = value & ptc_mask;
755 acpi_os_write_port((acpi_io_address) throttling->
756 control_register.address,
757 (u32) (ptc_value << bit_offset),
758 (u32) (bit_width + bit_offset));
761 case ACPI_ADR_SPACE_FIXED_HARDWARE:
762 ret = acpi_throttling_wrmsr(pr, value);
765 printk(KERN_ERR PREFIX "Unknown addr space %d\n",
766 (u32) (throttling->control_register.space_id));
771 static int acpi_get_throttling_state(struct acpi_processor *pr,
776 for (i = 0; i < pr->throttling.state_count; i++) {
777 struct acpi_processor_tx_tss *tx =
778 (struct acpi_processor_tx_tss *)&(pr->throttling.
780 if (tx->control == value)
783 if (i > pr->throttling.state_count)
788 static int acpi_get_throttling_value(struct acpi_processor *pr,
789 int state, acpi_integer *value)
793 if (state >= 0 && state <= pr->throttling.state_count) {
794 struct acpi_processor_tx_tss *tx =
795 (struct acpi_processor_tx_tss *)&(pr->throttling.
797 *value = tx->control;
803 static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
812 if (!pr->flags.throttling)
815 pr->throttling.state = 0;
818 ret = acpi_read_throttling_status(pr, &value);
820 state = acpi_get_throttling_state(pr, value);
821 pr->throttling.state = state;
827 static int acpi_processor_get_throttling(struct acpi_processor *pr)
829 cpumask_t saved_mask;
830 cpumask_of_cpu_ptr_declare(new_mask);
836 if (!pr->flags.throttling)
839 * Migrate task to the cpu pointed by pr.
841 saved_mask = current->cpus_allowed;
842 cpumask_of_cpu_ptr_next(new_mask, pr->id);
843 set_cpus_allowed_ptr(current, new_mask);
844 ret = pr->throttling.acpi_processor_get_throttling(pr);
845 /* restore the previous state */
846 set_cpus_allowed_ptr(current, &saved_mask);
851 static int acpi_processor_get_fadt_info(struct acpi_processor *pr)
855 if (!pr->throttling.address) {
856 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
858 } else if (!pr->throttling.duty_width) {
859 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
862 /* TBD: Support duty_cycle values that span bit 4. */
863 else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
864 printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");
868 pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
871 * Compute state values. Note that throttling displays a linear power
872 * performance relationship (at 50% performance the CPU will consume
873 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
876 step = (1000 / pr->throttling.state_count);
878 for (i = 0; i < pr->throttling.state_count; i++) {
879 pr->throttling.states[i].performance = 1000 - step * i;
880 pr->throttling.states[i].power = 1000 - step * i;
885 static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
895 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
898 if (!pr->flags.throttling)
901 if (state == pr->throttling.state)
904 if (state < pr->throttling_platform_limit)
907 * Calculate the duty_value and duty_mask.
910 duty_value = pr->throttling.state_count - state;
912 duty_value <<= pr->throttling.duty_offset;
914 /* Used to clear all duty_value bits */
915 duty_mask = pr->throttling.state_count - 1;
917 duty_mask <<= acpi_gbl_FADT.duty_offset;
918 duty_mask = ~duty_mask;
924 * Disable throttling by writing a 0 to bit 4. Note that we must
925 * turn it off before you can change the duty_value.
927 value = inl(pr->throttling.address);
930 outl(value, pr->throttling.address);
934 * Write the new duty_value and then enable throttling. Note
935 * that a state value of 0 leaves throttling disabled.
940 outl(value, pr->throttling.address);
943 outl(value, pr->throttling.address);
946 pr->throttling.state = state;
950 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
951 "Throttling state set to T%d (%d%%)\n", state,
952 (pr->throttling.states[state].performance ? pr->
953 throttling.states[state].performance / 10 : 0)));
958 static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
967 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
970 if (!pr->flags.throttling)
973 if (state == pr->throttling.state)
976 if (state < pr->throttling_platform_limit)
980 ret = acpi_get_throttling_value(pr, state, &value);
982 acpi_write_throttling_state(pr, value);
983 pr->throttling.state = state;
989 int acpi_processor_set_throttling(struct acpi_processor *pr, int state)
991 cpumask_t saved_mask;
992 cpumask_of_cpu_ptr_declare(new_mask);
995 struct acpi_processor *match_pr;
996 struct acpi_processor_throttling *p_throttling;
997 struct throttling_tstate t_state;
998 cpumask_t online_throttling_cpus;
1003 if (!pr->flags.throttling)
1006 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1009 saved_mask = current->cpus_allowed;
1010 t_state.target_state = state;
1011 p_throttling = &(pr->throttling);
1012 cpus_and(online_throttling_cpus, cpu_online_map,
1013 p_throttling->shared_cpu_map);
1015 * The throttling notifier will be called for every
1016 * affected cpu in order to get one proper T-state.
1017 * The notifier event is THROTTLING_PRECHANGE.
1019 for_each_cpu_mask_nr(i, online_throttling_cpus) {
1021 acpi_processor_throttling_notifier(THROTTLING_PRECHANGE,
1025 * The function of acpi_processor_set_throttling will be called
1026 * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
1027 * it is necessary to call it for every affected cpu. Otherwise
1028 * it can be called only for the cpu pointed by pr.
1030 if (p_throttling->shared_type == DOMAIN_COORD_TYPE_SW_ANY) {
1031 cpumask_of_cpu_ptr_next(new_mask, pr->id);
1032 set_cpus_allowed_ptr(current, new_mask);
1033 ret = p_throttling->acpi_processor_set_throttling(pr,
1034 t_state.target_state);
1037 * When the T-state coordination is SW_ALL or HW_ALL,
1038 * it is necessary to set T-state for every affected
1041 for_each_cpu_mask_nr(i, online_throttling_cpus) {
1042 match_pr = per_cpu(processors, i);
1044 * If the pointer is invalid, we will report the
1045 * error message and continue.
1048 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1049 "Invalid Pointer for CPU %d\n", i));
1053 * If the throttling control is unsupported on CPU i,
1054 * we will report the error message and continue.
1056 if (!match_pr->flags.throttling) {
1057 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1058 "Throttling Controll is unsupported "
1063 cpumask_of_cpu_ptr_next(new_mask, i);
1064 set_cpus_allowed_ptr(current, new_mask);
1065 ret = match_pr->throttling.
1066 acpi_processor_set_throttling(
1067 match_pr, t_state.target_state);
1071 * After the set_throttling is called, the
1072 * throttling notifier is called for every
1073 * affected cpu to update the T-states.
1074 * The notifier event is THROTTLING_POSTCHANGE
1076 for_each_cpu_mask_nr(i, online_throttling_cpus) {
1078 acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE,
1081 /* restore the previous state */
1082 set_cpus_allowed_ptr(current, &saved_mask);
1086 int acpi_processor_get_throttling_info(struct acpi_processor *pr)
1089 struct acpi_processor_throttling *pthrottling;
1091 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1092 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1093 pr->throttling.address,
1094 pr->throttling.duty_offset,
1095 pr->throttling.duty_width));
1101 * Evaluate _PTC, _TSS and _TPC
1102 * They must all be present or none of them can be used.
1104 if (acpi_processor_get_throttling_control(pr) ||
1105 acpi_processor_get_throttling_states(pr) ||
1106 acpi_processor_get_platform_limit(pr))
1108 pr->throttling.acpi_processor_get_throttling =
1109 &acpi_processor_get_throttling_fadt;
1110 pr->throttling.acpi_processor_set_throttling =
1111 &acpi_processor_set_throttling_fadt;
1112 if (acpi_processor_get_fadt_info(pr))
1115 pr->throttling.acpi_processor_get_throttling =
1116 &acpi_processor_get_throttling_ptc;
1117 pr->throttling.acpi_processor_set_throttling =
1118 &acpi_processor_set_throttling_ptc;
1122 * If TSD package for one CPU can't be parsed successfully, it means
1123 * that this CPU will have no coordination with other CPUs.
1125 if (acpi_processor_get_tsd(pr)) {
1126 pthrottling = &pr->throttling;
1127 pthrottling->tsd_valid_flag = 0;
1128 cpu_set(pr->id, pthrottling->shared_cpu_map);
1129 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
1133 * PIIX4 Errata: We don't support throttling on the original PIIX4.
1134 * This shouldn't be an issue as few (if any) mobile systems ever
1137 if (errata.piix4.throttle) {
1138 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1139 "Throttling not supported on PIIX4 A- or B-step\n"));
1143 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
1144 pr->throttling.state_count));
1146 pr->flags.throttling = 1;
1149 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
1150 * thermal) decide to lower performance if it so chooses, but for now
1151 * we'll crank up the speed.
1154 result = acpi_processor_get_throttling(pr);
1158 if (pr->throttling.state) {
1159 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1160 "Disabling throttling (was T%d)\n",
1161 pr->throttling.state));
1162 result = acpi_processor_set_throttling(pr, 0);
1169 pr->flags.throttling = 0;
1174 /* proc interface */
1176 static int acpi_processor_throttling_seq_show(struct seq_file *seq,
1179 struct acpi_processor *pr = seq->private;
1186 if (!(pr->throttling.state_count > 0)) {
1187 seq_puts(seq, "<not supported>\n");
1191 result = acpi_processor_get_throttling(pr);
1195 "Could not determine current throttling state.\n");
1199 seq_printf(seq, "state count: %d\n"
1200 "active state: T%d\n"
1201 "state available: T%d to T%d\n",
1202 pr->throttling.state_count, pr->throttling.state,
1203 pr->throttling_platform_limit,
1204 pr->throttling.state_count - 1);
1206 seq_puts(seq, "states:\n");
1207 if (pr->throttling.acpi_processor_get_throttling ==
1208 acpi_processor_get_throttling_fadt) {
1209 for (i = 0; i < pr->throttling.state_count; i++)
1210 seq_printf(seq, " %cT%d: %02d%%\n",
1211 (i == pr->throttling.state ? '*' : ' '), i,
1212 (pr->throttling.states[i].performance ? pr->
1213 throttling.states[i].performance / 10 : 0));
1215 for (i = 0; i < pr->throttling.state_count; i++)
1216 seq_printf(seq, " %cT%d: %02d%%\n",
1217 (i == pr->throttling.state ? '*' : ' '), i,
1218 (int)pr->throttling.states_tss[i].
1226 static int acpi_processor_throttling_open_fs(struct inode *inode,
1229 return single_open(file, acpi_processor_throttling_seq_show,
1233 static ssize_t acpi_processor_write_throttling(struct file *file,
1234 const char __user * buffer,
1235 size_t count, loff_t * data)
1238 struct seq_file *m = file->private_data;
1239 struct acpi_processor *pr = m->private;
1240 char state_string[5] = "";
1242 size_t state_val = 0;
1243 char tmpbuf[5] = "";
1245 if (!pr || (count > sizeof(state_string) - 1))
1248 if (copy_from_user(state_string, buffer, count))
1251 state_string[count] = '\0';
1252 if ((count > 0) && (state_string[count-1] == '\n'))
1253 state_string[count-1] = '\0';
1255 charp = state_string;
1256 if ((state_string[0] == 't') || (state_string[0] == 'T'))
1259 state_val = simple_strtoul(charp, NULL, 0);
1260 if (state_val >= pr->throttling.state_count)
1263 snprintf(tmpbuf, 5, "%zu", state_val);
1265 if (strcmp(tmpbuf, charp) != 0)
1268 result = acpi_processor_set_throttling(pr, state_val);
1275 struct file_operations acpi_processor_throttling_fops = {
1276 .owner = THIS_MODULE,
1277 .open = acpi_processor_throttling_open_fs,
1279 .write = acpi_processor_write_throttling,
1280 .llseek = seq_lseek,
1281 .release = single_release,