processor_thermal.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * processor_thermal.c - Passive cooling submodule of the ACPI processor driver
  3. *
  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
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. *
  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.
  16. *
  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.
  21. *
  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.
  25. *
  26. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27. */
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/init.h>
  31. #include <linux/cpufreq.h>
  32. #include <linux/acpi.h>
  33. #include <acpi/processor.h>
  34. #include <asm/uaccess.h>
  35. #define PREFIX "ACPI: "
  36. #define ACPI_PROCESSOR_CLASS "processor"
  37. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  38. ACPI_MODULE_NAME("processor_thermal");
  39. #ifdef CONFIG_CPU_FREQ
  40. /* If a passive cooling situation is detected, primarily CPUfreq is used, as it
  41. * offers (in most cases) voltage scaling in addition to frequency scaling, and
  42. * thus a cubic (instead of linear) reduction of energy. Also, we allow for
  43. * _any_ cpufreq driver and not only the acpi-cpufreq driver.
  44. */
  45. #define CPUFREQ_THERMAL_MIN_STEP 0
  46. #define CPUFREQ_THERMAL_MAX_STEP 3
  47. static DEFINE_PER_CPU(unsigned int, cpufreq_thermal_reduction_pctg);
  48. static unsigned int acpi_thermal_cpufreq_is_init = 0;
  49. #define reduction_pctg(cpu) \
  50. per_cpu(cpufreq_thermal_reduction_pctg, phys_package_first_cpu(cpu))
  51. /*
  52. * Emulate "per package data" using per cpu data (which should really be
  53. * provided elsewhere)
  54. *
  55. * Note we can lose a CPU on cpu hotunplug, in this case we forget the state
  56. * temporarily. Fortunately that's not a big issue here (I hope)
  57. */
  58. static int phys_package_first_cpu(int cpu)
  59. {
  60. int i;
  61. int id = topology_physical_package_id(cpu);
  62. for_each_online_cpu(i)
  63. if (topology_physical_package_id(i) == id)
  64. return i;
  65. return 0;
  66. }
  67. static int cpu_has_cpufreq(unsigned int cpu)
  68. {
  69. struct cpufreq_policy policy;
  70. if (!acpi_thermal_cpufreq_is_init || cpufreq_get_policy(&policy, cpu))
  71. return 0;
  72. return 1;
  73. }
  74. static int acpi_thermal_cpufreq_notifier(struct notifier_block *nb,
  75. unsigned long event, void *data)
  76. {
  77. struct cpufreq_policy *policy = data;
  78. unsigned long max_freq = 0;
  79. if (event != CPUFREQ_ADJUST)
  80. goto out;
  81. max_freq = (
  82. policy->cpuinfo.max_freq *
  83. (100 - reduction_pctg(policy->cpu) * 20)
  84. ) / 100;
  85. cpufreq_verify_within_limits(policy, 0, max_freq);
  86. out:
  87. return 0;
  88. }
  89. static struct notifier_block acpi_thermal_cpufreq_notifier_block = {
  90. .notifier_call = acpi_thermal_cpufreq_notifier,
  91. };
  92. static int cpufreq_get_max_state(unsigned int cpu)
  93. {
  94. if (!cpu_has_cpufreq(cpu))
  95. return 0;
  96. return CPUFREQ_THERMAL_MAX_STEP;
  97. }
  98. static int cpufreq_get_cur_state(unsigned int cpu)
  99. {
  100. if (!cpu_has_cpufreq(cpu))
  101. return 0;
  102. return reduction_pctg(cpu);
  103. }
  104. static int cpufreq_set_cur_state(unsigned int cpu, int state)
  105. {
  106. int i;
  107. if (!cpu_has_cpufreq(cpu))
  108. return 0;
  109. reduction_pctg(cpu) = state;
  110. /*
  111. * Update all the CPUs in the same package because they all
  112. * contribute to the temperature and often share the same
  113. * frequency.
  114. */
  115. for_each_online_cpu(i) {
  116. if (topology_physical_package_id(i) ==
  117. topology_physical_package_id(cpu))
  118. cpufreq_update_policy(i);
  119. }
  120. return 0;
  121. }
  122. void acpi_thermal_cpufreq_init(void)
  123. {
  124. int i;
  125. i = cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block,
  126. CPUFREQ_POLICY_NOTIFIER);
  127. if (!i)
  128. acpi_thermal_cpufreq_is_init = 1;
  129. }
  130. void acpi_thermal_cpufreq_exit(void)
  131. {
  132. if (acpi_thermal_cpufreq_is_init)
  133. cpufreq_unregister_notifier
  134. (&acpi_thermal_cpufreq_notifier_block,
  135. CPUFREQ_POLICY_NOTIFIER);
  136. acpi_thermal_cpufreq_is_init = 0;
  137. }
  138. #else /* ! CONFIG_CPU_FREQ */
  139. static int cpufreq_get_max_state(unsigned int cpu)
  140. {
  141. return 0;
  142. }
  143. static int cpufreq_get_cur_state(unsigned int cpu)
  144. {
  145. return 0;
  146. }
  147. static int cpufreq_set_cur_state(unsigned int cpu, int state)
  148. {
  149. return 0;
  150. }
  151. #endif
  152. /* thermal cooling device callbacks */
  153. static int acpi_processor_max_state(struct acpi_processor *pr)
  154. {
  155. int max_state = 0;
  156. /*
  157. * There exists four states according to
  158. * cpufreq_thermal_reduction_pctg. 0, 1, 2, 3
  159. */
  160. max_state += cpufreq_get_max_state(pr->id);
  161. if (pr->flags.throttling)
  162. max_state += (pr->throttling.state_count -1);
  163. return max_state;
  164. }
  165. static int
  166. processor_get_max_state(struct thermal_cooling_device *cdev,
  167. unsigned long *state)
  168. {
  169. struct acpi_device *device = cdev->devdata;
  170. struct acpi_processor *pr;
  171. if (!device)
  172. return -EINVAL;
  173. pr = acpi_driver_data(device);
  174. if (!pr)
  175. return -EINVAL;
  176. *state = acpi_processor_max_state(pr);
  177. return 0;
  178. }
  179. static int
  180. processor_get_cur_state(struct thermal_cooling_device *cdev,
  181. unsigned long *cur_state)
  182. {
  183. struct acpi_device *device = cdev->devdata;
  184. struct acpi_processor *pr;
  185. if (!device)
  186. return -EINVAL;
  187. pr = acpi_driver_data(device);
  188. if (!pr)
  189. return -EINVAL;
  190. *cur_state = cpufreq_get_cur_state(pr->id);
  191. if (pr->flags.throttling)
  192. *cur_state += pr->throttling.state;
  193. return 0;
  194. }
  195. static int
  196. processor_set_cur_state(struct thermal_cooling_device *cdev,
  197. unsigned long state)
  198. {
  199. struct acpi_device *device = cdev->devdata;
  200. struct acpi_processor *pr;
  201. int result = 0;
  202. int max_pstate;
  203. if (!device)
  204. return -EINVAL;
  205. pr = acpi_driver_data(device);
  206. if (!pr)
  207. return -EINVAL;
  208. max_pstate = cpufreq_get_max_state(pr->id);
  209. if (state > acpi_processor_max_state(pr))
  210. return -EINVAL;
  211. if (state <= max_pstate) {
  212. if (pr->flags.throttling && pr->throttling.state)
  213. result = acpi_processor_set_throttling(pr, 0, false);
  214. cpufreq_set_cur_state(pr->id, state);
  215. } else {
  216. cpufreq_set_cur_state(pr->id, max_pstate);
  217. result = acpi_processor_set_throttling(pr,
  218. state - max_pstate, false);
  219. }
  220. return result;
  221. }
  222. const struct thermal_cooling_device_ops processor_cooling_ops = {
  223. .get_max_state = processor_get_max_state,
  224. .get_cur_state = processor_get_cur_state,
  225. .set_cur_state = processor_set_cur_state,
  226. };