acpi-cpufreq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * acpi-cpufreq.c - ACPI Processor P-States Driver ($Revision: 1.3 $)
  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) 2002 - 2004 Dominik Brodowski <linux@brodo.de>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  23. *
  24. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. */
  26. #include <linux/config.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/cpufreq.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/compiler.h>
  34. #include <linux/sched.h> /* current */
  35. #include <asm/io.h>
  36. #include <asm/delay.h>
  37. #include <asm/uaccess.h>
  38. #include <linux/acpi.h>
  39. #include <acpi/processor.h>
  40. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)
  41. MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
  42. MODULE_DESCRIPTION("ACPI Processor P-States Driver");
  43. MODULE_LICENSE("GPL");
  44. struct cpufreq_acpi_io {
  45. struct acpi_processor_performance acpi_data;
  46. struct cpufreq_frequency_table *freq_table;
  47. unsigned int resume;
  48. };
  49. static struct cpufreq_acpi_io *acpi_io_data[NR_CPUS];
  50. static struct cpufreq_driver acpi_cpufreq_driver;
  51. static unsigned int acpi_pstate_strict;
  52. static int
  53. acpi_processor_write_port(
  54. u16 port,
  55. u8 bit_width,
  56. u32 value)
  57. {
  58. if (bit_width <= 8) {
  59. outb(value, port);
  60. } else if (bit_width <= 16) {
  61. outw(value, port);
  62. } else if (bit_width <= 32) {
  63. outl(value, port);
  64. } else {
  65. return -ENODEV;
  66. }
  67. return 0;
  68. }
  69. static int
  70. acpi_processor_read_port(
  71. u16 port,
  72. u8 bit_width,
  73. u32 *ret)
  74. {
  75. *ret = 0;
  76. if (bit_width <= 8) {
  77. *ret = inb(port);
  78. } else if (bit_width <= 16) {
  79. *ret = inw(port);
  80. } else if (bit_width <= 32) {
  81. *ret = inl(port);
  82. } else {
  83. return -ENODEV;
  84. }
  85. return 0;
  86. }
  87. static int
  88. acpi_processor_set_performance (
  89. struct cpufreq_acpi_io *data,
  90. unsigned int cpu,
  91. int state)
  92. {
  93. u16 port = 0;
  94. u8 bit_width = 0;
  95. int ret = 0;
  96. u32 value = 0;
  97. int i = 0;
  98. struct cpufreq_freqs cpufreq_freqs;
  99. cpumask_t saved_mask;
  100. int retval;
  101. dprintk("acpi_processor_set_performance\n");
  102. /*
  103. * TBD: Use something other than set_cpus_allowed.
  104. * As set_cpus_allowed is a bit racy,
  105. * with any other set_cpus_allowed for this process.
  106. */
  107. saved_mask = current->cpus_allowed;
  108. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  109. if (smp_processor_id() != cpu) {
  110. return (-EAGAIN);
  111. }
  112. if (state == data->acpi_data.state) {
  113. if (unlikely(data->resume)) {
  114. dprintk("Called after resume, resetting to P%d\n", state);
  115. data->resume = 0;
  116. } else {
  117. dprintk("Already at target state (P%d)\n", state);
  118. retval = 0;
  119. goto migrate_end;
  120. }
  121. }
  122. dprintk("Transitioning from P%d to P%d\n",
  123. data->acpi_data.state, state);
  124. /* cpufreq frequency struct */
  125. cpufreq_freqs.cpu = cpu;
  126. cpufreq_freqs.old = data->freq_table[data->acpi_data.state].frequency;
  127. cpufreq_freqs.new = data->freq_table[state].frequency;
  128. /* notify cpufreq */
  129. cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_PRECHANGE);
  130. /*
  131. * First we write the target state's 'control' value to the
  132. * control_register.
  133. */
  134. port = data->acpi_data.control_register.address;
  135. bit_width = data->acpi_data.control_register.bit_width;
  136. value = (u32) data->acpi_data.states[state].control;
  137. dprintk("Writing 0x%08x to port 0x%04x\n", value, port);
  138. ret = acpi_processor_write_port(port, bit_width, value);
  139. if (ret) {
  140. dprintk("Invalid port width 0x%04x\n", bit_width);
  141. retval = ret;
  142. goto migrate_end;
  143. }
  144. /*
  145. * Assume the write went through when acpi_pstate_strict is not used.
  146. * As read status_register is an expensive operation and there
  147. * are no specific error cases where an IO port write will fail.
  148. */
  149. if (acpi_pstate_strict) {
  150. /* Then we read the 'status_register' and compare the value
  151. * with the target state's 'status' to make sure the
  152. * transition was successful.
  153. * Note that we'll poll for up to 1ms (100 cycles of 10us)
  154. * before giving up.
  155. */
  156. port = data->acpi_data.status_register.address;
  157. bit_width = data->acpi_data.status_register.bit_width;
  158. dprintk("Looking for 0x%08x from port 0x%04x\n",
  159. (u32) data->acpi_data.states[state].status, port);
  160. for (i=0; i<100; i++) {
  161. ret = acpi_processor_read_port(port, bit_width, &value);
  162. if (ret) {
  163. dprintk("Invalid port width 0x%04x\n", bit_width);
  164. retval = ret;
  165. goto migrate_end;
  166. }
  167. if (value == (u32) data->acpi_data.states[state].status)
  168. break;
  169. udelay(10);
  170. }
  171. } else {
  172. i = 0;
  173. value = (u32) data->acpi_data.states[state].status;
  174. }
  175. /* notify cpufreq */
  176. cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_POSTCHANGE);
  177. if (unlikely(value != (u32) data->acpi_data.states[state].status)) {
  178. unsigned int tmp = cpufreq_freqs.new;
  179. cpufreq_freqs.new = cpufreq_freqs.old;
  180. cpufreq_freqs.old = tmp;
  181. cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_PRECHANGE);
  182. cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_POSTCHANGE);
  183. printk(KERN_WARNING "acpi-cpufreq: Transition failed\n");
  184. retval = -ENODEV;
  185. goto migrate_end;
  186. }
  187. dprintk("Transition successful after %d microseconds\n", i * 10);
  188. data->acpi_data.state = state;
  189. retval = 0;
  190. migrate_end:
  191. set_cpus_allowed(current, saved_mask);
  192. return (retval);
  193. }
  194. static int
  195. acpi_cpufreq_target (
  196. struct cpufreq_policy *policy,
  197. unsigned int target_freq,
  198. unsigned int relation)
  199. {
  200. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  201. unsigned int next_state = 0;
  202. unsigned int result = 0;
  203. dprintk("acpi_cpufreq_setpolicy\n");
  204. result = cpufreq_frequency_table_target(policy,
  205. data->freq_table,
  206. target_freq,
  207. relation,
  208. &next_state);
  209. if (result)
  210. return (result);
  211. result = acpi_processor_set_performance (data, policy->cpu, next_state);
  212. return (result);
  213. }
  214. static int
  215. acpi_cpufreq_verify (
  216. struct cpufreq_policy *policy)
  217. {
  218. unsigned int result = 0;
  219. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  220. dprintk("acpi_cpufreq_verify\n");
  221. result = cpufreq_frequency_table_verify(policy,
  222. data->freq_table);
  223. return (result);
  224. }
  225. static unsigned long
  226. acpi_cpufreq_guess_freq (
  227. struct cpufreq_acpi_io *data,
  228. unsigned int cpu)
  229. {
  230. if (cpu_khz) {
  231. /* search the closest match to cpu_khz */
  232. unsigned int i;
  233. unsigned long freq;
  234. unsigned long freqn = data->acpi_data.states[0].core_frequency * 1000;
  235. for (i=0; i < (data->acpi_data.state_count - 1); i++) {
  236. freq = freqn;
  237. freqn = data->acpi_data.states[i+1].core_frequency * 1000;
  238. if ((2 * cpu_khz) > (freqn + freq)) {
  239. data->acpi_data.state = i;
  240. return (freq);
  241. }
  242. }
  243. data->acpi_data.state = data->acpi_data.state_count - 1;
  244. return (freqn);
  245. } else
  246. /* assume CPU is at P0... */
  247. data->acpi_data.state = 0;
  248. return data->acpi_data.states[0].core_frequency * 1000;
  249. }
  250. static int
  251. acpi_cpufreq_cpu_init (
  252. struct cpufreq_policy *policy)
  253. {
  254. unsigned int i;
  255. unsigned int cpu = policy->cpu;
  256. struct cpufreq_acpi_io *data;
  257. unsigned int result = 0;
  258. struct cpuinfo_x86 *c = &cpu_data[policy->cpu];
  259. dprintk("acpi_cpufreq_cpu_init\n");
  260. data = kzalloc(sizeof(struct cpufreq_acpi_io), GFP_KERNEL);
  261. if (!data)
  262. return (-ENOMEM);
  263. acpi_io_data[cpu] = data;
  264. result = acpi_processor_register_performance(&data->acpi_data, cpu);
  265. if (result)
  266. goto err_free;
  267. if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
  268. acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
  269. }
  270. /* capability check */
  271. if (data->acpi_data.state_count <= 1) {
  272. dprintk("No P-States\n");
  273. result = -ENODEV;
  274. goto err_unreg;
  275. }
  276. if ((data->acpi_data.control_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO) ||
  277. (data->acpi_data.status_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
  278. dprintk("Unsupported address space [%d, %d]\n",
  279. (u32) (data->acpi_data.control_register.space_id),
  280. (u32) (data->acpi_data.status_register.space_id));
  281. result = -ENODEV;
  282. goto err_unreg;
  283. }
  284. /* alloc freq_table */
  285. data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) * (data->acpi_data.state_count + 1), GFP_KERNEL);
  286. if (!data->freq_table) {
  287. result = -ENOMEM;
  288. goto err_unreg;
  289. }
  290. /* detect transition latency */
  291. policy->cpuinfo.transition_latency = 0;
  292. for (i=0; i<data->acpi_data.state_count; i++) {
  293. if ((data->acpi_data.states[i].transition_latency * 1000) > policy->cpuinfo.transition_latency)
  294. policy->cpuinfo.transition_latency = data->acpi_data.states[i].transition_latency * 1000;
  295. }
  296. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  297. /* The current speed is unknown and not detectable by ACPI... */
  298. policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
  299. /* table init */
  300. for (i=0; i<=data->acpi_data.state_count; i++)
  301. {
  302. data->freq_table[i].index = i;
  303. if (i<data->acpi_data.state_count)
  304. data->freq_table[i].frequency = data->acpi_data.states[i].core_frequency * 1000;
  305. else
  306. data->freq_table[i].frequency = CPUFREQ_TABLE_END;
  307. }
  308. result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
  309. if (result) {
  310. goto err_freqfree;
  311. }
  312. /* notify BIOS that we exist */
  313. acpi_processor_notify_smm(THIS_MODULE);
  314. printk(KERN_INFO "acpi-cpufreq: CPU%u - ACPI performance management activated.\n",
  315. cpu);
  316. for (i = 0; i < data->acpi_data.state_count; i++)
  317. dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
  318. (i == data->acpi_data.state?'*':' '), i,
  319. (u32) data->acpi_data.states[i].core_frequency,
  320. (u32) data->acpi_data.states[i].power,
  321. (u32) data->acpi_data.states[i].transition_latency);
  322. cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
  323. /*
  324. * the first call to ->target() should result in us actually
  325. * writing something to the appropriate registers.
  326. */
  327. data->resume = 1;
  328. return (result);
  329. err_freqfree:
  330. kfree(data->freq_table);
  331. err_unreg:
  332. acpi_processor_unregister_performance(&data->acpi_data, cpu);
  333. err_free:
  334. kfree(data);
  335. acpi_io_data[cpu] = NULL;
  336. return (result);
  337. }
  338. static int
  339. acpi_cpufreq_cpu_exit (
  340. struct cpufreq_policy *policy)
  341. {
  342. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  343. dprintk("acpi_cpufreq_cpu_exit\n");
  344. if (data) {
  345. cpufreq_frequency_table_put_attr(policy->cpu);
  346. acpi_io_data[policy->cpu] = NULL;
  347. acpi_processor_unregister_performance(&data->acpi_data, policy->cpu);
  348. kfree(data);
  349. }
  350. return (0);
  351. }
  352. static int
  353. acpi_cpufreq_resume (
  354. struct cpufreq_policy *policy)
  355. {
  356. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  357. dprintk("acpi_cpufreq_resume\n");
  358. data->resume = 1;
  359. return (0);
  360. }
  361. static struct freq_attr* acpi_cpufreq_attr[] = {
  362. &cpufreq_freq_attr_scaling_available_freqs,
  363. NULL,
  364. };
  365. static struct cpufreq_driver acpi_cpufreq_driver = {
  366. .verify = acpi_cpufreq_verify,
  367. .target = acpi_cpufreq_target,
  368. .init = acpi_cpufreq_cpu_init,
  369. .exit = acpi_cpufreq_cpu_exit,
  370. .resume = acpi_cpufreq_resume,
  371. .name = "acpi-cpufreq",
  372. .owner = THIS_MODULE,
  373. .attr = acpi_cpufreq_attr,
  374. };
  375. static int __init
  376. acpi_cpufreq_init (void)
  377. {
  378. int result = 0;
  379. dprintk("acpi_cpufreq_init\n");
  380. result = cpufreq_register_driver(&acpi_cpufreq_driver);
  381. return (result);
  382. }
  383. static void __exit
  384. acpi_cpufreq_exit (void)
  385. {
  386. dprintk("acpi_cpufreq_exit\n");
  387. cpufreq_unregister_driver(&acpi_cpufreq_driver);
  388. return;
  389. }
  390. module_param(acpi_pstate_strict, uint, 0644);
  391. MODULE_PARM_DESC(acpi_pstate_strict, "value 0 or non-zero. non-zero -> strict ACPI checks are performed during frequency changes.");
  392. late_initcall(acpi_cpufreq_init);
  393. module_exit(acpi_cpufreq_exit);
  394. MODULE_ALIAS("acpi");