processor_thermal.c 8.3 KB

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