vexpress-spc-cpufreq.c 15 KB

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