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 @retval EFI_SUCCESS Operation successful.
398 @retval EFI_DEVICE_ERROR The host rejected the request. The host error
399 code has been logged on the DEBUG_ERROR level.
401 @return Codes for unexpected errors in VirtIo
402 messaging, or request/response
407 VirtioGpuSendCommand (
408 IN OUT VGPU_DEV
*VgpuDev
,
409 IN VIRTIO_GPU_CONTROL_TYPE RequestType
,
411 IN OUT
volatile VIRTIO_GPU_CONTROL_HEADER
*Header
,
415 DESC_INDICES Indices
;
416 volatile VIRTIO_GPU_CONTROL_HEADER Response
;
419 EFI_PHYSICAL_ADDRESS RequestDeviceAddress
;
421 EFI_PHYSICAL_ADDRESS ResponseDeviceAddress
;
425 // Initialize Header.
427 Header
->Type
= RequestType
;
429 Header
->Flags
= VIRTIO_GPU_FLAG_FENCE
;
430 Header
->FenceId
= VgpuDev
->FenceId
++;
439 ASSERT (RequestSize
>= sizeof *Header
);
440 ASSERT (RequestSize
<= MAX_UINT32
);
443 // Map request and response to bus master device addresses.
445 Status
= VirtioMapAllBytesInSharedBuffer (
447 VirtioOperationBusMasterRead
,
450 &RequestDeviceAddress
,
453 if (EFI_ERROR (Status
)) {
457 Status
= VirtioMapAllBytesInSharedBuffer (
459 VirtioOperationBusMasterWrite
,
462 &ResponseDeviceAddress
,
465 if (EFI_ERROR (Status
)) {
470 // Compose the descriptor chain.
472 VirtioPrepare (&VgpuDev
->Ring
, &Indices
);
475 RequestDeviceAddress
,
482 ResponseDeviceAddress
,
483 (UINT32
)sizeof Response
,
491 Status
= VirtioFlush (
493 VIRTIO_GPU_CONTROL_QUEUE
,
498 if (EFI_ERROR (Status
)) {
503 // Verify response size.
505 if (ResponseSize
!= sizeof Response
) {
508 "%a: malformed response to Request=0x%x\n",
512 Status
= EFI_PROTOCOL_ERROR
;
517 // Unmap response and request, in reverse order of mapping. On error, the
518 // respective mapping is invalidated anyway, only the data may not have been
519 // committed to system memory (in case of VirtioOperationBusMasterWrite).
521 Status
= VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, ResponseMap
);
522 if (EFI_ERROR (Status
)) {
526 Status
= VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, RequestMap
);
527 if (EFI_ERROR (Status
)) {
532 // Parse the response.
534 if (Response
.Type
== VirtioGpuRespOkNodata
) {
540 "%a: Request=0x%x Response=0x%x\n",
545 return EFI_DEVICE_ERROR
;
548 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, ResponseMap
);
551 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, RequestMap
);
557 The following functions send requests to the VirtIo GPU device model, await
558 the answer from the host, and return a status. They share the following
561 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
562 device. The caller is responsible to have
563 successfully invoked VirtioGpuInit() on VgpuDev
564 previously, while VirtioGpuUninit() must not have
565 been called on VgpuDev.
567 @retval EFI_INVALID_PARAMETER Invalid command-specific parameters were
568 detected by this driver.
570 @retval EFI_SUCCESS Operation successful.
572 @retval EFI_DEVICE_ERROR The host rejected the request. The host error
573 code has been logged on the DEBUG_ERROR level.
575 @return Codes for unexpected errors in VirtIo
578 For the command-specific parameters, please consult the GPU Device section of
579 the VirtIo 1.0 specification (see references in
580 "OvmfPkg/Include/IndustryStandard/VirtioGpu.h").
583 VirtioGpuResourceCreate2d (
584 IN OUT VGPU_DEV
*VgpuDev
,
585 IN UINT32 ResourceId
,
586 IN VIRTIO_GPU_FORMATS Format
,
591 volatile VIRTIO_GPU_RESOURCE_CREATE_2D Request
;
593 if (ResourceId
== 0) {
594 return EFI_INVALID_PARAMETER
;
597 Request
.ResourceId
= ResourceId
;
598 Request
.Format
= (UINT32
)Format
;
599 Request
.Width
= Width
;
600 Request
.Height
= Height
;
602 return VirtioGpuSendCommand (
604 VirtioGpuCmdResourceCreate2d
,
612 VirtioGpuResourceUnref (
613 IN OUT VGPU_DEV
*VgpuDev
,
617 volatile VIRTIO_GPU_RESOURCE_UNREF Request
;
619 if (ResourceId
== 0) {
620 return EFI_INVALID_PARAMETER
;
623 Request
.ResourceId
= ResourceId
;
626 return VirtioGpuSendCommand (
628 VirtioGpuCmdResourceUnref
,
636 VirtioGpuResourceAttachBacking (
637 IN OUT VGPU_DEV
*VgpuDev
,
638 IN UINT32 ResourceId
,
639 IN EFI_PHYSICAL_ADDRESS BackingStoreDeviceAddress
,
640 IN UINTN NumberOfPages
643 volatile VIRTIO_GPU_RESOURCE_ATTACH_BACKING Request
;
645 if (ResourceId
== 0) {
646 return EFI_INVALID_PARAMETER
;
649 Request
.ResourceId
= ResourceId
;
650 Request
.NrEntries
= 1;
651 Request
.Entry
.Addr
= BackingStoreDeviceAddress
;
652 Request
.Entry
.Length
= (UINT32
)EFI_PAGES_TO_SIZE (NumberOfPages
);
653 Request
.Entry
.Padding
= 0;
655 return VirtioGpuSendCommand (
657 VirtioGpuCmdResourceAttachBacking
,
665 VirtioGpuResourceDetachBacking (
666 IN OUT VGPU_DEV
*VgpuDev
,
670 volatile VIRTIO_GPU_RESOURCE_DETACH_BACKING Request
;
672 if (ResourceId
== 0) {
673 return EFI_INVALID_PARAMETER
;
676 Request
.ResourceId
= ResourceId
;
680 // In this case, we set Fence to TRUE, because after this function returns,
681 // the caller might reasonably want to repurpose the backing pages
682 // immediately. Thus we should ensure that the host releases all references
683 // to the backing pages before we return.
685 return VirtioGpuSendCommand (
687 VirtioGpuCmdResourceDetachBacking
,
695 VirtioGpuSetScanout (
696 IN OUT VGPU_DEV
*VgpuDev
,
705 volatile VIRTIO_GPU_SET_SCANOUT Request
;
708 // Unlike for most other commands, ResourceId=0 is valid; it
709 // is used to disable a scanout.
711 Request
.Rectangle
.X
= X
;
712 Request
.Rectangle
.Y
= Y
;
713 Request
.Rectangle
.Width
= Width
;
714 Request
.Rectangle
.Height
= Height
;
715 Request
.ScanoutId
= ScanoutId
;
716 Request
.ResourceId
= ResourceId
;
718 return VirtioGpuSendCommand (
720 VirtioGpuCmdSetScanout
,
728 VirtioGpuTransferToHost2d (
729 IN OUT VGPU_DEV
*VgpuDev
,
738 volatile VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D Request
;
740 if (ResourceId
== 0) {
741 return EFI_INVALID_PARAMETER
;
744 Request
.Rectangle
.X
= X
;
745 Request
.Rectangle
.Y
= Y
;
746 Request
.Rectangle
.Width
= Width
;
747 Request
.Rectangle
.Height
= Height
;
748 Request
.Offset
= Offset
;
749 Request
.ResourceId
= ResourceId
;
752 return VirtioGpuSendCommand (
754 VirtioGpuCmdTransferToHost2d
,
762 VirtioGpuResourceFlush (
763 IN OUT VGPU_DEV
*VgpuDev
,
771 volatile VIRTIO_GPU_RESOURCE_FLUSH Request
;
773 if (ResourceId
== 0) {
774 return EFI_INVALID_PARAMETER
;
777 Request
.Rectangle
.X
= X
;
778 Request
.Rectangle
.Y
= Y
;
779 Request
.Rectangle
.Width
= Width
;
780 Request
.Rectangle
.Height
= Height
;
781 Request
.ResourceId
= ResourceId
;
784 return VirtioGpuSendCommand (
786 VirtioGpuCmdResourceFlush
,