summaryrefslogtreecommitdiff
path: root/DynamicTablesPkg/Library/Acpi/Common/AcpiMsctLib/MsctGenerator.c
blob: 736f8788de51bccce85229de17f4771951f133e0 (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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
/** @file
  ACPI Maximum System Characteristics Table (MSCT) Generator

  Copyright (C) 2026 Advanced Micro Devices, Inc. All rights reserved.

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

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

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

#include "MsctGenerator.h"

/**
  Standard MSCT Generator

  Requirements:
    The following Configuration Manager Object(s) are used by this Generator:
    - EArchCommonObjMsctMaxPhysicalAddrInfo (REQUIRED)
    - EArchCommonObjMemoryAffinityInfo (OPTIONAL)
    - EArmObjGicCInfo (OPTIONAL)
    - EX64ObjLocalApicX2ApicAffinityInfo (OPTIONAL)
*/

/**
  This macro expands to a function that retrieves the maximum physical address
  information from the Configuration Manager.
*/
GET_OBJECT_LIST (
  EObjNameSpaceArchCommon,
  EArchCommonObjMsctMaxPhysicalAddrInfo,
  CM_ARCH_COMMON_MSCT_MAX_PHYSICAL_ADDR_INFO
  );

/**
  This macro expands to a function that retrieves the Memory Affinity
  information from the Configuration Manager.
*/
GET_OBJECT_LIST (
  EObjNameSpaceArchCommon,
  EArchCommonObjMemoryAffinityInfo,
  CM_ARCH_COMMON_MEMORY_AFFINITY_INFO
  );

/** Add Id to Seen if not already present, incrementing *UniqueCount on success.

  Performs a linear scan of Seen[0..*UniqueCount-1]. If Id is not found, it is
  appended at Seen[*UniqueCount] and *UniqueCount is incremented. The caller
  must ensure the Seen array has capacity for at least one additional element.

  @param [in]      Id           Domain identifier to record.
  @param [in, out] Seen         Array of already-recorded unique IDs.
  @param [in, out] UniqueCount  Number of valid entries in Seen on entry;
                                incremented by one when Id is newly added.

**/
STATIC
VOID
AddIfUnique (
  IN     UINT32  Id,
  IN OUT UINT32  *Seen,
  IN OUT UINT32  *UniqueCount
  )
{
  UINT32  SeenIndex;

  for (SeenIndex = 0; SeenIndex < *UniqueCount; SeenIndex++) {
    if (Seen[SeenIndex] == Id) {
      return;
    }
  }

  Seen[(*UniqueCount)++] = Id;
}

/** Comparison function for sorting Maximum Proximity Domain.

  @param [in]  Left    Pointer to the left Proximity Domain structure.
  @param [in]  Right   Pointer to the right Proximity Domain structure.

  @retval -1 If Left proximity domain < Right proximity domain
  @retval  0 If Left proximity domain == Right proximity domain
  @retval  1 If Left proximity domain > Right proximity domain
**/
INTN
EFIAPI
SortByProximityDomainRange (
  IN  CONST VOID  *Left,
  IN  CONST VOID  *Right
  )
{
  CONST EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE  *LeftInfo;
  CONST EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE  *RightInfo;

  LeftInfo  = (CONST EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE *)Left;
  RightInfo = (CONST EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE *)Right;

  if (LeftInfo->ProximityDomainRangeLow < RightInfo->ProximityDomainRangeLow) {
    return -1;
  } else if (LeftInfo->ProximityDomainRangeLow > RightInfo->ProximityDomainRangeLow) {
    return 1;
  } else {
    // ProximityDomainRangeLow are equal, compare ProximityDomainRangeHigh
    if (LeftInfo->ProximityDomainRangeHigh < RightInfo->ProximityDomainRangeHigh) {
      return -1;
    } else if (LeftInfo->ProximityDomainRangeHigh > RightInfo->ProximityDomainRangeHigh) {
      return 1;
    } else {
      return 0;
    }
  }
}

/** Get processor domain information.

  @param [in]      CfgMgrProtocol       Pointer to the Configuration Manager
                                        Protocol.
  @param [out]     ProcDomainInfo       Pointer to the processor domain information.
  @param [out]     ProcDomainInfoCount  Pointer to the count of processor domain
                                        information structures.

  @retval EFI_SUCCESS           Operation completed successfully.
  @retval EFI_INVALID_PARAMETER A parameter is invalid.
  @retval EFI_OUT_OF_RESOURCES  Memory allocation failed.
  @retval Other                 Other EFI_STATUS error from called functions.
**/
EFI_STATUS
EFIAPI
GetProcessorDomainInfo (
  IN  CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL  *CONST                 CfgMgrProtocol,
  OUT       EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE  **ProcDomainInfo,
  OUT       UINT32                                                       *ProcDomainInfoCount
  )
{
  EFI_STATUS  Status;
  UINT32      *ProcDomainArch;
  UINT32      ProcDomainArchCount;
  UINT32      Index;
  UINT32      ProcDomainIndex;
  UINT32      ProcDomainCount;

  EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE  *ProcDomain;

  if ((CfgMgrProtocol == NULL) ||
      (ProcDomainInfo == NULL) ||
      (ProcDomainInfoCount == NULL))
  {
    ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);
    return EFI_INVALID_PARAMETER;
  }

  ProcDomain          = NULL;
  ProcDomainArch      = NULL;
  ProcDomainArchCount = 0;

  Status = GetArchProcessorDomainInfo (
             CfgMgrProtocol,
             &ProcDomainArch,
             &ProcDomainArchCount
             );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  if ((ProcDomainArchCount == 0) || (ProcDomainArch == NULL)) {
    return EFI_NOT_FOUND;
  }

  ProcDomainCount = 0;
  ProcDomain      = NULL;
  for (Index = 0; Index < ProcDomainArchCount; Index++) {
    for (ProcDomainIndex = 0; ProcDomainIndex < ProcDomainCount; ProcDomainIndex++) {
      if ((ProcDomain != NULL) &&
          (ProcDomain[ProcDomainIndex].ProximityDomainRangeLow == ProcDomainArch[Index]))
      {
        ProcDomain[ProcDomainIndex].MaximumProcessorCapacity++;
        break;
      }
    }

    if (ProcDomainIndex == ProcDomainCount) {
      ProcDomain = ReallocatePool (
                     sizeof (EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE) * ProcDomainCount,
                     sizeof (EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE) * (ProcDomainCount + 1),
                     ProcDomain
                     );
      if (ProcDomain == NULL) {
        DEBUG ((
          DEBUG_ERROR,
          "ERROR: MSCT: Failed to reallocate memory for Processor Domain Info.\n"
          ));
        Status = EFI_OUT_OF_RESOURCES;
        ASSERT_EFI_ERROR (Status);
        goto error_handler;
      }

      ProcDomain[ProcDomainCount].Revision                 = EFI_ACPI_6_5_MAXIMUM_SYSTEM_CHARACTERISTICS_TABLE_REVISION;
      ProcDomain[ProcDomainCount].Length                   = sizeof (EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE);
      ProcDomain[ProcDomainCount].ProximityDomainRangeLow  = ProcDomainArch[Index];
      ProcDomain[ProcDomainCount].ProximityDomainRangeHigh = ProcDomainArch[Index];
      ProcDomain[ProcDomainCount].MaximumProcessorCapacity = 1;
      ProcDomainCount++;
    }
  }

  *ProcDomainInfo      = ProcDomain;
  *ProcDomainInfoCount = ProcDomainCount;

  FreePool (ProcDomainArch);
  return EFI_SUCCESS;

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

  if (ProcDomainArch != NULL) {
    FreePool (ProcDomainArch);
  }

  return Status;
}

/** Get memory domain information.

  @param [in]      CfgMgrProtocol      Pointer to the Configuration Manager
                                        Protocol.
  @param [out]     MemDomainInfo       Pointer to the memory domain information.
  @param [out]     MemDomainInfoCount  Pointer to the count of memory domain
                                        information structures.

  @retval EFI_SUCCESS           Operation completed successfully.
  @retval EFI_INVALID_PARAMETER A parameter is invalid.
  @retval EFI_OUT_OF_RESOURCES  Memory allocation failed.
  @retval Other                 Other EFI_STATUS error from called functions.
**/
EFI_STATUS
EFIAPI
GetMemoryDomainInfo (
  IN  CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL  *CONST                 CfgMgrProtocol,
  OUT       EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE  **MemDomainInfo,
  OUT       UINT32                                                       *MemDomainInfoCount
  )
{
  EFI_STATUS  Status;
  UINT32      Index;
  UINT32      MemAffCount;
  UINT32      ProximityDomain;

  CM_ARCH_COMMON_MEMORY_AFFINITY_INFO                          *MemAffInfo;
  EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE  *MemDomain;

  if ((CfgMgrProtocol == NULL) ||
      (MemDomainInfo == NULL) ||
      (MemDomainInfoCount == NULL))
  {
    ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);
    return EFI_INVALID_PARAMETER;
  }

  MemDomain  = NULL;
  MemAffInfo = NULL;

  /// Get Memory Affinity Information
  Status = GetEArchCommonObjMemoryAffinityInfo (
             CfgMgrProtocol,
             CM_NULL_TOKEN,
             &MemAffInfo,
             &MemAffCount
             );
  if (EFI_ERROR (Status)) {
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: MSCT: Failed to get Memory Affinity Info. Status = %r\n",
      Status
      ));
    ASSERT_EFI_ERROR (Status);
    return Status;
  }

  if ((MemAffInfo == NULL) || (MemAffCount == 0)) {
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: MSCT: Invalid Memory Affinity Info data.\n"
      ));
    Status = EFI_NOT_FOUND;
    return Status;
  }

  MemDomain = AllocateZeroPool (
                MemAffCount * sizeof (EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE)
                );
  if (MemDomain == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: MSCT: Failed to allocate memory for Memory Domain Info. Status = %r\n",
      Status
      ));
    ASSERT_EFI_ERROR (Status);
    return Status;
  }

  for (Index = 0; Index < MemAffCount; Index++) {
    Status = GetProximityDomainId (
               CfgMgrProtocol,
               MemAffInfo[Index].ProximityDomain,
               MemAffInfo[Index].ProximityDomainToken,
               &ProximityDomain
               );
    if (EFI_ERROR (Status)) {
      DEBUG ((
        DEBUG_ERROR,
        "MSCT: Cannot obtain memory Proximity ID. Status = %r\n",
        Status
        ));
      ASSERT_EFI_ERROR (Status);
      goto return_handler;
    }

    MemDomain[Index].Revision = EFI_ACPI_6_5_MAXIMUM_SYSTEM_CHARACTERISTICS_TABLE_REVISION;
    MemDomain[Index].Length   = sizeof (EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE);

    MemDomain[Index].ProximityDomainRangeLow  = ProximityDomain;
    MemDomain[Index].ProximityDomainRangeHigh = ProximityDomain;
    MemDomain[Index].MaximumMemoryCapacity    = MemAffInfo[Index].Length;
  }

  PerformQuickSort (
    MemDomain,
    MemAffCount,
    sizeof (EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE),
    SortByProximityDomainRange
    );

  *MemDomainInfo      = MemDomain;
  *MemDomainInfoCount = MemAffCount;
  return EFI_SUCCESS;

return_handler:
  if (MemDomain != NULL) {
    FreePool (MemDomain);
  }

  return Status;
}

/** Extract unique memory proximity domain IDs from a populated MemDomainInfo array.

  The input array must already be sorted by ProximityDomainRangeLow, as
  produced by GetMemoryDomainInfo(). Consecutive entries with the same
  ProximityDomainRangeLow value are treated as duplicates and collapsed to a
  single ID. The caller is responsible for freeing the returned array with
  FreePool().

  @param [in]      CfgMgrProtocol    Pointer to the Configuration Manager
                                     Protocol. Reserved for future use;
                                     must not be NULL.
  @param [in]      MemDomainInfo     Sorted array of memory domain information
                                     structures, as returned by
                                     GetMemoryDomainInfo(). May be NULL only
                                     when MemDomainInfoCount is 0.
  @param [in]      MemDomainInfoCount  Number of entries in MemDomainInfo.
  @param [out]     MemDomainIds      On success, pointer to a caller-owned
                                     array of unique memory proximity domain
                                     IDs. Must be freed by the caller with
                                     FreePool(). Set to NULL when count is 0.
  @param [out]     MemDomainIdsCount  On success, number of entries in
                                      MemDomainIds (cardinal count of unique
                                      memory proximity domains).

  @retval EFI_SUCCESS           Operation completed successfully.
  @retval EFI_INVALID_PARAMETER A parameter is invalid.
  @retval EFI_NOT_FOUND         MemDomainInfoCount is 0 or MemDomainInfo is NULL.
  @retval EFI_OUT_OF_RESOURCES  Memory allocation failed.
**/
STATIC
EFI_STATUS
EFIAPI
GetUniqueMemoryDomainInfo (
  IN  CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL  *CONST                         CfgMgrProtocol,
  IN  CONST EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE  *CONST  MemDomainInfo,
  IN        UINT32                                                               MemDomainInfoCount,
  OUT       UINT32                                                               **MemDomainIds,
  OUT       UINT32                                                               *MemDomainIdsCount
  )
{
  UINT32  Index;
  UINT32  UniqueCount;
  UINT32  *UniqueIds;

  if ((CfgMgrProtocol == NULL) ||
      (MemDomainIds == NULL) ||
      (MemDomainIdsCount == NULL))
  {
    ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);
    return EFI_INVALID_PARAMETER;
  }

  if ((MemDomainInfo == NULL) || (MemDomainInfoCount == 0)) {
    *MemDomainIds      = NULL;
    *MemDomainIdsCount = 0;
    return EFI_NOT_FOUND;
  }

  // Allocate worst-case: all entries could have distinct proximity domain IDs.
  UniqueIds = AllocateZeroPool (sizeof (UINT32) * MemDomainInfoCount);
  if (UniqueIds == NULL) {
    ASSERT_EFI_ERROR (EFI_OUT_OF_RESOURCES);
    return EFI_OUT_OF_RESOURCES;
  }

  // The input array is sorted by ProximityDomainRangeLow; scan linearly and
  // record a new entry only when the ID differs from the previous one.
  UniqueCount  = 0;
  UniqueIds[0] = MemDomainInfo[0].ProximityDomainRangeLow;
  UniqueCount  = 1;
  for (Index = 1; Index < MemDomainInfoCount; Index++) {
    if (MemDomainInfo[Index].ProximityDomainRangeLow !=
        UniqueIds[UniqueCount - 1])
    {
      UniqueIds[UniqueCount++] = MemDomainInfo[Index].ProximityDomainRangeLow;
    }
  }

  *MemDomainIds      = UniqueIds;
  *MemDomainIdsCount = UniqueCount;
  return EFI_SUCCESS;
}

/** Compute the total number of unique proximity domain IDs across the
    proximity-domain-bearing sets (memory and processor) in the system.

  The two input sets may overlap (the same proximity domain ID can host both
  CPUs and memory). This function unions the two sets and returns the cardinal
  count of distinct IDs, which maps directly to the ACPI MSCT
  MaximumNumberOfProximityDomains field (0-based, so the caller subtracts 1).

  Clock domains are intentionally excluded: per ACPI 6.5/6.6 section 5.2.19,
  clock domains form a separate identifier namespace (_CDM) from proximity
  domains (_PXM) and are reported via MaximumNumberOfClockDomains.

  @param [in]   MemDomainIds         Array of unique memory proximity domain
                                     IDs, as returned by GetUniqueMemoryDomainInfo().
                                     May be NULL when MemDomainIdsCount is 0.
  @param [in]   MemDomainIdsCount    Number of entries in MemDomainIds.
  @param [in]   ProcDomainInfo       Array of unique processor proximity domain
                                     structures, as returned by
                                     GetProcessorDomainInfo(). Each entry's
                                     ProximityDomainRangeLow is the domain ID.
                                     May be NULL when ProcDomainInfoCount is 0.
  @param [in]   ProcDomainInfoCount  Number of entries in ProcDomainInfo.
  @param [out]  UniqueDomainCount    On success, the cardinal count of unique
                                     proximity domain IDs across both sets.

  @retval EFI_SUCCESS           Operation completed successfully.
  @retval EFI_INVALID_PARAMETER UniqueDomainCount is NULL.
  @retval EFI_OUT_OF_RESOURCES  Memory allocation failed.
**/
STATIC
EFI_STATUS
EFIAPI
GetUniqueTotalDomainCount (
  IN  CONST UINT32                                                       *MemDomainIds,
  IN        UINT32                                                       MemDomainIdsCount,
  IN  CONST EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE  *ProcDomainInfo,
  IN        UINT32                                                       ProcDomainInfoCount,
  OUT       UINT32                                                       *UniqueDomainCount
  )
{
  UINT32  MaxTotal;
  UINT32  UniqueCount;
  UINT32  Index;
  UINT32  *Seen;

  if (UniqueDomainCount == NULL) {
    ASSERT_EFI_ERROR (EFI_INVALID_PARAMETER);
    return EFI_INVALID_PARAMETER;
  }

  // Worst-case: all IDs are distinct across both sets.
  MaxTotal = MemDomainIdsCount + ProcDomainInfoCount;
  if (MaxTotal == 0) {
    *UniqueDomainCount = 0;
    return EFI_SUCCESS;
  }

  Seen = AllocateZeroPool (sizeof (UINT32) * MaxTotal);
  if (Seen == NULL) {
    ASSERT_EFI_ERROR (EFI_OUT_OF_RESOURCES);
    return EFI_OUT_OF_RESOURCES;
  }

  UniqueCount = 0;

  // Union memory domain IDs.
  for (Index = 0; Index < MemDomainIdsCount; Index++) {
    AddIfUnique (MemDomainIds[Index], Seen, &UniqueCount);
  }

  // Union processor domain IDs (ProximityDomainRangeLow is the unique ID per entry).
  for (Index = 0; Index < ProcDomainInfoCount; Index++) {
    AddIfUnique (ProcDomainInfo[Index].ProximityDomainRangeLow, Seen, &UniqueCount);
  }

  FreePool (Seen);
  *UniqueDomainCount = UniqueCount;
  return EFI_SUCCESS;
}

/** Update the MSCT Table with configuration data.

  @param [in]      CfgMgrProtocol      Pointer to the Configuration Manager
                                        Protocol.
  @param [out]     AcpiMsctTable       Pointer to the ACPI MSCT table.

  @retval EFI_SUCCESS           Operation completed successfully.
  @retval EFI_INVALID_PARAMETER A parameter is invalid.
  @retval EFI_OUT_OF_RESOURCES  Memory allocation failed.
  @retval Other                 Other EFI_STATUS error from called functions.
**/
EFI_STATUS
EFIAPI
UpdateMsctTable (
  IN  CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL  *CONST              CfgMgrProtocol,
  OUT       EFI_ACPI_6_5_MAXIMUM_SYSTEM_CHARACTERISTICS_TABLE_HEADER  **AcpiMsctTable
  )
{
  EFI_STATUS  Status;
  UINT32      ClockDomainIdsCount;
  UINT32      *MemDomainIds;
  UINT32      MemDomainIdsCount;
  UINT32      MaxProximityDomain;
  UINT32      DomainInfoCount;
  UINT32      DomainInfoIndex;
  UINT32      MemDomainInfoCount;
  UINT32      MemIndex;
  UINT32      MemNextIndex;
  UINT32      ProcDomainInfoCount;
  UINT32      ProcIndex;
  UINT32      TableSize;
  UINT8       *DomainInfoPtr;

  CM_ARCH_COMMON_MSCT_MAX_PHYSICAL_ADDR_INFO                   *MaxPhysAddrInfo;
  EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE  *DomainInfo;
  EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE  *MemDomainInfo;
  EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE  *ProcDomainInfo;
  EFI_ACPI_6_5_MAXIMUM_SYSTEM_CHARACTERISTICS_TABLE_HEADER     *MsctTable;

  MsctTable       = NULL;
  MemDomainInfo   = NULL;
  ProcDomainInfo  = NULL;
  DomainInfo      = NULL;
  DomainInfoPtr   = NULL;
  MaxPhysAddrInfo = NULL;
  MemDomainIds    = NULL;

  MemDomainInfoCount  = 0;
  ProcDomainInfoCount = 0;
  DomainInfoCount     = 0;
  DomainInfoIndex     = 0;
  MaxProximityDomain  = 0;
  MemDomainIdsCount   = 0;

  Status = GetEArchCommonObjMsctMaxPhysicalAddrInfo (
             CfgMgrProtocol,
             CM_NULL_TOKEN,
             &MaxPhysAddrInfo,
             NULL
             );
  if (EFI_ERROR (Status)) {
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: MSCT: Failed to get Max Physical Address Info. Status = %r\n",
      Status
      ));
    goto error_handler;
  }

  ClockDomainIdsCount = 0;
  Status              = GetArchClockDomainInfo (
                          CfgMgrProtocol,
                          &ClockDomainIdsCount
                          );
  if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) {
    goto error_handler;
  }

  if ((!EFI_ERROR (Status)) && (ClockDomainIdsCount == 0)) {
    // GetArchClockDomainInfo() succeeded but returned count=0, which is a
    // Configuration Manager error — a successful query must report at least 1
    // clock domain. Platforms without _CDM objects should return EFI_NOT_FOUND.
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: MSCT: Clock domain query succeeded but returned count 0.\n"
      ));
    Status = EFI_INVALID_PARAMETER;
    ASSERT_EFI_ERROR (Status);
    goto error_handler;
  }

  Status = GetMemoryDomainInfo (
             CfgMgrProtocol,
             &MemDomainInfo,
             &MemDomainInfoCount
             );
  if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) {
    goto error_handler;
  }

  Status = GetUniqueMemoryDomainInfo (
             CfgMgrProtocol,
             MemDomainInfo,
             MemDomainInfoCount,
             &MemDomainIds,
             &MemDomainIdsCount
             );
  if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) {
    goto error_handler;
  }

  Status = GetProcessorDomainInfo (
             CfgMgrProtocol,
             &ProcDomainInfo,
             &ProcDomainInfoCount
             );
  if (EFI_ERROR (Status) && (Status != EFI_NOT_FOUND)) {
    goto error_handler;
  }

  // Find the system's total unique proximity domain ids (memory + processor).
  // Clock domains are a separate namespace and are counted independently.
  Status = GetUniqueTotalDomainCount (
             MemDomainIds,
             MemDomainIdsCount,
             ProcDomainInfo,
             ProcDomainInfoCount,
             &MaxProximityDomain
             );
  if (EFI_ERROR (Status)) {
    goto error_handler;
  }

  DomainInfoCount = MemDomainInfoCount + ProcDomainInfoCount;
  if (DomainInfoCount == 0) {
    DEBUG ((
      DEBUG_INFO,
      "MSCT: No Memory/Processor Domain information found.\n"
      ));
    goto generate_msct_table;
  }

  DomainInfo = AllocateZeroPool (
                 sizeof (EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE) *
                 DomainInfoCount
                 );
  if (DomainInfo == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    goto error_handler;
  }

  // Accumulate the maximum memory capacity per proximity domain. The array is
  // sorted by ProximityDomainRangeLow, so entries sharing a domain are
  // contiguous. Fold every entry of a run into the run's first entry and
  // invalidate the rest.
  for (MemIndex = 0; MemIndex < MemDomainInfoCount; MemIndex++) {
    if (MemDomainInfo[MemIndex].ProximityDomainRangeLow == MAX_UINT32) {
      continue;
    }

    for (MemNextIndex = MemIndex + 1; MemNextIndex < MemDomainInfoCount; MemNextIndex++) {
      if (MemDomainInfo[MemNextIndex].ProximityDomainRangeLow !=
          MemDomainInfo[MemIndex].ProximityDomainRangeLow)
      {
        break;
      }

      // Maximum memory capacity per domain.
      MemDomainInfo[MemIndex].MaximumMemoryCapacity +=
        MemDomainInfo[MemNextIndex].MaximumMemoryCapacity;
      // Invalidate the folded record.
      MemDomainInfo[MemNextIndex].ProximityDomainRangeLow  = MAX_UINT32;
      MemDomainInfo[MemNextIndex].ProximityDomainRangeHigh = MAX_UINT32;
    }
  }

  ///
  /// Perform union of MemDomainInfo and ProcDomainInfo for same proximity domain, letting:
  /// - ProcDomainInfo[] with Proximity domains describing Processors with no memory associated
  /// - MemDomainInfo[] with Proximity domains describing memory with no processor associated
  ///
  DomainInfoIndex = 0;
  for (ProcIndex = 0; ProcIndex < ProcDomainInfoCount; ProcIndex++) {
    if (ProcDomainInfo[ProcIndex].ProximityDomainRangeLow == MAX_UINT32) {
      continue;
    }

    for (MemIndex = 0; MemIndex < MemDomainInfoCount; MemIndex++) {
      if (MemDomainInfo[MemIndex].ProximityDomainRangeLow == MAX_UINT32) {
        continue;
      }

      if (ProcDomainInfo[ProcIndex].ProximityDomainRangeLow ==
          MemDomainInfo[MemIndex].ProximityDomainRangeLow)
      {
        CopyMem (
          &DomainInfo[DomainInfoIndex],
          &ProcDomainInfo[ProcIndex],
          sizeof (EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE)
          );
        DomainInfo[DomainInfoIndex].MaximumMemoryCapacity = MemDomainInfo[MemIndex].MaximumMemoryCapacity;
        DomainInfoIndex++;

        /// Invalidate the records
        ProcDomainInfo[ProcIndex].ProximityDomainRangeLow  = MAX_UINT32;
        ProcDomainInfo[ProcIndex].ProximityDomainRangeHigh = MAX_UINT32;
        MemDomainInfo[MemIndex].ProximityDomainRangeLow    = MAX_UINT32;
        MemDomainInfo[MemIndex].ProximityDomainRangeHigh   = MAX_UINT32;
        break;
      }
    }
  }

  /// Club the symmetric continuous proximity domains.
  if (ProcDomainInfoCount > 1) {
    PerformQuickSort (
      ProcDomainInfo,
      ProcDomainInfoCount,
      sizeof (EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE),
      SortByProximityDomainRange
      );
    for (ProcIndex = ProcDomainInfoCount - 1; ProcIndex > 0; ProcIndex--) {
      if (ProcDomainInfo[ProcIndex].ProximityDomainRangeLow == MAX_UINT32) {
        continue;
      }

      if ((ProcDomainInfo[ProcIndex].ProximityDomainRangeLow ==
           (ProcDomainInfo[ProcIndex - 1].ProximityDomainRangeLow + 1)) &&
          (ProcDomainInfo[ProcIndex].MaximumProcessorCapacity ==
           (ProcDomainInfo[ProcIndex - 1].MaximumProcessorCapacity)))
      {
        ProcDomainInfo[ProcIndex - 1].ProximityDomainRangeHigh =
          ProcDomainInfo[ProcIndex].ProximityDomainRangeHigh;

        ProcDomainInfo[ProcIndex].ProximityDomainRangeLow  = MAX_UINT32;
        ProcDomainInfo[ProcIndex].ProximityDomainRangeHigh = MAX_UINT32;
      }
    }
  }

  /// Copy the processor proximity record (i.e. the processors with no memory associated)
  for (ProcIndex = 0; ProcIndex < ProcDomainInfoCount; ProcIndex++) {
    if (ProcDomainInfo[ProcIndex].ProximityDomainRangeLow == MAX_UINT32) {
      continue;
    }

    CopyMem (
      &DomainInfo[DomainInfoIndex],
      &ProcDomainInfo[ProcIndex],
      sizeof (EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE)
      );
    DomainInfoIndex++;
  }

  /// Copy remaining memory record (i.e. the memory with no processor associated)
  for (MemIndex = 0; MemIndex < MemDomainInfoCount; MemIndex++) {
    if (MemDomainInfo[MemIndex].ProximityDomainRangeLow == MAX_UINT32) {
      continue;
    }

    CopyMem (
      &DomainInfo[DomainInfoIndex],
      &MemDomainInfo[MemIndex],
      sizeof (EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE)
      );
    DomainInfoIndex++;
  }

  /// Sort the final list
  PerformQuickSort (
    DomainInfo,
    DomainInfoIndex,
    sizeof (EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE),
    SortByProximityDomainRange
    );

generate_msct_table:
  /// Calculate the size needed for the MSCT table
  TableSize = sizeof (EFI_ACPI_6_5_MAXIMUM_SYSTEM_CHARACTERISTICS_TABLE_HEADER) +
              (sizeof (EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE) * DomainInfoIndex);

  /// Allocate the Buffer for MSCT table
  MsctTable = (EFI_ACPI_6_5_MAXIMUM_SYSTEM_CHARACTERISTICS_TABLE_HEADER *)AllocateZeroPool (TableSize);
  if (MsctTable == NULL) {
    Status = EFI_OUT_OF_RESOURCES;
    DEBUG ((
      DEBUG_ERROR,
      "MSCT: Failed to allocate memory for MSCT Table, Size = %d, Status = %r\n",
      TableSize,
      Status
      ));
    ASSERT_EFI_ERROR (Status);
    goto error_handler;
  }

  if (DomainInfoIndex > 0) {
    DomainInfoPtr = (UINT8 *)MsctTable + sizeof (EFI_ACPI_6_5_MAXIMUM_SYSTEM_CHARACTERISTICS_TABLE_HEADER);
    CopyMem (
      DomainInfoPtr,
      (UINT8 *)DomainInfo,
      sizeof (EFI_ACPI_6_5_MAXIMUM_PROXIMITY_DOMAIN_INFORMATION_STRUCTURE) * DomainInfoIndex
      );
    MsctTable->OffsetProxDomInfo = sizeof (EFI_ACPI_6_5_MAXIMUM_SYSTEM_CHARACTERISTICS_TABLE_HEADER);
  }

  MsctTable->Header.Length = TableSize;

  ///
  /// As per the specification maximum number of proximity domain
  /// should be -1 of actual domain value.
  /// If there is no domain information then it will be concidered as single domain.
  ///
  if (MaxProximityDomain > 0) {
    MsctTable->MaximumNumberOfProximityDomains = MaxProximityDomain - 1;
  } else {
    MsctTable->MaximumNumberOfProximityDomains = 0;
  }

  if (ClockDomainIdsCount > 0) {
    MsctTable->MaximumNumberOfClockDomains = ClockDomainIdsCount - 1;
  } else {
    MsctTable->MaximumNumberOfClockDomains = 0;
  }

  MsctTable->MaximumPhysicalAddress = MaxPhysAddrInfo->MaxPhysicalAddress;
  *AcpiMsctTable                    = MsctTable;

  if (MemDomainIds != NULL) {
    FreePool (MemDomainIds);
  }

  if (MemDomainInfo != NULL) {
    FreePool (MemDomainInfo);
  }

  if (ProcDomainInfo != NULL) {
    FreePool (ProcDomainInfo);
  }

  if (DomainInfo != NULL) {
    FreePool (DomainInfo);
  }

  return EFI_SUCCESS;

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

  if (MemDomainInfo != NULL) {
    FreePool (MemDomainInfo);
  }

  if (ProcDomainInfo != NULL) {
    FreePool (ProcDomainInfo);
  }

  if (DomainInfo != NULL) {
    FreePool (DomainInfo);
  }

  if (MsctTable != NULL) {
    FreePool (MsctTable);
  }

  return Status;
}

/** Build the ACPI MSCT 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 [out]     Table          Pointer to the constructed ACPI Table.

@retval EFI_SUCCESS             Table generated successfully.
@retval EFI_INVALID_PARAMETER   A parameter is invalid.
@retval EFI_OUT_OF_RESOURCES    Memory allocation failed.
@retval Other                   Other EFI_STATUS error from called functions.

**/
STATIC
EFI_STATUS
EFIAPI
BuildMsctTable (
  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_STATUS  Status;

  EFI_ACPI_6_5_MAXIMUM_SYSTEM_CHARACTERISTICS_TABLE_HEADER  *MsctTable;

  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: MSCT: 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;
  MsctTable = NULL;

  Status = UpdateMsctTable (
             CfgMgrProtocol,
             &MsctTable
             );
  if (EFI_ERROR (Status)) {
    DEBUG ((
      DEBUG_ERROR,
      "ERROR: MSCT: Failed to update MSCT table. Status = %r\n",
      Status
      ));
    ASSERT_EFI_ERROR (Status);
    goto error_handler;
  }

  // Add ACPI header
  Status = AddAcpiHeader (
             CfgMgrProtocol,
             This,
             &MsctTable->Header,
             AcpiTableInfo,
             MsctTable->Header.Length
             );
  if (EFI_ERROR (Status)) {
    DEBUG ((
      DEBUG_ERROR,
      "MSCT: Failed to add ACPI header. Status = %r\n",
      Status
      ));
    ASSERT_EFI_ERROR (Status);
    goto error_handler;
  }

  *Table = (EFI_ACPI_DESCRIPTION_HEADER *)MsctTable;
  return EFI_SUCCESS;

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

  return Status;
}

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

/** The interface for the MSCT Table Generator.
*/
STATIC
CONST
ACPI_TABLE_GENERATOR  MsctGenerator = {
  // Generator ID
  CREATE_STD_ACPI_TABLE_GEN_ID (EStdAcpiTableIdMsct),
  // Generator Description
  L"ACPI.STD.MSCT.GENERATOR",
  // ACPI Table Signature
  EFI_ACPI_6_5_MAXIMUM_SYSTEM_CHARACTERISTICS_TABLE_SIGNATURE,
  // ACPI Table Revision supported by this Generator
  EFI_ACPI_6_5_MAXIMUM_SYSTEM_CHARACTERISTICS_TABLE_REVISION,
  // Minimum supported ACPI Table Revision
  EFI_ACPI_6_5_MAXIMUM_SYSTEM_CHARACTERISTICS_TABLE_REVISION,
  // Creator ID
  TABLE_GENERATOR_CREATOR_ID,
  // Creator Revision
  MSCT_GENERATOR_REVISION,
  // Build Table function
  BuildMsctTable,
  // Free Resource function
  NULL,
  // Extended build function not needed
  NULL,
  // Extended build function not implemented by the generator.
  // Hence extended free resource function is not required.
  NULL
};

/** AcpiMsctLib constructor

@param[in]  ImageHandle   The firmware allocated handle for the EFI image.
@param[in]  SystemTable   A pointer to the EFI System Table.

@retval EFI_SUCCESS       The constructor always returns EFI_SUCCESS.
@retval Other             Other EFI_STATUS error from called functions.

**/
EFI_STATUS
EFIAPI
AcpiMsctLibConstructor (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS  Status;

  Status = RegisterAcpiTableGenerator (&MsctGenerator);
  if (EFI_ERROR (Status)) {
    DEBUG ((
      DEBUG_ERROR,
      "MSCT: Register Generator Failed. Status = %r\n",
      Status
      ));
    ASSERT_EFI_ERROR (Status);
  } else {
    DEBUG ((
      DEBUG_INFO,
      "MSCT: Register Generator. Status = %r\n",
      Status
      ));
  }

  return Status;
}

/** AcpiMsctLib destructor

@param[in]  ImageHandle   The firmware allocated handle for the EFI image.
@param[in]  SystemTable   A pointer to the EFI System Table.

@retval EFI_SUCCESS       The destructor always returns EFI_SUCCESS.
@retval Other             Other EFI_STATUS error from called functions.

**/
EFI_STATUS
EFIAPI
AcpiMsctLibDestructor (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS  Status;

  Status = DeregisterAcpiTableGenerator (&MsctGenerator);
  if (EFI_ERROR (Status)) {
    DEBUG ((
      DEBUG_ERROR,
      "MSCT: Deregister Generator Failed. Status = %r\n",
      Status
      ));
    ASSERT_EFI_ERROR (Status);
  } else {
    DEBUG ((
      DEBUG_INFO,
      "MSCT: Deregister Generator. Status = %r\n",
      Status
      ));
  }

  return Status;
}