3 VirtIo GPU initialization, and commands (primitives) for the GPU device.
5 Copyright (C) 2016, Red Hat, Inc.
6 Copyright (c) 2017, AMD Inc, All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Library/VirtioLib.h>
14 #include "VirtioGpu.h"
17 Configure the VirtIo GPU device that underlies VgpuDev.
19 @param[in,out] VgpuDev The VGPU_DEV object to set up VirtIo messaging for.
20 On input, the caller is responsible for having
21 initialized VgpuDev->VirtIo. On output, VgpuDev->Ring
22 has been initialized, and synchronous VirtIo GPU
23 commands (primitives) can be submitted to the device.
25 @retval EFI_SUCCESS VirtIo GPU configuration successful.
27 @retval EFI_UNSUPPORTED The host-side configuration of the VirtIo GPU is not
28 supported by this driver.
30 @retval Error codes from underlying functions.
34 IN OUT VGPU_DEV
*VgpuDev
44 // Execute virtio-v1.0-cs04, 3.1.1 Driver Requirements: Device
47 // 1. Reset the device.
50 Status
= VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
51 if (EFI_ERROR (Status
)) {
56 // 2. Set the ACKNOWLEDGE status bit [...]
58 NextDevStat
|= VSTAT_ACK
;
59 Status
= VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
60 if (EFI_ERROR (Status
)) {
65 // 3. Set the DRIVER status bit [...]
67 NextDevStat
|= VSTAT_DRIVER
;
68 Status
= VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
69 if (EFI_ERROR (Status
)) {
74 // 4. Read device feature bits...
76 Status
= VgpuDev
->VirtIo
->GetDeviceFeatures (VgpuDev
->VirtIo
, &Features
);
77 if (EFI_ERROR (Status
)) {
80 if ((Features
& VIRTIO_F_VERSION_1
) == 0) {
81 Status
= EFI_UNSUPPORTED
;
85 // We only want the most basic 2D features.
87 Features
&= VIRTIO_F_VERSION_1
| VIRTIO_F_IOMMU_PLATFORM
;
90 // ... and write the subset of feature bits understood by the [...] driver to
92 // 5. Set the FEATURES_OK status bit.
93 // 6. Re-read device status to ensure the FEATURES_OK bit is still set [...]
95 Status
= Virtio10WriteFeatures (VgpuDev
->VirtIo
, Features
, &NextDevStat
);
96 if (EFI_ERROR (Status
)) {
101 // 7. Perform device-specific setup, including discovery of virtqueues for
104 Status
= VgpuDev
->VirtIo
->SetQueueSel (VgpuDev
->VirtIo
,
105 VIRTIO_GPU_CONTROL_QUEUE
);
106 if (EFI_ERROR (Status
)) {
109 Status
= VgpuDev
->VirtIo
->GetQueueNumMax (VgpuDev
->VirtIo
, &QueueSize
);
110 if (EFI_ERROR (Status
)) {
115 // We implement each VirtIo GPU command that we use with two descriptors:
116 // request, response.
119 Status
= EFI_UNSUPPORTED
;
124 // [...] population of virtqueues [...]
126 Status
= VirtioRingInit (VgpuDev
->VirtIo
, QueueSize
, &VgpuDev
->Ring
);
127 if (EFI_ERROR (Status
)) {
131 // If anything fails from here on, we have to release the ring.
133 Status
= VirtioRingMap (
139 if (EFI_ERROR (Status
)) {
143 // If anything fails from here on, we have to unmap the ring.
145 Status
= VgpuDev
->VirtIo
->SetQueueAddress (
150 if (EFI_ERROR (Status
)) {
155 // 8. Set the DRIVER_OK status bit.
157 NextDevStat
|= VSTAT_DRIVER_OK
;
158 Status
= VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
159 if (EFI_ERROR (Status
)) {
166 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, VgpuDev
->RingMap
);
169 VirtioRingUninit (VgpuDev
->VirtIo
, &VgpuDev
->Ring
);
173 // If any of these steps go irrecoverably wrong, the driver SHOULD set the
174 // FAILED status bit to indicate that it has given up on the device (it can
175 // reset the device later to restart if desired). [...]
177 // VirtIo access failure here should not mask the original error.
179 NextDevStat
|= VSTAT_FAILED
;
180 VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
186 De-configure the VirtIo GPU device that underlies VgpuDev.
188 @param[in,out] VgpuDev The VGPU_DEV object to tear down VirtIo messaging
189 for. On input, the caller is responsible for having
190 called VirtioGpuInit(). On output, VgpuDev->Ring has
191 been uninitialized; VirtIo GPU commands (primitives)
192 can no longer be submitted to the device.
196 IN OUT VGPU_DEV
*VgpuDev
200 // Resetting the VirtIo device makes it release its resources and forget its
203 VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, 0);
204 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, VgpuDev
->RingMap
);
205 VirtioRingUninit (VgpuDev
->VirtIo
, &VgpuDev
->Ring
);
209 Allocate, zero and map memory, for bus master common buffer operation, to be
210 attached as backing store to a host-side VirtIo GPU resource.
212 @param[in] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
215 @param[in] NumberOfPages The number of whole pages to allocate and map.
217 @param[out] HostAddress The system memory address of the allocated area.
219 @param[out] DeviceAddress The bus master device address of the allocated
220 area. The VirtIo GPU device may be programmed to
221 access the allocated area through DeviceAddress;
222 DeviceAddress is to be passed to the
223 VirtioGpuResourceAttachBacking() function, as the
224 BackingStoreDeviceAddress parameter.
226 @param[out] Mapping A resulting token to pass to
227 VirtioGpuUnmapAndFreeBackingStore().
229 @retval EFI_SUCCESS The requested number of pages has been allocated, zeroed
232 @return Status codes propagated from
233 VgpuDev->VirtIo->AllocateSharedPages() and
234 VirtioMapAllBytesInSharedBuffer().
237 VirtioGpuAllocateZeroAndMapBackingStore (
238 IN VGPU_DEV
*VgpuDev
,
239 IN UINTN NumberOfPages
,
240 OUT VOID
**HostAddress
,
241 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
246 VOID
*NewHostAddress
;
248 Status
= VgpuDev
->VirtIo
->AllocateSharedPages (
253 if (EFI_ERROR (Status
)) {
258 // Avoid exposing stale data to the device even temporarily: zero the area
259 // before mapping it.
261 ZeroMem (NewHostAddress
, EFI_PAGES_TO_SIZE (NumberOfPages
));
263 Status
= VirtioMapAllBytesInSharedBuffer (
264 VgpuDev
->VirtIo
, // VirtIo
265 VirtioOperationBusMasterCommonBuffer
, // Operation
266 NewHostAddress
, // HostAddress
267 EFI_PAGES_TO_SIZE (NumberOfPages
), // NumberOfBytes
268 DeviceAddress
, // DeviceAddress
271 if (EFI_ERROR (Status
)) {
272 goto FreeSharedPages
;
275 *HostAddress
= NewHostAddress
;
279 VgpuDev
->VirtIo
->FreeSharedPages (
288 Unmap and free memory originally allocated and mapped with
289 VirtioGpuAllocateZeroAndMapBackingStore().
291 If the memory allocated and mapped with
292 VirtioGpuAllocateZeroAndMapBackingStore() was attached to a host-side VirtIo
293 GPU resource with VirtioGpuResourceAttachBacking(), then the caller is
294 responsible for detaching the backing store from the same resource, with
295 VirtioGpuResourceDetachBacking(), before calling this function.
297 @param[in] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
300 @param[in] NumberOfPages The NumberOfPages parameter originally passed to
301 VirtioGpuAllocateZeroAndMapBackingStore().
303 @param[in] HostAddress The HostAddress value originally output by
304 VirtioGpuAllocateZeroAndMapBackingStore().
306 @param[in] Mapping The token that was originally output by
307 VirtioGpuAllocateZeroAndMapBackingStore().
310 VirtioGpuUnmapAndFreeBackingStore (
311 IN VGPU_DEV
*VgpuDev
,
312 IN UINTN NumberOfPages
,
313 IN VOID
*HostAddress
,
317 VgpuDev
->VirtIo
->UnmapSharedBuffer (
321 VgpuDev
->VirtIo
->FreeSharedPages (
329 EFI_EVENT_NOTIFY function for the VGPU_DEV.ExitBoot event. It resets the
330 VirtIo device, causing it to release its resources and to forget its
333 This function may only be called (that is, VGPU_DEV.ExitBoot may only be
334 signaled) after VirtioGpuInit() returns and before VirtioGpuUninit() is
337 @param[in] Event Event whose notification function is being invoked.
339 @param[in] Context Pointer to the associated VGPU_DEV object.
350 DEBUG ((DEBUG_VERBOSE
, "%a: Context=0x%p\n", __FUNCTION__
, Context
));
352 VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, 0);
356 Internal utility function that sends a request to the VirtIo GPU device
357 model, awaits the answer from the host, and returns a status.
359 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
360 device. The caller is responsible to have
361 successfully invoked VirtioGpuInit() on VgpuDev
362 previously, while VirtioGpuUninit() must not have
363 been called on VgpuDev.
365 @param[in] RequestType The type of the request. The caller is responsible
366 for providing a VirtioGpuCmd* RequestType which, on
367 success, elicits a VirtioGpuRespOkNodata response
370 @param[in] Fence Whether to enable fencing for this request. Fencing
371 forces the host to complete the command before
372 producing a response. If Fence is TRUE, then
373 VgpuDev->FenceId is consumed, and incremented.
375 @param[in,out] Header Pointer to the caller-allocated request object. The
376 request must start with VIRTIO_GPU_CONTROL_HEADER.
377 This function overwrites all fields of Header before
378 submitting the request to the host:
380 - it sets Type from RequestType,
382 - it sets Flags and FenceId based on Fence,
384 - it zeroes CtxId and Padding.
386 @param[in] RequestSize Size of the entire caller-allocated request object,
387 including the leading VIRTIO_GPU_CONTROL_HEADER.
389 @retval EFI_SUCCESS Operation successful.
391 @retval EFI_DEVICE_ERROR The host rejected the request. The host error
392 code has been logged on the DEBUG_ERROR level.
394 @return Codes for unexpected errors in VirtIo
395 messaging, or request/response
400 VirtioGpuSendCommand (
401 IN OUT VGPU_DEV
*VgpuDev
,
402 IN VIRTIO_GPU_CONTROL_TYPE RequestType
,
404 IN OUT
volatile VIRTIO_GPU_CONTROL_HEADER
*Header
,
408 DESC_INDICES Indices
;
409 volatile VIRTIO_GPU_CONTROL_HEADER Response
;
412 EFI_PHYSICAL_ADDRESS RequestDeviceAddress
;
414 EFI_PHYSICAL_ADDRESS ResponseDeviceAddress
;
418 // Initialize Header.
420 Header
->Type
= RequestType
;
422 Header
->Flags
= VIRTIO_GPU_FLAG_FENCE
;
423 Header
->FenceId
= VgpuDev
->FenceId
++;
431 ASSERT (RequestSize
>= sizeof *Header
);
432 ASSERT (RequestSize
<= MAX_UINT32
);
435 // Map request and response to bus master device addresses.
437 Status
= VirtioMapAllBytesInSharedBuffer (
439 VirtioOperationBusMasterRead
,
442 &RequestDeviceAddress
,
445 if (EFI_ERROR (Status
)) {
448 Status
= VirtioMapAllBytesInSharedBuffer (
450 VirtioOperationBusMasterWrite
,
453 &ResponseDeviceAddress
,
456 if (EFI_ERROR (Status
)) {
461 // Compose the descriptor chain.
463 VirtioPrepare (&VgpuDev
->Ring
, &Indices
);
466 RequestDeviceAddress
,
473 ResponseDeviceAddress
,
474 (UINT32
)sizeof Response
,
482 Status
= VirtioFlush (VgpuDev
->VirtIo
, VIRTIO_GPU_CONTROL_QUEUE
,
483 &VgpuDev
->Ring
, &Indices
, &ResponseSize
);
484 if (EFI_ERROR (Status
)) {
489 // Verify response size.
491 if (ResponseSize
!= sizeof Response
) {
492 DEBUG ((DEBUG_ERROR
, "%a: malformed response to Request=0x%x\n",
493 __FUNCTION__
, (UINT32
)RequestType
));
494 Status
= EFI_PROTOCOL_ERROR
;
499 // Unmap response and request, in reverse order of mapping. On error, the
500 // respective mapping is invalidated anyway, only the data may not have been
501 // committed to system memory (in case of VirtioOperationBusMasterWrite).
503 Status
= VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, ResponseMap
);
504 if (EFI_ERROR (Status
)) {
507 Status
= VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, RequestMap
);
508 if (EFI_ERROR (Status
)) {
513 // Parse the response.
515 if (Response
.Type
== VirtioGpuRespOkNodata
) {
519 DEBUG ((DEBUG_ERROR
, "%a: Request=0x%x Response=0x%x\n", __FUNCTION__
,
520 (UINT32
)RequestType
, Response
.Type
));
521 return EFI_DEVICE_ERROR
;
524 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, ResponseMap
);
527 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, RequestMap
);
533 The following functions send requests to the VirtIo GPU device model, await
534 the answer from the host, and return a status. They share the following
537 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
538 device. The caller is responsible to have
539 successfully invoked VirtioGpuInit() on VgpuDev
540 previously, while VirtioGpuUninit() must not have
541 been called on VgpuDev.
543 @retval EFI_INVALID_PARAMETER Invalid command-specific parameters were
544 detected by this driver.
546 @retval EFI_SUCCESS Operation successful.
548 @retval EFI_DEVICE_ERROR The host rejected the request. The host error
549 code has been logged on the DEBUG_ERROR level.
551 @return Codes for unexpected errors in VirtIo
554 For the command-specific parameters, please consult the GPU Device section of
555 the VirtIo 1.0 specification (see references in
556 "OvmfPkg/Include/IndustryStandard/VirtioGpu.h").
559 VirtioGpuResourceCreate2d (
560 IN OUT VGPU_DEV
*VgpuDev
,
561 IN UINT32 ResourceId
,
562 IN VIRTIO_GPU_FORMATS Format
,
567 volatile VIRTIO_GPU_RESOURCE_CREATE_2D Request
;
569 if (ResourceId
== 0) {
570 return EFI_INVALID_PARAMETER
;
573 Request
.ResourceId
= ResourceId
;
574 Request
.Format
= (UINT32
)Format
;
575 Request
.Width
= Width
;
576 Request
.Height
= Height
;
578 return VirtioGpuSendCommand (
580 VirtioGpuCmdResourceCreate2d
,
588 VirtioGpuResourceUnref (
589 IN OUT VGPU_DEV
*VgpuDev
,
593 volatile VIRTIO_GPU_RESOURCE_UNREF Request
;
595 if (ResourceId
== 0) {
596 return EFI_INVALID_PARAMETER
;
599 Request
.ResourceId
= ResourceId
;
602 return VirtioGpuSendCommand (
604 VirtioGpuCmdResourceUnref
,
612 VirtioGpuResourceAttachBacking (
613 IN OUT VGPU_DEV
*VgpuDev
,
614 IN UINT32 ResourceId
,
615 IN EFI_PHYSICAL_ADDRESS BackingStoreDeviceAddress
,
616 IN UINTN NumberOfPages
619 volatile VIRTIO_GPU_RESOURCE_ATTACH_BACKING Request
;
621 if (ResourceId
== 0) {
622 return EFI_INVALID_PARAMETER
;
625 Request
.ResourceId
= ResourceId
;
626 Request
.NrEntries
= 1;
627 Request
.Entry
.Addr
= BackingStoreDeviceAddress
;
628 Request
.Entry
.Length
= (UINT32
)EFI_PAGES_TO_SIZE (NumberOfPages
);
629 Request
.Entry
.Padding
= 0;
631 return VirtioGpuSendCommand (
633 VirtioGpuCmdResourceAttachBacking
,
641 VirtioGpuResourceDetachBacking (
642 IN OUT VGPU_DEV
*VgpuDev
,
646 volatile VIRTIO_GPU_RESOURCE_DETACH_BACKING Request
;
648 if (ResourceId
== 0) {
649 return EFI_INVALID_PARAMETER
;
652 Request
.ResourceId
= ResourceId
;
656 // In this case, we set Fence to TRUE, because after this function returns,
657 // the caller might reasonably want to repurpose the backing pages
658 // immediately. Thus we should ensure that the host releases all references
659 // to the backing pages before we return.
661 return VirtioGpuSendCommand (
663 VirtioGpuCmdResourceDetachBacking
,
671 VirtioGpuSetScanout (
672 IN OUT VGPU_DEV
*VgpuDev
,
681 volatile VIRTIO_GPU_SET_SCANOUT Request
;
684 // Unlike for most other commands, ResourceId=0 is valid; it
685 // is used to disable a scanout.
687 Request
.Rectangle
.X
= X
;
688 Request
.Rectangle
.Y
= Y
;
689 Request
.Rectangle
.Width
= Width
;
690 Request
.Rectangle
.Height
= Height
;
691 Request
.ScanoutId
= ScanoutId
;
692 Request
.ResourceId
= ResourceId
;
694 return VirtioGpuSendCommand (
696 VirtioGpuCmdSetScanout
,
704 VirtioGpuTransferToHost2d (
705 IN OUT VGPU_DEV
*VgpuDev
,
714 volatile VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D Request
;
716 if (ResourceId
== 0) {
717 return EFI_INVALID_PARAMETER
;
720 Request
.Rectangle
.X
= X
;
721 Request
.Rectangle
.Y
= Y
;
722 Request
.Rectangle
.Width
= Width
;
723 Request
.Rectangle
.Height
= Height
;
724 Request
.Offset
= Offset
;
725 Request
.ResourceId
= ResourceId
;
728 return VirtioGpuSendCommand (
730 VirtioGpuCmdTransferToHost2d
,
738 VirtioGpuResourceFlush (
739 IN OUT VGPU_DEV
*VgpuDev
,
747 volatile VIRTIO_GPU_RESOURCE_FLUSH Request
;
749 if (ResourceId
== 0) {
750 return EFI_INVALID_PARAMETER
;
753 Request
.Rectangle
.X
= X
;
754 Request
.Rectangle
.Y
= Y
;
755 Request
.Rectangle
.Width
= Width
;
756 Request
.Rectangle
.Height
= Height
;
757 Request
.ResourceId
= ResourceId
;
760 return VirtioGpuSendCommand (
762 VirtioGpuCmdResourceFlush
,