2 PEIM to produce gPeiUsb2HostControllerPpiGuid based on gPeiUsbControllerPpiGuid
3 which is used to enable recovery function from USB Drivers.
5 Copyright (c) 2010, 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
;
83 Status
= PeiServicesAllocatePages (
88 ZeroMem ((VOID
*)(UINTN
)TempPtr
, Pages
*EFI_PAGE_SIZE
);
90 BufHost
= (VOID
*)(UINTN
)TempPtr
;
91 MappedAddr
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) BufHost
;
93 // Check whether the data structure used by the host controller
94 // should be restricted into the same 4G
96 if (Pool
->Check4G
&& (Pool
->Which4G
!= USB_HC_HIGH_32BIT (MappedAddr
))) {
100 Block
->BufHost
= BufHost
;
101 Block
->Buf
= (UINT8
*) ((UINTN
) MappedAddr
);
102 Block
->Mapping
= Mapping
;
110 Free the memory block from the memory pool.
112 @param Pool The memory pool to free the block from.
113 @param Block The memory block to free.
118 IN USBHC_MEM_POOL
*Pool
,
119 IN USBHC_MEM_BLOCK
*Block
122 ASSERT ((Pool
!= NULL
) && (Block
!= NULL
));
126 Alloc some memory from the block.
128 @param Block The memory block to allocate memory from.
129 @param Units Number of memory units to allocate.
131 @return The pointer to the allocated memory. If couldn't allocate the needed memory,
132 the return value is NULL.
136 UsbHcAllocMemFromBlock (
137 IN USBHC_MEM_BLOCK
*Block
,
148 ASSERT ((Block
!= 0) && (Units
!= 0));
154 for (Byte
= 0, Bit
= 0; Byte
< Block
->BitsLen
;) {
156 // If current bit is zero, the corresponding memory unit is
157 // available, otherwise we need to restart our searching.
158 // Available counts the consective number of zero bit.
160 if (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
)) {
163 if (Available
>= Units
) {
167 NEXT_BIT (Byte
, Bit
);
170 NEXT_BIT (Byte
, Bit
);
178 if (Available
< Units
) {
183 // Mark the memory as allocated
188 for (Count
= 0; Count
< Units
; Count
++) {
189 ASSERT (!USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
191 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] | (UINT8
) USB_HC_BIT (Bit
));
192 NEXT_BIT (Byte
, Bit
);
195 return Block
->Buf
+ (StartByte
* 8 + StartBit
) * USBHC_MEM_UNIT
;
199 Insert the memory block to the pool's list of the blocks.
201 @param Head The head of the memory pool's block list.
202 @param Block The memory block to insert.
206 UsbHcInsertMemBlockToPool (
207 IN USBHC_MEM_BLOCK
*Head
,
208 IN USBHC_MEM_BLOCK
*Block
211 ASSERT ((Head
!= NULL
) && (Block
!= NULL
));
212 Block
->Next
= Head
->Next
;
217 Is the memory block empty?
219 @param Block The memory block to check.
221 @retval TRUE The memory block is empty.
222 @retval FALSE The memory block isn't empty.
226 UsbHcIsMemBlockEmpty (
227 IN USBHC_MEM_BLOCK
*Block
233 for (Index
= 0; Index
< Block
->BitsLen
; Index
++) {
234 if (Block
->Bits
[Index
] != 0) {
243 Unlink the memory block from the pool's list.
245 @param Head The block list head of the memory's pool.
246 @param BlockToUnlink The memory block to unlink.
250 UsbHcUnlinkMemBlock (
251 IN USBHC_MEM_BLOCK
*Head
,
252 IN USBHC_MEM_BLOCK
*BlockToUnlink
255 USBHC_MEM_BLOCK
*Block
;
257 ASSERT ((Head
!= NULL
) && (BlockToUnlink
!= NULL
));
259 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
260 if (Block
->Next
== BlockToUnlink
) {
261 Block
->Next
= BlockToUnlink
->Next
;
262 BlockToUnlink
->Next
= NULL
;
269 Initialize the memory management pool for the host controller.
271 @param Ehc The EHCI device.
272 @param Check4G Whether the host controller requires allocated memory.
273 from one 4G address space.
274 @param Which4G The 4G memory area each memory allocated should be from.
276 @retval EFI_SUCCESS The memory pool is initialized.
277 @retval EFI_OUT_OF_RESOURCE Fail to init the memory pool.
282 IN PEI_USB2_HC_DEV
*Ehc
,
287 USBHC_MEM_POOL
*Pool
;
290 EFI_PHYSICAL_ADDRESS TempPtr
;
292 PageNumber
= sizeof(USBHC_MEM_POOL
)/PAGESIZE
+1;
293 Status
= PeiServicesAllocatePages (
299 if (EFI_ERROR (Status
)) {
302 ZeroMem ((VOID
*)(UINTN
)TempPtr
, PageNumber
*EFI_PAGE_SIZE
);
304 Pool
= (USBHC_MEM_POOL
*) ((UINTN
) TempPtr
);
306 Pool
->Check4G
= Check4G
;
307 Pool
->Which4G
= Which4G
;
308 Pool
->Head
= UsbHcAllocMemBlock (Ehc
, Pool
, USBHC_MEM_DEFAULT_PAGES
);
310 if (Pool
->Head
== NULL
) {
318 Release the memory management pool.
320 @param Pool The USB memory pool to free.
322 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
323 @retval EFI_SUCCESS The memory pool is freed.
328 IN USBHC_MEM_POOL
*Pool
331 USBHC_MEM_BLOCK
*Block
;
333 ASSERT (Pool
->Head
!= NULL
);
336 // Unlink all the memory blocks from the pool, then free them.
337 // UsbHcUnlinkMemBlock can't be used to unlink and free the
340 for (Block
= Pool
->Head
->Next
; Block
!= NULL
; Block
= Pool
->Head
->Next
) {
341 UsbHcFreeMemBlock (Pool
, Block
);
344 UsbHcFreeMemBlock (Pool
, Pool
->Head
);
350 Allocate some memory from the host controller's memory pool
351 which can be used to communicate with host controller.
353 @param Ehc The EHCI device.
354 @param Pool The host controller's memory pool.
355 @param Size Size of the memory to allocate.
357 @return The allocated memory or NULL.
362 IN PEI_USB2_HC_DEV
*Ehc
,
363 IN USBHC_MEM_POOL
*Pool
,
367 USBHC_MEM_BLOCK
*Head
;
368 USBHC_MEM_BLOCK
*Block
;
369 USBHC_MEM_BLOCK
*NewBlock
;
375 AllocSize
= USBHC_MEM_ROUND (Size
);
377 ASSERT (Head
!= NULL
);
380 // First check whether current memory blocks can satisfy the allocation.
382 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
383 Mem
= UsbHcAllocMemFromBlock (Block
, AllocSize
/ USBHC_MEM_UNIT
);
396 // Create a new memory block if there is not enough memory
397 // in the pool. If the allocation size is larger than the
398 // default page number, just allocate a large enough memory
399 // block. Otherwise allocate default pages.
401 if (AllocSize
> EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES
)) {
402 Pages
= EFI_SIZE_TO_PAGES (AllocSize
) + 1;
404 Pages
= USBHC_MEM_DEFAULT_PAGES
;
406 NewBlock
= UsbHcAllocMemBlock (Ehc
,Pool
, Pages
);
408 if (NewBlock
== NULL
) {
413 // Add the new memory block to the pool, then allocate memory from it
415 UsbHcInsertMemBlockToPool (Head
, NewBlock
);
416 Mem
= UsbHcAllocMemFromBlock (NewBlock
, AllocSize
/ USBHC_MEM_UNIT
);
426 Free the allocated memory back to the memory pool.
428 @param Pool The memory pool of the host controller.
429 @param Mem The memory to free.
430 @param Size The size of the memory to free.
435 IN USBHC_MEM_POOL
*Pool
,
440 USBHC_MEM_BLOCK
*Head
;
441 USBHC_MEM_BLOCK
*Block
;
449 AllocSize
= USBHC_MEM_ROUND (Size
);
450 ToFree
= (UINT8
*) Mem
;
452 for (Block
= Head
; Block
!= NULL
; Block
= Block
->Next
) {
454 // scan the memory block list for the memory block that
455 // completely contains the memory to free.
457 if ((Block
->Buf
<= ToFree
) && ((ToFree
+ AllocSize
) <= (Block
->Buf
+ Block
->BufLen
))) {
459 // compute the start byte and bit in the bit array
461 Byte
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) / 8;
462 Bit
= ((ToFree
- Block
->Buf
) / USBHC_MEM_UNIT
) % 8;
465 // reset associated bits in bit arry
467 for (Count
= 0; Count
< (AllocSize
/ USBHC_MEM_UNIT
); Count
++) {
468 ASSERT (USB_HC_BIT_IS_SET (Block
->Bits
[Byte
], Bit
));
470 Block
->Bits
[Byte
] = (UINT8
) (Block
->Bits
[Byte
] ^ USB_HC_BIT (Bit
));
471 NEXT_BIT (Byte
, Bit
);
479 // If Block == NULL, it means that the current memory isn't
480 // in the host controller's pool. This is critical because
481 // the caller has passed in a wrong memory point
483 ASSERT (Block
!= NULL
);
486 // Release the current memory block if it is empty and not the head
488 if ((Block
!= Head
) && UsbHcIsMemBlockEmpty (Block
)) {
489 UsbHcFreeMemBlock (Pool
, Block
);