|
44 | 44 | * to run the rebalance_domains for all idle cores and the cpu_capacity can be |
45 | 45 | * updated during this sequence. |
46 | 46 | */ |
47 | | -static DEFINE_PER_CPU(unsigned long, cpu_scale) = SCHED_CAPACITY_SCALE; |
48 | | -static DEFINE_MUTEX(cpu_scale_mutex); |
49 | 47 |
|
50 | | -unsigned long arch_scale_cpu_capacity(struct sched_domain *sd, int cpu) |
51 | | -{ |
52 | | - return per_cpu(cpu_scale, cpu); |
53 | | -} |
54 | | - |
55 | | -static void set_capacity_scale(unsigned int cpu, unsigned long capacity) |
56 | | -{ |
57 | | - per_cpu(cpu_scale, cpu) = capacity; |
58 | | -} |
59 | | - |
60 | | -static ssize_t cpu_capacity_show(struct device *dev, |
61 | | - struct device_attribute *attr, |
62 | | - char *buf) |
63 | | -{ |
64 | | - struct cpu *cpu = container_of(dev, struct cpu, dev); |
65 | | - |
66 | | - return sprintf(buf, "%lu\n", |
67 | | - arch_scale_cpu_capacity(NULL, cpu->dev.id)); |
68 | | -} |
69 | | - |
70 | | -static ssize_t cpu_capacity_store(struct device *dev, |
71 | | - struct device_attribute *attr, |
72 | | - const char *buf, |
73 | | - size_t count) |
74 | | -{ |
75 | | - struct cpu *cpu = container_of(dev, struct cpu, dev); |
76 | | - int this_cpu = cpu->dev.id, i; |
77 | | - unsigned long new_capacity; |
78 | | - ssize_t ret; |
79 | | - |
80 | | - if (count) { |
81 | | - ret = kstrtoul(buf, 0, &new_capacity); |
82 | | - if (ret) |
83 | | - return ret; |
84 | | - if (new_capacity > SCHED_CAPACITY_SCALE) |
85 | | - return -EINVAL; |
86 | | - |
87 | | - mutex_lock(&cpu_scale_mutex); |
88 | | - for_each_cpu(i, &cpu_topology[this_cpu].core_sibling) |
89 | | - set_capacity_scale(i, new_capacity); |
90 | | - mutex_unlock(&cpu_scale_mutex); |
91 | | - } |
92 | | - |
93 | | - return count; |
94 | | -} |
95 | | - |
96 | | -static DEVICE_ATTR_RW(cpu_capacity); |
97 | | - |
98 | | -static int register_cpu_capacity_sysctl(void) |
99 | | -{ |
100 | | - int i; |
101 | | - struct device *cpu; |
102 | | - |
103 | | - for_each_possible_cpu(i) { |
104 | | - cpu = get_cpu_device(i); |
105 | | - if (!cpu) { |
106 | | - pr_err("%s: too early to get CPU%d device!\n", |
107 | | - __func__, i); |
108 | | - continue; |
109 | | - } |
110 | | - device_create_file(cpu, &dev_attr_cpu_capacity); |
111 | | - } |
112 | | - |
113 | | - return 0; |
114 | | -} |
115 | | -subsys_initcall(register_cpu_capacity_sysctl); |
| 48 | +extern unsigned long |
| 49 | +arch_scale_cpu_capacity(struct sched_domain *sd, int cpu); |
| 50 | +extern void set_capacity_scale(unsigned int cpu, unsigned long capacity); |
116 | 51 |
|
117 | 52 | #ifdef CONFIG_OF |
118 | 53 | struct cpu_efficiency { |
@@ -141,145 +76,9 @@ static unsigned long *__cpu_capacity; |
141 | 76 |
|
142 | 77 | static unsigned long middle_capacity = 1; |
143 | 78 | static bool cap_from_dt = true; |
144 | | -static u32 *raw_capacity; |
145 | | -static bool cap_parsing_failed; |
146 | | -static u32 capacity_scale; |
147 | | - |
148 | | -static int __init parse_cpu_capacity(struct device_node *cpu_node, int cpu) |
149 | | -{ |
150 | | - int ret = 1; |
151 | | - u32 cpu_capacity; |
152 | | - |
153 | | - if (cap_parsing_failed) |
154 | | - return !ret; |
155 | | - |
156 | | - ret = of_property_read_u32(cpu_node, |
157 | | - "capacity-dmips-mhz", |
158 | | - &cpu_capacity); |
159 | | - if (!ret) { |
160 | | - if (!raw_capacity) { |
161 | | - raw_capacity = kcalloc(num_possible_cpus(), |
162 | | - sizeof(*raw_capacity), |
163 | | - GFP_KERNEL); |
164 | | - if (!raw_capacity) { |
165 | | - pr_err("cpu_capacity: failed to allocate memory for raw capacities\n"); |
166 | | - cap_parsing_failed = true; |
167 | | - return 0; |
168 | | - } |
169 | | - } |
170 | | - capacity_scale = max(cpu_capacity, capacity_scale); |
171 | | - raw_capacity[cpu] = cpu_capacity; |
172 | | - pr_debug("cpu_capacity: %s cpu_capacity=%u (raw)\n", |
173 | | - cpu_node->full_name, raw_capacity[cpu]); |
174 | | - } else { |
175 | | - if (raw_capacity) { |
176 | | - pr_err("cpu_capacity: missing %s raw capacity\n", |
177 | | - cpu_node->full_name); |
178 | | - pr_err("cpu_capacity: partial information: fallback to 1024 for all CPUs\n"); |
179 | | - } |
180 | | - cap_parsing_failed = true; |
181 | | - kfree(raw_capacity); |
182 | | - } |
183 | | - |
184 | | - return !ret; |
185 | | -} |
186 | | - |
187 | | -static void normalize_cpu_capacity(void) |
188 | | -{ |
189 | | - u64 capacity; |
190 | | - int cpu; |
191 | | - |
192 | | - if (!raw_capacity || cap_parsing_failed) |
193 | | - return; |
194 | | - |
195 | | - pr_debug("cpu_capacity: capacity_scale=%u\n", capacity_scale); |
196 | | - mutex_lock(&cpu_scale_mutex); |
197 | | - for_each_possible_cpu(cpu) { |
198 | | - capacity = (raw_capacity[cpu] << SCHED_CAPACITY_SHIFT) |
199 | | - / capacity_scale; |
200 | | - set_capacity_scale(cpu, capacity); |
201 | | - pr_debug("cpu_capacity: CPU%d cpu_capacity=%lu\n", |
202 | | - cpu, arch_scale_cpu_capacity(NULL, cpu)); |
203 | | - } |
204 | | - mutex_unlock(&cpu_scale_mutex); |
205 | | -} |
206 | | - |
207 | | -#ifdef CONFIG_CPU_FREQ |
208 | | -static cpumask_var_t cpus_to_visit; |
209 | | -static bool cap_parsing_done; |
210 | | -static void parsing_done_workfn(struct work_struct *work); |
211 | | -static DECLARE_WORK(parsing_done_work, parsing_done_workfn); |
212 | | - |
213 | | -static int |
214 | | -init_cpu_capacity_callback(struct notifier_block *nb, |
215 | | - unsigned long val, |
216 | | - void *data) |
217 | | -{ |
218 | | - struct cpufreq_policy *policy = data; |
219 | | - int cpu; |
220 | | - |
221 | | - if (cap_parsing_failed || cap_parsing_done) |
222 | | - return 0; |
223 | | - |
224 | | - switch (val) { |
225 | | - case CPUFREQ_NOTIFY: |
226 | | - pr_debug("cpu_capacity: init cpu capacity for CPUs [%*pbl] (to_visit=%*pbl)\n", |
227 | | - cpumask_pr_args(policy->related_cpus), |
228 | | - cpumask_pr_args(cpus_to_visit)); |
229 | | - cpumask_andnot(cpus_to_visit, |
230 | | - cpus_to_visit, |
231 | | - policy->related_cpus); |
232 | | - for_each_cpu(cpu, policy->related_cpus) { |
233 | | - raw_capacity[cpu] = arch_scale_cpu_capacity(NULL, cpu) * |
234 | | - policy->cpuinfo.max_freq / 1000UL; |
235 | | - capacity_scale = max(raw_capacity[cpu], capacity_scale); |
236 | | - } |
237 | | - if (cpumask_empty(cpus_to_visit)) { |
238 | | - normalize_cpu_capacity(); |
239 | | - kfree(raw_capacity); |
240 | | - pr_debug("cpu_capacity: parsing done\n"); |
241 | | - cap_parsing_done = true; |
242 | | - schedule_work(&parsing_done_work); |
243 | | - } |
244 | | - } |
245 | | - return 0; |
246 | | -} |
247 | | - |
248 | | -static struct notifier_block init_cpu_capacity_notifier = { |
249 | | - .notifier_call = init_cpu_capacity_callback, |
250 | | -}; |
251 | | - |
252 | | -static int __init register_cpufreq_notifier(void) |
253 | | -{ |
254 | | - if (cap_parsing_failed) |
255 | | - return -EINVAL; |
256 | | - |
257 | | - if (!alloc_cpumask_var(&cpus_to_visit, GFP_KERNEL)) { |
258 | | - pr_err("cpu_capacity: failed to allocate memory for cpus_to_visit\n"); |
259 | | - return -ENOMEM; |
260 | | - } |
261 | | - cpumask_copy(cpus_to_visit, cpu_possible_mask); |
262 | | - |
263 | | - return cpufreq_register_notifier(&init_cpu_capacity_notifier, |
264 | | - CPUFREQ_POLICY_NOTIFIER); |
265 | | -} |
266 | | -core_initcall(register_cpufreq_notifier); |
267 | | - |
268 | | -static void parsing_done_workfn(struct work_struct *work) |
269 | | -{ |
270 | | - cpufreq_unregister_notifier(&init_cpu_capacity_notifier, |
271 | | - CPUFREQ_POLICY_NOTIFIER); |
272 | | -} |
273 | | - |
274 | | -#else |
275 | | -static int __init free_raw_capacity(void) |
276 | | -{ |
277 | | - kfree(raw_capacity); |
278 | | - |
279 | | - return 0; |
280 | | -} |
281 | | -core_initcall(free_raw_capacity); |
282 | | -#endif |
| 79 | +extern bool cap_parsing_failed; |
| 80 | +extern void normalize_cpu_capacity(void); |
| 81 | +extern int __init parse_cpu_capacity(struct device_node *cpu_node, int cpu); |
283 | 82 |
|
284 | 83 | /* |
285 | 84 | * Iterate all CPUs' descriptor in DT and compute the efficiency |
|
0 commit comments