acpi-cpufreq.c 14 KB

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