acpi-cpufreq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 acpi_processor_performance *acpi_perf_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 i = 0;
  97. int ret = 0;
  98. u32 value = 0;
  99. int retval;
  100. struct acpi_processor_performance *perf;
  101. dprintk("acpi_processor_set_performance\n");
  102. retval = 0;
  103. perf = data->acpi_data;
  104. if (state == perf->state) {
  105. if (unlikely(data->resume)) {
  106. dprintk("Called after resume, resetting to P%d\n", state);
  107. data->resume = 0;
  108. } else {
  109. dprintk("Already at target state (P%d)\n", state);
  110. return (retval);
  111. }
  112. }
  113. dprintk("Transitioning from P%d to P%d\n", perf->state, state);
  114. /*
  115. * First we write the target state's 'control' value to the
  116. * control_register.
  117. */
  118. port = perf->control_register.address;
  119. bit_width = perf->control_register.bit_width;
  120. value = (u32) perf->states[state].control;
  121. dprintk("Writing 0x%08x to port 0x%04x\n", value, port);
  122. ret = acpi_processor_write_port(port, bit_width, value);
  123. if (ret) {
  124. dprintk("Invalid port width 0x%04x\n", bit_width);
  125. return (ret);
  126. }
  127. /*
  128. * Assume the write went through when acpi_pstate_strict is not used.
  129. * As read status_register is an expensive operation and there
  130. * are no specific error cases where an IO port write will fail.
  131. */
  132. if (acpi_pstate_strict) {
  133. /* Then we read the 'status_register' and compare the value
  134. * with the target state's 'status' to make sure the
  135. * transition was successful.
  136. * Note that we'll poll for up to 1ms (100 cycles of 10us)
  137. * before giving up.
  138. */
  139. port = perf->status_register.address;
  140. bit_width = perf->status_register.bit_width;
  141. dprintk("Looking for 0x%08x from port 0x%04x\n",
  142. (u32) perf->states[state].status, port);
  143. for (i = 0; i < 100; i++) {
  144. ret = acpi_processor_read_port(port, bit_width, &value);
  145. if (ret) {
  146. dprintk("Invalid port width 0x%04x\n", bit_width);
  147. return (ret);
  148. }
  149. if (value == (u32) perf->states[state].status)
  150. break;
  151. udelay(10);
  152. }
  153. } else {
  154. i = 0;
  155. value = (u32) perf->states[state].status;
  156. }
  157. if (unlikely(value != (u32) perf->states[state].status)) {
  158. printk(KERN_WARNING "acpi-cpufreq: Transition failed\n");
  159. retval = -ENODEV;
  160. return (retval);
  161. }
  162. dprintk("Transition successful after %d microseconds\n", i * 10);
  163. perf->state = state;
  164. return (retval);
  165. }
  166. static int
  167. acpi_cpufreq_target (
  168. struct cpufreq_policy *policy,
  169. unsigned int target_freq,
  170. unsigned int relation)
  171. {
  172. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  173. struct acpi_processor_performance *perf;
  174. struct cpufreq_freqs freqs;
  175. cpumask_t online_policy_cpus;
  176. cpumask_t saved_mask;
  177. cpumask_t set_mask;
  178. cpumask_t covered_cpus;
  179. unsigned int cur_state = 0;
  180. unsigned int next_state = 0;
  181. unsigned int result = 0;
  182. unsigned int j;
  183. unsigned int tmp;
  184. dprintk("acpi_cpufreq_setpolicy\n");
  185. result = cpufreq_frequency_table_target(policy,
  186. data->freq_table,
  187. target_freq,
  188. relation,
  189. &next_state);
  190. if (unlikely(result))
  191. return (result);
  192. perf = data->acpi_data;
  193. cur_state = perf->state;
  194. freqs.old = data->freq_table[cur_state].frequency;
  195. freqs.new = data->freq_table[next_state].frequency;
  196. #ifdef CONFIG_HOTPLUG_CPU
  197. /* cpufreq holds the hotplug lock, so we are safe from here on */
  198. cpus_and(online_policy_cpus, cpu_online_map, policy->cpus);
  199. #else
  200. online_policy_cpus = policy->cpus;
  201. #endif
  202. for_each_cpu_mask(j, online_policy_cpus) {
  203. freqs.cpu = j;
  204. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  205. }
  206. /*
  207. * We need to call driver->target() on all or any CPU in
  208. * policy->cpus, depending on policy->shared_type.
  209. */
  210. saved_mask = current->cpus_allowed;
  211. cpus_clear(covered_cpus);
  212. for_each_cpu_mask(j, online_policy_cpus) {
  213. /*
  214. * Support for SMP systems.
  215. * Make sure we are running on CPU that wants to change freq
  216. */
  217. cpus_clear(set_mask);
  218. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
  219. cpus_or(set_mask, set_mask, online_policy_cpus);
  220. else
  221. cpu_set(j, set_mask);
  222. set_cpus_allowed(current, set_mask);
  223. if (unlikely(!cpu_isset(smp_processor_id(), set_mask))) {
  224. dprintk("couldn't limit to CPUs in this domain\n");
  225. result = -EAGAIN;
  226. break;
  227. }
  228. result = acpi_processor_set_performance (data, j, next_state);
  229. if (result) {
  230. result = -EAGAIN;
  231. break;
  232. }
  233. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
  234. break;
  235. cpu_set(j, covered_cpus);
  236. }
  237. for_each_cpu_mask(j, online_policy_cpus) {
  238. freqs.cpu = j;
  239. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  240. }
  241. if (unlikely(result)) {
  242. /*
  243. * We have failed halfway through the frequency change.
  244. * We have sent callbacks to online_policy_cpus and
  245. * acpi_processor_set_performance() has been called on
  246. * coverd_cpus. Best effort undo..
  247. */
  248. if (!cpus_empty(covered_cpus)) {
  249. for_each_cpu_mask(j, covered_cpus) {
  250. policy->cpu = j;
  251. acpi_processor_set_performance (data,
  252. j,
  253. cur_state);
  254. }
  255. }
  256. tmp = freqs.new;
  257. freqs.new = freqs.old;
  258. freqs.old = tmp;
  259. for_each_cpu_mask(j, online_policy_cpus) {
  260. freqs.cpu = j;
  261. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  262. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  263. }
  264. }
  265. set_cpus_allowed(current, saved_mask);
  266. return (result);
  267. }
  268. static int
  269. acpi_cpufreq_verify (
  270. struct cpufreq_policy *policy)
  271. {
  272. unsigned int result = 0;
  273. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  274. dprintk("acpi_cpufreq_verify\n");
  275. result = cpufreq_frequency_table_verify(policy,
  276. data->freq_table);
  277. return (result);
  278. }
  279. static unsigned long
  280. acpi_cpufreq_guess_freq (
  281. struct cpufreq_acpi_io *data,
  282. unsigned int cpu)
  283. {
  284. struct acpi_processor_performance *perf = data->acpi_data;
  285. if (cpu_khz) {
  286. /* search the closest match to cpu_khz */
  287. unsigned int i;
  288. unsigned long freq;
  289. unsigned long freqn = perf->states[0].core_frequency * 1000;
  290. for (i = 0; i < (perf->state_count - 1); i++) {
  291. freq = freqn;
  292. freqn = perf->states[i+1].core_frequency * 1000;
  293. if ((2 * cpu_khz) > (freqn + freq)) {
  294. perf->state = i;
  295. return (freq);
  296. }
  297. }
  298. perf->state = perf->state_count - 1;
  299. return (freqn);
  300. } else {
  301. /* assume CPU is at P0... */
  302. perf->state = 0;
  303. return perf->states[0].core_frequency * 1000;
  304. }
  305. }
  306. /*
  307. * acpi_cpufreq_early_init - initialize ACPI P-States library
  308. *
  309. * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
  310. * in order to determine correct frequency and voltage pairings. We can
  311. * do _PDC and _PSD and find out the processor dependency for the
  312. * actual init that will happen later...
  313. */
  314. static int acpi_cpufreq_early_init_acpi(void)
  315. {
  316. struct acpi_processor_performance *data;
  317. unsigned int i, j;
  318. dprintk("acpi_cpufreq_early_init\n");
  319. for_each_cpu(i) {
  320. data = kzalloc(sizeof(struct acpi_processor_performance),
  321. GFP_KERNEL);
  322. if (!data) {
  323. for_each_cpu(j) {
  324. kfree(acpi_perf_data[j]);
  325. acpi_perf_data[j] = NULL;
  326. }
  327. return (-ENOMEM);
  328. }
  329. acpi_perf_data[i] = data;
  330. }
  331. /* Do initialization in ACPI core */
  332. acpi_processor_preregister_performance(acpi_perf_data);
  333. return 0;
  334. }
  335. static int
  336. acpi_cpufreq_cpu_init (
  337. struct cpufreq_policy *policy)
  338. {
  339. unsigned int i;
  340. unsigned int cpu = policy->cpu;
  341. struct cpufreq_acpi_io *data;
  342. unsigned int result = 0;
  343. struct cpuinfo_x86 *c = &cpu_data[policy->cpu];
  344. struct acpi_processor_performance *perf;
  345. dprintk("acpi_cpufreq_cpu_init\n");
  346. if (!acpi_perf_data[cpu])
  347. return (-ENODEV);
  348. data = kzalloc(sizeof(struct cpufreq_acpi_io), GFP_KERNEL);
  349. if (!data)
  350. return (-ENOMEM);
  351. data->acpi_data = acpi_perf_data[cpu];
  352. acpi_io_data[cpu] = data;
  353. result = acpi_processor_register_performance(data->acpi_data, cpu);
  354. if (result)
  355. goto err_free;
  356. perf = data->acpi_data;
  357. policy->cpus = perf->shared_cpu_map;
  358. policy->shared_type = perf->shared_type;
  359. if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
  360. acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
  361. }
  362. /* capability check */
  363. if (perf->state_count <= 1) {
  364. dprintk("No P-States\n");
  365. result = -ENODEV;
  366. goto err_unreg;
  367. }
  368. if ((perf->control_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO) ||
  369. (perf->status_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
  370. dprintk("Unsupported address space [%d, %d]\n",
  371. (u32) (perf->control_register.space_id),
  372. (u32) (perf->status_register.space_id));
  373. result = -ENODEV;
  374. goto err_unreg;
  375. }
  376. /* alloc freq_table */
  377. data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) * (perf->state_count + 1), GFP_KERNEL);
  378. if (!data->freq_table) {
  379. result = -ENOMEM;
  380. goto err_unreg;
  381. }
  382. /* detect transition latency */
  383. policy->cpuinfo.transition_latency = 0;
  384. for (i=0; i<perf->state_count; i++) {
  385. if ((perf->states[i].transition_latency * 1000) > policy->cpuinfo.transition_latency)
  386. policy->cpuinfo.transition_latency = perf->states[i].transition_latency * 1000;
  387. }
  388. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  389. /* The current speed is unknown and not detectable by ACPI... */
  390. policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
  391. /* table init */
  392. for (i=0; i<=perf->state_count; i++)
  393. {
  394. data->freq_table[i].index = i;
  395. if (i<perf->state_count)
  396. data->freq_table[i].frequency = perf->states[i].core_frequency * 1000;
  397. else
  398. data->freq_table[i].frequency = CPUFREQ_TABLE_END;
  399. }
  400. result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
  401. if (result) {
  402. goto err_freqfree;
  403. }
  404. /* notify BIOS that we exist */
  405. acpi_processor_notify_smm(THIS_MODULE);
  406. printk(KERN_INFO "acpi-cpufreq: CPU%u - ACPI performance management activated.\n",
  407. cpu);
  408. for (i = 0; i < perf->state_count; i++)
  409. dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
  410. (i == perf->state?'*':' '), i,
  411. (u32) perf->states[i].core_frequency,
  412. (u32) perf->states[i].power,
  413. (u32) perf->states[i].transition_latency);
  414. cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
  415. /*
  416. * the first call to ->target() should result in us actually
  417. * writing something to the appropriate registers.
  418. */
  419. data->resume = 1;
  420. return (result);
  421. err_freqfree:
  422. kfree(data->freq_table);
  423. err_unreg:
  424. acpi_processor_unregister_performance(perf, cpu);
  425. err_free:
  426. kfree(data);
  427. acpi_io_data[cpu] = NULL;
  428. return (result);
  429. }
  430. static int
  431. acpi_cpufreq_cpu_exit (
  432. struct cpufreq_policy *policy)
  433. {
  434. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  435. dprintk("acpi_cpufreq_cpu_exit\n");
  436. if (data) {
  437. cpufreq_frequency_table_put_attr(policy->cpu);
  438. acpi_io_data[policy->cpu] = NULL;
  439. acpi_processor_unregister_performance(data->acpi_data, policy->cpu);
  440. kfree(data);
  441. }
  442. return (0);
  443. }
  444. static int
  445. acpi_cpufreq_resume (
  446. struct cpufreq_policy *policy)
  447. {
  448. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  449. dprintk("acpi_cpufreq_resume\n");
  450. data->resume = 1;
  451. return (0);
  452. }
  453. static struct freq_attr* acpi_cpufreq_attr[] = {
  454. &cpufreq_freq_attr_scaling_available_freqs,
  455. NULL,
  456. };
  457. static struct cpufreq_driver acpi_cpufreq_driver = {
  458. .verify = acpi_cpufreq_verify,
  459. .target = acpi_cpufreq_target,
  460. .init = acpi_cpufreq_cpu_init,
  461. .exit = acpi_cpufreq_cpu_exit,
  462. .resume = acpi_cpufreq_resume,
  463. .name = "acpi-cpufreq",
  464. .owner = THIS_MODULE,
  465. .attr = acpi_cpufreq_attr,
  466. };
  467. static int __init
  468. acpi_cpufreq_init (void)
  469. {
  470. int result = 0;
  471. dprintk("acpi_cpufreq_init\n");
  472. result = acpi_cpufreq_early_init_acpi();
  473. if (!result)
  474. result = cpufreq_register_driver(&acpi_cpufreq_driver);
  475. return (result);
  476. }
  477. static void __exit
  478. acpi_cpufreq_exit (void)
  479. {
  480. unsigned int i;
  481. dprintk("acpi_cpufreq_exit\n");
  482. cpufreq_unregister_driver(&acpi_cpufreq_driver);
  483. for_each_cpu(i) {
  484. kfree(acpi_perf_data[i]);
  485. acpi_perf_data[i] = NULL;
  486. }
  487. return;
  488. }
  489. module_param(acpi_pstate_strict, uint, 0644);
  490. MODULE_PARM_DESC(acpi_pstate_strict, "value 0 or non-zero. non-zero -> strict ACPI checks are performed during frequency changes.");
  491. late_initcall(acpi_cpufreq_init);
  492. module_exit(acpi_cpufreq_exit);
  493. MODULE_ALIAS("acpi");