topology.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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 <asm/cputype.h>
  23. #include <asm/topology.h>
  24. /*
  25. * cpu capacity scale management
  26. */
  27. /*
  28. * cpu capacity 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_capacity field so the scheduler
  32. * can take this difference into account during load balance. A per cpu
  33. * structure is preferred because each CPU updates its own cpu_capacity field
  34. * during the load balance except for idle cores. One idle core is selected
  35. * to run the rebalance_domains for all idle cores and the cpu_capacity can be
  36. * updated during this sequence.
  37. */
  38. static DEFINE_PER_CPU(unsigned long, cpu_scale);
  39. unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu)
  40. {
  41. return per_cpu(cpu_scale, cpu);
  42. }
  43. unsigned long arch_get_max_cpu_capacity(int cpu)
  44. {
  45. return per_cpu(cpu_scale, cpu);
  46. }
  47. static void set_capacity_scale(unsigned int cpu, unsigned long capacity)
  48. {
  49. per_cpu(cpu_scale, cpu) = capacity;
  50. }
  51. static int __init get_cpu_for_node(struct device_node *node)
  52. {
  53. struct device_node *cpu_node;
  54. int cpu;
  55. cpu_node = of_parse_phandle(node, "cpu", 0);
  56. if (!cpu_node)
  57. return -1;
  58. for_each_possible_cpu(cpu) {
  59. if (of_get_cpu_node(cpu, NULL) == cpu_node) {
  60. of_node_put(cpu_node);
  61. return cpu;
  62. }
  63. }
  64. pr_crit("Unable to find CPU node for %s\n", cpu_node->full_name);
  65. of_node_put(cpu_node);
  66. return -1;
  67. }
  68. static int __init parse_core(struct device_node *core, int cluster_id,
  69. int core_id)
  70. {
  71. char name[10];
  72. bool leaf = true;
  73. int i = 0;
  74. int cpu;
  75. struct device_node *t;
  76. do {
  77. snprintf(name, sizeof(name), "thread%d", i);
  78. t = of_get_child_by_name(core, name);
  79. if (t) {
  80. leaf = false;
  81. cpu = get_cpu_for_node(t);
  82. if (cpu >= 0) {
  83. cpu_topology[cpu].cluster_id = cluster_id;
  84. cpu_topology[cpu].core_id = core_id;
  85. cpu_topology[cpu].thread_id = i;
  86. } else {
  87. pr_err("%s: Can't get CPU for thread\n",
  88. t->full_name);
  89. of_node_put(t);
  90. return -EINVAL;
  91. }
  92. of_node_put(t);
  93. }
  94. i++;
  95. } while (t);
  96. cpu = get_cpu_for_node(core);
  97. if (cpu >= 0) {
  98. if (!leaf) {
  99. pr_err("%s: Core has both threads and CPU\n",
  100. core->full_name);
  101. return -EINVAL;
  102. }
  103. cpu_topology[cpu].cluster_id = cluster_id;
  104. cpu_topology[cpu].core_id = core_id;
  105. } else if (leaf) {
  106. pr_err("%s: Can't get CPU for leaf core\n", core->full_name);
  107. return -EINVAL;
  108. }
  109. return 0;
  110. }
  111. static int __init parse_cluster(struct device_node *cluster, int depth)
  112. {
  113. char name[10];
  114. bool leaf = true;
  115. bool has_cores = false;
  116. struct device_node *c;
  117. static int cluster_id __initdata;
  118. int core_id = 0;
  119. int i, ret;
  120. /*
  121. * First check for child clusters; we currently ignore any
  122. * information about the nesting of clusters and present the
  123. * scheduler with a flat list of them.
  124. */
  125. i = 0;
  126. do {
  127. snprintf(name, sizeof(name), "cluster%d", i);
  128. c = of_get_child_by_name(cluster, name);
  129. if (c) {
  130. leaf = false;
  131. ret = parse_cluster(c, depth + 1);
  132. of_node_put(c);
  133. if (ret != 0)
  134. return ret;
  135. }
  136. i++;
  137. } while (c);
  138. /* Now check for cores */
  139. i = 0;
  140. do {
  141. snprintf(name, sizeof(name), "core%d", i);
  142. c = of_get_child_by_name(cluster, name);
  143. if (c) {
  144. has_cores = true;
  145. if (depth == 0) {
  146. pr_err("%s: cpu-map children should be clusters\n",
  147. c->full_name);
  148. of_node_put(c);
  149. return -EINVAL;
  150. }
  151. if (leaf) {
  152. ret = parse_core(c, cluster_id, core_id++);
  153. } else {
  154. pr_err("%s: Non-leaf cluster with core %s\n",
  155. cluster->full_name, name);
  156. ret = -EINVAL;
  157. }
  158. of_node_put(c);
  159. if (ret != 0)
  160. return ret;
  161. }
  162. i++;
  163. } while (c);
  164. if (leaf && !has_cores)
  165. pr_warn("%s: empty cluster\n", cluster->full_name);
  166. if (leaf)
  167. cluster_id++;
  168. return 0;
  169. }
  170. struct cpu_efficiency {
  171. const char *compatible;
  172. unsigned long efficiency;
  173. };
  174. /*
  175. * Table of relative efficiency of each processors
  176. * The efficiency value must fit in 20bit and the final
  177. * cpu_scale value must be in the range
  178. * 0 < cpu_scale < SCHED_CAPACITY_SCALE.
  179. * Processors that are not defined in the table,
  180. * use the default SCHED_CAPACITY_SCALE value for cpu_scale.
  181. */
  182. static const struct cpu_efficiency table_efficiency[] = {
  183. { "arm,cortex-a72", 4186 },
  184. { "arm,cortex-a57", 3891 },
  185. { "arm,cortex-a53", 2048 },
  186. { NULL, },
  187. };
  188. static unsigned long *__cpu_capacity;
  189. #define cpu_capacity(cpu) __cpu_capacity[cpu]
  190. static unsigned long max_cpu_perf, min_cpu_perf;
  191. static int __init parse_dt_topology(void)
  192. {
  193. struct device_node *cn, *map;
  194. int ret = 0;
  195. int cpu;
  196. cn = of_find_node_by_path("/cpus");
  197. if (!cn) {
  198. pr_err("No CPU information found in DT\n");
  199. return 0;
  200. }
  201. /*
  202. * When topology is provided cpu-map is essentially a root
  203. * cluster with restricted subnodes.
  204. */
  205. map = of_get_child_by_name(cn, "cpu-map");
  206. if (!map)
  207. goto out;
  208. ret = parse_cluster(map, 0);
  209. if (ret != 0)
  210. goto out_map;
  211. /*
  212. * Check that all cores are in the topology; the SMP code will
  213. * only mark cores described in the DT as possible.
  214. */
  215. for_each_possible_cpu(cpu)
  216. if (cpu_topology[cpu].cluster_id == -1)
  217. ret = -EINVAL;
  218. out_map:
  219. of_node_put(map);
  220. out:
  221. of_node_put(cn);
  222. return ret;
  223. }
  224. /*
  225. * Look for a customed capacity of a CPU in the cpu_capacity table during the
  226. * boot. The update of all CPUs is in O(n^2) for heteregeneous system but the
  227. * function returns directly for SMP systems or if there is no complete set
  228. * of cpu efficiency, clock frequency data for each cpu.
  229. */
  230. static void update_cpu_capacity(unsigned int cpu)
  231. {
  232. unsigned long capacity = cpu_capacity(cpu);
  233. if (!capacity || !max_cpu_perf) {
  234. cpu_capacity(cpu) = 0;
  235. return;
  236. }
  237. capacity *= SCHED_CAPACITY_SCALE;
  238. capacity /= max_cpu_perf;
  239. set_capacity_scale(cpu, capacity);
  240. pr_info("CPU%u: update cpu_capacity %lu\n",
  241. cpu, arch_scale_cpu_capacity(NULL, cpu));
  242. }
  243. /*
  244. * Scheduler load-tracking scale-invariance
  245. *
  246. * Provides the scheduler with a scale-invariance correction factor that
  247. * compensates for frequency scaling.
  248. */
  249. static DEFINE_PER_CPU(atomic_long_t, cpu_freq_capacity);
  250. static DEFINE_PER_CPU(atomic_long_t, cpu_max_freq);
  251. /* cpufreq callback function setting current cpu frequency */
  252. void arch_scale_set_curr_freq(int cpu, unsigned long freq)
  253. {
  254. unsigned long max = atomic_long_read(&per_cpu(cpu_max_freq, cpu));
  255. unsigned long curr;
  256. if (!max)
  257. return;
  258. curr = (freq * SCHED_CAPACITY_SCALE) / max;
  259. atomic_long_set(&per_cpu(cpu_freq_capacity, cpu), curr);
  260. }
  261. /* cpufreq callback function setting max cpu frequency */
  262. void arch_scale_set_max_freq(int cpu, unsigned long freq)
  263. {
  264. atomic_long_set(&per_cpu(cpu_max_freq, cpu), freq);
  265. }
  266. unsigned long arch_scale_freq_capacity(struct sched_domain *sd, int cpu)
  267. {
  268. unsigned long curr = atomic_long_read(&per_cpu(cpu_freq_capacity, cpu));
  269. if (!curr)
  270. return SCHED_CAPACITY_SCALE;
  271. return curr;
  272. }
  273. static void __init parse_dt_cpu_capacity(void)
  274. {
  275. const struct cpu_efficiency *cpu_eff;
  276. struct device_node *cn = NULL;
  277. int cpu = 0, i = 0;
  278. __cpu_capacity = kcalloc(nr_cpu_ids, sizeof(*__cpu_capacity),
  279. GFP_NOWAIT);
  280. min_cpu_perf = ULONG_MAX;
  281. max_cpu_perf = 0;
  282. for_each_possible_cpu(cpu) {
  283. const u32 *rate;
  284. int len;
  285. unsigned long cpu_perf;
  286. /* too early to use cpu->of_node */
  287. cn = of_get_cpu_node(cpu, NULL);
  288. if (!cn) {
  289. pr_err("missing device node for CPU %d\n", cpu);
  290. continue;
  291. }
  292. for (cpu_eff = table_efficiency; cpu_eff->compatible; cpu_eff++)
  293. if (of_device_is_compatible(cn, cpu_eff->compatible))
  294. break;
  295. if (cpu_eff->compatible == NULL)
  296. continue;
  297. rate = of_get_property(cn, "clock-frequency", &len);
  298. if (!rate || len != 4) {
  299. pr_err("%s missing clock-frequency property\n",
  300. cn->full_name);
  301. continue;
  302. }
  303. cpu_perf = ((be32_to_cpup(rate)) >> 20) * cpu_eff->efficiency;
  304. cpu_capacity(cpu) = cpu_perf;
  305. max_cpu_perf = max(max_cpu_perf, cpu_perf);
  306. min_cpu_perf = min(min_cpu_perf, cpu_perf);
  307. i++;
  308. }
  309. if (i < num_possible_cpus()) {
  310. max_cpu_perf = 0;
  311. min_cpu_perf = 0;
  312. }
  313. }
  314. /*
  315. * cpu topology table
  316. */
  317. struct cpu_topology cpu_topology[NR_CPUS];
  318. EXPORT_SYMBOL_GPL(cpu_topology);
  319. const struct cpumask *cpu_coregroup_mask(int cpu)
  320. {
  321. return &cpu_topology[cpu].core_sibling;
  322. }
  323. static void update_siblings_masks(unsigned int cpuid)
  324. {
  325. struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
  326. int cpu;
  327. /* update core and thread sibling masks */
  328. for_each_possible_cpu(cpu) {
  329. cpu_topo = &cpu_topology[cpu];
  330. if (cpuid_topo->cluster_id != cpu_topo->cluster_id)
  331. continue;
  332. cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
  333. if (cpu != cpuid)
  334. cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
  335. if (cpuid_topo->core_id != cpu_topo->core_id)
  336. continue;
  337. cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
  338. if (cpu != cpuid)
  339. cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
  340. }
  341. }
  342. void store_cpu_topology(unsigned int cpuid)
  343. {
  344. struct cpu_topology *cpuid_topo = &cpu_topology[cpuid];
  345. u64 mpidr;
  346. if (cpuid_topo->cluster_id != -1)
  347. goto topology_populated;
  348. mpidr = read_cpuid_mpidr();
  349. /* Uniprocessor systems can rely on default topology values */
  350. if (mpidr & MPIDR_UP_BITMASK)
  351. return;
  352. /* Create cpu topology mapping based on MPIDR. */
  353. if (mpidr & MPIDR_MT_BITMASK) {
  354. /* Multiprocessor system : Multi-threads per core */
  355. cpuid_topo->thread_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  356. cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  357. cpuid_topo->cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 2);
  358. } else {
  359. /* Multiprocessor system : Single-thread per core */
  360. cpuid_topo->thread_id = -1;
  361. cpuid_topo->core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  362. cpuid_topo->cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  363. }
  364. #ifndef CONFIG_SCHED_HMP
  365. cpuid_topo->partno = read_cpuid_part_number();
  366. #endif
  367. pr_debug("CPU%u: cluster %d core %d thread %d mpidr %#016llx\n",
  368. cpuid, cpuid_topo->cluster_id, cpuid_topo->core_id,
  369. cpuid_topo->thread_id, mpidr);
  370. topology_populated:
  371. #ifdef CONFIG_SCHED_HMP
  372. cpuid_topo->partno = read_cpuid_part_number();
  373. #endif
  374. update_siblings_masks(cpuid);
  375. update_cpu_capacity(cpuid);
  376. }
  377. static void __init reset_cpu_topology(void)
  378. {
  379. unsigned int cpu;
  380. for_each_possible_cpu(cpu) {
  381. struct cpu_topology *cpu_topo = &cpu_topology[cpu];
  382. cpu_topo->thread_id = -1;
  383. cpu_topo->core_id = 0;
  384. cpu_topo->cluster_id = -1;
  385. cpumask_clear(&cpu_topo->core_sibling);
  386. cpumask_set_cpu(cpu, &cpu_topo->core_sibling);
  387. cpumask_clear(&cpu_topo->thread_sibling);
  388. cpumask_set_cpu(cpu, &cpu_topo->thread_sibling);
  389. set_capacity_scale(cpu, SCHED_CAPACITY_SCALE);
  390. }
  391. }
  392. static int cpu_topology_init;
  393. /*
  394. * init_cpu_topology is called at boot when only one cpu is running
  395. * which prevent simultaneous write access to cpu_topology array
  396. */
  397. /*
  398. * init_cpu_topology is called at boot when only one cpu is running
  399. * which prevent simultaneous write access to cpu_topology array
  400. */
  401. void __init init_cpu_topology(void)
  402. {
  403. if (cpu_topology_init)
  404. return;
  405. reset_cpu_topology();
  406. /*
  407. * Discard anything that was parsed if we hit an error so we
  408. * don't use partial information.
  409. */
  410. if (parse_dt_topology())
  411. reset_cpu_topology();
  412. parse_dt_cpu_capacity();
  413. }
  414. #ifdef CONFIG_MTK_CPU_TOPOLOGY
  415. void __init arch_build_cpu_topology_domain(void)
  416. {
  417. init_cpu_topology();
  418. cpu_topology_init = 1;
  419. }
  420. #endif
  421. /*
  422. * Extras of CPU & Cluster functions
  423. */
  424. int arch_cpu_is_big(unsigned int cpu)
  425. {
  426. struct cpu_topology *cpu_topo = &cpu_topology[cpu];
  427. switch (cpu_topo->partno) {
  428. case ARM_CPU_PART_CORTEX_A57:
  429. case ARM_CPU_PART_CORTEX_A72:
  430. return 1;
  431. default:
  432. return 0;
  433. }
  434. }
  435. int arch_cpu_is_little(unsigned int cpu)
  436. {
  437. return !arch_cpu_is_big(cpu);
  438. }
  439. int arch_is_smp(void)
  440. {
  441. static int __arch_smp = -1;
  442. if (__arch_smp != -1)
  443. return __arch_smp;
  444. __arch_smp = (max_cpu_perf != min_cpu_perf) ? 0 : 1;
  445. return __arch_smp;
  446. }
  447. int arch_get_nr_clusters(void)
  448. {
  449. static int __arch_nr_clusters = -1;
  450. int max_id = 0;
  451. unsigned int cpu;
  452. if (__arch_nr_clusters != -1) {
  453. return __arch_nr_clusters;
  454. }
  455. /* assume socket id is monotonic increasing without gap. */
  456. for_each_possible_cpu(cpu) {
  457. struct cpu_topology *cpu_topo = &cpu_topology[cpu];
  458. if (cpu_topo->cluster_id > max_id)
  459. max_id = cpu_topo->cluster_id;
  460. }
  461. __arch_nr_clusters = max_id + 1;
  462. return __arch_nr_clusters;
  463. }
  464. int arch_is_multi_cluster(void)
  465. {
  466. return arch_get_nr_clusters() > 1 ? 1 : 0;
  467. }
  468. int arch_get_cluster_id(unsigned int cpu)
  469. {
  470. struct cpu_topology *cpu_topo = &cpu_topology[cpu];
  471. return cpu_topo->cluster_id < 0 ? 0 : cpu_topo->cluster_id;
  472. }
  473. void arch_get_cluster_cpus(struct cpumask *cpus, int cluster_id)
  474. {
  475. unsigned int cpu;
  476. cpumask_clear(cpus);
  477. for_each_possible_cpu(cpu) {
  478. struct cpu_topology *cpu_topo = &cpu_topology[cpu];
  479. if (cpu_topo->cluster_id == cluster_id)
  480. cpumask_set_cpu(cpu, cpus);
  481. }
  482. }
  483. int arch_better_capacity(unsigned int cpu)
  484. {
  485. BUG_ON(cpu >= num_possible_cpus());
  486. return cpu_capacity(cpu) > min_cpu_perf;
  487. }
  488. #ifdef CONFIG_SCHED_HMP
  489. void __init arch_get_fast_and_slow_cpus(struct cpumask *fast,
  490. struct cpumask *slow)
  491. {
  492. struct device_node *cn = NULL;
  493. int cpu;
  494. cpumask_clear(fast);
  495. cpumask_clear(slow);
  496. /* FIXME: temporarily use cluster id to identify cpumask */
  497. arch_get_cluster_cpus(slow, 0);
  498. arch_get_cluster_cpus(fast, 1);
  499. /* for_each_possible_cpu(cpu) {
  500. if (arch_cpu_is_little(cpu))
  501. cpumask_set_cpu(cpu, slow);
  502. else
  503. cpumask_set_cpu(cpu, fast);
  504. }
  505. */
  506. if (!cpumask_empty(fast) && !cpumask_empty(slow))
  507. return;
  508. /*
  509. * We didn't find both big and little cores so let's call all cores
  510. * fast as this will keep the system running, with all cores being
  511. * treated equal.
  512. */
  513. cpumask_setall(fast);
  514. cpumask_clear(slow);
  515. }
  516. struct cpumask hmp_fast_cpu_mask;
  517. struct cpumask hmp_slow_cpu_mask;
  518. void __init arch_get_hmp_domains(struct list_head *hmp_domains_list)
  519. {
  520. struct hmp_domain *domain;
  521. arch_get_fast_and_slow_cpus(&hmp_fast_cpu_mask, &hmp_slow_cpu_mask);
  522. /*
  523. * Initialize hmp_domains
  524. * Must be ordered with respect to compute capacity.
  525. * Fastest domain at head of list.
  526. */
  527. if (!cpumask_empty(&hmp_slow_cpu_mask)) {
  528. domain = (struct hmp_domain *)
  529. kmalloc(sizeof(struct hmp_domain), GFP_KERNEL);
  530. cpumask_copy(&domain->possible_cpus, &hmp_slow_cpu_mask);
  531. cpumask_and(&domain->cpus, cpu_online_mask, &domain->possible_cpus);
  532. list_add(&domain->hmp_domains, hmp_domains_list);
  533. }
  534. domain = (struct hmp_domain *)
  535. kmalloc(sizeof(struct hmp_domain), GFP_KERNEL);
  536. cpumask_copy(&domain->possible_cpus, &hmp_fast_cpu_mask);
  537. cpumask_and(&domain->cpus, cpu_online_mask, &domain->possible_cpus);
  538. list_add(&domain->hmp_domains, hmp_domains_list);
  539. }
  540. #else
  541. void __init arch_get_hmp_domains(struct list_head *hmp_domains_list) {}
  542. #endif /* CONFIG_SCHED_HMP */