SlitParser.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /** @file
  2. SLIT table parser
  3. Copyright (c) 2016 - 2019, ARM Limited. All rights reserved.
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. @par Reference(s):
  6. - ACPI 6.2 Specification - Errata A, September 2017
  7. **/
  8. #include <IndustryStandard/Acpi.h>
  9. #include <Library/PrintLib.h>
  10. #include <Library/UefiLib.h>
  11. #include "AcpiParser.h"
  12. #include "AcpiTableParser.h"
  13. // Local Variables
  14. STATIC CONST UINT64 *SlitSystemLocalityCount;
  15. STATIC ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo;
  16. /**
  17. An ACPI_PARSER array describing the ACPI SLIT table.
  18. **/
  19. STATIC CONST ACPI_PARSER SlitParser[] = {
  20. PARSE_ACPI_HEADER (&AcpiHdrInfo),
  21. { L"Number of System Localities", 8, 36, L"0x%lx", NULL,
  22. (VOID **)&SlitSystemLocalityCount,NULL, NULL }
  23. };
  24. /**
  25. Macro to get the value of a System Locality
  26. **/
  27. #define SLIT_ELEMENT(Ptr, i, j) *(Ptr + (i * LocalityCount) + j)
  28. /**
  29. This function parses the ACPI SLIT table.
  30. When trace is enabled this function parses the SLIT table and
  31. traces the ACPI table fields.
  32. This function also validates System Localities for the following:
  33. - Diagonal elements have a normalized value of 10
  34. - Relative distance from System Locality at i*N+j is same as
  35. j*N+i
  36. @param [in] Trace If TRUE, trace the ACPI fields.
  37. @param [in] Ptr Pointer to the start of the buffer.
  38. @param [in] AcpiTableLength Length of the ACPI table.
  39. @param [in] AcpiTableRevision Revision of the ACPI table.
  40. **/
  41. VOID
  42. EFIAPI
  43. ParseAcpiSlit (
  44. IN BOOLEAN Trace,
  45. IN UINT8 *Ptr,
  46. IN UINT32 AcpiTableLength,
  47. IN UINT8 AcpiTableRevision
  48. )
  49. {
  50. UINT32 Offset;
  51. UINT32 Count;
  52. UINT32 Index;
  53. UINT32 LocalityCount;
  54. UINT8 *LocalityPtr;
  55. CHAR16 Buffer[80]; // Used for AsciiName param of ParseAcpi
  56. if (!Trace) {
  57. return;
  58. }
  59. Offset = ParseAcpi (
  60. TRUE,
  61. 0,
  62. "SLIT",
  63. Ptr,
  64. AcpiTableLength,
  65. PARSER_PARAMS (SlitParser)
  66. );
  67. // Check if the values used to control the parsing logic have been
  68. // successfully read.
  69. if (SlitSystemLocalityCount == NULL) {
  70. IncrementErrorCount ();
  71. Print (
  72. L"ERROR: Insufficient table length. AcpiTableLength = %d.\n",
  73. AcpiTableLength
  74. );
  75. return;
  76. }
  77. /*
  78. Despite the 'Number of System Localities' being a 64-bit field in SLIT,
  79. the maximum number of localities that can be represented in SLIT is limited
  80. by the 'Length' field of the ACPI table.
  81. Since the ACPI table length field is 32-bit wide. The maximum number of
  82. localities that can be represented in SLIT can be calculated as:
  83. MaxLocality = sqrt (MAX_UINT32 - sizeof (EFI_ACPI_6_3_SYSTEM_LOCALITY_DISTANCE_INFORMATION_TABLE_HEADER))
  84. = 65535
  85. = MAX_UINT16
  86. */
  87. if (*SlitSystemLocalityCount > MAX_UINT16) {
  88. IncrementErrorCount ();
  89. Print (
  90. L"ERROR: The Number of System Localities provided can't be represented " \
  91. L"in the SLIT table. SlitSystemLocalityCount = %ld. " \
  92. L"MaxLocalityCountAllowed = %d.\n",
  93. *SlitSystemLocalityCount,
  94. MAX_UINT16
  95. );
  96. return;
  97. }
  98. LocalityCount = (UINT32)*SlitSystemLocalityCount;
  99. // Make sure system localities fit in the table buffer provided
  100. if (Offset + (LocalityCount * LocalityCount) > AcpiTableLength) {
  101. IncrementErrorCount ();
  102. Print (
  103. L"ERROR: Invalid Number of System Localities. " \
  104. L"SlitSystemLocalityCount = %ld. AcpiTableLength = %d.\n",
  105. *SlitSystemLocalityCount,
  106. AcpiTableLength
  107. );
  108. return;
  109. }
  110. LocalityPtr = Ptr + Offset;
  111. // We only print the Localities if the count is less than 16
  112. // If the locality count is more than 16 then refer to the
  113. // raw data dump.
  114. if (LocalityCount < 16) {
  115. UnicodeSPrint (
  116. Buffer,
  117. sizeof (Buffer),
  118. L"Entry[0x%lx][0x%lx]",
  119. LocalityCount,
  120. LocalityCount
  121. );
  122. PrintFieldName (0, Buffer);
  123. Print (L"\n");
  124. Print (L" ");
  125. for (Index = 0; Index < LocalityCount; Index++) {
  126. Print (L" (%3d) ", Index);
  127. }
  128. Print (L"\n");
  129. for (Count = 0; Count < LocalityCount; Count++) {
  130. Print (L" (%3d) ", Count);
  131. for (Index = 0; Index < LocalityCount; Index++) {
  132. Print (L" %3d ", SLIT_ELEMENT (LocalityPtr, Count, Index));
  133. }
  134. Print (L"\n");
  135. }
  136. }
  137. // Validate
  138. for (Count = 0; Count < LocalityCount; Count++) {
  139. for (Index = 0; Index < LocalityCount; Index++) {
  140. // Element[x][x] must be equal to 10
  141. if ((Count == Index) && (SLIT_ELEMENT (LocalityPtr, Count, Index) != 10)) {
  142. IncrementErrorCount ();
  143. Print (
  144. L"ERROR: Diagonal Element[0x%lx][0x%lx] (%3d)."
  145. L" Normalized Value is not 10\n",
  146. Count,
  147. Index,
  148. SLIT_ELEMENT (LocalityPtr, Count, Index)
  149. );
  150. }
  151. // Element[i][j] must be equal to Element[j][i]
  152. if (SLIT_ELEMENT (LocalityPtr, Count, Index) !=
  153. SLIT_ELEMENT (LocalityPtr, Index, Count))
  154. {
  155. IncrementErrorCount ();
  156. Print (
  157. L"ERROR: Relative distances for Element[0x%lx][0x%lx] (%3d) and \n"
  158. L"Element[0x%lx][0x%lx] (%3d) do not match.\n",
  159. Count,
  160. Index,
  161. SLIT_ELEMENT (LocalityPtr, Count, Index),
  162. Index,
  163. Count,
  164. SLIT_ELEMENT (LocalityPtr, Index, Count)
  165. );
  166. }
  167. }
  168. }
  169. }