OvmfPkg: Add AmdSevDxe driver
[mirror_edk2.git] / OvmfPkg / AmdSevDxe / AmdSevDxe.c
1 /** @file
2
3 AMD Sev Dxe driver. This driver is dispatched early in DXE, due to being list
4 in APRIORI. It clears C-bit from MMIO and NonExistent Memory space when SEV is
5 enabled.
6
7 Copyright (c) 2017, AMD Inc. All rights reserved.<BR>
8
9 This program and the accompanying materials
10 are licensed and made available under the terms and conditions of the BSD
11 License which accompanies this distribution. The full text of the license may
12 be found at http://opensource.org/licenses/bsd-license.php
13
14 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
15 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16
17 **/
18
19 #include <PiDxe.h>
20
21 #include <Library/BaseLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/MemoryAllocationLib.h>
25 #include <Library/UefiBootServicesTableLib.h>
26 #include <Library/DxeServicesTableLib.h>
27 #include <Library/MemEncryptSevLib.h>
28
29 EFI_STATUS
30 EFIAPI
31 AmdSevDxeEntryPoint (
32 IN EFI_HANDLE ImageHandle,
33 IN EFI_SYSTEM_TABLE *SystemTable
34 )
35 {
36 EFI_STATUS Status;
37 EFI_GCD_MEMORY_SPACE_DESCRIPTOR *AllDescMap;
38 UINTN NumEntries;
39 UINTN Index;
40
41 //
42 // Do nothing when SEV is not enabled
43 //
44 if (!MemEncryptSevIsEnabled ()) {
45 return EFI_UNSUPPORTED;
46 }
47
48 //
49 // Iterate through the GCD map and clear the C-bit from MMIO and NonExistent
50 // memory space. The NonExistent memory space will be used for mapping the MMIO
51 // space added later (eg PciRootBridge). By clearing both known MMIO and
52 // NonExistent memory space can gurantee that current and furture MMIO adds
53 // will have C-bit cleared.
54 //
55 Status = gDS->GetMemorySpaceMap (&NumEntries, &AllDescMap);
56 if (!EFI_ERROR (Status)) {
57 for (Index = 0; Index < NumEntries; Index++) {
58 CONST EFI_GCD_MEMORY_SPACE_DESCRIPTOR *Desc;
59
60 Desc = &AllDescMap[Index];
61 if (Desc->GcdMemoryType == EfiGcdMemoryTypeMemoryMappedIo ||
62 Desc->GcdMemoryType == EfiGcdMemoryTypeNonExistent) {
63 Status = MemEncryptSevClearPageEncMask (0,
64 Desc->BaseAddress,
65 EFI_SIZE_TO_PAGES(Desc->Length),
66 FALSE);
67 ASSERT_EFI_ERROR (Status);
68 }
69 }
70
71 FreePool (AllDescMap);
72 }
73
74 return EFI_SUCCESS;
75 }