Skip to content

Commit

Permalink
Removed bbswitch
Browse files Browse the repository at this point in the history
  • Loading branch information
Witko committed Jan 18, 2019
1 parent 97febf4 commit 4373500
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 52 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Created by .ignore support plugin (hsz.mobi)
nvidia-xrun.iml
.idea
test-install.sh
test-cleanup.sh
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Currently sudo is required as the script needs to wake up GPU, modprobe the nvid
* **/etc/X11/xinit/nvidia-xinitrc** - xinitrc config file. Contains the setting of provider output source
* **/etc/X11/xinit/nvidia-xinitrc.d** - custom xinitrc scripts directory
* **/etc/X11/nvidia-xorg.conf.d** - custom X config directory
* **/etc/default/nvidia-xorg** - nvidia-xrun config file
* **/usr/share/xsessions/nvidia-xrun-openbox.desktop** - xsession file for openbox
* **/usr/share/xsessions/nvidia-xrun-plasma.desktop** - xsession file for plasma
* **[OPTIONAL] ~/.nvidia-xinitrc** - user-level custom xinit script file. You can put here your favourite window manager for example
Expand Down Expand Up @@ -44,6 +45,7 @@ Also this way you can adjust some nvidia settings if you encounter issues:
# Option "AllowEmptyInitialConfiguration" "Yes"
# Option "UseDisplayDevice" "none"
EndSection
You also need to set the bus id in the `/etc/default/nvidia-xorg` file - e.g. `BUS_ID=0000:00:01.0`

## Automatically run window manager
For convenience you can create `nano ~/.nvidia-xinitrc` and put there your favourite window manager:
Expand Down
5 changes: 5 additions & 0 deletions config/nvidia-xrun
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BUS_ID=0000:00:01.0
MODULE_NVIDIA_DRM_PARAMS="modeset=1"
BUS_RESCAN_WAIT_SEC=1
MODULES_LOAD=(nvidia nvidia_uvm nvidia_modeset "nvidia_drm modeset=1")
MODULES_UNLOAD=(nvidia_drm nvidia_modeset nvidia_uvm nvidia)
19 changes: 10 additions & 9 deletions nvidia-xinitrc
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
#!/bin/bash
#!/usr/bin/env bash

userresources=$HOME/.Xresources
usermodmap=$HOME/.Xmodmap
sysresources=/etc/X11/xinit/.Xresources
sysmodmap=/etc/X11/xinit/.Xmodmap
userxinitrc=$HOME/.nvidia-xinitrc

# merge in defaults and keymaps
if [ -f ${sysresources} ]; then
if [[ -f ${sysresources} ]]; then
xrdb -merge ${sysresources}
fi

if [ -f ${sysmodmap} ]; then
if [[ -f ${sysmodmap} ]]; then
xmodmap ${sysmodmap}
fi

if [ -f "$userresources" ]; then
if [[ -f "$userresources" ]]; then
xrdb -merge "$userresources"
fi

if [ -f "$usermodmap" ]; then
if [[ -f "$usermodmap" ]]; then
xmodmap "$usermodmap"
fi

export LD_LIBRARY_PATH=/usr/lib64/nvidia/:/usr/lib32/nvidia:/usr/lib:${LD_LIBRARY_PATH}

# load additional configs
if [ -d /etc/X11/xinit/nvidia-xinitrc.d ] ; then
if [[ -d /etc/X11/xinit/nvidia-xinitrc.d ]] ; then
for f in /etc/X11/xinit/nvidia-xinitrc.d/?*.sh ; do
[ -x "$f" ] && . "$f"
[[ -x "$f" ]] && . "$f"
done
unset f
fi

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

if [ -f "$userxinitrc" ]; then
if [[ -f "$userxinitrc" ]]; then
sh ${userxinitrc} $*
else
if [ $# -gt 0 ]; then
if [[ $# -gt 0 ]]; then
sh -c "exec $*"
fi
fi
78 changes: 35 additions & 43 deletions nvidia-xrun
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
#!/usr/bin/env bash

DRY_RUN=0
function printHelp {
echo "Utility to run games and applications in separate X on discrete Nvidia graphic card"
Expand All @@ -9,7 +10,7 @@ function printHelp {
}

function execute {
if [ ${DRY_RUN} -eq 1 ]
if [[ ${DRY_RUN} -eq 1 ]]
then
echo ">>Dry run. Command: $*"
else
Expand All @@ -23,35 +24,38 @@ if [[ $EUID -eq 0 ]]; then
exit 1
fi

if [ "$1" == "-d" ]
if [[ "$1" == "-d" ]]
then
DRY_RUN=1
shift 1
fi

# load config file
. /etc/default/nvidia-xrun

# calculate current VT
LVT=`fgconsole`

# calculate first usable display
XNUM="-1"
SOCK="something"
while [ ! -z "$SOCK" ]
while [[ ! -z "$SOCK" ]]
do
XNUM=$(( $XNUM + 1 ))
SOCK=$(ls -A -1 /tmp/.X11-unix | grep "X$XNUM" )
done

NEWDISP=":$XNUM"

if [ ! -z "$*" ] # generate exec line if arguments are given
then
if [[ ! -z "$*" ]] # generate exec line if arguments are given
then
# test if executable exists in path
if [ -x "$(which $1 2> /dev/null)" ]
if [[ -x "$(which $1 2> /dev/null)" ]]
then
# generate exec line
EXECL="$(which $1)"
# test if executable exists on disk
elif [ -e "$(realpath "$1")" ]
elif [[ -e "$(realpath "$1")" ]]
then
# generate exec line
EXECL="$(realpath "$1")"
Expand All @@ -70,50 +74,38 @@ EXECL="/etc/X11/xinit/nvidia-xinitrc \"$EXECL\""
COMMAND="xinit $EXECL -- $NEWDISP vt$LVT -nolisten tcp -br -config nvidia-xorg.conf -configdir nvidia-xorg.conf.d"

# --------- TURNING ON GPU -----------
echo 'Waking up nvidia GPU'
if ! [ -f /proc/acpi/bbswitch ]
then
execute "sudo modprobe bbswitch"
if [[ ! -d /sys/bus/pci/devices/${BUS_ID} ]]; then
echo 'Rescanning PCI devices'
execute "sudo tee /sys/bus/pci/rescan <<<1"
echo "Waiting ${BUS_RESCAN_WAIT_SEC} second for rescan"
execute "sleep ${BUS_RESCAN_WAIT_SEC}"
fi
execute "sudo tee /proc/acpi/bbswitch <<<ON"

# ---------- LOADING MODULES ----------
echo 'Loading nvidia module'
execute "sudo modprobe nvidia"
echo 'Turning the card on'
execute "sudo tee /sys/bus/pci/devices/${BUS_ID}/power/control <<<on"

echo 'Loading nvidia_uvm module'
execute "sudo modprobe nvidia_uvm"

echo 'Loading nvidia_modeset module'
execute "sudo modprobe nvidia_modeset"

echo 'Loading nvidia_drm module'
execute "sudo modprobe nvidia_drm modeset=1"
# ---------- LOADING MODULES ----------
for module in "${MODULES_LOAD[@]}"
do
echo "Loading module ${module}"
execute "sudo modprobe ${module}"
done

# ---------- EXECUTING COMMAND --------
execute ${COMMAND}

# ---------- UNLOADING MODULES --------
echo 'Unloading nvidia_drm module'
execute "sudo rmmod nvidia_drm"

echo 'Unloading nvidia_modeset module'
execute "sudo rmmod nvidia_modeset"

echo 'Unloading nvidia_uvm module'
execute "sudo rmmod nvidia_uvm"

echo 'Unloading nvidia module'
execute "sudo rmmod nvidia"
for module in "${MODULES_UNLOAD[@]}"
do
echo "Unloading module ${module}"
execute "sudo modprobe ${module}"
done

# --------- TURNING OFF GPU ----------
if [ -f /proc/acpi/bbswitch ]
then
echo 'Turning off nvidia GPU'
execute "sudo tee /proc/acpi/bbswitch <<<OFF"

echo -n 'Current state of nvidia GPU: '
execute "cat /proc/acpi/bbswitch"
if [[ -f /sys/bus/pci/devices/${BUS_ID}/remove ]]; then
echo 'Removing Nvidia bus from the kernel'
execute "sudo tee /sys/bus/pci/devices/${BUS_ID}/remove <<<1"
else
echo "Bbswitch kernel module not loaded."
echo 'Enabling powersave for the PCIe controller'
execute "sudo tee /sys/bus/pci/devices/${BUS_ID}/power/control <<<auto"
fi

0 comments on commit 4373500

Please sign in to comment.