blob: 416ec2f5211df37b0a7151c918e6be6a5d58d0a6 [file] [log] [blame]
Juri Lelli2ef7a292017-05-31 17:59:28 +01001/*
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 Lelli615ffd62017-05-31 17:59:30 +010016#include <linux/arch_topology.h>
Juri Lelli2ef7a292017-05-31 17:59:28 +010017#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
Dietmar Eggemann0e27c562017-09-26 17:41:10 +010025DEFINE_PER_CPU(unsigned long, freq_scale) = SCHED_CAPACITY_SCALE;
26
27void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
28 unsigned long max_freq)
29{
30 unsigned long scale;
31 int i;
32
33 scale = (cur_freq << SCHED_CAPACITY_SHIFT) / max_freq;
34
35 for_each_cpu(i, cpus)
36 per_cpu(freq_scale, i) = scale;
37}
38
Juri Lelli2ef7a292017-05-31 17:59:28 +010039static DEFINE_MUTEX(cpu_scale_mutex);
40static DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
41
Juri Lelli4ca4f262017-05-31 17:59:31 +010042unsigned long topology_get_cpu_scale(struct sched_domain *sd, int cpu)
Juri Lelli2ef7a292017-05-31 17:59:28 +010043{
44 return per_cpu(cpu_scale, cpu);
45}
46
Juri Lelli4ca4f262017-05-31 17:59:31 +010047void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity)
Juri Lelli2ef7a292017-05-31 17:59:28 +010048{
49 per_cpu(cpu_scale, cpu) = capacity;
50}
51
52static ssize_t cpu_capacity_show(struct device *dev,
53 struct device_attribute *attr,
54 char *buf)
55{
56 struct cpu *cpu = container_of(dev, struct cpu, dev);
57
Viresh Kumar3eeba1a2017-06-23 14:55:30 +053058 return sprintf(buf, "%lu\n", topology_get_cpu_scale(NULL, cpu->dev.id));
Juri Lelli2ef7a292017-05-31 17:59:28 +010059}
60
61static ssize_t cpu_capacity_store(struct device *dev,
62 struct device_attribute *attr,
63 const char *buf,
64 size_t count)
65{
66 struct cpu *cpu = container_of(dev, struct cpu, dev);
67 int this_cpu = cpu->dev.id;
68 int i;
69 unsigned long new_capacity;
70 ssize_t ret;
71
72 if (!count)
73 return 0;
74
75 ret = kstrtoul(buf, 0, &new_capacity);
76 if (ret)
77 return ret;
78 if (new_capacity > SCHED_CAPACITY_SCALE)
79 return -EINVAL;
80
81 mutex_lock(&cpu_scale_mutex);
82 for_each_cpu(i, &cpu_topology[this_cpu].core_sibling)
Juri Lelli4ca4f262017-05-31 17:59:31 +010083 topology_set_cpu_scale(i, new_capacity);
Juri Lelli2ef7a292017-05-31 17:59:28 +010084 mutex_unlock(&cpu_scale_mutex);
85
86 return count;
87}
88
89static DEVICE_ATTR_RW(cpu_capacity);
90
91static int register_cpu_capacity_sysctl(void)
92{
93 int i;
94 struct device *cpu;
95
96 for_each_possible_cpu(i) {
97 cpu = get_cpu_device(i);
98 if (!cpu) {
99 pr_err("%s: too early to get CPU%d device!\n",
100 __func__, i);
101 continue;
102 }
103 device_create_file(cpu, &dev_attr_cpu_capacity);
104 }
105
106 return 0;
107}
108subsys_initcall(register_cpu_capacity_sysctl);
109
110static u32 capacity_scale;
111static u32 *raw_capacity;
Viresh Kumar62de1162017-06-23 14:55:33 +0530112
113static int __init free_raw_capacity(void)
114{
115 kfree(raw_capacity);
116 raw_capacity = NULL;
117
118 return 0;
119}
Juri Lelli2ef7a292017-05-31 17:59:28 +0100120
Juri Lelli4ca4f262017-05-31 17:59:31 +0100121void topology_normalize_cpu_scale(void)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100122{
123 u64 capacity;
124 int cpu;
125
Viresh Kumar62de1162017-06-23 14:55:33 +0530126 if (!raw_capacity)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100127 return;
128
129 pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale);
130 mutex_lock(&cpu_scale_mutex);
131 for_each_possible_cpu(cpu) {
132 pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n",
133 cpu, raw_capacity[cpu]);
134 capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT)
135 / capacity_scale;
Juri Lelli4ca4f262017-05-31 17:59:31 +0100136 topology_set_cpu_scale(cpu, capacity);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100137 pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
Juri Lelli4ca4f262017-05-31 17:59:31 +0100138 cpu, topology_get_cpu_scale(NULL, cpu));
Juri Lelli2ef7a292017-05-31 17:59:28 +0100139 }
140 mutex_unlock(&cpu_scale_mutex);
141}
142
Viresh Kumar805df292017-06-23 14:55:32 +0530143bool __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100144{
Viresh Kumar62de1162017-06-23 14:55:33 +0530145 static bool cap_parsing_failed;
Viresh Kumar805df292017-06-23 14:55:32 +0530146 int ret;
Juri Lelli2ef7a292017-05-31 17:59:28 +0100147 u32 cpu_capacity;
148
149 if (cap_parsing_failed)
Viresh Kumar805df292017-06-23 14:55:32 +0530150 return false;
Juri Lelli2ef7a292017-05-31 17:59:28 +0100151
Viresh Kumar3eeba1a2017-06-23 14:55:30 +0530152 ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz",
Juri Lelli2ef7a292017-05-31 17:59:28 +0100153 &cpu_capacity);
154 if (!ret) {
155 if (!raw_capacity) {
156 raw_capacity = kcalloc(num_possible_cpus(),
157 sizeof(*raw_capacity),
158 GFP_KERNEL);
159 if (!raw_capacity) {
160 pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
161 cap_parsing_failed = true;
Viresh Kumar805df292017-06-23 14:55:32 +0530162 return false;
Juri Lelli2ef7a292017-05-31 17:59:28 +0100163 }
164 }
165 capacity_scale = max(cpu_capacity, capacity_scale);
166 raw_capacity[cpu] = cpu_capacity;
Rob Herring6ef25412017-07-18 16:42:49 -0500167 pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n",
168 cpu_node, raw_capacity[cpu]);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100169 } else {
170 if (raw_capacity) {
Rob Herring6ef25412017-07-18 16:42:49 -0500171 pr_err("cpu_capacity: missing %pOF raw capacity\n",
172 cpu_node);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100173 pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
174 }
175 cap_parsing_failed = true;
Viresh Kumar62de1162017-06-23 14:55:33 +0530176 free_raw_capacity();
Juri Lelli2ef7a292017-05-31 17:59:28 +0100177 }
178
179 return !ret;
180}
181
182#ifdef CONFIG_CPU_FREQ
183static cpumask_var_t cpus_to_visit;
Juri Lelli2ef7a292017-05-31 17:59:28 +0100184static void parsing_done_workfn(struct work_struct *work);
185static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
186
187static int
188init_cpu_capacity_callback(struct notifier_block *nb,
189 unsigned long val,
190 void *data)
191{
192 struct cpufreq_policy *policy = data;
193 int cpu;
194
Viresh Kumard8bcf4d2017-06-23 14:55:34 +0530195 if (!raw_capacity)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100196 return 0;
197
Viresh Kumar93a57082017-06-23 14:55:31 +0530198 if (val != CPUFREQ_NOTIFY)
199 return 0;
200
201 pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
202 cpumask_pr_args(policy->related_cpus),
203 cpumask_pr_args(cpus_to_visit));
204
205 cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus);
206
207 for_each_cpu(cpu, policy->related_cpus) {
208 raw_capacity[cpu] = topology_get_cpu_scale(NULL, cpu) *
209 policy->cpuinfo.max_freq / 1000UL;
210 capacity_scale = max(raw_capacity[cpu], capacity_scale);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100211 }
Viresh Kumar93a57082017-06-23 14:55:31 +0530212
213 if (cpumask_empty(cpus_to_visit)) {
214 topology_normalize_cpu_scale();
Viresh Kumar62de1162017-06-23 14:55:33 +0530215 free_raw_capacity();
Viresh Kumar93a57082017-06-23 14:55:31 +0530216 pr_debug("cpu_capacity: parsing done\n");
Viresh Kumar93a57082017-06-23 14:55:31 +0530217 schedule_work(&parsing_done_work);
218 }
219
Juri Lelli2ef7a292017-05-31 17:59:28 +0100220 return 0;
221}
222
223static struct notifier_block init_cpu_capacity_notifier = {
224 .notifier_call = init_cpu_capacity_callback,
225};
226
227static int __init register_cpufreq_notifier(void)
228{
Dietmar Eggemann54082112017-09-26 17:41:06 +0100229 int ret;
230
Juri Lelli2ef7a292017-05-31 17:59:28 +0100231 /*
232 * on ACPI-based systems we need to use the default cpu capacity
233 * until we have the necessary code to parse the cpu capacity, so
234 * skip registering cpufreq notifier.
235 */
Juri Lellic105aa32017-05-31 17:59:29 +0100236 if (!acpi_disabled || !raw_capacity)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100237 return -EINVAL;
238
239 if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) {
240 pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
241 return -ENOMEM;
242 }
243
244 cpumask_copy(cpus_to_visit, cpu_possible_mask);
245
Dietmar Eggemann54082112017-09-26 17:41:06 +0100246 ret = cpufreq_register_notifier(&init_cpu_capacity_notifier,
247 CPUFREQ_POLICY_NOTIFIER);
248
249 if (ret)
250 free_cpumask_var(cpus_to_visit);
251
252 return ret;
Juri Lelli2ef7a292017-05-31 17:59:28 +0100253}
254core_initcall(register_cpufreq_notifier);
255
256static void parsing_done_workfn(struct work_struct *work)
257{
258 cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
259 CPUFREQ_POLICY_NOTIFIER);
Dietmar Eggemann54082112017-09-26 17:41:06 +0100260 free_cpumask_var(cpus_to_visit);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100261}
262
263#else
Juri Lelli2ef7a292017-05-31 17:59:28 +0100264core_initcall(free_raw_capacity);
265#endif