cppc_cpufreq.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 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 cpudata *cpu;
  67. struct cpufreq_freqs freqs;
  68. int ret = 0;
  69. cpu = all_cpu_data[policy->cpu];
  70. cpu->perf_ctrls.desired_perf = (u64)target_freq * policy->max / cppc_dmi_max_khz;
  71. freqs.old = policy->cur;
  72. freqs.new = target_freq;
  73. cpufreq_freq_transition_begin(policy, &freqs);
  74. ret = cppc_set_perf(cpu->cpu, &cpu->perf_ctrls);
  75. cpufreq_freq_transition_end(policy, &freqs, ret != 0);
  76. if (ret)
  77. pr_debug("Failed to set target on CPU:%d. ret:%d\n",
  78. cpu->cpu, ret);
  79. return ret;
  80. }
  81. static int cppc_verify_policy(struct cpufreq_policy *policy)
  82. {
  83. cpufreq_verify_within_cpu_limits(policy);
  84. return 0;
  85. }
  86. static void cppc_cpufreq_stop_cpu(struct cpufreq_policy *policy)
  87. {
  88. int cpu_num = policy->cpu;
  89. struct cpudata *cpu = all_cpu_data[cpu_num];
  90. int ret;
  91. cpu->perf_ctrls.desired_perf = cpu->perf_caps.lowest_perf;
  92. ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls);
  93. if (ret)
  94. pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
  95. cpu->perf_caps.lowest_perf, cpu_num, ret);
  96. }
  97. static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
  98. {
  99. struct cpudata *cpu;
  100. unsigned int cpu_num = policy->cpu;
  101. int ret = 0;
  102. cpu = all_cpu_data[policy->cpu];
  103. cpu->cpu = cpu_num;
  104. ret = cppc_get_perf_caps(policy->cpu, &cpu->perf_caps);
  105. if (ret) {
  106. pr_debug("Err reading CPU%d perf capabilities. ret:%d\n",
  107. cpu_num, ret);
  108. return ret;
  109. }
  110. cppc_dmi_max_khz = cppc_get_dmi_max_khz();
  111. policy->min = cpu->perf_caps.lowest_perf * cppc_dmi_max_khz / cpu->perf_caps.highest_perf;
  112. policy->max = cppc_dmi_max_khz;
  113. policy->cpuinfo.min_freq = policy->min;
  114. policy->cpuinfo.max_freq = policy->max;
  115. policy->shared_type = cpu->shared_type;
  116. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
  117. cpumask_copy(policy->cpus, cpu->shared_cpu_map);
  118. else if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL) {
  119. /* Support only SW_ANY for now. */
  120. pr_debug("Unsupported CPU co-ord type\n");
  121. return -EFAULT;
  122. }
  123. cpumask_set_cpu(policy->cpu, policy->cpus);
  124. cpu->cur_policy = policy;
  125. /* Set policy->cur to max now. The governors will adjust later. */
  126. policy->cur = cppc_dmi_max_khz;
  127. cpu->perf_ctrls.desired_perf = cpu->perf_caps.highest_perf;
  128. ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls);
  129. if (ret)
  130. pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
  131. cpu->perf_caps.highest_perf, cpu_num, ret);
  132. return ret;
  133. }
  134. static struct cpufreq_driver cppc_cpufreq_driver = {
  135. .flags = CPUFREQ_CONST_LOOPS,
  136. .verify = cppc_verify_policy,
  137. .target = cppc_cpufreq_set_target,
  138. .init = cppc_cpufreq_cpu_init,
  139. .stop_cpu = cppc_cpufreq_stop_cpu,
  140. .name = "cppc_cpufreq",
  141. };
  142. static int __init cppc_cpufreq_init(void)
  143. {
  144. int i, ret = 0;
  145. struct cpudata *cpu;
  146. if (acpi_disabled)
  147. return -ENODEV;
  148. all_cpu_data = kzalloc(sizeof(void *) * num_possible_cpus(), GFP_KERNEL);
  149. if (!all_cpu_data)
  150. return -ENOMEM;
  151. for_each_possible_cpu(i) {
  152. all_cpu_data[i] = kzalloc(sizeof(struct cpudata), GFP_KERNEL);
  153. if (!all_cpu_data[i])
  154. goto out;
  155. cpu = all_cpu_data[i];
  156. if (!zalloc_cpumask_var(&cpu->shared_cpu_map, GFP_KERNEL))
  157. goto out;
  158. }
  159. ret = acpi_get_psd_map(all_cpu_data);
  160. if (ret) {
  161. pr_debug("Error parsing PSD data. Aborting cpufreq registration.\n");
  162. goto out;
  163. }
  164. ret = cpufreq_register_driver(&cppc_cpufreq_driver);
  165. if (ret)
  166. goto out;
  167. return ret;
  168. out:
  169. for_each_possible_cpu(i)
  170. kfree(all_cpu_data[i]);
  171. kfree(all_cpu_data);
  172. return -ENODEV;
  173. }
  174. static void __exit cppc_cpufreq_exit(void)
  175. {
  176. struct cpudata *cpu;
  177. int i;
  178. cpufreq_unregister_driver(&cppc_cpufreq_driver);
  179. for_each_possible_cpu(i) {
  180. cpu = all_cpu_data[i];
  181. free_cpumask_var(cpu->shared_cpu_map);
  182. kfree(cpu);
  183. }
  184. kfree(all_cpu_data);
  185. }
  186. module_exit(cppc_cpufreq_exit);
  187. MODULE_AUTHOR("Ashwin Chaugule");
  188. MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
  189. MODULE_LICENSE("GPL");
  190. late_initcall(cppc_cpufreq_init);