]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/PlatformPei/MemTypeInfo.c
c709236a457a23bbb934c6ec8c12c9811fb33f08
[mirror_edk2.git] / OvmfPkg / PlatformPei / MemTypeInfo.c
1 /** @file
2 Produce a default memory type information HOB unless we can determine, from
3 the existence of the "MemoryTypeInformation" variable, that the DXE IPL PEIM
4 will produce the HOB.
5
6 Copyright (C) 2017-2020, Red Hat, Inc.
7
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9 **/
10
11 #include <Guid/MemoryTypeInformation.h>
12 #include <Library/BaseLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/HobLib.h>
15 #include <Library/PcdLib.h>
16 #include <Library/PeiServicesLib.h>
17 #include <Ppi/ReadOnlyVariable2.h>
18 #include <Uefi/UefiMultiPhase.h>
19
20 #include "Platform.h"
21
22 STATIC EFI_MEMORY_TYPE_INFORMATION mDefaultMemoryTypeInformation[] = {
23 { EfiACPIMemoryNVS, 0x004 },
24 { EfiACPIReclaimMemory, 0x008 },
25 { EfiReservedMemoryType, 0x004 },
26 { EfiRuntimeServicesData, 0x024 },
27 { EfiRuntimeServicesCode, 0x030 },
28 { EfiBootServicesCode, 0x180 },
29 { EfiBootServicesData, 0xF00 },
30 { EfiMaxMemoryType, 0x000 }
31 };
32
33 STATIC
34 VOID
35 BuildMemTypeInfoHob (
36 VOID
37 )
38 {
39 BuildGuidDataHob (
40 &gEfiMemoryTypeInformationGuid,
41 mDefaultMemoryTypeInformation,
42 sizeof mDefaultMemoryTypeInformation
43 );
44 DEBUG ((DEBUG_INFO, "%a: default memory type information HOB built\n",
45 __FUNCTION__));
46 }
47
48 /**
49 Notification function called when EFI_PEI_READ_ONLY_VARIABLE2_PPI becomes
50 available.
51
52 @param[in] PeiServices Indirect reference to the PEI Services Table.
53 @param[in] NotifyDescriptor Address of the notification descriptor data
54 structure.
55 @param[in] Ppi Address of the PPI that was installed.
56
57 @return Status of the notification. The status code returned from this
58 function is ignored.
59 **/
60 STATIC
61 EFI_STATUS
62 EFIAPI
63 OnReadOnlyVariable2Available (
64 IN EFI_PEI_SERVICES **PeiServices,
65 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor,
66 IN VOID *Ppi
67 )
68 {
69 EFI_PEI_READ_ONLY_VARIABLE2_PPI *ReadOnlyVariable2;
70 UINTN DataSize;
71 EFI_STATUS Status;
72
73 DEBUG ((DEBUG_VERBOSE, "%a\n", __FUNCTION__));
74
75 //
76 // Check if the "MemoryTypeInformation" variable exists, in the
77 // gEfiMemoryTypeInformationGuid namespace.
78 //
79 ReadOnlyVariable2 = Ppi;
80 DataSize = 0;
81 Status = ReadOnlyVariable2->GetVariable (
82 ReadOnlyVariable2,
83 EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,
84 &gEfiMemoryTypeInformationGuid,
85 NULL,
86 &DataSize,
87 NULL
88 );
89 switch (Status) {
90 case EFI_BUFFER_TOO_SMALL:
91 //
92 // The variable exists; the DXE IPL PEIM will build the HOB from it.
93 //
94 break;
95 case EFI_NOT_FOUND:
96 //
97 // The variable does not exist; install the default memory type information
98 // HOB.
99 //
100 BuildMemTypeInfoHob ();
101 break;
102 default:
103 DEBUG ((DEBUG_ERROR, "%a: unexpected: GetVariable(): %r\n", __FUNCTION__,
104 Status));
105 ASSERT (FALSE);
106 CpuDeadLoop ();
107 break;
108 }
109
110 return EFI_SUCCESS;
111 }
112
113 //
114 // Notification object for registering the callback, for when
115 // EFI_PEI_READ_ONLY_VARIABLE2_PPI becomes available.
116 //
117 STATIC CONST EFI_PEI_NOTIFY_DESCRIPTOR mReadOnlyVariable2Notify = {
118 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_DISPATCH |
119 EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST), // Flags
120 &gEfiPeiReadOnlyVariable2PpiGuid, // Guid
121 OnReadOnlyVariable2Available // Notify
122 };
123
124 VOID
125 MemTypeInfoInitialization (
126 VOID
127 )
128 {
129 EFI_STATUS Status;
130
131 if (!FeaturePcdGet (PcdSmmSmramRequire)) {
132 //
133 // EFI_PEI_READ_ONLY_VARIABLE2_PPI will never be available; install
134 // the default memory type information HOB right away.
135 //
136 BuildMemTypeInfoHob ();
137 return;
138 }
139
140 Status = PeiServicesNotifyPpi (&mReadOnlyVariable2Notify);
141 if (EFI_ERROR (Status)) {
142 DEBUG ((DEBUG_ERROR, "%a: failed to set up R/O Variable 2 callback: %r\n",
143 __FUNCTION__, Status));
144 ASSERT (FALSE);
145 CpuDeadLoop ();
146 }
147 }