本文主要通过WinDriver工具图形化的查看PCIe设备,来认识PCIe设备的空间结构。本文主要参考《PCI Express体系结构导读》和网友博客:点击打开链接。
一、PCIe设备空间结构
PCIe设备有三个独立的物理地址空间:设备存储器空间(memory)、IO空间和配置空间(config)。由于PCIe设备支持即插即用,存储器空间和IO空间是在设备插入host后由操作系统决定其映射的基地址。系统加电时,BIOS检测PCI总线,确定所有连接在PCI总线上的设备以及它们配置要求,并进行系统配置。所以,所有PCI设备自身必须实现配置空间,从而能实现参数自动配置,实现真正的即插即用。参考博客:获取PCI设备并初始化。另外,可参考微软PLX9x5x的代码,在驱动中读取PCI设备的配置空间:
// // Parse the resource list and save the resource information. // for (i=0; i < WdfCmResourceListGetCount(ResourcesTranslated); i++) { desc = WdfCmResourceListGetDescriptor( ResourcesTranslated, i ); if(!desc) { TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP, "WdfResourceCmGetDescriptor failed"); return STATUS_DEVICE_CONFIGURATION_ERROR; } switch (desc->Type) { case CmResourceTypeMemory: bar = NULL; if (foundSRAM && !foundSRAM2 && desc->u.Memory.Length == 0x400000) { SRAM2BasePA = desc->u.Memory.Start; SRAM2Length = desc->u.Memory.Length; foundSRAM2 = TRUE; bar = "BAR2"; KdPrint(("BAR2 found.\n")); } if (foundRegs && !foundSRAM && desc->u.Memory.Length == 0x400000) { SRAMBasePA = desc->u.Memory.Start; SRAMLength = desc->u.Memory.Length; foundSRAM = TRUE; bar = "BAR1"; KdPrint(("BAR1 found.\n" )); } if (!foundRegs && desc->u.Memory.Length == 0x1000) { regsBasePA = desc->u.Memory.Start; regsLength = desc->u.Memory.Length; foundRegs = TRUE; bar = "BAR0"; KdPrint(("BAR0 found.\n")); } TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP, " - Memory Resource [%I64X-%I64X] %s", desc->u.Memory.Start.QuadPart, desc->u.Memory.Start.QuadPart + desc->u.Memory.Length, (bar) ? bar : "<unrecognized>" ); KdPrint(("desc->u.Memory.Start is %x, desc->u.Memory.length is %x\n", desc->u.Memory.Start, desc->u.Memory.Length)); break; case CmResourceTypePort: bar = NULL; if (!foundPort && desc->u.Port.Length >= 0x100) { foundPort = TRUE; bar = "Port"; } TraceEvents(TRACE_LEVEL_INFORMATION, DBG_PNP, " - Port Resource [%08I64X-%08I64X] %s", desc->u.Port.Start.QuadPart, desc->u.Port.Start.QuadPart + desc->u.Port.Length, (bar) ? bar : "<unrecognized>" ); KdPrint(("CmResourceTypePort is found.\n" )); break; default: // // Ignore all other descriptors // break; } } if (!(foundRegs && foundSRAM)) { TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP, "PLxMapResources: Missing resources"); return STATUS_DEVICE_CONFIGURATION_ERROR; }
二、WinDriver截图
1,存储器空间
该设备共映射了6块存储器空间,其中第一块4kB,后面5块大小都是32MB。也可重windows的设备管理器中看到相同的结果。
2,配置空间