Sub topic 7
Why DMA: moving blocks without the CPU
This chapter assumes you know the instruction cycle, the roles of the CPU, memory and system bus, how programmed I/O polls a status register, and how an interrupt is accepted and serviced by an ISR. We now ask what remains expensive about interrupt-driven I/O, and how direct memory access (DMA) removes that cost.
The problem with word-at-a-time transfer
Programmed I/O and interrupt-driven I/O share one property: every word of data passes through the CPU. The CPU reads the device's data register into a CPU register, then writes it to memory (or the reverse for output). Each word therefore costs instructions, and in interrupt-driven I/O it also costs a full interrupt sequence.
Consider what one interrupt-driven word transfer involves:
- The device raises an interrupt.
- The CPU finishes the current instruction and saves the program counter and flags.
- The CPU jumps to the ISR through the interrupt vector.
- The ISR saves registers, reads the data register, stores the word in a buffer, updates a counter, and restores registers.
- The CPU returns to the interrupted program.
This sequence can cost hundreds or thousands of cycles. That is acceptable for a keyboard, which produces a few bytes per second. It is a disaster for a disk or network card that produces megabytes per second. The CPU would spend most of its time entering and leaving ISRs, and the interrupt overhead would dominate the useful work.
The key observation is that the CPU adds no value to a block transfer. It does not transform the data. It only acts as a courier between the device register and memory.
The DMA idea
Direct memory access (DMA) lets a dedicated hardware unit, the DMA controller, move data between a device and main memory without executing CPU instructions for each word. The CPU only does two things:
- Before the transfer, it programs the DMA controller: which device, which direction, where in memory, and how many words.
- After the transfer, it handles one interrupt that says the whole block is finished.
In between, the DMA controller generates memory addresses and issues the read and write signals itself. The CPU is free to run other instructions, except for the moments when the DMA controller needs the bus.
The cost structure changes from "per word" to "per block". That is the entire advantage of DMA, and it is the reason the GATE calculations in this topic compare per-word costs against per-block costs.
What the CPU still does
DMA does not remove the CPU from I/O entirely. A typical block transfer proceeds in three phases.
| Phase | Who acts | Work done |
|---|---|---|
| Setup | CPU | Writes the memory start address, word count, direction and device ID into DMA controller registers, then starts the transfer |
| Transfer | DMA controller | Requests the bus, moves each word between device and memory, increments the address, decrements the count |
| Completion | CPU | Takes one interrupt when the count reaches zero, then checks status and uses the data |
The setup typically costs a few instructions, perhaps a few thousand cycles once the operating system's driver overhead is included. The completion interrupt costs one ISR. Both costs are paid once per block, no matter how large the block is.
Why the bus is the shared resource
The CPU and the DMA controller both need the system bus (address, data and control lines) to reach memory. Only one of them can drive the bus at a time. The DMA controller therefore has to ask for permission, and the CPU has to give the bus up. Chapter detail on the request and grant signals comes next, but you need the basic consequence now:
- While the DMA controller owns the bus, the CPU cannot access main memory.
- The CPU can still execute instructions that need only registers or cache, so it is not always stopped completely.
- DMA reduces CPU work but does not make memory bandwidth free.
This bus sharing is why DMA has several transfer modes (burst, cycle stealing, transparent), which are the subject of a later chapter. They are different policies for deciding how much bus time the DMA controller takes and when.
Worked comparison: 1 MB/s device on a 500 MHz CPU
The numbers below match the code example in the topic plan. Assume:
- Device rate: 1,000,000 bytes per second.
- Word size: 4 bytes.
- CPU clock: 500 MHz, so 500,000,000 cycles per second.
- Interrupt-driven I/O: one interrupt per word, 1000 cycles per ISR.
- DMA: 4096-byte blocks, 2000 cycles of setup, 1000 cycles for the completion interrupt.
Interrupt-driven cost
Words per second: 1,000,000 / 4 = 250,000.
Cycles per second spent in ISRs: 250,000 × 1000 = 250,000,000.
Fraction of CPU time: 250,000,000 / 500,000,000 = 0.5, which is 50%.
DMA cost
Blocks per second: 1,000,000 / 4096 ≈ 244.14.
Cycles per block: 2000 + 1000 = 3000.
Cycles per second: 244.14 × 3000 ≈ 732,422.
Fraction of CPU time: 732,422 / 500,000,000 ≈ 0.00146, about 0.15%.
The same device costs the CPU half its time under interrupts and a seventh of one percent under DMA. The ratio is roughly 340 to 1. Two things produce it: a block holds 1024 words, and the per-block cost (3000 cycles) is only three times the per-word cost (1000 cycles).
You can verify this with the code from the plan:
# CPU time used by a 1 MB/s device on a 500 MHz CPU
rate_bytes_per_s = 1_000_000
word_bytes = 4
cpu_hz = 500_000_000
# Interrupt-driven: one interrupt per word, ISR costs 1000 cycles
words_per_s = rate_bytes_per_s / word_bytes
interrupt_fraction = words_per_s * 1000 / cpu_hz
# DMA: one transfer per 4096-byte block, 2000 cycles setup + 1000 cycles completion interrupt
block_bytes = 4096
blocks_per_s = rate_bytes_per_s / block_bytes
dma_fraction = blocks_per_s * (2000 + 1000) / cpu_hz
print(f"Interrupt-driven: {interrupt_fraction:.2%} of CPU time")
print(f"DMA: {dma_fraction:.4%} of CPU time")Break-even: when does DMA stop paying off?
DMA has a fixed cost per transfer, so it is not always the best choice. Let the per-word interrupt cost be W cycles and the per-block DMA cost be B cycles. For a block of n words:
- Interrupt-driven cost: n × W
- DMA cost: B
DMA wins when n × W > B, that is, when n > B / W. With W = 1000 and B = 3000, the break-even is 3 words. Any block larger than 3 words is cheaper by DMA in this model. In real systems the setup overhead (driver code, address translation, cache maintenance) is larger, so small transfers are often still done with programmed or interrupt-driven I/O.
Where the data lands: practical edge cases
Several details matter in exam questions and in real systems.
- Cache coherence. The DMA controller writes to memory directly. If the CPU has an older copy of that memory in its cache, it may read stale data. Systems either invalidate the cache lines before reading or use hardware that snoops DMA writes. For output, dirty cache lines must be written back before DMA reads memory.
- Virtual and physical addresses. A DMA controller usually works with physical addresses. If the buffer spans several pages that are not contiguous in physical memory, the driver must split it into several transfers or use scatter-gather lists, unless an I/O memory management unit translates addresses.
- Word count is fixed in advance. The controller stops when its count register reaches zero. If the device has less data than expected, the driver must handle the shortfall, typically through a status register or an error interrupt.
- Interrupts still exist. DMA does not replace interrupts. It reduces them from one per word to one per block, and the completion interrupt is how the CPU learns the transfer is done. Some systems also raise an interrupt on error.
- Programming the controller too late. If the device starts producing data before the DMA controller is programmed, data may be lost unless the device has its own buffer.
Comparing the three techniques
| Property | Programmed I/O | Interrupt-driven I/O | DMA |
|---|---|---|---|
| CPU waits for device | Yes, busy-waits | No | No |
| Path of data | Device, CPU, memory | Device, CPU, memory | Device to memory directly |
| CPU involvement per unit | Every word, plus all waiting time | Every word, one interrupt each | Once per block |
| Hardware added | Minimal | Interrupt logic | DMA controller |
| Suited to | Very fast simple devices, tiny systems | Slow or sporadic devices | High-rate block devices such as disks |
The progression follows the concept graph for this topic: polling wastes cycles on waiting, interrupts remove the waiting but keep the per-word cost, and DMA removes the per-word cost.
Common errors
- Saying DMA uses no CPU time. It uses setup and completion time, plus bus contention.
- Using the word count where the byte count is needed, or the reverse. Check whether the problem gives rate in bytes or words.
- Forgetting that the interrupt in DMA fires once per block, not once per word.
- Mixing units, such as MHz with cycles per second, or MB as 10^6 versus 2^20 bytes. State the convention you use and follow the problem's statement.
- Assuming DMA always wins. For very small transfers the fixed setup cost can exceed the per-word cost.
Exercise
A disk delivers data at 2 MB/s (1 MB = 10^6 bytes) in 4-byte words. The CPU runs at 1 GHz. Interrupt-driven I/O costs 500 cycles per word. DMA moves 8000-byte blocks and costs 4000 cycles of setup plus 2000 cycles for the completion interrupt per block. What fraction of CPU time does each method use, and for what block size in words does DMA become cheaper than interrupts?
Show answer
Interrupt-driven: words per second = 2,000,000 / 4 = 500,000. Cycles per second = 500,000 × 500 = 250,000,000. Fraction = 250,000,000 / 1,000,000,000 = 0.25, or 25%. DMA: blocks per second = 2,000,000 / 8000 = 250. Cycles per second = 250 × (4000 + 2000) = 1,500,000. Fraction = 1,500,000 / 1,000,000,000 = 0.0015, or 0.15%. Break-even: n × 500 > 6000 gives n > 12 words, so DMA is cheaper for any block larger than 12 words (48 bytes).
Exercise
Give two reasons why a system with DMA still needs interrupts, and one reason why the CPU can still be slowed down while DMA is running.
Show answer
The DMA controller needs a way to tell the CPU that the block transfer is complete, which is done with an interrupt. It can also use an interrupt to report errors. The CPU can still be slowed because the DMA controller and the CPU share the system bus, so when the DMA controller holds the bus the CPU cannot access main memory and must wait for any instruction or data fetch that misses the cache.