processor_throttling.c 8.8 KB

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