blob: e7cb0c6ade81ec7171a0a55fdb0cf0f5ab4cd199 [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>
18
Dietmar Eggemann0e27c562017-09-26 17:41:10 +010019DEFINE_PER_CPU(unsigned long, freq_scale) = SCHED_CAPACITY_SCALE;
Juri Lelli2ef7a292017-05-31 17:59:28 +010020
Dietmar Eggemann0e27c562017-09-26 17:41:10 +010021void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
22 unsigned long max_freq)
Juri Lelli2ef7a292017-05-31 17:59:28 +010023{
Dietmar Eggemann0e27c562017-09-26 17:41:10 +010024 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 Lelli2ef7a292017-05-31 17:59:28 +010031}
32
Juri Lelli2ef7a292017-05-31 17:59:28 +010033static DEFINE_MUTEX(cpu_scale_mutex);
Dietmar Eggemann8216f582017-09-26 17:41:11 +010034DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
Juri Lelli2ef7a292017-05-31 17:59:28 +010035
Juri Lelli4ca4f262017-05-31 17:59:31 +010036void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity)
Juri Lelli2ef7a292017-05-31 17:59:28 +010037{
38 per_cpu(cpu_scale, cpu) = capacity;
39}
40
41static 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 Kumar3eeba1a2017-06-23 14:55:30 +053047 return sprintf(buf, "%lu\n", topology_get_cpu_scale(NULL, cpu->dev.id));
Juri Lelli2ef7a292017-05-31 17:59:28 +010048}
49
50static 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 Lelli4ca4f262017-05-31 17:59:31 +010072 topology_set_cpu_scale(i, new_capacity);
Juri Lelli2ef7a292017-05-31 17:59:28 +010073 mutex_unlock(&cpu_scale_mutex);
74
75 return count;
76}
77
78static DEVICE_ATTR_RW(cpu_capacity);
79
80static 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}
97subsys_initcall(register_cpu_capacity_sysctl);
98
99static u32 capacity_scale;
100static u32 *raw_capacity;
Viresh Kumar62de1162017-06-23 14:55:33 +0530101
Prasad Sodagudi82d8ba72017-10-10 00:34:56 -0700102static int free_raw_capacity(void)
Viresh Kumar62de1162017-06-23 14:55:33 +0530103{
104 kfree(raw_capacity);
105 raw_capacity = NULL;
106
107 return 0;
108}
Juri Lelli2ef7a292017-05-31 17:59:28 +0100109
Juri Lelli4ca4f262017-05-31 17:59:31 +0100110void topology_normalize_cpu_scale(void)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100111{
112 u64 capacity;
113 int cpu;
114
Viresh Kumar62de1162017-06-23 14:55:33 +0530115 if (!raw_capacity)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100116 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 Lelli4ca4f262017-05-31 17:59:31 +0100125 topology_set_cpu_scale(cpu, capacity);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100126 pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
Juri Lelli4ca4f262017-05-31 17:59:31 +0100127 cpu, topology_get_cpu_scale(NULL, cpu));
Juri Lelli2ef7a292017-05-31 17:59:28 +0100128 }
129 mutex_unlock(&cpu_scale_mutex);
130}
131
Viresh Kumar805df292017-06-23 14:55:32 +0530132bool __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100133{
Viresh Kumar62de1162017-06-23 14:55:33 +0530134 static bool cap_parsing_failed;
Viresh Kumar805df292017-06-23 14:55:32 +0530135 int ret;
Juri Lelli2ef7a292017-05-31 17:59:28 +0100136 u32 cpu_capacity;
137
138 if (cap_parsing_failed)
Viresh Kumar805df292017-06-23 14:55:32 +0530139 return false;
Juri Lelli2ef7a292017-05-31 17:59:28 +0100140
Viresh Kumar3eeba1a2017-06-23 14:55:30 +0530141 ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz",
Juri Lelli2ef7a292017-05-31 17:59:28 +0100142 &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 Kumar805df292017-06-23 14:55:32 +0530151 return false;
Juri Lelli2ef7a292017-05-31 17:59:28 +0100152 }
153 }
154 capacity_scale = max(cpu_capacity, capacity_scale);
155 raw_capacity[cpu] = cpu_capacity;
Rob Herring6ef25412017-07-18 16:42:49 -0500156 pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n",
157 cpu_node, raw_capacity[cpu]);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100158 } else {
159 if (raw_capacity) {
Rob Herring6ef25412017-07-18 16:42:49 -0500160 pr_err("cpu_capacity: missing %pOF raw capacity\n",
161 cpu_node);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100162 pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
163 }
164 cap_parsing_failed = true;
Viresh Kumar62de1162017-06-23 14:55:33 +0530165 free_raw_capacity();
Juri Lelli2ef7a292017-05-31 17:59:28 +0100166 }
167
168 return !ret;
169}
170
171#ifdef CONFIG_CPU_FREQ
Gaku Inami9de9a442018-02-13 11:06:40 +0900172static cpumask_var_t cpus_to_visit;
173static void parsing_done_workfn(struct work_struct *work);
174static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100175
Gaku Inami9de9a442018-02-13 11:06:40 +0900176static int
Juri Lelli2ef7a292017-05-31 17:59:28 +0100177init_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 Kumard8bcf4d2017-06-23 14:55:34 +0530184 if (!raw_capacity)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100185 return 0;
186
Viresh Kumar93a57082017-06-23 14:55:31 +0530187 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 Lelli2ef7a292017-05-31 17:59:28 +0100200 }
Viresh Kumar93a57082017-06-23 14:55:31 +0530201
202 if (cpumask_empty(cpus_to_visit)) {
203 topology_normalize_cpu_scale();
Viresh Kumar62de1162017-06-23 14:55:33 +0530204 free_raw_capacity();
Viresh Kumar93a57082017-06-23 14:55:31 +0530205 pr_debug("cpu_capacity: parsing done\n");
Viresh Kumar93a57082017-06-23 14:55:31 +0530206 schedule_work(&parsing_done_work);
207 }
208
Juri Lelli2ef7a292017-05-31 17:59:28 +0100209 return 0;
210}
211
Gaku Inami9de9a442018-02-13 11:06:40 +0900212static struct notifier_block init_cpu_capacity_notifier = {
Juri Lelli2ef7a292017-05-31 17:59:28 +0100213 .notifier_call = init_cpu_capacity_callback,
214};
215
216static int __init register_cpufreq_notifier(void)
217{
Dietmar Eggemann54082112017-09-26 17:41:06 +0100218 int ret;
219
Juri Lelli2ef7a292017-05-31 17:59:28 +0100220 /*
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 Lellic105aa32017-05-31 17:59:29 +0100225 if (!acpi_disabled || !raw_capacity)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100226 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 Eggemann54082112017-09-26 17:41:06 +0100235 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 Lelli2ef7a292017-05-31 17:59:28 +0100242}
243core_initcall(register_cpufreq_notifier);
244
Gaku Inami9de9a442018-02-13 11:06:40 +0900245static void parsing_done_workfn(struct work_struct *work)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100246{
247 cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
248 CPUFREQ_POLICY_NOTIFIER);
Dietmar Eggemann54082112017-09-26 17:41:06 +0100249 free_cpumask_var(cpus_to_visit);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100250}
251
252#else
Juri Lelli2ef7a292017-05-31 17:59:28 +0100253core_initcall(free_raw_capacity);
254#endif