summaryrefslogtreecommitdiff
path: root/Board/EM/SwitchableGraphics/SgTpv/AcpiS3Save/SgTpvAcpiS3Save.c
blob: c179dbc1498c5cc5d24e74a6d38ae435d29eb876 (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2012, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**        5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093         **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************
//**********************************************************************
// $Header: /Alaska/SOURCE/Modules/SharkBayRefCodes/SwitchableGraphics/Sg TPV/Sg Acpi S3 Save/SgTpvAcpiS3Save.c 6     7/16/13 5:04a Joshchou $
//
// $Revision: 6 $
//
// $Date: 7/16/13 5:04a $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/SharkBayRefCodes/SwitchableGraphics/Sg TPV/Sg Acpi S3 Save/SgTpvAcpiS3Save.c $
// 
// 6     7/16/13 5:04a Joshchou
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Save the dGPU ssid to SANV for restore.
// 
// 5     3/21/13 3:39a Joshchou
// 
// 4     1/15/13 5:04a Joshchou
// [TAG]  		EIP107237
// [Category]  	New Feature
// [Description]  	Support SG on ULT platform.
// 
// 3     10/16/12 4:45a Joshchou
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Update for SA and ACPI RC 0.7.1
// [Files]  		SgTpvAcpiS3Save.c
// 
// 2     9/09/12 10:53p Joshchou
// [TAG]  		None
// [Category]  	Improvement
// [Description]  	Tpv module support for sharkbay.
// [Files]  		SgTpvAcpiS3Save.mak
// SgTpvAcpiS3Save.c
// SgTpvAcpiS3Save.dxs
// 
// 1     6/27/11 5:27a Alanlin
// [TAG]  		EIP61848
// [Category]  	New Feature
// [Description]  	Initial check-in.Integrated SwitchableGraphics Intel
// Reference code 0.6.0
// [Files]  		SgTpvAcpiS3Save.cif
// SgTpvAcpiS3Save.sdl
// SgTpvAcpiS3Save.mak
// SgTpvAcpiS3Save.c
// SgTpvAcpiS3Save.dxs
// 
// 
// 
//**********************************************************************
//<AMI_FHDR_START>
//----------------------------------------------------------------------------
//
// Name:		SgTpvAcpiS3Save.c
//
// Description:	This file will save SG TPV related offset date for S3 resume restore.
//
//----------------------------------------------------------------------------
//<AMI_FHDR_END>
#include <Token.h>
#include <AmiCspLib.h>
#include <AmiDxeLib.h>
#include <Setup.h>
#include <PCI.h>
#include <SaGlobalNvsArea\SaGlobalNvsArea.h>
#include "CpuRegs.h"
//#include "CpuPlatformLib.h"

#include "PchRegsPcie.h"

//----------------------------------------------------------------------------
// Constant, Macro and Type Definition(s)
//----------------------------------------------------------------------------
// Constant Definition(s)
#define NVIDIA_VID              0x10DE
#define NVOPT_SSID_OFFSET       0x40

#define AMD_SVID_OFFSET         0x4C
#define AMD_SDID_OFFSET         0x4E
#define AMD_VID                 0x1002

// Function Prototype(s)
VOID SgTpvAcpiS3Save (
    IN EFI_EVENT        Event,
    IN VOID             *Context
);

//----------------------------------------------------------------------------
// Variable and External Declaration(s)
//----------------------------------------------------------------------------
// Variable Declaration(s)
EFI_EVENT                       gEvent;
SYSTEM_AGENT_GLOBAL_NVS_AREA_PROTOCOL    *SaGlobalNvsArea;

// GUID Definition(s)
EFI_GUID gEfiPciIoProtocolGuid           = EFI_PCI_IO_PROTOCOL_GUID;
EFI_GUID gSetupGuid                      = SETUP_GUID;
EFI_GUID gSaGlobalNvsAreaProtocolGuid    = SYSTEM_AGENT_GLOBAL_NVS_AREA_PROTOCOL_GUID;
EFI_GUID gEfiBootScriptSaveGuid            = EFI_BOOT_SCRIPT_SAVE_GUID;

CPU_FAMILY            CpuFamilyId;



//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure: SgTpvAcpiS3Save_Init
//
// Description: This function is the entry point for this DXE. This function
//              will save SG TPV related offset date for S3 resume restore.
//
// Input:       ImageHandle - Image handle
//              SystemTable - Pointer to the system table
//
// Output:      Return Status based on errors that occurred while waiting for
//              time to expire.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
SgTpvAcpiS3Save_Init (
  IN EFI_HANDLE       ImageHandle,
  IN EFI_SYSTEM_TABLE *SystemTable
  )
{
    EFI_STATUS                    Status;
    UINTN              		      VariableSize = NULL;
    SETUP_DATA      		      *SetupData = NULL;



    InitAmiLib(ImageHandle,SystemTable);

  // Initialize Setup Data 
	Status = GetEfiVariable(
		L"Setup",
		&gSetupGuid,
		NULL,
		&VariableSize,
		&SetupData
	);

   
  // If Select SG mode
  if (SetupData->PrimaryDisplay == 4){

    Status = CreateReadyToBootEvent( TPL_NOTIFY, SgTpvAcpiS3Save, \
                                     NULL, &gEvent );

    ASSERT_EFI_ERROR(Status);

  }

    return EFI_SUCCESS;
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   SgTpvAcpiS3Save
//
// Description: This callback function is called when ReadyToBoot is
//              installed.
//
// Input:       Event   - Event of callback
//              Context - Context of callback.
//
// Output:      EFI_SUCCESS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID SgTpvAcpiS3Save (
    IN EFI_EVENT        Event,
    IN VOID             *Context )
{

    EFI_STATUS              Status;
    UINT16                  VendorId;
    UINT8                   EndpointBus;
    UINT16                  Data16;
    UINT32                  Data32;
    UINT16                  Count;
    UINTN                   Index;
    UINTN                   VariableSize = NULL;
    UINT32                  RegEax, RegEbx, RegEcx, RegEdx;
    SETUP_DATA      		*SetupData = NULL;
    EFI_BOOT_SCRIPT_SAVE_PROTOCOL   *BootScriptSave;
    UINT8                 RootPortDev;
    UINT8                 RootPortFun;


  TRACE((-1, "==============SgTpvAcpiS3Save==========\n"));
    // Initialize Setup Data 
	Status = GetEfiVariable(
		L"Setup",
		&gSetupGuid,
		NULL,
		&VariableSize,
		&SetupData
	);
    if (EFI_ERROR (Status)) {
      return ;
    }
    //
    //  Locate the SA Global NVS Protocol.
    //
    Status = pBS->LocateProtocol (
                    &gSaGlobalNvsAreaProtocolGuid,
                    NULL,
                    &SaGlobalNvsArea
                    );
    if (EFI_ERROR (Status)) {
      return ;
    }

    Status = pBS->LocateProtocol( \
                    &gEfiBootScriptSaveGuid, \
                    NULL, \
                    &BootScriptSave
                    );
    if (EFI_ERROR (Status)) {
      return ;
    }

   CPULib_CpuID(1, &RegEax, &RegEbx, &RegEcx, &RegEdx);
   CpuFamilyId = (RegEax & CPUID_FULL_FAMILY_MODEL);

   TRACE((-1, "CpuFamilyId = 0x%x\n",CpuFamilyId));
   RootPortDev = 1;
   RootPortFun = 0;
   
    if (CpuFamilyId == EnumCpuHswUlt) {
      /// For SwitchableGraphics support the dGPU is present on PCH RootPort
      RootPortDev = PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS;
      RootPortFun = SG_ULT_PORT_FUNC;
     }

    //EndpointBus  = READ_PCI8 (0, 1, 0, PCI_SBUS);
    EndpointBus = READ_PCI8 ( 0, RootPortDev, RootPortFun, PCI_SBUS);
    //
    //  Endpoint Device Not found
    //
    if (EndpointBus == 0xFF) { 
       return ;
    }

    VendorId = READ_PCI16 (EndpointBus, 0, 0, PCI_VID); 
    //
    //  if Device Not found or Devide is not AMD and Nvidia VGA device
    //
    if (VendorId == 0xFFFF || ((VendorId != NVIDIA_VID) && (VendorId != AMD_VID))) { 
       return ;
    }


    //
    // Store the Root port Bus assignemnt for S3 resume path
    //

    Data32 = READ_PCI32 (0, RootPortDev, RootPortFun, PCI_PBUS);
    BOOT_SCRIPT_S3_MEM_WRITE_MACRO (
    BootScriptSave, 
    EfiBootScriptWidthUint32, 
    NB_PCIE_CFG_ADDRESS(0, RootPortDev, RootPortFun, PCI_PBUS),
    1,
    &Data32
    );

    Data16 = READ_PCI16 (0, RootPortDev, RootPortFun, PCI_BAR3);
    BOOT_SCRIPT_S3_MEM_WRITE_MACRO (
    BootScriptSave, 
    EfiBootScriptWidthUint16, 
    NB_PCIE_CFG_ADDRESS(0, RootPortDev, RootPortFun, PCI_BAR3),
    1,
    &Data16
    );

    //
    // Store the Generic PCI config space of dGPU for S3 resume path 
    //

    for (Count = 4; Count < 0x40; Count+=4) {
    Data32 = READ_PCI32 (EndpointBus, 0, 0, Count);
    BOOT_SCRIPT_S3_MEM_WRITE_MACRO (
    BootScriptSave, 
    EfiBootScriptWidthUint32, 
    NB_PCIE_CFG_ADDRESS(EndpointBus, 0, 0, Count),
    1,
    &Data32
    );
    } 

  switch(VendorId){
    case NVIDIA_VID:
        //
        // if PEG is NVIDIA and supports Muxless mode
        //
      if (SetupData->SgMuxMode == 2){  


        //
        // Set a unique SSID on the Nv Optimus Graphics
        //
        Data32 = READ_PCI32 (0, 2, 0, PCI_SVID);
        WRITE_PCI32 (EndpointBus, 0, 0, NVOPT_SSID_OFFSET, Data32);

        SaGlobalNvsArea->Area->DgpuSsid = READ_PCI32 (EndpointBus, 0, 0, NVOPT_SSID_OFFSET); // DEBUG
        TRACE((-1, "DgpuSsid in SgTpvAcpiS3Save =0x%X\n", SaGlobalNvsArea->Area->DgpuSsid));

        BOOT_SCRIPT_S3_MEM_WRITE_MACRO (
        BootScriptSave, 
        EfiBootScriptWidthUint32, 
        NB_PCIE_CFG_ADDRESS(EndpointBus, 0, 0, NVOPT_SSID_OFFSET),
        1,
        &Data32
        );

        //
        // Disable Peg Endpoint Function 1 (HDMI Support)
        //
        Data16 = READ_PCI16(EndpointBus, 0, 0, PCI_CMD);
        Data16 = (Data16 & (!(BIT0+BIT1+BIT2)));
        Data16 = (Data16 | (BIT1+BIT2));
        WRITE_PCI16 (EndpointBus, 0, 0, PCI_CMD, Data16);

        Data32 = READ_PCI32 (EndpointBus, 0, 0, PCI_BAR0); // Resd Bar 0 MMIO address
        Data32 += 0x88488;
        Index = (UINT32)Data32;
        *(UINT32*)(Data32) &= ~BIT25;       // Clear Bit 25
        // Keep HDA enabled on cold boots. Optimus Gfx driver shall disable it if not needed
        // or read from CMOS...
        Data32 = ((SaGlobalNvsArea->Area->SgFeatureList & 0x2) << 24); // dGPUAudioCodec bit2
        BOOT_SCRIPT_S3_MEM_WRITE_MACRO (
        BootScriptSave, 
        EfiBootScriptWidthUint32, 
        (UINTN)(Index),  
        1,
        &Data32
        );

      }
    break;
    case AMD_VID:
        //
        // if PEG is AMD
        //
        //
        // Set a unique SSID on the AMD MXM
        //
        Data32 = READ_PCI32 (0, 2, 0, PCI_SVID);
        WRITE_PCI32 (EndpointBus, 0, 0, AMD_SVID_OFFSET, Data32);

        SaGlobalNvsArea->Area->DgpuSsid = READ_PCI32 (EndpointBus, 0, 0, AMD_SVID_OFFSET); // DEBUG
        TRACE((-1, "DgpuSsid in SgTpvAcpiS3Save =0x%X\n", SaGlobalNvsArea->Area->DgpuSsid));

        BOOT_SCRIPT_S3_MEM_WRITE_MACRO (
        BootScriptSave, 
        EfiBootScriptWidthUint32, 
        NB_PCIE_CFG_ADDRESS(EndpointBus, 0, 0, AMD_SVID_OFFSET),
        1,
        &Data32
        );

    break;
    default:
        //
        // either means the Device ID is not on the list of devices we know - we return from this function
    break;
        //
  }

        pBS->CloseEvent(Event); 
}



//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2012 American Megatrends, Inc.          **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**        5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093         **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************