processor_throttling.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * processor_throttling.c - Throttling 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/io.h>
  35. #include <asm/uaccess.h>
  36. #include <acpi/acpi_bus.h>
  37. #include <acpi/processor.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. Throttling Control
  45. -------------------------------------------------------------------------- */
  46. static int acpi_processor_get_throttling(struct acpi_processor *pr)
  47. {
  48. int state = 0;
  49. u32 value = 0;
  50. u32 duty_mask = 0;
  51. u32 duty_value = 0;
  52. if (!pr)
  53. return -EINVAL;
  54. if (!pr->flags.throttling)
  55. return -ENODEV;
  56. pr->throttling.state = 0;
  57. duty_mask = pr->throttling.state_count - 1;
  58. duty_mask <<= pr->throttling.duty_offset;
  59. local_irq_disable();
  60. value = inl(pr->throttling.address);
  61. /*
  62. * Compute the current throttling state when throttling is enabled
  63. * (bit 4 is on).
  64. */
  65. if (value & 0x10) {
  66. duty_value = value & duty_mask;
  67. duty_value >>= pr->throttling.duty_offset;
  68. if (duty_value)
  69. state = pr->throttling.state_count - duty_value;
  70. }
  71. pr->throttling.state = state;
  72. local_irq_enable();
  73. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  74. "Throttling state is T%d (%d%% throttling applied)\n",
  75. state, pr->throttling.states[state].performance));
  76. return 0;
  77. }
  78. int acpi_processor_set_throttling(struct acpi_processor *pr, int state)
  79. {
  80. u32 value = 0;
  81. u32 duty_mask = 0;
  82. u32 duty_value = 0;
  83. if (!pr)
  84. return -EINVAL;
  85. if ((state < 0) || (state > (pr->throttling.state_count - 1)))
  86. return -EINVAL;
  87. if (!pr->flags.throttling)
  88. return -ENODEV;
  89. if (state == pr->throttling.state)
  90. return 0;
  91. /*
  92. * Calculate the duty_value and duty_mask.
  93. */
  94. if (state) {
  95. duty_value = pr->throttling.state_count - state;
  96. duty_value <<= pr->throttling.duty_offset;
  97. /* Used to clear all duty_value bits */
  98. duty_mask = pr->throttling.state_count - 1;
  99. duty_mask <<= acpi_fadt.duty_offset;
  100. duty_mask = ~duty_mask;
  101. }
  102. local_irq_disable();
  103. /*
  104. * Disable throttling by writing a 0 to bit 4. Note that we must
  105. * turn it off before you can change the duty_value.
  106. */
  107. value = inl(pr->throttling.address);
  108. if (value & 0x10) {
  109. value &= 0xFFFFFFEF;
  110. outl(value, pr->throttling.address);
  111. }
  112. /*
  113. * Write the new duty_value and then enable throttling. Note
  114. * that a state value of 0 leaves throttling disabled.
  115. */
  116. if (state) {
  117. value &= duty_mask;
  118. value |= duty_value;
  119. outl(value, pr->throttling.address);
  120. value |= 0x00000010;
  121. outl(value, pr->throttling.address);
  122. }
  123. pr->throttling.state = state;
  124. local_irq_enable();
  125. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  126. "Throttling state set to T%d (%d%%)\n", state,
  127. (pr->throttling.states[state].performance ? pr->
  128. throttling.states[state].performance / 10 : 0)));
  129. return 0;
  130. }
  131. int acpi_processor_get_throttling_info(struct acpi_processor *pr)
  132. {
  133. int result = 0;
  134. int step = 0;
  135. int i = 0;
  136. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  137. "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
  138. pr->throttling.address,
  139. pr->throttling.duty_offset,
  140. pr->throttling.duty_width));
  141. if (!pr)
  142. return -EINVAL;
  143. /* TBD: Support ACPI 2.0 objects */
  144. if (!pr->throttling.address) {
  145. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
  146. return 0;
  147. } else if (!pr->throttling.duty_width) {
  148. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
  149. return 0;
  150. }
  151. /* TBD: Support duty_cycle values that span bit 4. */
  152. else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
  153. printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");
  154. return 0;
  155. }
  156. /*
  157. * PIIX4 Errata: We don't support throttling on the original PIIX4.
  158. * This shouldn't be an issue as few (if any) mobile systems ever
  159. * used this part.
  160. */
  161. if (errata.piix4.throttle) {
  162. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  163. "Throttling not supported on PIIX4 A- or B-step\n"));
  164. return 0;
  165. }
  166. pr->throttling.state_count = 1 << acpi_fadt.duty_width;
  167. /*
  168. * Compute state values. Note that throttling displays a linear power/
  169. * performance relationship (at 50% performance the CPU will consume
  170. * 50% power). Values are in 1/10th of a percent to preserve accuracy.
  171. */
  172. step = (1000 / pr->throttling.state_count);
  173. for (i = 0; i < pr->throttling.state_count; i++) {
  174. pr->throttling.states[i].performance = step * i;
  175. pr->throttling.states[i].power = step * i;
  176. }
  177. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
  178. pr->throttling.state_count));
  179. pr->flags.throttling = 1;
  180. /*
  181. * Disable throttling (if enabled). We'll let subsequent policy (e.g.
  182. * thermal) decide to lower performance if it so chooses, but for now
  183. * we'll crank up the speed.
  184. */
  185. result = acpi_processor_get_throttling(pr);
  186. if (result)
  187. goto end;
  188. if (pr->throttling.state) {
  189. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  190. "Disabling throttling (was T%d)\n",
  191. pr->throttling.state));
  192. result = acpi_processor_set_throttling(pr, 0);
  193. if (result)
  194. goto end;
  195. }
  196. end:
  197. if (result)
  198. pr->flags.throttling = 0;
  199. return result;
  200. }
  201. /* proc interface */
  202. static int acpi_processor_throttling_seq_show(struct seq_file *seq,
  203. void *offset)
  204. {
  205. struct acpi_processor *pr = (struct acpi_processor *)seq->private;
  206. int i = 0;
  207. int result = 0;
  208. if (!pr)
  209. goto end;
  210. if (!(pr->throttling.state_count > 0)) {
  211. seq_puts(seq, "<not supported>\n");
  212. goto end;
  213. }
  214. result = acpi_processor_get_throttling(pr);
  215. if (result) {
  216. seq_puts(seq,
  217. "Could not determine current throttling state.\n");
  218. goto end;
  219. }
  220. seq_printf(seq, "state count: %d\n"
  221. "active state: T%d\n",
  222. pr->throttling.state_count, pr->throttling.state);
  223. seq_puts(seq, "states:\n");
  224. for (i = 0; i < pr->throttling.state_count; i++)
  225. seq_printf(seq, " %cT%d: %02d%%\n",
  226. (i == pr->throttling.state ? '*' : ' '), i,
  227. (pr->throttling.states[i].performance ? pr->
  228. throttling.states[i].performance / 10 : 0));
  229. end:
  230. return 0;
  231. }
  232. static int acpi_processor_throttling_open_fs(struct inode *inode,
  233. struct file *file)
  234. {
  235. return single_open(file, acpi_processor_throttling_seq_show,
  236. PDE(inode)->data);
  237. }
  238. static ssize_t acpi_processor_write_throttling(struct file * file,
  239. const char __user * buffer,
  240. size_t count, loff_t * data)
  241. {
  242. int result = 0;
  243. struct seq_file *m = (struct seq_file *)file->private_data;
  244. struct acpi_processor *pr = (struct acpi_processor *)m->private;
  245. char state_string[12] = { '\0' };
  246. if (!pr || (count > sizeof(state_string) - 1))
  247. return -EINVAL;
  248. if (copy_from_user(state_string, buffer, count))
  249. return -EFAULT;
  250. state_string[count] = '\0';
  251. result = acpi_processor_set_throttling(pr,
  252. simple_strtoul(state_string,
  253. NULL, 0));
  254. if (result)
  255. return result;
  256. return count;
  257. }
  258. struct file_operations acpi_processor_throttling_fops = {
  259. .open = acpi_processor_throttling_open_fs,
  260. .read = seq_read,
  261. .write = acpi_processor_write_throttling,
  262. .llseek = seq_lseek,
  263. .release = single_release,
  264. };