3 EFI_GRAPHICS_OUTPUT_PROTOCOL member functions for the VirtIo GPU driver.
5 Copyright (C) 2016, Red Hat, Inc.
7 This program and the accompanying materials are licensed and made available
8 under the terms and conditions of the BSD License which accompanies this
9 distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
13 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include <Library/MemoryAllocationLib.h>
19 #include "VirtioGpu.h"
22 Release guest-side and host-side resources that are related to an initialized
25 param[in,out] VgpuGop The VGPU_GOP object to release resources for.
27 On input, the caller is responsible for having called
28 VgpuGop->Gop.SetMode() at least once successfully.
29 (This is equivalent to the requirement that
30 VgpuGop->BackingStore be non-NULL. It is also
31 equivalent to the requirement that VgpuGop->ResourceId
34 On output, resources will be released, and
35 VgpuGop->BackingStore and VgpuGop->ResourceId will be
38 param[in] DisableHead Whether this head (scanout) currently references the
39 resource identified by VgpuGop->ResourceId. Only pass
40 FALSE when VgpuGop->Gop.SetMode() calls this function
41 while switching between modes, and set it to TRUE
46 IN OUT VGPU_GOP
*VgpuGop
,
47 IN BOOLEAN DisableHead
52 ASSERT (VgpuGop
->ResourceId
!= 0);
53 ASSERT (VgpuGop
->BackingStore
!= NULL
);
56 // If any of the following host-side destruction steps fail, we can't get out
57 // of an inconsistent state, so we'll hang. In general errors in object
58 // destruction can hardly be recovered from.
62 // Dissociate head (scanout) #0 from the currently used 2D host resource,
63 // by setting ResourceId=0 for it.
65 Status
= VirtioGpuSetScanout (
66 VgpuGop
->ParentBus
, // VgpuDev
67 0, 0, 0, 0, // X, Y, Width, Height
74 // According to the GPU Device section of the VirtIo specification, the
75 // above operation is valid:
77 // "The driver can use resource_id = 0 to disable a scanout."
79 // However, in practice QEMU does not allow us to disable head (scanout) #0
80 // -- it rejects the command with response code 0x1202
81 // (VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID). Looking at the QEMU source
82 // code, function virtio_gpu_set_scanout() in "hw/display/virtio-gpu.c",
83 // this appears fully intentional, despite not being documented in the
86 // Surprisingly, ignoring the error here, and proceeding to release
87 // host-side resources that presumably underlie head (scanout) #0, work
88 // without any problems -- the driver survives repeated "disconnect" /
89 // "connect -r" commands in the UEFI shell.
91 // So, for now, let's just suppress the error.
98 ASSERT_EFI_ERROR (Status
);
99 if (EFI_ERROR (Status
)) {
105 // Detach backing pages from the currently used 2D host resource.
107 Status
= VirtioGpuResourceDetachBacking (
108 VgpuGop
->ParentBus
, // VgpuDev
109 VgpuGop
->ResourceId
// ResourceId
111 ASSERT_EFI_ERROR (Status
);
112 if (EFI_ERROR (Status
)) {
117 // Release backing pages.
119 FreePages (VgpuGop
->BackingStore
, VgpuGop
->NumberOfPages
);
120 VgpuGop
->BackingStore
= NULL
;
121 VgpuGop
->NumberOfPages
= 0;
124 // Destroy the currently used 2D host resource.
126 Status
= VirtioGpuResourceUnref (
127 VgpuGop
->ParentBus
, // VgpuDev
128 VgpuGop
->ResourceId
// ResourceId
130 ASSERT_EFI_ERROR (Status
);
131 if (EFI_ERROR (Status
)) {
134 VgpuGop
->ResourceId
= 0;
138 // The resolutions supported by this driver.
145 STATIC CONST GOP_RESOLUTION mGopResolutions
[] = {
186 // Macro for casting VGPU_GOP.Gop to VGPU_GOP.
188 #define VGPU_GOP_FROM_GOP(GopPointer) \
189 CR (GopPointer, VGPU_GOP, Gop, VGPU_GOP_SIG)
192 // EFI_GRAPHICS_OUTPUT_PROTOCOL member functions.
198 IN EFI_GRAPHICS_OUTPUT_PROTOCOL
*This
,
199 IN UINT32 ModeNumber
,
200 OUT UINTN
*SizeOfInfo
,
201 OUT EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
**Info
204 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
*GopModeInfo
;
206 if (ModeNumber
>= ARRAY_SIZE (mGopResolutions
)) {
207 return EFI_INVALID_PARAMETER
;
210 GopModeInfo
= AllocateZeroPool (sizeof *GopModeInfo
);
211 if (GopModeInfo
== NULL
) {
212 return EFI_OUT_OF_RESOURCES
;
215 GopModeInfo
->HorizontalResolution
= mGopResolutions
[ModeNumber
].Width
;
216 GopModeInfo
->VerticalResolution
= mGopResolutions
[ModeNumber
].Height
;
217 GopModeInfo
->PixelFormat
= PixelBltOnly
;
218 GopModeInfo
->PixelsPerScanLine
= mGopResolutions
[ModeNumber
].Width
;
220 *SizeOfInfo
= sizeof *GopModeInfo
;
229 IN EFI_GRAPHICS_OUTPUT_PROTOCOL
*This
,
234 UINT32 NewResourceId
;
235 UINTN NewNumberOfBytes
;
236 UINTN NewNumberOfPages
;
237 VOID
*NewBackingStore
;
241 if (ModeNumber
>= ARRAY_SIZE (mGopResolutions
)) {
242 return EFI_UNSUPPORTED
;
245 VgpuGop
= VGPU_GOP_FROM_GOP (This
);
248 // Distinguish the first (internal) call from the other (protocol consumer)
251 if (VgpuGop
->ResourceId
== 0) {
253 // Set up the Gop -> GopMode -> GopModeInfo pointer chain, and the other
254 // (nonzero) constant fields.
256 // No direct framebuffer access is supported, only Blt() is.
258 VgpuGop
->Gop
.Mode
= &VgpuGop
->GopMode
;
260 VgpuGop
->GopMode
.MaxMode
= (UINT32
)(ARRAY_SIZE (mGopResolutions
));
261 VgpuGop
->GopMode
.Info
= &VgpuGop
->GopModeInfo
;
262 VgpuGop
->GopMode
.SizeOfInfo
= sizeof VgpuGop
->GopModeInfo
;
264 VgpuGop
->GopModeInfo
.PixelFormat
= PixelBltOnly
;
267 // This is the first time we create a host side resource.
272 // We already have an active host side resource. Create the new one without
273 // interfering with the current one, so that we can cleanly bail out on
274 // error, without disturbing the current graphics mode.
276 // The formula below will alternate between IDs 1 and 2.
278 NewResourceId
= 3 - VgpuGop
->ResourceId
;
282 // Create the 2D host resource.
284 Status
= VirtioGpuResourceCreate2d (
285 VgpuGop
->ParentBus
, // VgpuDev
286 NewResourceId
, // ResourceId
287 VirtioGpuFormatB8G8R8X8Unorm
, // Format
288 mGopResolutions
[ModeNumber
].Width
, // Width
289 mGopResolutions
[ModeNumber
].Height
// Height
291 if (EFI_ERROR (Status
)) {
296 // Allocate guest backing store.
298 NewNumberOfBytes
= mGopResolutions
[ModeNumber
].Width
*
299 mGopResolutions
[ModeNumber
].Height
* sizeof (UINT32
);
300 NewNumberOfPages
= EFI_SIZE_TO_PAGES (NewNumberOfBytes
);
301 NewBackingStore
= AllocatePages (NewNumberOfPages
);
302 if (NewBackingStore
== NULL
) {
303 Status
= EFI_OUT_OF_RESOURCES
;
304 goto DestroyHostResource
;
307 // Fill visible part of backing store with black.
309 ZeroMem (NewBackingStore
, NewNumberOfBytes
);
312 // Attach backing store to the host resource.
314 Status
= VirtioGpuResourceAttachBacking (
315 VgpuGop
->ParentBus
, // VgpuDev
316 NewResourceId
, // ResourceId
317 (UINTN
)NewBackingStore
, // BackingStoreDeviceAddress
318 NewNumberOfPages
// NumberOfPages
320 if (EFI_ERROR (Status
)) {
321 goto FreeBackingStore
;
325 // Point head (scanout) #0 to the host resource.
327 Status
= VirtioGpuSetScanout (
328 VgpuGop
->ParentBus
, // VgpuDev
331 mGopResolutions
[ModeNumber
].Width
, // Width
332 mGopResolutions
[ModeNumber
].Height
, // Height
334 NewResourceId
// ResourceId
336 if (EFI_ERROR (Status
)) {
337 goto DetachBackingStore
;
341 // If this is not the first (i.e., internal) call, then we have to (a) flush
342 // the new resource to head (scanout) #0, after having flipped the latter to
343 // the former above, plus (b) release the old resources.
345 if (VgpuGop
->ResourceId
!= 0) {
346 Status
= VirtioGpuResourceFlush (
347 VgpuGop
->ParentBus
, // VgpuDev
350 mGopResolutions
[ModeNumber
].Width
, // Width
351 mGopResolutions
[ModeNumber
].Height
, // Height
352 NewResourceId
// ResourceId
354 if (EFI_ERROR (Status
)) {
356 // Flip head (scanout) #0 back to the current resource. If this fails, we
357 // cannot continue, as this error occurs on the error path and is
358 // therefore non-recoverable.
360 Status2
= VirtioGpuSetScanout (
361 VgpuGop
->ParentBus
, // VgpuDev
364 mGopResolutions
[This
->Mode
->Mode
].Width
, // Width
365 mGopResolutions
[This
->Mode
->Mode
].Height
, // Height
367 VgpuGop
->ResourceId
// ResourceId
369 ASSERT_EFI_ERROR (Status2
);
370 if (EFI_ERROR (Status2
)) {
373 goto DetachBackingStore
;
377 // Flush successful; release the old resources (without disabling head
380 ReleaseGopResources (VgpuGop
, FALSE
/* DisableHead */);
384 // This is either the first (internal) call when we have no old resources
385 // yet, or we've changed the mode successfully and released the old
388 ASSERT (VgpuGop
->ResourceId
== 0);
389 ASSERT (VgpuGop
->BackingStore
== NULL
);
391 VgpuGop
->ResourceId
= NewResourceId
;
392 VgpuGop
->BackingStore
= NewBackingStore
;
393 VgpuGop
->NumberOfPages
= NewNumberOfPages
;
396 // Populate Mode and ModeInfo (mutable fields only).
398 VgpuGop
->GopMode
.Mode
= ModeNumber
;
399 VgpuGop
->GopModeInfo
.HorizontalResolution
=
400 mGopResolutions
[ModeNumber
].Width
;
401 VgpuGop
->GopModeInfo
.VerticalResolution
= mGopResolutions
[ModeNumber
].Height
;
402 VgpuGop
->GopModeInfo
.PixelsPerScanLine
= mGopResolutions
[ModeNumber
].Width
;
406 Status2
= VirtioGpuResourceDetachBacking (VgpuGop
->ParentBus
, NewResourceId
);
407 ASSERT_EFI_ERROR (Status2
);
408 if (EFI_ERROR (Status2
)) {
413 FreePages (NewBackingStore
, NewNumberOfPages
);
416 Status2
= VirtioGpuResourceUnref (VgpuGop
->ParentBus
, NewResourceId
);
417 ASSERT_EFI_ERROR (Status2
);
418 if (EFI_ERROR (Status2
)) {
429 IN EFI_GRAPHICS_OUTPUT_PROTOCOL
*This
,
430 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltBuffer
, OPTIONAL
431 IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation
,
434 IN UINTN DestinationX
,
435 IN UINTN DestinationY
,
438 IN UINTN Delta OPTIONAL
442 UINT32 CurrentHorizontal
;
443 UINT32 CurrentVertical
;
446 UINTN ResourceOffset
;
449 VgpuGop
= VGPU_GOP_FROM_GOP (This
);
450 CurrentHorizontal
= VgpuGop
->GopModeInfo
.HorizontalResolution
;
451 CurrentVertical
= VgpuGop
->GopModeInfo
.VerticalResolution
;
454 // We can avoid pixel format conversion in the guest because the internal
455 // representation of EFI_GRAPHICS_OUTPUT_BLT_PIXEL and that of
456 // VirtioGpuFormatB8G8R8X8Unorm are identical.
458 SegmentSize
= Width
* sizeof (UINT32
);
461 // Delta is relevant for operations that read a rectangle from, or write a
462 // rectangle to, BltBuffer.
464 // In these cases, Delta is the stride of BltBuffer, in bytes. If Delta is
465 // zero, then Width is the entire width of BltBuffer, and the stride is
466 // supposed to be calculated from Width.
468 if (BltOperation
== EfiBltVideoToBltBuffer
||
469 BltOperation
== EfiBltBufferToVideo
) {
476 // For operations that write to the display, check if the destination fits
479 if (BltOperation
== EfiBltVideoFill
||
480 BltOperation
== EfiBltBufferToVideo
||
481 BltOperation
== EfiBltVideoToVideo
) {
482 if (DestinationX
> CurrentHorizontal
||
483 Width
> CurrentHorizontal
- DestinationX
||
484 DestinationY
> CurrentVertical
||
485 Height
> CurrentVertical
- DestinationY
) {
486 return EFI_INVALID_PARAMETER
;
491 // For operations that read from the display, check if the source fits onto
494 if (BltOperation
== EfiBltVideoToBltBuffer
||
495 BltOperation
== EfiBltVideoToVideo
) {
496 if (SourceX
> CurrentHorizontal
||
497 Width
> CurrentHorizontal
- SourceX
||
498 SourceY
> CurrentVertical
||
499 Height
> CurrentVertical
- SourceY
) {
500 return EFI_INVALID_PARAMETER
;
505 // Render the request. For requests that do not modify the display, there
506 // won't be further steps.
508 switch (BltOperation
) {
509 case EfiBltVideoFill
:
511 // Write data from the BltBuffer pixel (0, 0) directly to every pixel of
512 // the video display rectangle (DestinationX, DestinationY) (DestinationX +
513 // Width, DestinationY + Height). Only one pixel will be used from the
514 // BltBuffer. Delta is NOT used.
516 for (Y
= 0; Y
< Height
; ++Y
) {
518 VgpuGop
->BackingStore
+
519 (DestinationY
+ Y
) * CurrentHorizontal
+ DestinationX
,
526 case EfiBltVideoToBltBuffer
:
528 // Read data from the video display rectangle (SourceX, SourceY) (SourceX +
529 // Width, SourceY + Height) and place it in the BltBuffer rectangle
530 // (DestinationX, DestinationY ) (DestinationX + Width, DestinationY +
531 // Height). If DestinationX or DestinationY is not zero then Delta must be
532 // set to the length in bytes of a row in the BltBuffer.
534 for (Y
= 0; Y
< Height
; ++Y
) {
537 (DestinationY
+ Y
) * Delta
+ DestinationX
* sizeof *BltBuffer
,
538 VgpuGop
->BackingStore
+
539 (SourceY
+ Y
) * CurrentHorizontal
+ SourceX
,
545 case EfiBltBufferToVideo
:
547 // Write data from the BltBuffer rectangle (SourceX, SourceY) (SourceX +
548 // Width, SourceY + Height) directly to the video display rectangle
549 // (DestinationX, DestinationY) (DestinationX + Width, DestinationY +
550 // Height). If SourceX or SourceY is not zero then Delta must be set to the
551 // length in bytes of a row in the BltBuffer.
553 for (Y
= 0; Y
< Height
; ++Y
) {
555 VgpuGop
->BackingStore
+
556 (DestinationY
+ Y
) * CurrentHorizontal
+ DestinationX
,
558 (SourceY
+ Y
) * Delta
+ SourceX
* sizeof *BltBuffer
,
564 case EfiBltVideoToVideo
:
566 // Copy from the video display rectangle (SourceX, SourceY) (SourceX +
567 // Width, SourceY + Height) to the video display rectangle (DestinationX,
568 // DestinationY) (DestinationX + Width, DestinationY + Height). The
569 // BltBuffer and Delta are not used in this mode.
571 // A single invocation of CopyMem() handles overlap between source and
572 // destination (that is, within a single line), but for multiple
573 // invocations, we must handle overlaps.
575 if (SourceY
< DestinationY
) {
580 VgpuGop
->BackingStore
+
581 (DestinationY
+ Y
) * CurrentHorizontal
+ DestinationX
,
582 VgpuGop
->BackingStore
+
583 (SourceY
+ Y
) * CurrentHorizontal
+ SourceX
,
588 for (Y
= 0; Y
< Height
; ++Y
) {
590 VgpuGop
->BackingStore
+
591 (DestinationY
+ Y
) * CurrentHorizontal
+ DestinationX
,
592 VgpuGop
->BackingStore
+
593 (SourceY
+ Y
) * CurrentHorizontal
+ SourceX
,
601 return EFI_INVALID_PARAMETER
;
605 // For operations that wrote to the display, submit the updated area to the
606 // host -- update the host resource from guest memory.
608 ResourceOffset
= sizeof (UINT32
) * (DestinationY
* CurrentHorizontal
+
610 Status
= VirtioGpuTransferToHost2d (
611 VgpuGop
->ParentBus
, // VgpuDev
612 (UINT32
)DestinationX
, // X
613 (UINT32
)DestinationY
, // Y
614 (UINT32
)Width
, // Width
615 (UINT32
)Height
, // Height
616 ResourceOffset
, // Offset
617 VgpuGop
->ResourceId
// ResourceId
619 if (EFI_ERROR (Status
)) {
624 // Flush the updated resource to the display.
626 Status
= VirtioGpuResourceFlush (
627 VgpuGop
->ParentBus
, // VgpuDev
628 (UINT32
)DestinationX
, // X
629 (UINT32
)DestinationY
, // Y
630 (UINT32
)Width
, // Width
631 (UINT32
)Height
, // Height
632 VgpuGop
->ResourceId
// ResourceId
638 // Template for initializing VGPU_GOP.Gop.
640 CONST EFI_GRAPHICS_OUTPUT_PROTOCOL mGopTemplate
= {
644 NULL
// Mode, to be overwritten in the actual protocol instance