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