19extern BOOLEAN mReservedSharedMemSupported;
21#define SIZE_OF_MEM_RANGE(MemRange) (MemRange->HeaderSize + MemRange->DataSize)
23#define RESERVED_MEM_BITMAP_4K_MASK 0xf
24#define RESERVED_MEM_BITMAP_32K_MASK 0xff0
25#define RESERVED_MEM_BITMAP_128K_MASK 0x3000
26#define RESERVED_MEM_BITMAP_1M_MASK 0x40000
27#define RESERVED_MEM_BITMAP_2M_MASK 0x180000
28#define RESERVED_MEM_BITMAP_MASK 0x1fffff
65 { RESERVED_MEM_BITMAP_4K_MASK, 0, 4, SIZE_4KB, SIZE_4KB, 0 },
66 { RESERVED_MEM_BITMAP_32K_MASK, 4, 8, SIZE_32KB, SIZE_4KB, 0 },
67 { RESERVED_MEM_BITMAP_128K_MASK, 12, 2, SIZE_128KB, SIZE_4KB, 0 },
68 { RESERVED_MEM_BITMAP_1M_MASK, 14, 1, SIZE_1MB, SIZE_4KB, 0 },
69 { RESERVED_MEM_BITMAP_2M_MASK, 15, 2, SIZE_2MB, SIZE_4KB, 0 },
75STATIC UINT32 mReservedMemBitmap = 0;
85STATIC UINT32 mReservedSharedMemSize = 0;
101 if (mReservedSharedMemSize != 0) {
102 return mReservedSharedMemSize;
107 mReservedSharedMemSize += (SIZE_OF_MEM_RANGE (MemRange) * MemRange->Slots);
110 return mReservedSharedMemSize;
141 UINT32 Index1, Index2;
145 UINT64 SharedAddress;
147 if (!mReservedSharedMemSupported) {
148 return EFI_UNSUPPORTED;
156 "%a: ReservedMem (%d pages) address = 0x%llx\n",
162 mReservedMemBitmap = 0;
163 mReservedSharedMemAddress = PhysicalAddress;
167 MemRange->StartAddressOfMemRange = PhysicalAddress;
169 for (Index2 = 0; Index2 < MemRange->Slots; Index2++) {
170 SharedAddress = (UINT64)(
UINTN)(MemRange->StartAddressOfMemRange + Index2 * SIZE_OF_MEM_RANGE (MemRange) + MemRange->HeaderSize);
172 if (CC_GUEST_IS_SEV (
PcdGet64 (PcdConfidentialComputingGuestAttr))) {
178 ASSERT (!EFI_ERROR (Status));
179 }
else if (CC_GUEST_IS_TDX (
PcdGet64 (PcdConfidentialComputingGuestAttr))) {
185 ASSERT (!EFI_ERROR (Status));
191 PhysicalAddress += (MemRange->Slots * SIZE_OF_MEM_RANGE (MemRange));
204 BOOLEAN MemoryMapLocked
208 UINT32 Index1, Index2;
210 UINT64 SharedAddress;
212 if (!mReservedSharedMemSupported) {
218 for (Index2 = 0; Index2 < MemRange->Slots; Index2++) {
219 SharedAddress = (UINT64)(
UINTN)(MemRange->StartAddressOfMemRange + Index2 * SIZE_OF_MEM_RANGE (MemRange) + MemRange->HeaderSize);
221 if (CC_GUEST_IS_SEV (
PcdGet64 (PcdConfidentialComputingGuestAttr))) {
227 ASSERT (!EFI_ERROR (Status));
228 }
else if (CC_GUEST_IS_TDX (
PcdGet64 (PcdConfidentialComputingGuestAttr))) {
234 ASSERT (!EFI_ERROR (Status));
241 if (!MemoryMapLocked) {
243 mReservedSharedMemAddress = 0;
244 mReservedMemBitmap = 0;
247 mReservedSharedMemSupported =
FALSE;
272 OUT UINT32 *ReservedMemBit,
277 UINT32 ReservedMemBitmap;
280 UINTN PagesOfLastMemRange;
286 return EFI_INVALID_PARAMETER;
289 if (!mReservedSharedMemSupported) {
290 goto LegacyAllocateBuffer;
293 if (mReservedSharedMemAddress == 0) {
294 goto LegacyAllocateBuffer;
297 PagesOfLastMemRange = 0;
309 goto LegacyAllocateBuffer;
315 ReservedMemBitmap = mReservedMemBitmap;
317 if ((ReservedMemBitmap & MemRange->BitmapMask) == MemRange->BitmapMask) {
319 goto LegacyAllocateBuffer;
322 MemBitmap = (ReservedMemBitmap & MemRange->BitmapMask) >> MemRange->Shift;
324 for (Index = 0; Index < MemRange->Slots; Index++) {
325 if ((MemBitmap & (UINT8)(1<<Index)) == 0) {
330 ASSERT (Index != MemRange->Slots);
332 *PhysicalAddress = MemRange->StartAddressOfMemRange + Index * SIZE_OF_MEM_RANGE (MemRange) + MemRange->HeaderSize;
333 *ReservedMemBit = (UINT32)(1 << (Index + MemRange->Shift));
337 ReservedMemBitmap | *ReservedMemBit
342 "%a: range-size: %lx, start-address=0x%llx, pages=0x%llx, bits=0x%lx, bitmap: %lx => %lx\n",
349 ReservedMemBitmap | *ReservedMemBit
357 return gBS->AllocatePages (Type, MemoryType, Pages, PhysicalAddress);
383 MapInfo->NumberOfPages,
384 &MapInfo->ReservedMemBitmap,
385 &MapInfo->PlainTextAddress
400 IN UINT32 ReservedMemBit
403 UINT32 ReservedMemBitmap;
406 ReservedMemBitmap = mReservedMemBitmap;
410 ReservedMemBitmap & ~ReservedMemBit
425 if (MapInfo->ReservedMemBitmap == 0) {
426 gBS->FreePages (MapInfo->PlainTextAddress, MapInfo->NumberOfPages);
430 "%a: PlainTextAddress=0x%Lx, bits=0x%Lx, bitmap: %Lx => %Lx\n",
432 MapInfo->PlainTextAddress,
433 MapInfo->ReservedMemBitmap,
435 mReservedMemBitmap & ((UINT32)(~MapInfo->ReservedMemBitmap))
438 MapInfo->PlainTextAddress = 0;
439 MapInfo->ReservedMemBitmap = 0;
461 OUT UINT32 *ReservedMemBitmap
475 if (*ReservedMemBitmap != 0) {
476 *PhysicalAddress -= SIZE_4KB;
497 if (!mReservedSharedMemSupported) {
498 goto LegacyFreeCommonBuffer;
501 if (CommonBufferHeader->ReservedMemBitmap == 0) {
502 goto LegacyFreeCommonBuffer;
507 "%a: CommonBuffer=0x%Lx, bits=0x%Lx, bitmap: %Lx => %Lx\n",
509 (UINT64)(
UINTN)CommonBufferHeader + SIZE_4KB,
510 CommonBufferHeader->ReservedMemBitmap,
512 mReservedMemBitmap & ((UINT32)(~CommonBufferHeader->ReservedMemBitmap))
518LegacyFreeCommonBuffer:
519 return gBS->FreePages ((
UINTN)CommonBufferHeader, CommonBufferPages);
VOID EFIAPI FreePages(IN VOID *Buffer, IN UINTN Pages)
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 IoMmuAllocateCommonBuffer(IN EFI_MEMORY_TYPE MemoryType, IN UINTN CommonBufferPages, OUT EFI_PHYSICAL_ADDRESS *PhysicalAddress, OUT UINT32 *ReservedMemBitmap)
EFI_STATUS IoMmuFreeBounceBuffer(IN OUT MAP_INFO *MapInfo)
EFI_STATUS IoMmuFreeCommonBuffer(IN COMMON_BUFFER_HEADER *CommonBufferHeader, IN UINTN CommonBufferPages)
EFI_STATUS IoMmuInitReservedSharedMem(VOID)
EFI_STATUS IoMmuReleaseReservedSharedMem(BOOLEAN MemoryMapLocked)
EFI_STATUS IoMmuAllocateBounceBuffer(IN EFI_ALLOCATE_TYPE Type, IN EFI_MEMORY_TYPE MemoryType, IN OUT MAP_INFO *MapInfo)
STATIC VOID ClearReservedMemBit(IN UINT32 ReservedMemBit)
STATIC UINT32 CalcuateReservedMemSize(VOID)
STATIC IOMMU_RESERVED_MEM_RANGE mReservedMemRanges[]
#define ARRAY_SIZE(Array)
#define DEBUG(Expression)
RETURN_STATUS EFIAPI MemEncryptSevClearPageEncMask(IN PHYSICAL_ADDRESS Cr3BaseAddress, IN PHYSICAL_ADDRESS BaseAddress, IN UINTN NumPages)
RETURN_STATUS EFIAPI MemEncryptSevSetPageEncMask(IN PHYSICAL_ADDRESS Cr3BaseAddress, IN PHYSICAL_ADDRESS BaseAddress, IN UINTN NumPages)
RETURN_STATUS EFIAPI MemEncryptTdxSetPageSharedBit(IN PHYSICAL_ADDRESS Cr3BaseAddress, IN PHYSICAL_ADDRESS BaseAddress, IN UINTN NumPages)
RETURN_STATUS EFIAPI MemEncryptTdxClearPageSharedBit(IN PHYSICAL_ADDRESS Cr3BaseAddress, IN PHYSICAL_ADDRESS BaseAddress, IN UINTN NumPages)
#define PcdGet64(TokenName)
VOID *EFIAPI AllocatePages(IN UINTN Pages)
UINT32 EFIAPI InterlockedCompareExchange32(IN OUT volatile UINT32 *Value, IN UINT32 CompareValue, IN UINT32 ExchangeValue)
UINT64 EFI_PHYSICAL_ADDRESS
#define EFI_SIZE_TO_PAGES(Size)