]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Library/NonCoherentDmaLib/NonCoherentDmaLib.c
ArmPkg/PlatformBootManagerLib: regenerate boot options on boot failure
[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
227 if (Map->Operation == MapOperationBusMasterRead) {\r
228 CopyMem (Map->BufferAddress, (VOID *)(UINTN)Map->HostAddress,\r
229 *NumberOfBytes);\r
230 }\r
231 mCpu->FlushDataCache (mCpu, (UINTN)Map->BufferAddress, AllocSize,\r
232 EfiCpuFlushTypeWriteBack);\r
233\r
234 *DeviceAddress = HostToDeviceAddress (Map->BufferAddress);\r
235 } else if (Operation != MapOperationBusMasterRead &&\r
723102c7
AB
236 ((((UINTN)HostAddress & (mCpu->DmaBufferAlignment - 1)) != 0) ||\r
237 ((*NumberOfBytes & (mCpu->DmaBufferAlignment - 1)) != 0))) {\r
238\r
239 // Get the cacheability of the region\r
240 Status = gDS->GetMemorySpaceDescriptor ((UINTN)HostAddress, &GcdDescriptor);\r
241 if (EFI_ERROR(Status)) {\r
242 goto FreeMapInfo;\r
243 }\r
244\r
245 // If the mapped buffer is not an uncached buffer\r
246 if ((GcdDescriptor.Attributes & (EFI_MEMORY_WB | EFI_MEMORY_WT)) != 0) {\r
247 //\r
248 // Operations of type MapOperationBusMasterCommonBuffer are only allowed\r
249 // on uncached buffers.\r
250 //\r
251 if (Operation == MapOperationBusMasterCommonBuffer) {\r
62a75650 252 goto CommonBufferError;\r
723102c7
AB
253 }\r
254\r
255 //\r
256 // If the buffer does not fill entire cache lines we must double buffer\r
257 // into a suitably aligned allocation that allows us to invalidate the\r
258 // cache without running the risk of corrupting adjacent unrelated data.\r
259 // Note that pool allocations are guaranteed to be 8 byte aligned, so\r
260 // we only have to add (alignment - 8) worth of padding.\r
261 //\r
262 Map->DoubleBuffer = TRUE;\r
263 AllocSize = ALIGN_VALUE (*NumberOfBytes, mCpu->DmaBufferAlignment) +\r
264 (mCpu->DmaBufferAlignment - 8);\r
265 Map->BufferAddress = AllocatePool (AllocSize);\r
266 if (Map->BufferAddress == NULL) {\r
267 Status = EFI_OUT_OF_RESOURCES;\r
268 goto FreeMapInfo;\r
269 }\r
270\r
271 Buffer = ALIGN_POINTER (Map->BufferAddress, mCpu->DmaBufferAlignment);\r
272 *DeviceAddress = HostToDeviceAddress (Buffer);\r
273\r
274 //\r
275 // Get rid of any dirty cachelines covering the double buffer. This\r
276 // prevents them from being written back unexpectedly, potentially\r
277 // overwriting the data we receive from the device.\r
278 //\r
279 mCpu->FlushDataCache (mCpu, (UINTN)Buffer, *NumberOfBytes,\r
280 EfiCpuFlushTypeWriteBack);\r
281 } else {\r
282 Map->DoubleBuffer = FALSE;\r
283 }\r
284 } else {\r
285 Map->DoubleBuffer = FALSE;\r
286\r
287 DEBUG_CODE_BEGIN ();\r
288\r
289 //\r
290 // The operation type check above only executes if the buffer happens to be\r
291 // misaligned with respect to CWG, but even if it is aligned, we should not\r
292 // allow arbitrary buffers to be used for creating consistent mappings.\r
293 // So duplicate the check here when running in DEBUG mode, just to assert\r
294 // that we are not trying to create a consistent mapping for cached memory.\r
295 //\r
296 Status = gDS->GetMemorySpaceDescriptor ((UINTN)HostAddress, &GcdDescriptor);\r
297 ASSERT_EFI_ERROR(Status);\r
298\r
299 ASSERT (Operation != MapOperationBusMasterCommonBuffer ||\r
300 (GcdDescriptor.Attributes & (EFI_MEMORY_WB | EFI_MEMORY_WT)) == 0);\r
301\r
302 DEBUG_CODE_END ();\r
303\r
304 // Flush the Data Cache (should not have any effect if the memory region is\r
305 // uncached)\r
306 mCpu->FlushDataCache (mCpu, (UINTN)HostAddress, *NumberOfBytes,\r
307 EfiCpuFlushTypeWriteBackInvalidate);\r
308 }\r
309\r
310 Map->HostAddress = (UINTN)HostAddress;\r
311 Map->NumberOfBytes = *NumberOfBytes;\r
312 Map->Operation = Operation;\r
313\r
314 *Mapping = Map;\r
315\r
316 return EFI_SUCCESS;\r
317\r
62a75650
AB
318CommonBufferError:\r
319 DEBUG ((DEBUG_ERROR,\r
320 "%a: Operation type 'MapOperationBusMasterCommonBuffer' is only "\r
321 "supported\non memory regions that were allocated using "\r
322 "DmaAllocateBuffer ()\n", __FUNCTION__));\r
323 Status = EFI_UNSUPPORTED;\r
723102c7
AB
324FreeMapInfo:\r
325 FreePool (Map);\r
326\r
327 return Status;\r
328}\r
329\r
330\r
331/**\r
332 Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or\r
333 DmaMapBusMasterCommonBuffer() operation and releases any corresponding\r
334 resources.\r
335\r
336 @param Mapping The mapping value returned from DmaMap*().\r
337\r
338 @retval EFI_SUCCESS The range was unmapped.\r
339 @retval EFI_DEVICE_ERROR The data was not committed to the target system\r
340 memory.\r
341 @retval EFI_INVALID_PARAMETER An inconsistency was detected between the\r
342 mapping type and the DoubleBuffer field\r
343\r
344**/\r
345EFI_STATUS\r
346EFIAPI\r
347DmaUnmap (\r
348 IN VOID *Mapping\r
349 )\r
350{\r
351 MAP_INFO_INSTANCE *Map;\r
352 EFI_STATUS Status;\r
353 VOID *Buffer;\r
62a75650 354 UINTN AllocSize;\r
723102c7
AB
355\r
356 if (Mapping == NULL) {\r
357 ASSERT (FALSE);\r
358 return EFI_INVALID_PARAMETER;\r
359 }\r
360\r
361 Map = (MAP_INFO_INSTANCE *)Mapping;\r
362\r
363 Status = EFI_SUCCESS;\r
62a75650
AB
364 if (((UINTN)Map->HostAddress + Map->NumberOfBytes) > mDmaHostAddressLimit) {\r
365 AllocSize = ALIGN_VALUE (Map->NumberOfBytes, mCpu->DmaBufferAlignment);\r
366 if (Map->Operation == MapOperationBusMasterWrite) {\r
367 mCpu->FlushDataCache (mCpu, (UINTN)Map->BufferAddress, AllocSize,\r
368 EfiCpuFlushTypeInvalidate);\r
369 CopyMem ((VOID *)(UINTN)Map->HostAddress, Map->BufferAddress,\r
370 Map->NumberOfBytes);\r
371 }\r
372 FreePages (Map->BufferAddress, EFI_SIZE_TO_PAGES (AllocSize));\r
373 } else if (Map->DoubleBuffer) {\r
374\r
723102c7
AB
375 ASSERT (Map->Operation == MapOperationBusMasterWrite);\r
376\r
377 if (Map->Operation != MapOperationBusMasterWrite) {\r
378 Status = EFI_INVALID_PARAMETER;\r
379 } else {\r
380 Buffer = ALIGN_POINTER (Map->BufferAddress, mCpu->DmaBufferAlignment);\r
381\r
382 mCpu->FlushDataCache (mCpu, (UINTN)Buffer, Map->NumberOfBytes,\r
383 EfiCpuFlushTypeInvalidate);\r
384\r
385 CopyMem ((VOID *)(UINTN)Map->HostAddress, Buffer, Map->NumberOfBytes);\r
386\r
387 FreePool (Map->BufferAddress);\r
388 }\r
389 } else {\r
390 if (Map->Operation == MapOperationBusMasterWrite) {\r
391 //\r
392 // Make sure we read buffer from uncached memory and not the cache\r
393 //\r
394 mCpu->FlushDataCache (mCpu, Map->HostAddress, Map->NumberOfBytes,\r
395 EfiCpuFlushTypeInvalidate);\r
396 }\r
397 }\r
398\r
399 FreePool (Map);\r
400\r
401 return Status;\r
402}\r
403\r
404/**\r
405 Allocates pages that are suitable for an DmaMap() of type\r
406 MapOperationBusMasterCommonBuffer mapping.\r
407\r
408 @param MemoryType The type of memory to allocate,\r
409 EfiBootServicesData or EfiRuntimeServicesData.\r
410 @param Pages The number of pages to allocate.\r
411 @param HostAddress A pointer to store the base system memory\r
412 address of the allocated range.\r
413\r
414 @retval EFI_SUCCESS The requested memory pages were allocated.\r
415 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
416 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
417\r
418**/\r
419EFI_STATUS\r
420EFIAPI\r
421DmaAllocateBuffer (\r
422 IN EFI_MEMORY_TYPE MemoryType,\r
423 IN UINTN Pages,\r
424 OUT VOID **HostAddress\r
425 )\r
426{\r
427 return DmaAllocateAlignedBuffer (MemoryType, Pages, 0, HostAddress);\r
428}\r
429\r
430/**\r
431 Allocates pages that are suitable for an DmaMap() of type\r
432 MapOperationBusMasterCommonBuffer mapping, at the requested alignment.\r
433\r
434 @param MemoryType The type of memory to allocate,\r
435 EfiBootServicesData or EfiRuntimeServicesData.\r
436 @param Pages The number of pages to allocate.\r
437 @param Alignment Alignment in bytes of the base of the returned\r
438 buffer (must be a power of 2)\r
439 @param HostAddress A pointer to store the base system memory\r
440 address of the allocated range.\r
441\r
442 @retval EFI_SUCCESS The requested memory pages were allocated.\r
443 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
444 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
445\r
446**/\r
447EFI_STATUS\r
448EFIAPI\r
449DmaAllocateAlignedBuffer (\r
450 IN EFI_MEMORY_TYPE MemoryType,\r
451 IN UINTN Pages,\r
452 IN UINTN Alignment,\r
453 OUT VOID **HostAddress\r
454 )\r
455{\r
456 EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor;\r
457 VOID *Allocation;\r
458 UINT64 MemType;\r
459 UNCACHED_ALLOCATION *Alloc;\r
460 EFI_STATUS Status;\r
461\r
462 if (Alignment == 0) {\r
463 Alignment = EFI_PAGE_SIZE;\r
464 }\r
465\r
466 if (HostAddress == NULL ||\r
467 (Alignment & (Alignment - 1)) != 0) {\r
468 return EFI_INVALID_PARAMETER;\r
469 }\r
470\r
62a75650
AB
471 if (MemoryType == EfiBootServicesData ||\r
472 MemoryType == EfiRuntimeServicesData) {\r
473 Allocation = InternalAllocateAlignedPages (MemoryType, Pages, Alignment);\r
723102c7
AB
474 } else {\r
475 return EFI_INVALID_PARAMETER;\r
476 }\r
477\r
478 if (Allocation == NULL) {\r
479 return EFI_OUT_OF_RESOURCES;\r
480 }\r
481\r
482 // Get the cacheability of the region\r
483 Status = gDS->GetMemorySpaceDescriptor ((UINTN)Allocation, &GcdDescriptor);\r
484 if (EFI_ERROR(Status)) {\r
485 goto FreeBuffer;\r
486 }\r
487\r
488 // Choose a suitable uncached memory type that is supported by the region\r
489 if (GcdDescriptor.Capabilities & EFI_MEMORY_WC) {\r
490 MemType = EFI_MEMORY_WC;\r
491 } else if (GcdDescriptor.Capabilities & EFI_MEMORY_UC) {\r
492 MemType = EFI_MEMORY_UC;\r
493 } else {\r
494 Status = EFI_UNSUPPORTED;\r
495 goto FreeBuffer;\r
496 }\r
497\r
498 Alloc = AllocatePool (sizeof *Alloc);\r
499 if (Alloc == NULL) {\r
500 goto FreeBuffer;\r
501 }\r
502\r
503 Alloc->HostAddress = Allocation;\r
504 Alloc->NumPages = Pages;\r
505 Alloc->Attributes = GcdDescriptor.Attributes;\r
506\r
507 InsertHeadList (&UncachedAllocationList, &Alloc->Link);\r
508\r
509 // Remap the region with the new attributes\r
510 Status = gDS->SetMemorySpaceAttributes ((PHYSICAL_ADDRESS)(UINTN)Allocation,\r
511 EFI_PAGES_TO_SIZE (Pages),\r
512 MemType);\r
513 if (EFI_ERROR (Status)) {\r
514 goto FreeAlloc;\r
515 }\r
516\r
517 Status = mCpu->FlushDataCache (mCpu,\r
518 (PHYSICAL_ADDRESS)(UINTN)Allocation,\r
519 EFI_PAGES_TO_SIZE (Pages),\r
520 EfiCpuFlushTypeInvalidate);\r
521 if (EFI_ERROR (Status)) {\r
522 goto FreeAlloc;\r
523 }\r
524\r
525 *HostAddress = Allocation;\r
526\r
527 return EFI_SUCCESS;\r
528\r
529FreeAlloc:\r
530 RemoveEntryList (&Alloc->Link);\r
531 FreePool (Alloc);\r
532\r
533FreeBuffer:\r
534 FreePages (Allocation, Pages);\r
535 return Status;\r
536}\r
537\r
538\r
539/**\r
540 Frees memory that was allocated with DmaAllocateBuffer().\r
541\r
542 @param Pages The number of pages to free.\r
543 @param HostAddress The base system memory address of the allocated\r
544 range.\r
545\r
546 @retval EFI_SUCCESS The requested memory pages were freed.\r
547 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and\r
548 Pages was not allocated with\r
549 DmaAllocateBuffer().\r
550\r
551**/\r
552EFI_STATUS\r
553EFIAPI\r
554DmaFreeBuffer (\r
555 IN UINTN Pages,\r
556 IN VOID *HostAddress\r
557 )\r
558{\r
559 LIST_ENTRY *Link;\r
560 UNCACHED_ALLOCATION *Alloc;\r
561 BOOLEAN Found;\r
562 EFI_STATUS Status;\r
563\r
564 if (HostAddress == NULL) {\r
565 return EFI_INVALID_PARAMETER;\r
566 }\r
567\r
568 for (Link = GetFirstNode (&UncachedAllocationList), Found = FALSE;\r
569 !IsNull (&UncachedAllocationList, Link);\r
570 Link = GetNextNode (&UncachedAllocationList, Link)) {\r
571\r
572 Alloc = BASE_CR (Link, UNCACHED_ALLOCATION, Link);\r
573 if (Alloc->HostAddress == HostAddress && Alloc->NumPages == Pages) {\r
574 Found = TRUE;\r
575 break;\r
576 }\r
577 }\r
578\r
579 if (!Found) {\r
580 ASSERT (FALSE);\r
581 return EFI_INVALID_PARAMETER;\r
582 }\r
583\r
584 RemoveEntryList (&Alloc->Link);\r
585\r
586 Status = gDS->SetMemorySpaceAttributes ((PHYSICAL_ADDRESS)(UINTN)HostAddress,\r
587 EFI_PAGES_TO_SIZE (Pages),\r
588 Alloc->Attributes);\r
589 if (EFI_ERROR (Status)) {\r
590 goto FreeAlloc;\r
591 }\r
592\r
593 //\r
594 // If we fail to restore the original attributes, it is better to leak the\r
595 // memory than to return it to the heap\r
596 //\r
597 FreePages (HostAddress, Pages);\r
598\r
599FreeAlloc:\r
600 FreePool (Alloc);\r
601 return Status;\r
602}\r
603\r
604\r
605EFI_STATUS\r
606EFIAPI\r
607NonCoherentDmaLibConstructor (\r
608 IN EFI_HANDLE ImageHandle,\r
609 IN EFI_SYSTEM_TABLE *SystemTable\r
610 )\r
611{\r
612 InitializeListHead (&UncachedAllocationList);\r
613\r
62a75650
AB
614 //\r
615 // Ensure that the combination of DMA addressing offset and limit produces\r
616 // a sane value.\r
617 //\r
618 ASSERT (PcdGet64 (PcdDmaDeviceLimit) > PcdGet64 (PcdDmaDeviceOffset));\r
619\r
620 mDmaHostAddressLimit = PcdGet64 (PcdDmaDeviceLimit) -\r
621 PcdGet64 (PcdDmaDeviceOffset);\r
622\r
723102c7
AB
623 // Get the Cpu protocol for later use\r
624 return gBS->LocateProtocol (&gEfiCpuArchProtocolGuid, NULL, (VOID **)&mCpu);\r
625}\r