]>
Commit | Line | Data |
---|---|---|
1 | /** @file\r | |
2 | \r | |
3 | Routine procedures for memory allocate/free.\r | |
4 | \r | |
5 | Copyright (c) 2013 - 2015, Intel Corporation. All rights reserved.<BR>\r | |
6 | This program and the accompanying materials\r | |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | \r | |
17 | #include "Xhci.h"\r | |
18 | \r | |
19 | \r | |
20 | /**\r | |
21 | Allocate a block of memory to be used by the buffer pool.\r | |
22 | \r | |
23 | @param Pool The buffer pool to allocate memory for.\r | |
24 | @param Pages How many pages to allocate.\r | |
25 | \r | |
26 | @return The allocated memory block or NULL if failed.\r | |
27 | \r | |
28 | **/\r | |
29 | USBHC_MEM_BLOCK *\r | |
30 | UsbHcAllocMemBlock (\r | |
31 | IN USBHC_MEM_POOL *Pool,\r | |
32 | IN UINTN Pages\r | |
33 | )\r | |
34 | {\r | |
35 | USBHC_MEM_BLOCK *Block;\r | |
36 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
37 | VOID *BufHost;\r | |
38 | VOID *Mapping;\r | |
39 | EFI_PHYSICAL_ADDRESS MappedAddr;\r | |
40 | UINTN Bytes;\r | |
41 | EFI_STATUS Status;\r | |
42 | \r | |
43 | PciIo = Pool->PciIo;\r | |
44 | \r | |
45 | Block = AllocateZeroPool (sizeof (USBHC_MEM_BLOCK));\r | |
46 | if (Block == NULL) {\r | |
47 | return NULL;\r | |
48 | }\r | |
49 | \r | |
50 | //\r | |
51 | // each bit in the bit array represents USBHC_MEM_UNIT\r | |
52 | // bytes of memory in the memory block.\r | |
53 | //\r | |
54 | ASSERT (USBHC_MEM_UNIT * 8 <= EFI_PAGE_SIZE);\r | |
55 | \r | |
56 | Block->BufLen = EFI_PAGES_TO_SIZE (Pages);\r | |
57 | Block->BitsLen = Block->BufLen / (USBHC_MEM_UNIT * 8);\r | |
58 | Block->Bits = AllocateZeroPool (Block->BitsLen);\r | |
59 | \r | |
60 | if (Block->Bits == NULL) {\r | |
61 | gBS->FreePool (Block);\r | |
62 | return NULL;\r | |
63 | }\r | |
64 | \r | |
65 | //\r | |
66 | // Allocate the number of Pages of memory, then map it for\r | |
67 | // bus master read and write.\r | |
68 | //\r | |
69 | Status = PciIo->AllocateBuffer (\r | |
70 | PciIo,\r | |
71 | AllocateAnyPages,\r | |
72 | EfiBootServicesData,\r | |
73 | Pages,\r | |
74 | &BufHost,\r | |
75 | 0\r | |
76 | );\r | |
77 | \r | |
78 | if (EFI_ERROR (Status)) {\r | |
79 | goto FREE_BITARRAY;\r | |
80 | }\r | |
81 | \r | |
82 | Bytes = EFI_PAGES_TO_SIZE (Pages);\r | |
83 | Status = PciIo->Map (\r | |
84 | PciIo,\r | |
85 | EfiPciIoOperationBusMasterCommonBuffer,\r | |
86 | BufHost,\r | |
87 | &Bytes,\r | |
88 | &MappedAddr,\r | |
89 | &Mapping\r | |
90 | );\r | |
91 | \r | |
92 | if (EFI_ERROR (Status) || (Bytes != EFI_PAGES_TO_SIZE (Pages))) {\r | |
93 | goto FREE_BUFFER;\r | |
94 | }\r | |
95 | \r | |
96 | Block->BufHost = BufHost;\r | |
97 | Block->Buf = (UINT8 *) ((UINTN) MappedAddr);\r | |
98 | Block->Mapping = Mapping;\r | |
99 | \r | |
100 | return Block;\r | |
101 | \r | |
102 | FREE_BUFFER:\r | |
103 | PciIo->FreeBuffer (PciIo, Pages, BufHost);\r | |
104 | \r | |
105 | FREE_BITARRAY:\r | |
106 | gBS->FreePool (Block->Bits);\r | |
107 | gBS->FreePool (Block);\r | |
108 | return NULL;\r | |
109 | }\r | |
110 | \r | |
111 | \r | |
112 | /**\r | |
113 | Free the memory block from the memory pool.\r | |
114 | \r | |
115 | @param Pool The memory pool to free the block from.\r | |
116 | @param Block The memory block to free.\r | |
117 | \r | |
118 | **/\r | |
119 | VOID\r | |
120 | UsbHcFreeMemBlock (\r | |
121 | IN USBHC_MEM_POOL *Pool,\r | |
122 | IN USBHC_MEM_BLOCK *Block\r | |
123 | )\r | |
124 | {\r | |
125 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
126 | \r | |
127 | ASSERT ((Pool != NULL) && (Block != NULL));\r | |
128 | \r | |
129 | PciIo = Pool->PciIo;\r | |
130 | \r | |
131 | //\r | |
132 | // Unmap the common buffer then free the structures\r | |
133 | //\r | |
134 | PciIo->Unmap (PciIo, Block->Mapping);\r | |
135 | PciIo->FreeBuffer (PciIo, EFI_SIZE_TO_PAGES (Block->BufLen), Block->BufHost);\r | |
136 | \r | |
137 | gBS->FreePool (Block->Bits);\r | |
138 | gBS->FreePool (Block);\r | |
139 | }\r | |
140 | \r | |
141 | \r | |
142 | /**\r | |
143 | Alloc some memory from the block.\r | |
144 | \r | |
145 | @param Block The memory block to allocate memory from.\r | |
146 | @param Units Number of memory units to allocate.\r | |
147 | \r | |
148 | @return The pointer to the allocated memory. If couldn't allocate the needed memory,\r | |
149 | the return value is NULL.\r | |
150 | \r | |
151 | **/\r | |
152 | VOID *\r | |
153 | UsbHcAllocMemFromBlock (\r | |
154 | IN USBHC_MEM_BLOCK *Block,\r | |
155 | IN UINTN Units\r | |
156 | )\r | |
157 | {\r | |
158 | UINTN Byte;\r | |
159 | UINT8 Bit;\r | |
160 | UINTN StartByte;\r | |
161 | UINT8 StartBit;\r | |
162 | UINTN Available;\r | |
163 | UINTN Count;\r | |
164 | \r | |
165 | ASSERT ((Block != 0) && (Units != 0));\r | |
166 | \r | |
167 | StartByte = 0;\r | |
168 | StartBit = 0;\r | |
169 | Available = 0;\r | |
170 | \r | |
171 | for (Byte = 0, Bit = 0; Byte < Block->BitsLen;) {\r | |
172 | //\r | |
173 | // If current bit is zero, the corresponding memory unit is\r | |
174 | // available, otherwise we need to restart our searching.\r | |
175 | // Available counts the consective number of zero bit.\r | |
176 | //\r | |
177 | if (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit)) {\r | |
178 | Available++;\r | |
179 | \r | |
180 | if (Available >= Units) {\r | |
181 | break;\r | |
182 | }\r | |
183 | \r | |
184 | NEXT_BIT (Byte, Bit);\r | |
185 | \r | |
186 | } else {\r | |
187 | NEXT_BIT (Byte, Bit);\r | |
188 | \r | |
189 | Available = 0;\r | |
190 | StartByte = Byte;\r | |
191 | StartBit = Bit;\r | |
192 | }\r | |
193 | }\r | |
194 | \r | |
195 | if (Available < Units) {\r | |
196 | return NULL;\r | |
197 | }\r | |
198 | \r | |
199 | //\r | |
200 | // Mark the memory as allocated\r | |
201 | //\r | |
202 | Byte = StartByte;\r | |
203 | Bit = StartBit;\r | |
204 | \r | |
205 | for (Count = 0; Count < Units; Count++) {\r | |
206 | ASSERT (!USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit));\r | |
207 | \r | |
208 | Block->Bits[Byte] = (UINT8) (Block->Bits[Byte] | USB_HC_BIT (Bit));\r | |
209 | NEXT_BIT (Byte, Bit);\r | |
210 | }\r | |
211 | \r | |
212 | return Block->BufHost + (StartByte * 8 + StartBit) * USBHC_MEM_UNIT;\r | |
213 | }\r | |
214 | \r | |
215 | /**\r | |
216 | Calculate the corresponding pci bus address according to the Mem parameter.\r | |
217 | \r | |
218 | @param Pool The memory pool of the host controller.\r | |
219 | @param Mem The pointer to host memory.\r | |
220 | @param Size The size of the memory region.\r | |
221 | \r | |
222 | @return The pci memory address\r | |
223 | \r | |
224 | **/\r | |
225 | EFI_PHYSICAL_ADDRESS\r | |
226 | UsbHcGetPciAddrForHostAddr (\r | |
227 | IN USBHC_MEM_POOL *Pool,\r | |
228 | IN VOID *Mem,\r | |
229 | IN UINTN Size\r | |
230 | )\r | |
231 | {\r | |
232 | USBHC_MEM_BLOCK *Head;\r | |
233 | USBHC_MEM_BLOCK *Block;\r | |
234 | UINTN AllocSize;\r | |
235 | EFI_PHYSICAL_ADDRESS PhyAddr;\r | |
236 | UINTN Offset;\r | |
237 | \r | |
238 | Head = Pool->Head;\r | |
239 | AllocSize = USBHC_MEM_ROUND (Size);\r | |
240 | \r | |
241 | if (Mem == NULL) {\r | |
242 | return 0;\r | |
243 | }\r | |
244 | \r | |
245 | for (Block = Head; Block != NULL; Block = Block->Next) {\r | |
246 | //\r | |
247 | // scan the memory block list for the memory block that\r | |
248 | // completely contains the allocated memory.\r | |
249 | //\r | |
250 | if ((Block->BufHost <= (UINT8 *) Mem) && (((UINT8 *) Mem + AllocSize) <= (Block->BufHost + Block->BufLen))) {\r | |
251 | break;\r | |
252 | }\r | |
253 | }\r | |
254 | \r | |
255 | ASSERT ((Block != NULL));\r | |
256 | //\r | |
257 | // calculate the pci memory address for host memory address.\r | |
258 | //\r | |
259 | Offset = (UINT8 *)Mem - Block->BufHost;\r | |
260 | PhyAddr = (EFI_PHYSICAL_ADDRESS)(UINTN) (Block->Buf + Offset);\r | |
261 | return PhyAddr;\r | |
262 | }\r | |
263 | \r | |
264 | /**\r | |
265 | Calculate the corresponding host address according to the pci address.\r | |
266 | \r | |
267 | @param Pool The memory pool of the host controller.\r | |
268 | @param Mem The pointer to pci memory.\r | |
269 | @param Size The size of the memory region.\r | |
270 | \r | |
271 | @return The host memory address\r | |
272 | \r | |
273 | **/\r | |
274 | EFI_PHYSICAL_ADDRESS\r | |
275 | UsbHcGetHostAddrForPciAddr (\r | |
276 | IN USBHC_MEM_POOL *Pool,\r | |
277 | IN VOID *Mem,\r | |
278 | IN UINTN Size\r | |
279 | )\r | |
280 | {\r | |
281 | USBHC_MEM_BLOCK *Head;\r | |
282 | USBHC_MEM_BLOCK *Block;\r | |
283 | UINTN AllocSize;\r | |
284 | EFI_PHYSICAL_ADDRESS HostAddr;\r | |
285 | UINTN Offset;\r | |
286 | \r | |
287 | Head = Pool->Head;\r | |
288 | AllocSize = USBHC_MEM_ROUND (Size);\r | |
289 | \r | |
290 | if (Mem == NULL) {\r | |
291 | return 0;\r | |
292 | }\r | |
293 | \r | |
294 | for (Block = Head; Block != NULL; Block = Block->Next) {\r | |
295 | //\r | |
296 | // scan the memory block list for the memory block that\r | |
297 | // completely contains the allocated memory.\r | |
298 | //\r | |
299 | if ((Block->Buf <= (UINT8 *) Mem) && (((UINT8 *) Mem + AllocSize) <= (Block->Buf + Block->BufLen))) {\r | |
300 | break;\r | |
301 | }\r | |
302 | }\r | |
303 | \r | |
304 | ASSERT ((Block != NULL));\r | |
305 | //\r | |
306 | // calculate the pci memory address for host memory address.\r | |
307 | //\r | |
308 | Offset = (UINT8 *)Mem - Block->Buf;\r | |
309 | HostAddr = (EFI_PHYSICAL_ADDRESS)(UINTN) (Block->BufHost + Offset);\r | |
310 | return HostAddr;\r | |
311 | }\r | |
312 | \r | |
313 | /**\r | |
314 | Insert the memory block to the pool's list of the blocks.\r | |
315 | \r | |
316 | @param Head The head of the memory pool's block list.\r | |
317 | @param Block The memory block to insert.\r | |
318 | \r | |
319 | **/\r | |
320 | VOID\r | |
321 | UsbHcInsertMemBlockToPool (\r | |
322 | IN USBHC_MEM_BLOCK *Head,\r | |
323 | IN USBHC_MEM_BLOCK *Block\r | |
324 | )\r | |
325 | {\r | |
326 | ASSERT ((Head != NULL) && (Block != NULL));\r | |
327 | Block->Next = Head->Next;\r | |
328 | Head->Next = Block;\r | |
329 | }\r | |
330 | \r | |
331 | \r | |
332 | /**\r | |
333 | Is the memory block empty?\r | |
334 | \r | |
335 | @param Block The memory block to check.\r | |
336 | \r | |
337 | @retval TRUE The memory block is empty.\r | |
338 | @retval FALSE The memory block isn't empty.\r | |
339 | \r | |
340 | **/\r | |
341 | BOOLEAN\r | |
342 | UsbHcIsMemBlockEmpty (\r | |
343 | IN USBHC_MEM_BLOCK *Block\r | |
344 | )\r | |
345 | {\r | |
346 | UINTN Index;\r | |
347 | \r | |
348 | for (Index = 0; Index < Block->BitsLen; Index++) {\r | |
349 | if (Block->Bits[Index] != 0) {\r | |
350 | return FALSE;\r | |
351 | }\r | |
352 | }\r | |
353 | \r | |
354 | return TRUE;\r | |
355 | }\r | |
356 | \r | |
357 | \r | |
358 | /**\r | |
359 | Unlink the memory block from the pool's list.\r | |
360 | \r | |
361 | @param Head The block list head of the memory's pool.\r | |
362 | @param BlockToUnlink The memory block to unlink.\r | |
363 | \r | |
364 | **/\r | |
365 | VOID\r | |
366 | UsbHcUnlinkMemBlock (\r | |
367 | IN USBHC_MEM_BLOCK *Head,\r | |
368 | IN USBHC_MEM_BLOCK *BlockToUnlink\r | |
369 | )\r | |
370 | {\r | |
371 | USBHC_MEM_BLOCK *Block;\r | |
372 | \r | |
373 | ASSERT ((Head != NULL) && (BlockToUnlink != NULL));\r | |
374 | \r | |
375 | for (Block = Head; Block != NULL; Block = Block->Next) {\r | |
376 | if (Block->Next == BlockToUnlink) {\r | |
377 | Block->Next = BlockToUnlink->Next;\r | |
378 | BlockToUnlink->Next = NULL;\r | |
379 | break;\r | |
380 | }\r | |
381 | }\r | |
382 | }\r | |
383 | \r | |
384 | \r | |
385 | /**\r | |
386 | Initialize the memory management pool for the host controller.\r | |
387 | \r | |
388 | @param PciIo The PciIo that can be used to access the host controller.\r | |
389 | \r | |
390 | @retval EFI_SUCCESS The memory pool is initialized.\r | |
391 | @retval EFI_OUT_OF_RESOURCE Fail to init the memory pool.\r | |
392 | \r | |
393 | **/\r | |
394 | USBHC_MEM_POOL *\r | |
395 | UsbHcInitMemPool (\r | |
396 | IN EFI_PCI_IO_PROTOCOL *PciIo\r | |
397 | )\r | |
398 | {\r | |
399 | USBHC_MEM_POOL *Pool;\r | |
400 | \r | |
401 | Pool = AllocatePool (sizeof (USBHC_MEM_POOL));\r | |
402 | \r | |
403 | if (Pool == NULL) {\r | |
404 | return Pool;\r | |
405 | }\r | |
406 | \r | |
407 | Pool->PciIo = PciIo;\r | |
408 | Pool->Head = UsbHcAllocMemBlock (Pool, USBHC_MEM_DEFAULT_PAGES);\r | |
409 | \r | |
410 | if (Pool->Head == NULL) {\r | |
411 | gBS->FreePool (Pool);\r | |
412 | Pool = NULL;\r | |
413 | }\r | |
414 | \r | |
415 | return Pool;\r | |
416 | }\r | |
417 | \r | |
418 | \r | |
419 | /**\r | |
420 | Release the memory management pool.\r | |
421 | \r | |
422 | @param Pool The USB memory pool to free.\r | |
423 | \r | |
424 | @retval EFI_SUCCESS The memory pool is freed.\r | |
425 | @retval EFI_DEVICE_ERROR Failed to free the memory pool.\r | |
426 | \r | |
427 | **/\r | |
428 | EFI_STATUS\r | |
429 | UsbHcFreeMemPool (\r | |
430 | IN USBHC_MEM_POOL *Pool\r | |
431 | )\r | |
432 | {\r | |
433 | USBHC_MEM_BLOCK *Block;\r | |
434 | \r | |
435 | ASSERT (Pool->Head != NULL);\r | |
436 | \r | |
437 | //\r | |
438 | // Unlink all the memory blocks from the pool, then free them.\r | |
439 | // UsbHcUnlinkMemBlock can't be used to unlink and free the\r | |
440 | // first block.\r | |
441 | //\r | |
442 | for (Block = Pool->Head->Next; Block != NULL; Block = Pool->Head->Next) {\r | |
443 | UsbHcUnlinkMemBlock (Pool->Head, Block);\r | |
444 | UsbHcFreeMemBlock (Pool, Block);\r | |
445 | }\r | |
446 | \r | |
447 | UsbHcFreeMemBlock (Pool, Pool->Head);\r | |
448 | gBS->FreePool (Pool);\r | |
449 | return EFI_SUCCESS;\r | |
450 | }\r | |
451 | \r | |
452 | \r | |
453 | /**\r | |
454 | Allocate some memory from the host controller's memory pool\r | |
455 | which can be used to communicate with host controller.\r | |
456 | \r | |
457 | @param Pool The host controller's memory pool.\r | |
458 | @param Size Size of the memory to allocate.\r | |
459 | \r | |
460 | @return The allocated memory or NULL.\r | |
461 | \r | |
462 | **/\r | |
463 | VOID *\r | |
464 | UsbHcAllocateMem (\r | |
465 | IN USBHC_MEM_POOL *Pool,\r | |
466 | IN UINTN Size\r | |
467 | )\r | |
468 | {\r | |
469 | USBHC_MEM_BLOCK *Head;\r | |
470 | USBHC_MEM_BLOCK *Block;\r | |
471 | USBHC_MEM_BLOCK *NewBlock;\r | |
472 | VOID *Mem;\r | |
473 | UINTN AllocSize;\r | |
474 | UINTN Pages;\r | |
475 | \r | |
476 | Mem = NULL;\r | |
477 | AllocSize = USBHC_MEM_ROUND (Size);\r | |
478 | Head = Pool->Head;\r | |
479 | ASSERT (Head != NULL);\r | |
480 | \r | |
481 | //\r | |
482 | // First check whether current memory blocks can satisfy the allocation.\r | |
483 | //\r | |
484 | for (Block = Head; Block != NULL; Block = Block->Next) {\r | |
485 | Mem = UsbHcAllocMemFromBlock (Block, AllocSize / USBHC_MEM_UNIT);\r | |
486 | \r | |
487 | if (Mem != NULL) {\r | |
488 | ZeroMem (Mem, Size);\r | |
489 | break;\r | |
490 | }\r | |
491 | }\r | |
492 | \r | |
493 | if (Mem != NULL) {\r | |
494 | return Mem;\r | |
495 | }\r | |
496 | \r | |
497 | //\r | |
498 | // Create a new memory block if there is not enough memory\r | |
499 | // in the pool. If the allocation size is larger than the\r | |
500 | // default page number, just allocate a large enough memory\r | |
501 | // block. Otherwise allocate default pages.\r | |
502 | //\r | |
503 | if (AllocSize > EFI_PAGES_TO_SIZE (USBHC_MEM_DEFAULT_PAGES)) {\r | |
504 | Pages = EFI_SIZE_TO_PAGES (AllocSize) + 1;\r | |
505 | } else {\r | |
506 | Pages = USBHC_MEM_DEFAULT_PAGES;\r | |
507 | }\r | |
508 | \r | |
509 | NewBlock = UsbHcAllocMemBlock (Pool, Pages);\r | |
510 | \r | |
511 | if (NewBlock == NULL) {\r | |
512 | DEBUG ((EFI_D_ERROR, "UsbHcAllocateMem: failed to allocate block\n"));\r | |
513 | return NULL;\r | |
514 | }\r | |
515 | \r | |
516 | //\r | |
517 | // Add the new memory block to the pool, then allocate memory from it\r | |
518 | //\r | |
519 | UsbHcInsertMemBlockToPool (Head, NewBlock);\r | |
520 | Mem = UsbHcAllocMemFromBlock (NewBlock, AllocSize / USBHC_MEM_UNIT);\r | |
521 | \r | |
522 | if (Mem != NULL) {\r | |
523 | ZeroMem (Mem, Size);\r | |
524 | }\r | |
525 | \r | |
526 | return Mem;\r | |
527 | }\r | |
528 | \r | |
529 | \r | |
530 | /**\r | |
531 | Free the allocated memory back to the memory pool.\r | |
532 | \r | |
533 | @param Pool The memory pool of the host controller.\r | |
534 | @param Mem The memory to free.\r | |
535 | @param Size The size of the memory to free.\r | |
536 | \r | |
537 | **/\r | |
538 | VOID\r | |
539 | UsbHcFreeMem (\r | |
540 | IN USBHC_MEM_POOL *Pool,\r | |
541 | IN VOID *Mem,\r | |
542 | IN UINTN Size\r | |
543 | )\r | |
544 | {\r | |
545 | USBHC_MEM_BLOCK *Head;\r | |
546 | USBHC_MEM_BLOCK *Block;\r | |
547 | UINT8 *ToFree;\r | |
548 | UINTN AllocSize;\r | |
549 | UINTN Byte;\r | |
550 | UINTN Bit;\r | |
551 | UINTN Count;\r | |
552 | \r | |
553 | Head = Pool->Head;\r | |
554 | AllocSize = USBHC_MEM_ROUND (Size);\r | |
555 | ToFree = (UINT8 *) Mem;\r | |
556 | \r | |
557 | for (Block = Head; Block != NULL; Block = Block->Next) {\r | |
558 | //\r | |
559 | // scan the memory block list for the memory block that\r | |
560 | // completely contains the memory to free.\r | |
561 | //\r | |
562 | if ((Block->BufHost <= ToFree) && ((ToFree + AllocSize) <= (Block->BufHost + Block->BufLen))) {\r | |
563 | //\r | |
564 | // compute the start byte and bit in the bit array\r | |
565 | //\r | |
566 | Byte = ((ToFree - Block->BufHost) / USBHC_MEM_UNIT) / 8;\r | |
567 | Bit = ((ToFree - Block->BufHost) / USBHC_MEM_UNIT) % 8;\r | |
568 | \r | |
569 | //\r | |
570 | // reset associated bits in bit arry\r | |
571 | //\r | |
572 | for (Count = 0; Count < (AllocSize / USBHC_MEM_UNIT); Count++) {\r | |
573 | ASSERT (USB_HC_BIT_IS_SET (Block->Bits[Byte], Bit));\r | |
574 | \r | |
575 | Block->Bits[Byte] = (UINT8) (Block->Bits[Byte] ^ USB_HC_BIT (Bit));\r | |
576 | NEXT_BIT (Byte, Bit);\r | |
577 | }\r | |
578 | \r | |
579 | break;\r | |
580 | }\r | |
581 | }\r | |
582 | \r | |
583 | //\r | |
584 | // If Block == NULL, it means that the current memory isn't\r | |
585 | // in the host controller's pool. This is critical because\r | |
586 | // the caller has passed in a wrong memory point\r | |
587 | //\r | |
588 | ASSERT (Block != NULL);\r | |
589 | \r | |
590 | //\r | |
591 | // Release the current memory block if it is empty and not the head\r | |
592 | //\r | |
593 | if ((Block != Head) && UsbHcIsMemBlockEmpty (Block)) {\r | |
594 | UsbHcUnlinkMemBlock (Head, Block);\r | |
595 | UsbHcFreeMemBlock (Pool, Block);\r | |
596 | }\r | |
597 | \r | |
598 | return ;\r | |
599 | }\r | |
600 | \r | |
601 | /** \r | |
602 | Allocates pages at a specified alignment that are suitable for an EfiPciIoOperationBusMasterCommonBuffer mapping.\r | |
603 | \r | |
604 | If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r | |
605 | \r | |
606 | @param PciIo The PciIo that can be used to access the host controller.\r | |
607 | @param Pages The number of pages to allocate.\r | |
608 | @param Alignment The requested alignment of the allocation. Must be a power of two.\r | |
609 | @param HostAddress The system memory address to map to the PCI controller.\r | |
610 | @param DeviceAddress The resulting map address for the bus master PCI controller to \r | |
611 | use to access the hosts HostAddress.\r | |
612 | @param Mapping A resulting value to pass to Unmap().\r | |
613 | \r | |
614 | @retval EFI_SUCCESS Success to allocate aligned pages.\r | |
615 | @retval EFI_INVALID_PARAMETER Pages or Alignment is not valid.\r | |
616 | @retval EFI_OUT_OF_RESOURCES Do not have enough resources to allocate memory.\r | |
617 | \r | |
618 | \r | |
619 | **/\r | |
620 | EFI_STATUS\r | |
621 | UsbHcAllocateAlignedPages (\r | |
622 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
623 | IN UINTN Pages,\r | |
624 | IN UINTN Alignment,\r | |
625 | OUT VOID **HostAddress,\r | |
626 | OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r | |
627 | OUT VOID **Mapping\r | |
628 | )\r | |
629 | {\r | |
630 | EFI_STATUS Status;\r | |
631 | VOID *Memory;\r | |
632 | UINTN AlignedMemory;\r | |
633 | UINTN AlignmentMask;\r | |
634 | UINTN UnalignedPages;\r | |
635 | UINTN RealPages;\r | |
636 | UINTN Bytes;\r | |
637 | \r | |
638 | //\r | |
639 | // Alignment must be a power of two or zero.\r | |
640 | //\r | |
641 | ASSERT ((Alignment & (Alignment - 1)) == 0);\r | |
642 | \r | |
643 | if ((Alignment & (Alignment - 1)) != 0) {\r | |
644 | return EFI_INVALID_PARAMETER;\r | |
645 | }\r | |
646 | \r | |
647 | if (Pages == 0) {\r | |
648 | return EFI_INVALID_PARAMETER;\r | |
649 | }\r | |
650 | if (Alignment > EFI_PAGE_SIZE) {\r | |
651 | //\r | |
652 | // Calculate the total number of pages since alignment is larger than page size.\r | |
653 | //\r | |
654 | AlignmentMask = Alignment - 1;\r | |
655 | RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment);\r | |
656 | //\r | |
657 | // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.\r | |
658 | //\r | |
659 | ASSERT (RealPages > Pages);\r | |
660 | \r | |
661 | Status = PciIo->AllocateBuffer (\r | |
662 | PciIo,\r | |
663 | AllocateAnyPages,\r | |
664 | EfiBootServicesData,\r | |
665 | Pages,\r | |
666 | &Memory,\r | |
667 | 0\r | |
668 | ); \r | |
669 | if (EFI_ERROR (Status)) {\r | |
670 | return EFI_OUT_OF_RESOURCES;\r | |
671 | }\r | |
672 | AlignedMemory = ((UINTN) Memory + AlignmentMask) & ~AlignmentMask;\r | |
673 | UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN) Memory);\r | |
674 | if (UnalignedPages > 0) {\r | |
675 | //\r | |
676 | // Free first unaligned page(s).\r | |
677 | //\r | |
678 | Status = PciIo->FreeBuffer (PciIo, UnalignedPages, Memory);\r | |
679 | ASSERT_EFI_ERROR (Status);\r | |
680 | }\r | |
681 | Memory = (VOID *)(UINTN)(AlignedMemory + EFI_PAGES_TO_SIZE (Pages));\r | |
682 | UnalignedPages = RealPages - Pages - UnalignedPages;\r | |
683 | if (UnalignedPages > 0) {\r | |
684 | //\r | |
685 | // Free last unaligned page(s).\r | |
686 | //\r | |
687 | Status = PciIo->FreeBuffer (PciIo, UnalignedPages, Memory);\r | |
688 | ASSERT_EFI_ERROR (Status);\r | |
689 | }\r | |
690 | } else {\r | |
691 | //\r | |
692 | // Do not over-allocate pages in this case.\r | |
693 | //\r | |
694 | Status = PciIo->AllocateBuffer (\r | |
695 | PciIo,\r | |
696 | AllocateAnyPages,\r | |
697 | EfiBootServicesData,\r | |
698 | Pages,\r | |
699 | &Memory,\r | |
700 | 0\r | |
701 | );\r | |
702 | if (EFI_ERROR (Status)) {\r | |
703 | return EFI_OUT_OF_RESOURCES;\r | |
704 | }\r | |
705 | AlignedMemory = (UINTN) Memory;\r | |
706 | }\r | |
707 | \r | |
708 | Bytes = EFI_PAGES_TO_SIZE (Pages);\r | |
709 | Status = PciIo->Map (\r | |
710 | PciIo,\r | |
711 | EfiPciIoOperationBusMasterCommonBuffer,\r | |
712 | (VOID *) AlignedMemory,\r | |
713 | &Bytes,\r | |
714 | DeviceAddress,\r | |
715 | Mapping\r | |
716 | );\r | |
717 | \r | |
718 | if (EFI_ERROR (Status) || (Bytes != EFI_PAGES_TO_SIZE (Pages))) {\r | |
719 | Status = PciIo->FreeBuffer (PciIo, Pages, (VOID *) AlignedMemory);\r | |
720 | return EFI_OUT_OF_RESOURCES;\r | |
721 | }\r | |
722 | \r | |
723 | *HostAddress = (VOID *) AlignedMemory;\r | |
724 | \r | |
725 | return EFI_SUCCESS;\r | |
726 | }\r | |
727 | \r | |
728 | /**\r | |
729 | Frees memory that was allocated with UsbHcAllocateAlignedPages().\r | |
730 | \r | |
731 | @param PciIo The PciIo that can be used to access the host controller.\r | |
732 | @param HostAddress The system memory address to map to the PCI controller.\r | |
733 | @param Pages The number of 4 KB pages to free.\r | |
734 | @param Mapping The mapping value returned from Map().\r | |
735 | \r | |
736 | **/\r | |
737 | VOID\r | |
738 | UsbHcFreeAlignedPages (\r | |
739 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
740 | IN VOID *HostAddress,\r | |
741 | IN UINTN Pages,\r | |
742 | VOID *Mapping\r | |
743 | )\r | |
744 | {\r | |
745 | EFI_STATUS Status;\r | |
746 | \r | |
747 | ASSERT (Pages != 0);\r | |
748 | \r | |
749 | Status = PciIo->Unmap (PciIo, Mapping);\r | |
750 | ASSERT_EFI_ERROR (Status);\r | |
751 | \r | |
752 | Status = PciIo->FreeBuffer (\r | |
753 | PciIo,\r | |
754 | Pages,\r | |
755 | HostAddress\r | |
756 | ); \r | |
757 | ASSERT_EFI_ERROR (Status);\r | |
758 | }\r |