acpi-cpufreq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 <asm/io.h>
  34. #include <asm/delay.h>
  35. #include <asm/uaccess.h>
  36. #include <linux/acpi.h>
  37. #include <acpi/processor.h>
  38. #include "speedstep-est-common.h"
  39. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)
  40. MODULE_AUTHOR("Paul Diefenbaugh, Dominik Brodowski");
  41. MODULE_DESCRIPTION("ACPI Processor P-States Driver");
  42. MODULE_LICENSE("GPL");
  43. struct cpufreq_acpi_io {
  44. struct acpi_processor_performance acpi_data;
  45. struct cpufreq_frequency_table *freq_table;
  46. unsigned int resume;
  47. };
  48. static struct cpufreq_acpi_io *acpi_io_data[NR_CPUS];
  49. static struct cpufreq_driver acpi_cpufreq_driver;
  50. static int
  51. acpi_processor_write_port(
  52. u16 port,
  53. u8 bit_width,
  54. u32 value)
  55. {
  56. if (bit_width <= 8) {
  57. outb(value, port);
  58. } else if (bit_width <= 16) {
  59. outw(value, port);
  60. } else if (bit_width <= 32) {
  61. outl(value, port);
  62. } else {
  63. return -ENODEV;
  64. }
  65. return 0;
  66. }
  67. static int
  68. acpi_processor_read_port(
  69. u16 port,
  70. u8 bit_width,
  71. u32 *ret)
  72. {
  73. *ret = 0;
  74. if (bit_width <= 8) {
  75. *ret = inb(port);
  76. } else if (bit_width <= 16) {
  77. *ret = inw(port);
  78. } else if (bit_width <= 32) {
  79. *ret = inl(port);
  80. } else {
  81. return -ENODEV;
  82. }
  83. return 0;
  84. }
  85. static int
  86. acpi_processor_set_performance (
  87. struct cpufreq_acpi_io *data,
  88. unsigned int cpu,
  89. int state)
  90. {
  91. u16 port = 0;
  92. u8 bit_width = 0;
  93. int ret = 0;
  94. u32 value = 0;
  95. int i = 0;
  96. struct cpufreq_freqs cpufreq_freqs;
  97. cpumask_t saved_mask;
  98. int retval;
  99. dprintk("acpi_processor_set_performance\n");
  100. /*
  101. * TBD: Use something other than set_cpus_allowed.
  102. * As set_cpus_allowed is a bit racy,
  103. * with any other set_cpus_allowed for this process.
  104. */
  105. saved_mask = current->cpus_allowed;
  106. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  107. if (smp_processor_id() != cpu) {
  108. return (-EAGAIN);
  109. }
  110. if (state == data->acpi_data.state) {
  111. if (unlikely(data->resume)) {
  112. dprintk("Called after resume, resetting to P%d\n", state);
  113. data->resume = 0;
  114. } else {
  115. dprintk("Already at target state (P%d)\n", state);
  116. retval = 0;
  117. goto migrate_end;
  118. }
  119. }
  120. dprintk("Transitioning from P%d to P%d\n",
  121. data->acpi_data.state, state);
  122. /* cpufreq frequency struct */
  123. cpufreq_freqs.cpu = cpu;
  124. cpufreq_freqs.old = data->freq_table[data->acpi_data.state].frequency;
  125. cpufreq_freqs.new = data->freq_table[state].frequency;
  126. /* notify cpufreq */
  127. cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_PRECHANGE);
  128. /*
  129. * First we write the target state's 'control' value to the
  130. * control_register.
  131. */
  132. port = data->acpi_data.control_register.address;
  133. bit_width = data->acpi_data.control_register.bit_width;
  134. value = (u32) data->acpi_data.states[state].control;
  135. dprintk("Writing 0x%08x to port 0x%04x\n", value, port);
  136. ret = acpi_processor_write_port(port, bit_width, value);
  137. if (ret) {
  138. dprintk("Invalid port width 0x%04x\n", bit_width);
  139. retval = ret;
  140. goto migrate_end;
  141. }
  142. /*
  143. * Then we read the 'status_register' and compare the value with the
  144. * target state's 'status' to make sure the transition was successful.
  145. * Note that we'll poll for up to 1ms (100 cycles of 10us) before
  146. * giving up.
  147. */
  148. port = data->acpi_data.status_register.address;
  149. bit_width = data->acpi_data.status_register.bit_width;
  150. dprintk("Looking for 0x%08x from port 0x%04x\n",
  151. (u32) data->acpi_data.states[state].status, port);
  152. for (i=0; i<100; i++) {
  153. ret = acpi_processor_read_port(port, bit_width, &value);
  154. if (ret) {
  155. dprintk("Invalid port width 0x%04x\n", bit_width);
  156. retval = ret;
  157. goto migrate_end;
  158. }
  159. if (value == (u32) data->acpi_data.states[state].status)
  160. break;
  161. udelay(10);
  162. }
  163. /* notify cpufreq */
  164. cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_POSTCHANGE);
  165. if (value != (u32) data->acpi_data.states[state].status) {
  166. unsigned int tmp = cpufreq_freqs.new;
  167. cpufreq_freqs.new = cpufreq_freqs.old;
  168. cpufreq_freqs.old = tmp;
  169. cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_PRECHANGE);
  170. cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_POSTCHANGE);
  171. printk(KERN_WARNING "acpi-cpufreq: Transition failed\n");
  172. retval = -ENODEV;
  173. goto migrate_end;
  174. }
  175. dprintk("Transition successful after %d microseconds\n", i * 10);
  176. data->acpi_data.state = state;
  177. retval = 0;
  178. migrate_end:
  179. set_cpus_allowed(current, saved_mask);
  180. return (retval);
  181. }
  182. static int
  183. acpi_cpufreq_target (
  184. struct cpufreq_policy *policy,
  185. unsigned int target_freq,
  186. unsigned int relation)
  187. {
  188. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  189. unsigned int next_state = 0;
  190. unsigned int result = 0;
  191. dprintk("acpi_cpufreq_setpolicy\n");
  192. result = cpufreq_frequency_table_target(policy,
  193. data->freq_table,
  194. target_freq,
  195. relation,
  196. &next_state);
  197. if (result)
  198. return (result);
  199. result = acpi_processor_set_performance (data, policy->cpu, next_state);
  200. return (result);
  201. }
  202. static int
  203. acpi_cpufreq_verify (
  204. struct cpufreq_policy *policy)
  205. {
  206. unsigned int result = 0;
  207. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  208. dprintk("acpi_cpufreq_verify\n");
  209. result = cpufreq_frequency_table_verify(policy,
  210. data->freq_table);
  211. return (result);
  212. }
  213. static unsigned long
  214. acpi_cpufreq_guess_freq (
  215. struct cpufreq_acpi_io *data,
  216. unsigned int cpu)
  217. {
  218. if (cpu_khz) {
  219. /* search the closest match to cpu_khz */
  220. unsigned int i;
  221. unsigned long freq;
  222. unsigned long freqn = data->acpi_data.states[0].core_frequency * 1000;
  223. for (i=0; i < (data->acpi_data.state_count - 1); i++) {
  224. freq = freqn;
  225. freqn = data->acpi_data.states[i+1].core_frequency * 1000;
  226. if ((2 * cpu_khz) > (freqn + freq)) {
  227. data->acpi_data.state = i;
  228. return (freq);
  229. }
  230. }
  231. data->acpi_data.state = data->acpi_data.state_count - 1;
  232. return (freqn);
  233. } else
  234. /* assume CPU is at P0... */
  235. data->acpi_data.state = 0;
  236. return data->acpi_data.states[0].core_frequency * 1000;
  237. }
  238. /*
  239. * acpi_processor_cpu_init_pdc_est - let BIOS know about the SMP capabilities
  240. * of this driver
  241. * @perf: processor-specific acpi_io_data struct
  242. * @cpu: CPU being initialized
  243. *
  244. * To avoid issues with legacy OSes, some BIOSes require to be informed of
  245. * the SMP capabilities of OS P-state driver. Here we set the bits in _PDC
  246. * accordingly, for Enhanced Speedstep. Actual call to _PDC is done in
  247. * driver/acpi/processor.c
  248. */
  249. static void
  250. acpi_processor_cpu_init_pdc_est(
  251. struct acpi_processor_performance *perf,
  252. unsigned int cpu,
  253. struct acpi_object_list *obj_list
  254. )
  255. {
  256. union acpi_object *obj;
  257. u32 *buf;
  258. struct cpuinfo_x86 *c = cpu_data + cpu;
  259. dprintk("acpi_processor_cpu_init_pdc_est\n");
  260. if (!cpu_has(c, X86_FEATURE_EST))
  261. return;
  262. /* Initialize pdc. It will be used later. */
  263. if (!obj_list)
  264. return;
  265. if (!(obj_list->count && obj_list->pointer))
  266. return;
  267. obj = obj_list->pointer;
  268. if ((obj->buffer.length == 12) && obj->buffer.pointer) {
  269. buf = (u32 *)obj->buffer.pointer;
  270. buf[0] = ACPI_PDC_REVISION_ID;
  271. buf[1] = 1;
  272. buf[2] = ACPI_PDC_EST_CAPABILITY_SMP;
  273. perf->pdc = obj_list;
  274. }
  275. return;
  276. }
  277. /* CPU specific PDC initialization */
  278. static void
  279. acpi_processor_cpu_init_pdc(
  280. struct acpi_processor_performance *perf,
  281. unsigned int cpu,
  282. struct acpi_object_list *obj_list
  283. )
  284. {
  285. struct cpuinfo_x86 *c = cpu_data + cpu;
  286. dprintk("acpi_processor_cpu_init_pdc\n");
  287. perf->pdc = NULL;
  288. if (cpu_has(c, X86_FEATURE_EST))
  289. acpi_processor_cpu_init_pdc_est(perf, cpu, obj_list);
  290. return;
  291. }
  292. static int
  293. acpi_cpufreq_cpu_init (
  294. struct cpufreq_policy *policy)
  295. {
  296. unsigned int i;
  297. unsigned int cpu = policy->cpu;
  298. struct cpufreq_acpi_io *data;
  299. unsigned int result = 0;
  300. union acpi_object arg0 = {ACPI_TYPE_BUFFER};
  301. u32 arg0_buf[3];
  302. struct acpi_object_list arg_list = {1, &arg0};
  303. dprintk("acpi_cpufreq_cpu_init\n");
  304. /* setup arg_list for _PDC settings */
  305. arg0.buffer.length = 12;
  306. arg0.buffer.pointer = (u8 *) arg0_buf;
  307. data = kmalloc(sizeof(struct cpufreq_acpi_io), GFP_KERNEL);
  308. if (!data)
  309. return (-ENOMEM);
  310. memset(data, 0, sizeof(struct cpufreq_acpi_io));
  311. acpi_io_data[cpu] = data;
  312. acpi_processor_cpu_init_pdc(&data->acpi_data, cpu, &arg_list);
  313. result = acpi_processor_register_performance(&data->acpi_data, cpu);
  314. data->acpi_data.pdc = NULL;
  315. if (result)
  316. goto err_free;
  317. if (is_const_loops_cpu(cpu)) {
  318. acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
  319. }
  320. /* capability check */
  321. if (data->acpi_data.state_count <= 1) {
  322. dprintk("No P-States\n");
  323. result = -ENODEV;
  324. goto err_unreg;
  325. }
  326. if ((data->acpi_data.control_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO) ||
  327. (data->acpi_data.status_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
  328. dprintk("Unsupported address space [%d, %d]\n",
  329. (u32) (data->acpi_data.control_register.space_id),
  330. (u32) (data->acpi_data.status_register.space_id));
  331. result = -ENODEV;
  332. goto err_unreg;
  333. }
  334. /* alloc freq_table */
  335. data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) * (data->acpi_data.state_count + 1), GFP_KERNEL);
  336. if (!data->freq_table) {
  337. result = -ENOMEM;
  338. goto err_unreg;
  339. }
  340. /* detect transition latency */
  341. policy->cpuinfo.transition_latency = 0;
  342. for (i=0; i<data->acpi_data.state_count; i++) {
  343. if ((data->acpi_data.states[i].transition_latency * 1000) > policy->cpuinfo.transition_latency)
  344. policy->cpuinfo.transition_latency = data->acpi_data.states[i].transition_latency * 1000;
  345. }
  346. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  347. /* The current speed is unknown and not detectable by ACPI... */
  348. policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
  349. /* table init */
  350. for (i=0; i<=data->acpi_data.state_count; i++)
  351. {
  352. data->freq_table[i].index = i;
  353. if (i<data->acpi_data.state_count)
  354. data->freq_table[i].frequency = data->acpi_data.states[i].core_frequency * 1000;
  355. else
  356. data->freq_table[i].frequency = CPUFREQ_TABLE_END;
  357. }
  358. result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
  359. if (result) {
  360. goto err_freqfree;
  361. }
  362. /* notify BIOS that we exist */
  363. acpi_processor_notify_smm(THIS_MODULE);
  364. printk(KERN_INFO "acpi-cpufreq: CPU%u - ACPI performance management activated.\n",
  365. cpu);
  366. for (i = 0; i < data->acpi_data.state_count; i++)
  367. dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
  368. (i == data->acpi_data.state?'*':' '), i,
  369. (u32) data->acpi_data.states[i].core_frequency,
  370. (u32) data->acpi_data.states[i].power,
  371. (u32) data->acpi_data.states[i].transition_latency);
  372. cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
  373. return (result);
  374. err_freqfree:
  375. kfree(data->freq_table);
  376. err_unreg:
  377. acpi_processor_unregister_performance(&data->acpi_data, cpu);
  378. err_free:
  379. kfree(data);
  380. acpi_io_data[cpu] = NULL;
  381. return (result);
  382. }
  383. static int
  384. acpi_cpufreq_cpu_exit (
  385. struct cpufreq_policy *policy)
  386. {
  387. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  388. dprintk("acpi_cpufreq_cpu_exit\n");
  389. if (data) {
  390. cpufreq_frequency_table_put_attr(policy->cpu);
  391. acpi_io_data[policy->cpu] = NULL;
  392. acpi_processor_unregister_performance(&data->acpi_data, policy->cpu);
  393. kfree(data);
  394. }
  395. return (0);
  396. }
  397. static int
  398. acpi_cpufreq_resume (
  399. struct cpufreq_policy *policy)
  400. {
  401. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  402. dprintk("acpi_cpufreq_resume\n");
  403. data->resume = 1;
  404. return (0);
  405. }
  406. static struct freq_attr* acpi_cpufreq_attr[] = {
  407. &cpufreq_freq_attr_scaling_available_freqs,
  408. NULL,
  409. };
  410. static struct cpufreq_driver acpi_cpufreq_driver = {
  411. .verify = acpi_cpufreq_verify,
  412. .target = acpi_cpufreq_target,
  413. .init = acpi_cpufreq_cpu_init,
  414. .exit = acpi_cpufreq_cpu_exit,
  415. .resume = acpi_cpufreq_resume,
  416. .name = "acpi-cpufreq",
  417. .owner = THIS_MODULE,
  418. .attr = acpi_cpufreq_attr,
  419. };
  420. static int __init
  421. acpi_cpufreq_init (void)
  422. {
  423. int result = 0;
  424. dprintk("acpi_cpufreq_init\n");
  425. result = cpufreq_register_driver(&acpi_cpufreq_driver);
  426. return (result);
  427. }
  428. static void __exit
  429. acpi_cpufreq_exit (void)
  430. {
  431. dprintk("acpi_cpufreq_exit\n");
  432. cpufreq_unregister_driver(&acpi_cpufreq_driver);
  433. return;
  434. }
  435. late_initcall(acpi_cpufreq_init);
  436. module_exit(acpi_cpufreq_exit);
  437. MODULE_ALIAS("acpi");