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
;
55 STATIC CONST PROPERTY CompatibleProperties
[] = {
56 { PropertyTypeGic
, "arm,cortex-a15-gic" },
57 { PropertyTypeRtc
, "arm,pl031" },
58 { PropertyTypeVirtio
, "virtio,mmio" },
59 { PropertyTypeUart
, "arm,pl011" },
60 { PropertyTypeTimer
, "arm,armv7-timer" },
61 { PropertyTypeTimer
, "arm,armv8-timer" },
62 { PropertyTypePsci
, "arm,psci-0.2" },
63 { PropertyTypeFwCfg
, "qemu,fw-cfg-mmio" },
64 { PropertyTypeUnknown
, "" }
76 IN CONST CHAR8
*NodeType
,
80 CONST CHAR8
*Compatible
;
81 CONST PROPERTY
*CompatibleProperty
;
84 // A 'compatible' node may contain a sequence of NULL terminated
85 // compatible strings so check each one
87 for (Compatible
= NodeType
; Compatible
< NodeType
+ Size
&& *Compatible
;
88 Compatible
+= 1 + AsciiStrLen (Compatible
)) {
89 for (CompatibleProperty
= CompatibleProperties
; CompatibleProperty
->Compatible
[0]; CompatibleProperty
++) {
90 if (AsciiStrCmp (CompatibleProperty
->Compatible
, Compatible
) == 0) {
91 return CompatibleProperty
->Type
;
95 return PropertyTypeUnknown
;
100 InitializeVirtFdtDxe (
101 IN EFI_HANDLE ImageHandle
,
102 IN EFI_SYSTEM_TABLE
*SystemTable
105 VOID
*DeviceTreeBase
;
111 PROPERTY_TYPE PropType
;
113 VIRTIO_TRANSPORT_DEVICE_PATH
*DevicePath
;
116 UINT64 DistBase
, CpuBase
;
117 CONST INTERRUPT_PROPERTY
*InterruptProp
;
118 INT32 SecIntrNum
, IntrNum
, VirtIntrNum
, HypIntrNum
;
119 CONST CHAR8
*PsciMethod
;
120 UINT64 FwCfgSelectorAddress
;
121 UINT64 FwCfgSelectorSize
;
122 UINT64 FwCfgDataAddress
;
123 UINT64 FwCfgDataSize
;
125 DeviceTreeBase
= (VOID
*)(UINTN
)PcdGet64 (PcdDeviceTreeBaseAddress
);
126 ASSERT (DeviceTreeBase
!= NULL
);
128 if (fdt_check_header (DeviceTreeBase
) != 0) {
129 DEBUG ((EFI_D_ERROR
, "%a: No DTB found @ 0x%p\n", __FUNCTION__
, DeviceTreeBase
));
130 return EFI_NOT_FOUND
;
133 Status
= gBS
->InstallConfigurationTable (&gFdtTableGuid
, DeviceTreeBase
);
134 ASSERT_EFI_ERROR (Status
);
136 DEBUG ((EFI_D_INFO
, "%a: DTB @ 0x%p\n", __FUNCTION__
, DeviceTreeBase
));
140 // Now enumerate the nodes and install peripherals that we are interested in,
141 // i.e., GIC, RTC and virtio MMIO nodes
143 for (Prev
= 0;; Prev
= Node
) {
144 Node
= fdt_next_node (DeviceTreeBase
, Prev
, NULL
);
149 Type
= fdt_getprop (DeviceTreeBase
, Node
, "compatible", &Len
);
154 PropType
= GetTypeFromNode (Type
, Len
);
155 if (PropType
== PropertyTypeUnknown
) {
160 // Get the 'reg' property of this node. For now, we will assume
161 // 8 byte quantities for base and size, respectively.
162 // TODO use #cells root properties instead
164 RegProp
= fdt_getprop (DeviceTreeBase
, Node
, "reg", &Len
);
165 ASSERT ((RegProp
!= NULL
) || (PropType
== PropertyTypeTimer
) ||
166 (PropType
== PropertyTypePsci
));
169 case PropertyTypeFwCfg
:
170 ASSERT (Len
== 2 * sizeof (UINT64
));
172 FwCfgDataAddress
= fdt64_to_cpu (((UINT64
*)RegProp
)[0]);
174 FwCfgSelectorAddress
= FwCfgDataAddress
+ FwCfgDataSize
;
175 FwCfgSelectorSize
= 2;
178 // The following ASSERT()s express
180 // Address + Size - 1 <= MAX_UINTN
182 // for both registers, that is, that the last byte in each MMIO range is
183 // expressible as a MAX_UINTN. The form below is mathematically
184 // equivalent, and it also prevents any unsigned overflow before the
187 ASSERT (FwCfgSelectorAddress
<= MAX_UINTN
- FwCfgSelectorSize
+ 1);
188 ASSERT (FwCfgDataAddress
<= MAX_UINTN
- FwCfgDataSize
+ 1);
190 PcdSet64 (PcdFwCfgSelectorAddress
, FwCfgSelectorAddress
);
191 PcdSet64 (PcdFwCfgDataAddress
, FwCfgDataAddress
);
193 DEBUG ((EFI_D_INFO
, "Found FwCfg @ 0x%Lx/0x%Lx\n", FwCfgSelectorAddress
,
197 case PropertyTypeVirtio
:
200 // Create a unique device path for this transport on the fly
202 RegBase
= fdt64_to_cpu (((UINT64
*)RegProp
)[0]);
203 DevicePath
= (VIRTIO_TRANSPORT_DEVICE_PATH
*)CreateDeviceNode (
204 HARDWARE_DEVICE_PATH
,
206 sizeof (VIRTIO_TRANSPORT_DEVICE_PATH
));
207 if (DevicePath
== NULL
) {
208 DEBUG ((EFI_D_ERROR
, "%a: Out of memory\n", __FUNCTION__
));
212 CopyMem (&DevicePath
->Vendor
.Guid
, &gEfiCallerIdGuid
, sizeof (EFI_GUID
));
213 DevicePath
->PhysBase
= RegBase
;
214 SetDevicePathNodeLength (&DevicePath
->Vendor
,
215 sizeof (*DevicePath
) - sizeof (DevicePath
->End
));
216 SetDevicePathEndNode (&DevicePath
->End
);
219 Status
= gBS
->InstallProtocolInterface (&Handle
,
220 &gEfiDevicePathProtocolGuid
, EFI_NATIVE_INTERFACE
,
222 if (EFI_ERROR (Status
)) {
223 DEBUG ((EFI_D_ERROR
, "%a: Failed to install the EFI_DEVICE_PATH "
224 "protocol on a new handle (Status == %r)\n",
225 __FUNCTION__
, Status
));
226 FreePool (DevicePath
);
230 Status
= VirtioMmioInstallDevice (RegBase
, Handle
);
231 if (EFI_ERROR (Status
)) {
232 DEBUG ((EFI_D_ERROR
, "%a: Failed to install VirtIO transport @ 0x%Lx "
233 "on handle %p (Status == %r)\n", __FUNCTION__
, RegBase
,
236 Status
= gBS
->UninstallProtocolInterface (Handle
,
237 &gEfiDevicePathProtocolGuid
, DevicePath
);
238 ASSERT_EFI_ERROR (Status
);
239 FreePool (DevicePath
);
243 case PropertyTypeGic
:
246 DistBase
= fdt64_to_cpu (((UINT64
*)RegProp
)[0]);
247 CpuBase
= fdt64_to_cpu (((UINT64
*)RegProp
)[2]);
248 ASSERT (DistBase
< MAX_UINT32
);
249 ASSERT (CpuBase
< MAX_UINT32
);
251 PcdSet32 (PcdGicDistributorBase
, (UINT32
)DistBase
);
252 PcdSet32 (PcdGicInterruptInterfaceBase
, (UINT32
)CpuBase
);
254 DEBUG ((EFI_D_INFO
, "Found GIC @ 0x%Lx/0x%Lx\n", DistBase
, CpuBase
));
257 case PropertyTypeRtc
:
260 RegBase
= fdt64_to_cpu (((UINT64
*)RegProp
)[0]);
261 ASSERT (RegBase
< MAX_UINT32
);
263 PcdSet32 (PcdPL031RtcBase
, (UINT32
)RegBase
);
265 DEBUG ((EFI_D_INFO
, "Found PL031 RTC @ 0x%Lx\n", RegBase
));
269 case PropertyTypeTimer
:
271 // - interrupts : Interrupt list for secure, non-secure, virtual and
272 // hypervisor timers, in that order.
274 InterruptProp
= fdt_getprop (DeviceTreeBase
, Node
, "interrupts", &Len
);
277 SecIntrNum
= fdt32_to_cpu (InterruptProp
[0].Number
)
278 + (InterruptProp
[0].Type
? 16 : 0);
279 IntrNum
= fdt32_to_cpu (InterruptProp
[1].Number
)
280 + (InterruptProp
[1].Type
? 16 : 0);
281 VirtIntrNum
= fdt32_to_cpu (InterruptProp
[2].Number
)
282 + (InterruptProp
[2].Type
? 16 : 0);
283 HypIntrNum
= fdt32_to_cpu (InterruptProp
[3].Number
)
284 + (InterruptProp
[3].Type
? 16 : 0);
286 DEBUG ((EFI_D_INFO
, "Found Timer interrupts %d, %d, %d, %d\n",
287 SecIntrNum
, IntrNum
, VirtIntrNum
, HypIntrNum
));
289 PcdSet32 (PcdArmArchTimerSecIntrNum
, SecIntrNum
);
290 PcdSet32 (PcdArmArchTimerIntrNum
, IntrNum
);
291 PcdSet32 (PcdArmArchTimerVirtIntrNum
, VirtIntrNum
);
292 PcdSet32 (PcdArmArchTimerHypIntrNum
, HypIntrNum
);
295 case PropertyTypePsci
:
296 PsciMethod
= fdt_getprop (DeviceTreeBase
, Node
, "method", &Len
);
298 if (PsciMethod
&& AsciiStrnCmp (PsciMethod
, "hvc", 3) == 0) {
299 PcdSet32 (PcdArmPsciMethod
, 1);
300 } else if (PsciMethod
&& AsciiStrnCmp (PsciMethod
, "smc", 3) == 0) {
301 PcdSet32 (PcdArmPsciMethod
, 2);
303 DEBUG ((EFI_D_ERROR
, "%a: Unknown PSCI method \"%a\"\n", __FUNCTION__
,
314 // UEFI takes ownership of the RTC hardware, and exposes its functionality
315 // through the UEFI Runtime Services GetTime, SetTime, etc. This means we
316 // need to disable it in the device tree to prevent the OS from attaching its
317 // device driver as well.
319 if ((RtcNode
!= -1) &&
320 fdt_setprop_string (DeviceTreeBase
, RtcNode
, "status",
322 DEBUG ((EFI_D_WARN
, "Failed to set PL031 status to 'disabled'\n"));