GtdtParser.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /** @file
  2. GTDT table parser
  3. Copyright (c) 2016 - 2021, ARM Limited. All rights reserved.
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. @par Reference(s):
  6. - ACPI 6.4 Specification - January 2021
  7. **/
  8. #include <IndustryStandard/Acpi.h>
  9. #include <Library/UefiLib.h>
  10. #include "AcpiParser.h"
  11. #include "AcpiTableParser.h"
  12. #include "AcpiViewConfig.h"
  13. // "The number of GT Block Timers must be less than or equal to 8"
  14. #define GT_BLOCK_TIMER_COUNT_MAX 8
  15. // Local variables
  16. STATIC CONST UINT32 *GtdtPlatformTimerCount;
  17. STATIC CONST UINT32 *GtdtPlatformTimerOffset;
  18. STATIC CONST UINT8 *PlatformTimerType;
  19. STATIC CONST UINT16 *PlatformTimerLength;
  20. STATIC CONST UINT32 *GtBlockTimerCount;
  21. STATIC CONST UINT32 *GtBlockTimerOffset;
  22. STATIC ACPI_DESCRIPTION_HEADER_INFO AcpiHdrInfo;
  23. /**
  24. This function validates the GT Block timer count.
  25. @param [in] Ptr Pointer to the start of the field data.
  26. @param [in] Context Pointer to context specific information e.g. this
  27. could be a pointer to the ACPI table header.
  28. **/
  29. STATIC
  30. VOID
  31. EFIAPI
  32. ValidateGtBlockTimerCount (
  33. IN UINT8 *Ptr,
  34. IN VOID *Context
  35. )
  36. {
  37. UINT32 BlockTimerCount;
  38. BlockTimerCount = *(UINT32 *)Ptr;
  39. if (BlockTimerCount > GT_BLOCK_TIMER_COUNT_MAX) {
  40. IncrementErrorCount ();
  41. Print (
  42. L"\nERROR: Timer Count = %d. Max Timer Count is %d.",
  43. BlockTimerCount,
  44. GT_BLOCK_TIMER_COUNT_MAX
  45. );
  46. }
  47. }
  48. /**
  49. This function validates the GT Frame Number.
  50. @param [in] Ptr Pointer to the start of the field data.
  51. @param [in] Context Pointer to context specific information e.g. this
  52. could be a pointer to the ACPI table header.
  53. **/
  54. STATIC
  55. VOID
  56. EFIAPI
  57. ValidateGtFrameNumber (
  58. IN UINT8 *Ptr,
  59. IN VOID *Context
  60. )
  61. {
  62. UINT8 FrameNumber;
  63. FrameNumber = *(UINT8 *)Ptr;
  64. if (FrameNumber >= GT_BLOCK_TIMER_COUNT_MAX) {
  65. IncrementErrorCount ();
  66. Print (
  67. L"\nERROR: GT Frame Number = %d. GT Frame Number must be in range 0-%d.",
  68. FrameNumber,
  69. GT_BLOCK_TIMER_COUNT_MAX - 1
  70. );
  71. }
  72. }
  73. /**
  74. An ACPI_PARSER array describing the ACPI GTDT Table.
  75. **/
  76. STATIC CONST ACPI_PARSER GtdtParser[] = {
  77. PARSE_ACPI_HEADER (&AcpiHdrInfo),
  78. { L"CntControlBase Physical Address",8, 36, L"0x%lx", NULL, NULL,
  79. NULL, NULL },
  80. { L"Reserved", 4, 44, L"0x%x", NULL, NULL,NULL, NULL },
  81. { L"Secure EL1 timer GSIV", 4, 48, L"0x%x", NULL, NULL,NULL, NULL },
  82. { L"Secure EL1 timer FLAGS", 4, 52, L"0x%x", NULL, NULL,NULL, NULL },
  83. { L"Non-Secure EL1 timer GSIV", 4, 56, L"0x%x", NULL, NULL,NULL, NULL },
  84. { L"Non-Secure EL1 timer FLAGS", 4, 60, L"0x%x", NULL, NULL,NULL, NULL },
  85. { L"Virtual timer GSIV", 4, 64, L"0x%x", NULL, NULL,NULL, NULL },
  86. { L"Virtual timer FLAGS", 4, 68, L"0x%x", NULL, NULL,NULL, NULL },
  87. { L"Non-Secure EL2 timer GSIV", 4, 72, L"0x%x", NULL, NULL,NULL, NULL },
  88. { L"Non-Secure EL2 timer FLAGS", 4, 76, L"0x%x", NULL, NULL,NULL, NULL },
  89. { L"CntReadBase Physical address", 8, 80, L"0x%lx", NULL, NULL,NULL, NULL },
  90. { L"Platform Timer Count", 4, 88, L"%d", NULL,
  91. (VOID **)&GtdtPlatformTimerCount, NULL, NULL },
  92. { L"Platform Timer Offset", 4, 92, L"0x%x", NULL,
  93. (VOID **)&GtdtPlatformTimerOffset,NULL, NULL },
  94. { L"Virtual EL2 Timer GSIV", 4, 96, L"0x%x", NULL, NULL,NULL, NULL },
  95. { L"Virtual EL2 Timer Flags", 4, 100, L"0x%x", NULL, NULL,NULL, NULL }
  96. };
  97. /**
  98. An ACPI_PARSER array describing the Platform timer header.
  99. **/
  100. STATIC CONST ACPI_PARSER GtPlatformTimerHeaderParser[] = {
  101. { L"Type", 1, 0, NULL, NULL, (VOID **)&PlatformTimerType, NULL, NULL },
  102. { L"Length", 2, 1, NULL, NULL, (VOID **)&PlatformTimerLength, NULL, NULL },
  103. { L"Reserved", 1, 3, NULL, NULL, NULL, NULL, NULL }
  104. };
  105. /**
  106. An ACPI_PARSER array describing the Platform GT Block.
  107. **/
  108. STATIC CONST ACPI_PARSER GtBlockParser[] = {
  109. { L"Type", 1, 0, L"%d", NULL, NULL, NULL, NULL },
  110. { L"Length", 2, 1, L"%d", NULL, NULL, NULL, NULL },
  111. { L"Reserved", 1, 3, L"%x", NULL, NULL, NULL, NULL },
  112. { L"Physical address (CntCtlBase)", 8, 4, L"0x%lx", NULL, NULL, NULL, NULL },
  113. { L"Timer Count", 4, 12, L"%d", NULL, (VOID **)&GtBlockTimerCount,
  114. ValidateGtBlockTimerCount, NULL },
  115. { L"Timer Offset", 4, 16, L"%d", NULL, (VOID **)&GtBlockTimerOffset, NULL,
  116. NULL }
  117. };
  118. /**
  119. An ACPI_PARSER array describing the GT Block timer.
  120. **/
  121. STATIC CONST ACPI_PARSER GtBlockTimerParser[] = {
  122. { L"Frame Number", 1, 0, L"%d", NULL, NULL, ValidateGtFrameNumber, NULL },
  123. { L"Reserved", 3, 1, L"%x %x %x", Dump3Chars, NULL, NULL, NULL },
  124. { L"Physical address (CntBaseX)", 8, 4, L"0x%lx", NULL, NULL, NULL, NULL },
  125. { L"Physical address (CntEL0BaseX)", 8, 12, L"0x%lx", NULL, NULL, NULL,
  126. NULL },
  127. { L"Physical Timer GSIV", 4, 20, L"0x%x", NULL, NULL, NULL, NULL },
  128. { L"Physical Timer Flags", 4, 24, L"0x%x", NULL, NULL, NULL, NULL },
  129. { L"Virtual Timer GSIV", 4, 28, L"0x%x", NULL, NULL, NULL, NULL },
  130. { L"Virtual Timer Flags", 4, 32, L"0x%x", NULL, NULL, NULL, NULL },
  131. { L"Common Flags", 4, 36, L"0x%x", NULL, NULL, NULL, NULL }
  132. };
  133. /**
  134. An ACPI_PARSER array describing the Platform Watchdog.
  135. **/
  136. STATIC CONST ACPI_PARSER ArmGenericWatchdogParser[] = {
  137. { L"Type", 1, 0, L"%d", NULL, NULL, NULL, NULL },
  138. { L"Length", 2, 1, L"%d", NULL, NULL, NULL, NULL },
  139. { L"Reserved", 1, 3, L"%x", NULL, NULL, NULL, NULL },
  140. { L"RefreshFrame Physical address", 8, 4, L"0x%lx", NULL, NULL, NULL, NULL },
  141. { L"ControlFrame Physical address", 8, 12, L"0x%lx", NULL, NULL, NULL, NULL },
  142. { L"Watchdog Timer GSIV", 4, 20, L"0x%x", NULL, NULL, NULL, NULL },
  143. { L"Watchdog Timer Flags", 4, 24, L"0x%x", NULL, NULL, NULL, NULL }
  144. };
  145. /**
  146. This function parses the Platform GT Block.
  147. @param [in] Ptr Pointer to the start of the GT Block data.
  148. @param [in] Length Length of the GT Block structure.
  149. **/
  150. STATIC
  151. VOID
  152. DumpGTBlock (
  153. IN UINT8 *Ptr,
  154. IN UINT16 Length
  155. )
  156. {
  157. UINT32 Index;
  158. UINT32 Offset;
  159. ParseAcpi (
  160. TRUE,
  161. 2,
  162. "GT Block",
  163. Ptr,
  164. Length,
  165. PARSER_PARAMS (GtBlockParser)
  166. );
  167. // Check if the values used to control the parsing logic have been
  168. // successfully read.
  169. if ((GtBlockTimerCount == NULL) ||
  170. (GtBlockTimerOffset == NULL))
  171. {
  172. IncrementErrorCount ();
  173. Print (
  174. L"ERROR: Insufficient GT Block Structure length. Length = %d.\n",
  175. Length
  176. );
  177. return;
  178. }
  179. Offset = *GtBlockTimerOffset;
  180. Index = 0;
  181. // Parse the specified number of GT Block Timer Structures or the GT Block
  182. // Structure buffer length. Whichever is minimum.
  183. while ((Index++ < *GtBlockTimerCount) &&
  184. (Offset < Length))
  185. {
  186. Offset += ParseAcpi (
  187. TRUE,
  188. 2,
  189. "GT Block Timer",
  190. Ptr + Offset,
  191. Length - Offset,
  192. PARSER_PARAMS (GtBlockTimerParser)
  193. );
  194. }
  195. }
  196. /**
  197. This function parses the Platform Watchdog timer.
  198. @param [in] Ptr Pointer to the start of the watchdog timer data.
  199. @param [in] Length Length of the watchdog timer structure.
  200. **/
  201. STATIC
  202. VOID
  203. DumpWatchdogTimer (
  204. IN UINT8 *Ptr,
  205. IN UINT16 Length
  206. )
  207. {
  208. ParseAcpi (
  209. TRUE,
  210. 2,
  211. "Arm Generic Watchdog",
  212. Ptr,
  213. Length,
  214. PARSER_PARAMS (ArmGenericWatchdogParser)
  215. );
  216. }
  217. /**
  218. This function parses the ACPI GTDT table.
  219. When trace is enabled this function parses the GTDT table and
  220. traces the ACPI table fields.
  221. This function also parses the following platform timer structures:
  222. - GT Block timer
  223. - Watchdog timer
  224. This function also performs validation of the ACPI table fields.
  225. @param [in] Trace If TRUE, trace the ACPI fields.
  226. @param [in] Ptr Pointer to the start of the buffer.
  227. @param [in] AcpiTableLength Length of the ACPI table.
  228. @param [in] AcpiTableRevision Revision of the ACPI table.
  229. **/
  230. VOID
  231. EFIAPI
  232. ParseAcpiGtdt (
  233. IN BOOLEAN Trace,
  234. IN UINT8 *Ptr,
  235. IN UINT32 AcpiTableLength,
  236. IN UINT8 AcpiTableRevision
  237. )
  238. {
  239. UINT32 Index;
  240. UINT32 Offset;
  241. UINT8 *TimerPtr;
  242. if (!Trace) {
  243. return;
  244. }
  245. ParseAcpi (
  246. TRUE,
  247. 0,
  248. "GTDT",
  249. Ptr,
  250. AcpiTableLength,
  251. PARSER_PARAMS (GtdtParser)
  252. );
  253. // Check if the values used to control the parsing logic have been
  254. // successfully read.
  255. if ((GtdtPlatformTimerCount == NULL) ||
  256. (GtdtPlatformTimerOffset == NULL))
  257. {
  258. IncrementErrorCount ();
  259. Print (
  260. L"ERROR: Insufficient table length. AcpiTableLength = %d.\n",
  261. AcpiTableLength
  262. );
  263. return;
  264. }
  265. TimerPtr = Ptr + *GtdtPlatformTimerOffset;
  266. Offset = *GtdtPlatformTimerOffset;
  267. Index = 0;
  268. // Parse the specified number of Platform Timer Structures or the GTDT
  269. // buffer length. Whichever is minimum.
  270. while ((Index++ < *GtdtPlatformTimerCount) &&
  271. (Offset < AcpiTableLength))
  272. {
  273. // Parse the Platform Timer Header to obtain Length and Type
  274. ParseAcpi (
  275. FALSE,
  276. 0,
  277. NULL,
  278. TimerPtr,
  279. AcpiTableLength - Offset,
  280. PARSER_PARAMS (GtPlatformTimerHeaderParser)
  281. );
  282. // Check if the values used to control the parsing logic have been
  283. // successfully read.
  284. if ((PlatformTimerType == NULL) ||
  285. (PlatformTimerLength == NULL))
  286. {
  287. IncrementErrorCount ();
  288. Print (
  289. L"ERROR: Insufficient remaining table buffer length to read the " \
  290. L"Platform Timer Structure header. Length = %d.\n",
  291. AcpiTableLength - Offset
  292. );
  293. return;
  294. }
  295. // Validate Platform Timer Structure length
  296. if ((*PlatformTimerLength == 0) ||
  297. ((Offset + (*PlatformTimerLength)) > AcpiTableLength))
  298. {
  299. IncrementErrorCount ();
  300. Print (
  301. L"ERROR: Invalid Platform Timer Structure length. " \
  302. L"Length = %d. Offset = %d. AcpiTableLength = %d.\n",
  303. *PlatformTimerLength,
  304. Offset,
  305. AcpiTableLength
  306. );
  307. return;
  308. }
  309. switch (*PlatformTimerType) {
  310. case EFI_ACPI_6_4_GTDT_GT_BLOCK:
  311. DumpGTBlock (TimerPtr, *PlatformTimerLength);
  312. break;
  313. case EFI_ACPI_6_4_GTDT_ARM_GENERIC_WATCHDOG:
  314. DumpWatchdogTimer (TimerPtr, *PlatformTimerLength);
  315. break;
  316. default:
  317. IncrementErrorCount ();
  318. Print (
  319. L"ERROR: Invalid Platform Timer Type = %d\n",
  320. *PlatformTimerType
  321. );
  322. break;
  323. } // switch
  324. TimerPtr += *PlatformTimerLength;
  325. Offset += *PlatformTimerLength;
  326. } // while
  327. }