CoherentDmaLib.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /** @file
  2. Generic ARM implementation of DmaLib.h
  3. Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include <Uefi.h>
  7. #include <Library/DebugLib.h>
  8. #include <Library/DmaLib.h>
  9. #include <Library/MemoryAllocationLib.h>
  10. STATIC
  11. PHYSICAL_ADDRESS
  12. HostToDeviceAddress (
  13. IN VOID *Address
  14. )
  15. {
  16. return (PHYSICAL_ADDRESS)(UINTN)Address + PcdGet64 (PcdDmaDeviceOffset);
  17. }
  18. /**
  19. Provides the DMA controller-specific addresses needed to access system memory.
  20. Operation is relative to the DMA bus master.
  21. @param Operation Indicates if the bus master is going to read or write to system memory.
  22. @param HostAddress The system memory address to map to the DMA controller.
  23. @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
  24. that were mapped.
  25. @param DeviceAddress The resulting map address for the bus master controller to use to
  26. access the hosts HostAddress.
  27. @param Mapping A resulting value to pass to Unmap().
  28. @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
  29. @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
  30. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  31. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
  32. @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
  33. **/
  34. EFI_STATUS
  35. EFIAPI
  36. DmaMap (
  37. IN DMA_MAP_OPERATION Operation,
  38. IN VOID *HostAddress,
  39. IN OUT UINTN *NumberOfBytes,
  40. OUT PHYSICAL_ADDRESS *DeviceAddress,
  41. OUT VOID **Mapping
  42. )
  43. {
  44. if ((HostAddress == NULL) ||
  45. (NumberOfBytes == NULL) ||
  46. (DeviceAddress == NULL) ||
  47. (Mapping == NULL))
  48. {
  49. return EFI_INVALID_PARAMETER;
  50. }
  51. *DeviceAddress = HostToDeviceAddress (HostAddress);
  52. *Mapping = NULL;
  53. return EFI_SUCCESS;
  54. }
  55. /**
  56. Completes the DmaMapBusMasterRead(), DmaMapBusMasterWrite(), or DmaMapBusMasterCommonBuffer()
  57. operation and releases any corresponding resources.
  58. @param Mapping The mapping value returned from DmaMap*().
  59. @retval EFI_SUCCESS The range was unmapped.
  60. @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
  61. **/
  62. EFI_STATUS
  63. EFIAPI
  64. DmaUnmap (
  65. IN VOID *Mapping
  66. )
  67. {
  68. return EFI_SUCCESS;
  69. }
  70. /**
  71. Allocates pages that are suitable for an DmaMap() of type MapOperationBusMasterCommonBuffer.
  72. mapping.
  73. @param MemoryType The type of memory to allocate, EfiBootServicesData or
  74. EfiRuntimeServicesData.
  75. @param Pages The number of pages to allocate.
  76. @param HostAddress A pointer to store the base system memory address of the
  77. allocated range.
  78. @retval EFI_SUCCESS The requested memory pages were allocated.
  79. @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
  80. MEMORY_WRITE_COMBINE and MEMORY_CACHED.
  81. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  82. @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
  83. **/
  84. EFI_STATUS
  85. EFIAPI
  86. DmaAllocateBuffer (
  87. IN EFI_MEMORY_TYPE MemoryType,
  88. IN UINTN Pages,
  89. OUT VOID **HostAddress
  90. )
  91. {
  92. return DmaAllocateAlignedBuffer (MemoryType, Pages, 0, HostAddress);
  93. }
  94. /**
  95. Allocates pages that are suitable for an DmaMap() of type
  96. MapOperationBusMasterCommonBuffer mapping, at the requested alignment.
  97. @param MemoryType The type of memory to allocate, EfiBootServicesData or
  98. EfiRuntimeServicesData.
  99. @param Pages The number of pages to allocate.
  100. @param Alignment Alignment in bytes of the base of the returned
  101. buffer (must be a power of 2)
  102. @param HostAddress A pointer to store the base system memory address of the
  103. allocated range.
  104. @retval EFI_SUCCESS The requested memory pages were allocated.
  105. @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
  106. MEMORY_WRITE_COMBINE and MEMORY_CACHED.
  107. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  108. @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
  109. **/
  110. EFI_STATUS
  111. EFIAPI
  112. DmaAllocateAlignedBuffer (
  113. IN EFI_MEMORY_TYPE MemoryType,
  114. IN UINTN Pages,
  115. IN UINTN Alignment,
  116. OUT VOID **HostAddress
  117. )
  118. {
  119. if (Alignment == 0) {
  120. Alignment = EFI_PAGE_SIZE;
  121. }
  122. if ((HostAddress == NULL) ||
  123. ((Alignment & (Alignment - 1)) != 0))
  124. {
  125. return EFI_INVALID_PARAMETER;
  126. }
  127. //
  128. // The only valid memory types are EfiBootServicesData and EfiRuntimeServicesData
  129. //
  130. if (MemoryType == EfiBootServicesData) {
  131. *HostAddress = AllocateAlignedPages (Pages, Alignment);
  132. } else if (MemoryType == EfiRuntimeServicesData) {
  133. *HostAddress = AllocateAlignedRuntimePages (Pages, Alignment);
  134. } else {
  135. return EFI_INVALID_PARAMETER;
  136. }
  137. if (*HostAddress == NULL) {
  138. return EFI_OUT_OF_RESOURCES;
  139. }
  140. return EFI_SUCCESS;
  141. }
  142. /**
  143. Frees memory that was allocated with DmaAllocateBuffer().
  144. @param Pages The number of pages to free.
  145. @param HostAddress The base system memory address of the allocated range.
  146. @retval EFI_SUCCESS The requested memory pages were freed.
  147. @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
  148. was not allocated with DmaAllocateBuffer().
  149. **/
  150. EFI_STATUS
  151. EFIAPI
  152. DmaFreeBuffer (
  153. IN UINTN Pages,
  154. IN VOID *HostAddress
  155. )
  156. {
  157. if (HostAddress == NULL) {
  158. return EFI_INVALID_PARAMETER;
  159. }
  160. FreePages (HostAddress, Pages);
  161. return EFI_SUCCESS;
  162. }