]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Acpi/FirmwarePerformanceDataTableSmm/FirmwarePerformanceCommon.c
MdeModulePkg: Apply uncrustify changes
[mirror_edk2.git] / MdeModulePkg / Universal / Acpi / FirmwarePerformanceDataTableSmm / FirmwarePerformanceCommon.c
1 /** @file
2 This module collects performance data for MM driver boot records and S3 Suspend Performance Record.
3
4 This module registers report status code listener to collect performance data
5 for MM driver boot records and S3 Suspend Performance Record.
6
7 Caution: This module requires additional review when modified.
8 This driver will have external input - communicate buffer in MM mode.
9 This external input must be validated carefully to avoid security issue like
10 buffer overflow, integer overflow.
11
12 FpdtSmiHandler() will receive untrusted input and do basic validation.
13
14 Copyright (c) 2011 - 2021, Intel Corporation. All rights reserved.<BR>
15 SPDX-License-Identifier: BSD-2-Clause-Patent
16
17 **/
18
19 #include <PiMm.h>
20
21 #include <Protocol/MmReportStatusCodeHandler.h>
22
23 #include <Guid/FirmwarePerformance.h>
24
25 #include <Library/MmServicesTableLib.h>
26 #include <Library/BaseLib.h>
27 #include <Library/DebugLib.h>
28 #include <Library/TimerLib.h>
29 #include <Library/LockBoxLib.h>
30 #include <Library/PcdLib.h>
31 #include <Library/BaseMemoryLib.h>
32 #include "FirmwarePerformanceCommon.h"
33
34 EFI_MM_RSC_HANDLER_PROTOCOL *mRscHandlerProtocol = NULL;
35 UINT64 mSuspendStartTime = 0;
36 BOOLEAN mS3SuspendLockBoxSaved = FALSE;
37
38 /**
39 Report status code listener for MM. This is used to record the performance
40 data for S3 Suspend Start and S3 Suspend End in FPDT.
41
42 @param[in] CodeType Indicates the type of status code being reported.
43 @param[in] Value Describes the current status of a hardware or software entity.
44 This included information about the class and subclass that is used to
45 classify the entity as well as an operation.
46 @param[in] Instance The enumeration of a hardware or software entity within
47 the system. Valid instance numbers start with 1.
48 @param[in] CallerId This optional parameter may be used to identify the caller.
49 This parameter allows the status code driver to apply different rules to
50 different callers.
51 @param[in] Data This optional parameter may be used to pass additional data.
52
53 @retval EFI_SUCCESS Status code is what we expected.
54 @retval EFI_UNSUPPORTED Status code not supported.
55
56 **/
57 EFI_STATUS
58 EFIAPI
59 FpdtStatusCodeListenerMm (
60 IN EFI_STATUS_CODE_TYPE CodeType,
61 IN EFI_STATUS_CODE_VALUE Value,
62 IN UINT32 Instance,
63 IN EFI_GUID *CallerId,
64 IN EFI_STATUS_CODE_DATA *Data
65 )
66 {
67 EFI_STATUS Status;
68 UINT64 CurrentTime;
69 EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD S3SuspendRecord;
70
71 //
72 // Check whether status code is what we are interested in.
73 //
74 if ((CodeType & EFI_STATUS_CODE_TYPE_MASK) != EFI_PROGRESS_CODE) {
75 return EFI_UNSUPPORTED;
76 }
77
78 if ((Data != NULL) && CompareGuid (&Data->Type, &gEfiFirmwarePerformanceGuid)) {
79 DEBUG ((DEBUG_ERROR, "FpdtStatusCodeListenerMm: Performance data reported through gEfiFirmwarePerformanceGuid will not be collected by FirmwarePerformanceDataTableMm\n"));
80 return EFI_UNSUPPORTED;
81 }
82
83 if ((Value != PcdGet32 (PcdProgressCodeS3SuspendStart)) &&
84 (Value != PcdGet32 (PcdProgressCodeS3SuspendEnd)))
85 {
86 return EFI_UNSUPPORTED;
87 }
88
89 //
90 // Retrieve current time.
91 //
92 CurrentTime = GetTimeInNanoSecond (GetPerformanceCounter ());
93
94 if (Value == PcdGet32 (PcdProgressCodeS3SuspendStart)) {
95 //
96 // S3 Suspend started, record the performance data and return.
97 //
98 mSuspendStartTime = CurrentTime;
99 return EFI_SUCCESS;
100 }
101
102 //
103 // We are going to S3 sleep, record S3 Suspend End performance data.
104 //
105 S3SuspendRecord.SuspendStart = mSuspendStartTime;
106 S3SuspendRecord.SuspendEnd = CurrentTime;
107
108 //
109 // Save S3 suspend performance data to lock box, it will be used by Firmware Performance PEIM.
110 //
111 if (!mS3SuspendLockBoxSaved) {
112 Status = SaveLockBox (
113 &gEfiFirmwarePerformanceGuid,
114 &S3SuspendRecord,
115 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)
116 );
117 ASSERT_EFI_ERROR (Status);
118
119 mS3SuspendLockBoxSaved = TRUE;
120 } else {
121 Status = UpdateLockBox (
122 &gEfiFirmwarePerformanceGuid,
123 0,
124 &S3SuspendRecord,
125 sizeof (EFI_ACPI_5_0_FPDT_S3_SUSPEND_RECORD)
126 );
127 ASSERT_EFI_ERROR (Status);
128 }
129
130 return EFI_SUCCESS;
131 }
132
133 /**
134 The module Entry Point of the Firmware Performance Data Table MM driver.
135
136 @retval EFI_SUCCESS The entry point is executed successfully.
137 @retval Other Some error occurs when executing this entry point.
138
139 **/
140 EFI_STATUS
141 FirmwarePerformanceCommonEntryPoint (
142 VOID
143 )
144 {
145 EFI_STATUS Status;
146
147 //
148 // Get MM Report Status Code Handler Protocol.
149 //
150 Status = gMmst->MmLocateProtocol (
151 &gEfiMmRscHandlerProtocolGuid,
152 NULL,
153 (VOID **)&mRscHandlerProtocol
154 );
155 ASSERT_EFI_ERROR (Status);
156
157 //
158 // Register report status code listener for BootRecords and S3 Suspend Start and End.
159 //
160 Status = mRscHandlerProtocol->Register (FpdtStatusCodeListenerMm);
161 ASSERT_EFI_ERROR (Status);
162
163 return Status;
164 }