2 PEIM to produce gPeiUsb2HostControllerPpiGuid based on gPeiUsbControllerPpiGuid
3 which is used to enable recovery function from USB Drivers.
5 Copyright (c) 2014 - 2016, Intel Corporation. All rights reserved.<BR>
6 Copyright (C) 2022 Advanced Micro Devices, Inc. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
15 Allocate a block of memory to be used by the buffer pool.
17 @param Pages How many pages to allocate.
19 @return Pointer to the allocated memory block or NULL if failed.
27 USBHC_MEM_BLOCK
*Block
;
30 EFI_PHYSICAL_ADDRESS MappedAddr
;
33 EFI_PHYSICAL_ADDRESS TempPtr
;
35 PageNumber
= EFI_SIZE_TO_PAGES (sizeof (USBHC_MEM_BLOCK
));
36 Status
= PeiServicesAllocatePages (
42 if (EFI_ERROR (Status
)) {
46 ZeroMem ((VOID
*)(UINTN
)TempPtr
, EFI_PAGES_TO_SIZE (PageNumber
));
49 // each bit in the bit array represents USBHC_MEM_UNIT
50 // bytes of memory in the memory block.
52 ASSERT (USBHC_MEM_UNIT
* 8 <= EFI_PAGE_SIZE
);
54 Block
= (USBHC_MEM_BLOCK
*)(UINTN
)TempPtr
;
55 Block
->BufLen
= EFI_PAGES_TO_SIZE (Pages
);
56 Block
->BitsLen
= Block
->BufLen
/ (USBHC_MEM_UNIT
* 8);
58 PageNumber
= EFI_SIZE_TO_PAGES (Block
->BitsLen
);
59 Status
= PeiServicesAllocatePages (
65 if (EFI_ERROR (Status
)) {
69 ZeroMem ((VOID
*)(UINTN
)TempPtr
, EFI_PAGES_TO_SIZE (PageNumber
));
71 Block
->Bits
= (UINT8
*)(UINTN
)TempPtr
;
73 Status
= IoMmuAllocateBuffer (
79 if (EFI_ERROR (Status
)) {
83 ZeroMem ((VOID
*)(UINTN
)BufHost
, EFI_PAGES_TO_SIZE (Pages
));
85 Block
->BufHost
= (UINT8
*)(UINTN
)BufHost
;
86 Block
->Buf
= (UINT8
*)(UINTN
)MappedAddr
;
87 Block
->Mapping
= Mapping
;
94 Free the memory block from the memory pool.
96 @param Pool The memory pool to free the block from.
97 @param Block The memory block to free.
102 IN USBHC_MEM_POOL
*Pool
,
103 IN USBHC_MEM_BLOCK
*Block
106 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
108 IoMmuFreeBuffer (EFI_SIZE_TO_PAGES (Block
->BufLen
), Block
->BufHost
, Block
->Mapping
);
111 // No free memory in PEI.
116 Alloc some memory from the block.
118 @param Block The memory block to allocate memory from.
119 @param Units Number of memory units to allocate.
121 @return The pointer to the allocated memory.
122 If couldn't allocate the needed memory, the return value is NULL.
126 UsbHcAllocMemFromBlock (
127 IN USBHC_MEM_BLOCK
*Block
,
138 ASSERT ((Block
!= 0) && (Units
!= 0));
144 for (Byte
= 0, Bit
= 0; Byte
< Block
->BitsLen
;) {
146 // If current bit is zero, the corresponding memory unit is
147 // available, otherwise we need to restart our searching.
148 // Available counts the consective number of zero bit.
150 if (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
153 if (Available
>= Units
) {
157 NEXT_BIT (Byte
, Bit
);
159 NEXT_BIT (Byte
, Bit
);
167 if (Available
< Units
) {
172 // Mark the memory as allocated
177 for (Count
= 0; Count
< Units
; Count
++) {
178 ASSERT (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
180 Block
->Bits
[Byte
] = (UINT8
)(Block
->Bits
[Byte
] | (UINT8
)USB_HC_BIT (Bit
));
181 NEXT_BIT (Byte
, Bit
);
184 return Block
->BufHost
+ (StartByte
* 8 + StartBit
) * USBHC_MEM_UNIT
;
188 Calculate the corresponding pci bus address according to the Mem parameter.
190 @param Pool The memory pool of the host controller.
191 @param Mem The pointer to host memory.
192 @param Size The size of the memory region.
194 @return The pci memory address
198 UsbHcGetPciAddrForHostAddr (
199 IN USBHC_MEM_POOL
*Pool
,
204 USBHC_MEM_BLOCK
*Head
;
205 USBHC_MEM_BLOCK
*Block
;
207 EFI_PHYSICAL_ADDRESS PhyAddr
;
211 AllocSize
= USBHC_MEM_ROUND (Size
);
217 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
219 // scan the memory block list for the memory block that
220 // completely contains the allocated memory.
222 if ((Block
->BufHost
<= (UINT8
*)Mem
) && (((UINT8
*)Mem
+ AllocSize
) <= (Block
->BufHost
+ Block
->BufLen
))) {
227 ASSERT ((Block
!= NULL
));
229 // calculate the pci memory address for host memory address.
231 Offset
= (UINT8
*)Mem
- Block
->BufHost
;
232 PhyAddr
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)(Block
->Buf
+ Offset
);
237 Calculate the corresponding host address according to the pci address.
239 @param Pool The memory pool of the host controller.
240 @param Mem The pointer to pci memory.
241 @param Size The size of the memory region.
243 @return The host memory address
247 UsbHcGetHostAddrForPciAddr (
248 IN USBHC_MEM_POOL
*Pool
,
253 USBHC_MEM_BLOCK
*Head
;
254 USBHC_MEM_BLOCK
*Block
;
256 EFI_PHYSICAL_ADDRESS HostAddr
;
260 AllocSize
= USBHC_MEM_ROUND (Size
);
266 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
268 // scan the memory block list for the memory block that
269 // completely contains the allocated memory.
271 if ((Block
->Buf
<= (UINT8
*)Mem
) && (((UINT8
*)Mem
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
276 ASSERT ((Block
!= NULL
));
278 // calculate the host memory address for pci memory address.
280 Offset
= (UINT8
*)Mem
- Block
->Buf
;
281 HostAddr
= (EFI_PHYSICAL_ADDRESS
)(UINTN
)(Block
->BufHost
+ Offset
);
286 Insert the memory block to the pool's list of the blocks.
288 @param Head The head of the memory pool's block list.
289 @param Block The memory block to insert.
293 UsbHcInsertMemBlockToPool (
294 IN USBHC_MEM_BLOCK
*Head
,
295 IN USBHC_MEM_BLOCK
*Block
298 ASSERT ((Head
!= NULL
) && (Block
!= NULL
));
299 Block
->Next
= Head
->Next
;
304 Is the memory block empty?
306 @param Block The memory block to check.
308 @retval TRUE The memory block is empty.
309 @retval FALSE The memory block isn't empty.
313 UsbHcIsMemBlockEmpty (
314 IN USBHC_MEM_BLOCK
*Block
319 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
320 if (Block
->Bits
[Index
] != 0) {
329 Initialize the memory management pool for the host controller.
331 @return Pointer to the allocated memory pool or NULL if failed.
339 USBHC_MEM_POOL
*Pool
;
342 EFI_PHYSICAL_ADDRESS TempPtr
;
344 PageNumber
= EFI_SIZE_TO_PAGES (sizeof (USBHC_MEM_POOL
));
345 Status
= PeiServicesAllocatePages (
350 if (EFI_ERROR (Status
)) {
354 ZeroMem ((VOID
*)(UINTN
)TempPtr
, EFI_PAGES_TO_SIZE (PageNumber
));
356 Pool
= (USBHC_MEM_POOL
*)((UINTN
)TempPtr
);
357 Pool
->Head
= UsbHcAllocMemBlock (USBHC_MEM_DEFAULT_PAGES
);
359 if (Pool
->Head
== NULL
) {
361 // No free memory in PEI.
370 Unlink the memory block from the pool's list.
372 @param Head The block list head of the memory's pool.
373 @param BlockToUnlink The memory block to unlink.
377 UsbHcUnlinkMemBlock (
378 IN USBHC_MEM_BLOCK
*Head
,
379 IN USBHC_MEM_BLOCK
*BlockToUnlink
382 USBHC_MEM_BLOCK
*Block
;
384 ASSERT ((Head
!= NULL
) && (BlockToUnlink
!= NULL
));
386 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
387 if (Block
->Next
== BlockToUnlink
) {
388 Block
->Next
= BlockToUnlink
->Next
;
389 BlockToUnlink
->Next
= NULL
;
396 Release the memory management pool.
398 @param Pool The USB memory pool to free.
403 IN USBHC_MEM_POOL
*Pool
406 USBHC_MEM_BLOCK
*Block
;
408 ASSERT (Pool
->Head
!= NULL
);
411 // Unlink all the memory blocks from the pool, then free them.
412 // UsbHcUnlinkMemBlock can't be used to unlink and free the
415 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Pool
->Head
->Next
) {
416 UsbHcUnlinkMemBlock (Pool
->Head
, Block
);
417 UsbHcFreeMemBlock (Pool
, Block
);
420 UsbHcFreeMemBlock (Pool
, Pool
->Head
);
424 Allocate some memory from the host controller's memory pool
425 which can be used to communicate with host controller.
427 @param Pool The host controller's memory pool.
428 @param Size Size of the memory to allocate.
430 @return The allocated memory or NULL.
435 IN USBHC_MEM_POOL
*Pool
,
439 USBHC_MEM_BLOCK
*Head
;
440 USBHC_MEM_BLOCK
*Block
;
441 USBHC_MEM_BLOCK
*NewBlock
;
447 AllocSize
= USBHC_MEM_ROUND (Size
);
449 ASSERT (Head
!= NULL
);
452 // First check whether current memory blocks can satisfy the allocation.
454 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
455 Mem
= UsbHcAllocMemFromBlock (Block
, AllocSize
/ USBHC_MEM_UNIT
);
468 // Create a new memory block if there is not enough memory
469 // in the pool. If the allocation size is larger than the
470 // default page number, just allocate a large enough memory
471 // block. Otherwise allocate default pages.
473 if (AllocSize
> EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES
)) {
474 Pages
= EFI_SIZE_TO_PAGES (AllocSize
);
476 Pages
= USBHC_MEM_DEFAULT_PAGES
;
479 NewBlock
= UsbHcAllocMemBlock (Pages
);
481 if (NewBlock
== NULL
) {
486 // Add the new memory block to the pool, then allocate memory from it
488 UsbHcInsertMemBlockToPool (Head
, NewBlock
);
489 Mem
= UsbHcAllocMemFromBlock (NewBlock
, AllocSize
/ USBHC_MEM_UNIT
);
499 Free the allocated memory back to the memory pool.
501 @param Pool The memory pool of the host controller.
502 @param Mem The memory to free.
503 @param Size The size of the memory to free.
508 IN USBHC_MEM_POOL
*Pool
,
513 USBHC_MEM_BLOCK
*Head
;
514 USBHC_MEM_BLOCK
*Block
;
522 AllocSize
= USBHC_MEM_ROUND (Size
);
523 ToFree
= (UINT8
*)Mem
;
525 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
527 // scan the memory block list for the memory block that
528 // completely contains the memory to free.
530 if ((Block
->BufHost
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->BufHost
+ Block
->BufLen
))) {
532 // compute the start byte and bit in the bit array
534 Byte
= ((ToFree
- Block
->BufHost
) / USBHC_MEM_UNIT
) / 8;
535 Bit
= ((ToFree
- Block
->BufHost
) / USBHC_MEM_UNIT
) % 8;
538 // reset associated bits in bit array
540 for (Count
= 0; Count
< (AllocSize
/ USBHC_MEM_UNIT
); Count
++) {
541 ASSERT (USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
543 Block
->Bits
[Byte
] = (UINT8
)(Block
->Bits
[Byte
] ^ USB_HC_BIT (Bit
));
544 NEXT_BIT (Byte
, Bit
);
552 // If Block == NULL, it means that the current memory isn't
553 // in the host controller's pool. This is critical because
554 // the caller has passed in a wrong memory pointer
556 ASSERT (Block
!= NULL
);
559 // Release the current memory block if it is empty and not the head
561 if ((Block
!= Head
) && UsbHcIsMemBlockEmpty (Block
)) {
562 UsbHcUnlinkMemBlock (Head
, Block
);
563 UsbHcFreeMemBlock (Pool
, Block
);
568 Allocates pages at a specified alignment.
570 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
572 @param Pages The number of pages to allocate.
573 @param Alignment The requested alignment of the allocation. Must be a power of two.
574 @param HostAddress The system memory address to map to the PCI controller.
575 @param DeviceAddress The resulting map address for the bus master PCI controller to
576 use to access the hosts HostAddress.
577 @param Mapping A resulting value to pass to Unmap().
579 @retval EFI_SUCCESS Success to allocate aligned pages.
580 @retval EFI_INVALID_PARAMETER Pages or Alignment is not valid.
581 @retval EFI_OUT_OF_RESOURCES Do not have enough resources to allocate memory.
585 UsbHcAllocateAlignedPages (
588 OUT VOID
**HostAddress
,
589 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
595 EFI_PHYSICAL_ADDRESS DeviceMemory
;
598 // Alignment must be a power of two or zero.
600 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
602 if ((Alignment
& (Alignment
- 1)) != 0) {
603 return EFI_INVALID_PARAMETER
;
607 return EFI_INVALID_PARAMETER
;
610 if (Alignment
> EFI_PAGE_SIZE
) {
611 Status
= IoMmuAllocateAlignedBuffer (
618 if (EFI_ERROR (Status
)) {
619 return EFI_OUT_OF_RESOURCES
;
623 // Do not over-allocate pages in this case.
625 Status
= IoMmuAllocateBuffer (
631 if (EFI_ERROR (Status
)) {
632 return EFI_OUT_OF_RESOURCES
;
636 *HostAddress
= Memory
;
637 *DeviceAddress
= DeviceMemory
;
643 Frees memory that was allocated with UsbHcAllocateAlignedPages().
645 @param HostAddress The system memory address to map to the PCI controller.
646 @param Pages The number of pages to free.
647 @param Mapping The mapping value returned from Map().
651 UsbHcFreeAlignedPages (
652 IN VOID
*HostAddress
,
659 IoMmuFreeBuffer (Pages
, HostAddress
, Mapping
);