^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Arch specific cpu topology information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2016, ARM Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Written by: Juri Lelli, ARM Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/cpufreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/sched/topology.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/cpuset.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/cpumask.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/percpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <trace/hooks/topology.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) bool topology_scale_freq_invariant(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return cpufreq_supports_freq_invariance() ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) arch_freq_counters_available(cpu_online_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) __weak bool arch_freq_counters_available(const struct cpumask *cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) DEFINE_PER_CPU(unsigned long, freq_scale) = SCHED_CAPACITY_SCALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) EXPORT_PER_CPU_SYMBOL_GPL(freq_scale);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) void topology_set_freq_scale(const struct cpumask *cpus, unsigned long cur_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) unsigned long max_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) unsigned long scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (WARN_ON_ONCE(!cur_freq || !max_freq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * If the use of counters for FIE is enabled, just return as we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * want to update the scale factor with information from CPUFREQ.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * Instead the scale factor will be updated from arch_scale_freq_tick.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (arch_freq_counters_available(cpus))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) scale = (cur_freq << SCHED_CAPACITY_SHIFT) / max_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) trace_android_vh_arch_set_freq_scale(cpus, cur_freq, max_freq, &scale);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) for_each_cpu(i, cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) per_cpu(freq_scale, i) = scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) EXPORT_PER_CPU_SYMBOL_GPL(cpu_scale);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) per_cpu(cpu_scale, cpu) = capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) DEFINE_PER_CPU(unsigned long, thermal_pressure);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) EXPORT_PER_CPU_SYMBOL_GPL(thermal_pressure);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) void topology_set_thermal_pressure(const struct cpumask *cpus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) unsigned long th_pressure)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) for_each_cpu(cpu, cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) WRITE_ONCE(per_cpu(thermal_pressure, cpu), th_pressure);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) EXPORT_SYMBOL_GPL(topology_set_thermal_pressure);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static ssize_t cpu_capacity_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct cpu *cpu = container_of(dev, struct cpu, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return sysfs_emit(buf, "%lu\n", topology_get_cpu_scale(cpu->dev.id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static void update_topology_flags_workfn(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static DECLARE_WORK(update_topology_flags_work, update_topology_flags_workfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static DEVICE_ATTR_RO(cpu_capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static int register_cpu_capacity_sysctl(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct device *cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) for_each_possible_cpu(i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) cpu = get_cpu_device(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (!cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) pr_err("%s: too early to get CPU%d device!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) __func__, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) device_create_file(cpu, &dev_attr_cpu_capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) subsys_initcall(register_cpu_capacity_sysctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int update_topology;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) bool topology_update_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) EXPORT_SYMBOL_GPL(topology_update_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) int topology_update_cpu_topology(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return update_topology;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * Updating the sched_domains can't be done directly from cpufreq callbacks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * due to locking, so queue the work for later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static void update_topology_flags_workfn(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) update_topology = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) rebuild_sched_domains();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) topology_update_done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) trace_android_vh_update_topology_flags_workfn(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) pr_debug("sched_domain hierarchy rebuilt, flags updated\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) update_topology = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static DEFINE_PER_CPU(u32, freq_factor) = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static u32 *raw_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static int free_raw_capacity(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) kfree(raw_capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) raw_capacity = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) void topology_normalize_cpu_scale(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) u64 capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) u64 capacity_scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (!raw_capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) capacity_scale = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) capacity = raw_capacity[cpu] * per_cpu(freq_factor, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) capacity_scale = max(capacity, capacity_scale);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) pr_debug("cpu_capacity: capacity_scale=%llu\n", capacity_scale);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) capacity = raw_capacity[cpu] * per_cpu(freq_factor, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) capacity = div64_u64(capacity << SCHED_CAPACITY_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) capacity_scale);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) topology_set_cpu_scale(cpu, capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) cpu, topology_get_cpu_scale(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) bool __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) struct clk *cpu_clk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static bool cap_parsing_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) u32 cpu_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (cap_parsing_failed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) &cpu_capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (!raw_capacity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) raw_capacity = kcalloc(num_possible_cpus(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) sizeof(*raw_capacity),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (!raw_capacity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) cap_parsing_failed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) raw_capacity[cpu] = cpu_capacity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) cpu_node, raw_capacity[cpu]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * Update freq_factor for calculating early boot cpu capacities.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * For non-clk CPU DVFS mechanism, there's no way to get the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * frequency value now, assuming they are running at the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * frequency (by keeping the initial freq_factor value).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) cpu_clk = of_clk_get(cpu_node, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (!PTR_ERR_OR_ZERO(cpu_clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) per_cpu(freq_factor, cpu) =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) clk_get_rate(cpu_clk) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) clk_put(cpu_clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (raw_capacity) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) pr_err("cpu_capacity: missing %pOF raw capacity\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) cpu_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) cap_parsing_failed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) free_raw_capacity();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return !ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) #ifdef CONFIG_CPU_FREQ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static cpumask_var_t cpus_to_visit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static void parsing_done_workfn(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) init_cpu_capacity_callback(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) unsigned long val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct cpufreq_policy *policy = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (!raw_capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (val != CPUFREQ_CREATE_POLICY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) cpumask_pr_args(policy->related_cpus),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) cpumask_pr_args(cpus_to_visit));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) for_each_cpu(cpu, policy->related_cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) per_cpu(freq_factor, cpu) = policy->cpuinfo.max_freq / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (cpumask_empty(cpus_to_visit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) topology_normalize_cpu_scale();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) schedule_work(&update_topology_flags_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) free_raw_capacity();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) pr_debug("cpu_capacity: parsing done\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) schedule_work(&parsing_done_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static struct notifier_block init_cpu_capacity_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) .notifier_call = init_cpu_capacity_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static int __init register_cpufreq_notifier(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * on ACPI-based systems we need to use the default cpu capacity
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * until we have the necessary code to parse the cpu capacity, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * skip registering cpufreq notifier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (!acpi_disabled || !raw_capacity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) cpumask_copy(cpus_to_visit, cpu_possible_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) ret = cpufreq_register_notifier(&init_cpu_capacity_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) CPUFREQ_POLICY_NOTIFIER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) free_cpumask_var(cpus_to_visit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) core_initcall(register_cpufreq_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static void parsing_done_workfn(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) CPUFREQ_POLICY_NOTIFIER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) free_cpumask_var(cpus_to_visit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) core_initcall(free_raw_capacity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) #if defined(CONFIG_ARM64) || defined(CONFIG_RISCV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * This function returns the logic cpu number of the node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * There are basically three kinds of return values:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * (1) logic cpu number which is > 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * (2) -ENODEV when the device tree(DT) node is valid and found in the DT but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * there is no possible logical CPU in the kernel to match. This happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * when CONFIG_NR_CPUS is configure to be smaller than the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * CPU nodes in DT. We need to just ignore this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * (3) -1 if the node does not exist in the device tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static int __init get_cpu_for_node(struct device_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) struct device_node *cpu_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) cpu_node = of_parse_phandle(node, "cpu", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (!cpu_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) cpu = of_cpu_node_to_id(cpu_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (cpu >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) topology_parse_cpu_capacity(cpu_node, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) pr_info("CPU node for %pOF exist but the possible cpu range is :%*pbl\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) cpu_node, cpumask_pr_args(cpu_possible_mask));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) of_node_put(cpu_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static int __init parse_core(struct device_node *core, int package_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) int core_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) char name[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) bool leaf = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) struct device_node *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) snprintf(name, sizeof(name), "thread%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) t = of_get_child_by_name(core, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) leaf = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) cpu = get_cpu_for_node(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (cpu >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) cpu_topology[cpu].package_id = package_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) cpu_topology[cpu].core_id = core_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) cpu_topology[cpu].thread_id = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) } else if (cpu != -ENODEV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) pr_err("%pOF: Can't get CPU for thread\n", t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) of_node_put(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) of_node_put(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) } while (t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) cpu = get_cpu_for_node(core);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (cpu >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (!leaf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) pr_err("%pOF: Core has both threads and CPU\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) core);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) cpu_topology[cpu].package_id = package_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) cpu_topology[cpu].core_id = core_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) } else if (leaf && cpu != -ENODEV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) pr_err("%pOF: Can't get CPU for leaf core\n", core);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static int __init parse_cluster(struct device_node *cluster, int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) char name[20];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) bool leaf = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) bool has_cores = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) struct device_node *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static int package_id __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) int core_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * First check for child clusters; we currently ignore any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * information about the nesting of clusters and present the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * scheduler with a flat list of them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) snprintf(name, sizeof(name), "cluster%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) c = of_get_child_by_name(cluster, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) leaf = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) ret = parse_cluster(c, depth + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) of_node_put(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) } while (c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) /* Now check for cores */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) snprintf(name, sizeof(name), "core%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) c = of_get_child_by_name(cluster, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) has_cores = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (depth == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) pr_err("%pOF: cpu-map children should be clusters\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) of_node_put(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) if (leaf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) ret = parse_core(c, package_id, core_id++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) pr_err("%pOF: Non-leaf cluster with core %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) cluster, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) of_node_put(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) } while (c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (leaf && !has_cores)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) pr_warn("%pOF: empty cluster\n", cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) if (leaf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) package_id++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) static int __init parse_dt_topology(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) struct device_node *cn, *map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) cn = of_find_node_by_path("/cpus");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) if (!cn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) pr_err("No CPU information found in DT\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * When topology is provided cpu-map is essentially a root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * cluster with restricted subnodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) map = of_get_child_by_name(cn, "cpu-map");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (!map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) ret = parse_cluster(map, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) if (ret != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) goto out_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) topology_normalize_cpu_scale();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * Check that all cores are in the topology; the SMP code will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * only mark cores described in the DT as possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) for_each_possible_cpu(cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (cpu_topology[cpu].package_id == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) out_map:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) of_node_put(map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) of_node_put(cn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) * cpu topology table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) struct cpu_topology cpu_topology[NR_CPUS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) EXPORT_SYMBOL_GPL(cpu_topology);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) const struct cpumask *cpu_coregroup_mask(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) const cpumask_t *core_mask = cpumask_of_node(cpu_to_node(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) /* Find the smaller of NUMA, core or LLC siblings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (cpumask_subset(&cpu_topology[cpu].core_sibling, core_mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) /* not numa in package, lets use the package siblings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) core_mask = &cpu_topology[cpu].core_sibling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (cpu_topology[cpu].llc_id != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (cpumask_subset(&cpu_topology[cpu].llc_sibling, core_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) core_mask = &cpu_topology[cpu].llc_sibling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) return core_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) void update_siblings_masks(unsigned int cpuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) struct cpu_topology *cpu_topo, *cpuid_topo = &cpu_topology[cpuid];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) /* update core and thread sibling masks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) cpu_topo = &cpu_topology[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if (cpuid_topo->llc_id == cpu_topo->llc_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) cpumask_set_cpu(cpu, &cpuid_topo->llc_sibling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) cpumask_set_cpu(cpuid, &cpu_topo->llc_sibling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (cpuid_topo->package_id != cpu_topo->package_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) cpumask_set_cpu(cpuid, &cpu_topo->core_sibling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) cpumask_set_cpu(cpu, &cpuid_topo->core_sibling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (cpuid_topo->core_id != cpu_topo->core_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) cpumask_set_cpu(cpuid, &cpu_topo->thread_sibling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) cpumask_set_cpu(cpu, &cpuid_topo->thread_sibling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) static void clear_cpu_topology(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) struct cpu_topology *cpu_topo = &cpu_topology[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) cpumask_clear(&cpu_topo->llc_sibling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) cpumask_set_cpu(cpu, &cpu_topo->llc_sibling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) cpumask_clear(&cpu_topo->core_sibling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) cpumask_set_cpu(cpu, &cpu_topo->core_sibling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) cpumask_clear(&cpu_topo->thread_sibling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) cpumask_set_cpu(cpu, &cpu_topo->thread_sibling);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) void __init reset_cpu_topology(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) unsigned int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) struct cpu_topology *cpu_topo = &cpu_topology[cpu];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) cpu_topo->thread_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) cpu_topo->core_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) cpu_topo->package_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) cpu_topo->llc_id = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) clear_cpu_topology(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) void remove_cpu_topology(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) int sibling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) for_each_cpu(sibling, topology_core_cpumask(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) cpumask_clear_cpu(cpu, topology_core_cpumask(sibling));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) for_each_cpu(sibling, topology_sibling_cpumask(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) cpumask_clear_cpu(cpu, topology_sibling_cpumask(sibling));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) for_each_cpu(sibling, topology_llc_cpumask(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) cpumask_clear_cpu(cpu, topology_llc_cpumask(sibling));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) clear_cpu_topology(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) __weak int __init parse_acpi_topology(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) #if defined(CONFIG_ARM64) || defined(CONFIG_RISCV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) void __init init_cpu_topology(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) reset_cpu_topology();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) * Discard anything that was parsed if we hit an error so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) * don't use partial information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) if (parse_acpi_topology())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) reset_cpu_topology();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) else if (of_have_populated_dt() && parse_dt_topology())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) reset_cpu_topology();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) #endif