2019-06-01 16:08:51 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2017-06-24 13:11:52 +08:00
|
|
|
/*
|
|
|
|
* x86 APERF/MPERF KHz calculation for
|
|
|
|
* /sys/.../cpufreq/scaling_cur_freq
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Intel Corp.
|
|
|
|
* Author: Len Brown <len.brown@intel.com>
|
|
|
|
*/
|
2022-04-16 03:19:51 +08:00
|
|
|
#include <linux/cpufreq.h>
|
2017-07-28 20:45:03 +08:00
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/ktime.h>
|
2017-06-24 13:11:52 +08:00
|
|
|
#include <linux/math64.h>
|
|
|
|
#include <linux/percpu.h>
|
2020-09-04 06:23:29 +08:00
|
|
|
#include <linux/rcupdate.h>
|
2022-04-16 03:19:51 +08:00
|
|
|
#include <linux/sched/isolation.h>
|
|
|
|
#include <linux/sched/topology.h>
|
|
|
|
#include <linux/smp.h>
|
|
|
|
#include <linux/syscore_ops.h>
|
|
|
|
|
2022-04-16 03:19:59 +08:00
|
|
|
#include <asm/cpu.h>
|
2022-04-16 03:19:51 +08:00
|
|
|
#include <asm/cpu_device_id.h>
|
|
|
|
#include <asm/intel-family.h>
|
2017-06-24 13:11:52 +08:00
|
|
|
|
x86 / CPU: Always show current CPU frequency in /proc/cpuinfo
After commit 890da9cf0983 (Revert "x86: do not use cpufreq_quick_get()
for /proc/cpuinfo "cpu MHz"") the "cpu MHz" number in /proc/cpuinfo
on x86 can be either the nominal CPU frequency (which is constant)
or the frequency most recently requested by a scaling governor in
cpufreq, depending on the cpufreq configuration. That is somewhat
inconsistent and is different from what it was before 4.13, so in
order to restore the previous behavior, make it report the current
CPU frequency like the scaling_cur_freq sysfs file in cpufreq.
To that end, modify the /proc/cpuinfo implementation on x86 to use
aperfmperf_snapshot_khz() to snapshot the APERF and MPERF feedback
registers, if available, and use their values to compute the CPU
frequency to be reported as "cpu MHz".
However, do that carefully enough to avoid accumulating delays that
lead to unacceptable access times for /proc/cpuinfo on systems with
many CPUs. Run aperfmperf_snapshot_khz() once on all CPUs
asynchronously at the /proc/cpuinfo open time, add a single delay
upfront (if necessary) at that point and simply compute the current
frequency while running show_cpuinfo() for each individual CPU.
Also, to avoid slowing down /proc/cpuinfo accesses too much, reduce
the default delay between consecutive APERF and MPERF reads to 10 ms,
which should be sufficient to get large enough numbers for the
frequency computation in all cases.
Fixes: 890da9cf0983 (Revert "x86: do not use cpufreq_quick_get() for /proc/cpuinfo "cpu MHz"")
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Thomas Gleixner <tglx@linutronix.de>
Tested-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Ingo Molnar <mingo@kernel.org>
2017-11-15 09:13:40 +08:00
|
|
|
#include "cpu.h"
|
|
|
|
|
2022-04-16 03:19:56 +08:00
|
|
|
struct aperfmperf {
|
2022-04-16 03:20:01 +08:00
|
|
|
seqcount_t seq;
|
|
|
|
unsigned long last_update;
|
|
|
|
u64 acnt;
|
|
|
|
u64 mcnt;
|
2022-04-16 03:19:56 +08:00
|
|
|
u64 aperf;
|
|
|
|
u64 mperf;
|
|
|
|
};
|
|
|
|
|
2022-04-16 03:20:01 +08:00
|
|
|
static DEFINE_PER_CPU_SHARED_ALIGNED(struct aperfmperf, cpu_samples) = {
|
|
|
|
.seq = SEQCNT_ZERO(cpu_samples.seq)
|
|
|
|
};
|
2022-04-16 03:19:56 +08:00
|
|
|
|
2022-04-16 03:19:59 +08:00
|
|
|
static void init_counter_refs(void)
|
|
|
|
{
|
|
|
|
u64 aperf, mperf;
|
|
|
|
|
|
|
|
rdmsrl(MSR_IA32_APERF, aperf);
|
|
|
|
rdmsrl(MSR_IA32_MPERF, mperf);
|
|
|
|
|
|
|
|
this_cpu_write(cpu_samples.aperf, aperf);
|
|
|
|
this_cpu_write(cpu_samples.mperf, mperf);
|
|
|
|
}
|
|
|
|
|
2022-04-16 03:19:51 +08:00
|
|
|
#if defined(CONFIG_X86_64) && defined(CONFIG_SMP)
|
|
|
|
/*
|
|
|
|
* APERF/MPERF frequency ratio computation.
|
|
|
|
*
|
|
|
|
* The scheduler wants to do frequency invariant accounting and needs a <1
|
|
|
|
* ratio to account for the 'current' frequency, corresponding to
|
|
|
|
* freq_curr / freq_max.
|
|
|
|
*
|
|
|
|
* Since the frequency freq_curr on x86 is controlled by micro-controller and
|
|
|
|
* our P-state setting is little more than a request/hint, we need to observe
|
|
|
|
* the effective frequency 'BusyMHz', i.e. the average frequency over a time
|
|
|
|
* interval after discarding idle time. This is given by:
|
|
|
|
*
|
|
|
|
* BusyMHz = delta_APERF / delta_MPERF * freq_base
|
|
|
|
*
|
|
|
|
* where freq_base is the max non-turbo P-state.
|
|
|
|
*
|
|
|
|
* The freq_max term has to be set to a somewhat arbitrary value, because we
|
|
|
|
* can't know which turbo states will be available at a given point in time:
|
|
|
|
* it all depends on the thermal headroom of the entire package. We set it to
|
|
|
|
* the turbo level with 4 cores active.
|
|
|
|
*
|
|
|
|
* Benchmarks show that's a good compromise between the 1C turbo ratio
|
|
|
|
* (freq_curr/freq_max would rarely reach 1) and something close to freq_base,
|
|
|
|
* which would ignore the entire turbo range (a conspicuous part, making
|
|
|
|
* freq_curr/freq_max always maxed out).
|
|
|
|
*
|
|
|
|
* An exception to the heuristic above is the Atom uarch, where we choose the
|
|
|
|
* highest turbo level for freq_max since Atom's are generally oriented towards
|
|
|
|
* power efficiency.
|
|
|
|
*
|
|
|
|
* Setting freq_max to anything less than the 1C turbo ratio makes the ratio
|
|
|
|
* freq_curr / freq_max to eventually grow >1, in which case we clip it to 1.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEFINE_STATIC_KEY_FALSE(arch_scale_freq_key);
|
|
|
|
|
|
|
|
static u64 arch_turbo_freq_ratio = SCHED_CAPACITY_SCALE;
|
|
|
|
static u64 arch_max_freq_ratio = SCHED_CAPACITY_SCALE;
|
|
|
|
|
|
|
|
void arch_set_max_freq_ratio(bool turbo_disabled)
|
|
|
|
{
|
|
|
|
arch_max_freq_ratio = turbo_disabled ? SCHED_CAPACITY_SCALE :
|
|
|
|
arch_turbo_freq_ratio;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(arch_set_max_freq_ratio);
|
|
|
|
|
2022-04-16 03:19:54 +08:00
|
|
|
static bool __init turbo_disabled(void)
|
2022-04-16 03:19:51 +08:00
|
|
|
{
|
|
|
|
u64 misc_en;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = rdmsrl_safe(MSR_IA32_MISC_ENABLE, &misc_en);
|
|
|
|
if (err)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return (misc_en & MSR_IA32_MISC_ENABLE_TURBO_DISABLE);
|
|
|
|
}
|
|
|
|
|
2022-04-16 03:19:54 +08:00
|
|
|
static bool __init slv_set_max_freq_ratio(u64 *base_freq, u64 *turbo_freq)
|
2022-04-16 03:19:51 +08:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = rdmsrl_safe(MSR_ATOM_CORE_RATIOS, base_freq);
|
|
|
|
if (err)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
err = rdmsrl_safe(MSR_ATOM_CORE_TURBO_RATIOS, turbo_freq);
|
|
|
|
if (err)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*base_freq = (*base_freq >> 16) & 0x3F; /* max P state */
|
|
|
|
*turbo_freq = *turbo_freq & 0x3F; /* 1C turbo */
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define X86_MATCH(model) \
|
|
|
|
X86_MATCH_VENDOR_FAM_MODEL_FEATURE(INTEL, 6, \
|
|
|
|
INTEL_FAM6_##model, X86_FEATURE_APERFMPERF, NULL)
|
|
|
|
|
2022-04-16 03:19:54 +08:00
|
|
|
static const struct x86_cpu_id has_knl_turbo_ratio_limits[] __initconst = {
|
2022-04-16 03:19:51 +08:00
|
|
|
X86_MATCH(XEON_PHI_KNL),
|
|
|
|
X86_MATCH(XEON_PHI_KNM),
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2022-04-16 03:19:54 +08:00
|
|
|
static const struct x86_cpu_id has_skx_turbo_ratio_limits[] __initconst = {
|
2022-04-16 03:19:51 +08:00
|
|
|
X86_MATCH(SKYLAKE_X),
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2022-04-16 03:19:54 +08:00
|
|
|
static const struct x86_cpu_id has_glm_turbo_ratio_limits[] __initconst = {
|
2022-04-16 03:19:51 +08:00
|
|
|
X86_MATCH(ATOM_GOLDMONT),
|
|
|
|
X86_MATCH(ATOM_GOLDMONT_D),
|
|
|
|
X86_MATCH(ATOM_GOLDMONT_PLUS),
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2022-04-16 03:19:54 +08:00
|
|
|
static bool __init knl_set_max_freq_ratio(u64 *base_freq, u64 *turbo_freq,
|
|
|
|
int num_delta_fratio)
|
2022-04-16 03:19:51 +08:00
|
|
|
{
|
|
|
|
int fratio, delta_fratio, found;
|
|
|
|
int err, i;
|
|
|
|
u64 msr;
|
|
|
|
|
|
|
|
err = rdmsrl_safe(MSR_PLATFORM_INFO, base_freq);
|
|
|
|
if (err)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*base_freq = (*base_freq >> 8) & 0xFF; /* max P state */
|
|
|
|
|
|
|
|
err = rdmsrl_safe(MSR_TURBO_RATIO_LIMIT, &msr);
|
|
|
|
if (err)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
fratio = (msr >> 8) & 0xFF;
|
|
|
|
i = 16;
|
|
|
|
found = 0;
|
|
|
|
do {
|
|
|
|
if (found >= num_delta_fratio) {
|
|
|
|
*turbo_freq = fratio;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
delta_fratio = (msr >> (i + 5)) & 0x7;
|
|
|
|
|
|
|
|
if (delta_fratio) {
|
|
|
|
found += 1;
|
|
|
|
fratio -= delta_fratio;
|
|
|
|
}
|
|
|
|
|
|
|
|
i += 8;
|
|
|
|
} while (i < 64);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-16 03:19:54 +08:00
|
|
|
static bool __init skx_set_max_freq_ratio(u64 *base_freq, u64 *turbo_freq, int size)
|
2022-04-16 03:19:51 +08:00
|
|
|
{
|
|
|
|
u64 ratios, counts;
|
|
|
|
u32 group_size;
|
|
|
|
int err, i;
|
|
|
|
|
|
|
|
err = rdmsrl_safe(MSR_PLATFORM_INFO, base_freq);
|
|
|
|
if (err)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*base_freq = (*base_freq >> 8) & 0xFF; /* max P state */
|
|
|
|
|
|
|
|
err = rdmsrl_safe(MSR_TURBO_RATIO_LIMIT, &ratios);
|
|
|
|
if (err)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
err = rdmsrl_safe(MSR_TURBO_RATIO_LIMIT1, &counts);
|
|
|
|
if (err)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (i = 0; i < 64; i += 8) {
|
|
|
|
group_size = (counts >> i) & 0xFF;
|
|
|
|
if (group_size >= size) {
|
|
|
|
*turbo_freq = (ratios >> i) & 0xFF;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-04-16 03:19:54 +08:00
|
|
|
static bool __init core_set_max_freq_ratio(u64 *base_freq, u64 *turbo_freq)
|
2022-04-16 03:19:51 +08:00
|
|
|
{
|
|
|
|
u64 msr;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = rdmsrl_safe(MSR_PLATFORM_INFO, base_freq);
|
|
|
|
if (err)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
err = rdmsrl_safe(MSR_TURBO_RATIO_LIMIT, &msr);
|
|
|
|
if (err)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*base_freq = (*base_freq >> 8) & 0xFF; /* max P state */
|
|
|
|
*turbo_freq = (msr >> 24) & 0xFF; /* 4C turbo */
|
|
|
|
|
|
|
|
/* The CPU may have less than 4 cores */
|
|
|
|
if (!*turbo_freq)
|
|
|
|
*turbo_freq = msr & 0xFF; /* 1C turbo */
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-04-16 03:19:54 +08:00
|
|
|
static bool __init intel_set_max_freq_ratio(void)
|
2022-04-16 03:19:51 +08:00
|
|
|
{
|
|
|
|
u64 base_freq, turbo_freq;
|
|
|
|
u64 turbo_ratio;
|
|
|
|
|
|
|
|
if (slv_set_max_freq_ratio(&base_freq, &turbo_freq))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (x86_match_cpu(has_glm_turbo_ratio_limits) &&
|
|
|
|
skx_set_max_freq_ratio(&base_freq, &turbo_freq, 1))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (x86_match_cpu(has_knl_turbo_ratio_limits) &&
|
|
|
|
knl_set_max_freq_ratio(&base_freq, &turbo_freq, 1))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (x86_match_cpu(has_skx_turbo_ratio_limits) &&
|
|
|
|
skx_set_max_freq_ratio(&base_freq, &turbo_freq, 4))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (core_set_max_freq_ratio(&base_freq, &turbo_freq))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
out:
|
|
|
|
/*
|
|
|
|
* Some hypervisors advertise X86_FEATURE_APERFMPERF
|
|
|
|
* but then fill all MSR's with zeroes.
|
|
|
|
* Some CPUs have turbo boost but don't declare any turbo ratio
|
|
|
|
* in MSR_TURBO_RATIO_LIMIT.
|
|
|
|
*/
|
|
|
|
if (!base_freq || !turbo_freq) {
|
|
|
|
pr_debug("Couldn't determine cpu base or turbo frequency, necessary for scale-invariant accounting.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
turbo_ratio = div_u64(turbo_freq * SCHED_CAPACITY_SCALE, base_freq);
|
|
|
|
if (!turbo_ratio) {
|
|
|
|
pr_debug("Non-zero turbo and base frequencies led to a 0 ratio.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
arch_turbo_freq_ratio = turbo_ratio;
|
|
|
|
arch_set_max_freq_ratio(turbo_disabled());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_PM_SLEEP
|
|
|
|
static struct syscore_ops freq_invariance_syscore_ops = {
|
|
|
|
.resume = init_counter_refs,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void register_freq_invariance_syscore_ops(void)
|
|
|
|
{
|
|
|
|
register_syscore_ops(&freq_invariance_syscore_ops);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline void register_freq_invariance_syscore_ops(void) {}
|
|
|
|
#endif
|
|
|
|
|
2022-04-16 03:19:54 +08:00
|
|
|
static void freq_invariance_enable(void)
|
|
|
|
{
|
|
|
|
if (static_branch_unlikely(&arch_scale_freq_key)) {
|
|
|
|
WARN_ON_ONCE(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
static_branch_enable(&arch_scale_freq_key);
|
|
|
|
register_freq_invariance_syscore_ops();
|
|
|
|
pr_info("Estimated ratio of average max frequency by base frequency (times 1024): %llu\n", arch_max_freq_ratio);
|
|
|
|
}
|
|
|
|
|
|
|
|
void freq_invariance_set_perf_ratio(u64 ratio, bool turbo_disabled)
|
2022-04-16 03:19:51 +08:00
|
|
|
{
|
2022-04-16 03:19:54 +08:00
|
|
|
arch_turbo_freq_ratio = ratio;
|
|
|
|
arch_set_max_freq_ratio(turbo_disabled);
|
|
|
|
freq_invariance_enable();
|
|
|
|
}
|
2022-04-16 03:19:51 +08:00
|
|
|
|
2022-04-16 03:19:59 +08:00
|
|
|
static void __init bp_init_freq_invariance(void)
|
2022-04-16 03:19:54 +08:00
|
|
|
{
|
|
|
|
if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
|
|
|
|
return;
|
2022-04-16 03:19:51 +08:00
|
|
|
|
2022-04-16 03:19:54 +08:00
|
|
|
if (intel_set_max_freq_ratio())
|
|
|
|
freq_invariance_enable();
|
2022-04-16 03:19:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void disable_freq_invariance_workfn(struct work_struct *work)
|
|
|
|
{
|
2023-01-11 00:02:06 +08:00
|
|
|
int cpu;
|
|
|
|
|
2022-04-16 03:19:51 +08:00
|
|
|
static_branch_disable(&arch_scale_freq_key);
|
2023-01-11 00:02:06 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set arch_freq_scale to a default value on all cpus
|
|
|
|
* This negates the effect of scaling
|
|
|
|
*/
|
|
|
|
for_each_possible_cpu(cpu)
|
|
|
|
per_cpu(arch_freq_scale, cpu) = SCHED_CAPACITY_SCALE;
|
2022-04-16 03:19:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static DECLARE_WORK(disable_freq_invariance_work,
|
|
|
|
disable_freq_invariance_workfn);
|
|
|
|
|
|
|
|
DEFINE_PER_CPU(unsigned long, arch_freq_scale) = SCHED_CAPACITY_SCALE;
|
|
|
|
|
2022-04-16 03:19:57 +08:00
|
|
|
static void scale_freq_tick(u64 acnt, u64 mcnt)
|
2022-04-16 03:19:51 +08:00
|
|
|
{
|
2022-04-16 03:19:57 +08:00
|
|
|
u64 freq_scale;
|
2022-04-16 03:19:51 +08:00
|
|
|
|
2022-04-16 03:19:59 +08:00
|
|
|
if (!arch_scale_freq_invariant())
|
|
|
|
return;
|
|
|
|
|
2022-04-16 03:19:51 +08:00
|
|
|
if (check_shl_overflow(acnt, 2*SCHED_CAPACITY_SHIFT, &acnt))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (check_mul_overflow(mcnt, arch_max_freq_ratio, &mcnt) || !mcnt)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
freq_scale = div64_u64(acnt, mcnt);
|
|
|
|
if (!freq_scale)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (freq_scale > SCHED_CAPACITY_SCALE)
|
|
|
|
freq_scale = SCHED_CAPACITY_SCALE;
|
|
|
|
|
|
|
|
this_cpu_write(arch_freq_scale, freq_scale);
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
pr_warn("Scheduler frequency invariance went wobbly, disabling!\n");
|
|
|
|
schedule_work(&disable_freq_invariance_work);
|
|
|
|
}
|
2022-04-16 03:19:59 +08:00
|
|
|
#else
|
|
|
|
static inline void bp_init_freq_invariance(void) { }
|
|
|
|
static inline void scale_freq_tick(u64 acnt, u64 mcnt) { }
|
|
|
|
#endif /* CONFIG_X86_64 && CONFIG_SMP */
|
2022-04-16 03:19:57 +08:00
|
|
|
|
|
|
|
void arch_scale_freq_tick(void)
|
|
|
|
{
|
|
|
|
struct aperfmperf *s = this_cpu_ptr(&cpu_samples);
|
|
|
|
u64 acnt, mcnt, aperf, mperf;
|
|
|
|
|
2022-04-16 03:19:59 +08:00
|
|
|
if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF))
|
2022-04-16 03:19:57 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
rdmsrl(MSR_IA32_APERF, aperf);
|
|
|
|
rdmsrl(MSR_IA32_MPERF, mperf);
|
|
|
|
acnt = aperf - s->aperf;
|
|
|
|
mcnt = mperf - s->mperf;
|
|
|
|
|
|
|
|
s->aperf = aperf;
|
|
|
|
s->mperf = mperf;
|
|
|
|
|
2022-04-16 03:20:01 +08:00
|
|
|
raw_write_seqcount_begin(&s->seq);
|
|
|
|
s->last_update = jiffies;
|
|
|
|
s->acnt = acnt;
|
|
|
|
s->mcnt = mcnt;
|
|
|
|
raw_write_seqcount_end(&s->seq);
|
|
|
|
|
2022-04-16 03:19:57 +08:00
|
|
|
scale_freq_tick(acnt, mcnt);
|
|
|
|
}
|
2022-04-16 03:19:59 +08:00
|
|
|
|
2022-04-16 03:20:02 +08:00
|
|
|
/*
|
|
|
|
* Discard samples older than the define maximum sample age of 20ms. There
|
|
|
|
* is no point in sending IPIs in such a case. If the scheduler tick was
|
|
|
|
* not running then the CPU is either idle or isolated.
|
|
|
|
*/
|
|
|
|
#define MAX_SAMPLE_AGE ((unsigned long)HZ / 50)
|
|
|
|
|
2022-04-16 03:20:04 +08:00
|
|
|
unsigned int arch_freq_get_on_cpu(int cpu)
|
2022-04-16 03:20:02 +08:00
|
|
|
{
|
|
|
|
struct aperfmperf *s = per_cpu_ptr(&cpu_samples, cpu);
|
2022-04-25 23:45:42 +08:00
|
|
|
unsigned int seq, freq;
|
2022-04-16 03:20:02 +08:00
|
|
|
unsigned long last;
|
|
|
|
u64 acnt, mcnt;
|
|
|
|
|
|
|
|
if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF))
|
2022-04-25 23:45:42 +08:00
|
|
|
goto fallback;
|
2022-04-16 03:20:02 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
seq = raw_read_seqcount_begin(&s->seq);
|
|
|
|
last = s->last_update;
|
|
|
|
acnt = s->acnt;
|
|
|
|
mcnt = s->mcnt;
|
|
|
|
} while (read_seqcount_retry(&s->seq, seq));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bail on invalid count and when the last update was too long ago,
|
|
|
|
* which covers idle and NOHZ full CPUs.
|
|
|
|
*/
|
|
|
|
if (!mcnt || (jiffies - last) > MAX_SAMPLE_AGE)
|
2022-04-25 23:45:42 +08:00
|
|
|
goto fallback;
|
2022-04-16 03:20:02 +08:00
|
|
|
|
|
|
|
return div64_u64((cpu_khz * acnt), mcnt);
|
2022-04-25 23:45:42 +08:00
|
|
|
|
|
|
|
fallback:
|
|
|
|
freq = cpufreq_quick_get(cpu);
|
|
|
|
return freq ? freq : cpu_khz;
|
2022-04-16 03:20:02 +08:00
|
|
|
}
|
|
|
|
|
2022-04-16 03:19:59 +08:00
|
|
|
static int __init bp_init_aperfmperf(void)
|
|
|
|
{
|
|
|
|
if (!cpu_feature_enabled(X86_FEATURE_APERFMPERF))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
init_counter_refs();
|
|
|
|
bp_init_freq_invariance();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
early_initcall(bp_init_aperfmperf);
|
|
|
|
|
|
|
|
void ap_init_aperfmperf(void)
|
|
|
|
{
|
|
|
|
if (cpu_feature_enabled(X86_FEATURE_APERFMPERF))
|
|
|
|
init_counter_refs();
|
|
|
|
}
|