summaryrefslogtreecommitdiff
path: root/ReferenceCode/Chipset/SystemAgent/SaInit/Pei/PcieTrainingPhase3.c
blob: 6123968be960df0d56507f08a9ab2d4db90d9c9d (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
/** @file
  This driver trains the PEG interface.

@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"

#ifdef PEG_FLAG

#include EFI_PPI_DEFINITION (PchMeUma)
#include "PchMeUma.h"

typedef struct _MARGIN_DATA {
  UINT8 Preset;
  INT32 TimingMargin[SA_PEG_MAX_LANE];
} MARGIN_DATA;

UINT8
SelectBestPresetForLane (
  IN  PORT_INFO               *PortInfo,
  IN  UINT8                   Lane,
  IN  SA_PLATFORM_POLICY_PPI  *SaPlatformPolicyPpi,
  IN  SA_DATA_HOB             *SaDataHob,
  IN  UINT8                   TrainingPreset
  )
{
  UINT8   BestPreset;

  ///
  /// Init to EV default
  ///
  BestPreset = 7;

  ///
  /// EnableMargin    FoundUsablePreset    SaDataHob
  ///            Y                    Y        !NULL  -> Use training results; update SaDataHob
  ///            Y                    Y         NULL  -> Use training results
  ///            Y                    N        !NULL  -> Use Policy value; update SaDataHob
  ///            X                    N         NULL  -> Use Policy value
  ///            N                    X        !NULL  -> Restore from SaDataHob
  ///
  if (Lane < SA_PEG_MAX_LANE) {
    DEBUG ((EFI_D_INFO, "Preset for Lane %2d: ", Lane));
    if ((PortInfo->EnableMargin) && (PortInfo->FoundUsablePreset)) {
      ///
      /// Use the best preset found during training
      ///
      BestPreset = TrainingPreset;
      DEBUG ((EFI_D_INFO, "Search Result: P%d", BestPreset));
      if (SaDataHob != NULL) {
        DEBUG ((EFI_D_INFO, ". Saving value for next boot."));
        SaDataHob->PegData.BestPreset[Lane] = BestPreset;
      }
    } else if ((PortInfo->EnableMargin) && (!PortInfo->FoundUsablePreset) && (SaDataHob != NULL)) {
      ///
      /// Use the policy value; update SaDataHob
      ///
      BestPreset = (UINT8) SaPlatformPolicyPpi->PcieConfig->Gen3EndPointPreset[Lane];
      DEBUG ((EFI_D_INFO, "Applying Policy value: P%d. Saving value for next boot.", BestPreset));
      SaDataHob->PegData.BestPreset[Lane] = BestPreset;
    } else if ((!PortInfo->FoundUsablePreset) && (SaDataHob == NULL)) {
      ///
      /// Use the policy value
      ///
      BestPreset = (UINT8) SaPlatformPolicyPpi->PcieConfig->Gen3EndPointPreset[Lane];
      DEBUG ((EFI_D_INFO, "Applying Policy value: P%d", BestPreset));
    } else if ((!PortInfo->EnableMargin) && (SaDataHob != NULL)) {
      ///
      /// Use the preset found on a previous boot
      ///
      BestPreset = (UINT8) SaDataHob->PegData.BestPreset[Lane];
      DEBUG ((EFI_D_INFO, "Restoring previous value: P%d", BestPreset));
    }
    DEBUG ((EFI_D_INFO, "\n"));
  } else {
    ///
    /// Error: Non-existent lane
    ///
    DEBUG ((EFI_D_ERROR, "Illegal Lane: %d", Lane));
  }

  return BestPreset;
}

VOID
PegGen3PresetSearch (
  IN EFI_PEI_SERVICES       **PeiServices,
  IN SA_PLATFORM_POLICY_PPI *SaPlatformPolicyPpi,
  IN PEI_STALL_PPI          *StallPpi,
  SA_DATA_HOB               *SaDataHob
  )
{
  UINT8                 Index;
  INT32                 MarginScore;
  UINT8                 PegBus;
  UINT8                 PegDev;
  UINT8                 PegFunc;
  UINT16                LinkStatus;
  BOOLEAN               LoadedSavedPreset;
  BOOLEAN               EndpointDeviceChanged;
  UINT8                 TempIndex;
  BOOLEAN               SkipBundle0;
  CPU_FAMILY            CpuFamilyId;
  CPU_STEPPING          CpuSteppingId;
  MARGIN_DATA           MarginData[MAX_PRESETS];
  PORT_INFO             PortInfoList[SA_PEG_MAX_FUN];
  INT32                 LaneScores[SA_PEG_MAX_LANE];
  INT32                 BestScores[SA_PEG_MAX_LANE];
  UINT8                 BestPresets[SA_PEG_MAX_LANE];
  EFI_STATUS            Status;
  UINT32                MonitorPort;
  UINT8                 PortInfoListLength;
  UINT8                 PortIndex;
  UINT8                 Lane;
  BOOLEAN               AnyGen3CapableLinks;
  BOOLEAN               AnyMarginingNeeded;
  UINT8                 PreCursor;
  UINT8                 Cursor;
  UINT8                 PostCursor;
  UINT8                 FullSwing;
  UINT32                NominalRecoveryCount;
  UINT8                 FirstSkippedLane;
  UINT8                 LastSkippedLane;
  UINT32                Data32;
  BOOLEAN               SlotResetNeeded;
  PCH_ME_UMA_PPI        *PchMeUmaPpi;
  UINT8                 DetectedReplacedCpu;

  AnyMarginingNeeded = FALSE;
  CpuFamilyId        = GetCpuFamily();
  CpuSteppingId      = GetCpuStepping();

  ///
  /// Setup default presets to search
  ///
  MarginData[0].Preset = 7;
  MarginData[1].Preset = 3;
  MarginData[2].Preset = 5;

  ///
  /// Initialize Arrays
  ///
  for (Lane = 0; Lane < SA_PEG_MAX_LANE; Lane++) {
    LaneScores[Lane]  = -1;
    BestScores[Lane]  = -1;
    BestPresets[Lane] = MarginData[0].Preset;
  }

  DEBUG ((EFI_D_INFO, "PEG Gen3 Preset Search\n"));

  if (!SaPolicyEnablesGen3 (SaPlatformPolicyPpi)) {
    DEBUG ((EFI_D_INFO, " Gen3 is disabled by policy\n"));
    return;
  }
  if ((CpuFamilyId == EnumCpuHsw) && (CpuSteppingId < EnumHswB0)) {
    DEBUG ((EFI_D_WARN, " Gen3 preset search is not supported on this stepping\n"));
    return;
  }
  Data32 = (McD1PciCfg32 (R_SA_PEG_PEGTST_OFFSET) & BIT20) >> 20;
  if (Data32 != 0) {
    DEBUG ((EFI_D_WARN, " Gen3 preset search does not support lane reversal\n"));
    return;
  }

  ///
  /// If ME is supported and the CPU has been replaced, redo the Preset Search.
  /// Note that calling CpuReplacementCheck() can induce an ME-required warm reset.
  ///
  DetectedReplacedCpu = 0;
  Status              = EFI_SUCCESS;

  if ((SaDataHob != NULL) && (SaDataHob->PegDataValid)) {
    Status = (*PeiServices)->LocatePpi (PeiServices, &gPchMeUmaPpiGuid, 0, NULL, &PchMeUmaPpi);
    ASSERT_EFI_ERROR (Status);
    DEBUG ((EFI_D_INFO, "Calling CpuReplacementCheck\n"));
    Status = PchMeUmaPpi->CpuReplacementCheck (PeiServices, NULL, &DetectedReplacedCpu);
    ASSERT_EFI_ERROR (Status);
    DEBUG ((EFI_D_INFO, " ME reported CPU Replacement value: %x\n", DetectedReplacedCpu));
  }

  ///
  /// Get the furcation setup and port information
  ///
  GetPortInfo (&(PortInfoList[0]), &PortInfoListLength, &SkipBundle0);

  ///
  /// Make sure we at Gen3 before starting, if not attempt reset and see if that helps
  ///
  SlotResetNeeded = FALSE;
  for (PortIndex = 0; PortIndex < PortInfoListLength; PortIndex++) {
    PegBus  = (PortInfoList[PortIndex]).PegPort.Bus;
    PegDev  = (PortInfoList[PortIndex]).PegPort.Device;
    PegFunc = (PortInfoList[PortIndex]).PegPort.Function;
    LinkStatus = MmPci16 (0, PegBus, PegDev, PegFunc, R_SA_PEG_LSTS_OFFSET);
    if (((LinkStatus & 0x0F) != 3) && ((PortInfoList[PortIndex]).LinkIsGen3Capable)) {
      SlotResetNeeded = TRUE;
      break;
    }
  }
  if (SlotResetNeeded) {
    ///
    /// Bypass phase2 and assert slot reset
    ///
    McD1PciCfg32Or (R_SA_PEG_EQCFG_OFFSET, BIT15);
    Status = TogglePegSlotReset (PeiServices, StallPpi, SaPlatformPolicyPpi);
    if (!EFI_ERROR (Status)) {
      for (PortIndex = 0; PortIndex < PortInfoListLength; PortIndex++) {
        PegBus  = (PortInfoList[PortIndex]).PegPort.Bus;
        PegDev  = (PortInfoList[PortIndex]).PegPort.Device;
        PegFunc = (PortInfoList[PortIndex]).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);
      }
    }
  }

  ///
  /// Determine which PEG ports require testing
  ///
  AnyGen3CapableLinks = FALSE;
  for (PortIndex = 0; PortIndex < PortInfoListLength; PortIndex++) {
    PegBus  = (PortInfoList[PortIndex]).PegPort.Bus;
    PegDev  = (PortInfoList[PortIndex]).PegPort.Device;
    PegFunc = (PortInfoList[PortIndex]).PegPort.Function;

    ///
    /// If we already have Best Preset value from previous boot, use it and skip PresetSearch when end point device no change
    ///
    LoadedSavedPreset     = FALSE;
    EndpointDeviceChanged = TRUE;
    if (SaDataHob != NULL) {
      if (SaDataHob->PegDataValid) {
        LoadedSavedPreset = TRUE;
        if (SaDataHob->PegData.EndPointVendorIdDeviceId[PegFunc] == (PortInfoList[PortIndex]).EndPointVendorIdDeviceId) {
          EndpointDeviceChanged = FALSE;
        }
      }
      if (EndpointDeviceChanged) {
        ///
        /// Save new device ID vendor ID
        ///
        SaDataHob->PegData.EndPointVendorIdDeviceId[PegFunc] = (PortInfoList[PortIndex]).EndPointVendorIdDeviceId;
      }
    }
    DEBUG ((EFI_D_INFO, " PEG%x%x (%x:%x:%x) - LoadedSavedPreset = %d. EndpointDeviceChanged = %d.\n",
      PegDev, PegFunc, PegBus, PegDev, PegFunc, LoadedSavedPreset, EndpointDeviceChanged));

    ReportPcieLinkStatus (PegBus, PegDev, PegFunc);

    if ( (!LoadedSavedPreset)         ||
         (EndpointDeviceChanged)      ||
         (DetectedReplacedCpu   != 0) ||
         ((SaPlatformPolicyPpi->Revision >= SA_PLATFORM_POLICY_PPI_REVISION_2) &&
          (SaPlatformPolicyPpi->PcieConfig->PegGen3ForcePresetSearch == 1    ))) {
      (PortInfoList[PortIndex]).EnableMargin = TRUE;
    } else {
      (PortInfoList[PortIndex]).EnableMargin = FALSE;
    }

    if (!(PortInfoList[PortIndex]).LinkIsGen3Capable) {
      (PortInfoList[PortIndex]).EnableMargin = FALSE;
    } else {
      AnyGen3CapableLinks = TRUE;
    }
  } ///< PegFunc Loop end

  if (!AnyGen3CapableLinks) {
    DEBUG ((EFI_D_INFO, "Skipping Preset Search - No Gen3 capable links\n"));
    return;
  }

  ///
  /// Determine if any ports need to be trained.
  /// If any ports are trained, the corresponding endpoint should also be reset with PERST#.
  ///
  for (PortIndex = 0; PortIndex < PortInfoListLength; PortIndex++) {
    if ((PortInfoList[PortIndex]).EnableMargin == TRUE) {
      AnyMarginingNeeded = TRUE;
      break;
    }
  }

  if (AnyMarginingNeeded) {
    MonitorPort = OpenMonitor (PeiServices, SaPlatformPolicyPpi, StallPpi);

    McD1PciCfg32Or (R_SA_PEG_REUT_PH_CTR_OFFSET, B_SA_PEG_REUT_PH_CTR_AUTOCOMP_MASK);

    ///
    /// Presets Loop start
    ///
    for (Index = 0; Index < MAX_PRESETS; Index++) {
      ///
      /// Clear out old values
      ///
      for (TempIndex = 0; TempIndex < SA_PEG_MAX_LANE; TempIndex++) {
        MarginData[Index].TimingMargin[TempIndex]      = 0;
      } ///< End of for each Lane


      for (PortIndex = 0; PortIndex < PortInfoListLength; PortIndex++) {
        (PortInfoList[PortIndex]).SkipMargin = FALSE;
        PegBus  = (PortInfoList[PortIndex]).PegPort.Bus;
        PegDev  = (PortInfoList[PortIndex]).PegPort.Device;
        PegFunc = (PortInfoList[PortIndex]).PegPort.Function;

        if (!(PortInfoList[PortIndex]).LinkIsGen3Capable) {
          DEBUG ((EFI_D_INFO, "Skipping PEG%d%d - Not Gen3 capable\n", PegDev, PegFunc));
          continue;
        }

        ///
        /// Find first lane of the port for coefficient programming
        ///
        switch (PegFunc) {
          default:
          case 0:
            Lane             = 0;
            FirstSkippedLane = 0;
            LastSkippedLane  = 1;
            break;
          case 1:
            Lane             = 8;
            FirstSkippedLane = 8;
            LastSkippedLane  = 9;
            break;
          case 2:
            Lane             = 12;
            FirstSkippedLane = 12;
            LastSkippedLane  = 13;
            break;
        }

        ///
        /// Get FullSwing
        ///
        GetLinkPartnerFullSwing (Lane, &FullSwing);

        ///
        /// Get Coefficients
        ///
        GetCoefficientsFromPreset (MarginData[Index].Preset, FullSwing, &PreCursor, &Cursor, &PostCursor);

        ///
        /// Set Lane's Coefficients
        ///
        if (SkipBundle0) {
          for (TempIndex = FirstSkippedLane; TempIndex <= LastSkippedLane; TempIndex++) {
            SetPartnerTxCoefficients (TempIndex, &PreCursor, &Cursor, &PostCursor);
            ProgramPreset (1, MarginData[Index].Preset, PegFunc, TempIndex);
          }
        }
        for (TempIndex = 0; TempIndex < (PortInfoList[PortIndex]).LaneListLength; TempIndex++) {
          SetPartnerTxCoefficients ((PortInfoList[PortIndex]).LaneList[TempIndex], &PreCursor, &Cursor, &PostCursor);
          ProgramPreset (1, MarginData[Index].Preset, PegFunc, (PortInfoList[PortIndex]).LaneList[TempIndex]);
        }

        ///
        /// Set DOEQ bit
        ///
        MmPci32Or (0, PegBus, PegDev, PegFunc, R_SA_PEG_LCTL3_OFFSET, BIT0);
        RetrainLink (PeiServices, StallPpi, &((PortInfoList[PortIndex]).PegPort));

        ///
        /// Get the current link status
        ///
        LinkStatus = MmPci16 (0, PegBus, PegDev, PegFunc, R_SA_PEG_LSTS_OFFSET);
        ///
        /// No need to margin if couldn't get to Gen3 with this preset
        ///
        if ((LinkStatus & 0x0F) != 3) {
          RecoverLinkFailure (PeiServices, SaPlatformPolicyPpi, SaDataHob, StallPpi,
                              &((PortInfoList[PortIndex]).PegPort), 3,
                              GetNegotiatedWidth (&((PortInfoList[PortIndex]).PegPort)));
          LinkStatus = MmPci16 (0, PegBus, PegDev, PegFunc, R_SA_PEG_LSTS_OFFSET);
          if ((LinkStatus & 0x0F) != 3) {
            (PortInfoList[PortIndex]).SkipMargin = TRUE;
            continue;
          }
        }

        NominalRecoveryCount = SaPcieGetErrorCount (MonitorPort, PegFunc);
        StallPpi->Stall (PeiServices, StallPpi, SaPlatformPolicyPpi->PcieConfig->PegGen3PresetSearchDwellTime * STALL_ONE_MICRO_SECOND);
        NominalRecoveryCount = SaPcieGetErrorCount (MonitorPort, PegFunc) - NominalRecoveryCount;
        if (NominalRecoveryCount > 0) {
          (PortInfoList[PortIndex]).SkipMargin = TRUE;
          continue;
        }
        (PortInfoList[PortIndex]).FoundUsablePreset = TRUE;
      } ///< End of for each Port

      Status = RunMarginTest (
                 PeiServices,
                 SaPlatformPolicyPpi,
                 SaDataHob,
                 StallPpi,
                 MonitorPort,
                 &(PortInfoList[0]),
                 PortInfoListLength,
                 LaneLevelRxJitter,
                 MarginData[Index].TimingMargin
                 );

      if (EFI_ERROR (Status)) {
        for (TempIndex = 0; TempIndex < SA_PEG_MAX_LANE; TempIndex++) {
          MarginData[Index].TimingMargin[TempIndex] = 0;
        }
      }
    } ///< Presets Loop end

    McD1PciCfg32And (R_SA_PEG_REUT_PH_CTR_OFFSET, ~B_SA_PEG_REUT_PH_CTR_AUTOCOMP_MASK);
    CloseMonitor (SaPlatformPolicyPpi, MonitorPort);

    ///
    /// Find the preset with the maximum margin (largest of all margin values)
    ///
    for (Index = 0; Index < sizeof (MarginData) / sizeof (MarginData[0]); Index++) {
      for (Lane = 0; Lane < SA_PEG_MAX_LANE; Lane++) {
        LaneScores[Lane] = -1;
      }
      for (Lane = 0; Lane < SA_PEG_MAX_LANE; Lane++) {
        if (MarginData[Index].TimingMargin[Lane] != -1) {
          MarginScore = MarginData[Index].TimingMargin[Lane];
          LaneScores[Lane] = MarginScore;
        }
        if (LaneScores[Lane] > BestScores[Lane]) {
          BestScores[Lane]  = LaneScores[Lane];
          BestPresets[Lane] = MarginData[Index].Preset;
        }
      } ///< End of for each Lane
    } ///< End of for each Preset

    ///
    /// If Lanes 0-1 were skipped, copy the values from Lane 2
    ///
    if (SkipBundle0) {
      for (PortIndex = 0; PortIndex < PortInfoListLength; PortIndex++) {
        PegFunc = (PortInfoList[PortIndex]).PegPort.Function;
        switch (PegFunc) {
          default:
          case 0:
            Lane             = 2;
            FirstSkippedLane = 0;
            LastSkippedLane  = 1;
            break;
          case 1:
            Lane             = 10;
            FirstSkippedLane = 8;
            LastSkippedLane  = 9;
            break;
          case 2:
            Lane             = 14;
            FirstSkippedLane = 12;
            LastSkippedLane  = 13;
            break;
        }
        if (BestScores[Lane] != -1) {
          DEBUG ((EFI_D_INFO, "Using Lane %2d's Best Preset for Lanes %2d-%2d.\n", Lane, FirstSkippedLane, LastSkippedLane));
          for (TempIndex = FirstSkippedLane; TempIndex <= LastSkippedLane; TempIndex++) {
            BestScores[TempIndex]  = BestScores[Lane];
            BestPresets[TempIndex] = BestPresets[Lane];
          }
        }
      } ///< End of for each port
    } ///< End of SkipBundle0
  } ///< End of AnyMarginingNeeded

  for (PortIndex = 0; PortIndex < PortInfoListLength; PortIndex++) {
    PegBus  = (PortInfoList[PortIndex]).PegPort.Bus;
    PegDev  = (PortInfoList[PortIndex]).PegPort.Device;
    PegFunc = (PortInfoList[PortIndex]).PegPort.Function;
    if (!(PortInfoList[PortIndex]).LinkIsGen3Capable) {
      continue;
    }
    if (SkipBundle0) {
      switch (PegFunc) {
        default:
        case 0:
          FirstSkippedLane = 0;
          LastSkippedLane  = 1;
          break;
        case 1:
          FirstSkippedLane = 8;
          LastSkippedLane  = 9;
          break;
        case 2:
          FirstSkippedLane = 12;
          LastSkippedLane  = 13;
          break;
      }
      for (TempIndex = FirstSkippedLane; TempIndex <= LastSkippedLane; TempIndex++) {
        BestPresets[TempIndex] = SelectBestPresetForLane (
                                   &(PortInfoList[PortIndex]),
                                   TempIndex,
                                   SaPlatformPolicyPpi,
                                   SaDataHob,
                                   BestPresets[TempIndex]
                                   );
      }
    }
    for (TempIndex = 0; TempIndex < (PortInfoList[PortIndex]).LaneListLength; TempIndex++) {
      BestPresets[(PortInfoList[PortIndex]).LaneList[TempIndex]] = SelectBestPresetForLane (
                                                                     &(PortInfoList[PortIndex]),
                                                                     (PortInfoList[PortIndex]).LaneList[TempIndex],
                                                                     SaPlatformPolicyPpi,
                                                                     SaDataHob,
                                                                     BestPresets[(PortInfoList[PortIndex]).LaneList[TempIndex]]
                                                                     );
    }
  }

  ///
  /// Program the presets.  If any link was margined, also reset the
  /// endpoints in order to return the endpoints to a known-good state.
  ///
  if (AnyMarginingNeeded) {
    for (TempIndex = 0; TempIndex < SA_PEG_MAX_FUN; TempIndex++) {
      MmPci16Or (0, SA_PEG_BUS_NUM, SA_PEG_DEV_NUM, TempIndex, R_SA_PEG_LCTL_OFFSET, BIT4);
    }
    AssertPegSlotReset (SaPlatformPolicyPpi);
  }
  for (PortIndex = 0; PortIndex < PortInfoListLength; PortIndex++) {
    PegBus  = (PortInfoList[PortIndex]).PegPort.Bus;
    PegDev  = (PortInfoList[PortIndex]).PegPort.Device;
    PegFunc = (PortInfoList[PortIndex]).PegPort.Function;
    if (!(PortInfoList[PortIndex]).LinkIsGen3Capable) {
      DEBUG ((EFI_D_INFO, "PEG%d%d - Not Gen3 capable, skip coefficient programming.\n", PegDev, PegFunc));
      continue;
    }
    ///
    /// Find first lane of the port for coefficient programming
    ///
    switch (PegFunc) {
      default:
      case 0:
        Lane             = 0;
        FirstSkippedLane = 0;
        LastSkippedLane  = 1;
        break;
      case 1:
        Lane             = 8;
        FirstSkippedLane = 8;
        LastSkippedLane  = 9;
        break;
      case 2:
        Lane             = 12;
        FirstSkippedLane = 12;
        LastSkippedLane  = 13;
        break;
    }
    ///
    /// Get FullSwing
    ///
    GetLinkPartnerFullSwing (Lane, &FullSwing);

    if (SkipBundle0) {
      for (TempIndex = FirstSkippedLane; TempIndex <= LastSkippedLane; TempIndex++) {
        GetCoefficientsFromPreset (BestPresets[TempIndex], FullSwing, &PreCursor, &Cursor, &PostCursor);
        SetPartnerTxCoefficients (TempIndex, &PreCursor, &Cursor, &PostCursor);
        ProgramPreset (1, BestPresets[TempIndex], PegFunc, TempIndex);
      }
    }
    for (TempIndex = 0; TempIndex < (PortInfoList[PortIndex]).LaneListLength; TempIndex++) {
      ///
      /// Get Coefficients
      ///
      GetCoefficientsFromPreset (BestPresets[((PortInfoList[PortIndex]).LaneList[TempIndex])], FullSwing, &PreCursor, &Cursor, &PostCursor);

      ///
      /// Set Lane's Coefficients
      ///
      SetPartnerTxCoefficients ((PortInfoList[PortIndex]).LaneList[TempIndex], &PreCursor, &Cursor, &PostCursor);

      ///
      /// Set Phase 1 Presets
      ///
      ProgramPreset (1, BestPresets[((PortInfoList[PortIndex]).LaneList[TempIndex])], PegFunc, (PortInfoList[PortIndex]).LaneList[TempIndex]);
    }

    MmPci32Or (0, PegBus, PegDev, PegFunc, R_SA_PEG_LCTL3_OFFSET, BIT0);   ///< DOEQ
    MmPci16Or (0, PegBus, PegDev, PegFunc, R_SA_PEG_LCTL_OFFSET,  BIT5);   ///< Retrain link
  }

  if (AnyMarginingNeeded) {
    StallPpi->Stall (PeiServices, StallPpi, 100 * STALL_ONE_MICRO_SECOND);
    for (TempIndex = 0; TempIndex < SA_PEG_MAX_FUN; TempIndex++) {
      MmPci16And (0, SA_PEG_BUS_NUM, SA_PEG_DEV_NUM, TempIndex, R_SA_PEG_LCTL_OFFSET, (UINT16) ~(BIT4));
    }
    DeassertPegSlotReset (SaPlatformPolicyPpi);
  }

  for (PortIndex = 0; PortIndex < PortInfoListLength; PortIndex++) {
    PegBus  = (PortInfoList[PortIndex]).PegPort.Bus;
    PegDev  = (PortInfoList[PortIndex]).PegPort.Device;
    PegFunc = (PortInfoList[PortIndex]).PegPort.Function;
    WaitForL0 (PeiServices, StallPpi, &(PortInfoList[PortIndex].PegPort), FALSE);
    ReportPcieLinkStatus (PegBus, PegDev, PegFunc);
  }

  DEBUG ((EFI_D_INFO, "PEG Gen3 Preset Search done\n\n"));

  return;
}

BOOLEAN
SaPolicyEnablesGen3 (
  IN SA_PLATFORM_POLICY_PPI *SaPlatformPolicyPpi
  )
{
  UINTN   PegPortGenx;
  BOOLEAN Gen3Enabled;
  UINT8   Index;

  Gen3Enabled = FALSE;

  ///
  /// Check if Gen3 is enabled on PEG10/11/12
  ///
  for (Index = 0; Index < SA_PEG_MAX_FUN; Index++) {
    ///
    /// PegPortGenx: 0 = Auto, 1 = Gen1, 2 = Gen2, 3 = Gen3
    ///
    PegPortGenx = SaPlatformPolicyPpi->PcieConfig->PegGenx[Index];

    ///
    /// Check if the root port is present and the speed is not limited to Gen1/Gen2
    ///
    if ((PegPortGenx == PEG_AUTO) || (PegPortGenx == PEG_GEN3)) {
      Gen3Enabled = TRUE;
      break;
    }
  }
  return Gen3Enabled;
}

#endif // PEG_FLAG