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/arch_topology.h> 15 #include <linux/cacheinfo.h> 16 #include <linux/cpu.h> 17 #include <linux/cpufreq.h> 18 #include <linux/cpumask.h> 19 #include <linux/export.h> 20 #include <linux/init.h> 21 #include <linux/percpu.h> 22 #include <linux/node.h> 23 #include <linux/nodemask.h> 24 #include <linux/of.h> 25 #include <linux/sched.h> 26 #include <linux/sched/topology.h> 27 #include <linux/slab.h> 28 #include <linux/string.h> 29 30 #include <asm/cpu.h> 31 #include <asm/cputype.h> 32 #include <asm/topology.h> 33 34 /* 35 * cpu capacity scale management 36 */ 37 38 /* 39 * cpu capacity table 40 * This per cpu data structure describes the relative capacity of each core. 41 * On a heteregenous system, cores don't have the same computation capacity 42 * and we reflect that difference in the cpu_capacity field so the scheduler 43 * can take this difference into account during load balance. A per cpu 44 * structure is preferred because each CPU updates its own cpu_capacity field 45 * during the load balance except for idle cores. One idle core is selected 46 * to run the sched_balance_domains for all idle cores and the cpu_capacity can be 47 * updated during this sequence. 48 */ 49 50 #ifdef CONFIG_OF 51 struct cpu_efficiency { 52 const char *compatible; 53 unsigned long efficiency; 54 }; 55 56 /* 57 * Table of relative efficiency of each processors 58 * The efficiency value must fit in 20bit and the final 59 * cpu_scale value must be in the range 60 * 0 < cpu_scale < 3*SCHED_CAPACITY_SCALE/2 61 * in order to return at most 1 when DIV_ROUND_CLOSEST 62 * is used to compute the capacity of a CPU. 63 * Processors that are not defined in the table, 64 * use the default SCHED_CAPACITY_SCALE value for cpu_scale. 65 */ 66 static const struct cpu_efficiency table_efficiency[] = { 67 {"arm,cortex-a15", 3891}, 68 {"arm,cortex-a7", 2048}, 69 {NULL, }, 70 }; 71 72 static unsigned long *__cpu_capacity; 73 #define cpu_capacity(cpu) __cpu_capacity[cpu] 74 75 static unsigned long middle_capacity = 1; 76 static bool cap_from_dt = true; 77 78 /* 79 * Iterate all CPUs' descriptor in DT and compute the efficiency 80 * (as per table_efficiency). Also calculate a middle efficiency 81 * as close as possible to (max{eff_i} - min{eff_i}) / 2 82 * This is later used to scale the cpu_capacity field such that an 83 * 'average' CPU is of middle capacity. Also see the comments near 84 * table_efficiency[] and update_cpu_capacity(). 85 */ 86 static void __init parse_dt_topology(void) 87 { 88 const struct cpu_efficiency *cpu_eff; 89 struct device_node *cn = NULL; 90 unsigned long min_capacity = ULONG_MAX; 91 unsigned long max_capacity = 0; 92 unsigned long capacity = 0; 93 int cpu = 0; 94 95 __cpu_capacity = kcalloc(nr_cpu_ids, sizeof(*__cpu_capacity), 96 GFP_NOWAIT); 97 98 for_each_possible_cpu(cpu) { 99 const __be32 *rate; 100 int len; 101 102 /* too early to use cpu->of_node */ 103 cn = of_get_cpu_node(cpu, NULL); 104 if (!cn) { 105 pr_err("missing device node for CPU %d\n", cpu); 106 continue; 107 } 108 109 if (topology_parse_cpu_capacity(cn, cpu)) { 110 of_node_put(cn); 111 continue; 112 } 113 114 cap_from_dt = false; 115 116 for (cpu_eff = table_efficiency; cpu_eff->compatible; cpu_eff++) 117 if (of_device_is_compatible(cn, cpu_eff->compatible)) 118 break; 119 120 if (cpu_eff->compatible == NULL) 121 continue; 122 123 rate = of_get_property(cn, "clock-frequency", &len); 124 if (!rate || len != 4) { 125 pr_err("%pOF missing clock-frequency property\n", cn); 126 continue; 127 } 128 129 capacity = ((be32_to_cpup(rate)) >> 20) * cpu_eff->efficiency; 130 131 /* Save min capacity of the system */ 132 if (capacity < min_capacity) 133 min_capacity = capacity; 134 135 /* Save max capacity of the system */ 136 if (capacity > max_capacity) 137 max_capacity = capacity; 138 139 cpu_capacity(cpu) = capacity; 140 } 141 142 /* If min and max capacities are equals, we bypass the update of the 143 * cpu_scale because all CPUs have the same capacity. Otherwise, we 144 * compute a middle_capacity factor that will ensure that the capacity 145 * of an 'average' CPU of the system will be as close as possible to 146 * SCHED_CAPACITY_SCALE, which is the default value, but with the 147 * constraint explained near table_efficiency[]. 148 */ 149 if (4*max_capacity < (3*(max_capacity + min_capacity))) 150 middle_capacity = (min_capacity + max_capacity) 151 >> (SCHED_CAPACITY_SHIFT+1); 152 else 153 middle_capacity = ((max_capacity / 3) 154 >> (SCHED_CAPACITY_SHIFT-1)) + 1; 155 156 if (cap_from_dt) 157 topology_normalize_cpu_scale(); 158 } 159 160 /* 161 * Look for a customed capacity of a CPU in the cpu_capacity table during the 162 * boot. The update of all CPUs is in O(n^2) for heteregeneous system but the 163 * function returns directly for SMP system. 164 */ 165 static void update_cpu_capacity(unsigned int cpu) 166 { 167 if (!cpu_capacity(cpu) || cap_from_dt) 168 return; 169 170 topology_set_cpu_scale(cpu, cpu_capacity(cpu) / middle_capacity); 171 172 pr_info("CPU%u: update cpu_capacity %lu\n", 173 cpu, topology_get_cpu_scale(cpu)); 174 } 175 176 #else 177 static inline void parse_dt_topology(void) {} 178 static inline void update_cpu_capacity(unsigned int cpuid) {} 179 #endif 180 181 /* 182 * store_cpu_topology is called at boot when only one cpu is running 183 * and with the mutex cpu_hotplug.lock locked, when several cpus have booted, 184 * which prevents simultaneous write access to cpu_topology array 185 */ 186 void store_cpu_topology(unsigned int cpuid) 187 { 188 struct cpu_topology *cpuid_topo = &cpu_topology[cpuid]; 189 unsigned int mpidr; 190 191 if (cpuid_topo->package_id != -1) 192 goto topology_populated; 193 194 mpidr = read_cpuid_mpidr(); 195 196 /* create cpu topology mapping */ 197 if ((mpidr & MPIDR_SMP_BITMASK) == MPIDR_SMP_VALUE) { 198 /* 199 * This is a multiprocessor system 200 * multiprocessor format & multiprocessor mode field are set 201 */ 202 203 if (mpidr & MPIDR_MT_BITMASK) { 204 /* core performance interdependency */ 205 cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0); 206 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1); 207 cpuid_topo->package_id = MPIDR_AFFINITY_LEVEL(mpidr, 2); 208 } else { 209 /* largely independent cores */ 210 cpuid_topo->thread_id = -1; 211 cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0); 212 cpuid_topo->package_id = MPIDR_AFFINITY_LEVEL(mpidr, 1); 213 } 214 } else { 215 /* 216 * This is an uniprocessor system 217 * we are in multiprocessor format but uniprocessor system 218 * or in the old uniprocessor format 219 */ 220 cpuid_topo->thread_id = -1; 221 cpuid_topo->core_id = 0; 222 cpuid_topo->package_id = -1; 223 } 224 225 update_cpu_capacity(cpuid); 226 227 pr_info("CPU%u: thread %d, cpu %d, socket %d, mpidr %x\n", 228 cpuid, cpu_topology[cpuid].thread_id, 229 cpu_topology[cpuid].core_id, 230 cpu_topology[cpuid].package_id, mpidr); 231 232 topology_populated: 233 update_siblings_masks(cpuid); 234 } 235 236 /* 237 * init_cpu_topology is called at boot when only one cpu is running 238 * which prevent simultaneous write access to cpu_topology array 239 */ 240 void __init init_cpu_topology(void) 241 { 242 int cpu, ret; 243 244 reset_cpu_topology(); 245 smp_wmb(); 246 247 parse_dt_topology(); 248 249 for_each_possible_cpu(cpu) { 250 ret = fetch_cache_info(cpu); 251 if (!ret) 252 continue; 253 /* CPUs without a usable CLIDR return -EOPNOTSUPP. */ 254 if (ret != -ENOENT && ret != -EOPNOTSUPP) 255 pr_err("Early cacheinfo failed, ret = %d\n", ret); 256 return; 257 } 258 } 259