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