问题
I want to access the pci device tree information from user space programatically. Like the root complex and the devices connected to it. How can I do it please let me know.
Regards, Pradeep
回答1:
libpci or pcilib (on which lspci is based) uses sysfs, procfs, and possibly other means to access PCI information. You can check pciutils package source code for further reference:
https://github.com/gittup/pciutils
https://github.com/gittup/pciutils/blob/gittup/lspci.c
回答2:
From command line try to use lspci -tv command. But as far as I understand this command just parses /proc... If you need to do your task from program, you may have a look at lspci source code.
回答3:
You can access many information by browsing the sysfs. It's also possible to write to some files in sysfs to execute certain commands like a PCI bus re-enumeration ;)
For example listing all PCI devices can be done by:
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
Note: This is a Debian inside VMWare -> flat PCI hierachy.
Here is an example of device 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
You can see that device 0000:02:00.0 uses the driver 'uhci_hcd'.
Nice browsing :)
回答4:
Superuser uses 'lspci' if pciutils is installed.
# which lspci
/usr/sbin/lspci
If you are a regular user you might get something like this:
$ lspci
-bash: lspci: command not found
You still can use it(from user space or program) if you specify absolute file path(the output of 'which' executed by root):
$ /usr/sbin/lspci
来源:https://stackoverflow.com/questions/27420834/pci-device-info-access-in-linux-from-userspace