TianoCore EDK2 master
Loading...
Searching...
No Matches
DebugCommunicationLibUsb3Pei.c
Go to the documentation of this file.
1
9#include <PiPei.h>
11#include <Library/HobLib.h>
13#include <Ppi/IoMmu.h>
15
16GUID gUsb3DbgGuid = USB3_DBG_GUID;
17
29EFIAPI
31 IN EFI_PEI_SERVICES **PeiServices,
32 IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc,
33 IN VOID *Ppi
34 )
35{
36 USB3_DEBUG_PORT_HANDLE *Instance;
37
38 DEBUG ((DEBUG_INFO, "%a()\n", __func__));
39
40 Instance = GetUsb3DebugPortInstance ();
41 ASSERT (Instance != NULL);
42 if (!Instance->Ready) {
43 return EFI_SUCCESS;
44 }
45
46 Instance->InNotify = TRUE;
47
48 //
49 // Reinitialize USB3 debug port with granted DMA buffer from IOMMU PPI.
50 //
52
53 //
54 // Wait some time for host to be ready after re-initialization.
55 //
56 MicroSecondDelay (1000000);
57
58 Instance->InNotify = FALSE;
59
60 return EFI_SUCCESS;
61}
62
63EFI_PEI_NOTIFY_DESCRIPTOR mUsb3IoMmuPpiNotifyDesc = {
64 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
67};
68
90 IN EDKII_IOMMU_PPI *IoMmu,
91 IN UINTN Pages,
92 OUT VOID **HostAddress,
93 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
94 OUT VOID **Mapping
95 )
96{
97 EFI_STATUS Status;
98 UINTN NumberOfBytes;
99
100 *HostAddress = NULL;
101 *DeviceAddress = 0;
102 *Mapping = NULL;
103
104 Status = IoMmu->AllocateBuffer (
105 IoMmu,
107 Pages,
108 HostAddress,
109 0
110 );
111 if (EFI_ERROR (Status)) {
112 return EFI_OUT_OF_RESOURCES;
113 }
114
115 NumberOfBytes = EFI_PAGES_TO_SIZE (Pages);
116 Status = IoMmu->Map (
117 IoMmu,
119 *HostAddress,
120 &NumberOfBytes,
121 DeviceAddress,
122 Mapping
123 );
124 if (EFI_ERROR (Status)) {
125 IoMmu->FreeBuffer (IoMmu, Pages, *HostAddress);
126 *HostAddress = NULL;
127 return EFI_OUT_OF_RESOURCES;
128 }
129
130 Status = IoMmu->SetAttribute (
131 IoMmu,
132 *Mapping,
133 EDKII_IOMMU_ACCESS_READ | EDKII_IOMMU_ACCESS_WRITE
134 );
135 if (EFI_ERROR (Status)) {
136 IoMmu->Unmap (IoMmu, *Mapping);
137 IoMmu->FreeBuffer (IoMmu, Pages, *HostAddress);
138 *Mapping = NULL;
139 *HostAddress = NULL;
140 return Status;
141 }
142
143 return Status;
144}
145
154 VOID
155 )
156{
157 EFI_STATUS Status;
158 EDKII_IOMMU_PPI *IoMmu;
159
160 IoMmu = NULL;
161 Status = PeiServicesLocatePpi (
163 0,
164 NULL,
165 (VOID **)&IoMmu
166 );
167 if (!EFI_ERROR (Status) && (IoMmu != NULL)) {
168 return IoMmu;
169 }
170
171 return NULL;
172}
173
180 VOID
181 )
182{
183 USB3_DEBUG_PORT_HANDLE *Instance;
184 EFI_PHYSICAL_ADDRESS *AddrPtr;
186 EFI_STATUS Status;
187
188 Hob.Raw = GetFirstGuidHob (&gUsb3DbgGuid);
189 if (Hob.Raw == NULL) {
190 //
191 // Build HOB for the local instance and the buffer to save instance address pointer.
192 // Use the local instance in HOB temporarily.
193 //
194 AddrPtr = BuildGuidHob (
195 &gUsb3DbgGuid,
197 );
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);
207 }
208 } else {
209 AddrPtr = GET_GUID_HOB_DATA (Hob.Guid);
210 }
211
212 return AddrPtr;
213}
214
223VOID *
225 IN UINTN BufferSize
226 )
227{
228 VOID *Buf;
229 EFI_PHYSICAL_ADDRESS Address;
230 EFI_STATUS Status;
231 VOID *MemoryDiscoveredPpi;
232 EDKII_IOMMU_PPI *IoMmu;
233 VOID *HostAddress;
234 VOID *Mapping;
235
236 Buf = NULL;
237
238 //
239 // Make sure the allocated memory is physical memory.
240 //
241 Status = PeiServicesLocatePpi (
242 &gEfiPeiMemoryDiscoveredPpiGuid,
243 0,
244 NULL,
245 (VOID **)&MemoryDiscoveredPpi
246 );
247 if (!EFI_ERROR (Status)) {
248 IoMmu = Usb3GetIoMmu ();
249 if (IoMmu != NULL) {
250 Status = IoMmuAllocateBuffer (
251 IoMmu,
252 EFI_SIZE_TO_PAGES (BufferSize),
253 &HostAddress,
254 &Address,
255 &Mapping
256 );
257 if (!EFI_ERROR (Status)) {
258 ASSERT (Address == ((EFI_PHYSICAL_ADDRESS)(UINTN)HostAddress));
259 Buf = (VOID *)(UINTN)Address;
260 }
261 } else {
262 Status = PeiServicesAllocatePages (
264 EFI_SIZE_TO_PAGES (BufferSize),
265 &Address
266 );
267 if (!EFI_ERROR (Status)) {
268 Buf = (VOID *)(UINTN)Address;
269 }
270 }
271 }
272
273 return Buf;
274}
UINT64 UINTN
UINTN EFIAPI MicroSecondDelay(IN UINTN MicroSeconds)
VOID *EFIAPI GetFirstGuidHob(IN CONST EFI_GUID *Guid)
Definition: HobLib.c:215
VOID *EFIAPI BuildGuidHob(IN CONST EFI_GUID *Guid, IN UINTN DataLength)
Definition: HobLib.c:336
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
USB3_DEBUG_PORT_HANDLE * GetUsb3DebugPortInstance(VOID)
EFI_STATUS EFIAPI Usb3IoMmuPpiNotify(IN EFI_PEI_SERVICES **PeiServices, IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDesc, IN VOID *Ppi)
VOID * AllocateAlignBuffer(IN UINTN BufferSize)
EFI_PHYSICAL_ADDRESS * GetUsb3DebugPortInstanceAddrPtr(VOID)
EDKII_IOMMU_PPI * Usb3GetIoMmu(VOID)
EFI_STATUS IoMmuAllocateBuffer(IN EDKII_IOMMU_PPI *IoMmu, IN UINTN Pages, OUT VOID **HostAddress, OUT EFI_PHYSICAL_ADDRESS *DeviceAddress, OUT VOID **Mapping)
RETURN_STATUS EFIAPI InitializeUsbDebugHardware(IN USB_DEBUG_PORT_HANDLE *Handle)
EFI_STATUS EFIAPI PeiServicesLocatePpi(IN CONST EFI_GUID *Guid, IN UINTN Instance, IN OUT EFI_PEI_PPI_DESCRIPTOR **PpiDescriptor, IN OUT VOID **Ppi)
EFI_STATUS EFIAPI PeiServicesNotifyPpi(IN CONST EFI_PEI_NOTIFY_DESCRIPTOR *NotifyList)
EFI_STATUS EFIAPI PeiServicesAllocatePages(IN EFI_MEMORY_TYPE MemoryType, IN UINTN Pages, OUT EFI_PHYSICAL_ADDRESS *Memory)
#define NULL
Definition: Base.h:319
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
#define DEBUG(Expression)
Definition: DebugLib.h:434
EFI_GUID gEdkiiIoMmuPpiGuid
@ EdkiiIoMmuOperationBusMasterCommonBuffer
Definition: IoMmu.h:59
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
#define EFI_PAGES_TO_SIZE(Pages)
Definition: UefiBaseType.h:213
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SIZE_TO_PAGES(Size)
Definition: UefiBaseType.h:200
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
@ EfiACPIMemoryNVS
@ EfiRuntimeServicesData
Definition: Base.h:213