zoukankan      html  css  js  c++  java
  • PCI设备内存操作函数总结 分类: 浅谈PCI 2014-05-26 17:48 580人阅读 评论(0) 收藏

    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都不能差。

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Find the Smallest K Elements in an Array
    Count of Smaller Number
    Number of Inversion Couple
    Delete False Elements
    Sort Array
    Tree Diameter
    Segment Tree Implementation
    Java Programming Mock Tests
    zz Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)
    Algorithm about SubArrays & SubStrings
  • 原文地址:https://www.cnblogs.com/mao0504/p/4706620.html
Copyright © 2011-2022 走看看