]> git.proxmox.com Git - pve-common.git/blob - src/PVE/SysFSTools.pm
SysFSTools: implement filter by id in lspci
[pve-common.git] / src / PVE / SysFSTools.pm
1 package PVE::SysFSTools;
2
3 use strict;
4 use warnings;
5
6 use IO::File;
7
8 use PVE::Tools qw(file_read_firstline dir_glob_foreach);
9
10 my $pcisysfs = "/sys/bus/pci";
11 my $pciregex = "([a-f0-9]{4}):([a-f0-9]{2}):([a-f0-9]{2})\.([a-f0-9])";
12
13 sub lspci {
14 my ($filter) = @_;
15
16 my $devices = {};
17
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;
24 });
25
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 ];
30 }
31
32 return $devices;
33 }
34
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
38
39 my $iommu=1;
40 return $iommu;
41
42 }
43
44 sub file_write {
45 my ($filename, $buf) = @_;
46
47 my $fh = IO::File->new($filename, "w");
48 return undef if !$fh;
49
50 my $res = print $fh $buf;
51
52 $fh->close();
53
54 return $res;
55 }
56
57 sub pci_device_info {
58 my ($name) = @_;
59
60 my $res;
61
62 return undef if $name !~ m/^${pciregex}$/;
63 my ($domain, $bus, $slot, $func) = ($1, $2, $3, $4);
64
65 my $irq = file_read_firstline("$pcisysfs/devices/$name/irq");
66 return undef if !defined($irq) || $irq !~ m/^\d+$/;
67
68 my $vendor = file_read_firstline("$pcisysfs/devices/$name/vendor");
69 return undef if !defined($vendor) || $vendor !~ s/^0x//;
70
71 my $product = file_read_firstline("$pcisysfs/devices/$name/device");
72 return undef if !defined($product) || $product !~ s/^0x//;
73
74 $res = {
75 name => $name,
76 vendor => $vendor,
77 product => $product,
78 domain => $domain,
79 bus => $bus,
80 slot => $slot,
81 func => $func,
82 irq => $irq,
83 has_fl_reset => -f "$pcisysfs/devices/$name/reset" || 0,
84 };
85
86 return $res;
87 }
88
89 sub pci_dev_reset {
90 my ($dev) = @_;
91
92 my $name = $dev->{name};
93
94 my $fn = "$pcisysfs/devices/$name/reset";
95
96 return file_write($fn, "1");
97 }
98
99 sub pci_dev_bind_to_vfio {
100 my ($dev) = @_;
101
102 my $name = $dev->{name};
103
104 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
105
106 if (!-d $vfio_basedir) {
107 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
108 }
109 die "Cannot find vfio-pci module!\n" if !-d $vfio_basedir;
110
111 my $testdir = "$vfio_basedir/$name";
112 return 1 if -d $testdir;
113
114 my $data = "$dev->{vendor} $dev->{product}";
115 return undef if !file_write("$vfio_basedir/new_id", $data);
116
117 my $fn = "$pcisysfs/devices/$name/driver/unbind";
118 if (!file_write($fn, $name)) {
119 return undef if -f $fn;
120 }
121
122 $fn = "$vfio_basedir/bind";
123 if (! -d $testdir) {
124 return undef if !file_write($fn, $name);
125 }
126
127 return -d $testdir;
128 }
129
130 sub pci_dev_group_bind_to_vfio {
131 my ($pciid) = @_;
132
133 my $vfio_basedir = "$pcisysfs/drivers/vfio-pci";
134
135 if (!-d $vfio_basedir) {
136 system("/sbin/modprobe vfio-pci >/dev/null 2>/dev/null");
137 }
138 die "Cannot find vfio-pci module!\n" if !-d $vfio_basedir;
139
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);
143 closedir($D);
144
145 foreach my $pciid (@devs) {
146 $pciid =~ m/^([:\.\da-f]+)$/ or die "PCI ID $pciid not valid!\n";
147
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");
151
152 my $info = pci_device_info($1);
153 pci_dev_bind_to_vfio($info) || die "Cannot bind $pciid to vfio\n";
154 }
155
156 return 1;
157 }
158
159 1;