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