public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "John Chew" <yuinyee.chew@starfivetech.com>
To: <devel@edk2.groups.io>
Cc: John Chew <yuinyee.chew@starfivetech.com>,
	Sunil V L <sunilvl@ventanamicro.com>,
	Leif Lindholm <quic_llindhol@quicinc.com>,
	"Ard Biesheuvel" <ardb+tianocore@kernel.org>,
	Michael D Kinney <michael.d.kinney@intel.com>,
	Li Yong <yong.li@intel.com>
Subject: [edk2-devel] [PATCH v3 5/5] DesignWare/DwEmmcDxe: Force DMA buffer to allocate below 4GB
Date: Fri, 3 Nov 2023 10:51:31 +0800	[thread overview]
Message-ID: <20231103025131.1643-6-yuinyee.chew@starfivetech.com> (raw)
In-Reply-To: <20231103025131.1643-1-yuinyee.chew@starfivetech.com>

The buffer address passed into the read/write block function sometimes
larger than 4GB. This driver only support 32-bit DMA addressing.
MMC timeout will occur if DMA buffer is allocated in 64-bit address.

Cc: Sunil V L <sunilvl@ventanamicro.com>
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Li Yong <yong.li@intel.com>
Signed-off-by: John Chew <yuinyee.chew@starfivetech.com>
---
 Silicon/Synopsys/DesignWare/Drivers/DwEmmcDxe/DwEmmcDxe.c | 79 ++++++++++++++++----
 1 file changed, 64 insertions(+), 15 deletions(-)

diff --git a/Silicon/Synopsys/DesignWare/Drivers/DwEmmcDxe/DwEmmcDxe.c b/Silicon/Synopsys/DesignWare/Drivers/DwEmmcDxe/DwEmmcDxe.c
index 39e4d994fcd4..8233639b0ce8 100644
--- a/Silicon/Synopsys/DesignWare/Drivers/DwEmmcDxe/DwEmmcDxe.c
+++ b/Silicon/Synopsys/DesignWare/Drivers/DwEmmcDxe/DwEmmcDxe.c
@@ -601,27 +601,57 @@ DwEmmcWaitDmaComplete (
   return Status;
 }
 
+STATIC
+UINT32 *
+AllocateMemoryBelow4G (
+  IN UINTN  Size
+  )
+{
+  UINTN                 Pages;
+  EFI_PHYSICAL_ADDRESS  Address;
+  EFI_STATUS            Status;
+  UINT32                  *Buffer;
+
+  Pages   = EFI_SIZE_TO_PAGES (Size);
+  Address = 0xFFFFFFFF;
+
+  Status = gBS->AllocatePages (
+                  AllocateMaxAddress,
+                  EfiBootServicesData,
+                  Pages,
+                  &Address
+                  );
+  ASSERT_EFI_ERROR (Status);
+
+  Buffer = (UINT32 *)(UINTN)Address;
+  ZeroMem (Buffer, Size);
+
+  return Buffer;
+}
+
 EFI_STATUS
 DwEmmcReadBlockData (
-  IN EFI_MMC_HOST_PROTOCOL     *This,
-  IN EFI_LBA                    Lba,
-  IN UINTN                      Length,
-  IN UINT32*                   Buffer
+  IN EFI_MMC_HOST_PROTOCOL  *This,
+  IN EFI_LBA                Lba,
+  IN UINTN                  Length,
+  IN UINT32*                Buffer
   )
 {
   EFI_STATUS  Status;
   UINT32      DescPages, CountPerPage, Count;
   EFI_TPL     Tpl;
+  UINT32      *DmaBuf;
 
   Tpl = gBS->RaiseTPL (TPL_NOTIFY);
+  DmaBuf = AllocateMemoryBelow4G(Length);
 
   CountPerPage = EFI_PAGE_SIZE / 16;
   Count = (Length + DWEMMC_DMA_BUF_SIZE - 1) / DWEMMC_DMA_BUF_SIZE;
   DescPages = (Count + CountPerPage - 1) / CountPerPage;
 
-  InvalidateDataCacheRange (Buffer, Length);
+  InvalidateDataCacheRange (DmaBuf, Length);
 
-  Status = PrepareDmaData (gpIdmacDesc, Length, Buffer);
+  Status = PrepareDmaData (gpIdmacDesc, Length, DmaBuf);
   if (EFI_ERROR (Status)) {
     goto out;
   }
@@ -637,11 +667,14 @@ DwEmmcReadBlockData (
   Status = DwEmmcWaitDmaComplete(This, 1);
 
   if (DWMCI_SD_READ_MASK(mDwEmmcArgument) && (FixedPcdGetBool (PcdDwEmmcDxeCPULittleEndian))) {
-    Buffer[3] = SwapBytes32(Buffer[3]);
-    Buffer[4] = SwapBytes32(Buffer[4]);
+    DmaBuf[3] = SwapBytes32(DmaBuf[3]);
+    DmaBuf[4] = SwapBytes32(DmaBuf[4]);
   }
 
+  CopyMem(Buffer, DmaBuf, Length);
+
 out:
+  FreePages (DmaBuf, EFI_SIZE_TO_PAGES(Length));
   // Restore Tpl
   gBS->RestoreTPL (Tpl);
   return Status;
@@ -649,25 +682,29 @@ out:
 
 EFI_STATUS
 DwEmmcWriteBlockData (
-  IN EFI_MMC_HOST_PROTOCOL     *This,
-  IN EFI_LBA                    Lba,
-  IN UINTN                      Length,
-  IN UINT32*                    Buffer
+  IN EFI_MMC_HOST_PROTOCOL  *This,
+  IN EFI_LBA                Lba,
+  IN UINTN                  Length,
+  IN UINT32*                Buffer
   )
 {
   EFI_STATUS  Status;
   UINT32      DescPages, CountPerPage, Count;
   EFI_TPL     Tpl;
+  UINT32      *DmaBuf;
 
   Tpl = gBS->RaiseTPL (TPL_NOTIFY);
 
+  DmaBuf = AllocateMemoryBelow4G(Length);
+  CopyMem(DmaBuf, Buffer, Length);
+
   CountPerPage = EFI_PAGE_SIZE / 16;
   Count = (Length + DWEMMC_DMA_BUF_SIZE - 1) / DWEMMC_DMA_BUF_SIZE;
   DescPages = (Count + CountPerPage - 1) / CountPerPage;
 
-  WriteBackDataCacheRange (Buffer, Length);
+  WriteBackDataCacheRange (DmaBuf, Length);
 
-  Status = PrepareDmaData (gpIdmacDesc, Length, Buffer);
+  Status = PrepareDmaData (gpIdmacDesc, Length, DmaBuf);
   if (EFI_ERROR (Status)) {
     goto out;
   }
@@ -683,6 +720,7 @@ DwEmmcWriteBlockData (
   Status = DwEmmcWaitDmaComplete(This, 0);
 
 out:
+  FreePages (DmaBuf, EFI_SIZE_TO_PAGES(Length));
   // Restore Tpl
   gBS->RestoreTPL (Tpl);
   return Status;
@@ -772,6 +810,7 @@ DwEmmcDxeInitialize (
 {
   EFI_STATUS    Status;
   EFI_HANDLE    Handle;
+  EFI_PHYSICAL_ADDRESS  Address;
 
   if (!FixedPcdGetBool (PcdDwPermitObsoleteDrivers)) {
     ASSERT (FALSE);
@@ -781,7 +820,17 @@ DwEmmcDxeInitialize (
   Handle = NULL;
 
   DwEmmcAdjustFifoThreshold ();
-  gpIdmacDesc = (DWEMMC_IDMAC_DESCRIPTOR *)AllocatePages (DWEMMC_MAX_DESC_PAGES);
+
+  Address = 0xffffffff;
+  Status = gBS->AllocatePages (
+                  AllocateMaxAddress,
+                  EfiBootServicesData,
+                  DWEMMC_MAX_DESC_PAGES,
+                  &Address
+                  );
+  gpIdmacDesc = (DWEMMC_IDMAC_DESCRIPTOR *)(UINTN)Address;
+  ZeroMem (gpIdmacDesc, DWEMMC_MAX_DESC_PAGES);
+
   if (gpIdmacDesc == NULL) {
     return EFI_BUFFER_TOO_SMALL;
   }
-- 
2.34.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#110600): https://edk2.groups.io/g/devel/message/110600
Mute This Topic: https://groups.io/mt/102357283/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



  parent reply	other threads:[~2023-11-03  2:53 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-03  2:51 [edk2-devel] [PATCH v3 0/5] Designware MMCDXE changes and enhancement John Chew
2023-11-03  2:51 ` [edk2-devel] [PATCH v3 1/5] DesignWare/DwEmmcDxe: Enabled Internal IDMAC interrupt RX/TX register John Chew
2023-11-22 15:41   ` Ard Biesheuvel
2023-11-27  7:10     ` John Chew
2023-11-03  2:51 ` [edk2-devel] [PATCH v3 2/5] DesignWare/DwEmmcDxe: Add CPU little endian option John Chew
2023-11-22 15:45   ` Ard Biesheuvel
2023-11-27  7:09     ` John Chew
2023-11-03  2:51 ` [edk2-devel] [PATCH v3 3/5] DesignWare/DwEmmcDxe: Remove ARM dependency library John Chew
2023-11-22 15:46   ` Ard Biesheuvel
2023-11-03  2:51 ` [edk2-devel] [PATCH v3 4/5] DesignWare/DwEmmcDxe: Add handling for SDMMC John Chew
2023-11-22 15:47   ` Ard Biesheuvel
2023-11-03  2:51 ` John Chew [this message]
2023-11-22 15:49   ` [edk2-devel] [PATCH v3 5/5] DesignWare/DwEmmcDxe: Force DMA buffer to allocate below 4GB Ard Biesheuvel
2023-11-07  1:09 ` [edk2-devel] [PATCH v3 0/5] Designware MMCDXE changes and enhancement John Chew
2023-11-22  4:06 ` John Chew

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231103025131.1643-6-yuinyee.chew@starfivetech.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox