topology.c 8.6 KB

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