From patchwork Fri Aug 25 12:10:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 111013 Delivered-To: patch@linaro.org Received: by 10.140.95.78 with SMTP id h72csp909417qge; Fri, 25 Aug 2017 05:10:29 -0700 (PDT) X-Received: by 10.98.46.196 with SMTP id u187mr9872479pfu.199.1503663029101; Fri, 25 Aug 2017 05:10:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1503663029; cv=none; d=google.com; s=arc-20160816; b=tzwbx8qbr1Euecme9UoXDFQ8WnxpVUyR9+1N6p1jHMYupOOPjbgkaFj7Kc1Ib5mrxK yiakmen2iPyndk6hLSP1I+OWHCgLRU1MWCB/C+dlvE6dMhdZ9ANEgLxSAtIqWkj1BWpB GP9CzipyftJhFz5EfIsnZ5vFoomy9gdzIYWI3qT9kZphfp7gWpt1TbD6P13DMSvJ50No 0VxYOuMcl9SDxD5bRmNDFWJTvlz/6Xo+Jc3CJsgxYFwlyyayfikDU3rUMAmRWHmJE4oB 8+Wr/Rzv2OqlTP7s45wWzkU4aTL6ECeCM0Kmwou+HRmq/Bfu81NJBDCxUHuJ67j0ooR2 HU6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=KbnvtBSdsf+ErynGhhzPKzOoz2Qzzd6YNgVy5Cf5My0=; b=nG9/7HhF5V3fxBXB+dTdqpymhnROvmieNgAmNmHAVgkU3LCzLgz8pHXatwRPEEEsl2 OyEz62GNWUyOrB/iOxKnZqqho11Ys5lnn4UE2gRPMJMuui8CvBvD7Llui96h+v55JDP7 Fjdh8/+kY0dts7KxU3bAdjxDiWZEB16W9PI3DIu6vnbUH72uuH4nIIoj10Xf5UEr3JVz r/r8hsMB4ovYAn3GTyorh932eHv3Ne+eJZz7LMtoY6sBKHZuV+msHHbeF2R0pUnH1AyL MighNsp0uSY6s6bk1BN/4u4ezLc8Be3oiXJ+OGbKxwSrIUrF8/P/om2lpwRIv3sD2iFx 8g1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=DcFP/L5W; 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 sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from ml01.01.org (ml01.01.org. [2001:19d0:306:5::1]) by mx.google.com with ESMTPS id 43si4968225plc.358.2017.08.25.05.10.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 Aug 2017 05:10:29 -0700 (PDT) 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 header.s=google header.b=DcFP/L5W; 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 sp=NONE dis=NONE) header.from=linaro.org Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 998C72095B9C1; Fri, 25 Aug 2017 05:07:51 -0700 (PDT) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received: from mail-wm0-x233.google.com (mail-wm0-x233.google.com [IPv6:2a00:1450:400c:c09::233]) (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 EB95821D1E2FD for ; Fri, 25 Aug 2017 05:07:49 -0700 (PDT) Received: by mail-wm0-x233.google.com with SMTP id y71so1270500wmd.0 for ; Fri, 25 Aug 2017 05:10:26 -0700 (PDT) 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=Y1sSqGmfUOfPbY32xGHgfLILD2DZ7QTKNifTerqPotA=; b=DcFP/L5WeUqFw1k60Zd/zu7Xs6fYAockClUYgsYUXF4VuEgVwKjK/cL5/bhGLitV61 LgdJ94lVHFdyiKY8m8IXZ79URk0K0Kebte/QRxZInswnFXuc5uMqoyahCDW1TfF25Fqz fyunsTusGIy3plsruWRqTKYXcuABUG4Nx+h+A= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Y1sSqGmfUOfPbY32xGHgfLILD2DZ7QTKNifTerqPotA=; b=U0AHhI04MmuU8GDHiElxKX05HaH0ZrOebvLtomptIKtiqv0lt+E5y7wdW/nyfUSfnw UbN4W0IKSTOmkKorZoTQxJ/GX26inFCVNhBJsZTfh/NTL8QicIlE05FzSnpz0Bh+pwNV GMySgaKps9lO9VRgv+m9wVf3JFQQDoV5LFtvWOqgsvJwikOSJ6YDmYUrIWM7VSUaHUj9 HS+7rRpoSVtn2Lg7vi4YNwvvR+iPENhUIBN9ARrxXQ9Eut7S/x6NRfZ7Ctawf+iMrvux v51qKWw0PkbnNCpUmTPHTgRJqblN4ZLb/LyddGiS+Jlk+hOd8fJnvk+VPbgTmNIonbXq AaWg== X-Gm-Message-State: AHYfb5gm5LJLnfbx+qJRELH+KGmtsnETPkvEX3UMW6BrnWTd5nP0dDGv f5rThdmOH4hesXWGjpVgqw== X-Received: by 10.28.97.132 with SMTP id v126mr1279543wmb.18.1503663024151; Fri, 25 Aug 2017 05:10:24 -0700 (PDT) Received: from localhost.localdomain ([105.137.125.182]) by smtp.gmail.com with ESMTPSA id 9sm1996137wmo.35.2017.08.25.05.10.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 Aug 2017 05:10:23 -0700 (PDT) From: Ard Biesheuvel To: edk2-devel@lists.01.org, leif.lindholm@linaro.org Date: Fri, 25 Aug 2017 13:10:10 +0100 Message-Id: <20170825121014.15739-2-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170825121014.15739-1-ard.biesheuvel@linaro.org> References: <20170825121014.15739-1-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH 1/5] EmbeddedPkg/DmaLib: add routine to allocate aligned buffers X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Ard Biesheuvel MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" DmaLib's purpose is to manage memory that is shared between the host and DMA capable devices. In some cases, this requires a larger alignment than page size, and we currently don't cater for that in DmaLib. So add a variant of DmaAllocateBuffer () that takes an alignment parameter. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Ard Biesheuvel --- EmbeddedPkg/Include/Library/DmaLib.h | 30 +++++++++++- EmbeddedPkg/Library/NullDmaLib/NullDmaLib.c | 48 ++++++++++++++++++-- 2 files changed, 72 insertions(+), 6 deletions(-) -- 2.11.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel diff --git a/EmbeddedPkg/Include/Library/DmaLib.h b/EmbeddedPkg/Include/Library/DmaLib.h index 3814291c2875..1843814c65ca 100644 --- a/EmbeddedPkg/Include/Library/DmaLib.h +++ b/EmbeddedPkg/Include/Library/DmaLib.h @@ -155,5 +155,33 @@ DmaFreeBuffer ( ); -#endif +/** + Allocates pages that are suitable for an DmaMap() of type + MapOperationBusMasterCommonBuffer mapping, at the requested alignment. + + @param MemoryType The type of memory to allocate, EfiBootServicesData or + EfiRuntimeServicesData. + @param Pages The number of pages to allocate. + @param Alignment Alignment in bytes of the base of the returned + buffer (must be a power of 2) + @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 +DmaAllocateAlignedBuffer ( + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN Pages, + IN UINTN Alignment, + OUT VOID **HostAddress + ); + + +#endif diff --git a/EmbeddedPkg/Library/NullDmaLib/NullDmaLib.c b/EmbeddedPkg/Library/NullDmaLib/NullDmaLib.c index a0bb57541d60..4cbe349190a9 100644 --- a/EmbeddedPkg/Library/NullDmaLib/NullDmaLib.c +++ b/EmbeddedPkg/Library/NullDmaLib/NullDmaLib.c @@ -100,23 +100,61 @@ DmaAllocateBuffer ( OUT VOID **HostAddress ) { - if (HostAddress == NULL) { + return DmaAllocateAlignedBuffer (MemoryType, Pages, 0, HostAddress); +} + + +/** + Allocates pages that are suitable for an DmaMap() of type + MapOperationBusMasterCommonBuffer mapping, at the requested alignment. + + @param MemoryType The type of memory to allocate, EfiBootServicesData or + EfiRuntimeServicesData. + @param Pages The number of pages to allocate. + @param Alignment Alignment in bytes of the base of the returned + buffer (must be a power of 2) + @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 +DmaAllocateAlignedBuffer ( + IN EFI_MEMORY_TYPE MemoryType, + IN UINTN Pages, + IN UINTN Alignment, + OUT VOID **HostAddress + ) +{ + if (Alignment == 0) { + Alignment = EFI_PAGE_SIZE; + } + + if (HostAddress == NULL || + (Alignment & (Alignment - 1)) != 0) { return EFI_INVALID_PARAMETER; } // // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData // - // We used uncached memory to keep coherency - // if (MemoryType == EfiBootServicesData) { - *HostAddress = AllocatePages (Pages); + *HostAddress = AllocateAlignedPages (Pages, Alignment); } else if (MemoryType != EfiRuntimeServicesData) { - *HostAddress = AllocateRuntimePages (Pages); + *HostAddress = AllocateAlignedRuntimePages (Pages, Alignment); } else { return EFI_INVALID_PARAMETER; } + if (*HostAddress == NULL) { + return EFI_OUT_OF_RESOURCES; + } return EFI_SUCCESS; }