acpi-cpufreq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. /* cpufreq holds the hotplug lock, so we are safe from here on */
  197. cpus_and(online_policy_cpus, cpu_online_map, policy->cpus);
  198. for_each_cpu_mask(j, online_policy_cpus) {
  199. freqs.cpu = j;
  200. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  201. }
  202. /*
  203. * We need to call driver->target() on all or any CPU in
  204. * policy->cpus, depending on policy->shared_type.
  205. */
  206. saved_mask = current->cpus_allowed;
  207. cpus_clear(covered_cpus);
  208. for_each_cpu_mask(j, online_policy_cpus) {
  209. /*
  210. * Support for SMP systems.
  211. * Make sure we are running on CPU that wants to change freq
  212. */
  213. cpus_clear(set_mask);
  214. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
  215. cpus_or(set_mask, set_mask, online_policy_cpus);
  216. else
  217. cpu_set(j, set_mask);
  218. set_cpus_allowed(current, set_mask);
  219. if (unlikely(!cpu_isset(smp_processor_id(), set_mask))) {
  220. dprintk("couldn't limit to CPUs in this domain\n");
  221. result = -EAGAIN;
  222. break;
  223. }
  224. result = acpi_processor_set_performance (data, j, next_state);
  225. if (result) {
  226. result = -EAGAIN;
  227. break;
  228. }
  229. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
  230. break;
  231. cpu_set(j, covered_cpus);
  232. }
  233. for_each_cpu_mask(j, online_policy_cpus) {
  234. freqs.cpu = j;
  235. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  236. }
  237. if (unlikely(result)) {
  238. /*
  239. * We have failed halfway through the frequency change.
  240. * We have sent callbacks to online_policy_cpus and
  241. * acpi_processor_set_performance() has been called on
  242. * coverd_cpus. Best effort undo..
  243. */
  244. if (!cpus_empty(covered_cpus)) {
  245. for_each_cpu_mask(j, covered_cpus) {
  246. policy->cpu = j;
  247. acpi_processor_set_performance (data,
  248. j,
  249. cur_state);
  250. }
  251. }
  252. tmp = freqs.new;
  253. freqs.new = freqs.old;
  254. freqs.old = tmp;
  255. for_each_cpu_mask(j, online_policy_cpus) {
  256. freqs.cpu = j;
  257. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  258. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  259. }
  260. }
  261. set_cpus_allowed(current, saved_mask);
  262. return (result);
  263. }
  264. static int
  265. acpi_cpufreq_verify (
  266. struct cpufreq_policy *policy)
  267. {
  268. unsigned int result = 0;
  269. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  270. dprintk("acpi_cpufreq_verify\n");
  271. result = cpufreq_frequency_table_verify(policy,
  272. data->freq_table);
  273. return (result);
  274. }
  275. static unsigned long
  276. acpi_cpufreq_guess_freq (
  277. struct cpufreq_acpi_io *data,
  278. unsigned int cpu)
  279. {
  280. struct acpi_processor_performance *perf = data->acpi_data;
  281. if (cpu_khz) {
  282. /* search the closest match to cpu_khz */
  283. unsigned int i;
  284. unsigned long freq;
  285. unsigned long freqn = perf->states[0].core_frequency * 1000;
  286. for (i = 0; i < (perf->state_count - 1); i++) {
  287. freq = freqn;
  288. freqn = perf->states[i+1].core_frequency * 1000;
  289. if ((2 * cpu_khz) > (freqn + freq)) {
  290. perf->state = i;
  291. return (freq);
  292. }
  293. }
  294. perf->state = perf->state_count - 1;
  295. return (freqn);
  296. } else {
  297. /* assume CPU is at P0... */
  298. perf->state = 0;
  299. return perf->states[0].core_frequency * 1000;
  300. }
  301. }
  302. /*
  303. * acpi_cpufreq_early_init - initialize ACPI P-States library
  304. *
  305. * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
  306. * in order to determine correct frequency and voltage pairings. We can
  307. * do _PDC and _PSD and find out the processor dependency for the
  308. * actual init that will happen later...
  309. */
  310. static int acpi_cpufreq_early_init_acpi(void)
  311. {
  312. struct acpi_processor_performance *data;
  313. unsigned int i, j;
  314. dprintk("acpi_cpufreq_early_init\n");
  315. for_each_cpu(i) {
  316. data = kzalloc(sizeof(struct acpi_processor_performance),
  317. GFP_KERNEL);
  318. if (!data) {
  319. for_each_cpu(j) {
  320. kfree(acpi_perf_data[j]);
  321. acpi_perf_data[j] = NULL;
  322. }
  323. return (-ENOMEM);
  324. }
  325. acpi_perf_data[i] = data;
  326. }
  327. /* Do initialization in ACPI core */
  328. acpi_processor_preregister_performance(acpi_perf_data);
  329. return 0;
  330. }
  331. static int
  332. acpi_cpufreq_cpu_init (
  333. struct cpufreq_policy *policy)
  334. {
  335. unsigned int i;
  336. unsigned int cpu = policy->cpu;
  337. struct cpufreq_acpi_io *data;
  338. unsigned int result = 0;
  339. struct cpuinfo_x86 *c = &cpu_data[policy->cpu];
  340. struct acpi_processor_performance *perf;
  341. dprintk("acpi_cpufreq_cpu_init\n");
  342. if (!acpi_perf_data[cpu])
  343. return (-ENODEV);
  344. data = kzalloc(sizeof(struct cpufreq_acpi_io), GFP_KERNEL);
  345. if (!data)
  346. return (-ENOMEM);
  347. data->acpi_data = acpi_perf_data[cpu];
  348. acpi_io_data[cpu] = data;
  349. result = acpi_processor_register_performance(data->acpi_data, cpu);
  350. if (result)
  351. goto err_free;
  352. perf = data->acpi_data;
  353. policy->cpus = perf->shared_cpu_map;
  354. policy->shared_type = perf->shared_type;
  355. if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
  356. acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
  357. }
  358. /* capability check */
  359. if (perf->state_count <= 1) {
  360. dprintk("No P-States\n");
  361. result = -ENODEV;
  362. goto err_unreg;
  363. }
  364. if ((perf->control_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO) ||
  365. (perf->status_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
  366. dprintk("Unsupported address space [%d, %d]\n",
  367. (u32) (perf->control_register.space_id),
  368. (u32) (perf->status_register.space_id));
  369. result = -ENODEV;
  370. goto err_unreg;
  371. }
  372. /* alloc freq_table */
  373. data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) * (perf->state_count + 1), GFP_KERNEL);
  374. if (!data->freq_table) {
  375. result = -ENOMEM;
  376. goto err_unreg;
  377. }
  378. /* detect transition latency */
  379. policy->cpuinfo.transition_latency = 0;
  380. for (i=0; i<perf->state_count; i++) {
  381. if ((perf->states[i].transition_latency * 1000) > policy->cpuinfo.transition_latency)
  382. policy->cpuinfo.transition_latency = perf->states[i].transition_latency * 1000;
  383. }
  384. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  385. /* The current speed is unknown and not detectable by ACPI... */
  386. policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
  387. /* table init */
  388. for (i=0; i<=perf->state_count; i++)
  389. {
  390. data->freq_table[i].index = i;
  391. if (i<perf->state_count)
  392. data->freq_table[i].frequency = perf->states[i].core_frequency * 1000;
  393. else
  394. data->freq_table[i].frequency = CPUFREQ_TABLE_END;
  395. }
  396. result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
  397. if (result) {
  398. goto err_freqfree;
  399. }
  400. /* notify BIOS that we exist */
  401. acpi_processor_notify_smm(THIS_MODULE);
  402. printk(KERN_INFO "acpi-cpufreq: CPU%u - ACPI performance management activated.\n",
  403. cpu);
  404. for (i = 0; i < perf->state_count; i++)
  405. dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
  406. (i == perf->state?'*':' '), i,
  407. (u32) perf->states[i].core_frequency,
  408. (u32) perf->states[i].power,
  409. (u32) perf->states[i].transition_latency);
  410. cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
  411. /*
  412. * the first call to ->target() should result in us actually
  413. * writing something to the appropriate registers.
  414. */
  415. data->resume = 1;
  416. return (result);
  417. err_freqfree:
  418. kfree(data->freq_table);
  419. err_unreg:
  420. acpi_processor_unregister_performance(perf, cpu);
  421. err_free:
  422. kfree(data);
  423. acpi_io_data[cpu] = NULL;
  424. return (result);
  425. }
  426. static int
  427. acpi_cpufreq_cpu_exit (
  428. struct cpufreq_policy *policy)
  429. {
  430. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  431. dprintk("acpi_cpufreq_cpu_exit\n");
  432. if (data) {
  433. cpufreq_frequency_table_put_attr(policy->cpu);
  434. acpi_io_data[policy->cpu] = NULL;
  435. acpi_processor_unregister_performance(data->acpi_data, policy->cpu);
  436. kfree(data);
  437. }
  438. return (0);
  439. }
  440. static int
  441. acpi_cpufreq_resume (
  442. struct cpufreq_policy *policy)
  443. {
  444. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  445. dprintk("acpi_cpufreq_resume\n");
  446. data->resume = 1;
  447. return (0);
  448. }
  449. static struct freq_attr* acpi_cpufreq_attr[] = {
  450. &cpufreq_freq_attr_scaling_available_freqs,
  451. NULL,
  452. };
  453. static struct cpufreq_driver acpi_cpufreq_driver = {
  454. .verify = acpi_cpufreq_verify,
  455. .target = acpi_cpufreq_target,
  456. .init = acpi_cpufreq_cpu_init,
  457. .exit = acpi_cpufreq_cpu_exit,
  458. .resume = acpi_cpufreq_resume,
  459. .name = "acpi-cpufreq",
  460. .owner = THIS_MODULE,
  461. .attr = acpi_cpufreq_attr,
  462. };
  463. static int __init
  464. acpi_cpufreq_init (void)
  465. {
  466. int result = 0;
  467. dprintk("acpi_cpufreq_init\n");
  468. result = acpi_cpufreq_early_init_acpi();
  469. if (!result)
  470. result = cpufreq_register_driver(&acpi_cpufreq_driver);
  471. return (result);
  472. }
  473. static void __exit
  474. acpi_cpufreq_exit (void)
  475. {
  476. unsigned int i;
  477. dprintk("acpi_cpufreq_exit\n");
  478. cpufreq_unregister_driver(&acpi_cpufreq_driver);
  479. for_each_cpu(i) {
  480. kfree(acpi_perf_data[i]);
  481. acpi_perf_data[i] = NULL;
  482. }
  483. return;
  484. }
  485. module_param(acpi_pstate_strict, uint, 0644);
  486. MODULE_PARM_DESC(acpi_pstate_strict, "value 0 or non-zero. non-zero -> strict ACPI checks are performed during frequency changes.");
  487. late_initcall(acpi_cpufreq_init);
  488. module_exit(acpi_cpufreq_exit);
  489. MODULE_ALIAS("acpi");