Make.conf/VIDEO CARDS/Daniel's Simple Hybrid Graphics

From Funtoo
Jump to: navigation, search

This page documents how I, Daniel Robbins set up my Thinkpad P1 so that I can switch between Intel integrated graphics and NVIDIA graphics by simply entering the BIOS and toggling the appropriate setting.

Before We Start

Before we get started, it's important to mention that this configuration is for Funtoo Linux 1.4 or later, and it is using the desktop mix-in, which will auto-enable Intel integrated graphics. You will need to manually enable NVIDIA proprietary drivers via use of the gfxcard-nvidia mix-in. See Make.conf/VIDEO_CARDS#Funtoo Graphics Mix-Ins and follow the steps listed for the gfxcard-nvidia mix-in. You'll need to perform an emerge -auDN nvidia-kernel-modules @world among other things.

Also note that as I go through the steps below, I already have my system updated and all the graphical stuff like GNOME ready to use/test.

First, Intel

OK, let's get started by setting up Intel graphics. For this configuration, we will set up the system to use Intel graphics only, and the NVIDIA graphics chip will lie dormant.

Configure your BIOS to boot using hybrid graphics. This will keep the Intel integrated graphics enabled as the primary graphics device, but the NVIDIA graphics chip will still be visible using lspci. Now, we will try to configure X:

root # X -configure

A sample xorg.conf.new file will be written to /root. This file requires some editing -- it will not work as-is. You will want to edit it and remove all references to the "nvidia" driver and associated screens, etc. This is the file that I ended up with and it should likely work for your purposes as well:

   /root/xorg.conf.new
Section "ServerLayout"
        Identifier     "X.org Configured"
        Screen      0  "Screen0" 0 0
        InputDevice    "Mouse0" "CorePointer"
        InputDevice    "Keyboard0" "CoreKeyboard"
EndSection

Section "Files"
        ModulePath   "/usr/lib64/xorg/modules"
        FontPath     "/usr/share/fonts/misc/"
        FontPath     "/usr/share/fonts/TTF/"
        FontPath     "/usr/share/fonts/OTF/"
        FontPath     "/usr/share/fonts/Type1/"
        FontPath     "/usr/share/fonts/100dpi/"
        FontPath     "/usr/share/fonts/75dpi/"
EndSection

Section "Module"
        Load  "glx"
EndSection

Section "InputDevice"
        Identifier  "Keyboard0"
        Driver      "kbd"
EndSection

Section "InputDevice"
        Identifier  "Mouse0"
        Driver      "mouse"
        Option      "Protocol" "auto"
        Option      "Device" "/dev/input/mice"
        Option      "ZAxisMapping" "4 5 6 7"
EndSection

Section "Monitor"
        Identifier   "Monitor0"
        VendorName   "Monitor Vendor"
        ModelName    "Monitor Model"
EndSection

Section "Monitor"
        Identifier   "Monitor1"
        VendorName   "Monitor Vendor"
        ModelName    "Monitor Model"
EndSection

Section "Device"
        Identifier  "Card0"
        Driver      "intel"
EndSection

Section "Screen"
        Identifier "Screen0"
        Device     "Card0"
        Monitor    "Monitor0"
        SubSection "Display"
                Viewport   0 0
                Depth     1
        EndSubSection
        SubSection "Display"
                Viewport   0 0
                Depth     4
        EndSubSection
        SubSection "Display"
                Viewport   0 0
                Depth     8
        EndSubSection
        SubSection "Display"
                Viewport   0 0
                Depth     15
        EndSubSection
        SubSection "Display"
                Viewport   0 0
                Depth     16
        EndSubSection
        SubSection "Display"
                Viewport   0 0
                Depth     24
        EndSubSection
EndSection

Copy this file to /etc/X11 and ensure that your Intel integrated graphics are working properly:

root # cp /root/xorg.conf.new /etc/X11/xorg.conf
root # /etc/init.d/xdm start

Once satisfied with the results, we're almost ready to move on to NVIDIA. But first, let's review what we did here. In hybrid graphics mode, both the Intel and NVIDIA graphics devices are visible to X. We have this special configuration which is set up to only use Intel, even though X will "see" the NVIDIA card as well. This is what we want. In hybrid mode, we'll just be using Intel for power savings and your NVIDIA graphics chip will lie dormant.

Finding an Intel Snippet

Before playing with NVIDIA graphics, type lspci and find the line that lists your Intel graphics chip. It will typically say something like Intel UHD Graphics or something similar. Copy this string to a temporary file -- we will use a snippet from it later.

Now, NVIDIA

With Funtoo Linux 1.4+, as long as you have properly enabled NVIDIA graphics via the gfxcard-nvidia mix-in and followed all associated setup steps, your X server should happily start without any xorg.conf in /etc/X11. In fact, we don't want our Intel-focused xorg.conf in /etc/X11 so let's move it out of the way:

root # mv /etc/X11/xorg.conf /root/xorg.conf.intel

Now, reboot your system and select Dedicated Graphics in the BIOS, and start up Funtoo again. Your X server should now be working properly, now using the proprietary NVIDIA driver. Confirm this is the case, and now we are ready for the next step.

Dynamic Switching

Now that we have both Intel and NVIDIA working separately, it is now time to do a little magic so we can simply flip the switch in the BIOS to select our preferred graphics chip and have Funtoo do the rest. We're going to take advantage of this fact -- when we are in dedicated (NVIDIA) graphics mode, the Intel integrated graphics chip is not visible using lspci. We'll create the following script called /etc/init.d/hybrid-mode:

   /etc/init.d/hybrid-mode
#!/sbin/openrc-run

name="hybrid-mode"
description="Dynamically configures video to our liking"
INTEL_LSPCI_SNIPPET="UHD Graphics"

depend() {
	before xdm
}

start() {
	einfo "Configuring xorg for current video"
	if [ -n "$(lspci | grep \"${INTEL_LSPCI_SNIPPET}\")" ]; then
		cp /root/xorg.conf.intel /etc/X11/xorg.conf
	else
		rm -f /etc/X11/xorg.conf
                modprobe nvidia nvidia_modeset nvidia_drm
	fi
	eend 0
}

Set the value of INTEL_LSPCI_SNIPPET to a portion of the Intel snippet text you saved earlier (it just needs to be a unique substring of this text -- make sure you get spelling and capitalization correct!) Next, we will blacklist nvidia modules so they do not load automatically when Intel is enabled -- as you can see, our script takes care of manually loading the modules for us.

   /etc/modprobe.d/nvidia-blacklist
blacklist nvidia
blacklist nvidia_modeset
blacklist nvidia_drm

Now, make the script executable and add it to your default runlevel:

root # chmod +x /etc/init.d/hybrid-mode
root # rc-update add hybrid-mode default

Now, this script will run at every boot. If it sees the snippet in lspci, it will enable Intel graphics, otherwise NVIDIA. You should now be able to toggle hybrid and dedicated graphics using the BIOS and have Funtoo adjust accordingly. Enjoy!