1 // SPDX-License-Identifier: GPL-2.0
3 * Versatile Express SPC CPUFreq Interface driver
5 * Copyright (C) 2013 - 2019 ARM Ltd.
6 * Sudeep Holla <sudeep.holla@arm.com>
8 * Copyright (C) 2013 Linaro.
9 * Viresh Kumar <viresh.kumar@linaro.org>
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/clk.h>
15 #include <linux/cpu.h>
16 #include <linux/cpufreq.h>
17 #include <linux/cpumask.h>
18 #include <linux/device.h>
19 #include <linux/module.h>
20 #include <linux/mutex.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>
27 /* Currently we support only two clusters */
30 #define MAX_CLUSTERS 2
32 #ifdef CONFIG_BL_SWITCHER
33 #include <asm/bL_switcher.h>
34 static bool bL_switching_enabled
;
35 #define is_bL_switching_enabled() bL_switching_enabled
36 #define set_switching_enabled(x) (bL_switching_enabled = (x))
38 #define is_bL_switching_enabled() false
39 #define set_switching_enabled(x) do { } while (0)
40 #define bL_switch_request(...) do { } while (0)
41 #define bL_switcher_put_enabled() do { } while (0)
42 #define bL_switcher_get_enabled() do { } while (0)
45 #define ACTUAL_FREQ(cluster, freq) ((cluster == A7_CLUSTER) ? freq << 1 : freq)
46 #define VIRT_FREQ(cluster, freq) ((cluster == A7_CLUSTER) ? freq >> 1 : freq)
48 static struct clk
*clk
[MAX_CLUSTERS
];
49 static struct cpufreq_frequency_table
*freq_table
[MAX_CLUSTERS
+ 1];
50 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) */
55 static DEFINE_PER_CPU(unsigned int, physical_cluster
);
56 static DEFINE_PER_CPU(unsigned int, cpu_last_req_freq
);
58 static struct mutex cluster_lock
[MAX_CLUSTERS
];
60 static inline int raw_cpu_to_cluster(int cpu
)
62 return topology_physical_package_id(cpu
);
65 static inline int cpu_to_cluster(int cpu
)
67 return is_bL_switching_enabled() ?
68 MAX_CLUSTERS
: raw_cpu_to_cluster(cpu
);
71 static unsigned int find_cluster_maxfreq(int cluster
)
74 u32 max_freq
= 0, cpu_freq
;
76 for_each_online_cpu(j
) {
77 cpu_freq
= per_cpu(cpu_last_req_freq
, j
);
79 if (cluster
== per_cpu(physical_cluster
, j
) &&
87 static unsigned int clk_get_cpu_rate(unsigned int cpu
)
89 u32 cur_cluster
= per_cpu(physical_cluster
, cpu
);
90 u32 rate
= clk_get_rate(clk
[cur_cluster
]) / 1000;
92 /* For switcher we use virtual A7 clock rates */
93 if (is_bL_switching_enabled())
94 rate
= VIRT_FREQ(cur_cluster
, rate
);
99 static unsigned int ve_spc_cpufreq_get_rate(unsigned int cpu
)
101 if (is_bL_switching_enabled())
102 return per_cpu(cpu_last_req_freq
, cpu
);
104 return clk_get_cpu_rate(cpu
);
108 ve_spc_cpufreq_set_rate(u32 cpu
, u32 old_cluster
, u32 new_cluster
, u32 rate
)
110 u32 new_rate
, prev_rate
;
112 bool bLs
= is_bL_switching_enabled();
114 mutex_lock(&cluster_lock
[new_cluster
]);
117 prev_rate
= per_cpu(cpu_last_req_freq
, cpu
);
118 per_cpu(cpu_last_req_freq
, cpu
) = rate
;
119 per_cpu(physical_cluster
, cpu
) = new_cluster
;
121 new_rate
= find_cluster_maxfreq(new_cluster
);
122 new_rate
= ACTUAL_FREQ(new_cluster
, new_rate
);
127 ret
= clk_set_rate(clk
[new_cluster
], new_rate
* 1000);
130 * FIXME: clk_set_rate hasn't returned an error here however it
131 * may be that clk_change_rate failed due to hardware or
132 * firmware issues and wasn't able to report that due to the
133 * current design of the clk core layer. To work around this
134 * problem we will read back the clock rate and check it is
135 * correct. This needs to be removed once clk core is fixed.
137 if (clk_get_rate(clk
[new_cluster
]) != new_rate
* 1000)
143 per_cpu(cpu_last_req_freq
, cpu
) = prev_rate
;
144 per_cpu(physical_cluster
, cpu
) = old_cluster
;
147 mutex_unlock(&cluster_lock
[new_cluster
]);
152 mutex_unlock(&cluster_lock
[new_cluster
]);
154 /* Recalc freq for old cluster when switching clusters */
155 if (old_cluster
!= new_cluster
) {
157 bL_switch_request(cpu
, new_cluster
);
159 mutex_lock(&cluster_lock
[old_cluster
]);
161 /* Set freq of old cluster if there are cpus left on it */
162 new_rate
= find_cluster_maxfreq(old_cluster
);
163 new_rate
= ACTUAL_FREQ(old_cluster
, new_rate
);
166 clk_set_rate(clk
[old_cluster
], new_rate
* 1000)) {
167 pr_err("%s: clk_set_rate failed: %d, old cluster: %d\n",
168 __func__
, ret
, old_cluster
);
170 mutex_unlock(&cluster_lock
[old_cluster
]);
176 /* Set clock frequency */
177 static int ve_spc_cpufreq_set_target(struct cpufreq_policy
*policy
,
180 u32 cpu
= policy
->cpu
, cur_cluster
, new_cluster
, actual_cluster
;
181 unsigned int freqs_new
;
183 cur_cluster
= cpu_to_cluster(cpu
);
184 new_cluster
= actual_cluster
= per_cpu(physical_cluster
, cpu
);
186 freqs_new
= freq_table
[cur_cluster
][index
].frequency
;
188 if (is_bL_switching_enabled()) {
189 if (actual_cluster
== A15_CLUSTER
&& freqs_new
< clk_big_min
)
190 new_cluster
= A7_CLUSTER
;
191 else if (actual_cluster
== A7_CLUSTER
&&
192 freqs_new
> clk_little_max
)
193 new_cluster
= A15_CLUSTER
;
196 return ve_spc_cpufreq_set_rate(cpu
, actual_cluster
, new_cluster
,
200 static inline u32
get_table_count(struct cpufreq_frequency_table
*table
)
204 for (count
= 0; table
[count
].frequency
!= CPUFREQ_TABLE_END
; count
++)
210 /* get the minimum frequency in the cpufreq_frequency_table */
211 static inline u32
get_table_min(struct cpufreq_frequency_table
*table
)
213 struct cpufreq_frequency_table
*pos
;
216 cpufreq_for_each_entry(pos
, table
)
217 if (pos
->frequency
< min_freq
)
218 min_freq
= pos
->frequency
;
222 /* get the maximum frequency in the cpufreq_frequency_table */
223 static inline u32
get_table_max(struct cpufreq_frequency_table
*table
)
225 struct cpufreq_frequency_table
*pos
;
228 cpufreq_for_each_entry(pos
, table
)
229 if (pos
->frequency
> max_freq
)
230 max_freq
= pos
->frequency
;
234 static bool search_frequency(struct cpufreq_frequency_table
*table
, int size
,
239 for (count
= 0; count
< size
; count
++) {
240 if (table
[count
].frequency
== freq
)
247 static int merge_cluster_tables(void)
249 int i
, j
, k
= 0, count
= 1;
250 struct cpufreq_frequency_table
*table
;
252 for (i
= 0; i
< MAX_CLUSTERS
; i
++)
253 count
+= get_table_count(freq_table
[i
]);
255 table
= kcalloc(count
, sizeof(*table
), GFP_KERNEL
);
259 freq_table
[MAX_CLUSTERS
] = table
;
261 /* Add in reverse order to get freqs in increasing order */
262 for (i
= MAX_CLUSTERS
- 1; i
>= 0; i
--, count
= k
) {
263 for (j
= 0; freq_table
[i
][j
].frequency
!= CPUFREQ_TABLE_END
;
265 if (i
== A15_CLUSTER
&&
266 search_frequency(table
, count
, freq_table
[i
][j
].frequency
))
267 continue; /* skip duplicates */
268 table
[k
++].frequency
=
269 VIRT_FREQ(i
, freq_table
[i
][j
].frequency
);
273 table
[k
].driver_data
= k
;
274 table
[k
].frequency
= CPUFREQ_TABLE_END
;
279 static void _put_cluster_clk_and_freq_table(struct device
*cpu_dev
,
280 const struct cpumask
*cpumask
)
282 u32 cluster
= raw_cpu_to_cluster(cpu_dev
->id
);
284 if (!freq_table
[cluster
])
287 clk_put(clk
[cluster
]);
288 dev_pm_opp_free_cpufreq_table(cpu_dev
, &freq_table
[cluster
]);
291 static void put_cluster_clk_and_freq_table(struct device
*cpu_dev
,
292 const struct cpumask
*cpumask
)
294 u32 cluster
= cpu_to_cluster(cpu_dev
->id
);
297 if (atomic_dec_return(&cluster_usage
[cluster
]))
300 if (cluster
< MAX_CLUSTERS
)
301 return _put_cluster_clk_and_freq_table(cpu_dev
, cpumask
);
303 for_each_present_cpu(i
) {
304 struct device
*cdev
= get_cpu_device(i
);
309 _put_cluster_clk_and_freq_table(cdev
, cpumask
);
312 /* free virtual table */
313 kfree(freq_table
[cluster
]);
316 static int _get_cluster_clk_and_freq_table(struct device
*cpu_dev
,
317 const struct cpumask
*cpumask
)
319 u32 cluster
= raw_cpu_to_cluster(cpu_dev
->id
);
322 if (freq_table
[cluster
])
326 * platform specific SPC code must initialise the opp table
327 * so just check if the OPP count is non-zero
329 ret
= dev_pm_opp_get_opp_count(cpu_dev
) <= 0;
333 ret
= dev_pm_opp_init_cpufreq_table(cpu_dev
, &freq_table
[cluster
]);
337 clk
[cluster
] = clk_get(cpu_dev
, NULL
);
338 if (!IS_ERR(clk
[cluster
]))
341 dev_err(cpu_dev
, "%s: Failed to get clk for cpu: %d, cluster: %d\n",
342 __func__
, cpu_dev
->id
, cluster
);
343 ret
= PTR_ERR(clk
[cluster
]);
344 dev_pm_opp_free_cpufreq_table(cpu_dev
, &freq_table
[cluster
]);
347 dev_err(cpu_dev
, "%s: Failed to get data for cluster: %d\n", __func__
,
352 static int get_cluster_clk_and_freq_table(struct device
*cpu_dev
,
353 const struct cpumask
*cpumask
)
355 u32 cluster
= cpu_to_cluster(cpu_dev
->id
);
358 if (atomic_inc_return(&cluster_usage
[cluster
]) != 1)
361 if (cluster
< MAX_CLUSTERS
) {
362 ret
= _get_cluster_clk_and_freq_table(cpu_dev
, cpumask
);
364 atomic_dec(&cluster_usage
[cluster
]);
369 * Get data for all clusters and fill virtual cluster with a merge of
372 for_each_present_cpu(i
) {
373 struct device
*cdev
= get_cpu_device(i
);
378 ret
= _get_cluster_clk_and_freq_table(cdev
, cpumask
);
383 ret
= merge_cluster_tables();
387 /* Assuming 2 cluster, set clk_big_min and clk_little_max */
388 clk_big_min
= get_table_min(freq_table
[A15_CLUSTER
]);
389 clk_little_max
= VIRT_FREQ(A7_CLUSTER
,
390 get_table_max(freq_table
[A7_CLUSTER
]));
395 for_each_present_cpu(i
) {
396 struct device
*cdev
= get_cpu_device(i
);
401 _put_cluster_clk_and_freq_table(cdev
, cpumask
);
404 atomic_dec(&cluster_usage
[cluster
]);
409 /* Per-CPU initialization */
410 static int ve_spc_cpufreq_init(struct cpufreq_policy
*policy
)
412 u32 cur_cluster
= cpu_to_cluster(policy
->cpu
);
413 struct device
*cpu_dev
;
416 cpu_dev
= get_cpu_device(policy
->cpu
);
418 pr_err("%s: failed to get cpu%d device\n", __func__
,
423 if (cur_cluster
< MAX_CLUSTERS
) {
426 dev_pm_opp_get_sharing_cpus(cpu_dev
, policy
->cpus
);
428 for_each_cpu(cpu
, policy
->cpus
)
429 per_cpu(physical_cluster
, cpu
) = cur_cluster
;
431 /* Assumption: during init, we are always running on A15 */
432 per_cpu(physical_cluster
, policy
->cpu
) = A15_CLUSTER
;
435 ret
= get_cluster_clk_and_freq_table(cpu_dev
, policy
->cpus
);
439 policy
->freq_table
= freq_table
[cur_cluster
];
440 policy
->cpuinfo
.transition_latency
= 1000000; /* 1 ms */
442 if (is_bL_switching_enabled())
443 per_cpu(cpu_last_req_freq
, policy
->cpu
) =
444 clk_get_cpu_rate(policy
->cpu
);
446 dev_info(cpu_dev
, "%s: CPU %d initialized\n", __func__
, policy
->cpu
);
450 static void ve_spc_cpufreq_exit(struct cpufreq_policy
*policy
)
452 struct device
*cpu_dev
;
454 cpu_dev
= get_cpu_device(policy
->cpu
);
456 pr_err("%s: failed to get cpu%d device\n", __func__
,
461 put_cluster_clk_and_freq_table(cpu_dev
, policy
->related_cpus
);
464 static struct cpufreq_driver ve_spc_cpufreq_driver
= {
465 .name
= "vexpress-spc",
466 .flags
= CPUFREQ_HAVE_GOVERNOR_PER_POLICY
|
467 CPUFREQ_NEED_INITIAL_FREQ_CHECK
,
468 .verify
= cpufreq_generic_frequency_table_verify
,
469 .target_index
= ve_spc_cpufreq_set_target
,
470 .get
= ve_spc_cpufreq_get_rate
,
471 .init
= ve_spc_cpufreq_init
,
472 .exit
= ve_spc_cpufreq_exit
,
473 .register_em
= cpufreq_register_em_with_opp
,
474 .attr
= cpufreq_generic_attr
,
477 #ifdef CONFIG_BL_SWITCHER
478 static int bL_cpufreq_switcher_notifier(struct notifier_block
*nfb
,
479 unsigned long action
, void *_arg
)
481 pr_debug("%s: action: %ld\n", __func__
, action
);
484 case BL_NOTIFY_PRE_ENABLE
:
485 case BL_NOTIFY_PRE_DISABLE
:
486 cpufreq_unregister_driver(&ve_spc_cpufreq_driver
);
489 case BL_NOTIFY_POST_ENABLE
:
490 set_switching_enabled(true);
491 cpufreq_register_driver(&ve_spc_cpufreq_driver
);
494 case BL_NOTIFY_POST_DISABLE
:
495 set_switching_enabled(false);
496 cpufreq_register_driver(&ve_spc_cpufreq_driver
);
506 static struct notifier_block bL_switcher_notifier
= {
507 .notifier_call
= bL_cpufreq_switcher_notifier
,
510 static int __bLs_register_notifier(void)
512 return bL_switcher_register_notifier(&bL_switcher_notifier
);
515 static int __bLs_unregister_notifier(void)
517 return bL_switcher_unregister_notifier(&bL_switcher_notifier
);
520 static int __bLs_register_notifier(void) { return 0; }
521 static int __bLs_unregister_notifier(void) { return 0; }
524 static int ve_spc_cpufreq_probe(struct platform_device
*pdev
)
528 set_switching_enabled(bL_switcher_get_enabled());
530 for (i
= 0; i
< MAX_CLUSTERS
; i
++)
531 mutex_init(&cluster_lock
[i
]);
533 if (!is_bL_switching_enabled())
534 ve_spc_cpufreq_driver
.flags
|= CPUFREQ_IS_COOLING_DEV
;
536 ret
= cpufreq_register_driver(&ve_spc_cpufreq_driver
);
538 pr_info("%s: Failed registering platform driver: %s, err: %d\n",
539 __func__
, ve_spc_cpufreq_driver
.name
, ret
);
541 ret
= __bLs_register_notifier();
543 cpufreq_unregister_driver(&ve_spc_cpufreq_driver
);
545 pr_info("%s: Registered platform driver: %s\n",
546 __func__
, ve_spc_cpufreq_driver
.name
);
549 bL_switcher_put_enabled();
553 static void ve_spc_cpufreq_remove(struct platform_device
*pdev
)
555 bL_switcher_get_enabled();
556 __bLs_unregister_notifier();
557 cpufreq_unregister_driver(&ve_spc_cpufreq_driver
);
558 bL_switcher_put_enabled();
559 pr_info("%s: Un-registered platform driver: %s\n", __func__
,
560 ve_spc_cpufreq_driver
.name
);
563 static struct platform_driver ve_spc_cpufreq_platdrv
= {
565 .name
= "vexpress-spc-cpufreq",
567 .probe
= ve_spc_cpufreq_probe
,
568 .remove_new
= ve_spc_cpufreq_remove
,
570 module_platform_driver(ve_spc_cpufreq_platdrv
);
572 MODULE_ALIAS("platform:vexpress-spc-cpufreq");
573 MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
574 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
575 MODULE_DESCRIPTION("Vexpress SPC ARM big LITTLE cpufreq driver");
576 MODULE_LICENSE("GPL v2");