ECAM access formula in PCI Express page is wrong

All about the OSDev Wiki. Discussions about the organization and general structure of articles and how to use the wiki. Request changes here if you don't know how to use the wiki.
Post Reply
jayk54321
Posts: 3
Joined: Sat Mar 12, 2022 1:43 am

ECAM access formula in PCI Express page is wrong

Post by jayk54321 »

From the PCI_Express wiki:
To access a specific register within a device's PCI configuration space, you have to use the device's PCI Segment Group and bus to determine which memory mapped PCI configuration space area to use, and obtain the starting physical address and starting bus number for that memory mapped area. Once you have the correct starting physical address and starting bus number for that memory mapped area you would use the following formula to determine where the (4096-byte) area for a function's PCI configuration space is: Physical_Address = MMIO_Starting_Physical_Address + ((Bus - MMIO_Starting_Bus) << 20 | Device << 15 | Function << 12).
Why subtract the starting bus address here? From my reading of the PCI firmware specification, the starting bus address is where you must start enumerating relative to the ECAM base. Whatever PCI device bdfs you successfully enumerate from there, you would just use those bus numbers directly relative to the base. They would be greater than the starting bus address anyway.
Octocontrabass
Member
Member
Posts: 5491
Joined: Mon Mar 25, 2013 7:01 pm

Re: ECAM access formula in PCI Express page is wrong

Post by Octocontrabass »

I'm not sure if that's outright wrong or just poor wording. The address given in the MCFG table represents where bus 0 would be mapped if bus 0 is mapped, which means the actual base address of the MMIO might be a higher address if bus 0 is not mapped. If you calculate the real MMIO base address according to the lowest mapped bus, then you would need to subtract the starting bus to get back to the address given by the MCFG table.

In practice, bus 0 is always mapped.

You should be able to log in to the wiki, so feel free to correct that page as you see fit.
jayk54321
Posts: 3
Joined: Sat Mar 12, 2022 1:43 am

Re: ECAM access formula in PCI Express page is wrong

Post by jayk54321 »

You're right, the subtraction would be required if someone were using a base address relative to the lowest mapped bus. However, the address in the MCFG table is always relative to bus zero, as I quote from the PCI firmware spec:
For PCI-X and PCI Express platforms utilizing the enhanced
configuration access method, the base address of the memory mapped configuration space
always corresponds to bus number 0 (regardless of the start bus number decoded by the host
bridge) and further must comply with alignment requirements of the corresponding local bus
specification.
So someone would have to modify the address they read from MCFG to end up in this situation. I'll add this context to the wiki. Thanks.
Post Reply