public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Ashish Singhal" <ashishsingha@nvidia.com>
To: <devel@edk2.groups.io>, <hao.a.wu@intel.com>, <ray.ni@intel.com>
Cc: Ashish Singhal <ashishsingha@nvidia.com>
Subject: [PATCH v2] MdeModulePkg/XhciDxe: Fix Aligned Page Allocation
Date: Fri, 11 Oct 2019 12:07:03 -0600	[thread overview]
Message-ID: <1f0bc680ed44632b3aa2e961a711708355cbb890.1570816666.git.ashishsingha@nvidia.com> (raw)
In-Reply-To: <cover.1570816666.git.ashishsingha@nvidia.com>

While allocating pages aligned at an alignment higher than
4K, allocate memory taking into consideration the padding
required for that alignment. The calls to free pages takes
care of this already.

Signed-off-by: Ashish Singhal <ashishsingha@nvidia.com>
---
 MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c |  2 +-
 MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c | 19 ++++++++++++++++++-
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c b/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c
index fd79988..aa69c47 100644
--- a/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c
+++ b/MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c
@@ -656,7 +656,7 @@ UsbHcAllocateAlignedPages (
                       PciIo,
                       AllocateAnyPages,
                       EfiBootServicesData,
-                      Pages,
+                      RealPages,
                       &Memory,
                       0
                       );
diff --git a/MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c b/MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c
index 56c0b90..074beee 100644
--- a/MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c
+++ b/MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c
@@ -593,7 +593,7 @@ UsbHcAllocateAlignedPages (
     ASSERT (RealPages > Pages);
 
     Status = IoMmuAllocateBuffer (
-               Pages,
+               RealPages,
                &Memory,
                &DeviceMemory,
                Mapping
@@ -603,6 +603,23 @@ UsbHcAllocateAlignedPages (
     }
     AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;
     AlignedDeviceMemory = ((UINTN) DeviceMemory + AlignmentMask) & ~AlignmentMask;
+    UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);
+    if (UnalignedPages > 0) {
+      //
+      // Free first unaligned page(s).
+      //
+      Status = IoMmuFreeBuffer (UnalignedPages, Memory, Mapping);
+      ASSERT_EFI_ERROR (Status);
+    }
+    Memory = (VOID *)(UINTN)(AlignedMemory + EFI_PAGES_TO_SIZE (Pages));
+    UnalignedPages = RealPages - Pages - UnalignedPages;
+    if (UnalignedPages > 0) {
+      //
+      // Free last unaligned page(s).
+      //
+      Status = IoMmuFreeBuffer (UnalignedPages, Memory, Mapping);
+      ASSERT_EFI_ERROR (Status);
+    }
   } else {
     //
     // Do not over-allocate pages in this case.
-- 
2.7.4


      reply	other threads:[~2019-10-11 18:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-11 18:07 [PATCH v2] Fix Aligned Page Allocation For XHCI Ashish Singhal
2019-10-11 18:07 ` Ashish Singhal [this message]

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=1f0bc680ed44632b3aa2e961a711708355cbb890.1570816666.git.ashishsingha@nvidia.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