acpi-cpufreq.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * arch/ia64/kernel/cpufreq/acpi-cpufreq.c
  3. * This file provides the ACPI based P-state support. This
  4. * module works with generic cpufreq infrastructure. Most of
  5. * the code is based on i386 version
  6. * (arch/i386/kernel/cpu/cpufreq/acpi-cpufreq.c)
  7. *
  8. * Copyright (C) 2005 Intel Corp
  9. * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/cpufreq.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/seq_file.h>
  18. #include <asm/io.h>
  19. #include <asm/uaccess.h>
  20. #include <asm/pal.h>
  21. #include <linux/acpi.h>
  22. #include <acpi/processor.h>
  23. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "acpi-cpufreq", msg)
  24. MODULE_AUTHOR("Venkatesh Pallipadi");
  25. MODULE_DESCRIPTION("ACPI Processor P-States Driver");
  26. MODULE_LICENSE("GPL");
  27. struct cpufreq_acpi_io {
  28. struct acpi_processor_performance acpi_data;
  29. struct cpufreq_frequency_table *freq_table;
  30. unsigned int resume;
  31. };
  32. static struct cpufreq_acpi_io *acpi_io_data[NR_CPUS];
  33. static struct cpufreq_driver acpi_cpufreq_driver;
  34. static int
  35. processor_set_pstate (
  36. u32 value)
  37. {
  38. s64 retval;
  39. dprintk("processor_set_pstate\n");
  40. retval = ia64_pal_set_pstate((u64)value);
  41. if (retval) {
  42. dprintk("Failed to set freq to 0x%x, with error 0x%lx\n",
  43. value, retval);
  44. return -ENODEV;
  45. }
  46. return (int)retval;
  47. }
  48. static int
  49. processor_get_pstate (
  50. u32 *value)
  51. {
  52. u64 pstate_index = 0;
  53. s64 retval;
  54. dprintk("processor_get_pstate\n");
  55. retval = ia64_pal_get_pstate(&pstate_index,
  56. PAL_GET_PSTATE_TYPE_INSTANT);
  57. *value = (u32) pstate_index;
  58. if (retval)
  59. dprintk("Failed to get current freq with "
  60. "error 0x%lx, idx 0x%x\n", retval, *value);
  61. return (int)retval;
  62. }
  63. /* To be used only after data->acpi_data is initialized */
  64. static unsigned
  65. extract_clock (
  66. struct cpufreq_acpi_io *data,
  67. unsigned value,
  68. unsigned int cpu)
  69. {
  70. unsigned long i;
  71. dprintk("extract_clock\n");
  72. for (i = 0; i < data->acpi_data.state_count; i++) {
  73. if (value == data->acpi_data.states[i].status)
  74. return data->acpi_data.states[i].core_frequency;
  75. }
  76. return data->acpi_data.states[i-1].core_frequency;
  77. }
  78. static unsigned int
  79. processor_get_freq (
  80. struct cpufreq_acpi_io *data,
  81. unsigned int cpu)
  82. {
  83. int ret = 0;
  84. u32 value = 0;
  85. cpumask_t saved_mask;
  86. unsigned long clock_freq;
  87. dprintk("processor_get_freq\n");
  88. saved_mask = current->cpus_allowed;
  89. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  90. if (smp_processor_id() != cpu)
  91. goto migrate_end;
  92. /* processor_get_pstate gets the instantaneous frequency */
  93. ret = processor_get_pstate(&value);
  94. if (ret) {
  95. set_cpus_allowed_ptr(current, &saved_mask);
  96. printk(KERN_WARNING "get performance failed with error %d\n",
  97. ret);
  98. ret = 0;
  99. goto migrate_end;
  100. }
  101. clock_freq = extract_clock(data, value, cpu);
  102. ret = (clock_freq*1000);
  103. migrate_end:
  104. set_cpus_allowed_ptr(current, &saved_mask);
  105. return ret;
  106. }
  107. static int
  108. processor_set_freq (
  109. struct cpufreq_acpi_io *data,
  110. unsigned int cpu,
  111. int state)
  112. {
  113. int ret = 0;
  114. u32 value = 0;
  115. struct cpufreq_freqs cpufreq_freqs;
  116. cpumask_t saved_mask;
  117. int retval;
  118. dprintk("processor_set_freq\n");
  119. saved_mask = current->cpus_allowed;
  120. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  121. if (smp_processor_id() != cpu) {
  122. retval = -EAGAIN;
  123. goto migrate_end;
  124. }
  125. if (state == data->acpi_data.state) {
  126. if (unlikely(data->resume)) {
  127. dprintk("Called after resume, resetting to P%d\n", state);
  128. data->resume = 0;
  129. } else {
  130. dprintk("Already at target state (P%d)\n", state);
  131. retval = 0;
  132. goto migrate_end;
  133. }
  134. }
  135. dprintk("Transitioning from P%d to P%d\n",
  136. data->acpi_data.state, state);
  137. /* cpufreq frequency struct */
  138. cpufreq_freqs.cpu = cpu;
  139. cpufreq_freqs.old = data->freq_table[data->acpi_data.state].frequency;
  140. cpufreq_freqs.new = data->freq_table[state].frequency;
  141. /* notify cpufreq */
  142. cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_PRECHANGE);
  143. /*
  144. * First we write the target state's 'control' value to the
  145. * control_register.
  146. */
  147. value = (u32) data->acpi_data.states[state].control;
  148. dprintk("Transitioning to state: 0x%08x\n", value);
  149. ret = processor_set_pstate(value);
  150. if (ret) {
  151. unsigned int tmp = cpufreq_freqs.new;
  152. cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_POSTCHANGE);
  153. cpufreq_freqs.new = cpufreq_freqs.old;
  154. cpufreq_freqs.old = tmp;
  155. cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_PRECHANGE);
  156. cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_POSTCHANGE);
  157. printk(KERN_WARNING "Transition failed with error %d\n", ret);
  158. retval = -ENODEV;
  159. goto migrate_end;
  160. }
  161. cpufreq_notify_transition(&cpufreq_freqs, CPUFREQ_POSTCHANGE);
  162. data->acpi_data.state = state;
  163. retval = 0;
  164. migrate_end:
  165. set_cpus_allowed_ptr(current, &saved_mask);
  166. return (retval);
  167. }
  168. static unsigned int
  169. acpi_cpufreq_get (
  170. unsigned int cpu)
  171. {
  172. struct cpufreq_acpi_io *data = acpi_io_data[cpu];
  173. dprintk("acpi_cpufreq_get\n");
  174. return processor_get_freq(data, cpu);
  175. }
  176. static int
  177. acpi_cpufreq_target (
  178. struct cpufreq_policy *policy,
  179. unsigned int target_freq,
  180. unsigned int relation)
  181. {
  182. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  183. unsigned int next_state = 0;
  184. unsigned int result = 0;
  185. dprintk("acpi_cpufreq_setpolicy\n");
  186. result = cpufreq_frequency_table_target(policy,
  187. data->freq_table, target_freq, relation, &next_state);
  188. if (result)
  189. return (result);
  190. result = processor_set_freq(data, policy->cpu, next_state);
  191. return (result);
  192. }
  193. static int
  194. acpi_cpufreq_verify (
  195. struct cpufreq_policy *policy)
  196. {
  197. unsigned int result = 0;
  198. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  199. dprintk("acpi_cpufreq_verify\n");
  200. result = cpufreq_frequency_table_verify(policy,
  201. data->freq_table);
  202. return (result);
  203. }
  204. static int
  205. acpi_cpufreq_cpu_init (
  206. struct cpufreq_policy *policy)
  207. {
  208. unsigned int i;
  209. unsigned int cpu = policy->cpu;
  210. struct cpufreq_acpi_io *data;
  211. unsigned int result = 0;
  212. dprintk("acpi_cpufreq_cpu_init\n");
  213. data = kzalloc(sizeof(struct cpufreq_acpi_io), GFP_KERNEL);
  214. if (!data)
  215. return (-ENOMEM);
  216. acpi_io_data[cpu] = data;
  217. result = acpi_processor_register_performance(&data->acpi_data, cpu);
  218. if (result)
  219. goto err_free;
  220. /* capability check */
  221. if (data->acpi_data.state_count <= 1) {
  222. dprintk("No P-States\n");
  223. result = -ENODEV;
  224. goto err_unreg;
  225. }
  226. if ((data->acpi_data.control_register.space_id !=
  227. ACPI_ADR_SPACE_FIXED_HARDWARE) ||
  228. (data->acpi_data.status_register.space_id !=
  229. ACPI_ADR_SPACE_FIXED_HARDWARE)) {
  230. dprintk("Unsupported address space [%d, %d]\n",
  231. (u32) (data->acpi_data.control_register.space_id),
  232. (u32) (data->acpi_data.status_register.space_id));
  233. result = -ENODEV;
  234. goto err_unreg;
  235. }
  236. /* alloc freq_table */
  237. data->freq_table = kmalloc(sizeof(struct cpufreq_frequency_table) *
  238. (data->acpi_data.state_count + 1),
  239. GFP_KERNEL);
  240. if (!data->freq_table) {
  241. result = -ENOMEM;
  242. goto err_unreg;
  243. }
  244. /* detect transition latency */
  245. policy->cpuinfo.transition_latency = 0;
  246. for (i=0; i<data->acpi_data.state_count; i++) {
  247. if ((data->acpi_data.states[i].transition_latency * 1000) >
  248. policy->cpuinfo.transition_latency) {
  249. policy->cpuinfo.transition_latency =
  250. data->acpi_data.states[i].transition_latency * 1000;
  251. }
  252. }
  253. policy->cur = processor_get_freq(data, policy->cpu);
  254. /* table init */
  255. for (i = 0; i <= data->acpi_data.state_count; i++)
  256. {
  257. data->freq_table[i].index = i;
  258. if (i < data->acpi_data.state_count) {
  259. data->freq_table[i].frequency =
  260. data->acpi_data.states[i].core_frequency * 1000;
  261. } else {
  262. data->freq_table[i].frequency = CPUFREQ_TABLE_END;
  263. }
  264. }
  265. result = cpufreq_frequency_table_cpuinfo(policy, data->freq_table);
  266. if (result) {
  267. goto err_freqfree;
  268. }
  269. /* notify BIOS that we exist */
  270. acpi_processor_notify_smm(THIS_MODULE);
  271. printk(KERN_INFO "acpi-cpufreq: CPU%u - ACPI performance management "
  272. "activated.\n", cpu);
  273. for (i = 0; i < data->acpi_data.state_count; i++)
  274. dprintk(" %cP%d: %d MHz, %d mW, %d uS, %d uS, 0x%x 0x%x\n",
  275. (i == data->acpi_data.state?'*':' '), i,
  276. (u32) data->acpi_data.states[i].core_frequency,
  277. (u32) data->acpi_data.states[i].power,
  278. (u32) data->acpi_data.states[i].transition_latency,
  279. (u32) data->acpi_data.states[i].bus_master_latency,
  280. (u32) data->acpi_data.states[i].status,
  281. (u32) data->acpi_data.states[i].control);
  282. cpufreq_frequency_table_get_attr(data->freq_table, policy->cpu);
  283. /* the first call to ->target() should result in us actually
  284. * writing something to the appropriate registers. */
  285. data->resume = 1;
  286. return (result);
  287. err_freqfree:
  288. kfree(data->freq_table);
  289. err_unreg:
  290. acpi_processor_unregister_performance(&data->acpi_data, cpu);
  291. err_free:
  292. kfree(data);
  293. acpi_io_data[cpu] = NULL;
  294. return (result);
  295. }
  296. static int
  297. acpi_cpufreq_cpu_exit (
  298. struct cpufreq_policy *policy)
  299. {
  300. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  301. dprintk("acpi_cpufreq_cpu_exit\n");
  302. if (data) {
  303. cpufreq_frequency_table_put_attr(policy->cpu);
  304. acpi_io_data[policy->cpu] = NULL;
  305. acpi_processor_unregister_performance(&data->acpi_data,
  306. policy->cpu);
  307. kfree(data);
  308. }
  309. return (0);
  310. }
  311. static struct freq_attr* acpi_cpufreq_attr[] = {
  312. &cpufreq_freq_attr_scaling_available_freqs,
  313. NULL,
  314. };
  315. static struct cpufreq_driver acpi_cpufreq_driver = {
  316. .verify = acpi_cpufreq_verify,
  317. .target = acpi_cpufreq_target,
  318. .get = acpi_cpufreq_get,
  319. .init = acpi_cpufreq_cpu_init,
  320. .exit = acpi_cpufreq_cpu_exit,
  321. .name = "acpi-cpufreq",
  322. .owner = THIS_MODULE,
  323. .attr = acpi_cpufreq_attr,
  324. };
  325. static int __init
  326. acpi_cpufreq_init (void)
  327. {
  328. dprintk("acpi_cpufreq_init\n");
  329. return cpufreq_register_driver(&acpi_cpufreq_driver);
  330. }
  331. static void __exit
  332. acpi_cpufreq_exit (void)
  333. {
  334. dprintk("acpi_cpufreq_exit\n");
  335. cpufreq_unregister_driver(&acpi_cpufreq_driver);
  336. return;
  337. }
  338. late_initcall(acpi_cpufreq_init);
  339. module_exit(acpi_cpufreq_exit);