1. 程式人生 > >dma_alloc_coherent (建立一致性 DMA 對映函式)

dma_alloc_coherent (建立一致性 DMA 對映函式)

1、函式申明

/**
 * dma_alloc_coherent - allocate consistent memory for DMA
 * @dev: valid struct device pointer, or NULL for ISA and EISA-like devices
 * @size: required memory size
 * @handle: bus-specific DMA address
 *
 * Allocate some uncached, unbuffered memory for a device for
 * performing DMA.  This function allocates pages, and
will * return the CPU-viewed address, and sets @handle to be the * device-viewed address. */ void *dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, int flag);

該函式實際獲得兩個地址,
1、函式的返回值是一個 void *,代表緩衝區的核心虛擬地址
2、相關的匯流排地址(實體地址),儲存在dma_handle中

2、呼叫

A =dma_alloc_coherent(B,C
,D,GFP_KERNEL); 含義: A: 記憶體的虛擬起始地址,在核心要用此地址來操作所分配的記憶體 B: struct device指標,可以平臺初始化裡指定,主要是dma_mask之類,可參考framebuffer C: 實際分配大小,傳入dma_map_size即可 D: 返回的記憶體實體地址,dma就可以用。 所以,A和D是一一對應的,只不過,A是虛擬地址,而D是實體地址。對 任意一個操作都將改變緩衝區內容。當然要注意操作環境。

注size最好以頁為單位分配。