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
)) {
49 ZeroMem ((VOID
*)(UINTN
)TempPtr
, PageNumber
*EFI_PAGE_SIZE
);
52 // each bit in the bit array represents USBHC_MEM_UNIT
53 // bytes of memory in the memory block.
55 ASSERT (USBHC_MEM_UNIT
* 8 <= EFI_PAGE_SIZE
);
57 Block
= (USBHC_MEM_BLOCK
*)(UINTN
)TempPtr
;
58 Block
->BufLen
= EFI_PAGES_TO_SIZE (Pages
);
59 Block
->BitsLen
= Block
->BufLen
/ (USBHC_MEM_UNIT
* 8);
61 PageNumber
= (Block
->BitsLen
)/PAGESIZE
+1;
62 Status
= PeiServicesAllocatePages (
68 if (EFI_ERROR (Status
)) {
71 ZeroMem ((VOID
*)(UINTN
)TempPtr
, PageNumber
*EFI_PAGE_SIZE
);
73 Block
->Bits
= (UINT8
*)(UINTN
)TempPtr
;
75 Status
= IoMmuAllocateBuffer (
82 if (EFI_ERROR (Status
)) {
85 ZeroMem (BufHost
, Pages
*EFI_PAGE_SIZE
);
88 // Check whether the data structure used by the host controller
89 // should be restricted into the same 4G
91 if (Pool
->Check4G
&& (Pool
->Which4G
!= USB_HC_HIGH_32BIT (MappedAddr
))) {
95 Block
->BufHost
= BufHost
;
96 Block
->Buf
= (UINT8
*) ((UINTN
) MappedAddr
);
97 Block
->Mapping
= Mapping
;
105 Free the memory block from the memory pool.
107 @param Ehc The EHCI device.
108 @param Pool The memory pool to free the block from.
109 @param Block The memory block to free.
114 IN PEI_USB2_HC_DEV
*Ehc
,
115 IN USBHC_MEM_POOL
*Pool
,
116 IN USBHC_MEM_BLOCK
*Block
119 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
121 IoMmuFreeBuffer (Ehc
->IoMmu
, EFI_SIZE_TO_PAGES (Block
->BufLen
), Block
->BufHost
, Block
->Mapping
);
125 Alloc some memory from the block.
127 @param Block The memory block to allocate memory from.
128 @param Units Number of memory units to allocate.
130 @return The pointer to the allocated memory. If couldn't allocate the needed memory,
131 the return value is NULL.
135 UsbHcAllocMemFromBlock (
136 IN USBHC_MEM_BLOCK
*Block
,
147 ASSERT ((Block
!= 0) && (Units
!= 0));
153 for (Byte
= 0, Bit
= 0; Byte
< Block
->BitsLen
;) {
155 // If current bit is zero, the corresponding memory unit is
156 // available, otherwise we need to restart our searching.
157 // Available counts the consective number of zero bit.
159 if (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
162 if (Available
>= Units
) {
166 NEXT_BIT (Byte
, Bit
);
169 NEXT_BIT (Byte
, Bit
);
177 if (Available
< Units
) {
182 // Mark the memory as allocated
187 for (Count
= 0; Count
< Units
; Count
++) {
188 ASSERT (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
190 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] | (UINT8
) USB_HC_BIT (Bit
));
191 NEXT_BIT (Byte
, Bit
);
194 return Block
->Buf
+ (StartByte
* 8 + StartBit
) * USBHC_MEM_UNIT
;
198 Calculate the corresponding pci bus address according to the Mem parameter.
200 @param Pool The memory pool of the host controller.
201 @param Mem The pointer to host memory.
202 @param Size The size of the memory region.
204 @return the pci memory address
207 UsbHcGetPciAddressForHostMem (
208 IN USBHC_MEM_POOL
*Pool
,
213 USBHC_MEM_BLOCK
*Head
;
214 USBHC_MEM_BLOCK
*Block
;
216 EFI_PHYSICAL_ADDRESS PhyAddr
;
220 AllocSize
= USBHC_MEM_ROUND (Size
);
226 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
228 // scan the memory block list for the memory block that
229 // completely contains the allocated memory.
231 if ((Block
->BufHost
<= (UINT8
*) Mem
) && (((UINT8
*) Mem
+ AllocSize
) <= (Block
->BufHost
+ Block
->BufLen
))) {
236 ASSERT ((Block
!= NULL
));
238 // calculate the pci memory address for host memory address.
240 Offset
= (UINT8
*)Mem
- Block
->BufHost
;
241 PhyAddr
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) (Block
->Buf
+ Offset
);
246 Insert the memory block to the pool's list of the blocks.
248 @param Head The head of the memory pool's block list.
249 @param Block The memory block to insert.
253 UsbHcInsertMemBlockToPool (
254 IN USBHC_MEM_BLOCK
*Head
,
255 IN USBHC_MEM_BLOCK
*Block
258 ASSERT ((Head
!= NULL
) && (Block
!= NULL
));
259 Block
->Next
= Head
->Next
;
264 Is the memory block empty?
266 @param Block The memory block to check.
268 @retval TRUE The memory block is empty.
269 @retval FALSE The memory block isn't empty.
273 UsbHcIsMemBlockEmpty (
274 IN USBHC_MEM_BLOCK
*Block
280 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
281 if (Block
->Bits
[Index
] != 0) {
291 Initialize the memory management pool for the host controller.
293 @param Ehc The EHCI device.
294 @param Check4G Whether the host controller requires allocated memory.
295 from one 4G address space.
296 @param Which4G The 4G memory area each memory allocated should be from.
298 @retval EFI_SUCCESS The memory pool is initialized.
299 @retval EFI_OUT_OF_RESOURCE Fail to init the memory pool.
304 IN PEI_USB2_HC_DEV
*Ehc
,
309 USBHC_MEM_POOL
*Pool
;
312 EFI_PHYSICAL_ADDRESS TempPtr
;
314 PageNumber
= sizeof(USBHC_MEM_POOL
)/PAGESIZE
+1;
315 Status
= PeiServicesAllocatePages (
321 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
;
428 NewBlock
= UsbHcAllocMemBlock (Ehc
,Pool
, Pages
);
430 if (NewBlock
== NULL
) {
435 // Add the new memory block to the pool, then allocate memory from it
437 UsbHcInsertMemBlockToPool (Head
, NewBlock
);
438 Mem
= UsbHcAllocMemFromBlock (NewBlock
, AllocSize
/ USBHC_MEM_UNIT
);
448 Free the allocated memory back to the memory pool.
450 @param Ehc The EHCI device.
451 @param Pool The memory pool of the host controller.
452 @param Mem The memory to free.
453 @param Size The size of the memory to free.
458 IN PEI_USB2_HC_DEV
*Ehc
,
459 IN USBHC_MEM_POOL
*Pool
,
464 USBHC_MEM_BLOCK
*Head
;
465 USBHC_MEM_BLOCK
*Block
;
473 AllocSize
= USBHC_MEM_ROUND (Size
);
474 ToFree
= (UINT8
*) Mem
;
476 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
478 // scan the memory block list for the memory block that
479 // completely contains the memory to free.
481 if ((Block
->Buf
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
483 // compute the start byte and bit in the bit array
485 Byte
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) / 8;
486 Bit
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) % 8;
489 // reset associated bits in bit array
491 for (Count
= 0; Count
< (AllocSize
/ USBHC_MEM_UNIT
); Count
++) {
492 ASSERT (USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
494 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] ^ USB_HC_BIT (Bit
));
495 NEXT_BIT (Byte
, Bit
);
503 // If Block == NULL, it means that the current memory isn't
504 // in the host controller's pool. This is critical because
505 // the caller has passed in a wrong memory point
507 ASSERT (Block
!= NULL
);
510 // Release the current memory block if it is empty and not the head
512 if ((Block
!= Head
) && UsbHcIsMemBlockEmpty (Block
)) {
513 UsbHcFreeMemBlock (Ehc
, Pool
, Block
);