acpi-cpufreq.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. acpi_processor_preregister_performance(acpi_perf_data);
  331. return 0;
  332. }
  333. static int
  334. acpi_cpufreq_cpu_init (
  335. struct cpufreq_policy *policy)
  336. {
  337. unsigned int i;
  338. unsigned int cpu = policy->cpu;
  339. struct cpufreq_acpi_io *data;
  340. unsigned int result = 0;
  341. struct cpuinfo_x86 *c = &cpu_data[policy->cpu];
  342. struct acpi_processor_performance *perf;
  343. dprintk("acpi_cpufreq_cpu_init\n");
  344. if (!acpi_perf_data[cpu])
  345. return (-ENODEV);
  346. data = kzalloc(sizeof(struct cpufreq_acpi_io), GFP_KERNEL);
  347. if (!data)
  348. return (-ENOMEM);
  349. data->acpi_data = acpi_perf_data[cpu];
  350. acpi_io_data[cpu] = data;
  351. result = acpi_processor_register_performance(data->acpi_data, cpu);
  352. if (result)
  353. goto err_free;
  354. perf = data->acpi_data;
  355. policy->shared_type = perf->shared_type;
  356. /*
  357. * Will let policy->cpus know about dependency only when software
  358. * coordination is required.
  359. */
  360. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
  361. policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
  362. policy->cpus = perf->shared_cpu_map;
  363. if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
  364. acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
  365. }
  366. /* capability check */
  367. if (perf->state_count <= 1) {
  368. dprintk("No P-States\n");
  369. result = -ENODEV;
  370. goto err_unreg;
  371. }
  372. if ((perf->control_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO) ||
  373. (perf->status_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
  374. dprintk("Unsupported address space [%d, %d]\n",
  375. (u32) (perf->control_register.space_id),
  376. (u32) (perf->status_register.space_id));
  377. result = -ENODEV;
  378. goto err_unreg;
  379. }
  380. /* alloc freq_table */
  381. data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) * (perf->state_count + 1), GFP_KERNEL);
  382. if (!data->freq_table) {
  383. result = -ENOMEM;
  384. goto err_unreg;
  385. }
  386. /* detect transition latency */
  387. policy->cpuinfo.transition_latency = 0;
  388. for (i=0; i<perf->state_count; i++) {
  389. if ((perf->states[i].transition_latency * 1000) > policy->cpuinfo.transition_latency)
  390. policy->cpuinfo.transition_latency = perf->states[i].transition_latency * 1000;
  391. }
  392. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  393. /* The current speed is unknown and not detectable by ACPI... */
  394. policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
  395. /* table init */
  396. for (i=0; i<=perf->state_count; i++)
  397. {
  398. data->freq_table[i].index = i;
  399. if (i<perf->state_count)
  400. data->freq_table[i].frequency = perf->states[i].core_frequency * 1000;
  401. else
  402. data->freq_table[i].frequency = CPUFREQ_TABLE_END;
  403. }
  404. result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
  405. if (result) {
  406. goto err_freqfree;
  407. }
  408. /* notify BIOS that we exist */
  409. acpi_processor_notify_smm(THIS_MODULE);
  410. printk(KERN_INFO "acpi-cpufreq: CPU%u - ACPI performance management activated.\n",
  411. cpu);
  412. for (i = 0; i < perf->state_count; i++)
  413. dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
  414. (i == perf->state?'*':' '), i,
  415. (u32) perf->states[i].core_frequency,
  416. (u32) perf->states[i].power,
  417. (u32) perf->states[i].transition_latency);
  418. cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
  419. /*
  420. * the first call to ->target() should result in us actually
  421. * writing something to the appropriate registers.
  422. */
  423. data->resume = 1;
  424. return (result);
  425. err_freqfree:
  426. kfree(data->freq_table);
  427. err_unreg:
  428. acpi_processor_unregister_performance(perf, cpu);
  429. err_free:
  430. kfree(data);
  431. acpi_io_data[cpu] = NULL;
  432. return (result);
  433. }
  434. static int
  435. acpi_cpufreq_cpu_exit (
  436. struct cpufreq_policy *policy)
  437. {
  438. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  439. dprintk("acpi_cpufreq_cpu_exit\n");
  440. if (data) {
  441. cpufreq_frequency_table_put_attr(policy->cpu);
  442. acpi_io_data[policy->cpu] = NULL;
  443. acpi_processor_unregister_performance(data->acpi_data, policy->cpu);
  444. kfree(data);
  445. }
  446. return (0);
  447. }
  448. static int
  449. acpi_cpufreq_resume (
  450. struct cpufreq_policy *policy)
  451. {
  452. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  453. dprintk("acpi_cpufreq_resume\n");
  454. data->resume = 1;
  455. return (0);
  456. }
  457. static struct freq_attr* acpi_cpufreq_attr[] = {
  458. &cpufreq_freq_attr_scaling_available_freqs,
  459. NULL,
  460. };
  461. static struct cpufreq_driver acpi_cpufreq_driver = {
  462. .verify = acpi_cpufreq_verify,
  463. .target = acpi_cpufreq_target,
  464. .init = acpi_cpufreq_cpu_init,
  465. .exit = acpi_cpufreq_cpu_exit,
  466. .resume = acpi_cpufreq_resume,
  467. .name = "acpi-cpufreq",
  468. .owner = THIS_MODULE,
  469. .attr = acpi_cpufreq_attr,
  470. .flags = CPUFREQ_STICKY,
  471. };
  472. static int __init
  473. acpi_cpufreq_init (void)
  474. {
  475. int result = 0;
  476. dprintk("acpi_cpufreq_init\n");
  477. result = acpi_cpufreq_early_init_acpi();
  478. if (!result)
  479. result = cpufreq_register_driver(&acpi_cpufreq_driver);
  480. return (result);
  481. }
  482. static void __exit
  483. acpi_cpufreq_exit (void)
  484. {
  485. unsigned int i;
  486. dprintk("acpi_cpufreq_exit\n");
  487. cpufreq_unregister_driver(&acpi_cpufreq_driver);
  488. for_each_possible_cpu(i) {
  489. kfree(acpi_perf_data[i]);
  490. acpi_perf_data[i] = NULL;
  491. }
  492. return;
  493. }
  494. module_param(acpi_pstate_strict, uint, 0644);
  495. MODULE_PARM_DESC(acpi_pstate_strict, "value 0 or non-zero. non-zero -> strict ACPI checks are performed during frequency changes.");
  496. late_initcall(acpi_cpufreq_init);
  497. module_exit(acpi_cpufreq_exit);
  498. MODULE_ALIAS("acpi");