From patchwork Tue Apr 19 14:55:33 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 66103 Delivered-To: patch@linaro.org Received: by 10.140.93.198 with SMTP id d64csp1897733qge; Tue, 19 Apr 2016 07:55:51 -0700 (PDT) X-Received: by 10.66.183.173 with SMTP id en13mr4743064pac.8.1461077750944; Tue, 19 Apr 2016 07:55:50 -0700 (PDT) Return-Path: Received: from ml01.01.org (ml01.01.org. [2001:19d0:306:5::1]) by mx.google.com with ESMTPS id vq9si7236913pab.209.2016.04.19.07.55.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Apr 2016 07:55:50 -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; 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 3EF331A2298; Tue, 19 Apr 2016 07:55:47 -0700 (PDT) X-Original-To: edk2-devel@lists.01.org Delivered-To: edk2-devel@lists.01.org Received: from mail-wm0-x235.google.com (mail-wm0-x235.google.com [IPv6:2a00:1450:400c:c09::235]) (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 17F541A2298 for ; Tue, 19 Apr 2016 07:55:46 -0700 (PDT) Received: by mail-wm0-x235.google.com with SMTP id n3so34394396wmn.0 for ; Tue, 19 Apr 2016 07:55:46 -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=mEf6Rtc5pvDAfz7PpYkOyaOYQV3d5EfmUYs9kdkJmLA=; b=kT79iwnYTiC3yLpmH5T/WWg3ndvmAaQpcHhW1UouXOXHENUVyT7fATvdsfn+u89pfZ 19AFsFxT68XzRKz/g/zTorqkOz/mnbAaulOk+WYdfxO2ZgyvBzE1OdeGXMgwYKt+wfg6 YVTvqXaIonCIqt0lmUJBJ1TrO5Kk307/RAXls= 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=mEf6Rtc5pvDAfz7PpYkOyaOYQV3d5EfmUYs9kdkJmLA=; b=DMOi/7IBOqmq1AXCCMFAKHA8W9erxfSRDfNf5S7FyH5PPxFucuy3KaVddvGcdcDRzf vFWL9WwxquzoApzAEF9/9Omm6ibCvBPLrSLXmnl+6BpSl88mTVcilAkQIoQM6f8NHEaY nmsbOUsc+vPIAVyctJPtU78YkU5sg8m2jG1dz46h5dfrCXxNI8n3uPpIKzj7Rbl/UuvV ODNm9QKBv0eGD4Bi9XK6begiftH5piJEpyZWGQaS8BMtY7LnW48VpAlQnAcCsJseBe9n RmDu+Fg9LjjgTuSGh7l1HecWX3WVXS5xpnl8RQ3HsPw5z16eU42XeS8LQwXzIAE5KaDm nh4Q== X-Gm-Message-State: AOPr4FWXrdOb492i9c8bIOzf7BAQp3C6RyTPbm7y6OzeQqfPqZK+gw74tMBrkIiBzAOJWy1O X-Received: by 10.28.165.142 with SMTP id o136mr3969101wme.31.1461077744728; Tue, 19 Apr 2016 07:55:44 -0700 (PDT) Received: from localhost.localdomain ([195.55.142.58]) by smtp.gmail.com with ESMTPSA id i5sm684778wjx.15.2016.04.19.07.55.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 19 Apr 2016 07:55:44 -0700 (PDT) From: Ard Biesheuvel To: edk2-devel@lists.01.org, leif.lindholm@linaro.org, ryan.harkin@linaro.org Date: Tue, 19 Apr 2016 16:55:33 +0200 Message-Id: <1461077734-4327-4-git-send-email-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1461077734-4327-1-git-send-email-ard.biesheuvel@linaro.org> References: <1461077734-4327-1-git-send-email-ard.biesheuvel@linaro.org> Subject: [edk2] [PATCH 4/5] ArmPkg/ArmDmaLib: reject consistent DMA mappings of cached memory X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: heyi.guo@linaro.org, Ard Biesheuvel MIME-Version: 1.0 Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" DmaMap () operations of type MapOperationBusMasterCommonBuffer should return a mapping that is coherent between the CPU and the device. For this reason, the API only allows DmaMap () to be called with this operation type if the memory to be mapped was allocated by DmaAllocateBuffer (), which in this implementation guarantees the coherency by using uncached mappings on the CPU side. This means that, if we encounter a cached mapping in DmaMap () with this operation type, the code is either broken, or someone is violating the API, but simply proceeding with a double buffer makes no sense at all, and can only cause problems. So instead, actively reject this operation type for cached memory mappings. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Ard Biesheuvel --- ArmPkg/Library/ArmDmaLib/ArmDmaLib.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) -- 2.5.0 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel Reviewed-by: Leif Lindholm diff --git a/ArmPkg/Library/ArmDmaLib/ArmDmaLib.c b/ArmPkg/Library/ArmDmaLib/ArmDmaLib.c index 7f6598318a91..7e518ed3b83e 100644 --- a/ArmPkg/Library/ArmDmaLib/ArmDmaLib.c +++ b/ArmPkg/Library/ArmDmaLib/ArmDmaLib.c @@ -103,6 +103,18 @@ DmaMap ( // If the mapped buffer is not an uncached buffer if ((GcdDescriptor.Attributes & (EFI_MEMORY_WB | EFI_MEMORY_WT)) != 0) { // + // Operations of type MapOperationBusMasterCommonBuffer are only allowed + // on uncached buffers. + // + if (Operation == MapOperationBusMasterCommonBuffer) { + DEBUG ((EFI_D_ERROR, + "%a: Operation type 'MapOperationBusMasterCommonBuffer' is only supported\n" + "on memory regions that were allocated using DmaAllocateBuffer ()\n", + __FUNCTION__)); + return EFI_UNSUPPORTED; + } + + // // If the buffer does not fill entire cache lines we must double buffer into // uncached memory. Device (PCI) address becomes uncached page. // @@ -112,7 +124,7 @@ DmaMap ( return Status; } - if ((Operation == MapOperationBusMasterRead) || (Operation == MapOperationBusMasterCommonBuffer)) { + if (Operation == MapOperationBusMasterRead) { CopyMem (Buffer, HostAddress, *NumberOfBytes); } @@ -168,7 +180,9 @@ DmaUnmap ( Map = (MAP_INFO_INSTANCE *)Mapping; if (Map->DoubleBuffer) { - if ((Map->Operation == MapOperationBusMasterWrite) || (Map->Operation == MapOperationBusMasterCommonBuffer)) { + ASSERT (Map->Operation != MapOperationBusMasterCommonBuffer); + + if (Map->Operation == MapOperationBusMasterWrite) { CopyMem ((VOID *)(UINTN)Map->HostAddress, (VOID *)(UINTN)Map->DeviceAddress, Map->NumberOfBytes); }