PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src-rt/linux/linux-2.6/drivers/acpi/processor_throttling.c

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