topology.c 8.7 KB

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