1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * processor_thermal.c - Passive cooling submodule of the ACPI processor driver 4 * 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 10 */ 11 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> 19 20 #include "internal.h" 21 22 #ifdef CONFIG_CPU_FREQ 23 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. 28 */ 29 30 #define CPUFREQ_THERMAL_MIN_STEP 0 31 32 static int cpufreq_thermal_max_step __read_mostly = 3; 33 34 /* 35 * Minimum throttle percentage for processor_thermal cooling device. 36 * The processor_thermal driver uses it to calculate the percentage amount by 37 * which cpu frequency must be reduced for each cooling state. This is also used 38 * to calculate the maximum number of throttling steps or cooling states. 39 */ 40 static int cpufreq_thermal_reduction_pctg __read_mostly = 20; 41 42 static DEFINE_PER_CPU(unsigned int, cpufreq_thermal_reduction_step); 43 44 #define reduction_step(cpu) \ 45 per_cpu(cpufreq_thermal_reduction_step, phys_package_first_cpu(cpu)) 46 47 /* 48 * Emulate "per package data" using per cpu data (which should really be 49 * provided elsewhere) 50 * 51 * Note we can lose a CPU on cpu hotunplug, in this case we forget the state 52 * temporarily. Fortunately that's not a big issue here (I hope) 53 */ 54 static int phys_package_first_cpu(int cpu) 55 { 56 int i; 57 int id = topology_physical_package_id(cpu); 58 59 for_each_online_cpu(i) 60 if (topology_physical_package_id(i) == id) 61 return i; 62 return 0; 63 } 64 65 static bool cpu_has_cpufreq(unsigned int cpu) 66 { 67 if (!acpi_processor_cpufreq_init) 68 return 0; 69 70 struct cpufreq_policy *policy __free(put_cpufreq_policy) = cpufreq_cpu_get(cpu); 71 72 return policy != NULL; 73 } 74 75 static int cpufreq_get_max_state(unsigned int cpu) 76 { 77 if (!cpu_has_cpufreq(cpu)) 78 return 0; 79 80 return cpufreq_thermal_max_step; 81 } 82 83 static int cpufreq_get_cur_state(unsigned int cpu) 84 { 85 if (!cpu_has_cpufreq(cpu)) 86 return 0; 87 88 return reduction_step(cpu); 89 } 90 91 static bool cpufreq_update_thermal_limit(unsigned int cpu, struct acpi_processor *pr) 92 { 93 unsigned long max_freq; 94 int ret; 95 96 struct cpufreq_policy *policy __free(put_cpufreq_policy) = cpufreq_cpu_get(cpu); 97 if (!policy) 98 return false; 99 100 max_freq = (policy->cpuinfo.max_freq * 101 (100 - reduction_step(cpu) * cpufreq_thermal_reduction_pctg)) / 100; 102 103 ret = freq_qos_update_request(&pr->thermal_req, max_freq); 104 if (ret < 0) { 105 pr_warn("Failed to update thermal freq constraint: CPU%d (%d)\n", 106 pr->id, ret); 107 } 108 109 return true; 110 } 111 112 static int cpufreq_set_cur_state(unsigned int cpu, int state) 113 { 114 struct acpi_processor *pr; 115 int i; 116 117 if (!cpu_has_cpufreq(cpu)) 118 return 0; 119 120 reduction_step(cpu) = state; 121 122 /* 123 * Update all the CPUs in the same package because they all 124 * contribute to the temperature and often share the same 125 * frequency. 126 */ 127 for_each_online_cpu(i) { 128 if (topology_physical_package_id(i) != 129 topology_physical_package_id(cpu)) 130 continue; 131 132 pr = per_cpu(processors, i); 133 134 if (unlikely(!freq_qos_request_active(&pr->thermal_req))) 135 continue; 136 137 if (!cpufreq_update_thermal_limit(i, pr)) 138 return -EINVAL; 139 } 140 return 0; 141 } 142 143 static void acpi_thermal_cpufreq_config(void) 144 { 145 int cpufreq_pctg = acpi_arch_thermal_cpufreq_pctg(); 146 147 if (!cpufreq_pctg) 148 return; 149 150 cpufreq_thermal_reduction_pctg = cpufreq_pctg; 151 152 /* 153 * Derive the MAX_STEP from minimum throttle percentage so that the reduction 154 * percentage doesn't end up becoming negative. Also, cap the MAX_STEP so that 155 * the CPU performance doesn't become 0. 156 */ 157 cpufreq_thermal_max_step = (100 / cpufreq_pctg) - 2; 158 } 159 160 void acpi_thermal_cpufreq_init(struct cpufreq_policy *policy) 161 { 162 unsigned int cpu; 163 164 acpi_thermal_cpufreq_config(); 165 166 for_each_cpu(cpu, policy->related_cpus) { 167 struct acpi_processor *pr = per_cpu(processors, cpu); 168 int ret; 169 170 if (!pr) 171 continue; 172 173 ret = freq_qos_add_request(&policy->constraints, 174 &pr->thermal_req, 175 FREQ_QOS_MAX, INT_MAX); 176 if (ret < 0) { 177 pr_err("Failed to add freq constraint for CPU%d (%d)\n", 178 cpu, ret); 179 continue; 180 } 181 182 thermal_cooling_device_update(pr->cdev); 183 } 184 } 185 186 void acpi_thermal_cpufreq_exit(struct cpufreq_policy *policy) 187 { 188 unsigned int cpu; 189 190 for_each_cpu(cpu, policy->related_cpus) { 191 struct acpi_processor *pr = per_cpu(processors, cpu); 192 193 if (!pr) 194 continue; 195 196 freq_qos_remove_request(&pr->thermal_req); 197 198 thermal_cooling_device_update(pr->cdev); 199 } 200 } 201 #else /* ! CONFIG_CPU_FREQ */ 202 static int cpufreq_get_max_state(unsigned int cpu) 203 { 204 return 0; 205 } 206 207 static int cpufreq_get_cur_state(unsigned int cpu) 208 { 209 return 0; 210 } 211 212 static int cpufreq_set_cur_state(unsigned int cpu, int state) 213 { 214 return 0; 215 } 216 217 #endif 218 219 /* thermal cooling device callbacks */ 220 static int acpi_processor_max_state(struct acpi_processor *pr) 221 { 222 int max_state = 0; 223 224 /* 225 * There exists four states according to 226 * cpufreq_thermal_reduction_step. 0, 1, 2, 3 227 */ 228 max_state += cpufreq_get_max_state(pr->id); 229 if (pr->flags.throttling) 230 max_state += (pr->throttling.state_count -1); 231 232 return max_state; 233 } 234 static int 235 processor_get_max_state(struct thermal_cooling_device *cdev, 236 unsigned long *state) 237 { 238 struct acpi_processor *pr = cdev->devdata; 239 240 *state = acpi_processor_max_state(pr); 241 return 0; 242 } 243 244 static int 245 processor_get_cur_state(struct thermal_cooling_device *cdev, 246 unsigned long *cur_state) 247 { 248 struct acpi_processor *pr = cdev->devdata; 249 250 *cur_state = cpufreq_get_cur_state(pr->id); 251 if (pr->flags.throttling) 252 *cur_state += pr->throttling.state; 253 return 0; 254 } 255 256 static int 257 processor_set_cur_state(struct thermal_cooling_device *cdev, 258 unsigned long state) 259 { 260 struct acpi_processor *pr = cdev->devdata; 261 int result = 0; 262 int max_pstate; 263 264 max_pstate = cpufreq_get_max_state(pr->id); 265 266 if (state > acpi_processor_max_state(pr)) 267 return -EINVAL; 268 269 if (state <= max_pstate) { 270 if (pr->flags.throttling && pr->throttling.state) 271 result = acpi_processor_set_throttling(pr, 0, false); 272 cpufreq_set_cur_state(pr->id, state); 273 } else { 274 cpufreq_set_cur_state(pr->id, max_pstate); 275 result = acpi_processor_set_throttling(pr, 276 state - max_pstate, false); 277 } 278 return result; 279 } 280 281 const struct thermal_cooling_device_ops processor_cooling_ops = { 282 .get_max_state = processor_get_max_state, 283 .get_cur_state = processor_get_cur_state, 284 .set_cur_state = processor_set_cur_state, 285 }; 286 287 int acpi_processor_thermal_init(struct acpi_processor *pr) 288 { 289 pr->cdev = thermal_cooling_device_create(pr->dev, "Processor", pr, 290 &processor_cooling_ops); 291 if (IS_ERR(pr->cdev)) 292 return PTR_ERR(pr->cdev); 293 294 dev_dbg(pr->dev, "registered as cooling_device%d\n", pr->cdev->id); 295 296 return 0; 297 } 298 299 void acpi_processor_thermal_exit(struct acpi_processor *pr) 300 { 301 if (!IS_ERR_OR_NULL(pr->cdev)) { 302 thermal_cooling_device_unregister(pr->cdev); 303 pr->cdev = NULL; 304 } 305 } 306