Commit | Line | Data |
---|---|---|
49ba9447 | 1 | /**@file\r |
2 | Memory Detection for Virtual Machines.\r | |
3 | \r | |
4b455f7b | 4 | Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r |
56d7640a | 5 | This program and the accompanying materials\r |
49ba9447 | 6 | are licensed and made available under the terms and conditions of the BSD License\r |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | Module Name:\r | |
14 | \r | |
15 | MemDetect.c\r | |
16 | \r | |
17 | **/\r | |
18 | \r | |
19 | //\r | |
20 | // The package level header files this module uses\r | |
21 | //\r | |
22 | #include <PiPei.h>\r | |
23 | \r | |
24 | //\r | |
25 | // The Library classes this module consumes\r | |
26 | //\r | |
27 | #include <Library/DebugLib.h>\r | |
28 | #include <Library/HobLib.h>\r | |
29 | #include <Library/IoLib.h>\r | |
c1c2669c | 30 | #include <Library/PcdLib.h>\r |
49ba9447 | 31 | #include <Library/PeimEntryPoint.h>\r |
32 | #include <Library/ResourcePublicationLib.h>\r | |
e8e5cd4a | 33 | #include <Library/MtrrLib.h>\r |
49ba9447 | 34 | \r |
35 | #include "Platform.h"\r | |
36 | #include "Cmos.h"\r | |
37 | \r | |
4b455f7b | 38 | UINT32\r |
c0e10976 | 39 | GetSystemMemorySizeBelow4gb (\r |
4b455f7b | 40 | VOID\r |
49ba9447 | 41 | )\r |
42 | {\r | |
43 | UINT8 Cmos0x34;\r | |
44 | UINT8 Cmos0x35;\r | |
45 | \r | |
46 | //\r | |
47 | // CMOS 0x34/0x35 specifies the system memory above 16 MB.\r | |
48 | // * CMOS(0x35) is the high byte\r | |
49 | // * CMOS(0x34) is the low byte\r | |
50 | // * The size is specified in 64kb chunks\r | |
51 | // * Since this is memory above 16MB, the 16MB must be added\r | |
52 | // into the calculation to get the total memory size.\r | |
53 | //\r | |
54 | \r | |
55 | Cmos0x34 = (UINT8) CmosRead8 (0x34);\r | |
56 | Cmos0x35 = (UINT8) CmosRead8 (0x35);\r | |
57 | \r | |
55cdb67a | 58 | return (((UINTN)((Cmos0x35 << 8) + Cmos0x34) << 16) + SIZE_16MB);\r |
49ba9447 | 59 | }\r |
60 | \r | |
61 | \r | |
c0e10976 | 62 | STATIC\r |
63 | UINT64\r | |
64 | GetSystemMemorySizeAbove4gb (\r | |
65 | )\r | |
66 | {\r | |
67 | UINT32 Size;\r | |
68 | UINTN CmosIndex;\r | |
69 | \r | |
70 | //\r | |
71 | // CMOS 0x5b-0x5d specifies the system memory above 4GB MB.\r | |
72 | // * CMOS(0x5d) is the most significant size byte\r | |
73 | // * CMOS(0x5c) is the middle size byte\r | |
74 | // * CMOS(0x5b) is the least significant size byte\r | |
75 | // * The size is specified in 64kb chunks\r | |
76 | //\r | |
77 | \r | |
78 | Size = 0;\r | |
79 | for (CmosIndex = 0x5d; CmosIndex >= 0x5b; CmosIndex--) {\r | |
80 | Size = (UINT32) (Size << 8) + (UINT32) CmosRead8 (CmosIndex);\r | |
81 | }\r | |
82 | \r | |
83 | return LShiftU64 (Size, 16);\r | |
84 | }\r | |
85 | \r | |
36658fff WL |
86 | /**\r |
87 | Publish PEI core memory\r | |
88 | \r | |
89 | @return EFI_SUCCESS The PEIM initialized successfully.\r | |
90 | \r | |
91 | **/\r | |
92 | EFI_STATUS\r | |
93 | PublishPeiMemory (\r | |
94 | VOID\r | |
95 | )\r | |
96 | {\r | |
97 | EFI_STATUS Status;\r | |
98 | EFI_PHYSICAL_ADDRESS MemoryBase;\r | |
99 | UINT64 MemorySize;\r | |
100 | UINT64 LowerMemorySize;\r | |
101 | \r | |
8e54500f JJ |
102 | if (mBootMode == BOOT_ON_S3_RESUME) {\r |
103 | MemoryBase = PcdGet32 (PcdS3AcpiReservedMemoryBase);\r | |
104 | MemorySize = PcdGet32 (PcdS3AcpiReservedMemorySize);\r | |
105 | } else {\r | |
106 | LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r | |
107 | \r | |
108 | //\r | |
109 | // Determine the range of memory to use during PEI\r | |
110 | //\r | |
111 | MemoryBase = PcdGet32 (PcdOvmfDxeMemFvBase) + PcdGet32 (PcdOvmfDxeMemFvSize);\r | |
112 | MemorySize = LowerMemorySize - MemoryBase;\r | |
113 | if (MemorySize > SIZE_64MB) {\r | |
114 | MemoryBase = LowerMemorySize - SIZE_64MB;\r | |
115 | MemorySize = SIZE_64MB;\r | |
116 | }\r | |
36658fff WL |
117 | }\r |
118 | \r | |
119 | //\r | |
120 | // Publish this memory to the PEI Core\r | |
121 | //\r | |
122 | Status = PublishSystemMemory(MemoryBase, MemorySize);\r | |
123 | ASSERT_EFI_ERROR (Status);\r | |
124 | \r | |
125 | return Status;\r | |
126 | }\r | |
127 | \r | |
c0e10976 | 128 | \r |
49ba9447 | 129 | /**\r |
c034906e | 130 | Peform Memory Detection for QEMU / KVM\r |
49ba9447 | 131 | \r |
132 | **/\r | |
c034906e JJ |
133 | STATIC\r |
134 | VOID\r | |
135 | QemuInitializeRam (\r | |
136 | VOID\r | |
49ba9447 | 137 | )\r |
138 | {\r | |
c0e10976 | 139 | UINT64 LowerMemorySize;\r |
140 | UINT64 UpperMemorySize;\r | |
49ba9447 | 141 | \r |
c034906e | 142 | DEBUG ((EFI_D_INFO, "%a called\n", __FUNCTION__));\r |
49ba9447 | 143 | \r |
144 | //\r | |
145 | // Determine total memory size available\r | |
146 | //\r | |
c0e10976 | 147 | LowerMemorySize = GetSystemMemorySizeBelow4gb ();\r |
148 | UpperMemorySize = GetSystemMemorySizeAbove4gb ();\r | |
49ba9447 | 149 | \r |
49ba9447 | 150 | //\r |
151 | // Create memory HOBs\r | |
152 | //\r | |
9ab36385 | 153 | AddMemoryRangeHob (BASE_1MB, LowerMemorySize);\r |
c1c2669c | 154 | AddMemoryRangeHob (0, BASE_512KB + BASE_128KB);\r |
49ba9447 | 155 | \r |
9ab36385 | 156 | MtrrSetMemoryAttribute (BASE_1MB, LowerMemorySize - BASE_1MB, CacheWriteBack);\r |
e8e5cd4a | 157 | \r |
27d7e63f | 158 | MtrrSetMemoryAttribute (0, BASE_512KB + BASE_128KB, CacheWriteBack);\r |
e8e5cd4a | 159 | \r |
c0e10976 | 160 | if (UpperMemorySize != 0) {\r |
161 | AddUntestedMemoryBaseSizeHob (BASE_4GB, UpperMemorySize);\r | |
e8e5cd4a | 162 | \r |
27d7e63f | 163 | MtrrSetMemoryAttribute (BASE_4GB, UpperMemorySize, CacheWriteBack);\r |
c0e10976 | 164 | }\r |
49ba9447 | 165 | }\r |
166 | \r | |
c034906e JJ |
167 | /**\r |
168 | Publish system RAM and reserve memory regions\r | |
169 | \r | |
170 | **/\r | |
171 | VOID\r | |
172 | InitializeRamRegions (\r | |
173 | VOID\r | |
174 | )\r | |
175 | {\r | |
2818c158 JJ |
176 | if (!mXen) {\r |
177 | QemuInitializeRam ();\r | |
178 | } else {\r | |
2818c158 JJ |
179 | XenPublishRamRegions ();\r |
180 | }\r | |
8e54500f JJ |
181 | \r |
182 | if (mS3Supported && mBootMode != BOOT_ON_S3_RESUME) {\r | |
183 | //\r | |
184 | // This is the memory range that will be used for PEI on S3 resume\r | |
185 | //\r | |
186 | BuildMemoryAllocationHob (\r | |
187 | (EFI_PHYSICAL_ADDRESS)(UINTN) PcdGet32 (PcdS3AcpiReservedMemoryBase),\r | |
188 | (UINT64)(UINTN) PcdGet32 (PcdS3AcpiReservedMemorySize),\r | |
189 | EfiACPIMemoryNVS\r | |
190 | );\r | |
191 | }\r | |
c034906e | 192 | }\r |