Some people recently asked me about PCI device drivers, and after communicating with them, I recommend that they take a look at <<the Windows NT device Driver book>> This book, personal feeling, this book is very consistent and smooth.
PCI Device driver basically includes the resource acquisition of PCI, the reading and writing of the configuration space, the processing of interrupts, and the processing of the second half of the interrupt in DPC.
At the same time, must understand the DMA, Scattergater, Mapregister, Common buffer and other basic.
1.1 PCI Device Resource acquisition
The resource of a PCI device is that the system is dynamically allocated based on the properties of the device (the value of the register in the configuration space).
Only the resources allocated by these systems in PnP start are available in the driver:
For example: I developed the PCI TV card driver, the use of the two types of resources, Cmresourcetypeport and Cmresourcetypeinterrupt.
The PORT address is used as the first address of the device register, after which the device registers can be accessed using Write_port_ulong and Read_port_ulong plus the corresponding offset.
Interrupt resources explained in the content, then mainly as the parameters of the Ioconnectinterrupt system function, the device hardware interrupt associated with the ISR, the Kinterrupt instance is the device interrupt software form of the carrier.
1.2 DMA
DMA device, in the system is divided into master and slave, another very important ability is whether to support Scatter/gather.
These capabilities are ultimately reflected in the members of the data structures defined by device_description, such as Dmawidth, Scattergather, Master, Dma32bitaddresses, dma64bitaddresses.
The system eventually abstracts various types of device DMA into an instance of Dma_adapter, which is the carrier of the device DMA software form.
The driver code uses the Iogetdmaadapter system call, the physical device object PDO and the DMA description structure as the parameter, finally obtains this Dma_adapter object, as the subsequent series of DMA related operation entity object.
1.3 Map Register
User space, the virtual memory of the kernel space is associated with the physical memory, which is mapped by the page table, and the driver programmer often uses the MDL, which is also a mapping between virtual memory and physical memory in a particular region.
The DMA device needs to look at the system memory from the point of view of the bus address (also called the logical address in MSDN) and the physical mapping of the memory.
The mapping relationship is assumed by the map register.
However, the map registers are based on the system, some are hardware implementations, and some are specific pieces of memory that are partitioned in the software.
The Iogetdmaadapter call is also the process of applying for a map register to the system.
1.4 Common Buffer
And that's the question that people ask most.
Simply put, Common buffer is applied on behalf of Dma_adapter, and after successful application, it can access the continuous physical memory either through the virtual address or through the address of the logical address space that the DMA controller belongs to.
Its advantage is the physical continuous, the problem is that the system of continuous physical memory is with the system running time, more and more scarce.
The Allocatecommonbuffer system call is present as the Dmaoperations form of the dma_adapter, so the specific piece of common buffer can be said to be associated with a specific DMA controller.
After the Allocatecommonbuffer is successfully invoked, the logical address of the virtual address and the logical space to which the DMA controller belongs is returned.
The PCI TV card developed by the author is to allocate a small contiguous physical memory by Allocatecommonbuffer, which is used to store the Scatter/gather list (the logical address of a block of memory scatter_gather_list. Elements[i]. The Address.lowpart is scatter_gather_list with the length of the memory. Elements[i]. Length, the corresponding operation via common buffer virtual address).
The Scatter/gather list is eventually read by a DMA controller with S/G capability (corresponding to the logical address of the common buffer), and the DMA read/write operation is performed based on the table entry.
1.5 s/g
The ability of the s/g is the characteristic of the DMA controller and, if it has the ability to s/g, it can be batched in a DMA operation, otherwise, the DMA operation must be done using maptransfer once in a while.
The connection between virtual memory and physical memory in system space is expressed by IOALLOCATEMDL and Mmbuildmdlfornonpagedpool to establish a specific MDL.
Thereafter, the S/g list of virtual address memory described by MDL is obtained through getscattergatherlist in Dma_adapter dmaoperations, and finally, in Getscattergatherlist's
In the Executionroutine function, the list is populated with table (starting logical address and length) of common buffer for use by the DMA controller.
1.6 ISR and DPC
As mentioned earlier, ISR is registered through Ioconnectinterrupt.
The ISR is actually called when the device is interrupted, but the specific matter is referred to the (KEINSERTQUEUEDPC) DPC.
DPC, by KEINITIALIZEDPC system calls, associates the DPC object KDPC with the specific Kdeferred_routine DPC handler function.
1.7 Access to the PCI device configuration space
In fact, in general, Windows PCI devices do not require access to the PCI device configuration space.
But as a complete PCI device driver, here is a mention.
Since the configuration space of the PCI device is separate from the Io/mem space, the access mode of the Io/mem is mentioned earlier, and the configuration space is accessed as follows:
Define variables: Bus_interface_standard M_businterfacestandard;
Established: IRP, Main and secondary respectively, IRP_MJ_PNP, Irp_mn_query_interface, get Bus_interface_standard data structure.
After that, the registers of the PCI configuration space are read and written by SetBusData and GetBusData in the Bus_interface_standard.
PCI device drivers can be used entirely on PCIe devices, which, after all, do not have much difference on the top.
Unlike USB drivers, PCI devices need to consider all aspects of the drive design, I hope this article is useful for reference.