]>
Commit | Line | Data |
---|---|---|
2ef7a295 JL |
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> | |
615ffd63 | 16 | #include <linux/arch_topology.h> |
2ef7a295 JL |
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 | ||
0e27c567 | 25 | DEFINE_PER_CPU(unsigned long, freq_scale) = SCHED_CAPACITY_SCALE; |
2ef7a295 | 26 | |
0e27c567 DE |
27 | void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq, |
28 | unsigned long max_freq) | |
2ef7a295 | 29 | { |
0e27c567 DE |
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; | |
2ef7a295 JL |
37 | } |
38 | ||
2ef7a295 | 39 | static DEFINE_MUTEX(cpu_scale_mutex); |
8216f588 | 40 | DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE; |
2ef7a295 | 41 | |
4ca4f26a | 42 | void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity) |
2ef7a295 JL |
43 | { |
44 | per_cpu(cpu_scale, cpu) = capacity; | |
45 | } | |
46 | ||
47 | static ssize_t cpu_capacity_show(struct device *dev, | |
48 | struct device_attribute *attr, | |
49 | char *buf) | |
50 | { | |
51 | struct cpu *cpu = container_of(dev, struct cpu, dev); | |
52 | ||
3eeba1a2 | 53 | return sprintf(buf, "%lu\n", topology_get_cpu_scale(NULL, cpu->dev.id)); |
2ef7a295 JL |
54 | } |
55 | ||
56 | static ssize_t cpu_capacity_store(struct device *dev, | |
57 | struct device_attribute *attr, | |
58 | const char *buf, | |
59 | size_t count) | |
60 | { | |
61 | struct cpu *cpu = container_of(dev, struct cpu, dev); | |
62 | int this_cpu = cpu->dev.id; | |
63 | int i; | |
64 | unsigned long new_capacity; | |
65 | ssize_t ret; | |
66 | ||
67 | if (!count) | |
68 | return 0; | |
69 | ||
70 | ret = kstrtoul(buf, 0, &new_capacity); | |
71 | if (ret) | |
72 | return ret; | |
73 | if (new_capacity > SCHED_CAPACITY_SCALE) | |
74 | return -EINVAL; | |
75 | ||
76 | mutex_lock(&cpu_scale_mutex); | |
77 | for_each_cpu(i, &cpu_topology[this_cpu].core_sibling) | |
4ca4f26a | 78 | topology_set_cpu_scale(i, new_capacity); |
2ef7a295 JL |
79 | mutex_unlock(&cpu_scale_mutex); |
80 | ||
81 | return count; | |
82 | } | |
83 | ||
84 | static DEVICE_ATTR_RW(cpu_capacity); | |
85 | ||
86 | static 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 | } | |
103 | subsys_initcall(register_cpu_capacity_sysctl); | |
104 | ||
105 | static u32 capacity_scale; | |
106 | static u32 *raw_capacity; | |
62de1161 | 107 | |
82d8ba71 | 108 | static int free_raw_capacity(void) |
62de1161 VK |
109 | { |
110 | kfree(raw_capacity); | |
111 | raw_capacity = NULL; | |
112 | ||
113 | return 0; | |
114 | } | |
2ef7a295 | 115 | |
4ca4f26a | 116 | void topology_normalize_cpu_scale(void) |
2ef7a295 JL |
117 | { |
118 | u64 capacity; | |
119 | int cpu; | |
120 | ||
62de1161 | 121 | if (!raw_capacity) |
2ef7a295 JL |
122 | return; |
123 | ||
124 | pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale); | |
125 | mutex_lock(&cpu_scale_mutex); | |
126 | for_each_possible_cpu(cpu) { | |
127 | pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n", | |
128 | cpu, raw_capacity[cpu]); | |
129 | capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT) | |
130 | / capacity_scale; | |
4ca4f26a | 131 | topology_set_cpu_scale(cpu, capacity); |
2ef7a295 | 132 | pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n", |
4ca4f26a | 133 | cpu, topology_get_cpu_scale(NULL, cpu)); |
2ef7a295 JL |
134 | } |
135 | mutex_unlock(&cpu_scale_mutex); | |
136 | } | |
137 | ||
805df296 | 138 | bool __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu) |
2ef7a295 | 139 | { |
62de1161 | 140 | static bool cap_parsing_failed; |
805df296 | 141 | int ret; |
2ef7a295 JL |
142 | u32 cpu_capacity; |
143 | ||
144 | if (cap_parsing_failed) | |
805df296 | 145 | return false; |
2ef7a295 | 146 | |
3eeba1a2 | 147 | ret = of_property_read_u32(cpu_node, "capacity-dmips-mhz", |
2ef7a295 JL |
148 | &cpu_capacity); |
149 | if (!ret) { | |
150 | if (!raw_capacity) { | |
151 | raw_capacity = kcalloc(num_possible_cpus(), | |
152 | sizeof(*raw_capacity), | |
153 | GFP_KERNEL); | |
154 | if (!raw_capacity) { | |
155 | pr_err("cpu_capacity: failed to allocate memory for raw capacities\n"); | |
156 | cap_parsing_failed = true; | |
805df296 | 157 | return false; |
2ef7a295 JL |
158 | } |
159 | } | |
160 | capacity_scale = max(cpu_capacity, capacity_scale); | |
161 | raw_capacity[cpu] = cpu_capacity; | |
6ef2541f RH |
162 | pr_debug("cpu_capacity: %pOF cpu_capacity=%u (raw)\n", |
163 | cpu_node, raw_capacity[cpu]); | |
2ef7a295 JL |
164 | } else { |
165 | if (raw_capacity) { | |
6ef2541f RH |
166 | pr_err("cpu_capacity: missing %pOF raw capacity\n", |
167 | cpu_node); | |
2ef7a295 JL |
168 | pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n"); |
169 | } | |
170 | cap_parsing_failed = true; | |
62de1161 | 171 | free_raw_capacity(); |
2ef7a295 JL |
172 | } |
173 | ||
174 | return !ret; | |
175 | } | |
176 | ||
177 | #ifdef CONFIG_CPU_FREQ | |
452562ab SH |
178 | static cpumask_var_t cpus_to_visit __initdata; |
179 | static void __init parsing_done_workfn(struct work_struct *work); | |
180 | static __initdata DECLARE_WORK(parsing_done_work, parsing_done_workfn); | |
2ef7a295 | 181 | |
452562ab | 182 | static int __init |
2ef7a295 JL |
183 | init_cpu_capacity_callback(struct notifier_block *nb, |
184 | unsigned long val, | |
185 | void *data) | |
186 | { | |
187 | struct cpufreq_policy *policy = data; | |
188 | int cpu; | |
189 | ||
d8bcf4db | 190 | if (!raw_capacity) |
2ef7a295 JL |
191 | return 0; |
192 | ||
93a57081 VK |
193 | if (val != CPUFREQ_NOTIFY) |
194 | return 0; | |
195 | ||
196 | pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n", | |
197 | cpumask_pr_args(policy->related_cpus), | |
198 | cpumask_pr_args(cpus_to_visit)); | |
199 | ||
200 | cpumask_andnot(cpus_to_visit, cpus_to_visit, policy->related_cpus); | |
201 | ||
202 | for_each_cpu(cpu, policy->related_cpus) { | |
203 | raw_capacity[cpu] = topology_get_cpu_scale(NULL, cpu) * | |
204 | policy->cpuinfo.max_freq / 1000UL; | |
205 | capacity_scale = max(raw_capacity[cpu], capacity_scale); | |
2ef7a295 | 206 | } |
93a57081 VK |
207 | |
208 | if (cpumask_empty(cpus_to_visit)) { | |
209 | topology_normalize_cpu_scale(); | |
62de1161 | 210 | free_raw_capacity(); |
93a57081 | 211 | pr_debug("cpu_capacity: parsing done\n"); |
93a57081 VK |
212 | schedule_work(&parsing_done_work); |
213 | } | |
214 | ||
2ef7a295 JL |
215 | return 0; |
216 | } | |
217 | ||
452562ab | 218 | static struct notifier_block init_cpu_capacity_notifier __initdata = { |
2ef7a295 JL |
219 | .notifier_call = init_cpu_capacity_callback, |
220 | }; | |
221 | ||
222 | static int __init register_cpufreq_notifier(void) | |
223 | { | |
5408211a DE |
224 | int ret; |
225 | ||
2ef7a295 JL |
226 | /* |
227 | * on ACPI-based systems we need to use the default cpu capacity | |
228 | * until we have the necessary code to parse the cpu capacity, so | |
229 | * skip registering cpufreq notifier. | |
230 | */ | |
c105aa31 | 231 | if (!acpi_disabled || !raw_capacity) |
2ef7a295 JL |
232 | return -EINVAL; |
233 | ||
234 | if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) { | |
235 | pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n"); | |
236 | return -ENOMEM; | |
237 | } | |
238 | ||
239 | cpumask_copy(cpus_to_visit, cpu_possible_mask); | |
240 | ||
5408211a DE |
241 | ret = cpufreq_register_notifier(&init_cpu_capacity_notifier, |
242 | CPUFREQ_POLICY_NOTIFIER); | |
243 | ||
244 | if (ret) | |
245 | free_cpumask_var(cpus_to_visit); | |
246 | ||
247 | return ret; | |
2ef7a295 JL |
248 | } |
249 | core_initcall(register_cpufreq_notifier); | |
250 | ||
452562ab | 251 | static void __init parsing_done_workfn(struct work_struct *work) |
2ef7a295 JL |
252 | { |
253 | cpufreq_unregister_notifier(&init_cpu_capacity_notifier, | |
254 | CPUFREQ_POLICY_NOTIFIER); | |
5408211a | 255 | free_cpumask_var(cpus_to_visit); |
2ef7a295 JL |
256 | } |
257 | ||
258 | #else | |
2ef7a295 JL |
259 | core_initcall(free_raw_capacity); |
260 | #endif |