Introduction

Direct memory access (DMA) lets an I/O interface move a block of data between a device and main memory without the processor handling each word. The processor programs a DMA controller with a memory address, a word count and a direction, and goes back to other work; the controller takes control of the system bus when it needs it, transfers the words, and normally raises one completion interrupt when enabled; completion may instead be polled.

GATE asks you to compute the fraction of time the processor is held off the bus under burst and cycle-stealing DMA, to compare DMA with interrupt-driven and programmed I/O for the same device, to work out block transfer times, and to describe how the bus is handed between the processor and the DMA controller. Programmed and interrupt-driven I/O are covered in I/O Interface and Interrupts.

Prerequisites

Core concepts

The DMA controller

RegisterRole
Address registerMemory address of the next word; incremented after each transfer
Word count registerWords still to transfer; decremented after each transfer
Control and status registerDirection (read or write memory), mode, interrupt enable, done flag

A controller may have several channels, each with its own set of registers, so several devices can share it.

A DMA transfer, step by step

  1. The processor writes the starting address, the word count and the command into the controller, then continues with other work (often another process).
  2. When the device has data, the controller asserts bus request (BR, or HOLD).
  3. The processor (or the bus arbiter) finishes its current bus cycle and asserts bus grant (BG, or HLDA), releasing the bus.
  4. The controller places the address on the bus and moves the word between device and memory; it updates its address and count.
  5. Depending on the mode, it releases the bus after one word or after the whole block.
  6. When the count reaches zero, the controller sets its done flag and interrupts the processor once if enabled; otherwise completion can be polled.

The processor can be held off the bus in the middle of an instruction, at any bus cycle boundary, unlike an interrupt, which waits for the instruction to finish: DMA only borrows bus cycles and does not need the processor's registers.

Modes

ModeBus use by the controllerEffect on the processor
Burst (block)Keeps the bus for the whole blockBlocked from the bus for the entire transfer; fastest for the device
Cycle stealingTakes the bus for one word at a time, then releases itLoses one bus cycle per word, spread out; the processor keeps running between steals
Transparent (hidden)Uses the bus only in cycles when the processor does not need itNo slowdown; needs logic to detect free cycles, and the transfer may be slow

Bus arbitration

When several masters (the processor and DMA controllers) can drive the bus, an arbiter decides who gets it. In centralised arbitration, one arbiter receives the requests; the grant may be daisy-chained, so the controller nearest the arbiter has the highest priority, exactly as with interrupt acknowledge. In distributed arbitration, the masters compare their priority codes on shared lines and the highest wins without a central arbiter.

Bus use over 16 bus cycles in burst, cycle-stealing and transparent DMA
Figure 1. Sixteen bus cycles transferring 4 words: burst DMA takes cycles 1 to 4 together, cycle stealing takes cycles 1, 5, 9 and 13, and transparent DMA uses only cycles 3, 7, 11 and 15, which the processor does not need; the processor uses all the other cycles.

How it works

Why DMA scales to fast devices

With interrupt-driven I/O the processor pays for an interrupt per unit of transfer; with DMA it pays one set-up per block and normally one completion interrupt when enabled (or a polling cost instead). The only remaining cost is bus contention: each word moved by DMA occupies one bus cycle that the processor might have wanted.

Algorithms and pseudocode

This pseudocode uses word addresses, so +1 advances one word; count is in words. For byte-addressable memory, advance the address by the number of bytes per word instead.

Text
DMA-CONTROLLER(address, count, direction, mode) // mode is BURST or CYCLE-STEALING only
  while count > 0
      wait until the device has a word ready (or, in burst mode, the block is buffered)
      assert BR;  wait for BG                       // processor releases the bus
      repeat
          transfer one word between device and memory[address]
          address <- address + 1;  count <- count - 1
      until mode = CYCLE-STEALING or count = 0      // burst keeps the bus
      release the bus (drop BR)
  set done;  raise a completion interrupt if enabled (otherwise completion may be polled)

Mathematical explanation

Cycle stealing. If the device transfers at RR bytes per second in words of ww bytes, and each steal holds the bus for tct_c seconds, the fraction of bus time taken is

Rw tc.\frac{R}{w}\, t_c .

That is the processor's worst-case slowdown, reached only if it needs the bus in every cycle.

Burst mode with a buffered device. If the device fills a buffer of BB bytes at rate RR (taking tprep=B/Rt_{\text{prep}} = B / R) and the controller then empties it in a burst taking ttxt_{\text{tx}}, the processor is blocked for

ttxtprep+ttx\frac{t_{\text{tx}}}{t_{\text{prep}} + t_{\text{tx}}}

of the time, when preparation and transfer do not overlap.

Overlapped preparation. If the device prepares the next unit while the controller moves the previous one (and the move is the shorter of the two), a new unit is ready every tprept_{\text{prep}}, so the cycle length is tprept_{\text{prep}} and the processor is blocked for

ttxtprep\frac{t_{\text{tx}}}{t_{\text{prep}}}

of the time. The same two formulas apply per word in cycle-stealing mode, with tprept_{\text{prep}} the time the device needs per word and ttxt_{\text{tx}} one stolen bus cycle. Read whether the question lets preparation and transfer overlap: the two answers are close when ttx≪tprept_{\text{tx}} \ll t_{\text{prep}} but are not equal.

Processor cost per block. Set-up cycles ++ completion interrupt cycles, paid R/BR / B times per second; as a fraction of a clock of ff Hz:

RB⋅csetup+cintf.\frac{R}{B} \cdot \frac{c_{\text{setup}} + c_{\text{int}}}{f}.

Worked examples

Example 1: DMA against interrupts for a disk

A disk delivers 8 MB/s (8×1068 \times 10^6 bytes per second) to a 2 GHz processor. With interrupts per 16-byte unit at 1000 cycles each, the processor spends 25% of its time (see I/O Interface and Interrupts). With DMA in 4 KB (4096-byte) blocks, 1000 cycles of set-up and 500 cycles for the completion interrupt:

8×1064096×15002×109≈1953×7.5×10−7≈0.146%.\frac{8 \times 10^6}{4096} \times \frac{1500}{2 \times 10^9} \approx 1953 \times 7.5 \times 10^{-7} \approx 0.146\%.

Example 2: cycle-stealing slowdown

A device transfers 2 MB/s in 4-byte words by cycle stealing; each steal takes one 100 ns bus cycle. Steals per second: 2×106/4=5×1052 \times 10^6 / 4 = 5 \times 10^5, occupying 5×105×1005 \times 10^5 \times 100 ns =0.05= 0.05 s per second, so 5% of bus time. If the processor needs the bus in only 40% of its cycles and the steals fall independently of its bus use, about 40% of the steals collide with a processor bus cycle, so it loses about 0.4×5%=2%0.4 \times 5\% = 2\% of its cycles; 5% is the worst case.

Example 3: burst mode with a buffer

A device fills a 2000-byte buffer at 1 MB/s (10610^6 bytes per second), then the controller empties it into memory over a 100 MB/s bus in burst mode. Preparation: 2000/106=22000 / 10^6 = 2 ms. Transfer: 2000/108=20 μ2000 / 10^8 = 20\ \mus. The processor is blocked for 20/(2000+20)≈0.99%20 / (2000 + 20) \approx 0.99\% of the time. If the device keeps filling a second buffer while the first is being emptied, a burst starts every 2 ms and the blocked fraction is 20/2000=1%20 / 2000 = 1\%.

The same device-paced reasoning per word: a device that prepares one word every 2 μ\mus, with each steal taking 50 ns, blocks the processor for 50/2000=2.5%50/2000 = 2.5\% of the time if preparation overlaps the steal, and 50/2050≈2.44%50/2050 \approx 2.44\% if the device waits for each steal to finish before preparing the next word.

Example 4: the three modes on one timeline (Figure 1)

A block of 4 words is moved during 16 bus cycles. The processor needs the bus in every cycle except 3, 7, 11 and 15 (when it is executing internally).

ModeCycles taken by DMAProcessor bus cycles lost
Burst1, 2, 3, 43 (cycle 3 was free anyway), all at the start
Cycle stealing1, 5, 9, 134, one every 4 cycles
Transparent3, 7, 11, 150

Burst delivers the block soonest (by cycle 4); transparent delivers it last (cycle 15) but costs the processor nothing.

Example 5: transfer time of a block

A 512-word block moves by cycle stealing, one word every 10 bus cycles of 50 ns (the device's pace). The last word moves 511 periods after the first, so the transfer spans 511×10+1=5111511 \times 10 + 1 = 5111 bus cycles ≈255.6 μ\approx 255.6\ \mus, of which only 512 cycles (25.6 μ\mus) are taken from the processor.

Related material. DMA reads and writes main memory directly. If a cache holds a copy of a line that DMA overwrites, the processor would read stale data; if a write-back cache holds a dirty line that DMA reads from memory, the device would get stale data. Systems either flush or invalidate the affected lines in software before and after DMA, or let the cache snoop DMA bus traffic.

Important properties and observations

  • Normally one enabled completion interrupt per block, not per word, is what makes DMA cheap for the processor.
  • The processor can be held off between bus cycles, even mid-instruction; interrupts wait for the instruction to end.
  • Burst is fastest for the transfer and worst for processor latency; cycle stealing spreads the cost.
  • Transparent DMA has no processor cost but depends on free bus cycles.
  • The bus-time fraction taken by cycle stealing is (R/w) tc(R/w)\,t_c, an upper bound on the slowdown.
  • Daisy-chained bus grant gives priority by position, as in interrupt acknowledge.
  • Common misconceptions.
    • "The processor is idle during DMA." It runs; it only loses the bus cycles the controller takes.
    • DMA need not interrupt for each word. Normally it raises one completion interrupt per block when enabled; completion may instead be polled.
    • "Cycle stealing is slower for the processor than burst." It costs the same number of bus cycles, spread out.

GATE examination perspective

  • CYCLE STEALING. Words per second times bus cycle time gives the fraction of bus time taken.
  • BURST. Blocked fraction =ttx/(tprep+ttx)= t_{\text{tx}} / (t_{\text{prep}} + t_{\text{tx}}) when the device prepares and then bursts.
  • OVERLAP OR NOT. If preparation of the next unit overlaps the transfer, the blocked fraction is ttx/tprept_{\text{tx}} / t_{\text{prep}} instead; decide from the wording before computing.
  • DMA AGAINST INTERRUPTS. Per-unit interrupt cost against per-block set-up plus one interrupt.
  • SEQUENCE. Program the controller, BR, BG, transfer, update address and count, completion interrupt at count zero if enabled, otherwise poll.
  • UNITS. Keep KB as 1024 or 1000 exactly as the question states; mixing them shifts answers.
  • MODES. Burst: whole block at once; cycle stealing: one word per grant; transparent: only free cycles.

Summary

Sources

  1. C. Hamacher, Z. Vranesic, S. Zaky, Computer Organization, 5th ed., McGraw-Hill, 2002: Chapter 4, Section 4.4 (Direct Memory Access, including 4.4.1 Bus Arbitration).
  2. W. Stallings, Computer Organization and Architecture, 10th ed., Pearson: Section 7.5 (Direct Memory Access).

Next steps