0
votes

Based on what I have learned from the comments and answers (thanks everyone!), I edited the question to be more targeted:

DMA:

Before first DMA, CPU has to setup things like RAM address range reserved to be used by the device for DMA. Once the setup work is done, can the device initiate the transfer at will, basically owning that part of RAM, or it still has to get some sort of permission from CPU again before every sinlge DMA transfer?

MMIO:

CPU accessing the device memory via mmio is more expensive compared to CPU accessing RAM, but I can see on my desktop, pci devices reserve hundreds of mega bytes for mmio, what is an example that this can be used efficiently (As opposed to copy the data back to RAM using DMA and then access them)?

2
Your question doesn't make too much sense to me. I think DMA generally refers to the device accessing system memory directly (i.e. without going via the CPU). Hence, direct memory access :)tangrs
"Based on what I have learned ..., I edited the question" -- No, don't replace your post like that. That makes the previous comments and answers irrelevant to the new question. If you have a follow-up question, then post a new question.sawdust

2 Answers

1
votes

Look at it from the device's perspective. The device can:

  • directly access memory itself (using DMA itself)

  • wait for the CPU to transfer data to it (by providing memory mapped IO for CPU to use)

So the question is, if the CPU can access the PCIe memory by memory-maps, why does it have to do DMAs?

The CPU doesn't use DMA at all. The entire point of DMA is to allow the CPU to do other things (or nothing) while the device does the DMA. The end result is a significant performance increase for the system as a whole - e.g. CPU/s doing lots of other work, while lots of devices (hard drive controller, video card, audio card, network card, ...) are also using DMA to transfer data around.

1
votes

CPU can access this memory as if it is DRAM, by memory mapped IO.

You're misusing terminology.
Instead of "DRAM" you should be using the term "main memory", aka system memory or RAM.
On modern computers main/system memory is implemented by some type of SDRAM (synchronous dynamic RAM).
Conflating the functional term (e.g. main memory) with the hardware implementation (e.g. DDR3 SDRAM) seems harmless, but can lead to the false syllogism that "RAM is volatile" or other misunderstandings.

Memory mapping can put the memory/memories of a PCIe device in the same address space as main memory.

CPU can transfer a chunk of data from this PCIe device's memory into real physical memory, via DMA. And then CPU can access the physical memory freely.

"Real physical memory" is redundant. What other types of "physical memory" are there? There's no "fake physical memory".

You seem to be referring to the use of a buffer in main memory as "DMA".
That is misguided.
DMA is not required in order to employ or copy data to a buffer in main memory.

So the question is, if the CPU can access the PCIe memory by memory-maps, why does it have to do DMAs?

You seem to be misusing terminology.
You might want to study this article on PCIe.

Is it because PCIe bus is slow for random access?

Accessing data from a PCIe device is very slow compared to main/system memory.
This has nothing to do with "random access".
Information (e.g. data retrieval) over the PCIe bus is accomplished with (high-speed) packets (even when the PCIe memory is mapped into processor address space).

And if so, DMA is basically a single dump to speedup frequent random access, and memory-mapped IO is for occasional access?

You're misusing terminology.

If the software is written inefficiently or only needs to use the data just once, then it might access the PCIe memory.

But if the software is going to access the data more than once or deems a "local" copy to be more efficient, then the software could allocate a buffer in main/system memory and copy the data from PCIe memory to main/system memory using either PIO (programmed I/O by the CPU) or DMA (direct memory access by a PCIe bus master or system DMA controller).

The use of buffers is widespread in computers.
A large part of "computing time" is spent on buffering and copying and moving data around.
I/O is almost always performed between a device and a buffer in main memory, even if direct device-to-device transfer is possible.
Do not mislabel the use of a buffer as "DMA".

For some info on DMA, see Why driver need to map DMA buffers when dma-engine is in device?
and dma vs interrupt-driven i/o .