arch_topology.c 21 KB

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