On Fri, Mar 06, 2020 at 04:44:14PM +0100, Gerd Hoffmann wrote: > Factor out TimerKHz and ShiftTSC calculation to tsctimer_configure(). > > Signed-off-by: Gerd Hoffmann <kra...@redhat.com> > --- > src/hw/timer.c | 21 +++++++++++++-------- > 1 file changed, 13 insertions(+), 8 deletions(-) > > diff --git a/src/hw/timer.c b/src/hw/timer.c > index bdcb3bfca211..ce3d63cd75f6 100644 > --- a/src/hw/timer.c > +++ b/src/hw/timer.c > @@ -58,6 +58,18 @@ u8 ShiftTSC VARFSEG; > * Internal timer setup > ****************************************************************/ > > +static void > +tsctimer_configure(u64 t) > +{ > + while (t >= (1<<24)) { > + ShiftTSC++; > + t = (t + 1) >> 1; > + } > + TimerKHz = DIV_ROUND_UP((u32)t, 1000 * PMTIMER_TO_PIT); > + TimerPort = 0; > + dprintf(1, "CPU Mhz=%u\n", (TimerKHz << ShiftTSC) / 1000); > +} > + > #define CALIBRATE_COUNT 0x800 // Approx 1.7ms > > // Calibrate the CPU time-stamp-counter > @@ -87,14 +99,7 @@ tsctimer_setup(void) > dprintf(6, "tsc calibrate start=%u end=%u diff=%u\n" > , (u32)start, (u32)end, (u32)diff); > u64 t = DIV_ROUND_UP(diff * PMTIMER_HZ, CALIBRATE_COUNT); > - while (t >= (1<<24)) { > - ShiftTSC++; > - t = (t + 1) >> 1; > - } > - TimerKHz = DIV_ROUND_UP((u32)t, 1000 * PMTIMER_TO_PIT); > - TimerPort = 0; > - > - dprintf(1, "CPU Mhz=%u\n", (TimerKHz << ShiftTSC) / 1000); > + tsctimer_configure(t); > }
The PMTIMER_TO_PIT in DIV_ROUND_UP((u32)t, 1000 * PMTIMER_TO_PIT) is directly tied to the measurement interval performed in tsctimer_setup(). Specifically, the code calculates the number of tsc ticks in n PIT intervals and then calculates "tsc_hz=pit_hz*elapsed_tsc/n". The complicated math is to avoid 64bit divides. I recommend against moving that low-level math to another function, as I fear the relationship would then be harder to understand. -Kevin _______________________________________________ SeaBIOS mailing list -- seabios@seabios.org To unsubscribe send an email to seabios-le...@seabios.org