]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/XhciDxe/UsbHcMem.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / XhciDxe / UsbHcMem.c
1 /** @file
2
3 Routine procedures for memory allocate/free.
4
5 Copyright (c) 2013 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10
11 #include "Xhci.h"
12
13
14 /**
15 Allocate a block of memory to be used by the buffer pool.
16
17 @param Pool The buffer pool to allocate memory for.
18 @param Pages How many pages to allocate.
19
20 @return The allocated memory block or NULL if failed.
21
22 **/
23 USBHC_MEM_BLOCK *
24 UsbHcAllocMemBlock (
25 IN USBHC_MEM_POOL *Pool,
26 IN UINTN Pages
27 )
28 {
29 USBHC_MEM_BLOCK *Block;
30 EFI_PCI_IO_PROTOCOL *PciIo;
31 VOID *BufHost;
32 VOID *Mapping;
33 EFI_PHYSICAL_ADDRESS MappedAddr;
34 UINTN Bytes;
35 EFI_STATUS Status;
36
37 PciIo = Pool->PciIo;
38
39 Block = AllocateZeroPool (sizeof (USBHC_MEM_BLOCK));
40 if (Block == NULL) {
41 return NULL;
42 }
43
44 //
45 // each bit in the bit array represents USBHC_MEM_UNIT
46 // bytes of memory in the memory block.
47 //
48 ASSERT (USBHC_MEM_UNIT * 8 <= EFI_PAGE_SIZE);
49
50 Block->BufLen = EFI_PAGES_TO_SIZE (Pages);
51 Block->BitsLen = Block->BufLen / (USBHC_MEM_UNIT * 8);
52 Block->Bits = AllocateZeroPool (Block->BitsLen);
53
54 if (Block->Bits == NULL) {
55 gBS->FreePool (Block);
56 return NULL;
57 }
58
59 //
60 // Allocate the number of Pages of memory, then map it for
61 // bus master read and write.
62 //
63 Status = PciIo->AllocateBuffer (
64 PciIo,
65 AllocateAnyPages,
66 EfiBootServicesData,
67 Pages,
68 &BufHost,
69 0
70 );
71
72 if (EFI_ERROR (Status)) {
73 goto FREE_BITARRAY;
74 }
75
76 Bytes = EFI_PAGES_TO_SIZE (Pages);
77 Status = PciIo->Map (
78 PciIo,
79 EfiPciIoOperationBusMasterCommonBuffer,
80 BufHost,
81 &Bytes,
82 &MappedAddr,
83 &Mapping
84 );
85
86 if (EFI_ERROR (Status) || (Bytes != EFI_PAGES_TO_SIZE (Pages))) {
87 goto FREE_BUFFER;
88 }
89
90 Block->BufHost = BufHost;
91 Block->Buf = (UINT8 *) ((UINTN) MappedAddr);
92 Block->Mapping = Mapping;
93
94 return Block;
95
96 FREE_BUFFER:
97 PciIo->FreeBuffer (PciIo, Pages, BufHost);
98
99 FREE_BITARRAY:
100 gBS->FreePool (Block->Bits);
101 gBS->FreePool (Block);
102 return NULL;
103 }
104
105
106 /**
107 Free the memory block from the memory pool.
108
109 @param Pool The memory pool to free the block from.
110 @param Block The memory block to free.
111
112 **/
113 VOID
114 UsbHcFreeMemBlock (
115 IN USBHC_MEM_POOL *Pool,
116 IN USBHC_MEM_BLOCK *Block
117 )
118 {
119 EFI_PCI_IO_PROTOCOL *PciIo;
120
121 ASSERT ((Pool != NULL) && (Block != NULL));
122
123 PciIo = Pool->PciIo;
124
125 //
126 // Unmap the common buffer then free the structures
127 //
128 PciIo->Unmap (PciIo, Block->Mapping);
129 PciIo->FreeBuffer (PciIo, EFI_SIZE_TO_PAGES (Block->BufLen), Block->BufHost);
130
131 gBS->FreePool (Block->Bits);
132 gBS->FreePool (Block);
133 }
134
135
136 /**
137 Alloc some memory from the block.
138
139 @param Block The memory block to allocate memory from.
140 @param Units Number of memory units to allocate.
141
142 @return The pointer to the allocated memory. If couldn't allocate the needed memory,
143 the return value is NULL.
144
145 **/
146 VOID *
147 UsbHcAllocMemFromBlock (
148 IN USBHC_MEM_BLOCK *Block,
149 IN UINTN Units
150 )
151 {
152 UINTN Byte;
153 UINT8 Bit;
154 UINTN StartByte;
155 UINT8 StartBit;
156 UINTN Available;
157 UINTN Count;
158
159 ASSERT ((Block != 0) && (Units != 0));
160
161 StartByte = 0;
162 StartBit = 0;
163 Available = 0;
164
165 for (Byte = 0, Bit = 0; Byte < Block->BitsLen;) {
166 //
167 // If current bit is zero, the corresponding memory unit is
168 // available, otherwise we need to restart our searching.
169 // Available counts the consective number of zero bit.
170 //
171 if (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit)) {
172 Available++;
173
174 if (Available >= Units) {
175 break;
176 }
177
178 NEXT_BIT (Byte, Bit);
179
180 } else {
181 NEXT_BIT (Byte, Bit);
182
183 Available = 0;
184 StartByte = Byte;
185 StartBit = Bit;
186 }
187 }
188
189 if (Available < Units) {
190 return NULL;
191 }
192
193 //
194 // Mark the memory as allocated
195 //
196 Byte = StartByte;
197 Bit = StartBit;
198
199 for (Count = 0; Count < Units; Count++) {
200 ASSERT (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit));
201
202 Block->Bits[Byte] = (UINT8) (Block->Bits[Byte] | USB_HC_BIT (Bit));
203 NEXT_BIT (Byte, Bit);
204 }
205
206 return Block->BufHost + (StartByte * 8 + StartBit) * USBHC_MEM_UNIT;
207 }
208
209 /**
210 Calculate the corresponding pci bus address according to the Mem parameter.
211
212 @param Pool The memory pool of the host controller.
213 @param Mem The pointer to host memory.
214 @param Size The size of the memory region.
215
216 @return The pci memory address
217
218 **/
219 EFI_PHYSICAL_ADDRESS
220 UsbHcGetPciAddrForHostAddr (
221 IN USBHC_MEM_POOL *Pool,
222 IN VOID *Mem,
223 IN UINTN Size
224 )
225 {
226 USBHC_MEM_BLOCK *Head;
227 USBHC_MEM_BLOCK *Block;
228 UINTN AllocSize;
229 EFI_PHYSICAL_ADDRESS PhyAddr;
230 UINTN Offset;
231
232 Head = Pool->Head;
233 AllocSize = USBHC_MEM_ROUND (Size);
234
235 if (Mem == NULL) {
236 return 0;
237 }
238
239 for (Block = Head; Block != NULL; Block = Block->Next) {
240 //
241 // scan the memory block list for the memory block that
242 // completely contains the allocated memory.
243 //
244 if ((Block->BufHost <= (UINT8 *) Mem) && (((UINT8 *) Mem + AllocSize) <= (Block->BufHost + Block->BufLen))) {
245 break;
246 }
247 }
248
249 ASSERT ((Block != NULL));
250 //
251 // calculate the pci memory address for host memory address.
252 //
253 Offset = (UINT8 *)Mem - Block->BufHost;
254 PhyAddr = (EFI_PHYSICAL_ADDRESS)(UINTN) (Block->Buf + Offset);
255 return PhyAddr;
256 }
257
258 /**
259 Calculate the corresponding host address according to the pci address.
260
261 @param Pool The memory pool of the host controller.
262 @param Mem The pointer to pci memory.
263 @param Size The size of the memory region.
264
265 @return The host memory address
266
267 **/
268 EFI_PHYSICAL_ADDRESS
269 UsbHcGetHostAddrForPciAddr (
270 IN USBHC_MEM_POOL *Pool,
271 IN VOID *Mem,
272 IN UINTN Size
273 )
274 {
275 USBHC_MEM_BLOCK *Head;
276 USBHC_MEM_BLOCK *Block;
277 UINTN AllocSize;
278 EFI_PHYSICAL_ADDRESS HostAddr;
279 UINTN Offset;
280
281 Head = Pool->Head;
282 AllocSize = USBHC_MEM_ROUND (Size);
283
284 if (Mem == NULL) {
285 return 0;
286 }
287
288 for (Block = Head; Block != NULL; Block = Block->Next) {
289 //
290 // scan the memory block list for the memory block that
291 // completely contains the allocated memory.
292 //
293 if ((Block->Buf <= (UINT8 *) Mem) && (((UINT8 *) Mem + AllocSize) <= (Block->Buf + Block->BufLen))) {
294 break;
295 }
296 }
297
298 ASSERT ((Block != NULL));
299 //
300 // calculate the pci memory address for host memory address.
301 //
302 Offset = (UINT8 *)Mem - Block->Buf;
303 HostAddr = (EFI_PHYSICAL_ADDRESS)(UINTN) (Block->BufHost + Offset);
304 return HostAddr;
305 }
306
307 /**
308 Insert the memory block to the pool's list of the blocks.
309
310 @param Head The head of the memory pool's block list.
311 @param Block The memory block to insert.
312
313 **/
314 VOID
315 UsbHcInsertMemBlockToPool (
316 IN USBHC_MEM_BLOCK *Head,
317 IN USBHC_MEM_BLOCK *Block
318 )
319 {
320 ASSERT ((Head != NULL) && (Block != NULL));
321 Block->Next = Head->Next;
322 Head->Next = Block;
323 }
324
325
326 /**
327 Is the memory block empty?
328
329 @param Block The memory block to check.
330
331 @retval TRUE The memory block is empty.
332 @retval FALSE The memory block isn't empty.
333
334 **/
335 BOOLEAN
336 UsbHcIsMemBlockEmpty (
337 IN USBHC_MEM_BLOCK *Block
338 )
339 {
340 UINTN Index;
341
342 for (Index = 0; Index < Block->BitsLen; Index++) {
343 if (Block->Bits[Index] != 0) {
344 return FALSE;
345 }
346 }
347
348 return TRUE;
349 }
350
351
352 /**
353 Unlink the memory block from the pool's list.
354
355 @param Head The block list head of the memory's pool.
356 @param BlockToUnlink The memory block to unlink.
357
358 **/
359 VOID
360 UsbHcUnlinkMemBlock (
361 IN USBHC_MEM_BLOCK *Head,
362 IN USBHC_MEM_BLOCK *BlockToUnlink
363 )
364 {
365 USBHC_MEM_BLOCK *Block;
366
367 ASSERT ((Head != NULL) && (BlockToUnlink != NULL));
368
369 for (Block = Head; Block != NULL; Block = Block->Next) {
370 if (Block->Next == BlockToUnlink) {
371 Block->Next = BlockToUnlink->Next;
372 BlockToUnlink->Next = NULL;
373 break;
374 }
375 }
376 }
377
378
379 /**
380 Initialize the memory management pool for the host controller.
381
382 @param PciIo The PciIo that can be used to access the host controller.
383
384 @retval EFI_SUCCESS The memory pool is initialized.
385 @retval EFI_OUT_OF_RESOURCE Fail to init the memory pool.
386
387 **/
388 USBHC_MEM_POOL *
389 UsbHcInitMemPool (
390 IN EFI_PCI_IO_PROTOCOL *PciIo
391 )
392 {
393 USBHC_MEM_POOL *Pool;
394
395 Pool = AllocatePool (sizeof (USBHC_MEM_POOL));
396
397 if (Pool == NULL) {
398 return Pool;
399 }
400
401 Pool->PciIo = PciIo;
402 Pool->Head = UsbHcAllocMemBlock (Pool, USBHC_MEM_DEFAULT_PAGES);
403
404 if (Pool->Head == NULL) {
405 gBS->FreePool (Pool);
406 Pool = NULL;
407 }
408
409 return Pool;
410 }
411
412
413 /**
414 Release the memory management pool.
415
416 @param Pool The USB memory pool to free.
417
418 @retval EFI_SUCCESS The memory pool is freed.
419 @retval EFI_DEVICE_ERROR Failed to free the memory pool.
420
421 **/
422 EFI_STATUS
423 UsbHcFreeMemPool (
424 IN USBHC_MEM_POOL *Pool
425 )
426 {
427 USBHC_MEM_BLOCK *Block;
428
429 ASSERT (Pool->Head != NULL);
430
431 //
432 // Unlink all the memory blocks from the pool, then free them.
433 // UsbHcUnlinkMemBlock can't be used to unlink and free the
434 // first block.
435 //
436 for (Block = Pool->Head->Next; Block != NULL; Block = Pool->Head->Next) {
437 UsbHcUnlinkMemBlock (Pool->Head, Block);
438 UsbHcFreeMemBlock (Pool, Block);
439 }
440
441 UsbHcFreeMemBlock (Pool, Pool->Head);
442 gBS->FreePool (Pool);
443 return EFI_SUCCESS;
444 }
445
446
447 /**
448 Allocate some memory from the host controller's memory pool
449 which can be used to communicate with host controller.
450
451 @param Pool The host controller's memory pool.
452 @param Size Size of the memory to allocate.
453
454 @return The allocated memory or NULL.
455
456 **/
457 VOID *
458 UsbHcAllocateMem (
459 IN USBHC_MEM_POOL *Pool,
460 IN UINTN Size
461 )
462 {
463 USBHC_MEM_BLOCK *Head;
464 USBHC_MEM_BLOCK *Block;
465 USBHC_MEM_BLOCK *NewBlock;
466 VOID *Mem;
467 UINTN AllocSize;
468 UINTN Pages;
469
470 Mem = NULL;
471 AllocSize = USBHC_MEM_ROUND (Size);
472 Head = Pool->Head;
473 ASSERT (Head != NULL);
474
475 //
476 // First check whether current memory blocks can satisfy the allocation.
477 //
478 for (Block = Head; Block != NULL; Block = Block->Next) {
479 Mem = UsbHcAllocMemFromBlock (Block, AllocSize / USBHC_MEM_UNIT);
480
481 if (Mem != NULL) {
482 ZeroMem (Mem, Size);
483 break;
484 }
485 }
486
487 if (Mem != NULL) {
488 return Mem;
489 }
490
491 //
492 // Create a new memory block if there is not enough memory
493 // in the pool. If the allocation size is larger than the
494 // default page number, just allocate a large enough memory
495 // block. Otherwise allocate default pages.
496 //
497 if (AllocSize > EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES)) {
498 Pages = EFI_SIZE_TO_PAGES (AllocSize) + 1;
499 } else {
500 Pages = USBHC_MEM_DEFAULT_PAGES;
501 }
502
503 NewBlock = UsbHcAllocMemBlock (Pool, Pages);
504
505 if (NewBlock == NULL) {
506 DEBUG ((EFI_D_ERROR, "UsbHcAllocateMem: failed to allocate block\n"));
507 return NULL;
508 }
509
510 //
511 // Add the new memory block to the pool, then allocate memory from it
512 //
513 UsbHcInsertMemBlockToPool (Head, NewBlock);
514 Mem = UsbHcAllocMemFromBlock (NewBlock, AllocSize / USBHC_MEM_UNIT);
515
516 if (Mem != NULL) {
517 ZeroMem (Mem, Size);
518 }
519
520 return Mem;
521 }
522
523
524 /**
525 Free the allocated memory back to the memory pool.
526
527 @param Pool The memory pool of the host controller.
528 @param Mem The memory to free.
529 @param Size The size of the memory to free.
530
531 **/
532 VOID
533 UsbHcFreeMem (
534 IN USBHC_MEM_POOL *Pool,
535 IN VOID *Mem,
536 IN UINTN Size
537 )
538 {
539 USBHC_MEM_BLOCK *Head;
540 USBHC_MEM_BLOCK *Block;
541 UINT8 *ToFree;
542 UINTN AllocSize;
543 UINTN Byte;
544 UINTN Bit;
545 UINTN Count;
546
547 Head = Pool->Head;
548 AllocSize = USBHC_MEM_ROUND (Size);
549 ToFree = (UINT8 *) Mem;
550
551 for (Block = Head; Block != NULL; Block = Block->Next) {
552 //
553 // scan the memory block list for the memory block that
554 // completely contains the memory to free.
555 //
556 if ((Block->BufHost <= ToFree) && ((ToFree + AllocSize) <= (Block->BufHost + Block->BufLen))) {
557 //
558 // compute the start byte and bit in the bit array
559 //
560 Byte = ((ToFree - Block->BufHost) / USBHC_MEM_UNIT) / 8;
561 Bit = ((ToFree - Block->BufHost) / USBHC_MEM_UNIT) % 8;
562
563 //
564 // reset associated bits in bit array
565 //
566 for (Count = 0; Count < (AllocSize / USBHC_MEM_UNIT); Count++) {
567 ASSERT (USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit));
568
569 Block->Bits[Byte] = (UINT8) (Block->Bits[Byte] ^ USB_HC_BIT (Bit));
570 NEXT_BIT (Byte, Bit);
571 }
572
573 break;
574 }
575 }
576
577 //
578 // If Block == NULL, it means that the current memory isn't
579 // in the host controller's pool. This is critical because
580 // the caller has passed in a wrong memory point
581 //
582 ASSERT (Block != NULL);
583
584 //
585 // Release the current memory block if it is empty and not the head
586 //
587 if ((Block != Head) && UsbHcIsMemBlockEmpty (Block)) {
588 UsbHcUnlinkMemBlock (Head, Block);
589 UsbHcFreeMemBlock (Pool, Block);
590 }
591
592 return ;
593 }
594
595 /**
596 Allocates pages at a specified alignment that are suitable for an EfiPciIoOperationBusMasterCommonBuffer mapping.
597
598 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
599
600 @param PciIo The PciIo that can be used to access the host controller.
601 @param Pages The number of pages to allocate.
602 @param Alignment The requested alignment of the allocation. Must be a power of two.
603 @param HostAddress The system memory address to map to the PCI controller.
604 @param DeviceAddress The resulting map address for the bus master PCI controller to
605 use to access the hosts HostAddress.
606 @param Mapping A resulting value to pass to Unmap().
607
608 @retval EFI_SUCCESS Success to allocate aligned pages.
609 @retval EFI_INVALID_PARAMETER Pages or Alignment is not valid.
610 @retval EFI_OUT_OF_RESOURCES Do not have enough resources to allocate memory.
611
612
613 **/
614 EFI_STATUS
615 UsbHcAllocateAlignedPages (
616 IN EFI_PCI_IO_PROTOCOL *PciIo,
617 IN UINTN Pages,
618 IN UINTN Alignment,
619 OUT VOID **HostAddress,
620 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
621 OUT VOID **Mapping
622 )
623 {
624 EFI_STATUS Status;
625 VOID *Memory;
626 UINTN AlignedMemory;
627 UINTN AlignmentMask;
628 UINTN UnalignedPages;
629 UINTN RealPages;
630 UINTN Bytes;
631
632 //
633 // Alignment must be a power of two or zero.
634 //
635 ASSERT ((Alignment & (Alignment - 1)) == 0);
636
637 if ((Alignment & (Alignment - 1)) != 0) {
638 return EFI_INVALID_PARAMETER;
639 }
640
641 if (Pages == 0) {
642 return EFI_INVALID_PARAMETER;
643 }
644 if (Alignment > EFI_PAGE_SIZE) {
645 //
646 // Calculate the total number of pages since alignment is larger than page size.
647 //
648 AlignmentMask = Alignment - 1;
649 RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment);
650 //
651 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
652 //
653 ASSERT (RealPages > Pages);
654
655 Status = PciIo->AllocateBuffer (
656 PciIo,
657 AllocateAnyPages,
658 EfiBootServicesData,
659 Pages,
660 &Memory,
661 0
662 );
663 if (EFI_ERROR (Status)) {
664 return EFI_OUT_OF_RESOURCES;
665 }
666 AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;
667 UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);
668 if (UnalignedPages > 0) {
669 //
670 // Free first unaligned page(s).
671 //
672 Status = PciIo->FreeBuffer (PciIo, UnalignedPages, Memory);
673 ASSERT_EFI_ERROR (Status);
674 }
675 Memory = (VOID *)(UINTN)(AlignedMemory + EFI_PAGES_TO_SIZE (Pages));
676 UnalignedPages = RealPages - Pages - UnalignedPages;
677 if (UnalignedPages > 0) {
678 //
679 // Free last unaligned page(s).
680 //
681 Status = PciIo->FreeBuffer (PciIo, UnalignedPages, Memory);
682 ASSERT_EFI_ERROR (Status);
683 }
684 } else {
685 //
686 // Do not over-allocate pages in this case.
687 //
688 Status = PciIo->AllocateBuffer (
689 PciIo,
690 AllocateAnyPages,
691 EfiBootServicesData,
692 Pages,
693 &Memory,
694 0
695 );
696 if (EFI_ERROR (Status)) {
697 return EFI_OUT_OF_RESOURCES;
698 }
699 AlignedMemory = (UINTN) Memory;
700 }
701
702 Bytes = EFI_PAGES_TO_SIZE (Pages);
703 Status = PciIo->Map (
704 PciIo,
705 EfiPciIoOperationBusMasterCommonBuffer,
706 (VOID *) AlignedMemory,
707 &Bytes,
708 DeviceAddress,
709 Mapping
710 );
711
712 if (EFI_ERROR (Status) || (Bytes != EFI_PAGES_TO_SIZE (Pages))) {
713 Status = PciIo->FreeBuffer (PciIo, Pages, (VOID *) AlignedMemory);
714 return EFI_OUT_OF_RESOURCES;
715 }
716
717 *HostAddress = (VOID *) AlignedMemory;
718
719 return EFI_SUCCESS;
720 }
721
722 /**
723 Frees memory that was allocated with UsbHcAllocateAlignedPages().
724
725 @param PciIo The PciIo that can be used to access the host controller.
726 @param HostAddress The system memory address to map to the PCI controller.
727 @param Pages The number of 4 KB pages to free.
728 @param Mapping The mapping value returned from Map().
729
730 **/
731 VOID
732 UsbHcFreeAlignedPages (
733 IN EFI_PCI_IO_PROTOCOL *PciIo,
734 IN VOID *HostAddress,
735 IN UINTN Pages,
736 VOID *Mapping
737 )
738 {
739 EFI_STATUS Status;
740
741 ASSERT (Pages != 0);
742
743 Status = PciIo->Unmap (PciIo, Mapping);
744 ASSERT_EFI_ERROR (Status);
745
746 Status = PciIo->FreeBuffer (
747 PciIo,
748 Pages,
749 HostAddress
750 );
751 ASSERT_EFI_ERROR (Status);
752 }