NonCoherentDmaLib.c 20 KB

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