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 = kzalloc_objs(*raw_capacity, 284 num_possible_cpus()); 285 if (!raw_capacity) { 286 cap_parsing_failed = true; 287 return false; 288 } 289 } 290 raw_capacity[cpu] = cpu_capacity; 291 pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n", 292 cpu_node, raw_capacity[cpu]); 293 294 /* 295 * Update capacity_freq_ref for calculating early boot CPU capacities. 296 * For non-clk CPU DVFS mechanism, there's no way to get the 297 * frequency value now, assuming they are running at the same 298 * frequency (by keeping the initial capacity_freq_ref value). 299 */ 300 cpu_clk = of_clk_get(cpu_node, 0); 301 if (!IS_ERR_OR_NULL(cpu_clk)) { 302 per_cpu(capacity_freq_ref, cpu) = 303 clk_get_rate(cpu_clk) / HZ_PER_KHZ; 304 clk_put(cpu_clk); 305 } 306 } else { 307 if (raw_capacity) { 308 pr_err("cpu_capacity: missing %pOF raw capacity\n", 309 cpu_node); 310 pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n"); 311 } 312 cap_parsing_failed = true; 313 free_raw_capacity(); 314 } 315 316 return !ret; 317 } 318 319 void __weak freq_inv_set_max_ratio(int cpu, u64 max_rate) 320 { 321 } 322 323 #ifdef CONFIG_ACPI_CPPC_LIB 324 #include <acpi/cppc_acpi.h> 325 326 static inline void topology_init_cpu_capacity_cppc(void) 327 { 328 u64 capacity, capacity_scale = 0; 329 struct cppc_perf_caps perf_caps; 330 int cpu; 331 332 if (likely(!acpi_cpc_valid())) 333 return; 334 335 raw_capacity = kzalloc_objs(*raw_capacity, num_possible_cpus()); 336 if (!raw_capacity) 337 return; 338 339 for_each_possible_cpu(cpu) { 340 if (!cppc_get_perf_caps(cpu, &perf_caps) && 341 (perf_caps.highest_perf >= perf_caps.nominal_perf) && 342 (perf_caps.highest_perf >= perf_caps.lowest_perf)) { 343 raw_capacity[cpu] = perf_caps.highest_perf; 344 capacity_scale = max_t(u64, capacity_scale, raw_capacity[cpu]); 345 346 per_cpu(capacity_freq_ref, cpu) = cppc_perf_to_khz(&perf_caps, raw_capacity[cpu]); 347 348 pr_debug("cpu_capacity: CPU%d cpu_capacity=%u (raw).\n", 349 cpu, raw_capacity[cpu]); 350 continue; 351 } 352 353 pr_err("cpu_capacity: CPU%d missing/invalid highest performance.\n", cpu); 354 pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n"); 355 goto exit; 356 } 357 358 for_each_possible_cpu(cpu) { 359 freq_inv_set_max_ratio(cpu, 360 per_cpu(capacity_freq_ref, cpu) * HZ_PER_KHZ); 361 362 capacity = raw_capacity[cpu]; 363 capacity = div64_u64(capacity << SCHED_CAPACITY_SHIFT, 364 capacity_scale); 365 topology_set_cpu_scale(cpu, capacity); 366 pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n", 367 cpu, topology_get_cpu_scale(cpu)); 368 } 369 370 schedule_work(&update_topology_flags_work); 371 pr_debug("cpu_capacity: cpu_capacity initialization done\n"); 372 373 exit: 374 free_raw_capacity(); 375 } 376 void acpi_processor_init_invariance_cppc(void) 377 { 378 topology_init_cpu_capacity_cppc(); 379 } 380 #endif 381 382 #ifdef CONFIG_CPU_FREQ 383 static cpumask_var_t cpus_to_visit; 384 static void parsing_done_workfn(struct work_struct *work); 385 static DECLARE_WORK(parsing_done_work, parsing_done_workfn); 386 387 static int 388 init_cpu_capacity_callback(struct notifier_block *nb, 389 unsigned long val, 390 void *data) 391 { 392 struct cpufreq_policy *policy = data; 393 int cpu; 394 395 if (val != CPUFREQ_CREATE_POLICY) 396 return 0; 397 398 pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n", 399 cpumask_pr_args(policy->related_cpus), 400 cpumask_pr_args(cpus_to_visit)); 401 402 cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus); 403 404 for_each_cpu(cpu, policy->related_cpus) { 405 per_cpu(capacity_freq_ref, cpu) = policy->cpuinfo.max_freq; 406 freq_inv_set_max_ratio(cpu, 407 per_cpu(capacity_freq_ref, cpu) * HZ_PER_KHZ); 408 } 409 410 if (cpumask_empty(cpus_to_visit)) { 411 if (raw_capacity) { 412 topology_normalize_cpu_scale(); 413 schedule_work(&update_topology_flags_work); 414 free_raw_capacity(); 415 } 416 pr_debug("cpu_capacity: parsing done\n"); 417 schedule_work(&parsing_done_work); 418 } 419 420 return 0; 421 } 422 423 static struct notifier_block init_cpu_capacity_notifier = { 424 .notifier_call = init_cpu_capacity_callback, 425 }; 426 427 static int __init register_cpufreq_notifier(void) 428 { 429 int ret; 430 431 /* 432 * On ACPI-based systems skip registering cpufreq notifier as cpufreq 433 * information is not needed for cpu capacity initialization. 434 */ 435 if (!acpi_disabled) 436 return -EINVAL; 437 438 if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) 439 return -ENOMEM; 440 441 cpumask_copy(cpus_to_visit, cpu_possible_mask); 442 443 ret = cpufreq_register_notifier(&init_cpu_capacity_notifier, 444 CPUFREQ_POLICY_NOTIFIER); 445 446 if (ret) 447 free_cpumask_var(cpus_to_visit); 448 449 return ret; 450 } 451 core_initcall(register_cpufreq_notifier); 452 453 static void parsing_done_workfn(struct work_struct *work) 454 { 455 cpufreq_unregister_notifier(&init_cpu_capacity_notifier, 456 CPUFREQ_POLICY_NOTIFIER); 457 free_cpumask_var(cpus_to_visit); 458 } 459 460 #else 461 core_initcall(free_raw_capacity); 462 #endif 463 464 #if defined(CONFIG_ARM64) || defined(CONFIG_RISCV) 465 466 /* Used to enable the SMT control */ 467 static unsigned int max_smt_thread_num = 1; 468 469 /* 470 * This function returns the logic cpu number of the node. 471 * There are basically three kinds of return values: 472 * (1) logic cpu number which is > 0. 473 * (2) -ENODEV when the device tree(DT) node is valid and found in the DT but 474 * there is no possible logical CPU in the kernel to match. This happens 475 * when CONFIG_NR_CPUS is configure to be smaller than the number of 476 * CPU nodes in DT. We need to just ignore this case. 477 * (3) -1 if the node does not exist in the device tree 478 */ 479 static int __init get_cpu_for_node(struct device_node *node) 480 { 481 int cpu; 482 struct device_node *cpu_node __free(device_node) = 483 of_parse_phandle(node, "cpu", 0); 484 485 if (!cpu_node) 486 return -1; 487 488 cpu = of_cpu_node_to_id(cpu_node); 489 if (cpu >= 0) 490 topology_parse_cpu_capacity(cpu_node, cpu); 491 else 492 pr_info("CPU node for %pOF exist but the possible cpu range is :%*pbl\n", 493 cpu_node, cpumask_pr_args(cpu_possible_mask)); 494 495 return cpu; 496 } 497 498 static int __init parse_core(struct device_node *core, int package_id, 499 int cluster_id, int core_id) 500 { 501 char name[20]; 502 bool leaf = true; 503 int i = 0; 504 int cpu; 505 506 do { 507 snprintf(name, sizeof(name), "thread%d", i); 508 struct device_node *t __free(device_node) = 509 of_get_child_by_name(core, name); 510 511 if (!t) 512 break; 513 514 leaf = false; 515 cpu = get_cpu_for_node(t); 516 if (cpu >= 0) { 517 cpu_topology[cpu].package_id = package_id; 518 cpu_topology[cpu].cluster_id = cluster_id; 519 cpu_topology[cpu].core_id = core_id; 520 cpu_topology[cpu].thread_id = i; 521 } else if (cpu != -ENODEV) { 522 pr_err("%pOF: Can't get CPU for thread\n", t); 523 return -EINVAL; 524 } 525 i++; 526 } while (1); 527 528 max_smt_thread_num = max_t(unsigned int, max_smt_thread_num, i); 529 530 cpu = get_cpu_for_node(core); 531 if (cpu >= 0) { 532 if (!leaf) { 533 pr_err("%pOF: Core has both threads and CPU\n", 534 core); 535 return -EINVAL; 536 } 537 538 cpu_topology[cpu].package_id = package_id; 539 cpu_topology[cpu].cluster_id = cluster_id; 540 cpu_topology[cpu].core_id = core_id; 541 } else if (leaf && cpu != -ENODEV) { 542 pr_err("%pOF: Can't get CPU for leaf core\n", core); 543 return -EINVAL; 544 } 545 546 return 0; 547 } 548 549 static int __init parse_cluster(struct device_node *cluster, int package_id, 550 int cluster_id, int depth) 551 { 552 char name[20]; 553 bool leaf = true; 554 bool has_cores = false; 555 int core_id = 0; 556 int i, ret; 557 558 /* 559 * First check for child clusters; we currently ignore any 560 * information about the nesting of clusters and present the 561 * scheduler with a flat list of them. 562 */ 563 i = 0; 564 do { 565 snprintf(name, sizeof(name), "cluster%d", i); 566 struct device_node *c __free(device_node) = 567 of_get_child_by_name(cluster, name); 568 569 if (!c) 570 break; 571 572 leaf = false; 573 ret = parse_cluster(c, package_id, i, depth + 1); 574 if (depth > 0) 575 pr_warn("Topology for clusters of clusters not yet supported\n"); 576 if (ret != 0) 577 return ret; 578 i++; 579 } while (1); 580 581 /* Now check for cores */ 582 i = 0; 583 do { 584 snprintf(name, sizeof(name), "core%d", i); 585 struct device_node *c __free(device_node) = 586 of_get_child_by_name(cluster, name); 587 588 if (!c) 589 break; 590 591 has_cores = true; 592 593 if (depth == 0) { 594 pr_err("%pOF: cpu-map children should be clusters\n", c); 595 return -EINVAL; 596 } 597 598 if (leaf) { 599 ret = parse_core(c, package_id, cluster_id, core_id++); 600 if (ret != 0) 601 return ret; 602 } else { 603 pr_err("%pOF: Non-leaf cluster with core %s\n", 604 cluster, name); 605 return -EINVAL; 606 } 607 608 i++; 609 } while (1); 610 611 if (leaf && !has_cores) 612 pr_warn("%pOF: empty cluster\n", cluster); 613 614 return 0; 615 } 616 617 static int __init parse_socket(struct device_node *socket) 618 { 619 char name[20]; 620 bool has_socket = false; 621 int package_id = 0, ret; 622 623 do { 624 snprintf(name, sizeof(name), "socket%d", package_id); 625 struct device_node *c __free(device_node) = 626 of_get_child_by_name(socket, name); 627 628 if (!c) 629 break; 630 631 has_socket = true; 632 ret = parse_cluster(c, package_id, -1, 0); 633 if (ret != 0) 634 return ret; 635 636 package_id++; 637 } while (1); 638 639 if (!has_socket) 640 ret = parse_cluster(socket, 0, -1, 0); 641 642 /* 643 * Reset the max_smt_thread_num to 1 on failure. Since on failure 644 * we need to notify the framework the SMT is not supported, but 645 * max_smt_thread_num can be initialized to the SMT thread number 646 * of the cores which are successfully parsed. 647 */ 648 if (ret) 649 max_smt_thread_num = 1; 650 651 cpu_smt_set_num_threads(max_smt_thread_num, max_smt_thread_num); 652 653 return ret; 654 } 655 656 static int __init parse_dt_topology(void) 657 { 658 int ret = 0; 659 int cpu; 660 struct device_node *cn __free(device_node) = 661 of_find_node_by_path("/cpus"); 662 663 if (!cn) { 664 pr_err("No CPU information found in DT\n"); 665 return 0; 666 } 667 668 /* 669 * When topology is provided cpu-map is essentially a root 670 * cluster with restricted subnodes. 671 */ 672 struct device_node *map __free(device_node) = 673 of_get_child_by_name(cn, "cpu-map"); 674 675 if (!map) 676 return ret; 677 678 ret = parse_socket(map); 679 if (ret != 0) 680 return ret; 681 682 topology_normalize_cpu_scale(); 683 684 /* 685 * Check that all cores are in the topology; the SMP code will 686 * only mark cores described in the DT as possible. 687 */ 688 for_each_possible_cpu(cpu) 689 if (cpu_topology[cpu].package_id < 0) { 690 return -EINVAL; 691 } 692 693 return ret; 694 } 695 #endif 696 697 /* 698 * cpu topology table 699 */ 700 struct cpu_topology cpu_topology[NR_CPUS]; 701 EXPORT_SYMBOL_GPL(cpu_topology); 702 703 const struct cpumask *cpu_coregroup_mask(int cpu) 704 { 705 const cpumask_t *core_mask = cpumask_of_node(cpu_to_node(cpu)); 706 707 /* Find the smaller of NUMA, core or LLC siblings */ 708 if (cpumask_subset(&cpu_topology[cpu].core_sibling, core_mask)) { 709 /* not numa in package, lets use the package siblings */ 710 core_mask = &cpu_topology[cpu].core_sibling; 711 } 712 713 if (last_level_cache_is_valid(cpu)) { 714 if (cpumask_subset(&cpu_topology[cpu].llc_sibling, core_mask)) 715 core_mask = &cpu_topology[cpu].llc_sibling; 716 } 717 718 /* 719 * For systems with no shared cpu-side LLC but with clusters defined, 720 * extend core_mask to cluster_siblings. The sched domain builder will 721 * then remove MC as redundant with CLS if SCHED_CLUSTER is enabled. 722 */ 723 if (IS_ENABLED(CONFIG_SCHED_CLUSTER) && 724 cpumask_subset(core_mask, &cpu_topology[cpu].cluster_sibling)) 725 core_mask = &cpu_topology[cpu].cluster_sibling; 726 727 return core_mask; 728 } 729 730 const struct cpumask *cpu_clustergroup_mask(int cpu) 731 { 732 /* 733 * Forbid cpu_clustergroup_mask() to span more or the same CPUs as 734 * cpu_coregroup_mask(). 735 */ 736 if (cpumask_subset(cpu_coregroup_mask(cpu), 737 &cpu_topology[cpu].cluster_sibling)) 738 return topology_sibling_cpumask(cpu); 739 740 return &cpu_topology[cpu].cluster_sibling; 741 } 742 743 void update_siblings_masks(unsigned int cpuid) 744 { 745 struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid]; 746 int cpu, ret; 747 748 ret = detect_cache_attributes(cpuid); 749 if (ret && ret != -ENOENT) 750 pr_info("Early cacheinfo allocation failed, ret = %d\n", ret); 751 752 /* update core and thread sibling masks */ 753 for_each_online_cpu(cpu) { 754 cpu_topo = &cpu_topology[cpu]; 755 756 if (last_level_cache_is_shared(cpu, cpuid)) { 757 cpumask_set_cpu(cpu, &cpuid_topo->llc_sibling); 758 cpumask_set_cpu(cpuid, &cpu_topo->llc_sibling); 759 } 760 761 if (cpuid_topo->package_id != cpu_topo->package_id) 762 continue; 763 764 cpumask_set_cpu(cpuid, &cpu_topo->core_sibling); 765 cpumask_set_cpu(cpu, &cpuid_topo->core_sibling); 766 767 if (cpuid_topo->cluster_id != cpu_topo->cluster_id) 768 continue; 769 770 if (cpuid_topo->cluster_id >= 0) { 771 cpumask_set_cpu(cpu, &cpuid_topo->cluster_sibling); 772 cpumask_set_cpu(cpuid, &cpu_topo->cluster_sibling); 773 } 774 775 if (cpuid_topo->core_id != cpu_topo->core_id) 776 continue; 777 778 cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling); 779 cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling); 780 } 781 } 782 783 static void clear_cpu_topology(int cpu) 784 { 785 struct cpu_topology *cpu_topo = &cpu_topology[cpu]; 786 787 cpumask_clear(&cpu_topo->llc_sibling); 788 cpumask_set_cpu(cpu, &cpu_topo->llc_sibling); 789 790 cpumask_clear(&cpu_topo->cluster_sibling); 791 cpumask_set_cpu(cpu, &cpu_topo->cluster_sibling); 792 793 cpumask_clear(&cpu_topo->core_sibling); 794 cpumask_set_cpu(cpu, &cpu_topo->core_sibling); 795 cpumask_clear(&cpu_topo->thread_sibling); 796 cpumask_set_cpu(cpu, &cpu_topo->thread_sibling); 797 } 798 799 void __init reset_cpu_topology(void) 800 { 801 unsigned int cpu; 802 803 for_each_possible_cpu(cpu) { 804 struct cpu_topology *cpu_topo = &cpu_topology[cpu]; 805 806 cpu_topo->thread_id = -1; 807 cpu_topo->core_id = -1; 808 cpu_topo->cluster_id = -1; 809 cpu_topo->package_id = -1; 810 811 clear_cpu_topology(cpu); 812 } 813 } 814 815 void remove_cpu_topology(unsigned int cpu) 816 { 817 int sibling; 818 819 for_each_cpu(sibling, topology_core_cpumask(cpu)) 820 cpumask_clear_cpu(cpu, topology_core_cpumask(sibling)); 821 for_each_cpu(sibling, topology_sibling_cpumask(cpu)) 822 cpumask_clear_cpu(cpu, topology_sibling_cpumask(sibling)); 823 for_each_cpu(sibling, topology_cluster_cpumask(cpu)) 824 cpumask_clear_cpu(cpu, topology_cluster_cpumask(sibling)); 825 for_each_cpu(sibling, topology_llc_cpumask(cpu)) 826 cpumask_clear_cpu(cpu, topology_llc_cpumask(sibling)); 827 828 clear_cpu_topology(cpu); 829 } 830 831 #if defined(CONFIG_ARM64) || defined(CONFIG_RISCV) 832 struct cpu_smt_info { 833 unsigned int thread_num; 834 int core_id; 835 }; 836 837 static bool __init acpi_cpu_is_threaded(int cpu) 838 { 839 int is_threaded = acpi_pptt_cpu_is_thread(cpu); 840 841 /* 842 * if the PPTT doesn't have thread information, check for architecture 843 * specific fallback if available 844 */ 845 if (is_threaded < 0) 846 is_threaded = arch_cpu_is_threaded(); 847 848 return !!is_threaded; 849 } 850 851 /* 852 * Propagate the topology information of the processor_topology_node tree to the 853 * cpu_topology array. 854 */ 855 __weak int __init parse_acpi_topology(void) 856 { 857 unsigned int max_smt_thread_num = 1; 858 struct cpu_smt_info *entry; 859 struct xarray hetero_cpu; 860 unsigned long hetero_id; 861 int cpu, topology_id; 862 863 if (acpi_disabled) 864 return 0; 865 866 xa_init(&hetero_cpu); 867 868 for_each_possible_cpu(cpu) { 869 topology_id = find_acpi_cpu_topology(cpu, 0); 870 if (topology_id < 0) 871 return topology_id; 872 873 if (acpi_cpu_is_threaded(cpu)) { 874 cpu_topology[cpu].thread_id = topology_id; 875 topology_id = find_acpi_cpu_topology(cpu, 1); 876 cpu_topology[cpu].core_id = topology_id; 877 878 /* 879 * In the PPTT, CPUs below a node with the 'identical 880 * implementation' flag have the same number of threads. 881 * Count the number of threads for only one CPU (i.e. 882 * one core_id) among those with the same hetero_id. 883 * See the comment of find_acpi_cpu_topology_hetero_id() 884 * for more details. 885 * 886 * One entry is created for each node having: 887 * - the 'identical implementation' flag 888 * - its parent not having the flag 889 */ 890 hetero_id = find_acpi_cpu_topology_hetero_id(cpu); 891 entry = xa_load(&hetero_cpu, hetero_id); 892 if (!entry) { 893 entry = kzalloc_obj(*entry); 894 WARN_ON_ONCE(!entry); 895 896 if (entry) { 897 entry->core_id = topology_id; 898 entry->thread_num = 1; 899 xa_store(&hetero_cpu, hetero_id, 900 entry, GFP_KERNEL); 901 } 902 } else if (entry->core_id == topology_id) { 903 entry->thread_num++; 904 } 905 } else { 906 cpu_topology[cpu].thread_id = -1; 907 cpu_topology[cpu].core_id = topology_id; 908 } 909 topology_id = find_acpi_cpu_topology_cluster(cpu); 910 cpu_topology[cpu].cluster_id = topology_id; 911 topology_id = find_acpi_cpu_topology_package(cpu); 912 cpu_topology[cpu].package_id = topology_id; 913 } 914 915 /* 916 * This is a short loop since the number of XArray elements is the 917 * number of heterogeneous CPU clusters. On a homogeneous system 918 * there's only one entry in the XArray. 919 */ 920 xa_for_each(&hetero_cpu, hetero_id, entry) { 921 max_smt_thread_num = max(max_smt_thread_num, entry->thread_num); 922 xa_erase(&hetero_cpu, hetero_id); 923 kfree(entry); 924 } 925 926 cpu_smt_set_num_threads(max_smt_thread_num, max_smt_thread_num); 927 xa_destroy(&hetero_cpu); 928 return 0; 929 } 930 931 void __init init_cpu_topology(void) 932 { 933 int cpu, ret; 934 935 reset_cpu_topology(); 936 ret = parse_acpi_topology(); 937 if (!ret) 938 ret = of_have_populated_dt() && parse_dt_topology(); 939 940 if (ret) { 941 /* 942 * Discard anything that was parsed if we hit an error so we 943 * don't use partial information. But do not return yet to give 944 * arch-specific early cache level detection a chance to run. 945 */ 946 reset_cpu_topology(); 947 } 948 949 for_each_possible_cpu(cpu) { 950 ret = fetch_cache_info(cpu); 951 if (!ret) 952 continue; 953 else if (ret != -ENOENT) 954 pr_err("Early cacheinfo failed, ret = %d\n", ret); 955 return; 956 } 957 } 958 959 void store_cpu_topology(unsigned int cpuid) 960 { 961 struct cpu_topology *cpuid_topo = &cpu_topology[cpuid]; 962 963 if (cpuid_topo->package_id != -1) 964 goto topology_populated; 965 966 cpuid_topo->thread_id = -1; 967 cpuid_topo->core_id = cpuid; 968 cpuid_topo->package_id = cpu_to_node(cpuid); 969 970 pr_debug("CPU%u: package %d core %d thread %d\n", 971 cpuid, cpuid_topo->package_id, cpuid_topo->core_id, 972 cpuid_topo->thread_id); 973 974 topology_populated: 975 update_siblings_masks(cpuid); 976 } 977 #endif 978