Skip to content

Commit

Permalink
Merge pull request #76 from mengmeet/intel
Browse files Browse the repository at this point in the history
Intel support / Intel 处理器支持
  • Loading branch information
honjow authored May 26, 2024
2 parents b756b84 + 990f833 commit 16d28a9
Show file tree
Hide file tree
Showing 5 changed files with 697 additions and 267 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ yalc.lock
# Ignore output folder

backend/out
backend/*.sh
Makefile

.history
31 changes: 27 additions & 4 deletions backend/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging
import glob
import os
import yaml
import traceback
import decky_plugin
Expand Down Expand Up @@ -31,22 +32,43 @@
DECKY_PLUGIN_DIR = decky_plugin.DECKY_PLUGIN_DIR
SH_PATH = "{}/backend/sh_tools.sh".format(DECKY_PLUGIN_DIR)
RYZENADJ_PATH = "{}/bin/ryzenadj".format(DECKY_PLUGIN_DIR)
GPU_DEVICE_PATH = glob.glob("/sys/class/drm/card?/device")[0]
GPUFREQ_PATH = "{}/pp_od_clk_voltage".format(GPU_DEVICE_PATH)
GPULEVEL_PATH = "{}/power_dpm_force_performance_level".format(GPU_DEVICE_PATH)
# AMD_GPU_DEVICE_PATH = glob.glob("/sys/class/drm/card?/device")[0]
# AMD_GPUFREQ_PATH = "{}/pp_od_clk_voltage".format(AMD_GPU_DEVICE_PATH)
# AMD_GPULEVEL_PATH = "{}/power_dpm_force_performance_level".format(AMD_GPU_DEVICE_PATH)
PLATFORM_PROFILE_PATH = "/sys/firmware/acpi/platform_profile"
PLATFORM_PROFILE_CHOICES_PATH = "/sys/firmware/acpi/platform_profile_choices"

for p in glob.glob("/sys/class/drm/card?"):
if os.path.exists(f"{p}/device/enable"):
with open(f"{p}/device/enable", "r") as f:
if f.read().strip() == "1":
AMD_GPU_DEVICE_PATH = f"{p}/device"
AMD_GPUFREQ_PATH = f"{AMD_GPU_DEVICE_PATH}/pp_od_clk_voltage"
AMD_GPULEVEL_PATH = f"{AMD_GPU_DEVICE_PATH}/power_dpm_force_performance_level"

# read adn write
INTEL_GPU_MIN_FREQ = f"{p}/gt_min_freq_mhz"
INTEL_GPU_MAX_FREQ = f"{p}/gt_max_freq_mhz"
INTEL_GPU_BOOST_FREQ = f"{p}/gt_boost_freq_mhz"

# read only
INTEL_GPU_MAX_LIMIT = f"{p}/gt_RP0_freq_mhz"
INTEL_GPU_NORMAL_LIMIT = f"{p}/gt_RP1_freq_mhz"
INTEL_GPU_MIN_LIMIT = f"{p}/gt_RPn_freq_mhz"
INTEL_GPU_CUR_FREQ = f"{p}/gt_cur_freq_mhz"
break

FAN_HWMON_CONFIG_DIR = f"{DECKY_PLUGIN_DIR}/backend/fan_config/hwmon"
FAN_EC_CONFIG_DIR = f"{DECKY_PLUGIN_DIR}/backend/fan_config/ec"
except Exception as e:
logging.error(f"路径配置异常|{e}")
logging.error(f"路径配置异常|{e}", exc_info=True)

# 设备信息获取配置
try:
cpuinfo_path = "/proc/cpuinfo"
cpuinfo = open(cpuinfo_path, "r").read()
CPU_ID = cpuinfo.split("model name")[1].split(":")[1].split("\n")[0].strip()
CPU_VENDOR = cpuinfo.split("vendor_id")[1].split(":")[1].split("\n")[0].strip()
PRODUCT_NAME = open("/sys/devices/virtual/dmi/id/product_name", "r").read().strip()
PRODUCT_VERSION = (
open("/sys/devices/virtual/dmi/id/product_version", "r").read().strip()
Expand Down Expand Up @@ -84,6 +106,7 @@
"ONEXPLAYER F1": 35,
"ONEXPLAYER F1 EVA-01": 35,
"G1619-04": 45, # GPD WINMAX2
"G1618-03": 28, # GPD WIN3
"G1618-04": 45, # GPD WIN4
"G1617-01": 30, # GPD WIN mini
"ROG Ally RC71L_RC71L": 30,
Expand Down
93 changes: 87 additions & 6 deletions backend/cpu.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import glob
import subprocess
import os
import re
import traceback
from config import logging, SH_PATH, RYZENADJ_PATH
from config import (
TDP_LIMIT_CONFIG_CPU,
TDP_LIMIT_CONFIG_PRODUCT,
PRODUCT_NAME,
CPU_ID,
CPU_VENDOR,
logging,
SH_PATH,
RYZENADJ_PATH,
)

# 初始参数
Expand Down Expand Up @@ -125,6 +129,60 @@ def get_cpu_AvailableFreq(self):
return []

def set_cpuTDP(self, value: int):
if CPU_VENDOR == "GenuineIntel":
return self.set_cpuTDP_Intel(value)
elif CPU_VENDOR == "AuthenticAMD":
self.set_cpuTDP_AMD(value)
else:
logging.error("set_cpuTDP error: unknown CPU_VENDOR")
return False

def __get_intel_rapl_path(self):
rapl_path = ""
rapl_long = ""
rapl_short = ""
try:
# 遍历 /sys/class/powercap/intel-rapl/intel-rapl:*/ 如果 name 是 package-0 则是cpu
for r_path in glob.glob("/sys/class/powercap/intel-rapl/intel-rapl:?"):
if os.path.isdir(r_path):
name_path = os.path.join(r_path, "name")
with open(name_path, "r") as file:
name = file.read().strip()
if name == "package-0":
rapl_path = r_path
break
for f in glob.glob(f"{rapl_path}/constraint_?_name"):
if os.path.isfile(f):
with open(f, "r") as file:
name = file.read().strip()
if name == "short_term":
rapl_short = f.replace("_name", "_power_limit_uw")
elif name == "long_term":
rapl_long = f.replace("_name", "_power_limit_uw")
return rapl_long, rapl_short
except Exception as e:
logging.error(e, exc_info=True)

def set_cpuTDP_Intel(self, value: int):
try:
# 遍历 /sys/class/powercap/intel-rapl/*/ 如果 name 是 package-0 则是cpu
logging.debug("set_cpuTDP_Intel {}".format(value))
tdp = value * 1000000
rapl_long, rapl_short = self.__get_intel_rapl_path()
if rapl_long == "" or rapl_short == "":
logging.error("set_cpuTDP_Intel error: rapl path not found")
return False
with open(rapl_long, "w") as file:
file.write(str(tdp))
with open(rapl_short, "w") as file:
file.write(str(tdp))
return True

except Exception as e:
logging.error(e, exc_info=True)
return False

def set_cpuTDP_AMD(self, value: int):
try:
if value >= 3:
tdp = value * 1000
Expand Down Expand Up @@ -281,8 +339,18 @@ def set_smt(self, value: bool):

def set_cpuBoost(self, value: bool):
boost_path = "/sys/devices/system/cpu/cpufreq/boost"
pstate_boost_path = "/sys/devices/system/cpu/amd_pstate/cpb_boost"
amd_pstate_path = "/sys/devices/system/cpu/amd_pstate/status"

# amd
amd_pstate_dir = "/sys/devices/system/cpu/amd_pstate"
pstate_boost_path = "${pstate_path}/cpb_boost"
amd_state_path = "${pstate_path}/status"

# intel
hwp_dynamic_boost_path = (
"/sys/devices/system/cpu/intel_pstate/hwp_dynamic_boost"
)
no_turbo_path = "/sys/devices/system/cpu/intel_pstate/no_turbo"

try:
logging.debug("set_cpuBoost {}".format(value))
global cpu_boost
Expand All @@ -291,8 +359,8 @@ def set_cpuBoost(self, value: bool):
# 如果不存在 pstate_boost_path
if not os.path.exists(pstate_boost_path):
# 切换为 passive 模式
if os.path.exists(amd_pstate_path):
open(amd_pstate_path, "w").write("passive")
if os.path.exists(amd_state_path) and os.path.exists(amd_pstate_dir):
open(amd_state_path, "w").write("passive")

# 设置 boost
if os.path.exists(boost_path):
Expand All @@ -301,7 +369,7 @@ def set_cpuBoost(self, value: bool):
file.write("1")
else:
file.write("0")

# 设置 pstate_boost
if os.path.exists(pstate_boost_path):
with open(pstate_boost_path, "w") as file:
Expand All @@ -310,6 +378,19 @@ def set_cpuBoost(self, value: bool):
else:
file.write("0")

# 设置 hwp_dynamic_boost
if os.path.exists(hwp_dynamic_boost_path):
with open(hwp_dynamic_boost_path, "w") as file:
file.write("1")

# 设置 no_turbo
if os.path.exists(no_turbo_path):
with open(no_turbo_path, "w") as file:
if cpu_boost:
file.write("0")
else:
file.write("1")

return True
except Exception as e:
logging.error(traceback.format_exc())
Expand Down
Loading

0 comments on commit 16d28a9

Please sign in to comment.