acpi-cpufreq.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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 <linux/dmi.h>
  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. value = (u32) perf->states[state].status;
  155. }
  156. if (unlikely(value != (u32) perf->states[state].status)) {
  157. printk(KERN_WARNING "acpi-cpufreq: Transition failed\n");
  158. retval = -ENODEV;
  159. return (retval);
  160. }
  161. dprintk("Transition successful after %d microseconds\n", i * 10);
  162. perf->state = state;
  163. return (retval);
  164. }
  165. static int
  166. acpi_cpufreq_target (
  167. struct cpufreq_policy *policy,
  168. unsigned int target_freq,
  169. unsigned int relation)
  170. {
  171. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  172. struct acpi_processor_performance *perf;
  173. struct cpufreq_freqs freqs;
  174. cpumask_t online_policy_cpus;
  175. cpumask_t saved_mask;
  176. cpumask_t set_mask;
  177. cpumask_t covered_cpus;
  178. unsigned int cur_state = 0;
  179. unsigned int next_state = 0;
  180. unsigned int result = 0;
  181. unsigned int j;
  182. unsigned int tmp;
  183. dprintk("acpi_cpufreq_setpolicy\n");
  184. result = cpufreq_frequency_table_target(policy,
  185. data->freq_table,
  186. target_freq,
  187. relation,
  188. &next_state);
  189. if (unlikely(result))
  190. return (result);
  191. perf = data->acpi_data;
  192. cur_state = perf->state;
  193. freqs.old = data->freq_table[cur_state].frequency;
  194. freqs.new = data->freq_table[next_state].frequency;
  195. #ifdef CONFIG_HOTPLUG_CPU
  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. #else
  199. online_policy_cpus = policy->cpus;
  200. #endif
  201. for_each_cpu_mask(j, online_policy_cpus) {
  202. freqs.cpu = j;
  203. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  204. }
  205. /*
  206. * We need to call driver->target() on all or any CPU in
  207. * policy->cpus, depending on policy->shared_type.
  208. */
  209. saved_mask = current->cpus_allowed;
  210. cpus_clear(covered_cpus);
  211. for_each_cpu_mask(j, online_policy_cpus) {
  212. /*
  213. * Support for SMP systems.
  214. * Make sure we are running on CPU that wants to change freq
  215. */
  216. cpus_clear(set_mask);
  217. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
  218. cpus_or(set_mask, set_mask, online_policy_cpus);
  219. else
  220. cpu_set(j, set_mask);
  221. set_cpus_allowed(current, set_mask);
  222. if (unlikely(!cpu_isset(smp_processor_id(), set_mask))) {
  223. dprintk("couldn't limit to CPUs in this domain\n");
  224. result = -EAGAIN;
  225. break;
  226. }
  227. result = acpi_processor_set_performance (data, j, next_state);
  228. if (result) {
  229. result = -EAGAIN;
  230. break;
  231. }
  232. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
  233. break;
  234. cpu_set(j, covered_cpus);
  235. }
  236. for_each_cpu_mask(j, online_policy_cpus) {
  237. freqs.cpu = j;
  238. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  239. }
  240. if (unlikely(result)) {
  241. /*
  242. * We have failed halfway through the frequency change.
  243. * We have sent callbacks to online_policy_cpus and
  244. * acpi_processor_set_performance() has been called on
  245. * coverd_cpus. Best effort undo..
  246. */
  247. if (!cpus_empty(covered_cpus)) {
  248. for_each_cpu_mask(j, covered_cpus) {
  249. policy->cpu = j;
  250. acpi_processor_set_performance (data,
  251. j,
  252. cur_state);
  253. }
  254. }
  255. tmp = freqs.new;
  256. freqs.new = freqs.old;
  257. freqs.old = tmp;
  258. for_each_cpu_mask(j, online_policy_cpus) {
  259. freqs.cpu = j;
  260. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  261. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  262. }
  263. }
  264. set_cpus_allowed(current, saved_mask);
  265. return (result);
  266. }
  267. static int
  268. acpi_cpufreq_verify (
  269. struct cpufreq_policy *policy)
  270. {
  271. unsigned int result = 0;
  272. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  273. dprintk("acpi_cpufreq_verify\n");
  274. result = cpufreq_frequency_table_verify(policy,
  275. data->freq_table);
  276. return (result);
  277. }
  278. static unsigned long
  279. acpi_cpufreq_guess_freq (
  280. struct cpufreq_acpi_io *data,
  281. unsigned int cpu)
  282. {
  283. struct acpi_processor_performance *perf = data->acpi_data;
  284. if (cpu_khz) {
  285. /* search the closest match to cpu_khz */
  286. unsigned int i;
  287. unsigned long freq;
  288. unsigned long freqn = perf->states[0].core_frequency * 1000;
  289. for (i = 0; i < (perf->state_count - 1); i++) {
  290. freq = freqn;
  291. freqn = perf->states[i+1].core_frequency * 1000;
  292. if ((2 * cpu_khz) > (freqn + freq)) {
  293. perf->state = i;
  294. return (freq);
  295. }
  296. }
  297. perf->state = perf->state_count - 1;
  298. return (freqn);
  299. } else {
  300. /* assume CPU is at P0... */
  301. perf->state = 0;
  302. return perf->states[0].core_frequency * 1000;
  303. }
  304. }
  305. /*
  306. * acpi_cpufreq_early_init - initialize ACPI P-States library
  307. *
  308. * Initialize the ACPI P-States library (drivers/acpi/processor_perflib.c)
  309. * in order to determine correct frequency and voltage pairings. We can
  310. * do _PDC and _PSD and find out the processor dependency for the
  311. * actual init that will happen later...
  312. */
  313. static int acpi_cpufreq_early_init_acpi(void)
  314. {
  315. struct acpi_processor_performance *data;
  316. unsigned int i, j;
  317. dprintk("acpi_cpufreq_early_init\n");
  318. for_each_possible_cpu(i) {
  319. data = kzalloc(sizeof(struct acpi_processor_performance),
  320. GFP_KERNEL);
  321. if (!data) {
  322. for_each_possible_cpu(j) {
  323. kfree(acpi_perf_data[j]);
  324. acpi_perf_data[j] = NULL;
  325. }
  326. return (-ENOMEM);
  327. }
  328. acpi_perf_data[i] = data;
  329. }
  330. /* Do initialization in ACPI core */
  331. return acpi_processor_preregister_performance(acpi_perf_data);
  332. }
  333. /*
  334. * Some BIOSes do SW_ANY coordination internally, either set it up in hw
  335. * or do it in BIOS firmware and won't inform about it to OS. If not
  336. * detected, this has a side effect of making CPU run at a different speed
  337. * than OS intended it to run at. Detect it and handle it cleanly.
  338. */
  339. static int bios_with_sw_any_bug;
  340. static int sw_any_bug_found(struct dmi_system_id *d)
  341. {
  342. bios_with_sw_any_bug = 1;
  343. return 0;
  344. }
  345. static struct dmi_system_id sw_any_bug_dmi_table[] = {
  346. {
  347. .callback = sw_any_bug_found,
  348. .ident = "Supermicro Server X6DLP",
  349. .matches = {
  350. DMI_MATCH(DMI_SYS_VENDOR, "Supermicro"),
  351. DMI_MATCH(DMI_BIOS_VERSION, "080010"),
  352. DMI_MATCH(DMI_PRODUCT_NAME, "X6DLP"),
  353. },
  354. },
  355. { }
  356. };
  357. static int
  358. acpi_cpufreq_cpu_init (
  359. struct cpufreq_policy *policy)
  360. {
  361. unsigned int i;
  362. unsigned int cpu = policy->cpu;
  363. struct cpufreq_acpi_io *data;
  364. unsigned int result = 0;
  365. struct cpuinfo_x86 *c = &cpu_data[policy->cpu];
  366. struct acpi_processor_performance *perf;
  367. dprintk("acpi_cpufreq_cpu_init\n");
  368. if (!acpi_perf_data[cpu])
  369. return (-ENODEV);
  370. data = kzalloc(sizeof(struct cpufreq_acpi_io), GFP_KERNEL);
  371. if (!data)
  372. return (-ENOMEM);
  373. data->acpi_data = acpi_perf_data[cpu];
  374. acpi_io_data[cpu] = data;
  375. result = acpi_processor_register_performance(data->acpi_data, cpu);
  376. if (result)
  377. goto err_free;
  378. perf = data->acpi_data;
  379. policy->shared_type = perf->shared_type;
  380. /*
  381. * Will let policy->cpus know about dependency only when software
  382. * coordination is required.
  383. */
  384. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL ||
  385. policy->shared_type == CPUFREQ_SHARED_TYPE_ANY) {
  386. policy->cpus = perf->shared_cpu_map;
  387. }
  388. #ifdef CONFIG_SMP
  389. dmi_check_system(sw_any_bug_dmi_table);
  390. if (bios_with_sw_any_bug && cpus_weight(policy->cpus) == 1) {
  391. policy->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  392. policy->cpus = cpu_core_map[cpu];
  393. }
  394. #endif
  395. if (cpu_has(c, X86_FEATURE_CONSTANT_TSC)) {
  396. acpi_cpufreq_driver.flags |= CPUFREQ_CONST_LOOPS;
  397. }
  398. /* capability check */
  399. if (perf->state_count <= 1) {
  400. dprintk("No P-States\n");
  401. result = -ENODEV;
  402. goto err_unreg;
  403. }
  404. if ((perf->control_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO) ||
  405. (perf->status_register.space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
  406. dprintk("Unsupported address space [%d, %d]\n",
  407. (u32) (perf->control_register.space_id),
  408. (u32) (perf->status_register.space_id));
  409. result = -ENODEV;
  410. goto err_unreg;
  411. }
  412. /* alloc freq_table */
  413. data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) * (perf->state_count + 1), GFP_KERNEL);
  414. if (!data->freq_table) {
  415. result = -ENOMEM;
  416. goto err_unreg;
  417. }
  418. /* detect transition latency */
  419. policy->cpuinfo.transition_latency = 0;
  420. for (i=0; i<perf->state_count; i++) {
  421. if ((perf->states[i].transition_latency * 1000) > policy->cpuinfo.transition_latency)
  422. policy->cpuinfo.transition_latency = perf->states[i].transition_latency * 1000;
  423. }
  424. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  425. /* The current speed is unknown and not detectable by ACPI... */
  426. policy->cur = acpi_cpufreq_guess_freq(data, policy->cpu);
  427. /* table init */
  428. for (i=0; i<=perf->state_count; i++)
  429. {
  430. data->freq_table[i].index = i;
  431. if (i<perf->state_count)
  432. data->freq_table[i].frequency = perf->states[i].core_frequency * 1000;
  433. else
  434. data->freq_table[i].frequency = CPUFREQ_TABLE_END;
  435. }
  436. result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
  437. if (result) {
  438. goto err_freqfree;
  439. }
  440. /* notify BIOS that we exist */
  441. acpi_processor_notify_smm(THIS_MODULE);
  442. printk(KERN_INFO "acpi-cpufreq: CPU%u - ACPI performance management activated.\n",
  443. cpu);
  444. for (i = 0; i < perf->state_count; i++)
  445. dprintk(" %cP%d: %d MHz, %d mW, %d uS\n",
  446. (i == perf->state?'*':' '), i,
  447. (u32) perf->states[i].core_frequency,
  448. (u32) perf->states[i].power,
  449. (u32) perf->states[i].transition_latency);
  450. cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
  451. /*
  452. * the first call to ->target() should result in us actually
  453. * writing something to the appropriate registers.
  454. */
  455. data->resume = 1;
  456. return (result);
  457. err_freqfree:
  458. kfree(data->freq_table);
  459. err_unreg:
  460. acpi_processor_unregister_performance(perf, cpu);
  461. err_free:
  462. kfree(data);
  463. acpi_io_data[cpu] = NULL;
  464. return (result);
  465. }
  466. static int
  467. acpi_cpufreq_cpu_exit (
  468. struct cpufreq_policy *policy)
  469. {
  470. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  471. dprintk("acpi_cpufreq_cpu_exit\n");
  472. if (data) {
  473. cpufreq_frequency_table_put_attr(policy->cpu);
  474. acpi_io_data[policy->cpu] = NULL;
  475. acpi_processor_unregister_performance(data->acpi_data, policy->cpu);
  476. kfree(data);
  477. }
  478. return (0);
  479. }
  480. static int
  481. acpi_cpufreq_resume (
  482. struct cpufreq_policy *policy)
  483. {
  484. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  485. dprintk("acpi_cpufreq_resume\n");
  486. data->resume = 1;
  487. return (0);
  488. }
  489. static struct freq_attr* acpi_cpufreq_attr[] = {
  490. &cpufreq_freq_attr_scaling_available_freqs,
  491. NULL,
  492. };
  493. static struct cpufreq_driver acpi_cpufreq_driver = {
  494. .verify = acpi_cpufreq_verify,
  495. .target = acpi_cpufreq_target,
  496. .init = acpi_cpufreq_cpu_init,
  497. .exit = acpi_cpufreq_cpu_exit,
  498. .resume = acpi_cpufreq_resume,
  499. .name = "acpi-cpufreq",
  500. .owner = THIS_MODULE,
  501. .attr = acpi_cpufreq_attr,
  502. };
  503. static int __init
  504. acpi_cpufreq_init (void)
  505. {
  506. dprintk("acpi_cpufreq_init\n");
  507. acpi_cpufreq_early_init_acpi();
  508. return cpufreq_register_driver(&acpi_cpufreq_driver);
  509. }
  510. static void __exit
  511. acpi_cpufreq_exit (void)
  512. {
  513. unsigned int i;
  514. dprintk("acpi_cpufreq_exit\n");
  515. cpufreq_unregister_driver(&acpi_cpufreq_driver);
  516. for_each_possible_cpu(i) {
  517. kfree(acpi_perf_data[i]);
  518. acpi_perf_data[i] = NULL;
  519. }
  520. return;
  521. }
  522. module_param(acpi_pstate_strict, uint, 0644);
  523. MODULE_PARM_DESC(acpi_pstate_strict, "value 0 or non-zero. non-zero -> strict ACPI checks are performed during frequency changes.");
  524. late_initcall(acpi_cpufreq_init);
  525. module_exit(acpi_cpufreq_exit);
  526. MODULE_ALIAS("acpi");