processor_thermal.c 9.5 KB

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