arm_big_little.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * ARM big.LITTLE Platforms CPUFreq support
  3. *
  4. * Copyright (C) 2013 ARM Ltd.
  5. * Sudeep KarkadaNagesha <sudeep.karkadanagesha@arm.com>
  6. *
  7. * Copyright (C) 2013 Linaro.
  8. * Viresh Kumar <viresh.kumar@linaro.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  15. * kind, whether express or implied; without even the implied warranty
  16. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/clk.h>
  21. #include <linux/cpu.h>
  22. #include <linux/cpufreq.h>
  23. #include <linux/cpumask.h>
  24. #include <linux/export.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/opp.h>
  27. #include <linux/slab.h>
  28. #include <linux/topology.h>
  29. #include <linux/types.h>
  30. #include "arm_big_little.h"
  31. /* Currently we support only two clusters */
  32. #define MAX_CLUSTERS 2
  33. static struct cpufreq_arm_bL_ops *arm_bL_ops;
  34. static struct clk *clk[MAX_CLUSTERS];
  35. static struct cpufreq_frequency_table *freq_table[MAX_CLUSTERS];
  36. static atomic_t cluster_usage[MAX_CLUSTERS] = {ATOMIC_INIT(0), ATOMIC_INIT(0)};
  37. static unsigned int bL_cpufreq_get(unsigned int cpu)
  38. {
  39. u32 cur_cluster = cpu_to_cluster(cpu);
  40. return clk_get_rate(clk[cur_cluster]) / 1000;
  41. }
  42. /* Validate policy frequency range */
  43. static int bL_cpufreq_verify_policy(struct cpufreq_policy *policy)
  44. {
  45. u32 cur_cluster = cpu_to_cluster(policy->cpu);
  46. return cpufreq_frequency_table_verify(policy, freq_table[cur_cluster]);
  47. }
  48. /* Set clock frequency */
  49. static int bL_cpufreq_set_target(struct cpufreq_policy *policy,
  50. unsigned int target_freq, unsigned int relation)
  51. {
  52. struct cpufreq_freqs freqs;
  53. u32 cpu = policy->cpu, freq_tab_idx, cur_cluster;
  54. int ret = 0;
  55. cur_cluster = cpu_to_cluster(policy->cpu);
  56. freqs.old = bL_cpufreq_get(policy->cpu);
  57. /* Determine valid target frequency using freq_table */
  58. cpufreq_frequency_table_target(policy, freq_table[cur_cluster],
  59. target_freq, relation, &freq_tab_idx);
  60. freqs.new = freq_table[cur_cluster][freq_tab_idx].frequency;
  61. pr_debug("%s: cpu: %d, cluster: %d, oldfreq: %d, target freq: %d, new freq: %d\n",
  62. __func__, cpu, cur_cluster, freqs.old, target_freq,
  63. freqs.new);
  64. if (freqs.old == freqs.new)
  65. return 0;
  66. cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
  67. ret = clk_set_rate(clk[cur_cluster], freqs.new * 1000);
  68. if (ret) {
  69. pr_err("clk_set_rate failed: %d\n", ret);
  70. return ret;
  71. }
  72. policy->cur = freqs.new;
  73. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  74. return ret;
  75. }
  76. static void put_cluster_clk_and_freq_table(struct device *cpu_dev)
  77. {
  78. u32 cluster = cpu_to_cluster(cpu_dev->id);
  79. if (!atomic_dec_return(&cluster_usage[cluster])) {
  80. clk_put(clk[cluster]);
  81. opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
  82. dev_dbg(cpu_dev, "%s: cluster: %d\n", __func__, cluster);
  83. }
  84. }
  85. static int get_cluster_clk_and_freq_table(struct device *cpu_dev)
  86. {
  87. u32 cluster = cpu_to_cluster(cpu_dev->id);
  88. char name[14] = "cpu-cluster.";
  89. int ret;
  90. if (atomic_inc_return(&cluster_usage[cluster]) != 1)
  91. return 0;
  92. ret = arm_bL_ops->init_opp_table(cpu_dev);
  93. if (ret) {
  94. dev_err(cpu_dev, "%s: init_opp_table failed, cpu: %d, err: %d\n",
  95. __func__, cpu_dev->id, ret);
  96. goto atomic_dec;
  97. }
  98. ret = opp_init_cpufreq_table(cpu_dev, &freq_table[cluster]);
  99. if (ret) {
  100. dev_err(cpu_dev, "%s: failed to init cpufreq table, cpu: %d, err: %d\n",
  101. __func__, cpu_dev->id, ret);
  102. goto atomic_dec;
  103. }
  104. name[12] = cluster + '0';
  105. clk[cluster] = clk_get_sys(name, NULL);
  106. if (!IS_ERR(clk[cluster])) {
  107. dev_dbg(cpu_dev, "%s: clk: %p & freq table: %p, cluster: %d\n",
  108. __func__, clk[cluster], freq_table[cluster],
  109. cluster);
  110. return 0;
  111. }
  112. dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d, cluster: %d\n",
  113. __func__, cpu_dev->id, cluster);
  114. ret = PTR_ERR(clk[cluster]);
  115. opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
  116. atomic_dec:
  117. atomic_dec(&cluster_usage[cluster]);
  118. dev_err(cpu_dev, "%s: Failed to get data for cluster: %d\n", __func__,
  119. cluster);
  120. return ret;
  121. }
  122. /* Per-CPU initialization */
  123. static int bL_cpufreq_init(struct cpufreq_policy *policy)
  124. {
  125. u32 cur_cluster = cpu_to_cluster(policy->cpu);
  126. struct device *cpu_dev;
  127. int ret;
  128. cpu_dev = get_cpu_device(policy->cpu);
  129. if (!cpu_dev) {
  130. pr_err("%s: failed to get cpu%d device\n", __func__,
  131. policy->cpu);
  132. return -ENODEV;
  133. }
  134. ret = get_cluster_clk_and_freq_table(cpu_dev);
  135. if (ret)
  136. return ret;
  137. ret = cpufreq_frequency_table_cpuinfo(policy, freq_table[cur_cluster]);
  138. if (ret) {
  139. dev_err(cpu_dev, "CPU %d, cluster: %d invalid freq table\n",
  140. policy->cpu, cur_cluster);
  141. put_cluster_clk_and_freq_table(cpu_dev);
  142. return ret;
  143. }
  144. cpufreq_frequency_table_get_attr(freq_table[cur_cluster], policy->cpu);
  145. if (arm_bL_ops->get_transition_latency)
  146. policy->cpuinfo.transition_latency =
  147. arm_bL_ops->get_transition_latency(cpu_dev);
  148. else
  149. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  150. policy->cur = bL_cpufreq_get(policy->cpu);
  151. cpumask_copy(policy->cpus, topology_core_cpumask(policy->cpu));
  152. dev_info(cpu_dev, "%s: CPU %d initialized\n", __func__, policy->cpu);
  153. return 0;
  154. }
  155. static int bL_cpufreq_exit(struct cpufreq_policy *policy)
  156. {
  157. struct device *cpu_dev;
  158. cpu_dev = get_cpu_device(policy->cpu);
  159. if (!cpu_dev) {
  160. pr_err("%s: failed to get cpu%d device\n", __func__,
  161. policy->cpu);
  162. return -ENODEV;
  163. }
  164. put_cluster_clk_and_freq_table(cpu_dev);
  165. dev_dbg(cpu_dev, "%s: Exited, cpu: %d\n", __func__, policy->cpu);
  166. return 0;
  167. }
  168. /* Export freq_table to sysfs */
  169. static struct freq_attr *bL_cpufreq_attr[] = {
  170. &cpufreq_freq_attr_scaling_available_freqs,
  171. NULL,
  172. };
  173. static struct cpufreq_driver bL_cpufreq_driver = {
  174. .name = "arm-big-little",
  175. .flags = CPUFREQ_STICKY,
  176. .verify = bL_cpufreq_verify_policy,
  177. .target = bL_cpufreq_set_target,
  178. .get = bL_cpufreq_get,
  179. .init = bL_cpufreq_init,
  180. .exit = bL_cpufreq_exit,
  181. .have_governor_per_policy = true,
  182. .attr = bL_cpufreq_attr,
  183. };
  184. int bL_cpufreq_register(struct cpufreq_arm_bL_ops *ops)
  185. {
  186. int ret;
  187. if (arm_bL_ops) {
  188. pr_debug("%s: Already registered: %s, exiting\n", __func__,
  189. arm_bL_ops->name);
  190. return -EBUSY;
  191. }
  192. if (!ops || !strlen(ops->name) || !ops->init_opp_table) {
  193. pr_err("%s: Invalid arm_bL_ops, exiting\n", __func__);
  194. return -ENODEV;
  195. }
  196. arm_bL_ops = ops;
  197. ret = cpufreq_register_driver(&bL_cpufreq_driver);
  198. if (ret) {
  199. pr_info("%s: Failed registering platform driver: %s, err: %d\n",
  200. __func__, ops->name, ret);
  201. arm_bL_ops = NULL;
  202. } else {
  203. pr_info("%s: Registered platform driver: %s\n", __func__,
  204. ops->name);
  205. }
  206. return ret;
  207. }
  208. EXPORT_SYMBOL_GPL(bL_cpufreq_register);
  209. void bL_cpufreq_unregister(struct cpufreq_arm_bL_ops *ops)
  210. {
  211. if (arm_bL_ops != ops) {
  212. pr_err("%s: Registered with: %s, can't unregister, exiting\n",
  213. __func__, arm_bL_ops->name);
  214. return;
  215. }
  216. cpufreq_unregister_driver(&bL_cpufreq_driver);
  217. pr_info("%s: Un-registered platform driver: %s\n", __func__,
  218. arm_bL_ops->name);
  219. arm_bL_ops = NULL;
  220. }
  221. EXPORT_SYMBOL_GPL(bL_cpufreq_unregister);