summaryrefslogtreecommitdiff
path: root/Board/NB/NBDXEBoard.c
blob: 5fbe360a329e7182ef7909a144e87b549fc42e1d (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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************

//*************************************************************************
// $Header: /Alaska/BIN/Chipset/Intel/NorthBridge/Haswell/Intel SystemAgent NB Board/NBDXEBoard.c 7     5/13/14 10:37p Dennisliu $
//
// $Revision: 7 $
//
// $Date: 5/13/14 10:37p $
//*************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/BIN/Chipset/Intel/NorthBridge/Haswell/Intel SystemAgent NB Board/NBDXEBoard.c $
// 
// 7     5/13/14 10:37p Dennisliu
// [TAG]           EIP167027
// [Category]      Improvement
// [Description]   [SharkBay Aptio4]Variable's attribute needs to be
// reviewed by SA component driver
// [Files]         NBDXEBoard.c; IntelSaGopSetup.c; IntelSaGopPolicy.c;
// NBDxe.c; NbPciCSP.c; PciHostBridge.c;
//
// 6     6/28/13 7:55a Jeffch
//  [TAG]          None
//  [Severity]     Bug Fix
//  [Description]  Fix Boot to OS will BSOD when disable
// SwitchableGraphics_SUPPORT.
//  [Symptom]      update asl name XBAS in NBDXEBoard.c.
//  [RootCause]    RC will not update asl name XBAS when disable
// SwitchableGraphics_SUPPORT.
//  [Files]        NBDxeBoard.c
//
// 5     1/28/13 3:46a Jeffch
// [TAG]          None
// [Severity]     Spec update
// [Description]  Update SA RC 1.0.
// [Files]        NBPei.c; GetSetupData.c; NbSetupData.h; NBDXE.c;
//                  NB.sd; NB.uni; NBDxeBoard.c
//
// 4     12/14/12 4:51a Jeffch
// [TAG]          EIP106709
// [Severity]     Important
// [Description]  Support PCIE Primary display.
// [Files]        NB.sd; NBDXEboard.c; NB.uni;
// GetSetupData.c;NbSetupData.h;
//
// 3     10/30/12 7:17a Jeffch
// [TAG]          None
// [Severity]     Important
// [Description]  Update SA RC 0.72.
// [Files]        NB.sdl; NBDXEboard.c
//
// 2     10/14/12 12:42a Jeffch
// [TAG]          None
// [Severity]     Important
// [Description]  Update by XTU4.0.
// [Files]        NB.h, GetSetupData.c, NbSetupData.h
// [TAG]          None
// [Severity]     Important
// [Description]  Follow Update by Mahobay.
// [Files]        NB.sdl, NB.mak.c; NBDXEBoard.c
//
// 1     2/08/12 4:33a Yurenlai
// Intel Haswell/NB eChipset initially releases.
//
//*************************************************************************
//<AMI_FHDR_START>
//
// Name:        NBDXEBoard.C
//
// Description: This file contains DXE stage board component code for
//              Template NB
//
//<AMI_FHDR_END>
//*************************************************************************

//----------------------------------------------------------------------------
// Include(s)
//----------------------------------------------------------------------------

#include <Efi.h>
#include <Pei.h>
#include <token.h>
#include <Setup.h>
#include <AmiLib.h>
#include <AmiDxeLib.h>
#include <AmiCspLib.h>
#include <Protocol\NBMemInfo.h>
#include <Protocol\MpService.h>

#if SMBIOS_SUPPORT
#include <Protocol\SmbiosDynamicData.h>
#endif

#if CSM_SUPPORT
#include <Protocol\CsmPlatform.h>
#include <Protocol\LegacyBiosPlatform.h>
#endif
#define _SA_COMMON_DEFINITIONS_H_
#include <Protocol\SaPlatformPolicy\SaPlatformPolicy.h>
#include <SaGlobalNvsArea\SaGlobalNvsArea.h>
//----------------------------------------------------------------------------
// Constant, Macro and Type Definition(s)
//----------------------------------------------------------------------------
// Constant Definition(s)

// Macro Definition(s)


// Type Definition(s)

// Function Prototype(s)

// Protocols that are installed

//----------------------------------------------------------------------------
// Variable and External Declaration(s)
//----------------------------------------------------------------------------
// Variable Declaration(s)

// GUID Definition(s)
EFI_GUID gSaGlobalNvsAreaProtocolGuid    = SYSTEM_AGENT_GLOBAL_NVS_AREA_PROTOCOL_GUID;
EFI_GUID mDxePlatformSaPolicyGuid = DXE_PLATFORM_SA_POLICY_GUID;
// Protocol Definition(s)
SYSTEM_AGENT_GLOBAL_NVS_AREA_PROTOCOL    *gSaGlobalNvsArea;
DXE_PLATFORM_SA_POLICY_PROTOCOL          mDxePlatformSaPolicy;
// External Declaration(s)
NB_SETUP_DATA *gNbSetupData;
// Function Definition(s)
VOID NbSaGlobalNvsAreaNotify (
    IN EFI_EVENT        Event,
    IN VOID             *Context
);
//----------------------------------------------------------------------------

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   NBDXE_BoardInit
//
// Description: This function initializes the board specific component in
//              in the chipset north bridge
//
// 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 NBDXE_BoardInit (
    IN EFI_HANDLE           ImageHandle,
    IN EFI_SYSTEM_TABLE     *SystemTable,
    IN NB_SETUP_DATA        *SetupData )
{
    EFI_STATUS         Status = EFI_SUCCESS;
    EFI_EVENT           NbSaGlobalNvsAreaEvent;
    VOID                *NbSaGlobalNvsAreaReg;

    InitAmiLib(ImageHandle, SystemTable);

    gNbSetupData = SetupData;

    //NbSetupdata Pass to SaGlobalNvsArea.
    Status = pBS->CreateEvent (
                    EFI_EVENT_NOTIFY_SIGNAL,
                    TPL_CALLBACK,
                    NbSaGlobalNvsAreaNotify,
                    NULL,
                    &NbSaGlobalNvsAreaEvent
                    );

    if (!EFI_ERROR (Status)) {
      Status = pBS->RegisterProtocolNotify (
                      &gSaGlobalNvsAreaProtocolGuid,
                      NbSaGlobalNvsAreaEvent,
                      &NbSaGlobalNvsAreaReg
                      );
    }


    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   NbSaGlobalNvsAreaNotify
//
// Description: This callback function is called when a SaGlobalNvsArea Protocol is
//              installed.
//
// Input:       Event   - Event of callback
//              Context - Context of callback.
//
// Output:      EFI_SUCCESS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID NbSaGlobalNvsAreaNotify (
    IN EFI_EVENT        Event,
    IN VOID             *Context )
{
  EFI_STATUS         Status = EFI_SUCCESS;

  ///
  ///  Locate the SA Global NVS Protocol.
  ///
  Status = pBS->LocateProtocol (
                  &gSaGlobalNvsAreaProtocolGuid,
                  NULL,
                  (VOID **) &gSaGlobalNvsArea
                  );
  if (!(EFI_ERROR (Status)))
  {

    ///
    /// Get the platform setup policy.
    ///
    Status = pBS->LocateProtocol (&mDxePlatformSaPolicyGuid, NULL, (VOID **) &mDxePlatformSaPolicy);
    ASSERT_EFI_ERROR (Status);

    gSaGlobalNvsArea->Area->IgdBootType                 = gNbSetupData->IgdBootType;
    gSaGlobalNvsArea->Area->IgdPanelType                = gNbSetupData->LcdPanelType;
    gSaGlobalNvsArea->Area->IgdPanelScaling             = gNbSetupData->LcdPanelScaling;
    gSaGlobalNvsArea->Area->IgdSciSmiMode               = 0;   // 0=Enable, 1= Disabled
//-    gSaGlobalNvsArea->Area->IgdTvFormat                 = NbSetupData->IgdTV1Standard;
//-    gSaGlobalNvsArea->Area->IgdTvMinor                  = NbSetupData->IgdTV2Standard;
    gSaGlobalNvsArea->Area->IgdSscConfig                = gNbSetupData->IgdLcdSSCC;
    gSaGlobalNvsArea->Area->IgdBiaConfig                = gNbSetupData->IgdLcdIBia;

    gSaGlobalNvsArea->Area->BacklightControlSupport     = gNbSetupData->IgdLcdBlc;
    gSaGlobalNvsArea->Area->AlsEnable                   = gNbSetupData->AlsEnable;
    gSaGlobalNvsArea->Area->IgdDvmtMemSize              = gNbSetupData->IgdDvmt50TotalAlloc;
    gSaGlobalNvsArea->Area->GfxTurboIMON                = gNbSetupData->GfxTurboIMON;

    // Get SFF power mode platform data for the IGD driver.  Flip the bit (bitwise xor)
    // since Setup value is opposite of NVS and IGD OpRegion value.
    gSaGlobalNvsArea->Area->IgdPowerConservation        = gNbSetupData->LowPowerMode ^ BIT0;
    gSaGlobalNvsArea->Area->XPcieCfgBaseAddress         = (UINT32)NB_PCIE_CFG_ADDRESS (0, 0, 0, 0);

    //
    // Give the full brightness as the initial value
    //
    gSaGlobalNvsArea->Area->BrightnessPercentage        = 100;

    if(gNbSetupData->AlsEnable) {
      if (gSaGlobalNvsArea->Area->IgdBiaConfig == 0) {
        //
        // Re-change the ALS to disable according to setup.
        //
        gSaGlobalNvsArea->Area->AlsEnable = 0;
      } else if (gSaGlobalNvsArea->Area->IgdBiaConfig < 6) {
        gSaGlobalNvsArea->Area->AlsEnable = 2;
        gSaGlobalNvsArea->Area->AlsAdjustmentFactor = gSaGlobalNvsArea->Area->IgdBiaConfig * 20;
      } else if (gSaGlobalNvsArea->Area->IgdBiaConfig == 6) {
        //
        // The default value from VBT
        //
        gSaGlobalNvsArea->Area->AlsAdjustmentFactor = 100;
        gSaGlobalNvsArea->Area->AlsEnable = 2;
      }
    }

    // Update DeviceIds
    gSaGlobalNvsArea->Area->DeviceId1                   = DeviceID1;
    gSaGlobalNvsArea->Area->DeviceId2                   = DeviceID2;
    gSaGlobalNvsArea->Area->DeviceId3                   = DeviceID3;
    gSaGlobalNvsArea->Area->DeviceId4                   = DeviceID4;
    gSaGlobalNvsArea->Area->DeviceId5                   = DeviceID5;
    gSaGlobalNvsArea->Area->DeviceId6                   = DeviceID6;
    gSaGlobalNvsArea->Area->DeviceId7                   = DeviceID7;
    gSaGlobalNvsArea->Area->DeviceId8                   = DeviceID8;
    gSaGlobalNvsArea->Area->NumberOfValidDeviceId       = ValidDeviceIDs;
    gSaGlobalNvsArea->Area->CurrentDeviceList           = 0x0f;
    gSaGlobalNvsArea->Area->PreviousDeviceList          = 0x0f;

    gSaGlobalNvsArea->Area->Peg0LtrEnable               = mDxePlatformSaPolicy.PcieConfig->PegPwrOpt[0].LtrEnable;
    gSaGlobalNvsArea->Area->Peg0ObffEnable              = mDxePlatformSaPolicy.PcieConfig->PegPwrOpt[0].ObffEnable;
    gSaGlobalNvsArea->Area->Peg0ObffEnable              = mDxePlatformSaPolicy.PcieConfig->PegPwrOpt[0].ObffEnable;
    gSaGlobalNvsArea->Area->Peg1LtrEnable               = mDxePlatformSaPolicy.PcieConfig->PegPwrOpt[1].LtrEnable;
    gSaGlobalNvsArea->Area->Peg1ObffEnable              = mDxePlatformSaPolicy.PcieConfig->PegPwrOpt[1].ObffEnable;
    gSaGlobalNvsArea->Area->Peg2LtrEnable               = mDxePlatformSaPolicy.PcieConfig->PegPwrOpt[2].LtrEnable;
    gSaGlobalNvsArea->Area->Peg2ObffEnable              = mDxePlatformSaPolicy.PcieConfig->PegPwrOpt[2].ObffEnable;
    gSaGlobalNvsArea->Area->PegLtrMaxSnoopLatency       = mDxePlatformSaPolicy.PcieConfig->PegPwrOpt[0].LtrMaxSnoopLatency;
    gSaGlobalNvsArea->Area->PegLtrMaxNoSnoopLatency     = mDxePlatformSaPolicy.PcieConfig->PegPwrOpt[0].LtrMaxNoSnoopLatency;
    gSaGlobalNvsArea->Area->Peg0PowerDownUnusedBundles  = gNbSetupData->PowerDownUnusedBundles[0];
    gSaGlobalNvsArea->Area->Peg1PowerDownUnusedBundles  = gNbSetupData->PowerDownUnusedBundles[1];
    gSaGlobalNvsArea->Area->Peg2PowerDownUnusedBundles  = gNbSetupData->PowerDownUnusedBundles[2];
    gSaGlobalNvsArea->Area->IgdState = 1;

    // If SCI mode is disabled in setup or IGD is disabled return
    if ((READ_PCI32_IGD (0) == 0xFFFFFFFF)) {
        gSaGlobalNvsArea->Area->IgdState = 0;
    }

    gSaGlobalNvsArea->Area->LidState       = 1;

  }

  // Kill event
  pBS->CloseEvent(Event);

}

#if CSM_SUPPORT

typedef struct  {
    UINT8        PegBus;
    UINT8        PegDev;
    UINT8        PegFun;
    EFI_HANDLE   Handle;
} NB_PEG_DEVICES_STRUCT;

NB_PEG_DEVICES_STRUCT NBPegVgaList[] =
{
  {NB_BUS, PCIEBRN_DEV6, PCIEBRN_FUN,  NULL},
  {NB_BUS, PCIEBRN_DEV,  PCIEBRN_FUN,  NULL},
  {NB_BUS, PCIEBRN_DEV,  PCIEBRN_FUN1, NULL},
  {NB_BUS, PCIEBRN_DEV,  PCIEBRN_FUN2, NULL},
};
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Name:    NBGetPlatformHandle
//
// Description: Returns a buffer of handles for the requested subfunction.
//
// Input:
//  This            Indicates the EFI_LEGACY_BIOS_PLATFORM_PROTOCOL instance.
//  Mode            Specifies what handle to return.
//                      GetVgaHandle
//                      GetIdeHandle
//                      GetIsaBusHandle
//                      GetUsbHandle
//  Type            Handle Modifier - Mode specific
//  HandleBuffer    Pointer to buffer containing all Handles matching the
//                  specified criteria. Handles are sorted in priority order.
//                  Type EFI_HANDLE is defined in InstallProtocolInterface()
//                  in the EFI 1.10 Specification.
//  HandleCount     Number of handles in HandleBuffer.
//  AdditionalData  Pointer to additional data returned - mode specific..
//
// Output:
//  EFI_SUCCESS The handle is valid.
//  EFI_UNSUPPORTED Mode is not supported on this platform.
//  EFI_NOT_FOUND   The handle is not known.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS
NBGetPlatformHandle (
  IN  EFI_LEGACY_BIOS_PLATFORM_PROTOCOL     *This,
  IN  EFI_GET_PLATFORM_HANDLE_MODE          Mode,
  IN  UINT16                                Type,
  OUT EFI_HANDLE                            **HandleBuffer,
  OUT UINTN                                 *HandleCount,
  OUT VOID                                  OPTIONAL **AdditionalData
)
{
    EFI_STATUS    Status = EFI_SUCCESS;
//------------------------
    switch (Mode) {
        case EfiGetPlatformVgaHandle:{
                UINTN                  sz = 0, i;
                UINT8                  PriVideo=0; //3=AUTO 0=IGD 1=PEG 2=PCI
                UINT8                  PrimaryPeg=0; //0=AUTO 1=PEG11 2=PEG12
                UINT8                  PrimaryPcie=0; //0=AUTO 1=PCIE1 2=PCIE2....
                EFI_HANDLE             *hb, *vh, peg=NULL, peg00=NULL, pcie0=NULL, igd=NULL, pcie=NULL;
                T_ITEM_LIST            VgaList={0,0,NULL};
                PCI_DEV_INFO           *dev;
                EFI_PCI_IO_PROTOCOL    *pciio;
                BOOLEAN                ChangeIgfxToAuto = FALSE;
                BOOLEAN                ChangePegToAuto = FALSE;
                BOOLEAN                ChangepcieToAuto = FALSE;
                BOOLEAN                ChangePrimaryPeg = FALSE;
                BOOLEAN                ChangePrimaryPcie = FALSE;
                NB_SETUP_DATA           *NBSetupData = NULL;
                UINTN                   VariableSize = sizeof(NB_SETUP_DATA);
                //-----------------

                Status = pBS->AllocatePool( EfiBootServicesData, \
                                VariableSize, \
                                &NBSetupData );
                ASSERT_EFI_ERROR(Status);

                GetNbSetupData( pRS, NBSetupData, FALSE );


                PriVideo = NBSetupData->PrimaryDisplay;
                PrimaryPeg  = NBSetupData->PrimaryPeg;
                PrimaryPcie  = NBSetupData->PrimaryPcie;

                // Free memory used for setup data
                pBS->FreePool( NBSetupData );

                //Get all handles with PCI IO Protocol..
                Status=pBS->LocateHandleBuffer(ByProtocol,&gEfiPciIoProtocolGuid,NULL, &sz, &hb);
                if(EFI_ERROR(Status)) return Status;

                for(i=0; i< sz; i++) {

                    Status=pBS->HandleProtocol(hb[i],&gEfiPciIoProtocolGuid,&pciio);
                    if(EFI_ERROR(Status)) {
                        pBS->FreePool(hb);
                        return Status;
                    }

                    dev = (PCI_DEV_INFO*)pciio;
                    //
                    // When a graphics card with internal PCI-PCI bridges (GTX 295) is attached to
                    // PCH PCIe ports video is not displayed.
                    // PCI to PCI bridge has another device with Baseclasscode as 0x03 and  Sub Classcode
                    // as 0x02. And in CSMBsp.c file, it was seeing this as Video controller.
                    //
                    if((dev->Class.BaseClassCode == PCI_CL_DISPLAY) && (dev->Class.SubClassCode == 0x00)) {
                        Status=AppendItemLst(&VgaList, (VOID*)dev);
                        if(EFI_ERROR(Status)) return Status;
                    }
                }

                pBS->FreePool(hb);

                vh=Malloc(sizeof(EFI_HANDLE));
                *vh=NULL;

                for(i=0; i<VgaList.ItemCount; i++) {
                PCI_DEV_INFO               *brg;
                //--------------------------------
                //This is an Easy task we know exactly where it is
                dev = VgaList.Items[i];
                if(igd == NULL) {
                    if((dev->ParentBrg->Type == tPciRootBrg)
                    && (dev->Address.Addr.Device == 2)
                    && (dev->Address.Addr.Function == 0))
                        igd=dev->Handle;
                    }

                //Some Video cards Implement a MULTY FUNCTIONAL Devices;
                //Some - use PCI 2 PCI bridg(es) to multiply itself
                //to Avoid further complications regarding such situation
                //we'll try to handle this situation!
                //We will assume for Primary Video Device Selection that it always
                //will be the function 0 of mulifunctional Device.
                brg = dev->ParentBrg;

                if(peg == NULL) {
                    while(brg->Type == tPci2PciBrg) {
                        if( (brg->DevVenId.VenId == 0x8086 )   //Intel's Vendor ID
                        && (brg->ParentBrg->Type != tPci2PciBrg ) //Must be connected to the HOST
                        && (brg->Address.Addr.Device == 1)     //Device #1
                        && (brg->Address.Addr.Function >= 0)    //Function #0 ~ 2
                        && ( (dev->PciIo.RomImage != NULL )     //Device behind this Bridge must have ROM
                        || (dev->Bar[PCI_MAX_BAR_NO].Type != tBarUnused) )
                        && (dev->Address.Addr.Function == 0 )
                        && (dev->Capab & EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY)
                        && (dev->Capab & EFI_PCI_IO_ATTRIBUTE_VGA_IO) )
                        {
                            if (PrimaryPeg != 0) {
                               if (brg->Address.Addr.Device == 1)
                               {
                                  if (brg->Address.Addr.Function == 1 && PrimaryPeg == 1)
                                     peg = dev->Handle;
                                  else if (brg->Address.Addr.Function == 2 && PrimaryPeg == 2)
                                     peg = dev->Handle;
                                  else if (brg->Address.Addr.Function == 0)
                                     peg00 = dev->Handle;

                               }
                            } else peg=dev->Handle;
                        }

                        if(peg != NULL)break;
                        brg = brg->ParentBrg;
                    } //#### while
                } //#### if(peg == NULL)

                //Do the same for Video that might be behind PCI 2 PCI bridge device 1Eh
                brg=dev->ParentBrg;
                if(pcie == NULL) {
                    while(brg->Type == tPci2PciBrg) {
                        if( (brg->DevVenId.VenId == 0x8086 )    //Intel's Vendor ID
                        && (brg->ParentBrg->Type != tPci2PciBrg )  //Must be connected to the HOST
                        && ((brg->Address.Addr.Device == 0x1C)  // 0:1C:x - PCIe root ports on PCH
                        || (brg->Address.Addr.Device == 0x1E))   // 0:1E:0 - PCI-to-PCI bridge on PCH
                        && ( (dev->PciIo.RomImage != NULL )      //Device must have an Option ROM
                        || (dev->Bar[PCI_MAX_BAR_NO].Type != tBarUnused) )
                        && (dev->Address.Addr.Function == 0 ))
                        {
                            if (PrimaryPcie != 0) {
                               if (brg->Address.Addr.Device == 0x1C)
                               {
                                  if (brg->Address.Addr.Function == 1 && PrimaryPcie == 1)
                                     pcie = dev->Handle;
                                  else if (brg->Address.Addr.Function == 2 && PrimaryPcie == 2)
                                     pcie = dev->Handle;
                                 if (brg->Address.Addr.Function == 3 && PrimaryPcie == 3)
                                     pcie = dev->Handle;
                                 if (brg->Address.Addr.Function == 4 && PrimaryPcie == 4)
                                     pcie = dev->Handle;
                                  else if (brg->Address.Addr.Function == 5 && PrimaryPcie == 5)
                                     pcie = dev->Handle;
                                  else if (brg->Address.Addr.Function == 6 && PrimaryPcie == 6)
                                     pcie = dev->Handle;
                                  else if (brg->Address.Addr.Function == 7 && PrimaryPcie == 7)
                                     pcie = dev->Handle;
                                  else if (brg->Address.Addr.Function == 0)
                                     pcie0 = dev->Handle;

                               }
                            } else pcie=dev->Handle;
                        }

                        if(pcie != NULL) break;
                        brg = brg->ParentBrg;
                    } //#### while
                } //#### if(pcie == NULL)
            } //#### for;

            if (PrimaryPeg != 0 && peg == NULL) {
                peg = peg00;
                ChangePrimaryPeg = TRUE;
            }

            if (PrimaryPcie != 0 && pcie == NULL) {
                pcie = pcie0;
                ChangePrimaryPcie = TRUE;
            }

            //Clear Temp Buffer we had for VGA Handles;
            ClearItemLst(&VgaList, FALSE);
            //3=AUTO 0=IGD 1=PEG 2=pcie
            switch(PriVideo) {
                case 3 : //0 = auto PEG -> pcie -> IGD
                    if(peg) {
                        *vh = peg;
                        break;
                    }

                    if(pcie) {
                        *vh = pcie;
                         break;
                    }

                    if(igd) *vh = igd;
                    break;

                case 4 : //SG: 1=IGD
                case 0 : //1=IGD  b0|d2|f0
                    if(igd) {
                        *vh = igd;
                    } else {
                        ChangeIgfxToAuto = TRUE;
                    }
                    break;

                case 1 : //1=PEG Bridge b0|d1|f0
                    if(peg) {
                        *vh = peg;
                    } else {
                        ChangePegToAuto = TRUE;
                    }
                    break;

                case 2 : //1=PCI2 PCI bridge  b0|d1E|f0
                    if(pcie) {
                        *vh = pcie;
                    } else {
                        ChangepcieToAuto = TRUE;
                    }
                    break;
            } // switch

            if(*vh == NULL) {
                Status = EFI_UNSUPPORTED;
                pBS->FreePool(vh);
            } else {
                Status = EFI_SUCCESS;
                *HandleCount=1;
                *HandleBuffer=vh;
            }

            if(ChangeIgfxToAuto || ChangePegToAuto || ChangepcieToAuto || ChangePrimaryPeg || ChangePrimaryPcie) {
#if defined APAC_NB_SETUP_SUPPORT || defined NB_SETUP_SUPPORT
#if APAC_NB_SETUP_SUPPORT == 1 || NB_SETUP_SUPPORT == 1
                EFI_STATUS             SetupStatus;
                SETUP_DATA             *SetupData = NULL;
                EFI_GUID               SetupGuid  = SETUP_GUID;
                UINT32                 Attributes;  // [ EIP167027 ]


                //SetupStatus = GetEfiVariable(L"Setup",&SetupGuid,NULL,&sz,&SetupData);    // [ EIP167027 ]
                SetupStatus = GetEfiVariable(L"Setup",&SetupGuid,&Attributes,&sz,&SetupData);
                if (!EFI_ERROR(SetupStatus))
                {
                   if (ChangePrimaryPeg) SetupData->PrimaryPeg = 0; // change to AUTO
                   if (ChangePrimaryPcie) SetupData->PrimaryPcie = 0; // change to AUTO

                   if(ChangeIgfxToAuto || ChangePegToAuto || ChangepcieToAuto)
                      SetupData->PrimaryDisplay = 3; // change to AUTO

                   //SetupStatus = pRS->SetVariable (
                   //                    L"Setup",
                   //                    &SetupGuid,
                   //                    EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
                   //                    sizeof(SETUP_DATA),
                   //                    SetupData);
                   SetupStatus = pRS->SetVariable (
                                       L"Setup",
                                       &SetupGuid,
                                       Attributes,
                                       sizeof(SETUP_DATA),
                                       SetupData);
                   ASSERT_EFI_ERROR (SetupStatus);
                }
#endif
#endif
            }
        }break;
        default: Status = EFI_UNSUPPORTED;
    } // switch

    return Status;
}

#endif

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