Skip to content

Commit a5f1b18

Browse files
vireshkwilldeacon
authored andcommitted
arm64: topology: Make AMUs work with modular cpufreq drivers
The AMU counters won't get used today if the cpufreq driver is built as a module as the amu core requires everything to be ready by late init. Fix that properly by registering for cpufreq policy notifier. Note that the amu core don't have any cpufreq dependency after the first time CPUFREQ_CREATE_POLICY notifier is called for all the CPUs. And so we don't need to do anything on the CPUFREQ_REMOVE_POLICY notifier. And for the same reason we check if the CPUs are already parsed in the beginning of amu_fie_setup() and skip if that is true. Alternatively we can shoot a work from there to unregister the notifier instead, but that seemed too much instead of this simple check. While at it, convert the print message to pr_debug instead of pr_info. Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org> Reviewed-by: Ionela Voinescu <ionela.voinescu@arm.com> Tested-by: Ionela Voinescu <ionela.voinescu@arm.com> Link: https://lore.kernel.org/r/89c1921334443e133c9c8791b4693607d65ed9f5.1610104461.git.viresh.kumar@linaro.org Signed-off-by: Will Deacon <will@kernel.org>
1 parent 47b10b7 commit a5f1b18

1 file changed

Lines changed: 48 additions & 44 deletions

File tree

‎arch/arm64/kernel/topology.c‎

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -199,69 +199,38 @@ static int freq_inv_set_max_ratio(int cpu, u64 max_rate, u64 ref_rate)
199199
return 0;
200200
}
201201

202-
static inline void
203-
enable_policy_freq_counters(int cpu, cpumask_var_t valid_cpus)
204-
{
205-
struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
206-
207-
if (!policy) {
208-
pr_debug("CPU%d: No cpufreq policy found.\n", cpu);
209-
return;
210-
}
211-
212-
if (cpumask_subset(policy->related_cpus, valid_cpus))
213-
cpumask_or(amu_fie_cpus, policy->related_cpus,
214-
amu_fie_cpus);
215-
216-
cpufreq_cpu_put(policy);
217-
}
218-
219202
static DEFINE_STATIC_KEY_FALSE(amu_fie_key);
220203
#define amu_freq_invariant() static_branch_unlikely(&amu_fie_key)
221204

222-
static int __init init_amu_fie(void)
205+
static void amu_fie_setup(const struct cpumask *cpus)
223206
{
224-
cpumask_var_t valid_cpus;
225207
bool invariant;
226-
int ret = 0;
227208
int cpu;
228209

229-
if (!zalloc_cpumask_var(&valid_cpus, GFP_KERNEL))
230-
return -ENOMEM;
231-
232-
if (!zalloc_cpumask_var(&amu_fie_cpus, GFP_KERNEL)) {
233-
ret = -ENOMEM;
234-
goto free_valid_mask;
235-
}
210+
/* We are already set since the last insmod of cpufreq driver */
211+
if (unlikely(cpumask_subset(cpus, amu_fie_cpus)))
212+
return;
236213

237-
for_each_present_cpu(cpu) {
214+
for_each_cpu(cpu, cpus) {
238215
if (!freq_counters_valid(cpu) ||
239216
freq_inv_set_max_ratio(cpu,
240217
cpufreq_get_hw_max_freq(cpu) * 1000,
241218
arch_timer_get_rate()))
242-
continue;
243-
244-
cpumask_set_cpu(cpu, valid_cpus);
245-
enable_policy_freq_counters(cpu, valid_cpus);
219+
return;
246220
}
247221

248-
/* Overwrite amu_fie_cpus if all CPUs support AMU */
249-
if (cpumask_equal(valid_cpus, cpu_present_mask))
250-
cpumask_copy(amu_fie_cpus, cpu_present_mask);
251-
252-
if (cpumask_empty(amu_fie_cpus))
253-
goto free_valid_mask;
222+
cpumask_or(amu_fie_cpus, amu_fie_cpus, cpus);
254223

255224
invariant = topology_scale_freq_invariant();
256225

257226
/* We aren't fully invariant yet */
258227
if (!invariant && !cpumask_equal(amu_fie_cpus, cpu_present_mask))
259-
goto free_valid_mask;
228+
return;
260229

261230
static_branch_enable(&amu_fie_key);
262231

263-
pr_info("CPUs[%*pbl]: counters will be used for FIE.",
264-
cpumask_pr_args(amu_fie_cpus));
232+
pr_debug("CPUs[%*pbl]: counters will be used for FIE.",
233+
cpumask_pr_args(cpus));
265234

266235
/*
267236
* Task scheduler behavior depends on frequency invariance support,
@@ -271,13 +240,48 @@ static int __init init_amu_fie(void)
271240
*/
272241
if (!invariant)
273242
rebuild_sched_domains_energy();
243+
}
244+
245+
static int init_amu_fie_callback(struct notifier_block *nb, unsigned long val,
246+
void *data)
247+
{
248+
struct cpufreq_policy *policy = data;
249+
250+
if (val == CPUFREQ_CREATE_POLICY)
251+
amu_fie_setup(policy->related_cpus);
252+
253+
/*
254+
* We don't need to handle CPUFREQ_REMOVE_POLICY event as the AMU
255+
* counters don't have any dependency on cpufreq driver once we have
256+
* initialized AMU support and enabled invariance. The AMU counters will
257+
* keep on working just fine in the absence of the cpufreq driver, and
258+
* for the CPUs for which there are no counters available, the last set
259+
* value of freq_scale will remain valid as that is the frequency those
260+
* CPUs are running at.
261+
*/
262+
263+
return 0;
264+
}
265+
266+
static struct notifier_block init_amu_fie_notifier = {
267+
.notifier_call = init_amu_fie_callback,
268+
};
269+
270+
static int __init init_amu_fie(void)
271+
{
272+
int ret;
273+
274+
if (!zalloc_cpumask_var(&amu_fie_cpus, GFP_KERNEL))
275+
return -ENOMEM;
274276

275-
free_valid_mask:
276-
free_cpumask_var(valid_cpus);
277+
ret = cpufreq_register_notifier(&init_amu_fie_notifier,
278+
CPUFREQ_POLICY_NOTIFIER);
279+
if (ret)
280+
free_cpumask_var(amu_fie_cpus);
277281

278282
return ret;
279283
}
280-
late_initcall_sync(init_amu_fie);
284+
core_initcall(init_amu_fie);
281285

282286
bool arch_freq_counters_available(const struct cpumask *cpus)
283287
{

0 commit comments

Comments
 (0)