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