From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id EB15881E4E for ; Sun, 27 Nov 2016 17:58:41 -0800 (PST) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga103.fm.intel.com with ESMTP; 27 Nov 2016 17:58:41 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,561,1473145200"; d="scan'208";a="1064962312" Received: from fmsmsx103.amr.corp.intel.com ([10.18.124.201]) by orsmga001.jf.intel.com with ESMTP; 27 Nov 2016 17:58:40 -0800 Received: from fmsmsx155.amr.corp.intel.com (10.18.116.71) by FMSMSX103.amr.corp.intel.com (10.18.124.201) with Microsoft SMTP Server (TLS) id 14.3.248.2; Sun, 27 Nov 2016 17:58:40 -0800 Received: from shsmsx102.ccr.corp.intel.com (10.239.4.154) by FMSMSX155.amr.corp.intel.com (10.18.116.71) with Microsoft SMTP Server (TLS) id 14.3.248.2; Sun, 27 Nov 2016 17:58:40 -0800 Received: from shsmsx104.ccr.corp.intel.com ([169.254.5.142]) by shsmsx102.ccr.corp.intel.com ([169.254.2.239]) with mapi id 14.03.0248.002; Mon, 28 Nov 2016 09:58:38 +0800 From: "Ni, Ruiyu" To: Ard Biesheuvel , "edk2-devel@lists.01.org" , "leif.lindholm@linaro.org" , "Gao, Liming" , "afish@apple.com" , "Kinney, Michael D" , "mw@semihalf.com" , "Tian, Feng" Thread-Topic: [PATCH v4 2/5] MdeModulePkg: introduce helper library to register non-discoverable devices Thread-Index: AQHSRzKSk3OfK7Q1vkOi3UKXSTembKDtpxNw Date: Mon, 28 Nov 2016 01:58:37 +0000 Deferred-Delivery: Mon, 28 Nov 2016 01:58:00 +0000 Message-ID: <734D49CCEBEEF84792F5B80ED585239D58E8D00F@SHSMSX104.ccr.corp.intel.com> References: <1480088538-21834-1-git-send-email-ard.biesheuvel@linaro.org> <1480088538-21834-3-git-send-email-ard.biesheuvel@linaro.org> In-Reply-To: <1480088538-21834-3-git-send-email-ard.biesheuvel@linaro.org> Accept-Language: en-US, zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Subject: Re: [PATCH v4 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: , X-List-Received-Date: Mon, 28 Nov 2016 01:58:42 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Ruiyu Ni Thanks/Ray > -----Original Message----- > From: Ard Biesheuvel [mailto:ard.biesheuvel@linaro.org] > Sent: Friday, November 25, 2016 11:42 PM > To: edk2-devel@lists.01.org; leif.lindholm@linaro.org; Gao, Liming > ; afish@apple.com; Ni, Ruiyu ; > Kinney, Michael D ; mw@semihalf.com; Tian, > Feng > Cc: Ard Biesheuvel > Subject: [PATCH v4 2/5] MdeModulePkg: introduce helper library to registe= r > non-discoverable devices >=20 > Non-discoverable devices need to be registered explicitly by the platform= . > Introduce a helper library that takes care of this. >=20 > 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. >=20 > Contributed-under: TianoCore Contribution Agreement 1.0 > Signed-off-by: Ard Biesheuvel > --- > MdeModulePkg/Include/Library/NonDiscoverableDeviceRegistrationLib.h > | 62 ++++++ >=20 > MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscove > rableDeviceRegistrationLib.c | 204 ++++++++++++++++++++ >=20 > MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscove > rableDeviceRegistrationLib.inf | 46 +++++ > MdeModulePkg/MdeModulePkg.dec = | 4 > + > MdeModulePkg/MdeModulePkg.dsc = | 2 > + > 5 files changed, 318 insertions(+) >=20 > 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 devic= e > + @param[in] DmaType Whether the device is DMA coherent > + @param[in] InitFunc Initialization routine to be invok= ed when > + the device is enabled > + @param[in,out] Handle The handle onto which to install t= he > + non-discoverable device protocol. > + If Handle is NULL or *Handle is NU= LL, a > + new handle will be allocated. > + @param[in] NumMmioResources The number of UINTN base/size > pairs that > + follow, each describing an MMIO re= gion > + 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/NonDisco > verableDeviceRegistrationLib.c > b/MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDisco > verableDeviceRegistrationLib.c > new file mode 100644 > index 000000000000..94cd946b69d3 > --- /dev/null > +++ > b/MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDisco > +++ verableDeviceRegistrationLib.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 devic= e > + @param[in] DmaType Whether the device is DMA coherent > + @param[in] InitFunc Initialization routine to be invok= ed when > + the device is enabled > + @param[in,out] Handle The handle onto which to install t= he > + non-discoverable device protocol. > + If Handle is NULL or *Handle is NU= LL, a > + new handle will be allocated. > + @param[in] NumMmioResources The number of UINTN base/size > pairs that > + follow, each describing an MMIO re= gion > + 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 >=3D NonDiscoverableDeviceTypeMax || > + DmaType >=3D NonDiscoverableDeviceDmaTypeMax || > + NumMmioResources =3D=3D 0) { > + return EFI_INVALID_PARAMETER; > + } > + > + if (Handle =3D=3D NULL) { > + Handle =3D &LocalHandle; > + LocalHandle =3D NULL; > + } > + > + AllocSize =3D sizeof *Device + > + NumMmioResources * sizeof > (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR) + > + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR); Device =3D > + (NON_DISCOVERABLE_DEVICE *)AllocateZeroPool (AllocSize); if (Device > + =3D=3D NULL) { > + return EFI_OUT_OF_RESOURCES; > + } > + > + Device->Type =3D GetGuidFromType (Type); ASSERT (Device->Type !=3D > + NULL); > + > + Device->DmaType =3D DmaType; > + Device->Initialize =3D InitFunc; > + Device->Resources =3D (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *)(Device > + > + 1); > + > + VA_START (Args, NumMmioResources); > + for (Index =3D 0; Index < NumMmioResources; Index++) { > + Desc =3D &Device->Resources [Index]; > + Base =3D VA_ARG (Args, UINTN); > + Size =3D VA_ARG (Args, UINTN); > + > + Desc->Desc =3D ACPI_ADDRESS_SPACE_DESCRIPTOR; > + Desc->Len =3D sizeof *Desc - 3; > + Desc->AddrRangeMin =3D Base; > + Desc->AddrLen =3D Size; > + Desc->AddrRangeMax =3D Base + Size - 1; > + Desc->ResType =3D ACPI_ADDRESS_SPACE_TYPE_MEM; > + Desc->AddrSpaceGranularity =3D (Base + Size > SIZE_4GB) ? 64 : 32; > + Desc->AddrTranslationOffset =3D 0; > + } > + VA_END (Args); > + > + End =3D (EFI_ACPI_END_TAG_DESCRIPTOR *)&Device->Resources > + [NumMmioResources]; > + > + End->Desc =3D ACPI_END_TAG_DESCRIPTOR; > + End->Checksum =3D 0; > + > + DevicePath =3D (NON_DISCOVERABLE_DEVICE_PATH *)CreateDeviceNode ( > + HARDWARE_DEVICE_PATH, > + HW_VENDOR_DP, > + sizeof (*DevicePath)); > + if (DevicePath =3D=3D NULL) { > + Status =3D 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 =3D Device->Resources > + [0].AddrRangeMin; DevicePath->ResourceType =3D Device->Resources > + [0].ResType; > + > + SetDevicePathNodeLength (&DevicePath->Vendor, > + sizeof (*DevicePath) - sizeof (DevicePath->End)); > + SetDevicePathEndNode (&DevicePath->End); > + > + Status =3D 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/NonDisco > verableDeviceRegistrationLib.inf > b/MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDisco > verableDeviceRegistrationLib.inf > new file mode 100644 > index 000000000000..ba32324c303e > --- /dev/null > +++ > b/MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDisco > +++ verableDeviceRegistrationLib.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 =3D 0x00010019 > + BASE_NAME =3D NonDiscoverableDeviceRegistrationLi= b > + FILE_GUID =3D 8802ae41-8184-49cb-8aec-62627cd7ceb= 4 > + MODULE_TYPE =3D DXE_DRIVER > + VERSION_STRING =3D 1.0 > + LIBRARY_CLASS =3D NonDiscoverableDeviceRegistrationLi= b > + > +[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 588f5e97dbc2..315337fa4000 > 100644 > --- a/MdeModulePkg/MdeModulePkg.dec > +++ b/MdeModulePkg/MdeModulePkg.dec > @@ -161,6 +161,10 @@ [LibraryClasses] > # > FmpAuthenticationLib|Include/Library/FmpAuthenticationLib.h >=20 > + ## @libraryclass Provides a service to register non-discoverable dev= ice > + ## > + > + > NonDiscoverableDeviceRegistrationLib|Include/Library/NonDiscoverableDe > + viceRegistrationLib.h > + > [Guids] > ## MdeModule package token space guid > # Include/Guid/MdeModulePkgTokenSpace.h > diff --git a/MdeModulePkg/MdeModulePkg.dsc > b/MdeModulePkg/MdeModulePkg.dsc index bfecb3078737..20e5e8c78be0 > 100644 > --- a/MdeModulePkg/MdeModulePkg.dsc > +++ b/MdeModulePkg/MdeModulePkg.dsc > @@ -102,6 +102,7 @@ [LibraryClasses] >=20 > AuthVariableLib|MdeModulePkg/Library/AuthVariableLibNull/AuthVariableL > ibNull.inf > VarCheckLib|MdeModulePkg/Library/VarCheckLib/VarCheckLib.inf > FileExplorerLib|MdeModulePkg/Library/FileExplorerLib/FileExplorerLib.i= nf > + > + > NonDiscoverableDeviceRegistrationLib|MdeModulePkg/Library/NonDiscove > ra > + bleDeviceRegistrationLib/NonDiscoverableDeviceRegistrationLib.inf >=20 >=20 > FmpAuthenticationLib|MdeModulePkg/Library/FmpAuthenticationLibNull/F > mpAuthenticationLibNull.inf >=20 > CapsuleLib|MdeModulePkg/Library/DxeCapsuleLibNull/DxeCapsuleLibNull.i > nf > @@ -322,6 +323,7 @@ [Components] > MdeModulePkg/Library/PeiIpmiLibIpmiPpi/PeiIpmiLibIpmiPpi.inf >=20 > MdeModulePkg/Library/SmmIpmiLibSmmIpmiProtocol/SmmIpmiLibSmmIp > miProtocol.inf > MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltLib.inf > + > + > MdeModulePkg/Library/NonDiscoverableDeviceRegistrationLib/NonDiscove > ra > + bleDeviceRegistrationLib.inf >=20 > MdeModulePkg/Universal/BdsDxe/BdsDxe.inf >=20 > MdeModulePkg/Application/BootManagerMenuApp/BootManagerMenuAp > p.inf > -- > 2.7.4