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>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
14 Allocate a block of memory to be used by the buffer pool.
16 @param Pages How many pages to allocate.
18 @return Pointer to the allocated memory block or NULL if failed.
26 USBHC_MEM_BLOCK
*Block
;
29 EFI_PHYSICAL_ADDRESS MappedAddr
;
32 EFI_PHYSICAL_ADDRESS TempPtr
;
34 PageNumber
= EFI_SIZE_TO_PAGES (sizeof (USBHC_MEM_BLOCK
));
35 Status
= PeiServicesAllocatePages (
41 if (EFI_ERROR (Status
)) {
44 ZeroMem ((VOID
*) (UINTN
) TempPtr
, EFI_PAGES_TO_SIZE (PageNumber
));
47 // each bit in the bit array represents USBHC_MEM_UNIT
48 // bytes of memory in the memory block.
50 ASSERT (USBHC_MEM_UNIT
* 8 <= EFI_PAGE_SIZE
);
52 Block
= (USBHC_MEM_BLOCK
*) (UINTN
) TempPtr
;
53 Block
->BufLen
= EFI_PAGES_TO_SIZE (Pages
);
54 Block
->BitsLen
= Block
->BufLen
/ (USBHC_MEM_UNIT
* 8);
56 PageNumber
= EFI_SIZE_TO_PAGES (Block
->BitsLen
);
57 Status
= PeiServicesAllocatePages (
63 if (EFI_ERROR (Status
)) {
66 ZeroMem ((VOID
*) (UINTN
) TempPtr
, EFI_PAGES_TO_SIZE (PageNumber
));
68 Block
->Bits
= (UINT8
*) (UINTN
) TempPtr
;
70 Status
= IoMmuAllocateBuffer (
76 if (EFI_ERROR (Status
)) {
79 ZeroMem ((VOID
*) (UINTN
) BufHost
, EFI_PAGES_TO_SIZE (Pages
));
81 Block
->BufHost
= (UINT8
*) (UINTN
) BufHost
;
82 Block
->Buf
= (UINT8
*) (UINTN
) MappedAddr
;
83 Block
->Mapping
= Mapping
;
90 Free the memory block from the memory pool.
92 @param Pool The memory pool to free the block from.
93 @param Block The memory block to free.
98 IN USBHC_MEM_POOL
*Pool
,
99 IN USBHC_MEM_BLOCK
*Block
102 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
104 IoMmuFreeBuffer (EFI_SIZE_TO_PAGES (Block
->BufLen
), Block
->BufHost
, Block
->Mapping
);
107 // No free memory in PEI.
112 Alloc some memory from the block.
114 @param Block The memory block to allocate memory from.
115 @param Units Number of memory units to allocate.
117 @return The pointer to the allocated memory.
118 If couldn't allocate the needed memory, the return value is NULL.
122 UsbHcAllocMemFromBlock (
123 IN USBHC_MEM_BLOCK
*Block
,
134 ASSERT ((Block
!= 0) && (Units
!= 0));
140 for (Byte
= 0, Bit
= 0; Byte
< Block
->BitsLen
;) {
142 // If current bit is zero, the corresponding memory unit is
143 // available, otherwise we need to restart our searching.
144 // Available counts the consective number of zero bit.
146 if (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
149 if (Available
>= Units
) {
153 NEXT_BIT (Byte
, Bit
);
155 NEXT_BIT (Byte
, Bit
);
163 if (Available
< Units
) {
168 // Mark the memory as allocated
173 for (Count
= 0; Count
< Units
; Count
++) {
174 ASSERT (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
176 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] | (UINT8
) USB_HC_BIT (Bit
));
177 NEXT_BIT (Byte
, Bit
);
180 return Block
->BufHost
+ (StartByte
* 8 + StartBit
) * USBHC_MEM_UNIT
;
184 Calculate the corresponding pci bus address according to the Mem parameter.
186 @param Pool The memory pool of the host controller.
187 @param Mem The pointer to host memory.
188 @param Size The size of the memory region.
190 @return The pci memory address
194 UsbHcGetPciAddrForHostAddr (
195 IN USBHC_MEM_POOL
*Pool
,
200 USBHC_MEM_BLOCK
*Head
;
201 USBHC_MEM_BLOCK
*Block
;
203 EFI_PHYSICAL_ADDRESS PhyAddr
;
207 AllocSize
= USBHC_MEM_ROUND (Size
);
213 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
215 // scan the memory block list for the memory block that
216 // completely contains the allocated memory.
218 if ((Block
->BufHost
<= (UINT8
*) Mem
) && (((UINT8
*) Mem
+ AllocSize
) <= (Block
->BufHost
+ Block
->BufLen
))) {
223 ASSERT ((Block
!= NULL
));
225 // calculate the pci memory address for host memory address.
227 Offset
= (UINT8
*) Mem
- Block
->BufHost
;
228 PhyAddr
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) (Block
->Buf
+ Offset
);
233 Calculate the corresponding host address according to the pci address.
235 @param Pool The memory pool of the host controller.
236 @param Mem The pointer to pci memory.
237 @param Size The size of the memory region.
239 @return The host memory address
243 UsbHcGetHostAddrForPciAddr (
244 IN USBHC_MEM_POOL
*Pool
,
249 USBHC_MEM_BLOCK
*Head
;
250 USBHC_MEM_BLOCK
*Block
;
252 EFI_PHYSICAL_ADDRESS HostAddr
;
256 AllocSize
= USBHC_MEM_ROUND (Size
);
262 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
264 // scan the memory block list for the memory block that
265 // completely contains the allocated memory.
267 if ((Block
->Buf
<= (UINT8
*) Mem
) && (((UINT8
*) Mem
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
272 ASSERT ((Block
!= NULL
));
274 // calculate the host memory address for pci memory address.
276 Offset
= (UINT8
*) Mem
- Block
->Buf
;
277 HostAddr
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) (Block
->BufHost
+ Offset
);
282 Insert the memory block to the pool's list of the blocks.
284 @param Head The head of the memory pool's block list.
285 @param Block The memory block to insert.
289 UsbHcInsertMemBlockToPool (
290 IN USBHC_MEM_BLOCK
*Head
,
291 IN USBHC_MEM_BLOCK
*Block
294 ASSERT ((Head
!= NULL
) && (Block
!= NULL
));
295 Block
->Next
= Head
->Next
;
300 Is the memory block empty?
302 @param Block The memory block to check.
304 @retval TRUE The memory block is empty.
305 @retval FALSE The memory block isn't empty.
309 UsbHcIsMemBlockEmpty (
310 IN USBHC_MEM_BLOCK
*Block
315 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
316 if (Block
->Bits
[Index
] != 0) {
327 Initialize the memory management pool for the host controller.
329 @return Pointer to the allocated memory pool or NULL if failed.
337 USBHC_MEM_POOL
*Pool
;
340 EFI_PHYSICAL_ADDRESS TempPtr
;
342 PageNumber
= EFI_SIZE_TO_PAGES (sizeof (USBHC_MEM_POOL
));
343 Status
= PeiServicesAllocatePages (
348 if (EFI_ERROR (Status
)) {
351 ZeroMem ((VOID
*) (UINTN
) TempPtr
, EFI_PAGES_TO_SIZE (PageNumber
));
353 Pool
= (USBHC_MEM_POOL
*) ((UINTN
) TempPtr
);
354 Pool
->Head
= UsbHcAllocMemBlock (USBHC_MEM_DEFAULT_PAGES
);
356 if (Pool
->Head
== NULL
) {
358 // No free memory in PEI.
367 Release the memory management pool.
369 @param Pool The USB memory pool to free.
374 IN USBHC_MEM_POOL
*Pool
377 USBHC_MEM_BLOCK
*Block
;
379 ASSERT (Pool
->Head
!= NULL
);
382 // Unlink all the memory blocks from the pool, then free them.
383 // UsbHcUnlinkMemBlock can't be used to unlink and free the
386 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Pool
->Head
->Next
) {
387 //UsbHcUnlinkMemBlock (Pool->Head, Block);
388 UsbHcFreeMemBlock (Pool
, Block
);
391 UsbHcFreeMemBlock (Pool
, Pool
->Head
);
395 Allocate some memory from the host controller's memory pool
396 which can be used to communicate with host controller.
398 @param Pool The host controller's memory pool.
399 @param Size Size of the memory to allocate.
401 @return The allocated memory or NULL.
406 IN USBHC_MEM_POOL
*Pool
,
410 USBHC_MEM_BLOCK
*Head
;
411 USBHC_MEM_BLOCK
*Block
;
412 USBHC_MEM_BLOCK
*NewBlock
;
418 AllocSize
= USBHC_MEM_ROUND (Size
);
420 ASSERT (Head
!= NULL
);
423 // First check whether current memory blocks can satisfy the allocation.
425 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
426 Mem
= UsbHcAllocMemFromBlock (Block
, AllocSize
/ USBHC_MEM_UNIT
);
439 // Create a new memory block if there is not enough memory
440 // in the pool. If the allocation size is larger than the
441 // default page number, just allocate a large enough memory
442 // block. Otherwise allocate default pages.
444 if (AllocSize
> EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES
)) {
445 Pages
= EFI_SIZE_TO_PAGES (AllocSize
);
447 Pages
= USBHC_MEM_DEFAULT_PAGES
;
449 NewBlock
= UsbHcAllocMemBlock (Pages
);
451 if (NewBlock
== NULL
) {
456 // Add the new memory block to the pool, then allocate memory from it
458 UsbHcInsertMemBlockToPool (Head
, NewBlock
);
459 Mem
= UsbHcAllocMemFromBlock (NewBlock
, AllocSize
/ USBHC_MEM_UNIT
);
469 Free the allocated memory back to the memory pool.
471 @param Pool The memory pool of the host controller.
472 @param Mem The memory to free.
473 @param Size The size of the memory to free.
478 IN USBHC_MEM_POOL
*Pool
,
483 USBHC_MEM_BLOCK
*Head
;
484 USBHC_MEM_BLOCK
*Block
;
492 AllocSize
= USBHC_MEM_ROUND (Size
);
493 ToFree
= (UINT8
*) Mem
;
495 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
497 // scan the memory block list for the memory block that
498 // completely contains the memory to free.
500 if ((Block
->BufHost
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->BufHost
+ Block
->BufLen
))) {
502 // compute the start byte and bit in the bit array
504 Byte
= ((ToFree
- Block
->BufHost
) / USBHC_MEM_UNIT
) / 8;
505 Bit
= ((ToFree
- Block
->BufHost
) / USBHC_MEM_UNIT
) % 8;
508 // reset associated bits in bit array
510 for (Count
= 0; Count
< (AllocSize
/ USBHC_MEM_UNIT
); Count
++) {
511 ASSERT (USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
513 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] ^ USB_HC_BIT (Bit
));
514 NEXT_BIT (Byte
, Bit
);
522 // If Block == NULL, it means that the current memory isn't
523 // in the host controller's pool. This is critical because
524 // the caller has passed in a wrong memory pointer
526 ASSERT (Block
!= NULL
);
529 // Release the current memory block if it is empty and not the head
531 if ((Block
!= Head
) && UsbHcIsMemBlockEmpty (Block
)) {
532 //UsbHcUnlinkMemBlock (Head, Block);
533 UsbHcFreeMemBlock (Pool
, Block
);
538 Allocates pages at a specified alignment.
540 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
542 @param Pages The number of pages to allocate.
543 @param Alignment The requested alignment of the allocation. Must be a power of two.
544 @param HostAddress The system memory address to map to the PCI controller.
545 @param DeviceAddress The resulting map address for the bus master PCI controller to
546 use to access the hosts HostAddress.
547 @param Mapping A resulting value to pass to Unmap().
549 @retval EFI_SUCCESS Success to allocate aligned pages.
550 @retval EFI_INVALID_PARAMETER Pages or Alignment is not valid.
551 @retval EFI_OUT_OF_RESOURCES Do not have enough resources to allocate memory.
555 UsbHcAllocateAlignedPages (
558 OUT VOID
**HostAddress
,
559 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
565 EFI_PHYSICAL_ADDRESS DeviceMemory
;
568 // Alignment must be a power of two or zero.
570 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
572 if ((Alignment
& (Alignment
- 1)) != 0) {
573 return EFI_INVALID_PARAMETER
;
577 return EFI_INVALID_PARAMETER
;
580 if (Alignment
> EFI_PAGE_SIZE
) {
581 Status
= IoMmuAllocateAlignedBuffer (
588 if (EFI_ERROR (Status
)) {
589 return EFI_OUT_OF_RESOURCES
;
593 // Do not over-allocate pages in this case.
595 Status
= IoMmuAllocateBuffer (
601 if (EFI_ERROR (Status
)) {
602 return EFI_OUT_OF_RESOURCES
;
606 *HostAddress
= Memory
;
607 *DeviceAddress
= DeviceMemory
;
613 Frees memory that was allocated with UsbHcAllocateAlignedPages().
615 @param HostAddress The system memory address to map to the PCI controller.
616 @param Pages The number of pages to free.
617 @param Mapping The mapping value returned from Map().
621 UsbHcFreeAlignedPages (
622 IN VOID
*HostAddress
,
629 IoMmuFreeBuffer (Pages
, HostAddress
, Mapping
);