topology.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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/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 <linux/string.h>
  23. #include <linux/cpufreq.h>
  24. #include <asm/cpu.h>
  25. #include <asm/cputype.h>
  26. #include <asm/topology.h>
  27. static DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
  28. static DEFINE_MUTEX(cpu_scale_mutex);
  29. unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
  30. {
  31. return per_cpu(cpu_scale, cpu);
  32. }
  33. static void set_capacity_scale(unsigned int cpu, unsigned long capacity)
  34. {
  35. per_cpu(cpu_scale, cpu) = capacity;
  36. }
  37. #ifdef CONFIG_PROC_SYSCTL
  38. static ssize_t cpu_capacity_show(struct device *dev,
  39. struct device_attribute *attr,
  40. char *buf)
  41. {
  42. struct cpu *cpu = container_of(dev, struct cpu, dev);
  43. return sprintf(buf, "%lu\n",
  44. arch_scale_cpu_capacity(NULL, cpu->dev.id));
  45. }
  46. static ssize_t cpu_capacity_store(struct device *dev,
  47. struct device_attribute *attr,
  48. const char *buf,
  49. size_t count)
  50. {
  51. struct cpu *cpu = container_of(dev, struct cpu, dev);
  52. int this_cpu = cpu->dev.id, i;
  53. unsigned long new_capacity;
  54. ssize_t ret;
  55. if (count) {
  56. ret = kstrtoul(buf, 0, &new_capacity);
  57. if (ret)
  58. return ret;
  59. if (new_capacity > SCHED_CAPACITY_SCALE)
  60. return -EINVAL;
  61. mutex_lock(&cpu_scale_mutex);
  62. for_each_cpu(i, &cpu_topology[this_cpu].core_sibling)
  63. set_capacity_scale(i, new_capacity);
  64. mutex_unlock(&cpu_scale_mutex);
  65. }
  66. return count;
  67. }
  68. static DEVICE_ATTR_RW(cpu_capacity);
  69. static int register_cpu_capacity_sysctl(void)
  70. {
  71. int i;
  72. struct device *cpu;
  73. for_each_possible_cpu(i) {
  74. cpu = get_cpu_device(i);
  75. if (!cpu) {
  76. pr_err("%s: too early to get CPU%d device!\n",
  77. __func__, i);
  78. continue;
  79. }
  80. device_create_file(cpu, &dev_attr_cpu_capacity);
  81. }
  82. return 0;
  83. }
  84. subsys_initcall(register_cpu_capacity_sysctl);
  85. #endif
  86. static u32 capacity_scale;
  87. static u32 *raw_capacity;
  88. static bool cap_parsing_failed;
  89. static void __init parse_cpu_capacity(struct device_node *cpu_node, int cpu)
  90. {
  91. int ret;
  92. u32 cpu_capacity;
  93. if (cap_parsing_failed)
  94. return;
  95. ret = of_property_read_u32(cpu_node,
  96. "capacity-dmips-mhz",
  97. &cpu_capacity);
  98. if (!ret) {
  99. if (!raw_capacity) {
  100. raw_capacity = kcalloc(num_possible_cpus(),
  101. sizeof(*raw_capacity),
  102. GFP_KERNEL);
  103. if (!raw_capacity) {
  104. pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
  105. cap_parsing_failed = true;
  106. return;
  107. }
  108. }
  109. capacity_scale = max(cpu_capacity, capacity_scale);
  110. raw_capacity[cpu] = cpu_capacity;
  111. pr_debug("cpu_capacity: %s cpu_capacity=%u (raw)\n",
  112. cpu_node->full_name, raw_capacity[cpu]);
  113. } else {
  114. if (raw_capacity) {
  115. pr_err("cpu_capacity: missing %s raw capacity\n",
  116. cpu_node->full_name);
  117. pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
  118. }
  119. cap_parsing_failed = true;
  120. kfree(raw_capacity);
  121. }
  122. }
  123. static void normalize_cpu_capacity(void)
  124. {
  125. u64 capacity;
  126. int cpu;
  127. if (!raw_capacity || cap_parsing_failed)
  128. return;
  129. pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale);
  130. mutex_lock(&cpu_scale_mutex);
  131. for_each_possible_cpu(cpu) {
  132. pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n",
  133. cpu, raw_capacity[cpu]);
  134. capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT)
  135. / capacity_scale;
  136. set_capacity_scale(cpu, capacity);
  137. pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
  138. cpu, arch_scale_cpu_capacity(NULL, cpu));
  139. }
  140. mutex_unlock(&cpu_scale_mutex);
  141. }
  142. #ifdef CONFIG_CPU_FREQ
  143. static cpumask_var_t cpus_to_visit;
  144. static bool cap_parsing_done;
  145. static void parsing_done_workfn(struct work_struct *work);
  146. static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
  147. static int
  148. init_cpu_capacity_callback(struct notifier_block *nb,
  149. unsigned long val,
  150. void *data)
  151. {
  152. struct cpufreq_policy *policy = data;
  153. int cpu;
  154. if (cap_parsing_failed || cap_parsing_done)
  155. return 0;
  156. switch (val) {
  157. case CPUFREQ_NOTIFY:
  158. pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
  159. cpumask_pr_args(policy->related_cpus),
  160. cpumask_pr_args(cpus_to_visit));
  161. cpumask_andnot(cpus_to_visit,
  162. cpus_to_visit,
  163. policy->related_cpus);
  164. for_each_cpu(cpu, policy->related_cpus) {
  165. raw_capacity[cpu] = arch_scale_cpu_capacity(NULL, cpu) *
  166. policy->cpuinfo.max_freq / 1000UL;
  167. capacity_scale = max(raw_capacity[cpu], capacity_scale);
  168. }
  169. if (cpumask_empty(cpus_to_visit)) {
  170. normalize_cpu_capacity();
  171. kfree(raw_capacity);
  172. pr_debug("cpu_capacity: parsing done\n");
  173. cap_parsing_done = true;
  174. schedule_work(&parsing_done_work);
  175. }
  176. }
  177. return 0;
  178. }
  179. static struct notifier_block init_cpu_capacity_notifier = {
  180. .notifier_call = init_cpu_capacity_callback,
  181. };
  182. static int __init register_cpufreq_notifier(void)
  183. {
  184. if (cap_parsing_failed)
  185. return -EINVAL;
  186. if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) {
  187. pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
  188. return -ENOMEM;
  189. }
  190. cpumask_copy(cpus_to_visit, cpu_possible_mask);
  191. return cpufreq_register_notifier(&init_cpu_capacity_notifier,
  192. CPUFREQ_POLICY_NOTIFIER);
  193. }
  194. core_initcall(register_cpufreq_notifier);
  195. static void parsing_done_workfn(struct work_struct *work)
  196. {
  197. cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
  198. CPUFREQ_POLICY_NOTIFIER);
  199. }
  200. #else
  201. static int __init free_raw_capacity(void)
  202. {
  203. kfree(raw_capacity);
  204. return 0;
  205. }
  206. core_initcall(free_raw_capacity);
  207. #endif
  208. static int __init get_cpu_for_node(struct device_node *node)
  209. {
  210. struct device_node *cpu_node;
  211. int cpu;
  212. cpu_node = of_parse_phandle(node, "cpu", 0);
  213. if (!cpu_node)
  214. return -1;
  215. for_each_possible_cpu(cpu) {
  216. if (of_get_cpu_node(cpu, NULL) == cpu_node) {
  217. parse_cpu_capacity(cpu_node, cpu);
  218. of_node_put(cpu_node);
  219. return cpu;
  220. }
  221. }
  222. pr_crit("Unable to find CPU node for %s\n", cpu_node->full_name);
  223. of_node_put(cpu_node);
  224. return -1;
  225. }
  226. static int __init parse_core(struct device_node *core, int cluster_id,
  227. int core_id)
  228. {
  229. char name[10];
  230. bool leaf = true;
  231. int i = 0;
  232. int cpu;
  233. struct device_node *t;
  234. do {
  235. snprintf(name, sizeof(name), "thread%d", i);
  236. t = of_get_child_by_name(core, name);
  237. if (t) {
  238. leaf = false;
  239. cpu = get_cpu_for_node(t);
  240. if (cpu >= 0) {
  241. cpu_topology[cpu].cluster_id = cluster_id;
  242. cpu_topology[cpu].core_id = core_id;
  243. cpu_topology[cpu].thread_id = i;
  244. } else {
  245. pr_err("%s: Can't get CPU for thread\n",
  246. t->full_name);
  247. of_node_put(t);
  248. return -EINVAL;
  249. }
  250. of_node_put(t);
  251. }
  252. i++;
  253. } while (t);
  254. cpu = get_cpu_for_node(core);
  255. if (cpu >= 0) {
  256. if (!leaf) {
  257. pr_err("%s: Core has both threads and CPU\n",
  258. core->full_name);
  259. return -EINVAL;
  260. }
  261. cpu_topology[cpu].cluster_id = cluster_id;
  262. cpu_topology[cpu].core_id = core_id;
  263. } else if (leaf) {
  264. pr_err("%s: Can't get CPU for leaf core\n", core->full_name);
  265. return -EINVAL;
  266. }
  267. return 0;
  268. }
  269. static int __init parse_cluster(struct device_node *cluster, int depth)
  270. {
  271. char name[10];
  272. bool leaf = true;
  273. bool has_cores = false;
  274. struct device_node *c;
  275. static int cluster_id __initdata;
  276. int core_id = 0;
  277. int i, ret;
  278. /*
  279. * First check for child clusters; we currently ignore any
  280. * information about the nesting of clusters and present the
  281. * scheduler with a flat list of them.
  282. */
  283. i = 0;
  284. do {
  285. snprintf(name, sizeof(name), "cluster%d", i);
  286. c = of_get_child_by_name(cluster, name);
  287. if (c) {
  288. leaf = false;
  289. ret = parse_cluster(c, depth + 1);
  290. of_node_put(c);
  291. if (ret != 0)
  292. return ret;
  293. }
  294. i++;
  295. } while (c);
  296. /* Now check for cores */
  297. i = 0;
  298. do {
  299. snprintf(name, sizeof(name), "core%d", i);
  300. c = of_get_child_by_name(cluster, name);
  301. if (c) {
  302. has_cores = true;
  303. if (depth == 0) {
  304. pr_err("%s: cpu-map children should be clusters\n",
  305. c->full_name);
  306. of_node_put(c);
  307. return -EINVAL;
  308. }
  309. if (leaf) {
  310. ret = parse_core(c, cluster_id, core_id++);
  311. } else {
  312. pr_err("%s: Non-leaf cluster with core %s\n",
  313. cluster->full_name, name);
  314. ret = -EINVAL;
  315. }
  316. of_node_put(c);
  317. if (ret != 0)
  318. return ret;
  319. }
  320. i++;
  321. } while (c);
  322. if (leaf && !has_cores)
  323. pr_warn("%s: empty cluster\n", cluster->full_name);
  324. if (leaf)
  325. cluster_id++;
  326. return 0;
  327. }
  328. static int __init parse_dt_topology(void)
  329. {
  330. struct device_node *cn, *map;
  331. int ret = 0;
  332. int cpu;
  333. cn = of_find_node_by_path("/cpus");
  334. if (!cn) {
  335. pr_err("No CPU information found in DT\n");
  336. return 0;
  337. }
  338. /*
  339. * When topology is provided cpu-map is essentially a root
  340. * cluster with restricted subnodes.
  341. */
  342. map = of_get_child_by_name(cn, "cpu-map");
  343. if (!map) {
  344. cap_parsing_failed = true;
  345. goto out;
  346. }
  347. ret = parse_cluster(map, 0);
  348. if (ret != 0)
  349. goto out_map;
  350. normalize_cpu_capacity();
  351. /*
  352. * Check that all cores are in the topology; the SMP code will
  353. * only mark cores described in the DT as possible.
  354. */
  355. for_each_possible_cpu(cpu)
  356. if (cpu_topology[cpu].cluster_id == -1)
  357. ret = -EINVAL;
  358. out_map:
  359. of_node_put(map);
  360. out:
  361. of_node_put(cn);
  362. return ret;
  363. }
  364. /*
  365. * cpu topology table
  366. */
  367. struct cpu_topology cpu_topology[NR_CPUS];
  368. EXPORT_SYMBOL_GPL(cpu_topology);
  369. const struct cpumask *cpu_coregroup_mask(int cpu)
  370. {
  371. return &cpu_topology[cpu].core_sibling;
  372. }
  373. static void update_siblings_masks(unsigned int cpuid)
  374. {
  375. struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
  376. int cpu;
  377. /* update core and thread sibling masks */
  378. for_each_possible_cpu(cpu) {
  379. cpu_topo = &cpu_topology[cpu];
  380. if (cpuid_topo->cluster_id != cpu_topo->cluster_id)
  381. continue;
  382. cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
  383. if (cpu != cpuid)
  384. cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
  385. if (cpuid_topo->core_id != cpu_topo->core_id)
  386. continue;
  387. cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
  388. if (cpu != cpuid)
  389. cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
  390. }
  391. }
  392. void store_cpu_topology(unsigned int cpuid)
  393. {
  394. struct cpu_topology *cpuid_topo = &cpu_topology[cpuid];
  395. u64 mpidr;
  396. if (cpuid_topo->cluster_id != -1)
  397. goto topology_populated;
  398. mpidr = read_cpuid_mpidr();
  399. /* Uniprocessor systems can rely on default topology values */
  400. if (mpidr & MPIDR_UP_BITMASK)
  401. return;
  402. /* Create cpu topology mapping based on MPIDR. */
  403. if (mpidr & MPIDR_MT_BITMASK) {
  404. /* Multiprocessor system : Multi-threads per core */
  405. cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  406. cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  407. cpuid_topo->cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 2) |
  408. MPIDR_AFFINITY_LEVEL(mpidr, 3) << 8;
  409. } else {
  410. /* Multiprocessor system : Single-thread per core */
  411. cpuid_topo->thread_id = -1;
  412. cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  413. cpuid_topo->cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 1) |
  414. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 8 |
  415. MPIDR_AFFINITY_LEVEL(mpidr, 3) << 16;
  416. }
  417. pr_debug("CPU%u: cluster %d core %d thread %d mpidr %#016llx\n",
  418. cpuid, cpuid_topo->cluster_id, cpuid_topo->core_id,
  419. cpuid_topo->thread_id, mpidr);
  420. topology_populated:
  421. update_siblings_masks(cpuid);
  422. }
  423. static void __init reset_cpu_topology(void)
  424. {
  425. unsigned int cpu;
  426. for_each_possible_cpu(cpu) {
  427. struct cpu_topology *cpu_topo = &cpu_topology[cpu];
  428. cpu_topo->thread_id = -1;
  429. cpu_topo->core_id = 0;
  430. cpu_topo->cluster_id = -1;
  431. cpumask_clear(&cpu_topo->core_sibling);
  432. cpumask_set_cpu(cpu, &cpu_topo->core_sibling);
  433. cpumask_clear(&cpu_topo->thread_sibling);
  434. cpumask_set_cpu(cpu, &cpu_topo->thread_sibling);
  435. }
  436. }
  437. void __init init_cpu_topology(void)
  438. {
  439. reset_cpu_topology();
  440. /*
  441. * Discard anything that was parsed if we hit an error so we
  442. * don't use partial information.
  443. */
  444. if (of_have_populated_dt() && parse_dt_topology())
  445. reset_cpu_topology();
  446. }