SratParser.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /** @file
  2. SRAT table parser
  3. Copyright (c) 2016 - 2020, ARM Limited. All rights reserved.
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. @par Reference(s):
  6. - ACPI 6.3 Specification - January 2019
  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. #include "AcpiViewConfig.h"
  14. // Local Variables
  15. STATIC CONST UINT8 *SratRAType;
  16. STATIC CONST UINT8 *SratRALength;
  17. STATIC CONST UINT8 *SratDeviceHandleType;
  18. STATIC ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo;
  19. /**
  20. This function validates the Reserved field in the SRAT table header.
  21. @param [in] Ptr Pointer to the start of the field data.
  22. @param [in] Context Pointer to context specific information e.g. this
  23. could be a pointer to the ACPI table header.
  24. **/
  25. STATIC
  26. VOID
  27. EFIAPI
  28. ValidateSratReserved (
  29. IN UINT8 *Ptr,
  30. IN VOID *Context
  31. )
  32. {
  33. if (*(UINT32 *)Ptr != 1) {
  34. IncrementErrorCount ();
  35. Print (L"\nERROR: Reserved should be 1 for backward compatibility.\n");
  36. }
  37. }
  38. /**
  39. This function validates the Device Handle Type field in the Generic Initiator
  40. Affinity Structure.
  41. @param [in] Ptr Pointer to the start of the field data.
  42. @param [in] Context Pointer to context specific information e.g. this
  43. could be a pointer to the ACPI table header.
  44. **/
  45. STATIC
  46. VOID
  47. EFIAPI
  48. ValidateSratDeviceHandleType (
  49. IN UINT8 *Ptr,
  50. IN VOID *Context
  51. )
  52. {
  53. UINT8 DeviceHandleType;
  54. DeviceHandleType = *Ptr;
  55. if (DeviceHandleType > EFI_ACPI_6_3_PCI_DEVICE_HANDLE) {
  56. IncrementErrorCount ();
  57. Print (
  58. L"\nERROR: Invalid Device Handle Type: %d. Must be between 0 and %d.",
  59. DeviceHandleType,
  60. EFI_ACPI_6_3_PCI_DEVICE_HANDLE
  61. );
  62. }
  63. }
  64. /**
  65. This function traces the PCI BDF Number field inside Device Handle - PCI
  66. @param [in] Format Format string for tracing the data.
  67. @param [in] Ptr Pointer to the start of the buffer.
  68. **/
  69. STATIC
  70. VOID
  71. EFIAPI
  72. DumpSratPciBdfNumber (
  73. IN CONST CHAR16 *Format,
  74. IN UINT8 *Ptr
  75. )
  76. {
  77. CHAR16 Buffer[OUTPUT_FIELD_COLUMN_WIDTH];
  78. Print (L"\n");
  79. /*
  80. The PCI BDF Number subfields are printed in the order specified in the ACPI
  81. specification. The format of the 16-bit PCI BDF Number field is as follows:
  82. +-----+------+------+
  83. |DEV | FUNC | BUS |
  84. +-----+------+------+
  85. |15:11| 10:8 | 7:0 |
  86. +-----+------+------+
  87. */
  88. // Print PCI Bus Number (Bits 7:0 of Byte 2)
  89. UnicodeSPrint (
  90. Buffer,
  91. sizeof (Buffer),
  92. L"PCI Bus Number"
  93. );
  94. PrintFieldName (4, Buffer);
  95. Print (
  96. L"0x%x\n",
  97. *Ptr
  98. );
  99. Ptr++;
  100. // Print PCI Device Number (Bits 7:3 of Byte 3)
  101. UnicodeSPrint (
  102. Buffer,
  103. sizeof (Buffer),
  104. L"PCI Device Number"
  105. );
  106. PrintFieldName (4, Buffer);
  107. Print (
  108. L"0x%x\n",
  109. (*Ptr & (BIT7 | BIT6 | BIT5 | BIT4 | BIT3)) >> 3
  110. );
  111. // PCI Function Number (Bits 2:0 of Byte 3)
  112. UnicodeSPrint (
  113. Buffer,
  114. sizeof (Buffer),
  115. L"PCI Function Number"
  116. );
  117. PrintFieldName (4, Buffer);
  118. Print (
  119. L"0x%x\n",
  120. *Ptr & (BIT2 | BIT1 | BIT0)
  121. );
  122. }
  123. /**
  124. An ACPI_PARSER array describing the Device Handle - ACPI
  125. **/
  126. STATIC CONST ACPI_PARSER SratDeviceHandleAcpiParser[] = {
  127. { L"ACPI_HID", 8, 0, L"0x%lx", NULL, NULL, NULL, NULL },
  128. { L"ACPI_UID", 4, 8, L"0x%x", NULL, NULL, NULL, NULL },
  129. { L"Reserved", 4, 12, L"0x%x", NULL, NULL, NULL, NULL }
  130. };
  131. /**
  132. An ACPI_PARSER array describing the Device Handle - PCI
  133. **/
  134. STATIC CONST ACPI_PARSER SratDeviceHandlePciParser[] = {
  135. { L"PCI Segment", 2, 0, L"0x%x", NULL, NULL, NULL, NULL },
  136. { L"PCI BDF Number", 2, 2, NULL, DumpSratPciBdfNumber, NULL, NULL, NULL },
  137. { L"Reserved", 12, 4, L"%x %x %x %x - %x %x %x %x - %x %x %x %x", Dump12Chars,
  138. NULL, NULL, NULL }
  139. };
  140. /**
  141. This function traces the Device Handle field inside Generic Initiator
  142. Affinity Structure.
  143. @param [in] Format Format string for tracing the data.
  144. @param [in] Ptr Pointer to the start of the buffer.
  145. **/
  146. STATIC
  147. VOID
  148. EFIAPI
  149. DumpSratDeviceHandle (
  150. IN CONST CHAR16 *Format,
  151. IN UINT8 *Ptr
  152. )
  153. {
  154. if (SratDeviceHandleType == NULL) {
  155. IncrementErrorCount ();
  156. Print (L"\nERROR: Device Handle Type read incorrectly.\n");
  157. return;
  158. }
  159. Print (L"\n");
  160. if (*SratDeviceHandleType == EFI_ACPI_6_3_ACPI_DEVICE_HANDLE) {
  161. ParseAcpi (
  162. TRUE,
  163. 2,
  164. NULL,
  165. Ptr,
  166. sizeof (EFI_ACPI_6_3_DEVICE_HANDLE_ACPI),
  167. PARSER_PARAMS (SratDeviceHandleAcpiParser)
  168. );
  169. } else if (*SratDeviceHandleType == EFI_ACPI_6_3_PCI_DEVICE_HANDLE) {
  170. ParseAcpi (
  171. TRUE,
  172. 2,
  173. NULL,
  174. Ptr,
  175. sizeof (EFI_ACPI_6_3_DEVICE_HANDLE_PCI),
  176. PARSER_PARAMS (SratDeviceHandlePciParser)
  177. );
  178. }
  179. }
  180. /**
  181. This function traces the APIC Proximity Domain field.
  182. @param [in] Format Format string for tracing the data.
  183. @param [in] Ptr Pointer to the start of the buffer.
  184. **/
  185. STATIC
  186. VOID
  187. EFIAPI
  188. DumpSratApicProximity (
  189. IN CONST CHAR16 *Format,
  190. IN UINT8 *Ptr
  191. )
  192. {
  193. UINT32 ProximityDomain;
  194. ProximityDomain = Ptr[0] | (Ptr[1] << 8) | (Ptr[2] << 16);
  195. Print (Format, ProximityDomain);
  196. }
  197. /**
  198. An ACPI_PARSER array describing the SRAT Table.
  199. **/
  200. STATIC CONST ACPI_PARSER SratParser[] = {
  201. PARSE_ACPI_HEADER (&AcpiHdrInfo),
  202. { L"Reserved", 4, 36, L"0x%x", NULL, NULL, ValidateSratReserved, NULL },
  203. { L"Reserved", 8, 40, L"0x%lx", NULL, NULL, NULL, NULL }
  204. };
  205. /**
  206. An ACPI_PARSER array describing the Resource Allocation structure header.
  207. **/
  208. STATIC CONST ACPI_PARSER SratResourceAllocationParser[] = {
  209. { L"Type", 1, 0, NULL, NULL, (VOID **)&SratRAType, NULL, NULL },
  210. { L"Length", 1, 1, NULL, NULL, (VOID **)&SratRALength, NULL, NULL }
  211. };
  212. /**
  213. An ACPI_PARSER array describing the GICC Affinity structure.
  214. **/
  215. STATIC CONST ACPI_PARSER SratGicCAffinityParser[] = {
  216. { L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL },
  217. { L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL },
  218. { L"Proximity Domain", 4, 2, L"0x%x", NULL, NULL, NULL, NULL },
  219. { L"ACPI Processor UID", 4, 6, L"0x%x", NULL, NULL, NULL, NULL },
  220. { L"Flags", 4, 10, L"0x%x", NULL, NULL, NULL, NULL },
  221. { L"Clock Domain", 4, 14, L"0x%x", NULL, NULL, NULL, NULL }
  222. };
  223. /**
  224. An ACPI_PARSER array describing the GIC ITS Affinity structure.
  225. **/
  226. STATIC CONST ACPI_PARSER SratGicITSAffinityParser[] = {
  227. { L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL },
  228. { L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL },
  229. { L"Proximity Domain", 4, 2, L"0x%x", NULL, NULL, NULL, NULL },
  230. { L"Reserved", 2, 6, L"0x%x", NULL, NULL, NULL, NULL },
  231. { L"ITS Id", 4, 8, L"0x%x", NULL, NULL, NULL, NULL },
  232. };
  233. /**
  234. An ACPI_PARSER array describing the Generic Initiator Affinity Structure
  235. **/
  236. STATIC CONST ACPI_PARSER SratGenericInitiatorAffinityParser[] = {
  237. { L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL },
  238. { L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL },
  239. { L"Reserved", 1, 2, L"0x%x", NULL, NULL, NULL, NULL },
  240. { L"Device Handle Type", 1, 3, L"%d", NULL, (VOID **)&SratDeviceHandleType,
  241. ValidateSratDeviceHandleType, NULL },
  242. { L"Proximity Domain", 4, 4, L"0x%x", NULL, NULL, NULL, NULL },
  243. { L"Device Handle", 16, 8, L"%s", DumpSratDeviceHandle, NULL, NULL, NULL },
  244. { L"Flags", 4, 24, L"0x%x", NULL, NULL, NULL, NULL },
  245. { L"Reserved", 4, 28, L"0x%x", NULL, NULL, NULL, NULL }
  246. };
  247. /**
  248. An ACPI_PARSER array describing the Memory Affinity structure.
  249. **/
  250. STATIC CONST ACPI_PARSER SratMemAffinityParser[] = {
  251. { L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL },
  252. { L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL },
  253. { L"Proximity Domain", 4, 2, L"0x%x", NULL, NULL, NULL, NULL },
  254. { L"Reserved", 2, 6, L"0x%x", NULL, NULL, NULL, NULL },
  255. { L"Base Address Low", 4, 8, L"0x%x", NULL, NULL, NULL, NULL },
  256. { L"Base Address High", 4, 12, L"0x%x", NULL, NULL, NULL, NULL },
  257. { L"Length Low", 4, 16, L"0x%x", NULL, NULL, NULL, NULL },
  258. { L"Length High", 4, 20, L"0x%x", NULL, NULL, NULL, NULL },
  259. { L"Reserved", 4, 24, L"0x%x", NULL, NULL, NULL, NULL },
  260. { L"Flags", 4, 28, L"0x%x", NULL, NULL, NULL, NULL },
  261. { L"Reserved", 8, 32, L"0x%lx", NULL, NULL, NULL, NULL }
  262. };
  263. /**
  264. An ACPI_PARSER array describing the APIC/SAPIC Affinity structure.
  265. **/
  266. STATIC CONST ACPI_PARSER SratApciSapicAffinityParser[] = {
  267. { L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL },
  268. { L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL },
  269. { L"Proximity Domain [7:0]", 1, 2, L"0x%x", NULL, NULL, NULL, NULL },
  270. { L"APIC ID", 1, 3, L"0x%x", NULL, NULL, NULL, NULL },
  271. { L"Flags", 4, 4, L"0x%x", NULL, NULL, NULL, NULL },
  272. { L"Local SAPIC EID", 1, 8, L"0x%x", NULL, NULL, NULL, NULL },
  273. { L"Proximity Domain [31:8]", 3, 9, L"0x%x", DumpSratApicProximity,
  274. NULL, NULL, NULL },
  275. { L"Clock Domain", 4, 12, L"0x%x", NULL, NULL, NULL, NULL }
  276. };
  277. /**
  278. An ACPI_PARSER array describing the Processor Local x2APIC Affinity structure.
  279. **/
  280. STATIC CONST ACPI_PARSER SratX2ApciAffinityParser[] = {
  281. { L"Type", 1, 0, L"0x%x", NULL, NULL, NULL, NULL },
  282. { L"Length", 1, 1, L"0x%x", NULL, NULL, NULL, NULL },
  283. { L"Reserved", 2, 2, L"0x%x", NULL, NULL, NULL, NULL },
  284. { L"Proximity Domain", 4, 4, L"0x%x", NULL, NULL, NULL, NULL },
  285. { L"X2APIC ID", 4, 8, L"0x%x", NULL, NULL, NULL, NULL },
  286. { L"Flags", 4, 12, L"0x%x", NULL, NULL, NULL, NULL },
  287. { L"Clock Domain", 4, 16, L"0x%x", NULL, NULL, NULL, NULL },
  288. { L"Reserved", 4, 20, L"0x%x", NULL, NULL, NULL, NULL }
  289. };
  290. /**
  291. This function parses the ACPI SRAT table.
  292. When trace is enabled this function parses the SRAT table and
  293. traces the ACPI table fields.
  294. This function parses the following Resource Allocation Structures:
  295. - Processor Local APIC/SAPIC Affinity Structure
  296. - Memory Affinity Structure
  297. - Processor Local x2APIC Affinity Structure
  298. - GICC Affinity Structure
  299. This function also performs validation of the ACPI table fields.
  300. @param [in] Trace If TRUE, trace the ACPI fields.
  301. @param [in] Ptr Pointer to the start of the buffer.
  302. @param [in] AcpiTableLength Length of the ACPI table.
  303. @param [in] AcpiTableRevision Revision of the ACPI table.
  304. **/
  305. VOID
  306. EFIAPI
  307. ParseAcpiSrat (
  308. IN BOOLEAN Trace,
  309. IN UINT8 *Ptr,
  310. IN UINT32 AcpiTableLength,
  311. IN UINT8 AcpiTableRevision
  312. )
  313. {
  314. UINT32 Offset;
  315. UINT8 *ResourcePtr;
  316. UINT32 GicCAffinityIndex;
  317. UINT32 GicITSAffinityIndex;
  318. UINT32 GenericInitiatorAffinityIndex;
  319. UINT32 MemoryAffinityIndex;
  320. UINT32 ApicSapicAffinityIndex;
  321. UINT32 X2ApicAffinityIndex;
  322. CHAR8 Buffer[80]; // Used for AsciiName param of ParseAcpi
  323. GicCAffinityIndex = 0;
  324. GicITSAffinityIndex = 0;
  325. GenericInitiatorAffinityIndex = 0;
  326. MemoryAffinityIndex = 0;
  327. ApicSapicAffinityIndex = 0;
  328. X2ApicAffinityIndex = 0;
  329. if (!Trace) {
  330. return;
  331. }
  332. Offset = ParseAcpi (
  333. TRUE,
  334. 0,
  335. "SRAT",
  336. Ptr,
  337. AcpiTableLength,
  338. PARSER_PARAMS (SratParser)
  339. );
  340. ResourcePtr = Ptr + Offset;
  341. while (Offset < AcpiTableLength) {
  342. ParseAcpi (
  343. FALSE,
  344. 0,
  345. NULL,
  346. ResourcePtr,
  347. AcpiTableLength - Offset,
  348. PARSER_PARAMS (SratResourceAllocationParser)
  349. );
  350. // Check if the values used to control the parsing logic have been
  351. // successfully read.
  352. if ((SratRAType == NULL) ||
  353. (SratRALength == NULL))
  354. {
  355. IncrementErrorCount ();
  356. Print (
  357. L"ERROR: Insufficient remaining table buffer length to read the " \
  358. L"Static Resource Allocation structure header. Length = %d.\n",
  359. AcpiTableLength - Offset
  360. );
  361. return;
  362. }
  363. // Validate Static Resource Allocation Structure length
  364. if ((*SratRALength == 0) ||
  365. ((Offset + (*SratRALength)) > AcpiTableLength))
  366. {
  367. IncrementErrorCount ();
  368. Print (
  369. L"ERROR: Invalid Static Resource Allocation Structure length. " \
  370. L"Length = %d. Offset = %d. AcpiTableLength = %d.\n",
  371. *SratRALength,
  372. Offset,
  373. AcpiTableLength
  374. );
  375. return;
  376. }
  377. switch (*SratRAType) {
  378. case EFI_ACPI_6_3_GICC_AFFINITY:
  379. AsciiSPrint (
  380. Buffer,
  381. sizeof (Buffer),
  382. "GICC Affinity Structure [%d]",
  383. GicCAffinityIndex++
  384. );
  385. ParseAcpi (
  386. TRUE,
  387. 2,
  388. Buffer,
  389. ResourcePtr,
  390. *SratRALength,
  391. PARSER_PARAMS (SratGicCAffinityParser)
  392. );
  393. break;
  394. case EFI_ACPI_6_3_GIC_ITS_AFFINITY:
  395. AsciiSPrint (
  396. Buffer,
  397. sizeof (Buffer),
  398. "GIC ITS Affinity Structure [%d]",
  399. GicITSAffinityIndex++
  400. );
  401. ParseAcpi (
  402. TRUE,
  403. 2,
  404. Buffer,
  405. ResourcePtr,
  406. *SratRALength,
  407. PARSER_PARAMS (SratGicITSAffinityParser)
  408. );
  409. break;
  410. case EFI_ACPI_6_3_GENERIC_INITIATOR_AFFINITY:
  411. AsciiSPrint (
  412. Buffer,
  413. sizeof (Buffer),
  414. "Generic Initiator Affinity Structure [%d]",
  415. GenericInitiatorAffinityIndex++
  416. );
  417. ParseAcpi (
  418. TRUE,
  419. 2,
  420. Buffer,
  421. ResourcePtr,
  422. *SratRALength,
  423. PARSER_PARAMS (SratGenericInitiatorAffinityParser)
  424. );
  425. break;
  426. case EFI_ACPI_6_3_MEMORY_AFFINITY:
  427. AsciiSPrint (
  428. Buffer,
  429. sizeof (Buffer),
  430. "Memory Affinity Structure [%d]",
  431. MemoryAffinityIndex++
  432. );
  433. ParseAcpi (
  434. TRUE,
  435. 2,
  436. Buffer,
  437. ResourcePtr,
  438. *SratRALength,
  439. PARSER_PARAMS (SratMemAffinityParser)
  440. );
  441. break;
  442. case EFI_ACPI_6_3_PROCESSOR_LOCAL_APIC_SAPIC_AFFINITY:
  443. AsciiSPrint (
  444. Buffer,
  445. sizeof (Buffer),
  446. "APIC/SAPIC Affinity Structure [%d]",
  447. ApicSapicAffinityIndex++
  448. );
  449. ParseAcpi (
  450. TRUE,
  451. 2,
  452. Buffer,
  453. ResourcePtr,
  454. *SratRALength,
  455. PARSER_PARAMS (SratApciSapicAffinityParser)
  456. );
  457. break;
  458. case EFI_ACPI_6_3_PROCESSOR_LOCAL_X2APIC_AFFINITY:
  459. AsciiSPrint (
  460. Buffer,
  461. sizeof (Buffer),
  462. "X2APIC Affinity Structure [%d]",
  463. X2ApicAffinityIndex++
  464. );
  465. ParseAcpi (
  466. TRUE,
  467. 2,
  468. Buffer,
  469. ResourcePtr,
  470. *SratRALength,
  471. PARSER_PARAMS (SratX2ApciAffinityParser)
  472. );
  473. break;
  474. default:
  475. IncrementErrorCount ();
  476. Print (L"ERROR: Unknown SRAT Affinity type = 0x%x\n", *SratRAType);
  477. break;
  478. }
  479. ResourcePtr += (*SratRALength);
  480. Offset += (*SratRALength);
  481. }
  482. }