summaryrefslogtreecommitdiff
path: root/EDK/Foundation/Core/Pei/PeiMain/PeiMain.c
blob: 692944c8d4be2f5a6f7385eff888a2c294525efd (plain)
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
/*++

Copyright (c) 2004 - 2011, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which 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:

  PeiMain.c

Abstract:

  Pei Core Main Entry Point

Revision History

--*/

#include "Tiano.h"
#include "PeiCore.h"
#include "PeiLib.h"
#include EFI_PPI_DEFINITION (MemoryDiscovered)
#include EFI_PPI_DEFINITION (FindFv)
#include EFI_GUID_DEFINITION (StatusCodeDataTypeId)
#include EFI_GUID_DEFINITION (StatusCode)

//
//CAR is filled with this initial value during SEC phase
//
#define INIT_CAR_VALUE 0x5AA55AA5

#ifdef FIRMWARE_PERFORMANCE

EFI_STATUS
GetTimerValue (
  OUT UINT64    *TimerValue
  );

#endif

static EFI_PEI_PPI_DESCRIPTOR mMemoryDiscoveredPpi = {
  (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
  &gPeiMemoryDiscoveredPpiGuid,
  NULL
};

//
// Pei Core Module Variables
// 
//
static EFI_PEI_SERVICES  mPS = {
  {
    PEI_SERVICES_SIGNATURE,
    PEI_SERVICES_REVISION,
    sizeof (EFI_PEI_SERVICES),
    0,
    0
  },
  PeiInstallPpi,
  PeiReInstallPpi,
  PeiLocatePpi,
  PeiNotifyPpi,

  PeiGetBootMode,
  PeiSetBootMode,

  PeiGetHobList,
  PeiCreateHob,

  PeiFvFindNextVolume,
  PeiFfsFindNextFile,
  PeiFfsFindSectionData,

  PeiInstallPeiMemory,
  PeiAllocatePages,
  PeiAllocatePool,
  PeiCoreCopyMem,
  PeiCoreSetMem,

  PeiReportStatusCode,

  PeiCoreResetSystem
};

VOID
EFIAPI
AsmWriteMm7 (
  IN UINT64   Value
  );

EFI_STATUS
EFIAPI
PeiCore (
  IN EFI_PEI_STARTUP_DESCRIPTOR  *PeiStartupDescriptor,
  IN PEI_CORE_INSTANCE           *OldCoreData
  );

//
// Main entry point to the PEI Core
//
EFI_PEI_CORE_ENTRY_POINT (PeiMain)

EFI_STATUS
EFIAPI
PeiMain (
  IN EFI_PEI_STARTUP_DESCRIPTOR  *PeiStartupDescriptor
  )
/*++

Routine Description:

  Main entry point to Pei Core.

Arguments:

  PeiStartupDescriptor - Information and services provided by SEC phase.

Returns:

  This function never returns

--*/
{
  return PeiCore (PeiStartupDescriptor, NULL);
}

EFI_STATUS
EFIAPI
PeiCore (
  IN EFI_PEI_STARTUP_DESCRIPTOR  *PeiStartupDescriptor,
  IN PEI_CORE_INSTANCE           *OldCoreData
  )
/*++

Routine Description:

  The entry routine to Pei Core, invoked by PeiMain during transition
  from SEC to PEI. After switching stack in the PEI core, it will restart
  with the old core data.

Arguments:

  PeiStartupDescriptor - Information and services provided by SEC phase.
  OldCoreData          - Pointer to old core data that is used to initialize the
                         core's data areas.

Returns:

  This function never returns
  EFI_NOT_FOUND        - Never reach

--*/
{
  PEI_CORE_INSTANCE                                     PrivateData;
  EFI_STATUS                                            Status;
  PEI_CORE_TEMP_POINTERS                                TempPtr;
  PEI_CORE_DISPATCH_DATA                                *DispatchData;
  
 
#ifdef FIRMWARE_PERFORMANCE
  UINT64                      mTick;

  mTick = 0;
  if (OldCoreData == NULL) {
    GetTimerValue (&mTick);
  }
#endif

  
  //
  // For IPF in CAR mode the real memory access is uncached,in InstallPeiMemory()
  //  the 63-bit of address is set to 1.
  //
  SWITCH_TO_CACHE_MODE (OldCoreData);

  if (OldCoreData != NULL) {
    PeiCoreCopyMem (&PrivateData, OldCoreData, sizeof (PEI_CORE_INSTANCE));
  } else {
    PeiCoreSetMem (&PrivateData, sizeof (PEI_CORE_INSTANCE), 0);
  }

  PrivateData.Signature = PEI_CORE_HANDLE_SIGNATURE;
  PrivateData.PS = &mPS;
  
  //
  //  Mm7 stored PeiServicesTablePointer in EdkII Glue Library
  //  Update Mm7 now
  //
#if (defined(EFI32)) || (defined(EFIX64))
  AsmWriteMm7 ((UINT64)(UINTN)(&PrivateData.PS));
#endif

  InitializeMemoryServices (&PrivateData.PS, PeiStartupDescriptor, OldCoreData);

  InitializePpiServices (&PrivateData.PS, OldCoreData);

  InitializeSecurityServices (&PrivateData.PS, OldCoreData);

  PEI_DEBUG_CODE (
    InstallCoreDebugMaskPpi (&PrivateData.PS, OldCoreData);
  )

  InitializeDispatcherData (&PrivateData.PS, OldCoreData, PeiStartupDescriptor);

  if (OldCoreData != NULL) {

    PEI_PERF_END (&PrivateData.PS,L"PreMem", NULL, 0);
    PEI_PERF_START (&PrivateData.PS,L"PostMem", NULL, 0);

    //
    // The following code dumps out interesting cache as RAM usage information
    // so we can keep tabs on how the cache as RAM is being utilized.  The
    // PEI_DEBUG_CODE macro is used to prevent this code from being compiled
    // on a debug build.
    //
    PEI_DEBUG_CODE (
      {
        UINTN  *StackPointer;
        UINTN  StackValue;

        StackValue = INIT_CAR_VALUE;
      for (StackPointer = (UINTN *) OldCoreData->MaxTopOfCarHeap;
           ((UINTN) StackPointer < ((UINTN) OldCoreData->BottomOfCarHeap + OldCoreData->SizeOfCacheAsRam)) 
           && StackValue == INIT_CAR_VALUE;
           StackPointer++) {
        StackValue = *StackPointer;
      }

      PEI_DEBUG ((&PrivateData.PS, EFI_D_INFO, "Total Cache as RAM:    %d bytes.\n", OldCoreData->SizeOfCacheAsRam));
      PEI_DEBUG (
        (
        &PrivateData.PS, EFI_D_INFO, "  CAR stack ever used: %d bytes.\n",
        ((UINTN) OldCoreData->TopOfCarHeap - (UINTN) StackPointer)
        )
        );
        PEI_DEBUG (
        (
        &PrivateData.PS, EFI_D_INFO, "  CAR heap used:       %d bytes.\n",
        ((UINTN) OldCoreData->HobList.HandoffInformationTable->EfiFreeMemoryBottom -
         (UINTN) OldCoreData->HobList.Raw)
        )
        );
      }
    )

    //
    // Alert any listeners that there is permanent memory available
    // 
    PEI_PERF_START (&PrivateData.PS,L"DisMem", NULL, 0);
    Status = PeiInstallPpi (&PrivateData.PS, &mMemoryDiscoveredPpi);
    PEI_PERF_END (&PrivateData.PS,L"DisMem", NULL, 0);

  } else {
    
    //
    // Report Status Code EFI_SW_PC_INIT
    //
    PEI_REPORT_STATUS_CODE (
      &(PrivateData.PS),
      EFI_PROGRESS_CODE,
      EFI_SOFTWARE_PEI_CORE | EFI_SW_PC_INIT,
      0,
      NULL,
      NULL
      );

    //
    // If first pass, start performance measurement.
    //
    PEI_PERF_START (&PrivateData.PS,L"PreMem", NULL, mTick);

    //
    // If SEC provided any PPI services to PEI, install them.
    //
    if (PeiStartupDescriptor->DispatchTable != NULL) {
      Status = PeiInstallPpi (&PrivateData.PS, PeiStartupDescriptor->DispatchTable);

      ASSERT_PEI_ERROR (&PrivateData.PS, Status);
    }
  }

  DispatchData = &PrivateData.DispatchData;

  //
  // Call PEIM dispatcher
  //
  PeiDispatcher (PeiStartupDescriptor, &PrivateData, DispatchData);

  //
  // Check if InstallPeiMemory service was called.
  // 
  PEI_ASSERT(&PrivateData.PS, PrivateData.PeiMemoryInstalled == TRUE);

  PEI_PERF_END (&PrivateData.PS, L"PostMem", NULL, 0);

  Status = PeiLocatePpi (
            &PrivateData.PS,
            &gEfiDxeIplPpiGuid,
            0,
            NULL,
            &TempPtr.DxeIpl
            );
  ASSERT_PEI_ERROR (&PrivateData.PS, Status);

  PEI_DEBUG ((&PrivateData.PS, EFI_D_INFO, "DXE IPL Entry\n"));
  Status = TempPtr.DxeIpl->Entry (
                             TempPtr.DxeIpl,
                             &PrivateData.PS,
                             PrivateData.HobList
                             );

  ASSERT_PEI_ERROR (&PrivateData.PS, Status);
  
  return EFI_NOT_FOUND;
}