]>
Commit | Line | Data |
---|---|---|
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 | |
23 | typedef 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 | |
58e68140 LE |
31 | #define COMMON_BUFFER_SIG SIGNATURE_64 ('C', 'M', 'N', 'B', 'U', 'F', 'F', 'R')\r |
32 | \r | |
33 | //\r | |
34 | // The following structure enables Map() and Unmap() to perform in-place\r | |
35 | // decryption and encryption, respectively, for BusMasterCommonBuffer[64]\r | |
36 | // operations, without dynamic memory allocation or release.\r | |
37 | //\r | |
38 | // Both COMMON_BUFFER_HEADER and COMMON_BUFFER_HEADER.StashBuffer are allocated\r | |
39 | // by AllocateBuffer() and released by FreeBuffer().\r | |
40 | //\r | |
41 | #pragma pack (1)\r | |
42 | typedef struct {\r | |
43 | UINT64 Signature;\r | |
44 | \r | |
45 | //\r | |
46 | // Always allocated from EfiBootServicesData type memory, and always\r | |
47 | // encrypted.\r | |
48 | //\r | |
49 | VOID *StashBuffer;\r | |
50 | \r | |
51 | //\r | |
52 | // Followed by the actual common buffer, starting at the next page.\r | |
53 | //\r | |
54 | } COMMON_BUFFER_HEADER;\r | |
55 | #pragma pack ()\r | |
f9d129e6 BS |
56 | \r |
57 | /**\r | |
812568fb LE |
58 | Provides the controller-specific addresses required to access system memory\r |
59 | from a DMA bus master. On SEV guest, the DMA operations must be performed on\r | |
60 | shared buffer hence we allocate a bounce buffer to map the HostAddress to a\r | |
61 | DeviceAddress. The Encryption attribute is removed from the DeviceAddress\r | |
62 | buffer.\r | |
f9d129e6 BS |
63 | \r |
64 | @param This The protocol instance pointer.\r | |
65 | @param Operation Indicates if the bus master is going to read or\r | |
66 | write to system memory.\r | |
812568fb LE |
67 | @param HostAddress The system memory address to map to the PCI\r |
68 | controller.\r | |
f9d129e6 | 69 | @param NumberOfBytes On input the number of bytes to map. On output\r |
812568fb LE |
70 | the number of bytes that were mapped.\r |
71 | @param DeviceAddress The resulting map address for the bus master\r | |
72 | PCI controller to use to access the hosts\r | |
73 | HostAddress.\r | |
f9d129e6 BS |
74 | @param Mapping A resulting value to pass to Unmap().\r |
75 | \r | |
812568fb LE |
76 | @retval EFI_SUCCESS The range was mapped for the returned\r |
77 | NumberOfBytes.\r | |
78 | @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common\r | |
79 | buffer.\r | |
f9d129e6 | 80 | @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r |
812568fb LE |
81 | @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a\r |
82 | lack of resources.\r | |
83 | @retval EFI_DEVICE_ERROR The system hardware could not map the requested\r | |
84 | address.\r | |
f9d129e6 BS |
85 | \r |
86 | **/\r | |
87 | EFI_STATUS\r | |
88 | EFIAPI\r | |
89 | IoMmuMap (\r | |
90 | IN EDKII_IOMMU_PROTOCOL *This,\r | |
91 | IN EDKII_IOMMU_OPERATION Operation,\r | |
92 | IN VOID *HostAddress,\r | |
93 | IN OUT UINTN *NumberOfBytes,\r | |
94 | OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r | |
95 | OUT VOID **Mapping\r | |
96 | )\r | |
97 | {\r | |
98 | EFI_STATUS Status;\r | |
f9d129e6 | 99 | MAP_INFO *MapInfo;\r |
f9d129e6 | 100 | EFI_ALLOCATE_TYPE AllocateType;\r |
58e68140 LE |
101 | COMMON_BUFFER_HEADER *CommonBufferHeader;\r |
102 | VOID *DecryptionSource;\r | |
f9d129e6 BS |
103 | \r |
104 | if (HostAddress == NULL || NumberOfBytes == NULL || DeviceAddress == NULL ||\r | |
105 | Mapping == NULL) {\r | |
106 | return EFI_INVALID_PARAMETER;\r | |
107 | }\r | |
108 | \r | |
f9d129e6 BS |
109 | //\r |
110 | // Allocate a MAP_INFO structure to remember the mapping when Unmap() is\r | |
111 | // called later.\r | |
112 | //\r | |
113 | MapInfo = AllocatePool (sizeof (MAP_INFO));\r | |
114 | if (MapInfo == NULL) {\r | |
e130229c LE |
115 | Status = EFI_OUT_OF_RESOURCES;\r |
116 | goto Failed;\r | |
f9d129e6 BS |
117 | }\r |
118 | \r | |
119 | //\r | |
e130229c | 120 | // Initialize the MAP_INFO structure, except the PlainTextAddress field\r |
f9d129e6 BS |
121 | //\r |
122 | MapInfo->Operation = Operation;\r | |
123 | MapInfo->NumberOfBytes = *NumberOfBytes;\r | |
124 | MapInfo->NumberOfPages = EFI_SIZE_TO_PAGES (MapInfo->NumberOfBytes);\r | |
e130229c | 125 | MapInfo->CryptedAddress = (UINTN)HostAddress;\r |
f9d129e6 BS |
126 | \r |
127 | //\r | |
e130229c | 128 | // In the switch statement below, we point "MapInfo->PlainTextAddress" to the\r |
58e68140 | 129 | // plaintext buffer, according to Operation. We also set "DecryptionSource".\r |
e130229c LE |
130 | //\r |
131 | MapInfo->PlainTextAddress = MAX_ADDRESS;\r | |
132 | AllocateType = AllocateAnyPages;\r | |
58e68140 | 133 | DecryptionSource = (VOID *)(UINTN)MapInfo->CryptedAddress;\r |
e130229c LE |
134 | switch (Operation) {\r |
135 | //\r | |
136 | // For BusMasterRead[64] and BusMasterWrite[64] operations, a bounce buffer\r | |
137 | // is necessary regardless of whether the original (crypted) buffer crosses\r | |
138 | // the 4GB limit or not -- we have to allocate a separate plaintext buffer.\r | |
139 | // The only variable is whether the plaintext buffer should be under 4GB.\r | |
f9d129e6 | 140 | //\r |
e130229c LE |
141 | case EdkiiIoMmuOperationBusMasterRead:\r |
142 | case EdkiiIoMmuOperationBusMasterWrite:\r | |
143 | MapInfo->PlainTextAddress = BASE_4GB - 1;\r | |
144 | AllocateType = AllocateMaxAddress;\r | |
145 | //\r | |
146 | // fall through\r | |
147 | //\r | |
148 | case EdkiiIoMmuOperationBusMasterRead64:\r | |
149 | case EdkiiIoMmuOperationBusMasterWrite64:\r | |
150 | //\r | |
151 | // Allocate the implicit plaintext bounce buffer.\r | |
152 | //\r | |
153 | Status = gBS->AllocatePages (\r | |
154 | AllocateType,\r | |
155 | EfiBootServicesData,\r | |
156 | MapInfo->NumberOfPages,\r | |
157 | &MapInfo->PlainTextAddress\r | |
158 | );\r | |
159 | if (EFI_ERROR (Status)) {\r | |
160 | goto FreeMapInfo;\r | |
161 | }\r | |
162 | break;\r | |
163 | \r | |
164 | //\r | |
58e68140 LE |
165 | // For BusMasterCommonBuffer[64] operations, a to-be-plaintext buffer and a\r |
166 | // stash buffer (for in-place decryption) have been allocated already, with\r | |
167 | // AllocateBuffer(). We only check whether the address of the to-be-plaintext\r | |
168 | // buffer is low enough for the requested operation.\r | |
e130229c LE |
169 | //\r |
170 | case EdkiiIoMmuOperationBusMasterCommonBuffer:\r | |
171 | if ((MapInfo->CryptedAddress > BASE_4GB) ||\r | |
172 | (EFI_PAGES_TO_SIZE (MapInfo->NumberOfPages) >\r | |
173 | BASE_4GB - MapInfo->CryptedAddress)) {\r | |
174 | //\r | |
175 | // CommonBuffer operations cannot be remapped. If the common buffer is\r | |
176 | // above 4GB, then it is not possible to generate a mapping, so return an\r | |
177 | // error.\r | |
178 | //\r | |
179 | Status = EFI_UNSUPPORTED;\r | |
180 | goto FreeMapInfo;\r | |
181 | }\r | |
182 | //\r | |
183 | // fall through\r | |
184 | //\r | |
185 | case EdkiiIoMmuOperationBusMasterCommonBuffer64:\r | |
186 | //\r | |
58e68140 | 187 | // The buffer at MapInfo->CryptedAddress comes from AllocateBuffer().\r |
e130229c LE |
188 | //\r |
189 | MapInfo->PlainTextAddress = MapInfo->CryptedAddress;\r | |
e130229c | 190 | //\r |
58e68140 LE |
191 | // Stash the crypted data.\r |
192 | //\r | |
193 | CommonBufferHeader = (COMMON_BUFFER_HEADER *)(\r | |
194 | (UINTN)MapInfo->CryptedAddress - EFI_PAGE_SIZE\r | |
195 | );\r | |
196 | ASSERT (CommonBufferHeader->Signature == COMMON_BUFFER_SIG);\r | |
197 | CopyMem (\r | |
198 | CommonBufferHeader->StashBuffer,\r | |
199 | (VOID *)(UINTN)MapInfo->CryptedAddress,\r | |
200 | MapInfo->NumberOfBytes\r | |
201 | );\r | |
202 | //\r | |
203 | // Point "DecryptionSource" to the stash buffer so that we decrypt\r | |
204 | // it to the original location, after the switch statement.\r | |
e130229c | 205 | //\r |
58e68140 LE |
206 | DecryptionSource = CommonBufferHeader->StashBuffer;\r |
207 | break;\r | |
e130229c LE |
208 | \r |
209 | default:\r | |
210 | //\r | |
211 | // Operation is invalid\r | |
212 | //\r | |
213 | Status = EFI_INVALID_PARAMETER;\r | |
214 | goto FreeMapInfo;\r | |
f9d129e6 BS |
215 | }\r |
216 | \r | |
217 | //\r | |
e130229c | 218 | // Clear the memory encryption mask on the plaintext buffer.\r |
f9d129e6 | 219 | //\r |
812568fb LE |
220 | Status = MemEncryptSevClearPageEncMask (\r |
221 | 0,\r | |
dc194ce3 | 222 | MapInfo->PlainTextAddress,\r |
812568fb LE |
223 | MapInfo->NumberOfPages,\r |
224 | TRUE\r | |
225 | );\r | |
f1658838 LE |
226 | ASSERT_EFI_ERROR (Status);\r |
227 | if (EFI_ERROR (Status)) {\r | |
228 | CpuDeadLoop ();\r | |
229 | }\r | |
f9d129e6 BS |
230 | \r |
231 | //\r | |
232 | // If this is a read operation from the Bus Master's point of view,\r | |
233 | // then copy the contents of the real buffer into the mapped buffer\r | |
234 | // so the Bus Master can read the contents of the real buffer.\r | |
235 | //\r | |
58e68140 LE |
236 | // For BusMasterCommonBuffer[64] operations, the CopyMem() below will decrypt\r |
237 | // the original data (from the stash buffer) back to the original location.\r | |
238 | //\r | |
f9d129e6 | 239 | if (Operation == EdkiiIoMmuOperationBusMasterRead ||\r |
58e68140 LE |
240 | Operation == EdkiiIoMmuOperationBusMasterRead64 ||\r |
241 | Operation == EdkiiIoMmuOperationBusMasterCommonBuffer ||\r | |
242 | Operation == EdkiiIoMmuOperationBusMasterCommonBuffer64) {\r | |
f9d129e6 | 243 | CopyMem (\r |
dc194ce3 | 244 | (VOID *) (UINTN) MapInfo->PlainTextAddress,\r |
58e68140 | 245 | DecryptionSource,\r |
f9d129e6 BS |
246 | MapInfo->NumberOfBytes\r |
247 | );\r | |
248 | }\r | |
249 | \r | |
250 | //\r | |
e130229c | 251 | // Populate output parameters.\r |
f9d129e6 | 252 | //\r |
dc194ce3 | 253 | *DeviceAddress = MapInfo->PlainTextAddress;\r |
f9d129e6 BS |
254 | *Mapping = MapInfo;\r |
255 | \r | |
812568fb LE |
256 | DEBUG ((\r |
257 | DEBUG_VERBOSE,\r | |
c7ef2ed2 | 258 | "%a PlainText 0x%Lx Crypted 0x%Lx Pages 0x%Lx Bytes 0x%Lx\n",\r |
812568fb | 259 | __FUNCTION__,\r |
dc194ce3 | 260 | MapInfo->PlainTextAddress,\r |
c7ef2ed2 | 261 | MapInfo->CryptedAddress,\r |
60aa3a0e LE |
262 | (UINT64)MapInfo->NumberOfPages,\r |
263 | (UINT64)MapInfo->NumberOfBytes\r | |
812568fb | 264 | ));\r |
f9d129e6 BS |
265 | \r |
266 | return EFI_SUCCESS;\r | |
e130229c LE |
267 | \r |
268 | FreeMapInfo:\r | |
269 | FreePool (MapInfo);\r | |
270 | \r | |
271 | Failed:\r | |
272 | *NumberOfBytes = 0;\r | |
273 | return Status;\r | |
f9d129e6 BS |
274 | }\r |
275 | \r | |
276 | /**\r | |
277 | Completes the Map() operation and releases any corresponding resources.\r | |
278 | \r | |
279 | @param This The protocol instance pointer.\r | |
280 | @param Mapping The mapping value returned from Map().\r | |
281 | \r | |
282 | @retval EFI_SUCCESS The range was unmapped.\r | |
812568fb LE |
283 | @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by\r |
284 | Map().\r | |
285 | @retval EFI_DEVICE_ERROR The data was not committed to the target system\r | |
286 | memory.\r | |
f9d129e6 BS |
287 | **/\r |
288 | EFI_STATUS\r | |
289 | EFIAPI\r | |
290 | IoMmuUnmap (\r | |
291 | IN EDKII_IOMMU_PROTOCOL *This,\r | |
292 | IN VOID *Mapping\r | |
293 | )\r | |
294 | {\r | |
295 | MAP_INFO *MapInfo;\r | |
296 | EFI_STATUS Status;\r | |
58e68140 LE |
297 | COMMON_BUFFER_HEADER *CommonBufferHeader;\r |
298 | VOID *EncryptionTarget;\r | |
f9d129e6 BS |
299 | \r |
300 | if (Mapping == NULL) {\r | |
301 | return EFI_INVALID_PARAMETER;\r | |
302 | }\r | |
303 | \r | |
58e68140 LE |
304 | MapInfo = (MAP_INFO *)Mapping;\r |
305 | \r | |
f9d129e6 | 306 | //\r |
58e68140 | 307 | // set CommonBufferHeader to suppress incorrect compiler/analyzer warnings\r |
f9d129e6 | 308 | //\r |
58e68140 | 309 | CommonBufferHeader = NULL;\r |
f9d129e6 BS |
310 | \r |
311 | //\r | |
58e68140 LE |
312 | // For BusMasterWrite[64] operations and BusMasterCommonBuffer[64] operations\r |
313 | // we have to encrypt the results, ultimately to the original place (i.e.,\r | |
314 | // "MapInfo->CryptedAddress").\r | |
f9d129e6 | 315 | //\r |
58e68140 LE |
316 | // For BusMasterCommonBuffer[64] operations however, this encryption has to\r |
317 | // land in-place, so divert the encryption to the stash buffer first.\r | |
318 | //\r | |
319 | EncryptionTarget = (VOID *)(UINTN)MapInfo->CryptedAddress;\r | |
320 | \r | |
321 | switch (MapInfo->Operation) {\r | |
322 | case EdkiiIoMmuOperationBusMasterCommonBuffer:\r | |
323 | case EdkiiIoMmuOperationBusMasterCommonBuffer64:\r | |
324 | ASSERT (MapInfo->PlainTextAddress == MapInfo->CryptedAddress);\r | |
325 | \r | |
326 | CommonBufferHeader = (COMMON_BUFFER_HEADER *)(\r | |
327 | (UINTN)MapInfo->PlainTextAddress - EFI_PAGE_SIZE\r | |
328 | );\r | |
329 | ASSERT (CommonBufferHeader->Signature == COMMON_BUFFER_SIG);\r | |
330 | EncryptionTarget = CommonBufferHeader->StashBuffer;\r | |
331 | //\r | |
332 | // fall through\r | |
333 | //\r | |
334 | \r | |
335 | case EdkiiIoMmuOperationBusMasterWrite:\r | |
336 | case EdkiiIoMmuOperationBusMasterWrite64:\r | |
f9d129e6 | 337 | CopyMem (\r |
58e68140 | 338 | EncryptionTarget,\r |
dc194ce3 | 339 | (VOID *) (UINTN) MapInfo->PlainTextAddress,\r |
f9d129e6 BS |
340 | MapInfo->NumberOfBytes\r |
341 | );\r | |
58e68140 LE |
342 | break;\r |
343 | \r | |
344 | default:\r | |
345 | //\r | |
346 | // nothing to encrypt after BusMasterRead[64] operations\r | |
347 | //\r | |
348 | break;\r | |
f9d129e6 BS |
349 | }\r |
350 | \r | |
812568fb LE |
351 | DEBUG ((\r |
352 | DEBUG_VERBOSE,\r | |
c7ef2ed2 | 353 | "%a PlainText 0x%Lx Crypted 0x%Lx Pages 0x%Lx Bytes 0x%Lx\n",\r |
812568fb | 354 | __FUNCTION__,\r |
dc194ce3 | 355 | MapInfo->PlainTextAddress,\r |
c7ef2ed2 | 356 | MapInfo->CryptedAddress,\r |
60aa3a0e LE |
357 | (UINT64)MapInfo->NumberOfPages,\r |
358 | (UINT64)MapInfo->NumberOfBytes\r | |
812568fb | 359 | ));\r |
58e68140 | 360 | \r |
f9d129e6 | 361 | //\r |
58e68140 LE |
362 | // Restore the memory encryption mask on the area we used to hold the\r |
363 | // plaintext.\r | |
f9d129e6 | 364 | //\r |
812568fb LE |
365 | Status = MemEncryptSevSetPageEncMask (\r |
366 | 0,\r | |
dc194ce3 | 367 | MapInfo->PlainTextAddress,\r |
812568fb LE |
368 | MapInfo->NumberOfPages,\r |
369 | TRUE\r | |
370 | );\r | |
f1658838 LE |
371 | ASSERT_EFI_ERROR (Status);\r |
372 | if (EFI_ERROR (Status)) {\r | |
373 | CpuDeadLoop ();\r | |
374 | }\r | |
f9d129e6 BS |
375 | \r |
376 | //\r | |
58e68140 LE |
377 | // For BusMasterCommonBuffer[64] operations, copy the stashed data to the\r |
378 | // original (now encrypted) location.\r | |
379 | //\r | |
380 | // For all other operations, fill the late bounce buffer (which existed as\r | |
381 | // plaintext at some point) with zeros, and then release it.\r | |
382 | //\r | |
383 | if (MapInfo->Operation == EdkiiIoMmuOperationBusMasterCommonBuffer ||\r | |
384 | MapInfo->Operation == EdkiiIoMmuOperationBusMasterCommonBuffer64) {\r | |
385 | CopyMem (\r | |
386 | (VOID *)(UINTN)MapInfo->CryptedAddress,\r | |
387 | CommonBufferHeader->StashBuffer,\r | |
388 | MapInfo->NumberOfBytes\r | |
389 | );\r | |
390 | } else {\r | |
391 | ZeroMem (\r | |
392 | (VOID *)(UINTN)MapInfo->PlainTextAddress,\r | |
393 | EFI_PAGES_TO_SIZE (MapInfo->NumberOfPages)\r | |
394 | );\r | |
395 | gBS->FreePages (MapInfo->PlainTextAddress, MapInfo->NumberOfPages);\r | |
396 | }\r | |
397 | \r | |
398 | //\r | |
399 | // Free the MAP_INFO structure.\r | |
f9d129e6 | 400 | //\r |
f9d129e6 BS |
401 | FreePool (Mapping);\r |
402 | return EFI_SUCCESS;\r | |
403 | }\r | |
404 | \r | |
405 | /**\r | |
406 | Allocates pages that are suitable for an OperationBusMasterCommonBuffer or\r | |
407 | OperationBusMasterCommonBuffer64 mapping.\r | |
408 | \r | |
409 | @param This The protocol instance pointer.\r | |
410 | @param Type This parameter is not used and must be ignored.\r | |
812568fb LE |
411 | @param MemoryType The type of memory to allocate,\r |
412 | EfiBootServicesData or EfiRuntimeServicesData.\r | |
f9d129e6 | 413 | @param Pages The number of pages to allocate.\r |
812568fb LE |
414 | @param HostAddress A pointer to store the base system memory\r |
415 | address of the allocated range.\r | |
416 | @param Attributes The requested bit mask of attributes for the\r | |
417 | allocated range.\r | |
f9d129e6 BS |
418 | \r |
419 | @retval EFI_SUCCESS The requested memory pages were allocated.\r | |
812568fb LE |
420 | @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal\r |
421 | attribute bits are MEMORY_WRITE_COMBINE and\r | |
422 | MEMORY_CACHED.\r | |
f9d129e6 BS |
423 | @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r |
424 | @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.\r | |
425 | \r | |
426 | **/\r | |
427 | EFI_STATUS\r | |
428 | EFIAPI\r | |
429 | IoMmuAllocateBuffer (\r | |
430 | IN EDKII_IOMMU_PROTOCOL *This,\r | |
431 | IN EFI_ALLOCATE_TYPE Type,\r | |
432 | IN EFI_MEMORY_TYPE MemoryType,\r | |
433 | IN UINTN Pages,\r | |
434 | IN OUT VOID **HostAddress,\r | |
435 | IN UINT64 Attributes\r | |
436 | )\r | |
437 | {\r | |
438 | EFI_STATUS Status;\r | |
439 | EFI_PHYSICAL_ADDRESS PhysicalAddress;\r | |
58e68140 LE |
440 | VOID *StashBuffer;\r |
441 | UINTN CommonBufferPages;\r | |
442 | COMMON_BUFFER_HEADER *CommonBufferHeader;\r | |
f9d129e6 BS |
443 | \r |
444 | //\r | |
445 | // Validate Attributes\r | |
446 | //\r | |
447 | if ((Attributes & EDKII_IOMMU_ATTRIBUTE_INVALID_FOR_ALLOCATE_BUFFER) != 0) {\r | |
448 | return EFI_UNSUPPORTED;\r | |
449 | }\r | |
450 | \r | |
451 | //\r | |
452 | // Check for invalid inputs\r | |
453 | //\r | |
454 | if (HostAddress == NULL) {\r | |
455 | return EFI_INVALID_PARAMETER;\r | |
456 | }\r | |
457 | \r | |
458 | //\r | |
459 | // The only valid memory types are EfiBootServicesData and\r | |
460 | // EfiRuntimeServicesData\r | |
461 | //\r | |
462 | if (MemoryType != EfiBootServicesData &&\r | |
463 | MemoryType != EfiRuntimeServicesData) {\r | |
464 | return EFI_INVALID_PARAMETER;\r | |
465 | }\r | |
466 | \r | |
58e68140 LE |
467 | //\r |
468 | // We'll need a header page for the COMMON_BUFFER_HEADER structure.\r | |
469 | //\r | |
470 | if (Pages > MAX_UINTN - 1) {\r | |
471 | return EFI_OUT_OF_RESOURCES;\r | |
472 | }\r | |
473 | CommonBufferPages = Pages + 1;\r | |
474 | \r | |
475 | //\r | |
476 | // Allocate the stash in EfiBootServicesData type memory.\r | |
477 | //\r | |
478 | // Map() will temporarily save encrypted data in the stash for\r | |
479 | // BusMasterCommonBuffer[64] operations, so the data can be decrypted to the\r | |
480 | // original location.\r | |
481 | //\r | |
482 | // Unmap() will temporarily save plaintext data in the stash for\r | |
483 | // BusMasterCommonBuffer[64] operations, so the data can be encrypted to the\r | |
484 | // original location.\r | |
485 | //\r | |
486 | // StashBuffer always resides in encrypted memory.\r | |
487 | //\r | |
488 | StashBuffer = AllocatePages (Pages);\r | |
489 | if (StashBuffer == NULL) {\r | |
490 | return EFI_OUT_OF_RESOURCES;\r | |
491 | }\r | |
492 | \r | |
f9d129e6 BS |
493 | PhysicalAddress = (UINTN)-1;\r |
494 | if ((Attributes & EDKII_IOMMU_ATTRIBUTE_DUAL_ADDRESS_CYCLE) == 0) {\r | |
495 | //\r | |
496 | // Limit allocations to memory below 4GB\r | |
497 | //\r | |
498 | PhysicalAddress = SIZE_4GB - 1;\r | |
499 | }\r | |
500 | Status = gBS->AllocatePages (\r | |
501 | AllocateMaxAddress,\r | |
502 | MemoryType,\r | |
58e68140 | 503 | CommonBufferPages,\r |
f9d129e6 BS |
504 | &PhysicalAddress\r |
505 | );\r | |
58e68140 LE |
506 | if (EFI_ERROR (Status)) {\r |
507 | goto FreeStashBuffer;\r | |
f9d129e6 BS |
508 | }\r |
509 | \r | |
58e68140 LE |
510 | CommonBufferHeader = (VOID *)(UINTN)PhysicalAddress;\r |
511 | PhysicalAddress += EFI_PAGE_SIZE;\r | |
512 | \r | |
513 | CommonBufferHeader->Signature = COMMON_BUFFER_SIG;\r | |
514 | CommonBufferHeader->StashBuffer = StashBuffer;\r | |
515 | \r | |
516 | *HostAddress = (VOID *)(UINTN)PhysicalAddress;\r | |
517 | \r | |
812568fb LE |
518 | DEBUG ((\r |
519 | DEBUG_VERBOSE,\r | |
520 | "%a Address 0x%Lx Pages 0x%Lx\n",\r | |
521 | __FUNCTION__,\r | |
522 | PhysicalAddress,\r | |
60aa3a0e | 523 | (UINT64)Pages\r |
812568fb | 524 | ));\r |
58e68140 LE |
525 | return EFI_SUCCESS;\r |
526 | \r | |
527 | FreeStashBuffer:\r | |
528 | FreePages (StashBuffer, Pages);\r | |
f9d129e6 BS |
529 | return Status;\r |
530 | }\r | |
531 | \r | |
532 | /**\r | |
533 | Frees memory that was allocated with AllocateBuffer().\r | |
534 | \r | |
535 | @param This The protocol instance pointer.\r | |
536 | @param Pages The number of pages to free.\r | |
812568fb LE |
537 | @param HostAddress The base system memory address of the allocated\r |
538 | range.\r | |
f9d129e6 BS |
539 | \r |
540 | @retval EFI_SUCCESS The requested memory pages were freed.\r | |
812568fb LE |
541 | @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and\r |
542 | Pages was not allocated with AllocateBuffer().\r | |
f9d129e6 BS |
543 | \r |
544 | **/\r | |
545 | EFI_STATUS\r | |
546 | EFIAPI\r | |
547 | IoMmuFreeBuffer (\r | |
548 | IN EDKII_IOMMU_PROTOCOL *This,\r | |
549 | IN UINTN Pages,\r | |
550 | IN VOID *HostAddress\r | |
551 | )\r | |
552 | {\r | |
58e68140 LE |
553 | UINTN CommonBufferPages;\r |
554 | COMMON_BUFFER_HEADER *CommonBufferHeader;\r | |
555 | \r | |
556 | CommonBufferPages = Pages + 1;\r | |
557 | CommonBufferHeader = (COMMON_BUFFER_HEADER *)(\r | |
558 | (UINTN)HostAddress - EFI_PAGE_SIZE\r | |
559 | );\r | |
f9d129e6 BS |
560 | \r |
561 | //\r | |
58e68140 | 562 | // Check the signature.\r |
f9d129e6 | 563 | //\r |
58e68140 LE |
564 | ASSERT (CommonBufferHeader->Signature == COMMON_BUFFER_SIG);\r |
565 | if (CommonBufferHeader->Signature != COMMON_BUFFER_SIG) {\r | |
566 | return EFI_INVALID_PARAMETER;\r | |
567 | }\r | |
568 | \r | |
569 | //\r | |
570 | // Free the stash buffer. This buffer was always encrypted, so no need to\r | |
571 | // zero it.\r | |
572 | //\r | |
573 | FreePages (CommonBufferHeader->StashBuffer, Pages);\r | |
f9d129e6 | 574 | \r |
812568fb LE |
575 | DEBUG ((\r |
576 | DEBUG_VERBOSE,\r | |
577 | "%a Address 0x%Lx Pages 0x%Lx\n",\r | |
578 | __FUNCTION__,\r | |
60aa3a0e LE |
579 | (UINT64)(UINTN)HostAddress,\r |
580 | (UINT64)Pages\r | |
812568fb | 581 | ));\r |
58e68140 LE |
582 | \r |
583 | //\r | |
584 | // Release the common buffer itself. Unmap() has re-encrypted it in-place, so\r | |
585 | // no need to zero it.\r | |
586 | //\r | |
587 | return gBS->FreePages ((UINTN)CommonBufferHeader, CommonBufferPages);\r | |
f9d129e6 BS |
588 | }\r |
589 | \r | |
590 | \r | |
591 | /**\r | |
592 | Set IOMMU attribute for a system memory.\r | |
593 | \r | |
594 | If the IOMMU protocol exists, the system memory cannot be used\r | |
595 | for DMA by default.\r | |
596 | \r | |
597 | When a device requests a DMA access for a system memory,\r | |
598 | the device driver need use SetAttribute() to update the IOMMU\r | |
599 | attribute to request DMA access (read and/or write).\r | |
600 | \r | |
601 | The DeviceHandle is used to identify which device submits the request.\r | |
812568fb LE |
602 | The IOMMU implementation need translate the device path to an IOMMU device\r |
603 | ID, and set IOMMU hardware register accordingly.\r | |
f9d129e6 BS |
604 | 1) DeviceHandle can be a standard PCI device.\r |
605 | The memory for BusMasterRead need set EDKII_IOMMU_ACCESS_READ.\r | |
606 | The memory for BusMasterWrite need set EDKII_IOMMU_ACCESS_WRITE.\r | |
812568fb LE |
607 | The memory for BusMasterCommonBuffer need set\r |
608 | EDKII_IOMMU_ACCESS_READ|EDKII_IOMMU_ACCESS_WRITE.\r | |
609 | After the memory is used, the memory need set 0 to keep it being\r | |
610 | protected.\r | |
f9d129e6 | 611 | 2) DeviceHandle can be an ACPI device (ISA, I2C, SPI, etc).\r |
812568fb LE |
612 | The memory for DMA access need set EDKII_IOMMU_ACCESS_READ and/or\r |
613 | EDKII_IOMMU_ACCESS_WRITE.\r | |
f9d129e6 BS |
614 | \r |
615 | @param[in] This The protocol instance pointer.\r | |
812568fb LE |
616 | @param[in] DeviceHandle The device who initiates the DMA access\r |
617 | request.\r | |
f9d129e6 BS |
618 | @param[in] Mapping The mapping value returned from Map().\r |
619 | @param[in] IoMmuAccess The IOMMU access.\r | |
620 | \r | |
812568fb LE |
621 | @retval EFI_SUCCESS The IoMmuAccess is set for the memory range\r |
622 | specified by DeviceAddress and Length.\r | |
f9d129e6 | 623 | @retval EFI_INVALID_PARAMETER DeviceHandle is an invalid handle.\r |
812568fb LE |
624 | @retval EFI_INVALID_PARAMETER Mapping is not a value that was returned by\r |
625 | Map().\r | |
626 | @retval EFI_INVALID_PARAMETER IoMmuAccess specified an illegal combination\r | |
627 | of access.\r | |
f9d129e6 | 628 | @retval EFI_UNSUPPORTED DeviceHandle is unknown by the IOMMU.\r |
812568fb LE |
629 | @retval EFI_UNSUPPORTED The bit mask of IoMmuAccess is not supported\r |
630 | by the IOMMU.\r | |
631 | @retval EFI_UNSUPPORTED The IOMMU does not support the memory range\r | |
632 | specified by Mapping.\r | |
633 | @retval EFI_OUT_OF_RESOURCES There are not enough resources available to\r | |
634 | modify the IOMMU access.\r | |
635 | @retval EFI_DEVICE_ERROR The IOMMU device reported an error while\r | |
636 | attempting the operation.\r | |
f9d129e6 BS |
637 | \r |
638 | **/\r | |
639 | EFI_STATUS\r | |
640 | EFIAPI\r | |
641 | IoMmuSetAttribute (\r | |
642 | IN EDKII_IOMMU_PROTOCOL *This,\r | |
643 | IN EFI_HANDLE DeviceHandle,\r | |
644 | IN VOID *Mapping,\r | |
645 | IN UINT64 IoMmuAccess\r | |
646 | )\r | |
647 | {\r | |
648 | return EFI_UNSUPPORTED;\r | |
649 | }\r | |
650 | \r | |
651 | EDKII_IOMMU_PROTOCOL mAmdSev = {\r | |
652 | EDKII_IOMMU_PROTOCOL_REVISION,\r | |
653 | IoMmuSetAttribute,\r | |
654 | IoMmuMap,\r | |
655 | IoMmuUnmap,\r | |
656 | IoMmuAllocateBuffer,\r | |
657 | IoMmuFreeBuffer,\r | |
658 | };\r | |
659 | \r | |
660 | /**\r | |
661 | Initialize Iommu Protocol.\r | |
662 | \r | |
663 | **/\r | |
db125079 | 664 | EFI_STATUS\r |
f9d129e6 BS |
665 | EFIAPI\r |
666 | AmdSevInstallIoMmuProtocol (\r | |
667 | VOID\r | |
668 | )\r | |
669 | {\r | |
670 | EFI_STATUS Status;\r | |
671 | EFI_HANDLE Handle;\r | |
672 | \r | |
673 | Handle = NULL;\r | |
674 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
675 | &Handle,\r | |
676 | &gEdkiiIoMmuProtocolGuid, &mAmdSev,\r | |
677 | NULL\r | |
678 | );\r | |
db125079 | 679 | return Status;\r |
f9d129e6 | 680 | }\r |