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 <asm/io.h>
  35. #include <asm/delay.h>
  36. #include <asm/uaccess.h>
  37. #include <linux/acpi.h>
  38. #include <acpi/processor.h>
  39. #include "speedstep-est-common.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. /*
  251. * acpi_processor_cpu_init_pdc_est - let BIOS know about the SMP capabilities
  252. * of this driver
  253. * @perf: processor-specific acpi_io_data struct
  254. * @cpu: CPU being initialized
  255. *
  256. * To avoid issues with legacy OSes, some BIOSes require to be informed of
  257. * the SMP capabilities of OS P-state driver. Here we set the bits in _PDC
  258. * accordingly, for Enhanced Speedstep. Actual call to _PDC is done in
  259. * driver/acpi/processor.c
  260. */
  261. static void
  262. acpi_processor_cpu_init_pdc_est(
  263. struct acpi_processor_performance *perf,
  264. unsigned int cpu,
  265. struct acpi_object_list *obj_list
  266. )
  267. {
  268. union acpi_object *obj;
  269. u32 *buf;
  270. struct cpuinfo_x86 *c = cpu_data + cpu;
  271. dprintk("acpi_processor_cpu_init_pdc_est\n");
  272. if (!cpu_has(c, X86_FEATURE_EST))
  273. return;
  274. /* Initialize pdc. It will be used later. */
  275. if (!obj_list)
  276. return;
  277. if (!(obj_list->count && obj_list->pointer))
  278. return;
  279. obj = obj_list->pointer;
  280. if ((obj->buffer.length == 12) && obj->buffer.pointer) {
  281. buf = (u32 *)obj->buffer.pointer;
  282. buf[0] = ACPI_PDC_REVISION_ID;
  283. buf[1] = 1;
  284. buf[2] = ACPI_PDC_EST_CAPABILITY_SMP;
  285. perf->pdc = obj_list;
  286. }
  287. return;
  288. }
  289. /* CPU specific PDC initialization */
  290. static void
  291. acpi_processor_cpu_init_pdc(
  292. struct acpi_processor_performance *perf,
  293. unsigned int cpu,
  294. struct acpi_object_list *obj_list
  295. )
  296. {
  297. struct cpuinfo_x86 *c = cpu_data + cpu;
  298. dprintk("acpi_processor_cpu_init_pdc\n");
  299. perf->pdc = NULL;
  300. if (cpu_has(c, X86_FEATURE_EST))
  301. acpi_processor_cpu_init_pdc_est(perf, cpu, obj_list);
  302. return;
  303. }
  304. static int
  305. acpi_cpufreq_cpu_init (
  306. struct cpufreq_policy *policy)
  307. {
  308. unsigned int i;
  309. unsigned int cpu = policy->cpu;
  310. struct cpufreq_acpi_io *data;
  311. unsigned int result = 0;
  312. union acpi_object arg0 = {ACPI_TYPE_BUFFER};
  313. u32 arg0_buf[3];
  314. struct acpi_object_list arg_list = {1, &arg0};
  315. dprintk("acpi_cpufreq_cpu_init\n");
  316. /* setup arg_list for _PDC settings */
  317. arg0.buffer.length = 12;
  318. arg0.buffer.pointer = (u8 *) arg0_buf;
  319. data = kmalloc(sizeof(struct cpufreq_acpi_io), GFP_KERNEL);
  320. if (!data)
  321. return (-ENOMEM);
  322. memset(data, 0, sizeof(struct cpufreq_acpi_io));
  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");