Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 1 | /* |
| 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> |
Uwe Kleine-König | 39c8bba | 2014-08-08 08:56:30 +0200 | [diff] [blame] | 27 | #include <linux/module.h> |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 28 | #include <linux/mutex.h> |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 29 | #include <linux/of_platform.h> |
Nishanth Menon | e4db1c7 | 2013-09-19 16:03:52 -0500 | [diff] [blame] | 30 | #include <linux/pm_opp.h> |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 31 | #include <linux/slab.h> |
| 32 | #include <linux/topology.h> |
| 33 | #include <linux/types.h> |
| 34 | |
| 35 | #include "arm_big_little.h" |
| 36 | |
| 37 | /* Currently we support only two clusters */ |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 38 | #define A15_CLUSTER 0 |
| 39 | #define A7_CLUSTER 1 |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 40 | #define MAX_CLUSTERS 2 |
| 41 | |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 42 | #ifdef CONFIG_BL_SWITCHER |
Sudeep Holla | 1473014 | 2015-05-13 13:35:52 +0100 | [diff] [blame] | 43 | #include <asm/bL_switcher.h> |
Nicolas Pitre | 45cac11 | 2013-10-30 15:44:41 -0400 | [diff] [blame] | 44 | static bool bL_switching_enabled; |
| 45 | #define is_bL_switching_enabled() bL_switching_enabled |
| 46 | #define set_switching_enabled(x) (bL_switching_enabled = (x)) |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 47 | #else |
| 48 | #define is_bL_switching_enabled() false |
Nicolas Pitre | 45cac11 | 2013-10-30 15:44:41 -0400 | [diff] [blame] | 49 | #define set_switching_enabled(x) do { } while (0) |
Sudeep Holla | 1473014 | 2015-05-13 13:35:52 +0100 | [diff] [blame] | 50 | #define bL_switch_request(...) do { } while (0) |
| 51 | #define bL_switcher_put_enabled() do { } while (0) |
| 52 | #define bL_switcher_get_enabled() do { } while (0) |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 53 | #endif |
| 54 | |
| 55 | #define ACTUAL_FREQ(cluster, freq) ((cluster == A7_CLUSTER) ? freq << 1 : freq) |
| 56 | #define VIRT_FREQ(cluster, freq) ((cluster == A7_CLUSTER) ? freq >> 1 : freq) |
| 57 | |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 58 | static struct cpufreq_arm_bL_ops *arm_bL_ops; |
| 59 | static struct clk *clk[MAX_CLUSTERS]; |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 60 | static struct cpufreq_frequency_table *freq_table[MAX_CLUSTERS + 1]; |
| 61 | static atomic_t cluster_usage[MAX_CLUSTERS + 1]; |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 62 | |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 63 | static unsigned int clk_big_min; /* (Big) clock frequencies */ |
| 64 | static unsigned int clk_little_max; /* Maximum clock frequency (Little) */ |
| 65 | |
| 66 | static DEFINE_PER_CPU(unsigned int, physical_cluster); |
| 67 | static DEFINE_PER_CPU(unsigned int, cpu_last_req_freq); |
| 68 | |
| 69 | static struct mutex cluster_lock[MAX_CLUSTERS]; |
| 70 | |
| 71 | static inline int raw_cpu_to_cluster(int cpu) |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 72 | { |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 73 | return topology_physical_package_id(cpu); |
| 74 | } |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 75 | |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 76 | static inline int cpu_to_cluster(int cpu) |
| 77 | { |
| 78 | return is_bL_switching_enabled() ? |
| 79 | MAX_CLUSTERS : raw_cpu_to_cluster(cpu); |
| 80 | } |
| 81 | |
| 82 | static unsigned int find_cluster_maxfreq(int cluster) |
| 83 | { |
| 84 | int j; |
| 85 | u32 max_freq = 0, cpu_freq; |
| 86 | |
| 87 | for_each_online_cpu(j) { |
| 88 | cpu_freq = per_cpu(cpu_last_req_freq, j); |
| 89 | |
| 90 | if ((cluster == per_cpu(physical_cluster, j)) && |
| 91 | (max_freq < cpu_freq)) |
| 92 | max_freq = cpu_freq; |
| 93 | } |
| 94 | |
| 95 | pr_debug("%s: cluster: %d, max freq: %d\n", __func__, cluster, |
| 96 | max_freq); |
| 97 | |
| 98 | return max_freq; |
| 99 | } |
| 100 | |
| 101 | static unsigned int clk_get_cpu_rate(unsigned int cpu) |
| 102 | { |
| 103 | u32 cur_cluster = per_cpu(physical_cluster, cpu); |
| 104 | u32 rate = clk_get_rate(clk[cur_cluster]) / 1000; |
| 105 | |
| 106 | /* For switcher we use virtual A7 clock rates */ |
| 107 | if (is_bL_switching_enabled()) |
| 108 | rate = VIRT_FREQ(cur_cluster, rate); |
| 109 | |
| 110 | pr_debug("%s: cpu: %d, cluster: %d, freq: %u\n", __func__, cpu, |
| 111 | cur_cluster, rate); |
| 112 | |
| 113 | return rate; |
| 114 | } |
| 115 | |
| 116 | static unsigned int bL_cpufreq_get_rate(unsigned int cpu) |
| 117 | { |
| 118 | if (is_bL_switching_enabled()) { |
| 119 | pr_debug("%s: freq: %d\n", __func__, per_cpu(cpu_last_req_freq, |
| 120 | cpu)); |
| 121 | |
| 122 | return per_cpu(cpu_last_req_freq, cpu); |
| 123 | } else { |
| 124 | return clk_get_cpu_rate(cpu); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static unsigned int |
| 129 | bL_cpufreq_set_rate(u32 cpu, u32 old_cluster, u32 new_cluster, u32 rate) |
| 130 | { |
| 131 | u32 new_rate, prev_rate; |
| 132 | int ret; |
| 133 | bool bLs = is_bL_switching_enabled(); |
| 134 | |
| 135 | mutex_lock(&cluster_lock[new_cluster]); |
| 136 | |
| 137 | if (bLs) { |
| 138 | prev_rate = per_cpu(cpu_last_req_freq, cpu); |
| 139 | per_cpu(cpu_last_req_freq, cpu) = rate; |
| 140 | per_cpu(physical_cluster, cpu) = new_cluster; |
| 141 | |
| 142 | new_rate = find_cluster_maxfreq(new_cluster); |
| 143 | new_rate = ACTUAL_FREQ(new_cluster, new_rate); |
| 144 | } else { |
| 145 | new_rate = rate; |
| 146 | } |
| 147 | |
| 148 | pr_debug("%s: cpu: %d, old cluster: %d, new cluster: %d, freq: %d\n", |
| 149 | __func__, cpu, old_cluster, new_cluster, new_rate); |
| 150 | |
| 151 | ret = clk_set_rate(clk[new_cluster], new_rate * 1000); |
| 152 | if (WARN_ON(ret)) { |
| 153 | pr_err("clk_set_rate failed: %d, new cluster: %d\n", ret, |
| 154 | new_cluster); |
| 155 | if (bLs) { |
| 156 | per_cpu(cpu_last_req_freq, cpu) = prev_rate; |
| 157 | per_cpu(physical_cluster, cpu) = old_cluster; |
| 158 | } |
| 159 | |
| 160 | mutex_unlock(&cluster_lock[new_cluster]); |
| 161 | |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | mutex_unlock(&cluster_lock[new_cluster]); |
| 166 | |
| 167 | /* Recalc freq for old cluster when switching clusters */ |
| 168 | if (old_cluster != new_cluster) { |
| 169 | pr_debug("%s: cpu: %d, old cluster: %d, new cluster: %d\n", |
| 170 | __func__, cpu, old_cluster, new_cluster); |
| 171 | |
| 172 | /* Switch cluster */ |
| 173 | bL_switch_request(cpu, new_cluster); |
| 174 | |
| 175 | mutex_lock(&cluster_lock[old_cluster]); |
| 176 | |
| 177 | /* Set freq of old cluster if there are cpus left on it */ |
| 178 | new_rate = find_cluster_maxfreq(old_cluster); |
| 179 | new_rate = ACTUAL_FREQ(old_cluster, new_rate); |
| 180 | |
| 181 | if (new_rate) { |
| 182 | pr_debug("%s: Updating rate of old cluster: %d, to freq: %d\n", |
| 183 | __func__, old_cluster, new_rate); |
| 184 | |
| 185 | if (clk_set_rate(clk[old_cluster], new_rate * 1000)) |
| 186 | pr_err("%s: clk_set_rate failed: %d, old cluster: %d\n", |
| 187 | __func__, ret, old_cluster); |
| 188 | } |
| 189 | mutex_unlock(&cluster_lock[old_cluster]); |
| 190 | } |
| 191 | |
Sudeep Holla | 0a95e63 | 2015-04-27 10:51:05 +0100 | [diff] [blame] | 192 | /* |
| 193 | * FIXME: clk_set_rate has to handle the case where clk_change_rate |
| 194 | * can fail due to hardware or firmware issues. Until the clk core |
| 195 | * layer is fixed, we can check here. In most of the cases we will |
| 196 | * be reading only the cached value anyway. This needs to be removed |
| 197 | * once clk core is fixed. |
| 198 | */ |
| 199 | if (bL_cpufreq_get_rate(cpu) != new_rate) |
| 200 | return -EIO; |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 201 | return 0; |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 204 | /* Set clock frequency */ |
| 205 | static int bL_cpufreq_set_target(struct cpufreq_policy *policy, |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 206 | unsigned int index) |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 207 | { |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 208 | u32 cpu = policy->cpu, cur_cluster, new_cluster, actual_cluster; |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 209 | unsigned int freqs_new; |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 210 | |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 211 | cur_cluster = cpu_to_cluster(cpu); |
| 212 | new_cluster = actual_cluster = per_cpu(physical_cluster, cpu); |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 213 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 214 | freqs_new = freq_table[cur_cluster][index].frequency; |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 215 | |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 216 | if (is_bL_switching_enabled()) { |
| 217 | if ((actual_cluster == A15_CLUSTER) && |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 218 | (freqs_new < clk_big_min)) { |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 219 | new_cluster = A7_CLUSTER; |
| 220 | } else if ((actual_cluster == A7_CLUSTER) && |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 221 | (freqs_new > clk_little_max)) { |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 222 | new_cluster = A15_CLUSTER; |
| 223 | } |
| 224 | } |
| 225 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 226 | return bL_cpufreq_set_rate(cpu, actual_cluster, new_cluster, freqs_new); |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 229 | static inline u32 get_table_count(struct cpufreq_frequency_table *table) |
| 230 | { |
| 231 | int count; |
| 232 | |
| 233 | for (count = 0; table[count].frequency != CPUFREQ_TABLE_END; count++) |
| 234 | ; |
| 235 | |
| 236 | return count; |
| 237 | } |
| 238 | |
| 239 | /* get the minimum frequency in the cpufreq_frequency_table */ |
| 240 | static inline u32 get_table_min(struct cpufreq_frequency_table *table) |
| 241 | { |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 242 | struct cpufreq_frequency_table *pos; |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 243 | uint32_t min_freq = ~0; |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 244 | cpufreq_for_each_entry(pos, table) |
| 245 | if (pos->frequency < min_freq) |
| 246 | min_freq = pos->frequency; |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 247 | return min_freq; |
| 248 | } |
| 249 | |
| 250 | /* get the maximum frequency in the cpufreq_frequency_table */ |
| 251 | static inline u32 get_table_max(struct cpufreq_frequency_table *table) |
| 252 | { |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 253 | struct cpufreq_frequency_table *pos; |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 254 | uint32_t max_freq = 0; |
Stratos Karafotis | 041526f | 2014-04-25 23:15:38 +0300 | [diff] [blame] | 255 | cpufreq_for_each_entry(pos, table) |
| 256 | if (pos->frequency > max_freq) |
| 257 | max_freq = pos->frequency; |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 258 | return max_freq; |
| 259 | } |
| 260 | |
| 261 | static int merge_cluster_tables(void) |
| 262 | { |
| 263 | int i, j, k = 0, count = 1; |
| 264 | struct cpufreq_frequency_table *table; |
| 265 | |
| 266 | for (i = 0; i < MAX_CLUSTERS; i++) |
| 267 | count += get_table_count(freq_table[i]); |
| 268 | |
| 269 | table = kzalloc(sizeof(*table) * count, GFP_KERNEL); |
| 270 | if (!table) |
| 271 | return -ENOMEM; |
| 272 | |
| 273 | freq_table[MAX_CLUSTERS] = table; |
| 274 | |
| 275 | /* Add in reverse order to get freqs in increasing order */ |
| 276 | for (i = MAX_CLUSTERS - 1; i >= 0; i--) { |
| 277 | for (j = 0; freq_table[i][j].frequency != CPUFREQ_TABLE_END; |
| 278 | j++) { |
| 279 | table[k].frequency = VIRT_FREQ(i, |
| 280 | freq_table[i][j].frequency); |
| 281 | pr_debug("%s: index: %d, freq: %d\n", __func__, k, |
| 282 | table[k].frequency); |
| 283 | k++; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | table[k].driver_data = k; |
| 288 | table[k].frequency = CPUFREQ_TABLE_END; |
| 289 | |
| 290 | pr_debug("%s: End, table: %p, count: %d\n", __func__, table, k); |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static void _put_cluster_clk_and_freq_table(struct device *cpu_dev) |
| 296 | { |
| 297 | u32 cluster = raw_cpu_to_cluster(cpu_dev->id); |
| 298 | |
| 299 | if (!freq_table[cluster]) |
| 300 | return; |
| 301 | |
| 302 | clk_put(clk[cluster]); |
| 303 | dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]); |
Viresh Kumar | 493b4cd | 2014-11-25 16:04:20 +0530 | [diff] [blame] | 304 | if (arm_bL_ops->free_opp_table) |
| 305 | arm_bL_ops->free_opp_table(cpu_dev); |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 306 | dev_dbg(cpu_dev, "%s: cluster: %d\n", __func__, cluster); |
| 307 | } |
| 308 | |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 309 | static void put_cluster_clk_and_freq_table(struct device *cpu_dev) |
| 310 | { |
| 311 | u32 cluster = cpu_to_cluster(cpu_dev->id); |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 312 | int i; |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 313 | |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 314 | if (atomic_dec_return(&cluster_usage[cluster])) |
| 315 | return; |
| 316 | |
| 317 | if (cluster < MAX_CLUSTERS) |
| 318 | return _put_cluster_clk_and_freq_table(cpu_dev); |
| 319 | |
| 320 | for_each_present_cpu(i) { |
| 321 | struct device *cdev = get_cpu_device(i); |
| 322 | if (!cdev) { |
| 323 | pr_err("%s: failed to get cpu%d device\n", __func__, i); |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | _put_cluster_clk_and_freq_table(cdev); |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 328 | } |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 329 | |
| 330 | /* free virtual table */ |
| 331 | kfree(freq_table[cluster]); |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 334 | static int _get_cluster_clk_and_freq_table(struct device *cpu_dev) |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 335 | { |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 336 | u32 cluster = raw_cpu_to_cluster(cpu_dev->id); |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 337 | int ret; |
| 338 | |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 339 | if (freq_table[cluster]) |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 340 | return 0; |
| 341 | |
| 342 | ret = arm_bL_ops->init_opp_table(cpu_dev); |
| 343 | if (ret) { |
| 344 | dev_err(cpu_dev, "%s: init_opp_table failed, cpu: %d, err: %d\n", |
| 345 | __func__, cpu_dev->id, ret); |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 346 | goto out; |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 349 | ret = dev_pm_opp_init_cpufreq_table(cpu_dev, &freq_table[cluster]); |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 350 | if (ret) { |
| 351 | dev_err(cpu_dev, "%s: failed to init cpufreq table, cpu: %d, err: %d\n", |
| 352 | __func__, cpu_dev->id, ret); |
Viresh Kumar | 493b4cd | 2014-11-25 16:04:20 +0530 | [diff] [blame] | 353 | goto free_opp_table; |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Sudeep Holla | b904f5c | 2015-04-27 10:51:06 +0100 | [diff] [blame] | 356 | clk[cluster] = clk_get(cpu_dev, NULL); |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 357 | if (!IS_ERR(clk[cluster])) { |
| 358 | dev_dbg(cpu_dev, "%s: clk: %p & freq table: %p, cluster: %d\n", |
| 359 | __func__, clk[cluster], freq_table[cluster], |
| 360 | cluster); |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | dev_err(cpu_dev, "%s: Failed to get clk for cpu: %d, cluster: %d\n", |
| 365 | __func__, cpu_dev->id, cluster); |
| 366 | ret = PTR_ERR(clk[cluster]); |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 367 | dev_pm_opp_free_cpufreq_table(cpu_dev, &freq_table[cluster]); |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 368 | |
Viresh Kumar | 493b4cd | 2014-11-25 16:04:20 +0530 | [diff] [blame] | 369 | free_opp_table: |
| 370 | if (arm_bL_ops->free_opp_table) |
| 371 | arm_bL_ops->free_opp_table(cpu_dev); |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 372 | out: |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 373 | dev_err(cpu_dev, "%s: Failed to get data for cluster: %d\n", __func__, |
| 374 | cluster); |
| 375 | return ret; |
| 376 | } |
| 377 | |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 378 | static int get_cluster_clk_and_freq_table(struct device *cpu_dev) |
| 379 | { |
| 380 | u32 cluster = cpu_to_cluster(cpu_dev->id); |
| 381 | int i, ret; |
| 382 | |
| 383 | if (atomic_inc_return(&cluster_usage[cluster]) != 1) |
| 384 | return 0; |
| 385 | |
| 386 | if (cluster < MAX_CLUSTERS) { |
| 387 | ret = _get_cluster_clk_and_freq_table(cpu_dev); |
| 388 | if (ret) |
| 389 | atomic_dec(&cluster_usage[cluster]); |
| 390 | return ret; |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * Get data for all clusters and fill virtual cluster with a merge of |
| 395 | * both |
| 396 | */ |
| 397 | for_each_present_cpu(i) { |
| 398 | struct device *cdev = get_cpu_device(i); |
| 399 | if (!cdev) { |
| 400 | pr_err("%s: failed to get cpu%d device\n", __func__, i); |
| 401 | return -ENODEV; |
| 402 | } |
| 403 | |
| 404 | ret = _get_cluster_clk_and_freq_table(cdev); |
| 405 | if (ret) |
| 406 | goto put_clusters; |
| 407 | } |
| 408 | |
| 409 | ret = merge_cluster_tables(); |
| 410 | if (ret) |
| 411 | goto put_clusters; |
| 412 | |
| 413 | /* Assuming 2 cluster, set clk_big_min and clk_little_max */ |
| 414 | clk_big_min = get_table_min(freq_table[0]); |
| 415 | clk_little_max = VIRT_FREQ(1, get_table_max(freq_table[1])); |
| 416 | |
| 417 | pr_debug("%s: cluster: %d, clk_big_min: %d, clk_little_max: %d\n", |
| 418 | __func__, cluster, clk_big_min, clk_little_max); |
| 419 | |
| 420 | return 0; |
| 421 | |
| 422 | put_clusters: |
| 423 | for_each_present_cpu(i) { |
| 424 | struct device *cdev = get_cpu_device(i); |
| 425 | if (!cdev) { |
| 426 | pr_err("%s: failed to get cpu%d device\n", __func__, i); |
| 427 | return -ENODEV; |
| 428 | } |
| 429 | |
| 430 | _put_cluster_clk_and_freq_table(cdev); |
| 431 | } |
| 432 | |
| 433 | atomic_dec(&cluster_usage[cluster]); |
| 434 | |
| 435 | return ret; |
| 436 | } |
| 437 | |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 438 | /* Per-CPU initialization */ |
| 439 | static int bL_cpufreq_init(struct cpufreq_policy *policy) |
| 440 | { |
| 441 | u32 cur_cluster = cpu_to_cluster(policy->cpu); |
| 442 | struct device *cpu_dev; |
| 443 | int ret; |
| 444 | |
| 445 | cpu_dev = get_cpu_device(policy->cpu); |
| 446 | if (!cpu_dev) { |
| 447 | pr_err("%s: failed to get cpu%d device\n", __func__, |
| 448 | policy->cpu); |
| 449 | return -ENODEV; |
| 450 | } |
| 451 | |
| 452 | ret = get_cluster_clk_and_freq_table(cpu_dev); |
| 453 | if (ret) |
| 454 | return ret; |
| 455 | |
Viresh Kumar | 39b10eb | 2013-09-16 18:56:08 +0530 | [diff] [blame] | 456 | ret = cpufreq_table_validate_and_show(policy, freq_table[cur_cluster]); |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 457 | if (ret) { |
| 458 | dev_err(cpu_dev, "CPU %d, cluster: %d invalid freq table\n", |
| 459 | policy->cpu, cur_cluster); |
| 460 | put_cluster_clk_and_freq_table(cpu_dev); |
| 461 | return ret; |
| 462 | } |
| 463 | |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 464 | if (cur_cluster < MAX_CLUSTERS) { |
viresh kumar | 8f3ba3d | 2014-03-14 12:10:55 +0530 | [diff] [blame] | 465 | int cpu; |
| 466 | |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 467 | cpumask_copy(policy->cpus, topology_core_cpumask(policy->cpu)); |
| 468 | |
viresh kumar | 8f3ba3d | 2014-03-14 12:10:55 +0530 | [diff] [blame] | 469 | for_each_cpu(cpu, policy->cpus) |
| 470 | per_cpu(physical_cluster, cpu) = cur_cluster; |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 471 | } else { |
| 472 | /* Assumption: during init, we are always running on A15 */ |
| 473 | per_cpu(physical_cluster, policy->cpu) = A15_CLUSTER; |
| 474 | } |
| 475 | |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 476 | if (arm_bL_ops->get_transition_latency) |
| 477 | policy->cpuinfo.transition_latency = |
| 478 | arm_bL_ops->get_transition_latency(cpu_dev); |
| 479 | else |
| 480 | policy->cpuinfo.transition_latency = CPUFREQ_ETERNAL; |
| 481 | |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 482 | if (is_bL_switching_enabled()) |
| 483 | per_cpu(cpu_last_req_freq, policy->cpu) = clk_get_cpu_rate(policy->cpu); |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 484 | |
Viresh Kumar | 2b80f31 | 2013-04-29 13:24:48 +0000 | [diff] [blame] | 485 | dev_info(cpu_dev, "%s: CPU %d initialized\n", __func__, policy->cpu); |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static int bL_cpufreq_exit(struct cpufreq_policy *policy) |
| 490 | { |
| 491 | struct device *cpu_dev; |
| 492 | |
| 493 | cpu_dev = get_cpu_device(policy->cpu); |
| 494 | if (!cpu_dev) { |
| 495 | pr_err("%s: failed to get cpu%d device\n", __func__, |
| 496 | policy->cpu); |
| 497 | return -ENODEV; |
| 498 | } |
| 499 | |
| 500 | put_cluster_clk_and_freq_table(cpu_dev); |
| 501 | dev_dbg(cpu_dev, "%s: Exited, cpu: %d\n", __func__, policy->cpu); |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 506 | static struct cpufreq_driver bL_cpufreq_driver = { |
| 507 | .name = "arm-big-little", |
Viresh Kumar | 0b981e7 | 2013-10-02 14:13:18 +0530 | [diff] [blame] | 508 | .flags = CPUFREQ_STICKY | |
Viresh Kumar | ae6b427 | 2013-12-03 11:20:45 +0530 | [diff] [blame] | 509 | CPUFREQ_HAVE_GOVERNOR_PER_POLICY | |
| 510 | CPUFREQ_NEED_INITIAL_FREQ_CHECK, |
Viresh Kumar | 3c75a15 | 2013-10-03 20:27:57 +0530 | [diff] [blame] | 511 | .verify = cpufreq_generic_frequency_table_verify, |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 512 | .target_index = bL_cpufreq_set_target, |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 513 | .get = bL_cpufreq_get_rate, |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 514 | .init = bL_cpufreq_init, |
| 515 | .exit = bL_cpufreq_exit, |
Viresh Kumar | 3c75a15 | 2013-10-03 20:27:57 +0530 | [diff] [blame] | 516 | .attr = cpufreq_generic_attr, |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 517 | }; |
| 518 | |
Sudeep Holla | 1473014 | 2015-05-13 13:35:52 +0100 | [diff] [blame] | 519 | #ifdef CONFIG_BL_SWITCHER |
Nicolas Pitre | 45cac11 | 2013-10-30 15:44:41 -0400 | [diff] [blame] | 520 | static int bL_cpufreq_switcher_notifier(struct notifier_block *nfb, |
| 521 | unsigned long action, void *_arg) |
| 522 | { |
| 523 | pr_debug("%s: action: %ld\n", __func__, action); |
| 524 | |
| 525 | switch (action) { |
| 526 | case BL_NOTIFY_PRE_ENABLE: |
| 527 | case BL_NOTIFY_PRE_DISABLE: |
| 528 | cpufreq_unregister_driver(&bL_cpufreq_driver); |
| 529 | break; |
| 530 | |
| 531 | case BL_NOTIFY_POST_ENABLE: |
| 532 | set_switching_enabled(true); |
| 533 | cpufreq_register_driver(&bL_cpufreq_driver); |
| 534 | break; |
| 535 | |
| 536 | case BL_NOTIFY_POST_DISABLE: |
| 537 | set_switching_enabled(false); |
| 538 | cpufreq_register_driver(&bL_cpufreq_driver); |
| 539 | break; |
| 540 | |
| 541 | default: |
| 542 | return NOTIFY_DONE; |
| 543 | } |
| 544 | |
| 545 | return NOTIFY_OK; |
| 546 | } |
| 547 | |
| 548 | static struct notifier_block bL_switcher_notifier = { |
| 549 | .notifier_call = bL_cpufreq_switcher_notifier, |
| 550 | }; |
| 551 | |
Sudeep Holla | 1473014 | 2015-05-13 13:35:52 +0100 | [diff] [blame] | 552 | static int __bLs_register_notifier(void) |
| 553 | { |
| 554 | return bL_switcher_register_notifier(&bL_switcher_notifier); |
| 555 | } |
| 556 | |
| 557 | static int __bLs_unregister_notifier(void) |
| 558 | { |
| 559 | return bL_switcher_unregister_notifier(&bL_switcher_notifier); |
| 560 | } |
| 561 | #else |
| 562 | static int __bLs_register_notifier(void) { return 0; } |
| 563 | static int __bLs_unregister_notifier(void) { return 0; } |
| 564 | #endif |
| 565 | |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 566 | int bL_cpufreq_register(struct cpufreq_arm_bL_ops *ops) |
| 567 | { |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 568 | int ret, i; |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 569 | |
| 570 | if (arm_bL_ops) { |
| 571 | pr_debug("%s: Already registered: %s, exiting\n", __func__, |
| 572 | arm_bL_ops->name); |
| 573 | return -EBUSY; |
| 574 | } |
| 575 | |
| 576 | if (!ops || !strlen(ops->name) || !ops->init_opp_table) { |
| 577 | pr_err("%s: Invalid arm_bL_ops, exiting\n", __func__); |
| 578 | return -ENODEV; |
| 579 | } |
| 580 | |
| 581 | arm_bL_ops = ops; |
| 582 | |
Sudeep Holla | 1473014 | 2015-05-13 13:35:52 +0100 | [diff] [blame] | 583 | set_switching_enabled(bL_switcher_get_enabled()); |
Nicolas Pitre | 45cac11 | 2013-10-30 15:44:41 -0400 | [diff] [blame] | 584 | |
Viresh Kumar | e79a23c | 2013-10-30 15:44:40 -0400 | [diff] [blame] | 585 | for (i = 0; i < MAX_CLUSTERS; i++) |
| 586 | mutex_init(&cluster_lock[i]); |
| 587 | |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 588 | ret = cpufreq_register_driver(&bL_cpufreq_driver); |
| 589 | if (ret) { |
| 590 | pr_info("%s: Failed registering platform driver: %s, err: %d\n", |
| 591 | __func__, ops->name, ret); |
| 592 | arm_bL_ops = NULL; |
| 593 | } else { |
Sudeep Holla | 1473014 | 2015-05-13 13:35:52 +0100 | [diff] [blame] | 594 | ret = __bLs_register_notifier(); |
Nicolas Pitre | 45cac11 | 2013-10-30 15:44:41 -0400 | [diff] [blame] | 595 | if (ret) { |
| 596 | cpufreq_unregister_driver(&bL_cpufreq_driver); |
| 597 | arm_bL_ops = NULL; |
| 598 | } else { |
| 599 | pr_info("%s: Registered platform driver: %s\n", |
| 600 | __func__, ops->name); |
| 601 | } |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Nicolas Pitre | 45cac11 | 2013-10-30 15:44:41 -0400 | [diff] [blame] | 604 | bL_switcher_put_enabled(); |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 605 | return ret; |
| 606 | } |
| 607 | EXPORT_SYMBOL_GPL(bL_cpufreq_register); |
| 608 | |
| 609 | void bL_cpufreq_unregister(struct cpufreq_arm_bL_ops *ops) |
| 610 | { |
| 611 | if (arm_bL_ops != ops) { |
| 612 | pr_err("%s: Registered with: %s, can't unregister, exiting\n", |
| 613 | __func__, arm_bL_ops->name); |
| 614 | return; |
| 615 | } |
| 616 | |
Nicolas Pitre | 45cac11 | 2013-10-30 15:44:41 -0400 | [diff] [blame] | 617 | bL_switcher_get_enabled(); |
Sudeep Holla | 1473014 | 2015-05-13 13:35:52 +0100 | [diff] [blame] | 618 | __bLs_unregister_notifier(); |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 619 | cpufreq_unregister_driver(&bL_cpufreq_driver); |
Nicolas Pitre | 45cac11 | 2013-10-30 15:44:41 -0400 | [diff] [blame] | 620 | bL_switcher_put_enabled(); |
Viresh Kumar | 8a67f0e | 2013-04-01 12:57:49 +0000 | [diff] [blame] | 621 | pr_info("%s: Un-registered platform driver: %s\n", __func__, |
| 622 | arm_bL_ops->name); |
| 623 | arm_bL_ops = NULL; |
| 624 | } |
| 625 | EXPORT_SYMBOL_GPL(bL_cpufreq_unregister); |
Uwe Kleine-König | 39c8bba | 2014-08-08 08:56:30 +0200 | [diff] [blame] | 626 | |
| 627 | MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>"); |
| 628 | MODULE_DESCRIPTION("Generic ARM big LITTLE cpufreq driver"); |
| 629 | MODULE_LICENSE("GPL v2"); |