blob: edfcf8d982e4186a80a3bf76ab626976bb86d200 [file] [log] [blame]
Greg Kroah-Hartman6ee97d32017-11-07 17:30:08 +01001// SPDX-License-Identifier: GPL-2.0
Juri Lelli2ef7a292017-05-31 17:59:28 +01002/*
3 * Arch specific cpu topology information
4 *
5 * Copyright (C) 2016, ARM Ltd.
6 * Written by: Juri Lelli, ARM Ltd.
Juri Lelli2ef7a292017-05-31 17:59:28 +01007 */
8
9#include <linux/acpi.h>
Juri Lelli615ffd62017-05-31 17:59:30 +010010#include <linux/arch_topology.h>
Juri Lelli2ef7a292017-05-31 17:59:28 +010011#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>
Morten Rasmussenbb1fbdd2018-07-20 14:32:32 +010018#include <linux/cpuset.h>
Juri Lelli2ef7a292017-05-31 17:59:28 +010019
Dietmar Eggemann0e27c562017-09-26 17:41:10 +010020DEFINE_PER_CPU(unsigned long, freq_scale) = SCHED_CAPACITY_SCALE;
Juri Lelli2ef7a292017-05-31 17:59:28 +010021
Dietmar Eggemann0e27c562017-09-26 17:41:10 +010022void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
23 unsigned long max_freq)
Juri Lelli2ef7a292017-05-31 17:59:28 +010024{
Dietmar Eggemann0e27c562017-09-26 17:41:10 +010025 unsigned long scale;
26 int i;
27
28 scale = (cur_freq << SCHED_CAPACITY_SHIFT) / max_freq;
29
30 for_each_cpu(i, cpus)
31 per_cpu(freq_scale, i) = scale;
Juri Lelli2ef7a292017-05-31 17:59:28 +010032}
33
Juri Lelli2ef7a292017-05-31 17:59:28 +010034static DEFINE_MUTEX(cpu_scale_mutex);
Dietmar Eggemann8216f582017-09-26 17:41:11 +010035DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
Juri Lelli2ef7a292017-05-31 17:59:28 +010036
Juri Lelli4ca4f262017-05-31 17:59:31 +010037void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity)
Juri Lelli2ef7a292017-05-31 17:59:28 +010038{
39 per_cpu(cpu_scale, cpu) = capacity;
40}
41
42static ssize_t cpu_capacity_show(struct device *dev,
43 struct device_attribute *attr,
44 char *buf)
45{
46 struct cpu *cpu = container_of(dev, struct cpu, dev);
47
Viresh Kumar3eeba1a2017-06-23 14:55:30 +053048 return sprintf(buf, "%lu\n", topology_get_cpu_scale(NULL, cpu->dev.id));
Juri Lelli2ef7a292017-05-31 17:59:28 +010049}
50
Morten Rasmussenbb1fbdd2018-07-20 14:32:32 +010051static void update_topology_flags_workfn(struct work_struct *work);
52static DECLARE_WORK(update_topology_flags_work, update_topology_flags_workfn);
53
Juri Lelli2ef7a292017-05-31 17:59:28 +010054static ssize_t cpu_capacity_store(struct device *dev,
55 struct device_attribute *attr,
56 const char *buf,
57 size_t count)
58{
59 struct cpu *cpu = container_of(dev, struct cpu, dev);
60 int this_cpu = cpu->dev.id;
61 int i;
62 unsigned long new_capacity;
63 ssize_t ret;
64
65 if (!count)
66 return 0;
67
68 ret = kstrtoul(buf, 0, &new_capacity);
69 if (ret)
70 return ret;
71 if (new_capacity > SCHED_CAPACITY_SCALE)
72 return -EINVAL;
73
74 mutex_lock(&cpu_scale_mutex);
75 for_each_cpu(i, &cpu_topology[this_cpu].core_sibling)
Juri Lelli4ca4f262017-05-31 17:59:31 +010076 topology_set_cpu_scale(i, new_capacity);
Juri Lelli2ef7a292017-05-31 17:59:28 +010077 mutex_unlock(&cpu_scale_mutex);
78
Morten Rasmussenbb1fbdd2018-07-20 14:32:32 +010079 schedule_work(&update_topology_flags_work);
80
Juri Lelli2ef7a292017-05-31 17:59:28 +010081 return count;
82}
83
84static DEVICE_ATTR_RW(cpu_capacity);
85
86static int register_cpu_capacity_sysctl(void)
87{
88 int i;
89 struct device *cpu;
90
91 for_each_possible_cpu(i) {
92 cpu = get_cpu_device(i);
93 if (!cpu) {
94 pr_err("%s: too early to get CPU%d device!\n",
95 __func__, i);
96 continue;
97 }
98 device_create_file(cpu, &dev_attr_cpu_capacity);
99 }
100
101 return 0;
102}
103subsys_initcall(register_cpu_capacity_sysctl);
104
Morten Rasmussenbb1fbdd2018-07-20 14:32:32 +0100105static int update_topology;
106
107int topology_update_cpu_topology(void)
108{
109 return update_topology;
110}
111
112/*
113 * Updating the sched_domains can't be done directly from cpufreq callbacks
114 * due to locking, so queue the work for later.
115 */
116static void update_topology_flags_workfn(struct work_struct *work)
117{
118 update_topology = 1;
119 rebuild_sched_domains();
120 pr_debug("sched_domain hierarchy rebuilt, flags updated\n");
121 update_topology = 0;
122}
123
Juri Lelli2ef7a292017-05-31 17:59:28 +0100124static u32 capacity_scale;
125static u32 *raw_capacity;
Viresh Kumar62de1162017-06-23 14:55:33 +0530126
Prasad Sodagudi82d8ba72017-10-10 00:34:56 -0700127static int free_raw_capacity(void)
Viresh Kumar62de1162017-06-23 14:55:33 +0530128{
129 kfree(raw_capacity);
130 raw_capacity = NULL;
131
132 return 0;
133}
Juri Lelli2ef7a292017-05-31 17:59:28 +0100134
Juri Lelli4ca4f262017-05-31 17:59:31 +0100135void topology_normalize_cpu_scale(void)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100136{
137 u64 capacity;
138 int cpu;
139
Viresh Kumar62de1162017-06-23 14:55:33 +0530140 if (!raw_capacity)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100141 return;
142
143 pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale);
144 mutex_lock(&cpu_scale_mutex);
145 for_each_possible_cpu(cpu) {
146 pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n",
147 cpu, raw_capacity[cpu]);
148 capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT)
149 / capacity_scale;
Juri Lelli4ca4f262017-05-31 17:59:31 +0100150 topology_set_cpu_scale(cpu, capacity);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100151 pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
Juri Lelli4ca4f262017-05-31 17:59:31 +0100152 cpu, topology_get_cpu_scale(NULL, cpu));
Juri Lelli2ef7a292017-05-31 17:59:28 +0100153 }
154 mutex_unlock(&cpu_scale_mutex);
155}
156
Viresh Kumar805df292017-06-23 14:55:32 +0530157bool __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100158{
Viresh Kumar62de1162017-06-23 14:55:33 +0530159 static bool cap_parsing_failed;
Viresh Kumar805df292017-06-23 14:55:32 +0530160 int ret;
Juri Lelli2ef7a292017-05-31 17:59:28 +0100161 u32 cpu_capacity;
162
163 if (cap_parsing_failed)
Viresh Kumar805df292017-06-23 14:55:32 +0530164 return false;
Juri Lelli2ef7a292017-05-31 17:59:28 +0100165
Viresh Kumar3eeba1a2017-06-23 14:55:30 +0530166 ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz",
Juri Lelli2ef7a292017-05-31 17:59:28 +0100167 &cpu_capacity);
168 if (!ret) {
169 if (!raw_capacity) {
170 raw_capacity = kcalloc(num_possible_cpus(),
171 sizeof(*raw_capacity),
172 GFP_KERNEL);
173 if (!raw_capacity) {
174 pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
175 cap_parsing_failed = true;
Viresh Kumar805df292017-06-23 14:55:32 +0530176 return false;
Juri Lelli2ef7a292017-05-31 17:59:28 +0100177 }
178 }
179 capacity_scale = max(cpu_capacity, capacity_scale);
180 raw_capacity[cpu] = cpu_capacity;
Rob Herring6ef25412017-07-18 16:42:49 -0500181 pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n",
182 cpu_node, raw_capacity[cpu]);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100183 } else {
184 if (raw_capacity) {
Rob Herring6ef25412017-07-18 16:42:49 -0500185 pr_err("cpu_capacity: missing %pOF raw capacity\n",
186 cpu_node);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100187 pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
188 }
189 cap_parsing_failed = true;
Viresh Kumar62de1162017-06-23 14:55:33 +0530190 free_raw_capacity();
Juri Lelli2ef7a292017-05-31 17:59:28 +0100191 }
192
193 return !ret;
194}
195
196#ifdef CONFIG_CPU_FREQ
Gaku Inami9de9a442018-02-13 11:06:40 +0900197static cpumask_var_t cpus_to_visit;
198static void parsing_done_workfn(struct work_struct *work);
199static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100200
Gaku Inami9de9a442018-02-13 11:06:40 +0900201static int
Juri Lelli2ef7a292017-05-31 17:59:28 +0100202init_cpu_capacity_callback(struct notifier_block *nb,
203 unsigned long val,
204 void *data)
205{
206 struct cpufreq_policy *policy = data;
207 int cpu;
208
Viresh Kumard8bcf4d2017-06-23 14:55:34 +0530209 if (!raw_capacity)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100210 return 0;
211
Viresh Kumar93a57082017-06-23 14:55:31 +0530212 if (val != CPUFREQ_NOTIFY)
213 return 0;
214
215 pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
216 cpumask_pr_args(policy->related_cpus),
217 cpumask_pr_args(cpus_to_visit));
218
219 cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus);
220
221 for_each_cpu(cpu, policy->related_cpus) {
222 raw_capacity[cpu] = topology_get_cpu_scale(NULL, cpu) *
223 policy->cpuinfo.max_freq / 1000UL;
224 capacity_scale = max(raw_capacity[cpu], capacity_scale);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100225 }
Viresh Kumar93a57082017-06-23 14:55:31 +0530226
227 if (cpumask_empty(cpus_to_visit)) {
228 topology_normalize_cpu_scale();
Morten Rasmussenbb1fbdd2018-07-20 14:32:32 +0100229 schedule_work(&update_topology_flags_work);
Viresh Kumar62de1162017-06-23 14:55:33 +0530230 free_raw_capacity();
Viresh Kumar93a57082017-06-23 14:55:31 +0530231 pr_debug("cpu_capacity: parsing done\n");
Viresh Kumar93a57082017-06-23 14:55:31 +0530232 schedule_work(&parsing_done_work);
233 }
234
Juri Lelli2ef7a292017-05-31 17:59:28 +0100235 return 0;
236}
237
Gaku Inami9de9a442018-02-13 11:06:40 +0900238static struct notifier_block init_cpu_capacity_notifier = {
Juri Lelli2ef7a292017-05-31 17:59:28 +0100239 .notifier_call = init_cpu_capacity_callback,
240};
241
242static int __init register_cpufreq_notifier(void)
243{
Dietmar Eggemann54082112017-09-26 17:41:06 +0100244 int ret;
245
Juri Lelli2ef7a292017-05-31 17:59:28 +0100246 /*
247 * on ACPI-based systems we need to use the default cpu capacity
248 * until we have the necessary code to parse the cpu capacity, so
249 * skip registering cpufreq notifier.
250 */
Juri Lellic105aa32017-05-31 17:59:29 +0100251 if (!acpi_disabled || !raw_capacity)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100252 return -EINVAL;
253
254 if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) {
255 pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
256 return -ENOMEM;
257 }
258
259 cpumask_copy(cpus_to_visit, cpu_possible_mask);
260
Dietmar Eggemann54082112017-09-26 17:41:06 +0100261 ret = cpufreq_register_notifier(&init_cpu_capacity_notifier,
262 CPUFREQ_POLICY_NOTIFIER);
263
264 if (ret)
265 free_cpumask_var(cpus_to_visit);
266
267 return ret;
Juri Lelli2ef7a292017-05-31 17:59:28 +0100268}
269core_initcall(register_cpufreq_notifier);
270
Gaku Inami9de9a442018-02-13 11:06:40 +0900271static void parsing_done_workfn(struct work_struct *work)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100272{
273 cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
274 CPUFREQ_POLICY_NOTIFIER);
Dietmar Eggemann54082112017-09-26 17:41:06 +0100275 free_cpumask_var(cpus_to_visit);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100276}
277
278#else
Juri Lelli2ef7a292017-05-31 17:59:28 +0100279core_initcall(free_raw_capacity);
280#endif