Sub topic 7
Why DMA: Moving Blocks Without the CPU
Prerequisites and the basic problem
This chapter assumes that you understand the CPU, memory, buses, registers, instruction cycles, and subroutine calls. You should also know that an I/O device communicates with the computer through an interface containing data, status, and control registers.
Direct memory access (DMA) is a technique for transferring a block of data between an I/O device and main memory without requiring the CPU to execute one instruction for every transferred word.
Consider a disk reading a large file. With programmed I/O, the CPU repeatedly checks the device status register and moves each word itself. With interrupt-driven I/O, the device interrupts the CPU repeatedly, perhaps once per word or once per small buffer. Both methods make the CPU participate in every part of the transfer.
DMA changes this arrangement:
- The CPU configures a DMA controller.
- The DMA controller obtains control of the system bus.
- The controller transfers data directly between the device and memory.
- The DMA controller informs the CPU when the block is complete.
The CPU still starts and supervises the operation, but it does not handle every word.
Why per-word CPU involvement is expensive
A transfer requires more than copying a value. The CPU may need to:
- Test whether the device is ready.
- Read or write the device data register.
- Read or update a memory location.
- Decrease a byte or word counter.
- Branch back to repeat the loop.
- Save and restore registers during an interrupt.
For a small, slow device, this overhead may be acceptable. For a fast device or a large block, the CPU can spend most of its time moving data instead of running application code.
The difference is especially important when the device transfers data at a high rate. An interrupt once per word causes many interrupt entries and exits. DMA replaces many small CPU actions with a small number of larger actions: setup, possible progress management, and completion handling.
| Transfer technique | CPU activity during the block | Typical use |
|---|---|---|
| Programmed I/O | Moves every word and polls status | Simple or very slow devices |
| Interrupt-driven I/O | Services frequent device interrupts | Moderate-rate devices |
| DMA | Sets up the transfer and handles completion | High-rate block devices |
The DMA transfer model
A DMA controller is hardware connected to the system bus and to one or more I/O interfaces. It contains registers such as:
- A memory address register, which stores the next memory location.
- A transfer count register, which stores the number of bytes or words remaining.
- A control or status register, which records direction, device selection, and completion state.
For a device-to-memory transfer, the device supplies data to the DMA controller. The controller writes that data into successive memory locations. After each transfer, it updates the memory address and decreases the count.
For a memory-to-device transfer, the direction is reversed. The DMA controller reads from memory and supplies the data to the device.
The CPU normally programs the controller before the transfer begins. It provides the starting memory address, transfer length, direction, and device or channel. When the count reaches zero, the controller can raise an interrupt so the CPU can process the completed buffer.
CPU overhead is reduced, not eliminated
DMA does not make the CPU irrelevant. The CPU pays a setup cost and usually handles a completion interrupt. The DMA controller also uses the bus, so the CPU may temporarily wait for memory access or share bus cycles with the transfer.
The important comparison is the number of CPU actions. Suppose a device transfers 1 MB/s, each word contains 4 bytes, and servicing one interrupt costs 1,000 CPU cycles. If the device interrupts once per word, it generates 250,000 interrupts per second. A DMA transfer using 4,096-byte blocks generates only about 245 block operations per second.
rate_bytes_per_s = 1_000_000
word_bytes = 4
interrupt_cycles = 1_000
cpu_hz = 500_000_000
words_per_s = rate_bytes_per_s / word_bytes
interrupt_cpu_fraction = words_per_s * interrupt_cycles / cpu_hz
block_bytes = 4096
blocks_per_s = rate_bytes_per_s / block_bytes
dma_setup_cycles = 2000
dma_completion_cycles = 1000
dma_cpu_fraction = blocks_per_s * (dma_setup_cycles + dma_completion_cycles) / cpu_hz
print(f"Interrupt-driven CPU time: {interrupt_cpu_fraction:.2%}")
print(f"DMA CPU time: {dma_cpu_fraction:.4%}")The exact result depends on the hardware and transfer mode. DMA can use a burst of bus cycles, take one cycle at a time, or transfer only when the CPU is not using the bus. These modes affect CPU performance and are examined separately.
GATE-style reasoning
When a problem compares DMA with interrupts, identify the unit that causes CPU overhead:
- Per word or byte: multiply the device rate by the CPU cycles per service.
- Per block: divide the device rate by the block size, then multiply by setup and completion costs.
- CPU fraction: divide total overhead cycles per second by the CPU clock rate.
Exercise
A device transfers 2,000,000 bytes per second. An interrupt-driven design services one 4-byte word at a time, and each service costs 800 CPU cycles. A DMA design transfers 8,000-byte blocks, with 4,000 CPU cycles per block. The CPU runs at 400 MHz. What fraction of CPU time does each design consume?
Show answer
Interrupt-driven I/O uses (2,000,000 / 4) × 800 / 400,000,000 = 1.00% of CPU time. DMA uses (2,000,000 / 8,000) × 4,000 / 400,000,000 = 0.25% of CPU time.