1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * processor_throttling.c - Throttling submodule of the ACPI processor driver
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * Copyright (C) 2004 Dominik Brodowski <linux@brodo.de>
8 * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
9 * - Added processor hotplug support
12 #define pr_fmt(fmt) "ACPI: " fmt
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/sched.h>
19 #include <linux/cpufreq.h>
20 #include <linux/acpi.h>
21 #include <linux/uaccess.h>
22 #include <acpi/processor.h>
29 * 0 -> acpi processor driver doesn't ignore _TPC values
30 * 1 -> acpi processor driver ignores _TPC values
32 static int ignore_tpc;
33 module_param(ignore_tpc, int, 0644);
34 MODULE_PARM_DESC(ignore_tpc, "Disable broken BIOS _TPC throttling support");
36 struct throttling_tstate {
37 unsigned int cpu; /* cpu nr */
38 int target_state; /* target T-state */
41 struct acpi_processor_throttling_arg {
42 struct acpi_processor *pr;
47 #define THROTTLING_PRECHANGE (1)
48 #define THROTTLING_POSTCHANGE (2)
50 static int acpi_processor_get_throttling(struct acpi_processor *pr);
51 static int __acpi_processor_set_throttling(struct acpi_processor *pr,
52 int state, bool force, bool direct);
54 static int acpi_processor_update_tsd_coord(void)
59 cpumask_var_t covered_cpus;
60 struct acpi_processor *pr, *match_pr;
61 struct acpi_tsd_package *pdomain, *match_pdomain;
62 struct acpi_processor_throttling *pthrottling, *match_pthrottling;
64 if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
68 * Now that we have _TSD data from all CPUs, lets setup T-state
69 * coordination between all CPUs.
71 for_each_possible_cpu(i) {
72 pr = per_cpu(processors, i);
76 /* Basic validity check for domain info */
77 pthrottling = &(pr->throttling);
80 * If tsd package for one cpu is invalid, the coordination
81 * among all CPUs is thought as invalid.
84 if (!pthrottling->tsd_valid_flag) {
92 for_each_possible_cpu(i) {
93 pr = per_cpu(processors, i);
97 if (cpumask_test_cpu(i, covered_cpus))
99 pthrottling = &pr->throttling;
101 pdomain = &(pthrottling->domain_info);
102 cpumask_set_cpu(i, pthrottling->shared_cpu_map);
103 cpumask_set_cpu(i, covered_cpus);
105 * If the number of processor in the TSD domain is 1, it is
106 * unnecessary to parse the coordination for this CPU.
108 if (pdomain->num_processors <= 1)
111 /* Validate the Domain info */
112 count_target = pdomain->num_processors;
114 for_each_possible_cpu(j) {
118 match_pr = per_cpu(processors, j);
122 match_pthrottling = &(match_pr->throttling);
123 match_pdomain = &(match_pthrottling->domain_info);
124 if (match_pdomain->domain != pdomain->domain)
127 /* Here i and j are in the same domain.
128 * If two TSD packages have the same domain, they
129 * should have the same num_porcessors and
130 * coordination type. Otherwise it will be regarded
133 if (match_pdomain->num_processors != count_target) {
138 if (pdomain->coord_type != match_pdomain->coord_type) {
143 cpumask_set_cpu(j, covered_cpus);
144 cpumask_set_cpu(j, pthrottling->shared_cpu_map);
146 for_each_possible_cpu(j) {
150 match_pr = per_cpu(processors, j);
154 match_pthrottling = &(match_pr->throttling);
155 match_pdomain = &(match_pthrottling->domain_info);
156 if (match_pdomain->domain != pdomain->domain)
160 * If some CPUS have the same domain, they
161 * will have the same shared_cpu_map.
163 cpumask_copy(match_pthrottling->shared_cpu_map,
164 pthrottling->shared_cpu_map);
169 free_cpumask_var(covered_cpus);
171 for_each_possible_cpu(i) {
172 pr = per_cpu(processors, i);
177 * Assume no coordination on any error parsing domain info.
178 * The coordination type will be forced as SW_ALL.
181 pthrottling = &(pr->throttling);
182 cpumask_clear(pthrottling->shared_cpu_map);
183 cpumask_set_cpu(i, pthrottling->shared_cpu_map);
184 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
192 * Update the T-state coordination after the _TSD
193 * data for all cpus is obtained.
195 void acpi_processor_throttling_init(void)
197 if (acpi_processor_update_tsd_coord())
198 pr_debug("Assume no T-state coordination\n");
201 static int acpi_processor_throttling_notifier(unsigned long event, void *data)
203 struct throttling_tstate *p_tstate = data;
204 struct acpi_processor *pr;
207 struct acpi_processor_limit *p_limit;
208 struct acpi_processor_throttling *p_throttling;
211 pr = per_cpu(processors, cpu);
213 pr_debug("Invalid pr pointer\n");
216 if (!pr->flags.throttling) {
217 acpi_handle_debug(pr->handle,
218 "Throttling control unsupported on CPU %d\n",
222 target_state = p_tstate->target_state;
223 p_throttling = &(pr->throttling);
225 case THROTTLING_PRECHANGE:
227 * Prechange event is used to choose one proper t-state,
228 * which meets the limits of thermal, user and _TPC.
230 p_limit = &pr->limit;
231 if (p_limit->thermal.tx > target_state)
232 target_state = p_limit->thermal.tx;
233 if (p_limit->user.tx > target_state)
234 target_state = p_limit->user.tx;
235 if (pr->throttling_platform_limit > target_state)
236 target_state = pr->throttling_platform_limit;
237 if (target_state >= p_throttling->state_count) {
238 pr_warn("Exceed the limit of T-state \n");
239 target_state = p_throttling->state_count - 1;
241 p_tstate->target_state = target_state;
242 acpi_handle_debug(pr->handle,
243 "PreChange Event: target T-state of CPU %d is T%d\n",
246 case THROTTLING_POSTCHANGE:
248 * Postchange event is only used to update the
249 * T-state flag of acpi_processor_throttling.
251 p_throttling->state = target_state;
252 acpi_handle_debug(pr->handle,
253 "PostChange Event: CPU %d is switched to T%d\n",
257 pr_warn("Unsupported Throttling notifier event\n");
265 * _TPC - Throttling Present Capabilities
267 static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
269 acpi_status status = 0;
270 unsigned long long tpc = 0;
278 status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);
279 if (ACPI_FAILURE(status)) {
280 if (status != AE_NOT_FOUND)
281 acpi_evaluation_failure_warn(pr->handle, "_TPC", status);
287 pr->throttling_platform_limit = (int)tpc;
291 int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
294 int throttling_limit;
296 struct acpi_processor_limit *limit;
302 result = acpi_processor_get_platform_limit(pr);
304 /* Throttling Limit is unsupported */
308 throttling_limit = pr->throttling_platform_limit;
309 if (throttling_limit >= pr->throttling.state_count) {
310 /* Uncorrect Throttling Limit */
314 current_state = pr->throttling.state;
315 if (current_state > throttling_limit) {
317 * The current state can meet the requirement of
318 * _TPC limit. But it is reasonable that OSPM changes
319 * t-states from high to low for better performance.
320 * Of course the limit condition of thermal
321 * and user should be considered.
324 target_state = throttling_limit;
325 if (limit->thermal.tx > target_state)
326 target_state = limit->thermal.tx;
327 if (limit->user.tx > target_state)
328 target_state = limit->user.tx;
329 } else if (current_state == throttling_limit) {
331 * Unnecessary to change the throttling state
336 * If the current state is lower than the limit of _TPC, it
337 * will be forced to switch to the throttling state defined
338 * by throttling_platfor_limit.
339 * Because the previous state meets with the limit condition
340 * of thermal and user, it is unnecessary to check it again.
342 target_state = throttling_limit;
344 return acpi_processor_set_throttling(pr, target_state, false);
348 * This function is used to reevaluate whether the T-state is valid
349 * after one CPU is onlined/offlined.
350 * It is noted that it won't reevaluate the following properties for
353 * 2. the number of supported T-state
356 void acpi_processor_reevaluate_tstate(struct acpi_processor *pr,
362 /* When one CPU is offline, the T-state throttling
363 * will be invalidated.
365 pr->flags.throttling = 0;
368 /* the following is to recheck whether the T-state is valid for
371 if (!pr->throttling.state_count) {
372 /* If the number of T-state is invalid, it is
375 pr->flags.throttling = 0;
378 pr->flags.throttling = 1;
380 /* Disable throttling (if enabled). We'll let subsequent
381 * policy (e.g.thermal) decide to lower performance if it
382 * so chooses, but for now we'll crank up the speed.
385 result = acpi_processor_get_throttling(pr);
389 if (pr->throttling.state) {
390 result = acpi_processor_set_throttling(pr, 0, false);
397 pr->flags.throttling = 0;
400 * _PTC - Processor Throttling Control (and status) register location
402 static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
405 acpi_status status = 0;
406 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
407 union acpi_object *ptc = NULL;
408 union acpi_object obj;
409 struct acpi_processor_throttling *throttling;
411 status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
412 if (ACPI_FAILURE(status)) {
413 if (status != AE_NOT_FOUND)
414 acpi_evaluation_failure_warn(pr->handle, "_PTC", status);
419 ptc = (union acpi_object *)buffer.pointer;
420 if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)
421 || (ptc->package.count != 2)) {
422 pr_err("Invalid _PTC data\n");
431 obj = ptc->package.elements[0];
433 if ((obj.type != ACPI_TYPE_BUFFER)
434 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
435 || (obj.buffer.pointer == NULL)) {
436 pr_err("Invalid _PTC data (control_register)\n");
440 memcpy(&pr->throttling.control_register, obj.buffer.pointer,
441 sizeof(struct acpi_ptc_register));
447 obj = ptc->package.elements[1];
449 if ((obj.type != ACPI_TYPE_BUFFER)
450 || (obj.buffer.length < sizeof(struct acpi_ptc_register))
451 || (obj.buffer.pointer == NULL)) {
452 pr_err("Invalid _PTC data (status_register)\n");
457 memcpy(&pr->throttling.status_register, obj.buffer.pointer,
458 sizeof(struct acpi_ptc_register));
460 throttling = &pr->throttling;
462 if ((throttling->control_register.bit_width +
463 throttling->control_register.bit_offset) > 32) {
464 pr_err("Invalid _PTC control register\n");
469 if ((throttling->status_register.bit_width +
470 throttling->status_register.bit_offset) > 32) {
471 pr_err("Invalid _PTC status register\n");
477 kfree(buffer.pointer);
483 * _TSS - Throttling Supported States
485 static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
488 acpi_status status = AE_OK;
489 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
490 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
491 struct acpi_buffer state = { 0, NULL };
492 union acpi_object *tss = NULL;
495 status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
496 if (ACPI_FAILURE(status)) {
497 if (status != AE_NOT_FOUND)
498 acpi_evaluation_failure_warn(pr->handle, "_TSS", status);
503 tss = buffer.pointer;
504 if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
505 pr_err("Invalid _TSS data\n");
510 acpi_handle_debug(pr->handle, "Found %d throttling states\n",
513 pr->throttling.state_count = tss->package.count;
514 pr->throttling.states_tss =
515 kmalloc_array(tss->package.count,
516 sizeof(struct acpi_processor_tx_tss),
518 if (!pr->throttling.states_tss) {
523 for (i = 0; i < pr->throttling.state_count; i++) {
525 struct acpi_processor_tx_tss *tx =
526 (struct acpi_processor_tx_tss *)&(pr->throttling.
529 state.length = sizeof(struct acpi_processor_tx_tss);
532 acpi_handle_debug(pr->handle, "Extracting state %d\n", i);
534 status = acpi_extract_package(&(tss->package.elements[i]),
536 if (ACPI_FAILURE(status)) {
537 acpi_handle_warn(pr->handle, "Invalid _TSS data: %s\n",
538 acpi_format_exception(status));
540 kfree(pr->throttling.states_tss);
544 if (!tx->freqpercentage) {
545 pr_err("Invalid _TSS data: freq is zero\n");
547 kfree(pr->throttling.states_tss);
553 kfree(buffer.pointer);
559 * _TSD - T-State Dependencies
561 static int acpi_processor_get_tsd(struct acpi_processor *pr)
564 acpi_status status = AE_OK;
565 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
566 struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
567 struct acpi_buffer state = { 0, NULL };
568 union acpi_object *tsd = NULL;
569 struct acpi_tsd_package *pdomain;
570 struct acpi_processor_throttling *pthrottling;
572 pthrottling = &pr->throttling;
573 pthrottling->tsd_valid_flag = 0;
575 status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
576 if (ACPI_FAILURE(status)) {
577 if (status != AE_NOT_FOUND)
578 acpi_evaluation_failure_warn(pr->handle, "_TSD", status);
583 tsd = buffer.pointer;
584 if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
585 pr_err("Invalid _TSD data\n");
590 if (tsd->package.count != 1) {
591 pr_err("Invalid _TSD data\n");
596 pdomain = &(pr->throttling.domain_info);
598 state.length = sizeof(struct acpi_tsd_package);
599 state.pointer = pdomain;
601 status = acpi_extract_package(&(tsd->package.elements[0]),
603 if (ACPI_FAILURE(status)) {
604 pr_err("Invalid _TSD data\n");
609 if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
610 pr_err("Unknown _TSD:num_entries\n");
615 if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
616 pr_err("Unknown _TSD:revision\n");
621 pthrottling = &pr->throttling;
622 pthrottling->tsd_valid_flag = 1;
623 pthrottling->shared_type = pdomain->coord_type;
624 cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
626 * If the coordination type is not defined in ACPI spec,
627 * the tsd_valid_flag will be clear and coordination type
628 * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
630 if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
631 pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
632 pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
633 pthrottling->tsd_valid_flag = 0;
634 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
638 kfree(buffer.pointer);
642 /* --------------------------------------------------------------------------
644 -------------------------------------------------------------------------- */
645 static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
655 if (!pr->flags.throttling)
659 * We don't care about error returns - we just try to mark
660 * these reserved so that nobody else is confused into thinking
661 * that this region might be unused..
663 * (In particular, allocating the IO range for Cardbus)
665 request_region(pr->throttling.address, 6, "ACPI CPU throttle");
667 pr->throttling.state = 0;
669 duty_mask = pr->throttling.state_count - 1;
671 duty_mask <<= pr->throttling.duty_offset;
675 value = inl(pr->throttling.address);
678 * Compute the current throttling state when throttling is enabled
682 duty_value = value & duty_mask;
683 duty_value >>= pr->throttling.duty_offset;
686 state = pr->throttling.state_count - duty_value;
689 pr->throttling.state = state;
693 acpi_handle_debug(pr->handle,
694 "Throttling state is T%d (%d%% throttling applied)\n",
695 state, pr->throttling.states[state].performance);
701 static int acpi_throttling_rdmsr(u64 *value)
703 u64 msr_high, msr_low;
707 if ((this_cpu_read(cpu_info.x86_vendor) != X86_VENDOR_INTEL) ||
708 !this_cpu_has(X86_FEATURE_ACPI)) {
709 pr_err("HARDWARE addr space,NOT supported yet\n");
713 rdmsr_safe(MSR_IA32_THERM_CONTROL,
714 (u32 *)&msr_low, (u32 *) &msr_high);
715 msr = (msr_high << 32) | msr_low;
722 static int acpi_throttling_wrmsr(u64 value)
727 if ((this_cpu_read(cpu_info.x86_vendor) != X86_VENDOR_INTEL) ||
728 !this_cpu_has(X86_FEATURE_ACPI)) {
729 pr_err("HARDWARE addr space,NOT supported yet\n");
732 wrmsr_safe(MSR_IA32_THERM_CONTROL,
733 msr & 0xffffffff, msr >> 32);
739 static int acpi_throttling_rdmsr(u64 *value)
741 pr_err("HARDWARE addr space,NOT supported yet\n");
745 static int acpi_throttling_wrmsr(u64 value)
747 pr_err("HARDWARE addr space,NOT supported yet\n");
752 static int acpi_read_throttling_status(struct acpi_processor *pr,
755 u32 bit_width, bit_offset;
758 struct acpi_processor_throttling *throttling;
761 throttling = &pr->throttling;
762 switch (throttling->status_register.space_id) {
763 case ACPI_ADR_SPACE_SYSTEM_IO:
764 bit_width = throttling->status_register.bit_width;
765 bit_offset = throttling->status_register.bit_offset;
767 acpi_os_read_port((acpi_io_address) throttling->status_register.
769 (u32) (bit_width + bit_offset));
770 ptc_mask = (1 << bit_width) - 1;
771 *value = (u64) ((ptc_value >> bit_offset) & ptc_mask);
774 case ACPI_ADR_SPACE_FIXED_HARDWARE:
775 ret = acpi_throttling_rdmsr(value);
778 pr_err("Unknown addr space %d\n",
779 (u32) (throttling->status_register.space_id));
784 static int acpi_write_throttling_state(struct acpi_processor *pr,
787 u32 bit_width, bit_offset;
790 struct acpi_processor_throttling *throttling;
793 throttling = &pr->throttling;
794 switch (throttling->control_register.space_id) {
795 case ACPI_ADR_SPACE_SYSTEM_IO:
796 bit_width = throttling->control_register.bit_width;
797 bit_offset = throttling->control_register.bit_offset;
798 ptc_mask = (1 << bit_width) - 1;
799 ptc_value = value & ptc_mask;
801 acpi_os_write_port((acpi_io_address) throttling->
802 control_register.address,
803 (u32) (ptc_value << bit_offset),
804 (u32) (bit_width + bit_offset));
807 case ACPI_ADR_SPACE_FIXED_HARDWARE:
808 ret = acpi_throttling_wrmsr(value);
811 pr_err("Unknown addr space %d\n",
812 (u32) (throttling->control_register.space_id));
817 static int acpi_get_throttling_state(struct acpi_processor *pr,
822 for (i = 0; i < pr->throttling.state_count; i++) {
823 struct acpi_processor_tx_tss *tx =
824 (struct acpi_processor_tx_tss *)&(pr->throttling.
826 if (tx->control == value)
832 static int acpi_get_throttling_value(struct acpi_processor *pr,
833 int state, u64 *value)
837 if (state >= 0 && state <= pr->throttling.state_count) {
838 struct acpi_processor_tx_tss *tx =
839 (struct acpi_processor_tx_tss *)&(pr->throttling.
841 *value = tx->control;
847 static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
856 if (!pr->flags.throttling)
859 pr->throttling.state = 0;
862 ret = acpi_read_throttling_status(pr, &value);
864 state = acpi_get_throttling_state(pr, value);
866 acpi_handle_debug(pr->handle,
867 "Invalid throttling state, reset\n");
869 ret = __acpi_processor_set_throttling(pr, state, true,
874 pr->throttling.state = state;
880 static long __acpi_processor_get_throttling(void *data)
882 struct acpi_processor *pr = data;
884 return pr->throttling.acpi_processor_get_throttling(pr);
887 static int acpi_processor_get_throttling(struct acpi_processor *pr)
892 if (!pr->flags.throttling)
896 * This is either called from the CPU hotplug callback of
897 * processor_driver or via the ACPI probe function. In the latter
898 * case the CPU is not guaranteed to be online. Both call sites are
899 * protected against CPU hotplug.
901 if (!cpu_online(pr->id))
904 return call_on_cpu(pr->id, __acpi_processor_get_throttling, pr, false);
907 static int acpi_processor_get_fadt_info(struct acpi_processor *pr)
911 if (!pr->throttling.address) {
912 acpi_handle_debug(pr->handle, "No throttling register\n");
914 } else if (!pr->throttling.duty_width) {
915 acpi_handle_debug(pr->handle, "No throttling states\n");
918 /* TBD: Support duty_cycle values that span bit 4. */
919 else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
920 pr_warn("duty_cycle spans bit 4\n");
924 pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
927 * Compute state values. Note that throttling displays a linear power
928 * performance relationship (at 50% performance the CPU will consume
929 * 50% power). Values are in 1/10th of a percent to preserve accuracy.
932 step = (1000 / pr->throttling.state_count);
934 for (i = 0; i < pr->throttling.state_count; i++) {
935 pr->throttling.states[i].performance = 1000 - step * i;
936 pr->throttling.states[i].power = 1000 - step * i;
941 static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
942 int state, bool force)
951 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
954 if (!pr->flags.throttling)
957 if (!force && (state == pr->throttling.state))
960 if (state < pr->throttling_platform_limit)
963 * Calculate the duty_value and duty_mask.
966 duty_value = pr->throttling.state_count - state;
968 duty_value <<= pr->throttling.duty_offset;
970 /* Used to clear all duty_value bits */
971 duty_mask = pr->throttling.state_count - 1;
973 duty_mask <<= acpi_gbl_FADT.duty_offset;
974 duty_mask = ~duty_mask;
980 * Disable throttling by writing a 0 to bit 4. Note that we must
981 * turn it off before you can change the duty_value.
983 value = inl(pr->throttling.address);
986 outl(value, pr->throttling.address);
990 * Write the new duty_value and then enable throttling. Note
991 * that a state value of 0 leaves throttling disabled.
996 outl(value, pr->throttling.address);
999 outl(value, pr->throttling.address);
1002 pr->throttling.state = state;
1006 acpi_handle_debug(pr->handle,
1007 "Throttling state set to T%d (%d%%)\n", state,
1008 (pr->throttling.states[state].performance ? pr->
1009 throttling.states[state].performance / 10 : 0));
1014 static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
1015 int state, bool force)
1023 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1026 if (!pr->flags.throttling)
1029 if (!force && (state == pr->throttling.state))
1032 if (state < pr->throttling_platform_limit)
1036 ret = acpi_get_throttling_value(pr, state, &value);
1038 acpi_write_throttling_state(pr, value);
1039 pr->throttling.state = state;
1045 static long acpi_processor_throttling_fn(void *data)
1047 struct acpi_processor_throttling_arg *arg = data;
1048 struct acpi_processor *pr = arg->pr;
1050 return pr->throttling.acpi_processor_set_throttling(pr,
1051 arg->target_state, arg->force);
1054 static int __acpi_processor_set_throttling(struct acpi_processor *pr,
1055 int state, bool force, bool direct)
1059 struct acpi_processor *match_pr;
1060 struct acpi_processor_throttling *p_throttling;
1061 struct acpi_processor_throttling_arg arg;
1062 struct throttling_tstate t_state;
1067 if (!pr->flags.throttling)
1070 if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1073 if (cpu_is_offline(pr->id)) {
1075 * the cpu pointed by pr->id is offline. Unnecessary to change
1076 * the throttling state any more.
1081 t_state.target_state = state;
1082 p_throttling = &(pr->throttling);
1085 * The throttling notifier will be called for every
1086 * affected cpu in order to get one proper T-state.
1087 * The notifier event is THROTTLING_PRECHANGE.
1089 for_each_cpu_and(i, cpu_online_mask, p_throttling->shared_cpu_map) {
1091 acpi_processor_throttling_notifier(THROTTLING_PRECHANGE,
1095 * The function of acpi_processor_set_throttling will be called
1096 * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
1097 * it is necessary to call it for every affected cpu. Otherwise
1098 * it can be called only for the cpu pointed by pr.
1100 if (p_throttling->shared_type == DOMAIN_COORD_TYPE_SW_ANY) {
1102 arg.target_state = state;
1104 ret = call_on_cpu(pr->id, acpi_processor_throttling_fn, &arg,
1108 * When the T-state coordination is SW_ALL or HW_ALL,
1109 * it is necessary to set T-state for every affected
1112 for_each_cpu_and(i, cpu_online_mask,
1113 p_throttling->shared_cpu_map) {
1114 match_pr = per_cpu(processors, i);
1116 * If the pointer is invalid, we will report the
1117 * error message and continue.
1120 acpi_handle_debug(pr->handle,
1121 "Invalid Pointer for CPU %d\n", i);
1125 * If the throttling control is unsupported on CPU i,
1126 * we will report the error message and continue.
1128 if (!match_pr->flags.throttling) {
1129 acpi_handle_debug(pr->handle,
1130 "Throttling Control unsupported on CPU %d\n", i);
1135 arg.target_state = state;
1137 ret = call_on_cpu(pr->id, acpi_processor_throttling_fn,
1142 * After the set_throttling is called, the
1143 * throttling notifier is called for every
1144 * affected cpu to update the T-states.
1145 * The notifier event is THROTTLING_POSTCHANGE
1147 for_each_cpu_and(i, cpu_online_mask, p_throttling->shared_cpu_map) {
1149 acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE,
1156 int acpi_processor_set_throttling(struct acpi_processor *pr, int state,
1159 return __acpi_processor_set_throttling(pr, state, force, false);
1162 int acpi_processor_get_throttling_info(struct acpi_processor *pr)
1165 struct acpi_processor_throttling *pthrottling;
1167 acpi_handle_debug(pr->handle,
1168 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1169 pr->throttling.address,
1170 pr->throttling.duty_offset,
1171 pr->throttling.duty_width);
1174 * Evaluate _PTC, _TSS and _TPC
1175 * They must all be present or none of them can be used.
1177 if (acpi_processor_get_throttling_control(pr) ||
1178 acpi_processor_get_throttling_states(pr) ||
1179 acpi_processor_get_platform_limit(pr)) {
1180 pr->throttling.acpi_processor_get_throttling =
1181 &acpi_processor_get_throttling_fadt;
1182 pr->throttling.acpi_processor_set_throttling =
1183 &acpi_processor_set_throttling_fadt;
1184 if (acpi_processor_get_fadt_info(pr))
1187 pr->throttling.acpi_processor_get_throttling =
1188 &acpi_processor_get_throttling_ptc;
1189 pr->throttling.acpi_processor_set_throttling =
1190 &acpi_processor_set_throttling_ptc;
1194 * If TSD package for one CPU can't be parsed successfully, it means
1195 * that this CPU will have no coordination with other CPUs.
1197 if (acpi_processor_get_tsd(pr)) {
1198 pthrottling = &pr->throttling;
1199 pthrottling->tsd_valid_flag = 0;
1200 cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
1201 pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
1205 * PIIX4 Errata: We don't support throttling on the original PIIX4.
1206 * This shouldn't be an issue as few (if any) mobile systems ever
1209 if (errata.piix4.throttle) {
1210 acpi_handle_debug(pr->handle,
1211 "Throttling not supported on PIIX4 A- or B-step\n");
1215 acpi_handle_debug(pr->handle, "Found %d throttling states\n",
1216 pr->throttling.state_count);
1218 pr->flags.throttling = 1;
1221 * Disable throttling (if enabled). We'll let subsequent policy (e.g.
1222 * thermal) decide to lower performance if it so chooses, but for now
1223 * we'll crank up the speed.
1226 result = acpi_processor_get_throttling(pr);
1230 if (pr->throttling.state) {
1231 acpi_handle_debug(pr->handle,
1232 "Disabling throttling (was T%d)\n",
1233 pr->throttling.state);
1234 result = acpi_processor_set_throttling(pr, 0, false);
1241 pr->flags.throttling = 0;