Skip to content

Instantly share code, notes, and snippets.

@ohaiibuzzle
Last active September 3, 2023 20:03
Show Gist options
  • Save ohaiibuzzle/078944107fa2459da39e68bf42a87e3a to your computer and use it in GitHub Desktop.
Save ohaiibuzzle/078944107fa2459da39e68bf42a87e3a to your computer and use it in GitHub Desktop.
Script to calculate right values for MSR_PKG_POWER_LIMIT
import math
# Get this value by reading msr 0x606
rapl_power_unit_value = 0x330A0E08
# pkg_pl_value = 0x860000DD8600
# Extract the power unit values
# Power units are in bits 3:0
power_units = pow(0.5, (rapl_power_unit_value & 0xF))
time_units = pow(0.5, ((rapl_power_unit_value & 0xF0) >> 8))
print(f"Power Units: {power_units}W")
print(f"Time Units: {time_units}ms")
# pl1 = (pkg_pl_value & 0x7FFF) * power_units
# print("PL1: ", pl1)
# pl1_enable = (pkg_pl_value & 0x8000) >> 15
# print("PL1 Enable: ", pl1_enable)
# pl1_clamp = (pkg_pl_value & 0x10000) >> 16
# print("PL1 Clamp: ", pl1_clamp)
# # pl1_y is bit 21:17
# pl1_y = (pkg_pl_value & 0x3E0000) >> 17
# # pl1_z is bit 23:22
# pl1_z = (pkg_pl_value & 0xC00000) >> 22
# # Time limit = 2^Y * (1.0 + Z/4.0) * Time_Unit
# pl1_tl = pow(2, pl1_y) * (1.0 + pl1_z / 4.0) * time_units
# print("PL1 Time Window: ", pl1_tl)
# pl2 = (pkg_pl_value >> 32 & 0x7FFF) * power_units
# print("PL2: ", pl2)
# pl2_enable = (pkg_pl_value >> 32 & 0x8000) >> 15
# print("PL2 Enable: ", pl2_enable)
# pl2_clamp = (pkg_pl_value >> 32 & 0x10000) >> 16
# print("PL2 Clamp: ", pl2_clamp)
# pl2_y = (pkg_pl_value >> 32 & 0x3E0000) >> 17
# pl2_z = (pkg_pl_value >> 32 & 0xC00000) >> 22
# pl2_tl = pow(2, pl2_y) * (1.0 + pl2_z / 4.0) * time_units
# print("PL2 Time Window: ", pl2_tl)
# print("Lock: ", (pkg_pl_value >> 63) & 0x1)
# These values are meant for a passively cooled N3450
pl1 = 6
pl1_enable = 1
pl1_clamp = 1
pl1_tl = 28672
pl2 = 10
pl2_enable = 1
pl2_clamp = 1
pl2_tl = 8000
lock = 0
# repacking the values
pkg_pl_value = 0
pkg_pl_value |= int(pl1 / power_units)
pkg_pl_value |= pl1_enable << 15
pkg_pl_value |= pl1_clamp << 16
# repack the time window
pl1_y = int(math.log(pl1_tl / time_units, 2))
pl1_z = int((pl1_tl / time_units / pow(2, pl1_y) - 1) * 4)
pkg_pl_value |= pl1_y << 17
pkg_pl_value |= pl1_z << 22
pkg_pl_value |= int(pl2 / power_units) << 32
pkg_pl_value |= pl2_enable << 47
pkg_pl_value |= pl2_clamp << 48
pl2_y = int(math.log(pl2_tl / time_units, 2))
pl2_z = int((pl2_tl / time_units / pow(2, pl2_y) - 1) * 4)
pkg_pl_value |= pl2_y << 49
pkg_pl_value |= pl2_z << 54
pkg_pl_value |= lock << 63
print("Repacked: ", hex(pkg_pl_value))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment