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