summaryrefslogtreecommitdiff
path: root/ReferenceCode/Chipset/SystemAgent/SaInit/Pei/SaDmiPeim.c
blob: ea817d85e05f9c2f5e395d3f9cc8a4230ac7ed41 (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
/** @file
  SA Dmi PEI Initialization library

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

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

**/
#include "SaDmiPeim.h"
#include "PciExpressInit.h"

///
/// Functions
///
/**
  Initialize DMI Tc/Vc mapping through SA-PCH.

  @param[in] PeiServices          - General purpose services available to every PEIM.
  @param[in] SaPlatformPolicyPpi  - Instance of SA_PLATFORM_POLICY_PPI

  @retval EFI_SUCCESS
**/
EFI_STATUS
SaDmiTcVcInit (
  IN EFI_PEI_SERVICES          **PeiServices,
  IN SA_PLATFORM_POLICY_PPI    *SaPlatformPolicyPpi
  )
{
  EFI_STATUS        Status;
  UINT64            MchBar;
  UINT64            DmiBar;
  PCH_INIT_PPI      *PchInitPpi;
  PCH_DMI_TC_VC_PPI *PchDmiTcVcMapPpi;
  CPU_FAMILY        CpuFamilyId;
  UINT8             i;

  MchBar  = McD0PciCfg64 (R_SA_MCHBAR) &~BIT0;
  DmiBar  = McD0PciCfg64 (R_SA_DMIBAR) &~BIT0;
  CpuFamilyId = GetCpuFamily();

  ///
  /// Locate PchInitPpi and PchDmiTcVcMapPpi
  ///
  Status = (*PeiServices)->LocatePpi (PeiServices, &gPchInitPpiGuid, 0, NULL, (VOID **) &PchInitPpi);
  ASSERT_EFI_ERROR (Status);

  Status = (*PeiServices)->LocatePpi (PeiServices, &gPchDmiTcVcMapPpiGuid, 0, NULL, (VOID **) &PchDmiTcVcMapPpi);
  ASSERT_EFI_ERROR (Status);

  ///
  /// SA OPI Initialization
  ///
  if (CpuFamilyId == EnumCpuHswUlt) {
    MmioOr8 ((UINTN) (DmiBar + 0xA78), BIT1);
  }

  ///
  /// Update DmiTcVcMapping based on Policy
  ///
  PchDmiTcVcMapPpi->DmiVc[DmiVcTypeVc1].Enable  = (BOOLEAN) SaPlatformPolicyPpi->PcieConfig->DmiVc1;
  PchDmiTcVcMapPpi->DmiVc[DmiVcTypeVcp].Enable  = (BOOLEAN) SaPlatformPolicyPpi->PcieConfig->DmiVcp;
  PchDmiTcVcMapPpi->DmiVc[DmiVcTypeVcm].Enable  = (BOOLEAN) SaPlatformPolicyPpi->PcieConfig->DmiVcm;

  for (i = 0; i < DmiTcTypeMax; i++) {
    if (((PchDmiTcVcMapPpi->DmiTc[i].Vc == DmiVcTypeVc1) && (PchDmiTcVcMapPpi->DmiVc[DmiVcTypeVc1].Enable == FALSE)) ||
        ((PchDmiTcVcMapPpi->DmiTc[i].Vc == DmiVcTypeVcp) && (PchDmiTcVcMapPpi->DmiVc[DmiVcTypeVcp].Enable == FALSE)) ||
        ((PchDmiTcVcMapPpi->DmiTc[i].Vc == DmiVcTypeVcm) && (PchDmiTcVcMapPpi->DmiVc[DmiVcTypeVcm].Enable == FALSE))
        ) {
      PchDmiTcVcMapPpi->DmiTc[i].Vc = DmiVcTypeVc0;
    }
  }
  ///
  /// Program NB TC/VC mapping
  ///
  SaSetDmiTcVcMapping (PchDmiTcVcMapPpi, DmiBar);

  ///
  /// Call PchDmiTcVcProgPoll
  ///
  Status = PchInitPpi->DmiTcVcProgPoll (PeiServices);
  ASSERT_EFI_ERROR (Status);

  ///
  /// Poll NB negotiation completion
  ///
  SaPollDmiVcStatus (PchDmiTcVcMapPpi, DmiBar);
  ASSERT_EFI_ERROR (Status);

  return EFI_SUCCESS;

}

/**
  Map SA DMI TCs to VC

  @param[in] PchDmiTcVcMapPpi     - Instance of PCH_DMI_TC_VC_PPI
  @param[in] DmiBar               - DMIBAR address

  @retval EFI_SUCCESS            -  Succeed.
  @retval EFI_INVALID_PARAMETER  -  Wrong phase parameter passed in.
**/
EFI_STATUS
SaSetDmiTcVcMapping (
  IN    PCH_DMI_TC_VC_PPI   *PchDmiTcVcMapPpi,
  IN    UINT64              DmiBar
  )
{
  UINT32  Data32And;
  UINT32  Data32Or;
  UINT8   Data8And;
  UINT8   Data8Or;
  UINT8   Index;
  UINT16  Register;
  UINT8   VcId;
  UINT8   VcMap[DmiVcTypeMax] = { 0 };

  ///
  /// Set the TC/VC mappings
  ///
  for (Index = 0; Index < DmiTcTypeMax; Index++) {
    VcMap[PchDmiTcVcMapPpi->DmiTc[Index].Vc] |= (BIT0 << Index);
  }
  ///
  /// System BIOS must perform the following steps for VC0 configuration.
  ///   Program the TCs/VC0 map by setting DMIBAR offset 014h [7:1] = '0111 101b'.
  ///
  /// Private Virtual Channel Configuration
  /// Step1. Assign Virtual Channel ID 2 to VCp:
  ///    Programming the DMIVCPRCTL DMI Port Register DMIBAR Offset 02Ch[26:24] = '010b'.
  ///
  /// Step2. Set TC2 to VCp:
  ///    Program the DMIVCPRCTL DMI Port Register DMIBAR offset 02Ch [7:1] = '0000 010b'.
  ///
  /// Step3. Enable VCp by programming the DMIVCPRCTL DMI Port Register DMIBAR Offset 02Ch[31] = '1b'.
  ///
  /// Virtual Channel for ME (VCm) Configuration
  /// This is configured by ConfigMemMe
  ///
  /// Step1. Assign Virtual Channel ID 7 to VCm:
  ///    Programming the DMIVCMRCTL DMI Port Register DMIBAR Offset 038h[26:24] = '111b'.
  ///
  /// Step2. Enable VCm:
  ///    Programming the DMIVMPRCTL DMI Port Register DMIBAR Offset 038h[31] = '1b'.
  ///
  /// Step3. Enable VCm by programming the DMIVCMRCTL DMI Port Register DMIBAR Offset 038h[31] = '1b'.
  ///
  for (Index = 0; Index < DmiVcTypeMax; Index++) {
    if (PchDmiTcVcMapPpi->DmiVc[Index].Enable == PCH_DEVICE_ENABLE) {
      ///
      /// Map TCs to VC, Set the VC ID, Enable VC
      ///
      VcId = PchDmiTcVcMapPpi->DmiVc[Index].VcId,

      Data32And = (UINT32) (~(V_SA_DMIBAR_DMIVCCTL_ID | B_SA_DMIBAR_DMIVCCTL_TVM_MASK));
      Data32Or = VcId << N_SA_DMIBAR_DMIVCCTL_ID;
      Data32Or |= VcMap[Index];
      Data32Or |= N_SA_DMIBAR_DMIVCCTL_EN;

      switch (Index) {
      case DmiVcTypeVc0:
        Register = R_SA_DMIBAR_DMIVC0RCTL_OFFSET;
        break;

      case DmiVcTypeVc1:
        Register = R_SA_DMIBAR_DMIVC1RCTL_OFFSET;
        break;

      case DmiVcTypeVcp:
        Register = R_SA_DMIBAR_DMIVCPRCTL_OFFSET;
        break;

      case DmiVcTypeVcm:
        Register = R_SA_DMIBAR_DMIVCMRCTL_OFFSET;
        break;

      default:
        return EFI_INVALID_PARAMETER;
      }

      MmioAndThenOr32 ((UINTN) (DmiBar + Register), Data32And, Data32Or);
    }
  }
  ///
  /// System BIOS must program the extended VC Count:
  ///    Set the DMI Port Register DMIBAR Offset 004h[2:0]=001b
  ///
  Data8And = (UINT8) (~0x07);
  if (PchDmiTcVcMapPpi->DmiVc[DmiVcTypeVc1].Enable == TRUE) {
    Data8Or = 1;
  } else {
    Data8Or = 0;
  }

  MmioAndThenOr8 ((UINTN) (DmiBar + R_SA_DMIBAR_DMIPVCCAP1_OFFSET), Data8And, Data8Or);

  return EFI_SUCCESS;
}

/**
  Poll SA DMI negotiation completion

  @param[in] PchDmiTcVcMapPpi     - Instance of PCH_DMI_TC_VC_PPI
  @param[in] DmiBar               - DMIBAR address

  @retval EFI_SUCCESS            -  Succeed.
  @retval EFI_INVALID_PARAMETER  -  Wrong phase parameter passed in.
**/
EFI_STATUS
SaPollDmiVcStatus (
  IN    PCH_DMI_TC_VC_PPI   *PchDmiTcVcMapPpi,
  IN    UINT64              DmiBar
  )
{
  UINT8   Index;
  UINT16  Register;

  ///
  /// 6.2.3.2 - Step 4, Poll until VC1 has been negotiated
  ///    Read the DMIVC1RSTS DMI Port Register Offset 026h until [1]==0
  ///
  /// 6.2.3.3 - Step4. Poll the VCp Negotiation Pending bit until it reads 0:
  ///    Read the DMIVCPRSTS DMI Port Register Offset 032h until [1]==0
  ///
  /// 6.2.3.4 - Step4. Poll the VCm Negotiation Pending bit until it reads 0:
  ///    Read the DMIVCMRSTS DMI Port Register Offset 03Eh until [1]==0
  ///
  for (Index = 0; Index < DmiVcTypeMax; Index++) {
    if (PchDmiTcVcMapPpi->DmiVc[Index].Enable == PCH_DEVICE_ENABLE) {
      switch (Index) {
      case DmiVcTypeVc0:
        Register = R_SA_DMIBAR_DMIVC0RSTS_OFFSET;
        break;

      case DmiVcTypeVc1:
        Register = R_SA_DMIBAR_DMIVC1RSTS_OFFSET;
        break;

      case DmiVcTypeVcp:
        Register = R_SA_DMIBAR_DMIVCPRSTS_OFFSET;
        break;

      case DmiVcTypeVcm:
        Register = R_SA_DMIBAR_DMIVCMRSTS_OFFSET;
        break;

      default:
        return EFI_INVALID_PARAMETER;
      }
      ///
      /// Wait for negotiation to complete
      ///
      while ((MmioRead16 ((UINTN) (DmiBar + Register)) & B_SA_DMIBAR_DMISTS_NP) != 0);
    }
  }

  return EFI_SUCCESS;
}

#ifdef DMI_FLAG
/**
  Initialize DMI.

  @param[in] PeiServices          - General purpose services available to every PEIM.
  @param[in] SaPlatformPolicyPpi  - Instance of SA_PLATFORM_POLICY_PPI

  @retval EFI_SUCCESS
**/
EFI_STATUS
DmiInit (
  IN EFI_PEI_SERVICES          **PeiServices,
  IN SA_PLATFORM_POLICY_PPI    *SaPlatformPolicyPpi
  )
{
  EFI_STATUS              Status;
  UINT64                  MchBar;
  UINT64                  DmiBar;
  PCH_INIT_PPI            *PchInitPpi;
  PCH_PLATFORM_POLICY_PPI *PchPlatformPolicyPpi;
  UINT8                   CpuSteppingId;
  BOOLEAN                 DmiGen2Enable;
  UINT16                  LinkStatus;
  UINT32                  Data32Or;

  ///
  /// Read the CPU stepping
  ///
  CpuSteppingId = GetCpuStepping();

  ///
  /// BridgeId = (UINT8) (McD0PciCfg16 (R_MC_DEVICE_ID) & 0xF0);
  /// BridgeSteppingId = BridgeId + CpuSteppingId;
  ///
  MchBar  = McD0PciCfg64 (R_SA_MCHBAR) &~BIT0;
  DmiBar  = McD0PciCfg64 (R_SA_DMIBAR) &~BIT0;

  ///
  /// Get RCBA through the PchPlatformPolicy PPI
  ///
  Status = (**PeiServices).LocatePpi (
                            PeiServices,
                            &gPchPlatformPolicyPpiGuid,
                            0,
                            NULL,
                            &PchPlatformPolicyPpi
                            );
  ASSERT_EFI_ERROR (Status);

  if (EFI_ERROR (Status)) {
    DEBUG ((EFI_D_ERROR, "Can't locate PchPlatformPolicy PPI - exiting.\n"));
    return Status;
  }
  ///
  /// Perform DMI Recipe steps
  ///
  DEBUG ((EFI_D_INFO, "DMI Recipe...\n"));
  PegDmiRecipe (SaPlatformPolicyPpi, (UINT32) MchBar, (UINT32) DmiBar, 0, 0);

  ///
  /// Additional DMI steps. See SA BIOS Spec.
  ///
  DEBUG ((EFI_D_INFO, "Run AdditionalDmiProgramSteps!\n"));
  AdditionalDmiProgramSteps (SaPlatformPolicyPpi, (UINT32) MchBar, (UINT32) DmiBar);

  DmiGen2Enable = TRUE;
  if ((SaPlatformPolicyPpi->PcieConfig->DmiGen2 == 0) ||
      ((MmioRead8 ((UINTN) PchPlatformPolicyPpi->Rcba + R_PCH_RCRB_LCAP) & (BIT0 | BIT1 | BIT2 | BIT3)) == 0x1) ||
      (McDevFunPciCfg32 (0, 0, 0, R_SA_MC_CAPID0_A_OFFSET) & BIT22)
     ) {
    DEBUG ((EFI_D_WARN, "DMI Gen2 is Disabled or not capable, staying at Gen1 !\n"));
    DmiGen2Enable = FALSE;
  }

  if (DmiGen2Enable) {
    ///
    /// Locate PchInitPpi
    ///
    Status = (*PeiServices)->LocatePpi (PeiServices, &gPchInitPpiGuid, 0, NULL, &PchInitPpi);
    ASSERT_EFI_ERROR (Status);

    ///
    /// Program PCH TLS to Gen 2
    ///
    PchInitPpi->DmiGen2Prog (PeiServices);

    ///
    /// Program CPU Max Link Speed to Gen 2
    ///
    MmioAndThenOr32 ((UINTN) (DmiBar + R_SA_DMIBAR_LCAP_OFFSET), (UINT32)~0xF, 2);
  }
  Data32Or = (MmioRead32 ((UINTN) (DmiBar + R_SA_DMIBAR_LCAP_OFFSET)) & (BIT3 | BIT2 | BIT1 | BIT0));
  MmioAndThenOr32 ((UINTN) (DmiBar + R_SA_DMIBAR_LCTL2_OFFSET), (UINT32)~(BIT3 | BIT2 | BIT1 | BIT0), Data32Or);

  ///
  /// Retrain link
  ///
  DmiLinkTrain (DmiBar);

  ///
  /// Retrain link if it is GEN2 Capable and it is not yet set to GEN2
  ///
  if (DmiGen2Enable &&
      ((((MmioRead16 ((UINTN) (DmiBar + R_SA_DMIBAR_LSTS_OFFSET))) & 0x0F) != DMI_GEN2) ||
       (((MmioRead16 ((UINTN) PchPlatformPolicyPpi->Rcba + R_PCH_RCRB_LSTS)) & 0x0F) != DMI_GEN2))
     ) {
    DEBUG ((EFI_D_INFO, "DMI Link re-train to set GEN2\n"));
    DmiLinkTrain (DmiBar);
  }
  ///
  /// Get the current link status
  ///
  LinkStatus = MmioRead16 ((UINTN) (DmiBar + R_SA_DMIBAR_LSTS_OFFSET));
  DEBUG ((EFI_D_INFO, "DMI trained to x%d at Gen%d\n", (LinkStatus >> 4) & 0x3F, LinkStatus & 0x0F));

  return EFI_SUCCESS;
}

/**
  DMI link training

  @param[in] DmiBar - DMIBAR address
**/
VOID
DmiLinkTrain (
  IN  UINT64 DmiBar
  )
{
  ///
  /// Retrain link
  ///
  MmioOr8 ((UINTN) (DmiBar + R_SA_DMIBAR_LCTL_OFFSET), BIT5);

  ///
  /// Wait for link training complete
  ///
  while ((MmioRead16 ((UINTN) (DmiBar + R_SA_DMIBAR_LSTS_OFFSET)) & BIT11) != 0)
    ;
}

/**
  Additional DMI Programming Steps at PEI

  @param[in] SaPlatformPolicyPpi - pointer to SA_PLATFORM_POLICY_PPI
  @param[in] MchBar              - MCHBAR address
  @param[in] DmiBar              - DMIBAR address
**/
VOID
AdditionalDmiProgramSteps (
  IN SA_PLATFORM_POLICY_PPI    *SaPlatformPolicyPpi,
  IN UINT32                    MchBar,
  IN UINT32                    DmiBar
  )
{
  UINT32  Data32And;
  UINT32  Data32Or;

  ///
  /// Disable DMI and PEG Debug Align Message - set 0x258[29] = '1b'
  ///
  Data32And = (UINT32) ~BIT29;
  Data32Or  = BIT29;
  Mmio32AndThenOr (DmiBar, R_SA_DMIBAR_CFG4_OFFSET, Data32And, Data32Or);

  ///
  /// Overwrite DMICC (DMIBAR offset 0x208) to 0x6B5
  ///
  Data32And = (UINT32)~(BIT10 | BIT9 | BIT8 | BIT7 | BIT6 | BIT5 | BIT4 | BIT3 | BIT2 | BIT1 | BIT0);
  Data32Or  = 0x6B5;
  Mmio32AndThenOr (DmiBar, R_SA_DMIBAR_DMICC_OFFSET, Data32And, Data32Or);

  ///
  /// Set L0SLAT[15:0] to 0x2020
  ///
  Data32And = (UINT32) ~(0xFFFF);
  Data32Or  = 0x00002020;
  Mmio32AndThenOr (DmiBar, R_SA_DMIBAR_L0SLAT_OFFSET, Data32And, Data32Or);
}
#endif // DMI_FLAG