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