TianoCore EDK2 master
|
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/MemEncryptSevLib.h>
#include <Library/MemEncryptTdxLib.h>
#include <Library/PcdLib.h>
#include <Library/SynchronizationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include "IoMmuInternal.h"
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 |
Copyright (c) 2022, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent
Definition in file IoMmuBuffer.c.
#define RESERVED_MEM_BITMAP_128K_MASK 0x3000 |
Definition at line 25 of file IoMmuBuffer.c.
#define RESERVED_MEM_BITMAP_1M_MASK 0x40000 |
Definition at line 26 of file IoMmuBuffer.c.
#define RESERVED_MEM_BITMAP_2M_MASK 0x180000 |
Definition at line 27 of file IoMmuBuffer.c.
#define RESERVED_MEM_BITMAP_32K_MASK 0xff0 |
Definition at line 24 of file IoMmuBuffer.c.
#define RESERVED_MEM_BITMAP_4K_MASK 0xf |
Definition at line 23 of file IoMmuBuffer.c.
#define RESERVED_MEM_BITMAP_MASK 0x1fffff |
Definition at line 28 of file IoMmuBuffer.c.
#define SIZE_OF_MEM_RANGE | ( | MemRange | ) | (MemRange->HeaderSize + MemRange->DataSize) |
Definition at line 21 of file IoMmuBuffer.c.
STATIC UINT32 CalcuateReservedMemSize | ( | VOID | ) |
Calculate the size of reserved memory.
UINT32 | Size of the reserved memory |
Definition at line 94 of file IoMmuBuffer.c.
Clear a bit in the reserved memory bitmap in a thread safe manner
ReservedMemBit | The bit to clear |
Definition at line 399 of file IoMmuBuffer.c.
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.
Type | Allocate type |
MemoryType | The memory type to be allocated |
Pages | Pages to be allocated. |
ReservedMemBitmap | Bitmap of the allocated memory region |
PhysicalAddress | Pointer to the data part of allocated memory region |
EFI_SUCCESS | Successfully allocate the buffer |
Other | As the error code indicates |
Definition at line 268 of file IoMmuBuffer.c.
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.
Type | Allocate type |
MemoryType | The memory type to be allocated |
MapInfo | Pointer to the MAP_INFO |
EFI_SUCCESS | Successfully allocate the bounce buffer |
Other | As the error code indicates |
Definition at line 372 of file IoMmuBuffer.c.
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.
MemoryType | Memory type |
CommonBufferPages | Pages of CommonBuffer |
PhysicalAddress | Allocated physical address |
ReservedMemBitmap | Bitmap which indicates the allocation of reserved memory |
EFI_SUCCESS | Successfully allocate the common buffer |
Other | As the error code indicates |
Definition at line 457 of file IoMmuBuffer.c.
EFI_STATUS IoMmuFreeBounceBuffer | ( | IN OUT MAP_INFO * | MapInfo | ) |
Free the bounce buffer allocated in IoMmuAllocateBounceBuffer.
MapInfo | Pointer to the MAP_INFO |
Definition at line 421 of file IoMmuBuffer.c.
EFI_STATUS IoMmuFreeCommonBuffer | ( | IN COMMON_BUFFER_HEADER * | CommonBufferHeader, |
IN UINTN | CommonBufferPages | ||
) |
Free CommonBuffer which is allocated by IoMmuAllocateCommonBuffer().
CommonBufferHeader | Pointer to the CommonBufferHeader |
CommonBufferPages | Pages of CommonBuffer |
EFI_SUCCESS | Successfully free the common buffer |
Other | As the error code indicates |
Definition at line 492 of file IoMmuBuffer.c.
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 | |-----------------------------------------|
EFI_SUCCESS | Successfully initialize the reserved memory. |
EFI_UNSUPPORTED | This feature is not supported. |
Definition at line 136 of file IoMmuBuffer.c.
EFI_STATUS IoMmuReleaseReservedSharedMem | ( | BOOLEAN | MemoryMapLocked | ) |
Release the pre-alloc shared memory.
EFI_SUCCESS | Successfully release the shared memory |
Definition at line 203 of file IoMmuBuffer.c.
STATIC UINT32 mReservedMemBitmap = 0 |
Definition at line 75 of file IoMmuBuffer.c.
STATIC IOMMU_RESERVED_MEM_RANGE mReservedMemRanges[] |
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 },
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.
STATIC EFI_PHYSICAL_ADDRESS mReservedSharedMemAddress = 0 |
Definition at line 80 of file IoMmuBuffer.c.
STATIC UINT32 mReservedSharedMemSize = 0 |
Definition at line 85 of file IoMmuBuffer.c.