From 14683405be233d4860d85f7d7db6da7e9385a0f9 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 24 Apr 2019 16:36:35 -0700 Subject: [PATCH] fatal: update to use clkrst api on 8.0.0+ --- stratosphere/fatal/source/fatal_main.cpp | 19 +++++++-- .../fatal/source/fatal_task_clock.cpp | 39 +++++++++++++++---- .../fatal/source/fatal_task_clock.hpp | 1 + 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/stratosphere/fatal/source/fatal_main.cpp b/stratosphere/fatal/source/fatal_main.cpp index 4600b951d..22c1ac6e2 100644 --- a/stratosphere/fatal/source/fatal_main.cpp +++ b/stratosphere/fatal/source/fatal_main.cpp @@ -102,9 +102,16 @@ void __appInit(void) { std::abort(); } - rc = pcvInitialize(); - if (R_FAILED(rc)) { - std::abort(); + if (GetRuntimeFirmwareVersion() >= FirmwareVersion_800) { + rc = clkrstInitialize(); + if (R_FAILED(rc)) { + std::abort(); + } + } else { + rc = pcvInitialize(); + if (R_FAILED(rc)) { + std::abort(); + } } rc = lblInitialize(); @@ -155,7 +162,11 @@ void __appExit(void) { spsmExit(); psmExit(); lblExit(); - pcvExit(); + if (GetRuntimeFirmwareVersion() >= FirmwareVersion_800) { + clkrstExit(); + } else { + pcvExit(); + } bpcExit(); i2cExit(); pminfoExit(); diff --git a/stratosphere/fatal/source/fatal_task_clock.cpp b/stratosphere/fatal/source/fatal_task_clock.cpp index 3bd1d296c..6fe9a849f 100644 --- a/stratosphere/fatal/source/fatal_task_clock.cpp +++ b/stratosphere/fatal/source/fatal_task_clock.cpp @@ -17,6 +17,31 @@ #include #include "fatal_task_clock.hpp" +Result AdjustClockTask::AdjustClockForModule(PcvModule module, u32 hz) { + Result rc; + + if (GetRuntimeFirmwareVersion() >= FirmwareVersion_800) { + /* On 8.0.0+, convert to module id + use clkrst API. */ + PcvModuleId module_id; + if (R_FAILED((rc = pcvGetModuleId(&module_id, module)))) { + return rc; + } + + ClkrstSession session; + Result rc = clkrstOpenSession(&session, module_id, 3); + if (R_FAILED(rc)) { + return rc; + } + ON_SCOPE_EXIT { clkrstCloseSession(&session); }; + + rc = clkrstSetClockRate(&session, hz); + } else { + /* On 1.0.0-7.0.1, use pcv API. */ + rc = pcvSetClockRate(module, hz); + } + + return rc; +} Result AdjustClockTask::AdjustClock() { /* Fatal sets the CPU to 1020MHz, the GPU to 307 MHz, and the EMC to 1331MHz. */ @@ -24,19 +49,19 @@ Result AdjustClockTask::AdjustClock() { constexpr u32 GPU_CLOCK_307MHZ = 0x124F8000L; constexpr u32 EMC_CLOCK_1331MHZ = 0x4F588000L; Result rc = ResultSuccess; - - if (R_FAILED((rc = pcvSetClockRate(PcvModule_Cpu, CPU_CLOCK_1020MHZ)))) { + + if (R_FAILED((rc = AdjustClockForModule(PcvModule_CpuBus, CPU_CLOCK_1020MHZ)))) { return rc; } - - if (R_FAILED((rc = pcvSetClockRate(PcvModule_Gpu, GPU_CLOCK_307MHZ)))) { + + if (R_FAILED((rc = AdjustClockForModule(PcvModule_GPU, GPU_CLOCK_307MHZ)))) { return rc; } - - if (R_FAILED((rc = pcvSetClockRate(PcvModule_Emc, EMC_CLOCK_1331MHZ)))) { + + if (R_FAILED((rc = AdjustClockForModule(PcvModule_EMC, EMC_CLOCK_1331MHZ)))) { return rc; } - + return rc; } diff --git a/stratosphere/fatal/source/fatal_task_clock.hpp b/stratosphere/fatal/source/fatal_task_clock.hpp index 9bbbe4d82..942196cad 100644 --- a/stratosphere/fatal/source/fatal_task_clock.hpp +++ b/stratosphere/fatal/source/fatal_task_clock.hpp @@ -21,6 +21,7 @@ class AdjustClockTask : public IFatalTask { private: + Result AdjustClockForModule(PcvModule module, u32 hz); Result AdjustClock(); public: AdjustClockTask(FatalThrowContext *ctx, u64 title_id) : IFatalTask(ctx, title_id) { }