sysclk: remove old hocclk, bump version
This commit is contained in:
237
Source/hoc-clk/sysmodule/src/board/board.cpp
Normal file
237
Source/hoc-clk/sysmodule/src/board/board.cpp
Normal 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. */
|
||||
}
|
||||
|
||||
}
|
||||
55
Source/hoc-clk/sysmodule/src/board/board.hpp
Normal file
55
Source/hoc-clk/sysmodule/src/board/board.hpp
Normal 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();
|
||||
|
||||
}
|
||||
230
Source/hoc-clk/sysmodule/src/board/board_freq.cpp
Normal file
230
Source/hoc-clk/sysmodule/src/board/board_freq.cpp
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
90
Source/hoc-clk/sysmodule/src/board/board_freq.hpp
Normal file
90
Source/hoc-clk/sysmodule/src/board/board_freq.hpp
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
111
Source/hoc-clk/sysmodule/src/board/board_fuse.cpp
Normal file
111
Source/hoc-clk/sysmodule/src/board/board_fuse.cpp
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
49
Source/hoc-clk/sysmodule/src/board/board_fuse.hpp
Normal file
49
Source/hoc-clk/sysmodule/src/board/board_fuse.hpp
Normal 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);
|
||||
|
||||
}
|
||||
188
Source/hoc-clk/sysmodule/src/board/board_load.cpp
Normal file
188
Source/hoc-clk/sysmodule/src/board/board_load.cpp
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
40
Source/hoc-clk/sysmodule/src/board/board_load.hpp
Normal file
40
Source/hoc-clk/sysmodule/src/board/board_load.hpp
Normal 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);
|
||||
|
||||
}
|
||||
72
Source/hoc-clk/sysmodule/src/board/board_misc.cpp
Normal file
72
Source/hoc-clk/sysmodule/src/board/board_misc.cpp
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
39
Source/hoc-clk/sysmodule/src/board/board_misc.hpp
Normal file
39
Source/hoc-clk/sysmodule/src/board/board_misc.hpp
Normal 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();
|
||||
|
||||
}
|
||||
53
Source/hoc-clk/sysmodule/src/board/board_name.cpp
Normal file
53
Source/hoc-clk/sysmodule/src/board/board_name.cpp
Normal 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);
|
||||
}
|
||||
|
||||
}
|
||||
38
Source/hoc-clk/sysmodule/src/board/board_name.hpp
Normal file
38
Source/hoc-clk/sysmodule/src/board/board_name.hpp
Normal 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);
|
||||
|
||||
}
|
||||
57
Source/hoc-clk/sysmodule/src/board/board_profile.cpp
Normal file
57
Source/hoc-clk/sysmodule/src/board/board_profile.cpp
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
34
Source/hoc-clk/sysmodule/src/board/board_profile.hpp
Normal file
34
Source/hoc-clk/sysmodule/src/board/board_profile.hpp
Normal 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();
|
||||
|
||||
}
|
||||
107
Source/hoc-clk/sysmodule/src/board/board_sensor.cpp
Normal file
107
Source/hoc-clk/sysmodule/src/board/board_sensor.cpp
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
36
Source/hoc-clk/sysmodule/src/board/board_sensor.hpp
Normal file
36
Source/hoc-clk/sysmodule/src/board/board_sensor.hpp
Normal 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);
|
||||
|
||||
}
|
||||
455
Source/hoc-clk/sysmodule/src/board/board_volt.cpp
Normal file
455
Source/hoc-clk/sysmodule/src/board/board_volt.cpp
Normal 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), ®, 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];
|
||||
}
|
||||
|
||||
}
|
||||
64
Source/hoc-clk/sysmodule/src/board/board_volt.hpp
Normal file
64
Source/hoc-clk/sysmodule/src/board/board_volt.hpp
Normal 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);
|
||||
|
||||
}
|
||||
736
Source/hoc-clk/sysmodule/src/board/display_refresh_rate.cpp
Normal file
736
Source/hoc-clk/sysmodule/src/board/display_refresh_rate.cpp
Normal 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));
|
||||
}
|
||||
}
|
||||
127
Source/hoc-clk/sysmodule/src/board/display_refresh_rate.hpp
Normal file
127
Source/hoc-clk/sysmodule/src/board/display_refresh_rate.hpp
Normal 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);
|
||||
}
|
||||
642
Source/hoc-clk/sysmodule/src/clock_manager.cpp
Normal file
642
Source/hoc-clk/sysmodule/src/clock_manager.cpp
Normal file
@@ -0,0 +1,642 @@
|
||||
/*
|
||||
* 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 "clock_manager.hpp"
|
||||
#include <cstring>
|
||||
#include "file_utils.hpp"
|
||||
#include "board/board.hpp"
|
||||
#include "process_management.hpp"
|
||||
#include "errors.hpp"
|
||||
#include "ipc_service.hpp"
|
||||
#include "kip.hpp"
|
||||
#include <i2c.h>
|
||||
#include "board/display_refresh_rate.hpp"
|
||||
#include <cstdio>
|
||||
#include <crc32.h>
|
||||
#include "config.hpp"
|
||||
#include "integrations.hpp"
|
||||
#include <nxExt/cpp/lockable_mutex.h>
|
||||
#include "kip.hpp"
|
||||
#include "governor.hpp"
|
||||
|
||||
#define HOSPPC_HAS_BOOST (hosversionAtLeast(7,0,0))
|
||||
|
||||
namespace clockManager {
|
||||
|
||||
|
||||
bool gRunning = false;
|
||||
LockableMutex gContextMutex;
|
||||
SysClkContext gContext = {};
|
||||
FreqTable gFreqTable[SysClkModule_EnumMax];
|
||||
std::uint64_t gLastTempLogNs = 0;
|
||||
std::uint64_t gLastFreqLogNs = 0;
|
||||
std::uint64_t gLastPowerLogNs = 0;
|
||||
std::uint64_t gLastCsvWriteNs = 0;
|
||||
|
||||
bool IsAssignableHz(SysClkModule module, std::uint32_t hz)
|
||||
{
|
||||
switch (module) {
|
||||
case SysClkModule_CPU:
|
||||
return hz >= 500000000;
|
||||
case SysClkModule_MEM:
|
||||
return hz >= 665600000;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
std::uint32_t GetMaxAllowedHz(SysClkModule module, SysClkProfile profile)
|
||||
{
|
||||
if (config::GetConfigValue(HocClkConfigValue_UncappedClocks)) {
|
||||
return ~0; // Integer limit, uncapped clocks ON
|
||||
} else {
|
||||
if (module == SysClkModule_GPU) {
|
||||
if (profile < SysClkProfile_HandheldCharging) {
|
||||
switch (board::GetSocType()) {
|
||||
case SysClkSocType_Erista:
|
||||
return 460800000;
|
||||
case SysClkSocType_Mariko:
|
||||
switch (config::GetConfigValue(KipConfigValue_marikoGpuUV)) {
|
||||
case 0:
|
||||
return 614400000;
|
||||
case 1:
|
||||
return 691200000;
|
||||
case 2:
|
||||
return 768000000;
|
||||
default:
|
||||
return 614400000;
|
||||
}
|
||||
default:
|
||||
return 460800000;
|
||||
}
|
||||
} else if (profile <= SysClkProfile_HandheldChargingUSB) {
|
||||
switch (board::GetSocType()) {
|
||||
case SysClkSocType_Erista:
|
||||
return 768000000;
|
||||
case SysClkSocType_Mariko:
|
||||
switch (config::GetConfigValue(KipConfigValue_marikoGpuUV)) {
|
||||
case 0:
|
||||
return 844800000;
|
||||
case 1:
|
||||
return 921600000;
|
||||
case 2:
|
||||
return 998400000;
|
||||
default:
|
||||
return 844800000;
|
||||
}
|
||||
default:
|
||||
return 768000000;
|
||||
}
|
||||
}
|
||||
} else if (module == SysClkModule_CPU) {
|
||||
if (profile < SysClkProfile_HandheldCharging && board::GetSocType() == SysClkSocType_Erista) {
|
||||
return 1581000000;
|
||||
} else {
|
||||
return ~0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::uint32_t GetNearestHz(SysClkModule module, std::uint32_t inHz, std::uint32_t maxHz)
|
||||
{
|
||||
std::uint32_t *freqs = &gFreqTable[module].list[0];
|
||||
size_t count = gFreqTable[module].count - 1;
|
||||
|
||||
size_t i = 0;
|
||||
while (i < count) {
|
||||
if (maxHz > 0 && freqs[i] >= maxHz) {
|
||||
break;
|
||||
}
|
||||
if (inHz <= ((std::uint64_t)freqs[i] + freqs[i + 1]) / 2) {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
return freqs[i];
|
||||
}
|
||||
|
||||
void ResetToStockClocks()
|
||||
{
|
||||
board::ResetToStockCpu();
|
||||
if (config::GetConfigValue(HorizonOCConfigValue_LiveCpuUv)) {
|
||||
if (board::GetSocType() == SysClkSocType_Erista)
|
||||
board::SetDfllTunings(config::GetConfigValue(KipConfigValue_eristaCpuUV), 0, 1581000000);
|
||||
else
|
||||
board::SetDfllTunings(config::GetConfigValue(KipConfigValue_marikoCpuUVLow), config::GetConfigValue(KipConfigValue_marikoCpuUVHigh), board::CalculateTbreak(config::GetConfigValue(KipConfigValue_tableConf)));
|
||||
}
|
||||
|
||||
board::ResetToStockGpu();
|
||||
}
|
||||
|
||||
bool ConfigIntervalTimeout(SysClkConfigValue intervalMsConfigValue, std::uint64_t ns, std::uint64_t *lastLogNs)
|
||||
{
|
||||
std::uint64_t logInterval = config::GetConfigValue(intervalMsConfigValue) * 1000000ULL;
|
||||
bool shouldLog = logInterval && ((ns - *lastLogNs) > logInterval);
|
||||
|
||||
if (shouldLog) {
|
||||
*lastLogNs = ns;
|
||||
}
|
||||
|
||||
return shouldLog;
|
||||
}
|
||||
|
||||
void RefreshFreqTableRow(SysClkModule module)
|
||||
{
|
||||
std::scoped_lock lock{gContextMutex};
|
||||
|
||||
std::uint32_t freqs[SYSCLK_FREQ_LIST_MAX];
|
||||
std::uint32_t count;
|
||||
|
||||
fileUtils::LogLine("[mgr] %s freq list refresh", board::GetModuleName(module, true));
|
||||
board::GetFreqList(module, &freqs[0], SYSCLK_FREQ_LIST_MAX, &count);
|
||||
|
||||
std::uint32_t *hz = &gFreqTable[module].list[0];
|
||||
gFreqTable[module].count = 0;
|
||||
for (std::uint32_t i = 0; i < count; i++) {
|
||||
if (!IsAssignableHz(module, freqs[i])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
*hz = freqs[i];
|
||||
fileUtils::LogLine("[mgr] %02u - %u - %u.%u MHz", gFreqTable[module].count, *hz, *hz / 1000000, *hz / 100000 - *hz / 1000000 * 10);
|
||||
|
||||
gFreqTable[module].count++;
|
||||
hz++;
|
||||
}
|
||||
|
||||
fileUtils::LogLine("[mgr] count = %u", gFreqTable[module].count);
|
||||
}
|
||||
|
||||
void HandleSafetyFeatures()
|
||||
{
|
||||
if (config::GetConfigValue(HocClkConfigValue_HandheldTDP) && (gContext.profile != SysClkProfile_Docked)) {
|
||||
if (board::GetConsoleType() == HorizonOCConsoleType_Hoag) {
|
||||
if (board::GetPowerMw(SysClkPowerSensor_Avg) < -(int)config::GetConfigValue(HocClkConfigValue_LiteTDPLimit)) {
|
||||
ResetToStockClocks();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (board::GetPowerMw(SysClkPowerSensor_Avg) < -(int)config::GetConfigValue(HocClkConfigValue_HandheldTDPLimit)) {
|
||||
ResetToStockClocks();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (((tmp451TempSoc() / 1000) > (int)config::GetConfigValue(HocClkConfigValue_ThermalThrottleThreshold)) && config::GetConfigValue(HocClkConfigValue_ThermalThrottle)) {
|
||||
ResetToStockClocks();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void HandleMiscFeatures()
|
||||
{
|
||||
if (config::GetConfigValue(HorizonOCConfigValue_BatteryChargeCurrent)) {
|
||||
I2c_Bq24193_SetFastChargeCurrentLimit(config::GetConfigValue(HorizonOCConfigValue_BatteryChargeCurrent));
|
||||
}
|
||||
}
|
||||
|
||||
void DVFSBeforeSet(u32 targetHz)
|
||||
{
|
||||
s32 dvfsOffset = config::GetConfigValue(HorizonOCConfigValue_DVFSOffset);
|
||||
u32 vmin = board::GetMinimumGpuVmin(targetHz / 1000000, board::GetGpuSpeedoBracket()) + dvfsOffset;
|
||||
|
||||
board::PcvHijackGpuVolts(vmin);
|
||||
|
||||
/* Update the voltage. */
|
||||
if (I2c_BuckConverter_GetMvOut(&I2c_Mariko_GPU) < vmin) {
|
||||
I2c_BuckConverter_SetMvOut(&I2c_Mariko_GPU, vmin);
|
||||
}
|
||||
|
||||
gContext.voltages[HocClkVoltage_GPU] = vmin * 1000;
|
||||
}
|
||||
|
||||
void DVFSAfterSet(u32 targetHz)
|
||||
{
|
||||
s32 dvfsOffset = config::GetConfigValue(HorizonOCConfigValue_DVFSOffset);
|
||||
dvfsOffset = std::max(dvfsOffset, -80);
|
||||
u32 vmin = board::GetMinimumGpuVmin(targetHz / 1000000, board::GetGpuSpeedoBracket());
|
||||
|
||||
if (vmin) {
|
||||
vmin += dvfsOffset;
|
||||
}
|
||||
|
||||
u32 maxHz = GetMaxAllowedHz(SysClkModule_GPU, gContext.profile);
|
||||
u32 nearestHz = GetNearestHz(SysClkModule_GPU, targetHz, maxHz);
|
||||
board::PcvHijackGpuVolts(vmin);
|
||||
|
||||
if (targetHz) {
|
||||
board::SetHz(SysClkModule_GPU, ~0);
|
||||
board::SetHz(SysClkModule_GPU, nearestHz);
|
||||
} else {
|
||||
board::SetHz(SysClkModule_GPU, ~0);
|
||||
board::ResetToStockGpu();
|
||||
}
|
||||
}
|
||||
|
||||
void HandleCpuUv()
|
||||
{
|
||||
if (board::GetSocType() == SysClkSocType_Erista)
|
||||
board::SetDfllTunings(config::GetConfigValue(KipConfigValue_eristaCpuUV), 0, 1581000000);
|
||||
else
|
||||
board::SetDfllTunings(config::GetConfigValue(KipConfigValue_marikoCpuUVLow), config::GetConfigValue(KipConfigValue_marikoCpuUVHigh), board::CalculateTbreak(config::GetConfigValue(KipConfigValue_tableConf)));
|
||||
}
|
||||
|
||||
void DVFSReset()
|
||||
{
|
||||
if (board::GetSocType() == SysClkSocType_Mariko && config::GetConfigValue(HorizonOCConfigValue_DVFSMode) == DVFSMode_Hijack) {
|
||||
board::PcvHijackGpuVolts(0);
|
||||
|
||||
u32 targetHz = gContext.overrideFreqs[SysClkModule_GPU];
|
||||
if (!targetHz) {
|
||||
targetHz = config::GetAutoClockHz(gContext.applicationId, SysClkModule_GPU, gContext.profile, false);
|
||||
if (!targetHz) {
|
||||
targetHz = config::GetAutoClockHz(GLOBAL_PROFILE_ID, SysClkModule_GPU, gContext.profile, false);
|
||||
}
|
||||
}
|
||||
u32 maxHz = GetMaxAllowedHz(SysClkModule_GPU, gContext.profile);
|
||||
u32 nearestHz = GetNearestHz(SysClkModule_GPU, targetHz, maxHz);
|
||||
|
||||
board::SetHz(SysClkModule_GPU, ~0);
|
||||
if (targetHz) {
|
||||
board::SetHz(SysClkModule_GPU, nearestHz);
|
||||
} else {
|
||||
board::ResetToStockGpu();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HandleFreqReset(SysClkModule module, bool isBoost)
|
||||
{
|
||||
switch (module) {
|
||||
case SysClkModule_CPU:
|
||||
if (!(isBoost || (config::GetConfigValue(HocClkConfigValue_OverwriteBoostMode) && isBoost)))
|
||||
board::ResetToStockCpu();
|
||||
if (config::GetConfigValue(HorizonOCConfigValue_LiveCpuUv)) {
|
||||
if (board::GetSocType() == SysClkSocType_Erista)
|
||||
board::SetDfllTunings(config::GetConfigValue(KipConfigValue_eristaCpuUV), 0, 1581000000);
|
||||
else
|
||||
board::SetDfllTunings(config::GetConfigValue(KipConfigValue_marikoCpuUVLow), config::GetConfigValue(KipConfigValue_marikoCpuUVHigh), board::CalculateTbreak(config::GetConfigValue(KipConfigValue_tableConf)));
|
||||
}
|
||||
break;
|
||||
case SysClkModule_GPU:
|
||||
board::ResetToStockGpu();
|
||||
break;
|
||||
case SysClkModule_MEM:
|
||||
board::ResetToStockMem();
|
||||
DVFSReset();
|
||||
break;
|
||||
case HorizonOCModule_Display:
|
||||
if (config::GetConfigValue(HorizonOCConfigValue_OverwriteRefreshRate)) {
|
||||
board::ResetToStockDisplay();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void SetClocks(bool isBoost)
|
||||
{
|
||||
std::uint32_t targetHz = 0;
|
||||
std::uint32_t maxHz = 0;
|
||||
std::uint32_t nearestHz = 0;
|
||||
|
||||
if (isBoost && !config::GetConfigValue(HocClkConfigValue_OverwriteBoostMode)) {
|
||||
u32 boostFreq = board::GetHz(SysClkModule_CPU);
|
||||
if (boostFreq / 1000000 > 1785) {
|
||||
board::SetHz(SysClkModule_CPU, boostFreq);
|
||||
}
|
||||
return; // Return if we aren't overwriting boost mode
|
||||
}
|
||||
|
||||
bool returnRaw = false; // Return a value scaled to MHz instead of raw value
|
||||
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++) {
|
||||
u32 oldHz = board::GetHz((SysClkModule)module); // Get Old hz (used primarily for DVFS Logic)
|
||||
|
||||
if (module > SysClkModule_MEM)
|
||||
returnRaw = true;
|
||||
else
|
||||
returnRaw = false;
|
||||
targetHz = gContext.overrideFreqs[module];
|
||||
if (!targetHz) {
|
||||
targetHz = config::GetAutoClockHz(gContext.applicationId, (SysClkModule)module, gContext.profile, returnRaw);
|
||||
if (!targetHz)
|
||||
targetHz = config::GetAutoClockHz(GLOBAL_PROFILE_ID, (SysClkModule)module, gContext.profile, returnRaw);
|
||||
}
|
||||
|
||||
if (module == HorizonOCModule_Governor) {
|
||||
governor::HandleGovernor(targetHz);
|
||||
}
|
||||
|
||||
bool noCPU = governor::isCpuGovernorEnabled;
|
||||
bool noGPU = governor::isGpuGovernorEnabled;
|
||||
bool noDisp = governor::isVRREnabled;
|
||||
if (noDisp && module == HorizonOCModule_Display)
|
||||
continue;
|
||||
|
||||
if (module == HorizonOCModule_Display && config::GetConfigValue(HorizonOCConfigValue_OverwriteRefreshRate) && !noDisp) {
|
||||
if (targetHz) {
|
||||
board::SetHz(HorizonOCModule_Display, targetHz);
|
||||
gContext.freqs[HorizonOCModule_Display] = targetHz;
|
||||
gContext.realFreqs[HorizonOCModule_Display] = targetHz;
|
||||
} else {
|
||||
HandleFreqReset(HorizonOCModule_Display, isBoost);
|
||||
}
|
||||
}
|
||||
|
||||
// Skip GPU and CPU if governors handle them
|
||||
if (module > SysClkModule_MEM) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (noCPU && module == SysClkModule_CPU)
|
||||
continue;
|
||||
if (noGPU && module == SysClkModule_GPU)
|
||||
continue;
|
||||
|
||||
if (targetHz) {
|
||||
maxHz = GetMaxAllowedHz((SysClkModule)module, gContext.profile);
|
||||
nearestHz = GetNearestHz((SysClkModule)module, targetHz, maxHz);
|
||||
|
||||
if (nearestHz != gContext.freqs[module]) {
|
||||
fileUtils::LogLine(
|
||||
"[mgr] %s clock set : %u.%u MHz (target = %u.%u MHz)",
|
||||
board::GetModuleName((SysClkModule)module, true),
|
||||
nearestHz / 1000000, nearestHz / 100000 - nearestHz / 1000000 * 10,
|
||||
targetHz / 1000000, targetHz / 100000 - targetHz / 1000000 * 10
|
||||
);
|
||||
|
||||
if (module == SysClkModule_MEM && board::GetSocType() == SysClkSocType_Mariko && targetHz > oldHz && config::GetConfigValue(HorizonOCConfigValue_DVFSMode) == DVFSMode_Hijack) {
|
||||
DVFSBeforeSet(targetHz);
|
||||
}
|
||||
|
||||
board::SetHz((SysClkModule)module, nearestHz);
|
||||
gContext.freqs[module] = nearestHz;
|
||||
|
||||
if (module == SysClkModule_CPU && config::GetConfigValue(HorizonOCConfigValue_LiveCpuUv)) {
|
||||
HandleCpuUv();
|
||||
}
|
||||
|
||||
if (module == SysClkModule_MEM && board::GetSocType() == SysClkSocType_Mariko && targetHz < oldHz && config::GetConfigValue(HorizonOCConfigValue_DVFSMode) == DVFSMode_Hijack) {
|
||||
DVFSAfterSet(targetHz);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
HandleFreqReset((SysClkModule)module, isBoost);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool RefreshContext()
|
||||
{
|
||||
bool hasChanged = false;
|
||||
|
||||
std::uint32_t mode = 0;
|
||||
Result rc = apmExtGetCurrentPerformanceConfiguration(&mode);
|
||||
ASSERT_RESULT_OK(rc, "apmExtGetCurrentPerformanceConfiguration");
|
||||
|
||||
std::uint64_t applicationId = processManagement::GetCurrentApplicationId();
|
||||
if (applicationId != gContext.applicationId) {
|
||||
fileUtils::LogLine("[mgr] TitleID change: %016lX", applicationId);
|
||||
gContext.applicationId = applicationId;
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
SysClkProfile profile = board::GetProfile();
|
||||
if (profile != gContext.profile) {
|
||||
fileUtils::LogLine("[mgr] Profile change: %s", board::GetProfileName(profile, true));
|
||||
gContext.profile = profile;
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
// restore clocks to stock values on app or profile change
|
||||
if (hasChanged) {
|
||||
board::ResetToStock();
|
||||
if (board::GetSocType() == SysClkSocType_Mariko && config::GetConfigValue(HorizonOCConfigValue_DVFSMode) == DVFSMode_Hijack) {
|
||||
board::PcvHijackGpuVolts(0);
|
||||
board::SetHz(SysClkModule_GPU, ~0);
|
||||
board::ResetToStockGpu();
|
||||
}
|
||||
WaitForNextTick();
|
||||
}
|
||||
|
||||
std::uint32_t hz = 0;
|
||||
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++) {
|
||||
hz = board::GetHz((SysClkModule)module);
|
||||
if (hz != 0 && hz != gContext.freqs[module]) {
|
||||
fileUtils::LogLine("[mgr] %s clock change: %u.%u MHz", board::GetModuleName((SysClkModule)module, true), hz / 1000000, hz / 100000 - hz / 1000000 * 10);
|
||||
gContext.freqs[module] = hz;
|
||||
hasChanged = true;
|
||||
}
|
||||
|
||||
hz = config::GetOverrideHz((SysClkModule)module);
|
||||
if (hz != gContext.overrideFreqs[module]) {
|
||||
if (hz) {
|
||||
fileUtils::LogLine("[mgr] %s override change: %u.%u MHz", board::GetModuleName((SysClkModule)module, true), hz / 1000000, hz / 100000 - hz / 1000000 * 10);
|
||||
}
|
||||
gContext.overrideFreqs[module] = hz;
|
||||
hasChanged = true;
|
||||
}
|
||||
}
|
||||
|
||||
std::uint64_t ns = armTicksToNs(armGetSystemTick());
|
||||
|
||||
// temperatures do not and should not force a refresh, hasChanged untouched
|
||||
std::uint32_t millis = 0;
|
||||
bool shouldLogTemp = ConfigIntervalTimeout(SysClkConfigValue_TempLogIntervalMs, ns, &gLastTempLogNs);
|
||||
for (unsigned int sensor = 0; sensor < SysClkThermalSensor_EnumMax; sensor++) {
|
||||
millis = board::GetTemperatureMilli((SysClkThermalSensor)sensor);
|
||||
if (shouldLogTemp) {
|
||||
fileUtils::LogLine("[mgr] %s temp: %u.%u °C", board::GetThermalSensorName((SysClkThermalSensor)sensor, true), millis / 1000, (millis - millis / 1000 * 1000) / 100);
|
||||
}
|
||||
gContext.temps[sensor] = millis;
|
||||
}
|
||||
|
||||
// power stats do not and should not force a refresh, hasChanged untouched
|
||||
std::int32_t mw = 0;
|
||||
bool shouldLogPower = ConfigIntervalTimeout(SysClkConfigValue_PowerLogIntervalMs, ns, &gLastPowerLogNs);
|
||||
for (unsigned int sensor = 0; sensor < SysClkPowerSensor_EnumMax; sensor++) {
|
||||
mw = board::GetPowerMw((SysClkPowerSensor)sensor);
|
||||
if (shouldLogPower) {
|
||||
fileUtils::LogLine("[mgr] Power %s: %d mW", board::GetPowerSensorName((SysClkPowerSensor)sensor, false), mw);
|
||||
}
|
||||
gContext.power[sensor] = mw;
|
||||
}
|
||||
|
||||
// real freqs do not and should not force a refresh, hasChanged untouched
|
||||
std::uint32_t realHz = 0;
|
||||
bool shouldLogFreq = ConfigIntervalTimeout(SysClkConfigValue_FreqLogIntervalMs, ns, &gLastFreqLogNs);
|
||||
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++) {
|
||||
realHz = board::GetRealHz((SysClkModule)module);
|
||||
if (shouldLogFreq) {
|
||||
fileUtils::LogLine("[mgr] %s real freq: %u.%u MHz", board::GetModuleName((SysClkModule)module, true), realHz / 1000000, realHz / 100000 - realHz / 1000000 * 10);
|
||||
}
|
||||
gContext.realFreqs[module] = realHz;
|
||||
}
|
||||
|
||||
// ram load do not and should not force a refresh, hasChanged untouched
|
||||
for (unsigned int loadSource = 0; loadSource < SysClkPartLoad_EnumMax; loadSource++) {
|
||||
gContext.partLoad[loadSource] = board::GetPartLoad((SysClkPartLoad)loadSource);
|
||||
}
|
||||
|
||||
for (unsigned int voltageSource = 0; voltageSource < HocClkVoltage_EnumMax; voltageSource++) {
|
||||
gContext.voltages[voltageSource] = board::GetVoltage((HocClkVoltage)voltageSource);
|
||||
}
|
||||
|
||||
if (ConfigIntervalTimeout(SysClkConfigValue_CsvWriteIntervalMs, ns, &gLastCsvWriteNs)) {
|
||||
fileUtils::WriteContextToCsv(&gContext);
|
||||
}
|
||||
|
||||
// this->context->maxDisplayFreq = board::GetHighestDockedDisplayRate();
|
||||
u32 targetHz = gContext.overrideFreqs[HorizonOCModule_Display];
|
||||
if (!targetHz) {
|
||||
targetHz = config::GetAutoClockHz(gContext.applicationId, HorizonOCModule_Display, gContext.profile, true);
|
||||
if (!targetHz)
|
||||
targetHz = config::GetAutoClockHz(GLOBAL_PROFILE_ID, HorizonOCModule_Display, gContext.profile, true);
|
||||
}
|
||||
|
||||
if (board::GetConsoleType() != HorizonOCConsoleType_Hoag)
|
||||
board::SetDisplayRefreshDockedState(gContext.profile == SysClkProfile_Docked);
|
||||
|
||||
if (gContext.isSaltyNXInstalled)
|
||||
gContext.fps = integrations::GetSaltyNXFPS();
|
||||
else
|
||||
gContext.fps = 254; // N/A
|
||||
|
||||
if (gContext.isSaltyNXInstalled)
|
||||
gContext.resolutionHeight = integrations::GetSaltyNXResolutionHeight();
|
||||
else
|
||||
gContext.resolutionHeight = 0; // N/A
|
||||
|
||||
return hasChanged;
|
||||
}
|
||||
|
||||
void Initialize()
|
||||
{
|
||||
config::Initialize();
|
||||
|
||||
gContext = {};
|
||||
gContext.applicationId = 0;
|
||||
gContext.profile = SysClkProfile_Handheld;
|
||||
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++) {
|
||||
gContext.freqs[module] = 0;
|
||||
gContext.realFreqs[module] = 0;
|
||||
gContext.overrideFreqs[module] = 0;
|
||||
RefreshFreqTableRow((SysClkModule)module);
|
||||
}
|
||||
|
||||
gRunning = false;
|
||||
gLastTempLogNs = 0;
|
||||
gLastCsvWriteNs = 0;
|
||||
|
||||
kip::GetKipData();
|
||||
|
||||
board::FuseData *fuse = board::GetFuseData();
|
||||
|
||||
gContext.speedos[0] = fuse->cpuSpeedo;
|
||||
gContext.speedos[1] = fuse->gpuSpeedo;
|
||||
gContext.speedos[2] = fuse->socSpeedo;
|
||||
gContext.iddq[0] = fuse->cpuIDDQ;
|
||||
gContext.iddq[1] = fuse->gpuIDDQ;
|
||||
gContext.iddq[2] = fuse->socIDDQ;
|
||||
gContext.waferX = fuse->waferX;
|
||||
gContext.waferY = fuse->waferY;
|
||||
|
||||
gContext.dramID = board::GetDramID();
|
||||
gContext.isDram8GB = board::IsDram8GB();
|
||||
board::SetGpuSchedulingMode((GpuSchedulingMode)config::GetConfigValue(HorizonOCConfigValue_GPUScheduling), (GpuSchedulingOverrideMethod)config::GetConfigValue(HorizonOCConfigValue_GPUSchedulingMethod));
|
||||
gContext.gpuSchedulingMode = (GpuSchedulingMode)config::GetConfigValue(HorizonOCConfigValue_GPUScheduling);
|
||||
|
||||
gContext.isSysDockInstalled = integrations::GetSysDockState();
|
||||
gContext.isSaltyNXInstalled = integrations::GetSaltyNXState();
|
||||
if (gContext.isSaltyNXInstalled) {
|
||||
integrations::LoadSaltyNX();
|
||||
}
|
||||
|
||||
gContext.isUsingRetroSuper = integrations::GetRETROSuperStatus();
|
||||
governor::startThreads();
|
||||
}
|
||||
|
||||
void Exit()
|
||||
{
|
||||
governor::exitThreads();
|
||||
config::Exit();
|
||||
}
|
||||
|
||||
SysClkContext GetCurrentContext()
|
||||
{
|
||||
std::scoped_lock lock{gContextMutex};
|
||||
return gContext;
|
||||
}
|
||||
|
||||
void SetRunning(bool running)
|
||||
{
|
||||
gRunning = running;
|
||||
}
|
||||
|
||||
bool Running()
|
||||
{
|
||||
return gRunning;
|
||||
}
|
||||
|
||||
void GetFreqList(SysClkModule module, std::uint32_t *list, std::uint32_t maxCount, std::uint32_t *outCount)
|
||||
{
|
||||
ASSERT_ENUM_VALID(SysClkModule, module);
|
||||
|
||||
*outCount = std::min(maxCount, gFreqTable[module].count);
|
||||
memcpy(list, &gFreqTable[module].list[0], *outCount * sizeof(gFreqTable[0].list[0]));
|
||||
}
|
||||
|
||||
void Tick()
|
||||
{
|
||||
fileUtils::LogLine("CPU Temp: %d", board::GetTemperatureMilli(HorizonOCThermalSensor_CPU));
|
||||
std::scoped_lock lock{gContextMutex};
|
||||
std::uint32_t mode = 0;
|
||||
Result rc = apmExtGetCurrentPerformanceConfiguration(&mode);
|
||||
ASSERT_RESULT_OK(rc, "apmExtGetCurrentPerformanceConfiguration");
|
||||
|
||||
bool isBoost = apmExtIsBoostMode(mode);
|
||||
|
||||
HandleSafetyFeatures();
|
||||
|
||||
if (RefreshContext() || config::Refresh()) {
|
||||
HandleMiscFeatures();
|
||||
SetClocks(isBoost);
|
||||
}
|
||||
}
|
||||
|
||||
void WaitForNextTick()
|
||||
{
|
||||
if (board::GetHz(SysClkModule_MEM) > 665000000)
|
||||
svcSleepThread(config::GetConfigValue(SysClkConfigValue_PollingIntervalMs) * 1000000ULL);
|
||||
else
|
||||
svcSleepThread(5000 * 1000000ULL); // 5 seconds in sleep mode
|
||||
}
|
||||
} // namespace clockManager
|
||||
69
Source/hoc-clk/sysmodule/src/clock_manager.hpp
Normal file
69
Source/hoc-clk/sysmodule/src/clock_manager.hpp
Normal file
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* 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>
|
||||
#include <nxExt/cpp/lockable_mutex.h>
|
||||
|
||||
namespace clockManager {
|
||||
|
||||
struct FreqTable {
|
||||
std::uint32_t count;
|
||||
std::uint32_t list[SYSCLK_FREQ_LIST_MAX];
|
||||
};
|
||||
|
||||
|
||||
extern bool hasChanged;
|
||||
|
||||
// instance variables
|
||||
extern bool gRunning;
|
||||
extern LockableMutex gContextMutex;
|
||||
extern SysClkContext gContext;
|
||||
extern FreqTable gFreqTable[SysClkModule_EnumMax];
|
||||
extern std::uint64_t gLastTempLogNs;
|
||||
extern std::uint64_t gLastFreqLogNs;
|
||||
extern std::uint64_t gLastPowerLogNs;
|
||||
extern std::uint64_t gLastCsvWriteNs;
|
||||
|
||||
|
||||
void Initialize();
|
||||
void Exit();
|
||||
|
||||
SysClkContext GetCurrentContext();
|
||||
|
||||
void SetRunning(bool running);
|
||||
bool Running();
|
||||
|
||||
std::uint32_t GetMaxAllowedHz(SysClkModule module, SysClkProfile profile);
|
||||
bool IsAssignableHz(SysClkModule module, std::uint32_t hz);
|
||||
|
||||
void GetFreqList(SysClkModule module, std::uint32_t* list, std::uint32_t maxCount, std::uint32_t* outCount);
|
||||
|
||||
void Tick();
|
||||
void WaitForNextTick();
|
||||
}
|
||||
477
Source/hoc-clk/sysmodule/src/config.cpp
Normal file
477
Source/hoc-clk/sysmodule/src/config.cpp
Normal file
@@ -0,0 +1,477 @@
|
||||
/*
|
||||
* 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 "config.hpp"
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <atomic>
|
||||
#include <initializer_list>
|
||||
#include <minIni.h>
|
||||
#include <nxExt.h>
|
||||
#include "board/board.hpp"
|
||||
#include "errors.hpp"
|
||||
#include "file_utils.hpp"
|
||||
|
||||
namespace config {
|
||||
|
||||
uint64_t configValues[SysClkConfigValue_EnumMax];
|
||||
|
||||
namespace {
|
||||
|
||||
bool gLoaded = false;
|
||||
std::string gPath;
|
||||
time_t gMtime = 0;
|
||||
std::atomic_bool gEnabled{false};
|
||||
std::uint32_t gOverrideFreqs[SysClkModule_EnumMax];
|
||||
std::map<std::tuple<std::uint64_t, SysClkProfile, SysClkModule>, std::uint32_t> gProfileMHzMap;
|
||||
std::map<std::uint64_t, std::uint8_t> gProfileCountMap;
|
||||
LockableMutex gConfigMutex;
|
||||
LockableMutex gOverrideMutex;
|
||||
|
||||
time_t CheckModificationTime() {
|
||||
time_t mtime = 0;
|
||||
struct stat st;
|
||||
if (stat(gPath.c_str(), &st) == 0) {
|
||||
mtime = st.st_mtime;
|
||||
}
|
||||
return mtime;
|
||||
}
|
||||
|
||||
std::uint32_t FindClockMHz(std::uint64_t tid, SysClkModule module, SysClkProfile profile) {
|
||||
if (gLoaded) {
|
||||
auto it = gProfileMHzMap.find(std::make_tuple(tid, profile, module));
|
||||
if (it != gProfileMHzMap.end()) {
|
||||
return it->second;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::uint32_t FindClockHzFromProfiles(std::uint64_t tid, SysClkModule module, std::initializer_list<SysClkProfile> profiles, u32 mhzMultiplier = 1000000) {
|
||||
std::uint32_t mhz = 0;
|
||||
|
||||
if (gLoaded) {
|
||||
for (auto profile: profiles) {
|
||||
mhz = FindClockMHz(tid, module, profile);
|
||||
if (mhz) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return std::max((std::uint32_t)0, mhz * mhzMultiplier);
|
||||
}
|
||||
|
||||
int BrowseIniFunc(const char* section, const char* key, const char* value, void* userdata) {
|
||||
(void)userdata;
|
||||
std::uint64_t input;
|
||||
if (!strcmp(section, CONFIG_VAL_SECTION)) {
|
||||
for (unsigned int kval = 0; kval < SysClkConfigValue_EnumMax; kval++) {
|
||||
if (!strcmp(key, sysclkFormatConfigValue((SysClkConfigValue)kval, false))) {
|
||||
input = strtoul(value, NULL, 0);
|
||||
if (!sysclkValidConfigValue((SysClkConfigValue)kval, input)) {
|
||||
input = sysclkDefaultConfigValue((SysClkConfigValue)kval);
|
||||
fileUtils::LogLine("[cfg] Invalid value for key '%s' in section '%s': using default %d", key, section, input);
|
||||
}
|
||||
configValues[kval] = input;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
fileUtils::LogLine("[cfg] Skipping key '%s' in section '%s': Unrecognized config value", key, section);
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::uint64_t tid = strtoul(section, NULL, 16);
|
||||
|
||||
if (!tid || strlen(section) != 16) {
|
||||
fileUtils::LogLine("[cfg] Skipping key '%s' in section '%s': Invalid TitleID", key, section);
|
||||
return 1;
|
||||
}
|
||||
|
||||
SysClkProfile parsedProfile = SysClkProfile_EnumMax;
|
||||
SysClkModule parsedModule = SysClkModule_EnumMax;
|
||||
|
||||
for (unsigned int profile = 0; profile < SysClkProfile_EnumMax; profile++) {
|
||||
const char* profileCode = board::GetProfileName((SysClkProfile)profile, false);
|
||||
size_t profileCodeLen = strlen(profileCode);
|
||||
|
||||
if (!strncmp(key, profileCode, profileCodeLen) && key[profileCodeLen] == '_') {
|
||||
const char* subkey = key + profileCodeLen + 1;
|
||||
|
||||
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++) {
|
||||
const char* moduleCode = board::GetModuleName((SysClkModule)module, false);
|
||||
size_t moduleCodeLen = strlen(moduleCode);
|
||||
if (!strncmp(subkey, moduleCode, moduleCodeLen) && subkey[moduleCodeLen] == '\0') {
|
||||
parsedProfile = (SysClkProfile)profile;
|
||||
parsedModule = (SysClkModule)module;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (parsedModule == SysClkModule_EnumMax || parsedProfile == SysClkProfile_EnumMax) {
|
||||
fileUtils::LogLine("[cfg] Skipping key '%s' in section '%s': Unrecognized key", key, section);
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::uint32_t mhz = strtoul(value, NULL, 10);
|
||||
if (!mhz) {
|
||||
fileUtils::LogLine("[cfg] Skipping key '%s' in section '%s': Invalid value", key, section);
|
||||
return 1;
|
||||
}
|
||||
|
||||
gProfileMHzMap[std::make_tuple(tid, parsedProfile, parsedModule)] = mhz;
|
||||
auto it = gProfileCountMap.find(tid);
|
||||
if (it == gProfileCountMap.end()) {
|
||||
gProfileCountMap[tid] = 1;
|
||||
} else {
|
||||
it->second++;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Close() {
|
||||
gLoaded = false;
|
||||
gProfileMHzMap.clear();
|
||||
gProfileCountMap.clear();
|
||||
for (unsigned int i = 0; i < SysClkConfigValue_EnumMax; i++) {
|
||||
configValues[i] = sysclkDefaultConfigValue((SysClkConfigValue)i);
|
||||
}
|
||||
}
|
||||
|
||||
void Load() {
|
||||
fileUtils::LogLine("[cfg] Reading %s", gPath.c_str());
|
||||
|
||||
Close();
|
||||
gMtime = CheckModificationTime();
|
||||
if (!gMtime) {
|
||||
fileUtils::LogLine("[cfg] Error finding file");
|
||||
} else if (!ini_browse(&BrowseIniFunc, nullptr, gPath.c_str())) {
|
||||
fileUtils::LogLine("[cfg] Error loading file");
|
||||
}
|
||||
|
||||
gLoaded = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Initialize() {
|
||||
gPath = FILE_CONFIG_DIR "/config.ini";
|
||||
gLoaded = false;
|
||||
gProfileMHzMap.clear();
|
||||
gProfileCountMap.clear();
|
||||
gMtime = 0;
|
||||
gEnabled = false;
|
||||
for (unsigned int i = 0; i < SysClkModule_EnumMax; i++) {
|
||||
gOverrideFreqs[i] = 0;
|
||||
}
|
||||
for (unsigned int i = 0; i < SysClkConfigValue_EnumMax; i++) {
|
||||
configValues[i] = sysclkDefaultConfigValue((SysClkConfigValue)i);
|
||||
}
|
||||
}
|
||||
|
||||
void Exit() {
|
||||
std::scoped_lock lock{gConfigMutex};
|
||||
Close();
|
||||
}
|
||||
|
||||
bool Refresh() {
|
||||
std::scoped_lock lock{gConfigMutex};
|
||||
if (!gLoaded || gMtime != CheckModificationTime()) {
|
||||
Load();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool HasProfilesLoaded() {
|
||||
std::scoped_lock lock{gConfigMutex};
|
||||
return gLoaded;
|
||||
}
|
||||
|
||||
std::uint32_t GetAutoClockHz(std::uint64_t tid, SysClkModule module, SysClkProfile profile, bool returnRaw) {
|
||||
std::scoped_lock lock{gConfigMutex};
|
||||
switch (profile) {
|
||||
case SysClkProfile_Handheld:
|
||||
return FindClockHzFromProfiles(tid, module, {SysClkProfile_Handheld}, returnRaw ? 1 : 1000000);
|
||||
case SysClkProfile_HandheldCharging:
|
||||
case SysClkProfile_HandheldChargingUSB:
|
||||
return FindClockHzFromProfiles(tid, module, {SysClkProfile_HandheldChargingUSB, SysClkProfile_HandheldCharging, SysClkProfile_Handheld}, returnRaw ? 1 : 1000000);
|
||||
case SysClkProfile_HandheldChargingOfficial:
|
||||
return FindClockHzFromProfiles(tid, module, {SysClkProfile_HandheldChargingOfficial, SysClkProfile_HandheldCharging, SysClkProfile_Handheld}, returnRaw ? 1 : 1000000);
|
||||
case SysClkProfile_Docked:
|
||||
return FindClockHzFromProfiles(tid, module, {SysClkProfile_Docked}, returnRaw ? 1 : 1000000);
|
||||
default:
|
||||
ERROR_THROW("Unhandled SysClkProfile: %u", profile);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void GetProfiles(std::uint64_t tid, SysClkTitleProfileList* out_profiles) {
|
||||
std::scoped_lock lock{gConfigMutex};
|
||||
for (unsigned int profile = 0; profile < SysClkProfile_EnumMax; profile++) {
|
||||
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++) {
|
||||
out_profiles->mhzMap[profile][module] = FindClockMHz(tid, (SysClkModule)module, (SysClkProfile)profile);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool SetProfiles(std::uint64_t tid, SysClkTitleProfileList* profiles, bool immediate) {
|
||||
std::scoped_lock lock{gConfigMutex};
|
||||
uint8_t numProfiles = 0;
|
||||
|
||||
char section[17] = {0};
|
||||
snprintf(section, sizeof(section), "%016lX", tid);
|
||||
|
||||
std::vector<std::string> keys;
|
||||
std::vector<std::string> values;
|
||||
keys.reserve(SysClkProfile_EnumMax * SysClkModule_EnumMax);
|
||||
values.reserve(SysClkProfile_EnumMax * SysClkModule_EnumMax);
|
||||
|
||||
std::uint32_t* mhz = &profiles->mhz[0];
|
||||
|
||||
for (unsigned int profile = 0; profile < SysClkProfile_EnumMax; profile++) {
|
||||
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++) {
|
||||
if (*mhz) {
|
||||
numProfiles++;
|
||||
|
||||
std::string key = std::string(board::GetProfileName((SysClkProfile)profile, false)) +
|
||||
"_" +
|
||||
board::GetModuleName((SysClkModule)module, false);
|
||||
std::string value = std::to_string(*mhz);
|
||||
|
||||
keys.push_back(key);
|
||||
values.push_back(value);
|
||||
}
|
||||
mhz++;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<const char*> keyPointers;
|
||||
std::vector<const char*> valuePointers;
|
||||
keyPointers.reserve(keys.size() + 1);
|
||||
valuePointers.reserve(values.size() + 1);
|
||||
|
||||
for (size_t i = 0; i < keys.size(); i++) {
|
||||
keyPointers.push_back(keys[i].c_str());
|
||||
valuePointers.push_back(values[i].c_str());
|
||||
}
|
||||
keyPointers.push_back(NULL);
|
||||
valuePointers.push_back(NULL);
|
||||
|
||||
if (!ini_putsection(section, keyPointers.data(), valuePointers.data(), gPath.c_str())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (immediate) {
|
||||
mhz = &profiles->mhz[0];
|
||||
gProfileCountMap[tid] = numProfiles;
|
||||
for (unsigned int profile = 0; profile < SysClkProfile_EnumMax; profile++) {
|
||||
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++) {
|
||||
if (*mhz) {
|
||||
gProfileMHzMap[std::make_tuple(tid, (SysClkProfile)profile, (SysClkModule)module)] = *mhz;
|
||||
} else {
|
||||
gProfileMHzMap.erase(std::make_tuple(tid, (SysClkProfile)profile, (SysClkModule)module));
|
||||
}
|
||||
mhz++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::uint8_t GetProfileCount(std::uint64_t tid) {
|
||||
auto it = gProfileCountMap.find(tid);
|
||||
if (it == gProfileCountMap.end()) {
|
||||
return 0;
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
void SetEnabled(bool enabled) {
|
||||
gEnabled = enabled;
|
||||
}
|
||||
|
||||
bool Enabled() {
|
||||
return gEnabled;
|
||||
}
|
||||
|
||||
void SetOverrideHz(SysClkModule module, std::uint32_t hz) {
|
||||
ASSERT_ENUM_VALID(SysClkModule, module);
|
||||
std::scoped_lock lock{gOverrideMutex};
|
||||
gOverrideFreqs[module] = hz;
|
||||
}
|
||||
|
||||
std::uint32_t GetOverrideHz(SysClkModule module) {
|
||||
ASSERT_ENUM_VALID(SysClkModule, module);
|
||||
std::scoped_lock lock{gOverrideMutex};
|
||||
return gOverrideFreqs[module];
|
||||
}
|
||||
|
||||
std::uint64_t GetConfigValue(SysClkConfigValue kval) {
|
||||
ASSERT_ENUM_VALID(SysClkConfigValue, kval);
|
||||
std::scoped_lock lock{gConfigMutex};
|
||||
return configValues[kval];
|
||||
}
|
||||
|
||||
const char* GetConfigValueName(SysClkConfigValue kval, bool pretty) {
|
||||
ASSERT_ENUM_VALID(SysClkConfigValue, kval);
|
||||
return sysclkFormatConfigValue(kval, pretty);
|
||||
}
|
||||
|
||||
void GetConfigValues(SysClkConfigValueList* out_configValues) {
|
||||
std::scoped_lock lock{gConfigMutex};
|
||||
for (unsigned int kval = 0; kval < SysClkConfigValue_EnumMax; kval++) {
|
||||
out_configValues->values[kval] = configValues[kval];
|
||||
}
|
||||
}
|
||||
|
||||
bool SetConfigValues(SysClkConfigValueList* configValues, bool immediate) {
|
||||
std::scoped_lock lock{gConfigMutex};
|
||||
|
||||
std::vector<const char*> iniKeys;
|
||||
std::vector<std::string> iniValues;
|
||||
iniKeys.reserve(SysClkConfigValue_EnumMax + 1);
|
||||
iniValues.reserve(SysClkConfigValue_EnumMax);
|
||||
|
||||
for (unsigned int kval = 0; kval < SysClkConfigValue_EnumMax; kval++) {
|
||||
if (!sysclkValidConfigValue((SysClkConfigValue)kval, configValues->values[kval]) ||
|
||||
configValues->values[kval] == sysclkDefaultConfigValue((SysClkConfigValue)kval)) {
|
||||
continue;
|
||||
}
|
||||
iniValues.push_back(std::to_string(configValues->values[kval]));
|
||||
iniKeys.push_back(sysclkFormatConfigValue((SysClkConfigValue)kval, false));
|
||||
}
|
||||
|
||||
iniKeys.push_back(NULL);
|
||||
|
||||
std::vector<const char*> valuePointers;
|
||||
valuePointers.reserve(iniValues.size() + 1);
|
||||
for (const auto& val : iniValues) {
|
||||
valuePointers.push_back(val.c_str());
|
||||
}
|
||||
valuePointers.push_back(NULL);
|
||||
|
||||
if (!ini_putsection(CONFIG_VAL_SECTION, iniKeys.data(), valuePointers.data(), gPath.c_str())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (immediate) {
|
||||
for (unsigned int kval = 0; kval < SysClkConfigValue_EnumMax; kval++) {
|
||||
if (sysclkValidConfigValue((SysClkConfigValue)kval, configValues->values[kval])) {
|
||||
config::configValues[kval] = configValues->values[kval];
|
||||
} else {
|
||||
config::configValues[kval] = sysclkDefaultConfigValue((SysClkConfigValue)kval);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ResetConfigValue(SysClkConfigValue kval) {
|
||||
if (!SYSCLK_ENUM_VALID(SysClkConfigValue, kval)) {
|
||||
fileUtils::LogLine("[cfg] Invalid SysClkConfigValue: %u", kval);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::scoped_lock lock{gConfigMutex};
|
||||
|
||||
std::uint64_t defaultValue = sysclkDefaultConfigValue(kval);
|
||||
|
||||
std::vector<const char*> iniKeys;
|
||||
std::vector<std::string> iniValues;
|
||||
iniKeys.reserve(2);
|
||||
iniValues.reserve(1);
|
||||
|
||||
iniKeys.push_back(sysclkFormatConfigValue(kval, false));
|
||||
iniValues.push_back("");
|
||||
iniKeys.push_back(NULL);
|
||||
|
||||
std::vector<const char*> valuePointers;
|
||||
valuePointers.reserve(iniValues.size() + 1);
|
||||
for (const auto& val : iniValues) {
|
||||
valuePointers.push_back(val.c_str());
|
||||
}
|
||||
valuePointers.push_back(NULL);
|
||||
|
||||
if (!ini_putsection(CONFIG_VAL_SECTION, iniKeys.data(), valuePointers.data(), gPath.c_str())) {
|
||||
fileUtils::LogLine("[cfg] Failed to reset config value %u in INI", kval);
|
||||
return false;
|
||||
}
|
||||
|
||||
configValues[kval] = defaultValue;
|
||||
fileUtils::LogLine("[cfg] Reset config value %u to default: %llu", kval, defaultValue);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SetConfigValue(SysClkConfigValue kval, std::uint64_t value, bool immediate) {
|
||||
if (!SYSCLK_ENUM_VALID(SysClkConfigValue, kval)) {
|
||||
return false;
|
||||
}
|
||||
if (!sysclkValidConfigValue(kval, value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::scoped_lock lock{gConfigMutex};
|
||||
|
||||
std::vector<const char*> iniKeys;
|
||||
std::vector<std::string> iniValues;
|
||||
iniKeys.reserve(2);
|
||||
iniValues.reserve(1);
|
||||
|
||||
iniKeys.push_back(sysclkFormatConfigValue(kval, false));
|
||||
iniValues.push_back(std::to_string(value));
|
||||
iniKeys.push_back(NULL);
|
||||
|
||||
std::vector<const char*> valuePointers;
|
||||
valuePointers.reserve(2);
|
||||
valuePointers.push_back(iniValues[0].c_str());
|
||||
valuePointers.push_back(NULL);
|
||||
|
||||
if (!ini_putsection(CONFIG_VAL_SECTION, iniKeys.data(), valuePointers.data(), gPath.c_str())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (immediate) {
|
||||
configValues[kval] = value;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
61
Source/hoc-clk/sysmodule/src/config.hpp
Normal file
61
Source/hoc-clk/sysmodule/src/config.hpp
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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>
|
||||
|
||||
#define CONFIG_VAL_SECTION "values"
|
||||
|
||||
namespace config {
|
||||
|
||||
void Initialize();
|
||||
void Exit();
|
||||
|
||||
bool Refresh();
|
||||
bool HasProfilesLoaded();
|
||||
|
||||
std::uint8_t GetProfileCount(std::uint64_t tid);
|
||||
void GetProfiles(std::uint64_t tid, SysClkTitleProfileList* out_profiles);
|
||||
bool SetProfiles(std::uint64_t tid, SysClkTitleProfileList* profiles, bool immediate);
|
||||
std::uint32_t GetAutoClockHz(std::uint64_t tid, SysClkModule module, SysClkProfile profile, bool returnRaw);
|
||||
|
||||
void SetEnabled(bool enabled);
|
||||
bool Enabled();
|
||||
void SetOverrideHz(SysClkModule module, std::uint32_t hz);
|
||||
std::uint32_t GetOverrideHz(SysClkModule module);
|
||||
|
||||
std::uint64_t GetConfigValue(SysClkConfigValue val);
|
||||
const char* GetConfigValueName(SysClkConfigValue val, bool pretty);
|
||||
void GetConfigValues(SysClkConfigValueList* out_configValues);
|
||||
bool SetConfigValues(SysClkConfigValueList* configValues, bool immediate);
|
||||
bool ResetConfigValue(SysClkConfigValue kval);
|
||||
bool SetConfigValue(SysClkConfigValue kval, std::uint64_t value, bool immediate = true);
|
||||
|
||||
extern uint64_t configValues[SysClkConfigValue_EnumMax];
|
||||
|
||||
}
|
||||
57
Source/hoc-clk/sysmodule/src/errors.cpp
Normal file
57
Source/hoc-clk/sysmodule/src/errors.cpp
Normal 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 "errors.hpp"
|
||||
#include "file_utils.hpp"
|
||||
#include <cstdarg>
|
||||
#include <cstring>
|
||||
|
||||
namespace errors {
|
||||
|
||||
namespace {
|
||||
|
||||
const char* FormatMessage(const char* format, va_list args) {
|
||||
size_t len = vsnprintf(NULL, 0, format, args) * sizeof(char);
|
||||
char* buf = (char*)malloc(len + 1);
|
||||
if (buf == NULL) {
|
||||
return format;
|
||||
}
|
||||
vsnprintf(buf, len + 1, format, args);
|
||||
return buf;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ThrowException(const char* format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
const char* msg = FormatMessage(format, args);
|
||||
va_end(args);
|
||||
fileUtils::LogLine(format, args);
|
||||
throw std::runtime_error(msg);
|
||||
}
|
||||
|
||||
}
|
||||
48
Source/hoc-clk/sysmodule/src/errors.hpp
Normal file
48
Source/hoc-clk/sysmodule/src/errors.hpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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 <stdexcept>
|
||||
|
||||
#define ERROR_THROW(format, ...) errors::ThrowException(format "\n in %s:%u", ##__VA_ARGS__, __FILE__, __LINE__)
|
||||
#define ERROR_RESULT_THROW(rc, format, ...) ERROR_THROW(format "\n RC: [0x%x] %04d-%04d", ##__VA_ARGS__, rc, R_MODULE(rc), R_DESCRIPTION(rc))
|
||||
#define ASSERT_RESULT_OK(rc, format, ...) \
|
||||
if (R_FAILED(rc)) \
|
||||
{ \
|
||||
ERROR_RESULT_THROW(rc, "ASSERT_RESULT_OK: " format, ##__VA_ARGS__); \
|
||||
}
|
||||
#define ASSERT_ENUM_VALID(n, v) \
|
||||
if (!SYSCLK_ENUM_VALID(n, v)) { \
|
||||
ERROR_THROW("No such %s: %u", #n, v); \
|
||||
}
|
||||
|
||||
namespace errors {
|
||||
|
||||
void ThrowException(const char* format, ...);
|
||||
|
||||
}
|
||||
200
Source/hoc-clk/sysmodule/src/file_utils.cpp
Normal file
200
Source/hoc-clk/sysmodule/src/file_utils.cpp
Normal file
@@ -0,0 +1,200 @@
|
||||
/*
|
||||
* Copyright (c) Souldbminer 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 "file_utils.hpp"
|
||||
#include <nxExt.h>
|
||||
|
||||
extern "C" void __libnx_init_time(void);
|
||||
|
||||
namespace fileUtils {
|
||||
|
||||
namespace {
|
||||
|
||||
LockableMutex g_log_mutex;
|
||||
LockableMutex g_csv_mutex;
|
||||
std::atomic_bool g_has_initialized = false;
|
||||
bool g_log_enabled = false;
|
||||
std::uint64_t g_last_flag_check = 0;
|
||||
|
||||
void RefreshFlags(bool force) {
|
||||
std::uint64_t now = armTicksToNs(armGetSystemTick());
|
||||
if (!force && (now - g_last_flag_check) < FILE_FLAG_CHECK_INTERVAL_NS) {
|
||||
return;
|
||||
}
|
||||
|
||||
FILE* file = fopen(FILE_LOG_FLAG_PATH, "r");
|
||||
if (file) {
|
||||
g_log_enabled = true;
|
||||
fclose(file);
|
||||
} else {
|
||||
g_log_enabled = false;
|
||||
}
|
||||
|
||||
g_last_flag_check = now;
|
||||
}
|
||||
|
||||
void InitializeThreadFunc(void* args) {
|
||||
Initialize();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool IsInitialized() {
|
||||
return g_has_initialized;
|
||||
}
|
||||
|
||||
bool IsLogEnabled() {
|
||||
return g_log_enabled;
|
||||
}
|
||||
|
||||
void LogLine(const char* format, ...) {
|
||||
std::scoped_lock lock{g_log_mutex};
|
||||
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
if (g_has_initialized) {
|
||||
RefreshFlags(false);
|
||||
|
||||
if (g_log_enabled) {
|
||||
FILE* file = fopen(FILE_LOG_FILE_PATH, "a");
|
||||
|
||||
if (file) {
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_REALTIME, &now);
|
||||
struct tm* nowTm = localtime(&now.tv_sec);
|
||||
|
||||
fprintf(file, "[%04d-%02d-%02d %02d:%02d:%02d.%03ld] ", nowTm->tm_year+1900, nowTm->tm_mon+1, nowTm->tm_mday, nowTm->tm_hour, nowTm->tm_min, nowTm->tm_sec, now.tv_nsec / 1000000UL);
|
||||
vfprintf(file, format, args);
|
||||
fprintf(file, "\n");
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void WriteContextToCsv(const SysClkContext* context) {
|
||||
std::scoped_lock lock{g_csv_mutex};
|
||||
|
||||
FILE* file = fopen(FILE_CONTEXT_CSV_PATH, "a");
|
||||
|
||||
if (file) {
|
||||
// Print header
|
||||
if (!ftell(file)) {
|
||||
fprintf(file, "timestamp,profile,app_tid");
|
||||
|
||||
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++) {
|
||||
fprintf(file, ",%s_hz", sysclkFormatModule((SysClkModule)module, false));
|
||||
}
|
||||
|
||||
for (unsigned int sensor = 0; sensor < SysClkThermalSensor_EnumMax; sensor++) {
|
||||
fprintf(file, ",%s_milliC", sysclkFormatThermalSensor((SysClkThermalSensor)sensor, false));
|
||||
}
|
||||
|
||||
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++) {
|
||||
fprintf(file, ",%s_real_hz", sysclkFormatModule((SysClkModule)module, false));
|
||||
}
|
||||
|
||||
for (unsigned int sensor = 0; sensor < SysClkPowerSensor_EnumMax; sensor++) {
|
||||
fprintf(file, ",%s_mw", sysclkFormatPowerSensor((SysClkPowerSensor)sensor, false));
|
||||
}
|
||||
|
||||
fprintf(file, "\n");
|
||||
}
|
||||
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_REALTIME, &now);
|
||||
|
||||
fprintf(file, "%ld%03ld,%s,%016lx", now.tv_sec, now.tv_nsec / 1000000UL, sysclkFormatProfile(context->profile, false), context->applicationId);
|
||||
|
||||
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++) {
|
||||
fprintf(file, ",%d", context->freqs[module]);
|
||||
}
|
||||
|
||||
for (unsigned int sensor = 0; sensor < SysClkThermalSensor_EnumMax; sensor++) {
|
||||
fprintf(file, ",%d", context->temps[sensor]);
|
||||
}
|
||||
|
||||
for (unsigned int module = 0; module < SysClkModule_EnumMax; module++) {
|
||||
fprintf(file, ",%d", context->realFreqs[module]);
|
||||
}
|
||||
|
||||
for (unsigned int sensor = 0; sensor < SysClkPowerSensor_EnumMax; sensor++) {
|
||||
fprintf(file, ",%d", context->power[sensor]);
|
||||
}
|
||||
|
||||
fprintf(file, "\n");
|
||||
fclose(file);
|
||||
}
|
||||
}
|
||||
|
||||
void InitializeAsync() {
|
||||
Thread initThread = {0};
|
||||
threadCreate(&initThread, InitializeThreadFunc, NULL, NULL, 0x4000, 0x15, 0);
|
||||
threadStart(&initThread);
|
||||
}
|
||||
|
||||
Result Initialize() {
|
||||
Result rc = 0;
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
rc = timeInitialize();
|
||||
}
|
||||
|
||||
__libnx_init_time();
|
||||
timeExit();
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
rc = fsInitialize();
|
||||
}
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
rc = fsdevMountSdmc();
|
||||
}
|
||||
|
||||
if (R_SUCCEEDED(rc)) {
|
||||
RefreshFlags(true);
|
||||
g_has_initialized = true;
|
||||
LogLine("=== hoc-clk " TARGET_VERSION " ===");
|
||||
LogLine("by m4xw, natinusala, p-sam, Souldbminer, Lightos_ and Dominatorul");
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
void Exit() {
|
||||
if (!g_has_initialized) {
|
||||
return;
|
||||
}
|
||||
|
||||
g_has_initialized = false;
|
||||
g_log_enabled = false;
|
||||
|
||||
fsdevUnmountAll();
|
||||
fsExit();
|
||||
}
|
||||
|
||||
}
|
||||
53
Source/hoc-clk/sysmodule/src/file_utils.hpp
Normal file
53
Source/hoc-clk/sysmodule/src/file_utils.hpp
Normal 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
|
||||
* --------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <switch.h>
|
||||
#include <time.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <atomic>
|
||||
#include <cstdarg>
|
||||
#include <sysclk.h>
|
||||
|
||||
#define FILE_CONFIG_DIR "/config/" TARGET
|
||||
#define FILE_FLAG_CHECK_INTERVAL_NS (10000ULL * 1000000000ULL)
|
||||
#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"
|
||||
|
||||
namespace fileUtils {
|
||||
|
||||
void Exit();
|
||||
Result Initialize();
|
||||
bool IsInitialized();
|
||||
bool IsLogEnabled();
|
||||
void InitializeAsync();
|
||||
void LogLine(const char* format, ...);
|
||||
void WriteContextToCsv(const SysClkContext* context);
|
||||
|
||||
}
|
||||
349
Source/hoc-clk/sysmodule/src/governor.cpp
Normal file
349
Source/hoc-clk/sysmodule/src/governor.cpp
Normal file
@@ -0,0 +1,349 @@
|
||||
#include "governor.hpp"
|
||||
|
||||
namespace governor {
|
||||
|
||||
#define POLL_NS 5'000'000 // 5 ms – governor poll rate
|
||||
#define DOWN_HOLD_TICKS 10 // 50 ms – how long to in POLL_NS to hold while ramping down
|
||||
#define STEP_UTIL 900 // multiplier for step calculations
|
||||
|
||||
bool isGpuGovernorEnabled = false;
|
||||
bool isCpuGovernorEnabled = false;
|
||||
bool lastGpuGovernorState = false;
|
||||
bool lastCpuGovernorState = false;
|
||||
bool lastVrrGovernorState = false;
|
||||
bool hasChanged = true;
|
||||
bool isCpuGovernorInBoostMode = false;
|
||||
bool isVRREnabled = false;
|
||||
|
||||
// thread handles
|
||||
Thread cpuGovernorTHREAD;
|
||||
Thread gpuGovernorTHREAD;
|
||||
Thread vrrTHREAD;
|
||||
|
||||
void HandleGovernor(uint32_t targetHz)
|
||||
{
|
||||
u32 tempTargetHz = clockManager::gContext.overrideFreqs[HorizonOCModule_Governor];
|
||||
if (!tempTargetHz) {
|
||||
tempTargetHz = config::GetAutoClockHz(clockManager::gContext.applicationId, HorizonOCModule_Governor, clockManager::gContext.profile, true);
|
||||
if (!tempTargetHz)
|
||||
tempTargetHz = config::GetAutoClockHz(GLOBAL_PROFILE_ID, HorizonOCModule_Governor, clockManager::gContext.profile, true);
|
||||
}
|
||||
|
||||
auto resolve = [](u8 app, u8 temp) -> u8 {
|
||||
if (temp == ComponentGovernor_Disabled) return ComponentGovernor_Disabled;
|
||||
if (temp != ComponentGovernor_DoNotOverride) return temp;
|
||||
return app;
|
||||
};
|
||||
|
||||
u8 effectiveCpu = resolve(GovernorStateCpu(targetHz), GovernorStateCpu(tempTargetHz));
|
||||
u8 effectiveGpu = resolve(GovernorStateGpu(targetHz), GovernorStateGpu(tempTargetHz));
|
||||
u8 effectiveVrr = resolve(GovernorStateVrr(targetHz), GovernorStateVrr(tempTargetHz));
|
||||
|
||||
bool newCpuGovernorState = (effectiveCpu == ComponentGovernor_Enabled);
|
||||
bool newGpuGovernorState = (effectiveGpu == ComponentGovernor_Enabled);
|
||||
bool newVrrGovernorState = (effectiveVrr == ComponentGovernor_Enabled);
|
||||
|
||||
isCpuGovernorEnabled = newCpuGovernorState;
|
||||
isGpuGovernorEnabled = newGpuGovernorState;
|
||||
isVRREnabled = newVrrGovernorState;
|
||||
|
||||
if (newCpuGovernorState == false && lastCpuGovernorState == true) {
|
||||
svcSleepThread(100'000'000); // thread syncing. probably a cleaner way to do this but hey, it works!
|
||||
board::ResetToStockCpu();
|
||||
}
|
||||
if (newGpuGovernorState == false && lastGpuGovernorState == true) {
|
||||
svcSleepThread(100'000'000);
|
||||
board::ResetToStockGpu();
|
||||
}
|
||||
if (newVrrGovernorState == false && lastVrrGovernorState == true) {
|
||||
svcSleepThread(100'000'000);
|
||||
board::ResetToStockDisplay();
|
||||
}
|
||||
if (newCpuGovernorState != lastCpuGovernorState || newGpuGovernorState != lastGpuGovernorState || newVrrGovernorState != lastVrrGovernorState) {
|
||||
fileUtils::LogLine("[mgr] Governor state changed: CPU %s, GPU %s, VRR %s", newCpuGovernorState ? "enabled" : "disabled", newGpuGovernorState ? "enabled" : "disabled", newVrrGovernorState ? "enabled" : "disabled");
|
||||
lastCpuGovernorState = newCpuGovernorState;
|
||||
lastGpuGovernorState = newGpuGovernorState;
|
||||
lastVrrGovernorState = newVrrGovernorState;
|
||||
}
|
||||
}
|
||||
|
||||
u32 SchedutilTargetHz(u32 util, u32 tableMaxHz)
|
||||
{
|
||||
u64 hz = (u64)tableMaxHz * util / STEP_UTIL;
|
||||
return (u32)(std::min(hz, static_cast<u64>(tableMaxHz)));
|
||||
}
|
||||
|
||||
u32 TableIndexForHz(const clockManager::FreqTable& table, u32 targetHz)
|
||||
{
|
||||
for (u32 i = 0; i < table.count; i++)
|
||||
if (table.list[i] >= targetHz)
|
||||
return i;
|
||||
return table.count - 1;
|
||||
}
|
||||
|
||||
u32 ResolveTargetHz(SysClkModule module)
|
||||
{
|
||||
u32 hz = clockManager::gContext.overrideFreqs[module];
|
||||
if (!hz)
|
||||
hz = config::GetAutoClockHz(
|
||||
clockManager::gContext.applicationId, module,
|
||||
clockManager::gContext.profile, false);
|
||||
if (!hz)
|
||||
hz = config::GetAutoClockHz(
|
||||
GLOBAL_PROFILE_ID, module,
|
||||
clockManager::gContext.profile, false);
|
||||
return hz;
|
||||
}
|
||||
|
||||
void CpuGovernorThread(void* arg)
|
||||
{
|
||||
(void)arg;
|
||||
|
||||
u32 downHoldRemaining = 0;
|
||||
u32 lastHz = 0;
|
||||
u32 minHz = 612;
|
||||
u32 tick = 0;
|
||||
for (;;) {
|
||||
if (!clockManager::gRunning || !isCpuGovernorEnabled) {
|
||||
downHoldRemaining = 0;
|
||||
lastHz = 0;
|
||||
svcSleepThread(POLL_NS);
|
||||
continue;
|
||||
}
|
||||
|
||||
u32 mode = 0;
|
||||
Result rc = apmExtGetCurrentPerformanceConfiguration(&mode);
|
||||
|
||||
if (R_SUCCEEDED(rc) && apmExtIsBoostMode(mode)) {
|
||||
isCpuGovernorInBoostMode = true;
|
||||
downHoldRemaining = 0;
|
||||
lastHz = 0;
|
||||
continue; // TODO: figure out a way to get boost clock easily and set it instead of just skipping the governor
|
||||
} else if (!apmExtIsBoostMode(mode)) {
|
||||
isCpuGovernorInBoostMode = false;
|
||||
}
|
||||
|
||||
auto& table = clockManager::gFreqTable[SysClkModule_CPU];
|
||||
|
||||
if (table.count == 0)
|
||||
continue;
|
||||
|
||||
std::scoped_lock lock{clockManager::gContextMutex};
|
||||
|
||||
u32 cpuLoad = board::GetPartLoad(HocClkPartLoad_CPUMax);
|
||||
|
||||
u32 tableMaxHz = table.list[table.count - 1];
|
||||
u32 desiredHz = SchedutilTargetHz(cpuLoad, tableMaxHz);
|
||||
u32 targetHz = ResolveTargetHz(SysClkModule_CPU);
|
||||
u32 maxHz = clockManager::GetMaxAllowedHz(SysClkModule_CPU, clockManager::gContext.profile);
|
||||
|
||||
if (targetHz && desiredHz > targetHz)
|
||||
desiredHz = targetHz;
|
||||
|
||||
if (maxHz && desiredHz > maxHz)
|
||||
desiredHz = maxHz;
|
||||
|
||||
u32 newHz = table.list[TableIndexForHz(table, desiredHz)];
|
||||
|
||||
// ramp up fast, go down slow
|
||||
bool goingDown = (lastHz != 0) && (newHz < lastHz);
|
||||
|
||||
if (!goingDown)
|
||||
downHoldRemaining = 0;
|
||||
else if (downHoldRemaining == 0)
|
||||
downHoldRemaining = DOWN_HOLD_TICKS;
|
||||
|
||||
if (downHoldRemaining > 0)
|
||||
downHoldRemaining--;
|
||||
|
||||
if (++tick > 50) {
|
||||
minHz = config::GetConfigValue(HorizonOCConfigValue_CpuGovernorMinimumFreq);
|
||||
tick = 0;
|
||||
}
|
||||
|
||||
if (newHz < minHz)
|
||||
newHz = minHz;
|
||||
|
||||
if ((!goingDown || (downHoldRemaining == 0)) && clockManager::IsAssignableHz(SysClkModule_CPU, newHz)) {
|
||||
board::SetHz(SysClkModule_CPU, newHz);
|
||||
clockManager::gContext.freqs[SysClkModule_CPU] = newHz;
|
||||
lastHz = newHz;
|
||||
}
|
||||
|
||||
svcSleepThread(POLL_NS);
|
||||
}
|
||||
}
|
||||
|
||||
void GovernorThread(void* arg)
|
||||
{
|
||||
(void)arg;
|
||||
|
||||
u32 downHoldRemaining = 0;
|
||||
u32 lastHz = 0;
|
||||
|
||||
for (;;) {
|
||||
if (!clockManager::gRunning || !isGpuGovernorEnabled) {
|
||||
downHoldRemaining = 0;
|
||||
lastHz = 0;
|
||||
svcSleepThread(POLL_NS);
|
||||
continue;
|
||||
}
|
||||
|
||||
auto& table = clockManager::gFreqTable[SysClkModule_GPU];
|
||||
if (table.count == 0)
|
||||
continue;
|
||||
|
||||
std::scoped_lock lock{clockManager::gContextMutex};
|
||||
|
||||
u32 gpuLoad = board::GetPartLoad(HocClkPartLoad_GPU);
|
||||
u32 tableMaxHz = table.list[table.count - 1];
|
||||
u32 desiredHz = SchedutilTargetHz(gpuLoad, tableMaxHz);
|
||||
u32 targetHz = ResolveTargetHz(SysClkModule_GPU);
|
||||
u32 maxHz = clockManager::GetMaxAllowedHz(SysClkModule_GPU, clockManager::gContext.profile);
|
||||
|
||||
if (targetHz && desiredHz > targetHz)
|
||||
desiredHz = targetHz;
|
||||
|
||||
if (maxHz && desiredHz > maxHz)
|
||||
desiredHz = maxHz;
|
||||
|
||||
u32 newHz = table.list[TableIndexForHz(table, desiredHz)];
|
||||
bool goingDown = (lastHz != 0) && (newHz < lastHz);
|
||||
|
||||
if (!goingDown)
|
||||
downHoldRemaining = 0;
|
||||
else if (downHoldRemaining == 0)
|
||||
downHoldRemaining = DOWN_HOLD_TICKS;
|
||||
|
||||
if (downHoldRemaining > 0)
|
||||
downHoldRemaining--;
|
||||
|
||||
if ((!goingDown || (downHoldRemaining == 0)) && clockManager::IsAssignableHz(SysClkModule_GPU, newHz)) {
|
||||
board::SetHz(SysClkModule_GPU, newHz);
|
||||
clockManager::gContext.freqs[SysClkModule_GPU] = newHz;
|
||||
lastHz = newHz;
|
||||
}
|
||||
|
||||
svcSleepThread(POLL_NS);
|
||||
}
|
||||
}
|
||||
|
||||
void VRRThread(void* arg)
|
||||
{
|
||||
(void)arg;
|
||||
|
||||
u8 tick = 0;
|
||||
for (;;) {
|
||||
if (!clockManager::gRunning || clockManager::gContext.profile == SysClkProfile_Docked || !isVRREnabled) {
|
||||
svcSleepThread(POLL_NS);
|
||||
continue;
|
||||
}
|
||||
|
||||
std::scoped_lock lock{clockManager::gContextMutex};
|
||||
|
||||
u8 fps;
|
||||
|
||||
if (clockManager::gContext.isSaltyNXInstalled) {
|
||||
fps = integrations::GetSaltyNXFPS();
|
||||
} else {
|
||||
svcSleepThread(~0ULL); // effectively disable the thread if SaltyNX isn't installed, as there's no point in it running
|
||||
continue;
|
||||
}
|
||||
|
||||
if (fps == 254) {
|
||||
svcSleepThread(POLL_NS);
|
||||
continue;
|
||||
}
|
||||
// if(appletGetFocusState() != AppletFocusState_InFocus) {
|
||||
// board::ResetToStockDisplay();
|
||||
// continue;
|
||||
// }
|
||||
|
||||
u32 targetHz = clockManager::gContext.overrideFreqs[HorizonOCModule_Display];
|
||||
if (!targetHz) {
|
||||
targetHz = config::GetAutoClockHz(clockManager::gContext.applicationId, HorizonOCModule_Display, clockManager::gContext.profile, false);
|
||||
if (!targetHz)
|
||||
targetHz = config::GetAutoClockHz(GLOBAL_PROFILE_ID, HorizonOCModule_Display, clockManager::gContext.profile, false);
|
||||
}
|
||||
|
||||
u8 maxDisplay;
|
||||
if (targetHz) {
|
||||
maxDisplay = targetHz;
|
||||
} else {
|
||||
maxDisplay = 60; // don't assume display stuff!
|
||||
}
|
||||
|
||||
u8 minDisplay = board::GetConsoleType() == HorizonOCConsoleType_Aula ? 45 : 40;
|
||||
if (maxDisplay == minDisplay)
|
||||
continue;
|
||||
|
||||
if (fps >= minDisplay && fps <= maxDisplay) {
|
||||
board::SetHz(HorizonOCModule_Display, fps);
|
||||
clockManager::gContext.freqs[HorizonOCModule_Display] = fps;
|
||||
clockManager::gContext.realFreqs[HorizonOCModule_Display] = fps;
|
||||
} else {
|
||||
for (u32 i = 0; i < 10; i++) {
|
||||
u32 compareHz = fps * i;
|
||||
if (compareHz >= minDisplay && compareHz <= maxDisplay) {
|
||||
board::SetHz(HorizonOCModule_Display, compareHz);
|
||||
clockManager::gContext.freqs[HorizonOCModule_Display] = compareHz;
|
||||
clockManager::gContext.realFreqs[HorizonOCModule_Display] = compareHz;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (++tick > 50) {
|
||||
board::SetHz(HorizonOCModule_Display, maxDisplay);
|
||||
tick = 0;
|
||||
svcSleepThread(50'000'000);
|
||||
}
|
||||
|
||||
svcSleepThread(POLL_NS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void startThreads() {
|
||||
|
||||
threadCreate(
|
||||
&cpuGovernorTHREAD,
|
||||
CpuGovernorThread,
|
||||
nullptr,
|
||||
NULL,
|
||||
0x2000,
|
||||
0x3F,
|
||||
-2
|
||||
);
|
||||
|
||||
threadCreate(
|
||||
&gpuGovernorTHREAD,
|
||||
GovernorThread,
|
||||
nullptr,
|
||||
NULL,
|
||||
0x2000,
|
||||
0x3F,
|
||||
-2
|
||||
);
|
||||
|
||||
threadCreate(
|
||||
&vrrTHREAD,
|
||||
VRRThread,
|
||||
nullptr,
|
||||
NULL,
|
||||
0x2000,
|
||||
0x3F,
|
||||
-2
|
||||
);
|
||||
|
||||
threadStart(&cpuGovernorTHREAD);
|
||||
threadStart(&gpuGovernorTHREAD);
|
||||
threadStart(&vrrTHREAD);
|
||||
}
|
||||
|
||||
void exitThreads() {
|
||||
threadClose(&cpuGovernorTHREAD);
|
||||
threadClose(&gpuGovernorTHREAD);
|
||||
threadClose(&vrrTHREAD);
|
||||
}
|
||||
}
|
||||
27
Source/hoc-clk/sysmodule/src/governor.hpp
Normal file
27
Source/hoc-clk/sysmodule/src/governor.hpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <switch.h>
|
||||
#include <sysclk.h>
|
||||
#include "board/board.hpp"
|
||||
#include "clock_manager.hpp"
|
||||
#include <cstring>
|
||||
#include "file_utils.hpp"
|
||||
#include "board/board.hpp"
|
||||
#include "errors.hpp"
|
||||
#include "config.hpp"
|
||||
#include "integrations.hpp"
|
||||
#include <nxExt/cpp/lockable_mutex.h>
|
||||
|
||||
namespace governor {
|
||||
extern bool isCpuGovernorInBoostMode;
|
||||
extern bool isVRREnabled;
|
||||
extern bool isGpuGovernorEnabled;
|
||||
extern bool isCpuGovernorEnabled;
|
||||
extern bool lastGpuGovernorState;
|
||||
extern bool lastCpuGovernorState;
|
||||
extern bool lastVrrGovernorState;
|
||||
void startThreads();
|
||||
void exitThreads();
|
||||
void HandleGovernor(uint32_t targetHz);
|
||||
void CpuGovernorThread(void* arg);
|
||||
void GovernorThread(void* arg);
|
||||
void VRRThread(void* arg);
|
||||
}
|
||||
156
Source/hoc-clk/sysmodule/src/integrations.cpp
Normal file
156
Source/hoc-clk/sysmodule/src/integrations.cpp
Normal file
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* 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 "integrations.hpp"
|
||||
#include <sys/stat.h>
|
||||
#include <SaltyNX.h>
|
||||
#include "process_management.hpp"
|
||||
|
||||
namespace integrations {
|
||||
|
||||
namespace {
|
||||
|
||||
NxFpsSharedBlock* gNxFps = nullptr;
|
||||
SharedMemory gSharedMemory = {};
|
||||
bool gSharedMemoryUsed = false;
|
||||
Handle gRemoteSharedMemory = 1;
|
||||
u64 gPrevTid = 0;
|
||||
|
||||
bool CheckSaltyNXPort() {
|
||||
Handle saltysd;
|
||||
|
||||
for (int i = 0; i < 67; i++) {
|
||||
if (R_SUCCEEDED(svcConnectToNamedPort(&saltysd, "InjectServ"))) {
|
||||
svcCloseHandle(saltysd);
|
||||
break;
|
||||
}
|
||||
if (i == 66) return false;
|
||||
svcSleepThread(1'000'000);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 67; i++) {
|
||||
if (R_SUCCEEDED(svcConnectToNamedPort(&saltysd, "InjectServ"))) {
|
||||
svcCloseHandle(saltysd);
|
||||
return true;
|
||||
}
|
||||
svcSleepThread(1'000'000);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void SearchSharedMemoryBlock(uintptr_t base) {
|
||||
ptrdiff_t search_offset = 0;
|
||||
while (search_offset < 0x1000) {
|
||||
gNxFps = (NxFpsSharedBlock*)(base + search_offset);
|
||||
if (gNxFps->MAGIC == 0x465053)
|
||||
return;
|
||||
search_offset += 4;
|
||||
}
|
||||
gNxFps = nullptr;
|
||||
}
|
||||
|
||||
void LoadSharedMemory() {
|
||||
if (SaltySD_Connect())
|
||||
return;
|
||||
SaltySD_GetSharedMemoryHandle(&gRemoteSharedMemory);
|
||||
SaltySD_Term();
|
||||
shmemLoadRemote(&gSharedMemory, gRemoteSharedMemory, 0x1000, Perm_Rw);
|
||||
if (!shmemMap(&gSharedMemory))
|
||||
gSharedMemoryUsed = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool GetSysDockState() {
|
||||
struct stat st = {0};
|
||||
return stat("sdmc:/atmosphere/contents/42000000000000A0/flags/boot2.flag", &st) == 0;
|
||||
}
|
||||
|
||||
bool GetSaltyNXState() {
|
||||
struct stat st = {0};
|
||||
return stat("sdmc:/atmosphere/contents/0000000000534C56/flags/boot2.flag", &st) == 0;
|
||||
}
|
||||
|
||||
bool GetRETROSuperStatus() {
|
||||
struct stat st = {0};
|
||||
return stat("sdmc:/config/horizon-oc/retro.flag", &st) == 0;
|
||||
}
|
||||
|
||||
void LoadSaltyNX() {
|
||||
if (!CheckSaltyNXPort())
|
||||
return;
|
||||
LoadSharedMemory();
|
||||
}
|
||||
|
||||
u8 GetSaltyNXFPS() {
|
||||
if (!gSharedMemoryUsed)
|
||||
return 254;
|
||||
|
||||
u64 tid = processManagement::GetCurrentApplicationId();
|
||||
if (tid == 0)
|
||||
return 254;
|
||||
|
||||
if (gPrevTid != tid) {
|
||||
gNxFps = nullptr;
|
||||
gPrevTid = tid;
|
||||
}
|
||||
|
||||
if (!gNxFps) {
|
||||
uintptr_t base = (uintptr_t)shmemGetAddr(&gSharedMemory);
|
||||
SearchSharedMemoryBlock(base);
|
||||
}
|
||||
|
||||
return gNxFps ? gNxFps->FPS : 254;
|
||||
}
|
||||
|
||||
u16 GetSaltyNXResolutionHeight() {
|
||||
if (!gSharedMemoryUsed)
|
||||
return 0;
|
||||
|
||||
u64 tid = processManagement::GetCurrentApplicationId();
|
||||
if (tid == 0)
|
||||
return 0;
|
||||
|
||||
if (gPrevTid != tid) {
|
||||
gNxFps = nullptr;
|
||||
gPrevTid = tid;
|
||||
}
|
||||
|
||||
if (!gNxFps) {
|
||||
uintptr_t base = (uintptr_t)shmemGetAddr(&gSharedMemory);
|
||||
SearchSharedMemoryBlock(base);
|
||||
}
|
||||
|
||||
if (gNxFps) {
|
||||
gNxFps->renderCalls[0].calls = 0xFFFF;
|
||||
svcSleepThread(10*1000);
|
||||
return gNxFps->renderCalls[0].height == 0 ? gNxFps->viewportCalls[0].height : gNxFps->renderCalls[0].height;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
78
Source/hoc-clk/sysmodule/src/integrations.hpp
Normal file
78
Source/hoc-clk/sysmodule/src/integrations.hpp
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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 integrations {
|
||||
|
||||
struct NxFpsSharedBlock {
|
||||
uint32_t MAGIC;
|
||||
uint8_t FPS;
|
||||
float FPSavg;
|
||||
bool pluginActive;
|
||||
uint8_t FPSlocked;
|
||||
uint8_t FPSmode;
|
||||
uint8_t ZeroSync;
|
||||
uint8_t patchApplied;
|
||||
uint8_t API;
|
||||
uint32_t FPSticks[10];
|
||||
uint8_t Buffers;
|
||||
uint8_t SetBuffers;
|
||||
uint8_t ActiveBuffers;
|
||||
uint8_t SetActiveBuffers;
|
||||
union {
|
||||
struct {
|
||||
bool handheld: 1;
|
||||
bool docked: 1;
|
||||
unsigned int reserved: 6;
|
||||
} NX_PACKED ds;
|
||||
uint8_t general;
|
||||
} displaySync;
|
||||
struct resolutionCalls {
|
||||
uint16_t width;
|
||||
uint16_t height;
|
||||
uint16_t calls;
|
||||
} renderCalls[8], viewportCalls[8];
|
||||
bool forceOriginalRefreshRate;
|
||||
bool dontForce60InDocked;
|
||||
bool forceSuspend;
|
||||
uint8_t currentRefreshRate;
|
||||
float readSpeedPerSecond;
|
||||
uint8_t FPSlockedDocked;
|
||||
uint64_t frameNumber;
|
||||
} NX_PACKED;
|
||||
|
||||
bool GetSysDockState();
|
||||
bool GetSaltyNXState();
|
||||
bool GetRETROSuperStatus();
|
||||
void LoadSaltyNX();
|
||||
u8 GetSaltyNXFPS();
|
||||
u16 GetSaltyNXResolutionHeight();
|
||||
|
||||
}
|
||||
294
Source/hoc-clk/sysmodule/src/ipc_service.cpp
Normal file
294
Source/hoc-clk/sysmodule/src/ipc_service.cpp
Normal file
@@ -0,0 +1,294 @@
|
||||
/*
|
||||
* 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 "ipc_service.hpp"
|
||||
#include <cstring>
|
||||
#include <switch.h>
|
||||
#include <nxExt.h>
|
||||
#include "file_utils.hpp"
|
||||
#include "errors.hpp"
|
||||
#include "clock_manager.hpp"
|
||||
#include "config.hpp"
|
||||
#include "kip.hpp"
|
||||
namespace ipcService {
|
||||
|
||||
namespace {
|
||||
|
||||
bool gRunning = false;
|
||||
Thread gThread;
|
||||
LockableMutex gThreadMutex;
|
||||
IpcServer gServer;
|
||||
|
||||
Result GetApiVersion(u32* out_version) {
|
||||
*out_version = SYSCLK_IPC_API_VERSION;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result GetVersionString(char* out_buf, size_t bufSize) {
|
||||
if (bufSize) {
|
||||
strncpy(out_buf, TARGET_VERSION, bufSize-1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result GetCurrentContext(SysClkContext* out_ctx) {
|
||||
*out_ctx = clockManager::GetCurrentContext();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result ExitHandler() {
|
||||
clockManager::SetRunning(false);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result GetProfileCount(std::uint64_t* tid, std::uint8_t* out_count) {
|
||||
if (!config::HasProfilesLoaded()) {
|
||||
return SYSCLK_ERROR(ConfigNotLoaded);
|
||||
}
|
||||
*out_count = config::GetProfileCount(*tid);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result GetProfiles(std::uint64_t* tid, SysClkTitleProfileList* out_profiles) {
|
||||
if (!config::HasProfilesLoaded()) {
|
||||
return SYSCLK_ERROR(ConfigNotLoaded);
|
||||
}
|
||||
config::GetProfiles(*tid, out_profiles);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result SetProfiles(SysClkIpc_SetProfiles_Args* args) {
|
||||
if (!config::HasProfilesLoaded()) {
|
||||
return SYSCLK_ERROR(ConfigNotLoaded);
|
||||
}
|
||||
SysClkTitleProfileList profiles = args->profiles;
|
||||
if (!config::SetProfiles(args->tid, &profiles, true)) {
|
||||
return SYSCLK_ERROR(ConfigSaveFailed);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result SetEnabled(std::uint8_t* enabled) {
|
||||
config::SetEnabled(*enabled);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result SetOverride(SysClkIpc_SetOverride_Args* args) {
|
||||
if (!SYSCLK_ENUM_VALID(SysClkModule, args->module)) {
|
||||
return SYSCLK_ERROR(Generic);
|
||||
}
|
||||
config::SetOverrideHz(args->module, args->hz);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result GetConfigValuesHandler(SysClkConfigValueList* out_configValues) {
|
||||
if (!config::HasProfilesLoaded()) {
|
||||
return SYSCLK_ERROR(ConfigNotLoaded);
|
||||
}
|
||||
config::GetConfigValues(out_configValues);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result SetConfigValuesHandler(SysClkConfigValueList* configValues) {
|
||||
if (!config::HasProfilesLoaded()) {
|
||||
return SYSCLK_ERROR(ConfigNotLoaded);
|
||||
}
|
||||
SysClkConfigValueList copy = *configValues;
|
||||
if (!config::SetConfigValues(©, true)) {
|
||||
return SYSCLK_ERROR(ConfigSaveFailed);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result GetFreqList(SysClkIpc_GetFreqList_Args* args, std::uint32_t* out_list, std::size_t size, std::uint32_t* out_count) {
|
||||
if (!SYSCLK_ENUM_VALID(SysClkModule, args->module)) {
|
||||
return SYSCLK_ERROR(Generic);
|
||||
}
|
||||
if (args->maxCount != size/sizeof(*out_list)) {
|
||||
return SYSCLK_ERROR(Generic);
|
||||
}
|
||||
clockManager::GetFreqList(args->module, out_list, args->maxCount, out_count);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result ServiceHandlerFunc(void* arg, const IpcServerRequest* r, u8* out_data, size_t* out_dataSize) {
|
||||
(void)arg;
|
||||
switch (r->data.cmdId) {
|
||||
case SysClkIpcCmd_GetApiVersion:
|
||||
*out_dataSize = sizeof(u32);
|
||||
return GetApiVersion((u32*)out_data);
|
||||
|
||||
case SysClkIpcCmd_GetVersionString:
|
||||
if (r->hipc.meta.num_recv_buffers >= 1) {
|
||||
return GetVersionString(
|
||||
(char*)hipcGetBufferAddress(r->hipc.data.recv_buffers),
|
||||
hipcGetBufferSize(r->hipc.data.recv_buffers)
|
||||
);
|
||||
}
|
||||
break;
|
||||
|
||||
case SysClkIpcCmd_GetCurrentContext:
|
||||
if (r->data.size >= sizeof(std::uint64_t) && r->hipc.meta.num_recv_buffers >= 1) {
|
||||
size_t bufSize = hipcGetBufferSize(r->hipc.data.recv_buffers);
|
||||
if (bufSize >= sizeof(SysClkContext)) {
|
||||
return GetCurrentContext((SysClkContext*)hipcGetBufferAddress(r->hipc.data.recv_buffers));
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case SysClkIpcCmd_Exit:
|
||||
return ExitHandler();
|
||||
|
||||
case SysClkIpcCmd_GetProfileCount:
|
||||
if (r->data.size >= sizeof(std::uint64_t)) {
|
||||
*out_dataSize = sizeof(std::uint8_t);
|
||||
return GetProfileCount((std::uint64_t*)r->data.ptr, (std::uint8_t*)out_data);
|
||||
}
|
||||
break;
|
||||
|
||||
case SysClkIpcCmd_GetProfiles:
|
||||
if (r->data.size >= sizeof(std::uint64_t) && r->hipc.meta.num_recv_buffers >= 1) {
|
||||
size_t bufSize = hipcGetBufferSize(r->hipc.data.recv_buffers);
|
||||
if (bufSize >= sizeof(SysClkTitleProfileList)) {
|
||||
return GetProfiles((std::uint64_t*)r->data.ptr, (SysClkTitleProfileList*)hipcGetBufferAddress(r->hipc.data.recv_buffers));
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case SysClkIpcCmd_SetProfiles:
|
||||
if (r->data.size >= sizeof(SysClkIpc_SetProfiles_Args)) {
|
||||
return SetProfiles((SysClkIpc_SetProfiles_Args*)r->data.ptr);
|
||||
}
|
||||
break;
|
||||
|
||||
case SysClkIpcCmd_SetEnabled:
|
||||
if (r->data.size >= sizeof(std::uint8_t)) {
|
||||
return SetEnabled((std::uint8_t*)r->data.ptr);
|
||||
}
|
||||
break;
|
||||
|
||||
case SysClkIpcCmd_SetOverride:
|
||||
if (r->data.size >= sizeof(SysClkIpc_SetOverride_Args)) {
|
||||
return SetOverride((SysClkIpc_SetOverride_Args*)r->data.ptr);
|
||||
}
|
||||
break;
|
||||
|
||||
case SysClkIpcCmd_GetConfigValues:
|
||||
if (r->hipc.meta.num_recv_buffers >= 1) {
|
||||
size_t bufSize = hipcGetBufferSize(r->hipc.data.recv_buffers);
|
||||
if (bufSize >= sizeof(SysClkConfigValueList)) {
|
||||
return GetConfigValuesHandler((SysClkConfigValueList*)hipcGetBufferAddress(r->hipc.data.recv_buffers));
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case SysClkIpcCmd_SetConfigValues:
|
||||
if (r->hipc.meta.num_send_buffers >= 1) {
|
||||
size_t bufSize = hipcGetBufferSize(r->hipc.data.send_buffers);
|
||||
if (bufSize >= sizeof(SysClkConfigValueList)) {
|
||||
return SetConfigValuesHandler((SysClkConfigValueList*)hipcGetBufferAddress(r->hipc.data.send_buffers));
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case SysClkIpcCmd_GetFreqList:
|
||||
if (r->data.size >= sizeof(SysClkIpc_GetFreqList_Args) && r->hipc.meta.num_recv_buffers >= 1) {
|
||||
*out_dataSize = sizeof(std::uint32_t);
|
||||
return GetFreqList(
|
||||
(SysClkIpc_GetFreqList_Args*)r->data.ptr,
|
||||
(std::uint32_t*)hipcGetBufferAddress(r->hipc.data.recv_buffers),
|
||||
hipcGetBufferSize(r->hipc.data.recv_buffers),
|
||||
(std::uint32_t*)out_data
|
||||
);
|
||||
}
|
||||
break;
|
||||
|
||||
case HocClkIpcCmd_SetKipData:
|
||||
if (r->data.size >= 0) {
|
||||
kip::SetKipData();
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return SYSCLK_ERROR(Generic);
|
||||
}
|
||||
|
||||
void ProcessThreadFunc(void* arg) {
|
||||
(void)arg;
|
||||
Result rc;
|
||||
while (true) {
|
||||
rc = ipcServerProcess(&gServer, &ServiceHandlerFunc, nullptr);
|
||||
if (R_FAILED(rc)) {
|
||||
if (rc == KERNELRESULT(Cancelled)) {
|
||||
return;
|
||||
}
|
||||
if (rc != KERNELRESULT(ConnectionClosed)) {
|
||||
fileUtils::LogLine("[ipc] ipcServerProcess: [0x%x] %04d-%04d", rc, R_MODULE(rc), R_DESCRIPTION(rc));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Initialize() {
|
||||
std::int32_t priority;
|
||||
Result rc = svcGetThreadPriority(&priority, CUR_THREAD_HANDLE);
|
||||
ASSERT_RESULT_OK(rc, "svcGetThreadPriority");
|
||||
rc = ipcServerInit(&gServer, SYSCLK_IPC_SERVICE_NAME, 42);
|
||||
ASSERT_RESULT_OK(rc, "ipcServerInit");
|
||||
rc = threadCreate(&gThread, &ProcessThreadFunc, nullptr, NULL, 0x2000, priority, -2);
|
||||
ASSERT_RESULT_OK(rc, "threadCreate");
|
||||
gRunning = false;
|
||||
}
|
||||
|
||||
void Exit() {
|
||||
SetRunning(false);
|
||||
Result rc = threadClose(&gThread);
|
||||
ASSERT_RESULT_OK(rc, "threadClose");
|
||||
rc = ipcServerExit(&gServer);
|
||||
ASSERT_RESULT_OK(rc, "ipcServerExit");
|
||||
}
|
||||
|
||||
void SetRunning(bool running) {
|
||||
std::scoped_lock lock{gThreadMutex};
|
||||
if (gRunning == running) {
|
||||
return;
|
||||
}
|
||||
|
||||
gRunning = running;
|
||||
|
||||
if (running) {
|
||||
Result rc = threadStart(&gThread);
|
||||
ASSERT_RESULT_OK(rc, "threadStart");
|
||||
} else {
|
||||
svcCancelSynchronization(gThread.handle);
|
||||
threadWaitForExit(&gThread);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
38
Source/hoc-clk/sysmodule/src/ipc_service.hpp
Normal file
38
Source/hoc-clk/sysmodule/src/ipc_service.hpp
Normal 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 <sysclk.h>
|
||||
#include <switch.h>
|
||||
|
||||
namespace ipcService {
|
||||
|
||||
void Initialize();
|
||||
void Exit();
|
||||
void SetRunning(bool running);
|
||||
|
||||
}
|
||||
255
Source/hoc-clk/sysmodule/src/kip.cpp
Normal file
255
Source/hoc-clk/sysmodule/src/kip.cpp
Normal file
@@ -0,0 +1,255 @@
|
||||
/*
|
||||
* 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 "kip.hpp"
|
||||
#include "board/board.hpp"
|
||||
|
||||
namespace kip {
|
||||
|
||||
bool kipAvailable = false;
|
||||
|
||||
void SetKipData()
|
||||
{
|
||||
// TODO: figure out if this REALLY causes issues (i doubt it)
|
||||
// if(board::GetSocType() == SysClkSocType_Mariko) {
|
||||
// if(R_FAILED(I2c_BuckConverter_SetMvOut(&I2c_Mariko_DRAM_VDDQ, config::GetConfigValue(KipConfigValue_marikoEmcVddqVolt) / 1000))) {
|
||||
// fileUtils::LogLine("[clock_manager] Failed set i2c vddq");
|
||||
// notification::writeNotification("Horizon OC\nFailed to write I2C\nwhile setting vddq");
|
||||
// }
|
||||
// }
|
||||
CustomizeTable table;
|
||||
FILE* fp;
|
||||
fp = fopen("sdmc:/atmosphere/kips/hoc.kip", "r");
|
||||
|
||||
if (fp == NULL) {
|
||||
notification::writeNotification("Horizon OC\nKip opening failed");
|
||||
kipAvailable = false;
|
||||
return;
|
||||
} else {
|
||||
kipAvailable = true;
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
if (!cust_read_and_cache("sdmc:/atmosphere/kips/hoc.kip", &table)) {
|
||||
fileUtils::LogLine("[clock_manager] Failed to read KIP file");
|
||||
notification::writeNotification("Horizon OC\nKip read failed");
|
||||
return;
|
||||
}
|
||||
|
||||
CUST_WRITE_FIELD_BATCH(&table, custRev, config::GetConfigValue(KipConfigValue_custRev));
|
||||
// CUST_WRITE_FIELD_BATCH(&table, mtcConf, config::GetConfigValue(KipConfigValue_mtcConf));
|
||||
CUST_WRITE_FIELD_BATCH(&table, hpMode, config::GetConfigValue(KipConfigValue_hpMode));
|
||||
|
||||
CUST_WRITE_FIELD_BATCH(&table, commonEmcMemVolt, config::GetConfigValue(KipConfigValue_commonEmcMemVolt));
|
||||
CUST_WRITE_FIELD_BATCH(&table, eristaEmcMaxClock, config::GetConfigValue(KipConfigValue_eristaEmcMaxClock));
|
||||
CUST_WRITE_FIELD_BATCH(&table, eristaEmcMaxClock1, config::GetConfigValue(KipConfigValue_eristaEmcMaxClock1));
|
||||
CUST_WRITE_FIELD_BATCH(&table, eristaEmcMaxClock2, config::GetConfigValue(KipConfigValue_eristaEmcMaxClock2));
|
||||
CUST_WRITE_FIELD_BATCH(&table, marikoEmcMaxClock, config::GetConfigValue(KipConfigValue_marikoEmcMaxClock));
|
||||
CUST_WRITE_FIELD_BATCH(&table, marikoEmcVddqVolt, config::GetConfigValue(KipConfigValue_marikoEmcVddqVolt));
|
||||
CUST_WRITE_FIELD_BATCH(&table, emcDvbShift, config::GetConfigValue(KipConfigValue_emcDvbShift));
|
||||
|
||||
CUST_WRITE_FIELD_BATCH(&table, t1_tRCD, config::GetConfigValue(KipConfigValue_t1_tRCD));
|
||||
CUST_WRITE_FIELD_BATCH(&table, t2_tRP, config::GetConfigValue(KipConfigValue_t2_tRP));
|
||||
CUST_WRITE_FIELD_BATCH(&table, t3_tRAS, config::GetConfigValue(KipConfigValue_t3_tRAS));
|
||||
CUST_WRITE_FIELD_BATCH(&table, t4_tRRD, config::GetConfigValue(KipConfigValue_t4_tRRD));
|
||||
CUST_WRITE_FIELD_BATCH(&table, t5_tRFC, config::GetConfigValue(KipConfigValue_t5_tRFC));
|
||||
CUST_WRITE_FIELD_BATCH(&table, t6_tRTW, config::GetConfigValue(KipConfigValue_t6_tRTW));
|
||||
CUST_WRITE_FIELD_BATCH(&table, t7_tWTR, config::GetConfigValue(KipConfigValue_t7_tWTR));
|
||||
CUST_WRITE_FIELD_BATCH(&table, t8_tREFI, config::GetConfigValue(KipConfigValue_t8_tREFI));
|
||||
CUST_WRITE_FIELD_BATCH(&table, mem_burst_read_latency, config::GetConfigValue(KipConfigValue_mem_burst_read_latency));
|
||||
CUST_WRITE_FIELD_BATCH(&table, mem_burst_write_latency, config::GetConfigValue(KipConfigValue_mem_burst_write_latency));
|
||||
CUST_WRITE_FIELD_BATCH(&table, eristaCpuUV, config::GetConfigValue(KipConfigValue_eristaCpuUV));
|
||||
CUST_WRITE_FIELD_BATCH(&table, eristaCpuVmin, config::GetConfigValue(KipConfigValue_eristaCpuVmin));
|
||||
CUST_WRITE_FIELD_BATCH(&table, eristaCpuMaxVolt, config::GetConfigValue(KipConfigValue_eristaCpuMaxVolt));
|
||||
CUST_WRITE_FIELD_BATCH(&table, eristaCpuUnlock, config::GetConfigValue(KipConfigValue_eristaCpuUnlock));
|
||||
|
||||
CUST_WRITE_FIELD_BATCH(&table, marikoCpuUVLow, config::GetConfigValue(KipConfigValue_marikoCpuUVLow));
|
||||
CUST_WRITE_FIELD_BATCH(&table, marikoCpuUVHigh, config::GetConfigValue(KipConfigValue_marikoCpuUVHigh));
|
||||
CUST_WRITE_FIELD_BATCH(&table, tableConf, config::GetConfigValue(KipConfigValue_tableConf));
|
||||
CUST_WRITE_FIELD_BATCH(&table, marikoCpuLowVmin, config::GetConfigValue(KipConfigValue_marikoCpuLowVmin));
|
||||
CUST_WRITE_FIELD_BATCH(&table, marikoCpuHighVmin, config::GetConfigValue(KipConfigValue_marikoCpuHighVmin));
|
||||
CUST_WRITE_FIELD_BATCH(&table, marikoCpuMaxVolt, config::GetConfigValue(KipConfigValue_marikoCpuMaxVolt));
|
||||
CUST_WRITE_FIELD_BATCH(&table, marikoCpuMaxClock, config::GetConfigValue(KipConfigValue_marikoCpuMaxClock));
|
||||
|
||||
CUST_WRITE_FIELD_BATCH(&table, eristaCpuBoostClock, config::GetConfigValue(KipConfigValue_eristaCpuBoostClock));
|
||||
CUST_WRITE_FIELD_BATCH(&table, marikoCpuBoostClock, config::GetConfigValue(KipConfigValue_marikoCpuBoostClock));
|
||||
|
||||
CUST_WRITE_FIELD_BATCH(&table, eristaGpuUV, config::GetConfigValue(KipConfigValue_eristaGpuUV));
|
||||
CUST_WRITE_FIELD_BATCH(&table, eristaGpuVmin, config::GetConfigValue(KipConfigValue_eristaGpuVmin));
|
||||
|
||||
CUST_WRITE_FIELD_BATCH(&table, marikoGpuUV, config::GetConfigValue(KipConfigValue_marikoGpuUV));
|
||||
CUST_WRITE_FIELD_BATCH(&table, marikoGpuVmin, config::GetConfigValue(KipConfigValue_marikoGpuVmin));
|
||||
CUST_WRITE_FIELD_BATCH(&table, marikoGpuVmax, config::GetConfigValue(KipConfigValue_marikoGpuVmax));
|
||||
|
||||
CUST_WRITE_FIELD_BATCH(&table, commonGpuVoltOffset, config::GetConfigValue(KipConfigValue_commonGpuVoltOffset));
|
||||
CUST_WRITE_FIELD_BATCH(&table, gpuSpeedo, config::GetConfigValue(KipConfigValue_gpuSpeedo));
|
||||
|
||||
for (int i = 0; i < 24; i++) {
|
||||
table.marikoGpuVoltArray[i] = config::GetConfigValue((SysClkConfigValue)(KipConfigValue_g_volt_76800 + i));
|
||||
}
|
||||
|
||||
for (int i = 0; i < 27; i++) {
|
||||
table.eristaGpuVoltArray[i] = config::GetConfigValue((SysClkConfigValue)(KipConfigValue_g_volt_e_76800 + i));
|
||||
}
|
||||
|
||||
CUST_WRITE_FIELD_BATCH(&table, t6_tRTW_fine_tune, config::GetConfigValue(KipConfigValue_t6_tRTW_fine_tune));
|
||||
CUST_WRITE_FIELD_BATCH(&table, t7_tWTR_fine_tune, config::GetConfigValue(KipConfigValue_t7_tWTR_fine_tune));
|
||||
|
||||
if (!cust_write_table("sdmc:/atmosphere/kips/hoc.kip", &table)) {
|
||||
fileUtils::LogLine("[clock_manager] Failed to write KIP file");
|
||||
notification::writeNotification("Horizon OC\nKip write failed");
|
||||
}
|
||||
|
||||
SysClkConfigValueList configValues;
|
||||
config::GetConfigValues(&configValues);
|
||||
|
||||
configValues.values[KipCrc32] = (u64)crc32::checksum_file("sdmc:/atmosphere/kips/hoc.kip"); // write checksum
|
||||
|
||||
if (config::SetConfigValues(&configValues, false)) {
|
||||
fileUtils::LogLine("[clock_manager] Successfully loaded KIP data into config");
|
||||
} else {
|
||||
fileUtils::LogLine("[clock_manager] Warning: Failed to set config values from KIP");
|
||||
notification::writeNotification("Horizon OC\nKip config set failed");
|
||||
}
|
||||
}
|
||||
|
||||
// I know this is very hacky, but the config system in the sysmodule doesn't really support writing
|
||||
|
||||
void GetKipData()
|
||||
{
|
||||
FILE* fp;
|
||||
if (config::Refresh()) {
|
||||
fp = fopen("sdmc:/atmosphere/kips/hoc.kip", "r");
|
||||
|
||||
if (fp == NULL) {
|
||||
notification::writeNotification("Horizon OC\nKip opening failed");
|
||||
kipAvailable = false;
|
||||
return;
|
||||
} else {
|
||||
kipAvailable = true;
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
SysClkConfigValueList configValues;
|
||||
config::GetConfigValues(&configValues);
|
||||
|
||||
CustomizeTable table;
|
||||
|
||||
if (!cust_read_and_cache("sdmc:/atmosphere/kips/hoc.kip", &table)) {
|
||||
fileUtils::LogLine("[clock_manager] Failed to read KIP file for GetKipData");
|
||||
notification::writeNotification("Horizon OC\nKip read failed");
|
||||
return;
|
||||
}
|
||||
|
||||
if ((u64)crc32::checksum_file("sdmc:/atmosphere/kips/hoc.kip") != config::GetConfigValue(KipCrc32) && !config::GetConfigValue(HocClkConfigValue_IsFirstLoad)) {
|
||||
SetKipData();
|
||||
notification::writeNotification("Horizon OC\nKIP has been updated");
|
||||
notification::writeNotification("Horizon OC\nPlease reboot your console");
|
||||
notification::writeNotification("Horizon OC\nto complete the update");
|
||||
return;
|
||||
}
|
||||
if (config::GetConfigValue(HocClkConfigValue_IsFirstLoad) == true) {
|
||||
configValues.values[HocClkConfigValue_IsFirstLoad] = (u64)false;
|
||||
notification::writeNotification("Horizon OC has been installed");
|
||||
}
|
||||
|
||||
configValues.values[KipCrc32] = (u64)crc32::checksum_file("sdmc:/atmosphere/kips/hoc.kip"); // write checksum
|
||||
// configValues.values[KipConfigValue_mtcConf] = cust_get_mtc_conf(&table);
|
||||
configValues.values[KipConfigValue_hpMode] = cust_get_hp_mode(&table);
|
||||
|
||||
configValues.values[KipConfigValue_commonEmcMemVolt] = cust_get_common_emc_volt(&table);
|
||||
configValues.values[KipConfigValue_eristaEmcMaxClock] = cust_get_erista_emc_max(&table);
|
||||
configValues.values[KipConfigValue_eristaEmcMaxClock1] = cust_get_erista_emc_max1(&table);
|
||||
configValues.values[KipConfigValue_eristaEmcMaxClock2] = cust_get_erista_emc_max2(&table);
|
||||
configValues.values[KipConfigValue_marikoEmcMaxClock] = cust_get_mariko_emc_max(&table);
|
||||
configValues.values[KipConfigValue_marikoEmcVddqVolt] = cust_get_mariko_emc_vddq(&table);
|
||||
configValues.values[KipConfigValue_emcDvbShift] = cust_get_emc_dvb_shift(&table);
|
||||
|
||||
configValues.values[KipConfigValue_t1_tRCD] = cust_get_tRCD(&table);
|
||||
configValues.values[KipConfigValue_t2_tRP] = cust_get_tRP(&table);
|
||||
configValues.values[KipConfigValue_t3_tRAS] = cust_get_tRAS(&table);
|
||||
configValues.values[KipConfigValue_t4_tRRD] = cust_get_tRRD(&table);
|
||||
configValues.values[KipConfigValue_t5_tRFC] = cust_get_tRFC(&table);
|
||||
configValues.values[KipConfigValue_t6_tRTW] = cust_get_tRTW(&table);
|
||||
configValues.values[KipConfigValue_t7_tWTR] = cust_get_tWTR(&table);
|
||||
configValues.values[KipConfigValue_t8_tREFI] = cust_get_tREFI(&table);
|
||||
configValues.values[KipConfigValue_mem_burst_read_latency] = cust_get_burst_read_lat(&table);
|
||||
configValues.values[KipConfigValue_mem_burst_write_latency] = cust_get_burst_write_lat(&table);
|
||||
|
||||
configValues.values[KipConfigValue_eristaCpuUV] = cust_get_erista_cpu_uv(&table);
|
||||
configValues.values[KipConfigValue_eristaCpuVmin] = cust_get_eristaCpuVmin(&table);
|
||||
configValues.values[KipConfigValue_eristaCpuMaxVolt] = cust_get_erista_cpu_max_volt(&table);
|
||||
configValues.values[KipConfigValue_eristaCpuUnlock] = cust_get_eristaCpuUnlock(&table);
|
||||
|
||||
configValues.values[KipConfigValue_marikoCpuUVLow] = cust_get_mariko_cpu_uv_low(&table);
|
||||
configValues.values[KipConfigValue_marikoCpuUVHigh] = cust_get_mariko_cpu_uv_high(&table);
|
||||
configValues.values[KipConfigValue_tableConf] = cust_get_table_conf(&table);
|
||||
configValues.values[KipConfigValue_marikoCpuLowVmin] = cust_get_mariko_cpu_low_vmin(&table);
|
||||
configValues.values[KipConfigValue_marikoCpuHighVmin] = cust_get_mariko_cpu_high_vmin(&table);
|
||||
configValues.values[KipConfigValue_marikoCpuMaxVolt] = cust_get_mariko_cpu_max_volt(&table);
|
||||
configValues.values[KipConfigValue_marikoCpuMaxClock] = cust_get_marikoCpuMaxClock(&table);
|
||||
configValues.values[KipConfigValue_eristaCpuBoostClock] = cust_get_erista_cpu_boost(&table);
|
||||
configValues.values[KipConfigValue_marikoCpuBoostClock] = cust_get_mariko_cpu_boost(&table);
|
||||
|
||||
configValues.values[KipConfigValue_eristaGpuUV] = cust_get_erista_gpu_uv(&table);
|
||||
configValues.values[KipConfigValue_eristaGpuVmin] = cust_get_erista_gpu_vmin(&table);
|
||||
configValues.values[KipConfigValue_marikoGpuUV] = cust_get_mariko_gpu_uv(&table);
|
||||
configValues.values[KipConfigValue_marikoGpuVmin] = cust_get_mariko_gpu_vmin(&table);
|
||||
configValues.values[KipConfigValue_marikoGpuVmax] = cust_get_mariko_gpu_vmax(&table);
|
||||
configValues.values[KipConfigValue_commonGpuVoltOffset] = cust_get_common_gpu_offset(&table);
|
||||
configValues.values[KipConfigValue_gpuSpeedo] = board::GetFuseData()->gpuSpeedo; // cust_get_gpu_speedo(&table);
|
||||
|
||||
for (int i = 0; i < 24; i++) {
|
||||
configValues.values[KipConfigValue_g_volt_76800 + i] = cust_get_mariko_gpu_volt(&table, i);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 27; i++) {
|
||||
configValues.values[KipConfigValue_g_volt_e_76800 + i] = cust_get_erista_gpu_volt(&table, i);
|
||||
}
|
||||
|
||||
configValues.values[KipConfigValue_t7_tWTR_fine_tune] = cust_get_tWTR_fine_tune(&table);
|
||||
configValues.values[KipConfigValue_t6_tRTW_fine_tune] = cust_get_tRTW_fine_tune(&table);
|
||||
|
||||
// if(cust_get_cust_rev(&table) == KIP_CUST_REV)
|
||||
// return;
|
||||
|
||||
if (sizeof(SysClkConfigValueList) <= sizeof(configValues)) {
|
||||
if (config::SetConfigValues(&configValues, false)) {
|
||||
fileUtils::LogLine("[clock_manager] Successfully loaded KIP data into config");
|
||||
} else {
|
||||
fileUtils::LogLine("[clock_manager] Warning: Failed to set config values from KIP");
|
||||
notification::writeNotification("Horizon OC\nKip config set failed");
|
||||
}
|
||||
} else {
|
||||
fileUtils::LogLine("[clock_manager] Error: Config value list buffer size mismatch");
|
||||
notification::writeNotification("Horizon OC\nConfig Buffer Mismatch");
|
||||
}
|
||||
} else {
|
||||
fileUtils::LogLine("[clock_manager] Config refresh error in GetKipData!");
|
||||
notification::writeNotification("Horizon OC\nConfig refresh failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
467
Source/hoc-clk/sysmodule/src/kip.hpp
Normal file
467
Source/hoc-clk/sysmodule/src/kip.hpp
Normal file
@@ -0,0 +1,467 @@
|
||||
/*
|
||||
* 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 <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "config.hpp"
|
||||
#include "file_utils.hpp"
|
||||
#include <notification.h>
|
||||
#include <crc32.h>
|
||||
|
||||
#pragma pack(push, 1)
|
||||
|
||||
namespace kip {
|
||||
extern bool kipAvailable;
|
||||
|
||||
typedef struct {
|
||||
u8 cust[4];
|
||||
u32 custRev;
|
||||
u32 placeholder;
|
||||
u32 hpMode;
|
||||
u32 commonEmcMemVolt;
|
||||
u32 eristaEmcMaxClock;
|
||||
u32 eristaEmcMaxClock1;
|
||||
u32 eristaEmcMaxClock2;
|
||||
u32 marikoEmcMaxClock;
|
||||
u32 marikoEmcVddqVolt;
|
||||
u32 emcDvbShift;
|
||||
// advanced config
|
||||
u32 t1_tRCD;
|
||||
u32 t2_tRP;
|
||||
u32 t3_tRAS;
|
||||
u32 t4_tRRD;
|
||||
u32 t5_tRFC;
|
||||
u32 t6_tRTW;
|
||||
u32 t7_tWTR;
|
||||
u32 t8_tREFI;
|
||||
|
||||
u32 mem_burst_read_latency;
|
||||
u32 mem_burst_write_latency;
|
||||
|
||||
u32 eristaCpuUV;
|
||||
u32 eristaCpuVmin;
|
||||
u32 eristaCpuMaxVolt;
|
||||
u32 eristaCpuUnlock;
|
||||
|
||||
u32 marikoCpuUVLow;
|
||||
u32 marikoCpuUVHigh;
|
||||
u32 tableConf;
|
||||
u32 marikoCpuLowVmin;
|
||||
u32 marikoCpuHighVmin;
|
||||
u32 marikoCpuMaxVolt;
|
||||
u32 marikoCpuMaxClock;
|
||||
|
||||
u32 eristaCpuBoostClock;
|
||||
u32 marikoCpuBoostClock;
|
||||
|
||||
u32 eristaGpuUV;
|
||||
u32 eristaGpuVmin;
|
||||
|
||||
u32 marikoGpuUV;
|
||||
u32 marikoGpuVmin;
|
||||
u32 marikoGpuVmax;
|
||||
|
||||
u32 commonGpuVoltOffset;
|
||||
|
||||
u32 gpuSpeedo;
|
||||
|
||||
u32 eristaGpuVoltArray[27];
|
||||
u32 marikoGpuVoltArray[24];
|
||||
|
||||
u32 t6_tRTW_fine_tune;
|
||||
u32 t7_tWTR_fine_tune;
|
||||
|
||||
u32 reserved[60];
|
||||
} CustomizeTable;
|
||||
|
||||
#pragma pack(pop)
|
||||
|
||||
#define CUST_MAGIC "CUST"
|
||||
#define CUST_MAGIC_LEN 4
|
||||
|
||||
typedef struct {
|
||||
FILE* file;
|
||||
long offset;
|
||||
CustomizeTable cached_table;
|
||||
bool has_cache;
|
||||
} CustHandle;
|
||||
|
||||
static inline bool cust_find_offset(FILE* f, long* out_offset) {
|
||||
u8 buf[512];
|
||||
long pos = 0;
|
||||
fseek(f, 0, SEEK_SET);
|
||||
|
||||
while (1) {
|
||||
size_t r = fread(buf, 1, sizeof(buf), f);
|
||||
if (r < CUST_MAGIC_LEN) break;
|
||||
|
||||
for (size_t i = 0; i <= r - CUST_MAGIC_LEN; i++) {
|
||||
if (memcmp(&buf[i], CUST_MAGIC, CUST_MAGIC_LEN) == 0) {
|
||||
*out_offset = pos + (long)i;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
pos += (long)(r - (CUST_MAGIC_LEN - 1));
|
||||
fseek(f, pos, SEEK_SET);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline bool cust_read_table(const char* path, CustomizeTable* out) {
|
||||
FILE* f = fopen(path, "rb");
|
||||
if (!f) return false;
|
||||
|
||||
long off;
|
||||
if (!cust_find_offset(f, &off)) {
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
|
||||
fseek(f, 0, SEEK_END);
|
||||
long size = ftell(f);
|
||||
|
||||
if (off + (long)sizeof(CustomizeTable) > size) {
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
|
||||
fseek(f, off, SEEK_SET);
|
||||
bool ok = fread(out, 1, sizeof(CustomizeTable), f) == sizeof(CustomizeTable);
|
||||
fclose(f);
|
||||
|
||||
return ok && memcmp(out->cust, CUST_MAGIC, CUST_MAGIC_LEN) == 0;
|
||||
}
|
||||
|
||||
static inline bool cust_write_table(const char* path, const CustomizeTable* in) {
|
||||
FILE* f = fopen(path, "r+b");
|
||||
if (!f) return false;
|
||||
|
||||
long off;
|
||||
if (!cust_find_offset(f, &off)) {
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
|
||||
fseek(f, 0, SEEK_END);
|
||||
long size = ftell(f);
|
||||
|
||||
if (off + (long)sizeof(CustomizeTable) > size) {
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
|
||||
fseek(f, off, SEEK_SET);
|
||||
bool ok = fwrite(in, 1, sizeof(CustomizeTable), f) == sizeof(CustomizeTable);
|
||||
fflush(f);
|
||||
fclose(f);
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
static inline bool cust_read_and_cache(const char* path, CustomizeTable* out) {
|
||||
return cust_read_table(path, out);
|
||||
}
|
||||
|
||||
#define CUST_WRITE_FIELD_BATCH(table, field, val) \
|
||||
do { \
|
||||
(table)->field = (val); \
|
||||
} while (0)
|
||||
|
||||
#define CUST_WRITE_FIELD(path, field, val) \
|
||||
do { \
|
||||
CustomizeTable t; \
|
||||
if (!cust_read_table(path, &t)) return false; \
|
||||
t.field = (val); \
|
||||
return cust_write_table(path, &t); \
|
||||
} while (0)
|
||||
|
||||
static inline bool cust_set_cust_rev(const char* p, u32 v) { CUST_WRITE_FIELD(p, custRev, v); }
|
||||
// static inline bool cust_set_mtc_conf(const char* p, u32 v) { CUST_WRITE_FIELD(p, mtcConf, v); }
|
||||
static inline bool cust_set_hp_mode(const char* p, u32 v) { CUST_WRITE_FIELD(p, hpMode, v); }
|
||||
|
||||
static inline bool cust_set_common_emc_volt(const char* p, u32 v) { CUST_WRITE_FIELD(p, commonEmcMemVolt, v); }
|
||||
static inline bool cust_set_erista_emc_max(const char* p, u32 v) { CUST_WRITE_FIELD(p, eristaEmcMaxClock, v); }
|
||||
static inline bool cust_set_erista_emc_max1(const char* p, u32 v) { CUST_WRITE_FIELD(p, eristaEmcMaxClock1, v); }
|
||||
static inline bool cust_set_erista_emc_max2(const char* p, u32 v) { CUST_WRITE_FIELD(p, eristaEmcMaxClock2, v); }
|
||||
static inline bool cust_set_mariko_emc_max(const char* p, u32 v) { CUST_WRITE_FIELD(p, marikoEmcMaxClock, v); }
|
||||
static inline bool cust_set_mariko_emc_vddq(const char* p, u32 v) { CUST_WRITE_FIELD(p, marikoEmcVddqVolt, v); }
|
||||
static inline bool cust_set_emc_dvb_shift(const char* p, u32 v) { CUST_WRITE_FIELD(p, emcDvbShift, v); }
|
||||
|
||||
static inline bool cust_set_tRCD(const char* p, u32 v) { CUST_WRITE_FIELD(p, t1_tRCD, v); }
|
||||
static inline bool cust_set_tRP(const char* p, u32 v) { CUST_WRITE_FIELD(p, t2_tRP, v); }
|
||||
static inline bool cust_set_tRAS(const char* p, u32 v) { CUST_WRITE_FIELD(p, t3_tRAS, v); }
|
||||
static inline bool cust_set_tRRD(const char* p, u32 v) { CUST_WRITE_FIELD(p, t4_tRRD, v); }
|
||||
static inline bool cust_set_tRFC(const char* p, u32 v) { CUST_WRITE_FIELD(p, t5_tRFC, v); }
|
||||
static inline bool cust_set_tRTW(const char* p, u32 v) { CUST_WRITE_FIELD(p, t6_tRTW, v); }
|
||||
static inline bool cust_set_tWTR(const char* p, u32 v) { CUST_WRITE_FIELD(p, t7_tWTR, v); }
|
||||
static inline bool cust_set_tREFI(const char* p, u32 v) { CUST_WRITE_FIELD(p, t8_tREFI, v); }
|
||||
static inline bool cust_set_tRTW_fine_tune(const char* p, u32 v) { CUST_WRITE_FIELD(p, t6_tRTW_fine_tune, v); }
|
||||
static inline bool cust_set_tWTR_fine_tune(const char* p, u32 v) { CUST_WRITE_FIELD(p, t7_tWTR_fine_tune, v); }
|
||||
static inline bool cust_set_burst_read_lat(const char* p, u32 v) { CUST_WRITE_FIELD(p, mem_burst_read_latency, v); }
|
||||
static inline bool cust_set_burst_write_lat(const char* p, u32 v) { CUST_WRITE_FIELD(p, mem_burst_write_latency, v); }
|
||||
|
||||
static inline bool cust_set_erista_cpu_uv(const char* p, u32 v) { CUST_WRITE_FIELD(p, eristaCpuUV, v); }
|
||||
static inline bool cust_set_eristaCpuVmin(const char* p, u32 v) { CUST_WRITE_FIELD(p, eristaCpuVmin, v); }
|
||||
static inline bool cust_set_erista_cpu_max_volt(const char* p, u32 v) { CUST_WRITE_FIELD(p, eristaCpuMaxVolt, v); }
|
||||
static inline bool cust_set_eristaCpuUnlock(const char* p, u32 v) { CUST_WRITE_FIELD(p, eristaCpuUnlock, v); }
|
||||
|
||||
static inline bool cust_set_mariko_cpu_uv_low(const char* p, u32 v) { CUST_WRITE_FIELD(p, marikoCpuUVLow, v); }
|
||||
static inline bool cust_set_mariko_cpu_uv_high(const char* p, u32 v) { CUST_WRITE_FIELD(p, marikoCpuUVHigh, v); }
|
||||
static inline bool cust_set_mariko_cpu_low_vmin(const char* p, u32 v) { CUST_WRITE_FIELD(p, marikoCpuLowVmin, v); }
|
||||
static inline bool cust_set_mariko_cpu_high_vmin(const char* p, u32 v) { CUST_WRITE_FIELD(p, marikoCpuHighVmin, v); }
|
||||
static inline bool cust_set_mariko_cpu_max_volt(const char* p, u32 v) { CUST_WRITE_FIELD(p, marikoCpuMaxVolt, v); }
|
||||
static inline bool cust_set_erista_cpu_boost(const char* p, u32 v) { CUST_WRITE_FIELD(p, eristaCpuBoostClock, v); }
|
||||
static inline bool cust_set_mariko_cpu_boost(const char* p, u32 v) { CUST_WRITE_FIELD(p, marikoCpuBoostClock, v); }
|
||||
|
||||
static inline bool cust_set_erista_gpu_uv(const char* p, u32 v) { CUST_WRITE_FIELD(p, eristaGpuUV, v); }
|
||||
static inline bool cust_set_erista_gpu_vmin(const char* p, u32 v) { CUST_WRITE_FIELD(p, eristaGpuVmin, v); }
|
||||
static inline bool cust_set_mariko_gpu_uv(const char* p, u32 v) { CUST_WRITE_FIELD(p, marikoGpuUV, v); }
|
||||
static inline bool cust_set_mariko_gpu_vmin(const char* p, u32 v) { CUST_WRITE_FIELD(p, marikoGpuVmin, v); }
|
||||
static inline bool cust_set_mariko_gpu_vmax(const char* p, u32 v) { CUST_WRITE_FIELD(p, marikoGpuVmax, v); }
|
||||
static inline bool cust_set_common_gpu_offset(const char* p, u32 v) { CUST_WRITE_FIELD(p, commonGpuVoltOffset, v); }
|
||||
static inline bool cust_set_gpu_speedo(const char* p, u32 v) { CUST_WRITE_FIELD(p, gpuSpeedo, v); }
|
||||
static inline bool cust_set_marikoCpuMaxClock(const char* p, u32 v) { CUST_WRITE_FIELD(p, marikoCpuMaxClock, v); }
|
||||
|
||||
/* GPU VOLT ARRAY HELPERS */
|
||||
static inline bool cust_set_erista_gpu_volt(const char* p, int idx, u32 v) {
|
||||
if (idx < 0 || idx >= 27) return false;
|
||||
CustomizeTable t;
|
||||
if (!cust_read_table(p, &t)) return false;
|
||||
t.eristaGpuVoltArray[idx] = v;
|
||||
return cust_write_table(p, &t);
|
||||
}
|
||||
|
||||
static inline bool cust_set_mariko_gpu_volt(const char* p, int idx, u32 v) {
|
||||
if (idx < 0 || idx >= 24) return false;
|
||||
CustomizeTable t;
|
||||
if (!cust_read_table(p, &t)) return false;
|
||||
t.marikoGpuVoltArray[idx] = v;
|
||||
return cust_write_table(p, &t);
|
||||
}
|
||||
|
||||
static inline u32 cust_get_field(const CustomizeTable* t, u32 offset) {
|
||||
if (!t) return 0;
|
||||
return *(u32*)((u8*)t + offset);
|
||||
}
|
||||
|
||||
#define CUST_GET_FIELD(table, field) ((table) ? (table)->field : 0)
|
||||
|
||||
static inline u32 cust_get_cust_rev(const CustomizeTable* t) { return CUST_GET_FIELD(t, custRev); }
|
||||
// static inline u32 cust_get_mtc_conf(const CustomizeTable* t) { return CUST_GET_FIELD(t, mtcConf); }
|
||||
static inline u32 cust_get_hp_mode(const CustomizeTable* t) { return CUST_GET_FIELD(t, hpMode); }
|
||||
|
||||
static inline u32 cust_get_common_emc_volt(const CustomizeTable* t) { return CUST_GET_FIELD(t, commonEmcMemVolt); }
|
||||
static inline u32 cust_get_erista_emc_max(const CustomizeTable* t) { return CUST_GET_FIELD(t, eristaEmcMaxClock); }
|
||||
static inline u32 cust_get_erista_emc_max1(const CustomizeTable* t) { return CUST_GET_FIELD(t, eristaEmcMaxClock1); }
|
||||
static inline u32 cust_get_erista_emc_max2(const CustomizeTable* t) { return CUST_GET_FIELD(t, eristaEmcMaxClock2); }
|
||||
static inline u32 cust_get_mariko_emc_max(const CustomizeTable* t) { return CUST_GET_FIELD(t, marikoEmcMaxClock); }
|
||||
static inline u32 cust_get_mariko_emc_vddq(const CustomizeTable* t) { return CUST_GET_FIELD(t, marikoEmcVddqVolt); }
|
||||
static inline u32 cust_get_emc_dvb_shift(const CustomizeTable* t) { return CUST_GET_FIELD(t, emcDvbShift); }
|
||||
|
||||
static inline u32 cust_get_tRCD(const CustomizeTable* t) { return CUST_GET_FIELD(t, t1_tRCD); }
|
||||
static inline u32 cust_get_tRP(const CustomizeTable* t) { return CUST_GET_FIELD(t, t2_tRP); }
|
||||
static inline u32 cust_get_tRAS(const CustomizeTable* t) { return CUST_GET_FIELD(t, t3_tRAS); }
|
||||
static inline u32 cust_get_tRRD(const CustomizeTable* t) { return CUST_GET_FIELD(t, t4_tRRD); }
|
||||
static inline u32 cust_get_tRFC(const CustomizeTable* t) { return CUST_GET_FIELD(t, t5_tRFC); }
|
||||
static inline u32 cust_get_tRTW(const CustomizeTable* t) { return CUST_GET_FIELD(t, t6_tRTW); }
|
||||
static inline u32 cust_get_tWTR(const CustomizeTable* t) { return CUST_GET_FIELD(t, t7_tWTR); }
|
||||
static inline u32 cust_get_tREFI(const CustomizeTable* t) { return CUST_GET_FIELD(t, t8_tREFI); }
|
||||
static inline u32 cust_get_tRTW_fine_tune(const CustomizeTable* t) { return CUST_GET_FIELD(t, t6_tRTW_fine_tune); }
|
||||
static inline u32 cust_get_tWTR_fine_tune(const CustomizeTable* t) { return CUST_GET_FIELD(t, t7_tWTR_fine_tune); }
|
||||
static inline u32 cust_get_burst_read_lat(const CustomizeTable* t) { return CUST_GET_FIELD(t, mem_burst_read_latency); }
|
||||
static inline u32 cust_get_burst_write_lat(const CustomizeTable* t) { return CUST_GET_FIELD(t, mem_burst_write_latency); }
|
||||
|
||||
static inline u32 cust_get_erista_cpu_uv(const CustomizeTable* t) { return CUST_GET_FIELD(t, eristaCpuUV); }
|
||||
static inline u32 cust_get_eristaCpuVmin(const CustomizeTable* t) { return CUST_GET_FIELD(t, eristaCpuVmin); }
|
||||
static inline u32 cust_get_erista_cpu_max_volt(const CustomizeTable* t) { return CUST_GET_FIELD(t, eristaCpuMaxVolt); }
|
||||
static inline u32 cust_get_eristaCpuUnlock(const CustomizeTable* t) { return CUST_GET_FIELD(t, eristaCpuUnlock); }
|
||||
|
||||
static inline u32 cust_get_mariko_cpu_uv_low(const CustomizeTable* t) { return CUST_GET_FIELD(t, marikoCpuUVLow); }
|
||||
static inline u32 cust_get_mariko_cpu_uv_high(const CustomizeTable* t) { return CUST_GET_FIELD(t, marikoCpuUVHigh); }
|
||||
static inline u32 cust_get_mariko_cpu_low_vmin(const CustomizeTable* t) { return CUST_GET_FIELD(t, marikoCpuLowVmin); }
|
||||
static inline u32 cust_get_mariko_cpu_high_vmin(const CustomizeTable* t) { return CUST_GET_FIELD(t, marikoCpuHighVmin); }
|
||||
static inline u32 cust_get_mariko_cpu_max_volt(const CustomizeTable* t) { return CUST_GET_FIELD(t, marikoCpuMaxVolt); }
|
||||
static inline u32 cust_get_erista_cpu_boost(const CustomizeTable* t) { return CUST_GET_FIELD(t, eristaCpuBoostClock); }
|
||||
static inline u32 cust_get_mariko_cpu_boost(const CustomizeTable* t) { return CUST_GET_FIELD(t, marikoCpuBoostClock); }
|
||||
static inline u32 cust_get_table_conf(const CustomizeTable* t) { return CUST_GET_FIELD(t, tableConf); }
|
||||
|
||||
static inline u32 cust_get_erista_gpu_uv(const CustomizeTable* t) { return CUST_GET_FIELD(t, eristaGpuUV); }
|
||||
static inline u32 cust_get_erista_gpu_vmin(const CustomizeTable* t) { return CUST_GET_FIELD(t, eristaGpuVmin); }
|
||||
static inline u32 cust_get_mariko_gpu_uv(const CustomizeTable* t) { return CUST_GET_FIELD(t, marikoGpuUV); }
|
||||
static inline u32 cust_get_mariko_gpu_vmin(const CustomizeTable* t) { return CUST_GET_FIELD(t, marikoGpuVmin); }
|
||||
static inline u32 cust_get_mariko_gpu_vmax(const CustomizeTable* t) { return CUST_GET_FIELD(t, marikoGpuVmax); }
|
||||
static inline u32 cust_get_common_gpu_offset(const CustomizeTable* t) { return CUST_GET_FIELD(t, commonGpuVoltOffset); }
|
||||
static inline u32 cust_get_gpu_speedo(const CustomizeTable* t) { return CUST_GET_FIELD(t, gpuSpeedo); }
|
||||
static inline u32 cust_get_marikoCpuMaxClock(const CustomizeTable* t) { return CUST_GET_FIELD(t, marikoCpuMaxClock); }
|
||||
|
||||
static inline u32 cust_get_erista_gpu_volt(const CustomizeTable* t, int idx) {
|
||||
if (!t || idx < 0 || idx >= 27) return 0;
|
||||
return t->eristaGpuVoltArray[idx];
|
||||
}
|
||||
|
||||
static inline u32 cust_get_mariko_gpu_volt(const CustomizeTable* t, int idx) {
|
||||
if (!t || idx < 0 || idx >= 24) return 0;
|
||||
return t->marikoGpuVoltArray[idx];
|
||||
}
|
||||
|
||||
#define DECL_ERISTA_GPU_VOLT_HELPER(freq, idx) \
|
||||
static inline bool cust_set_erista_gpu_volt_##freq( \
|
||||
const char* p, u32 v) { \
|
||||
return cust_set_erista_gpu_volt(p, idx, v); \
|
||||
}
|
||||
|
||||
#define DECL_MARIKO_GPU_VOLT_HELPER(freq, idx) \
|
||||
static inline bool cust_set_mariko_gpu_volt_##freq( \
|
||||
const char* p, u32 v) { \
|
||||
return cust_set_mariko_gpu_volt(p, idx, v); \
|
||||
}
|
||||
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(76800, 0)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(115200, 1)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(153600, 2)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(192000, 3)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(230400, 4)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(268800, 5)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(307200, 6)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(345600, 7)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(384000, 8)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(422400, 9)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(460800, 10)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(499200, 11)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(537600, 12)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(576000, 13)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(614400, 14)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(652800, 15)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(691200, 16)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(729600, 17)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(768000, 18)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(806400, 19)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(844800, 20)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(883200, 21)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(921600, 22)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(960000, 23)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(998400, 24)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(1036800, 25)
|
||||
DECL_ERISTA_GPU_VOLT_HELPER(1075200, 26)
|
||||
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(76800, 0)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(153600, 1)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(230400, 2)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(307200, 3)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(384000, 4)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(460800, 5)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(537600, 6)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(614400, 7)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(691200, 8)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(768000, 9)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(844800, 10)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(921600, 11)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(998400, 12)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(1075200, 13)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(1152000, 14)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(1228800, 15)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(1267200, 16)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(1305600, 17)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(1344000, 18)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(1382400, 19)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(1420800, 20)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(1459200, 21)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(1497600, 22)
|
||||
DECL_MARIKO_GPU_VOLT_HELPER(1536000, 23)
|
||||
|
||||
#define DECL_ERISTA_GPU_VOLT_GET(freq, idx) \
|
||||
static inline u32 cust_get_erista_gpu_volt_##freq##_val(const char* p) { \
|
||||
CustomizeTable t; \
|
||||
if (!cust_read_table(p, &t)) return 0; \
|
||||
return cust_get_erista_gpu_volt(&t, idx); \
|
||||
}
|
||||
#define DECL_MARIKO_GPU_VOLT_GET(freq, idx) \
|
||||
static inline u32 cust_get_mariko_gpu_volt_##freq##_val(const char* p) { \
|
||||
CustomizeTable t; \
|
||||
if (!cust_read_table(p, &t)) return 0; \
|
||||
return cust_get_mariko_gpu_volt(&t, idx); \
|
||||
}
|
||||
|
||||
DECL_ERISTA_GPU_VOLT_GET(76800, 0)
|
||||
DECL_ERISTA_GPU_VOLT_GET(115200, 1)
|
||||
DECL_ERISTA_GPU_VOLT_GET(153600, 2)
|
||||
DECL_ERISTA_GPU_VOLT_GET(192000, 3)
|
||||
DECL_ERISTA_GPU_VOLT_GET(230400, 4)
|
||||
DECL_ERISTA_GPU_VOLT_GET(268800, 5)
|
||||
DECL_ERISTA_GPU_VOLT_GET(307200, 6)
|
||||
DECL_ERISTA_GPU_VOLT_GET(345600, 7)
|
||||
DECL_ERISTA_GPU_VOLT_GET(384000, 8)
|
||||
DECL_ERISTA_GPU_VOLT_GET(422400, 9)
|
||||
DECL_ERISTA_GPU_VOLT_GET(460800, 10)
|
||||
DECL_ERISTA_GPU_VOLT_GET(499200, 11)
|
||||
DECL_ERISTA_GPU_VOLT_GET(537600, 12)
|
||||
DECL_ERISTA_GPU_VOLT_GET(576000, 13)
|
||||
DECL_ERISTA_GPU_VOLT_GET(614400, 14)
|
||||
DECL_ERISTA_GPU_VOLT_GET(652800, 15)
|
||||
DECL_ERISTA_GPU_VOLT_GET(691200, 16)
|
||||
DECL_ERISTA_GPU_VOLT_GET(729600, 17)
|
||||
DECL_ERISTA_GPU_VOLT_GET(768000, 18)
|
||||
DECL_ERISTA_GPU_VOLT_GET(806400, 19)
|
||||
DECL_ERISTA_GPU_VOLT_GET(844800, 20)
|
||||
DECL_ERISTA_GPU_VOLT_GET(883200, 21)
|
||||
DECL_ERISTA_GPU_VOLT_GET(921600, 22)
|
||||
DECL_ERISTA_GPU_VOLT_GET(960000, 23)
|
||||
DECL_ERISTA_GPU_VOLT_GET(998400, 24)
|
||||
DECL_ERISTA_GPU_VOLT_GET(1036800, 25)
|
||||
DECL_ERISTA_GPU_VOLT_GET(1075200, 26)
|
||||
|
||||
DECL_MARIKO_GPU_VOLT_GET(76800, 0)
|
||||
DECL_MARIKO_GPU_VOLT_GET(153600, 1)
|
||||
DECL_MARIKO_GPU_VOLT_GET(230400, 2)
|
||||
DECL_MARIKO_GPU_VOLT_GET(307200, 3)
|
||||
DECL_MARIKO_GPU_VOLT_GET(384000, 4)
|
||||
DECL_MARIKO_GPU_VOLT_GET(460800, 5)
|
||||
DECL_MARIKO_GPU_VOLT_GET(537600, 6)
|
||||
DECL_MARIKO_GPU_VOLT_GET(614400, 7)
|
||||
DECL_MARIKO_GPU_VOLT_GET(691200, 8)
|
||||
DECL_MARIKO_GPU_VOLT_GET(768000, 9)
|
||||
DECL_MARIKO_GPU_VOLT_GET(844800, 10)
|
||||
DECL_MARIKO_GPU_VOLT_GET(921600, 11)
|
||||
DECL_MARIKO_GPU_VOLT_GET(998400, 12)
|
||||
DECL_MARIKO_GPU_VOLT_GET(1075200, 13)
|
||||
DECL_MARIKO_GPU_VOLT_GET(1152000, 14)
|
||||
DECL_MARIKO_GPU_VOLT_GET(1228800, 15)
|
||||
DECL_MARIKO_GPU_VOLT_GET(1267200, 16)
|
||||
DECL_MARIKO_GPU_VOLT_GET(1305600, 17)
|
||||
DECL_MARIKO_GPU_VOLT_GET(1344000, 18)
|
||||
DECL_MARIKO_GPU_VOLT_GET(1382400, 19)
|
||||
DECL_MARIKO_GPU_VOLT_GET(1420800, 20)
|
||||
DECL_MARIKO_GPU_VOLT_GET(1459200, 21)
|
||||
DECL_MARIKO_GPU_VOLT_GET(1497600, 22)
|
||||
DECL_MARIKO_GPU_VOLT_GET(1536000, 23)
|
||||
|
||||
void SetKipData();
|
||||
void GetKipData();
|
||||
}
|
||||
169
Source/hoc-clk/sysmodule/src/main.cpp
Normal file
169
Source/hoc-clk/sysmodule/src/main.cpp
Normal file
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* 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 <cstdlib>
|
||||
#include <cstring>
|
||||
#include <malloc.h>
|
||||
|
||||
#include <switch.h>
|
||||
|
||||
#include "errors.hpp"
|
||||
#include "file_utils.hpp"
|
||||
#include "board/board.hpp"
|
||||
#include "process_management.hpp"
|
||||
#include "clock_manager.hpp"
|
||||
#include "ipc_service.hpp"
|
||||
#include "config.hpp"
|
||||
#define INNER_HEAP_SIZE 0x45000
|
||||
|
||||
|
||||
extern "C"
|
||||
{
|
||||
void virtmemSetup(void);
|
||||
|
||||
extern std::uint32_t __start__;
|
||||
|
||||
std::uint32_t __nx_applet_type = AppletType_None;
|
||||
TimeServiceType __nx_time_service_type = TimeServiceType_System;
|
||||
std::uint32_t __nx_fs_num_sessions = 1;
|
||||
u32 __nx_nv_transfermem_size = 0x8000;
|
||||
size_t nx_inner_heap_size = INNER_HEAP_SIZE;
|
||||
char nx_inner_heap[INNER_HEAP_SIZE];
|
||||
NvServiceType __nx_nv_service_type = NvServiceType_Factory;
|
||||
|
||||
|
||||
void __libnx_initheap(void)
|
||||
{
|
||||
void* addr = nx_inner_heap;
|
||||
size_t size = nx_inner_heap_size;
|
||||
|
||||
/* Newlib Heap Management */
|
||||
extern char* fake_heap_start;
|
||||
extern char* fake_heap_end;
|
||||
|
||||
fake_heap_start = (char*)addr;
|
||||
fake_heap_end = (char*)addr + size;
|
||||
|
||||
virtmemSetup();
|
||||
}
|
||||
|
||||
void __appInit(void)
|
||||
{
|
||||
if (R_FAILED(smInitialize()))
|
||||
{
|
||||
fatalThrow(MAKERESULT(Module_Libnx, LibnxError_InitFail_SM));
|
||||
}
|
||||
|
||||
Result rc = setsysInitialize();
|
||||
if (R_SUCCEEDED(rc))
|
||||
{
|
||||
SetSysFirmwareVersion fw;
|
||||
rc = setsysGetFirmwareVersion(&fw);
|
||||
if (R_SUCCEEDED(rc))
|
||||
hosversionSet(MAKEHOSVERSION(fw.major, fw.minor, fw.micro));
|
||||
setsysExit();
|
||||
}
|
||||
|
||||
// rc = fanInitialize();
|
||||
// if (R_FAILED(rc))
|
||||
// diagAbortWithResult(MAKERESULT(Module_Libnx, LibnxError_ShouldNotHappen));
|
||||
|
||||
rc = i2cInitialize();
|
||||
if (R_FAILED(rc))
|
||||
diagAbortWithResult(MAKERESULT(Module_Libnx, LibnxError_ShouldNotHappen));
|
||||
rc = appletInitialize();
|
||||
if (R_FAILED(rc))
|
||||
diagAbortWithResult(MAKERESULT(Module_Libnx, LibnxError_ShouldNotHappen));
|
||||
}
|
||||
|
||||
void __appExit(void)
|
||||
{
|
||||
// CloseFanControllerThread();
|
||||
// fanExit();
|
||||
i2cExit();
|
||||
fsExit();
|
||||
fsdevUnmountAll();
|
||||
appletExit();
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
Result rc = fileUtils::Initialize();
|
||||
if (R_FAILED(rc))
|
||||
{
|
||||
fatalThrow(rc);
|
||||
return 1;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
board::Initialize();
|
||||
processManagement::Initialize();
|
||||
|
||||
processManagement::WaitForQLaunch();
|
||||
|
||||
clockManager::Initialize();
|
||||
ipcService::Initialize();
|
||||
|
||||
fileUtils::LogLine("Ready");
|
||||
|
||||
clockManager::SetRunning(true);
|
||||
config::SetEnabled(true);
|
||||
ipcService::SetRunning(true);
|
||||
// TemperaturePoint *table;
|
||||
// ReadConfigFile(&table);
|
||||
// InitFanController(table);
|
||||
// StartFanControllerThread();
|
||||
|
||||
while (clockManager::Running())
|
||||
{
|
||||
clockManager::Tick();
|
||||
clockManager::WaitForNextTick();
|
||||
}
|
||||
|
||||
ipcService::SetRunning(false);
|
||||
ipcService::Exit();
|
||||
clockManager::Exit();
|
||||
processManagement::Exit();
|
||||
board::Exit();
|
||||
}
|
||||
catch (const std::exception &ex)
|
||||
{
|
||||
fileUtils::LogLine("[!] %s", ex.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
std::exception_ptr p = std::current_exception();
|
||||
fileUtils::LogLine("[!?] %s", p ? p.__cxa_exception_type()->name() : "...");
|
||||
}
|
||||
|
||||
fileUtils::LogLine("Exit");
|
||||
svcSleepThread(1000000ULL);
|
||||
fileUtils::Exit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
87
Source/hoc-clk/sysmodule/src/process_management.cpp
Normal file
87
Source/hoc-clk/sysmodule/src/process_management.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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 "process_management.hpp"
|
||||
#include "file_utils.hpp"
|
||||
#include "errors.hpp"
|
||||
|
||||
namespace processManagement {
|
||||
|
||||
namespace {
|
||||
constexpr u64 Qlaunch = 0x0100000000001000ULL;
|
||||
constexpr u32 IsQlaunch = 0x20f;
|
||||
}
|
||||
|
||||
void Initialize() {
|
||||
Result rc = 0;
|
||||
|
||||
rc = pmdmntInitialize();
|
||||
ASSERT_RESULT_OK(rc, "pmdmntInitialize");
|
||||
|
||||
rc = pminfoInitialize();
|
||||
ASSERT_RESULT_OK(rc, "pminfoInitialize");
|
||||
}
|
||||
|
||||
void WaitForQLaunch() {
|
||||
|
||||
Result rc = 0;
|
||||
u64 pid = 0;
|
||||
do {
|
||||
rc = pmdmntGetProcessId(&pid, Qlaunch);
|
||||
svcSleepThread(50 * 1000000ULL); // 50ms
|
||||
} while (R_FAILED(rc));
|
||||
}
|
||||
|
||||
u64 GetCurrentApplicationId() {
|
||||
Result rc = 0;
|
||||
u64 pid = 0;
|
||||
u64 tid = 0;
|
||||
rc = pmdmntGetApplicationProcessId(&pid);
|
||||
|
||||
if (rc == IsQlaunch) {
|
||||
return Qlaunch;
|
||||
}
|
||||
|
||||
ASSERT_RESULT_OK(rc, "pmdmntGetApplicationProcessId");
|
||||
|
||||
rc = pminfoGetProgramId(&tid, pid);
|
||||
|
||||
if (rc == IsQlaunch) {
|
||||
return Qlaunch;
|
||||
}
|
||||
|
||||
ASSERT_RESULT_OK(rc, "pminfoGetProgramId");
|
||||
|
||||
return tid;
|
||||
}
|
||||
|
||||
void Exit() {
|
||||
pmdmntExit();
|
||||
pminfoExit();
|
||||
}
|
||||
|
||||
}
|
||||
38
Source/hoc-clk/sysmodule/src/process_management.hpp
Normal file
38
Source/hoc-clk/sysmodule/src/process_management.hpp
Normal 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>
|
||||
|
||||
namespace processManagement {
|
||||
|
||||
void Initialize();
|
||||
void WaitForQLaunch();
|
||||
u64 GetCurrentApplicationId();
|
||||
void Exit();
|
||||
|
||||
}
|
||||
649
Source/hoc-clk/sysmodule/src/soctherm.cpp
Normal file
649
Source/hoc-clk/sysmodule/src/soctherm.cpp
Normal file
@@ -0,0 +1,649 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Author:
|
||||
* Mikko Perttunen <mperttunen@nvidia.com>
|
||||
*
|
||||
* 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 "soctherm.hpp"
|
||||
#include "board/board.hpp"
|
||||
#include "file_utils.hpp"
|
||||
|
||||
namespace soctherm {
|
||||
|
||||
namespace {
|
||||
|
||||
#define FUSE_CACHE_OFFSET 0x800
|
||||
#define FUSE_TSENSOR_COMMON 0xA80
|
||||
#define CAR_CLK_SOURCE_TSENSOR 0x3B8
|
||||
#define CAR_CLK_OUT_ENB_V 0x360
|
||||
|
||||
#define CLK_RST_CONTROLLER_RST_DEVICES 0xC
|
||||
#define SWR_SOC_THERM_RST 1 << 14
|
||||
|
||||
#define CLK_RST_CONTROLLER_CLK_OUT_ENB 0x18
|
||||
#define CLK_ENB_SOC_THERM 1 << 14
|
||||
|
||||
#define CAR_CLK_SOURCE_TSENSOR_VAL 0x8000005E
|
||||
|
||||
#define NOMINAL_CALIB_FT 105
|
||||
#define NOMINAL_CALIB_CP 25
|
||||
|
||||
#define THERMCTL_LEVEL0_GROUP_CPU 0x0
|
||||
#define THERMCTL_LEVEL0_GROUP_GPU 0x4
|
||||
#define THERMCTL_LEVEL0_GROUP_MEM 0x8
|
||||
#define THERMCTL_LEVEL0_GROUP_TSENSE 0xc
|
||||
|
||||
#define TEGRA124_SOCTHERM_SENSOR_CPU 0
|
||||
#define TEGRA124_SOCTHERM_SENSOR_MEM 1
|
||||
#define TEGRA124_SOCTHERM_SENSOR_GPU 2
|
||||
#define TEGRA124_SOCTHERM_SENSOR_PLLX 3
|
||||
#define TEGRA124_SOCTHERM_SENSOR_NUM 4
|
||||
|
||||
#define TEGRA_SOCTHERM_THROT_LEVEL_NONE 0
|
||||
#define TEGRA_SOCTHERM_THROT_LEVEL_LOW 1
|
||||
#define TEGRA_SOCTHERM_THROT_LEVEL_MED 2
|
||||
#define TEGRA_SOCTHERM_THROT_LEVEL_HIGH 3
|
||||
|
||||
#define SENSOR_CONFIG2 8
|
||||
#define SENSOR_CONFIG2_THERMA_MASK (0xffffu << 16)
|
||||
#define SENSOR_CONFIG2_THERMA_SHIFT 16
|
||||
#define SENSOR_CONFIG2_THERMB_MASK 0xffff
|
||||
#define SENSOR_CONFIG2_THERMB_SHIFT 0
|
||||
|
||||
#define THERMCTL_THERMTRIP_CTL 0x80
|
||||
|
||||
#define SENSOR_PDIV 0x1c0
|
||||
#define SENSOR_PDIV_CPU_MASK (0xf << 12)
|
||||
#define SENSOR_PDIV_GPU_MASK (0xf << 8)
|
||||
#define SENSOR_PDIV_MEM_MASK (0xf << 4)
|
||||
#define SENSOR_PDIV_PLLX_MASK (0xf << 0)
|
||||
|
||||
#define SENSOR_HOTSPOT_OFF 0x1c4
|
||||
#define SENSOR_HOTSPOT_CPU_MASK (0xff << 16)
|
||||
#define SENSOR_HOTSPOT_GPU_MASK (0xff << 8)
|
||||
#define SENSOR_HOTSPOT_MEM_MASK (0xff << 0)
|
||||
|
||||
#define SENSOR_HW_PLLX_OFFSET_EN 0x1e4
|
||||
#define SENSOR_HW_PLLX_OFFSET_MEM_EN_MASK BIT(2)
|
||||
#define SENSOR_HW_PLLX_OFFSET_CPU_EN_MASK BIT(1)
|
||||
#define SENSOR_HW_PLLX_OFFSET_GPU_EN_MASK BIT(0)
|
||||
|
||||
#define SENSOR_HW_PLLX_OFFSET_MIN 0x1e8
|
||||
#define SENSOR_HW_PLLX_OFFSET_MAX 0x1ec
|
||||
#define SENSOR_HW_PLLX_OFFSET_MEM_MASK (0xff << 16)
|
||||
#define SENSOR_HW_PLLX_OFFSET_GPU_MASK (0xff << 8)
|
||||
#define SENSOR_HW_PLLX_OFFSET_CPU_MASK (0xff << 0)
|
||||
|
||||
#define SENSOR_TEMP1 0x1c8
|
||||
#define SENSOR_TEMP1_CPU_TEMP_MASK (0xffffu << 16)
|
||||
#define SENSOR_TEMP1_GPU_TEMP_MASK 0xffff
|
||||
#define SENSOR_TEMP2 0x1cc
|
||||
#define SENSOR_TEMP2_MEM_TEMP_MASK (0xffffu << 16)
|
||||
#define SENSOR_TEMP2_PLLX_TEMP_MASK 0xffff
|
||||
|
||||
#define SENSOR_VALID 0x1e0
|
||||
#define SENSOR_GPU_VALID_MASK BIT(9)
|
||||
#define SENSOR_CPU_VALID_MASK 0xf
|
||||
#define SENSOR_MEM_VALID_MASK (0x3 << 10)
|
||||
|
||||
#define TEGRA210_THERMTRIP_ANY_EN_MASK (0x1u << 31)
|
||||
#define TEGRA210_THERMTRIP_MEM_EN_MASK (0x1 << 30)
|
||||
#define TEGRA210_THERMTRIP_GPU_EN_MASK (0x1 << 29)
|
||||
#define TEGRA210_THERMTRIP_CPU_EN_MASK (0x1 << 28)
|
||||
#define TEGRA210_THERMTRIP_TSENSE_EN_MASK (0x1 << 27)
|
||||
#define TEGRA210_THERMTRIP_GPUMEM_THRESH_MASK (0x1ff << 18)
|
||||
#define TEGRA210_THERMTRIP_CPU_THRESH_MASK (0x1ff << 9)
|
||||
#define TEGRA210_THERMTRIP_TSENSE_THRESH_MASK 0x1ff
|
||||
|
||||
#define TEGRA210_THERM_IRQ_MEM_MASK (0x3 << 24)
|
||||
#define TEGRA210_THERM_IRQ_GPU_MASK (0x3 << 16)
|
||||
#define TEGRA210_THERM_IRQ_CPU_MASK (0x3 << 8)
|
||||
#define TEGRA210_THERM_IRQ_TSENSE_MASK (0x3 << 0)
|
||||
|
||||
#define TEGRA210_THERMCTL_LVL0_UP_THRESH_MASK (0x1ff << 18)
|
||||
#define TEGRA210_THERMCTL_LVL0_DN_THRESH_MASK (0x1ff << 9)
|
||||
|
||||
#define TEGRA210_THRESH_GRAIN 500
|
||||
#define TEGRA210_BPTT 9
|
||||
|
||||
#define FUSE_TSENSOR_CALIB_CP_TS_BASE_MASK 0x1fff
|
||||
#define FUSE_TSENSOR_CALIB_FT_TS_BASE_MASK (0x1fff << 13)
|
||||
#define FUSE_TSENSOR_CALIB_FT_TS_BASE_SHIFT 13
|
||||
#define CALIB_COEFFICIENT 1000000LL
|
||||
|
||||
#define SENSOR_CONFIG0 0
|
||||
#define SENSOR_CONFIG0_STOP BIT(0)
|
||||
#define SENSOR_CONFIG0_CPTR_OVER BIT(2)
|
||||
#define SENSOR_CONFIG0_OVER BIT(3)
|
||||
#define SENSOR_CONFIG0_TCALC_OVER BIT(4)
|
||||
#define SENSOR_CONFIG0_TALL_MASK (0xfffff << 8)
|
||||
#define SENSOR_CONFIG0_TALL_SHIFT 8
|
||||
|
||||
#define PDIV_RATE_T210B0 0xCC0C
|
||||
#define PDIV_RATE_T210 0x8888
|
||||
#define HOTSPOT_VAL 0xA0500
|
||||
|
||||
#define SENSOR_CONFIG1 4
|
||||
#define SENSOR_CONFIG1_TSAMPLE_MASK 0x3ff
|
||||
#define SENSOR_CONFIG1_TSAMPLE_SHIFT 0
|
||||
#define SENSOR_CONFIG1_TIDDQ_EN_MASK (0x3f << 15)
|
||||
#define SENSOR_CONFIG1_TIDDQ_EN_SHIFT 15
|
||||
#define SENSOR_CONFIG1_TEN_COUNT_MASK (0x3f << 24)
|
||||
#define SENSOR_CONFIG1_TEN_COUNT_SHIFT 24
|
||||
#define SENSOR_CONFIG1_TEMP_ENABLE BIT(31)
|
||||
|
||||
#define READBACK_VALUE_MASK 0xff00
|
||||
#define READBACK_VALUE_SHIFT 8
|
||||
#define READBACK_ADD_HALF BIT(7)
|
||||
#define READBACK_NEGATE BIT(0)
|
||||
|
||||
#define PDIV_MASK_T210B0 0xFFFF00F0
|
||||
#define HOTSPOT_MASK_T210B0 0xFF0000FF
|
||||
|
||||
#define PDIV_MASK_T210 0xFFFF0000
|
||||
#define HOTSPOT_MASK_T210 0xFF000000
|
||||
|
||||
#define TSENSOR_TSENSOR_CLKEN 0x1DC
|
||||
#define TSENSOR_TSENSOR_ENABLE 225
|
||||
|
||||
const TSensorFuse tfuse = {
|
||||
.fuse_base_cp_mask = 0x3ff << 11,
|
||||
.fuse_base_cp_shift = 11,
|
||||
.fuse_base_ft_mask = 0x7ffu << 21,
|
||||
.fuse_base_ft_shift = 21,
|
||||
.fuse_shift_ft_mask = 0x1f << 6,
|
||||
.fuse_shift_ft_shift = 6,
|
||||
.fuse_spare_realignment = 0,
|
||||
};
|
||||
|
||||
const TSensorConfig eristaConf = {
|
||||
.tall = 16300,
|
||||
.tiddq_en = 1,
|
||||
.ten_count = 1,
|
||||
.pdiv = 8,
|
||||
.pdiv_ate = 8,
|
||||
.tsample = 120,
|
||||
.tsample_ate = 480,
|
||||
};
|
||||
|
||||
const TSensorConfig marikoConf = {
|
||||
.tall = 16300,
|
||||
.tiddq_en = 1,
|
||||
.ten_count = 1,
|
||||
.pdiv = 12,
|
||||
.pdiv_ate = 6,
|
||||
.tsample = 240,
|
||||
.tsample_ate = 480,
|
||||
};
|
||||
|
||||
const struct TSensorGroup tSensorGroupCpu = {
|
||||
.name = "cpu",
|
||||
.id = TEGRA124_SOCTHERM_SENSOR_CPU,
|
||||
.sensor_temp_offset = SENSOR_TEMP1,
|
||||
.sensor_temp_mask = SENSOR_TEMP1_CPU_TEMP_MASK,
|
||||
.pdiv_mask = SENSOR_PDIV_CPU_MASK,
|
||||
.pllx_hotspot_diff = 10,
|
||||
.pllx_hotspot_mask = SENSOR_HOTSPOT_CPU_MASK,
|
||||
.hw_pllx_offset_mask = SENSOR_HW_PLLX_OFFSET_CPU_MASK,
|
||||
.hw_pllx_offset_en_mask = SENSOR_HW_PLLX_OFFSET_CPU_EN_MASK,
|
||||
.thermtrip_enable_mask = TEGRA210_THERMTRIP_CPU_EN_MASK,
|
||||
.thermtrip_any_en_mask = TEGRA210_THERMTRIP_ANY_EN_MASK,
|
||||
.thermtrip_threshold_mask = TEGRA210_THERMTRIP_CPU_THRESH_MASK,
|
||||
.thermctl_lvl0_offset = THERMCTL_LEVEL0_GROUP_CPU,
|
||||
.thermctl_isr_mask = TEGRA210_THERM_IRQ_CPU_MASK,
|
||||
.thermctl_lvl0_up_thresh_mask = TEGRA210_THERMCTL_LVL0_UP_THRESH_MASK,
|
||||
.thermctl_lvl0_dn_thresh_mask = TEGRA210_THERMCTL_LVL0_DN_THRESH_MASK,
|
||||
};
|
||||
|
||||
const struct TSensorGroup tSensorGroupGpu = {
|
||||
.name = "gpu",
|
||||
.id = TEGRA124_SOCTHERM_SENSOR_GPU,
|
||||
.sensor_temp_offset = SENSOR_TEMP1,
|
||||
.sensor_temp_mask = SENSOR_TEMP1_GPU_TEMP_MASK,
|
||||
.pdiv_mask = SENSOR_PDIV_GPU_MASK,
|
||||
.pllx_hotspot_diff = 5,
|
||||
.pllx_hotspot_mask = SENSOR_HOTSPOT_GPU_MASK,
|
||||
.hw_pllx_offset_mask = SENSOR_HW_PLLX_OFFSET_GPU_MASK,
|
||||
.hw_pllx_offset_en_mask = SENSOR_HW_PLLX_OFFSET_GPU_EN_MASK,
|
||||
.thermtrip_enable_mask = TEGRA210_THERMTRIP_GPU_EN_MASK,
|
||||
.thermtrip_any_en_mask = TEGRA210_THERMTRIP_ANY_EN_MASK,
|
||||
.thermtrip_threshold_mask = TEGRA210_THERMTRIP_GPUMEM_THRESH_MASK,
|
||||
.thermctl_lvl0_offset = THERMCTL_LEVEL0_GROUP_GPU,
|
||||
.thermctl_isr_mask = TEGRA210_THERM_IRQ_GPU_MASK,
|
||||
.thermctl_lvl0_up_thresh_mask = TEGRA210_THERMCTL_LVL0_UP_THRESH_MASK,
|
||||
.thermctl_lvl0_dn_thresh_mask = TEGRA210_THERMCTL_LVL0_DN_THRESH_MASK,
|
||||
};
|
||||
|
||||
const struct TSensorGroup tSensorGroupPll = {
|
||||
.name = "pll",
|
||||
.id = TEGRA124_SOCTHERM_SENSOR_PLLX,
|
||||
.sensor_temp_offset = SENSOR_TEMP2,
|
||||
.sensor_temp_mask = SENSOR_TEMP2_PLLX_TEMP_MASK,
|
||||
.pdiv_mask = SENSOR_PDIV_PLLX_MASK,
|
||||
.thermtrip_enable_mask = TEGRA210_THERMTRIP_TSENSE_EN_MASK,
|
||||
.thermtrip_any_en_mask = TEGRA210_THERMTRIP_ANY_EN_MASK,
|
||||
.thermtrip_threshold_mask = TEGRA210_THERMTRIP_TSENSE_THRESH_MASK,
|
||||
.thermctl_lvl0_offset = THERMCTL_LEVEL0_GROUP_TSENSE,
|
||||
.thermctl_isr_mask = TEGRA210_THERM_IRQ_TSENSE_MASK,
|
||||
.thermctl_lvl0_up_thresh_mask = TEGRA210_THERMCTL_LVL0_UP_THRESH_MASK,
|
||||
.thermctl_lvl0_dn_thresh_mask = TEGRA210_THERMCTL_LVL0_DN_THRESH_MASK,
|
||||
};
|
||||
|
||||
const struct TSensorGroup eristaTSensorGroupMem = {
|
||||
.name = "mem",
|
||||
.id = TEGRA124_SOCTHERM_SENSOR_MEM,
|
||||
.sensor_temp_offset = SENSOR_TEMP2,
|
||||
.sensor_temp_mask = SENSOR_TEMP2_MEM_TEMP_MASK,
|
||||
.pdiv_mask = SENSOR_PDIV_MEM_MASK,
|
||||
.pllx_hotspot_diff = 0,
|
||||
.pllx_hotspot_mask = SENSOR_HOTSPOT_MEM_MASK,
|
||||
.hw_pllx_offset_mask = SENSOR_HW_PLLX_OFFSET_MEM_MASK,
|
||||
.hw_pllx_offset_en_mask = SENSOR_HW_PLLX_OFFSET_MEM_EN_MASK,
|
||||
.thermtrip_enable_mask = TEGRA210_THERMTRIP_MEM_EN_MASK,
|
||||
.thermtrip_any_en_mask = TEGRA210_THERMTRIP_ANY_EN_MASK,
|
||||
.thermtrip_threshold_mask = TEGRA210_THERMTRIP_GPUMEM_THRESH_MASK,
|
||||
.thermctl_lvl0_offset = THERMCTL_LEVEL0_GROUP_MEM,
|
||||
.thermctl_isr_mask = TEGRA210_THERM_IRQ_MEM_MASK,
|
||||
.thermctl_lvl0_up_thresh_mask = TEGRA210_THERMCTL_LVL0_UP_THRESH_MASK,
|
||||
.thermctl_lvl0_dn_thresh_mask = TEGRA210_THERMCTL_LVL0_DN_THRESH_MASK,
|
||||
};
|
||||
|
||||
const TSensor eristaTSensors[] = {
|
||||
{
|
||||
.name = "cpu0",
|
||||
.base = 0xc0,
|
||||
.config = &eristaConf,
|
||||
.calib_fuse_offset = 0x198,
|
||||
.fuse_corr = {
|
||||
.alpha = 1085000,
|
||||
.beta = 3244200,
|
||||
},
|
||||
.group = &tSensorGroupCpu,
|
||||
}, {
|
||||
.name = "cpu1",
|
||||
.base = 0xe0,
|
||||
.config = &eristaConf,
|
||||
.calib_fuse_offset = 0x184,
|
||||
.fuse_corr = {
|
||||
.alpha = 1126200,
|
||||
.beta = -67500,
|
||||
},
|
||||
.group = &tSensorGroupCpu,
|
||||
}, {
|
||||
.name = "cpu2",
|
||||
.base = 0x100,
|
||||
.config = &eristaConf,
|
||||
.calib_fuse_offset = 0x188,
|
||||
.fuse_corr = {
|
||||
.alpha = 1098400,
|
||||
.beta = 2251100,
|
||||
},
|
||||
.group = &tSensorGroupCpu,
|
||||
}, {
|
||||
.name = "cpu3",
|
||||
.base = 0x120,
|
||||
.config = &eristaConf,
|
||||
.calib_fuse_offset = 0x22c,
|
||||
.fuse_corr = {
|
||||
.alpha = 1108000,
|
||||
.beta = 602700,
|
||||
},
|
||||
.group = &tSensorGroupCpu,
|
||||
}, {
|
||||
.name = "gpu",
|
||||
.base = 0x180,
|
||||
.config = &eristaConf,
|
||||
.calib_fuse_offset = 0x254,
|
||||
.fuse_corr = {
|
||||
.alpha = 1074300,
|
||||
.beta = 2734900,
|
||||
},
|
||||
.group = &tSensorGroupGpu,
|
||||
}, {
|
||||
.name = "pllx",
|
||||
.base = 0x1a0,
|
||||
.config = &eristaConf,
|
||||
.calib_fuse_offset = 0x260,
|
||||
.fuse_corr = {
|
||||
.alpha = 1039700,
|
||||
.beta = 6829100,
|
||||
},
|
||||
.group = &tSensorGroupPll,
|
||||
}, {
|
||||
.name = "mem0",
|
||||
.base = 0x140,
|
||||
.config = &eristaConf,
|
||||
.calib_fuse_offset = 0x258,
|
||||
.fuse_corr = {
|
||||
.alpha = 1069200,
|
||||
.beta = 3549900,
|
||||
},
|
||||
.group = &eristaTSensorGroupMem,
|
||||
}, {
|
||||
.name = "mem1",
|
||||
.base = 0x160,
|
||||
.config = &eristaConf,
|
||||
.calib_fuse_offset = 0x25c,
|
||||
.fuse_corr = {
|
||||
.alpha = 1173700,
|
||||
.beta = -6263600,
|
||||
},
|
||||
.group = &eristaTSensorGroupMem,
|
||||
},
|
||||
};
|
||||
|
||||
const TSensor marikoTSensors[] = {
|
||||
{
|
||||
.name = "cpu0",
|
||||
.base = 0xc0,
|
||||
.config = &marikoConf,
|
||||
.calib_fuse_offset = 0x198,
|
||||
.fuse_corr = {
|
||||
.alpha = 1085000,
|
||||
.beta = 3244200,
|
||||
},
|
||||
.group = &tSensorGroupCpu,
|
||||
}, {
|
||||
.name = "cpu1",
|
||||
.base = 0xe0,
|
||||
.config = &marikoConf,
|
||||
.calib_fuse_offset = 0x184,
|
||||
.fuse_corr = {
|
||||
.alpha = 1126200,
|
||||
.beta = -67500,
|
||||
},
|
||||
.group = &tSensorGroupCpu,
|
||||
}, {
|
||||
.name = "cpu2",
|
||||
.base = 0x100,
|
||||
.config = &marikoConf,
|
||||
.calib_fuse_offset = 0x188,
|
||||
.fuse_corr = {
|
||||
.alpha = 1098400,
|
||||
.beta = 2251100,
|
||||
},
|
||||
.group = &tSensorGroupCpu,
|
||||
}, {
|
||||
.name = "cpu3",
|
||||
.base = 0x120,
|
||||
.config = &marikoConf,
|
||||
.calib_fuse_offset = 0x22c,
|
||||
.fuse_corr = {
|
||||
.alpha = 1108000,
|
||||
.beta = 602700,
|
||||
},
|
||||
.group = &tSensorGroupCpu,
|
||||
}, {
|
||||
.name = "gpu",
|
||||
.base = 0x180,
|
||||
.config = &marikoConf,
|
||||
.calib_fuse_offset = 0x254,
|
||||
.fuse_corr = {
|
||||
.alpha = 1074300,
|
||||
.beta = 2734900,
|
||||
},
|
||||
.group = &tSensorGroupGpu,
|
||||
}, {
|
||||
.name = "pllx",
|
||||
.base = 0x1a0,
|
||||
.config = &marikoConf,
|
||||
.calib_fuse_offset = 0x260,
|
||||
.fuse_corr = {
|
||||
.alpha = 1039700,
|
||||
.beta = 6829100,
|
||||
},
|
||||
.group = &tSensorGroupPll,
|
||||
},
|
||||
};
|
||||
|
||||
u32 calib[SocthermTSensor_EnumMax] = {};
|
||||
u64 socthermVa, carVa, fuseVa;
|
||||
bool isMariko;
|
||||
}
|
||||
|
||||
template<typename T = u32>
|
||||
static inline T ReadReg(u64 base, u32 offset) {
|
||||
return *reinterpret_cast<volatile T*>(base + offset);
|
||||
}
|
||||
|
||||
template<typename T = u32>
|
||||
static inline void WriteReg(u64 base, u32 offset, T value) {
|
||||
*reinterpret_cast<volatile T*>(base + offset) = value;
|
||||
}
|
||||
|
||||
template<typename T = u32>
|
||||
static inline void SetBits(u64 base, u32 offset, T mask) {
|
||||
WriteReg(base, offset, ReadReg<T>(base, offset) | mask);
|
||||
}
|
||||
|
||||
template<typename T = u32>
|
||||
static inline void ClearBits(u64 base, u32 offset, T mask) {
|
||||
WriteReg(base, offset, ReadReg<T>(base, offset) & ~mask);
|
||||
}
|
||||
|
||||
Result MapAddress(u64 &va, const u64 &physAddr, const char *name) {
|
||||
u64 outSize;
|
||||
Result mapResult = svcQueryMemoryMapping(&va, &outSize, physAddr, 0x1000);
|
||||
if (R_FAILED(mapResult)) {
|
||||
fileUtils::LogLine("[Soctherm] Failed to map %s! %u", name, R_DESCRIPTION(mapResult));
|
||||
}
|
||||
|
||||
return mapResult;
|
||||
}
|
||||
|
||||
static inline s32 sign_extend32(u32 value, int index) {
|
||||
u8 shift = 31 - index;
|
||||
return (s32) (value << shift) >> shift;
|
||||
}
|
||||
|
||||
static inline s64 div64_s64(s64 dividend, s64 divisor) {
|
||||
return dividend / divisor;
|
||||
}
|
||||
|
||||
static s64 div64_s64_precise(s64 a, s32 b) {
|
||||
s64 r, al;
|
||||
|
||||
al = a << 16;
|
||||
|
||||
r = div64_s64(al * 2 + 1, 2 * b);
|
||||
return r >> 16;
|
||||
}
|
||||
|
||||
bool IsDisabledThroughSleep() {
|
||||
return (ReadReg(carVa, CLK_RST_CONTROLLER_RST_DEVICES) & SWR_SOC_THERM_RST) || !(ReadReg(carVa, CLK_RST_CONTROLLER_CLK_OUT_ENB) & CLK_ENB_SOC_THERM);
|
||||
}
|
||||
|
||||
bool IsSensorEnabled() {
|
||||
return ReadReg(socthermVa, TSENSOR_TSENSOR_CLKEN);
|
||||
}
|
||||
|
||||
void EnableSensor(const TSensor *sensor, u32 sensorIdx) {
|
||||
u32 val = sensor->config->tall << SENSOR_CONFIG0_TALL_SHIFT;
|
||||
WriteReg(socthermVa, sensor->base + SENSOR_CONFIG0, val);
|
||||
|
||||
val = (sensor->config->tsample - 1) << SENSOR_CONFIG1_TSAMPLE_SHIFT;
|
||||
val |= sensor->config->tiddq_en << SENSOR_CONFIG1_TIDDQ_EN_SHIFT;
|
||||
val |= sensor->config->ten_count << SENSOR_CONFIG1_TEN_COUNT_SHIFT;
|
||||
val |= SENSOR_CONFIG1_TEMP_ENABLE;
|
||||
WriteReg(socthermVa, sensor->base + SENSOR_CONFIG1, val);
|
||||
WriteReg(socthermVa, sensor->base + SENSOR_CONFIG2, calib[sensorIdx]);
|
||||
}
|
||||
|
||||
s32 TranslateTemp(u16 val) {
|
||||
s32 t;
|
||||
|
||||
t = ((val & READBACK_VALUE_MASK) >> READBACK_VALUE_SHIFT) * 1000;
|
||||
if (val & READBACK_ADD_HALF) {
|
||||
t += 500;
|
||||
}
|
||||
|
||||
if (val & READBACK_NEGATE) {
|
||||
t *= -1;
|
||||
}
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
void StartSensors() {
|
||||
u32 pdiv, hotspot;
|
||||
|
||||
if (isMariko) {
|
||||
for (u32 i = 0; i < std::size(marikoTSensors); ++i) {
|
||||
EnableSensor(&marikoTSensors[i], i);
|
||||
}
|
||||
|
||||
pdiv = (ReadReg(socthermVa, SENSOR_PDIV) & PDIV_MASK_T210B0) | PDIV_RATE_T210B0;
|
||||
hotspot = (ReadReg(socthermVa, SENSOR_HOTSPOT_OFF) & HOTSPOT_MASK_T210B0) | HOTSPOT_VAL;
|
||||
} else {
|
||||
for (u32 i = 0; i < std::size(eristaTSensors); ++i) {
|
||||
EnableSensor(&eristaTSensors[i], i);
|
||||
}
|
||||
|
||||
pdiv = (ReadReg(socthermVa, SENSOR_PDIV) & PDIV_MASK_T210) | PDIV_RATE_T210;
|
||||
hotspot = (ReadReg(socthermVa, SENSOR_HOTSPOT_OFF) & HOTSPOT_MASK_T210) | HOTSPOT_VAL;
|
||||
|
||||
EnableSensor(&eristaTSensors[SocthermTSensor_MEM0], SocthermTSensor_MEM0);
|
||||
EnableSensor(&eristaTSensors[SocthermTSensor_MEM1], SocthermTSensor_MEM1);
|
||||
}
|
||||
|
||||
WriteReg(socthermVa, SENSOR_PDIV, pdiv);
|
||||
WriteReg(socthermVa, SENSOR_HOTSPOT_OFF, hotspot);
|
||||
WriteReg(socthermVa, TSENSOR_TSENSOR_CLKEN, TSENSOR_TSENSOR_ENABLE);
|
||||
}
|
||||
|
||||
void ReadSensors(TSensorTemps &temps) {
|
||||
if (IsDisabledThroughSleep()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!IsSensorEnabled()) {
|
||||
StartSensors();
|
||||
}
|
||||
|
||||
temps.cpu = TranslateTemp(ReadReg(socthermVa, SENSOR_TEMP1) >> 16);
|
||||
temps.gpu = TranslateTemp(ReadReg(socthermVa, SENSOR_TEMP1) & SENSOR_TEMP1_GPU_TEMP_MASK);
|
||||
temps.pllx = TranslateTemp(ReadReg(socthermVa, SENSOR_TEMP2) & SENSOR_TEMP2_PLLX_TEMP_MASK);
|
||||
|
||||
if (board::GetSocType() == SysClkSocType_Erista) {
|
||||
temps.mem = TranslateTemp(ReadReg(socthermVa, SENSOR_TEMP2) >> 16);
|
||||
} else {
|
||||
temps.mem = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void CalcSharedCal(const TSensorFuse *tfuse, TSensorSharedCalib *shared, u64 fuseVa) {
|
||||
s32 shifted_cp, shifted_ft;
|
||||
|
||||
u32 val = ReadReg(fuseVa, FUSE_TSENSOR_COMMON);
|
||||
|
||||
shared->base_cp = (val & tfuse->fuse_base_cp_mask) >> tfuse->fuse_base_cp_shift;
|
||||
shared->base_ft = (val & tfuse->fuse_base_ft_mask) >> tfuse->fuse_base_ft_shift;
|
||||
|
||||
shifted_ft = (val & tfuse->fuse_shift_ft_mask) >> tfuse->fuse_shift_ft_shift;
|
||||
shifted_ft = sign_extend32(shifted_ft, 4);
|
||||
|
||||
if (tfuse->fuse_spare_realignment) {
|
||||
val = ReadReg(fuseVa, tfuse->fuse_spare_realignment + FUSE_CACHE_OFFSET);
|
||||
}
|
||||
|
||||
shifted_cp = sign_extend32(val, 5);
|
||||
|
||||
shared->actual_temp_cp = 2 * NOMINAL_CALIB_CP + shifted_cp;
|
||||
shared->actual_temp_ft = 2 * NOMINAL_CALIB_FT + shifted_ft;
|
||||
}
|
||||
|
||||
void CalcTSensorCalib(const TSensorConfig *cfg, TSensorSharedCalib *shared, const FuseCorrCoeff *corr, u32 *calibration, u32 offset, u64 fuseVa) {
|
||||
u32 val, calib;
|
||||
s32 actual_tsensor_ft, actual_tsensor_cp;
|
||||
s32 delta_sens, delta_temp;
|
||||
s32 mult, div;
|
||||
s16 therma, thermb;
|
||||
s64 temp;
|
||||
|
||||
val = ReadReg(fuseVa, offset + FUSE_CACHE_OFFSET);
|
||||
|
||||
actual_tsensor_cp = (shared->base_cp * 64) + sign_extend32(val, 12);
|
||||
val = (val & FUSE_TSENSOR_CALIB_FT_TS_BASE_MASK) >> FUSE_TSENSOR_CALIB_FT_TS_BASE_SHIFT;
|
||||
actual_tsensor_ft = (shared->base_ft * 32) + sign_extend32(val, 12);
|
||||
|
||||
delta_sens = actual_tsensor_ft - actual_tsensor_cp;
|
||||
delta_temp = shared->actual_temp_ft - shared->actual_temp_cp;
|
||||
|
||||
mult = cfg->pdiv * cfg->tsample_ate;
|
||||
div = cfg->tsample * cfg->pdiv_ate;
|
||||
|
||||
temp = (s64)delta_temp * (1LL << 13) * mult;
|
||||
therma = div64_s64_precise(temp, (s64)delta_sens * div);
|
||||
|
||||
temp = ((s64)actual_tsensor_ft * shared->actual_temp_cp) - ((s64)actual_tsensor_cp * shared->actual_temp_ft);
|
||||
thermb = div64_s64_precise(temp, delta_sens);
|
||||
|
||||
temp = (s64)therma * corr->alpha;
|
||||
therma = div64_s64_precise(temp, CALIB_COEFFICIENT);
|
||||
|
||||
temp = (s64)thermb * corr->alpha + corr->beta;
|
||||
thermb = div64_s64_precise(temp, CALIB_COEFFICIENT);
|
||||
|
||||
calib = ((u16)therma << SENSOR_CONFIG2_THERMA_SHIFT) | ((u16)thermb << SENSOR_CONFIG2_THERMB_SHIFT);
|
||||
|
||||
*calibration = calib;
|
||||
}
|
||||
|
||||
void Initialize() {
|
||||
isMariko = board::GetSocType() == SysClkSocType_Mariko;
|
||||
|
||||
constexpr u64 SocthermPa = 0x700E2000, FusePa = 0x7000F000, CarPa = 0x60006000;
|
||||
R_UNLESS(MapAddress(socthermVa, SocthermPa, "soctherm"));
|
||||
R_UNLESS(MapAddress( fuseVa, FusePa, "fuse"));
|
||||
R_UNLESS(MapAddress( carVa, CarPa, "car"));
|
||||
|
||||
WriteReg(carVa, CAR_CLK_SOURCE_TSENSOR, CAR_CLK_SOURCE_TSENSOR_VAL);
|
||||
SetBits(carVa, CAR_CLK_OUT_ENB_V, 0x10);
|
||||
svcSleepThread(2000);
|
||||
|
||||
TSensorSharedCalib sharedCal = {};
|
||||
CalcSharedCal(&tfuse, &sharedCal, fuseVa);
|
||||
|
||||
if (isMariko) {
|
||||
for (u32 i = 0; i < std::size(marikoTSensors); ++i) {
|
||||
CalcTSensorCalib(marikoTSensors[i].config, &sharedCal, &marikoTSensors[i].fuse_corr, &calib[i], marikoTSensors[i].calib_fuse_offset, fuseVa);
|
||||
}
|
||||
} else {
|
||||
for (u32 i = 0; i < std::size(eristaTSensors); ++i) {
|
||||
CalcTSensorCalib(eristaTSensors[i].config, &sharedCal, &eristaTSensors[i].fuse_corr, &calib[i], eristaTSensors[i].calib_fuse_offset, fuseVa);
|
||||
}
|
||||
}
|
||||
|
||||
StartSensors();
|
||||
|
||||
fileUtils::LogLine("[Soctherm] Finished init.");
|
||||
}
|
||||
|
||||
}
|
||||
135
Source/hoc-clk/sysmodule/src/soctherm.hpp
Normal file
135
Source/hoc-clk/sysmodule/src/soctherm.hpp
Normal file
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2019, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Author:
|
||||
* Mikko Perttunen <mperttunen@nvidia.com>
|
||||
*
|
||||
* 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 soctherm {
|
||||
|
||||
#define R_UNLESS(rc) \
|
||||
do { \
|
||||
if (R_FAILED(rc)) { \
|
||||
return; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
struct TSensorConfig {
|
||||
u32 tall;
|
||||
u32 tiddq_en;
|
||||
u32 ten_count;
|
||||
u32 pdiv;
|
||||
u32 pdiv_ate;
|
||||
u32 tsample;
|
||||
u32 tsample_ate;
|
||||
};
|
||||
|
||||
struct FuseCorrCoeff {
|
||||
s32 alpha;
|
||||
s32 beta;
|
||||
};
|
||||
|
||||
struct TSensorGroup {
|
||||
const char *name;
|
||||
u8 id;
|
||||
u16 sensor_temp_offset;
|
||||
u32 sensor_temp_mask;
|
||||
u32 pdiv_mask;
|
||||
u32 pllx_hotspot_diff;
|
||||
u32 pllx_hotspot_mask;
|
||||
u32 hw_pllx_offset_mask;
|
||||
u32 hw_pllx_offset_en_mask;
|
||||
u32 thermtrip_enable_mask;
|
||||
u32 thermtrip_any_en_mask;
|
||||
u32 thermtrip_threshold_mask;
|
||||
u16 thermctl_lvl0_offset;
|
||||
u32 thermctl_isr_mask;
|
||||
u32 thermctl_lvl0_up_thresh_mask;
|
||||
u32 thermctl_lvl0_dn_thresh_mask;
|
||||
};
|
||||
|
||||
struct TSensorGroupOffsets {
|
||||
u32 max;
|
||||
u32 min;
|
||||
u32 hw_offsetting_en;
|
||||
const TSensorGroup *ttg;
|
||||
};
|
||||
|
||||
struct TSensor {
|
||||
const char *name;
|
||||
const u32 base;
|
||||
const TSensorConfig *config;
|
||||
const u32 calib_fuse_offset;
|
||||
const FuseCorrCoeff fuse_corr;
|
||||
const TSensorGroup *group;
|
||||
};
|
||||
|
||||
struct TSensorFuse {
|
||||
u32 fuse_base_cp_mask;
|
||||
u32 fuse_base_cp_shift;
|
||||
u32 fuse_base_ft_mask;
|
||||
u32 fuse_base_ft_shift;
|
||||
u32 fuse_shift_ft_mask;
|
||||
u32 fuse_shift_ft_shift;
|
||||
u32 fuse_spare_realignment;
|
||||
};
|
||||
|
||||
struct TSensorSharedCalib {
|
||||
u32 base_cp;
|
||||
u32 base_ft;
|
||||
u32 actual_temp_cp;
|
||||
u32 actual_temp_ft;
|
||||
};
|
||||
|
||||
enum SocthermTSensor : u32 {
|
||||
SocthermTSensor_CPU0 = 0,
|
||||
SocthermTSensor_CPU1 = 1,
|
||||
SocthermTSensor_CPU2 = 2,
|
||||
SocthermTSensor_CPU3 = 3,
|
||||
SocthermTSensor_GPU = 4,
|
||||
SocthermTSensor_PLLX = 5,
|
||||
SocthermTSensor_MEM0 = 6,
|
||||
SocthermTSensor_MEM1 = 7,
|
||||
SocthermTSensor_EnumMax = 8,
|
||||
};
|
||||
|
||||
struct TSensorTemps {
|
||||
s32 cpu;
|
||||
s32 gpu;
|
||||
s32 mem;
|
||||
s32 pllx;
|
||||
};
|
||||
|
||||
void Initialize();
|
||||
void ReadSensors(TSensorTemps &temps);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user