summaryrefslogtreecommitdiffstats
path: root/ShellPkg/Library/UefiShellAcpiViewCommandLib/Parsers/Srat/SratParser.c
blob: 3613900ae322483fdd3d3383de4e22ba75b2128b (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
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
/** @file
  SRAT table parser

  Copyright (c) 2016 - 2019, ARM Limited. All rights reserved.
  SPDX-License-Identifier: BSD-2-Clause-Patent

  @par Reference(s):
    - ACPI 6.3 Specification - January 2019
**/

#include <IndustryStandard/Acpi.h>
#include <Library/PrintLib.h>
#include <Library/UefiLib.h>
#include "AcpiParser.h"
#include "AcpiTableParser.h"

// Local Variables
STATIC CONST UINT8* SratRAType;
STATIC CONST UINT8* SratRALength;
STATIC CONST UINT8* SratDeviceHandleType;
STATIC ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo;

/**
  This function validates the Reserved field in the SRAT table header.

  @param [in] Ptr     Pointer to the start of the field data.
  @param [in] Context Pointer to context specific information e.g. this
                      could be a pointer to the ACPI table header.
**/
STATIC
VOID
EFIAPI
ValidateSratReserved (
  IN UINT8* Ptr,
  IN VOID*  Context
  )
{
  if (*(UINT32*)Ptr != 1) {
    IncrementErrorCount ();
    Print (L"\nERROR: Reserved should be 1 for backward compatibility.\n");
  }
}

/**
  This function validates the Device Handle Type field in the Generic Initiator
  Affinity Structure.

  @param [in] Ptr     Pointer to the start of the field data.
  @param [in] Context Pointer to context specific information e.g. this
                      could be a pointer to the ACPI table header.
**/
STATIC
VOID
EFIAPI
ValidateSratDeviceHandleType (
  IN UINT8* Ptr,
  IN VOID*  Context
  )
{
  UINT8   DeviceHandleType;

  DeviceHandleType = *Ptr;

  if (DeviceHandleType > EFI_ACPI_6_3_PCI_DEVICE_HANDLE) {
    IncrementErrorCount ();
    Print (
      L"\nERROR: Invalid Device Handle Type: %d. Must be between 0 and %d.",
      DeviceHandleType,
      EFI_ACPI_6_3_PCI_DEVICE_HANDLE
      );
  }
}

/**
  This function traces the PCI BDF Number field inside Device Handle - PCI

  @param [in] Format  Format string for tracing the data.
  @param [in] Ptr     Pointer to the start of the buffer.
**/
STATIC
VOID
EFIAPI
DumpSratPciBdfNumber (
  IN CONST CHAR16* Format,
  IN UINT8*        Ptr
  )
{
  CHAR16 Buffer[OUTPUT_FIELD_COLUMN_WIDTH];

  Print (L"\n");

  /*
    The PCI BDF Number subfields are printed in the order specified in the ACPI
    specification. The format of the 16-bit PCI BDF Number field is as follows:

    +-----+------+------+
    |DEV  | FUNC | BUS  |
    +-----+------+------+
    |15:11| 10:8 |  7:0 |
    +-----+------+------+
  */

  // Print PCI Bus Number (Bits 7:0 of Byte 2)
  UnicodeSPrint (
    Buffer,
    sizeof (Buffer),
    L"PCI Bus Number"
    );
  PrintFieldName (4, Buffer);
  Print (
    L"0x%x\n",
    *Ptr
    );

  Ptr++;

  // Print PCI Device Number (Bits 7:3 of Byte 3)
  UnicodeSPrint (
    Buffer,
    sizeof (Buffer),
    L"PCI Device Number"
    );
  PrintFieldName (4, Buffer);
  Print (
    L"0x%x\n",
    (*Ptr & (BIT7 | BIT6 | BIT5 | BIT4 | BIT3)) >> 3
    );

  // PCI Function Number (Bits 2:0 of Byte 3)
  UnicodeSPrint (
    Buffer,
    sizeof (Buffer),
    L"PCI Function Number"
    );
  PrintFieldName (4, Buffer);
  Print (
    L"0x%x\n",
    *Ptr & (BIT2 | BIT1 | BIT0)
    );
}

/**
  An ACPI_PARSER array describing the Device Handle - ACPI
**/
STATIC CONST ACPI_PARSER SratDeviceHandleAcpiParser[] = {
  {L"ACPI_HID", 8, 0, L"0x%lx", NULL, NULL, NULL, NULL},
  {L"ACPI_UID", 4, 8, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Reserved", 4, 12, L"0x%x", NULL, NULL, NULL, NULL}
};

/**
  An ACPI_PARSER array describing the Device Handle - PCI
**/
STATIC CONST ACPI_PARSER SratDeviceHandlePciParser[] = {
  {L"PCI Segment", 2, 0, L"0x%x", NULL, NULL, NULL, NULL},
  {L"PCI BDF Number", 2, 2, NULL, DumpSratPciBdfNumber, NULL, NULL, NULL},
  {L"Reserved", 12, 4, L"%x %x %x %x - %x %x %x %x - %x %x %x %x", Dump12Chars,
   NULL, NULL, NULL}
};

/**
  This function traces the Device Handle field inside Generic Initiator
  Affinity Structure.

  @param [in] Format  Format string for tracing the data.
  @param [in] Ptr     Pointer to the start of the buffer.
**/
STATIC
VOID
EFIAPI
DumpSratDeviceHandle (
  IN CONST CHAR16* Format,
  IN UINT8*        Ptr
 )
{
  if (SratDeviceHandleType == NULL) {
    IncrementErrorCount ();
    Print (L"\nERROR: Device Handle Type read incorrectly.\n");
    return;
  }

  Print (L"\n");

  if (*SratDeviceHandleType == EFI_ACPI_6_3_ACPI_DEVICE_HANDLE) {
    ParseAcpi (
      TRUE,
      2,
      NULL,
      Ptr,
      sizeof (EFI_ACPI_6_3_DEVICE_HANDLE_ACPI),
      PARSER_PARAMS (SratDeviceHandleAcpiParser)
      );
  } else if (*SratDeviceHandleType == EFI_ACPI_6_3_PCI_DEVICE_HANDLE) {
    ParseAcpi (
      TRUE,
      2,
      NULL,
      Ptr,
      sizeof (EFI_ACPI_6_3_DEVICE_HANDLE_PCI),
      PARSER_PARAMS (SratDeviceHandlePciParser)
      );
  }
}

/**
  This function traces the APIC Proximity Domain field.

  @param [in] Format  Format string for tracing the data.
  @param [in] Ptr     Pointer to the start of the buffer.
**/
STATIC
VOID
EFIAPI
DumpSratApicProximity (
 IN CONST CHAR16* Format,
 IN UINT8*        Ptr
 )
{
  UINT32 ProximityDomain;

  ProximityDomain = Ptr[0] | (Ptr[1] << 8) | (Ptr[2] << 16);

  Print (Format, ProximityDomain);
}

/**
  An ACPI_PARSER array describing the SRAT Table.
**/
STATIC CONST ACPI_PARSER SratParser[] = {
  PARSE_ACPI_HEADER (&AcpiHdrInfo),
  {L"Reserved", 4, 36, L"0x%x", NULL, NULL, ValidateSratReserved, NULL},
  {L"Reserved", 8, 40, L"0x%lx", NULL, NULL, NULL, NULL}
};

/**
  An ACPI_PARSER array describing the Resource Allocation structure header.
**/
STATIC CONST ACPI_PARSER SratResourceAllocationParser[] = {
  {L"Type", 1, 0, NULL, NULL, (VOID**)&SratRAType, NULL, NULL},
  {L"Length", 1, 1, NULL, NULL, (VOID**)&SratRALength, NULL, NULL}
};

/**
  An ACPI_PARSER array describing the GICC Affinity structure.
**/
STATIC CONST ACPI_PARSER SratGicCAffinityParser[] = {
  {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL},

  {L"Proximity Domain", 4, 2, L"0x%x", NULL, NULL, NULL, NULL},
  {L"ACPI Processor UID", 4, 6, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Flags", 4, 10, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Clock Domain", 4, 14, L"0x%x", NULL, NULL, NULL, NULL}
};

/**
  An ACPI_PARSER array describing the GIC ITS Affinity structure.
**/
STATIC CONST ACPI_PARSER SratGicITSAffinityParser[] = {
  {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL},

  {L"Proximity Domain", 4, 2, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Reserved", 2, 6, L"0x%x", NULL, NULL, NULL, NULL},
  {L"ITS Id", 4, 8, L"0x%x", NULL, NULL, NULL, NULL},
};

/**
  An ACPI_PARSER array describing the Generic Initiator Affinity Structure
**/
STATIC CONST ACPI_PARSER SratGenericInitiatorAffinityParser[] = {
  {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL},

  {L"Reserved", 1, 2, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Device Handle Type", 1, 3, L"%d", NULL, (VOID**)&SratDeviceHandleType,
   ValidateSratDeviceHandleType, NULL},
  {L"Proximity Domain", 4, 4, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Device Handle", 16, 8, L"%s", DumpSratDeviceHandle, NULL, NULL, NULL},
  {L"Flags", 4, 24, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Reserved", 4, 28, L"0x%x", NULL, NULL, NULL, NULL}
};

/**
  An ACPI_PARSER array describing the Memory Affinity structure.
**/
STATIC CONST ACPI_PARSER SratMemAffinityParser[] = {
  {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL},

  {L"Proximity Domain", 4, 2, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Reserved", 2, 6, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Base Address Low", 4, 8, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Base Address High", 4, 12, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Length Low", 4, 16, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Length High", 4, 20, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Reserved", 4, 24, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Flags", 4, 28, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Reserved", 8, 32, L"0x%lx", NULL, NULL, NULL, NULL}
};

/**
  An ACPI_PARSER array describing the APIC/SAPIC Affinity structure.
**/
STATIC CONST ACPI_PARSER SratApciSapicAffinityParser[] = {
  {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL},

  {L"Proximity Domain [7:0]", 1, 2, L"0x%x", NULL, NULL, NULL, NULL},
  {L"APIC ID", 1, 3, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Flags", 4, 4, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Local SAPIC EID", 1, 8, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Proximity Domain [31:8]", 3, 9, L"0x%x", DumpSratApicProximity,
   NULL, NULL, NULL},
  {L"Clock Domain", 4, 12, L"0x%x", NULL, NULL, NULL, NULL}
};

/**
  An ACPI_PARSER array describing the Processor Local x2APIC Affinity structure.
**/
STATIC CONST ACPI_PARSER SratX2ApciAffinityParser[] = {
  {L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL},

  {L"Reserved", 2, 2, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Proximity Domain", 4, 4, L"0x%x", NULL, NULL, NULL, NULL},
  {L"X2APIC ID", 4, 8, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Flags", 4, 12, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Clock Domain", 4, 16, L"0x%x", NULL, NULL, NULL, NULL},
  {L"Reserved", 4, 20, L"0x%x", NULL, NULL, NULL, NULL}
};

/**
  This function parses the ACPI SRAT table.
  When trace is enabled this function parses the SRAT table and
  traces the ACPI table fields.

  This function parses the following Resource Allocation Structures:
    - Processor Local APIC/SAPIC Affinity Structure
    - Memory Affinity Structure
    - Processor Local x2APIC Affinity Structure
    - GICC Affinity Structure

  This function also performs validation of the ACPI table fields.

  @param [in] Trace              If TRUE, trace the ACPI fields.
  @param [in] Ptr                Pointer to the start of the buffer.
  @param [in] AcpiTableLength    Length of the ACPI table.
  @param [in] AcpiTableRevision  Revision of the ACPI table.
**/
VOID
EFIAPI
ParseAcpiSrat (
  IN BOOLEAN Trace,
  IN UINT8*  Ptr,
  IN UINT32  AcpiTableLength,
  IN UINT8   AcpiTableRevision
  )
{
  UINT32 Offset;
  UINT8* ResourcePtr;
  UINT32 GicCAffinityIndex;
  UINT32 GicITSAffinityIndex;
  UINT32 GenericInitiatorAffinityIndex;
  UINT32 MemoryAffinityIndex;
  UINT32 ApicSapicAffinityIndex;
  UINT32 X2ApicAffinityIndex;
  CHAR8  Buffer[80];  // Used for AsciiName param of ParseAcpi

  GicCAffinityIndex = 0;
  GicITSAffinityIndex = 0;
  GenericInitiatorAffinityIndex = 0;
  MemoryAffinityIndex = 0;
  ApicSapicAffinityIndex = 0;
  X2ApicAffinityIndex = 0;

  if (!Trace) {
    return;
  }

  Offset = ParseAcpi (
             TRUE,
             0,
             "SRAT",
             Ptr,
             AcpiTableLength,
             PARSER_PARAMS (SratParser)
             );

  ResourcePtr = Ptr + Offset;

  while (Offset < AcpiTableLength) {
    ParseAcpi (
      FALSE,
      0,
      NULL,
      ResourcePtr,
      AcpiTableLength - Offset,
      PARSER_PARAMS (SratResourceAllocationParser)
      );

    // Check if the values used to control the parsing logic have been
    // successfully read.
    if ((SratRAType == NULL) ||
        (SratRALength == NULL)) {
      IncrementErrorCount ();
      Print (
        L"ERROR: Insufficient remaining table buffer length to read the " \
          L"Static Resource Allocation structure header. Length = %d.\n",
        AcpiTableLength - Offset
        );
      return;
    }

    // Make sure the SRAT structure lies inside the table
    if ((Offset + *SratRALength) > AcpiTableLength) {
      IncrementErrorCount ();
      Print (
        L"ERROR: Invalid SRAT structure length. SratRALength = %d. " \
          L"RemainingTableBufferLength = %d. SRAT parsing aborted.\n",
        *SratRALength,
        AcpiTableLength - Offset
        );
      return;
    }

    switch (*SratRAType) {
      case EFI_ACPI_6_3_GICC_AFFINITY:
        AsciiSPrint (
          Buffer,
          sizeof (Buffer),
          "GICC Affinity Structure [%d]",
          GicCAffinityIndex++
          );
        ParseAcpi (
          TRUE,
          2,
          Buffer,
          ResourcePtr,
          *SratRALength,
          PARSER_PARAMS (SratGicCAffinityParser)
          );
        break;

      case EFI_ACPI_6_3_GIC_ITS_AFFINITY:
        AsciiSPrint (
          Buffer,
          sizeof (Buffer),
          "GIC ITS Affinity Structure [%d]",
          GicITSAffinityIndex++
        );
        ParseAcpi (
          TRUE,
          2,
          Buffer,
          ResourcePtr,
          *SratRALength,
          PARSER_PARAMS (SratGicITSAffinityParser)
          );
        break;

      case EFI_ACPI_6_3_GENERIC_INITIATOR_AFFINITY:
        AsciiSPrint (
          Buffer,
          sizeof (Buffer),
          "Generic Initiator Affinity Structure [%d]",
          GenericInitiatorAffinityIndex++
        );
        ParseAcpi (
          TRUE,
          2,
          Buffer,
          ResourcePtr,
          *SratRALength,
          PARSER_PARAMS (SratGenericInitiatorAffinityParser)
        );
        break;

      case EFI_ACPI_6_3_MEMORY_AFFINITY:
        AsciiSPrint (
          Buffer,
          sizeof (Buffer),
          "Memory Affinity Structure [%d]",
          MemoryAffinityIndex++
          );
        ParseAcpi (
          TRUE,
          2,
          Buffer,
          ResourcePtr,
          *SratRALength,
          PARSER_PARAMS (SratMemAffinityParser)
          );
        break;

      case EFI_ACPI_6_3_PROCESSOR_LOCAL_APIC_SAPIC_AFFINITY:
        AsciiSPrint (
          Buffer,
          sizeof (Buffer),
          "APIC/SAPIC Affinity Structure [%d]",
          ApicSapicAffinityIndex++
          );
        ParseAcpi (
          TRUE,
          2,
          Buffer,
          ResourcePtr,
          *SratRALength,
          PARSER_PARAMS (SratApciSapicAffinityParser)
          );
        break;

      case EFI_ACPI_6_3_PROCESSOR_LOCAL_X2APIC_AFFINITY:
        AsciiSPrint (
          Buffer,
          sizeof (Buffer),
          "X2APIC Affinity Structure [%d]",
          X2ApicAffinityIndex++
          );
        ParseAcpi (
          TRUE,
          2,
          Buffer,
          ResourcePtr,
          *SratRALength,
          PARSER_PARAMS (SratX2ApciAffinityParser)
          );
        break;

      default:
        IncrementErrorCount ();
        Print (L"ERROR: Unknown SRAT Affinity type = 0x%x\n", *SratRAType);
        break;
    }

    ResourcePtr += (*SratRALength);
    Offset += (*SratRALength);
  }
}