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也是有開銷的,數據量少的話還不如直接讀。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章