cppc_cpufreq.c 4.5 KB

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