2 Debug Port Library implementation based on usb3 debug port.
4 Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <Library/PeiServicesLib.h>
17 #include <Library/HobLib.h>
18 #include <Ppi/MemoryDiscovered.h>
19 #include <Ppi/IoMmu.h>
20 #include "DebugCommunicationLibUsb3Internal.h"
22 GUID gUsb3DbgGuid
= USB3_DBG_GUID
;
25 USB3 IOMMU PPI notify.
27 @param[in] PeiServices Pointer to PEI Services Table.
28 @param[in] NotifyDesc Pointer to the descriptor for the Notification event that
29 caused this function to execute.
30 @param[in] Ppi Pointer to the PPI data associated with this function.
32 @retval EFI_STATUS Always return EFI_SUCCESS
37 IN EFI_PEI_SERVICES
**PeiServices
,
38 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDesc
,
42 USB3_DEBUG_PORT_HANDLE
*Instance
;
44 DEBUG ((DEBUG_INFO
, "%a()\n", __FUNCTION__
));
46 Instance
= GetUsb3DebugPortInstance ();
47 ASSERT (Instance
!= NULL
);
49 Instance
->InNotify
= TRUE
;
52 // Reinitialize USB3 debug port with granted DMA buffer from IOMMU PPI.
54 InitializeUsbDebugHardware (Instance
);
57 // Wait some time for host to be ready after re-initialization.
59 MicroSecondDelay (1000000);
61 Instance
->InNotify
= FALSE
;
66 EFI_PEI_NOTIFY_DESCRIPTOR mUsb3IoMmuPpiNotifyDesc
= {
67 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
73 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
74 OperationBusMasterCommonBuffer64 mapping.
76 @param IoMmu Pointer to IOMMU PPI.
77 @param Pages The number of pages to allocate.
78 @param HostAddress A pointer to store the base system memory address of the
80 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
81 access the hosts HostAddress.
82 @param Mapping A resulting value to pass to Unmap().
84 @retval EFI_SUCCESS The requested memory pages were allocated.
85 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
86 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
87 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
88 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
93 IN EDKII_IOMMU_PPI
*IoMmu
,
95 OUT VOID
**HostAddress
,
96 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
107 Status
= IoMmu
->AllocateBuffer (
114 if (EFI_ERROR (Status
)) {
115 return EFI_OUT_OF_RESOURCES
;
118 NumberOfBytes
= EFI_PAGES_TO_SIZE (Pages
);
119 Status
= IoMmu
->Map (
121 EdkiiIoMmuOperationBusMasterCommonBuffer
,
127 if (EFI_ERROR (Status
)) {
128 IoMmu
->FreeBuffer (IoMmu
, Pages
, *HostAddress
);
130 return EFI_OUT_OF_RESOURCES
;
132 Status
= IoMmu
->SetAttribute (
135 EDKII_IOMMU_ACCESS_READ
| EDKII_IOMMU_ACCESS_WRITE
137 if (EFI_ERROR (Status
)) {
138 IoMmu
->Unmap (IoMmu
, *Mapping
);
139 IoMmu
->FreeBuffer (IoMmu
, Pages
, *HostAddress
);
151 @return Pointer to IOMMU PPI.
160 EDKII_IOMMU_PPI
*IoMmu
;
163 Status
= PeiServicesLocatePpi (
169 if (!EFI_ERROR (Status
) && (IoMmu
!= NULL
)) {
177 Return USB3 debug instance address.
180 USB3_DEBUG_PORT_HANDLE
*
181 GetUsb3DebugPortInstance (
185 USB3_DEBUG_PORT_HANDLE
*Instance
;
186 EFI_PEI_HOB_POINTERS Hob
;
189 Hob
.Raw
= GetFirstGuidHob (&gUsb3DbgGuid
);
190 if (Hob
.Raw
== NULL
) {
192 // Save Instance into HOB
194 Instance
= BuildGuidHob (
196 sizeof (USB3_DEBUG_PORT_HANDLE
)
198 ASSERT (Instance
!= NULL
);
199 ZeroMem (Instance
, sizeof (USB3_DEBUG_PORT_HANDLE
));
201 Instance
->FromHob
= TRUE
;
202 Instance
->Initialized
= USB3DBG_UNINITIALIZED
;
204 Instance
= GET_GUID_HOB_DATA (Hob
.Guid
);
207 if (!Instance
->InNotify
) {
208 DiscoverInitializeUsbDebugPort (Instance
);
211 if (Instance
->Ready
&&
212 !Instance
->PpiNotifyRegistered
&&
213 (Usb3GetIoMmu () == NULL
)) {
214 Status
= PeiServicesNotifyPpi (&mUsb3IoMmuPpiNotifyDesc
);
215 ASSERT_EFI_ERROR (Status
);
216 Instance
->PpiNotifyRegistered
= TRUE
;
223 Allocate aligned memory for XHC's usage.
225 @param BufferSize The size, in bytes, of the Buffer.
227 @return A pointer to the allocated buffer or NULL if allocation fails.
231 AllocateAlignBuffer (
236 EFI_PHYSICAL_ADDRESS Address
;
238 VOID
*MemoryDiscoveredPpi
;
239 EDKII_IOMMU_PPI
*IoMmu
;
246 // Make sure the allocated memory is physical memory.
248 Status
= PeiServicesLocatePpi (
249 &gEfiPeiMemoryDiscoveredPpiGuid
,
252 (VOID
**) &MemoryDiscoveredPpi
254 if (!EFI_ERROR (Status
)) {
255 IoMmu
= Usb3GetIoMmu ();
257 Status
= IoMmuAllocateBuffer (
259 EFI_SIZE_TO_PAGES (BufferSize
),
264 if (!EFI_ERROR (Status
)) {
265 ASSERT (Address
== ((EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
));
266 Buf
= (VOID
*)(UINTN
) Address
;
269 Status
= PeiServicesAllocatePages (
271 EFI_SIZE_TO_PAGES (BufferSize
),
274 if (!EFI_ERROR (Status
)) {
275 Buf
= (VOID
*)(UINTN
) Address
;