From f358899140522ad640766a1bccf18e1d22d542cb Mon Sep 17 00:00:00 2001 From: Aluveitie Date: Tue, 26 May 2020 15:38:43 +0200 Subject: [PATCH] Fixes potential cause of freezes after sleep --- .../AMDRyzenCPUPowerManagement.cpp | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/AMDRyzenCPUPowerManagement/AMDRyzenCPUPowerManagement.cpp b/AMDRyzenCPUPowerManagement/AMDRyzenCPUPowerManagement.cpp index 7674034..94a013f 100644 --- a/AMDRyzenCPUPowerManagement/AMDRyzenCPUPowerManagement.cpp +++ b/AMDRyzenCPUPowerManagement/AMDRyzenCPUPowerManagement.cpp @@ -39,9 +39,9 @@ bool AMDRyzenCPUPowerManagement::init(OSDictionary *dictionary){ IOLog("AMDCPUSupport v%s, init\n", xStringify(MODULE_VERSION)); IOLog("AMDCPUSupport::enter dlinking..\n"); - + pmRyzen_symtable_ready = 0; - + pmRyzen_symtable._wrmsr_carefully = lookup_symbol("_wrmsr_carefully"); pmRyzen_symtable._KUNCUserNotificationDisplayAlert = lookup_symbol("_KUNCUserNotificationDisplayAlert"); pmRyzen_symtable._cpu_to_processor = lookup_symbol("_cpu_to_processor"); @@ -309,11 +309,11 @@ bool AMDRyzenCPUPowerManagement::start(IOService *provider){ IOLog("AMDCPUSupport::start no PCI support found, failing...\n"); return false; } - + // while (!pmRyzen_symtable_ready) { // IOSleep(200); // } - + void *safe_wrmsr = pmRyzen_symtable._wrmsr_carefully; if(!safe_wrmsr){ IOLog("AMDCPUSupport::start WARN: Can't find _wrmsr_carefully, proceeding with unsafe wrmsr\n"); @@ -381,7 +381,7 @@ IOReturn AMDRyzenCPUPowerManagement::setPowerState(unsigned long powerStateOrdin IOLog("AMDCPUSupport::setPowerState preparing for sleep\n"); sleepState.sleep = true; sleepState.cpb = getCPBState(); - dumpPstate(sleepState.pstate); + //dumpPstate(sleepState.pstate); stopWorkLoop(); } else if (1 == powerStateOrdinal && sleepState.sleep) { @@ -389,7 +389,7 @@ IOReturn AMDRyzenCPUPowerManagement::setPowerState(unsigned long powerStateOrdin IOLog("AMDCPUSupport::setPowerState preparing for wakeup\n"); sleepState.sleep = false; setCPBState(sleepState.cpb); - writePstate(sleepState.pstate); + //writePstate(sleepState.pstate); startWorkLoop(); } @@ -502,18 +502,22 @@ void AMDRyzenCPUPowerManagement::calculateEffectiveFrequency(uint8_t physical){ //If an overflow of either the MPERF or APERF register occurs between read of last MPERF and //read of last APERF, the effective frequency calculated in is invalid. if(APERF <= lastAPERF || MPERF <= lastMPERF) { -// IOLog("AMDCPUSupport::calculateEffectiveFrequency: frequency is invalid!!!"); + IOLog("AMDCPUSupport::calculateEffectiveFrequency: frequency is invalid for %u", physical); + lastAPERF_PerCore[physical] = APERF; + lastMPERF_PerCore[physical] = MPERF; return; } float freqP0 = PStateDefClock_perCore[0]; uint64_t deltaAPERF = APERF - lastAPERF; + uint64_t deltaMPERF = MPERF - lastMPERF; deltaAPERF_PerCore[physical] = deltaAPERF; - deltaMPERF_PerCore[physical] = MPERF - lastMPERF; - float effFreq = ((float)deltaAPERF / (float)(MPERF - lastMPERF)) * freqP0; - - effFreq_perCore[physical] = effFreq; + deltaMPERF_PerCore[physical] = deltaMPERF; + if (deltaMPERF != 0) { + float effFreq = ((float)deltaAPERF / (float)(deltaMPERF)) * freqP0; + effFreq_perCore[physical] = effFreq; + } lastAPERF_PerCore[physical] = APERF; lastMPERF_PerCore[physical] = MPERF; @@ -714,7 +718,7 @@ EXPORT extern "C" kern_return_t amdryzencpupm_kern_start(kmod_info_t *, void *) // This works better and increases boot speed in some cases. PE_parse_boot_argn("liludelay", &ADDPR(debugPrintDelay), sizeof(ADDPR(debugPrintDelay))); ADDPR(debugEnabled) = checkKernelArgument("-amdpdbg"); - + // IOLog("AMDCPUSupport::enter dlinking..\n"); // // pmRyzen_symtable_ready = 0; @@ -730,7 +734,7 @@ EXPORT extern "C" kern_return_t amdryzencpupm_kern_start(kmod_info_t *, void *) // pmRyzen_symtable._i386_cpu_IPI = lookup_symbol("_i386_cpu_IPI"); // pmRyzen_symtable_ready = 1; // IOLog("AMDCPUSupport::enter link finished.\n"); - + return KERN_SUCCESS; }