]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/EhciPei/UsbHcMem.c
MdeModulePkg EhciPei: Remove a redundant function
[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 /**
298 Initialize the memory management pool for the host controller.
299
300 @param Ehc The EHCI device.
301 @param Check4G Whether the host controller requires allocated memory.
302 from one 4G address space.
303 @param Which4G The 4G memory area each memory allocated should be from.
304
305 @retval EFI_SUCCESS The memory pool is initialized.
306 @retval EFI_OUT_OF_RESOURCE Fail to init the memory pool.
307
308 **/
309 USBHC_MEM_POOL *
310 UsbHcInitMemPool (
311 IN PEI_USB2_HC_DEV *Ehc,
312 IN BOOLEAN Check4G,
313 IN UINT32 Which4G
314 )
315 {
316 USBHC_MEM_POOL *Pool;
317 UINTN PageNumber;
318 EFI_STATUS Status;
319 EFI_PHYSICAL_ADDRESS TempPtr;
320
321 PageNumber = sizeof(USBHC_MEM_POOL)/PAGESIZE +1;
322 Status = PeiServicesAllocatePages (
323 EfiBootServicesCode,
324 PageNumber,
325 &TempPtr
326 );
327
328 if (EFI_ERROR (Status)) {
329 return NULL;
330 }
331 ZeroMem ((VOID *)(UINTN)TempPtr, PageNumber*EFI_PAGE_SIZE);
332
333 Pool = (USBHC_MEM_POOL *) ((UINTN) TempPtr);
334
335 Pool->Check4G = Check4G;
336 Pool->Which4G = Which4G;
337 Pool->Head = UsbHcAllocMemBlock (Ehc, Pool, USBHC_MEM_DEFAULT_PAGES);
338
339 if (Pool->Head == NULL) {
340 Pool = NULL;
341 }
342
343 return Pool;
344 }
345
346 /**
347 Release the memory management pool.
348
349 @param Ehc The EHCI device.
350 @param Pool The USB memory pool to free.
351
352 @retval EFI_DEVICE_ERROR Fail to free the memory pool.
353 @retval EFI_SUCCESS The memory pool is freed.
354
355 **/
356 EFI_STATUS
357 UsbHcFreeMemPool (
358 IN PEI_USB2_HC_DEV *Ehc,
359 IN USBHC_MEM_POOL *Pool
360 )
361 {
362 USBHC_MEM_BLOCK *Block;
363
364 ASSERT (Pool->Head != NULL);
365
366 //
367 // Unlink all the memory blocks from the pool, then free them.
368 //
369 for (Block = Pool->Head->Next; Block != NULL; Block = Block->Next) {
370 UsbHcFreeMemBlock (Ehc, Pool, Block);
371 }
372
373 UsbHcFreeMemBlock (Ehc, Pool, Pool->Head);
374
375 return EFI_SUCCESS;
376 }
377
378 /**
379 Allocate some memory from the host controller's memory pool
380 which can be used to communicate with host controller.
381
382 @param Ehc The EHCI device.
383 @param Pool The host controller's memory pool.
384 @param Size Size of the memory to allocate.
385
386 @return The allocated memory or NULL.
387
388 **/
389 VOID *
390 UsbHcAllocateMem (
391 IN PEI_USB2_HC_DEV *Ehc,
392 IN USBHC_MEM_POOL *Pool,
393 IN UINTN Size
394 )
395 {
396 USBHC_MEM_BLOCK *Head;
397 USBHC_MEM_BLOCK *Block;
398 USBHC_MEM_BLOCK *NewBlock;
399 VOID *Mem;
400 UINTN AllocSize;
401 UINTN Pages;
402
403 Mem = NULL;
404 AllocSize = USBHC_MEM_ROUND (Size);
405 Head = Pool->Head;
406 ASSERT (Head != NULL);
407
408 //
409 // First check whether current memory blocks can satisfy the allocation.
410 //
411 for (Block = Head; Block != NULL; Block = Block->Next) {
412 Mem = UsbHcAllocMemFromBlock (Block, AllocSize / USBHC_MEM_UNIT);
413
414 if (Mem != NULL) {
415 ZeroMem (Mem, Size);
416 break;
417 }
418 }
419
420 if (Mem != NULL) {
421 return Mem;
422 }
423
424 //
425 // Create a new memory block if there is not enough memory
426 // in the pool. If the allocation size is larger than the
427 // default page number, just allocate a large enough memory
428 // block. Otherwise allocate default pages.
429 //
430 if (AllocSize > EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES)) {
431 Pages = EFI_SIZE_TO_PAGES (AllocSize) + 1;
432 } else {
433 Pages = USBHC_MEM_DEFAULT_PAGES;
434 }
435 NewBlock = UsbHcAllocMemBlock (Ehc,Pool, Pages);
436
437 if (NewBlock == NULL) {
438 return NULL;
439 }
440
441 //
442 // Add the new memory block to the pool, then allocate memory from it
443 //
444 UsbHcInsertMemBlockToPool (Head, NewBlock);
445 Mem = UsbHcAllocMemFromBlock (NewBlock, AllocSize / USBHC_MEM_UNIT);
446
447 if (Mem != NULL) {
448 ZeroMem (Mem, Size);
449 }
450
451 return Mem;
452 }
453
454 /**
455 Free the allocated memory back to the memory pool.
456
457 @param Ehc The EHCI device.
458 @param Pool The memory pool of the host controller.
459 @param Mem The memory to free.
460 @param Size The size of the memory to free.
461
462 **/
463 VOID
464 UsbHcFreeMem (
465 IN PEI_USB2_HC_DEV *Ehc,
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 array
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 UsbHcFreeMemBlock (Ehc, Pool, Block);
521 }
522
523 return ;
524 }