From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-it0-x231.google.com (mail-it0-x231.google.com [IPv6:2607:f8b0:4001:c0b::231]) (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 6279F2095B9E6 for ; Thu, 17 Aug 2017 05:24:33 -0700 (PDT) Received: by mail-it0-x231.google.com with SMTP id 77so30190134itj.1 for ; Thu, 17 Aug 2017 05:27:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=GBlL8oGguQIEnc5kkuNM1BWOY89xhyEV5rZ2g2UHB7k=; b=ADAXUlMzhmals9gARbyUEoNU0EBJWyAhG/dn2FqETO+d3o/iojgAl+i3kB/t92yL/H 0DzpQ7hgCpwcpTf+0icm5tYCbHGJmXoaCRWExDRJqzigzpi6zwft2047wcbfBlumCPrP Hqkg+DusCnw+cP8YbbPcQcTKVZO2waM1qu49U= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=GBlL8oGguQIEnc5kkuNM1BWOY89xhyEV5rZ2g2UHB7k=; b=Y3E3L9B1inlvua8CmsW6HYa010cPbDMiIz5n7uUzdeUOwqkj4wLU88/akPPaKeOPuD /Kk2q9s9pIO60n4jmS+S+fpCvqU5x8B2Z8HsiRs88vsDehcLu1Z2yfYVqZPTa6STuKLW 8PMXD58aThimxtY8Zd43s7KMYG6ZoLK5CGdZNPMsFnck6h/W2J242qsxtgcUzkZ58B/r SuR6gbov/+l1kujvnmyFmBDvGsZNunVyRPYCzKi6OEIqEunmwWE9T+HC/fTYjziKJsPY JmvxQBZKjS0OCJCKhYoEflO+B5Ht+wlfYmEEoVEeCY1xjH1fNhG5mG2HLPwMe4WOb3M3 Ppag== X-Gm-Message-State: AHYfb5j8w/nh84bU0sHsmfT8sCZvigPGA9TybAG1B7uEiKkzfrOz522s KxKsoUdUpr3r97fQhAzpuBX/SlqeZk5JkDk= X-Received: by 10.36.175.76 with SMTP id l12mr1617091iti.167.1502972819250; Thu, 17 Aug 2017 05:26:59 -0700 (PDT) MIME-Version: 1.0 Received: by 10.107.162.1 with HTTP; Thu, 17 Aug 2017 05:26:58 -0700 (PDT) In-Reply-To: <20170817122546.17683-1-ard.biesheuvel@linaro.org> References: <20170817122546.17683-1-ard.biesheuvel@linaro.org> From: Ard Biesheuvel Date: Thu, 17 Aug 2017 13:26:58 +0100 Message-ID: To: "edk2-devel@lists.01.org" , Leif Lindholm , Laszlo Ersek Cc: Ard Biesheuvel Subject: Re: [PATCH] ArmPkg/ArmDmaLib: use double buffering only for bus master write 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: , X-List-Received-Date: Thu, 17 Aug 2017 12:24:33 -0000 Content-Type: text/plain; charset="UTF-8" On 17 August 2017 at 13:25, Ard Biesheuvel wrote: > The ArmPkg implementation of DmaLib uses double buffering to ensure > that any attempt to perform non-coherent DMA on unaligned buffers cannot > corrupt adjacent unrelated data which happens to share cachelines with > the data we are exchanging with the device. > > Such corruption can only occur on bus master read, in which case we have *write* not read, apologies. > to invalidate the caches to ensure the CPU will see the data written to > memory by the device. In the bus master write case, we can simply clean > and invalidate at the same time, which may purge unrelated adjacent data > from the caches, but will not corrupt its contents. > > Also, this double buffer does not necessarily have to be allocated from > uncached memory: by the same reasoning, we can perform cache invalidation > on an ordinary pool allocation as long as we take the same alignment > constraints into account. > > So update our code accordingly: remove double buffering from the bus > master read path, and switch to a pool allocation for the double buffer. > > Contributed-under: TianoCore Contribution Agreement 1.0 > Signed-off-by: Ard Biesheuvel > --- > ArmPkg/Library/ArmDmaLib/ArmDmaLib.c | 47 ++++++++++++-------- > 1 file changed, 28 insertions(+), 19 deletions(-) > > diff --git a/ArmPkg/Library/ArmDmaLib/ArmDmaLib.c b/ArmPkg/Library/ArmDmaLib/ArmDmaLib.c > index f4ee9e4c5ea2..61d70614bff0 100644 > --- a/ArmPkg/Library/ArmDmaLib/ArmDmaLib.c > +++ b/ArmPkg/Library/ArmDmaLib/ArmDmaLib.c > @@ -80,6 +80,7 @@ DmaMap ( > MAP_INFO_INSTANCE *Map; > VOID *Buffer; > EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor; > + UINTN AllocSize; > > if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL || Mapping == NULL ) { > return EFI_INVALID_PARAMETER; > @@ -104,8 +105,9 @@ DmaMap ( > return EFI_OUT_OF_RESOURCES; > } > > - if ((((UINTN)HostAddress & (mCpu->DmaBufferAlignment - 1)) != 0) || > - ((*NumberOfBytes & (mCpu->DmaBufferAlignment - 1)) != 0)) { > + if (Operation != MapOperationBusMasterRead && > + ((((UINTN)HostAddress & (mCpu->DmaBufferAlignment - 1)) != 0) || > + ((*NumberOfBytes & (mCpu->DmaBufferAlignment - 1)) != 0))) { > > // Get the cacheability of the region > Status = gDS->GetMemorySpaceDescriptor ((UINTN)HostAddress, &GcdDescriptor); > @@ -129,21 +131,24 @@ DmaMap ( > } > > // > - // If the buffer does not fill entire cache lines we must double buffer into > - // uncached memory. Device (PCI) address becomes uncached page. > + // If the buffer does not fill entire cache lines we must double buffer > + // into a suitably aligned allocation that allows us to invalidate the > + // cache without running the risk of corrupting adjacent unrelated data. > + // Note that pool allocations are guaranteed to be 8 byte aligned, so > + // we only have to add (alignment - 8) worth of padding. > // > - Map->DoubleBuffer = TRUE; > - Status = DmaAllocateBuffer (EfiBootServicesData, EFI_SIZE_TO_PAGES (*NumberOfBytes), &Buffer); > - if (EFI_ERROR (Status)) { > + Map->DoubleBuffer = TRUE; > + AllocSize = ALIGN_VALUE (*NumberOfBytes, mCpu->DmaBufferAlignment) + > + (mCpu->DmaBufferAlignment - 8); > + Map->BufferAddress = AllocatePool (AllocSize); > + if (Map->BufferAddress == NULL) { > + Status = EFI_OUT_OF_RESOURCES; > goto FreeMapInfo; > } > > - if (Operation == MapOperationBusMasterRead) { > - CopyMem (Buffer, HostAddress, *NumberOfBytes); > - } > - > + Buffer = ALIGN_POINTER (Map->BufferAddress, mCpu->DmaBufferAlignment); > *DeviceAddress = HostToDeviceAddress (ConvertToPhysicalAddress (Buffer)); > - Map->BufferAddress = Buffer; > + > } else { > Map->DoubleBuffer = FALSE; > } > @@ -207,6 +212,7 @@ DmaUnmap ( > { > MAP_INFO_INSTANCE *Map; > EFI_STATUS Status; > + VOID *Buffer; > > if (Mapping == NULL) { > ASSERT (FALSE); > @@ -217,17 +223,20 @@ DmaUnmap ( > > Status = EFI_SUCCESS; > if (Map->DoubleBuffer) { > - ASSERT (Map->Operation != MapOperationBusMasterCommonBuffer); > + ASSERT (Map->Operation == MapOperationBusMasterWrite); > > - if (Map->Operation == MapOperationBusMasterCommonBuffer) { > + if (Map->Operation != MapOperationBusMasterWrite) { > Status = EFI_INVALID_PARAMETER; > - } else if (Map->Operation == MapOperationBusMasterWrite) { > - CopyMem ((VOID *)(UINTN)Map->HostAddress, Map->BufferAddress, > - Map->NumberOfBytes); > - } > + } else { > + Buffer = ALIGN_POINTER (Map->BufferAddress, mCpu->DmaBufferAlignment); > + > + mCpu->FlushDataCache (mCpu, (UINTN)Buffer, Map->NumberOfBytes, > + EfiCpuFlushTypeInvalidate); > > - DmaFreeBuffer (EFI_SIZE_TO_PAGES (Map->NumberOfBytes), Map->BufferAddress); > + CopyMem ((VOID *)(UINTN)Map->HostAddress, Buffer, Map->NumberOfBytes); > > + FreePool (Map->BufferAddress); > + } > } else { > if (Map->Operation == MapOperationBusMasterWrite) { > // > -- > 2.11.0 >