Bug 454433

Summary: External display is not detected when laptop is running on battery
Product: [Plasma] kwin Reporter: VesperLlama <kde>
Component: platform-drmAssignee: KWin default assignee <kwin-bugs-null>
Status: RESOLVED UPSTREAM    
Severity: normal CC: nate, xaver.hugl
Priority: NOR    
Version First Reported In: 5.24.90   
Target Milestone: ---   
Platform: Arch Linux   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:
Attachments: Log when monitor is not detected
Log when monitor is detected
drm_info output

Description VesperLlama 2022-05-26 12:25:41 UTC
SUMMARY
My laptop has a dGPU which is the AMD RX 6600M to which external monitors connect to. When I plug in the HDMI cable on Wayland session after login and the laptop is running on battery then the external monitor is not detected by Plasma but the monitor shows that it is connected to a source but it just shows a black screen. When I plug in the charger and then plug in the HDMI cable then the monitor is detected by Plasma. 

A regression in 5.24.90 –
In Plasma 5.24.90, after plugging in the charger, the display shows up in Display Configuration, but it still doesn't display the output to the monitor and only outputs on the laptop screen and it incorrectly identifies the displays. 
But on 5.24.5, the display correctly outputs to the monitor after following the previous steps. 

On X11 (both 5.24.5 and 5.24.90) and if I connect the monitor before logging in through SDDM, it works without having to connect the laptop to the charger.


STEPS TO REPRODUCE
1. Login to Plasma Wayland
2. Connect HDMI cable to laptop
3. Check Display Configuration

OBSERVED RESULT
The external display is not detected until laptop is plugged in to charger and then reconnecting HDMI cable.

EXPECTED RESULT
The external monitor should work even on battery.

SOFTWARE/OS VERSIONS
Distro: Arch Linux
KDE Plasma Version: 5.24.5 and 5.24.90
KDE Frameworks Version: 5.94.0
Qt Version: 5.15.4

ADDITIONAL INFORMATION
Output of "kscreen-doctor --outputs" when external monitor is not detected –

Output: 1 AU Optronics eDP-1-unknown enabled connected primary Panel Modes: 0:1920x1080@144*! 1:1920x1080@60 2:1680x1050@144 3:1280x1024@144 4:1440x900@144 5:1280x800@144 6:1280x720@144 7:1024x768@144 8:800x600@144 9:640x480@144 10:1600x900@2058702 Geometry: 0,0 1920x1080 Scale: 1 Rotation: 1 Overscan: 0 Vrr: Automatic RgbRange: unknown primary


Output of "kscreen-doctor --outputs" when external monitor is detected –

Output: 1 AU Optronics eDP-1-unknown enabled connected primary Panel Modes: 0:1920x1080@144*! 1:1920x1080@60 2:1680x1050@144 3:1280x1024@144 4:1440x900@144 5:1280x800@144 6:1280x720@144 7:1024x768@144 8:800x600@144 9:640x480@144 10:1600x900@2058702 Geometry: 0,0 1920x1080 Scale: 1 Rotation: 1 Overscan: 0 Vrr: Automatic RgbRange: unknown primary
Output: 2 Dell Inc. DELL P2217H/M84D98373U8B disabled connected  HDMI Modes: 0:1920x1080@60*! 1:1920x1080@60 2:1920x1080@60 3:1920x1080@50 4:1680x1050@60 5:1600x900@60 6:1280x1024@75 7:1280x1024@60 8:1440x900@60 9:1280x800@60 10:1152x864@75 11:1280x720@60 12:1280x720@60 13:1280x720@60 14:1280x720@50 15:1024x768@75 16:1024x768@60 17:800x600@75 18:800x600@60 19:720x576@50 20:720x480@60 21:720x480@60 22:720x480@60 23:720x480@60 24:720x480@60 25:640x480@75 26:640x480@60 27:640x480@60 28:640x480@60 29:720x400@70 30:1368x768@-1501773 Geometry: 0,0 1920x1080 Scale: 1 Rotation: 1 Overscan: 0 Vrr: incapable RgbRange: unknown


On 5.24.90, the external monitor always says disabled and changing the display output in Plasma doesn't do anything. 

On 5.24.90, wayland-session.log is empty for me, but on 5.24.5, it shows the following error when I plug in the monitor on battery –
kwin_wayland_drm: failed to open drm device at "/dev/dri/card0"
Comment 1 Zamundaaa 2022-05-27 00:09:47 UTC
Please add 
QT_LOGGING_RULES="kwin_wayland_*.debug=true"
into your /etc/environment and reboot. Then attach your monitor and 
journalctl --boot 0 --user-unit plasma-kwin_wayland | grep kwin_wayland_drm
here
What could also be useful is the output of drm_info, with the monitor attached
Comment 2 VesperLlama 2022-05-27 11:28:56 UTC
Created attachment 149253 [details]
Log when monitor is not detected
Comment 3 VesperLlama 2022-05-27 11:29:33 UTC
Created attachment 149254 [details]
Log when monitor is detected
Comment 4 VesperLlama 2022-05-27 11:30:26 UTC
Created attachment 149255 [details]
drm_info output

I have attached the required files
Comment 5 VesperLlama 2022-05-27 11:35:19 UTC
I am also facing this bug (https://bugs.kde.org/show_bug.cgi?id=454086) so the monitor is not working correctly but it is detected in Plasma.
Comment 6 Zamundaaa 2022-05-27 17:35:38 UTC
When the monitor is not detected, KWin is unable to access the GPU: 
> kwin_wayland_drm: failed to open drm device at "/dev/dri/card0"
When the monitor is not detected, what does "ls /dev/dri" say?
Comment 7 VesperLlama 2022-05-27 18:15:07 UTC
> ls /dev/dri
> by-path  card0  card1  renderD128  renderD129

It gives the same output when laptop is charging and the monitor is detected. Both GPUs are displayed. If I run any app on the dGPU using DRI_PRIME=1 it works fine on battery.
Comment 8 Zamundaaa 2022-05-27 18:21:05 UTC
If you run an app on the dGPU before you connect the external monitor, does that make a difference?
Comment 9 VesperLlama 2022-05-27 18:47:11 UTC
Yes, the monitor is detected if an app is running on the dGPU. I ran glxgears, firefox and KDE systemsettings on the dGPU then connected the monitor and it was detected in all three cases.

Also, system settings, powerdevil and kded5 crashes with a segmentation fault when the monitor is connected and detected if I am on the Display Configuration page. Could this be related?
Comment 10 Zamundaaa 2022-05-27 19:07:14 UTC
The problem is that KWin completely stops bothering the GPU and driver when there's no outputs connected to it. When you're not running any apps on it while on battery, it'll be completely shut down to save power.

Now we only need to find out why KWin can't wake up the GPU while other apps can do it. In the output of
journalctl --boot 0 --user-unit plasma-kwin_wayland
when the monitor is not detected, is there something like
> kwin_wayland_core: Failed to open /dev/dri/card0 (error reason)
? If so, what is the error reason?

The crash of other apps is unrelated, you're most likely experiencing https://bugs.kde.org/show_bug.cgi?id=451028
Comment 11 VesperLlama 2022-05-27 19:40:57 UTC
> kwin_wayland[2772]: kwin_wayland_drm: failed to open drm device at "/dev/dri/card0"

It just shows this. There's no reason given with it. I also don't see any line with "kwin_wayland_core". I used the environment variable stated earlier. Do I need to add a different one?

My laptop has a LED which lights up when the dGPU is awake and it does light up when I plug in the HDMI cable. So the dGPU wakes up when I plug it in (also checked with powertop) but still kwin can't access it.
Comment 12 Bug Janitor Service 2022-05-28 11:55:03 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/kwin/-/merge_requests/2482
Comment 13 Zamundaaa 2022-05-28 11:56:29 UTC
> I also don't see any line with "kwin_wayland_core"
Oh, sorry, it's only "kwin_core". With that, you do need to adjust the environment variable to be 
QT_LOGGING_RULES="kwin_*.debug=true"

If that still doesn't show anything, can you try the linked merge request and see if that produces a message in the log?
Comment 14 VesperLlama 2022-05-28 13:43:42 UTC
> kwin_core: Failed to open /dev/dri/card0 device (Permission denied)

It gives this error. Should I test the merge request?
Comment 15 Zamundaaa 2022-05-28 14:03:09 UTC
No need, I think this is a logind bug then. Can you open an issue at https://github.com/systemd/systemd/issues about TakeDevice failing with "Permission denied" despite KWin having the needed permissions, and link it here? I'll add more information to it where needed.

Until the problem with logind is fixed, I'll add a workaround that allows you to make KWin not release unused GPUs. It may come with increased power draw when the GPU isn't actually used, but it should prevent the problem from appearing
Comment 16 VesperLlama 2022-05-28 14:42:59 UTC
I have opened an issue here according to what I understood about this.
https://github.com/systemd/systemd/issues/23547


> It may come with increased power draw when the GPU isn't actually used
So wll the GPU never go to sleep? I will prefer less power draw instead of this. I can just workaround this myself by running any app on the dGPU before plugging it in.
Comment 17 Zamundaaa 2022-05-28 19:45:17 UTC
ok, good. Let's continue tracking this bug in the systemd repo then