IoMmuInternal.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /** @file
  2. Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #ifndef IOMMU_INTERNAL_H_
  6. #define IOMMU_INTERNAL_H_
  7. #include <Base.h>
  8. #include <Protocol/IoMmu.h>
  9. #include <Uefi/UefiBaseType.h>
  10. #include <Uefi/UefiSpec.h>
  11. #define MAP_INFO_SIG SIGNATURE_64 ('M', 'A', 'P', '_', 'I', 'N', 'F', 'O')
  12. typedef struct {
  13. UINT64 Signature;
  14. LIST_ENTRY Link;
  15. EDKII_IOMMU_OPERATION Operation;
  16. UINTN NumberOfBytes;
  17. UINTN NumberOfPages;
  18. EFI_PHYSICAL_ADDRESS CryptedAddress;
  19. EFI_PHYSICAL_ADDRESS PlainTextAddress;
  20. UINT32 ReservedMemBitmap;
  21. } MAP_INFO;
  22. #define COMMON_BUFFER_SIG SIGNATURE_64 ('C', 'M', 'N', 'B', 'U', 'F', 'F', 'R')
  23. #pragma pack (1)
  24. //
  25. // The following structure enables Map() and Unmap() to perform in-place
  26. // decryption and encryption, respectively, for BusMasterCommonBuffer[64]
  27. // operations, without dynamic memory allocation or release.
  28. //
  29. // Both COMMON_BUFFER_HEADER and COMMON_BUFFER_HEADER.StashBuffer are allocated
  30. // by AllocateBuffer() and released by FreeBuffer().
  31. //
  32. typedef struct {
  33. UINT64 Signature;
  34. //
  35. // Always allocated from EfiBootServicesData type memory, and always
  36. // encrypted.
  37. //
  38. VOID *StashBuffer;
  39. //
  40. // Bitmap of reserved memory
  41. //
  42. UINT32 ReservedMemBitmap;
  43. //
  44. // Followed by the actual common buffer, starting at the next page.
  45. //
  46. } COMMON_BUFFER_HEADER;
  47. //
  48. // This data structure defines a memory range in the reserved memory region.
  49. // Please refer to IoMmuInitReservedSharedMem() for detailed information.
  50. //
  51. // The memory region looks like:
  52. // |------------|----------------------------|
  53. // | Header | Data |
  54. // | 4k, private| 4k/32k/128k/etc, shared |
  55. // |-----------------------------------------|
  56. //
  57. typedef struct {
  58. UINT32 BitmapMask;
  59. UINT32 Shift;
  60. UINT32 Slots;
  61. UINT32 DataSize;
  62. UINT32 HeaderSize;
  63. EFI_PHYSICAL_ADDRESS StartAddressOfMemRange;
  64. } IOMMU_RESERVED_MEM_RANGE;
  65. #pragma pack()
  66. /**
  67. * Allocate a memory region and convert it to be shared. This memory region will be
  68. * used in the DMA operation.
  69. *
  70. * The pre-alloc memory contains pieces of memory regions with different size. The
  71. * allocation of the shared memory regions are indicated by a 32-bit bitmap (mReservedMemBitmap).
  72. *
  73. * The memory regions are consumed by IoMmuAllocateBuffer (in which CommonBuffer is allocated) and
  74. * IoMmuMap (in which bounce buffer is allocated).
  75. *
  76. * The CommonBuffer contains 2 parts, one page for CommonBufferHeader which is private memory,
  77. * the other part is shared memory. So the layout of a piece of memory region after initialization
  78. * looks like:
  79. *
  80. * |------------|----------------------------|
  81. * | Header | Data | <-- a piece of pre-alloc memory region
  82. * | 4k, private| 4k/32k/128k/etc, shared |
  83. * |-----------------------------------------|
  84. *
  85. * @retval EFI_SUCCESS Successfully initialize the reserved memory.
  86. * @retval EFI_UNSUPPORTED This feature is not supported.
  87. */
  88. EFI_STATUS
  89. IoMmuInitReservedSharedMem (
  90. VOID
  91. );
  92. /**
  93. * Release the pre-alloc shared memory.
  94. *
  95. * @retval EFI_SUCCESS Successfully release the shared memory
  96. */
  97. EFI_STATUS
  98. IoMmuReleaseReservedSharedMem (
  99. BOOLEAN MemoryMapLocked
  100. );
  101. /**
  102. * Allocate reserved shared memory for bounce buffer.
  103. *
  104. * @param Type Allocate type
  105. * @param MemoryType The memory type to be allocated
  106. * @param MapInfo Pointer to the MAP_INFO
  107. *
  108. * @retval EFI_SUCCESS Successfully allocate the bounce buffer
  109. * @retval Other As the error code indicates
  110. */
  111. EFI_STATUS
  112. IoMmuAllocateBounceBuffer (
  113. IN EFI_ALLOCATE_TYPE Type,
  114. IN EFI_MEMORY_TYPE MemoryType,
  115. IN OUT MAP_INFO *MapInfo
  116. );
  117. /**
  118. * Free the bounce buffer allocated in IoMmuAllocateBounceBuffer.
  119. *
  120. * @param MapInfo Pointer to the MAP_INFO
  121. * @return EFI_SUCCESS Successfully free the bounce buffer.
  122. */
  123. EFI_STATUS
  124. IoMmuFreeBounceBuffer (
  125. IN OUT MAP_INFO *MapInfo
  126. );
  127. /**
  128. * Allocate CommonBuffer from pre-allocated shared memory.
  129. *
  130. * @param MemoryType Memory type
  131. * @param CommonBufferPages Pages of CommonBuffer
  132. * @param PhysicalAddress Allocated physical address
  133. * @param ReservedMemBitmap Bitmap which indicates the allocation of reserved memory
  134. *
  135. * @retval EFI_SUCCESS Successfully allocate the common buffer
  136. * @retval Other As the error code indicates
  137. */
  138. EFI_STATUS
  139. IoMmuAllocateCommonBuffer (
  140. IN EFI_MEMORY_TYPE MemoryType,
  141. IN UINTN CommonBufferPages,
  142. OUT EFI_PHYSICAL_ADDRESS *PhysicalAddress,
  143. OUT UINT32 *ReservedMemBitmap
  144. );
  145. /**
  146. * Free CommonBuffer which is allocated by IoMmuAllocateCommonBuffer().
  147. *
  148. * @param CommonBufferHeader Pointer to the CommonBufferHeader
  149. * @param CommonBufferPages Pages of CommonBuffer
  150. *
  151. * @retval EFI_SUCCESS Successfully free the common buffer
  152. * @retval Other As the error code indicates
  153. */
  154. EFI_STATUS
  155. IoMmuFreeCommonBuffer (
  156. IN COMMON_BUFFER_HEADER *CommonBufferHeader,
  157. IN UINTN CommonBufferPages
  158. );
  159. #endif