]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/VirtioGpuDxe/Commands.c
OvmfPkg/VirtioGpuDxe: provide functions for sending VirtIo GPU commands
[mirror_edk2.git] / OvmfPkg / VirtioGpuDxe / Commands.c
1 /** @file
2
3 VirtIo GPU initialization, and commands (primitives) for the GPU device.
4
5 Copyright (C) 2016, Red Hat, Inc.
6
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
11
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.
14
15 **/
16
17 #include <Library/VirtioLib.h>
18
19 #include "VirtioGpu.h"
20
21 /**
22 Configure the VirtIo GPU device that underlies VgpuDev.
23
24 @param[in,out] VgpuDev The VGPU_DEV object to set up VirtIo messaging for.
25 On input, the caller is responsible for having
26 initialized VgpuDev->VirtIo. On output, VgpuDev->Ring
27 has been initialized, and synchronous VirtIo GPU
28 commands (primitives) can be submitted to the device.
29
30 @retval EFI_SUCCESS VirtIo GPU configuration successful.
31
32 @retval EFI_UNSUPPORTED The host-side configuration of the VirtIo GPU is not
33 supported by this driver.
34
35 @retval Error codes from underlying functions.
36 **/
37 EFI_STATUS
38 VirtioGpuInit (
39 IN OUT VGPU_DEV *VgpuDev
40 )
41 {
42 UINT8 NextDevStat;
43 EFI_STATUS Status;
44 UINT64 Features;
45 UINT16 QueueSize;
46
47 //
48 // Execute virtio-v1.0-cs04, 3.1.1 Driver Requirements: Device
49 // Initialization.
50 //
51 // 1. Reset the device.
52 //
53 NextDevStat = 0;
54 Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);
55 if (EFI_ERROR (Status)) {
56 goto Failed;
57 }
58
59 //
60 // 2. Set the ACKNOWLEDGE status bit [...]
61 //
62 NextDevStat |= VSTAT_ACK;
63 Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);
64 if (EFI_ERROR (Status)) {
65 goto Failed;
66 }
67
68 //
69 // 3. Set the DRIVER status bit [...]
70 //
71 NextDevStat |= VSTAT_DRIVER;
72 Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);
73 if (EFI_ERROR (Status)) {
74 goto Failed;
75 }
76
77 //
78 // 4. Read device feature bits...
79 //
80 Status = VgpuDev->VirtIo->GetDeviceFeatures (VgpuDev->VirtIo, &Features);
81 if (EFI_ERROR (Status)) {
82 goto Failed;
83 }
84 if ((Features & VIRTIO_F_VERSION_1) == 0) {
85 Status = EFI_UNSUPPORTED;
86 goto Failed;
87 }
88 //
89 // We only want the most basic 2D features.
90 //
91 Features &= VIRTIO_F_VERSION_1;
92
93 //
94 // ... and write the subset of feature bits understood by the [...] driver to
95 // the device. [...]
96 // 5. Set the FEATURES_OK status bit.
97 // 6. Re-read device status to ensure the FEATURES_OK bit is still set [...]
98 //
99 Status = Virtio10WriteFeatures (VgpuDev->VirtIo, Features, &NextDevStat);
100 if (EFI_ERROR (Status)) {
101 goto Failed;
102 }
103
104 //
105 // 7. Perform device-specific setup, including discovery of virtqueues for
106 // the device [...]
107 //
108 Status = VgpuDev->VirtIo->SetQueueSel (VgpuDev->VirtIo,
109 VIRTIO_GPU_CONTROL_QUEUE);
110 if (EFI_ERROR (Status)) {
111 goto Failed;
112 }
113 Status = VgpuDev->VirtIo->GetQueueNumMax (VgpuDev->VirtIo, &QueueSize);
114 if (EFI_ERROR (Status)) {
115 goto Failed;
116 }
117
118 //
119 // We implement each VirtIo GPU command that we use with two descriptors:
120 // request, response.
121 //
122 if (QueueSize < 2) {
123 Status = EFI_UNSUPPORTED;
124 goto Failed;
125 }
126
127 //
128 // [...] population of virtqueues [...]
129 //
130 Status = VirtioRingInit (QueueSize, &VgpuDev->Ring);
131 if (EFI_ERROR (Status)) {
132 goto Failed;
133 }
134 Status = VgpuDev->VirtIo->SetQueueAddress (VgpuDev->VirtIo, &VgpuDev->Ring);
135 if (EFI_ERROR (Status)) {
136 goto ReleaseQueue;
137 }
138
139 //
140 // 8. Set the DRIVER_OK status bit.
141 //
142 NextDevStat |= VSTAT_DRIVER_OK;
143 Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);
144 if (EFI_ERROR (Status)) {
145 goto ReleaseQueue;
146 }
147
148 return EFI_SUCCESS;
149
150 ReleaseQueue:
151 VirtioRingUninit (&VgpuDev->Ring);
152
153 Failed:
154 //
155 // If any of these steps go irrecoverably wrong, the driver SHOULD set the
156 // FAILED status bit to indicate that it has given up on the device (it can
157 // reset the device later to restart if desired). [...]
158 //
159 // VirtIo access failure here should not mask the original error.
160 //
161 NextDevStat |= VSTAT_FAILED;
162 VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);
163
164 return Status;
165 }
166
167 /**
168 De-configure the VirtIo GPU device that underlies VgpuDev.
169
170 @param[in,out] VgpuDev The VGPU_DEV object to tear down VirtIo messaging
171 for. On input, the caller is responsible for having
172 called VirtioGpuInit(). On output, VgpuDev->Ring has
173 been uninitialized; VirtIo GPU commands (primitives)
174 can no longer be submitted to the device.
175 **/
176 VOID
177 VirtioGpuUninit (
178 IN OUT VGPU_DEV *VgpuDev
179 )
180 {
181 //
182 // Resetting the VirtIo device makes it release its resources and forget its
183 // configuration.
184 //
185 VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, 0);
186 VirtioRingUninit (&VgpuDev->Ring);
187 }
188
189 /**
190 EFI_EVENT_NOTIFY function for the VGPU_DEV.ExitBoot event. It resets the
191 VirtIo device, causing it to release its resources and to forget its
192 configuration.
193
194 This function may only be called (that is, VGPU_DEV.ExitBoot may only be
195 signaled) after VirtioGpuInit() returns and before VirtioGpuUninit() is
196 called.
197
198 @param[in] Event Event whose notification function is being invoked.
199
200 @param[in] Context Pointer to the associated VGPU_DEV object.
201 **/
202 VOID
203 EFIAPI
204 VirtioGpuExitBoot (
205 IN EFI_EVENT Event,
206 IN VOID *Context
207 )
208 {
209 VGPU_DEV *VgpuDev;
210
211 VgpuDev = Context;
212 VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, 0);
213 }
214
215 /**
216 Internal utility function that sends a request to the VirtIo GPU device
217 model, awaits the answer from the host, and returns a status.
218
219 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
220 device. The caller is responsible to have
221 successfully invoked VirtioGpuInit() on VgpuDev
222 previously, while VirtioGpuUninit() must not have
223 been called on VgpuDev.
224
225 @param[in] RequestType The type of the request. The caller is responsible
226 for providing a VirtioGpuCmd* RequestType which, on
227 success, elicits a VirtioGpuRespOkNodata response
228 from the host.
229
230 @param[in] Fence Whether to enable fencing for this request. Fencing
231 forces the host to complete the command before
232 producing a response. If Fence is TRUE, then
233 VgpuDev->FenceId is consumed, and incremented.
234
235 @param[in,out] Header Pointer to the caller-allocated request object. The
236 request must start with VIRTIO_GPU_CONTROL_HEADER.
237 This function overwrites all fields of Header before
238 submitting the request to the host:
239
240 - it sets Type from RequestType,
241
242 - it sets Flags and FenceId based on Fence,
243
244 - it zeroes CtxId and Padding.
245
246 @param[in] RequestSize Size of the entire caller-allocated request object,
247 including the leading VIRTIO_GPU_CONTROL_HEADER.
248
249 @retval EFI_SUCCESS Operation successful.
250
251 @retval EFI_DEVICE_ERROR The host rejected the request. The host error
252 code has been logged on the EFI_D_ERROR level.
253
254 @return Codes for unexpected errors in VirtIo
255 messaging.
256 **/
257 STATIC
258 EFI_STATUS
259 VirtioGpuSendCommand (
260 IN OUT VGPU_DEV *VgpuDev,
261 IN VIRTIO_GPU_CONTROL_TYPE RequestType,
262 IN BOOLEAN Fence,
263 IN OUT volatile VIRTIO_GPU_CONTROL_HEADER *Header,
264 IN UINTN RequestSize
265 )
266 {
267 DESC_INDICES Indices;
268 volatile VIRTIO_GPU_CONTROL_HEADER Response;
269 EFI_STATUS Status;
270 UINT32 ResponseSize;
271
272 //
273 // Initialize Header.
274 //
275 Header->Type = RequestType;
276 if (Fence) {
277 Header->Flags = VIRTIO_GPU_FLAG_FENCE;
278 Header->FenceId = VgpuDev->FenceId++;
279 } else {
280 Header->Flags = 0;
281 Header->FenceId = 0;
282 }
283 Header->CtxId = 0;
284 Header->Padding = 0;
285
286 ASSERT (RequestSize >= sizeof *Header);
287
288 //
289 // Compose the descriptor chain.
290 //
291 VirtioPrepare (&VgpuDev->Ring, &Indices);
292 VirtioAppendDesc (&VgpuDev->Ring, (UINTN)Header, RequestSize,
293 VRING_DESC_F_NEXT, &Indices);
294 VirtioAppendDesc (&VgpuDev->Ring, (UINTN)&Response, sizeof Response,
295 VRING_DESC_F_WRITE, &Indices);
296
297 //
298 // Send the command.
299 //
300 Status = VirtioFlush (VgpuDev->VirtIo, VIRTIO_GPU_CONTROL_QUEUE,
301 &VgpuDev->Ring, &Indices, &ResponseSize);
302 if (EFI_ERROR (Status)) {
303 return Status;
304 }
305
306 //
307 // Parse the response.
308 //
309 if (ResponseSize != sizeof Response) {
310 DEBUG ((EFI_D_ERROR, "%a: malformed response to Request=0x%x\n",
311 __FUNCTION__, (UINT32)RequestType));
312 return EFI_PROTOCOL_ERROR;
313 }
314
315 if (Response.Type == VirtioGpuRespOkNodata) {
316 return EFI_SUCCESS;
317 }
318
319 DEBUG ((EFI_D_ERROR, "%a: Request=0x%x Response=0x%x\n", __FUNCTION__,
320 (UINT32)RequestType, Response.Type));
321 return EFI_DEVICE_ERROR;
322 }
323
324 /**
325 The following functions send requests to the VirtIo GPU device model, await
326 the answer from the host, and return a status. They share the following
327 interface details:
328
329 @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
330 device. The caller is responsible to have
331 successfully invoked VirtioGpuInit() on VgpuDev
332 previously, while VirtioGpuUninit() must not have
333 been called on VgpuDev.
334
335 @retval EFI_INVALID_PARAMETER Invalid command-specific parameters were
336 detected by this driver.
337
338 @retval EFI_SUCCESS Operation successful.
339
340 @retval EFI_DEVICE_ERROR The host rejected the request. The host error
341 code has been logged on the EFI_D_ERROR level.
342
343 @return Codes for unexpected errors in VirtIo
344 messaging.
345
346 For the command-specific parameters, please consult the GPU Device section of
347 the VirtIo 1.0 specification (see references in
348 "OvmfPkg/Include/IndustryStandard/VirtioGpu.h").
349 **/
350 EFI_STATUS
351 VirtioGpuResourceCreate2d (
352 IN OUT VGPU_DEV *VgpuDev,
353 IN UINT32 ResourceId,
354 IN VIRTIO_GPU_FORMATS Format,
355 IN UINT32 Width,
356 IN UINT32 Height
357 )
358 {
359 volatile VIRTIO_GPU_RESOURCE_CREATE_2D Request;
360
361 if (ResourceId == 0) {
362 return EFI_INVALID_PARAMETER;
363 }
364
365 Request.ResourceId = ResourceId;
366 Request.Format = (UINT32)Format;
367 Request.Width = Width;
368 Request.Height = Height;
369
370 return VirtioGpuSendCommand (
371 VgpuDev,
372 VirtioGpuCmdResourceCreate2d,
373 FALSE, // Fence
374 &Request.Header,
375 sizeof Request
376 );
377 }
378
379 EFI_STATUS
380 VirtioGpuResourceUnref (
381 IN OUT VGPU_DEV *VgpuDev,
382 IN UINT32 ResourceId
383 )
384 {
385 volatile VIRTIO_GPU_RESOURCE_UNREF Request;
386
387 if (ResourceId == 0) {
388 return EFI_INVALID_PARAMETER;
389 }
390
391 Request.ResourceId = ResourceId;
392 Request.Padding = 0;
393
394 return VirtioGpuSendCommand (
395 VgpuDev,
396 VirtioGpuCmdResourceUnref,
397 FALSE, // Fence
398 &Request.Header,
399 sizeof Request
400 );
401 }
402
403 EFI_STATUS
404 VirtioGpuResourceAttachBacking (
405 IN OUT VGPU_DEV *VgpuDev,
406 IN UINT32 ResourceId,
407 IN VOID *FirstBackingPage,
408 IN UINTN NumberOfPages
409 )
410 {
411 volatile VIRTIO_GPU_RESOURCE_ATTACH_BACKING Request;
412
413 if (ResourceId == 0) {
414 return EFI_INVALID_PARAMETER;
415 }
416
417 Request.ResourceId = ResourceId;
418 Request.NrEntries = 1;
419 Request.Entry.Addr = (UINTN)FirstBackingPage;
420 Request.Entry.Length = (UINT32)EFI_PAGES_TO_SIZE (NumberOfPages);
421 Request.Entry.Padding = 0;
422
423 return VirtioGpuSendCommand (
424 VgpuDev,
425 VirtioGpuCmdResourceAttachBacking,
426 FALSE, // Fence
427 &Request.Header,
428 sizeof Request
429 );
430 }
431
432 EFI_STATUS
433 VirtioGpuResourceDetachBacking (
434 IN OUT VGPU_DEV *VgpuDev,
435 IN UINT32 ResourceId
436 )
437 {
438 volatile VIRTIO_GPU_RESOURCE_DETACH_BACKING Request;
439
440 if (ResourceId == 0) {
441 return EFI_INVALID_PARAMETER;
442 }
443
444 Request.ResourceId = ResourceId;
445 Request.Padding = 0;
446
447 //
448 // In this case, we set Fence to TRUE, because after this function returns,
449 // the caller might reasonably want to repurpose the backing pages
450 // immediately. Thus we should ensure that the host releases all references
451 // to the backing pages before we return.
452 //
453 return VirtioGpuSendCommand (
454 VgpuDev,
455 VirtioGpuCmdResourceDetachBacking,
456 TRUE, // Fence
457 &Request.Header,
458 sizeof Request
459 );
460 }
461
462 EFI_STATUS
463 VirtioGpuSetScanout (
464 IN OUT VGPU_DEV *VgpuDev,
465 IN UINT32 X,
466 IN UINT32 Y,
467 IN UINT32 Width,
468 IN UINT32 Height,
469 IN UINT32 ScanoutId,
470 IN UINT32 ResourceId
471 )
472 {
473 volatile VIRTIO_GPU_SET_SCANOUT Request;
474
475 //
476 // Unlike for most other commands, ResourceId=0 is valid; it
477 // is used to disable a scanout.
478 //
479 Request.Rectangle.X = X;
480 Request.Rectangle.Y = Y;
481 Request.Rectangle.Width = Width;
482 Request.Rectangle.Height = Height;
483 Request.ScanoutId = ScanoutId;
484 Request.ResourceId = ResourceId;
485
486 return VirtioGpuSendCommand (
487 VgpuDev,
488 VirtioGpuCmdSetScanout,
489 FALSE, // Fence
490 &Request.Header,
491 sizeof Request
492 );
493 }
494
495 EFI_STATUS
496 VirtioGpuTransferToHost2d (
497 IN OUT VGPU_DEV *VgpuDev,
498 IN UINT32 X,
499 IN UINT32 Y,
500 IN UINT32 Width,
501 IN UINT32 Height,
502 IN UINT64 Offset,
503 IN UINT32 ResourceId
504 )
505 {
506 volatile VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D Request;
507
508 if (ResourceId == 0) {
509 return EFI_INVALID_PARAMETER;
510 }
511
512 Request.Rectangle.X = X;
513 Request.Rectangle.Y = Y;
514 Request.Rectangle.Width = Width;
515 Request.Rectangle.Height = Height;
516 Request.Offset = Offset;
517 Request.ResourceId = ResourceId;
518 Request.Padding = 0;
519
520 return VirtioGpuSendCommand (
521 VgpuDev,
522 VirtioGpuCmdTransferToHost2d,
523 FALSE, // Fence
524 &Request.Header,
525 sizeof Request
526 );
527 }
528
529 EFI_STATUS
530 VirtioGpuResourceFlush (
531 IN OUT VGPU_DEV *VgpuDev,
532 IN UINT32 X,
533 IN UINT32 Y,
534 IN UINT32 Width,
535 IN UINT32 Height,
536 IN UINT32 ResourceId
537 )
538 {
539 volatile VIRTIO_GPU_RESOURCE_FLUSH Request;
540
541 if (ResourceId == 0) {
542 return EFI_INVALID_PARAMETER;
543 }
544
545 Request.Rectangle.X = X;
546 Request.Rectangle.Y = Y;
547 Request.Rectangle.Width = Width;
548 Request.Rectangle.Height = Height;
549 Request.ResourceId = ResourceId;
550 Request.Padding = 0;
551
552 return VirtioGpuSendCommand (
553 VgpuDev,
554 VirtioGpuCmdResourceFlush,
555 FALSE, // Fence
556 &Request.Header,
557 sizeof Request
558 );
559 }