1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.virtualisation.kvmgt;
7 kernelPackages = config.boot.kernelPackages;
8 vgpuOptions = {
9 uuid = mkOption {
10 type = types.string;
11 description = "UUID of VGPU device. You can generate one with <package>libossp_uuid</package>.";
12 };
13 };
14in {
15 options = {
16 virtualisation.kvmgt = {
17 enable = mkEnableOption ''
18 KVMGT (iGVT-g) VGPU support. Allows Qemu/KVM guests to share host's Intel integrated graphics card.
19 Currently only one graphical device can be shared
20 '';
21 # multi GPU support is under the question
22 device = mkOption {
23 type = types.string;
24 default = "0000:00:02.0";
25 description = "PCI ID of graphics card. You can figure it with <command>ls /sys/class/mdev_bus</command>.";
26 };
27 vgpus = mkOption {
28 default = {};
29 type = with types; attrsOf (submodule [ { options = vgpuOptions; } ]);
30 description = ''
31 Virtual GPUs to be used in Qemu. You can find devices via <command>ls /sys/bus/pci/devices/*/mdev_supported_types</command>
32 and find info about device via <command>cat /sys/bus/pci/devices/*/mdev_supported_types/i915-GVTg_V5_4/description</command>
33 '';
34 example = {
35 "i915-GVTg_V5_8" = {
36 uuid = "a297db4a-f4c2-11e6-90f6-d3b88d6c9525";
37 };
38 };
39 };
40 };
41 };
42
43 config = mkIf cfg.enable {
44 assertions = singleton {
45 assertion = versionAtLeast kernelPackages.kernel.version "4.16";
46 message = "KVMGT is not properly supported for kernels older than 4.16";
47 };
48 boot.kernelParams = [ "i915.enable_gvt=1" ];
49 systemd.services = mapAttrs' (name: value:
50 nameValuePair "kvmgt-${name}" {
51 description = "KVMGT VGPU ${name}";
52 serviceConfig = {
53 Type = "oneshot";
54 RemainAfterExit = true;
55 ExecStart = "${pkgs.runtimeShell} -c 'echo ${value.uuid} > /sys/bus/pci/devices/${cfg.device}/mdev_supported_types/${name}/create'";
56 ExecStop = "${pkgs.runtimeShell} -c 'echo 1 > /sys/bus/pci/devices/${cfg.device}/${value.uuid}/remove'";
57 };
58 wantedBy = [ "multi-user.target" ];
59 }
60 ) cfg.vgpus;
61 };
62
63 meta.maintainers = with maintainers; [ gnidorah ];
64}