从用户空间访问linux中的pci设备信息

时间:2014-12-11 10:40:12

标签: linux linux-kernel pci-e pci-bus

我想以编程方式从用户空间访问pci设备树信息。就像根复合体和连接它的设备一样。我该怎么办,请告诉我。

此致 普拉迪普

4 个答案:

答案 0 :(得分:4)

libpci或pcilib(lspci所基于的)使用sysfs,procfs和其他可能的方法来访问PCI信息。您可以查看pciutils包源代码以获得进一步的参考:

https://github.com/gittup/pciutils

https://github.com/gittup/pciutils/blob/gittup/lspci.c

答案 1 :(得分:1)

从命令行尝试使用lspci -tv命令。 但据我所知,这个命令只是解析/ proc ...如果你需要从程序中完成任务,你可以查看lspci源代码。

答案 2 :(得分:1)

您可以通过浏览sysfs来访问许多信息。也可以写入sysfs中的某些文件来执行某些命令,如PCI总线重新枚举;)

例如列出所有PCI设备可以通过以下方式完成:

paebbels@debian8:/sys/bus/pci/devices$ ll /sys/bus/pci/devices
insgesamt 0
drwxr-xr-x 2 root root 0 Dez 12 00:33 .
drwxr-xr-x 5 root root 0 Dez 12 00:33 ..
lrwxrwxrwx 1 root root 0 Dez 12 00:33 0000:00:00.0 -> ../../../devices/pci0000:00/0000:00:00.0
lrwxrwxrwx 1 root root 0 Dez 12 00:33 0000:00:01.0 -> ../../../devices/pci0000:00/0000:00:01.0
lrwxrwxrwx 1 root root 0 Dez 12 00:33 0000:00:07.0 -> ../../../devices/pci0000:00/0000:00:07.0
lrwxrwxrwx 1 root root 0 Dez 12 00:33 0000:00:07.1 -> ../../../devices/pci0000:00/0000:00:07.1
lrwxrwxrwx 1 root root 0 Dez 12 00:33 0000:00:07.3 -> ../../../devices/pci0000:00/0000:00:07.3
...
lrwxrwxrwx 1 root root 0 Dez 12 00:33 0000:00:18.2 -> ../../../devices/pci0000:00/0000:00:18.2
lrwxrwxrwx 1 root root 0 Dez 12 00:33 0000:00:18.3 -> ../../../devices/pci0000:00/0000:00:18.3
lrwxrwxrwx 1 root root 0 Dez 12 00:33 0000:00:18.4 -> ../../../devices/pci0000:00/0000:00:18.4
lrwxrwxrwx 1 root root 0 Dez 12 00:33 0000:00:18.5 -> ../../../devices/pci0000:00/0000:00:18.5
lrwxrwxrwx 1 root root 0 Dez 12 00:33 0000:00:18.6 -> ../../../devices/pci0000:00/0000:00:18.6
lrwxrwxrwx 1 root root 0 Dez 12 00:33 0000:00:18.7 -> ../../../devices/pci0000:00/0000:00:18.7
lrwxrwxrwx 1 root root 0 Dez 12 00:33 0000:02:00.0 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:00.0
lrwxrwxrwx 1 root root 0 Dez 12 00:33 0000:02:01.0 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:01.0
lrwxrwxrwx 1 root root 0 Dez 12 00:33 0000:02:02.0 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:02.0
lrwxrwxrwx 1 root root 0 Dez 12 00:33 0000:02:03.0 -> ../../../devices/pci0000:00/0000:00:11.0/0000:02:03.0
lrwxrwxrwx 1 root root 0 Dez 12 00:33 0000:03:00.0 -> ../../../devices/pci0000:00/0000:00:15.0/0000:03:00.0

注意:这是VMWare中的Debian - >扁平PCI层次。

以下是设备0000:02:00.0:

的示例
paebbels@debian8:/sys/bus/pci/devices/0000:02:00.0$ ls -ahlL /sys/bus/pci/devices/0000\:02\:00.0
insgesamt 0
drwxr-xr-x 4 root root    0 Dez 12 00:37 .
drwxr-xr-x 8 root root    0 Dez 12 00:37 ..
-r--r--r-- 1 root root 4,0K Dez 12 00:37 acpi_index
-rw-r--r-- 1 root root 4,0K Dez 12 00:37 broken_parity_status
-r--r--r-- 1 root root 4,0K Dez 12 00:37 class
-rw-r--r-- 1 root root  256 Dez 12 00:37 config
-r--r--r-- 1 root root 4,0K Dez 12 00:37 consistent_dma_mask_bits
-rw-r--r-- 1 root root 4,0K Dez 12 00:37 d3cold_allowed
-r--r--r-- 1 root root 4,0K Dez 12 00:37 device
-r--r--r-- 1 root root 4,0K Dez 12 00:37 dma_mask_bits
drwxr-xr-x 2 root root    0 Dez  8 11:18 driver -> ../../../../bus/pci/drivers/uhci_hcd
-rw-r--r-- 1 root root 4,0K Dez 12 00:37 driver_override
-rw-r--r-- 1 root root 4,0K Dez 12 00:37 enabled
drwxr-xr-x 3 root root    0 Dez 12 00:37 firmware_node -> ../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A03:00/device:05/device:06
-r--r--r-- 1 root root 4,0K Dez 12 00:37 irq
-r--r--r-- 1 root root 4,0K Dez 12 00:37 label
-r--r--r-- 1 root root 4,0K Dez 12 00:37 local_cpulist
-r--r--r-- 1 root root 4,0K Dez 12 00:37 local_cpus
-r--r--r-- 1 root root 4,0K Dez 12 00:37 modalias
-rw-r--r-- 1 root root 4,0K Dez 12 00:37 msi_bus
-r--r--r-- 1 root root 4,0K Dez 12 00:37 numa_node
-r--r--r-- 1 root root 4,0K Dez 12 00:37 pools
drwxr-xr-x 2 root root    0 Dez 12 00:37 power
--w--w---- 1 root root 4,0K Dez 12 00:37 remove
--w--w---- 1 root root 4,0K Dez 12 00:37 rescan
--w------- 1 root root 4,0K Dez 12 00:37 reset
-r--r--r-- 1 root root 4,0K Dez 12 00:37 resource
-rw------- 1 root root   32 Dez 12 00:37 resource4
drwxr-xr-x 5 root root    0 Dez 12 00:33 subsystem -> ../../../../bus/pci
-r--r--r-- 1 root root 4,0K Dez 12 00:37 subsystem_device
-r--r--r-- 1 root root 4,0K Dez 12 00:37 subsystem_vendor
-rw-r--r-- 1 root root 4,0K Dez 12 00:37 uevent
drwxr-xr-x 5 root root    0 Dez 12 00:37 usb4
-r--r--r-- 1 root root 4,0K Dez 12 00:37 vendor

您可以看到设备0000:02:00.0使用了驱动程序' uhci_hcd'。

不错的浏览:)

答案 3 :(得分:0)

如果安装了pciutils,则超级用户使用'lspci'。

 # which lspci
/usr/sbin/lspci

如果您是普通用户,则可能会得到以下信息:

 $ lspci
-bash: lspci: command not found

如果您指定绝对文件路径(由root执行的“ which”的输出),您仍然可以使用它(从用户空间或程序):

 $ /usr/sbin/lspci