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