processor_thermal.c 12 KB

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