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>
33 VENDOR_DEVICE_PATH Vendor
;
35 EFI_DEVICE_PATH_PROTOCOL End
;
36 } VIRTIO_TRANSPORT_DEVICE_PATH
;
54 STATIC CONST PROPERTY CompatibleProperties
[] = {
55 { PropertyTypeGic
, "arm,cortex-a15-gic" },
56 { PropertyTypeRtc
, "arm,pl031" },
57 { PropertyTypeVirtio
, "virtio,mmio" },
58 { PropertyTypeUart
, "arm,pl011" },
59 { PropertyTypeTimer
, "arm,armv7-timer" },
60 { PropertyTypeTimer
, "arm,armv8-timer" },
61 { PropertyTypePsci
, "arm,psci-0.2" },
62 { PropertyTypeUnknown
, "" }
74 IN CONST CHAR8
*NodeType
,
78 CONST CHAR8
*Compatible
;
79 CONST PROPERTY
*CompatibleProperty
;
82 // A 'compatible' node may contain a sequence of NULL terminated
83 // compatible strings so check each one
85 for (Compatible
= NodeType
; Compatible
< NodeType
+ Size
&& *Compatible
;
86 Compatible
+= 1 + AsciiStrLen (Compatible
)) {
87 for (CompatibleProperty
= CompatibleProperties
; CompatibleProperty
->Compatible
[0]; CompatibleProperty
++) {
88 if (AsciiStrCmp (CompatibleProperty
->Compatible
, Compatible
) == 0) {
89 return CompatibleProperty
->Type
;
93 return PropertyTypeUnknown
;
98 InitializeVirtFdtDxe (
99 IN EFI_HANDLE ImageHandle
,
100 IN EFI_SYSTEM_TABLE
*SystemTable
103 VOID
*DeviceTreeBase
;
109 PROPERTY_TYPE PropType
;
111 VIRTIO_TRANSPORT_DEVICE_PATH
*DevicePath
;
114 UINT64 DistBase
, CpuBase
;
115 CONST INTERRUPT_PROPERTY
*InterruptProp
;
116 INT32 SecIntrNum
, IntrNum
, VirtIntrNum
, HypIntrNum
;
117 CONST CHAR8
*PsciMethod
;
119 DeviceTreeBase
= (VOID
*)(UINTN
)PcdGet64 (PcdDeviceTreeBaseAddress
);
120 ASSERT (DeviceTreeBase
!= NULL
);
122 if (fdt_check_header (DeviceTreeBase
) != 0) {
123 DEBUG ((EFI_D_ERROR
, "%a: No DTB found @ 0x%p\n", __FUNCTION__
, DeviceTreeBase
));
124 return EFI_NOT_FOUND
;
127 Status
= gBS
->InstallConfigurationTable (&gFdtTableGuid
, DeviceTreeBase
);
128 ASSERT_EFI_ERROR (Status
);
130 DEBUG ((EFI_D_INFO
, "%a: DTB @ 0x%p\n", __FUNCTION__
, DeviceTreeBase
));
134 // Now enumerate the nodes and install peripherals that we are interested in,
135 // i.e., GIC, RTC and virtio MMIO nodes
137 for (Prev
= 0;; Prev
= Node
) {
138 Node
= fdt_next_node (DeviceTreeBase
, Prev
, NULL
);
143 Type
= fdt_getprop (DeviceTreeBase
, Node
, "compatible", &Len
);
148 PropType
= GetTypeFromNode (Type
, Len
);
149 if (PropType
== PropertyTypeUnknown
) {
154 // Get the 'reg' property of this node. For now, we will assume
155 // 8 byte quantities for base and size, respectively.
156 // TODO use #cells root properties instead
158 RegProp
= fdt_getprop (DeviceTreeBase
, Node
, "reg", &Len
);
159 ASSERT ((RegProp
!= NULL
) || (PropType
== PropertyTypeTimer
) ||
160 (PropType
== PropertyTypePsci
));
163 case PropertyTypeVirtio
:
166 // Create a unique device path for this transport on the fly
168 RegBase
= fdt64_to_cpu (((UINT64
*)RegProp
)[0]);
169 DevicePath
= (VIRTIO_TRANSPORT_DEVICE_PATH
*)CreateDeviceNode (
170 HARDWARE_DEVICE_PATH
,
172 sizeof (VIRTIO_TRANSPORT_DEVICE_PATH
));
173 if (DevicePath
== NULL
) {
174 DEBUG ((EFI_D_ERROR
, "%a: Out of memory\n", __FUNCTION__
));
178 CopyMem (&DevicePath
->Vendor
.Guid
, &gEfiCallerIdGuid
, sizeof (EFI_GUID
));
179 DevicePath
->PhysBase
= RegBase
;
180 SetDevicePathNodeLength (&DevicePath
->Vendor
,
181 sizeof (*DevicePath
) - sizeof (DevicePath
->End
));
182 SetDevicePathEndNode (&DevicePath
->End
);
185 Status
= gBS
->InstallProtocolInterface (&Handle
,
186 &gEfiDevicePathProtocolGuid
, EFI_NATIVE_INTERFACE
,
188 if (EFI_ERROR (Status
)) {
189 DEBUG ((EFI_D_ERROR
, "%a: Failed to install the EFI_DEVICE_PATH "
190 "protocol on a new handle (Status == %r)\n",
191 __FUNCTION__
, Status
));
192 FreePool (DevicePath
);
196 Status
= VirtioMmioInstallDevice (RegBase
, Handle
);
197 if (EFI_ERROR (Status
)) {
198 DEBUG ((EFI_D_ERROR
, "%a: Failed to install VirtIO transport @ 0x%Lx "
199 "on handle %p (Status == %r)\n", __FUNCTION__
, RegBase
,
202 Status
= gBS
->UninstallProtocolInterface (Handle
,
203 &gEfiDevicePathProtocolGuid
, DevicePath
);
204 ASSERT_EFI_ERROR (Status
);
205 FreePool (DevicePath
);
209 case PropertyTypeGic
:
212 DistBase
= fdt64_to_cpu (((UINT64
*)RegProp
)[0]);
213 CpuBase
= fdt64_to_cpu (((UINT64
*)RegProp
)[2]);
214 ASSERT (DistBase
< MAX_UINT32
);
215 ASSERT (CpuBase
< MAX_UINT32
);
217 PcdSet32 (PcdGicDistributorBase
, (UINT32
)DistBase
);
218 PcdSet32 (PcdGicInterruptInterfaceBase
, (UINT32
)CpuBase
);
220 DEBUG ((EFI_D_INFO
, "Found GIC @ 0x%Lx/0x%Lx\n", DistBase
, CpuBase
));
223 case PropertyTypeRtc
:
226 RegBase
= fdt64_to_cpu (((UINT64
*)RegProp
)[0]);
227 ASSERT (RegBase
< MAX_UINT32
);
229 PcdSet32 (PcdPL031RtcBase
, (UINT32
)RegBase
);
231 DEBUG ((EFI_D_INFO
, "Found PL031 RTC @ 0x%Lx\n", RegBase
));
235 case PropertyTypeTimer
:
237 // - interrupts : Interrupt list for secure, non-secure, virtual and
238 // hypervisor timers, in that order.
240 InterruptProp
= fdt_getprop (DeviceTreeBase
, Node
, "interrupts", &Len
);
243 SecIntrNum
= fdt32_to_cpu (InterruptProp
[0].Number
)
244 + (InterruptProp
[0].Type
? 16 : 0);
245 IntrNum
= fdt32_to_cpu (InterruptProp
[1].Number
)
246 + (InterruptProp
[1].Type
? 16 : 0);
247 VirtIntrNum
= fdt32_to_cpu (InterruptProp
[2].Number
)
248 + (InterruptProp
[2].Type
? 16 : 0);
249 HypIntrNum
= fdt32_to_cpu (InterruptProp
[3].Number
)
250 + (InterruptProp
[3].Type
? 16 : 0);
252 DEBUG ((EFI_D_INFO
, "Found Timer interrupts %d, %d, %d, %d\n",
253 SecIntrNum
, IntrNum
, VirtIntrNum
, HypIntrNum
));
255 PcdSet32 (PcdArmArchTimerSecIntrNum
, SecIntrNum
);
256 PcdSet32 (PcdArmArchTimerIntrNum
, IntrNum
);
257 PcdSet32 (PcdArmArchTimerVirtIntrNum
, VirtIntrNum
);
258 PcdSet32 (PcdArmArchTimerHypIntrNum
, HypIntrNum
);
261 case PropertyTypePsci
:
262 PsciMethod
= fdt_getprop (DeviceTreeBase
, Node
, "method", &Len
);
264 if (PsciMethod
&& AsciiStrnCmp (PsciMethod
, "hvc", 3) == 0) {
265 PcdSet32 (PcdArmPsciMethod
, 1);
266 } else if (PsciMethod
&& AsciiStrnCmp (PsciMethod
, "smc", 3) == 0) {
267 PcdSet32 (PcdArmPsciMethod
, 2);
269 DEBUG ((EFI_D_ERROR
, "%a: Unknown PSCI method \"%a\"\n", __FUNCTION__
,
280 // UEFI takes ownership of the RTC hardware, and exposes its functionality
281 // through the UEFI Runtime Services GetTime, SetTime, etc. This means we
282 // need to disable it in the device tree to prevent the OS from attaching its
283 // device driver as well.
285 if ((RtcNode
!= -1) &&
286 fdt_setprop_string (DeviceTreeBase
, RtcNode
, "status",
288 DEBUG ((EFI_D_WARN
, "Failed to set PL031 status to 'disabled'\n"));