Mark Brown | f6e763b | 2014-03-04 07:51:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * arch/arm64/kernel/topology.c |
| 3 | * |
| 4 | * Copyright (C) 2011,2013,2014 Linaro Limited. |
| 5 | * |
| 6 | * Based on the arm32 version written by Vincent Guittot in turn based on |
| 7 | * 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> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/percpu.h> |
| 18 | #include <linux/node.h> |
| 19 | #include <linux/nodemask.h> |
Mark Brown | ebdc944 | 2014-05-02 21:38:29 +0100 | [diff] [blame] | 20 | #include <linux/of.h> |
Mark Brown | f6e763b | 2014-03-04 07:51:17 +0000 | [diff] [blame] | 21 | #include <linux/sched.h> |
Robin Randhawa | 4f56999 | 2015-06-09 15:10:00 +0100 | [diff] [blame^] | 22 | #include <linux/sched.h> |
| 23 | #include <linux/sched_energy.h> |
Mark Brown | f6e763b | 2014-03-04 07:51:17 +0000 | [diff] [blame] | 24 | |
Zi Shen Lim | 4e6f708 | 2014-06-07 01:55:27 +0100 | [diff] [blame] | 25 | #include <asm/cputype.h> |
Mark Brown | f6e763b | 2014-03-04 07:51:17 +0000 | [diff] [blame] | 26 | #include <asm/topology.h> |
| 27 | |
Juri Lelli | 5e900d4 | 2015-04-30 11:53:48 +0100 | [diff] [blame] | 28 | static DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE; |
| 29 | |
| 30 | unsigned long scale_cpu_capacity(struct sched_domain *sd, int cpu) |
| 31 | { |
| 32 | return per_cpu(cpu_scale, cpu); |
| 33 | } |
| 34 | |
| 35 | static void set_capacity_scale(unsigned int cpu, unsigned long capacity) |
| 36 | { |
| 37 | per_cpu(cpu_scale, cpu) = capacity; |
| 38 | } |
| 39 | |
Mark Brown | ebdc944 | 2014-05-02 21:38:29 +0100 | [diff] [blame] | 40 | static int __init get_cpu_for_node(struct device_node *node) |
| 41 | { |
| 42 | struct device_node *cpu_node; |
| 43 | int cpu; |
| 44 | |
| 45 | cpu_node = of_parse_phandle(node, "cpu", 0); |
| 46 | if (!cpu_node) |
| 47 | return -1; |
| 48 | |
| 49 | for_each_possible_cpu(cpu) { |
| 50 | if (of_get_cpu_node(cpu, NULL) == cpu_node) { |
| 51 | of_node_put(cpu_node); |
| 52 | return cpu; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | pr_crit("Unable to find CPU node for %s\n", cpu_node->full_name); |
| 57 | |
| 58 | of_node_put(cpu_node); |
| 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | static int __init parse_core(struct device_node *core, int cluster_id, |
| 63 | int core_id) |
| 64 | { |
| 65 | char name[10]; |
| 66 | bool leaf = true; |
| 67 | int i = 0; |
| 68 | int cpu; |
| 69 | struct device_node *t; |
| 70 | |
| 71 | do { |
| 72 | snprintf(name, sizeof(name), "thread%d", i); |
| 73 | t = of_get_child_by_name(core, name); |
| 74 | if (t) { |
| 75 | leaf = false; |
| 76 | cpu = get_cpu_for_node(t); |
| 77 | if (cpu >= 0) { |
| 78 | cpu_topology[cpu].cluster_id = cluster_id; |
| 79 | cpu_topology[cpu].core_id = core_id; |
| 80 | cpu_topology[cpu].thread_id = i; |
| 81 | } else { |
| 82 | pr_err("%s: Can't get CPU for thread\n", |
| 83 | t->full_name); |
| 84 | of_node_put(t); |
| 85 | return -EINVAL; |
| 86 | } |
| 87 | of_node_put(t); |
| 88 | } |
| 89 | i++; |
| 90 | } while (t); |
| 91 | |
| 92 | cpu = get_cpu_for_node(core); |
| 93 | if (cpu >= 0) { |
| 94 | if (!leaf) { |
| 95 | pr_err("%s: Core has both threads and CPU\n", |
| 96 | core->full_name); |
| 97 | return -EINVAL; |
| 98 | } |
| 99 | |
| 100 | cpu_topology[cpu].cluster_id = cluster_id; |
| 101 | cpu_topology[cpu].core_id = core_id; |
| 102 | } else if (leaf) { |
| 103 | pr_err("%s: Can't get CPU for leaf core\n", core->full_name); |
| 104 | return -EINVAL; |
| 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int __init parse_cluster(struct device_node *cluster, int depth) |
| 111 | { |
| 112 | char name[10]; |
| 113 | bool leaf = true; |
| 114 | bool has_cores = false; |
| 115 | struct device_node *c; |
| 116 | static int cluster_id __initdata; |
| 117 | int core_id = 0; |
| 118 | int i, ret; |
| 119 | |
| 120 | /* |
| 121 | * First check for child clusters; we currently ignore any |
| 122 | * information about the nesting of clusters and present the |
| 123 | * scheduler with a flat list of them. |
| 124 | */ |
| 125 | i = 0; |
| 126 | do { |
| 127 | snprintf(name, sizeof(name), "cluster%d", i); |
| 128 | c = of_get_child_by_name(cluster, name); |
| 129 | if (c) { |
| 130 | leaf = false; |
| 131 | ret = parse_cluster(c, depth + 1); |
| 132 | of_node_put(c); |
| 133 | if (ret != 0) |
| 134 | return ret; |
| 135 | } |
| 136 | i++; |
| 137 | } while (c); |
| 138 | |
| 139 | /* Now check for cores */ |
| 140 | i = 0; |
| 141 | do { |
| 142 | snprintf(name, sizeof(name), "core%d", i); |
| 143 | c = of_get_child_by_name(cluster, name); |
| 144 | if (c) { |
| 145 | has_cores = true; |
| 146 | |
| 147 | if (depth == 0) { |
| 148 | pr_err("%s: cpu-map children should be clusters\n", |
| 149 | c->full_name); |
| 150 | of_node_put(c); |
| 151 | return -EINVAL; |
| 152 | } |
| 153 | |
| 154 | if (leaf) { |
| 155 | ret = parse_core(c, cluster_id, core_id++); |
| 156 | } else { |
| 157 | pr_err("%s: Non-leaf cluster with core %s\n", |
| 158 | cluster->full_name, name); |
| 159 | ret = -EINVAL; |
| 160 | } |
| 161 | |
| 162 | of_node_put(c); |
| 163 | if (ret != 0) |
| 164 | return ret; |
| 165 | } |
| 166 | i++; |
| 167 | } while (c); |
| 168 | |
| 169 | if (leaf && !has_cores) |
| 170 | pr_warn("%s: empty cluster\n", cluster->full_name); |
| 171 | |
| 172 | if (leaf) |
| 173 | cluster_id++; |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int __init parse_dt_topology(void) |
| 179 | { |
| 180 | struct device_node *cn, *map; |
| 181 | int ret = 0; |
| 182 | int cpu; |
| 183 | |
| 184 | cn = of_find_node_by_path("/cpus"); |
| 185 | if (!cn) { |
| 186 | pr_err("No CPU information found in DT\n"); |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * When topology is provided cpu-map is essentially a root |
| 192 | * cluster with restricted subnodes. |
| 193 | */ |
| 194 | map = of_get_child_by_name(cn, "cpu-map"); |
| 195 | if (!map) |
| 196 | goto out; |
| 197 | |
| 198 | ret = parse_cluster(map, 0); |
| 199 | if (ret != 0) |
| 200 | goto out_map; |
| 201 | |
| 202 | /* |
| 203 | * Check that all cores are in the topology; the SMP code will |
| 204 | * only mark cores described in the DT as possible. |
| 205 | */ |
Zi Shen Lim | 4e6f708 | 2014-06-07 01:55:27 +0100 | [diff] [blame] | 206 | for_each_possible_cpu(cpu) |
| 207 | if (cpu_topology[cpu].cluster_id == -1) |
Mark Brown | ebdc944 | 2014-05-02 21:38:29 +0100 | [diff] [blame] | 208 | ret = -EINVAL; |
Mark Brown | ebdc944 | 2014-05-02 21:38:29 +0100 | [diff] [blame] | 209 | |
| 210 | out_map: |
| 211 | of_node_put(map); |
| 212 | out: |
| 213 | of_node_put(cn); |
| 214 | return ret; |
| 215 | } |
| 216 | |
Mark Brown | f6e763b | 2014-03-04 07:51:17 +0000 | [diff] [blame] | 217 | /* |
| 218 | * cpu topology table |
| 219 | */ |
| 220 | struct cpu_topology cpu_topology[NR_CPUS]; |
| 221 | EXPORT_SYMBOL_GPL(cpu_topology); |
| 222 | |
Robin Randhawa | 4f56999 | 2015-06-09 15:10:00 +0100 | [diff] [blame^] | 223 | /* sd energy functions */ |
| 224 | static inline |
| 225 | const struct sched_group_energy * const cpu_cluster_energy(int cpu) |
| 226 | { |
| 227 | struct sched_group_energy *sge = sge_array[cpu][SD_LEVEL1]; |
| 228 | |
| 229 | if (!sge) { |
| 230 | pr_warn("Invalid sched_group_energy for Cluster%d\n", cpu); |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | return sge; |
| 235 | } |
| 236 | |
| 237 | static inline |
| 238 | const struct sched_group_energy * const cpu_core_energy(int cpu) |
| 239 | { |
| 240 | struct sched_group_energy *sge = sge_array[cpu][SD_LEVEL0]; |
| 241 | |
| 242 | if (!sge) { |
| 243 | pr_warn("Invalid sched_group_energy for CPU%d\n", cpu); |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | return sge; |
| 248 | } |
| 249 | |
Mark Brown | f6e763b | 2014-03-04 07:51:17 +0000 | [diff] [blame] | 250 | const struct cpumask *cpu_coregroup_mask(int cpu) |
| 251 | { |
| 252 | return &cpu_topology[cpu].core_sibling; |
| 253 | } |
| 254 | |
Juri Lelli | 5e900d4 | 2015-04-30 11:53:48 +0100 | [diff] [blame] | 255 | static inline int cpu_corepower_flags(void) |
| 256 | { |
| 257 | return SD_SHARE_PKG_RESOURCES | SD_SHARE_POWERDOMAIN | \ |
| 258 | SD_SHARE_CAP_STATES; |
| 259 | } |
| 260 | |
| 261 | static struct sched_domain_topology_level arm64_topology[] = { |
| 262 | #ifdef CONFIG_SCHED_MC |
| 263 | { cpu_coregroup_mask, cpu_corepower_flags, cpu_core_energy, SD_INIT_NAME(MC) }, |
| 264 | #endif |
| 265 | { cpu_cpu_mask, NULL, cpu_cluster_energy, SD_INIT_NAME(DIE) }, |
| 266 | { NULL, }, |
| 267 | }; |
| 268 | |
| 269 | static void update_cpu_capacity(unsigned int cpu) |
| 270 | { |
| 271 | unsigned long capacity = SCHED_CAPACITY_SCALE; |
| 272 | |
| 273 | if (cpu_core_energy(cpu)) { |
| 274 | int max_cap_idx = cpu_core_energy(cpu)->nr_cap_states - 1; |
| 275 | capacity = cpu_core_energy(cpu)->cap_states[max_cap_idx].cap; |
| 276 | } |
| 277 | |
| 278 | set_capacity_scale(cpu, capacity); |
| 279 | |
| 280 | pr_info("CPU%d: update cpu_capacity %lu\n", |
| 281 | cpu, arch_scale_cpu_capacity(NULL, cpu)); |
| 282 | } |
| 283 | |
Mark Brown | f6e763b | 2014-03-04 07:51:17 +0000 | [diff] [blame] | 284 | static void update_siblings_masks(unsigned int cpuid) |
| 285 | { |
| 286 | struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid]; |
| 287 | int cpu; |
| 288 | |
Mark Brown | f6e763b | 2014-03-04 07:51:17 +0000 | [diff] [blame] | 289 | /* update core and thread sibling masks */ |
| 290 | for_each_possible_cpu(cpu) { |
| 291 | cpu_topo = &cpu_topology[cpu]; |
| 292 | |
| 293 | if (cpuid_topo->cluster_id != cpu_topo->cluster_id) |
| 294 | continue; |
| 295 | |
| 296 | cpumask_set_cpu(cpuid, &cpu_topo->core_sibling); |
| 297 | if (cpu != cpuid) |
| 298 | cpumask_set_cpu(cpu, &cpuid_topo->core_sibling); |
| 299 | |
| 300 | if (cpuid_topo->core_id != cpu_topo->core_id) |
| 301 | continue; |
| 302 | |
| 303 | cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling); |
| 304 | if (cpu != cpuid) |
| 305 | cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void store_cpu_topology(unsigned int cpuid) |
| 310 | { |
Zi Shen Lim | 4e6f708 | 2014-06-07 01:55:27 +0100 | [diff] [blame] | 311 | struct cpu_topology *cpuid_topo = &cpu_topology[cpuid]; |
| 312 | u64 mpidr; |
| 313 | |
| 314 | if (cpuid_topo->cluster_id != -1) |
| 315 | goto topology_populated; |
| 316 | |
| 317 | mpidr = read_cpuid_mpidr(); |
| 318 | |
| 319 | /* Uniprocessor systems can rely on default topology values */ |
| 320 | if (mpidr & MPIDR_UP_BITMASK) |
| 321 | return; |
| 322 | |
| 323 | /* Create cpu topology mapping based on MPIDR. */ |
| 324 | if (mpidr & MPIDR_MT_BITMASK) { |
| 325 | /* Multiprocessor system : Multi-threads per core */ |
| 326 | cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0); |
| 327 | cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1); |
Mark Brown | 1cefdae | 2014-11-21 00:36:49 +0000 | [diff] [blame] | 328 | cpuid_topo->cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 2) | |
| 329 | MPIDR_AFFINITY_LEVEL(mpidr, 3) << 8; |
Zi Shen Lim | 4e6f708 | 2014-06-07 01:55:27 +0100 | [diff] [blame] | 330 | } else { |
| 331 | /* Multiprocessor system : Single-thread per core */ |
| 332 | cpuid_topo->thread_id = -1; |
| 333 | cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0); |
Mark Brown | 1cefdae | 2014-11-21 00:36:49 +0000 | [diff] [blame] | 334 | cpuid_topo->cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 1) | |
| 335 | MPIDR_AFFINITY_LEVEL(mpidr, 2) << 8 | |
| 336 | MPIDR_AFFINITY_LEVEL(mpidr, 3) << 16; |
Zi Shen Lim | 4e6f708 | 2014-06-07 01:55:27 +0100 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | pr_debug("CPU%u: cluster %d core %d thread %d mpidr %#016llx\n", |
| 340 | cpuid, cpuid_topo->cluster_id, cpuid_topo->core_id, |
| 341 | cpuid_topo->thread_id, mpidr); |
| 342 | |
| 343 | topology_populated: |
Mark Brown | f6e763b | 2014-03-04 07:51:17 +0000 | [diff] [blame] | 344 | update_siblings_masks(cpuid); |
Juri Lelli | 5e900d4 | 2015-04-30 11:53:48 +0100 | [diff] [blame] | 345 | update_cpu_capacity(cpuid); |
Mark Brown | f6e763b | 2014-03-04 07:51:17 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Mark Brown | ebdc944 | 2014-05-02 21:38:29 +0100 | [diff] [blame] | 348 | static void __init reset_cpu_topology(void) |
Mark Brown | f6e763b | 2014-03-04 07:51:17 +0000 | [diff] [blame] | 349 | { |
| 350 | unsigned int cpu; |
| 351 | |
Mark Brown | f6e763b | 2014-03-04 07:51:17 +0000 | [diff] [blame] | 352 | for_each_possible_cpu(cpu) { |
| 353 | struct cpu_topology *cpu_topo = &cpu_topology[cpu]; |
| 354 | |
| 355 | cpu_topo->thread_id = -1; |
Mark Brown | c31bf04 | 2014-05-02 21:38:28 +0100 | [diff] [blame] | 356 | cpu_topo->core_id = 0; |
Mark Brown | f6e763b | 2014-03-04 07:51:17 +0000 | [diff] [blame] | 357 | cpu_topo->cluster_id = -1; |
Mark Brown | c31bf04 | 2014-05-02 21:38:28 +0100 | [diff] [blame] | 358 | |
Mark Brown | f6e763b | 2014-03-04 07:51:17 +0000 | [diff] [blame] | 359 | cpumask_clear(&cpu_topo->core_sibling); |
Mark Brown | c31bf04 | 2014-05-02 21:38:28 +0100 | [diff] [blame] | 360 | cpumask_set_cpu(cpu, &cpu_topo->core_sibling); |
Mark Brown | f6e763b | 2014-03-04 07:51:17 +0000 | [diff] [blame] | 361 | cpumask_clear(&cpu_topo->thread_sibling); |
Mark Brown | c31bf04 | 2014-05-02 21:38:28 +0100 | [diff] [blame] | 362 | cpumask_set_cpu(cpu, &cpu_topo->thread_sibling); |
Mark Brown | f6e763b | 2014-03-04 07:51:17 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
Mark Brown | ebdc944 | 2014-05-02 21:38:29 +0100 | [diff] [blame] | 365 | |
| 366 | void __init init_cpu_topology(void) |
| 367 | { |
| 368 | reset_cpu_topology(); |
| 369 | |
| 370 | /* |
| 371 | * Discard anything that was parsed if we hit an error so we |
| 372 | * don't use partial information. |
| 373 | */ |
Sudeep Holla | e094d44 | 2015-07-23 18:28:26 +0100 | [diff] [blame] | 374 | if (of_have_populated_dt() && parse_dt_topology()) |
Mark Brown | ebdc944 | 2014-05-02 21:38:29 +0100 | [diff] [blame] | 375 | reset_cpu_topology(); |
Robin Randhawa | 4f56999 | 2015-06-09 15:10:00 +0100 | [diff] [blame^] | 376 | else |
| 377 | set_sched_topology(arm64_topology); |
| 378 | |
| 379 | init_sched_energy_costs(); |
Mark Brown | ebdc944 | 2014-05-02 21:38:29 +0100 | [diff] [blame] | 380 | } |