]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/EhciPei/UsbHcMem.c
MdeModulePkg: Replace BSD License with BSD+Patent License
[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 ZeroMem ((VOID *)(UINTN)TempPtr, PageNumber*EFI_PAGE_SIZE);
50
51 //
52 // each bit in the bit array represents USBHC_MEM_UNIT
53 // bytes of memory in the memory block.
54 //
55 ASSERT (USBHC_MEM_UNIT * 8 <= EFI_PAGE_SIZE);
56
57 Block = (USBHC_MEM_BLOCK*)(UINTN)TempPtr;
58 Block->BufLen = EFI_PAGES_TO_SIZE (Pages);
59 Block->BitsLen = Block->BufLen / (USBHC_MEM_UNIT * 8);
60
61 PageNumber = (Block->BitsLen)/PAGESIZE +1;
62 Status = PeiServicesAllocatePages (
63 EfiBootServicesCode,
64 PageNumber,
65 &TempPtr
66 );
67
68 if (EFI_ERROR (Status)) {
69 return NULL;
70 }
71 ZeroMem ((VOID *)(UINTN)TempPtr, PageNumber*EFI_PAGE_SIZE);
72
73 Block->Bits = (UINT8 *)(UINTN)TempPtr;
74
75 Status = IoMmuAllocateBuffer (
76 Ehc->IoMmu,
77 Pages,
78 (VOID **) &BufHost,
79 &MappedAddr,
80 &Mapping
81 );
82 if (EFI_ERROR (Status)) {
83 return NULL;
84 }
85 ZeroMem (BufHost, Pages*EFI_PAGE_SIZE);
86
87 //
88 // Check whether the data structure used by the host controller
89 // should be restricted into the same 4G
90 //
91 if (Pool->Check4G && (Pool->Which4G != USB_HC_HIGH_32BIT (MappedAddr))) {
92 return NULL;
93 }
94
95 Block->BufHost = BufHost;
96 Block->Buf = (UINT8 *) ((UINTN) MappedAddr);
97 Block->Mapping = Mapping;
98 Block->Next = NULL;
99
100 return Block;
101
102 }
103
104 /**
105 Free the memory block from the memory pool.
106
107 @param Ehc The EHCI device.
108 @param Pool The memory pool to free the block from.
109 @param Block The memory block to free.
110
111 **/
112 VOID
113 UsbHcFreeMemBlock (
114 IN PEI_USB2_HC_DEV *Ehc,
115 IN USBHC_MEM_POOL *Pool,
116 IN USBHC_MEM_BLOCK *Block
117 )
118 {
119 ASSERT ((Pool != NULL) && (Block != NULL));
120
121 IoMmuFreeBuffer (Ehc->IoMmu, EFI_SIZE_TO_PAGES (Block->BufLen), Block->BufHost, Block->Mapping);
122 }
123
124 /**
125 Alloc some memory from the block.
126
127 @param Block The memory block to allocate memory from.
128 @param Units Number of memory units to allocate.
129
130 @return The pointer to the allocated memory. If couldn't allocate the needed memory,
131 the return value is NULL.
132
133 **/
134 VOID *
135 UsbHcAllocMemFromBlock (
136 IN USBHC_MEM_BLOCK *Block,
137 IN UINTN Units
138 )
139 {
140 UINTN Byte;
141 UINT8 Bit;
142 UINTN StartByte;
143 UINT8 StartBit;
144 UINTN Available;
145 UINTN Count;
146
147 ASSERT ((Block != 0) && (Units != 0));
148
149 StartByte = 0;
150 StartBit = 0;
151 Available = 0;
152
153 for (Byte = 0, Bit = 0; Byte < Block->BitsLen;) {
154 //
155 // If current bit is zero, the corresponding memory unit is
156 // available, otherwise we need to restart our searching.
157 // Available counts the consective number of zero bit.
158 //
159 if (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit)) {
160 Available++;
161
162 if (Available >= Units) {
163 break;
164 }
165
166 NEXT_BIT (Byte, Bit);
167
168 } else {
169 NEXT_BIT (Byte, Bit);
170
171 Available = 0;
172 StartByte = Byte;
173 StartBit = Bit;
174 }
175 }
176
177 if (Available < Units) {
178 return NULL;
179 }
180
181 //
182 // Mark the memory as allocated
183 //
184 Byte = StartByte;
185 Bit = StartBit;
186
187 for (Count = 0; Count < Units; Count++) {
188 ASSERT (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit));
189
190 Block->Bits[Byte] = (UINT8) (Block->Bits[Byte] | (UINT8) USB_HC_BIT (Bit));
191 NEXT_BIT (Byte, Bit);
192 }
193
194 return Block->Buf + (StartByte * 8 + StartBit) * USBHC_MEM_UNIT;
195 }
196
197 /**
198 Calculate the corresponding pci bus address according to the Mem parameter.
199
200 @param Pool The memory pool of the host controller.
201 @param Mem The pointer to host memory.
202 @param Size The size of the memory region.
203
204 @return the pci memory address
205 **/
206 EFI_PHYSICAL_ADDRESS
207 UsbHcGetPciAddressForHostMem (
208 IN USBHC_MEM_POOL *Pool,
209 IN VOID *Mem,
210 IN UINTN Size
211 )
212 {
213 USBHC_MEM_BLOCK *Head;
214 USBHC_MEM_BLOCK *Block;
215 UINTN AllocSize;
216 EFI_PHYSICAL_ADDRESS PhyAddr;
217 UINTN Offset;
218
219 Head = Pool->Head;
220 AllocSize = USBHC_MEM_ROUND (Size);
221
222 if (Mem == NULL) {
223 return 0;
224 }
225
226 for (Block = Head; Block != NULL; Block = Block->Next) {
227 //
228 // scan the memory block list for the memory block that
229 // completely contains the allocated memory.
230 //
231 if ((Block->BufHost <= (UINT8 *) Mem) && (((UINT8 *) Mem + AllocSize) <= (Block->BufHost + Block->BufLen))) {
232 break;
233 }
234 }
235
236 ASSERT ((Block != NULL));
237 //
238 // calculate the pci memory address for host memory address.
239 //
240 Offset = (UINT8 *)Mem - Block->BufHost;
241 PhyAddr = (EFI_PHYSICAL_ADDRESS)(UINTN) (Block->Buf + Offset);
242 return PhyAddr;
243 }
244
245 /**
246 Insert the memory block to the pool's list of the blocks.
247
248 @param Head The head of the memory pool's block list.
249 @param Block The memory block to insert.
250
251 **/
252 VOID
253 UsbHcInsertMemBlockToPool (
254 IN USBHC_MEM_BLOCK *Head,
255 IN USBHC_MEM_BLOCK *Block
256 )
257 {
258 ASSERT ((Head != NULL) && (Block != NULL));
259 Block->Next = Head->Next;
260 Head->Next = Block;
261 }
262
263 /**
264 Is the memory block empty?
265
266 @param Block The memory block to check.
267
268 @retval TRUE The memory block is empty.
269 @retval FALSE The memory block isn't empty.
270
271 **/
272 BOOLEAN
273 UsbHcIsMemBlockEmpty (
274 IN USBHC_MEM_BLOCK *Block
275 )
276 {
277 UINTN Index;
278
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 /**
291 Initialize the memory management pool for the host controller.
292
293 @param Ehc The EHCI device.
294 @param Check4G Whether the host controller requires allocated memory.
295 from one 4G address space.
296 @param Which4G The 4G memory area each memory allocated should be from.
297
298 @retval EFI_SUCCESS The memory pool is initialized.
299 @retval EFI_OUT_OF_RESOURCE Fail to init the memory pool.
300
301 **/
302 USBHC_MEM_POOL *
303 UsbHcInitMemPool (
304 IN PEI_USB2_HC_DEV *Ehc,
305 IN BOOLEAN Check4G,
306 IN UINT32 Which4G
307 )
308 {
309 USBHC_MEM_POOL *Pool;
310 UINTN PageNumber;
311 EFI_STATUS Status;
312 EFI_PHYSICAL_ADDRESS TempPtr;
313
314 PageNumber = sizeof(USBHC_MEM_POOL)/PAGESIZE +1;
315 Status = PeiServicesAllocatePages (
316 EfiBootServicesCode,
317 PageNumber,
318 &TempPtr
319 );
320
321 if (EFI_ERROR (Status)) {
322 return NULL;
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 NewBlock = UsbHcAllocMemBlock (Ehc,Pool, Pages);
429
430 if (NewBlock == NULL) {
431 return NULL;
432 }
433
434 //
435 // Add the new memory block to the pool, then allocate memory from it
436 //
437 UsbHcInsertMemBlockToPool (Head, NewBlock);
438 Mem = UsbHcAllocMemFromBlock (NewBlock, AllocSize / USBHC_MEM_UNIT);
439
440 if (Mem != NULL) {
441 ZeroMem (Mem, Size);
442 }
443
444 return Mem;
445 }
446
447 /**
448 Free the allocated memory back to the memory pool.
449
450 @param Ehc The EHCI device.
451 @param Pool The memory pool of the host controller.
452 @param Mem The memory to free.
453 @param Size The size of the memory to free.
454
455 **/
456 VOID
457 UsbHcFreeMem (
458 IN PEI_USB2_HC_DEV *Ehc,
459 IN USBHC_MEM_POOL *Pool,
460 IN VOID *Mem,
461 IN UINTN Size
462 )
463 {
464 USBHC_MEM_BLOCK *Head;
465 USBHC_MEM_BLOCK *Block;
466 UINT8 *ToFree;
467 UINTN AllocSize;
468 UINTN Byte;
469 UINTN Bit;
470 UINTN Count;
471
472 Head = Pool->Head;
473 AllocSize = USBHC_MEM_ROUND (Size);
474 ToFree = (UINT8 *) Mem;
475
476 for (Block = Head; Block != NULL; Block = Block->Next) {
477 //
478 // scan the memory block list for the memory block that
479 // completely contains the memory to free.
480 //
481 if ((Block->Buf <= ToFree) && ((ToFree + AllocSize) <= (Block->Buf + Block->BufLen))) {
482 //
483 // compute the start byte and bit in the bit array
484 //
485 Byte = ((ToFree - Block->Buf) / USBHC_MEM_UNIT) / 8;
486 Bit = ((ToFree - Block->Buf) / USBHC_MEM_UNIT) % 8;
487
488 //
489 // reset associated bits in bit array
490 //
491 for (Count = 0; Count < (AllocSize / USBHC_MEM_UNIT); Count++) {
492 ASSERT (USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit));
493
494 Block->Bits[Byte] = (UINT8) (Block->Bits[Byte] ^ USB_HC_BIT (Bit));
495 NEXT_BIT (Byte, Bit);
496 }
497
498 break;
499 }
500 }
501
502 //
503 // If Block == NULL, it means that the current memory isn't
504 // in the host controller's pool. This is critical because
505 // the caller has passed in a wrong memory point
506 //
507 ASSERT (Block != NULL);
508
509 //
510 // Release the current memory block if it is empty and not the head
511 //
512 if ((Block != Head) && UsbHcIsMemBlockEmpty (Block)) {
513 UsbHcFreeMemBlock (Ehc, Pool, Block);
514 }
515
516 return ;
517 }