arch_topology.c 22 KB

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