FreeBSD and Nvidia Optimus

Posted on January 5, 2020

This post is a “step by step” tutorial to describe how I can use my Nvidia GeForce GT540M graphic card of my FreeBSD laptop, screenshot.

FreeBSD is a registered trademark of the FreeBSD Foundation, NVIDIA and Optimus are registered trademarks of the NVIDIA Corporation.

IMPORTANT! This post is obsoleted by a new official FreeBSD port x11/nvidia-hybrid-graphics.


“Step 0”
I assume you are using FreeBSD and have a Nvidia Optimus graphic card. My laptop is wiki.freebsd.org/Laptops/Acer_Aspire_5742G, at the time of writing my FreeBSD version is:

% uname -a
FreeBSD fbsd.home 13.0-CURRENT FreeBSD 13.0-CURRENT r352742 GENERIC  amd64

I can use the Intel video card by installing graphics/drm-current-kmod (you may need to install drm-fbsd12.0-kmod or drm-fbsd11.2-kmod), it is important to install drm via ports not package, following the instructions of the port and editing the Section “Device” of /etc/X11/xorg.conf:

... extra ...

Section "Device"
        ### Available Driver options are:-
        ### Values: <i>: integer, <f>: float, <bool>: "True"/"False",
        ### <string>: "String", <freq>: "<f> Hz/kHz/MHz",
        ### <percent>: "<f>%"
        ### [arg]: arg optional
        #Option     "ShadowFB"                  # [<bool>]
        #Option     "DefaultRefresh"            # [<bool>]
        #Option     "ModeSetClearScreen"        # [<bool>]
        Identifier  "Card0"
        Driver      "intel"
        BusID       "PCI:0:2:0"
EndSection

#Section "Device"
        ### Available Driver options are:-
        ### Values: <i>: integer, <f>: float, <bool>: "True"/"False",
        ### <string>: "String", <freq>: "<f> Hz/kHz/MHz",
        ### <percent>: "<f>%"
        ### [arg]: arg optional
        #Option     "ShadowFB"                  # [<bool>]
        #Option     "DefaultRefresh"            # [<bool>]
        #Option     "ModeSetClearScreen"        # [<bool>]
        #Identifier  "Card1"
        #Driver      "vesa"
        #BusID       "PCI:1:0:0"
#EndSection

... extra ...

“Step 1”

Install the Nvidia driver and the optirun utility:

% git clone https://github.com/pouya-eghbali/freebsd-nvidia-optimus.git
% cd freebsd-nvidia-optimus
% sudo mkdir /usr/ports/x11/nvidia-optimus-driver
% sudo cp -pr ./nvidia-optimus-driver /usr/ports/x11
% sudo mkdir /usr/ports/x11/nvidia-optimus-driver-390
% sudo cp -pr ./nvidia-optimus-driver-390 /usr/ports/x11
% cd /usr/ports/x11/nvidia-optimus-driver-390 ; sudo make install clean

I installed the legacy version 390, you could install the latest version:

% cd /usr/ports/x11/nvidia-optimus-driver ; sudo make install clean

Finally you may need to run:

% rehash

“Step 2”

Start the Optimus service:

% sudo /usr/local/etc/rc.d/optimus onestart

The output is:

% sudo /usr/local/etc/rc.d/optimus onestart
Starting optimus
%
X.Org X Server 1.18.4
Release Date: 2016-07-19
X Protocol Version 11, Revision 0
Build Operating System: FreeBSD 13.0-CURRENT amd64
Current Operating System: FreeBSD fbsd.home 13.0-CURRENT FreeBSD 13.0-CURRENT
r352742 GENERIC amd64
Build Date: 26 September 2019  06:56:18AM

Current version of pixman: 0.38.4
        Before reporting problems, check http://wiki.x.org
        to make sure that you have the latest version.
Markers: (--) probed, (**) from config file, (==) default setting,
        (++) from command line, (!!) notice, (II) informational,
        (WW) warning, (EE) error, (NI) not implemented, (??) unknown.
(==) Log file: "/var/log/Xorg.8.log", Time: Sun Jan  5 03:32:11 2020
(++) Using config file: "/usr/local/etc/X11/optimus.x.conf"
(==) Using system config directory "/usr/local/share/X11/xorg.conf.d"

The command could lock the shell, no problem, just type the Enter key

<Press ENTER>

%

The shell is again ready to run your commands.


Step 3

Install games/trigger-rally (http://trigger-rally.sourceforge.net/) and run it

% trigger-rally

Don’t play, close the game, the output should show:

...
Graphics: Intel Open Source Technology Center Mesa DRI Intel(R) Ironlake Mobile
Using OpenGL 2.1 Mesa 18.3.2
...

Now run

% optirun trigger-rally

Close the game, finally you can see:

...
Graphics: NVIDIA Corporation GeForce GT 540M/PCIe/SSE2
Using OpenGL 4.5.0 NVIDIA 390.116
...

Alike you could install games/sauerbraten, the info about the graphic card is at the top-left corner of the starting screen screenshot.


Step 4

Enjoy! Some screenshot:


Step 5

Github project: https://github.com/pouya-eghbali/freebsd-nvidia-optimus

FreeBSD port x11/nvidia-driver-optimus (Work In Progress): PR 192617

Problems and Troubleshoot: FreeBSD Forum “nvidia optimus driver for freebsd”

Switch off the Nvidia Optimus card: FreeBSD wiki “TuningPowerConsumption”