processor_thermal.c 9.4 KB

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