TianoCore EDK2 master
Loading...
Searching...
No Matches
UsbHcMem.c
Go to the documentation of this file.
1
12#include "XhcPeim.h"
13
24 IN UINTN Pages
25 )
26{
27 USBHC_MEM_BLOCK *Block;
28 VOID *BufHost;
29 VOID *Mapping;
30 EFI_PHYSICAL_ADDRESS MappedAddr;
31 EFI_STATUS Status;
32 UINTN PageNumber;
34
35 PageNumber = EFI_SIZE_TO_PAGES (sizeof (USBHC_MEM_BLOCK));
38 PageNumber,
39 &TempPtr
40 );
41
42 if (EFI_ERROR (Status)) {
43 return NULL;
44 }
45
46 ZeroMem ((VOID *)(UINTN)TempPtr, EFI_PAGES_TO_SIZE (PageNumber));
47
48 //
49 // each bit in the bit array represents USBHC_MEM_UNIT
50 // bytes of memory in the memory block.
51 //
52 ASSERT (USBHC_MEM_UNIT * 8 <= EFI_PAGE_SIZE);
53
54 Block = (USBHC_MEM_BLOCK *)(UINTN)TempPtr;
55 Block->BufLen = EFI_PAGES_TO_SIZE (Pages);
56 Block->BitsLen = Block->BufLen / (USBHC_MEM_UNIT * 8);
57
58 PageNumber = EFI_SIZE_TO_PAGES (Block->BitsLen);
61 PageNumber,
62 &TempPtr
63 );
64
65 if (EFI_ERROR (Status)) {
66 return NULL;
67 }
68
69 ZeroMem ((VOID *)(UINTN)TempPtr, EFI_PAGES_TO_SIZE (PageNumber));
70
71 Block->Bits = (UINT8 *)(UINTN)TempPtr;
72
73 Status = IoMmuAllocateBuffer (
74 Pages,
75 &BufHost,
76 &MappedAddr,
77 &Mapping
78 );
79 if (EFI_ERROR (Status)) {
80 return NULL;
81 }
82
83 ZeroMem ((VOID *)(UINTN)BufHost, EFI_PAGES_TO_SIZE (Pages));
84
85 Block->BufHost = (UINT8 *)(UINTN)BufHost;
86 Block->Buf = (UINT8 *)(UINTN)MappedAddr;
87 Block->Mapping = Mapping;
88 Block->Next = NULL;
89
90 return Block;
91}
92
100VOID
102 IN USBHC_MEM_POOL *Pool,
103 IN USBHC_MEM_BLOCK *Block
104 )
105{
106 ASSERT ((Pool != NULL) && (Block != NULL));
107
108 IoMmuFreeBuffer (EFI_SIZE_TO_PAGES (Block->BufLen), Block->BufHost, Block->Mapping);
109
110 //
111 // No free memory in PEI.
112 //
113}
114
125VOID *
127 IN USBHC_MEM_BLOCK *Block,
128 IN UINTN Units
129 )
130{
131 UINTN Byte;
132 UINT8 Bit;
133 UINTN StartByte;
134 UINT8 StartBit;
135 UINTN Available;
136 UINTN Count;
137
138 ASSERT ((Block != 0) && (Units != 0));
139
140 StartByte = 0;
141 StartBit = 0;
142 Available = 0;
143
144 for (Byte = 0, Bit = 0; Byte < Block->BitsLen;) {
145 //
146 // If current bit is zero, the corresponding memory unit is
147 // available, otherwise we need to restart our searching.
148 // Available counts the consective number of zero bit.
149 //
150 if (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit)) {
151 Available++;
152
153 if (Available >= Units) {
154 break;
155 }
156
157 NEXT_BIT (Byte, Bit);
158 } else {
159 NEXT_BIT (Byte, Bit);
160
161 Available = 0;
162 StartByte = Byte;
163 StartBit = Bit;
164 }
165 }
166
167 if (Available < Units) {
168 return NULL;
169 }
170
171 //
172 // Mark the memory as allocated
173 //
174 Byte = StartByte;
175 Bit = StartBit;
176
177 for (Count = 0; Count < Units; Count++) {
178 ASSERT (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit));
179
180 Block->Bits[Byte] = (UINT8)(Block->Bits[Byte] | (UINT8)USB_HC_BIT (Bit));
181 NEXT_BIT (Byte, Bit);
182 }
183
184 return Block->BufHost + (StartByte * 8 + StartBit) * USBHC_MEM_UNIT;
185}
186
200 IN USBHC_MEM_POOL *Pool,
201 IN VOID *Mem,
202 IN UINTN Size,
203 IN BOOLEAN Alignment
204 )
205{
206 USBHC_MEM_BLOCK *Head;
207 USBHC_MEM_BLOCK *Block;
208 UINTN AllocSize;
209 EFI_PHYSICAL_ADDRESS PhyAddr;
210 UINTN Offset;
211
212 Head = Pool->Head;
213 if (Alignment) {
214 AllocSize = USBHC_MEM_ROUND (Size);
215 } else {
216 AllocSize = Size;
217 }
218
219 if (Mem == NULL) {
220 return 0;
221 }
222
223 for (Block = Head; Block != NULL; Block = Block->Next) {
224 //
225 // scan the memory block list for the memory block that
226 // completely contains the allocated memory.
227 //
228 if ((Block->BufHost <= (UINT8 *)Mem) && (((UINT8 *)Mem + AllocSize) <= (Block->BufHost + Block->BufLen))) {
229 break;
230 }
231 }
232
233 ASSERT ((Block != NULL));
234 //
235 // calculate the pci memory address for host memory address.
236 //
237 Offset = (UINT8 *)Mem - Block->BufHost;
238 PhyAddr = (EFI_PHYSICAL_ADDRESS)(UINTN)(Block->Buf + Offset);
239 return PhyAddr;
240}
241
255 IN USBHC_MEM_POOL *Pool,
256 IN VOID *Mem,
257 IN UINTN Size,
258 IN BOOLEAN Alignment
259 )
260{
261 USBHC_MEM_BLOCK *Head;
262 USBHC_MEM_BLOCK *Block;
263 UINTN AllocSize;
264 EFI_PHYSICAL_ADDRESS HostAddr;
265 UINTN Offset;
266
267 Head = Pool->Head;
268 if (Alignment) {
269 AllocSize = USBHC_MEM_ROUND (Size);
270 } else {
271 AllocSize = Size;
272 }
273
274 if (Mem == NULL) {
275 return 0;
276 }
277
278 for (Block = Head; Block != NULL; Block = Block->Next) {
279 //
280 // scan the memory block list for the memory block that
281 // completely contains the allocated memory.
282 //
283 if ((Block->Buf <= (UINT8 *)Mem) && (((UINT8 *)Mem + AllocSize) <= (Block->Buf + Block->BufLen))) {
284 break;
285 }
286 }
287
288 ASSERT ((Block != NULL));
289 //
290 // calculate the host memory address for pci memory address.
291 //
292 Offset = (UINT8 *)Mem - Block->Buf;
293 HostAddr = (EFI_PHYSICAL_ADDRESS)(UINTN)(Block->BufHost + Offset);
294 return HostAddr;
295}
296
304VOID
306 IN USBHC_MEM_BLOCK *Head,
307 IN USBHC_MEM_BLOCK *Block
308 )
309{
310 ASSERT ((Head != NULL) && (Block != NULL));
311 Block->Next = Head->Next;
312 Head->Next = Block;
313}
314
324BOOLEAN
326 IN USBHC_MEM_BLOCK *Block
327 )
328{
329 UINTN Index;
330
331 for (Index = 0; Index < Block->BitsLen; Index++) {
332 if (Block->Bits[Index] != 0) {
333 return FALSE;
334 }
335 }
336
337 return TRUE;
338}
339
348 VOID
349 )
350{
351 USBHC_MEM_POOL *Pool;
352 UINTN PageNumber;
353 EFI_STATUS Status;
354 EFI_PHYSICAL_ADDRESS TempPtr;
355
356 PageNumber = EFI_SIZE_TO_PAGES (sizeof (USBHC_MEM_POOL));
357 Status = PeiServicesAllocatePages (
359 PageNumber,
360 &TempPtr
361 );
362 if (EFI_ERROR (Status)) {
363 return NULL;
364 }
365
366 ZeroMem ((VOID *)(UINTN)TempPtr, EFI_PAGES_TO_SIZE (PageNumber));
367
368 Pool = (USBHC_MEM_POOL *)((UINTN)TempPtr);
369 Pool->Head = UsbHcAllocMemBlock (USBHC_MEM_DEFAULT_PAGES);
370
371 if (Pool->Head == NULL) {
372 //
373 // No free memory in PEI.
374 //
375 Pool = NULL;
376 }
377
378 return Pool;
379}
380
388VOID
390 IN USBHC_MEM_BLOCK *Head,
391 IN USBHC_MEM_BLOCK *BlockToUnlink
392 )
393{
394 USBHC_MEM_BLOCK *Block;
395
396 ASSERT ((Head != NULL) && (BlockToUnlink != NULL));
397
398 for (Block = Head; Block != NULL; Block = Block->Next) {
399 if (Block->Next == BlockToUnlink) {
400 Block->Next = BlockToUnlink->Next;
401 BlockToUnlink->Next = NULL;
402 break;
403 }
404 }
405}
406
413VOID
415 IN USBHC_MEM_POOL *Pool
416 )
417{
418 USBHC_MEM_BLOCK *Block;
419
420 ASSERT (Pool->Head != NULL);
421
422 //
423 // Unlink all the memory blocks from the pool, then free them.
424 // UsbHcUnlinkMemBlock can't be used to unlink and free the
425 // first block.
426 //
427 for (Block = Pool->Head->Next; Block != NULL; Block = Pool->Head->Next) {
428 UsbHcUnlinkMemBlock (Pool->Head, Block);
429 UsbHcFreeMemBlock (Pool, Block);
430 }
431
432 UsbHcFreeMemBlock (Pool, Pool->Head);
433}
434
445VOID *
447 IN USBHC_MEM_POOL *Pool,
448 IN UINTN Size
449 )
450{
451 USBHC_MEM_BLOCK *Head;
452 USBHC_MEM_BLOCK *Block;
453 USBHC_MEM_BLOCK *NewBlock;
454 VOID *Mem;
455 UINTN AllocSize;
456 UINTN Pages;
457
458 Mem = NULL;
459 AllocSize = USBHC_MEM_ROUND (Size);
460 Head = Pool->Head;
461 ASSERT (Head != NULL);
462
463 //
464 // First check whether current memory blocks can satisfy the allocation.
465 //
466 for (Block = Head; Block != NULL; Block = Block->Next) {
467 Mem = UsbHcAllocMemFromBlock (Block, AllocSize / USBHC_MEM_UNIT);
468
469 if (Mem != NULL) {
470 ZeroMem (Mem, Size);
471 break;
472 }
473 }
474
475 if (Mem != NULL) {
476 return Mem;
477 }
478
479 //
480 // Create a new memory block if there is not enough memory
481 // in the pool. If the allocation size is larger than the
482 // default page number, just allocate a large enough memory
483 // block. Otherwise allocate default pages.
484 //
485 if (AllocSize > EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES)) {
486 Pages = EFI_SIZE_TO_PAGES (AllocSize);
487 } else {
488 Pages = USBHC_MEM_DEFAULT_PAGES;
489 }
490
491 NewBlock = UsbHcAllocMemBlock (Pages);
492
493 if (NewBlock == NULL) {
494 return NULL;
495 }
496
497 //
498 // Add the new memory block to the pool, then allocate memory from it
499 //
500 UsbHcInsertMemBlockToPool (Head, NewBlock);
501 Mem = UsbHcAllocMemFromBlock (NewBlock, AllocSize / USBHC_MEM_UNIT);
502
503 if (Mem != NULL) {
504 ZeroMem (Mem, Size);
505 }
506
507 return Mem;
508}
509
518VOID
520 IN USBHC_MEM_POOL *Pool,
521 IN VOID *Mem,
522 IN UINTN Size
523 )
524{
525 USBHC_MEM_BLOCK *Head;
526 USBHC_MEM_BLOCK *Block;
527 UINT8 *ToFree;
528 UINTN AllocSize;
529 UINTN Byte;
530 UINTN Bit;
531 UINTN Count;
532
533 Head = Pool->Head;
534 AllocSize = USBHC_MEM_ROUND (Size);
535 ToFree = (UINT8 *)Mem;
536
537 for (Block = Head; Block != NULL; Block = Block->Next) {
538 //
539 // scan the memory block list for the memory block that
540 // completely contains the memory to free.
541 //
542 if ((Block->BufHost <= ToFree) && ((ToFree + AllocSize) <= (Block->BufHost + Block->BufLen))) {
543 //
544 // compute the start byte and bit in the bit array
545 //
546 Byte = ((ToFree - Block->BufHost) / USBHC_MEM_UNIT) / 8;
547 Bit = ((ToFree - Block->BufHost) / USBHC_MEM_UNIT) % 8;
548
549 //
550 // reset associated bits in bit array
551 //
552 for (Count = 0; Count < (AllocSize / USBHC_MEM_UNIT); Count++) {
553 ASSERT (USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit));
554
555 Block->Bits[Byte] = (UINT8)(Block->Bits[Byte] ^ USB_HC_BIT (Bit));
556 NEXT_BIT (Byte, Bit);
557 }
558
559 break;
560 }
561 }
562
563 //
564 // If Block == NULL, it means that the current memory isn't
565 // in the host controller's pool. This is critical because
566 // the caller has passed in a wrong memory pointer
567 //
568 ASSERT (Block != NULL);
569
570 //
571 // Release the current memory block if it is empty and not the head
572 //
573 if ((Block != Head) && UsbHcIsMemBlockEmpty (Block)) {
574 UsbHcUnlinkMemBlock (Head, Block);
575 UsbHcFreeMemBlock (Pool, Block);
576 }
577}
578
598 IN UINTN Pages,
599 IN UINTN Alignment,
600 OUT VOID **HostAddress,
601 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
602 OUT VOID **Mapping
603 )
604{
605 EFI_STATUS Status;
606 VOID *Memory;
607 EFI_PHYSICAL_ADDRESS DeviceMemory;
608
609 //
610 // Alignment must be a power of two or zero.
611 //
612 ASSERT ((Alignment & (Alignment - 1)) == 0);
613
614 if ((Alignment & (Alignment - 1)) != 0) {
615 return EFI_INVALID_PARAMETER;
616 }
617
618 if (Pages == 0) {
619 return EFI_INVALID_PARAMETER;
620 }
621
622 if (Alignment > EFI_PAGE_SIZE) {
624 Pages,
625 Alignment,
626 &Memory,
627 &DeviceMemory,
628 Mapping
629 );
630 if (EFI_ERROR (Status)) {
631 return EFI_OUT_OF_RESOURCES;
632 }
633 } else {
634 //
635 // Do not over-allocate pages in this case.
636 //
637 Status = IoMmuAllocateBuffer (
638 Pages,
639 &Memory,
640 &DeviceMemory,
641 Mapping
642 );
643 if (EFI_ERROR (Status)) {
644 return EFI_OUT_OF_RESOURCES;
645 }
646 }
647
648 *HostAddress = Memory;
649 *DeviceAddress = DeviceMemory;
650
651 return EFI_SUCCESS;
652}
653
662VOID
664 IN VOID *HostAddress,
665 IN UINTN Pages,
666 IN VOID *Mapping
667 )
668{
669 ASSERT (Pages != 0);
670
671 IoMmuFreeBuffer (Pages, HostAddress, Mapping);
672}
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)
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 UsbHcUnlinkMemBlock(IN USBHC_MEM_BLOCK *Head, IN USBHC_MEM_BLOCK *BlockToUnlink)
Definition: UsbHcMem.c:317
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
#define OUT
Definition: Base.h:284
EFI_STATUS IoMmuAllocateAlignedBuffer(IN UINTN Pages, IN UINTN Alignment, OUT VOID **HostAddress, OUT EFI_PHYSICAL_ADDRESS *DeviceAddress, OUT VOID **Mapping)
Definition: DmaMem.c:255
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
@ EfiBootServicesData
EFI_PHYSICAL_ADDRESS UsbHcGetPciAddrForHostAddr(IN USBHC_MEM_POOL *Pool, IN VOID *Mem, IN UINTN Size, IN BOOLEAN Alignment)
Definition: UsbHcMem.c:235
EFI_STATUS UsbHcAllocateAlignedPages(IN EFI_PCI_IO_PROTOCOL *PciIo, IN UINTN Pages, IN UINTN Alignment, OUT VOID **HostAddress, OUT EFI_PHYSICAL_ADDRESS *DeviceAddress, OUT VOID **Mapping)
Definition: UsbHcMem.c:637
EFI_PHYSICAL_ADDRESS UsbHcGetHostAddrForPciAddr(IN USBHC_MEM_POOL *Pool, IN VOID *Mem, IN UINTN Size, IN BOOLEAN Alignment)
Definition: UsbHcMem.c:290
VOID UsbHcFreeAlignedPages(IN EFI_PCI_IO_PROTOCOL *PciIo, IN VOID *HostAddress, IN UINTN Pages, VOID *Mapping)
Definition: UsbHcMem.c:758