processor_thermal.c 6.8 KB

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