How is memory mapped to certain hardware? How is MMIO accomplished exactly?

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!


Solution 1:

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.