1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * processor_thermal.c - Passive cooling submodule of the ACPI processor driver
9 * - Added processor hotplug support
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/cpufreq.h>
16 #include <linux/acpi.h>
17 #include <acpi/processor.h>
18 #include <linux/uaccess.h>
20 #define PREFIX "ACPI: "
22 #ifdef CONFIG_CPU_FREQ
24 /* If a passive cooling situation is detected, primarily CPUfreq is used, as it
25 * offers (in most cases) voltage scaling in addition to frequency scaling, and
26 * thus a cubic (instead of linear) reduction of energy. Also, we allow for
27 * _any_ cpufreq driver and not only the acpi-cpufreq driver.
30 #define CPUFREQ_THERMAL_MIN_STEP 0
31 #define CPUFREQ_THERMAL_MAX_STEP 3
33 static DEFINE_PER_CPU(unsigned int, cpufreq_thermal_reduction_pctg);
35 #define reduction_pctg(cpu) \
36 per_cpu(cpufreq_thermal_reduction_pctg, phys_package_first_cpu(cpu))
39 * Emulate "per package data" using per cpu data (which should really be
42 * Note we can lose a CPU on cpu hotunplug, in this case we forget the state
43 * temporarily. Fortunately that's not a big issue here (I hope)
45 static int phys_package_first_cpu(int cpu)
48 int id = topology_physical_package_id(cpu);
50 for_each_online_cpu(i)
51 if (topology_physical_package_id(i) == id)
56 static int cpu_has_cpufreq(unsigned int cpu)
58 struct cpufreq_policy policy;
59 if (!acpi_processor_cpufreq_init || cpufreq_get_policy(&policy, cpu))
64 static int cpufreq_get_max_state(unsigned int cpu)
66 if (!cpu_has_cpufreq(cpu))
69 return CPUFREQ_THERMAL_MAX_STEP;
72 static int cpufreq_get_cur_state(unsigned int cpu)
74 if (!cpu_has_cpufreq(cpu))
77 return reduction_pctg(cpu);
80 static int cpufreq_set_cur_state(unsigned int cpu, int state)
82 struct cpufreq_policy *policy;
83 struct acpi_processor *pr;
84 unsigned long max_freq;
87 if (!cpu_has_cpufreq(cpu))
90 reduction_pctg(cpu) = state;
93 * Update all the CPUs in the same package because they all
94 * contribute to the temperature and often share the same
97 for_each_online_cpu(i) {
98 if (topology_physical_package_id(i) !=
99 topology_physical_package_id(cpu))
102 pr = per_cpu(processors, i);
104 if (unlikely(!freq_qos_request_active(&pr->thermal_req)))
107 policy = cpufreq_cpu_get(i);
111 max_freq = (policy->cpuinfo.max_freq * (100 - reduction_pctg(i) * 20)) / 100;
113 cpufreq_cpu_put(policy);
115 ret = freq_qos_update_request(&pr->thermal_req, max_freq);
117 pr_warn("Failed to update thermal freq constraint: CPU%d (%d)\n",
124 void acpi_thermal_cpufreq_init(struct cpufreq_policy *policy)
128 for_each_cpu(cpu, policy->related_cpus) {
129 struct acpi_processor *pr = per_cpu(processors, cpu);
135 ret = freq_qos_add_request(&policy->constraints,
137 FREQ_QOS_MAX, INT_MAX);
139 pr_err("Failed to add freq constraint for CPU%d (%d)\n",
144 void acpi_thermal_cpufreq_exit(struct cpufreq_policy *policy)
148 for_each_cpu(cpu, policy->related_cpus) {
149 struct acpi_processor *pr = per_cpu(processors, policy->cpu);
152 freq_qos_remove_request(&pr->thermal_req);
155 #else /* ! CONFIG_CPU_FREQ */
156 static int cpufreq_get_max_state(unsigned int cpu)
161 static int cpufreq_get_cur_state(unsigned int cpu)
166 static int cpufreq_set_cur_state(unsigned int cpu, int state)
173 /* thermal cooling device callbacks */
174 static int acpi_processor_max_state(struct acpi_processor *pr)
179 * There exists four states according to
180 * cpufreq_thermal_reduction_pctg. 0, 1, 2, 3
182 max_state += cpufreq_get_max_state(pr->id);
183 if (pr->flags.throttling)
184 max_state += (pr->throttling.state_count -1);
189 processor_get_max_state(struct thermal_cooling_device *cdev,
190 unsigned long *state)
192 struct acpi_device *device = cdev->devdata;
193 struct acpi_processor *pr;
198 pr = acpi_driver_data(device);
202 *state = acpi_processor_max_state(pr);
207 processor_get_cur_state(struct thermal_cooling_device *cdev,
208 unsigned long *cur_state)
210 struct acpi_device *device = cdev->devdata;
211 struct acpi_processor *pr;
216 pr = acpi_driver_data(device);
220 *cur_state = cpufreq_get_cur_state(pr->id);
221 if (pr->flags.throttling)
222 *cur_state += pr->throttling.state;
227 processor_set_cur_state(struct thermal_cooling_device *cdev,
230 struct acpi_device *device = cdev->devdata;
231 struct acpi_processor *pr;
238 pr = acpi_driver_data(device);
242 max_pstate = cpufreq_get_max_state(pr->id);
244 if (state > acpi_processor_max_state(pr))
247 if (state <= max_pstate) {
248 if (pr->flags.throttling && pr->throttling.state)
249 result = acpi_processor_set_throttling(pr, 0, false);
250 cpufreq_set_cur_state(pr->id, state);
252 cpufreq_set_cur_state(pr->id, max_pstate);
253 result = acpi_processor_set_throttling(pr,
254 state - max_pstate, false);
259 const struct thermal_cooling_device_ops processor_cooling_ops = {
260 .get_max_state = processor_get_max_state,
261 .get_cur_state = processor_get_cur_state,
262 .set_cur_state = processor_set_cur_state,