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