01. Can data be moved without the CPU?
In a computer, components exchange information over buses, which act like main communication highways. Buses are classified by the type of information they carry: data bus, address bus, and control bus. They can also be categorized by the devices they connect, such as main memory bus and I/O bus.
The CPU is the central component of a computer system. It communicates with main memory via the main memory bus and with external devices such as disks and network cards via the I/O bus. The CPU performs data movement, computation, and control flow, and it constantly processes many tasks. However, simple data copy and storage operations are sometimes less important compared with other tasks the CPU must handle.
For example, while a CPU is busy decoding and rendering a video, initiating a file copy from a USB drive (peripheral A) to a local disk (peripheral B) will trigger an interrupt and require CPU time to handle the copy, potentially affecting video playback.
Is it possible to transfer data from peripheral A to peripheral B while minimizing or eliminating CPU involvement?
Yes. The idea is to provide a direct data path between peripheral A and peripheral B so data can be copied from A to B without passing through the CPU.
02. What is DMA and how does it enable direct transfers?
DMA stands for Direct Memory Access. It provides high-speed data transfer between peripherals and memory or between memory regions without CPU intervention. With DMA, data can be moved quickly while freeing the CPU to perform other tasks.
Transfers are performed under the control of a DMA controller. Apart from short processing at the start and end of a transfer (typically handling interrupts), the CPU is not involved during the data movement. This allows CPU computation and I/O operations to proceed in parallel, improving overall system efficiency.
DMA eliminates the need for the CPU and its registers to participate in traditional data transfer loops. Common cases include peripheral-to-memory, memory-to-peripheral, memory-to-memory, and peripheral-to-peripheral transfers. Conceptually, these are all transfers from one memory address range to another, since a peripheral data register is effectively a memory location.
03. How is DMA used in Linux?
Linux supports three mechanisms for data transfer between disks and main memory: polling, I/O interrupts, and DMA transfers.
- Polling uses a busy loop to repeatedly check the I/O port.
- I/O interrupt mode means the disk issues an interrupt when data is ready, and the CPU performs the transfer.
- DMA mode adds a DMA disk controller that handles the data transfer, reducing CPU resource consumption compared with I/O interrupts.
I/O interrupt flow
Before DMA, I/O between applications and disks relied on CPU-driven interrupts. Each disk read caused a CPU interrupt and a context switch:
- The user process issues a read system call, switching from user mode to kernel mode and blocking while waiting for data.
- The CPU issues an I/O request to the disk; the disk controller buffers the data.
- When data is ready, the disk raises an I/O interrupt.
- The CPU handles the interrupt, copying data from the disk controller buffer to a kernel buffer, then from the kernel buffer to the user buffer.
- The user process returns to user mode and resumes execution when scheduled.
DMA transfer flow
With a DMA disk controller handling reads and writes, the CPU is relieved of most I/O work. The sequence becomes:
- The user process issues a read system call, switching to kernel mode and blocking while waiting for data.
- The CPU issues a command to the DMA disk controller to perform the transfer.
- The DMA controller issues the I/O request to the disk and moves data into the controller buffer without CPU involvement.
- When the disk notifies completion, the DMA controller copies data from its buffer to the kernel buffer.
- The DMA controller signals the CPU that the read is complete; the CPU then copies data from the kernel buffer to the user buffer.
- The user process returns to user mode and resumes execution when scheduled.
04. Summary
Most modern hardware devices, including disk controllers, network cards, GPUs, and sound cards, support DMA. Combined with virtual memory, DMA reduces the number of data copies and system calls when moving data between I/O devices and user-space programs, minimizing CPU involvement in such transfers and lowering CPU load for I/O tasks.