]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/IoMmuDxe/AmdSevIoMmu.c
OvmfPkg: Apply uncrustify changes
[mirror_edk2.git] / OvmfPkg / IoMmuDxe / AmdSevIoMmu.c
1 /** @file
2
3 The protocol provides support to allocate, free, map and umap a DMA buffer
4 for bus master (e.g PciHostBridge). When SEV is enabled, the DMA operations
5 must be performed on unencrypted buffer hence we use a bounce buffer to map
6 the guest buffer into an unencrypted DMA buffer.
7
8 Copyright (c) 2017, AMD Inc. All rights reserved.<BR>
9 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
10
11 SPDX-License-Identifier: BSD-2-Clause-Patent
12
13 **/
14
15 #include "AmdSevIoMmu.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 } MAP_INFO;
28
29 //
30 // List of the MAP_INFO structures that have been set up by IoMmuMap() and not
31 // yet torn down by IoMmuUnmap(). The list represents the full set of mappings
32 // currently in effect.
33 //
34 STATIC LIST_ENTRY mMapInfos = INITIALIZE_LIST_HEAD_VARIABLE (mMapInfos);
35
36 #define COMMON_BUFFER_SIG SIGNATURE_64 ('C', 'M', 'N', 'B', 'U', 'F', 'F', 'R')
37
38 //
39 // ASCII names for EDKII_IOMMU_OPERATION constants, for debug logging.
40 //
41 STATIC CONST CHAR8 *CONST
42 mBusMasterOperationName[EdkiiIoMmuOperationMaximum] = {
43 "Read",
44 "Write",
45 "CommonBuffer",
46 "Read64",
47 "Write64",
48 "CommonBuffer64"
49 };
50
51 //
52 // The following structure enables Map() and Unmap() to perform in-place
53 // decryption and encryption, respectively, for BusMasterCommonBuffer[64]
54 // operations, without dynamic memory allocation or release.
55 //
56 // Both COMMON_BUFFER_HEADER and COMMON_BUFFER_HEADER.StashBuffer are allocated
57 // by AllocateBuffer() and released by FreeBuffer().
58 //
59 #pragma pack (1)
60 typedef struct {
61 UINT64 Signature;
62
63 //
64 // Always allocated from EfiBootServicesData type memory, and always
65 // encrypted.
66 //
67 VOID *StashBuffer;
68
69 //
70 // Followed by the actual common buffer, starting at the next page.
71 //
72 } COMMON_BUFFER_HEADER;
73 #pragma pack ()
74
75 /**
76 Provides the controller-specific addresses required to access system memory
77 from a DMA bus master. On SEV guest, the DMA operations must be performed on
78 shared buffer hence we allocate a bounce buffer to map the HostAddress to a
79 DeviceAddress. The Encryption attribute is removed from the DeviceAddress
80 buffer.
81
82 @param This The protocol instance pointer.
83 @param Operation Indicates if the bus master is going to read or
84 write to system memory.
85 @param HostAddress The system memory address to map to the PCI
86 controller.
87 @param NumberOfBytes On input the number of bytes to map. On output
88 the number of bytes that were mapped.
89 @param DeviceAddress The resulting map address for the bus master
90 PCI controller to use to access the hosts
91 HostAddress.
92 @param Mapping A resulting value to pass to Unmap().
93
94 @retval EFI_SUCCESS The range was mapped for the returned
95 NumberOfBytes.
96 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common
97 buffer.
98 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
99 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
100 lack of resources.
101 @retval EFI_DEVICE_ERROR The system hardware could not map the requested
102 address.
103
104 **/
105 EFI_STATUS
106 EFIAPI
107 IoMmuMap (
108 IN EDKII_IOMMU_PROTOCOL *This,
109 IN EDKII_IOMMU_OPERATION Operation,
110 IN VOID *HostAddress,
111 IN OUT UINTN *NumberOfBytes,
112 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
113 OUT VOID **Mapping
114 )
115 {
116 EFI_STATUS Status;
117 MAP_INFO *MapInfo;
118 EFI_ALLOCATE_TYPE AllocateType;
119 COMMON_BUFFER_HEADER *CommonBufferHeader;
120 VOID *DecryptionSource;
121
122 DEBUG ((
123 DEBUG_VERBOSE,
124 "%a: Operation=%a Host=0x%p Bytes=0x%Lx\n",
125 __FUNCTION__,
126 ((Operation >= 0 &&
127 Operation < ARRAY_SIZE (mBusMasterOperationName)) ?
128 mBusMasterOperationName[Operation] :
129 "Invalid"),
130 HostAddress,
131 (UINT64)((NumberOfBytes == NULL) ? 0 : *NumberOfBytes)
132 ));
133
134 if ((HostAddress == NULL) || (NumberOfBytes == NULL) || (DeviceAddress == NULL) ||
135 (Mapping == NULL))
136 {
137 return EFI_INVALID_PARAMETER;
138 }
139
140 //
141 // Allocate a MAP_INFO structure to remember the mapping when Unmap() is
142 // called later.
143 //
144 MapInfo = AllocatePool (sizeof (MAP_INFO));
145 if (MapInfo == NULL) {
146 Status = EFI_OUT_OF_RESOURCES;
147 goto Failed;
148 }
149
150 //
151 // Initialize the MAP_INFO structure, except the PlainTextAddress field
152 //
153 ZeroMem (&MapInfo->Link, sizeof MapInfo->Link);
154 MapInfo->Signature = MAP_INFO_SIG;
155 MapInfo->Operation = Operation;
156 MapInfo->NumberOfBytes = *NumberOfBytes;
157 MapInfo->NumberOfPages = EFI_SIZE_TO_PAGES (MapInfo->NumberOfBytes);
158 MapInfo->CryptedAddress = (UINTN)HostAddress;
159
160 //
161 // In the switch statement below, we point "MapInfo->PlainTextAddress" to the
162 // plaintext buffer, according to Operation. We also set "DecryptionSource".
163 //
164 MapInfo->PlainTextAddress = MAX_ADDRESS;
165 AllocateType = AllocateAnyPages;
166 DecryptionSource = (VOID *)(UINTN)MapInfo->CryptedAddress;
167 switch (Operation) {
168 //
169 // For BusMasterRead[64] and BusMasterWrite[64] operations, a bounce buffer
170 // is necessary regardless of whether the original (crypted) buffer crosses
171 // the 4GB limit or not -- we have to allocate a separate plaintext buffer.
172 // The only variable is whether the plaintext buffer should be under 4GB.
173 //
174 case EdkiiIoMmuOperationBusMasterRead:
175 case EdkiiIoMmuOperationBusMasterWrite:
176 MapInfo->PlainTextAddress = BASE_4GB - 1;
177 AllocateType = AllocateMaxAddress;
178 //
179 // fall through
180 //
181 case EdkiiIoMmuOperationBusMasterRead64:
182 case EdkiiIoMmuOperationBusMasterWrite64:
183 //
184 // Allocate the implicit plaintext bounce buffer.
185 //
186 Status = gBS->AllocatePages (
187 AllocateType,
188 EfiBootServicesData,
189 MapInfo->NumberOfPages,
190 &MapInfo->PlainTextAddress
191 );
192 if (EFI_ERROR (Status)) {
193 goto FreeMapInfo;
194 }
195
196 break;
197
198 //
199 // For BusMasterCommonBuffer[64] operations, a to-be-plaintext buffer and a
200 // stash buffer (for in-place decryption) have been allocated already, with
201 // AllocateBuffer(). We only check whether the address of the to-be-plaintext
202 // buffer is low enough for the requested operation.
203 //
204 case EdkiiIoMmuOperationBusMasterCommonBuffer:
205 if ((MapInfo->CryptedAddress > BASE_4GB) ||
206 (EFI_PAGES_TO_SIZE (MapInfo->NumberOfPages) >
207 BASE_4GB - MapInfo->CryptedAddress))
208 {
209 //
210 // CommonBuffer operations cannot be remapped. If the common buffer is
211 // above 4GB, then it is not possible to generate a mapping, so return an
212 // error.
213 //
214 Status = EFI_UNSUPPORTED;
215 goto FreeMapInfo;
216 }
217
218 //
219 // fall through
220 //
221 case EdkiiIoMmuOperationBusMasterCommonBuffer64:
222 //
223 // The buffer at MapInfo->CryptedAddress comes from AllocateBuffer().
224 //
225 MapInfo->PlainTextAddress = MapInfo->CryptedAddress;
226 //
227 // Stash the crypted data.
228 //
229 CommonBufferHeader = (COMMON_BUFFER_HEADER *)(
230 (UINTN)MapInfo->CryptedAddress - EFI_PAGE_SIZE
231 );
232 ASSERT (CommonBufferHeader->Signature == COMMON_BUFFER_SIG);
233 CopyMem (
234 CommonBufferHeader->StashBuffer,
235 (VOID *)(UINTN)MapInfo->CryptedAddress,
236 MapInfo->NumberOfBytes
237 );
238 //
239 // Point "DecryptionSource" to the stash buffer so that we decrypt
240 // it to the original location, after the switch statement.
241 //
242 DecryptionSource = CommonBufferHeader->StashBuffer;
243 break;
244
245 default:
246 //
247 // Operation is invalid
248 //
249 Status = EFI_INVALID_PARAMETER;
250 goto FreeMapInfo;
251 }
252
253 //
254 // Clear the memory encryption mask on the plaintext buffer.
255 //
256 Status = MemEncryptSevClearPageEncMask (
257 0,
258 MapInfo->PlainTextAddress,
259 MapInfo->NumberOfPages
260 );
261 ASSERT_EFI_ERROR (Status);
262 if (EFI_ERROR (Status)) {
263 CpuDeadLoop ();
264 }
265
266 //
267 // If this is a read operation from the Bus Master's point of view,
268 // then copy the contents of the real buffer into the mapped buffer
269 // so the Bus Master can read the contents of the real buffer.
270 //
271 // For BusMasterCommonBuffer[64] operations, the CopyMem() below will decrypt
272 // the original data (from the stash buffer) back to the original location.
273 //
274 if ((Operation == EdkiiIoMmuOperationBusMasterRead) ||
275 (Operation == EdkiiIoMmuOperationBusMasterRead64) ||
276 (Operation == EdkiiIoMmuOperationBusMasterCommonBuffer) ||
277 (Operation == EdkiiIoMmuOperationBusMasterCommonBuffer64))
278 {
279 CopyMem (
280 (VOID *)(UINTN)MapInfo->PlainTextAddress,
281 DecryptionSource,
282 MapInfo->NumberOfBytes
283 );
284 }
285
286 //
287 // Track all MAP_INFO structures.
288 //
289 InsertHeadList (&mMapInfos, &MapInfo->Link);
290 //
291 // Populate output parameters.
292 //
293 *DeviceAddress = MapInfo->PlainTextAddress;
294 *Mapping = MapInfo;
295
296 DEBUG ((
297 DEBUG_VERBOSE,
298 "%a: Mapping=0x%p Device(PlainText)=0x%Lx Crypted=0x%Lx Pages=0x%Lx\n",
299 __FUNCTION__,
300 MapInfo,
301 MapInfo->PlainTextAddress,
302 MapInfo->CryptedAddress,
303 (UINT64)MapInfo->NumberOfPages
304 ));
305
306 return EFI_SUCCESS;
307
308 FreeMapInfo:
309 FreePool (MapInfo);
310
311 Failed:
312 *NumberOfBytes = 0;
313 return Status;
314 }
315
316 /**
317 Completes the Map() operation and releases any corresponding resources.
318
319 This is an internal worker function that only extends the Map() API with
320 the MemoryMapLocked parameter.
321
322 @param This The protocol instance pointer.
323 @param Mapping The mapping value returned from Map().
324 @param MemoryMapLocked The function is executing on the stack of
325 gBS->ExitBootServices(); changes to the UEFI
326 memory map are forbidden.
327
328 @retval EFI_SUCCESS The range was unmapped.
329 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by
330 Map().
331 @retval EFI_DEVICE_ERROR The data was not committed to the target system
332 memory.
333 **/
334 STATIC
335 EFI_STATUS
336 EFIAPI
337 IoMmuUnmapWorker (
338 IN EDKII_IOMMU_PROTOCOL *This,
339 IN VOID *Mapping,
340 IN BOOLEAN MemoryMapLocked
341 )
342 {
343 MAP_INFO *MapInfo;
344 EFI_STATUS Status;
345 COMMON_BUFFER_HEADER *CommonBufferHeader;
346 VOID *EncryptionTarget;
347
348 DEBUG ((
349 DEBUG_VERBOSE,
350 "%a: Mapping=0x%p MemoryMapLocked=%d\n",
351 __FUNCTION__,
352 Mapping,
353 MemoryMapLocked
354 ));
355
356 if (Mapping == NULL) {
357 return EFI_INVALID_PARAMETER;
358 }
359
360 MapInfo = (MAP_INFO *)Mapping;
361
362 //
363 // set CommonBufferHeader to suppress incorrect compiler/analyzer warnings
364 //
365 CommonBufferHeader = NULL;
366
367 //
368 // For BusMasterWrite[64] operations and BusMasterCommonBuffer[64] operations
369 // we have to encrypt the results, ultimately to the original place (i.e.,
370 // "MapInfo->CryptedAddress").
371 //
372 // For BusMasterCommonBuffer[64] operations however, this encryption has to
373 // land in-place, so divert the encryption to the stash buffer first.
374 //
375 EncryptionTarget = (VOID *)(UINTN)MapInfo->CryptedAddress;
376
377 switch (MapInfo->Operation) {
378 case EdkiiIoMmuOperationBusMasterCommonBuffer:
379 case EdkiiIoMmuOperationBusMasterCommonBuffer64:
380 ASSERT (MapInfo->PlainTextAddress == MapInfo->CryptedAddress);
381
382 CommonBufferHeader = (COMMON_BUFFER_HEADER *)(
383 (UINTN)MapInfo->PlainTextAddress - EFI_PAGE_SIZE
384 );
385 ASSERT (CommonBufferHeader->Signature == COMMON_BUFFER_SIG);
386 EncryptionTarget = CommonBufferHeader->StashBuffer;
387 //
388 // fall through
389 //
390
391 case EdkiiIoMmuOperationBusMasterWrite:
392 case EdkiiIoMmuOperationBusMasterWrite64:
393 CopyMem (
394 EncryptionTarget,
395 (VOID *)(UINTN)MapInfo->PlainTextAddress,
396 MapInfo->NumberOfBytes
397 );
398 break;
399
400 default:
401 //
402 // nothing to encrypt after BusMasterRead[64] operations
403 //
404 break;
405 }
406
407 //
408 // Restore the memory encryption mask on the area we used to hold the
409 // plaintext.
410 //
411 Status = MemEncryptSevSetPageEncMask (
412 0,
413 MapInfo->PlainTextAddress,
414 MapInfo->NumberOfPages
415 );
416 ASSERT_EFI_ERROR (Status);
417 if (EFI_ERROR (Status)) {
418 CpuDeadLoop ();
419 }
420
421 //
422 // For BusMasterCommonBuffer[64] operations, copy the stashed data to the
423 // original (now encrypted) location.
424 //
425 // For all other operations, fill the late bounce buffer (which existed as
426 // plaintext at some point) with zeros, and then release it (unless the UEFI
427 // memory map is locked).
428 //
429 if ((MapInfo->Operation == EdkiiIoMmuOperationBusMasterCommonBuffer) ||
430 (MapInfo->Operation == EdkiiIoMmuOperationBusMasterCommonBuffer64))
431 {
432 CopyMem (
433 (VOID *)(UINTN)MapInfo->CryptedAddress,
434 CommonBufferHeader->StashBuffer,
435 MapInfo->NumberOfBytes
436 );
437 } else {
438 ZeroMem (
439 (VOID *)(UINTN)MapInfo->PlainTextAddress,
440 EFI_PAGES_TO_SIZE (MapInfo->NumberOfPages)
441 );
442 if (!MemoryMapLocked) {
443 gBS->FreePages (MapInfo->PlainTextAddress, MapInfo->NumberOfPages);
444 }
445 }
446
447 //
448 // Forget the MAP_INFO structure, then free it (unless the UEFI memory map is
449 // locked).
450 //
451 RemoveEntryList (&MapInfo->Link);
452 if (!MemoryMapLocked) {
453 FreePool (MapInfo);
454 }
455
456 return EFI_SUCCESS;
457 }
458
459 /**
460 Completes the Map() operation and releases any corresponding resources.
461
462 @param This The protocol instance pointer.
463 @param Mapping The mapping value returned from Map().
464
465 @retval EFI_SUCCESS The range was unmapped.
466 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by
467 Map().
468 @retval EFI_DEVICE_ERROR The data was not committed to the target system
469 memory.
470 **/
471 EFI_STATUS
472 EFIAPI
473 IoMmuUnmap (
474 IN EDKII_IOMMU_PROTOCOL *This,
475 IN VOID *Mapping
476 )
477 {
478 return IoMmuUnmapWorker (
479 This,
480 Mapping,
481 FALSE // MemoryMapLocked
482 );
483 }
484
485 /**
486 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
487 OperationBusMasterCommonBuffer64 mapping.
488
489 @param This The protocol instance pointer.
490 @param Type This parameter is not used and must be ignored.
491 @param MemoryType The type of memory to allocate,
492 EfiBootServicesData or EfiRuntimeServicesData.
493 @param Pages The number of pages to allocate.
494 @param HostAddress A pointer to store the base system memory
495 address of the allocated range.
496 @param Attributes The requested bit mask of attributes for the
497 allocated range.
498
499 @retval EFI_SUCCESS The requested memory pages were allocated.
500 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal
501 attribute bits are MEMORY_WRITE_COMBINE and
502 MEMORY_CACHED.
503 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
504 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
505
506 **/
507 EFI_STATUS
508 EFIAPI
509 IoMmuAllocateBuffer (
510 IN EDKII_IOMMU_PROTOCOL *This,
511 IN EFI_ALLOCATE_TYPE Type,
512 IN EFI_MEMORY_TYPE MemoryType,
513 IN UINTN Pages,
514 IN OUT VOID **HostAddress,
515 IN UINT64 Attributes
516 )
517 {
518 EFI_STATUS Status;
519 EFI_PHYSICAL_ADDRESS PhysicalAddress;
520 VOID *StashBuffer;
521 UINTN CommonBufferPages;
522 COMMON_BUFFER_HEADER *CommonBufferHeader;
523
524 DEBUG ((
525 DEBUG_VERBOSE,
526 "%a: MemoryType=%u Pages=0x%Lx Attributes=0x%Lx\n",
527 __FUNCTION__,
528 (UINT32)MemoryType,
529 (UINT64)Pages,
530 Attributes
531 ));
532
533 //
534 // Validate Attributes
535 //
536 if ((Attributes & EDKII_IOMMU_ATTRIBUTE_INVALID_FOR_ALLOCATE_BUFFER) != 0) {
537 return EFI_UNSUPPORTED;
538 }
539
540 //
541 // Check for invalid inputs
542 //
543 if (HostAddress == NULL) {
544 return EFI_INVALID_PARAMETER;
545 }
546
547 //
548 // The only valid memory types are EfiBootServicesData and
549 // EfiRuntimeServicesData
550 //
551 if ((MemoryType != EfiBootServicesData) &&
552 (MemoryType != EfiRuntimeServicesData))
553 {
554 return EFI_INVALID_PARAMETER;
555 }
556
557 //
558 // We'll need a header page for the COMMON_BUFFER_HEADER structure.
559 //
560 if (Pages > MAX_UINTN - 1) {
561 return EFI_OUT_OF_RESOURCES;
562 }
563
564 CommonBufferPages = Pages + 1;
565
566 //
567 // Allocate the stash in EfiBootServicesData type memory.
568 //
569 // Map() will temporarily save encrypted data in the stash for
570 // BusMasterCommonBuffer[64] operations, so the data can be decrypted to the
571 // original location.
572 //
573 // Unmap() will temporarily save plaintext data in the stash for
574 // BusMasterCommonBuffer[64] operations, so the data can be encrypted to the
575 // original location.
576 //
577 // StashBuffer always resides in encrypted memory.
578 //
579 StashBuffer = AllocatePages (Pages);
580 if (StashBuffer == NULL) {
581 return EFI_OUT_OF_RESOURCES;
582 }
583
584 PhysicalAddress = (UINTN)-1;
585 if ((Attributes & EDKII_IOMMU_ATTRIBUTE_DUAL_ADDRESS_CYCLE) == 0) {
586 //
587 // Limit allocations to memory below 4GB
588 //
589 PhysicalAddress = SIZE_4GB - 1;
590 }
591
592 Status = gBS->AllocatePages (
593 AllocateMaxAddress,
594 MemoryType,
595 CommonBufferPages,
596 &PhysicalAddress
597 );
598 if (EFI_ERROR (Status)) {
599 goto FreeStashBuffer;
600 }
601
602 CommonBufferHeader = (VOID *)(UINTN)PhysicalAddress;
603 PhysicalAddress += EFI_PAGE_SIZE;
604
605 CommonBufferHeader->Signature = COMMON_BUFFER_SIG;
606 CommonBufferHeader->StashBuffer = StashBuffer;
607
608 *HostAddress = (VOID *)(UINTN)PhysicalAddress;
609
610 DEBUG ((
611 DEBUG_VERBOSE,
612 "%a: Host=0x%Lx Stash=0x%p\n",
613 __FUNCTION__,
614 PhysicalAddress,
615 StashBuffer
616 ));
617 return EFI_SUCCESS;
618
619 FreeStashBuffer:
620 FreePages (StashBuffer, Pages);
621 return Status;
622 }
623
624 /**
625 Frees memory that was allocated with AllocateBuffer().
626
627 @param This The protocol instance pointer.
628 @param Pages The number of pages to free.
629 @param HostAddress The base system memory address of the allocated
630 range.
631
632 @retval EFI_SUCCESS The requested memory pages were freed.
633 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and
634 Pages was not allocated with AllocateBuffer().
635
636 **/
637 EFI_STATUS
638 EFIAPI
639 IoMmuFreeBuffer (
640 IN EDKII_IOMMU_PROTOCOL *This,
641 IN UINTN Pages,
642 IN VOID *HostAddress
643 )
644 {
645 UINTN CommonBufferPages;
646 COMMON_BUFFER_HEADER *CommonBufferHeader;
647
648 DEBUG ((
649 DEBUG_VERBOSE,
650 "%a: Host=0x%p Pages=0x%Lx\n",
651 __FUNCTION__,
652 HostAddress,
653 (UINT64)Pages
654 ));
655
656 CommonBufferPages = Pages + 1;
657 CommonBufferHeader = (COMMON_BUFFER_HEADER *)(
658 (UINTN)HostAddress - EFI_PAGE_SIZE
659 );
660
661 //
662 // Check the signature.
663 //
664 ASSERT (CommonBufferHeader->Signature == COMMON_BUFFER_SIG);
665 if (CommonBufferHeader->Signature != COMMON_BUFFER_SIG) {
666 return EFI_INVALID_PARAMETER;
667 }
668
669 //
670 // Free the stash buffer. This buffer was always encrypted, so no need to
671 // zero it.
672 //
673 FreePages (CommonBufferHeader->StashBuffer, Pages);
674
675 //
676 // Release the common buffer itself. Unmap() has re-encrypted it in-place, so
677 // no need to zero it.
678 //
679 return gBS->FreePages ((UINTN)CommonBufferHeader, CommonBufferPages);
680 }
681
682 /**
683 Set IOMMU attribute for a system memory.
684
685 If the IOMMU protocol exists, the system memory cannot be used
686 for DMA by default.
687
688 When a device requests a DMA access for a system memory,
689 the device driver need use SetAttribute() to update the IOMMU
690 attribute to request DMA access (read and/or write).
691
692 The DeviceHandle is used to identify which device submits the request.
693 The IOMMU implementation need translate the device path to an IOMMU device
694 ID, and set IOMMU hardware register accordingly.
695 1) DeviceHandle can be a standard PCI device.
696 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
697 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
698 The memory for BusMasterCommonBuffer need set
699 EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
700 After the memory is used, the memory need set 0 to keep it being
701 protected.
702 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
703 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or
704 EDKII_IOMMU_ACCESS_WRITE.
705
706 @param[in] This The protocol instance pointer.
707 @param[in] DeviceHandle The device who initiates the DMA access
708 request.
709 @param[in] Mapping The mapping value returned from Map().
710 @param[in] IoMmuAccess The IOMMU access.
711
712 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range
713 specified by DeviceAddress and Length.
714 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
715 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by
716 Map().
717 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination
718 of access.
719 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
720 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported
721 by the IOMMU.
722 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range
723 specified by Mapping.
724 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
725 modify the IOMMU access.
726 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while
727 attempting the operation.
728
729 **/
730 EFI_STATUS
731 EFIAPI
732 IoMmuSetAttribute (
733 IN EDKII_IOMMU_PROTOCOL *This,
734 IN EFI_HANDLE DeviceHandle,
735 IN VOID *Mapping,
736 IN UINT64 IoMmuAccess
737 )
738 {
739 return EFI_UNSUPPORTED;
740 }
741
742 EDKII_IOMMU_PROTOCOL mAmdSev = {
743 EDKII_IOMMU_PROTOCOL_REVISION,
744 IoMmuSetAttribute,
745 IoMmuMap,
746 IoMmuUnmap,
747 IoMmuAllocateBuffer,
748 IoMmuFreeBuffer,
749 };
750
751 /**
752 Notification function that is queued when gBS->ExitBootServices() signals the
753 EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. This function signals another
754 event, received as Context, and returns.
755
756 Signaling an event in this context is safe. The UEFI spec allows
757 gBS->SignalEvent() to return EFI_SUCCESS only; EFI_OUT_OF_RESOURCES is not
758 listed, hence memory is not allocated. The edk2 implementation also does not
759 release memory (and we only have to care about the edk2 implementation
760 because EDKII_IOMMU_PROTOCOL is edk2-specific anyway).
761
762 @param[in] Event Event whose notification function is being invoked.
763 Event is permitted to request the queueing of this
764 function at TPL_CALLBACK or TPL_NOTIFY task
765 priority level.
766
767 @param[in] EventToSignal Identifies the EFI_EVENT to signal. EventToSignal
768 is permitted to request the queueing of its
769 notification function only at TPL_CALLBACK level.
770 **/
771 STATIC
772 VOID
773 EFIAPI
774 AmdSevExitBoot (
775 IN EFI_EVENT Event,
776 IN VOID *EventToSignal
777 )
778 {
779 //
780 // (1) The NotifyFunctions of all the events in
781 // EFI_EVENT_GROUP_EXIT_BOOT_SERVICES will have been queued before
782 // AmdSevExitBoot() is entered.
783 //
784 // (2) AmdSevExitBoot() is executing minimally at TPL_CALLBACK.
785 //
786 // (3) AmdSevExitBoot() has been queued in unspecified order relative to the
787 // NotifyFunctions of all the other events in
788 // EFI_EVENT_GROUP_EXIT_BOOT_SERVICES whose NotifyTpl is the same as
789 // Event's.
790 //
791 // Consequences:
792 //
793 // - If Event's NotifyTpl is TPL_CALLBACK, then some other NotifyFunctions
794 // queued at TPL_CALLBACK may be invoked after AmdSevExitBoot() returns.
795 //
796 // - If Event's NotifyTpl is TPL_NOTIFY, then some other NotifyFunctions
797 // queued at TPL_NOTIFY may be invoked after AmdSevExitBoot() returns; plus
798 // *all* NotifyFunctions queued at TPL_CALLBACK will be invoked strictly
799 // after all NotifyFunctions queued at TPL_NOTIFY, including
800 // AmdSevExitBoot(), have been invoked.
801 //
802 // - By signaling EventToSignal here, whose NotifyTpl is TPL_CALLBACK, we
803 // queue EventToSignal's NotifyFunction after the NotifyFunctions of *all*
804 // events in EFI_EVENT_GROUP_EXIT_BOOT_SERVICES.
805 //
806 DEBUG ((DEBUG_VERBOSE, "%a\n", __FUNCTION__));
807 gBS->SignalEvent (EventToSignal);
808 }
809
810 /**
811 Notification function that is queued after the notification functions of all
812 events in the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. The same memory
813 map restrictions apply.
814
815 This function unmaps all currently existing IOMMU mappings.
816
817 @param[in] Event Event whose notification function is being invoked. Event
818 is permitted to request the queueing of this function
819 only at TPL_CALLBACK task priority level.
820
821 @param[in] Context Ignored.
822 **/
823 STATIC
824 VOID
825 EFIAPI
826 AmdSevUnmapAllMappings (
827 IN EFI_EVENT Event,
828 IN VOID *Context
829 )
830 {
831 LIST_ENTRY *Node;
832 LIST_ENTRY *NextNode;
833 MAP_INFO *MapInfo;
834
835 DEBUG ((DEBUG_VERBOSE, "%a\n", __FUNCTION__));
836
837 //
838 // All drivers that had set up IOMMU mappings have halted their respective
839 // controllers by now; tear down the mappings.
840 //
841 for (Node = GetFirstNode (&mMapInfos); Node != &mMapInfos; Node = NextNode) {
842 NextNode = GetNextNode (&mMapInfos, Node);
843 MapInfo = CR (Node, MAP_INFO, Link, MAP_INFO_SIG);
844 IoMmuUnmapWorker (
845 &mAmdSev, // This
846 MapInfo, // Mapping
847 TRUE // MemoryMapLocked
848 );
849 }
850 }
851
852 /**
853 Initialize Iommu Protocol.
854
855 **/
856 EFI_STATUS
857 EFIAPI
858 AmdSevInstallIoMmuProtocol (
859 VOID
860 )
861 {
862 EFI_STATUS Status;
863 EFI_EVENT UnmapAllMappingsEvent;
864 EFI_EVENT ExitBootEvent;
865 EFI_HANDLE Handle;
866
867 //
868 // Create the "late" event whose notification function will tear down all
869 // left-over IOMMU mappings.
870 //
871 Status = gBS->CreateEvent (
872 EVT_NOTIFY_SIGNAL, // Type
873 TPL_CALLBACK, // NotifyTpl
874 AmdSevUnmapAllMappings, // NotifyFunction
875 NULL, // NotifyContext
876 &UnmapAllMappingsEvent // Event
877 );
878 if (EFI_ERROR (Status)) {
879 return Status;
880 }
881
882 //
883 // Create the event whose notification function will be queued by
884 // gBS->ExitBootServices() and will signal the event created above.
885 //
886 Status = gBS->CreateEvent (
887 EVT_SIGNAL_EXIT_BOOT_SERVICES, // Type
888 TPL_CALLBACK, // NotifyTpl
889 AmdSevExitBoot, // NotifyFunction
890 UnmapAllMappingsEvent, // NotifyContext
891 &ExitBootEvent // Event
892 );
893 if (EFI_ERROR (Status)) {
894 goto CloseUnmapAllMappingsEvent;
895 }
896
897 Handle = NULL;
898 Status = gBS->InstallMultipleProtocolInterfaces (
899 &Handle,
900 &gEdkiiIoMmuProtocolGuid,
901 &mAmdSev,
902 NULL
903 );
904 if (EFI_ERROR (Status)) {
905 goto CloseExitBootEvent;
906 }
907
908 return EFI_SUCCESS;
909
910 CloseExitBootEvent:
911 gBS->CloseEvent (ExitBootEvent);
912
913 CloseUnmapAllMappingsEvent:
914 gBS->CloseEvent (UnmapAllMappingsEvent);
915
916 return Status;
917 }