[HowTo] Set up PRIME with NVIDIA proprietary driver

nvidia
optimus
prime

#1

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.

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

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.

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
Section "Module"
    Load "modesetting"
EndSection

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

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
options nvidia_drm modeset=1

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.

LightDM

Create a new file with the following content:

/usr/local/bin/optimus.sh
#!/bin/sh

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

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

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

GDM

Create a new file,

/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

Or using the above script,

[Desktop Entry]
Type=Application
Name=Optimus
Exec=/usr/local/bin/optimus.sh
NoDisplay=true
X-GNOME-Autostart-Phase=DisplayServer

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.

SDDM

Create a new file,

/usr/share/sddm/scripts/Xsetup
xrandr --setprovideroutputsource modesetting NVIDIA-0
xrandr --auto

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


Impossible to boot after installing Nvidia drivers (gtx1060 laptop)
Nvidia non-free driver prevents me from loading Manjaro
Performance regression with NVIDIA 390.25
GNOME freezing waking up from suspend, now even after restart GUI wont start
KMail corrupt after upgrades
No display after installation on Nvidia drivers
Issues Installing Proprietary Nvidia drivers
Using Only Nividia Card on Optimus System
HDMI port not working on Dell Inspiron 7567
Subpar browser experience
Second monitor does not work Alienware 13 i3 nvidia / intel
Dell XPS 15 (9550) Suspend to ram once; but not a second time
Multiple monitor dual gpu issues
Dell G7 i7 w GTX1060 6gb
Enabling GPU in Steam Games - Crash on Startup
Need help with my laptop's recovery and driver installation
Dell G7 i7 w GTX1060 6gb
Issues setting up PRIME on a laptop
Random crashes w/flickering black bars
Bad Hardware Support for new devices (laptops) and nVidia cards (mobile variant)
Nvidia proprietary drivers not installable
Install Works, but black screen after login
Bad Hardware Support for new devices (laptops) and nVidia cards (mobile variant)
Black screen after login each time after installing nvidia driver
Dell XPS 15 9570 Discrete Graphics
Unable to install Manjaro, problem with dual graphics
Lock Screen Freeze Manjaro 18 XFCE with Nvidia Prime
Nvidia driver problems
Is it possible to use Prime like in Ubuntu?
[SOLVED] System hang when using mhwd on Entroware Kratos
Optimus laptops & Steam Play (Proton), any news?
[Steam][Bumblebee] Cs:go/dota2 MX940
Manjaro uses integrated graphics card instead of dedicated
[Testing Update] 2018-02-14 - Kernels, Plasma, KDE Framework, Themes
When will PRIME support be added to mhwd?
Cannot manage to have a external monitor and laptop display working at the same time
Installing manjaro linux to external hdd
[Solved] Open GLX Extention not supported by display
System76 & Manjaro
HDMI port not working on Dell Inspiron 7567
[Solved] "NVIDIA X driver not loaded" when running nvidia-settings
[SOLVED] Nvidia Prime Issue With Inspiron 15 GTX 1050TI
[SOLVED] Nvidia Prime Issue With Inspiron 15 GTX 1050TI
NVIDIA Setting Restore on Boot
[SOLVED] Nvidia Prime Issue With Inspiron 15 GTX 1050TI
BIOS allows discrete and hybrid nvidia, but how does manjaro swap?
BIOS allows discrete and hybrid nvidia, but how does manjaro swap?
NVIDIA-bumblebee stops working, needs to be reinstalled
Nvidia-intel hybrid performance issue
Nvidia-prime - manjaro
Dell laptop -- hybrid Intel/nVidia 1050Ti -- dual-monitor issues
Dell laptop -- hybrid Intel/nVidia 1050Ti -- dual-monitor issues
Dell laptop -- hybrid Intel/nVidia 1050Ti -- dual-monitor issues
Intel HD 630 and Nvidia problem
NVIDIA issue (hybrid graphics and non-free driver)
Primusrun very slow?
Trouble installing Manjaro on System with Nvidia GTX 1050 Ti
[SOLVED] Bad performance with Steam game using Nvidia bumblebee
Lenovo Ideapad 330-17IKB randomly freezes
GDM + Bumblebee = black login screen
Can't make Intel + Nvidia gpu's works
Can't boot to manjaro after installing NVIDIA drivers
Fragen zu Intel/Nvidia Hybrid Grafik und nonfree Treiber
Only boot on dual screen
External monitor does not work (hdmi)
(940mx + intel ) Nonfree driver issue ! Looking for helps :(
[SOLVED] UI of some apps not responding but I know they're working and "could not enable discrete graphics"
Manjaro stopped booting
Xorg can't find GLX for Nvidia
Bumblebee: optirun working, primusrun not working
Multiple issues with laptop's GPU
MCE and other problems
[SOLVED] New laptops i7-8750
Deepin Screen Tearing with NVIDIA Drivers - HP Envy
Start with black screen after non-free nvidia-driver installation
Boot Stuck at TPM Error
Start with black screen after non-free nvidia-driver installation
Video-Hybrid-Intel-Nvidia-Bublebee External Display
Video-Hybrid-Intel-Nvidia-Bublebee External Display
Dell XPS 15 9560 - Dual Display (HDMI Out, USB-C to HDMI Adapter Out)
[SOLVED] OpenGL not detected with primusrun and NVIDIA 390xx
Old desktop (6 years) and nvidia GeForce GT 730
[TOC] Nvidia GPU related Tutorials
Nvidia prime fail to load nouveau driver
Setting up PRIME reduces resolution to 960x540 (16:9)
Prime profiles missing?
Use so called prime instead of bumblebee for nvidia optimus based laptops
Problem with Manjaro on MSI GP72MVR 7RFX gaming laptop
A question about this tutorial (prime with nvidia driver)
How to only use Nvidia drivers?
Fan at maximum speed
Problem installing NVIDIA Drivers - without NVIDIA Optimus
Need help for Nvidia Prime setup
OS not booting. Lighted black screen with white underscore
OS not booting. Lighted black screen with white underscore
Running dedicated GPU Card in my laptop
Manjaro hangs after login on MSI GS63VR
Black screen after installing nvidia drivers - laptop dont work with nvidia drivers only!
Which GPU driver should I use?
Getting stuck after entering login details
Artifact card game on dedicated graphics
[bumblebee, opti/primusrun] error: [XORG] (EE) kbd: <default keyboard>: failed to set us as foreground pgrp (Inappropriate ioctl for device)
Where to find resources for newbies?
Where to find resources for newbies?
Nvidia prime leads to black screen
Nvidia prime leads to black screen
[SOLVED] Issue with Nvidia Graphics
Hardware Detection Issue
XCOM 2 not using NVIDIA Drivers
Dell XPS 15 9570 with PRIME -> OS Freezes on wake up
Hardware Detection Issue
How to install nvidia drivers only?
How to install nvidia drivers only?
Unable to boot into system after installation
Optimus setup with nouveau works perfectly on live ISO but doesn't work on a fresh installation
Cannot install Manjaro with the "nonfree" driver option
#3

Fantastic writeup! More people should be aware of the advantages. Once you leave bumblebee behind you never look back.


#4

A bit off-topic, any idea when 390 will hit Stable branch?


#5

With the next stable update. :wink:


Failed to create Vulkan device (Rise of the Tomb Raider)
#6

Nice.


#7

Will boot parameters replace the need for a startup script and modprobe configuration?

GRUB_CMDLINE_LINUX_DEFAULT= "nvidia-drm.modeset=1


#8

No.

Yes. It’s a different way of setting the same option.


#9

Are these extensions enabled by default? Are they even necessary?

Section "Extensions"
    Option "Composite" "Enable"
    Option "RENDER" "Enable"
EndSection

#10

you can try (“Composite” “Enable” should work) and check xorg.log, it will say enabled or unknown option or something like that

e.g:

grep Composite .local/share/xorg/Xorg.0.log
[    12.470] (**) Extension "Composite" is enabled
[    12.803] (II)         Composite (RENDER acceleration)

be aware that xorg.log could be either in .local or in /var (depends if started as user or with root permissions)
if unsure check timestamp


#11

I was curious if these options are already on default and if setting through the extensions section is redundant. Also, for a compositing window manager like Cinnamon, would it even have an effect to enable through Xorg?

I have it set to run as root to ensure KMS is active.


#12

remove it and check xorg.log if its still enabled
(or xdpyinfo|grep Composite)

I really dont know


#13

So, according to arch wiki Composite isn’t needed for window managers that already include. I assume that means Damage and Render extensions are redundant also.

ARCHWIKI: Some window managers (e.g. Compiz, Enlightenment, KWin, Marco, Metacity, Muffin, Mutter, Xfwm) do compositing on their own. For other window managers, a standalone composite manager can be used.

#14

A post was split to a new topic: When will PRIME support be added to mhwd?


#16

Can you please explain why you say so? :thinking:
From what I see in OP’s table both gpus are enabled all the time, so I assume this is a PC only solution?


#17

That’s about it.


#18

What kind of difference are we talking about? As far as I have read it is said that bumblebee is not reducing performance. So I am really curious.

Also, I assume on laptop it is a battery draining solution?


#19

It’s all hardware-dependent. The only way to see if it works better for you is to try it.

The point of the OP was not to discuss the relative merits, but the method of switching from render offload to display offload, only including a brief overview of the differences in the introduction.


#20

There is a small mistake here the option requires a boolean as it is presented here it will hang the system.

Reference:

Option "AllowEmptyInitialConfiguration" "boolean"

    Normally, the NVIDIA X driver will fail to start if it cannot find any display devices connected to the NVIDIA GPU. AllowEmptyInitialConfiguration overrides that behavior so that the X server will start anyway, even if no display devices are connected.

    Enabling this option makes sense in configurations when starting the X server with no display devices connected to the NVIDIA GPU is expected, but one might be connected later. For example, some monitors do not show up as connected when they are powered off, even if they are physically connected to the GPU.

    Another scenario where this is useful is in Optimus-based laptops, where RandR 1.4 display offload (see Chapter 32, Offloading Graphics Display with RandR 1.4) is used to display the screen on the non-NVIDIA internal display panel, but an external display might be connected later.

    Default: off. The driver will refuse to start if it can't find at least one connected display device.

@jonathon following this tutorial using my modification did work here but the system started to overheat and I had to manually run the whole startkde to the gpu which was bad. I remmember I did not have to do it on my good setup so I dont know what I did have that differs from this that you posted


#21

According to the NVIDIA driver documentation it does not require a boolean.

You might want to link to your source.


#22

http://us.download.nvidia.com/XFree86/Linux-x86/390.48/README/xconfigoptions.html

if its working without should be logged in xorg.log