summaryrefslogtreecommitdiff
path: root/ReferenceCode/Chipset/SystemAgent/SaInit/Pei/PcieTrainingLinkRecovery.c
blob: 1480bc78804c32e5e0c4e39065af661b2e9e7da5 (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
/*++ @file
  This driver recovers the PEG link.

@copyright
  Copyright (c) 2012 - 2014 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 "PcieTraining.h"
#include "PciExpressInit.h"
#include EFI_PPI_CONSUMER (PchReset)

#ifdef PEG_FLAG

EFI_STATUS
EnsureLinkIsHealthy (
  IN EFI_PEI_SERVICES       **PeiServices,
  IN SA_PLATFORM_POLICY_PPI *SaPlatformPolicyPpi,
  IN SA_DATA_HOB            *SaDataHob,
  IN PEI_STALL_PPI          *StallPpi,
  IN PEG_PORT               *PegPort,
  IN UINT8                  OriginalLinkSpeed,
  IN UINT8                  OriginalLinkWidth
  )
{
  EFI_STATUS Status;

  Status = EFI_SUCCESS;

  ///
  /// Check Link Status and Recover the Link if needed
  ///
  Status = RecoverLinkWidth (PeiServices, StallPpi, PegPort, OriginalLinkWidth);
  if (EFI_ERROR (Status)) {
    Status = RecoverLinkFailure (PeiServices, SaPlatformPolicyPpi, SaDataHob, StallPpi, PegPort, OriginalLinkSpeed, OriginalLinkWidth);
    if (EFI_ERROR (Status)) {
      return Status;
    }
  }
  Status = RecoverLinkSpeed (PeiServices, StallPpi, PegPort, OriginalLinkSpeed);
  if (EFI_ERROR (Status)) {
    Status = RecoverLinkFailure (PeiServices, SaPlatformPolicyPpi, SaDataHob, StallPpi, PegPort, OriginalLinkSpeed, OriginalLinkWidth);
    if (EFI_ERROR (Status)) {
      return Status;
    }
  }
  Status = WaitForL0 (PeiServices, StallPpi, PegPort, FALSE);
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_INFO, "DOWNGRADE, Link is not in L0\n"));
    Status = RetrainLink (PeiServices, StallPpi, PegPort);
    if (EFI_ERROR (Status)) {
      Status = RecoverLinkFailure (PeiServices, SaPlatformPolicyPpi, SaDataHob, StallPpi, PegPort, OriginalLinkSpeed, OriginalLinkWidth);
      if (EFI_ERROR (Status)) {
        return Status;
      }
    }
  }

  return Status;
}

/**
  Wait until link is up.

  @param[in] PeiServices  - Pointer to the PEI services table
  @param[in] StallPpi     - Pointer to PEI_STALL_PPI
  @param[in] PegPort      - Pointer PEG Port
  @param[in] UseVcu       - If TRUE, use VCU to determine link state.  If FALSE, use MMIO CFG to determine link state.

  @retval EFI_SUCCESS     - Completed successfully before timeout
  @retval EFI_TIMEOUT     - Timed out
**/
EFI_STATUS
WaitForL0 (
  IN EFI_PEI_SERVICES **PeiServices,
  IN PEI_STALL_PPI    *StallPpi,
  IN PEG_PORT         *PegPort,
  IN BOOLEAN          UseVcu
  )
{
  UINT32        MchBar;
  UINT32        i;
  EFI_STATUS    Status;
  UINT32        VcuAddress;
  UINT8         VcuReadOp;
  UINT8         VcuWriteOp;
  CPU_FAMILY    CpuFamilyId;
  CPU_STEPPING  CpuSteppingId;
  BOOLEAN       CheckEq;
  BOOLEAN       CompletedEq;
  UINT32        EqStatus;
  UINT32        LinkStatus;

  Status         = EFI_TIMEOUT;
  CheckEq        = (PegPort->EndpointMaxLinkSpeed >= 0x3) ? TRUE : FALSE;
  CompletedEq    = FALSE;
  i              = 0;
  MchBar         = McD0PciCfg64 (R_SA_MCHBAR) & ~BIT0;
  CpuFamilyId    = GetCpuFamily();
  CpuSteppingId  = GetCpuStepping();

#ifndef AMI_OVERRIDE_FOR_ULT_FASTBOOT
  if (CpuFamilyId == EnumCpuHswUlt) return EFI_UNSUPPORTED;
#endif // AMI_OVERRIDE_FOR_ULT_FASTBOOT

  if ((CpuFamilyId == EnumCpuHsw) && (CpuSteppingId == EnumHswA0)) {
    VcuAddress = R_SA_VCU_REUT_PH1_PIS_ADDRESS_REV1;
    VcuReadOp  = V_SA_VCU_OPCODE_READ_CSR_REV1;
    VcuWriteOp = V_SA_VCU_OPCODE_WRITE_CSR_REV1;
  } else {
    VcuAddress = R_SA_VCU_REUT_PH1_PIS_ADDRESS_REV2;
    VcuReadOp  = V_SA_VCU_OPCODE_READ_CSR_REV2;
    VcuWriteOp = V_SA_VCU_OPCODE_WRITE_CSR_REV2;
  }

  ///
  /// If endpoint's LCAP.MLS (Spec section 7.8.6) indicated Gen3 capability, first wait for equalization to complete.
  /// Check equalization status LSTS2.EC (Spec section 7.8.20) until Gen3 equalization successfully completed.
  ///
  if (CheckEq && !UseVcu) {
    for (; i < 100; i++) {
      EqStatus = MmPci16 (0, PegPort->Bus, PegPort->Device, PegPort->Function, R_SA_PEG_LSTS2_OFFSET);
      EqStatus = (EqStatus >> 1) & 0x1;
      if (EqStatus == 0x1) {
        CompletedEq = TRUE;
        break;
      }
      StallPpi->Stall (PeiServices, StallPpi, STALL_ONE_MILLI_SECOND);
    }
  }

  ///
  /// Check for L0 status.  If !UseVcu, check PEGSTS.
  /// Continue up to 100 msec of combined delay.
  /// Skip if equalization was needed but didn't successfully complete.
  ///
  if ((CheckEq && CompletedEq) || !CheckEq || UseVcu) {
    for (; i < 100; i++) {
      if (UseVcu) {
        LinkStatus = SendVcuApiSequence (MchBar, VcuAddress, VcuReadOp, 0);
        LinkStatus = (LinkStatus >> (PegPort->Function * B_SA_PEG_REUT_PH1_PIS_ST_STEP)) & B_SA_PEG_REUT_PH1_PIS_ST_MASK;
        if (LinkStatus == 0x10) {
          Status = EFI_SUCCESS;
          break;
        }
      } else {
        LinkStatus = MmPci32 (0, PegPort->Bus, PegPort->Device, PegPort->Function, R_SA_PEG_PEGSTS_OFFSET);
        LinkStatus = (LinkStatus >> 16) & 0xF;
        if (LinkStatus == 0x7) {
          Status = EFI_SUCCESS;
          break;
        }
      }
      StallPpi->Stall (PeiServices, StallPpi, STALL_ONE_MILLI_SECOND);
    }
  }

  return Status;
}

EFI_STATUS
SetPchGpio (
  IN UINT8    GpioNumber,
  IN UINT8    Level
  )
/**
  This function sets a GPIO to a particular level.

  @param[in] GpioNumber          - PCH GPIO number
  @param[in] Level               - 0 = Low, 1 = High

  @retval EFI_SUCCESS            - Did toggle GPIO
  @retval EFI_UNSUPPORTED        - Didn't toggle GPIO
  @retval EFI_INVALID_PARAMETER  - Didn't toggle GPIO
**/
{
  UINT32      Data32;
  UINT16      LpcDeviceId;
  UINT16      GpioBase;
  UINT16      UseSelOffset;
  UINT16      IoSelOffset;
  UINT16      LvlOffset;
  UINT8       GpioBit;
  EFI_STATUS  Status;

  Level       &= 0x1;
  Status       = EFI_SUCCESS;
  GpioBase     = 0;
  UseSelOffset = 0;
  IoSelOffset  = 0;
  LvlOffset    = 0;
  GpioBit      = 0;

  LpcDeviceId = McDevFunPciCfg16 (
                  DEFAULT_PCI_BUS_NUMBER_PCH,
                  PCI_DEVICE_NUMBER_PCH_LPC,
                  PCI_FUNCTION_NUMBER_PCH_LPC,
                  R_PCH_LPC_DEVICE_ID
                  );
  if (!IS_PCH_LPT_LPC_DEVICE_ID (LpcDeviceId)) {
    Status = EFI_UNSUPPORTED;
    return Status;
  }

  GpioBase = McDevFunPciCfg16 (
               DEFAULT_PCI_BUS_NUMBER_PCH,
               PCI_DEVICE_NUMBER_PCH_LPC,
               PCI_FUNCTION_NUMBER_PCH_LPC,
               R_PCH_LPC_GPIO_BASE
               ) & B_PCH_LPC_GPIO_BASE_BAR;
  if (GpioBase == 0) {
    Status = EFI_UNSUPPORTED;
    return Status;
  }

  if (GpioNumber < 0x20) {
    UseSelOffset = R_PCH_GPIO_USE_SEL;
    IoSelOffset  = R_PCH_GPIO_IO_SEL;
    LvlOffset    = R_PCH_GPIO_LVL;
    GpioBit      = GpioNumber;
  } else if (GpioNumber < 0x40) {
    UseSelOffset = R_PCH_GPIO_USE_SEL2;
    IoSelOffset  = R_PCH_GPIO_IO_SEL2;
    LvlOffset    = R_PCH_GPIO_LVL2;
    GpioBit      = GpioNumber - 0x20;
  } else if (GpioNumber < 0x60) {
    UseSelOffset = R_PCH_GPIO_USE_SEL3;
    IoSelOffset  = R_PCH_GPIO_IO_SEL3;
    LvlOffset    = R_PCH_GPIO_LVL3;
    GpioBit      = GpioNumber - 0x40;
  } else {
    Status = EFI_INVALID_PARAMETER;
    return Status;
  }

  if (!EFI_ERROR (Status)) {
    DEBUG ((EFI_D_INFO, "Setting GPIO%d to %x\n", GpioNumber, Level));
    IoOr32    ((UINTN) (GpioBase + UseSelOffset), (UINT32)  (1 << GpioBit));
    IoAnd32   ((UINTN) (GpioBase + IoSelOffset),  (UINT32) ~(1 << GpioBit));
    Data32   = IoRead32  ((UINTN) (GpioBase + LvlOffset));
    Data32  &= (UINT32) ~(1     << GpioBit);
    Data32  |= (UINT32)  (Level << GpioBit);
    IoWrite32 ((UINTN) (GpioBase + LvlOffset), Data32);
  }

  return Status;
}

EFI_STATUS
TogglePegSlotReset (
  IN EFI_PEI_SERVICES       **PeiServices,
  IN PEI_STALL_PPI          *StallPpi,
  IN SA_PLATFORM_POLICY_PPI *SaPlatformPolicyPpi
  )
/**
  This function asserts and deasserts a GPIO that controls PERST#.
  The specific GPIO and its active level is provided by a policy.
  The GPIO minimum assertion time, T_PERST (100 usec), is defined in the PCIe CEM Specification.

  @param[in] PeiServices         - Pointer to the PEI services table
  @param[in] StallPpi            - Pointer to PEI_STALL_PPI
  @param[in] SaPlatformPolicyPpi - Pointer to SA_PLATFORM_POLICY_PPI

  @retval EFI_SUCCESS            - Did toggle GPIO
  @retval EFI_UNSUPPORTED        - Didn't toggle GPIO
  @retval EFI_INVALID_PARAMETER  - Didn't toggle GPIO
**/
{
  EFI_STATUS  Status;
  UINT8       i;

  DEBUG ((EFI_D_INFO, "Toggling PEG slot reset.\n"));
  for (i = 0; i < SA_PEG_MAX_FUN; i++) {
    MmPci16Or (0, SA_PEG_BUS_NUM, SA_PEG_DEV_NUM, i, R_SA_PEG_LCTL_OFFSET, BIT4);
  }
  Status = AssertPegSlotReset (SaPlatformPolicyPpi);
  if (!EFI_ERROR (Status)) {
    StallPpi->Stall (PeiServices, StallPpi, 100 * STALL_ONE_MICRO_SECOND);
    for (i = 0; i < SA_PEG_MAX_FUN; i++) {
      MmPci16And (0, SA_PEG_BUS_NUM, SA_PEG_DEV_NUM, i, R_SA_PEG_LCTL_OFFSET, (UINT16) ~(BIT4));
    }
    Status = DeassertPegSlotReset (SaPlatformPolicyPpi);
  } else {
    for (i = 0; i < SA_PEG_MAX_FUN; i++) {
      MmPci16And (0, SA_PEG_BUS_NUM, SA_PEG_DEV_NUM, i, R_SA_PEG_LCTL_OFFSET, (UINT16) ~(BIT4));
    }
  }

  return Status;
}

EFI_STATUS
AssertPegSlotReset (
  IN SA_PLATFORM_POLICY_PPI *SaPlatformPolicyPpi
  )
/**
  This function asserts a GPIO that controls PERST#.
  The specific GPIO and its active level is provided by a policy.
  The GPIO minimum assertion time, T_PERST (100 usec), is defined in the PCIe CEM Specification.

  @param[in] SaPlatformPolicyPpi - Pointer to SA_PLATFORM_POLICY_PPI

  @retval EFI_SUCCESS            - Did assert GPIO
  @retval EFI_UNSUPPORTED        - Didn't assert GPIO
  @retval EFI_INVALID_PARAMETER  - Didn't assert GPIO
**/
{
  EFI_STATUS  Status;
  UINT8       GpioNumber;
  UINT8       AssertLevel;

  Status = EFI_SUCCESS;

  DEBUG ((EFI_D_INFO, "Asserting PEG slot reset.\n"));

  if (!((SaPlatformPolicyPpi->Revision >= SA_PLATFORM_POLICY_PPI_REVISION_3) &&
        (SaPlatformPolicyPpi->PcieConfig->PegGpioData->GpioSupport == TRUE))) {
    Status = EFI_UNSUPPORTED;
  }

  if (!EFI_ERROR (Status)) {
    GpioNumber  = (UINT8) (SaPlatformPolicyPpi->PcieConfig->PegGpioData->SaPegReset->Value);
    AssertLevel = (UINT8) (SaPlatformPolicyPpi->PcieConfig->PegGpioData->SaPegReset->Active & 0x1);
    Status      = SetPchGpio (GpioNumber, AssertLevel);
  }

  return Status;
}

EFI_STATUS
DeassertPegSlotReset (
  IN SA_PLATFORM_POLICY_PPI *SaPlatformPolicyPpi
  )
/**
  This function deasserts a GPIO that controls PERST#.
  The specific GPIO and its active level is provided by a policy.
  The GPIO minimum assertion time, T_PERST (100 usec), is defined in the PCIe CEM Specification.

  @param[in] SaPlatformPolicyPpi - Pointer to SA_PLATFORM_POLICY_PPI

  @retval EFI_SUCCESS            - Did deassert GPIO
  @retval EFI_UNSUPPORTED        - Didn't deassert GPIO
  @retval EFI_INVALID_PARAMETER  - Didn't deassert GPIO
**/
{
  EFI_STATUS  Status;
  UINT8       GpioNumber;
  UINT8       DeassertLevel;

  Status = EFI_SUCCESS;

  DEBUG ((EFI_D_INFO, "Deasserting PEG slot reset.\n"));

  if (!((SaPlatformPolicyPpi->Revision >= SA_PLATFORM_POLICY_PPI_REVISION_3) &&
        (SaPlatformPolicyPpi->PcieConfig->PegGpioData->GpioSupport == TRUE))) {
    Status = EFI_UNSUPPORTED;
  }

  if (!EFI_ERROR (Status)) {
    GpioNumber    = (UINT8)  (SaPlatformPolicyPpi->PcieConfig->PegGpioData->SaPegReset->Value);
    DeassertLevel = (UINT8) ((SaPlatformPolicyPpi->PcieConfig->PegGpioData->SaPegReset->Active & 0x1) ^ 0x1);
    SetPchGpio (GpioNumber, DeassertLevel);
  }

  return Status;
}

EFI_STATUS
RecoverLinkFailure (
  IN EFI_PEI_SERVICES       **PeiServices,
  IN SA_PLATFORM_POLICY_PPI *SaPlatformPolicyPpi,
  IN SA_DATA_HOB            *SaDataHob,
  IN PEI_STALL_PPI          *StallPpi,
  IN PEG_PORT               *PegPort,
  IN UINT8                  OriginalLinkSpeed,
  IN UINT8                  OriginalLinkWidth
  )
{
  EFI_STATUS Status;
  UINT8      CurrentLinkWidth;
  UINT8      CurrentLinkSpeed;
  UINT8      PegBus;
  UINT8      PegDev;
  UINT8      PegFunc;

  ///
  /// A platform reset should be done after presets are saved in NVRAM
  ///
  if (SaDataHob != NULL) {
    if ((SaPlatformPolicyPpi->Revision >= SA_PLATFORM_POLICY_PPI_REVISION_2) &&
        (SaPlatformPolicyPpi->PcieConfig->PegGen3ForcePresetSearch == 0)) {
      SaDataHob->PegPlatformResetRequired = TRUE;
    } else if (SaPlatformPolicyPpi->Revision < SA_PLATFORM_POLICY_PPI_REVISION_2) {
      SaDataHob->PegPlatformResetRequired = TRUE;
    }
  }

  ///
  /// Bypass phase2 and assert slot reset
  ///
  McD1PciCfg32Or (R_SA_PEG_EQCFG_OFFSET, BIT15);
  Status = TogglePegSlotReset (PeiServices, StallPpi, SaPlatformPolicyPpi);
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_INFO, "Error 0x%x.\n", Status));
  } else {
    DEBUG ((EFI_D_INFO, "Success.\n"));
  }
  PegBus  = PegPort->Bus;
  PegDev  = PegPort->Device;
  PegFunc = PegPort->Function;
  ///
  /// Wait for Equalization Done
  ///
  while (((MmPci32 (0, PegBus, PegDev, PegFunc, R_SA_PEG_LSTS2_OFFSET) >> 1) & 0x1) != 0x1);
  ///
  /// Wait for flow control credits exchange
  ///
  WaitForVc0Negotiation (PeiServices, StallPpi, PegBus, PegDev, PegFunc);

  CurrentLinkWidth = GetNegotiatedWidth (PegPort);
  if (CurrentLinkWidth < OriginalLinkWidth) {
    DEBUG ((EFI_D_ERROR, "Link Width DOWNGRADED!\n"));
    Status = EFI_TIMEOUT;
  }
  CurrentLinkSpeed = GetLinkSpeed (PegPort);
  if (CurrentLinkSpeed < OriginalLinkSpeed) {
    DEBUG ((EFI_D_ERROR, "Link Speed DOWNGRADED!\n"));
    Status = EFI_TIMEOUT;
  }

  return Status;
}

BOOLEAN
LinkIsDowngraded (
  IN PEG_PORT *PegPort,
  IN UINT8    OriginalLinkSpeed,
  IN UINT8    OriginalLinkWidth
  )
{
  BOOLEAN   IsDowngraded;

  IsDowngraded = FALSE;

  if (OriginalLinkSpeed != GetLinkSpeed (PegPort)) {
    DEBUG ((EFI_D_INFO, "Link speed downgrade detected\n"));
    IsDowngraded = TRUE;
  }
  if (OriginalLinkWidth != GetNegotiatedWidth (PegPort)) {
    DEBUG ((EFI_D_INFO, "Link width downgrade detected\n"));
    IsDowngraded = TRUE;
  }
  return IsDowngraded;
}

EFI_STATUS
SecondaryBusReset (
  IN EFI_PEI_SERVICES **PeiServices,
  IN PEI_STALL_PPI    *StallPpi,
  IN PEG_PORT         *PegPort
  )
{
  EFI_STATUS Status;

  Status = EFI_SUCCESS;
  DEBUG ((EFI_D_INFO, "SECONDARY BUS RESET!\n"));
  MmPci16Or (0, PegPort->Bus, PegPort->Device, PegPort->Function, R_SA_PEG_BCTRL_OFFSET, B_SA_PEG_BCTRL_SRESET_MASK);
  MmPci16And (0, PegPort->Bus, PegPort->Device, PegPort->Function, R_SA_PEG_BCTRL_OFFSET,~(B_SA_PEG_BCTRL_SRESET_MASK));
  Status = WaitForL0 (PeiServices, StallPpi, PegPort, FALSE);
  DEBUG ((EFI_D_INFO, "Reset Complete\n"));

  return Status;
}

EFI_STATUS
ResetPhyLayer (
  IN EFI_PEI_SERVICES **PeiServices,
  IN PEI_STALL_PPI    *StallPpi,
  IN PEG_PORT         *PegPort
  )
{
  EFI_STATUS        Status;
  UINT32            MchBar;
  UINT32            Data32;
  UINT32            VcuAddress;
  UINT8             VcuReadOp;
  UINT8             VcuWriteOp;
  BOOLEAN           UseVcu;
  CPU_FAMILY        CpuFamilyId;
  CPU_STEPPING      CpuSteppingId;

  UseVcu        = TRUE;
  Status        = EFI_SUCCESS;
  MchBar        = McD0PciCfg64 (R_SA_MCHBAR) &~BIT0;
  CpuFamilyId   = GetCpuFamily();
  CpuSteppingId = GetCpuStepping();

#ifndef AMI_OVERRIDE_FOR_ULT_FASTBOOT
  if (CpuFamilyId == EnumCpuHswUlt) return EFI_UNSUPPORTED;
#endif // AMI_OVERRIDE_FOR_ULT_FASTBOOT

  if ((CpuFamilyId == EnumCpuHsw) && (CpuSteppingId == EnumHswA0)) {
    VcuAddress = R_SA_VCU_REUT_PH_CTR_ADDRESS_REV1;
    VcuReadOp  = V_SA_VCU_OPCODE_READ_CSR_REV1;
    VcuWriteOp = V_SA_VCU_OPCODE_WRITE_CSR_REV1;
  } else {
    VcuAddress = R_SA_VCU_REUT_PH_CTR_ADDRESS_REV2;
    VcuReadOp  = V_SA_VCU_OPCODE_READ_CSR_REV2;
    VcuWriteOp = V_SA_VCU_OPCODE_WRITE_CSR_REV2;
  }

  DEBUG ((EFI_D_INFO, "PHY LAYER RESET!\n"));
  if (UseVcu) {
    Data32  = SendVcuApiSequence (MchBar, VcuAddress, VcuReadOp, 0);
    Data32 &= (UINT32) ~(BIT1);
    SendVcuApiSequence (MchBar, VcuAddress, VcuWriteOp, Data32);
  } else {
    McD1PciCfg32And (R_SA_PEG_REUT_PH_CTR_OFFSET, ~(B_SA_PEG_REUT_PH_CTR_RESETMOD_MASK));
  }

  if (UseVcu) {
    Data32  = SendVcuApiSequence (MchBar, VcuAddress, VcuReadOp, 0);
    Data32 |= (UINT32) BIT0;
    SendVcuApiSequence (MchBar, VcuAddress, VcuWriteOp, Data32);
  } else {
    McD1PciCfg32Or (R_SA_PEG_REUT_PH_CTR_OFFSET, B_SA_PEG_REUT_PH_CTR_PHYRESET_MASK);
  }

  if (UseVcu) {
    Data32  = SendVcuApiSequence (MchBar, VcuAddress, VcuReadOp, 0);
    Data32 |= (UINT32) BIT13;
    SendVcuApiSequence (MchBar, VcuAddress, VcuWriteOp, Data32);
  } else {
    McD1PciCfg32Or (R_SA_PEG_REUT_PH_CTR_OFFSET, B_SA_PEG_REUT_PH_CTR_AUTOCOMP_MASK);
  }

  if (UseVcu) {
    Data32  = SendVcuApiSequence (MchBar, VcuAddress, VcuReadOp, 0);
    Data32 &= (UINT32) ~(BIT0);
    SendVcuApiSequence (MchBar, VcuAddress, VcuWriteOp, Data32);
  } else {
    McD1PciCfg32And (R_SA_PEG_REUT_PH_CTR_OFFSET, ~(B_SA_PEG_REUT_PH_CTR_PHYRESET_MASK));
  }

  Status = WaitForL0 (PeiServices, StallPpi, PegPort, TRUE);
  DEBUG ((EFI_D_INFO, "Reset Complete\n"));

  return Status;
}

EFI_STATUS
RetrainLink (
  IN EFI_PEI_SERVICES **PeiServices,
  IN PEI_STALL_PPI    *StallPpi,
  IN PEG_PORT         *PegPort
  )
{
  EFI_STATUS Status;

  ///
  /// Initiate Link Retrain
  ///
  MmPci16Or (0, PegPort->Bus, PegPort->Device, PegPort->Function, R_SA_PEG_LCTL_OFFSET, 0x20);

  Status = WaitForL0 (PeiServices, StallPpi, PegPort, FALSE);
  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_INFO, "Link retrain FAILED!!!\n"));
  }

  return Status;
}

UINT8
GetNegotiatedWidth (
  IN PEG_PORT *PegPort
  )
{
  UINT16 Lsts;

  Lsts = MmPci16 (0, PegPort->Bus, PegPort->Device, PegPort->Function, R_SA_PEG_LSTS_OFFSET);

  return (UINT8)((Lsts >> 4) & 0x3F);
}

EFI_STATUS
RecoverLinkWidth (
  IN EFI_PEI_SERVICES **PeiServices,
  IN PEI_STALL_PPI    *StallPpi,
  IN PEG_PORT         *PegPort,
  IN UINT8            OriginalLinkWidth
  )
{
  EFI_STATUS Status;
  UINT8      CurrentLinkWidth;

  Status = EFI_SUCCESS;

  CurrentLinkWidth = GetNegotiatedWidth (PegPort);
  if (CurrentLinkWidth < OriginalLinkWidth) {
    DEBUG ((EFI_D_INFO, "DOWNGRADE from x%d to x%d detected\n", OriginalLinkWidth, CurrentLinkWidth));
    MmPci32AndThenOr (
      0,
      PegPort->Bus,
      PegPort->Device,
      PegPort->Function,
      R_SA_PEG_LTSSMC_OFFSET,
      B_SA_PEG_LTSSMC_WIDTH_MASK,
      OriginalLinkWidth
    );
    MmPci16Or (0, PegPort->Bus, PegPort->Device, PegPort->Function, R_SA_PEG_LCTL_OFFSET, 0x10);
    StallPpi->Stall (PeiServices, StallPpi, STALL_ONE_MICRO_SECOND);
    MmPci16And (0, PegPort->Bus, PegPort->Device, PegPort->Function, R_SA_PEG_LCTL_OFFSET, 0xFFEF);
    Status = WaitForL0 (PeiServices, StallPpi, PegPort, FALSE);
    MmPci32Or (
      0,
      PegPort->Bus,
      PegPort->Device,
      PegPort->Function,
      R_SA_PEG_LTSSMC_OFFSET,
      0x1F
    );
    if (EFI_ERROR (Status)) {
      return Status;
    }

    CurrentLinkWidth = GetNegotiatedWidth (PegPort);
    if (CurrentLinkWidth == OriginalLinkWidth) {
      DEBUG ((EFI_D_INFO, "Width Recovery Successful\n"));
      Status = EFI_SUCCESS;
    } else {
      DEBUG ((EFI_D_INFO, "Width Recovery FAILED!\n"));
      Status = EFI_DEVICE_ERROR;
    }
  }

  return Status;
}

UINT8
GetLinkSpeed (
  IN PEG_PORT *PegPort
  )
{
  UINT16 Lsts;

  Lsts = MmPci16 (0, PegPort->Bus, PegPort->Device, PegPort->Function, R_SA_PEG_LSTS_OFFSET);

  return (UINT8)(Lsts & 0xF);
}

EFI_STATUS
RecoverLinkSpeed (
  IN EFI_PEI_SERVICES **PeiServices,
  IN PEI_STALL_PPI    *StallPpi,
  IN PEG_PORT         *PegPort,
  IN UINT8            OriginalLinkSpeed
  )
{
  EFI_STATUS Status;
  UINT8      CurrentLinkSpeed;

  Status = EFI_SUCCESS;

  CurrentLinkSpeed = GetLinkSpeed (PegPort);
  if (CurrentLinkSpeed < OriginalLinkSpeed) {
    DEBUG ((EFI_D_INFO, "DOWNGRADE from Gen %d to Gen %d detected\n", OriginalLinkSpeed, CurrentLinkSpeed));

    Status = RetrainLink (PeiServices, StallPpi, PegPort);
    if (EFI_ERROR (Status)) {
      DEBUG ((EFI_D_INFO, "Link Speed Recovery FAILED!\n"));
      return Status;
    }

    CurrentLinkSpeed = GetLinkSpeed (PegPort);
    if (CurrentLinkSpeed < OriginalLinkSpeed) {
      DEBUG ((EFI_D_INFO, "Link Speed Recovery FAILED!\n"));
      Status = EFI_DEVICE_ERROR;
    } else {
      DEBUG ((EFI_D_INFO, "Link Speed Recovery Successful\n"));
    }
  }

  return Status;
}

VOID
PcieTrainingWarmReset (
  IN EFI_PEI_SERVICES **PeiServices
  )
{
  EFI_STATUS      Status;
  PCH_RESET_PPI   *PchResetPpi;

  Status = (*PeiServices)->LocatePpi (
                              PeiServices,
                              &gPchResetPpiGuid,
                              0,
                              NULL,
                              &PchResetPpi
                              );
  ASSERT_EFI_ERROR (Status);
  PchResetPpi->Reset (PchResetPpi, WarmReset);

  return;
}

#endif // PEG_FLAG