13

Realize that I am NOT asking about how MMIO (memory mapped input/output) is set up, but asking how it is mapped, I.e, what maps it exactly? Not what is mapped to what, I can Google a memory map for that.

What I want to know is, such as once you first power on the computer, what exactly "maps" the memory, or how does that work?

Basically, what sets up the memory or "maps" it for certain purposes?

I had originally thought it was boot firmware, like BIOS or EFI/UEFI, but another user on another site told me it had to do with a memory controller. Is that true?

But how, if in more detail, if not too much to ask? Thanks very much to any who clarify!

2
  • The "mapping" is done in hardware, specifically an address decoder. The specific address or range of addresses that correspond to a device generates a CS (chip select) signal for that device.
    – sawdust
    May 15, 2013 at 2:36
  • Not clear enough. May 15, 2013 at 2:59

2 Answers 2

13

There are a few steps. First, the BIOS discovers all the devices on the system. Then it interrogates each device to decide whether the BIOS will set that device up and, if so, determine how much memory address space, if any, the device needs. The BIOS then assigns space to each device and program's the address decoder by writing to its BAR (base address register).

In sum, the BIOS:

  1. Discovers the device's BARs (base address register). Each device can have up to six BARs.

  2. For each BAR, asks the BAR how much address space it needs.

  3. Assigns the BAR a block of address space.

  4. Programs the BAR with the base address of the chosen block of memory address space.

The device's address decoder now responds to reads and writes inside that block of address space. From then on, when the CPU (or any device in the system capable of DMA) reads from or writes to any address inside that range, the device will respond to the request.

Note that no memory is assigned to the device. The memory is on the device. It's memory address space that's assigned to the device.

You can, of course, find the process detailed on Wikipedia's PCI configuration space page.

8
  • So I was right; it is the firmware that does it. Thanks! May 15, 2013 at 5:44
  • 3
    This is a PC-centric answer that describes only how PCI sets up memory mapping for a card. PCI is a bus for cards that are "plug and play", and designed to be self configuring. More often, devices are assigned their addresses when the motherboard or SBC or SoC is designed, and then hardwired (or programmed into an FPGA) into an address decoder. Integrated devices simply don't need dynamic assignment of their addresses like a PCI card.
    – sawdust
    May 15, 2013 at 9:25
  • 1
    It was a PC-centric question. May 15, 2013 at 16:48
  • I like sawdust's answer better; he/she shouls have elaborated better. May 16, 2013 at 23:19
  • If the BAR is in the device and the BIOS firmware programs the BAR in the endpoints, whats the role of BAR in PCI ? I see the spec says PCI BARs.. What are PCI BARs? Whats the role of PCI here if the CPU writes/reads are handled by the address decoder in the endpoint device
    – AlphaGoku
    Nov 26, 2019 at 4:14
2

(Below is a conceptual explanation in almost plain words.)

Each device has some resource. Someone needs to access that resource. This someone can be cpu or other devices.

CPU requests resources from the outside world by sending out addresses. These addresses are usually classified into different categories. Such as memory address or IO address. Different categories are called different address spaces, which implies different instructions.

Memory addresses work with load-store instructions such as mov. IO addresses with IO instructions such as in or out. Different flavors of CPU use different categorization schemas. Some companies prefer to use memory addresses to address everything, such as some Motorola CPUs. Some companies prefer to use separate memory and IO addresses, such as Intel. There's a saying that programmers like Motorola because they only need to care about load-store instructions. But electrical engineers like Intel because the separation of memory/IO addresses simplifies the hardware implementation.

Anyway, after addresses are put on the address bus, some device should respond to that address by placing data on the data bus. The hardware pieces are all connected together somehow. There's many switch/decoders along the way. With different addresses, different requests are routed to different places. Kind of like a network.

Each device in the system must know which requests it should respond. The devices tell this through address-mapping, i.e. a device remembers which addresses it should respond. And this usually comes in the form an address range.

Someone must tell a device such info. And that someone can be OS/BIOS/firmware. When they do this arrangement, they are doing the address-mapping.

For PCI/PCIe, we need to program the BAR decoder/register of the device to tell it the base address of the addresses arriving at it. And the PCIe device implicitly/inherently knows the address range size used by each BAR. And OS/FW/BIOS can know that by writing all 1s to the BAR register. With this base/size info, the PCIe device can accurately identify which BAR an arriving address falls into. And then properly calculate the offset from the arriving address. And finally access the RAM/registers implemented on the PCIe device.

2
  • 1
    Good answer. It seems that there is a small gap between the last 2 paragraphs: a pcie device (EP) is not directly sitting on the system interconnect thus it cannot directly see the bus address issued by CPU. It has to be the Root Complex, I guess, who knows the valid memory ranges (somehow) of all devices beneath it, and then forward the access requests downstream (somehow).
    – bruin
    May 27, 2022 at 13:08
  • @bruin Yes. The root complex bridges the host domain and the PCIe domain. The host address info is encoded in the fields of PCIe TLP (Transaction Layer Packet) for various transactions. The packet travels down the PCIe hierarchy and is routed to the right endpoint device based on this address info and the pre-configured routing info in the root ports, switch ports along the way. Feb 15, 2023 at 6:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .