1 /** @file
2 
3   The routine procedure for uhci memory allocate/free.
4 
5 Copyright (c) 2007 - 2016, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7 
8 **/
9 
10 #include "Uhci.h"
11 
12 
13 /**
14   Allocate a block of memory to be used by the buffer pool.
15 
16   @param  Pool           The buffer pool to allocate memory for.
17   @param  Pages          How many pages to allocate.
18 
19   @return The allocated memory block or NULL if failed.
20 
21 **/
22 USBHC_MEM_BLOCK *
UsbHcAllocMemBlock(IN USBHC_MEM_POOL * Pool,IN UINTN Pages)23 UsbHcAllocMemBlock (
24   IN  USBHC_MEM_POOL      *Pool,
25   IN  UINTN               Pages
26   )
27 {
28   USBHC_MEM_BLOCK         *Block;
29   EFI_PCI_IO_PROTOCOL     *PciIo;
30   VOID                    *BufHost;
31   VOID                    *Mapping;
32   EFI_PHYSICAL_ADDRESS    MappedAddr;
33   UINTN                   Bytes;
34   EFI_STATUS              Status;
35 
36   PciIo = Pool->PciIo;
37 
38   Block = AllocateZeroPool (sizeof (USBHC_MEM_BLOCK));
39   if (Block == NULL) {
40     return NULL;
41   }
42 
43   //
44   // each bit in the bit array represents USBHC_MEM_UNIT
45   // bytes of memory in the memory block.
46   //
47   ASSERT (USBHC_MEM_UNIT * 8 <= EFI_PAGE_SIZE);
48 
49   Block->BufLen   = EFI_PAGES_TO_SIZE (Pages);
50   Block->BitsLen  = Block->BufLen / (USBHC_MEM_UNIT * 8);
51   Block->Bits     = AllocateZeroPool (Block->BitsLen);
52 
53   if (Block->Bits == NULL) {
54     gBS->FreePool (Block);
55     return NULL;
56   }
57 
58   //
59   // Allocate the number of Pages of memory, then map it for
60   // bus master read and write.
61   //
62   Status = PciIo->AllocateBuffer (
63                     PciIo,
64                     AllocateAnyPages,
65                     EfiBootServicesData,
66                     Pages,
67                     &BufHost,
68                     0
69                     );
70 
71   if (EFI_ERROR (Status)) {
72     goto FREE_BITARRAY;
73   }
74 
75   Bytes = EFI_PAGES_TO_SIZE (Pages);
76   Status = PciIo->Map (
77                     PciIo,
78                     EfiPciIoOperationBusMasterCommonBuffer,
79                     BufHost,
80                     &Bytes,
81                     &MappedAddr,
82                     &Mapping
83                     );
84 
85   if (EFI_ERROR (Status) || (Bytes != EFI_PAGES_TO_SIZE (Pages))) {
86     goto FREE_BUFFER;
87   }
88 
89   //
90   // Check whether the data structure used by the host controller
91   // should be restricted into the same 4G
92   //
93   if (Pool->Check4G && (Pool->Which4G != USB_HC_HIGH_32BIT (MappedAddr))) {
94     PciIo->Unmap (PciIo, Mapping);
95     goto FREE_BUFFER;
96   }
97 
98   Block->BufHost  = BufHost;
99   Block->Buf      = (UINT8 *) ((UINTN) MappedAddr);
100   Block->Mapping  = Mapping;
101 
102   return Block;
103 
104 FREE_BUFFER:
105   PciIo->FreeBuffer (PciIo, Pages, BufHost);
106 
107 FREE_BITARRAY:
108   gBS->FreePool (Block->Bits);
109   gBS->FreePool (Block);
110   return NULL;
111 }
112 
113 
114 /**
115   Free the memory block from the memory pool.
116 
117   @param  Pool           The memory pool to free the block from.
118   @param  Block          The memory block to free.
119 
120 **/
121 VOID
UsbHcFreeMemBlock(IN USBHC_MEM_POOL * Pool,IN USBHC_MEM_BLOCK * Block)122 UsbHcFreeMemBlock (
123   IN USBHC_MEM_POOL       *Pool,
124   IN USBHC_MEM_BLOCK      *Block
125   )
126 {
127   EFI_PCI_IO_PROTOCOL     *PciIo;
128 
129   ASSERT ((Pool != NULL) && (Block != NULL));
130 
131   PciIo = Pool->PciIo;
132 
133   //
134   // Unmap the common buffer then free the structures
135   //
136   PciIo->Unmap (PciIo, Block->Mapping);
137   PciIo->FreeBuffer (PciIo, EFI_SIZE_TO_PAGES (Block->BufLen), Block->BufHost);
138 
139   gBS->FreePool (Block->Bits);
140   gBS->FreePool (Block);
141 }
142 
143 
144 /**
145   Alloc some memory from the block.
146 
147   @param  Block           The memory block to allocate memory from.
148   @param  Units           Number of memory units to allocate.
149 
150   @return EFI_SUCCESS     The needed memory is allocated.
151   @return EFI_NOT_FOUND   Can't find the free memory.
152 
153 **/
154 VOID *
UsbHcAllocMemFromBlock(IN USBHC_MEM_BLOCK * Block,IN UINTN Units)155 UsbHcAllocMemFromBlock (
156   IN  USBHC_MEM_BLOCK     *Block,
157   IN  UINTN               Units
158   )
159 {
160   UINTN                   Byte;
161   UINT8                   Bit;
162   UINTN                   StartByte;
163   UINT8                   StartBit;
164   UINTN                   Available;
165   UINTN                   Count;
166 
167   ASSERT ((Block != 0) && (Units != 0));
168 
169   StartByte  = 0;
170   StartBit   = 0;
171   Available  = 0;
172 
173   for (Byte = 0, Bit = 0; Byte < Block->BitsLen;) {
174     //
175     // If current bit is zero, the corresponding memory unit is
176     // available, otherwise we need to restart our searching.
177     // Available counts the consective number of zero bit.
178     //
179     if (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit)) {
180       Available++;
181 
182       if (Available >= Units) {
183         break;
184       }
185 
186       NEXT_BIT (Byte, Bit);
187 
188     } else {
189       NEXT_BIT (Byte, Bit);
190 
191       Available  = 0;
192       StartByte  = Byte;
193       StartBit   = Bit;
194     }
195   }
196 
197   if (Available < Units) {
198     return NULL;
199   }
200 
201   //
202   // Mark the memory as allocated
203   //
204   Byte  = StartByte;
205   Bit   = StartBit;
206 
207   for (Count = 0; Count < Units; Count++) {
208     ASSERT (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit));
209 
210     Block->Bits[Byte] = (UINT8) (Block->Bits[Byte] | (UINT8) USB_HC_BIT (Bit));
211     NEXT_BIT (Byte, Bit);
212   }
213 
214   return Block->Buf + (StartByte * 8 + StartBit) * USBHC_MEM_UNIT;
215 }
216 
217 /**
218   Calculate the corresponding pci bus address according to the Mem parameter.
219 
220   @param  Pool           The memory pool of the host controller.
221   @param  Mem            The pointer to host memory.
222   @param  Size           The size of the memory region.
223 
224   @return the pci memory address
225 **/
226 EFI_PHYSICAL_ADDRESS
UsbHcGetPciAddressForHostMem(IN USBHC_MEM_POOL * Pool,IN VOID * Mem,IN UINTN Size)227 UsbHcGetPciAddressForHostMem (
228   IN USBHC_MEM_POOL       *Pool,
229   IN VOID                 *Mem,
230   IN UINTN                Size
231   )
232 {
233   USBHC_MEM_BLOCK         *Head;
234   USBHC_MEM_BLOCK         *Block;
235   UINTN                   AllocSize;
236   EFI_PHYSICAL_ADDRESS    PhyAddr;
237   UINTN                   Offset;
238 
239   Head      = Pool->Head;
240   AllocSize = USBHC_MEM_ROUND (Size);
241 
242   if (Mem == NULL) {
243     return 0;
244   }
245 
246   for (Block = Head; Block != NULL; Block = Block->Next) {
247     //
248     // scan the memory block list for the memory block that
249     // completely contains the allocated memory.
250     //
251     if ((Block->BufHost <= (UINT8 *) Mem) && (((UINT8 *) Mem + AllocSize) <= (Block->BufHost + Block->BufLen))) {
252       break;
253     }
254   }
255 
256   ASSERT ((Block != NULL));
257   //
258   // calculate the pci memory address for host memory address.
259   //
260   Offset = (UINT8 *)Mem - Block->BufHost;
261   PhyAddr = (EFI_PHYSICAL_ADDRESS)(UINTN) (Block->Buf + Offset);
262   return PhyAddr;
263 }
264 
265 /**
266   Insert the memory block to the pool's list of the blocks.
267 
268   @param  Head           The head of the memory pool's block list.
269   @param  Block          The memory block to insert.
270 
271 **/
272 VOID
UsbHcInsertMemBlockToPool(IN USBHC_MEM_BLOCK * Head,IN USBHC_MEM_BLOCK * Block)273 UsbHcInsertMemBlockToPool (
274   IN USBHC_MEM_BLOCK      *Head,
275   IN USBHC_MEM_BLOCK      *Block
276   )
277 {
278   ASSERT ((Head != NULL) && (Block != NULL));
279   Block->Next = Head->Next;
280   Head->Next  = Block;
281 }
282 
283 
284 /**
285   Is the memory block empty?
286 
287   @param  Block     The memory block to check.
288 
289   @return TRUE      The memory block is empty.
290   @return FALSE     The memory block isn't empty.
291 
292 **/
293 BOOLEAN
UsbHcIsMemBlockEmpty(IN USBHC_MEM_BLOCK * Block)294 UsbHcIsMemBlockEmpty (
295   IN USBHC_MEM_BLOCK     *Block
296   )
297 {
298   UINTN                   Index;
299 
300   for (Index = 0; Index < Block->BitsLen; Index++) {
301     if (Block->Bits[Index] != 0) {
302       return FALSE;
303     }
304   }
305 
306   return TRUE;
307 }
308 
309 
310 /**
311   Unlink the memory block from the pool's list.
312 
313   @param  Head           The block list head of the memory's pool.
314   @param  BlockToUnlink  The memory block to unlink.
315 
316 **/
317 VOID
UsbHcUnlinkMemBlock(IN USBHC_MEM_BLOCK * Head,IN USBHC_MEM_BLOCK * BlockToUnlink)318 UsbHcUnlinkMemBlock (
319   IN USBHC_MEM_BLOCK      *Head,
320   IN USBHC_MEM_BLOCK      *BlockToUnlink
321   )
322 {
323   USBHC_MEM_BLOCK         *Block;
324 
325   ASSERT ((Head != NULL) && (BlockToUnlink != NULL));
326 
327   for (Block = Head; Block != NULL; Block = Block->Next) {
328     if (Block->Next == BlockToUnlink) {
329       Block->Next         = BlockToUnlink->Next;
330       BlockToUnlink->Next = NULL;
331       break;
332     }
333   }
334 }
335 
336 
337 /**
338   Initialize the memory management pool for the host controller.
339 
340   @param  PciIo                 The PciIo that can be used to access the host controller.
341   @param  Check4G               Whether the host controller requires allocated memory
342                                 from one 4G address space.
343   @param  Which4G               The 4G memory area each memory allocated should be from.
344 
345   @return EFI_SUCCESS           The memory pool is initialized.
346   @return EFI_OUT_OF_RESOURCE   Fail to init the memory pool.
347 
348 **/
349 USBHC_MEM_POOL *
UsbHcInitMemPool(IN EFI_PCI_IO_PROTOCOL * PciIo,IN BOOLEAN Check4G,IN UINT32 Which4G)350 UsbHcInitMemPool (
351   IN EFI_PCI_IO_PROTOCOL  *PciIo,
352   IN BOOLEAN              Check4G,
353   IN UINT32               Which4G
354   )
355 {
356   USBHC_MEM_POOL          *Pool;
357 
358   Pool = AllocatePool (sizeof (USBHC_MEM_POOL));
359 
360   if (Pool == NULL) {
361     return Pool;
362   }
363 
364   Pool->PciIo   = PciIo;
365   Pool->Check4G = Check4G;
366   Pool->Which4G = Which4G;
367   Pool->Head    = UsbHcAllocMemBlock (Pool, USBHC_MEM_DEFAULT_PAGES);
368 
369   if (Pool->Head == NULL) {
370     gBS->FreePool (Pool);
371     Pool = NULL;
372   }
373 
374   return Pool;
375 }
376 
377 
378 /**
379   Release the memory management pool.
380 
381   @param  Pool               The USB memory pool to free.
382 
383   @return EFI_SUCCESS        The memory pool is freed.
384   @return EFI_DEVICE_ERROR   Failed to free the memory pool.
385 
386 **/
387 EFI_STATUS
UsbHcFreeMemPool(IN USBHC_MEM_POOL * Pool)388 UsbHcFreeMemPool (
389   IN USBHC_MEM_POOL       *Pool
390   )
391 {
392   USBHC_MEM_BLOCK *Block;
393 
394   ASSERT (Pool->Head != NULL);
395 
396   //
397   // Unlink all the memory blocks from the pool, then free them.
398   // UsbHcUnlinkMemBlock can't be used to unlink and free the
399   // first block.
400   //
401   for (Block = Pool->Head->Next; Block != NULL; Block = Pool->Head->Next) {
402     UsbHcUnlinkMemBlock (Pool->Head, Block);
403     UsbHcFreeMemBlock (Pool, Block);
404   }
405 
406   UsbHcFreeMemBlock (Pool, Pool->Head);
407   gBS->FreePool (Pool);
408   return EFI_SUCCESS;
409 }
410 
411 
412 /**
413   Allocate some memory from the host controller's memory pool
414   which can be used to communicate with host controller.
415 
416   @param  Pool           The host controller's memory pool.
417   @param  Size           Size of the memory to allocate.
418 
419   @return The allocated memory or NULL.
420 
421 **/
422 VOID *
UsbHcAllocateMem(IN USBHC_MEM_POOL * Pool,IN UINTN Size)423 UsbHcAllocateMem (
424   IN  USBHC_MEM_POOL      *Pool,
425   IN  UINTN               Size
426   )
427 {
428   USBHC_MEM_BLOCK         *Head;
429   USBHC_MEM_BLOCK         *Block;
430   USBHC_MEM_BLOCK         *NewBlock;
431   VOID                    *Mem;
432   UINTN                   AllocSize;
433   UINTN                   Pages;
434 
435   Mem       = NULL;
436   AllocSize = USBHC_MEM_ROUND (Size);
437   Head      = Pool->Head;
438   ASSERT (Head != NULL);
439 
440   //
441   // First check whether current memory blocks can satisfy the allocation.
442   //
443   for (Block = Head; Block != NULL; Block = Block->Next) {
444     Mem = UsbHcAllocMemFromBlock (Block, AllocSize / USBHC_MEM_UNIT);
445 
446     if (Mem != NULL) {
447       ZeroMem (Mem, Size);
448       break;
449     }
450   }
451 
452   if (Mem != NULL) {
453     return Mem;
454   }
455 
456   //
457   // Create a new memory block if there is not enough memory
458   // in the pool. If the allocation size is larger than the
459   // default page number, just allocate a large enough memory
460   // block. Otherwise allocate default pages.
461   //
462   if (AllocSize > EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES)) {
463     Pages = EFI_SIZE_TO_PAGES (AllocSize) + 1;
464   } else {
465     Pages = USBHC_MEM_DEFAULT_PAGES;
466   }
467 
468   NewBlock = UsbHcAllocMemBlock (Pool, Pages);
469 
470   if (NewBlock == NULL) {
471     DEBUG ((EFI_D_INFO, "UsbHcAllocateMem: failed to allocate block\n"));
472     return NULL;
473   }
474 
475   //
476   // Add the new memory block to the pool, then allocate memory from it
477   //
478   UsbHcInsertMemBlockToPool (Head, NewBlock);
479   Mem = UsbHcAllocMemFromBlock (NewBlock, AllocSize / USBHC_MEM_UNIT);
480 
481   if (Mem != NULL) {
482     ZeroMem (Mem, Size);
483   }
484 
485   return Mem;
486 }
487 
488 
489 /**
490   Free the allocated memory back to the memory pool.
491 
492   @param  Pool           The memory pool of the host controller.
493   @param  Mem            The memory to free.
494   @param  Size           The size of the memory to free.
495 
496 **/
497 VOID
UsbHcFreeMem(IN USBHC_MEM_POOL * Pool,IN VOID * Mem,IN UINTN Size)498 UsbHcFreeMem (
499   IN USBHC_MEM_POOL       *Pool,
500   IN VOID                 *Mem,
501   IN UINTN                Size
502   )
503 {
504   USBHC_MEM_BLOCK         *Head;
505   USBHC_MEM_BLOCK         *Block;
506   UINT8                   *ToFree;
507   UINTN                   AllocSize;
508   UINTN                   Byte;
509   UINTN                   Bit;
510   UINTN                   Count;
511 
512   Head      = Pool->Head;
513   AllocSize = USBHC_MEM_ROUND (Size);
514   ToFree    = (UINT8 *) Mem;
515 
516   for (Block = Head; Block != NULL; Block = Block->Next) {
517     //
518     // scan the memory block list for the memory block that
519     // completely contains the memory to free.
520     //
521     if ((Block->Buf <= ToFree) && ((ToFree + AllocSize) <= (Block->Buf + Block->BufLen))) {
522       //
523       // compute the start byte and bit in the bit array
524       //
525       Byte  = ((ToFree - Block->Buf) / USBHC_MEM_UNIT) / 8;
526       Bit   = ((ToFree - Block->Buf) / USBHC_MEM_UNIT) % 8;
527 
528       //
529       // reset associated bits in bit array
530       //
531       for (Count = 0; Count < (AllocSize / USBHC_MEM_UNIT); Count++) {
532         ASSERT (USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit));
533 
534         Block->Bits[Byte] = (UINT8) (Block->Bits[Byte] ^ USB_HC_BIT (Bit));
535         NEXT_BIT (Byte, Bit);
536       }
537 
538       break;
539     }
540   }
541 
542   //
543   // If Block == NULL, it means that the current memory isn't
544   // in the host controller's pool. This is critical because
545   // the caller has passed in a wrong memory point
546   //
547   ASSERT (Block != NULL);
548 
549   //
550   // Release the current memory block if it is empty and not the head
551   //
552   if ((Block != Head) && UsbHcIsMemBlockEmpty (Block)) {
553     UsbHcUnlinkMemBlock (Head, Block);
554     UsbHcFreeMemBlock (Pool, Block);
555   }
556 
557   return ;
558 }
559