cpufreq_dt: Find the closest frequency

When building the frequencies table we convert the value in the DTS to
megahertz and loose precision. While it's not a problem for most of the
DTS it is when the expected frequency value is strict down to the hertz.
So it's either we don't truncate the value and have some ugly and long
values in the sysctls or we just find the closest frequency.
Do the latter.

Reviewed by:	mmel
Differential Revision:	https://reviews.freebsd.org/D41762
Sponsored by:	Beckhoff Automation GmbH & Co. KG
This commit is contained in:
Emmanuel Vadot 2023-09-06 18:40:17 +02:00
parent 229c65a83f
commit 17c17872ca

View file

@ -104,17 +104,26 @@ static const struct cpufreq_dt_opp *
cpufreq_dt_find_opp(device_t dev, uint64_t freq)
{
struct cpufreq_dt_softc *sc;
ssize_t n;
uint64_t diff, best_diff;
ssize_t n, best_n;
sc = device_get_softc(dev);
diff = 0;
best_diff = ~0;
DPRINTF(dev, "Looking for freq %ju\n", freq);
for (n = 0; n < sc->nopp; n++)
if (CPUFREQ_CMP(sc->opp[n].freq, freq))
return (&sc->opp[n]);
for (n = 0; n < sc->nopp; n++) {
diff = abs64((int64_t)sc->opp[n].freq - (int64_t)freq);
DPRINTF(dev, "Testing %ju, diff is %ju\n", sc->opp[n].freq, diff);
if (diff < best_diff) {
best_diff = diff;
best_n = n;
DPRINTF(dev, "%ju is best for now\n", sc->opp[n].freq);
}
}
DPRINTF(dev, "Couldn't find one\n");
return (NULL);
DPRINTF(dev, "Will use %ju\n", sc->opp[best_n].freq);
return (&sc->opp[best_n]);
}
static void