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