Part 5 of 7 · 2 min read
Direct memory access
Think of a manager who gives a courier a list: "take these 100 boxes from the van to the shelf", and goes back to work. The courier calls the office once, at the end. The analogy stops in one place: a courier carries boxes down a separate path, while a DMA transfer uses the same memory bus that the processor needs.
In plain words: Direct memory access (DMA) lets the device, or a DMA controller, copy data to or from main memory without the processor. The driver gives it three things: the memory address, the number of bytes, and the direction. The processor then does other work. The controller moves all the data, and raises one interrupt when the whole transfer is done. The processor pays a small set-up cost and one interrupt, however large the transfer is.
A worked example (made up to show the idea). Moving 1 MiB with programmed I/O needs 1,048,576 / 4 = 262,144 words at 20 ns, so the processor is busy for 5.24 ms. With DMA the processor spends 5 microseconds on set-up and 5 microseconds on the final interrupt, which is 10 microseconds. The ratio is 5.24 ms / 10 microseconds, which is about 524. A device that moves data at 100 MB/s needs 1,048,576 / 100,000,000 s = 10.5 ms for the transfer. During that time the processor is free, and the device uses part of the memory bus, so the processor may see slightly slower memory.
The key lines. The driver must give the device a physical address, because the device does not use the page tables of a process. The memory must also not be moved or paged out during the transfer. Linux therefore pins the pages, and uses an IOMMU on many machines to translate the addresses that a device uses.
Direct memory access (DMA)
Direct memory access is a method in which a device or a DMA controller transfers data directly between the device and main memory, without the processor copying each word. The processor gives the address, the size and the direction, and it receives one interrupt when the transfer is complete.
Example: a disk read of 1 MiB into a buffer is started by the driver with one command, and the processor is interrupted once when the data is in memory.
Interview angle
A common question is "What is DMA and why is it used?" A good answer says that DMA moves the data without processor work, that the processor sets up a transfer and takes one interrupt at the end, and that the data must be at physical addresses that stay in place during the transfer.
Check yourself: 1. What three things does the driver give a DMA controller? 2. Why is DMA better than programmed I/O for a large transfer but possibly not for a transfer of 4 bytes? 3. Why must the buffer be pinned in memory?