cppc_cpufreq.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 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 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 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 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->shared_type = cpu->shared_type;
  83. if (policy->shared_type == CPUFREQ_SHARED_TYPE_ANY)
  84. cpumask_copy(policy->cpus, cpu->shared_cpu_map);
  85. else if (policy->shared_type == CPUFREQ_SHARED_TYPE_ALL) {
  86. /* Support only SW_ANY for now. */
  87. pr_debug("Unsupported CPU co-ord type\n");
  88. return -EFAULT;
  89. }
  90. cpumask_set_cpu(policy->cpu, policy->cpus);
  91. cpu->cur_policy = policy;
  92. /* Set policy->cur to max now. The governors will adjust later. */
  93. policy->cur = cpu->perf_ctrls.desired_perf = cpu->perf_caps.highest_perf;
  94. ret = cppc_set_perf(cpu_num, &cpu->perf_ctrls);
  95. if (ret)
  96. pr_debug("Err setting perf value:%d on CPU:%d. ret:%d\n",
  97. cpu->perf_caps.highest_perf, cpu_num, ret);
  98. return ret;
  99. }
  100. static struct cpufreq_driver cppc_cpufreq_driver = {
  101. .flags = CPUFREQ_CONST_LOOPS,
  102. .verify = cppc_verify_policy,
  103. .target = cppc_cpufreq_set_target,
  104. .init = cppc_cpufreq_cpu_init,
  105. .stop_cpu = cppc_cpufreq_stop_cpu,
  106. .name = "cppc_cpufreq",
  107. };
  108. static int __init cppc_cpufreq_init(void)
  109. {
  110. int i, ret = 0;
  111. struct cpudata *cpu;
  112. if (acpi_disabled)
  113. return -ENODEV;
  114. all_cpu_data = kzalloc(sizeof(void *) * num_possible_cpus(), GFP_KERNEL);
  115. if (!all_cpu_data)
  116. return -ENOMEM;
  117. for_each_possible_cpu(i) {
  118. all_cpu_data[i] = kzalloc(sizeof(struct cpudata), GFP_KERNEL);
  119. if (!all_cpu_data[i])
  120. goto out;
  121. cpu = all_cpu_data[i];
  122. if (!zalloc_cpumask_var(&cpu->shared_cpu_map, GFP_KERNEL))
  123. goto out;
  124. }
  125. ret = acpi_get_psd_map(all_cpu_data);
  126. if (ret) {
  127. pr_debug("Error parsing PSD data. Aborting cpufreq registration.\n");
  128. goto out;
  129. }
  130. ret = cpufreq_register_driver(&cppc_cpufreq_driver);
  131. if (ret)
  132. goto out;
  133. return ret;
  134. out:
  135. for_each_possible_cpu(i)
  136. kfree(all_cpu_data[i]);
  137. kfree(all_cpu_data);
  138. return -ENODEV;
  139. }
  140. static void __exit cppc_cpufreq_exit(void)
  141. {
  142. struct cpudata *cpu;
  143. int i;
  144. cpufreq_unregister_driver(&cppc_cpufreq_driver);
  145. for_each_possible_cpu(i) {
  146. cpu = all_cpu_data[i];
  147. free_cpumask_var(cpu->shared_cpu_map);
  148. kfree(cpu);
  149. }
  150. kfree(all_cpu_data);
  151. }
  152. module_exit(cppc_cpufreq_exit);
  153. MODULE_AUTHOR("Ashwin Chaugule");
  154. MODULE_DESCRIPTION("CPUFreq driver based on the ACPI CPPC v5.0+ spec");
  155. MODULE_LICENSE("GPL");
  156. late_initcall(cppc_cpufreq_init);