topology.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 power scale management
  27. */
  28. /*
  29. * cpu power 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_power field so the scheduler can
  33. * take this difference into account during load balance. A per cpu structure
  34. * is preferred because each CPU updates its own cpu_power field during the
  35. * load balance except for idle cores. One idle core is selected to run the
  36. * rebalance_domains for all idle cores and the cpu_power can be updated
  37. * during this sequence.
  38. */
  39. static DEFINE_PER_CPU(unsigned long, cpu_scale);
  40. unsigned long arch_scale_freq_power(struct sched_domain *sd, int cpu)
  41. {
  42. return per_cpu(cpu_scale, cpu);
  43. }
  44. static void set_power_scale(unsigned int cpu, unsigned long power)
  45. {
  46. per_cpu(cpu_scale, cpu) = power;
  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_POWER_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_POWER_SCALE value for cpu_scale.
  62. */
  63. struct cpu_efficiency table_efficiency[] = {
  64. {"arm,cortex-a15", 3891},
  65. {"arm,cortex-a7", 2048},
  66. {NULL, },
  67. };
  68. unsigned long *__cpu_capacity;
  69. #define cpu_capacity(cpu) __cpu_capacity[cpu]
  70. 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_power field such that an
  76. * 'average' CPU is of middle power. Also see the comments near
  77. * table_efficiency[] and update_cpu_power().
  78. */
  79. static void __init parse_dt_topology(void)
  80. {
  81. struct cpu_efficiency *cpu_eff;
  82. struct device_node *cn = NULL;
  83. unsigned long min_capacity = (unsigned long)(-1);
  84. unsigned long max_capacity = 0;
  85. unsigned long capacity = 0;
  86. int alloc_size, cpu = 0;
  87. alloc_size = nr_cpu_ids * sizeof(*__cpu_capacity);
  88. __cpu_capacity = kzalloc(alloc_size, 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_POWER_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_POWER_SHIFT+1);
  128. else
  129. middle_capacity = ((max_capacity / 3)
  130. >> (SCHED_POWER_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. void update_cpu_power(unsigned int cpu)
  138. {
  139. if (!cpu_capacity(cpu))
  140. return;
  141. set_power_scale(cpu, cpu_capacity(cpu) / middle_capacity);
  142. printk(KERN_INFO "CPU%u: update cpu_power %lu\n",
  143. cpu, arch_scale_freq_power(NULL, cpu));
  144. }
  145. #else
  146. static inline void parse_dt_topology(void) {}
  147. static inline void update_cpu_power(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. void update_siblings_masks(unsigned int cpuid)
  159. {
  160. struct cputopo_arm *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
  161. int cpu;
  162. /* update core and thread sibling masks */
  163. for_each_possible_cpu(cpu) {
  164. cpu_topo = &cpu_topology[cpu];
  165. if (cpuid_topo->socket_id != cpu_topo->socket_id)
  166. continue;
  167. cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
  168. if (cpu != cpuid)
  169. cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
  170. if (cpuid_topo->core_id != cpu_topo->core_id)
  171. continue;
  172. cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
  173. if (cpu != cpuid)
  174. cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
  175. }
  176. smp_wmb();
  177. }
  178. /*
  179. * store_cpu_topology is called at boot when only one cpu is running
  180. * and with the mutex cpu_hotplug.lock locked, when several cpus have booted,
  181. * which prevents simultaneous write access to cpu_topology array
  182. */
  183. void store_cpu_topology(unsigned int cpuid)
  184. {
  185. struct cputopo_arm *cpuid_topo = &cpu_topology[cpuid];
  186. unsigned int mpidr;
  187. /* If the cpu topology has been already set, just return */
  188. if (cpuid_topo->core_id != -1)
  189. return;
  190. mpidr = read_cpuid_mpidr();
  191. /* create cpu topology mapping */
  192. if ((mpidr & MPIDR_SMP_BITMASK) == MPIDR_SMP_VALUE) {
  193. /*
  194. * This is a multiprocessor system
  195. * multiprocessor format & multiprocessor mode field are set
  196. */
  197. if (mpidr & MPIDR_MT_BITMASK) {
  198. /* core performance interdependency */
  199. cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  200. cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  201. cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 2);
  202. } else {
  203. /* largely independent cores */
  204. cpuid_topo->thread_id = -1;
  205. cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  206. cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  207. }
  208. } else {
  209. /*
  210. * This is an uniprocessor system
  211. * we are in multiprocessor format but uniprocessor system
  212. * or in the old uniprocessor format
  213. */
  214. cpuid_topo->thread_id = -1;
  215. cpuid_topo->core_id = 0;
  216. cpuid_topo->socket_id = -1;
  217. }
  218. update_siblings_masks(cpuid);
  219. update_cpu_power(cpuid);
  220. printk(KERN_INFO "CPU%u: thread %d, cpu %d, socket %d, mpidr %x\n",
  221. cpuid, cpu_topology[cpuid].thread_id,
  222. cpu_topology[cpuid].core_id,
  223. cpu_topology[cpuid].socket_id, mpidr);
  224. }
  225. /*
  226. * init_cpu_topology is called at boot when only one cpu is running
  227. * which prevent simultaneous write access to cpu_topology array
  228. */
  229. void __init init_cpu_topology(void)
  230. {
  231. unsigned int cpu;
  232. /* init core mask and power*/
  233. for_each_possible_cpu(cpu) {
  234. struct cputopo_arm *cpu_topo = &(cpu_topology[cpu]);
  235. cpu_topo->thread_id = -1;
  236. cpu_topo->core_id = -1;
  237. cpu_topo->socket_id = -1;
  238. cpumask_clear(&cpu_topo->core_sibling);
  239. cpumask_clear(&cpu_topo->thread_sibling);
  240. set_power_scale(cpu, SCHED_POWER_SCALE);
  241. }
  242. smp_wmb();
  243. parse_dt_topology();
  244. }