summaryrefslogtreecommitdiff
path: root/DynamicTablesPkg/Library/Acpi/Common/AcpiErstLib/ErstGenerator.c
blob: a7cdad664e5d4f29eeab7ba2492d67fb03e6126b (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
/** @file
  ERST Table Generator

  Copyright (c) 2026, Arm Limited. All rights reserved.

  SPDX-License-Identifier: BSD-2-Clause-Patent

  @par Glossary:
  - Cm or CM   - Configuration Manager
  - Obj or OBJ - Object

  @par Reference(s):
  - ACPI 6.6 Specification, May 2025
**/

#include <AcpiTableGenerator.h>
#include <ConfigurationManagerHelper.h>
#include <ConfigurationManagerObject.h>
#include <IndustryStandard/Acpi66.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/TableHelperLib.h>
#include <Protocol/ConfigurationManagerProtocol.h>

#define ERST_RESERVED_SERIALIZATION_ACTION  0x0C

/** Standard ERST Generator

Requirements:
  The following Configuration Manager Object is required by this Generator:
  - EArchCommonObjErstInstructionsInfo
*/

/** Retrieve the ERST Serialization Instruction Entry information. */
GET_OBJECT_LIST (
  EObjNameSpaceArchCommon,
  EArchCommonObjErstInstructionsInfo,
  CM_ARCH_COMMON_ERST_INSTRUCTIONS_INFO
  );

/** Return whether an ERST instruction accesses the Register Region.

  @param [in] Instruction  ERST serialization instruction.

  @retval TRUE   The instruction accesses the Register Region.
  @retval FALSE  The instruction does not access the Register Region.
**/
STATIC
BOOLEAN
EFIAPI
IsRegisterAccessInstruction (
  IN UINT8  Instruction
  )
{
  switch (Instruction) {
    case EFI_ACPI_6_6_ERST_READ_REGISTER:
    case EFI_ACPI_6_6_ERST_READ_REGISTER_VALUE:
    case EFI_ACPI_6_6_ERST_WRITE_REGISTER:
    case EFI_ACPI_6_6_ERST_WRITE_REGISTER_VALUE:
    case EFI_ACPI_6_6_ERST_LOAD_VAR1:
    case EFI_ACPI_6_6_ERST_LOAD_VAR2:
    case EFI_ACPI_6_6_ERST_STORE_VAR1:
    case EFI_ACPI_6_6_ERST_ADD_VALUE:
    case EFI_ACPI_6_6_ERST_SUBTRACT_VALUE:
    case EFI_ACPI_6_6_ERST_STALL_WHILE_TRUE:
    case EFI_ACPI_6_6_ERST_SKIP_NEXT_INSTRUCTION_IF_TRUE:
    case EFI_ACPI_6_6_ERST_SET_SRC_ADDRESS_BASE:
    case EFI_ACPI_6_6_ERST_SET_DST_ADDRESS_BASE:
    case EFI_ACPI_6_6_ERST_MOVE_DATA:
      return TRUE;

    default:
      return FALSE;
  }
}

/** Validate an ERST Mask against the Register Region bit width.

  @param [in] Instruction  Pointer to an ERST CM instruction entry.

  @retval EFI_SUCCESS            The Register Region and Mask are valid.
  @retval EFI_INVALID_PARAMETER  The Register Region width or Mask is invalid.
**/
STATIC
EFI_STATUS
EFIAPI
ValidateRegisterMask (
  IN CONST CM_ARCH_COMMON_ERST_INSTRUCTIONS_INFO  *Instruction
  )
{
  CONST EFI_ACPI_6_6_GENERIC_ADDRESS_STRUCTURE  *RegisterRegion;

  ASSERT (Instruction != NULL);

  RegisterRegion = &Instruction->RegisterRegion;

  // The ERST Mask field is 64 bits and cannot describe a wider bit range.
  if (RegisterRegion->RegisterBitWidth > 64) {
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: ERST: Register Region width = %u exceeds 64 bits\n",
      RegisterRegion->RegisterBitWidth
      ));
    return EFI_INVALID_PARAMETER;
  }

  if ((RegisterRegion->RegisterBitWidth < 64) &&
      ((Instruction->Mask >> RegisterRegion->RegisterBitWidth) != 0))
  {
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: ERST: Mask = 0x%llx exceeds Register Region width = %u\n",
      Instruction->Mask,
      RegisterRegion->RegisterBitWidth
      ));
    return EFI_INVALID_PARAMETER;
  }

  return EFI_SUCCESS;
}

/** Validate the ERST CM instruction entry list.

  @param [in] Instructions       Pointer to the CM instruction entry array.
  @param [in] InstructionCount   Number of entries in Instructions.

  @retval EFI_SUCCESS            The instruction list is valid.
  @retval EFI_INVALID_PARAMETER  An instruction entry is invalid.
**/
STATIC
EFI_STATUS
EFIAPI
ValidateErstInstructions (
  IN CONST CM_ARCH_COMMON_ERST_INSTRUCTIONS_INFO  *Instructions,
  IN       UINT32                                 InstructionCount
  )
{
  BOOLEAN     ActionSeen[EFI_ACPI_6_6_ERST_GET_EXECUTE_OPERATION_TIMINGS + 1];
  EFI_STATUS  Status;
  UINT32      GroupEnd;
  UINT32      GroupStart;
  UINT32      Index;
  UINT8       PreviousAction;

  ASSERT (Instructions != NULL);
  ASSERT (InstructionCount != 0);

  ZeroMem (ActionSeen, sizeof (ActionSeen));
  PreviousAction = MAX_UINT8;

  for (Index = 0; Index < InstructionCount; Index++) {
    if ((Instructions[Index].SerializationAction >
         EFI_ACPI_6_6_ERST_GET_EXECUTE_OPERATION_TIMINGS) ||
        (Instructions[Index].SerializationAction ==
         ERST_RESERVED_SERIALIZATION_ACTION))
    {
      DEBUG ((
        DEBUG_ERROR,
        "ERROR: ERST: Invalid Serialization Action encoding = 0x%x at index %u\n",
        Instructions[Index].SerializationAction,
        Index
        ));
      return EFI_INVALID_PARAMETER;
    }

    if (Instructions[Index].Instruction > EFI_ACPI_6_6_ERST_MOVE_DATA) {
      DEBUG ((
        DEBUG_ERROR,
        "ERROR: ERST: Invalid Instruction encoding = 0x%x at index %u\n",
        Instructions[Index].Instruction,
        Index
        ));
      return EFI_INVALID_PARAMETER;
    }

    if ((Instructions[Index].Flags &
         ~EFI_ACPI_6_6_ERST_PRESERVE_REGISTER) != 0)
    {
      DEBUG ((
        DEBUG_ERROR,
        "ERROR: ERST: Invalid Flags = 0x%x at index %u\n",
        Instructions[Index].Flags,
        Index
        ));
      return EFI_INVALID_PARAMETER;
    }

    if (Instructions[Index].SerializationAction != PreviousAction) {
      if (ActionSeen[Instructions[Index].SerializationAction]) {
        DEBUG ((
          DEBUG_ERROR,
          "ERROR: ERST: Instructions for Serialization Action = 0x%x "
          "are not consecutive.\n",
          Instructions[Index].SerializationAction
          ));
        return EFI_INVALID_PARAMETER;
      }

      ActionSeen[Instructions[Index].SerializationAction] = TRUE;
      PreviousAction                                      = Instructions[Index].SerializationAction;
    }

    if (IsRegisterAccessInstruction (Instructions[Index].Instruction)) {
      Status = ValidateRegisterMask (&Instructions[Index]);
      if (EFI_ERROR (Status)) {
        return Status;
      }
    }
  }

  GroupStart = 0;
  while (GroupStart < InstructionCount) {
    GroupEnd = GroupStart + 1;
    while ((GroupEnd < InstructionCount) &&
           (Instructions[GroupEnd].SerializationAction ==
            Instructions[GroupStart].SerializationAction))
    {
      GroupEnd++;
    }

    for (Index = GroupStart; Index < GroupEnd; Index++) {
      if ((Instructions[Index].Instruction == EFI_ACPI_6_6_ERST_GOTO) &&
          (Instructions[Index].Value >= (GroupEnd - GroupStart)))
      {
        DEBUG ((
          DEBUG_ERROR,
          "ERROR: ERST: GOTO target = 0x%llx at index %u is outside its "
          "Serialization Action.\n",
          Instructions[Index].Value,
          Index
          ));
        return EFI_INVALID_PARAMETER;
      }
    }

    GroupStart = GroupEnd;
  }

  return EFI_SUCCESS;
}

/** Populate the ERST Serialization Instruction Entry list.

  @param [out] ErstEntries       Pointer to the first ACPI instruction entry.
  @param [in]  Instructions      Pointer to the CM instruction entry array.
  @param [in]  InstructionCount  Number of entries in Instructions.
**/
STATIC
VOID
EFIAPI
PopulateErstInstructions (
  OUT EFI_ACPI_6_6_ERST_SERIALIZATION_INSTRUCTION_ENTRY  *ErstEntries,
  IN  CONST CM_ARCH_COMMON_ERST_INSTRUCTIONS_INFO        *Instructions,
  IN        UINT32                                       InstructionCount
  )
{
  UINT32  Index;

  ASSERT (ErstEntries != NULL);
  ASSERT (Instructions != NULL);

  for (Index = 0; Index < InstructionCount; Index++) {
    ErstEntries[Index].SerializationAction = Instructions[Index].SerializationAction;
    ErstEntries[Index].Instruction         = Instructions[Index].Instruction;
    ErstEntries[Index].Flags               = Instructions[Index].Flags;
    ErstEntries[Index].Value               = Instructions[Index].Value;
    ErstEntries[Index].Mask                = Instructions[Index].Mask;

    CopyMem (
      &ErstEntries[Index].RegisterRegion,
      &Instructions[Index].RegisterRegion,
      sizeof (ErstEntries[Index].RegisterRegion)
      );
  }
}

/** Construct the ERST ACPI table.

  This function invokes the Configuration Manager protocol interface to get
  the Serialization Instruction Entries required for generating the table.

  @param [in]  This            Pointer to the table generator.
  @param [in]  AcpiTableInfo   Pointer to the ACPI Table Info.
  @param [in]  CfgMgrProtocol  Pointer to the Configuration Manager Protocol.
  @param [out] Table           Pointer to the constructed ACPI Table.

  @retval EFI_SUCCESS            Table generated successfully.
  @retval EFI_INVALID_PARAMETER  A parameter or CM instruction is invalid.
  @retval EFI_NOT_FOUND          The required object was not found.
  @retval EFI_OUT_OF_RESOURCES   Memory allocation failed.
**/
STATIC
EFI_STATUS
EFIAPI
BuildErstTable (
  IN  CONST ACPI_TABLE_GENERATOR                  *CONST  This,
  IN  CONST CM_STD_OBJ_ACPI_TABLE_INFO            *CONST  AcpiTableInfo,
  IN  CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL  *CONST  CfgMgrProtocol,
  OUT       EFI_ACPI_DESCRIPTION_HEADER          **CONST  Table
  )
{
  EFI_ACPI_6_6_ERROR_RECORD_SERIALIZATION_TABLE_HEADER  *Erst;
  EFI_ACPI_6_6_ERST_SERIALIZATION_INSTRUCTION_ENTRY     *ErstEntries;
  CM_ARCH_COMMON_ERST_INSTRUCTIONS_INFO                 *Instructions;
  UINT32                                                InstructionCount;
  EFI_STATUS                                            Status;
  UINT32                                                TableSize;

  ASSERT (This != NULL);
  ASSERT (AcpiTableInfo != NULL);
  ASSERT (CfgMgrProtocol != NULL);
  ASSERT (Table != NULL);
  ASSERT (AcpiTableInfo->TableGeneratorId == This->GeneratorID);
  ASSERT (AcpiTableInfo->AcpiTableSignature == This->AcpiTableSignature);

  if ((AcpiTableInfo->AcpiTableRevision < This->MinAcpiTableRevision) ||
      (AcpiTableInfo->AcpiTableRevision > This->AcpiTableRevision))
  {
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: ERST: Requested table revision = %u is not supported. "
      "Supported table revision: Minimum = %u, Maximum = %u\n",
      AcpiTableInfo->AcpiTableRevision,
      This->MinAcpiTableRevision,
      This->AcpiTableRevision
      ));
    return EFI_INVALID_PARAMETER;
  }

  *Table           = NULL;
  Instructions     = NULL;
  InstructionCount = 0;

  Status = GetEArchCommonObjErstInstructionsInfo (
             CfgMgrProtocol,
             CM_NULL_TOKEN,
             &Instructions,
             &InstructionCount
             );
  if (Status == EFI_NOT_FOUND) {
    DEBUG ((DEBUG_INFO, "ERST: No instruction entries found; skipping table.\n"));
    return EFI_NOT_FOUND;
  }

  if (EFI_ERROR (Status)) {
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: ERST: Failed to get instruction entries. Status = %r\n",
      Status
      ));
    return Status;
  }

  Status = ValidateErstInstructions (Instructions, InstructionCount);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  TableSize =
    sizeof (EFI_ACPI_6_6_ERROR_RECORD_SERIALIZATION_TABLE_HEADER) +
    (InstructionCount *
     sizeof (EFI_ACPI_6_6_ERST_SERIALIZATION_INSTRUCTION_ENTRY));

  Erst = AllocateZeroPool (TableSize);
  if (Erst == NULL) {
    DEBUG ((DEBUG_ERROR, "ERROR: ERST: Failed to allocate the ERST table.\n"));
    return EFI_OUT_OF_RESOURCES;
  }

  Status = AddAcpiHeader (
             CfgMgrProtocol,
             This,
             &Erst->Header,
             AcpiTableInfo,
             TableSize
             );
  if (EFI_ERROR (Status)) {
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: ERST: Failed to add ACPI header. Status = %r\n",
      Status
      ));
    FreePool (Erst);
    return Status;
  }

  ErstEntries =
    (EFI_ACPI_6_6_ERST_SERIALIZATION_INSTRUCTION_ENTRY *)(Erst + 1);
  PopulateErstInstructions (
    ErstEntries,
    Instructions,
    InstructionCount
    );

  Erst->SerializationHeaderSize = sizeof (*Erst) - sizeof (Erst->Header);
  Erst->InstructionEntryCount   = InstructionCount;
  *Table                        = &Erst->Header;

  return EFI_SUCCESS;
}

/** Free resources allocated for constructing the ERST table.

  @param [in]      This            Pointer to the table generator.
  @param [in]      AcpiTableInfo   Pointer to the ACPI Table Info.
  @param [in]      CfgMgrProtocol  Pointer to the Configuration Manager
                                   Protocol interface.
  @param [in, out] Table           Pointer to the constructed ACPI Table.

  @retval EFI_SUCCESS            The resources were freed successfully.
  @retval EFI_INVALID_PARAMETER  The table pointer is NULL.
**/
STATIC
EFI_STATUS
EFIAPI
FreeErstTableResources (
  IN      CONST ACPI_TABLE_GENERATOR                  *CONST  This,
  IN      CONST CM_STD_OBJ_ACPI_TABLE_INFO            *CONST  AcpiTableInfo,
  IN      CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL  *CONST  CfgMgrProtocol,
  IN OUT        EFI_ACPI_DESCRIPTION_HEADER          **CONST  Table
  )
{
  ASSERT (This != NULL);
  ASSERT (AcpiTableInfo != NULL);
  ASSERT (CfgMgrProtocol != NULL);
  ASSERT (AcpiTableInfo->TableGeneratorId == This->GeneratorID);
  ASSERT (AcpiTableInfo->AcpiTableSignature == This->AcpiTableSignature);

  if (Table == NULL) {
    DEBUG ((DEBUG_ERROR, "ERROR: ERST: Invalid Table Pointer\n"));
    ASSERT (Table != NULL);
    return EFI_INVALID_PARAMETER;
  }

  if (*Table != NULL) {
    FreePool (*Table);
    *Table = NULL;
  }

  return EFI_SUCCESS;
}

/** This macro defines the ERST Table Generator revision. */
#define ERST_GENERATOR_REVISION  CREATE_REVISION (1, 0)

/** The interface for the ERST Table Generator. */
STATIC
CONST
ACPI_TABLE_GENERATOR  ErstGenerator = {
  // Generator ID
  CREATE_STD_ACPI_TABLE_GEN_ID (EStdAcpiTableIdErst),
  // Generator Description
  L"ACPI.STD.ERST.GENERATOR",
  // ACPI Table Signature
  EFI_ACPI_6_6_ERROR_RECORD_SERIALIZATION_TABLE_SIGNATURE,
  // ACPI Table Revision supported by this Generator
  EFI_ACPI_6_6_ERROR_RECORD_SERIALIZATION_TABLE_REVISION,
  // Minimum supported ACPI Table Revision
  EFI_ACPI_6_6_ERROR_RECORD_SERIALIZATION_TABLE_REVISION,
  // Creator ID
  TABLE_GENERATOR_CREATOR_ID,
  // Creator Revision
  ERST_GENERATOR_REVISION,
  // Build Table function
  BuildErstTable,
  // Free Resource function
  FreeErstTableResources,
  // Extended build function not needed.
  NULL,
  // Extended free resource function not needed.
  NULL
};

/** Register the Generator with the ACPI Table Factory.

  @param [in] ImageHandle  The handle to the image.
  @param [in] SystemTable  Pointer to the System Table.

  @retval EFI_SUCCESS            The Generator is registered.
  @retval EFI_INVALID_PARAMETER  A parameter is invalid.
  @retval EFI_ALREADY_STARTED    The Generator is already registered.
**/
EFI_STATUS
EFIAPI
AcpiErstLibConstructor (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS  Status;

  Status = RegisterAcpiTableGenerator (&ErstGenerator);
  DEBUG ((DEBUG_INFO, "ERST: Register Generator. Status = %r\n", Status));
  ASSERT_EFI_ERROR (Status);
  return Status;
}

/** Deregister the Generator from the ACPI Table Factory.

  @param [in] ImageHandle  The handle to the image.
  @param [in] SystemTable  Pointer to the System Table.

  @retval EFI_SUCCESS            The Generator is deregistered.
  @retval EFI_INVALID_PARAMETER  A parameter is invalid.
  @retval EFI_NOT_FOUND          The Generator is not registered.
**/
EFI_STATUS
EFIAPI
AcpiErstLibDestructor (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS  Status;

  Status = DeregisterAcpiTableGenerator (&ErstGenerator);
  DEBUG ((DEBUG_INFO, "ERST: Deregister Generator. Status = %r\n", Status));
  ASSERT_EFI_ERROR (Status);
  return Status;
}