#!/bin/bash # fanfix — toolkit for living with the dead GPU fan on this laptop # (PCSpecialist Valeon II 17 / TongFang GM7RGxM, Ryzen 9 6900HX + RTX 3070 Ti). # # Background: with a fan dead, the embedded controller (EC) pins every CPU core at 399 MHz the # instant the CPU reaches ~79 °C and holds it until ~47 °C. That policy lives in firmware and # cannot be switched off from Linux; the workaround is to cap CPU clocks so the CPU never gets # there (3.2 GHz ≈ base clock → ~70 °C under full load), and optionally to run the surviving # CPU fan harder than the EC's quiet-tuned curve. # # Fan control talks to the EC through the firmware's own ACPI methods (\_SB.INOU.ECRR/ECRW — # the same ones the kernel's uniwill-laptop driver uses) via the acpi_call module, and follows # the register procedure of TUXEDO's driver for this chassis family (uniwill_keyboard.h). # # Usage: fanfix help set -uo pipefail CAP_DEFAULT_KHZ=3200000 CONF=/etc/tmpfiles.d/99-cpu-freq-cap.conf OLD_CONF=/etc/tmpfiles.d/99-cpu-no-boost.conf TRIP_MHZ=600 # any core reporting below this under load = EC clamp active CPU0=/sys/devices/system/cpu/cpu0/cpufreq ACPI_CALL=/proc/acpi/call EC_DEV='\_SB.INOU' # ACPI device behind INOU0000 (Uniwill EC bridge) FAN_UNIT=fanfix-fan.service FAN_CONF=/etc/fanfix.conf # optional: CURVE="45:30 55:45 60:55 65:70 70:85 75:100" DEFAULT_CURVE="45:30 55:45 60:55 65:70 70:85 75:100" FAN_MIN_PCT=30 # refuse slower manual settings (EC trips at 79 °C; 34 % held 69 °C capped) SELF=$(readlink -f "$0") [ -r "$FAN_CONF" ] && . "$FAN_CONF" # ---------- generic helpers ---------------------------------------------------------------- hwmon_by_name() { local h; for h in /sys/class/hwmon/hwmon*; do [ "$(cat "$h/name" 2>/dev/null)" = "$1" ] && { echo "$h"; return 0; }; done; return 1; } rd() { cat "$1" 2>/dev/null || echo 0; } tctl() { local h; h=$(hwmon_by_name k10temp) && echo $(( $(rd "$h/temp1_input") / 1000 )) || echo 0; } ppt_w() { local h; h=$(hwmon_by_name amdgpu) && awk "BEGIN{printf \"%.1f\", $(rd "$h/power1_input")/1e6}" || echo "?"; } freqs() { grep MHz /proc/cpuinfo | awk '{s+=$4; if($4>m)m=$4; if(n==""||$4/dev/null; wait 2>/dev/null; } # ---------- EC access (root; needs acpi_call) ---------------------------------------------- ec_ready() { [ -w "$ACPI_CALL" ] || modprobe acpi_call 2>/dev/null; [ -w "$ACPI_CALL" ]; } ec_raw() { printf '%s' "$1" > "$ACPI_CALL" || return 1; local o; o=$(tr -d '\0\n\r ' < "$ACPI_CALL"); printf '%s' "$o"; } ec_read() { # ec_read 0x0741 -> decimal value local out; out=$(ec_raw "$(printf '%s.ECRR 0x%04x' "$EC_DEV" "$1")") || return 1 [[ "$out" =~ ^0x[0-9a-fA-F]+$ ]] || { echo "EC read $(printf '0x%04x' "$1") failed: $out" >&2; return 1; } echo $(( out )) } ec_write() { # ec_write 0x1804 200 local out; out=$(ec_raw "$(printf '%s.ECRW 0x%04x 0x%02x' "$EC_DEV" "$1" "$2")") || return 1 case "$out" in Error*|"not called"|"") echo "EC write $(printf '0x%04x' "$1") failed: ${out:-no result}" >&2; return 1;; esac sleep 0.005 } ec_set_bits() { local v; v=$(ec_read "$1") || return 1; ec_write "$1" $(( v | $2 )); } ec_clear_bits() { local v; v=$(ec_read "$1") || return 1; ec_write "$1" $(( v & ~$2 & 0xff )); } ec_bit() { local v; v=$(ec_read "$1") || return 1; echo $(( (v >> $2) & 1 )); } # EC register map (TongFang/Uniwill; from kernel uniwill-laptop + tuxedo-drivers + qc71_laptop) R_AP_OEM=0x0741 # bit0 manual-ctrl/OEM-app-present, bit5 FAN_ABNORMAL R_FAN_MODE=0x0751 # bit6 "full fan mode" (old direct control), bit4 turbo, bit5 high, bit7 user R_PWM1=0x075B; R_PWM2=0x075C # current duty 0..200 (read-only) R_FAN_CTRL=0x078E # bit6 = universal (custom-table) fan control supported R_TBL_SPLIT=0x07C5 # bit7 = separate CPU/GPU tables R_TBL_ENABLE=0x07C6 # bit2 = enable custom tables 0x0F00.. R_CPU_TBL_END=0x0F00; R_CPU_TBL_START=0x0F10; R_CPU_TBL_SPEED=0x0F20 R_GPU_TBL_END=0x0F30; R_GPU_TBL_START=0x0F40; R_GPU_TBL_SPEED=0x0F50 R_PWM1_W=0x1804; R_PWM2_W=0x1809 # writable duty 0..200 (old direct control) R_CPU_TEMP=0x043E; R_GPU_TEMP=0x044F R_FAN1_RPM=0x0464; R_FAN2_RPM=0x046C # 16-bit big-endian universal_ctrl() { ec_bit $R_FAN_CTRL 6; } # 1 = custom-table path available tables_enabled() { ec_bit $R_TBL_ENABLE 2; } pct_to_duty() { echo $(( $1 * 200 / 100 )); } duty_to_pct() { echo $(( $1 * 100 / 200 )); } fan_init_tables() { # TUXEDO uw_init_fan(): one controllable zone 0-115 °C + dummy fail-safe zones at 100 % local i ec_clear_bits $R_FAN_MODE 0x40 [ "$(ec_bit $R_TBL_SPLIT 7)" = 1 ] || ec_set_bits $R_TBL_SPLIT 0x80 ec_write $R_CPU_TBL_END 115; ec_write $R_CPU_TBL_START 0; ec_write $R_CPU_TBL_SPEED 1 ec_write $R_GPU_TBL_END 120; ec_write $R_GPU_TBL_START 0; ec_write $R_GPU_TBL_SPEED 1 for i in $(seq 1 15); do ec_write $(( R_CPU_TBL_END + i )) $(( 115 + i + 1 )); ec_write $(( R_CPU_TBL_START + i )) $(( 115 + i )); ec_write $(( R_CPU_TBL_SPEED + i )) 200 ec_write $(( R_GPU_TBL_END + i )) $(( 115 + i + 1 )); ec_write $(( R_GPU_TBL_START + i )) $(( 115 + i )); ec_write $(( R_GPU_TBL_SPEED + i )) 200 done [ "$(tables_enabled)" = 1 ] || ec_set_bits $R_TBL_ENABLE 0x04 } fan_apply_duty() { # fan_apply_duty <0..200> (both fans; GPU fan is dead anyway) local d=$1 if [ "$(universal_ctrl)" = 1 ]; then [ "$(tables_enabled)" = 1 ] && [ "$(ec_bit $R_FAN_MODE 6)" = 0 ] || fan_init_tables ec_write $R_CPU_TBL_SPEED "$d"; ec_write $R_GPU_TBL_SPEED "$d" ec_write $R_PWM1_W "$d"; ec_write $R_PWM2_W "$d" else # old path: "full fan mode" + direct duty; EC ramps to 100 % on entry, so write quickly & repeatedly local i; [ "$(ec_bit $R_FAN_MODE 6)" = 1 ] || ec_set_bits $R_FAN_MODE 0x40 for i in 1 2 3 4 5 6 7 8 9 10; do ec_write $R_PWM1_W "$d"; ec_write $R_PWM2_W "$d"; sleep 0.01; done fi } fan_set_auto() { if [ "$(universal_ctrl)" = 1 ]; then [ "$(tables_enabled)" = 1 ] && ec_clear_bits $R_TBL_ENABLE 0x04 [ "$(ec_bit $R_TBL_SPLIT 7)" = 1 ] && ec_clear_bits $R_TBL_SPLIT 0x80 fi [ "$(ec_bit $R_FAN_MODE 6)" = 1 ] && ec_clear_bits $R_FAN_MODE 0x40 return 0 } fan_mode_text() { # what is controlling the fans right now if systemctl is-active --quiet "$FAN_UNIT" 2>/dev/null; then echo "curve daemon (fanfix)"; return; fi if [ "$(ec_bit $R_FAN_MODE 6)" = 1 ]; then echo "manual (old full-fan mode)"; return; fi if [ "$(tables_enabled)" = 1 ]; then echo "manual (custom table, $(duty_to_pct "$(ec_read $R_CPU_TBL_SPEED)") %)"; return; fi echo "auto (EC curve)" } curve_pct_for_temp() { # linear interpolation over CURVE="t:p t:p ..." local t=$1 curve=${CURVE:-$DEFAULT_CURVE} pt pp lt=-1 lp=-1 for pair in $curve; do pt=${pair%%:*}; pp=${pair##*:} if [ "$t" -le "$pt" ]; then if [ "$lt" -lt 0 ]; then echo "$pp"; else echo $(( lp + (pp - lp) * (t - lt) / (pt - lt) )); fi; return fi; lt=$pt; lp=$pp done echo "$lp" } # ---------- commands ----------------------------------------------------------------------- cmd_status() { bold "fanfix status — $(date '+%H:%M:%S')" local cap=$(( $(cap_khz)/1000 )) mx=$(( $(max_khz)/1000 )) if [ "$cap" -le 3300 ]; then ok "CPU cap: ${cap} MHz (turbo disabled; hardware max ${mx} MHz)"; else bad "CPU NOT capped (${cap} MHz) — run: fanfix install"; fi [ -e "$CONF" ] && ok "cap persists across reboots ($CONF)" || warn "cap is not persisted — run: fanfix install" [ "$(rd /sys/devices/system/cpu/cpufreq/boost)" = 1 ] && ok "global boost flag = 1 (needed by power-profiles-daemon)" || warn "global boost flag = 0 — power-profiles-daemon profile switching will fail; run: fanfix install" local prof; prof=$(powerprofilesctl get 2>/dev/null || echo '?') [ "$prof" = performance ] && ok "power profile: performance (instant ramp-up)" || warn "power profile: $prof (performance is snappier; power-saver also safe)" read -r avg mn mxf <<<"$(freqs)" echo " temp: CPU $(tctl) °C package $(ppt_w) W clocks avg ${avg} / max ${mxf} MHz" echo " $(fan_line)" if systemctl is-active --quiet "$FAN_UNIT" 2>/dev/null; then ok "fan curve daemon running (fanfix fan curve off to stop)"; fi local busy; busy=$(cpu_busy_pct); read -r avg mn mxf <<<"$(freqs)" if [ "$busy" -gt 25 ] && [ "$mxf" -lt "$TRIP_MHZ" ]; then bad "EC THROTTLE ACTIVE RIGHT NOW (cores at ${mxf} MHz with ${busy}% load) — lower the cap: fanfix install 3000"; else ok "no EC throttle active (load ${busy}%)"; fi } cmd_watch() { local iv=${1:-1} echo "live view every ${iv}s — Ctrl-C to quit (THROTTLE = EC clamp active)" printf "%-8s %-6s %-8s %-20s %-s\n" "time" "Tctl" "pkg W" "clocks avg/max MHz" "fans" while true; do read -r avg mn mxf <<<"$(freqs)" local flag=""; [ "$mxf" -lt "$TRIP_MHZ" ] && flag=" <-- THROTTLE" printf "%-8s %3d°C %6s %5d / %5d %s%s\n" "$(date +%H:%M:%S)" "$(tctl)" "$(ppt_w)" "$avg" "$mxf" "$(fan_line)" "$flag" sleep "$iv" done } cmd_test() { local secs=${1:-30} threads=${2:-$(nproc)} bold "fanfix test — ${threads} threads for ${secs}s (Ctrl-C aborts)" echo "cap: $(( $(cap_khz)/1000 )) MHz profile: $(powerprofilesctl get 2>/dev/null)" stress_start "$threads" "$secs"; trap 'stress_stop; exit 130' INT TERM local i trips=0 prev=9999 tmax=0 fsum=0 n=0 for i in $(seq 1 "$secs"); do sleep 1 read -r avg mn mxf <<<"$(freqs)"; local t; t=$(tctl) [ "$mxf" -lt "$TRIP_MHZ" ] && [ "$prev" -ge "$TRIP_MHZ" ] && trips=$((trips+1)); prev=$mxf [ "$t" -gt "$tmax" ] && tmax=$t; fsum=$((fsum+avg)); n=$((n+1)) printf " %3ds Tctl=%2d°C pkg=%5sW avg=%4d max=%4d MHz %s%s\n" "$i" "$t" "$(ppt_w)" "$avg" "$mxf" "$(fan_line)" "$([ "$mxf" -lt "$TRIP_MHZ" ] && echo ' <-- THROTTLE')" done stress_stop; trap - INT TERM echo; bold "result: peak ${tmax}°C, mean clock $((fsum/n)) MHz, EC throttle events: ${trips}" if [ "$trips" -gt 0 ]; then bad "throttling still happens — lower the cap: fanfix install 3000"; else ok "no throttling — cap is doing its job"; fi [ "$tmax" -ge 75 ] && warn "peak ${tmax}°C is close to the 79°C trip; consider: fanfix install 3000" } cmd_install() { local cap=${1:-$CAP_DEFAULT_KHZ} [[ "$cap" =~ ^[0-9]+$ ]] || die "cap must be a number (MHz or kHz), e.g. 3200 or 3200000" [ "$cap" -lt 100000 ] && cap=$((cap*1000)) # MHz given → kHz if [ "$cap" -lt 1500000 ] || [ "$cap" -gt "$(max_khz)" ]; then die "cap out of range (1500–$(( $(max_khz)/1000 )) MHz)"; fi bold "fanfix install — cap every core at $((cap/1000)) MHz (asks for sudo)" if [ -e "$OLD_CONF" ] || [ "$(rd /sys/devices/system/cpu/cpufreq/boost)" = 0 ]; then echo " undoing old global-boost approach (conflicts with power-profiles-daemon)" sudo rm -f "$OLD_CONF"; echo 1 | sudo tee /sys/devices/system/cpu/cpufreq/boost >/dev/null fi sudo tee "$CONF" >/dev/null </dev/null || warn "could not set performance profile (powerprofilesctl get)" sudo systemd-tmpfiles --create "$CONF" # profile switches toggle boost; re-assert the cap ok "cap = $(( $(cap_khz)/1000 )) MHz on all cores, persisted in $CONF" ok "profile = $(powerprofilesctl get 2>/dev/null)" echo; cmd_test 20 } cmd_uninstall() { bold "fanfix uninstall — remove the CPU cap (asks for sudo)" sudo rm -f "$CONF" "$OLD_CONF" echo 1 | sudo tee /sys/devices/system/cpu/cpufreq/boost >/dev/null local f; for f in /sys/devices/system/cpu/cpu*/cpufreq/scaling_max_freq; do sudo tee "$f" >/dev/null < "${f%scaling_max_freq}cpuinfo_max_freq"; done ok "cap removed: max = $(( $(cap_khz)/1000 )) MHz, turbo boost back on" warn "only do this once the GPU fan is replaced — otherwise the 399 MHz stalls come back" } cmd_fans() { local persist=0; [ "${1:-}" = "--persist" ] && persist=1 if ! hwmon_by_name uniwill >/dev/null; then bold "loading fan driver (uniwill-laptop force=1) (asks for sudo)" sudo modprobe uniwill-laptop force=1 || die "modprobe failed"; sleep 1 fi if [ "$persist" = 1 ]; then echo 'uniwill-laptop' | sudo tee /etc/modules-load.d/uniwill-laptop.conf >/dev/null echo 'options uniwill-laptop force=1' | sudo tee /etc/modprobe.d/uniwill-laptop.conf >/dev/null ok "fan driver will load at every boot (remove /etc/modules-load.d/uniwill-laptop.conf to undo)" fi echo " $(fan_line)" local h; h=$(hwmon_by_name uniwill); [ "$(rd "$h/fan2_input")" = 0 ] && [ "$(rd "$h/pwm2")" -gt 0 ] && bad "GPU fan commanded $(( $(rd "$h/pwm2")*100/255 ))% but 0 rpm → dead/jammed" || true } # ---- fan control ---- cmd_fan() { local sub=${1:-status}; shift || true case "$sub" in setup) cmd_fan_setup "$@" ;; status) need_root fan status; cmd_fan_status ;; auto) need_root fan auto; fan_guard; fan_stop_daemon; fan_set_auto && ok "fans back to automatic (EC curve)" ;; max) need_root fan max; fan_guard; fan_stop_daemon; fan_apply_duty 200 && ok "fans set to 100 % (manual) — 'fanfix fan auto' to release" ;; curve) cmd_fan_curve "$@" ;; [0-9]*) local p=${sub%\%}; [[ "$p" =~ ^[0-9]+$ ]] || die "percent must be a number" [ "$p" -le 100 ] || die "max is 100" [ "$p" -ge "$FAN_MIN_PCT" ] || die "refusing < ${FAN_MIN_PCT} % — EC stalls the CPU at 79 °C; 30 % is the floor (use 'fanfix fan auto' for idle fan-off)" need_root fan "$sub"; fan_guard; fan_stop_daemon fan_apply_duty "$(pct_to_duty "$p")" && ok "fans set to ${p} % (manual) — stays until 'fanfix fan auto' (EC trip at 79 °C still protects you)" ;; -h|--help|help) cmd_help ;; *) die "unknown fan command: $sub" ;; esac } fan_guard() { ec_ready || die "acpi_call not available — run: fanfix fan setup"; ec_read $R_AP_OEM >/dev/null || die "EC not reachable via ${EC_DEV}.ECRR"; } fan_stop_daemon() { systemctl is-active --quiet "$FAN_UNIT" 2>/dev/null && { systemctl stop "$FAN_UNIT"; echo " (stopped fan curve daemon)"; }; return 0; } cmd_fan_setup() { bold "fanfix fan setup — install EC access (acpi_call) and load drivers (asks for sudo)" if ! modinfo acpi_call >/dev/null 2>&1; then sudo pacman -S --needed --noconfirm acpi_call-dkms || die "pacman failed (needs internet; package: acpi_call-dkms)" fi sudo modprobe acpi_call || die "could not load acpi_call" sudo modprobe uniwill-laptop force=1 2>/dev/null || true printf 'acpi_call\nuniwill-laptop\n' | sudo tee /etc/modules-load.d/fanfix.conf >/dev/null echo 'options uniwill-laptop force=1' | sudo tee /etc/modprobe.d/uniwill-laptop.conf >/dev/null ok "acpi_call + uniwill-laptop load at every boot" sudo "$SELF" fan status } cmd_fan_status() { fan_guard bold "fanfix fan status" local ap; ap=$(ec_read $R_AP_OEM) echo " mode : $(fan_mode_text)" echo " CPU fan : $(( $(ec_read $R_FAN1_RPM)*256 + $(ec_read $((R_FAN1_RPM+1))) )) rpm duty $(duty_to_pct "$(ec_read $R_PWM1)") % CPU $(ec_read $R_CPU_TEMP) °C" echo " GPU fan : $(( $(ec_read $R_FAN2_RPM)*256 + $(ec_read $((R_FAN2_RPM+1))) )) rpm duty $(duty_to_pct "$(ec_read $R_PWM2)") % GPU $(ec_read $R_GPU_TEMP) °C" echo " EC flags : fan-abnormal=$(( (ap>>5)&1 )) manual-ctrl=$(( ap&1 )) universal-fan-ctrl=$(universal_ctrl) custom-tables=$(tables_enabled) full-fan-mode=$(ec_bit $R_FAN_MODE 6) (0x0751=$(printf '0x%02x' "$(ec_read $R_FAN_MODE)"))" [ "$(( (ap>>5)&1 ))" = 1 ] && warn "EC reports FAN ABNORMAL — this is the flag behind the 79 °C throttle policy" systemctl is-active --quiet "$FAN_UNIT" 2>/dev/null && echo " curve : ${CURVE:-$DEFAULT_CURVE} (from ${FAN_CONF}, if present)" return 0 } cmd_fan_curve() { local a=${1:-status} case "$a" in on|start) need_root fan curve on; fan_guard cat > /etc/systemd/system/$FAN_UNIT </dev/null; rm -f /etc/systemd/system/$FAN_UNIT; systemctl daemon-reload fan_guard; fan_set_auto && ok "curve daemon removed, fans back to automatic" ;; restart) need_root fan curve restart; systemctl restart "$FAN_UNIT" && ok "restarted" ;; status|*) systemctl is-active --quiet "$FAN_UNIT" 2>/dev/null && { ok "curve daemon running"; systemctl status "$FAN_UNIT" --no-pager -n 5 2>/dev/null | tail -6; } || echo " curve daemon not running (fanfix fan curve on)" ;; esac } cmd_fan_daemon() { # root; run by the systemd unit [ -r "$FAN_CONF" ] && . "$FAN_CONF" ec_ready || { echo "acpi_call missing"; exit 1; } trap 'fan_set_auto; exit 0' TERM INT local last=-1 t p d n=0 echo "fanfix fan-daemon: curve ${CURVE:-$DEFAULT_CURVE}" while true; do t=$(tctl); [ "$t" -le 0 ] && t=$(ec_read $R_CPU_TEMP 2>/dev/null || echo 0) if [ "$t" -le 0 ]; then fan_set_auto; echo "no temperature reading — fans released to auto"; sleep 5; continue; fi p=$(curve_pct_for_temp "$t"); [ "$p" -lt "$FAN_MIN_PCT" ] && p=$FAN_MIN_PCT d=$(pct_to_duty "$p") # re-apply when the target changes by ≥2 %, and at least every 60 s (old-path ECs revert after a while) local changed=0; [ $(( d > last ? d - last : last - d )) -ge 4 ] && changed=1 if [ "$changed" = 1 ] || [ $(( n % 30 )) = 0 ]; then fan_apply_duty "$d" && last=$d [ "$changed" = 1 ] && echo "CPU ${t} °C → fan ${p} %" fi n=$((n+1)); sleep 2 done } cmd_help() { cat < fixed fan speed in % (manual, until 'auto'); e.g. fanfix fan 60 [sudo] fanfix fan max 100 % [sudo] fanfix fan auto give control back to the EC [sudo] fanfix fan curve on|off background daemon: CPU temp → fan %, more cooling than the EC's curve [sudo] default curve ${DEFAULT_CURVE}; override in ${FAN_CONF} Typical: fanfix status · stalls seen? → fanfix install 3000 · want more headroom? → fanfix fan curve on · fan replaced? → fanfix uninstall EOF } case "${1:-help}" in status) shift; cmd_status "$@" ;; watch) shift; cmd_watch "$@" ;; test) shift; cmd_test "$@" ;; install|cap) shift; cmd_install "$@" ;; uninstall|uncap) shift; cmd_uninstall "$@" ;; fans) shift; cmd_fans "$@" ;; fan) shift; cmd_fan "$@" ;; fan-daemon) cmd_fan_daemon ;; fan-release) ec_ready && fan_set_auto ;; help|-h|--help) cmd_help ;; *) bad "unknown command: $1"; cmd_help; exit 2 ;; esac