#!/bin/sh

# Initial value read 2026-02-26: e4005f

# BD-PROCHOT is a mechanism to protect processor overheating.
# The side-effect is that is slow the hell out of it by throttling the
# CPU frequency to its minimum.
#
# The model-specific register (MSR) holds the configuration for this
# feature at address 0x1FC. The last bit defines wether or not BD-PROCHOT
# will actually throttle o not the CPU.
#
# Clearing that last bit unleash the CPU (or rather prevents limiting it).

# Get full register value
reg="0x$(doas rdmsr 0x1FC)"

usage() {
	echo "usage: $(basename $0) [-cs]" >&2
}

bdph_bit() {
	echo $((reg & 0x000001))
}
bdph_set() {
	echo $((reg | 0x000001))
}
bdph_clr() {
	echo $((reg & 0xfffffe))
}

while getopts "hcs" OPT; do
	case "$OPT" in
	c) v=$((reg & 0xfffffe));;
	s) v=$((reg | 0x1));;
	h) usage; exit 0;;
	*) usage; exit 1;;
	esac
done
shift $((OPTIND-1))

if [ -n "$v" ]; then
	doas wrmsr 0x1FC $(printf %d $v)
	doas rdmsr 0x1FC
else
	bdph_bit
fi
