Greg Kroah-Hartman | 6ee97d3 | 2017-11-07 17:30:08 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Arch specific cpu topology information |
| 4 | * |
| 5 | * Copyright (C) 2016, ARM Ltd. |
| 6 | * Written by: Juri Lelli, ARM Ltd. |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/acpi.h> |
| 10 | #include <linux/cpu.h> |
| 11 | #include <linux/cpufreq.h> |
| 12 | #include <linux/device.h> |
| 13 | #include <linux/of.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/string.h> |
| 16 | #include <linux/sched/topology.h> |
Morten Rasmussen | bb1fbdd | 2018-07-20 14:32:32 +0100 | [diff] [blame] | 17 | #include <linux/cpuset.h> |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 18 | |
Dietmar Eggemann | 0e27c56 | 2017-09-26 17:41:10 +0100 | [diff] [blame] | 19 | DEFINE_PER_CPU(unsigned long, freq_scale) = SCHED_CAPACITY_SCALE; |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 20 | |
Dietmar Eggemann | 0e27c56 | 2017-09-26 17:41:10 +0100 | [diff] [blame] | 21 | void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq, |
| 22 | unsigned long max_freq) |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 23 | { |
Dietmar Eggemann | 0e27c56 | 2017-09-26 17:41:10 +0100 | [diff] [blame] | 24 | unsigned long scale; |
| 25 | int i; |
| 26 | |
| 27 | scale = (cur_freq << SCHED_CAPACITY_SHIFT) / max_freq; |
| 28 | |
| 29 | for_each_cpu(i, cpus) |
| 30 | per_cpu(freq_scale, i) = scale; |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Dietmar Eggemann | 8216f58 | 2017-09-26 17:41:11 +0100 | [diff] [blame] | 33 | DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE; |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 34 | |
Juri Lelli | 4ca4f26 | 2017-05-31 17:59:31 +0100 | [diff] [blame] | 35 | void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity) |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 36 | { |
| 37 | per_cpu(cpu_scale, cpu) = capacity; |
| 38 | } |
| 39 | |
| 40 | static ssize_t cpu_capacity_show(struct device *dev, |
| 41 | struct device_attribute *attr, |
| 42 | char *buf) |
| 43 | { |
| 44 | struct cpu *cpu = container_of(dev, struct cpu, dev); |
| 45 | |
Vincent Guittot | 8ec59c0 | 2019-06-17 17:00:17 +0200 | [diff] [blame^] | 46 | return sprintf(buf, "%lu\n", topology_get_cpu_scale(cpu->dev.id)); |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Morten Rasmussen | bb1fbdd | 2018-07-20 14:32:32 +0100 | [diff] [blame] | 49 | static void update_topology_flags_workfn(struct work_struct *work); |
| 50 | static DECLARE_WORK(update_topology_flags_work, update_topology_flags_workfn); |
| 51 | |
Lingutla Chandrasekhar | 5d777b1 | 2019-04-01 09:54:41 +0530 | [diff] [blame] | 52 | static DEVICE_ATTR_RO(cpu_capacity); |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 53 | |
| 54 | static int register_cpu_capacity_sysctl(void) |
| 55 | { |
| 56 | int i; |
| 57 | struct device *cpu; |
| 58 | |
| 59 | for_each_possible_cpu(i) { |
| 60 | cpu = get_cpu_device(i); |
| 61 | if (!cpu) { |
| 62 | pr_err("%s: too early to get CPU%d device!\n", |
| 63 | __func__, i); |
| 64 | continue; |
| 65 | } |
| 66 | device_create_file(cpu, &dev_attr_cpu_capacity); |
| 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | subsys_initcall(register_cpu_capacity_sysctl); |
| 72 | |
Morten Rasmussen | bb1fbdd | 2018-07-20 14:32:32 +0100 | [diff] [blame] | 73 | static int update_topology; |
| 74 | |
| 75 | int topology_update_cpu_topology(void) |
| 76 | { |
| 77 | return update_topology; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Updating the sched_domains can't be done directly from cpufreq callbacks |
| 82 | * due to locking, so queue the work for later. |
| 83 | */ |
| 84 | static void update_topology_flags_workfn(struct work_struct *work) |
| 85 | { |
| 86 | update_topology = 1; |
| 87 | rebuild_sched_domains(); |
| 88 | pr_debug("sched_domain hierarchy rebuilt, flags updated\n"); |
| 89 | update_topology = 0; |
| 90 | } |
| 91 | |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 92 | static u32 capacity_scale; |
| 93 | static u32 *raw_capacity; |
Viresh Kumar | 62de116 | 2017-06-23 14:55:33 +0530 | [diff] [blame] | 94 | |
Prasad Sodagudi | 82d8ba7 | 2017-10-10 00:34:56 -0700 | [diff] [blame] | 95 | static int free_raw_capacity(void) |
Viresh Kumar | 62de116 | 2017-06-23 14:55:33 +0530 | [diff] [blame] | 96 | { |
| 97 | kfree(raw_capacity); |
| 98 | raw_capacity = NULL; |
| 99 | |
| 100 | return 0; |
| 101 | } |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 102 | |
Juri Lelli | 4ca4f26 | 2017-05-31 17:59:31 +0100 | [diff] [blame] | 103 | void topology_normalize_cpu_scale(void) |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 104 | { |
| 105 | u64 capacity; |
| 106 | int cpu; |
| 107 | |
Viresh Kumar | 62de116 | 2017-06-23 14:55:33 +0530 | [diff] [blame] | 108 | if (!raw_capacity) |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 109 | return; |
| 110 | |
| 111 | pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale); |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 112 | for_each_possible_cpu(cpu) { |
| 113 | pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n", |
| 114 | cpu, raw_capacity[cpu]); |
| 115 | capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT) |
| 116 | / capacity_scale; |
Juri Lelli | 4ca4f26 | 2017-05-31 17:59:31 +0100 | [diff] [blame] | 117 | topology_set_cpu_scale(cpu, capacity); |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 118 | pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n", |
Vincent Guittot | 8ec59c0 | 2019-06-17 17:00:17 +0200 | [diff] [blame^] | 119 | cpu, topology_get_cpu_scale(cpu)); |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 120 | } |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Viresh Kumar | 805df29 | 2017-06-23 14:55:32 +0530 | [diff] [blame] | 123 | bool __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu) |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 124 | { |
Viresh Kumar | 62de116 | 2017-06-23 14:55:33 +0530 | [diff] [blame] | 125 | static bool cap_parsing_failed; |
Viresh Kumar | 805df29 | 2017-06-23 14:55:32 +0530 | [diff] [blame] | 126 | int ret; |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 127 | u32 cpu_capacity; |
| 128 | |
| 129 | if (cap_parsing_failed) |
Viresh Kumar | 805df29 | 2017-06-23 14:55:32 +0530 | [diff] [blame] | 130 | return false; |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 131 | |
Viresh Kumar | 3eeba1a | 2017-06-23 14:55:30 +0530 | [diff] [blame] | 132 | ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz", |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 133 | &cpu_capacity); |
| 134 | if (!ret) { |
| 135 | if (!raw_capacity) { |
| 136 | raw_capacity = kcalloc(num_possible_cpus(), |
| 137 | sizeof(*raw_capacity), |
| 138 | GFP_KERNEL); |
| 139 | if (!raw_capacity) { |
| 140 | pr_err("cpu_capacity: failed to allocate memory for raw capacities\n"); |
| 141 | cap_parsing_failed = true; |
Viresh Kumar | 805df29 | 2017-06-23 14:55:32 +0530 | [diff] [blame] | 142 | return false; |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | capacity_scale = max(cpu_capacity, capacity_scale); |
| 146 | raw_capacity[cpu] = cpu_capacity; |
Rob Herring | 6ef2541 | 2017-07-18 16:42:49 -0500 | [diff] [blame] | 147 | pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n", |
| 148 | cpu_node, raw_capacity[cpu]); |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 149 | } else { |
| 150 | if (raw_capacity) { |
Rob Herring | 6ef2541 | 2017-07-18 16:42:49 -0500 | [diff] [blame] | 151 | pr_err("cpu_capacity: missing %pOF raw capacity\n", |
| 152 | cpu_node); |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 153 | pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n"); |
| 154 | } |
| 155 | cap_parsing_failed = true; |
Viresh Kumar | 62de116 | 2017-06-23 14:55:33 +0530 | [diff] [blame] | 156 | free_raw_capacity(); |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | return !ret; |
| 160 | } |
| 161 | |
| 162 | #ifdef CONFIG_CPU_FREQ |
Gaku Inami | 9de9a44 | 2018-02-13 11:06:40 +0900 | [diff] [blame] | 163 | static cpumask_var_t cpus_to_visit; |
| 164 | static void parsing_done_workfn(struct work_struct *work); |
| 165 | static DECLARE_WORK(parsing_done_work, parsing_done_workfn); |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 166 | |
Gaku Inami | 9de9a44 | 2018-02-13 11:06:40 +0900 | [diff] [blame] | 167 | static int |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 168 | init_cpu_capacity_callback(struct notifier_block *nb, |
| 169 | unsigned long val, |
| 170 | void *data) |
| 171 | { |
| 172 | struct cpufreq_policy *policy = data; |
| 173 | int cpu; |
| 174 | |
Viresh Kumar | d8bcf4d | 2017-06-23 14:55:34 +0530 | [diff] [blame] | 175 | if (!raw_capacity) |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 176 | return 0; |
| 177 | |
Viresh Kumar | 93a5708 | 2017-06-23 14:55:31 +0530 | [diff] [blame] | 178 | if (val != CPUFREQ_NOTIFY) |
| 179 | return 0; |
| 180 | |
| 181 | pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n", |
| 182 | cpumask_pr_args(policy->related_cpus), |
| 183 | cpumask_pr_args(cpus_to_visit)); |
| 184 | |
| 185 | cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus); |
| 186 | |
| 187 | for_each_cpu(cpu, policy->related_cpus) { |
Vincent Guittot | 8ec59c0 | 2019-06-17 17:00:17 +0200 | [diff] [blame^] | 188 | raw_capacity[cpu] = topology_get_cpu_scale(cpu) * |
Viresh Kumar | 93a5708 | 2017-06-23 14:55:31 +0530 | [diff] [blame] | 189 | policy->cpuinfo.max_freq / 1000UL; |
| 190 | capacity_scale = max(raw_capacity[cpu], capacity_scale); |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 191 | } |
Viresh Kumar | 93a5708 | 2017-06-23 14:55:31 +0530 | [diff] [blame] | 192 | |
| 193 | if (cpumask_empty(cpus_to_visit)) { |
| 194 | topology_normalize_cpu_scale(); |
Morten Rasmussen | bb1fbdd | 2018-07-20 14:32:32 +0100 | [diff] [blame] | 195 | schedule_work(&update_topology_flags_work); |
Viresh Kumar | 62de116 | 2017-06-23 14:55:33 +0530 | [diff] [blame] | 196 | free_raw_capacity(); |
Viresh Kumar | 93a5708 | 2017-06-23 14:55:31 +0530 | [diff] [blame] | 197 | pr_debug("cpu_capacity: parsing done\n"); |
Viresh Kumar | 93a5708 | 2017-06-23 14:55:31 +0530 | [diff] [blame] | 198 | schedule_work(&parsing_done_work); |
| 199 | } |
| 200 | |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 201 | return 0; |
| 202 | } |
| 203 | |
Gaku Inami | 9de9a44 | 2018-02-13 11:06:40 +0900 | [diff] [blame] | 204 | static struct notifier_block init_cpu_capacity_notifier = { |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 205 | .notifier_call = init_cpu_capacity_callback, |
| 206 | }; |
| 207 | |
| 208 | static int __init register_cpufreq_notifier(void) |
| 209 | { |
Dietmar Eggemann | 5408211 | 2017-09-26 17:41:06 +0100 | [diff] [blame] | 210 | int ret; |
| 211 | |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 212 | /* |
| 213 | * on ACPI-based systems we need to use the default cpu capacity |
| 214 | * until we have the necessary code to parse the cpu capacity, so |
| 215 | * skip registering cpufreq notifier. |
| 216 | */ |
Juri Lelli | c105aa3 | 2017-05-31 17:59:29 +0100 | [diff] [blame] | 217 | if (!acpi_disabled || !raw_capacity) |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 218 | return -EINVAL; |
| 219 | |
| 220 | if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) { |
| 221 | pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n"); |
| 222 | return -ENOMEM; |
| 223 | } |
| 224 | |
| 225 | cpumask_copy(cpus_to_visit, cpu_possible_mask); |
| 226 | |
Dietmar Eggemann | 5408211 | 2017-09-26 17:41:06 +0100 | [diff] [blame] | 227 | ret = cpufreq_register_notifier(&init_cpu_capacity_notifier, |
| 228 | CPUFREQ_POLICY_NOTIFIER); |
| 229 | |
| 230 | if (ret) |
| 231 | free_cpumask_var(cpus_to_visit); |
| 232 | |
| 233 | return ret; |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 234 | } |
| 235 | core_initcall(register_cpufreq_notifier); |
| 236 | |
Gaku Inami | 9de9a44 | 2018-02-13 11:06:40 +0900 | [diff] [blame] | 237 | static void parsing_done_workfn(struct work_struct *work) |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 238 | { |
| 239 | cpufreq_unregister_notifier(&init_cpu_capacity_notifier, |
| 240 | CPUFREQ_POLICY_NOTIFIER); |
Dietmar Eggemann | 5408211 | 2017-09-26 17:41:06 +0100 | [diff] [blame] | 241 | free_cpumask_var(cpus_to_visit); |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | #else |
Juri Lelli | 2ef7a29 | 2017-05-31 17:59:28 +0100 | [diff] [blame] | 245 | core_initcall(free_raw_capacity); |
| 246 | #endif |