3 EFI_GRAPHICS_OUTPUT_PROTOCOL member functions for the VirtIo GPU driver.
5 Copyright (C) 2016, Red Hat, Inc.
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Library/MemoryAllocationLib.h>
13 #include "VirtioGpu.h"
16 Release guest-side and host-side resources that are related to an initialized
19 param[in,out] VgpuGop The VGPU_GOP object to release resources for.
21 On input, the caller is responsible for having called
22 VgpuGop->Gop.SetMode() at least once successfully.
23 (This is equivalent to the requirement that
24 VgpuGop->BackingStore be non-NULL. It is also
25 equivalent to the requirement that VgpuGop->ResourceId
28 On output, resources will be released, and
29 VgpuGop->BackingStore and VgpuGop->ResourceId will be
32 param[in] DisableHead Whether this head (scanout) currently references the
33 resource identified by VgpuGop->ResourceId. Only pass
34 FALSE when VgpuGop->Gop.SetMode() calls this function
35 while switching between modes, and set it to TRUE
40 IN OUT VGPU_GOP
*VgpuGop
,
41 IN BOOLEAN DisableHead
46 ASSERT (VgpuGop
->ResourceId
!= 0);
47 ASSERT (VgpuGop
->BackingStore
!= NULL
);
50 // If any of the following host-side destruction steps fail, we can't get out
51 // of an inconsistent state, so we'll hang. In general errors in object
52 // destruction can hardly be recovered from.
56 // Dissociate head (scanout) #0 from the currently used 2D host resource,
57 // by setting ResourceId=0 for it.
59 Status
= VirtioGpuSetScanout (
60 VgpuGop
->ParentBus
, // VgpuDev
64 0, // X, Y, Width, Height
71 // According to the GPU Device section of the VirtIo specification, the
72 // above operation is valid:
74 // "The driver can use resource_id = 0 to disable a scanout."
76 // However, in practice QEMU does not allow us to disable head (scanout) #0
77 // -- it rejects the command with response code 0x1202
78 // (VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID). Looking at the QEMU source
79 // code, function virtio_gpu_set_scanout() in "hw/display/virtio-gpu.c",
80 // this appears fully intentional, despite not being documented in the
83 // Surprisingly, ignoring the error here, and proceeding to release
84 // host-side resources that presumably underlie head (scanout) #0, work
85 // without any problems -- the driver survives repeated "disconnect" /
86 // "connect -r" commands in the UEFI shell.
88 // So, for now, let's just suppress the error.
95 ASSERT_EFI_ERROR (Status
);
96 if (EFI_ERROR (Status
)) {
102 // Detach backing pages from the currently used 2D host resource.
104 Status
= VirtioGpuResourceDetachBacking (
105 VgpuGop
->ParentBus
, // VgpuDev
106 VgpuGop
->ResourceId
// ResourceId
108 ASSERT_EFI_ERROR (Status
);
109 if (EFI_ERROR (Status
)) {
114 // Unmap and release backing pages.
116 VirtioGpuUnmapAndFreeBackingStore (
117 VgpuGop
->ParentBus
, // VgpuDev
118 VgpuGop
->NumberOfPages
, // NumberOfPages
119 VgpuGop
->BackingStore
, // HostAddress
120 VgpuGop
->BackingStoreMap
// Mapping
122 VgpuGop
->BackingStore
= NULL
;
123 VgpuGop
->NumberOfPages
= 0;
124 VgpuGop
->BackingStoreMap
= NULL
;
127 // Destroy the currently used 2D host resource.
129 Status
= VirtioGpuResourceUnref (
130 VgpuGop
->ParentBus
, // VgpuDev
131 VgpuGop
->ResourceId
// ResourceId
133 ASSERT_EFI_ERROR (Status
);
134 if (EFI_ERROR (Status
)) {
138 VgpuGop
->ResourceId
= 0;
142 // The resolutions supported by this driver.
149 STATIC CONST GOP_RESOLUTION mGopResolutions
[] = {
190 // Macro for casting VGPU_GOP.Gop to VGPU_GOP.
192 #define VGPU_GOP_FROM_GOP(GopPointer) \
193 CR (GopPointer, VGPU_GOP, Gop, VGPU_GOP_SIG)
196 // EFI_GRAPHICS_OUTPUT_PROTOCOL member functions.
202 IN EFI_GRAPHICS_OUTPUT_PROTOCOL
*This
,
203 IN UINT32 ModeNumber
,
204 OUT UINTN
*SizeOfInfo
,
205 OUT EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
**Info
208 EFI_GRAPHICS_OUTPUT_MODE_INFORMATION
*GopModeInfo
;
210 if (ModeNumber
>= ARRAY_SIZE (mGopResolutions
)) {
211 return EFI_INVALID_PARAMETER
;
214 GopModeInfo
= AllocateZeroPool (sizeof *GopModeInfo
);
215 if (GopModeInfo
== NULL
) {
216 return EFI_OUT_OF_RESOURCES
;
219 GopModeInfo
->HorizontalResolution
= mGopResolutions
[ModeNumber
].Width
;
220 GopModeInfo
->VerticalResolution
= mGopResolutions
[ModeNumber
].Height
;
221 GopModeInfo
->PixelFormat
= PixelBltOnly
;
222 GopModeInfo
->PixelsPerScanLine
= mGopResolutions
[ModeNumber
].Width
;
224 *SizeOfInfo
= sizeof *GopModeInfo
;
233 IN EFI_GRAPHICS_OUTPUT_PROTOCOL
*This
,
238 UINT32 NewResourceId
;
239 UINTN NewNumberOfBytes
;
240 UINTN NewNumberOfPages
;
241 VOID
*NewBackingStore
;
242 EFI_PHYSICAL_ADDRESS NewBackingStoreDeviceAddress
;
243 VOID
*NewBackingStoreMap
;
248 if (ModeNumber
>= ARRAY_SIZE (mGopResolutions
)) {
249 return EFI_UNSUPPORTED
;
252 VgpuGop
= VGPU_GOP_FROM_GOP (This
);
255 // Distinguish the first (internal) call from the other (protocol consumer)
258 if (VgpuGop
->ResourceId
== 0) {
260 // Set up the Gop -> GopMode -> GopModeInfo pointer chain, and the other
261 // (nonzero) constant fields.
263 // No direct framebuffer access is supported, only Blt() is.
265 VgpuGop
->Gop
.Mode
= &VgpuGop
->GopMode
;
267 VgpuGop
->GopMode
.MaxMode
= (UINT32
)(ARRAY_SIZE (mGopResolutions
));
268 VgpuGop
->GopMode
.Info
= &VgpuGop
->GopModeInfo
;
269 VgpuGop
->GopMode
.SizeOfInfo
= sizeof VgpuGop
->GopModeInfo
;
271 VgpuGop
->GopModeInfo
.PixelFormat
= PixelBltOnly
;
274 // This is the first time we create a host side resource.
279 // We already have an active host side resource. Create the new one without
280 // interfering with the current one, so that we can cleanly bail out on
281 // error, without disturbing the current graphics mode.
283 // The formula below will alternate between IDs 1 and 2.
285 NewResourceId
= 3 - VgpuGop
->ResourceId
;
289 // Create the 2D host resource.
291 Status
= VirtioGpuResourceCreate2d (
292 VgpuGop
->ParentBus
, // VgpuDev
293 NewResourceId
, // ResourceId
294 VirtioGpuFormatB8G8R8X8Unorm
, // Format
295 mGopResolutions
[ModeNumber
].Width
, // Width
296 mGopResolutions
[ModeNumber
].Height
// Height
298 if (EFI_ERROR (Status
)) {
303 // Allocate, zero and map guest backing store, for bus master common buffer
306 NewNumberOfBytes
= mGopResolutions
[ModeNumber
].Width
*
307 mGopResolutions
[ModeNumber
].Height
* sizeof (UINT32
);
308 NewNumberOfPages
= EFI_SIZE_TO_PAGES (NewNumberOfBytes
);
309 Status
= VirtioGpuAllocateZeroAndMapBackingStore (
310 VgpuGop
->ParentBus
, // VgpuDev
311 NewNumberOfPages
, // NumberOfPages
312 &NewBackingStore
, // HostAddress
313 &NewBackingStoreDeviceAddress
, // DeviceAddress
314 &NewBackingStoreMap
// Mapping
316 if (EFI_ERROR (Status
)) {
317 goto DestroyHostResource
;
321 // Attach backing store to the host resource.
323 Status
= VirtioGpuResourceAttachBacking (
324 VgpuGop
->ParentBus
, // VgpuDev
325 NewResourceId
, // ResourceId
326 NewBackingStoreDeviceAddress
, // BackingStoreDeviceAddress
327 NewNumberOfPages
// NumberOfPages
329 if (EFI_ERROR (Status
)) {
330 goto UnmapAndFreeBackingStore
;
334 // Point head (scanout) #0 to the host resource.
336 Status
= VirtioGpuSetScanout (
337 VgpuGop
->ParentBus
, // VgpuDev
340 mGopResolutions
[ModeNumber
].Width
, // Width
341 mGopResolutions
[ModeNumber
].Height
, // Height
343 NewResourceId
// ResourceId
345 if (EFI_ERROR (Status
)) {
346 goto DetachBackingStore
;
350 // If this is not the first (i.e., internal) call, then we have to (a) flush
351 // the new resource to head (scanout) #0, after having flipped the latter to
352 // the former above, plus (b) release the old resources.
354 if (VgpuGop
->ResourceId
!= 0) {
355 Status
= VirtioGpuResourceFlush (
356 VgpuGop
->ParentBus
, // VgpuDev
359 mGopResolutions
[ModeNumber
].Width
, // Width
360 mGopResolutions
[ModeNumber
].Height
, // Height
361 NewResourceId
// ResourceId
363 if (EFI_ERROR (Status
)) {
365 // Flip head (scanout) #0 back to the current resource. If this fails, we
366 // cannot continue, as this error occurs on the error path and is
367 // therefore non-recoverable.
369 Status2
= VirtioGpuSetScanout (
370 VgpuGop
->ParentBus
, // VgpuDev
373 mGopResolutions
[This
->Mode
->Mode
].Width
, // Width
374 mGopResolutions
[This
->Mode
->Mode
].Height
, // Height
376 VgpuGop
->ResourceId
// ResourceId
378 ASSERT_EFI_ERROR (Status2
);
379 if (EFI_ERROR (Status2
)) {
383 goto DetachBackingStore
;
387 // Flush successful; release the old resources (without disabling head
390 ReleaseGopResources (VgpuGop
, FALSE
/* DisableHead */);
394 // This is either the first (internal) call when we have no old resources
395 // yet, or we've changed the mode successfully and released the old
398 ASSERT (VgpuGop
->ResourceId
== 0);
399 ASSERT (VgpuGop
->BackingStore
== NULL
);
401 VgpuGop
->ResourceId
= NewResourceId
;
402 VgpuGop
->BackingStore
= NewBackingStore
;
403 VgpuGop
->NumberOfPages
= NewNumberOfPages
;
404 VgpuGop
->BackingStoreMap
= NewBackingStoreMap
;
407 // Populate Mode and ModeInfo (mutable fields only).
409 VgpuGop
->GopMode
.Mode
= ModeNumber
;
410 VgpuGop
->GopModeInfo
.HorizontalResolution
=
411 mGopResolutions
[ModeNumber
].Width
;
412 VgpuGop
->GopModeInfo
.VerticalResolution
= mGopResolutions
[ModeNumber
].Height
;
413 VgpuGop
->GopModeInfo
.PixelsPerScanLine
= mGopResolutions
[ModeNumber
].Width
;
417 Status2
= VirtioGpuResourceDetachBacking (VgpuGop
->ParentBus
, NewResourceId
);
418 ASSERT_EFI_ERROR (Status2
);
419 if (EFI_ERROR (Status2
)) {
423 UnmapAndFreeBackingStore
:
424 VirtioGpuUnmapAndFreeBackingStore (
425 VgpuGop
->ParentBus
, // VgpuDev
426 NewNumberOfPages
, // NumberOfPages
427 NewBackingStore
, // HostAddress
428 NewBackingStoreMap
// Mapping
432 Status2
= VirtioGpuResourceUnref (VgpuGop
->ParentBus
, NewResourceId
);
433 ASSERT_EFI_ERROR (Status2
);
434 if (EFI_ERROR (Status2
)) {
445 IN EFI_GRAPHICS_OUTPUT_PROTOCOL
*This
,
446 IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL
*BltBuffer OPTIONAL
,
447 IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation
,
450 IN UINTN DestinationX
,
451 IN UINTN DestinationY
,
454 IN UINTN Delta OPTIONAL
458 UINT32 CurrentHorizontal
;
459 UINT32 CurrentVertical
;
462 UINTN ResourceOffset
;
465 VgpuGop
= VGPU_GOP_FROM_GOP (This
);
466 CurrentHorizontal
= VgpuGop
->GopModeInfo
.HorizontalResolution
;
467 CurrentVertical
= VgpuGop
->GopModeInfo
.VerticalResolution
;
470 // We can avoid pixel format conversion in the guest because the internal
471 // representation of EFI_GRAPHICS_OUTPUT_BLT_PIXEL and that of
472 // VirtioGpuFormatB8G8R8X8Unorm are identical.
474 SegmentSize
= Width
* sizeof (UINT32
);
477 // Delta is relevant for operations that read a rectangle from, or write a
478 // rectangle to, BltBuffer.
480 // In these cases, Delta is the stride of BltBuffer, in bytes. If Delta is
481 // zero, then Width is the entire width of BltBuffer, and the stride is
482 // supposed to be calculated from Width.
484 if ((BltOperation
== EfiBltVideoToBltBuffer
) ||
485 (BltOperation
== EfiBltBufferToVideo
))
493 // For operations that write to the display, check if the destination fits
496 if ((BltOperation
== EfiBltVideoFill
) ||
497 (BltOperation
== EfiBltBufferToVideo
) ||
498 (BltOperation
== EfiBltVideoToVideo
))
500 if ((DestinationX
> CurrentHorizontal
) ||
501 (Width
> CurrentHorizontal
- DestinationX
) ||
502 (DestinationY
> CurrentVertical
) ||
503 (Height
> CurrentVertical
- DestinationY
))
505 return EFI_INVALID_PARAMETER
;
510 // For operations that read from the display, check if the source fits onto
513 if ((BltOperation
== EfiBltVideoToBltBuffer
) ||
514 (BltOperation
== EfiBltVideoToVideo
))
516 if ((SourceX
> CurrentHorizontal
) ||
517 (Width
> CurrentHorizontal
- SourceX
) ||
518 (SourceY
> CurrentVertical
) ||
519 (Height
> CurrentVertical
- SourceY
))
521 return EFI_INVALID_PARAMETER
;
526 // Render the request. For requests that do not modify the display, there
527 // won't be further steps.
529 switch (BltOperation
) {
530 case EfiBltVideoFill
:
532 // Write data from the BltBuffer pixel (0, 0) directly to every pixel of
533 // the video display rectangle (DestinationX, DestinationY) (DestinationX +
534 // Width, DestinationY + Height). Only one pixel will be used from the
535 // BltBuffer. Delta is NOT used.
537 for (Y
= 0; Y
< Height
; ++Y
) {
539 VgpuGop
->BackingStore
+
540 (DestinationY
+ Y
) * CurrentHorizontal
+ DestinationX
,
548 case EfiBltVideoToBltBuffer
:
550 // Read data from the video display rectangle (SourceX, SourceY) (SourceX +
551 // Width, SourceY + Height) and place it in the BltBuffer rectangle
552 // (DestinationX, DestinationY ) (DestinationX + Width, DestinationY +
553 // Height). If DestinationX or DestinationY is not zero then Delta must be
554 // set to the length in bytes of a row in the BltBuffer.
556 for (Y
= 0; Y
< Height
; ++Y
) {
559 (DestinationY
+ Y
) * Delta
+ DestinationX
* sizeof *BltBuffer
,
560 VgpuGop
->BackingStore
+
561 (SourceY
+ Y
) * CurrentHorizontal
+ SourceX
,
568 case EfiBltBufferToVideo
:
570 // Write data from the BltBuffer rectangle (SourceX, SourceY) (SourceX +
571 // Width, SourceY + Height) directly to the video display rectangle
572 // (DestinationX, DestinationY) (DestinationX + Width, DestinationY +
573 // Height). If SourceX or SourceY is not zero then Delta must be set to the
574 // length in bytes of a row in the BltBuffer.
576 for (Y
= 0; Y
< Height
; ++Y
) {
578 VgpuGop
->BackingStore
+
579 (DestinationY
+ Y
) * CurrentHorizontal
+ DestinationX
,
581 (SourceY
+ Y
) * Delta
+ SourceX
* sizeof *BltBuffer
,
588 case EfiBltVideoToVideo
:
590 // Copy from the video display rectangle (SourceX, SourceY) (SourceX +
591 // Width, SourceY + Height) to the video display rectangle (DestinationX,
592 // DestinationY) (DestinationX + Width, DestinationY + Height). The
593 // BltBuffer and Delta are not used in this mode.
595 // A single invocation of CopyMem() handles overlap between source and
596 // destination (that is, within a single line), but for multiple
597 // invocations, we must handle overlaps.
599 if (SourceY
< DestinationY
) {
604 VgpuGop
->BackingStore
+
605 (DestinationY
+ Y
) * CurrentHorizontal
+ DestinationX
,
606 VgpuGop
->BackingStore
+
607 (SourceY
+ Y
) * CurrentHorizontal
+ SourceX
,
612 for (Y
= 0; Y
< Height
; ++Y
) {
614 VgpuGop
->BackingStore
+
615 (DestinationY
+ Y
) * CurrentHorizontal
+ DestinationX
,
616 VgpuGop
->BackingStore
+
617 (SourceY
+ Y
) * CurrentHorizontal
+ SourceX
,
626 return EFI_INVALID_PARAMETER
;
630 // For operations that wrote to the display, submit the updated area to the
631 // host -- update the host resource from guest memory.
633 ResourceOffset
= sizeof (UINT32
) * (DestinationY
* CurrentHorizontal
+
635 Status
= VirtioGpuTransferToHost2d (
636 VgpuGop
->ParentBus
, // VgpuDev
637 (UINT32
)DestinationX
, // X
638 (UINT32
)DestinationY
, // Y
639 (UINT32
)Width
, // Width
640 (UINT32
)Height
, // Height
641 ResourceOffset
, // Offset
642 VgpuGop
->ResourceId
// ResourceId
644 if (EFI_ERROR (Status
)) {
649 // Flush the updated resource to the display.
651 Status
= VirtioGpuResourceFlush (
652 VgpuGop
->ParentBus
, // VgpuDev
653 (UINT32
)DestinationX
, // X
654 (UINT32
)DestinationY
, // Y
655 (UINT32
)Width
, // Width
656 (UINT32
)Height
, // Height
657 VgpuGop
->ResourceId
// ResourceId
663 // Template for initializing VGPU_GOP.Gop.
665 CONST EFI_GRAPHICS_OUTPUT_PROTOCOL mGopTemplate
= {
669 NULL
// Mode, to be overwritten in the actual protocol instance