cppc_cpufreq.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/vmalloc.h>
  21. #include <acpi/cppc_acpi.h>
  22. /*
  23. * These structs contain information parsed from per CPU
  24. * ACPI _CPC structures.
  25. * e.g. For each CPU the highest, lowest supported
  26. * performance capabilities, desired performance level
  27. * requested etc.
  28. */
  29. static struct cppc_cpudata **all_cpu_data;
  30. static int cppc_cpufreq_set_target(struct cpufreq_policy *policy,
  31. unsigned int target_freq,
  32. unsigned int relation)
  33. {
  34. struct cppc_cpudata *cpu;
  35. struct cpufreq_freqs freqs;
  36. int ret = 0;
  37. cpu = all_cpu_data[policy->cpu];
  38. cpu->perf_ctrls.desired_perf = target_freq;
  39. freqs.old = policy->cur;
  40. freqs.new = target_freq;
  41. cpufreq_freq_transition_begin(policy, &freqs);
  42. ret = cppc_set_perf(cpu->cpu, &cpu->perf_ctrls);
  43. cpufreq_freq_transition_end(policy, &freqs, ret != 0);
  44. if (ret)
  45. pr_debug("Failed to set target on CPU:%d. ret:%d\n",
  46. cpu->cpu, ret);
  47. return ret;
  48. }
  49. static int cppc_verify_policy(struct cpufreq_policy *policy)
  50. {
  51. cpufreq_verify_within_cpu_limits(policy);
  52. return 0;
  53. }
  54. static void cppc_cpufreq_stop_cpu(struct cpufreq_policy *policy)
  55. {
  56. int cpu_num = policy->cpu;
  57. struct cppc_cpudata *cpu = all_cpu_data[cpu_num];
  58. int ret;
  59. cpu->perf_ctrls.desired_perf = cpu->perf_caps.lowest_perf;
  60. ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls);
  61. if (ret)
  62. pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
  63. cpu->perf_caps.lowest_perf, cpu_num, ret);
  64. }
  65. static int cppc_cpufreq_cpu_init(struct cpufreq_policy *policy)
  66. {
  67. struct cppc_cpudata *cpu;
  68. unsigned int cpu_num = policy->cpu;
  69. int ret = 0;
  70. cpu = all_cpu_data[policy->cpu];
  71. cpu->cpu = cpu_num;
  72. ret = cppc_get_perf_caps(policy->cpu, &cpu->perf_caps);
  73. if (ret) {
  74. pr_debug("Err reading CPU%d perf capabilities. ret:%d\n",
  75. cpu_num, ret);
  76. return ret;
  77. }
  78. policy->min = cpu->perf_caps.lowest_perf;
  79. policy->max = cpu->perf_caps.highest_perf;
  80. policy->cpuinfo.min_freq = policy->min;
  81. policy->cpuinfo.max_freq = policy->max;
  82. policy->cpuinfo.transition_latency = cppc_get_transition_latency(cpu_num);
  83. policy->shared_type = cpu->shared_type;
  84. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
  85. cpumask_copy(policy->cpus, cpu->shared_cpu_map);
  86. else if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL) {
  87. /* Support only SW_ANY for now. */
  88. pr_debug("Unsupported CPU co-ord type\n");
  89. return -EFAULT;
  90. }
  91. cpumask_set_cpu(policy->cpu, policy->cpus);
  92. cpu->cur_policy = policy;
  93. /* Set policy->cur to max now. The governors will adjust later. */
  94. policy->cur = cpu->perf_ctrls.desired_perf = cpu->perf_caps.highest_perf;
  95. ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls);
  96. if (ret)
  97. pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
  98. cpu->perf_caps.highest_perf, cpu_num, ret);
  99. return ret;
  100. }
  101. static struct cpufreq_driver cppc_cpufreq_driver = {
  102. .flags = CPUFREQ_CONST_LOOPS,
  103. .verify = cppc_verify_policy,
  104. .target = cppc_cpufreq_set_target,
  105. .init = cppc_cpufreq_cpu_init,
  106. .stop_cpu = cppc_cpufreq_stop_cpu,
  107. .name = "cppc_cpufreq",
  108. };
  109. static int __init cppc_cpufreq_init(void)
  110. {
  111. int i, ret = 0;
  112. struct cppc_cpudata *cpu;
  113. if (acpi_disabled)
  114. return -ENODEV;
  115. all_cpu_data = kzalloc(sizeof(void *) * num_possible_cpus(), GFP_KERNEL);
  116. if (!all_cpu_data)
  117. return -ENOMEM;
  118. for_each_possible_cpu(i) {
  119. all_cpu_data[i] = kzalloc(sizeof(struct cppc_cpudata), GFP_KERNEL);
  120. if (!all_cpu_data[i])
  121. goto out;
  122. cpu = all_cpu_data[i];
  123. if (!zalloc_cpumask_var(&cpu->shared_cpu_map, GFP_KERNEL))
  124. goto out;
  125. }
  126. ret = acpi_get_psd_map(all_cpu_data);
  127. if (ret) {
  128. pr_debug("Error parsing PSD data. Aborting cpufreq registration.\n");
  129. goto out;
  130. }
  131. ret = cpufreq_register_driver(&cppc_cpufreq_driver);
  132. if (ret)
  133. goto out;
  134. return ret;
  135. out:
  136. for_each_possible_cpu(i)
  137. kfree(all_cpu_data[i]);
  138. kfree(all_cpu_data);
  139. return -ENODEV;
  140. }
  141. static void __exit cppc_cpufreq_exit(void)
  142. {
  143. struct cppc_cpudata *cpu;
  144. int i;
  145. cpufreq_unregister_driver(&cppc_cpufreq_driver);
  146. for_each_possible_cpu(i) {
  147. cpu = all_cpu_data[i];
  148. free_cpumask_var(cpu->shared_cpu_map);
  149. kfree(cpu);
  150. }
  151. kfree(all_cpu_data);
  152. }
  153. module_exit(cppc_cpufreq_exit);
  154. MODULE_AUTHOR("Ashwin Chaugule");
  155. MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
  156. MODULE_LICENSE("GPL");
  157. late_initcall(cppc_cpufreq_init);