From patchwork Thu Aug 28 14:13:58 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 36219 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-vc0-f200.google.com (mail-vc0-f200.google.com [209.85.220.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id D3FE6202DD for ; Thu, 28 Aug 2014 14:15:02 +0000 (UTC) Received: by mail-vc0-f200.google.com with SMTP id hq11sf3223495vcb.3 for ; Thu, 28 Aug 2014 07:15:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:subject:precedence:reply-to:list-id:list-unsubscribe :list-archive:list-post:list-help:list-subscribe:mime-version :errors-to:x-original-sender:x-original-authentication-results :mailing-list:content-type:content-transfer-encoding; bh=n2ynRnt7b7eh6syqK+KNLbwPB1O747dsVD0E/6g3FOY=; b=feNiFY4TRH6lEfDnMl7EnG+kgl0rTSKymcikezsZi7L10SNGUukjMOX80lswkg3bU4 6tSjJQUOba2OBOc2q6t0D8drc5laZc43fdgmuj41sBX3Ih2yVqIVPa6h/+AJjurOVJwo f6P3MfCTez6D92z0SUZxbOWQS2LZGEezcVu9fARn2U9MNujR6TtY3hhuGOO1mcYOlaiR jkVYTKqtFrZd26O7KiA0TPVbNk9Z0mABoUfdlJt8UoqLFFQbNbWkoJ0VWiG/ISt8IJgy LSHCQk0zRSuTxZRGbS0V5VAXRMdMvbA41zZrgmToc1HLkEx5vjVb3ihmrETMFSY3ftR1 XVPw== X-Gm-Message-State: ALoCoQlIgj1LB8E2aTIFMaXa70+npkZMJD5GFxKHoQz/FWVWGjRL18KdGTS9W6jlYU2PZbnQKkG5 X-Received: by 10.236.63.67 with SMTP id z43mr336263yhc.11.1409235302510; Thu, 28 Aug 2014 07:15:02 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.49.83 with SMTP id p77ls373788qga.97.gmail; Thu, 28 Aug 2014 07:15:02 -0700 (PDT) X-Received: by 10.220.186.196 with SMTP id ct4mr941168vcb.51.1409235302358; Thu, 28 Aug 2014 07:15:02 -0700 (PDT) Received: from mail-vc0-f169.google.com (mail-vc0-f169.google.com [209.85.220.169]) by mx.google.com with ESMTPS id fc9si3597990vdc.2.2014.08.28.07.14.57 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 28 Aug 2014 07:14:57 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.169 as permitted sender) client-ip=209.85.220.169; Received: by mail-vc0-f169.google.com with SMTP id hq11so884561vcb.0 for ; Thu, 28 Aug 2014 07:14:57 -0700 (PDT) X-Received: by 10.52.129.200 with SMTP id ny8mr2804207vdb.27.1409235297843; Thu, 28 Aug 2014 07:14:57 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.45.67 with SMTP id uj3csp243485vcb; Thu, 28 Aug 2014 07:14:57 -0700 (PDT) X-Received: by 10.50.164.167 with SMTP id yr7mr37840707igb.37.1409235297161; Thu, 28 Aug 2014 07:14:57 -0700 (PDT) Received: from lists.sourceforge.net (lists.sourceforge.net. [216.34.181.88]) by mx.google.com with ESMTPS id vn9si9372532igb.55.2014.08.28.07.14.56 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 28 Aug 2014 07:14:57 -0700 (PDT) Received-SPF: pass (google.com: domain of edk2-devel-bounces@lists.sourceforge.net designates 216.34.181.88 as permitted sender) client-ip=216.34.181.88; Received: from localhost ([127.0.0.1] helo=sfs-ml-2.v29.ch3.sourceforge.com) by sfs-ml-2.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1XN0TP-0002uV-I7; Thu, 28 Aug 2014 14:14:47 +0000 Received: from sog-mx-2.v43.ch3.sourceforge.com ([172.29.43.192] helo=mx.sourceforge.net) by sfs-ml-2.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1XN0TO-0002uA-3q for edk2-devel@lists.sourceforge.net; Thu, 28 Aug 2014 14:14:46 +0000 Received-SPF: pass (sog-mx-2.v43.ch3.sourceforge.com: domain of linaro.org designates 74.125.82.182 as permitted sender) client-ip=74.125.82.182; envelope-from=ard.biesheuvel@linaro.org; helo=mail-we0-f182.google.com; Received: from mail-we0-f182.google.com ([74.125.82.182]) by sog-mx-2.v43.ch3.sourceforge.com with esmtps (TLSv1:RC4-SHA:128) (Exim 4.76) id 1XN0TM-0002Sz-NK for edk2-devel@lists.sourceforge.net; Thu, 28 Aug 2014 14:14:46 +0000 Received: by mail-we0-f182.google.com with SMTP id w62so813689wes.41 for ; Thu, 28 Aug 2014 07:14:38 -0700 (PDT) X-Received: by 10.194.78.4 with SMTP id x4mr5730286wjw.44.1409235278399; Thu, 28 Aug 2014 07:14:38 -0700 (PDT) Received: from ards-macbook-pro.local (BSN-61-108-214.dial-up.dsl.siol.net. [86.61.108.214]) by mx.google.com with ESMTPSA id bk6sm10278657wjb.26.2014.08.28.07.14.36 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 28 Aug 2014 07:14:37 -0700 (PDT) From: Ard Biesheuvel To: lersek@redhat.com, olivier.martin@arm.com, edk2-devel@lists.sourceforge.net, peter.maydell@linaro.org, christoffer.dall@linaro.org, drjones@redhat.com, ilias.biris@linaro.org, leif.lindholm@linaro.org Date: Thu, 28 Aug 2014 16:13:58 +0200 Message-Id: <1409235244-25783-11-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 1.8.3.2 In-Reply-To: <1409235244-25783-1-git-send-email-ard.biesheuvel@linaro.org> References: <1409235244-25783-1-git-send-email-ard.biesheuvel@linaro.org> X-Spam-Score: -1.5 (-) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -1.5 SPF_CHECK_PASS SPF reports sender host as permitted sender for sender-domain -0.0 SPF_PASS SPF: sender matches SPF record X-Headers-End: 1XN0TM-0002Sz-NK Subject: [edk2] [PATCH v4 10/16] ArmVirtualizationPkg: add VirtFdtDxe driver X-BeenThere: edk2-devel@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list Reply-To: edk2-devel@lists.sourceforge.net List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.sourceforge.net X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: ard.biesheuvel@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.169 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 This driver enumerates the device nodes in the device tree located at the base address passed in gArmTokenSpaceGuid.PcdDeviceTreeBaseAddress, and installs drivers for devices it cares about (GIC interrupt controller, RTC, architected timer interrupt) Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ard Biesheuvel --- .../ArmVirtualizationPkg/VirtFdtDxe/VirtFdtDxe.c | 261 +++++++++++++++++++++ .../ArmVirtualizationPkg/VirtFdtDxe/VirtFdtDxe.inf | 59 +++++ 2 files changed, 320 insertions(+) create mode 100644 ArmPlatformPkg/ArmVirtualizationPkg/VirtFdtDxe/VirtFdtDxe.c create mode 100644 ArmPlatformPkg/ArmVirtualizationPkg/VirtFdtDxe/VirtFdtDxe.inf diff --git a/ArmPlatformPkg/ArmVirtualizationPkg/VirtFdtDxe/VirtFdtDxe.c b/ArmPlatformPkg/ArmVirtualizationPkg/VirtFdtDxe/VirtFdtDxe.c new file mode 100644 index 000000000000..28c34c1cb094 --- /dev/null +++ b/ArmPlatformPkg/ArmVirtualizationPkg/VirtFdtDxe/VirtFdtDxe.c @@ -0,0 +1,261 @@ +/** @file +* Device tree enumeration DXE driver for ARM Virtual Machines +* +* Copyright (c) 2014, Linaro Ltd. All rights reserved.
+* This program and the accompanying materials are +* licensed and made available under the terms and conditions of the BSD License +* which accompanies this distribution. The full text of the license may be found at +* http://opensource.org/licenses/bsd-license.php +* +* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +* +**/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +CONST UINT32 mUint32Max = 0xFFFFFFFFU; + +#pragma pack (1) +typedef struct { + VENDOR_DEVICE_PATH Vendor; + UINT64 PhysBase; + EFI_DEVICE_PATH_PROTOCOL End; +} VIRTIO_TRANSPORT_DEVICE_PATH; +#pragma pack () + +typedef enum { + PropertyTypeUnknown, + PropertyTypeGic, + PropertyTypeRtc, + PropertyTypeVirtio, + PropertyTypeUart, + PropertyTypeTimer, +} PROPERTY_TYPE; + +typedef struct { + PROPERTY_TYPE Type; + CHAR8 Compatible[20]; +} PROPERTY; + +STATIC CONST PROPERTY CompatibleProperties[] = { + { PropertyTypeGic, "arm,cortex-a15-gic" }, + { PropertyTypeRtc, "arm,pl031" }, + { PropertyTypeVirtio, "virtio,mmio" }, + { PropertyTypeUart, "arm,pl011" }, + { PropertyTypeTimer, "arm,armv7-timer" }, + { PropertyTypeTimer, "arm,armv8-timer" }, + { PropertyTypeUnknown, "" } +}; + +typedef struct { + UINT32 Type; + UINT32 Number; + UINT32 Flags; +} INTERRUPT_PROP; + +STATIC +PROPERTY_TYPE +GetTypeFromNode ( + IN CONST CHAR8 *NodeType, + IN UINTN Size + ) +{ + CONST CHAR8 *Compatible; + + // + // A 'compatible' node may contain a sequence of NULL terminated + // compatible strings so check each one + // + for (Compatible = NodeType; Compatible < NodeType + Size && *Compatible; + Compatible += 1 + AsciiStrLen (Compatible)) { + + CONST PROPERTY *CompProp; + + for (CompProp = CompatibleProperties; CompProp->Compatible[0]; CompProp++) { + if (AsciiStrCmp (CompProp->Compatible, Compatible) == 0) { + return CompProp->Type; + } + } + } + return PropertyTypeUnknown; +} + +EFI_STATUS +EFIAPI +InitializeVirtFdtDxe ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + VOID *DeviceTreeBase; + INT32 Node, Prev; + EFI_STATUS Status; + + DeviceTreeBase = (VOID *)(UINTN)PcdGet64 (PcdDeviceTreeBaseAddress); + ASSERT (DeviceTreeBase != NULL); + + if (fdt_check_header (DeviceTreeBase) != 0) { + DEBUG ((EFI_D_ERROR, "%a: No DTB found @ 0x%p\n", + __FUNCTION__, DeviceTreeBase)); + return EFI_NOT_FOUND; + } + + Status = gBS->InstallConfigurationTable (&gFdtTableGuid, DeviceTreeBase); + ASSERT_EFI_ERROR (Status); + + DEBUG ((EFI_D_INFO, "%a: DTB @ 0x%p\n", __FUNCTION__, DeviceTreeBase)); + + // + // Now enumerate the nodes and install peripherals that we are interested in, + // i.e., GIC, RTC and virtio MMIO nodes + // + for (Prev = 0;; Prev = Node) { + CONST CHAR8 *Type; + INT32 Len; + PROPERTY_TYPE PropType; + CONST VOID *RegProp; + VIRTIO_TRANSPORT_DEVICE_PATH *DevicePath; + EFI_HANDLE Handle; + UINT64 RegBase; + UINT64 DistBase, CpuBase; + CONST INTERRUPT_PROP *InterruptProp; + INT32 SecIntrNum, IntrNum, VirtIntrNum, HypIntrNum; + + Node = fdt_next_node (DeviceTreeBase, Prev, NULL); + if (Node < 0) { + break; + } + + Type = fdt_getprop (DeviceTreeBase, Node, "compatible", &Len); + if (Type == NULL) { + continue; + } + + PropType = GetTypeFromNode (Type, Len); + if (PropType == PropertyTypeUnknown) { + continue; + } + + // + // Get the 'reg' property of this node. For now, we will assume + // 8 byte quantities for base and size, respectively. + // TODO use #cells root properties instead + // + RegProp = fdt_getprop (DeviceTreeBase, Node, "reg", &Len); + ASSERT (RegProp != NULL || PropType == PropertyTypeTimer); + + switch (PropType) { + + case PropertyTypeVirtio: + ASSERT (Len == 16); + // + // Create a unique device path for this transport on the fly + // + RegBase = fdt64_to_cpu (((UINT64 *)RegProp)[0]); + DevicePath = (VIRTIO_TRANSPORT_DEVICE_PATH *)CreateDeviceNode ( + HARDWARE_DEVICE_PATH, + HW_VENDOR_DP, + sizeof (VIRTIO_TRANSPORT_DEVICE_PATH)); + + CopyMem (&DevicePath->Vendor.Guid, &gEfiCallerIdGuid, sizeof (EFI_GUID)); + DevicePath->PhysBase = RegBase; + SetDevicePathNodeLength (&DevicePath->Vendor, + sizeof (*DevicePath) - sizeof (DevicePath->End)); + SetDevicePathEndNode (&DevicePath->End); + + Handle = NULL; + Status = gBS->InstallProtocolInterface (&Handle, + &gEfiDevicePathProtocolGuid, EFI_NATIVE_INTERFACE, + DevicePath); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "%a: Failed to install the EFI_DEVICE_PATH " + "protocol on a new handle (Status == %r)\n", + __FUNCTION__, Status)); + break; + } + + Status = VirtioMmioInstallDevice (RegBase, Handle); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "%a: Failed to install VirtIO transport @ 0x%Lx " + "on handle %p\n (Status == %r)", + __FUNCTION__, RegBase, Handle, Status)); + + Status = gBS->UninstallProtocolInterface (Handle, + &gEfiDevicePathProtocolGuid, + EFI_NATIVE_INTERFACE); + ASSERT_EFI_ERROR (Status); + } + break; + + case PropertyTypeGic: + ASSERT (Len == 32); + + DistBase = fdt64_to_cpu (((UINT64 *)RegProp)[0]); + CpuBase = fdt64_to_cpu (((UINT64 *)RegProp)[2]); + ASSERT (DistBase < mUint32Max); + ASSERT (CpuBase < mUint32Max); + + PcdSet32 (PcdGicDistributorBase, (UINT32)DistBase); + PcdSet32 (PcdGicInterruptInterfaceBase, (UINT32)CpuBase); + + DEBUG ((EFI_D_INFO, "Found GIC @ 0x%Lx/0x%Lx\n", DistBase, CpuBase)); + break; + + case PropertyTypeRtc: + ASSERT (Len == 16); + + RegBase = fdt64_to_cpu (((UINT64 *)RegProp)[0]); + ASSERT (RegBase < mUint32Max); + + PcdSet32 (PcdPL031RtcBase, (UINT32)RegBase); + + DEBUG ((EFI_D_INFO, "Found PL031 RTC @ 0x%Lx\n", RegBase)); + break; + + case PropertyTypeTimer: + + // + // - interrupts : Interrupt list for secure, non-secure, virtual and + // hypervisor timers, in that order. + // + InterruptProp = fdt_getprop (DeviceTreeBase, Node, "interrupts", &Len); + ASSERT (Len == 48); + + SecIntrNum = fdt32_to_cpu (InterruptProp[0].Number) + + (InterruptProp[0].Type ? 16 : 0); + IntrNum = fdt32_to_cpu (InterruptProp[1].Number) + + (InterruptProp[1].Type ? 16 : 0); + VirtIntrNum = fdt32_to_cpu (InterruptProp[2].Number) + + (InterruptProp[2].Type ? 16 : 0); + HypIntrNum = fdt32_to_cpu (InterruptProp[3].Number) + + (InterruptProp[3].Type ? 16 : 0); + + DEBUG ((EFI_D_INFO, "Found Timer interrupts %d, %d, %d, %d\n", + SecIntrNum, IntrNum, VirtIntrNum, HypIntrNum)); + + PcdSet32 (PcdArmArchTimerSecIntrNum, SecIntrNum); + PcdSet32 (PcdArmArchTimerIntrNum, IntrNum); + PcdSet32 (PcdArmArchTimerVirtIntrNum, VirtIntrNum); + PcdSet32 (PcdArmArchTimerHypIntrNum, HypIntrNum); + break; + + default: + break; + } + } + return Status; +} diff --git a/ArmPlatformPkg/ArmVirtualizationPkg/VirtFdtDxe/VirtFdtDxe.inf b/ArmPlatformPkg/ArmVirtualizationPkg/VirtFdtDxe/VirtFdtDxe.inf new file mode 100644 index 000000000000..4c16d0032704 --- /dev/null +++ b/ArmPlatformPkg/ArmVirtualizationPkg/VirtFdtDxe/VirtFdtDxe.inf @@ -0,0 +1,59 @@ +## @file +# Device tree enumeration DXE driver for ARM Virtual Machines +# +# Copyright (c) 2014, Linaro Ltd. All rights reserved.
+# This program and the accompanying materials are +# licensed and made available under the terms and conditions of the BSD License +# which accompanies this distribution. The full text of the license may be found at +# http://opensource.org/licenses/bsd-license.php +# +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# +## + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = VirtFdtDxe + FILE_GUID = 837DCA9E-E874-4D82-B29A-23FE0E23D1E2 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + + ENTRY_POINT = InitializeVirtFdtDxe + +[Sources] + VirtFdtDxe.c + +[Packages] + MdePkg/MdePkg.dec + ArmPkg/ArmPkg.dec + ArmPlatformPkg/ArmPlatformPkg.dec + EmbeddedPkg/EmbeddedPkg.dec + OvmfPkg/OvmfPkg.dec + +[LibraryClasses] + BaseLib + PcdLib + UefiDriverEntryPoint + DxeServicesLib + FdtLib + VirtioMmioDeviceLib + +[Guids] + gFdtTableGuid + +[Pcd] + gArmTokenSpaceGuid.PcdDeviceTreeBaseAddress + gArmTokenSpaceGuid.PcdGicDistributorBase + gArmTokenSpaceGuid.PcdGicInterruptInterfaceBase + gArmTokenSpaceGuid.PcdArmArchTimerSecIntrNum + gArmTokenSpaceGuid.PcdArmArchTimerIntrNum + gArmTokenSpaceGuid.PcdArmArchTimerVirtIntrNum + gArmTokenSpaceGuid.PcdArmArchTimerHypIntrNum + gArmPlatformTokenSpaceGuid.PcdPL031RtcBase + +[Protocols] + gEfiDevicePathProtocolGuid + +[Depex] + TRUE