arch_topology.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Arch specific cpu topology information
  4. *
  5. * Copyright (C) 2016, ARM Ltd.
  6. * Written by: Juri Lelli, ARM Ltd.
  7. */
  8. #include <linux/acpi.h>
  9. #include <linux/cacheinfo.h>
  10. #include <linux/cleanup.h>
  11. #include <linux/cpu.h>
  12. #include <linux/cpufreq.h>
  13. #include <linux/cpu_smt.h>
  14. #include <linux/device.h>
  15. #include <linux/of.h>
  16. #include <linux/slab.h>
  17. #include <linux/sched/topology.h>
  18. #include <linux/cpuset.h>
  19. #include <linux/cpumask.h>
  20. #include <linux/init.h>
  21. #include <linux/rcupdate.h>
  22. #include <linux/sched.h>
  23. #include <linux/units.h>
  24. #define CREATE_TRACE_POINTS
  25. #include <trace/events/hw_pressure.h>
  26. static DEFINE_PER_CPU(struct scale_freq_data __rcu *, sft_data);
  27. static struct cpumask scale_freq_counters_mask;
  28. static bool scale_freq_invariant;
  29. DEFINE_PER_CPU(unsigned long, capacity_freq_ref) = 0;
  30. EXPORT_PER_CPU_SYMBOL_GPL(capacity_freq_ref);
  31. static bool supports_scale_freq_counters(const struct cpumask *cpus)
  32. {
  33. int i;
  34. for_each_cpu(i, cpus) {
  35. if (cpumask_test_cpu(i, &scale_freq_counters_mask))
  36. return true;
  37. }
  38. return false;
  39. }
  40. bool topology_scale_freq_invariant(void)
  41. {
  42. return cpufreq_supports_freq_invariance() ||
  43. supports_scale_freq_counters(cpu_online_mask);
  44. }
  45. static void update_scale_freq_invariant(bool status)
  46. {
  47. if (scale_freq_invariant == status)
  48. return;
  49. /*
  50. * Task scheduler behavior depends on frequency invariance support,
  51. * either cpufreq or counter driven. If the support status changes as
  52. * a result of counter initialisation and use, retrigger the build of
  53. * scheduling domains to ensure the information is propagated properly.
  54. */
  55. if (topology_scale_freq_invariant() == status) {
  56. scale_freq_invariant = status;
  57. rebuild_sched_domains_energy();
  58. }
  59. }
  60. void topology_set_scale_freq_source(struct scale_freq_data *data,
  61. const struct cpumask *cpus)
  62. {
  63. struct scale_freq_data *sfd;
  64. int cpu;
  65. /*
  66. * Avoid calling rebuild_sched_domains() unnecessarily if FIE is
  67. * supported by cpufreq.
  68. */
  69. if (cpumask_empty(&scale_freq_counters_mask))
  70. scale_freq_invariant = topology_scale_freq_invariant();
  71. rcu_read_lock();
  72. for_each_cpu(cpu, cpus) {
  73. sfd = rcu_dereference(*per_cpu_ptr(&sft_data, cpu));
  74. /* Use ARCH provided counters whenever possible */
  75. if (!sfd || sfd->source != SCALE_FREQ_SOURCE_ARCH) {
  76. rcu_assign_pointer(per_cpu(sft_data, cpu), data);
  77. cpumask_set_cpu(cpu, &scale_freq_counters_mask);
  78. }
  79. }
  80. rcu_read_unlock();
  81. update_scale_freq_invariant(true);
  82. }
  83. EXPORT_SYMBOL_GPL(topology_set_scale_freq_source);
  84. void topology_clear_scale_freq_source(enum scale_freq_source source,
  85. const struct cpumask *cpus)
  86. {
  87. struct scale_freq_data *sfd;
  88. int cpu;
  89. rcu_read_lock();
  90. for_each_cpu(cpu, cpus) {
  91. sfd = rcu_dereference(*per_cpu_ptr(&sft_data, cpu));
  92. if (sfd && sfd->source == source) {
  93. rcu_assign_pointer(per_cpu(sft_data, cpu), NULL);
  94. cpumask_clear_cpu(cpu, &scale_freq_counters_mask);
  95. }
  96. }
  97. rcu_read_unlock();
  98. /*
  99. * Make sure all references to previous sft_data are dropped to avoid
  100. * use-after-free races.
  101. */
  102. synchronize_rcu();
  103. update_scale_freq_invariant(false);
  104. }
  105. EXPORT_SYMBOL_GPL(topology_clear_scale_freq_source);
  106. void topology_scale_freq_tick(void)
  107. {
  108. struct scale_freq_data *sfd = rcu_dereference_sched(*this_cpu_ptr(&sft_data));
  109. if (sfd)
  110. sfd->set_freq_scale();
  111. }
  112. DEFINE_PER_CPU(unsigned long, arch_freq_scale) = SCHED_CAPACITY_SCALE;
  113. EXPORT_PER_CPU_SYMBOL_GPL(arch_freq_scale);
  114. void topology_set_freq_scale(const struct cpumask *cpus, unsigned long cur_freq,
  115. unsigned long max_freq)
  116. {
  117. unsigned long scale;
  118. int i;
  119. if (WARN_ON_ONCE(!cur_freq || !max_freq))
  120. return;
  121. /*
  122. * If the use of counters for FIE is enabled, just return as we don't
  123. * want to update the scale factor with information from CPUFREQ.
  124. * Instead the scale factor will be updated from arch_scale_freq_tick.
  125. */
  126. if (supports_scale_freq_counters(cpus))
  127. return;
  128. scale = (cur_freq << SCHED_CAPACITY_SHIFT) / max_freq;
  129. for_each_cpu(i, cpus)
  130. per_cpu(arch_freq_scale, i) = scale;
  131. }
  132. DEFINE_PER_CPU(unsigned long, hw_pressure);
  133. /**
  134. * topology_update_hw_pressure() - Update HW pressure for CPUs
  135. * @cpus : The related CPUs for which capacity has been reduced
  136. * @capped_freq : The maximum allowed frequency that CPUs can run at
  137. *
  138. * Update the value of HW pressure for all @cpus in the mask. The
  139. * cpumask should include all (online+offline) affected CPUs, to avoid
  140. * operating on stale data when hot-plug is used for some CPUs. The
  141. * @capped_freq reflects the currently allowed max CPUs frequency due to
  142. * HW capping. It might be also a boost frequency value, which is bigger
  143. * than the internal 'capacity_freq_ref' max frequency. In such case the
  144. * pressure value should simply be removed, since this is an indication that
  145. * there is no HW throttling. The @capped_freq must be provided in kHz.
  146. */
  147. void topology_update_hw_pressure(const struct cpumask *cpus,
  148. unsigned long capped_freq)
  149. {
  150. unsigned long max_capacity, capacity, pressure;
  151. u32 max_freq;
  152. int cpu;
  153. cpu = cpumask_first(cpus);
  154. max_capacity = arch_scale_cpu_capacity(cpu);
  155. max_freq = arch_scale_freq_ref(cpu);
  156. /*
  157. * Handle properly the boost frequencies, which should simply clean
  158. * the HW pressure value.
  159. */
  160. if (max_freq <= capped_freq)
  161. capacity = max_capacity;
  162. else
  163. capacity = mult_frac(max_capacity, capped_freq, max_freq);
  164. pressure = max_capacity - capacity;
  165. trace_hw_pressure_update(cpu, pressure);
  166. for_each_cpu(cpu, cpus)
  167. WRITE_ONCE(per_cpu(hw_pressure, cpu), pressure);
  168. }
  169. EXPORT_SYMBOL_GPL(topology_update_hw_pressure);
  170. static void update_topology_flags_workfn(struct work_struct *work);
  171. static DECLARE_WORK(update_topology_flags_work, update_topology_flags_workfn);
  172. static int update_topology;
  173. int topology_update_cpu_topology(void)
  174. {
  175. return update_topology;
  176. }
  177. /*
  178. * Updating the sched_domains can't be done directly from cpufreq callbacks
  179. * due to locking, so queue the work for later.
  180. */
  181. static void update_topology_flags_workfn(struct work_struct *work)
  182. {
  183. update_topology = 1;
  184. rebuild_sched_domains();
  185. pr_debug("sched_domain hierarchy rebuilt, flags updated\n");
  186. update_topology = 0;
  187. }
  188. static u32 *raw_capacity;
  189. static int free_raw_capacity(void)
  190. {
  191. kfree(raw_capacity);
  192. raw_capacity = NULL;
  193. return 0;
  194. }
  195. void topology_normalize_cpu_scale(void)
  196. {
  197. u64 capacity;
  198. u64 capacity_scale;
  199. int cpu;
  200. if (!raw_capacity)
  201. return;
  202. capacity_scale = 1;
  203. for_each_possible_cpu(cpu) {
  204. capacity = raw_capacity[cpu] *
  205. (per_cpu(capacity_freq_ref, cpu) ?: 1);
  206. capacity_scale = max(capacity, capacity_scale);
  207. }
  208. pr_debug("cpu_capacity: capacity_scale=%llu\n", capacity_scale);
  209. for_each_possible_cpu(cpu) {
  210. capacity = raw_capacity[cpu] *
  211. (per_cpu(capacity_freq_ref, cpu) ?: 1);
  212. capacity = div64_u64(capacity << SCHED_CAPACITY_SHIFT,
  213. capacity_scale);
  214. topology_set_cpu_scale(cpu, capacity);
  215. pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
  216. cpu, topology_get_cpu_scale(cpu));
  217. }
  218. }
  219. bool __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
  220. {
  221. struct clk *cpu_clk;
  222. static bool cap_parsing_failed;
  223. int ret;
  224. u32 cpu_capacity;
  225. if (cap_parsing_failed)
  226. return false;
  227. ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz",
  228. &cpu_capacity);
  229. if (!ret) {
  230. if (!raw_capacity) {
  231. raw_capacity = kcalloc(num_possible_cpus(),
  232. sizeof(*raw_capacity),
  233. GFP_KERNEL);
  234. if (!raw_capacity) {
  235. cap_parsing_failed = true;
  236. return false;
  237. }
  238. }
  239. raw_capacity[cpu] = cpu_capacity;
  240. pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n",
  241. cpu_node, raw_capacity[cpu]);
  242. /*
  243. * Update capacity_freq_ref for calculating early boot CPU capacities.
  244. * For non-clk CPU DVFS mechanism, there's no way to get the
  245. * frequency value now, assuming they are running at the same
  246. * frequency (by keeping the initial capacity_freq_ref value).
  247. */
  248. cpu_clk = of_clk_get(cpu_node, 0);
  249. if (!IS_ERR_OR_NULL(cpu_clk)) {
  250. per_cpu(capacity_freq_ref, cpu) =
  251. clk_get_rate(cpu_clk) / HZ_PER_KHZ;
  252. clk_put(cpu_clk);
  253. }
  254. } else {
  255. if (raw_capacity) {
  256. pr_err("cpu_capacity: missing %pOF raw capacity\n",
  257. cpu_node);
  258. pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
  259. }
  260. cap_parsing_failed = true;
  261. free_raw_capacity();
  262. }
  263. return !ret;
  264. }
  265. void __weak freq_inv_set_max_ratio(int cpu, u64 max_rate)
  266. {
  267. }
  268. #ifdef CONFIG_ACPI_CPPC_LIB
  269. #include <acpi/cppc_acpi.h>
  270. static inline void topology_init_cpu_capacity_cppc(void)
  271. {
  272. u64 capacity, capacity_scale = 0;
  273. struct cppc_perf_caps perf_caps;
  274. int cpu;
  275. if (likely(!acpi_cpc_valid()))
  276. return;
  277. raw_capacity = kcalloc(num_possible_cpus(), sizeof(*raw_capacity),
  278. GFP_KERNEL);
  279. if (!raw_capacity)
  280. return;
  281. for_each_possible_cpu(cpu) {
  282. if (!cppc_get_perf_caps(cpu, &perf_caps) &&
  283. (perf_caps.highest_perf >= perf_caps.nominal_perf) &&
  284. (perf_caps.highest_perf >= perf_caps.lowest_perf)) {
  285. raw_capacity[cpu] = perf_caps.highest_perf;
  286. capacity_scale = max_t(u64, capacity_scale, raw_capacity[cpu]);
  287. per_cpu(capacity_freq_ref, cpu) = cppc_perf_to_khz(&perf_caps, raw_capacity[cpu]);
  288. pr_debug("cpu_capacity: CPU%d cpu_capacity=%u (raw).\n",
  289. cpu, raw_capacity[cpu]);
  290. continue;
  291. }
  292. pr_err("cpu_capacity: CPU%d missing/invalid highest performance.\n", cpu);
  293. pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
  294. goto exit;
  295. }
  296. for_each_possible_cpu(cpu) {
  297. freq_inv_set_max_ratio(cpu,
  298. per_cpu(capacity_freq_ref, cpu) * HZ_PER_KHZ);
  299. capacity = raw_capacity[cpu];
  300. capacity = div64_u64(capacity << SCHED_CAPACITY_SHIFT,
  301. capacity_scale);
  302. topology_set_cpu_scale(cpu, capacity);
  303. pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
  304. cpu, topology_get_cpu_scale(cpu));
  305. }
  306. schedule_work(&update_topology_flags_work);
  307. pr_debug("cpu_capacity: cpu_capacity initialization done\n");
  308. exit:
  309. free_raw_capacity();
  310. }
  311. void acpi_processor_init_invariance_cppc(void)
  312. {
  313. topology_init_cpu_capacity_cppc();
  314. }
  315. #endif
  316. #ifdef CONFIG_CPU_FREQ
  317. static cpumask_var_t cpus_to_visit;
  318. static void parsing_done_workfn(struct work_struct *work);
  319. static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
  320. static int
  321. init_cpu_capacity_callback(struct notifier_block *nb,
  322. unsigned long val,
  323. void *data)
  324. {
  325. struct cpufreq_policy *policy = data;
  326. int cpu;
  327. if (val != CPUFREQ_CREATE_POLICY)
  328. return 0;
  329. pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
  330. cpumask_pr_args(policy->related_cpus),
  331. cpumask_pr_args(cpus_to_visit));
  332. cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus);
  333. for_each_cpu(cpu, policy->related_cpus) {
  334. per_cpu(capacity_freq_ref, cpu) = policy->cpuinfo.max_freq;
  335. freq_inv_set_max_ratio(cpu,
  336. per_cpu(capacity_freq_ref, cpu) * HZ_PER_KHZ);
  337. }
  338. if (cpumask_empty(cpus_to_visit)) {
  339. if (raw_capacity) {
  340. topology_normalize_cpu_scale();
  341. schedule_work(&update_topology_flags_work);
  342. free_raw_capacity();
  343. }
  344. pr_debug("cpu_capacity: parsing done\n");
  345. schedule_work(&parsing_done_work);
  346. }
  347. return 0;
  348. }
  349. static struct notifier_block init_cpu_capacity_notifier = {
  350. .notifier_call = init_cpu_capacity_callback,
  351. };
  352. static int __init register_cpufreq_notifier(void)
  353. {
  354. int ret;
  355. /*
  356. * On ACPI-based systems skip registering cpufreq notifier as cpufreq
  357. * information is not needed for cpu capacity initialization.
  358. */
  359. if (!acpi_disabled)
  360. return -EINVAL;
  361. if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL))
  362. return -ENOMEM;
  363. cpumask_copy(cpus_to_visit, cpu_possible_mask);
  364. ret = cpufreq_register_notifier(&init_cpu_capacity_notifier,
  365. CPUFREQ_POLICY_NOTIFIER);
  366. if (ret)
  367. free_cpumask_var(cpus_to_visit);
  368. return ret;
  369. }
  370. core_initcall(register_cpufreq_notifier);
  371. static void parsing_done_workfn(struct work_struct *work)
  372. {
  373. cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
  374. CPUFREQ_POLICY_NOTIFIER);
  375. free_cpumask_var(cpus_to_visit);
  376. }
  377. #else
  378. core_initcall(free_raw_capacity);
  379. #endif
  380. #if defined(CONFIG_ARM64) || defined(CONFIG_RISCV)
  381. /* Used to enable the SMT control */
  382. static unsigned int max_smt_thread_num = 1;
  383. /*
  384. * This function returns the logic cpu number of the node.
  385. * There are basically three kinds of return values:
  386. * (1) logic cpu number which is > 0.
  387. * (2) -ENODEV when the device tree(DT) node is valid and found in the DT but
  388. * there is no possible logical CPU in the kernel to match. This happens
  389. * when CONFIG_NR_CPUS is configure to be smaller than the number of
  390. * CPU nodes in DT. We need to just ignore this case.
  391. * (3) -1 if the node does not exist in the device tree
  392. */
  393. static int __init get_cpu_for_node(struct device_node *node)
  394. {
  395. int cpu;
  396. struct device_node *cpu_node __free(device_node) =
  397. of_parse_phandle(node, "cpu", 0);
  398. if (!cpu_node)
  399. return -1;
  400. cpu = of_cpu_node_to_id(cpu_node);
  401. if (cpu >= 0)
  402. topology_parse_cpu_capacity(cpu_node, cpu);
  403. else
  404. pr_info("CPU node for %pOF exist but the possible cpu range is :%*pbl\n",
  405. cpu_node, cpumask_pr_args(cpu_possible_mask));
  406. return cpu;
  407. }
  408. static int __init parse_core(struct device_node *core, int package_id,
  409. int cluster_id, int core_id)
  410. {
  411. char name[20];
  412. bool leaf = true;
  413. int i = 0;
  414. int cpu;
  415. do {
  416. snprintf(name, sizeof(name), "thread%d", i);
  417. struct device_node *t __free(device_node) =
  418. of_get_child_by_name(core, name);
  419. if (!t)
  420. break;
  421. leaf = false;
  422. cpu = get_cpu_for_node(t);
  423. if (cpu >= 0) {
  424. cpu_topology[cpu].package_id = package_id;
  425. cpu_topology[cpu].cluster_id = cluster_id;
  426. cpu_topology[cpu].core_id = core_id;
  427. cpu_topology[cpu].thread_id = i;
  428. } else if (cpu != -ENODEV) {
  429. pr_err("%pOF: Can't get CPU for thread\n", t);
  430. return -EINVAL;
  431. }
  432. i++;
  433. } while (1);
  434. max_smt_thread_num = max_t(unsigned int, max_smt_thread_num, i);
  435. cpu = get_cpu_for_node(core);
  436. if (cpu >= 0) {
  437. if (!leaf) {
  438. pr_err("%pOF: Core has both threads and CPU\n",
  439. core);
  440. return -EINVAL;
  441. }
  442. cpu_topology[cpu].package_id = package_id;
  443. cpu_topology[cpu].cluster_id = cluster_id;
  444. cpu_topology[cpu].core_id = core_id;
  445. } else if (leaf && cpu != -ENODEV) {
  446. pr_err("%pOF: Can't get CPU for leaf core\n", core);
  447. return -EINVAL;
  448. }
  449. return 0;
  450. }
  451. static int __init parse_cluster(struct device_node *cluster, int package_id,
  452. int cluster_id, int depth)
  453. {
  454. char name[20];
  455. bool leaf = true;
  456. bool has_cores = false;
  457. int core_id = 0;
  458. int i, ret;
  459. /*
  460. * First check for child clusters; we currently ignore any
  461. * information about the nesting of clusters and present the
  462. * scheduler with a flat list of them.
  463. */
  464. i = 0;
  465. do {
  466. snprintf(name, sizeof(name), "cluster%d", i);
  467. struct device_node *c __free(device_node) =
  468. of_get_child_by_name(cluster, name);
  469. if (!c)
  470. break;
  471. leaf = false;
  472. ret = parse_cluster(c, package_id, i, depth + 1);
  473. if (depth > 0)
  474. pr_warn("Topology for clusters of clusters not yet supported\n");
  475. if (ret != 0)
  476. return ret;
  477. i++;
  478. } while (1);
  479. /* Now check for cores */
  480. i = 0;
  481. do {
  482. snprintf(name, sizeof(name), "core%d", i);
  483. struct device_node *c __free(device_node) =
  484. of_get_child_by_name(cluster, name);
  485. if (!c)
  486. break;
  487. has_cores = true;
  488. if (depth == 0) {
  489. pr_err("%pOF: cpu-map children should be clusters\n", c);
  490. return -EINVAL;
  491. }
  492. if (leaf) {
  493. ret = parse_core(c, package_id, cluster_id, core_id++);
  494. if (ret != 0)
  495. return ret;
  496. } else {
  497. pr_err("%pOF: Non-leaf cluster with core %s\n",
  498. cluster, name);
  499. return -EINVAL;
  500. }
  501. i++;
  502. } while (1);
  503. if (leaf && !has_cores)
  504. pr_warn("%pOF: empty cluster\n", cluster);
  505. return 0;
  506. }
  507. static int __init parse_socket(struct device_node *socket)
  508. {
  509. char name[20];
  510. bool has_socket = false;
  511. int package_id = 0, ret;
  512. do {
  513. snprintf(name, sizeof(name), "socket%d", package_id);
  514. struct device_node *c __free(device_node) =
  515. of_get_child_by_name(socket, name);
  516. if (!c)
  517. break;
  518. has_socket = true;
  519. ret = parse_cluster(c, package_id, -1, 0);
  520. if (ret != 0)
  521. return ret;
  522. package_id++;
  523. } while (1);
  524. if (!has_socket)
  525. ret = parse_cluster(socket, 0, -1, 0);
  526. /*
  527. * Reset the max_smt_thread_num to 1 on failure. Since on failure
  528. * we need to notify the framework the SMT is not supported, but
  529. * max_smt_thread_num can be initialized to the SMT thread number
  530. * of the cores which are successfully parsed.
  531. */
  532. if (ret)
  533. max_smt_thread_num = 1;
  534. cpu_smt_set_num_threads(max_smt_thread_num, max_smt_thread_num);
  535. return ret;
  536. }
  537. static int __init parse_dt_topology(void)
  538. {
  539. int ret = 0;
  540. int cpu;
  541. struct device_node *cn __free(device_node) =
  542. of_find_node_by_path("/cpus");
  543. if (!cn) {
  544. pr_err("No CPU information found in DT\n");
  545. return 0;
  546. }
  547. /*
  548. * When topology is provided cpu-map is essentially a root
  549. * cluster with restricted subnodes.
  550. */
  551. struct device_node *map __free(device_node) =
  552. of_get_child_by_name(cn, "cpu-map");
  553. if (!map)
  554. return ret;
  555. ret = parse_socket(map);
  556. if (ret != 0)
  557. return ret;
  558. topology_normalize_cpu_scale();
  559. /*
  560. * Check that all cores are in the topology; the SMP code will
  561. * only mark cores described in the DT as possible.
  562. */
  563. for_each_possible_cpu(cpu)
  564. if (cpu_topology[cpu].package_id < 0) {
  565. return -EINVAL;
  566. }
  567. return ret;
  568. }
  569. #endif
  570. /*
  571. * cpu topology table
  572. */
  573. struct cpu_topology cpu_topology[NR_CPUS];
  574. EXPORT_SYMBOL_GPL(cpu_topology);
  575. const struct cpumask *cpu_coregroup_mask(int cpu)
  576. {
  577. const cpumask_t *core_mask = cpumask_of_node(cpu_to_node(cpu));
  578. /* Find the smaller of NUMA, core or LLC siblings */
  579. if (cpumask_subset(&cpu_topology[cpu].core_sibling, core_mask)) {
  580. /* not numa in package, lets use the package siblings */
  581. core_mask = &cpu_topology[cpu].core_sibling;
  582. }
  583. if (last_level_cache_is_valid(cpu)) {
  584. if (cpumask_subset(&cpu_topology[cpu].llc_sibling, core_mask))
  585. core_mask = &cpu_topology[cpu].llc_sibling;
  586. }
  587. /*
  588. * For systems with no shared cpu-side LLC but with clusters defined,
  589. * extend core_mask to cluster_siblings. The sched domain builder will
  590. * then remove MC as redundant with CLS if SCHED_CLUSTER is enabled.
  591. */
  592. if (IS_ENABLED(CONFIG_SCHED_CLUSTER) &&
  593. cpumask_subset(core_mask, &cpu_topology[cpu].cluster_sibling))
  594. core_mask = &cpu_topology[cpu].cluster_sibling;
  595. return core_mask;
  596. }
  597. const struct cpumask *cpu_clustergroup_mask(int cpu)
  598. {
  599. /*
  600. * Forbid cpu_clustergroup_mask() to span more or the same CPUs as
  601. * cpu_coregroup_mask().
  602. */
  603. if (cpumask_subset(cpu_coregroup_mask(cpu),
  604. &cpu_topology[cpu].cluster_sibling))
  605. return topology_sibling_cpumask(cpu);
  606. return &cpu_topology[cpu].cluster_sibling;
  607. }
  608. void update_siblings_masks(unsigned int cpuid)
  609. {
  610. struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
  611. int cpu, ret;
  612. ret = detect_cache_attributes(cpuid);
  613. if (ret && ret != -ENOENT)
  614. pr_info("Early cacheinfo allocation failed, ret = %d\n", ret);
  615. /* update core and thread sibling masks */
  616. for_each_online_cpu(cpu) {
  617. cpu_topo = &cpu_topology[cpu];
  618. if (last_level_cache_is_shared(cpu, cpuid)) {
  619. cpumask_set_cpu(cpu, &cpuid_topo->llc_sibling);
  620. cpumask_set_cpu(cpuid, &cpu_topo->llc_sibling);
  621. }
  622. if (cpuid_topo->package_id != cpu_topo->package_id)
  623. continue;
  624. cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
  625. cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
  626. if (cpuid_topo->cluster_id != cpu_topo->cluster_id)
  627. continue;
  628. if (cpuid_topo->cluster_id >= 0) {
  629. cpumask_set_cpu(cpu, &cpuid_topo->cluster_sibling);
  630. cpumask_set_cpu(cpuid, &cpu_topo->cluster_sibling);
  631. }
  632. if (cpuid_topo->core_id != cpu_topo->core_id)
  633. continue;
  634. cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
  635. cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
  636. }
  637. }
  638. static void clear_cpu_topology(int cpu)
  639. {
  640. struct cpu_topology *cpu_topo = &cpu_topology[cpu];
  641. cpumask_clear(&cpu_topo->llc_sibling);
  642. cpumask_set_cpu(cpu, &cpu_topo->llc_sibling);
  643. cpumask_clear(&cpu_topo->cluster_sibling);
  644. cpumask_set_cpu(cpu, &cpu_topo->cluster_sibling);
  645. cpumask_clear(&cpu_topo->core_sibling);
  646. cpumask_set_cpu(cpu, &cpu_topo->core_sibling);
  647. cpumask_clear(&cpu_topo->thread_sibling);
  648. cpumask_set_cpu(cpu, &cpu_topo->thread_sibling);
  649. }
  650. void __init reset_cpu_topology(void)
  651. {
  652. unsigned int cpu;
  653. for_each_possible_cpu(cpu) {
  654. struct cpu_topology *cpu_topo = &cpu_topology[cpu];
  655. cpu_topo->thread_id = -1;
  656. cpu_topo->core_id = -1;
  657. cpu_topo->cluster_id = -1;
  658. cpu_topo->package_id = -1;
  659. clear_cpu_topology(cpu);
  660. }
  661. }
  662. void remove_cpu_topology(unsigned int cpu)
  663. {
  664. int sibling;
  665. for_each_cpu(sibling, topology_core_cpumask(cpu))
  666. cpumask_clear_cpu(cpu, topology_core_cpumask(sibling));
  667. for_each_cpu(sibling, topology_sibling_cpumask(cpu))
  668. cpumask_clear_cpu(cpu, topology_sibling_cpumask(sibling));
  669. for_each_cpu(sibling, topology_cluster_cpumask(cpu))
  670. cpumask_clear_cpu(cpu, topology_cluster_cpumask(sibling));
  671. for_each_cpu(sibling, topology_llc_cpumask(cpu))
  672. cpumask_clear_cpu(cpu, topology_llc_cpumask(sibling));
  673. clear_cpu_topology(cpu);
  674. }
  675. #if defined(CONFIG_ARM64) || defined(CONFIG_RISCV)
  676. struct cpu_smt_info {
  677. unsigned int thread_num;
  678. int core_id;
  679. };
  680. static bool __init acpi_cpu_is_threaded(int cpu)
  681. {
  682. int is_threaded = acpi_pptt_cpu_is_thread(cpu);
  683. /*
  684. * if the PPTT doesn't have thread information, check for architecture
  685. * specific fallback if available
  686. */
  687. if (is_threaded < 0)
  688. is_threaded = arch_cpu_is_threaded();
  689. return !!is_threaded;
  690. }
  691. /*
  692. * Propagate the topology information of the processor_topology_node tree to the
  693. * cpu_topology array.
  694. */
  695. __weak int __init parse_acpi_topology(void)
  696. {
  697. unsigned int max_smt_thread_num = 1;
  698. struct cpu_smt_info *entry;
  699. struct xarray hetero_cpu;
  700. unsigned long hetero_id;
  701. int cpu, topology_id;
  702. if (acpi_disabled)
  703. return 0;
  704. xa_init(&hetero_cpu);
  705. for_each_possible_cpu(cpu) {
  706. topology_id = find_acpi_cpu_topology(cpu, 0);
  707. if (topology_id < 0)
  708. return topology_id;
  709. if (acpi_cpu_is_threaded(cpu)) {
  710. cpu_topology[cpu].thread_id = topology_id;
  711. topology_id = find_acpi_cpu_topology(cpu, 1);
  712. cpu_topology[cpu].core_id = topology_id;
  713. /*
  714. * In the PPTT, CPUs below a node with the 'identical
  715. * implementation' flag have the same number of threads.
  716. * Count the number of threads for only one CPU (i.e.
  717. * one core_id) among those with the same hetero_id.
  718. * See the comment of find_acpi_cpu_topology_hetero_id()
  719. * for more details.
  720. *
  721. * One entry is created for each node having:
  722. * - the 'identical implementation' flag
  723. * - its parent not having the flag
  724. */
  725. hetero_id = find_acpi_cpu_topology_hetero_id(cpu);
  726. entry = xa_load(&hetero_cpu, hetero_id);
  727. if (!entry) {
  728. entry = kzalloc_obj(*entry);
  729. WARN_ON_ONCE(!entry);
  730. if (entry) {
  731. entry->core_id = topology_id;
  732. entry->thread_num = 1;
  733. xa_store(&hetero_cpu, hetero_id,
  734. entry, GFP_KERNEL);
  735. }
  736. } else if (entry->core_id == topology_id) {
  737. entry->thread_num++;
  738. }
  739. } else {
  740. cpu_topology[cpu].thread_id = -1;
  741. cpu_topology[cpu].core_id = topology_id;
  742. }
  743. topology_id = find_acpi_cpu_topology_cluster(cpu);
  744. cpu_topology[cpu].cluster_id = topology_id;
  745. topology_id = find_acpi_cpu_topology_package(cpu);
  746. cpu_topology[cpu].package_id = topology_id;
  747. }
  748. /*
  749. * This is a short loop since the number of XArray elements is the
  750. * number of heterogeneous CPU clusters. On a homogeneous system
  751. * there's only one entry in the XArray.
  752. */
  753. xa_for_each(&hetero_cpu, hetero_id, entry) {
  754. max_smt_thread_num = max(max_smt_thread_num, entry->thread_num);
  755. xa_erase(&hetero_cpu, hetero_id);
  756. kfree(entry);
  757. }
  758. cpu_smt_set_num_threads(max_smt_thread_num, max_smt_thread_num);
  759. xa_destroy(&hetero_cpu);
  760. return 0;
  761. }
  762. void __init init_cpu_topology(void)
  763. {
  764. int cpu, ret;
  765. reset_cpu_topology();
  766. ret = parse_acpi_topology();
  767. if (!ret)
  768. ret = of_have_populated_dt() && parse_dt_topology();
  769. if (ret) {
  770. /*
  771. * Discard anything that was parsed if we hit an error so we
  772. * don't use partial information. But do not return yet to give
  773. * arch-specific early cache level detection a chance to run.
  774. */
  775. reset_cpu_topology();
  776. }
  777. for_each_possible_cpu(cpu) {
  778. ret = fetch_cache_info(cpu);
  779. if (!ret)
  780. continue;
  781. else if (ret != -ENOENT)
  782. pr_err("Early cacheinfo failed, ret = %d\n", ret);
  783. return;
  784. }
  785. }
  786. void store_cpu_topology(unsigned int cpuid)
  787. {
  788. struct cpu_topology *cpuid_topo = &cpu_topology[cpuid];
  789. if (cpuid_topo->package_id != -1)
  790. goto topology_populated;
  791. cpuid_topo->thread_id = -1;
  792. cpuid_topo->core_id = cpuid;
  793. cpuid_topo->package_id = cpu_to_node(cpuid);
  794. pr_debug("CPU%u: package %d core %d thread %d\n",
  795. cpuid, cpuid_topo->package_id, cpuid_topo->core_id,
  796. cpuid_topo->thread_id);
  797. topology_populated:
  798. update_siblings_masks(cpuid);
  799. }
  800. #endif