lenovo_fix.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #!/usr/bin/env python3
  2. import argparse
  3. import configparser
  4. import dbus
  5. import os
  6. import psutil
  7. import struct
  8. import subprocess
  9. import sys
  10. from collections import defaultdict
  11. from dbus.mainloop.glib import DBusGMainLoop
  12. from errno import EACCES, EPERM
  13. from gi.repository import GLib
  14. from mmio import MMIO, MMIOError
  15. from multiprocessing import cpu_count
  16. from threading import Event, Thread
  17. SYSFS_POWER_PATH = '/sys/class/power_supply/AC/online'
  18. VOLTAGE_PLANES = {
  19. 'CORE': 0,
  20. 'GPU': 1,
  21. 'CACHE': 2,
  22. 'UNCORE': 3,
  23. 'ANALOGIO': 4,
  24. }
  25. TRIP_TEMP_RANGE = [40, 97]
  26. power = {'source': None, 'method': 'polling'}
  27. platform_info_bits = {
  28. 'maximum_non_turbo_ratio': [8, 15],
  29. 'maximum_efficiency_ratio': [40, 47],
  30. 'minimum_operating_ratio': [48, 55],
  31. 'feature_ppin_cap': [23, 23],
  32. 'feature_programmable_turbo_ratio': [28, 28],
  33. 'feature_programmable_tdp_limit': [29, 29],
  34. 'number_of_additional_tdp_profiles': [33, 34],
  35. 'feature_programmable_temperature_target': [30, 30],
  36. 'feature_low_power_mode': [32, 32]
  37. }
  38. thermal_status_bits = {
  39. 'thermal_limit_status': [0, 0],
  40. 'thermal_limit_log': [1, 1],
  41. 'prochot_or_forcepr_status': [2, 2],
  42. 'prochot_or_forcepr_log': [3, 3],
  43. 'crit_temp_status': [4, 4],
  44. 'crit_temp_log': [5, 5],
  45. 'thermal_threshold1_status': [6, 6],
  46. 'thermal_threshold1_log': [7, 7],
  47. 'thermal_threshold2_status': [8, 8],
  48. 'thermal_threshold2_log': [9, 9],
  49. 'power_limit_status': [10, 10],
  50. 'power_limit_log': [11, 11],
  51. 'current_limit_status': [12, 12],
  52. 'current_limit_log': [13, 13],
  53. 'cross_domain_limit_status': [14, 14],
  54. 'cross_domain_limit_log': [15, 15],
  55. 'cpu_temp': [16, 22],
  56. 'temp_resolution': [27, 30],
  57. 'reading_valid': [31, 31],
  58. }
  59. def writemsr(msr, val):
  60. msr_list = ['/dev/cpu/{:d}/msr'.format(x) for x in range(cpu_count())]
  61. if not os.path.exists(msr_list[0]):
  62. try:
  63. subprocess.check_call(('modprobe', 'msr'))
  64. except subprocess.CalledProcessError:
  65. print('[E] Unable to load the msr module.')
  66. sys.exit(1)
  67. try:
  68. for addr in msr_list:
  69. f = os.open(addr, os.O_WRONLY)
  70. os.lseek(f, msr, os.SEEK_SET)
  71. os.write(f, struct.pack('Q', val))
  72. os.close(f)
  73. except (IOError, OSError) as e:
  74. if e.errno == EPERM or e.errno == EACCES:
  75. print('[E] Unable to write to MSR. Try to disable Secure Boot.')
  76. sys.exit(1)
  77. else:
  78. raise e
  79. # returns the value between from_bit and to_bit as unsigned long
  80. def readmsr(msr, from_bit=0, to_bit=63, cpu=None, flatten=False):
  81. assert cpu is None or cpu in range(cpu_count())
  82. if from_bit > to_bit:
  83. print('[E] Wrong readmsr bit params')
  84. sys.exit(1)
  85. msr_list = ['/dev/cpu/{:d}/msr'.format(x) for x in range(cpu_count())]
  86. if not os.path.exists(msr_list[0]):
  87. try:
  88. subprocess.check_call(('modprobe', 'msr'))
  89. except subprocess.CalledProcessError:
  90. print('[E] Unable to load the msr module.')
  91. sys.exit(1)
  92. try:
  93. output = []
  94. for addr in msr_list:
  95. f = os.open(addr, os.O_RDONLY)
  96. os.lseek(f, msr, os.SEEK_SET)
  97. val = struct.unpack('Q', os.read(f, 8))[0]
  98. os.close(f)
  99. output.append(get_value_for_bits(val, from_bit, to_bit))
  100. if flatten:
  101. return output[0] if len(set(output)) == 1 else output
  102. return output[cpu] if cpu is not None else output
  103. except (IOError, OSError) as e:
  104. if e.errno == EPERM or e.errno == EACCES:
  105. print('[E] Unable to read from MSR. Try to disable Secure Boot.')
  106. sys.exit(1)
  107. else:
  108. raise e
  109. def get_value_for_bits(val, from_bit=0, to_bit=63):
  110. mask = sum(2**x for x in range(from_bit, to_bit + 1))
  111. return (val & mask) >> from_bit
  112. def is_on_battery():
  113. with open(SYSFS_POWER_PATH) as f:
  114. return not bool(int(f.read()))
  115. def get_cpu_platform_info():
  116. features_msr_value = readmsr(0xce, cpu=0)
  117. cpu_platform_info = {}
  118. for key, value in platform_info_bits.items():
  119. cpu_platform_info[key] = int(get_value_for_bits(features_msr_value, value[0], value[1]))
  120. return cpu_platform_info
  121. def get_reset_thermal_status():
  122. #read thermal status
  123. thermal_status_msr_value = readmsr(0x19c)
  124. thermal_status = []
  125. for core in range(cpu_count()):
  126. thermal_status_core = {}
  127. for key, value in thermal_status_bits.items():
  128. thermal_status_core[key] = int(get_value_for_bits(thermal_status_msr_value[core], value[0], value[1]))
  129. thermal_status.append(thermal_status_core)
  130. #reset log bits
  131. writemsr(0x19c, 0)
  132. return thermal_status
  133. def get_time_unit():
  134. # 0.000977 is the time unit of my CPU
  135. # TODO formula might be different for other CPUs
  136. return 1.0 / 2**readmsr(0x606, 16, 19, cpu=0)
  137. def get_power_unit():
  138. # 0.125 is the power unit of my CPU
  139. # TODO formula might be different for other CPUs
  140. return 1.0 / 2**readmsr(0x606, 0, 3, cpu=0)
  141. def get_critical_temp():
  142. # the critical temperature for my CPU is 100 'C
  143. return readmsr(0x1a2, 16, 23, cpu=0)
  144. def calc_time_window_vars(t):
  145. time_unit = get_time_unit()
  146. for Y in range(2**5):
  147. for Z in range(2**2):
  148. if t <= (2**Y) * (1. + Z / 4.) * time_unit:
  149. return (Y, Z)
  150. raise ValueError('Unable to find a good combination!')
  151. def undervolt(config):
  152. for plane in VOLTAGE_PLANES:
  153. write_value = calc_undervolt_msr(plane, config.getfloat('UNDERVOLT', plane))
  154. writemsr(0x150, write_value)
  155. if args.debug:
  156. write_value &= 0xFFFFFFFF
  157. writemsr(0x150, 0x8000001000000000 | (VOLTAGE_PLANES[plane] << 40))
  158. read_value = readmsr(0x150, flatten=True)
  159. match = write_value == read_value
  160. print('[D] Undervolt plane {:s} - write {:#x} - read {:#x} - match {}'.format(
  161. plane, write_value, read_value, match))
  162. def calc_undervolt_msr(plane, offset):
  163. assert offset <= 0
  164. assert plane in VOLTAGE_PLANES
  165. offset = int(round(offset * 1.024))
  166. offset = 0xFFE00000 & ((offset & 0xFFF) << 21)
  167. return 0x8000001100000000 | (VOLTAGE_PLANES[plane] << 40) | offset
  168. def load_config():
  169. config = configparser.ConfigParser()
  170. config.read(args.config)
  171. # config values sanity check
  172. for power_source in ('AC', 'BATTERY'):
  173. for option in (
  174. 'Update_Rate_s',
  175. 'PL1_Tdp_W',
  176. 'PL1_Duration_s',
  177. 'PL2_Tdp_W',
  178. 'PL2_Duration_S',
  179. ):
  180. config.set(power_source, option, str(max(0.1, config.getfloat(power_source, option))))
  181. trip_temp = config.getfloat(power_source, 'Trip_Temp_C')
  182. valid_trip_temp = min(TRIP_TEMP_RANGE[1], max(TRIP_TEMP_RANGE[0], trip_temp))
  183. if trip_temp != valid_trip_temp:
  184. config.set(power_source, 'Trip_Temp_C', str(valid_trip_temp))
  185. print('[!] Overriding invalid "Trip_Temp_C" value in "{:s}": {:.1f} -> {:.1f}'.format(
  186. power_source, trip_temp, valid_trip_temp))
  187. for plane in VOLTAGE_PLANES:
  188. value = config.getfloat('UNDERVOLT', plane)
  189. valid_value = min(0, value)
  190. if value != valid_value:
  191. config.set('UNDERVOLT', plane, str(valid_value))
  192. print('[!] Overriding invalid "UNDERVOLT" value in "{:s}" voltage plane: {:.0f} -> {:.0f}'.format(
  193. plane, value, valid_value))
  194. return config
  195. def calc_reg_values(platform_info, config):
  196. regs = defaultdict(dict)
  197. for power_source in ('AC', 'BATTERY'):
  198. if platform_info['feature_programmable_temperature_target'] != 1:
  199. print("[W] Setting temperature target is not supported by this CPU")
  200. else:
  201. # the critical temperature for my CPU is 100 'C
  202. critical_temp = get_critical_temp()
  203. # update the allowed temp range to keep at least 3 'C from the CPU critical temperature
  204. global TRIP_TEMP_RANGE
  205. TRIP_TEMP_RANGE[1] = min(TRIP_TEMP_RANGE[1], critical_temp - 3)
  206. trip_offset = int(round(critical_temp - config.getfloat(power_source, 'Trip_Temp_C')))
  207. regs[power_source]['MSR_TEMPERATURE_TARGET'] = trip_offset << 24
  208. power_unit = get_power_unit()
  209. PL1 = int(round(config.getfloat(power_source, 'PL1_Tdp_W') / power_unit))
  210. Y, Z = calc_time_window_vars(config.getfloat(power_source, 'PL1_Duration_s'))
  211. TW1 = Y | (Z << 5)
  212. PL2 = int(round(config.getfloat(power_source, 'PL2_Tdp_W') / power_unit))
  213. Y, Z = calc_time_window_vars(config.getfloat(power_source, 'PL2_Duration_s'))
  214. TW2 = Y | (Z << 5)
  215. regs[power_source]['MSR_PKG_POWER_LIMIT'] = PL1 | (1 << 15) | (TW1 << 17) | (PL2 << 32) | (1 << 47) | (
  216. TW2 << 49)
  217. # cTDP
  218. c_tdp_target_value = config.getint(power_source, 'cTDP', fallback=None)
  219. if c_tdp_target_value is not None:
  220. if platform_info['feature_programmable_tdp_limit'] != 1:
  221. print("[W] cTDP setting not supported by this CPU")
  222. elif platform_info['number_of_additional_tdp_profiles'] < c_tdp_target_value:
  223. print("[W] the configured cTDP profile is not supported by this CPU")
  224. else:
  225. valid_c_tdp_target_value = max(0, c_tdp_target_value)
  226. regs[power_source]['MSR_CONFIG_TDP_CONTROL'] = valid_c_tdp_target_value
  227. return regs
  228. def set_hwp(pref):
  229. # set HWP energy performance hints
  230. assert pref in ('performance', 'balance_performance', 'default', 'balance_power', 'power')
  231. CPUs = [
  232. '/sys/devices/system/cpu/cpu{:d}/cpufreq/energy_performance_preference'.format(x) for x in range(cpu_count())
  233. ]
  234. for i, c in enumerate(CPUs):
  235. with open(c, 'wb') as f:
  236. f.write(pref.encode())
  237. if args.debug:
  238. with open(c) as f:
  239. read_value = f.read().strip()
  240. match = pref == read_value
  241. print('[D] HWP for cpu{:d} - write "{:s}" - read "{:s}" - match {}'.format(i, pref, read_value, match))
  242. def power_thread(config, regs, exit_event):
  243. try:
  244. mchbar_mmio = MMIO(0xfed159a0, 8)
  245. except MMIOError:
  246. print('[E] Unable to open /dev/mem. Try to disable Secure Boot.')
  247. sys.exit(1)
  248. while not exit_event.is_set():
  249. #print thermal status
  250. if args.debug:
  251. thermal_status = get_reset_thermal_status()
  252. for index, core_thermal_status in enumerate(thermal_status):
  253. for key, value in core_thermal_status.items():
  254. print('[D] core {} thermal status: {} = {}'.format(index, key.replace("_", " "), value))
  255. # switch back to sysfs polling
  256. if power['method'] == 'polling':
  257. power['source'] = 'BATTERY' if is_on_battery() else 'AC'
  258. # set temperature trip point
  259. if 'MSR_TEMPERATURE_TARGET' in regs[power['source']]:
  260. write_value = regs[power['source']]['MSR_TEMPERATURE_TARGET']
  261. writemsr(0x1a2, write_value)
  262. if args.debug:
  263. read_value = readmsr(0x1a2, 24, 29, flatten=True)
  264. match = write_value >> 24 == read_value
  265. print('[D] TEMPERATURE_TARGET - write {:#x} - read {:#x} - match {}'.format(
  266. write_value >> 24, read_value, match))
  267. # set cTDP
  268. if 'MSR_CONFIG_TDP_CONTROL' in regs[power['source']]:
  269. write_value = regs[power['source']]['MSR_CONFIG_TDP_CONTROL']
  270. writemsr(0x64b, write_value)
  271. if args.debug:
  272. read_value = readmsr(0x64b, 0, 1, flatten=True)
  273. match = write_value == read_value
  274. print('[D] CONFIG_TDP_CONTROL - write {:#x} - read {:#x} - match {}'.format(
  275. write_value, read_value, match))
  276. # set PL1/2 on MSR
  277. write_value = regs[power['source']]['MSR_PKG_POWER_LIMIT']
  278. writemsr(0x610, write_value)
  279. if args.debug:
  280. read_value = readmsr(0x610, 0, 55, flatten=True)
  281. match = write_value == read_value
  282. print('[D] MSR PACKAGE_POWER_LIMIT - write {:#x} - read {:#x} - match {}'.format(
  283. write_value, read_value, match))
  284. # set MCHBAR register to the same PL1/2 values
  285. mchbar_mmio.write32(0, write_value & 0xffffffff)
  286. mchbar_mmio.write32(4, write_value >> 32)
  287. if args.debug:
  288. read_value = mchbar_mmio.read32(0) | (mchbar_mmio.read32(4) << 32)
  289. match = write_value == read_value
  290. print('[D] MCHBAR PACKAGE_POWER_LIMIT - write {:#x} - read {:#x} - match {}'.format(
  291. write_value, read_value, match))
  292. wait_t = config.getfloat(power['source'], 'Update_Rate_s')
  293. enable_hwp_mode = config.getboolean('AC', 'HWP_Mode', fallback=False)
  294. if power['source'] == 'AC' and enable_hwp_mode:
  295. cpu_usage = float(psutil.cpu_percent(interval=wait_t))
  296. # set full performance mode only when load is greater than this threshold (~ at least 1 core full speed)
  297. performance_mode = cpu_usage > 100. / (cpu_count() * 1.25)
  298. # check again if we are on AC, since in the meantime we might have switched to BATTERY
  299. if not is_on_battery():
  300. set_hwp('performance' if performance_mode else 'balance_performance')
  301. else:
  302. exit_event.wait(wait_t)
  303. def main():
  304. global args
  305. if os.geteuid() != 0:
  306. print('[E] No root no party. Try again with sudo.')
  307. sys.exit(1)
  308. parser = argparse.ArgumentParser()
  309. parser.add_argument('--debug', action='store_true', help='add some debug info and additional checks')
  310. parser.add_argument('--config', default='/etc/lenovo_fix.conf', help='override default config file path')
  311. args = parser.parse_args()
  312. power['source'] = 'BATTERY' if is_on_battery() else 'AC'
  313. config = load_config()
  314. platform_info = get_cpu_platform_info()
  315. if args.debug:
  316. for key, value in platform_info.items():
  317. print('[D] cpu platform info: {} = {}'.format(key.replace("_", " "), value))
  318. regs = calc_reg_values(platform_info, config)
  319. if not config.getboolean('GENERAL', 'Enabled'):
  320. return
  321. exit_event = Event()
  322. thread = Thread(target=power_thread, args=(config, regs, exit_event))
  323. thread.daemon = True
  324. thread.start()
  325. undervolt(config)
  326. # handle dbus events for applying undervolt on resume from sleep/hybernate
  327. def handle_sleep_callback(sleeping):
  328. if not sleeping:
  329. undervolt(config)
  330. def handle_ac_callback(*args):
  331. try:
  332. power['source'] = 'BATTERY' if args[1]['Online'] == 0 else 'AC'
  333. power['method'] = 'dbus'
  334. except:
  335. power['method'] = 'polling'
  336. DBusGMainLoop(set_as_default=True)
  337. bus = dbus.SystemBus()
  338. # add dbus receiver only if undervolt is enabled in config
  339. if any(config.getfloat('UNDERVOLT', plane) != 0 for plane in VOLTAGE_PLANES):
  340. bus.add_signal_receiver(handle_sleep_callback, 'PrepareForSleep', 'org.freedesktop.login1.Manager',
  341. 'org.freedesktop.login1')
  342. bus.add_signal_receiver(
  343. handle_ac_callback,
  344. signal_name="PropertiesChanged",
  345. dbus_interface="org.freedesktop.DBus.Properties",
  346. path="/org/freedesktop/UPower/devices/line_power_AC")
  347. try:
  348. loop = GLib.MainLoop()
  349. loop.run()
  350. except (KeyboardInterrupt, SystemExit):
  351. pass
  352. exit_event.set()
  353. loop.quit()
  354. thread.join(timeout=1)
  355. if __name__ == '__main__':
  356. main()