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