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 
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 
14 #include <linux/clk.h>
15 #include <linux/cpu.h>
16 #include <linux/cpufreq.h>
17 #include <linux/cpumask.h>
18 #include <linux/cpu_cooling.h>
19 #include <linux/device.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/of_platform.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_opp.h>
25 #include <linux/slab.h>
26 #include <linux/topology.h>
27 #include <linux/types.h>
28 
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>
36 static 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 
50 static struct thermal_cooling_device *cdev[MAX_CLUSTERS];
51 static struct clk *clk[MAX_CLUSTERS];
52 static struct cpufreq_frequency_table *freq_table[MAX_CLUSTERS + 1];
53 static atomic_t cluster_usage[MAX_CLUSTERS + 1];
54 
55 static unsigned int clk_big_min;	/* (Big) clock frequencies */
56 static unsigned int clk_little_max;	/* Maximum clock frequency (Little) */
57 
58 static DEFINE_PER_CPU(unsigned int, physical_cluster);
59 static DEFINE_PER_CPU(unsigned int, cpu_last_req_freq);
60 
61 static struct mutex cluster_lock[MAX_CLUSTERS];
62 
63 static inline int raw_cpu_to_cluster(int cpu)
64 {
65 	return topology_physical_package_id(cpu);
66 }
67 
68 static inline int cpu_to_cluster(int cpu)
69 {
70 	return is_bL_switching_enabled() ?
71 		MAX_CLUSTERS : raw_cpu_to_cluster(cpu);
72 }
73 
74 static 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 
87 	return max_freq;
88 }
89 
90 static 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 
99 	return rate;
100 }
101 
102 static unsigned int ve_spc_cpufreq_get_rate(unsigned int cpu)
103 {
104 	if (is_bL_switching_enabled())
105 		return per_cpu(cpu_last_req_freq, cpu);
106 	else
107 		return clk_get_cpu_rate(cpu);
108 }
109 
110 static unsigned int
111 ve_spc_cpufreq_set_rate(u32 cpu, u32 old_cluster, u32 new_cluster, u32 rate)
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 
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)) {
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) {
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 
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);
172 		}
173 		mutex_unlock(&cluster_lock[old_cluster]);
174 	}
175 
176 	return 0;
177 }
178 
179 /* Set clock frequency */
180 static int ve_spc_cpufreq_set_target(struct cpufreq_policy *policy,
181 				     unsigned int index)
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 && freqs_new < clk_big_min)
194 			new_cluster = A7_CLUSTER;
195 		else if (actual_cluster == A7_CLUSTER &&
196 			 freqs_new > clk_little_max)
197 			new_cluster = A15_CLUSTER;
198 	}
199 
200 	ret = ve_spc_cpufreq_set_rate(cpu, actual_cluster, new_cluster,
201 				      freqs_new);
202 
203 	if (!ret) {
204 		arch_set_freq_scale(policy->related_cpus, freqs_new,
205 				    policy->cpuinfo.max_freq);
206 	}
207 
208 	return ret;
209 }
210 
211 static inline u32 get_table_count(struct cpufreq_frequency_table *table)
212 {
213 	int count;
214 
215 	for (count = 0; table[count].frequency != CPUFREQ_TABLE_END; count++)
216 		;
217 
218 	return count;
219 }
220 
221 /* get the minimum frequency in the cpufreq_frequency_table */
222 static inline u32 get_table_min(struct cpufreq_frequency_table *table)
223 {
224 	struct cpufreq_frequency_table *pos;
225 	u32 min_freq = ~0;
226 
227 	cpufreq_for_each_entry(pos, table)
228 		if (pos->frequency < min_freq)
229 			min_freq = pos->frequency;
230 	return min_freq;
231 }
232 
233 /* get the maximum frequency in the cpufreq_frequency_table */
234 static inline u32 get_table_max(struct cpufreq_frequency_table *table)
235 {
236 	struct cpufreq_frequency_table *pos;
237 	u32 max_freq = 0;
238 
239 	cpufreq_for_each_entry(pos, table)
240 		if (pos->frequency > max_freq)
241 			max_freq = pos->frequency;
242 	return max_freq;
243 }
244 
245 static bool search_frequency(struct cpufreq_frequency_table *table, int size,
246 			     unsigned int freq)
247 {
248 	int count;
249 
250 	for (count = 0; count < size; count++) {
251 		if (table[count].frequency == freq)
252 			return true;
253 	}
254 
255 	return false;
256 }
257 
258 static int merge_cluster_tables(void)
259 {
260 	int i, j, k = 0, count = 1;
261 	struct cpufreq_frequency_table *table;
262 
263 	for (i = 0; i < MAX_CLUSTERS; i++)
264 		count += get_table_count(freq_table[i]);
265 
266 	table = kcalloc(count, sizeof(*table), GFP_KERNEL);
267 	if (!table)
268 		return -ENOMEM;
269 
270 	freq_table[MAX_CLUSTERS] = table;
271 
272 	/* Add in reverse order to get freqs in increasing order */
273 	for (i = MAX_CLUSTERS - 1; i >= 0; i--, count = k) {
274 		for (j = 0; freq_table[i][j].frequency != CPUFREQ_TABLE_END;
275 		     j++) {
276 			if (i == A15_CLUSTER &&
277 			    search_frequency(table, count, freq_table[i][j].frequency))
278 				continue; /* skip duplicates */
279 			table[k++].frequency =
280 				VIRT_FREQ(i, freq_table[i][j].frequency);
281 		}
282 	}
283 
284 	table[k].driver_data = k;
285 	table[k].frequency = CPUFREQ_TABLE_END;
286 
287 	return 0;
288 }
289 
290 static void _put_cluster_clk_and_freq_table(struct device *cpu_dev,
291 					    const struct cpumask *cpumask)
292 {
293 	u32 cluster = raw_cpu_to_cluster(cpu_dev->id);
294 
295 	if (!freq_table[cluster])
296 		return;
297 
298 	clk_put(clk[cluster]);
299 	dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
300 }
301 
302 static void put_cluster_clk_and_freq_table(struct device *cpu_dev,
303 					   const struct cpumask *cpumask)
304 {
305 	u32 cluster = cpu_to_cluster(cpu_dev->id);
306 	int i;
307 
308 	if (atomic_dec_return(&cluster_usage[cluster]))
309 		return;
310 
311 	if (cluster < MAX_CLUSTERS)
312 		return _put_cluster_clk_and_freq_table(cpu_dev, cpumask);
313 
314 	for_each_present_cpu(i) {
315 		struct device *cdev = get_cpu_device(i);
316 
317 		if (!cdev)
318 			return;
319 
320 		_put_cluster_clk_and_freq_table(cdev, cpumask);
321 	}
322 
323 	/* free virtual table */
324 	kfree(freq_table[cluster]);
325 }
326 
327 static int _get_cluster_clk_and_freq_table(struct device *cpu_dev,
328 					   const struct cpumask *cpumask)
329 {
330 	u32 cluster = raw_cpu_to_cluster(cpu_dev->id);
331 	int ret;
332 
333 	if (freq_table[cluster])
334 		return 0;
335 
336 	/*
337 	 * platform specific SPC code must initialise the opp table
338 	 * so just check if the OPP count is non-zero
339 	 */
340 	ret = dev_pm_opp_get_opp_count(cpu_dev) <= 0;
341 	if (ret)
342 		goto out;
343 
344 	ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table[cluster]);
345 	if (ret)
346 		goto out;
347 
348 	clk[cluster] = clk_get(cpu_dev, NULL);
349 	if (!IS_ERR(clk[cluster]))
350 		return 0;
351 
352 	dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d, cluster: %d\n",
353 		__func__, cpu_dev->id, cluster);
354 	ret = PTR_ERR(clk[cluster]);
355 	dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
356 
357 out:
358 	dev_err(cpu_dev, "%s: Failed to get data for cluster: %d\n", __func__,
359 		cluster);
360 	return ret;
361 }
362 
363 static int get_cluster_clk_and_freq_table(struct device *cpu_dev,
364 					  const struct cpumask *cpumask)
365 {
366 	u32 cluster = cpu_to_cluster(cpu_dev->id);
367 	int i, ret;
368 
369 	if (atomic_inc_return(&cluster_usage[cluster]) != 1)
370 		return 0;
371 
372 	if (cluster < MAX_CLUSTERS) {
373 		ret = _get_cluster_clk_and_freq_table(cpu_dev, cpumask);
374 		if (ret)
375 			atomic_dec(&cluster_usage[cluster]);
376 		return ret;
377 	}
378 
379 	/*
380 	 * Get data for all clusters and fill virtual cluster with a merge of
381 	 * both
382 	 */
383 	for_each_present_cpu(i) {
384 		struct device *cdev = get_cpu_device(i);
385 
386 		if (!cdev)
387 			return -ENODEV;
388 
389 		ret = _get_cluster_clk_and_freq_table(cdev, cpumask);
390 		if (ret)
391 			goto put_clusters;
392 	}
393 
394 	ret = merge_cluster_tables();
395 	if (ret)
396 		goto put_clusters;
397 
398 	/* Assuming 2 cluster, set clk_big_min and clk_little_max */
399 	clk_big_min = get_table_min(freq_table[A15_CLUSTER]);
400 	clk_little_max = VIRT_FREQ(A7_CLUSTER,
401 				   get_table_max(freq_table[A7_CLUSTER]));
402 
403 	return 0;
404 
405 put_clusters:
406 	for_each_present_cpu(i) {
407 		struct device *cdev = get_cpu_device(i);
408 
409 		if (!cdev)
410 			return -ENODEV;
411 
412 		_put_cluster_clk_and_freq_table(cdev, cpumask);
413 	}
414 
415 	atomic_dec(&cluster_usage[cluster]);
416 
417 	return ret;
418 }
419 
420 /* Per-CPU initialization */
421 static int ve_spc_cpufreq_init(struct cpufreq_policy *policy)
422 {
423 	u32 cur_cluster = cpu_to_cluster(policy->cpu);
424 	struct device *cpu_dev;
425 	int ret;
426 
427 	cpu_dev = get_cpu_device(policy->cpu);
428 	if (!cpu_dev) {
429 		pr_err("%s: failed to get cpu%d device\n", __func__,
430 		       policy->cpu);
431 		return -ENODEV;
432 	}
433 
434 	if (cur_cluster < MAX_CLUSTERS) {
435 		int cpu;
436 
437 		dev_pm_opp_get_sharing_cpus(cpu_dev, policy->cpus);
438 
439 		for_each_cpu(cpu, policy->cpus)
440 			per_cpu(physical_cluster, cpu) = cur_cluster;
441 	} else {
442 		/* Assumption: during init, we are always running on A15 */
443 		per_cpu(physical_cluster, policy->cpu) = A15_CLUSTER;
444 	}
445 
446 	ret = get_cluster_clk_and_freq_table(cpu_dev, policy->cpus);
447 	if (ret)
448 		return ret;
449 
450 	policy->freq_table = freq_table[cur_cluster];
451 	policy->cpuinfo.transition_latency = 1000000; /* 1 ms */
452 
453 	dev_pm_opp_of_register_em(policy->cpus);
454 
455 	if (is_bL_switching_enabled())
456 		per_cpu(cpu_last_req_freq, policy->cpu) =
457 						clk_get_cpu_rate(policy->cpu);
458 
459 	dev_info(cpu_dev, "%s: CPU %d initialized\n", __func__, policy->cpu);
460 	return 0;
461 }
462 
463 static int ve_spc_cpufreq_exit(struct cpufreq_policy *policy)
464 {
465 	struct device *cpu_dev;
466 	int cur_cluster = cpu_to_cluster(policy->cpu);
467 
468 	if (cur_cluster < MAX_CLUSTERS) {
469 		cpufreq_cooling_unregister(cdev[cur_cluster]);
470 		cdev[cur_cluster] = NULL;
471 	}
472 
473 	cpu_dev = get_cpu_device(policy->cpu);
474 	if (!cpu_dev) {
475 		pr_err("%s: failed to get cpu%d device\n", __func__,
476 		       policy->cpu);
477 		return -ENODEV;
478 	}
479 
480 	put_cluster_clk_and_freq_table(cpu_dev, policy->related_cpus);
481 	return 0;
482 }
483 
484 static void ve_spc_cpufreq_ready(struct cpufreq_policy *policy)
485 {
486 	int cur_cluster = cpu_to_cluster(policy->cpu);
487 
488 	/* Do not register a cpu_cooling device if we are in IKS mode */
489 	if (cur_cluster >= MAX_CLUSTERS)
490 		return;
491 
492 	cdev[cur_cluster] = of_cpufreq_cooling_register(policy);
493 }
494 
495 static struct cpufreq_driver ve_spc_cpufreq_driver = {
496 	.name			= "vexpress-spc",
497 	.flags			= CPUFREQ_STICKY |
498 					CPUFREQ_HAVE_GOVERNOR_PER_POLICY |
499 					CPUFREQ_NEED_INITIAL_FREQ_CHECK,
500 	.verify			= cpufreq_generic_frequency_table_verify,
501 	.target_index		= ve_spc_cpufreq_set_target,
502 	.get			= ve_spc_cpufreq_get_rate,
503 	.init			= ve_spc_cpufreq_init,
504 	.exit			= ve_spc_cpufreq_exit,
505 	.ready			= ve_spc_cpufreq_ready,
506 	.attr			= cpufreq_generic_attr,
507 };
508 
509 #ifdef CONFIG_BL_SWITCHER
510 static int bL_cpufreq_switcher_notifier(struct notifier_block *nfb,
511 					unsigned long action, void *_arg)
512 {
513 	pr_debug("%s: action: %ld\n", __func__, action);
514 
515 	switch (action) {
516 	case BL_NOTIFY_PRE_ENABLE:
517 	case BL_NOTIFY_PRE_DISABLE:
518 		cpufreq_unregister_driver(&ve_spc_cpufreq_driver);
519 		break;
520 
521 	case BL_NOTIFY_POST_ENABLE:
522 		set_switching_enabled(true);
523 		cpufreq_register_driver(&ve_spc_cpufreq_driver);
524 		break;
525 
526 	case BL_NOTIFY_POST_DISABLE:
527 		set_switching_enabled(false);
528 		cpufreq_register_driver(&ve_spc_cpufreq_driver);
529 		break;
530 
531 	default:
532 		return NOTIFY_DONE;
533 	}
534 
535 	return NOTIFY_OK;
536 }
537 
538 static struct notifier_block bL_switcher_notifier = {
539 	.notifier_call = bL_cpufreq_switcher_notifier,
540 };
541 
542 static int __bLs_register_notifier(void)
543 {
544 	return bL_switcher_register_notifier(&bL_switcher_notifier);
545 }
546 
547 static int __bLs_unregister_notifier(void)
548 {
549 	return bL_switcher_unregister_notifier(&bL_switcher_notifier);
550 }
551 #else
552 static int __bLs_register_notifier(void) { return 0; }
553 static int __bLs_unregister_notifier(void) { return 0; }
554 #endif
555 
556 static int ve_spc_cpufreq_probe(struct platform_device *pdev)
557 {
558 	int ret, i;
559 
560 	set_switching_enabled(bL_switcher_get_enabled());
561 
562 	for (i = 0; i < MAX_CLUSTERS; i++)
563 		mutex_init(&cluster_lock[i]);
564 
565 	ret = cpufreq_register_driver(&ve_spc_cpufreq_driver);
566 	if (ret) {
567 		pr_info("%s: Failed registering platform driver: %s, err: %d\n",
568 			__func__, ve_spc_cpufreq_driver.name, ret);
569 	} else {
570 		ret = __bLs_register_notifier();
571 		if (ret)
572 			cpufreq_unregister_driver(&ve_spc_cpufreq_driver);
573 		else
574 			pr_info("%s: Registered platform driver: %s\n",
575 				__func__, ve_spc_cpufreq_driver.name);
576 	}
577 
578 	bL_switcher_put_enabled();
579 	return ret;
580 }
581 
582 static int ve_spc_cpufreq_remove(struct platform_device *pdev)
583 {
584 	bL_switcher_get_enabled();
585 	__bLs_unregister_notifier();
586 	cpufreq_unregister_driver(&ve_spc_cpufreq_driver);
587 	bL_switcher_put_enabled();
588 	pr_info("%s: Un-registered platform driver: %s\n", __func__,
589 		ve_spc_cpufreq_driver.name);
590 	return 0;
591 }
592 
593 static struct platform_driver ve_spc_cpufreq_platdrv = {
594 	.driver = {
595 		.name	= "vexpress-spc-cpufreq",
596 	},
597 	.probe		= ve_spc_cpufreq_probe,
598 	.remove		= ve_spc_cpufreq_remove,
599 };
600 module_platform_driver(ve_spc_cpufreq_platdrv);
601 
602 MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
603 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
604 MODULE_DESCRIPTION("Vexpress SPC ARM big LITTLE cpufreq driver");
605 MODULE_LICENSE("GPL v2");
606