DMA Details
DMA Details
[DMA]
What is Direct Memory Access?
Hardware mechanism that allows peripheral components to transfer their I/O data
directly to and from main memorywithout the need to involve the system processor.
DMA Programming
The programming of a device's DMA controller is hardwarespecific. Normally, the OS needs to have
the local
device address, the physical memory address on the PC,and the size of the memory
block to transfer.
Then the register that initiates the transfer is set.
The CPU initiates the transfer, then it executes other ops while the transfer is in progress,
finally it receives an interrupt from the DMA controller when the transfer is done
Hardware using DMA: disk drives, graphics cards,network cards, sound cards
Non-coherent systems:
OS ensures that the cache lines are flushed before an outgoing DMA transfer is
started and invalidated beforea memory range affected by an incoming DMA transfer
is accessed. The OS makes sure that the memory
range is not accessed by any running threads in themeantime.
• Must occupy contiguous pages in physical memory due to PCI bus requirement
using physical addresses
• Note: other buses (e.g. Sbus) use virtual memoryaddresses for the transfer
• The right kind of memory must be allocated since not allmemory zones are suitable:
high memory may not work with some devices because of limited address space
• Memory can be allocated at boot or runtime but driverscan only allocate at runtime
• Device may have DMA limitations – 32 bit addressingis assumed but may be
changed with
int dma_set_mask(struct device *dev, u64 mask);
(e.g. for 24 bit addressing: mask = 0xFFFFFF)
• Cache coherency:
copies of recently accessed memory areas are in cacheif device writes to memory,
cache area is invalidated so it will have to be paged in
If device reads data from memory, cache flushed out first
• Generic DMA layer ensures all of above are not a problemover many architectures
provided some rules are obeyed
• The buffer must not be unmapped while DMA is stillactive, or serious system
instability is guaranteed.
• Create array sgof scatterlist entries and fill them inLet nentbe the number of
entries
• Call
int dma_map_sg(device, sg, nent, direction)
returns the number of DMA buffers to send (<= nent)
• dad.c
• usb_skel.c