summaryrefslogtreecommitdiff
path: root/Chipset/SB/AcpiModeEnable.c
blob: 618f9c8056d6e7638543b3f94fa47ae51fac3894 (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
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2013, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************

//*************************************************************************
// $Header: /Alaska/BIN/Chipset/Intel/SouthBridge/LynxPoint/Intel Pch SB Chipset/AcpiModeEnable/AcpiModeEnable.c 17    5/23/13 1:57a Scottyang $
//
// $Revision: 17 $
//
// $Date: 5/23/13 1:57a $
//*************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/BIN/Chipset/Intel/SouthBridge/LynxPoint/Intel Pch SB Chipset/AcpiModeEnable/AcpiModeEnable.c $
// 
// 17    5/23/13 1:57a Scottyang
// [TAG]  		EIP120623
// [Category]  	Improvement
// [Description]  	LCD turn on automatically when resume from S3.
// [Files]  		SBPEI.c, SBDxe.c, AcpiModeEnable.c
// 
// 16    5/09/13 6:32a Scottyang
// [TAG]  		None
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	S3 resume Verb table will error when enable token
// "AMI_INIT_VERB_TABLE_IN_S3".
// [RootCause]  	The FrontPanel is wrong.
// [Solution]  	Change FrontPanel to frontSideNo.
// [Files]  		AcpiModeEnable.c
// 
// 15    4/23/13 4:20a Wesleychen
// [TAG]         None
// [Category]    Improvement
// [Description] Add token "ONLY_CLEAR_RTC_EN_IN_PEI" for improve
//               "EIP120623".
// [Files]       AcpiModeEnable.c; SB.SDL; SBPEI.c
// 
// 14    4/18/13 12:17a Wesleychen
// [TAG]        EIP120623
// [Category]   Bug Fix
// [Severity]   Important
// [Symptom]    LCD doesn't turn on automatically when resume from S3.
// [RootCause]  PM1_STS (PMBASE+00h) are cleared in EnableAcpiMode().
// [Solution]   Avoid PM1_STS clearing behavior is occurring in S3
//              resuming.
//              *AcpiModeEnable.c Rev#8~11(EIP101628 & EIP118531) are are
//              no need be existence.
// [Files]      SBPEI.c; AcpiModeEnable.c
// 
// 12    4/08/13 2:27a Wesleychen
// Revise the content of history.
// 
// 7     10/12/12 2:08a Scottyang
// [TAG]         EIP76432
// [Category]  	 Bug Fix
// [Severity]  	 Normal
// [Symptom]  	 It will BSOD ,while system boot to OS at "starting windows"
//               press the KB in succession.
// [RootCause]   System BSOD is caused by a large number of SMI generated.
// [Solution]    Disable legacy USB SMI in ACPI enable stage.
// [Files]       AcpiModeEnable.c; SBDXE.c
// 
// 6     10/01/12 5:51a Victortu
// [TAG]         None
// [Category]    Improvement
// [Description] Update EIP#102349 "GPE0 setting and SCI routing for ULT
//               platfoem".
// [Files]       AcpiModeEnable.c, AcpiModeEnable.sdl
// 
// 5     9/26/12 3:57a Victortu
// [TAG]           None
// [Category]      Improvement
// [Description]   Fixed RTC on iFFS failed.
// [Files]         AcpiModeEnable.c
// 
// 4     9/12/12 5:16a Victortu
// [TAG]           None
// [Category]      Improvement
// [Description]   Modify for ULT GPIO changed by PCH LPT-LP EDS 1.0.
// [Files]         SB.H, SB.sdl, AcpiModeEnable.c, AcpiModeEnable.sdl,
// SBPEI.c
// 
// 3     7/27/12 6:16a Victortu
// [TAG]           None
// [Category]      Improvement
// [Description]   Update to support ULT Platform.
// [Files]         SB.H, SB.mak, SB.sdl, SB.sd, SBSetup.c,
// AcpiModeEnable.c, SBDxe.c, SBPEI.c, SBSMI.c, SleepSmi.c,
// SmiHandlerPorting.c, SmiHandlerPorting2.c, SBPPI.h, Pch.sdl
// 
// 2     4/25/12 9:25a Victortu
// [TAG]         None
// [Category]    Improvement
// [Description] Apply AcpiModeEnable support PI 1.2.
// [Files]       AcpiModeEnable.c
// 
// 1     2/08/12 8:30a Yurenlai
// Intel Lynx Point/SB eChipset initially releases.
// 
//*************************************************************************
//<AMI_FHDR_START>
//
// Name:        AcpiModeEnable.C
//
// Description: Provide functions to enable and disable ACPI mode
//
//<AMI_FHDR_END>
//*************************************************************************

//---------------------------------------------------------------------------
// Include(s)
//---------------------------------------------------------------------------
#include <Token.h>
#include <AmiDxeLib.h>

#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION>=0x4028B)
#include <Protocol\SmmBase2.h>
#include <Protocol\SmmSwDispatch2.h>
#else
#include <Protocol\SmmBase.h>
#include <Protocol\SmmSwDispatch.h>
#endif

#include <Protocol\DevicePath.h>
#include <Token.h>
#include <AmiCspLib.h>
#include <PchAccess.h>
#include "RTC.h"
#include "HDAVBTBL.h"
#include "AcpiModeEnable.h"

#if defined(PchDxePlatformPolicy_SUPPORT) && PchDxePlatformPolicy_SUPPORT
#include <PchDxePlatformPolicy.h>
#endif // PchDxePlatformPolicy_SUPPORT end

//---------------------------------------------------------------------------
// Constant, Macro and Type Definition(s)
//---------------------------------------------------------------------------
// Constant Definition(s)

#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
#define AMI_SMM_SW_DISPATCH_PROTOCOL EFI_SMM_SW_DISPATCH2_PROTOCOL
#define AMI_SMM_SW_DISPATCH_CONTEXT  EFI_SMM_SW_REGISTER_CONTEXT
#define SMM_CHILD_DISPATCH_SUCCESS   EFI_SUCCESS
#else
#define AMI_SMM_SW_DISPATCH_PROTOCOL EFI_SMM_SW_DISPATCH_PROTOCOL
#define AMI_SMM_SW_DISPATCH_CONTEXT  EFI_SMM_SW_DISPATCH_CONTEXT
#define SMM_CHILD_DISPATCH_SUCCESS
#endif

// Macro Definition(s)

// Type Definition(s)

// Function Prototype(s)
VOID InitParts (
    IN VOID*                        DispatchHandle,
    IN CONST VOID                   *DispatchContext
);

VOID InitParts2 (
    IN VOID*                        DispatchHandle,
    IN CONST VOID                   *DispatchContext
);

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

ACPI_DISPATCH_LINK  *gAcpiEnDispatchHead = 0, *gAcpiEnDispatchTail = 0;
ACPI_DISPATCH_LINK  *gAcpiDisDispatchHead = 0, *gAcpiDisDispatchTail = 0;

UINT8  gFirstInFlag = 0;
UINT16 wPM1_SaveState;
UINT32 dGPE_SaveState;
#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
EFI_SMM_BASE2_PROTOCOL  *gSmmBase2;
#endif

// GUID Definition(s)

EFI_GUID gEfiAcpiEnDispatchProtocolGuid = EFI_ACPI_EN_DISPATCH_PROTOCOL_GUID;
EFI_GUID gEfiAcpiDisDispatchProtocolGuid = EFI_ACPI_DIS_DISPATCH_PROTOCOL_GUID;

// Protocol Definition(s)

// External Declaration(s)

// Function Definition(s)
//----------------------------------------------------------------------------

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   SbGpioSciInit
//
// Description: Programming the corresponding GPIO pin to generate SCI#.
//
// Input:       None
//
// Output:      None
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID SbGpioSciInit (VOID)
{
  UINT32 GpioRoute;
  UINT8  GpioIndex;
  UINT32 GpioSmiEnable;
  UINT32 GpioNmiEnable;

  if (GetPchSeries() == PchLp) {
    GpioRoute = READ_IO32(GPIO_BASE_ADDRESS+GP_IOREG_GPI_ROUT2);
    GpioSmiEnable = READ_IO32(GPIO_BASE_ADDRESS+GP_IOREG_ALTGP_SMI_EN);
    GpioNmiEnable = READ_IO32(GPIO_BASE_ADDRESS+GP_IOREG_PCHLP_GPI_NMI_EN);

    GpioRoute &= ~GPIO_SCI_BITMAP;
    GpioSmiEnable &= ~GPIO_SCI_BITMAP;
    GpioNmiEnable &= ~GPIO_SCI_BITMAP;

    WRITE_IO16(GPIO_BASE_ADDRESS+GP_IOREG_GPI_ROUT2, GpioRoute);  
    WRITE_IO16(GPIO_BASE_ADDRESS+GP_IOREG_ALTGP_SMI_EN, GpioSmiEnable); 
    WRITE_IO16(GPIO_BASE_ADDRESS+GP_IOREG_PCHLP_GPI_NMI_EN, GpioNmiEnable); 
  } else {
    GpioRoute = READ_PCI32_SB(R_PCH_LPC_GPI_ROUT);
    for (GpioIndex = 0; GpioIndex < 16; GpioIndex++) {
      if (GPIO_SCI_BITMAP & (UINT16)(1 << GpioIndex)) {
        GpioRoute &= ~(3 << (GpioIndex * 2));
        GpioRoute |= (2 << (GpioIndex * 2));
      }
    }
    WRITE_PCI32_SB(R_PCH_LPC_GPI_ROUT, GpioRoute);
  }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   EnableAcpiMode
//
// Description: This function enable ACPI mode by clearing all SMI and
//              enabling SCI generation
//              This routine is also called on a S3 resume for special ACPI
//              programming.
//              Status should not be cleared on S3 resume. Enables are
//              already taken care of.
//
// Input:       PI 0.91, 1.0
//                  DispatchHandle   - SMI dispatcher handle
//                  *DispatchContext - Pointer to the dispatch context
//              PI 1.1, 1.2
//                  DispatchHandle  - SMI dispatcher handle
//                  *DispatchContext- Points to an optional S/W SMI context
//                  CommBuffer      - Points to the optional communication
//                                    buffer
//                  CommBufferSize  - Points to the size of the optional
//                                    communication buffer
//
// Output:      EFI_STATUS if the new SMM PI is applied.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
EFI_STATUS EnableAcpiMode (
	IN EFI_HANDLE       DispatchHandle,
	IN CONST VOID       *DispatchContext OPTIONAL,
	IN OUT VOID         *CommBuffer OPTIONAL,
	IN OUT UINTN        *CommBufferSize OPTIONAL )
#else
VOID EnableAcpiMode (
    IN EFI_HANDLE                   DispatchHandle,
    IN EFI_SMM_SW_DISPATCH_CONTEXT  *DispatchContext )
#endif
{

    ACPI_DISPATCH_LINK      *Link;
    UINT16                  wordValue;
    UINT32                  dwordValue;
    PCH_SERIES              PchSeries = GetPchSeries();

#if defined EMUL6064_SUPPORT && EMUL6064_SUPPORT == 1
    if (READ_PCI8_SB(R_PCH_LPC_ULKMC)) // 0x94
      WRITE_PCI8_SB(R_PCH_LPC_ULKMC, 0); // 0x94
#endif

    if ( gFirstInFlag == 0 ) {
      gFirstInFlag = 1;

      //  Check if WAK bit is set, if yes skip clearing status
      wordValue = READ_IO16_PM(ACPI_IOREG_PM1_STS); // 0x00
      if (wordValue & B_PCH_ACPI_PM1_STS_WAK) {
      //  NAPA-CHANGES Disable and Clear GPE0 Sources
        if (PchSeries == PchLp) {
          WRITE_IO32_PM(ACPI_PCHLP_IOREG_GPE0_EN+0x0c, 0x0000);    //GPE0_EN 0x9C
          WRITE_IO32_PM(ACPI_PCHLP_IOREG_GPE0_STS+0x0c, 0xffffffff);  //GPE0_STS 0x8C
        } else {
          WRITE_IO32_PM(ACPI_IOREG_GPE0_EN, 0x0000);    //GPE0_EN 0x28
          WRITE_IO32_PM(ACPI_IOREG_GPE0_STS, 0xffffffff);  //GPE0_STS 0x20
        }
      } else {
        //Disable SMI Sources
        dwordValue = READ_IO32_PM(ACPI_IOREG_SMI_EN);    //SMI_EN (SMI Control and Enable register.)

        dwordValue &= ~(B_PCH_SMI_EN_ON_SLP_EN | B_PCH_SMI_EN_SWSMI_TMR);  // Clear SLP_SMI_EN and SWSMI_TMR bit.

                                        // [EIP76432]>>
#if defined EMUL6064_SUPPORT && EMUL6064_SUPPORT == 1
      dwordValue &= ~(B_PCH_SMI_EN_LEGACY_USB);
#endif
                                        // <<[EIP76432]

        WRITE_IO32_PM(ACPI_IOREG_SMI_EN, dwordValue);

        //Disable and Clear PM1 Sources except power button
        wPM1_SaveState = READ_IO16_PM(ACPI_IOREG_PM1_EN);  //PM1_EN
        WRITE_IO16_PM(ACPI_IOREG_PM1_EN, (UINT16)B_PCH_ACPI_PM1_EN_PWRBTN);    //PM1_EN Bit 8: PWRBTN_EN
        WRITE_IO16_PM(ACPI_IOREG_PM1_STS, 0xffff);    //PM1_STS 0x00

        //Disable and Clear GPE0 Sources
        if (PchSeries == PchLp) {
          dGPE_SaveState = READ_IO16_PM(ACPI_PCHLP_IOREG_GPE0_EN+0x0c);//GPE0_EN 0x9C
          WRITE_IO32_PM(ACPI_PCHLP_IOREG_GPE0_EN+0x0c, 0x00000000);    //GPE0_EN 0x9C
          WRITE_IO32_PM(ACPI_PCHLP_IOREG_GPE0_STS+0x0c, 0xffffffff);  //GPE0_STS 0x8C
        } else {
          dGPE_SaveState = READ_IO16_PM(ACPI_IOREG_GPE0_EN);//GPE0_EN 0x28
          WRITE_IO32_PM(ACPI_IOREG_GPE0_EN, 0x00000000);    //GPE0_EN 0x28
          WRITE_IO32_PM(ACPI_IOREG_GPE0_STS, 0xffffffff);  //GPE0_STS
        }

        //Set day of month alarm invalid - ACPI 1.0 section 4.7.2.4
        IoWrite8(CMOS_ADDR_PORT, 0xd | 0x80);                //RTC_REGD
        IoWrite8(CMOS_DATA_PORT, 0);
      }
    }

    //SCI_EN = 1
    SET_IO8_PM(ACPI_IOREG_PM1_CNTL, B_PCH_ACPI_PM1_CNT_SCI_EN); //PM1_CNT

    SbGpioSciInit();

    InitParts(DispatchHandle, DispatchContext);

    for (Link = gAcpiEnDispatchHead; Link; Link = Link->Link)
    {
        Link->Function(Link);
    }

    WRITE_IO8 (PCI_DEBUG_PORT, SW_SMI_ACPI_ENABLE);

    return SMM_CHILD_DISPATCH_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   DisableAcpiMode
//
// Description: This function disables ACPI mode by enabling SMI generation
//
// Input:       PI 0.91, 1.0
//                  DispatchHandle   - SMI dispatcher handle
//                  *DispatchContext - Pointer to the dispatch context
//              PI 1.1, 1.2
//                  DispatchHandle  - SMI dispatcher handle
//                  *DispatchContext- Points to an optional S/W SMI context
//                  CommBuffer      - Points to the optional communication
//                                    buffer
//                  CommBufferSize  - Points to the size of the optional
//                                    communication buffer
//
// Output:      EFI_STATUS if the new SMM PI is applied.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
EFI_STATUS DisableAcpiMode (
	IN EFI_HANDLE       DispatchHandle,
	IN CONST VOID       *DispatchContext OPTIONAL,
	IN OUT VOID         *CommBuffer OPTIONAL,
	IN OUT UINTN        *CommBufferSize OPTIONAL )
#else
VOID DisableAcpiMode (
    IN EFI_HANDLE                   DispatchHandle,
    IN EFI_SMM_SW_DISPATCH_CONTEXT  *DispatchContext )
#endif
{
      ACPI_DISPATCH_LINK *Link;

    //Clear PM Sources and set Enables
    WRITE_IO16_PM(ACPI_IOREG_PM1_STS, 0xffff); // 0x00
    WRITE_IO16_PM(ACPI_IOREG_PM1_EN, wPM1_SaveState); // 0x02

    //Clear GPE0 Sources and set Enables
    if (GetPchSeries() == PchLp) {
      WRITE_IO32_PM(ACPI_PCHLP_IOREG_GPE0_STS + 0x0c, 0xffffffff);  //GPE0_STS 0x8C
      WRITE_IO32_PM(ACPI_PCHLP_IOREG_GPE0_EN + 0x0c, dGPE_SaveState); //GPE0_EN 0x9C
    } else {
      WRITE_IO32_PM(ACPI_IOREG_GPE0_STS + 0, 0xffffffff);  //GPE0_STS 0x20
      WRITE_IO32_PM(ACPI_IOREG_GPE0_STS + 4, 0xffffffff);  //GPE0_STS 0x24
      WRITE_IO32_PM(ACPI_IOREG_GPE0_EN, dGPE_SaveState); //GPE0_EN 0x28
    }

    //Disable SCI
    RESET_IO8_PM(ACPI_IOREG_PM1_CNTL, BIT00); // 0x04
    InitParts2(DispatchHandle, DispatchContext);
    for (Link = gAcpiDisDispatchHead; Link; Link = Link->Link)
    {
        Link->Function(Link);
    }


    WRITE_IO8 (PCI_DEBUG_PORT, SW_SMI_ACPI_DISABLE);

    return SMM_CHILD_DISPATCH_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   AddLink
//
// Description: Create and add link to specified list.
//
// Input:  Size -
//              Head -
//              Tail -
//
// Output:     VOID Pointer
//
// Modified:
//
// Referrals:   SmmAllocatePool
//
// Notes:       Here is the control flow of this function:
//              1. Allocate Link in Smm Pool.
//              2. Add Link to end.
//              3. Return Link address.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

VOID * AddLink(
    IN UINT32       Size,
    IN VOID         **Head,
    IN VOID         **Tail)
{
    VOID *Link;

    if (pSmst->SmmAllocatePool(0, Size, &Link) != EFI_SUCCESS) return 0;

    ((GENERIC_LINK*)Link)->Link = 0;
    if (!*Head)
    {
        *Head = *Tail = Link;
    }
    else
    {
        ((GENERIC_LINK*)*Tail)->Link = Link;
        *Tail = Link;
    }

    return Link;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   RemoveLink
//
// Description: Remove link from specified list.
//
// Input:  Handle - EFI Handle
//              Head -
//              Tail -
//
// Output:     BOOLEAN
//                  TRUE if link was removed. FALSE if link not in the list.
//
// Modified:
//
// Referrals:   SmmFreePool
//
// Notes:       Here is the control flow of this function:
//              1. Search link list for Link.
//              2. Remove link from list.
//              3. Free link.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

BOOLEAN RemoveLink(
    IN EFI_HANDLE   Handle,
    IN VOID         **Head,
    IN VOID         **Tail)
{
    GENERIC_LINK *PrevLink, *Link;

    PrevLink = *Head;

    // Is link first. Link address is the same as the Handle.
    if (((GENERIC_LINK*)*Head) == Handle)
    {
        if (PrevLink == *Tail) *Tail = 0; // If Tail = Head, then 0.
        *Head = PrevLink->Link;
        pSmst->SmmFreePool(PrevLink);
        return TRUE;
    }

    // Find Link.
    for (Link = PrevLink->Link; Link; PrevLink = Link, Link = Link->Link)
    {
        if (Link == Handle)     // Link address is the same as the Handle.
        {
            if (Link == *Tail) *Tail = PrevLink;
            PrevLink->Link = Link->Link;
            pSmst->SmmFreePool(Link);
            return TRUE;
        }
    }

    return FALSE;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   EfiAcpiEnRegister
//
// Description: Register a Link on ACPI enable SMI.
//
// Input:  This -
//              Function -
//              Context -
//
//
// Output:     Handle -
//              EFI_STATUS
//
// Modified:    gAcpiEnDispatchHead, gAcpiEnDispatchTail
//
// Referrals:   AddLink
//
// Notes:       Here is the control flow of this function:
//              1. Verify if Context if valid. If invalid,
//                 return EFI_INVALID_PARAMETER.
//              2. Allocate structure and add to link list.
//              3. Fill link.
//              4. Enable Smi Source.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EfiAcpiEnRegister(
    IN EFI_ACPI_DISPATCH_PROTOCOL   *This,
    IN EFI_ACPI_DISPATCH            Function,
    OUT EFI_HANDLE                  *Handle)
{
    ACPI_DISPATCH_LINK *NewLink;

    NewLink = AddLink(sizeof(ACPI_DISPATCH_LINK), \
                      &gAcpiEnDispatchHead, \
                      &gAcpiEnDispatchTail);
    if (!NewLink) return EFI_OUT_OF_RESOURCES;

    NewLink->Function   = Function;
    *Handle = NewLink;

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   EfiAcpiEnUnregister
//
// Description: Unregister a Link on ACPI enable SMI.
//
// Input:  This -
//              Handle -
//
// Output:     EFI_STATUS
//
// Modified:    gAcpiEnDispatchHead, gAcpiEnDispatchTail
//
// Referrals:   RemoveLink
//
// Notes:       Here is the control flow of this function:
//              1. Remove link. If no link, return EFI_INVALID_PARAMETER.
//              2. Disable SMI Source if no other handlers using source.
//              3. Return EFI_SUCCESS.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EfiAcpiEnUnregister(
    IN EFI_ACPI_DISPATCH_PROTOCOL   *This,
    IN EFI_HANDLE                   Handle)
{
    if (!RemoveLink(Handle, &gAcpiEnDispatchHead, &gAcpiEnDispatchTail))
        return EFI_INVALID_PARAMETER;
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   EfiAcpiDisRegister
//
// Description: Register a Link on ACPI disable SMI.
//
// Input:  This -
//              Function -
//              *Context -
//
//
// Output:     Handle - EFI Handle
//              EFI_STATUS
//
// Modified:    gAcpiDisDispatchHead, gAcpiDisDispatchTail
//
// Referrals:   AddLink
//
// Notes:       Here is the control flow of this function:
//              1. Verify if Context if valid. If invalid,
//                 return EFI_INVALID_PARAMETER.
//              2. Allocate structure and add to link list.
//              3. Fill link.
//              4. Enable Smi Source.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EfiAcpiDisRegister(
    IN EFI_ACPI_DISPATCH_PROTOCOL   *This,
    IN EFI_ACPI_DISPATCH            Function,
    OUT EFI_HANDLE                  *Handle)
{
    ACPI_DISPATCH_LINK *NewLink;

    NewLink = AddLink(sizeof(ACPI_DISPATCH_LINK), \
                      &gAcpiDisDispatchHead, \
                      &gAcpiDisDispatchTail);
    if (!NewLink) return EFI_OUT_OF_RESOURCES;

    NewLink->Function   = Function;
    *Handle = NewLink;

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   EfiAcpiDisUnregister
//
// Description: Unregister a Link on ACPI Disable SMI.
//
// Input:  This -
//              Handle - EFI Handle
//
// Output:     EFI_STATUS
//
// Modified:    gAcpiDisDispatchHead, gAcpiDisDispatchTail
//
// Referrals:   RemoveLink
//
// Notes:       Here is the control flow of this function:
//              1. Remove link. If no link, return EFI_INVALID_PARAMETER.
//              2. Disable SMI Source if no other handlers using source.
//              3. Return EFI_SUCCESS.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS EfiAcpiDisUnregister(
    IN EFI_ACPI_DISPATCH_PROTOCOL   *This,
    IN EFI_HANDLE                   Handle)
{
    if (!RemoveLink(Handle, &gAcpiDisDispatchHead, &gAcpiDisDispatchTail))
        return EFI_INVALID_PARAMETER;
    return EFI_SUCCESS;
}

EFI_ACPI_DISPATCH_PROTOCOL gEfiAcpiEnDispatchProtocol = \
        {EfiAcpiEnRegister, EfiAcpiEnUnregister};

EFI_ACPI_DISPATCH_PROTOCOL gEfiAcpiDisDispatchProtocol = \
        {EfiAcpiDisRegister, EfiAcpiDisUnregister};

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   SbAcpiS3PatchedFunction
//
// Description: This function will be called when ACPI S3 resuming.
//
// Input:       PI 0.91, 1.0
//                  DispatchHandle   - SMI dispatcher handle
//                  *DispatchContext - Pointer to the dispatch context
//              PI 1.1, 1.2
//                  DispatchHandle  - SMI dispatcher handle
//                  *DispatchContext- Points to an optional S/W SMI context
//                  CommBuffer      - Points to the optional communication
//                                    buffer
//                  CommBufferSize  - Points to the size of the optional
//                                    communication buffer
//
// Output:      EFI_STATUS if the new SMM PI is applied.
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
EFI_STATUS SbAcpiS3PatchedFunction (
	IN EFI_HANDLE       DispatchHandle,
	IN CONST VOID       *Context OPTIONAL,
	IN OUT VOID         *CommBuffer OPTIONAL,
	IN OUT UINTN        *CommBufferSize OPTIONAL )
#else
VOID SbAcpiS3PatchedFunction (
    IN EFI_HANDLE                   DispatchHandle,
    IN EFI_SMM_SW_DISPATCH_CONTEXT  *DispatchContext )
#endif
{
#if AMI_INIT_VERB_TABLE_IN_S3
    UINT8               CodecIdx;
    UINT8               j;
    UINT8               i;
    UINT16              Cmd16;
    UINT16              TimeOut;
    UINT16              SDIPin;
    UINT32              Buffer32;
    UINT32              CodecId;
    UINT32              Base32;
    UINT8               VerbTblSize = sizeof(HdaVerbTbl) / \
                                                      sizeof(HDA_VERB_TABLE);
    volatile UINT32     *pHdaVerbTbl;

    if ((READ_MEM8_RCRB(RCRB_MMIO_FD) & 0x10) == 0) { // 0x3418
        Base32 = READ_PCI32_HDA(R_PCH_HDA_HDBARL) & B_PCH_HDA_HDBARL_LBA; // 0x10
        Cmd16 = READ_PCI16_HDA(R_PCH_HDA_COMMAND);
        WRITE_PCI16_HDA(R_PCH_HDA_COMMAND, Cmd16 | B_PCH_HDA_COMMAND_MSE);

        // Deassert the CRST bit in Azalia to cause the link to start up
        SET_MEM8(Base32 | R_HDA_GCTL, B_HDA_GCTL_CRST);

        // Turn off the link by writing a 0 to the Controller Reset bit in
        // HD Audio, poll Controller Reset bit until it reads back as 0.
        RESET_MEM8(Base32 | R_HDA_GCTL, B_HDA_GCTL_CRST);

        for (TimeOut = 0; TimeOut < 0x8000; TimeOut++)
           if ((READ_MEM8(Base32 | R_HDA_GCTL) & B_HDA_GCTL_CRST) == 0) break;

        // Turn on the link again by  writing a 1 to Controller Reset bit
        // This causes a codec link re-enumeration. Delay for 1ms, 
        // then poll Controller Reset bit until it reads back 1.
        SET_MEM8(Base32 | R_HDA_GCTL, B_HDA_GCTL_CRST);
        CountTime(1000, PM_BASE_ADDRESS); // 1ms

        for (TimeOut = 0; TimeOut < 0x8000; TimeOut++)
           if (READ_MEM8(Base32 | R_HDA_GCTL) & B_HDA_GCTL_CRST) break;

        // Set the map of SDI pins
        SDIPin = READ_MEM8(Base32 | R_HDA_STATESTS);
        for (CodecIdx = MAX_NUM_HD_CODECS; CodecIdx; CodecIdx--) {
           if (SDIPin & (1 << (CodecIdx - 1))) {
             SDIPin &= ~(1 << (CodecIdx - 1));
             for (TimeOut = 0; TimeOut < 0x8000; TimeOut++) {
                if ((READ_MEM16(Base32 | R_HDA_IRS) & B_HDA_IRS_ICB) == 0) break;
             }

             if (TimeOut == 0x8000) {
               RESET_MEM8(Base32 | R_HDA_GCTL, B_HDA_GCTL_CRST);
               SET_MEM8(Base32 | R_HDA_GCTL, B_HDA_GCTL_CRST);
             }

             Buffer32 = ((CodecIdx - 1) << 28) | 0xf0000;
             WRITE_MEM32(Base32|R_HDA_IC, Buffer32);

             SET_MEM16(Base32 | R_HDA_IRS, B_HDA_IRS_ICB | B_HDA_IRS_IRV);

             while ((READ_MEM16(Base32 | R_HDA_IRS) & (B_HDA_IRS_ICB | B_HDA_IRS_IRV)) != B_HDA_IRS_IRV);

             CodecId = READ_MEM32(Base32 | R_HDA_IR);

             // CheckforValidCodec

             if (VerbTblSize == 0) break;

             for (j = 0; j < VerbTblSize; j++) {
                if (HdaVerbTbl[j].CodecId32 == CodecId) {
                  pHdaVerbTbl = HdaVerbTbl[j].VerbPtr;
                  Buffer32 = *pHdaVerbTbl;
                  if ((UINT8)(Buffer32 >> 28) == (CodecIdx - 1))
                    break;
                }
             }

             // Program Pin Widget Configuration
             if (j != VerbTblSize) {
               pHdaVerbTbl = HdaVerbTbl[j].VerbPtr;
               for(i = 0; i < (HdaVerbTbl[j].frontSideNo + HdaVerbTbl[j].RearSideNo) * 4; i++) {
                 // Poll the ICB bit of the ICS register at HDABAR + 68h[0] until it return 0.
                 for (TimeOut = 0; TimeOut < 0x8000; TimeOut++) {
                   if ((READ_MEM16(Base32 | R_HDA_IRS) & B_HDA_IRS_ICB) == 0)
                    break; 
                 }
                 // Write the current verb(DWORD) to the IC register at HDABAR + 60h
                 WRITE_MEM32(Base32 | R_HDA_IC, *pHdaVerbTbl++);
                 // Send verb to Codec
                 SET_MEM16(Base32 | R_HDA_IRS, (B_HDA_IRS_ICB | B_HDA_IRS_IRV));
               }
            }
        }
      }
    }

    // Restore Command Register
    WRITE_PCI16_HDA(R_PCH_HDA_COMMAND, Cmd16);
#endif

    return SMM_CHILD_DISPATCH_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   InSmmFunction
//
// Description: Install ACPI Software SMI Handlers.
//
// Input:       ImageHandle  - Image handle
//              *SystemTable - Pointer to the system table
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS InSmmFunction (
    IN EFI_HANDLE           ImageHandle,
    IN EFI_SYSTEM_TABLE     *SystemTable )
{
    EFI_STATUS                   Status;
    EFI_HANDLE                   Handle = NULL;
    EFI_HANDLE  DummyHandle = NULL;
    AMI_SMM_SW_DISPATCH_PROTOCOL *SwDispatch = NULL;
    AMI_SMM_SW_DISPATCH_CONTEXT  AcpiEnableContext = {SW_SMI_ACPI_ENABLE};
    AMI_SMM_SW_DISPATCH_CONTEXT  AcpiDisableContext = {SW_SMI_ACPI_DISABLE};
    AMI_SMM_SW_DISPATCH_CONTEXT  AcpiS3PatchContext = {SW_SMI_SB_ACPI_S3};
#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
    EFI_SMM_SYSTEM_TABLE2   *pSmst2;
#endif

#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
    Status = InitAmiSmmLib( ImageHandle, SystemTable );
    if (EFI_ERROR(Status)) return Status;

    // We are in SMM, retrieve the pointer to SMM System Table
    Status = gSmmBase2->GetSmstLocation( gSmmBase2, &pSmst2);
    if (EFI_ERROR(Status)) return EFI_UNSUPPORTED;

    Status = pSmst2->SmmLocateProtocol( &gEfiSmmSwDispatch2ProtocolGuid , \
                                       NULL, \
                                       &SwDispatch );
    TRACE((TRACE_ALWAYS, "Smm Locate Protocol gEfiSmmSwDispatch2ProtocolGuid, Status = %r\n",Status));
#else
    Status = pBS->LocateProtocol( &gEfiSmmSwDispatchProtocolGuid , \
                                  NULL, \
                                  &SwDispatch );
#endif
    if (EFI_ERROR(Status)) return Status;

    Status = SwDispatch->Register( SwDispatch, \
                                   SbAcpiS3PatchedFunction, \
                                   &AcpiS3PatchContext, \
                                   &Handle );
    if (EFI_ERROR(Status)) return Status;

    Status = SwDispatch->Register( SwDispatch, \
                                   EnableAcpiMode, \
                                   &AcpiEnableContext, \
                                   &Handle );
    if (EFI_ERROR(Status)) return Status;

    Status = SwDispatch->Register( SwDispatch, \
                                   DisableAcpiMode, \
                                   &AcpiDisableContext,\
                                   &Handle );
    if (EFI_ERROR(Status)) return Status;

    Status = pBS->InstallMultipleProtocolInterfaces(
             &DummyHandle, 
             &gEfiAcpiEnDispatchProtocolGuid, 
             &gEfiAcpiEnDispatchProtocol, 
             &gEfiAcpiDisDispatchProtocolGuid, 
             &gEfiAcpiDisDispatchProtocol, 
             NULL);

    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   AcpiModeEnableInit
//
// Description: installs appropriate ACPI enable/disable Dispatch Protocol.
//
// Input:       ImageHandle  - Image handle
//              *SystemTable - Pointer to the system table 
//
// Output:      EFI_STATUS
//
// Referrals:   InitAmiLib, InitSmmHandler
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS AcpiModeEnableInit (
    IN EFI_HANDLE           ImageHandle,
    IN EFI_SYSTEM_TABLE     *SystemTable )
{
    EFI_STATUS Status = EFI_SUCCESS;

    InitAmiLib( ImageHandle, SystemTable );

#if defined(PI_SPECIFICATION_VERSION)&&(PI_SPECIFICATION_VERSION>=0x0001000A)&&(CORE_COMBINED_VERSION >= 0x4028B)
    Status = SystemTable->BootServices->LocateProtocol( \
                                                &gEfiSmmBase2ProtocolGuid, \
                                                NULL, \
                                                &gSmmBase2 );
    ASSERT_EFI_ERROR(Status);
#endif

    return InitSmmHandler( ImageHandle, SystemTable, InSmmFunction, NULL );
}

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