processor_thermal.c 12 KB

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