summaryrefslogtreecommitdiffstats
path: root/MdePkg/Library/UefiLib/Acpi.c
blob: 983d3774c3e99d52ce3df539970acc377e0fb825 (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
/** @file
  This module provides help function for finding ACPI table.

  Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include "UefiLibInternal.h"
#include <IndustryStandard/Acpi.h>
#include <Guid/Acpi.h>

/**
  This function scans ACPI table in XSDT/RSDT.

  @param Sdt                    ACPI XSDT/RSDT.
  @param TablePointerSize       Size of table pointer: 8(XSDT) or 4(RSDT).
  @param Signature              ACPI table signature.
  @param PreviousTable          Pointer to previous returned table to locate
                                next table, or NULL to locate first table.
  @param PreviousTableLocated   Pointer to the indicator about whether the
                                previous returned table could be located, or
                                NULL if PreviousTable is NULL.

  If PreviousTable is NULL and PreviousTableLocated is not NULL, then ASSERT().
  If PreviousTable is not NULL and PreviousTableLocated is NULL, then ASSERT().

  @return ACPI table or NULL if not found.

**/
EFI_ACPI_COMMON_HEADER *
ScanTableInSDT (
  IN  EFI_ACPI_DESCRIPTION_HEADER  *Sdt,
  IN  UINTN                        TablePointerSize,
  IN  UINT32                       Signature,
  IN  EFI_ACPI_COMMON_HEADER       *PreviousTable  OPTIONAL,
  OUT BOOLEAN                      *PreviousTableLocated OPTIONAL
  )
{
  UINTN                   Index;
  UINTN                   EntryCount;
  UINT64                  EntryPtr;
  UINTN                   BasePtr;
  EFI_ACPI_COMMON_HEADER  *Table;

  if (PreviousTableLocated != NULL) {
    ASSERT (PreviousTable != NULL);
    *PreviousTableLocated = FALSE;
  } else {
    ASSERT (PreviousTable == NULL);
  }

  if (Sdt == NULL) {
    return NULL;
  }

  EntryCount = (Sdt->Length - sizeof (EFI_ACPI_DESCRIPTION_HEADER)) / TablePointerSize;

  BasePtr = (UINTN)(Sdt + 1);
  for (Index = 0; Index < EntryCount; Index++) {
    EntryPtr = 0;
    CopyMem (&EntryPtr, (VOID *)(BasePtr + Index * TablePointerSize), TablePointerSize);
    Table = (EFI_ACPI_COMMON_HEADER *)((UINTN)(EntryPtr));
    if ((Table != NULL) && (Table->Signature == Signature)) {
      if (PreviousTable != NULL) {
        if (Table == PreviousTable) {
          *PreviousTableLocated = TRUE;
        } else if (*PreviousTableLocated) {
          //
          // Return next table.
          //
          return Table;
        }
      } else {
        //
        // Return first table.
        //
        return Table;
      }
    }
  }

  return NULL;
}

/**
  To locate FACS in FADT.

  @param Fadt   FADT table pointer.

  @return FACS table pointer or NULL if not found.

**/
EFI_ACPI_COMMON_HEADER *
LocateAcpiFacsFromFadt (
  IN EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE  *Fadt
  )
{
  EFI_ACPI_COMMON_HEADER  *Facs;
  UINT64                  Data64;

  if (Fadt == NULL) {
    return NULL;
  }

  if (Fadt->Header.Revision < EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_REVISION) {
    Facs = (EFI_ACPI_COMMON_HEADER *)(UINTN)Fadt->FirmwareCtrl;
  } else {
    CopyMem (&Data64, &Fadt->XFirmwareCtrl, sizeof (UINT64));
    if (Data64 != 0) {
      Facs = (EFI_ACPI_COMMON_HEADER *)(UINTN)Data64;
    } else {
      Facs = (EFI_ACPI_COMMON_HEADER *)(UINTN)Fadt->FirmwareCtrl;
    }
  }

  return Facs;
}

/**
  To locate DSDT in FADT.

  @param Fadt   FADT table pointer.

  @return DSDT table pointer or NULL if not found.

**/
EFI_ACPI_COMMON_HEADER *
LocateAcpiDsdtFromFadt (
  IN EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE  *Fadt
  )
{
  EFI_ACPI_COMMON_HEADER  *Dsdt;
  UINT64                  Data64;

  if (Fadt == NULL) {
    return NULL;
  }

  if (Fadt->Header.Revision < EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_REVISION) {
    Dsdt = (EFI_ACPI_COMMON_HEADER *)(UINTN)Fadt->Dsdt;
  } else {
    CopyMem (&Data64, &Fadt->XDsdt, sizeof (UINT64));
    if (Data64 != 0) {
      Dsdt = (EFI_ACPI_COMMON_HEADER *)(UINTN)Data64;
    } else {
      Dsdt = (EFI_ACPI_COMMON_HEADER *)(UINTN)Fadt->Dsdt;
    }
  }

  return Dsdt;
}

/**
  To locate ACPI table in ACPI ConfigurationTable.

  @param AcpiGuid               The GUID used to get ACPI ConfigurationTable.
  @param Signature              ACPI table signature.
  @param PreviousTable          Pointer to previous returned table to locate
                                next table, or NULL to locate first table.
  @param PreviousTableLocated   Pointer to the indicator to return whether the
                                previous returned table could be located or not,
                                or NULL if PreviousTable is NULL.

  If PreviousTable is NULL and PreviousTableLocated is not NULL, then ASSERT().
  If PreviousTable is not NULL and PreviousTableLocated is NULL, then ASSERT().
  If AcpiGuid is NULL, then ASSERT().

  @return ACPI table or NULL if not found.

**/
EFI_ACPI_COMMON_HEADER *
LocateAcpiTableInAcpiConfigurationTable (
  IN  EFI_GUID                *AcpiGuid,
  IN  UINT32                  Signature,
  IN  EFI_ACPI_COMMON_HEADER  *PreviousTable  OPTIONAL,
  OUT BOOLEAN                 *PreviousTableLocated OPTIONAL
  )
{
  EFI_STATUS                                    Status;
  EFI_ACPI_COMMON_HEADER                        *Table;
  EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER  *Rsdp;
  EFI_ACPI_DESCRIPTION_HEADER                   *Rsdt;
  EFI_ACPI_DESCRIPTION_HEADER                   *Xsdt;
  EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE     *Fadt;

  if (PreviousTableLocated != NULL) {
    ASSERT (PreviousTable != NULL);
    *PreviousTableLocated = FALSE;
  } else {
    ASSERT (PreviousTable == NULL);
  }

  Rsdp = NULL;
  //
  // Get ACPI ConfigurationTable (RSD_PTR)
  //
  Status = EfiGetSystemConfigurationTable (AcpiGuid, (VOID **)&Rsdp);
  if (EFI_ERROR (Status) || (Rsdp == NULL)) {
    return NULL;
  }

  Table = NULL;

  //
  // Search XSDT
  //
  if (Rsdp->Revision >= EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER_REVISION) {
    Xsdt = (EFI_ACPI_DESCRIPTION_HEADER *)(UINTN)Rsdp->XsdtAddress;
    if (Signature == EFI_ACPI_2_0_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE) {
      ASSERT (PreviousTable == NULL);
      //
      // It is to locate DSDT,
      // need to locate FADT first.
      //
      Fadt = (EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE *)ScanTableInSDT (
                                                            Xsdt,
                                                            sizeof (UINT64),
                                                            EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE,
                                                            NULL,
                                                            NULL
                                                            );

      if (Fadt != NULL) {
        Table = LocateAcpiDsdtFromFadt (Fadt);
      } else {
        Table = NULL;
      }
    } else if (Signature == EFI_ACPI_2_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE) {
      ASSERT (PreviousTable == NULL);
      //
      // It is to locate FACS,
      // need to locate FADT first.
      //
      Fadt = (EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE *)ScanTableInSDT (
                                                            Xsdt,
                                                            sizeof (UINT64),
                                                            EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE,
                                                            NULL,
                                                            NULL
                                                            );

      if (Fadt != NULL) {
        Table = LocateAcpiFacsFromFadt (Fadt);
      } else {
        Table = NULL;
      }
    } else {
      Table = ScanTableInSDT (
                Xsdt,
                sizeof (UINT64),
                Signature,
                PreviousTable,
                PreviousTableLocated
                );
    }
  }

  if (Table != NULL) {
    return Table;
  } else if ((PreviousTableLocated != NULL) &&
             *PreviousTableLocated)
  {
    //
    // PreviousTable could be located in XSDT,
    // but next table could not be located in XSDT.
    //
    return NULL;
  }

  //
  // Search RSDT
  //
  Rsdt = (EFI_ACPI_DESCRIPTION_HEADER *)(UINTN)Rsdp->RsdtAddress;
  if (Signature == EFI_ACPI_2_0_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE) {
    ASSERT (PreviousTable == NULL);
    //
    // It is to locate DSDT,
    // need to locate FADT first.
    //
    Fadt = (EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE *)ScanTableInSDT (
                                                          Rsdt,
                                                          sizeof (UINT32),
                                                          EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE,
                                                          NULL,
                                                          NULL
                                                          );

    if (Fadt != NULL) {
      Table = LocateAcpiDsdtFromFadt (Fadt);
    } else {
      Table = NULL;
    }
  } else if (Signature == EFI_ACPI_2_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE) {
    ASSERT (PreviousTable == NULL);
    //
    // It is to locate FACS,
    // need to locate FADT first.
    //
    Fadt = (EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE *)ScanTableInSDT (
                                                          Rsdt,
                                                          sizeof (UINT32),
                                                          EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE,
                                                          NULL,
                                                          NULL
                                                          );

    if (Fadt != NULL) {
      Table = LocateAcpiFacsFromFadt (Fadt);
    } else {
      Table = NULL;
    }
  } else {
    Table = ScanTableInSDT (
              Rsdt,
              sizeof (UINT32),
              Signature,
              PreviousTable,
              PreviousTableLocated
              );
  }

  return Table;
}

/**
  This function locates next ACPI table in XSDT/RSDT based on Signature and
  previous returned Table.

  If PreviousTable is NULL:
  This function will locate the first ACPI table in XSDT/RSDT based on
  Signature in gEfiAcpi20TableGuid system configuration table first, and then
  gEfiAcpi10TableGuid system configuration table.
  This function will locate in XSDT first, and then RSDT.
  For DSDT, this function will locate XDsdt in FADT first, and then Dsdt in
  FADT.
  For FACS, this function will locate XFirmwareCtrl in FADT first, and then
  FirmwareCtrl in FADT.

  If PreviousTable is not NULL:
  1. If it could be located in XSDT in gEfiAcpi20TableGuid system configuration
     table, then this function will just locate next table in XSDT in
     gEfiAcpi20TableGuid system configuration table.
  2. If it could be located in RSDT in gEfiAcpi20TableGuid system configuration
     table, then this function will just locate next table in RSDT in
     gEfiAcpi20TableGuid system configuration table.
  3. If it could be located in RSDT in gEfiAcpi10TableGuid system configuration
     table, then this function will just locate next table in RSDT in
     gEfiAcpi10TableGuid system configuration table.

  It's not supported that PreviousTable is not NULL but PreviousTable->Signature
  is not same with Signature, NULL will be returned.

  @param Signature          ACPI table signature.
  @param PreviousTable      Pointer to previous returned table to locate next
                            table, or NULL to locate first table.

  @return Next ACPI table or NULL if not found.

**/
EFI_ACPI_COMMON_HEADER *
EFIAPI
EfiLocateNextAcpiTable (
  IN UINT32                  Signature,
  IN EFI_ACPI_COMMON_HEADER  *PreviousTable OPTIONAL
  )
{
  EFI_ACPI_COMMON_HEADER  *Table;
  BOOLEAN                 TempPreviousTableLocated;
  BOOLEAN                 *PreviousTableLocated;

  if (PreviousTable != NULL) {
    if (PreviousTable->Signature != Signature) {
      //
      // PreviousTable->Signature is not same with Signature.
      //
      return NULL;
    } else if ((Signature == EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE) ||
               (Signature == EFI_ACPI_2_0_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE) ||
               (Signature == EFI_ACPI_2_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE))
    {
      //
      // There is only one FADT/DSDT/FACS table,
      // so don't try to locate next one.
      //
      return NULL;
    }

    PreviousTableLocated  = &TempPreviousTableLocated;
    *PreviousTableLocated = FALSE;
  } else {
    PreviousTableLocated = NULL;
  }

  Table = LocateAcpiTableInAcpiConfigurationTable (
            &gEfiAcpi20TableGuid,
            Signature,
            PreviousTable,
            PreviousTableLocated
            );
  if (Table != NULL) {
    return Table;
  } else if ((PreviousTableLocated != NULL) &&
             *PreviousTableLocated)
  {
    //
    // PreviousTable could be located in gEfiAcpi20TableGuid system
    // configuration table, but next table could not be located in
    // gEfiAcpi20TableGuid system configuration table.
    //
    return NULL;
  }

  return LocateAcpiTableInAcpiConfigurationTable (
           &gEfiAcpi10TableGuid,
           Signature,
           PreviousTable,
           PreviousTableLocated
           );
}

/**
  This function locates first ACPI table in XSDT/RSDT based on Signature.

  This function will locate the first ACPI table in XSDT/RSDT based on
  Signature in gEfiAcpi20TableGuid system configuration table first, and then
  gEfiAcpi10TableGuid system configuration table.
  This function will locate in XSDT first, and then RSDT.
  For DSDT, this function will locate XDsdt in FADT first, and then Dsdt in
  FADT.
  For FACS, this function will locate XFirmwareCtrl in FADT first, and then
  FirmwareCtrl in FADT.

  @param Signature          ACPI table signature.

  @return First ACPI table or NULL if not found.

**/
EFI_ACPI_COMMON_HEADER *
EFIAPI
EfiLocateFirstAcpiTable (
  IN UINT32  Signature
  )
{
  return EfiLocateNextAcpiTable (Signature, NULL);
}