]> git.proxmox.com Git - mirror_edk2.git/blob - ArmVirtPkg/XenAcpiPlatformDxe/XenAcpiPlatformDxe.c
438b0ff0f0e2e2b4c74470b80d8ffbbdcc296c9f
[mirror_edk2.git] / ArmVirtPkg / XenAcpiPlatformDxe / XenAcpiPlatformDxe.c
1 /** @file
2 Xen ARM ACPI Platform Driver using Xen ARM multiboot protocol
3
4 Copyright (C) 2016, Linaro Ltd. All rights reserved.
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include <Library/BaseLib.h>
11 #include <Library/DebugLib.h>
12 #include <Library/UefiBootServicesTableLib.h>
13 #include <Library/UefiDriverEntryPoint.h>
14
15 #include <Protocol/AcpiTable.h>
16 #include <Protocol/FdtClient.h>
17
18 #include <IndustryStandard/Acpi.h>
19
20 /**
21 Get the address of Xen ACPI Root System Description Pointer (RSDP)
22 structure.
23
24 @param RsdpStructurePtr Return pointer to RSDP structure
25
26 @return EFI_SUCCESS Find Xen RSDP structure successfully.
27 @return EFI_NOT_FOUND Don't find Xen RSDP structure.
28 @return EFI_ABORTED Find Xen RSDP structure, but it's not integrated.
29
30 **/
31 STATIC
32 EFI_STATUS
33 EFIAPI
34 GetXenArmAcpiRsdp (
35 OUT EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER **RsdpPtr
36 )
37 {
38 EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER *RsdpStructurePtr;
39 EFI_STATUS Status;
40 FDT_CLIENT_PROTOCOL *FdtClient;
41 CONST UINT64 *Reg;
42 UINT32 RegSize;
43 UINTN AddressCells, SizeCells;
44 UINT64 RegBase;
45 UINT8 Sum;
46
47 RsdpStructurePtr = NULL;
48 FdtClient = NULL;
49 //
50 // Get the RSDP structure address from DeviceTree
51 //
52 Status = gBS->LocateProtocol (&gFdtClientProtocolGuid, NULL,
53 (VOID **)&FdtClient);
54 ASSERT_EFI_ERROR (Status);
55
56 Status = FdtClient->FindCompatibleNodeReg (FdtClient, "xen,guest-acpi",
57 (CONST VOID **)&Reg, &AddressCells, &SizeCells,
58 &RegSize);
59 if (EFI_ERROR (Status)) {
60 DEBUG ((DEBUG_WARN, "%a: No 'xen,guest-acpi' compatible DT node found\n",
61 __FUNCTION__));
62 return EFI_NOT_FOUND;
63 }
64
65 ASSERT (AddressCells == 2);
66 ASSERT (SizeCells == 2);
67 ASSERT (RegSize == 2 * sizeof (UINT64));
68
69 RegBase = SwapBytes64(Reg[0]);
70 RsdpStructurePtr =
71 (EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER *)(UINTN)RegBase;
72
73 if (RsdpStructurePtr && RsdpStructurePtr->Revision >= 2) {
74 Sum = CalculateSum8 ((CONST UINT8 *)RsdpStructurePtr,
75 sizeof (EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER));
76 if (Sum != 0) {
77 return EFI_ABORTED;
78 }
79 }
80
81 *RsdpPtr = RsdpStructurePtr;
82 return EFI_SUCCESS;
83 }
84
85 /**
86 Get Xen Acpi tables from the RSDP structure. And installs Xen ACPI tables
87 into the RSDT/XSDT using InstallAcpiTable. Some signature of the installed
88 ACPI tables are: FACP, APIC, GTDT, DSDT.
89
90 @param AcpiProtocol Protocol instance pointer.
91
92 @return EFI_SUCCESS The table was successfully inserted.
93 @return EFI_INVALID_PARAMETER Either AcpiTableBuffer is NULL, TableHandle is
94 NULL, or AcpiTableBufferSize and the size
95 field embedded in the ACPI table pointed to
96 by AcpiTableBuffer are not in sync.
97 @return EFI_OUT_OF_RESOURCES Insufficient resources exist to complete the request.
98
99 **/
100 STATIC
101 EFI_STATUS
102 EFIAPI
103 InstallXenArmTables (
104 IN EFI_ACPI_TABLE_PROTOCOL *AcpiProtocol
105 )
106 {
107 EFI_STATUS Status;
108 UINTN TableHandle;
109 VOID *CurrentTableEntry;
110 UINTN CurrentTablePointer;
111 EFI_ACPI_DESCRIPTION_HEADER *CurrentTable;
112 UINTN Index;
113 UINTN NumberOfTableEntries;
114 EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER *XenAcpiRsdpStructurePtr;
115 EFI_ACPI_DESCRIPTION_HEADER *Xsdt;
116 EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE *FadtTable;
117 EFI_ACPI_DESCRIPTION_HEADER *DsdtTable;
118
119 XenAcpiRsdpStructurePtr = NULL;
120 FadtTable = NULL;
121 DsdtTable = NULL;
122 TableHandle = 0;
123 NumberOfTableEntries = 0;
124
125 //
126 // Try to find Xen ARM ACPI tables
127 //
128 Status = GetXenArmAcpiRsdp (&XenAcpiRsdpStructurePtr);
129 if (EFI_ERROR (Status)) {
130 DEBUG ((DEBUG_INFO, "%a: No RSDP table found\n", __FUNCTION__));
131 return Status;
132 }
133
134 //
135 // If XSDT table is find, just install its tables.
136 //
137 if (XenAcpiRsdpStructurePtr->XsdtAddress) {
138 //
139 // Retrieve the addresses of XSDT and
140 // calculate the number of its table entries.
141 //
142 Xsdt = (EFI_ACPI_DESCRIPTION_HEADER *) (UINTN)
143 XenAcpiRsdpStructurePtr->XsdtAddress;
144 NumberOfTableEntries = (Xsdt->Length -
145 sizeof (EFI_ACPI_DESCRIPTION_HEADER)) /
146 sizeof (UINT64);
147 //
148 // Install ACPI tables found in XSDT.
149 //
150 for (Index = 0; Index < NumberOfTableEntries; Index++) {
151 //
152 // Get the table entry from XSDT
153 //
154 CurrentTableEntry = (VOID *) ((UINT8 *) Xsdt +
155 sizeof (EFI_ACPI_DESCRIPTION_HEADER) +
156 Index * sizeof (UINT64));
157 CurrentTablePointer = (UINTN) *(UINT64 *)CurrentTableEntry;
158 CurrentTable = (EFI_ACPI_DESCRIPTION_HEADER *) CurrentTablePointer;
159
160 //
161 // Install the XSDT tables
162 //
163 Status = AcpiProtocol->InstallAcpiTable (
164 AcpiProtocol,
165 CurrentTable,
166 CurrentTable->Length,
167 &TableHandle
168 );
169
170 if (EFI_ERROR (Status)) {
171 return Status;
172 }
173
174 //
175 // Get the FACS and DSDT table address from the table FADT
176 //
177 if (!AsciiStrnCmp ((CHAR8 *) &CurrentTable->Signature, "FACP", 4)) {
178 FadtTable = (EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE *)
179 (UINTN) CurrentTablePointer;
180 DsdtTable = (EFI_ACPI_DESCRIPTION_HEADER *) (UINTN) FadtTable->Dsdt;
181 }
182 }
183 }
184
185 //
186 // Install DSDT table.
187 //
188 Status = AcpiProtocol->InstallAcpiTable (
189 AcpiProtocol,
190 DsdtTable,
191 DsdtTable->Length,
192 &TableHandle
193 );
194 if (EFI_ERROR (Status)) {
195 return Status;
196 }
197
198 return EFI_SUCCESS;
199 }
200
201 STATIC
202 EFI_ACPI_TABLE_PROTOCOL *
203 FindAcpiTableProtocol (
204 VOID
205 )
206 {
207 EFI_STATUS Status;
208 EFI_ACPI_TABLE_PROTOCOL *AcpiTable;
209
210 AcpiTable = NULL;
211 Status = gBS->LocateProtocol (
212 &gEfiAcpiTableProtocolGuid,
213 NULL,
214 (VOID**)&AcpiTable
215 );
216 ASSERT_EFI_ERROR (Status);
217 return AcpiTable;
218 }
219
220 /**
221 Entrypoint of Xen ARM Acpi Platform driver.
222
223 @param ImageHandle
224 @param SystemTable
225
226 @return EFI_SUCCESS
227 @return EFI_LOAD_ERROR
228 @return EFI_OUT_OF_RESOURCES
229
230 **/
231
232 EFI_STATUS
233 EFIAPI
234 XenAcpiPlatformEntryPoint (
235 IN EFI_HANDLE ImageHandle,
236 IN EFI_SYSTEM_TABLE *SystemTable
237 )
238 {
239 EFI_STATUS Status;
240
241 Status = InstallXenArmTables (FindAcpiTableProtocol ());
242 return Status;
243 }