summaryrefslogtreecommitdiff
path: root/ReferenceCode/Chipset/LynxPoint/PchInit/Smm/PchLateInitSmm.c
blob: e29c5a1586b47acf7ab264bd2ac12891c57ed3d5 (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
/** @file
  PCH S3 Save and Restore SMM Driver Entry

@copyright
  Copyright (c) 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 "PchLateInitSmm.h"

STATIC PCH_SAVE_RESTORE_REG PchSaveRestoreReg_Common[] = {
  {Acpi, R_PCH_ACPI_PM1_EN, 0, BIT14, 2},
  {Rcrb, 0x3300, 0, 0xFFFFFFFF, 4},
  {Rcrb, 0x3304, 0, 0xFFFFFFFF, 4},
  {Rcrb, 0x3308, 0, 0xFFFFFFFF, 4},
  {Rcrb, 0x330C, 0, 0xFFFFFFFF, 4}
};

#ifdef TRAD_FLAG
STATIC PCH_SAVE_RESTORE_REG PchSaveRestoreReg_PchH[] = {
  {Acpi, R_PCH_ACPI_GPE0a_EN, 0, 0xFFFF0246, 4},
  {Acpi, R_PCH_ACPI_GPE0b_EN, 0, 0xFF000040, 4},
  {Tco , R_PCH_TCO2_CNT , 0, BIT5 | BIT4, 1}
};
#endif // TRAD_FLAG

#ifdef ULT_FLAG
STATIC PCH_SAVE_RESTORE_REG PchSaveRestoreReg_PchLp[] = {
  {Rcrb, 0x3320, 0, 0xFFFFFFFF, 4}
};
#endif // ULT_FLAG

STATIC PCH_SAVE_RESTORE_PCI PchSaveRestorePciReg_Common[] = {
  {PciCfg, PCI_DEVICE_NUMBER_PCH_AZALIA, 0, 0, R_PCH_HDA_PCS + 1, 0, BIT0, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_AZALIA, 0, PCI_BASE_ADDRESSREG_OFFSET, R_HDA_WAKEEN, 0, BIT3 | BIT2 | BIT1 | BIT0, 1, NULL},
  {PciCfg, PCI_DEVICE_NUMBER_PCH_USB, 0, 0, R_PCH_EHCI_PWR_CNTL_STS + 1, 0, BIT0, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_CONFIGFLAG, 0, BIT0, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 2, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 6, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 10, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 14, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 18, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 22, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 26, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 30, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 34, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 38, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciCfg, PCI_DEVICE_NUMBER_PCH_XHCI, 0, 0, R_PCH_XHCI_PWR_CNTL_STS + 1, 0, BIT0 | BIT7, 1, NULL},
  {PciCfg, PCI_DEVICE_NUMBER_PCH_XHCI, 0, 0, R_PCH_XHCI_USB2PR, 0, 0x7FFF, 2, NULL},
  {PciCfg, PCI_DEVICE_NUMBER_PCH_XHCI, 0, 0, R_PCH_XHCI_USB3PR, 0, 0x3F, 1, NULL},
  {PciCfg, PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS, 0, 0, R_PCH_PCIE_PMCS + 1, 0, BIT0, 1, NULL},
  {PciCfg, PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS, 1, 0, R_PCH_PCIE_PMCS + 1, 0, BIT0, 1, NULL},
  {PciCfg, PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS, 2, 0, R_PCH_PCIE_PMCS + 1, 0, BIT0, 1, NULL},
  {PciCfg, PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS, 3, 0, R_PCH_PCIE_PMCS + 1, 0, BIT0, 1, NULL},
  {PciCfg, PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS, 4, 0, R_PCH_PCIE_PMCS + 1, 0, BIT0, 1, NULL},
  {PciCfg, PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS, 5, 0, R_PCH_PCIE_PMCS + 1, 0, BIT0, 1, NULL}
};

#ifdef TRAD_FLAG
STATIC PCH_SAVE_RESTORE_PCI PchSaveRestorePciReg_PchH[] = {
  {PciCfg, PCI_DEVICE_NUMBER_PCH_USB_EXT, 0, 0, R_PCH_EHCI_PWR_CNTL_STS + 1, 0, BIT0, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB_EXT, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_CONFIGFLAG, 0, BIT0, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB_EXT, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 2, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB_EXT, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 6, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB_EXT, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 10, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB_EXT, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 14, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB_EXT, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 18, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB_EXT, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 22, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB_EXT, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 26, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB_EXT, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 30, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB_EXT, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 34, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_USB_EXT, 0, PCI_BASE_ADDRESSREG_OFFSET, R_PCH_EHCI_PORTSC0 + 38, 0, BIT6 | BIT5 | BIT4, 1, NULL},
  {PciCfg, PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS, 6, 0, R_PCH_PCIE_PMCS + 1, 0, BIT0, 1, NULL},
  {PciCfg, PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS, 7, 0, R_PCH_PCIE_PMCS + 1, 0, BIT0, 1, NULL}
};
#endif // TRAD_FLAG

#ifdef ULT_FLAG
STATIC PCH_SAVE_RESTORE_PCI PchSaveRestorePciReg_PchLp[] = {
  {PciMmr, PCI_DEVICE_NUMBER_PCH_SATA, 2, R_PCH_SATA_AHCI_BAR, R_PCH_SATA_AHCI_P0DEVSLP, 0, 0x1FFFFFFF, 4, RestorePxDevSlp},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_SATA, 2, R_PCH_SATA_AHCI_BAR, R_PCH_SATA_AHCI_P1DEVSLP, 0, 0x1FFFFFFF, 4, RestorePxDevSlp},
  {PciMmr, PCI_DEVICE_NUMBER_PCH_SATA, 2, R_PCH_SATA_AHCI_BAR, R_PCH_SATA_AHCI_P3DEVSLP, 0, 0x1FFFFFFF, 4, RestorePxDevSlp}
};
#endif // ULT_FLAG
DEVICE_POWER_STATE DevicePowerState[] = {
  {PCI_DEVICE_NUMBER_PCH_AZALIA, 0, DeviceD0},
  {PCI_DEVICE_NUMBER_PCH_USB_EXT, 0, DeviceD0},
  {PCI_DEVICE_NUMBER_PCH_USB, 0, DeviceD0},
  {PCI_DEVICE_NUMBER_PCH_XHCI, 0, DeviceD0},
  {PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS, 0, DeviceD0},
  {PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS, 1, DeviceD0},
  {PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS, 2, DeviceD0},
  {PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS, 3, DeviceD0},
  {PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS, 4, DeviceD0},
  {PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS, 5, DeviceD0},
  {PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS, 6, DeviceD0},
  {PCI_DEVICE_NUMBER_PCH_PCIE_ROOT_PORTS, 7, DeviceD0}
};

PCH_SAVE_RESTORE_PCI_WRAP PchSaveRestorePciRegWrap[] = {
#ifdef TRAD_FLAG
  {PchSaveRestorePciReg_Common, ARRAY_SIZE(PchSaveRestorePciReg_Common), PchH},
  {PchSaveRestorePciReg_PchH, ARRAY_SIZE (PchSaveRestorePciReg_PchH), PchH}
#endif // TRAD_FLAG
#if (defined ULT_FLAG) && (defined TRAD_FLAG)
  ,
#endif // ULT_FLAG && TRAD_FLAG
#ifdef ULT_FLAG
  {PchSaveRestorePciReg_Common, ARRAY_SIZE(PchSaveRestorePciReg_Common), PchLp},
  {PchSaveRestorePciReg_PchLp, ARRAY_SIZE(PchSaveRestorePciReg_PchLp), PchLp}
#endif // ULT_FLAG
};

PCH_SAVE_RESTORE_REG_WRAP PchSaveRestoreRegWrap[] = {
#ifdef TRAD_FLAG
  {PchSaveRestoreReg_Common, ARRAY_SIZE(PchSaveRestoreReg_Common), PchH},
  {PchSaveRestoreReg_PchH, ARRAY_SIZE(PchSaveRestoreReg_PchH), PchH}
#endif // TRAD_FLAG
#if (defined ULT_FLAG) && (defined TRAD_FLAG)
  ,
#endif // ULT_FLAG && TRAD_FLAG
#ifdef ULT_FLAG
  {PchSaveRestoreReg_Common, ARRAY_SIZE(PchSaveRestoreReg_Common), PchLp},
  {PchSaveRestoreReg_PchLp, ARRAY_SIZE(PchSaveRestoreReg_PchLp), PchLp}  
#endif // ULT_FLAG
};

UINT32      PciMemBase = 0;

/**

  Find the Offset to a given Capabilities ID
  CAPID list:
    0x01 = PCI Power Management Interface
    0x04 = Slot Identification
    0x05 = MSI Capability
    0x10 = PCI Express Capability

  @param[in] Bus       -   Pci Bus Number
  @param[in] Device    -   Pci Device Number
  @param[in] Func   -   Pci Function Number
  @param[in] CapId     -   CAPID to search for

  @retval 0       - CAPID not found
  @retval Other   - CAPID found, Offset of desired CAPID

**/
UINT8
PcieFindCapId (
  IN UINT8   Bus,
  IN UINT8   Device,
  IN UINT8   Func,
  IN UINT8   CapId
  )
{
  UINT8 CapHeader;

  ///
  /// Always start at Offset 0x34
  ///
  CapHeader = MmioRead8 (MmPciAddress (0, Bus, Device, Func, PCI_CAPBILITY_POINTER_OFFSET));
  if (CapHeader == 0xFF) {
    return 0;
  }

  while (CapHeader != 0) {
    ///
    /// Bottom 2 bits of the pointers are reserved per PCI Local Bus Spec 2.2
    ///
    CapHeader &= ~(BIT1 | BIT0);
    ///
    /// Search for desired CapID
    ///
    if (MmioRead8 (MmPciAddress (0, Bus, Device, Func, CapHeader)) == CapId) {
      return CapHeader;
    }

    CapHeader = MmioRead8 (MmPciAddress (0, Bus, Device, Func, CapHeader + 1));
  }

  return 0;
}

/**

  Save Device Power State and restore later

  @param[in] Device    -   Pci Device Number
  @param[in] Function  -   Pci Function Number

  @retval None

**/
VOID
SaveDevPowState (
  IN UINT8   Device,
  IN UINT8   Function
  )
{
  UINTN    index;

  for (index = 0; index < ARRAY_SIZE(DevicePowerState); index++) {
    if ((DevicePowerState[index].Device == Device) && (DevicePowerState[index].Function) == Function) {
      DevicePowerState[index].PowerState = DeviceD3;
      break;
    }
  }
}

/**

  Restore Device Power State back to D3

  @retval None

**/
VOID
RestoreDevPowState (
  VOID
  )
{
  UINTN    index;
  UINTN    PciBaseAddress;
  UINT8    CapOffset;

  for (index = 0; index < ARRAY_SIZE(DevicePowerState); index++) {
    if (DevicePowerState[index].PowerState == DeviceD3) {
      PciBaseAddress = MmPciAddress(
                         0,
                         0,
                         DevicePowerState[index].Device,
                         DevicePowerState[index].Function,
                         0
                       );
      CapOffset = PcieFindCapId (
                    0,
                    DevicePowerState[index].Device,
                    DevicePowerState[index].Function,
                    EFI_PCI_CAPABILITY_ID_PCIPM
                  );
      if (CapOffset != 0) {
        MmioOr8 (PciBaseAddress + CapOffset + 0x4, DeviceD3);
      }
    }
  }
}

/**
  Restore PxDevSlp

  @param[in] *PchSaveRestorePci  - Pointer to Pch Save Restore Pci to be restored.

  @retval None
**/
VOID
RestorePxDevSlp(
  IN PCH_SAVE_RESTORE_PCI   *PchSaveRestorePci
  )
{
  UINTN  PciBaseAddress;
  UINT32 PciBar;
  UINTN  Address;
  UINT8  PciCmd;
  UINT32 Data32;
  UINT32 PortIndex;

  ASSERT ((PchSaveRestorePci->AccessType == PciMmr) && 
         (PchSaveRestorePci->Device == PCI_DEVICE_NUMBER_PCH_SATA) && 
         ((PchSaveRestorePci->Offset == R_PCH_SATA_AHCI_P0DEVSLP) || 
          (PchSaveRestorePci->Offset == R_PCH_SATA_AHCI_P1DEVSLP) || 
          (PchSaveRestorePci->Offset == R_PCH_SATA_AHCI_P3DEVSLP)) &&
         (PchSaveRestorePci->Width == 4));

  PciBaseAddress = MmPciAddress (
                   0,
                   0,
                   PchSaveRestorePci->Device,
                   PchSaveRestorePci->Function,
                   0
                 );
  PortIndex = (PchSaveRestorePci->Offset - R_PCH_SATA_AHCI_P0DEVSLP)/0x80;
  PciCmd = MmioRead8 (PciBaseAddress + PCI_COMMAND_OFFSET);
  MmioWrite8 (PciBaseAddress + PCI_COMMAND_OFFSET, BIT1);
  PciBar = MmioRead32 (PciBaseAddress + PchSaveRestorePci->BarOffset);
  MmioWrite32 (PciBaseAddress + PchSaveRestorePci->BarOffset, PciMemBase);
  Address = PciMemBase + PchSaveRestorePci->Offset;
  ///
  /// Restore DM and DITO
  ///
  MmioAndThenOr32 (Address, (UINT32)~PchSaveRestorePci->Mask, (PchSaveRestorePci->Data & (B_PCH_SATA_AHCI_PxDEVSLP_DM_MASK | B_PCH_SATA_AHCI_PxDEVSLP_DITO_MASK)));

  ///
  /// Makesure PxCMD.ST and PxDEVSLP.ADSE are cleared to '0' before updating PxDEVSLP.DETO and PxDEVSLP.MDAT value.
  ///
  Data32 = MmioRead32 (PciMemBase + (R_PCH_SATA_AHCI_P0CMD + (0x80 * PortIndex)));
  MmioAnd32 (PciMemBase + (R_PCH_SATA_AHCI_P0CMD + (0x80 * PortIndex)), (UINT32)~B_PCH_SATA_AHCI_PxCMD_ST);
  MmioAnd32 (Address, (UINT32)~B_PCH_SATA_AHCI_PxDEVSLP_ADSE);
  MmioOr32 (Address, (PchSaveRestorePci->Data & (UINT32)~B_PCH_SATA_AHCI_PxDEVSLP_ADSE));
  MmioOr32 (PciMemBase + (R_PCH_SATA_AHCI_P0CMD + (0x80 * PortIndex)), (Data32 & B_PCH_SATA_AHCI_PxCMD_ST));
  MmioOr32 (Address, (PchSaveRestorePci->Data & B_PCH_SATA_AHCI_PxDEVSLP_ADSE));

  ///
  /// Restore original PCI command and bar
  ///
  MmioWrite8 (PciBaseAddress + PCI_COMMAND_OFFSET, PciCmd);
  MmioWrite32 (PciBaseAddress + PchSaveRestorePci->BarOffset, PciBar);
}

/**
  A SMI callback to do PCH SMI register restoration

  @param[in] DispatchHandle  - The handle of this callback, obtained when registering
  @param[in] DispatchContext - Pointer to the EFI_SMM_IO_TRAP_DISPATCH_CALLBACK_CONTEXT

  @retval None
**/
VOID
PchIoTrapSmiCallback (
  IN  EFI_HANDLE                                  DispatchHandle,
  IN  EFI_SMM_IO_TRAP_DISPATCH_CALLBACK_CONTEXT   *CallbackContext
  )
{
  UINT16                  AcpiBase;
  UINT16                  TcoBase;
  UINTN                   index;
  UINTN                   i;
  UINTN                   PciBaseAddress;
  UINTN                   Address;
  UINT8                   PowerState;
  UINT8                   PciCmd;
  UINT32                  PciBar;
  UINT32                  Mask;
  PCH_SERIES              PchSeries;

  AcpiBase   = PchLpcPciCfg32(R_PCH_LPC_ACPI_BASE) & B_PCH_LPC_ACPI_BASE_BAR;
  TcoBase    = AcpiBase + PCH_TCO_BASE;
  PowerState = 0x0;
  PchSeries  = GetPchSeries();

  ///
  /// Restoring IO and MMIO registers
  ///
  for (i = 0; i < ARRAY_SIZE (PchSaveRestoreRegWrap); i++) {
    if (PchSeries != PchSaveRestoreRegWrap[i].PchSeries) {
      continue;
    }
    for (index = 0; index < PchSaveRestoreRegWrap[i].size; index++) {
      Mask = PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Mask;
      switch (PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].AccessType) {
        case Tco:
          Address = TcoBase + PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Address;
          switch (PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Width) { 
            case 4:
              IoAndThenOr32 (Address,  (UINT32) ~Mask, PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data);
              break;
            case 2:
              IoAndThenOr16 (Address, (UINT16) ~Mask, (UINT16) (PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data));
              break;
            case 1:
              IoAndThenOr8 (Address, (UINT8) ~Mask, (UINT8) (PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data));
              break;
          }
          break;          
        case Acpi:
          Address = AcpiBase + PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Address;
          switch (PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Width) {
            case 4:
              IoAndThenOr32 (Address, (UINT32) ~Mask, PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data);
              break;
            case 2:
              IoAndThenOr16 (Address, (UINT16) ~Mask, (UINT16) (PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data));
              break;
            case 1:
              IoAndThenOr8 (Address, (UINT8) ~Mask, (UINT8) (PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data));
              break;
          }
          break;
        case Rcrb:
          Address = PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Address;
          switch (PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Width) {
            case 4:
              PchMmRcrb32AndThenOr (Address, (UINT32) ~Mask, PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data);
              break;
            case 2:
              PchMmRcrb16AndThenOr (Address, (UINT16) ~Mask, (UINT16) (PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data));
              break;
            case 1:
              PchMmRcrb8AndThenOr (Address, (UINT8) ~Mask, (UINT8) (PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data));
              break;
          }
          break;
      }
    }
  }

  ///
  /// Restoring PCI config space and PCI bar registers
  ///
  for (i = 0; i < ARRAY_SIZE (PchSaveRestorePciRegWrap); i++) {
    if (PchSeries != PchSaveRestorePciRegWrap[i].PchSeries) {
      continue;
    }
    for (index = 0; index < PchSaveRestorePciRegWrap[i].size; index++) {
      PciBaseAddress = MmPciAddress(
                         0,
                         0,
                         PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Device,
                         PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Function,
                         0
                       );
      if (MmioRead32(PciBaseAddress) == 0xFFFFFFFF) {
        continue;
      }
      if (PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].RestoreFunction == NULL) {
        Mask = PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Mask;
        switch (PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].AccessType) {
          case PciCfg:
            Address = PciBaseAddress + PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Offset;
            switch (PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Width) {
              case 4:
                MmioAndThenOr32 (Address, (UINT32) ~Mask, PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Data);
                break;
              case 2:
                MmioAndThenOr16 (Address, (UINT16) ~Mask, (UINT16) (PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Data));
                break;
              case 1:
                MmioAndThenOr8 (Address, (UINT8) ~Mask, (UINT8) (PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Data));
                break;
            }
            break;
          case PciMmr:
            PciCmd = MmioRead8 (PciBaseAddress + PCI_COMMAND_OFFSET);
            MmioWrite8 (PciBaseAddress + PCI_COMMAND_OFFSET, BIT1);
            PciBar = MmioRead32 (PciBaseAddress + PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].BarOffset);
            MmioWrite32 (PciBaseAddress + PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].BarOffset, PciMemBase);
            Address = PciMemBase + PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Offset;
            switch (PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Width) {
              case 4:
                MmioAndThenOr32 (Address, (UINT32) ~Mask, PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Data);
                break;
              case 2:
                MmioAndThenOr16 (Address, (UINT16) ~Mask, (UINT16) (PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Data));
                break;
              case 1:
                MmioAndThenOr8 (Address, (UINT8) ~Mask, (UINT8) (PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Data));
                break;
            }
            ///
            /// Restore original PCI command and bar
            ///
            MmioWrite8 (PciBaseAddress + PCI_COMMAND_OFFSET, PciCmd);
            MmioWrite32 (PciBaseAddress + PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].BarOffset, PciBar);
            break;
        }
      } else {
        PchSaveRestorePciRegWrap[i].PchSaveRestorePci [index].RestoreFunction (&(PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index]));
      }
    }
  }
}

/**
  This function save PCH register before enter S3

  @param[in] Handle    Handle of the callback
  @param[in] Context   The dispatch context

  @retval EFI_SUCCESS  PCH register saved
**/
EFI_STATUS
PchS3EntryCallBack (
  IN  EFI_HANDLE                        Handle,
  IN  EFI_SMM_SX_DISPATCH_CONTEXT       *Context
  )
{
  UINT32                  AcpiBase;
  UINT32                  TcoBase;
  UINTN                   PciBaseAddress;
  UINTN                   index;
  UINTN                   i;
  UINT8                   PowerState;
  UINT8                   CapOffset;
  UINT32                  PciBar;
  UINT32                  Mask;
  UINT32                  Address;
  PCH_SERIES              PchSeries;

  AcpiBase   = PchLpcPciCfg32(R_PCH_LPC_ACPI_BASE) & B_PCH_LPC_ACPI_BASE_BAR;
  TcoBase    = AcpiBase + PCH_TCO_BASE;
  PowerState = 0x0;
  PchSeries  = GetPchSeries();

  ///
  /// Saving IO and MMIO registers
  ///
  for (i = 0; i < ARRAY_SIZE (PchSaveRestoreRegWrap); i++) {
    if (PchSeries != PchSaveRestoreRegWrap[i].PchSeries) {
      continue;
    }
    for (index = 0; index < PchSaveRestoreRegWrap[i].size; index++) {
      Mask = PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Mask;
      switch (PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].AccessType) {
        case Tco:
          Address = TcoBase + PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Address;
          switch (PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Width) {
            case 4:
              PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data = (IoRead32 (Address)) & Mask;
              break;
            case 2:
              PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data = (IoRead16 (Address)) & Mask;
              break;
            case 1:
              PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data = (IoRead8 (Address)) & Mask;
              break;
          }
          break;
        case Acpi:
          Address = AcpiBase + PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Address;
          switch (PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Width) {
            case 4:
              PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data = (IoRead32 (Address)) & Mask;
              break;
            case 2:
              PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data = (IoRead16 (Address)) & Mask;
              break;
            case 1:
              PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data = (IoRead8 (Address)) & Mask;
              break;
          }
          break;
        case Rcrb:
          Address = PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Address;
          switch (PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Width) {
            case 4:
              PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data = (PchMmRcrb32 (Address)) & Mask;
              break;
            case 2:
              PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data = (PchMmRcrb16 (Address)) & Mask;
              break;
            case 1:
              PchSaveRestoreRegWrap[i].PchSaveRestoreReg[index].Data = (PchMmRcrb8 (Address)) & Mask;
              break;
          }
          break;
      }
    }
  }

  ///
  /// Saving PCI config space and PCI bar registers
  ///
  for (i = 0; i < ARRAY_SIZE (PchSaveRestorePciRegWrap); i++) {
    if (PchSeries != PchSaveRestorePciRegWrap[i].PchSeries) {
      continue;
    }
    for (index = 0; index < PchSaveRestorePciRegWrap[i].size; index++) {
      PciBaseAddress = MmPciAddress(
                         0,
                         0,
                         PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Device,
                         PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Function,
                         0
                       );
      if (MmioRead32(PciBaseAddress) == 0xFFFFFFFF) {
        continue;
      }
      Mask = PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Mask;
      switch (PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].AccessType) {
        case PciCfg:
          Address = (UINT32) PciBaseAddress + PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Offset;
          switch (PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Width) {
            case 4:
              PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Data = (MmioRead32 (Address)) & Mask;
              break;
            case 2:
              PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Data = (MmioRead16 (Address)) & Mask;
               break;
            case 1:
              PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Data = (MmioRead8 (Address)) & Mask;
              break;
          }
          break;
        case PciMmr:
          PciBar = MmioRead32 (PciBaseAddress + PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].BarOffset);
          CapOffset = PcieFindCapId (
                        0,
                        PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Device,
                        PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Function,
                        EFI_PCI_CAPABILITY_ID_PCIPM
                      );
          if (CapOffset != 0) {
            PowerState = MmioRead8(PciBaseAddress + CapOffset + 0x4) & (BIT1 | BIT0);
            if (PowerState == DeviceD3) {
              ///
              /// Bring up device to D0
              ///
              MmioAnd8 (PciBaseAddress + CapOffset + 0x4, (UINT8)~(BIT1 | BIT0));
              MmioWrite32 (PciBaseAddress + PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].BarOffset, PciBar);
              MmioWrite8 (PciBaseAddress + PCI_COMMAND_OFFSET, BIT1);
              SaveDevPowState(PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Device, PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Function);
            }
          }
          PciBar = PciBar & (UINT32)~(0xF);
          if (PciBar == 0x0) {
            continue;
          }
          Address = PciBar + PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Offset;
          switch (PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Width) {
            case 4:
              PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Data = (MmioRead32 (Address)) & Mask;
              break;
            case 2:
              PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Data = (MmioRead16 (Address)) & Mask;
              break;
            case 1:
              PchSaveRestorePciRegWrap[i].PchSaveRestorePci[index].Data = (MmioRead8 (Address)) & Mask;
              break;
          }
          break;
      }
    }
  }

  ///
  /// Restore devices to D3
  ///
  RestoreDevPowState();

  return EFI_SUCCESS;
}

/**
  Initializes the PCH SMM handler for PCH save and restore

  @param[in] ImageHandle - Handle for the image of this driver
  @param[in] SystemTable - Pointer to the EFI System Table

  @retval EFI_SUCCESS    - PCH SMM handler was installed
**/
EFI_STATUS
PchLateInitSmmEntryPoint (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS                                Status;
  EFI_PHYSICAL_ADDRESS                      MemBase;
  EFI_SMM_IO_TRAP_DISPATCH_PROTOCOL         *PchIoTrap;
  EFI_SMM_IO_TRAP_DISPATCH_REGISTER_CONTEXT PchIoTrapContext;
  EFI_HANDLE                                PchIoTrapHandle;
  EFI_SMM_SX_DISPATCH_CONTEXT               SxDispatchContext;
  EFI_SMM_SX_DISPATCH_PROTOCOL              *SxDispatchProtocol;
  EFI_HANDLE                                SxDispatchHandle;
  PCH_LATE_INIT_SMM_VARIABLE                SaveRestoreData;

  DEBUG ((EFI_D_INFO, "PchLateInitSmmEntryPoint()\n"));

  ///
  /// Locate the PCH Trap dispatch protocol
  ///
  Status = gBS->LocateProtocol (&gEfiSmmIoTrapDispatchProtocolGuid, NULL, &PchIoTrap);
  ASSERT_EFI_ERROR (Status);

  PchIoTrapContext.Type     = ReadWriteTrap;
  PchIoTrapContext.Length   = 4;
  PchIoTrapContext.Address  = 0;
  PchIoTrapContext.Context  = NULL;
  PchIoTrapContext.MergeDisable = FALSE;
  Status = PchIoTrap->Register (
                        PchIoTrap,
                        PchIoTrapSmiCallback,
                        &PchIoTrapContext,
                        &PchIoTrapHandle
                        );
  ASSERT_EFI_ERROR (Status);

  SaveRestoreData.IoTrapAddress = PchIoTrapContext.Address;

  DEBUG ((EFI_D_INFO, "PchIotrapSmiAddress = 0x%x\n", PchIoTrapContext.Address));

  ///
  /// Locate the Sx Dispatch Protocol
  ///
  Status = gBS->LocateProtocol (
                  &gEfiSmmSxDispatchProtocolGuid,
                  NULL,
                  &SxDispatchProtocol
                  );
  ASSERT_EFI_ERROR (Status);
  ///
  /// Register the callback for S3 entry
  ///
  SxDispatchContext.Type  = SxS3;
  SxDispatchContext.Phase = SxEntry;
  Status = SxDispatchProtocol->Register (
                                SxDispatchProtocol,
                                PchS3EntryCallBack,
                                &SxDispatchContext,
                                &SxDispatchHandle
                                );
  ASSERT_EFI_ERROR (Status);

  MemBase = 0x0ffffffff;
#ifndef AMI_OVERRIDE_FOR_SMM
  Status = gDS->AllocateMemorySpace (
                  EfiGcdAllocateMaxAddressSearchTopDown,
                  EfiGcdMemoryTypeMemoryMappedIo,
                  16,  // 2^16: 64K Alignment
                  0x10000,     // 64K Length
                  &MemBase,
                  ImageHandle,
                  NULL
                  );
#else
  Status = gDS->AllocateMemorySpace (
                  EfiGcdAllocateMaxAddressSearchBottomUp,
                  EfiGcdMemoryTypeMemoryMappedIo,
                  16,  // 2^16: 64K Alignment
                  0x10000,     // 64K Length
                  &MemBase,
                  ImageHandle,
                  NULL
                  );
#endif // AMI_OVERRIDE_FOR_SMM
  ASSERT_EFI_ERROR (Status);

  PciMemBase = (UINT32) MemBase;

  SaveRestoreData.PciMemBase = PciMemBase;

  Status = gRT->SetVariable (
                  PCH_INIT_PEI_VARIABLE_NAME,
                  &gPchInitPeiVariableGuid,
                  EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
                  sizeof (PCH_LATE_INIT_SMM_VARIABLE),
                  &SaveRestoreData
                  );
  ASSERT_EFI_ERROR (Status);

  return EFI_SUCCESS;
}