2 Debug Port Library implementation based on usb3 debug port.
4 Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Library/PeiServicesLib.h>
11 #include <Library/HobLib.h>
12 #include <Ppi/MemoryDiscovered.h>
13 #include <Ppi/IoMmu.h>
14 #include "DebugCommunicationLibUsb3Internal.h"
16 GUID gUsb3DbgGuid
= USB3_DBG_GUID
;
19 USB3 IOMMU PPI notify.
21 @param[in] PeiServices Pointer to PEI Services Table.
22 @param[in] NotifyDesc Pointer to the descriptor for the Notification event that
23 caused this function to execute.
24 @param[in] Ppi Pointer to the PPI data associated with this function.
26 @retval EFI_STATUS Always return EFI_SUCCESS
31 IN EFI_PEI_SERVICES
**PeiServices
,
32 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDesc
,
36 USB3_DEBUG_PORT_HANDLE
*Instance
;
38 DEBUG ((DEBUG_INFO
, "%a()\n", __FUNCTION__
));
40 Instance
= GetUsb3DebugPortInstance ();
41 ASSERT (Instance
!= NULL
);
42 if (!Instance
->Ready
) {
46 Instance
->InNotify
= TRUE
;
49 // Reinitialize USB3 debug port with granted DMA buffer from IOMMU PPI.
51 InitializeUsbDebugHardware (Instance
);
54 // Wait some time for host to be ready after re-initialization.
56 MicroSecondDelay (1000000);
58 Instance
->InNotify
= FALSE
;
63 EFI_PEI_NOTIFY_DESCRIPTOR mUsb3IoMmuPpiNotifyDesc
= {
64 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
70 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
71 OperationBusMasterCommonBuffer64 mapping.
73 @param IoMmu Pointer to IOMMU PPI.
74 @param Pages The number of pages to allocate.
75 @param HostAddress A pointer to store the base system memory address of the
77 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
78 access the hosts HostAddress.
79 @param Mapping A resulting value to pass to Unmap().
81 @retval EFI_SUCCESS The requested memory pages were allocated.
82 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
83 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
84 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
85 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
90 IN EDKII_IOMMU_PPI
*IoMmu
,
92 OUT VOID
**HostAddress
,
93 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
104 Status
= IoMmu
->AllocateBuffer (
106 EfiRuntimeServicesData
,
111 if (EFI_ERROR (Status
)) {
112 return EFI_OUT_OF_RESOURCES
;
115 NumberOfBytes
= EFI_PAGES_TO_SIZE (Pages
);
116 Status
= IoMmu
->Map (
118 EdkiiIoMmuOperationBusMasterCommonBuffer
,
124 if (EFI_ERROR (Status
)) {
125 IoMmu
->FreeBuffer (IoMmu
, Pages
, *HostAddress
);
127 return EFI_OUT_OF_RESOURCES
;
130 Status
= IoMmu
->SetAttribute (
133 EDKII_IOMMU_ACCESS_READ
| EDKII_IOMMU_ACCESS_WRITE
135 if (EFI_ERROR (Status
)) {
136 IoMmu
->Unmap (IoMmu
, *Mapping
);
137 IoMmu
->FreeBuffer (IoMmu
, Pages
, *HostAddress
);
149 @return Pointer to IOMMU PPI.
158 EDKII_IOMMU_PPI
*IoMmu
;
161 Status
= PeiServicesLocatePpi (
167 if (!EFI_ERROR (Status
) && (IoMmu
!= NULL
)) {
175 Return USB3 debug instance address pointer.
178 EFI_PHYSICAL_ADDRESS
*
179 GetUsb3DebugPortInstanceAddrPtr (
183 USB3_DEBUG_PORT_HANDLE
*Instance
;
184 EFI_PHYSICAL_ADDRESS
*AddrPtr
;
185 EFI_PEI_HOB_POINTERS Hob
;
188 Hob
.Raw
= GetFirstGuidHob (&gUsb3DbgGuid
);
189 if (Hob
.Raw
== NULL
) {
191 // Build HOB for the local instance and the buffer to save instance address pointer.
192 // Use the local instance in HOB temporarily.
194 AddrPtr
= BuildGuidHob (
196 sizeof (EFI_PHYSICAL_ADDRESS
) + sizeof (USB3_DEBUG_PORT_HANDLE
)
198 ASSERT (AddrPtr
!= NULL
);
199 ZeroMem (AddrPtr
, sizeof (EFI_PHYSICAL_ADDRESS
) + sizeof (USB3_DEBUG_PORT_HANDLE
));
200 Instance
= (USB3_DEBUG_PORT_HANDLE
*)(AddrPtr
+ 1);
201 *AddrPtr
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)Instance
;
202 Instance
->FromHob
= TRUE
;
203 Instance
->Initialized
= USB3DBG_UNINITIALIZED
;
204 if (Usb3GetIoMmu () == NULL
) {
205 Status
= PeiServicesNotifyPpi (&mUsb3IoMmuPpiNotifyDesc
);
206 ASSERT_EFI_ERROR (Status
);
209 AddrPtr
= GET_GUID_HOB_DATA (Hob
.Guid
);
216 Allocate aligned memory for XHC's usage.
218 @param BufferSize The size, in bytes, of the Buffer.
220 @return A pointer to the allocated buffer or NULL if allocation fails.
224 AllocateAlignBuffer (
229 EFI_PHYSICAL_ADDRESS Address
;
231 VOID
*MemoryDiscoveredPpi
;
232 EDKII_IOMMU_PPI
*IoMmu
;
239 // Make sure the allocated memory is physical memory.
241 Status
= PeiServicesLocatePpi (
242 &gEfiPeiMemoryDiscoveredPpiGuid
,
245 (VOID
**)&MemoryDiscoveredPpi
247 if (!EFI_ERROR (Status
)) {
248 IoMmu
= Usb3GetIoMmu ();
250 Status
= IoMmuAllocateBuffer (
252 EFI_SIZE_TO_PAGES (BufferSize
),
257 if (!EFI_ERROR (Status
)) {
258 ASSERT (Address
== ((EFI_PHYSICAL_ADDRESS
)(UINTN
)HostAddress
));
259 Buf
= (VOID
*)(UINTN
)Address
;
262 Status
= PeiServicesAllocatePages (
264 EFI_SIZE_TO_PAGES (BufferSize
),
267 if (!EFI_ERROR (Status
)) {
268 Buf
= (VOID
*)(UINTN
)Address
;