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
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
/** @file
 
This driver is responsible for the registration of child drivers
and the abstraction of the QNC SMI sources.
 
Copyright (c) 2013-2015 Intel Corporation.
 
SPDX-License-Identifier: BSD-2-Clause-Patent
 
**/
 
//
// Include common header file for this module.
//
#include "CommonHeader.h"
 
#include "QNCSmmHelpers.h"
 
//
// Help handle porting bit shifts to IA-64.
//
#define BIT_ZERO 0x00000001
 
 
VOID
QNCSmmPublishDispatchProtocols(
  VOID
  )
{
  UINTN      Index;
  EFI_STATUS Status;
 
  //
  // Install protocol interfaces.
  //
  for (Index = 0; Index < NUM_PROTOCOLS; Index++) {
    Status = gSmst->SmmInstallProtocolInterface (
                 &mPrivateData.InstallMultProtHandle,
                      mPrivateData.Protocols[Index].Guid,
                      EFI_NATIVE_INTERFACE,
                      &mPrivateData.Protocols[Index].Protocols.Generic
                 );
 
  ASSERT_EFI_ERROR (Status);
}
}
 
EFI_STATUS
QNCSmmInitHardware(
  VOID
  )
/*++
 
Routine Description:
 
  Initialize bits that aren't necessarily related to an SMI source.
 
Dependencies:
 
  gSmst - SMM System Table; contains an entry for SMM CPU IO
 
Returns:
 
  EFI_SUCCESS.  Asserts, otherwise.
 
--*/
{
  EFI_STATUS Status;
 
  //
  // Clear all SMIs
  //
  QNCSmmClearSmi();
 
  Status = QNCSmmEnableGlobalSmiBit ();
  ASSERT_EFI_ERROR (Status);
 
  //
  // Be *really* sure to clear all SMIs
  //
  QNCSmmClearSmi ();
 
  return EFI_SUCCESS;
}
 
EFI_STATUS
QNCSmmEnableGlobalSmiBit (
  VOID
  )
/*++
 
Routine Description:
 
  Enables the QNC to generate SMIs. Note that no SMIs will be generated
  if no SMI sources are enabled. Conversely, no enabled SMI source will
  generate SMIs if SMIs are not globally enabled. This is the main
  switchbox for SMI generation.
 
Arguments:
 
  None
 
Returns:
 
  EFI_SUCCESS.
  Asserts, otherwise.
 
--*/
{
  UINT32        NewValue;
 
  //
  // Enable SMI globally
  //
  NewValue = QNCPortRead (QUARK_NC_HOST_BRIDGE_SB_PORT_ID, QNC_MSG_FSBIC_REG_HMISC);
  NewValue |= SMI_EN;
  QNCPortWrite (QUARK_NC_HOST_BRIDGE_SB_PORT_ID, QNC_MSG_FSBIC_REG_HMISC, NewValue);
 
  return EFI_SUCCESS;
}
 
EFI_STATUS
QNCSmmClearSmi(
  VOID
  )
/*++
 
Routine Description:
 
  Clears the SMI after all SMI source have been processed.
  Note that this function will not work correctly (as it is
  written) unless all SMI sources have been processed.
  A revision of this function could manually clear all SMI
  status bits to guarantee success.
 
Returns:
 
  EFI_SUCCESS.
  Asserts, otherwise.
 
--*/
{
  BOOLEAN EosSet;
  BOOLEAN SciEn;
 
  UINT32 Pm1Cnt = 0;
  UINT16 Pm1Sts = 0;
  UINT32 Gpe0Sts = 0;
  UINT32 SmiSts  = 0;
 
  //
  // Determine whether an ACPI OS is present (via the SCI_EN bit)
  //
  Pm1Cnt = IoRead32(PcdGet16 (PcdPm1blkIoBaseAddress) + R_QNC_PM1BLK_PM1C);
  SciEn = (BOOLEAN)((Pm1Cnt & B_QNC_PM1BLK_PM1C_SCIEN) == B_QNC_PM1BLK_PM1C_SCIEN);
 
  if (SciEn == FALSE) {
 
    //
    // Clear any SMIs that double as SCIs (when SCI_EN==0)
    //
    Pm1Sts = (B_QNC_PM1BLK_PM1S_WAKE | B_QNC_PM1BLK_PM1S_PCIEWSTS | B_QNC_PM1BLK_PM1S_RTC | B_QNC_PM1BLK_PM1S_GLOB | B_QNC_PM1BLK_PM1S_TO);
 
    Gpe0Sts = B_QNC_GPE0BLK_GPE0S_ALL;
 
    IoOr16((PcdGet16 (PcdPm1blkIoBaseAddress) + R_QNC_PM1BLK_PM1S), Pm1Sts);
    IoOr32(((UINT16)(LpcPciCfg32 (R_QNC_LPC_GPE0BLK) & 0xFFFF) + R_QNC_GPE0BLK_GPE0S), Gpe0Sts);
  }
 
  //
  // Clear all SMIs that are unaffected by SCI_EN
  //
  SmiSts = IoRead32((UINT16)(LpcPciCfg32 (R_QNC_LPC_GPE0BLK) & 0xFFFF) + R_QNC_GPE0BLK_SMIS);
  SmiSts |= B_QNC_GPE0BLK_SMIS_ALL;
  IoWrite32(((UINT16)(LpcPciCfg32 (R_QNC_LPC_GPE0BLK) & 0xFFFF) + R_QNC_GPE0BLK_SMIS), SmiSts);
 
  //
  // Try to clear the EOS bit. ASSERT on an error
  //
  EosSet = QNCSmmSetAndCheckEos();
  ASSERT (EosSet);
 
  return EFI_SUCCESS;
}
 
BOOLEAN
QNCSmmSetAndCheckEos(
  VOID
  )
{
  //
  // Reset the QNC to generate subsequent SMIs
  //
  IoOr32(((UINT16)(LpcPciCfg32 (R_QNC_LPC_GPE0BLK) & 0xFFFF) + R_QNC_GPE0BLK_SMIS), B_QNC_GPE0BLK_SMIS_EOS);
    return TRUE;
}
 
BOOLEAN
QNCSmmGetSciEn(
  )
{
  BOOLEAN SciEn;
  UINT32 Pm1Cnt;
 
  //
  // Determine whether an ACPI OS is present (via the SCI_EN bit)
  //
  Pm1Cnt = IoRead32(PcdGet16 (PcdPm1blkIoBaseAddress) + R_QNC_PM1BLK_PM1C);
 
  SciEn = (BOOLEAN)((Pm1Cnt & B_QNC_PM1BLK_PM1C_SCIEN) == B_QNC_PM1BLK_PM1C_SCIEN);
 
  return SciEn;
}
 
//
// These may or may not need to change w/ the QNC version; they're highly IA-32 dependent, though.
//
 
BOOLEAN
ReadBitDesc (
  CONST QNC_SMM_BIT_DESC  *BitDesc
  )
{
  UINT64           Register;
  UINT32           PciBus;
  UINT32           PciDev;
  UINT32           PciFun;
  UINT32           PciReg;
  BOOLEAN          BitWasOne;
 
  ASSERT (BitDesc != NULL );
  ASSERT (!IS_BIT_DESC_NULL( *BitDesc ) );
 
  Register  = 0;
  BitWasOne = FALSE;
 
  switch (BitDesc->Reg.Type) {
 
  case ACPI_ADDR_TYPE:
    //
    // Double check that we correctly read in the acpi base address
    //
    ASSERT ((PcdGet16 (PcdPm1blkIoBaseAddress) != 0x0) && ((PcdGet16 (PcdPm1blkIoBaseAddress) & 0x1) != 0x1) );
 
    switch (BitDesc->SizeInBytes) {
 
    case 0:
      //
      // Chances are that this field didn't get initialized.
      // Check your assignments to bit descriptions.
      //
      ASSERT (FALSE );
      break;
 
      case 1:
      Register = (UINT64) IoRead8 (PcdGet16 (PcdPm1blkIoBaseAddress) + BitDesc->Reg.Data.acpi);
        break;
 
      case 2:
      Register = (UINT64) IoRead16 (PcdGet16 (PcdPm1blkIoBaseAddress) + BitDesc->Reg.Data.acpi);
        break;
 
      case 4:
      Register = (UINT64) IoRead32 (PcdGet16 (PcdPm1blkIoBaseAddress) + BitDesc->Reg.Data.acpi);
        break;
 
      default:
        //
        // Unsupported or invalid register size
        //
        ASSERT (FALSE );
        break;
      };
 
    if ((Register & LShiftU64 (BIT_ZERO, BitDesc->Bit)) != 0) {
        BitWasOne = TRUE;
      } else {
        BitWasOne = FALSE;
      }
    break;
 
  case GPE_ADDR_TYPE:
      //
    // Double check that we correctly read in the gpe base address
      //
    ASSERT (((UINT16)(LpcPciCfg32 (R_QNC_LPC_GPE0BLK) & 0xFFFF) != 0x0) && (((UINT16)(LpcPciCfg32 (R_QNC_LPC_GPE0BLK) & 0xFFFF) & 0x1) != 0x1) );
 
    switch (BitDesc->SizeInBytes) {
 
    case 0:
      //
      // Chances are that this field didn't get initialized.
      // Check your assignments to bit descriptions.
      //
      ASSERT (FALSE );
      break;
 
    case 1:
      Register = (UINT64) IoRead8 ((UINT16)(LpcPciCfg32 (R_QNC_LPC_GPE0BLK) & 0xFFFF) + BitDesc->Reg.Data.gpe);
      break;
 
    case 2:
      Register = (UINT64) IoRead16 ((UINT16)(LpcPciCfg32 (R_QNC_LPC_GPE0BLK) & 0xFFFF) + BitDesc->Reg.Data.gpe);
      break;
 
    case 4:
      Register = (UINT64) IoRead32 ((UINT16)(LpcPciCfg32 (R_QNC_LPC_GPE0BLK) & 0xFFFF) + BitDesc->Reg.Data.gpe);
      break;
 
    default:
      //
      // Unsupported or invalid register size
      //
      ASSERT (FALSE );
      break;
    };
 
    if ((Register & LShiftU64 (BIT_ZERO, BitDesc->Bit)) != 0) {
        BitWasOne = TRUE;
      } else {
        BitWasOne = FALSE;
      }
    break;
 
  case MEMORY_MAPPED_IO_ADDRESS_TYPE:
    //
    // Read the register, and it with the bit to read
    //
 
    //
    // This code does not support reads greater then 64 bits
    //
    ASSERT (BitDesc->SizeInBytes <= 8);
    CopyMem (&Register, BitDesc->Reg.Data.Mmio, BitDesc->SizeInBytes);
    Register &= LShiftU64 (BIT0, BitDesc->Bit);
    if (Register) {
      BitWasOne = TRUE;
    } else {
      BitWasOne = FALSE;
    }
    break;
 
  case PCI_ADDR_TYPE:
    PciBus = BitDesc->Reg.Data.pci.Fields.Bus;
    PciDev = BitDesc->Reg.Data.pci.Fields.Dev;
    PciFun = BitDesc->Reg.Data.pci.Fields.Fnc;
    PciReg = BitDesc->Reg.Data.pci.Fields.Reg;
    switch (BitDesc->SizeInBytes) {
 
    case 0:
      //
      // Chances are that this field didn't get initialized.
      // Check your assignments to bit descriptions.
      ASSERT (FALSE );
      break;
 
    case 1:
      Register = (UINT64) PciRead8 (PCI_LIB_ADDRESS (PciBus, PciDev, PciFun, PciReg));
      break;
 
    case 2:
      Register = (UINT64) PciRead16 (PCI_LIB_ADDRESS (PciBus, PciDev, PciFun, PciReg));
      break;
 
    case 4:
      Register = (UINT64) PciRead32 (PCI_LIB_ADDRESS (PciBus, PciDev, PciFun, PciReg));
      break;
 
    default:
      //
      // Unsupported or invalid register size
      //
      ASSERT (FALSE );
      break;
    };
 
    if ((Register & LShiftU64 (BIT_ZERO, BitDesc->Bit)) != 0) {
        BitWasOne = TRUE;
    } else {
      BitWasOne = FALSE;
    }
    break;
 
  default:
    //
    // This address type is not yet implemented
    //
    ASSERT (FALSE );
    break;
  };
 
  return BitWasOne;
}
 
VOID
WriteBitDesc (
  CONST QNC_SMM_BIT_DESC   *BitDesc,
  CONST BOOLEAN           ValueToWrite
  )
{
  UINT64           Register;
  UINT64           AndVal;
  UINT64           OrVal;
  UINT32           PciBus;
  UINT32           PciDev;
  UINT32           PciFun;
  UINT32           PciReg;
 
  ASSERT (BitDesc != NULL);
  ASSERT (!IS_BIT_DESC_NULL(*BitDesc));
 
  AndVal = ~(BIT_ZERO << (BitDesc->Bit));
  OrVal  = ((UINT32)ValueToWrite) << (BitDesc->Bit);
 
  switch (BitDesc->Reg.Type) {
 
  case ACPI_ADDR_TYPE:
    //
    // Double check that we correctly read in the acpi base address
    //
    ASSERT ((PcdGet16 (PcdPm1blkIoBaseAddress) != 0x0) && ((PcdGet16 (PcdPm1blkIoBaseAddress) & 0x1) != 0x1));
 
    switch (BitDesc->SizeInBytes) {
 
    case 0:
      //
      // Chances are that this field didn't get initialized.
      // Check your assignments to bit descriptions.
      //
      ASSERT (FALSE );
      break;
 
    case 1:
      IoAndThenOr8 (PcdGet16 (PcdPm1blkIoBaseAddress) + BitDesc->Reg.Data.acpi, (UINT8)AndVal, (UINT8)OrVal);
      break;
 
    case 2:
      IoAndThenOr16 (PcdGet16 (PcdPm1blkIoBaseAddress) + BitDesc->Reg.Data.acpi, (UINT16)AndVal, (UINT16)OrVal);
      break;
 
    case 4:
      IoAndThenOr32 (PcdGet16 (PcdPm1blkIoBaseAddress) + BitDesc->Reg.Data.acpi, (UINT32)AndVal, (UINT32)OrVal);
      break;
 
    default:
      //
      // Unsupported or invalid register size
      //
      ASSERT (FALSE );
      break;
    };
    break;
 
  case GPE_ADDR_TYPE:
    //
    // Double check that we correctly read in the gpe base address
    //
    ASSERT (((UINT16)(LpcPciCfg32 (R_QNC_LPC_GPE0BLK) & 0xFFFF) != 0x0) && (((UINT16)(LpcPciCfg32 (R_QNC_LPC_GPE0BLK) & 0xFFFF) & 0x1) != 0x1));
 
    switch (BitDesc->SizeInBytes) {
 
    case 0:
      //
      // Chances are that this field didn't get initialized.
      // Check your assignments to bit descriptions.
      //
      ASSERT (FALSE );
      break;
 
    case 1:
      IoAndThenOr8 ((UINT16)(LpcPciCfg32 (R_QNC_LPC_GPE0BLK) & 0xFFFF) + BitDesc->Reg.Data.gpe, (UINT8)AndVal, (UINT8)OrVal);
      break;
 
    case 2:
      IoAndThenOr16 ((UINT16)(LpcPciCfg32 (R_QNC_LPC_GPE0BLK) & 0xFFFF) + BitDesc->Reg.Data.gpe, (UINT16)AndVal, (UINT16)OrVal);
      break;
 
    case 4:
      IoAndThenOr32 ((UINT16)(LpcPciCfg32 (R_QNC_LPC_GPE0BLK) & 0xFFFF) + BitDesc->Reg.Data.gpe, (UINT32)AndVal, (UINT32)OrVal);
      break;
 
    default:
      //
      // Unsupported or invalid register size
      //
      ASSERT (FALSE );
      break;
    };
      break;
 
  case MEMORY_MAPPED_IO_ADDRESS_TYPE:
    //
    // Read the register, or it with the bit to set, then write it back.
    //
 
    //
    // This code does not support writes greater then 64 bits
    //
    ASSERT (BitDesc->SizeInBytes <= 8);
    CopyMem (&Register, BitDesc->Reg.Data.Mmio, BitDesc->SizeInBytes);
    Register &= AndVal;
    Register |= OrVal;
    CopyMem (BitDesc->Reg.Data.Mmio, &Register, BitDesc->SizeInBytes);
    break;
 
  case PCI_ADDR_TYPE:
    PciBus = BitDesc->Reg.Data.pci.Fields.Bus;
    PciDev = BitDesc->Reg.Data.pci.Fields.Dev;
    PciFun = BitDesc->Reg.Data.pci.Fields.Fnc;
    PciReg = BitDesc->Reg.Data.pci.Fields.Reg;
    switch (BitDesc->SizeInBytes) {
 
    case 0:
      //
      // Chances are that this field didn't get initialized -- check your assignments
      // to bit descriptions.
      //
      ASSERT (FALSE );
      break;
 
    case 1:
      PciAndThenOr8 (PCI_LIB_ADDRESS (PciBus, PciDev, PciFun, PciReg), (UINT8) AndVal, (UINT8) OrVal);
      break;
 
    case 2:
      PciAndThenOr16 (PCI_LIB_ADDRESS (PciBus, PciDev, PciFun, PciReg), (UINT16) AndVal, (UINT16) OrVal);
      break;
 
    case 4:
      PciAndThenOr32 (PCI_LIB_ADDRESS (PciBus, PciDev, PciFun, PciReg), (UINT32) AndVal, (UINT32) OrVal);
      break;
 
    default:
      //
      // Unsupported or invalid register size
      //
      ASSERT (FALSE );
      break;
    };
    break;
 
    default:
    //
    // This address type is not yet implemented
    //
    ASSERT (FALSE );
    break;
  };
}