+sub parse_hostpci {
+ my ($value) = @_;
+
+ return undef if !$value;
+
+ my $res = PVE::JSONSchema::parse_property_string($hostpci_fmt, $value);
+
+ my @idlist = split(/;/, $res->{host});
+ delete $res->{host};
+ foreach my $id (@idlist) {
+ my $devs = PVE::SysFSTools::lspci($id);
+ die "no PCI device found for '$id'\n" if !scalar(@$devs);
+ push @{$res->{pciid}}, @$devs;
+ }
+ return $res;
+}
+
+sub print_hostpci_devices {
+ my ($conf, $devices, $winversion, $q35, $bridges, $arch, $machine_type) = @_;
+
+ my $kvm_off = 0;
+ my $gpu_passthrough = 0;
+
+ for (my $i = 0; $i < $MAX_HOSTPCI_DEVICES; $i++) {
+ my $id = "hostpci$i";
+ my $d = parse_hostpci($conf->{$id});
+ next if !$d;
+
+ if (my $pcie = $d->{pcie}) {
+ die "q35 machine model is not enabled" if !$q35;
+ # win7 wants to have the pcie devices directly on the pcie bus
+ # instead of in the root port
+ if ($winversion == 7) {
+ $pciaddr = print_pcie_addr("${id}bus0");
+ } else {
+ # add more root ports if needed, 4 are present by default
+ # by pve-q35 cfgs, rest added here on demand.
+ if ($i > 3) {
+ push @$devices, '-device', print_pcie_root_port($i);
+ }
+ $pciaddr = print_pcie_addr($id);
+ }
+ } else {
+ $pciaddr = print_pci_addr($id, $bridges, $arch, $machine_type);
+ }
+
+ my $xvga = '';
+ if ($d->{'x-vga'}) {
+ $xvga = ',x-vga=on' if !($conf->{bios} && $conf->{bios} eq 'ovmf');
+ $kvm_off = 1;
+ $vga->{type} = 'none' if !defined($conf->{vga});
+ $gpu_passthrough = 1;
+ }
+
+ my $pcidevices = $d->{pciid};
+ my $multifunction = 1 if @$pcidevices > 1;
+
+ my $sysfspath;
+ if ($d->{mdev} && scalar(@$pcidevices) == 1) {
+ my $pci_id = $pcidevices->[0]->{id};
+ my $uuid = PVE::SysFSTools::generate_mdev_uuid($vmid, $i);
+ $sysfspath = "/sys/bus/pci/devices/$pci_id/$uuid";
+ } elsif ($d->{mdev}) {
+ warn "ignoring mediated device '$id' with multifunction device\n";
+ }
+
+ my $j=0;
+ foreach my $pcidevice (@$pcidevices) {
+ my $devicestr = "vfio-pci";
+
+ if ($sysfspath) {
+ $devicestr .= ",sysfsdev=$sysfspath";
+ } else {
+ $devicestr .= ",host=$pcidevice->{id}";
+ }
+
+ my $mf_addr = $multifunction ? ".$j" : '';
+ $devicestr .= ",id=${id}${mf_addr}${pciaddr}${mf_addr}";
+
+ if ($j == 0) {
+ $devicestr .= ',rombar=0' if defined($d->{rombar}) && !$d->{rombar};
+ $devicestr .= "$xvga";
+ $devicestr .= ",multifunction=on" if $multifunction;
+ $devicestr .= ",romfile=/usr/share/kvm/$d->{romfile}" if $d->{romfile};
+ }
+
+ push @$devices, '-device', $devicestr;
+ $j++;
+ }
+ }
+
+ return ($kvm_off, $gpu_passthrough);
+}
+