Some people recently asked me about PCI device drivers, and after communicating with them, I suggest they take a look at <<the Windows NT device Driver book>> This book, personally feel that 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, you must also understand the basics of DMA, Scattergater, Mapregister, Common buffer, and so on.
1.1 PCI Device Resource acquisition
The resources of a PCI device are dynamically allocated by the system based on the properties of the device (the value of the registers 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 categories 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 interviewed using Write_port_ulong and Read_port_ulong plus the corresponding offset.
Interrupt resources explained in the content, then mainly as the Ioconnectinterrupt system function of the parameters, 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, the system is divided into master and slave, another very important ability is whether to support Scatter/gather.
These capabilities are finally shown now in the members of the data structures defined by device_description, such as: Dmawidth, Scattergather, Master, Dma32bitaddresses, dma64bitaddresses.
The system has finally abstracted various types of device DMA as 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 descriptive narrative structure as a reference, finally get the Dma_adapter object, as a series of DMA related operations of the 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 ape 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.
To put it simply, Common buffer is applied on behalf of Dma_adapter, and after successful application, it can access the continuous physical memory through the virtual address and 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 with the implementation of the system time elapsed, more and more scarce.
The Allocatecommonbuffer system call is present as the Dmaoperations form of the Dma_adapter, so a detailed piece of common buffer can be said to be associated with a detailed 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, corresponding to the virtual address of the operation via common buffer).
The Scatter/gather list is finally read by the 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, assuming the ability to s/g, the DMA operation can be done in batches, otherwise, the DMA operation must be completed using Maptransfer once.
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, the ISR was registered through the Ioconnectinterrupt.
The ISR is actually called when the device is interrupted, but the details are referred to (KEINSERTQUEUEDPC) DPC for processing.
DPC, by KEINITIALIZEDPC system calls, associates the DPC object KDPC with the detailed Kdeferred_routine DPC handler function.
1.7 Access to PCI device configuration space
In fact, under normal circumstances, Windows PCI devices do not need to access 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 interview method of the Io/mem is mentioned earlier, and the access to the configuration space is 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 are fully capable of being used on PCIe devices, which, after all, do not make much difference on the upper level.
Unlike USB drivers, PCI devices need to consider all aspects of the drive design, I hope this article will be useful for reference.
27th: PCI In Windows driver, DMA, ISR, DPC, Scattergater, Mapregsiter, Commonbuffer, Configspace