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 This program and the accompanying materials
8 are licensed and made available under the terms and conditions
9 of the BSD License which accompanies this distribution. The
10 full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 Allocate a block of memory to be used by the buffer pool.
23 @param Ehc The EHCI device.
24 @param Pool The buffer pool to allocate memory for.
25 @param Pages How many pages to allocate.
27 @return The allocated memory block or NULL if failed.
32 IN PEI_USB2_HC_DEV
*Ehc
,
33 IN USBHC_MEM_POOL
*Pool
,
37 USBHC_MEM_BLOCK
*Block
;
40 EFI_PHYSICAL_ADDRESS MappedAddr
;
43 EFI_PHYSICAL_ADDRESS TempPtr
;
46 PageNumber
= sizeof(USBHC_MEM_BLOCK
)/PAGESIZE
+1;
47 Status
= PeiServicesAllocatePages (
53 if (EFI_ERROR (Status
)) {
56 ZeroMem ((VOID
*)(UINTN
)TempPtr
, PageNumber
*EFI_PAGE_SIZE
);
59 // each bit in the bit array represents USBHC_MEM_UNIT
60 // bytes of memory in the memory block.
62 ASSERT (USBHC_MEM_UNIT
* 8 <= EFI_PAGE_SIZE
);
64 Block
= (USBHC_MEM_BLOCK
*)(UINTN
)TempPtr
;
65 Block
->BufLen
= EFI_PAGES_TO_SIZE (Pages
);
66 Block
->BitsLen
= Block
->BufLen
/ (USBHC_MEM_UNIT
* 8);
68 PageNumber
= (Block
->BitsLen
)/PAGESIZE
+1;
69 Status
= PeiServicesAllocatePages (
75 if (EFI_ERROR (Status
)) {
78 ZeroMem ((VOID
*)(UINTN
)TempPtr
, PageNumber
*EFI_PAGE_SIZE
);
80 Block
->Bits
= (UINT8
*)(UINTN
)TempPtr
;
82 Status
= IoMmuAllocateBuffer (
89 if (EFI_ERROR (Status
)) {
92 ZeroMem (BufHost
, Pages
*EFI_PAGE_SIZE
);
95 // Check whether the data structure used by the host controller
96 // should be restricted into the same 4G
98 if (Pool
->Check4G
&& (Pool
->Which4G
!= USB_HC_HIGH_32BIT (MappedAddr
))) {
102 Block
->BufHost
= BufHost
;
103 Block
->Buf
= (UINT8
*) ((UINTN
) MappedAddr
);
104 Block
->Mapping
= Mapping
;
112 Free the memory block from the memory pool.
114 @param Ehc The EHCI device.
115 @param Pool The memory pool to free the block from.
116 @param Block The memory block to free.
121 IN PEI_USB2_HC_DEV
*Ehc
,
122 IN USBHC_MEM_POOL
*Pool
,
123 IN USBHC_MEM_BLOCK
*Block
126 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
128 IoMmuFreeBuffer (Ehc
->IoMmu
, EFI_SIZE_TO_PAGES (Block
->BufLen
), Block
->BufHost
, Block
->Mapping
);
132 Alloc some memory from the block.
134 @param Block The memory block to allocate memory from.
135 @param Units Number of memory units to allocate.
137 @return The pointer to the allocated memory. If couldn't allocate the needed memory,
138 the return value is NULL.
142 UsbHcAllocMemFromBlock (
143 IN USBHC_MEM_BLOCK
*Block
,
154 ASSERT ((Block
!= 0) && (Units
!= 0));
160 for (Byte
= 0, Bit
= 0; Byte
< Block
->BitsLen
;) {
162 // If current bit is zero, the corresponding memory unit is
163 // available, otherwise we need to restart our searching.
164 // Available counts the consective number of zero bit.
166 if (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
169 if (Available
>= Units
) {
173 NEXT_BIT (Byte
, Bit
);
176 NEXT_BIT (Byte
, Bit
);
184 if (Available
< Units
) {
189 // Mark the memory as allocated
194 for (Count
= 0; Count
< Units
; Count
++) {
195 ASSERT (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
197 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] | (UINT8
) USB_HC_BIT (Bit
));
198 NEXT_BIT (Byte
, Bit
);
201 return Block
->Buf
+ (StartByte
* 8 + StartBit
) * USBHC_MEM_UNIT
;
205 Calculate the corresponding pci bus address according to the Mem parameter.
207 @param Pool The memory pool of the host controller.
208 @param Mem The pointer to host memory.
209 @param Size The size of the memory region.
211 @return the pci memory address
214 UsbHcGetPciAddressForHostMem (
215 IN USBHC_MEM_POOL
*Pool
,
220 USBHC_MEM_BLOCK
*Head
;
221 USBHC_MEM_BLOCK
*Block
;
223 EFI_PHYSICAL_ADDRESS PhyAddr
;
227 AllocSize
= USBHC_MEM_ROUND (Size
);
233 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
235 // scan the memory block list for the memory block that
236 // completely contains the allocated memory.
238 if ((Block
->BufHost
<= (UINT8
*) Mem
) && (((UINT8
*) Mem
+ AllocSize
) <= (Block
->BufHost
+ Block
->BufLen
))) {
243 ASSERT ((Block
!= NULL
));
245 // calculate the pci memory address for host memory address.
247 Offset
= (UINT8
*)Mem
- Block
->BufHost
;
248 PhyAddr
= (EFI_PHYSICAL_ADDRESS
)(UINTN
) (Block
->Buf
+ Offset
);
253 Insert the memory block to the pool's list of the blocks.
255 @param Head The head of the memory pool's block list.
256 @param Block The memory block to insert.
260 UsbHcInsertMemBlockToPool (
261 IN USBHC_MEM_BLOCK
*Head
,
262 IN USBHC_MEM_BLOCK
*Block
265 ASSERT ((Head
!= NULL
) && (Block
!= NULL
));
266 Block
->Next
= Head
->Next
;
271 Is the memory block empty?
273 @param Block The memory block to check.
275 @retval TRUE The memory block is empty.
276 @retval FALSE The memory block isn't empty.
280 UsbHcIsMemBlockEmpty (
281 IN USBHC_MEM_BLOCK
*Block
287 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
288 if (Block
->Bits
[Index
] != 0) {
297 Unlink the memory block from the pool's list.
299 @param Head The block list head of the memory's pool.
300 @param BlockToUnlink The memory block to unlink.
304 UsbHcUnlinkMemBlock (
305 IN USBHC_MEM_BLOCK
*Head
,
306 IN USBHC_MEM_BLOCK
*BlockToUnlink
309 USBHC_MEM_BLOCK
*Block
;
311 ASSERT ((Head
!= NULL
) && (BlockToUnlink
!= NULL
));
313 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
314 if (Block
->Next
== BlockToUnlink
) {
315 Block
->Next
= BlockToUnlink
->Next
;
316 BlockToUnlink
->Next
= NULL
;
323 Initialize the memory management pool for the host controller.
325 @param Ehc The EHCI device.
326 @param Check4G Whether the host controller requires allocated memory.
327 from one 4G address space.
328 @param Which4G The 4G memory area each memory allocated should be from.
330 @retval EFI_SUCCESS The memory pool is initialized.
331 @retval EFI_OUT_OF_RESOURCE Fail to init the memory pool.
336 IN PEI_USB2_HC_DEV
*Ehc
,
341 USBHC_MEM_POOL
*Pool
;
344 EFI_PHYSICAL_ADDRESS TempPtr
;
346 PageNumber
= sizeof(USBHC_MEM_POOL
)/PAGESIZE
+1;
347 Status
= PeiServicesAllocatePages (
353 if (EFI_ERROR (Status
)) {
356 ZeroMem ((VOID
*)(UINTN
)TempPtr
, PageNumber
*EFI_PAGE_SIZE
);
358 Pool
= (USBHC_MEM_POOL
*) ((UINTN
) TempPtr
);
360 Pool
->Check4G
= Check4G
;
361 Pool
->Which4G
= Which4G
;
362 Pool
->Head
= UsbHcAllocMemBlock (Ehc
, Pool
, USBHC_MEM_DEFAULT_PAGES
);
364 if (Pool
->Head
== NULL
) {
372 Release the memory management pool.
374 @param Ehc The EHCI device.
375 @param Pool The USB memory pool to free.
377 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
378 @retval EFI_SUCCESS The memory pool is freed.
383 IN PEI_USB2_HC_DEV
*Ehc
,
384 IN USBHC_MEM_POOL
*Pool
387 USBHC_MEM_BLOCK
*Block
;
389 ASSERT (Pool
->Head
!= NULL
);
392 // Unlink all the memory blocks from the pool, then free them.
393 // UsbHcUnlinkMemBlock can't be used to unlink and free the
396 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Block
->Next
) {
397 UsbHcFreeMemBlock (Ehc
, Pool
, Block
);
400 UsbHcFreeMemBlock (Ehc
, Pool
, Pool
->Head
);
406 Allocate some memory from the host controller's memory pool
407 which can be used to communicate with host controller.
409 @param Ehc The EHCI device.
410 @param Pool The host controller's memory pool.
411 @param Size Size of the memory to allocate.
413 @return The allocated memory or NULL.
418 IN PEI_USB2_HC_DEV
*Ehc
,
419 IN USBHC_MEM_POOL
*Pool
,
423 USBHC_MEM_BLOCK
*Head
;
424 USBHC_MEM_BLOCK
*Block
;
425 USBHC_MEM_BLOCK
*NewBlock
;
431 AllocSize
= USBHC_MEM_ROUND (Size
);
433 ASSERT (Head
!= NULL
);
436 // First check whether current memory blocks can satisfy the allocation.
438 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
439 Mem
= UsbHcAllocMemFromBlock (Block
, AllocSize
/ USBHC_MEM_UNIT
);
452 // Create a new memory block if there is not enough memory
453 // in the pool. If the allocation size is larger than the
454 // default page number, just allocate a large enough memory
455 // block. Otherwise allocate default pages.
457 if (AllocSize
> EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES
)) {
458 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
460 Pages
= USBHC_MEM_DEFAULT_PAGES
;
462 NewBlock
= UsbHcAllocMemBlock (Ehc
,Pool
, Pages
);
464 if (NewBlock
== NULL
) {
469 // Add the new memory block to the pool, then allocate memory from it
471 UsbHcInsertMemBlockToPool (Head
, NewBlock
);
472 Mem
= UsbHcAllocMemFromBlock (NewBlock
, AllocSize
/ USBHC_MEM_UNIT
);
482 Free the allocated memory back to the memory pool.
484 @param Ehc The EHCI device.
485 @param Pool The memory pool of the host controller.
486 @param Mem The memory to free.
487 @param Size The size of the memory to free.
492 IN PEI_USB2_HC_DEV
*Ehc
,
493 IN USBHC_MEM_POOL
*Pool
,
498 USBHC_MEM_BLOCK
*Head
;
499 USBHC_MEM_BLOCK
*Block
;
507 AllocSize
= USBHC_MEM_ROUND (Size
);
508 ToFree
= (UINT8
*) Mem
;
510 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
512 // scan the memory block list for the memory block that
513 // completely contains the memory to free.
515 if ((Block
->Buf
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
517 // compute the start byte and bit in the bit array
519 Byte
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) / 8;
520 Bit
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) % 8;
523 // reset associated bits in bit array
525 for (Count
= 0; Count
< (AllocSize
/ USBHC_MEM_UNIT
); Count
++) {
526 ASSERT (USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
528 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] ^ USB_HC_BIT (Bit
));
529 NEXT_BIT (Byte
, Bit
);
537 // If Block == NULL, it means that the current memory isn't
538 // in the host controller's pool. This is critical because
539 // the caller has passed in a wrong memory point
541 ASSERT (Block
!= NULL
);
544 // Release the current memory block if it is empty and not the head
546 if ((Block
!= Head
) && UsbHcIsMemBlockEmpty (Block
)) {
547 UsbHcFreeMemBlock (Ehc
, Pool
, Block
);