NonCoherentIoMmuDxe.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /** @file
  2. Copyright (c) 2019, Linaro, Ltd. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #include <PiDxe.h>
  6. #include <Library/BaseLib.h>
  7. #include <Library/DebugLib.h>
  8. #include <Library/DmaLib.h>
  9. #include <Library/UefiBootServicesTableLib.h>
  10. #include <Protocol/IoMmu.h>
  11. /**
  12. Set IOMMU attribute for a system memory.
  13. If the IOMMU protocol exists, the system memory cannot be used
  14. for DMA by default.
  15. When a device requests a DMA access for a system memory,
  16. the device driver need use SetAttribute() to update the IOMMU
  17. attribute to request DMA access (read and/or write).
  18. The DeviceHandle is used to identify which device submits the request.
  19. The IOMMU implementation need translate the device path to an IOMMU device
  20. ID, and set IOMMU hardware register accordingly.
  21. 1) DeviceHandle can be a standard PCI device.
  22. The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.
  23. The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.
  24. The memory for BusMasterCommonBuffer need set
  25. EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.
  26. After the memory is used, the memory need set 0 to keep it being
  27. protected.
  28. 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).
  29. The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or
  30. EDKII_IOMMU_ACCESS_WRITE.
  31. @param[in] This The protocol instance pointer.
  32. @param[in] DeviceHandle The device who initiates the DMA access
  33. request.
  34. @param[in] Mapping The mapping value returned from Map().
  35. @param[in] IoMmuAccess The IOMMU access.
  36. @retval EFI_SUCCESS The IoMmuAccess is set for the memory range
  37. specified by DeviceAddress and Length.
  38. @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.
  39. @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by
  40. Map().
  41. @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination
  42. of access.
  43. @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.
  44. @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported
  45. by the IOMMU.
  46. @retval EFI_UNSUPPORTED The IOMMU does not support the memory range
  47. specified by Mapping.
  48. @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
  49. modify the IOMMU access.
  50. @retval EFI_DEVICE_ERROR The IOMMU device reported an error while
  51. attempting the operation.
  52. **/
  53. STATIC
  54. EFI_STATUS
  55. EFIAPI
  56. NonCoherentIoMmuSetAttribute (
  57. IN EDKII_IOMMU_PROTOCOL *This,
  58. IN EFI_HANDLE DeviceHandle,
  59. IN VOID *Mapping,
  60. IN UINT64 IoMmuAccess
  61. )
  62. {
  63. return EFI_UNSUPPORTED;
  64. }
  65. /**
  66. Provides the controller-specific addresses required to access system memory
  67. from a DMA bus master. On SEV guest, the DMA operations must be performed on
  68. shared buffer hence we allocate a bounce buffer to map the HostAddress to a
  69. DeviceAddress. The Encryption attribute is removed from the DeviceAddress
  70. buffer.
  71. @param This The protocol instance pointer.
  72. @param Operation Indicates if the bus master is going to read or
  73. write to system memory.
  74. @param HostAddress The system memory address to map to the PCI
  75. controller.
  76. @param NumberOfBytes On input the number of bytes to map. On output
  77. the number of bytes that were mapped.
  78. @param DeviceAddress The resulting map address for the bus master
  79. PCI controller to use to access the hosts
  80. HostAddress.
  81. @param Mapping A resulting value to pass to Unmap().
  82. @retval EFI_SUCCESS The range was mapped for the returned
  83. NumberOfBytes.
  84. @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common
  85. buffer.
  86. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  87. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a
  88. lack of resources.
  89. @retval EFI_DEVICE_ERROR The system hardware could not map the requested
  90. address.
  91. **/
  92. STATIC
  93. EFI_STATUS
  94. EFIAPI
  95. NonCoherentIoMmuMap (
  96. IN EDKII_IOMMU_PROTOCOL *This,
  97. IN EDKII_IOMMU_OPERATION Operation,
  98. IN VOID *HostAddress,
  99. IN OUT UINTN *NumberOfBytes,
  100. OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
  101. OUT VOID **Mapping
  102. )
  103. {
  104. DMA_MAP_OPERATION DmaOperation;
  105. switch (Operation) {
  106. case EdkiiIoMmuOperationBusMasterRead:
  107. case EdkiiIoMmuOperationBusMasterRead64:
  108. DmaOperation = MapOperationBusMasterRead;
  109. break;
  110. case EdkiiIoMmuOperationBusMasterWrite:
  111. case EdkiiIoMmuOperationBusMasterWrite64:
  112. DmaOperation = MapOperationBusMasterWrite;
  113. break;
  114. case EdkiiIoMmuOperationBusMasterCommonBuffer:
  115. case EdkiiIoMmuOperationBusMasterCommonBuffer64:
  116. DmaOperation = MapOperationBusMasterCommonBuffer;
  117. break;
  118. default:
  119. ASSERT (FALSE);
  120. return EFI_INVALID_PARAMETER;
  121. }
  122. return DmaMap (
  123. DmaOperation,
  124. HostAddress,
  125. NumberOfBytes,
  126. DeviceAddress,
  127. Mapping
  128. );
  129. }
  130. /**
  131. Completes the Map() operation and releases any corresponding resources.
  132. @param This The protocol instance pointer.
  133. @param Mapping The mapping value returned from Map().
  134. @retval EFI_SUCCESS The range was unmapped.
  135. @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by
  136. Map().
  137. @retval EFI_DEVICE_ERROR The data was not committed to the target system
  138. memory.
  139. **/
  140. STATIC
  141. EFI_STATUS
  142. EFIAPI
  143. NonCoherentIoMmuUnmap (
  144. IN EDKII_IOMMU_PROTOCOL *This,
  145. IN VOID *Mapping
  146. )
  147. {
  148. return DmaUnmap (Mapping);
  149. }
  150. /**
  151. Allocates pages that are suitable for an OperationBusMasterCommonBuffer or
  152. OperationBusMasterCommonBuffer64 mapping.
  153. @param This The protocol instance pointer.
  154. @param Type This parameter is not used and must be ignored.
  155. @param MemoryType The type of memory to allocate,
  156. EfiBootServicesData or EfiRuntimeServicesData.
  157. @param Pages The number of pages to allocate.
  158. @param HostAddress A pointer to store the base system memory
  159. address of the allocated range.
  160. @param Attributes The requested bit mask of attributes for the
  161. allocated range.
  162. @retval EFI_SUCCESS The requested memory pages were allocated.
  163. @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal
  164. attribute bits are MEMORY_WRITE_COMBINE and
  165. MEMORY_CACHED.
  166. @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
  167. @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
  168. **/
  169. STATIC
  170. EFI_STATUS
  171. EFIAPI
  172. NonCoherentIoMmuAllocateBuffer (
  173. IN EDKII_IOMMU_PROTOCOL *This,
  174. IN EFI_ALLOCATE_TYPE Type,
  175. IN EFI_MEMORY_TYPE MemoryType,
  176. IN UINTN Pages,
  177. IN OUT VOID **HostAddress,
  178. IN UINT64 Attributes
  179. )
  180. {
  181. return DmaAllocateBuffer (MemoryType, Pages, HostAddress);
  182. }
  183. /**
  184. Frees memory that was allocated with AllocateBuffer().
  185. @param This The protocol instance pointer.
  186. @param Pages The number of pages to free.
  187. @param HostAddress The base system memory address of the allocated
  188. range.
  189. @retval EFI_SUCCESS The requested memory pages were freed.
  190. @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and
  191. Pages was not allocated with AllocateBuffer().
  192. **/
  193. STATIC
  194. EFI_STATUS
  195. EFIAPI
  196. NonCoherentIoMmuFreeBuffer (
  197. IN EDKII_IOMMU_PROTOCOL *This,
  198. IN UINTN Pages,
  199. IN VOID *HostAddress
  200. )
  201. {
  202. return DmaFreeBuffer (Pages, HostAddress);
  203. }
  204. STATIC EDKII_IOMMU_PROTOCOL mNonCoherentIoMmuOps = {
  205. EDKII_IOMMU_PROTOCOL_REVISION,
  206. NonCoherentIoMmuSetAttribute,
  207. NonCoherentIoMmuMap,
  208. NonCoherentIoMmuUnmap,
  209. NonCoherentIoMmuAllocateBuffer,
  210. NonCoherentIoMmuFreeBuffer,
  211. };
  212. EFI_STATUS
  213. EFIAPI
  214. NonCoherentIoMmuDxeEntryPoint (
  215. IN EFI_HANDLE ImageHandle,
  216. IN EFI_SYSTEM_TABLE *SystemTable
  217. )
  218. {
  219. return gBS->InstallMultipleProtocolInterfaces (
  220. &ImageHandle,
  221. &gEdkiiIoMmuProtocolGuid,
  222. &mNonCoherentIoMmuOps,
  223. NULL
  224. );
  225. }