TianoCore EDK2 master
Loading...
Searching...
No Matches
IoMmuBuffer.c File Reference

Go to the source code of this file.

Macros

#define SIZE_OF_MEM_RANGE(MemRange)   (MemRange->HeaderSize + MemRange->DataSize)
 
#define RESERVED_MEM_BITMAP_4K_MASK   0xf
 
#define RESERVED_MEM_BITMAP_32K_MASK   0xff0
 
#define RESERVED_MEM_BITMAP_128K_MASK   0x3000
 
#define RESERVED_MEM_BITMAP_1M_MASK   0x40000
 
#define RESERVED_MEM_BITMAP_2M_MASK   0x180000
 
#define RESERVED_MEM_BITMAP_MASK   0x1fffff
 

Functions

STATIC UINT32 CalcuateReservedMemSize (VOID)
 
EFI_STATUS IoMmuInitReservedSharedMem (VOID)
 
EFI_STATUS IoMmuReleaseReservedSharedMem (BOOLEAN MemoryMapLocked)
 
STATIC EFI_STATUS InternalAllocateBuffer (IN EFI_ALLOCATE_TYPE Type, IN EFI_MEMORY_TYPE MemoryType, IN UINTN Pages, OUT UINT32 *ReservedMemBit, IN OUT EFI_PHYSICAL_ADDRESS *PhysicalAddress)
 
EFI_STATUS IoMmuAllocateBounceBuffer (IN EFI_ALLOCATE_TYPE Type, IN EFI_MEMORY_TYPE MemoryType, IN OUT MAP_INFO *MapInfo)
 
STATIC VOID ClearReservedMemBit (IN UINT32 ReservedMemBit)
 
EFI_STATUS IoMmuFreeBounceBuffer (IN OUT MAP_INFO *MapInfo)
 
EFI_STATUS IoMmuAllocateCommonBuffer (IN EFI_MEMORY_TYPE MemoryType, IN UINTN CommonBufferPages, OUT EFI_PHYSICAL_ADDRESS *PhysicalAddress, OUT UINT32 *ReservedMemBitmap)
 
EFI_STATUS IoMmuFreeCommonBuffer (IN COMMON_BUFFER_HEADER *CommonBufferHeader, IN UINTN CommonBufferPages)
 

Variables

BOOLEAN mReservedSharedMemSupported
 
STATIC IOMMU_RESERVED_MEM_RANGE mReservedMemRanges []
 
STATIC UINT32 mReservedMemBitmap = 0
 
STATIC EFI_PHYSICAL_ADDRESS mReservedSharedMemAddress = 0
 
STATIC UINT32 mReservedSharedMemSize = 0
 

Detailed Description

Copyright (c) 2022, Intel Corporation. All rights reserved.

SPDX-License-Identifier: BSD-2-Clause-Patent

Definition in file IoMmuBuffer.c.

Macro Definition Documentation

◆ RESERVED_MEM_BITMAP_128K_MASK

#define RESERVED_MEM_BITMAP_128K_MASK   0x3000

Definition at line 25 of file IoMmuBuffer.c.

◆ RESERVED_MEM_BITMAP_1M_MASK

#define RESERVED_MEM_BITMAP_1M_MASK   0x40000

Definition at line 26 of file IoMmuBuffer.c.

◆ RESERVED_MEM_BITMAP_2M_MASK

#define RESERVED_MEM_BITMAP_2M_MASK   0x180000

Definition at line 27 of file IoMmuBuffer.c.

◆ RESERVED_MEM_BITMAP_32K_MASK

#define RESERVED_MEM_BITMAP_32K_MASK   0xff0

Definition at line 24 of file IoMmuBuffer.c.

◆ RESERVED_MEM_BITMAP_4K_MASK

#define RESERVED_MEM_BITMAP_4K_MASK   0xf

Definition at line 23 of file IoMmuBuffer.c.

◆ RESERVED_MEM_BITMAP_MASK

#define RESERVED_MEM_BITMAP_MASK   0x1fffff

Definition at line 28 of file IoMmuBuffer.c.

◆ SIZE_OF_MEM_RANGE

#define SIZE_OF_MEM_RANGE (   MemRange)    (MemRange->HeaderSize + MemRange->DataSize)

Definition at line 21 of file IoMmuBuffer.c.

Function Documentation

◆ CalcuateReservedMemSize()

STATIC UINT32 CalcuateReservedMemSize ( VOID  )

Calculate the size of reserved memory.

Return values
UINT32Size of the reserved memory

Definition at line 94 of file IoMmuBuffer.c.

◆ ClearReservedMemBit()

STATIC VOID ClearReservedMemBit ( IN UINT32  ReservedMemBit)

Clear a bit in the reserved memory bitmap in a thread safe manner

Parameters
ReservedMemBitThe bit to clear

Definition at line 399 of file IoMmuBuffer.c.

◆ InternalAllocateBuffer()

STATIC EFI_STATUS InternalAllocateBuffer ( IN EFI_ALLOCATE_TYPE  Type,
IN EFI_MEMORY_TYPE  MemoryType,
IN UINTN  Pages,
OUT UINT32 *  ReservedMemBit,
IN OUT EFI_PHYSICAL_ADDRESS PhysicalAddress 
)

Allocate from the reserved memory pool. If the reserved shared memory is exausted or there is no suitalbe size, it turns to the LegacyAllocateBuffer.

Parameters
TypeAllocate type
MemoryTypeThe memory type to be allocated
PagesPages to be allocated.
ReservedMemBitmapBitmap of the allocated memory region
PhysicalAddressPointer to the data part of allocated memory region
Return values
EFI_SUCCESSSuccessfully allocate the buffer
OtherAs the error code indicates

Definition at line 268 of file IoMmuBuffer.c.

◆ IoMmuAllocateBounceBuffer()

EFI_STATUS IoMmuAllocateBounceBuffer ( IN EFI_ALLOCATE_TYPE  Type,
IN EFI_MEMORY_TYPE  MemoryType,
IN OUT MAP_INFO MapInfo 
)

Allocate reserved shared memory for bounce buffer.

Parameters
TypeAllocate type
MemoryTypeThe memory type to be allocated
MapInfoPointer to the MAP_INFO
Return values
EFI_SUCCESSSuccessfully allocate the bounce buffer
OtherAs the error code indicates

Definition at line 372 of file IoMmuBuffer.c.

◆ IoMmuAllocateCommonBuffer()

EFI_STATUS IoMmuAllocateCommonBuffer ( IN EFI_MEMORY_TYPE  MemoryType,
IN UINTN  CommonBufferPages,
OUT EFI_PHYSICAL_ADDRESS PhysicalAddress,
OUT UINT32 *  ReservedMemBitmap 
)

Allocate CommonBuffer from pre-allocated shared memory.

Parameters
MemoryTypeMemory type
CommonBufferPagesPages of CommonBuffer
PhysicalAddressAllocated physical address
ReservedMemBitmapBitmap which indicates the allocation of reserved memory
Return values
EFI_SUCCESSSuccessfully allocate the common buffer
OtherAs the error code indicates

Definition at line 457 of file IoMmuBuffer.c.

◆ IoMmuFreeBounceBuffer()

EFI_STATUS IoMmuFreeBounceBuffer ( IN OUT MAP_INFO MapInfo)

Free the bounce buffer allocated in IoMmuAllocateBounceBuffer.

Parameters
MapInfoPointer to the MAP_INFO
Returns
EFI_SUCCESS Successfully free the bounce buffer.

Definition at line 421 of file IoMmuBuffer.c.

◆ IoMmuFreeCommonBuffer()

EFI_STATUS IoMmuFreeCommonBuffer ( IN COMMON_BUFFER_HEADER CommonBufferHeader,
IN UINTN  CommonBufferPages 
)

Free CommonBuffer which is allocated by IoMmuAllocateCommonBuffer().

Parameters
CommonBufferHeaderPointer to the CommonBufferHeader
CommonBufferPagesPages of CommonBuffer
Return values
EFI_SUCCESSSuccessfully free the common buffer
OtherAs the error code indicates

Definition at line 492 of file IoMmuBuffer.c.

◆ IoMmuInitReservedSharedMem()

EFI_STATUS IoMmuInitReservedSharedMem ( VOID  )

Allocate a memory region and convert it to be shared. This memory region will be used in the DMA operation.

The pre-alloc memory contains pieces of memory regions with different size. The allocation of the shared memory regions are indicated by a 32-bit bitmap (mReservedMemBitmap).

The memory regions are consumed by IoMmuAllocateBuffer (in which CommonBuffer is allocated) and IoMmuMap (in which bounce buffer is allocated).

The CommonBuffer contains 2 parts, one page for CommonBufferHeader which is private memory, the other part is shared memory. So the layout of a piece of memory region after initialization looks like:

|------------|----------------------------|
| Header     |    Data                    |  <-- a piece of pre-alloc memory region
| 4k, private| 4k/32k/128k/etc, shared    |
|-----------------------------------------|
Return values
EFI_SUCCESSSuccessfully initialize the reserved memory.
EFI_UNSUPPORTEDThis feature is not supported.

Definition at line 136 of file IoMmuBuffer.c.

◆ IoMmuReleaseReservedSharedMem()

EFI_STATUS IoMmuReleaseReservedSharedMem ( BOOLEAN  MemoryMapLocked)

Release the pre-alloc shared memory.

Return values
EFI_SUCCESSSuccessfully release the shared memory

Definition at line 203 of file IoMmuBuffer.c.

Variable Documentation

◆ mReservedMemBitmap

STATIC UINT32 mReservedMemBitmap = 0

Definition at line 75 of file IoMmuBuffer.c.

◆ mReservedMemRanges

STATIC IOMMU_RESERVED_MEM_RANGE mReservedMemRanges[]
Initial value:
= {
{ RESERVED_MEM_BITMAP_4K_MASK, 0, 4, SIZE_4KB, SIZE_4KB, 0 },
{ RESERVED_MEM_BITMAP_32K_MASK, 4, 8, SIZE_32KB, SIZE_4KB, 0 },
{ RESERVED_MEM_BITMAP_128K_MASK, 12, 2, SIZE_128KB, SIZE_4KB, 0 },
{ RESERVED_MEM_BITMAP_1M_MASK, 14, 1, SIZE_1MB, SIZE_4KB, 0 },
{ RESERVED_MEM_BITMAP_2M_MASK, 15, 2, SIZE_2MB, SIZE_4KB, 0 },
}

mReservedMemRanges describes the layout of the reserved memory. The reserved memory consists of disfferent size of memory region. The pieces of memory with the same size are managed by one entry in the mReservedMemRanges. All the pieces of memories are managed by mReservedMemBitmap which is a UINT32. It means it can manage at most 32 pieces of memory. Because of the layout of CommonBuffer (1-page header + n-page data), a piece of reserved memory consists of 2 parts: Header + Data.

So put all these together, mReservedMemRanges and mReservedMemBitmap are designed to manage the reserved memory.

Use the second entry of mReservedMemRanges as an example. { RESERVED_MEM_BITMAP_32K_MASK, 4, 8, SIZE_32KB, SIZE_4KB, 0 },

  • RESERVED_MEM_BITMAP_32K_MASK is 0xff0. It means bit4-11 in mReservedMemBitmap is reserved for 32K size memory.
  • 4 is the shift of mReservedMemBitmap.
  • 8 means there are 8 pieces of 32K size memory.
  • SIZE_32KB indicates the size of Data part.
  • SIZE_4KB is the size of Header part.
  • 0 is the start address of this memory range which will be populated when the reserved memory is initialized.

The size and count of the memory region are derived from the experience. For a typical grub boot, there are about 5100 IoMmu/DMA operation. Most of these DMA operation require the memory with size less than 32K (~5080). But we find in grub boot there may be 2 DMA operation which require for the memory larger than 1M. And these 2 DMA operation occur concurrently. So we reserve 2 pieces of memory with size of SIZE_2MB. This is for the best boot performance.

If all the reserved memory are exausted, then it will fall back to the legacy memory allocation as before.

Definition at line 64 of file IoMmuBuffer.c.

◆ mReservedSharedMemAddress

STATIC EFI_PHYSICAL_ADDRESS mReservedSharedMemAddress = 0

Definition at line 80 of file IoMmuBuffer.c.

◆ mReservedSharedMemSize

STATIC UINT32 mReservedSharedMemSize = 0

Definition at line 85 of file IoMmuBuffer.c.

◆ mReservedSharedMemSupported

BOOLEAN mReservedSharedMemSupported
extern

Definition at line 30 of file CcIoMmu.c.