2 * x86 APERF/MPERF KHz calculation for
3 * /sys/.../cpufreq/scaling_cur_freq
5 * Copyright (C) 2017 Intel Corp.
6 * Author: Len Brown <len.brown@intel.com>
8 * This file is licensed under GPLv2.
11 #include <linux/jiffies.h>
12 #include <linux/math64.h>
13 #include <linux/percpu.h>
14 #include <linux/smp.h>
16 struct aperfmperf_sample {
18 unsigned long jiffies;
23 static DEFINE_PER_CPU(struct aperfmperf_sample, samples);
26 * aperfmperf_snapshot_khz()
27 * On the current CPU, snapshot APERF, MPERF, and jiffies
28 * unless we already did it within 10ms
29 * calculate kHz, save snapshot
31 static void aperfmperf_snapshot_khz(void *dummy)
33 u64 aperf, aperf_delta;
34 u64 mperf, mperf_delta;
35 struct aperfmperf_sample *s = this_cpu_ptr(&samples);
37 /* Don't bother re-computing within 10 ms */
38 if (time_before(jiffies, s->jiffies + HZ/100))
41 rdmsrl(MSR_IA32_APERF, aperf);
42 rdmsrl(MSR_IA32_MPERF, mperf);
44 aperf_delta = aperf - s->aperf;
45 mperf_delta = mperf - s->mperf;
48 * There is no architectural guarantee that MPERF
49 * increments faster than we can read it.
55 * if (cpu_khz * aperf_delta) fits into ULLONG_MAX, then
56 * khz = (cpu_khz * aperf_delta) / mperf_delta
58 if (div64_u64(ULLONG_MAX, cpu_khz) > aperf_delta)
59 s->khz = div64_u64((cpu_khz * aperf_delta), mperf_delta);
60 else /* khz = aperf_delta / (mperf_delta / cpu_khz) */
61 s->khz = div64_u64(aperf_delta,
62 div64_u64(mperf_delta, cpu_khz));
68 unsigned int arch_freq_get_on_cpu(int cpu)
73 if (!static_cpu_has(X86_FEATURE_APERFMPERF))
76 smp_call_function_single(cpu, aperfmperf_snapshot_khz, NULL, 1);
78 return per_cpu(samples.khz, cpu);