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