From 21b2d172a2f068f721ff1e225b52c560f5445feb Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Wed, 11 Nov 2026 16:37:05 -0500 Subject: [PATCH 1/2] drm/apple: start the internal display's firmware from the resume When the panel was off by DPMS before a sleep, as it is when KWin blanks the outputs for it, the DCP firmware stays stopped through the resume. It starts only when KWin turns the output on again after the thaw: on the Neo that is 0.57 s into the wake, and the warm restart then runs inside the modeset. macOS doesn't wait for that. In both wakes of the recording with the display in use, the display domains go on 78-82 ms into the wake, DCP's HELLO comes 0.5 ms later and its first display call about 10 ms after that, which by its timing comes from the kernel's power path, not WindowServer. Frames follow from about 0.3 s. Start the firmware from the DCP's resume on a work item, so it comes up while the other devices resume, and leave the panel dark. That is the state a refused idle leaves: the firmware running and the display off. DPMS on, DPMS off and the next sleep already handle it, and they now wait for the start first. DPMS on then only sends set_power_state(1) and waits for the scanout DART, as on a refused idle. If DPMS on doesn't come within wake_prewarm_ms (10 s), the firmware stops again as DPMS off stops it, so a closed lid or a session that keeps the output off ends up as before. A dark wake doesn't start it. wake_prewarm_ms=0 keeps the old behaviour. Signed-off-by: Diljit Singh --- drivers/gpu/drm/apple/dcp-internal.h | 6 ++ drivers/gpu/drm/apple/dcp.c | 104 ++++++++++++++++++++++++++- 2 files changed, 108 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index f2ab6f88567f5..96c4b7f2647f0 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -698,6 +698,12 @@ struct apple_dcp { struct notifier_block s2r_pm_nb; /* This driver raised the display domains, and nothing has lowered them since. */ bool s2r_domains_up; + /* Firmware started by the resume, panel still dark: DPMS on lights it. */ + bool s2r_prewarmed; + struct work_struct s2r_prewarm_work; + struct delayed_work s2r_prewarm_off_wq; + /* Orders the start from the resume and its timeout against DPMS on and off. */ + struct mutex s2r_prewarm_lock; /* Counted in pmc-dcp, the number of powered DCPs */ bool pmc_up; bool s2r_power_on; diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 05c22685393ef..dec14a725faad 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -379,6 +379,17 @@ module_param(t6041_dcpext, bool, 0444); MODULE_PARM_DESC(t6041_dcpext, "T6041: drive the external display processors (default 1; 0 leaves them alone)"); +/* + * After a wake that finds the internal panel off by DPMS, start its firmware from the + * resume, as macOS does about 83 ms into a wake, so that DPMS on only has to light the + * panel. If DPMS on hasn't come this long after, the firmware stops again. 0: the + * firmware waits for DPMS on, as before. + */ +static unsigned int wake_prewarm_ms = 10000; +module_param(wake_prewarm_ms, uint, 0644); +MODULE_PARM_DESC(wake_prewarm_ms, + "Start the internal display's firmware at resume, stop it if DPMS stays off this long (ms, 0: off)"); + bool dcp_t6041_ext_enabled(void) { return t6041_dcpext; @@ -4399,6 +4410,8 @@ static bool dcp_h17g_panel(struct apple_dcp *dcp) static void dcp_dpms_power_off(struct apple_dcp *dcp); static void dcp_dpms_power_on(struct apple_dcp *dcp); +static void dcp_s2r_prewarm_work(struct work_struct *work); +static void dcp_s2r_prewarm_off_work(struct work_struct *work); static bool dcp_uses_soft_dpms(struct apple_dcp *dcp) { @@ -6423,6 +6436,9 @@ static int dcp_platform_probe_pipe(struct platform_device *pdev) /* System sleep and shutdown cancel these even if no bind succeeded. */ INIT_WORK(&dcp->vblank_wq, dcp_delayed_vblank); INIT_DELAYED_WORK(&dcp->swap_watchdog_wq, dcp_swap_watchdog); + INIT_WORK(&dcp->s2r_prewarm_work, dcp_s2r_prewarm_work); + INIT_DELAYED_WORK(&dcp->s2r_prewarm_off_wq, dcp_s2r_prewarm_off_work); + mutex_init(&dcp->s2r_prewarm_lock); INIT_WORK(&dcp->bl_register_wq, dcp_work_register_backlight); INIT_WORK(&dcp->bl_update_wq, dcp_work_update_backlight); mutex_init(&dcp->bl_register_mutex); @@ -6925,6 +6941,11 @@ static void dcp_platform_remove(struct platform_device *pdev) { struct apple_dcp *dcp = platform_get_drvdata(pdev); + if (dcp) { + cancel_work_sync(&dcp->s2r_prewarm_work); + cancel_delayed_work_sync(&dcp->s2r_prewarm_off_wq); + } + if (dcp && dcp->external && !dcp->external_native) { /* Enabled for the external start at probe. No component unbind. */ disable_delayed_work_sync(&dcp->typec_reconnect_wq); @@ -8155,12 +8176,15 @@ static int dcp_s2r_resume(struct apple_dcp *dcp) return 0; } dcp->s2r_dark_held = false; - /* Off by DPMS before the sleep: it stays off until DPMS on brings it back. */ + /* Off by DPMS before the sleep: the panel stays off until DPMS on brings it back. */ if (dcp->dpms_powered_off) { /* Off after a refused idle, its power may be on: DPMS on needs it lowered. */ if (!real) dcp_s2r_refused_domains_off(dcp); dcp_s2r_put_darts(dcp); + /* Its firmware starts now, alongside the devices still resuming. */ + if (real && !pm_dark_wake() && READ_ONCE(wake_prewarm_ms)) + queue_work(system_highpri_wq, &dcp->s2r_prewarm_work); return 0; } dcp_s2r_restart(dcp, real); @@ -8239,7 +8263,7 @@ static bool dcp_dpms_power_cycles(struct apple_dcp *dcp) return dcp_s2r_capable(dcp) && pm_suspend_target_state == PM_SUSPEND_ON; } -static void dcp_dpms_power_off(struct apple_dcp *dcp) +static void __dcp_dpms_power_off(struct apple_dcp *dcp) { ktime_t start = ktime_get(); int ret; @@ -8289,6 +8313,19 @@ static void dcp_dpms_power_on(struct apple_dcp *dcp) ktime_t start = ktime_get(); int ret; + /* A firmware start from the resume finishes first; then only the panel is left. */ + flush_work(&dcp->s2r_prewarm_work); + cancel_delayed_work_sync(&dcp->s2r_prewarm_off_wq); + guard(mutex)(&dcp->s2r_prewarm_lock); + if (dcp->s2r_prewarmed) { + dcp->s2r_prewarmed = false; + dcp->dpms_powered_off = false; + dcp->s2r_refused_off = false; + dcp_s2r_light_running(dcp); + dev_info(dcp->dev, "DPMS on: panel lit on the firmware the resume started, %lld ms\n", + ktime_ms_delta(ktime_get(), start)); + return; + } if (dcp->s2r_refused_off) { dcp->s2r_refused_off = false; dcp_s2r_light_running(dcp); @@ -8314,6 +8351,64 @@ static void dcp_dpms_power_on(struct apple_dcp *dcp) ktime_ms_delta(ktime_get(), start)); } +/* + * From the resume: the firmware up with the panel dark, the state a refused idle leaves, + * which DPMS on, DPMS off and the next sleep all handle. macOS has DCP's HELLO 83 ms into + * a wake and its first display call about 10 ms later. + */ +static void dcp_s2r_prewarm_work(struct work_struct *work) +{ + struct apple_dcp *dcp = container_of(work, struct apple_dcp, s2r_prewarm_work); + ktime_t start = ktime_get(); + int ret; + + guard(mutex)(&dcp->s2r_prewarm_lock); + if (!dcp->dpms_powered_off || !READ_ONCE(dcp->s2r_fw_down) || READ_ONCE(dcp->crashed)) + return; + dcp_s2r_find_darts(dcp); + ret = dcp_s2r_warm_restart(dcp); + if (ret) { + /* Back as DPMS off left it, stopped and powered down: DPMS on starts it again. */ + dcp_s2r_stop_failed_start(dcp); + dcp_s2r_stop_streams(dcp); + dcp_s2r_domains_off(dcp); + dcp_s2r_put_darts(dcp); + dev_warn(dcp->dev, "wake: firmware start failed, DPMS on tries again: %d\n", ret); + return; + } + dcp_s2r_put_darts(dcp); + WRITE_ONCE(dcp->s2r_fw_down, false); + dcp->s2r_refused_off = true; + dcp->s2r_prewarmed = true; + mod_delayed_work(system_freezable_wq, &dcp->s2r_prewarm_off_wq, + msecs_to_jiffies(READ_ONCE(wake_prewarm_ms))); + dev_info(dcp->dev, "wake: firmware up in %lld ms, panel off until DPMS on\n", + ktime_ms_delta(ktime_get(), start)); +} + +/* DPMS stayed off after the wake, with the lid closed say: stopped again as DPMS off does. */ +static void dcp_s2r_prewarm_off_work(struct work_struct *work) +{ + struct apple_dcp *dcp = container_of(to_delayed_work(work), struct apple_dcp, + s2r_prewarm_off_wq); + + guard(mutex)(&dcp->s2r_prewarm_lock); + if (!dcp->s2r_prewarmed) + return; + dcp->s2r_prewarmed = false; + dev_info(dcp->dev, "wake: DPMS stayed off, display firmware stopped again\n"); + __dcp_dpms_power_off(dcp); +} + +static void dcp_dpms_power_off(struct apple_dcp *dcp) +{ + flush_work(&dcp->s2r_prewarm_work); + cancel_delayed_work_sync(&dcp->s2r_prewarm_off_wq); + guard(mutex)(&dcp->s2r_prewarm_lock); + dcp->s2r_prewarmed = false; + __dcp_dpms_power_off(dcp); +} + bool dcp_s2r_firmware_stopped(struct platform_device *pdev) { struct apple_dcp *dcp = platform_get_drvdata(pdev); @@ -8329,6 +8424,11 @@ static int dcp_platform_suspend(struct device *dev) if (dcp_takeover_was_aborted(dcp)) return -EBUSY; + /* A firmware start from the last resume finishes first; this sleep stops it again. */ + flush_work(&dcp->s2r_prewarm_work); + cancel_delayed_work_sync(&dcp->s2r_prewarm_off_wq); + dcp->s2r_prewarmed = false; + if (pm_suspend_via_firmware() && dcp_s2r_capable(dcp)) return dcp_s2r_suspend(dcp); if (pm_suspend_via_firmware() && dcp_s2r_ext_capable(dcp)) From b5989a04769a32ed58ec0cdfae3690db548d68df Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Wed, 11 Nov 2026 17:02:23 -0500 Subject: [PATCH 2/2] drm/apple: light the internal panel from the resume, as macOS does With the firmware started from the resume (the previous patch), the panel itself still waits for KWin's DPMS on, 0.57 s into a wake on the Neo. macOS doesn't wait for WindowServer there either: about 10 ms after DCP's HELLO, 90 ms into the wake, its power path sends the display-on calls, A414(0), A478 twice, A476(1) (the clamshell state), A478, A412 and set_power_state(1), and frames follow from about 0.3 s. Its backlight comes on after the first frame. On a full wake with the lid open, do the same from the resume work: after the warm restart, send those calls and wait for D572, as DPMS on would. The panel is then powered with its backlight at the DPMS-off level, 0. When KWin turns the output on, DPMS on finds the panel already on and leaves only the mode set and the first frame to the commit. That frame carries the backlight level from before the sleep, as DPMS on already brings it back. If DPMS on doesn't come within wake_prewarm_ms, set_power_state(0) and the firmware stop turn it off again, and a sleep before then does the same. The backlight's own present reuses the retained scanout, and after a sleep that's the frame from before it, which needn't be what KWin shows next: the session may lock as it resumes. Hold that present after a wake until KWin's first frame, which then carries the level, so nothing from before the sleep is shown. The log says when the old frame would have been reused, also with wake_light=0. The lid comes from the SMC: its noirq resume now reads MSLD on every wake, not only on a wake for the lid alone, and the sleep driver keeps it for the display. With the lid read closed, the firmware and panel stay off, as macOS keeps the internal display off with the lid closed. A dark wake lights nothing, as before. A lid that couldn't be read gives the previous patch's behaviour. wake_light=0 gives the previous patch's behaviour, and wake_prewarm_ms=0 the behaviour before both. Signed-off-by: Diljit Singh --- drivers/gpu/drm/apple/dcp-internal.h | 8 +++ drivers/gpu/drm/apple/dcp.c | 104 ++++++++++++++++++++++++--- drivers/gpu/drm/apple/iomfb.c | 19 +++++ drivers/mfd/macsmc-test.c | 7 +- drivers/mfd/macsmc.c | 15 ++-- drivers/soc/apple/sleep.c | 16 +++++ include/linux/soc/apple/sleep.h | 21 ++++++ 7 files changed, 174 insertions(+), 16 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index 96c4b7f2647f0..90939cb1cb2cc 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -704,6 +704,14 @@ struct apple_dcp { struct delayed_work s2r_prewarm_off_wq; /* Orders the start from the resume and its timeout against DPMS on and off. */ struct mutex s2r_prewarm_lock; + /* This wake lights the panel from the resume too: full wake, lid read open. */ + bool s2r_wake_light; + /* Lit by the resume, with the CRTC still off: DPMS on has nothing left to power. */ + bool s2r_wake_lit; + ktime_t s2r_wake_lit_at; + /* The retained scanout is from before the sleep; with hold, no present reuses it. */ + bool s2r_scanout_stale; + bool s2r_scanout_hold; /* Counted in pmc-dcp, the number of powered DCPs */ bool pmc_up; bool s2r_power_on; diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index dec14a725faad..6b770a664296c 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -390,6 +391,17 @@ module_param(wake_prewarm_ms, uint, 0644); MODULE_PARM_DESC(wake_prewarm_ms, "Start the internal display's firmware at resume, stop it if DPMS stays off this long (ms, 0: off)"); +/* + * With wake_prewarm_ms set, on a full wake with the lid open, also power the panel from + * the resume, A414(0) to set_power_state(1) as macOS's power path sends them, and keep it + * dark until the compositor's first frame, which brings back the level from before the + * sleep. A closed lid leaves the display and its firmware off. 0: firmware only. + */ +static bool wake_light = true; +module_param(wake_light, bool, 0644); +MODULE_PARM_DESC(wake_light, + "With wake_prewarm_ms, also power the internal panel at resume if the lid is open (default 1)"); + bool dcp_t6041_ext_enabled(void) { return t6041_dcpext; @@ -7201,6 +7213,10 @@ static void dcp_platform_complete(struct device *dev) struct apple_dcp *dcp = dev_get_drvdata(dev); dcp_s2r_dark_finish(dcp); + /* The sleep failed, or wasn't one that stops the firmware: it still times out. */ + if (READ_ONCE(dcp->s2r_wake_lit)) + mod_delayed_work(system_freezable_wq, &dcp->s2r_prewarm_off_wq, + msecs_to_jiffies(READ_ONCE(wake_prewarm_ms))); if (!dcp->external) return; mutex_lock(&dcp->hpd_mutex); @@ -8156,6 +8172,23 @@ static int dcp_s2r_restart(struct apple_dcp *dcp, bool real) return ret; } +/* + * A full wake that finds the panel off by DPMS. With wake_light, a lid read closed leaves + * the display and its firmware off, as macOS keeps the internal display off with the lid + * closed, and a lid read open lights the panel too. A lid not read only starts the firmware. + */ +static void dcp_s2r_wake_queue(struct apple_dcp *dcp) +{ + int closed = READ_ONCE(wake_light) ? apple_sleep_lid_closed_at_wake() : -ENODATA; + + if (closed > 0) { + dev_info(dcp->dev, "wake: lid closed, display and firmware stay off\n"); + return; + } + dcp->s2r_wake_light = !closed; + queue_work(system_highpri_wq, &dcp->s2r_prewarm_work); +} + static int dcp_s2r_resume(struct apple_dcp *dcp) { bool real = pm_resume_via_firmware(); @@ -8176,15 +8209,21 @@ static int dcp_s2r_resume(struct apple_dcp *dcp) return 0; } dcp->s2r_dark_held = false; - /* Off by DPMS before the sleep: the panel stays off until DPMS on brings it back. */ + /* Off by DPMS before the sleep: DPMS on, or this wake, brings it back. */ if (dcp->dpms_powered_off) { /* Off after a refused idle, its power may be on: DPMS on needs it lowered. */ if (!real) dcp_s2r_refused_domains_off(dcp); dcp_s2r_put_darts(dcp); + if (real) { + /* Its last frame is from before the sleep, and may not be what comes next. */ + WRITE_ONCE(dcp->s2r_scanout_stale, true); + WRITE_ONCE(dcp->s2r_scanout_hold, + READ_ONCE(wake_prewarm_ms) && READ_ONCE(wake_light)); + } /* Its firmware starts now, alongside the devices still resuming. */ if (real && !pm_dark_wake() && READ_ONCE(wake_prewarm_ms)) - queue_work(system_highpri_wq, &dcp->s2r_prewarm_work); + dcp_s2r_wake_queue(dcp); return 0; } dcp_s2r_restart(dcp, real); @@ -8317,6 +8356,13 @@ static void dcp_dpms_power_on(struct apple_dcp *dcp) flush_work(&dcp->s2r_prewarm_work); cancel_delayed_work_sync(&dcp->s2r_prewarm_off_wq); guard(mutex)(&dcp->s2r_prewarm_lock); + /* Lit by the resume: only the mode set and the first frame are left, in the commit. */ + if (dcp->s2r_wake_lit) { + dcp->s2r_wake_lit = false; + dev_info(dcp->dev, "DPMS on: panel already on, %lld ms after the resume lit it\n", + ktime_ms_delta(start, dcp->s2r_wake_lit_at)); + return; + } if (dcp->s2r_prewarmed) { dcp->s2r_prewarmed = false; dcp->dpms_powered_off = false; @@ -8354,15 +8400,20 @@ static void dcp_dpms_power_on(struct apple_dcp *dcp) /* * From the resume: the firmware up with the panel dark, the state a refused idle leaves, * which DPMS on, DPMS off and the next sleep all handle. macOS has DCP's HELLO 83 ms into - * a wake and its first display call about 10 ms later. + * a wake and its first display call about 10 ms later. With the lid open and wake_light, + * the panel then powers up too, still dark, as DPMS on would power it. */ static void dcp_s2r_prewarm_work(struct work_struct *work) { struct apple_dcp *dcp = container_of(work, struct apple_dcp, s2r_prewarm_work); ktime_t start = ktime_get(); + bool light; + s64 fw_ms; int ret; guard(mutex)(&dcp->s2r_prewarm_lock); + light = dcp->s2r_wake_light; + dcp->s2r_wake_light = false; if (!dcp->dpms_powered_off || !READ_ONCE(dcp->s2r_fw_down) || READ_ONCE(dcp->crashed)) return; dcp_s2r_find_darts(dcp); @@ -8379,11 +8430,32 @@ static void dcp_s2r_prewarm_work(struct work_struct *work) dcp_s2r_put_darts(dcp); WRITE_ONCE(dcp->s2r_fw_down, false); dcp->s2r_refused_off = true; - dcp->s2r_prewarmed = true; + if (!light) { + dcp->s2r_prewarmed = true; + mod_delayed_work(system_freezable_wq, &dcp->s2r_prewarm_off_wq, + msecs_to_jiffies(READ_ONCE(wake_prewarm_ms))); + dev_info(dcp->dev, "wake: firmware up in %lld ms, panel off until DPMS on\n", + ktime_ms_delta(ktime_get(), start)); + return; + } + + /* + * Then what DPMS on would do: A414(0), A478 x2, A476(1), A478, A412, A473(1) and D572, + * as macOS's power path sends them 10 ms after DCP's HELLO. The backlight stays at its + * DPMS-off level, 0, until DPMS on: the compositor's first frame brings the old level. + */ + fw_ms = ktime_ms_delta(ktime_get(), start); + dcp->s2r_refused_off = false; + dcp->dpms_powered_off = false; + dcp_s2r_light_running(dcp); + if (READ_ONCE(dcp->crashed)) + return; + dcp->s2r_wake_lit = true; + dcp->s2r_wake_lit_at = ktime_get(); mod_delayed_work(system_freezable_wq, &dcp->s2r_prewarm_off_wq, msecs_to_jiffies(READ_ONCE(wake_prewarm_ms))); - dev_info(dcp->dev, "wake: firmware up in %lld ms, panel off until DPMS on\n", - ktime_ms_delta(ktime_get(), start)); + dev_info(dcp->dev, "wake: panel on in %lld ms (firmware %lld ms), dark until the first frame\n", + ktime_ms_delta(dcp->s2r_wake_lit_at, start), fw_ms); } /* DPMS stayed off after the wake, with the lid closed say: stopped again as DPMS off does. */ @@ -8393,6 +8465,13 @@ static void dcp_s2r_prewarm_off_work(struct work_struct *work) s2r_prewarm_off_wq); guard(mutex)(&dcp->s2r_prewarm_lock); + /* Lit and never shown: off as DPMS off does it, set_power_state(0) first. */ + if (dcp->s2r_wake_lit) { + dcp->s2r_wake_lit = false; + dev_info(dcp->dev, "wake: DPMS stayed off, panel and firmware off again\n"); + __dcp_dpms_power_off(dcp); + return; + } if (!dcp->s2r_prewarmed) return; dcp->s2r_prewarmed = false; @@ -8406,6 +8485,7 @@ static void dcp_dpms_power_off(struct apple_dcp *dcp) cancel_delayed_work_sync(&dcp->s2r_prewarm_off_wq); guard(mutex)(&dcp->s2r_prewarm_lock); dcp->s2r_prewarmed = false; + dcp->s2r_wake_lit = false; __dcp_dpms_power_off(dcp); } @@ -8429,8 +8509,16 @@ static int dcp_platform_suspend(struct device *dev) cancel_delayed_work_sync(&dcp->s2r_prewarm_off_wq); dcp->s2r_prewarmed = false; - if (pm_suspend_via_firmware() && dcp_s2r_capable(dcp)) - return dcp_s2r_suspend(dcp); + if (pm_suspend_via_firmware() && dcp_s2r_capable(dcp)) { + int ret = dcp_s2r_suspend(dcp); + + /* Lit by the last resume with its CRTC off: the next resume finds it off by DPMS. */ + if (!ret && dcp->s2r_wake_lit) { + dcp->s2r_wake_lit = false; + dcp->dpms_powered_off = true; + } + return ret; + } if (pm_suspend_via_firmware() && dcp_s2r_ext_capable(dcp)) return dcp_s2r_ext_suspend(dcp); diff --git a/drivers/gpu/drm/apple/iomfb.c b/drivers/gpu/drm/apple/iomfb.c index 0a9e1cd356920..0d00da38a12ae 100644 --- a/drivers/gpu/drm/apple/iomfb.c +++ b/drivers/gpu/drm/apple/iomfb.c @@ -3494,6 +3494,19 @@ static void iomfb_backlight_start(struct apple_dcp *dcp, have_surface |= !!scanout->fb[i]; if (!have_surface) return; + /* + * That surface is the frame from before the sleep. With hold, the level waits for + * the compositor's first frame, which then carries it, as macOS lights its backlight + * after the first frame. + */ + if (READ_ONCE(dcp->s2r_scanout_stale)) { + WRITE_ONCE(dcp->s2r_scanout_stale, false); + dev_info(dcp->dev, "wake: brightness present %s\n", + READ_ONCE(dcp->s2r_scanout_hold) ? "held for the first new frame" : + "on the frame from before the sleep"); + } + if (READ_ONCE(dcp->s2r_scanout_hold)) + return; dcp->swap.h17p = scanout->request; if (dcp->fw_compat == DCP_FIRMWARE_V_27_0) @@ -3557,6 +3570,12 @@ static void iomfb_atomic_start(struct apple_dcp *dcp, return; } dcp->iomfb.next_scanout = atomic->scanout; + /* The first frame since the wake, the compositor's: the held level goes with it. */ + WRITE_ONCE(dcp->s2r_scanout_stale, false); + if (READ_ONCE(dcp->s2r_scanout_hold)) { + WRITE_ONCE(dcp->s2r_scanout_hold, false); + dev_info(dcp->dev, "wake: first new frame, backlight back with it\n"); + } if (dcp->fw_compat == DCP_FIRMWARE_V_27_0) iomfb_flush_v27(dcp, atomic->crtc, atomic->state); else diff --git a/drivers/mfd/macsmc-test.c b/drivers/mfd/macsmc-test.c index d6ccf8a3970a2..9bd0dc0cbee1d 100644 --- a/drivers/mfd/macsmc-test.c +++ b/drivers/mfd/macsmc-test.c @@ -337,11 +337,14 @@ static void macsmc_lid_wake_test(struct kunit *test) KUNIT_EXPECT_EQ(test, ctx->seen[3], SMC_KEY(waka)); KUNIT_EXPECT_EQ(test, ctx->seen[4], SMC_KEY(MSLD)); - /* The lid with anything else is a normal wake: the lid isn't read. */ + /* The lid with anything else is a normal wake: the lid is still read, for the display. */ ctx->seen_count = 0; ctx->record = BIT_ULL(10) | BIT_ULL(0); apple_smc_sleep_wake(&ctx->smc); - KUNIT_EXPECT_EQ(test, ctx->seen_count, 4U); + KUNIT_ASSERT_EQ(test, ctx->seen_count, 5U); + KUNIT_EXPECT_EQ(test, ctx->seen[4], SMC_KEY(MSLD)); + if (IS_ENABLED(CONFIG_APPLE_SOC_SLEEP)) + KUNIT_EXPECT_EQ(test, apple_sleep_lid_closed_at_wake(), 1); } static void macsmc_wake_reason_test(struct kunit *test) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index fce9dc9527e66..ceff8718b6956 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -832,23 +832,26 @@ static int apple_smc_wake_reason_bit(const char *name) } /* - * Woken for the lid alone, which the SMC reports for the lid opening and for it closing: - * tell the sleep driver if it's closed now. + * Tell the sleep driver where the lid is now, for the display. Woken for the lid alone, + * which the SMC reports for the lid opening and for it closing: also if it's closed now. */ static void apple_smc_lid_wake(struct apple_smc *smc) { int bit = apple_smc_wake_reason_bit("lid"); + bool lid_alone = bit >= 0 && smc->wake_record == BIT_ULL(bit); u8 lid = 0; int ret; - if (bit < 0 || smc->wake_record != BIT_ULL(bit)) - return; - + /* No MSLD on a machine without a lid: the display then isn't told either way. */ ret = apple_smc_read_u8(smc, SMC_KEY(MSLD), &lid); if (ret < 0) { - dev_warn(smc->dev, "Failed to read the lid after a wake: %d\n", ret); + if (lid_alone) + dev_warn(smc->dev, "Failed to read the lid after a wake: %d\n", ret); return; } + apple_sleep_lid_at_wake(lid); + if (!lid_alone) + return; dev_info(smc->dev, "woken for the lid, which is %s\n", lid ? "closed" : "open"); if (lid) apple_sleep_lid_closed_wake(); diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index c78be9b28b3da..65cb488630e3c 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -1984,6 +1984,21 @@ void apple_sleep_lid_closed_wake(void) } EXPORT_SYMBOL_GPL(apple_sleep_lid_closed_wake); +/* Read in the SMC's noirq resume. Forgotten as each sleep begins. */ +static int sleep_lid_closed = -ENODATA; + +void apple_sleep_lid_at_wake(bool closed) +{ + WRITE_ONCE(sleep_lid_closed, closed); +} +EXPORT_SYMBOL_GPL(apple_sleep_lid_at_wake); + +int apple_sleep_lid_closed_at_wake(void) +{ + return READ_ONCE(sleep_lid_closed); +} +EXPORT_SYMBOL_GPL(apple_sleep_lid_closed_at_wake); + /* * The SMC woke the AP for the port alone, and this sleep cut the debug link: the cut's own * events. Sleep again. The link stays down until tipd rearms it at a full wake, so the next @@ -2928,6 +2943,7 @@ static int apple_sleep_begin(suspend_state_t state) { /* The last sleep's cut is its port controller's to take, by its .complete. */ WRITE_ONCE(sleep_debug_link_cut, false); + WRITE_ONCE(sleep_lid_closed, -ENODATA); /* The PM core also calls .begin for s2idle when no s2idle ops are installed. */ if (state != PM_SUSPEND_MEM) return 0; diff --git a/include/linux/soc/apple/sleep.h b/include/linux/soc/apple/sleep.h index 6ce00ba6001d9..fb78d197992c1 100644 --- a/include/linux/soc/apple/sleep.h +++ b/include/linux/soc/apple/sleep.h @@ -126,6 +126,18 @@ bool apple_sleep_debug_link_up(void); */ void apple_sleep_lid_closed_wake(void); +/* + * From the SMC's driver, in its noirq resume: the lid as it reads now, after every + * sleep the SMC was told of, whatever woke it. + */ +void apple_sleep_lid_at_wake(bool closed); + +/* + * For the display: 1 if the lid read closed in this wake's noirq resume, 0 if open, + * -ENODATA if it wasn't read. macOS keeps the internal panel off with the lid closed. + */ +int apple_sleep_lid_closed_at_wake(void); + /* * From the SMC's driver, in its noirq resume: the SMC woke the AP for the port alone. If * this sleep cut the debug link and nothing else woke the SoC, it's a dark wake. @@ -255,6 +267,15 @@ static inline void apple_sleep_lid_closed_wake(void) { } +static inline void apple_sleep_lid_at_wake(bool closed) +{ +} + +static inline int apple_sleep_lid_closed_at_wake(void) +{ + return -ENODATA; +} + static inline void apple_sleep_debug_link_cut_wake(void) { }