topology.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 = cpumask_of_node(cpu_to_node(cpu));
  190. /* Find the smaller of NUMA, core or LLC siblings */
  191. if (cpumask_subset(&cpu_topology[cpu].core_sibling, core_mask)) {
  192. /* not numa in package, lets use the package siblings */
  193. core_mask = &cpu_topology[cpu].core_sibling;
  194. }
  195. if (cpu_topology[cpu].llc_id != -1) {
  196. if (cpumask_subset(&cpu_topology[cpu].llc_sibling, core_mask))
  197. core_mask = &cpu_topology[cpu].llc_sibling;
  198. }
  199. return core_mask;
  200. }
  201. static void update_siblings_masks(unsigned int cpuid)
  202. {
  203. struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
  204. int cpu;
  205. /* update core and thread sibling masks */
  206. for_each_online_cpu(cpu) {
  207. cpu_topo = &cpu_topology[cpu];
  208. if (cpuid_topo->llc_id == cpu_topo->llc_id) {
  209. cpumask_set_cpu(cpu, &cpuid_topo->llc_sibling);
  210. cpumask_set_cpu(cpuid, &cpu_topo->llc_sibling);
  211. }
  212. if (cpuid_topo->package_id != cpu_topo->package_id)
  213. continue;
  214. cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
  215. cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
  216. if (cpuid_topo->core_id != cpu_topo->core_id)
  217. continue;
  218. cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
  219. cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
  220. }
  221. }
  222. void store_cpu_topology(unsigned int cpuid)
  223. {
  224. struct cpu_topology *cpuid_topo = &cpu_topology[cpuid];
  225. u64 mpidr;
  226. if (cpuid_topo->package_id != -1)
  227. goto topology_populated;
  228. mpidr = read_cpuid_mpidr();
  229. /* Uniprocessor systems can rely on default topology values */
  230. if (mpidr & MPIDR_UP_BITMASK)
  231. return;
  232. /* Create cpu topology mapping based on MPIDR. */
  233. if (mpidr & MPIDR_MT_BITMASK) {
  234. /* Multiprocessor system : Multi-threads per core */
  235. cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  236. cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  237. cpuid_topo->package_id = MPIDR_AFFINITY_LEVEL(mpidr, 2) |
  238. MPIDR_AFFINITY_LEVEL(mpidr, 3) << 8;
  239. } else {
  240. /* Multiprocessor system : Single-thread per core */
  241. cpuid_topo->thread_id = -1;
  242. cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  243. cpuid_topo->package_id = MPIDR_AFFINITY_LEVEL(mpidr, 1) |
  244. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 8 |
  245. MPIDR_AFFINITY_LEVEL(mpidr, 3) << 16;
  246. }
  247. pr_debug("CPU%u: cluster %d core %d thread %d mpidr %#016llx\n",
  248. cpuid, cpuid_topo->package_id, cpuid_topo->core_id,
  249. cpuid_topo->thread_id, mpidr);
  250. topology_populated:
  251. update_siblings_masks(cpuid);
  252. }
  253. static void clear_cpu_topology(int cpu)
  254. {
  255. struct cpu_topology *cpu_topo = &cpu_topology[cpu];
  256. cpumask_clear(&cpu_topo->llc_sibling);
  257. cpumask_set_cpu(cpu, &cpu_topo->llc_sibling);
  258. cpumask_clear(&cpu_topo->core_sibling);
  259. cpumask_set_cpu(cpu, &cpu_topo->core_sibling);
  260. cpumask_clear(&cpu_topo->thread_sibling);
  261. cpumask_set_cpu(cpu, &cpu_topo->thread_sibling);
  262. }
  263. static void __init reset_cpu_topology(void)
  264. {
  265. unsigned int cpu;
  266. for_each_possible_cpu(cpu) {
  267. struct cpu_topology *cpu_topo = &cpu_topology[cpu];
  268. cpu_topo->thread_id = -1;
  269. cpu_topo->core_id = 0;
  270. cpu_topo->package_id = -1;
  271. cpu_topo->llc_id = -1;
  272. clear_cpu_topology(cpu);
  273. }
  274. }
  275. void remove_cpu_topology(unsigned int cpu)
  276. {
  277. int sibling;
  278. for_each_cpu(sibling, topology_core_cpumask(cpu))
  279. cpumask_clear_cpu(cpu, topology_core_cpumask(sibling));
  280. for_each_cpu(sibling, topology_sibling_cpumask(cpu))
  281. cpumask_clear_cpu(cpu, topology_sibling_cpumask(sibling));
  282. for_each_cpu(sibling, topology_llc_cpumask(cpu))
  283. cpumask_clear_cpu(cpu, topology_llc_cpumask(sibling));
  284. clear_cpu_topology(cpu);
  285. }
  286. #ifdef CONFIG_ACPI
  287. /*
  288. * Propagate the topology information of the processor_topology_node tree to the
  289. * cpu_topology array.
  290. */
  291. static int __init parse_acpi_topology(void)
  292. {
  293. bool is_threaded;
  294. int cpu, topology_id;
  295. is_threaded = read_cpuid_mpidr() & MPIDR_MT_BITMASK;
  296. for_each_possible_cpu(cpu) {
  297. int i, cache_id;
  298. topology_id = find_acpi_cpu_topology(cpu, 0);
  299. if (topology_id < 0)
  300. return topology_id;
  301. if (is_threaded) {
  302. cpu_topology[cpu].thread_id = topology_id;
  303. topology_id = find_acpi_cpu_topology(cpu, 1);
  304. cpu_topology[cpu].core_id = topology_id;
  305. } else {
  306. cpu_topology[cpu].thread_id = -1;
  307. cpu_topology[cpu].core_id = topology_id;
  308. }
  309. topology_id = find_acpi_cpu_topology_package(cpu);
  310. cpu_topology[cpu].package_id = topology_id;
  311. i = acpi_find_last_cache_level(cpu);
  312. if (i > 0) {
  313. /*
  314. * this is the only part of cpu_topology that has
  315. * a direct relationship with the cache topology
  316. */
  317. cache_id = find_acpi_cpu_cache_topology(cpu, i);
  318. if (cache_id > 0)
  319. cpu_topology[cpu].llc_id = cache_id;
  320. }
  321. }
  322. return 0;
  323. }
  324. #else
  325. static inline int __init parse_acpi_topology(void)
  326. {
  327. return -EINVAL;
  328. }
  329. #endif
  330. void __init init_cpu_topology(void)
  331. {
  332. reset_cpu_topology();
  333. /*
  334. * Discard anything that was parsed if we hit an error so we
  335. * don't use partial information.
  336. */
  337. if (!acpi_disabled && parse_acpi_topology())
  338. reset_cpu_topology();
  339. else if (of_have_populated_dt() && parse_dt_topology())
  340. reset_cpu_topology();
  341. }