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

@copyright
  Copyright (c) 2012 - 2013 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

UINT16
GetErrorTarget (
  IN  SA_PLATFORM_POLICY_PPI *SaPlatformPolicyPpi
  )
{
  UINT16  ErrorTarget;

  if ((SaPlatformPolicyPpi->Revision >= SA_PLATFORM_POLICY_PPI_REVISION_3) &&
      (SaPlatformPolicyPpi->PcieConfig->PegGen3PresetSearchErrorTarget >= 1)) {
    ErrorTarget = SaPlatformPolicyPpi->PcieConfig->PegGen3PresetSearchErrorTarget;
  } else {
    ErrorTarget = 4;
  }

  return ErrorTarget;
}

VOID
GetPortInfo (
  OUT PORT_INFO *PortInfoList,
  OUT UINT8     *PortInfoListLength,
  OUT BOOLEAN   *SkipBundle0
  )
{
  UINT32            HwStrap;
  UINT8             PegBus;
  UINT8             PegDev;
  UINT8             PcieController;
  UINT8             Index;
  UINT8             Lane;
  UINT8             LaneIndex;
  UINT8             FurcationSetup[SA_PEG_MAX_FUN];
  UINT8             PcieControllerList[SA_PEG_MAX_FUN];
  UINT8             NumberToCheck;
  UINT8             StartLane;
  UINT8             Width;
  UINT32            Lcap;
  UINT32            CapOffset;
  CPU_FAMILY        CpuFamilyId;
  CPU_STEPPING      CpuSteppingId;

  PegBus        = SA_MC_BUS;
  PegDev        = 1;
  HwStrap       = (McD1PciCfg32(R_SA_PEG_FUSESCMN_OFFSET) >> 16) & 0x3;
  CpuFamilyId   = GetCpuFamily();
  CpuSteppingId = GetCpuStepping();

  switch(HwStrap) {
    case SA_PEG_x8_x4_x4:
      FurcationSetup[0] = 8;
      FurcationSetup[1] = 4;
      FurcationSetup[2] = 4;
      NumberToCheck     = 3;
      break;
    case SA_PEG_x8_x8_x0:
      FurcationSetup[0] = 8;
      FurcationSetup[1] = 8;
      NumberToCheck     = 2;
      break;
    default:
    case SA_PEG_x16_x0_x0:
      FurcationSetup[0] = 16;
      NumberToCheck     = 1;
      break;
  }

  ///
  /// Figure out which PcieControllers are enabled
  ///
  (*PortInfoListLength) = 0;
  for (PcieController = 0; PcieController < NumberToCheck; PcieController++) {
    ///
    /// Sanity check to make sure width > 0
    ///
    if (FurcationSetup[PcieController] == 0) {
      continue;
    }

    ///
    /// Check to make sure the Root Port Exists
    ///
    if (MmPci16 (0, PegBus, PegDev, PcieController, PCI_VID) == 0xFFFF) {
      continue;
    }

    ///
    /// Add the PcieController to the list of enabled controllers
    ///
    PcieControllerList[(*PortInfoListLength)] = PcieController;
    (*PortInfoListLength)++;
  }

  ///
  /// If needed, skip Bundle 0's preset search and use Bundle 1's preset instead.
  ///
  (*SkipBundle0) = FALSE;
  if (((CpuFamilyId == EnumCpuHsw) && (CpuSteppingId < EnumHswC0)) ||
      ((CpuFamilyId == EnumCpuCrw) && (CpuSteppingId < EnumCrwC0))) {
    (*SkipBundle0) = TRUE;
  }
  if ((*SkipBundle0)) {
    DEBUG ((EFI_D_INFO, "Skipping each controller's Lane 0-1 preset searches; using their Lane 2 preset instead.\n"));
  }

  StartLane = 0;
  for (Index = 0; Index < (*PortInfoListLength); Index++) {
    PcieController = PcieControllerList[Index];

    ///
    /// Get information for the current port
    ///
    (PortInfoList[Index]).EnableMargin                 = TRUE;
    (PortInfoList[Index]).FoundUsablePreset            = FALSE;
    (PortInfoList[Index]).PegPort.Bus                  = PegBus;
    (PortInfoList[Index]).PegPort.Device               = PegDev;
    (PortInfoList[Index]).PegPort.Function             = PcieController;
    (PortInfoList[Index]).PegPort.Index                = PcieController;
    (PortInfoList[Index]).PegPort.EndpointMaxLinkSpeed = 0;
    ReportPcieLinkStatus (PegBus, PegDev, PcieController);
    Width                                              = GetNegotiatedWidth(&((PortInfoList[Index]).PegPort));
    (PortInfoList[Index]).LaneListLength               = Width;
    for (Lane = 0, LaneIndex = 0; Lane < Width; Lane++) {
      if ((*SkipBundle0)) {
        if ((Lane == 0) || (Lane == 1)) {
          (PortInfoList[Index]).LaneListLength--;
          continue;
        }
      }
      if (LaneIndex < SA_PEG_MAX_LANE) {
        (PortInfoList[Index]).LaneList[LaneIndex] = (Lane + StartLane);
      }
      LaneIndex++;
    }

    ///
    /// Check that both root port and endpoint support Gen3
    ///
    Lcap = MmPci32 (0, PegBus, PegDev, PcieController, R_SA_PEG_LCAP_OFFSET);
    if ((Lcap & 0x0F) != 3) {
      DEBUG ((EFI_D_INFO, " PEG%x%x (%x:%x:%x) - Root Port is not Gen3-capable. Max Link Speed = %d.\n",
        PegDev, PcieController, PegBus, PegDev, PcieController, Lcap & 0x0F));
      (PortInfoList[Index]).LinkIsGen3Capable = FALSE;
    } else {

      DEBUG ((EFI_D_INFO, " PEG%x%x (%x:%x:%x) - Root Port is Gen3-capable.\n",
        PegDev, PcieController, PegBus, PegDev, PcieController));

      ///
      /// Set PEG PortBus = 1 to Read Endpoint.
      ///
      MmPci32AndThenOr (0, PegBus, PegDev, PcieController, PCI_PBUS, 0xFF0000FF, 0x00010100);

      ///
      /// A config write is required in order for the device to re-capture the Bus number,
      /// according to PCI Express Base Specification, 2.2.6.2
      /// Write to a read-only register VendorID to not cause any side effects.
      ///
      MmPci16 (0, 1, 0, 0, PCI_VID) = 0;

      ///
      /// Save end point vendor id and device id
      ///
      (PortInfoList[Index]).EndPointVendorIdDeviceId = MmPci32 (0, 1, 0, 0, 0);

      ///
      /// Negotiation Done?
      ///
      if ((MmPci16 (0, PegBus, PegDev, PcieController, R_SA_PEG_VC0RSTS_OFFSET) & BIT1) != 0) {
        (PortInfoList[Index]).LinkIsGen3Capable = FALSE;
        DEBUG ((EFI_D_INFO, " PEG%x%x (%x:%x:%x) - VC0 negotiation is pending! Skipping endpoint.\n",
          PegDev, PcieController, PegBus, PegDev, PcieController, Lcap & 0x0F));
        ReportPcieLinkStatus (PegBus, PegDev, PcieController);
      } else {
        ///
        /// Get the pointer to the Port PCI Express Capability Structure.
        ///
        CapOffset = PcieFindCapId (1, 0, 0, EFI_PCI_CAPABILITY_ID_PCIEXP);
        if (CapOffset == 0) {
          (PortInfoList[Index]).LinkIsGen3Capable = FALSE;
          DEBUG ((EFI_D_INFO, " PEG%x%x (%x:%x:%x) - Endpoint is not Gen3-capable. No PCIe Capability found.\n",
            PegDev, PcieController, PegBus, PegDev, PcieController, Lcap & 0x0F));
        } else {
          Lcap = MmPci32 (0, 1, 0, 0, CapOffset + 0x0C);
          (PortInfoList[Index]).PegPort.EndpointMaxLinkSpeed = Lcap & 0x0F;
          if ((Lcap & 0x0F) < 3) {
            (PortInfoList[Index]).LinkIsGen3Capable = FALSE;
            DEBUG ((EFI_D_INFO, " PEG%x%x (%x:%x:%x) - Endpoint is not Gen3-capable. Max Link Speed = %d.\n",
              PegDev, PcieController, PegBus, PegDev, PcieController, Lcap & 0x0F));
          } else {
            (PortInfoList[Index]).LinkIsGen3Capable = TRUE;
            DEBUG ((EFI_D_INFO, " PEG%x%x (%x:%x:%x) - Endpoint is Gen3-capable\n",
              PegDev, PcieController, PegBus, PegDev, PcieController, Lcap & 0x0F));
          }
        }
      }
      ///
      /// Restore bus numbers on the PEG bridge.
      ///
      MmPci32And (0, PegBus, PegDev, PcieController, PCI_PBUS, 0xFF0000FF);
    }

    StartLane += FurcationSetup[PcieController];
  } ///< End of for each port

  return;
}

EFI_STATUS
RunMarginTest (
  IN  EFI_PEI_SERVICES        **PeiServices,
  IN  SA_PLATFORM_POLICY_PPI  *SaPlatformPolicyPpi,
  IN  SA_DATA_HOB             *SaDataHob,
  IN  PEI_STALL_PPI           *StallPpi,
  IN  UINT32                  MonitorPort,
  IN  PORT_INFO               *PortInfoList,
  IN  UINT8                   PortInfoListLength,
  IN  MARGIN_TEST_TYPE        MarginTest,
  OUT INT32                   *Margins
  )
{
  EFI_STATUS Status;
  UINT8      *LaneList;
  UINT8      Lane;
  UINT8      PortListIndex;
  UINT8      OriginalSpeed;
  UINT8      OriginalWidth;
  UINT8      LaneListLength;
  PEG_PORT   *PegPort;

  Status = EFI_SUCCESS;

  ///
  /// Initialize Margins to -1.  Since -1 is an invalid value, we know that lane wasn't tested if its margin == -1
  ///
  for (Lane = 0; Lane < SA_PEG_MAX_LANE; Lane++) {
    Margins[Lane] = -1;
  }

  for (PortListIndex = 0; PortListIndex < PortInfoListLength; PortListIndex++) {
    ///
    /// Test all lanes associated with this the current port
    ///
    LaneList       = &((PortInfoList[PortListIndex]).LaneList[0]);
    LaneListLength =   (PortInfoList[PortListIndex]).LaneListLength;
    PegPort        = &((PortInfoList[PortListIndex]).PegPort);
    if ((PortInfoList[PortListIndex]).EnableMargin == FALSE ||
        (PortInfoList[PortListIndex]).SkipMargin   == TRUE) {
      continue;
    }

    if ((PortInfoList[PortListIndex]).LinkIsGen3Capable) {
      OriginalSpeed = 3;
    } else {
      OriginalSpeed = GetLinkSpeed (PegPort);
    }
    OriginalWidth = GetNegotiatedWidth (PegPort);

    switch (MarginTest) {
      case LaneLevelRxJitter:
        Status = LaneLevelJitterTest (
                 PeiServices,
                 SaPlatformPolicyPpi,
                 SaDataHob,
                 StallPpi,
                 MonitorPort,
                 LaneList,
                 LaneListLength,
                 PegPort,
                 OriginalSpeed,
                 OriginalWidth,
                 FALSE,
                 Margins
                 );
        break;
      default:
        DEBUG ((EFI_D_WARN, "Invalid Margin Test Requested.\n"));
        break;
    }
  }

  return Status;
}

EFI_STATUS
LaneLevelJitterTest (
  IN  EFI_PEI_SERVICES       **PeiServices,
  IN  SA_PLATFORM_POLICY_PPI *SaPlatformPolicyPpi,
  IN  SA_DATA_HOB            *SaDataHob,
  IN  PEI_STALL_PPI          *StallPpi,
  IN  UINT32                 MonitorPort,
  IN  UINT8                  *LaneList,
  IN  UINT8                  LaneListLength,
  IN  PEG_PORT               *PegPort,
  IN  UINT8                  OriginalLinkSpeed,
  IN  UINT8                  OriginalLinkWidth,
  IN  BOOLEAN                TxJitterTest,
  OUT INT32                  *Margins
  )
{
  EFI_STATUS Status;
  UINT8     LaneListIndex;
  INT8      Jitter;
  UINT32    Errors;
  UINT32    PreviousErrors;
  UINT8     Lane;
  UINT32    RecoveryCount;
  BOOLEAN   AbortMargin;
  UINT16    ErrorTarget;
  UINT8     ConvergenceCounter;
  INT32     LastMargin;
  INT32     MarginDifference;
  INT32     StartJitter;
  INT8      MarginDirection;
  UINT8     RepeatCount;

  ErrorTarget = GetErrorTarget (SaPlatformPolicyPpi);

  for (LaneListIndex = 0; LaneListIndex < LaneListLength; LaneListIndex++) {
    Lane            = LaneList[LaneListIndex];
    Errors          = 0;
    AbortMargin     = FALSE;
    Margins[Lane]   = 0;
    MarginDirection = 1;

    if (TxJitterTest) {
      ConfigureTxJitterMux (Lane, SaPlatformPolicyPpi->PlatformData->MchBar);
      EnableTxJitterInjection (Lane, TRUE);
    }

    ///
    /// Determine value to start at
    ///
    if (LaneListIndex == 0) {
      StartJitter = 0;
    } else {
      StartJitter = (Margins[LaneList[LaneListIndex - 1]] / 100) - JITTER_MARGIN_INITIAL_OFFSET;
      if (StartJitter < 0) {
        StartJitter = 0;
      }
    }

    ConvergenceCounter = 0;
    LastMargin         = -1;
    RepeatCount        = 0;
    while (ConvergenceCounter < MARGIN_CONVERGANCE_MIN_MATCHES && RepeatCount < MARGIN_CONVERGANCE_MAX_REPEATS) {
      RepeatCount++;
      ///
      /// Determine whether to go up or down from starting point
      ///
      AbortMargin   = FALSE;
      RecoveryCount = SaPcieGetErrorCount (MonitorPort, PegPort->Function);
      Status        = SetJitterTolerance (&Lane, 1, (UINT8) StartJitter);
      ASSERT_EFI_ERROR (Status);

      Errors = SaPciePointTest (PeiServices, SaPlatformPolicyPpi, StallPpi, MonitorPort, PegPort, RecoveryCount);
      if (Errors >= ErrorTarget) {
        if (StartJitter == 0) {
          Margins[Lane] = 0;
          AbortMargin   = TRUE;
        } else {
          MarginDirection = -1;
        }
      } else {
        MarginDirection = 1;
      }
      for (Jitter =  (INT8) (StartJitter + MarginDirection);
           Jitter <  JITTER_LENGTH &&
           Jitter >= 0             &&
           (!AbortMargin);
           Jitter = (INT8) (Jitter + MarginDirection)) {
        ///
        /// Check for a link downgrade
        ///
        AbortMargin = LinkIsDowngraded (PegPort, OriginalLinkSpeed, OriginalLinkWidth);
        if (AbortMargin) {
          if (MarginDirection < 0) {
            LastMargin         = -1;
            ConvergenceCounter = 0;
            StartJitter        = 0;
            MarginDirection    = 1;
          } else {
            if (Jitter == 0) {
              Margins[Lane] = 0;
            } else {
              Margins[Lane] = (Jitter - 1) * 100;
            }
          }
          break;
        }

        ///
        /// Get initial recovery count
        ///
        RecoveryCount = SaPcieGetErrorCount (MonitorPort, PegPort->Function);
        Status        = SetJitterTolerance (&Lane, 1, Jitter);
        ASSERT_EFI_ERROR (Status);

        PreviousErrors = Errors;
        Errors         = SaPciePointTest (PeiServices, SaPlatformPolicyPpi, StallPpi, MonitorPort, PegPort, RecoveryCount);
        if (MarginDirection < 0) {
          if (Errors < ErrorTarget) { ///< Downward direction has started passing
            Margins[Lane] = InterpolateMargin (ErrorTarget, PreviousErrors, Errors, (INT32) Jitter);
            break;
          }
        } else {
          if (Errors >= ErrorTarget) { ///< Upward direction has started failing
            Margins[Lane] = InterpolateMargin (ErrorTarget, Errors, PreviousErrors, (INT32) Jitter);
            break;
          }
        }
      } ///< End of for loop

      ///
      /// Check if we never reached the error target
      ///
      if (MarginDirection < 0) {
        if ((Errors >= ErrorTarget) && (!AbortMargin)) {
          Margins[Lane] = 0;
        }
      } else {
        if ((Errors < ErrorTarget) && (!AbortMargin)) {
          Margins[Lane] = JITTER_LENGTH * 100;
        }
      }

      ///
      /// Compute the next margin point to start at
      ///
      StartJitter = (Margins[Lane] / 100) - JITTER_MARGIN_INITIAL_OFFSET;
      if (StartJitter < 0) {
        StartJitter = 0;
      }

      ///
      /// Check for convergance
      ///
      if (LastMargin == -1) {
        LastMargin = Margins[Lane];
      } else {
        MarginDifference = CalculateMarginDifference (LastMargin, Margins[Lane]);
        if (MarginDifference <= MARGIN_CONVERGANCE_ALLOWED_DELTA) {
          ConvergenceCounter++;
        } else {
          ConvergenceCounter = 0;
        }
        LastMargin = Margins[Lane];
      }

      if (LinkIsDowngraded (PegPort, OriginalLinkSpeed, OriginalLinkWidth)) {
        Status = SetJitterTolerance (&Lane, 1, 0);
        ASSERT_EFI_ERROR (Status);
      }

      ///
      /// If the link degraded in any way, bring it back to functional state
      ///
      Status = EnsureLinkIsHealthy (PeiServices, SaPlatformPolicyPpi, SaDataHob, StallPpi, PegPort, OriginalLinkSpeed, OriginalLinkWidth);
      if (EFI_ERROR (Status)) {
        return Status;
      }
    } ///< End of repeat while loop

    ///
    /// Remove Jitter in preparation for testing the next lane
    ///
    Status = SetJitterTolerance (&Lane, 1, 0);
    ASSERT_EFI_ERROR (Status);
    if (TxJitterTest) {
      EnableTxJitterInjection (Lane, FALSE);
    }
  } ///< End of for each lane loop

  return EFI_SUCCESS;
}

UINT32
SaPciePointTest (
  IN EFI_PEI_SERVICES       **PeiServices,
  IN SA_PLATFORM_POLICY_PPI *SaPlatformPolicyPpi,
  IN PEI_STALL_PPI          *StallPpi,
  IN UINT32                 MonitorPort,
  IN PEG_PORT               *PegPort,
  IN UINT32                 InitialRecoveryCount
  )
{
  UINT32  Data32;

  StallPpi->Stall (PeiServices, StallPpi, SaPlatformPolicyPpi->PcieConfig->PegGen3PresetSearchDwellTime);
  Data32 = SaPcieGetErrorCount (MonitorPort, PegPort->Function) - InitialRecoveryCount;

  return Data32;
}

INT32
CalculateMarginDifference (
  IN INT32                   Margin1,
  IN INT32                   Margin2
  )
{
  if (Margin1 < Margin2) {
    return Margin2 - Margin1;
  } else {
    return Margin1 - Margin2;
  }
}

INT32
InterpolateMargin (
  IN UINT32 ErrorTarget,
  IN UINT32 CurrentErrorCount,
  IN UINT32 PreviousErrorCount,
  IN INT32  FailingPoint
  )
{
  UINT32 LnErrorTarget;
  UINT32 LnCurrentErrorCount;
  UINT32 LnPreviousErrorCount;
  INT32  Margin;

  if (ErrorTarget > 40000) {
    ErrorTarget = 40000;
  }
  if (CurrentErrorCount > 40000) {
    CurrentErrorCount = 40000;
  }
  if (PreviousErrorCount > 40000) {
    PreviousErrorCount = 40000;
  }

  LnErrorTarget        = NaturalLog (ErrorTarget * 100);
  LnCurrentErrorCount  = NaturalLog (CurrentErrorCount * 100);
  LnPreviousErrorCount = NaturalLog (PreviousErrorCount * 100);

  if (FailingPoint >= 0) {
    if ((LnCurrentErrorCount - LnPreviousErrorCount) == 0) {
      Margin = (FailingPoint - 1) * 100;
    } else {
      Margin = ((LnErrorTarget - LnPreviousErrorCount) * 100) /
               (LnCurrentErrorCount - LnPreviousErrorCount)   +
               ((FailingPoint - 1) * 100);
    }
    if (Margin < 0) {
      Margin = 0;
    }
  } else {
    if ((LnCurrentErrorCount - LnPreviousErrorCount) == 0) {
      Margin = (FailingPoint + 1) * 100;
    } else {
      Margin = ((FailingPoint + 1) * 100)                     -
               ((LnErrorTarget - LnPreviousErrorCount) * 100) /
               (LnCurrentErrorCount - LnPreviousErrorCount);
    }
    if (Margin > 0) {
      Margin = 0;
    }
  }

  return Margin;
}


UINT32
NaturalLog (
  IN UINT32 Input
  )
  /*++

  Routine Description:

    This function calculates the Natural Log of the Input parameter using integers

  Arguments:

    Input           - 100 times a number to get the Natural log from.
                    - Max Input Number is 40,000 (without 100x)

  Returns:

    Output          - 100 times the actual result. Accurate within +/- 2

  --*/
{
  UINT32  Output;

  ///
  ///Special case - treat 0 recoveries as 1 recovery for interpolation purposes
  ///
  if (Input == 0) {
    return 0;
  }

  Output = 0;
  while (Input > 271) {
    Input   = (Input * 1000) / 2718;
    Output += 100;
  }

  Output += ((-16 * Input * Input + 11578 * Input - 978860) / 10000);

  return Output;
}

#endif // PEG_FLAG