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