Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 1 | /* |
| 2 | * arch/arm/kernel/topology.c |
| 3 | * |
| 4 | * Copyright (C) 2011 Linaro Limited. |
| 5 | * Written by: Vincent Guittot |
| 6 | * |
| 7 | * based on arch/sh/kernel/topology.c |
| 8 | * |
| 9 | * This file is subject to the terms and conditions of the GNU General Public |
| 10 | * License. See the file "COPYING" in the main directory of this archive |
| 11 | * for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/cpu.h> |
| 15 | #include <linux/cpumask.h> |
Arnd Bergmann | 92bdd3f | 2013-05-31 22:49:22 +0100 | [diff] [blame] | 16 | #include <linux/export.h> |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 17 | #include <linux/init.h> |
| 18 | #include <linux/percpu.h> |
| 19 | #include <linux/node.h> |
| 20 | #include <linux/nodemask.h> |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 21 | #include <linux/of.h> |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 22 | #include <linux/sched.h> |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 23 | #include <linux/slab.h> |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 24 | |
| 25 | #include <asm/cputype.h> |
| 26 | #include <asm/topology.h> |
| 27 | |
Vincent Guittot | 130d9aa | 2012-07-10 14:08:40 +0100 | [diff] [blame] | 28 | /* |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 29 | * cpu capacity scale management |
Vincent Guittot | 130d9aa | 2012-07-10 14:08:40 +0100 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | /* |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 33 | * cpu capacity table |
Vincent Guittot | 130d9aa | 2012-07-10 14:08:40 +0100 | [diff] [blame] | 34 | * This per cpu data structure describes the relative capacity of each core. |
| 35 | * On a heteregenous system, cores don't have the same computation capacity |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 36 | * and we reflect that difference in the cpu_capacity field so the scheduler |
| 37 | * can take this difference into account during load balance. A per cpu |
| 38 | * structure is preferred because each CPU updates its own cpu_capacity field |
| 39 | * during the load balance except for idle cores. One idle core is selected |
| 40 | * to run the rebalance_domains for all idle cores and the cpu_capacity can be |
| 41 | * updated during this sequence. |
Vincent Guittot | 130d9aa | 2012-07-10 14:08:40 +0100 | [diff] [blame] | 42 | */ |
Juri Lelli | d78e13a | 2016-01-07 16:27:33 +0100 | [diff] [blame] | 43 | static DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE; |
Vincent Guittot | 130d9aa | 2012-07-10 14:08:40 +0100 | [diff] [blame] | 44 | |
Morten Rasmussen | 25cea24 | 2015-04-14 16:25:31 +0100 | [diff] [blame] | 45 | unsigned long scale_cpu_capacity(struct sched_domain *sd, int cpu) |
Vincent Guittot | 130d9aa | 2012-07-10 14:08:40 +0100 | [diff] [blame] | 46 | { |
Jon Medhurst | aa9ea84 | 2016-06-02 12:18:08 +0000 | [diff] [blame] | 47 | #ifdef CONFIG_CPU_FREQ |
Dietmar Eggemann | 568913e | 2015-09-23 17:59:55 +0100 | [diff] [blame] | 48 | unsigned long max_freq_scale = cpufreq_scale_max_freq_capacity(cpu); |
| 49 | |
| 50 | return per_cpu(cpu_scale, cpu) * max_freq_scale >> SCHED_CAPACITY_SHIFT; |
| 51 | #else |
Vincent Guittot | 130d9aa | 2012-07-10 14:08:40 +0100 | [diff] [blame] | 52 | return per_cpu(cpu_scale, cpu); |
Dietmar Eggemann | 568913e | 2015-09-23 17:59:55 +0100 | [diff] [blame] | 53 | #endif |
Vincent Guittot | 130d9aa | 2012-07-10 14:08:40 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 56 | static void set_capacity_scale(unsigned int cpu, unsigned long capacity) |
Vincent Guittot | 130d9aa | 2012-07-10 14:08:40 +0100 | [diff] [blame] | 57 | { |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 58 | per_cpu(cpu_scale, cpu) = capacity; |
Vincent Guittot | 130d9aa | 2012-07-10 14:08:40 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Jeevan Shriram | 490837a | 2017-03-01 17:52:49 -0800 | [diff] [blame] | 61 | static int __init get_cpu_for_node(struct device_node *node) |
| 62 | { |
| 63 | struct device_node *cpu_node; |
| 64 | int cpu; |
| 65 | |
| 66 | cpu_node = of_parse_phandle(node, "cpu", 0); |
| 67 | if (!cpu_node) |
| 68 | return -EINVAL; |
| 69 | |
| 70 | for_each_possible_cpu(cpu) { |
| 71 | if (of_get_cpu_node(cpu, NULL) == cpu_node) { |
| 72 | of_node_put(cpu_node); |
| 73 | return cpu; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | pr_crit("Unable to find CPU node for %s\n", cpu_node->full_name); |
| 78 | |
| 79 | of_node_put(cpu_node); |
| 80 | return -EINVAL; |
| 81 | } |
| 82 | |
| 83 | static int __init parse_core(struct device_node *core, int cluster_id, |
| 84 | int core_id) |
| 85 | { |
| 86 | char name[10]; |
| 87 | bool leaf = true; |
| 88 | int i = 0; |
| 89 | int cpu; |
| 90 | struct device_node *t; |
| 91 | |
| 92 | do { |
| 93 | snprintf(name, sizeof(name), "thread%d", i); |
| 94 | t = of_get_child_by_name(core, name); |
| 95 | if (t) { |
| 96 | leaf = false; |
| 97 | cpu = get_cpu_for_node(t); |
| 98 | if (cpu >= 0) { |
| 99 | cpu_topology[cpu].socket_id = cluster_id; |
| 100 | cpu_topology[cpu].core_id = core_id; |
| 101 | cpu_topology[cpu].thread_id = i; |
| 102 | } else { |
| 103 | pr_err("%s: Can't get CPU for thread\n", |
| 104 | t->full_name); |
| 105 | of_node_put(t); |
| 106 | return -EINVAL; |
| 107 | } |
| 108 | of_node_put(t); |
| 109 | } |
| 110 | i++; |
| 111 | } while (t); |
| 112 | |
| 113 | cpu = get_cpu_for_node(core); |
| 114 | if (cpu >= 0) { |
| 115 | if (!leaf) { |
| 116 | pr_err("%s: Core has both threads and CPU\n", |
| 117 | core->full_name); |
| 118 | return -EINVAL; |
| 119 | } |
| 120 | |
| 121 | cpu_topology[cpu].socket_id = cluster_id; |
| 122 | cpu_topology[cpu].core_id = core_id; |
| 123 | } else if (leaf) { |
| 124 | pr_err("%s: Can't get CPU for leaf core\n", core->full_name); |
| 125 | return -EINVAL; |
| 126 | } |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int __init parse_cluster(struct device_node *cluster, int depth) |
| 132 | { |
| 133 | static int cluster_id __initdata; |
| 134 | char name[10]; |
| 135 | bool leaf = true; |
| 136 | bool has_cores = false; |
| 137 | struct device_node *c; |
| 138 | int core_id = 0; |
| 139 | int i, ret; |
| 140 | |
| 141 | /* |
| 142 | * First check for child clusters; we currently ignore any |
| 143 | * information about the nesting of clusters and present the |
| 144 | * scheduler with a flat list of them. |
| 145 | */ |
| 146 | i = 0; |
| 147 | do { |
| 148 | snprintf(name, sizeof(name), "cluster%d", i); |
| 149 | c = of_get_child_by_name(cluster, name); |
| 150 | if (c) { |
| 151 | leaf = false; |
| 152 | ret = parse_cluster(c, depth + 1); |
| 153 | of_node_put(c); |
| 154 | if (ret != 0) |
| 155 | return ret; |
| 156 | } |
| 157 | i++; |
| 158 | } while (c); |
| 159 | |
| 160 | /* Now check for cores */ |
| 161 | i = 0; |
| 162 | do { |
| 163 | snprintf(name, sizeof(name), "core%d", i); |
| 164 | c = of_get_child_by_name(cluster, name); |
| 165 | if (c) { |
| 166 | has_cores = true; |
| 167 | |
| 168 | if (depth == 0) { |
| 169 | pr_err("%s: cpu-map children should be clusters\n", |
| 170 | c->full_name); |
| 171 | of_node_put(c); |
| 172 | return -EINVAL; |
| 173 | } |
| 174 | |
| 175 | if (leaf) { |
| 176 | ret = parse_core(c, cluster_id, core_id++); |
| 177 | } else { |
| 178 | pr_err("%s: Non-leaf cluster with core %s\n", |
| 179 | cluster->full_name, name); |
| 180 | ret = -EINVAL; |
| 181 | } |
| 182 | |
| 183 | of_node_put(c); |
| 184 | if (ret != 0) |
| 185 | return ret; |
| 186 | } |
| 187 | i++; |
| 188 | } while (c); |
| 189 | |
| 190 | if (leaf && !has_cores) |
| 191 | pr_warn("%s: empty cluster\n", cluster->full_name); |
| 192 | |
| 193 | if (leaf) |
| 194 | cluster_id++; |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
Srivatsa Vaddagiri | fd5704a | 2014-03-31 19:42:27 -0700 | [diff] [blame^] | 199 | static DEFINE_PER_CPU(unsigned long, cpu_efficiency) = SCHED_CAPACITY_SCALE; |
| 200 | |
| 201 | unsigned long arch_get_cpu_efficiency(int cpu) |
| 202 | { |
| 203 | return per_cpu(cpu_efficiency, cpu); |
| 204 | } |
| 205 | |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 206 | #ifdef CONFIG_OF |
| 207 | struct cpu_efficiency { |
| 208 | const char *compatible; |
| 209 | unsigned long efficiency; |
| 210 | }; |
| 211 | |
| 212 | /* |
| 213 | * Table of relative efficiency of each processors |
| 214 | * The efficiency value must fit in 20bit and the final |
| 215 | * cpu_scale value must be in the range |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 216 | * 0 < cpu_scale < 3*SCHED_CAPACITY_SCALE/2 |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 217 | * in order to return at most 1 when DIV_ROUND_CLOSEST |
| 218 | * is used to compute the capacity of a CPU. |
| 219 | * Processors that are not defined in the table, |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 220 | * use the default SCHED_CAPACITY_SCALE value for cpu_scale. |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 221 | */ |
Mark Brown | 145bc29 | 2013-12-10 12:10:17 +0100 | [diff] [blame] | 222 | static const struct cpu_efficiency table_efficiency[] = { |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 223 | {"arm,cortex-a15", 3891}, |
| 224 | {"arm,cortex-a7", 2048}, |
| 225 | {NULL, }, |
| 226 | }; |
| 227 | |
Mark Brown | 145bc29 | 2013-12-10 12:10:17 +0100 | [diff] [blame] | 228 | static unsigned long *__cpu_capacity; |
Sudeep KarkadaNagesha | 816a8de | 2013-06-17 14:20:00 +0100 | [diff] [blame] | 229 | #define cpu_capacity(cpu) __cpu_capacity[cpu] |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 230 | |
Mark Brown | 145bc29 | 2013-12-10 12:10:17 +0100 | [diff] [blame] | 231 | static unsigned long middle_capacity = 1; |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 232 | |
| 233 | /* |
| 234 | * Iterate all CPUs' descriptor in DT and compute the efficiency |
| 235 | * (as per table_efficiency). Also calculate a middle efficiency |
| 236 | * as close as possible to (max{eff_i} - min{eff_i}) / 2 |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 237 | * This is later used to scale the cpu_capacity field such that an |
| 238 | * 'average' CPU is of middle capacity. Also see the comments near |
| 239 | * table_efficiency[] and update_cpu_capacity(). |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 240 | */ |
Jeevan Shriram | 490837a | 2017-03-01 17:52:49 -0800 | [diff] [blame] | 241 | static int __init parse_dt_topology(void) |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 242 | { |
Mark Brown | 145bc29 | 2013-12-10 12:10:17 +0100 | [diff] [blame] | 243 | const struct cpu_efficiency *cpu_eff; |
Jeevan Shriram | 490837a | 2017-03-01 17:52:49 -0800 | [diff] [blame] | 244 | struct device_node *cn = NULL, *map; |
Mark Brown | 44ae903 | 2014-03-20 15:16:54 +0100 | [diff] [blame] | 245 | unsigned long min_capacity = ULONG_MAX; |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 246 | unsigned long max_capacity = 0; |
| 247 | unsigned long capacity = 0; |
Jeevan Shriram | 490837a | 2017-03-01 17:52:49 -0800 | [diff] [blame] | 248 | int cpu = 0, ret = 0; |
| 249 | |
Srinivas Ramana | 3ddfb50 | 2016-06-28 12:02:28 +0530 | [diff] [blame] | 250 | __cpu_capacity = kcalloc(nr_cpu_ids, sizeof(*__cpu_capacity), |
| 251 | GFP_NOWAIT); |
| 252 | |
Jeevan Shriram | 490837a | 2017-03-01 17:52:49 -0800 | [diff] [blame] | 253 | cn = of_find_node_by_path("/cpus"); |
| 254 | if (!cn) { |
| 255 | pr_err("No CPU information found in DT\n"); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * When topology is provided cpu-map is essentially a root |
| 261 | * cluster with restricted subnodes. |
| 262 | */ |
| 263 | map = of_get_child_by_name(cn, "cpu-map"); |
| 264 | if (!map) |
| 265 | goto out; |
| 266 | |
| 267 | ret = parse_cluster(map, 0); |
| 268 | if (ret != 0) |
| 269 | goto out_map; |
| 270 | |
| 271 | /* |
| 272 | * Check that all cores are in the topology; the SMP code will |
| 273 | * only mark cores described in the DT as possible. |
| 274 | */ |
| 275 | for_each_possible_cpu(cpu) |
| 276 | if (cpu_topology[cpu].socket_id == -1) |
| 277 | ret = -EINVAL; |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 278 | |
Sudeep KarkadaNagesha | 816a8de | 2013-06-17 14:20:00 +0100 | [diff] [blame] | 279 | for_each_possible_cpu(cpu) { |
| 280 | const u32 *rate; |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 281 | int len; |
| 282 | |
Sudeep KarkadaNagesha | 816a8de | 2013-06-17 14:20:00 +0100 | [diff] [blame] | 283 | /* too early to use cpu->of_node */ |
| 284 | cn = of_get_cpu_node(cpu, NULL); |
| 285 | if (!cn) { |
| 286 | pr_err("missing device node for CPU %d\n", cpu); |
| 287 | continue; |
| 288 | } |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 289 | |
| 290 | for (cpu_eff = table_efficiency; cpu_eff->compatible; cpu_eff++) |
| 291 | if (of_device_is_compatible(cn, cpu_eff->compatible)) |
| 292 | break; |
| 293 | |
| 294 | if (cpu_eff->compatible == NULL) |
| 295 | continue; |
| 296 | |
Srivatsa Vaddagiri | fd5704a | 2014-03-31 19:42:27 -0700 | [diff] [blame^] | 297 | per_cpu(cpu_efficiency, cpu) = cpu_eff->efficiency; |
| 298 | |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 299 | rate = of_get_property(cn, "clock-frequency", &len); |
| 300 | if (!rate || len != 4) { |
| 301 | pr_err("%s missing clock-frequency property\n", |
| 302 | cn->full_name); |
| 303 | continue; |
| 304 | } |
| 305 | |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 306 | capacity = ((be32_to_cpup(rate)) >> 20) * cpu_eff->efficiency; |
| 307 | |
| 308 | /* Save min capacity of the system */ |
| 309 | if (capacity < min_capacity) |
| 310 | min_capacity = capacity; |
| 311 | |
| 312 | /* Save max capacity of the system */ |
| 313 | if (capacity > max_capacity) |
| 314 | max_capacity = capacity; |
| 315 | |
Sudeep KarkadaNagesha | 816a8de | 2013-06-17 14:20:00 +0100 | [diff] [blame] | 316 | cpu_capacity(cpu) = capacity; |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 317 | } |
| 318 | |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 319 | /* If min and max capacities are equals, we bypass the update of the |
| 320 | * cpu_scale because all CPUs have the same capacity. Otherwise, we |
| 321 | * compute a middle_capacity factor that will ensure that the capacity |
| 322 | * of an 'average' CPU of the system will be as close as possible to |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 323 | * SCHED_CAPACITY_SCALE, which is the default value, but with the |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 324 | * constraint explained near table_efficiency[]. |
| 325 | */ |
Sudeep KarkadaNagesha | 816a8de | 2013-06-17 14:20:00 +0100 | [diff] [blame] | 326 | if (4*max_capacity < (3*(max_capacity + min_capacity))) |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 327 | middle_capacity = (min_capacity + max_capacity) |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 328 | >> (SCHED_CAPACITY_SHIFT+1); |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 329 | else |
| 330 | middle_capacity = ((max_capacity / 3) |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 331 | >> (SCHED_CAPACITY_SHIFT-1)) + 1; |
Jeevan Shriram | 490837a | 2017-03-01 17:52:49 -0800 | [diff] [blame] | 332 | out_map: |
| 333 | of_node_put(map); |
| 334 | out: |
| 335 | of_node_put(cn); |
| 336 | return ret; |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Dietmar Eggemann | b4ca4bc | 2015-07-10 13:57:19 +0100 | [diff] [blame] | 339 | static const struct sched_group_energy * const cpu_core_energy(int cpu); |
| 340 | |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 341 | /* |
| 342 | * Look for a customed capacity of a CPU in the cpu_capacity table during the |
| 343 | * boot. The update of all CPUs is in O(n^2) for heteregeneous system but the |
| 344 | * function returns directly for SMP system. |
| 345 | */ |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 346 | static void update_cpu_capacity(unsigned int cpu) |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 347 | { |
Dietmar Eggemann | b4ca4bc | 2015-07-10 13:57:19 +0100 | [diff] [blame] | 348 | unsigned long capacity = SCHED_CAPACITY_SCALE; |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 349 | |
Dietmar Eggemann | b4ca4bc | 2015-07-10 13:57:19 +0100 | [diff] [blame] | 350 | if (cpu_core_energy(cpu)) { |
| 351 | int max_cap_idx = cpu_core_energy(cpu)->nr_cap_states - 1; |
| 352 | capacity = cpu_core_energy(cpu)->cap_states[max_cap_idx].cap; |
| 353 | } |
| 354 | |
| 355 | set_capacity_scale(cpu, capacity); |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 356 | |
Russell King | 4ed89f2 | 2014-10-28 11:26:42 +0000 | [diff] [blame] | 357 | pr_info("CPU%u: update cpu_capacity %lu\n", |
Vincent Guittot | d3bfca1 | 2014-08-26 13:06:48 +0200 | [diff] [blame] | 358 | cpu, arch_scale_cpu_capacity(NULL, cpu)); |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | #else |
Jeevan Shriram | 490837a | 2017-03-01 17:52:49 -0800 | [diff] [blame] | 362 | static inline int parse_dt_topology(void) {} |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 363 | static inline void update_cpu_capacity(unsigned int cpuid) {} |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 364 | #endif |
| 365 | |
Lorenzo Pieralisi | dca463da | 2012-11-15 17:30:32 +0000 | [diff] [blame] | 366 | /* |
Vincent Guittot | 130d9aa | 2012-07-10 14:08:40 +0100 | [diff] [blame] | 367 | * cpu topology table |
| 368 | */ |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 369 | struct cputopo_arm cpu_topology[NR_CPUS]; |
Arnd Bergmann | 92bdd3f | 2013-05-31 22:49:22 +0100 | [diff] [blame] | 370 | EXPORT_SYMBOL_GPL(cpu_topology); |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 371 | |
Vincent Guittot | 4cbd6b1 | 2011-11-29 15:50:20 +0100 | [diff] [blame] | 372 | const struct cpumask *cpu_coregroup_mask(int cpu) |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 373 | { |
| 374 | return &cpu_topology[cpu].core_sibling; |
| 375 | } |
| 376 | |
Vincent Guittot | fb2aa85 | 2014-04-11 11:44:41 +0200 | [diff] [blame] | 377 | /* |
| 378 | * The current assumption is that we can power gate each core independently. |
| 379 | * This will be superseded by DT binding once available. |
| 380 | */ |
| 381 | const struct cpumask *cpu_corepower_mask(int cpu) |
| 382 | { |
| 383 | return &cpu_topology[cpu].thread_sibling; |
| 384 | } |
| 385 | |
Mark Brown | 145bc29 | 2013-12-10 12:10:17 +0100 | [diff] [blame] | 386 | static void update_siblings_masks(unsigned int cpuid) |
Vincent Guittot | cb75dac | 2012-07-10 14:11:11 +0100 | [diff] [blame] | 387 | { |
| 388 | struct cputopo_arm *cpu_topo, *cpuid_topo = &cpu_topology[cpuid]; |
| 389 | int cpu; |
| 390 | |
| 391 | /* update core and thread sibling masks */ |
| 392 | for_each_possible_cpu(cpu) { |
| 393 | cpu_topo = &cpu_topology[cpu]; |
| 394 | |
| 395 | if (cpuid_topo->socket_id != cpu_topo->socket_id) |
| 396 | continue; |
| 397 | |
| 398 | cpumask_set_cpu(cpuid, &cpu_topo->core_sibling); |
| 399 | if (cpu != cpuid) |
| 400 | cpumask_set_cpu(cpu, &cpuid_topo->core_sibling); |
| 401 | |
| 402 | if (cpuid_topo->core_id != cpu_topo->core_id) |
| 403 | continue; |
| 404 | |
| 405 | cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling); |
| 406 | if (cpu != cpuid) |
| 407 | cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling); |
| 408 | } |
Srinivas Ramana | 3ddfb50 | 2016-06-28 12:02:28 +0530 | [diff] [blame] | 409 | |
| 410 | smp_wmb(); /* Ensure mask is updated*/ |
Vincent Guittot | cb75dac | 2012-07-10 14:11:11 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 413 | /* |
| 414 | * store_cpu_topology is called at boot when only one cpu is running |
| 415 | * and with the mutex cpu_hotplug.lock locked, when several cpus have booted, |
| 416 | * which prevents simultaneous write access to cpu_topology array |
| 417 | */ |
| 418 | void store_cpu_topology(unsigned int cpuid) |
| 419 | { |
| 420 | struct cputopo_arm *cpuid_topo = &cpu_topology[cpuid]; |
| 421 | unsigned int mpidr; |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 422 | |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 423 | if (cpuid_topo->core_id != -1) |
Jeevan Shriram | 490837a | 2017-03-01 17:52:49 -0800 | [diff] [blame] | 424 | goto topology_populated; |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 425 | |
| 426 | mpidr = read_cpuid_mpidr(); |
| 427 | |
| 428 | /* create cpu topology mapping */ |
| 429 | if ((mpidr & MPIDR_SMP_BITMASK) == MPIDR_SMP_VALUE) { |
| 430 | /* |
| 431 | * This is a multiprocessor system |
| 432 | * multiprocessor format & multiprocessor mode field are set |
| 433 | */ |
| 434 | |
| 435 | if (mpidr & MPIDR_MT_BITMASK) { |
| 436 | /* core performance interdependency */ |
Lorenzo Pieralisi | 71db5bf | 2012-11-16 15:24:06 +0000 | [diff] [blame] | 437 | cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0); |
| 438 | cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1); |
| 439 | cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 2); |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 440 | } else { |
| 441 | /* largely independent cores */ |
| 442 | cpuid_topo->thread_id = -1; |
Lorenzo Pieralisi | 71db5bf | 2012-11-16 15:24:06 +0000 | [diff] [blame] | 443 | cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0); |
| 444 | cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 1); |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 445 | } |
| 446 | } else { |
| 447 | /* |
| 448 | * This is an uniprocessor system |
| 449 | * we are in multiprocessor format but uniprocessor system |
| 450 | * or in the old uniprocessor format |
| 451 | */ |
| 452 | cpuid_topo->thread_id = -1; |
| 453 | cpuid_topo->core_id = 0; |
| 454 | cpuid_topo->socket_id = -1; |
| 455 | } |
| 456 | |
Jeevan Shriram | 490837a | 2017-03-01 17:52:49 -0800 | [diff] [blame] | 457 | pr_info("CPU%u: thread %d, cpu %d, cluster %d, mpidr %x\n", |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 458 | cpuid, cpu_topology[cpuid].thread_id, |
| 459 | cpu_topology[cpuid].core_id, |
| 460 | cpu_topology[cpuid].socket_id, mpidr); |
Jeevan Shriram | 490837a | 2017-03-01 17:52:49 -0800 | [diff] [blame] | 461 | |
| 462 | topology_populated: |
| 463 | update_siblings_masks(cpuid); |
| 464 | update_cpu_capacity(cpuid); |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 465 | } |
| 466 | |
Dietmar Eggemann | 61100bd | 2014-11-14 17:16:41 +0000 | [diff] [blame] | 467 | /* |
| 468 | * ARM TC2 specific energy cost model data. There are no unit requirements for |
| 469 | * the data. Data can be normalized to any reference point, but the |
| 470 | * normalization must be consistent. That is, one bogo-joule/watt must be the |
| 471 | * same quantity for all data, but we don't care what it is. |
| 472 | */ |
| 473 | static struct idle_state idle_states_cluster_a7[] = { |
| 474 | { .power = 25 }, /* arch_cpu_idle() (active idle) = WFI */ |
| 475 | { .power = 25 }, /* WFI */ |
| 476 | { .power = 10 }, /* cluster-sleep-l */ |
| 477 | }; |
| 478 | |
| 479 | static struct idle_state idle_states_cluster_a15[] = { |
| 480 | { .power = 70 }, /* arch_cpu_idle() (active idle) = WFI */ |
| 481 | { .power = 70 }, /* WFI */ |
| 482 | { .power = 25 }, /* cluster-sleep-b */ |
| 483 | }; |
| 484 | |
| 485 | static struct capacity_state cap_states_cluster_a7[] = { |
| 486 | /* Cluster only power */ |
| 487 | { .cap = 150, .power = 2967, }, /* 350 MHz */ |
| 488 | { .cap = 172, .power = 2792, }, /* 400 MHz */ |
| 489 | { .cap = 215, .power = 2810, }, /* 500 MHz */ |
| 490 | { .cap = 258, .power = 2815, }, /* 600 MHz */ |
| 491 | { .cap = 301, .power = 2919, }, /* 700 MHz */ |
| 492 | { .cap = 344, .power = 2847, }, /* 800 MHz */ |
| 493 | { .cap = 387, .power = 3917, }, /* 900 MHz */ |
| 494 | { .cap = 430, .power = 4905, }, /* 1000 MHz */ |
| 495 | }; |
| 496 | |
| 497 | static struct capacity_state cap_states_cluster_a15[] = { |
| 498 | /* Cluster only power */ |
| 499 | { .cap = 426, .power = 7920, }, /* 500 MHz */ |
| 500 | { .cap = 512, .power = 8165, }, /* 600 MHz */ |
| 501 | { .cap = 597, .power = 8172, }, /* 700 MHz */ |
| 502 | { .cap = 682, .power = 8195, }, /* 800 MHz */ |
| 503 | { .cap = 768, .power = 8265, }, /* 900 MHz */ |
| 504 | { .cap = 853, .power = 8446, }, /* 1000 MHz */ |
| 505 | { .cap = 938, .power = 11426, }, /* 1100 MHz */ |
| 506 | { .cap = 1024, .power = 15200, }, /* 1200 MHz */ |
| 507 | }; |
| 508 | |
| 509 | static struct sched_group_energy energy_cluster_a7 = { |
| 510 | .nr_idle_states = ARRAY_SIZE(idle_states_cluster_a7), |
| 511 | .idle_states = idle_states_cluster_a7, |
| 512 | .nr_cap_states = ARRAY_SIZE(cap_states_cluster_a7), |
| 513 | .cap_states = cap_states_cluster_a7, |
| 514 | }; |
| 515 | |
| 516 | static struct sched_group_energy energy_cluster_a15 = { |
| 517 | .nr_idle_states = ARRAY_SIZE(idle_states_cluster_a15), |
| 518 | .idle_states = idle_states_cluster_a15, |
| 519 | .nr_cap_states = ARRAY_SIZE(cap_states_cluster_a15), |
| 520 | .cap_states = cap_states_cluster_a15, |
| 521 | }; |
| 522 | |
| 523 | static struct idle_state idle_states_core_a7[] = { |
| 524 | { .power = 0 }, /* arch_cpu_idle (active idle) = WFI */ |
| 525 | { .power = 0 }, /* WFI */ |
| 526 | { .power = 0 }, /* cluster-sleep-l */ |
| 527 | }; |
| 528 | |
| 529 | static struct idle_state idle_states_core_a15[] = { |
| 530 | { .power = 0 }, /* arch_cpu_idle (active idle) = WFI */ |
| 531 | { .power = 0 }, /* WFI */ |
| 532 | { .power = 0 }, /* cluster-sleep-b */ |
| 533 | }; |
| 534 | |
| 535 | static struct capacity_state cap_states_core_a7[] = { |
| 536 | /* Power per cpu */ |
| 537 | { .cap = 150, .power = 187, }, /* 350 MHz */ |
| 538 | { .cap = 172, .power = 275, }, /* 400 MHz */ |
| 539 | { .cap = 215, .power = 334, }, /* 500 MHz */ |
| 540 | { .cap = 258, .power = 407, }, /* 600 MHz */ |
| 541 | { .cap = 301, .power = 447, }, /* 700 MHz */ |
| 542 | { .cap = 344, .power = 549, }, /* 800 MHz */ |
| 543 | { .cap = 387, .power = 761, }, /* 900 MHz */ |
| 544 | { .cap = 430, .power = 1024, }, /* 1000 MHz */ |
| 545 | }; |
| 546 | |
| 547 | static struct capacity_state cap_states_core_a15[] = { |
| 548 | /* Power per cpu */ |
| 549 | { .cap = 426, .power = 2021, }, /* 500 MHz */ |
| 550 | { .cap = 512, .power = 2312, }, /* 600 MHz */ |
| 551 | { .cap = 597, .power = 2756, }, /* 700 MHz */ |
| 552 | { .cap = 682, .power = 3125, }, /* 800 MHz */ |
| 553 | { .cap = 768, .power = 3524, }, /* 900 MHz */ |
| 554 | { .cap = 853, .power = 3846, }, /* 1000 MHz */ |
| 555 | { .cap = 938, .power = 5177, }, /* 1100 MHz */ |
| 556 | { .cap = 1024, .power = 6997, }, /* 1200 MHz */ |
| 557 | }; |
| 558 | |
| 559 | static struct sched_group_energy energy_core_a7 = { |
| 560 | .nr_idle_states = ARRAY_SIZE(idle_states_core_a7), |
| 561 | .idle_states = idle_states_core_a7, |
| 562 | .nr_cap_states = ARRAY_SIZE(cap_states_core_a7), |
| 563 | .cap_states = cap_states_core_a7, |
| 564 | }; |
| 565 | |
| 566 | static struct sched_group_energy energy_core_a15 = { |
| 567 | .nr_idle_states = ARRAY_SIZE(idle_states_core_a15), |
| 568 | .idle_states = idle_states_core_a15, |
| 569 | .nr_cap_states = ARRAY_SIZE(cap_states_core_a15), |
| 570 | .cap_states = cap_states_core_a15, |
| 571 | }; |
| 572 | |
| 573 | /* sd energy functions */ |
| 574 | static inline |
| 575 | const struct sched_group_energy * const cpu_cluster_energy(int cpu) |
| 576 | { |
| 577 | return cpu_topology[cpu].socket_id ? &energy_cluster_a7 : |
| 578 | &energy_cluster_a15; |
| 579 | } |
| 580 | |
| 581 | static inline |
| 582 | const struct sched_group_energy * const cpu_core_energy(int cpu) |
| 583 | { |
| 584 | return cpu_topology[cpu].socket_id ? &energy_core_a7 : |
| 585 | &energy_core_a15; |
| 586 | } |
| 587 | |
Guenter Roeck | b6220ad | 2014-06-24 18:05:29 -0700 | [diff] [blame] | 588 | static inline int cpu_corepower_flags(void) |
Vincent Guittot | fb2aa85 | 2014-04-11 11:44:41 +0200 | [diff] [blame] | 589 | { |
Morten Rasmussen | 858d718 | 2015-01-13 13:50:46 +0000 | [diff] [blame] | 590 | return SD_SHARE_PKG_RESOURCES | SD_SHARE_POWERDOMAIN | \ |
| 591 | SD_SHARE_CAP_STATES; |
Vincent Guittot | fb2aa85 | 2014-04-11 11:44:41 +0200 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | static struct sched_domain_topology_level arm_topology[] = { |
| 595 | #ifdef CONFIG_SCHED_MC |
Dietmar Eggemann | 61100bd | 2014-11-14 17:16:41 +0000 | [diff] [blame] | 596 | { cpu_coregroup_mask, cpu_corepower_flags, cpu_core_energy, SD_INIT_NAME(MC) }, |
Vincent Guittot | fb2aa85 | 2014-04-11 11:44:41 +0200 | [diff] [blame] | 597 | #endif |
Dietmar Eggemann | 61100bd | 2014-11-14 17:16:41 +0000 | [diff] [blame] | 598 | { cpu_cpu_mask, NULL, cpu_cluster_energy, SD_INIT_NAME(DIE) }, |
Vincent Guittot | fb2aa85 | 2014-04-11 11:44:41 +0200 | [diff] [blame] | 599 | { NULL, }, |
| 600 | }; |
| 601 | |
Srinivas Ramana | 3ddfb50 | 2016-06-28 12:02:28 +0530 | [diff] [blame] | 602 | static void __init reset_cpu_topology(void) |
| 603 | { |
| 604 | unsigned int cpu; |
| 605 | |
| 606 | for_each_possible_cpu(cpu) { |
| 607 | struct cputopo_arm *cpu_topo = &cpu_topology[cpu]; |
| 608 | |
| 609 | cpu_topo->thread_id = -1; |
| 610 | cpu_topo->core_id = -1; |
| 611 | cpu_topo->socket_id = -1; |
| 612 | |
| 613 | cpumask_clear(&cpu_topo->core_sibling); |
| 614 | cpumask_clear(&cpu_topo->thread_sibling); |
| 615 | } |
| 616 | smp_wmb(); |
| 617 | } |
| 618 | |
| 619 | static void __init reset_cpu_capacity(void) |
| 620 | { |
| 621 | unsigned int cpu; |
| 622 | |
| 623 | for_each_possible_cpu(cpu) |
| 624 | set_capacity_scale(cpu, SCHED_CAPACITY_SCALE); |
| 625 | } |
| 626 | |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 627 | /* |
| 628 | * init_cpu_topology is called at boot when only one cpu is running |
| 629 | * which prevent simultaneous write access to cpu_topology array |
| 630 | */ |
Venkatraman Sathiyamoorthy | f7e416e | 2012-08-03 07:58:33 +0100 | [diff] [blame] | 631 | void __init init_cpu_topology(void) |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 632 | { |
| 633 | unsigned int cpu; |
| 634 | |
Nicolas Pitre | ca8ce3d | 2014-05-26 18:19:39 -0400 | [diff] [blame] | 635 | /* init core mask and capacity */ |
Srinivas Ramana | 3ddfb50 | 2016-06-28 12:02:28 +0530 | [diff] [blame] | 636 | reset_cpu_topology(); |
| 637 | reset_cpu_capacity(); |
| 638 | smp_wmb(); /* Ensure CPU topology and capacity are up to date */ |
Vincent Guittot | 339ca09 | 2012-07-10 14:13:12 +0100 | [diff] [blame] | 639 | |
Jeevan Shriram | 490837a | 2017-03-01 17:52:49 -0800 | [diff] [blame] | 640 | if (parse_dt_topology()) { |
Srinivas Ramana | 3ddfb50 | 2016-06-28 12:02:28 +0530 | [diff] [blame] | 641 | reset_cpu_topology(); |
| 642 | reset_cpu_capacity(); |
Jeevan Shriram | 490837a | 2017-03-01 17:52:49 -0800 | [diff] [blame] | 643 | } |
Vincent Guittot | fb2aa85 | 2014-04-11 11:44:41 +0200 | [diff] [blame] | 644 | |
Srivatsa Vaddagiri | 7cb075f | 2015-04-20 12:35:48 +0530 | [diff] [blame] | 645 | for_each_possible_cpu(cpu) |
| 646 | update_siblings_masks(cpu); |
| 647 | |
Vincent Guittot | fb2aa85 | 2014-04-11 11:44:41 +0200 | [diff] [blame] | 648 | /* Set scheduler topology descriptor */ |
| 649 | set_sched_topology(arm_topology); |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 650 | } |