diff --git a/hosts/nixos-test/default.nix b/hosts/nixos-test/default.nix index 4977426..72f5bbc 100644 --- a/hosts/nixos-test/default.nix +++ b/hosts/nixos-test/default.nix @@ -12,6 +12,8 @@ ../../modules/keymaps/us.nix ../../modules/pipewire.nix ../../modules/plasma.nix + + ../../modules # Import optional configuration # Include the results of the hardware scan. ./hardware-configuration.nix diff --git a/hosts/perso-desktop/default.nix b/hosts/perso-desktop/default.nix index 51ef4f0..e0dc2f2 100644 --- a/hosts/perso-desktop/default.nix +++ b/hosts/perso-desktop/default.nix @@ -16,6 +16,8 @@ ../../modules/bluetooth.nix ../../modules/pipewire.nix + ../../modules # Import optional configuration + # Include the results of the hardware scan. ./hardware-configuration.nix ]; diff --git a/hosts/perso-laptop/default.nix b/hosts/perso-laptop/default.nix index 7533143..1d7c270 100644 --- a/hosts/perso-laptop/default.nix +++ b/hosts/perso-laptop/default.nix @@ -16,6 +16,8 @@ ../../modules/bluetooth.nix ../../modules/pipewire.nix + ../../modules # Import optional configuration + # Include the results of the hardware scan. ./hardware-configuration.nix ]; @@ -38,4 +40,6 @@ # information bus: pci@0000:01:00.0 nvidiaBusId = "PCI:1:0:0"; }; + + customModules.gpuPassthrough.enable = true; } diff --git a/modules/default.nix b/modules/default.nix new file mode 100644 index 0000000..979c98e --- /dev/null +++ b/modules/default.nix @@ -0,0 +1,7 @@ +{ ... }: + +{ + imports = [ + ./gpuPassthrough + ]; +} \ No newline at end of file diff --git a/modules/gpuPassthrough/default.nix b/modules/gpuPassthrough/default.nix new file mode 100644 index 0000000..4ed4a54 --- /dev/null +++ b/modules/gpuPassthrough/default.nix @@ -0,0 +1,28 @@ +{ config, pkgs, lib, ... }: +with lib; +{ + options.customModules.gpuPassthrough = { + enable = mkEnableOption '' + Enable gpu passthgrouth with my custom configurations + ''; + }; + config = + let + cfg = config.customModules.gpuPassthrough; + in + mkIf cfg.enable { + programs.virt-manager.enable = true; + + virtualisation.libvirtd = { + enable = true; + + # hooks.qemu = { + # win10 = { + # prepare.begin = { + + # }; + # }; + # }; + }; + }; +} \ No newline at end of file diff --git a/modules/gpuPassthrough/scripts/alloc_hugepages.sh b/modules/gpuPassthrough/scripts/alloc_hugepages.sh new file mode 100644 index 0000000..bd96923 --- /dev/null +++ b/modules/gpuPassthrough/scripts/alloc_hugepages.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +## Load the config file +source "/etc/libvirt/hooks/kvm.conf" + +## Calculate number of hugepages to allocate from memory (in MB) +HUGEPAGES="$(($VM_MEMORY/$(($(grep Hugepagesize /proc/meminfo | awk '{print $2}')/1024))))" + +echo "Allocating hugepages..." +echo $HUGEPAGES > /proc/sys/vm/nr_hugepages +ALLOC_PAGES=$(cat /proc/sys/vm/nr_hugepages) + +TRIES=0 +while (( $ALLOC_PAGES != $HUGEPAGES && $TRIES < 1000 )) +do + echo 1 > /proc/sys/vm/compact_memory ## defrag ram + echo $HUGEPAGES > /proc/sys/vm/nr_hugepages + ALLOC_PAGES=$(cat /proc/sys/vm/nr_hugepages) + echo "Succesfully allocated $ALLOC_PAGES / $HUGEPAGES" + let TRIES+=1 +done + +if [ "$ALLOC_PAGES" -ne "$HUGEPAGES" ] +then + echo "Not able to allocate all hugepages. Reverting..." + echo 0 > /proc/sys/vm/nr_hugepages + exit 1 +fi diff --git a/modules/gpuPassthrough/scripts/cpu_mode_ondemand.sh b/modules/gpuPassthrough/scripts/cpu_mode_ondemand.sh new file mode 100644 index 0000000..07a5b4a --- /dev/null +++ b/modules/gpuPassthrough/scripts/cpu_mode_ondemand.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +## Enable CPU governor on-demand mode +cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor +for file in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do echo "ondemand" > $file; done +cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor diff --git a/modules/gpuPassthrough/scripts/cpu_mode_performance.sh b/modules/gpuPassthrough/scripts/cpu_mode_performance.sh new file mode 100644 index 0000000..7a1c821 --- /dev/null +++ b/modules/gpuPassthrough/scripts/cpu_mode_performance.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +## Enable CPU governor performance mode +cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor +for file in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor; do echo "performance" > $file; done +cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor diff --git a/modules/gpuPassthrough/scripts/dealloc_hugepages.sh b/modules/gpuPassthrough/scripts/dealloc_hugepages.sh new file mode 100644 index 0000000..887bc1e --- /dev/null +++ b/modules/gpuPassthrough/scripts/dealloc_hugepages.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +echo 0 > /proc/sys/vm/nr_hugepages diff --git a/modules/gpuPassthrough/scripts/start.sh b/modules/gpuPassthrough/scripts/start.sh new file mode 100644 index 0000000..c6a2f7a --- /dev/null +++ b/modules/gpuPassthrough/scripts/start.sh @@ -0,0 +1,34 @@ +#!/bin/bash +# Helpful to read output when debugging +set -x + +# Load variables +source "/etc/libvirt/hooks/kvm.conf" + +# Stop display manager +systemctl stop gdm.service + +# Unbind VTconsoles +echo 0 > /sys/class/vtconsole/vtcon0/bind +echo 0 > /sys/class/vtconsole/vtcon1/bind + +# Unbind EFI-Framebuffer +echo efi-framebuffer.0 > /sys/bus/platform/drivers/efi-framebuffer/unbind + +# Avoid a Race condition +sleep 5 + +# Unload all Nvidia drivers +modprobe -r nvidia_drm +modprobe -r nvidia_modeset +modprobe -r nvidia_uvm +modprobe -r nvidia + +# Unbind the GPU from display driver +virsh nodedev-detach $VIRSH_GPU_VIDEO +virsh nodedev-detach $VIRSH_GPU_AUDIO +virsh nodedev-detach $VIRSH_USB +virsh nodedev-detach $VIRSH_SERIAL_BUS + +# Load VFIO Kernel Module +modprobe vfio-pci diff --git a/modules/gpuPassthrough/scripts/stop.sh b/modules/gpuPassthrough/scripts/stop.sh new file mode 100644 index 0000000..9d7d483 --- /dev/null +++ b/modules/gpuPassthrough/scripts/stop.sh @@ -0,0 +1,35 @@ +#!/bin/bash +set -x + +# Load variables +source "/etc/libvirt/hooks/kvm.conf" + +# Unload VFIO-PCI Kernel Driver +modprobe -r vfio-pci +modprobe -r vfio_iommu_type1 +modprobe -r vfio + +# Re-Bind GPU to Nvidia Driver +virsh nodedev-reattach $VIRSH_GPU_VIDEO +virsh nodedev-reattach $VIRSH_GPU_AUDIO +virsh nodedev-reattach $VIRSH_USB +virsh nodedev-reattach $VIRSH_SERIAL_BUS + +# Rebind VT consoles +echo 1 > /sys/class/vtconsole/vtcon0/bind +echo 1 > /sys/class/vtconsole/vtcon1/bind + +# Bind EFI-Framebuffer +nvidia-xconfig --query-gpu-info > /dev/null 2>&1 +echo "efi-framebuffer.0" > /sys/bus/platform/drivers/efi-framebuffer/bind + +# Load all Nvidia drivers +modprobe nvidia_drm +modprobe nvidia_modeset +modprobe drm_kms_helper +modprobe drm +modprobe nvidia_uvm +modprobe nvidia + +# Restart Display Manager +systemctl start gdm.service diff --git a/modules/gpuPassthrough/win10/win10.xml b/modules/gpuPassthrough/win10/win10.xml new file mode 100644 index 0000000..59e8aa6 --- /dev/null +++ b/modules/gpuPassthrough/win10/win10.xml @@ -0,0 +1,247 @@ + + win10 + b3d036ee-75ff-43fe-be76-b8c5494f5dd7 + + + + + + 25165824 + 25165824 + + + + 32 + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + hvm + /usr/share/edk2-ovmf/x64/OVMF_CODE.fd + /var/lib/libvirt/qemu/nvram/win10_VARS.fd + + + + + + + + + + + + + + + + + + + + + + + + + + + + destroy + restart + destroy + + + + + + /usr/bin/qemu-system-x86_64 + + + + + +
+ + + + + + +
+ + + + + + +
+ + +
+ + +
+ + + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + +
+ + + + + +
+ + + +