1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
|
/** @file
EFI_GRAPHICS_OUTPUT_PROTOCOL member functions for the VirtIo GPU driver.
Copyright (C) 2016, Red Hat, Inc.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <Library/MemoryAllocationLib.h>
#include "VirtioGpu.h"
/**
Release guest-side and host-side resources that are related to an initialized
VGPU_GOP.Gop.
param[in,out] VgpuGop The VGPU_GOP object to release resources for.
On input, the caller is responsible for having called
VgpuGop->Gop.SetMode() at least once successfully.
(This is equivalent to the requirement that
VgpuGop->BackingStore be non-NULL. It is also
equivalent to the requirement that VgpuGop->ResourceId
be nonzero.)
On output, resources will be released, and
VgpuGop->BackingStore and VgpuGop->ResourceId will be
nulled.
param[in] DisableHead Whether this head (scanout) currently references the
resource identified by VgpuGop->ResourceId. Only pass
FALSE when VgpuGop->Gop.SetMode() calls this function
while switching between modes, and set it to TRUE
every other time.
**/
VOID
ReleaseGopResources (
IN OUT VGPU_GOP *VgpuGop,
IN BOOLEAN DisableHead
)
{
EFI_STATUS Status;
ASSERT (VgpuGop->ResourceId != 0);
ASSERT (VgpuGop->BackingStore != NULL);
//
// If any of the following host-side destruction steps fail, we can't get out
// of an inconsistent state, so we'll hang. In general errors in object
// destruction can hardly be recovered from.
//
if (DisableHead) {
//
// Dissociate head (scanout) #0 from the currently used 2D host resource,
// by setting ResourceId=0 for it.
//
Status = VirtioGpuSetScanout (
VgpuGop->ParentBus, // VgpuDev
0, 0, 0, 0, // X, Y, Width, Height
0, // ScanoutId
0 // ResourceId
);
//
// HACK BEGINS HERE
//
// According to the GPU Device section of the VirtIo specification, the
// above operation is valid:
//
// "The driver can use resource_id = 0 to disable a scanout."
//
// However, in practice QEMU does not allow us to disable head (scanout) #0
// -- it rejects the command with response code 0x1202
// (VIRTIO_GPU_RESP_ERR_INVALID_SCANOUT_ID). Looking at the QEMU source
// code, function virtio_gpu_set_scanout() in "hw/display/virtio-gpu.c",
// this appears fully intentional, despite not being documented in the
// spec.
//
// Surprisingly, ignoring the error here, and proceeding to release
// host-side resources that presumably underlie head (scanout) #0, work
// without any problems -- the driver survives repeated "disconnect" /
// "connect -r" commands in the UEFI shell.
//
// So, for now, let's just suppress the error.
//
Status = EFI_SUCCESS;
//
// HACK ENDS HERE
//
ASSERT_EFI_ERROR (Status);
if (EFI_ERROR (Status)) {
CpuDeadLoop ();
}
}
//
// Detach backing pages from the currently used 2D host resource.
//
Status = VirtioGpuResourceDetachBacking (
VgpuGop->ParentBus, // VgpuDev
VgpuGop->ResourceId // ResourceId
);
ASSERT_EFI_ERROR (Status);
if (EFI_ERROR (Status)) {
CpuDeadLoop ();
}
//
// Unmap and release backing pages.
//
VirtioGpuUnmapAndFreeBackingStore (
VgpuGop->ParentBus, // VgpuDev
VgpuGop->NumberOfPages, // NumberOfPages
VgpuGop->BackingStore, // HostAddress
VgpuGop->BackingStoreMap // Mapping
);
VgpuGop->BackingStore = NULL;
VgpuGop->NumberOfPages = 0;
VgpuGop->BackingStoreMap = NULL;
//
// Destroy the currently used 2D host resource.
//
Status = VirtioGpuResourceUnref (
VgpuGop->ParentBus, // VgpuDev
VgpuGop->ResourceId // ResourceId
);
ASSERT_EFI_ERROR (Status);
if (EFI_ERROR (Status)) {
CpuDeadLoop ();
}
VgpuGop->ResourceId = 0;
}
//
// The resolutions supported by this driver.
//
typedef struct {
UINT32 Width;
UINT32 Height;
} GOP_RESOLUTION;
STATIC CONST GOP_RESOLUTION mGopResolutions[] = {
{ 640, 480 },
{ 800, 480 },
{ 800, 600 },
{ 832, 624 },
{ 960, 640 },
{ 1024, 600 },
{ 1024, 768 },
{ 1152, 864 },
{ 1152, 870 },
{ 1280, 720 },
{ 1280, 760 },
{ 1280, 768 },
{ 1280, 800 },
{ 1280, 960 },
{ 1280, 1024 },
{ 1360, 768 },
{ 1366, 768 },
{ 1400, 1050 },
{ 1440, 900 },
{ 1600, 900 },
{ 1600, 1200 },
{ 1680, 1050 },
{ 1920, 1080 },
{ 1920, 1200 },
{ 1920, 1440 },
{ 2000, 2000 },
{ 2048, 1536 },
{ 2048, 2048 },
{ 2560, 1440 },
{ 2560, 1600 },
{ 2560, 2048 },
{ 2800, 2100 },
{ 3200, 2400 },
{ 3840, 2160 },
{ 4096, 2160 },
{ 7680, 4320 },
{ 8192, 4320 },
};
//
// Macro for casting VGPU_GOP.Gop to VGPU_GOP.
//
#define VGPU_GOP_FROM_GOP(GopPointer) \
CR (GopPointer, VGPU_GOP, Gop, VGPU_GOP_SIG)
//
// EFI_GRAPHICS_OUTPUT_PROTOCOL member functions.
//
STATIC
EFI_STATUS
EFIAPI
GopQueryMode (
IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
IN UINT32 ModeNumber,
OUT UINTN *SizeOfInfo,
OUT EFI_GRAPHICS_OUTPUT_MODE_INFORMATION **Info
)
{
EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *GopModeInfo;
if (ModeNumber >= ARRAY_SIZE (mGopResolutions)) {
return EFI_INVALID_PARAMETER;
}
GopModeInfo = AllocateZeroPool (sizeof *GopModeInfo);
if (GopModeInfo == NULL) {
return EFI_OUT_OF_RESOURCES;
}
GopModeInfo->HorizontalResolution = mGopResolutions[ModeNumber].Width;
GopModeInfo->VerticalResolution = mGopResolutions[ModeNumber].Height;
GopModeInfo->PixelFormat = PixelBltOnly;
GopModeInfo->PixelsPerScanLine = mGopResolutions[ModeNumber].Width;
*SizeOfInfo = sizeof *GopModeInfo;
*Info = GopModeInfo;
return EFI_SUCCESS;
}
STATIC
EFI_STATUS
EFIAPI
GopSetMode (
IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
IN UINT32 ModeNumber
)
{
VGPU_GOP *VgpuGop;
UINT32 NewResourceId;
UINTN NewNumberOfBytes;
UINTN NewNumberOfPages;
VOID *NewBackingStore;
EFI_PHYSICAL_ADDRESS NewBackingStoreDeviceAddress;
VOID *NewBackingStoreMap;
EFI_STATUS Status;
EFI_STATUS Status2;
if (ModeNumber >= ARRAY_SIZE (mGopResolutions)) {
return EFI_UNSUPPORTED;
}
VgpuGop = VGPU_GOP_FROM_GOP (This);
//
// Distinguish the first (internal) call from the other (protocol consumer)
// calls.
//
if (VgpuGop->ResourceId == 0) {
//
// Set up the Gop -> GopMode -> GopModeInfo pointer chain, and the other
// (nonzero) constant fields.
//
// No direct framebuffer access is supported, only Blt() is.
//
VgpuGop->Gop.Mode = &VgpuGop->GopMode;
VgpuGop->GopMode.MaxMode = (UINT32)(ARRAY_SIZE (mGopResolutions));
VgpuGop->GopMode.Info = &VgpuGop->GopModeInfo;
VgpuGop->GopMode.SizeOfInfo = sizeof VgpuGop->GopModeInfo;
VgpuGop->GopModeInfo.PixelFormat = PixelBltOnly;
//
// This is the first time we create a host side resource.
//
NewResourceId = 1;
} else {
//
// We already have an active host side resource. Create the new one without
// interfering with the current one, so that we can cleanly bail out on
// error, without disturbing the current graphics mode.
//
// The formula below will alternate between IDs 1 and 2.
//
NewResourceId = 3 - VgpuGop->ResourceId;
}
//
// Create the 2D host resource.
//
Status = VirtioGpuResourceCreate2d (
VgpuGop->ParentBus, // VgpuDev
NewResourceId, // ResourceId
VirtioGpuFormatB8G8R8X8Unorm, // Format
mGopResolutions[ModeNumber].Width, // Width
mGopResolutions[ModeNumber].Height // Height
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Allocate, zero and map guest backing store, for bus master common buffer
// operation.
//
NewNumberOfBytes = mGopResolutions[ModeNumber].Width *
mGopResolutions[ModeNumber].Height * sizeof (UINT32);
NewNumberOfPages = EFI_SIZE_TO_PAGES (NewNumberOfBytes);
Status = VirtioGpuAllocateZeroAndMapBackingStore (
VgpuGop->ParentBus, // VgpuDev
NewNumberOfPages, // NumberOfPages
&NewBackingStore, // HostAddress
&NewBackingStoreDeviceAddress, // DeviceAddress
&NewBackingStoreMap // Mapping
);
if (EFI_ERROR (Status)) {
goto DestroyHostResource;
}
//
// Attach backing store to the host resource.
//
Status = VirtioGpuResourceAttachBacking (
VgpuGop->ParentBus, // VgpuDev
NewResourceId, // ResourceId
NewBackingStoreDeviceAddress, // BackingStoreDeviceAddress
NewNumberOfPages // NumberOfPages
);
if (EFI_ERROR (Status)) {
goto UnmapAndFreeBackingStore;
}
//
// Point head (scanout) #0 to the host resource.
//
Status = VirtioGpuSetScanout (
VgpuGop->ParentBus, // VgpuDev
0, // X
0, // Y
mGopResolutions[ModeNumber].Width, // Width
mGopResolutions[ModeNumber].Height, // Height
0, // ScanoutId
NewResourceId // ResourceId
);
if (EFI_ERROR (Status)) {
goto DetachBackingStore;
}
//
// If this is not the first (i.e., internal) call, then we have to (a) flush
// the new resource to head (scanout) #0, after having flipped the latter to
// the former above, plus (b) release the old resources.
//
if (VgpuGop->ResourceId != 0) {
Status = VirtioGpuResourceFlush (
VgpuGop->ParentBus, // VgpuDev
0, // X
0, // Y
mGopResolutions[ModeNumber].Width, // Width
mGopResolutions[ModeNumber].Height, // Height
NewResourceId // ResourceId
);
if (EFI_ERROR (Status)) {
//
// Flip head (scanout) #0 back to the current resource. If this fails, we
// cannot continue, as this error occurs on the error path and is
// therefore non-recoverable.
//
Status2 = VirtioGpuSetScanout (
VgpuGop->ParentBus, // VgpuDev
0, // X
0, // Y
mGopResolutions[This->Mode->Mode].Width, // Width
mGopResolutions[This->Mode->Mode].Height, // Height
0, // ScanoutId
VgpuGop->ResourceId // ResourceId
);
ASSERT_EFI_ERROR (Status2);
if (EFI_ERROR (Status2)) {
CpuDeadLoop ();
}
goto DetachBackingStore;
}
//
// Flush successful; release the old resources (without disabling head
// (scanout) #0).
//
ReleaseGopResources (VgpuGop, FALSE /* DisableHead */);
}
//
// This is either the first (internal) call when we have no old resources
// yet, or we've changed the mode successfully and released the old
// resources.
//
ASSERT (VgpuGop->ResourceId == 0);
ASSERT (VgpuGop->BackingStore == NULL);
VgpuGop->ResourceId = NewResourceId;
VgpuGop->BackingStore = NewBackingStore;
VgpuGop->NumberOfPages = NewNumberOfPages;
VgpuGop->BackingStoreMap = NewBackingStoreMap;
//
// Populate Mode and ModeInfo (mutable fields only).
//
VgpuGop->GopMode.Mode = ModeNumber;
VgpuGop->GopModeInfo.HorizontalResolution =
mGopResolutions[ModeNumber].Width;
VgpuGop->GopModeInfo.VerticalResolution = mGopResolutions[ModeNumber].Height;
VgpuGop->GopModeInfo.PixelsPerScanLine = mGopResolutions[ModeNumber].Width;
return EFI_SUCCESS;
DetachBackingStore:
Status2 = VirtioGpuResourceDetachBacking (VgpuGop->ParentBus, NewResourceId);
ASSERT_EFI_ERROR (Status2);
if (EFI_ERROR (Status2)) {
CpuDeadLoop ();
}
UnmapAndFreeBackingStore:
VirtioGpuUnmapAndFreeBackingStore (
VgpuGop->ParentBus, // VgpuDev
NewNumberOfPages, // NumberOfPages
NewBackingStore, // HostAddress
NewBackingStoreMap // Mapping
);
DestroyHostResource:
Status2 = VirtioGpuResourceUnref (VgpuGop->ParentBus, NewResourceId);
ASSERT_EFI_ERROR (Status2);
if (EFI_ERROR (Status2)) {
CpuDeadLoop ();
}
return Status;
}
STATIC
EFI_STATUS
EFIAPI
GopBlt (
IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
IN EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer, OPTIONAL
IN EFI_GRAPHICS_OUTPUT_BLT_OPERATION BltOperation,
IN UINTN SourceX,
IN UINTN SourceY,
IN UINTN DestinationX,
IN UINTN DestinationY,
IN UINTN Width,
IN UINTN Height,
IN UINTN Delta OPTIONAL
)
{
VGPU_GOP *VgpuGop;
UINT32 CurrentHorizontal;
UINT32 CurrentVertical;
UINTN SegmentSize;
UINTN Y;
UINTN ResourceOffset;
EFI_STATUS Status;
VgpuGop = VGPU_GOP_FROM_GOP (This);
CurrentHorizontal = VgpuGop->GopModeInfo.HorizontalResolution;
CurrentVertical = VgpuGop->GopModeInfo.VerticalResolution;
//
// We can avoid pixel format conversion in the guest because the internal
// representation of EFI_GRAPHICS_OUTPUT_BLT_PIXEL and that of
// VirtioGpuFormatB8G8R8X8Unorm are identical.
//
SegmentSize = Width * sizeof (UINT32);
//
// Delta is relevant for operations that read a rectangle from, or write a
// rectangle to, BltBuffer.
//
// In these cases, Delta is the stride of BltBuffer, in bytes. If Delta is
// zero, then Width is the entire width of BltBuffer, and the stride is
// supposed to be calculated from Width.
//
if (BltOperation == EfiBltVideoToBltBuffer ||
BltOperation == EfiBltBufferToVideo) {
if (Delta == 0) {
Delta = SegmentSize;
}
}
//
// For operations that write to the display, check if the destination fits
// onto the display.
//
if (BltOperation == EfiBltVideoFill ||
BltOperation == EfiBltBufferToVideo ||
BltOperation == EfiBltVideoToVideo) {
if (DestinationX > CurrentHorizontal ||
Width > CurrentHorizontal - DestinationX ||
DestinationY > CurrentVertical ||
Height > CurrentVertical - DestinationY) {
return EFI_INVALID_PARAMETER;
}
}
//
// For operations that read from the display, check if the source fits onto
// the display.
//
if (BltOperation == EfiBltVideoToBltBuffer ||
BltOperation == EfiBltVideoToVideo) {
if (SourceX > CurrentHorizontal ||
Width > CurrentHorizontal - SourceX ||
SourceY > CurrentVertical ||
Height > CurrentVertical - SourceY) {
return EFI_INVALID_PARAMETER;
}
}
//
// Render the request. For requests that do not modify the display, there
// won't be further steps.
//
switch (BltOperation) {
case EfiBltVideoFill:
//
// Write data from the BltBuffer pixel (0, 0) directly to every pixel of
// the video display rectangle (DestinationX, DestinationY) (DestinationX +
// Width, DestinationY + Height). Only one pixel will be used from the
// BltBuffer. Delta is NOT used.
//
for (Y = 0; Y < Height; ++Y) {
SetMem32 (
VgpuGop->BackingStore +
(DestinationY + Y) * CurrentHorizontal + DestinationX,
SegmentSize,
*(UINT32 *)BltBuffer
);
}
break;
case EfiBltVideoToBltBuffer:
//
// Read data from the video display rectangle (SourceX, SourceY) (SourceX +
// Width, SourceY + Height) and place it in the BltBuffer rectangle
// (DestinationX, DestinationY ) (DestinationX + Width, DestinationY +
// Height). If DestinationX or DestinationY is not zero then Delta must be
// set to the length in bytes of a row in the BltBuffer.
//
for (Y = 0; Y < Height; ++Y) {
CopyMem (
(UINT8 *)BltBuffer +
(DestinationY + Y) * Delta + DestinationX * sizeof *BltBuffer,
VgpuGop->BackingStore +
(SourceY + Y) * CurrentHorizontal + SourceX,
SegmentSize
);
}
return EFI_SUCCESS;
case EfiBltBufferToVideo:
//
// Write data from the BltBuffer rectangle (SourceX, SourceY) (SourceX +
// Width, SourceY + Height) directly to the video display rectangle
// (DestinationX, DestinationY) (DestinationX + Width, DestinationY +
// Height). If SourceX or SourceY is not zero then Delta must be set to the
// length in bytes of a row in the BltBuffer.
//
for (Y = 0; Y < Height; ++Y) {
CopyMem (
VgpuGop->BackingStore +
(DestinationY + Y) * CurrentHorizontal + DestinationX,
(UINT8 *)BltBuffer +
(SourceY + Y) * Delta + SourceX * sizeof *BltBuffer,
SegmentSize
);
}
break;
case EfiBltVideoToVideo:
//
// Copy from the video display rectangle (SourceX, SourceY) (SourceX +
// Width, SourceY + Height) to the video display rectangle (DestinationX,
// DestinationY) (DestinationX + Width, DestinationY + Height). The
// BltBuffer and Delta are not used in this mode.
//
// A single invocation of CopyMem() handles overlap between source and
// destination (that is, within a single line), but for multiple
// invocations, we must handle overlaps.
//
if (SourceY < DestinationY) {
Y = Height;
while (Y > 0) {
--Y;
CopyMem (
VgpuGop->BackingStore +
(DestinationY + Y) * CurrentHorizontal + DestinationX,
VgpuGop->BackingStore +
(SourceY + Y) * CurrentHorizontal + SourceX,
SegmentSize
);
}
} else {
for (Y = 0; Y < Height; ++Y) {
CopyMem (
VgpuGop->BackingStore +
(DestinationY + Y) * CurrentHorizontal + DestinationX,
VgpuGop->BackingStore +
(SourceY + Y) * CurrentHorizontal + SourceX,
SegmentSize
);
}
}
break;
default:
return EFI_INVALID_PARAMETER;
}
//
// For operations that wrote to the display, submit the updated area to the
// host -- update the host resource from guest memory.
//
ResourceOffset = sizeof (UINT32) * (DestinationY * CurrentHorizontal +
DestinationX);
Status = VirtioGpuTransferToHost2d (
VgpuGop->ParentBus, // VgpuDev
(UINT32)DestinationX, // X
(UINT32)DestinationY, // Y
(UINT32)Width, // Width
(UINT32)Height, // Height
ResourceOffset, // Offset
VgpuGop->ResourceId // ResourceId
);
if (EFI_ERROR (Status)) {
return Status;
}
//
// Flush the updated resource to the display.
//
Status = VirtioGpuResourceFlush (
VgpuGop->ParentBus, // VgpuDev
(UINT32)DestinationX, // X
(UINT32)DestinationY, // Y
(UINT32)Width, // Width
(UINT32)Height, // Height
VgpuGop->ResourceId // ResourceId
);
return Status;
}
//
// Template for initializing VGPU_GOP.Gop.
//
CONST EFI_GRAPHICS_OUTPUT_PROTOCOL mGopTemplate = {
GopQueryMode,
GopSetMode,
GopBlt,
NULL // Mode, to be overwritten in the actual protocol instance
};
|