Skip to content

Commit dcabe10

Browse files
committed
Merge branch 'for-next/topology' into for-next/core
Cleanup to the AMU support code and initialisation rework to support cpufreq drivers built as modules. * for-next/topology: arm64: topology: Make AMUs work with modular cpufreq drivers arm64: topology: Reorder init_amu_fie() a bit arm64: topology: Avoid the have_policy check
2 parents d23fa87 + a5f1b18 commit dcabe10

1 file changed

Lines changed: 56 additions & 59 deletions

File tree

‎arch/arm64/kernel/topology.c‎

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

202-
static inline bool
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 false;
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-
return true;
219-
}
220-
221202
static DEFINE_STATIC_KEY_FALSE(amu_fie_key);
222203
#define amu_freq_invariant() static_branch_unlikely(&amu_fie_key)
223204

224-
static int __init init_amu_fie(void)
205+
static void amu_fie_setup(const struct cpumask *cpus)
225206
{
226-
bool invariance_status = topology_scale_freq_invariant();
227-
cpumask_var_t valid_cpus;
228-
bool have_policy = false;
229-
int ret = 0;
207+
bool invariant;
230208
int cpu;
231209

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

240-
for_each_present_cpu(cpu) {
214+
for_each_cpu(cpu, cpus) {
241215
if (!freq_counters_valid(cpu) ||
242216
freq_inv_set_max_ratio(cpu,
243217
cpufreq_get_hw_max_freq(cpu) * 1000,
244218
arch_timer_get_rate()))
245-
continue;
246-
247-
cpumask_set_cpu(cpu, valid_cpus);
248-
have_policy |= enable_policy_freq_counters(cpu, valid_cpus);
219+
return;
249220
}
250221

251-
/*
252-
* If we are not restricted by cpufreq policies, we only enable
253-
* the use of the AMU feature for FIE if all CPUs support AMU.
254-
* Otherwise, enable_policy_freq_counters has already enabled
255-
* policy cpus.
256-
*/
257-
if (!have_policy && cpumask_equal(valid_cpus, cpu_present_mask))
258-
cpumask_or(amu_fie_cpus, amu_fie_cpus, valid_cpus);
222+
cpumask_or(amu_fie_cpus, amu_fie_cpus, cpus);
259223

260-
if (!cpumask_empty(amu_fie_cpus)) {
261-
pr_info("CPUs[%*pbl]: counters will be used for FIE.",
262-
cpumask_pr_args(amu_fie_cpus));
263-
static_branch_enable(&amu_fie_key);
264-
}
224+
invariant = topology_scale_freq_invariant();
265225

266-
/*
267-
* If the system is not fully invariant after AMU init, disable
268-
* partial use of counters for frequency invariance.
269-
*/
270-
if (!topology_scale_freq_invariant())
271-
static_branch_disable(&amu_fie_key);
226+
/* We aren't fully invariant yet */
227+
if (!invariant && !cpumask_equal(amu_fie_cpus, cpu_present_mask))
228+
return;
229+
230+
static_branch_enable(&amu_fie_key);
231+
232+
pr_debug("CPUs[%*pbl]: counters will be used for FIE.",
233+
cpumask_pr_args(cpus));
272234

273235
/*
274236
* Task scheduler behavior depends on frequency invariance support,
275237
* either cpufreq or counter driven. If the support status changes as
276238
* a result of counter initialisation and use, retrigger the build of
277239
* scheduling domains to ensure the information is propagated properly.
278240
*/
279-
if (invariance_status != topology_scale_freq_invariant())
241+
if (!invariant)
280242
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;
281276

282-
free_valid_mask:
283-
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);
284281

285282
return ret;
286283
}
287-
late_initcall_sync(init_amu_fie);
284+
core_initcall(init_amu_fie);
288285

289286
bool arch_freq_counters_available(const struct cpumask *cpus)
290287
{

0 commit comments

Comments
 (0)