From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM03-BY2-obe.outbound.protection.outlook.com (mail-by2nam03on0615.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe4a::615]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 54DB9821FA for ; Thu, 2 Mar 2017 10:46:49 -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=QLMzMeJwMYNtKelP846Opvv9Z+W91y0llTq6FJxTsVM=; b=uqingo9adfbUjLK4wSd0agAF+MPNmk+YbiCK0SRKosJO3iK3xujnuVVXeyVQiBpRLTVZ8jz5+Soz4KdoLr9yF8RjueL6KWtZwvTKrBGhs/btomed1EFLUsLqnM2CGEinlfsq+6kKBu+J1nPf694scKz5myfBLBMhUjugEM0++b4= Authentication-Results: ml01.01.org; dkim=none (message not signed) header.d=none;ml01.01.org; dmarc=none action=none header.from=amd.com; Received: from leduran-Precision-WorkStation-T5400.amd.com (165.204.77.1) by CY4PR12MB1239.namprd12.prod.outlook.com (10.168.167.14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.933.12; Thu, 2 Mar 2017 18:46:45 +0000 From: Leo Duran To: CC: Leo Duran , Feng Tian , Star Zeng Date: Thu, 2 Mar 2017 12:46:31 -0600 Message-ID: <1488480396-32358-2-git-send-email-leo.duran@amd.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1488480396-32358-1-git-send-email-leo.duran@amd.com> References: <1488480396-32358-1-git-send-email-leo.duran@amd.com> MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: DM5PR17CA0027.namprd17.prod.outlook.com (10.173.128.141) To CY4PR12MB1239.namprd12.prod.outlook.com (10.168.167.14) X-MS-Office365-Filtering-Correlation-Id: 25e4e75e-d710-45d7-b0ce-08d4619c7a31 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(48565401081); SRVR:CY4PR12MB1239; X-Microsoft-Exchange-Diagnostics: 1; CY4PR12MB1239; 3:u72p4QPKDQew+AEAOWqmc+vXyoxiMO/hyW+k76+RBys/4bGVsvHE0ACinjjY8T+bACXFR79wUuyV3yUAmKFQTZ72t31z6fdX/42DFo+VZhO2oP/rpQdWMx9mlft1StguKDQUR8ovJbTuLG5IyKhbOynMmE4Hhfbufk9lHY0vILHMB26KBN+kHHcFBA3CRAcBWs57UpMr9XFOeJtv1TJAUpYua2aGvXhp/iqm5r77sHzdEbf69JqKooMQB1DgNu7L15YLm4oAtUuJX9AfjA3c/Ldns4K2UgpILYMA8lTFCTM=; 25:6ggacL+q37ET9g1vjZXm/de3K/UZNK1ErKNisjICPEiyS50YwSGWIlRlQfE7vus6e9AlMcReZ1VxLS5F7n1XK32l8sJsEsMliipD2ut4ixe4rggWaTNZuoYq8PPpy5pFlEAVg5mDIRB/KhJGZ0wsk3WQYf7KS49aGKy5RUhqT0D+aTQ9D46ohVFoo4aL5XTYwSRIWMDUk+T4vHahFheRVzCly+MxNiOciQ2mTpBeTS9R6d5aqle9kFUtt2ix7U2wD1+J6s8J1/m/SvIWE8AlxKZpUxbMudbohalY6RXgctNvXrM3M4JS3gayV7H5+WOe6avSRS4khSniGvWldTgPxja/NNF3z+dhlH2pP1q4fxNNQxgXjOtGATEEQkx0uRitq4mYx2BQihpPxRHqoecezzrZTc1fS/o//vMS9jxYZ+SU1C0peBQaHCAJcmnw8geZ0EZoT896k9PmOnpl98SXPg== X-Microsoft-Exchange-Diagnostics: 1; CY4PR12MB1239; 31:hoDUC0bpZJ7R8+K6d9xT+XswYNUBn1gTVwRY2Om6e/g4h29kRU0ZnW1ZmDcV8EWUmgia28WvX7ejXuYVUKq5q6PbRcKH9+zlUq2nZNCeo4RhKtkHRc309H/ktNQY/N4hoK8lxYZt8XIcICilZaYeBTreve1daWIsCOOlN2dXUgH9PhlgPbIo2ooDdKvAdGjpnnHDwjz3Ml2mfMkjH0BJmqs5w6O7M65RcPexvp/4NwxYulefI3SpfkYS7QbqK7fV; 20:zatQjUaNjcXmWkRB7iJ6KMemHNo3bDWbcKex4b/iYlsTkiLeOSAqljLq3kPZfMWlaXSp4YlxyC4Z0XmQ07ZpxL3BP2rpofasFR5BUVNZ6vj/1M1SSb+nW1sTTEa+SYNoc6p0mXkFioZpUPOlv+6Bt90/um2oRL84w6jf2LsXf9fDBjFzQGyN0Wo/JHT+E93nPAwNLCi6Il5IusBv+Nk2wKp53EQ0tuGWBgANWbGd1YtfOjR3oI2qX4kHN7SUP0uinViQycDhIwqW0ktW2euQD5i5SSQ+niqJi2ae0LiujOFzGfgv+N5npXSA/XrQAqO0XgQVtqAjID2ScMtNirHv88mnEfMvPiI/2rQY/fPEKFLGl1PKjy/A01h99AOBtcLbiH6H9TKpA/afsGAZ6tz3H99w2E7e54u6Gd84P4X5KnM+lYsO1OelYdL9xdznSX4B5TbAyi3ZptFD4lMml7XiFixB1kLtNXFRjbPb/jGIrsHWjCJPL32NU2dVfQEV7LNS X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110)(228905959029699); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040375)(601004)(2401047)(8121501046)(5005006)(3002001)(10201501046)(6055026)(6041248)(20161123564025)(20161123562025)(20161123560025)(20161123555025)(20161123558025)(6072148); SRVR:CY4PR12MB1239; BCL:0; PCL:0; RULEID:; SRVR:CY4PR12MB1239; X-Microsoft-Exchange-Diagnostics: 1; CY4PR12MB1239; 4:1v5eN9WTkli4suKZ2rMu3etjshUslt3ASBS3d8gPfwRbXFvm2fh1YRPjanVXVufHzvDbugftdXJpWvdFowHt5QtMzOAO23P69VnNQUnNq3uYDHXKFrnrfBXum7v1BTNbD2pVoSgKqJUpGD9UTxgigEl6eKWYrxNEQmE72lK5iMw+alFkc74+vCDRBZ4PIudrX3+I/8UxmJjD1rcMzHRm/C+xaUW0Zb04elIt8YIhZNpYjT3XQU+u3xaMgppUEtakCoMzWl5OkqUuR5KonP+yf+EWfzwlnwuwW1QapuPSc5EVooF77epURPFy4Emk7saq3vz24BFmhjh5UY0LW6Sb+Zx8gM32yTlW/y943BsHIqSOr4UBu2rF/Clx2BA45DorWIifZL6xvqElmcm79JiKr2YKoUfB9kfQiE87Ve4sAXtYUl4QUNeaFufrMSqOtTshemn0XIoTqVaW6xrzOnpF1SNraLgbVKU39W1KKdZcQBdSatWGx7RzHu3jI2FuHFc4PlIZIIvp8dB+0kx08g+KjQTeYWvZcSQFhPnfwlKUvpu5uvjsaBPKtacE/SbPm3NC0N0nzka25BNa0odRj6cwDBdBJVkb0bHc6ch/54skpifeQ3Y+KqaKq74fj9a4XFRRMhwDE8B3m+qFkLsp8rbg6R5nwYMQrdvvBtrx05gYX65NpK53vNrb9xJzM7mdCW+0Ddfdp9AnwCQHkd8Xz2/OoA== X-Forefront-PRVS: 023495660C X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(979002)(6029001)(6009001)(7916002)(39850400002)(39450400003)(39410400002)(39860400002)(39840400002)(76176999)(6486002)(2906002)(50986999)(25786008)(6116002)(189998001)(6666003)(7736002)(36756003)(6916009)(2950100002)(3846002)(305945005)(5660300001)(66066001)(50226002)(42186005)(53416004)(47776003)(16799955002)(4326008)(48376002)(15188155005)(50466002)(966004)(33646002)(81166006)(92566002)(8676002)(6306002)(53376002)(110136004)(38730400002)(5003940100001)(86362001)(54906002)(53936002)(2351001)(969003)(989001)(999001)(1009001)(1019001); DIR:OUT; SFP:1101; SCL:1; SRVR:CY4PR12MB1239; H:leduran-Precision-WorkStation-T5400.amd.com; FPR:; SPF:None; MLV:ovrnspm; PTR:InfoNoRecords; LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?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H6mPXNZP+EII3S92uMLJMXxIJtEarPq8pMk3/80rikDwNhZPoT9O7TPlKKhAzFnxo77EbfiCea1mAQu4/o+t6VViKoXyWaxA==; 24:rJbPDU7iwPTOeIry2gYDTuaw228FT3VaMBdedZ2pIlCXhQ0GiTjcJbXOQRhW1oopo12WxAgtHFSpnWGKloegsxc8EwWmS7qudl6g3jmtYlE= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; CY4PR12MB1239; 7:lkiorO+KxGWr/hm8//faHgNlC4svxVuA6TgPRfue9K5C9GpR4ReJJ7Hy2UlaCW5srKjKWH7B3A4g/rl6dvAb/PTrormRLPpkuJLnDy0bycEAiNf9MjQ8dWezmrveY00GJ/1lu9snLXbrXu+TKUzQRt7cwc2d44nOGSbVctZ2lJLEHH7/rCO3nFqopLa4MR1qs5JNqjCVpiwK+wfw7uoRh20cfJFPGy2bhowkKVCG062v0QYF9A87IzyOUDBL9kiEVGlPNpQLBdX3E5fYCouRRFZS+IcRXsmMKqZhFnqNCX9fhdSXxXRGjcAR9PwO0Fb7fjIEa60X/H0Osxh+Ifj71w==; 20:LN9HmQbMmSv62jrv8I87zWOEO9wR54iqNOvy41DoQN8Y5nKiTLltcKe4tttIUhvLd1LY7UC8uwqFpRxwRPREMU2WdVUREQVjhip2CfyrDNx0bHUH1vgqEqyWXE+9nJKjIprokosynuiKYLjXm1e6TctVyfTKauBRvzjTvoPW4MU6NpkaCARscef3l9TI6rXuM5370bGy7rZAGlw3/sw1bxj3z8m3t7bknjFSDcchaBDtzX6ougixlwkOsmLPuTby X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Mar 2017 18:46:45.6760 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR12MB1239 Subject: [PATCH v4 1/6] MdeModulePkg: Add DxeBmDmaLib (BmDmaLib class) 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: Thu, 02 Mar 2017 18:46:49 -0000 Content-Type: text/plain This patch provides an abstraction layer for Bus-master DMA operations as currently 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 cloned from the existing DmaLib with an additional DmaAbove4GB (BOOLEAN) parameter for the Map and Allocate interfaces, so that decisions can be made about the need to allocate DMA buffers below the 4GB boundary. 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 | 2 + 4 files changed, 555 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..070340c --- /dev/null +++ b/MdeModulePkg/Include/Library/BmDmaLib.h @@ -0,0 +1,161 @@ +/** @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.
+ + 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 system 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 to read or write to system memory. + @param HostAddress The system memory address to map to the DMA controller. + @param NumberOfBytes On input the number of bytes to map. On 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 BmDmaUnmap(). + + @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 target 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 memory address of the + allocated range. + + @retval EFI_SUCCESS The requested memory pages were allocated. + @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal 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 HostAddress The base system memory address of the allocated range. + @param Pages The number of pages to free. + + @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 BmDmaAllocateBuffer(). + +**/ +EFI_STATUS +EFIAPI +BmDmaFreeBuffer ( + IN VOID *HostAddress, + IN UINTN Pages + ); + + +#endif + diff --git a/MdeModulePkg/Library/DxeBmDmaLib/DxeBmDmaLib.c b/MdeModulePkg/Library/DxeBmDmaLib/DxeBmDmaLib.c new file mode 100644 index 0000000..a342c9e --- /dev/null +++ b/MdeModulePkg/Library/DxeBmDmaLib/DxeBmDmaLib.c @@ -0,0 +1,351 @@ +/** @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. + + 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 = AllocatePool (sizeof (MAP_INFO)); + if (MapInfo == NULL) { + *NumberOfBytes = 0; + return EFI_OUT_OF_RESOURCES; + } + + // + // Initialize the MAP_INFO structure + // + MapInfo->Operation = Operation; + MapInfo->NumberOfBytes = *NumberOfBytes; + MapInfo->NumberOfPages = EFI_SIZE_TO_PAGES (MapInfo->NumberOfBytes); + MapInfo->HostAddress = HostAddress; + + if (ForceBelow4GB) { + // + // Limit allocations to memory below 4GB + // + AllocateType = AllocateMaxAddress; + MapInfo->MappedHostAddress = SIZE_4GB - 1; + } else { + AllocateType = AllocateAnyPages; + } + + // + // Allocate DMA bounce buffer + // + Status = gBS->AllocatePages ( + AllocateType, + EfiBootServicesData, + MapInfo->NumberOfPages, + &MapInfo->MappedHostAddress + ); + + if (EFI_ERROR (Status)) { + FreePool (MapInfo); + *NumberOfBytes = 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 == DmaOperationBusMasterRead) { + CopyMem ( + (VOID *) (UINTN) MapInfo->MappedHostAddress, + (VOID *) (UINTN) MapInfo->HostAddress, + MapInfo->NumberOfBytes + ); + } + + // + // The DeviceAddress is the address of the mapped buffer + // + *DeviceAddress = MapInfo->MappedHostAddress; + + // + // Return a pointer to the MAP_INFO structure in Mapping + // + *Mapping = MapInfo; + + return EFI_SUCCESS; +} + + +/** + Provides the DMA controller-specific addresses needed to access system 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 to read or write to system memory. + @param HostAddress The system memory address to map to the DMA controller. + @param NumberOfBytes On input the number of bytes to map. On 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 BmDmaUnmap(). + + @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 == NULL || NumberOfBytes == NULL || DeviceAddress == NULL || + Mapping == NULL || (UINT32) Operation >= DmaOperationBusMasterMaximum) { + return EFI_INVALID_PARAMETER; + } + + PhysicalAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress; + if (DmaAbove4GB || (PhysicalAddress + *NumberOfBytes) <= SIZE_4GB) { + // + // If we CAN handle DMA above 4GB or the transfer is below 4GB, + // the DeviceAddress is simply the HostAddress + // + *DeviceAddress = PhysicalAddress; + *Mapping = 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 buffer below 4GB. + // + if (Operation == 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 target system memory. + +**/ +EFI_STATUS +EFIAPI +BmDmaUnmap ( + IN VOID *Mapping + ) +{ + MAP_INFO *MapInfo; + + // + // Check for invalid inputs + // + if (Mapping == 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 == 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 = (MAP_INFO *)Mapping; + if (MapInfo->Operation == 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 memory address of the + allocated range. + + @retval EFI_SUCCESS The requested memory pages were allocated. + @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal 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 == NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // The only valid memory types are EfiBootServicesData and + // EfiRuntimeServicesData + // + if (MemoryType != EfiBootServicesData && + MemoryType != EfiRuntimeServicesData) { + return EFI_INVALID_PARAMETER; + } + + if (DmaAbove4GB) { + AllocateType = AllocateAnyPages; + } else { + // + // Limit allocations to memory below 4GB + // + AllocateType = AllocateMaxAddress; + PhysicalAddress = (EFI_PHYSICAL_ADDRESS) (SIZE_4GB - 1); + } + Status = gBS->AllocatePages ( + AllocateType, + MemoryType, + Pages, + &PhysicalAddress + ); + if (!EFI_ERROR (Status)) { + *HostAddress = (VOID *) (UINTN) PhysicalAddress; + } + + return Status; +} + + +/** + Frees memory that was allocated with BmDmaAllocateBuffer(). + + @param HostAddress The base system memory address of the allocated range. + @param Pages The number of pages to free. + + @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 BmDmaAllocateBuffer(). + +**/ +EFI_STATUS +EFIAPI +BmDmaFreeBuffer ( + IN VOID *HostAddress, + IN UINTN Pages + ) +{ + 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..4ddb27d --- /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 = 0x00010005 + BASE_NAME = DxeBmDmaLib + FILE_GUID = daa403e0-071d-44ef-95cf-7f2472e4a4d5 + MODULE_TYPE = DXE_DRIVER + VERSION_STRING = 1.0 + LIBRARY_CLASS = BmDmaLib|DXE_DRIVER + +[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 1bb361a..0c63197 100644 --- a/MdeModulePkg/MdeModulePkg.dsc +++ b/MdeModulePkg/MdeModulePkg.dsc @@ -3,6 +3,7 @@ # # (C) Copyright 2014 Hewlett-Packard Development Company, L.P.
# Copyright (c) 2007 - 2016, Intel Corporation. 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 @@ -275,6 +276,7 @@ MdeModulePkg/Core/Pei/PeiMain.inf MdeModulePkg/Core/RuntimeDxe/RuntimeDxe.inf + MdeModulePkg/Library/DxeBmDmaLib/DxeBmDmaLib.inf MdeModulePkg/Library/DxeCapsuleLibNull/DxeCapsuleLibNull.inf MdeModulePkg/Library/UefiMemoryAllocationProfileLib/UefiMemoryAllocationProfileLib.inf MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeCoreMemoryAllocationLib.inf -- 2.7.4