processor_thermal.c 6.8 KB

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