public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH v3] Fix Aligned Page Allocation For XHCI
@ 2019-10-11 18:55 Ashish Singhal
  2019-10-11 18:55 ` [PATCH v3] MdeModulePkg/XhciDxe: Fix Aligned Page Allocation Ashish Singhal
  0 siblings, 1 reply; 2+ messages in thread
From: Ashish Singhal @ 2019-10-11 18:55 UTC (permalink / raw)
  To: devel, hao.a.wu, ray.ni; +Cc: Ashish Singhal

This patch set is an attempt to fix the error where we allocate incorrectly
aligned memory for XHCI PDI and DXE. The change for DXE phase has been verified
already but change for PEI needs to be verified by Hao as I do not have a
setup to be able to verify that. The change made to fix PEI is taken from the
way it has been fixed in DXE. This avoids the need for adding any new function
and takes care of allocating and freeing extra memory in the parent function.

Ashish Singhal (1):
  MdeModulePkg/XhciDxe: Fix Aligned Page Allocation

 MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c |  2 +-
 MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c | 20 +++++++++++++++++++-
 2 files changed, 20 insertions(+), 2 deletions(-)

-- 
2.7.4


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH v3] MdeModulePkg/XhciDxe: Fix Aligned Page Allocation
  2019-10-11 18:55 [PATCH v3] Fix Aligned Page Allocation For XHCI Ashish Singhal
@ 2019-10-11 18:55 ` Ashish Singhal
  0 siblings, 0 replies; 2+ messages in thread
From: Ashish Singhal @ 2019-10-11 18:55 UTC (permalink / raw)
  To: devel, hao.a.wu, ray.ni; +Cc: Ashish Singhal

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 | 20 +++++++++++++++++++-
 2 files changed, 20 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..f95b5e5 100644
--- a/MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c
+++ b/MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c
@@ -567,6 +567,7 @@ UsbHcAllocateAlignedPages (
   EFI_PHYSICAL_ADDRESS  DeviceMemory;
   UINTN                 AlignedDeviceMemory;
   UINTN                 RealPages;
+  UINTN                 UnalignedPages;
 
   //
   // Alignment must be a power of two or zero.
@@ -593,7 +594,7 @@ UsbHcAllocateAlignedPages (
     ASSERT (RealPages > Pages);
 
     Status = IoMmuAllocateBuffer (
-               Pages,
+               RealPages,
                &Memory,
                &DeviceMemory,
                Mapping
@@ -603,6 +604,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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-10-11 18:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-10-11 18:55 [PATCH v3] Fix Aligned Page Allocation For XHCI Ashish Singhal
2019-10-11 18:55 ` [PATCH v3] MdeModulePkg/XhciDxe: Fix Aligned Page Allocation Ashish Singhal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox