Chapter 33 of 39

Direct Memory Access (DMA)

In Programmed I/O & Interrupts we freed the CPU from waiting for a device, but not from moving the data: every byte still passed through a CPU register inside an interrupt service routine. For a keyboard that is fine. For a disk, SSD or network card delivering millions of bytes per second it is hopeless, as we will calculate below.

The solution is Direct Memory Access (DMA): a separate hardware unit moves a whole block of data directly between the device and main memory, and the CPU is told only once, when the block is finished. This chapter explains the DMA controller, the step-by-step transfer protocol, the three transfer modes (burst, cycle stealing, transparent), the classic GATE numericals on CPU time and bus blocking, and practical issues such as cache coherence.


Why We Need DMA

Suppose a disk delivers 20 MB/s (106 bytes per MB) in 4-byte words to a 1 GHz CPU, and one interrupt costs 500 cycles.

Words per second       = 20 x 10^6 / 4 = 5 x 10^6
Interrupt cost needed  = 5 x 10^6 x 500 = 2.5 x 10^9 cycles per second
CPU available          = 1 x 10^9 cycles per second
Required fraction      = 250%   -> impossible

Even if the CPU did nothing else, it could not keep up with interrupt-per-word I/O. And the data itself is just being copied from one place to another; the CPU adds nothing to it. So we hand the copying to a specialist.

Direct Memory Access (DMA): a technique in which a DMA controller takes control of the system bus and transfers a block of data directly between an I/O device and main memory, without the data passing through the CPU. The CPU only initialises the transfer and handles a single interrupt at the end.


The DMA Controller

A DMA controller (DMAC) is like a tiny, single-purpose processor whose only job is "copy N words from here to there". It contains:

Register / logic

Purpose

Address register

Memory address of the next word to transfer; incremented (or decremented) after each word

Word count (byte count) register

Number of words still to transfer; decremented after each word; when it reaches zero the transfer is complete

Control register

Direction (read from memory or write to memory), transfer mode, enable bits

Status register

Done, error, which channel requested

Data buffer (some designs)

Holds a word on its way between device and memory

Control logic

Bus request/grant handshake with the CPU, request/acknowledge handshake with the device

                 Address bus
   +-------+  ========================================================
   |       |       |                     |                     |
   |  CPU  |  +----+-----+         +-----+------+        +-----+-----+
   |       |  |  Memory  |         |    DMA     |        |   I/O     |
   |       |  +----+-----+         | controller |        |  device   |
   +-+---+-+       |               | addr reg   |        | interface |
     |   |    =====|=== Data bus ==| count reg  |========|           |
     |   |                         | control    |        +--+-----+--+
     |   +-- BG (bus grant) ------>|            |<- DREQ --+     |
     +<----- BR (bus request) -----|            |-- DACK ------->+
              (HOLD / HLDA on Intel)  |  INTR to CPU when count = 0
                                      +--->

The CPU talks to the DMA controller's registers like any other I/O interface (using isolated or memory-mapped I/O). Many controllers have several independent channels, one per device; the classic Intel 8237 had four, each with a 16-bit address and a 16-bit count register. In modern systems the DMA engine is usually built into the device itself (a bus-mastering device such as an NVMe SSD or network card).


How a DMA Transfer Works, Step by Step

Take a disk read: a block goes from the device into memory.

  1. CPU initialises the DMA controller: writes the starting memory address, the word count, the direction (device to memory) and the mode into its registers, and tells the device interface to start. The CPU now goes back to other work.

  2. Device is ready with a word and raises DMA request (DREQ) to the controller.

  3. The controller asks the CPU for the bus by raising bus request (BR), called HOLD on Intel processors.

  4. The CPU finishes its current bus cycle, puts its address, data and control outputs into the high-impedance state (it "lets go" of the bus) and replies with bus grant (BG), called HLDA on Intel.

  5. The controller places the memory address on the address bus, activates the memory write line and sends DMA acknowledge (DACK) to the device, which places its word on the data bus. The word goes straight from device to memory.

  6. The controller increments the address register and decrements the count register.

  7. Depending on the mode, it either keeps the bus for the next word or releases it (drops BR, and the CPU drops BG and continues).

  8. When the count reaches zero, the controller releases the bus and raises an interrupt. The ISR checks the status for errors and tells the OS that the block is ready.

So the CPU's involvement is: a few register writes at the start and one interrupt at the end. Everything in between is hardware.

The CPU Gives Up the Bus More Often Than It Takes Interrupts

A subtle but examined point: the CPU recognises an interrupt only at the end of an instruction, but it can grant the bus to DMA at the end of any bus (machine) cycle, even in the middle of an instruction. Granting the bus is harmless because the CPU does not need to save any state: it simply pauses until the bus is free again. These pause points are often called DMA breakpoints, as opposed to interrupt breakpoints.

One instruction cycle:
| fetch | decode | fetch operand | execute | store result |
^        ^        ^               ^         ^              ^
DMA may take the bus at each bus-cycle boundary            Interrupt checked
                                                           only here (end)

DMA Transfer Modes

The CPU and the DMA controller share one bus. How long the controller keeps it decides how much the CPU is slowed down.

1. Burst (Block) Mode

Once it gets the bus, the controller transfers the entire block before releasing it. Fastest transfer, but the CPU cannot use the bus (fetch instructions or data from memory) for the whole burst. It suits fast devices that can deliver words as quickly as memory accepts them, usually because the device has buffered the whole block first.

2. Cycle Stealing Mode

The controller takes the bus for one word (one bus cycle), then gives it back; it requests the bus again for the next word. The CPU is delayed by one memory cycle at a time, interleaved with its own work. The block takes longer, but the CPU is never locked out for long. This suits devices that produce words more slowly than memory can store them.

3. Transparent (Hidden) Mode

The controller uses the bus only in cycles when the CPU is not using it (for example while the CPU is decoding or executing internally). The CPU is never slowed down at all, but the controller must detect idle bus cycles and the transfer is the slowest.

Mode

Bus held for

Transfer speed

CPU slowdown

Hardware complexity

Burst

The whole block

Fastest

CPU blocked for the whole burst

Simple

Cycle stealing

One word at a time

Medium

Small, spread out

Medium

Transparent

Only idle bus cycles

Slowest

None

Most complex


Worked Example 1: CPU Time Spent on a DMA Transfer

Problem. A disk transfers data to memory using DMA at 20 MB/s. The processor runs at 1 GHz and spends 500 clock cycles to initiate each DMA transfer and 1500 cycles to complete it (the ISR). Each DMA transfer moves a 16 KB block. What percentage of processor time is consumed by the disk while it is continuously transferring? (Take K = 103 and M = 106.)

Solution.

Time to transfer one block = 16 x 10^3 / (20 x 10^6) s = 0.8 ms = 800 µs
CPU cycles per block       = 500 + 1500 = 2000 cycles
CPU time per block         = 2000 / 10^9 s = 2 µs

Percentage of CPU time     = 2 / 800 x 100 = 0.25%

Compare this with 250% for interrupt-per-word I/O on the same disk. The disk now causes only 20 MB/s / 16 KB = 1250 interrupts per second instead of 5 million.

(Some solutions divide by 800 + 2 µs instead of 800 µs, giving 0.249%, which rounds to the same answer. If the question says the disk is continuously transferring, the natural reading is that the 800 µs repeats back to back, so divide by 800.)


Worked Example 2: Cycle Stealing

Problem. A device sends data to memory at 10 MB/s in 4-byte words using cycle-stealing DMA. Each stolen memory (bus) cycle takes 50 ns, during which the CPU cannot access memory. Find the worst-case percentage of time the CPU is blocked.

Words per second = 10 x 10^6 / 4 = 2.5 x 10^6
One word every   = 1 / (2.5 x 10^6) s = 400 ns
Stolen per word  = 50 ns

CPU blocked      = 50 / 400 = 12.5%  (worst case: if the CPU needed
                                      the bus in every one of those cycles)

It is "worst case" because a CPU with a cache often does not need the bus during a stolen cycle, so the real slowdown is smaller.


Worked Example 3: Burst vs Cycle Stealing for One Block

Same device and memory (a new word ready every 400 ns, memory write 50 ns), block of 1000 words.

Cycle stealing:
   block takes 1000 x 400 ns = 400 µs
   CPU blocked 1000 x 50 ns  = 50 µs, in 1000 tiny pieces (12.5%)

Burst, device has buffered the whole block first:
   bus held 1000 x 50 ns     = 50 µs in one piece
   CPU blocked 50 µs at once, free during the 400 µs the device spent filling its buffer

Burst, device NOT buffered (controller grabs the bus and waits for each word):
   bus held for the full 400 µs
   CPU blocked 400 µs = 100% of the transfer time

The CPU loses the same 50 µs in the first two cases; what differs is whether it loses it in one chunk or spread out. The third case shows why burst mode is only used with buffered (fast) devices: holding the bus while waiting for a slow device wastes it.


Worked Example 4: Size of the Count Register (GATE 2016)

Problem. The size of the data count register of a DMA controller is 16 bits. The processor needs to transfer a file of 29,154 kilobytes from disk to main memory. The memory is byte addressable. What is the minimum number of times the DMA controller needs to get control of the system bus from the processor to transfer the file?

Solution. A 16-bit count register can count up to 216 = 65,536 bytes in one DMA operation (one bus acquisition in burst mode).

File size      = 29,154 KB = 29,154 x 1024 bytes
Transfers      = 29,154 x 1024 / 65,536 = 29,154 / 64 = 455.53
Minimum number = ceil(455.53) = 456

The official answer is 456, which uses 1 KB = 1024 bytes. The fraction rounds up, because the last partial block still needs its own bus acquisition.


Variations of DMA Hardware

  • Two-cycle (flow-through) DMA: the controller reads the word into its own buffer in one bus cycle and writes it to the destination in a second. Works for memory-to-memory copies too, but uses the bus twice per word.

  • Fly-by (single-cycle) DMA: the device puts data on the bus and memory captures it in the same cycle (the controller supplies only the address and control signals). Half the bus traffic; this is the mode our step-by-step description used.

  • Scatter-gather DMA: the controller follows a list of (address, length) descriptors in memory, so one operation can fill many non-contiguous buffers. Network cards and NVMe SSDs work this way, which is why the OS does not need large physically contiguous buffers.

  • Configurations: Stallings describes three bus layouts: a single bus shared by CPU, DMA and devices (each word crosses the bus twice with a separate DMA module); DMA integrated with the I/O module; and a separate I/O bus behind the DMA module (each word crosses the system bus once).


DMA and the Cache

DMA reads and writes main memory directly, but the CPU may be working with copies of the same addresses in its cache (see Cache Memory and Cache Replacement & Write Policies). Two problems arise:

  • DMA input (device to memory): the cache may still hold the old data for those addresses, so the CPU reads stale values.

  • DMA output (memory to device) with a write-back cache: the newest data may still be dirty in the cache and not yet in memory, so the device sends stale values.

Solutions: mark DMA buffers non-cacheable; have the driver flush (write back) dirty lines before a DMA output and invalidate cached lines after a DMA input; or use hardware that snoops DMA traffic and keeps the cache coherent automatically, as most x86 systems do. Modern systems also put an IOMMU between devices and memory, which translates device addresses and stops a faulty or malicious device from writing anywhere in memory.


Comparison of the Three I/O Techniques

Point

Programmed I/O

Interrupt-driven I/O

DMA

Who moves the data

CPU

CPU (in the ISR)

DMA controller

Data passes through CPU registers?

Yes

Yes

No

CPU involvement per block

Whole transfer time

One interrupt per word

Setup + one interrupt per block

CPU slowed by bus sharing?

Not applicable

Not applicable

Yes (burst or cycle stealing), except transparent mode

Extra hardware

None

Interrupt logic

DMA controller, bus arbitration

Suitable for

Simple, slow, rarely used devices

Slow, unpredictable devices

High-speed block devices: disk, SSD, network, audio, GPU


Common Mistakes

  • "With DMA the CPU is not involved at all." The CPU sets up the controller, handles the completion interrupt, and loses bus cycles to the controller.

  • "The CPU is idle during DMA." No, it keeps executing; it pauses only when it needs the bus at the same time as the controller.

  • Thinking DMA waits for the end of an instruction. The bus can be granted at the end of any bus cycle; interrupts are taken at the end of an instruction.

  • Rounding the count-register answer down. The last partial block still needs a transfer: use the ceiling.

  • Mixing KB = 1000 and KB = 1024 inside one calculation. Pick one convention (the question's) and use it consistently; in ratio problems it often cancels.

  • Forgetting cache coherence when a device writes memory behind the cache's back.


GATE and Interview Questions

A hard disk with a transfer rate of 10 MB/s is constantly transferring data to memory using DMA. The processor runs at 600 MHz and takes 300 and 900 clock cycles to initiate and complete a DMA transfer. If each transfer is 20 KB, what percentage of processor time is consumed? (GATE 2004)

Transfer time = 20 KB / 10 MB/s = 2 ms. CPU time = (300 + 900) / 600 MHz = 2 µs. Percentage = 2 µs / 2 ms = 0.1%.

Which signals are used between the CPU and a DMA controller, and between the controller and the device?

Controller to CPU: bus request (HOLD); CPU to controller: bus grant (HLDA). Device to controller: DMA request (DREQ); controller to device: DMA acknowledge (DACK). The controller also raises an interrupt when the count reaches zero.

A DMA controller has a 12-bit word count register and the memory is byte addressable. How many bus acquisitions (burst mode) are needed to transfer 1 MB (220 bytes)?

Each transfer moves at most 212 = 4096 bytes. 220 / 212 = 28 = 256 acquisitions.

A device transfers 2-byte words at 1 MB/s by cycle stealing; each stolen cycle is 100 ns. What fraction of time can the CPU be blocked?

Words per second = 106 / 2 = 5 x 105, so one word every 2 µs = 2000 ns. Blocked fraction = 100 / 2000 = 5% (worst case).

Difference between burst mode and cycle stealing?

Burst mode keeps the bus until the whole block is transferred (fast transfer, CPU locked out for the burst). Cycle stealing takes the bus for one word at a time and returns it (slower transfer, CPU delayed only one cycle at a time).

Why can DMA cause stale data in caches, and how is it handled?

DMA writes main memory directly, so cached copies become stale; and with a write-back cache, memory itself may be stale when DMA reads it. Handled by non-cacheable DMA buffers, explicit cache flush/invalidate by the driver, or hardware snooping of DMA transactions.


Summary

  • Interrupt-per-word I/O cannot keep up with fast devices (our 20 MB/s disk would need 250% of the CPU). DMA moves whole blocks between device and memory without the CPU.

  • The DMA controller has address, count and control registers; it uses DREQ/DACK with the device and BR/BG (HOLD/HLDA) with the CPU, and interrupts when the count reaches zero.

  • The CPU can grant the bus after any bus cycle; it takes interrupts only after an instruction.

  • Modes: burst (whole block, fastest, CPU locked out), cycle stealing (one word at a time), transparent (only idle cycles, no slowdown).

  • Key formulas: CPU % = (setup + completion time) / block transfer time; cycle-stealing blocking = memory cycle / time between words; number of DMA operations = ceil(total bytes / max count).

  • DMA must be kept coherent with caches (non-cacheable buffers, flush/invalidate or snooping).

This completes the I/O part of the course. Next, in Pipelining Basics, we return to the CPU and see how it overlaps the execution of many instructions to run much faster.