PCI设备内存操作函数总结 分类: 浅谈PCI 2014-05-26 17:48 580人阅读 评论(0) 收藏
2014-05-26 17:48
585 查看
1. ExAllocatePool()
函数说明:
ExAllocatePool allocates pool memory of the specified type and returns a pointer to the allocated
block.
函数定义:
PVOID ExAllocatePool(
__in POOL_TYPE PoolType,
__in SIZE_T NumberOfBytes
);
代码中用来分配设备描述DMA adepter设备的特性(DEVICE_DESCRIPTION),作为参数传递给IoGetDmaAdapter()函数。在调用IoGetDmaAdapter()函数之前就要对PDEVICE_DESCRIPTION结构进行指定各种特性。
实际中这样使用:
PDEVICE_DESCRIPTION DeviceDescription = (PDEVICE_DESCRIPTION) ExAllocatePool
(PagedPool, sizeof(DEVICE_DESCRIPTION));
2. RtlZeroMemory()
函数说明:
The RtlZeroMemory routine fills a block of memory with zeros, given a pointer to the block and the length, in bytes, to be filled.
函数定义:
VOID RtlZeroMemory(
__in VOID UNALIGNED *Destination,
__in SIZE_T Length
);
3. IoGetDmaAdapter()
函数说明:
The IoGetDmaAdapter routine returns a pointer to the DMA adapter structure for a physical device object.
函数定义:
struct _DMA_ADAPTER* IoGetDmaAdapter(
__in_opt PDEVICE_OBJECT PhysicalDeviceObject,
__in struct _DEVICE_DESCRIPTION *DeviceDescription,
__out PULONG NumberOfMapRegisters
);
实际代码中这样使用:
ULONG NumberOfMapRegisters=100;
pdx->DmaAdapter=IoGetDmaAdapter(pdx->NextStackDevice,DeviceDescription,&NumberOfMapRegisters);// NextStackDevice在AddDevice函数中已经attach了。
4. AllocateCommonBuffer()
函数说明:
The AllocateCommonBuffer routine allocates memory and maps it so that it is simultaneously accessible from both the processor and a device for DMA operations。
函数定义:
PVOID AllocateCommonBuffer(
__in PDMA_ADAPTER DmaAdapter,
__in ULONG Length,
__out PPHYSICAL_ADDRESS LogicalAddress,
__in BOOLEAN CacheEnabled
);
注:第一个参数是IoGetDmaAdapter()返回的,第三个参数是自己顶一顶一个物理地址类型,用来接收分配得到的物理地址首址。
返回值:返回值为虚拟地址,供上层使用
实际代码中这样使用:
pdx->descAddress=pdx->allocateCommonBuffer(pdx->DmaAdapter,(ULONG)DESC_ADDRESS*PORT_NUM,&pdx->DescLogicalAddress,FALSE);
附:使用该函数必不可少的会使用下面的函数
pdx->DmaAdapter=IoGetDmaAdapter(pdx->NextStackDevice,DeviceDescription,&NumberOfMapRegisters);
//创建一个DMA适配器
pdx->allocateCommonBuffer=*pdx->DmaAdapter->DmaOperations->AllocateCommonBuffer;
//分配连续的物理内存DMA函数
pdx->freeCommonBuffer = *pdx->DmaAdapter->DmaOperations->FreeCommonBuffer;
//释放连续的物理内存DMA函数
pdx->putDmaAdapter=*pdx->DmaAdapter->DmaOperations->PutDmaAdapter;
//释放DMA Adapter对象
当通过AllocateCommonBuffer()分配得到地址之后需要把这个地址分成两部分来处理,与硬件打交道的需要用LogicAddress 与 应用程序打交道需用 Virtual Address.
最好将AllocateCommonBuffer()函数取得的这两个地址保存到设备扩展中特定的变量中(根据内存块的用处),便于以后操作。可以这样做:
pdx->RxDescVirBase=(PCHAR)pdx->descAddress;
pdx->RxDescPhyBase=(ULONG)(pdx->DescLogicalAddress.LowPart); //物理地址
WRITE_REGISTER_ULONG( (PULONG)&pdx->pHBARegs->RxAddr_des_0,pdx->rx_fc_des
c_buf_phy[0]+16);
WRITE_REGISTER_ULONG( (PULONG)&pdx->pHBARegs->RxAddr_des_addr0_ptr,pdx->r
x_fc_desc_buf_phy[0]+4);
注意前面的 +16 和 +4 实际分别代表美一块内存的的偏移位置,由于我们定义了这块内存的结构,并在里面布局,+16 其实代表了struct rx_fc_ctl_tab 结构中的 struct rx_fc_desc_entity entity[RX_FC_DESC_NUM] 域,这里又是一个结构,也就是我们最终会用到的“描述符”。注意:我们这里的操作是将一个地址写到一个寄存器,这个地址必须是物理地址,也就是前面说的LogicAddress。这里pHBARegs是通过pdx->pHBARegs=(PHBA_REGS)pdx->RegsBase;
获得的,也就是在CmResourceTypeMemory传过来时解析,代表硬件提供的DMA寄存器首址。
对于硬件的初始化就是通过WRITE_REGISTER_ULONG()函数来完成,针对设备的起始地址来进行偏移,自己定义结构来匹配各个寄存器的内容,注意这个自己定义的结构一定要严格跟硬件寄存器相同,一bit都不能差。
函数说明:
ExAllocatePool allocates pool memory of the specified type and returns a pointer to the allocated
block.
函数定义:
PVOID ExAllocatePool(
__in POOL_TYPE PoolType,
__in SIZE_T NumberOfBytes
);
代码中用来分配设备描述DMA adepter设备的特性(DEVICE_DESCRIPTION),作为参数传递给IoGetDmaAdapter()函数。在调用IoGetDmaAdapter()函数之前就要对PDEVICE_DESCRIPTION结构进行指定各种特性。
实际中这样使用:
PDEVICE_DESCRIPTION DeviceDescription = (PDEVICE_DESCRIPTION) ExAllocatePool
(PagedPool, sizeof(DEVICE_DESCRIPTION));
2. RtlZeroMemory()
函数说明:
The RtlZeroMemory routine fills a block of memory with zeros, given a pointer to the block and the length, in bytes, to be filled.
函数定义:
VOID RtlZeroMemory(
__in VOID UNALIGNED *Destination,
__in SIZE_T Length
);
3. IoGetDmaAdapter()
函数说明:
The IoGetDmaAdapter routine returns a pointer to the DMA adapter structure for a physical device object.
函数定义:
struct _DMA_ADAPTER* IoGetDmaAdapter(
__in_opt PDEVICE_OBJECT PhysicalDeviceObject,
__in struct _DEVICE_DESCRIPTION *DeviceDescription,
__out PULONG NumberOfMapRegisters
);
实际代码中这样使用:
ULONG NumberOfMapRegisters=100;
pdx->DmaAdapter=IoGetDmaAdapter(pdx->NextStackDevice,DeviceDescription,&NumberOfMapRegisters);// NextStackDevice在AddDevice函数中已经attach了。
4. AllocateCommonBuffer()
函数说明:
The AllocateCommonBuffer routine allocates memory and maps it so that it is simultaneously accessible from both the processor and a device for DMA operations。
函数定义:
PVOID AllocateCommonBuffer(
__in PDMA_ADAPTER DmaAdapter,
__in ULONG Length,
__out PPHYSICAL_ADDRESS LogicalAddress,
__in BOOLEAN CacheEnabled
);
注:第一个参数是IoGetDmaAdapter()返回的,第三个参数是自己顶一顶一个物理地址类型,用来接收分配得到的物理地址首址。
返回值:返回值为虚拟地址,供上层使用
实际代码中这样使用:
pdx->descAddress=pdx->allocateCommonBuffer(pdx->DmaAdapter,(ULONG)DESC_ADDRESS*PORT_NUM,&pdx->DescLogicalAddress,FALSE);
附:使用该函数必不可少的会使用下面的函数
pdx->DmaAdapter=IoGetDmaAdapter(pdx->NextStackDevice,DeviceDescription,&NumberOfMapRegisters);
//创建一个DMA适配器
pdx->allocateCommonBuffer=*pdx->DmaAdapter->DmaOperations->AllocateCommonBuffer;
//分配连续的物理内存DMA函数
pdx->freeCommonBuffer = *pdx->DmaAdapter->DmaOperations->FreeCommonBuffer;
//释放连续的物理内存DMA函数
pdx->putDmaAdapter=*pdx->DmaAdapter->DmaOperations->PutDmaAdapter;
//释放DMA Adapter对象
当通过AllocateCommonBuffer()分配得到地址之后需要把这个地址分成两部分来处理,与硬件打交道的需要用LogicAddress 与 应用程序打交道需用 Virtual Address.
最好将AllocateCommonBuffer()函数取得的这两个地址保存到设备扩展中特定的变量中(根据内存块的用处),便于以后操作。可以这样做:
pdx->RxDescVirBase=(PCHAR)pdx->descAddress;
pdx->RxDescPhyBase=(ULONG)(pdx->DescLogicalAddress.LowPart); //物理地址
WRITE_REGISTER_ULONG( (PULONG)&pdx->pHBARegs->RxAddr_des_0,pdx->rx_fc_des
c_buf_phy[0]+16);
WRITE_REGISTER_ULONG( (PULONG)&pdx->pHBARegs->RxAddr_des_addr0_ptr,pdx->r
x_fc_desc_buf_phy[0]+4);
注意前面的 +16 和 +4 实际分别代表美一块内存的的偏移位置,由于我们定义了这块内存的结构,并在里面布局,+16 其实代表了struct rx_fc_ctl_tab 结构中的 struct rx_fc_desc_entity entity[RX_FC_DESC_NUM] 域,这里又是一个结构,也就是我们最终会用到的“描述符”。注意:我们这里的操作是将一个地址写到一个寄存器,这个地址必须是物理地址,也就是前面说的LogicAddress。这里pHBARegs是通过pdx->pHBARegs=(PHBA_REGS)pdx->RegsBase;
获得的,也就是在CmResourceTypeMemory传过来时解析,代表硬件提供的DMA寄存器首址。
对于硬件的初始化就是通过WRITE_REGISTER_ULONG()函数来完成,针对设备的起始地址来进行偏移,自己定义结构来匹配各个寄存器的内容,注意这个自己定义的结构一定要严格跟硬件寄存器相同,一bit都不能差。
相关文章推荐
- 3.2 PCI设备的数据传递 分类: 浅谈PCI 2013-07-22 16:27 329人阅读 评论(0) 收藏
- 3.3.4 PCI设备进行DMA写时发生Cache命中 分类: 浅谈PCI 2013-07-22 16:27 309人阅读 评论(0) 收藏
- 2.3 PCI桥与PCI设备的配置空间 分类: 浅谈PCI 2013-07-22 16:27 311人阅读 评论(0) 收藏
- 3.3.2 PCI设备对不可Cache的存储器空间进行DMA读写 分类: 浅谈PCI 2013-07-22 16:27 411人阅读 评论(0) 收藏
- 3.1 PCI设备BAR空间的初始化 分类: 浅谈PCI 2013-07-22 16:27 367人阅读 评论(0) 收藏
- 3.3.3 PCI设备对可Cache的存储器空间进行DMA读写 分类: 浅谈PCI 2013-07-22 16:27 329人阅读 评论(0) 收藏
- 【总结】纯php 操作 mysql 数据库 分类: php mysql 数据库 增删改查 2014-04-16 14:47 461人阅读 评论(0) 收藏
- PCIe设备的配置空间 分类: 浅谈PCI-E 2013-07-22 16:31 720人阅读 评论(0) 收藏
- Linux系统常用目录操作函数 分类: B3_LINUX 2013-02-18 16:44 486人阅读 评论(0) 收藏
- PCI设备内存操作函数总结
- PCI设备内存操作函数总结
- PCI和PCIE插槽有什么区别? 分类: 浅谈PCI-E 2013-07-22 16:31 780人阅读 评论(0) 收藏
- PCI设备内存操作函数总结
- POI操作Excel常用方法总结 分类: B1_JAVA 2013-08-23 10:01 349人阅读 评论(0) 收藏
- Android - 文件读写操作 总结 分类: Android 2015-07-20 18:01 4人阅读 评论(0) 收藏
- PCI-X总线 分类: 浅谈PCI 2014-07-01 08:28 697人阅读 评论(2) 收藏
- python常用函数总结 分类: python基础学习 2014-02-07 14:12 260人阅读 评论(0) 收藏
- PCI-E配置MSI中断流程解析 分类: windows驱动程序WDM 浅谈PCI-E 2015-01-04 17:30 483人阅读 评论(0) 收藏
- 【总结】CI框架 操作 mysql 数据库 分类: php CI框架 mysql 增删改查 数据库 2014-08-03 22:16 200人阅读 评论(0) 收藏
- PHP从零单排(十六)PHP操作MySQL常见错误信息及分析 分类: 技巧总结 2014-05-03 11:34 139人阅读 评论(0) 收藏