Home  >  Article  >  Computer Tutorials  >  Is there any source code for DMA Windows driver?

Is there any source code for DMA Windows driver?

PHPz
PHPzforward
2024-01-18 08:30:16593browse

dma windows驱动源码吗?

The related operations of DMA are introduced on page 545 of "In-depth Understanding of the Linux Kernel". When talking about DMA, we have to mention the issue of Cache. The book cites the following example to describe the Cache consistency problem:

Assume that the device driver fills some data into the memory buffer, and then immediately instructs the hardware device to read the data using DMA transfer. If DMA accesses these physical RAM memory cells, and the contents of the corresponding hardware cache line have not yet been written to RAM, then the hardware device will only read the old value in the memory buffer.

There are now two ways to deal with DMA buffers:

Consistent DMA mapping:

The book is more abstract, and the popular term is any DMA buffer Any changes to the area will be directly updated into the memory, which is also called synchronized or consistent.

Streaming DMA mapping:

According to personal understanding, the stream here is the input and output stream. We need to specify the direction of the DMA buffer in advance, such as reading the buffer or writing. buffer zone. It is also called asynchronous or non-consistent. Please see below for details.

Because in the x86 architecture, the hardware device driver itself will snoop the accessed hardware cache, so there is no DMA consistency problem in the x86 architecture. For other architectures such as MIPS, SPARC and POWERPC (including ARM), it is necessary to ensure their DMA consistency in software.

Regarding how to choose between the above two, there is a suitable suggestion in the book. If the CPU and DMA processor access a buffer in an unpredictable way, then the consistent DMA mapping method must be forced to be used (here I Unpredictable (understood as not being able to determine when they will access the buffer). In other cases, a streaming DMA mapping approach is preferable because dealing with consistent DMA mapping is cumbersome on some architectures and can lead to more Low system performance.

Here is a detailed introduction to streaming DMA:

The buffer that needs to be accessed needs to be mapped before data transmission (the mapping here means that some functions need to be called to inform the kernel that the buffer is streamed mapped), which is unmapped after transfer.

Starting a streaming DMA data transfer is divided into the following steps:

1. Allocate a DMA buffer.

When the DMA device does not use S/G (scatter/gather) mode, it must be ensured that the buffer is physically continuous. The Linux kernel has two functions for allocating continuous memory: kmalloc() and __get_free_pages(). Both functions have the maximum value for allocating continuous memory. kmalloc allocates bytes as a unit, the maximum is about 64KB, __get_free_pages() allocates pages as a unit, and can allocate a maximum of 2^order number of pages. The maximum value of the order parameter Determined by the MAX_ORDER macro in the include/linux/Mmzone.h file (in the default 2.6.18 kernel version, this macro is defined as 10. That is to say, in theory, the __get_free_pages function can apply for up to 1

## at a time #2. Establish streaming mapping.

After reading and writing access to the DMA buffer, and before starting the DMA device transfer, enable the dma_map_single() function to establish a streaming DMA mapping. These two functions accept buffers The linear address of the area is used as its parameter and the corresponding bus address is returned.

3. Release the streaming mapping.

When the DMA transfer is completed, we need to release the mapping, then call dma_unmap_single() Function.

Note:

(1). To avoid cache coherency issues, the driver should call dma_sync_single_for_device(() if necessary before starting a DMA data transfer from RAM to the device ) function refreshes the cache line corresponding to the DMA buffer.

(2). The device driver cannot access the memory buffer before a DMA data transfer from the device to RAM is completed, but if necessary If so, the driver should call the dma_sync_single_for_cpu() function to invalidate the corresponding hardware cache line before reading the buffer.

(3). Although the bottom layer of kmalloc is also implemented using __get_free_pages, the corresponding release buffer of kmalloc The area function is kfree, and the release buffer function corresponding to __get_free_pages is free_pages. Several application and release functions specifically related to __get_free_pages are as follows:

Application function:

alloc_pages(gfp_mask ,order) returns the address of the first allocated page frame descriptor, or NULL if the allocation failed. __get_free_pages(gfp_mask,order) is similar to alloc_pages(), but it returns the linear address of the first allocated page. If If you need to obtain the page frame number corresponding to the linear address, you need to call the virt_to_page(addr) macro to generate the linear address. Release function: __free_pages(page, order) The main emphasis here is that page is the page frame number free_pages where the linear first address of the buffer is to be released. (page, order) This function is similar to __free_pages(page, order), but the parameter it receives is the linear address addr

of the first page frame to be released.

The above is the detailed content of Is there any source code for DMA Windows driver?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:xtzjcz.com. If there is any infringement, please contact admin@php.cn delete