]>
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 | ||
25 | static DEFINE_MUTEX(cpu_scale_mutex); | |
26 | static DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE; | |
27 | ||
4ca4f26a | 28 | unsigned long topology_get_cpu_scale(struct sched_domain *sd, int cpu) |
2ef7a295 JL |
29 | { |
30 | return per_cpu(cpu_scale, cpu); | |
31 | } | |
32 | ||
4ca4f26a | 33 | void topology_set_cpu_scale(unsigned int cpu, unsigned long capacity) |
2ef7a295 JL |
34 | { |
35 | per_cpu(cpu_scale, cpu) = capacity; | |
36 | } | |
37 | ||
38 | static 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 | ||
44 | return sprintf(buf, "%lu\n", | |
4ca4f26a | 45 | topology_get_cpu_scale(NULL, cpu->dev.id)); |
2ef7a295 JL |
46 | } |
47 | ||
48 | static ssize_t cpu_capacity_store(struct device *dev, | |
49 | struct device_attribute *attr, | |
50 | const char *buf, | |
51 | size_t count) | |
52 | { | |
53 | struct cpu *cpu = container_of(dev, struct cpu, dev); | |
54 | int this_cpu = cpu->dev.id; | |
55 | int i; | |
56 | unsigned long new_capacity; | |
57 | ssize_t ret; | |
58 | ||
59 | if (!count) | |
60 | return 0; | |
61 | ||
62 | ret = kstrtoul(buf, 0, &new_capacity); | |
63 | if (ret) | |
64 | return ret; | |
65 | if (new_capacity > SCHED_CAPACITY_SCALE) | |
66 | return -EINVAL; | |
67 | ||
68 | mutex_lock(&cpu_scale_mutex); | |
69 | for_each_cpu(i, &cpu_topology[this_cpu].core_sibling) | |
4ca4f26a | 70 | topology_set_cpu_scale(i, new_capacity); |
2ef7a295 JL |
71 | mutex_unlock(&cpu_scale_mutex); |
72 | ||
73 | return count; | |
74 | } | |
75 | ||
76 | static DEVICE_ATTR_RW(cpu_capacity); | |
77 | ||
78 | static int register_cpu_capacity_sysctl(void) | |
79 | { | |
80 | int i; | |
81 | struct device *cpu; | |
82 | ||
83 | for_each_possible_cpu(i) { | |
84 | cpu = get_cpu_device(i); | |
85 | if (!cpu) { | |
86 | pr_err("%s: too early to get CPU%d device!\n", | |
87 | __func__, i); | |
88 | continue; | |
89 | } | |
90 | device_create_file(cpu, &dev_attr_cpu_capacity); | |
91 | } | |
92 | ||
93 | return 0; | |
94 | } | |
95 | subsys_initcall(register_cpu_capacity_sysctl); | |
96 | ||
97 | static u32 capacity_scale; | |
98 | static u32 *raw_capacity; | |
c105aa31 | 99 | static bool cap_parsing_failed; |
2ef7a295 | 100 | |
4ca4f26a | 101 | void topology_normalize_cpu_scale(void) |
2ef7a295 JL |
102 | { |
103 | u64 capacity; | |
104 | int cpu; | |
105 | ||
106 | if (!raw_capacity || cap_parsing_failed) | |
107 | return; | |
108 | ||
109 | pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale); | |
110 | mutex_lock(&cpu_scale_mutex); | |
111 | for_each_possible_cpu(cpu) { | |
112 | pr_debug("cpu_capacity: cpu=%d raw_capacity=%u\n", | |
113 | cpu, raw_capacity[cpu]); | |
114 | capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT) | |
115 | / capacity_scale; | |
4ca4f26a | 116 | topology_set_cpu_scale(cpu, capacity); |
2ef7a295 | 117 | pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n", |
4ca4f26a | 118 | cpu, topology_get_cpu_scale(NULL, cpu)); |
2ef7a295 JL |
119 | } |
120 | mutex_unlock(&cpu_scale_mutex); | |
121 | } | |
122 | ||
4ca4f26a | 123 | int __init topology_parse_cpu_capacity(struct device_node *cpu_node, int cpu) |
2ef7a295 JL |
124 | { |
125 | int ret = 1; | |
126 | u32 cpu_capacity; | |
127 | ||
128 | if (cap_parsing_failed) | |
129 | return !ret; | |
130 | ||
131 | ret = of_property_read_u32(cpu_node, | |
132 | "capacity-dmips-mhz", | |
133 | &cpu_capacity); | |
134 | if (!ret) { | |
135 | if (!raw_capacity) { | |
136 | raw_capacity = kcalloc(num_possible_cpus(), | |
137 | sizeof(*raw_capacity), | |
138 | GFP_KERNEL); | |
139 | if (!raw_capacity) { | |
140 | pr_err("cpu_capacity: failed to allocate memory for raw capacities\n"); | |
141 | cap_parsing_failed = true; | |
142 | return 0; | |
143 | } | |
144 | } | |
145 | capacity_scale = max(cpu_capacity, capacity_scale); | |
146 | raw_capacity[cpu] = cpu_capacity; | |
147 | pr_debug("cpu_capacity: %s cpu_capacity=%u (raw)\n", | |
148 | cpu_node->full_name, raw_capacity[cpu]); | |
149 | } else { | |
150 | if (raw_capacity) { | |
151 | pr_err("cpu_capacity: missing %s raw capacity\n", | |
152 | cpu_node->full_name); | |
153 | pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n"); | |
154 | } | |
155 | cap_parsing_failed = true; | |
156 | kfree(raw_capacity); | |
157 | } | |
158 | ||
159 | return !ret; | |
160 | } | |
161 | ||
162 | #ifdef CONFIG_CPU_FREQ | |
b5cd9d26 SH |
163 | static bool cap_parsing_done __initdata; |
164 | static cpumask_var_t cpus_to_visit __initdata; | |
165 | static void __init parsing_done_workfn(struct work_struct *work); | |
166 | static __initdata DECLARE_WORK(parsing_done_work, parsing_done_workfn); | |
2ef7a295 | 167 | |
b5cd9d26 | 168 | static int __init |
2ef7a295 JL |
169 | init_cpu_capacity_callback(struct notifier_block *nb, |
170 | unsigned long val, | |
171 | void *data) | |
172 | { | |
173 | struct cpufreq_policy *policy = data; | |
174 | int cpu; | |
175 | ||
176 | if (cap_parsing_failed || cap_parsing_done) | |
177 | return 0; | |
178 | ||
179 | switch (val) { | |
180 | case CPUFREQ_NOTIFY: | |
181 | pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n", | |
182 | cpumask_pr_args(policy->related_cpus), | |
183 | cpumask_pr_args(cpus_to_visit)); | |
184 | cpumask_andnot(cpus_to_visit, | |
185 | cpus_to_visit, | |
186 | policy->related_cpus); | |
187 | for_each_cpu(cpu, policy->related_cpus) { | |
4ca4f26a | 188 | raw_capacity[cpu] = topology_get_cpu_scale(NULL, cpu) * |
2ef7a295 JL |
189 | policy->cpuinfo.max_freq / 1000UL; |
190 | capacity_scale = max(raw_capacity[cpu], capacity_scale); | |
191 | } | |
192 | if (cpumask_empty(cpus_to_visit)) { | |
4ca4f26a | 193 | topology_normalize_cpu_scale(); |
2ef7a295 JL |
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 | } | |
200 | return 0; | |
201 | } | |
202 | ||
b5cd9d26 | 203 | static struct notifier_block init_cpu_capacity_notifier __initdata = { |
2ef7a295 JL |
204 | .notifier_call = init_cpu_capacity_callback, |
205 | }; | |
206 | ||
207 | static 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 | */ | |
c105aa31 | 214 | if (!acpi_disabled || !raw_capacity) |
2ef7a295 JL |
215 | 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 | } | |
227 | core_initcall(register_cpufreq_notifier); | |
228 | ||
b5cd9d26 | 229 | static void __init parsing_done_workfn(struct work_struct *work) |
2ef7a295 JL |
230 | { |
231 | cpufreq_unregister_notifier(&init_cpu_capacity_notifier, | |
232 | CPUFREQ_POLICY_NOTIFIER); | |
233 | } | |
234 | ||
235 | #else | |
236 | static int __init free_raw_capacity(void) | |
237 | { | |
238 | kfree(raw_capacity); | |
239 | ||
240 | return 0; | |
241 | } | |
242 | core_initcall(free_raw_capacity); | |
243 | #endif |