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