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