2 PEIM to produce gPeiUsb2HostControllerPpiGuid based on gPeiUsbControllerPpiGuid
3 which is used to enable recovery function from USB Drivers.
5 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
14 Allocate a block of memory to be used by the buffer pool.
16 @param Ehc The EHCI device.
17 @param Pool The buffer pool to allocate memory for.
18 @param Pages How many pages to allocate.
20 @return The allocated memory block or NULL if failed.
25 IN PEI_USB2_HC_DEV
*Ehc
,
26 IN USBHC_MEM_POOL
*Pool
,
30 USBHC_MEM_BLOCK
*Block
;
33 EFI_PHYSICAL_ADDRESS MappedAddr
;
36 EFI_PHYSICAL_ADDRESS TempPtr
;
39 PageNumber
= sizeof (USBHC_MEM_BLOCK
)/PAGESIZE
+1;
40 Status
= PeiServicesAllocatePages (
46 if (EFI_ERROR (Status
)) {
50 ZeroMem ((VOID
*)(UINTN
)TempPtr
, PageNumber
*EFI_PAGE_SIZE
);
53 // each bit in the bit array represents USBHC_MEM_UNIT
54 // bytes of memory in the memory block.
56 ASSERT (USBHC_MEM_UNIT
* 8 <= EFI_PAGE_SIZE
);
58 Block
= (USBHC_MEM_BLOCK
*)(UINTN
)TempPtr
;
59 Block
->BufLen
= EFI_PAGES_TO_SIZE (Pages
);
60 Block
->BitsLen
= Block
->BufLen
/ (USBHC_MEM_UNIT
* 8);
62 PageNumber
= (Block
->BitsLen
)/PAGESIZE
+1;
63 Status
= PeiServicesAllocatePages (
69 if (EFI_ERROR (Status
)) {
73 ZeroMem ((VOID
*)(UINTN
)TempPtr
, PageNumber
*EFI_PAGE_SIZE
);
75 Block
->Bits
= (UINT8
*)(UINTN
)TempPtr
;
77 Status
= IoMmuAllocateBuffer (
84 if (EFI_ERROR (Status
)) {
88 ZeroMem (BufHost
, Pages
*EFI_PAGE_SIZE
);
91 // Check whether the data structure used by the host controller
92 // should be restricted into the same 4G
94 if (Pool
->Check4G
&& (Pool
->Which4G
!= USB_HC_HIGH_32BIT (MappedAddr
))) {
98 Block
->BufHost
= BufHost
;
99 Block
->Buf
= (UINT8
*)((UINTN
)MappedAddr
);
100 Block
->Mapping
= Mapping
;
107 Free the memory block from the memory pool.
109 @param Ehc The EHCI device.
110 @param Pool The memory pool to free the block from.
111 @param Block The memory block to free.
116 IN PEI_USB2_HC_DEV
*Ehc
,
117 IN USBHC_MEM_POOL
*Pool
,
118 IN USBHC_MEM_BLOCK
*Block
121 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
123 IoMmuFreeBuffer (Ehc
->IoMmu
, EFI_SIZE_TO_PAGES (Block
->BufLen
), Block
->BufHost
, Block
->Mapping
);
127 Alloc some memory from the block.
129 @param Block The memory block to allocate memory from.
130 @param Units Number of memory units to allocate.
132 @return The pointer to the allocated memory. If couldn't allocate the needed memory,
133 the return value is NULL.
137 UsbHcAllocMemFromBlock (
138 IN USBHC_MEM_BLOCK
*Block
,
149 ASSERT ((Block
!= 0) && (Units
!= 0));
155 for (Byte
= 0, Bit
= 0; Byte
< Block
->BitsLen
;) {
157 // If current bit is zero, the corresponding memory unit is
158 // available, otherwise we need to restart our searching.
159 // Available counts the consective number of zero bit.
161 if (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
164 if (Available
>= Units
) {
168 NEXT_BIT (Byte
, Bit
);
170 NEXT_BIT (Byte
, Bit
);
178 if (Available
< Units
) {
183 // Mark the memory as allocated
188 for (Count
= 0; Count
< Units
; Count
++) {
189 ASSERT (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
191 Block
->Bits
[Byte
] = (UINT8
)(Block
->Bits
[Byte
] | (UINT8
)USB_HC_BIT (Bit
));
192 NEXT_BIT (Byte
, Bit
);
195 return Block
->Buf
+ (StartByte
* 8 + StartBit
) * USBHC_MEM_UNIT
;
199 Calculate the corresponding pci bus address according to the Mem parameter.
201 @param Pool The memory pool of the host controller.
202 @param Mem The pointer to host memory.
203 @param Size The size of the memory region.
205 @return the pci memory address
208 UsbHcGetPciAddressForHostMem (
209 IN USBHC_MEM_POOL
*Pool
,
214 USBHC_MEM_BLOCK
*Head
;
215 USBHC_MEM_BLOCK
*Block
;
217 EFI_PHYSICAL_ADDRESS PhyAddr
;
221 AllocSize
= USBHC_MEM_ROUND (Size
);
227 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
229 // scan the memory block list for the memory block that
230 // completely contains the allocated memory.
232 if ((Block
->BufHost
<= (UINT8
*)Mem
) && (((UINT8
*)Mem
+ AllocSize
) <= (Block
->BufHost
+ Block
->BufLen
))) {
237 ASSERT ((Block
!= NULL
));
239 // calculate the pci memory address for host memory address.
241 Offset
= (UINT8
*)Mem
- Block
->BufHost
;
242 PhyAddr
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)(Block
->Buf
+ Offset
);
247 Insert the memory block to the pool's list of the blocks.
249 @param Head The head of the memory pool's block list.
250 @param Block The memory block to insert.
254 UsbHcInsertMemBlockToPool (
255 IN USBHC_MEM_BLOCK
*Head
,
256 IN USBHC_MEM_BLOCK
*Block
259 ASSERT ((Head
!= NULL
) && (Block
!= NULL
));
260 Block
->Next
= Head
->Next
;
265 Is the memory block empty?
267 @param Block The memory block to check.
269 @retval TRUE The memory block is empty.
270 @retval FALSE The memory block isn't empty.
274 UsbHcIsMemBlockEmpty (
275 IN USBHC_MEM_BLOCK
*Block
280 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
281 if (Block
->Bits
[Index
] != 0) {
290 Initialize the memory management pool for the host controller.
292 @param Ehc The EHCI device.
293 @param Check4G Whether the host controller requires allocated memory.
294 from one 4G address space.
295 @param Which4G The 4G memory area each memory allocated should be from.
297 @retval EFI_SUCCESS The memory pool is initialized.
298 @retval EFI_OUT_OF_RESOURCE Fail to init the memory pool.
303 IN PEI_USB2_HC_DEV
*Ehc
,
308 USBHC_MEM_POOL
*Pool
;
311 EFI_PHYSICAL_ADDRESS TempPtr
;
313 PageNumber
= sizeof (USBHC_MEM_POOL
)/PAGESIZE
+1;
314 Status
= PeiServicesAllocatePages (
320 if (EFI_ERROR (Status
)) {
324 ZeroMem ((VOID
*)(UINTN
)TempPtr
, PageNumber
*EFI_PAGE_SIZE
);
326 Pool
= (USBHC_MEM_POOL
*)((UINTN
)TempPtr
);
328 Pool
->Check4G
= Check4G
;
329 Pool
->Which4G
= Which4G
;
330 Pool
->Head
= UsbHcAllocMemBlock (Ehc
, Pool
, USBHC_MEM_DEFAULT_PAGES
);
332 if (Pool
->Head
== NULL
) {
340 Release the memory management pool.
342 @param Ehc The EHCI device.
343 @param Pool The USB memory pool to free.
345 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
346 @retval EFI_SUCCESS The memory pool is freed.
351 IN PEI_USB2_HC_DEV
*Ehc
,
352 IN USBHC_MEM_POOL
*Pool
355 USBHC_MEM_BLOCK
*Block
;
357 ASSERT (Pool
->Head
!= NULL
);
360 // Unlink all the memory blocks from the pool, then free them.
362 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Block
->Next
) {
363 UsbHcFreeMemBlock (Ehc
, Pool
, Block
);
366 UsbHcFreeMemBlock (Ehc
, Pool
, Pool
->Head
);
372 Allocate some memory from the host controller's memory pool
373 which can be used to communicate with host controller.
375 @param Ehc The EHCI device.
376 @param Pool The host controller's memory pool.
377 @param Size Size of the memory to allocate.
379 @return The allocated memory or NULL.
384 IN PEI_USB2_HC_DEV
*Ehc
,
385 IN USBHC_MEM_POOL
*Pool
,
389 USBHC_MEM_BLOCK
*Head
;
390 USBHC_MEM_BLOCK
*Block
;
391 USBHC_MEM_BLOCK
*NewBlock
;
397 AllocSize
= USBHC_MEM_ROUND (Size
);
399 ASSERT (Head
!= NULL
);
402 // First check whether current memory blocks can satisfy the allocation.
404 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
405 Mem
= UsbHcAllocMemFromBlock (Block
, AllocSize
/ USBHC_MEM_UNIT
);
418 // Create a new memory block if there is not enough memory
419 // in the pool. If the allocation size is larger than the
420 // default page number, just allocate a large enough memory
421 // block. Otherwise allocate default pages.
423 if (AllocSize
> EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES
)) {
424 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
426 Pages
= USBHC_MEM_DEFAULT_PAGES
;
429 NewBlock
= UsbHcAllocMemBlock (Ehc
, Pool
, Pages
);
431 if (NewBlock
== NULL
) {
436 // Add the new memory block to the pool, then allocate memory from it
438 UsbHcInsertMemBlockToPool (Head
, NewBlock
);
439 Mem
= UsbHcAllocMemFromBlock (NewBlock
, AllocSize
/ USBHC_MEM_UNIT
);
449 Free the allocated memory back to the memory pool.
451 @param Ehc The EHCI device.
452 @param Pool The memory pool of the host controller.
453 @param Mem The memory to free.
454 @param Size The size of the memory to free.
459 IN PEI_USB2_HC_DEV
*Ehc
,
460 IN USBHC_MEM_POOL
*Pool
,
465 USBHC_MEM_BLOCK
*Head
;
466 USBHC_MEM_BLOCK
*Block
;
474 AllocSize
= USBHC_MEM_ROUND (Size
);
475 ToFree
= (UINT8
*)Mem
;
477 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
479 // scan the memory block list for the memory block that
480 // completely contains the memory to free.
482 if ((Block
->Buf
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
484 // compute the start byte and bit in the bit array
486 Byte
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) / 8;
487 Bit
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) % 8;
490 // reset associated bits in bit array
492 for (Count
= 0; Count
< (AllocSize
/ USBHC_MEM_UNIT
); Count
++) {
493 ASSERT (USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
495 Block
->Bits
[Byte
] = (UINT8
)(Block
->Bits
[Byte
] ^ USB_HC_BIT (Bit
));
496 NEXT_BIT (Byte
, Bit
);
504 // If Block == NULL, it means that the current memory isn't
505 // in the host controller's pool. This is critical because
506 // the caller has passed in a wrong memory point
508 ASSERT (Block
!= NULL
);
511 // Release the current memory block if it is empty and not the head
513 if ((Block
!= Head
) && UsbHcIsMemBlockEmpty (Block
)) {
514 UsbHcFreeMemBlock (Ehc
, Pool
, Block
);