34

I've read about the difference between port mapped IO and memory mapped IO, but I can't figure out how memory mapped Io is implemented in modern operating systems (windows or linux)

What I know is that a part of the physical memory is reserved to communicate with the hardware and there's a MMIO Unit involved in taking care of the bus communication and other memory-related stuff

How would a driver communicate with underlying hardware? What are the functions that the driver would use? Are the addresses to communicate with a video card fixed or is there some kind of "agreement" before using them?

I'm still rather confused

paulAl
  • 949
  • 2
  • 10
  • 17
  • 1
    Have you looked at [Using I/O Memory](http://www.makelinux.net/ldd3/chp-9-sect-4) and [Memory Mapping and DMA](http://www.makelinux.net/ldd3/chp-15.shtml)? – Appleman1234 Mar 11 '12 at 11:38
  • 1
    Let's see if I got it(in linux): 1) I assign a memory IO area at the address the hardware producer told me to 2) I use ioremap to get it translated from physical addresses to virtual addresses (so this resolves both segmentation and paging I suppose) 3) I use iowrite/ioread and similar to read and write in that area. Is this correct? – paulAl Mar 11 '12 at 11:58
  • That is correct, remember to free to allocate memory region after usage. – Appleman1234 Mar 11 '12 at 12:12

3 Answers3

44

The following statement in your question is wrong:

What I know is that a part of the physical memory is reserved to communicate with the hardware

A part of the physical memory is not reserved for communication with the hardware. A part of the physical address space, to which the physical memory and memory mapped IO are mapped, is. This memory layout is permanent, but user programs do not see it directly - instead, they run into their own virtual address space to which the kernel can decide to map, wherever it wants, physical memory and IO ranges.

You may want to read the following articles which I believe contain answers to most of your questions:

Gnurou
  • 7,923
  • 3
  • 22
  • 32
3

http://en.wikipedia.org/wiki/Memory-mapped_I/O

http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/IO/mapped.html

Essentially it is just a form of accessing the data, as if you are saving / reading from the memory. But the hardware will snoop on the address bus, and when it sees the address targetting for him, it will just receive the data on the data bus.

Peter Teoh
  • 6,337
  • 4
  • 42
  • 58
2

Are you asking about Memory mapped files, or memory mapped port-IO?

Memory mapped files are done by paging out the pages and intercepting page-faults to those addresses. This is all done by the OS by negotiation between the file-system manager and the page-fault handler.

Memory mapped port-IO is done at the CPU level by overloading address lines as port-IO lines which allow writes to memory to be translated onto the QPI bus lines as port-IO. This is all done by the processor interacting with the motherboard. The only other thing that the OS needs to do is to tell the MMU not to coalese reads and writes through the PAE must-writethrough and no-cache bits.

SecurityMatt
  • 6,593
  • 1
  • 22
  • 28
  • Some expansion of the acronyms would be very helpful here. – Rikki Oct 10 '16 at 13:41
  • This is actually the answer that guided me to the write direction. "As far as a program is concerned, addresses in its logical address space are always available. However, if an application accesses an address on a memory page that is not currently in physical RAM, a page fault occurs. When that happens, the virtual memory system invokes a special page-fault handler to respond to the fault immediately." This handler may do different things, including interaction with disk. – Konstantin Nikitin Nov 30 '17 at 00:18