arm_big_little.c 7.1 KB

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