blob: a3cd7c869c3ee95e3ac270362f118a313a7e76e0 [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
25static DEFINE_MUTEX(cpu_scale_mutex);
26static DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE;
27
Juri Lelli4ca4f262017-05-31 17:59:31 +010028unsigned long topology_get_cpu_scale(struct sched_domain *sd, int cpu)
Juri Lelli2ef7a292017-05-31 17:59:28 +010029{
30 return per_cpu(cpu_scale, cpu);
31}
32
Juri Lelli4ca4f262017-05-31 17:59:31 +010033void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity)
Juri Lelli2ef7a292017-05-31 17:59:28 +010034{
35 per_cpu(cpu_scale, cpu) = capacity;
36}
37
38static 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 Kumar3eeba1a2017-06-23 14:55:30 +053044 return sprintf(buf, "%lu\n", topology_get_cpu_scale(NULL, cpu->dev.id));
Juri Lelli2ef7a292017-05-31 17:59:28 +010045}
46
47static 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 Lelli4ca4f262017-05-31 17:59:31 +010069 topology_set_cpu_scale(i, new_capacity);
Juri Lelli2ef7a292017-05-31 17:59:28 +010070 mutex_unlock(&cpu_scale_mutex);
71
72 return count;
73}
74
75static DEVICE_ATTR_RW(cpu_capacity);
76
77static 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}
94subsys_initcall(register_cpu_capacity_sysctl);
95
96static u32 capacity_scale;
97static u32 *raw_capacity;
Juri Lellic105aa32017-05-31 17:59:29 +010098static bool cap_parsing_failed;
Juri Lelli2ef7a292017-05-31 17:59:28 +010099
Juri Lelli4ca4f262017-05-31 17:59:31 +0100100void topology_normalize_cpu_scale(void)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100101{
102 u64 capacity;
103 int cpu;
104
105 if (!raw_capacity || cap_parsing_failed)
106 return;
107
108 pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale);
109 mutex_lock(&cpu_scale_mutex);
110 for_each_possible_cpu(cpu) {
111 pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n",
112 cpu, raw_capacity[cpu]);
113 capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT)
114 / capacity_scale;
Juri Lelli4ca4f262017-05-31 17:59:31 +0100115 topology_set_cpu_scale(cpu, capacity);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100116 pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n",
Juri Lelli4ca4f262017-05-31 17:59:31 +0100117 cpu, topology_get_cpu_scale(NULL, cpu));
Juri Lelli2ef7a292017-05-31 17:59:28 +0100118 }
119 mutex_unlock(&cpu_scale_mutex);
120}
121
Juri Lelli4ca4f262017-05-31 17:59:31 +0100122int __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100123{
124 int ret = 1;
125 u32 cpu_capacity;
126
127 if (cap_parsing_failed)
128 return !ret;
129
Viresh Kumar3eeba1a2017-06-23 14:55:30 +0530130 ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz",
Juri Lelli2ef7a292017-05-31 17:59:28 +0100131 &cpu_capacity);
132 if (!ret) {
133 if (!raw_capacity) {
134 raw_capacity = kcalloc(num_possible_cpus(),
135 sizeof(*raw_capacity),
136 GFP_KERNEL);
137 if (!raw_capacity) {
138 pr_err("cpu_capacity: failed to allocate memory for raw capacities\n");
139 cap_parsing_failed = true;
140 return 0;
141 }
142 }
143 capacity_scale = max(cpu_capacity, capacity_scale);
144 raw_capacity[cpu] = cpu_capacity;
145 pr_debug("cpu_capacity: %s cpu_capacity=%u (raw)\n",
146 cpu_node->full_name, raw_capacity[cpu]);
147 } else {
148 if (raw_capacity) {
149 pr_err("cpu_capacity: missing %s raw capacity\n",
150 cpu_node->full_name);
151 pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n");
152 }
153 cap_parsing_failed = true;
154 kfree(raw_capacity);
155 }
156
157 return !ret;
158}
159
160#ifdef CONFIG_CPU_FREQ
161static cpumask_var_t cpus_to_visit;
162static bool cap_parsing_done;
163static void parsing_done_workfn(struct work_struct *work);
164static DECLARE_WORK(parsing_done_work, parsing_done_workfn);
165
166static int
167init_cpu_capacity_callback(struct notifier_block *nb,
168 unsigned long val,
169 void *data)
170{
171 struct cpufreq_policy *policy = data;
172 int cpu;
173
174 if (cap_parsing_failed || cap_parsing_done)
175 return 0;
176
Viresh Kumar93a57082017-06-23 14:55:31 +0530177 if (val != CPUFREQ_NOTIFY)
178 return 0;
179
180 pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n",
181 cpumask_pr_args(policy->related_cpus),
182 cpumask_pr_args(cpus_to_visit));
183
184 cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus);
185
186 for_each_cpu(cpu, policy->related_cpus) {
187 raw_capacity[cpu] = topology_get_cpu_scale(NULL, cpu) *
188 policy->cpuinfo.max_freq / 1000UL;
189 capacity_scale = max(raw_capacity[cpu], capacity_scale);
Juri Lelli2ef7a292017-05-31 17:59:28 +0100190 }
Viresh Kumar93a57082017-06-23 14:55:31 +0530191
192 if (cpumask_empty(cpus_to_visit)) {
193 topology_normalize_cpu_scale();
194 kfree(raw_capacity);
195 pr_debug("cpu_capacity: parsing done\n");
196 cap_parsing_done = true;
197 schedule_work(&parsing_done_work);
198 }
199
Juri Lelli2ef7a292017-05-31 17:59:28 +0100200 return 0;
201}
202
203static struct notifier_block init_cpu_capacity_notifier = {
204 .notifier_call = init_cpu_capacity_callback,
205};
206
207static int __init register_cpufreq_notifier(void)
208{
209 /*
210 * on ACPI-based systems we need to use the default cpu capacity
211 * until we have the necessary code to parse the cpu capacity, so
212 * skip registering cpufreq notifier.
213 */
Juri Lellic105aa32017-05-31 17:59:29 +0100214 if (!acpi_disabled || !raw_capacity)
Juri Lelli2ef7a292017-05-31 17:59:28 +0100215 return -EINVAL;
216
217 if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) {
218 pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n");
219 return -ENOMEM;
220 }
221
222 cpumask_copy(cpus_to_visit, cpu_possible_mask);
223
224 return cpufreq_register_notifier(&init_cpu_capacity_notifier,
225 CPUFREQ_POLICY_NOTIFIER);
226}
227core_initcall(register_cpufreq_notifier);
228
229static void parsing_done_workfn(struct work_struct *work)
230{
231 cpufreq_unregister_notifier(&init_cpu_capacity_notifier,
232 CPUFREQ_POLICY_NOTIFIER);
233}
234
235#else
236static int __init free_raw_capacity(void)
237{
238 kfree(raw_capacity);
239
240 return 0;
241}
242core_initcall(free_raw_capacity);
243#endif