summaryrefslogtreecommitdiff
path: root/Chipset/SB/Smm2/SmmChildDispatch2Main.c
blob: bae5efcf101fb69428b2e0abae2cc46f85915f99 (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
#pragma optimize ("", off)
//*************************************************************************
//*************************************************************************
//**                                                                     **
//**        (C)Copyright 1985-2011, American Megatrends, Inc.            **
//**                                                                     **
//**                       All Rights Reserved.                          **
//**                                                                     **
//**      5555 Oakbrook Parkway, Suite 200, Norcross, GA 30093           **
//**                                                                     **
//**                       Phone: (770)-246-8600                         **
//**                                                                     **
//*************************************************************************
//*************************************************************************

//*************************************************************************
// $Header: /Alaska/BIN/Chipset/Intel/SouthBridge/LynxPoint/Intel Pch SB Chipset/SmmChildDispatcher2/SmmChildDispatch2Main.c 4     10/19/12 2:52a Scottyang $
//
// $Revision: 4 $
//
// $Date: 10/19/12 2:52a $
//*************************************************************************
// Revision History
// ----------------
// $Log: /Alaska/BIN/Chipset/Intel/SouthBridge/LynxPoint/Intel Pch SB Chipset/SmmChildDispatcher2/SmmChildDispatch2Main.c $
// 
// 4     10/19/12 2:52a Scottyang
// [TAG]       EIP93461
// [Category]  Bug Fix
// [Severity]  Normal
// [Symptom]   System halt when AFUDOS is running with /N /ME command.
// [RootCause] An unexpected BIOSWR_STS is set, it causes BIOS stuck
//             at SMM dispatcher.
// [Solution]  Clear BIOSWR_STS if BIOS Lock Enable is not set.
// [Files]     SmiHandlerPorting2.c; SmmChildDispatch2Main.c
//             SmmChildDispatcher2.sdl; SmmChildDispatch2.h
//             SB\SBGeneric.c
// 
// 3     8/30/12 9:50a Victortu
// [TAG]           None
// [Category]      Improvement
// [Description]   Implement EIP#73211 and EIP#79156 for OA 3.0 function.
// [Files]         SmiHandlerGeneric2.c, SmmChildDispatch2Main.c
// 
// 2     4/25/12 9:32a Victortu
// [TAG]         None
// [Category]    Improvement
// [Description] Reprogram SMM ChildDispatcher drivers.
// [Files]       SmiHandlerGeneric.c; SmiHandlerPorting.c;
// SmiHandlerGeneric2.c; SmmChildDispatch2Main.c; SmmChildDispatcher2.mak;
// SmmChildDispatcher2.sdl; SmmChildDispatch.h; SmmChildDispatchMain.c;
// SmmChildDispatchProtocol.c; SmmChildDispatcher.dxs;
// PchSmiDispatcher.sdl
// 
// 1     2/08/12 8:28a Yurenlai
// Intel Lynx Point/SB eChipset initially releases.
// 
//*************************************************************************
//<AMI_FHDR_START>
//
// Name:        SmmChildDispatch2Main.c
//
// Description: This file contains implementation of module entry point,
//              generic RegisterHandler and UnregisterHandler routines 
//              and main dispatcher loop.
//
//<AMI_FHDR_END>
//*************************************************************************

//---------------------------------------------------------------------------
// Include(s)
//---------------------------------------------------------------------------

#include <AmiCspLib.h>
#include <Smm.h>
#include <Protocol\SmmBase2.h>
#include <Protocol\SmmCpu.h>
#include "SmmChildDispatch2.h"
#include "SmmChildDispatch2Protocol.h"

//---------------------------------------------------------------------------
// Constant, Macro and Type Definition(s)
//---------------------------------------------------------------------------
// Constant Definition(s)

#define SMI_HANDLER_SIGNATURE 0x48494d53    //SMIH

// Macro Definition(s)

// Type Definition(s)

// Function Prototype(s)

//---------------------------------------------------------------------------
// Variable and External Declaration(s)
//---------------------------------------------------------------------------
// Variable Declaration(s)

EFI_SMM_SMI_CONTEXT2    SmiContext;
EFI_SMM_BASE2_PROTOCOL  *gSmmBase2;
EFI_SMM_SYSTEM_TABLE2   *gSmst2;
EFI_SMM_CPU_PROTOCOL    *gEfiSmmCpuProtocol;

// GUID Definition(s)

EFI_GUID gEfiSmmChildDispatcher2Guid = SMM_CHILD_DISPATCHER2_GUID;

// Protocol Definition(s)

// External Declaration(s)

extern SMM_CHILD_DISPATCHER2 Smm2Handler[];

extern EFI_SMM_SW_DISPATCH2_PROTOCOL gEfiSmmSwDispatch2Protocol;
extern EFI_SMM_SX_DISPATCH2_PROTOCOL gEfiSmmSxDispatch2Protocol;
extern EFI_SMM_PERIODIC_TIMER_DISPATCH2_PROTOCOL gEfiSmmPeriodicTimerDispatch2Protocol;
extern EFI_SMM_USB_DISPATCH2_PROTOCOL gEfiSmmUsbDispatch2Protocol;
extern EFI_SMM_GPI_DISPATCH2_PROTOCOL gEfiSmmGpiDispatch2Protocol;
extern EFI_SMM_STANDBY_BUTTON_DISPATCH2_PROTOCOL gEfiSmmStandbyButtonDispatch2Protocol;
extern EFI_SMM_POWER_BUTTON_DISPATCH2_PROTOCOL gEfiSmmPowerButonDispatch2Protocol;
extern EFI_SMM_IO_TRAP_DISPATCH2_PROTOCOL gEfiSmmIoTrapDispatch2Protocol;

// Function Definition(s)

//---------------------------------------------------------------------------

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   RegisterHandler2
//
// Description: This function registers SMI II handler and returns registered
//              handle 
//
// Input:       Type        - Type of SMI handler
//              Function    - Pointer to callback function
//              *Context    - Pointer to callback context
//              ContextSize - Callback context size
//              *Handle     - Pointer to store registered handle
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS RegisterHandler2 (
    IN EFI_SMM_SMI2                 Type,
    IN SMI_GENERIC_CALLBACK2        Function,
    IN VOID                         *Context,
    IN UINTN                        ContextSize,
    OUT EFI_HANDLE                  *Handle )
{
    EFI_STATUS      Status;
    HANDLER_LINK2   *HandlerLink;

    Status = Smm2Handler[Type].HandlerProtocol.VerifyContext( Context );
    if(EFI_ERROR(Status)) return Status;

    Status = gSmst2->SmmAllocatePool( 0,
                                     sizeof(HANDLER_LINK2) + ContextSize - 1,
                                     &HandlerLink );
    if(EFI_ERROR(Status)) return Status;

    HandlerLink->Callback = Function;
    HandlerLink->Signature = SMI_HANDLER_SIGNATURE;
    MemCpy( HandlerLink->Context, Context, ContextSize );

    DListAdd(&(Smm2Handler[Type].RegisteredCallbacks), (DLINK *)HandlerLink);

    Smm2Handler[Type].HandlerProtocol.AddHandler( HandlerLink->Context );
    *Handle = HandlerLink;

    return EFI_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   FindHandler
//
// Description: This function find the handle in the Type of SMI hander list.
//
// Input:       Type   - Type of SMI handler
//              Handle - Handle of found handler
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS FindHandler(
    IN EFI_SMM_SMI2 Type,
    IN EFI_HANDLE Handle)
{
    HANDLER_LINK2 *HandlerLink = (HANDLER_LINK2 *)Handle;
    HANDLER_LINK2 *NodeLink = NULL;

    if (HandlerLink->Signature != SMI_HANDLER_SIGNATURE)
        return EFI_INVALID_PARAMETER;

    if (Smm2Handler[Type].RegisteredCallbacks.Size <= 0)
        return EFI_UNSUPPORTED;

    NodeLink = (HANDLER_LINK2 *)Smm2Handler[Type].RegisteredCallbacks.pHead;
    for (; NodeLink != NULL; NodeLink = (HANDLER_LINK2 *)NodeLink->Link.pNext)
    {
        if (NodeLink == HandlerLink)
            return EFI_SUCCESS;
    }

    return EFI_UNSUPPORTED;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   UnregisterHandler2
//
// Description: This function unregisters SMI II handler with given handle
//
// Input:       Type   - Type of SMI handler
//              Handle - Handle of registered handler
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS UnregisterHandler2 (
    IN EFI_SMM_SMI2         Type,
    IN EFI_HANDLE           Handle )
{
    EFI_STATUS Status;
    HANDLER_LINK2 *HandlerLink = (HANDLER_LINK2 *)Handle;

    Status = FindHandler(Type, Handle);
    if (Status == EFI_SUCCESS)
    {
        Smm2Handler[Type].HandlerProtocol.RemoveHandler( HandlerLink->Context );
        DListDelete( &(Smm2Handler[Type].RegisteredCallbacks), (DLINK *)HandlerLink );
        gSmst2->SmmFreePool( HandlerLink );
    }

    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   ChildDispatcher2
//
// Description: This function implements main SMI II dispatcher.
//
// Input:       DispatchHandle  - SMM dispatch handle
//              *Context        - Pointer to the dispatched context
//              CommBuffer      - Pointer to a collection of data in memory
//                                that will be conveyed from a non-SMM 
//                                environment into an SMM environment.
//              CommBufferSize  - Pointer to the size of the CommBuffer
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS ChildDispatcher2 (
    IN EFI_HANDLE           DispatchHandle,
    IN CONST VOID           *Context OPTIONAL,
    IN OUT VOID             *CommBuffer OPTIONAL,
    IN OUT UINTN            *CommBufferSize OPTIONAL )
{
    EFI_STATUS  Status = EFI_UNSUPPORTED;
    UINT32      Index;
    BOOLEAN     HandledSmi;

    do
    {
        HandledSmi = FALSE;
        for (Index = EfiSmmSwSmi2; Index < EfiSmmMaxSmi2; Index++)
        {
            if (Smm2Handler[Index].HandlerProtocol.GetContext != 0 && 
                Smm2Handler[Index].HandlerProtocol.GetContext())
            {
                Status = Smm2Handler[Index].HandlerProtocol.DispatchSmi();
                if (Status != EFI_WARN_INTERRUPT_SOURCE_QUIESCED)
                {
                    HandledSmi = TRUE;
                }
            }
        }
    } while (HandledSmi);
                                        // [EIP93461]>
    SbSmiWorkaround2();
                                        // <[EIP93461]
    return EFI_HANDLER_SUCCESS;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   InSmmFunction
//
// Description: This function initializes Child dispatcher in SMM mode
//
// Input:       ImageHandle  - Image handle
//              *SystemTable - Pointer to system table
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS InSmmFunction ( 
    IN EFI_HANDLE           ImageHandle,
    IN EFI_SYSTEM_TABLE     *SystemTable )
{
    EFI_STATUS      Status;
    EFI_HANDLE      Handle = NULL;
    EFI_HANDLE      BsHandle = NULL;
    EFI_HANDLE      RootHandle;

#if (CHILD_DISPATCHER_SUPPORT == 0)
    ClearAllSmi();
#endif

    // Locate SMM CPU Protocols
    Status = gSmst2->SmmLocateProtocol( &gEfiCpuProtocolGuid,
                                        NULL,
                                        &gEfiSmmCpuProtocol);

    // Install Protocols
    Status = gSmst2->SmmInstallProtocolInterface(
                                    &Handle,
                                    &gEfiSmmSwDispatch2ProtocolGuid,
                                    EFI_NATIVE_INTERFACE,
                                    &gEfiSmmSwDispatch2Protocol);
    ASSERT_EFI_ERROR(Status);

    Status = pBS->InstallProtocolInterface(
                                    &BsHandle,
                                    &gEfiSmmSwDispatch2ProtocolGuid,
                                    EFI_NATIVE_INTERFACE,
                                    NULL);
    ASSERT_EFI_ERROR(Status);

    Status = gSmst2->SmmInstallProtocolInterface(
                                    &Handle,
                                    &gEfiSmmSxDispatch2ProtocolGuid,
                                    EFI_NATIVE_INTERFACE,
                                    &gEfiSmmSxDispatch2Protocol);
    ASSERT_EFI_ERROR(Status);

    Status = pBS->InstallProtocolInterface(
                                    &BsHandle,
                                    &gEfiSmmSxDispatch2ProtocolGuid,
                                    EFI_NATIVE_INTERFACE,
                                    NULL);
    ASSERT_EFI_ERROR(Status);

    Status = gSmst2->SmmInstallProtocolInterface(
                                    &Handle,
                                    &gEfiSmmPeriodicTimerDispatch2ProtocolGuid,
                                    EFI_NATIVE_INTERFACE,
                                    &gEfiSmmPeriodicTimerDispatch2Protocol);
    ASSERT_EFI_ERROR(Status);

    Status = pBS->InstallProtocolInterface(
                                    &BsHandle,
                                    &gEfiSmmPeriodicTimerDispatch2ProtocolGuid,
                                    EFI_NATIVE_INTERFACE,
                                    NULL);
    ASSERT_EFI_ERROR(Status);

    Status = gSmst2->SmmInstallProtocolInterface(
                                    &Handle,
                                    &gEfiSmmUsbDispatch2ProtocolGuid,
                                    EFI_NATIVE_INTERFACE,
                                    &gEfiSmmUsbDispatch2Protocol);
    ASSERT_EFI_ERROR(Status);

    Status = pBS->InstallProtocolInterface(
                                    &BsHandle,
                                    &gEfiSmmUsbDispatch2ProtocolGuid,
                                    EFI_NATIVE_INTERFACE,
                                    NULL);
    ASSERT_EFI_ERROR(Status);

    Status = gSmst2->SmmInstallProtocolInterface(
                                    &Handle,
                                    &gEfiSmmGpiDispatch2ProtocolGuid,
                                    EFI_NATIVE_INTERFACE,
                                    &gEfiSmmGpiDispatch2Protocol);
    ASSERT_EFI_ERROR(Status);

    Status = pBS->InstallProtocolInterface(
                                    &BsHandle,
                                    &gEfiSmmGpiDispatch2ProtocolGuid,
                                    EFI_NATIVE_INTERFACE,
                                    NULL);
    ASSERT_EFI_ERROR(Status);

    Status = gSmst2->SmmInstallProtocolInterface(
                                    &Handle,
                                    &gEfiSmmStandbyButtonDispatch2ProtocolGuid,
                                    EFI_NATIVE_INTERFACE,
                                    &gEfiSmmStandbyButtonDispatch2Protocol);
    ASSERT_EFI_ERROR(Status);

    Status = pBS->InstallProtocolInterface(
                                    &BsHandle,
                                    &gEfiSmmStandbyButtonDispatch2ProtocolGuid,
                                    EFI_NATIVE_INTERFACE,
                                    NULL);
    ASSERT_EFI_ERROR(Status);

    Status = gSmst2->SmmInstallProtocolInterface(
                                    &Handle,
                                    &gEfiSmmPowerButtonDispatch2ProtocolGuid,
                                    EFI_NATIVE_INTERFACE,
                                    &gEfiSmmPowerButonDispatch2Protocol);
    ASSERT_EFI_ERROR(Status);

    Status = pBS->InstallProtocolInterface(
                                    &BsHandle,
                                    &gEfiSmmPowerButtonDispatch2ProtocolGuid,
                                    EFI_NATIVE_INTERFACE,
                                    NULL);
    ASSERT_EFI_ERROR(Status);

    Status = gSmst2->SmmInstallProtocolInterface(
                                    &Handle,
                                    &gEfiSmmIoTrapDispatch2ProtocolGuid,
                                    EFI_NATIVE_INTERFACE,
                                    &gEfiSmmIoTrapDispatch2Protocol);
    ASSERT_EFI_ERROR(Status);

    Status = pBS->InstallProtocolInterface(
                                    &BsHandle,
                                    &gEfiSmmIoTrapDispatch2ProtocolGuid,
                                    EFI_NATIVE_INTERFACE,
                                    NULL);
    ASSERT_EFI_ERROR(Status);

    Status = gSmst2->SmiHandlerRegister(ChildDispatcher2, NULL, &RootHandle);

    return Status;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
//
// Procedure:   SmmChildDispatch2EntryPoint
//
// Description: SMM Child Dispatcher II module entry point
//
// Input:       ImageHandle  - Image handle
//              *SystemTable - Pointer to system table
//
// Output:      EFI_STATUS
//----------------------------------------------------------------------------
//<AMI_PHDR_END>

EFI_STATUS SmmChildDispatch2EntryPoint (
    IN EFI_HANDLE               ImageHandle,
    IN EFI_SYSTEM_TABLE         *SystemTable )
{
    EFI_STATUS Status;
    BOOLEAN InSmram = FALSE;

    InitAmiLib( ImageHandle, SystemTable );

    Status = pBS->LocateProtocol(&gEfiSmmBase2ProtocolGuid, NULL, &gSmmBase2);
    if (!EFI_ERROR(Status))
    {
        Status = gSmmBase2->InSmm(gSmmBase2, &InSmram);
        if ((!EFI_ERROR(Status)) &&
            (InSmram))
        {
            Status = InitAmiSmmLib( ImageHandle, SystemTable );
            if (EFI_ERROR(Status))
                return Status;

            Status = gSmmBase2->GetSmstLocation(gSmmBase2, &gSmst2);
            if (!EFI_ERROR(Status))
            {
                Status = InSmmFunction(ImageHandle, SystemTable);
                return Status;
            }
            else
            {
                gSmst2 = NULL;
            }
        }
        else
        {
            // DXE initialize.
        }
    }

    return EFI_SUCCESS;
}

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