arm_big_little.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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/module.h>
  26. #include <linux/mutex.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/pm_opp.h>
  29. #include <linux/slab.h>
  30. #include <linux/topology.h>
  31. #include <linux/types.h>
  32. #include <asm/bL_switcher.h>
  33. #include "arm_big_little.h"
  34. /* Currently we support only two clusters */
  35. #define A15_CLUSTER 0
  36. #define A7_CLUSTER 1
  37. #define MAX_CLUSTERS 2
  38. #ifdef CONFIG_BL_SWITCHER
  39. static bool bL_switching_enabled;
  40. #define is_bL_switching_enabled() bL_switching_enabled
  41. #define set_switching_enabled(x) (bL_switching_enabled = (x))
  42. #else
  43. #define is_bL_switching_enabled() false
  44. #define set_switching_enabled(x) do { } while (0)
  45. #endif
  46. #define ACTUAL_FREQ(cluster, freq) ((cluster == A7_CLUSTER) ? freq << 1 : freq)
  47. #define VIRT_FREQ(cluster, freq) ((cluster == A7_CLUSTER) ? freq >> 1 : freq)
  48. static struct cpufreq_arm_bL_ops *arm_bL_ops;
  49. static struct clk *clk[MAX_CLUSTERS];
  50. static struct cpufreq_frequency_table *freq_table[MAX_CLUSTERS + 1];
  51. static atomic_t cluster_usage[MAX_CLUSTERS + 1];
  52. static unsigned int clk_big_min; /* (Big) clock frequencies */
  53. static unsigned int clk_little_max; /* Maximum clock frequency (Little) */
  54. static DEFINE_PER_CPU(unsigned int, physical_cluster);
  55. static DEFINE_PER_CPU(unsigned int, cpu_last_req_freq);
  56. static struct mutex cluster_lock[MAX_CLUSTERS];
  57. static inline int raw_cpu_to_cluster(int cpu)
  58. {
  59. return topology_physical_package_id(cpu);
  60. }
  61. static inline int cpu_to_cluster(int cpu)
  62. {
  63. return is_bL_switching_enabled() ?
  64. MAX_CLUSTERS : raw_cpu_to_cluster(cpu);
  65. }
  66. static unsigned int find_cluster_maxfreq(int cluster)
  67. {
  68. int j;
  69. u32 max_freq = 0, cpu_freq;
  70. for_each_online_cpu(j) {
  71. cpu_freq = per_cpu(cpu_last_req_freq, j);
  72. if ((cluster == per_cpu(physical_cluster, j)) &&
  73. (max_freq < cpu_freq))
  74. max_freq = cpu_freq;
  75. }
  76. pr_debug("%s: cluster: %d, max freq: %d\n", __func__, cluster,
  77. max_freq);
  78. return max_freq;
  79. }
  80. static unsigned int clk_get_cpu_rate(unsigned int cpu)
  81. {
  82. u32 cur_cluster = per_cpu(physical_cluster, cpu);
  83. u32 rate = clk_get_rate(clk[cur_cluster]) / 1000;
  84. /* For switcher we use virtual A7 clock rates */
  85. if (is_bL_switching_enabled())
  86. rate = VIRT_FREQ(cur_cluster, rate);
  87. pr_debug("%s: cpu: %d, cluster: %d, freq: %u\n", __func__, cpu,
  88. cur_cluster, rate);
  89. return rate;
  90. }
  91. static unsigned int bL_cpufreq_get_rate(unsigned int cpu)
  92. {
  93. if (is_bL_switching_enabled()) {
  94. pr_debug("%s: freq: %d\n", __func__, per_cpu(cpu_last_req_freq,
  95. cpu));
  96. return per_cpu(cpu_last_req_freq, cpu);
  97. } else {
  98. return clk_get_cpu_rate(cpu);
  99. }
  100. }
  101. static unsigned int
  102. bL_cpufreq_set_rate(u32 cpu, u32 old_cluster, u32 new_cluster, u32 rate)
  103. {
  104. u32 new_rate, prev_rate;
  105. int ret;
  106. bool bLs = is_bL_switching_enabled();
  107. mutex_lock(&cluster_lock[new_cluster]);
  108. if (bLs) {
  109. prev_rate = per_cpu(cpu_last_req_freq, cpu);
  110. per_cpu(cpu_last_req_freq, cpu) = rate;
  111. per_cpu(physical_cluster, cpu) = new_cluster;
  112. new_rate = find_cluster_maxfreq(new_cluster);
  113. new_rate = ACTUAL_FREQ(new_cluster, new_rate);
  114. } else {
  115. new_rate = rate;
  116. }
  117. pr_debug("%s: cpu: %d, old cluster: %d, new cluster: %d, freq: %d\n",
  118. __func__, cpu, old_cluster, new_cluster, new_rate);
  119. ret = clk_set_rate(clk[new_cluster], new_rate * 1000);
  120. if (WARN_ON(ret)) {
  121. pr_err("clk_set_rate failed: %d, new cluster: %d\n", ret,
  122. new_cluster);
  123. if (bLs) {
  124. per_cpu(cpu_last_req_freq, cpu) = prev_rate;
  125. per_cpu(physical_cluster, cpu) = old_cluster;
  126. }
  127. mutex_unlock(&cluster_lock[new_cluster]);
  128. return ret;
  129. }
  130. mutex_unlock(&cluster_lock[new_cluster]);
  131. /* Recalc freq for old cluster when switching clusters */
  132. if (old_cluster != new_cluster) {
  133. pr_debug("%s: cpu: %d, old cluster: %d, new cluster: %d\n",
  134. __func__, cpu, old_cluster, new_cluster);
  135. /* Switch cluster */
  136. bL_switch_request(cpu, new_cluster);
  137. mutex_lock(&cluster_lock[old_cluster]);
  138. /* Set freq of old cluster if there are cpus left on it */
  139. new_rate = find_cluster_maxfreq(old_cluster);
  140. new_rate = ACTUAL_FREQ(old_cluster, new_rate);
  141. if (new_rate) {
  142. pr_debug("%s: Updating rate of old cluster: %d, to freq: %d\n",
  143. __func__, old_cluster, new_rate);
  144. if (clk_set_rate(clk[old_cluster], new_rate * 1000))
  145. pr_err("%s: clk_set_rate failed: %d, old cluster: %d\n",
  146. __func__, ret, old_cluster);
  147. }
  148. mutex_unlock(&cluster_lock[old_cluster]);
  149. }
  150. return 0;
  151. }
  152. /* Set clock frequency */
  153. static int bL_cpufreq_set_target(struct cpufreq_policy *policy,
  154. unsigned int index)
  155. {
  156. u32 cpu = policy->cpu, cur_cluster, new_cluster, actual_cluster;
  157. unsigned int freqs_new;
  158. cur_cluster = cpu_to_cluster(cpu);
  159. new_cluster = actual_cluster = per_cpu(physical_cluster, cpu);
  160. freqs_new = freq_table[cur_cluster][index].frequency;
  161. if (is_bL_switching_enabled()) {
  162. if ((actual_cluster == A15_CLUSTER) &&
  163. (freqs_new < clk_big_min)) {
  164. new_cluster = A7_CLUSTER;
  165. } else if ((actual_cluster == A7_CLUSTER) &&
  166. (freqs_new > clk_little_max)) {
  167. new_cluster = A15_CLUSTER;
  168. }
  169. }
  170. return bL_cpufreq_set_rate(cpu, actual_cluster, new_cluster, freqs_new);
  171. }
  172. static inline u32 get_table_count(struct cpufreq_frequency_table *table)
  173. {
  174. int count;
  175. for (count = 0; table[count].frequency != CPUFREQ_TABLE_END; count++)
  176. ;
  177. return count;
  178. }
  179. /* get the minimum frequency in the cpufreq_frequency_table */
  180. static inline u32 get_table_min(struct cpufreq_frequency_table *table)
  181. {
  182. struct cpufreq_frequency_table *pos;
  183. uint32_t min_freq = ~0;
  184. cpufreq_for_each_entry(pos, table)
  185. if (pos->frequency < min_freq)
  186. min_freq = pos->frequency;
  187. return min_freq;
  188. }
  189. /* get the maximum frequency in the cpufreq_frequency_table */
  190. static inline u32 get_table_max(struct cpufreq_frequency_table *table)
  191. {
  192. struct cpufreq_frequency_table *pos;
  193. uint32_t max_freq = 0;
  194. cpufreq_for_each_entry(pos, table)
  195. if (pos->frequency > max_freq)
  196. max_freq = pos->frequency;
  197. return max_freq;
  198. }
  199. static int merge_cluster_tables(void)
  200. {
  201. int i, j, k = 0, count = 1;
  202. struct cpufreq_frequency_table *table;
  203. for (i = 0; i < MAX_CLUSTERS; i++)
  204. count += get_table_count(freq_table[i]);
  205. table = kzalloc(sizeof(*table) * count, GFP_KERNEL);
  206. if (!table)
  207. return -ENOMEM;
  208. freq_table[MAX_CLUSTERS] = table;
  209. /* Add in reverse order to get freqs in increasing order */
  210. for (i = MAX_CLUSTERS - 1; i >= 0; i--) {
  211. for (j = 0; freq_table[i][j].frequency != CPUFREQ_TABLE_END;
  212. j++) {
  213. table[k].frequency = VIRT_FREQ(i,
  214. freq_table[i][j].frequency);
  215. pr_debug("%s: index: %d, freq: %d\n", __func__, k,
  216. table[k].frequency);
  217. k++;
  218. }
  219. }
  220. table[k].driver_data = k;
  221. table[k].frequency = CPUFREQ_TABLE_END;
  222. pr_debug("%s: End, table: %p, count: %d\n", __func__, table, k);
  223. return 0;
  224. }
  225. static void _put_cluster_clk_and_freq_table(struct device *cpu_dev)
  226. {
  227. u32 cluster = raw_cpu_to_cluster(cpu_dev->id);
  228. if (!freq_table[cluster])
  229. return;
  230. clk_put(clk[cluster]);
  231. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
  232. if (arm_bL_ops->free_opp_table)
  233. arm_bL_ops->free_opp_table(cpu_dev);
  234. dev_dbg(cpu_dev, "%s: cluster: %d\n", __func__, cluster);
  235. }
  236. static void put_cluster_clk_and_freq_table(struct device *cpu_dev)
  237. {
  238. u32 cluster = cpu_to_cluster(cpu_dev->id);
  239. int i;
  240. if (atomic_dec_return(&cluster_usage[cluster]))
  241. return;
  242. if (cluster < MAX_CLUSTERS)
  243. return _put_cluster_clk_and_freq_table(cpu_dev);
  244. for_each_present_cpu(i) {
  245. struct device *cdev = get_cpu_device(i);
  246. if (!cdev) {
  247. pr_err("%s: failed to get cpu%d device\n", __func__, i);
  248. return;
  249. }
  250. _put_cluster_clk_and_freq_table(cdev);
  251. }
  252. /* free virtual table */
  253. kfree(freq_table[cluster]);
  254. }
  255. static int _get_cluster_clk_and_freq_table(struct device *cpu_dev)
  256. {
  257. u32 cluster = raw_cpu_to_cluster(cpu_dev->id);
  258. char name[14] = "cpu-cluster.";
  259. int ret;
  260. if (freq_table[cluster])
  261. return 0;
  262. ret = arm_bL_ops->init_opp_table(cpu_dev);
  263. if (ret) {
  264. dev_err(cpu_dev, "%s: init_opp_table failed, cpu: %d, err: %d\n",
  265. __func__, cpu_dev->id, ret);
  266. goto out;
  267. }
  268. ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table[cluster]);
  269. if (ret) {
  270. dev_err(cpu_dev, "%s: failed to init cpufreq table, cpu: %d, err: %d\n",
  271. __func__, cpu_dev->id, ret);
  272. goto free_opp_table;
  273. }
  274. name[12] = cluster + '0';
  275. clk[cluster] = clk_get(cpu_dev, name);
  276. if (!IS_ERR(clk[cluster])) {
  277. dev_dbg(cpu_dev, "%s: clk: %p & freq table: %p, cluster: %d\n",
  278. __func__, clk[cluster], freq_table[cluster],
  279. cluster);
  280. return 0;
  281. }
  282. dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d, cluster: %d\n",
  283. __func__, cpu_dev->id, cluster);
  284. ret = PTR_ERR(clk[cluster]);
  285. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
  286. free_opp_table:
  287. if (arm_bL_ops->free_opp_table)
  288. arm_bL_ops->free_opp_table(cpu_dev);
  289. out:
  290. dev_err(cpu_dev, "%s: Failed to get data for cluster: %d\n", __func__,
  291. cluster);
  292. return ret;
  293. }
  294. static int get_cluster_clk_and_freq_table(struct device *cpu_dev)
  295. {
  296. u32 cluster = cpu_to_cluster(cpu_dev->id);
  297. int i, ret;
  298. if (atomic_inc_return(&cluster_usage[cluster]) != 1)
  299. return 0;
  300. if (cluster < MAX_CLUSTERS) {
  301. ret = _get_cluster_clk_and_freq_table(cpu_dev);
  302. if (ret)
  303. atomic_dec(&cluster_usage[cluster]);
  304. return ret;
  305. }
  306. /*
  307. * Get data for all clusters and fill virtual cluster with a merge of
  308. * both
  309. */
  310. for_each_present_cpu(i) {
  311. struct device *cdev = get_cpu_device(i);
  312. if (!cdev) {
  313. pr_err("%s: failed to get cpu%d device\n", __func__, i);
  314. return -ENODEV;
  315. }
  316. ret = _get_cluster_clk_and_freq_table(cdev);
  317. if (ret)
  318. goto put_clusters;
  319. }
  320. ret = merge_cluster_tables();
  321. if (ret)
  322. goto put_clusters;
  323. /* Assuming 2 cluster, set clk_big_min and clk_little_max */
  324. clk_big_min = get_table_min(freq_table[0]);
  325. clk_little_max = VIRT_FREQ(1, get_table_max(freq_table[1]));
  326. pr_debug("%s: cluster: %d, clk_big_min: %d, clk_little_max: %d\n",
  327. __func__, cluster, clk_big_min, clk_little_max);
  328. return 0;
  329. put_clusters:
  330. for_each_present_cpu(i) {
  331. struct device *cdev = get_cpu_device(i);
  332. if (!cdev) {
  333. pr_err("%s: failed to get cpu%d device\n", __func__, i);
  334. return -ENODEV;
  335. }
  336. _put_cluster_clk_and_freq_table(cdev);
  337. }
  338. atomic_dec(&cluster_usage[cluster]);
  339. return ret;
  340. }
  341. /* Per-CPU initialization */
  342. static int bL_cpufreq_init(struct cpufreq_policy *policy)
  343. {
  344. u32 cur_cluster = cpu_to_cluster(policy->cpu);
  345. struct device *cpu_dev;
  346. int ret;
  347. cpu_dev = get_cpu_device(policy->cpu);
  348. if (!cpu_dev) {
  349. pr_err("%s: failed to get cpu%d device\n", __func__,
  350. policy->cpu);
  351. return -ENODEV;
  352. }
  353. ret = get_cluster_clk_and_freq_table(cpu_dev);
  354. if (ret)
  355. return ret;
  356. ret = cpufreq_table_validate_and_show(policy, freq_table[cur_cluster]);
  357. if (ret) {
  358. dev_err(cpu_dev, "CPU %d, cluster: %d invalid freq table\n",
  359. policy->cpu, cur_cluster);
  360. put_cluster_clk_and_freq_table(cpu_dev);
  361. return ret;
  362. }
  363. if (cur_cluster < MAX_CLUSTERS) {
  364. int cpu;
  365. cpumask_copy(policy->cpus, topology_core_cpumask(policy->cpu));
  366. for_each_cpu(cpu, policy->cpus)
  367. per_cpu(physical_cluster, cpu) = cur_cluster;
  368. } else {
  369. /* Assumption: during init, we are always running on A15 */
  370. per_cpu(physical_cluster, policy->cpu) = A15_CLUSTER;
  371. }
  372. if (arm_bL_ops->get_transition_latency)
  373. policy->cpuinfo.transition_latency =
  374. arm_bL_ops->get_transition_latency(cpu_dev);
  375. else
  376. policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
  377. if (is_bL_switching_enabled())
  378. per_cpu(cpu_last_req_freq, policy->cpu) = clk_get_cpu_rate(policy->cpu);
  379. dev_info(cpu_dev, "%s: CPU %d initialized\n", __func__, policy->cpu);
  380. return 0;
  381. }
  382. static int bL_cpufreq_exit(struct cpufreq_policy *policy)
  383. {
  384. struct device *cpu_dev;
  385. cpu_dev = get_cpu_device(policy->cpu);
  386. if (!cpu_dev) {
  387. pr_err("%s: failed to get cpu%d device\n", __func__,
  388. policy->cpu);
  389. return -ENODEV;
  390. }
  391. put_cluster_clk_and_freq_table(cpu_dev);
  392. dev_dbg(cpu_dev, "%s: Exited, cpu: %d\n", __func__, policy->cpu);
  393. return 0;
  394. }
  395. static struct cpufreq_driver bL_cpufreq_driver = {
  396. .name = "arm-big-little",
  397. .flags = CPUFREQ_STICKY |
  398. CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
  399. CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  400. .verify = cpufreq_generic_frequency_table_verify,
  401. .target_index = bL_cpufreq_set_target,
  402. .get = bL_cpufreq_get_rate,
  403. .init = bL_cpufreq_init,
  404. .exit = bL_cpufreq_exit,
  405. .attr = cpufreq_generic_attr,
  406. };
  407. static int bL_cpufreq_switcher_notifier(struct notifier_block *nfb,
  408. unsigned long action, void *_arg)
  409. {
  410. pr_debug("%s: action: %ld\n", __func__, action);
  411. switch (action) {
  412. case BL_NOTIFY_PRE_ENABLE:
  413. case BL_NOTIFY_PRE_DISABLE:
  414. cpufreq_unregister_driver(&bL_cpufreq_driver);
  415. break;
  416. case BL_NOTIFY_POST_ENABLE:
  417. set_switching_enabled(true);
  418. cpufreq_register_driver(&bL_cpufreq_driver);
  419. break;
  420. case BL_NOTIFY_POST_DISABLE:
  421. set_switching_enabled(false);
  422. cpufreq_register_driver(&bL_cpufreq_driver);
  423. break;
  424. default:
  425. return NOTIFY_DONE;
  426. }
  427. return NOTIFY_OK;
  428. }
  429. static struct notifier_block bL_switcher_notifier = {
  430. .notifier_call = bL_cpufreq_switcher_notifier,
  431. };
  432. int bL_cpufreq_register(struct cpufreq_arm_bL_ops *ops)
  433. {
  434. int ret, i;
  435. if (arm_bL_ops) {
  436. pr_debug("%s: Already registered: %s, exiting\n", __func__,
  437. arm_bL_ops->name);
  438. return -EBUSY;
  439. }
  440. if (!ops || !strlen(ops->name) || !ops->init_opp_table) {
  441. pr_err("%s: Invalid arm_bL_ops, exiting\n", __func__);
  442. return -ENODEV;
  443. }
  444. arm_bL_ops = ops;
  445. ret = bL_switcher_get_enabled();
  446. set_switching_enabled(ret);
  447. for (i = 0; i < MAX_CLUSTERS; i++)
  448. mutex_init(&cluster_lock[i]);
  449. ret = cpufreq_register_driver(&bL_cpufreq_driver);
  450. if (ret) {
  451. pr_info("%s: Failed registering platform driver: %s, err: %d\n",
  452. __func__, ops->name, ret);
  453. arm_bL_ops = NULL;
  454. } else {
  455. ret = bL_switcher_register_notifier(&bL_switcher_notifier);
  456. if (ret) {
  457. cpufreq_unregister_driver(&bL_cpufreq_driver);
  458. arm_bL_ops = NULL;
  459. } else {
  460. pr_info("%s: Registered platform driver: %s\n",
  461. __func__, ops->name);
  462. }
  463. }
  464. bL_switcher_put_enabled();
  465. return ret;
  466. }
  467. EXPORT_SYMBOL_GPL(bL_cpufreq_register);
  468. void bL_cpufreq_unregister(struct cpufreq_arm_bL_ops *ops)
  469. {
  470. if (arm_bL_ops != ops) {
  471. pr_err("%s: Registered with: %s, can't unregister, exiting\n",
  472. __func__, arm_bL_ops->name);
  473. return;
  474. }
  475. bL_switcher_get_enabled();
  476. bL_switcher_unregister_notifier(&bL_switcher_notifier);
  477. cpufreq_unregister_driver(&bL_cpufreq_driver);
  478. bL_switcher_put_enabled();
  479. pr_info("%s: Un-registered platform driver: %s\n", __func__,
  480. arm_bL_ops->name);
  481. arm_bL_ops = NULL;
  482. }
  483. EXPORT_SYMBOL_GPL(bL_cpufreq_unregister);
  484. MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
  485. MODULE_DESCRIPTION("Generic ARM big LITTLE cpufreq driver");
  486. MODULE_LICENSE("GPL v2");