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