cppc_cpufreq.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * CPPC (Collaborative Processor Performance Control) driver for
  3. * interfacing with the CPUfreq layer and governors. See
  4. * cppc_acpi.c for CPPC specific methods.
  5. *
  6. * (C) Copyright 2014, 2015 Linaro Ltd.
  7. * Author: Ashwin Chaugule <ashwin.chaugule@linaro.org>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; version 2
  12. * of the License.
  13. */
  14. #define pr_fmt(fmt) "CPPC Cpufreq:" fmt
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/delay.h>
  18. #include <linux/cpu.h>
  19. #include <linux/cpufreq.h>
  20. #include <linux/dmi.h>
  21. #include <linux/vmalloc.h>
  22. #include <asm/unaligned.h>
  23. #include <acpi/cppc_acpi.h>
  24. /* Minimum struct length needed for the DMI processor entry we want */
  25. #define DMI_ENTRY_PROCESSOR_MIN_LENGTH 48
  26. /* Offest in the DMI processor structure for the max frequency */
  27. #define DMI_PROCESSOR_MAX_SPEED 0x14
  28. /*
  29. * These structs contain information parsed from per CPU
  30. * ACPI _CPC structures.
  31. * e.g. For each CPU the highest, lowest supported
  32. * performance capabilities, desired performance level
  33. * requested etc.
  34. */
  35. static struct cppc_cpudata **all_cpu_data;
  36. /* Capture the max KHz from DMI */
  37. static u64 cppc_dmi_max_khz;
  38. /* Callback function used to retrieve the max frequency from DMI */
  39. static void cppc_find_dmi_mhz(const struct dmi_header *dm, void *private)
  40. {
  41. const u8 *dmi_data = (const u8 *)dm;
  42. u16 *mhz = (u16 *)private;
  43. if (dm->type == DMI_ENTRY_PROCESSOR &&
  44. dm->length >= DMI_ENTRY_PROCESSOR_MIN_LENGTH) {
  45. u16 val = (u16)get_unaligned((const u16 *)
  46. (dmi_data + DMI_PROCESSOR_MAX_SPEED));
  47. *mhz = val > *mhz ? val : *mhz;
  48. }
  49. }
  50. /* Look up the max frequency in DMI */
  51. static u64 cppc_get_dmi_max_khz(void)
  52. {
  53. u16 mhz = 0;
  54. dmi_walk(cppc_find_dmi_mhz, &mhz);
  55. /*
  56. * Real stupid fallback value, just in case there is no
  57. * actual value set.
  58. */
  59. mhz = mhz ? mhz : 1;
  60. return (1000 * mhz);
  61. }
  62. static int cppc_cpufreq_set_target(struct cpufreq_policy *policy,
  63. unsigned int target_freq,
  64. unsigned int relation)
  65. {
  66. struct cppc_cpudata *cpu;
  67. struct cpufreq_freqs freqs;
  68. u32 desired_perf;
  69. int ret = 0;
  70. cpu = all_cpu_data[policy->cpu];
  71. desired_perf = (u64)target_freq * cpu->perf_caps.highest_perf / cppc_dmi_max_khz;
  72. /* Return if it is exactly the same perf */
  73. if (desired_perf == cpu->perf_ctrls.desired_perf)
  74. return ret;
  75. cpu->perf_ctrls.desired_perf = desired_perf;
  76. freqs.old = policy->cur;
  77. freqs.new = target_freq;
  78. cpufreq_freq_transition_begin(policy, &freqs);
  79. ret = cppc_set_perf(cpu->cpu, &cpu->perf_ctrls);
  80. cpufreq_freq_transition_end(policy, &freqs, ret != 0);
  81. if (ret)
  82. pr_debug("Failed to set target on CPU:%d. ret:%d\n",
  83. cpu->cpu, ret);
  84. return ret;
  85. }
  86. static int cppc_verify_policy(struct cpufreq_policy *policy)
  87. {
  88. cpufreq_verify_within_cpu_limits(policy);
  89. return 0;
  90. }
  91. static void cppc_cpufreq_stop_cpu(struct cpufreq_policy *policy)
  92. {
  93. int cpu_num = policy->cpu;
  94. struct cppc_cpudata *cpu = all_cpu_data[cpu_num];
  95. int ret;
  96. cpu->perf_ctrls.desired_perf = cpu->perf_caps.lowest_perf;
  97. ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls);
  98. if (ret)
  99. pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
  100. cpu->perf_caps.lowest_perf, cpu_num, ret);
  101. }
  102. static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
  103. {
  104. struct cppc_cpudata *cpu;
  105. unsigned int cpu_num = policy->cpu;
  106. int ret = 0;
  107. cpu = all_cpu_data[policy->cpu];
  108. cpu->cpu = cpu_num;
  109. ret = cppc_get_perf_caps(policy->cpu, &cpu->perf_caps);
  110. if (ret) {
  111. pr_debug("Err reading CPU%d perf capabilities. ret:%d\n",
  112. cpu_num, ret);
  113. return ret;
  114. }
  115. cppc_dmi_max_khz = cppc_get_dmi_max_khz();
  116. policy->min = cpu->perf_caps.lowest_perf * cppc_dmi_max_khz / cpu->perf_caps.highest_perf;
  117. policy->max = cppc_dmi_max_khz;
  118. policy->cpuinfo.min_freq = policy->min;
  119. policy->cpuinfo.max_freq = policy->max;
  120. policy->cpuinfo.transition_latency = cppc_get_transition_latency(cpu_num);
  121. policy->shared_type = cpu->shared_type;
  122. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
  123. cpumask_copy(policy->cpus, cpu->shared_cpu_map);
  124. else if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL) {
  125. /* Support only SW_ANY for now. */
  126. pr_debug("Unsupported CPU co-ord type\n");
  127. return -EFAULT;
  128. }
  129. cpumask_set_cpu(policy->cpu, policy->cpus);
  130. cpu->cur_policy = policy;
  131. /* Set policy->cur to max now. The governors will adjust later. */
  132. policy->cur = cppc_dmi_max_khz;
  133. cpu->perf_ctrls.desired_perf = cpu->perf_caps.highest_perf;
  134. ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls);
  135. if (ret)
  136. pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
  137. cpu->perf_caps.highest_perf, cpu_num, ret);
  138. return ret;
  139. }
  140. static struct cpufreq_driver cppc_cpufreq_driver = {
  141. .flags = CPUFREQ_CONST_LOOPS,
  142. .verify = cppc_verify_policy,
  143. .target = cppc_cpufreq_set_target,
  144. .init = cppc_cpufreq_cpu_init,
  145. .stop_cpu = cppc_cpufreq_stop_cpu,
  146. .name = "cppc_cpufreq",
  147. };
  148. static int __init cppc_cpufreq_init(void)
  149. {
  150. int i, ret = 0;
  151. struct cppc_cpudata *cpu;
  152. if (acpi_disabled)
  153. return -ENODEV;
  154. all_cpu_data = kzalloc(sizeof(void *) * num_possible_cpus(), GFP_KERNEL);
  155. if (!all_cpu_data)
  156. return -ENOMEM;
  157. for_each_possible_cpu(i) {
  158. all_cpu_data[i] = kzalloc(sizeof(struct cppc_cpudata), GFP_KERNEL);
  159. if (!all_cpu_data[i])
  160. goto out;
  161. cpu = all_cpu_data[i];
  162. if (!zalloc_cpumask_var(&cpu->shared_cpu_map, GFP_KERNEL))
  163. goto out;
  164. }
  165. ret = acpi_get_psd_map(all_cpu_data);
  166. if (ret) {
  167. pr_debug("Error parsing PSD data. Aborting cpufreq registration.\n");
  168. goto out;
  169. }
  170. ret = cpufreq_register_driver(&cppc_cpufreq_driver);
  171. if (ret)
  172. goto out;
  173. return ret;
  174. out:
  175. for_each_possible_cpu(i)
  176. kfree(all_cpu_data[i]);
  177. kfree(all_cpu_data);
  178. return -ENODEV;
  179. }
  180. static void __exit cppc_cpufreq_exit(void)
  181. {
  182. struct cppc_cpudata *cpu;
  183. int i;
  184. cpufreq_unregister_driver(&cppc_cpufreq_driver);
  185. for_each_possible_cpu(i) {
  186. cpu = all_cpu_data[i];
  187. free_cpumask_var(cpu->shared_cpu_map);
  188. kfree(cpu);
  189. }
  190. kfree(all_cpu_data);
  191. }
  192. module_exit(cppc_cpufreq_exit);
  193. MODULE_AUTHOR("Ashwin Chaugule");
  194. MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
  195. MODULE_LICENSE("GPL");
  196. late_initcall(cppc_cpufreq_init);