Re: [RFC PATCH 1/5] x86: tsc: add tsc to art helpers
From: Thomas Gleixner
Date: Thu Oct 17 2019 - 07:15:27 EST
On Tue, 1 Oct 2019, Felipe Balbi wrote:
> (sorry for the long delay, got caught up in other tasks)
Delayed by vacation :)
> Thomas Gleixner <tglx@xxxxxxxxxxxxx> writes:
> > On Thu, 15 Aug 2019, Felipe Balbi wrote:
> >> Thomas Gleixner <tglx@xxxxxxxxxxxxx> writes:
> >> > On Tue, 16 Jul 2019, Felipe Balbi wrote:
> >> >
> >> > So some information what those interfaces are used for and why they are
> >> > needed would be really helpful.
> >> Okay, I have some more details about this. The TGPIO device itself uses
> >> ART since TSC is not directly available to anything other than the
> >> CPU. The 'problem' here is that reading ART incurs extra latency which
> >> we would like to avoid. Therefore, we use TSC and scale it to
> >> nanoseconds which, would be the same as ART to ns.
> > Fine. But that's not really correct:
> > TSC = art_to_tsc_offset + ART * scale;
> From silicon folks I got the equation:
> ART = ECX * EBX / EAX;
What is the content of ECX/EBX/EAX and where is it coming from?
> If I'm reading this correctly, that's basically what
> native_calibrate_tsc() does (together with some error checking the safe
> defaults). Couldn't we, instead, just have a single function like below?
> u64 convert_tsc_to_art_ns()
> return x86_platform.calibrate_tsc();
Huch? How is that supposed to work? calibrate_tsc() returns the TSC
> Another way would be extract the important parts from
> native_calibrate_tsc() into a separate helper. This would safe another
> call to cpuid(0x15,...);
The relation between TSC and ART is already established via detect_art()
which reads all relevant data out of CPUID(ART_CPUID_LEAF).
We use exactly that information for convert_art_to_tsc() so the obvious
solution for calculating ART from TSC is to do the reverse operation.
rem = do_div(art, art_to_tsc_denominator);
res = art * art_to_tsc_numerator;
tmp = rem * art_to_tsc_numerator;
res += tmp + art_to_tsc_offset;
which is translated into math:
TSC = ART * SCALE + OFFSET
SCALE = N / D
N = CPUID(ART_CPUID_LEAF).EAX
D = CPUID(ART_CPUID_LEAF).EBX
So the obvious reverse operation is:
ART = (TSC - OFFSET) / SCALE;
Translating that into code should not be rocket science.