]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/VirtioGpuDxe/Commands.c
OvmfPkg: Apply uncrustify changes
[mirror_edk2.git] / OvmfPkg / VirtioGpuDxe / Commands.c
CommitLineData
c5f235bb
LE
1/** @file\r
2\r
3 VirtIo GPU initialization, and commands (primitives) for the GPU device.\r
4\r
5 Copyright (C) 2016, Red Hat, Inc.\r
fc2c1543 6 Copyright (c) 2017, AMD Inc, All rights reserved.<BR>\r
c5f235bb 7\r
b26f0cf9 8 SPDX-License-Identifier: BSD-2-Clause-Patent\r
c5f235bb
LE
9\r
10**/\r
11\r
c5f235bb
LE
12#include <Library/VirtioLib.h>\r
13\r
14#include "VirtioGpu.h"\r
15\r
16/**\r
17 Configure the VirtIo GPU device that underlies VgpuDev.\r
18\r
19 @param[in,out] VgpuDev The VGPU_DEV object to set up VirtIo messaging for.\r
20 On input, the caller is responsible for having\r
21 initialized VgpuDev->VirtIo. On output, VgpuDev->Ring\r
22 has been initialized, and synchronous VirtIo GPU\r
23 commands (primitives) can be submitted to the device.\r
24\r
25 @retval EFI_SUCCESS VirtIo GPU configuration successful.\r
26\r
27 @retval EFI_UNSUPPORTED The host-side configuration of the VirtIo GPU is not\r
28 supported by this driver.\r
29\r
30 @retval Error codes from underlying functions.\r
31**/\r
32EFI_STATUS\r
33VirtioGpuInit (\r
ac0a286f 34 IN OUT VGPU_DEV *VgpuDev\r
c5f235bb
LE
35 )\r
36{\r
ac0a286f
MK
37 UINT8 NextDevStat;\r
38 EFI_STATUS Status;\r
39 UINT64 Features;\r
40 UINT16 QueueSize;\r
41 UINT64 RingBaseShift;\r
c5f235bb
LE
42\r
43 //\r
44 // Execute virtio-v1.0-cs04, 3.1.1 Driver Requirements: Device\r
45 // Initialization.\r
46 //\r
47 // 1. Reset the device.\r
48 //\r
49 NextDevStat = 0;\r
ac0a286f 50 Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);\r
c5f235bb
LE
51 if (EFI_ERROR (Status)) {\r
52 goto Failed;\r
53 }\r
54\r
55 //\r
56 // 2. Set the ACKNOWLEDGE status bit [...]\r
57 //\r
58 NextDevStat |= VSTAT_ACK;\r
ac0a286f 59 Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);\r
c5f235bb
LE
60 if (EFI_ERROR (Status)) {\r
61 goto Failed;\r
62 }\r
63\r
64 //\r
65 // 3. Set the DRIVER status bit [...]\r
66 //\r
67 NextDevStat |= VSTAT_DRIVER;\r
ac0a286f 68 Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);\r
c5f235bb
LE
69 if (EFI_ERROR (Status)) {\r
70 goto Failed;\r
71 }\r
72\r
73 //\r
74 // 4. Read device feature bits...\r
75 //\r
76 Status = VgpuDev->VirtIo->GetDeviceFeatures (VgpuDev->VirtIo, &Features);\r
77 if (EFI_ERROR (Status)) {\r
78 goto Failed;\r
79 }\r
ac0a286f 80\r
c5f235bb
LE
81 if ((Features & VIRTIO_F_VERSION_1) == 0) {\r
82 Status = EFI_UNSUPPORTED;\r
83 goto Failed;\r
84 }\r
ac0a286f 85\r
c5f235bb
LE
86 //\r
87 // We only want the most basic 2D features.\r
88 //\r
db528909 89 Features &= VIRTIO_F_VERSION_1 | VIRTIO_F_IOMMU_PLATFORM;\r
c5f235bb
LE
90\r
91 //\r
92 // ... and write the subset of feature bits understood by the [...] driver to\r
93 // the device. [...]\r
94 // 5. Set the FEATURES_OK status bit.\r
95 // 6. Re-read device status to ensure the FEATURES_OK bit is still set [...]\r
96 //\r
97 Status = Virtio10WriteFeatures (VgpuDev->VirtIo, Features, &NextDevStat);\r
98 if (EFI_ERROR (Status)) {\r
99 goto Failed;\r
100 }\r
101\r
102 //\r
103 // 7. Perform device-specific setup, including discovery of virtqueues for\r
104 // the device [...]\r
105 //\r
ac0a286f
MK
106 Status = VgpuDev->VirtIo->SetQueueSel (\r
107 VgpuDev->VirtIo,\r
108 VIRTIO_GPU_CONTROL_QUEUE\r
109 );\r
c5f235bb
LE
110 if (EFI_ERROR (Status)) {\r
111 goto Failed;\r
112 }\r
ac0a286f 113\r
c5f235bb
LE
114 Status = VgpuDev->VirtIo->GetQueueNumMax (VgpuDev->VirtIo, &QueueSize);\r
115 if (EFI_ERROR (Status)) {\r
116 goto Failed;\r
117 }\r
118\r
119 //\r
120 // We implement each VirtIo GPU command that we use with two descriptors:\r
121 // request, response.\r
122 //\r
123 if (QueueSize < 2) {\r
124 Status = EFI_UNSUPPORTED;\r
125 goto Failed;\r
126 }\r
127\r
128 //\r
129 // [...] population of virtqueues [...]\r
130 //\r
fc2c1543 131 Status = VirtioRingInit (VgpuDev->VirtIo, QueueSize, &VgpuDev->Ring);\r
c5f235bb
LE
132 if (EFI_ERROR (Status)) {\r
133 goto Failed;\r
134 }\r
ac0a286f 135\r
9bc5026c
LE
136 //\r
137 // If anything fails from here on, we have to release the ring.\r
138 //\r
139 Status = VirtioRingMap (\r
140 VgpuDev->VirtIo,\r
141 &VgpuDev->Ring,\r
142 &RingBaseShift,\r
143 &VgpuDev->RingMap\r
144 );\r
145 if (EFI_ERROR (Status)) {\r
146 goto ReleaseQueue;\r
147 }\r
ac0a286f 148\r
9bc5026c
LE
149 //\r
150 // If anything fails from here on, we have to unmap the ring.\r
151 //\r
53a4c604
BS
152 Status = VgpuDev->VirtIo->SetQueueAddress (\r
153 VgpuDev->VirtIo,\r
154 &VgpuDev->Ring,\r
9bc5026c 155 RingBaseShift\r
53a4c604 156 );\r
c5f235bb 157 if (EFI_ERROR (Status)) {\r
9bc5026c 158 goto UnmapQueue;\r
c5f235bb
LE
159 }\r
160\r
161 //\r
162 // 8. Set the DRIVER_OK status bit.\r
163 //\r
164 NextDevStat |= VSTAT_DRIVER_OK;\r
ac0a286f 165 Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);\r
c5f235bb 166 if (EFI_ERROR (Status)) {\r
9bc5026c 167 goto UnmapQueue;\r
c5f235bb
LE
168 }\r
169\r
170 return EFI_SUCCESS;\r
171\r
9bc5026c
LE
172UnmapQueue:\r
173 VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, VgpuDev->RingMap);\r
174\r
c5f235bb 175ReleaseQueue:\r
fc2c1543 176 VirtioRingUninit (VgpuDev->VirtIo, &VgpuDev->Ring);\r
c5f235bb
LE
177\r
178Failed:\r
179 //\r
180 // If any of these steps go irrecoverably wrong, the driver SHOULD set the\r
181 // FAILED status bit to indicate that it has given up on the device (it can\r
182 // reset the device later to restart if desired). [...]\r
183 //\r
184 // VirtIo access failure here should not mask the original error.\r
185 //\r
186 NextDevStat |= VSTAT_FAILED;\r
187 VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);\r
188\r
189 return Status;\r
190}\r
191\r
192/**\r
193 De-configure the VirtIo GPU device that underlies VgpuDev.\r
194\r
195 @param[in,out] VgpuDev The VGPU_DEV object to tear down VirtIo messaging\r
196 for. On input, the caller is responsible for having\r
197 called VirtioGpuInit(). On output, VgpuDev->Ring has\r
198 been uninitialized; VirtIo GPU commands (primitives)\r
199 can no longer be submitted to the device.\r
200**/\r
201VOID\r
202VirtioGpuUninit (\r
ac0a286f 203 IN OUT VGPU_DEV *VgpuDev\r
c5f235bb
LE
204 )\r
205{\r
206 //\r
207 // Resetting the VirtIo device makes it release its resources and forget its\r
208 // configuration.\r
209 //\r
210 VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, 0);\r
9bc5026c 211 VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, VgpuDev->RingMap);\r
fc2c1543 212 VirtioRingUninit (VgpuDev->VirtIo, &VgpuDev->Ring);\r
c5f235bb
LE
213}\r
214\r
5c8f091b
LE
215/**\r
216 Allocate, zero and map memory, for bus master common buffer operation, to be\r
217 attached as backing store to a host-side VirtIo GPU resource.\r
218\r
219 @param[in] VgpuDev The VGPU_DEV object that represents the VirtIo GPU\r
220 device.\r
221\r
222 @param[in] NumberOfPages The number of whole pages to allocate and map.\r
223\r
224 @param[out] HostAddress The system memory address of the allocated area.\r
225\r
226 @param[out] DeviceAddress The bus master device address of the allocated\r
227 area. The VirtIo GPU device may be programmed to\r
228 access the allocated area through DeviceAddress;\r
229 DeviceAddress is to be passed to the\r
230 VirtioGpuResourceAttachBacking() function, as the\r
231 BackingStoreDeviceAddress parameter.\r
232\r
233 @param[out] Mapping A resulting token to pass to\r
234 VirtioGpuUnmapAndFreeBackingStore().\r
235\r
236 @retval EFI_SUCCESS The requested number of pages has been allocated, zeroed\r
237 and mapped.\r
238\r
239 @return Status codes propagated from\r
240 VgpuDev->VirtIo->AllocateSharedPages() and\r
241 VirtioMapAllBytesInSharedBuffer().\r
242**/\r
243EFI_STATUS\r
244VirtioGpuAllocateZeroAndMapBackingStore (\r
ac0a286f
MK
245 IN VGPU_DEV *VgpuDev,\r
246 IN UINTN NumberOfPages,\r
247 OUT VOID **HostAddress,\r
248 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r
249 OUT VOID **Mapping\r
5c8f091b
LE
250 )\r
251{\r
ac0a286f
MK
252 EFI_STATUS Status;\r
253 VOID *NewHostAddress;\r
5c8f091b
LE
254\r
255 Status = VgpuDev->VirtIo->AllocateSharedPages (\r
256 VgpuDev->VirtIo,\r
257 NumberOfPages,\r
258 &NewHostAddress\r
259 );\r
260 if (EFI_ERROR (Status)) {\r
261 return Status;\r
262 }\r
263\r
264 //\r
265 // Avoid exposing stale data to the device even temporarily: zero the area\r
266 // before mapping it.\r
267 //\r
268 ZeroMem (NewHostAddress, EFI_PAGES_TO_SIZE (NumberOfPages));\r
269\r
270 Status = VirtioMapAllBytesInSharedBuffer (\r
271 VgpuDev->VirtIo, // VirtIo\r
272 VirtioOperationBusMasterCommonBuffer, // Operation\r
273 NewHostAddress, // HostAddress\r
274 EFI_PAGES_TO_SIZE (NumberOfPages), // NumberOfBytes\r
275 DeviceAddress, // DeviceAddress\r
276 Mapping // Mapping\r
277 );\r
278 if (EFI_ERROR (Status)) {\r
279 goto FreeSharedPages;\r
280 }\r
281\r
282 *HostAddress = NewHostAddress;\r
283 return EFI_SUCCESS;\r
284\r
285FreeSharedPages:\r
286 VgpuDev->VirtIo->FreeSharedPages (\r
287 VgpuDev->VirtIo,\r
288 NumberOfPages,\r
289 NewHostAddress\r
290 );\r
291 return Status;\r
292}\r
293\r
294/**\r
295 Unmap and free memory originally allocated and mapped with\r
296 VirtioGpuAllocateZeroAndMapBackingStore().\r
297\r
298 If the memory allocated and mapped with\r
299 VirtioGpuAllocateZeroAndMapBackingStore() was attached to a host-side VirtIo\r
300 GPU resource with VirtioGpuResourceAttachBacking(), then the caller is\r
301 responsible for detaching the backing store from the same resource, with\r
302 VirtioGpuResourceDetachBacking(), before calling this function.\r
303\r
304 @param[in] VgpuDev The VGPU_DEV object that represents the VirtIo GPU\r
305 device.\r
306\r
307 @param[in] NumberOfPages The NumberOfPages parameter originally passed to\r
308 VirtioGpuAllocateZeroAndMapBackingStore().\r
309\r
310 @param[in] HostAddress The HostAddress value originally output by\r
311 VirtioGpuAllocateZeroAndMapBackingStore().\r
312\r
313 @param[in] Mapping The token that was originally output by\r
314 VirtioGpuAllocateZeroAndMapBackingStore().\r
315**/\r
316VOID\r
317VirtioGpuUnmapAndFreeBackingStore (\r
ac0a286f
MK
318 IN VGPU_DEV *VgpuDev,\r
319 IN UINTN NumberOfPages,\r
320 IN VOID *HostAddress,\r
321 IN VOID *Mapping\r
5c8f091b
LE
322 )\r
323{\r
324 VgpuDev->VirtIo->UnmapSharedBuffer (\r
325 VgpuDev->VirtIo,\r
326 Mapping\r
327 );\r
328 VgpuDev->VirtIo->FreeSharedPages (\r
329 VgpuDev->VirtIo,\r
330 NumberOfPages,\r
331 HostAddress\r
332 );\r
333}\r
334\r
c5f235bb
LE
335/**\r
336 EFI_EVENT_NOTIFY function for the VGPU_DEV.ExitBoot event. It resets the\r
337 VirtIo device, causing it to release its resources and to forget its\r
338 configuration.\r
339\r
340 This function may only be called (that is, VGPU_DEV.ExitBoot may only be\r
341 signaled) after VirtioGpuInit() returns and before VirtioGpuUninit() is\r
342 called.\r
343\r
344 @param[in] Event Event whose notification function is being invoked.\r
345\r
346 @param[in] Context Pointer to the associated VGPU_DEV object.\r
347**/\r
348VOID\r
349EFIAPI\r
350VirtioGpuExitBoot (\r
ac0a286f
MK
351 IN EFI_EVENT Event,\r
352 IN VOID *Context\r
c5f235bb
LE
353 )\r
354{\r
ac0a286f 355 VGPU_DEV *VgpuDev;\r
c5f235bb 356\r
8ddd12e5 357 DEBUG ((DEBUG_VERBOSE, "%a: Context=0x%p\n", __FUNCTION__, Context));\r
c5f235bb
LE
358 VgpuDev = Context;\r
359 VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, 0);\r
360}\r
a66ea3b5
LE
361\r
362/**\r
363 Internal utility function that sends a request to the VirtIo GPU device\r
364 model, awaits the answer from the host, and returns a status.\r
365\r
366 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU\r
367 device. The caller is responsible to have\r
368 successfully invoked VirtioGpuInit() on VgpuDev\r
369 previously, while VirtioGpuUninit() must not have\r
370 been called on VgpuDev.\r
371\r
372 @param[in] RequestType The type of the request. The caller is responsible\r
373 for providing a VirtioGpuCmd* RequestType which, on\r
374 success, elicits a VirtioGpuRespOkNodata response\r
375 from the host.\r
376\r
377 @param[in] Fence Whether to enable fencing for this request. Fencing\r
378 forces the host to complete the command before\r
379 producing a response. If Fence is TRUE, then\r
380 VgpuDev->FenceId is consumed, and incremented.\r
381\r
382 @param[in,out] Header Pointer to the caller-allocated request object. The\r
383 request must start with VIRTIO_GPU_CONTROL_HEADER.\r
384 This function overwrites all fields of Header before\r
385 submitting the request to the host:\r
386\r
387 - it sets Type from RequestType,\r
388\r
389 - it sets Flags and FenceId based on Fence,\r
390\r
391 - it zeroes CtxId and Padding.\r
392\r
393 @param[in] RequestSize Size of the entire caller-allocated request object,\r
394 including the leading VIRTIO_GPU_CONTROL_HEADER.\r
395\r
396 @retval EFI_SUCCESS Operation successful.\r
397\r
398 @retval EFI_DEVICE_ERROR The host rejected the request. The host error\r
70d5086c 399 code has been logged on the DEBUG_ERROR level.\r
a66ea3b5
LE
400\r
401 @return Codes for unexpected errors in VirtIo\r
067b6483
LE
402 messaging, or request/response\r
403 mapping/unmapping.\r
a66ea3b5
LE
404**/\r
405STATIC\r
406EFI_STATUS\r
407VirtioGpuSendCommand (\r
ac0a286f
MK
408 IN OUT VGPU_DEV *VgpuDev,\r
409 IN VIRTIO_GPU_CONTROL_TYPE RequestType,\r
410 IN BOOLEAN Fence,\r
411 IN OUT volatile VIRTIO_GPU_CONTROL_HEADER *Header,\r
412 IN UINTN RequestSize\r
a66ea3b5
LE
413 )\r
414{\r
ac0a286f
MK
415 DESC_INDICES Indices;\r
416 volatile VIRTIO_GPU_CONTROL_HEADER Response;\r
417 EFI_STATUS Status;\r
418 UINT32 ResponseSize;\r
419 EFI_PHYSICAL_ADDRESS RequestDeviceAddress;\r
420 VOID *RequestMap;\r
421 EFI_PHYSICAL_ADDRESS ResponseDeviceAddress;\r
422 VOID *ResponseMap;\r
a66ea3b5
LE
423\r
424 //\r
425 // Initialize Header.\r
426 //\r
ac0a286f 427 Header->Type = RequestType;\r
a66ea3b5
LE
428 if (Fence) {\r
429 Header->Flags = VIRTIO_GPU_FLAG_FENCE;\r
430 Header->FenceId = VgpuDev->FenceId++;\r
431 } else {\r
432 Header->Flags = 0;\r
433 Header->FenceId = 0;\r
434 }\r
ac0a286f
MK
435\r
436 Header->CtxId = 0;\r
437 Header->Padding = 0;\r
a66ea3b5
LE
438\r
439 ASSERT (RequestSize >= sizeof *Header);\r
7f1bf51b 440 ASSERT (RequestSize <= MAX_UINT32);\r
a66ea3b5 441\r
067b6483
LE
442 //\r
443 // Map request and response to bus master device addresses.\r
444 //\r
445 Status = VirtioMapAllBytesInSharedBuffer (\r
446 VgpuDev->VirtIo,\r
447 VirtioOperationBusMasterRead,\r
448 (VOID *)Header,\r
449 RequestSize,\r
450 &RequestDeviceAddress,\r
451 &RequestMap\r
452 );\r
453 if (EFI_ERROR (Status)) {\r
454 return Status;\r
455 }\r
ac0a286f 456\r
067b6483
LE
457 Status = VirtioMapAllBytesInSharedBuffer (\r
458 VgpuDev->VirtIo,\r
459 VirtioOperationBusMasterWrite,\r
460 (VOID *)&Response,\r
461 sizeof Response,\r
462 &ResponseDeviceAddress,\r
463 &ResponseMap\r
464 );\r
465 if (EFI_ERROR (Status)) {\r
466 goto UnmapRequest;\r
467 }\r
468\r
a66ea3b5
LE
469 //\r
470 // Compose the descriptor chain.\r
471 //\r
472 VirtioPrepare (&VgpuDev->Ring, &Indices);\r
067b6483
LE
473 VirtioAppendDesc (\r
474 &VgpuDev->Ring,\r
475 RequestDeviceAddress,\r
476 (UINT32)RequestSize,\r
477 VRING_DESC_F_NEXT,\r
478 &Indices\r
479 );\r
480 VirtioAppendDesc (\r
481 &VgpuDev->Ring,\r
482 ResponseDeviceAddress,\r
483 (UINT32)sizeof Response,\r
484 VRING_DESC_F_WRITE,\r
485 &Indices\r
486 );\r
a66ea3b5
LE
487\r
488 //\r
489 // Send the command.\r
490 //\r
ac0a286f
MK
491 Status = VirtioFlush (\r
492 VgpuDev->VirtIo,\r
493 VIRTIO_GPU_CONTROL_QUEUE,\r
494 &VgpuDev->Ring,\r
495 &Indices,\r
496 &ResponseSize\r
497 );\r
a66ea3b5 498 if (EFI_ERROR (Status)) {\r
067b6483 499 goto UnmapResponse;\r
a66ea3b5
LE
500 }\r
501\r
502 //\r
067b6483 503 // Verify response size.\r
a66ea3b5
LE
504 //\r
505 if (ResponseSize != sizeof Response) {\r
ac0a286f
MK
506 DEBUG ((\r
507 DEBUG_ERROR,\r
508 "%a: malformed response to Request=0x%x\n",\r
509 __FUNCTION__,\r
510 (UINT32)RequestType\r
511 ));\r
067b6483
LE
512 Status = EFI_PROTOCOL_ERROR;\r
513 goto UnmapResponse;\r
a66ea3b5
LE
514 }\r
515\r
067b6483
LE
516 //\r
517 // Unmap response and request, in reverse order of mapping. On error, the\r
518 // respective mapping is invalidated anyway, only the data may not have been\r
519 // committed to system memory (in case of VirtioOperationBusMasterWrite).\r
520 //\r
521 Status = VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, ResponseMap);\r
522 if (EFI_ERROR (Status)) {\r
523 goto UnmapRequest;\r
524 }\r
ac0a286f 525\r
067b6483
LE
526 Status = VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, RequestMap);\r
527 if (EFI_ERROR (Status)) {\r
528 return Status;\r
529 }\r
530\r
531 //\r
532 // Parse the response.\r
533 //\r
a66ea3b5
LE
534 if (Response.Type == VirtioGpuRespOkNodata) {\r
535 return EFI_SUCCESS;\r
536 }\r
537\r
ac0a286f
MK
538 DEBUG ((\r
539 DEBUG_ERROR,\r
540 "%a: Request=0x%x Response=0x%x\n",\r
541 __FUNCTION__,\r
542 (UINT32)RequestType,\r
543 Response.Type\r
544 ));\r
a66ea3b5 545 return EFI_DEVICE_ERROR;\r
067b6483
LE
546\r
547UnmapResponse:\r
548 VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, ResponseMap);\r
549\r
550UnmapRequest:\r
551 VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, RequestMap);\r
552\r
553 return Status;\r
a66ea3b5
LE
554}\r
555\r
556/**\r
557 The following functions send requests to the VirtIo GPU device model, await\r
558 the answer from the host, and return a status. They share the following\r
559 interface details:\r
560\r
561 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU\r
562 device. The caller is responsible to have\r
563 successfully invoked VirtioGpuInit() on VgpuDev\r
564 previously, while VirtioGpuUninit() must not have\r
565 been called on VgpuDev.\r
566\r
567 @retval EFI_INVALID_PARAMETER Invalid command-specific parameters were\r
568 detected by this driver.\r
569\r
570 @retval EFI_SUCCESS Operation successful.\r
571\r
572 @retval EFI_DEVICE_ERROR The host rejected the request. The host error\r
70d5086c 573 code has been logged on the DEBUG_ERROR level.\r
a66ea3b5
LE
574\r
575 @return Codes for unexpected errors in VirtIo\r
576 messaging.\r
577\r
578 For the command-specific parameters, please consult the GPU Device section of\r
579 the VirtIo 1.0 specification (see references in\r
580 "OvmfPkg/Include/IndustryStandard/VirtioGpu.h").\r
581**/\r
582EFI_STATUS\r
583VirtioGpuResourceCreate2d (\r
ac0a286f
MK
584 IN OUT VGPU_DEV *VgpuDev,\r
585 IN UINT32 ResourceId,\r
586 IN VIRTIO_GPU_FORMATS Format,\r
587 IN UINT32 Width,\r
588 IN UINT32 Height\r
a66ea3b5
LE
589 )\r
590{\r
ac0a286f 591 volatile VIRTIO_GPU_RESOURCE_CREATE_2D Request;\r
a66ea3b5
LE
592\r
593 if (ResourceId == 0) {\r
594 return EFI_INVALID_PARAMETER;\r
595 }\r
596\r
597 Request.ResourceId = ResourceId;\r
598 Request.Format = (UINT32)Format;\r
599 Request.Width = Width;\r
600 Request.Height = Height;\r
601\r
602 return VirtioGpuSendCommand (\r
603 VgpuDev,\r
604 VirtioGpuCmdResourceCreate2d,\r
605 FALSE, // Fence\r
606 &Request.Header,\r
607 sizeof Request\r
608 );\r
609}\r
610\r
611EFI_STATUS\r
612VirtioGpuResourceUnref (\r
ac0a286f
MK
613 IN OUT VGPU_DEV *VgpuDev,\r
614 IN UINT32 ResourceId\r
a66ea3b5
LE
615 )\r
616{\r
ac0a286f 617 volatile VIRTIO_GPU_RESOURCE_UNREF Request;\r
a66ea3b5
LE
618\r
619 if (ResourceId == 0) {\r
620 return EFI_INVALID_PARAMETER;\r
621 }\r
622\r
623 Request.ResourceId = ResourceId;\r
624 Request.Padding = 0;\r
625\r
626 return VirtioGpuSendCommand (\r
627 VgpuDev,\r
628 VirtioGpuCmdResourceUnref,\r
629 FALSE, // Fence\r
630 &Request.Header,\r
631 sizeof Request\r
632 );\r
633}\r
634\r
635EFI_STATUS\r
636VirtioGpuResourceAttachBacking (\r
ac0a286f
MK
637 IN OUT VGPU_DEV *VgpuDev,\r
638 IN UINT32 ResourceId,\r
639 IN EFI_PHYSICAL_ADDRESS BackingStoreDeviceAddress,\r
640 IN UINTN NumberOfPages\r
a66ea3b5
LE
641 )\r
642{\r
ac0a286f 643 volatile VIRTIO_GPU_RESOURCE_ATTACH_BACKING Request;\r
a66ea3b5
LE
644\r
645 if (ResourceId == 0) {\r
646 return EFI_INVALID_PARAMETER;\r
647 }\r
648\r
649 Request.ResourceId = ResourceId;\r
650 Request.NrEntries = 1;\r
5409c6ab 651 Request.Entry.Addr = BackingStoreDeviceAddress;\r
a66ea3b5
LE
652 Request.Entry.Length = (UINT32)EFI_PAGES_TO_SIZE (NumberOfPages);\r
653 Request.Entry.Padding = 0;\r
654\r
655 return VirtioGpuSendCommand (\r
656 VgpuDev,\r
657 VirtioGpuCmdResourceAttachBacking,\r
658 FALSE, // Fence\r
659 &Request.Header,\r
660 sizeof Request\r
661 );\r
662}\r
663\r
664EFI_STATUS\r
665VirtioGpuResourceDetachBacking (\r
ac0a286f
MK
666 IN OUT VGPU_DEV *VgpuDev,\r
667 IN UINT32 ResourceId\r
a66ea3b5
LE
668 )\r
669{\r
ac0a286f 670 volatile VIRTIO_GPU_RESOURCE_DETACH_BACKING Request;\r
a66ea3b5
LE
671\r
672 if (ResourceId == 0) {\r
673 return EFI_INVALID_PARAMETER;\r
674 }\r
675\r
676 Request.ResourceId = ResourceId;\r
677 Request.Padding = 0;\r
678\r
679 //\r
680 // In this case, we set Fence to TRUE, because after this function returns,\r
681 // the caller might reasonably want to repurpose the backing pages\r
682 // immediately. Thus we should ensure that the host releases all references\r
683 // to the backing pages before we return.\r
684 //\r
685 return VirtioGpuSendCommand (\r
686 VgpuDev,\r
687 VirtioGpuCmdResourceDetachBacking,\r
688 TRUE, // Fence\r
689 &Request.Header,\r
690 sizeof Request\r
691 );\r
692}\r
693\r
694EFI_STATUS\r
695VirtioGpuSetScanout (\r
ac0a286f
MK
696 IN OUT VGPU_DEV *VgpuDev,\r
697 IN UINT32 X,\r
698 IN UINT32 Y,\r
699 IN UINT32 Width,\r
700 IN UINT32 Height,\r
701 IN UINT32 ScanoutId,\r
702 IN UINT32 ResourceId\r
a66ea3b5
LE
703 )\r
704{\r
ac0a286f 705 volatile VIRTIO_GPU_SET_SCANOUT Request;\r
a66ea3b5
LE
706\r
707 //\r
708 // Unlike for most other commands, ResourceId=0 is valid; it\r
709 // is used to disable a scanout.\r
710 //\r
711 Request.Rectangle.X = X;\r
712 Request.Rectangle.Y = Y;\r
713 Request.Rectangle.Width = Width;\r
714 Request.Rectangle.Height = Height;\r
715 Request.ScanoutId = ScanoutId;\r
716 Request.ResourceId = ResourceId;\r
717\r
718 return VirtioGpuSendCommand (\r
719 VgpuDev,\r
720 VirtioGpuCmdSetScanout,\r
721 FALSE, // Fence\r
722 &Request.Header,\r
723 sizeof Request\r
724 );\r
725}\r
726\r
727EFI_STATUS\r
728VirtioGpuTransferToHost2d (\r
ac0a286f
MK
729 IN OUT VGPU_DEV *VgpuDev,\r
730 IN UINT32 X,\r
731 IN UINT32 Y,\r
732 IN UINT32 Width,\r
733 IN UINT32 Height,\r
734 IN UINT64 Offset,\r
735 IN UINT32 ResourceId\r
a66ea3b5
LE
736 )\r
737{\r
ac0a286f 738 volatile VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D Request;\r
a66ea3b5
LE
739\r
740 if (ResourceId == 0) {\r
741 return EFI_INVALID_PARAMETER;\r
742 }\r
743\r
744 Request.Rectangle.X = X;\r
745 Request.Rectangle.Y = Y;\r
746 Request.Rectangle.Width = Width;\r
747 Request.Rectangle.Height = Height;\r
748 Request.Offset = Offset;\r
749 Request.ResourceId = ResourceId;\r
750 Request.Padding = 0;\r
751\r
752 return VirtioGpuSendCommand (\r
753 VgpuDev,\r
754 VirtioGpuCmdTransferToHost2d,\r
755 FALSE, // Fence\r
756 &Request.Header,\r
757 sizeof Request\r
758 );\r
759}\r
760\r
761EFI_STATUS\r
762VirtioGpuResourceFlush (\r
ac0a286f
MK
763 IN OUT VGPU_DEV *VgpuDev,\r
764 IN UINT32 X,\r
765 IN UINT32 Y,\r
766 IN UINT32 Width,\r
767 IN UINT32 Height,\r
768 IN UINT32 ResourceId\r
a66ea3b5
LE
769 )\r
770{\r
ac0a286f 771 volatile VIRTIO_GPU_RESOURCE_FLUSH Request;\r
a66ea3b5
LE
772\r
773 if (ResourceId == 0) {\r
774 return EFI_INVALID_PARAMETER;\r
775 }\r
776\r
777 Request.Rectangle.X = X;\r
778 Request.Rectangle.Y = Y;\r
779 Request.Rectangle.Width = Width;\r
780 Request.Rectangle.Height = Height;\r
781 Request.ResourceId = ResourceId;\r
782 Request.Padding = 0;\r
783\r
784 return VirtioGpuSendCommand (\r
785 VgpuDev,\r
786 VirtioGpuCmdResourceFlush,\r
787 FALSE, // Fence\r
788 &Request.Header,\r
789 sizeof Request\r
790 );\r
791}\r