]> git.proxmox.com Git - mirror_edk2.git/blame - EmbeddedPkg/Drivers/NonCoherentIoMmuDxe/NonCoherentIoMmuDxe.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[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
e7108d0e
MK
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
49054b6b
AB
116 )\r
117{\r
e7108d0e 118 DMA_MAP_OPERATION DmaOperation;\r
49054b6b
AB
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
e7108d0e
MK
141 return DmaMap (\r
142 DmaOperation,\r
143 HostAddress,\r
144 NumberOfBytes,\r
145 DeviceAddress,\r
146 Mapping\r
147 );\r
49054b6b
AB
148}\r
149\r
150/**\r
151 Completes the Map() operation and releases any corresponding resources.\r
152\r
153 @param This The protocol instance pointer.\r
154 @param Mapping The mapping value returned from Map().\r
155\r
156 @retval EFI_SUCCESS The range was unmapped.\r
157 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by\r
158 Map().\r
159 @retval EFI_DEVICE_ERROR The data was not committed to the target system\r
160 memory.\r
161**/\r
162STATIC\r
163EFI_STATUS\r
164EFIAPI\r
165NonCoherentIoMmuUnmap (\r
e7108d0e
MK
166 IN EDKII_IOMMU_PROTOCOL *This,\r
167 IN VOID *Mapping\r
49054b6b
AB
168 )\r
169{\r
170 return DmaUnmap (Mapping);\r
171}\r
172\r
173/**\r
174 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or\r
175 OperationBusMasterCommonBuffer64 mapping.\r
176\r
177 @param This The protocol instance pointer.\r
178 @param Type This parameter is not used and must be ignored.\r
179 @param MemoryType The type of memory to allocate,\r
180 EfiBootServicesData or EfiRuntimeServicesData.\r
181 @param Pages The number of pages to allocate.\r
182 @param HostAddress A pointer to store the base system memory\r
183 address of the allocated range.\r
184 @param Attributes The requested bit mask of attributes for the\r
185 allocated range.\r
186\r
187 @retval EFI_SUCCESS The requested memory pages were allocated.\r
188 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal\r
189 attribute bits are MEMORY_WRITE_COMBINE and\r
190 MEMORY_CACHED.\r
191 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
192 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
193\r
194**/\r
195STATIC\r
196EFI_STATUS\r
197EFIAPI\r
198NonCoherentIoMmuAllocateBuffer (\r
e7108d0e
MK
199 IN EDKII_IOMMU_PROTOCOL *This,\r
200 IN EFI_ALLOCATE_TYPE Type,\r
201 IN EFI_MEMORY_TYPE MemoryType,\r
202 IN UINTN Pages,\r
203 IN OUT VOID **HostAddress,\r
204 IN UINT64 Attributes\r
49054b6b
AB
205 )\r
206{\r
207 return DmaAllocateBuffer (MemoryType, Pages, HostAddress);\r
208}\r
209\r
210/**\r
211 Frees memory that was allocated with AllocateBuffer().\r
212\r
213 @param This The protocol instance pointer.\r
214 @param Pages The number of pages to free.\r
215 @param HostAddress The base system memory address of the allocated\r
216 range.\r
217\r
218 @retval EFI_SUCCESS The requested memory pages were freed.\r
219 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and\r
220 Pages was not allocated with AllocateBuffer().\r
221\r
222**/\r
223STATIC\r
224EFI_STATUS\r
225EFIAPI\r
226NonCoherentIoMmuFreeBuffer (\r
e7108d0e
MK
227 IN EDKII_IOMMU_PROTOCOL *This,\r
228 IN UINTN Pages,\r
229 IN VOID *HostAddress\r
49054b6b
AB
230 )\r
231{\r
232 return DmaFreeBuffer (Pages, HostAddress);\r
233}\r
234\r
235STATIC EDKII_IOMMU_PROTOCOL mNonCoherentIoMmuOps = {\r
236 EDKII_IOMMU_PROTOCOL_REVISION,\r
237 NonCoherentIoMmuSetAttribute,\r
238 NonCoherentIoMmuMap,\r
239 NonCoherentIoMmuUnmap,\r
240 NonCoherentIoMmuAllocateBuffer,\r
241 NonCoherentIoMmuFreeBuffer,\r
242};\r
243\r
49054b6b
AB
244EFI_STATUS\r
245EFIAPI\r
246NonCoherentIoMmuDxeEntryPoint (\r
e7108d0e
MK
247 IN EFI_HANDLE ImageHandle,\r
248 IN EFI_SYSTEM_TABLE *SystemTable\r
49054b6b
AB
249 )\r
250{\r
e7108d0e
MK
251 return gBS->InstallMultipleProtocolInterfaces (\r
252 &ImageHandle,\r
253 &gEdkiiIoMmuProtocolGuid,\r
254 &mNonCoherentIoMmuOps,\r
255 NULL\r
256 );\r
49054b6b 257}\r