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