lenovo_fix.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. #!/usr/bin/env python3
  2. import argparse
  3. import configparser
  4. import dbus
  5. import glob
  6. import os
  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. DEFAULT_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 cpu_usage_pct(exit_event, interval=1.0):
  117. last_idle = last_total = 0
  118. for i in range(2):
  119. with open('/proc/stat') as f:
  120. fields = [float(column) for column in f.readline().strip().split()[1:]]
  121. idle, total = fields[3], sum(fields)
  122. idle_delta, total_delta = idle - last_idle, total - last_total
  123. last_idle, last_total = idle, total
  124. if i == 0:
  125. exit_event.wait(interval)
  126. return 100.0 * (1.0 - idle_delta / total_delta)
  127. def get_value_for_bits(val, from_bit=0, to_bit=63):
  128. mask = sum(2**x for x in range(from_bit, to_bit + 1))
  129. return (val & mask) >> from_bit
  130. def is_on_battery(config):
  131. try:
  132. for path in glob.glob(config.get('Sysfs_Power_Path', 'GENERAL', fallback=DEFAULT_SYSFS_POWER_PATH)):
  133. with open(path) as f:
  134. return not bool(int(f.read()))
  135. except:
  136. pass
  137. print('[E] No valid Sysfs_Power_Path found!')
  138. sys.exit(1)
  139. def get_cpu_platform_info():
  140. features_msr_value = readmsr(0xce, cpu=0)
  141. cpu_platform_info = {}
  142. for key, value in platform_info_bits.items():
  143. cpu_platform_info[key] = int(get_value_for_bits(features_msr_value, value[0], value[1]))
  144. return cpu_platform_info
  145. def get_reset_thermal_status():
  146. # read thermal status
  147. thermal_status_msr_value = readmsr(0x19c)
  148. thermal_status = []
  149. for core in range(cpu_count()):
  150. thermal_status_core = {}
  151. for key, value in thermal_status_bits.items():
  152. thermal_status_core[key] = int(get_value_for_bits(thermal_status_msr_value[core], value[0], value[1]))
  153. thermal_status.append(thermal_status_core)
  154. # reset log bits
  155. writemsr(0x19c, 0)
  156. return thermal_status
  157. def get_time_unit():
  158. # 0.000977 is the time unit of my CPU
  159. # TODO formula might be different for other CPUs
  160. return 1.0 / 2**readmsr(0x606, 16, 19, cpu=0)
  161. def get_power_unit():
  162. # 0.125 is the power unit of my CPU
  163. # TODO formula might be different for other CPUs
  164. return 1.0 / 2**readmsr(0x606, 0, 3, cpu=0)
  165. def get_critical_temp():
  166. # the critical temperature for my CPU is 100 'C
  167. return readmsr(0x1a2, 16, 23, cpu=0)
  168. def get_cur_pkg_power_limits():
  169. value = readmsr(0x610, 0, 55, flatten=True)
  170. return {
  171. 'PL1': get_value_for_bits(value, 0, 14),
  172. 'TW1': get_value_for_bits(value, 17, 23),
  173. 'PL2': get_value_for_bits(value, 32, 46),
  174. 'TW2': get_value_for_bits(value, 49, 55),
  175. }
  176. def calc_time_window_vars(t):
  177. time_unit = get_time_unit()
  178. for Y in range(2**5):
  179. for Z in range(2**2):
  180. if t <= (2**Y) * (1. + Z / 4.) * time_unit:
  181. return (Y, Z)
  182. raise ValueError('Unable to find a good combination!')
  183. def calc_undervolt_msr(plane, offset):
  184. """Return the value to be written in the MSR 150h for setting the given
  185. offset voltage (in mV) to the given voltage plane.
  186. """
  187. assert offset <= 0
  188. assert plane in VOLTAGE_PLANES
  189. offset = int(round(offset * 1.024))
  190. offset = 0xFFE00000 & ((offset & 0xFFF) << 21)
  191. return 0x8000001100000000 | (VOLTAGE_PLANES[plane] << 40) | offset
  192. def calc_undervolt_mv(msr_value):
  193. """Return the offset voltage (in mV) from the given raw MSR 150h value.
  194. """
  195. offset = (msr_value & 0xFFE00000) >> 21
  196. offset = offset if offset <= 0x400 else -(0x800 - offset)
  197. return int(round(offset / 1.024))
  198. def undervolt(config):
  199. for plane in VOLTAGE_PLANES:
  200. write_offset_mv = config.getfloat('UNDERVOLT', plane, fallback=0.)
  201. write_value = calc_undervolt_msr(plane, write_offset_mv)
  202. writemsr(0x150, write_value)
  203. if args.debug:
  204. write_value &= 0xFFFFFFFF
  205. writemsr(0x150, 0x8000001000000000 | (VOLTAGE_PLANES[plane] << 40))
  206. read_value = readmsr(0x150, flatten=True)
  207. read_offset_mv = calc_undervolt_mv(read_value)
  208. match = OK if write_value == read_value else ERR
  209. print('[D] Undervolt plane {:s} - write {:.0f} mV ({:#x}) - read {:.0f} mV ({:#x}) - match {}'.format(
  210. plane, write_offset_mv, write_value, read_offset_mv, read_value, match))
  211. def load_config():
  212. config = configparser.ConfigParser()
  213. config.read(args.config)
  214. # config values sanity check
  215. for power_source in ('AC', 'BATTERY'):
  216. for option in (
  217. 'Update_Rate_s',
  218. 'PL1_Tdp_W',
  219. 'PL1_Duration_s',
  220. 'PL2_Tdp_W',
  221. 'PL2_Duration_S',
  222. ):
  223. value = config.getfloat(power_source, option, fallback=None)
  224. if value is not None:
  225. value = config.set(power_source, option, str(max(0.1, value)))
  226. elif option == 'Update_Rate_s':
  227. print('[E] The mandatory "Update_Rate_s" parameter is missing.')
  228. sys.exit(1)
  229. trip_temp = config.getfloat(power_source, 'Trip_Temp_C', fallback=None)
  230. if trip_temp is not None:
  231. valid_trip_temp = min(TRIP_TEMP_RANGE[1], max(TRIP_TEMP_RANGE[0], trip_temp))
  232. if trip_temp != valid_trip_temp:
  233. config.set(power_source, 'Trip_Temp_C', str(valid_trip_temp))
  234. print('[!] Overriding invalid "Trip_Temp_C" value in "{:s}": {:.1f} -> {:.1f}'.format(
  235. power_source, trip_temp, valid_trip_temp))
  236. for plane in VOLTAGE_PLANES:
  237. value = config.getfloat('UNDERVOLT', plane)
  238. valid_value = min(0, value)
  239. if value != valid_value:
  240. config.set('UNDERVOLT', plane, str(valid_value))
  241. print('[!] Overriding invalid "UNDERVOLT" value in "{:s}" voltage plane: {:.0f} -> {:.0f}'.format(
  242. plane, value, valid_value))
  243. return config
  244. def calc_reg_values(platform_info, config):
  245. regs = defaultdict(dict)
  246. for power_source in ('AC', 'BATTERY'):
  247. if platform_info['feature_programmable_temperature_target'] != 1:
  248. print("[W] Setting temperature target is not supported by this CPU")
  249. else:
  250. # the critical temperature for my CPU is 100 'C
  251. critical_temp = get_critical_temp()
  252. # update the allowed temp range to keep at least 3 'C from the CPU critical temperature
  253. global TRIP_TEMP_RANGE
  254. TRIP_TEMP_RANGE[1] = min(TRIP_TEMP_RANGE[1], critical_temp - 3)
  255. Trip_Temp_C = config.getfloat(power_source, 'Trip_Temp_C', fallback=None)
  256. if Trip_Temp_C is not None:
  257. trip_offset = int(round(critical_temp - Trip_Temp_C))
  258. regs[power_source]['MSR_TEMPERATURE_TARGET'] = trip_offset << 24
  259. else:
  260. print('[I] {:s} trip temperature is disabled in config.'.format(power_source))
  261. power_unit = get_power_unit()
  262. PL1_Tdp_W = config.getfloat(power_source, 'PL1_Tdp_W', fallback=None)
  263. PL1_Duration_s = config.getfloat(power_source, 'PL1_Duration_s', fallback=None)
  264. PL2_Tdp_W = config.getfloat(power_source, 'PL2_Tdp_W', fallback=None)
  265. PL2_Duration_s = config.getfloat(power_source, 'PL2_Duration_s', fallback=None)
  266. if (PL1_Tdp_W, PL1_Duration_s, PL2_Tdp_W, PL2_Duration_s).count(None) < 4:
  267. cur_pkg_power_limits = get_cur_pkg_power_limits()
  268. if PL1_Tdp_W is None:
  269. PL1 = cur_pkg_power_limits['PL1']
  270. print('[I] {:s} PL1_Tdp_W disabled in config.'.format(power_source))
  271. else:
  272. PL1 = int(round(PL1_Tdp_W / power_unit))
  273. if PL1_Duration_s is None:
  274. TW1 = cur_pkg_power_limits['TW1']
  275. print('[I] {:s} PL1_Duration_s disabled in config.'.format(power_source))
  276. else:
  277. Y, Z = calc_time_window_vars(PL1_Duration_s)
  278. TW1 = Y | (Z << 5)
  279. if PL2_Tdp_W is None:
  280. PL2 = cur_pkg_power_limits['PL2']
  281. print('[I] {:s} PL2_Tdp_W disabled in config.'.format(power_source))
  282. else:
  283. PL2 = int(round(PL2_Tdp_W / power_unit))
  284. if PL2_Duration_s is None:
  285. TW2 = cur_pkg_power_limits['TW2']
  286. print('[I] {:s} PL2_Duration_s disabled in config.'.format(power_source))
  287. else:
  288. Y, Z = calc_time_window_vars(PL2_Duration_s)
  289. TW2 = Y | (Z << 5)
  290. regs[power_source]['MSR_PKG_POWER_LIMIT'] = PL1 | (1 << 15) | (TW1 << 17) | (PL2 << 32) | (1 << 47) | (
  291. TW2 << 49)
  292. else:
  293. print('[I] {:s} package power limits are disabled in config.'.format(power_source))
  294. # cTDP
  295. c_tdp_target_value = config.getint(power_source, 'cTDP', fallback=None)
  296. if c_tdp_target_value is not None:
  297. if platform_info['feature_programmable_tdp_limit'] != 1:
  298. print("[W] cTDP setting not supported by this CPU")
  299. elif platform_info['number_of_additional_tdp_profiles'] < c_tdp_target_value:
  300. print("[W] the configured cTDP profile is not supported by this CPU")
  301. else:
  302. valid_c_tdp_target_value = max(0, c_tdp_target_value)
  303. regs[power_source]['MSR_CONFIG_TDP_CONTROL'] = valid_c_tdp_target_value
  304. return regs
  305. def set_hwp(pref):
  306. # set HWP energy performance hints
  307. assert pref in ('performance', 'balance_performance', 'default', 'balance_power', 'power')
  308. CPUs = [
  309. '/sys/devices/system/cpu/cpu{:d}/cpufreq/energy_performance_preference'.format(x) for x in range(cpu_count())
  310. ]
  311. for i, c in enumerate(CPUs):
  312. with open(c, 'wb') as f:
  313. f.write(pref.encode())
  314. if args.debug:
  315. with open(c) as f:
  316. read_value = f.read().strip()
  317. match = OK if pref == read_value else ERR
  318. print('[D] HWP for cpu{:d} - write "{:s}" - read "{:s}" - match {}'.format(i, pref, read_value, match))
  319. def power_thread(config, regs, exit_event):
  320. try:
  321. mchbar_mmio = MMIO(0xfed159a0, 8)
  322. except MMIOError:
  323. print('[E] Unable to open /dev/mem. Try to disable Secure Boot.')
  324. sys.exit(1)
  325. while not exit_event.is_set():
  326. #print thermal status
  327. if args.debug:
  328. thermal_status = get_reset_thermal_status()
  329. for index, core_thermal_status in enumerate(thermal_status):
  330. for key, value in core_thermal_status.items():
  331. print('[D] core {} thermal status: {} = {}'.format(index, key.replace("_", " "), value))
  332. # switch back to sysfs polling
  333. if power['method'] == 'polling':
  334. power['source'] = 'BATTERY' if is_on_battery(config) else 'AC'
  335. # set temperature trip point
  336. if 'MSR_TEMPERATURE_TARGET' in regs[power['source']]:
  337. write_value = regs[power['source']]['MSR_TEMPERATURE_TARGET']
  338. writemsr(0x1a2, write_value)
  339. if args.debug:
  340. read_value = readmsr(0x1a2, 24, 29, flatten=True)
  341. match = OK if write_value >> 24 == read_value else ERR
  342. print('[D] TEMPERATURE_TARGET - write {:#x} - read {:#x} - match {}'.format(
  343. write_value >> 24, read_value, match))
  344. # set cTDP
  345. if 'MSR_CONFIG_TDP_CONTROL' in regs[power['source']]:
  346. write_value = regs[power['source']]['MSR_CONFIG_TDP_CONTROL']
  347. writemsr(0x64b, write_value)
  348. if args.debug:
  349. read_value = readmsr(0x64b, 0, 1, flatten=True)
  350. match = OK if write_value == read_value else ERR
  351. print('[D] CONFIG_TDP_CONTROL - write {:#x} - read {:#x} - match {}'.format(
  352. write_value, read_value, match))
  353. # set PL1/2 on MSR
  354. write_value = regs[power['source']]['MSR_PKG_POWER_LIMIT']
  355. writemsr(0x610, write_value)
  356. if args.debug:
  357. read_value = readmsr(0x610, 0, 55, flatten=True)
  358. match = OK if write_value == read_value else ERR
  359. print('[D] MSR PACKAGE_POWER_LIMIT - write {:#x} - read {:#x} - match {}'.format(
  360. write_value, read_value, match))
  361. # set MCHBAR register to the same PL1/2 values
  362. mchbar_mmio.write32(0, write_value & 0xffffffff)
  363. mchbar_mmio.write32(4, write_value >> 32)
  364. if args.debug:
  365. read_value = mchbar_mmio.read32(0) | (mchbar_mmio.read32(4) << 32)
  366. match = OK if write_value == read_value else ERR
  367. print('[D] MCHBAR PACKAGE_POWER_LIMIT - write {:#x} - read {:#x} - match {}'.format(
  368. write_value, read_value, match))
  369. wait_t = config.getfloat(power['source'], 'Update_Rate_s')
  370. enable_hwp_mode = config.getboolean('AC', 'HWP_Mode', fallback=False)
  371. if power['source'] == 'AC' and enable_hwp_mode:
  372. cpu_usage = cpu_usage_pct(exit_event, interval=wait_t)
  373. # set full performance mode only when load is greater than this threshold (~ at least 1 core full speed)
  374. performance_mode = cpu_usage > 100. / (cpu_count() * 1.25)
  375. # check again if we are on AC, since in the meantime we might have switched to BATTERY
  376. if not is_on_battery(config):
  377. set_hwp('performance' if performance_mode else 'balance_performance')
  378. else:
  379. exit_event.wait(wait_t)
  380. def main():
  381. global args
  382. if os.geteuid() != 0:
  383. print('[E] No root no party. Try again with sudo.')
  384. sys.exit(1)
  385. parser = argparse.ArgumentParser()
  386. parser.add_argument('--debug', action='store_true', help='add some debug info and additional checks')
  387. parser.add_argument('--config', default='/etc/lenovo_fix.conf', help='override default config file path')
  388. args = parser.parse_args()
  389. config = load_config()
  390. power['source'] = 'BATTERY' if is_on_battery(config) else 'AC'
  391. platform_info = get_cpu_platform_info()
  392. if args.debug:
  393. for key, value in platform_info.items():
  394. print('[D] cpu platform info: {} = {}'.format(key.replace("_", " "), value))
  395. regs = calc_reg_values(platform_info, config)
  396. if not config.getboolean('GENERAL', 'Enabled'):
  397. return
  398. exit_event = Event()
  399. thread = Thread(target=power_thread, args=(config, regs, exit_event))
  400. thread.daemon = True
  401. thread.start()
  402. undervolt(config)
  403. # handle dbus events for applying undervolt on resume from sleep/hybernate
  404. def handle_sleep_callback(sleeping):
  405. if not sleeping:
  406. undervolt(config)
  407. def handle_ac_callback(*args):
  408. try:
  409. power['source'] = 'BATTERY' if args[1]['Online'] == 0 else 'AC'
  410. power['method'] = 'dbus'
  411. except:
  412. power['method'] = 'polling'
  413. DBusGMainLoop(set_as_default=True)
  414. bus = dbus.SystemBus()
  415. # add dbus receiver only if undervolt is enabled in config
  416. if any(config.getfloat('UNDERVOLT', plane) != 0 for plane in VOLTAGE_PLANES):
  417. bus.add_signal_receiver(handle_sleep_callback, 'PrepareForSleep', 'org.freedesktop.login1.Manager',
  418. 'org.freedesktop.login1')
  419. bus.add_signal_receiver(
  420. handle_ac_callback,
  421. signal_name="PropertiesChanged",
  422. dbus_interface="org.freedesktop.DBus.Properties",
  423. path="/org/freedesktop/UPower/devices/line_power_AC")
  424. try:
  425. loop = GLib.MainLoop()
  426. loop.run()
  427. except (KeyboardInterrupt, SystemExit):
  428. pass
  429. exit_event.set()
  430. loop.quit()
  431. thread.join(timeout=1)
  432. if __name__ == '__main__':
  433. main()