Merge tag 'for-linus-4.13-v2' of git://github.com/cminyard/linux-ipmi
[sfrench/cifs-2.6.git] / arch / x86 / kernel / cpu / aperfmperf.c
1 /*
2  * x86 APERF/MPERF KHz calculation for
3  * /sys/.../cpufreq/scaling_cur_freq
4  *
5  * Copyright (C) 2017 Intel Corp.
6  * Author: Len Brown <len.brown@intel.com>
7  *
8  * This file is licensed under GPLv2.
9  */
10
11 #include <linux/jiffies.h>
12 #include <linux/math64.h>
13 #include <linux/percpu.h>
14 #include <linux/smp.h>
15
16 struct aperfmperf_sample {
17         unsigned int    khz;
18         unsigned long   jiffies;
19         u64     aperf;
20         u64     mperf;
21 };
22
23 static DEFINE_PER_CPU(struct aperfmperf_sample, samples);
24
25 /*
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
30  */
31 static void aperfmperf_snapshot_khz(void *dummy)
32 {
33         u64 aperf, aperf_delta;
34         u64 mperf, mperf_delta;
35         struct aperfmperf_sample *s = this_cpu_ptr(&samples);
36
37         /* Don't bother re-computing within 10 ms */
38         if (time_before(jiffies, s->jiffies + HZ/100))
39                 return;
40
41         rdmsrl(MSR_IA32_APERF, aperf);
42         rdmsrl(MSR_IA32_MPERF, mperf);
43
44         aperf_delta = aperf - s->aperf;
45         mperf_delta = mperf - s->mperf;
46
47         /*
48          * There is no architectural guarantee that MPERF
49          * increments faster than we can read it.
50          */
51         if (mperf_delta == 0)
52                 return;
53
54         /*
55          * if (cpu_khz * aperf_delta) fits into ULLONG_MAX, then
56          *      khz = (cpu_khz * aperf_delta) / mperf_delta
57          */
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));
63         s->jiffies = jiffies;
64         s->aperf = aperf;
65         s->mperf = mperf;
66 }
67
68 unsigned int arch_freq_get_on_cpu(int cpu)
69 {
70         if (!cpu_khz)
71                 return 0;
72
73         if (!static_cpu_has(X86_FEATURE_APERFMPERF))
74                 return 0;
75
76         smp_call_function_single(cpu, aperfmperf_snapshot_khz, NULL, 1);
77
78         return per_cpu(samples.khz, cpu);
79 }