PCIe BAR Size Calculator
Decode PCI Express Base Address Registers and calculate the address-space size reported by a BAR probe mask. Analyze Memory BARs, I/O BARs, 32-bit and 64-bit BARs, prefetchable capability, alignment and configured address ranges.
size = (~mask + 1) within the BAR’s address width. The original
BAR value must be restored afterward.
—
What Is a PCIe BAR?
A Base Address Register, or BAR, is a PCI or PCI Express configuration-space register used to describe a device resource that must be mapped into system Memory or I/O address space.
PCIe endpoints commonly use Memory BARs for register windows, device memory, framebuffers, DMA control structures and other memory-mapped resources.
How PCIe BAR Sizing Works
BAR size is traditionally discovered by saving the BAR value, disabling the corresponding resource decoder, writing all ones, reading the value back and examining which address bits are hardwired to zero.
1. Save BAR value
2. Disable Memory or I/O decoding
3. Write 0xFFFFFFFF
4. Read BAR back
5. Mask attribute bits
6. Compute size
7. Restore original BARA 64-bit Memory BAR requires the corresponding upper BAR DWORD to be included in the sizing operation.
PCIe BAR Size Formula
After removing the non-address attribute bits, the basic sizing calculation is:
Size = (~BAR_Mask + 1)
within the implemented BAR address widthThe same relationship can also be written as two’s-complement negation of the masked probe value.
4 KiB BAR Example
Probe readback:
0xFFFFF004
Memory BAR attribute bits:
0x4
Address mask:
0xFFFFF000
Invert + 1:
~0xFFFFF000 + 1
= 0x00001000
BAR size:
4096 bytes
= 4 KiBPCIe Memory BAR Bit Layout
Memory BAR low DWORD:
Bit 0:
0 = Memory BAR
Bits 2:1:
Memory type
Bit 3:
Prefetchable
Bits 31:4:
Base address bitsMemory BAR Type Bits
| Bits 2:1 | Meaning |
|---|---|
| 00 | 32-bit Memory BAR |
| 01 | Legacy below-1-MB encoding |
| 10 | 64-bit Memory BAR |
| 11 | Reserved |
32-Bit Memory BAR
A conventional 32-bit Memory BAR occupies one BAR register and exposes address bits in bits 31 through 4. The resulting resource must be aligned to its implemented size.
Configured BAR:
0x80000000
Base address:
0x8000000064-Bit PCIe BAR
A 64-bit Memory BAR consumes two consecutive BAR registers. The low BAR holds the type and prefetchable flags plus the low address bits, while the next BAR holds address bits 63 through 32.
BAR n:
Low 32 bits + attributes
BAR n+1:
Upper 32 address bitsThe combined base address can therefore extend above the 4 GiB boundary.
PCIe I/O BAR
If bit 0 is one, the BAR represents I/O space rather than Memory space. For an I/O BAR, the low address bits have a different reserved/attribute layout.
I/O BAR:
Bit 0:
1 = I/O space
Bits 1:
Reserved / implementation-defined legacy field
Address mask:
typically bits 31:2Prefetchable BAR
Bit 3 of a Memory BAR indicates whether reads can be prefetched without causing device side effects. Prefetchable resources are commonly used for device memory where speculative reads are safe.
Control/status register regions with read side effects are normally non-prefetchable.
BAR Alignment Requirement
A BAR’s assigned base address must normally be naturally aligned to the size of the resource.
BAR size:
64 KiB
Required base alignment:
64 KiB
Valid:
0x80000000
0x80010000
Not naturally aligned:
0x8000800064 KiB BAR Size Example
Address mask:
0xFFFF0000
Size:
~0xFFFF0000 + 1
= 0x00010000
= 65,536 bytes
= 64 KiB1 MiB BAR Size Example
Address mask:
0xFFF00000
Size:
~0xFFF00000 + 1
= 0x00100000
= 1,048,576 bytes
= 1 MiBConfigured BAR Address
When a BAR contains its assigned address rather than a size-probe result, attribute bits must be masked before interpreting the base address.
Memory BAR:
0x80000004
Low attribute bits:
0x4
Base address:
0x80000000Calculate BAR Address Range
When the base and size are known, the inclusive mapped address range is:
End Address =
Base Address
+ BAR Size
- 1For a 4 KiB BAR mapped at 0x80000000:
Start:
0x80000000
End:
0x80000FFFWhy BAR Sizes Are Powers of Two
The BAR mask mechanism naturally represents resource sizes as powers of two with corresponding natural alignment. Implemented address bits form a mask whose trailing zero region determines the allocation size.
PCIe device designs should follow the applicable BAR sizing requirements for the resource type being implemented.
BAR Registers in PCIe Configuration Space
Type 0 configuration headers used by endpoints provide a set of BAR register locations beginning at configuration offset 0x10. A 64-bit BAR consumes two adjacent 32-bit entries, reducing the number of independent BAR resources that can be represented.
BAR0, BAR1, BAR2 and Other BAR Numbers
Names such as BAR0 and BAR2 refer to the position of a Base Address Register within the PCI configuration header. They do not by themselves determine the BAR’s size or type.
For example, BAR0 can be 32-bit or 64-bit depending on the type bits returned by the device.
Why a 64-Bit BAR Uses Two Registers
A configuration BAR register is only 32 bits wide. A 64-bit resource therefore requires both the current BAR register and the immediately following BAR register to hold the complete address.
Software must treat the pair as one logical BAR.
PCIe BAR Size Calculator FAQs
How do I calculate PCIe BAR size?
What does BAR bit 0 mean?
How do I know if a BAR is 64-bit?
Does a 64-bit BAR use two BAR registers?
What does prefetchable mean?
Why must a BAR address be aligned to its size?
What does a mask of 0xFFFFF000 mean?
What does 0xFFFF0000 correspond to?
Can I calculate the end address from a BAR?
Should software leave all ones written to a BAR?
Calculate PCIe BAR Size and Address Range
Decode PCI Express BAR probe results and configured resource addresses for endpoint development, FPGA validation, driver debugging and PCIe configuration analysis.