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