Switching between Intel and NVIDIA graphics processors on a laptop with NVIDIA Optimus hardware running Gentoo Linux

I have a Clevo W230SS laptop with an Intel HD 4600 IGP and an NVIDIA GeForce GTX 860M GPU, running KDE in Gentoo Linux. I could have installed Bumblebee in order to use the NVIDIA GPU with selected applications, but I decided instead to switch manually between the graphics processors. To achieve this I created two Desktop Configuration Files with nice icons on my Desktop, to launch two simple Bash scripts which configure my installation for the desired graphics processor, which will then be used when I next login to KDE. All the necessary files are listed below for Gentoo Linux. You would need to modify them if you are using a different distribution.

The Bash script ~/intel.sh contains the following:

#!/bin/bash
echo
echo "Your installation is currently configured to use the following graphics processor:"
echo
GPU=`eselect opengl list | grep \* | awk '{ print $2 }'`
if [ "$GPU" = "xorg-x11" ]; then
  echo "Intel HD 4600 Integrated Graphics Processor"
  echo
  echo "You do not need to do anything. Please close this window."
elif [ "$GPU" = "nvidia" ]; then
  echo "NVIDIA GeForce GTX 860M"
  echo
  echo "This script will configure your installation to use the"
  echo "Intel HD 4600 Integrated Graphics Controller all the time."
  echo
  echo "Enter your own password."
  echo
  sudo eselect opengl set xorg-x11
  sudo cp /home/fitzcarraldo/Xsetup.intel /usr/share/config/kdm/Xsetup
  sudo cp /home/fitzcarraldo/xorg.conf.intel /etc/X11/xorg.conf
  echo
  echo "Now you should logout to restart X Windows."
fi

The Bash script ~/nvidia.sh contains the following:

#!/bin/bash
echo
echo "Your installation is currently configured to use the following graphics processor:"
echo
GPU=`eselect opengl list | grep \* | awk '{ print $2 }'`
if [ "$GPU" = "nvidia" ]; then
  echo "NVIDIA GeForce GTX 860M"
  echo
  echo "You do not need to do anything. Please close this window."
elif [ "$GPU" = "xorg-x11" ]; then
  echo "Intel HD 4600 Integrated Graphics Processor"
  echo
  echo "This script will configure your installation to use the NVIDIA GeForce GTX 860M GPU all the time."
  echo
  echo "Enter your own password."
  echo
  sudo eselect opengl set nvidia
  sudo cp /home/fitzcarraldo/Xsetup.nvidia /usr/share/config/kdm/Xsetup
  sudo cp /home/fitzcarraldo/xorg.conf.nvidia /etc/X11/xorg.conf
  echo
  echo "Now you should logout to restart X Windows."
fi

I created two files, xorg.conf.intel and xorg.conf.nvidia, in my home directory. The corresponding Bash script copies the applicable file to the directory /etc/X11/ to create an xorg.conf file with the correct contents for the graphics processor.

The file ~/xorg.conf.intel contains the following:

Section "Device" 
   Identifier  "Intel Graphics" 
   Driver      "intel" 
   Option      "AccelMethod" "sna" 
   Option      "TearFree" "true" 
EndSection

The file ~/xorg.conf.nvidia contains the following:

Section "ServerLayout"
    Identifier     "Layout0"
    Screen      0  "nvidia" 0 0
    Inactive       "intel"
EndSection

Section "Monitor"
    Identifier     "Monitor0"
    Option         "DPMS"
EndSection

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

Section "Device"
    Identifier     "intel"
    Driver         "modesetting"
    BusID          "PCI:0:2:0"
EndSection

Section "Screen"
    Identifier     "nvidia"
    Device         "nvidia"
    Monitor        "Monitor0"
    DefaultDepth    24
    Option         "UseDisplayDevice" "none"
    SubSection     "Display"
        Depth       24
#        Modes      "nvidia-auto-select"
        Virtual     1920 1080
    EndSubSection
EndSection

Section "Screen"
    Identifier     "intel"
    Device         "intel"
    Monitor        "Monitor0"
EndSection

I created two files, Xsetup.intel and Xsetup.nvidia, in my home directory. The corresponding Bash script copies the applicable file to the directory /usr/share/config/kdm/ to create an Xsetup file with the correct contents for the graphics processor.

The file ~/Xsetup.intel is listed below. As you can see, everything is commented out in the file.

#! /bin/sh
# Xsetup - run as root before the login dialog appears

#xconsole -geometry 480x130-0-0 -notify -verbose -fn fixed -exitOnFail -file /dev/xconsole &

The file Xsetup.nvidia is listed below. As you can see, it contains two xrandr commands.

#! /bin/sh
# Xsetup - run as root before the login dialog appears

#xconsole -geometry 480x130-0-0 -notify -verbose -fn fixed -exitOnFail -file /dev/xconsole &
xrandr --setprovideroutputsource modesetting NVIDIA-0
xrandr --auto

I downloaded nice icons from the Web for the Desktop Configuration Files and put them in the directory ~/Pictures/Icons/.

The file ~/Desktop/Select\ Intel\ HD\ Graphics.desktop is listed below.

[Desktop Entry]
Comment[en_GB]=Run a script to configure your installation to use Intel Integrated Graphics when you restart X Windows
Comment=Run a script to configure your installation to use Intel Integrated Graphics when you restart X Windows
Exec=/home/fitzcarraldo/intel.sh
GenericName[en_GB]=Configure your installation to use Intel HD Graphics
GenericName=Configure your installation to use Intel HD Graphics
Icon=/home/fitzcarraldo/Pictures/Icons/intel_hd_graphics_icon.png
MimeType=
Name[en_GB]=Intel HD Graphics
Name=Intel HD Graphics
Path=
StartupNotify=true
Terminal=true
TerminalOptions=\s--noclose
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=none
X-KDE-SubstituteUID=false
X-KDE-Username=fitzcarraldo

The file ~/Desktop/Select\ NVIDIA\ GPU.desktop is listed below.

[Desktop Entry]
Comment[en_GB]=Run a script to configure your installation to use the NVIDIA GeForce GTX 860M GPU when you restart X Windows
Comment=Run a script to configure your installation to use the NVIDIA GeForce GTX 860M GPU when you restart X Windows
Exec=/home/fitzcarraldo/nvidia.sh
GenericName[en_GB]=Configure your installation to use the NVIDIA GeForce GTX 860M GPU
GenericName=Configure your installation to use the NVIDIA GeForce GTX 860M GPU
Icon=/home/fitzcarraldo/Pictures/Icons/nvidia_icon.png
MimeType=
Name[en_GB]=NVIDIA GPU
Name=NVIDIA GPU
Path=
StartupNotify=true
Terminal=true
TerminalOptions=\s--noclose
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=none
X-KDE-SubstituteUID=false
X-KDE-Username=fitzcarraldo

Don’t forget to make all the Bash script files and .desktop files executable. All I have to do is double-click on either icon on the Desktop and a Konsole window pops up and tells me what to do. Next time I log in, the graphics processor I selected will be active.

My thanks go to Gentoo Linux user arthanis for showing me the way in the Gentoo Forums thread [SOLVED] Optimus and Nvidia.

Update (September 24, 2016): I upgraded xorg-server from 1.17.4 to 1.18.4 today and had to change the following line in /etc/X11/xorg.conf:

Screen 0 "nvidia" 0 0

to:

Screen 1 "nvidia" 0 0

See the Gentoo Forums post What’s up with xorg-server 1.18 and Optimus? for further details

About Fitzcarraldo
A Linux user with an interest in all things technical.

9 Responses to Switching between Intel and NVIDIA graphics processors on a laptop with NVIDIA Optimus hardware running Gentoo Linux

  1. Richard Priester says:

    Hallo Fitz.
    Really sorry to bother you. I have found your description to switch from Intel to NVidia (Optimus) with scripts via Google search. I’m using Sabayon 15.08 with KDE 4.14.10.

    I have a core i7-4510 with build in HD4400 and a second NVidia Geforce 840M.
    I copied your files onto the appropriate locations on my notebook and changed my username in the batches only. I could start nvidia.sh, and received the massage to log out. I logged out then and expected the Login screen (again). Instead a new login window the screen went black and blank with a blinking cursor up left in first line.

    As root I have checked, xorg.conf.nvidia was copied to /etc/X11/xorg.conf and and also /usr/share/config/kdm/Xsetup (including xrandr commands).
    My Optimus works fine with bumblebee (optirun, primusrun). Here I can switch and run (e.g.) Unigine Heaven much faster than with Intel.
    So, what can I do else to run pure nvidia?
    Thank you for your any support, really.

    -Linuxfluesterer (I’m member of Sabayon forum)

  2. Richard Priester says:

    Thanks for your reply, Fitz. I have checked: This is (I assume) same as you have:
    00:02.0 VGA compatible controller: Intel Corporation Haswell-ULT Integrated Graphics Controller (rev 0b)
    and
    01:00.0 3D controller: NVIDIA Corporation GM108M [GeForce 840M] (rev ff)

    As of now, I have no second monitor connected on HDMI, which I finally want to run with the NVidia graphics.

    -Linuxfluesterer

    • Fitzcarraldo says:

      You need to have your kernel configured appropriately, and also the correct parameters specified in the kernel boot line in the file grub.cfg. See under X Windows and NVIDIA Optimus in my earlier post A new laptop.

      Below is an excerpt from the kernel configuration in my Gentoo installation, although I have no idea if all of them are relevant here or if the list is exhaustive.

      CONFIG_DRM_KMS_HELPER=y
      CONFIG_DRM_KMS_FB_HELPER=y
      CONFIG_DRM_I915=y
      CONFIG_DRM_I915_FBDEV=y
      CONFIG_DRM_I915_KMS=y
      CONFIG_SND_HDA_I915=y
      CONFIG_AGP=y
      CONFIG_AGP_INTEL=y
      CONFIG_TREE_RCU=y
      CONFIG_RCU_STALL_COMMON=y
      CONFIG_RCU_FANOUT=64
      CONFIG_RCU_FANOUT_LEAF=16
      CONFIG_RCU_FAST_NO_HZ=y
      CONFIG_RCU_CPU_STALL_TIMEOUT=21
      

      I don’t know what Sabayon Linux has, but in my Gentoo Linux installation I have the following in the file /etc/default/grub (the file /etc/default/sabayon-grub in your case) which I used to generate the file grub.cfg:

      GRUB_CMDLINE_LINUX_DEFAULT="drm_kms_helper.edid_firmware=edid/1920x1080_Clevo_W230SS.bin i915.modeset=1 rcutree.rcu_idle_gp_delay=1"
      

      I created the EDID firmware file by following the instructions from the Gentoo Wiki article (see my above-mentioned post for the link).

  3. Richard Priester says:

    Hallo Fitz.
    After I was busy in last days, I have tried to understand the way to create my own EDID binary fimware file today. I’m honest: That’s too complicated, I don’t get it (for my Sabayon). So, I’ll stop here. Maybe in future there will be a simpler way in Linux Kernel or a tool similar to nvidiasetup to activate and adopt the necessary steps. I mean, if bumblee switching already works, then it should be possible at all in general. Or am I wrong?
    At any rate, thank you for your support.
    -Linuxfluester

    • Fitzcarraldo says:

      Why do you think it is difficult to create the EDID firmware file? According to the Gentoo Wiki article I mentioned earlier, it should be straightforward if the driver can actually read an EDID from the monitor:

      # emerge --ask x11-misc/read-edid
      # get-edid > /lib/firmware/edid/1920x1080_Clevo_W230SS.bin

      As I have a Clevo W230SS laptop, I named the firmware file 1920x1080_Clevo_W230SS.bin but it could have any name.

      The package x11-misc/read-edid exists in Sabayon Linux, so you should be able to generate the firmware file:

      # equo install read-edid
      # mkdir /lib64/firmware/edid
      # get-edid > /lib/firmware/edid/any_name_you_want.bin

      • Richard Priester says:

        Thank you again, Fitz.
        Exactly, the edid is not the real problem. It is to compile the Kernel with my Sabayon. I have tried some month before, but there were only errors, I even could not execute “make menuconfig” so that I could change the .config parameters. And yes, I have downloaded “sys-kernel/sabayon-sources” before.Many years ago (maybe 10?) I have compiled some own kernels with SuSE distros. That worked. But with Sabayon I haven’t had that luck.
        This is the complicated part I really can not solve, and it will have to be repeated then for every patch or mainline of a new kernel.
        In my Sabayon, /usr/src/linux-sabayon-4.1.0/.config the differences to your settings for grub-changes are:
        CONFIG_DRM=m
        CONFIG_DRM_KMS_HELPER=m
        # CONFIG_DRM_LOAD_EDID_FIRMWARE is not set
        CONFIG_DRM_I915=m
        CONFIG_SND_HDA=m
        CONFIG_AGP=m
        CONFIG_AGP_INTEL=m
        # CONFIG_RCU_FANOUT_NO_HZ is not set
        CONFIG_RCU_CPU_STALL_TIMEOUT=60
        …the other lines are identical to yours…

        -Linuxfluesterer

  4. Pingback: Getting KDE Plasma 5 to work with the NVIDIA closed-source driver in Gentoo Linux | Fitzcarraldo's Blog

  5. Pingback: Text too small in X Windows when using nvidia-drivers | Fitzcarraldo's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: