Manjaro 在笔记本上的多显示器配置


Manjaro 在笔记本上的多显示器配置

背景

还是老规矩,我先介绍一下这个问题的背景。 解决方法请翻到解决方法一节。

环境:

intel 集成显卡 + Nvidia 独立显卡,目前笔记本主流配置。但 HDMI 接口实际上是与 Nvidia 显卡相连接的,默认驱动无法对 N 卡输出。

症状:

  • 无法接外接显示器,接上后无论是系统设置还是inxi -Fx都无法识别外置显示器。
  • 检查启动日志,发现虽然开机时检测到了显示器,但后面就没有继续显示了。

如果你的是外接显卡坞+显示器,这篇教程不适合你。不过有网友(感谢@sunlitforest)用另一种方法操作成功了,详情请看这里

分析问题

参照ArchWiki上面的说法),让Optimus显卡在linux里面有三种实现方法:

  1. BIOS里面禁用掉一个显卡。缺点:难以切换显卡。
  2. 用nouveau的PRIME功能。但是性能没有专有驱动好,睡眠和挂起也会出现问题。
  3. 用Bumblebee。不过Bumblebee很难支持双显示器。
  4. 用专有驱动。
  5. 用nvidia-xrun,让X服务器跑在n卡上。(貌似和1一样,但是1是硬件层面上的禁用。)

解决方案

我照着这个帖子的步骤原封不动地操作,成功了。所以,接下来一定要按照步骤严格操作。原文在这里.

前言

如何在Optimus的笔记本上开启PRIME:

方法 优点 缺点
bumblebee(Render offload) 按需使用独立显卡,Manjaro默认配置 有一定的开销,对性能有所影响
PRIME 直接使用独立显卡,性能更强 两块显卡都一直供电,需要手动配置
optimus-manager 显卡切换更简单 暂不成熟,还在开发中
video-hybrid-intel-nvidia-440xx-prime NVIDIA 官方支持的 render offload. 在译者机器上仍有 bug,不建议用

Note:最简单的方法就是在主板设置里面禁用集成显卡,如果可以就直接禁用掉。
optimus-manager可以实现两块显卡的半自动切换,但是目前仍然不太成熟。

移除Bumblebee

如果你选择了non-free驱动,mhwd将会自动安装bumblebee。 bumblebee很碍事,所以我们得先干掉它。

sudo mhwd -r pci nonfree 0300

安装NVIDIA驱动

sudo mhwd -i pci video-nvidia-440xx

这两步操作也可以通过系统设置里面的“硬件设定”来完成。video-nvidia-440xx仍然适用于本教程

修改MHWD设置

mhwd会自动生成一些设置。这里我们需要重新手动配置,这样PRIME才能生效。

另起炉灶

首先要删掉/etc/X11/xorg.conf.d/90-mhwd.conf (如果你不放心可以备份一下嘛。) 然后新建一个文件/etc/X11/xorg.conf.d/optimus.conf

Section "Module"
    Load "modesetting"
EndSection

Section "Device"
    Identifier "nvidia"
    Driver "nvidia"
    BusID "PCI:1:0:0"
    Option "AllowEmptyInitialConfiguration"
EndSection

注意:BusID一栏需要根据自己的电脑配置进行修改。BusID的值是这么查询的:lspci | grep -E "VGA|3D"

禁用模块

PRIME依赖于nvidia-drm,但mhwd在默认情况下会禁用它,我们需要手动启用。 此外,为了确保nvidia驱动正常开启,我们还需要禁用其他一些模块。 所以,我们需要对/etc/modprobe.d动动手脚。

首先,我们得把mhwd自动生成的黑名单删掉。视情况而定

ls /etc/modprobe.d/mhwd*
sudo rm /etc/modprobe.d/mhwd-gpu.conf
sudo rm /etc/modprobe.d/mhwd-nvidia.conf

然后我们要新建一个黑名单,屏蔽一些其他的模块。 新建一个文件,什么名字都可以,conf结尾就行写入:/etc/modprobe.d/nvidia.conf

blacklist nouveau
blacklist nvidiafb
blacklist rivafb

开启 modeset

创建一个新文件,/etc/modprobe.d/nvidia-drm.conf

options nvidia_drm modeset=1

设置启动脚本

我们要设置一下桌面环境的输出源。这是最难的部分,可能会花掉很长时间。 如果你现在重启,桌面环境的输出可能会有一些问题。

我们需要写一个启动脚本,让桌面环境加载的时候能够正确选择输出源。

注意,视你的具体环境(DM)执行下列的某一个步骤,不要执行所有步骤。

针对 LightDM (xfce默认) 创建一个文件,/usr/local/bin/optimus.sh

#!/bin/sh

xrandr --setprovideroutputsource modesetting NVIDIA-0
xrandr --auto

然后按照国际惯例(hhh),让它可写可执行:chmod a+rx /usr/local/bin/optimus.sh 现在你还得把它设置为启动脚本。 编辑/etc/lightdm/lightdm.conf,然后在[Seat:* ]这一节设置 display-setup-script=/usr/local/bin/optimus.sh


针对 GDM (Gnome默认)

创建一个文件/usr/local/share/optimus.desktop

[Desktop Entry]
Type=Application
Name=Optimus
Exec=sh -c "xrandr --setprovideroutputsource modesetting NVIDIA-0; xrandr --auto"
NoDisplay=true
X-GNOME-Autostart-Phase=DisplayServer

(Exec处也可以替换成一个脚本,写一个就像LightDM里面一样的脚本)
然后建立连接,让它随着GDM启动。

sudo ln -s /usr/local/share/optimus.desktop /usr/share/gdm/greeter/autostart/optimus.desktop
sudo ln -s /usr/local/share/optimus.desktop /etc/xdg/autostart/optimus.desktop

针对 SDDM (KDE 默认)

创建一个文件:/usr/share/sddm/scripts/Xsetup

#!/bin/sh

xrandr --setprovideroutputsource modesetting NVIDIA-0
xrandr --auto

然后用chmod让它变为可执行的文件。

最后的工作

如果以上的工作都已经完成,我建议你再做一步。 我当时没有做这一步,之后重启发现内核模块加载错误。 所以还是建议,编译一下内核。

sudo mkinitcpio -P linux

最后重启,大功告成。

验证是否成功

其实也没有什么必要去验证了,如果你真的是按照上面的步骤来的,没有自己改动的话。相信是一定会成功的。

当你重启之后,可以敲glxinfo | grep -i vendor 然后看看是不是像下面这样的信息。

$ glxinfo | grep -i vendor
server glx vendor string: NVIDIA Corporation
client glx vendor string: NVIDIA Corporation
OpenGL vendor string: NVIDIA Corporation

如果是,说明你已经设置成功。


如果你是国际友人

The latest versions of Xorg, kernel, and NVIDIA driver all support PRIME output but setting this up is a bit of a faff. What follows is a method to get this working on an Optimus-enabled laptop.


440xx update: If you have something which supports driver series 440xx then you should switch to video-hybrid-intel-nvidia-440xx-prime and ignore this guide (unless you want to use only the dGPU). Specify using the dGPU with e.g. prime-run glxinfo.


Note: Fermi-based GPUs should use nvidia-390xx instead of nvidia.

Method Advantages Disadvantages
bumblebee, using “render offload” Uses the dGPU only when requested, allows power saving, is the Manjaro default Some overhead so lower raw performance
PRIME, using “output offload” Uses the dGPU directly, better raw performance dGPU and iGPU both powered on constantly, needs manual configuration
optimus-manager Easy switch between iGPU and dGPU usage Very much pre-release and under rapid development
video-hybrid-intel-nvidia-440xx-prime NVIDIA’s updated render offloading. Works perfectly on supported hardware. Only supported by 440xx and newer driver series

*Note: The easiest way to switch to the dGPU is to disable the iGPU in your BIOS. If that option is available all of this is unnecessary and you can just go ahead and use the NVIDIA driver.*

*Note 2: While the package optimus-manager offers a semi-automated way of switching configuration between iGPU and dGPU, as of 15 December 2018 (UTC) it is still under heavy development.*

*Disclaimer: NVIDIA’s support for Optimus under Linux is a right mess. There are many different hardware configurations so while this approach works for many it may not work for you. Read the Arch wiki for some options.*

Step 1: remove bumblebee

If you installed with the non-free driver option mhwd will have set up bumblebee for you. This will get in the way so the first step is to remove it. Use the mhwd command-line or simply remove it via Manjaro Settings Manager.

Step 2: install the NVIDIA driver

Use mhwd or MSM to install the nvidia driver in the normal way.

Step 3: break fix mhwd‘s configuration

mhwd does the sensible thing and puts configuration in place as though the NVIDIA GPU was the only device available. We need to change this setup so PRIME will work.

Step 3.1: set up a new Xorg configuration

Firstly, remove /etc/X11/xorg.conf.d/90-mhwd.conf and replace it with:

 /etc/X11/xorg.conf.d/optimus.conf

While the BusID value above should be correct for most Optimus laptops you should check your values with lspci | grep -E "VGA|3D" .

Step 3.2: Refine blacklisting

PRIME relies on nvidia-drm and mhwd puts it in a blacklist by default, but to ensure the nvidia kernel module will load we still need to blacklist certain other modules. Therefore, you’ll have to do some editing of the files in /etc/modprobe.d.

To remove the existing blacklist, edit, move or remove any related mhwd-* files in /etc/modprobe.d/, e.g.

ls /etc/modprobe.d/mhwd*
sudo rm /etc/modprobe.d/mhwd-gpu.conf
sudo rm /etc/modprobe.d/mhwd-nvidia.conf

The end result must include a blacklist of the following modules, e.g. in /etc/modprobe.d/nvidia.conf:

blacklist nouveau
blacklist nvidiafb
blacklist rivafb

Step 4: enable nvidia-drm.modeset

Create a new file,

 /etc/modprobe.d/nvidia-drm.conf

Step 5: Set the output source for your DM.

This is the most complicated part and the one which will take longest to get right. If you reboot now, your DM will load but display on the wrong output; the laptop display will be entirely blank (powered off).

We need to set a startup script to load the correct settings while the DM is loading.

NOTE: Use only the part which matches your current DM. You don’t need to do all of these.

For LightDM

e.g. Xfce edition

Create a new file with the following content:

 /usr/local/bin/optimus.sh

Make sure to set it world read-execute, chmod a+rx /usr/local/bin/optimus.sh.

Now you have to get this to load in LightDM’s startup sequence, so edit /etc/lightdm/lightdm.conf and set this at the [Seat:\*] section

display-setup-script=/usr/local/bin/optimus.sh

For GDM

e.g. GNOME edition

Create a new file,

 /usr/local/share/optimus.desktop

and link it into place so it starts with GDM and on login

sudo ln -s /usr/local/share/optimus.desktop /usr/share/gdm/greeter/autostart/optimus.desktop
sudo ln -s /usr/local/share/optimus.desktop /etc/xdg/autostart/optimus.desktop

You’ll also have to use X, not Wayland.

For SDDM

e.g. KDE edition

Create a new file,

 /usr/share/sddm/scripts/Xsetup

and make it executable.

Step 6: reboot

If everything is set correctly, when you reboot your DM will load, you can log in, and:

$ glxinfo | grep -i vendor
server glx vendor string: NVIDIA Corporation
client glx vendor string: NVIDIA Corporation
OpenGL vendor string: NVIDIA Corporation

Hooray! You’re running X via the dGPU not the iGPU!

If you have multiple displays you may have to configure their layout again in the normal way.

References and reading


文章作者: Justice
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Justice !
  目录