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
49 // Execute virtio-v1.0-cs04, 3.1.1 Driver Requirements: Device
52 // 1. Reset the device.
55 Status
= VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
56 if (EFI_ERROR (Status
)) {
61 // 2. Set the ACKNOWLEDGE status bit [...]
63 NextDevStat
|= VSTAT_ACK
;
64 Status
= VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
65 if (EFI_ERROR (Status
)) {
70 // 3. Set the DRIVER status bit [...]
72 NextDevStat
|= VSTAT_DRIVER
;
73 Status
= VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
74 if (EFI_ERROR (Status
)) {
79 // 4. Read device feature bits...
81 Status
= VgpuDev
->VirtIo
->GetDeviceFeatures (VgpuDev
->VirtIo
, &Features
);
82 if (EFI_ERROR (Status
)) {
85 if ((Features
& VIRTIO_F_VERSION_1
) == 0) {
86 Status
= EFI_UNSUPPORTED
;
90 // We only want the most basic 2D features.
92 Features
&= VIRTIO_F_VERSION_1
;
95 // ... and write the subset of feature bits understood by the [...] driver to
97 // 5. Set the FEATURES_OK status bit.
98 // 6. Re-read device status to ensure the FEATURES_OK bit is still set [...]
100 Status
= Virtio10WriteFeatures (VgpuDev
->VirtIo
, Features
, &NextDevStat
);
101 if (EFI_ERROR (Status
)) {
106 // 7. Perform device-specific setup, including discovery of virtqueues for
109 Status
= VgpuDev
->VirtIo
->SetQueueSel (VgpuDev
->VirtIo
,
110 VIRTIO_GPU_CONTROL_QUEUE
);
111 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
)) {
135 Status
= VgpuDev
->VirtIo
->SetQueueAddress (
140 if (EFI_ERROR (Status
)) {
145 // 8. Set the DRIVER_OK status bit.
147 NextDevStat
|= VSTAT_DRIVER_OK
;
148 Status
= VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
149 if (EFI_ERROR (Status
)) {
156 VirtioRingUninit (VgpuDev
->VirtIo
, &VgpuDev
->Ring
);
160 // If any of these steps go irrecoverably wrong, the driver SHOULD set the
161 // FAILED status bit to indicate that it has given up on the device (it can
162 // reset the device later to restart if desired). [...]
164 // VirtIo access failure here should not mask the original error.
166 NextDevStat
|= VSTAT_FAILED
;
167 VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, NextDevStat
);
173 De-configure the VirtIo GPU device that underlies VgpuDev.
175 @param[in,out] VgpuDev The VGPU_DEV object to tear down VirtIo messaging
176 for. On input, the caller is responsible for having
177 called VirtioGpuInit(). On output, VgpuDev->Ring has
178 been uninitialized; VirtIo GPU commands (primitives)
179 can no longer be submitted to the device.
183 IN OUT VGPU_DEV
*VgpuDev
187 // Resetting the VirtIo device makes it release its resources and forget its
190 VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, 0);
191 VirtioRingUninit (VgpuDev
->VirtIo
, &VgpuDev
->Ring
);
195 EFI_EVENT_NOTIFY function for the VGPU_DEV.ExitBoot event. It resets the
196 VirtIo device, causing it to release its resources and to forget its
199 This function may only be called (that is, VGPU_DEV.ExitBoot may only be
200 signaled) after VirtioGpuInit() returns and before VirtioGpuUninit() is
203 @param[in] Event Event whose notification function is being invoked.
205 @param[in] Context Pointer to the associated VGPU_DEV object.
217 VgpuDev
->VirtIo
->SetDeviceStatus (VgpuDev
->VirtIo
, 0);
221 Internal utility function that sends a request to the VirtIo GPU device
222 model, awaits the answer from the host, and returns a status.
224 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
225 device. The caller is responsible to have
226 successfully invoked VirtioGpuInit() on VgpuDev
227 previously, while VirtioGpuUninit() must not have
228 been called on VgpuDev.
230 @param[in] RequestType The type of the request. The caller is responsible
231 for providing a VirtioGpuCmd* RequestType which, on
232 success, elicits a VirtioGpuRespOkNodata response
235 @param[in] Fence Whether to enable fencing for this request. Fencing
236 forces the host to complete the command before
237 producing a response. If Fence is TRUE, then
238 VgpuDev->FenceId is consumed, and incremented.
240 @param[in,out] Header Pointer to the caller-allocated request object. The
241 request must start with VIRTIO_GPU_CONTROL_HEADER.
242 This function overwrites all fields of Header before
243 submitting the request to the host:
245 - it sets Type from RequestType,
247 - it sets Flags and FenceId based on Fence,
249 - it zeroes CtxId and Padding.
251 @param[in] RequestSize Size of the entire caller-allocated request object,
252 including the leading VIRTIO_GPU_CONTROL_HEADER.
254 @retval EFI_SUCCESS Operation successful.
256 @retval EFI_DEVICE_ERROR The host rejected the request. The host error
257 code has been logged on the EFI_D_ERROR level.
259 @return Codes for unexpected errors in VirtIo
264 VirtioGpuSendCommand (
265 IN OUT VGPU_DEV
*VgpuDev
,
266 IN VIRTIO_GPU_CONTROL_TYPE RequestType
,
268 IN OUT
volatile VIRTIO_GPU_CONTROL_HEADER
*Header
,
272 DESC_INDICES Indices
;
273 volatile VIRTIO_GPU_CONTROL_HEADER Response
;
278 // Initialize Header.
280 Header
->Type
= RequestType
;
282 Header
->Flags
= VIRTIO_GPU_FLAG_FENCE
;
283 Header
->FenceId
= VgpuDev
->FenceId
++;
291 ASSERT (RequestSize
>= sizeof *Header
);
292 ASSERT (RequestSize
<= MAX_UINT32
);
295 // Compose the descriptor chain.
297 VirtioPrepare (&VgpuDev
->Ring
, &Indices
);
298 VirtioAppendDesc (&VgpuDev
->Ring
, (UINTN
)Header
, (UINT32
)RequestSize
,
299 VRING_DESC_F_NEXT
, &Indices
);
300 VirtioAppendDesc (&VgpuDev
->Ring
, (UINTN
)&Response
, sizeof Response
,
301 VRING_DESC_F_WRITE
, &Indices
);
306 Status
= VirtioFlush (VgpuDev
->VirtIo
, VIRTIO_GPU_CONTROL_QUEUE
,
307 &VgpuDev
->Ring
, &Indices
, &ResponseSize
);
308 if (EFI_ERROR (Status
)) {
313 // Parse the response.
315 if (ResponseSize
!= sizeof Response
) {
316 DEBUG ((EFI_D_ERROR
, "%a: malformed response to Request=0x%x\n",
317 __FUNCTION__
, (UINT32
)RequestType
));
318 return EFI_PROTOCOL_ERROR
;
321 if (Response
.Type
== VirtioGpuRespOkNodata
) {
325 DEBUG ((EFI_D_ERROR
, "%a: Request=0x%x Response=0x%x\n", __FUNCTION__
,
326 (UINT32
)RequestType
, Response
.Type
));
327 return EFI_DEVICE_ERROR
;
331 The following functions send requests to the VirtIo GPU device model, await
332 the answer from the host, and return a status. They share the following
335 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
336 device. The caller is responsible to have
337 successfully invoked VirtioGpuInit() on VgpuDev
338 previously, while VirtioGpuUninit() must not have
339 been called on VgpuDev.
341 @retval EFI_INVALID_PARAMETER Invalid command-specific parameters were
342 detected by this driver.
344 @retval EFI_SUCCESS Operation successful.
346 @retval EFI_DEVICE_ERROR The host rejected the request. The host error
347 code has been logged on the EFI_D_ERROR level.
349 @return Codes for unexpected errors in VirtIo
352 For the command-specific parameters, please consult the GPU Device section of
353 the VirtIo 1.0 specification (see references in
354 "OvmfPkg/Include/IndustryStandard/VirtioGpu.h").
357 VirtioGpuResourceCreate2d (
358 IN OUT VGPU_DEV
*VgpuDev
,
359 IN UINT32 ResourceId
,
360 IN VIRTIO_GPU_FORMATS Format
,
365 volatile VIRTIO_GPU_RESOURCE_CREATE_2D Request
;
367 if (ResourceId
== 0) {
368 return EFI_INVALID_PARAMETER
;
371 Request
.ResourceId
= ResourceId
;
372 Request
.Format
= (UINT32
)Format
;
373 Request
.Width
= Width
;
374 Request
.Height
= Height
;
376 return VirtioGpuSendCommand (
378 VirtioGpuCmdResourceCreate2d
,
386 VirtioGpuResourceUnref (
387 IN OUT VGPU_DEV
*VgpuDev
,
391 volatile VIRTIO_GPU_RESOURCE_UNREF Request
;
393 if (ResourceId
== 0) {
394 return EFI_INVALID_PARAMETER
;
397 Request
.ResourceId
= ResourceId
;
400 return VirtioGpuSendCommand (
402 VirtioGpuCmdResourceUnref
,
410 VirtioGpuResourceAttachBacking (
411 IN OUT VGPU_DEV
*VgpuDev
,
412 IN UINT32 ResourceId
,
413 IN VOID
*FirstBackingPage
,
414 IN UINTN NumberOfPages
417 volatile VIRTIO_GPU_RESOURCE_ATTACH_BACKING Request
;
419 if (ResourceId
== 0) {
420 return EFI_INVALID_PARAMETER
;
423 Request
.ResourceId
= ResourceId
;
424 Request
.NrEntries
= 1;
425 Request
.Entry
.Addr
= (UINTN
)FirstBackingPage
;
426 Request
.Entry
.Length
= (UINT32
)EFI_PAGES_TO_SIZE (NumberOfPages
);
427 Request
.Entry
.Padding
= 0;
429 return VirtioGpuSendCommand (
431 VirtioGpuCmdResourceAttachBacking
,
439 VirtioGpuResourceDetachBacking (
440 IN OUT VGPU_DEV
*VgpuDev
,
444 volatile VIRTIO_GPU_RESOURCE_DETACH_BACKING Request
;
446 if (ResourceId
== 0) {
447 return EFI_INVALID_PARAMETER
;
450 Request
.ResourceId
= ResourceId
;
454 // In this case, we set Fence to TRUE, because after this function returns,
455 // the caller might reasonably want to repurpose the backing pages
456 // immediately. Thus we should ensure that the host releases all references
457 // to the backing pages before we return.
459 return VirtioGpuSendCommand (
461 VirtioGpuCmdResourceDetachBacking
,
469 VirtioGpuSetScanout (
470 IN OUT VGPU_DEV
*VgpuDev
,
479 volatile VIRTIO_GPU_SET_SCANOUT Request
;
482 // Unlike for most other commands, ResourceId=0 is valid; it
483 // is used to disable a scanout.
485 Request
.Rectangle
.X
= X
;
486 Request
.Rectangle
.Y
= Y
;
487 Request
.Rectangle
.Width
= Width
;
488 Request
.Rectangle
.Height
= Height
;
489 Request
.ScanoutId
= ScanoutId
;
490 Request
.ResourceId
= ResourceId
;
492 return VirtioGpuSendCommand (
494 VirtioGpuCmdSetScanout
,
502 VirtioGpuTransferToHost2d (
503 IN OUT VGPU_DEV
*VgpuDev
,
512 volatile VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D Request
;
514 if (ResourceId
== 0) {
515 return EFI_INVALID_PARAMETER
;
518 Request
.Rectangle
.X
= X
;
519 Request
.Rectangle
.Y
= Y
;
520 Request
.Rectangle
.Width
= Width
;
521 Request
.Rectangle
.Height
= Height
;
522 Request
.Offset
= Offset
;
523 Request
.ResourceId
= ResourceId
;
526 return VirtioGpuSendCommand (
528 VirtioGpuCmdTransferToHost2d
,
536 VirtioGpuResourceFlush (
537 IN OUT VGPU_DEV
*VgpuDev
,
545 volatile VIRTIO_GPU_RESOURCE_FLUSH Request
;
547 if (ResourceId
== 0) {
548 return EFI_INVALID_PARAMETER
;
551 Request
.Rectangle
.X
= X
;
552 Request
.Rectangle
.Y
= Y
;
553 Request
.Rectangle
.Width
= Width
;
554 Request
.Rectangle
.Height
= Height
;
555 Request
.ResourceId
= ResourceId
;
558 return VirtioGpuSendCommand (
560 VirtioGpuCmdResourceFlush
,