1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/** @file
Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials are licensed and made available under
the terms and conditions of the BSD License that accompanies this distribution.
The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
Module Name:
MemoryPeim.c
Abstract:
Tiano PEIM to provide the platform support functionality.
This file implements the Platform Memory Range PPI
--*/
#include "PlatformEarlyInit.h"
//
// Need min. of 48MB PEI phase
//
#define PEI_MIN_MEMORY_SIZE (6 * 0x800000)
#define PEI_RECOVERY_MIN_MEMORY_SIZE (6 * 0x800000)
//
// This is the memory needed for PEI to start up DXE.
//
// Over-estimating this size will lead to higher fragmentation
// of main memory. Under-estimation of this will cause catastrophic
// failure of PEI to load DXE. Generally, the failure may only be
// realized during capsule updates.
//
#define PRERESERVED_PEI_MEMORY ( \
EFI_SIZE_TO_PAGES (3 * 0x800000) /* PEI Core memory based stack */ \
)
EFI_MEMORY_TYPE_INFORMATION mDefaultMemoryTypeInformation[] = {
{ EfiACPIReclaimMemory, 0x40 }, // 0x40 pages = 256k for ASL
{ EfiACPIMemoryNVS, 0x100 }, // 0x100 pages = 1 MB for S3, SMM, HII, etc
{ EfiReservedMemoryType, 0x600 }, // 48k for BIOS Reserved
{ EfiMemoryMappedIO, 0 },
{ EfiMemoryMappedIOPortSpace, 0 },
{ EfiPalCode, 0 },
{ EfiRuntimeServicesCode, 0x200 },
{ EfiRuntimeServicesData, 0x100 },
{ EfiLoaderCode, 0x100 },
{ EfiLoaderData, 0x100 },
{ EfiBootServicesCode, 0x800 },
{ EfiBootServicesData, 0x2500},
{ EfiConventionalMemory, 0 },
{ EfiUnusableMemory, 0 },
{ EfiMaxMemoryType, 0 }
};
STATIC
EFI_STATUS
GetMemorySize (
IN CONST EFI_PEI_SERVICES **PeiServices,
OUT UINT64 *LowMemoryLength,
OUT UINT64 *HighMemoryLength
);
EFI_STATUS
EFIAPI
SetPeiCacheMode (
IN CONST EFI_PEI_SERVICES **PeiServices
)
{
EFI_STATUS Status;
PEI_CACHE_PPI *CachePpi;
EFI_BOOT_MODE BootMode;
UINT64 MemoryLength;
UINT64 MemOverflow;
UINT64 MemoryLengthUc;
UINT64 MaxMemoryLength;
UINT64 LowMemoryLength;
UINT64 HighMemoryLength;
UINT8 Index;
MTRR_SETTINGS MtrrSetting;
//
// Load Cache PPI
//
Status = (**PeiServices).LocatePpi (
PeiServices,
&gPeiCachePpiGuid, // GUID
0, // Instance
NULL, // EFI_PEI_PPI_DESCRIPTOR
(void **)&CachePpi // PPI
);
if (!EFI_ERROR(Status)) {
//
// Clear the CAR Settings (Default Cache Type => UC)
//
DEBUG ((EFI_D_INFO, "Reset cache attribute and disable CAR. \n"));
CachePpi->ResetCache(
(EFI_PEI_SERVICES**)PeiServices,
CachePpi
);
}
//
// Variable initialization
//
LowMemoryLength = 0;
HighMemoryLength = 0;
MemoryLengthUc = 0;
Status = (*PeiServices)->GetBootMode (
PeiServices,
&BootMode
);
//
// Determine memory usage
//
GetMemorySize (
PeiServices,
&LowMemoryLength,
&HighMemoryLength
);
LowMemoryLength = (EFI_PHYSICAL_ADDRESS)MmPci32( 0, 0, 2, 0, 0x70);
LowMemoryLength &= 0xFFF00000ULL;
MaxMemoryLength = LowMemoryLength;
//
// Round up to nearest 256MB with high memory and 64MB w/o high memory
//
if (HighMemoryLength != 0 ) {
MemOverflow = (LowMemoryLength & 0x0fffffff);
if (MemOverflow != 0) {
MaxMemoryLength = LowMemoryLength + (0x10000000 - MemOverflow);
}
} else {
MemOverflow = (LowMemoryLength & 0x03ffffff);
if (MemOverflow != 0) {
MaxMemoryLength = LowMemoryLength + (0x4000000 - MemOverflow);
}
}
ZeroMem (&MtrrSetting, sizeof(MTRR_SETTINGS));
for (Index = 0; Index < 2; Index++) {
MtrrSetting.Fixed.Mtrr[Index]=0x0606060606060606;
}
for (Index = 2; Index < 11; Index++) {
MtrrSetting.Fixed.Mtrr[Index]=0x0505050505050505;
}
//
// Cache the flash area to improve the boot performance in PEI phase
//
Index = 0;
MtrrSetting.Variables.Mtrr[0].Base = (FixedPcdGet32 (PcdFlashAreaBaseAddress) | CacheWriteProtected);
MtrrSetting.Variables.Mtrr[0].Mask = ((~((UINT64)(FixedPcdGet32 (PcdFlashAreaSize) - 1))) & MTRR_LIB_CACHE_VALID_ADDRESS) | MTRR_LIB_CACHE_MTRR_ENABLED;
Index ++;
MemOverflow =0;
while (MaxMemoryLength > MemOverflow){
MtrrSetting.Variables.Mtrr[Index].Base = (MemOverflow & MTRR_LIB_CACHE_VALID_ADDRESS) | CacheWriteBack;
MemoryLength = MaxMemoryLength - MemOverflow;
MemoryLength = GetPowerOfTwo64 (MemoryLength);
MtrrSetting.Variables.Mtrr[Index].Mask = ((~(MemoryLength - 1)) & MTRR_LIB_CACHE_VALID_ADDRESS) | MTRR_LIB_CACHE_MTRR_ENABLED;
MemOverflow += MemoryLength;
Index++;
}
MemoryLength = LowMemoryLength;
while (MaxMemoryLength != MemoryLength) {
MemoryLengthUc = GetPowerOfTwo64 (MaxMemoryLength - MemoryLength);
MtrrSetting.Variables.Mtrr[Index].Base = ((MaxMemoryLength - MemoryLengthUc) & MTRR_LIB_CACHE_VALID_ADDRESS) | CacheUncacheable;
MtrrSetting.Variables.Mtrr[Index].Mask= ((~(MemoryLengthUc - 1)) & MTRR_LIB_CACHE_VALID_ADDRESS) | MTRR_LIB_CACHE_MTRR_ENABLED;
MaxMemoryLength -= MemoryLengthUc;
Index++;
}
MemOverflow =0x100000000;
while (HighMemoryLength > 0) {
MtrrSetting.Variables.Mtrr[Index].Base = (MemOverflow & MTRR_LIB_CACHE_VALID_ADDRESS) | CacheWriteBack;
MemoryLength = HighMemoryLength;
MemoryLength = GetPowerOfTwo64 (MemoryLength);
if (MemoryLength > MemOverflow){
MemoryLength = MemOverflow;
}
MtrrSetting.Variables.Mtrr[Index].Mask = ((~(MemoryLength - 1)) & MTRR_LIB_CACHE_VALID_ADDRESS) | MTRR_LIB_CACHE_MTRR_ENABLED;
MemOverflow += MemoryLength;
HighMemoryLength -= MemoryLength;
Index++;
}
for (Index = 0; Index < MTRR_NUMBER_OF_VARIABLE_MTRR; Index++) {
if (MtrrSetting.Variables.Mtrr[Index].Base == 0){
break;
}
DEBUG ((EFI_D_INFO, "Base=%lx, Mask=%lx\n",MtrrSetting.Variables.Mtrr[Index].Base ,MtrrSetting.Variables.Mtrr[Index].Mask));
}
//
// set FE/E bits for IA32_MTRR_DEF_TYPE
//
MtrrSetting.MtrrDefType |= 3 <<10;
MtrrSetAllMtrrs(&MtrrSetting);
return EFI_SUCCESS;
}
EFI_STATUS
EFIAPI
SetDxeCacheMode (
IN CONST EFI_PEI_SERVICES **PeiServices
)
{
//
// This is not needed for now.
//
return EFI_SUCCESS;
}
STATIC
EFI_STATUS
GetMemorySize (
IN CONST EFI_PEI_SERVICES **PeiServices,
OUT UINT64 *LowMemoryLength,
OUT UINT64 *HighMemoryLength
)
{
EFI_STATUS Status;
EFI_PEI_HOB_POINTERS Hob;
*HighMemoryLength = 0;
*LowMemoryLength = 0x100000;
//
// Get the HOB list for processing
//
Status = (*PeiServices)->GetHobList (PeiServices, (void **)&Hob.Raw);
if (EFI_ERROR(Status)) {
return Status;
}
//
// Collect memory ranges
//
while (!END_OF_HOB_LIST (Hob)) {
if (Hob.Header->HobType == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
if (Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) {
//
// Need memory above 1MB to be collected here
//
if (Hob.ResourceDescriptor->PhysicalStart >= 0x100000 &&
Hob.ResourceDescriptor->PhysicalStart < (EFI_PHYSICAL_ADDRESS) 0x100000000) {
*LowMemoryLength += (UINT64) (Hob.ResourceDescriptor->ResourceLength);
} else if (Hob.ResourceDescriptor->PhysicalStart >= (EFI_PHYSICAL_ADDRESS) 0x100000000) {
*HighMemoryLength += (UINT64) (Hob.ResourceDescriptor->ResourceLength);
}
}
}
Hob.Raw = GET_NEXT_HOB (Hob);
}
return EFI_SUCCESS;
}
/**
Publish Memory Type Information.
@param NULL
@retval EFI_SUCCESS Success.
@retval Others Errors have occurred.
**/
EFI_STATUS
EFIAPI
PublishMemoryTypeInfo (
void
)
{
EFI_STATUS Status;
EFI_PEI_READ_ONLY_VARIABLE2_PPI *Variable;
UINTN DataSize;
EFI_MEMORY_TYPE_INFORMATION MemoryData[EfiMaxMemoryType + 1];
Status = PeiServicesLocatePpi (
&gEfiPeiReadOnlyVariable2PpiGuid,
0,
NULL,
(void **)&Variable
);
if (EFI_ERROR(Status)) {
DEBUG((EFI_D_ERROR, "WARNING: Locating Pei variable failed 0x%x \n", Status));
DEBUG((EFI_D_ERROR, "Build Hob from default\n"));
//
// Build the default GUID'd HOB for DXE
//
BuildGuidDataHob (
&gEfiMemoryTypeInformationGuid,
mDefaultMemoryTypeInformation,
sizeof (mDefaultMemoryTypeInformation)
);
return Status;
}
DataSize = sizeof (MemoryData);
//
// This variable is saved in BDS stage. Now read it back
//
Status = Variable->GetVariable (
Variable,
EFI_MEMORY_TYPE_INFORMATION_VARIABLE_NAME,
&gEfiMemoryTypeInformationGuid,
NULL,
&DataSize,
&MemoryData
);
if (EFI_ERROR (Status)) {
//
//build default
//
DEBUG((EFI_D_ERROR, "Build Hob from default\n"));
BuildGuidDataHob (
&gEfiMemoryTypeInformationGuid,
mDefaultMemoryTypeInformation,
sizeof (mDefaultMemoryTypeInformation)
);
} else {
//
// Build the GUID'd HOB for DXE from variable
//
DEBUG((EFI_D_ERROR, "Build Hob from variable \n"));
BuildGuidDataHob (
&gEfiMemoryTypeInformationGuid,
MemoryData,
DataSize
);
}
return Status;
}
|