summaryrefslogtreecommitdiff
path: root/DynamicTablesPkg/Library/Acpi/Common/AcpiHmatLib/HmatGenerator.c
blob: fe980f1cdcbdd76109098a9d1083b0ead5ff2b99 (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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/** @file
  HMAT Table Generator

  Copyright (c) 2026, Google LLC. All rights reserved.<BR>

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

**/

#include <Library/AcpiLib.h>
#include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Protocol/AcpiTable.h>
#include <Library/BaseMemoryLib.h>

// Module specific include files.
#include <AcpiTableGenerator.h>
#include <ConfigurationManagerHelper.h>
#include <ConfigurationManagerObject.h>
#include <Library/CmObjHelperLib.h>
#include <Library/TableHelperLib.h>
#include <Protocol/ConfigurationManagerProtocol.h>

/** Standard HMAT Generator

Requirements:
  The following Configuration Manager Object(s) are required by
  this Generator:
  - EArchCommonObjMemoryLatBwInfo
  - EArchCommonObjMemoryProximityDomainAttrInfo
  - EArchCommonObjProximityDomainInfo
  - EArchCommonObjProximityDomainRelationInfo

*/

/** Retrieve the Proximity Domain Info */
GET_OBJECT_LIST (
  EObjNameSpaceArchCommon,
  EArchCommonObjProximityDomainInfo,
  CM_ARCH_COMMON_PROXIMITY_DOMAIN_INFO
  );

/** Retrieve the Proximity Domain Relation Info. */
GET_OBJECT_LIST (
  EObjNameSpaceArchCommon,
  EArchCommonObjProximityDomainRelationInfo,
  CM_ARCH_COMMON_PROXIMITY_DOMAIN_RELATION_INFO
  );

/** Retrieve the Proximity Domain Attr Info */
GET_OBJECT_LIST (
  EObjNameSpaceArchCommon,
  EArchCommonObjMemoryProximityDomainAttrInfo,
  CM_ARCH_COMMON_MEMORY_PROXIMITY_DOMAIN_ATTR_INFO
  );

/** Retrieve the SSLBI Info */
GET_OBJECT_LIST (
  EObjNameSpaceArchCommon,
  EArchCommonObjMemoryLatBwInfo,
  CM_ARCH_COMMON_MEMORY_LAT_BW_INFO
  );

/** Find an index of the specified domain ID in an array.

  @param [in]  DomainId         Domain ID to find.
  @param [in]  DomainIds        Pointer to an array with domain IDs.
  @param [in]  DomainIdCount    Number of elements in DomainIds array.
  @param [out] DomainIdIndex    Pointer where the index of the found ID is stored.
                                Can be NULL if the actual index doesn't matter.

  @retval EFI_SUCCESS   If DomainId was found
  @retval EFI_NOT_FOUND If DomainId wasn't found
**/
STATIC
EFI_STATUS
FindDomainIndex (
  IN  UINT32  DomainId,
  IN  UINT32  *DomainIds,
  IN  UINTN   DomainIdCount,
  OUT UINTN   *DomainIdIndex OPTIONAL
  )
{
  UINTN  Index;

  for (Index = 0; Index < DomainIdCount; Index++) {
    if (DomainIds[Index] == DomainId) {
      if (DomainIdIndex != NULL) {
        *DomainIdIndex = Index;
      }

      return EFI_SUCCESS;
    }
  }

  return EFI_NOT_FOUND;
}

/** Computes how many bytes are required to hold an ACPI HMAT SSLBI table.

  @param [in] InitiatorProximityDomainCount Number of initiator domains in an SSLBI record.
  @param [in] TargetProximityDomainCount    Number of target domains in an SSLBI record.

  @retval Bytes required to store SSLBI record.
**/
STATIC
UINT32
GetSslbiTableSize (
  IN UINT32  InitiatorProximityDomainCount,
  IN UINT32  TargetProximityDomainCount
  )
{
  return sizeof (UINT32) * InitiatorProximityDomainCount +
         sizeof (UINT32) * TargetProximityDomainCount +
         sizeof (UINT16) * InitiatorProximityDomainCount * TargetProximityDomainCount +
         sizeof (EFI_ACPI_6_4_HMAT_STRUCTURE_SYSTEM_LOCALITY_LATENCY_AND_BANDWIDTH_INFO);
}

/** Populate SSLBI record and return the actual size of the record to caller.

  @param [in]       CfgMgrProtocol          ConfigurationManager protocol.
  @param [in]       CmMemLatBwInfo          SSLBI record information.
  @param [in]       CmMemLatBwRelations     Array of relations to be populated into SSLBI.
  @param [in]       CmMemLatBwRelationCount Number of elements in the relations array.
  @param [in]       ProximityDomainCount    Maximum number of proximity domains, used to
                                            sanity check the relations array data.
  @param [in, out]  MemLatBwInfo            SSLBI to be populated. Caller is responsible for
                                            allocating memory area large enough to fit the data.
  @param [out]      RecordLength            Actual size of the SSLBI record in bytes,
                                            header + relations matrix.

  @retval EFI_INVALID_PARAMETER The relations matrix data is not valid: it's either spare
                                or number of initiators/targets larger than number of domains
                                in the system.
**/
STATIC
EFI_STATUS
AddMemLatBwInfo (
  IN      CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL   *CONST                           CfgMgrProtocol,
  IN      CONST CM_ARCH_COMMON_MEMORY_LAT_BW_INFO                                       *CmMemLatBwInfo,
  IN      CONST CM_ARCH_COMMON_PROXIMITY_DOMAIN_RELATION_INFO                           *CmMemLatBwRelations,
  IN            UINTN                                                                   CmMemLatBwRelationCount,
  IN            UINT32                                                                  ProximityDomainCount,
  IN  OUT       EFI_ACPI_6_4_HMAT_STRUCTURE_SYSTEM_LOCALITY_LATENCY_AND_BANDWIDTH_INFO  *MemLatBwInfo,
  OUT           UINTN                                                                   *RecordLength
  )
{
  UINTN       Index;
  UINT32      *InitiatorDomains;
  UINT32      InitiatorDomainCount;
  UINT32      *TargetDomains;
  UINT32      TargetDomainCount;
  EFI_STATUS  Status;
  UINT32      DomainId;
  UINTN       InitiatorIdx;
  UINTN       TargetIdx;
  UINT16      *Relations;
  UINT8       *TablePtr;

  ASSERT (CfgMgrProtocol != NULL);
  ASSERT (MemLatBwInfo != NULL);
  ASSERT (CmMemLatBwInfo != NULL);
  ASSERT (CmMemLatBwRelations != NULL);
  ASSERT (CmMemLatBwRelationCount > 0);
  ASSERT (ProximityDomainCount > 0);
  ASSERT (RecordLength != NULL);

  if (CmMemLatBwRelationCount == 0) {
    return EFI_INVALID_PARAMETER;
  }

  MemLatBwInfo->Type = EFI_ACPI_6_4_HMAT_TYPE_SYSTEM_LOCALITY_LATENCY_AND_BANDWIDTH_INFO;

  CopyMem (&MemLatBwInfo->Flags, &CmMemLatBwInfo->Flags, sizeof (UINT8));
  if ((MemLatBwInfo->Flags.MemoryHierarchy > EFI_ACPI_6_4_HMAT_MEMORY_HIERARCHY_L3_CACHE) ||
      (MemLatBwInfo->Flags.Reserved != 0))
  {
    return EFI_INVALID_PARAMETER;
  }

  MemLatBwInfo->DataType        = CmMemLatBwInfo->DataType;
  MemLatBwInfo->MinTransferSize = CmMemLatBwInfo->MinTransferSize;
  MemLatBwInfo->EntryBaseUnit   = CmMemLatBwInfo->EntryBaseUnit;

  TablePtr = (UINT8 *)MemLatBwInfo;

  TablePtr            += sizeof (EFI_ACPI_6_4_HMAT_STRUCTURE_SYSTEM_LOCALITY_LATENCY_AND_BANDWIDTH_INFO);
  InitiatorDomainCount = 0;
  InitiatorDomains     = (UINT32 *)TablePtr;

  // Append Initiator ProximityDomains.
  for (Index = 0; Index < CmMemLatBwRelationCount; Index++) {
    Status = GetProximityDomainId (
               CfgMgrProtocol,
               0,
               CmMemLatBwRelations[Index].FirstDomainToken,
               &DomainId
               );
    if (EFI_ERROR (Status)) {
      ASSERT_EFI_ERROR (Status);
      return Status;
    }

    if (FindDomainIndex (DomainId, InitiatorDomains, InitiatorDomainCount, NULL) == EFI_NOT_FOUND) {
      if (InitiatorDomainCount >= ProximityDomainCount) {
        return EFI_INVALID_PARAMETER;
      }

      InitiatorDomains[InitiatorDomainCount] = DomainId;
      InitiatorDomainCount++;
    }
  }

  // Append Target ProximityDomains.
  TargetDomainCount = 0;
  TargetDomains     = InitiatorDomains + InitiatorDomainCount;
  for (Index = 0; Index < CmMemLatBwRelationCount; Index++) {
    Status = GetProximityDomainId (
               CfgMgrProtocol,
               0,
               CmMemLatBwRelations[Index].SecondDomainToken,
               &DomainId
               );
    if (EFI_ERROR (Status)) {
      ASSERT_EFI_ERROR (Status);
      return Status;
    }

    if (FindDomainIndex (DomainId, TargetDomains, TargetDomainCount, NULL) == EFI_NOT_FOUND) {
      if (TargetDomainCount >= ProximityDomainCount) {
        return EFI_INVALID_PARAMETER;
      }

      TargetDomains[TargetDomainCount] = DomainId;
      TargetDomainCount++;
    }
  }

  DEBUG ((
    DEBUG_INFO,
    "HMAT: Lat/Bw info table dimensions: %d x %d\n",
    InitiatorDomainCount,
    TargetDomainCount
    ));

  if (InitiatorDomainCount * TargetDomainCount != CmMemLatBwRelationCount) {
    DEBUG ((
      DEBUG_ERROR,
      "HMAT: Lat/Bw info table dimensions %dx%d do not match elements count %d (should be %d)\n",
      InitiatorDomainCount,
      TargetDomainCount,
      CmMemLatBwRelationCount,
      InitiatorDomainCount * TargetDomainCount
      ));
    return EFI_INVALID_PARAMETER;
  }

  Relations = (UINT16 *)(TargetDomains + TargetDomainCount);
  for (Index = 0; Index < CmMemLatBwRelationCount; Index++) {
    Status = GetProximityDomainId (
               CfgMgrProtocol,
               0,
               CmMemLatBwRelations[Index].FirstDomainToken,
               &DomainId
               );
    if (EFI_ERROR (Status)) {
      ASSERT_EFI_ERROR (Status);
      return Status;
    }

    Status = FindDomainIndex (DomainId, InitiatorDomains, InitiatorDomainCount, &InitiatorIdx);
    ASSERT_EFI_ERROR (Status);

    Status = GetProximityDomainId (
               CfgMgrProtocol,
               0,
               CmMemLatBwRelations[Index].SecondDomainToken,
               &DomainId
               );
    if (EFI_ERROR (Status)) {
      ASSERT_EFI_ERROR (Status);
      return Status;
    }

    Status = FindDomainIndex (DomainId, TargetDomains, TargetDomainCount, &TargetIdx);
    ASSERT_EFI_ERROR (Status);

    if (CmMemLatBwRelations[Index].Relation > MAX_UINT16) {
      DEBUG ((
        DEBUG_ERROR,
        "HMAT: relation value of element %d does not fit in 16 bits: %d\n",
        Index,
        CmMemLatBwRelations[Index].Relation
        ));
      return EFI_INVALID_PARAMETER;
    }

    Relations[InitiatorIdx * TargetDomainCount + TargetIdx] = CmMemLatBwRelations[Index].Relation & 0xFFFF;
  }

  // Update record info
  MemLatBwInfo->Length                            = GetSslbiTableSize (InitiatorDomainCount, TargetDomainCount);
  MemLatBwInfo->NumberOfInitiatorProximityDomains = InitiatorDomainCount;
  MemLatBwInfo->NumberOfTargetProximityDomains    = TargetDomainCount;
  *RecordLength                                   = MemLatBwInfo->Length;

  return EFI_SUCCESS;
}

/** Add the MemProxDomainAttr Information record to the HMAT Table.

  @param [in]  CfgMgrProtocol                   Pointer to the Configuration Manager Protocol Interface.
  @param [in]  MemProximityDomainAttrInfo       Pointer to a Proximity Domain Attribute array in an HMAT table.
                                                The caller is responsible for allocating enough memory to fit
                                                the data.
  @param [in]  CmMemProximityDomainAttrInfo     Pointer ConfigurationManager objects array with Domain Attribute data.
  @param [in]  MemProximityDomainAttrCount      Number of entries in CmMemProximityDomainAttrInfo.

  @retval EFI_SUCCESS if attributes were added
  @retval error code otherwise
**/
STATIC
EFI_STATUS
AddMemProximityDomainAttrInfo (
  IN  CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL *CONST               CfgMgrProtocol,
  IN  EFI_ACPI_6_4_HMAT_STRUCTURE_MEMORY_PROXIMITY_DOMAIN_ATTRIBUTES  *MemProximityDomainAttrInfo,
  IN  CONST CM_ARCH_COMMON_MEMORY_PROXIMITY_DOMAIN_ATTR_INFO          *CmMemProximityDomainAttrInfo,
  IN  UINT32                                                          MemProximityDomainAttrCount
  )
{
  EFI_STATUS  Status;

  if (MemProximityDomainAttrCount == 0) {
    return EFI_SUCCESS;
  }

  ASSERT (MemProximityDomainAttrInfo != NULL);
  ASSERT (CmMemProximityDomainAttrInfo != NULL);

  while (MemProximityDomainAttrCount-- != 0) {
    MemProximityDomainAttrInfo->Type   = EFI_ACPI_6_4_HMAT_TYPE_MEMORY_PROXIMITY_DOMAIN_ATTRIBUTES;
    MemProximityDomainAttrInfo->Length = sizeof (EFI_ACPI_6_4_HMAT_STRUCTURE_MEMORY_PROXIMITY_DOMAIN_ATTRIBUTES);
    CopyMem (&MemProximityDomainAttrInfo->Flags, &CmMemProximityDomainAttrInfo->Flags, sizeof (UINT16));
    if (MemProximityDomainAttrInfo->Flags.Reserved != 0) {
      return EFI_INVALID_PARAMETER;
    }

    if (CmMemProximityDomainAttrInfo->InitiatorProximityDomain
        == CmMemProximityDomainAttrInfo->MemoryProximityDomain)
    {
      return EFI_INVALID_PARAMETER;
    }

    Status = GetProximityDomainId (
               CfgMgrProtocol,
               0,
               CmMemProximityDomainAttrInfo->InitiatorProximityDomain,
               &MemProximityDomainAttrInfo->InitiatorProximityDomain
               );
    if (EFI_ERROR (Status)) {
      ASSERT_EFI_ERROR (Status);
      return Status;
    }

    Status = GetProximityDomainId (
               CfgMgrProtocol,
               0,
               CmMemProximityDomainAttrInfo->MemoryProximityDomain,
               &MemProximityDomainAttrInfo->MemoryProximityDomain
               );
    if (EFI_ERROR (Status)) {
      ASSERT_EFI_ERROR (Status);
      return Status;
    }

    MemProximityDomainAttrInfo++;
    CmMemProximityDomainAttrInfo++;
  }

  return EFI_SUCCESS;
}

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

  @param [in]      This           Pointer to the ACPI 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 ACPI Table.

  @retval EFI_SUCCESS           The resources were freed successfully.
  @retval EFI_INVALID_PARAMETER The table pointer is NULL or invalid.
**/
STATIC
EFI_STATUS
EFIAPI
FreeHmatTable (
  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) || (*Table == NULL)) {
    DEBUG ((DEBUG_ERROR, "ERROR: HMAT: Invalid Table Pointer\n"));
    ASSERT ((Table != NULL) && (*Table != NULL));
    return EFI_INVALID_PARAMETER;
  }

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

  return EFI_SUCCESS;
}

/** Construct the HMAT ACPI table.

  This function invokes the Configuration Manager protocol interface
  to get the required hardware information for generating the ACPI
  table.

  If this function allocates any resources then they must be freed
  in the FreeXXXXTableResourcesEx function.

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

  @retval EFI_SUCCESS            Table generated successfully.
  @retval EFI_BAD_BUFFER_SIZE    The size returned by the Configuration
                                 Manager is less than the Object size for
                                 the requested object.
  @retval EFI_INVALID_PARAMETER  A parameter is invalid.
  @retval EFI_NOT_FOUND          Could not find information.
  @retval EFI_OUT_OF_RESOURCES   Could not allocate memory.
**/
STATIC
EFI_STATUS
EFIAPI
BuildHmatTable (
  IN  CONST ACPI_TABLE_GENERATOR                            *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_STATUS                                                Status;
  UINTN                                                     TableSize;
  UINT32                                                    MemProximityDomainAttrCount;
  UINT32                                                    MemLatBwCount;
  CM_ARCH_COMMON_MEMORY_PROXIMITY_DOMAIN_ATTR_INFO          *MemProximityDomainAttrInfo;
  CM_ARCH_COMMON_MEMORY_LAT_BW_INFO                         *MemLatBwInfos;
  CM_ARCH_COMMON_PROXIMITY_DOMAIN_RELATION_INFO             *MemLatBwRelations;
  CM_ARCH_COMMON_PROXIMITY_DOMAIN_INFO                      *ProximityDomainInfo;
  UINT32                                                    ProximityDomainInfoCount;
  UINT32                                                    MemLatBwRelationsCount;
  UINTN                                                     MemProximityDomainAttrOffset;
  UINTN                                                     MemLatBwOffset;
  UINT32                                                    Index;
  UINT64                                                    MaxLatBwSize;
  UINTN                                                     LatBwEntrySize;
  EFI_ACPI_6_4_HETEROGENEOUS_MEMORY_ATTRIBUTE_TABLE_HEADER  *Hmat;
  VOID                                                      *FinalTable;

  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: HMAT: Requested table revision = %d, is not supported."
      "Supported table revision: Minimum = %d, Maximum = %d\n",
      AcpiTableInfo->AcpiTableRevision,
      This->MinAcpiTableRevision,
      This->AcpiTableRevision
      ));
    return EFI_INVALID_PARAMETER;
  }

  *Table = NULL;

  Status = GetEArchCommonObjProximityDomainInfo (
             CfgMgrProtocol,
             CM_NULL_TOKEN,
             &ProximityDomainInfo,
             &ProximityDomainInfoCount
             );

  if (EFI_ERROR (Status)) {
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: HMAT: Failed to get ProximityDomainInfo. Status = %r\n",
      Status
      ));
    goto error_handler;
  }

  MemProximityDomainAttrCount = 0;

  Status = GetEArchCommonObjMemoryProximityDomainAttrInfo (
             CfgMgrProtocol,
             CM_NULL_TOKEN,
             &MemProximityDomainAttrInfo,
             &MemProximityDomainAttrCount
             );

  if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) {
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: HMAT: Failed to get MemProximityDomain Info. Status = %r\n",
      Status
      ));
    goto error_handler;
  }

  MemLatBwCount = 0;

  Status = GetEArchCommonObjMemoryLatBwInfo (
             CfgMgrProtocol,
             CM_NULL_TOKEN,
             &MemLatBwInfos,
             &MemLatBwCount
             );

  if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) {
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: HMAT: Failed to get MemLatBwInfo. Status = %r\n",
      Status
      ));
    goto error_handler;
  }

  TableSize = sizeof (EFI_ACPI_6_4_HETEROGENEOUS_MEMORY_ATTRIBUTE_TABLE_HEADER);

  MemProximityDomainAttrOffset = TableSize;
  TableSize                   +=
    (sizeof (EFI_ACPI_6_4_HMAT_STRUCTURE_MEMORY_PROXIMITY_DOMAIN_ATTRIBUTES) * MemProximityDomainAttrCount);

  MemLatBwOffset = TableSize;

  // The largest SSLBI entry is full matrix where all proximity
  // domains act as initiators and targets. Go for max case, adjust size later.
  MaxLatBwSize = GetSslbiTableSize (ProximityDomainInfoCount, ProximityDomainInfoCount);
  TableSize   += MemLatBwCount * MaxLatBwSize;

  if (TableSize > MAX_UINT32) {
    return EFI_INVALID_PARAMETER;
  }

  // Allocate the Buffer for HMAT table
  *Table = (EFI_ACPI_DESCRIPTION_HEADER *)AllocateZeroPool (TableSize);

  if (*Table == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: HMAT: Failed to allocate memory for HMAT Table, Size = %d," \
      " Status = %r\n",
      TableSize,
      Status
      ));
    goto error_handler;
  }

  Hmat = (EFI_ACPI_6_4_HETEROGENEOUS_MEMORY_ATTRIBUTE_TABLE_HEADER *)*Table;

  // Build HMAT table.
  Status = AddAcpiHeader (
             CfgMgrProtocol,
             This,
             &Hmat->Header,
             AcpiTableInfo,
             (UINT32)TableSize
             );
  if (EFI_ERROR (Status)) {
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: HMAT: Failed to add ACPI header. Status = %r\n",
      Status
      ));
    goto error_handler;
  }

  Status = AddMemProximityDomainAttrInfo (
             CfgMgrProtocol,
             (EFI_ACPI_6_4_HMAT_STRUCTURE_MEMORY_PROXIMITY_DOMAIN_ATTRIBUTES *)((UINT8 *)Hmat + MemProximityDomainAttrOffset),
             MemProximityDomainAttrInfo,
             MemProximityDomainAttrCount
             );

  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "%a: Failed to add Proximity Domain Attr structures: %r\n", __func__, Status));
    goto error_handler;
  }

  for (Index = 0; Index < MemLatBwCount; Index++) {
    Status = GetEArchCommonObjProximityDomainRelationInfo (
               CfgMgrProtocol,
               MemLatBwInfos[Index].RelativeDistanceArray,
               &MemLatBwRelations,
               &MemLatBwRelationsCount
               );
    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "%a: Failed to get relative distances array: %r\n", __func__, Status));
      goto error_handler;
    }

    Status = AddMemLatBwInfo (
               CfgMgrProtocol,
               &MemLatBwInfos[Index],
               MemLatBwRelations,
               MemLatBwRelationsCount,
               ProximityDomainInfoCount,
               (EFI_ACPI_6_4_HMAT_STRUCTURE_SYSTEM_LOCALITY_LATENCY_AND_BANDWIDTH_INFO *)((UINT8 *)Hmat + MemLatBwOffset),
               &LatBwEntrySize
               );
    if (EFI_ERROR (Status)) {
      DEBUG ((DEBUG_ERROR, "%a: Failed to add Lat/Bw info: %r\n", __func__, Status));
      goto error_handler;
    }

    MemLatBwOffset += LatBwEntrySize;
  }

  // Update header with the actual length of the generated table
  // and re-allocate it to an actual size
  Hmat->Header.Length = (UINT32)MemLatBwOffset;
  FinalTable          = AllocateCopyPool (Hmat->Header.Length, *Table);
  if (FinalTable == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: HMAT: Failed to allocate memory for HMAT Table, Size = %d," \
      " Status = %r\n",
      Hmat->Header.Length,
      Status
      ));
    goto error_handler;
  }

  FreePool (*Table);
  *Table = FinalTable;

  return Status;

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

  return Status;
}

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

/** The interface for the HMAT Table Generator.
*/
STATIC
CONST
ACPI_TABLE_GENERATOR  HmatGenerator = {
  // Generator ID
  CREATE_STD_ACPI_TABLE_GEN_ID (EStdAcpiTableIdHmat),
  // Generator Description
  L"ACPI.STD.HMAT.GENERATOR",
  // ACPI Table Signature
  EFI_ACPI_6_4_HETEROGENEOUS_MEMORY_ATTRIBUTE_TABLE_SIGNATURE,
  // ACPI Table Revision supported by this Generator
  EFI_ACPI_6_4_HETEROGENEOUS_MEMORY_ATTRIBUTE_TABLE_REVISION,
  // Minimum supported ACPI Table Revision
  EFI_ACPI_6_4_HETEROGENEOUS_MEMORY_ATTRIBUTE_TABLE_REVISION,
  // Creator ID
  TABLE_GENERATOR_CREATOR_ID_ARM,
  // Creator Revision
  HMAT_GENERATOR_REVISION,
  // Build table function. Use the extended version instead.
  BuildHmatTable,
  // Free table function. Use the extended version instead.
  FreeHmatTable,
  // Extended Build table function.
  NULL,
  // Extended free function.
  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 for the Table ID
                                is already registered.
**/
EFI_STATUS
EFIAPI
AcpiHmatLibConstructor (
  IN  EFI_HANDLE        ImageHandle,
  IN  EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS  Status;

  Status = RegisterAcpiTableGenerator (&HmatGenerator);
  DEBUG ((DEBUG_INFO, "HMAT: 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
AcpiHmatLibDestructor (
  IN  EFI_HANDLE        ImageHandle,
  IN  EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS  Status;

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