2 * Device tree enumeration DXE driver for ARM Virtual Machines
4 * Copyright (c) 2014, Linaro Ltd. All rights reserved.<BR>
6 * This program and the accompanying materials are
7 * 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
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.
16 #include <Library/BaseLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/UefiLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/UefiDriverEntryPoint.h>
21 #include <Library/MemoryAllocationLib.h>
22 #include <Library/UefiBootServicesTableLib.h>
23 #include <Library/VirtioMmioDeviceLib.h>
24 #include <Library/DevicePathLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/DxeServicesLib.h>
27 #include <Library/HobLib.h>
29 #include <Library/XenIoMmioLib.h>
32 #include <Guid/VirtioMmioTransport.h>
33 #include <Guid/FdtHob.h>
37 VENDOR_DEVICE_PATH Vendor
;
39 EFI_DEVICE_PATH_PROTOCOL End
;
40 } VIRTIO_TRANSPORT_DEVICE_PATH
;
55 STATIC CONST PROPERTY CompatibleProperties
[] = {
56 { PropertyTypeVirtio
, "virtio,mmio" },
57 { PropertyTypeUart
, "arm,pl011" },
58 { PropertyTypeXen
, "xen,xen" },
59 { PropertyTypeUnknown
, "" }
65 IN CONST CHAR8
*NodeType
,
69 CONST CHAR8
*Compatible
;
70 CONST PROPERTY
*CompatibleProperty
;
73 // A 'compatible' node may contain a sequence of NULL terminated
74 // compatible strings so check each one
76 for (Compatible
= NodeType
; Compatible
< NodeType
+ Size
&& *Compatible
;
77 Compatible
+= 1 + AsciiStrLen (Compatible
)) {
78 for (CompatibleProperty
= CompatibleProperties
; CompatibleProperty
->Compatible
[0]; CompatibleProperty
++) {
79 if (AsciiStrCmp (CompatibleProperty
->Compatible
, Compatible
) == 0) {
80 return CompatibleProperty
->Type
;
84 return PropertyTypeUnknown
;
89 InitializeVirtFdtDxe (
90 IN EFI_HANDLE ImageHandle
,
91 IN EFI_SYSTEM_TABLE
*SystemTable
100 PROPERTY_TYPE PropType
;
102 VIRTIO_TRANSPORT_DEVICE_PATH
*DevicePath
;
106 Hob
= GetFirstGuidHob(&gFdtHobGuid
);
107 if (Hob
== NULL
|| GET_GUID_HOB_DATA_SIZE (Hob
) != sizeof (UINT64
)) {
108 return EFI_NOT_FOUND
;
110 DeviceTreeBase
= (VOID
*)(UINTN
)*(UINT64
*)GET_GUID_HOB_DATA (Hob
);
112 if (fdt_check_header (DeviceTreeBase
) != 0) {
113 DEBUG ((EFI_D_ERROR
, "%a: No DTB found @ 0x%p\n", __FUNCTION__
, DeviceTreeBase
));
114 return EFI_NOT_FOUND
;
117 DEBUG ((EFI_D_INFO
, "%a: DTB @ 0x%p\n", __FUNCTION__
, DeviceTreeBase
));
120 // Now enumerate the nodes and install peripherals that we are interested in,
121 // i.e., GIC, RTC and virtio MMIO nodes
123 for (Prev
= 0;; Prev
= Node
) {
124 Node
= fdt_next_node (DeviceTreeBase
, Prev
, NULL
);
129 Type
= fdt_getprop (DeviceTreeBase
, Node
, "compatible", &Len
);
134 PropType
= GetTypeFromNode (Type
, Len
);
135 if (PropType
== PropertyTypeUnknown
) {
140 // Get the 'reg' property of this node. For now, we will assume
141 // 8 byte quantities for base and size, respectively.
142 // TODO use #cells root properties instead
144 RegProp
= fdt_getprop (DeviceTreeBase
, Node
, "reg", &Len
);
145 ASSERT (RegProp
!= NULL
);
148 case PropertyTypeVirtio
:
151 // Create a unique device path for this transport on the fly
153 RegBase
= fdt64_to_cpu (((UINT64
*)RegProp
)[0]);
154 DevicePath
= (VIRTIO_TRANSPORT_DEVICE_PATH
*)CreateDeviceNode (
155 HARDWARE_DEVICE_PATH
,
157 sizeof (VIRTIO_TRANSPORT_DEVICE_PATH
));
158 if (DevicePath
== NULL
) {
159 DEBUG ((EFI_D_ERROR
, "%a: Out of memory\n", __FUNCTION__
));
163 CopyMem (&DevicePath
->Vendor
.Guid
, &gVirtioMmioTransportGuid
,
165 DevicePath
->PhysBase
= RegBase
;
166 SetDevicePathNodeLength (&DevicePath
->Vendor
,
167 sizeof (*DevicePath
) - sizeof (DevicePath
->End
));
168 SetDevicePathEndNode (&DevicePath
->End
);
171 Status
= gBS
->InstallProtocolInterface (&Handle
,
172 &gEfiDevicePathProtocolGuid
, EFI_NATIVE_INTERFACE
,
174 if (EFI_ERROR (Status
)) {
175 DEBUG ((EFI_D_ERROR
, "%a: Failed to install the EFI_DEVICE_PATH "
176 "protocol on a new handle (Status == %r)\n",
177 __FUNCTION__
, Status
));
178 FreePool (DevicePath
);
182 Status
= VirtioMmioInstallDevice (RegBase
, Handle
);
183 if (EFI_ERROR (Status
)) {
184 DEBUG ((EFI_D_ERROR
, "%a: Failed to install VirtIO transport @ 0x%Lx "
185 "on handle %p (Status == %r)\n", __FUNCTION__
, RegBase
,
188 Status
= gBS
->UninstallProtocolInterface (Handle
,
189 &gEfiDevicePathProtocolGuid
, DevicePath
);
190 ASSERT_EFI_ERROR (Status
);
191 FreePool (DevicePath
);
195 case PropertyTypeXen
:
199 // Retrieve the reg base from this node and wire it up to the
200 // MMIO flavor of the XenBus root device I/O protocol
202 RegBase
= fdt64_to_cpu (((UINT64
*)RegProp
)[0]);
204 Status
= XenIoMmioInstall (&Handle
, RegBase
);
205 if (EFI_ERROR (Status
)) {
206 DEBUG ((EFI_D_ERROR
, "%a: XenIoMmioInstall () failed on a new handle "
207 "(Status == %r)\n", __FUNCTION__
, Status
));
211 DEBUG ((EFI_D_INFO
, "Found Xen node with Grant table @ 0x%Lx\n", RegBase
));
220 if (!FeaturePcdGet (PcdPureAcpiBoot
)) {
222 // Only install the FDT as a configuration table if we want to leave it up
223 // to the OS to decide whether it prefers ACPI over DT.
225 Status
= gBS
->InstallConfigurationTable (&gFdtTableGuid
, DeviceTreeBase
);
226 ASSERT_EFI_ERROR (Status
);