[sys-clk-OC] Disable unsafe frequencies by default; Remove emulator-mode; Move fast-charging handler to sysmodule

This commit is contained in:
KazushiM
2022-05-15 17:48:24 +08:00
parent fd77dfa6eb
commit 6f5de53382
14 changed files with 222 additions and 100 deletions

View File

@@ -60,7 +60,7 @@ ClockManager::ClockManager()
this->oc = new SysClkOcExtra;
this->oc->systemCoreBoostCPU = false;
this->oc->gotBoostCPUFreq = false;
this->oc->handheldEmulatorMode = false;
this->oc->allowUnsafeFreq = false;
this->oc->reverseNXMode = ReverseNX_NotFound;
this->oc->maxMEMFreq = 0;
this->oc->boostCPUFreq = 1785'000'000;
@@ -148,7 +148,7 @@ uint32_t ClockManager::GetHz(SysClkModule module)
if (hz)
{
/* Considering realProfile frequency limit */
hz = Clocks::GetNearestHz(module, this->context->realProfile, hz);
hz = Clocks::GetNearestHz(module, this->context->realProfile, hz, this->oc->allowUnsafeFreq);
if (module == SysClkModule_MEM && hz == MAX_MEM_CLOCK)
{
@@ -165,23 +165,6 @@ uint32_t ClockManager::GetHz(SysClkModule module)
}
}
/* Handle Handheld Emulator-Mode limit */
if (this->context->realProfile == SysClkProfile_Handheld)
{
switch (module)
{
case SysClkModule_CPU:
this->oc->handheldEmulatorMode = (hz > SYSCLK_CPU_HANDHELD_MAX_HZ);
break;
case SysClkModule_GPU:
if (this->oc->handheldEmulatorMode)
hz = std::min(hz, SYSCLK_GPU_HANDHELD_EMULATOR_HZ);
break;
default:
break;
}
}
/* Handle CPU Auto Boost, no user-defined hz required */
if (module == SysClkModule_CPU)
{
@@ -199,7 +182,11 @@ void ClockManager::Tick()
{
std::scoped_lock lock{this->contextMutex};
if ((this->RefreshContext() || this->config->Refresh()) && this->context->enabled)
bool setClock = false;
setClock |= this->config->Refresh();
setClock |= this->RefreshContext();
setClock &= this->context->enabled;
if (setClock)
{
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++)
{
@@ -344,26 +331,64 @@ bool ClockManager::CheckReverseNXRT()
{
bool shouldAdjustProfile = false;
ReverseNXMode getMode = this->GetConfig()->GetReverseNXRTModeAndClear();
ReverseNXMode getMode = this->GetConfig()->GetReverseNXRTMode();
if (getMode)
{
this->oc->reverseNXMode = getMode;
this->GetConfig()->SetReverseNXRTMode(ReverseNX_GotValue);
this->oc->reverseNXMode = (getMode == ReverseNX_RTResetToDefault) ?
ReverseNX_SystemDefault : getMode;
shouldAdjustProfile = true;
}
if (getMode == ReverseNX_RTResetToDefault)
{
this->oc->reverseNXMode = ReverseNX_SystemDefault;
}
return shouldAdjustProfile;
}
void ClockManager::ChargingHandler()
{
smInitialize();
psmInitialize();
ChargeInfo* chargeInfoField = new ChargeInfo;
Service* session = psmGetServiceSession();
serviceDispatchOut(session, GetBatteryChargeInfoFields, *(chargeInfoField));
bool fastChargingEnabled = chargeInfoField->ChargeCurrentLimit > 768;
bool fastChargingConfig = !(this->GetConfig()->GetConfigValue(SysClkConfigValue_DisableFastCharging));
if (fastChargingEnabled != fastChargingConfig)
{
serviceDispatch(session, fastChargingConfig ? EnableFastBatteryCharging : DisableFastBatteryCharging);
}
// bool isChargerConnected = (chargeInfoField->ChargerType != ChargerType_None);
// if (isChargerConnected)
// {
// u32 chargeNow = 0;
// if (R_SUCCEEDED(psmGetBatteryChargePercentage(&chargeNow)))
// {
// bool isCharging = ((chargeInfoField->unk_x14 >> 8) & 1);
// u32 chargeLimit = this->GetConfig()->GetConfigValue(SysClkConfigValue_ChargingLimitPercentage);
// if (isCharging && chargeLimit < chargeNow) {
// serviceDispatch(session, DisableBatteryCharging);
// } else if (!isCharging && chargeNow < 100 && chargeLimit > chargeNow) {
// serviceDispatch(session, EnableBatteryCharging);
// }
// }
// }
delete chargeInfoField;
psmExit();
smExit();
}
bool ClockManager::RefreshContext()
{
ChargingHandler();
bool hasChanged = false;
bool enabled = this->GetConfig()->Enabled();
bool isReverseNXSyncEnabled = this->GetConfig()->GetConfigValue(SysClkConfigValue_SyncReverseNXMode);
this->oc->allowUnsafeFreq = this->GetConfig()->GetConfigValue(SysClkConfigValue_AllowUnsafeFrequencies);
bool enabled = this->GetConfig()->Enabled();
if(enabled != this->context->enabled)
{
this->context->enabled = enabled;

View File

@@ -32,6 +32,64 @@ class ClockManager
SysClkContext GetCurrentContext();
Config* GetConfig();
typedef enum {
PDCtrler_NewPDO = 1, //Received new Power Data Object
PDCtrler_NoPD = 2, //No Power Delivery source is detected
PDCtrler_AcceptedRDO = 3 //Received and accepted Request Data Object
} ChargeInfoPDCtrler; //BM92T series
typedef enum {
PowerRole_Sink = 1,
PowerRole_Source = 2
} ChargeInfoPowerRole;
typedef enum {
ChargerType_None = 0,
ChargerType_PD = 1,
ChargerType_TypeC_1500mA = 2,
ChargerType_TypeC_3000mA = 3,
ChargerType_DCP = 4,
ChargerType_CDP = 5,
ChargerType_SDP = 6,
ChargerType_Apple_500mA = 7,
ChargerType_Apple_1000mA = 8,
ChargerType_Apple_2000mA = 9
} ChargeInfoChargerType;
typedef enum {
Flags_NoHub = BIT(0), //If hub is disconnected
Flags_Rail = BIT(8), //At least one Joy-con is charging from rail
Flags_SPDSRC = BIT(12), //OTG
Flags_ACC = BIT(16) //Accessory
} ChargeInfoFlags;
typedef struct {
int32_t InputCurrentLimit; //Input (Sink) current limit in mA
int32_t VBUSCurrentLimit; //Output (Source/VBUS/OTG) current limit in mA
int32_t ChargeCurrentLimit; //Battery charging current limit in mA (512mA when Docked, 768mA when BatteryTemperature < 17.0 C)
int32_t ChargeVoltageLimit; //Battery charging voltage limit in mV (3952mV when BatteryTemperature >= 51.0 C)
int32_t unk_x10; //Possibly an emum, getting the same value as PowerRole in all tested cases
int32_t unk_x14; //Possibly flags
ChargeInfoPDCtrler PDCtrlerState; //Power Delivery Controller State
int32_t BatteryTemperature; //Battery temperature in milli C
int32_t RawBatteryCharge; //Raw battery charged capacity per cent-mille (i.e. 100% = 100000 pcm)
int32_t VoltageAvg; //Voltage avg in mV (more in Notes)
int32_t BatteryAge; //Battery age (capacity full / capacity design) per cent-mille (i.e. 100% = 100000 pcm)
ChargeInfoPowerRole PowerRole;
ChargeInfoChargerType ChargerType;
int32_t ChargerVoltageLimit; //Charger and external device voltage limit in mV
int32_t ChargerCurrentLimit; //Charger and external device current limit in mA
ChargeInfoFlags Flags; //Unknown flags
} ChargeInfo;
typedef enum {
EnableBatteryCharging = 2,
DisableBatteryCharging = 3,
EnableFastBatteryCharging = 10,
DisableFastBatteryCharging = 11,
GetBatteryChargeInfoFields = 17,
} IPsmServerCmd;
protected:
ClockManager();
virtual ~ClockManager();
@@ -58,4 +116,6 @@ class ClockManager
void CheckReverseNXTool();
bool CheckReverseNXRT();
void ChargingHandler();
};

View File

@@ -282,10 +282,10 @@ std::uint32_t Clocks::GetCurrentHz(SysClkModule module)
return hz;
}
std::uint32_t Clocks::GetNearestHz(SysClkModule module, SysClkProfile profile, std::uint32_t inHz)
std::uint32_t Clocks::GetNearestHz(SysClkModule module, SysClkProfile profile, std::uint32_t inHz, bool allowUnsafe)
{
std::uint32_t hz = GetNearestHz(module, inHz);
std::uint32_t maxHz = GetMaxAllowedHz(module, profile);
std::uint32_t maxHz = GetMaxAllowedHz(module, profile, allowUnsafe);
if(maxHz != 0)
{
@@ -295,11 +295,15 @@ std::uint32_t Clocks::GetNearestHz(SysClkModule module, SysClkProfile profile, s
return hz;
}
std::uint32_t Clocks::GetMaxAllowedHz(SysClkModule module, SysClkProfile profile)
std::uint32_t Clocks::GetMaxAllowedHz(SysClkModule module, SysClkProfile profile, bool allowUnsafe)
{
switch (module) {
case SysClkModule_CPU:
if (!allowUnsafe)
return SYSCLK_CPU_SAFE_MAX_HZ;
break;
case SysClkModule_GPU:
if (profile == SysClkProfile_Handheld)
if (profile == SysClkProfile_Handheld || !allowUnsafe)
return SYSCLK_GPU_HANDHELD_MAX_HZ;
if (profile == SysClkProfile_HandheldChargingUSB)
return SYSCLK_GPU_CHARGING_USB_MAX_HZ;

View File

@@ -27,7 +27,7 @@ class Clocks
static const char* GetProfileName(SysClkProfile profile, bool pretty);
static const char* GetModuleName(SysClkModule module, bool pretty);
static const char* GetThermalSensorName(SysClkThermalSensor sensor, bool pretty);
static std::uint32_t GetNearestHz(SysClkModule module, SysClkProfile profile, std::uint32_t inHz);
static std::uint32_t GetNearestHz(SysClkModule module, SysClkProfile profile, std::uint32_t inHz, bool allowUnsafe);
static std::uint32_t GetTemperatureMilli(SysClkThermalSensor sensor);
protected:
@@ -36,5 +36,5 @@ class Clocks
static PcvModuleId GetPcvModuleId(SysClkModule sysclkModule);
static std::uint32_t GetNearestHz(SysClkModule module, std::uint32_t inHz);
static void GetList(SysClkModule module, std::uint32_t **outClocks);
static std::uint32_t GetMaxAllowedHz(SysClkModule module, SysClkProfile profile);
static std::uint32_t GetMaxAllowedHz(SysClkModule module, SysClkProfile profile, bool allowUnsafe);
};

View File

@@ -483,10 +483,9 @@ bool Config::SetConfigValues(SysClkConfigValueList* configValues, bool immediate
return true;
}
ReverseNXMode Config::GetReverseNXRTModeAndClear() {
ReverseNXMode Config::GetReverseNXRTMode() {
std::scoped_lock lock{this->reverseNXRTMutex};
ReverseNXMode mode = this->reverseNXRTMode;
this->reverseNXRTMode = ReverseNX_GotValue;
return mode;
}

View File

@@ -47,7 +47,7 @@ class Config
const char* GetConfigValueName(SysClkConfigValue val, bool pretty);
void GetConfigValues(SysClkConfigValueList* out_configValues);
bool SetConfigValues(SysClkConfigValueList* configValues, bool immediate);
ReverseNXMode GetReverseNXRTModeAndClear();
ReverseNXMode GetReverseNXRTMode();
void SetReverseNXRTMode(ReverseNXMode);
protected:
void Load();