topology.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. struct cpu_capacity {
  69. unsigned long hwid;
  70. unsigned long capacity;
  71. };
  72. struct cpu_capacity *cpu_capacity;
  73. unsigned long middle_capacity = 1;
  74. /*
  75. * Iterate all CPUs' descriptor in DT and compute the efficiency
  76. * (as per table_efficiency). Also calculate a middle efficiency
  77. * as close as possible to (max{eff_i} - min{eff_i}) / 2
  78. * This is later used to scale the cpu_power field such that an
  79. * 'average' CPU is of middle power. Also see the comments near
  80. * table_efficiency[] and update_cpu_power().
  81. */
  82. static void __init parse_dt_topology(void)
  83. {
  84. struct cpu_efficiency *cpu_eff;
  85. struct device_node *cn = NULL;
  86. unsigned long min_capacity = (unsigned long)(-1);
  87. unsigned long max_capacity = 0;
  88. unsigned long capacity = 0;
  89. int alloc_size, cpu = 0;
  90. alloc_size = nr_cpu_ids * sizeof(struct cpu_capacity);
  91. cpu_capacity = kzalloc(alloc_size, GFP_NOWAIT);
  92. while ((cn = of_find_node_by_type(cn, "cpu"))) {
  93. const u32 *rate, *reg;
  94. int len;
  95. if (cpu >= num_possible_cpus())
  96. break;
  97. for (cpu_eff = table_efficiency; cpu_eff->compatible; cpu_eff++)
  98. if (of_device_is_compatible(cn, cpu_eff->compatible))
  99. break;
  100. if (cpu_eff->compatible == NULL)
  101. continue;
  102. rate = of_get_property(cn, "clock-frequency", &len);
  103. if (!rate || len != 4) {
  104. pr_err("%s missing clock-frequency property\n",
  105. cn->full_name);
  106. continue;
  107. }
  108. reg = of_get_property(cn, "reg", &len);
  109. if (!reg || len != 4) {
  110. pr_err("%s missing reg property\n", cn->full_name);
  111. continue;
  112. }
  113. capacity = ((be32_to_cpup(rate)) >> 20) * cpu_eff->efficiency;
  114. /* Save min capacity of the system */
  115. if (capacity < min_capacity)
  116. min_capacity = capacity;
  117. /* Save max capacity of the system */
  118. if (capacity > max_capacity)
  119. max_capacity = capacity;
  120. cpu_capacity[cpu].capacity = capacity;
  121. cpu_capacity[cpu++].hwid = be32_to_cpup(reg);
  122. }
  123. if (cpu < num_possible_cpus())
  124. cpu_capacity[cpu].hwid = (unsigned long)(-1);
  125. /* If min and max capacities are equals, we bypass the update of the
  126. * cpu_scale because all CPUs have the same capacity. Otherwise, we
  127. * compute a middle_capacity factor that will ensure that the capacity
  128. * of an 'average' CPU of the system will be as close as possible to
  129. * SCHED_POWER_SCALE, which is the default value, but with the
  130. * constraint explained near table_efficiency[].
  131. */
  132. if (min_capacity == max_capacity)
  133. cpu_capacity[0].hwid = (unsigned long)(-1);
  134. else if (4*max_capacity < (3*(max_capacity + min_capacity)))
  135. middle_capacity = (min_capacity + max_capacity)
  136. >> (SCHED_POWER_SHIFT+1);
  137. else
  138. middle_capacity = ((max_capacity / 3)
  139. >> (SCHED_POWER_SHIFT-1)) + 1;
  140. }
  141. /*
  142. * Look for a customed capacity of a CPU in the cpu_capacity table during the
  143. * boot. The update of all CPUs is in O(n^2) for heteregeneous system but the
  144. * function returns directly for SMP system.
  145. */
  146. void update_cpu_power(unsigned int cpu, unsigned long hwid)
  147. {
  148. unsigned int idx = 0;
  149. /* look for the cpu's hwid in the cpu capacity table */
  150. for (idx = 0; idx < num_possible_cpus(); idx++) {
  151. if (cpu_capacity[idx].hwid == hwid)
  152. break;
  153. if (cpu_capacity[idx].hwid == -1)
  154. return;
  155. }
  156. if (idx == num_possible_cpus())
  157. return;
  158. set_power_scale(cpu, cpu_capacity[idx].capacity / middle_capacity);
  159. printk(KERN_INFO "CPU%u: update cpu_power %lu\n",
  160. cpu, arch_scale_freq_power(NULL, cpu));
  161. }
  162. #else
  163. static inline void parse_dt_topology(void) {}
  164. static inline void update_cpu_power(unsigned int cpuid, unsigned int mpidr) {}
  165. #endif
  166. /*
  167. * cpu topology table
  168. */
  169. struct cputopo_arm cpu_topology[NR_CPUS];
  170. EXPORT_SYMBOL_GPL(cpu_topology);
  171. const struct cpumask *cpu_coregroup_mask(int cpu)
  172. {
  173. return &cpu_topology[cpu].core_sibling;
  174. }
  175. void update_siblings_masks(unsigned int cpuid)
  176. {
  177. struct cputopo_arm *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
  178. int cpu;
  179. /* update core and thread sibling masks */
  180. for_each_possible_cpu(cpu) {
  181. cpu_topo = &cpu_topology[cpu];
  182. if (cpuid_topo->socket_id != cpu_topo->socket_id)
  183. continue;
  184. cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
  185. if (cpu != cpuid)
  186. cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
  187. if (cpuid_topo->core_id != cpu_topo->core_id)
  188. continue;
  189. cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
  190. if (cpu != cpuid)
  191. cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
  192. }
  193. smp_wmb();
  194. }
  195. /*
  196. * store_cpu_topology is called at boot when only one cpu is running
  197. * and with the mutex cpu_hotplug.lock locked, when several cpus have booted,
  198. * which prevents simultaneous write access to cpu_topology array
  199. */
  200. void store_cpu_topology(unsigned int cpuid)
  201. {
  202. struct cputopo_arm *cpuid_topo = &cpu_topology[cpuid];
  203. unsigned int mpidr;
  204. /* If the cpu topology has been already set, just return */
  205. if (cpuid_topo->core_id != -1)
  206. return;
  207. mpidr = read_cpuid_mpidr();
  208. /* create cpu topology mapping */
  209. if ((mpidr & MPIDR_SMP_BITMASK) == MPIDR_SMP_VALUE) {
  210. /*
  211. * This is a multiprocessor system
  212. * multiprocessor format & multiprocessor mode field are set
  213. */
  214. if (mpidr & MPIDR_MT_BITMASK) {
  215. /* core performance interdependency */
  216. cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  217. cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  218. cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 2);
  219. } else {
  220. /* largely independent cores */
  221. cpuid_topo->thread_id = -1;
  222. cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  223. cpuid_topo->socket_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  224. }
  225. } else {
  226. /*
  227. * This is an uniprocessor system
  228. * we are in multiprocessor format but uniprocessor system
  229. * or in the old uniprocessor format
  230. */
  231. cpuid_topo->thread_id = -1;
  232. cpuid_topo->core_id = 0;
  233. cpuid_topo->socket_id = -1;
  234. }
  235. update_siblings_masks(cpuid);
  236. update_cpu_power(cpuid, mpidr & MPIDR_HWID_BITMASK);
  237. printk(KERN_INFO "CPU%u: thread %d, cpu %d, socket %d, mpidr %x\n",
  238. cpuid, cpu_topology[cpuid].thread_id,
  239. cpu_topology[cpuid].core_id,
  240. cpu_topology[cpuid].socket_id, mpidr);
  241. }
  242. /*
  243. * init_cpu_topology is called at boot when only one cpu is running
  244. * which prevent simultaneous write access to cpu_topology array
  245. */
  246. void __init init_cpu_topology(void)
  247. {
  248. unsigned int cpu;
  249. /* init core mask and power*/
  250. for_each_possible_cpu(cpu) {
  251. struct cputopo_arm *cpu_topo = &(cpu_topology[cpu]);
  252. cpu_topo->thread_id = -1;
  253. cpu_topo->core_id = -1;
  254. cpu_topo->socket_id = -1;
  255. cpumask_clear(&cpu_topo->core_sibling);
  256. cpumask_clear(&cpu_topo->thread_sibling);
  257. set_power_scale(cpu, SCHED_POWER_SCALE);
  258. }
  259. smp_wmb();
  260. parse_dt_topology();
  261. }