summaryrefslogtreecommitdiff
path: root/ReferenceCode/Chipset/LynxPoint/S3Support/Dxe/PchS3Support.c
blob: 943ffd7dcaf462d56b89fd0c48377cdbc60538a8 (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
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
  This is the driver that implements the PCH S3 Support protocol

@copyright
  Copyright (c) 1999 - 2015 Intel Corporation. All rights reserved
  This software and associated documentation (if any) is furnished
  under a license and may only be used or copied in accordance
  with the terms of the license. Except as permitted by such
  license, no part of this software or documentation may be
  reproduced, stored in a retrieval system, or transmitted in any
  form or by any means without the express written consent of
  Intel Corporation.

  This file contains an 'Intel Peripheral Driver' and uniquely
  identified as "Intel Reference Module" and is
  licensed for Intel CPUs and chipsets under the terms of your
  license agreement with Intel or your vendor.  This file may
  be modified by the user, subject to additional terms of the
  license agreement

**/
#include "PchS3Support.h"
#include "S3SupportHob.h"
// AMI_OVERRIDE, [ EIP217847 ] >>>
#include "token.h"
// AMI_OVERRIDE, [ EIP217847 ] <<<

//
// Global Variables
//
EFI_HANDLE                        mImageHandle;
EFI_PCH_S3_SUPPORT_PROTOCOL       mPchS3SupportProtocol;
EFI_PCH_S3_SUPPORT_SMM_PROTOCOL   mPchS3SupportSmmProtocol;
UINT32                            mPchS3ImageEntryPoint;
EFI_PCH_S3_DISPATCH_ARRAY         *mPchS3CustomDispatchScript;

//
// GUID Definitions
//
EFI_GUID gS3SupportHobGuid        = S3_SUPPORT_HOB_GUID;
EFI_GUID gS3SupportSmramDataGuid  = EFI_PCH_S3_SUPPORT_DATA_GUID;

//
// Functions
//

/**
  PCH S3 support driver entry point

  @param[in] ImageHandle          Handle for the image of this driver
  @param[in] SystemTable          Pointer to the EFI System Table

  @retval EFI_STATUS
**/
EFI_STATUS
EFIAPI
PchS3SupportEntryPoint (
  IN EFI_HANDLE         ImageHandle,
  IN EFI_SYSTEM_TABLE   *SystemTable
  )
{
  EFI_STATUS                        Status;

  DEBUG ((EFI_D_INFO, "PchS3SupportEntryPoint() Start\n"));
  mImageHandle = ImageHandle;

  ///
  /// Initialize the Boot Services memory for the Dispatch Script Array
  ///
  Status = InitializePchS3CustomScriptMemory();
  ASSERT_EFI_ERROR (Status);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  DEBUG ((EFI_D_INFO, "Dispatch Script Array Space initialized.\n"));

  ///
  /// Retrieve the PCH S3 Support PEIM entry point and load it into the Module variable
  ///
  Status = LoadPchS3ImageEntryPoint (&mPchS3ImageEntryPoint);
  ASSERT_EFI_ERROR (Status);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  DEBUG ((EFI_D_INFO, "PCH S3 Image Entry Point intialized.\n"));

  ///
  /// Initialize and Install the PCH S3 Support and PCH S3 SMM Support protocols
  ///
  mPchS3SupportSmmProtocol.DispatchArray = mPchS3CustomDispatchScript;
  mPchS3SupportSmmProtocol.ProtocolSize = 1;  // Allocate one page
  mPchS3SupportProtocol.SetDispatchItem = PchS3SetDispatchItem;
  mPchS3SupportProtocol.ReadyToLock = S3SupportReadyToLock;
  Status = gBS->InstallMultipleProtocolInterfaces (
                  &mImageHandle,
                  &gEfiPchS3SupportProtocolGuid,
                  &mPchS3SupportProtocol,
                  &gEfiPchS3SupportSmmProtocolGuid,
                  &mPchS3SupportSmmProtocol,
                  NULL
                  );
  ASSERT_EFI_ERROR (Status);

  DEBUG ((EFI_D_INFO, "PchS3SupportEntryPoint() End\n"));

  return Status;
}

/**
  Set an item to be dispatched at S3 resume time. This will initially create a Script
  entry in Boot Services memory.  At the same time, the entry point of the PCH S3 support
  image is returned to be used in subsequent boot script save calls.

  @param[in] This                       Pointer to the protocol instance.
  @param[in] DispatchItem               The item to be dispatched.
  @param[out] S3DispatchEntryPoint      The entry point of the PCH S3 support image.

  @retval EFI_STATUS                    Successfully completed.
  @retval EFI_OUT_OF_RESOURCES          Out of resources.
**/
EFI_STATUS
EFIAPI
PchS3SetDispatchItem (
  IN     EFI_PCH_S3_SUPPORT_PROTOCOL   *This,
  IN     EFI_PCH_S3_DISPATCH_ITEM      *InputDispatchItem,
  OUT    EFI_PHYSICAL_ADDRESS          *S3DispatchEntryPoint
  )
{
  EFI_STATUS                Status;
  UINT32                    TypeSize;
  UINT32                    ParameterSize;
  UINT32                    Size;
  UINT8                     *CurrentPos;

  DEBUG ((EFI_D_INFO, "PchS3SetDispatchItem() Start\n"));

  Status = EFI_SUCCESS;

  DEBUG ((EFI_D_INFO, "Dispatch Item Address: 0x%x; Dispatch Item Type: %x\n", (UINTN)InputDispatchItem, (UINTN)InputDispatchItem->ItemType.Value));

  ///
  /// Calculate the size required;
  /// ** Always round up to be 8 byte aligned as the script is initially created from 64-bit code in DXE
  ///
  switch (InputDispatchItem->ItemType.Value) {
  case PchS3ItemTypeSendCodecCommand:
    ParameterSize = QWORD_ALIGNED_SIZE(EFI_PCH_S3_PARAMETER_SEND_CODEC_COMMAND);
    break;

  case PchS3ItemTypeInitPcieRootPortDownstream:
    ParameterSize = QWORD_ALIGNED_SIZE(EFI_PCH_S3_PARAMETER_INIT_PCIE_ROOT_PORT_DOWNSTREAM);
    break;

  case PchS3ItemTypePcieSetPm:
    ParameterSize = QWORD_ALIGNED_SIZE(EFI_PCH_S3_PARAMETER_PCIE_SET_PM);
    break;

  case PchS3ItemTypeProgramIobp:
    ParameterSize = QWORD_ALIGNED_SIZE(EFI_PCH_S3_PARAMETER_PROG_IOBP);
    break;

  default:
    ParameterSize = 0;
    DEBUG ((EFI_D_INFO, "Unrecognized Custom Dispatch Type\n"));
    ASSERT (FALSE);
    return EFI_INVALID_PARAMETER;
  }
  
  ///
  /// Round up TypeSize to be 8 byte aligned
  ///
  TypeSize  = QWORD_ALIGNED_SIZE (EFI_PCH_S3_DISPATCH_ITEM_TYPE);

  ///
  /// Total size is TypeSize + ParameterSize
  ///
  Size = TypeSize + ParameterSize;

  if (mPchS3CustomDispatchScript->BufferSpaceRemaining < Size) {
    DEBUG ((EFI_D_INFO, "Space remaining in Dispatch Script buffer is too small\n"));
    ASSERT (FALSE);
    return EFI_OUT_OF_RESOURCES;
  }

  if (mPchS3CustomDispatchScript->NextDispatchItem == NULL) {
    DEBUG ((EFI_D_INFO, "S3 Support Protocol has been unregistered.  Error.\n"));
    ASSERT (FALSE);
    return EFI_ACCESS_DENIED;
  }

  ///
  /// Store the dispatch type and dispatch parameter
  ///
  CurrentPos  = mPchS3CustomDispatchScript->NextDispatchItem;
  *(EFI_PCH_S3_DISPATCH_ITEM_TYPE *)CurrentPos = InputDispatchItem->ItemType.Value;
  CurrentPos += TypeSize;
  CopyMem (CurrentPos, InputDispatchItem->Parameter, ParameterSize);

  ///
  /// Move the pointer to the NextDispatchItem ahead to free space in our buffer
  /// and decrement the space remaining data
  ///
  mPchS3CustomDispatchScript->NextDispatchItem += Size;
  mPchS3CustomDispatchScript->BufferSpaceRemaining -= Size;

  ///
  /// Return the S3 Image's entry point
  ///
  *S3DispatchEntryPoint = mPchS3ImageEntryPoint;

  DEBUG ((EFI_D_INFO, "PchS3SetDispatchItem() End\n"));

  return Status;
}
  

/**
  Perform the EFI_PCH_S3_SUPPORT_SMM_PROTOCOL IO Trap to invoke DispatchArray data copy and
  IO Trap Unregister.

  @param[in] This                       Pointer to the protocol instance.

  @retval EFI_SUCCESS                   Successfully completed.
**/
EFI_STATUS
EFIAPI
S3SupportReadyToLock(
  IN    EFI_PCH_S3_SUPPORT_PROTOCOL   *This
  )
{
  EFI_STATUS                        Status;


  DEBUG ((EFI_D_INFO, "S3SupportExitPmAuthCallback() Start\n"));

  Status = EFI_SUCCESS;

  DEBUG ((EFI_D_INFO, "Invoke the S3 Support IO Trap: 0x%x\n", mPchS3SupportSmmProtocol.PchS3SupportIoTrap));

  ///
  /// Invoke the SMM IO Trap Handler for invoking the data copy to SMRAM and unregistration of the IO Trap
  ///
  IoWrite32 (mPchS3SupportSmmProtocol.PchS3SupportIoTrap, 0);

  if (mImageHandle != NULL)
  {
    DEBUG ((EFI_D_INFO, "Uninstall the S3 Support Protocol\n", mPchS3SupportSmmProtocol.PchS3SupportIoTrap));

    Status = gBS->UninstallMultipleProtocolInterfaces (
                  mImageHandle,
                  &gEfiPchS3SupportProtocolGuid,
                  &mPchS3SupportProtocol,
                  NULL
                  );
    ASSERT_EFI_ERROR (Status);
  }
  DEBUG ((EFI_D_INFO, "S3SupportExitPmAuthCallback() End\n"));

  return Status;
}

/**
  Initialize the Pch S3 Custom Script memory area.  This will later be transferred to SMRAM.
  
  @param[in] VOID

  @retval EFI_SUCCESS                   Successfully completed.
  @retval EFI_OUT_OF_RESOURCES          Not enough space was available to allocate for the BS memory required.
**/
EFI_STATUS
InitializePchS3CustomScriptMemory (
  VOID
  )
{
  EFI_STATUS            Status;
  EFI_PHYSICAL_ADDRESS  Address;

  ///
  /// Allocate Boot Services memory for the initial copy of the PCH S3 Custom Dispatch Script
  ///
  Status = (gBS->AllocatePool)(
    EfiBootServicesData, 
    EFI_PAGE_SIZE, 
    &(VOID *)Address);
  if (!EFI_ERROR (Status)) {

    mPchS3CustomDispatchScript = (EFI_PCH_S3_DISPATCH_ARRAY *)(UINTN)Address;

    ///
    /// Initialize the DispatchScriptArray
    /// Ensure to account for the HOB space that will be needed for moving the data from SMRAM to normal
    /// memory during S3 resume in the MaximumBufferSize parameter.
    ///
    mPchS3CustomDispatchScript->PchS3CustomScriptGuid = gS3SupportSmramDataGuid;
    mPchS3CustomDispatchScript->MaximumBufferSize = EFI_PAGE_SIZE - QWORD_ALIGNED_SIZE(EFI_HOB_GUID_TYPE);
    mPchS3CustomDispatchScript->BufferSpaceRemaining = mPchS3CustomDispatchScript->MaximumBufferSize - QWORD_ALIGNED_SIZE(EFI_PCH_S3_DISPATCH_ARRAY);
    mPchS3CustomDispatchScript->NextDispatchItem = (UINT8*)mPchS3CustomDispatchScript + QWORD_ALIGNED_SIZE(EFI_PCH_S3_DISPATCH_ARRAY);
  }

  return Status;
}


/**
  Load the entry point address of the PCHS3Peim from the HOB that it generated during the PEI phase of POST

  @param[out] ImageEntryPoint     The ImageEntryPoint after success loading

  @retval EFI_STATUS
**/
EFI_STATUS
LoadPchS3ImageEntryPoint (
  OUT   UINT32          *ImageEntryPoint
  )
{
  EFI_STATUS                      Status;
  S3_SUPPORT_HOB                  *S3SupportHob;
//  EFI_SPI_DATA_PROTOCOL           *SpiDataInterface;

  DEBUG ((EFI_D_INFO, "LoadPchS3ImageEntryPoint() Start\n"));

  Status = EFI_SUCCESS;
  S3SupportHob = NULL;
  *ImageEntryPoint = 0;

  //
  // Search for the S3SupportHob
  //
  S3SupportHob = GetFirstGuidHob(&gS3SupportHobGuid);
  if (S3SupportHob == NULL) {
    DEBUG ((EFI_D_INFO, "S3SupportHob not found.\n"));
    ASSERT_EFI_ERROR (Status);
    return Status;
  }

  //
  // Find the SPI protocol and save the pointer.
  //
//  Status = gBS->LocateProtocol (&gEfiSpiDataProtocolGuid, NULL, &SpiDataInterface);
//  if (EFI_ERROR (Status)) {
//    DEBUG ((EFI_D_ERROR, "ERROR - Spi LocateProtocol failed!\n"));
//    return Status;
//  }

  ///
  /// If the PCH S3 PEIM is not located in flash, fail
  ///
// AMI_OVERRIDE, [ EIP217847 ] >>>
//  if (S3SupportHob->PchS3PeimEntryPoint < SpiDataInterface->BiosStartMemoryAddress ||
//      S3SupportHob->PchS3PeimEntryPoint > SpiDataInterface->BiosStartMemoryAddress + SpiDataInterface->BiosSize)
  if ((S3SupportHob->PchS3PeimEntryPoint < (0xFFFFFFFF - FLASH_SIZE + 1)) || (S3SupportHob->PchS3PeimEntryPoint > 0xFFFFFFFF))
// AMI_OVERRIDE, [ EIP217847 ] <<<
  {
    DEBUG ((EFI_D_INFO, "PchS3Image is NOT located in Flash. Current Entry Point: %x\n", S3SupportHob->PchS3PeimEntryPoint));
    
    ASSERT(FALSE);
    return EFI_SECURITY_VIOLATION;
  }

  // Load the HOB data from PEI execution which contains the entry point of the PCHS3Peim from Flash
  *ImageEntryPoint = S3SupportHob->PchS3PeimEntryPoint;

  DEBUG ((EFI_D_INFO, "PchS3Image is Located in Flash at Entry Point: %x\n", S3SupportHob->PchS3PeimEntryPoint));
  DEBUG ((EFI_D_INFO, "LoadPchS3ImageEntryPoint() End\n"));
  
  return Status;
}