1/** @file
2*  Generic Timer Description Table (GTDT)
3*
4*  Copyright (c) 2012 - 2014, ARM Limited. All rights reserved.
5*
6*  This program and the accompanying materials
7*  are licensed and made available under the terms and conditions of the BSD License
8*  which accompanies this distribution.  The full text of the license may be found at
9*  http://opensource.org/licenses/bsd-license.php
10*
11*  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12*  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13*
14**/
15
16#include "ArmPlatform.h"
17#include <Library/AcpiLib.h>
18#include <Library/PcdLib.h>
19#include <IndustryStandard/Acpi.h>
20
21#define GTDT_GLOBAL_FLAGS_MAPPED      EFI_ACPI_5_0_GTDT_GLOBAL_FLAG_MEMORY_MAPPED_BLOCK_PRESENT
22#define GTDT_GLOBAL_FLAGS_NOT_MAPPED  0
23#define GTDT_GLOBAL_FLAGS_EDGE        EFI_ACPI_5_0_GTDT_GLOBAL_FLAG_INTERRUPT_MODE
24#define GTDT_GLOBAL_FLAGS_LEVEL       0
25
26// Note: We could have a build flag that switches between memory mapped/non-memory mapped timer
27#ifdef SYSTEM_TIMER_BASE_ADDRESS
28  #define GTDT_GLOBAL_FLAGS             (GTDT_GLOBAL_FLAGS_MAPPED | GTDT_GLOBAL_FLAGS_LEVEL)
29#else
30  #define GTDT_GLOBAL_FLAGS             (GTDT_GLOBAL_FLAGS_NOT_MAPPED | GTDT_GLOBAL_FLAGS_LEVEL)
31  #define SYSTEM_TIMER_BASE_ADDRESS     0xFFFFFFFFFFFFFFFF
32#endif
33
34#define GTDT_TIMER_EDGE_TRIGGERED   EFI_ACPI_5_0_GTDT_TIMER_FLAG_TIMER_INTERRUPT_MODE
35#define GTDT_TIMER_LEVEL_TRIGGERED  0
36#define GTDT_TIMER_ACTIVE_LOW       EFI_ACPI_5_0_GTDT_TIMER_FLAG_TIMER_INTERRUPT_POLARITY
37#define GTDT_TIMER_ACTIVE_HIGH      0
38
39#define GTDT_GTIMER_FLAGS           (GTDT_TIMER_ACTIVE_LOW | GTDT_TIMER_LEVEL_TRIGGERED)
40
41#ifdef ARM_JUNO_ACPI_5_0
42  EFI_ACPI_5_0_GENERIC_TIMER_DESCRIPTION_TABLE Gtdt = {
43    ARM_ACPI_HEADER(
44      EFI_ACPI_5_0_GENERIC_TIMER_DESCRIPTION_TABLE_SIGNATURE,
45      EFI_ACPI_5_0_GENERIC_TIMER_DESCRIPTION_TABLE,
46      EFI_ACPI_5_0_GENERIC_TIMER_DESCRIPTION_TABLE_REVISION
47    ),
48    SYSTEM_TIMER_BASE_ADDRESS,                    // UINT64  PhysicalAddress
49    GTDT_GLOBAL_FLAGS,                            // UINT32  GlobalFlags
50    FixedPcdGet32 (PcdArmArchTimerSecIntrNum),    // UINT32  SecurePL1TimerGSIV
51    GTDT_GTIMER_FLAGS,                            // UINT32  SecurePL1TimerFlags
52    FixedPcdGet32 (PcdArmArchTimerIntrNum),       // UINT32  NonSecurePL1TimerGSIV
53    GTDT_GTIMER_FLAGS,                            // UINT32  NonSecurePL1TimerFlags
54    FixedPcdGet32 (PcdArmArchTimerVirtIntrNum),   // UINT32  VirtualTimerGSIV
55    GTDT_GTIMER_FLAGS,                            // UINT32  VirtualTimerFlags
56    FixedPcdGet32 (PcdArmArchTimerHypIntrNum),    // UINT32  NonSecurePL2TimerGSIV
57    GTDT_GTIMER_FLAGS                             // UINT32  NonSecurePL2TimerFlags
58  };
59#else
60  #pragma pack (1)
61
62  typedef struct {
63    EFI_ACPI_5_1_GENERIC_TIMER_DESCRIPTION_TABLE          Gtdt;
64    EFI_ACPI_5_1_GTDT_SBSA_GENERIC_WATCHDOG_STRUCTURE     Watchdogs[JUNO_WATCHDOG_COUNT];
65  } EFI_ACPI_5_1_GENERIC_TIMER_DESCRIPTION_TABLES;
66
67  #pragma pack ()
68
69  EFI_ACPI_5_1_GENERIC_TIMER_DESCRIPTION_TABLES Gtdt = {
70    {
71      ARM_ACPI_HEADER(
72        EFI_ACPI_5_1_GENERIC_TIMER_DESCRIPTION_TABLE_SIGNATURE,
73        EFI_ACPI_5_1_GENERIC_TIMER_DESCRIPTION_TABLE,
74        EFI_ACPI_5_1_GENERIC_TIMER_DESCRIPTION_TABLE_REVISION
75      ),
76      SYSTEM_TIMER_BASE_ADDRESS,                    // UINT64  PhysicalAddress
77      0,                                            // UINT32  Reserved
78      FixedPcdGet32 (PcdArmArchTimerSecIntrNum),    // UINT32  SecurePL1TimerGSIV
79      GTDT_GTIMER_FLAGS,                            // UINT32  SecurePL1TimerFlags
80      FixedPcdGet32 (PcdArmArchTimerIntrNum),       // UINT32  NonSecurePL1TimerGSIV
81      GTDT_GTIMER_FLAGS,                            // UINT32  NonSecurePL1TimerFlags
82      FixedPcdGet32 (PcdArmArchTimerVirtIntrNum),   // UINT32  VirtualTimerGSIV
83      GTDT_GTIMER_FLAGS,                            // UINT32  VirtualTimerFlags
84      FixedPcdGet32 (PcdArmArchTimerHypIntrNum),    // UINT32  NonSecurePL2TimerGSIV
85      GTDT_GTIMER_FLAGS,                            // UINT32  NonSecurePL2TimerFlags
86      0xFFFFFFFFFFFFFFFF,                           // UINT64  CntReadBasePhysicalAddress
87      JUNO_WATCHDOG_COUNT,                          // UINT32  PlatformTimerCount
88      sizeof (EFI_ACPI_5_1_GENERIC_TIMER_DESCRIPTION_TABLE) // UINT32 PlatfromTimerOffset
89    },
90    {
91      EFI_ACPI_5_1_SBSA_GENERIC_WATCHDOG_STRUCTURE_INIT(
92          FixedPcdGet32 (PcdGenericWatchdogRefreshBase), FixedPcdGet32 (PcdGenericWatchdogControlBase), 93, 0),
93      EFI_ACPI_5_1_SBSA_GENERIC_WATCHDOG_STRUCTURE_INIT(
94          FixedPcdGet32 (PcdGenericWatchdogRefreshBase), FixedPcdGet32 (PcdGenericWatchdogControlBase), 94, EFI_ACPI_5_1_GTDT_SBSA_GENERIC_WATCHDOG_FLAG_SECURE_TIMER)
95    }
96  };
97#endif
98
99//
100// Reference the table being generated to prevent the optimizer from removing the
101// data structure from the executable
102//
103VOID* CONST ReferenceAcpiTable = &Gtdt;
104