Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Linux DMA programming method?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what is the Linux DMA programming method". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

DMA, namely Direct Memory Access, is a technology that allows peripherals to access memory data directly without the participation of CPU. When the peripherals finish reading and writing to the block of memory, DMAC notifies CPU through interruptions. This technology is mostly used for peripheral control that requires high data volume and data transmission speed, such as display devices.

DMA and Cache consistency

We know that in order to improve the running efficiency of the system, modern CPU adopts multi-level cache structure, including the use of multi-level Cache technology to cache data in memory to alleviate the difference between CPU and memory speed. Under this premise, it is obvious that if the data in DMA memory has been cached by Cache, and the peripherals modify the data in it, it will cause the problem of mismatch between Cache data and memory data, that is, the consistency between DMA and Cache. The easiest way to solve this problem is to disable the Cache function for DMA memory, which obviously leads to performance degradation

Virtual address VS physical address VS bus address

In a computer with MMU, CPU sees a virtual address, which is sent to MMU and converted into a physical address, and then the virtual address is converted into a bus address through the corresponding circuit, which is the address seen by the peripheral. So, the address seen by the DMA peripheral is actually the bus address. The Linux kernel provides the corresponding API to translate between the three addresses:

/ / Virtual-> physical virt_to_phys () / / physical-> Virtual ioremap () / / Virtual-> bus virt_to_bus () / / bus-> Virtual bus_to_virt ()

DMA address mask

DMA peripherals may not be able to perform DMA operations on all memory addresses, so the DMA address mask should be used.

Int dma_set_mask (struct device * dev,u64 mask)

For example, a DMA peripheral that can only access 24-bit addresses uses dma_set_mask (dev,0xffffff).

Programming flow

Here is the process of using DMA memory in a kernel program:

Consistent DMA

If you use the DMA buffer in the driver, you can use the API provided by the kernel that has taken consistency into account:

/ * request_dma-apply for DMA tunnel * On certain platforms, we have to allocate an interrupt as well... * / int request_dma (unsigned int chan, const char * device_id); / * * 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 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, gfp_t flag) / / apply for DMA buffer void * pci_alloc_consistent (struct pci_dev * hwdev, size_t size, dma_addr_t * dma_handle) of PCI devices / / release DMA buffer void dma_free_coherent (struct device * dev, size_t size, void * cpu_addr) Dma_addr_t dma_handle) / / release DMA buffer void pci_free_consistent () of PCI device / * free_dma-release DMA channel * On certain platforms, we have to free interrupt as well... * / void free_dma (unsigned int chan)

Streaming DMA

If you use the buffer of the application layer to establish the DMA request instead of the buffer in the driver, and you may only use functions such as kmalloc to apply, then you need to use the streaming DMA buffer, in addition, you need to solve the problem of Cache consistency.

/ * request_dma-apply for DMA tunnel * On certain platforms, we have to allocate an interrupt as well... * / int request_dma (unsigned int chan, const char * device_id); / / Map streaming DMAdma_addr_t dma_map_single (struct device * dev,void * buf, size_t size, enum dma_datadirection direction); / / the driver acquires the ownership of DMA, which is usually not supposed to be done by void dma_sync_single_for_cpu (struct device * dev,dma_addr_t dma_handle_t bus_addr,size_t size, enum dma_data_direction direction) / return DMA ownership to device void dma_sync_single_for_device (struct device * dev,dma_addr_t dma_handle_t bus_addr,size_t size, enum dma_data_direction direction); / / de-map streaming DMAdma_addr_t dma_unmap_single (struct device * dev,void * buf, size_t size, enum dma_datadirection direction); / * * free_dma-release DMA channel * On certain platforms, we have to free interrupt as well... * / void free_dma (unsigned int chan); this is the end of the content of "what is the programming method of Linux DMA". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report