vexpress-spc-cpufreq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Versatile Express SPC CPUFreq Interface driver
  4. *
  5. * Copyright (C) 2013 - 2019 ARM Ltd.
  6. * Sudeep Holla <sudeep.holla@arm.com>
  7. *
  8. * Copyright (C) 2013 Linaro.
  9. * Viresh Kumar <viresh.kumar@linaro.org>
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/clk.h>
  13. #include <linux/cpu.h>
  14. #include <linux/cpufreq.h>
  15. #include <linux/cpumask.h>
  16. #include <linux/device.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_opp.h>
  21. #include <linux/slab.h>
  22. #include <linux/topology.h>
  23. #include <linux/types.h>
  24. /* Currently we support only two clusters */
  25. #define A15_CLUSTER 0
  26. #define A7_CLUSTER 1
  27. #define MAX_CLUSTERS 2
  28. #ifdef CONFIG_BL_SWITCHER
  29. #include <asm/bL_switcher.h>
  30. static bool bL_switching_enabled;
  31. #define is_bL_switching_enabled() bL_switching_enabled
  32. #define set_switching_enabled(x) (bL_switching_enabled = (x))
  33. #else
  34. #define is_bL_switching_enabled() false
  35. #define set_switching_enabled(x) do { } while (0)
  36. #define bL_switch_request(...) do { } while (0)
  37. #define bL_switcher_put_enabled() do { } while (0)
  38. #define bL_switcher_get_enabled() do { } while (0)
  39. #endif
  40. #define ACTUAL_FREQ(cluster, freq) ((cluster == A7_CLUSTER) ? freq << 1 : freq)
  41. #define VIRT_FREQ(cluster, freq) ((cluster == A7_CLUSTER) ? freq >> 1 : freq)
  42. static struct clk *clk[MAX_CLUSTERS];
  43. static struct cpufreq_frequency_table *freq_table[MAX_CLUSTERS + 1];
  44. static atomic_t cluster_usage[MAX_CLUSTERS + 1];
  45. static unsigned int clk_big_min; /* (Big) clock frequencies */
  46. static unsigned int clk_little_max; /* Maximum clock frequency (Little) */
  47. static DEFINE_PER_CPU(unsigned int, physical_cluster);
  48. static DEFINE_PER_CPU(unsigned int, cpu_last_req_freq);
  49. static struct mutex cluster_lock[MAX_CLUSTERS];
  50. static inline int raw_cpu_to_cluster(int cpu)
  51. {
  52. return topology_physical_package_id(cpu);
  53. }
  54. static inline int cpu_to_cluster(int cpu)
  55. {
  56. return is_bL_switching_enabled() ?
  57. MAX_CLUSTERS : raw_cpu_to_cluster(cpu);
  58. }
  59. static unsigned int find_cluster_maxfreq(int cluster)
  60. {
  61. int j;
  62. u32 max_freq = 0, cpu_freq;
  63. for_each_online_cpu(j) {
  64. cpu_freq = per_cpu(cpu_last_req_freq, j);
  65. if (cluster == per_cpu(physical_cluster, j) &&
  66. max_freq < cpu_freq)
  67. max_freq = cpu_freq;
  68. }
  69. return max_freq;
  70. }
  71. static unsigned int clk_get_cpu_rate(unsigned int cpu)
  72. {
  73. u32 cur_cluster = per_cpu(physical_cluster, cpu);
  74. u32 rate = clk_get_rate(clk[cur_cluster]) / 1000;
  75. /* For switcher we use virtual A7 clock rates */
  76. if (is_bL_switching_enabled())
  77. rate = VIRT_FREQ(cur_cluster, rate);
  78. return rate;
  79. }
  80. static unsigned int ve_spc_cpufreq_get_rate(unsigned int cpu)
  81. {
  82. if (is_bL_switching_enabled())
  83. return per_cpu(cpu_last_req_freq, cpu);
  84. else
  85. return clk_get_cpu_rate(cpu);
  86. }
  87. static unsigned int
  88. ve_spc_cpufreq_set_rate(u32 cpu, u32 old_cluster, u32 new_cluster, u32 rate)
  89. {
  90. u32 new_rate, prev_rate;
  91. int ret;
  92. bool bLs = is_bL_switching_enabled();
  93. mutex_lock(&cluster_lock[new_cluster]);
  94. if (bLs) {
  95. prev_rate = per_cpu(cpu_last_req_freq, cpu);
  96. per_cpu(cpu_last_req_freq, cpu) = rate;
  97. per_cpu(physical_cluster, cpu) = new_cluster;
  98. new_rate = find_cluster_maxfreq(new_cluster);
  99. new_rate = ACTUAL_FREQ(new_cluster, new_rate);
  100. } else {
  101. new_rate = rate;
  102. }
  103. ret = clk_set_rate(clk[new_cluster], new_rate * 1000);
  104. if (!ret) {
  105. /*
  106. * FIXME: clk_set_rate hasn't returned an error here however it
  107. * may be that clk_change_rate failed due to hardware or
  108. * firmware issues and wasn't able to report that due to the
  109. * current design of the clk core layer. To work around this
  110. * problem we will read back the clock rate and check it is
  111. * correct. This needs to be removed once clk core is fixed.
  112. */
  113. if (clk_get_rate(clk[new_cluster]) != new_rate * 1000)
  114. ret = -EIO;
  115. }
  116. if (WARN_ON(ret)) {
  117. if (bLs) {
  118. per_cpu(cpu_last_req_freq, cpu) = prev_rate;
  119. per_cpu(physical_cluster, cpu) = old_cluster;
  120. }
  121. mutex_unlock(&cluster_lock[new_cluster]);
  122. return ret;
  123. }
  124. mutex_unlock(&cluster_lock[new_cluster]);
  125. /* Recalc freq for old cluster when switching clusters */
  126. if (old_cluster != new_cluster) {
  127. /* Switch cluster */
  128. bL_switch_request(cpu, new_cluster);
  129. mutex_lock(&cluster_lock[old_cluster]);
  130. /* Set freq of old cluster if there are cpus left on it */
  131. new_rate = find_cluster_maxfreq(old_cluster);
  132. new_rate = ACTUAL_FREQ(old_cluster, new_rate);
  133. if (new_rate &&
  134. clk_set_rate(clk[old_cluster], new_rate * 1000)) {
  135. pr_err("%s: clk_set_rate failed: %d, old cluster: %d\n",
  136. __func__, ret, old_cluster);
  137. }
  138. mutex_unlock(&cluster_lock[old_cluster]);
  139. }
  140. return 0;
  141. }
  142. /* Set clock frequency */
  143. static int ve_spc_cpufreq_set_target(struct cpufreq_policy *policy,
  144. unsigned int index)
  145. {
  146. u32 cpu = policy->cpu, cur_cluster, new_cluster, actual_cluster;
  147. unsigned int freqs_new;
  148. cur_cluster = cpu_to_cluster(cpu);
  149. new_cluster = actual_cluster = per_cpu(physical_cluster, cpu);
  150. freqs_new = freq_table[cur_cluster][index].frequency;
  151. if (is_bL_switching_enabled()) {
  152. if (actual_cluster == A15_CLUSTER && freqs_new < clk_big_min)
  153. new_cluster = A7_CLUSTER;
  154. else if (actual_cluster == A7_CLUSTER &&
  155. freqs_new > clk_little_max)
  156. new_cluster = A15_CLUSTER;
  157. }
  158. return ve_spc_cpufreq_set_rate(cpu, actual_cluster, new_cluster,
  159. freqs_new);
  160. }
  161. static inline u32 get_table_count(struct cpufreq_frequency_table *table)
  162. {
  163. int count;
  164. for (count = 0; table[count].frequency != CPUFREQ_TABLE_END; count++)
  165. ;
  166. return count;
  167. }
  168. /* get the minimum frequency in the cpufreq_frequency_table */
  169. static inline u32 get_table_min(struct cpufreq_frequency_table *table)
  170. {
  171. struct cpufreq_frequency_table *pos;
  172. u32 min_freq = ~0;
  173. cpufreq_for_each_entry(pos, table)
  174. if (pos->frequency < min_freq)
  175. min_freq = pos->frequency;
  176. return min_freq;
  177. }
  178. /* get the maximum frequency in the cpufreq_frequency_table */
  179. static inline u32 get_table_max(struct cpufreq_frequency_table *table)
  180. {
  181. struct cpufreq_frequency_table *pos;
  182. u32 max_freq = 0;
  183. cpufreq_for_each_entry(pos, table)
  184. if (pos->frequency > max_freq)
  185. max_freq = pos->frequency;
  186. return max_freq;
  187. }
  188. static bool search_frequency(struct cpufreq_frequency_table *table, int size,
  189. unsigned int freq)
  190. {
  191. int count;
  192. for (count = 0; count < size; count++) {
  193. if (table[count].frequency == freq)
  194. return true;
  195. }
  196. return false;
  197. }
  198. static int merge_cluster_tables(void)
  199. {
  200. int i, j, k = 0, count = 1;
  201. struct cpufreq_frequency_table *table;
  202. for (i = 0; i < MAX_CLUSTERS; i++)
  203. count += get_table_count(freq_table[i]);
  204. table = kzalloc_objs(*table, count);
  205. if (!table)
  206. return -ENOMEM;
  207. freq_table[MAX_CLUSTERS] = table;
  208. /* Add in reverse order to get freqs in increasing order */
  209. for (i = MAX_CLUSTERS - 1; i >= 0; i--, count = k) {
  210. for (j = 0; freq_table[i][j].frequency != CPUFREQ_TABLE_END;
  211. j++) {
  212. if (i == A15_CLUSTER &&
  213. search_frequency(table, count, freq_table[i][j].frequency))
  214. continue; /* skip duplicates */
  215. table[k++].frequency =
  216. VIRT_FREQ(i, freq_table[i][j].frequency);
  217. }
  218. }
  219. table[k].driver_data = k;
  220. table[k].frequency = CPUFREQ_TABLE_END;
  221. return 0;
  222. }
  223. static void _put_cluster_clk_and_freq_table(struct device *cpu_dev,
  224. const struct cpumask *cpumask)
  225. {
  226. u32 cluster = raw_cpu_to_cluster(cpu_dev->id);
  227. if (!freq_table[cluster])
  228. return;
  229. clk_put(clk[cluster]);
  230. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
  231. }
  232. static void put_cluster_clk_and_freq_table(struct device *cpu_dev,
  233. const struct cpumask *cpumask)
  234. {
  235. u32 cluster = cpu_to_cluster(cpu_dev->id);
  236. int i;
  237. if (atomic_dec_return(&cluster_usage[cluster]))
  238. return;
  239. if (cluster < MAX_CLUSTERS)
  240. return _put_cluster_clk_and_freq_table(cpu_dev, cpumask);
  241. for_each_present_cpu(i) {
  242. struct device *cdev = get_cpu_device(i);
  243. if (!cdev)
  244. return;
  245. _put_cluster_clk_and_freq_table(cdev, cpumask);
  246. }
  247. /* free virtual table */
  248. kfree(freq_table[cluster]);
  249. }
  250. static int _get_cluster_clk_and_freq_table(struct device *cpu_dev,
  251. const struct cpumask *cpumask)
  252. {
  253. u32 cluster = raw_cpu_to_cluster(cpu_dev->id);
  254. int ret;
  255. if (freq_table[cluster])
  256. return 0;
  257. /*
  258. * platform specific SPC code must initialise the opp table
  259. * so just check if the OPP count is non-zero
  260. */
  261. ret = dev_pm_opp_get_opp_count(cpu_dev) <= 0;
  262. if (ret)
  263. goto out;
  264. ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table[cluster]);
  265. if (ret)
  266. goto out;
  267. clk[cluster] = clk_get(cpu_dev, NULL);
  268. if (!IS_ERR(clk[cluster]))
  269. return 0;
  270. dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d, cluster: %d\n",
  271. __func__, cpu_dev->id, cluster);
  272. ret = PTR_ERR(clk[cluster]);
  273. dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
  274. out:
  275. dev_err(cpu_dev, "%s: Failed to get data for cluster: %d\n", __func__,
  276. cluster);
  277. return ret;
  278. }
  279. static int get_cluster_clk_and_freq_table(struct device *cpu_dev,
  280. const struct cpumask *cpumask)
  281. {
  282. u32 cluster = cpu_to_cluster(cpu_dev->id);
  283. int i, ret;
  284. if (atomic_inc_return(&cluster_usage[cluster]) != 1)
  285. return 0;
  286. if (cluster < MAX_CLUSTERS) {
  287. ret = _get_cluster_clk_and_freq_table(cpu_dev, cpumask);
  288. if (ret)
  289. atomic_dec(&cluster_usage[cluster]);
  290. return ret;
  291. }
  292. /*
  293. * Get data for all clusters and fill virtual cluster with a merge of
  294. * both
  295. */
  296. for_each_present_cpu(i) {
  297. struct device *cdev = get_cpu_device(i);
  298. if (!cdev)
  299. return -ENODEV;
  300. ret = _get_cluster_clk_and_freq_table(cdev, cpumask);
  301. if (ret)
  302. goto put_clusters;
  303. }
  304. ret = merge_cluster_tables();
  305. if (ret)
  306. goto put_clusters;
  307. /* Assuming 2 cluster, set clk_big_min and clk_little_max */
  308. clk_big_min = get_table_min(freq_table[A15_CLUSTER]);
  309. clk_little_max = VIRT_FREQ(A7_CLUSTER,
  310. get_table_max(freq_table[A7_CLUSTER]));
  311. return 0;
  312. put_clusters:
  313. for_each_present_cpu(i) {
  314. struct device *cdev = get_cpu_device(i);
  315. if (!cdev)
  316. return -ENODEV;
  317. _put_cluster_clk_and_freq_table(cdev, cpumask);
  318. }
  319. atomic_dec(&cluster_usage[cluster]);
  320. return ret;
  321. }
  322. /* Per-CPU initialization */
  323. static int ve_spc_cpufreq_init(struct cpufreq_policy *policy)
  324. {
  325. u32 cur_cluster = cpu_to_cluster(policy->cpu);
  326. struct device *cpu_dev;
  327. int ret;
  328. cpu_dev = get_cpu_device(policy->cpu);
  329. if (!cpu_dev) {
  330. pr_err("%s: failed to get cpu%d device\n", __func__,
  331. policy->cpu);
  332. return -ENODEV;
  333. }
  334. if (cur_cluster < MAX_CLUSTERS) {
  335. int cpu;
  336. dev_pm_opp_get_sharing_cpus(cpu_dev, policy->cpus);
  337. for_each_cpu(cpu, policy->cpus)
  338. per_cpu(physical_cluster, cpu) = cur_cluster;
  339. } else {
  340. /* Assumption: during init, we are always running on A15 */
  341. per_cpu(physical_cluster, policy->cpu) = A15_CLUSTER;
  342. }
  343. ret = get_cluster_clk_and_freq_table(cpu_dev, policy->cpus);
  344. if (ret)
  345. return ret;
  346. policy->freq_table = freq_table[cur_cluster];
  347. policy->cpuinfo.transition_latency = 1000000; /* 1 ms */
  348. if (is_bL_switching_enabled())
  349. per_cpu(cpu_last_req_freq, policy->cpu) =
  350. clk_get_cpu_rate(policy->cpu);
  351. dev_info(cpu_dev, "%s: CPU %d initialized\n", __func__, policy->cpu);
  352. return 0;
  353. }
  354. static void ve_spc_cpufreq_exit(struct cpufreq_policy *policy)
  355. {
  356. struct device *cpu_dev;
  357. cpu_dev = get_cpu_device(policy->cpu);
  358. if (!cpu_dev) {
  359. pr_err("%s: failed to get cpu%d device\n", __func__,
  360. policy->cpu);
  361. return;
  362. }
  363. put_cluster_clk_and_freq_table(cpu_dev, policy->related_cpus);
  364. }
  365. static struct cpufreq_driver ve_spc_cpufreq_driver = {
  366. .name = "vexpress-spc",
  367. .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
  368. CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  369. .verify = cpufreq_generic_frequency_table_verify,
  370. .target_index = ve_spc_cpufreq_set_target,
  371. .get = ve_spc_cpufreq_get_rate,
  372. .init = ve_spc_cpufreq_init,
  373. .exit = ve_spc_cpufreq_exit,
  374. .register_em = cpufreq_register_em_with_opp,
  375. };
  376. #ifdef CONFIG_BL_SWITCHER
  377. static int bL_cpufreq_switcher_notifier(struct notifier_block *nfb,
  378. unsigned long action, void *_arg)
  379. {
  380. pr_debug("%s: action: %ld\n", __func__, action);
  381. switch (action) {
  382. case BL_NOTIFY_PRE_ENABLE:
  383. case BL_NOTIFY_PRE_DISABLE:
  384. cpufreq_unregister_driver(&ve_spc_cpufreq_driver);
  385. break;
  386. case BL_NOTIFY_POST_ENABLE:
  387. set_switching_enabled(true);
  388. cpufreq_register_driver(&ve_spc_cpufreq_driver);
  389. break;
  390. case BL_NOTIFY_POST_DISABLE:
  391. set_switching_enabled(false);
  392. cpufreq_register_driver(&ve_spc_cpufreq_driver);
  393. break;
  394. default:
  395. return NOTIFY_DONE;
  396. }
  397. return NOTIFY_OK;
  398. }
  399. static struct notifier_block bL_switcher_notifier = {
  400. .notifier_call = bL_cpufreq_switcher_notifier,
  401. };
  402. static int __bLs_register_notifier(void)
  403. {
  404. return bL_switcher_register_notifier(&bL_switcher_notifier);
  405. }
  406. static int __bLs_unregister_notifier(void)
  407. {
  408. return bL_switcher_unregister_notifier(&bL_switcher_notifier);
  409. }
  410. #else
  411. static int __bLs_register_notifier(void) { return 0; }
  412. static int __bLs_unregister_notifier(void) { return 0; }
  413. #endif
  414. static int ve_spc_cpufreq_probe(struct platform_device *pdev)
  415. {
  416. int ret, i;
  417. set_switching_enabled(bL_switcher_get_enabled());
  418. for (i = 0; i < MAX_CLUSTERS; i++)
  419. mutex_init(&cluster_lock[i]);
  420. if (!is_bL_switching_enabled())
  421. ve_spc_cpufreq_driver.flags |= CPUFREQ_IS_COOLING_DEV;
  422. ret = cpufreq_register_driver(&ve_spc_cpufreq_driver);
  423. if (ret) {
  424. pr_info("%s: Failed registering platform driver: %s, err: %d\n",
  425. __func__, ve_spc_cpufreq_driver.name, ret);
  426. } else {
  427. ret = __bLs_register_notifier();
  428. if (ret)
  429. cpufreq_unregister_driver(&ve_spc_cpufreq_driver);
  430. else
  431. pr_info("%s: Registered platform driver: %s\n",
  432. __func__, ve_spc_cpufreq_driver.name);
  433. }
  434. bL_switcher_put_enabled();
  435. return ret;
  436. }
  437. static void ve_spc_cpufreq_remove(struct platform_device *pdev)
  438. {
  439. bL_switcher_get_enabled();
  440. __bLs_unregister_notifier();
  441. cpufreq_unregister_driver(&ve_spc_cpufreq_driver);
  442. bL_switcher_put_enabled();
  443. pr_info("%s: Un-registered platform driver: %s\n", __func__,
  444. ve_spc_cpufreq_driver.name);
  445. }
  446. static struct platform_driver ve_spc_cpufreq_platdrv = {
  447. .driver = {
  448. .name = "vexpress-spc-cpufreq",
  449. },
  450. .probe = ve_spc_cpufreq_probe,
  451. .remove = ve_spc_cpufreq_remove,
  452. };
  453. module_platform_driver(ve_spc_cpufreq_platdrv);
  454. MODULE_ALIAS("platform:vexpress-spc-cpufreq");
  455. MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
  456. MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
  457. MODULE_DESCRIPTION("Vexpress SPC ARM big LITTLE cpufreq driver");
  458. MODULE_LICENSE("GPL v2");