From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM03-BY2-obe.outbound.protection.outlook.com (mail-by2nam03on0620.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe4a::620]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 41E098178B for ; Mon, 9 Jan 2017 21:51:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=7T0fVh07gLJMX0l2pAaHg9h8AmpCaLlfQgCV2RL1rFA=; b=nAcr8PmJ1fyCYrvx5vtbNNHlH1Rp8NaaDBLFKOKTKQu8FI6KM+5R0wjT4VtvMZabTprEjTagYT+t3wBpO9sihMNIK60HoBkzm+h931t4f3Pr3IllVT9N1IkCpGiwCqcVXWAa1W84UnJEVRvMlyBnL1jK34f8Budjitz/La5z+JY= Received: from DM5PR12MB1243.namprd12.prod.outlook.com (10.168.237.22) by BY2PR12MB0662.namprd12.prod.outlook.com (10.163.113.151) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.829.7; Tue, 10 Jan 2017 05:51:41 +0000 Received: from DM5PR12MB1243.namprd12.prod.outlook.com ([10.168.237.22]) by DM5PR12MB1243.namprd12.prod.outlook.com ([10.168.237.22]) with mapi id 15.01.0829.013; Tue, 10 Jan 2017 05:51:40 +0000 From: "Duran, Leo" To: "afish@apple.com" CC: edk2-devel , "Tian, Feng" , "Singh, Brijesh" , Ard Biesheuvel , Jordan Justen , "Agyeman, Prince" , "lersek@redhat.com" , "star.zeng@intel.com" Thread-Topic: [edk2] [RFC 1/6] MdeModulePkg: Add DxeBmDmaLib library Thread-Index: AQHSatbn2YW32IQR4U+g68PPmMGVcaEw4HmAgABUV2A= Date: Tue, 10 Jan 2017 05:51:40 +0000 Message-ID: References: <1484007421-15462-1-git-send-email-leo.duran@amd.com> <1484007421-15462-2-git-send-email-leo.duran@amd.com> <24BD7DF0-C741-4DE7-965E-46451968F574@apple.com> In-Reply-To: <24BD7DF0-C741-4DE7-965E-46451968F574@apple.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: spf=none (sender IP is ) smtp.mailfrom=leo.duran@amd.com; x-originating-ip: [2605:6000:e7c4:2000:349a:4d6c:af1b:6d9f] x-microsoft-exchange-diagnostics: 1; BY2PR12MB0662; 7:32a7O19SNpkgC2SsOdyis/2qb2ObkhgejMwTxHB+Mztke8T43fjZKI1fbX+xr/NyMp+9ia01w/Wkwc8eNZ2j0mCSdWM2X9XESYfKHGzUUjguvntLgNCmN7Y89QcQrJV41F8ghoiMloLWU+SQLQAFlLyZ3tsid0fRo70NweddDqFCoW5MJpJMbJBexCNq4Y0z6rNttsqZ8bRqJhH0xI4NeMaDt+MgOVf6No7EUF+an4CLp2b5XKnCWbVexnIBEO6/iWBf01tg3UtoFrcXXLy0bgmOItpMyJY42SuBoDWwlkEzZA84/Xi5t4Nme8KrOG1PrCWZjiqY1N2TyPQA72dGsAB/W6Rc8thEsxQcENQARV9FCm7NiEqvI+EeRlGXTyIY8sWAoVL13ZTBfQpOGzgc/CrVHZ9Qzy+asDQYBtgNaL6DuIgnCnoKSd2S13jIagLB+Hj5jnm2aO5jTO9vLRfLPA==; 20:pAtWRa1S5/eZK18qZo1vl5M2N3QoNe+alZVUqZBdRmp4YWBNSxi9TjpOluA4s/gEp8ZJczsilVNK5fF31jNzI89oAPd4deM9SsEwXmsHE68II0MpRKcH7upkVY8Vc4jE72nkYgPaOkwIa9RJpa8GV0kdEk8857QSrFtypKeeCu/xJbLgxC5DiDL4Q9vjSS9hwqaxl+uTD58GpcZDz/539pxCtT8EBcaQMUuaEyad1/0nqrbw/9xDWJsKOwsMiDK2 x-forefront-antispam-report: SFV:SKI; SCL:-1SFV:NSPM; SFS:(10009020)(6009001)(6029001)(7916002)(39850400002)(39410400002)(39860400002)(39840400002)(39450400003)(13464003)(189002)(24454002)(377454003)(199003)(68736007)(77096006)(6436002)(50986999)(5660300001)(6506006)(99286003)(2900100001)(38730400001)(97736004)(55016002)(110136003)(101416001)(54906002)(5640700003)(25786008)(76176999)(105586002)(7696004)(106116001)(4326007)(54356999)(2501003)(229853002)(102836003)(6116002)(106356001)(2906002)(2950100002)(6916009)(33656002)(8666007)(305945005)(189998001)(1730700003)(92566002)(7736002)(15188155005)(81156014)(8676002)(81166006)(2351001)(16799955002)(3660700001)(575784001)(8936002)(86362001)(3280700002)(31430400001)(15395725005)(74316002)(9686003)(6306002)(122556002)(559001)(579004); DIR:OUT; SFP:1101; SCL:1; SRVR:BY2PR12MB0662; H:DM5PR12MB1243.namprd12.prod.outlook.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; x-ms-office365-filtering-correlation-id: f96af26f-11c3-4152-5187-08d4391cbfc9 x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001);SRVR:BY2PR12MB0662; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(767451399110)(162533806227266)(31960201722614)(228905959029699); x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(6040375)(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046)(6055026)(6041248)(20161123562025)(20161123555025)(20161123564025)(20161123560025)(6072148); SRVR:BY2PR12MB0662; BCL:0; PCL:0; RULEID:; SRVR:BY2PR12MB0662; x-forefront-prvs: 01834E39B7 received-spf: None (protection.outlook.com: amd.com does not designate permitted sender hosts) spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-originalarrivaltime: 10 Jan 2017 05:51:40.4080 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY2PR12MB0662 Subject: Re: [RFC 1/6] MdeModulePkg: Add DxeBmDmaLib library 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: Tue, 10 Jan 2017 05:51:46 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable > -----Original Message----- > From: afish@apple.com [mailto:afish@apple.com] > Sent: Monday, January 09, 2017 6:47 PM > To: Duran, Leo > Cc: edk2-devel ; Tian, Feng > ; Singh, Brijesh ; Ard > Biesheuvel ; Jordan Justen > ; Agyeman, Prince > ; lersek@redhat.com; star.zeng@intel.com > Subject: Re: [edk2] [RFC 1/6] MdeModulePkg: Add DxeBmDmaLib library >=20 >=20 > > On Jan 9, 2017, at 4:16 PM, Leo Duran wrote: > > > > This patch provides an abstraction layer for DMA operations > > implemented by the PciHostBridgeDxe driver. The intent is to then > > allow override of this library as may be required by specific hardware > > implementations, such as AMD's Secure Encrypted Virtualization (SEV). > > > > This new (BmDmaLib class) library is modeled after the existing DmaLib > > plus an extra DmaAbove4GB (BOOLEAN) parameter in the Map and > Allocate > > interfaces, so that decisions can be made about the need to allocate > > DMA buffers below the 4GB boundary. > > [Duran, Leo]=20 Hi Andrew, Yes, I believe a version of this library class could be used for IOMMU mapp= ings. The DMA API in the Linux kernel does exactly that. Regards, Leo. >=20 > Leo, >=20 > Sorry to jump in late, and maybe this is a phase 2 kind of thing, but sho= uld we > support a generic IOMMU architecture via this library? >=20 > I'm not really familiar with AMD's SEV, but in general an IO MMU can rest= rict > a DMA buffer to a specific PCI device, and block other PCI devices from > having access to that buffer. That would seem to imply it would be good t= o > have the option of passing the PCI device via these library APIs or I gue= ss we > could add an Ex version of the APIs that include the PCI device? You coul= d > always implement the Ex versions of the API by ignoring the PCI device (i= f > you don't care about per device IOMMU pertection) and calling the non-Ex > version. >=20 > To be honest it is kind of hard to fully implement an IOMMU with the curr= ent > UEFI spec as is, but I thought it be worth asking the question? >=20 > Thanks, >=20 > Andrew Fish >=20 >=20 > > Cc: Feng Tian > > Cc: Star Zeng > > Contributed-under: TianoCore Contribution Agreement 1.0 > > Signed-off-by: Leo Duran > > --- > > MdeModulePkg/Include/Library/BmDmaLib.h | 161 +++++++++++ > > MdeModulePkg/Library/DxeBmDmaLib/DxeBmDmaLib.c | 351 > +++++++++++++++++++++++ > > MdeModulePkg/Library/DxeBmDmaLib/DxeBmDmaLib.inf | 41 +++ > > MdeModulePkg/MdeModulePkg.dsc | 1 + > > 4 files changed, 554 insertions(+) > > create mode 100644 MdeModulePkg/Include/Library/BmDmaLib.h > > create mode 100644 > MdeModulePkg/Library/DxeBmDmaLib/DxeBmDmaLib.c > > create mode 100644 > MdeModulePkg/Library/DxeBmDmaLib/DxeBmDmaLib.inf > > > > diff --git a/MdeModulePkg/Include/Library/BmDmaLib.h > > b/MdeModulePkg/Include/Library/BmDmaLib.h > > new file mode 100644 > > index 0000000..2c0e50f > > --- /dev/null > > +++ b/MdeModulePkg/Include/Library/BmDmaLib.h > > @@ -0,0 +1,161 @@ > > +/** @file > > + DMA abstraction library APIs. Based on PCI IO protocol DMA abstracti= ons. > > + > > + Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.
> > + Copyright (c) 2017, AMD Inc. All rights reserved.
> > + > > + DMA Bus Master Read Operation: > > + Call BmDmaMap() for DmaOperationBusMasterRead. > > + Program the DMA Bus Master with the DeviceAddress returned by > BmDmaMap(). > > + Start the DMA Bus Master. > > + Wait for DMA Bus Master to complete the read operation. > > + Call BmDmaUnmap(). > > + > > + DMA Bus Master Write Operation: > > + Call BmDmaMap() for DmaOperationBusMasterWrite. > > + Program the DMA Bus Master with the DeviceAddress returned by > BmDmaMap(). > > + Start the DMA Bus Master. > > + Wait for DMA Bus Master to complete the write operation. > > + Call BmDmaUnmap(). > > + > > + DMA Bus Master Common Buffer Operation: > > + Call BmDmaAllocateBuffer() to allocate a common buffer. > > + Call BmDmaMap() for DmaOperationBusMasterCommonBuffer. > > + Program the DMA Bus Master with the DeviceAddress returned by > BmDmaMap(). > > + The common buffer can now be accessed equally by the processor and > the DMA bus master. > > + Call BmDmaUnmap(). > > + Call BmDmaFreeBuffer(). > > + > > + 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. > > + > > + Derived from: > > + EmbeddedPkg/Include/Library/DmaLib.h > > + > > +**/ > > + > > +#ifndef __BM_DMA_LIB_H__ > > +#define __BM_DMA_LIB_H__ > > + > > + > > +typedef enum { > > + /// > > + /// A read operation from system memory by a bus master. > > + /// > > + DmaOperationBusMasterRead, > > + /// > > + /// A write operation from system memory by a bus master. > > + /// > > + DmaOperationBusMasterWrite, > > + /// > > + /// Provides both read and write access to system memory by both > > +the processor and a > > + /// bus master. The buffer is coherent from both the processor's and= the > bus master's point of view. > > + /// > > + DmaOperationBusMasterCommonBuffer, > > + DmaOperationBusMasterMaximum > > +} BM_DMA_OPERATION; > > + > > + > > +/** > > + Provides the DMA controller-specific addresses needed to access syst= em > memory. > > + > > + Operation is relative to the DMA bus master. > > + > > + @param DmaAbove4GB Indicates capability of DMA operations > above 4GB. > > + @param Operation Indicates if the bus master is going t= o read or > write to system memory. > > + @param HostAddress The system memory address to map to th= e > DMA controller. > > + @param NumberOfBytes On input the number of bytes to map. O= n > output the number of bytes > > + that were mapped. > > + @param DeviceAddress The resulting map address for the bus > master controller to use to > > + access the hosts HostAddress. > > + @param Mapping A resulting value to pass to BmDmaUnma= p(). > > + > > + @retval EFI_SUCCESS The range was mapped for the returned > NumberOfBytes. > > + @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a > common buffer. > > + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. > > + @retval EFI_OUT_OF_RESOURCES The request could not be completed > due to a lack of resources. > > + @retval EFI_DEVICE_ERROR The system hardware could not map the > requested address. > > + > > +**/ > > +EFI_STATUS > > +EFIAPI > > +BmDmaMap ( > > + IN BOOLEAN DmaAbove4GB, > > + IN BM_DMA_OPERATION Operation, > > + IN VOID *HostAddress, > > + IN OUT UINTN *NumberOfBytes, > > + OUT PHYSICAL_ADDRESS *DeviceAddress, > > + OUT VOID **Mapping > > + ); > > + > > + > > +/** > > + Completes the DmaOperationBusMasterRead/Write/CommonBuffer > > +operation > > + and releases any corresponding resources. > > + > > + @param Mapping The mapping value returned from > BmDmaMap(). > > + > > + @retval EFI_SUCCESS The range was unmapped. > > + @retval EFI_DEVICE_ERROR The data was not committed to the targ= et > system memory. > > + > > +**/ > > +EFI_STATUS > > +EFIAPI > > +BmDmaUnmap ( > > + IN VOID *Mapping > > + ); > > + > > + > > +/** > > + Allocates pages that are suitable for a BmDmaMap() of type > DmaOperationBusMasterCommonBuffer. > > + > > + @param DmaAbove4GB Indicates capability of DMA operations > above 4GB. > > + @param MemoryType The type of memory to allocate: > EfiBootServicesData or > > + EfiRuntimeServicesData. > > + @param Pages The number of pages to allocate. > > + @param HostAddress A pointer to store the base system mem= ory > address of the > > + allocated range. > > + > > + @retval EFI_SUCCESS The requested memory pages were alloca= ted. > > + @retval EFI_UNSUPPORTED Attributes is unsupported. The only le= gal > attribute bits are > > + MEMORY_WRITE_COMBINE and MEMORY_CACHED= . > > + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. > > + @retval EFI_OUT_OF_RESOURCES The memory pages could not be > allocated. > > + > > +**/ > > +EFI_STATUS > > +EFIAPI > > +BmDmaAllocateBuffer ( > > + IN BOOLEAN DmaAbove4GB, > > + IN EFI_MEMORY_TYPE MemoryType, > > + IN UINTN Pages, > > + OUT VOID **HostAddress > > + ); > > + > > + > > +/** > > + Frees memory that was allocated with BmDmaAllocateBuffer(). > > + > > + @param Pages The number of pages to free. > > + @param HostAddress The base system memory address of the > allocated range. > > + > > + @retval EFI_SUCCESS The requested memory pages were freed. > > + @retval EFI_INVALID_PARAMETER The memory range specified by > HostAddress and Pages > > + was not allocated with BmDmaAllocateBu= ffer(). > > + > > +**/ > > +EFI_STATUS > > +EFIAPI > > +BmDmaFreeBuffer ( > > + IN UINTN Pages, > > + IN VOID *HostAddress > > + ); > > + > > + > > +#endif > > + > > diff --git a/MdeModulePkg/Library/DxeBmDmaLib/DxeBmDmaLib.c > > b/MdeModulePkg/Library/DxeBmDmaLib/DxeBmDmaLib.c > > new file mode 100644 > > index 0000000..824b51f > > --- /dev/null > > +++ b/MdeModulePkg/Library/DxeBmDmaLib/DxeBmDmaLib.c > > @@ -0,0 +1,351 @@ > > +/** @file > > + DMA abstraction library APIs. Based on PCI IO protocol DMA abstracti= ons. > > + > > + Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.
> > + Copyright (c) 2017, AMD Inc. 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. > > + > > + Derived from: > > + MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciRootBridgeIo.c > > + > > +**/ > > + > > +#include > > +#include > > +#include > > +#include > > +#include #include > > + #include > > + > > +#include > > + > > + > > +#define FORCE_BELOW_4GB_TRUE TRUE > > +#define FORCE_BELOW_4GB_FALSE FALSE > > +#define NO_MAPPING (VOID *) (UINTN) -1 > > + > > + > > +typedef struct { > > + BM_DMA_OPERATION Operation; > > + UINTN NumberOfBytes; > > + UINTN NumberOfPages; > > + EFI_PHYSICAL_ADDRESS HostAddress; > > + EFI_PHYSICAL_ADDRESS MappedHostAddress; } MAP_INFO; > > + > > + > > +EFI_STATUS > > +AllocateBounceBuffer ( > > + IN BOOLEAN ForceBelow4GB, > > + IN BM_DMA_OPERATION Operation, > > + IN EFI_PHYSICAL_ADDRESS HostAddress, > > + IN OUT UINTN *NumberOfBytes, > > + OUT PHYSICAL_ADDRESS *DeviceAddress, > > + OUT VOID **Mapping > > + ) > > +{ > > + EFI_STATUS Status; > > + MAP_INFO *MapInfo; > > + EFI_ALLOCATE_TYPE AllocateType; > > + > > + // > > + // Allocate a MAP_INFO structure to remember the mapping when > > + Unmap() is // called later. > > + // > > + MapInfo =3D AllocatePool (sizeof (MAP_INFO)); if (MapInfo =3D=3D NU= LL) { > > + *NumberOfBytes =3D 0; > > + return EFI_OUT_OF_RESOURCES; > > + } > > + > > + // > > + // Initialize the MAP_INFO structure // > > + MapInfo->Operation =3D Operation; > > + MapInfo->NumberOfBytes =3D *NumberOfBytes; MapInfo- > >NumberOfPages =3D > > + EFI_SIZE_TO_PAGES (MapInfo->NumberOfBytes); > > + MapInfo->HostAddress =3D HostAddress; > > + > > + if (ForceBelow4GB) { > > + // > > + // Limit allocations to memory below 4GB > > + // > > + AllocateType =3D AllocateMaxAddress; > > + MapInfo->MappedHostAddress =3D SIZE_4GB - 1; } else { > > + AllocateType =3D AllocateAnyPages; > > + } > > + > > + // > > + // Allocate DMA bounce buffer > > + // > > + Status =3D gBS->AllocatePages ( > > + AllocateType, > > + EfiBootServicesData, > > + MapInfo->NumberOfPages, > > + &MapInfo->MappedHostAddress > > + ); > > + > > + if (EFI_ERROR (Status)) { > > + FreePool (MapInfo); > > + *NumberOfBytes =3D 0; > > + return Status; > > + } > > + > > + // > > + // If this is a read operation from the Bus Master's point of view, > > + // then copy the contents of the real buffer into the mapped buffer > > + // so the Bus Master can read the contents of the real buffer. > > + // > > + if (Operation =3D=3D DmaOperationBusMasterRead) { > > + CopyMem ( > > + (VOID *) (UINTN) MapInfo->MappedHostAddress, > > + (VOID *) (UINTN) MapInfo->HostAddress, > > + MapInfo->NumberOfBytes > > + ); > > + } > > + > > + // > > + // The DeviceAddress is the address of the mapped buffer // > > + *DeviceAddress =3D MapInfo->MappedHostAddress; > > + > > + // > > + // Return a pointer to the MAP_INFO structure in Mapping // > > + *Mapping =3D MapInfo; > > + > > + return EFI_SUCCESS; > > +} > > + > > + > > +/** > > + Provides the DMA controller-specific addresses needed to access syst= em > memory. > > + > > + Operation is relative to the DMA bus master. > > + > > + @param DmaAbove4GB Indicates capability of DMA operations > above 4GB. > > + @param Operation Indicates if the bus master is going t= o read or > write to system memory. > > + @param HostAddress The system memory address to map to th= e > DMA controller. > > + @param NumberOfBytes On input the number of bytes to map. O= n > output the number of bytes > > + that were mapped. > > + @param DeviceAddress The resulting map address for the bus > master controller to use to > > + access the hosts HostAddress. > > + @param Mapping A resulting value to pass to BmDmaUnma= p(). > > + > > + @retval EFI_SUCCESS The range was mapped for the returned > NumberOfBytes. > > + @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a > common buffer. > > + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. > > + @retval EFI_OUT_OF_RESOURCES The request could not be completed > due to a lack of resources. > > + @retval EFI_DEVICE_ERROR The system hardware could not map the > requested address. > > + > > +**/ > > +EFI_STATUS > > +EFIAPI > > +BmDmaMap ( > > + IN BOOLEAN DmaAbove4GB, > > + IN BM_DMA_OPERATION Operation, > > + IN VOID *HostAddress, > > + IN OUT UINTN *NumberOfBytes, > > + OUT PHYSICAL_ADDRESS *DeviceAddress, > > + OUT VOID **Mapping > > + ) > > +{ > > + EFI_PHYSICAL_ADDRESS PhysicalAddress; > > + > > + // > > + // Check for invalid inputs > > + // > > + if (HostAddress =3D=3D NULL || NumberOfBytes =3D=3D NULL || DeviceAd= dress > =3D=3D NULL || > > + Mapping =3D=3D NULL || (UINT32) Operation >=3D > DmaOperationBusMasterMaximum) { > > + return EFI_INVALID_PARAMETER; > > + } > > + > > + PhysicalAddress =3D (EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress; if > > + (DmaAbove4GB || (PhysicalAddress + *NumberOfBytes) <=3D SIZE_4GB) { > > + // > > + // If we CAN handle DMA above 4GB or the transfer is below 4GB, > > + // the DeviceAddress is simply the HostAddress > > + // > > + *DeviceAddress =3D PhysicalAddress; > > + *Mapping =3D NO_MAPPING; > > + > > + return EFI_SUCCESS; > > + } > > + > > + // > > + // If we cannot handle DMA above 4GB and any part of the DMA > > + transfer // being is above 4GB, then map the DMA transfer to a buffe= r > below 4GB. > > + // > > + if (Operation =3D=3D DmaOperationBusMasterCommonBuffer) { > > + // > > + // Common Buffer operations cannot be remapped, so return an error= . > > + // > > + return EFI_UNSUPPORTED; > > + } > > + > > + return AllocateBounceBuffer ( > > + Operation, > > + FORCE_BELOW_4GB_TRUE, > > + PhysicalAddress, > > + NumberOfBytes, > > + DeviceAddress, > > + Mapping > > + ); > > +} > > + > > + > > +/** > > + Completes the DmaOperationBusMasterRead/Write/CommonBuffer > > +operation > > + and releases any corresponding resources. > > + > > + @param Mapping The mapping value returned from > BmDmaMap(). > > + > > + @retval EFI_SUCCESS The range was unmapped. > > + @retval EFI_DEVICE_ERROR The data was not committed to the targ= et > system memory. > > + > > +**/ > > +EFI_STATUS > > +EFIAPI > > +BmDmaUnmap ( > > + IN VOID *Mapping > > + ) > > +{ > > + MAP_INFO *MapInfo; > > + > > + // > > + // Check for invalid inputs > > + // > > + if (Mapping =3D=3D NULL) { > > + return EFI_INVALID_PARAMETER; > > + } > > + > > + // > > + // See if the Map() operation associated with this Unmap() required > > + a mapping // buffer. If a mapping buffer was not required, then > > + this function simply // returns EFI_SUCCESS. > > + // > > + if (Mapping =3D=3D NO_MAPPING) { > > + return EFI_SUCCESS; > > + } > > + > > + // > > + // If this is a write operation from the Bus Master's point of > > + view, // then copy the contents of the mapped buffer into the real > > + buffer // so the processor can read the contents of the real buffer. > > + // > > + MapInfo =3D (MAP_INFO *)Mapping; > > + if (MapInfo->Operation =3D=3D DmaOperationBusMasterWrite) { > > + CopyMem ( > > + (VOID *) (UINTN) MapInfo->HostAddress, > > + (VOID *) (UINTN) MapInfo->MappedHostAddress, > > + MapInfo->NumberOfBytes > > + ); > > + } > > + > > + // > > + // Free the mapped buffer and the MAP_INFO structure. > > + // > > + gBS->FreePages (MapInfo->MappedHostAddress, > > +MapInfo->NumberOfPages); > > + FreePool (Mapping); > > + return EFI_SUCCESS; > > +} > > + > > + > > +/** > > + Allocates pages that are suitable for a BmDmaMap() of type > DmaOperationBusMasterCommonBuffer. > > + > > + @param DmaAbove4GB Indicates capability of DMA operations > above 4GB. > > + @param MemoryType The type of memory to allocate: > EfiBootServicesData or > > + EfiRuntimeServicesData. > > + @param Pages The number of pages to allocate. > > + @param HostAddress A pointer to store the base system mem= ory > address of the > > + allocated range. > > + > > + @retval EFI_SUCCESS The requested memory pages were alloca= ted. > > + @retval EFI_UNSUPPORTED Attributes is unsupported. The only le= gal > attribute bits are > > + MEMORY_WRITE_COMBINE and MEMORY_CACHED= . > > + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. > > + @retval EFI_OUT_OF_RESOURCES The memory pages could not be > allocated. > > + > > +**/ > > +EFI_STATUS > > +EFIAPI > > +BmDmaAllocateBuffer ( > > + IN BOOLEAN DmaAbove4GB, > > + IN EFI_MEMORY_TYPE MemoryType, > > + IN UINTN Pages, > > + OUT VOID **HostAddress > > + ) > > +{ > > + EFI_STATUS Status; > > + EFI_PHYSICAL_ADDRESS PhysicalAddress; > > + EFI_ALLOCATE_TYPE AllocateType; > > + > > + // > > + // Check for invalid inputs > > + // > > + if (HostAddress =3D=3D NULL) { > > + return EFI_INVALID_PARAMETER; > > + } > > + > > + // > > + // The only valid memory types are EfiBootServicesData and // > > + EfiRuntimeServicesData // if (MemoryType !=3D EfiBootServicesData &= & > > + MemoryType !=3D EfiRuntimeServicesData) { > > + return EFI_INVALID_PARAMETER; > > + } > > + > > + if (DmaAbove4GB) { > > + AllocateType =3D AllocateAnyPages; > > + } else { > > + // > > + // Limit allocations to memory below 4GB > > + // > > + AllocateType =3D AllocateMaxAddress; > > + PhysicalAddress =3D (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1); } > > + Status =3D gBS->AllocatePages ( > > + AllocateType, > > + MemoryType, > > + Pages, > > + &PhysicalAddress > > + ); > > + if (!EFI_ERROR (Status)) { > > + *HostAddress =3D (VOID *) (UINTN) PhysicalAddress; } > > + > > + return Status; > > +} > > + > > + > > +/** > > + Frees memory that was allocated with BmDmaAllocateBuffer(). > > + > > + @param Pages The number of pages to free. > > + @param HostAddress The base system memory address of the > allocated range. > > + > > + @retval EFI_SUCCESS The requested memory pages were freed. > > + @retval EFI_INVALID_PARAMETER The memory range specified by > HostAddress and Pages > > + was not allocated with BmDmaAllocateBu= ffer(). > > + > > +**/ > > +EFI_STATUS > > +EFIAPI > > +BmDmaFreeBuffer ( > > + IN UINTN Pages, > > + IN VOID *HostAddress > > + ) > > +{ > > + return gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) > HostAddress, > > +Pages); } > > + > > diff --git a/MdeModulePkg/Library/DxeBmDmaLib/DxeBmDmaLib.inf > > b/MdeModulePkg/Library/DxeBmDmaLib/DxeBmDmaLib.inf > > new file mode 100644 > > index 0000000..06bb8b1 > > --- /dev/null > > +++ b/MdeModulePkg/Library/DxeBmDmaLib/DxeBmDmaLib.inf > > @@ -0,0 +1,41 @@ > > +## @file > > +# > > +# DMA abstraction library APIs. Based on PCI IO protocol DMA > abstractions. > > +# > > +# Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.
# > > +Copyright (c) 2017, AMD Inc. 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 0x00010005 > > + BASE_NAME =3D DxeBmDmaLib > > + FILE_GUID =3D daa403e0-071d-44ef-95cf-7f2472e4a= 4d5 > > + MODULE_TYPE =3D DXE_DRIVER > > + VERSION_STRING =3D 1.0 > > + LIBRARY_CLASS =3D BmDmaLib > > + > > +[Sources.common] > > + DxeBmDmaLib.c > > + > > +[Packages] > > + MdePkg/MdePkg.dec > > + MdeModulePkg/MdeModulePkg.dec > > + > > +[LibraryClasses] > > + BaseLib > > + BaseMemoryLib > > + DebugLib > > + DxeServicesTableLib > > + MemoryAllocationLib > > + UefiBootServicesTableLib > > + > > + > > diff --git a/MdeModulePkg/MdeModulePkg.dsc > > b/MdeModulePkg/MdeModulePkg.dsc index 5996fe5..311922e 100644 > > --- a/MdeModulePkg/MdeModulePkg.dsc > > +++ b/MdeModulePkg/MdeModulePkg.dsc > > @@ -275,6 +275,7 @@ [Components] > > MdeModulePkg/Core/Pei/PeiMain.inf > > MdeModulePkg/Core/RuntimeDxe/RuntimeDxe.inf > > > > + MdeModulePkg/Library/DxeBmDmaLib/DxeBmDmaLib.inf > > MdeModulePkg/Library/DxeCapsuleLibNull/DxeCapsuleLibNull.inf > > > MdeModulePkg/Library/UefiMemoryAllocationProfileLib/UefiMemoryAlloca > tionProfileLib.inf > > > > > MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllo > catio > > nLib.inf > > -- > > 1.9.1 > > > > _______________________________________________ > > edk2-devel mailing list > > edk2-devel@lists.01.org > > https://lists.01.org/mailman/listinfo/edk2-devel