diff options
author | Emmanuel Vadot <manu@FreeBSD.org> | 2023-09-06 16:40:17 +0000 |
---|---|---|
committer | Emmanuel Vadot <manu@FreeBSD.org> | 2023-09-08 07:44:32 +0000 |
commit | 17c17872ca98df0e2b9f9c7a2c41ef73f7dee21a (patch) | |
tree | ce0db0c418ddcf6b2d59dc28e06cd642795fb7b0 | |
parent | 229c65a83fbe0ede8c617e35d8f8c14d5ebadc19 (diff) | |
download | src-17c17872ca98df0e2b9f9c7a2c41ef73f7dee21a.tar.gz src-17c17872ca98df0e2b9f9c7a2c41ef73f7dee21a.zip |
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
-rw-r--r-- | sys/dev/cpufreq/cpufreq_dt.c | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/sys/dev/cpufreq/cpufreq_dt.c b/sys/dev/cpufreq/cpufreq_dt.c index aaeada3a4e58..be434cabb4fd 100644 --- a/sys/dev/cpufreq/cpufreq_dt.c +++ b/sys/dev/cpufreq/cpufreq_dt.c @@ -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 |