lenovo_fix.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/env python3
  2. import configparser
  3. import dbus
  4. import glob
  5. import os
  6. import struct
  7. import subprocess
  8. from collections import defaultdict
  9. from dbus.mainloop.glib import DBusGMainLoop
  10. from periphery import MMIO
  11. from threading import Event, Thread
  12. try:
  13. from gi.repository import GObject
  14. except ImportError:
  15. import gobject as GObject
  16. SYSFS_POWER_PATH = '/sys/class/power_supply/AC/online'
  17. CONFIG_PATH = '/etc/lenovo_fix.conf'
  18. VOLTAGE_PLANES = {
  19. 'CORE': 0,
  20. 'GPU': 1,
  21. 'CACHE': 2,
  22. 'UNCORE': 3,
  23. 'ANALOGIO': 4,
  24. }
  25. def writemsr(msr, val):
  26. n = glob.glob('/dev/cpu/[0-9]*/msr')
  27. for c in n:
  28. f = os.open(c, os.O_WRONLY)
  29. os.lseek(f, msr, os.SEEK_SET)
  30. os.write(f, struct.pack('Q', val))
  31. os.close(f)
  32. if not n:
  33. try:
  34. subprocess.check_call(('modprobe', 'msr'))
  35. except subprocess.CalledProcessError:
  36. raise OSError("Unable to load msr module.")
  37. def is_on_battery():
  38. with open(SYSFS_POWER_PATH) as f:
  39. return not bool(int(f.read()))
  40. def calc_time_window_vars(t):
  41. for Y in range(2**5):
  42. for Z in range(2**2):
  43. if t <= (2**Y) * (1. + Z / 4.) * 0.000977:
  44. return (Y, Z)
  45. raise Exception('Unable to find a good combination!')
  46. def undervolt(config):
  47. for plane in VOLTAGE_PLANES:
  48. writemsr(0x150, calc_undervolt_msr(plane, config.getfloat('UNDERVOLT', plane)))
  49. def calc_undervolt_msr(plane, offset):
  50. assert offset <= 0
  51. assert plane in VOLTAGE_PLANES
  52. offset = int(round(offset * 1.024))
  53. offset = 0xFFE00000 & ((offset & 0xFFF) << 21)
  54. return 0x8000001100000000 | (VOLTAGE_PLANES[plane] << 40) | offset
  55. def load_config():
  56. config = configparser.ConfigParser()
  57. config.read(CONFIG_PATH)
  58. for power_source in ('AC', 'BATTERY'):
  59. assert 0 < config.getfloat(power_source, 'Update_Rate_s')
  60. assert 0 < config.getfloat(power_source, 'PL1_Tdp_W')
  61. assert 0 < config.getfloat(power_source, 'PL1_Duration_s')
  62. assert 0 < config.getfloat(power_source, 'PL2_Tdp_W')
  63. assert 0 < config.getfloat(power_source, 'PL2_Duration_S')
  64. assert 40 < config.getfloat(power_source, 'Trip_Temp_C') < 98
  65. for plane in VOLTAGE_PLANES:
  66. assert config.getfloat('UNDERVOLT', plane) <= 0
  67. return config
  68. def calc_reg_values(config):
  69. regs = defaultdict(dict)
  70. for power_source in ('AC', 'BATTERY'):
  71. # the critical temperature for this CPU is 100 C
  72. trip_offset = int(round(100 - config.getfloat(power_source, 'Trip_Temp_C')))
  73. regs[power_source]['MSR_TEMPERATURE_TARGET'] = trip_offset << 24
  74. # 0.125 is the power unit of this CPU
  75. PL1 = int(round(config.getfloat(power_source, 'PL1_Tdp_W') / 0.125))
  76. Y, Z = calc_time_window_vars(config.getfloat(power_source, 'PL1_Duration_s'))
  77. TW1 = Y | (Z << 5)
  78. PL2 = int(round(config.getfloat(power_source, 'PL2_Tdp_W') / 0.125))
  79. Y, Z = calc_time_window_vars(config.getfloat(power_source, 'PL2_Duration_s'))
  80. TW2 = Y | (Z << 5)
  81. regs[power_source]['MSR_PKG_POWER_LIMIT'] = PL1 | (1 << 15) | (TW1 << 17) | (PL2 << 32) | (1 << 47) | (
  82. TW2 << 49)
  83. return regs
  84. def power_thread(config, regs, exit_event):
  85. mchbar_mmio = MMIO(0xfed159a0, 8)
  86. while not exit_event.is_set():
  87. power_source = 'BATTERY' if is_on_battery() else 'AC'
  88. # set temperature trip point
  89. writemsr(0x1a2, regs[power_source]['MSR_TEMPERATURE_TARGET'])
  90. # set PL1/2 on MSR
  91. writemsr(0x610, regs[power_source]['MSR_PKG_POWER_LIMIT'])
  92. # set MCHBAR register to the same PL1/2 values
  93. mchbar_mmio.write32(0, regs[power_source]['MSR_PKG_POWER_LIMIT'] & 0xffffffff)
  94. mchbar_mmio.write32(4, regs[power_source]['MSR_PKG_POWER_LIMIT'] >> 32)
  95. exit_event.wait(config.getfloat(power_source, 'Update_Rate_s'))
  96. def main():
  97. config = load_config()
  98. regs = calc_reg_values(config)
  99. if not config.getboolean('GENERAL', 'Enabled'):
  100. return
  101. exit_event = Event()
  102. t = Thread(target=power_thread, args=(config, regs, exit_event))
  103. t.start()
  104. undervolt(config)
  105. # handle dbus events for applying undervolt on resume from sleep/hybernate
  106. def handle_sleep_callback(sleeping):
  107. if not sleeping:
  108. undervolt(config)
  109. DBusGMainLoop(set_as_default=True)
  110. bus = dbus.SystemBus()
  111. # add dbus receiver only if undervolt is enabled in config
  112. if any(config.getfloat('UNDERVOLT', plane) != 0 for plane in VOLTAGE_PLANES):
  113. bus.add_signal_receiver(handle_sleep_callback, 'PrepareForSleep', 'org.freedesktop.login1.Manager',
  114. 'org.freedesktop.login1')
  115. try:
  116. GObject.threads_init()
  117. loop = GObject.MainLoop()
  118. loop.run()
  119. except (KeyboardInterrupt, SystemExit):
  120. pass
  121. exit_event.set()
  122. loop.quit()
  123. t.join()
  124. if __name__ == '__main__':
  125. main()