ia64-acpi-cpufreq.c 8.2 KB

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