Fix ReverseNX RT/Tool mode synchronization

This commit is contained in:
KazushiM
2022-10-16 11:38:21 +08:00
parent dd466ffb79
commit d596016c84
5 changed files with 245 additions and 159 deletions

View File

@@ -44,7 +44,7 @@ index 6628524..640629f 160000
-Subproject commit 66285245361a02e5480c7bb7dac9ef6449ae6181 -Subproject commit 66285245361a02e5480c7bb7dac9ef6449ae6181
+Subproject commit 640629f49f9e8997ef0769b21b26f4fc177c736f +Subproject commit 640629f49f9e8997ef0769b21b26f4fc177c736f
diff --git a/Overlay/source/main.cpp b/Overlay/source/main.cpp diff --git a/Overlay/source/main.cpp b/Overlay/source/main.cpp
index cec060c..90c020b 100644 index cec060c..13818bb 100644
--- a/Overlay/source/main.cpp --- a/Overlay/source/main.cpp
+++ b/Overlay/source/main.cpp +++ b/Overlay/source/main.cpp
@@ -1,9 +1,12 @@ @@ -1,9 +1,12 @@
@@ -60,7 +60,7 @@ index cec060c..90c020b 100644
bool PluginRunning = false; bool PluginRunning = false;
bool closed = false; bool closed = false;
Handle debug; Handle debug;
@@ -20,6 +23,10 @@ bool plugin = false; @@ -20,6 +23,11 @@ bool plugin = false;
char DockedChar[32]; char DockedChar[32];
char SystemChar[32]; char SystemChar[32];
char PluginChar[36]; char PluginChar[36];
@@ -68,10 +68,26 @@ index cec060c..90c020b 100644
+static Service g_sysclkSrv; +static Service g_sysclkSrv;
+static std::atomic<std::size_t> g_refCnt; +static std::atomic<std::size_t> g_refCnt;
+bool sysclkComm = false; +bool sysclkComm = false;
+bool sysclkSync = false;
bool CheckPort () { bool CheckPort () {
Handle saltysd; Handle saltysd;
@@ -54,6 +61,57 @@ bool isServiceRunning(const char *serviceName) { @@ -43,17 +51,84 @@ bool CheckPort () {
return false;
}
-bool isServiceRunning(const char *serviceName) {
- Handle handle;
- SmServiceName service_name = smEncodeName(serviceName);
+bool isServiceRunning(const char *serviceName) {
+ Handle handle;
+ SmServiceName service_name = smEncodeName(serviceName);
if (R_FAILED(smRegisterService(&handle, service_name, false, 1))) return true;
else {
- svcCloseHandle(handle);
+ svcCloseHandle(handle);
smUnregisterService(service_name);
return false;
} }
} }
@@ -80,56 +96,81 @@ index cec060c..90c020b 100644
+ +
+enum SysClkIpcCmd +enum SysClkIpcCmd
+{ +{
+ SysClkIpcCmd_GetApiVersion = 0, + SysClkIpcCmd_GetApiVersion = 0,
+ SysClkIpcCmd_SetReverseNXRTMode = 11, + SysClkIpcCmd_GetConfigValues = 9,
+ SysClkIpcCmd_SetReverseNXRTMode = 11,
+};
+
+enum SysClkConfigValue
+{
+ SysClkConfigValue_SyncReverseNXMode = 4,
+ SysClkConfigValue_EnumMax = 8,
+};
+
+enum ReverseNXMode
+{
+ ReverseNX_NotValid = 0,
+ ReverseNX_SystemDefault = 0,
+ ReverseNX_Handheld,
+ ReverseNX_Docked,
+};
+
+struct SysClkConfigValueList
+{
+ uint64_t values[SysClkConfigValue_EnumMax];
+}; +};
+ +
+void sysclkIpcExit(void) +void sysclkIpcExit(void)
+{ +{
+ if (--g_refCnt == 0) + if (--g_refCnt == 0)
+ { + serviceClose(&g_sysclkSrv);
+ serviceClose(&g_sysclkSrv);
+ }
+} +}
+ +
+Result sysclkIpcInitialize(void) +Result sysclkIpcInitialize(void)
+{ +{
+ Result rc = 0; + Result rc = 0;
+ +
+ g_refCnt++; + g_refCnt++;
+ +
+ if (serviceIsActive(&g_sysclkSrv)) + if (serviceIsActive(&g_sysclkSrv))
+ return 0; + return 0;
+ +
+ rc = smGetService(&g_sysclkSrv, SYSCLK_IPC_SERVICE_NAME); + rc = smGetService(&g_sysclkSrv, SYSCLK_IPC_SERVICE_NAME);
+ +
+ if (R_FAILED(rc)) sysclkIpcExit(); + if (R_FAILED(rc))
+ sysclkIpcExit();
+ +
+ return rc; + return rc;
+} +}
+ +
+Result sysclkIpcGetAPIVersion(u32* out_ver) +Result sysclkIpcGetAPIVersion(u32* out_ver)
+{ +{
+ return serviceDispatchOut(&g_sysclkSrv, SysClkIpcCmd_GetApiVersion, *out_ver); + return serviceDispatchOut(&g_sysclkSrv, SysClkIpcCmd_GetApiVersion, *out_ver);
+} +}
+ +
+typedef enum +Result sysclkIpcGetConfigValues(SysClkConfigValueList* out_configValues)
+{ +{
+ ReverseNX_SystemDefault = 0, + return serviceDispatchOut(&g_sysclkSrv, SysClkIpcCmd_GetConfigValues, *out_configValues);
+ ReverseNX_Handheld, +}
+ ReverseNX_Docked,
+ ReverseNX_RTResetToDefault,
+} ReverseNXMode;
+ +
+Result sysclkIpcSetReverseNXRTMode(ReverseNXMode mode) +Result sysclkIpcSetReverseNXRTMode(ReverseNXMode mode)
+{ +{
+ return serviceDispatchIn(&g_sysclkSrv, SysClkIpcCmd_SetReverseNXRTMode, mode); + return serviceDispatchIn(&g_sysclkSrv, SysClkIpcCmd_SetReverseNXRTMode, mode);
+} +}
+ +
class GuiTest : public tsl::Gui { class GuiTest : public tsl::Gui {
public: public:
GuiTest(u8 arg1, u8 arg2, bool arg3) { } GuiTest(u8 arg1, u8 arg2, bool arg3) { }
@@ -88,6 +146,7 @@ public: @@ -67,7 +142,7 @@ public:
// A list that can contain sub elements and handles scrolling
auto list = new tsl::elm::List();
-
+
list->addItem(new tsl::elm::CustomDrawer([](tsl::gfx::Renderer *renderer, s32 x, s32 y, s32 w, s32 h) {
if (SaltySD == false) renderer->drawString("SaltyNX is not working!", false, x, y+50, 20, renderer->a(0xF33F));
else if (plugin == false) renderer->drawString("Can't detect ReverseNX-RT plugin!", false, x, y+50, 20, renderer->a(0xF33F));
@@ -88,16 +163,18 @@ public:
else { else {
renderer->drawString(SystemChar, false, x, y+40, 20, renderer->a(0xFFFF)); renderer->drawString(SystemChar, false, x, y+40, 20, renderer->a(0xFFFF));
renderer->drawString(DockedChar, false, x, y+60, 20, renderer->a(0xFFFF)); renderer->drawString(DockedChar, false, x, y+60, 20, renderer->a(0xFFFF));
@@ -137,11 +178,12 @@ index cec060c..90c020b 100644
} }
} }
}), 100); }), 100);
@@ -95,9 +154,10 @@ public:
if (MAGIC == 0x06BA7E39) { if (MAGIC == 0x06BA7E39) {
auto *clickableListItem = new tsl::elm::ListItem("Change system control"); auto *clickableListItem = new tsl::elm::ListItem("Change system control");
clickableListItem->setClickListener([](u64 keys) { - clickableListItem->setClickListener([](u64 keys) {
- if (keys & KEY_A) { - if (keys & KEY_A) {
+ clickableListItem->setClickListener([](u64 keys) {
+ if (keys & HidNpadButton_A) { + if (keys & HidNpadButton_A) {
if (PluginRunning == true) { if (PluginRunning == true) {
def = !def; def = !def;
@@ -149,11 +191,16 @@ index cec060c..90c020b 100644
if (dmntcht == true) { if (dmntcht == true) {
dmntchtWriteCheatProcessMemory(def_address, &def, 0x1); dmntchtWriteCheatProcessMemory(def_address, &def, 0x1);
dmntchtReadCheatProcessMemory(def_address, &def, 0x1); dmntchtReadCheatProcessMemory(def_address, &def, 0x1);
@@ -120,9 +180,10 @@ public: @@ -117,12 +194,13 @@ public:
});
list->addItem(clickableListItem);
-
+
auto *clickableListItem2 = new tsl::elm::ListItem("Change mode"); auto *clickableListItem2 = new tsl::elm::ListItem("Change mode");
clickableListItem2->setClickListener([](u64 keys) { - clickableListItem2->setClickListener([](u64 keys) {
- if (keys & KEY_A) { - if (keys & KEY_A) {
+ clickableListItem2->setClickListener([](u64 keys) {
+ if (keys & HidNpadButton_A) { + if (keys & HidNpadButton_A) {
if (PluginRunning == true && def == false) { if (PluginRunning == true && def == false) {
isDocked =! isDocked; isDocked =! isDocked;
@@ -161,16 +208,44 @@ index cec060c..90c020b 100644
if (dmntcht == true) { if (dmntcht == true) {
dmntchtWriteCheatProcessMemory(docked_address, &isDocked, 0x1); dmntchtWriteCheatProcessMemory(docked_address, &isDocked, 0x1);
dmntchtReadCheatProcessMemory(docked_address, &isDocked, 0x1); dmntchtReadCheatProcessMemory(docked_address, &isDocked, 0x1);
@@ -145,7 +206,7 @@ public: @@ -137,15 +215,15 @@ public:
}
else return false;
}
-
+
return false;
});
list->addItem(clickableListItem2);
}
else if (SaltySD == true && plugin == true && check == false) { else if (SaltySD == true && plugin == true && check == false) {
auto *clickableListItem = new tsl::elm::ListItem("(De)activate plugin"); auto *clickableListItem = new tsl::elm::ListItem("(De)activate plugin");
clickableListItem->setClickListener([](u64 keys) { - clickableListItem->setClickListener([](u64 keys) {
- if (keys & KEY_A) { - if (keys & KEY_A) {
+ clickableListItem->setClickListener([](u64 keys) {
+ if (keys & HidNpadButton_A) { + if (keys & HidNpadButton_A) {
if (bak == false) { if (bak == false) {
rename("sdmc:/SaltySD/plugins/ReverseNX-RT.elf", "sdmc:/SaltySD/plugins/ReverseNX-RT.elf.bak"); rename("sdmc:/SaltySD/plugins/ReverseNX-RT.elf", "sdmc:/SaltySD/plugins/ReverseNX-RT.elf.bak");
bak = true; bak = true;
@@ -179,12 +240,22 @@ public: @@ -156,7 +234,7 @@ public:
}
return true;
}
-
+
return false;
});
list->addItem(clickableListItem);
@@ -164,7 +242,7 @@ public:
// Add the list to the frame for it to be drawn
frame->setContent(list);
-
+
// Return the frame to have it become the top level element of this Gui
return frame;
}
@@ -179,30 +257,39 @@ public:
closed = true; closed = true;
} }
if (PluginRunning == true) { if (PluginRunning == true) {
@@ -188,14 +263,23 @@ index cec060c..90c020b 100644
+ } + }
+ if (def && defChanged) { + if (def && defChanged) {
+ defChanged = false; + defChanged = false;
+ sysclkIpcSetReverseNXRTMode(ReverseNX_RTResetToDefault); + sysclkIpcSetReverseNXRTMode(ReverseNX_SystemDefault);
+ } + }
+ } + }
i = 0; i = 0;
} }
else i++; else i++;
@@ -198,11 +269,10 @@ public: }
-
+
if (isDocked == true) sprintf(DockedChar, "Mode: Docked");
else sprintf(DockedChar, "Mode: Handheld");
-
+
if (def == true) sprintf(SystemChar, "Controlled by system: Yes");
else sprintf(SystemChar, "Controlled by system: No");
-
+
if (bak == false) sprintf(PluginChar, "ReverseNX-RT plugin is activated."); if (bak == false) sprintf(PluginChar, "ReverseNX-RT plugin is activated.");
else sprintf(PluginChar, "ReverseNX-RT plugin is deactivated."); else sprintf(PluginChar, "ReverseNX-RT plugin is deactivated.");
- -
@@ -207,29 +291,71 @@ index cec060c..90c020b 100644
return false; // Return true here to singal the inputs have been consumed return false; // Return true here to singal the inputs have been consumed
} }
}; };
@@ -234,6 +304,13 @@ public: @@ -213,10 +300,10 @@ public:
virtual void initServices() override {
smInitialize();
fsdevMountSdmc();
-
+
SaltySD = CheckPort();
if (SaltySD == false) return;
-
+
FILE* temp = fopen("sdmc:/SaltySD/plugins/ReverseNX-RT.elf", "r");
if (temp != NULL) {
fclose(temp);
@@ -234,6 +321,20 @@ public:
else return; else return;
} }
+ // Initialize sysclkIpc + sysclkComm = false;
+ sysclkSync = false;
+ if (isServiceRunning(SYSCLK_IPC_SERVICE_NAME)) { + if (isServiceRunning(SYSCLK_IPC_SERVICE_NAME)) {
+ uint32_t apiVer; + uint32_t apiVer;
+ if (R_SUCCEEDED(sysclkIpcInitialize()) && R_SUCCEEDED(sysclkIpcGetAPIVersion(&apiVer)) && apiVer == SYSCLK_IPC_API_VERSION) + if (R_SUCCEEDED(sysclkIpcInitialize()) && R_SUCCEEDED(sysclkIpcGetAPIVersion(&apiVer)) && apiVer == SYSCLK_IPC_API_VERSION) {
+ sysclkComm = true; + sysclkComm = true;
+
+ SysClkConfigValueList* list = new SysClkConfigValueList;
+ if (R_SUCCEEDED(sysclkIpcGetConfigValues(list)))
+ sysclkSync = bool(list->values[SysClkConfigValue_SyncReverseNXMode]);
+ delete list;
+ }
+ } + }
+ +
if (R_FAILED(pmdmntGetApplicationProcessId(&PID))) remove("sdmc:/SaltySD/ReverseNX-RT.hex"); if (R_FAILED(pmdmntGetApplicationProcessId(&PID))) remove("sdmc:/SaltySD/ReverseNX-RT.hex");
else { else {
check = true; check = true;
@@ -278,10 +355,13 @@ public: @@ -244,9 +345,9 @@ public:
fread(&def_address, 0x5, 1, offset);
fread(&MAGIC_address, 0x5, 1, offset);
fclose(offset);
-
+
dmntcht = isServiceRunning("dmnt:cht");
-
+
if (dmntcht == true) {
if (R_SUCCEEDED(dmntchtInitialize())) {
bool out = false;
@@ -272,16 +373,19 @@ public:
}
}
}
-
+
if (isDocked == true) sprintf(DockedChar, "Mode: Docked");
else sprintf(DockedChar, "Mode: Handheld");
-
+
if (def == true) sprintf(SystemChar, "Controlled by system: Yes"); if (def == true) sprintf(SystemChar, "Controlled by system: Yes");
else sprintf(SystemChar, "Controlled by system: No"); else sprintf(SystemChar, "Controlled by system: No");
-
+
+ sprintf(SysclkChar, "Sys-clk-OC Sync: %s", sysclkComm ? (sysclkSync ? "ON" : "OFF") : "UNAVAILABLE");
+ +
+ sprintf(SysclkChar, "Sys-clk-OC Sync Service: %s", sysclkComm ? "ON" : "OFF");
} // Called at the start to initialize all services necessary for this Overlay } // Called at the start to initialize all services necessary for this Overlay
-
+
virtual void exitServices() override { virtual void exitServices() override {
+ sysclkIpcExit(); + sysclkIpcExit();
dmntchtExit(); dmntchtExit();

View File

@@ -45,7 +45,6 @@ typedef struct
uint8_t enabled; uint8_t enabled;
uint64_t applicationId; uint64_t applicationId;
SysClkProfile profile; SysClkProfile profile;
SysClkProfile realProfile;
uint32_t freqs[SysClkModule_EnumMax]; uint32_t freqs[SysClkModule_EnumMax];
uint32_t overrideFreqs[SysClkModule_EnumMax]; uint32_t overrideFreqs[SysClkModule_EnumMax];
uint32_t temps[SysClkThermalSensor_EnumMax]; uint32_t temps[SysClkThermalSensor_EnumMax];
@@ -54,21 +53,20 @@ typedef struct
typedef enum typedef enum
{ {
ReverseNX_SystemDefault = 0,
ReverseNX_NotFound = 0, ReverseNX_NotFound = 0,
ReverseNX_NotValid = 0, ReverseNX_SystemDefault = 0,
ReverseNX_GotValue = 0,
ReverseNX_Handheld, ReverseNX_Handheld,
ReverseNX_Docked, ReverseNX_Docked,
ReverseNX_RTResetToDefault,
} ReverseNXMode; } ReverseNXMode;
typedef struct typedef struct
{ {
bool systemCoreBoostCPU; bool systemCoreBoostCPU;
bool gotBoostCPUFreq;
bool allowUnsafeFreq; bool allowUnsafeFreq;
ReverseNXMode reverseNXMode; bool syncReverseNXMode;
SysClkProfile realProfile;
ReverseNXMode reverseNXToolMode;
ReverseNXMode reverseNXRTMode;
uint32_t maxMEMFreq; uint32_t maxMEMFreq;
uint32_t boostCPUFreq; uint32_t boostCPUFreq;
} SysClkOcExtra; } SysClkOcExtra;

View File

@@ -45,7 +45,6 @@ ClockManager::ClockManager()
this->context = new SysClkContext; this->context = new SysClkContext;
this->context->applicationId = 0; this->context->applicationId = 0;
this->context->profile = SysClkProfile_Handheld; this->context->profile = SysClkProfile_Handheld;
this->context->realProfile = SysClkProfile_Handheld;
this->context->enabled = false; this->context->enabled = false;
for(unsigned int i = 0; i < SysClkModule_EnumMax; i++) for(unsigned int i = 0; i < SysClkModule_EnumMax; i++)
{ {
@@ -59,11 +58,13 @@ ClockManager::ClockManager()
this->oc = new SysClkOcExtra; this->oc = new SysClkOcExtra;
this->oc->systemCoreBoostCPU = false; this->oc->systemCoreBoostCPU = false;
this->oc->gotBoostCPUFreq = false;
this->oc->allowUnsafeFreq = false; this->oc->allowUnsafeFreq = false;
this->oc->reverseNXMode = ReverseNX_NotFound; this->oc->syncReverseNXMode = false;
this->oc->realProfile = SysClkProfile_Handheld;
this->oc->reverseNXToolMode = ReverseNX_NotFound;
this->oc->reverseNXRTMode = ReverseNX_NotFound;
this->oc->maxMEMFreq = 0; this->oc->maxMEMFreq = 0;
this->oc->boostCPUFreq = 1785'000'000; this->oc->boostCPUFreq = 0;
} }
ClockManager::~ClockManager() ClockManager::~ClockManager()
@@ -77,24 +78,11 @@ bool ClockManager::IsCpuBoostMode()
{ {
std::uint32_t confId = this->context->perfConfId; std::uint32_t confId = this->context->perfConfId;
bool isCpuBoostMode = (confId == 0x92220009 || confId == 0x9222000A); bool isCpuBoostMode = (confId == 0x92220009 || confId == 0x9222000A);
if (isCpuBoostMode && !this->oc->gotBoostCPUFreq) if (isCpuBoostMode && !this->oc->boostCPUFreq)
{ this->oc->boostCPUFreq = std::max(this->context->freqs[SysClkModule_CPU], 1785'000'000U);
this->oc->gotBoostCPUFreq = true;
this->oc->boostCPUFreq = std::max(this->context->freqs[SysClkModule_CPU], this->oc->boostCPUFreq);
}
return isCpuBoostMode; return isCpuBoostMode;
} }
bool ClockManager::IsReverseNXModeValid()
{
return (this->oc->reverseNXMode);
}
bool ClockManager::IsReverseNXDocked()
{
return (this->oc->reverseNXMode == ReverseNX_Docked);
}
void ClockManager::SetRunning(bool running) void ClockManager::SetRunning(bool running)
{ {
this->running = running; this->running = running;
@@ -107,10 +95,8 @@ bool ClockManager::Running()
uint32_t ClockManager::GetHz(SysClkModule module) uint32_t ClockManager::GetHz(SysClkModule module)
{ {
uint32_t hz = 0;
/* Temp override setting */ /* Temp override setting */
hz = this->context->overrideFreqs[module]; uint32_t hz = this->context->overrideFreqs[module];
/* Per-Game setting */ /* Per-Game setting */
if (!hz) if (!hz)
@@ -120,25 +106,23 @@ uint32_t ClockManager::GetHz(SysClkModule module)
if (!hz) if (!hz)
hz = this->config->GetAutoClockHz(SYSCLK_GLOBAL_PROFILE_TID, module, this->context->profile); hz = this->config->GetAutoClockHz(SYSCLK_GLOBAL_PROFILE_TID, module, this->context->profile);
/* Return pre-set hz if ReverseNX is enabled, downclock is disabled when realProfile == Docked */ /* Return pre-set hz */
if (!hz && IsReverseNXModeValid()) if (!hz && this->oc->syncReverseNXMode && GetReverseNXMode())
{ {
switch(module) switch (module)
{ {
case SysClkModule_CPU: case SysClkModule_CPU:
hz = 1020'000'000; hz = 1020'000'000;
break; break;
case SysClkModule_GPU: case SysClkModule_GPU:
if (!IsReverseNXDocked() && this->context->realProfile != SysClkProfile_Docked) hz = (GetReverseNXMode() == ReverseNX_Docked ||
hz = 460'800'000; this->oc->realProfile == SysClkProfile_Docked) ?
else 768'000'000 : 460'800'000;
hz = 768'000'000;
break; break;
case SysClkModule_MEM: case SysClkModule_MEM:
if (!IsReverseNXDocked() && this->context->realProfile != SysClkProfile_Docked) hz = (GetReverseNXMode() == ReverseNX_Docked ||
hz = 1600000000; this->oc->realProfile == SysClkProfile_Docked) ?
else MAX_MEM_CLOCK : 1600'000'000;
hz = MAX_MEM_CLOCK;
break; break;
default: default:
break; break;
@@ -148,17 +132,17 @@ uint32_t ClockManager::GetHz(SysClkModule module)
if (hz) if (hz)
{ {
/* Considering realProfile frequency limit */ /* Considering realProfile frequency limit */
hz = Clocks::GetNearestHz(module, this->context->realProfile, hz, this->oc->allowUnsafeFreq); hz = Clocks::GetNearestHz(module, this->oc->realProfile, hz, this->oc->allowUnsafeFreq);
if (module == SysClkModule_MEM && hz == MAX_MEM_CLOCK) if (module == SysClkModule_MEM && hz == MAX_MEM_CLOCK)
{ {
/* Trigger Max Mem Clock and record it */ /* Trigger Max Mem Clock and record it */
if (!this->oc->maxMEMFreq) if (!this->oc->maxMEMFreq)
{ {
uint32_t CurrentHz = Clocks::GetCurrentHz(SysClkModule_MEM); uint32_t currentHz = Clocks::GetCurrentHz(SysClkModule_MEM);
Clocks::SetHz(SysClkModule_MEM, MAX_MEM_CLOCK); Clocks::SetHz(SysClkModule_MEM, MAX_MEM_CLOCK);
this->oc->maxMEMFreq = Clocks::GetCurrentHz(SysClkModule_MEM); this->oc->maxMEMFreq = Clocks::GetCurrentHz(SysClkModule_MEM);
Clocks::SetHz(SysClkModule_MEM, CurrentHz); Clocks::SetHz(SysClkModule_MEM, currentHz);
} }
return this->oc->maxMEMFreq; return this->oc->maxMEMFreq;
@@ -170,8 +154,8 @@ uint32_t ClockManager::GetHz(SysClkModule module)
{ {
if (this->oc->systemCoreBoostCPU && hz < this->oc->boostCPUFreq) if (this->oc->systemCoreBoostCPU && hz < this->oc->boostCPUFreq)
return this->oc->boostCPUFreq; return this->oc->boostCPUFreq;
else if (!hz) if (!hz)
/* Prevent crash when hz = 0 in SetHz(0), trigger RefreshContext() and Tick() */ /* Trigger RefreshContext() and Tick(), resetting default CPU frequency */
return 1020'000'000; return 1020'000'000;
} }
@@ -182,11 +166,7 @@ void ClockManager::Tick()
{ {
std::scoped_lock lock{this->contextMutex}; std::scoped_lock lock{this->contextMutex};
bool setClock = false; if (this->RefreshContext() && this->context->enabled)
setClock |= this->config->Refresh();
setClock |= this->RefreshContext();
setClock &= this->context->enabled;
if (setClock)
{ {
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++) for (unsigned int module = 0; module < SysClkModule_EnumMax; module++)
{ {
@@ -216,7 +196,7 @@ void ClockManager::WaitForNextTick()
bool isAutoBoostEnabled = this->GetConfig()->GetConfigValue(SysClkConfigValue_AutoCPUBoost); bool isAutoBoostEnabled = this->GetConfig()->GetConfigValue(SysClkConfigValue_AutoCPUBoost);
if ( isAutoBoostEnabled if ( isAutoBoostEnabled
&& this->context->realProfile != SysClkProfile_Handheld && this->oc->realProfile != SysClkProfile_Handheld
&& this->context->enabled && this->context->enabled
&& this->context->freqs[SysClkModule_CPU] <= this->oc->boostCPUFreq) && this->context->freqs[SysClkModule_CPU] <= this->oc->boostCPUFreq)
{ {
@@ -257,23 +237,16 @@ void ClockManager::WaitForNextTick()
SysClkProfile ClockManager::ReverseNXProfileHandler() SysClkProfile ClockManager::ReverseNXProfileHandler()
{ {
if (!IsReverseNXModeValid()) switch (GetReverseNXMode())
{ {
return this->context->realProfile; case ReverseNX_Docked:
return SysClkProfile_Docked;
case ReverseNX_Handheld:
return (this->oc->realProfile == SysClkProfile_Docked) ?
SysClkProfile_HandheldChargingOfficial : this->oc->realProfile;
default:
return this->oc->realProfile;
} }
if (IsReverseNXDocked())
{
return SysClkProfile_Docked;
}
if (// !IsReversedNXDocked() &&
this->context->realProfile == SysClkProfile_Docked)
{
return SysClkProfile_HandheldChargingOfficial;
}
return this->context->realProfile;
} }
ReverseNXMode ClockManager::ReverseNXFileHandler(const char* filePath) ReverseNXMode ClockManager::ReverseNXFileHandler(const char* filePath)
@@ -291,22 +264,22 @@ ReverseNXMode ClockManager::ReverseNXFileHandler(const char* filePath)
fread(&readBuffer, 1, sizeof(readBuffer), readFile); fread(&readBuffer, 1, sizeof(readBuffer), readFile);
fclose(readFile); fclose(readFile);
if (!memcmp(&readBuffer, &magicDocked, sizeof(readBuffer))) if (R_SUCCEEDED(memcmp(&readBuffer, &magicDocked, sizeof(readBuffer))))
return ReverseNX_Docked; return ReverseNX_Docked;
if (!memcmp(&readBuffer, &magicHandheld, sizeof(readBuffer))) if (R_SUCCEEDED(memcmp(&readBuffer, &magicHandheld, sizeof(readBuffer))))
return ReverseNX_Handheld; return ReverseNX_Handheld;
return ReverseNX_NotValid; return ReverseNX_NotFound;
} }
void ClockManager::CheckReverseNXTool() ReverseNXMode ClockManager::GetReverseNXToolMode()
{ {
bool shouldCheckReverseNXTool = FileUtils::ExistReverseNXTool(); bool shouldCheckReverseNXTool = FileUtils::ExistReverseNXTool();
if (!shouldCheckReverseNXTool) if (!shouldCheckReverseNXTool)
return; return ReverseNX_NotFound;
ReverseNXMode getMode = ReverseNX_NotValid; ReverseNXMode getMode = ReverseNX_NotFound;
if (this->context->applicationId != PROCESS_MANAGEMENT_QLAUNCH_TID) if (this->context->applicationId != PROCESS_MANAGEMENT_QLAUNCH_TID)
{ {
const char asmFileName[] = "_ZN2nn2oe18GetPerformanceModeEv.asm64"; // Checking one asm64 file is enough const char asmFileName[] = "_ZN2nn2oe18GetPerformanceModeEv.asm64"; // Checking one asm64 file is enough
@@ -324,23 +297,14 @@ void ClockManager::CheckReverseNXTool()
} }
} }
this->oc->reverseNXMode = getMode; return getMode;
} }
bool ClockManager::CheckReverseNXRT() ReverseNXMode ClockManager::GetReverseNXMode()
{ {
bool shouldAdjustProfile = false; if (this->oc->reverseNXRTMode)
return this->oc->reverseNXRTMode;
ReverseNXMode getMode = this->GetConfig()->GetReverseNXRTMode(); return this->oc->reverseNXToolMode;
if (getMode)
{
this->GetConfig()->SetReverseNXRTMode(ReverseNX_GotValue);
this->oc->reverseNXMode = (getMode == ReverseNX_RTResetToDefault) ?
ReverseNX_SystemDefault : getMode;
shouldAdjustProfile = true;
}
return shouldAdjustProfile;
} }
void ClockManager::ChargingHandler() void ClockManager::ChargingHandler()
@@ -351,14 +315,11 @@ void ClockManager::ChargingHandler()
Service* session = psmGetServiceSession(); Service* session = psmGetServiceSession();
serviceDispatchOut(session, GetBatteryChargeInfoFields, *(chargeInfoField)); serviceDispatchOut(session, GetBatteryChargeInfoFields, *(chargeInfoField));
bool fastChargingEnabled = chargeInfoField->ChargeCurrentLimit > 768; bool fastChargingState = chargeInfoField->ChargeCurrentLimit > 768;
bool fastChargingConfig = !(this->GetConfig()->GetConfigValue(SysClkConfigValue_DisableFastCharging)); bool fastChargingConfig = !(this->GetConfig()->GetConfigValue(SysClkConfigValue_DisableFastCharging));
if (fastChargingEnabled != fastChargingConfig) if (fastChargingState != fastChargingConfig)
{
serviceDispatch(session, fastChargingConfig ? EnableFastBatteryCharging : DisableFastBatteryCharging); serviceDispatch(session, fastChargingConfig ? EnableFastBatteryCharging : DisableFastBatteryCharging);
}
u32 chargeLimit = this->GetConfig()->GetConfigValue(SysClkConfigValue_ChargingLimitPercentage);
bool isChargerConnected = (chargeInfoField->ChargerType != ChargerType_None); bool isChargerConnected = (chargeInfoField->ChargerType != ChargerType_None);
if (isChargerConnected) if (isChargerConnected)
{ {
@@ -366,6 +327,7 @@ void ClockManager::ChargingHandler()
if (R_SUCCEEDED(psmGetBatteryChargePercentage(&chargeNow))) if (R_SUCCEEDED(psmGetBatteryChargePercentage(&chargeNow)))
{ {
bool isCharging = ((chargeInfoField->unk_x14 >> 8) & 1); bool isCharging = ((chargeInfoField->unk_x14 >> 8) & 1);
u32 chargeLimit = this->GetConfig()->GetConfigValue(SysClkConfigValue_ChargingLimitPercentage);
if (isCharging && chargeLimit < chargeNow) { if (isCharging && chargeLimit < chargeNow) {
serviceDispatch(session, DisableBatteryCharging); serviceDispatch(session, DisableBatteryCharging);
} }
@@ -384,8 +346,8 @@ bool ClockManager::RefreshContext()
{ {
ChargingHandler(); ChargingHandler();
bool hasChanged = false; bool hasChanged = this->config->Refresh();
bool isReverseNXSyncEnabled = this->GetConfig()->GetConfigValue(SysClkConfigValue_SyncReverseNXMode); this->oc->syncReverseNXMode = this->GetConfig()->GetConfigValue(SysClkConfigValue_SyncReverseNXMode);
this->oc->allowUnsafeFreq = this->GetConfig()->GetConfigValue(SysClkConfigValue_AllowUnsafeFrequencies); this->oc->allowUnsafeFreq = this->GetConfig()->GetConfigValue(SysClkConfigValue_AllowUnsafeFrequencies);
bool enabled = this->GetConfig()->Enabled(); bool enabled = this->GetConfig()->Enabled();
@@ -403,18 +365,17 @@ bool ClockManager::RefreshContext()
this->context->applicationId = applicationId; this->context->applicationId = applicationId;
hasChanged = true; hasChanged = true;
/* Clear ReverseNX state and recheck -Tool patches*/ /* Clear ReverseNX state */
if (isReverseNXSyncEnabled) { this->GetConfig()->SetReverseNXRTMode(ReverseNX_NotFound);
this->oc->reverseNXMode = ReverseNX_SystemDefault; this->oc->reverseNXRTMode = ReverseNX_NotFound;
CheckReverseNXTool(); this->oc->reverseNXToolMode = GetReverseNXToolMode();
}
} }
SysClkProfile profile = Clocks::GetCurrentProfile(); SysClkProfile profile = Clocks::GetCurrentProfile();
if (profile != this->context->realProfile) if (profile != this->oc->realProfile)
{ {
FileUtils::LogLine("[mgr] Profile change: %s", Clocks::GetProfileName(profile, true)); FileUtils::LogLine("[mgr] Profile change: %s", Clocks::GetProfileName(profile, true));
this->context->realProfile = profile; this->oc->realProfile = profile;
hasChanged = true; hasChanged = true;
} }
@@ -431,16 +392,19 @@ bool ClockManager::RefreshContext()
} }
} }
// restore clocks to stock values on app or profile change {
// and let ptm module handle boost clocks rather than resetting this->oc->reverseNXRTMode = this->GetConfig()->GetReverseNXRTMode();
if (hasChanged && !IsCpuBoostMode()) SysClkProfile currentProfile = this->context->profile;
Clocks::ResetToStock(); SysClkProfile expectedProfile = this->oc->syncReverseNXMode ?
ReverseNXProfileHandler() : this->oc->realProfile;
this->context->profile = expectedProfile;
if (currentProfile != expectedProfile)
hasChanged = true;
}
/* Check ReverseNX-RT and adjust nominal profile when context changes */ // let ptm module handle boost clocks rather than resetting
if (isReverseNXSyncEnabled) { if (hasChanged && !IsCpuBoostMode()) {
hasChanged |= CheckReverseNXRT(); Clocks::ResetToStock();
if (hasChanged)
this->context->profile = ReverseNXProfileHandler();
} }
std::uint32_t hz = 0; std::uint32_t hz = 0;

View File

@@ -107,15 +107,14 @@ class ClockManager
SysClkOcExtra *oc; SysClkOcExtra *oc;
bool IsCpuBoostMode(); bool IsCpuBoostMode();
bool IsReverseNXModeValid();
bool IsReverseNXDocked();
uint32_t GetHz(SysClkModule); uint32_t GetHz(SysClkModule);
SysClkProfile ReverseNXProfileHandler(); SysClkProfile ReverseNXProfileHandler();
ReverseNXMode ReverseNXFileHandler(const char*); ReverseNXMode ReverseNXFileHandler(const char*);
ReverseNXMode GetReverseNXToolMode();
ReverseNXMode GetReverseNXMode();
void CheckReverseNXTool();
bool CheckReverseNXRT();
void ChargingHandler(); void ChargingHandler();
}; };

View File

@@ -86,7 +86,6 @@ bool Config::Refresh()
if (!this->loaded || this->mtime != this->CheckModificationTime()) if (!this->loaded || this->mtime != this->CheckModificationTime())
{ {
this->Load(); this->Load();
Clocks::ResetToStock(); // Reset to stock since we can't detect if user set "Do not override" for specific module
return true; return true;
} }
return false; return false;