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