topology.c 9.0 KB

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