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
)) {
81 if ((Features
& VIRTIO_F_VERSION_1
) == 0) {
82 Status
= EFI_UNSUPPORTED
;
87 // We only want the most basic 2D features.
89 Features
&= VIRTIO_F_VERSION_1
| VIRTIO_F_IOMMU_PLATFORM
;
92 // ... and write the subset of feature bits understood by the [...] driver to
94 // 5. Set the FEATURES_OK status bit.
95 // 6. Re-read device status to ensure the FEATURES_OK bit is still set [...]
97 Status
= Virtio10WriteFeatures (VgpuDev
->VirtIo
, Features
, &NextDevStat
);
98 if (EFI_ERROR (Status
)) {
103 // 7. Perform device-specific setup, including discovery of virtqueues for
106 Status
= VgpuDev
->VirtIo
->SetQueueSel (
108 VIRTIO_GPU_CONTROL_QUEUE
110 if (EFI_ERROR (Status
)) {
114 Status
= VgpuDev
->VirtIo
->GetQueueNumMax (VgpuDev
->VirtIo
, &QueueSize
);
115 if (EFI_ERROR (Status
)) {
120 // We implement each VirtIo GPU command that we use with two descriptors:
121 // request, response.
124 Status
= EFI_UNSUPPORTED
;
129 // [...] population of virtqueues [...]
131 Status
= VirtioRingInit (VgpuDev
->VirtIo
, QueueSize
, &VgpuDev
->Ring
);
132 if (EFI_ERROR (Status
)) {
137 // If anything fails from here on, we have to release the ring.
139 Status
= VirtioRingMap (
145 if (EFI_ERROR (Status
)) {
150 // If anything fails from here on, we have to unmap the ring.
152 Status
= VgpuDev
->VirtIo
->SetQueueAddress (
157 if (EFI_ERROR (Status
)) {
162 // 8. Set the DRIVER_OK status bit.
164 NextDevStat
|= VSTAT_DRIVER_OK
;
165 Status
= VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
166 if (EFI_ERROR (Status
)) {
173 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, VgpuDev
->RingMap
);
176 VirtioRingUninit (VgpuDev
->VirtIo
, &VgpuDev
->Ring
);
180 // If any of these steps go irrecoverably wrong, the driver SHOULD set the
181 // FAILED status bit to indicate that it has given up on the device (it can
182 // reset the device later to restart if desired). [...]
184 // VirtIo access failure here should not mask the original error.
186 NextDevStat
|= VSTAT_FAILED
;
187 VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
193 De-configure the VirtIo GPU device that underlies VgpuDev.
195 @param[in,out] VgpuDev The VGPU_DEV object to tear down VirtIo messaging
196 for. On input, the caller is responsible for having
197 called VirtioGpuInit(). On output, VgpuDev->Ring has
198 been uninitialized; VirtIo GPU commands (primitives)
199 can no longer be submitted to the device.
203 IN OUT VGPU_DEV
*VgpuDev
207 // Resetting the VirtIo device makes it release its resources and forget its
210 VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, 0);
211 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, VgpuDev
->RingMap
);
212 VirtioRingUninit (VgpuDev
->VirtIo
, &VgpuDev
->Ring
);
216 Allocate, zero and map memory, for bus master common buffer operation, to be
217 attached as backing store to a host-side VirtIo GPU resource.
219 @param[in] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
222 @param[in] NumberOfPages The number of whole pages to allocate and map.
224 @param[out] HostAddress The system memory address of the allocated area.
226 @param[out] DeviceAddress The bus master device address of the allocated
227 area. The VirtIo GPU device may be programmed to
228 access the allocated area through DeviceAddress;
229 DeviceAddress is to be passed to the
230 VirtioGpuResourceAttachBacking() function, as the
231 BackingStoreDeviceAddress parameter.
233 @param[out] Mapping A resulting token to pass to
234 VirtioGpuUnmapAndFreeBackingStore().
236 @retval EFI_SUCCESS The requested number of pages has been allocated, zeroed
239 @return Status codes propagated from
240 VgpuDev->VirtIo->AllocateSharedPages() and
241 VirtioMapAllBytesInSharedBuffer().
244 VirtioGpuAllocateZeroAndMapBackingStore (
245 IN VGPU_DEV
*VgpuDev
,
246 IN UINTN NumberOfPages
,
247 OUT VOID
**HostAddress
,
248 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
253 VOID
*NewHostAddress
;
255 Status
= VgpuDev
->VirtIo
->AllocateSharedPages (
260 if (EFI_ERROR (Status
)) {
265 // Avoid exposing stale data to the device even temporarily: zero the area
266 // before mapping it.
268 ZeroMem (NewHostAddress
, EFI_PAGES_TO_SIZE (NumberOfPages
));
270 Status
= VirtioMapAllBytesInSharedBuffer (
271 VgpuDev
->VirtIo
, // VirtIo
272 VirtioOperationBusMasterCommonBuffer
, // Operation
273 NewHostAddress
, // HostAddress
274 EFI_PAGES_TO_SIZE (NumberOfPages
), // NumberOfBytes
275 DeviceAddress
, // DeviceAddress
278 if (EFI_ERROR (Status
)) {
279 goto FreeSharedPages
;
282 *HostAddress
= NewHostAddress
;
286 VgpuDev
->VirtIo
->FreeSharedPages (
295 Unmap and free memory originally allocated and mapped with
296 VirtioGpuAllocateZeroAndMapBackingStore().
298 If the memory allocated and mapped with
299 VirtioGpuAllocateZeroAndMapBackingStore() was attached to a host-side VirtIo
300 GPU resource with VirtioGpuResourceAttachBacking(), then the caller is
301 responsible for detaching the backing store from the same resource, with
302 VirtioGpuResourceDetachBacking(), before calling this function.
304 @param[in] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
307 @param[in] NumberOfPages The NumberOfPages parameter originally passed to
308 VirtioGpuAllocateZeroAndMapBackingStore().
310 @param[in] HostAddress The HostAddress value originally output by
311 VirtioGpuAllocateZeroAndMapBackingStore().
313 @param[in] Mapping The token that was originally output by
314 VirtioGpuAllocateZeroAndMapBackingStore().
317 VirtioGpuUnmapAndFreeBackingStore (
318 IN VGPU_DEV
*VgpuDev
,
319 IN UINTN NumberOfPages
,
320 IN VOID
*HostAddress
,
324 VgpuDev
->VirtIo
->UnmapSharedBuffer (
328 VgpuDev
->VirtIo
->FreeSharedPages (
336 EFI_EVENT_NOTIFY function for the VGPU_DEV.ExitBoot event. It resets the
337 VirtIo device, causing it to release its resources and to forget its
340 This function may only be called (that is, VGPU_DEV.ExitBoot may only be
341 signaled) after VirtioGpuInit() returns and before VirtioGpuUninit() is
344 @param[in] Event Event whose notification function is being invoked.
346 @param[in] Context Pointer to the associated VGPU_DEV object.
357 DEBUG ((DEBUG_VERBOSE
, "%a: Context=0x%p\n", __FUNCTION__
, Context
));
359 VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, 0);
363 Internal utility function that sends a request to the VirtIo GPU device
364 model, awaits the answer from the host, and returns a status.
366 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
367 device. The caller is responsible to have
368 successfully invoked VirtioGpuInit() on VgpuDev
369 previously, while VirtioGpuUninit() must not have
370 been called on VgpuDev.
372 @param[in] RequestType The type of the request. The caller is responsible
373 for providing a VirtioGpuCmd* RequestType which, on
374 success, elicits a VirtioGpuRespOkNodata response
377 @param[in] Fence Whether to enable fencing for this request. Fencing
378 forces the host to complete the command before
379 producing a response. If Fence is TRUE, then
380 VgpuDev->FenceId is consumed, and incremented.
382 @param[in,out] Header Pointer to the caller-allocated request object. The
383 request must start with VIRTIO_GPU_CONTROL_HEADER.
384 This function overwrites all fields of Header before
385 submitting the request to the host:
387 - it sets Type from RequestType,
389 - it sets Flags and FenceId based on Fence,
391 - it zeroes CtxId and Padding.
393 @param[in] RequestSize Size of the entire caller-allocated request object,
394 including the leading VIRTIO_GPU_CONTROL_HEADER.
396 @param[in] ResponseType The type of the response (VirtioGpuResp*).
398 @param[in,out] Response Pointer to the caller-allocated response object. The
399 request must start with VIRTIO_GPU_CONTROL_HEADER.
401 @param[in] ResponseSize Size of the entire caller-allocated response object,
402 including the leading VIRTIO_GPU_CONTROL_HEADER.
404 @retval EFI_SUCCESS Operation successful.
406 @retval EFI_DEVICE_ERROR The host rejected the request. The host error
407 code has been logged on the DEBUG_ERROR level.
409 @return Codes for unexpected errors in VirtIo
410 messaging, or request/response
415 VirtioGpuSendCommandWithReply (
416 IN OUT VGPU_DEV
*VgpuDev
,
417 IN VIRTIO_GPU_CONTROL_TYPE RequestType
,
419 IN OUT
volatile VIRTIO_GPU_CONTROL_HEADER
*Header
,
420 IN UINTN RequestSize
,
421 IN VIRTIO_GPU_CONTROL_TYPE ResponseType
,
422 IN OUT
volatile VIRTIO_GPU_CONTROL_HEADER
*Response
,
423 IN UINTN ResponseSize
426 DESC_INDICES Indices
;
428 UINT32 ResponseSizeRet
;
429 EFI_PHYSICAL_ADDRESS RequestDeviceAddress
;
431 EFI_PHYSICAL_ADDRESS ResponseDeviceAddress
;
435 // Initialize Header.
437 Header
->Type
= RequestType
;
439 Header
->Flags
= VIRTIO_GPU_FLAG_FENCE
;
440 Header
->FenceId
= VgpuDev
->FenceId
++;
449 ASSERT (RequestSize
>= sizeof *Header
);
450 ASSERT (RequestSize
<= MAX_UINT32
);
453 // Map request and response to bus master device addresses.
455 Status
= VirtioMapAllBytesInSharedBuffer (
457 VirtioOperationBusMasterRead
,
460 &RequestDeviceAddress
,
463 if (EFI_ERROR (Status
)) {
467 Status
= VirtioMapAllBytesInSharedBuffer (
469 VirtioOperationBusMasterWrite
,
472 &ResponseDeviceAddress
,
475 if (EFI_ERROR (Status
)) {
480 // Compose the descriptor chain.
482 VirtioPrepare (&VgpuDev
->Ring
, &Indices
);
485 RequestDeviceAddress
,
492 ResponseDeviceAddress
,
493 (UINT32
)ResponseSize
,
501 Status
= VirtioFlush (
503 VIRTIO_GPU_CONTROL_QUEUE
,
508 if (EFI_ERROR (Status
)) {
513 // Verify response size.
515 if (ResponseSize
!= ResponseSizeRet
) {
518 "%a: malformed response to Request=0x%x\n",
522 Status
= EFI_PROTOCOL_ERROR
;
527 // Unmap response and request, in reverse order of mapping. On error, the
528 // respective mapping is invalidated anyway, only the data may not have been
529 // committed to system memory (in case of VirtioOperationBusMasterWrite).
531 Status
= VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, ResponseMap
);
532 if (EFI_ERROR (Status
)) {
536 Status
= VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, RequestMap
);
537 if (EFI_ERROR (Status
)) {
542 // Parse the response.
544 if (Response
->Type
== (UINT32
)ResponseType
) {
550 "%a: Request=0x%x Response=0x%x (expected 0x%x)\n",
556 return EFI_DEVICE_ERROR
;
559 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, ResponseMap
);
562 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, RequestMap
);
568 Simplified version of VirtioGpuSendCommandWithReply() for commands
569 which do not send back any data.
573 VirtioGpuSendCommand (
574 IN OUT VGPU_DEV
*VgpuDev
,
575 IN VIRTIO_GPU_CONTROL_TYPE RequestType
,
577 IN OUT
volatile VIRTIO_GPU_CONTROL_HEADER
*Header
,
581 volatile VIRTIO_GPU_CONTROL_HEADER Response
;
583 return VirtioGpuSendCommandWithReply (
589 VirtioGpuRespOkNodata
,
596 The following functions send requests to the VirtIo GPU device model, await
597 the answer from the host, and return a status. They share the following
600 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
601 device. The caller is responsible to have
602 successfully invoked VirtioGpuInit() on VgpuDev
603 previously, while VirtioGpuUninit() must not have
604 been called on VgpuDev.
606 @retval EFI_INVALID_PARAMETER Invalid command-specific parameters were
607 detected by this driver.
609 @retval EFI_SUCCESS Operation successful.
611 @retval EFI_DEVICE_ERROR The host rejected the request. The host error
612 code has been logged on the DEBUG_ERROR level.
614 @return Codes for unexpected errors in VirtIo
617 For the command-specific parameters, please consult the GPU Device section of
618 the VirtIo 1.0 specification (see references in
619 "OvmfPkg/Include/IndustryStandard/VirtioGpu.h").
622 VirtioGpuResourceCreate2d (
623 IN OUT VGPU_DEV
*VgpuDev
,
624 IN UINT32 ResourceId
,
625 IN VIRTIO_GPU_FORMATS Format
,
630 volatile VIRTIO_GPU_RESOURCE_CREATE_2D Request
;
632 if (ResourceId
== 0) {
633 return EFI_INVALID_PARAMETER
;
636 Request
.ResourceId
= ResourceId
;
637 Request
.Format
= (UINT32
)Format
;
638 Request
.Width
= Width
;
639 Request
.Height
= Height
;
641 return VirtioGpuSendCommand (
643 VirtioGpuCmdResourceCreate2d
,
651 VirtioGpuResourceUnref (
652 IN OUT VGPU_DEV
*VgpuDev
,
656 volatile VIRTIO_GPU_RESOURCE_UNREF Request
;
658 if (ResourceId
== 0) {
659 return EFI_INVALID_PARAMETER
;
662 Request
.ResourceId
= ResourceId
;
665 return VirtioGpuSendCommand (
667 VirtioGpuCmdResourceUnref
,
675 VirtioGpuResourceAttachBacking (
676 IN OUT VGPU_DEV
*VgpuDev
,
677 IN UINT32 ResourceId
,
678 IN EFI_PHYSICAL_ADDRESS BackingStoreDeviceAddress
,
679 IN UINTN NumberOfPages
682 volatile VIRTIO_GPU_RESOURCE_ATTACH_BACKING Request
;
684 if (ResourceId
== 0) {
685 return EFI_INVALID_PARAMETER
;
688 Request
.ResourceId
= ResourceId
;
689 Request
.NrEntries
= 1;
690 Request
.Entry
.Addr
= BackingStoreDeviceAddress
;
691 Request
.Entry
.Length
= (UINT32
)EFI_PAGES_TO_SIZE (NumberOfPages
);
692 Request
.Entry
.Padding
= 0;
694 return VirtioGpuSendCommand (
696 VirtioGpuCmdResourceAttachBacking
,
704 VirtioGpuResourceDetachBacking (
705 IN OUT VGPU_DEV
*VgpuDev
,
709 volatile VIRTIO_GPU_RESOURCE_DETACH_BACKING Request
;
711 if (ResourceId
== 0) {
712 return EFI_INVALID_PARAMETER
;
715 Request
.ResourceId
= ResourceId
;
719 // In this case, we set Fence to TRUE, because after this function returns,
720 // the caller might reasonably want to repurpose the backing pages
721 // immediately. Thus we should ensure that the host releases all references
722 // to the backing pages before we return.
724 return VirtioGpuSendCommand (
726 VirtioGpuCmdResourceDetachBacking
,
734 VirtioGpuSetScanout (
735 IN OUT VGPU_DEV
*VgpuDev
,
744 volatile VIRTIO_GPU_SET_SCANOUT Request
;
747 // Unlike for most other commands, ResourceId=0 is valid; it
748 // is used to disable a scanout.
750 Request
.Rectangle
.X
= X
;
751 Request
.Rectangle
.Y
= Y
;
752 Request
.Rectangle
.Width
= Width
;
753 Request
.Rectangle
.Height
= Height
;
754 Request
.ScanoutId
= ScanoutId
;
755 Request
.ResourceId
= ResourceId
;
757 return VirtioGpuSendCommand (
759 VirtioGpuCmdSetScanout
,
767 VirtioGpuTransferToHost2d (
768 IN OUT VGPU_DEV
*VgpuDev
,
777 volatile VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D Request
;
779 if (ResourceId
== 0) {
780 return EFI_INVALID_PARAMETER
;
783 Request
.Rectangle
.X
= X
;
784 Request
.Rectangle
.Y
= Y
;
785 Request
.Rectangle
.Width
= Width
;
786 Request
.Rectangle
.Height
= Height
;
787 Request
.Offset
= Offset
;
788 Request
.ResourceId
= ResourceId
;
791 return VirtioGpuSendCommand (
793 VirtioGpuCmdTransferToHost2d
,
801 VirtioGpuResourceFlush (
802 IN OUT VGPU_DEV
*VgpuDev
,
810 volatile VIRTIO_GPU_RESOURCE_FLUSH Request
;
812 if (ResourceId
== 0) {
813 return EFI_INVALID_PARAMETER
;
816 Request
.Rectangle
.X
= X
;
817 Request
.Rectangle
.Y
= Y
;
818 Request
.Rectangle
.Width
= Width
;
819 Request
.Rectangle
.Height
= Height
;
820 Request
.ResourceId
= ResourceId
;
823 return VirtioGpuSendCommand (
825 VirtioGpuCmdResourceFlush
,
833 VirtioGpuGetDisplayInfo (
834 IN OUT VGPU_DEV
*VgpuDev
,
835 volatile VIRTIO_GPU_RESP_DISPLAY_INFO
*Response
838 volatile VIRTIO_GPU_CONTROL_HEADER Request
;
840 return VirtioGpuSendCommandWithReply (
842 VirtioGpuCmdGetDisplayInfo
,
846 VirtioGpuRespOkDisplayInfo
,