acpi-cpufreq.c 14 KB

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