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 This program and the accompanying materials are licensed and made available
9 under the terms and conditions of the BSD License which accompanies this
10 distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
14 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <Library/VirtioLib.h>
20 #include "VirtioGpu.h"
23 Configure the VirtIo GPU device that underlies VgpuDev.
25 @param[in,out] VgpuDev The VGPU_DEV object to set up VirtIo messaging for.
26 On input, the caller is responsible for having
27 initialized VgpuDev->VirtIo. On output, VgpuDev->Ring
28 has been initialized, and synchronous VirtIo GPU
29 commands (primitives) can be submitted to the device.
31 @retval EFI_SUCCESS VirtIo GPU configuration successful.
33 @retval EFI_UNSUPPORTED The host-side configuration of the VirtIo GPU is not
34 supported by this driver.
36 @retval Error codes from underlying functions.
40 IN OUT VGPU_DEV
*VgpuDev
50 // Execute virtio-v1.0-cs04, 3.1.1 Driver Requirements: Device
53 // 1. Reset the device.
56 Status
= VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
57 if (EFI_ERROR (Status
)) {
62 // 2. Set the ACKNOWLEDGE status bit [...]
64 NextDevStat
|= VSTAT_ACK
;
65 Status
= VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
66 if (EFI_ERROR (Status
)) {
71 // 3. Set the DRIVER status bit [...]
73 NextDevStat
|= VSTAT_DRIVER
;
74 Status
= VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
75 if (EFI_ERROR (Status
)) {
80 // 4. Read device feature bits...
82 Status
= VgpuDev
->VirtIo
->GetDeviceFeatures (VgpuDev
->VirtIo
, &Features
);
83 if (EFI_ERROR (Status
)) {
86 if ((Features
& VIRTIO_F_VERSION_1
) == 0) {
87 Status
= EFI_UNSUPPORTED
;
91 // We only want the most basic 2D features.
93 Features
&= VIRTIO_F_VERSION_1
| VIRTIO_F_IOMMU_PLATFORM
;
96 // ... and write the subset of feature bits understood by the [...] driver to
98 // 5. Set the FEATURES_OK status bit.
99 // 6. Re-read device status to ensure the FEATURES_OK bit is still set [...]
101 Status
= Virtio10WriteFeatures (VgpuDev
->VirtIo
, Features
, &NextDevStat
);
102 if (EFI_ERROR (Status
)) {
107 // 7. Perform device-specific setup, including discovery of virtqueues for
110 Status
= VgpuDev
->VirtIo
->SetQueueSel (VgpuDev
->VirtIo
,
111 VIRTIO_GPU_CONTROL_QUEUE
);
112 if (EFI_ERROR (Status
)) {
115 Status
= VgpuDev
->VirtIo
->GetQueueNumMax (VgpuDev
->VirtIo
, &QueueSize
);
116 if (EFI_ERROR (Status
)) {
121 // We implement each VirtIo GPU command that we use with two descriptors:
122 // request, response.
125 Status
= EFI_UNSUPPORTED
;
130 // [...] population of virtqueues [...]
132 Status
= VirtioRingInit (VgpuDev
->VirtIo
, QueueSize
, &VgpuDev
->Ring
);
133 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
)) {
149 // If anything fails from here on, we have to unmap the ring.
151 Status
= VgpuDev
->VirtIo
->SetQueueAddress (
156 if (EFI_ERROR (Status
)) {
161 // 8. Set the DRIVER_OK status bit.
163 NextDevStat
|= VSTAT_DRIVER_OK
;
164 Status
= VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
165 if (EFI_ERROR (Status
)) {
172 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, VgpuDev
->RingMap
);
175 VirtioRingUninit (VgpuDev
->VirtIo
, &VgpuDev
->Ring
);
179 // If any of these steps go irrecoverably wrong, the driver SHOULD set the
180 // FAILED status bit to indicate that it has given up on the device (it can
181 // reset the device later to restart if desired). [...]
183 // VirtIo access failure here should not mask the original error.
185 NextDevStat
|= VSTAT_FAILED
;
186 VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
192 De-configure the VirtIo GPU device that underlies VgpuDev.
194 @param[in,out] VgpuDev The VGPU_DEV object to tear down VirtIo messaging
195 for. On input, the caller is responsible for having
196 called VirtioGpuInit(). On output, VgpuDev->Ring has
197 been uninitialized; VirtIo GPU commands (primitives)
198 can no longer be submitted to the device.
202 IN OUT VGPU_DEV
*VgpuDev
206 // Resetting the VirtIo device makes it release its resources and forget its
209 VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, 0);
210 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, VgpuDev
->RingMap
);
211 VirtioRingUninit (VgpuDev
->VirtIo
, &VgpuDev
->Ring
);
215 Allocate, zero and map memory, for bus master common buffer operation, to be
216 attached as backing store to a host-side VirtIo GPU resource.
218 @param[in] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
221 @param[in] NumberOfPages The number of whole pages to allocate and map.
223 @param[out] HostAddress The system memory address of the allocated area.
225 @param[out] DeviceAddress The bus master device address of the allocated
226 area. The VirtIo GPU device may be programmed to
227 access the allocated area through DeviceAddress;
228 DeviceAddress is to be passed to the
229 VirtioGpuResourceAttachBacking() function, as the
230 BackingStoreDeviceAddress parameter.
232 @param[out] Mapping A resulting token to pass to
233 VirtioGpuUnmapAndFreeBackingStore().
235 @retval EFI_SUCCESS The requested number of pages has been allocated, zeroed
238 @return Status codes propagated from
239 VgpuDev->VirtIo->AllocateSharedPages() and
240 VirtioMapAllBytesInSharedBuffer().
243 VirtioGpuAllocateZeroAndMapBackingStore (
244 IN VGPU_DEV
*VgpuDev
,
245 IN UINTN NumberOfPages
,
246 OUT VOID
**HostAddress
,
247 OUT EFI_PHYSICAL_ADDRESS
*DeviceAddress
,
252 VOID
*NewHostAddress
;
254 Status
= VgpuDev
->VirtIo
->AllocateSharedPages (
259 if (EFI_ERROR (Status
)) {
264 // Avoid exposing stale data to the device even temporarily: zero the area
265 // before mapping it.
267 ZeroMem (NewHostAddress
, EFI_PAGES_TO_SIZE (NumberOfPages
));
269 Status
= VirtioMapAllBytesInSharedBuffer (
270 VgpuDev
->VirtIo
, // VirtIo
271 VirtioOperationBusMasterCommonBuffer
, // Operation
272 NewHostAddress
, // HostAddress
273 EFI_PAGES_TO_SIZE (NumberOfPages
), // NumberOfBytes
274 DeviceAddress
, // DeviceAddress
277 if (EFI_ERROR (Status
)) {
278 goto FreeSharedPages
;
281 *HostAddress
= NewHostAddress
;
285 VgpuDev
->VirtIo
->FreeSharedPages (
294 Unmap and free memory originally allocated and mapped with
295 VirtioGpuAllocateZeroAndMapBackingStore().
297 If the memory allocated and mapped with
298 VirtioGpuAllocateZeroAndMapBackingStore() was attached to a host-side VirtIo
299 GPU resource with VirtioGpuResourceAttachBacking(), then the caller is
300 responsible for detaching the backing store from the same resource, with
301 VirtioGpuResourceDetachBacking(), before calling this function.
303 @param[in] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
306 @param[in] NumberOfPages The NumberOfPages parameter originally passed to
307 VirtioGpuAllocateZeroAndMapBackingStore().
309 @param[in] HostAddress The HostAddress value originally output by
310 VirtioGpuAllocateZeroAndMapBackingStore().
312 @param[in] Mapping The token that was originally output by
313 VirtioGpuAllocateZeroAndMapBackingStore().
316 VirtioGpuUnmapAndFreeBackingStore (
317 IN VGPU_DEV
*VgpuDev
,
318 IN UINTN NumberOfPages
,
319 IN VOID
*HostAddress
,
323 VgpuDev
->VirtIo
->UnmapSharedBuffer (
327 VgpuDev
->VirtIo
->FreeSharedPages (
335 EFI_EVENT_NOTIFY function for the VGPU_DEV.ExitBoot event. It resets the
336 VirtIo device, causing it to release its resources and to forget its
339 This function may only be called (that is, VGPU_DEV.ExitBoot may only be
340 signaled) after VirtioGpuInit() returns and before VirtioGpuUninit() is
343 @param[in] Event Event whose notification function is being invoked.
345 @param[in] Context Pointer to the associated VGPU_DEV object.
357 VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, 0);
360 // If VirtioGpuDriverBindingStart() and VirtioGpuDriverBindingStop() have
361 // been called thus far in such a sequence that right now our (sole) child
362 // handle exists -- with the GOP on it standing for head (scanout) #0 --,
363 // then we have to unmap the current video mode's backing store.
365 if (VgpuDev
->Child
!= NULL
) {
367 // The current video mode is guaranteed to have a valid and mapped backing
368 // store, due to the first Gop.SetMode() call, made internally in
371 ASSERT (VgpuDev
->Child
->BackingStore
!= NULL
);
373 VgpuDev
->VirtIo
->UnmapSharedBuffer (
375 VgpuDev
->Child
->BackingStoreMap
379 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, VgpuDev
->RingMap
);
383 Internal utility function that sends a request to the VirtIo GPU device
384 model, awaits the answer from the host, and returns a status.
386 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
387 device. The caller is responsible to have
388 successfully invoked VirtioGpuInit() on VgpuDev
389 previously, while VirtioGpuUninit() must not have
390 been called on VgpuDev.
392 @param[in] RequestType The type of the request. The caller is responsible
393 for providing a VirtioGpuCmd* RequestType which, on
394 success, elicits a VirtioGpuRespOkNodata response
397 @param[in] Fence Whether to enable fencing for this request. Fencing
398 forces the host to complete the command before
399 producing a response. If Fence is TRUE, then
400 VgpuDev->FenceId is consumed, and incremented.
402 @param[in,out] Header Pointer to the caller-allocated request object. The
403 request must start with VIRTIO_GPU_CONTROL_HEADER.
404 This function overwrites all fields of Header before
405 submitting the request to the host:
407 - it sets Type from RequestType,
409 - it sets Flags and FenceId based on Fence,
411 - it zeroes CtxId and Padding.
413 @param[in] RequestSize Size of the entire caller-allocated request object,
414 including the leading VIRTIO_GPU_CONTROL_HEADER.
416 @retval EFI_SUCCESS Operation successful.
418 @retval EFI_DEVICE_ERROR The host rejected the request. The host error
419 code has been logged on the EFI_D_ERROR level.
421 @return Codes for unexpected errors in VirtIo
422 messaging, or request/response
427 VirtioGpuSendCommand (
428 IN OUT VGPU_DEV
*VgpuDev
,
429 IN VIRTIO_GPU_CONTROL_TYPE RequestType
,
431 IN OUT
volatile VIRTIO_GPU_CONTROL_HEADER
*Header
,
435 DESC_INDICES Indices
;
436 volatile VIRTIO_GPU_CONTROL_HEADER Response
;
439 EFI_PHYSICAL_ADDRESS RequestDeviceAddress
;
441 EFI_PHYSICAL_ADDRESS ResponseDeviceAddress
;
445 // Initialize Header.
447 Header
->Type
= RequestType
;
449 Header
->Flags
= VIRTIO_GPU_FLAG_FENCE
;
450 Header
->FenceId
= VgpuDev
->FenceId
++;
458 ASSERT (RequestSize
>= sizeof *Header
);
459 ASSERT (RequestSize
<= MAX_UINT32
);
462 // Map request and response to bus master device addresses.
464 Status
= VirtioMapAllBytesInSharedBuffer (
466 VirtioOperationBusMasterRead
,
469 &RequestDeviceAddress
,
472 if (EFI_ERROR (Status
)) {
475 Status
= VirtioMapAllBytesInSharedBuffer (
477 VirtioOperationBusMasterWrite
,
480 &ResponseDeviceAddress
,
483 if (EFI_ERROR (Status
)) {
488 // Compose the descriptor chain.
490 VirtioPrepare (&VgpuDev
->Ring
, &Indices
);
493 RequestDeviceAddress
,
500 ResponseDeviceAddress
,
501 (UINT32
)sizeof Response
,
509 Status
= VirtioFlush (VgpuDev
->VirtIo
, VIRTIO_GPU_CONTROL_QUEUE
,
510 &VgpuDev
->Ring
, &Indices
, &ResponseSize
);
511 if (EFI_ERROR (Status
)) {
516 // Verify response size.
518 if (ResponseSize
!= sizeof Response
) {
519 DEBUG ((EFI_D_ERROR
, "%a: malformed response to Request=0x%x\n",
520 __FUNCTION__
, (UINT32
)RequestType
));
521 Status
= EFI_PROTOCOL_ERROR
;
526 // Unmap response and request, in reverse order of mapping. On error, the
527 // respective mapping is invalidated anyway, only the data may not have been
528 // committed to system memory (in case of VirtioOperationBusMasterWrite).
530 Status
= VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, ResponseMap
);
531 if (EFI_ERROR (Status
)) {
534 Status
= VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, RequestMap
);
535 if (EFI_ERROR (Status
)) {
540 // Parse the response.
542 if (Response
.Type
== VirtioGpuRespOkNodata
) {
546 DEBUG ((EFI_D_ERROR
, "%a: Request=0x%x Response=0x%x\n", __FUNCTION__
,
547 (UINT32
)RequestType
, Response
.Type
));
548 return EFI_DEVICE_ERROR
;
551 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, ResponseMap
);
554 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, RequestMap
);
560 The following functions send requests to the VirtIo GPU device model, await
561 the answer from the host, and return a status. They share the following
564 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
565 device. The caller is responsible to have
566 successfully invoked VirtioGpuInit() on VgpuDev
567 previously, while VirtioGpuUninit() must not have
568 been called on VgpuDev.
570 @retval EFI_INVALID_PARAMETER Invalid command-specific parameters were
571 detected by this driver.
573 @retval EFI_SUCCESS Operation successful.
575 @retval EFI_DEVICE_ERROR The host rejected the request. The host error
576 code has been logged on the EFI_D_ERROR level.
578 @return Codes for unexpected errors in VirtIo
581 For the command-specific parameters, please consult the GPU Device section of
582 the VirtIo 1.0 specification (see references in
583 "OvmfPkg/Include/IndustryStandard/VirtioGpu.h").
586 VirtioGpuResourceCreate2d (
587 IN OUT VGPU_DEV
*VgpuDev
,
588 IN UINT32 ResourceId
,
589 IN VIRTIO_GPU_FORMATS Format
,
594 volatile VIRTIO_GPU_RESOURCE_CREATE_2D Request
;
596 if (ResourceId
== 0) {
597 return EFI_INVALID_PARAMETER
;
600 Request
.ResourceId
= ResourceId
;
601 Request
.Format
= (UINT32
)Format
;
602 Request
.Width
= Width
;
603 Request
.Height
= Height
;
605 return VirtioGpuSendCommand (
607 VirtioGpuCmdResourceCreate2d
,
615 VirtioGpuResourceUnref (
616 IN OUT VGPU_DEV
*VgpuDev
,
620 volatile VIRTIO_GPU_RESOURCE_UNREF Request
;
622 if (ResourceId
== 0) {
623 return EFI_INVALID_PARAMETER
;
626 Request
.ResourceId
= ResourceId
;
629 return VirtioGpuSendCommand (
631 VirtioGpuCmdResourceUnref
,
639 VirtioGpuResourceAttachBacking (
640 IN OUT VGPU_DEV
*VgpuDev
,
641 IN UINT32 ResourceId
,
642 IN EFI_PHYSICAL_ADDRESS BackingStoreDeviceAddress
,
643 IN UINTN NumberOfPages
646 volatile VIRTIO_GPU_RESOURCE_ATTACH_BACKING Request
;
648 if (ResourceId
== 0) {
649 return EFI_INVALID_PARAMETER
;
652 Request
.ResourceId
= ResourceId
;
653 Request
.NrEntries
= 1;
654 Request
.Entry
.Addr
= BackingStoreDeviceAddress
;
655 Request
.Entry
.Length
= (UINT32
)EFI_PAGES_TO_SIZE (NumberOfPages
);
656 Request
.Entry
.Padding
= 0;
658 return VirtioGpuSendCommand (
660 VirtioGpuCmdResourceAttachBacking
,
668 VirtioGpuResourceDetachBacking (
669 IN OUT VGPU_DEV
*VgpuDev
,
673 volatile VIRTIO_GPU_RESOURCE_DETACH_BACKING Request
;
675 if (ResourceId
== 0) {
676 return EFI_INVALID_PARAMETER
;
679 Request
.ResourceId
= ResourceId
;
683 // In this case, we set Fence to TRUE, because after this function returns,
684 // the caller might reasonably want to repurpose the backing pages
685 // immediately. Thus we should ensure that the host releases all references
686 // to the backing pages before we return.
688 return VirtioGpuSendCommand (
690 VirtioGpuCmdResourceDetachBacking
,
698 VirtioGpuSetScanout (
699 IN OUT VGPU_DEV
*VgpuDev
,
708 volatile VIRTIO_GPU_SET_SCANOUT Request
;
711 // Unlike for most other commands, ResourceId=0 is valid; it
712 // is used to disable a scanout.
714 Request
.Rectangle
.X
= X
;
715 Request
.Rectangle
.Y
= Y
;
716 Request
.Rectangle
.Width
= Width
;
717 Request
.Rectangle
.Height
= Height
;
718 Request
.ScanoutId
= ScanoutId
;
719 Request
.ResourceId
= ResourceId
;
721 return VirtioGpuSendCommand (
723 VirtioGpuCmdSetScanout
,
731 VirtioGpuTransferToHost2d (
732 IN OUT VGPU_DEV
*VgpuDev
,
741 volatile VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D Request
;
743 if (ResourceId
== 0) {
744 return EFI_INVALID_PARAMETER
;
747 Request
.Rectangle
.X
= X
;
748 Request
.Rectangle
.Y
= Y
;
749 Request
.Rectangle
.Width
= Width
;
750 Request
.Rectangle
.Height
= Height
;
751 Request
.Offset
= Offset
;
752 Request
.ResourceId
= ResourceId
;
755 return VirtioGpuSendCommand (
757 VirtioGpuCmdTransferToHost2d
,
765 VirtioGpuResourceFlush (
766 IN OUT VGPU_DEV
*VgpuDev
,
774 volatile VIRTIO_GPU_RESOURCE_FLUSH Request
;
776 if (ResourceId
== 0) {
777 return EFI_INVALID_PARAMETER
;
780 Request
.Rectangle
.X
= X
;
781 Request
.Rectangle
.Y
= Y
;
782 Request
.Rectangle
.Width
= Width
;
783 Request
.Rectangle
.Height
= Height
;
784 Request
.ResourceId
= ResourceId
;
787 return VirtioGpuSendCommand (
789 VirtioGpuCmdResourceFlush
,