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