vexpress-spc-cpufreq.c 14 KB

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