summaryrefslogtreecommitdiff
path: root/DuetPkg/DxeIpl/LegacyTable.c
blob: 823a25ac5a472d9171e4c0088a9a9f7a55bb1e5f (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
/** @file

Copyright (c) 2006, Intel Corporation                                                         
All rights reserved. This program and the accompanying materials                          
are licensed and made available under the terms and conditions of the BSD License         
which accompanies this distribution.  The full text of the license may be found at        
http://opensource.org/licenses/bsd-license.php                                            
                                                                                          
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             

Module Name:
  LegacyTable.c

Abstract:

Revision History:

**/

#include "DxeIpl.h"
#include "HobGeneration.h"
#include "Debug.h"

#define ACPI_RSD_PTR      0x2052545020445352LL
#define MPS_PTR           SIGNATURE_32('_','M','P','_')
#define SMBIOS_PTR        SIGNATURE_32('_','S','M','_')

#define EBDA_BASE_ADDRESS 0x40E

VOID *
FindAcpiRsdPtr (
  VOID
  )
{
  UINTN                           Address;
  UINTN                           Index;

  //
  // First Seach 0x0e0000 - 0x0fffff for RSD Ptr
  //
  for (Address = 0xe0000; Address < 0xfffff; Address += 0x10) {
    if (*(UINT64 *)(Address) == ACPI_RSD_PTR) {
      return (VOID *)Address;
    }
  }

  //
  // Search EBDA
  //

  Address = (*(UINT16 *)(UINTN)(EBDA_BASE_ADDRESS)) << 4;
  for (Index = 0; Index < 0x400 ; Index += 16) {
    if (*(UINT64 *)(Address + Index) == ACPI_RSD_PTR) {
      return (VOID *)Address;
    }
  }
  return NULL;
}

VOID *
FindSMBIOSPtr (
  VOID
  )
{
  UINTN                           Address;

  //
  // First Seach 0x0f0000 - 0x0fffff for SMBIOS Ptr
  //
  for (Address = 0xf0000; Address < 0xfffff; Address += 0x10) {
    if (*(UINT32 *)(Address) == SMBIOS_PTR) {
      return (VOID *)Address;
    }
  }
  return NULL;
}

VOID *
FindMPSPtr (
  VOID
  )
{
  UINTN                           Address;
  UINTN                           Index;

  //
  // First Seach 0x0e0000 - 0x0fffff for MPS Ptr
  //
  for (Address = 0xe0000; Address < 0xfffff; Address += 0x10) {
    if (*(UINT32 *)(Address) == MPS_PTR) {
      return (VOID *)Address;
    }
  }

  //
  // Search EBDA
  //

  Address = (*(UINT16 *)(UINTN)(EBDA_BASE_ADDRESS)) << 4;
  for (Index = 0; Index < 0x400 ; Index += 16) {
    if (*(UINT32 *)(Address + Index) == MPS_PTR) {
      return (VOID *)Address;
    }
  }
  return NULL;
}

#pragma pack(1)
typedef struct {
  UINT8           Signature[8];
  UINT8           Checksum;
  UINT8           OemId[6];
  UINT8           Revision;
  UINT32          RsdtAddress;
  UINT32          Length;
  UINT64          XsdtAddress;
  UINT8           ExtendedChecksum;
  UINT8           Reserved[3];
} RSDP_TABLE;

typedef struct {
  UINT32          Signature;
  UINT32          Length;
  UINT8           Revision;
  UINT8           Checksum;
  UINT8           OemId[6];
  UINT8           OemTableId[8];
  UINT32          OemRevision;
  UINT8           CreatorId[4];
  UINT32          CreatorRevision;
} DESCRIPTION_HEADER;

typedef struct {
  DESCRIPTION_HEADER    Header;
  UINT32                Entry;
} RSDT_TABLE;

typedef struct {
  DESCRIPTION_HEADER    Header;
  UINT64                Entry;
} XSDT_TABLE;

typedef struct {
  UINT8                 Address_Space_ID;
  UINT8                 Register_Bit_Width;
  UINT8                 Register_Bit_Offset;
  UINT8                 Access_Size;
  UINT64                Address;
} GADDRESS_STRUCTURE;

#pragma pack()

VOID
ScanTableInRSDT (
  RSDT_TABLE            *Rsdt,
  UINT32                Signature,
  DESCRIPTION_HEADER    **FoundTable
  )
{
  UINTN                     Index;
  UINT32                    EntryCount;
  UINT32                    *EntryPtr;
  DESCRIPTION_HEADER        *Table;
  
  *FoundTable = NULL;
  
  EntryCount = (Rsdt->Header.Length - sizeof (DESCRIPTION_HEADER)) / sizeof(UINT32);
  
  EntryPtr = &Rsdt->Entry;
  for (Index = 0; Index < EntryCount; Index ++, EntryPtr ++) {
    Table = (DESCRIPTION_HEADER*)((UINTN)(*EntryPtr));
    if (Table->Signature == Signature) {
      *FoundTable = Table;
      break;
    }
  }
  
  return;
}

VOID
ScanTableInXSDT (
  XSDT_TABLE            *Xsdt,
  UINT32                Signature,
  DESCRIPTION_HEADER    **FoundTable
  )
{
  UINTN         Index;
  UINT32        EntryCount;
  UINT64        EntryPtr;
  UINTN         BasePtr;
  
  DESCRIPTION_HEADER    *Table;
  
  *FoundTable = NULL;
  
  EntryCount = (Xsdt->Header.Length - sizeof (DESCRIPTION_HEADER)) / sizeof(UINT64);
  
  BasePtr = (UINTN)(&(Xsdt->Entry));
  for (Index = 0; Index < EntryCount; Index ++) {
    CopyMem (&EntryPtr, (VOID *)(BasePtr + Index * sizeof(UINT64)), sizeof(UINT64));
    Table = (DESCRIPTION_HEADER*)((UINTN)(EntryPtr));
    if (Table->Signature == Signature) {
      *FoundTable = Table;
      break;
    }
  }
  
  return;
}

VOID *
FindAcpiPtr (
  IN HOB_TEMPLATE  *Hob,
  UINT32           Signature
  )
{
  DESCRIPTION_HEADER    *AcpiTable;
  RSDP_TABLE            *Rsdp;
  RSDT_TABLE            *Rsdt;
  XSDT_TABLE            *Xsdt;
 
  AcpiTable = NULL;

  //
  // Check ACPI2.0 table
  //
  if ((int)Hob->Acpi20.Table != -1) {
    Rsdp = (RSDP_TABLE *)(UINTN)Hob->Acpi20.Table;
    Rsdt = (RSDT_TABLE *)(UINTN)Rsdp->RsdtAddress;
    Xsdt = NULL;
    if ((Rsdp->Revision >= 2) && (Rsdp->XsdtAddress < (UINT64)(UINTN)-1)) {
      Xsdt = (XSDT_TABLE *)(UINTN)Rsdp->XsdtAddress;
    }
    //
    // Check Xsdt
    //
    if (Xsdt != NULL) {
      ScanTableInXSDT (Xsdt, Signature, &AcpiTable);
    }
    //
    // Check Rsdt
    //
    if ((AcpiTable == NULL) && (Rsdt != NULL)) {
      ScanTableInRSDT (Rsdt, Signature, &AcpiTable);
    }
  }
  
  //
  // Check ACPI1.0 table
  //
  if ((AcpiTable == NULL) && ((int)Hob->Acpi.Table != -1)) {
    Rsdp = (RSDP_TABLE *)(UINTN)Hob->Acpi.Table;
    Rsdt = (RSDT_TABLE *)(UINTN)Rsdp->RsdtAddress;
    //
    // Check Rsdt
    //
    if (Rsdt != NULL) {
      ScanTableInRSDT (Rsdt, Signature, &AcpiTable);
    }
  }

  return AcpiTable;
}

#pragma pack(1)
//#define MCFG_SIGNATURE  0x4746434D
#define MCFG_SIGNATURE SIGNATURE_32 ('M', 'C', 'F', 'G')
typedef struct {
  UINT64  BaseAddress;
  UINT16  PciSegmentGroupNumber;
  UINT8   StartBusNumber;
  UINT8   EndBusNumber;
  UINT32  Reserved;
} MCFG_STRUCTURE;

#define FADT_SIGNATURE SIGNATURE_32 ('F', 'A', 'C', 'P')
typedef struct {
  DESCRIPTION_HEADER    Header;
  UINT32                FIRMWARE_CTRL;
  UINT32                DSDT;
  UINT8                 INT_MODEL;
  UINT8                 Preferred_PM_Profile;
  UINT16                SCI_INIT;
  UINT32                SMI_CMD;
  UINT8                 ACPI_ENABLE;
  UINT8                 ACPI_DISABLE;
  UINT8                 S4BIOS_REQ;
  UINT8                 PSTATE_CNT;
  UINT32                PM1a_EVT_BLK;
  UINT32                PM1b_EVT_BLK;
  UINT32                PM1a_CNT_BLK;
  UINT32                PM1b_CNT_BLK;
  UINT32                PM2_CNT_BLK;
  UINT32                PM_TMR_BLK;
  UINT32                GPE0_BLK;
  UINT32                GPE1_BLK;
  UINT8                 PM1_EVT_LEN;
  UINT8                 PM1_CNT_LEN;
  UINT8                 PM2_CNT_LEN;
  UINT8                 PM_TMR_LEN;
  UINT8                 GPE0_BLK_LEN;
  UINT8                 GPE1_BLK_LEN;
  UINT8                 GPE1_BASE;
  UINT8                 CST_CNT;
  UINT16                P_LVL2_LAT;
  UINT16                P_LVL3_LAT;
  UINT16                FLUSH_SIZE;
  UINT16                FLUSH_STRIDE;
  UINT8                 DUTY_OFFSET;
  UINT8                 DUTY_WIDTH;
  UINT8                 DAY_ALARM;
  UINT8                 MON_ALARM;
  UINT8                 CENTRY;
  UINT16                IAPC_BOOT_ARCH;
  UINT8                 Reserved_111;
  UINT32                Flags;
  GADDRESS_STRUCTURE    RESET_REG;
  UINT8                 RESET_VALUE;
  UINT8                 Reserved_129[3];
  UINT64                X_FIRMWARE_CTRL;
  UINT64                X_DSDT;
  GADDRESS_STRUCTURE    X_PM1a_EVT_BLK;
  GADDRESS_STRUCTURE    X_PM1b_EVT_BLK;
  GADDRESS_STRUCTURE    X_PM1a_CNT_BLK;
  GADDRESS_STRUCTURE    X_PM1b_CNT_BLK;
  GADDRESS_STRUCTURE    X_PM2_CNT_BLK;
  GADDRESS_STRUCTURE    X_PM_TMR_BLK;
  GADDRESS_STRUCTURE    X_GPE0_BLK;
  GADDRESS_STRUCTURE    X_GPE1_BLK;
} FADT_TABLE;

#pragma pack()

VOID
PrepareMcfgTable (
  IN HOB_TEMPLATE  *Hob
  )
{
  DESCRIPTION_HEADER    *McfgTable;
  MCFG_STRUCTURE        *Mcfg;
  UINTN                 McfgCount;
  UINTN                 Index;

  McfgTable = FindAcpiPtr (Hob, MCFG_SIGNATURE);
  if (McfgTable == NULL) {
    return ;
  }

  Mcfg = (MCFG_STRUCTURE *)((UINTN)McfgTable + sizeof(DESCRIPTION_HEADER) + sizeof(UINT64));
  McfgCount = (McfgTable->Length - sizeof(DESCRIPTION_HEADER) - sizeof(UINT64)) / sizeof(MCFG_STRUCTURE);

  //
  // Fill PciExpress info on Hob
  // Note: Only for 1st segment
  //
  for (Index = 0; Index < McfgCount; Index++) {
    if (Mcfg[Index].PciSegmentGroupNumber == 0) {
      Hob->PciExpress.PciExpressBaseAddressInfo.PciExpressBaseAddress = Mcfg[Index].BaseAddress;
      break;
    }
  }

  return ;
}

VOID
PrepareFadtTable (
  IN HOB_TEMPLATE  *Hob
  )
{
  FADT_TABLE            *Fadt;
  EFI_ACPI_DESCRIPTION  *AcpiDescription;

  Fadt = FindAcpiPtr (Hob, FADT_SIGNATURE);
  if (Fadt == NULL) {
    return ;
  }

  AcpiDescription = &Hob->AcpiInfo.AcpiDescription;
  //
  // Fill AcpiDescription according to FADT
  // Currently, only for PM_TMR
  //
  AcpiDescription->PM_TMR_LEN = Fadt->PM_TMR_LEN;
  AcpiDescription->TMR_VAL_EXT = (UINT8)((Fadt->Flags & 0x100) != 0);
  if ((Fadt->Header.Revision >= 3) && (Fadt->Header.Length >= sizeof(FADT_TABLE))) {
    CopyMem (
      &AcpiDescription->PM_TMR_BLK,
      &Fadt->X_PM_TMR_BLK,
      sizeof(GADDRESS_STRUCTURE)
      );
    CopyMem (
      &AcpiDescription->RESET_REG,
      &Fadt->RESET_REG,
      sizeof(GADDRESS_STRUCTURE)
      );
    AcpiDescription->RESET_VALUE = Fadt->RESET_VALUE;
  }
  if (AcpiDescription->PM_TMR_BLK.Address == 0) {
    AcpiDescription->PM_TMR_BLK.Address          = Fadt->PM_TMR_BLK;
    AcpiDescription->PM_TMR_BLK.AddressSpaceId   = ACPI_ADDRESS_ID_IO;
    AcpiDescription->PM_TMR_BLK.RegisterBitWidth = (UINT8) ((AcpiDescription->TMR_VAL_EXT == 0) ? 24 : 32);
  }

  return ;
}

VOID
PrepareHobLegacyTable (
  IN HOB_TEMPLATE  *Hob
  )
{
  CHAR8    PrintBuffer[256];

  Hob->Acpi.Table   = (EFI_PHYSICAL_ADDRESS)(UINTN)FindAcpiRsdPtr ();
  AsciiSPrint (PrintBuffer, 256, "\nAcpiTable=0x%x ", (UINT32)(UINTN)Hob->Acpi.Table);
  PrintString (PrintBuffer);
  Hob->Acpi20.Table = (EFI_PHYSICAL_ADDRESS)(UINTN)FindAcpiRsdPtr ();
  Hob->Smbios.Table = (EFI_PHYSICAL_ADDRESS)(UINTN)FindSMBIOSPtr ();
  AsciiSPrint (PrintBuffer, 256, "SMBIOS Table=0x%x ", (UINT32)(UINTN)Hob->Smbios.Table);
  PrintString (PrintBuffer);
  Hob->Mps.Table    = (EFI_PHYSICAL_ADDRESS)(UINTN)FindMPSPtr ();
  AsciiSPrint (PrintBuffer, 256, "MPS Table=0x%x\n", (UINT32)(UINTN)Hob->Mps.Table);
  PrintString (PrintBuffer);

  PrepareMcfgTable (Hob);

  PrepareFadtTable (Hob);

  return ;
}