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 #fixme : need to check IOMMU support
37 #http://www.linux-kvm.org/page/How_to_assign_devices_with_VT-d_in_KVM
45 my ($filename, $buf) = @_;
47 my $fh = IO
::File-
>new($filename, "w");
50 my $res = print $fh $buf;
62 return undef if $name !~ m/^${pciregex}$/;
63 my ($domain, $bus, $slot, $func) = ($1, $2, $3, $4);
65 my $irq = file_read_firstline
("$pcisysfs/devices/$name/irq");
66 return undef if !defined($irq) || $irq !~ m/^\d+$/;
68 my $vendor = file_read_firstline
("$pcisysfs/devices/$name/vendor");
69 return undef if !defined($vendor) || $vendor !~ s/^0x//;
71 my $product = file_read_firstline
("$pcisysfs/devices/$name/device");
72 return undef if !defined($product) || $product !~ s/^0x//;
83 has_fl_reset
=> -f
"$pcisysfs/devices/$name/reset" || 0,
92 my $name = $dev->{name
};
94 my $fn = "$pcisysfs/devices/$name/reset";
96 return file_write
($fn, "1");
99 sub pci_dev_bind_to_vfio
{
102 my $name = $dev->{name
};
104 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
106 if (!-d
$vfio_basedir) {
107 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
109 die "Cannot find vfio-pci module!\n" if !-d
$vfio_basedir;
111 my $testdir = "$vfio_basedir/$name";
112 return 1 if -d
$testdir;
114 my $data = "$dev->{vendor} $dev->{product}";
115 return undef if !file_write
("$vfio_basedir/new_id", $data);
117 my $fn = "$pcisysfs/devices/$name/driver/unbind";
118 if (!file_write
($fn, $name)) {
119 return undef if -f
$fn;
122 $fn = "$vfio_basedir/bind";
124 return undef if !file_write
($fn, $name);
130 sub pci_dev_group_bind_to_vfio
{
133 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
135 if (!-d
$vfio_basedir) {
136 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
138 die "Cannot find vfio-pci module!\n" if !-d
$vfio_basedir;
140 # get IOMMU group devices
141 opendir(my $D, "$pcisysfs/devices/0000:$pciid/iommu_group/devices/") || die "Cannot open iommu_group: $!\n";
142 my @devs = grep /^0000:/, readdir($D);
145 foreach my $pciid (@devs) {
146 $pciid =~ m/^([:\.\da-f]+)$/ or die "PCI ID $pciid not valid!\n";
148 # pci bridges, switches or root ports are not supported
149 # they have a pci_bus subdirectory so skip them
150 next if (-e
"$pcisysfs/devices/$pciid/pci_bus");
152 my $info = pci_device_info
($1);
153 pci_dev_bind_to_vfio
($info) || die "Cannot bind $pciid to vfio\n";