- IPC service for handling ReverseNX-RT communication, reducing I/O requests

- Set config for "Auto CPU Boost"/"Sync ReverseNX Mode" in overlay

- Apply hardened freq cap to handheld mode (CPU@1963MHz, GPU@921MHz)
This commit is contained in:
KazushiM
2022-02-11 01:37:47 +08:00
parent 94ac52e80b
commit 4ff20f15f1
21 changed files with 266 additions and 324 deletions

View File

@@ -14,8 +14,6 @@ I'd appreciate if someone is willing to contribute or upload latest binaries. Bu
- Overclocking in general (often combined with overvolting and overheating) will _degrade internal components_ - SoC, VRM(Voltage Regulator Module), Battery, etc. - _faster_ than you and the manufacturer have expected.
- There is **no dynamic frequency scaling** in HOS, which makes _overclocking acts differently than PC_ or other mobile devices. The console will be _sticking to what frequency you've set in the long term_, until you close the game or put it into sleep.
- Higher RAM clocks without proper timings could be UNSTABLE and cause graphical glitches / instabilities / filesystem corruption. **Always make backup before usage.**
@@ -53,6 +51,9 @@ I'd appreciate if someone is willing to contribute or upload latest binaries. Bu
- It has been proved safe without charger (not reaching battery power draw threshold)
- Unsafe: CPU/GPU @ 2397/1305 MHz
- Without chargers, CPU/GPU would be capped @ 1963/921 MHz.
- Without official chargers, GPU would be capped @ 1267 MHz.
- Why **NOT RECOMMENDED**?
- See `Current Flow` in sys-clk-OC overlay `Miscellaneous` (on battery) or measure power draw from charger yourself.
@@ -68,24 +69,22 @@ I'd appreciate if someone is willing to contribute or upload latest binaries. Bu
- GPU: 1305 MHz (no overvolting, less than official threshold 1050 mV)
- NVIDIA Official Maximum: 1267.2 MHz
- ~~Tested with deko3d compute shaders converted from Maxwell SASS assembly. Single-precision floating point (FP32 FFMA) performance maxes out at 1305 MHz.~~
- 1305 MHz CANNOT be set without charger connected.
- **Modded sys-clk and ReverseNX**(-RT)
- No need to change clocks manually after toggling modes in ReverseNX (Optional)
- To disable this feature, use original version of ReverseNX-RT and remove `/config/sys-clk/ReverseNX_sync.flag`.
- Auto-Boost CPU for faster game loading (Optional)
- Enable CPU Boost (1785 MHz) when CPU Core#3 (System Core) is stressed, especially when the game is loading assets from eMMC/SD card (I/O ops).
- Auto-Boost will be enabled only when charger is connected.
- To disable this feature, remove `/config/sys-clk/boost.flag`.
- Permanent global clock override
- Expected usage: set maximum DRAM clocks for all games and profiles.
- View charger & battery info, toggle charging/fast-charging(2A) in overlay
- Extend battery life expectancy by maintaining battery charge at 40% - 60% and disabling fast charging if possible.
- Known issue: Fast charging toggle will be reset in-game.
- Miscellaneous
- Auto CPU Boost: For faster game loading
- Enable CPU Boost (1785 MHz) when CPU Core#3 (System Core) is stressed, especially when the game is loading assets from eMMC/SD card (I/O ops).
- Auto-Boost will be enabled only when charger is connected.
- Sync ReverseNX Mode: No need to change clocks manually after toggling modes in ReverseNX
- View charger & battery info, toggle charging/fast-charging(2A) in overlay
- Extend battery life expectancy by maintaining battery charge at 40% - 60% and disabling fast charging if possible.
- Known issue: Fast charging toggle will be reset in-game.

View File

@@ -8,7 +8,7 @@ index 3e35c18..6b42bc5 100644
*.nro
+*.DS_Store
diff --git a/Overlay/Makefile b/Overlay/Makefile
index f881ed9..88fca31 100644
index f881ed9..7184bb0 100644
--- a/Overlay/Makefile
+++ b/Overlay/Makefile
@@ -38,7 +38,7 @@ include $(DEVKITPRO)/libnx/switch_rules
@@ -16,17 +16,26 @@ index f881ed9..88fca31 100644
#---------------------------------------------------------------------------------
APP_TITLE := ReverseNX-RT
-APP_VERSION := 1.0.1
+APP_VERSION := 1.0.1-OC
+APP_VERSION := 1.0.2-OC
TARGET := ReverseNX-RT-ovl
BUILD := build
@@ -53,7 +53,7 @@ NO_ICON := 1
#---------------------------------------------------------------------------------
ARCH := -march=armv8-a+crc+crypto -mtune=cortex-a57 -mtp=soft -fPIE
-CFLAGS := -g -Wall -Os -ffunction-sections \
+CFLAGS := -g -Wall -O2 -ffunction-sections \
$(ARCH) $(DEFINES)
CFLAGS += $(INCLUDE) -D__SWITCH__ -DAPP_VERSION="\"$(APP_VERSION)\""
diff --git a/Overlay/libs/Atmosphere-libs b/Overlay/libs/Atmosphere-libs
index 2d522dc..c4d0335 160000
--- a/Overlay/libs/Atmosphere-libs
+++ b/Overlay/libs/Atmosphere-libs
@@ -1 +1 @@
-Subproject commit 2d522dc6a12b2eb5eb3f103a8c5b5126ca301b1a
+Subproject commit c4d0335b79da7207b49abf1988f45b0168b692f0-dirty
+Subproject commit c4d0335b79da7207b49abf1988f45b0168b692f0
diff --git a/Overlay/libs/libtesla b/Overlay/libs/libtesla
index 6628524..640629f 160000
--- a/Overlay/libs/libtesla
@@ -35,15 +44,15 @@ index 6628524..640629f 160000
-Subproject commit 66285245361a02e5480c7bb7dac9ef6449ae6181
+Subproject commit 640629f49f9e8997ef0769b21b26f4fc177c736f
diff --git a/Overlay/source/main.cpp b/Overlay/source/main.cpp
index cec060c..1a6080e 100644
index cec060c..90c020b 100644
--- a/Overlay/source/main.cpp
+++ b/Overlay/source/main.cpp
@@ -2,8 +2,12 @@
@@ -1,9 +1,12 @@
#define TESLA_INIT_IMPL // If you have more than one file using the tesla header, only define this in the main one
#include <tesla.hpp> // The Tesla Header
+#include <atomic>
#include "dmntcht.h"
+#define PROCESS_MANAGEMENT_QLAUNCH_TID 0x0100000000001000ULL
+
bool def = true;
+bool defChanged = false;
bool isDocked = false;
@@ -51,54 +60,76 @@ index cec060c..1a6080e 100644
bool PluginRunning = false;
bool closed = false;
Handle debug;
@@ -17,9 +21,16 @@ bool dmntcht = false;
bool SaltySD = false;
bool bak = false;
bool plugin = false;
+bool sysclkComm = false;
+uint8_t sysclkSyncInfoShowTick = 0;
+constexpr uint8_t sysclkSyncChecksPerSec = 10;
+constexpr uint8_t sysclkSyncFramesPerCheck = 60 / sysclkSyncChecksPerSec;
+constexpr uint8_t sysclkSyncInfoShowInitTick = 3 * sysclkSyncFramesPerCheck;
+uint8_t sysclkSyncRetry = 0;
@@ -20,6 +23,10 @@ bool plugin = false;
char DockedChar[32];
char SystemChar[32];
char PluginChar[36];
+char SysclkChar[36];
+static Service g_sysclkSrv;
+static std::atomic<std::size_t> g_refCnt;
+bool sysclkComm = false;
bool CheckPort () {
Handle saltysd;
@@ -54,6 +65,29 @@ bool isServiceRunning(const char *serviceName) {
@@ -54,6 +61,57 @@ bool isServiceRunning(const char *serviceName) {
}
}
+u64 GetCurrentApplicationId()
+#define SYSCLK_IPC_API_VERSION 1
+#define SYSCLK_IPC_SERVICE_NAME "sysclkOC"
+
+enum SysClkIpcCmd
+{
+ SysClkIpcCmd_GetApiVersion = 0,
+ SysClkIpcCmd_SetReverseNXRTMode = 11,
+};
+
+void sysclkIpcExit(void)
+{
+ if (--g_refCnt == 0)
+ {
+ serviceClose(&g_sysclkSrv);
+ }
+}
+
+Result sysclkIpcInitialize(void)
+{
+ // Copied from sys-clk
+ Result rc = 0;
+ std::uint64_t pid = 0;
+ std::uint64_t tid = 0;
+ rc = pmdmntGetApplicationProcessId(&pid);
+
+ if (rc == 0x20f)
+ {
+ return PROCESS_MANAGEMENT_QLAUNCH_TID;
+ }
+ g_refCnt++;
+
+ rc = pminfoGetProgramId(&tid, pid);
+ if (serviceIsActive(&g_sysclkSrv))
+ return 0;
+
+ if (rc == 0x20f)
+ {
+ return PROCESS_MANAGEMENT_QLAUNCH_TID;
+ }
+ rc = smGetService(&g_sysclkSrv, SYSCLK_IPC_SERVICE_NAME);
+
+ return tid;
+ if (R_FAILED(rc)) sysclkIpcExit();
+
+ return rc;
+}
+
+Result sysclkIpcGetAPIVersion(u32* out_ver)
+{
+ return serviceDispatchOut(&g_sysclkSrv, SysClkIpcCmd_GetApiVersion, *out_ver);
+}
+
+typedef enum
+{
+ ReverseNX_SystemDefault = 0,
+ ReverseNX_Handheld,
+ ReverseNX_Docked,
+ ReverseNX_RTResetToDefault,
+} ReverseNXMode;
+
+Result sysclkIpcSetReverseNXRTMode(ReverseNXMode mode)
+{
+ return serviceDispatchIn(&g_sysclkSrv, SysClkIpcCmd_SetReverseNXRTMode, mode);
+}
+
class GuiTest : public tsl::Gui {
public:
GuiTest(u8 arg1, u8 arg2, bool arg3) { }
@@ -88,6 +122,7 @@ public:
@@ -88,6 +146,7 @@ public:
else {
renderer->drawString(SystemChar, false, x, y+40, 20, renderer->a(0xFFFF));
renderer->drawString(DockedChar, false, x, y+60, 20, renderer->a(0xFFFF));
@@ -106,7 +137,7 @@ index cec060c..1a6080e 100644
}
}
}), 100);
@@ -95,9 +130,10 @@ public:
@@ -95,9 +154,10 @@ public:
if (MAGIC == 0x06BA7E39) {
auto *clickableListItem = new tsl::elm::ListItem("Change system control");
clickableListItem->setClickListener([](u64 keys) {
@@ -118,7 +149,7 @@ index cec060c..1a6080e 100644
if (dmntcht == true) {
dmntchtWriteCheatProcessMemory(def_address, &def, 0x1);
dmntchtReadCheatProcessMemory(def_address, &def, 0x1);
@@ -120,9 +156,10 @@ public:
@@ -120,9 +180,10 @@ public:
auto *clickableListItem2 = new tsl::elm::ListItem("Change mode");
clickableListItem2->setClickListener([](u64 keys) {
@@ -130,7 +161,7 @@ index cec060c..1a6080e 100644
if (dmntcht == true) {
dmntchtWriteCheatProcessMemory(docked_address, &isDocked, 0x1);
dmntchtReadCheatProcessMemory(docked_address, &isDocked, 0x1);
@@ -145,7 +182,7 @@ public:
@@ -145,7 +206,7 @@ public:
else if (SaltySD == true && plugin == true && check == false) {
auto *clickableListItem = new tsl::elm::ListItem("(De)activate plugin");
clickableListItem->setClickListener([](u64 keys) {
@@ -139,93 +170,35 @@ index cec060c..1a6080e 100644
if (bak == false) {
rename("sdmc:/SaltySD/plugins/ReverseNX-RT.elf", "sdmc:/SaltySD/plugins/ReverseNX-RT.elf.bak");
bak = true;
@@ -185,6 +222,40 @@ public:
@@ -179,12 +240,22 @@ public:
closed = true;
}
if (PluginRunning == true) {
- if (i > 59) {
+ if (i > 29) {
if (dmntcht == true) dmntchtReadCheatProcessMemory(docked_address, &isDocked, 0x1);
else if (R_SUCCEEDED(svcDebugActiveProcess(&debug, PID))) {
svcReadDebugProcessMemory(&isDocked, debug, docked_address, 0x1);
svcCloseHandle(debug);
}
+ if (sysclkComm && isDockedChanged && def == false && bak == false)
+ {
+ u64 currentTID = GetCurrentApplicationId();
+ if (currentTID != PROCESS_MANAGEMENT_QLAUNCH_TID)
+ {
+ FILE* sysclkConf = fopen("/config/sys-clk/ReverseNX-RT.conf", "wb+");
+ if (sysclkConf)
+ {
+ uint8_t ctrlState = isDocked + 1;
+ fwrite(&currentTID, 1, sizeof(currentTID), sysclkConf);
+ fwrite(&ctrlState, 1, sizeof(ctrlState), sysclkConf);
+ fclose(sysclkConf);
+ sysclkSyncInfoShowTick = sysclkSyncInfoShowInitTick;
+ }
+ if (sysclkComm && !bak) {
+ if (!def && isDockedChanged) {
+ isDockedChanged = false;
+ sysclkIpcSetReverseNXRTMode(isDocked ? ReverseNX_Docked : ReverseNX_Handheld);
+ }
+ isDockedChanged = false;
+ }
+ if (sysclkComm && defChanged && def == true && bak == false) // change from force mode to system-controlled mode
+ {
+ u64 currentTID = GetCurrentApplicationId();
+ if (currentTID != PROCESS_MANAGEMENT_QLAUNCH_TID)
+ {
+ FILE* sysclkConf = fopen("/config/sys-clk/ReverseNX-RT.conf", "wb+");
+ if (sysclkConf)
+ {
+ uint8_t ctrlState = 3; // ReverseNX_RTResetToDefault
+ fwrite(&currentTID, 1, sizeof(currentTID), sysclkConf);
+ fwrite(&ctrlState, 1, sizeof(ctrlState), sysclkConf);
+ fclose(sysclkConf);
+ sysclkSyncInfoShowTick = sysclkSyncInfoShowInitTick;
+ }
+ if (def && defChanged) {
+ defChanged = false;
+ sysclkIpcSetReverseNXRTMode(ReverseNX_RTResetToDefault);
+ }
+ defChanged = false;
+ }
i = 0;
}
else i++;
@@ -198,11 +269,51 @@ public:
@@ -198,11 +269,10 @@ public:
if (bak == false) sprintf(PluginChar, "ReverseNX-RT plugin is activated.");
else sprintf(PluginChar, "ReverseNX-RT plugin is deactivated.");
-
+
+ if (sysclkComm)
+ {
+ if (sysclkSyncInfoShowTick == sysclkSyncInfoShowInitTick)
+ {
+ if (sysclkSyncRetry >= sysclkSyncInfoShowInitTick)
+ {
+ sprintf(SysclkChar, "Sys-clk Profile Sync: Error!");
+ }
+ else if (i % sysclkSyncFramesPerCheck == 0)
+ {
+ /* Check if conf file is detected by sys-clk */
+ FILE* sysclkConf = fopen("/config/sys-clk/ReverseNX-RT.conf", "r");
+ if (sysclkConf)
+ {
+ fclose(sysclkConf);
+ sprintf(SysclkChar, "Sys-clk Profile Sync: Pending...");
+ sysclkSyncRetry++;
+ }
+ else
+ {
+ sprintf(SysclkChar, "Sys-clk Profile Sync: Success!");
+ sysclkSyncRetry = 0;
+ sysclkSyncInfoShowTick--;
+ }
+ }
+ }
+ else if (sysclkSyncInfoShowTick)
+ {
+ if (i % sysclkSyncFramesPerCheck == 0)
+ sysclkSyncInfoShowTick--;
+ }
+ else
+ {
+ sprintf(SysclkChar, "Sys-clk Profile Sync: ON");
+ }
+ }
+ else
+ {
+ sprintf(SysclkChar, "Sys-clk Profile Sync: OFF");
+ }
}
// Called once every frame to handle inputs not handled by other UI elements
@@ -234,54 +207,31 @@ index cec060c..1a6080e 100644
return false; // Return true here to singal the inputs have been consumed
}
};
@@ -213,6 +324,7 @@ public:
virtual void initServices() override {
smInitialize();
fsdevMountSdmc();
+ pminfoInitialize();
SaltySD = CheckPort();
if (SaltySD == false) return;
@@ -234,6 +346,19 @@ public:
@@ -234,6 +304,13 @@ public:
else return;
}
+ // Check if sys-clk exist
+ temp = fopen("sdmc:/atmosphere/contents/00FF0000636C6BFF/exefs.nsp", "r");
+ if (temp != NULL)
+ {
+ fclose(temp);
+ temp = fopen("sdmc:/config/sys-clk/ReverseNX_sync.flag", "r");
+ if (temp)
+ {
+ // Initialize sysclkIpc
+ if (isServiceRunning(SYSCLK_IPC_SERVICE_NAME)) {
+ uint32_t apiVer;
+ if (R_SUCCEEDED(sysclkIpcInitialize()) && R_SUCCEEDED(sysclkIpcGetAPIVersion(&apiVer)) && apiVer == SYSCLK_IPC_API_VERSION)
+ sysclkComm = true;
+ fclose(temp);
+ }
+ }
+
if (R_FAILED(pmdmntGetApplicationProcessId(&PID))) remove("sdmc:/SaltySD/ReverseNX-RT.hex");
else {
check = true;
@@ -278,12 +403,22 @@ public:
@@ -278,10 +355,13 @@ public:
if (def == true) sprintf(SystemChar, "Controlled by system: Yes");
else sprintf(SystemChar, "Controlled by system: No");
+
+ if (sysclkComm)
+ {
+ sprintf(SysclkChar, "Sys-clk Profile Sync: ON");
+ }
+ else
+ {
+ sprintf(SysclkChar, "Sys-clk Profile Sync: OFF");
+ }
+ sprintf(SysclkChar, "Sys-clk-OC Sync Service: %s", sysclkComm ? "ON" : "OFF");
} // Called at the start to initialize all services necessary for this Overlay
virtual void exitServices() override {
+ sysclkIpcExit();
dmntchtExit();
if (dmntcht == false) svcCloseHandle(debug);
+ pminfoExit();
fsdevUnmountDevice("sdmc");
smExit();
} // Callet at the end to clean up all services previously initialized

View File

@@ -77,8 +77,8 @@ To protect the battery from excessive strain, clocks requested from config may b
| | Handheld | Charging (USB) | Charging (Official) | Docked |
|:-------:|:--------:|:--------------:|:-------------------:|:------:|
| **MEM** | - | - | - | - |
| **CPU** | - | - | - | - |
| **GPU** | 1267 | - | - | - |
| **CPU** | 1963 | - | - | - |
| **GPU** | 921 | 1267 | - | - |
## Installation
@@ -180,6 +180,8 @@ The `[values]` section allows you to alter timings in sys-clk, you should not ne
| Key | Desc | Default |
|:-----------------------:|-------------------------------------------------------------------------------|:-------:|
|**auto_cpu_boost** | Auto-boost CPU when system Core #3 utilization is high | ON |
|**sync_reversenx_mode** | Sync nominal profile (mode) with ReverseNX (-Tool and -RT) | ON |
|**temp_log_interval_ms** | Defines how often sys-clk log temperatures, in milliseconds (`0` to disable) | 0 ms |
|**csv_write_interval_ms**| Defines how often sys-clk writes to the CSV, in milliseconds (`0` to disable) | 0 ms |
|**poll_interval_ms** | Defines how fast sys-clk checks and applies profiles, in milliseconds | 500 ms |

View File

@@ -30,6 +30,7 @@ Result sysclkIpcGetProfiles(u64 tid, SysClkTitleProfileList* out_profiles);
Result sysclkIpcSetProfiles(u64 tid, SysClkTitleProfileList* profiles);
Result sysclkIpcGetConfigValues(SysClkConfigValueList* out_configValues);
Result sysclkIpcSetConfigValues(SysClkConfigValueList* configValues);
Result sysclkIpcSetReverseNXRTMode(ReverseNXMode mode);
static inline Result sysclkIpcRemoveOverride(SysClkModule module)
{

View File

@@ -57,6 +57,7 @@ typedef enum
ReverseNX_SystemDefault = 0,
ReverseNX_NotFound = 0,
ReverseNX_NotValid = 0,
ReverseNX_GotValue = 0,
ReverseNX_Handheld,
ReverseNX_Docked,
ReverseNX_RTResetToDefault,
@@ -65,11 +66,7 @@ typedef enum
typedef struct
{
bool systemCoreBoostCPU;
bool systemCoreCheckStuck;
ReverseNXMode reverseNXMode;
uint64_t tickWaitTimeMs;
ReverseNXMode reverseNXMode;
uint32_t maxMEMFreq;
} SysClkOcExtra;
@@ -81,7 +78,9 @@ typedef struct
};
} SysClkTitleProfileList;
#define SYSCLK_GPU_HANDHELD_MAX_HZ 1267200000
#define SYSCLK_CPU_HANDHELD_MAX_HZ 1963500000
#define SYSCLK_GPU_HANDHELD_MAX_HZ 921600000
#define SYSCLK_GPU_CHARGING_USB_MAX_HZ 1267200000
extern uint32_t sysclk_g_freq_table_mem_hz[];
extern uint32_t sysclk_g_freq_table_cpu_hz[];

View File

@@ -17,6 +17,8 @@ typedef enum {
SysClkConfigValue_PollingIntervalMs = 0,
SysClkConfigValue_TempLogIntervalMs,
SysClkConfigValue_CsvWriteIntervalMs,
SysClkConfigValue_AutoCPUBoost,
SysClkConfigValue_SyncReverseNXMode,
SysClkConfigValue_EnumMax,
} SysClkConfigValue;
@@ -34,6 +36,10 @@ static inline const char* sysclkFormatConfigValue(SysClkConfigValue val, bool pr
return pretty ? "Temperature logging interval (ms)" : "temp_log_interval_ms";
case SysClkConfigValue_CsvWriteIntervalMs:
return pretty ? "CSV write interval (ms)" : "csv_write_interval_ms";
case SysClkConfigValue_AutoCPUBoost:
return pretty ? "Enable Auto CPU Boost" : "auto_cpu_boost";
case SysClkConfigValue_SyncReverseNXMode:
return pretty ? "Enable ReverseNX Mode Sync" : "sync_reversenx_mode";
default:
return NULL;
}
@@ -44,10 +50,13 @@ static inline uint64_t sysclkDefaultConfigValue(SysClkConfigValue val)
switch(val)
{
case SysClkConfigValue_PollingIntervalMs:
return 300ULL;
return 500ULL;
case SysClkConfigValue_TempLogIntervalMs:
case SysClkConfigValue_CsvWriteIntervalMs:
return 0ULL;
case SysClkConfigValue_AutoCPUBoost:
case SysClkConfigValue_SyncReverseNXMode:
return 1ULL;
default:
return 0ULL;
}
@@ -62,6 +71,9 @@ static inline uint64_t sysclkValidConfigValue(SysClkConfigValue val, uint64_t in
case SysClkConfigValue_TempLogIntervalMs:
case SysClkConfigValue_CsvWriteIntervalMs:
return true;
case SysClkConfigValue_AutoCPUBoost:
case SysClkConfigValue_SyncReverseNXMode:
return (input & 0x1) == input;
default:
return false;
}

View File

@@ -13,8 +13,8 @@
#include <stdint.h>
#include "clocks.h"
#define SYSCLK_IPC_API_VERSION 0
#define SYSCLK_IPC_SERVICE_NAME "sys:clk"
#define SYSCLK_IPC_API_VERSION 1
#define SYSCLK_IPC_SERVICE_NAME "sysclkOC"
enum SysClkIpcCmd
{
@@ -29,6 +29,7 @@ enum SysClkIpcCmd
SysClkIpcCmd_SetOverride = 8,
SysClkIpcCmd_GetConfigValues = 9,
SysClkIpcCmd_SetConfigValues = 10,
SysClkIpcCmd_SetReverseNXRTMode = 11,
};

View File

@@ -114,3 +114,8 @@ Result sysclkIpcSetConfigValues(SysClkConfigValueList* configValues)
{
return serviceDispatchIn(&g_sysclkSrv, SysClkIpcCmd_SetConfigValues, *configValues);
}
Result sysclkIpcSetReverseNXRTMode(ReverseNXMode mode)
{
return serviceDispatchIn(&g_sysclkSrv, SysClkIpcCmd_SetReverseNXRTMode, mode);
}

View File

@@ -205,8 +205,12 @@ void StatusTab::updateWarningForProfile(SysClkProfile profile, bool animated)
case SysClkProfile_Handheld:
if (this->warningLabel->isHidden())
this->warningLabel->show([](){});
this->warningLabel->setText("\uE140 Maximum GPU frequency is " + formatFreq(SYSCLK_GPU_HANDHELD_MAX_HZ) + " because you're in handheld mode.");
this->warningLabel->setText("\uE140 Handheld Mode: Max CPU freq: " + formatFreq(SYSCLK_CPU_HANDHELD_MAX_HZ) + ", Max GPU freq: " + formatFreq(SYSCLK_GPU_HANDHELD_MAX_HZ));
break;
case SysClkProfile_HandheldChargingUSB:
if (this->warningLabel->isHidden())
this->warningLabel->show([](){});
this->warningLabel->setText("\uE140 USB Charging Mode: Max GPU freq: " + formatFreq(SYSCLK_GPU_CHARGING_USB_MAX_HZ));
break;
default:
if (!this->warningLabel->isHidden())

View File

@@ -14,12 +14,14 @@
MiscGui::MiscGui()
{
this->configList = new SysClkConfigValueList {};
this->chargeInfo = new ChargeInfo {};
this->i2cInfo = new I2cInfo {};
}
MiscGui::~MiscGui()
{
delete this->configList;
delete this->chargeInfo;
delete this->i2cInfo;
}
@@ -28,11 +30,36 @@ void MiscGui::preDraw(tsl::gfx::Renderer* render)
{
BaseMenuGui::preDraw(render);
render->drawString(this->infoOutput, false, 40, 300, SMALL_TEXT_SIZE, DESC_COLOR);
render->drawString(this->infoOutput, false, 40, 440, SMALL_TEXT_SIZE, DESC_COLOR);
}
tsl::elm::ToggleListItem* MiscGui::addConfigToggle(SysClkConfigValue configVal, std::string labelName) {
tsl::elm::ToggleListItem* toggle = new tsl::elm::ToggleListItem(labelName, this->configList->values[configVal]);
toggle->setStateChangedListener([this, configVal](bool state) {
this->configList->values[configVal] = uint64_t(state);
Result rc = sysclkIpcSetConfigValues(this->configList);
if (R_FAILED(rc))
FatalGui::openWithResultCode("sysclkIpcSetConfigValues", rc);
this->lastContextUpdate = armGetSystemTick();
});
this->listElement->addItem(toggle);
return toggle;
}
void MiscGui::updateConfigToggle(tsl::elm::ToggleListItem *toggle, SysClkConfigValue configVal) {
if (toggle != nullptr) {
toggle->setState(this->configList->values[configVal]);
}
}
void MiscGui::listUI()
{
sysclkIpcGetConfigValues(this->configList);
this->cpuBoostToggle = addConfigToggle(SysClkConfigValue_AutoCPUBoost, "Auto CPU Boost");
this->syncModeToggle = addConfigToggle(SysClkConfigValue_SyncReverseNXMode, "Sync ReverseNX Mode");
// Charging
this->chargingToggle = new tsl::elm::ToggleListItem("Charging", false);
chargingToggle->setStateChangedListener([this](bool state) {
@@ -63,9 +90,14 @@ void MiscGui::listUI()
this->listElement->addItem(this->fastChargingToggle);
}
void MiscGui::update()
{
BaseMenuGui::update();
void MiscGui::refresh() {
BaseMenuGui::refresh();
if (this->context) {
sysclkIpcGetConfigValues(this->configList);
updateConfigToggle(this->cpuBoostToggle, SysClkConfigValue_AutoCPUBoost);
updateConfigToggle(this->syncModeToggle, SysClkConfigValue_SyncReverseNXMode);
}
if (++frameCounter >= 60)
{

View File

@@ -20,7 +20,7 @@ class MiscGui : public BaseMenuGui
~MiscGui();
void preDraw(tsl::gfx::Renderer* render) override;
void listUI() override;
void update() override;
void refresh() override;
protected:
typedef enum {
@@ -307,8 +307,12 @@ class MiscGui : public BaseMenuGui
return PsmIsFastCharging() == enable;
}
tsl::elm::ToggleListItem *chargingToggle, *fastChargingToggle;
tsl::elm::ToggleListItem* addConfigToggle(SysClkConfigValue, std::string);
void updateConfigToggle(tsl::elm::ToggleListItem*, SysClkConfigValue);
tsl::elm::ToggleListItem *cpuBoostToggle, *syncModeToggle, *chargingToggle, *fastChargingToggle;
SysClkConfigValueList* configList;
ChargeInfo* chargeInfo;
I2cInfo* i2cInfo;
bool isEnoughPowerSupplied = false;

View File

@@ -1,5 +1,5 @@
{
"name": "sys:clk",
"name": "sysclkOC",
"title_id": "0x00FF0000636C6BFF",
"title_id_range_min": "0x00FF0000636C6BFF",
"title_id_range_max": "0x00FF0000636C6BFF",
@@ -18,7 +18,7 @@
"*"
],
"service_host": [
"sys:clk"
"sysclkOC"
],
"kernel_capabilities": [
{

View File

@@ -60,7 +60,6 @@ ClockManager::ClockManager()
this->oc = new SysClkOcExtra;
this->oc->systemCoreBoostCPU = false;
this->oc->reverseNXMode = ReverseNX_NotFound;
this->oc->tickWaitTimeMs = 0;
this->oc->maxMEMFreq = 0;
}
@@ -77,7 +76,7 @@ bool ClockManager::IsCpuBoostMode()
return (confId == 0x92220009 || confId == 0x9222000A);
}
bool ClockManager::IsReverseNXEnabled()
bool ClockManager::IsReverseNXModeValid()
{
return (this->oc->reverseNXMode);
}
@@ -113,7 +112,7 @@ uint32_t ClockManager::GetHz(SysClkModule module)
hz = this->config->GetAutoClockHz(this->context->applicationId, module, this->context->profile);
/* Return pre-set hz if ReverseNX is enabled, downclock is disabled when realProfile == Docked */
if (!hz && IsReverseNXEnabled())
if (!hz && IsReverseNXModeValid())
{
switch(module)
{
@@ -198,10 +197,11 @@ void ClockManager::Tick()
void ClockManager::WaitForNextTick()
{
/* Self-check system core (#3) usage via idleticks at intervals (Not enabled at higher CPU freq or without charger) */
this->oc->tickWaitTimeMs = this->GetConfig()->GetConfigValue(SysClkConfigValue_PollingIntervalMs);
uint64_t tickWaitTimeNs = this->oc->tickWaitTimeMs * 1000000ULL;
uint64_t tickWaitTimeMs = this->GetConfig()->GetConfigValue(SysClkConfigValue_PollingIntervalMs);
uint64_t tickWaitTimeNs = tickWaitTimeMs * 1000000ULL;
if ( FileUtils::IsBoostEnabled()
uint64_t isAutoBoostEnabled = this->GetConfig()->GetConfigValue(SysClkConfigValue_AutoCPUBoost);
if ( isAutoBoostEnabled
&& this->context->realProfile != SysClkProfile_Handheld
&& this->context->enabled
&& this->context->freqs[SysClkModule_CPU] <= CPU_BOOST_FREQ)
@@ -214,7 +214,7 @@ void ClockManager::WaitForNextTick()
/* Convert idletick to free% */
/* If CPU core usage is 0%, then idletick = 19'200'000 per sec */
uint64_t systemCoreIdleTick = systemCoreIdleTickNext - systemCoreIdleTickPrev;
uint64_t freeIdleTick = 19'200 * this->oc->tickWaitTimeMs;
uint64_t freeIdleTick = 19'200 * tickWaitTimeMs;
uint8_t freePerc = systemCoreIdleTick / (freeIdleTick / 100);
uint8_t systemCoreBoostFreeThreshold = 5;
@@ -244,62 +244,11 @@ void ClockManager::WaitForNextTick()
{
svcSleepThread(tickWaitTimeNs);
}
/* Signal that systemCore is not busy */
// {
// std::scoped_lock lock{this->systemCoreStuckCountMutex};
// this->oc->systemCoreStuckCount = 0;
// }
}
/* Tricky part, see IpcService implementation of calling static member function */
// void ClockManager::CheckSystemCoreStuck(void *arg)
// {
// ClockManager* clkMgr = (ClockManager*)arg;
// while (clkMgr->oc->systemCoreCheckStuck)
// {
// svcSleepThread(clkMgr->oc->tickWaitTimeMs * 1000000ULL);
// if (clkMgr->context->freqs[SysClkModule_CPU] >= clkMgr->CPU_BOOST_FREQ)
// continue;
// // Core #0,1,2 will check if Core#3 is stuck (threshold count = 2*3 = 6)
// {
// std::scoped_lock lock{clkMgr->systemCoreStuckCountMutex};
// if (++clkMgr->oc->systemCoreStuckCount > 6)
// {
// clkMgr->oc->systemCoreStuckCount = -6;
// Clocks::SetHz(SysClkModule_CPU, clkMgr->CPU_BOOST_FREQ);
// }
// }
// }
// }
// void ClockManager::StartCheckSystemCore()
// {
// this->oc->systemCoreCheckStuck = true;
// threadCreate(&this->t_CheckSystemCoreStuck_0, this->CheckSystemCoreStuck, this, NULL, 0x1000, 0x20, 0);
// threadCreate(&this->t_CheckSystemCoreStuck_1, this->CheckSystemCoreStuck, this, NULL, 0x1000, 0x20, 1);
// threadCreate(&this->t_CheckSystemCoreStuck_2, this->CheckSystemCoreStuck, this, NULL, 0x1000, 0x20, 2);
// threadStart(&this->t_CheckSystemCoreStuck_0);
// threadStart(&this->t_CheckSystemCoreStuck_1);
// threadStart(&this->t_CheckSystemCoreStuck_2);
// }
// void ClockManager::StopCheckSystemCore()
// {
// this->oc->systemCoreCheckStuck = false;
// threadWaitForExit(&this->t_CheckSystemCoreStuck_0);
// threadWaitForExit(&this->t_CheckSystemCoreStuck_1);
// threadWaitForExit(&this->t_CheckSystemCoreStuck_2);
// threadClose(&this->t_CheckSystemCoreStuck_0);
// threadClose(&this->t_CheckSystemCoreStuck_1);
// threadClose(&this->t_CheckSystemCoreStuck_2);
// }
SysClkProfile ClockManager::ReverseNXProfileHandler()
{
if (!IsReverseNXEnabled())
if (!IsReverseNXModeValid())
{
return this->context->realProfile;
}
@@ -318,52 +267,33 @@ SysClkProfile ClockManager::ReverseNXProfileHandler()
return this->context->realProfile;
}
ReverseNXMode ClockManager::ReverseNXFileHandler(bool checkTool, const char* filePath)
ReverseNXMode ClockManager::ReverseNXFileHandler(const char* filePath)
{
FILE *readFile;
readFile = fopen(filePath, "rb");
if (!readFile)
{
return ReverseNX_NotFound;
}
if (checkTool)
{
uint64_t magicDocked = 0xD65F03C0320003E0;
uint64_t magicHandheld = 0xD65F03C052A00000;
uint64_t magicDocked = 0xD65F03C0320003E0;
uint64_t magicHandheld = 0xD65F03C052A00000;
uint64_t readBuffer = 0;
fread(&readBuffer, 1, sizeof(readBuffer), readFile);
fclose(readFile);
uint64_t readBuffer = 0;
fread(&readBuffer, 1, sizeof(readBuffer), readFile);
fclose(readFile);
if (!memcmp(&readBuffer, &magicDocked, sizeof(readBuffer)))
return ReverseNX_Docked;
if (!memcmp(&readBuffer, &magicDocked, sizeof(readBuffer)))
return ReverseNX_Docked;
if (!memcmp(&readBuffer, &magicHandheld, sizeof(readBuffer)))
return ReverseNX_Handheld;
}
else // checkRT
{
uint64_t tidBuffer = 0;
uint8_t ctrlBuffer = 0;
fread(&tidBuffer, 1, sizeof(tidBuffer), readFile);
fread(&ctrlBuffer, 1, sizeof(ctrlBuffer), readFile);
fclose(readFile);
remove(filePath);
if (!memcmp(&this->context->applicationId, &tidBuffer, sizeof(tidBuffer)))
{
return static_cast<ReverseNXMode>(ctrlBuffer);
}
}
if (!memcmp(&readBuffer, &magicHandheld, sizeof(readBuffer)))
return ReverseNX_Handheld;
return ReverseNX_NotValid;
}
void ClockManager::CheckReverseNXTool()
{
bool shouldCheckReverseNXTool = FileUtils::IsReverseNXSyncEnabled() && FileUtils::ExistReverseNXTool();
bool shouldCheckReverseNXTool = FileUtils::ExistReverseNXTool();
if (!shouldCheckReverseNXTool)
return;
@@ -375,13 +305,13 @@ void ClockManager::CheckReverseNXTool()
/* Check global override */
snprintf(asmFilePath, sizeof(asmFilePath), "/SaltySD/patches/%s", asmFileName);
getMode = ReverseNXFileHandler(true, asmFilePath);
getMode = ReverseNXFileHandler(asmFilePath);
if (!getMode)
{
/* Check per-game override */
snprintf(asmFilePath, sizeof(asmFilePath), "/SaltySD/patches/%016lX/%s", this->context->applicationId, asmFileName);
getMode = ReverseNXFileHandler(true, asmFilePath);
getMode = ReverseNXFileHandler(asmFilePath);
}
}
@@ -390,13 +320,9 @@ void ClockManager::CheckReverseNXTool()
bool ClockManager::CheckReverseNXRT()
{
bool shouldCheckReverseNXRT = FileUtils::IsReverseNXSyncEnabled();
if (!shouldCheckReverseNXRT)
return false;
bool shouldAdjustProfile = false;
ReverseNXMode getMode = ReverseNXFileHandler(false, FILE_REVERSENX_RT_CONF_PATH);
ReverseNXMode getMode = this->GetConfig()->GetReverseNXRTModeAndClear();
if (getMode)
{
this->oc->reverseNXMode = getMode;
@@ -415,6 +341,7 @@ bool ClockManager::RefreshContext()
{
bool hasChanged = false;
bool enabled = this->GetConfig()->Enabled();
uint64_t isReverseNXSyncEnabled = this->GetConfig()->GetConfigValue(SysClkConfigValue_SyncReverseNXMode);
if(enabled != this->context->enabled)
{
this->context->enabled = enabled;
@@ -430,8 +357,10 @@ bool ClockManager::RefreshContext()
hasChanged = true;
/* Clear ReverseNX state and recheck -Tool patches*/
this->oc->reverseNXMode = ReverseNX_SystemDefault;
CheckReverseNXTool();
if (isReverseNXSyncEnabled) {
this->oc->reverseNXMode = ReverseNX_SystemDefault;
CheckReverseNXTool();
}
}
SysClkProfile profile = Clocks::GetCurrentProfile();
@@ -460,9 +389,11 @@ bool ClockManager::RefreshContext()
Clocks::ResetToStock();
/* Check ReverseNX-RT and adjust nominal profile when context changes */
hasChanged |= CheckReverseNXRT();
if (hasChanged)
this->context->profile = ReverseNXProfileHandler();
if (isReverseNXSyncEnabled) {
hasChanged |= CheckReverseNXRT();
if (hasChanged)
this->context->profile = ReverseNXProfileHandler();
}
std::uint32_t hz = 0;
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++)

View File

@@ -50,20 +50,13 @@ class ClockManager
const uint32_t CPU_BOOST_FREQ = 1785'000'000;
bool IsCpuBoostMode();
bool IsReverseNXEnabled();
bool IsReverseNXModeValid();
bool IsReverseNXDocked();
uint32_t GetHz(SysClkModule);
SysClkProfile ReverseNXProfileHandler();
ReverseNXMode ReverseNXFileHandler(bool, const char*);
ReverseNXMode ReverseNXFileHandler(const char*);
void CheckReverseNXTool();
bool CheckReverseNXRT();
// LockableMutex systemCoreStuckCountMutex;
// Thread t_CheckSystemCoreStuck_0, t_CheckSystemCoreStuck_1, t_CheckSystemCoreStuck_2;
// static void CheckSystemCoreStuck(void *arg);
// void StartCheckSystemCore();
// void StopCheckSystemCore();
};

View File

@@ -297,12 +297,19 @@ std::uint32_t Clocks::GetNearestHz(SysClkModule module, SysClkProfile profile, s
std::uint32_t Clocks::GetMaxAllowedHz(SysClkModule module, SysClkProfile profile)
{
if(module == SysClkModule_GPU)
{
if(profile < SysClkProfile_HandheldCharging)
{
return SYSCLK_GPU_HANDHELD_MAX_HZ;
}
switch (module) {
case SysClkModule_CPU:
if (profile == SysClkProfile_Handheld)
return SYSCLK_CPU_HANDHELD_MAX_HZ;
break;
case SysClkModule_GPU:
if (profile == SysClkProfile_Handheld)
return SYSCLK_GPU_HANDHELD_MAX_HZ;
if (profile == SysClkProfile_HandheldChargingUSB)
return SYSCLK_GPU_CHARGING_USB_MAX_HZ;
break;
default:
break;
}
return 0;

View File

@@ -35,6 +35,8 @@ Config::Config(std::string path)
{
this->configValues[i] = sysclkDefaultConfigValue((SysClkConfigValue)i);
}
this->reverseNXRTMode = ReverseNX_NotFound;
}
Config::~Config()
@@ -480,3 +482,15 @@ bool Config::SetConfigValues(SysClkConfigValueList* configValues, bool immediate
return true;
}
ReverseNXMode Config::GetReverseNXRTModeAndClear() {
std::scoped_lock lock{this->reverseNXRTMutex};
ReverseNXMode mode = this->reverseNXRTMode;
this->reverseNXRTMode = ReverseNX_GotValue;
return mode;
}
void Config::SetReverseNXRTMode(ReverseNXMode mode) {
std::scoped_lock lock{this->reverseNXRTMutex};
this->reverseNXRTMode = mode;
}

View File

@@ -47,6 +47,8 @@ class Config
const char* GetConfigValueName(SysClkConfigValue val, bool pretty);
void GetConfigValues(SysClkConfigValueList* out_configValues);
bool SetConfigValues(SysClkConfigValueList* configValues, bool immediate);
ReverseNXMode GetReverseNXRTModeAndClear();
void SetReverseNXRTMode(ReverseNXMode);
protected:
void Load();
void Close();
@@ -63,7 +65,9 @@ class Config
time_t mtime;
LockableMutex configMutex;
LockableMutex overrideMutex;
LockableMutex reverseNXRTMutex;
std::atomic_bool enabled;
std::uint32_t overrideFreqs[SysClkModule_EnumMax];
std::uint64_t configValues[SysClkConfigValue_EnumMax];
ReverseNXMode reverseNXRTMode;
};

View File

@@ -18,9 +18,7 @@ static LockableMutex g_log_mutex;
static LockableMutex g_csv_mutex;
static std::atomic_bool g_has_initialized = false;
static bool g_log_enabled = false;
static bool g_boost_enabled = false;
static bool g_reversenx_tool_exist = false;
static bool g_reversenx_sync_enabled = false;
static std::uint64_t g_last_flag_check = 0;
extern "C" void __libnx_init_time(void);
@@ -136,20 +134,6 @@ void FileUtils::RefreshFlags(bool force)
void FileUtils::InitCheckFlags()
{
FILE *file;
file = fopen(FILE_BOOST_FLAG_PATH, "r");
if (file)
{
g_boost_enabled = true;
fclose(file);
}
file = fopen(FILE_REVERSENX_SYNC_FLAG_PATH, "r");
if (file)
{
g_reversenx_sync_enabled = true;
fclose(file);
}
file = fopen(FILE_SALTYNX_PATH, "r");
if (file)
{
@@ -158,21 +142,11 @@ void FileUtils::InitCheckFlags()
}
}
bool FileUtils::IsBoostEnabled()
{
return g_boost_enabled;
}
bool FileUtils::ExistReverseNXTool()
{
return g_reversenx_tool_exist;
}
bool FileUtils::IsReverseNXSyncEnabled()
{
return g_reversenx_sync_enabled;
}
void FileUtils::InitializeAsync()
{
Thread initThread = {0};

View File

@@ -22,9 +22,7 @@
#define FILE_CONTEXT_CSV_PATH FILE_CONFIG_DIR "/context.csv"
#define FILE_LOG_FLAG_PATH FILE_CONFIG_DIR "/log.flag"
#define FILE_LOG_FILE_PATH FILE_CONFIG_DIR "/log.txt"
#define FILE_BOOST_FLAG_PATH FILE_CONFIG_DIR "/boost.flag"
#define FILE_SALTYNX_PATH "/atmosphere/contents/0000000000534C56/flags/boot2.flag" // Just check for SaltyNX boot flag
#define FILE_REVERSENX_SYNC_FLAG_PATH FILE_CONFIG_DIR "/ReverseNX_sync.flag"
#define FILE_REVERSENX_RT_CONF_PATH FILE_CONFIG_DIR "/ReverseNX-RT.conf"
class FileUtils
@@ -34,8 +32,6 @@ class FileUtils
static Result Initialize();
static bool IsInitialized();
static bool IsLogEnabled();
static bool IsBoostEnabled();
static bool IsReverseNXSyncEnabled();
static bool ExistReverseNXTool();
static void InitializeAsync();
static void LogLine(const char *format, ...);

View File

@@ -153,6 +153,13 @@ Result IpcService::ServiceHandlerFunc(void* arg, const IpcServerRequest* r, u8*
return ipcSrv->SetConfigValues((SysClkConfigValueList*)r->data.ptr);
}
break;
case SysClkIpcCmd_SetReverseNXRTMode:
if (r->data.size >= sizeof(ReverseNXMode)) {
ReverseNXMode mode = *((ReverseNXMode*)r->data.ptr);
return ipcSrv->SetReverseNXRTMode(mode);
}
break;
}
return SYSCLK_ERROR(Generic);
@@ -288,4 +295,10 @@ Result IpcService::SetConfigValues(SysClkConfigValueList* configValues)
}
return 0;
}
}
Result IpcService::SetReverseNXRTMode(ReverseNXMode mode) {
Config* config = ClockManager::GetInstance()->GetConfig();
config->SetReverseNXRTMode(mode);
return 0;
}

View File

@@ -36,6 +36,7 @@ class IpcService
Result SetOverride(SysClkIpc_SetOverride_Args* args);
Result GetConfigValues(SysClkConfigValueList* out_configValues);
Result SetConfigValues(SysClkConfigValueList* configValues);
Result SetReverseNXRTMode(ReverseNXMode mode);
bool running;
Thread thread;