arm_big_little.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. freqs.new = freqs.old;
  71. }
  72. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  73. return ret;
  74. }
  75. static void put_cluster_clk_and_freq_table(struct device *cpu_dev)
  76. {
  77. u32 cluster = cpu_to_cluster(cpu_dev->id);
  78. if (!atomic_dec_return(&cluster_usage[cluster])) {
  79. clk_put(clk[cluster]);
  80. opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
  81. dev_dbg(cpu_dev, "%s: cluster: %d\n", __func__, cluster);
  82. }
  83. }
  84. static int get_cluster_clk_and_freq_table(struct device *cpu_dev)
  85. {
  86. u32 cluster = cpu_to_cluster(cpu_dev->id);
  87. char name[14] = "cpu-cluster.";
  88. int ret;
  89. if (atomic_inc_return(&cluster_usage[cluster]) != 1)
  90. return 0;
  91. ret = arm_bL_ops->init_opp_table(cpu_dev);
  92. if (ret) {
  93. dev_err(cpu_dev, "%s: init_opp_table failed, cpu: %d, err: %d\n",
  94. __func__, cpu_dev->id, ret);
  95. goto atomic_dec;
  96. }
  97. ret = opp_init_cpufreq_table(cpu_dev, &freq_table[cluster]);
  98. if (ret) {
  99. dev_err(cpu_dev, "%s: failed to init cpufreq table, cpu: %d, err: %d\n",
  100. __func__, cpu_dev->id, ret);
  101. goto atomic_dec;
  102. }
  103. name[12] = cluster + '0';
  104. clk[cluster] = clk_get_sys(name, NULL);
  105. if (!IS_ERR(clk[cluster])) {
  106. dev_dbg(cpu_dev, "%s: clk: %p & freq table: %p, cluster: %d\n",
  107. __func__, clk[cluster], freq_table[cluster],
  108. cluster);
  109. return 0;
  110. }
  111. dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d, cluster: %d\n",
  112. __func__, cpu_dev->id, cluster);
  113. ret = PTR_ERR(clk[cluster]);
  114. opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
  115. atomic_dec:
  116. atomic_dec(&cluster_usage[cluster]);
  117. dev_err(cpu_dev, "%s: Failed to get data for cluster: %d\n", __func__,
  118. cluster);
  119. return ret;
  120. }
  121. /* Per-CPU initialization */
  122. static int bL_cpufreq_init(struct cpufreq_policy *policy)
  123. {
  124. u32 cur_cluster = cpu_to_cluster(policy->cpu);
  125. struct device *cpu_dev;
  126. int ret;
  127. cpu_dev = get_cpu_device(policy->cpu);
  128. if (!cpu_dev) {
  129. pr_err("%s: failed to get cpu%d device\n", __func__,
  130. policy->cpu);
  131. return -ENODEV;
  132. }
  133. ret = get_cluster_clk_and_freq_table(cpu_dev);
  134. if (ret)
  135. return ret;
  136. ret = cpufreq_frequency_table_cpuinfo(policy, freq_table[cur_cluster]);
  137. if (ret) {
  138. dev_err(cpu_dev, "CPU %d, cluster: %d invalid freq table\n",
  139. policy->cpu, cur_cluster);
  140. put_cluster_clk_and_freq_table(cpu_dev);
  141. return ret;
  142. }
  143. cpufreq_frequency_table_get_attr(freq_table[cur_cluster], policy->cpu);
  144. if (arm_bL_ops->get_transition_latency)
  145. policy->cpuinfo.transition_latency =
  146. arm_bL_ops->get_transition_latency(cpu_dev);
  147. else
  148. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  149. policy->cur = bL_cpufreq_get(policy->cpu);
  150. cpumask_copy(policy->cpus, topology_core_cpumask(policy->cpu));
  151. dev_info(cpu_dev, "%s: CPU %d initialized\n", __func__, policy->cpu);
  152. return 0;
  153. }
  154. static int bL_cpufreq_exit(struct cpufreq_policy *policy)
  155. {
  156. struct device *cpu_dev;
  157. cpu_dev = get_cpu_device(policy->cpu);
  158. if (!cpu_dev) {
  159. pr_err("%s: failed to get cpu%d device\n", __func__,
  160. policy->cpu);
  161. return -ENODEV;
  162. }
  163. put_cluster_clk_and_freq_table(cpu_dev);
  164. dev_dbg(cpu_dev, "%s: Exited, cpu: %d\n", __func__, policy->cpu);
  165. return 0;
  166. }
  167. /* Export freq_table to sysfs */
  168. static struct freq_attr *bL_cpufreq_attr[] = {
  169. &cpufreq_freq_attr_scaling_available_freqs,
  170. NULL,
  171. };
  172. static struct cpufreq_driver bL_cpufreq_driver = {
  173. .name = "arm-big-little",
  174. .flags = CPUFREQ_STICKY,
  175. .verify = bL_cpufreq_verify_policy,
  176. .target = bL_cpufreq_set_target,
  177. .get = bL_cpufreq_get,
  178. .init = bL_cpufreq_init,
  179. .exit = bL_cpufreq_exit,
  180. .have_governor_per_policy = true,
  181. .attr = bL_cpufreq_attr,
  182. };
  183. int bL_cpufreq_register(struct cpufreq_arm_bL_ops *ops)
  184. {
  185. int ret;
  186. if (arm_bL_ops) {
  187. pr_debug("%s: Already registered: %s, exiting\n", __func__,
  188. arm_bL_ops->name);
  189. return -EBUSY;
  190. }
  191. if (!ops || !strlen(ops->name) || !ops->init_opp_table) {
  192. pr_err("%s: Invalid arm_bL_ops, exiting\n", __func__);
  193. return -ENODEV;
  194. }
  195. arm_bL_ops = ops;
  196. ret = cpufreq_register_driver(&bL_cpufreq_driver);
  197. if (ret) {
  198. pr_info("%s: Failed registering platform driver: %s, err: %d\n",
  199. __func__, ops->name, ret);
  200. arm_bL_ops = NULL;
  201. } else {
  202. pr_info("%s: Registered platform driver: %s\n", __func__,
  203. ops->name);
  204. }
  205. return ret;
  206. }
  207. EXPORT_SYMBOL_GPL(bL_cpufreq_register);
  208. void bL_cpufreq_unregister(struct cpufreq_arm_bL_ops *ops)
  209. {
  210. if (arm_bL_ops != ops) {
  211. pr_err("%s: Registered with: %s, can't unregister, exiting\n",
  212. __func__, arm_bL_ops->name);
  213. return;
  214. }
  215. cpufreq_unregister_driver(&bL_cpufreq_driver);
  216. pr_info("%s: Un-registered platform driver: %s\n", __func__,
  217. arm_bL_ops->name);
  218. arm_bL_ops = NULL;
  219. }
  220. EXPORT_SYMBOL_GPL(bL_cpufreq_unregister);