]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Library/NonCoherentDmaLib/NonCoherentDmaLib.c
EmbeddedPkg/NonCoherentDmaLib: Avoid dereferencing unset Map field
[mirror_edk2.git] / EmbeddedPkg / Library / NonCoherentDmaLib / NonCoherentDmaLib.c
CommitLineData
723102c7
AB
1/** @file\r
2\r
3 Generic non-coherent implementation of DmaLib.h\r
4\r
5 Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>\r
6 Copyright (c) 2015 - 2017, Linaro, Ltd. All rights reserved.<BR>\r
7\r
878b807a 8 SPDX-License-Identifier: BSD-2-Clause-Patent\r
723102c7
AB
9\r
10**/\r
11\r
12#include <PiDxe.h>\r
13#include <Library/BaseLib.h>\r
14#include <Library/DebugLib.h>\r
15#include <Library/DmaLib.h>\r
16#include <Library/DxeServicesTableLib.h>\r
17#include <Library/MemoryAllocationLib.h>\r
18#include <Library/UefiBootServicesTableLib.h>\r
19#include <Library/IoLib.h>\r
20#include <Library/BaseMemoryLib.h>\r
21\r
22#include <Protocol/Cpu.h>\r
23\r
24typedef struct {\r
25 EFI_PHYSICAL_ADDRESS HostAddress;\r
26 VOID *BufferAddress;\r
27 UINTN NumberOfBytes;\r
28 DMA_MAP_OPERATION Operation;\r
29 BOOLEAN DoubleBuffer;\r
30} MAP_INFO_INSTANCE;\r
31\r
32\r
33typedef struct {\r
34 LIST_ENTRY Link;\r
35 VOID *HostAddress;\r
36 UINTN NumPages;\r
37 UINT64 Attributes;\r
38} UNCACHED_ALLOCATION;\r
39\r
40STATIC EFI_CPU_ARCH_PROTOCOL *mCpu;\r
41STATIC LIST_ENTRY UncachedAllocationList;\r
42\r
62a75650
AB
43STATIC PHYSICAL_ADDRESS mDmaHostAddressLimit;\r
44\r
723102c7
AB
45STATIC\r
46PHYSICAL_ADDRESS\r
47HostToDeviceAddress (\r
48 IN VOID *Address\r
49 )\r
50{\r
51 return (PHYSICAL_ADDRESS)(UINTN)Address + PcdGet64 (PcdDmaDeviceOffset);\r
52}\r
53\r
62a75650
AB
54/**\r
55 Allocates one or more 4KB pages of a certain memory type at a specified\r
56 alignment.\r
57\r
58 Allocates the number of 4KB pages specified by Pages of a certain memory type\r
59 with an alignment specified by Alignment. The allocated buffer is returned.\r
60 If Pages is 0, then NULL is returned. If there is not enough memory at the\r
61 specified alignment remaining to satisfy the request, then NULL is returned.\r
62 If Alignment is not a power of two and Alignment is not zero, then ASSERT().\r
63 If Pages plus EFI_SIZE_TO_PAGES (Alignment) overflows, then ASSERT().\r
64\r
65 @param MemoryType The type of memory to allocate.\r
66 @param Pages The number of 4 KB pages to allocate.\r
67 @param Alignment The requested alignment of the allocation.\r
68 Must be a power of two.\r
69 If Alignment is zero, then byte alignment is\r
70 used.\r
71\r
72 @return A pointer to the allocated buffer or NULL if allocation fails.\r
73\r
74**/\r
75STATIC\r
76VOID *\r
77InternalAllocateAlignedPages (\r
78 IN EFI_MEMORY_TYPE MemoryType,\r
79 IN UINTN Pages,\r
80 IN UINTN Alignment\r
81 )\r
82{\r
83 EFI_STATUS Status;\r
84 EFI_PHYSICAL_ADDRESS Memory;\r
85 UINTN AlignedMemory;\r
86 UINTN AlignmentMask;\r
87 UINTN UnalignedPages;\r
88 UINTN RealPages;\r
89\r
90 //\r
91 // Alignment must be a power of two or zero.\r
92 //\r
93 ASSERT ((Alignment & (Alignment - 1)) == 0);\r
94\r
95 if (Pages == 0) {\r
96 return NULL;\r
97 }\r
98 if (Alignment > EFI_PAGE_SIZE) {\r
99 //\r
100 // Calculate the total number of pages since alignment is larger than page\r
101 // size.\r
102 //\r
103 AlignmentMask = Alignment - 1;\r
104 RealPages = Pages + EFI_SIZE_TO_PAGES (Alignment);\r
105 //\r
106 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not\r
107 // overflow.\r
108 //\r
109 ASSERT (RealPages > Pages);\r
110\r
111 Memory = mDmaHostAddressLimit;\r
112 Status = gBS->AllocatePages (AllocateMaxAddress, MemoryType, RealPages,\r
113 &Memory);\r
114 if (EFI_ERROR (Status)) {\r
115 return NULL;\r
116 }\r
117 AlignedMemory = ((UINTN)Memory + AlignmentMask) & ~AlignmentMask;\r
118 UnalignedPages = EFI_SIZE_TO_PAGES (AlignedMemory - (UINTN)Memory);\r
119 if (UnalignedPages > 0) {\r
120 //\r
121 // Free first unaligned page(s).\r
122 //\r
123 Status = gBS->FreePages (Memory, UnalignedPages);\r
124 ASSERT_EFI_ERROR (Status);\r
125 }\r
126 Memory = AlignedMemory + EFI_PAGES_TO_SIZE (Pages);\r
127 UnalignedPages = RealPages - Pages - UnalignedPages;\r
128 if (UnalignedPages > 0) {\r
129 //\r
130 // Free last unaligned page(s).\r
131 //\r
132 Status = gBS->FreePages (Memory, UnalignedPages);\r
133 ASSERT_EFI_ERROR (Status);\r
134 }\r
135 } else {\r
136 //\r
137 // Do not over-allocate pages in this case.\r
138 //\r
139 Memory = mDmaHostAddressLimit;\r
140 Status = gBS->AllocatePages (AllocateMaxAddress, MemoryType, Pages,\r
141 &Memory);\r
142 if (EFI_ERROR (Status)) {\r
143 return NULL;\r
144 }\r
145 AlignedMemory = (UINTN)Memory;\r
146 }\r
147 return (VOID *)AlignedMemory;\r
148}\r
149\r
723102c7
AB
150/**\r
151 Provides the DMA controller-specific addresses needed to access system memory.\r
152\r
153 Operation is relative to the DMA bus master.\r
154\r
155 @param Operation Indicates if the bus master is going to read or\r
156 write to system memory.\r
157 @param HostAddress The system memory address to map to the DMA\r
158 controller.\r
159 @param NumberOfBytes On input the number of bytes to map. On output\r
160 the number of bytes that were mapped.\r
161 @param DeviceAddress The resulting map address for the bus master\r
162 controller to use to access the host's\r
163 HostAddress.\r
164 @param Mapping A resulting value to pass to Unmap().\r
165\r
166 @retval EFI_SUCCESS The range was mapped for the returned\r
167 NumberOfBytes.\r
168 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common\r
169 buffer.\r
170 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
171 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack\r
172 of resources.\r
173 @retval EFI_DEVICE_ERROR The system hardware could not map the requested\r
174 address.\r
175\r
176**/\r
177EFI_STATUS\r
178EFIAPI\r
179DmaMap (\r
180 IN DMA_MAP_OPERATION Operation,\r
181 IN VOID *HostAddress,\r
182 IN OUT UINTN *NumberOfBytes,\r
183 OUT PHYSICAL_ADDRESS *DeviceAddress,\r
184 OUT VOID **Mapping\r
185 )\r
186{\r
187 EFI_STATUS Status;\r
188 MAP_INFO_INSTANCE *Map;\r
189 VOID *Buffer;\r
190 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
191 UINTN AllocSize;\r
192\r
193 if (HostAddress == NULL ||\r
194 NumberOfBytes == NULL ||\r
195 DeviceAddress == NULL ||\r
196 Mapping == NULL ) {\r
197 return EFI_INVALID_PARAMETER;\r
198 }\r
199\r
200 if (Operation >= MapOperationMaximum) {\r
201 return EFI_INVALID_PARAMETER;\r
202 }\r
203\r
204 *DeviceAddress = HostToDeviceAddress (HostAddress);\r
205\r
206 // Remember range so we can flush on the other side\r
207 Map = AllocatePool (sizeof (MAP_INFO_INSTANCE));\r
208 if (Map == NULL) {\r
209 return EFI_OUT_OF_RESOURCES;\r
210 }\r
211\r
62a75650
AB
212 if (((UINTN)HostAddress + *NumberOfBytes) > mDmaHostAddressLimit) {\r
213\r
214 if (Operation == MapOperationBusMasterCommonBuffer) {\r
215 goto CommonBufferError;\r
216 }\r
217\r
218 AllocSize = ALIGN_VALUE (*NumberOfBytes, mCpu->DmaBufferAlignment);\r
219 Map->BufferAddress = InternalAllocateAlignedPages (EfiBootServicesData,\r
220 EFI_SIZE_TO_PAGES (AllocSize),\r
221 mCpu->DmaBufferAlignment);\r
222 if (Map->BufferAddress == NULL) {\r
223 Status = EFI_OUT_OF_RESOURCES;\r
224 goto FreeMapInfo;\r
225 }\r
226\r
ef56f55d 227 if (Operation == MapOperationBusMasterRead) {\r
8f22a331 228 CopyMem (Map->BufferAddress, (VOID *)(UINTN)HostAddress, *NumberOfBytes);\r
62a75650
AB
229 }\r
230 mCpu->FlushDataCache (mCpu, (UINTN)Map->BufferAddress, AllocSize,\r
231 EfiCpuFlushTypeWriteBack);\r
232\r
233 *DeviceAddress = HostToDeviceAddress (Map->BufferAddress);\r
234 } else if (Operation != MapOperationBusMasterRead &&\r
723102c7
AB
235 ((((UINTN)HostAddress & (mCpu->DmaBufferAlignment - 1)) != 0) ||\r
236 ((*NumberOfBytes & (mCpu->DmaBufferAlignment - 1)) != 0))) {\r
237\r
238 // Get the cacheability of the region\r
239 Status = gDS->GetMemorySpaceDescriptor ((UINTN)HostAddress, &GcdDescriptor);\r
240 if (EFI_ERROR(Status)) {\r
241 goto FreeMapInfo;\r
242 }\r
243\r
244 // If the mapped buffer is not an uncached buffer\r
245 if ((GcdDescriptor.Attributes & (EFI_MEMORY_WB | EFI_MEMORY_WT)) != 0) {\r
246 //\r
247 // Operations of type MapOperationBusMasterCommonBuffer are only allowed\r
248 // on uncached buffers.\r
249 //\r
250 if (Operation == MapOperationBusMasterCommonBuffer) {\r
62a75650 251 goto CommonBufferError;\r
723102c7
AB
252 }\r
253\r
254 //\r
255 // If the buffer does not fill entire cache lines we must double buffer\r
256 // into a suitably aligned allocation that allows us to invalidate the\r
257 // cache without running the risk of corrupting adjacent unrelated data.\r
258 // Note that pool allocations are guaranteed to be 8 byte aligned, so\r
259 // we only have to add (alignment - 8) worth of padding.\r
260 //\r
261 Map->DoubleBuffer = TRUE;\r
262 AllocSize = ALIGN_VALUE (*NumberOfBytes, mCpu->DmaBufferAlignment) +\r
263 (mCpu->DmaBufferAlignment - 8);\r
264 Map->BufferAddress = AllocatePool (AllocSize);\r
265 if (Map->BufferAddress == NULL) {\r
266 Status = EFI_OUT_OF_RESOURCES;\r
267 goto FreeMapInfo;\r
268 }\r
269\r
270 Buffer = ALIGN_POINTER (Map->BufferAddress, mCpu->DmaBufferAlignment);\r
271 *DeviceAddress = HostToDeviceAddress (Buffer);\r
272\r
273 //\r
274 // Get rid of any dirty cachelines covering the double buffer. This\r
275 // prevents them from being written back unexpectedly, potentially\r
276 // overwriting the data we receive from the device.\r
277 //\r
278 mCpu->FlushDataCache (mCpu, (UINTN)Buffer, *NumberOfBytes,\r
279 EfiCpuFlushTypeWriteBack);\r
280 } else {\r
281 Map->DoubleBuffer = FALSE;\r
282 }\r
283 } else {\r
284 Map->DoubleBuffer = FALSE;\r
285\r
286 DEBUG_CODE_BEGIN ();\r
287\r
288 //\r
289 // The operation type check above only executes if the buffer happens to be\r
290 // misaligned with respect to CWG, but even if it is aligned, we should not\r
291 // allow arbitrary buffers to be used for creating consistent mappings.\r
292 // So duplicate the check here when running in DEBUG mode, just to assert\r
293 // that we are not trying to create a consistent mapping for cached memory.\r
294 //\r
295 Status = gDS->GetMemorySpaceDescriptor ((UINTN)HostAddress, &GcdDescriptor);\r
296 ASSERT_EFI_ERROR(Status);\r
297\r
298 ASSERT (Operation != MapOperationBusMasterCommonBuffer ||\r
299 (GcdDescriptor.Attributes & (EFI_MEMORY_WB | EFI_MEMORY_WT)) == 0);\r
300\r
301 DEBUG_CODE_END ();\r
302\r
303 // Flush the Data Cache (should not have any effect if the memory region is\r
304 // uncached)\r
305 mCpu->FlushDataCache (mCpu, (UINTN)HostAddress, *NumberOfBytes,\r
306 EfiCpuFlushTypeWriteBackInvalidate);\r
307 }\r
308\r
309 Map->HostAddress = (UINTN)HostAddress;\r
310 Map->NumberOfBytes = *NumberOfBytes;\r
311 Map->Operation = Operation;\r
312\r
313 *Mapping = Map;\r
314\r
315 return EFI_SUCCESS;\r
316\r
62a75650
AB
317CommonBufferError:\r
318 DEBUG ((DEBUG_ERROR,\r
319 "%a: Operation type 'MapOperationBusMasterCommonBuffer' is only "\r
320 "supported\non memory regions that were allocated using "\r
321 "DmaAllocateBuffer ()\n", __FUNCTION__));\r
322 Status = EFI_UNSUPPORTED;\r
723102c7
AB
323FreeMapInfo:\r
324 FreePool (Map);\r
325\r
326 return Status;\r
327}\r
328\r
329\r
330/**\r
331 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or\r
332 DmaMapBusMasterCommonBuffer() operation and releases any corresponding\r
333 resources.\r
334\r
335 @param Mapping The mapping value returned from DmaMap*().\r
336\r
337 @retval EFI_SUCCESS The range was unmapped.\r
338 @retval EFI_DEVICE_ERROR The data was not committed to the target system\r
339 memory.\r
340 @retval EFI_INVALID_PARAMETER An inconsistency was detected between the\r
341 mapping type and the DoubleBuffer field\r
342\r
343**/\r
344EFI_STATUS\r
345EFIAPI\r
346DmaUnmap (\r
347 IN VOID *Mapping\r
348 )\r
349{\r
350 MAP_INFO_INSTANCE *Map;\r
351 EFI_STATUS Status;\r
352 VOID *Buffer;\r
62a75650 353 UINTN AllocSize;\r
723102c7
AB
354\r
355 if (Mapping == NULL) {\r
356 ASSERT (FALSE);\r
357 return EFI_INVALID_PARAMETER;\r
358 }\r
359\r
360 Map = (MAP_INFO_INSTANCE *)Mapping;\r
361\r
362 Status = EFI_SUCCESS;\r
62a75650
AB
363 if (((UINTN)Map->HostAddress + Map->NumberOfBytes) > mDmaHostAddressLimit) {\r
364 AllocSize = ALIGN_VALUE (Map->NumberOfBytes, mCpu->DmaBufferAlignment);\r
365 if (Map->Operation == MapOperationBusMasterWrite) {\r
366 mCpu->FlushDataCache (mCpu, (UINTN)Map->BufferAddress, AllocSize,\r
367 EfiCpuFlushTypeInvalidate);\r
368 CopyMem ((VOID *)(UINTN)Map->HostAddress, Map->BufferAddress,\r
369 Map->NumberOfBytes);\r
370 }\r
371 FreePages (Map->BufferAddress, EFI_SIZE_TO_PAGES (AllocSize));\r
372 } else if (Map->DoubleBuffer) {\r
373\r
723102c7
AB
374 ASSERT (Map->Operation == MapOperationBusMasterWrite);\r
375\r
376 if (Map->Operation != MapOperationBusMasterWrite) {\r
377 Status = EFI_INVALID_PARAMETER;\r
378 } else {\r
379 Buffer = ALIGN_POINTER (Map->BufferAddress, mCpu->DmaBufferAlignment);\r
380\r
381 mCpu->FlushDataCache (mCpu, (UINTN)Buffer, Map->NumberOfBytes,\r
382 EfiCpuFlushTypeInvalidate);\r
383\r
384 CopyMem ((VOID *)(UINTN)Map->HostAddress, Buffer, Map->NumberOfBytes);\r
385\r
386 FreePool (Map->BufferAddress);\r
387 }\r
388 } else {\r
389 if (Map->Operation == MapOperationBusMasterWrite) {\r
390 //\r
391 // Make sure we read buffer from uncached memory and not the cache\r
392 //\r
393 mCpu->FlushDataCache (mCpu, Map->HostAddress, Map->NumberOfBytes,\r
394 EfiCpuFlushTypeInvalidate);\r
395 }\r
396 }\r
397\r
398 FreePool (Map);\r
399\r
400 return Status;\r
401}\r
402\r
403/**\r
404 Allocates pages that are suitable for an DmaMap() of type\r
405 MapOperationBusMasterCommonBuffer mapping.\r
406\r
407 @param MemoryType The type of memory to allocate,\r
408 EfiBootServicesData or EfiRuntimeServicesData.\r
409 @param Pages The number of pages to allocate.\r
410 @param HostAddress A pointer to store the base system memory\r
411 address of the allocated range.\r
412\r
413 @retval EFI_SUCCESS The requested memory pages were allocated.\r
414 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
415 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
416\r
417**/\r
418EFI_STATUS\r
419EFIAPI\r
420DmaAllocateBuffer (\r
421 IN EFI_MEMORY_TYPE MemoryType,\r
422 IN UINTN Pages,\r
423 OUT VOID **HostAddress\r
424 )\r
425{\r
426 return DmaAllocateAlignedBuffer (MemoryType, Pages, 0, HostAddress);\r
427}\r
428\r
429/**\r
430 Allocates pages that are suitable for an DmaMap() of type\r
431 MapOperationBusMasterCommonBuffer mapping, at the requested alignment.\r
432\r
433 @param MemoryType The type of memory to allocate,\r
434 EfiBootServicesData or EfiRuntimeServicesData.\r
435 @param Pages The number of pages to allocate.\r
436 @param Alignment Alignment in bytes of the base of the returned\r
437 buffer (must be a power of 2)\r
438 @param HostAddress A pointer to store the base system memory\r
439 address of the allocated range.\r
440\r
441 @retval EFI_SUCCESS The requested memory pages were allocated.\r
442 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
443 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
444\r
445**/\r
446EFI_STATUS\r
447EFIAPI\r
448DmaAllocateAlignedBuffer (\r
449 IN EFI_MEMORY_TYPE MemoryType,\r
450 IN UINTN Pages,\r
451 IN UINTN Alignment,\r
452 OUT VOID **HostAddress\r
453 )\r
454{\r
455 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
456 VOID *Allocation;\r
457 UINT64 MemType;\r
458 UNCACHED_ALLOCATION *Alloc;\r
459 EFI_STATUS Status;\r
460\r
461 if (Alignment == 0) {\r
462 Alignment = EFI_PAGE_SIZE;\r
463 }\r
464\r
465 if (HostAddress == NULL ||\r
466 (Alignment & (Alignment - 1)) != 0) {\r
467 return EFI_INVALID_PARAMETER;\r
468 }\r
469\r
62a75650
AB
470 if (MemoryType == EfiBootServicesData ||\r
471 MemoryType == EfiRuntimeServicesData) {\r
472 Allocation = InternalAllocateAlignedPages (MemoryType, Pages, Alignment);\r
723102c7
AB
473 } else {\r
474 return EFI_INVALID_PARAMETER;\r
475 }\r
476\r
477 if (Allocation == NULL) {\r
478 return EFI_OUT_OF_RESOURCES;\r
479 }\r
480\r
481 // Get the cacheability of the region\r
482 Status = gDS->GetMemorySpaceDescriptor ((UINTN)Allocation, &GcdDescriptor);\r
483 if (EFI_ERROR(Status)) {\r
484 goto FreeBuffer;\r
485 }\r
486\r
487 // Choose a suitable uncached memory type that is supported by the region\r
488 if (GcdDescriptor.Capabilities & EFI_MEMORY_WC) {\r
489 MemType = EFI_MEMORY_WC;\r
490 } else if (GcdDescriptor.Capabilities & EFI_MEMORY_UC) {\r
491 MemType = EFI_MEMORY_UC;\r
492 } else {\r
493 Status = EFI_UNSUPPORTED;\r
494 goto FreeBuffer;\r
495 }\r
496\r
497 Alloc = AllocatePool (sizeof *Alloc);\r
498 if (Alloc == NULL) {\r
499 goto FreeBuffer;\r
500 }\r
501\r
502 Alloc->HostAddress = Allocation;\r
503 Alloc->NumPages = Pages;\r
504 Alloc->Attributes = GcdDescriptor.Attributes;\r
505\r
506 InsertHeadList (&UncachedAllocationList, &Alloc->Link);\r
507\r
508 // Remap the region with the new attributes\r
509 Status = gDS->SetMemorySpaceAttributes ((PHYSICAL_ADDRESS)(UINTN)Allocation,\r
510 EFI_PAGES_TO_SIZE (Pages),\r
511 MemType);\r
512 if (EFI_ERROR (Status)) {\r
513 goto FreeAlloc;\r
514 }\r
515\r
516 Status = mCpu->FlushDataCache (mCpu,\r
517 (PHYSICAL_ADDRESS)(UINTN)Allocation,\r
518 EFI_PAGES_TO_SIZE (Pages),\r
519 EfiCpuFlushTypeInvalidate);\r
520 if (EFI_ERROR (Status)) {\r
521 goto FreeAlloc;\r
522 }\r
523\r
524 *HostAddress = Allocation;\r
525\r
526 return EFI_SUCCESS;\r
527\r
528FreeAlloc:\r
529 RemoveEntryList (&Alloc->Link);\r
530 FreePool (Alloc);\r
531\r
532FreeBuffer:\r
533 FreePages (Allocation, Pages);\r
534 return Status;\r
535}\r
536\r
537\r
538/**\r
539 Frees memory that was allocated with DmaAllocateBuffer().\r
540\r
541 @param Pages The number of pages to free.\r
542 @param HostAddress The base system memory address of the allocated\r
543 range.\r
544\r
545 @retval EFI_SUCCESS The requested memory pages were freed.\r
546 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and\r
547 Pages was not allocated with\r
548 DmaAllocateBuffer().\r
549\r
550**/\r
551EFI_STATUS\r
552EFIAPI\r
553DmaFreeBuffer (\r
554 IN UINTN Pages,\r
555 IN VOID *HostAddress\r
556 )\r
557{\r
558 LIST_ENTRY *Link;\r
559 UNCACHED_ALLOCATION *Alloc;\r
560 BOOLEAN Found;\r
561 EFI_STATUS Status;\r
562\r
563 if (HostAddress == NULL) {\r
564 return EFI_INVALID_PARAMETER;\r
565 }\r
566\r
567 for (Link = GetFirstNode (&UncachedAllocationList), Found = FALSE;\r
568 !IsNull (&UncachedAllocationList, Link);\r
569 Link = GetNextNode (&UncachedAllocationList, Link)) {\r
570\r
571 Alloc = BASE_CR (Link, UNCACHED_ALLOCATION, Link);\r
572 if (Alloc->HostAddress == HostAddress && Alloc->NumPages == Pages) {\r
573 Found = TRUE;\r
574 break;\r
575 }\r
576 }\r
577\r
578 if (!Found) {\r
579 ASSERT (FALSE);\r
580 return EFI_INVALID_PARAMETER;\r
581 }\r
582\r
583 RemoveEntryList (&Alloc->Link);\r
584\r
585 Status = gDS->SetMemorySpaceAttributes ((PHYSICAL_ADDRESS)(UINTN)HostAddress,\r
586 EFI_PAGES_TO_SIZE (Pages),\r
587 Alloc->Attributes);\r
588 if (EFI_ERROR (Status)) {\r
589 goto FreeAlloc;\r
590 }\r
591\r
592 //\r
593 // If we fail to restore the original attributes, it is better to leak the\r
594 // memory than to return it to the heap\r
595 //\r
596 FreePages (HostAddress, Pages);\r
597\r
598FreeAlloc:\r
599 FreePool (Alloc);\r
600 return Status;\r
601}\r
602\r
603\r
604EFI_STATUS\r
605EFIAPI\r
606NonCoherentDmaLibConstructor (\r
607 IN EFI_HANDLE ImageHandle,\r
608 IN EFI_SYSTEM_TABLE *SystemTable\r
609 )\r
610{\r
611 InitializeListHead (&UncachedAllocationList);\r
612\r
62a75650
AB
613 //\r
614 // Ensure that the combination of DMA addressing offset and limit produces\r
615 // a sane value.\r
616 //\r
617 ASSERT (PcdGet64 (PcdDmaDeviceLimit) > PcdGet64 (PcdDmaDeviceOffset));\r
618\r
619 mDmaHostAddressLimit = PcdGet64 (PcdDmaDeviceLimit) -\r
620 PcdGet64 (PcdDmaDeviceOffset);\r
621\r
723102c7
AB
622 // Get the Cpu protocol for later use\r
623 return gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&mCpu);\r
624}\r