processor_throttling.c 8.9 KB

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