lenovo_fix.py 20 KB

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