aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuel Vadot <manu@FreeBSD.org>2026-11-17 20:42:52 +0000
committerEmmanuel Vadot <manu@FreeBSD.org>2026-11-29 15:08:03 +0000
commit7178a47a4ffd2c501e8f2a1432f8e8ff93b9c3a1 (patch)
treeb344a7a5c5c68a9673925e08ffe5a12ace5f7edd
parent8e918cfd6110a9a23c032f12c20eee987ee5ad19 (diff)
cpufreq_dt: Honor opp-supported-hw and opp-microvolt-<name>
Use the hardware version given by the SoC specific code to select the OPPs and their voltages, as described by the OPP binding. An OPP with opp-supported-hw is only used if one of its sub-groups matches the hardware version, which means that each level of the sub-group has a bit in common with the version. The voltages are read from opp-microvolt-<name> if the SoC specific code gives a name and the OPP has this property, from opp-microvolt otherwise. If no hardware version is known the OPPs with opp-supported-hw are not used as it is not safe to use an OPP that the hardware might not support. Setting the hw.cpufreq_dt.ignore_supported_hw loader tunable to 1 uses them all, as it was done before. Reviewed by: mmel Differential Revision: https://reviews.freebsd.org/D61699
-rw-r--r--sys/dev/cpufreq/cpufreq_dt.c93
1 files changed, 85 insertions, 8 deletions
diff --git a/sys/dev/cpufreq/cpufreq_dt.c b/sys/dev/cpufreq/cpufreq_dt.c
index 7bcedde27408..21c56eef3264 100644
--- a/sys/dev/cpufreq/cpufreq_dt.c
+++ b/sys/dev/cpufreq/cpufreq_dt.c
@@ -37,6 +37,7 @@
#include <sys/cpu.h>
#include <sys/cpuset.h>
#include <sys/smp.h>
+#include <sys/sysctl.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
@@ -78,6 +79,14 @@ struct cpufreq_dt_opp {
*/
#define CPUFREQ_DT_NS_TO_US(ns) (((uint64_t)(ns) + 999) / 1000)
+static SYSCTL_NODE(_hw, OID_AUTO, cpufreq_dt, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
+ "Generic DT cpufreq driver");
+
+static int cpufreq_dt_ignore_supported_hw = 0;
+SYSCTL_INT(_hw_cpufreq_dt, OID_AUTO, ignore_supported_hw, CTLFLAG_RDTUN,
+ &cpufreq_dt_ignore_supported_hw, 0,
+ "Use the OPPs even if opp-supported-hw does not match the hardware");
+
SET_DECLARE(cpufreq_dt_compat_set, const struct ofw_compat_data);
SET_DECLARE(cpufreq_dt_match_set, struct kobj_class);
@@ -419,16 +428,68 @@ cpufreq_dt_oppv1_parse(struct cpufreq_dt_softc *sc, phandle_t node)
return (0);
}
+/*
+ * An OPP with opp-supported-hw is only usable by the hardware versions it
+ * lists. The property is a list of sub-groups, each one having a bitmask for
+ * each level of version, and it matches if all the levels of a sub-group have
+ * a bit in common with the version of the hardware.
+ */
+static bool
+cpufreq_dt_opp_supported(struct cpufreq_dt_softc *sc, phandle_t opp)
+{
+ uint32_t *versions;
+ bool supported;
+ int ncells, nlevels, i, level;
+
+ if (!OF_hasprop(opp, "opp-supported-hw") ||
+ cpufreq_dt_ignore_supported_hw)
+ return (true);
+
+ /* The OPP is restricted to some versions and we do not know ours. */
+ nlevels = sc->hw.nlevels;
+ if (nlevels == 0)
+ return (false);
+
+ ncells = OF_getencprop_alloc_multi(opp, "opp-supported-hw",
+ sizeof(*versions), (void **)&versions);
+ if (ncells <= 0 || ncells % nlevels != 0) {
+ OF_prop_free(versions);
+ return (false);
+ }
+
+ supported = false;
+ for (i = 0; i < ncells / nlevels && !supported; i++) {
+ supported = true;
+ for (level = 0; level < nlevels; level++)
+ if ((versions[i * nlevels + level] &
+ sc->hw.levels[level]) == 0)
+ supported = false;
+ }
+ OF_prop_free(versions);
+
+ return (supported);
+}
+
static int
cpufreq_dt_oppv2_parse_uvolt(struct cpufreq_dt_softc *sc, phandle_t node,
struct cpufreq_dt_opp *opp)
{
uint32_t *volts;
+ const char *prop;
+ char name[40];
int nvolt;
+ /* Use opp-microvolt-<name> if there is one, opp-microvolt otherwise */
+ prop = "opp-microvolt";
+ if (sc->hw.name[0] != '\0') {
+ snprintf(name, sizeof(name), "opp-microvolt-%s", sc->hw.name);
+ if (OF_hasprop(node, name))
+ prop = name;
+ }
+
if (CPUFREQ_DT_HAVE_REGULATOR(sc)) {
- nvolt = OF_getencprop_alloc_multi(node, "opp-microvolt",
- sizeof(*volts), (void **)&volts);
+ nvolt = OF_getencprop_alloc_multi(node, prop, sizeof(*volts),
+ (void **)&volts);
if (nvolt == 1) {
opp->uvolt_target = volts[0];
opp->uvolt_min = volts[0];
@@ -439,7 +500,7 @@ cpufreq_dt_oppv2_parse_uvolt(struct cpufreq_dt_softc *sc, phandle_t node,
opp->uvolt_max = volts[2];
} else {
device_printf(sc->dev,
- "Wrong count of opp-microvolt property\n");
+ "Wrong count of %s property\n", prop);
OF_prop_free(volts);
return (ENXIO);
}
@@ -461,7 +522,7 @@ cpufreq_dt_oppv2_parse(struct cpufreq_dt_softc *sc, phandle_t node)
kobj_t vendor;
pcell_t cell[2];
uint32_t lat;
- int error, i;
+ int error, i, ndropped;
/*
* operating-points-v2 does not require the voltage entries
@@ -495,14 +556,29 @@ cpufreq_dt_oppv2_parse(struct cpufreq_dt_softc *sc, phandle_t node)
}
/* opp-hz is a required property, do not count the nodes without it */
- for (opp = OF_child(opp_table); opp > 0; opp = OF_peer(opp))
- if (OF_hasprop(opp, "opp-hz"))
- sc->nopp += 1;
+ ndropped = 0;
+ for (opp = OF_child(opp_table); opp > 0; opp = OF_peer(opp)) {
+ if (!OF_hasprop(opp, "opp-hz"))
+ continue;
+ if (!cpufreq_dt_opp_supported(sc, opp)) {
+ ndropped++;
+ continue;
+ }
+ sc->nopp += 1;
+ }
+ if (ndropped > 0 && bootverbose)
+ device_printf(sc->dev, "%d OPPs not supported by the hardware "
+ "(see hw.cpufreq_dt.ignore_supported_hw)\n", ndropped);
sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
for (i = 0, opp_table = OF_child(opp_table); opp_table > 0;
- opp_table = OF_peer(opp_table), i++) {
+ opp_table = OF_peer(opp_table)) {
+ /* Skip the nodes that were not counted */
+ if (!OF_hasprop(opp_table, "opp-hz") ||
+ !cpufreq_dt_opp_supported(sc, opp_table))
+ continue;
+
/* opp-hz is a required property */
if (OF_getencprop(opp_table, "opp-hz", cell,
sizeof(cell)) == -1)
@@ -535,6 +611,7 @@ cpufreq_dt_oppv2_parse(struct cpufreq_dt_softc *sc, phandle_t node)
sc->opp[i].freq / 1000000,
sc->opp[i].freq % 1000000,
sc->opp[i].uvolt_target);
+ i++;
}
return (0);
}