blob: 1d29d1acbda7055a07f3afccbc5ff44ec8994353 [file] [log] [blame]
Viresh Kumar8a67f0e2013-04-01 12:57:49 +00001/*
2 * ARM big.LITTLE Platforms CPUFreq support
3 *
4 * Copyright (C) 2013 ARM Ltd.
5 * Sudeep KarkadaNagesha <sudeep.karkadanagesha@arm.com>
6 *
7 * Copyright (C) 2013 Linaro.
8 * Viresh Kumar <viresh.kumar@linaro.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
15 * kind, whether express or implied; without even the implied warranty
16 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22#include <linux/clk.h>
23#include <linux/cpu.h>
24#include <linux/cpufreq.h>
25#include <linux/cpumask.h>
26#include <linux/export.h>
27#include <linux/of_platform.h>
28#include <linux/opp.h>
29#include <linux/slab.h>
30#include <linux/topology.h>
31#include <linux/types.h>
32
33#include "arm_big_little.h"
34
35/* Currently we support only two clusters */
36#define MAX_CLUSTERS 2
37
38static struct cpufreq_arm_bL_ops *arm_bL_ops;
39static struct clk *clk[MAX_CLUSTERS];
40static struct cpufreq_frequency_table *freq_table[MAX_CLUSTERS];
41static atomic_t cluster_usage[MAX_CLUSTERS] = {ATOMIC_INIT(0), ATOMIC_INIT(0)};
42
43static int cpu_to_cluster(int cpu)
44{
45 return topology_physical_package_id(cpu);
46}
47
48static unsigned int bL_cpufreq_get(unsigned int cpu)
49{
50 u32 cur_cluster = cpu_to_cluster(cpu);
51
52 return clk_get_rate(clk[cur_cluster]) / 1000;
53}
54
55/* Validate policy frequency range */
56static int bL_cpufreq_verify_policy(struct cpufreq_policy *policy)
57{
58 u32 cur_cluster = cpu_to_cluster(policy->cpu);
59
60 return cpufreq_frequency_table_verify(policy, freq_table[cur_cluster]);
61}
62
63/* Set clock frequency */
64static int bL_cpufreq_set_target(struct cpufreq_policy *policy,
65 unsigned int target_freq, unsigned int relation)
66{
67 struct cpufreq_freqs freqs;
68 u32 cpu = policy->cpu, freq_tab_idx, cur_cluster;
69 int ret = 0;
70
71 cur_cluster = cpu_to_cluster(policy->cpu);
72
73 freqs.old = bL_cpufreq_get(policy->cpu);
74
75 /* Determine valid target frequency using freq_table */
76 cpufreq_frequency_table_target(policy, freq_table[cur_cluster],
77 target_freq, relation, &freq_tab_idx);
78 freqs.new = freq_table[cur_cluster][freq_tab_idx].frequency;
79
80 freqs.cpu = policy->cpu;
81
82 pr_debug("%s: cpu: %d, cluster: %d, oldfreq: %d, target freq: %d, new freq: %d\n",
83 __func__, cpu, cur_cluster, freqs.old, target_freq,
84 freqs.new);
85
86 if (freqs.old == freqs.new)
87 return 0;
88
89 for_each_cpu(freqs.cpu, policy->cpus)
90 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
91
92 ret = clk_set_rate(clk[cur_cluster], freqs.new * 1000);
93 if (ret) {
94 pr_err("clk_set_rate failed: %d\n", ret);
95 return ret;
96 }
97
98 policy->cur = freqs.new;
99
100 for_each_cpu(freqs.cpu, policy->cpus)
101 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
102
103 return ret;
104}
105
106static void put_cluster_clk_and_freq_table(struct device *cpu_dev)
107{
108 u32 cluster = cpu_to_cluster(cpu_dev->id);
109
110 if (!atomic_dec_return(&cluster_usage[cluster])) {
111 clk_put(clk[cluster]);
112 opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
113 dev_dbg(cpu_dev, "%s: cluster: %d\n", __func__, cluster);
114 }
115}
116
117static int get_cluster_clk_and_freq_table(struct device *cpu_dev)
118{
119 u32 cluster = cpu_to_cluster(cpu_dev->id);
120 char name[14] = "cpu-cluster.";
121 int ret;
122
123 if (atomic_inc_return(&cluster_usage[cluster]) != 1)
124 return 0;
125
126 ret = arm_bL_ops->init_opp_table(cpu_dev);
127 if (ret) {
128 dev_err(cpu_dev, "%s: init_opp_table failed, cpu: %d, err: %d\n",
129 __func__, cpu_dev->id, ret);
130 goto atomic_dec;
131 }
132
133 ret = opp_init_cpufreq_table(cpu_dev, &freq_table[cluster]);
134 if (ret) {
135 dev_err(cpu_dev, "%s: failed to init cpufreq table, cpu: %d, err: %d\n",
136 __func__, cpu_dev->id, ret);
137 goto atomic_dec;
138 }
139
140 name[12] = cluster + '0';
141 clk[cluster] = clk_get_sys(name, NULL);
142 if (!IS_ERR(clk[cluster])) {
143 dev_dbg(cpu_dev, "%s: clk: %p & freq table: %p, cluster: %d\n",
144 __func__, clk[cluster], freq_table[cluster],
145 cluster);
146 return 0;
147 }
148
149 dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d, cluster: %d\n",
150 __func__, cpu_dev->id, cluster);
151 ret = PTR_ERR(clk[cluster]);
152 opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]);
153
154atomic_dec:
155 atomic_dec(&cluster_usage[cluster]);
156 dev_err(cpu_dev, "%s: Failed to get data for cluster: %d\n", __func__,
157 cluster);
158 return ret;
159}
160
161/* Per-CPU initialization */
162static int bL_cpufreq_init(struct cpufreq_policy *policy)
163{
164 u32 cur_cluster = cpu_to_cluster(policy->cpu);
165 struct device *cpu_dev;
166 int ret;
167
168 cpu_dev = get_cpu_device(policy->cpu);
169 if (!cpu_dev) {
170 pr_err("%s: failed to get cpu%d device\n", __func__,
171 policy->cpu);
172 return -ENODEV;
173 }
174
175 ret = get_cluster_clk_and_freq_table(cpu_dev);
176 if (ret)
177 return ret;
178
179 ret = cpufreq_frequency_table_cpuinfo(policy, freq_table[cur_cluster]);
180 if (ret) {
181 dev_err(cpu_dev, "CPU %d, cluster: %d invalid freq table\n",
182 policy->cpu, cur_cluster);
183 put_cluster_clk_and_freq_table(cpu_dev);
184 return ret;
185 }
186
187 cpufreq_frequency_table_get_attr(freq_table[cur_cluster], policy->cpu);
188
189 if (arm_bL_ops->get_transition_latency)
190 policy->cpuinfo.transition_latency =
191 arm_bL_ops->get_transition_latency(cpu_dev);
192 else
193 policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL;
194
195 policy->cur = bL_cpufreq_get(policy->cpu);
196
197 cpumask_copy(policy->cpus, topology_core_cpumask(policy->cpu));
198
199 dev_info(cpu_dev, "CPU %d initialized\n", policy->cpu);
200 return 0;
201}
202
203static int bL_cpufreq_exit(struct cpufreq_policy *policy)
204{
205 struct device *cpu_dev;
206
207 cpu_dev = get_cpu_device(policy->cpu);
208 if (!cpu_dev) {
209 pr_err("%s: failed to get cpu%d device\n", __func__,
210 policy->cpu);
211 return -ENODEV;
212 }
213
214 put_cluster_clk_and_freq_table(cpu_dev);
215 dev_dbg(cpu_dev, "%s: Exited, cpu: %d\n", __func__, policy->cpu);
216
217 return 0;
218}
219
220/* Export freq_table to sysfs */
221static struct freq_attr *bL_cpufreq_attr[] = {
222 &cpufreq_freq_attr_scaling_available_freqs,
223 NULL,
224};
225
226static struct cpufreq_driver bL_cpufreq_driver = {
227 .name = "arm-big-little",
228 .flags = CPUFREQ_STICKY,
229 .verify = bL_cpufreq_verify_policy,
230 .target = bL_cpufreq_set_target,
231 .get = bL_cpufreq_get,
232 .init = bL_cpufreq_init,
233 .exit = bL_cpufreq_exit,
234 .have_multiple_policies = true,
235 .attr = bL_cpufreq_attr,
236};
237
238int bL_cpufreq_register(struct cpufreq_arm_bL_ops *ops)
239{
240 int ret;
241
242 if (arm_bL_ops) {
243 pr_debug("%s: Already registered: %s, exiting\n", __func__,
244 arm_bL_ops->name);
245 return -EBUSY;
246 }
247
248 if (!ops || !strlen(ops->name) || !ops->init_opp_table) {
249 pr_err("%s: Invalid arm_bL_ops, exiting\n", __func__);
250 return -ENODEV;
251 }
252
253 arm_bL_ops = ops;
254
255 ret = cpufreq_register_driver(&bL_cpufreq_driver);
256 if (ret) {
257 pr_info("%s: Failed registering platform driver: %s, err: %d\n",
258 __func__, ops->name, ret);
259 arm_bL_ops = NULL;
260 } else {
261 pr_info("%s: Registered platform driver: %s\n", __func__,
262 ops->name);
263 }
264
265 return ret;
266}
267EXPORT_SYMBOL_GPL(bL_cpufreq_register);
268
269void bL_cpufreq_unregister(struct cpufreq_arm_bL_ops *ops)
270{
271 if (arm_bL_ops != ops) {
272 pr_err("%s: Registered with: %s, can't unregister, exiting\n",
273 __func__, arm_bL_ops->name);
274 return;
275 }
276
277 cpufreq_unregister_driver(&bL_cpufreq_driver);
278 pr_info("%s: Un-registered platform driver: %s\n", __func__,
279 arm_bL_ops->name);
280 arm_bL_ops = NULL;
281}
282EXPORT_SYMBOL_GPL(bL_cpufreq_unregister);