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