processor_thermal.c 6.5 KB

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