]>
Commit | Line | Data |
---|---|---|
f9d129e6 BS |
1 | /** @file\r |
2 | \r | |
812568fb LE |
3 | The protocol provides support to allocate, free, map and umap a DMA buffer\r |
4 | for bus master (e.g PciHostBridge). When SEV is enabled, the DMA operations\r | |
5 | must be performed on unencrypted buffer hence we use a bounce buffer to map\r | |
6 | the guest buffer into an unencrypted DMA buffer.\r | |
f9d129e6 BS |
7 | \r |
8 | Copyright (c) 2017, AMD Inc. All rights reserved.<BR>\r | |
9 | Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>\r | |
10 | \r | |
11 | This program and the accompanying materials are licensed and made available\r | |
12 | under the terms and conditions of the BSD License which accompanies this\r | |
13 | distribution. The full text of the license may be found at\r | |
14 | http://opensource.org/licenses/bsd-license.php\r | |
15 | \r | |
16 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
17 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
18 | \r | |
19 | **/\r | |
20 | \r | |
21 | #include "AmdSevIoMmu.h"\r | |
22 | \r | |
23 | typedef struct {\r | |
24 | EDKII_IOMMU_OPERATION Operation;\r | |
25 | UINTN NumberOfBytes;\r | |
26 | UINTN NumberOfPages;\r | |
c7ef2ed2 | 27 | EFI_PHYSICAL_ADDRESS CryptedAddress;\r |
dc194ce3 | 28 | EFI_PHYSICAL_ADDRESS PlainTextAddress;\r |
f9d129e6 BS |
29 | } MAP_INFO;\r |
30 | \r | |
58e68140 LE |
31 | #define COMMON_BUFFER_SIG SIGNATURE_64 ('C', 'M', 'N', 'B', 'U', 'F', 'F', 'R')\r |
32 | \r | |
33 | //\r | |
34 | // The following structure enables Map() and Unmap() to perform in-place\r | |
35 | // decryption and encryption, respectively, for BusMasterCommonBuffer[64]\r | |
36 | // operations, without dynamic memory allocation or release.\r | |
37 | //\r | |
38 | // Both COMMON_BUFFER_HEADER and COMMON_BUFFER_HEADER.StashBuffer are allocated\r | |
39 | // by AllocateBuffer() and released by FreeBuffer().\r | |
40 | //\r | |
41 | #pragma pack (1)\r | |
42 | typedef struct {\r | |
43 | UINT64 Signature;\r | |
44 | \r | |
45 | //\r | |
46 | // Always allocated from EfiBootServicesData type memory, and always\r | |
47 | // encrypted.\r | |
48 | //\r | |
49 | VOID *StashBuffer;\r | |
50 | \r | |
51 | //\r | |
52 | // Followed by the actual common buffer, starting at the next page.\r | |
53 | //\r | |
54 | } COMMON_BUFFER_HEADER;\r | |
55 | #pragma pack ()\r | |
f9d129e6 BS |
56 | \r |
57 | /**\r | |
812568fb LE |
58 | Provides the controller-specific addresses required to access system memory\r |
59 | from a DMA bus master. On SEV guest, the DMA operations must be performed on\r | |
60 | shared buffer hence we allocate a bounce buffer to map the HostAddress to a\r | |
61 | DeviceAddress. The Encryption attribute is removed from the DeviceAddress\r | |
62 | buffer.\r | |
f9d129e6 BS |
63 | \r |
64 | @param This The protocol instance pointer.\r | |
65 | @param Operation Indicates if the bus master is going to read or\r | |
66 | write to system memory.\r | |
812568fb LE |
67 | @param HostAddress The system memory address to map to the PCI\r |
68 | controller.\r | |
f9d129e6 | 69 | @param NumberOfBytes On input the number of bytes to map. On output\r |
812568fb LE |
70 | the number of bytes that were mapped.\r |
71 | @param DeviceAddress The resulting map address for the bus master\r | |
72 | PCI controller to use to access the hosts\r | |
73 | HostAddress.\r | |
f9d129e6 BS |
74 | @param Mapping A resulting value to pass to Unmap().\r |
75 | \r | |
812568fb LE |
76 | @retval EFI_SUCCESS The range was mapped for the returned\r |
77 | NumberOfBytes.\r | |
78 | @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common\r | |
79 | buffer.\r | |
f9d129e6 | 80 | @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r |
812568fb LE |
81 | @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r |
82 | lack of resources.\r | |
83 | @retval EFI_DEVICE_ERROR The system hardware could not map the requested\r | |
84 | address.\r | |
f9d129e6 BS |
85 | \r |
86 | **/\r | |
87 | EFI_STATUS\r | |
88 | EFIAPI\r | |
89 | IoMmuMap (\r | |
90 | IN EDKII_IOMMU_PROTOCOL *This,\r | |
91 | IN EDKII_IOMMU_OPERATION Operation,\r | |
92 | IN VOID *HostAddress,\r | |
93 | IN OUT UINTN *NumberOfBytes,\r | |
94 | OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r | |
95 | OUT VOID **Mapping\r | |
96 | )\r | |
97 | {\r | |
98 | EFI_STATUS Status;\r | |
f9d129e6 | 99 | MAP_INFO *MapInfo;\r |
f9d129e6 | 100 | EFI_ALLOCATE_TYPE AllocateType;\r |
58e68140 LE |
101 | COMMON_BUFFER_HEADER *CommonBufferHeader;\r |
102 | VOID *DecryptionSource;\r | |
f9d129e6 BS |
103 | \r |
104 | if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL ||\r | |
105 | Mapping == NULL) {\r | |
106 | return EFI_INVALID_PARAMETER;\r | |
107 | }\r | |
108 | \r | |
f9d129e6 BS |
109 | //\r |
110 | // Allocate a MAP_INFO structure to remember the mapping when Unmap() is\r | |
111 | // called later.\r | |
112 | //\r | |
113 | MapInfo = AllocatePool (sizeof (MAP_INFO));\r | |
114 | if (MapInfo == NULL) {\r | |
e130229c LE |
115 | Status = EFI_OUT_OF_RESOURCES;\r |
116 | goto Failed;\r | |
f9d129e6 BS |
117 | }\r |
118 | \r | |
119 | //\r | |
e130229c | 120 | // Initialize the MAP_INFO structure, except the PlainTextAddress field\r |
f9d129e6 BS |
121 | //\r |
122 | MapInfo->Operation = Operation;\r | |
123 | MapInfo->NumberOfBytes = *NumberOfBytes;\r | |
124 | MapInfo->NumberOfPages = EFI_SIZE_TO_PAGES (MapInfo->NumberOfBytes);\r | |
e130229c | 125 | MapInfo->CryptedAddress = (UINTN)HostAddress;\r |
f9d129e6 BS |
126 | \r |
127 | //\r | |
e130229c | 128 | // In the switch statement below, we point "MapInfo->PlainTextAddress" to the\r |
58e68140 | 129 | // plaintext buffer, according to Operation. We also set "DecryptionSource".\r |
e130229c LE |
130 | //\r |
131 | MapInfo->PlainTextAddress = MAX_ADDRESS;\r | |
132 | AllocateType = AllocateAnyPages;\r | |
58e68140 | 133 | DecryptionSource = (VOID *)(UINTN)MapInfo->CryptedAddress;\r |
e130229c LE |
134 | switch (Operation) {\r |
135 | //\r | |
136 | // For BusMasterRead[64] and BusMasterWrite[64] operations, a bounce buffer\r | |
137 | // is necessary regardless of whether the original (crypted) buffer crosses\r | |
138 | // the 4GB limit or not -- we have to allocate a separate plaintext buffer.\r | |
139 | // The only variable is whether the plaintext buffer should be under 4GB.\r | |
f9d129e6 | 140 | //\r |
e130229c LE |
141 | case EdkiiIoMmuOperationBusMasterRead:\r |
142 | case EdkiiIoMmuOperationBusMasterWrite:\r | |
143 | MapInfo->PlainTextAddress = BASE_4GB - 1;\r | |
144 | AllocateType = AllocateMaxAddress;\r | |
145 | //\r | |
146 | // fall through\r | |
147 | //\r | |
148 | case EdkiiIoMmuOperationBusMasterRead64:\r | |
149 | case EdkiiIoMmuOperationBusMasterWrite64:\r | |
150 | //\r | |
151 | // Allocate the implicit plaintext bounce buffer.\r | |
152 | //\r | |
153 | Status = gBS->AllocatePages (\r | |
154 | AllocateType,\r | |
155 | EfiBootServicesData,\r | |
156 | MapInfo->NumberOfPages,\r | |
157 | &MapInfo->PlainTextAddress\r | |
158 | );\r | |
159 | if (EFI_ERROR (Status)) {\r | |
160 | goto FreeMapInfo;\r | |
161 | }\r | |
162 | break;\r | |
163 | \r | |
164 | //\r | |
58e68140 LE |
165 | // For BusMasterCommonBuffer[64] operations, a to-be-plaintext buffer and a\r |
166 | // stash buffer (for in-place decryption) have been allocated already, with\r | |
167 | // AllocateBuffer(). We only check whether the address of the to-be-plaintext\r | |
168 | // buffer is low enough for the requested operation.\r | |
e130229c LE |
169 | //\r |
170 | case EdkiiIoMmuOperationBusMasterCommonBuffer:\r | |
171 | if ((MapInfo->CryptedAddress > BASE_4GB) ||\r | |
172 | (EFI_PAGES_TO_SIZE (MapInfo->NumberOfPages) >\r | |
173 | BASE_4GB - MapInfo->CryptedAddress)) {\r | |
174 | //\r | |
175 | // CommonBuffer operations cannot be remapped. If the common buffer is\r | |
176 | // above 4GB, then it is not possible to generate a mapping, so return an\r | |
177 | // error.\r | |
178 | //\r | |
179 | Status = EFI_UNSUPPORTED;\r | |
180 | goto FreeMapInfo;\r | |
181 | }\r | |
182 | //\r | |
183 | // fall through\r | |
184 | //\r | |
185 | case EdkiiIoMmuOperationBusMasterCommonBuffer64:\r | |
186 | //\r | |
58e68140 | 187 | // The buffer at MapInfo->CryptedAddress comes from AllocateBuffer().\r |
e130229c LE |
188 | //\r |
189 | MapInfo->PlainTextAddress = MapInfo->CryptedAddress;\r | |
e130229c | 190 | //\r |
58e68140 LE |
191 | // Stash the crypted data.\r |
192 | //\r | |
193 | CommonBufferHeader = (COMMON_BUFFER_HEADER *)(\r | |
194 | (UINTN)MapInfo->CryptedAddress - EFI_PAGE_SIZE\r | |
195 | );\r | |
196 | ASSERT (CommonBufferHeader->Signature == COMMON_BUFFER_SIG);\r | |
197 | CopyMem (\r | |
198 | CommonBufferHeader->StashBuffer,\r | |
199 | (VOID *)(UINTN)MapInfo->CryptedAddress,\r | |
200 | MapInfo->NumberOfBytes\r | |
201 | );\r | |
202 | //\r | |
203 | // Point "DecryptionSource" to the stash buffer so that we decrypt\r | |
204 | // it to the original location, after the switch statement.\r | |
e130229c | 205 | //\r |
58e68140 LE |
206 | DecryptionSource = CommonBufferHeader->StashBuffer;\r |
207 | break;\r | |
e130229c LE |
208 | \r |
209 | default:\r | |
210 | //\r | |
211 | // Operation is invalid\r | |
212 | //\r | |
213 | Status = EFI_INVALID_PARAMETER;\r | |
214 | goto FreeMapInfo;\r | |
f9d129e6 BS |
215 | }\r |
216 | \r | |
217 | //\r | |
e130229c | 218 | // Clear the memory encryption mask on the plaintext buffer.\r |
f9d129e6 | 219 | //\r |
812568fb LE |
220 | Status = MemEncryptSevClearPageEncMask (\r |
221 | 0,\r | |
dc194ce3 | 222 | MapInfo->PlainTextAddress,\r |
812568fb LE |
223 | MapInfo->NumberOfPages,\r |
224 | TRUE\r | |
225 | );\r | |
f9d129e6 BS |
226 | ASSERT_EFI_ERROR(Status);\r |
227 | \r | |
228 | //\r | |
229 | // If this is a read operation from the Bus Master's point of view,\r | |
230 | // then copy the contents of the real buffer into the mapped buffer\r | |
231 | // so the Bus Master can read the contents of the real buffer.\r | |
232 | //\r | |
58e68140 LE |
233 | // For BusMasterCommonBuffer[64] operations, the CopyMem() below will decrypt\r |
234 | // the original data (from the stash buffer) back to the original location.\r | |
235 | //\r | |
f9d129e6 | 236 | if (Operation == EdkiiIoMmuOperationBusMasterRead ||\r |
58e68140 LE |
237 | Operation == EdkiiIoMmuOperationBusMasterRead64 ||\r |
238 | Operation == EdkiiIoMmuOperationBusMasterCommonBuffer ||\r | |
239 | Operation == EdkiiIoMmuOperationBusMasterCommonBuffer64) {\r | |
f9d129e6 | 240 | CopyMem (\r |
dc194ce3 | 241 | (VOID *) (UINTN) MapInfo->PlainTextAddress,\r |
58e68140 | 242 | DecryptionSource,\r |
f9d129e6 BS |
243 | MapInfo->NumberOfBytes\r |
244 | );\r | |
245 | }\r | |
246 | \r | |
247 | //\r | |
e130229c | 248 | // Populate output parameters.\r |
f9d129e6 | 249 | //\r |
dc194ce3 | 250 | *DeviceAddress = MapInfo->PlainTextAddress;\r |
f9d129e6 BS |
251 | *Mapping = MapInfo;\r |
252 | \r | |
812568fb LE |
253 | DEBUG ((\r |
254 | DEBUG_VERBOSE,\r | |
c7ef2ed2 | 255 | "%a PlainText 0x%Lx Crypted 0x%Lx Pages 0x%Lx Bytes 0x%Lx\n",\r |
812568fb | 256 | __FUNCTION__,\r |
dc194ce3 | 257 | MapInfo->PlainTextAddress,\r |
c7ef2ed2 | 258 | MapInfo->CryptedAddress,\r |
60aa3a0e LE |
259 | (UINT64)MapInfo->NumberOfPages,\r |
260 | (UINT64)MapInfo->NumberOfBytes\r | |
812568fb | 261 | ));\r |
f9d129e6 BS |
262 | \r |
263 | return EFI_SUCCESS;\r | |
e130229c LE |
264 | \r |
265 | FreeMapInfo:\r | |
266 | FreePool (MapInfo);\r | |
267 | \r | |
268 | Failed:\r | |
269 | *NumberOfBytes = 0;\r | |
270 | return Status;\r | |
f9d129e6 BS |
271 | }\r |
272 | \r | |
273 | /**\r | |
274 | Completes the Map() operation and releases any corresponding resources.\r | |
275 | \r | |
276 | @param This The protocol instance pointer.\r | |
277 | @param Mapping The mapping value returned from Map().\r | |
278 | \r | |
279 | @retval EFI_SUCCESS The range was unmapped.\r | |
812568fb LE |
280 | @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by\r |
281 | Map().\r | |
282 | @retval EFI_DEVICE_ERROR The data was not committed to the target system\r | |
283 | memory.\r | |
f9d129e6 BS |
284 | **/\r |
285 | EFI_STATUS\r | |
286 | EFIAPI\r | |
287 | IoMmuUnmap (\r | |
288 | IN EDKII_IOMMU_PROTOCOL *This,\r | |
289 | IN VOID *Mapping\r | |
290 | )\r | |
291 | {\r | |
292 | MAP_INFO *MapInfo;\r | |
293 | EFI_STATUS Status;\r | |
58e68140 LE |
294 | COMMON_BUFFER_HEADER *CommonBufferHeader;\r |
295 | VOID *EncryptionTarget;\r | |
f9d129e6 BS |
296 | \r |
297 | if (Mapping == NULL) {\r | |
298 | return EFI_INVALID_PARAMETER;\r | |
299 | }\r | |
300 | \r | |
58e68140 LE |
301 | MapInfo = (MAP_INFO *)Mapping;\r |
302 | \r | |
f9d129e6 | 303 | //\r |
58e68140 | 304 | // set CommonBufferHeader to suppress incorrect compiler/analyzer warnings\r |
f9d129e6 | 305 | //\r |
58e68140 | 306 | CommonBufferHeader = NULL;\r |
f9d129e6 BS |
307 | \r |
308 | //\r | |
58e68140 LE |
309 | // For BusMasterWrite[64] operations and BusMasterCommonBuffer[64] operations\r |
310 | // we have to encrypt the results, ultimately to the original place (i.e.,\r | |
311 | // "MapInfo->CryptedAddress").\r | |
f9d129e6 | 312 | //\r |
58e68140 LE |
313 | // For BusMasterCommonBuffer[64] operations however, this encryption has to\r |
314 | // land in-place, so divert the encryption to the stash buffer first.\r | |
315 | //\r | |
316 | EncryptionTarget = (VOID *)(UINTN)MapInfo->CryptedAddress;\r | |
317 | \r | |
318 | switch (MapInfo->Operation) {\r | |
319 | case EdkiiIoMmuOperationBusMasterCommonBuffer:\r | |
320 | case EdkiiIoMmuOperationBusMasterCommonBuffer64:\r | |
321 | ASSERT (MapInfo->PlainTextAddress == MapInfo->CryptedAddress);\r | |
322 | \r | |
323 | CommonBufferHeader = (COMMON_BUFFER_HEADER *)(\r | |
324 | (UINTN)MapInfo->PlainTextAddress - EFI_PAGE_SIZE\r | |
325 | );\r | |
326 | ASSERT (CommonBufferHeader->Signature == COMMON_BUFFER_SIG);\r | |
327 | EncryptionTarget = CommonBufferHeader->StashBuffer;\r | |
328 | //\r | |
329 | // fall through\r | |
330 | //\r | |
331 | \r | |
332 | case EdkiiIoMmuOperationBusMasterWrite:\r | |
333 | case EdkiiIoMmuOperationBusMasterWrite64:\r | |
f9d129e6 | 334 | CopyMem (\r |
58e68140 | 335 | EncryptionTarget,\r |
dc194ce3 | 336 | (VOID *) (UINTN) MapInfo->PlainTextAddress,\r |
f9d129e6 BS |
337 | MapInfo->NumberOfBytes\r |
338 | );\r | |
58e68140 LE |
339 | break;\r |
340 | \r | |
341 | default:\r | |
342 | //\r | |
343 | // nothing to encrypt after BusMasterRead[64] operations\r | |
344 | //\r | |
345 | break;\r | |
f9d129e6 BS |
346 | }\r |
347 | \r | |
812568fb LE |
348 | DEBUG ((\r |
349 | DEBUG_VERBOSE,\r | |
c7ef2ed2 | 350 | "%a PlainText 0x%Lx Crypted 0x%Lx Pages 0x%Lx Bytes 0x%Lx\n",\r |
812568fb | 351 | __FUNCTION__,\r |
dc194ce3 | 352 | MapInfo->PlainTextAddress,\r |
c7ef2ed2 | 353 | MapInfo->CryptedAddress,\r |
60aa3a0e LE |
354 | (UINT64)MapInfo->NumberOfPages,\r |
355 | (UINT64)MapInfo->NumberOfBytes\r | |
812568fb | 356 | ));\r |
58e68140 | 357 | \r |
f9d129e6 | 358 | //\r |
58e68140 LE |
359 | // Restore the memory encryption mask on the area we used to hold the\r |
360 | // plaintext.\r | |
f9d129e6 | 361 | //\r |
812568fb LE |
362 | Status = MemEncryptSevSetPageEncMask (\r |
363 | 0,\r | |
dc194ce3 | 364 | MapInfo->PlainTextAddress,\r |
812568fb LE |
365 | MapInfo->NumberOfPages,\r |
366 | TRUE\r | |
367 | );\r | |
f9d129e6 BS |
368 | ASSERT_EFI_ERROR(Status);\r |
369 | \r | |
370 | //\r | |
58e68140 LE |
371 | // For BusMasterCommonBuffer[64] operations, copy the stashed data to the\r |
372 | // original (now encrypted) location.\r | |
373 | //\r | |
374 | // For all other operations, fill the late bounce buffer (which existed as\r | |
375 | // plaintext at some point) with zeros, and then release it.\r | |
376 | //\r | |
377 | if (MapInfo->Operation == EdkiiIoMmuOperationBusMasterCommonBuffer ||\r | |
378 | MapInfo->Operation == EdkiiIoMmuOperationBusMasterCommonBuffer64) {\r | |
379 | CopyMem (\r | |
380 | (VOID *)(UINTN)MapInfo->CryptedAddress,\r | |
381 | CommonBufferHeader->StashBuffer,\r | |
382 | MapInfo->NumberOfBytes\r | |
383 | );\r | |
384 | } else {\r | |
385 | ZeroMem (\r | |
386 | (VOID *)(UINTN)MapInfo->PlainTextAddress,\r | |
387 | EFI_PAGES_TO_SIZE (MapInfo->NumberOfPages)\r | |
388 | );\r | |
389 | gBS->FreePages (MapInfo->PlainTextAddress, MapInfo->NumberOfPages);\r | |
390 | }\r | |
391 | \r | |
392 | //\r | |
393 | // Free the MAP_INFO structure.\r | |
f9d129e6 | 394 | //\r |
f9d129e6 BS |
395 | FreePool (Mapping);\r |
396 | return EFI_SUCCESS;\r | |
397 | }\r | |
398 | \r | |
399 | /**\r | |
400 | Allocates pages that are suitable for an OperationBusMasterCommonBuffer or\r | |
401 | OperationBusMasterCommonBuffer64 mapping.\r | |
402 | \r | |
403 | @param This The protocol instance pointer.\r | |
404 | @param Type This parameter is not used and must be ignored.\r | |
812568fb LE |
405 | @param MemoryType The type of memory to allocate,\r |
406 | EfiBootServicesData or EfiRuntimeServicesData.\r | |
f9d129e6 | 407 | @param Pages The number of pages to allocate.\r |
812568fb LE |
408 | @param HostAddress A pointer to store the base system memory\r |
409 | address of the allocated range.\r | |
410 | @param Attributes The requested bit mask of attributes for the\r | |
411 | allocated range.\r | |
f9d129e6 BS |
412 | \r |
413 | @retval EFI_SUCCESS The requested memory pages were allocated.\r | |
812568fb LE |
414 | @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal\r |
415 | attribute bits are MEMORY_WRITE_COMBINE and\r | |
416 | MEMORY_CACHED.\r | |
f9d129e6 BS |
417 | @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r |
418 | @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r | |
419 | \r | |
420 | **/\r | |
421 | EFI_STATUS\r | |
422 | EFIAPI\r | |
423 | IoMmuAllocateBuffer (\r | |
424 | IN EDKII_IOMMU_PROTOCOL *This,\r | |
425 | IN EFI_ALLOCATE_TYPE Type,\r | |
426 | IN EFI_MEMORY_TYPE MemoryType,\r | |
427 | IN UINTN Pages,\r | |
428 | IN OUT VOID **HostAddress,\r | |
429 | IN UINT64 Attributes\r | |
430 | )\r | |
431 | {\r | |
432 | EFI_STATUS Status;\r | |
433 | EFI_PHYSICAL_ADDRESS PhysicalAddress;\r | |
58e68140 LE |
434 | VOID *StashBuffer;\r |
435 | UINTN CommonBufferPages;\r | |
436 | COMMON_BUFFER_HEADER *CommonBufferHeader;\r | |
f9d129e6 BS |
437 | \r |
438 | //\r | |
439 | // Validate Attributes\r | |
440 | //\r | |
441 | if ((Attributes & EDKII_IOMMU_ATTRIBUTE_INVALID_FOR_ALLOCATE_BUFFER) != 0) {\r | |
442 | return EFI_UNSUPPORTED;\r | |
443 | }\r | |
444 | \r | |
445 | //\r | |
446 | // Check for invalid inputs\r | |
447 | //\r | |
448 | if (HostAddress == NULL) {\r | |
449 | return EFI_INVALID_PARAMETER;\r | |
450 | }\r | |
451 | \r | |
452 | //\r | |
453 | // The only valid memory types are EfiBootServicesData and\r | |
454 | // EfiRuntimeServicesData\r | |
455 | //\r | |
456 | if (MemoryType != EfiBootServicesData &&\r | |
457 | MemoryType != EfiRuntimeServicesData) {\r | |
458 | return EFI_INVALID_PARAMETER;\r | |
459 | }\r | |
460 | \r | |
58e68140 LE |
461 | //\r |
462 | // We'll need a header page for the COMMON_BUFFER_HEADER structure.\r | |
463 | //\r | |
464 | if (Pages > MAX_UINTN - 1) {\r | |
465 | return EFI_OUT_OF_RESOURCES;\r | |
466 | }\r | |
467 | CommonBufferPages = Pages + 1;\r | |
468 | \r | |
469 | //\r | |
470 | // Allocate the stash in EfiBootServicesData type memory.\r | |
471 | //\r | |
472 | // Map() will temporarily save encrypted data in the stash for\r | |
473 | // BusMasterCommonBuffer[64] operations, so the data can be decrypted to the\r | |
474 | // original location.\r | |
475 | //\r | |
476 | // Unmap() will temporarily save plaintext data in the stash for\r | |
477 | // BusMasterCommonBuffer[64] operations, so the data can be encrypted to the\r | |
478 | // original location.\r | |
479 | //\r | |
480 | // StashBuffer always resides in encrypted memory.\r | |
481 | //\r | |
482 | StashBuffer = AllocatePages (Pages);\r | |
483 | if (StashBuffer == NULL) {\r | |
484 | return EFI_OUT_OF_RESOURCES;\r | |
485 | }\r | |
486 | \r | |
f9d129e6 BS |
487 | PhysicalAddress = (UINTN)-1;\r |
488 | if ((Attributes & EDKII_IOMMU_ATTRIBUTE_DUAL_ADDRESS_CYCLE) == 0) {\r | |
489 | //\r | |
490 | // Limit allocations to memory below 4GB\r | |
491 | //\r | |
492 | PhysicalAddress = SIZE_4GB - 1;\r | |
493 | }\r | |
494 | Status = gBS->AllocatePages (\r | |
495 | AllocateMaxAddress,\r | |
496 | MemoryType,\r | |
58e68140 | 497 | CommonBufferPages,\r |
f9d129e6 BS |
498 | &PhysicalAddress\r |
499 | );\r | |
58e68140 LE |
500 | if (EFI_ERROR (Status)) {\r |
501 | goto FreeStashBuffer;\r | |
f9d129e6 BS |
502 | }\r |
503 | \r | |
58e68140 LE |
504 | CommonBufferHeader = (VOID *)(UINTN)PhysicalAddress;\r |
505 | PhysicalAddress += EFI_PAGE_SIZE;\r | |
506 | \r | |
507 | CommonBufferHeader->Signature = COMMON_BUFFER_SIG;\r | |
508 | CommonBufferHeader->StashBuffer = StashBuffer;\r | |
509 | \r | |
510 | *HostAddress = (VOID *)(UINTN)PhysicalAddress;\r | |
511 | \r | |
812568fb LE |
512 | DEBUG ((\r |
513 | DEBUG_VERBOSE,\r | |
514 | "%a Address 0x%Lx Pages 0x%Lx\n",\r | |
515 | __FUNCTION__,\r | |
516 | PhysicalAddress,\r | |
60aa3a0e | 517 | (UINT64)Pages\r |
812568fb | 518 | ));\r |
58e68140 LE |
519 | return EFI_SUCCESS;\r |
520 | \r | |
521 | FreeStashBuffer:\r | |
522 | FreePages (StashBuffer, Pages);\r | |
f9d129e6 BS |
523 | return Status;\r |
524 | }\r | |
525 | \r | |
526 | /**\r | |
527 | Frees memory that was allocated with AllocateBuffer().\r | |
528 | \r | |
529 | @param This The protocol instance pointer.\r | |
530 | @param Pages The number of pages to free.\r | |
812568fb LE |
531 | @param HostAddress The base system memory address of the allocated\r |
532 | range.\r | |
f9d129e6 BS |
533 | \r |
534 | @retval EFI_SUCCESS The requested memory pages were freed.\r | |
812568fb LE |
535 | @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and\r |
536 | Pages was not allocated with AllocateBuffer().\r | |
f9d129e6 BS |
537 | \r |
538 | **/\r | |
539 | EFI_STATUS\r | |
540 | EFIAPI\r | |
541 | IoMmuFreeBuffer (\r | |
542 | IN EDKII_IOMMU_PROTOCOL *This,\r | |
543 | IN UINTN Pages,\r | |
544 | IN VOID *HostAddress\r | |
545 | )\r | |
546 | {\r | |
58e68140 LE |
547 | UINTN CommonBufferPages;\r |
548 | COMMON_BUFFER_HEADER *CommonBufferHeader;\r | |
549 | \r | |
550 | CommonBufferPages = Pages + 1;\r | |
551 | CommonBufferHeader = (COMMON_BUFFER_HEADER *)(\r | |
552 | (UINTN)HostAddress - EFI_PAGE_SIZE\r | |
553 | );\r | |
f9d129e6 BS |
554 | \r |
555 | //\r | |
58e68140 | 556 | // Check the signature.\r |
f9d129e6 | 557 | //\r |
58e68140 LE |
558 | ASSERT (CommonBufferHeader->Signature == COMMON_BUFFER_SIG);\r |
559 | if (CommonBufferHeader->Signature != COMMON_BUFFER_SIG) {\r | |
560 | return EFI_INVALID_PARAMETER;\r | |
561 | }\r | |
562 | \r | |
563 | //\r | |
564 | // Free the stash buffer. This buffer was always encrypted, so no need to\r | |
565 | // zero it.\r | |
566 | //\r | |
567 | FreePages (CommonBufferHeader->StashBuffer, Pages);\r | |
f9d129e6 | 568 | \r |
812568fb LE |
569 | DEBUG ((\r |
570 | DEBUG_VERBOSE,\r | |
571 | "%a Address 0x%Lx Pages 0x%Lx\n",\r | |
572 | __FUNCTION__,\r | |
60aa3a0e LE |
573 | (UINT64)(UINTN)HostAddress,\r |
574 | (UINT64)Pages\r | |
812568fb | 575 | ));\r |
58e68140 LE |
576 | \r |
577 | //\r | |
578 | // Release the common buffer itself. Unmap() has re-encrypted it in-place, so\r | |
579 | // no need to zero it.\r | |
580 | //\r | |
581 | return gBS->FreePages ((UINTN)CommonBufferHeader, CommonBufferPages);\r | |
f9d129e6 BS |
582 | }\r |
583 | \r | |
584 | \r | |
585 | /**\r | |
586 | Set IOMMU attribute for a system memory.\r | |
587 | \r | |
588 | If the IOMMU protocol exists, the system memory cannot be used\r | |
589 | for DMA by default.\r | |
590 | \r | |
591 | When a device requests a DMA access for a system memory,\r | |
592 | the device driver need use SetAttribute() to update the IOMMU\r | |
593 | attribute to request DMA access (read and/or write).\r | |
594 | \r | |
595 | The DeviceHandle is used to identify which device submits the request.\r | |
812568fb LE |
596 | The IOMMU implementation need translate the device path to an IOMMU device\r |
597 | ID, and set IOMMU hardware register accordingly.\r | |
f9d129e6 BS |
598 | 1) DeviceHandle can be a standard PCI device.\r |
599 | The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.\r | |
600 | The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.\r | |
812568fb LE |
601 | The memory for BusMasterCommonBuffer need set\r |
602 | EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.\r | |
603 | After the memory is used, the memory need set 0 to keep it being\r | |
604 | protected.\r | |
f9d129e6 | 605 | 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).\r |
812568fb LE |
606 | The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or\r |
607 | EDKII_IOMMU_ACCESS_WRITE.\r | |
f9d129e6 BS |
608 | \r |
609 | @param[in] This The protocol instance pointer.\r | |
812568fb LE |
610 | @param[in] DeviceHandle The device who initiates the DMA access\r |
611 | request.\r | |
f9d129e6 BS |
612 | @param[in] Mapping The mapping value returned from Map().\r |
613 | @param[in] IoMmuAccess The IOMMU access.\r | |
614 | \r | |
812568fb LE |
615 | @retval EFI_SUCCESS The IoMmuAccess is set for the memory range\r |
616 | specified by DeviceAddress and Length.\r | |
f9d129e6 | 617 | @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.\r |
812568fb LE |
618 | @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by\r |
619 | Map().\r | |
620 | @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination\r | |
621 | of access.\r | |
f9d129e6 | 622 | @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.\r |
812568fb LE |
623 | @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported\r |
624 | by the IOMMU.\r | |
625 | @retval EFI_UNSUPPORTED The IOMMU does not support the memory range\r | |
626 | specified by Mapping.\r | |
627 | @retval EFI_OUT_OF_RESOURCES There are not enough resources available to\r | |
628 | modify the IOMMU access.\r | |
629 | @retval EFI_DEVICE_ERROR The IOMMU device reported an error while\r | |
630 | attempting the operation.\r | |
f9d129e6 BS |
631 | \r |
632 | **/\r | |
633 | EFI_STATUS\r | |
634 | EFIAPI\r | |
635 | IoMmuSetAttribute (\r | |
636 | IN EDKII_IOMMU_PROTOCOL *This,\r | |
637 | IN EFI_HANDLE DeviceHandle,\r | |
638 | IN VOID *Mapping,\r | |
639 | IN UINT64 IoMmuAccess\r | |
640 | )\r | |
641 | {\r | |
642 | return EFI_UNSUPPORTED;\r | |
643 | }\r | |
644 | \r | |
645 | EDKII_IOMMU_PROTOCOL mAmdSev = {\r | |
646 | EDKII_IOMMU_PROTOCOL_REVISION,\r | |
647 | IoMmuSetAttribute,\r | |
648 | IoMmuMap,\r | |
649 | IoMmuUnmap,\r | |
650 | IoMmuAllocateBuffer,\r | |
651 | IoMmuFreeBuffer,\r | |
652 | };\r | |
653 | \r | |
654 | /**\r | |
655 | Initialize Iommu Protocol.\r | |
656 | \r | |
657 | **/\r | |
db125079 | 658 | EFI_STATUS\r |
f9d129e6 BS |
659 | EFIAPI\r |
660 | AmdSevInstallIoMmuProtocol (\r | |
661 | VOID\r | |
662 | )\r | |
663 | {\r | |
664 | EFI_STATUS Status;\r | |
665 | EFI_HANDLE Handle;\r | |
666 | \r | |
667 | Handle = NULL;\r | |
668 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
669 | &Handle,\r | |
670 | &gEdkiiIoMmuProtocolGuid, &mAmdSev,\r | |
671 | NULL\r | |
672 | );\r | |
db125079 | 673 | return Status;\r |
f9d129e6 | 674 | }\r |