topology.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * arch/arm64/kernel/topology.c
  3. *
  4. * Copyright (C) 2011,2013,2014 Linaro Limited.
  5. *
  6. * Based on the arm32 version written by Vincent Guittot in turn based on
  7. * 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/acpi.h>
  14. #include <linux/arch_topology.h>
  15. #include <linux/cacheinfo.h>
  16. #include <linux/cpu.h>
  17. #include <linux/cpumask.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/smp.h>
  27. #include <linux/string.h>
  28. #include <asm/cpu.h>
  29. #include <asm/cputype.h>
  30. #include <asm/topology.h>
  31. static int __init get_cpu_for_node(struct device_node *node)
  32. {
  33. struct device_node *cpu_node;
  34. int cpu;
  35. cpu_node = of_parse_phandle(node, "cpu", 0);
  36. if (!cpu_node)
  37. return -1;
  38. cpu = of_cpu_node_to_id(cpu_node);
  39. if (cpu >= 0)
  40. topology_parse_cpu_capacity(cpu_node, cpu);
  41. else
  42. pr_crit("Unable to find CPU node for %pOF\n", cpu_node);
  43. of_node_put(cpu_node);
  44. return cpu;
  45. }
  46. static int __init parse_core(struct device_node *core, int package_id,
  47. int core_id)
  48. {
  49. char name[10];
  50. bool leaf = true;
  51. int i = 0;
  52. int cpu;
  53. struct device_node *t;
  54. do {
  55. snprintf(name, sizeof(name), "thread%d", i);
  56. t = of_get_child_by_name(core, name);
  57. if (t) {
  58. leaf = false;
  59. cpu = get_cpu_for_node(t);
  60. if (cpu >= 0) {
  61. cpu_topology[cpu].package_id = package_id;
  62. cpu_topology[cpu].core_id = core_id;
  63. cpu_topology[cpu].thread_id = i;
  64. } else {
  65. pr_err("%pOF: Can't get CPU for thread\n",
  66. t);
  67. of_node_put(t);
  68. return -EINVAL;
  69. }
  70. of_node_put(t);
  71. }
  72. i++;
  73. } while (t);
  74. cpu = get_cpu_for_node(core);
  75. if (cpu >= 0) {
  76. if (!leaf) {
  77. pr_err("%pOF: Core has both threads and CPU\n",
  78. core);
  79. return -EINVAL;
  80. }
  81. cpu_topology[cpu].package_id = package_id;
  82. cpu_topology[cpu].core_id = core_id;
  83. } else if (leaf) {
  84. pr_err("%pOF: Can't get CPU for leaf core\n", core);
  85. return -EINVAL;
  86. }
  87. return 0;
  88. }
  89. static int __init parse_cluster(struct device_node *cluster, int depth)
  90. {
  91. char name[10];
  92. bool leaf = true;
  93. bool has_cores = false;
  94. struct device_node *c;
  95. static int package_id __initdata;
  96. int core_id = 0;
  97. int i, ret;
  98. /*
  99. * First check for child clusters; we currently ignore any
  100. * information about the nesting of clusters and present the
  101. * scheduler with a flat list of them.
  102. */
  103. i = 0;
  104. do {
  105. snprintf(name, sizeof(name), "cluster%d", i);
  106. c = of_get_child_by_name(cluster, name);
  107. if (c) {
  108. leaf = false;
  109. ret = parse_cluster(c, depth + 1);
  110. of_node_put(c);
  111. if (ret != 0)
  112. return ret;
  113. }
  114. i++;
  115. } while (c);
  116. /* Now check for cores */
  117. i = 0;
  118. do {
  119. snprintf(name, sizeof(name), "core%d", i);
  120. c = of_get_child_by_name(cluster, name);
  121. if (c) {
  122. has_cores = true;
  123. if (depth == 0) {
  124. pr_err("%pOF: cpu-map children should be clusters\n",
  125. c);
  126. of_node_put(c);
  127. return -EINVAL;
  128. }
  129. if (leaf) {
  130. ret = parse_core(c, package_id, core_id++);
  131. } else {
  132. pr_err("%pOF: Non-leaf cluster with core %s\n",
  133. cluster, name);
  134. ret = -EINVAL;
  135. }
  136. of_node_put(c);
  137. if (ret != 0)
  138. return ret;
  139. }
  140. i++;
  141. } while (c);
  142. if (leaf && !has_cores)
  143. pr_warn("%pOF: empty cluster\n", cluster);
  144. if (leaf)
  145. package_id++;
  146. return 0;
  147. }
  148. static int __init parse_dt_topology(void)
  149. {
  150. struct device_node *cn, *map;
  151. int ret = 0;
  152. int cpu;
  153. cn = of_find_node_by_path("/cpus");
  154. if (!cn) {
  155. pr_err("No CPU information found in DT\n");
  156. return 0;
  157. }
  158. /*
  159. * When topology is provided cpu-map is essentially a root
  160. * cluster with restricted subnodes.
  161. */
  162. map = of_get_child_by_name(cn, "cpu-map");
  163. if (!map)
  164. goto out;
  165. ret = parse_cluster(map, 0);
  166. if (ret != 0)
  167. goto out_map;
  168. topology_normalize_cpu_scale();
  169. /*
  170. * Check that all cores are in the topology; the SMP code will
  171. * only mark cores described in the DT as possible.
  172. */
  173. for_each_possible_cpu(cpu)
  174. if (cpu_topology[cpu].package_id == -1)
  175. ret = -EINVAL;
  176. out_map:
  177. of_node_put(map);
  178. out:
  179. of_node_put(cn);
  180. return ret;
  181. }
  182. /*
  183. * cpu topology table
  184. */
  185. struct cpu_topology cpu_topology[NR_CPUS];
  186. EXPORT_SYMBOL_GPL(cpu_topology);
  187. const struct cpumask *cpu_coregroup_mask(int cpu)
  188. {
  189. const cpumask_t *core_mask = &cpu_topology[cpu].core_sibling;
  190. if (cpu_topology[cpu].llc_id != -1) {
  191. if (cpumask_subset(&cpu_topology[cpu].llc_siblings, core_mask))
  192. core_mask = &cpu_topology[cpu].llc_siblings;
  193. }
  194. return core_mask;
  195. }
  196. static void update_siblings_masks(unsigned int cpuid)
  197. {
  198. struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
  199. int cpu;
  200. /* update core and thread sibling masks */
  201. for_each_possible_cpu(cpu) {
  202. cpu_topo = &cpu_topology[cpu];
  203. if (cpuid_topo->llc_id == cpu_topo->llc_id) {
  204. cpumask_set_cpu(cpu, &cpuid_topo->llc_siblings);
  205. cpumask_set_cpu(cpuid, &cpu_topo->llc_siblings);
  206. }
  207. if (cpuid_topo->package_id != cpu_topo->package_id)
  208. continue;
  209. cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
  210. if (cpu != cpuid)
  211. cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
  212. if (cpuid_topo->core_id != cpu_topo->core_id)
  213. continue;
  214. cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
  215. if (cpu != cpuid)
  216. cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
  217. }
  218. }
  219. void store_cpu_topology(unsigned int cpuid)
  220. {
  221. struct cpu_topology *cpuid_topo = &cpu_topology[cpuid];
  222. u64 mpidr;
  223. if (cpuid_topo->package_id != -1)
  224. goto topology_populated;
  225. mpidr = read_cpuid_mpidr();
  226. /* Uniprocessor systems can rely on default topology values */
  227. if (mpidr & MPIDR_UP_BITMASK)
  228. return;
  229. /* Create cpu topology mapping based on MPIDR. */
  230. if (mpidr & MPIDR_MT_BITMASK) {
  231. /* Multiprocessor system : Multi-threads per core */
  232. cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  233. cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  234. cpuid_topo->package_id = MPIDR_AFFINITY_LEVEL(mpidr, 2) |
  235. MPIDR_AFFINITY_LEVEL(mpidr, 3) << 8;
  236. } else {
  237. /* Multiprocessor system : Single-thread per core */
  238. cpuid_topo->thread_id = -1;
  239. cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  240. cpuid_topo->package_id = MPIDR_AFFINITY_LEVEL(mpidr, 1) |
  241. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 8 |
  242. MPIDR_AFFINITY_LEVEL(mpidr, 3) << 16;
  243. }
  244. pr_debug("CPU%u: cluster %d core %d thread %d mpidr %#016llx\n",
  245. cpuid, cpuid_topo->package_id, cpuid_topo->core_id,
  246. cpuid_topo->thread_id, mpidr);
  247. topology_populated:
  248. update_siblings_masks(cpuid);
  249. }
  250. static void __init reset_cpu_topology(void)
  251. {
  252. unsigned int cpu;
  253. for_each_possible_cpu(cpu) {
  254. struct cpu_topology *cpu_topo = &cpu_topology[cpu];
  255. cpu_topo->thread_id = -1;
  256. cpu_topo->core_id = 0;
  257. cpu_topo->package_id = -1;
  258. cpu_topo->llc_id = -1;
  259. cpumask_clear(&cpu_topo->llc_siblings);
  260. cpumask_set_cpu(cpu, &cpu_topo->llc_siblings);
  261. cpumask_clear(&cpu_topo->core_sibling);
  262. cpumask_set_cpu(cpu, &cpu_topo->core_sibling);
  263. cpumask_clear(&cpu_topo->thread_sibling);
  264. cpumask_set_cpu(cpu, &cpu_topo->thread_sibling);
  265. }
  266. }
  267. #ifdef CONFIG_ACPI
  268. /*
  269. * Propagate the topology information of the processor_topology_node tree to the
  270. * cpu_topology array.
  271. */
  272. static int __init parse_acpi_topology(void)
  273. {
  274. bool is_threaded;
  275. int cpu, topology_id;
  276. is_threaded = read_cpuid_mpidr() & MPIDR_MT_BITMASK;
  277. for_each_possible_cpu(cpu) {
  278. int i, cache_id;
  279. topology_id = find_acpi_cpu_topology(cpu, 0);
  280. if (topology_id < 0)
  281. return topology_id;
  282. if (is_threaded) {
  283. cpu_topology[cpu].thread_id = topology_id;
  284. topology_id = find_acpi_cpu_topology(cpu, 1);
  285. cpu_topology[cpu].core_id = topology_id;
  286. } else {
  287. cpu_topology[cpu].thread_id = -1;
  288. cpu_topology[cpu].core_id = topology_id;
  289. }
  290. topology_id = find_acpi_cpu_topology_package(cpu);
  291. cpu_topology[cpu].package_id = topology_id;
  292. i = acpi_find_last_cache_level(cpu);
  293. if (i > 0) {
  294. /*
  295. * this is the only part of cpu_topology that has
  296. * a direct relationship with the cache topology
  297. */
  298. cache_id = find_acpi_cpu_cache_topology(cpu, i);
  299. if (cache_id > 0)
  300. cpu_topology[cpu].llc_id = cache_id;
  301. }
  302. }
  303. return 0;
  304. }
  305. #else
  306. static inline int __init parse_acpi_topology(void)
  307. {
  308. return -EINVAL;
  309. }
  310. #endif
  311. void __init init_cpu_topology(void)
  312. {
  313. reset_cpu_topology();
  314. /*
  315. * Discard anything that was parsed if we hit an error so we
  316. * don't use partial information.
  317. */
  318. if (!acpi_disabled && parse_acpi_topology())
  319. reset_cpu_topology();
  320. else if (of_have_populated_dt() && parse_dt_topology())
  321. reset_cpu_topology();
  322. }