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