lenovo_fix.py 16 KB

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