Section
7.7 Adding the Exit Boot Services feature
in
Driver Writer's Guide for UEFI 2.3.1 (v1.01)
writes,
> Examples from the EDK II that use this feature are the PCI device
> drivers for USB Host Controllers. Some USB Host Controllers are PCI Bus
> Masters that continuously access a memory buffer to poll for operation
> requests. Access to this memory buffer by a USB Host Controller may be
> required to boot an operation system, but this activity must be
> terminated when the OS calls ExitBootServices() . The typical action in
> the Exit Boot Services Event for these types of drivers is to disable
> the PCI bus master and place the USB Host Controller into a halted
> state.
XhcExitBootService() already halts the host controller, which causes the
host controller to forget about all common buffers configured previously.
However, if the system has a component that translates device addresses to
system memory addresses (such as an IOMMU), then the translations put in
place when the common buffers were originally mapped should also be
undone, before handing control to the OS.
XhciDxe maps three kinds of common buffers:
(1) The Scratchpad Buffer Array, and
(2) the Scratchpad Buffers pointed-to by elements of the Scratchpad Buffer
Array, are initially mapped in
XhcDriverBindingStart()
XhcInitSched()
UsbHcAllocateAlignedPages()
and they are unmapped and mapped again, for an unspecified number of
times, in
XhcReset()
XhcFreeSched()
UsbHcFreeAlignedPages()
XhcInitSched()
UsbHcAllocateAlignedPages()
(3) Memory blocks in the USB Host Controller memory pool are first added
in
XhcDriverBindingStart()
XhcInitSched()
UsbHcInitMemPool()
UsbHcAllocMemBlock()
and then for an unspecified number of times in
UsbHcAllocateMem()
UsbHcAllocMemBlock()
whenever the pool has to be extended. Memory blocks are also unmapped
and mapped again in
XhcReset()
XhcFreeSched()
UsbHcFreeMemPool()
UsbHcFreeMemBlock()
XhcInitSched()
UsbHcInitMemPool()
UsbHcAllocMemBlock()
All three kinds of common buffers are unmapped in the following call tree:
XhcDriverBindingStop()
XhcFreeSched()
UsbHcFreeAlignedPages() <-
UsbHcFreeMemPool()
UsbHcFreeMemBlock() <-
The common buffers should be unmapped at ExitBootServices() the same way.
In that context however, we must not free memory (because that could alter
the UEFI memory map, which is forbidden for ExitBootServices()
notification functions). So call PciIo->Unmap() without calling
PciIo->FreeBuffer().
Cc: Brijesh Singh <brijesh.singh@amd.com>
Cc: Eric Dong <eric.dong@intel.com>
Cc: Star Zeng <star.zeng@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
---
MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c | 31 ++++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c b/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c
index c884f4c3146c..d2a04492510f 100644
--- a/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c
+++ b/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c
@@ -1859,6 +1859,9 @@ XhcExitBootService (
{
USB_XHCI_INSTANCE *Xhc;
EFI_PCI_IO_PROTOCOL *PciIo;
+ UINT32 Index;
+ USBHC_MEM_POOL *MemPool;
+ USBHC_MEM_BLOCK *MemBlock;
Xhc = (USB_XHCI_INSTANCE*) Context;
PciIo = Xhc->PciIo;
@@ -1885,6 +1888,34 @@ XhcExitBootService (
Xhc->OriginalPciAttributes,
NULL
);
+
+ //
+ // Unmap Scratchpad Buffers and Scratchpad Buffer Array.
+ //
+ if (Xhc->MaxScratchpadBufs > 0) {
+ for (Index = 0; Index < Xhc->MaxScratchpadBufs; Index++) {
+ if ((VOID *)(UINTN)Xhc->ScratchEntry[Index] != NULL) {
+ PciIo->Unmap (PciIo, (VOID *)Xhc->ScratchEntryMap[Index]);
+ }
+ }
+ if (Xhc->ScratchBuf != NULL) {
+ PciIo->Unmap (PciIo, Xhc->ScratchMap);
+ }
+ }
+
+ //
+ // Unmap the memory pool.
+ //
+ MemPool = Xhc->MemPool;
+ if (MemPool != NULL) {
+ for (MemBlock = MemPool->Head;
+ MemBlock != NULL;
+ MemBlock = MemBlock->Next) {
+ if (MemBlock->BufHost != NULL) {
+ MemPool->PciIo->Unmap (MemPool->PciIo, MemBlock->Mapping);
+ }
+ }
+ }
}
/**
--
2.14.1.3.gb7cf6e02401b
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
© 2016 - 2024 Red Hat, Inc.