processor_thermal.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 || cpufreq_get_policy(&policy, cpu))
  87. return 0;
  88. return 1;
  89. }
  90. static int acpi_thermal_cpufreq_increase(unsigned int cpu)
  91. {
  92. if (!cpu_has_cpufreq(cpu))
  93. return -ENODEV;
  94. if (cpufreq_thermal_reduction_pctg[cpu] < 60) {
  95. cpufreq_thermal_reduction_pctg[cpu] += 20;
  96. cpufreq_update_policy(cpu);
  97. return 0;
  98. }
  99. return -ERANGE;
  100. }
  101. static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
  102. {
  103. if (!cpu_has_cpufreq(cpu))
  104. return -ENODEV;
  105. if (cpufreq_thermal_reduction_pctg[cpu] > 20)
  106. cpufreq_thermal_reduction_pctg[cpu] -= 20;
  107. else
  108. cpufreq_thermal_reduction_pctg[cpu] = 0;
  109. cpufreq_update_policy(cpu);
  110. /* We reached max freq again and can leave passive mode */
  111. return !cpufreq_thermal_reduction_pctg[cpu];
  112. }
  113. static int acpi_thermal_cpufreq_notifier(struct notifier_block *nb,
  114. unsigned long event, void *data)
  115. {
  116. struct cpufreq_policy *policy = data;
  117. unsigned long max_freq = 0;
  118. if (event != CPUFREQ_ADJUST)
  119. goto out;
  120. max_freq =
  121. (policy->cpuinfo.max_freq *
  122. (100 - cpufreq_thermal_reduction_pctg[policy->cpu])) / 100;
  123. cpufreq_verify_within_limits(policy, 0, max_freq);
  124. out:
  125. return 0;
  126. }
  127. static struct notifier_block acpi_thermal_cpufreq_notifier_block = {
  128. .notifier_call = acpi_thermal_cpufreq_notifier,
  129. };
  130. void acpi_thermal_cpufreq_init(void)
  131. {
  132. int i;
  133. for (i = 0; i < NR_CPUS; i++)
  134. cpufreq_thermal_reduction_pctg[i] = 0;
  135. i = cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block,
  136. CPUFREQ_POLICY_NOTIFIER);
  137. if (!i)
  138. acpi_thermal_cpufreq_is_init = 1;
  139. }
  140. void acpi_thermal_cpufreq_exit(void)
  141. {
  142. if (acpi_thermal_cpufreq_is_init)
  143. cpufreq_unregister_notifier
  144. (&acpi_thermal_cpufreq_notifier_block,
  145. CPUFREQ_POLICY_NOTIFIER);
  146. acpi_thermal_cpufreq_is_init = 0;
  147. }
  148. #else /* ! CONFIG_CPU_FREQ */
  149. static int acpi_thermal_cpufreq_increase(unsigned int cpu)
  150. {
  151. return -ENODEV;
  152. }
  153. static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
  154. {
  155. return -ENODEV;
  156. }
  157. #endif
  158. int acpi_processor_set_thermal_limit(acpi_handle handle, int type)
  159. {
  160. int result = 0;
  161. struct acpi_processor *pr = NULL;
  162. struct acpi_device *device = NULL;
  163. int tx = 0, max_tx_px = 0;
  164. ACPI_FUNCTION_TRACE("acpi_processor_set_thermal_limit");
  165. if ((type < ACPI_PROCESSOR_LIMIT_NONE)
  166. || (type > ACPI_PROCESSOR_LIMIT_DECREMENT))
  167. return_VALUE(-EINVAL);
  168. result = acpi_bus_get_device(handle, &device);
  169. if (result)
  170. return_VALUE(result);
  171. pr = (struct acpi_processor *)acpi_driver_data(device);
  172. if (!pr)
  173. return_VALUE(-ENODEV);
  174. /* Thermal limits are always relative to the current Px/Tx state. */
  175. if (pr->flags.throttling)
  176. pr->limit.thermal.tx = pr->throttling.state;
  177. /*
  178. * Our default policy is to only use throttling at the lowest
  179. * performance state.
  180. */
  181. tx = pr->limit.thermal.tx;
  182. switch (type) {
  183. case ACPI_PROCESSOR_LIMIT_NONE:
  184. do {
  185. result = acpi_thermal_cpufreq_decrease(pr->id);
  186. } while (!result);
  187. tx = 0;
  188. break;
  189. case ACPI_PROCESSOR_LIMIT_INCREMENT:
  190. /* if going up: P-states first, T-states later */
  191. result = acpi_thermal_cpufreq_increase(pr->id);
  192. if (!result)
  193. goto end;
  194. else if (result == -ERANGE)
  195. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  196. "At maximum performance state\n"));
  197. if (pr->flags.throttling) {
  198. if (tx == (pr->throttling.state_count - 1))
  199. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  200. "At maximum throttling state\n"));
  201. else
  202. tx++;
  203. }
  204. break;
  205. case ACPI_PROCESSOR_LIMIT_DECREMENT:
  206. /* if going down: T-states first, P-states later */
  207. if (pr->flags.throttling) {
  208. if (tx == 0) {
  209. max_tx_px = 1;
  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) {
  219. /*
  220. * We only could get -ERANGE, 1 or 0.
  221. * In the first two cases we reached max freq again.
  222. */
  223. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  224. "At minimum performance state\n"));
  225. max_tx_px = 1;
  226. } else
  227. max_tx_px = 0;
  228. break;
  229. }
  230. end:
  231. if (pr->flags.throttling) {
  232. pr->limit.thermal.px = 0;
  233. pr->limit.thermal.tx = tx;
  234. result = acpi_processor_apply_limit(pr);
  235. if (result)
  236. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  237. "Unable to set thermal limit\n"));
  238. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Thermal limit now (P%d:T%d)\n",
  239. pr->limit.thermal.px, pr->limit.thermal.tx));
  240. } else
  241. result = 0;
  242. if (max_tx_px)
  243. return_VALUE(1);
  244. else
  245. return_VALUE(result);
  246. }
  247. int acpi_processor_get_limit_info(struct acpi_processor *pr)
  248. {
  249. ACPI_FUNCTION_TRACE("acpi_processor_get_limit_info");
  250. if (!pr)
  251. return_VALUE(-EINVAL);
  252. if (pr->flags.throttling)
  253. pr->flags.limit = 1;
  254. return_VALUE(0);
  255. }
  256. /* /proc interface */
  257. static int acpi_processor_limit_seq_show(struct seq_file *seq, void *offset)
  258. {
  259. struct acpi_processor *pr = (struct acpi_processor *)seq->private;
  260. ACPI_FUNCTION_TRACE("acpi_processor_limit_seq_show");
  261. if (!pr)
  262. goto end;
  263. if (!pr->flags.limit) {
  264. seq_puts(seq, "<not supported>\n");
  265. goto end;
  266. }
  267. seq_printf(seq, "active limit: P%d:T%d\n"
  268. "user limit: P%d:T%d\n"
  269. "thermal limit: P%d:T%d\n",
  270. pr->limit.state.px, pr->limit.state.tx,
  271. pr->limit.user.px, pr->limit.user.tx,
  272. pr->limit.thermal.px, pr->limit.thermal.tx);
  273. end:
  274. return_VALUE(0);
  275. }
  276. static int acpi_processor_limit_open_fs(struct inode *inode, struct file *file)
  277. {
  278. return single_open(file, acpi_processor_limit_seq_show,
  279. PDE(inode)->data);
  280. }
  281. static ssize_t acpi_processor_write_limit(struct file * file,
  282. const char __user * buffer,
  283. size_t count, loff_t * data)
  284. {
  285. int result = 0;
  286. struct seq_file *m = (struct seq_file *)file->private_data;
  287. struct acpi_processor *pr = (struct acpi_processor *)m->private;
  288. char limit_string[25] = { '\0' };
  289. int px = 0;
  290. int tx = 0;
  291. ACPI_FUNCTION_TRACE("acpi_processor_write_limit");
  292. if (!pr || (count > sizeof(limit_string) - 1)) {
  293. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid argument\n"));
  294. return_VALUE(-EINVAL);
  295. }
  296. if (copy_from_user(limit_string, buffer, count)) {
  297. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data\n"));
  298. return_VALUE(-EFAULT);
  299. }
  300. limit_string[count] = '\0';
  301. if (sscanf(limit_string, "%d:%d", &px, &tx) != 2) {
  302. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data format\n"));
  303. return_VALUE(-EINVAL);
  304. }
  305. if (pr->flags.throttling) {
  306. if ((tx < 0) || (tx > (pr->throttling.state_count - 1))) {
  307. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid tx\n"));
  308. return_VALUE(-EINVAL);
  309. }
  310. pr->limit.user.tx = tx;
  311. }
  312. result = acpi_processor_apply_limit(pr);
  313. return_VALUE(count);
  314. }
  315. struct file_operations acpi_processor_limit_fops = {
  316. .open = acpi_processor_limit_open_fs,
  317. .read = seq_read,
  318. .write = acpi_processor_write_limit,
  319. .llseek = seq_lseek,
  320. .release = single_release,
  321. };