Skip to content

Instantly share code, notes, and snippets.

@Jimmy-Z
Created February 21, 2021 18:16
Show Gist options
  • Save Jimmy-Z/6d481a6b6ec9843fb1b1a3a86f0e51ad to your computer and use it in GitHub Desktop.
Save Jimmy-Z/6d481a6b6ec9843fb1b1a3a86f0e51ad to your computer and use it in GitHub Desktop.
AMD GPU power control
#!/bin/sh
enum () {
# what happens if you have more than 10 cards?
ls /sys/class/drm/|grep -E '^card[0-9]$'
}
get_hwmon () {
card=$1
# is it possible to have more than 1 hwmon?
ls /sys/class/drm/${card}/device/hwmon/
}
fanmodename () {
case $1 in
"1") echo manual ;;
"2") echo auto ;;
*) echo unknown ;;
esac
}
vendorname () {
case $1 in
"0x1002") echo AMD ;;
*) echo unknown ;;
esac
}
devicename () {
case $1 in
"0x67b1") echo Hawaii Pro ;;
"0x67df") echo Ellesmere ;;
*) echo unknown ;;
esac
}
show () {
card=$1
device=/sys/class/drm/$card/device
vid=$(cat $device/vendor)
did=$(cat $device/device)
echo === $(vendorname $vid) $(devicename $did) \($card $vid:$did\) ===
if test $vid != "0x1002"; then
echo unsupported
return
fi
mon=$device/hwmon/$(get_hwmon $card)
power=$(cat $mon/power1_average)
power=$(expr $power / 1000000)
powercap=$(cat $mon/power1_cap)
powercap=$(expr $powercap / 1000000)
fanmode=$(cat $mon/pwm1_enable)
fanpwm=$(cat $mon/pwm1)
fanpwmmax=$(cat $mon/pwm1_max)
fanrpm=$(cat $mon/fan1_input)
temp=$(cat $mon/temp1_input)
temp=$(expr $temp / 1000)
mem=$(cat $device/mem_info_vram_used)
mem=$(expr $mem / 1048576)
memtotal=$(cat $device/mem_info_vram_total)
memtotal=$(expr $memtotal / 1048576)
echo Power: $power/$powercap Watts, Temp: $temp C, Fan: $fanrpm RPM \($(fanmodename $fanmode) $fanpwm/$fanpwmmax\), VRAM: $mem/$memtotal MB
}
cfg () {
hwmon=/sys/class/drm/card$1/device/hwmon/$(get_hwmon card$1)
pwr=$2
pwm=$3
echo $(expr $pwr \* 1000000)|sudo tee $hwmon/power1_cap
if test $(cat $hwmon/pwm1_enable) -ne 1; then
echo "setting fan mode to manual"
echo 1|sudo tee $hwmon/pwm1_enable
fi
echo $pwm|sudo tee $hwmon/pwm1
}
if test $# -eq 0; then
for card in $(enum); do
show $card
done
exit 0
fi
# TODO: check parameters
cfg $1 $2 $3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment