TianoCore EDK2 master
Loading...
Searching...
No Matches
UsbHcMem.c
Go to the documentation of this file.
1
11#include "EhcPeim.h"
12
26 IN USBHC_MEM_POOL *Pool,
27 IN UINTN Pages
28 )
29{
30 USBHC_MEM_BLOCK *Block;
31 VOID *BufHost;
32 VOID *Mapping;
33 EFI_PHYSICAL_ADDRESS MappedAddr;
34 EFI_STATUS Status;
35 UINTN PageNumber;
37
38 Mapping = NULL;
39 PageNumber = sizeof (USBHC_MEM_BLOCK)/PAGESIZE +1;
42 PageNumber,
43 &TempPtr
44 );
45
46 if (EFI_ERROR (Status)) {
47 return NULL;
48 }
49
50 ZeroMem ((VOID *)(UINTN)TempPtr, PageNumber*EFI_PAGE_SIZE);
51
52 //
53 // each bit in the bit array represents USBHC_MEM_UNIT
54 // bytes of memory in the memory block.
55 //
56 ASSERT (USBHC_MEM_UNIT * 8 <= EFI_PAGE_SIZE);
57
58 Block = (USBHC_MEM_BLOCK *)(UINTN)TempPtr;
59 Block->BufLen = EFI_PAGES_TO_SIZE (Pages);
60 Block->BitsLen = Block->BufLen / (USBHC_MEM_UNIT * 8);
61
62 PageNumber = (Block->BitsLen)/PAGESIZE +1;
65 PageNumber,
66 &TempPtr
67 );
68
69 if (EFI_ERROR (Status)) {
70 return NULL;
71 }
72
73 ZeroMem ((VOID *)(UINTN)TempPtr, PageNumber*EFI_PAGE_SIZE);
74
75 Block->Bits = (UINT8 *)(UINTN)TempPtr;
76
77 Status = IoMmuAllocateBuffer (
78 Ehc->IoMmu,
79 Pages,
80 (VOID **)&BufHost,
81 &MappedAddr,
82 &Mapping
83 );
84 if (EFI_ERROR (Status)) {
85 return NULL;
86 }
87
88 ZeroMem (BufHost, Pages*EFI_PAGE_SIZE);
89
90 //
91 // Check whether the data structure used by the host controller
92 // should be restricted into the same 4G
93 //
94 if (Pool->Check4G && (Pool->Which4G != USB_HC_HIGH_32BIT (MappedAddr))) {
95 return NULL;
96 }
97
98 Block->BufHost = BufHost;
99 Block->Buf = (UINT8 *)((UINTN)MappedAddr);
100 Block->Mapping = Mapping;
101 Block->Next = NULL;
102
103 return Block;
104}
105
114VOID
116 IN PEI_USB2_HC_DEV *Ehc,
117 IN USBHC_MEM_POOL *Pool,
118 IN USBHC_MEM_BLOCK *Block
119 )
120{
121 ASSERT ((Pool != NULL) && (Block != NULL));
122
123 IoMmuFreeBuffer (Ehc->IoMmu, EFI_SIZE_TO_PAGES (Block->BufLen), Block->BufHost, Block->Mapping);
124}
125
136VOID *
138 IN USBHC_MEM_BLOCK *Block,
139 IN UINTN Units
140 )
141{
142 UINTN Byte;
143 UINT8 Bit;
144 UINTN StartByte;
145 UINT8 StartBit;
146 UINTN Available;
147 UINTN Count;
148
149 ASSERT ((Block != 0) && (Units != 0));
150
151 StartByte = 0;
152 StartBit = 0;
153 Available = 0;
154
155 for (Byte = 0, Bit = 0; Byte < Block->BitsLen;) {
156 //
157 // If current bit is zero, the corresponding memory unit is
158 // available, otherwise we need to restart our searching.
159 // Available counts the consective number of zero bit.
160 //
161 if (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit)) {
162 Available++;
163
164 if (Available >= Units) {
165 break;
166 }
167
168 NEXT_BIT (Byte, Bit);
169 } else {
170 NEXT_BIT (Byte, Bit);
171
172 Available = 0;
173 StartByte = Byte;
174 StartBit = Bit;
175 }
176 }
177
178 if (Available < Units) {
179 return NULL;
180 }
181
182 //
183 // Mark the memory as allocated
184 //
185 Byte = StartByte;
186 Bit = StartBit;
187
188 for (Count = 0; Count < Units; Count++) {
189 ASSERT (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit));
190
191 Block->Bits[Byte] = (UINT8)(Block->Bits[Byte] | (UINT8)USB_HC_BIT (Bit));
192 NEXT_BIT (Byte, Bit);
193 }
194
195 return Block->Buf + (StartByte * 8 + StartBit) * USBHC_MEM_UNIT;
196}
197
209 IN USBHC_MEM_POOL *Pool,
210 IN VOID *Mem,
211 IN UINTN Size
212 )
213{
214 USBHC_MEM_BLOCK *Head;
215 USBHC_MEM_BLOCK *Block;
216 UINTN AllocSize;
217 EFI_PHYSICAL_ADDRESS PhyAddr;
218 UINTN Offset;
219
220 Head = Pool->Head;
221 AllocSize = USBHC_MEM_ROUND (Size);
222
223 if (Mem == NULL) {
224 return 0;
225 }
226
227 for (Block = Head; Block != NULL; Block = Block->Next) {
228 //
229 // scan the memory block list for the memory block that
230 // completely contains the allocated memory.
231 //
232 if ((Block->BufHost <= (UINT8 *)Mem) && (((UINT8 *)Mem + AllocSize) <= (Block->BufHost + Block->BufLen))) {
233 break;
234 }
235 }
236
237 ASSERT ((Block != NULL));
238 //
239 // calculate the pci memory address for host memory address.
240 //
241 Offset = (UINT8 *)Mem - Block->BufHost;
242 PhyAddr = (EFI_PHYSICAL_ADDRESS)(UINTN)(Block->Buf + Offset);
243 return PhyAddr;
244}
245
253VOID
255 IN USBHC_MEM_BLOCK *Head,
256 IN USBHC_MEM_BLOCK *Block
257 )
258{
259 ASSERT ((Head != NULL) && (Block != NULL));
260 Block->Next = Head->Next;
261 Head->Next = Block;
262}
263
273BOOLEAN
275 IN USBHC_MEM_BLOCK *Block
276 )
277{
278 UINTN Index;
279
280 for (Index = 0; Index < Block->BitsLen; Index++) {
281 if (Block->Bits[Index] != 0) {
282 return FALSE;
283 }
284 }
285
286 return TRUE;
287}
288
303 IN PEI_USB2_HC_DEV *Ehc,
304 IN BOOLEAN Check4G,
305 IN UINT32 Which4G
306 )
307{
308 USBHC_MEM_POOL *Pool;
309 UINTN PageNumber;
310 EFI_STATUS Status;
311 EFI_PHYSICAL_ADDRESS TempPtr;
312
313 PageNumber = sizeof (USBHC_MEM_POOL)/PAGESIZE +1;
314 Status = PeiServicesAllocatePages (
316 PageNumber,
317 &TempPtr
318 );
319
320 if (EFI_ERROR (Status)) {
321 return NULL;
322 }
323
324 ZeroMem ((VOID *)(UINTN)TempPtr, PageNumber*EFI_PAGE_SIZE);
325
326 Pool = (USBHC_MEM_POOL *)((UINTN)TempPtr);
327
328 Pool->Check4G = Check4G;
329 Pool->Which4G = Which4G;
330 Pool->Head = UsbHcAllocMemBlock (Ehc, Pool, USBHC_MEM_DEFAULT_PAGES);
331
332 if (Pool->Head == NULL) {
333 Pool = NULL;
334 }
335
336 return Pool;
337}
338
351 IN PEI_USB2_HC_DEV *Ehc,
352 IN USBHC_MEM_POOL *Pool
353 )
354{
355 USBHC_MEM_BLOCK *Block;
356
357 ASSERT (Pool->Head != NULL);
358
359 //
360 // Unlink all the memory blocks from the pool, then free them.
361 //
362 for (Block = Pool->Head->Next; Block != NULL; Block = Block->Next) {
363 UsbHcFreeMemBlock (Ehc, Pool, Block);
364 }
365
366 UsbHcFreeMemBlock (Ehc, Pool, Pool->Head);
367
368 return EFI_SUCCESS;
369}
370
382VOID *
384 IN PEI_USB2_HC_DEV *Ehc,
385 IN USBHC_MEM_POOL *Pool,
386 IN UINTN Size
387 )
388{
389 USBHC_MEM_BLOCK *Head;
390 USBHC_MEM_BLOCK *Block;
391 USBHC_MEM_BLOCK *NewBlock;
392 VOID *Mem;
393 UINTN AllocSize;
394 UINTN Pages;
395
396 Mem = NULL;
397 AllocSize = USBHC_MEM_ROUND (Size);
398 Head = Pool->Head;
399 ASSERT (Head != NULL);
400
401 //
402 // First check whether current memory blocks can satisfy the allocation.
403 //
404 for (Block = Head; Block != NULL; Block = Block->Next) {
405 Mem = UsbHcAllocMemFromBlock (Block, AllocSize / USBHC_MEM_UNIT);
406
407 if (Mem != NULL) {
408 ZeroMem (Mem, Size);
409 break;
410 }
411 }
412
413 if (Mem != NULL) {
414 return Mem;
415 }
416
417 //
418 // Create a new memory block if there is not enough memory
419 // in the pool. If the allocation size is larger than the
420 // default page number, just allocate a large enough memory
421 // block. Otherwise allocate default pages.
422 //
423 if (AllocSize > EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES)) {
424 Pages = EFI_SIZE_TO_PAGES (AllocSize) + 1;
425 } else {
426 Pages = USBHC_MEM_DEFAULT_PAGES;
427 }
428
429 NewBlock = UsbHcAllocMemBlock (Ehc, Pool, Pages);
430
431 if (NewBlock == NULL) {
432 return NULL;
433 }
434
435 //
436 // Add the new memory block to the pool, then allocate memory from it
437 //
438 UsbHcInsertMemBlockToPool (Head, NewBlock);
439 Mem = UsbHcAllocMemFromBlock (NewBlock, AllocSize / USBHC_MEM_UNIT);
440
441 if (Mem != NULL) {
442 ZeroMem (Mem, Size);
443 }
444
445 return Mem;
446}
447
457VOID
459 IN PEI_USB2_HC_DEV *Ehc,
460 IN USBHC_MEM_POOL *Pool,
461 IN VOID *Mem,
462 IN UINTN Size
463 )
464{
465 USBHC_MEM_BLOCK *Head;
466 USBHC_MEM_BLOCK *Block;
467 UINT8 *ToFree;
468 UINTN AllocSize;
469 UINTN Byte;
470 UINTN Bit;
471 UINTN Count;
472
473 Head = Pool->Head;
474 AllocSize = USBHC_MEM_ROUND (Size);
475 ToFree = (UINT8 *)Mem;
476
477 for (Block = Head; Block != NULL; Block = Block->Next) {
478 //
479 // scan the memory block list for the memory block that
480 // completely contains the memory to free.
481 //
482 if ((Block->Buf <= ToFree) && ((ToFree + AllocSize) <= (Block->Buf + Block->BufLen))) {
483 //
484 // compute the start byte and bit in the bit array
485 //
486 Byte = ((ToFree - Block->Buf) / USBHC_MEM_UNIT) / 8;
487 Bit = ((ToFree - Block->Buf) / USBHC_MEM_UNIT) % 8;
488
489 //
490 // reset associated bits in bit array
491 //
492 for (Count = 0; Count < (AllocSize / USBHC_MEM_UNIT); Count++) {
493 ASSERT (USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit));
494
495 Block->Bits[Byte] = (UINT8)(Block->Bits[Byte] ^ USB_HC_BIT (Bit));
496 NEXT_BIT (Byte, Bit);
497 }
498
499 break;
500 }
501 }
502
503 //
504 // If Block == NULL, it means that the current memory isn't
505 // in the host controller's pool. This is critical because
506 // the caller has passed in a wrong memory point
507 //
508 ASSERT (Block != NULL);
509
510 //
511 // Release the current memory block if it is empty and not the head
512 //
513 if ((Block != Head) && UsbHcIsMemBlockEmpty (Block)) {
514 UsbHcFreeMemBlock (Ehc, Pool, Block);
515 }
516
517 return;
518}
UINT64 UINTN
EFI_STATUS IoMmuAllocateBuffer(IN UINTN Pages, OUT VOID **HostAddress, OUT EFI_PHYSICAL_ADDRESS *DeviceAddress, OUT VOID **Mapping)
Definition: DmaMem.c:170
EFI_STATUS IoMmuFreeBuffer(IN UINTN Pages, IN VOID *HostAddress, IN VOID *Mapping)
Definition: DmaMem.c:251
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
EFI_PHYSICAL_ADDRESS UsbHcGetPciAddressForHostMem(IN USBHC_MEM_POOL *Pool, IN VOID *Mem, IN UINTN Size)
Definition: UsbHcMem.c:223
VOID * UsbHcAllocMemFromBlock(IN USBHC_MEM_BLOCK *Block, IN UINTN Units)
Definition: UsbHcMem.c:152
EFI_STATUS UsbHcFreeMemPool(IN USBHC_MEM_POOL *Pool)
Definition: UsbHcMem.c:385
VOID UsbHcFreeMemBlock(IN USBHC_MEM_POOL *Pool, IN USBHC_MEM_BLOCK *Block)
Definition: UsbHcMem.c:120
BOOLEAN UsbHcIsMemBlockEmpty(IN USBHC_MEM_BLOCK *Block)
Definition: UsbHcMem.c:294
VOID UsbHcFreeMem(IN USBHC_MEM_POOL *Pool, IN VOID *Mem, IN UINTN Size)
Definition: UsbHcMem.c:493
VOID * UsbHcAllocateMem(IN USBHC_MEM_POOL *Pool, IN UINTN Size)
Definition: UsbHcMem.c:419
USBHC_MEM_BLOCK * UsbHcAllocMemBlock(IN USBHC_MEM_POOL *Pool, IN UINTN Pages)
Definition: UsbHcMem.c:22
VOID UsbHcInsertMemBlockToPool(IN USBHC_MEM_BLOCK *Head, IN USBHC_MEM_BLOCK *Block)
Definition: UsbHcMem.c:274
USBHC_MEM_POOL * UsbHcInitMemPool(IN EFI_PCI_IO_PROTOCOL *PciIo, IN BOOLEAN Check4G, IN UINT32 Which4G)
Definition: UsbHcMem.c:348
EFI_STATUS EFIAPI PeiServicesAllocatePages(IN EFI_MEMORY_TYPE MemoryType, IN UINTN Pages, OUT EFI_PHYSICAL_ADDRESS *Memory)
#define NULL
Definition: Base.h:319
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
UINT64 EFI_PHYSICAL_ADDRESS
Definition: UefiBaseType.h:50
#define EFI_PAGES_TO_SIZE(Pages)
Definition: UefiBaseType.h:213
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define EFI_SIZE_TO_PAGES(Size)
Definition: UefiBaseType.h:200
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
@ EfiBootServicesCode