lenovo_fix.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. #!/usr/bin/env python3
  2. from __future__ import print_function
  3. import argparse
  4. import configparser
  5. import glob
  6. import gzip
  7. import os
  8. import re
  9. import struct
  10. import subprocess
  11. import sys
  12. from collections import defaultdict
  13. from datetime import datetime
  14. from errno import EACCES, EPERM
  15. from multiprocessing import cpu_count
  16. from platform import uname
  17. from threading import Event, Thread
  18. from time import time
  19. import dbus
  20. from dbus.mainloop.glib import DBusGMainLoop
  21. from gi.repository import GLib
  22. from mmio import MMIO, MMIOError
  23. DEFAULT_SYSFS_POWER_PATH = '/sys/class/power_supply/AC*/online'
  24. VOLTAGE_PLANES = {'CORE': 0, 'GPU': 1, 'CACHE': 2, 'UNCORE': 3, 'ANALOGIO': 4}
  25. CURRENT_PLANES = {'CORE': 0, 'GPU': 1, 'CACHE': 2}
  26. TRIP_TEMP_RANGE = [40, 97]
  27. UNDERVOLT_KEYS = ('UNDERVOLT', 'UNDERVOLT.AC', 'UNDERVOLT.BATTERY')
  28. ICCMAX_KEYS = ('ICCMAX', 'ICCMAX.AC', 'ICCMAX.BATTERY')
  29. power = {'source': None, 'method': 'polling'}
  30. HWP_VALUE = 0x20
  31. HWP_INTERVAL = 60
  32. platform_info_bits = {
  33. 'maximum_non_turbo_ratio': [8, 15],
  34. 'maximum_efficiency_ratio': [40, 47],
  35. 'minimum_operating_ratio': [48, 55],
  36. 'feature_ppin_cap': [23, 23],
  37. 'feature_programmable_turbo_ratio': [28, 28],
  38. 'feature_programmable_tdp_limit': [29, 29],
  39. 'number_of_additional_tdp_profiles': [33, 34],
  40. 'feature_programmable_temperature_target': [30, 30],
  41. 'feature_low_power_mode': [32, 32],
  42. }
  43. thermal_status_bits = {
  44. 'thermal_limit_status': [0, 0],
  45. 'thermal_limit_log': [1, 1],
  46. 'prochot_or_forcepr_status': [2, 2],
  47. 'prochot_or_forcepr_log': [3, 3],
  48. 'crit_temp_status': [4, 4],
  49. 'crit_temp_log': [5, 5],
  50. 'thermal_threshold1_status': [6, 6],
  51. 'thermal_threshold1_log': [7, 7],
  52. 'thermal_threshold2_status': [8, 8],
  53. 'thermal_threshold2_log': [9, 9],
  54. 'power_limit_status': [10, 10],
  55. 'power_limit_log': [11, 11],
  56. 'current_limit_status': [12, 12],
  57. 'current_limit_log': [13, 13],
  58. 'cross_domain_limit_status': [14, 14],
  59. 'cross_domain_limit_log': [15, 15],
  60. 'cpu_temp': [16, 22],
  61. 'temp_resolution': [27, 30],
  62. 'reading_valid': [31, 31],
  63. }
  64. supported_cpus = {
  65. 'Haswell': (0x3C, 0x3F, 0x45, 0x46),
  66. 'Broadwell': (0x3D, 0x47, 0x4F, 0x56),
  67. 'Skylake': (0x4E, 0x55),
  68. 'Skylake-S': (0x5E,),
  69. 'Ice Lake': (0x7E,),
  70. 'Kaby Lake (R)': (0x8E, 0x9E),
  71. 'Coffee Lake': (0x9E,),
  72. 'Cannon Lake': (0x66,),
  73. 'Comet Lake': (0xA6,),
  74. }
  75. class bcolors:
  76. YELLOW = '\033[93m'
  77. GREEN = '\033[92m'
  78. RED = '\033[91m'
  79. RESET = '\033[0m'
  80. BOLD = '\033[1m'
  81. OK = bcolors.GREEN + bcolors.BOLD + 'OK' + bcolors.RESET
  82. ERR = bcolors.RED + bcolors.BOLD + 'ERR' + bcolors.RESET
  83. LIM = bcolors.YELLOW + bcolors.BOLD + 'LIM' + bcolors.RESET
  84. log_history = set()
  85. def log(msg, oneshot=False, end='\n'):
  86. outfile = args.log if args.log else sys.stdout
  87. if msg.strip() not in log_history or oneshot is False:
  88. tstamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
  89. full_msg = '{:s}: {:s}'.format(tstamp, msg) if args.log else msg
  90. print(full_msg, file=outfile, end=end)
  91. log_history.add(msg.strip())
  92. def fatal(msg, code=1, end='\n'):
  93. outfile = args.log if args.log else sys.stderr
  94. tstamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
  95. full_msg = '{:s}: [E] {:s}'.format(tstamp, msg) if args.log else '[E] {:s}'.format(msg)
  96. print(full_msg, file=outfile, end=end)
  97. sys.exit(code)
  98. def warning(msg, oneshot=True, end='\n'):
  99. outfile = args.log if args.log else sys.stderr
  100. if msg.strip() not in log_history or oneshot is False:
  101. tstamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
  102. full_msg = '{:s}: [W] {:s}'.format(tstamp, msg) if args.log else '[W] {:s}'.format(msg)
  103. print(full_msg, file=outfile, end=end)
  104. log_history.add(msg.strip())
  105. def writemsr(msr, val):
  106. msr_list = ['/dev/cpu/{:d}/msr'.format(x) for x in range(cpu_count())]
  107. if not os.path.exists(msr_list[0]):
  108. try:
  109. subprocess.check_call(('modprobe', 'msr'))
  110. except subprocess.CalledProcessError:
  111. fatal('Unable to load the msr module.')
  112. try:
  113. for addr in msr_list:
  114. f = os.open(addr, os.O_WRONLY)
  115. os.lseek(f, msr, os.SEEK_SET)
  116. os.write(f, struct.pack('Q', val))
  117. os.close(f)
  118. except (IOError, OSError) as e:
  119. if e.errno == EPERM or e.errno == EACCES:
  120. fatal(
  121. 'Unable to write to MSR. Try to disable Secure Boot '
  122. 'and check if your kernel does not restrict access to MSR.'
  123. )
  124. else:
  125. raise e
  126. # returns the value between from_bit and to_bit as unsigned long
  127. def readmsr(msr, from_bit=0, to_bit=63, cpu=None, flatten=False):
  128. assert cpu is None or cpu in range(cpu_count())
  129. if from_bit > to_bit:
  130. fatal('Wrong readmsr bit params')
  131. msr_list = ['/dev/cpu/{:d}/msr'.format(x) for x in range(cpu_count())]
  132. if not os.path.exists(msr_list[0]):
  133. try:
  134. subprocess.check_call(('modprobe', 'msr'))
  135. except subprocess.CalledProcessError:
  136. fatal('Unable to load the msr module.')
  137. try:
  138. output = []
  139. for addr in msr_list:
  140. f = os.open(addr, os.O_RDONLY)
  141. os.lseek(f, msr, os.SEEK_SET)
  142. val = struct.unpack('Q', os.read(f, 8))[0]
  143. os.close(f)
  144. output.append(get_value_for_bits(val, from_bit, to_bit))
  145. if flatten:
  146. return output[0] if len(set(output)) == 1 else output
  147. return output[cpu] if cpu is not None else output
  148. except (IOError, OSError) as e:
  149. if e.errno == EPERM or e.errno == EACCES:
  150. fatal('Unable to read from MSR. Try to disable Secure Boot.')
  151. else:
  152. raise e
  153. def get_value_for_bits(val, from_bit=0, to_bit=63):
  154. mask = sum(2 ** x for x in range(from_bit, to_bit + 1))
  155. return (val & mask) >> from_bit
  156. def is_on_battery(config):
  157. try:
  158. for path in glob.glob(config.get('GENERAL', 'Sysfs_Power_Path', fallback=DEFAULT_SYSFS_POWER_PATH)):
  159. with open(path) as f:
  160. return not bool(int(f.read()))
  161. raise
  162. except:
  163. warning('No valid Sysfs_Power_Path found! Trying upower method #1')
  164. try:
  165. out = subprocess.check_output(('upower', '-i', '/org/freedesktop/UPower/devices/line_power_AC'))
  166. res = re.search(rb'online:\s+(yes|no)', out).group(1).decode().strip()
  167. if res == 'yes':
  168. return False
  169. elif res == 'no':
  170. return True
  171. raise
  172. except:
  173. warning('Trying upower method #2')
  174. try:
  175. out = subprocess.check_output(('upower', '-i', '/org/freedesktop/UPower/devices/battery_BAT0'))
  176. res = re.search(rb'state:\s+(.+)', out).group(1).decode().strip()
  177. if res == 'discharging':
  178. return True
  179. elif res in ('fully-charged', 'charging'):
  180. return False
  181. except:
  182. pass
  183. warning('No valid power detection methods found. Assuming that the system is running on battery power.')
  184. return True
  185. def get_cpu_platform_info():
  186. features_msr_value = readmsr(0xCE, cpu=0)
  187. cpu_platform_info = {}
  188. for key, value in platform_info_bits.items():
  189. cpu_platform_info[key] = int(get_value_for_bits(features_msr_value, value[0], value[1]))
  190. return cpu_platform_info
  191. def get_reset_thermal_status():
  192. # read thermal status
  193. thermal_status_msr_value = readmsr(0x19C)
  194. thermal_status = []
  195. for core in range(cpu_count()):
  196. thermal_status_core = {}
  197. for key, value in thermal_status_bits.items():
  198. thermal_status_core[key] = int(get_value_for_bits(thermal_status_msr_value[core], value[0], value[1]))
  199. thermal_status.append(thermal_status_core)
  200. # reset log bits
  201. writemsr(0x19C, 0)
  202. return thermal_status
  203. def get_time_unit():
  204. # 0.000977 is the time unit of my CPU
  205. # TODO formula might be different for other CPUs
  206. return 1.0 / 2 ** readmsr(0x606, 16, 19, cpu=0)
  207. def get_power_unit():
  208. # 0.125 is the power unit of my CPU
  209. # TODO formula might be different for other CPUs
  210. return 1.0 / 2 ** readmsr(0x606, 0, 3, cpu=0)
  211. def get_critical_temp():
  212. # the critical temperature for my CPU is 100 'C
  213. return readmsr(0x1A2, 16, 23, cpu=0)
  214. def get_cur_pkg_power_limits():
  215. value = readmsr(0x610, 0, 55, flatten=True)
  216. return {
  217. 'PL1': get_value_for_bits(value, 0, 14),
  218. 'TW1': get_value_for_bits(value, 17, 23),
  219. 'PL2': get_value_for_bits(value, 32, 46),
  220. 'TW2': get_value_for_bits(value, 49, 55),
  221. }
  222. def calc_time_window_vars(t):
  223. time_unit = get_time_unit()
  224. for Y in range(2 ** 5):
  225. for Z in range(2 ** 2):
  226. if t <= (2 ** Y) * (1.0 + Z / 4.0) * time_unit:
  227. return (Y, Z)
  228. raise ValueError('Unable to find a good combination!')
  229. def calc_undervolt_msr(plane, offset):
  230. """Return the value to be written in the MSR 150h for setting the given
  231. offset voltage (in mV) to the given voltage plane.
  232. """
  233. assert offset <= 0
  234. assert plane in VOLTAGE_PLANES
  235. offset = int(round(offset * 1.024))
  236. offset = 0xFFE00000 & ((offset & 0xFFF) << 21)
  237. return 0x8000001100000000 | (VOLTAGE_PLANES[plane] << 40) | offset
  238. def calc_undervolt_mv(msr_value):
  239. """Return the offset voltage (in mV) from the given raw MSR 150h value.
  240. """
  241. offset = (msr_value & 0xFFE00000) >> 21
  242. offset = offset if offset <= 0x400 else -(0x800 - offset)
  243. return int(round(offset / 1.024))
  244. def get_undervolt(plane=None, convert=False):
  245. planes = [plane] if plane in VOLTAGE_PLANES else VOLTAGE_PLANES
  246. out = {}
  247. for plane in planes:
  248. writemsr(0x150, 0x8000001000000000 | (VOLTAGE_PLANES[plane] << 40))
  249. read_value = readmsr(0x150, flatten=True) & 0xFFFFFFFF
  250. out[plane] = calc_undervolt_mv(read_value) if convert else read_value
  251. return out
  252. def undervolt(config):
  253. for plane in VOLTAGE_PLANES:
  254. write_offset_mv = config.getfloat(
  255. 'UNDERVOLT.{:s}'.format(power['source']), plane, fallback=config.getfloat('UNDERVOLT', plane, fallback=0.0)
  256. )
  257. write_value = calc_undervolt_msr(plane, write_offset_mv)
  258. writemsr(0x150, write_value)
  259. if args.debug:
  260. write_value &= 0xFFFFFFFF
  261. read_value = get_undervolt(plane)[plane]
  262. read_offset_mv = calc_undervolt_mv(read_value)
  263. match = OK if write_value == read_value else ERR
  264. log(
  265. '[D] Undervolt plane {:s} - write {:.0f} mV ({:#x}) - read {:.0f} mV ({:#x}) - match {}'.format(
  266. plane, write_offset_mv, write_value, read_offset_mv, read_value, match
  267. )
  268. )
  269. def calc_icc_max_msr(plane, current):
  270. """Return the value to be written in the MSR 150h for setting the given
  271. IccMax (in A) to the given current plane.
  272. """
  273. assert 0 < current <= 0x3FF
  274. assert plane in CURRENT_PLANES
  275. current = int(round(current * 4))
  276. return 0x8000001700000000 | (CURRENT_PLANES[plane] << 40) | current
  277. def calc_icc_max_amp(msr_value):
  278. """Return the max current (in A) from the given raw MSR 150h value.
  279. """
  280. return (msr_value & 0x3FF) / 4.0
  281. def get_icc_max(plane=None, convert=False):
  282. planes = [plane] if plane in CURRENT_PLANES else CURRENT_PLANES
  283. out = {}
  284. for plane in planes:
  285. writemsr(0x150, 0x8000001600000000 | (CURRENT_PLANES[plane] << 40))
  286. read_value = readmsr(0x150, flatten=True) & 0x3FF
  287. out[plane] = calc_icc_max_amp(read_value) if convert else read_value
  288. return out
  289. def set_icc_max(config):
  290. for plane in CURRENT_PLANES:
  291. try:
  292. write_current_amp = config.getfloat(
  293. 'ICCMAX.{:s}'.format(power['source']), plane, fallback=config.getfloat('ICCMAX', plane, fallback=-1.0)
  294. )
  295. if write_current_amp > 0:
  296. write_value = calc_icc_max_msr(plane, write_current_amp)
  297. writemsr(0x150, write_value)
  298. if args.debug:
  299. write_value &= 0x3FF
  300. read_value = get_icc_max(plane)[plane]
  301. read_current_A = calc_icc_max_amp(read_value)
  302. match = OK if write_value == read_value else ERR
  303. log(
  304. '[D] IccMax plane {:s} - write {:.2f} A ({:#x}) - read {:.2f} A ({:#x}) - match {}'.format(
  305. plane, write_current_amp, write_value, read_current_A, read_value, match
  306. )
  307. )
  308. except (configparser.NoSectionError, configparser.NoOptionError):
  309. pass
  310. def load_config():
  311. config = configparser.ConfigParser()
  312. config.read(args.config)
  313. # config values sanity check
  314. for power_source in ('AC', 'BATTERY'):
  315. for option in ('Update_Rate_s', 'PL1_Tdp_W', 'PL1_Duration_s', 'PL2_Tdp_W', 'PL2_Duration_S'):
  316. value = config.getfloat(power_source, option, fallback=None)
  317. if value is not None:
  318. value = config.set(power_source, option, str(max(0.001, value)))
  319. elif option == 'Update_Rate_s':
  320. fatal('The mandatory "Update_Rate_s" parameter is missing.')
  321. trip_temp = config.getfloat(power_source, 'Trip_Temp_C', fallback=None)
  322. if trip_temp is not None:
  323. valid_trip_temp = min(TRIP_TEMP_RANGE[1], max(TRIP_TEMP_RANGE[0], trip_temp))
  324. if trip_temp != valid_trip_temp:
  325. config.set(power_source, 'Trip_Temp_C', str(valid_trip_temp))
  326. log(
  327. '[!] Overriding invalid "Trip_Temp_C" value in "{:s}": {:.1f} -> {:.1f}'.format(
  328. power_source, trip_temp, valid_trip_temp
  329. )
  330. )
  331. # fix any invalid value (ie. > 0) in the undervolt settings
  332. for key in UNDERVOLT_KEYS:
  333. for plane in VOLTAGE_PLANES:
  334. if key in config:
  335. value = config.getfloat(key, plane)
  336. valid_value = min(0, value)
  337. if value != valid_value:
  338. config.set(key, plane, str(valid_value))
  339. log(
  340. '[!] Overriding invalid "{:s}" value in "{:s}" voltage plane: {:.0f} -> {:.0f}'.format(
  341. key, plane, value, valid_value
  342. )
  343. )
  344. # handle the case where only one of UNDERVOLT.AC, UNDERVOLT.BATTERY keys exists
  345. # by forcing the other key to all zeros (ie. no undervolt)
  346. if any(key in config for key in UNDERVOLT_KEYS[1:]):
  347. for key in UNDERVOLT_KEYS[1:]:
  348. if key not in config:
  349. config.add_section(key)
  350. for plane in VOLTAGE_PLANES:
  351. value = config.getfloat(key, plane, fallback=0.0)
  352. config.set(key, plane, str(value))
  353. # Check for CORE/CACHE values mismatch
  354. for key in UNDERVOLT_KEYS:
  355. if key in config:
  356. if config.getfloat(key, 'CORE', fallback=0) != config.getfloat(key, 'CACHE', fallback=0):
  357. warning('On Skylake and newer CPUs CORE and CACHE values should match!')
  358. break
  359. iccmax_enabled = False
  360. # check for invalid values (ie. <= 0 or > 0x3FF) in the IccMax settings
  361. for key in ICCMAX_KEYS:
  362. for plane in CURRENT_PLANES:
  363. if key in config:
  364. try:
  365. value = config.getfloat(key, plane)
  366. if value <= 0 or value >= 0x3FF:
  367. raise ValueError
  368. iccmax_enabled = True
  369. except ValueError:
  370. warning('Invalid value for {:s} in {:s}'.format(plane, key), oneshot=False)
  371. config.remove_option(key, plane)
  372. except configparser.NoOptionError:
  373. pass
  374. if iccmax_enabled:
  375. warning('Warning! Raising IccMax above design limits can damage your system!')
  376. return config
  377. def calc_reg_values(platform_info, config):
  378. regs = defaultdict(dict)
  379. for power_source in ('AC', 'BATTERY'):
  380. if platform_info['feature_programmable_temperature_target'] != 1:
  381. warning("Setting temperature target is not supported by this CPU")
  382. else:
  383. # the critical temperature for my CPU is 100 'C
  384. critical_temp = get_critical_temp()
  385. # update the allowed temp range to keep at least 3 'C from the CPU critical temperature
  386. global TRIP_TEMP_RANGE
  387. TRIP_TEMP_RANGE[1] = min(TRIP_TEMP_RANGE[1], critical_temp - 3)
  388. Trip_Temp_C = config.getfloat(power_source, 'Trip_Temp_C', fallback=None)
  389. if Trip_Temp_C is not None:
  390. trip_offset = int(round(critical_temp - Trip_Temp_C))
  391. regs[power_source]['MSR_TEMPERATURE_TARGET'] = trip_offset << 24
  392. else:
  393. log('[I] {:s} trip temperature is disabled in config.'.format(power_source))
  394. power_unit = get_power_unit()
  395. PL1_Tdp_W = config.getfloat(power_source, 'PL1_Tdp_W', fallback=None)
  396. PL1_Duration_s = config.getfloat(power_source, 'PL1_Duration_s', fallback=None)
  397. PL2_Tdp_W = config.getfloat(power_source, 'PL2_Tdp_W', fallback=None)
  398. PL2_Duration_s = config.getfloat(power_source, 'PL2_Duration_s', fallback=None)
  399. if (PL1_Tdp_W, PL1_Duration_s, PL2_Tdp_W, PL2_Duration_s).count(None) < 4:
  400. cur_pkg_power_limits = get_cur_pkg_power_limits()
  401. if PL1_Tdp_W is None:
  402. PL1 = cur_pkg_power_limits['PL1']
  403. log('[I] {:s} PL1_Tdp_W disabled in config.'.format(power_source))
  404. else:
  405. PL1 = int(round(PL1_Tdp_W / power_unit))
  406. if PL1_Duration_s is None:
  407. TW1 = cur_pkg_power_limits['TW1']
  408. log('[I] {:s} PL1_Duration_s disabled in config.'.format(power_source))
  409. else:
  410. Y, Z = calc_time_window_vars(PL1_Duration_s)
  411. TW1 = Y | (Z << 5)
  412. if PL2_Tdp_W is None:
  413. PL2 = cur_pkg_power_limits['PL2']
  414. log('[I] {:s} PL2_Tdp_W disabled in config.'.format(power_source))
  415. else:
  416. PL2 = int(round(PL2_Tdp_W / power_unit))
  417. if PL2_Duration_s is None:
  418. TW2 = cur_pkg_power_limits['TW2']
  419. log('[I] {:s} PL2_Duration_s disabled in config.'.format(power_source))
  420. else:
  421. Y, Z = calc_time_window_vars(PL2_Duration_s)
  422. TW2 = Y | (Z << 5)
  423. regs[power_source]['MSR_PKG_POWER_LIMIT'] = (
  424. PL1 | (1 << 15) | (1 << 16) | (TW1 << 17) | (PL2 << 32) | (1 << 47) | (TW2 << 49)
  425. )
  426. else:
  427. log('[I] {:s} package power limits are disabled in config.'.format(power_source))
  428. # cTDP
  429. c_tdp_target_value = config.getint(power_source, 'cTDP', fallback=None)
  430. if c_tdp_target_value is not None:
  431. if platform_info['feature_programmable_tdp_limit'] != 1:
  432. log("[W] cTDP setting not supported by this CPU")
  433. elif platform_info['number_of_additional_tdp_profiles'] < c_tdp_target_value:
  434. log("[W] the configured cTDP profile is not supported by this CPU")
  435. else:
  436. valid_c_tdp_target_value = max(0, c_tdp_target_value)
  437. regs[power_source]['MSR_CONFIG_TDP_CONTROL'] = valid_c_tdp_target_value
  438. return regs
  439. def set_hwp():
  440. # set HWP energy performance preference
  441. cur_val = readmsr(0x774, cpu=0)
  442. new_val = (cur_val & 0xFFFFFFFF00FFFFFF) | (HWP_VALUE << 24)
  443. writemsr(0x774, new_val)
  444. if args.debug:
  445. read_value = readmsr(0x774, from_bit=24, to_bit=31)[0]
  446. match = OK if HWP_VALUE == read_value else ERR
  447. log('[D] HWP - write "{:#02x}" - read "{:#02x}" - match {}'.format(HWP_VALUE, read_value, match))
  448. def set_disable_bdprochot():
  449. # Disable BDPROCHOT
  450. cur_val = readmsr(0x1FC, flatten=True)
  451. new_val = cur_val & 0xFFFFFFFFFFFFFFFE
  452. writemsr(0x1FC, new_val)
  453. if args.debug:
  454. read_value = readmsr(0x1FC, from_bit=31, to_bit=31)[0]
  455. match = OK if ~read_value else ERR
  456. log('[D] BDPROCHOT - write "{:#02x}" - read "{:#02x}" - match {}'.format(0, read_value, match))
  457. def power_thread(config, regs, exit_event):
  458. try:
  459. mchbar_mmio = MMIO(0xFED159A0, 8)
  460. except MMIOError:
  461. warning('Unable to open /dev/mem. TDP override might not work correctly.')
  462. warning('Try to disable Secure Boot and/or enable CONFIG_DEVMEM in kernel config.')
  463. mchbar_mmio = None
  464. next_hwp_write = 0
  465. while not exit_event.is_set():
  466. # log thermal status
  467. if args.debug:
  468. thermal_status = get_reset_thermal_status()
  469. for index, core_thermal_status in enumerate(thermal_status):
  470. for key, value in core_thermal_status.items():
  471. log('[D] core {} thermal status: {} = {}'.format(index, key.replace("_", " "), value))
  472. # switch back to sysfs polling
  473. if power['method'] == 'polling':
  474. power['source'] = 'BATTERY' if is_on_battery(config) else 'AC'
  475. # set temperature trip point
  476. if 'MSR_TEMPERATURE_TARGET' in regs[power['source']]:
  477. write_value = regs[power['source']]['MSR_TEMPERATURE_TARGET']
  478. writemsr(0x1A2, write_value)
  479. if args.debug:
  480. read_value = readmsr(0x1A2, 24, 29, flatten=True)
  481. match = OK if write_value >> 24 == read_value else ERR
  482. log(
  483. '[D] TEMPERATURE_TARGET - write {:#x} - read {:#x} - match {}'.format(
  484. write_value >> 24, read_value, match
  485. )
  486. )
  487. # set cTDP
  488. if 'MSR_CONFIG_TDP_CONTROL' in regs[power['source']]:
  489. write_value = regs[power['source']]['MSR_CONFIG_TDP_CONTROL']
  490. writemsr(0x64B, write_value)
  491. if args.debug:
  492. read_value = readmsr(0x64B, 0, 1, flatten=True)
  493. match = OK if write_value == read_value else ERR
  494. log(
  495. '[D] CONFIG_TDP_CONTROL - write {:#x} - read {:#x} - match {}'.format(
  496. write_value, read_value, match
  497. )
  498. )
  499. # set PL1/2 on MSR
  500. write_value = regs[power['source']]['MSR_PKG_POWER_LIMIT']
  501. writemsr(0x610, write_value)
  502. if args.debug:
  503. read_value = readmsr(0x610, 0, 55, flatten=True)
  504. match = OK if write_value == read_value else ERR
  505. log(
  506. '[D] MSR PACKAGE_POWER_LIMIT - write {:#x} - read {:#x} - match {}'.format(
  507. write_value, read_value, match
  508. )
  509. )
  510. if mchbar_mmio is not None:
  511. # set MCHBAR register to the same PL1/2 values
  512. mchbar_mmio.write32(0, write_value & 0xFFFFFFFF)
  513. mchbar_mmio.write32(4, write_value >> 32)
  514. if args.debug:
  515. read_value = mchbar_mmio.read32(0) | (mchbar_mmio.read32(4) << 32)
  516. match = OK if write_value == read_value else ERR
  517. log(
  518. '[D] MCHBAR PACKAGE_POWER_LIMIT - write {:#x} - read {:#x} - match {}'.format(
  519. write_value, read_value, match
  520. )
  521. )
  522. # Disable BDPROCHOT
  523. disable_bdprochot = config.getboolean(power['source'], 'Disable_BDPROCHOT', fallback=None)
  524. if disable_bdprochot is not None:
  525. set_disable_bdprochot()
  526. wait_t = config.getfloat(power['source'], 'Update_Rate_s')
  527. enable_hwp_mode = config.getboolean('AC', 'HWP_Mode', fallback=False)
  528. # set HWP less frequently. Just to be safe since (e.g.) TLP might reset this value
  529. if (
  530. enable_hwp_mode
  531. and next_hwp_write <= time()
  532. and (
  533. (power['method'] == 'dbus' and power['source'] == 'AC')
  534. or (power['method'] == 'polling' and not is_on_battery(config))
  535. )
  536. ):
  537. set_hwp()
  538. next_hwp_write = time() + HWP_INTERVAL
  539. else:
  540. exit_event.wait(wait_t)
  541. def check_kernel():
  542. if os.geteuid() != 0:
  543. fatal('No root no party. Try again with sudo.')
  544. kernel_config = None
  545. try:
  546. with open(os.path.join('/boot', 'config-{:s}'.format(uname()[2]))) as f:
  547. kernel_config = f.read()
  548. except IOError:
  549. config_gz_path = os.path.join('/proc', 'config.gz')
  550. try:
  551. if not os.path.isfile(config_gz_path):
  552. subprocess.check_call(('modprobe', 'configs'))
  553. with gzip.open(config_gz_path) as f:
  554. kernel_config = f.read().decode()
  555. except (subprocess.CalledProcessError, IOError):
  556. pass
  557. if kernel_config is None:
  558. log('[W] Unable to obtain and validate kernel config.')
  559. return
  560. elif not re.search('CONFIG_DEVMEM=y', kernel_config):
  561. warning('Bad kernel config: you need CONFIG_DEVMEM=y.')
  562. if not re.search('CONFIG_X86_MSR=(y|m)', kernel_config):
  563. fatal('Bad kernel config: you need CONFIG_X86_MSR builtin or as module.')
  564. def check_cpu():
  565. try:
  566. with open('/proc/cpuinfo') as f:
  567. cpuinfo = {}
  568. for row in f.readlines():
  569. try:
  570. key, value = map(lambda x: x.strip(), row.split(':'))
  571. if key == 'processor' and value == '1':
  572. break
  573. try:
  574. cpuinfo[key] = int(value, 0)
  575. except ValueError:
  576. cpuinfo[key] = value
  577. except ValueError:
  578. pass
  579. if cpuinfo['vendor_id'] != 'GenuineIntel':
  580. fatal('This tool is designed for Intel CPUs only.')
  581. cpu_model = None
  582. for model in supported_cpus:
  583. if cpuinfo['model'] in supported_cpus[model]:
  584. cpu_model = model
  585. break
  586. if cpuinfo['cpu family'] != 6 or cpu_model is None:
  587. fatal('Your CPU model is not supported.')
  588. log('[I] Detected CPU architecture: Intel {:s}'.format(cpu_model))
  589. except:
  590. fatal('Unable to identify CPU model.')
  591. def monitor(exit_event, wait):
  592. IA32_THERM_STATUS = 0x19C
  593. IA32_PERF_STATUS = 0x198
  594. MSR_RAPL_POWER_UNIT = 0x606
  595. MSR_INTEL_PKG_ENERGY_STATUS = 0x611
  596. MSR_PP1_ENERGY_STATUS = 0x641
  597. MSR_DRAM_ENERGY_STATUS = 0x619
  598. wait = max(0.1, wait)
  599. rapl_power_unit = 0.5 ** readmsr(MSR_RAPL_POWER_UNIT, from_bit=8, to_bit=12, cpu=0)
  600. power_plane_msr = {
  601. 'Package': MSR_INTEL_PKG_ENERGY_STATUS,
  602. 'Graphics': MSR_PP1_ENERGY_STATUS,
  603. 'DRAM': MSR_DRAM_ENERGY_STATUS,
  604. }
  605. prev_energy = {
  606. 'Package': (readmsr(MSR_INTEL_PKG_ENERGY_STATUS, cpu=0) * rapl_power_unit, time()),
  607. 'Graphics': (readmsr(MSR_PP1_ENERGY_STATUS, cpu=0) * rapl_power_unit, time()),
  608. 'DRAM': (readmsr(MSR_DRAM_ENERGY_STATUS, cpu=0) * rapl_power_unit, time()),
  609. }
  610. undervolt_values = get_undervolt(convert=True)
  611. undervolt_output = ' | '.join('{:s}: {:.2f} mV'.format(plane, undervolt_values[plane]) for plane in VOLTAGE_PLANES)
  612. log('[D] Undervolt offsets: {:s}'.format(undervolt_output))
  613. iccmax_values = get_icc_max(convert=True)
  614. iccmax_output = ' | '.join('{:s}: {:.2f} A'.format(plane, iccmax_values[plane]) for plane in CURRENT_PLANES)
  615. log('[D] IccMax: {:s}'.format(iccmax_output))
  616. log('[D] Realtime monitoring of throttling causes:\n')
  617. while not exit_event.is_set():
  618. value = readmsr(IA32_THERM_STATUS, from_bit=0, to_bit=15, cpu=0)
  619. offsets = {'Thermal': 0, 'Power': 10, 'Current': 12, 'Cross-domain (e.g. GPU)': 14}
  620. output = ('{:s}: {:s}'.format(cause, LIM if bool((value >> offsets[cause]) & 1) else OK) for cause in offsets)
  621. # ugly code, just testing...
  622. vcore = readmsr(IA32_PERF_STATUS, from_bit=32, to_bit=47, cpu=0) / (2.0 ** 13) * 1000
  623. stats2 = {'VCore': '{:.0f} mV'.format(vcore)}
  624. for power_plane in ('Package', 'Graphics', 'DRAM'):
  625. energy_j = readmsr(power_plane_msr[power_plane], cpu=0) * rapl_power_unit
  626. now = time()
  627. prev_energy[power_plane], energy_w = (
  628. (energy_j, now),
  629. (energy_j - prev_energy[power_plane][0]) / (now - prev_energy[power_plane][1]),
  630. )
  631. stats2[power_plane] = '{:.1f} W'.format(energy_w)
  632. output2 = ('{:s}: {:s}'.format(label, stats2[label]) for label in stats2)
  633. terminator = '\n' if args.log else '\r'
  634. log(
  635. '[{}] {} || {}{}'.format(power['source'], ' - '.join(output), ' - '.join(output2), ' ' * 10),
  636. end=terminator,
  637. )
  638. exit_event.wait(wait)
  639. def main():
  640. global args
  641. parser = argparse.ArgumentParser()
  642. exclusive_group = parser.add_mutually_exclusive_group()
  643. exclusive_group.add_argument('--debug', action='store_true', help='add some debug info and additional checks')
  644. exclusive_group.add_argument(
  645. '--monitor',
  646. metavar='update_rate',
  647. const=1.0,
  648. type=float,
  649. nargs='?',
  650. help='realtime monitoring of throttling causes (default 1s)',
  651. )
  652. parser.add_argument('--config', default='/etc/lenovo_fix.conf', help='override default config file path')
  653. parser.add_argument('--force', action='store_true', help='bypass compatibility checks (EXPERTS only)')
  654. parser.add_argument('--log', metavar='/path/to/file', help='log to file instead of stdout')
  655. args = parser.parse_args()
  656. if args.log:
  657. try:
  658. args.log = open(args.log, 'w')
  659. except:
  660. args.log = None
  661. fatal('Unable to write to the log file!')
  662. if not args.force:
  663. check_kernel()
  664. check_cpu()
  665. log('[I] Loading config file.')
  666. config = load_config()
  667. power['source'] = 'BATTERY' if is_on_battery(config) else 'AC'
  668. platform_info = get_cpu_platform_info()
  669. if args.debug:
  670. for key, value in platform_info.items():
  671. log('[D] cpu platform info: {} = {}'.format(key.replace("_", " "), value))
  672. regs = calc_reg_values(platform_info, config)
  673. if not config.getboolean('GENERAL', 'Enabled'):
  674. log('[I] Throttled is disabled in config file... Quitting. :(')
  675. return
  676. exit_event = Event()
  677. thread = Thread(target=power_thread, args=(config, regs, exit_event))
  678. thread.daemon = True
  679. thread.start()
  680. undervolt(config)
  681. set_icc_max(config)
  682. # handle dbus events for applying undervolt/IccMax on resume from sleep/hybernate
  683. def handle_sleep_callback(sleeping):
  684. if not sleeping:
  685. undervolt(config)
  686. set_icc_max(config)
  687. def handle_ac_callback(*args):
  688. try:
  689. power['source'] = 'BATTERY' if args[1]['Online'] == 0 else 'AC'
  690. power['method'] = 'dbus'
  691. except:
  692. power['method'] = 'polling'
  693. DBusGMainLoop(set_as_default=True)
  694. bus = dbus.SystemBus()
  695. # add dbus receiver only if undervolt/IccMax is enabled in config
  696. if any(
  697. config.getfloat(key, plane, fallback=0) != 0 for plane in VOLTAGE_PLANES for key in UNDERVOLT_KEYS + ICCMAX_KEYS
  698. ):
  699. bus.add_signal_receiver(
  700. handle_sleep_callback, 'PrepareForSleep', 'org.freedesktop.login1.Manager', 'org.freedesktop.login1'
  701. )
  702. bus.add_signal_receiver(
  703. handle_ac_callback,
  704. signal_name="PropertiesChanged",
  705. dbus_interface="org.freedesktop.DBus.Properties",
  706. path="/org/freedesktop/UPower/devices/line_power_AC",
  707. )
  708. log('[I] Starting main loop.')
  709. if args.monitor is not None:
  710. monitor_thread = Thread(target=monitor, args=(exit_event, args.monitor))
  711. monitor_thread.daemon = True
  712. monitor_thread.start()
  713. try:
  714. loop = GLib.MainLoop()
  715. loop.run()
  716. except (KeyboardInterrupt, SystemExit):
  717. pass
  718. exit_event.set()
  719. loop.quit()
  720. thread.join(timeout=1)
  721. if args.monitor is not None:
  722. monitor_thread.join(timeout=0.1)
  723. if __name__ == '__main__':
  724. main()