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