Some notebook systems with EC-driven backlight control appear to have a
firmware bug which causes the system to use GPU-driven backlight control
upon a fresh boot, but then switches to EC-driven backlight control
after completing a suspend/resume cycle. All the while, the firmware
reports that the backlight is under EC control, regardless of what is
actually controlling the backlight brightness.
This leads to the following behavior:
* nvidia-wmi-ec-backlight gets probed on a fresh boot, due to the
WMI-wrapped ACPI method erroneously reporting EC control.
* nvidia-wmi-ec-backlight does not work until after a suspend/resume
cycle, due to the backlight control actually being GPU-driven.
* GPU drivers also register their own backlight handlers: in the case
of the notebook system where this behavior has been observed, both
amdgpu and the NVIDIA proprietary driver register backlight handlers.
* The GPU which has backlight control upon a fresh boot (amdgpu in the
case observed so far) can successfully control the backlight through
its backlight driver's sysfs interface, but stops working after the
first suspend/resume cycle.
* nvidia-wmi-ec-backlight is unable to control the backlight upon a
fresh boot, but begins to work after the first suspend/resume cycle.
* The GPU which does not have backlight control (NVIDIA in this case)
is not able to control the backlight at any point while the system
is in operation. On similar hybrid systems with an EC-controlled
backlight, and AMD/NVIDIA iGPU/dGPU, the NVIDIA proprietary driver
does not register its backlight handler. It has not been determined
whether the non-functional handler registered by the NVIDIA driver
is due to another firmware bug, or a bug in the NVIDIA driver.
Since nvidia-wmi-ec-backlight registers as a BACKLIGHT_FIRMWARE type
device, it takes precedence over the BACKLIGHT_RAW devices registered
by the GPU drivers. This in turn leads to backlight control appearing
to be non-functional until after completing a suspend/resume cycle.
However, it is still possible to control the backlight through direct
interaction with the working GPU driver's backlight sysfs interface.
These systems also appear to have a second firmware bug which resets
the EC's brightness level to 100% on resume, but leaves the state in
the kernel at the pre-suspend level. This causes attempts to save
and restore the backlight level across the suspend/resume cycle to
fail, due to the level appearing not to change even though it did.
In order to work around these issue, add quirk tables to detect
systems that are known to show these behaviors. So far, there is
only one known system that requires these workarounds, and both
issues are present on that system, but the quirks are tracked in
separate tables to make it easier to add them to other systems which
may exhibit one of the bugs, but not the other. The original systems
that this driver was tested on during development do not exhibit
either of these quirks.
If a system with the "GPU driver has backlight control" quirk is
detected, nvidia-wmi-ec-backlight will grab a reference to the working
(when freshly booted) GPU backlight handler and relays any backlight
brightness level change requests directed at the EC to also be applied
to the GPU backlight interface. This leads to redundant updates
directed at the GPU backlight driver after a suspend/resume cycle, but
it does allow the EC backlight control to work when the system is
freshly booted.
If a system with the "backlight level reset to full on resume" quirk
is detected, nvidia-wmi-ec-backlight will register a PM notifier to
reset the backlight to the previous level upon resume.
These workarounds are also plumbed through to kernel module parameters,
to make it easier for users who suspect they may be affected by one or
both of these bugs to test whether these workarounds are effective on
their systems as well.
Signed-off-by: Daniel Dadap <ddadap@xxxxxxxxxx>
Tested-by: Alexandru Dinu <alex.dinu07@xxxxxxxxx>
---
.../platform/x86/nvidia-wmi-ec-backlight.c | 181 +++++++++++++++++-
1 file changed, 179 insertions(+), 2 deletions(-)
diff --git a/drivers/platform/x86/nvidia-wmi-ec-backlight.c b/drivers/platform/x86/nvidia-wmi-ec-backlight.c
index 61e37194df70..ccb3b506c12c 100644
--- a/drivers/platform/x86/nvidia-wmi-ec-backlight.c
+++ b/drivers/platform/x86/nvidia-wmi-ec-backlight.c
@@ -3,8 +3,11 @@
* Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
*/
+#define pr_fmt(f) "%s: " f "\n", KBUILD_MODNAME
+
#include <linux/acpi.h>
#include <linux/backlight.h>
+#include <linux/dmi.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/types.h>
@@ -75,6 +78,69 @@ struct wmi_brightness_args {
u32 ignored[3];
};
+/**
+ * struct nvidia_wmi_ec_backlight_priv - driver private data
+ * @bl_dev: the associated backlight device
+ * @proxy_target: backlight device which receives relayed brightness changes
+ * @notifier: notifier block for resume callback
+ */
+struct nvidia_wmi_ec_backlight_priv {
+ struct backlight_device *bl_dev;
+ struct backlight_device *proxy_target;
+ struct notifier_block nb;
+};
+
+static char *backlight_proxy_target;
+module_param(backlight_proxy_target, charp, 0);
+MODULE_PARM_DESC(backlight_proxy_target, "Relay brightness change requests to the named backlight driver, on systems which erroneously report EC backlight control.");
+
+static int max_reprobe_attempts = 128;
+module_param(max_reprobe_attempts, int, 0);
+MODULE_PARM_DESC(max_reprobe_attempts, "Limit of reprobe attempts when relaying brightness change requests.");
+
+static bool restore_level_on_resume;
+module_param(restore_level_on_resume, bool, 0);
+MODULE_PARM_DESC(restore_level_on_resume, "Restore the backlight level when resuming from suspend, on systems which reset the EC's backlight level on resume.");
+
+static int assign_relay_quirk(const struct dmi_system_id *id)
+{
+ backlight_proxy_target = id->driver_data;
+ return true;
+}
+
+#define PROXY_QUIRK_ENTRY(vendor, product, quirk_data) { \
+ .callback = assign_relay_quirk, \
+ .matches = { \
+ DMI_MATCH(DMI_SYS_VENDOR, vendor), \
+ DMI_MATCH(DMI_PRODUCT_VERSION, product) \
+ }, \
+ .driver_data = quirk_data \
+}
+
+static const struct dmi_system_id proxy_quirk_table[] = {
+ PROXY_QUIRK_ENTRY("LENOVO", "Legion S7 15ACH6", "amdgpu_bl1"),
+ { }
+};
+
+static int assign_restore_quirk(const struct dmi_system_id *id)
+{
+ restore_level_on_resume = true;
+ return true;
+}
+
+#define RESTORE_QUIRK_ENTRY(vendor, product) { \
+ .callback = assign_restore_quirk, \
+ .matches = { \
+ DMI_MATCH(DMI_SYS_VENDOR, vendor), \
+ DMI_MATCH(DMI_PRODUCT_VERSION, product), \
+ } \
+}
+
+static const struct dmi_system_id restore_quirk_table[] = {
+ RESTORE_QUIRK_ENTRY("LENOVO", "Legion S7 15ACH6"),
+ { }
+};
+
/**
* wmi_brightness_notify() - helper function for calling WMI-wrapped ACPI method
* @w: Pointer to the struct wmi_device identified by %WMI_BRIGHTNESS_GUID