ia64-acpi-cpufreq.c 8.1 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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. MODULE_AUTHOR("Venkatesh Pallipadi");
  24. MODULE_DESCRIPTION("ACPI Processor P-States Driver");
  25. MODULE_LICENSE("GPL");
  26. struct cpufreq_acpi_io {
  27. struct acpi_processor_performance acpi_data;
  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. pr_warn("get performance failed with error %d\n", ret);
  95. ret = 0;
  96. goto migrate_end;
  97. }
  98. clock_freq = extract_clock(data, value, cpu);
  99. ret = (clock_freq*1000);
  100. migrate_end:
  101. set_cpus_allowed_ptr(current, &saved_mask);
  102. return ret;
  103. }
  104. static int
  105. processor_set_freq (
  106. struct cpufreq_acpi_io *data,
  107. struct cpufreq_policy *policy,
  108. int state)
  109. {
  110. int ret = 0;
  111. u32 value = 0;
  112. cpumask_t saved_mask;
  113. int retval;
  114. pr_debug("processor_set_freq\n");
  115. saved_mask = current->cpus_allowed;
  116. set_cpus_allowed_ptr(current, cpumask_of(policy->cpu));
  117. if (smp_processor_id() != policy->cpu) {
  118. retval = -EAGAIN;
  119. goto migrate_end;
  120. }
  121. if (state == data->acpi_data.state) {
  122. if (unlikely(data->resume)) {
  123. pr_debug("Called after resume, resetting to P%d\n", state);
  124. data->resume = 0;
  125. } else {
  126. pr_debug("Already at target state (P%d)\n", state);
  127. retval = 0;
  128. goto migrate_end;
  129. }
  130. }
  131. pr_debug("Transitioning from P%d to P%d\n",
  132. data->acpi_data.state, state);
  133. /*
  134. * First we write the target state's 'control' value to the
  135. * control_register.
  136. */
  137. value = (u32) data->acpi_data.states[state].control;
  138. pr_debug("Transitioning to state: 0x%08x\n", value);
  139. ret = processor_set_pstate(value);
  140. if (ret) {
  141. pr_warn("Transition failed with error %d\n", ret);
  142. retval = -ENODEV;
  143. goto migrate_end;
  144. }
  145. data->acpi_data.state = state;
  146. retval = 0;
  147. migrate_end:
  148. set_cpus_allowed_ptr(current, &saved_mask);
  149. return (retval);
  150. }
  151. static unsigned int
  152. acpi_cpufreq_get (
  153. unsigned int cpu)
  154. {
  155. struct cpufreq_acpi_io *data = acpi_io_data[cpu];
  156. pr_debug("acpi_cpufreq_get\n");
  157. return processor_get_freq(data, cpu);
  158. }
  159. static int
  160. acpi_cpufreq_target (
  161. struct cpufreq_policy *policy,
  162. unsigned int index)
  163. {
  164. return processor_set_freq(acpi_io_data[policy->cpu], policy, index);
  165. }
  166. static int
  167. acpi_cpufreq_cpu_init (
  168. struct cpufreq_policy *policy)
  169. {
  170. unsigned int i;
  171. unsigned int cpu = policy->cpu;
  172. struct cpufreq_acpi_io *data;
  173. unsigned int result = 0;
  174. struct cpufreq_frequency_table *freq_table;
  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. freq_table = kzalloc(sizeof(*freq_table) *
  201. (data->acpi_data.state_count + 1),
  202. GFP_KERNEL);
  203. if (!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. freq_table[i].frequency =
  221. data->acpi_data.states[i].core_frequency * 1000;
  222. } else {
  223. freq_table[i].frequency = CPUFREQ_TABLE_END;
  224. }
  225. }
  226. result = cpufreq_table_validate_and_show(policy, freq_table);
  227. if (result) {
  228. goto err_freqfree;
  229. }
  230. /* notify BIOS that we exist */
  231. acpi_processor_notify_smm(THIS_MODULE);
  232. pr_info("CPU%u - ACPI performance management activated\n", cpu);
  233. for (i = 0; i < data->acpi_data.state_count; i++)
  234. pr_debug(" %cP%d: %d MHz, %d mW, %d uS, %d uS, 0x%x 0x%x\n",
  235. (i == data->acpi_data.state?'*':' '), i,
  236. (u32) data->acpi_data.states[i].core_frequency,
  237. (u32) data->acpi_data.states[i].power,
  238. (u32) data->acpi_data.states[i].transition_latency,
  239. (u32) data->acpi_data.states[i].bus_master_latency,
  240. (u32) data->acpi_data.states[i].status,
  241. (u32) data->acpi_data.states[i].control);
  242. /* the first call to ->target() should result in us actually
  243. * writing something to the appropriate registers. */
  244. data->resume = 1;
  245. return (result);
  246. err_freqfree:
  247. kfree(freq_table);
  248. err_unreg:
  249. acpi_processor_unregister_performance(cpu);
  250. err_free:
  251. kfree(data);
  252. acpi_io_data[cpu] = NULL;
  253. return (result);
  254. }
  255. static int
  256. acpi_cpufreq_cpu_exit (
  257. struct cpufreq_policy *policy)
  258. {
  259. struct cpufreq_acpi_io *data = acpi_io_data[policy->cpu];
  260. pr_debug("acpi_cpufreq_cpu_exit\n");
  261. if (data) {
  262. acpi_io_data[policy->cpu] = NULL;
  263. acpi_processor_unregister_performance(policy->cpu);
  264. kfree(policy->freq_table);
  265. kfree(data);
  266. }
  267. return (0);
  268. }
  269. static struct cpufreq_driver acpi_cpufreq_driver = {
  270. .verify = cpufreq_generic_frequency_table_verify,
  271. .target_index = acpi_cpufreq_target,
  272. .get = acpi_cpufreq_get,
  273. .init = acpi_cpufreq_cpu_init,
  274. .exit = acpi_cpufreq_cpu_exit,
  275. .name = "acpi-cpufreq",
  276. .attr = cpufreq_generic_attr,
  277. };
  278. static int __init
  279. acpi_cpufreq_init (void)
  280. {
  281. pr_debug("acpi_cpufreq_init\n");
  282. return cpufreq_register_driver(&acpi_cpufreq_driver);
  283. }
  284. static void __exit
  285. acpi_cpufreq_exit (void)
  286. {
  287. pr_debug("acpi_cpufreq_exit\n");
  288. cpufreq_unregister_driver(&acpi_cpufreq_driver);
  289. return;
  290. }
  291. late_initcall(acpi_cpufreq_init);
  292. module_exit(acpi_cpufreq_exit);