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