Linux 預留記憶體 DMA 使用心得

2021-09-28 18:42:23 字數 4176 閱讀 1331

linux reserved memory 預留記憶體

本文採用的是 上文的 「通過dma api預留記憶體」的方式

通過命令

#cat /proc/iomem

7ff00000-7ff00fff : /dma@7ff00000

7ff00000-7ff00fff : /dma@7ff00000

7ff50000-7ff50fff : /hdlcd@7ff50000

7ff60000-7ff60fff : /hdlcd@7ff60000

7ff80000-7ff80fff : /uart@7ff80000

7ff80000-7ff80fff : /uart@7ff80000

7ff90000-7ff90fff : /i2s@7ff90000

7ffa0000-7ffa0fff : /i2c@7ffa0000

7ffc0000-7ffcffff : /ehci@7ffc0000

7ffd0000-7ffd0fff : /memory-controller@7ffd0000

80000000-dfffffff : system ram

....

..

可以看到系統記憶體其中的一塊為

80000000-dfffffff : system ram
reserved-memory ;}

; sample@0x64000000

;

系統啟動後再次使用命令檢視記憶體:

#cat /proc/iomem

7ff00000-7ff00fff : /dma@7ff00000

7ff00000-7ff00fff : /dma@7ff00000

7ff50000-7ff50fff : /hdlcd@7ff50000

7ff60000-7ff60fff : /hdlcd@7ff60000

7ff80000-7ff80fff : /uart@7ff80000

7ff80000-7ff80fff : /uart@7ff80000

7ff90000-7ff90fff : /i2s@7ff90000

7ffa0000-7ffa0fff : /i2c@7ffa0000

7ffc0000-7ffcffff : /ehci@7ffc0000

7ffd0000-7ffd0fff : /memory-controller@7ffd0000

80000000-bfffffff : system ram

如上文所述,通過以下**來使用這一塊的記憶體:

define alloc_size (

0x20000000

)/* initialize reserved memory resources */

int rc=0;

dma_addr_t dma_handle;

void

* vaddr;

rc =

of_reserved_mem_device_init

(dev);if

(rc)

/* allocate memory */

dma_set_coherent_mask

(dev,

0xffffffff);

vaddr =

dma_alloc_coherent

(dev, alloc_size,

&dma_handle, gfp_kernel)

;dev_info

(dev,

"allocated coherent memory, vaddr: 0x%0llx, paddr: 0x%0llx\n"

,(u64)lp->vaddr, lp->paddr)

;

其中 vaddr 是核心**可以直接使用的虛擬位址。

dma_handle 是這一塊記憶體區域的實體地址。

dma_alloc_coherent 介紹

/**

* 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)

;

sample_mmap

(struct file *file,

struct vm_area_struct *vma)

dma_mmap_coherent介紹:

/**

* dma_mmap_coherent - map a coherent dma allocation into user space

* @dev: valid struct device pointer, or null for isa and eisa-like devices

* @cpu_addr: kernel cpu-view address returned from dma_alloc_coherent

* @handle: device-view address returned from dma_alloc_coherent

* @size: size of memory originally requested in dma_alloc_coherent

* * map a coherent dma buffer previously allocated by dma_alloc_coherent

* into user space. the coherent dma buffer must not be freed by the

*/int

dma_mmap_coherent

(struct device *dev,

struct vm_area_struct *vma,

void

*cpu_addr, dma_addr_t handle, size_t size)

;

dma_free_coherent

(dev, alloc_size , vaddr , dma_handle)

;

dma_free_coherent介紹:

void

dma_free_coherent

(struct device *dev, size_t size,

void

*cpu_addr,

dma_addr_t dma_handle)

free a region of consistent memory you previously allocated. dev,

size and dma_handle must all be the same as those passed into

dma_alloc_coherent()

. cpu_addr must be the virtual address returned by

the dma_alloc_coherent()

.note that unlike their sibling allocation calls, these routines

may only be called with irqs enabled.

linux記憶體管理 之 DMA

1 dma 直接記憶體訪問 用於在主存貯器和裝置記憶體之間的大量資料的交換,無須cpu參與,可以提高系統效率。2 核心中的dma層 3 dma的位址 dma addr t 4 dma set mask 查詢dma定址範圍 5 dma對映 a 一致性dma對映 static inline void d...

linux核心DMA記憶體分配

linux系統啟動過程中,會初始化整個核心位址空間,將其劃分為各個型別段,其中包括物理記憶體對映區 又稱之為邏輯位址對映區 虛擬記憶體分配區 高階頁面對映區 系統保留對映區 dma對映區 這個類似物理記憶體對映區,其對映的實體地址是連續的 這幾個區域等等,linux核心在啟動的時候通過arch ar...

linux通過核心啟動引數預留系統記憶體

1 首先正常啟動核心 我們需要啟動核心後查詢一些資訊。2.檢視系統記憶體資訊 核心啟動後通過 cat proc iomem檢視記憶體的分布,結果類似 100000000 10dffffff system ram但是我們會看到有很多條這樣的項,我們主要關注 system ram 這樣的項,因為這代表系...