Switching between nVidia and Intel GPUs graphics on Linux

Mnemonic

Bronze Level Poster
The biggest problem I have found with Linux on my Defiance II (Skylake) laptop is the inability to reliably switch between the nVidia graphics and Intel HD graphics. With the latest nVidia drivers (364.12), while you can select the Intel HD graphics (by going in to the BIOS) and use the Intel driver, the nVidia GPU is still powered on, which means battery drain, heat, and possibly some fan noise.

I have finally figured out a way to reliably switch between the nVidia and Intel GPUs on my Defiance II running Ubuntu 15.10 (this should work for other Skylake laptops that also have nVidia graphics). Using these instructions, if you go in to the BIOS and set the graphics to "MSHYBRID", then the nVidia GPU will be disabled and powered down. If you select the "NVIDIA" option, then the nVidia GPU will be used. You should make sure you have a fairly up to date kernel installed too (I'm currently running the 4.5 kernel).

The outline of the steps are as follows:

1. Install the latest nVidia driver:

Code:
sudo apt-add-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo apt-get install nvidia-364

2. Create a script to turn the nVidia card on or off:

Code:
#!/bin/bash

is_loaded() {
  lsmod | grep -wo nvidia > /dev/null
  return $?
}

on() {
  is_loaded
  ret=$?
  if [ $ret -eq 1 ]; then
    tee /proc/acpi/bbswitch <<<ON > /dev/null
    modprobe nvidia_drm nvidia_modeset nvidia_uvm nvidia
  fi
}

off() {
  is_loaded
  ret=$?
  if [ $ret -eq 0 ]; then
    rmmod nvidia_drm nvidia_modeset nvidia_uvm nvidia
    tee /proc/acpi/bbswitch <<<OFF > /dev/null
  fi
}

status() {
  cat /proc/acpi/bbswitch | grep -oE '[^ ]+$'
}

case $1 in
  on|off|status)
    if [ -f /proc/acpi/bbswitch ]; then 
      "$1"
    fi
    ;;
  *) echo "Usage: [sudo] nvidia {on|off|status}"
esac

Save the script as "nvidia", and make sure it's executable:

Code:
chmod +x ./nvidia

Then move the script to /usr/local/bin/:

Code:
sudo mv ./nvidia /usr/local/bin/

3. In your /etc/rc.local, cut power to the nVidia GPU if in MSHYBRID mode:

Add the following to /etc/rc.local (just before the "exit" line at the very end):

Code:
if [ -f /proc/acpi/bbswitch ]; then
  /usr/local/bin/nvidia off
fi


With the above in place, you will be able to switch between the Intel and nVidia GPUs by going in to the BIOS and selecting which GPU you want to use. There is no need to select anything in NVIDIA Prime.

If you're in MSHYBRID mode, the script in step 2 makes the following commands available:

sudo nvidia off (turn off the nVidia card - useful if you're running Bumblebee, because Bumblebee doesn't turn the nVidia card back off when you close an application that was opened with optirun).
sudo nvidia on (turn on the nVidia card - again, useful if you intend to use Bumblebee, because optirun will not work if the nVidia card is powered off).
nvidia status (return the status of the nVidia card - ON means it's powered on, OFF means it's powered off).

Hope this helps.
 
Last edited:

JimmyVD

New member
I've not tried this yet, but I've got a Lafitte II on the way and I'm sure this will be massively helpful.

Thanks very much!
 
Top