DMA的经典解读

Today all computers are architectured the same way: a central processor and a number of peripherals. In order to exchange data, these peripherals are interconnected by a bus over which all communications go. Figure outlines the layout of peripherals in a standard computer.

现在的计算机的架构大概都是下图这样的。

图片发自App

The first user of the bus is the CPU. The CPU uses the bus to access system memory and other peripherals. However, the CPU is not the only one able to write andread data to the peripherals, the peripherals themselves also have the capability to exchange information directly. In particular, a peripheral which has the ability to read and write to memory without the CPU intervention is said to be DMA (Direct Memory Access) capable, and the memory transaction is usually called a DMA. This type of transaction is interesting, because it allows the driver to use the GPU instead of the CPU to do memory transfers. Since the CPU doesn’t need to actively work any more to achieve those transfers, and since it allows better asynchronicity between the CPU and the GPU, better performance can be attained. Common uses of DMA include improving the performance of texture uploads or streaming video. Today, all graphics processors feature this ability (named DMA bus mastering) which consists in the card requesting and subsequently taking control of the bus for a number of microseconds.

没有DMA时,只能是CPU与外设读写数据,然后从内存存取数据,有了DMA之后,CPU设置一下DMA,外设就可以直接与内存进行数据交互,比如GPU的纹理数据传输、视频流传输,一般一次DMA会占用内存总线几个毫秒。

If a peripheral has the ability to achieve DMA to or from an uncontiguous list of memory pages (which is very convenient when the data is not contiguous in memory), it is said to have DMA scatter-gather capability (as it can scatter data to different memory pages, or gather data from different pages).

DMA能从不连续内存读/写数据的能力就是scatter-gather模式(老哥儿想当初开发EFM32的时候死活搞不明白这个模式是干啥的,一个破48M的芯片的DMA支持这功能算不算脑抽)。

Notice that the DMA capability can be a downside in some cases. For example on real time systems, this means the CPU is unable to access the bus while a DMA transaction is in progress, and since DMA transactions happen asynchronously this can lead to missing a real time scheduling deadline. Another example is small DMA memory transfers, where the CPU overhead of setting up the DMA is greater than the gain in asynchronicity and therefore transfers slow down. So while DMA has a lot of advantages from a performance viewpoint, there are situations where it should be avoided.

注意有些内存总线的设计上,DMA传数据会占用一段的内存总线时间,独占,CPU不能访问的那种,实时任务可能超时,另外CPU设置DMA也是有开销的,数据量少的话还不如直接读。

你可能感兴趣的:(DMA的经典解读)