summaryrefslogtreecommitdiff
path: root/ReferenceCode/Chipset/LynxPoint/PchSmiDispatcher/Smm/PchxSmmHelpers.c
blob: f6513a7c7600799b34f7f20d2a0d4975c017adce (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
/** @file
  This driver is responsible for the registration of child drivers
  and the abstraction of the PCH SMI sources.

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

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

**/
#include "PchSmmHelpers.h"

//
// Help handle porting bit shifts to IA-64.
//
#define BIT_ZERO  0x00000001

/**
  Publish SMI Dispatch protocols.

  @param[in] None

  @retval None
**/
VOID
PchSmmPublishDispatchProtocols (
  VOID
  )
{
  EFI_STATUS  Status;

  ///
  /// Install protocol interfaces.
  ///
  Status = gBS->InstallMultipleProtocolInterfaces (
                  &mPrivateData.InstallMultProtHandle,
                  &gEfiSmmGpiDispatchProtocolGuid,
                  &mPrivateData.Protocols[GpiType].Protocols.Gpi,
                  &gEfiSmmSxDispatchProtocolGuid,
                  &mPrivateData.Protocols[SxType].Protocols.Sx,
                  &gEfiSmmSwDispatchProtocolGuid,
                  &mPrivateData.Protocols[SwType].Protocols.Sw,
                  &gEfiSmmIchnDispatchProtocolGuid,
                  &mPrivateData.Protocols[IchnType].Protocols.Ichn,
                  &gEfiSmmIchnDispatchExProtocolGuid,
                  &mPrivateData.Protocols[IchnExType].Protocols.IchnEx,
                  &gEfiSmmPowerButtonDispatchProtocolGuid,
                  &mPrivateData.Protocols[PowerButtonType].Protocols.PowerButton,
                  &gEfiSmmPeriodicTimerDispatchProtocolGuid,
                  &mPrivateData.Protocols[PeriodicTimerType].Protocols.PeriodicTimer,
                  &gEfiSmmUsbDispatchProtocolGuid,
                  &mPrivateData.Protocols[UsbType].Protocols.Usb,
                  NULL
                  );
  ASSERT_EFI_ERROR (Status);
}

/**
  Initialize bits that aren't necessarily related to an SMI source.

  @param[in] None

  @retval EFI_SUCCESS             SMI source initialization completed.
  @retval Asserts                 Global Smi Bit is not enabled successfully.
**/
EFI_STATUS
PchSmmInitHardware (
  VOID
  )
{
  EFI_STATUS  Status;

  ///
  /// Clear all SMIs
  ///
  PchSmmClearSmi ();

  Status = PchSmmEnableGlobalSmiBit ();
  ASSERT_EFI_ERROR (Status);

  ///
  /// Be *really* sure to clear all SMIs
  ///
  PchSmmClearSmi ();

  return EFI_SUCCESS;
}

/**
  Enables the PCH to generate SMIs. Note that no SMIs will be generated
  if no SMI sources are enabled. Conversely, no enabled SMI source will
  generate SMIs if SMIs are not globally enabled. This is the main
  switchbox for SMI generation.

  @param[in] None

  @retval EFI_SUCCESS             Enable Global Smi Bit completed
**/
EFI_STATUS
PchSmmEnableGlobalSmiBit (
  VOID
  )
{
  UINT32  SmiEn;

  SmiEn = IoRead32 ((UINTN) (mAcpiBaseAddr + R_PCH_SMI_EN));

  ///
  /// Set the "global smi enable" bit
  ///
  SmiEn |= B_PCH_SMI_EN_GBL_SMI;

  IoWrite32 ((UINTN) (mAcpiBaseAddr + R_PCH_SMI_EN), SmiEn);

  return EFI_SUCCESS;
}

/**
  Clears the SMI after all SMI source have been processed.
  Note that this function will not work correctly (as it is
  written) unless all SMI sources have been processed.
  A revision of this function could manually clear all SMI
  status bits to guarantee success.

  @param[in] None

  @retval EFI_SUCCESS             Clears the SMIs completed
  @retval Asserts                 EOS was not set to a 1
**/
EFI_STATUS
PchSmmClearSmi (
  VOID
  )
{
  BOOLEAN     EosSet;
  BOOLEAN     SciEn;
  UINT32      Pm1Cnt;
  UINT16      Pm1Sts;
  UINT32      Gpe0Sts;
  UINT32      Gpe0aStsLow;
  UINT32      Gpe0bStsHigh;
  UINT32      SmiSts;
  UINT32      AltGpiSmiSts;
  UINT16      DevActSts;
  UINT16      Tco1Sts;
  UINT16      Tco2Sts;
  PCH_SERIES  PchSeries;

  PchSeries    = GetPchSeries();
  Gpe0Sts      = 0;
  Gpe0aStsLow  = 0;
  Gpe0bStsHigh = 0;
  AltGpiSmiSts = 0;
  ///
  /// Determine whether an ACPI OS is present (via the SCI_EN bit)
  ///
  Pm1Cnt = IoRead32 ((UINTN) (mAcpiBaseAddr + R_PCH_ACPI_PM1_CNT));
  SciEn  = (BOOLEAN) ((Pm1Cnt & B_PCH_ACPI_PM1_CNT_SCI_EN) == B_PCH_ACPI_PM1_CNT_SCI_EN);
  if (!SciEn) {
    ///
    /// Clear any SMIs that double as SCIs (when SCI_EN==0)
    ///
    Pm1Sts        = IoRead16 ((UINTN) (mAcpiBaseAddr + R_PCH_ACPI_PM1_STS));
    if (PchSeries == PchLp) {
      Gpe0Sts   = IoRead32 ((UINTN) (mAcpiBaseAddr + R_PCH_ACPI_GPE0_STS_127_96));
    } else if (PchSeries == PchH) {
      Gpe0aStsLow   = IoRead32 ((UINTN) (mAcpiBaseAddr + R_PCH_ACPI_GPE0a_STS));
      Gpe0bStsHigh  = IoRead32 ((UINTN) (mAcpiBaseAddr + R_PCH_ACPI_GPE0b_STS));
    }

    Pm1Sts |=
      (
        B_PCH_ACPI_PM1_STS_WAK |
        B_PCH_ACPI_PM1_STS_PRBTNOR |
        B_PCH_ACPI_PM1_STS_RTC |
        B_PCH_ACPI_PM1_STS_PWRBTN |
        B_PCH_ACPI_PM1_STS_GBL |
        B_PCH_ACPI_PM1_STS_TMROF
      );

    if (PchSeries == PchLp) {
      Gpe0Sts |=
        (
          B_PCH_ACPI_GPE0_STS_127_96_PME_B0 |
          B_PCH_ACPI_GPE0_STS_127_96_PME |
          B_PCH_ACPI_GPE0_STS_127_96_BATLOW |
          B_PCH_ACPI_GPE0_STS_127_96_PCI_EXP |
          B_PCH_ACPI_GPE0_STS_127_96_RI |
          B_PCH_ACPI_GPE0_STS_127_96_SMB_WAK |
          B_PCH_ACPI_GPE0_STS_127_96_TC0SCI |
          B_PCH_ACPI_GPE0_STS_127_96_HOT_PLUG |
          B_PCH_ACPI_GPE0_STS_127_96_BATLOW |
          B_PCH_ACPI_GPE0_STS_127_96_GP27
        );
    } else if (PchSeries == PchH) {
      Gpe0aStsLow |=
        (
          B_PCH_ACPI_GPE0a_STS_PME_B0 |
          B_PCH_ACPI_GPE0a_STS_PME |
          B_PCH_ACPI_GPE0a_STS_BATLOW |
          B_PCH_ACPI_GPE0a_STS_PCI_EXP |
          B_PCH_ACPI_GPE0a_STS_RI |
          B_PCH_ACPI_GPE0a_STS_SMB_WAK |
          B_PCH_ACPI_GPE0a_STS_TC0SCI |
          B_PCH_ACPI_GPE0a_STS_HOT_PLUG |
          B_PCH_ACPI_GPE0a_STS_BATLOW
        );

      Gpe0bStsHigh |= (B_PCH_ACPI_GPE0b_STS_GP27);
    }

    IoWrite16 ((UINTN) (mAcpiBaseAddr + R_PCH_ACPI_PM1_STS), (UINT16) Pm1Sts);
    if (PchSeries == PchLp) {
      IoWrite32 ((UINTN) (mAcpiBaseAddr + R_PCH_ACPI_GPE0_STS_127_96), (UINT32) Gpe0Sts);
    } else if (PchSeries == PchH) {
      IoWrite32 ((UINTN) (mAcpiBaseAddr + R_PCH_ACPI_GPE0a_STS), (UINT32) Gpe0aStsLow);
      IoWrite32 ((UINTN) (mAcpiBaseAddr + R_PCH_ACPI_GPE0b_STS), (UINT32) Gpe0bStsHigh);
    }
  }
  ///
  /// Clear all SMIs that are unaffected by SCI_EN
  ///
  if (PchSeries == PchLp) {
    AltGpiSmiSts  = IoRead32 ((UINTN) (mGpioBaseAddr + R_PCH_LPTLP_ALT_GP_SMI_STS));
  } else if (PchSeries == PchH) {
    AltGpiSmiSts  = IoRead16 ((UINTN) (mAcpiBaseAddr + R_PCH_LPTH_ALT_GP_SMI_STS));
  }
  SmiSts        = IoRead32 ((UINTN) (mAcpiBaseAddr + R_PCH_SMI_STS));
  DevActSts     = IoRead16 ((UINTN) (mAcpiBaseAddr + R_PCH_DEVACT_STS));
  Tco1Sts       = IoRead16 ((UINTN) (mAcpiBaseAddr + PCH_TCO_BASE + R_PCH_TCO1_STS));
  Tco2Sts       = IoRead16 ((UINTN) (mAcpiBaseAddr + PCH_TCO_BASE + R_PCH_TCO2_STS));

  SmiSts |=
    (
      B_PCH_SMI_STS_SMBUS |
      B_PCH_SMI_STS_PERIODIC |
      B_PCH_SMI_STS_TCO |
      B_PCH_SMI_STS_MCSMI |
      B_PCH_SMI_STS_SWSMI_TMR |
      B_PCH_SMI_STS_APM |
      B_PCH_SMI_STS_ON_SLP_EN |
      B_PCH_SMI_STS_BIOS
    );
  AltGpiSmiSts |= 0xFFFF;
  DevActSts |=
    (
      B_PCH_DEVACT_STS_KBC |
      B_PCH_DEVACT_STS_PIRQDH |
      B_PCH_DEVACT_STS_PIRQCG |
      B_PCH_DEVACT_STS_PIRQBF |
      B_PCH_DEVACT_STS_PIRQAE
    );
  Tco1Sts |=
    (
      B_PCH_TCO1_STS_DMISERR |
      B_PCH_TCO1_STS_DMISMI |
      B_PCH_TCO1_STS_DMISCI |
      B_PCH_TCO1_STS_BIOSWR |
      B_PCH_TCO1_STS_NEWCENTURY |
      B_PCH_TCO1_STS_TIMEOUT |
      B_PCH_TCO1_STS_TCO_INT |
      B_PCH_TCO1_STS_SW_TCO_SMI
    );
  if(PchSeries == PchLp){
    IoWrite32 ((UINTN) (mGpioBaseAddr + R_PCH_LPTLP_ALT_GP_SMI_STS), AltGpiSmiSts);
  } else if (PchSeries == PchH) {
    IoWrite16 ((UINTN) (mAcpiBaseAddr + R_PCH_LPTH_ALT_GP_SMI_STS), (UINT16)AltGpiSmiSts);
  }
  IoWrite16 ((UINTN) (mAcpiBaseAddr + PCH_TCO_BASE + R_PCH_TCO1_STS), Tco1Sts);

  Tco2Sts |= B_PCH_TCO2_STS_SECOND_TO;
  IoWrite16 ((UINTN) (mAcpiBaseAddr + PCH_TCO_BASE + R_PCH_TCO2_STS), Tco2Sts);

  Tco2Sts |= (B_PCH_TCO2_STS_SMLINK_SLV_SMI | B_PCH_TCO2_STS_BOOT | B_PCH_TCO2_STS_INTRD_DET);
  IoWrite16 ((UINTN) (mAcpiBaseAddr + PCH_TCO_BASE + R_PCH_TCO2_STS), Tco2Sts);

  IoWrite32 ((UINTN) (mAcpiBaseAddr + R_PCH_SMI_STS), SmiSts);

  IoWrite16 ((UINTN) (mAcpiBaseAddr + R_PCH_DEVACT_STS), DevActSts);

  ///
  /// Try to clear the EOS bit. ASSERT on an error
  ///
  EosSet = PchSmmSetAndCheckEos ();
  ASSERT (EosSet);

  return EFI_SUCCESS;
}

/**
  Set the SMI EOS bit after all SMI source have been processed.

  @param[in] None

  @retval FALSE                   EOS was not set to a 1; this is an error
  @retval TRUE                    EOS was correctly set to a 1
**/
BOOLEAN
PchSmmSetAndCheckEos (
  VOID
  )
{
  UINT32  SmiEn;

  SmiEn = IoRead32 ((UINTN) (mAcpiBaseAddr + R_PCH_SMI_EN));

  ///
  /// Reset the PCH to generate subsequent SMIs
  ///
  SmiEn |= B_PCH_SMI_EN_EOS;

  IoWrite32 ((UINTN) (mAcpiBaseAddr + R_PCH_SMI_EN), SmiEn);

  ///
  /// Double check that the assert worked
  ///
  SmiEn = IoRead32 ((UINTN) (mAcpiBaseAddr + R_PCH_SMI_EN));

  ///
  /// Return TRUE if EOS is set correctly
  ///
  if ((SmiEn & B_PCH_SMI_EN_EOS) == 0) {
    ///
    /// EOS was not set to a 1; this is an error
    ///
    return FALSE;
  } else {
    ///
    /// EOS was correctly set to a 1
    ///
    return TRUE;
  }
}

/**
  Determine whether an ACPI OS is present (via the SCI_EN bit)

  @param[in] None

  @retval TRUE                    ACPI OS is present
  @retval FALSE                   ACPI OS is not present
**/
BOOLEAN
PchSmmGetSciEn (
  VOID
  )
{
  BOOLEAN SciEn;
  UINT32  Pm1Cnt;

  ///
  /// Determine whether an ACPI OS is present (via the SCI_EN bit)
  ///
  Pm1Cnt  = IoRead32 ((UINTN) (mAcpiBaseAddr + R_PCH_ACPI_PM1_CNT));
  SciEn   = (BOOLEAN) ((Pm1Cnt & B_PCH_ACPI_PM1_CNT_SCI_EN) == B_PCH_ACPI_PM1_CNT_SCI_EN);

  return SciEn;
}

/**
  Read a specifying bit with the register
  These may or may not need to change w/ the PCH version; they're highly IA-32 dependent, though.

  @param[in] BitDesc              The struct that includes register address, size in byte and bit number

  @retval TRUE                    The bit is enabled
  @retval FALSE                   The bit is disabled
**/
BOOLEAN
ReadBitDesc (
  const PCH_SMM_BIT_DESC  *BitDesc
  )
{
  EFI_STATUS  Status;
  UINT64      Register;
  UINT32      PciBus;
  UINT32      PciDev;
  UINT32      PciFun;
  UINT32      PciReg;
  UINTN       RegSize;
  BOOLEAN     BitWasOne;
  UINTN       ShiftCount;
  UINTN       RegisterOffset;
  UINT32      BaseAddr;

  ASSERT (BitDesc != NULL);
  ASSERT (!IS_BIT_DESC_NULL (*BitDesc));

  RegSize     = 0;
  Register    = 0;
  ShiftCount  = 0;
  BitWasOne   = FALSE;

  switch (BitDesc->Reg.Type) {

  case ACPI_ADDR_TYPE:
  case GPIO_ADDR_TYPE:
    if(BitDesc->Reg.Type == ACPI_ADDR_TYPE){
      RegisterOffset  = BitDesc->Reg.Data.acpi;
      BaseAddr        = mAcpiBaseAddr;
    } else {
      RegisterOffset  = BitDesc->Reg.Data.gpio;
      BaseAddr        = mGpioBaseAddr;
    }
    switch (BitDesc->SizeInBytes) {

    case 0:
      ///
      /// Chances are that this field didn't get initialized.
      /// Check your assignments to bit descriptions.
      ///
      ASSERT (FALSE);
      break;

    case 1:
      RegSize = SMM_IO_UINT8;
      break;

    case 2:
      RegSize = SMM_IO_UINT16;
      break;

    case 4:
      RegSize = SMM_IO_UINT32;
      break;

    case 8:
      RegSize = SMM_IO_UINT64;
      break;

    default:
      ///
      /// Unsupported or invalid register size
      ///
      ASSERT (FALSE);
      break;
    }
    ///
    /// Double check that we correctly read in the acpi base address
    ///
    ASSERT ((BaseAddr != 0x0) && ((BaseAddr & 0x1) != 0x1));

    ShiftCount      = BitDesc->Bit;
    ///
    /// As current CPU Smm Io can only support at most
    /// 32-bit read/write,if Operation is 64 bit,
    /// we do a 32 bit operation according to BitDesc->Bit
    ///
    if (RegSize == SMM_IO_UINT64) {
      RegSize = SMM_IO_UINT32;
      ///
      /// If the operation is for high 32 bits
      ///
      if (BitDesc->Bit >= 32) {
        RegisterOffset += 4;
        ShiftCount -= 32;
      }
    }

    Status = mSmst->SmmIo.Io.Read (
                              &mSmst->SmmIo,
                              RegSize,
                              BaseAddr + RegisterOffset,
                              1,
                              &Register
                              );
    ASSERT_EFI_ERROR (Status);

    if ((Register & (LShiftU64 (BIT_ZERO, ShiftCount))) != 0) {
      BitWasOne = TRUE;
    } else {
      BitWasOne = FALSE;
    }
    break;

  case MEMORY_MAPPED_IO_ADDRESS_TYPE:
    ///
    /// Read the register, and it with the bit to read
    ///
    switch (BitDesc->SizeInBytes) {
    case 1:
      Register = (UINT64) MmioRead8 ((UINTN) BitDesc->Reg.Data.Mmio);
      break;

    case 2:
      Register = (UINT64) MmioRead16 ((UINTN) BitDesc->Reg.Data.Mmio);
      break;

    case 4:
      Register = (UINT64) MmioRead32 ((UINTN) BitDesc->Reg.Data.Mmio);
      break;

    case 8:
      Register                      = (UINT64) MmioRead32 ((UINTN) BitDesc->Reg.Data.Mmio);
      *((UINT32 *) (&Register) + 1) = MmioRead32 ((UINTN) BitDesc->Reg.Data.Mmio + 4);
      break;

    default:
      ///
      /// Unsupported or invalid register size
      ///
      ASSERT (FALSE);
      break;
    }

    Register = Register & (LShiftU64 (BIT0, BitDesc->Bit));
    if (Register) {
      BitWasOne = TRUE;
    } else {
      BitWasOne = FALSE;
    }
    break;

  case PCIE_ADDR_TYPE:
    PciBus  = BitDesc->Reg.Data.pcie.Fields.Bus;
    PciDev  = BitDesc->Reg.Data.pcie.Fields.Dev;
    PciFun  = BitDesc->Reg.Data.pcie.Fields.Fnc;
    PciReg  = BitDesc->Reg.Data.pcie.Fields.Reg;
    switch (BitDesc->SizeInBytes) {

    case 0:
      ///
      /// Chances are that this field didn't get initialized.
      /// Check your assignments to bit descriptions.
      ///
      ASSERT (FALSE);
      break;

    case 1:
      Register = (UINT64) MmioRead8 (MmPciAddress (0, PciBus, PciDev, PciFun, PciReg));
      break;

    case 2:
      Register = (UINT64) MmioRead16 (MmPciAddress (0, PciBus, PciDev, PciFun, PciReg));
      break;

    case 4:
      Register = (UINT64) MmioRead32 (MmPciAddress (0, PciBus, PciDev, PciFun, PciReg));
      break;

    default:
      ///
      /// Unsupported or invalid register size
      ///
      ASSERT (FALSE);
      break;
    }

    if ((Register & (LShiftU64 (BIT_ZERO, BitDesc->Bit))) != 0) {
      BitWasOne = TRUE;
    } else {
      BitWasOne = FALSE;
    }
    break;

  default:
    ///
    /// This address type is not yet implemented
    ///
    ASSERT (FALSE);
    break;
  }

  return BitWasOne;
}

/**
  Write a specifying bit with the register

  @param[in] BitDesc              The struct that includes register address, size in byte and bit number
  @param[in] ValueToWrite         The value to be wrote
  @param[in] WriteClear           If the rest bits of the register is write clear

  @retval None
**/
VOID
WriteBitDesc (
  const PCH_SMM_BIT_DESC  *BitDesc,
  const BOOLEAN           ValueToWrite,
  const BOOLEAN           WriteClear
  )
{
  EFI_STATUS  Status;
  UINT64      Register;
  UINT64      AndVal;
  UINT64      OrVal;
  UINT32      RegSize;
  UINT32      PciBus;
  UINT32      PciDev;
  UINT32      PciFun;
  UINT32      PciReg;
  UINTN       RegisterOffset;
  UINT32      BaseAddr;

  ASSERT (BitDesc != NULL);
  ASSERT (!IS_BIT_DESC_NULL (*BitDesc));

  RegSize   = 0;
  Register  = 0;

  if (WriteClear) {
    AndVal = LShiftU64 (BIT_ZERO, BitDesc->Bit);
  } else {
    AndVal = ~(LShiftU64 (BIT_ZERO, BitDesc->Bit));
  }

  OrVal = (LShiftU64 ((UINT32) ValueToWrite, BitDesc->Bit));

  switch (BitDesc->Reg.Type) {

  case ACPI_ADDR_TYPE:
  case GPIO_ADDR_TYPE:
    if(BitDesc->Reg.Type == ACPI_ADDR_TYPE){
      RegisterOffset  = BitDesc->Reg.Data.acpi;
      BaseAddr        = mAcpiBaseAddr;
    } else {
      RegisterOffset  = BitDesc->Reg.Data.gpio;
      BaseAddr        = mGpioBaseAddr;
    }
    switch (BitDesc->SizeInBytes) {

    case 0:
      ///
      /// Chances are that this field didn't get initialized.
      /// Check your assignments to bit descriptions.
      ///
      ASSERT (FALSE);
      break;

    case 1:
      RegSize = SMM_IO_UINT8;
      break;

    case 2:
      RegSize = SMM_IO_UINT16;
      break;

    case 4:
      RegSize = SMM_IO_UINT32;
      break;

    case 8:
      RegSize = SMM_IO_UINT64;
      break;

    default:
      ///
      /// Unsupported or invalid register size
      ///
      ASSERT (FALSE);
      break;
    }
    ///
    /// Double check that we correctly read in the acpi base address
    ///
    ASSERT ((BaseAddr != 0x0) && ((BaseAddr & 0x1) != 0x1));

    ///
    /// As current CPU Smm Io can only support at most
    /// 32-bit read/write,if Operation is 64 bit,
    /// we do a 32 bit operation according to BitDesc->Bit
    ///
    if (RegSize == SMM_IO_UINT64) {
      RegSize = SMM_IO_UINT32;
      ///
      /// If the operation is for high 32 bits
      ///
      if (BitDesc->Bit >= 32) {
        RegisterOffset += 4;

        if (WriteClear) {
          AndVal = LShiftU64 (BIT_ZERO, BitDesc->Bit - 32);
        } else {
          AndVal = ~(LShiftU64 (BIT_ZERO, BitDesc->Bit - 32));
        }

        OrVal = LShiftU64 ((UINT32) ValueToWrite, BitDesc->Bit - 32);
      }
    }

    Status = mSmst->SmmIo.Io.Read (
                              &mSmst->SmmIo,
                              RegSize,
                              BaseAddr + RegisterOffset,
                              1,
                              &Register
                              );
    ASSERT_EFI_ERROR (Status);

    Register &= AndVal;
    Register |= OrVal;

    Status = mSmst->SmmIo.Io.Write (
                              &mSmst->SmmIo,
                              RegSize,
                              BaseAddr + RegisterOffset,
                              1,
                              &Register
                              );
    ASSERT_EFI_ERROR (Status);
    break;

  case MEMORY_MAPPED_IO_ADDRESS_TYPE:
    ///
    /// Read the register, or it with the bit to set, then write it back.
    ///
    switch (BitDesc->SizeInBytes) {
    case 1:
      Register = (UINT64) MmioRead8 ((UINTN) BitDesc->Reg.Data.Mmio);
      break;

    case 2:
      Register = (UINT64) MmioRead16 ((UINTN) BitDesc->Reg.Data.Mmio);
      break;

    case 4:
      Register = (UINT64) MmioRead32 ((UINTN) BitDesc->Reg.Data.Mmio);
      break;

    case 8:
      Register                      = (UINT64) MmioRead32 ((UINTN) BitDesc->Reg.Data.Mmio);
      *((UINT32 *) (&Register) + 1) = MmioRead32 ((UINTN) BitDesc->Reg.Data.Mmio + 4);
      break;

    default:
      ///
      /// Unsupported or invalid register size
      ///
      ASSERT (FALSE);
      break;
    }

    Register &= AndVal;
    Register |= OrVal;
    ///
    /// Read the register, or it with the bit to set, then write it back.
    ///
    switch (BitDesc->SizeInBytes) {
    case 1:
      MmioWrite8 ((UINTN) BitDesc->Reg.Data.Mmio, (UINT8) Register);
      break;

    case 2:
      MmioWrite16 ((UINTN) BitDesc->Reg.Data.Mmio, (UINT16) Register);
      break;

    case 4:
      MmioWrite32 ((UINTN) BitDesc->Reg.Data.Mmio, (UINT32) Register);
      break;

    case 8:
      MmioWrite32 ((UINTN) BitDesc->Reg.Data.Mmio, (UINT32) Register);
      MmioWrite32 ((UINTN) BitDesc->Reg.Data.Mmio + 4, *((UINT32 *) (&Register) + 1));
      break;

    default:
      ///
      /// Unsupported or invalid register size
      ///
      ASSERT (FALSE);
      break;
    }
    break;

  case PCIE_ADDR_TYPE:
    PciBus  = BitDesc->Reg.Data.pcie.Fields.Bus;
    PciDev  = BitDesc->Reg.Data.pcie.Fields.Dev;
    PciFun  = BitDesc->Reg.Data.pcie.Fields.Fnc;
    PciReg  = BitDesc->Reg.Data.pcie.Fields.Reg;
    switch (BitDesc->SizeInBytes) {

    case 0:
      ///
      /// Chances are that this field didn't get initialized -- check your assignments
      /// to bit descriptions.
      ///
      ASSERT (FALSE);
      break;

    case 1:
      MmioAndThenOr8 (MmPciAddress (0, PciBus, PciDev, PciFun, PciReg), (UINT8) AndVal, (UINT8) OrVal);
      break;

    case 2:
      MmioAndThenOr16 (MmPciAddress (0, PciBus, PciDev, PciFun, PciReg), (UINT16) AndVal, (UINT16) OrVal);
      break;

    case 4:
      MmioAndThenOr32 (MmPciAddress (0, PciBus, PciDev, PciFun, PciReg), (UINT32) AndVal, (UINT32) OrVal);
      break;

    default:
      ///
      /// Unsupported or invalid register size
      ///
      ASSERT (FALSE);
      break;
    }
    break;

  default:
    ///
    /// This address type is not yet implemented
    ///
    ASSERT (FALSE);
    break;
  }
}