The idea: a bus made of packets

Old PCI was a shared bus: every card sat on the same wires and took turns. PCI Express (PCIe) keeps PCI's software view (configuration space, BARs, bus:device.function numbers) but replaces the wires with point-to-point links and a tree of switches. Everything that crosses a link is a packet. A read, a write, a reply and even an interrupt are packets called TLPs (transaction layer packets).

The root complex with CPU and DRAM at the top, root port 00:01.0. Link 1 goes down to a switch with upstream port 01:00.0 and two downstream ports: 02:00.0 owns memory FE000000 to FE0FFFFF and bus 3, 02:01.0 owns FE100000 to FE1FFFFF and bus 4. Link 2 goes to the NVMe SSD 03:00.0 with BAR0 FE000000, link 3 to the NIC 04:00.0 with BAR0 FE100000
Each switch port owns a memory window and a bus range: requests are routed by address, completions by bus number.

The tree's top is the root complex: it connects the CPU and DRAM to the links through root ports. At the bottom are endpoints, the devices: here an NVMe SSD and a network card (NIC). Every function has an ID, written bus:device.function: the SSD is 03:00.0 (bus 3, device 0, function 0). At boot the firmware gives each device's BAR (base address register) a range of physical addresses, and programs each switch port with a window that covers the BARs below it.

Three layers and the TLP header

PCIe has three layers, like a small network stack:

  • The transaction layer builds the TLP: a 3 or 4 DW header (1 DW = 4 bytes) and the data. It is the only part that switches look at end to end.
  • The data link layer works on one link only. It adds a 12-bit sequence number in front and a 32-bit LCRC behind, and makes sure every TLP arrives once and in order.
  • The physical layer sends the bytes over the lanes. Gen3 runs 8 GT/s per lane with 128b/130b encoding, so an x4 link carries about 3.9 GB/s each way.
A TLP on the wire: start framing, 12-bit sequence number, the 3 DW header with Fmt and Type, requester ID, tag, address and length, then the data, then the 32-bit LCRC. The sequence number and LCRC are added by the data link layer and replaced on every link
The header travels end to end; the sequence number and LCRC belong to one link and are made new on each hop.

The header's first byte says what the TLP is: Fmt (header size, with or without data) and Type. The three TLPs on this page:

TLPFmt / TypeWhat it isRouted by
MRd000 / 00000Memory Read Request, no data, non-postedaddress
MWr010 / 00000Memory Write Request with data, postedaddress
CplD010 / 01010Completion with DataID (requester's bus)

A request carries the requester ID (who asked) and a tag. The completion copies both, so the requester can match the answer to the question even when several reads are in flight.

Routing by address and by ID

A switch is a set of PCI-to-PCI bridges. Each downstream port has a memory base/limit window and a secondary/subordinate bus range. When a request arrives, the switch compares its address with the windows: 0xFE00001C is inside FE000000–FE0FFFFF, so it goes out of port 02:00.0. An address that no window covers goes upstream. A completion has no address. The switch reads the bus number of its requester ID instead: bus 0 is outside buses 2–4, so it goes up. Demo 1: CPU reads an NVMe register shows both decisions.

Posted and non-posted

A read is non-posted: the requester waits for a completion. A memory write is posted: once it is sent, the writer is done, and no completion ever comes back. That is why DMA writes are fast: the NIC can stream frames into memory without waiting for a round trip (Demo 2: NIC DMA write). It also means the writer gets no error report; ordering rules make sure a later read sees the write.

Ack, Nak and the replay buffer

The sender of each link keeps every TLP it sends in a replay buffer, under its sequence number (NEXT_TRANSMIT_SEQ counts up). The receiver checks the LCRC and that the sequence number is the one it expects (NEXT_RCV_SEQ). If both are fine it answers with an Ack DLLP (data link layer packet); Ack 7 means "7 and everything before it", and the sender drops them from the replay buffer. If the LCRC is wrong the receiver throws the TLP away and sends a Nak with the last good number. The sender then replays everything after it, unchanged (Demo 3: corrupted TLP). Software never sees this. A replay timer also resends if no Ack comes at all.

Time runs down. The root port sends MRd seq 7 and keeps it in the replay buffer. A bit flips; the switch finds a bad LCRC, discards it and sends Nak 6. The root port replays seq 7 from the buffer; the switch checks it, accepts it and sends Ack 7; the root port empties its replay buffer
A Nak names the last good sequence number; the sender resends what came after it from the replay buffer.

Credit-based flow control

A PCIe receiver never drops a TLP because it is full. Instead, before anything is sent, each receiver tells the other end how much buffer it has, in credits, separately for posted (PH, PD), non-posted (NPH, NPD) and completion (CplH, CplD) TLPs. One header credit holds one header; one data credit holds 16 bytes. A transmitter may send a TLP only when it has enough credits left. When the receiver has passed a TLP on and freed its buffer, it returns the credits with an UpdateFC DLLP.

On the page the root port has room for 2 posted headers and 8 data credits (128 bytes), small on purpose. A 128-byte write needs 1 PH and 8 PD, so after one write the second must wait at the switch until UpdateFC-P arrives (Demo 4: out of credits). Endpoints must advertise infinite completion credits, and the root port does here too (∞): a device that asks for data must have room for the answer.

Before any TLP can flow, the two ends of a link train it with ordered sets (TS1, TS2) in the physical layer's LTSSM: Detect (is there a receiver on each lane?), Polling (bit and symbol lock), Configuration (agree on the width, here x4, and the lane numbers), then L0, the working state, at Gen1 speed (2.5 GT/s). Both ends then advertise their speeds and go through Recovery to change to Gen3, 8 GT/s, with equalization. The data link layer then exchanges InitFC DLLPs to set the starting credits. The page starts with all links already up at x4 Gen3.

See also DMA, which follows a device writing into memory from the driver's side, and CRC, the kind of checksum the LCRC is.

What the page leaves out

The page draws the data link layer and credits only for link 1; links 2 and 3 do the same. It leaves out configuration requests (CfgRd0/CfgWr0, routed by ID during enumeration), 4 DW headers for 64-bit addresses, NPD credits, messages and MSI interrupts in detail, the replay timer and replay number rollover, ordering rules (a posted write may pass a read, not the other way), relaxed ordering and ID-based ordering, ECRC, poisoned TLPs and error reporting (AER), Max Read Request Size and split completions, the 8b/10b and 128b/130b encodings, power states (L0s, L1) and Gen4–Gen6 (PAM4, FLIT mode).

On the mind map

Open the Computer Architecture mind mapComputerArchitectureAbstraction and numbers: layers and bit patternsAbstraction and numbersCombinational logic: output depends only on inputsCombinational logicSequential logic: state held by a clockSequential logicMIPS software: how C becomes machine wordsMIPS softwareThe processor: datapath plus controlThe processorPipelines and parallelism: more instructions at oncePipelines and parallelismMemory and I/O: small fast near, big slow farMemory and I/OMemory arraysMemory arraysdecoder picks wordline, bitlines readCPU cacheCPU cachetag, index, offset; hit or missCache address breakdownCache address breakdownsets = lines/ways; tag = rest of addressCache Write PoliciesCache Write PoliciesWrite-back sends dirty lines at evictionLoop tiling and interchangeLoop tiling and interchangeWalk rows; tile so reused data fitsMemory hierarchyMemory hierarchyAMAT = hit time + miss rate × penaltyDRAM timingDRAM timingopen row is fast, row change is slowMESI and false sharingMESI and false sharinga write invalidates other copiesVirtual memoryVirtual memoryTLB caches the page tableEmbedded I/OEmbedded I/Odevices are addresses; poll or interruptMCU startup to mainMCU startup to mainCopy .data, zero .bss, then mainButton debounce FSMButton debounce FSMTwo equal 10 ms samples = one pressI2C BusI2C BusWired-AND bus: ACK, stretch, arbitrateUSB enumerationUSB enumerationAddress 0, descriptors, SET_CONFIGURATIONCAN bus arbitrationCAN bus arbitrationDominant 0 wins: lowest ID sends unharmedModbus RTU and TCPModbus RTU and TCPone master asks; bad CRC means silenceTDMA time-triggered busTDMA time-triggered busFixed slots; FTA drops extremes, averagesAMBA AXI, AHB, APBAMBA AXI, AHB, APBVALID and READY both 1 on the edgePCIe TLPsRequests by address, completions by IDBus vs Crossbar vs NoCBus vs Crossbar vs NoCBus: one at a time; NoC: links in parallelADC and PWMADC and PWMSAR = binary search; duty = OC1RS/(PR2+1)PID ControlPID ControlI removes error; clamp stops windupOpen the Computer Architecture mind mapComputer Architecturesmall fast near, big slow farMemory and I/OMemory arraysMemory arraysdecoder picks wordline, bitlines readCPU cacheCPU cachetag, index, offset; hit or missCache address breakdownCache address breakdownsets = lines/ways; tag = rest of addressCache Write PoliciesCache Write PoliciesWrite-back sends dirty lines at evictionLoop tiling and interchangeLoop tiling and interchangeWalk rows; tile so reused data fitsMemory hierarchyMemory hierarchyAMAT = hit time + miss rate × penaltyDRAM timingDRAM timingopen row is fast, row change is slowMESI and false sharingMESI and false sharinga write invalidates other copiesVirtual memoryVirtual memoryTLB caches the page tableEmbedded I/OEmbedded I/Odevices are addresses; poll or interruptMCU startup to mainMCU startup to mainCopy .data, zero .bss, then mainButton debounce FSMButton debounce FSMTwo equal 10 ms samples = one pressI2C BusI2C BusWired-AND bus: ACK, stretch, arbitrateUSB enumerationUSB enumerationAddress 0, descriptors, SET_CONFIGURATIONCAN bus arbitrationCAN bus arbitrationDominant 0 wins: lowest ID sends unharmedModbus RTU and TCPModbus RTU and TCPone master asks; bad CRC means silenceTDMA time-triggered busTDMA time-triggered busFixed slots; FTA drops extremes, averagesAMBA AXI, AHB, APBAMBA AXI, AHB, APBVALID and READY both 1 on the edgePCIe TLPsRequests by address, completions by IDBus vs Crossbar vs NoCBus vs Crossbar vs NoCBus: one at a time; NoC: links in parallelADC and PWMADC and PWMSAR = binary search; duty = OC1RS/(PR2+1)PID ControlPID ControlI removes error; clamp stops windup
This page on the Computer Architecture mind map · 79 pages in 7 branches · open it to see the whole map and test yourself in recall mode.