From patchwork Mon Dec 5 15:35:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 86577 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp1538057qgi; Mon, 5 Dec 2016 07:35:37 -0800 (PST) X-Received: by 10.99.47.7 with SMTP id v7mr104898162pgv.39.1480952137630; Mon, 05 Dec 2016 07:35:37 -0800 (PST) Return-Path: Received: from ml01.01.org (ml01.01.org. [2001:19d0:306:5::1]) by mx.google.com with ESMTPS id z66si15024044pfk.207.2016.12.05.07.35.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Dec 2016 07:35:37 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) client-ip=2001:19d0:306:5::1; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of edk2-devel-bounces@lists.01.org designates 2001:19d0:306:5::1 as permitted sender) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 5B94281FF5; Mon, 5 Dec 2016 07:35:37 -0800 (PST) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received: from mail-wj0-x232.google.com (mail-wj0-x232.google.com [IPv6:2a00:1450:400c:c01::232]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 492AD81FF5 for ; Mon, 5 Dec 2016 07:35:35 -0800 (PST) Received: by mail-wj0-x232.google.com with SMTP id v7so293679367wjy.2 for ; Mon, 05 Dec 2016 07:35:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=t0AjM7ewpAwaIJ0pUkQqrtkz72NbI6kScsEF9nClu7w=; b=fVUB/6tnLBV7ALNDKgacim5HeWHbVymtifVE5GjfmTyYKuykayNCEKSgNuW4Dv4PBj /DLsu+3MynZi575+SoLvAGaWKocmbghBZqfVBx4RNMIJY8ZNNVe1Zlb+xCSGqUb3D4oc qohRBf293B601wIQS9t90/wVI73fl2z1c2Vgs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=t0AjM7ewpAwaIJ0pUkQqrtkz72NbI6kScsEF9nClu7w=; b=mezGn0D9ma/L5MuLysQOrzXIYbzaBcwMvy+4yFQHeK5j0n9wSuBCh2VKfgFw/7xB2p 9tKaCCLRdd3HR7tyCPu8j2Gg0eIBtKA2+349EnbxaFViKjTAzbXSA3Ppt/ugfx/zTP2c YyAODfKk6p4ddzWvIz/pmqOWeY8hd1ouLSHiI+2XnJFOXyU+gz+bJyDPlt3Hnkp06Dbb 5kp6hOscbd7ksvkHJ1Lbub5KWmvd+LMSv5ckZhfi3E+fDXOv3V1C3knBYLLj2WZSZ+hs 3JuD2+zT5HnoWCfAO5m85wXXYFA96UH4m4DXhoZtryc/EbXX/f7uC+riLU5Pn1d1pcA1 yVHQ== X-Gm-Message-State: AKaTC02PXtWaVNwr96GSk9gkPfvx4pGGmtTbkJvh0UP3De8DKizwMy9UZEmFOhB8syxF0rMa X-Received: by 10.194.0.229 with SMTP id 5mr59654802wjh.55.1480952133482; Mon, 05 Dec 2016 07:35:33 -0800 (PST) Received: from localhost.localdomain ([105.144.52.243]) by smtp.gmail.com with ESMTPSA id xu5sm20875489wjc.49.2016.12.05.07.35.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 05 Dec 2016 07:35:32 -0800 (PST) From: Ard Biesheuvel To: edk2-devel@lists.01.org, leif.lindholm@linaro.org, ruiyu.ni@intel.com Date: Mon, 5 Dec 2016 15:35:19 +0000 Message-Id: <1480952122-27937-3-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1480952122-27937-1-git-send-email-ard.biesheuvel@linaro.org> References: <1480952122-27937-1-git-send-email-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH v5 2/5] MdeModulePkg: introduce helper library to register non-discoverable devices X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: feng.tian@intel.com, liming.gao@intel.com, Ard Biesheuvel MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" Non-discoverable devices need to be registered explicitly by the platform. Introduce a helper library that takes care of this. This implementation currently only supports registering devices that are covered by one or more MMIO resources. The underlying protocol allows for more flexibility than that, but this is currently sufficient for the use cases that we know about. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ard Biesheuvel Reviewed-by: Ruiyu Ni Tested-by: Marcin Wojtas --- MdeModulePkg/Include/Library/NonDiscoverableDeviceRegistrationLib.h | 62 ++++++ MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.c | 204 ++++++++++++++++++++ MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.inf | 46 +++++ MdeModulePkg/MdeModulePkg.dec | 4 + MdeModulePkg/MdeModulePkg.dsc | 2 + 5 files changed, 318 insertions(+) -- 2.7.4 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel diff --git a/MdeModulePkg/Include/Library/NonDiscoverableDeviceRegistrationLib.h b/MdeModulePkg/Include/Library/NonDiscoverableDeviceRegistrationLib.h new file mode 100644 index 000000000000..120d91466095 --- /dev/null +++ b/MdeModulePkg/Include/Library/NonDiscoverableDeviceRegistrationLib.h @@ -0,0 +1,62 @@ +/** @file + Copyright (c) 2016, 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. + +**/ + +#ifndef __NON_DISCOVERABLE_DEVICE_REGISTRATION_LIB_H__ +#define __NON_DISCOVERABLE_DEVICE_REGISTRATION_LIB_H__ + +#include + +typedef enum { + NonDiscoverableDeviceTypeAhci, + NonDiscoverableDeviceTypeAmba, + NonDiscoverableDeviceTypeEhci, + NonDiscoverableDeviceTypeNvme, + NonDiscoverableDeviceTypeOhci, + NonDiscoverableDeviceTypeSdhci, + NonDiscoverableDeviceTypeUfs, + NonDiscoverableDeviceTypeUhci, + NonDiscoverableDeviceTypeXhci, + NonDiscoverableDeviceTypeMax, +} NON_DISCOVERABLE_DEVICE_TYPE; + +/** + Register a non-discoverable MMIO device + + @param[in] DeviceType The type of non-discoverable device + @param[in] DmaType Whether the device is DMA coherent + @param[in] InitFunc Initialization routine to be invoked when + the device is enabled + @param[in,out] Handle The handle onto which to install the + non-discoverable device protocol. + If Handle is NULL or *Handle is NULL, a + new handle will be allocated. + @param[in] NumMmioResources The number of UINTN base/size pairs that + follow, each describing an MMIO region + owned by the device + + @retval EFI_SUCCESS The registration succeeded. + @retval Other The registration failed. + +**/ +EFI_STATUS +EFIAPI +RegisterNonDiscoverableMmioDevice ( + IN NON_DISCOVERABLE_DEVICE_TYPE Type, + IN NON_DISCOVERABLE_DEVICE_DMA_TYPE DmaType, + IN NON_DISCOVERABLE_DEVICE_INIT InitFunc, + IN OUT EFI_HANDLE *Handle OPTIONAL, + IN UINTN NumMmioResources, + ... + ); + +#endif diff --git a/MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.c b/MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.c new file mode 100644 index 000000000000..94cd946b69d3 --- /dev/null +++ b/MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.c @@ -0,0 +1,204 @@ +/** @file + Copyright (c) 2016, 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 + +STATIC +CONST EFI_GUID * +GetGuidFromType ( + IN NON_DISCOVERABLE_DEVICE_TYPE Type + ) +{ + switch (Type) { + case NonDiscoverableDeviceTypeAhci: + return &gEdkiiNonDiscoverableAhciDeviceGuid; + + case NonDiscoverableDeviceTypeAmba: + return &gEdkiiNonDiscoverableAmbaDeviceGuid; + + case NonDiscoverableDeviceTypeEhci: + return &gEdkiiNonDiscoverableEhciDeviceGuid; + + case NonDiscoverableDeviceTypeNvme: + return &gEdkiiNonDiscoverableNvmeDeviceGuid; + + case NonDiscoverableDeviceTypeOhci: + return &gEdkiiNonDiscoverableOhciDeviceGuid; + + case NonDiscoverableDeviceTypeSdhci: + return &gEdkiiNonDiscoverableSdhciDeviceGuid; + + case NonDiscoverableDeviceTypeUfs: + return &gEdkiiNonDiscoverableUfsDeviceGuid; + + case NonDiscoverableDeviceTypeUhci: + return &gEdkiiNonDiscoverableUhciDeviceGuid; + + case NonDiscoverableDeviceTypeXhci: + return &gEdkiiNonDiscoverableXhciDeviceGuid; + + default: + return NULL; + } +} + +#pragma pack (1) +typedef struct { + VENDOR_DEVICE_PATH Vendor; + UINT64 BaseAddress; + UINT8 ResourceType; + EFI_DEVICE_PATH_PROTOCOL End; +} NON_DISCOVERABLE_DEVICE_PATH; +#pragma pack () + +/** + Register a non-discoverable MMIO device + + @param[in] DeviceType The type of non-discoverable device + @param[in] DmaType Whether the device is DMA coherent + @param[in] InitFunc Initialization routine to be invoked when + the device is enabled + @param[in,out] Handle The handle onto which to install the + non-discoverable device protocol. + If Handle is NULL or *Handle is NULL, a + new handle will be allocated. + @param[in] NumMmioResources The number of UINTN base/size pairs that + follow, each describing an MMIO region + owned by the device + + @retval EFI_SUCCESS The registration succeeded. + @retval EFI_INVALID_PARAMETER An invalid argument was given + @retval Other The registration failed. + +**/ +EFI_STATUS +EFIAPI +RegisterNonDiscoverableMmioDevice ( + IN NON_DISCOVERABLE_DEVICE_TYPE Type, + IN NON_DISCOVERABLE_DEVICE_DMA_TYPE DmaType, + IN NON_DISCOVERABLE_DEVICE_INIT InitFunc, + IN OUT EFI_HANDLE *Handle OPTIONAL, + IN UINTN NumMmioResources, + ... + ) +{ + NON_DISCOVERABLE_DEVICE *Device; + NON_DISCOVERABLE_DEVICE_PATH *DevicePath; + EFI_HANDLE LocalHandle; + EFI_STATUS Status; + UINTN AllocSize; + UINTN Index; + VA_LIST Args; + EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Desc; + EFI_ACPI_END_TAG_DESCRIPTOR *End; + UINTN Base, Size; + + if (Type >= NonDiscoverableDeviceTypeMax || + DmaType >= NonDiscoverableDeviceDmaTypeMax || + NumMmioResources == 0) { + return EFI_INVALID_PARAMETER; + } + + if (Handle == NULL) { + Handle = &LocalHandle; + LocalHandle = NULL; + } + + AllocSize = sizeof *Device + + NumMmioResources * sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) + + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR); + Device = (NON_DISCOVERABLE_DEVICE *)AllocateZeroPool (AllocSize); + if (Device == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Device->Type = GetGuidFromType (Type); + ASSERT (Device->Type != NULL); + + Device->DmaType = DmaType; + Device->Initialize = InitFunc; + Device->Resources = (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *)(Device + 1); + + VA_START (Args, NumMmioResources); + for (Index = 0; Index < NumMmioResources; Index++) { + Desc = &Device->Resources [Index]; + Base = VA_ARG (Args, UINTN); + Size = VA_ARG (Args, UINTN); + + Desc->Desc = ACPI_ADDRESS_SPACE_DESCRIPTOR; + Desc->Len = sizeof *Desc - 3; + Desc->AddrRangeMin = Base; + Desc->AddrLen = Size; + Desc->AddrRangeMax = Base + Size - 1; + Desc->ResType = ACPI_ADDRESS_SPACE_TYPE_MEM; + Desc->AddrSpaceGranularity = (Base + Size > SIZE_4GB) ? 64 : 32; + Desc->AddrTranslationOffset = 0; + } + VA_END (Args); + + End = (EFI_ACPI_END_TAG_DESCRIPTOR *)&Device->Resources [NumMmioResources]; + + End->Desc = ACPI_END_TAG_DESCRIPTOR; + End->Checksum = 0; + + DevicePath = (NON_DISCOVERABLE_DEVICE_PATH *)CreateDeviceNode ( + HARDWARE_DEVICE_PATH, + HW_VENDOR_DP, + sizeof (*DevicePath)); + if (DevicePath == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto FreeDevice; + } + + CopyGuid (&DevicePath->Vendor.Guid, &gEdkiiNonDiscoverableDeviceProtocolGuid); + + // + // Use the base address and type of the first region to + // make the device path unique + // + DevicePath->BaseAddress = Device->Resources [0].AddrRangeMin; + DevicePath->ResourceType = Device->Resources [0].ResType; + + SetDevicePathNodeLength (&DevicePath->Vendor, + sizeof (*DevicePath) - sizeof (DevicePath->End)); + SetDevicePathEndNode (&DevicePath->End); + + Status = gBS->InstallMultipleProtocolInterfaces (Handle, + &gEdkiiNonDiscoverableDeviceProtocolGuid, Device, + &gEfiDevicePathProtocolGuid, DevicePath, + NULL); + if (EFI_ERROR (Status)) { + goto FreeDevicePath; + } + return EFI_SUCCESS; + +FreeDevicePath: + FreePool (DevicePath); + +FreeDevice: + FreePool (Device); + + return Status; +} diff --git a/MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.inf b/MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.inf new file mode 100644 index 000000000000..ba32324c303e --- /dev/null +++ b/MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.inf @@ -0,0 +1,46 @@ +# @file +# Copyright (c) 2016, 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 = 0x00010019 + BASE_NAME = NonDiscoverableDeviceRegistrationLib + FILE_GUID = 8802ae41-8184-49cb-8aec-62627cd7ceb4 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + LIBRARY_CLASS = NonDiscoverableDeviceRegistrationLib + +[Sources] + NonDiscoverableDeviceRegistrationLib.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + +[LibraryClasses] + BaseMemoryLib + DebugLib + DevicePathLib + UefiBootServicesTableLib + +[Protocols] + gEdkiiNonDiscoverableDeviceProtocolGuid + +[Guids] + gEdkiiNonDiscoverableAhciDeviceGuid + gEdkiiNonDiscoverableAmbaDeviceGuid + gEdkiiNonDiscoverableEhciDeviceGuid + gEdkiiNonDiscoverableNvmeDeviceGuid + gEdkiiNonDiscoverableOhciDeviceGuid + gEdkiiNonDiscoverableSdhciDeviceGuid + gEdkiiNonDiscoverableUfsDeviceGuid + gEdkiiNonDiscoverableUhciDeviceGuid + gEdkiiNonDiscoverableXhciDeviceGuid diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec index abd5da5daa0c..a5eb901c4de8 100644 --- a/MdeModulePkg/MdeModulePkg.dec +++ b/MdeModulePkg/MdeModulePkg.dec @@ -161,6 +161,10 @@ [LibraryClasses] # FmpAuthenticationLib|Include/Library/FmpAuthenticationLib.h + ## @libraryclass Provides a service to register non-discoverable device + ## + NonDiscoverableDeviceRegistrationLib|Include/Library/NonDiscoverableDeviceRegistrationLib.h + [Guids] ## MdeModule package token space guid # Include/Guid/MdeModulePkgTokenSpace.h diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc index d7db274cac7a..ada76594c580 100644 --- a/MdeModulePkg/MdeModulePkg.dsc +++ b/MdeModulePkg/MdeModulePkg.dsc @@ -102,6 +102,7 @@ [LibraryClasses] AuthVariableLib|MdeModulePkg/Library/AuthVariableLibNull/AuthVariableLibNull.inf VarCheckLib|MdeModulePkg/Library/VarCheckLib/VarCheckLib.inf FileExplorerLib|MdeModulePkg/Library/FileExplorerLib/FileExplorerLib.inf + NonDiscoverableDeviceRegistrationLib|MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.inf FmpAuthenticationLib|MdeModulePkg/Library/FmpAuthenticationLibNull/FmpAuthenticationLibNull.inf CapsuleLib|MdeModulePkg/Library/DxeCapsuleLibNull/DxeCapsuleLibNull.inf @@ -322,6 +323,7 @@ [Components] MdeModulePkg/Library/PeiIpmiLibIpmiPpi/PeiIpmiLibIpmiPpi.inf MdeModulePkg/Library/SmmIpmiLibSmmIpmiProtocol/SmmIpmiLibSmmIpmiProtocol.inf MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltLib.inf + MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.inf MdeModulePkg/Universal/BdsDxe/BdsDxe.inf MdeModulePkg/Application/BootManagerMenuApp/BootManagerMenuApp.inf