processor_thermal.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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/proc_fs.h>
  33. #include <linux/seq_file.h>
  34. #include <asm/uaccess.h>
  35. #include <acpi/acpi_bus.h>
  36. #include <acpi/processor.h>
  37. #include <acpi/acpi_drivers.h>
  38. #define ACPI_PROCESSOR_COMPONENT 0x01000000
  39. #define ACPI_PROCESSOR_CLASS "processor"
  40. #define ACPI_PROCESSOR_DRIVER_NAME "ACPI Processor Driver"
  41. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  42. ACPI_MODULE_NAME("acpi_processor")
  43. /* --------------------------------------------------------------------------
  44. Limit Interface
  45. -------------------------------------------------------------------------- */
  46. static int acpi_processor_apply_limit(struct acpi_processor *pr)
  47. {
  48. int result = 0;
  49. u16 px = 0;
  50. u16 tx = 0;
  51. ACPI_FUNCTION_TRACE("acpi_processor_apply_limit");
  52. if (!pr)
  53. return_VALUE(-EINVAL);
  54. if (!pr->flags.limit)
  55. return_VALUE(-ENODEV);
  56. if (pr->flags.throttling) {
  57. if (pr->limit.user.tx > tx)
  58. tx = pr->limit.user.tx;
  59. if (pr->limit.thermal.tx > tx)
  60. tx = pr->limit.thermal.tx;
  61. result = acpi_processor_set_throttling(pr, tx);
  62. if (result)
  63. goto end;
  64. }
  65. pr->limit.state.px = px;
  66. pr->limit.state.tx = tx;
  67. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  68. "Processor [%d] limit set to (P%d:T%d)\n", pr->id,
  69. pr->limit.state.px, pr->limit.state.tx));
  70. end:
  71. if (result)
  72. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unable to set limit\n"));
  73. return_VALUE(result);
  74. }
  75. #ifdef CONFIG_CPU_FREQ
  76. /* If a passive cooling situation is detected, primarily CPUfreq is used, as it
  77. * offers (in most cases) voltage scaling in addition to frequency scaling, and
  78. * thus a cubic (instead of linear) reduction of energy. Also, we allow for
  79. * _any_ cpufreq driver and not only the acpi-cpufreq driver.
  80. */
  81. static unsigned int cpufreq_thermal_reduction_pctg[NR_CPUS];
  82. static unsigned int acpi_thermal_cpufreq_is_init = 0;
  83. static int cpu_has_cpufreq(unsigned int cpu)
  84. {
  85. struct cpufreq_policy policy;
  86. if (!acpi_thermal_cpufreq_is_init)
  87. return -ENODEV;
  88. if (!cpufreq_get_policy(&policy, cpu))
  89. return -ENODEV;
  90. return 0;
  91. }
  92. static int acpi_thermal_cpufreq_increase(unsigned int cpu)
  93. {
  94. if (!cpu_has_cpufreq(cpu))
  95. return -ENODEV;
  96. if (cpufreq_thermal_reduction_pctg[cpu] < 60) {
  97. cpufreq_thermal_reduction_pctg[cpu] += 20;
  98. cpufreq_update_policy(cpu);
  99. return 0;
  100. }
  101. return -ERANGE;
  102. }
  103. static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
  104. {
  105. if (!cpu_has_cpufreq(cpu))
  106. return -ENODEV;
  107. if (cpufreq_thermal_reduction_pctg[cpu] >= 20) {
  108. cpufreq_thermal_reduction_pctg[cpu] -= 20;
  109. cpufreq_update_policy(cpu);
  110. return 0;
  111. }
  112. return -ERANGE;
  113. }
  114. static int acpi_thermal_cpufreq_notifier(struct notifier_block *nb,
  115. unsigned long event, void *data)
  116. {
  117. struct cpufreq_policy *policy = data;
  118. unsigned long max_freq = 0;
  119. if (event != CPUFREQ_ADJUST)
  120. goto out;
  121. max_freq =
  122. (policy->cpuinfo.max_freq *
  123. (100 - cpufreq_thermal_reduction_pctg[policy->cpu])) / 100;
  124. cpufreq_verify_within_limits(policy, 0, max_freq);
  125. out:
  126. return 0;
  127. }
  128. static struct notifier_block acpi_thermal_cpufreq_notifier_block = {
  129. .notifier_call = acpi_thermal_cpufreq_notifier,
  130. };
  131. void acpi_thermal_cpufreq_init(void)
  132. {
  133. int i;
  134. for (i = 0; i < NR_CPUS; i++)
  135. cpufreq_thermal_reduction_pctg[i] = 0;
  136. i = cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block,
  137. CPUFREQ_POLICY_NOTIFIER);
  138. if (!i)
  139. acpi_thermal_cpufreq_is_init = 1;
  140. }
  141. void acpi_thermal_cpufreq_exit(void)
  142. {
  143. if (acpi_thermal_cpufreq_is_init)
  144. cpufreq_unregister_notifier
  145. (&acpi_thermal_cpufreq_notifier_block,
  146. CPUFREQ_POLICY_NOTIFIER);
  147. acpi_thermal_cpufreq_is_init = 0;
  148. }
  149. #else /* ! CONFIG_CPU_FREQ */
  150. static int acpi_thermal_cpufreq_increase(unsigned int cpu)
  151. {
  152. return -ENODEV;
  153. }
  154. static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
  155. {
  156. return -ENODEV;
  157. }
  158. #endif
  159. int acpi_processor_set_thermal_limit(acpi_handle handle, int type)
  160. {
  161. int result = 0;
  162. struct acpi_processor *pr = NULL;
  163. struct acpi_device *device = NULL;
  164. int tx = 0;
  165. ACPI_FUNCTION_TRACE("acpi_processor_set_thermal_limit");
  166. if ((type < ACPI_PROCESSOR_LIMIT_NONE)
  167. || (type > ACPI_PROCESSOR_LIMIT_DECREMENT))
  168. return_VALUE(-EINVAL);
  169. result = acpi_bus_get_device(handle, &device);
  170. if (result)
  171. return_VALUE(result);
  172. pr = (struct acpi_processor *)acpi_driver_data(device);
  173. if (!pr)
  174. return_VALUE(-ENODEV);
  175. /* Thermal limits are always relative to the current Px/Tx state. */
  176. if (pr->flags.throttling)
  177. pr->limit.thermal.tx = pr->throttling.state;
  178. /*
  179. * Our default policy is to only use throttling at the lowest
  180. * performance state.
  181. */
  182. tx = pr->limit.thermal.tx;
  183. switch (type) {
  184. case ACPI_PROCESSOR_LIMIT_NONE:
  185. do {
  186. result = acpi_thermal_cpufreq_decrease(pr->id);
  187. } while (!result);
  188. tx = 0;
  189. break;
  190. case ACPI_PROCESSOR_LIMIT_INCREMENT:
  191. /* if going up: P-states first, T-states later */
  192. result = acpi_thermal_cpufreq_increase(pr->id);
  193. if (!result)
  194. goto end;
  195. else if (result == -ERANGE)
  196. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  197. "At maximum performance state\n"));
  198. if (pr->flags.throttling) {
  199. if (tx == (pr->throttling.state_count - 1))
  200. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  201. "At maximum throttling state\n"));
  202. else
  203. tx++;
  204. }
  205. break;
  206. case ACPI_PROCESSOR_LIMIT_DECREMENT:
  207. /* if going down: T-states first, P-states later */
  208. if (pr->flags.throttling) {
  209. if (tx == 0)
  210. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  211. "At minimum throttling state\n"));
  212. else {
  213. tx--;
  214. goto end;
  215. }
  216. }
  217. result = acpi_thermal_cpufreq_decrease(pr->id);
  218. if (result == -ERANGE)
  219. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  220. "At minimum performance state\n"));
  221. break;
  222. }
  223. end:
  224. if (pr->flags.throttling) {
  225. pr->limit.thermal.px = 0;
  226. pr->limit.thermal.tx = tx;
  227. result = acpi_processor_apply_limit(pr);
  228. if (result)
  229. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  230. "Unable to set thermal limit\n"));
  231. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Thermal limit now (P%d:T%d)\n",
  232. pr->limit.thermal.px, pr->limit.thermal.tx));
  233. } else
  234. result = 0;
  235. return_VALUE(result);
  236. }
  237. int acpi_processor_get_limit_info(struct acpi_processor *pr)
  238. {
  239. ACPI_FUNCTION_TRACE("acpi_processor_get_limit_info");
  240. if (!pr)
  241. return_VALUE(-EINVAL);
  242. if (pr->flags.throttling)
  243. pr->flags.limit = 1;
  244. return_VALUE(0);
  245. }
  246. /* /proc interface */
  247. static int acpi_processor_limit_seq_show(struct seq_file *seq, void *offset)
  248. {
  249. struct acpi_processor *pr = (struct acpi_processor *)seq->private;
  250. ACPI_FUNCTION_TRACE("acpi_processor_limit_seq_show");
  251. if (!pr)
  252. goto end;
  253. if (!pr->flags.limit) {
  254. seq_puts(seq, "<not supported>\n");
  255. goto end;
  256. }
  257. seq_printf(seq, "active limit: P%d:T%d\n"
  258. "user limit: P%d:T%d\n"
  259. "thermal limit: P%d:T%d\n",
  260. pr->limit.state.px, pr->limit.state.tx,
  261. pr->limit.user.px, pr->limit.user.tx,
  262. pr->limit.thermal.px, pr->limit.thermal.tx);
  263. end:
  264. return_VALUE(0);
  265. }
  266. static int acpi_processor_limit_open_fs(struct inode *inode, struct file *file)
  267. {
  268. return single_open(file, acpi_processor_limit_seq_show,
  269. PDE(inode)->data);
  270. }
  271. ssize_t acpi_processor_write_limit(struct file * file,
  272. const char __user * buffer,
  273. size_t count, loff_t * data)
  274. {
  275. int result = 0;
  276. struct seq_file *m = (struct seq_file *)file->private_data;
  277. struct acpi_processor *pr = (struct acpi_processor *)m->private;
  278. char limit_string[25] = { '\0' };
  279. int px = 0;
  280. int tx = 0;
  281. ACPI_FUNCTION_TRACE("acpi_processor_write_limit");
  282. if (!pr || (count > sizeof(limit_string) - 1)) {
  283. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid argument\n"));
  284. return_VALUE(-EINVAL);
  285. }
  286. if (copy_from_user(limit_string, buffer, count)) {
  287. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data\n"));
  288. return_VALUE(-EFAULT);
  289. }
  290. limit_string[count] = '\0';
  291. if (sscanf(limit_string, "%d:%d", &px, &tx) != 2) {
  292. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data format\n"));
  293. return_VALUE(-EINVAL);
  294. }
  295. if (pr->flags.throttling) {
  296. if ((tx < 0) || (tx > (pr->throttling.state_count - 1))) {
  297. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid tx\n"));
  298. return_VALUE(-EINVAL);
  299. }
  300. pr->limit.user.tx = tx;
  301. }
  302. result = acpi_processor_apply_limit(pr);
  303. return_VALUE(count);
  304. }
  305. struct file_operations acpi_processor_limit_fops = {
  306. .open = acpi_processor_limit_open_fs,
  307. .read = seq_read,
  308. .llseek = seq_lseek,
  309. .release = single_release,
  310. };