Skip to content

Commit c3559df

Browse files
committed
Fixes potential cause of freezes after sleep
1 parent 7f7caf0 commit c3559df

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

AMDRyzenCPUPowerManagement/AMDRyzenCPUPowerManagement.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ bool AMDRyzenCPUPowerManagement::init(OSDictionary *dictionary){
3939
IOLog("AMDCPUSupport v%s, init\n", xStringify(MODULE_VERSION));
4040

4141
IOLog("AMDCPUSupport::enter dlinking..\n");
42-
42+
4343
pmRyzen_symtable_ready = 0;
44-
44+
4545
retry:
4646
find_mach_header_addr(getKernelVersion() >= KernelVersion::BigSur);
4747
pmRyzen_symtable._wrmsr_carefully = lookup_symbol("_wrmsr_carefully");
48-
48+
4949
if(!pmRyzen_symtable._wrmsr_carefully){
5050
kextloadAlerts++;
5151
IOSleep(2);
5252
goto retry;
5353
}
54-
55-
54+
55+
5656
pmRyzen_symtable._KUNCUserNotificationDisplayAlert = lookup_symbol("_KUNCUserNotificationDisplayAlert");
5757
pmRyzen_symtable._cpu_to_processor = lookup_symbol("_cpu_to_processor");
5858
pmRyzen_symtable._tscFreq = lookup_symbol("_tscFreq");
@@ -320,11 +320,11 @@ bool AMDRyzenCPUPowerManagement::start(IOService *provider){
320320
IOLog("AMDCPUSupport::start no PCI support found, failing...\n");
321321
return false;
322322
}
323-
323+
324324
// while (!pmRyzen_symtable_ready) {
325325
// IOSleep(200);
326326
// }
327-
327+
328328
void *safe_wrmsr = pmRyzen_symtable._wrmsr_carefully;
329329
if(!safe_wrmsr){
330330
IOLog("AMDCPUSupport::start WARN: Can't find _wrmsr_carefully, proceeding with unsafe wrmsr\n");
@@ -392,15 +392,15 @@ IOReturn AMDRyzenCPUPowerManagement::setPowerState(unsigned long powerStateOrdin
392392
IOLog("AMDCPUSupport::setPowerState preparing for sleep\n");
393393
sleepState.sleep = true;
394394
sleepState.cpb = getCPBState();
395-
dumpPstate(sleepState.pstate);
395+
//dumpPstate(sleepState.pstate);
396396

397397
stopWorkLoop();
398398
} else if (1 == powerStateOrdinal && sleepState.sleep) {
399399
// Waking up
400400
IOLog("AMDCPUSupport::setPowerState preparing for wakeup\n");
401401
sleepState.sleep = false;
402402
setCPBState(sleepState.cpb);
403-
writePstate(sleepState.pstate);
403+
//writePstate(sleepState.pstate);
404404
startWorkLoop();
405405
}
406406

@@ -515,18 +515,20 @@ void AMDRyzenCPUPowerManagement::calculateEffectiveFrequency(uint8_t physical){
515515
//If an overflow of either the MPERF or APERF register occurs between read of last MPERF and
516516
//read of last APERF, the effective frequency calculated in is invalid.
517517
if(APERF <= lastAPERF || MPERF <= lastMPERF) {
518-
// IOLog("AMDCPUSupport::calculateEffectiveFrequency: frequency is invalid!!!");
518+
IOLog("AMDCPUSupport::calculateEffectiveFrequency: frequency is invalid for %u", physical);
519+
lastAPERF_PerCore[physical] = APERF;
520+
lastMPERF_PerCore[physical] = MPERF;
519521
return;
520522
}
521523

522524
float freqP0 = PStateDefClock_perCore[0];
523525

524526
uint64_t deltaAPERF = APERF - lastAPERF;
525-
float effFreq = ((float)deltaAPERF / (float)(MPERF - lastMPERF)) * freqP0;
526-
527-
effFreq_perCore[physical] = effFreq;
528-
529-
527+
uint64_t deltaMPERF = MPERF - lastMPERF;
528+
if (deltaMPERF != 0) {
529+
float effFreq = ((float)deltaAPERF / (float)(deltaMPERF)) * freqP0;
530+
effFreq_perCore[physical] = effFreq;
531+
}
530532
}
531533

532534
void AMDRyzenCPUPowerManagement::updateInstructionDelta(uint8_t cpu_num){
@@ -724,7 +726,7 @@ EXPORT extern "C" kern_return_t amdryzencpupm_kern_start(kmod_info_t *, void *)
724726
// This works better and increases boot speed in some cases.
725727
PE_parse_boot_argn("liludelay", &ADDPR(debugPrintDelay), sizeof(ADDPR(debugPrintDelay)));
726728
ADDPR(debugEnabled) = checkKernelArgument("-amdpdbg");
727-
729+
728730
// IOLog("AMDCPUSupport::enter dlinking..\n");
729731
//
730732
// pmRyzen_symtable_ready = 0;
@@ -740,7 +742,7 @@ EXPORT extern "C" kern_return_t amdryzencpupm_kern_start(kmod_info_t *, void *)
740742
// pmRyzen_symtable._i386_cpu_IPI = lookup_symbol("_i386_cpu_IPI");
741743
// pmRyzen_symtable_ready = 1;
742744
// IOLog("AMDCPUSupport::enter link finished.\n");
743-
745+
744746
return KERN_SUCCESS;
745747
}
746748

0 commit comments

Comments
 (0)