From b13d1ca5d3e8a52bfdfee72e277a85d0922a536b Mon Sep 17 00:00:00 2001 From: preflight-f Date: Thu, 13 Jan 2022 18:51:08 +0800 Subject: [PATCH 1/6] [Experimental] Ice Lake backlight fix. --- WhateverGreen/kern_igfx_backlight.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/WhateverGreen/kern_igfx_backlight.cpp b/WhateverGreen/kern_igfx_backlight.cpp index 9e3d695e..5615648e 100644 --- a/WhateverGreen/kern_igfx_backlight.cpp +++ b/WhateverGreen/kern_igfx_backlight.cpp @@ -184,6 +184,18 @@ void IGFX::BacklightRegistersFix::wrapCFLWriteRegisterPWMDuty1(void *controller, DBGLOG("igfx", "BLR: [CFL+] WriteRegister32: Called with register 0x%x and value 0x%x.", reg, value); PANIC_COND(reg != BXT_BLC_PWM_DUTY1, "igfx", "Fatal Error: Register should be BXT_BLC_PWM_DUTY1."); + // [Experimental] Ice Lake backlight fix. + // TODO: Excluding CFL (Is it really necessary?) + + if (value && callbackIGFX->modBacklightRegistersFix.driverBacklightFrequency == 0) { + uint32_t registerValue = callbackIGFX->readRegister32(controller, 0xC2014); + + uint32_t freq = 17777; + if (registerValue & (1 << 8)) freq = 22222; + + wrapCFLWriteRegisterPWMFreq1(controller, BXT_BLC_PWM_FREQ1, freq); + } + if (callbackIGFX->modBacklightRegistersFix.driverBacklightFrequency && callbackIGFX->modBacklightRegistersFix.targetBacklightFrequency) { // Translate the PWM duty cycle between the driver scale value and the HW scale value uint32_t rescaledValue = static_cast((value * static_cast(callbackIGFX->modBacklightRegistersFix.targetBacklightFrequency)) / static_cast(callbackIGFX->modBacklightRegistersFix.driverBacklightFrequency)); From 25f1ea55a75fd2ddf9cb4518e99b0ff74cf8d010 Mon Sep 17 00:00:00 2001 From: preflight-f Date: Fri, 14 Jan 2022 10:56:34 +0800 Subject: [PATCH 2/6] [#97] Some updates for the backlight fix. --- WhateverGreen/kern_igfx_backlight.cpp | 12 +++++------- WhateverGreen/kern_igfx_backlight.hpp | 10 ++++++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/WhateverGreen/kern_igfx_backlight.cpp b/WhateverGreen/kern_igfx_backlight.cpp index 5615648e..13fb0b83 100644 --- a/WhateverGreen/kern_igfx_backlight.cpp +++ b/WhateverGreen/kern_igfx_backlight.cpp @@ -185,15 +185,13 @@ void IGFX::BacklightRegistersFix::wrapCFLWriteRegisterPWMDuty1(void *controller, PANIC_COND(reg != BXT_BLC_PWM_DUTY1, "igfx", "Fatal Error: Register should be BXT_BLC_PWM_DUTY1."); // [Experimental] Ice Lake backlight fix. - // TODO: Excluding CFL (Is it really necessary?) if (value && callbackIGFX->modBacklightRegistersFix.driverBacklightFrequency == 0) { - uint32_t registerValue = callbackIGFX->readRegister32(controller, 0xC2014); - - uint32_t freq = 17777; - if (registerValue & (1 << 8)) freq = 22222; - - wrapCFLWriteRegisterPWMFreq1(controller, BXT_BLC_PWM_FREQ1, freq); + SYSLOG("igfx", "BLR: [CFL+] WriteRegister32: Ice Lake backlight fix was entered."); + uint32_t registerValue = callbackIGFX->readRegister32(controller, SFUSE_STRAP); + uint32_t selectedFreq = ICL_FREQ_NORMAL; + if (registerValue & SFUSE_STRAP_RAW_FREQUENCY) selectedFreq = ICL_FREQ_RAW; + wrapCFLWriteRegisterPWMFreq1(controller, BXT_BLC_PWM_FREQ1, selectedFreq); } if (callbackIGFX->modBacklightRegistersFix.driverBacklightFrequency && callbackIGFX->modBacklightRegistersFix.targetBacklightFrequency) { diff --git a/WhateverGreen/kern_igfx_backlight.hpp b/WhateverGreen/kern_igfx_backlight.hpp index 6d5aa77d..5ba8ae92 100644 --- a/WhateverGreen/kern_igfx_backlight.hpp +++ b/WhateverGreen/kern_igfx_backlight.hpp @@ -20,6 +20,16 @@ static constexpr uint32_t BXT_BLC_PWM_CTL1 = 0xC8250; static constexpr uint32_t BXT_BLC_PWM_FREQ1 = 0xC8254; static constexpr uint32_t BXT_BLC_PWM_DUTY1 = 0xC8258; +static constexpr uint32_t SFUSE_STRAP = 0xC2014; +static constexpr uint32_t SFUSE_STRAP_RAW_FREQUENCY = 1 << 8; + +/** + * Ice Lake freq + * Copied from `AppleIntelFramebufferController::start()` function + */ +static constexpr uint32_t ICL_FREQ_NORMAL = 17777; +static constexpr uint32_t ICL_FREQ_RAW = 22222; + /** * Represents a single brightness adjustment request */ From 26ceccb1c0e5cdfac1d6f36f8800ced0721ec79a Mon Sep 17 00:00:00 2001 From: preflight-f Date: Fri, 14 Jan 2022 14:42:30 +0800 Subject: [PATCH 3/6] [#97] Introduce startup argument `-igfxblrexpr`. --- WhateverGreen/kern_igfx.hpp | 5 +++++ WhateverGreen/kern_igfx_backlight.cpp | 12 ++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/WhateverGreen/kern_igfx.hpp b/WhateverGreen/kern_igfx.hpp index 2356d617..01b04b0b 100644 --- a/WhateverGreen/kern_igfx.hpp +++ b/WhateverGreen/kern_igfx.hpp @@ -488,6 +488,11 @@ class IGFX { */ bool enabled {false}; + /** + * Set to `true` if this submodule requires additional fixes for CFL+ + */ + bool requiresCFLAdditionalFix {false}; + /** * Set to `true` if this submodule requires patching the framebuffer driver */ diff --git a/WhateverGreen/kern_igfx_backlight.cpp b/WhateverGreen/kern_igfx_backlight.cpp index 13fb0b83..9c18f698 100644 --- a/WhateverGreen/kern_igfx_backlight.cpp +++ b/WhateverGreen/kern_igfx_backlight.cpp @@ -40,6 +40,8 @@ void IGFX::BacklightRegistersFix::processKernel(KernelPatcher &patcher, DeviceIn if (WIOKit::getOSDataValue(info->videoBuiltin, "max-backlight-freq", targetBacklightFrequency)) DBGLOG("igfx", "BLR: Will use the custom backlight frequency %u.", targetBacklightFrequency); + + requiresCFLAdditionalFix = checkKernelArgument("-igfxblrexpr"); } void IGFX::BacklightRegistersFix::processFramebufferKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) { @@ -184,13 +186,11 @@ void IGFX::BacklightRegistersFix::wrapCFLWriteRegisterPWMDuty1(void *controller, DBGLOG("igfx", "BLR: [CFL+] WriteRegister32: Called with register 0x%x and value 0x%x.", reg, value); PANIC_COND(reg != BXT_BLC_PWM_DUTY1, "igfx", "Fatal Error: Register should be BXT_BLC_PWM_DUTY1."); - // [Experimental] Ice Lake backlight fix. - - if (value && callbackIGFX->modBacklightRegistersFix.driverBacklightFrequency == 0) { - SYSLOG("igfx", "BLR: [CFL+] WriteRegister32: Ice Lake backlight fix was entered."); + if (value && callbackIGFX->modBacklightRegistersFix.driverBacklightFrequency == 0 && callbackIGFX->modBacklightRegistersFix.requiresCFLAdditionalFix) { + // CFL+ backlight additional fix. + DBGLOG("igfx", "BLR: [CFL+] WriteRegister32: Ice Lake backlight fix was entered."); uint32_t registerValue = callbackIGFX->readRegister32(controller, SFUSE_STRAP); - uint32_t selectedFreq = ICL_FREQ_NORMAL; - if (registerValue & SFUSE_STRAP_RAW_FREQUENCY) selectedFreq = ICL_FREQ_RAW; + uint32_t selectedFreq = (registerValue & SFUSE_STRAP_RAW_FREQUENCY) ? ICL_FREQ_RAW : ICL_FREQ_NORMAL; wrapCFLWriteRegisterPWMFreq1(controller, BXT_BLC_PWM_FREQ1, selectedFreq); } From f6cac4d4ec1c8885a677955aefa0088875eba92c Mon Sep 17 00:00:00 2001 From: preflight-f Date: Fri, 14 Jan 2022 15:27:46 +0800 Subject: [PATCH 4/6] [#97] Some updates. --- WhateverGreen/kern_igfx_backlight.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WhateverGreen/kern_igfx_backlight.cpp b/WhateverGreen/kern_igfx_backlight.cpp index 9c18f698..578415d3 100644 --- a/WhateverGreen/kern_igfx_backlight.cpp +++ b/WhateverGreen/kern_igfx_backlight.cpp @@ -186,7 +186,7 @@ void IGFX::BacklightRegistersFix::wrapCFLWriteRegisterPWMDuty1(void *controller, DBGLOG("igfx", "BLR: [CFL+] WriteRegister32: Called with register 0x%x and value 0x%x.", reg, value); PANIC_COND(reg != BXT_BLC_PWM_DUTY1, "igfx", "Fatal Error: Register should be BXT_BLC_PWM_DUTY1."); - if (value && callbackIGFX->modBacklightRegistersFix.driverBacklightFrequency == 0 && callbackIGFX->modBacklightRegistersFix.requiresCFLAdditionalFix) { + if (callbackIGFX->modBacklightRegistersFix.requiresCFLAdditionalFix && value && callbackIGFX->modBacklightRegistersFix.driverBacklightFrequency == 0) { // CFL+ backlight additional fix. DBGLOG("igfx", "BLR: [CFL+] WriteRegister32: Ice Lake backlight fix was entered."); uint32_t registerValue = callbackIGFX->readRegister32(controller, SFUSE_STRAP); From 86f7f09e03e2877a2e85782132a6cf3b6842087f Mon Sep 17 00:00:00 2001 From: preflight-f Date: Sat, 15 Jan 2022 10:45:35 +0800 Subject: [PATCH 5/6] [#97] Remove the startup argument `-igfxblrexpr`. --- WhateverGreen/kern_igfx.hpp | 5 ----- WhateverGreen/kern_igfx_backlight.cpp | 4 +--- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/WhateverGreen/kern_igfx.hpp b/WhateverGreen/kern_igfx.hpp index 01b04b0b..2356d617 100644 --- a/WhateverGreen/kern_igfx.hpp +++ b/WhateverGreen/kern_igfx.hpp @@ -488,11 +488,6 @@ class IGFX { */ bool enabled {false}; - /** - * Set to `true` if this submodule requires additional fixes for CFL+ - */ - bool requiresCFLAdditionalFix {false}; - /** * Set to `true` if this submodule requires patching the framebuffer driver */ diff --git a/WhateverGreen/kern_igfx_backlight.cpp b/WhateverGreen/kern_igfx_backlight.cpp index 578415d3..1238f12f 100644 --- a/WhateverGreen/kern_igfx_backlight.cpp +++ b/WhateverGreen/kern_igfx_backlight.cpp @@ -40,8 +40,6 @@ void IGFX::BacklightRegistersFix::processKernel(KernelPatcher &patcher, DeviceIn if (WIOKit::getOSDataValue(info->videoBuiltin, "max-backlight-freq", targetBacklightFrequency)) DBGLOG("igfx", "BLR: Will use the custom backlight frequency %u.", targetBacklightFrequency); - - requiresCFLAdditionalFix = checkKernelArgument("-igfxblrexpr"); } void IGFX::BacklightRegistersFix::processFramebufferKext(KernelPatcher &patcher, size_t index, mach_vm_address_t address, size_t size) { @@ -186,7 +184,7 @@ void IGFX::BacklightRegistersFix::wrapCFLWriteRegisterPWMDuty1(void *controller, DBGLOG("igfx", "BLR: [CFL+] WriteRegister32: Called with register 0x%x and value 0x%x.", reg, value); PANIC_COND(reg != BXT_BLC_PWM_DUTY1, "igfx", "Fatal Error: Register should be BXT_BLC_PWM_DUTY1."); - if (callbackIGFX->modBacklightRegistersFix.requiresCFLAdditionalFix && value && callbackIGFX->modBacklightRegistersFix.driverBacklightFrequency == 0) { + if (value && callbackIGFX->modBacklightRegistersFix.driverBacklightFrequency == 0) { // CFL+ backlight additional fix. DBGLOG("igfx", "BLR: [CFL+] WriteRegister32: Ice Lake backlight fix was entered."); uint32_t registerValue = callbackIGFX->readRegister32(controller, SFUSE_STRAP); From 6f4c3ecab37cd48f45dde83ae25eb99ce1684ce1 Mon Sep 17 00:00:00 2001 From: preflight-f Date: Sat, 15 Jan 2022 14:51:32 +0800 Subject: [PATCH 6/6] [#97] Changes for DBGLOG. --- WhateverGreen/kern_igfx_backlight.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WhateverGreen/kern_igfx_backlight.cpp b/WhateverGreen/kern_igfx_backlight.cpp index 1238f12f..2f96f1d8 100644 --- a/WhateverGreen/kern_igfx_backlight.cpp +++ b/WhateverGreen/kern_igfx_backlight.cpp @@ -186,7 +186,7 @@ void IGFX::BacklightRegistersFix::wrapCFLWriteRegisterPWMDuty1(void *controller, if (value && callbackIGFX->modBacklightRegistersFix.driverBacklightFrequency == 0) { // CFL+ backlight additional fix. - DBGLOG("igfx", "BLR: [CFL+] WriteRegister32: Ice Lake backlight fix was entered."); + DBGLOG("igfx", "BLR: [CFL+] WriteRegister32: Backlight additional fix was entered."); uint32_t registerValue = callbackIGFX->readRegister32(controller, SFUSE_STRAP); uint32_t selectedFreq = (registerValue & SFUSE_STRAP_RAW_FREQUENCY) ? ICL_FREQ_RAW : ICL_FREQ_NORMAL; wrapCFLWriteRegisterPWMFreq1(controller, BXT_BLC_PWM_FREQ1, selectedFreq);