Re: [edk2] Memory allocation on x64

Subject: Re: [edk2] Memory allocation on x64

From: Andrew Fish <afish@apple.com>

To: edk2-devel@lists.sourceforge.net

Date: 2011-09-16 23:48:21

It is not safe to use gBS->AllocatePool() for DMA. It may work on most systems, but it will not work on all systems. It also breaks almost all the time on ARM systems if you don't follow the DMA rules since DMA traffic is not coherent with memory. 

In UEFI 2.3.1 check out EFI_PCI_IO_PROTOCOL

Bus mastering PCI controllers can use the DMA services for DMA operations. There are three basic types of bus mastering DMA that is supported by this protocol. These are DMA reads by a bus master, DMA writes by a bus master, and common buffer DMA. The DMA read and write operations may need to be broken into smaller chunks. The caller of Map() must pay attention to the number of bytes that were mapped, and if required, loop until the entire buffer has been transferred. The following is a list of the different bus mastering DMA operations that are supported, and the sequence of EFI_PCI_IO_PROTOCOL interfaces that are used for each DMA operation type.

DMA Bus Master Read Operation:
==========================
Call Map() for EfiPciIoOperationBusMasterRead.
Program the DMA Bus Master with the DeviceAddress returned by Map(). Start the DMA Bus Master.
Wait for DMA Bus Master to complete the read operation.
Call Unmap().

DMA Bus Master Write Operation:
==========================
Call Map() for EfiPciOperationBusMasterWrite.
Program the DMA Bus Master with the DeviceAddress returned by Map().
Start the DMA Bus Master.
Wait for DMA Bus Master to complete the write operation.
Perform a PCI controller specific read transaction to flush all PCI write buffers (See PCI Specification Section 3.2.5.2) .
Call Flush().
Call Unmap().

DMA Bus Master Common Buffer Operation:
==================================
Call AllocateBuffer() to allocate a common buffer.
Call Map() for EfiPciIoOperationBusMasterCommonBuffer.
Program the DMA Bus Master with the DeviceAddress returned by Map().
The common buffer can now be accessed equally by the processor and the DMA bus master. Call Unmap().
Call FreeBuffer().

Andrew Fish





On Sep 16, 2011, at 11:09 AM, rangasai C wrote:

Thanks for that explanation. 
In case of PCI device drivers, currently for DMA operations, I am using gBS->AllocatePool() to allocate memory. I've noticed there is a EFI_PCI_IO_PROTOCOL function called AllocateBuffer ()

I am trying to understand the difference between gBS->AllocatePool () versus EFI_PCI_IO_PROTOCOL.AllocateBuffer () and which services is recommended to use while writing PCI device drivers ?

Thanks

On Wed, Sep 7, 2011 at 1:35 AM, Sun, Rui <rui.sun@intel.com> wrote:

Sai,

 

Generally, if a x64 platform reports available memory ranges (with TESTED_MEMORY_ATTRIBUTES) above 4GB via memory resource HOB to the DXE GCD, a buffer allocated via AllocatePool() or AllocatePages() can be anywhere in the 64-bit address space. If for some reason, a buffer under 4GB is required, for example, a 32-bit address for a table, then AllocatePages() can be called with AllocateMaxAddress Type and Memory being 0xFFFFFFFF.

 

It is possible that a x64 platform does not report available memory ranges (for example, without TESTED attribute) above 4GB to the DXE GCD, then normally a buffer allocated via AllocatePool() or AllocatePages() will be under 4GB. These memory ranges will be treated as reserved and wont be for allocation unless there is any memory promotion (turning reserved memory into available memory if out of memory).

 

 

From: rangasai C [mailto:rangasaic@gmail.com]
Sent: Wednesday, September 07, 2011 10:17 AM
To: edk2-devel@lists.sourceforge.net
Subject: Re: [edk2] Memory allocation on x64

 

How do I do that or should I be doing that ?

My understanding was it is not recommended to make any assumption on system memory map. I also read some where that it is not recommended to do things like ignoring upper 32 bits or masking them off. 

 

Could you clarify ?

 

Thanks,

Sai

On Tue, Sep 6, 2011 at 7:09 PM, Andrew Fish <afish@apple.com> wrote:

For portable code you need to request a buffer under 4GB if that is what you need. 

 

Andrew Fish

 

 

 

 

On Sep 6, 2011, at 6:43 PM, rangasai C wrote:

 

Hello all,

I was going through the memory allocation details in pre-boot environment. 

For IA-32 platforms, boot services like gBS->AllocatePool() will allocate a buffer below 4GB. 

For Itanium based platforms, the buffer could be allocated anywhere in the 64 bit address space. 

On a x64 based platform during pre-boot ( before ExitBootServices() is called, will a buffer be allocated below 4GB or will it be anywhere in the 64 bit address space ?

 

Thanks,

Sai

------------------------------------------------------------------------------
Using storage to extend the benefits of virtualization and iSCSI
Virtualization increases hardware utilization and delivers a new level of
agility. Learn what those decisions are and how to modernize your storage
and backup environments for virtualization.
http://www.accelacomm.com/jaw/sfnl/114/51434361/_______________________________________________
edk2-devel mailing list
edk2-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/edk2-devel

 


------------------------------------------------------------------------------
Using storage to extend the benefits of virtualization and iSCSI
Virtualization increases hardware utilization and delivers a new level of
agility. Learn what those decisions are and how to modernize your storage
and backup environments for virtualization.
http://www.accelacomm.com/jaw/sfnl/114/51434361/
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/edk2-devel

 


------------------------------------------------------------------------------
Using storage to extend the benefits of virtualization and iSCSI
Virtualization increases hardware utilization and delivers a new level of
agility. Learn what those decisions are and how to modernize your storage
and backup environments for virtualization.
http://www.accelacomm.com/jaw/sfnl/114/51434361/
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/edk2-devel


------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
http://p.sf.net/sfu/rim-devcon-copy2_______________________________________________
edk2-devel mailing list
edk2-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/edk2-devel