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