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
);
48 if (!Instance
->Ready
) {
52 Instance
->InNotify
= TRUE
;
55 // Reinitialize USB3 debug port with granted DMA buffer from IOMMU PPI.
57 InitializeUsbDebugHardware (Instance
);
60 // Wait some time for host to be ready after re-initialization.
62 MicroSecondDelay (1000000);
64 Instance
->InNotify
= FALSE
;
69 EFI_PEI_NOTIFY_DESCRIPTOR mUsb3IoMmuPpiNotifyDesc
= {
70 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
),
76 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
77 OperationBusMasterCommonBuffer64 mapping.
79 @param IoMmu Pointer to IOMMU PPI.
80 @param Pages The number of pages to allocate.
81 @param HostAddress A pointer to store the base system memory address of the
83 @param DeviceAddress The resulting map address for the bus master PCI controller to use to
84 access the hosts HostAddress.
85 @param Mapping A resulting value to pass to Unmap().
87 @retval EFI_SUCCESS The requested memory pages were allocated.
88 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
89 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
90 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
91 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
96 IN EDKII_IOMMU_PPI
*IoMmu
,
98 OUT VOID
**HostAddress
,
99 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
110 Status
= IoMmu
->AllocateBuffer (
112 EfiRuntimeServicesData
,
117 if (EFI_ERROR (Status
)) {
118 return EFI_OUT_OF_RESOURCES
;
121 NumberOfBytes
= EFI_PAGES_TO_SIZE (Pages
);
122 Status
= IoMmu
->Map (
124 EdkiiIoMmuOperationBusMasterCommonBuffer
,
130 if (EFI_ERROR (Status
)) {
131 IoMmu
->FreeBuffer (IoMmu
, Pages
, *HostAddress
);
133 return EFI_OUT_OF_RESOURCES
;
135 Status
= IoMmu
->SetAttribute (
138 EDKII_IOMMU_ACCESS_READ
| EDKII_IOMMU_ACCESS_WRITE
140 if (EFI_ERROR (Status
)) {
141 IoMmu
->Unmap (IoMmu
, *Mapping
);
142 IoMmu
->FreeBuffer (IoMmu
, Pages
, *HostAddress
);
154 @return Pointer to IOMMU PPI.
163 EDKII_IOMMU_PPI
*IoMmu
;
166 Status
= PeiServicesLocatePpi (
172 if (!EFI_ERROR (Status
) && (IoMmu
!= NULL
)) {
180 Return USB3 debug instance address pointer.
183 EFI_PHYSICAL_ADDRESS
*
184 GetUsb3DebugPortInstanceAddrPtr (
188 USB3_DEBUG_PORT_HANDLE
*Instance
;
189 EFI_PHYSICAL_ADDRESS
*AddrPtr
;
190 EFI_PEI_HOB_POINTERS Hob
;
193 Hob
.Raw
= GetFirstGuidHob (&gUsb3DbgGuid
);
194 if (Hob
.Raw
== NULL
) {
196 // Build HOB for the local instance and the buffer to save instance address pointer.
197 // Use the local instance in HOB temporarily.
199 AddrPtr
= BuildGuidHob (
201 sizeof (EFI_PHYSICAL_ADDRESS
) + sizeof (USB3_DEBUG_PORT_HANDLE
)
203 ASSERT (AddrPtr
!= NULL
);
204 ZeroMem (AddrPtr
, sizeof (EFI_PHYSICAL_ADDRESS
) + sizeof (USB3_DEBUG_PORT_HANDLE
));
205 Instance
= (USB3_DEBUG_PORT_HANDLE
*) (AddrPtr
+ 1);
206 *AddrPtr
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Instance
;
207 Instance
->FromHob
= TRUE
;
208 Instance
->Initialized
= USB3DBG_UNINITIALIZED
;
209 if (Usb3GetIoMmu () == NULL
) {
210 Status
= PeiServicesNotifyPpi (&mUsb3IoMmuPpiNotifyDesc
);
211 ASSERT_EFI_ERROR (Status
);
214 AddrPtr
= GET_GUID_HOB_DATA (Hob
.Guid
);
221 Allocate aligned memory for XHC's usage.
223 @param BufferSize The size, in bytes, of the Buffer.
225 @return A pointer to the allocated buffer or NULL if allocation fails.
229 AllocateAlignBuffer (
234 EFI_PHYSICAL_ADDRESS Address
;
236 VOID
*MemoryDiscoveredPpi
;
237 EDKII_IOMMU_PPI
*IoMmu
;
244 // Make sure the allocated memory is physical memory.
246 Status
= PeiServicesLocatePpi (
247 &gEfiPeiMemoryDiscoveredPpiGuid
,
250 (VOID
**) &MemoryDiscoveredPpi
252 if (!EFI_ERROR (Status
)) {
253 IoMmu
= Usb3GetIoMmu ();
255 Status
= IoMmuAllocateBuffer (
257 EFI_SIZE_TO_PAGES (BufferSize
),
262 if (!EFI_ERROR (Status
)) {
263 ASSERT (Address
== ((EFI_PHYSICAL_ADDRESS
) (UINTN
) HostAddress
));
264 Buf
= (VOID
*)(UINTN
) Address
;
267 Status
= PeiServicesAllocatePages (
269 EFI_SIZE_TO_PAGES (BufferSize
),
272 if (!EFI_ERROR (Status
)) {
273 Buf
= (VOID
*)(UINTN
) Address
;