]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/UhciDxe/UsbHcMem.c
For the data region which will be touched by Pci Bus Master, we should allocate memor...
[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 **/
127 VOID
128 UsbHcFreeMemBlock (
129 IN USBHC_MEM_POOL *Pool,
130 IN USBHC_MEM_BLOCK *Block
131 )
132 {
133 EFI_PCI_IO_PROTOCOL *PciIo;
134
135 ASSERT ((Pool != NULL) && (Block != NULL));
136
137 PciIo = Pool->PciIo;
138
139 //
140 // Unmap the common buffer then free the structures
141 //
142 PciIo->Unmap (PciIo, Block->Mapping);
143 PciIo->FreeBuffer (PciIo, EFI_SIZE_TO_PAGES (Block->BufLen), Block->BufHost);
144
145 gBS->FreePool (Block->Bits);
146 gBS->FreePool (Block);
147 }
148
149
150 /**
151 Alloc some memory from the block.
152
153 @param Block The memory block to allocate memory from.
154 @param Units Number of memory units to allocate.
155
156 @return EFI_SUCCESS The needed memory is allocated.
157 @return EFI_NOT_FOUND Can't find the free memory.
158
159 **/
160 VOID *
161 UsbHcAllocMemFromBlock (
162 IN USBHC_MEM_BLOCK *Block,
163 IN UINTN Units
164 )
165 {
166 UINTN Byte;
167 UINT8 Bit;
168 UINTN StartByte;
169 UINT8 StartBit;
170 UINTN Available;
171 UINTN Count;
172
173 ASSERT ((Block != 0) && (Units != 0));
174
175 StartByte = 0;
176 StartBit = 0;
177 Available = 0;
178
179 for (Byte = 0, Bit = 0; Byte < Block->BitsLen;) {
180 //
181 // If current bit is zero, the corresponding memory unit is
182 // available, otherwise we need to restart our searching.
183 // Available counts the consective number of zero bit.
184 //
185 if (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit)) {
186 Available++;
187
188 if (Available >= Units) {
189 break;
190 }
191
192 NEXT_BIT (Byte, Bit);
193
194 } else {
195 NEXT_BIT (Byte, Bit);
196
197 Available = 0;
198 StartByte = Byte;
199 StartBit = Bit;
200 }
201 }
202
203 if (Available < Units) {
204 return NULL;
205 }
206
207 //
208 // Mark the memory as allocated
209 //
210 Byte = StartByte;
211 Bit = StartBit;
212
213 for (Count = 0; Count < Units; Count++) {
214 ASSERT (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit));
215
216 Block->Bits[Byte] = (UINT8) (Block->Bits[Byte] | (UINT8) USB_HC_BIT (Bit));
217 NEXT_BIT (Byte, Bit);
218 }
219
220 return Block->Buf + (StartByte * 8 + StartBit) * USBHC_MEM_UNIT;
221 }
222
223
224 /**
225 Insert the memory block to the pool's list of the blocks.
226
227 @param Head The head of the memory pool's block list.
228 @param Block The memory block to insert.
229
230 **/
231 VOID
232 UsbHcInsertMemBlockToPool (
233 IN USBHC_MEM_BLOCK *Head,
234 IN USBHC_MEM_BLOCK *Block
235 )
236 {
237 ASSERT ((Head != NULL) && (Block != NULL));
238 Block->Next = Head->Next;
239 Head->Next = Block;
240 }
241
242
243 /**
244 Is the memory block empty?
245
246 @param Block The memory block to check.
247
248 @return TRUE The memory block is empty.
249 @return FALSE The memory block isn't empty.
250
251 **/
252 BOOLEAN
253 UsbHcIsMemBlockEmpty (
254 IN USBHC_MEM_BLOCK *Block
255 )
256 {
257 UINTN Index;
258
259 for (Index = 0; Index < Block->BitsLen; Index++) {
260 if (Block->Bits[Index] != 0) {
261 return FALSE;
262 }
263 }
264
265 return TRUE;
266 }
267
268
269 /**
270 Unlink the memory block from the pool's list.
271
272 @param Head The block list head of the memory's pool.
273 @param BlockToUnlink The memory block to unlink.
274
275 **/
276 VOID
277 UsbHcUnlinkMemBlock (
278 IN USBHC_MEM_BLOCK *Head,
279 IN USBHC_MEM_BLOCK *BlockToUnlink
280 )
281 {
282 USBHC_MEM_BLOCK *Block;
283
284 ASSERT ((Head != NULL) && (BlockToUnlink != NULL));
285
286 for (Block = Head; Block != NULL; Block = Block->Next) {
287 if (Block->Next == BlockToUnlink) {
288 Block->Next = BlockToUnlink->Next;
289 BlockToUnlink->Next = NULL;
290 break;
291 }
292 }
293 }
294
295
296 /**
297 Initialize the memory management pool for the host controller.
298
299 @param PciIo The PciIo that can be used to access the host controller.
300 @param Check4G Whether the host controller requires allocated memory
301 from one 4G address space.
302 @param Which4G The 4G memory area each memory allocated should be from.
303
304 @return EFI_SUCCESS The memory pool is initialized.
305 @return EFI_OUT_OF_RESOURCE Fail to init the memory pool.
306
307 **/
308 USBHC_MEM_POOL *
309 UsbHcInitMemPool (
310 IN EFI_PCI_IO_PROTOCOL *PciIo,
311 IN BOOLEAN Check4G,
312 IN UINT32 Which4G
313 )
314 {
315 USBHC_MEM_POOL *Pool;
316
317 Pool = AllocatePool (sizeof (USBHC_MEM_POOL));
318
319 if (Pool == NULL) {
320 return Pool;
321 }
322
323 Pool->PciIo = PciIo;
324 Pool->Check4G = Check4G;
325 Pool->Which4G = Which4G;
326 Pool->Head = UsbHcAllocMemBlock (Pool, USBHC_MEM_DEFAULT_PAGES);
327
328 if (Pool->Head == NULL) {
329 gBS->FreePool (Pool);
330 Pool = NULL;
331 }
332
333 return Pool;
334 }
335
336
337 /**
338 Release the memory management pool.
339
340 @param Pool The USB memory pool to free.
341
342 @return EFI_SUCCESS The memory pool is freed.
343 @return EFI_DEVICE_ERROR Failed to free the memory pool.
344
345 **/
346 EFI_STATUS
347 UsbHcFreeMemPool (
348 IN USBHC_MEM_POOL *Pool
349 )
350 {
351 USBHC_MEM_BLOCK *Block;
352
353 ASSERT (Pool->Head != NULL);
354
355 //
356 // Unlink all the memory blocks from the pool, then free them.
357 // UsbHcUnlinkMemBlock can't be used to unlink and free the
358 // first block.
359 //
360 for (Block = Pool->Head->Next; Block != NULL; Block = Pool->Head->Next) {
361 UsbHcUnlinkMemBlock (Pool->Head, Block);
362 UsbHcFreeMemBlock (Pool, Block);
363 }
364
365 UsbHcFreeMemBlock (Pool, Pool->Head);
366 gBS->FreePool (Pool);
367 return EFI_SUCCESS;
368 }
369
370
371 /**
372 Allocate some memory from the host controller's memory pool
373 which can be used to communicate with host controller.
374
375 @param Pool The host controller's memory pool.
376 @param Size Size of the memory to allocate.
377
378 @return The allocated memory or NULL.
379
380 **/
381 VOID *
382 UsbHcAllocateMem (
383 IN USBHC_MEM_POOL *Pool,
384 IN UINTN Size
385 )
386 {
387 USBHC_MEM_BLOCK *Head;
388 USBHC_MEM_BLOCK *Block;
389 USBHC_MEM_BLOCK *NewBlock;
390 VOID *Mem;
391 UINTN AllocSize;
392 UINTN Pages;
393
394 Mem = NULL;
395 AllocSize = USBHC_MEM_ROUND (Size);
396 Head = Pool->Head;
397 ASSERT (Head != NULL);
398
399 //
400 // First check whether current memory blocks can satisfy the allocation.
401 //
402 for (Block = Head; Block != NULL; Block = Block->Next) {
403 Mem = UsbHcAllocMemFromBlock (Block, AllocSize / USBHC_MEM_UNIT);
404
405 if (Mem != NULL) {
406 ZeroMem (Mem, Size);
407 break;
408 }
409 }
410
411 if (Mem != NULL) {
412 return Mem;
413 }
414
415 //
416 // Create a new memory block if there is not enough memory
417 // in the pool. If the allocation size is larger than the
418 // default page number, just allocate a large enough memory
419 // block. Otherwise allocate default pages.
420 //
421 if (AllocSize > EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES)) {
422 Pages = EFI_SIZE_TO_PAGES (AllocSize) + 1;
423 } else {
424 Pages = USBHC_MEM_DEFAULT_PAGES;
425 }
426
427 NewBlock = UsbHcAllocMemBlock (Pool, Pages);
428
429 if (NewBlock == NULL) {
430 DEBUG ((EFI_D_INFO, "UsbHcAllocateMem: failed to allocate block\n"));
431 return NULL;
432 }
433
434 //
435 // Add the new memory block to the pool, then allocate memory from it
436 //
437 UsbHcInsertMemBlockToPool (Head, NewBlock);
438 Mem = UsbHcAllocMemFromBlock (NewBlock, AllocSize / USBHC_MEM_UNIT);
439
440 if (Mem != NULL) {
441 ZeroMem (Mem, Size);
442 }
443
444 return Mem;
445 }
446
447
448 /**
449 Free the allocated memory back to the memory pool.
450
451 @param Pool The memory pool of the host controller.
452 @param Mem The memory to free.
453 @param Size The size of the memory to free.
454
455 **/
456 VOID
457 UsbHcFreeMem (
458 IN USBHC_MEM_POOL *Pool,
459 IN VOID *Mem,
460 IN UINTN Size
461 )
462 {
463 USBHC_MEM_BLOCK *Head;
464 USBHC_MEM_BLOCK *Block;
465 UINT8 *ToFree;
466 UINTN AllocSize;
467 UINTN Byte;
468 UINTN Bit;
469 UINTN Count;
470
471 Head = Pool->Head;
472 AllocSize = USBHC_MEM_ROUND (Size);
473 ToFree = (UINT8 *) Mem;
474
475 for (Block = Head; Block != NULL; Block = Block->Next) {
476 //
477 // scan the memory block list for the memory block that
478 // completely contains the memory to free.
479 //
480 if ((Block->Buf <= ToFree) && ((ToFree + AllocSize) <= (Block->Buf + Block->BufLen))) {
481 //
482 // compute the start byte and bit in the bit array
483 //
484 Byte = ((ToFree - Block->Buf) / USBHC_MEM_UNIT) / 8;
485 Bit = ((ToFree - Block->Buf) / USBHC_MEM_UNIT) % 8;
486
487 //
488 // reset associated bits in bit arry
489 //
490 for (Count = 0; Count < (AllocSize / USBHC_MEM_UNIT); Count++) {
491 ASSERT (USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit));
492
493 Block->Bits[Byte] = (UINT8) (Block->Bits[Byte] ^ USB_HC_BIT (Bit));
494 NEXT_BIT (Byte, Bit);
495 }
496
497 break;
498 }
499 }
500
501 //
502 // If Block == NULL, it means that the current memory isn't
503 // in the host controller's pool. This is critical because
504 // the caller has passed in a wrong memory point
505 //
506 ASSERT (Block != NULL);
507
508 //
509 // Release the current memory block if it is empty and not the head
510 //
511 if ((Block != Head) && UsbHcIsMemBlockEmpty (Block)) {
512 UsbHcUnlinkMemBlock (Head, Block);
513 UsbHcFreeMemBlock (Pool, Block);
514 }
515
516 return ;
517 }