summaryrefslogtreecommitdiff
path: root/Chipset/NB/GOP/IntelSaGopSetup/IntelSaGopSetup.c
blob: 0253aeb928bb11b12ab6ad31e961186f2583da5d (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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2012, 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/IntelGopDriver/IntelSaGopSetup/IntelSaGopSetup.c 6     7/11/14 3:48a Dennisliu $
//
// $Revision: 6 $
//
// $Date: 7/11/14 3:48a $
//*************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/BIN/Chipset/Intel/NorthBridge/Haswell/IntelGopDriver/IntelSaGopSetup/IntelSaGopSetup.c $
// 
// 6     7/11/14 3:48a Dennisliu
// [TAG]           None
// [Category]      Improvement
// [Description]   Problem: #if directive for TSE_BUILD=0x1208 (EIP176870)
// [Files]
// Board\NB\NBSetup\NBSetup.c
// Chipset\NB\GOP\IntelSaGopSetup\IntelSaGopSetup.c
// 
// 5     5/13/14 10:44p 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;
//
// 4     4/23/13 8:02a Ireneyang
// [TAG]          None
// [Severity]     Improvement
// [Description]  Support for BIST (Built-In Self Test) Protocol.
// [Files]        IntelSaGopSetup.c; IntelSaGopSetup.h;
// IntelSaGopSetup.sd;
//                IntelSaGopSetup.sd; IntelSaGopSetup.sdl;
//                IntelSaGopSetup.uni; IntelSaGopDriver.h;
//
// 3     4/08/13 6:31a Ireneyang
//
// 2     8/14/12 5:47a Yurenlai
// [TAG]         None
// [Severity]    Important
// [Description] Implemented BIOS Integration Guide Rev 1.0 to Intel SA
// GOP driver.
// [Files]       IntelSaGopDriver.cif, IntelSaGopDriver.mak,
//               IntelSaGopDriver.sdl, IntelSaGopPolicy.c,
// IntelSaGopSetup.c,
//               IntelSaGopSetup.h, IntelSaGopSetup.mak,
// IntelSaGopSetup.sd,
//               IntelSaGopSetup.sdl, IntelSaGopSetup.uni,
//               IntelSaGopSwitch.c, IntelSaGopDriver.h, NBPlatformData.h
//
// 1     3/08/12 10:55p Yurenlai
// [TAG]         None
// [Category]    Improvement
// [Description] Create Haswell Intel SA GOP Driver module part.
//               Notice : IntelGopDriver.efi and vbt.bin is dummy files.
//
//*************************************************************************
//<AMI_FHDR_START>
//
// Name:        IntelSaGopSetup.c
//
// Description: GOP Setup Rountines
//
//<AMI_FHDR_END>
//*************************************************************************

//----------------------------------------------------------------------------
// Include(s)
//----------------------------------------------------------------------------
#include <Token.h>
#include <Setup.h>
#include <SetupStrTokens.h>
#include <AmiDxeLib.h>
#include <AmiCspLib.h>
#include <Protocol\ComponentName2.h>
#include <Protocol\GraphicsOutput.h>
#include <Protocol\IntelSaGopDriver.h>
#include "IntelSaGopSetup.h"

static EFI_GUID                      gEfiVariableGuid = EFI_GLOBAL_VARIABLE;
static EFI_GUID                      gSetupGuid = SETUP_GUID;
static EFI_GUID                      gGopDisplayBrightnessProtocolGuid  = GOP_DISPLAY_BRIGHTNESS_PROTOCOL_GUID;
static EFI_GUID                      gGopDisplayBistProtocolGuid        = GOP_DISPLAY_BIST_PROTOCOL_GUID;

EFI_STATUS AmiGopDeviceCheck (
    IN  EFI_HANDLE                  ControllerHandle,
    IN  EFI_HANDLE                  DriverBindingHandle,
    IN  EFI_PCI_IO_PROTOCOL         *PciIo
);

EFI_STATUS GetIntelSaGopSetupDriverBindingHandle (
  IN  EFI_HANDLE  ControllerHandle,
  OUT EFI_HANDLE  *DriverBindingHandle
);

EFI_STATUS GetChildDeviceHandlesControledByDriver (
  IN  EFI_HANDLE  DriverBindingHandle,
  IN  EFI_HANDLE  ControllerHandle,
  OUT UINTN       *ChildControllerHandleCount,
  OUT EFI_HANDLE  **ChildControllerHandleBuffer
);

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Name:        GetIntelSaGopSetupDriverBindingHandle
//
// Description: None.
//
// Input:       None.
//
// Output:      None.
//
// Notes:       None.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
GetIntelSaGopSetupDriverBindingHandle (
  IN  EFI_HANDLE  ControllerHandle,
  OUT EFI_HANDLE  *DriverBindingHandle )
{
    EFI_STATUS                          Status;
    UINTN                               HandleCount;
    EFI_HANDLE                          *HandleBuffer;
    UINTN                               HandleIndex;
    EFI_GUID                            **ProtocolGuidArray;
    UINTN                               ArrayCount;
    UINTN                               ProtocolIndex;
    EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;
    UINTN                               OpenInfoCount;
    UINTN                               OpenInfoIndex;
    UINTN                               Index;

    Status = pBS->LocateHandleBuffer(
                  AllHandles,
                  NULL,
                  NULL,
                  &HandleCount,
                  &HandleBuffer);
    if (EFI_ERROR(Status)) return EFI_NOT_FOUND;

    for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
        ProtocolGuidArray = NULL;
        Status = pBS->ProtocolsPerHandle(
                      HandleBuffer[HandleIndex],
                      &ProtocolGuidArray,
                      &ArrayCount);
        if (EFI_ERROR(Status)) continue;

        for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) {
            Status = pBS->OpenProtocolInformation(
                          HandleBuffer[HandleIndex],
                          ProtocolGuidArray[ProtocolIndex],
                          &OpenInfo,
                          &OpenInfoCount);
            if (EFI_ERROR(Status)) continue;

            for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
                if (OpenInfo[OpenInfoIndex].ControllerHandle == ControllerHandle) {
                    if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_DRIVER) == EFI_OPEN_PROTOCOL_BY_DRIVER) {
                        for(Index = 0; Index < HandleCount; Index++) {
                            if(HandleBuffer[Index] == OpenInfo[OpenInfoIndex].AgentHandle) {
                                *DriverBindingHandle=HandleBuffer[Index];
                                pBS->FreePool(OpenInfo);
                                pBS->FreePool(ProtocolGuidArray);
                                pBS->FreePool(HandleBuffer);
                                return EFI_SUCCESS;
                            }
                        }
                    }
                }
            }
            if (OpenInfo != NULL) pBS->FreePool(OpenInfo);
        }
         if (ProtocolGuidArray != NULL) pBS->FreePool(ProtocolGuidArray);
    }
    if (HandleBuffer != NULL) pBS->FreePool(HandleBuffer);
    return EFI_NOT_FOUND;
}

// <AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Name: GetChildDeviceHandlesControledByDriver
//
// Description:
//  Get all child device handles which are being opened by a specific driver.
//  The rountine will allocate pool buffer for the found child device handles,
//  and it is the caller's responsibility to safe free the buffer.
//
// Input:
//  IN  EFI_HANDLE  DriverBindingHandle - the handle of a driver which
//                                        contains the binding protocol
//  IN  EFI_HANDLE  ControllerHandle - the device controller handle be opened
//                                     by its child device
//  OUT UINTN       ChildControllerHandleCount - the number of available
//                                               device handles returned in
//                                               ControllerHandleBuffer
//  OUT EFI_HANDLE  ChildControllerHandleBuffer - a pointer to the buffer to
//                                                return the array of child
//                                                device handles
//
// Output:
//     EFI_STATUS
//      If returned status is not succeful or find no available device,
//      the *ChildControllerHandleBuffer will be NULL
//
// Modified:
//
// Referrals:
//
// Notes:
//
//----------------------------------------------------------------------------
// <AMI_PHDR_END>

EFI_STATUS
GetChildDeviceHandlesControledByDriver (
  IN  EFI_HANDLE  DriverBindingHandle,
  IN  EFI_HANDLE  ControllerHandle,
  OUT UINTN       *ChildControllerHandleCount,
  OUT EFI_HANDLE  **ChildControllerHandleBuffer )
{
    UINTN                               HandleCount;
    EFI_HANDLE                          *HandleBuffer;
    BOOLEAN                             *HandleBufferMap;
    EFI_STATUS                          Status;
    UINTN                               HandleIndex;
    UINTN                               AvailableIndex;
    EFI_GUID                            **ProtocolGuidArray;
    UINTN                               ArrayCount;
    UINTN                               ProtocolIndex;
    EFI_OPEN_PROTOCOL_INFORMATION_ENTRY *OpenInfo;
    UINTN                               OpenInfoCount;
    UINTN                               OpenInfoIndex;

    *ChildControllerHandleCount  = 0;
    *ChildControllerHandleBuffer = NULL;
    HandleCount = 0;
    HandleBuffer = NULL;

    if ((DriverBindingHandle == NULL) || (ControllerHandle == NULL)) {
        Status = EFI_INVALID_PARAMETER;
        goto Error;
    }

    //
    // Retrieve the list of all handles from the handle database
    //
    Status = pBS->LocateHandleBuffer (
                  AllHandles,
                  NULL,
                  NULL,
                  &HandleCount,
                  &HandleBuffer);
    if (EFI_ERROR(Status)) goto Error;

    //
    // Create a map for HandleBuffer. If a handle in HandleBuffer is the wanted device handle, its map item is true.
    //
    HandleBufferMap = MallocZ (sizeof (BOOLEAN) * HandleCount);
    for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
        HandleBufferMap[HandleIndex] = FALSE;
    }

    //
    // Retrieve the list of all the protocols on each handle
    //
    Status = pBS->ProtocolsPerHandle (
                  ControllerHandle,
                  &ProtocolGuidArray,
                  &ArrayCount);
    if (!EFI_ERROR (Status)) {
        for (ProtocolIndex = 0; ProtocolIndex < ArrayCount; ProtocolIndex++) {
            //
            // Retrieve the list of agents that have opened each protocol
            //
            Status = pBS->OpenProtocolInformation (
                          ControllerHandle,
                          ProtocolGuidArray[ProtocolIndex],
                          &OpenInfo,
                          &OpenInfoCount);
            if (!EFI_ERROR (Status)) {
                for (OpenInfoIndex = 0; OpenInfoIndex < OpenInfoCount; OpenInfoIndex++) {
                    if (OpenInfo[OpenInfoIndex].AgentHandle == DriverBindingHandle) {
                        if ((OpenInfo[OpenInfoIndex].Attributes & EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) == EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER) {
                            //
                            // OpenInfo[OpenInfoIndex].ControllerHandle is the wanted child device handle, find it in the handlebuffer
                            // A bus driver maybe open a Controller with BY_CHILD_CONTROLLER attribute for different protocol many times,
                            //
                            for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
                                if (OpenInfo[OpenInfoIndex].ControllerHandle == HandleBuffer[HandleIndex]) {
                                    HandleBufferMap[HandleIndex] = TRUE;
                                }
                            }
                        }
                    }
                }
            pBS->FreePool (OpenInfo);
            }
        }
        pBS->FreePool (ProtocolGuidArray);
    }

    //
    // count how many device handles are found
    //
    for (HandleIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
        if (HandleBufferMap[HandleIndex]) {
            (*ChildControllerHandleCount)++;
        }
    }

    if (*ChildControllerHandleCount > 0) {
        //
        // Copy the found device handle to returned buffer
        //
        *ChildControllerHandleBuffer = MallocZ (sizeof (EFI_HANDLE) * (*ChildControllerHandleCount));
        for (HandleIndex = 0, AvailableIndex = 0; HandleIndex < HandleCount; HandleIndex++) {
            if (HandleBufferMap[HandleIndex]) {
                (*ChildControllerHandleBuffer)[AvailableIndex] = HandleBuffer[HandleIndex];
                AvailableIndex++;
            }
        }
    }

    if (HandleBuffer != NULL) pBS->FreePool (HandleBuffer);
    return EFI_SUCCESS;

Error:
    if (HandleBuffer != NULL) pBS->FreePool (HandleBuffer);
    return Status;
}

//<AMI_PHDR_START>
//---------------------------------------------------------------------------
// Name:        IntelSaGopSetupInfo
//
// Description: This function will display Brightness Option in setup menu,
//              if the system GOP supports it.
//
// Input:       EFI_HII_HANDLE HiiHandle
//
// Output:      VOID
//---------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID
IntelSaGopSetupInfo(
    EFI_HII_HANDLE                   HiiHandle,
    UINT16                           Class
)
{
  EFI_STATUS                         Status;
  UINTN                              SetupSize;
  NB_GOP_PLATFORM_DATA               NBGopPlatformData = {0};

  UINTN                              ControllerHandleIndex;
  EFI_HANDLE                         ControllerHandle;
  UINTN                              ControllerHandleCount = 0;
  EFI_HANDLE                         *ControllerHandleBuffer = NULL;

  EFI_HANDLE                         DriverBindingHandle;

  EFI_PCI_IO_PROTOCOL                *PciIo;

  UINTN                              ChildHandleCount = 0;
  EFI_HANDLE                         *ChildHandleBuffer = NULL;
  EFI_HANDLE                         ChildHandle;
  UINTN                              ChildHandleIndex = 0;
  UINT8                              GopBistEnable;
  UINT32                             CurrentBrightness = 255;
  GOP_DISPLAY_BRIGHTNESS_PROTOCOL    *GopDisplayBrightnessProtocol = NULL;
  GOP_DISPLAY_BIST_PROTOCOL          *GopDisplayBistProtocol = NULL;
  SETUP_DATA                         SetupData;

  SetupSize = sizeof (SETUP_DATA);
  Status = pRS->GetVariable (
    L"Setup",
    &gSetupGuid,
    NULL,
    &SetupSize,
    &SetupData
    );

  GopBistEnable = (Status == EFI_SUCCESS) ? SetupData.EnableBIST : FALSE;

  //
  // Get all drivers handles which has PCI IO Protocol
  //
  Status = pBS->LocateHandleBuffer(
                  ByProtocol,
                  &gEfiPciIoProtocolGuid,
                  NULL,
                  &ControllerHandleCount,
                  &ControllerHandleBuffer);
  if (EFI_ERROR(Status)) return;

  for (ControllerHandleIndex = 0; ControllerHandleIndex < ControllerHandleCount; ControllerHandleIndex++) {

        ControllerHandle = ControllerHandleBuffer[ControllerHandleIndex];
        Status = pBS->HandleProtocol (ControllerHandle, &gEfiPciIoProtocolGuid, &PciIo);
        if (EFI_ERROR(Status)) continue;

        //
        // Get Driver Binding Protocol for this VGA
        //
        Status = GetIntelSaGopSetupDriverBindingHandle (ControllerHandle, &DriverBindingHandle);
        if (EFI_ERROR(Status)) continue;

        Status = AmiGopDeviceCheck (ControllerHandle, DriverBindingHandle, PciIo);
        if (EFI_ERROR(Status)) continue;

        Status = GetChildDeviceHandlesControledByDriver (DriverBindingHandle, ControllerHandle, &ChildHandleCount, &ChildHandleBuffer);

        if (!EFI_ERROR(Status)) {
            for (ChildHandleIndex = 0; ChildHandleIndex < ChildHandleCount; ChildHandleIndex++) {
                ChildHandle = ChildHandleBuffer[ChildHandleIndex];

                  //
                  // Check if this device have Graphic Output Portocol.
                  // If it does, the system would support Brightness option in setup Menu.
                  //
                  Status = pBS->OpenProtocol (
                                ChildHandle,
                                &gEfiGraphicsOutputProtocolGuid,
                                NULL,
                                NULL,
                                NULL,
                                EFI_OPEN_PROTOCOL_TEST_PROTOCOL);

                  if (!EFI_ERROR(Status)) {

                     Status = pBS->OpenProtocol (
                                    ChildHandle,
                                    &gGopDisplayBrightnessProtocolGuid,
                                    (VOID**)&GopDisplayBrightnessProtocol,
                                    NULL,
                                    NULL,
                                    EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);

                     if (!EFI_ERROR(Status)) {

                        if(GopDisplayBrightnessProtocol->Revision >= GOP_DISPLAY_BRIGHTNESS_PROTOCOL_REVISION_01) {

                           NBGopPlatformData.BrightnessAvailable = 1;
                           Status = GopDisplayBrightnessProtocol->GetCurrentBrightnessLevel(GopDisplayBrightnessProtocol, &CurrentBrightness);

                           if (EFI_ERROR(Status)) CurrentBrightness = 255;
                        }//if(GopDisplayBrightnessProtocol->Revision >= GOP_DISPLAY_BRIGHTNESS_PROTOCOL_REVISION_01)
                     }

                     Status = pBS->OpenProtocol (
                                    ChildHandle,
                                    &gGopDisplayBistProtocolGuid,
                                    (VOID**)&GopDisplayBistProtocol,
                                    NULL,
                                    NULL,
                                    EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);

                     if (!EFI_ERROR(Status)) {

                        if(GopDisplayBistProtocol->Revision >= GOP_DISPLAY_BIST_PROTOCOL_REVISION_01) {

                           NBGopPlatformData.GopBistAvailable = 1;

                           if (GopBistEnable == 1)
                              Status = GopDisplayBistProtocol->EnableBist(GopDisplayBistProtocol);
                           else
                              Status = GopDisplayBistProtocol->DisableBist(GopDisplayBistProtocol);
                        }//if(GopDisplayBistProtocol->Revision >= GOP_DISPLAY_BIST_PROTOCOL_REVISION_01)
                     }

                  }

                  //Status = pRS->SetVariable ( // [ EIP167027 ]
                  //                  L"NBGopPlatformData",
                  //                  &gSetupGuid,
                  //                  EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
                  //                  sizeof (NB_GOP_PLATFORM_DATA),
                  //                  &NBGopPlatformData
                  //                  );
                  Status = pRS->SetVariable (
                                    L"NBGopPlatformData",
                                    &gSetupGuid,
                                    EFI_VARIABLE_BOOTSERVICE_ACCESS,
                                    sizeof (NB_GOP_PLATFORM_DATA),
                                    &NBGopPlatformData
                                    );

            }//for (ChildHandleIndex = 0; ChildHandleIndex < ChildHandleCount; ChildHandleIndex++)
        }//if (!EFI_ERROR(Status))
  }//for (ControllerHandleIndex = 0; ControllerHandleIndex < ControllerHandleCount; ControllerHandleIndex++)
  return;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Name:        IntelGopSwitchFunction
//
// Description: For overriding ELink, AmiDefaultGopSwitchFunction.
//              If it's Intel Device, the system should do this function, IntelGopSwitchFunction.
//
// Input:       None.
//
// Output:      None.
//
// Notes:       None.
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS
IntelGopSwitchFunction (
    IN  EFI_HANDLE                  ControllerHandle,
    IN  EFI_HANDLE                  DriverBindingHandle,
    IN  SETUP_DATA                  *SetupData,
    IN  EFI_DEVICE_PATH_PROTOCOL    *DevicePath )
{
    EFI_STATUS                      Status = EFI_SUCCESS;
    UINTN                           ChildHandleCount = 0;
    EFI_HANDLE                      *ChildHandleBuffer = NULL;
    UINTN                           ChildHandleIndex = 0;
    EFI_HANDLE                      OutPutDevHandle;

    EFI_DEVICE_PATH_PROTOCOL        *GopDevicePath = NULL;
    EFI_DEVICE_PATH_PROTOCOL        *GopAcpiAdrDPNode = NULL;
    ACPI_ADR_DEVICE_PATH            *AcpiDPNode;

    UINTN                           SetupSize;
    NB_GOP_PLATFORM_DATA            NBGopPlatformData = {0};
    GOP_DISPLAY_BRIGHTNESS_PROTOCOL *GopDisplayBrightnessProtocol = NULL;
    GOP_DISPLAY_BIST_PROTOCOL       *GopDisplayBistProtocol = NULL;
    UINT32                          MaxBrightness = 255;
    UINT32                          CurrentBrightness = 255;

    EFI_PCI_IO_PROTOCOL             *PciIo;
    PCI_DEV_INFO                    *Dev;

    Status = pBS->HandleProtocol (ControllerHandle, &gEfiPciIoProtocolGuid, &PciIo);
    if (EFI_ERROR(Status)) return Status;

    Dev = (PCI_DEV_INFO*)PciIo;

    //
    // If it's not Intel VGA, don't do this part.
    //
    if (Dev->DevVenId.VenId != 0x8086) {
      return EFI_UNSUPPORTED;
    }

    SetupSize = sizeof (NB_GOP_PLATFORM_DATA);

    AcpiDPNode = (ACPI_ADR_DEVICE_PATH*)GopAcpiAdrDPNode = DPGetLastNode(DevicePath);
    Status = pBS->ConnectController( ControllerHandle, NULL, GopAcpiAdrDPNode, TRUE );

//    Status = GetChildDeviceHandlesControledByDriver (DriverBindingHandle, ControllerHandle, &ChildHandleCount, &ChildHandleBuffer);

    if (!EFI_ERROR(Status)) {
      //
      // Find out Current Installed GOP Device
      //
//      for (ChildHandleIndex = 0; ChildHandleIndex < ChildHandleCount; ChildHandleIndex++) {
//        if (SetupData->GopOutputSelect == ChildHandleIndex) {
//
//          Status = pBS->OpenProtocol (
//                            ChildHandleBuffer[ChildHandleIndex],
//                            &gEfiDevicePathProtocolGuid,
//                            (VOID**)&GopDevicePath,
//                            NULL,
//                            NULL,
//                            EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
//          if (EFI_ERROR(Status)) break;
//
//          //
//          // If Current installed GOP is Intels', conncect it.
//          //
//          if (!EFI_ERROR(Status)) {
//            AcpiDPNode = (ACPI_ADR_DEVICE_PATH*)GopAcpiAdrDPNode = DPGetLastNode(GopDevicePath);
//            Status = pBS->ConnectController( ControllerHandle, NULL, GopAcpiAdrDPNode, TRUE );
//          }
//
//          //
//          // Check if this device have Graphic Output Portocol.
//          // If it does, the system would support Brightness option in setup Menu.
//          //
//          Status = pBS->OpenProtocol (
//                            ChildHandleBuffer[ChildHandleIndex],
//                            &gEfiGraphicsOutputProtocolGuid,
//                            NULL,
//                            NULL,
//                            NULL,
//                            EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
          Status = pBS->LocateDevicePath(&gGopDisplayBrightnessProtocolGuid, &DevicePath, &OutPutDevHandle);

          if (!EFI_ERROR(Status)) {

              Status = pBS->OpenProtocol (
                                OutPutDevHandle,
                                &gGopDisplayBrightnessProtocolGuid,
                                (VOID**)&GopDisplayBrightnessProtocol,
                                NULL,
                                NULL,
                                EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
              if (!EFI_ERROR(Status)) {
                  if(GopDisplayBrightnessProtocol->Revision >= GOP_DISPLAY_BRIGHTNESS_PROTOCOL_REVISION_01) {
                      NBGopPlatformData.BrightnessAvailable = 1;
                      Status = GopDisplayBrightnessProtocol->GetMaxBrightnessLevel(GopDisplayBrightnessProtocol, &MaxBrightness);
                      if (!EFI_ERROR(Status)) {
                          if (SetupData->GopBrightness > MaxBrightness) {
                                  SetupData->GopBrightness = MaxBrightness;
#if defined(EFI_SPECIFICATION_VERSION) && EFI_SPECIFICATION_VERSION > 0x20000
                                 Status = HiiLibSetBrowserData (SetupSize, SetupData, &gSetupGuid, L"Setup");
#endif
                           }
                           Status = GopDisplayBrightnessProtocol->GetCurrentBrightnessLevel(GopDisplayBrightnessProtocol, &CurrentBrightness);
                           if (!EFI_ERROR(Status)) {
                               if(CurrentBrightness != SetupData->GopBrightness)
                                   Status = GopDisplayBrightnessProtocol->SetBrightnessLevel(GopDisplayBrightnessProtocol, SetupData->GopBrightness);
                           }
                       }
                   } // GOP_DISPLAY_BRIGHTNESS_PROTOCOL_REVISION_01
               } //OpenProtocol gGopDisplayBrightnessProtocol

               Status = pBS->OpenProtocol (
                              OutPutDevHandle,
                              &gGopDisplayBistProtocolGuid,
                              (VOID**)&GopDisplayBistProtocol,
                              NULL,
                              NULL,
                              EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);

               if (!EFI_ERROR(Status)) {

                  if(GopDisplayBistProtocol->Revision >= GOP_DISPLAY_BIST_PROTOCOL_REVISION_01) {

                     NBGopPlatformData.GopBistAvailable = 1;

                     if (SetupData->EnableBIST == 1)
                        Status = GopDisplayBistProtocol->EnableBist(GopDisplayBistProtocol);
                     else
                        Status = GopDisplayBistProtocol->DisableBist(GopDisplayBistProtocol);
                  }//if(GopDisplayBistProtocol->Revision >= GOP_DISPLAY_BIST_PROTOCOL_REVISION_01)
               } //OpenProtocol gGopDisplayBistProtocolGuid

           }
//        }// if (SetupData->GopOutputSelect == ChildHandleIndex)
//      }// for (ChildHandleIndex = 0; ChildHandleIndex < ChildHandleCount; ChildHandleIndex++)
    }

    Status = HiiLibSetBrowserData (
                      sizeof (NB_GOP_PLATFORM_DATA),
                      &NBGopPlatformData,
                      &gSetupGuid,
                      L"NBGopPlatformData"
                      );

    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------
// Name:        IntelGopMultifunctionCallback
//
// Description: If the device support Multifunction, it supports its key
//              callbak funtion.
//
// Input:       None.
//
// Output:      None.
//
// Notes:       None.
//
//----------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS IntelGopMultifunctionCallback (
    IN EFI_HII_HANDLE           HiiHandle,
    IN UINT16                   Class,
    IN UINT16                   SubClass,
    IN UINT16                   Key )
{

    EFI_STATUS                         Status = EFI_SUCCESS;
    SETUP_DATA                         *SetupData = NULL;
    CALLBACK_PARAMETERS                *CallbackParameters = NULL;
    UINTN                              SetupSize;
    EFI_STATUS                         NBGopPlatformDataStatus = EFI_UNSUPPORTED;
    NB_GOP_PLATFORM_DATA               NBGopPlatformData = {0};
    UINTN                              ControllerHandleIndex;
    EFI_HANDLE                         ControllerHandle;
    UINTN                              ControllerHandleCount = 0;
    EFI_HANDLE                         *ControllerHandleBuffer = NULL;

    EFI_HANDLE                         DriverBindingHandle;

    EFI_PCI_IO_PROTOCOL                *PciIo;

    UINTN                              ChildHandleCount = 0;
    EFI_HANDLE                         *ChildHandleBuffer = NULL;
    EFI_HANDLE                         ChildHandle;
    UINTN                              ChildHandleIndex = 0;

    UINT32                             CurrentBrightness = 255;
    GOP_DISPLAY_BRIGHTNESS_PROTOCOL    *GopDisplayBrightnessProtocol = NULL;
    GOP_DISPLAY_BIST_PROTOCOL          *GopDisplayBistProtocol = NULL;

    UINT32                             MaxBrightness = 255;

    SetupSize = sizeof (SETUP_DATA);

    CallbackParameters = GetCallbackParameters();

#if ((TSE_BUILD >= 0x1224) && (EFI_SPECIFICATION_VERSION >= 0x2000A))
    if (CallbackParameters->Action != EFI_BROWSER_ACTION_CHANGED) return Status;
#elif ((TSE_BUILD > 0x1208) && (EFI_SPECIFICATION_VERSION >= 0x2000A))
    if (CallbackParameters->Action != EFI_BROWSER_ACTION_CHANGING) return Status;
#endif

    Status = pBS->AllocatePool (EfiBootServicesData, SetupSize, &SetupData);
    if(EFI_ERROR(Status)) return Status;

#if defined(EFI_SPECIFICATION_VERSION) && EFI_SPECIFICATION_VERSION > 0x20000
    Status = HiiLibGetBrowserData (&SetupSize, SetupData, &gSetupGuid, L"Setup");
    if(EFI_ERROR(Status)) return Status;
#else
    SetupData = (SETUP_DATA*)CallbackParameters->Data->NvRamMap;
#endif

#if defined(EFI_SPECIFICATION_VERSION) && EFI_SPECIFICATION_VERSION > 0x20000
    SetupSize = sizeof (NB_GOP_PLATFORM_DATA);
    NBGopPlatformDataStatus = HiiLibGetBrowserData (&SetupSize, &NBGopPlatformData, &gSetupGuid, L"NBGopPlatformData");
#endif
  //
  // Get all drivers handles which has PCI IO Protocol
  //
  Status = pBS->LocateHandleBuffer(
                  ByProtocol,
                  &gEfiPciIoProtocolGuid,
                  NULL,
                  &ControllerHandleCount,
                  &ControllerHandleBuffer);
  if (EFI_ERROR(Status)) return Status;

  for (ControllerHandleIndex = 0; ControllerHandleIndex < ControllerHandleCount; ControllerHandleIndex++) {

        ControllerHandle = ControllerHandleBuffer[ControllerHandleIndex];
        Status = pBS->HandleProtocol (ControllerHandle, &gEfiPciIoProtocolGuid, &PciIo);
        if (EFI_ERROR(Status)) continue;

        Status = GetIntelSaGopSetupDriverBindingHandle (ControllerHandle, &DriverBindingHandle);
        if (EFI_ERROR(Status)) continue;

        Status = AmiGopDeviceCheck (ControllerHandle, DriverBindingHandle, PciIo);
        if (EFI_ERROR(Status)) continue;

        Status = GetChildDeviceHandlesControledByDriver (DriverBindingHandle, ControllerHandle, &ChildHandleCount, &ChildHandleBuffer);

        if (!EFI_ERROR(Status)) {
            for (ChildHandleIndex = 0; ChildHandleIndex < ChildHandleCount; ChildHandleIndex++) {
                ChildHandle = ChildHandleBuffer[ChildHandleIndex];

                  //
                  // Check if this device have Graphic Output Portocol.
                  // If it does, the system would support Brightness option in setup Menu.
                  //
                  Status = pBS->OpenProtocol (
                                ChildHandle,
                                &gEfiGraphicsOutputProtocolGuid,
                                NULL,
                                NULL,
                                NULL,
                                EFI_OPEN_PROTOCOL_TEST_PROTOCOL);

                  if (!EFI_ERROR(Status)) {

                     if (Key == AGP_BRIGHTNESS_KEY) {
                         Status = pBS->OpenProtocol (
                                       ChildHandle,
                                       &gGopDisplayBrightnessProtocolGuid,
                                       (VOID**)&GopDisplayBrightnessProtocol,
                                       NULL,
                                       NULL,
                                       EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
                         if (!EFI_ERROR(Status)) {
                            if(GopDisplayBrightnessProtocol->Revision >= GOP_DISPLAY_BRIGHTNESS_PROTOCOL_REVISION_01) {
                               NBGopPlatformData.BrightnessAvailable = 1;
                               Status = GopDisplayBrightnessProtocol->GetMaxBrightnessLevel(GopDisplayBrightnessProtocol, &MaxBrightness);
                               if (!EFI_ERROR(Status)) {
                                    if (SetupData->GopBrightness > MaxBrightness) {
                                        SetupData->GopBrightness = MaxBrightness;
#if defined(EFI_SPECIFICATION_VERSION) && EFI_SPECIFICATION_VERSION > 0x20000
                                        SetupSize = sizeof (SETUP_DATA);
                                        Status = HiiLibSetBrowserData (SetupSize, SetupData, &gSetupGuid, L"Setup");
#endif
                                    }
                                    Status = GopDisplayBrightnessProtocol->GetCurrentBrightnessLevel(GopDisplayBrightnessProtocol, &CurrentBrightness);
                                    if (!EFI_ERROR(Status)) {
                                       if(CurrentBrightness != SetupData->GopBrightness)
                                           Status = GopDisplayBrightnessProtocol->SetBrightnessLevel(GopDisplayBrightnessProtocol, SetupData->GopBrightness);
                                    }
                               }
                            } // GOP_DISPLAY_BRIGHTNESS_PROTOCOL_REVISION_01
                         } //OpenProtocol GopDisplayBrightnessProtocol
                     } // AGP_BRIGHTNESS_KEY

                     if (Key == AGP_BIST_KEY) {
                         Status = pBS->OpenProtocol (
                                        ChildHandle,
                                        &gGopDisplayBistProtocolGuid,
                                        (VOID**)&GopDisplayBistProtocol,
                                        NULL,
                                        NULL,
                                        EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);

                         if (!EFI_ERROR(Status)) {

                            if(GopDisplayBistProtocol->Revision >= GOP_DISPLAY_BIST_PROTOCOL_REVISION_01) {

                               NBGopPlatformData.GopBistAvailable = 1;

                               if (SetupData->EnableBIST == 1)
                                  Status = GopDisplayBistProtocol->EnableBist(GopDisplayBistProtocol);
                               else
                                  Status = GopDisplayBistProtocol->DisableBist(GopDisplayBistProtocol);
                            }//if(GopDisplayBistProtocol->Revision >= GOP_DISPLAY_BIST_PROTOCOL_REVISION_01)
                         } //OpenProtocol gGopDisplayBistProtocolGuid
                     } // AGP_BIST_KEY
                  }//OpenProtocol gEfiGraphicsOutputProtocolGuid

            }//for (ChildHandleIndex = 0; ChildHandleIndex < ChildHandleCount; ChildHandleIndex++)
        }//if (!EFI_ERROR(Status))
  }//for (ControllerHandleIndex = 0; ControllerHandleIndex < ControllerHandleCount; ControllerHandleIndex++)

#if defined(EFI_SPECIFICATION_VERSION) && EFI_SPECIFICATION_VERSION > 0x20000
  if(!EFI_ERROR(NBGopPlatformDataStatus)) {
     HiiLibSetBrowserData (
              sizeof (NB_GOP_PLATFORM_DATA),
              &NBGopPlatformData,
              &gSetupGuid,
              L"NBGopPlatformData"
              );
  }
#endif
  return Status;
}
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2012, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************