Is there any source code for DMA Windows driver?
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 addrof 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Are you hooked on R.E.P.O.? Curious where your game progress is stored? This guide reveals the location of your R.E.P.O. save files and shows you how to protect them. Losing your game progress is frustrating, so let's ensure your hard work is safe

Monster Hunter Wilds Shader Compilation Fix: A Comprehensive Guide Many players are encountering an issue where Monster Hunter Wilds gets stuck endlessly compiling shaders. This guide provides effective solutions to resolve this frustrating problem a

Encountering the Monster Hunter Wilds communication error S9052-TAD-W79J? This frustrating issue affects many players, but several solutions have proven effective. This guide compiles these fixes to help you get back to hunting. Monster Hunter Wilds

To secure your Monster Hunter Wilds game progress, you'll need to know where the save files are located and how to back them up. This guide provides step-by-step instructions for both tasks. Monster Hunter Wilds, available on multiple platforms, requ

Understanding Microsoft Teams error tag 9hehw on Windows Microsoft Teams is a crucial communication and collaboration application by which users are able to joi

Windows 10 KB5053606 Update installation failed? This guide provides effective solutions! Many users are having problems trying to install the Windows 10 KB5053606 update released on Tuesday of Microsoft's March 2025 patch, such as SSH connection interruption, Dwm.exe unresponsiveness, or input method editor (IME) issues. Updates may be stuck during downloading or installation, or error codes may appear, such as 0x800f0922, 0xd000034, 0x80070437 and 0x80072efe. KB5053606 installation failure is usually caused by a variety of reasons, including Windows update component failure, system file corruption, and disk

Monster Hunter Wilds players are encountering frustrating crashes due to Fatal D3D or RE Engine Aborted errors. This guide provides solutions to resolve these issues and get you back to hunting. Understanding the Problem Monster Hunter Wilds uses Cap

Troubleshooting R.E.P.O. Game Crashes: A Comprehensive Guide Experiencing frustrating game crashes, especially in a team-based game like R.E.P.O., can be incredibly disruptive. This guide offers several solutions to resolve R.E.P.O. crashes, freezes
