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.
356 DEBUG ((DEBUG_VERBOSE
, "%a: Context=0x%p\n", __FUNCTION__
, Context
));
358 VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, 0);
362 Internal utility function that sends a request to the VirtIo GPU device
363 model, awaits the answer from the host, and returns a status.
365 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
366 device. The caller is responsible to have
367 successfully invoked VirtioGpuInit() on VgpuDev
368 previously, while VirtioGpuUninit() must not have
369 been called on VgpuDev.
371 @param[in] RequestType The type of the request. The caller is responsible
372 for providing a VirtioGpuCmd* RequestType which, on
373 success, elicits a VirtioGpuRespOkNodata response
376 @param[in] Fence Whether to enable fencing for this request. Fencing
377 forces the host to complete the command before
378 producing a response. If Fence is TRUE, then
379 VgpuDev->FenceId is consumed, and incremented.
381 @param[in,out] Header Pointer to the caller-allocated request object. The
382 request must start with VIRTIO_GPU_CONTROL_HEADER.
383 This function overwrites all fields of Header before
384 submitting the request to the host:
386 - it sets Type from RequestType,
388 - it sets Flags and FenceId based on Fence,
390 - it zeroes CtxId and Padding.
392 @param[in] RequestSize Size of the entire caller-allocated request object,
393 including the leading VIRTIO_GPU_CONTROL_HEADER.
395 @retval EFI_SUCCESS Operation successful.
397 @retval EFI_DEVICE_ERROR The host rejected the request. The host error
398 code has been logged on the EFI_D_ERROR level.
400 @return Codes for unexpected errors in VirtIo
401 messaging, or request/response
406 VirtioGpuSendCommand (
407 IN OUT VGPU_DEV
*VgpuDev
,
408 IN VIRTIO_GPU_CONTROL_TYPE RequestType
,
410 IN OUT
volatile VIRTIO_GPU_CONTROL_HEADER
*Header
,
414 DESC_INDICES Indices
;
415 volatile VIRTIO_GPU_CONTROL_HEADER Response
;
418 EFI_PHYSICAL_ADDRESS RequestDeviceAddress
;
420 EFI_PHYSICAL_ADDRESS ResponseDeviceAddress
;
424 // Initialize Header.
426 Header
->Type
= RequestType
;
428 Header
->Flags
= VIRTIO_GPU_FLAG_FENCE
;
429 Header
->FenceId
= VgpuDev
->FenceId
++;
437 ASSERT (RequestSize
>= sizeof *Header
);
438 ASSERT (RequestSize
<= MAX_UINT32
);
441 // Map request and response to bus master device addresses.
443 Status
= VirtioMapAllBytesInSharedBuffer (
445 VirtioOperationBusMasterRead
,
448 &RequestDeviceAddress
,
451 if (EFI_ERROR (Status
)) {
454 Status
= VirtioMapAllBytesInSharedBuffer (
456 VirtioOperationBusMasterWrite
,
459 &ResponseDeviceAddress
,
462 if (EFI_ERROR (Status
)) {
467 // Compose the descriptor chain.
469 VirtioPrepare (&VgpuDev
->Ring
, &Indices
);
472 RequestDeviceAddress
,
479 ResponseDeviceAddress
,
480 (UINT32
)sizeof Response
,
488 Status
= VirtioFlush (VgpuDev
->VirtIo
, VIRTIO_GPU_CONTROL_QUEUE
,
489 &VgpuDev
->Ring
, &Indices
, &ResponseSize
);
490 if (EFI_ERROR (Status
)) {
495 // Verify response size.
497 if (ResponseSize
!= sizeof Response
) {
498 DEBUG ((EFI_D_ERROR
, "%a: malformed response to Request=0x%x\n",
499 __FUNCTION__
, (UINT32
)RequestType
));
500 Status
= EFI_PROTOCOL_ERROR
;
505 // Unmap response and request, in reverse order of mapping. On error, the
506 // respective mapping is invalidated anyway, only the data may not have been
507 // committed to system memory (in case of VirtioOperationBusMasterWrite).
509 Status
= VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, ResponseMap
);
510 if (EFI_ERROR (Status
)) {
513 Status
= VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, RequestMap
);
514 if (EFI_ERROR (Status
)) {
519 // Parse the response.
521 if (Response
.Type
== VirtioGpuRespOkNodata
) {
525 DEBUG ((EFI_D_ERROR
, "%a: Request=0x%x Response=0x%x\n", __FUNCTION__
,
526 (UINT32
)RequestType
, Response
.Type
));
527 return EFI_DEVICE_ERROR
;
530 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, ResponseMap
);
533 VgpuDev
->VirtIo
->UnmapSharedBuffer (VgpuDev
->VirtIo
, RequestMap
);
539 The following functions send requests to the VirtIo GPU device model, await
540 the answer from the host, and return a status. They share the following
543 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
544 device. The caller is responsible to have
545 successfully invoked VirtioGpuInit() on VgpuDev
546 previously, while VirtioGpuUninit() must not have
547 been called on VgpuDev.
549 @retval EFI_INVALID_PARAMETER Invalid command-specific parameters were
550 detected by this driver.
552 @retval EFI_SUCCESS Operation successful.
554 @retval EFI_DEVICE_ERROR The host rejected the request. The host error
555 code has been logged on the EFI_D_ERROR level.
557 @return Codes for unexpected errors in VirtIo
560 For the command-specific parameters, please consult the GPU Device section of
561 the VirtIo 1.0 specification (see references in
562 "OvmfPkg/Include/IndustryStandard/VirtioGpu.h").
565 VirtioGpuResourceCreate2d (
566 IN OUT VGPU_DEV
*VgpuDev
,
567 IN UINT32 ResourceId
,
568 IN VIRTIO_GPU_FORMATS Format
,
573 volatile VIRTIO_GPU_RESOURCE_CREATE_2D Request
;
575 if (ResourceId
== 0) {
576 return EFI_INVALID_PARAMETER
;
579 Request
.ResourceId
= ResourceId
;
580 Request
.Format
= (UINT32
)Format
;
581 Request
.Width
= Width
;
582 Request
.Height
= Height
;
584 return VirtioGpuSendCommand (
586 VirtioGpuCmdResourceCreate2d
,
594 VirtioGpuResourceUnref (
595 IN OUT VGPU_DEV
*VgpuDev
,
599 volatile VIRTIO_GPU_RESOURCE_UNREF Request
;
601 if (ResourceId
== 0) {
602 return EFI_INVALID_PARAMETER
;
605 Request
.ResourceId
= ResourceId
;
608 return VirtioGpuSendCommand (
610 VirtioGpuCmdResourceUnref
,
618 VirtioGpuResourceAttachBacking (
619 IN OUT VGPU_DEV
*VgpuDev
,
620 IN UINT32 ResourceId
,
621 IN EFI_PHYSICAL_ADDRESS BackingStoreDeviceAddress
,
622 IN UINTN NumberOfPages
625 volatile VIRTIO_GPU_RESOURCE_ATTACH_BACKING Request
;
627 if (ResourceId
== 0) {
628 return EFI_INVALID_PARAMETER
;
631 Request
.ResourceId
= ResourceId
;
632 Request
.NrEntries
= 1;
633 Request
.Entry
.Addr
= BackingStoreDeviceAddress
;
634 Request
.Entry
.Length
= (UINT32
)EFI_PAGES_TO_SIZE (NumberOfPages
);
635 Request
.Entry
.Padding
= 0;
637 return VirtioGpuSendCommand (
639 VirtioGpuCmdResourceAttachBacking
,
647 VirtioGpuResourceDetachBacking (
648 IN OUT VGPU_DEV
*VgpuDev
,
652 volatile VIRTIO_GPU_RESOURCE_DETACH_BACKING Request
;
654 if (ResourceId
== 0) {
655 return EFI_INVALID_PARAMETER
;
658 Request
.ResourceId
= ResourceId
;
662 // In this case, we set Fence to TRUE, because after this function returns,
663 // the caller might reasonably want to repurpose the backing pages
664 // immediately. Thus we should ensure that the host releases all references
665 // to the backing pages before we return.
667 return VirtioGpuSendCommand (
669 VirtioGpuCmdResourceDetachBacking
,
677 VirtioGpuSetScanout (
678 IN OUT VGPU_DEV
*VgpuDev
,
687 volatile VIRTIO_GPU_SET_SCANOUT Request
;
690 // Unlike for most other commands, ResourceId=0 is valid; it
691 // is used to disable a scanout.
693 Request
.Rectangle
.X
= X
;
694 Request
.Rectangle
.Y
= Y
;
695 Request
.Rectangle
.Width
= Width
;
696 Request
.Rectangle
.Height
= Height
;
697 Request
.ScanoutId
= ScanoutId
;
698 Request
.ResourceId
= ResourceId
;
700 return VirtioGpuSendCommand (
702 VirtioGpuCmdSetScanout
,
710 VirtioGpuTransferToHost2d (
711 IN OUT VGPU_DEV
*VgpuDev
,
720 volatile VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D Request
;
722 if (ResourceId
== 0) {
723 return EFI_INVALID_PARAMETER
;
726 Request
.Rectangle
.X
= X
;
727 Request
.Rectangle
.Y
= Y
;
728 Request
.Rectangle
.Width
= Width
;
729 Request
.Rectangle
.Height
= Height
;
730 Request
.Offset
= Offset
;
731 Request
.ResourceId
= ResourceId
;
734 return VirtioGpuSendCommand (
736 VirtioGpuCmdTransferToHost2d
,
744 VirtioGpuResourceFlush (
745 IN OUT VGPU_DEV
*VgpuDev
,
753 volatile VIRTIO_GPU_RESOURCE_FLUSH Request
;
755 if (ResourceId
== 0) {
756 return EFI_INVALID_PARAMETER
;
759 Request
.Rectangle
.X
= X
;
760 Request
.Rectangle
.Y
= Y
;
761 Request
.Rectangle
.Width
= Width
;
762 Request
.Rectangle
.Height
= Height
;
763 Request
.ResourceId
= ResourceId
;
766 return VirtioGpuSendCommand (
768 VirtioGpuCmdResourceFlush
,