From 73e8ae4fbd1beb1b310719717f419fcc1ab979a5 Mon Sep 17 00:00:00 2001 From: erpalma Date: Mon, 2 Apr 2018 14:51:06 +0200 Subject: [PATCH] initial push --- Makefile | 7 +++++++ lenovo_fix.py | 38 ++++++++++++++++++++++++++++++++++++++ lenovo_fix.service | 9 +++++++++ 3 files changed, 54 insertions(+) create mode 100644 Makefile create mode 100755 lenovo_fix.py create mode 100644 lenovo_fix.service diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f9fb979 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +all: install + +install: + install -d /usr/local/sbin/ + install -m 700 lenovo_fix.py /usr/local/sbin/ + install -d /etc/systemd/system/ + install -m 644 lenovo_fix.service /etc/systemd/system/ \ No newline at end of file diff --git a/lenovo_fix.py b/lenovo_fix.py new file mode 100755 index 0000000..e3aee04 --- /dev/null +++ b/lenovo_fix.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python2 + +import glob +import os +import struct + +from periphery import MMIO +from time import sleep + +UPDATE_RATE_SEC = 15 + + +def writemsr(msr, val): + n = glob.glob('/dev/cpu/[0-9]*/msr') + for c in n: + f = os.open(c, os.O_WRONLY) + os.lseek(f, msr, os.SEEK_SET) + os.write(f, struct.pack('Q', val)) + os.close(f) + if not n: + raise OSError("msr module not loaded (run modprobe msr)") + + +def main(): + mchbar_mmio = MMIO(0xfed159a0, 8) + while True: + # set temperature trip point to 97 C + writemsr(0x1a2, 0x3000000) + # set MSR to PL1 45W, max duration - PL2 45W, 2ms + writemsr(0x610, 0x42816800fe8168) + # set MCHBAR register to the same PL1/2 values + mchbar_mmio.write32(0, 0x00fe8168) + mchbar_mmio.write32(4, 0x00428168) + sleep(UPDATE_RATE_SEC) + + +if __name__ == '__main__': + main() diff --git a/lenovo_fix.service b/lenovo_fix.service new file mode 100644 index 0000000..0735e88 --- /dev/null +++ b/lenovo_fix.service @@ -0,0 +1,9 @@ +[Unit] +Description=Fix Lenovo T480/T480s/X1C6 throttling on Linux + +[Service] +Type=simple +ExecStart=/usr/local/sbin/lenovo_fix.py + +[Install] +WantedBy=multi-user.target \ No newline at end of file