1 package PVE
::SysFSTools
;
8 use PVE
::Tools
qw(file_read_firstline dir_glob_foreach);
10 my $pcisysfs = "/sys/bus/pci";
11 my $pciregex = "([a-f0-9]{4}):([a-f0-9]{2}):([a-f0-9]{2})\.([a-f0-9])";
18 dir_glob_foreach
("$pcisysfs/devices", $pciregex, sub {
19 my (undef, undef, $bus, $slot, $function) = @_;
20 my $id = "$bus:$slot";
21 return if defined($filter) && $id ne $filter;
22 my $res = { id
=> $id, function
=> $function};
23 push @{$devices->{$id}}, $res;
26 # Entries should be sorted by functions.
27 foreach my $id (keys %$devices) {
28 my $dev = $devices->{$id};
29 $devices->{$id} = [ sort { $a->{function
} <=> $b->{function
} } @$dev ];
35 sub check_iommu_support
{
36 # iommu support if there is anything in /sys/class/iommu besides . or ..
37 return PVE
::Tools
::dir_glob_regex
('/sys/class/iommu/', "[^\.].*");
41 my ($filename, $buf) = @_;
43 my $fh = IO
::File-
>new($filename, "w");
46 my $res = print $fh $buf;
58 return undef if $name !~ m/^${pciregex}$/;
59 my ($domain, $bus, $slot, $func) = ($1, $2, $3, $4);
61 my $irq = file_read_firstline
("$pcisysfs/devices/$name/irq");
62 return undef if !defined($irq) || $irq !~ m/^\d+$/;
64 my $vendor = file_read_firstline
("$pcisysfs/devices/$name/vendor");
65 return undef if !defined($vendor) || $vendor !~ s/^0x//;
67 my $product = file_read_firstline
("$pcisysfs/devices/$name/device");
68 return undef if !defined($product) || $product !~ s/^0x//;
79 has_fl_reset
=> -f
"$pcisysfs/devices/$name/reset" || 0,
88 my $name = $dev->{name
};
90 my $fn = "$pcisysfs/devices/$name/reset";
92 return file_write
($fn, "1");
95 sub pci_dev_bind_to_vfio
{
98 my $name = $dev->{name
};
100 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
102 if (!-d
$vfio_basedir) {
103 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
105 die "Cannot find vfio-pci module!\n" if !-d
$vfio_basedir;
107 my $testdir = "$vfio_basedir/$name";
108 return 1 if -d
$testdir;
110 my $data = "$dev->{vendor} $dev->{product}";
111 return undef if !file_write
("$vfio_basedir/new_id", $data);
113 my $fn = "$pcisysfs/devices/$name/driver/unbind";
114 if (!file_write
($fn, $name)) {
115 return undef if -f
$fn;
118 $fn = "$vfio_basedir/bind";
120 return undef if !file_write
($fn, $name);
126 sub pci_dev_group_bind_to_vfio
{
129 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
131 if (!-d
$vfio_basedir) {
132 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
134 die "Cannot find vfio-pci module!\n" if !-d
$vfio_basedir;
136 # get IOMMU group devices
137 opendir(my $D, "$pcisysfs/devices/0000:$pciid/iommu_group/devices/") || die "Cannot open iommu_group: $!\n";
138 my @devs = grep /^0000:/, readdir($D);
141 foreach my $pciid (@devs) {
142 $pciid =~ m/^([:\.\da-f]+)$/ or die "PCI ID $pciid not valid!\n";
144 # pci bridges, switches or root ports are not supported
145 # they have a pci_bus subdirectory so skip them
146 next if (-e
"$pcisysfs/devices/$pciid/pci_bus");
148 my $info = pci_device_info
($1);
149 pci_dev_bind_to_vfio
($info) || die "Cannot bind $pciid to vfio\n";