From 88a49bc9a294645fd3476a2a703ff4ee0d3d090f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Eug=C3=AAnio?= Date: Sun, 3 Nov 2024 04:17:56 -0300 Subject: [PATCH] amdgpu: add custom fan control --- hosts/monolith/amdgpu.nix | 9 ++++++- scripts/amd-fan-control | 50 +++++++++++++++++++++++++++++++++++++++ scripts/default.nix | 1 + 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100755 scripts/amd-fan-control diff --git a/hosts/monolith/amdgpu.nix b/hosts/monolith/amdgpu.nix index c02af7e..f3cefaa 100644 --- a/hosts/monolith/amdgpu.nix +++ b/hosts/monolith/amdgpu.nix @@ -1,4 +1,4 @@ -{ pkgs, ... }: +{ pkgs, lib, ... }: let undervoltGpu = pkgs.writeShellScript "undervolt-gpu" '' set -xe @@ -18,6 +18,13 @@ in "amdgpu.ppfeaturemask=0xfffd7fff" # enable undervolting ]; + systemd.services.amd-fan-control = { + script = '' + ${lib.getExe pkgs.amd-fan-control} /sys/class/drm/card1/device 60 85 + ''; + wantedBy = [ "multi-user.target" ]; + }; + hardware.opengl.driSupport = true; # # For 32 bit applications hardware.opengl.driSupport32Bit = true; diff --git a/scripts/amd-fan-control b/scripts/amd-fan-control new file mode 100755 index 0000000..641c5cf --- /dev/null +++ b/scripts/amd-fan-control @@ -0,0 +1,50 @@ +#!/usr/bin/env bash + +set -e + +DEVICE="$1" # eg: /sys/class/drm/card1/device +HWMON=$(echo "$DEVICE"/hwmon/hwmon*) + +exit() { + echo "Setting controll to auto" >&2 + echo 2 > "$HWMON/pwm1_enable" +} + +trap exit EXIT INT + +bail() { + echo "Error: $@" >&2 + echo "Exiting..." >&2 + exit 1 +} + +if ! [ -d $HWMON ]; then + bail "Invalid HWMON" +fi + +TEMP_INPUT="$HWMON/temp2_input" + +if ! [ -f $TEMP_INPUT ]; then + bail "Invalid TEMP_INPUT" +fi + +MIN="$2" +MAX="$3" + +echo "Running..." >&2 +while true; do + TEMPERATURE_RAW=$(cat "$TEMP_INPUT") + TEMPERATURE="$(( $TEMPERATURE_RAW / 1000 ))" + # Remap from a number between 60_000..90_000 to 0..255 + PWM=$(( ($TEMPERATURE - $MIN) * 255 / ($MAX - $MIN) )) + + if [ "$PWM" -gt 255 ]; then + PWM=255 + elif [ "$PWM" -lt 0 ]; then + PWM=0 + fi + + echo 1 > "$HWMON/pwm1_enable" + echo "$PWM" > "$HWMON/pwm1" + sleep .1s +done diff --git a/scripts/default.nix b/scripts/default.nix index 79fe4c6..c621d47 100644 --- a/scripts/default.nix +++ b/scripts/default.nix @@ -23,6 +23,7 @@ in with final; createScripts { + amd-fan-control = [ bash ]; br = [ ]; bmenu = [ bemenu