From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from hqemgate15.nvidia.com (hqemgate15.nvidia.com [216.228.121.64]) by mx.groups.io with SMTP id smtpd.web11.342.1571160052380780205 for ; Tue, 15 Oct 2019 10:20:52 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@nvidia.com header.s=n1 header.b=ogGJxxgo; spf=pass (domain: nvidia.com, ip: 216.228.121.64, mailfrom: ashishsingha@nvidia.com) Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqemgate15.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Tue, 15 Oct 2019 10:21:03 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Tue, 15 Oct 2019 10:20:52 -0700 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Tue, 15 Oct 2019 10:20:52 -0700 Received: from HQMAIL109.nvidia.com (172.20.187.15) by HQMAIL105.nvidia.com (172.20.187.12) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 15 Oct 2019 17:20:51 +0000 Received: from HQMAIL107.nvidia.com (172.20.187.13) by HQMAIL109.nvidia.com (172.20.187.15) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 15 Oct 2019 17:20:51 +0000 Received: from hqnvemgw01.nvidia.com (172.20.150.20) by HQMAIL107.nvidia.com (172.20.187.13) with Microsoft SMTP Server (TLS) id 15.0.1473.3 via Frontend Transport; Tue, 15 Oct 2019 17:20:51 +0000 Received: from ashishsingha-lnx.nvidia.com (Not Verified[10.28.48.147]) by hqnvemgw01.nvidia.com with Trustwave SEG (v7,5,8,10121) id ; Tue, 15 Oct 2019 10:20:51 -0700 From: "Ashish Singhal" To: , , , CC: Ashish Singhal Subject: [PATCH v4 2/2] MdeModulePkg/XhciPei: Fix Aligned Page Allocation Date: Tue, 15 Oct 2019 11:20:47 -0600 Message-ID: X-Mailer: git-send-email 2.7.4 In-Reply-To: References: X-NVConfidentiality: public Return-Path: ashishsingha@nvidia.com MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1571160063; bh=QTSqJh1m+pkSH+ptzEZDFRDCuN58YpC8DXjmQVPLmlk=; h=X-PGP-Universal:From:To:CC:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:X-NVConfidentiality:MIME-Version: Content-Type; b=ogGJxxgocqk5unQMBZtiYTwiNseT5U1/+lU8fKkgEbnknlaRswIcyxzU67pSi8yiK sL/WSl3WR8H3Ewg++z5qpb5OwoOZgrKGT5rPFI33mKbBWeeQIJ8vRdoNlkiJ4nhq2H TUNPBU2E7f7rn+RR9YS340d2AnpzrF4GW3pWrGes8rfLfiIzhIN5MO/XNbMsTtKLJg 8+dfs7jKgLL6pr+SdpOsWEvDlRkn6M7mNO7wmNqkOrjj+uf9z5dj5NQXCMk+fxhWJS 0RJxi0/63XVbpAPpT0Q9w2RqX0AvGisSqkKhhjw03hX9cmvGLeXbWQw61kNXDaexHG fddlbmBYIXX+Q== Content-Type: text/plain Add support for allocating aligned pages at an alignment higher than 4K. The new function allocated memory taking into account the padding required and then frees up unused pages before mapping it. Change-Id: I32d36bfbff0eee93d14a9a1a86e5ce7635251b64 Signed-off-by: Ashish Singhal --- MdeModulePkg/Bus/Pci/XhciPei/DmaMem.c | 128 ++++++++++++++++++++++++++++++++ MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c | 25 +------ MdeModulePkg/Bus/Pci/XhciPei/XhcPeim.h | 28 +++++++ 3 files changed, 160 insertions(+), 21 deletions(-) diff --git a/MdeModulePkg/Bus/Pci/XhciPei/DmaMem.c b/MdeModulePkg/Bus/Pci/XhciPei/DmaMem.c index 71d6113..c4d93ae 100644 --- a/MdeModulePkg/Bus/Pci/XhciPei/DmaMem.c +++ b/MdeModulePkg/Bus/Pci/XhciPei/DmaMem.c @@ -225,6 +225,134 @@ IoMmuFreeBuffer ( } /** + Allocates aligned pages that are suitable for an OperationBusMasterCommonBuffer or + OperationBusMasterCommonBuffer64 mapping. + + @param Pages The number of pages to allocate. + @param Alignment The requested alignment of the allocation. Must be a power of two. + @param HostAddress A pointer to store the base system memory address of the + allocated range. + @param DeviceAddress The resulting map address for the bus master PCI controller to use to + access the hosts HostAddress. + @param Mapping A resulting value to pass to Unmap(). + + @retval EFI_SUCCESS The requested memory pages were allocated. + @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are + MEMORY_WRITE_COMBINE and MEMORY_CACHED. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated. + +**/ +EFI_STATUS +IoMmuAllocateAlignedBuffer ( + IN UINTN Pages, + IN UINTN Alignment, + OUT VOID **HostAddress, + OUT EFI_PHYSICAL_ADDRESS *DeviceAddress, + OUT VOID **Mapping + ) +{ + EFI_STATUS Status; + VOID *Memory; + UINTN AlignedMemory; + UINTN AlignmentMask; + UINTN UnalignedPages; + UINTN RealPages; + UINTN NumberOfBytes; + EFI_PHYSICAL_ADDRESS HostPhyAddress; + + *HostAddress = NULL; + *DeviceAddress = 0; + AlignmentMask = Alignment - 1; + + // + // Calculate the total number of pages since alignment is larger than page size. + // + RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment); + + // + // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow. + // + ASSERT (RealPages > Pages); + + if (mIoMmu != NULL) { + Status = mIoMmu->AllocateBuffer ( + mIoMmu, + EfiBootServicesData, + RealPages, + HostAddress, + 0 + ); + if (EFI_ERROR (Status)) { + return EFI_OUT_OF_RESOURCES; + } + Memory = *HostAddress; + AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask; + UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory); + if (UnalignedPages > 0) { + // + // Free first unaligned page(s). + // + Status = mIoMmu->FreeBuffer ( + mIoMmu, + UnalignedPages, + Memory); + if (EFI_ERROR (Status)) { + return Status; + } + } + Memory = (VOID *)(UINTN)(AlignedMemory + EFI_PAGES_TO_SIZE (Pages)); + UnalignedPages = RealPages - Pages - UnalignedPages; + if (UnalignedPages > 0) { + // + // Free last unaligned page(s). + // + Status = mIoMmu->FreeBuffer ( + mIoMmu, + UnalignedPages, + Memory); + if (EFI_ERROR (Status)) { + return Status; + } + } + *HostAddress = (VOID *) AlignedMemory; + NumberOfBytes = EFI_PAGES_TO_SIZE(Pages); + Status = mIoMmu->Map ( + mIoMmu, + EdkiiIoMmuOperationBusMasterCommonBuffer, + *HostAddress, + &NumberOfBytes, + DeviceAddress, + Mapping + ); + if (EFI_ERROR (Status)) { + return EFI_OUT_OF_RESOURCES; + } + Status = mIoMmu->SetAttribute ( + mIoMmu, + *Mapping, + EDKII_IOMMU_ACCESS_READ | EDKII_IOMMU_ACCESS_WRITE + ); + if (EFI_ERROR (Status)) { + return Status; + } + } else { + Status = PeiServicesAllocatePages ( + EfiBootServicesData, + RealPages, + &HostPhyAddress + ); + if (EFI_ERROR (Status)) { + return EFI_OUT_OF_RESOURCES; + } + *HostAddress = (VOID *)(((UINTN) HostPhyAddress + AlignmentMask) & ~AlignmentMask); + *DeviceAddress = ((UINTN) HostPhyAddress + AlignmentMask) & ~AlignmentMask; + *Mapping = NULL; + } + return Status; +} + +/** Initialize IOMMU. **/ VOID diff --git a/MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c b/MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c index 56c0b90..01f42285 100644 --- a/MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c +++ b/MdeModulePkg/Bus/Pci/XhciPei/UsbHcMem.c @@ -562,11 +562,7 @@ UsbHcAllocateAlignedPages ( { EFI_STATUS Status; VOID *Memory; - UINTN AlignedMemory; - UINTN AlignmentMask; EFI_PHYSICAL_ADDRESS DeviceMemory; - UINTN AlignedDeviceMemory; - UINTN RealPages; // // Alignment must be a power of two or zero. @@ -582,18 +578,9 @@ UsbHcAllocateAlignedPages ( } if (Alignment > EFI_PAGE_SIZE) { - // - // Calculate the total number of pages since alignment is larger than page size. - // - AlignmentMask = Alignment - 1; - RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment); - // - // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow. - // - ASSERT (RealPages > Pages); - - Status = IoMmuAllocateBuffer ( + Status = IoMmuAllocateAlignedBuffer ( Pages, + Alignment, &Memory, &DeviceMemory, Mapping @@ -601,8 +588,6 @@ UsbHcAllocateAlignedPages ( if (EFI_ERROR (Status)) { return EFI_OUT_OF_RESOURCES; } - AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask; - AlignedDeviceMemory = ((UINTN) DeviceMemory + AlignmentMask) & ~AlignmentMask; } else { // // Do not over-allocate pages in this case. @@ -616,12 +601,10 @@ UsbHcAllocateAlignedPages ( if (EFI_ERROR (Status)) { return EFI_OUT_OF_RESOURCES; } - AlignedMemory = (UINTN) Memory; - AlignedDeviceMemory = (UINTN) DeviceMemory; } - *HostAddress = (VOID *) AlignedMemory; - *DeviceAddress = (EFI_PHYSICAL_ADDRESS) AlignedDeviceMemory; + *HostAddress = Memory; + *DeviceAddress = DeviceMemory; return EFI_SUCCESS; } diff --git a/MdeModulePkg/Bus/Pci/XhciPei/XhcPeim.h b/MdeModulePkg/Bus/Pci/XhciPei/XhcPeim.h index 480e13b..03a55f3 100644 --- a/MdeModulePkg/Bus/Pci/XhciPei/XhcPeim.h +++ b/MdeModulePkg/Bus/Pci/XhciPei/XhcPeim.h @@ -342,4 +342,32 @@ IoMmuFreeBuffer ( IN VOID *Mapping ); +/** + Allocates aligned pages that are suitable for an OperationBusMasterCommonBuffer or + OperationBusMasterCommonBuffer64 mapping. + + @param Pages The number of pages to allocate. + @param Alignment The requested alignment of the allocation. Must be a power of two. + @param HostAddress A pointer to store the base system memory address of the + allocated range. + @param DeviceAddress The resulting map address for the bus master PCI controller to use to + access the hosts HostAddress. + @param Mapping A resulting value to pass to Unmap(). + + @retval EFI_SUCCESS The requested memory pages were allocated. + @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are + MEMORY_WRITE_COMBINE and MEMORY_CACHED. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated. + +**/ +EFI_STATUS +IoMmuAllocateAlignedBuffer ( + IN UINTN Pages, + IN UINTN Alignment, + OUT VOID **HostAddress, + OUT EFI_PHYSICAL_ADDRESS *DeviceAddress, + OUT VOID **Mapping + ); + #endif -- 2.7.4