hc
2024-03-22 a0752693d998599af469473b8dc239ef973a012f
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
/** @file
  Helper functions for PCH SMM dispatcher.
 
Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
 
**/
#include "PchSmmHelpers.h"
 
///
/// SUPPORT / HELPER FUNCTIONS (PCH version-independent)
///
 
/**
  Compare 2 SMM source descriptors' enable settings.
 
  @param[in] Src1                 Pointer to the PCH SMI source description table 1
  @param[in] Src2                 Pointer to the PCH SMI source description table 2
 
  @retval TRUE                    The enable settings of the 2 SMM source descriptors are identical.
  @retval FALSE                   The enable settings of the 2 SMM source descriptors are not identical.
**/
BOOLEAN
CompareEnables (
  CONST IN PCH_SMM_SOURCE_DESC *Src1,
  CONST IN PCH_SMM_SOURCE_DESC *Src2
  )
{
  BOOLEAN IsEqual;
  UINTN   DescIndex;
 
  IsEqual = TRUE;
  for (DescIndex = 0; DescIndex < NUM_EN_BITS; DescIndex++) {
    ///
    /// It's okay to compare a NULL bit description to a non-NULL bit description.
    /// They are unequal and these tests will generate the correct result.
    ///
    if (Src1->En[DescIndex].Bit != Src2->En[DescIndex].Bit ||
        Src1->En[DescIndex].Reg.Type != Src2->En[DescIndex].Reg.Type ||
        Src1->En[DescIndex].Reg.Data.raw != Src2->En[DescIndex].Reg.Data.raw
        ) {
      IsEqual = FALSE;
      break;
      ///
      /// out of for loop
      ///
    }
  }
 
  return IsEqual;
}
 
/**
  Compare a bit descriptor to the enables of source descriptor. Includes null address type.
 
  @param[in] BitDesc              Pointer to the PCH SMI bit descriptor
  @param[in] Src                  Pointer to the PCH SMI source description table 2
 
  @retval TRUE                    The bit desc is equal to any of the enables in source descriptor
  @retval FALSE                   The bid desc is not equal to all of the enables in source descriptor
**/
BOOLEAN
IsBitEqualToAnySourceEn (
  CONST IN PCH_SMM_BIT_DESC    *BitDesc,
  CONST IN PCH_SMM_SOURCE_DESC *Src
  )
{
  BOOLEAN IsEqual;
  UINTN   DescIndex;
 
  IsEqual = FALSE;
 
  for (DescIndex = 0; DescIndex < NUM_EN_BITS; ++DescIndex) {
    if ((BitDesc->Reg.Type == Src->En[DescIndex].Reg.Type) &&
        (BitDesc->Reg.Data.raw == Src->En[DescIndex].Reg.Data.raw) &&
        (BitDesc->Bit == Src->En[DescIndex].Bit)) {
      IsEqual = TRUE;
      break;
    }
  }
  return IsEqual;
}
 
/**
  Compare 2 SMM source descriptors' statuses.
 
  @param[in] Src1                 Pointer to the PCH SMI source description table 1
  @param[in] Src2                 Pointer to the PCH SMI source description table 2
 
  @retval TRUE                    The statuses of the 2 SMM source descriptors are identical.
  @retval FALSE                   The statuses of the 2 SMM source descriptors are not identical.
**/
BOOLEAN
CompareStatuses (
  CONST IN PCH_SMM_SOURCE_DESC *Src1,
  CONST IN PCH_SMM_SOURCE_DESC *Src2
  )
{
  BOOLEAN IsEqual;
  UINTN   DescIndex;
 
  IsEqual = TRUE;
 
  for (DescIndex = 0; DescIndex < NUM_STS_BITS; DescIndex++) {
    ///
    /// It's okay to compare a NULL bit description to a non-NULL bit description.
    /// They are unequal and these tests will generate the correct result.
    ///
    if (Src1->Sts[DescIndex].Bit != Src2->Sts[DescIndex].Bit ||
        Src1->Sts[DescIndex].Reg.Type != Src2->Sts[DescIndex].Reg.Type ||
        Src1->Sts[DescIndex].Reg.Data.raw != Src2->Sts[DescIndex].Reg.Data.raw
        ) {
      IsEqual = FALSE;
      break;
      ///
      /// out of for loop
      ///
    }
  }
 
  return IsEqual;
}
 
/**
  Compare 2 SMM source descriptors, based on Enable settings and Status settings of them.
 
  @param[in] Src1                 Pointer to the PCH SMI source description table 1
  @param[in] Src2                 Pointer to the PCH SMI source description table 2
 
  @retval TRUE                    The 2 SMM source descriptors are identical.
  @retval FALSE                   The 2 SMM source descriptors are not identical.
**/
BOOLEAN
CompareSources (
  CONST IN PCH_SMM_SOURCE_DESC *Src1,
  CONST IN PCH_SMM_SOURCE_DESC *Src2
  )
{
  return (BOOLEAN) (CompareEnables (Src1, Src2) && CompareStatuses (Src1, Src2));
}
 
/**
  Check if an SMM source is active.
 
  @param[in] Src                  Pointer to the PCH SMI source description table
  @param[in] SciEn                Indicate if SCI is enabled or not
  @param[in] SmiEnValue           Value from R_PCH_SMI_EN
  @param[in] SmiStsValue          Value from R_PCH_SMI_STS
 
  @retval TRUE                    It is active.
  @retval FALSE                   It is inactive.
**/
BOOLEAN
SourceIsActive (
  CONST IN PCH_SMM_SOURCE_DESC  *Src,
  CONST IN BOOLEAN              SciEn,
  CONST IN UINT32               SmiEnValue,
  CONST IN UINT32               SmiStsValue
  )
{
  UINTN   DescIndex;
 
  ///
  /// This source is dependent on SciEn, and SciEn == 1.  An ACPI OS is present,
  /// so we shouldn't do anything w/ this source until SciEn == 0.
  ///
  if ((Src->Flags == PCH_SMM_SCI_EN_DEPENDENT) && (SciEn)) {
    return FALSE;
  }
 
  ///
  /// Checking top level SMI status. If the status is not active, return false immediately
  ///
  if (!IS_BIT_DESC_NULL (Src->PmcSmiSts)) {
    if ((Src->PmcSmiSts.Reg.Type == ACPI_ADDR_TYPE) &&
        (Src->PmcSmiSts.Reg.Data.acpi == R_PCH_SMI_STS) &&
        ((SmiStsValue & (1u << Src->PmcSmiSts.Bit)) == 0)) {
      return FALSE;
    }
  }
 
  ///
  /// Read each bit desc from hardware and make sure it's a one
  ///
  for (DescIndex = 0; DescIndex < NUM_EN_BITS; DescIndex++) {
    if (!IS_BIT_DESC_NULL (Src->En[DescIndex])) {
      if ((Src->En[DescIndex].Reg.Type == ACPI_ADDR_TYPE) &&
          (Src->En[DescIndex].Reg.Data.acpi == R_PCH_SMI_EN) &&
          ((SmiEnValue & (1u << Src->En[DescIndex].Bit)) == 0)) {
        return FALSE;
      } else if (ReadBitDesc (&Src->En[DescIndex]) == 0) {
        return FALSE;
      }
    }
  }
 
  ///
  /// Read each bit desc from hardware and make sure it's a one
  ///
  for (DescIndex = 0; DescIndex < NUM_STS_BITS; DescIndex++) {
    if (!IS_BIT_DESC_NULL (Src->Sts[DescIndex])) {
      if ((Src->Sts[DescIndex].Reg.Type == ACPI_ADDR_TYPE) &&
          (Src->Sts[DescIndex].Reg.Data.acpi == R_PCH_SMI_STS) &&
          ((SmiStsValue & (1u << Src->Sts[DescIndex].Bit)) == 0)) {
        return FALSE;
      } else if (ReadBitDesc (&Src->Sts[DescIndex]) == 0) {
        return FALSE;
      }
    }
  }
 
  return TRUE;
}
 
/**
  Enable the SMI source event by set the SMI enable bit, this function would also clear SMI
  status bit to make initial state is correct
 
  @param[in] SrcDesc              Pointer to the PCH SMI source description table
 
**/
VOID
PchSmmEnableSource (
  CONST PCH_SMM_SOURCE_DESC *SrcDesc
  )
{
  UINTN DescIndex;
 
  ///
  /// Set enables to 1 by writing a 1
  ///
  for (DescIndex = 0; DescIndex < NUM_EN_BITS; DescIndex++) {
    if (!IS_BIT_DESC_NULL (SrcDesc->En[DescIndex])) {
      WriteBitDesc (&SrcDesc->En[DescIndex], 1, FALSE);
    }
  }
  ///
  /// Clear statuses to 0 by writing a 1
  ///
  for (DescIndex = 0; DescIndex < NUM_STS_BITS; DescIndex++) {
    if (!IS_BIT_DESC_NULL (SrcDesc->Sts[DescIndex])) {
      WriteBitDesc (&SrcDesc->Sts[DescIndex], 1, TRUE);
    }
  }
}
 
/**
  Disable the SMI source event by clear the SMI enable bit
 
  @param[in] SrcDesc              Pointer to the PCH SMI source description table
 
**/
VOID
PchSmmDisableSource (
  CONST PCH_SMM_SOURCE_DESC *SrcDesc
  )
{
  UINTN DescIndex;
 
  for (DescIndex = 0; DescIndex < NUM_EN_BITS; DescIndex++) {
    if (!IS_BIT_DESC_NULL (SrcDesc->En[DescIndex])) {
      WriteBitDesc (&SrcDesc->En[DescIndex], 0, FALSE);
    }
  }
}
 
/**
  Clear the SMI status bit by set the source bit of SMI status register
 
  @param[in] SrcDesc              Pointer to the PCH SMI source description table
 
**/
VOID
PchSmmClearSource (
  CONST PCH_SMM_SOURCE_DESC *SrcDesc
  )
{
  UINTN DescIndex;
 
  for (DescIndex = 0; DescIndex < NUM_STS_BITS; DescIndex++) {
    if (!IS_BIT_DESC_NULL (SrcDesc->Sts[DescIndex])) {
      WriteBitDesc (&SrcDesc->Sts[DescIndex], 1, TRUE);
    }
  }
}
 
/**
  Sets the source to a 1 and then waits for it to clear.
  Be very careful when calling this function -- it will not
  ASSERT.  An acceptable case to call the function is when
  waiting for the NEWCENTURY_STS bit to clear (which takes
  3 RTCCLKs).
 
  @param[in] SrcDesc              Pointer to the PCH SMI source description table
 
**/
VOID
PchSmmClearSourceAndBlock (
  CONST PCH_SMM_SOURCE_DESC *SrcDesc
  )
{
  UINTN   DescIndex;
  BOOLEAN IsSet;
 
  for (DescIndex = 0; DescIndex < NUM_STS_BITS; DescIndex++) {
 
    if (!IS_BIT_DESC_NULL (SrcDesc->Sts[DescIndex])) {
      ///
      /// Write the bit
      ///
      WriteBitDesc (&SrcDesc->Sts[DescIndex], 1, TRUE);
 
      ///
      /// Don't return until the bit actually clears.
      ///
      IsSet = TRUE;
      while (IsSet) {
        IsSet = ReadBitDesc (&SrcDesc->Sts[DescIndex]);
        ///
        /// IsSet will eventually clear -- or else we'll have
        /// an infinite loop.
        ///
      }
    }
  }
}