From 23730180f0d3349064fe4fbbe65b5efb802a54f6 Mon Sep 17 00:00:00 2001 From: erpalma Date: Thu, 30 Aug 2018 14:00:44 +0200 Subject: [PATCH] show undervolt planes values in mV in debug --- lenovo_fix.py | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/lenovo_fix.py b/lenovo_fix.py index 6e326c1..1027bc2 100755 --- a/lenovo_fix.py +++ b/lenovo_fix.py @@ -178,20 +178,10 @@ def calc_time_window_vars(t): raise ValueError('Unable to find a good combination!') -def undervolt(config): - for plane in VOLTAGE_PLANES: - write_value = calc_undervolt_msr(plane, config.getfloat('UNDERVOLT', plane)) - writemsr(0x150, write_value) - if args.debug: - write_value &= 0xFFFFFFFF - writemsr(0x150, 0x8000001000000000 | (VOLTAGE_PLANES[plane] << 40)) - read_value = readmsr(0x150, flatten=True) - match = write_value == read_value - print('[D] Undervolt plane {:s} - write {:#x} - read {:#x} - match {}'.format( - plane, write_value, read_value, match)) - - def calc_undervolt_msr(plane, offset): + """Return the value to be written in the MSR 150h for setting the given + offset voltage (in mV) to the given voltage plane. + """ assert offset <= 0 assert plane in VOLTAGE_PLANES offset = int(round(offset * 1.024)) @@ -199,6 +189,29 @@ def calc_undervolt_msr(plane, offset): return 0x8000001100000000 | (VOLTAGE_PLANES[plane] << 40) | offset +def calc_undervolt_mv(msr_value): + """Return the offset voltage (in mV) from the given raw MSR 150h value. + """ + offset = (msr_value & 0xFFE00000) >> 21 + offset = offset if offset <= 0x400 else -(0x800 - offset) + return int(round(offset / 1.024)) + + +def undervolt(config): + for plane in VOLTAGE_PLANES: + write_offset_mv = config.getfloat('UNDERVOLT', plane) + write_value = calc_undervolt_msr(plane, write_offset_mv) + writemsr(0x150, write_value) + if args.debug: + write_value &= 0xFFFFFFFF + writemsr(0x150, 0x8000001000000000 | (VOLTAGE_PLANES[plane] << 40)) + read_value = readmsr(0x150, flatten=True) + read_offset_mv = calc_undervolt_mv(read_value) + match = write_value == read_value + print('[D] Undervolt plane {:s} - write {:.0f} mV ({:#x}) - read {:.0f} mV ({:#x}) - match {}'.format( + plane, write_offset_mv, write_value, read_offset_mv, read_value, match)) + + def load_config(): config = configparser.ConfigParser() config.read(args.config)