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) {
298 Initialize the memory management pool for the host controller.
300 @param Ehc The EHCI device.
301 @param Check4G Whether the host controller requires allocated memory.
302 from one 4G address space.
303 @param Which4G The 4G memory area each memory allocated should be from.
305 @retval EFI_SUCCESS The memory pool is initialized.
306 @retval EFI_OUT_OF_RESOURCE Fail to init the memory pool.
311 IN PEI_USB2_HC_DEV
*Ehc
,
316 USBHC_MEM_POOL
*Pool
;
319 EFI_PHYSICAL_ADDRESS TempPtr
;
321 PageNumber
= sizeof(USBHC_MEM_POOL
)/PAGESIZE
+1;
322 Status
= PeiServicesAllocatePages (
328 if (EFI_ERROR (Status
)) {
331 ZeroMem ((VOID
*)(UINTN
)TempPtr
, PageNumber
*EFI_PAGE_SIZE
);
333 Pool
= (USBHC_MEM_POOL
*) ((UINTN
) TempPtr
);
335 Pool
->Check4G
= Check4G
;
336 Pool
->Which4G
= Which4G
;
337 Pool
->Head
= UsbHcAllocMemBlock (Ehc
, Pool
, USBHC_MEM_DEFAULT_PAGES
);
339 if (Pool
->Head
== NULL
) {
347 Release the memory management pool.
349 @param Ehc The EHCI device.
350 @param Pool The USB memory pool to free.
352 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
353 @retval EFI_SUCCESS The memory pool is freed.
358 IN PEI_USB2_HC_DEV
*Ehc
,
359 IN USBHC_MEM_POOL
*Pool
362 USBHC_MEM_BLOCK
*Block
;
364 ASSERT (Pool
->Head
!= NULL
);
367 // Unlink all the memory blocks from the pool, then free them.
369 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Block
->Next
) {
370 UsbHcFreeMemBlock (Ehc
, Pool
, Block
);
373 UsbHcFreeMemBlock (Ehc
, Pool
, Pool
->Head
);
379 Allocate some memory from the host controller's memory pool
380 which can be used to communicate with host controller.
382 @param Ehc The EHCI device.
383 @param Pool The host controller's memory pool.
384 @param Size Size of the memory to allocate.
386 @return The allocated memory or NULL.
391 IN PEI_USB2_HC_DEV
*Ehc
,
392 IN USBHC_MEM_POOL
*Pool
,
396 USBHC_MEM_BLOCK
*Head
;
397 USBHC_MEM_BLOCK
*Block
;
398 USBHC_MEM_BLOCK
*NewBlock
;
404 AllocSize
= USBHC_MEM_ROUND (Size
);
406 ASSERT (Head
!= NULL
);
409 // First check whether current memory blocks can satisfy the allocation.
411 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
412 Mem
= UsbHcAllocMemFromBlock (Block
, AllocSize
/ USBHC_MEM_UNIT
);
425 // Create a new memory block if there is not enough memory
426 // in the pool. If the allocation size is larger than the
427 // default page number, just allocate a large enough memory
428 // block. Otherwise allocate default pages.
430 if (AllocSize
> EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES
)) {
431 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
433 Pages
= USBHC_MEM_DEFAULT_PAGES
;
435 NewBlock
= UsbHcAllocMemBlock (Ehc
,Pool
, Pages
);
437 if (NewBlock
== NULL
) {
442 // Add the new memory block to the pool, then allocate memory from it
444 UsbHcInsertMemBlockToPool (Head
, NewBlock
);
445 Mem
= UsbHcAllocMemFromBlock (NewBlock
, AllocSize
/ USBHC_MEM_UNIT
);
455 Free the allocated memory back to the memory pool.
457 @param Ehc The EHCI device.
458 @param Pool The memory pool of the host controller.
459 @param Mem The memory to free.
460 @param Size The size of the memory to free.
465 IN PEI_USB2_HC_DEV
*Ehc
,
466 IN USBHC_MEM_POOL
*Pool
,
471 USBHC_MEM_BLOCK
*Head
;
472 USBHC_MEM_BLOCK
*Block
;
480 AllocSize
= USBHC_MEM_ROUND (Size
);
481 ToFree
= (UINT8
*) Mem
;
483 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
485 // scan the memory block list for the memory block that
486 // completely contains the memory to free.
488 if ((Block
->Buf
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
490 // compute the start byte and bit in the bit array
492 Byte
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) / 8;
493 Bit
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) % 8;
496 // reset associated bits in bit array
498 for (Count
= 0; Count
< (AllocSize
/ USBHC_MEM_UNIT
); Count
++) {
499 ASSERT (USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
501 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] ^ USB_HC_BIT (Bit
));
502 NEXT_BIT (Byte
, Bit
);
510 // If Block == NULL, it means that the current memory isn't
511 // in the host controller's pool. This is critical because
512 // the caller has passed in a wrong memory point
514 ASSERT (Block
!= NULL
);
517 // Release the current memory block if it is empty and not the head
519 if ((Block
!= Head
) && UsbHcIsMemBlockEmpty (Block
)) {
520 UsbHcFreeMemBlock (Ehc
, Pool
, Block
);