processor_thermal.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 <linux/sysdev.h>
  35. #include <asm/uaccess.h>
  36. #include <acpi/acpi_bus.h>
  37. #include <acpi/processor.h>
  38. #include <acpi/acpi_drivers.h>
  39. #define ACPI_PROCESSOR_COMPONENT 0x01000000
  40. #define ACPI_PROCESSOR_CLASS "processor"
  41. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  42. ACPI_MODULE_NAME("processor_thermal");
  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. #define CPUFREQ_THERMAL_MIN_STEP 0
  81. #define CPUFREQ_THERMAL_MAX_STEP 3
  82. static unsigned int cpufreq_thermal_reduction_pctg[NR_CPUS];
  83. static unsigned int acpi_thermal_cpufreq_is_init = 0;
  84. static int cpu_has_cpufreq(unsigned int cpu)
  85. {
  86. struct cpufreq_policy policy;
  87. if (!acpi_thermal_cpufreq_is_init || cpufreq_get_policy(&policy, cpu))
  88. return 0;
  89. return 1;
  90. }
  91. static int acpi_thermal_cpufreq_increase(unsigned int cpu)
  92. {
  93. if (!cpu_has_cpufreq(cpu))
  94. return -ENODEV;
  95. if (cpufreq_thermal_reduction_pctg[cpu] <
  96. CPUFREQ_THERMAL_MAX_STEP) {
  97. cpufreq_thermal_reduction_pctg[cpu]++;
  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] >
  108. (CPUFREQ_THERMAL_MIN_STEP + 1))
  109. cpufreq_thermal_reduction_pctg[cpu]--;
  110. else
  111. cpufreq_thermal_reduction_pctg[cpu] = 0;
  112. cpufreq_update_policy(cpu);
  113. /* We reached max freq again and can leave passive mode */
  114. return !cpufreq_thermal_reduction_pctg[cpu];
  115. }
  116. static int acpi_thermal_cpufreq_notifier(struct notifier_block *nb,
  117. unsigned long event, void *data)
  118. {
  119. struct cpufreq_policy *policy = data;
  120. unsigned long max_freq = 0;
  121. if (event != CPUFREQ_ADJUST)
  122. goto out;
  123. max_freq =
  124. (policy->cpuinfo.max_freq *
  125. (100 - cpufreq_thermal_reduction_pctg[policy->cpu] * 20)) / 100;
  126. cpufreq_verify_within_limits(policy, 0, max_freq);
  127. out:
  128. return 0;
  129. }
  130. static struct notifier_block acpi_thermal_cpufreq_notifier_block = {
  131. .notifier_call = acpi_thermal_cpufreq_notifier,
  132. };
  133. static int cpufreq_get_max_state(unsigned int cpu)
  134. {
  135. if (!cpu_has_cpufreq(cpu))
  136. return 0;
  137. return CPUFREQ_THERMAL_MAX_STEP;
  138. }
  139. static int cpufreq_get_cur_state(unsigned int cpu)
  140. {
  141. if (!cpu_has_cpufreq(cpu))
  142. return 0;
  143. return cpufreq_thermal_reduction_pctg[cpu];
  144. }
  145. static int cpufreq_set_cur_state(unsigned int cpu, int state)
  146. {
  147. if (!cpu_has_cpufreq(cpu))
  148. return 0;
  149. cpufreq_thermal_reduction_pctg[cpu] = state;
  150. cpufreq_update_policy(cpu);
  151. return 0;
  152. }
  153. void acpi_thermal_cpufreq_init(void)
  154. {
  155. int i;
  156. for (i = 0; i < NR_CPUS; i++)
  157. cpufreq_thermal_reduction_pctg[i] = 0;
  158. i = cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block,
  159. CPUFREQ_POLICY_NOTIFIER);
  160. if (!i)
  161. acpi_thermal_cpufreq_is_init = 1;
  162. }
  163. void acpi_thermal_cpufreq_exit(void)
  164. {
  165. if (acpi_thermal_cpufreq_is_init)
  166. cpufreq_unregister_notifier
  167. (&acpi_thermal_cpufreq_notifier_block,
  168. CPUFREQ_POLICY_NOTIFIER);
  169. acpi_thermal_cpufreq_is_init = 0;
  170. }
  171. #else /* ! CONFIG_CPU_FREQ */
  172. static int cpufreq_get_max_state(unsigned int cpu)
  173. {
  174. return 0;
  175. }
  176. static int cpufreq_get_cur_state(unsigned int cpu)
  177. {
  178. return 0;
  179. }
  180. static int cpufreq_set_cur_state(unsigned int cpu, int state)
  181. {
  182. return 0;
  183. }
  184. static int acpi_thermal_cpufreq_increase(unsigned int cpu)
  185. {
  186. return -ENODEV;
  187. }
  188. static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
  189. {
  190. return -ENODEV;
  191. }
  192. #endif
  193. int acpi_processor_set_thermal_limit(acpi_handle handle, int type)
  194. {
  195. int result = 0;
  196. struct acpi_processor *pr = NULL;
  197. struct acpi_device *device = NULL;
  198. int tx = 0, max_tx_px = 0;
  199. if ((type < ACPI_PROCESSOR_LIMIT_NONE)
  200. || (type > ACPI_PROCESSOR_LIMIT_DECREMENT))
  201. return -EINVAL;
  202. result = acpi_bus_get_device(handle, &device);
  203. if (result)
  204. return result;
  205. pr = acpi_driver_data(device);
  206. if (!pr)
  207. return -ENODEV;
  208. /* Thermal limits are always relative to the current Px/Tx state. */
  209. if (pr->flags.throttling)
  210. pr->limit.thermal.tx = pr->throttling.state;
  211. /*
  212. * Our default policy is to only use throttling at the lowest
  213. * performance state.
  214. */
  215. tx = pr->limit.thermal.tx;
  216. switch (type) {
  217. case ACPI_PROCESSOR_LIMIT_NONE:
  218. do {
  219. result = acpi_thermal_cpufreq_decrease(pr->id);
  220. } while (!result);
  221. tx = 0;
  222. break;
  223. case ACPI_PROCESSOR_LIMIT_INCREMENT:
  224. /* if going up: P-states first, T-states later */
  225. result = acpi_thermal_cpufreq_increase(pr->id);
  226. if (!result)
  227. goto end;
  228. else if (result == -ERANGE)
  229. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  230. "At maximum performance state\n"));
  231. if (pr->flags.throttling) {
  232. if (tx == (pr->throttling.state_count - 1))
  233. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  234. "At maximum throttling state\n"));
  235. else
  236. tx++;
  237. }
  238. break;
  239. case ACPI_PROCESSOR_LIMIT_DECREMENT:
  240. /* if going down: T-states first, P-states later */
  241. if (pr->flags.throttling) {
  242. if (tx == 0) {
  243. max_tx_px = 1;
  244. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  245. "At minimum throttling state\n"));
  246. } else {
  247. tx--;
  248. goto end;
  249. }
  250. }
  251. result = acpi_thermal_cpufreq_decrease(pr->id);
  252. if (result) {
  253. /*
  254. * We only could get -ERANGE, 1 or 0.
  255. * In the first two cases we reached max freq again.
  256. */
  257. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  258. "At minimum performance state\n"));
  259. max_tx_px = 1;
  260. } else
  261. max_tx_px = 0;
  262. break;
  263. }
  264. end:
  265. if (pr->flags.throttling) {
  266. pr->limit.thermal.px = 0;
  267. pr->limit.thermal.tx = tx;
  268. result = acpi_processor_apply_limit(pr);
  269. if (result)
  270. printk(KERN_ERR PREFIX "Unable to set thermal limit\n");
  271. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Thermal limit now (P%d:T%d)\n",
  272. pr->limit.thermal.px, pr->limit.thermal.tx));
  273. } else
  274. result = 0;
  275. if (max_tx_px)
  276. return 1;
  277. else
  278. return result;
  279. }
  280. int acpi_processor_get_limit_info(struct acpi_processor *pr)
  281. {
  282. if (!pr)
  283. return -EINVAL;
  284. if (pr->flags.throttling)
  285. pr->flags.limit = 1;
  286. return 0;
  287. }
  288. /* thermal coolign device callbacks */
  289. static int acpi_processor_max_state(struct acpi_processor *pr)
  290. {
  291. int max_state = 0;
  292. /*
  293. * There exists four states according to
  294. * cpufreq_thermal_reduction_ptg. 0, 1, 2, 3
  295. */
  296. max_state += cpufreq_get_max_state(pr->id);
  297. if (pr->flags.throttling)
  298. max_state += (pr->throttling.state_count -1);
  299. return max_state;
  300. }
  301. static int
  302. processor_get_max_state(struct thermal_cooling_device *cdev, char *buf)
  303. {
  304. struct acpi_device *device = cdev->devdata;
  305. struct acpi_processor *pr = acpi_driver_data(device);
  306. if (!device || !pr)
  307. return -EINVAL;
  308. return sprintf(buf, "%d\n", acpi_processor_max_state(pr));
  309. }
  310. static int
  311. processor_get_cur_state(struct thermal_cooling_device *cdev, char *buf)
  312. {
  313. struct acpi_device *device = cdev->devdata;
  314. struct acpi_processor *pr = acpi_driver_data(device);
  315. int cur_state;
  316. if (!device || !pr)
  317. return -EINVAL;
  318. cur_state = cpufreq_get_cur_state(pr->id);
  319. if (pr->flags.throttling)
  320. cur_state += pr->throttling.state;
  321. return sprintf(buf, "%d\n", cur_state);
  322. }
  323. static int
  324. processor_set_cur_state(struct thermal_cooling_device *cdev, unsigned int state)
  325. {
  326. struct acpi_device *device = cdev->devdata;
  327. struct acpi_processor *pr = acpi_driver_data(device);
  328. int result = 0;
  329. int max_pstate;
  330. if (!device || !pr)
  331. return -EINVAL;
  332. max_pstate = cpufreq_get_max_state(pr->id);
  333. if (state > acpi_processor_max_state(pr))
  334. return -EINVAL;
  335. if (state <= max_pstate) {
  336. if (pr->flags.throttling && pr->throttling.state)
  337. result = acpi_processor_set_throttling(pr, 0);
  338. cpufreq_set_cur_state(pr->id, state);
  339. } else {
  340. cpufreq_set_cur_state(pr->id, max_pstate);
  341. result = acpi_processor_set_throttling(pr,
  342. state - max_pstate);
  343. }
  344. return result;
  345. }
  346. struct thermal_cooling_device_ops processor_cooling_ops = {
  347. .get_max_state = processor_get_max_state,
  348. .get_cur_state = processor_get_cur_state,
  349. .set_cur_state = processor_set_cur_state,
  350. };
  351. /* /proc interface */
  352. static int acpi_processor_limit_seq_show(struct seq_file *seq, void *offset)
  353. {
  354. struct acpi_processor *pr = (struct acpi_processor *)seq->private;
  355. if (!pr)
  356. goto end;
  357. if (!pr->flags.limit) {
  358. seq_puts(seq, "<not supported>\n");
  359. goto end;
  360. }
  361. seq_printf(seq, "active limit: P%d:T%d\n"
  362. "user limit: P%d:T%d\n"
  363. "thermal limit: P%d:T%d\n",
  364. pr->limit.state.px, pr->limit.state.tx,
  365. pr->limit.user.px, pr->limit.user.tx,
  366. pr->limit.thermal.px, pr->limit.thermal.tx);
  367. end:
  368. return 0;
  369. }
  370. static int acpi_processor_limit_open_fs(struct inode *inode, struct file *file)
  371. {
  372. return single_open(file, acpi_processor_limit_seq_show,
  373. PDE(inode)->data);
  374. }
  375. static ssize_t acpi_processor_write_limit(struct file * file,
  376. const char __user * buffer,
  377. size_t count, loff_t * data)
  378. {
  379. int result = 0;
  380. struct seq_file *m = file->private_data;
  381. struct acpi_processor *pr = m->private;
  382. char limit_string[25] = { '\0' };
  383. int px = 0;
  384. int tx = 0;
  385. if (!pr || (count > sizeof(limit_string) - 1)) {
  386. return -EINVAL;
  387. }
  388. if (copy_from_user(limit_string, buffer, count)) {
  389. return -EFAULT;
  390. }
  391. limit_string[count] = '\0';
  392. if (sscanf(limit_string, "%d:%d", &px, &tx) != 2) {
  393. printk(KERN_ERR PREFIX "Invalid data format\n");
  394. return -EINVAL;
  395. }
  396. if (pr->flags.throttling) {
  397. if ((tx < 0) || (tx > (pr->throttling.state_count - 1))) {
  398. printk(KERN_ERR PREFIX "Invalid tx\n");
  399. return -EINVAL;
  400. }
  401. pr->limit.user.tx = tx;
  402. }
  403. result = acpi_processor_apply_limit(pr);
  404. return count;
  405. }
  406. struct file_operations acpi_processor_limit_fops = {
  407. .open = acpi_processor_limit_open_fs,
  408. .read = seq_read,
  409. .write = acpi_processor_write_limit,
  410. .llseek = seq_lseek,
  411. .release = single_release,
  412. };