]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/IoMmuDxe/AmdSevIoMmu.c
OvmfPkg/IoMmuDxe: propagate errors from AmdSevInstallIoMmuProtocol()
[mirror_edk2.git] / OvmfPkg / IoMmuDxe / AmdSevIoMmu.c
CommitLineData
f9d129e6
BS
1/** @file\r
2\r
812568fb
LE
3 The protocol provides support to allocate, free, map and umap a DMA buffer\r
4 for bus master (e.g PciHostBridge). When SEV is enabled, the DMA operations\r
5 must be performed on unencrypted buffer hence we use a bounce buffer to map\r
6 the guest buffer into an unencrypted DMA buffer.\r
f9d129e6
BS
7\r
8 Copyright (c) 2017, AMD Inc. All rights reserved.<BR>\r
9 Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>\r
10\r
11 This program and the accompanying materials are licensed and made available\r
12 under the terms and conditions of the BSD License which accompanies this\r
13 distribution. The full text of the license may be found at\r
14 http://opensource.org/licenses/bsd-license.php\r
15\r
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
18\r
19**/\r
20\r
21#include "AmdSevIoMmu.h"\r
22\r
23typedef struct {\r
24 EDKII_IOMMU_OPERATION Operation;\r
25 UINTN NumberOfBytes;\r
26 UINTN NumberOfPages;\r
c7ef2ed2 27 EFI_PHYSICAL_ADDRESS CryptedAddress;\r
dc194ce3 28 EFI_PHYSICAL_ADDRESS PlainTextAddress;\r
f9d129e6
BS
29} MAP_INFO;\r
30\r
31#define NO_MAPPING (VOID *) (UINTN) -1\r
32\r
33/**\r
812568fb
LE
34 Provides the controller-specific addresses required to access system memory\r
35 from a DMA bus master. On SEV guest, the DMA operations must be performed on\r
36 shared buffer hence we allocate a bounce buffer to map the HostAddress to a\r
37 DeviceAddress. The Encryption attribute is removed from the DeviceAddress\r
38 buffer.\r
f9d129e6
BS
39\r
40 @param This The protocol instance pointer.\r
41 @param Operation Indicates if the bus master is going to read or\r
42 write to system memory.\r
812568fb
LE
43 @param HostAddress The system memory address to map to the PCI\r
44 controller.\r
f9d129e6 45 @param NumberOfBytes On input the number of bytes to map. On output\r
812568fb
LE
46 the number of bytes that were mapped.\r
47 @param DeviceAddress The resulting map address for the bus master\r
48 PCI controller to use to access the hosts\r
49 HostAddress.\r
f9d129e6
BS
50 @param Mapping A resulting value to pass to Unmap().\r
51\r
812568fb
LE
52 @retval EFI_SUCCESS The range was mapped for the returned\r
53 NumberOfBytes.\r
54 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common\r
55 buffer.\r
f9d129e6 56 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
812568fb
LE
57 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r
58 lack of resources.\r
59 @retval EFI_DEVICE_ERROR The system hardware could not map the requested\r
60 address.\r
f9d129e6
BS
61\r
62**/\r
63EFI_STATUS\r
64EFIAPI\r
65IoMmuMap (\r
66 IN EDKII_IOMMU_PROTOCOL *This,\r
67 IN EDKII_IOMMU_OPERATION Operation,\r
68 IN VOID *HostAddress,\r
69 IN OUT UINTN *NumberOfBytes,\r
70 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r
71 OUT VOID **Mapping\r
72 )\r
73{\r
74 EFI_STATUS Status;\r
75 EFI_PHYSICAL_ADDRESS PhysicalAddress;\r
76 MAP_INFO *MapInfo;\r
77 EFI_PHYSICAL_ADDRESS DmaMemoryTop;\r
78 EFI_ALLOCATE_TYPE AllocateType;\r
79\r
80 if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL ||\r
81 Mapping == NULL) {\r
82 return EFI_INVALID_PARAMETER;\r
83 }\r
84\r
85 //\r
86 // Make sure that Operation is valid\r
87 //\r
88 if ((UINT32) Operation >= EdkiiIoMmuOperationMaximum) {\r
89 return EFI_INVALID_PARAMETER;\r
90 }\r
91 PhysicalAddress = (EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress;\r
92\r
93 DmaMemoryTop = (UINTN)-1;\r
94 AllocateType = AllocateAnyPages;\r
95\r
96 if (((Operation != EdkiiIoMmuOperationBusMasterRead64 &&\r
97 Operation != EdkiiIoMmuOperationBusMasterWrite64 &&\r
98 Operation != EdkiiIoMmuOperationBusMasterCommonBuffer64)) &&\r
99 ((PhysicalAddress + *NumberOfBytes) > SIZE_4GB)) {\r
100 //\r
101 // If the root bridge or the device cannot handle performing DMA above\r
102 // 4GB but any part of the DMA transfer being mapped is above 4GB, then\r
103 // map the DMA transfer to a buffer below 4GB.\r
104 //\r
105 DmaMemoryTop = SIZE_4GB - 1;\r
106 AllocateType = AllocateMaxAddress;\r
107\r
108 if (Operation == EdkiiIoMmuOperationBusMasterCommonBuffer ||\r
109 Operation == EdkiiIoMmuOperationBusMasterCommonBuffer64) {\r
110 //\r
111 // Common Buffer operations can not be remapped. If the common buffer\r
812568fb
LE
112 // if above 4GB, then it is not possible to generate a mapping, so\r
113 // return an error.\r
f9d129e6
BS
114 //\r
115 return EFI_UNSUPPORTED;\r
116 }\r
117 }\r
118\r
119 //\r
120 // CommandBuffer was allocated by us (AllocateBuffer) and is already in\r
121 // unencryted buffer so no need to create bounce buffer\r
122 //\r
123 if (Operation == EdkiiIoMmuOperationBusMasterCommonBuffer ||\r
124 Operation == EdkiiIoMmuOperationBusMasterCommonBuffer64) {\r
125 *Mapping = NO_MAPPING;\r
126 *DeviceAddress = PhysicalAddress;\r
127\r
128 return EFI_SUCCESS;\r
129 }\r
130\r
131 //\r
132 // Allocate a MAP_INFO structure to remember the mapping when Unmap() is\r
133 // called later.\r
134 //\r
135 MapInfo = AllocatePool (sizeof (MAP_INFO));\r
136 if (MapInfo == NULL) {\r
137 *NumberOfBytes = 0;\r
138 return EFI_OUT_OF_RESOURCES;\r
139 }\r
140\r
141 //\r
142 // Initialize the MAP_INFO structure\r
143 //\r
144 MapInfo->Operation = Operation;\r
145 MapInfo->NumberOfBytes = *NumberOfBytes;\r
146 MapInfo->NumberOfPages = EFI_SIZE_TO_PAGES (MapInfo->NumberOfBytes);\r
c7ef2ed2 147 MapInfo->CryptedAddress = PhysicalAddress;\r
dc194ce3 148 MapInfo->PlainTextAddress = DmaMemoryTop;\r
f9d129e6
BS
149\r
150 //\r
151 // Allocate a buffer to map the transfer to.\r
152 //\r
153 Status = gBS->AllocatePages (\r
154 AllocateType,\r
155 EfiBootServicesData,\r
156 MapInfo->NumberOfPages,\r
dc194ce3 157 &MapInfo->PlainTextAddress\r
f9d129e6
BS
158 );\r
159 if (EFI_ERROR (Status)) {\r
160 FreePool (MapInfo);\r
161 *NumberOfBytes = 0;\r
162 return Status;\r
163 }\r
164\r
165 //\r
166 // Clear the memory encryption mask from the device buffer\r
167 //\r
812568fb
LE
168 Status = MemEncryptSevClearPageEncMask (\r
169 0,\r
dc194ce3 170 MapInfo->PlainTextAddress,\r
812568fb
LE
171 MapInfo->NumberOfPages,\r
172 TRUE\r
173 );\r
f9d129e6
BS
174 ASSERT_EFI_ERROR(Status);\r
175\r
176 //\r
177 // If this is a read operation from the Bus Master's point of view,\r
178 // then copy the contents of the real buffer into the mapped buffer\r
179 // so the Bus Master can read the contents of the real buffer.\r
180 //\r
181 if (Operation == EdkiiIoMmuOperationBusMasterRead ||\r
182 Operation == EdkiiIoMmuOperationBusMasterRead64) {\r
183 CopyMem (\r
dc194ce3 184 (VOID *) (UINTN) MapInfo->PlainTextAddress,\r
c7ef2ed2 185 (VOID *) (UINTN) MapInfo->CryptedAddress,\r
f9d129e6
BS
186 MapInfo->NumberOfBytes\r
187 );\r
188 }\r
189\r
190 //\r
191 // The DeviceAddress is the address of the maped buffer below 4GB\r
192 //\r
dc194ce3 193 *DeviceAddress = MapInfo->PlainTextAddress;\r
f9d129e6
BS
194\r
195 //\r
196 // Return a pointer to the MAP_INFO structure in Mapping\r
197 //\r
198 *Mapping = MapInfo;\r
199\r
812568fb
LE
200 DEBUG ((\r
201 DEBUG_VERBOSE,\r
c7ef2ed2 202 "%a PlainText 0x%Lx Crypted 0x%Lx Pages 0x%Lx Bytes 0x%Lx\n",\r
812568fb 203 __FUNCTION__,\r
dc194ce3 204 MapInfo->PlainTextAddress,\r
c7ef2ed2 205 MapInfo->CryptedAddress,\r
60aa3a0e
LE
206 (UINT64)MapInfo->NumberOfPages,\r
207 (UINT64)MapInfo->NumberOfBytes\r
812568fb 208 ));\r
f9d129e6
BS
209\r
210 return EFI_SUCCESS;\r
211}\r
212\r
213/**\r
214 Completes the Map() operation and releases any corresponding resources.\r
215\r
216 @param This The protocol instance pointer.\r
217 @param Mapping The mapping value returned from Map().\r
218\r
219 @retval EFI_SUCCESS The range was unmapped.\r
812568fb
LE
220 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by\r
221 Map().\r
222 @retval EFI_DEVICE_ERROR The data was not committed to the target system\r
223 memory.\r
f9d129e6
BS
224**/\r
225EFI_STATUS\r
226EFIAPI\r
227IoMmuUnmap (\r
228 IN EDKII_IOMMU_PROTOCOL *This,\r
229 IN VOID *Mapping\r
230 )\r
231{\r
232 MAP_INFO *MapInfo;\r
233 EFI_STATUS Status;\r
234\r
235 if (Mapping == NULL) {\r
236 return EFI_INVALID_PARAMETER;\r
237 }\r
238\r
239 //\r
240 // See if the Map() operation associated with this Unmap() required a mapping\r
241 // buffer. If a mapping buffer was not required, then this function simply\r
242 // buffer. If a mapping buffer was not required, then this function simply\r
243 //\r
244 if (Mapping == NO_MAPPING) {\r
245 return EFI_SUCCESS;\r
246 }\r
247\r
248 MapInfo = (MAP_INFO *)Mapping;\r
249\r
250 //\r
251 // If this is a write operation from the Bus Master's point of view,\r
252 // then copy the contents of the mapped buffer into the real buffer\r
253 // so the processor can read the contents of the real buffer.\r
254 //\r
255 if (MapInfo->Operation == EdkiiIoMmuOperationBusMasterWrite ||\r
256 MapInfo->Operation == EdkiiIoMmuOperationBusMasterWrite64) {\r
257 CopyMem (\r
c7ef2ed2 258 (VOID *) (UINTN) MapInfo->CryptedAddress,\r
dc194ce3 259 (VOID *) (UINTN) MapInfo->PlainTextAddress,\r
f9d129e6
BS
260 MapInfo->NumberOfBytes\r
261 );\r
262 }\r
263\r
812568fb
LE
264 DEBUG ((\r
265 DEBUG_VERBOSE,\r
c7ef2ed2 266 "%a PlainText 0x%Lx Crypted 0x%Lx Pages 0x%Lx Bytes 0x%Lx\n",\r
812568fb 267 __FUNCTION__,\r
dc194ce3 268 MapInfo->PlainTextAddress,\r
c7ef2ed2 269 MapInfo->CryptedAddress,\r
60aa3a0e
LE
270 (UINT64)MapInfo->NumberOfPages,\r
271 (UINT64)MapInfo->NumberOfBytes\r
812568fb 272 ));\r
f9d129e6
BS
273 //\r
274 // Restore the memory encryption mask\r
275 //\r
812568fb
LE
276 Status = MemEncryptSevSetPageEncMask (\r
277 0,\r
dc194ce3 278 MapInfo->PlainTextAddress,\r
812568fb
LE
279 MapInfo->NumberOfPages,\r
280 TRUE\r
281 );\r
f9d129e6
BS
282 ASSERT_EFI_ERROR(Status);\r
283\r
284 //\r
285 // Free the mapped buffer and the MAP_INFO structure.\r
286 //\r
dc194ce3 287 gBS->FreePages (MapInfo->PlainTextAddress, MapInfo->NumberOfPages);\r
f9d129e6
BS
288 FreePool (Mapping);\r
289 return EFI_SUCCESS;\r
290}\r
291\r
292/**\r
293 Allocates pages that are suitable for an OperationBusMasterCommonBuffer or\r
294 OperationBusMasterCommonBuffer64 mapping.\r
295\r
296 @param This The protocol instance pointer.\r
297 @param Type This parameter is not used and must be ignored.\r
812568fb
LE
298 @param MemoryType The type of memory to allocate,\r
299 EfiBootServicesData or EfiRuntimeServicesData.\r
f9d129e6 300 @param Pages The number of pages to allocate.\r
812568fb
LE
301 @param HostAddress A pointer to store the base system memory\r
302 address of the allocated range.\r
303 @param Attributes The requested bit mask of attributes for the\r
304 allocated range.\r
f9d129e6
BS
305\r
306 @retval EFI_SUCCESS The requested memory pages were allocated.\r
812568fb
LE
307 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal\r
308 attribute bits are MEMORY_WRITE_COMBINE and\r
309 MEMORY_CACHED.\r
f9d129e6
BS
310 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
311 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r
312\r
313**/\r
314EFI_STATUS\r
315EFIAPI\r
316IoMmuAllocateBuffer (\r
317 IN EDKII_IOMMU_PROTOCOL *This,\r
318 IN EFI_ALLOCATE_TYPE Type,\r
319 IN EFI_MEMORY_TYPE MemoryType,\r
320 IN UINTN Pages,\r
321 IN OUT VOID **HostAddress,\r
322 IN UINT64 Attributes\r
323 )\r
324{\r
325 EFI_STATUS Status;\r
326 EFI_PHYSICAL_ADDRESS PhysicalAddress;\r
327\r
328 //\r
329 // Validate Attributes\r
330 //\r
331 if ((Attributes & EDKII_IOMMU_ATTRIBUTE_INVALID_FOR_ALLOCATE_BUFFER) != 0) {\r
332 return EFI_UNSUPPORTED;\r
333 }\r
334\r
335 //\r
336 // Check for invalid inputs\r
337 //\r
338 if (HostAddress == NULL) {\r
339 return EFI_INVALID_PARAMETER;\r
340 }\r
341\r
342 //\r
343 // The only valid memory types are EfiBootServicesData and\r
344 // EfiRuntimeServicesData\r
345 //\r
346 if (MemoryType != EfiBootServicesData &&\r
347 MemoryType != EfiRuntimeServicesData) {\r
348 return EFI_INVALID_PARAMETER;\r
349 }\r
350\r
351 PhysicalAddress = (UINTN)-1;\r
352 if ((Attributes & EDKII_IOMMU_ATTRIBUTE_DUAL_ADDRESS_CYCLE) == 0) {\r
353 //\r
354 // Limit allocations to memory below 4GB\r
355 //\r
356 PhysicalAddress = SIZE_4GB - 1;\r
357 }\r
358 Status = gBS->AllocatePages (\r
359 AllocateMaxAddress,\r
360 MemoryType,\r
361 Pages,\r
362 &PhysicalAddress\r
363 );\r
364 if (!EFI_ERROR (Status)) {\r
365 *HostAddress = (VOID *) (UINTN) PhysicalAddress;\r
366\r
367 //\r
368 // Clear memory encryption mask\r
369 //\r
370 Status = MemEncryptSevClearPageEncMask (0, PhysicalAddress, Pages, TRUE);\r
371 ASSERT_EFI_ERROR(Status);\r
372 }\r
373\r
812568fb
LE
374 DEBUG ((\r
375 DEBUG_VERBOSE,\r
376 "%a Address 0x%Lx Pages 0x%Lx\n",\r
377 __FUNCTION__,\r
378 PhysicalAddress,\r
60aa3a0e 379 (UINT64)Pages\r
812568fb 380 ));\r
f9d129e6
BS
381 return Status;\r
382}\r
383\r
384/**\r
385 Frees memory that was allocated with AllocateBuffer().\r
386\r
387 @param This The protocol instance pointer.\r
388 @param Pages The number of pages to free.\r
812568fb
LE
389 @param HostAddress The base system memory address of the allocated\r
390 range.\r
f9d129e6
BS
391\r
392 @retval EFI_SUCCESS The requested memory pages were freed.\r
812568fb
LE
393 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and\r
394 Pages was not allocated with AllocateBuffer().\r
f9d129e6
BS
395\r
396**/\r
397EFI_STATUS\r
398EFIAPI\r
399IoMmuFreeBuffer (\r
400 IN EDKII_IOMMU_PROTOCOL *This,\r
401 IN UINTN Pages,\r
402 IN VOID *HostAddress\r
403 )\r
404{\r
405 EFI_STATUS Status;\r
406\r
407 //\r
408 // Set memory encryption mask\r
409 //\r
812568fb
LE
410 Status = MemEncryptSevSetPageEncMask (\r
411 0,\r
412 (EFI_PHYSICAL_ADDRESS)(UINTN)HostAddress,\r
413 Pages,\r
414 TRUE\r
415 );\r
f9d129e6
BS
416 ASSERT_EFI_ERROR(Status);\r
417\r
812568fb
LE
418 DEBUG ((\r
419 DEBUG_VERBOSE,\r
420 "%a Address 0x%Lx Pages 0x%Lx\n",\r
421 __FUNCTION__,\r
60aa3a0e
LE
422 (UINT64)(UINTN)HostAddress,\r
423 (UINT64)Pages\r
812568fb 424 ));\r
f9d129e6
BS
425 return gBS->FreePages ((EFI_PHYSICAL_ADDRESS) (UINTN) HostAddress, Pages);\r
426}\r
427\r
428\r
429/**\r
430 Set IOMMU attribute for a system memory.\r
431\r
432 If the IOMMU protocol exists, the system memory cannot be used\r
433 for DMA by default.\r
434\r
435 When a device requests a DMA access for a system memory,\r
436 the device driver need use SetAttribute() to update the IOMMU\r
437 attribute to request DMA access (read and/or write).\r
438\r
439 The DeviceHandle is used to identify which device submits the request.\r
812568fb
LE
440 The IOMMU implementation need translate the device path to an IOMMU device\r
441 ID, and set IOMMU hardware register accordingly.\r
f9d129e6
BS
442 1) DeviceHandle can be a standard PCI device.\r
443 The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.\r
444 The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.\r
812568fb
LE
445 The memory for BusMasterCommonBuffer need set\r
446 EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.\r
447 After the memory is used, the memory need set 0 to keep it being\r
448 protected.\r
f9d129e6 449 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).\r
812568fb
LE
450 The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or\r
451 EDKII_IOMMU_ACCESS_WRITE.\r
f9d129e6
BS
452\r
453 @param[in] This The protocol instance pointer.\r
812568fb
LE
454 @param[in] DeviceHandle The device who initiates the DMA access\r
455 request.\r
f9d129e6
BS
456 @param[in] Mapping The mapping value returned from Map().\r
457 @param[in] IoMmuAccess The IOMMU access.\r
458\r
812568fb
LE
459 @retval EFI_SUCCESS The IoMmuAccess is set for the memory range\r
460 specified by DeviceAddress and Length.\r
f9d129e6 461 @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.\r
812568fb
LE
462 @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by\r
463 Map().\r
464 @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination\r
465 of access.\r
f9d129e6 466 @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.\r
812568fb
LE
467 @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported\r
468 by the IOMMU.\r
469 @retval EFI_UNSUPPORTED The IOMMU does not support the memory range\r
470 specified by Mapping.\r
471 @retval EFI_OUT_OF_RESOURCES There are not enough resources available to\r
472 modify the IOMMU access.\r
473 @retval EFI_DEVICE_ERROR The IOMMU device reported an error while\r
474 attempting the operation.\r
f9d129e6
BS
475\r
476**/\r
477EFI_STATUS\r
478EFIAPI\r
479IoMmuSetAttribute (\r
480 IN EDKII_IOMMU_PROTOCOL *This,\r
481 IN EFI_HANDLE DeviceHandle,\r
482 IN VOID *Mapping,\r
483 IN UINT64 IoMmuAccess\r
484 )\r
485{\r
486 return EFI_UNSUPPORTED;\r
487}\r
488\r
489EDKII_IOMMU_PROTOCOL mAmdSev = {\r
490 EDKII_IOMMU_PROTOCOL_REVISION,\r
491 IoMmuSetAttribute,\r
492 IoMmuMap,\r
493 IoMmuUnmap,\r
494 IoMmuAllocateBuffer,\r
495 IoMmuFreeBuffer,\r
496};\r
497\r
498/**\r
499 Initialize Iommu Protocol.\r
500\r
501**/\r
db125079 502EFI_STATUS\r
f9d129e6
BS
503EFIAPI\r
504AmdSevInstallIoMmuProtocol (\r
505 VOID\r
506 )\r
507{\r
508 EFI_STATUS Status;\r
509 EFI_HANDLE Handle;\r
510\r
511 Handle = NULL;\r
512 Status = gBS->InstallMultipleProtocolInterfaces (\r
513 &Handle,\r
514 &gEdkiiIoMmuProtocolGuid, &mAmdSev,\r
515 NULL\r
516 );\r
db125079 517 return Status;\r
f9d129e6 518}\r