From patchwork Wed Nov 16 16:59:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 82568 Delivered-To: patch@linaro.org Received: by 10.182.1.168 with SMTP id 8csp303389obn; Wed, 16 Nov 2016 08:59:48 -0800 (PST) X-Received: by 10.98.166.70 with SMTP id t67mr5896079pfe.132.1479315588247; Wed, 16 Nov 2016 08:59:48 -0800 (PST) Return-Path: Received: from ml01.01.org (ml01.01.org. [2001:19d0:306:5::1]) by mx.google.com with ESMTPS id c22si32627185pfk.219.2016.11.16.08.59.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Nov 2016 08:59:48 -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 C05E781EF6; Wed, 16 Nov 2016 08:59:42 -0800 (PST) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received: from mail-wm0-x236.google.com (mail-wm0-x236.google.com [IPv6:2a00:1450:400c:c09::236]) (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 1A04981EF0 for ; Wed, 16 Nov 2016 08:59:40 -0800 (PST) Received: by mail-wm0-x236.google.com with SMTP id t79so85572502wmt.0 for ; Wed, 16 Nov 2016 08:59:45 -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=lJLiPqXGssCKFoyZs5Ovb2xkhVhgijw0OUbPGb9Wm+0=; b=kPSy5dgxHWvIV2/w/qMSjoELCLdeEze4L/crsi/O4yEQ7E5ShD6b+Nm+BQsuBlZcuh aGwZjpDz9WOV2CcPv0EJKRg2g2KGzhN7JdFsf8M23xARULtojCnlSzVpxHLbN9AiaONM melRl8iVdfUgN3XMA7xOFtVVWjiuZlBToMO+I= 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=lJLiPqXGssCKFoyZs5Ovb2xkhVhgijw0OUbPGb9Wm+0=; b=RCdNhy8+0mi9IvCzLg3HwUj9Ueu9p2b8ZIhwvWxV6+KWqs9hNbR/6JabFgWUvZ1ODk iBU1HybM7fl64IEZor+HMU7hLv+M0p+/cpjEqzEQpV2afksYpwoemtJrCvl8zvQ50Csd txzSVTmETEYpW+Bp8zszu2XBH+MWUkZPVOaMm30+jnq6tVBH2miplALLlKztLGDgEbvP jmsmgFxHBNxszrjNdu4JdDuoP5uoBr60g3IxNRDb/YkwaYF4y5xMIzfncSvh0KQdOPua hr10+Y/atDYjRq3AidJnInBKCrq5Rp5lO1XNCyb5m08JaZsAeZRiHVBHiXvvkX1Lio53 W+lg== X-Gm-Message-State: ABUngvdvndMOAn5CR8BuCLjZfNt43ht5gAzg0E0gMI1/IMtnUFxLzibfBOUXEVuaWDmsjUY5 X-Received: by 10.28.211.72 with SMTP id k69mr10683415wmg.138.1479315583609; Wed, 16 Nov 2016 08:59:43 -0800 (PST) Received: from localhost.localdomain ([105.137.104.147]) by smtp.gmail.com with ESMTPSA id gk6sm41485516wjc.21.2016.11.16.08.59.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 16 Nov 2016 08:59:43 -0800 (PST) From: Ard Biesheuvel To: edk2-devel@lists.01.org, liming.gao@intel.com, ruiyu.ni@intel.com, michael.d.kinney@intel.com Date: Wed, 16 Nov 2016 16:59:28 +0000 Message-Id: <1479315571-14953-3-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1479315571-14953-1-git-send-email-ard.biesheuvel@linaro.org> References: <1479315571-14953-1-git-send-email-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH v3 2/5] MdeModule: 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: afish@apple.com, leif.lindholm@linaro.org, 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 --- MdeModulePkg/Include/Library/NonDiscoverableDeviceRegistrationLib.h | 49 ++++++ MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.c | 161 ++++++++++++++++++++ MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.inf | 34 +++++ MdeModulePkg/MdeModulePkg.dec | 4 + MdeModulePkg/MdeModulePkg.dsc | 2 + 5 files changed, 250 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..d5535e2e435c --- /dev/null +++ b/MdeModulePkg/Include/Library/NonDiscoverableDeviceRegistrationLib.h @@ -0,0 +1,49 @@ +/** @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 + +/** + 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..524b6794e18f --- /dev/null +++ b/MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.c @@ -0,0 +1,161 @@ +/** @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 + +#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 = Type; + 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..fc252c4b183c --- /dev/null +++ b/MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.inf @@ -0,0 +1,34 @@ +# @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] + DebugLib + DevicePathLib + UefiBootServicesTableLib + +[Protocols] + gEdkiiNonDiscoverableDeviceProtocolGuid diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec index 6b956fc80c93..930debea695b 100644 --- a/MdeModulePkg/MdeModulePkg.dec +++ b/MdeModulePkg/MdeModulePkg.dec @@ -157,6 +157,10 @@ [LibraryClasses] ## FrameBufferBltLib|Include/Library/FrameBufferBltLib.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 757d52dbede4..43421d610ede 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 [LibraryClasses.EBC.PEIM] IoLib|MdePkg/Library/PeiIoLibCpuIo/PeiIoLibCpuIo.inf @@ -317,6 +318,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