Detailed Explanation of linux DMA Interface Knowledge Points

  • 2021-07-22 12:14:34
  • OfStack

1. Two DMA mapping types

1.1. 1-DMA mapping (Consistent DMA mappings)

It is mainly used to map areas that have been used for a long time.

CPU and DMA controller do not need to consider the effect of cache.

consistent here is actually the concept of coherent, which cannot guarantee consistent, that is to say, memory barrier is needed to guarantee memory order.

1.2 Streaming DMA Mapping (streaming DMA mapping)

Mainly used for primary DMA transmission, which will be released after transmission is completed.

2. Specify the addressing range of DMA devices

include/linux/dma-mapping.h


//  Used for 1 Mapping scope of sexual memory mapping 
static inline int dma_set_coherent_mask(struct device *dev, u64 mask)
//  Mapping scope for streaming memory mapping 
static inline int dma_set_mask(struct device *dev, u64 mask);

3. DMA mapping interface

3.11 Attitudinal DMA Interface

Allocate larger DMA buffer


// dev        DMA Controller equipment 
// size         To be assigned DMA buffer Size 
// dma_handle   Return DMA buf Physical address of 
// flag         Allocation flag 
//  Return value        DMA buffer Virtual address of 
void *dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flag) 

// dev        DMA Controller equipment 
// size         Released DMA buffer Size 
// cpu_addr    DMA buf Virtual address of 
// dma_handle  DMA buf Physical address of 
void dma_free_coherent(struct device *dev, size_t size,
    void *cpu_addr, dma_addr_t dma_handle)

Allocate smaller DMA buffer, requested from dma poll.


/**
 * dma_pool_alloc -  From dma poll Obtain 1 Block 1 Sexual memory 
 * @pool:  That generates a memory block dma pool
 * @mem_flags: GFP_* bitmask
 * @handle:  Returns the of the memory block dma Address 
 */
void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
       dma_addr_t *handle)

/**
 * dma_pool_free -  Release memory back to dma pool
 * @pool: That generates a memory block dma pool
 * @vaddr:  Virtual address of memory block 
 * @dma:  Physical address of memory block 
 */
void dma_pool_free(struct dma_pool *pool, void *vaddr, dma_addr_t dma)

3.2 Streaming DMA Interface


// dev     Devices that need to map memory 
// ptr      Mapped buffer Virtual address 
// size     The size of the map 
// dir      Transmission direction 
// attr     Attribute 
//  Return value    dma Physical address 
dma_addr_t dma_map_single_attrs(struct device *dev, void *ptr,
             size_t size,
             enum dma_data_direction dir,
             unsigned long attrs)
// dev     Devices that need to map memory 
// addr   dma Physical address of the zone 
// size     The size of the map 
// dir      Transmission direction 
// attr     Attribute 
void dma_unmap_single_attrs(struct device *dev, dma_addr_t addr,
           size_t size,
           enum dma_data_direction dir,
           unsigned long attrs)

page mapping


dma_addr_t dma_map_page(struct device *dev, struct page *page,
           size_t offset, size_t size,
           enum dma_data_direction dir)

void dma_unmap_page(struct device *dev, dma_addr_t addr,
         size_t size, enum dma_data_direction dir)

Returns an dma mapping error


//  Return dma Mapping error 
int dma_mapping_error(struct device *dev, dma_addr_t dma_addr)

Mapping scatterlist


int dma_map_sg_attrs(struct device *dev, struct scatterlist *sg,
          int nents, enum dma_data_direction dir,
          unsigned long attrs)

void dma_unmap_sg_attrs(struct device *dev, struct scatterlist *sg,
           int nents, enum dma_data_direction dir,
           unsigned long attrs)

//  Return map Post dma Address and length 
sg_dma_address(struct scatterlist *sg)
sg_dma_len(struct scatterlist *sg)

sync Operation


void dma_sync_single_for_cpu(struct device *dev, dma_addr_t addr,
size_t size,
enum dma_data_direction dir)

void dma_sync_single_for_device(struct device *dev,
dma_addr_t addr, size_t size,
enum dma_data_direction dir)

void
dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
int nelems, enum dma_data_direction dir)

void
dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
int nelems, enum dma_data_direction dir)

The above is all the relevant knowledge points introduced this time. If you have any supplement, you can contact this site.


Related articles: