Page MenuHomeFreeBSD

D55592.1796378033.diff
No OneTemporary

Authored By
Unknown
Size
15 KB
Referenced Files
None
Subscribers
None

D55592.1796378033.diff

diff --git a/share/man/man4/Makefile b/share/man/man4/Makefile
--- a/share/man/man4/Makefile
+++ b/share/man/man4/Makefile
@@ -19,6 +19,7 @@
${_acpi_asus_wmi.4} \
${_acpi_dock.4} \
acpi_einj.4 \
+ acpi_fan.4 \
${_acpi_fujitsu.4} \
${_acpi_hp.4} \
${_acpi_ibm.4} \
diff --git a/share/man/man4/acpi_fan.4 b/share/man/man4/acpi_fan.4
new file mode 100644
--- /dev/null
+++ b/share/man/man4/acpi_fan.4
@@ -0,0 +1,200 @@
+.\"
+.\" Copyright (c) 2026 The FreeBSD Foundation
+.\"
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\"
+.Dd November 26, 2026
+.Dt ACPI_FAN 4
+.Os
+.Sh NAME
+.Nm acpi_fan
+.Nd ACPI fan device driver
+.Sh SYNOPSIS
+.Cd device acpi
+.Pp
+In
+.Xr loader.conf 5 :
+.Cd debug.acpi.disabled
+.Cd dev.fan.%d.set_level
+.Pp
+In
+.Xr sysctl.conf 5 :
+.Cd dev.fan.%d.fine_grained
+.Cd dev.fan.%d.set_level
+.Cd dev.fan.%d.status
+.Cd dev.fan.%d.steps
+.Cd dev.fan.%d.levels
+.Sh DESCRIPTION
+The
+.Nm
+driver attaches to ACPI fan devices, that is, devices with a hardware ID
+of
+.Li PNP0C0B
+or
+.Li INT3404 ,
+and exports their speed and state through
+.Xr sysctl 8 .
+.Pp
+Only fans that implement the ACPI 4.0 fan control interface are supported.
+The driver requires the
+.Li _FIF
+control method to be present and does not attach without it, so fans that
+are driven purely by the platform through a thermal zone's active cooling
+list are not claimed.
+.Pp
+Fans are numbered sequentially in the order they appear in the AML, and each
+one gets its own
+.Va dev.fan.%d
+sysctl tree.
+.Pp
+A fan supports one of two control models, as reported by the
+.Li FineGrainControl
+field of
+.Li _FIF :
+.Bl -bullet
+.It
+With fine grained control, any level from 0 to 100 may be requested, and the
+firmware interprets it as a percentage of maximum speed.
+.It
+Without fine grained control, only the discrete levels the firmware enumerates
+in its
+.Li _FPS
+performance state table may be requested.
+.El
+.Pp
+The
+.Va fine_grained
+sysctl reports which model is in use, and the set of sysctls the driver
+creates differs accordingly.
+.Sh HARDWARE
+The
+.Nm
+driver supports fans whose speed is controlled by the system firmware, as
+found in many laptops, mini PCs, and other systems where the fan is not
+attached to a motherboard fan header, including fans managed by the
+Intel Dynamic Platform and Thermal Framework (DPTF).
+.Sh SYSCTL VARIABLES
+.Bl -tag -width indent
+.It Va dev.fan.%d.fine_grained
+Whether the fan supports fine grained control.
+Read-only.
+.It Va dev.fan.%d.set_level
+The level last requested through this sysctl.
+Writing to it calls the firmware's
+.Li _FSL
+method to change the fan's speed.
+.Pp
+For a fine grained fan the value is a percentage of maximum speed, from 0 to
+100; values outside that range are rejected with
+.Er EINVAL .
+The value is rounded down to a multiple of
+.Va steps
+before being passed to the firmware.
+.Pp
+Otherwise the value must be one of the control values listed by
+.Va levels ,
+and any other value is rejected with
+.Er EINVAL .
+.It Va dev.fan.%d.status
+The fan's current control level and speed in revolutions per minute, read
+from the firmware's
+.Li _FST
+method at the time the sysctl is read.
+Read-only.
+.Pp
+A speed of 4294967295
+.Pq Li 0xffffffff
+means the platform does not report the fan's speed.
+.It Va dev.fan.%d.steps
+The step size recommended by the firmware for
+.Va set_level ,
+in percent.
+Present only when
+.Va fine_grained
+is 1.
+Read-only.
+.It Va dev.fan.%d.levels
+The fan's performance states, one per line, as enumerated by the firmware's
+.Li _FPS
+method.
+Present only when
+.Va fine_grained
+is 0.
+Read-only.
+.Pp
+Each line reports the control value to write to
+.Va set_level ,
+the active cooling trip point associated with the state, the expected speed
+in revolutions per minute, the expected noise level in centibels, and the
+expected power draw in milliwatts.
+Any of these fields may be 4294967295
+.Pq Li 0xffffffff ,
+which means the firmware does not provide that value.
+.El
+.Sh LOADER TUNABLES
+.Bl -tag -width indent
+.It Va debug.acpi.disabled
+Adding
+.Dq Li fan
+to this list prevents
+.Nm
+from attaching to any device.
+See
+.Xr acpi 4 .
+.It Va dev.fan.%d.set_level
+See description in
+.Sx SYSCTL VARIABLES .
+.El
+.Sh EXAMPLES
+List the fans present on the system:
+.Bd -literal -offset indent
+% sysctl -N dev.fan | cut -d. -f1-3 | sort -u
+dev.fan.0
+.Ed
+.Pp
+Show a fan's control model and current state:
+.Bd -literal -offset indent
+% sysctl dev.fan.0.fine_grained dev.fan.0.status
+dev.fan.0.fine_grained: 0
+dev.fan.0.status: Control Level: 50, Speed: 2400 RPM
+.Ed
+.Pp
+Since this fan does not support fine grained control, ask the firmware which
+levels it accepts, then select one:
+.Bd -literal -offset indent
+% sysctl dev.fan.0.levels
+dev.fan.0.levels: Ctrl: 0, TripPoint: 4294967295, Speed: 0, NoiseLevel: 0, Power: 0
+Ctrl: 50, TripPoint: 4294967295, Speed: 2400, NoiseLevel: 300, Power: 1500
+Ctrl: 100, TripPoint: 4294967295, Speed: 4800, NoiseLevel: 500, Power: 3000
+# sysctl dev.fan.0.set_level=100
+dev.fan.0.set_level: 50 -> 100
+.Ed
+.Sh SEE ALSO
+.Xr acpi 4 ,
+.Xr acpi_thermal 4 ,
+.Xr loader.conf 5 ,
+.Xr acpidump 8 ,
+.Xr sysctl 8
+.Sh HISTORY
+The
+.Nm
+driver first appeared in
+.Fx 16.0 .
+.Sh AUTHORS
+.An ShengYi Hung Aq Mt aokblast@FreeBSD.org
+.Sh CAVEATS
+The driver does not have exclusive control of the fan.
+Firmware and the ACPI thermal zones described in
+.Xr acpi_thermal 4
+may also change the fan's speed on their own, so the value of
+.Va set_level
+is only the level this driver last requested, not necessarily the level the
+fan is running at.
+Read
+.Va status
+for that.
+.Pp
+Across a suspend and resume cycle the driver records the fan's level, stops
+the fan, and restores the recorded level on resume.
+Restoring is best effort: a firmware that rejects the request is not treated
+as a resume failure.
diff --git a/sys/conf/files b/sys/conf/files
--- a/sys/conf/files
+++ b/sys/conf/files
@@ -769,6 +769,7 @@
dev/acpica/acpi_cmbat.c optional acpi
dev/acpica/acpi_cpu.c optional acpi
dev/acpica/acpi_ec.c optional acpi
+dev/acpica/acpi_fan.c optional acpi
dev/acpica/acpi_ged.c optional acpi_ged acpi
dev/acpica/acpi_isab.c optional acpi isa
dev/acpica/acpi_lid.c optional acpi
diff --git a/sys/dev/acpica/acpi_fan.c b/sys/dev/acpica/acpi_fan.c
new file mode 100644
--- /dev/null
+++ b/sys/dev/acpica/acpi_fan.c
@@ -0,0 +1,391 @@
+/*
+ * Copyright (c) 2026 The FreeBSD Foundation
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <sys/cdefs.h>
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+#include <sys/mutex.h>
+#include <sys/module.h>
+#include <sys/sbuf.h>
+
+#include <contrib/dev/acpica/include/acpi.h>
+
+#include <dev/acpica/acpivar.h>
+
+#define ACPI_FAN_INVALID_VALUE 0xffffffff
+
+struct acpi_fan_package {
+ uint32_t control;
+ uint32_t trip_point;
+ uint32_t speed;
+ uint32_t noise_level;
+ uint32_t power;
+};
+
+struct acpi_fan_param {
+ struct acpi_fan_package *pkgs;
+ int cnts;
+};
+
+struct acpi_fan_softc {
+ device_t dev;
+ int (*set_level)(struct acpi_fan_softc *sc, uint32_t val);
+ uint32_t cached_level;
+ bool fine_grained;
+ uint32_t step;
+ struct acpi_fan_param pkg;
+ struct mtx mtx;
+};
+
+static int
+acpi_fan_set(struct acpi_fan_softc *sc, uint32_t val)
+{
+ ACPI_OBJECT_LIST arglist;
+ ACPI_OBJECT arg;
+
+ arglist.Count = 1;
+ arglist.Pointer = &arg;
+ arg.Type = ACPI_TYPE_INTEGER;
+ arg.Integer.Value = val;
+ if (ACPI_FAILURE(AcpiEvaluateObject(acpi_get_handle(sc->dev), "_FSL",
+ &arglist, NULL)))
+ return (ENXIO);
+ return (0);
+}
+
+static int
+acpi_fan_set_fine_grained(struct acpi_fan_softc *sc, uint32_t val)
+{
+ if (val > 100)
+ return (EINVAL);
+ if (sc->step != 0)
+ val = rounddown(val, sc->step);
+ return (acpi_fan_set(sc, val));
+}
+
+static int
+acpi_fan_set_level(struct acpi_fan_softc *sc, uint32_t val)
+{
+ struct acpi_fan_package *pkg;
+ int i, n;
+
+ pkg = sc->pkg.pkgs;
+ for (i = 0, n = sc->pkg.cnts; i < n; i++) {
+ if (pkg[i].control == val)
+ break;
+ }
+ if (i == sc->pkg.cnts)
+ return (EINVAL);
+ return (acpi_fan_set(sc, val));
+}
+
+static int
+acpi_fan_get_status(const device_t dev, uint32_t *ctrl, uint32_t *speed)
+{
+ ACPI_HANDLE handle;
+ ACPI_BUFFER buffer;
+ ACPI_OBJECT *obj;
+ int err = 0;
+
+ handle = acpi_get_handle(dev);
+ buffer.Length = ACPI_ALLOCATE_BUFFER;
+ buffer.Pointer = NULL;
+ if (ACPI_FAILURE(AcpiEvaluateObject(handle, "_FST", NULL, &buffer)))
+ return (ENXIO);
+ obj = buffer.Pointer;
+ if (!ACPI_PKG_VALID(obj, 3)) {
+ err = EINVAL;
+ goto end;
+ }
+ if (ctrl && (err = acpi_PkgInt32(obj, 1, ctrl)) != 0)
+ goto end;
+ if (speed)
+ err = acpi_PkgInt32(obj, 2, speed);
+end:
+ AcpiOsFree(buffer.Pointer);
+ return (err);
+}
+
+static int
+sysctl_show_fan_status(SYSCTL_HANDLER_ARGS)
+{
+ const device_t dev = arg1;
+ struct sbuf sbs, *sb;
+ uint32_t control, speed;
+ int err;
+
+ if ((err = acpi_fan_get_status(dev, &control, &speed)) != 0)
+ return (err);
+ sb = sbuf_new_for_sysctl(&sbs, NULL, 0, req);
+ sbuf_printf(sb, "Control Level: %u, Speed: %u RPM", control, speed);
+ err = sbuf_finish(sb);
+ sbuf_delete(sb);
+ return (err);
+}
+
+static int
+sysctl_list_fan_levels(SYSCTL_HANDLER_ARGS)
+{
+ const device_t dev = arg1;
+ struct acpi_fan_softc *sc = device_get_softc(dev);
+ struct acpi_fan_package *pkg;
+ struct sbuf sbs, *sb;
+ int err, i, n;
+
+ sb = sbuf_new_for_sysctl(&sbs, NULL, 0, req);
+ for (i = 0, n = sc->pkg.cnts; i < n; i++) {
+ pkg = &sc->pkg.pkgs[i];
+ sbuf_printf(sb,
+ "Ctrl: %u, TripPoint: %u, Speed: %u, NoiseLevel: %u, Power: %u\n",
+ pkg->control, pkg->trip_point, pkg->speed, pkg->noise_level,
+ pkg->power);
+ }
+ err = sbuf_finish(sb);
+ sbuf_delete(sb);
+ return (err);
+}
+
+static int
+sysctl_set_fan_level(SYSCTL_HANDLER_ARGS)
+{
+ const device_t dev = arg1;
+ struct acpi_fan_softc *sc = device_get_softc(dev);
+ int err;
+ uint32_t val;
+
+ mtx_lock(&sc->mtx);
+ val = sc->cached_level;
+ mtx_unlock(&sc->mtx);
+ err = sysctl_handle_int(oidp, &val, 0, req);
+ if (err)
+ return (err);
+ mtx_lock(&sc->mtx);
+ if (val == sc->cached_level) {
+ mtx_unlock(&sc->mtx);
+ return (0);
+ }
+ mtx_unlock(&sc->mtx);
+ err = sc->set_level(sc, val);
+ if (err)
+ return (err);
+ mtx_lock(&sc->mtx);
+ sc->cached_level = val;
+ mtx_unlock(&sc->mtx);
+ return (0);
+}
+
+static int
+acpi_fan_probe(device_t dev)
+{
+ static char *fan_ids[] = { "PNP0C0B", "INT3404", NULL };
+ ACPI_STATUS status;
+ int res;
+
+ if (acpi_disabled("fan"))
+ return (ENXIO);
+ res = ACPI_ID_PROBE(device_get_parent(dev), dev, fan_ids, NULL);
+ if (res > 0)
+ return (ENXIO);
+ status = AcpiEvaluateObject(acpi_get_handle(dev), "_FIF", NULL, NULL);
+ if (ACPI_FAILURE(status))
+ return (ENXIO);
+ device_set_desc(dev, "ACPI Fan");
+ return (res);
+}
+
+static int
+acpi_fan_parse_fps(ACPI_HANDLE handle, struct acpi_fan_softc *sc)
+{
+ ACPI_BUFFER buf;
+ ACPI_OBJECT *pkg_obj, *obj;
+ int idx, cnt, err = 0;
+
+ buf.Pointer = NULL;
+ buf.Length = ACPI_ALLOCATE_BUFFER;
+ if (ACPI_FAILURE(AcpiEvaluateObject(handle, "_FPS", NULL, &buf)))
+ return (ENXIO);
+ /* The first element of the package is the revision. */
+ pkg_obj = buf.Pointer;
+ if (!ACPI_PKG_VALID(pkg_obj, 1)) {
+ err = EINVAL;
+ goto end;
+ }
+ cnt = pkg_obj->Package.Count - 1;
+ if (cnt == 0)
+ goto end;
+ sc->pkg.pkgs = malloc(cnt * sizeof(struct acpi_fan_package), M_DEVBUF,
+ M_WAITOK | M_ZERO);
+ for (idx = 0; idx < cnt; idx++) {
+ /* Skip over the revision. */
+ obj = &pkg_obj->Package.Elements[idx + 1];
+ if (!ACPI_PKG_VALID(obj, 5)) {
+ err = EINVAL;
+ goto fail;
+ }
+ if ((err = acpi_PkgInt32(obj, 0,
+ &sc->pkg.pkgs[idx].control)) != 0 ||
+ (err = acpi_PkgInt32(obj, 1,
+ &sc->pkg.pkgs[idx].trip_point)) != 0 ||
+ (err = acpi_PkgInt32(obj, 2,
+ &sc->pkg.pkgs[idx].speed)) != 0 ||
+ (err = acpi_PkgInt32(obj, 3,
+ &sc->pkg.pkgs[idx].noise_level)) != 0 ||
+ (err = acpi_PkgInt32(obj, 4,
+ &sc->pkg.pkgs[idx].power)) != 0)
+ goto fail;
+ }
+ sc->pkg.cnts = cnt;
+end:
+ AcpiOsFree(buf.Pointer);
+ return (err);
+fail:
+ free(sc->pkg.pkgs, M_DEVBUF);
+ sc->pkg.pkgs = NULL;
+ goto end;
+}
+
+static int
+acpi_fan_attach(device_t dev)
+{
+ ACPI_HANDLE handle;
+ ACPI_BUFFER buf;
+ ACPI_OBJECT *obj;
+ struct acpi_fan_softc *sc;
+ uint32_t val;
+ int err;
+
+ handle = acpi_get_handle(dev);
+ sc = device_get_softc(dev);
+ sc->dev = dev;
+ mtx_init(&sc->mtx, "acpi_fan", NULL, MTX_DEF);
+ buf.Pointer = NULL;
+ buf.Length = ACPI_ALLOCATE_BUFFER;
+ if (ACPI_FAILURE(AcpiEvaluateObject(handle, "_FIF", NULL, &buf))) {
+ err = ENXIO;
+ goto fail;
+ }
+ obj = buf.Pointer;
+
+ if (!ACPI_PKG_VALID(obj, 4)) {
+ err = ENXIO;
+ goto fail;
+ }
+ if ((err = acpi_PkgInt32(obj, 1, &val)) != 0)
+ goto fail;
+ sc->fine_grained = val != 0;
+ /* Fine grained control of the fan. Read the step. */
+ if (sc->fine_grained) {
+ if ((err = acpi_PkgInt32(obj, 2, &val)) != 0)
+ goto fail;
+ sc->step = val;
+ sc->set_level = &acpi_fan_set_fine_grained;
+ } else
+ sc->set_level = &acpi_fan_set_level;
+ AcpiOsFree(buf.Pointer);
+ buf.Pointer = NULL;
+
+ if (acpi_fan_parse_fps(handle, sc) != 0)
+ device_printf(dev, "Unable to parse _FPS object\n");
+ if (acpi_fan_get_status(dev, &sc->cached_level, NULL) != 0) {
+ device_printf(dev, "Failed to get _FST object\n");
+ sc->cached_level = ACPI_FAN_INVALID_VALUE;
+ }
+ if (sc->fine_grained) {
+ SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
+ SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
+ "steps", CTLFLAG_RD | CTLFLAG_MPSAFE, &sc->step, 0,
+ "Steps for fine grained control");
+ } else {
+ SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
+ SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
+ "levels", CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, dev,
+ 0, sysctl_list_fan_levels, "A",
+ "List all available speeds");
+ }
+ SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
+ SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "status",
+ CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, 0,
+ sysctl_show_fan_status, "A", "Show fan's status");
+ SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
+ SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "set_level",
+ CTLTYPE_UINT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, dev, 0,
+ sysctl_set_fan_level, "IU", "Set fan level");
+ SYSCTL_ADD_BOOL(device_get_sysctl_ctx(dev),
+ SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
+ "fine_grained", CTLFLAG_RD | CTLFLAG_MPSAFE, &sc->fine_grained, 0,
+ "Whether the fan supports fine grained control");
+ return (0);
+fail:
+ AcpiOsFree(buf.Pointer);
+ mtx_destroy(&sc->mtx);
+ return (err);
+}
+
+static int
+acpi_fan_detach(device_t dev)
+{
+ struct acpi_fan_softc *sc = device_get_softc(dev);
+
+ free(sc->pkg.pkgs, M_DEVBUF);
+ mtx_destroy(&sc->mtx);
+ return (0);
+}
+
+static int
+acpi_fan_suspend(device_t dev)
+{
+ struct acpi_fan_softc *sc = device_get_softc(dev);
+ uint32_t level;
+
+ if (acpi_fan_get_status(dev, &level, NULL) == 0) {
+ mtx_lock(&sc->mtx);
+ sc->cached_level = level;
+ mtx_unlock(&sc->mtx);
+ }
+ /*
+ * Stopping the fan is best effort: _FPS need not offer a control
+ * value of zero. Never fail the suspend over it.
+ */
+ (void)sc->set_level(sc, 0);
+ return (0);
+}
+
+static int
+acpi_fan_resume(device_t dev)
+{
+ struct acpi_fan_softc *sc = device_get_softc(dev);
+ uint32_t level;
+
+ mtx_lock(&sc->mtx);
+ level = sc->cached_level;
+ mtx_unlock(&sc->mtx);
+ if (level == ACPI_FAN_INVALID_VALUE)
+ return (0);
+ (void)sc->set_level(sc, level);
+ return (0);
+}
+
+static device_method_t acpi_fan_methods[] = {
+ /* device interface */
+ DEVMETHOD(device_probe, acpi_fan_probe),
+ DEVMETHOD(device_attach, acpi_fan_attach),
+ DEVMETHOD(device_detach, acpi_fan_detach),
+ DEVMETHOD(device_suspend, acpi_fan_suspend),
+ DEVMETHOD(device_resume, acpi_fan_resume),
+
+ DEVMETHOD_END
+};
+
+static driver_t acpi_fan_driver = {
+ "fan",
+ acpi_fan_methods,
+ sizeof(struct acpi_fan_softc),
+};
+
+DRIVER_MODULE(acpi_fan, acpi, acpi_fan_driver, 0, 0);
+MODULE_DEPEND(acpi_fan, acpi, 1, 1, 1);

File Metadata

Mime Type
text/plain
Expires
Fri, Dec 4, 9:53 AM (18 h, 45 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
42196035
Default Alt Text
D55592.1796378033.diff (15 KB)

Event Timeline