cppc_cpufreq.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. int i;
  124. cpumask_copy(policy->cpus, cpu->shared_cpu_map);
  125. for_each_cpu(i, policy->cpus) {
  126. if (unlikely(i == policy->cpu))
  127. continue;
  128. memcpy(&all_cpu_data[i]->perf_caps, &cpu->perf_caps,
  129. sizeof(cpu->perf_caps));
  130. }
  131. } else if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL) {
  132. /* Support only SW_ANY for now. */
  133. pr_debug("Unsupported CPU co-ord type\n");
  134. return -EFAULT;
  135. }
  136. cpumask_set_cpu(policy->cpu, policy->cpus);
  137. cpu->cur_policy = policy;
  138. /* Set policy->cur to max now. The governors will adjust later. */
  139. policy->cur = cppc_dmi_max_khz;
  140. cpu->perf_ctrls.desired_perf = cpu->perf_caps.highest_perf;
  141. ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls);
  142. if (ret)
  143. pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
  144. cpu->perf_caps.highest_perf, cpu_num, ret);
  145. return ret;
  146. }
  147. static struct cpufreq_driver cppc_cpufreq_driver = {
  148. .flags = CPUFREQ_CONST_LOOPS,
  149. .verify = cppc_verify_policy,
  150. .target = cppc_cpufreq_set_target,
  151. .init = cppc_cpufreq_cpu_init,
  152. .stop_cpu = cppc_cpufreq_stop_cpu,
  153. .name = "cppc_cpufreq",
  154. };
  155. static int __init cppc_cpufreq_init(void)
  156. {
  157. int i, ret = 0;
  158. struct cppc_cpudata *cpu;
  159. if (acpi_disabled)
  160. return -ENODEV;
  161. all_cpu_data = kzalloc(sizeof(void *) * num_possible_cpus(), GFP_KERNEL);
  162. if (!all_cpu_data)
  163. return -ENOMEM;
  164. for_each_possible_cpu(i) {
  165. all_cpu_data[i] = kzalloc(sizeof(struct cppc_cpudata), GFP_KERNEL);
  166. if (!all_cpu_data[i])
  167. goto out;
  168. cpu = all_cpu_data[i];
  169. if (!zalloc_cpumask_var(&cpu->shared_cpu_map, GFP_KERNEL))
  170. goto out;
  171. }
  172. ret = acpi_get_psd_map(all_cpu_data);
  173. if (ret) {
  174. pr_debug("Error parsing PSD data. Aborting cpufreq registration.\n");
  175. goto out;
  176. }
  177. ret = cpufreq_register_driver(&cppc_cpufreq_driver);
  178. if (ret)
  179. goto out;
  180. return ret;
  181. out:
  182. for_each_possible_cpu(i) {
  183. cpu = all_cpu_data[i];
  184. if (!cpu)
  185. break;
  186. free_cpumask_var(cpu->shared_cpu_map);
  187. kfree(cpu);
  188. }
  189. kfree(all_cpu_data);
  190. return -ENODEV;
  191. }
  192. static void __exit cppc_cpufreq_exit(void)
  193. {
  194. struct cppc_cpudata *cpu;
  195. int i;
  196. cpufreq_unregister_driver(&cppc_cpufreq_driver);
  197. for_each_possible_cpu(i) {
  198. cpu = all_cpu_data[i];
  199. free_cpumask_var(cpu->shared_cpu_map);
  200. kfree(cpu);
  201. }
  202. kfree(all_cpu_data);
  203. }
  204. module_exit(cppc_cpufreq_exit);
  205. MODULE_AUTHOR("Ashwin Chaugule");
  206. MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
  207. MODULE_LICENSE("GPL");
  208. late_initcall(cppc_cpufreq_init);