summaryrefslogtreecommitdiff
path: root/Core/CPU/CpuSpSmi.c
blob: bd4469dadeb803868c4c17f8a5c06c3b53159c40 (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
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1987-2013, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**         5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************
//**********************************************************************
// $Header: /Alaska/SOURCE/Modules/SharkBayRefCodes/Haswell/AMI Cpu PKG/CpuSp SMI/CpuSpSmi.c 8     5/24/15 11:37p Davidhsieh $
//
// $Revision: 8 $
//
// $Date: 5/24/15 11:37p $
//**********************************************************************
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/SharkBayRefCodes/Haswell/AMI Cpu PKG/CpuSp SMI/CpuSpSmi.c $
// 
// 8     5/24/15 11:37p Davidhsieh
// [TAG]  		EIP215675
// [Category]  	New Feature
// [Description]  	Smm Access Check feautre support
// [Files]  	Cpu.sdl, CpuDxe.c, CpuSpSmi.sdl, CpuSpsmi.c
// 
// 7     10/08/13 3:10a Crystallee
// [TAG]  		EIP137873
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	CPU Security Configuration(MSR 4E0[0]) will be changed
// after S3 resume
// [RootCause]  	Didn't restore the Cpu MSR 4e0 while S3 resume.
// [Solution]  	Restore the Cpu MSR 0x4E0 while S3 resume.
// 
// 6     1/24/13 10:45p Davidhsieh
// [TAG]  		EIP112381
// [Category]  	Bug Fix
// [Severity]  	Important
// [Symptom]  	TBoot S3 resume fail
// [RootCause]  	MSR 0x2e7 is not restored
// [Solution]  	Save and restore MSR 0x2e7 for S3
// 
// 5     12/18/12 9:23p Davidhsieh
// [TAG]              None
// [Category]       Improvement
// [Description]    Check CPU before lock SMM_FEATURE_CONTROL MSR
// 
// 4     12/09/12 10:35p Davidhsieh
// [TAG]                None
// [Category]         Improvement
// [Description]     Lock SMM_FEATURE_CONTROL MSR 
// 
// 3     10/04/12 9:20a Davidhsieh
// Change the procedure of configuring MTRR
// 
// 2     7/10/12 2:51a Davidhsieh
// [TAG]  		EIP93180
// [Category]  	Bug Fix
// [Severity]  	Important
// [Symptom]  	The "Event Kernel-Power 137" error event log shows while
// resumed from S3
// [RootCause]  	The fixed and variable Mtrrs of BSP are not restored
// [Solution]  	Restore BPS's mtrss
// 
// 1     2/07/12 4:00a Davidhsieh
//
//
//**********************************************************************

//<AMI_FHDR_START>
//---------------------------------------------------------------------------
//
// Name:        CpuSpSmi.C
//
// Description:	Provide functions to CPU specific SMI
//
//---------------------------------------------------------------------------
//<AMI_FHDR_END>

#include "CpuSpSmi.h"

UINT64	ReadMsr (UINT32 Msr);
VOID WriteMsr(UINT32 Msr, UINT64 Value);

UINTN MpMtrrSynchUpEntry (VOID);
VOID MpMtrrSynchUpExit (UINTN Cr4);

EFI_GUID              SwDispatchProtocolGuid = EFI_SMM_SW_DISPATCH_PROTOCOL_GUID;

EFI_SMM_SYSTEM_TABLE         *mSmst;
EFI_SMM_BASE_PROTOCOL        *pSmmBase;
EFI_SMM_SW_DISPATCH_PROTOCOL *gSwDispatch;

//
// MSR table for S3 resume
//
EFI_MSR_VALUES       mFixedMtrrValues[] = {
  { MTRR_FIX_64K_00000,          0 },
  { MTRR_FIX_16K_80000,          0 },
  { MTRR_FIX_16K_A0000,          0 },
  { MTRR_FIX_4K_C0000,           0 },
  { MTRR_FIX_4K_C8000,           0 },
  { MTRR_FIX_4K_D0000,           0 },
  { MTRR_FIX_4K_D8000,           0 },
  { MTRR_FIX_4K_E0000,           0 },
  { MTRR_FIX_4K_E8000,           0 },
  { MTRR_FIX_4K_F0000,           0 },
  { MTRR_FIX_4K_F8000,           0 },
  { 0,      0 }
};

EFI_MSR_VALUES       mVariableMtrrValues[] = {
  { MTRR_PHYS_BASE_0,             0 },
  { MTRR_PHYS_MASK_0,             0 },
  { MTRR_PHYS_BASE_1,             0 },
  { MTRR_PHYS_MASK_1,             0 },
  { MTRR_PHYS_BASE_2,             0 },
  { MTRR_PHYS_MASK_2,             0 },
  { MTRR_PHYS_BASE_3,             0 },
  { MTRR_PHYS_MASK_3,             0 },
  { MTRR_PHYS_BASE_4,             0 },
  { MTRR_PHYS_MASK_4,             0 },
  { MTRR_PHYS_BASE_5,             0 },
  { MTRR_PHYS_MASK_5,             0 },
  { MTRR_PHYS_BASE_6,             0 },
  { MTRR_PHYS_MASK_6,             0 },
  { MTRR_PHYS_BASE_7,             0 },
  { MTRR_PHYS_MASK_7,             0 },
  { MTRR_PHYS_BASE_8,             0 },
  { MTRR_PHYS_MASK_8,             0 },
  { MTRR_PHYS_BASE_9,             0 },
  { MTRR_PHYS_MASK_9,             0 }
};

#define MiscMsrCount 1

EFI_MSR_VALUES       mMiscMsrValues[] = {
  { 0x2e7,          0 }
};

EFI_MSR_VALUES		mSmmFeatureCtrl = { 0x4e0, 0};

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   SaveCpuMsr
//
// Description: This function saves the CPU MSRs for S3 resume usage.
//
// Input:       DispatchHandle      Handle to the Dispatcher
//              DispatchContext     SW SMM dispatcher context
//
// Output:      None
//
// Modified:
//      
// Referrals: 
//
// Notes:
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID 
SaveCpuMsr(
    IN EFI_HANDLE                   DispatchHandle,
    IN EFI_SMM_SW_DISPATCH_CONTEXT  *DispatchContext
 )
{
    UINT32  i;    
    
    if (Shr64(ReadMsr(0x17d),57)  & 0x03){
        if (!(ReadMsr(mSmmFeatureCtrl.Index) && 0x01))
            WriteMsr(mSmmFeatureCtrl.Index, ReadMsr(mSmmFeatureCtrl.Index) | 0x01);
		mSmmFeatureCtrl.Value = ReadMsr(mSmmFeatureCtrl.Index);
    }

    //Save Fixed MTRR
    for(i=0; i < NUM_OF_FIXED_MTRRS; i++)
    {
        mFixedMtrrValues[i].Value = ReadMsr(mFixedMtrrValues[i].Index);
    }
    
    //Save variable MTRR
    for (i = 0; i < ((UINT8)(ReadMsr(MSR_IA32_MTRR_CAP) & VCNT_MASK)); i++)
    {
        mVariableMtrrValues[i * 2].Value = ReadMsr(mVariableMtrrValues[i * 2].Index);        
        mVariableMtrrValues[i * 2 + 1].Value = ReadMsr(mVariableMtrrValues[i * 2 + 1].Index);
	}
	
	mMiscMsrValues[0].Value = ReadMsr(mMiscMsrValues[0].Index);
}


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   RestoreMsrOnCpu
//
// Description: This function restores the CPU MSRs during S3 resume.
//
// Input:       None
//
// Output:      None
//
// Modified:
//      
// Referrals: 
//
// Notes:
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID 
RestoreMsrOnCpu(IN VOID *data)
{
    UINT32  i;
    UINTN   Cr4;
    
    Cr4 = MpMtrrSynchUpEntry();
    
    //Restore Fixed MTRR
    for(i=0; i < NUM_OF_FIXED_MTRRS; i++)
    {
        WriteMsr(mFixedMtrrValues[i].Index, mFixedMtrrValues[i].Value );
    }
    
    //Restore variable MTRR
    for (i = 0; i < ((UINT8)(ReadMsr(MSR_IA32_MTRR_CAP) & VCNT_MASK)); i++)
    {
        WriteMsr(mVariableMtrrValues[i * 2].Index, mVariableMtrrValues[i * 2].Value );
        WriteMsr(mVariableMtrrValues[i * 2 + 1].Index, mVariableMtrrValues[i * 2 + 1].Value );     
	}
    
    MpMtrrSynchUpExit (Cr4);
     
    WriteMsr(mMiscMsrValues[0].Index, mMiscMsrValues[0].Value);
    
}   

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   RestoreCpuMsr
//
// Description: This function restores the CPU MSRs during S3 resume.
//
// Input:       DispatchHandle      Handle to the Dispatcher
//              DispatchContext     SW SMM dispatcher context
//
// Output:      None
//
// Modified:
//      
// Referrals: 
//
// Notes:
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID 
RestoreCpuMsr(
    IN EFI_HANDLE                   DispatchHandle,
    IN EFI_SMM_SW_DISPATCH_CONTEXT  *DispatchContext
 )
{
    UINT32  i;
    UINTN   Cr4;

	if (Shr64(ReadMsr(0x17d),57)  & 0x03){
        if (!(ReadMsr(mSmmFeatureCtrl.Index) && 0x01)){
            WriteMsr(mSmmFeatureCtrl.Index, (mSmmFeatureCtrl.Value & ~0x01));
			WriteMsr(mSmmFeatureCtrl.Index, ReadMsr(mSmmFeatureCtrl.Index) | 0x01);
		}
    }

    for (i = 0; i < pSmst->NumberOfCpus; ++i) {
        pSmst->SmmStartupThisAp(RestoreMsrOnCpu, i, NULL);
    }
    
    Cr4 = MpMtrrSynchUpEntry();
    RestoreMsrOnCpu(NULL);    
    MpMtrrSynchUpExit (Cr4);
}

#define SMRAM_CPU_DATA_VARIABLE_GUID \
  { \
    0x429501d9, 0xe447, 0x40f4, 0x86, 0x7b, 0x75, 0xc9, 0x3a, 0x1d, 0xb5, 0x4e \
  }

#define SMRAM_CPU_DATA_VARIABLE L"SmramCpuDataVar"

#define SMM_FROM_CPU_DRIVER_SAVE_INFO 0x81

EFI_GUID mSmramCpuDataVariableGuid = SMRAM_CPU_DATA_VARIABLE_GUID;
BOOLEAN mLocked = FALSE;

/**
  Dispatch function for a Software SMI handler.

  @param  DispatchHandle        The handle of this dispatch function.
  @param  DispatchContext       The pointer to the dispatch function's context.
                                The SwSmiInputValue field is filled in
                                by the software dispatch driver prior to
                                invoking this dispatch function.
                                The dispatch function will only be called
                                for input values for which it is registered.

  @return None

**/
VOID
EFIAPI
SmramSaveInfoHandler (
  IN  EFI_HANDLE                    DispatchHandle,
  IN  EFI_SMM_SW_DISPATCH_CONTEXT   *DispatchContext
  )
/**

@brief

  Dispatch function for a Software SMI handler

  @param[in] DispatchHandle  - The handle of this dispatch function.
  @param[in] DispatchContext - The pointer to the dispatch function's context.


**/
{
  EFI_STATUS  Status;
  UINT64      VarData[3];
  UINTN       VarSize;

  if (!mLocked && IoRead8 (SW_SMI_IO_ADDRESS+1) == SMM_FROM_CPU_DRIVER_SAVE_INFO) {
    VarSize = sizeof (VarData);
    Status = pRS->GetVariable (
                    SMRAM_CPU_DATA_VARIABLE,
                    &mSmramCpuDataVariableGuid,
                    NULL,
                    &VarSize,
                    VarData
                    );
    if (!EFI_ERROR (Status) && VarSize == sizeof (VarData)) {
      MemCpy (
        (VOID *) (UINTN) (VarData[0]),
        (VOID *) (UINTN) (VarData[1]),
        (UINTN) (VarData[2])
        );
    }

    mLocked = TRUE;
  }
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   CpuSmmFeatureEn
//
// Description: 
//
// Input:       DispatchHandle      Handle to the Dispatcher
//              DispatchContext     SW SMM dispatcher context
//
// Output:      None
//
// Modified:
//      
// Referrals: 
//
// Notes:
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
VOID 
CpuSmmFeatureEn(
    IN EFI_HANDLE                   DispatchHandle,
    IN EFI_SMM_SW_DISPATCH_CONTEXT  *DispatchContext
 )
{

    UINT64 MsrData;
    
    if (Shr64(ReadMsr(0x17d),58)  & 0x01){
        if (!(ReadMsr(0x4e0) && 0x01)){
            MsrData = ReadMsr(0x4e0);
            MsrData |= 0x4;
            WriteMsr(0x4e0, MsrData);			
		}
    }
}
//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   InSmmFunction
//
// Description: Called from InstallSmiHandler
//
// Input:
//
//
// Output:      None
//
// Modified:
//      
// Referrals: 
//
// Notes:
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS InSmmFunction(
    IN EFI_HANDLE          ImageHandle,
    IN EFI_SYSTEM_TABLE    *SystemTable
 )
{
    
    EFI_STATUS  Status;
    EFI_HANDLE  Handle;
    
    EFI_SMM_SW_DISPATCH_CONTEXT Save_CPU_MSR = {SW_SMI_SAVE_MSR};
    EFI_SMM_SW_DISPATCH_CONTEXT Restore_CPU_MSR = {SW_SMI_RESTORE_MSR};    
    EFI_SMM_SW_DISPATCH_CONTEXT CpuDriverSaveInfo = { SMM_FROM_SMBASE_DRIVER };
    EFI_SMM_SW_DISPATCH_CONTEXT CpuSmmFeatureEnable = {SW_SMI_ENABLE_SMM_FEATURE};

    Status = pBS->LocateProtocol(&SwDispatchProtocolGuid,NULL,&gSwDispatch);
    if (EFI_ERROR(Status)) {
        ASSERT_EFI_ERROR(Status);
        return Status;	
    }

    Status = gSwDispatch->Register(
        gSwDispatch,
        SaveCpuMsr,
        &Save_CPU_MSR,
        &Handle
    );
    if (EFI_ERROR(Status)) {
        ASSERT_EFI_ERROR(Status);
        return Status;	
    }

    Status = gSwDispatch->Register(
        gSwDispatch,
        RestoreCpuMsr,
        &Restore_CPU_MSR,
        &Handle
    );
    
    if (EFI_ERROR(Status)) {
        ASSERT_EFI_ERROR(Status);
        return Status;	
    }

    Status = gSwDispatch->Register(
        gSwDispatch,
        SmramSaveInfoHandler,
        &CpuDriverSaveInfo,
        &Handle
    );
    
    if (EFI_ERROR(Status)) {
        ASSERT_EFI_ERROR(Status);
        return Status;	
    }
    
    Status = gSwDispatch->Register(
        gSwDispatch,
        CpuSmmFeatureEn,
        &CpuSmmFeatureEnable,
        &Handle
    );
    
    Status = pBS->LocateProtocol(&gEfiSmmBaseProtocolGuid, NULL, &pSmmBase);
    if (EFI_ERROR(Status)) {
        return Status;
    }

    /*Status = pBS->LocateProtocol (&gEfiSmmThunkProtocolGuid, NULL, &mSmmThunk);
    if (EFI_ERROR(Status)) {
        TRACE_IDESMM(((UINTN)TRACE_ALWAYS, "SMM Thunk Protocol not located.\n"));
    } else {
        TRACE_IDESMM(((UINTN)TRACE_ALWAYS, "SMM Thunk Protocol located.\n"));
    }*/

    pSmmBase->GetSmstLocation (pSmmBase, &mSmst);

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   NotInSmmFunction
//
// Description: If Not In Smm Function
//
// Input:
//
//
// Output:      None
//
// Modified:
//      
// Referrals: 
//
// Notes:   
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
EFI_STATUS NotInSmmFunction(
 )
{
    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:   CpuSpSmiInit
//
// Description: Initializes CPU specific SMM Drivers.
//
// Input:
//
// Output:
//      EFI_STATUS
//
// Modified:
//
// Referrals:
//
// Notes:
//  Here is the control flow of this function:
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS CpuSpSmiInit(
    IN EFI_HANDLE       ImageHandle,
    IN EFI_SYSTEM_TABLE *SystemTable
 )
{
    //EFI_STATUS Status;

    InitAmiLib(ImageHandle,SystemTable);

    //Status = pBS->LocateProtocol(&SwDispatchProtocolGuid,NULL,&gSwDispatch);
    //if (EFI_ERROR(Status)) return Status;
//InitSmmHandler
    
   // return InitSmmHandler(ImageHandle, SystemTable, InSmmFunction, NonSmmElinkFunctions);
    
    return InitSmmHandler(ImageHandle, SystemTable, InSmmFunction, NULL);

    
}

//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1987-2013, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**         5555 Oakbrook Pkwy, Suite 200, Norcross, GA 30093        **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************