4 Copyright (c) 2017 - 2019, ARM Limited. All rights reserved.
5 SPDX-License-Identifier: BSD-2-Clause-Patent
8 - ACPI 6.2 Specification - Errata A, September 2017
12 #include <Library/AcpiLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/MemoryAllocationLib.h>
15 #include <Protocol/AcpiTable.h>
17 // Module specific include files.
18 #include <AcpiTableGenerator.h>
19 #include <ConfigurationManagerObject.h>
20 #include <ConfigurationManagerHelper.h>
21 #include <Library/TableHelperLib.h>
22 #include <Protocol/ConfigurationManagerProtocol.h>
24 /** ARM standard GTDT Generator
27 The following Configuration Manager Object(s) are required by
29 - EArmObjGenericTimerInfo
30 - EArmObjPlatformGenericWatchdogInfo (OPTIONAL)
31 - EArmObjPlatformGTBlockInfo (OPTIONAL)
32 - EArmObjGTBlockTimerFrameInfo (OPTIONAL)
35 /** This macro expands to a function that retrieves the Generic
36 Timer Information from the Configuration Manager.
40 EArmObjGenericTimerInfo
,
41 CM_ARM_GENERIC_TIMER_INFO
44 /** This macro expands to a function that retrieves the SBSA Generic
45 Watchdog Timer Information from the Configuration Manager.
49 EArmObjPlatformGenericWatchdogInfo
,
50 CM_ARM_GENERIC_WATCHDOG_INFO
53 /** This macro expands to a function that retrieves the Platform Generic
54 Timer Block Information from the Configuration Manager.
58 EArmObjPlatformGTBlockInfo
,
62 /** This macro expands to a function that retrieves the Generic
63 Timer Block Timer Frame Information from the Configuration Manager.
67 EArmObjGTBlockTimerFrameInfo
,
68 CM_ARM_GTBLOCK_TIMER_FRAME_INFO
71 /** Add the Generic Timer Information to the GTDT table.
73 Also update the Platform Timer offset information if the platform
74 implements platform timers.
76 @param [in] CfgMgrProtocol Pointer to the Configuration Manager
78 @param [in] Gtdt Pointer to the GTDT Table.
79 @param [in] PlatformTimerCount Platform timer count.
81 @retval EFI_SUCCESS Success.
82 @retval EFI_INVALID_PARAMETER A parameter is invalid.
83 @retval EFI_NOT_FOUND The required object was not found.
84 @retval EFI_BAD_BUFFER_SIZE The size returned by the Configuration
85 Manager is less than the Object size for the
92 IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL
* CONST CfgMgrProtocol
,
93 IN EFI_ACPI_6_2_GENERIC_TIMER_DESCRIPTION_TABLE
* CONST Gtdt
,
94 IN CONST UINT32 PlatformTimerCount
98 CM_ARM_GENERIC_TIMER_INFO
* GenericTimerInfo
;
100 ASSERT (CfgMgrProtocol
!= NULL
);
101 ASSERT (Gtdt
!= NULL
);
103 Status
= GetEArmObjGenericTimerInfo (
110 if (EFI_ERROR (Status
)) {
113 "ERROR: GTDT: Failed to get GenericTimerInfo. Status = %r\n",
119 Gtdt
->CntControlBasePhysicalAddress
=
120 GenericTimerInfo
->CounterControlBaseAddress
;
121 Gtdt
->Reserved
= EFI_ACPI_RESERVED_DWORD
;
122 Gtdt
->SecurePL1TimerGSIV
= GenericTimerInfo
->SecurePL1TimerGSIV
;
123 Gtdt
->SecurePL1TimerFlags
= GenericTimerInfo
->SecurePL1TimerFlags
;
124 Gtdt
->NonSecurePL1TimerGSIV
= GenericTimerInfo
->NonSecurePL1TimerGSIV
;
125 Gtdt
->NonSecurePL1TimerFlags
= GenericTimerInfo
->NonSecurePL1TimerFlags
;
126 Gtdt
->VirtualTimerGSIV
= GenericTimerInfo
->VirtualTimerGSIV
;
127 Gtdt
->VirtualTimerFlags
= GenericTimerInfo
->VirtualTimerFlags
;
128 Gtdt
->NonSecurePL2TimerGSIV
= GenericTimerInfo
->NonSecurePL2TimerGSIV
;
129 Gtdt
->NonSecurePL2TimerFlags
= GenericTimerInfo
->NonSecurePL2TimerFlags
;
130 Gtdt
->CntReadBasePhysicalAddress
=
131 GenericTimerInfo
->CounterReadBaseAddress
;
132 Gtdt
->PlatformTimerCount
= PlatformTimerCount
;
133 Gtdt
->PlatformTimerOffset
= (PlatformTimerCount
== 0) ? 0 :
134 sizeof (EFI_ACPI_6_2_GENERIC_TIMER_DESCRIPTION_TABLE
);
139 /** Add the SBSA Generic Watchdog Timers to the GTDT table.
141 @param [in] Gtdt Pointer to the GTDT Table.
142 @param [in] WatchdogOffset Offset to the watchdog information in the
144 @param [in] WatchdogInfoList Pointer to the watchdog information list.
145 @param [in] WatchdogCount Platform timer count.
149 AddGenericWatchdogList (
150 IN EFI_ACPI_6_2_GENERIC_TIMER_DESCRIPTION_TABLE
* CONST Gtdt
,
151 IN CONST UINT32 WatchdogOffset
,
152 IN CONST CM_ARM_GENERIC_WATCHDOG_INFO
* WatchdogInfoList
,
153 IN UINT32 WatchdogCount
156 EFI_ACPI_6_2_GTDT_SBSA_GENERIC_WATCHDOG_STRUCTURE
* Watchdog
;
158 ASSERT (Gtdt
!= NULL
);
159 ASSERT (WatchdogInfoList
!= NULL
);
161 Watchdog
= (EFI_ACPI_6_2_GTDT_SBSA_GENERIC_WATCHDOG_STRUCTURE
*)
162 ((UINT8
*)Gtdt
+ WatchdogOffset
);
164 while (WatchdogCount
-- != 0) {
165 // Add watchdog entry
166 DEBUG ((DEBUG_INFO
, "GTDT: Watchdog = 0x%p\n", Watchdog
));
167 Watchdog
->Type
= EFI_ACPI_6_2_GTDT_SBSA_GENERIC_WATCHDOG
;
169 sizeof (EFI_ACPI_6_2_GTDT_SBSA_GENERIC_WATCHDOG_STRUCTURE
);
170 Watchdog
->Reserved
= EFI_ACPI_RESERVED_BYTE
;
171 Watchdog
->RefreshFramePhysicalAddress
=
172 WatchdogInfoList
->RefreshFrameAddress
;
173 Watchdog
->WatchdogControlFramePhysicalAddress
=
174 WatchdogInfoList
->ControlFrameAddress
;
175 Watchdog
->WatchdogTimerGSIV
= WatchdogInfoList
->TimerGSIV
;
176 Watchdog
->WatchdogTimerFlags
= WatchdogInfoList
->Flags
;
182 /** Update the GT Block Timer Frame lists in the GTDT Table.
184 @param [in] GtBlockFrame Pointer to the GT Block Frames
186 @param [in] GTBlockTimerFrameList Pointer to the GT Block Frame
188 @param [in] GTBlockFrameCount Number of GT Block Frames.
190 @retval EFI_SUCCESS Table generated successfully.
191 @retval EFI_INVALID_PARAMETER A parameter is invalid.
195 AddGTBlockTimerFrames (
196 IN EFI_ACPI_6_2_GTDT_GT_BLOCK_TIMER_STRUCTURE
* GtBlockFrame
,
197 IN CONST CM_ARM_GTBLOCK_TIMER_FRAME_INFO
* GTBlockTimerFrameList
,
198 IN UINT32 GTBlockFrameCount
201 ASSERT (GtBlockFrame
!= NULL
);
202 ASSERT (GTBlockTimerFrameList
!= NULL
);
204 if (GTBlockFrameCount
> 8) {
207 "ERROR: GTDT: GT Block Frame Count %d is greater than 8\n",
210 ASSERT (GTBlockFrameCount
<= 8);
211 return EFI_INVALID_PARAMETER
;
214 while (GTBlockFrameCount
-- != 0) {
217 "GTDT: GtBlockFrame = 0x%p\n",
221 if (GTBlockTimerFrameList
->FrameNumber
>= 8) {
224 "ERROR: GTDT: Frame number %d is not in the range 0-7\n",
225 GTBlockTimerFrameList
->FrameNumber
227 return EFI_INVALID_PARAMETER
;
230 GtBlockFrame
->GTFrameNumber
= GTBlockTimerFrameList
->FrameNumber
;
231 GtBlockFrame
->Reserved
[0] = EFI_ACPI_RESERVED_BYTE
;
232 GtBlockFrame
->Reserved
[1] = EFI_ACPI_RESERVED_BYTE
;
233 GtBlockFrame
->Reserved
[2] = EFI_ACPI_RESERVED_BYTE
;
235 GtBlockFrame
->CntBaseX
= GTBlockTimerFrameList
->PhysicalAddressCntBase
;
236 GtBlockFrame
->CntEL0BaseX
=
237 GTBlockTimerFrameList
->PhysicalAddressCntEL0Base
;
239 GtBlockFrame
->GTxPhysicalTimerGSIV
=
240 GTBlockTimerFrameList
->PhysicalTimerGSIV
;
241 GtBlockFrame
->GTxPhysicalTimerFlags
=
242 GTBlockTimerFrameList
->PhysicalTimerFlags
;
244 GtBlockFrame
->GTxVirtualTimerGSIV
= GTBlockTimerFrameList
->VirtualTimerGSIV
;
245 GtBlockFrame
->GTxVirtualTimerFlags
=
246 GTBlockTimerFrameList
->VirtualTimerFlags
;
248 GtBlockFrame
->GTxCommonFlags
= GTBlockTimerFrameList
->CommonFlags
;
250 GTBlockTimerFrameList
++;
255 /** Add the GT Block Timers in the GTDT Table.
257 @param [in] CfgMgrProtocol Pointer to the Configuration Manager
259 @param [in] Gtdt Pointer to the GTDT Table.
260 @param [in] GTBlockOffset Offset of the GT Block
261 information in the GTDT Table.
262 @param [in] GTBlockInfo Pointer to the GT Block
264 @param [in] BlockTimerCount Number of GT Block Timers.
266 @retval EFI_SUCCESS Table generated successfully.
267 @retval EFI_INVALID_PARAMETER A parameter is invalid.
272 IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL
* CONST CfgMgrProtocol
,
273 IN EFI_ACPI_6_2_GENERIC_TIMER_DESCRIPTION_TABLE
* CONST Gtdt
,
274 IN CONST UINT32 GTBlockOffset
,
275 IN CONST CM_ARM_GTBLOCK_INFO
* GTBlockInfo
,
276 IN UINT32 BlockTimerCount
280 EFI_ACPI_6_2_GTDT_GT_BLOCK_STRUCTURE
* GTBlock
;
281 EFI_ACPI_6_2_GTDT_GT_BLOCK_TIMER_STRUCTURE
* GtBlockFrame
;
282 CM_ARM_GTBLOCK_TIMER_FRAME_INFO
* GTBlockTimerFrameList
;
283 UINT32 GTBlockTimerFrameCount
;
285 ASSERT (Gtdt
!= NULL
);
286 ASSERT (GTBlockInfo
!= NULL
);
288 GTBlock
= (EFI_ACPI_6_2_GTDT_GT_BLOCK_STRUCTURE
*)((UINT8
*)Gtdt
+
291 while (BlockTimerCount
-- != 0) {
292 DEBUG ((DEBUG_INFO
, "GTDT: GTBlock = 0x%p\n", GTBlock
));
294 Status
= GetEArmObjGTBlockTimerFrameInfo (
296 GTBlockInfo
->GTBlockTimerFrameToken
,
297 >BlockTimerFrameList
,
298 >BlockTimerFrameCount
300 if (EFI_ERROR (Status
) ||
301 (GTBlockTimerFrameCount
!= GTBlockInfo
->GTBlockTimerFrameCount
)) {
304 "ERROR: GTDT: Failed to get Generic Timer Frames. Status = %r\n",
310 GTBlock
->Type
= EFI_ACPI_6_2_GTDT_GT_BLOCK
;
311 GTBlock
->Length
= sizeof (EFI_ACPI_6_2_GTDT_GT_BLOCK_STRUCTURE
) +
312 (sizeof (EFI_ACPI_6_2_GTDT_GT_BLOCK_TIMER_STRUCTURE
) *
313 GTBlockInfo
->GTBlockTimerFrameCount
);
315 GTBlock
->Reserved
= EFI_ACPI_RESERVED_BYTE
;
316 GTBlock
->CntCtlBase
= GTBlockInfo
->GTBlockPhysicalAddress
;
317 GTBlock
->GTBlockTimerCount
= GTBlockInfo
->GTBlockTimerFrameCount
;
318 GTBlock
->GTBlockTimerOffset
=
319 sizeof (EFI_ACPI_6_2_GTDT_GT_BLOCK_STRUCTURE
);
321 GtBlockFrame
= (EFI_ACPI_6_2_GTDT_GT_BLOCK_TIMER_STRUCTURE
*)
322 ((UINT8
*)GTBlock
+ GTBlock
->GTBlockTimerOffset
);
324 // Add GT Block Timer frames
325 Status
= AddGTBlockTimerFrames (
327 GTBlockTimerFrameList
,
328 GTBlockTimerFrameCount
330 if (EFI_ERROR (Status
)) {
333 "ERROR: GTDT: Failed to add Generic Timer Frames. Status = %r\n",
340 GTBlock
= (EFI_ACPI_6_2_GTDT_GT_BLOCK_STRUCTURE
*)((UINT8
*)GTBlock
+
347 /** Construct the GTDT ACPI table.
349 Called by the Dynamic Table Manager, this function invokes the
350 Configuration Manager protocol interface to get the required hardware
351 information for generating the ACPI table.
353 If this function allocates any resources then they must be freed
354 in the FreeXXXXTableResources function.
356 @param [in] This Pointer to the table generator.
357 @param [in] AcpiTableInfo Pointer to the ACPI Table Info.
358 @param [in] CfgMgrProtocol Pointer to the Configuration Manager
360 @param [out] Table Pointer to the constructed ACPI Table.
362 @retval EFI_SUCCESS Table generated successfully.
363 @retval EFI_INVALID_PARAMETER A parameter is invalid.
364 @retval EFI_NOT_FOUND The required object was not found.
365 @retval EFI_BAD_BUFFER_SIZE The size returned by the Configuration
366 Manager is less than the Object size for the
368 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
374 IN CONST ACPI_TABLE_GENERATOR
* CONST This
,
375 IN CONST CM_STD_OBJ_ACPI_TABLE_INFO
* CONST AcpiTableInfo
,
376 IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL
* CONST CfgMgrProtocol
,
377 OUT EFI_ACPI_DESCRIPTION_HEADER
** CONST Table
382 UINT32 PlatformTimerCount
;
383 UINT32 WatchdogCount
;
384 UINT32 BlockTimerCount
;
385 CM_ARM_GENERIC_WATCHDOG_INFO
* WatchdogInfoList
;
386 CM_ARM_GTBLOCK_INFO
* GTBlockInfo
;
387 EFI_ACPI_6_2_GENERIC_TIMER_DESCRIPTION_TABLE
* Gtdt
;
389 UINT32 GTBlockOffset
;
390 UINT32 WatchdogOffset
;
392 ASSERT (This
!= NULL
);
393 ASSERT (AcpiTableInfo
!= NULL
);
394 ASSERT (CfgMgrProtocol
!= NULL
);
395 ASSERT (Table
!= NULL
);
396 ASSERT (AcpiTableInfo
->TableGeneratorId
== This
->GeneratorID
);
397 ASSERT (AcpiTableInfo
->AcpiTableSignature
== This
->AcpiTableSignature
);
399 if ((AcpiTableInfo
->AcpiTableRevision
< This
->MinAcpiTableRevision
) ||
400 (AcpiTableInfo
->AcpiTableRevision
> This
->AcpiTableRevision
)) {
403 "ERROR: GTDT: Requested table revision = %d, is not supported."
404 "Supported table revision: Minimum = %d, Maximum = %d\n",
405 AcpiTableInfo
->AcpiTableRevision
,
406 This
->MinAcpiTableRevision
,
407 This
->AcpiTableRevision
409 return EFI_INVALID_PARAMETER
;
413 Status
= GetEArmObjPlatformGTBlockInfo (
419 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_FOUND
)) {
422 "ERROR: GTDT: Failed to Get Platform GT Block Information." \
429 Status
= GetEArmObjPlatformGenericWatchdogInfo (
435 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_FOUND
)) {
438 "ERROR: GTDT: Failed to Get Platform Generic Watchdog Information." \
447 "GTDT: BlockTimerCount = %d, WatchdogCount = %d\n",
452 // Calculate the GTDT Table Size
453 PlatformTimerCount
= 0;
454 TableSize
= sizeof (EFI_ACPI_6_2_GENERIC_TIMER_DESCRIPTION_TABLE
);
455 if (BlockTimerCount
!= 0) {
456 GTBlockOffset
= TableSize
;
457 PlatformTimerCount
+= BlockTimerCount
;
458 TableSize
+= (sizeof (EFI_ACPI_6_2_GTDT_GT_BLOCK_STRUCTURE
) *
461 for (Idx
= 0; Idx
< BlockTimerCount
; Idx
++) {
462 if (GTBlockInfo
[Idx
].GTBlockTimerFrameCount
> 8) {
463 Status
= EFI_INVALID_PARAMETER
;
466 "GTDT: GTBockFrameCount cannot be more than 8." \
467 " GTBockFrameCount = %d, Status = %r\n",
468 GTBlockInfo
[Idx
].GTBlockTimerFrameCount
,
473 TableSize
+= (sizeof (EFI_ACPI_6_2_GTDT_GT_BLOCK_TIMER_STRUCTURE
) *
474 GTBlockInfo
[Idx
].GTBlockTimerFrameCount
);
479 "GTDT: GTBockOffset = 0x%x, PLATFORM_TIMER_COUNT = %d\n",
486 if (WatchdogCount
!= 0) {
487 WatchdogOffset
= TableSize
;
488 PlatformTimerCount
+= WatchdogCount
;
489 TableSize
+= (sizeof (EFI_ACPI_6_2_GTDT_SBSA_GENERIC_WATCHDOG_STRUCTURE
) *
493 "GTDT: WatchdogOffset = 0x%x, PLATFORM_TIMER_COUNT = %d\n",
499 *Table
= (EFI_ACPI_DESCRIPTION_HEADER
*)AllocateZeroPool (TableSize
);
500 if (*Table
== NULL
) {
501 Status
= EFI_OUT_OF_RESOURCES
;
504 "ERROR: GTDT: Failed to allocate memory for GTDT Table, Size = %d," \
512 Gtdt
= (EFI_ACPI_6_2_GENERIC_TIMER_DESCRIPTION_TABLE
*)*Table
;
515 "GTDT: Gtdt = 0x%p TableSize = 0x%x\n",
520 Status
= AddAcpiHeader (
527 if (EFI_ERROR (Status
)) {
530 "ERROR: GTDT: Failed to add ACPI header. Status = %r\n",
536 Status
= AddGenericTimerInfo (
541 if (EFI_ERROR (Status
)) {
544 "ERROR: GTDT: Failed to add Generic Timer Info. Status = %r\n",
550 if (BlockTimerCount
!= 0) {
551 Status
= AddGTBlockList (
558 if (EFI_ERROR (Status
)) {
561 "ERROR: GTDT: Failed to add GT Block timers. Status = %r\n",
568 if (WatchdogCount
!= 0) {
569 AddGenericWatchdogList (
580 if (*Table
!= NULL
) {
587 /** Free any resources allocated for constructing the GTDT.
589 @param [in] This Pointer to the table generator.
590 @param [in] AcpiTableInfo Pointer to the ACPI Table Info.
591 @param [in] CfgMgrProtocol Pointer to the Configuration Manager
593 @param [in, out] Table Pointer to the ACPI Table.
595 @retval EFI_SUCCESS The resources were freed successfully.
596 @retval EFI_INVALID_PARAMETER The table pointer is NULL or invalid.
600 FreeGtdtTableResources (
601 IN CONST ACPI_TABLE_GENERATOR
* CONST This
,
602 IN CONST CM_STD_OBJ_ACPI_TABLE_INFO
* CONST AcpiTableInfo
,
603 IN CONST EDKII_CONFIGURATION_MANAGER_PROTOCOL
* CONST CfgMgrProtocol
,
604 IN OUT EFI_ACPI_DESCRIPTION_HEADER
** CONST Table
607 ASSERT (This
!= NULL
);
608 ASSERT (AcpiTableInfo
!= NULL
);
609 ASSERT (CfgMgrProtocol
!= NULL
);
610 ASSERT (AcpiTableInfo
->TableGeneratorId
== This
->GeneratorID
);
611 ASSERT (AcpiTableInfo
->AcpiTableSignature
== This
->AcpiTableSignature
);
613 if ((Table
== NULL
) || (*Table
== NULL
)) {
614 DEBUG ((DEBUG_ERROR
, "ERROR: GTDT: Invalid Table Pointer\n"));
615 ASSERT ((Table
!= NULL
) && (*Table
!= NULL
));
616 return EFI_INVALID_PARAMETER
;
624 /** This macro defines the GTDT Table Generator revision.
626 #define GTDT_GENERATOR_REVISION CREATE_REVISION (1, 0)
628 /** The interface for the GTDT Table Generator.
632 ACPI_TABLE_GENERATOR GtdtGenerator
= {
634 CREATE_STD_ACPI_TABLE_GEN_ID (EStdAcpiTableIdGtdt
),
635 // Generator Description
636 L
"ACPI.STD.GTDT.GENERATOR",
637 // ACPI Table Signature
638 EFI_ACPI_6_2_GENERIC_TIMER_DESCRIPTION_TABLE_SIGNATURE
,
639 // ACPI Table Revision supported by this Generator
640 EFI_ACPI_6_2_GENERIC_TIMER_DESCRIPTION_TABLE_REVISION
,
641 // Minimum ACPI Table Revision supported by this Generator
642 EFI_ACPI_6_2_GENERIC_TIMER_DESCRIPTION_TABLE_REVISION
,
644 TABLE_GENERATOR_CREATOR_ID_ARM
,
646 GTDT_GENERATOR_REVISION
,
647 // Build Table function
649 // Free Resource function
650 FreeGtdtTableResources
,
651 // Extended build function not needed
653 // Extended build function not implemented by the generator.
654 // Hence extended free resource function is not required.
658 /** Register the Generator with the ACPI Table Factory.
660 @param [in] ImageHandle The handle to the image.
661 @param [in] SystemTable Pointer to the System Table.
663 @retval EFI_SUCCESS The Generator is registered.
664 @retval EFI_INVALID_PARAMETER A parameter is invalid.
665 @retval EFI_ALREADY_STARTED The Generator for the Table ID
666 is already registered.
670 AcpiGtdtLibConstructor (
671 IN CONST EFI_HANDLE ImageHandle
,
672 IN EFI_SYSTEM_TABLE
* CONST SystemTable
676 Status
= RegisterAcpiTableGenerator (&GtdtGenerator
);
677 DEBUG ((DEBUG_INFO
, "GTDT: Register Generator. Status = %r\n", Status
));
678 ASSERT_EFI_ERROR (Status
);
682 /** Deregister the Generator from the ACPI Table Factory.
684 @param [in] ImageHandle The handle to the image.
685 @param [in] SystemTable Pointer to the System Table.
687 @retval EFI_SUCCESS The Generator is deregistered.
688 @retval EFI_INVALID_PARAMETER A parameter is invalid.
689 @retval EFI_NOT_FOUND The Generator is not registered.
693 AcpiGtdtLibDestructor (
694 IN CONST EFI_HANDLE ImageHandle
,
695 IN EFI_SYSTEM_TABLE
* CONST SystemTable
699 Status
= DeregisterAcpiTableGenerator (&GtdtGenerator
);
700 DEBUG ((DEBUG_INFO
, "GTDT: Deregister Generator. Status = %r\n", Status
));
701 ASSERT_EFI_ERROR (Status
);