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