sysclk: remove old hocclk, bump version

This commit is contained in:
souldbminersmwc
2026-04-01 15:58:40 -04:00
parent 80fa802e88
commit e20bafd6ab
199 changed files with 13967 additions and 657 deletions

View File

@@ -0,0 +1,237 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#include <nxExt.h>
#include <sysclk.h>
#include <switch.h>
#include <pwm.h>
#include <registers.h>
#include <battery.h>
#include "display_refresh_rate.hpp"
#include <rgltr.h>
#include <notification.h>
#include "board.hpp"
#include "board_fuse.hpp"
#include "board_load.hpp"
#include "board_volt.hpp"
#include "board_misc.hpp"
#include "../soctherm.hpp"
#include "../integrations.hpp"
#include "../file_utils.hpp"
namespace board {
SysClkSocType gSocType;
u8 gDramID;
HorizonOCConsoleType gConsoleType = HorizonOCConsoleType_Iowa;
FuseData fuseData;
u8 speedoBracket;
PwmChannelSession iCon;
u32 fd = 0, fd2 = 0;
void FetchHardwareInfos() {
ReadFuses(fuseData);
SetGpuBracket(fuseData.gpuSpeedo, speedoBracket);
u64 sku = 0, dramID = 0;
Result rc = splInitialize();
ASSERT_RESULT_OK(rc, "splInitialize");
rc = splGetConfig(SplConfigItem_HardwareType, &sku);
ASSERT_RESULT_OK(rc, "splGetConfig");
rc = splGetConfig(SplConfigItem_DramId, &dramID);
ASSERT_RESULT_OK(rc, "splGetConfig");
gDramID = dramID;
splExit();
switch(sku) {
case 2 ... 5:
gSocType = SysClkSocType_Mariko;
break;
default:
gSocType = SysClkSocType_Erista;
}
if (gSocType == SysClkSocType_Mariko) {
CacheGpuVoltTable();
}
gConsoleType = static_cast<HorizonOCConsoleType>(sku);
}
/* TODO: Check for config */
void Initialize() {
Result rc = 0;
if (HOSSVC_HAS_CLKRST) {
rc = clkrstInitialize();
ASSERT_RESULT_OK(rc, "clkrstInitialize");
} else {
rc = pcvInitialize();
ASSERT_RESULT_OK(rc, "pcvInitialize");
}
rc = apmExtInitialize();
ASSERT_RESULT_OK(rc, "apmExtInitialize");
rc = psmInitialize();
ASSERT_RESULT_OK(rc, "psmInitialize");
if(HOSSVC_HAS_TC) {
rc = tcInitialize();
ASSERT_RESULT_OK(rc, "tcInitialize");
}
rc = max17050Initialize();
ASSERT_RESULT_OK(rc, "max17050Initialize");
rc = tmp451Initialize();
ASSERT_RESULT_OK(rc, "tmp451Initialize");
Result nvCheck = 1;
if (R_SUCCEEDED(nvInitialize())) {
nvCheck = nvOpen(&fd, "/dev/nvhost-ctrl-gpu");
// Result nvCheck_sched = nvOpen(&fd2, "/dev/nvsched-ctrl");
// /* This can be improved. */
// NvSchedSucceed(nvCheck_sched);
// if (R_SUCCEEDED(nvCheck_sched)) {
// SchedSetFD2(fd2);
// }
}
rc = rgltrInitialize();
ASSERT_RESULT_OK(rc, "rgltrInitialize");
rc = pmdmntInitialize();
ASSERT_RESULT_OK(rc, "pmdmntInitialize");
StartLoad(nvCheck, fd);
batteryInfoInitialize();
FetchHardwareInfos();
soctherm::Initialize();
Result pwmCheck = 1;
if (hosversionAtLeast(6,0,0) && R_SUCCEEDED(pwmInitialize())) {
pwmCheck = pwmOpenSession2(&iCon, 0x3D000001);
}
StartMiscThread(pwmCheck, &iCon);
u64 clkVirtAddr, dsiVirtAddr, outsize;
rc = svcQueryMemoryMapping(&clkVirtAddr, &outsize, 0x60006000, 0x1000);
ASSERT_RESULT_OK(rc, "svcQueryMemoryMapping (clk)");
rc = svcQueryMemoryMapping(&dsiVirtAddr, &outsize, 0x54300000, 0x40000);
ASSERT_RESULT_OK(rc, "svcQueryMemoryMapping (dsi)");
display::DisplayRefreshConfig cfg = {.clkVirtAddr = clkVirtAddr, .dsiVirtAddr = dsiVirtAddr, .isLite = (GetConsoleType() == HorizonOCConsoleType_Hoag), .isRetroSUPER = integrations::GetRETROSuperStatus()};
display::Initialize(&cfg);
CacheDfllData();
}
void Exit() {
if (HOSSVC_HAS_CLKRST) {
clkrstExit();
} else {
pcvExit();
}
apmExtExit();
psmExit();
if (HOSSVC_HAS_TC) {
tcExit();
}
max17050Exit();
tmp451Exit();
ExitLoad();
ExitMiscThread();
pwmChannelSessionClose(&iCon);
pwmExit();
rgltrExit();
batteryInfoExit();
pmdmntExit();
display::Shutdown();
nvExit();
}
SysClkSocType GetSocType() {
return gSocType;
}
HorizonOCConsoleType GetConsoleType() {
return gConsoleType;
}
u8 GetDramID() {
return gDramID;
}
bool IsDram8GB() {
SecmonArgs args = {};
args.X[0] = 0xF0000002;
args.X[1] = MC_REGISTER_BASE + MC_EMEM_CFG_0;
svcCallSecureMonitor(&args);
if (args.X[1] == (MC_REGISTER_BASE + MC_EMEM_CFG_0)) { // if param 1 is identical read failed
notification::writeNotification("Horizon OC\nSecmon read failed!\n This may be a hardware issue!");
return false;
}
return args.X[1] == 0x00002000 ? true : false;
}
/* TODO: Put this into a different file. */
void SetDisplayRefreshDockedState(bool docked) {
if (GetConsoleType() != HorizonOCConsoleType_Hoag) {
display::SetDockedState(docked);
}
}
FuseData *GetFuseData() {
return &fuseData;
}
u8 GetGpuSpeedoBracket() {
return speedoBracket;
}
bool IsUsingRetroSuperDisplay() {
return false; /* stub for now. */
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#include <switch.h>
#include <sysclk.h>
#include "board_fuse.hpp"
#include "board_load.hpp"
#include "board_name.hpp"
#include "board_freq.hpp"
#include "board_sensor.hpp"
#include "board_volt.hpp"
#include "board_profile.hpp"
#define HOSSVC_HAS_CLKRST (hosversionAtLeast(8,0,0))
#define HOSSVC_HAS_TC (hosversionAtLeast(5,0,0))
namespace board {
void Initialize();
void Exit();
SysClkSocType GetSocType();
HorizonOCConsoleType GetConsoleType();
u8 GetDramID();
u8 GetGpuSpeedoBracket();
bool IsDram8GB();
void SetDisplayRefreshDockedState(bool docked);
FuseData *GetFuseData();
bool IsUsingRetroSuperDisplay();
}

View File

@@ -0,0 +1,230 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#include <switch.h>
#include <sysclk.h>
#include <nxExt.h>
#include "display_refresh_rate.hpp"
#include "board.hpp"
#include "board_name.hpp"
#include "../errors.hpp"
namespace board {
PcvModule GetPcvModule(SysClkModule sysclkModule) {
switch (sysclkModule) {
case SysClkModule_CPU:
return PcvModule_CpuBus;
case SysClkModule_GPU:
return PcvModule_GPU;
case SysClkModule_MEM:
return PcvModule_EMC;
default:
ASSERT_ENUM_VALID(SysClkModule, sysclkModule);
}
return static_cast<PcvModule>(0);
}
PcvModuleId GetPcvModuleId(SysClkModule sysclkModule) {
PcvModuleId pcvModuleId;
Result rc = pcvGetModuleId(&pcvModuleId, GetPcvModule(sysclkModule));
ASSERT_RESULT_OK(rc, "pcvGetModuleId");
return pcvModuleId;
}
void ClkrstSetHz(ClkrstSession &session, u32 hz) {
ASSERT_RESULT_OK(clkrstSetClockRate(&session, hz), "clkrstSetClockRate");
}
void PcvSetHz(PcvModule moduleID, u32 hz) {
ASSERT_RESULT_OK(pcvSetClockRate(moduleID, hz), "pcvSetClockRate");
}
void SetHz(SysClkModule module, u32 hz) {
Result rc = 0;
bool usesGovenor = module > SysClkModule_MEM;
if (module == HorizonOCModule_Display) {
display::SetRate(hz);
return;
}
if (usesGovenor) {
return;
}
if (HOSSVC_HAS_CLKRST) {
ClkrstSession session = {};
rc = clkrstOpenSession(&session, GetPcvModuleId(module), 3);
ASSERT_RESULT_OK(rc, "clkrstOpenSession");
ClkrstSetHz(session, hz);
/* Voltage bug workaround. */
if (module == SysClkModule_CPU) {
svcSleepThread(250'000);
ClkrstSetHz(session, hz);
}
clkrstCloseSession(&session);
} else {
PcvSetHz(GetPcvModule(module), hz);
if (module == SysClkModule_CPU) {
svcSleepThread(250'000);
PcvSetHz(GetPcvModule(module), hz);
}
}
}
u32 GetDisplayRate(u32 hz) {
display::GetRate(&hz, false);
return hz;
}
u32 GetHz(SysClkModule module) {
Result rc = 0;
u32 hz = 0;
if (module == HorizonOCModule_Display) {
return GetDisplayRate(hz);
}
if (HOSSVC_HAS_CLKRST) {
ClkrstSession session = {};
rc = clkrstOpenSession(&session, GetPcvModuleId(module), 3);
ASSERT_RESULT_OK(rc, "clkrstOpenSession");
rc = clkrstGetClockRate(&session, &hz);
ASSERT_RESULT_OK(rc, "clkrstGetClockRate");
clkrstCloseSession(&session);
} else {
rc = pcvGetClockRate(GetPcvModule(module), &hz);
ASSERT_RESULT_OK(rc, "pcvGetClockRate");
}
return hz;
}
u32 GetRealHz(SysClkModule module) {
u32 hz = 0;
switch (module) {
case SysClkModule_CPU:
return t210ClkCpuFreq();
case SysClkModule_GPU:
return t210ClkGpuFreq();
case SysClkModule_MEM:
return t210ClkMemFreq();
case HorizonOCModule_Display:
return GetDisplayRate(hz);
return hz;
default:
ASSERT_ENUM_VALID(SysClkModule, module);
}
return 0;
}
void GetFreqList(SysClkModule module, u32 *outList, u32 maxCount, u32 *outCount) {
Result rc = 0;
PcvClockRatesListType type;
s32 tmpInMaxCount = maxCount;
s32 tmpOutCount = 0;
if (HOSSVC_HAS_CLKRST) {
ClkrstSession session = {};
rc = clkrstOpenSession(&session, GetPcvModuleId(module), 3);
ASSERT_RESULT_OK(rc, "clkrstOpenSession");
rc = clkrstGetPossibleClockRates(&session, outList, tmpInMaxCount, &type, &tmpOutCount);
ASSERT_RESULT_OK(rc, "clkrstGetPossibleClockRates");
clkrstCloseSession(&session);
} else {
rc = pcvGetPossibleClockRates(GetPcvModule(module), outList, tmpInMaxCount, &type, &tmpOutCount);
ASSERT_RESULT_OK(rc, "pcvGetPossibleClockRates");
}
if (type != PcvClockRatesListType_Discrete) {
ERROR_THROW("Unexpected PcvClockRatesListType: %u (module = %s)", type, GetModuleName(module, false));
}
*outCount = tmpOutCount;
}
u32 GetHighestDockedDisplayRate() {
if (GetConsoleType() != HorizonOCConsoleType_Hoag) {
return display::GetDockedHighestAllowed();
}
return 60;
}
void ResetToStock() {
Result rc;
if (hosversionAtLeast(9,0,0)) {
std::uint32_t confId = 0;
rc = apmExtGetCurrentPerformanceConfiguration(&confId);
ASSERT_RESULT_OK(rc, "apmExtGetCurrentPerformanceConfiguration");
SysClkApmConfiguration* apmConfiguration = nullptr;
for (size_t i = 0; sysclk_g_apm_configurations[i].id; ++i) {
if(sysclk_g_apm_configurations[i].id == confId) {
apmConfiguration = &sysclk_g_apm_configurations[i];
break;
}
}
if(!apmConfiguration) {
ERROR_THROW("Unknown apm configuration: %x", confId);
}
SetHz(SysClkModule_CPU, apmConfiguration->cpu_hz);
SetHz(SysClkModule_GPU, apmConfiguration->gpu_hz);
SetHz(SysClkModule_MEM, apmConfiguration->mem_hz);
} else {
u32 mode = 0;
rc = apmExtGetPerformanceMode(&mode);
ASSERT_RESULT_OK(rc, "apmExtGetPerformanceMode");
rc = apmExtSysRequestPerformanceMode(mode);
ASSERT_RESULT_OK(rc, "apmExtSysRequestPerformanceMode");
}
}
void ResetToStockDisplay() {
if (GetConsoleType() != HorizonOCConsoleType_Hoag) {
display::SetRate(60);
}
}
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#include <switch.h>
#include <sysclk.h>
#include <nxExt.h>
#include "../errors.hpp"
namespace board {
void SetHz(SysClkModule module, u32 hz);
u32 GetHz(SysClkModule module);
u32 GetRealHz(SysClkModule module);
void GetFreqList(SysClkModule module, u32 *outList, u32 maxCount, u32 *outCount);
u32 GetHighestDockedDisplayRate();
void ResetToStock();
void ResetToStockDisplay();
template <typename Getter>
void ResetToStockModule(Getter getHzFunc, SysClkModule module) {
Result rc = 0;
if (hosversionAtLeast(9, 0, 0)) {
u32 confId = 0;
rc = apmExtGetCurrentPerformanceConfiguration(&confId);
ASSERT_RESULT_OK(rc, "apmExtGetCurrentPerformanceConfiguration");
SysClkApmConfiguration* apmConfiguration = nullptr;
for (size_t i = 0; sysclk_g_apm_configurations[i].id; ++i) {
if (sysclk_g_apm_configurations[i].id == confId) {
apmConfiguration = &sysclk_g_apm_configurations[i];
break;
}
}
if (!apmConfiguration) {
ERROR_THROW("Unknown apm configuration: %x", confId);
}
SetHz(module, getHzFunc(*apmConfiguration));
} else {
u32 mode = 0;
rc = apmExtGetPerformanceMode(&mode);
ASSERT_RESULT_OK(rc, "apmExtGetPerformanceMode");
rc = apmExtSysRequestPerformanceMode(mode);
ASSERT_RESULT_OK(rc, "apmExtSysRequestPerformanceMode");
}
}
inline void ResetToStockCpu() {
ResetToStockModule([](const SysClkApmConfiguration& cfg) {return cfg.cpu_hz; }, SysClkModule_CPU);
}
inline void ResetToStockGpu() {
ResetToStockModule([](const SysClkApmConfiguration& cfg){ return cfg.gpu_hz; }, SysClkModule_GPU);
}
inline void ResetToStockMem() {
ResetToStockModule([](const SysClkApmConfiguration& cfg){ return cfg.mem_hz; }, SysClkModule_MEM);
}
}

View File

@@ -0,0 +1,111 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#include <switch.h>
#include <fuse.h>
#include "board_fuse.hpp"
#include <cstring>
namespace board {
void SetGpuBracket(u8 speedo, u8 &gpuBracket) {
if (speedo <= 1624) {
gpuBracket = 0;
return;
}
if (speedo <= 1689) {
gpuBracket = 1;
return;
}
if (speedo <= 1753) {
gpuBracket = 2;
return;
}
/* >= 1754 */
gpuBracket = 3;
}
void ReadFuses(FuseData &speedo) {
u64 pid = 0;
constexpr u64 UsbID = 0x0100000000000006;
if (R_FAILED(pmdmntGetProcessId(&pid, UsbID))) {
return;
}
Handle debug;
if (R_FAILED(svcDebugActiveProcess(&debug, pid))) {
return;
}
MemoryInfo mem_info = {};
u32 pageinfo = 0;
u64 addr = 0;
u8 stack[0x10] = {};
const u8 compare[0x10] = {};
u8 dump[0x400] = {};
constexpr u64 PageSize = 0x1000;
while (true) {
if (R_FAILED(svcQueryDebugProcessMemory(&mem_info, &pageinfo, debug, addr)) || mem_info.addr < addr) {
break;
}
if (mem_info.type == MemType_Io && mem_info.size == PageSize) {
if (R_FAILED(svcReadDebugProcessMemory(stack, debug, mem_info.addr, sizeof(stack)))) {
break;
}
if (memcmp(stack, compare, sizeof(stack)) == 0) {
if (R_FAILED(svcReadDebugProcessMemory(dump, debug, mem_info.addr + 0x800, sizeof(dump)))) {
break;
}
speedo.cpuSpeedo = *reinterpret_cast<u16*>(dump + FUSE_CPU_SPEEDO_0_CALIB);
speedo.gpuSpeedo = *reinterpret_cast<u16*>(dump + FUSE_CPU_SPEEDO_2_CALIB);
speedo.socSpeedo = *reinterpret_cast<u16*>(dump + FUSE_SOC_SPEEDO_0_CALIB);
speedo.cpuIDDQ = *reinterpret_cast<u16*>(dump + FUSE_CPU_IDDQ_CALIB) * 4;
speedo.gpuIDDQ = *reinterpret_cast<u16*>(dump + FUSE_GPU_IDDQ_CALIB) * 5;
speedo.socIDDQ = *reinterpret_cast<u16*>(dump + FUSE_SOC_IDDQ_CALIB) * 4;
speedo.waferX = *reinterpret_cast<u16*>(dump + FUSE_OPT_X_COORDINATE);
speedo.waferY = *reinterpret_cast<u16*>(dump + FUSE_OPT_Y_COORDINATE);
svcCloseHandle(debug);
return;
}
}
addr = mem_info.addr + mem_info.size;
}
svcCloseHandle(debug);
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#include <switch.h>
namespace board {
struct FuseData {
u16 cpuSpeedo;
u16 gpuSpeedo;
u16 socSpeedo;
u16 cpuIDDQ;
u16 gpuIDDQ;
u16 socIDDQ;
u16 waferX;
u16 waferY;
};
void ReadFuses(FuseData &speedo);
void SetGpuBracket(u8 gpuSpeedo, u8 &gpuBracket);
}

View File

@@ -0,0 +1,188 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#include <switch.h>
#include <sysclk.h>
#include <nxExt.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <minIni.h>
#include <battery.h>
#include "board_misc.hpp"
#include "board.hpp"
namespace board {
Thread gpuThread;
LEvent threadexit;
Thread cpuCore0Thread;
Thread cpuCore1Thread;
Thread cpuCore2Thread;
u32 gpuLoad;
u32 _fd, _fd2;
Result nvCheckSched;
u64 idletick0 = 0;
u64 idletick1 = 0;
u64 idletick2 = 0;
constexpr u64 CpuTimeOutNs = 500'000'000;
constexpr double Systemtickfrequency = 19200000.0 * (static_cast<double>(CpuTimeOutNs) / 1'000'000'000.0);
void GpuLoadThread(void *nvCheckPtr) {
Result nvCheck = *static_cast<Result *>(nvCheckPtr);
constexpr u32 GpuSamples = 8;
u32 gpu_load_array[GpuSamples] = {};
size_t i = 0;
constexpr u32 NVGPU_GPU_IOCTL_PMU_GET_GPU_LOAD = 0x80044715;
if (R_SUCCEEDED(nvCheck)) do {
u32 temp;
if (R_SUCCEEDED(nvIoctl(_fd, NVGPU_GPU_IOCTL_PMU_GET_GPU_LOAD, &temp))) {
gpu_load_array[i++ % GpuSamples] = temp;
gpuLoad = std::accumulate(&gpu_load_array[0], &gpu_load_array[GpuSamples], 0) / GpuSamples;
}
svcSleepThread(16'666'000); // wait a bit (this is the perfect amount of time to keep the reading accurate)
} while(true);
}
void CheckCore(void *idletickPtr) {
u64* idletick = static_cast<u64 *>(idletickPtr);
while(true) {
u64 idletickA;
u64 idletickB;
svcGetInfo(&idletickB, InfoType_IdleTickCount, INVALID_HANDLE, -1);
svcWaitForAddress(&threadexit, ArbitrationType_WaitIfEqual, 0, CpuTimeOutNs);
svcGetInfo(&idletickA, InfoType_IdleTickCount, INVALID_HANDLE, -1);
*idletick = idletickA - idletickB;
}
}
void StartLoad(Result nvCheck, u32 fd) {
_fd = fd;
threadCreate(&gpuThread, GpuLoadThread, &nvCheck, NULL, 0x1000, 0x3F, -2);
threadStart(&gpuThread);
leventClear(&threadexit);
threadCreate(&cpuCore0Thread, CheckCore, &idletick0, NULL, 0x1000, 0x10, 0);
threadCreate(&cpuCore1Thread, CheckCore, &idletick1, NULL, 0x1000, 0x10, 1);
threadCreate(&cpuCore2Thread, CheckCore, &idletick2, NULL, 0x1000, 0x10, 2);
threadStart(&cpuCore0Thread);
threadStart(&cpuCore1Thread);
threadStart(&cpuCore2Thread);
}
u32 GetMaxCpuLoad() {
float cpuUsage0 = std::clamp(((Systemtickfrequency - idletick0) / static_cast<double>(Systemtickfrequency)) * 1000.0, 0.0, 1000.0);
float cpuUsage1 = std::clamp(((Systemtickfrequency - idletick1) / static_cast<double>(Systemtickfrequency)) * 1000.0, 0.0, 1000.0);
float cpuUsage2 = std::clamp(((Systemtickfrequency - idletick2) / static_cast<double>(Systemtickfrequency)) * 1000.0, 0.0, 1000.0);
return std::round(std::max({cpuUsage0, cpuUsage1, cpuUsage2}));
}
u32 GetPartLoad(SysClkPartLoad loadSource) {
switch(loadSource) {
case SysClkPartLoad_EMC:
return t210EmcLoadAll();
case SysClkPartLoad_EMCCpu:
return t210EmcLoadCpu();
case HocClkPartLoad_GPU:
return gpuLoad;
case HocClkPartLoad_CPUMax:
return GetMaxCpuLoad();
case HocClkPartLoad_BAT:
BatteryChargeInfo info;
batteryInfoGetChargeInfo(&info);
return info.RawBatteryCharge;
case HocClkPartLoad_FAN:
return GetFanLevel();
default:
ASSERT_ENUM_VALID(SysClkPartLoad, loadSource);
}
return 0;
}
void ExitLoad() {
threadClose(&gpuThread);
threadClose(&cpuCore0Thread);
threadClose(&cpuCore1Thread);
threadClose(&cpuCore2Thread);
}
namespace {
constexpr u32 NVschedCtrlEnable = 0x00000601;
constexpr u32 NVschedCtrlDisable = 0x00000602;
}
void SetGpuSchedulingMode(GpuSchedulingMode mode, GpuSchedulingOverrideMethod method) {
if (R_FAILED(nvCheckSched) && method == GpuSchedulingOverrideMethod_NvService) {
return;
}
u32 temp;
bool enabled = false;
switch (mode) {
case GpuSchedulingMode_DoNotOverride: break;
case GpuSchedulingMode_Disabled:
if (method == GpuSchedulingOverrideMethod_NvService) {
nvIoctl(_fd2, NVschedCtrlDisable, &temp);
} else {
enabled = false;
}
break;
case GpuSchedulingMode_Enabled:
if (method == GpuSchedulingOverrideMethod_NvService) {
nvIoctl(_fd2, NVschedCtrlEnable, &temp);
} else {
enabled = true;
}
break;
default:
ASSERT_ENUM_VALID(GpuSchedulingMode, mode);
}
if (method == GpuSchedulingOverrideMethod_Ini) {
constexpr const char *IniPath = "sdmc:/atmosphere/config/system_settings.ini";
constexpr const char *Section = "am.gpu";
constexpr const char *Key = "gpu_scheduling_enabled";
const char *value = enabled ? "u8!0x1" : "u8!0x0";
ini_puts(Section, Key, value, IniPath);
}
}
void SchedSetFD2(u32 fd2) {
_fd2 = fd2;
}
void NvSchedSucceed(Result nvSched) {
nvCheckSched = nvSched;
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#include <switch.h>
#include <sysclk.h>
namespace board {
void StartLoad(Result nvCheck, u32 fd);
void ExitLoad();
u32 GetPartLoad(SysClkPartLoad loadSource);
void SetGpuSchedulingMode(GpuSchedulingMode mode, GpuSchedulingOverrideMethod method);
void SchedSetFD2(u32 fd2);
void NvSchedSucceed(Result nvSched);
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#include <switch.h>
#include <pwm.h>
#include <cmath>
#include <rgltr.h>
namespace board {
Thread miscThread;
u8 fanLevel = 0;
PwmChannelSession *_iCon;
void MiscThreadFunc(void *pwmCheckPtr) {
Result pwmCheck = *static_cast<Result *>(pwmCheckPtr);
double temp = 0;
double rotationDuty = 0;
while (true) {
if (R_SUCCEEDED(pwmCheck)) {
if (R_SUCCEEDED(pwmChannelSessionGetDutyCycle(_iCon, &temp))) {
temp *= 10;
temp = trunc(temp);
temp /= 10;
rotationDuty = 100.0 - temp;
}
}
fanLevel = static_cast<u8>(rotationDuty);
svcSleepThread(300'000'000);
}
}
u8 GetFanLevel() {
return fanLevel;
}
void StartMiscThread(Result pwmCheck, PwmChannelSession *iCon) {
_iCon = iCon;
threadCreate(&miscThread, MiscThreadFunc, &pwmCheck, NULL, 0x1000, 0x10, 3);
threadStart(&miscThread);
}
void ExitMiscThread() {
threadClose(&miscThread);
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#include <switch.h>
#include <sysclk.h>
#include <pwm.h>
namespace board {
void StartMiscThread(Result pwmCheck, PwmChannelSession *iCon);
void ExitMiscThread();
u8 GetFanLevel();
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#include <switch.h>
#include <sysclk.h>
#include "board.hpp"
namespace board {
const char *GetModuleName(SysClkModule module, bool pretty) {
ASSERT_ENUM_VALID(SysClkModule, module);
return sysclkFormatModule(module, pretty);
}
const char *GetProfileName(SysClkProfile profile, bool pretty) {
ASSERT_ENUM_VALID(SysClkProfile, profile);
return sysclkFormatProfile(profile, pretty);
}
const char *GetThermalSensorName(SysClkThermalSensor sensor, bool pretty) {
ASSERT_ENUM_VALID(SysClkThermalSensor, sensor);
return sysclkFormatThermalSensor(sensor, pretty);
}
const char *GetPowerSensorName(SysClkPowerSensor sensor, bool pretty) {
ASSERT_ENUM_VALID(SysClkPowerSensor, sensor);
return sysclkFormatPowerSensor(sensor, pretty);
}
}

View File

@@ -0,0 +1,38 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#include <switch.h>
#include <sysclk.h>
namespace board {
const char *GetModuleName(SysClkModule module, bool pretty);
const char *GetProfileName(SysClkProfile profile, bool pretty);
const char *GetThermalSensorName(SysClkThermalSensor sensor, bool pretty);
const char *GetPowerSensorName(SysClkPowerSensor sensor, bool pretty);
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#include <switch.h>
#include <sysclk.h>
#include <nxExt.h>
#include "board.hpp"
namespace board {
SysClkProfile GetProfile() {
u32 mode = 0;
Result rc = apmExtGetPerformanceMode(&mode);
ASSERT_RESULT_OK(rc, "apmExtGetPerformanceMode");
if (mode) {
return SysClkProfile_Docked;
}
PsmChargerType chargerType;
rc = psmGetChargerType(&chargerType);
ASSERT_RESULT_OK(rc, "psmGetChargerType");
if (chargerType == PsmChargerType_EnoughPower) {
return SysClkProfile_HandheldChargingOfficial;
} else if (chargerType == PsmChargerType_LowPower) {
return SysClkProfile_HandheldChargingUSB;
}
return SysClkProfile_Handheld;
}
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#include <sysclk.h>
namespace board {
SysClkProfile GetProfile();
}

View File

@@ -0,0 +1,107 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#include <sysclk.h>
#include <switch.h>
#include <nxExt.h>
#include <cmath>
#include <battery.h>
#include <pwm.h>
#include "board.hpp"
#include "../soctherm.hpp"
namespace board {
s32 GetTemperatureMilli(SysClkThermalSensor sensor) {
s32 millis = 0;
BatteryChargeInfo info;
soctherm::TSensorTemps temps = {};
soctherm::ReadSensors(temps);
switch(sensor) {
case SysClkThermalSensor_SOC: {
millis = tmp451TempSoc();
break;
}
case SysClkThermalSensor_PCB: {
millis = tmp451TempPcb();
break;
}
case SysClkThermalSensor_Skin: {
if (HOSSVC_HAS_TC) {
Result rc;
rc = tcGetSkinTemperatureMilliC(&millis);
ASSERT_RESULT_OK(rc, "tcGetSkinTemperatureMilliC");
}
break;
}
case HorizonOCThermalSensor_Battery: {
batteryInfoGetChargeInfo(&info);
millis = batteryInfoGetTemperatureMiliCelsius(&info);
break;
}
case HorizonOCThermalSensor_PMIC: {
millis = 50000;
break;
}
case HorizonOCThermalSensor_CPU: {
millis = temps.cpu;
break;
}
case HorizonOCThermalSensor_GPU: {
millis = temps.gpu;
break;
}
case HorizonOCThermalSensor_MEM: {
millis = temps.mem;
break;
}
case HorizonOCThermalSensor_PLLX: {
millis = temps.pllx;
}
default: {
ASSERT_ENUM_VALID(SysClkThermalSensor, sensor);
}
}
return std::max(0, millis);
}
s32 GetPowerMw(SysClkPowerSensor sensor) {
switch (sensor) {
case SysClkPowerSensor_Now:
return max17050PowerNow();
case SysClkPowerSensor_Avg:
return max17050PowerAvg();
default:
ASSERT_ENUM_VALID(SysClkPowerSensor, sensor);
}
return 0;
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#include <sysclk.h>
#include <switch.h>
namespace board {
s32 GetTemperatureMilli(SysClkThermalSensor sensor);
s32 GetPowerMw(SysClkPowerSensor sensor);
}

View File

@@ -0,0 +1,455 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#include <switch.h>
#include <sysclk.h>
#include <memmem.h>
#include <registers.h>
#include <cstring>
#include <rgltr.h>
#include <battery.h>
#include "board.hpp"
#include "board_freq.hpp"
#include "board_volt.hpp"
#include "../file_utils.hpp"
namespace board {
GpuVoltData voltData = {};
u64 cldvfs;
CpuDfllData cachedTune;
/* ... This really needs some cleanup... */
namespace {
struct EristaCpuUvEntry {
u32 tune0;
u32 tune1;
};
struct MarikoCpuUvEntry {
u32 tune0_low;
u32 tune0_high;
u32 tune1_low;
u32 tune1_high;
};
EristaCpuUvEntry eristaCpuUvTable[5] = {
{0xffff, 0x27007ff},
{0xefff, 0x27407ff},
{0xdfff, 0x27807ff},
{0xdfdf, 0x27a07ff},
{0xcfdf, 0x37007ff},
};
MarikoCpuUvEntry marikoCpuUvLow[12] = {
{0xffa0, 0xffff, 0x21107ff, 0},
{0x0, 0xffdf, 0x21107ff, 0x27207ff},
{0xffdf, 0xffdf, 0x21107ff, 0x27307ff},
{0xffff, 0xffdf, 0x21107ff, 0x27407ff},
{0x0, 0xffdf, 0x21607ff, 0x27707ff},
{0x0, 0xffdf, 0x21607ff, 0x27807ff},
{0x0, 0xdfff, 0x21607ff, 0x27b07ff},
{0xdfff, 0xdfff, 0x21707ff, 0x27b07ff},
{0xdfff, 0xdfff, 0x21707ff, 0x27c07ff},
{0xdfff, 0xdfff, 0x21707ff, 0x27d07ff},
{0xdfff, 0xdfff, 0x21707ff, 0x27e07ff},
{0xdfff, 0xdfff, 0x21707ff, 0x27f07ff},
};
MarikoCpuUvEntry marikoCpuUvHigh[12] = {
{0x0, 0xffff, 0, 0},
{0x0, 0xffdf, 0, 0x27207ff},
{0x0, 0xffdf, 0, 0x27307ff},
{0x0, 0xffdf, 0, 0x27407ff},
{0x0, 0xffdf, 0, 0x27707ff},
{0x0, 0xffdf, 0, 0x27807ff},
{0x0, 0xdfff, 0, 0x27b07ff},
{0x0, 0xdfff, 0, 0x27c07ff},
{0x0, 0xdfff, 0, 0x27d07ff},
{0x0, 0xdfff, 0, 0x27e07ff},
{0x0, 0xdfff, 0, 0x27f07ff},
{0x0, 0xdfff, 0, 0x27f07ff},
};
}
void CacheDfllData() {
u64 temp;
Result rc = svcQueryMemoryMapping(&cldvfs, &temp, CLDVFS_REGION_BASE, CLDVFS_REGION_SIZE);
ASSERT_RESULT_OK(rc, "svcQueryMemoryMapping (cldvfs)");
if (GetSocType() == SysClkSocType_Erista) {
cachedTune.tune0Low = *reinterpret_cast<u32 *>(cldvfs + CL_DVFS_TUNE0_0);
cachedTune.tune1Low = *reinterpret_cast<u32 *>(cldvfs + CL_DVFS_TUNE1_0);
} else {
SetHz(SysClkModule_CPU, 1785000000);
cachedTune.tune0High = *reinterpret_cast<u32 *>(cldvfs + CL_DVFS_TUNE0_0);
ResetToStockCpu();
}
}
/* TODO: clean up this code. */
void SetDfllTunings(u32 levelLow, u32 levelHigh, u32 tbreakPoint) {
u32* tune0_ptr = reinterpret_cast<u32 *>(cldvfs + CL_DVFS_TUNE0_0);
u32* tune1_ptr = reinterpret_cast<u32 *>(cldvfs + CL_DVFS_TUNE1_0);
if (GetSocType() == SysClkSocType_Mariko) {
if (GetHz(SysClkModule_CPU) < tbreakPoint && (levelLow || levelHigh)) {
if (levelLow) {
*tune0_ptr = marikoCpuUvLow[levelLow-1].tune0_low;
*tune1_ptr = marikoCpuUvLow[levelLow-1].tune1_low;
}
return;
} else {
if (levelLow) {
*tune0_ptr = marikoCpuUvLow[levelLow-1].tune0_low;
*tune1_ptr = marikoCpuUvLow[levelLow-1].tune1_low;
}
if (levelHigh) {
*tune0_ptr = marikoCpuUvHigh[levelHigh-1].tune0_high;
*tune1_ptr = marikoCpuUvHigh[levelHigh-1].tune1_high;
}
return;
}
if (GetHz(SysClkModule_CPU) < tbreakPoint || (!levelLow)) { // account for tbreak
*tune0_ptr = 0xCFFF;
*tune1_ptr = 0xFF072201;
return;
} else if (GetHz(SysClkModule_CPU) >= tbreakPoint || (!levelHigh)) {
*tune0_ptr = cachedTune.tune0High; // per console?
*tune1_ptr = 0xFFF7FF3F;
return;
}
} else {
if (GetHz(SysClkModule_CPU) < tbreakPoint || (!levelLow)) { // account for tbreak
*tune0_ptr = cachedTune.tune0Low; // I think each erista has a different tune0/tune1?
*tune1_ptr = cachedTune.tune1Low;
return;
} else {
if (levelLow) {
*tune0_ptr = eristaCpuUvTable[levelLow-1].tune0;
*tune1_ptr = eristaCpuUvTable[levelLow-1].tune1;
} else {
*tune0_ptr = 0x0;
*tune1_ptr = 0x0;
}
}
}
}
/*
enum TableConfig: u32 {
DEFAULT_TABLE = 1,
TBREAK_1581 = 2,
TBREAK_1683 = 3,
EXTREME_TABLE = 4,
};
*/
u32 CalculateTbreak(u32 table) {
if (GetSocType() == SysClkSocType_Erista) {
return 1581000000;
} else {
switch (table) {
case 1 ... 2:
case 4:
return 1581000000;
case 3:
return 1683000000;
default:
return 1581000000;
}
}
}
/*
* Switch Power domains (max77620):
* Name | Usage | uV step | uV min | uV default | uV max | Init
*-------+---------------+---------+--------+------------+---------+------------------
* sd0 | SoC | 12500 | 600000 | 625000 | 1400000 | 1.125V (pkg1.1)
* sd1 | SDRAM | 12500 | 600000 | 1125000 | 1125000 | 1.1V (pkg1.1)
* sd2 | ldo{0-1, 7-8} | 12500 | 600000 | 1325000 | 1350000 | 1.325V (pcv)
* sd3 | 1.8V general | 12500 | 600000 | 1800000 | 1800000 |
* ldo0 | Display Panel | 25000 | 800000 | 1200000 | 1200000 | 1.2V (pkg1.1)
* ldo1 | XUSB, PCIE | 25000 | 800000 | 1050000 | 1050000 | 1.05V (pcv)
* ldo2 | SDMMC1 | 50000 | 800000 | 1800000 | 3300000 |
* ldo3 | GC ASIC | 50000 | 800000 | 3100000 | 3100000 | 3.1V (pcv)
* ldo4 | RTC | 12500 | 800000 | 850000 | 850000 | 0.85V (AO, pcv)
* ldo5 | GC Card | 50000 | 800000 | 1800000 | 1800000 | 1.8V (pcv)
* ldo6 | Touch, ALS | 50000 | 800000 | 2900000 | 2900000 | 2.9V (pcv)
* ldo7 | XUSB | 50000 | 800000 | 1050000 | 1050000 | 1.05V (pcv)
* ldo8 | XUSB, DP, MCU | 50000 | 800000 | 1050000 | 2800000 | 1.05V/2.8V (pcv)
typedef enum {
PcvPowerDomainId_Max77620_Sd0 = 0x3A000080,
PcvPowerDomainId_Max77620_Sd1 = 0x3A000081, // vdd2
PcvPowerDomainId_Max77620_Sd2 = 0x3A000082,
PcvPowerDomainId_Max77620_Sd3 = 0x3A000083,
PcvPowerDomainId_Max77620_Ldo0 = 0x3A0000A0,
PcvPowerDomainId_Max77620_Ldo1 = 0x3A0000A1,
PcvPowerDomainId_Max77620_Ldo2 = 0x3A0000A2,
PcvPowerDomainId_Max77620_Ldo3 = 0x3A0000A3,
PcvPowerDomainId_Max77620_Ldo4 = 0x3A0000A4,
PcvPowerDomainId_Max77620_Ldo5 = 0x3A0000A5,
PcvPowerDomainId_Max77620_Ldo6 = 0x3A0000A6,
PcvPowerDomainId_Max77620_Ldo7 = 0x3A0000A7,
PcvPowerDomainId_Max77620_Ldo8 = 0x3A0000A8,
PcvPowerDomainId_Max77621_Cpu = 0x3A000003,
PcvPowerDomainId_Max77621_Gpu = 0x3A000004,
PcvPowerDomainId_Max77812_Cpu = 0x3A000003,
PcvPowerDomainId_Max77812_Gpu = 0x3A000004,
PcvPowerDomainId_Max77812_Dram = 0x3A000005, // vddq
} PowerDomainId;
*/
u32 GetVoltage(HocClkVoltage voltage) {
RgltrSession session;
Result rc = 0;
u32 out = 0;
BatteryChargeInfo info;
switch (voltage) {
case HocClkVoltage_SOC:
rc = rgltrOpenSession(&session, PcvPowerDomainId_Max77620_Sd0);
ASSERT_RESULT_OK(rc, "rgltrOpenSession")
rgltrGetVoltage(&session, &out);
rgltrCloseSession(&session);
break;
case HocClkVoltage_EMCVDD2:
rc = rgltrOpenSession(&session, PcvPowerDomainId_Max77620_Sd1);
ASSERT_RESULT_OK(rc, "rgltrOpenSession")
rgltrGetVoltage(&session, &out);
rgltrCloseSession(&session);
break;
case HocClkVoltage_CPU:
if (GetSocType() == SysClkSocType_Mariko) {
rc = rgltrOpenSession(&session, PcvPowerDomainId_Max77621_Cpu);
} else {
rc = rgltrOpenSession(&session, PcvPowerDomainId_Max77812_Cpu);
}
ASSERT_RESULT_OK(rc, "rgltrOpenSession")
rgltrGetVoltage(&session, &out);
rgltrCloseSession(&session);
break;
case HocClkVoltage_GPU:
if (GetSocType() == SysClkSocType_Mariko) {
rc = rgltrOpenSession(&session, PcvPowerDomainId_Max77621_Gpu);
} else {
rc = rgltrOpenSession(&session, PcvPowerDomainId_Max77812_Gpu);
}
ASSERT_RESULT_OK(rc, "rgltrOpenSession")
rgltrGetVoltage(&session, &out);
rgltrCloseSession(&session);
break;
case HocClkVoltage_EMCVDDQ_MarikoOnly:
if (GetSocType() == SysClkSocType_Mariko) {
rc = rgltrOpenSession(&session, PcvPowerDomainId_Max77812_Dram);
ASSERT_RESULT_OK(rc, "rgltrOpenSession")
rgltrGetVoltage(&session, &out);
rgltrCloseSession(&session);
} else {
out = GetVoltage(HocClkVoltage_EMCVDD2);
}
break;
case HocClkVoltage_Display:
rc = rgltrOpenSession(&session, PcvPowerDomainId_Max77620_Ldo0);
ASSERT_RESULT_OK(rc, "rgltrOpenSession")
rgltrGetVoltage(&session, &out);
rgltrCloseSession(&session);
break;
case HocClkVoltage_Battery:
batteryInfoGetChargeInfo(&info);
out = info.VoltageAvg;
break;
default:
ASSERT_ENUM_VALID(HocClkVoltage, voltage);
}
return out > 0 ? out : 0;
}
Handle GetPcvHandle() {
constexpr u64 PcvID = 0x10000000000001a;
u64 processIDList[80]{};
s32 processCount = 0;
Handle handle = INVALID_HANDLE;
DebugEventInfo debugEvent{};
/* Get all running processes. */
Result resultGetProcessList = svcGetProcessList(&processCount, processIDList, std::size(processIDList));
if (R_FAILED(resultGetProcessList)) {
return INVALID_HANDLE;
}
/* Try to find pcv. */
for (int i = 0; i < processCount; ++i) {
if (handle != INVALID_HANDLE) {
svcCloseHandle(handle);
handle = INVALID_HANDLE;
}
/* Try to debug process, if it fails, try next process. */
Result resultSvcDebugProcess = svcDebugActiveProcess(&handle, processIDList[i]);
if (R_FAILED(resultSvcDebugProcess)) {
continue;
}
/* Try to get a debug event. */
Result resultDebugEvent = svcGetDebugEvent(&debugEvent, handle);
if (R_SUCCEEDED(resultDebugEvent)) {
if (debugEvent.info.create_process.program_id == PcvID) {
return handle;
}
}
}
/* Failed to get handle. */
return INVALID_HANDLE;
}
void CacheGpuVoltTable() {
UnkRegulator reg = {
.voltageMin = 600000,
.voltageStep = 12500,
.voltageMax = 1400000,
};
Handle handle = GetPcvHandle();
if (handle == INVALID_HANDLE) {
fileUtils::LogLine("[dvfs] Invalid handle!");
return;
}
MemoryInfo memoryInfo = {};
u64 address = 0;
u32 pageInfo = 0;
constexpr u32 PageSize = 0x1000;
u8 buffer[PageSize];
/* Loop until failure. */
while (true) {
/* Find pcv heap. */
while (true) {
Result resultProcessMemory = svcQueryDebugProcessMemory(&memoryInfo, &pageInfo, handle, address);
address = memoryInfo.addr + memoryInfo.size;
if (R_FAILED(resultProcessMemory) || !address) {
svcCloseHandle(handle);
fileUtils::LogLine("[dvfs] Failed to get process data. %u", R_DESCRIPTION(resultProcessMemory));
handle = INVALID_HANDLE;
return;
}
if (memoryInfo.size && (memoryInfo.perm & 3) == 3 && static_cast<char>(memoryInfo.type) == 0x4) {
/* Found valid memory. */
break;
}
}
for (u64 base = 0; base < memoryInfo.size; base += PageSize) {
u32 memorySize = std::min(memoryInfo.size, static_cast<u64>(PageSize));
if (R_FAILED(svcReadDebugProcessMemory(buffer, handle, base + memoryInfo.addr, memorySize))) {
break;
}
u8 *resultFindReg = static_cast<u8 *>(memmem_impl(buffer, sizeof(buffer), &reg, sizeof(reg)));
u32 index = resultFindReg - buffer;
if (!resultFindReg) {
continue;
}
/* Assuming mariko. */
const u32 vmax = 800;
constexpr u32 VoltageTableOffset = 312;
if (!std::memcmp(&buffer[index + VoltageTableOffset], &vmax, sizeof(vmax))) {
std::memcpy(voltData.voltTable, &buffer[index + VoltageTableOffset], sizeof(voltData.voltTable));
voltData.voltTableAddress = base + memoryInfo.addr + VoltageTableOffset + index;
}
svcCloseHandle(handle);
handle = INVALID_HANDLE;
return;
}
}
svcCloseHandle(handle);
handle = INVALID_HANDLE;
return;
}
void PcvHijackGpuVolts(u32 vmin) {
u32 table[192];
static_assert(sizeof(table) == sizeof(voltData.voltTable), "Invalid gpu voltage table size!");
std::memcpy(table, voltData.voltTable, sizeof(voltData.voltTable));
if (voltData.ramVmin == vmin) {
return;
}
for (u32 i = 0; i < std::size(table); ++i) {
if (table[i] && table[i] <= vmin) {
table[i] = vmin;
}
}
Handle handle = GetPcvHandle();
if (handle == INVALID_HANDLE) {
fileUtils::LogLine("Invalid handle!");
return;
}
Result rc = svcWriteDebugProcessMemory(handle, table, voltData.voltTableAddress, sizeof(table));
if (R_SUCCEEDED(rc)) {
voltData.ramVmin = vmin;
}
svcCloseHandle(handle);
fileUtils::LogLine("[dvfs] voltage set to %u mV", vmin);
}
u32 GetMinimumGpuVmin(u32 freqMhz, u32 bracket) {
static const u32 ramTable[][22] = {
{ 2133, 2200, 2266, 2300, 2366, 2400, 2433, 2466, 2533, 2566, 2600, 2633, 2700, 2733, 2766, 2833, 2866, 2900, 2933, 3033, 3066, 3100, },
{ 2300, 2366, 2433, 2466, 2533, 2566, 2633, 2700, 2733, 2800, 2833, 2900, 2933, 2966, 3033, 3066, 3100, 3133, 3166, 3200, 3233, 3266, },
{ 2433, 2466, 2533, 2600, 2666, 2733, 2766, 2800, 2833, 2866, 2933, 2966, 3033, 3066, 3100, 3133, 3166, 3200, 3233, 3300, 3333, 3366, },
{ 2500, 2533, 2600, 2633, 2666, 2733, 2800, 2866, 2900, 2966, 3033, 3100, 3166, 3200, 3233, 3266, 3300, 3333, 3366, 3400, 3400, 3400, },
};
static const u32 gpuVoltArray[] = { 590, 600, 610, 620, 630, 640, 650, 660, 670, 680, 690, 700, 710, 720, 730, 740, 750, 760, 770, 780, 790, 800, };
if (freqMhz <= 1600) {
return 0;
}
for (u32 i = 0; i < std::size(gpuVoltArray); ++i) {
if (freqMhz <= ramTable[bracket][i]) {
return gpuVoltArray[i];
}
}
return gpuVoltArray[std::size(gpuVoltArray) - 1];
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (c) Souldbminer, Lightos_ and Horizon OC Contributors
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* --------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <p-sam@d3vs.net>, <natinusala@gmail.com>, <m4x@m4xw.net>
* wrote this file. As long as you retain this notice you can do whatever you
* want with this stuff. If you meet any of us some day, and you think this
* stuff is worth it, you can buy us a beer in return. - The sys-clk authors
* --------------------------------------------------------------------------
*/
#pragma once
#include <switch.h>
#include <sysclk.h>
namespace board {
struct GpuVoltData {
u32 voltTable[6][32] = {};
u64 voltTableAddress;
u32 ramVmin;
};
/* TODO: Find out what component this actually targets. */
struct UnkRegulator {
u32 voltageMin;
u32 voltageStep;
u32 voltageMax;
};
struct CpuDfllData {
u32 tune0Low;
u32 tune0High;
u32 tune1Low;
u32 tune1High;
// u32 tune_high_min_millivolts;
// u32 tune_high_margin_millivolts;
// u64 dvco_calibration_max;
};
void SetDfllTunings(u32 levelLow, u32 levelHigh, u32 tbreakPoint);
void CacheDfllData();
u32 CalculateTbreak(u32 table);
u32 GetVoltage(HocClkVoltage voltage);
void CacheGpuVoltTable();
void PcvHijackGpuVolts(u32 vmin);
u32 GetMinimumGpuVmin(u32 freqMhz, u32 bracket);
}

View File

@@ -0,0 +1,736 @@
/*
* Copyright (c) Souldbminer, based on reasearch by MasaGratoR and Cooler3D
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "display_refresh_rate.hpp"
#include <string.h>
#include <math.h>
#include <stdarg.h>
#include <switch.h>
namespace display {
#define DSI_CLOCK_HZ 234000000llu
#define NVDISP_GET_MODE2 0x803C021B
#define NVDISP_SET_MODE2 0x403C021C
#define NVDISP_VALIDATE_MODE2 0xC03C021D
#define NVDISP_GET_MODE_DB2 0xEF20021E
#define NVDISP_GET_PANEL_DATA 0xC01C0226
#define MAX_REFRESH_RATE 72
static DisplayRefreshConfig g_config = {0};
static bool g_initialized = false;
static uint8_t g_dockedHighestRefreshRate = 60;
static uint8_t g_dockedLinkRate = 10;
static bool g_wasRetroSuperTurnedOff = false;
static uint32_t g_lastVActive = 1080;
static bool g_canChangeRefreshRateDocked = false;
static uint8_t g_lastVActiveSet = 0;
static const uint8_t g_dockedRefreshRates[] = {40, 45, 50, 55, 60, 70, 72, 75, 80, 90, 95, 100, 110, 120, 130, 140, 144, 150, 160, 165, 170, 180, 190, 200, 210, 220, 230, 240};
// Calculate with this tool:
// https://tomverbeure.github.io/video_timings_calculator?horiz_pixels=1920&vert_pixels=1080&refresh_rate=240&margins=false&interlaced=false&bpc=8&color_fmt=rgb444&video_opt=false&custom_hblank=80&custom_vblank=6
/*
typedef struct {
uint16_t hFrontPorch;
uint8_t hSyncWidth;
uint8_t hBackPorch;
uint8_t vFrontPorch;
uint8_t vSyncWidth;
uint8_t vBackPorch;
uint8_t VIC;
uint32_t pixelClock_kHz;
} DockedTimings;
*/
static const DockedTimings g_dockedTimings1080p[] = {
{8, 32, 40, 7, 8, 6, 0, 88080}, // 40Hz
{8, 32, 40, 9, 8, 6, 0, 99270}, // 45Hz
{528, 44, 148, 4, 5, 36, 31, 148500}, // 50Hz
{8, 32, 40, 15, 8, 6, 0, 121990}, // 55Hz
{88, 44, 148, 4, 5, 36, 16, 148500}, // 60Hz
{8, 32, 40, 22, 8, 6, 0, 156240}, // 70Hz
{8, 32, 40, 23, 8, 6, 0, 160848}, // 72Hz
{8, 32, 40, 25, 8, 6, 0, 167850}, // 75Hz
{8, 32, 40, 28, 8, 6, 0, 179520}, // 80Hz
{8, 32, 40, 33, 8, 6, 0, 202860}, // 90Hz
{8, 32, 40, 36, 8, 6, 0, 214700}, // 95Hz
{528, 44, 148, 4, 5, 36, 64, 297000}, // 100Hz
{8, 32, 40, 44, 8, 6, 0, 250360}, // 110Hz
{88, 44, 148, 4, 5, 36, 63, 297000}, // 120Hz
{8, 32, 40, 55, 8, 6, 0, 298750}, //130Hz CVT-RBv2
{8, 32, 40, 61, 8, 6, 0, 323400}, //140Hz CVT-RBv2
{8, 32, 40, 63, 8, 6, 0, 333216}, //144Hz CVT-RBv2
{8, 32, 40, 67, 8, 6, 0, 348300}, //150Hz CVT-RBv2
{8, 32, 40, 72, 8, 6, 0, 373120}, //160Hz CVT-RBv2
{8, 32, 40, 75, 8, 6, 0, 385770}, //165Hz CVT-RBv2
{8, 32, 40, 78, 8, 6, 0, 398480}, //170Hz CVT-RBv2
{8, 32, 40, 84, 8, 6, 0, 424080}, //180Hz CVT-RBv2
{8, 32, 40, 90, 8, 6, 0, 449920}, //190Hz CVT-RBv2
{8, 32, 40, 96, 8, 6, 0, 476000}, //200Hz CVT-RBv2
{8, 32, 40, 102, 8, 6, 0, 502320}, //210Hz CVT-RBv2
{8, 32, 40, 108, 8, 6, 0, 528880}, //220Hz CVT-RBv2
{8, 32, 40, 114, 8, 6, 0, 555680}, //230Hz CVT-RBv2
{8, 32, 40, 121, 8, 6, 0, 583200}, //240Hz CVT-RBv2
// technically you can go to 476hz, but in practice, why would you?
};
// These timings *should* work but are untested
static const HandheldTimings g_handheldTimingsRETRO[] = {
{72, 136, 72, 1, 660, 9, 78000}, // 40Hz
{72, 136, 72, 1, 612, 9, 77982}, // 41Hz
{72, 136, 72, 1, 567, 9, 77994}, // 42Hz
{72, 136, 72, 1, 524, 9, 78002}, // 43Hz
{72, 136, 72, 1, 483, 9, 78012}, // 44Hz
{72, 136, 72, 1, 443, 9, 77985}, // 45Hz
{72, 136, 72, 1, 406, 9, 78016}, // 46Hz
{72, 136, 72, 1, 370, 9, 78020}, // 47Hz
{72, 136, 72, 1, 335, 9, 78000}, // 48Hz
{72, 136, 72, 1, 302, 9, 78008}, // 49Hz
{72, 136, 72, 1, 270, 9, 78000}, // 50Hz
{72, 136, 72, 1, 239, 9, 77979}, // 51Hz
{72, 136, 72, 1, 210, 9, 78000}, // 52Hz
{72, 136, 72, 1, 182, 9, 78016}, // 53Hz
{72, 136, 72, 1, 154, 9, 77976}, // 54Hz
{72, 136, 72, 1, 128, 9, 77990}, // 55Hz
{72, 136, 72, 1, 103, 9, 78008}, // 56Hz
{72, 136, 72, 1, 78, 9, 77976}, // 57Hz
{72, 136, 72, 1, 55, 9, 78010}, // 58Hz
{72, 136, 72, 1, 32, 9, 77998}, // 59Hz
{72, 136, 72, 1, 10, 9, 78000}, // 60Hz
};
static const MinMaxRefreshRate g_handheldModeRefreshRate = {40, 80};
static uint8_t _getDockedRefreshRateIterator(uint32_t refreshRate) {
for (size_t i = 0; i < sizeof(g_dockedRefreshRates) / sizeof(g_dockedRefreshRates[0]); i++) {
if (g_dockedRefreshRates[i] == refreshRate) return i;
}
return 0xFF;
}
static void _changeOledElvssSettings(const uint32_t* offsets, const uint32_t* value, uint32_t size, uint32_t start) {
if (!g_config.dsiVirtAddr || !value || !size) return;
volatile uint32_t* dsi = (uint32_t*)g_config.dsiVirtAddr;
#define DSI_VIDEO_MODE_CONTROL 0x4E
#define DSI_WR_DATA 0xA
#define DSI_TRIGGER 0x13
#define MIPI_DSI_DCS_SHORT_WRITE_PARAM 0x15
#define MIPI_DSI_DCS_LONG_WRITE 0x39
#define MIPI_DCS_PRIV_SM_SET_REG_OFFSET 0xB0
#define MIPI_DCS_PRIV_SM_SET_ELVSS 0xB1
dsi[DSI_VIDEO_MODE_CONTROL] = true;
svcSleepThread(20000000);
dsi[DSI_WR_DATA] = MIPI_DSI_DCS_LONG_WRITE | (5 << 8);
dsi[DSI_WR_DATA] = 0x5A5A5AE2;
dsi[DSI_WR_DATA] = 0x5A;
dsi[DSI_TRIGGER] = 0;
for (size_t i = start; i < size; i++) {
dsi[DSI_WR_DATA] = ((MIPI_DCS_PRIV_SM_SET_REG_OFFSET | ((offsets[i] % 0x100) << 8)) << 8) | MIPI_DSI_DCS_SHORT_WRITE_PARAM;
dsi[DSI_TRIGGER] = 0;
dsi[DSI_WR_DATA] = ((MIPI_DCS_PRIV_SM_SET_ELVSS | (value[i] << 8)) << 8) | MIPI_DSI_DCS_SHORT_WRITE_PARAM;
dsi[DSI_TRIGGER] = 0;
}
dsi[DSI_WR_DATA] = MIPI_DSI_DCS_LONG_WRITE | (5 << 8);
dsi[DSI_WR_DATA] = 0xA55A5AE2;
dsi[DSI_WR_DATA] = 0xA5;
dsi[DSI_TRIGGER] = 0;
dsi[DSI_VIDEO_MODE_CONTROL] = false;
svcSleepThread(20000000);
}
void SetDockedState(bool isDocked) {
g_config.isDocked = isDocked;
}
bool Initialize(const DisplayRefreshConfig* config) {
if (!config) return false;
g_config = *config;
g_initialized = true;
return true;
}
void CorrectOledGamma(uint32_t refresh_rate) {
static uint32_t last_refresh_rate = 60;
static int counter = 0;
if (g_config.isDocked || refresh_rate < 45 || refresh_rate > 60) {
last_refresh_rate = 60;
return;
}
if (counter != 9) {
counter++;
return;
}
counter = 0;
uint32_t offsets[] = {0x1A, 0x24, 0x25};
uint32_t values[] = {2, 0, 0x83};
if (refresh_rate == 60) {
if (last_refresh_rate == 60) return;
} else if (refresh_rate == 45) {
if (last_refresh_rate == 45) return;
uint32_t vals[] = {4, 1, 0};
memcpy(values, vals, sizeof(vals));
} else if (refresh_rate == 50) {
if (last_refresh_rate == 50) return;
uint32_t vals[] = {3, 1, 0};
memcpy(values, vals, sizeof(vals));
} else if (refresh_rate == 55) {
if (last_refresh_rate == 55) return;
uint32_t vals[] = {3, 1, 0};
memcpy(values, vals, sizeof(vals));
} else {
return;
}
for (int i = 0; i < 5; i++) {
_changeOledElvssSettings(offsets, values, 3, 0);
}
last_refresh_rate = refresh_rate;
}
void SetAllowedDockedRatesIPC(uint32_t refreshRates, bool is720p) {
// Function kept for API compatibility but does nothing
(void)refreshRates;
(void)is720p;
}
uint8_t GetDockedHighestAllowed(void) {
return (g_dockedHighestRefreshRate > 60) ? g_dockedHighestRefreshRate : 60;
}
static void _getDockedHighestRefreshRate(uint32_t fd_in) {
uint8_t highestRefreshRate = 60;
uint32_t fd = fd_in;
if(!fd) nvOpen(&fd, "/dev/nvdisp-disp1");
NvdcModeDB2 db2 = {0};
int rc = nvIoctl(fd, NVDISP_GET_MODE_DB2, &db2);
if (rc == 0) {
for (size_t i = 0; i < db2.num_modes; i++) {
if (db2.modes[i].hActive < 1920 || db2.modes[i].vActive < 1080)
continue;
uint32_t v_total = db2.modes[i].vActive + db2.modes[i].vSyncWidth + db2.modes[i].vFrontPorch + db2.modes[i].vBackPorch;
uint32_t h_total = db2.modes[i].hActive + db2.modes[i].hSyncWidth + db2.modes[i].hFrontPorch + db2.modes[i].hBackPorch;
double refreshRate = round((double)(db2.modes[i].pclkKHz * 1000) / (double)(v_total * h_total));
if (highestRefreshRate < (uint8_t)refreshRate)
highestRefreshRate = (uint8_t)refreshRate;
}
} else {
g_dockedHighestRefreshRate = 60;
}
const size_t numRates = sizeof(g_dockedRefreshRates) / sizeof(g_dockedRefreshRates[0]);
if (highestRefreshRate > g_dockedRefreshRates[numRates - 1])
highestRefreshRate = g_dockedRefreshRates[numRates - 1];
NvdcMode2 display_b = {0};
rc = nvIoctl(fd, NVDISP_GET_MODE2, &display_b);
struct dpaux_read_0x100 {
uint32_t cmd;
uint32_t addr;
uint32_t size;
struct {
unsigned char link_rate;
unsigned int lane_count: 5;
unsigned int unk1: 2;
unsigned int isFramingEnhanced: 1;
unsigned char downspread;
unsigned char training_pattern;
unsigned char lane_pattern[4];
unsigned char unk2[8];
} set;
} dpaux = {6, 0x100, 0x10};
rc = nvIoctl(fd, NVDISP_GET_PANEL_DATA, &dpaux);
if (rc == 0) {
g_dockedLinkRate = dpaux.set.link_rate;
// if (display_b.hActive == 1920 && display_b.vActive == 1080 && highestRefreshRate > 75 && dpaux.set.link_rate < 20 && )
// highestRefreshRate = 75;
}
if (!fd_in) nvClose(fd);
g_dockedHighestRefreshRate = highestRefreshRate;
}
static bool _setPLLDHandheldRefreshRate(uint32_t new_refreshRate) {
if (!g_config.clkVirtAddr) return false;
uint32_t fd = 0;
if (nvOpen(&fd, "/dev/nvdisp-disp0")) {
return false;
}
struct dpaux_read {
uint32_t cmd;
uint32_t addr;
uint32_t size;
struct {
unsigned int rev_minor : 4;
unsigned int rev_major : 4;
unsigned char link_rate;
unsigned int lane_count: 5;
unsigned int unk1: 2;
unsigned int isFramingEnhanced: 1;
unsigned char unk2[13];
} DPCD;
} dpaux = {6, 0, 0x10};
int rc = nvIoctl(fd, NVDISP_GET_PANEL_DATA, &dpaux);
nvClose(fd);
if (rc != 0x75c) return false;
PLLD_BASE base = {0};
PLLD_MISC misc = {0};
memcpy(&base, (void*)(g_config.clkVirtAddr + 0xD0), 4);
memcpy(&misc, (void*)(g_config.clkVirtAddr + 0xDC), 4);
uint32_t value = ((base.PLLD_DIVN / base.PLLD_DIVM) * 10) / 4;
if (value == 0 || value == 80) return false;
if (new_refreshRate > g_handheldModeRefreshRate.max) {
new_refreshRate = g_handheldModeRefreshRate.max;
} else if (new_refreshRate < g_handheldModeRefreshRate.min) {
bool skip = false;
for (size_t i = 2; i <= 4; i++) {
if (new_refreshRate * i == 60) {
skip = true;
new_refreshRate = 60;
break;
}
}
if (!skip) {
for (size_t i = 2; i <= 4; i++) {
if (((new_refreshRate * i) >= g_handheldModeRefreshRate.min) && ((new_refreshRate * i) <= g_handheldModeRefreshRate.max)) {
skip = true;
new_refreshRate *= i;
break;
}
}
}
if (!skip) new_refreshRate = 60;
}
uint32_t pixelClock = (9375 * ((4096 * ((2 * base.PLLD_DIVN) + 1)) + misc.PLLD_SDM_DIN)) / (8 * base.PLLD_DIVM);
uint16_t refreshRateNow = pixelClock / (DSI_CLOCK_HZ / 60);
if (refreshRateNow == new_refreshRate) {
return true;
}
uint8_t base_refreshRate = new_refreshRate - (new_refreshRate % 5);
base.PLLD_DIVN = (4 * base_refreshRate) / 10;
base.PLLD_DIVM = 1;
uint64_t expected_pixel_clock = (DSI_CLOCK_HZ * new_refreshRate) / 60;
misc.PLLD_SDM_DIN = ((8 * base.PLLD_DIVM * expected_pixel_clock) / 9375) - (4096 * ((2 * base.PLLD_DIVN) + 1));
memcpy((void*)(g_config.clkVirtAddr + 0xD0), &base, 4);
memcpy((void*)(g_config.clkVirtAddr + 0xDC), &misc, 4);
return true;
}
static bool _setNvDispDockedRefreshRate(uint32_t new_refreshRate) {
if (g_config.isLite || !g_canChangeRefreshRateDocked)
return false;
uint32_t fd = 0;
if (nvOpen(&fd, "/dev/nvdisp-disp1")) {
return false;
}
NvdcMode2 display_b = {0};
int rc = nvIoctl(fd, NVDISP_GET_MODE2, &display_b);
if (rc != 0) {
nvClose(fd);
return false;
}
if (!display_b.pclkKHz) {
nvClose(fd);
return false;
}
if (!((display_b.vActive == 480 && display_b.hActive == 720) ||
(display_b.vActive == 720 && display_b.hActive == 1280) ||
(display_b.vActive == 1080 && display_b.hActive == 1920))) {
nvClose(fd);
return false;
}
if (display_b.vActive != g_lastVActiveSet) {
g_lastVActiveSet = display_b.vActive;
}
uint32_t h_total = display_b.hActive + display_b.hFrontPorch + display_b.hSyncWidth + display_b.hBackPorch;
uint32_t v_total = display_b.vActive + display_b.vFrontPorch + display_b.vSyncWidth + display_b.vBackPorch;
uint32_t refreshRateNow = ((display_b.pclkKHz) * 1000 + 999) / (h_total * v_total);
int8_t itr = -1;
const size_t numRates = sizeof(g_dockedRefreshRates) / sizeof(g_dockedRefreshRates[0]);
// Find closest matching refresh rate
if ((new_refreshRate <= 60) && ((60 % new_refreshRate) == 0)) {
itr = _getDockedRefreshRateIterator(60);
}
if (itr == -1) {
for (size_t i = 0; i < numRates; i++) {
uint8_t val = g_dockedRefreshRates[i];
if ((val % new_refreshRate) == 0) {
itr = i;
break;
}
}
}
if (itr == -1) {
if (!g_config.matchLowestDocked) {
itr = _getDockedRefreshRateIterator(60);
} else {
for (size_t i = 0; i < numRates; i++) {
if (new_refreshRate < g_dockedRefreshRates[i]) {
itr = i;
break;
}
}
}
}
if (itr == -1) itr = _getDockedRefreshRateIterator(60);
// Clamp to highest allowed refresh rate
if (g_dockedRefreshRates[itr] > g_dockedHighestRefreshRate) {
for (int8_t i = itr; i >= 0; i--) {
if (g_dockedRefreshRates[i] <= g_dockedHighestRefreshRate) {
itr = i;
break;
}
}
}
if (refreshRateNow == g_dockedRefreshRates[itr]) {
nvClose(fd);
return true;
}
if (itr >= 0 && itr < (int8_t)numRates) {
if (display_b.vActive == 720) {
uint32_t clock = ((h_total * v_total) * g_dockedRefreshRates[itr]) / 1000;
display_b.pclkKHz = clock;
} else {
display_b.hFrontPorch = g_dockedTimings1080p[itr].hFrontPorch;
display_b.hSyncWidth = g_dockedTimings1080p[itr].hSyncWidth;
display_b.hBackPorch = g_dockedTimings1080p[itr].hBackPorch;
display_b.vFrontPorch = g_dockedTimings1080p[itr].vFrontPorch;
display_b.vSyncWidth = g_dockedTimings1080p[itr].vSyncWidth;
display_b.vBackPorch = g_dockedTimings1080p[itr].vBackPorch;
display_b.pclkKHz = g_dockedTimings1080p[itr].pixelClock_kHz;
display_b.vmode = (g_dockedRefreshRates[itr] >= 100 ? 0x400000 : 0x200000);
display_b.unk1 = (g_dockedRefreshRates[itr] >= 100 ? 0x80 : 0);
display_b.sync = 3;
display_b.bitsPerPixel = 24;
}
rc = nvIoctl(fd, NVDISP_VALIDATE_MODE2, &display_b);
if (rc == 0) {
rc = nvIoctl(fd, NVDISP_SET_MODE2, &display_b);
}
}
nvClose(fd);
return true;
}
static bool _setNvDispHandheldRefreshRate(uint32_t new_refreshRate) {
if (!g_config.isRetroSUPER) return false;
if (!g_config.displaySync) {
g_wasRetroSuperTurnedOff = false;
} else if (g_wasRetroSuperTurnedOff) {
svcSleepThread(2000000000);
g_wasRetroSuperTurnedOff = false;
}
svcSleepThread(1000000000);
uint32_t fd = 0;
if (nvOpen(&fd, "/dev/nvdisp-disp0")) {
return false;
}
NvdcMode2 display_b = {0};
int rc = nvIoctl(fd, NVDISP_GET_MODE2, &display_b);
if (rc != 0) {
nvClose(fd);
return false;
}
if (!display_b.pclkKHz) {
nvClose(fd);
return false;
}
if ((display_b.vActive == 1280 && display_b.hActive == 720) == false) {
nvClose(fd);
return false;
}
uint32_t h_total = display_b.hActive + display_b.hFrontPorch + display_b.hSyncWidth + display_b.hBackPorch;
uint32_t v_total = display_b.vActive + display_b.vFrontPorch + display_b.vSyncWidth + display_b.vBackPorch;
uint32_t refreshRateNow = ((display_b.pclkKHz) * 1000 + 999) / (h_total * v_total);
if (new_refreshRate > g_handheldModeRefreshRate.max) {
new_refreshRate = g_handheldModeRefreshRate.max;
} else if (new_refreshRate < g_handheldModeRefreshRate.min) {
bool skip = false;
for (size_t i = 2; i <= 4; i++) {
if (new_refreshRate * i == 60) {
skip = true;
new_refreshRate = 60;
break;
}
}
if (!skip) {
for (size_t i = 2; i <= (sizeof(g_handheldTimingsRETRO) / sizeof(g_handheldTimingsRETRO[0])); i++) {
if (((new_refreshRate * i) >= g_handheldModeRefreshRate.min) && ((new_refreshRate * i) <= g_handheldModeRefreshRate.max)) {
skip = true;
new_refreshRate *= i;
break;
}
}
}
if (!skip) new_refreshRate = 60;
}
if (new_refreshRate == refreshRateNow) {
nvClose(fd);
return true;
}
uint32_t itr = (new_refreshRate - 40) / 5;
display_b.hFrontPorch = g_handheldTimingsRETRO[itr].hFrontPorch;
display_b.hSyncWidth = g_handheldTimingsRETRO[itr].hSyncWidth;
display_b.hBackPorch = g_handheldTimingsRETRO[itr].hBackPorch;
display_b.vFrontPorch = g_handheldTimingsRETRO[itr].vFrontPorch;
display_b.vSyncWidth = g_handheldTimingsRETRO[itr].vSyncWidth;
display_b.vBackPorch = g_handheldTimingsRETRO[itr].vBackPorch;
display_b.pclkKHz = g_handheldTimingsRETRO[itr].pixelClock_kHz;
rc = nvIoctl(fd, NVDISP_VALIDATE_MODE2, &display_b);
if (rc == 0) {
for (size_t i = 0; i < 5; i++) {
nvIoctl(fd, NVDISP_SET_MODE2, &display_b);
}
}
nvClose(fd);
return true;
}
bool SetRate(uint32_t new_refreshRate) {
if (!new_refreshRate || !g_initialized) return false;
uint32_t fd = 0;
if (g_config.isRetroSUPER && !g_config.isDocked) {
return _setNvDispHandheldRefreshRate(new_refreshRate);
}
else if ((!g_config.isRetroSUPER && g_config.isLite) || R_FAILED(nvOpen(&fd, "/dev/nvdisp-disp1"))) {
if (_setPLLDHandheldRefreshRate(new_refreshRate) == false)
return false;
}
else {
struct dpaux_read {
uint32_t cmd;
uint32_t addr;
uint32_t size;
struct {
unsigned int rev_minor : 4;
unsigned int rev_major : 4;
unsigned char link_rate;
unsigned int lane_count: 5;
unsigned int unk1: 2;
unsigned int isFramingEnhanced: 1;
unsigned char unk2[13];
} DPCD;
} dpaux = {6, 0, 0x10};
int rc = nvIoctl(fd, NVDISP_GET_PANEL_DATA, &dpaux);
nvClose(fd);
if (rc != 0) {
if (!g_config.isRetroSUPER) {
return _setPLLDHandheldRefreshRate(new_refreshRate);
} else {
return _setNvDispHandheldRefreshRate(new_refreshRate);
}
} else {
if(g_config.isDocked)
return _setNvDispDockedRefreshRate(new_refreshRate);
else
return true;
}
}
return false;
}
bool GetRate(uint32_t* out_refreshRate, bool internal) {
if (!out_refreshRate || !g_initialized || !g_config.clkVirtAddr) return false;
static uint32_t value = 60;
if (g_config.isRetroSUPER && !g_config.isDocked) {
uint32_t fd = 0;
PLLD_BASE temp = {0};
PLLD_MISC misc = {0};
memcpy(&temp, (void*)(g_config.clkVirtAddr + 0xD0), 4);
memcpy(&misc, (void*)(g_config.clkVirtAddr + 0xDC), 4);
value = ((temp.PLLD_DIVN / temp.PLLD_DIVM) * 10) / 4;
if (value != 0 && value != 80) {
if (!nvOpen(&fd, "/dev/nvdisp-disp0")) {
NvdcMode2 display_b = {0};
if (nvIoctl(fd, NVDISP_GET_MODE2, &display_b) == 0) {
uint64_t h_total = display_b.hActive + display_b.hFrontPorch + display_b.hSyncWidth + display_b.hBackPorch;
uint64_t v_total = display_b.vActive + display_b.vFrontPorch + display_b.vSyncWidth + display_b.vBackPorch;
uint64_t pixelClock = display_b.pclkKHz * 1000 + 999;
value = (u32)(pixelClock / (h_total * v_total));
}
nvClose(fd);
} else {
return false;
}
} else {
g_wasRetroSuperTurnedOff = true;
}
}
else if ((!g_config.isPossiblySpoofedRetro) || (g_config.isPossiblySpoofedRetro && !g_config.isRetroSUPER)) {
PLLD_BASE temp = {0};
PLLD_MISC misc = {0};
memcpy(&temp, (void*)(g_config.clkVirtAddr + 0xD0), 4);
memcpy(&misc, (void*)(g_config.clkVirtAddr + 0xDC), 4);
value = ((temp.PLLD_DIVN / temp.PLLD_DIVM) * 10) / 4;
if (value == 0 || value == 80) {
// Docked mode
if (g_config.isLite) return false;
g_config.isDocked = true;
if (!g_canChangeRefreshRateDocked) {
uint32_t fd = 0;
if (!nvOpen(&fd, "/dev/nvdisp-disp1")) {
struct dpaux_read_0x100 {
uint32_t cmd;
uint32_t addr;
uint32_t size;
struct {
unsigned char link_rate;
unsigned int lane_count: 5;
unsigned int unk1: 2;
unsigned int isFramingEnhanced: 1;
unsigned char downspread;
unsigned char training_pattern;
unsigned char lane_pattern[4];
unsigned char unk2[8];
} set;
} dpaux = {6, 0x100, 0x10};
int rc = nvIoctl(fd, NVDISP_GET_PANEL_DATA, &dpaux);
nvClose(fd);
if (rc == 0) {
_getDockedHighestRefreshRate(0);
g_canChangeRefreshRateDocked = true;
} else {
svcSleepThread(1000000000);
return false;
}
} else {
return false;
}
}
if(internal) {
*out_refreshRate = value;
return true;
}
uint32_t fd = 0;
if (!nvOpen(&fd, "/dev/nvdisp-disp1")) {
NvdcMode2 display_b = {0};
if (nvIoctl(fd, NVDISP_GET_MODE2, &display_b) == 0) {
if (!display_b.pclkKHz) {
nvClose(fd);
return false;
}
if (g_lastVActive != display_b.vActive) {
g_lastVActive = display_b.vActive;
_getDockedHighestRefreshRate(fd);
}
uint64_t h_total = display_b.hActive + display_b.hFrontPorch + display_b.hSyncWidth + display_b.hBackPorch;
uint64_t v_total = display_b.vActive + display_b.vFrontPorch + display_b.vSyncWidth + display_b.vBackPorch;
uint64_t pixelClock = display_b.pclkKHz * 1000 + 999;
value = (u32)(pixelClock / (h_total * v_total));
} else {
value = 60;
}
nvClose(fd);
} else {
value = 60;
}
}
else if (!g_config.isRetroSUPER) {
// Handheld mode
g_config.isDocked = false;
g_canChangeRefreshRateDocked = false;
uint32_t pixelClock = (9375ULL * ((4096 * ((2 * temp.PLLD_DIVN) + 1)) + misc.PLLD_SDM_DIN)) / (8 * temp.PLLD_DIVM);
value = pixelClock / (DSI_CLOCK_HZ / 60);
}
else {
return false;
}
}
*out_refreshRate = value;
return true;
}
void Shutdown(void) {
g_initialized = false;
memset(&g_config, 0, sizeof(g_config));
}
}

View File

@@ -0,0 +1,127 @@
/*
* Copyright (c) Souldbminer, based on reasearch by MasaGratoR and Cooler3D
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
namespace display {
typedef struct {
uint16_t hFrontPorch;
uint8_t hSyncWidth;
uint8_t hBackPorch;
uint8_t vFrontPorch;
uint8_t vSyncWidth;
uint8_t vBackPorch;
uint8_t VIC;
uint32_t pixelClock_kHz;
} DockedTimings;
typedef struct {
uint8_t hSyncWidth;
uint16_t hFrontPorch;
uint8_t hBackPorch;
uint8_t vSyncWidth;
uint16_t vFrontPorch;
uint8_t vBackPorch;
uint32_t pixelClock_kHz;
} HandheldTimings;
typedef struct {
uint8_t min;
uint8_t max;
} MinMaxRefreshRate;
typedef struct {
uint32_t unk0;
uint32_t hActive;
uint32_t vActive;
uint32_t hSyncWidth;
uint32_t vSyncWidth;
uint32_t hFrontPorch;
uint32_t vFrontPorch;
uint32_t hBackPorch;
uint32_t vBackPorch;
uint32_t pclkKHz;
uint32_t bitsPerPixel;
uint32_t vmode;
uint32_t sync;
uint32_t unk1;
uint32_t reserved;
} NvdcMode2;
typedef struct {
NvdcMode2 modes[201];
uint32_t num_modes;
} NvdcModeDB2;
typedef struct {
unsigned int PLLD_DIVM: 8;
unsigned int reserved_1: 3;
unsigned int PLLD_DIVN: 8;
unsigned int reserved_2: 1;
unsigned int PLLD_DIVP: 3;
unsigned int CSI_CLK_SRC: 1;
unsigned int reserved_3: 1;
unsigned int PLL_D: 1;
unsigned int reserved_4: 1;
unsigned int PLLD_LOCK: 1;
unsigned int reserved_5: 1;
unsigned int PLLD_REF_DIS: 1;
unsigned int PLLD_ENABLE: 1;
unsigned int PLLD_BYPASS: 1;
} PLLD_BASE;
typedef struct {
signed int PLLD_SDM_DIN: 16;
unsigned int PLLD_EN_SDM: 1;
unsigned int PLLD_LOCK_OVERRIDE: 1;
unsigned int PLLD_EN_LCKDET: 1;
unsigned int PLLD_FREQLOCK: 1;
unsigned int PLLD_IDDQ: 1;
unsigned int PLLD_ENABLE_CLK: 1;
unsigned int PLLD_KVCO: 1;
unsigned int PLLD_KCP: 2;
unsigned int PLLD_PTS: 2;
unsigned int PLLD_LDPULSE_ADJ: 3;
unsigned int reserved: 2;
} PLLD_MISC;
typedef struct {
uint64_t clkVirtAddr;
uint64_t dsiVirtAddr;
bool isDocked;
bool isLite;
bool isRetroSUPER;
bool isPossiblySpoofedRetro;
bool dontForce60InDocked;
bool matchLowestDocked;
bool displaySync;
bool displaySyncOutOfFocus60;
bool displaySyncDocked;
bool displaySyncDockedOutOfFocus60;
} DisplayRefreshConfig;
bool Initialize(const DisplayRefreshConfig* config);
void SetDockedState(bool isDocked);
bool SetRate(uint32_t new_refreshRate);
bool GetRate(uint32_t* out_refreshRate, bool internal);
uint8_t GetDockedHighestAllowed(void);
void CorrectOledGamma(uint32_t refresh_rate);
void SetAllowedDockedRatesIPC(uint32_t refreshRates, bool is720p);
void Shutdown(void);
}