What is DMA (direct memory access)?

A mechanism that transfers data directly between peripherals and memory without going through the CPU, so the CPU can do other work during large transfers.

Field: CPUs and architecture

Japanese name: DMA(ダイレクトメモリアクセス)

How a DMA transfer works

In programmed I/O the CPU moves every word itself with load and store instructions. With direct memory access (DMA), the CPU instead programs a DMA controller, or the device's own DMA engine, with a source, a destination, a length and a trigger, then goes back to other work. The controller takes control of the bus, moves the data and raises a completion interrupt. In a microcontroller, for example, a timer can trigger an ADC conversion at fixed intervals and DMA can store each result into an array, waking the CPU only when the buffer is full.

Descriptor rings in high-speed devices

Network cards and NVMe SSDs use descriptor rings: circular tables in main memory whose entries give a buffer address and length. The CPU fills free descriptors and writes once to a device register, often called a doorbell, to say how far it has written. The device reads the descriptors by DMA, performs the transfers itself and marks finished entries complete. CPU and device thus advance many transfers without waiting for each other, and lists of scattered buffers can be handled in one operation, known as scatter-gather.

Caches and pinned memory

On some systems DMA bypasses the CPU caches, so the two views of memory can disagree. Where hardware does not keep them coherent, the driver writes dirty cache lines back to memory before a device reads a buffer, and invalidates the cached range before the CPU reads data a device has written, so stale values are not used. The operating system may also move or swap out ordinary pages at any time, so buffers for DMA are pinned in place; pinned host memory lets a GPU transfer data without an intermediate copy and overlap transfers with computation.

Protecting memory with an IOMMU

Because DMA reads and writes memory directly, a faulty or malicious device could overwrite kernel data or read encryption keys, a particular concern for devices attached through fast external ports. An IOMMU (input-output memory management unit) does for devices what the MMU does for the CPU: it translates the addresses devices use into physical addresses and blocks access outside the regions the operating system has granted. It also lets scattered physical pages appear contiguous to a device and makes it safe to assign a device directly to a virtual machine.

Try it: questions about DMA (direct memory access)

Which statement is correct about DMA using a ring of descriptors in memory, as widely used by network cards and NVMe SSDs?

  1. AThe CPU transfers data by writing it byte by byte into device registers
  2. BThe CPU only writes addresses and lengths into descriptors and notifies the device, which reads them and carries out the transfers itself
  3. CDescriptors exist only inside the device and are invisible to the CPU
Show answer and explanation

Answer: B. The CPU only writes addresses and lengths into descriptors and notifies the device, which reads them and carries out the transfers itself

A descriptor ring is a table in main memory, used in a circular way, where each entry says which address to transfer and how many bytes. The CPU fills free descriptors and tells the device once, through a register, how far it has written. The device reads the descriptors by DMA, transfers the data and either marks finished descriptors as complete or writes completions to a separate completion ring (such as an NVMe completion queue). CPU and device can advance many transfers without waiting for each other.

When a microcontroller captures many A/D conversion results at regular intervals, what is the advantage of using DMA?

  1. AThe A/D converter's resolution increases automatically
  2. BThe input voltage is amplified automatically
  3. CThe CPU need not read each result in an interrupt; DMA stores them in memory, reducing CPU load
Show answer and explanation

Answer: C. The CPU need not read each result in an interrupt; DMA stores them in memory, reducing CPU load

If the CPU reads each conversion in an interrupt, at tens of thousands of samples per second the overhead of entering and leaving interrupts alone can consume a large share of its time. With DMA, each completed conversion is written in turn into an array in memory, and the CPU is notified only when the array is full. Combined with a timer that triggers conversions, precisely spaced sampling continues without the CPU.

FAQ

What does DMA stand for?

DMA stands for direct memory access, a mechanism that lets peripherals transfer data to and from main memory without the CPU copying each word, so the CPU can do other work during the transfer.

What is the difference between DMA and programmed I/O?

In programmed I/O the CPU reads or writes every data word to the device itself. With DMA the CPU only sets up the transfer and is notified when it finishes, which saves CPU time for large or frequent transfers.

Why must the cache be invalidated after a DMA transfer into memory?

On systems without hardware cache coherence, the CPU's cache may still hold old copies of that memory range. Invalidating it forces the CPU to read the new data the device wrote.

Questions that mention DMA (direct memory access) (5)

DMA (direct memory access) appears in 7 practice questions across 6 topics, including CPUs and architecture, I/O Interfaces & Buses, Embedded Systems & Firmware.

  1. What countermeasure protects against external devices connected to a PC using DMA to read or write main memory without permission?
  2. Which mechanism transfers data directly between peripherals and memory without going through the CPU?
  3. Which correctly describes the write-back policy for caches?
  4. Why does pinning (page-locking) CPU memory before transferring it to the GPU speed up the transfer?
  5. Regarding hardware features that support control-flow integrity, which correctly pairs what protects returns from functions with what protects indirect calls and jumps such as those through function pointers?

Books on CPUs and architecture Ad

As an Amazon Associate I earn from qualifying purchases.

Comparisons that include this term

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary