arm_big_little.c 7.2 KB

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