processor_thermal.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. static int cpu_has_cpufreq(unsigned int cpu)
  52. {
  53. struct cpufreq_policy policy;
  54. if (!acpi_thermal_cpufreq_is_init || cpufreq_get_policy(&policy, cpu))
  55. return 0;
  56. return 1;
  57. }
  58. static int acpi_thermal_cpufreq_notifier(struct notifier_block *nb,
  59. unsigned long event, void *data)
  60. {
  61. struct cpufreq_policy *policy = data;
  62. unsigned long max_freq = 0;
  63. if (event != CPUFREQ_ADJUST)
  64. goto out;
  65. max_freq = (
  66. policy->cpuinfo.max_freq *
  67. (100 - per_cpu(cpufreq_thermal_reduction_pctg, policy->cpu) * 20)
  68. ) / 100;
  69. cpufreq_verify_within_limits(policy, 0, max_freq);
  70. out:
  71. return 0;
  72. }
  73. static struct notifier_block acpi_thermal_cpufreq_notifier_block = {
  74. .notifier_call = acpi_thermal_cpufreq_notifier,
  75. };
  76. static int cpufreq_get_max_state(unsigned int cpu)
  77. {
  78. if (!cpu_has_cpufreq(cpu))
  79. return 0;
  80. return CPUFREQ_THERMAL_MAX_STEP;
  81. }
  82. static int cpufreq_get_cur_state(unsigned int cpu)
  83. {
  84. if (!cpu_has_cpufreq(cpu))
  85. return 0;
  86. return per_cpu(cpufreq_thermal_reduction_pctg, cpu);
  87. }
  88. static int cpufreq_set_cur_state(unsigned int cpu, int state)
  89. {
  90. if (!cpu_has_cpufreq(cpu))
  91. return 0;
  92. per_cpu(cpufreq_thermal_reduction_pctg, cpu) = state;
  93. cpufreq_update_policy(cpu);
  94. return 0;
  95. }
  96. void acpi_thermal_cpufreq_init(void)
  97. {
  98. int i;
  99. for (i = 0; i < nr_cpu_ids; i++)
  100. if (cpu_present(i))
  101. per_cpu(cpufreq_thermal_reduction_pctg, i) = 0;
  102. i = cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block,
  103. CPUFREQ_POLICY_NOTIFIER);
  104. if (!i)
  105. acpi_thermal_cpufreq_is_init = 1;
  106. }
  107. void acpi_thermal_cpufreq_exit(void)
  108. {
  109. if (acpi_thermal_cpufreq_is_init)
  110. cpufreq_unregister_notifier
  111. (&acpi_thermal_cpufreq_notifier_block,
  112. CPUFREQ_POLICY_NOTIFIER);
  113. acpi_thermal_cpufreq_is_init = 0;
  114. }
  115. #else /* ! CONFIG_CPU_FREQ */
  116. static int cpufreq_get_max_state(unsigned int cpu)
  117. {
  118. return 0;
  119. }
  120. static int cpufreq_get_cur_state(unsigned int cpu)
  121. {
  122. return 0;
  123. }
  124. static int cpufreq_set_cur_state(unsigned int cpu, int state)
  125. {
  126. return 0;
  127. }
  128. #endif
  129. int acpi_processor_get_limit_info(struct acpi_processor *pr)
  130. {
  131. if (!pr)
  132. return -EINVAL;
  133. if (pr->flags.throttling)
  134. pr->flags.limit = 1;
  135. return 0;
  136. }
  137. /* thermal coolign device callbacks */
  138. static int acpi_processor_max_state(struct acpi_processor *pr)
  139. {
  140. int max_state = 0;
  141. /*
  142. * There exists four states according to
  143. * cpufreq_thermal_reduction_ptg. 0, 1, 2, 3
  144. */
  145. max_state += cpufreq_get_max_state(pr->id);
  146. if (pr->flags.throttling)
  147. max_state += (pr->throttling.state_count -1);
  148. return max_state;
  149. }
  150. static int
  151. processor_get_max_state(struct thermal_cooling_device *cdev,
  152. unsigned long *state)
  153. {
  154. struct acpi_device *device = cdev->devdata;
  155. struct acpi_processor *pr = acpi_driver_data(device);
  156. if (!device || !pr)
  157. return -EINVAL;
  158. *state = acpi_processor_max_state(pr);
  159. return 0;
  160. }
  161. static int
  162. processor_get_cur_state(struct thermal_cooling_device *cdev,
  163. unsigned long *cur_state)
  164. {
  165. struct acpi_device *device = cdev->devdata;
  166. struct acpi_processor *pr = acpi_driver_data(device);
  167. if (!device || !pr)
  168. return -EINVAL;
  169. *cur_state = cpufreq_get_cur_state(pr->id);
  170. if (pr->flags.throttling)
  171. *cur_state += pr->throttling.state;
  172. return 0;
  173. }
  174. static int
  175. processor_set_cur_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. int result = 0;
  181. int max_pstate;
  182. if (!device || !pr)
  183. return -EINVAL;
  184. max_pstate = cpufreq_get_max_state(pr->id);
  185. if (state > acpi_processor_max_state(pr))
  186. return -EINVAL;
  187. if (state <= max_pstate) {
  188. if (pr->flags.throttling && pr->throttling.state)
  189. result = acpi_processor_set_throttling(pr, 0, false);
  190. cpufreq_set_cur_state(pr->id, state);
  191. } else {
  192. cpufreq_set_cur_state(pr->id, max_pstate);
  193. result = acpi_processor_set_throttling(pr,
  194. state - max_pstate, false);
  195. }
  196. return result;
  197. }
  198. const struct thermal_cooling_device_ops processor_cooling_ops = {
  199. .get_max_state = processor_get_max_state,
  200. .get_cur_state = processor_get_cur_state,
  201. .set_cur_state = processor_set_cur_state,
  202. };