How PCI Express Works: Config Space, BARs and Link Training
How PCI Express links train, how configuration space and BARs describe a device, and how Linux 6.12 turns that into memory the driver can map and use.

A PCI Express device is not on a shared bus. It sits at the end of a dedicated serial link, and a surprising amount of what a driver sees is decided before any software runs: the link has trained to a speed and width, firmware or the kernel has assigned address ranges to the device, and its configuration space has been read and sorted. This post explains those three layers in the order they happen, with the arithmetic that lets you check a link's bandwidth yourself, and ties each one to what Linux 6.12 shows in lspci and sysfs. It is the concept behind reading configuration space with lspci and behind the driver code in the PCI driver skeleton.
The normative text is the PCI Express Base Specification from PCI-SIG, and the kernel's description of its enumeration is in the PCI documentation.
Point-to-point lanes, not a bus
Conventional PCI was a parallel bus shared by every device. PCI Express replaces it with point-to-point links. A link is made of one or more lanes, and each lane is two differential pairs, one for each direction, so a lane transmits and receives at the same time. A link is described by its width (x1, x2, x4, x8, x16) and its generation, which sets the signaling rate per lane:
| Generation | Rate per lane | Encoding | Usable per lane, each direction |
|---|---|---|---|
| 1.0 | 2.5 GT/s | 8b/10b | 250 MB/s |
| 2.0 | 5 GT/s | 8b/10b | 500 MB/s |
| 3.0 | 8 GT/s | 128b/130b | about 985 MB/s |
| 4.0 | 16 GT/s | 128b/130b | about 1,969 MB/s |
| 5.0 | 32 GT/s | 128b/130b | about 3,938 MB/s |
The last column is arithmetic, not a measurement: transfers per second times the encoding efficiency, divided by eight bits per byte. For Gen 3, 8 GT/s times 128/130 is 7.877 Gb/s, or 984.6 MB/s. For Gen 1 and 2, the 8b/10b code sends ten bits for every eight bits of data, which is a 20 percent overhead, and that is why Gen 3 moved to the lower-overhead 128b/130b code. Multiply by the number of lanes for the link's raw ceiling, so a Gen 3 x4 link tops out near 3.9 GB/s in each direction. Protocol overhead from packet headers and flow control reduces real throughput below that.
Three layers and the packets between them
The protocol is layered. The transaction layer builds and parses transaction layer packets (TLPs): memory read, memory write, I/O, configuration and message TLPs, and the completions that answer reads. The data link layer adds a sequence number and a checksum and retransmits a packet that arrived damaged, using an acknowledgement scheme. The physical layer serializes bytes, applies the encoding and handles link training.
For a driver author the transaction layer is the one that matters. When a CPU reads a device register, the root complex turns the load instruction into a memory-read TLP, sends it down the link, and waits for a completion TLP that carries the data. A write is a posted transaction: the CPU's store is sent and does not wait for a reply. This asymmetry explains a common device-driver surprise: a posted write may still be in flight when the next instruction runs, and a driver that needs to know the write has reached the device does a read from the same device afterward, which cannot complete until the earlier writes have.
Link training
When power comes on, or when a device is hot-plugged, the two ends of every link run a training procedure, controlled by a state machine called the LTSSM. Its main states, in order of a normal bring-up, are:
Detect: each side checks whether a receiver is present on the other end of the pairs.
Polling: the transmitters send training sequences, and each side checks it can lock onto the other's signal and agree on the bit rate.
Configuration: the ends negotiate the link width (using the lanes that work) and lane numbering.
L0: the normal operating state, in which TLPs flow.
Training starts at 2.5 GT/s so that any two devices can talk. If both ends support more, they then enter a recovery state, switch to a higher rate, retrain, and repeat until they reach the highest rate both sides support and the channel supports. The width is negotiated the same way, and a link can come up narrower than the connector if some lanes fail to train.
Linux shows the outcome in lspci -vv, as LnkCap (the best the device can do) and LnkSta (what it negotiated). A result like this one is a degraded link:
LnkCap: Port #0, Speed 16GT/s, Width x16, ASPM L1
LnkSta: Speed 8GT/s (downgraded), Width x8 (downgraded)The downgraded marker is the tool noting that the status is below the capability. The cause is typically the other end. A slot that supports only Gen 3, a riser cable, or a contact problem can each produce it. sysfs has the same numbers as current_link_speed, max_link_speed, current_link_width and max_link_width files in the device directory, which a monitoring script can compare.
Power saving works through the same machinery. Active State Power Management lets a link drop into low-power states L0s or L1 when idle, and return to L0 on demand. The exit latency is what lspci prints next to ASPM, and it is a cost you pay on the first access after idle.
Configuration space
Each function has configuration space, a block of registers that exists before any driver does. A conventional PCI function has 256 bytes. A PCI Express function has 4 KiB, with the first 256 bytes identical to the conventional layout, so old software still works. The first 64 bytes are a standard header: vendor and device ID, command and status, class code, the six base address registers, the subsystem IDs and the pointer to the capability list.
After the header comes a linked list of capabilities, each with an ID and a pointer to the next. Power management, MSI, MSI-X and the PCI Express capability itself are on this list. The extended space from offset 0x100 holds a second list of extended capabilities, such as advanced error reporting, SR-IOV and the IOMMU's address translation services.
On a PC the CPU reaches this space in one of two ways. The legacy way uses two I/O ports to select and read a register. The modern way maps all of it into physical memory as the enhanced configuration access mechanism (ECAM), where the address of a register is computed from the bus, device, function and offset: each function gets a 4 KiB window at base + (bus << 20 | device << 15 | function << 12). The kernel reads the base from an ACPI table named MCFG or from the device tree.
BARs: how a device gets address space
A device's registers and memory are not at fixed addresses. They live behind base address registers, which firmware or the kernel programs with a physical address during enumeration. The mechanism is a sizing trick that every kernel performs:
Save the BAR's current value.
Write all ones to the BAR.
Read it back. The device forces the bits that select addresses inside its window to read as zero, so the reply tells the window size.
Restore the original value.
If a 32-bit BAR reads back 0xFFFFC000, the low flag bits are masked off, the lowest settable address bit is bit 14, and the size is ~0xFFFFC000 + 1, which is 0x4000, or 16 KiB. The size is always a power of two and the base must be aligned to the size. A BAR whose low bits read 0x4 is a 64-bit memory BAR that uses the next BAR for the high half, which is why a device with a 64-bit BAR 0 has no usable BAR 1.
The kernel collects all the sizes, allocates non-overlapping address ranges from what the root complex and bridges can pass, and writes them back. A bridge has windows in its own configuration space that cover everything below it, which is why a device's resources are always inside its parent's. When the assignment fails for lack of space, the log says so, with lines such as BAR 0: no space for [mem size 0x10000000], and the device cannot be used.
After this, lspci shows Region 0: Memory at e1300000 (64-bit, non-prefetchable) [size=16K], and the driver maps that region with pci_iomap(), as shown in mapping PCI BARs. Prefetchable means reads have no side effects, so the CPU may combine and cache them, as for a frame buffer. Registers are non-prefetchable, because a read can change device state.
What the driver gets
By the time probe runs, all of this is finished: the link is trained, the function is enumerated and its BARs are assigned. The driver receives a struct pci_dev with the IDs, the resources and the interrupt information filled in. Its work is to enable the device, claim the regions, map them and set up interrupts and DMA.


