Skip to content
systemcoding.dev
Kernels

Read PCI Configuration Space With lspci and setpci

Read and decode PCI configuration space on Linux 6.12 with lspci and setpci from pciutils 3.13, from vendor IDs and BARs to capability lists and link status.

6 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, FreeBSD and Rust logos.

Every PCI and PCI Express device carries a small block of registers that describes it: who made it, what kind of device it is, where its memory windows are, and which optional features it supports. This block is called configuration space, and it is the first thing a driver reads and the first thing you should read when a device misbehaves. Linux ships two tools for it in the pciutils package: lspci reads and decodes, and setpci reads and writes raw registers. This post uses pciutils 3.13 on Linux 6.12 to find a device, decode its header, read single registers, and interpret the capability list, and it warns where a write can hurt.

The definitive layout is in the PCI Express Base Specification from PCI-SIG, and the kernel's view of it is described in the PCI documentation.

Find the device

Plain lspci lists every device by its bus address, in the form bus:device.function:

Shell
lspci -nn
Text
00:00.0 Host bridge [0600]: Intel Corporation Device [8086:9b61]
00:02.0 VGA compatible controller [0300]: Intel Corporation CometLake-U GT2 [UHD Graphics] [8086:9b41] (rev 02)
00:1f.6 Ethernet controller [0200]: Intel Corporation Ethernet Connection (10) I219-LM [8086:0d4f]
02:00.0 Non-Volatile memory controller [0108]: Samsung Electronics Co Ltd NVMe SSD Controller SM981/PM981/PM983 [144d:a808]

The -nn option adds the numeric IDs next to the names. Two numbers appear in each line. The one in the first brackets, such as 0108, is the class code: 01 is mass storage and 08 is the NVMe subclass. The pair at the end, 144d:a808, is the vendor ID and device ID, which a driver matches on. The names come from a database that may lag behind new hardware, and the numbers never lie.

Use the address in the first column to pick one device. The address is 02:00.0, bus 2, device 0, function 0, and it is what the -s option takes.

Decode the header

-v adds the decoded header, and each extra v adds detail. Reading the whole configuration space of a device needs root, because the registers beyond the first 64 bytes are hidden from normal users:

Shell
sudo lspci -s 02:00.0 -vv
Text
02:00.0 Non-Volatile memory controller: Samsung Electronics Co Ltd NVMe SSD Controller SM981/PM981/PM983 (prog-if 02 [NVM Express])
	Subsystem: Samsung Electronics Co Ltd Device a801
	Control: I/O- Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR- FastB2B- DisINTx+
	Status: Cap+ 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR- <PERR- INTx-
	Latency: 0
	Interrupt: pin A routed to IRQ 16
	Region 0: Memory at e1300000 (64-bit, non-prefetchable) [size=16K]
	Capabilities: [40] Power Management version 3
	Capabilities: [50] MSI: Enable- Count=1/32 Maskable- 64bit+
	Capabilities: [70] Express (v2) Endpoint, MSI 00
		LnkCap:	Port #0, Speed 8GT/s, Width x4, ASPM L1, Exit Latency L1 <64us
		LnkSta:	Speed 8GT/s, Width x4
	Capabilities: [b0] MSI-X: Enable+ Count=33 Masked-
	Kernel driver in use: nvme
	Kernel modules: nvme

Several lines deserve a close read.

The Control line is the decoded Command register. Mem+ means the device responds to memory accesses, BusMaster+ means it may start DMA, and I/O- means its I/O port space is off. A device with Mem- is disabled, which is how a driver that failed to enable it, or a firmware that left it off, looks from the outside.

Region 0 is BAR 0, the first base address register. It reports the address the firmware assigned, whether the region is 32 or 64 bit, whether it is prefetchable, and its size, which is 16 KiB here. A BAR that shows [disabled] or no address has not been assigned space, which usually means the system ran out of address space or the firmware did not allocate it.

The Capabilities lines walk a linked list that starts at a pointer in the header. [40] and [50] are the offsets in configuration space where each capability sits. Power management, MSI and MSI-X are interrupt and power features, and the Express entry is the PCI Express capability.

LnkCap and LnkSta are the most useful pair for performance questions. LnkCap is what the link can do (8 GT/s, which is PCIe 3.0, and four lanes), and LnkSta is what it negotiated. When LnkSta is lower than LnkCap, for example Speed 2.5GT/s or Width x1, the device is running degraded, and the cause is usually a slot, a riser or a bad contact. Some tools mark this with (downgraded).

The last two lines name the driver bound to the device, which is the next post's subject, finding the kernel driver with lspci -k.

Dump the raw bytes

To see the registers themselves, ask for a hex dump. -x shows the first 64 bytes, -xxx shows 256 bytes, and -xxxx shows the whole 4 KiB extended space of a PCI Express device:

Shell
sudo lspci -s 02:00.0 -xxx | head -n 6
Text
02:00.0 Non-Volatile memory controller: Samsung Electronics Co Ltd NVMe SSD Controller SM981/PM981/PM983 (rev 00)
00: 4d 14 08 a8 06 04 10 00 00 02 08 01 00 00 00 00
10: 04 00 30 e1 00 00 00 00 00 00 00 00 00 00 00 00
20: 00 00 00 00 00 00 00 00 00 00 00 00 4d 14 01 a8
30: 00 00 00 00 40 00 00 00 00 00 00 00 10 01 00 00

The dump is little-endian, which is why the vendor ID 144d appears as bytes 4d 14. The offsets in the left column are the standard header layout:

OffsetSizeRegister
0x002Vendor ID (4d 14 is 0x144d)
0x022Device ID (08 a8 is 0xa808)
0x042Command (06 04 is 0x0406: memory and bus master enabled)
0x062Status
0x081Revision ID
0x093Class code (02 08 01: programming interface, subclass, class)
0x104BAR 0 (04 00 30 e1 is 0xe1300004, where bit 2 marks a 64-bit BAR)
0x2c4Subsystem vendor and device ID
0x341Capabilities pointer (40)
0x3c1Interrupt line (10 here, which is IRQ 16)

Read them in this order the first few times, and the decoded output of -vv stops being mysterious, since each line is a formatted view of one of these fields. Note the low bits of BAR 0: 0x4 in the least significant nibble says the BAR is a 64-bit memory BAR, and the real base address is the value with those low four bits cleared, 0xe1300000.

Read one register with setpci

setpci reads a register by name or by offset and prints only that value. The syntax is setpci -s <address> <register>[.<width>], where the width suffix is b, w or l for 1, 2 or 4 bytes:

Shell
sudo setpci -s 02:00.0 VENDOR_ID DEVICE_ID COMMAND
sudo setpci -s 02:00.0 0x34.b
Text
144d
a808
0406
40

The first command reads three registers by name, and the second reads one byte at offset 0x34, the capabilities pointer. Using setpci in scripts is better than parsing lspci -vv, because the output is one number per line and does not depend on how the tool formats text.

Writing, and why not to

setpci can write by adding =value. For example, setpci -s 02:00.0 COMMAND=0x0406 writes the command register. Writing is how you disable bus mastering to stop a misbehaving device from doing DMA, or how some firmware workarounds are done. It is also how you crash a machine. Changing the command register of a device that a driver controls, or a BAR of a device in use, can hang the system, corrupt data or lose the device until reboot. The kernel does not know you changed the register, and its cached copy no longer matches.

Do not write configuration space on a production machine unless you have read what the register does in the specification, and prefer the kernel's own interfaces, such as the enable and remove files in sysfs, for anything you can express that way.

Configuration space without the tools

Everything above is available from sysfs too. Each device has a binary config file, and the kernel exposes parsed values as text files:

Shell
cat /sys/bus/pci/devices/0000:02:00.0/vendor
cat /sys/bus/pci/devices/0000:02:00.0/class
xxd -l 16 /sys/bus/pci/devices/0000:02:00.0/config
Text
0x144d
0x010802
00000000: 4d14 08a8 0604 1000 0002 0801 0000 0000  M...............

Reading config as a normal user returns only the first 64 bytes. The 0000: in the path is the PCI domain, which lspci hides when it is zero. This is convenient on minimal systems that lack pciutils, and in programs that want the registers directly.

Also inHardware
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.