OvmfPkg/Virtio: take RingBaseShift in SetQueueAddress()
[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 Copyright (c) 2017, AMD Inc, All rights reserved.<BR>
7
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
12
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.
15
16 **/
17
18 #include <Library/VirtioLib.h>
19
20 #include "VirtioGpu.h"
21
22 /**
23 Configure the VirtIo GPU device that underlies VgpuDev.
24
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.
30
31 @retval EFI_SUCCESS VirtIo GPU configuration successful.
32
33 @retval EFI_UNSUPPORTED The host-side configuration of the VirtIo GPU is not
34 supported by this driver.
35
36 @retval Error codes from underlying functions.
37 **/
38 EFI_STATUS
39 VirtioGpuInit (
40 IN OUT VGPU_DEV *VgpuDev
41 )
42 {
43 UINT8 NextDevStat;
44 EFI_STATUS Status;
45 UINT64 Features;
46 UINT16 QueueSize;
47
48 //
49 // Execute virtio-v1.0-cs04, 3.1.1 Driver Requirements: Device
50 // Initialization.
51 //
52 // 1. Reset the device.
53 //
54 NextDevStat = 0;
55 Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);
56 if (EFI_ERROR (Status)) {
57 goto Failed;
58 }
59
60 //
61 // 2. Set the ACKNOWLEDGE status bit [...]
62 //
63 NextDevStat |= VSTAT_ACK;
64 Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);
65 if (EFI_ERROR (Status)) {
66 goto Failed;
67 }
68
69 //
70 // 3. Set the DRIVER status bit [...]
71 //
72 NextDevStat |= VSTAT_DRIVER;
73 Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);
74 if (EFI_ERROR (Status)) {
75 goto Failed;
76 }
77
78 //
79 // 4. Read device feature bits...
80 //
81 Status = VgpuDev->VirtIo->GetDeviceFeatures (VgpuDev->VirtIo, &Features);
82 if (EFI_ERROR (Status)) {
83 goto Failed;
84 }
85 if ((Features & VIRTIO_F_VERSION_1) == 0) {
86 Status = EFI_UNSUPPORTED;
87 goto Failed;
88 }
89 //
90 // We only want the most basic 2D features.
91 //
92 Features &= VIRTIO_F_VERSION_1;
93
94 //
95 // ... and write the subset of feature bits understood by the [...] driver to
96 // the device. [...]
97 // 5. Set the FEATURES_OK status bit.
98 // 6. Re-read device status to ensure the FEATURES_OK bit is still set [...]
99 //
100 Status = Virtio10WriteFeatures (VgpuDev->VirtIo, Features, &NextDevStat);
101 if (EFI_ERROR (Status)) {
102 goto Failed;
103 }
104
105 //
106 // 7. Perform device-specific setup, including discovery of virtqueues for
107 // the device [...]
108 //
109 Status = VgpuDev->VirtIo->SetQueueSel (VgpuDev->VirtIo,
110 VIRTIO_GPU_CONTROL_QUEUE);
111 if (EFI_ERROR (Status)) {
112 goto Failed;
113 }
114 Status = VgpuDev->VirtIo->GetQueueNumMax (VgpuDev->VirtIo, &QueueSize);
115 if (EFI_ERROR (Status)) {
116 goto Failed;
117 }
118
119 //
120 // We implement each VirtIo GPU command that we use with two descriptors:
121 // request, response.
122 //
123 if (QueueSize < 2) {
124 Status = EFI_UNSUPPORTED;
125 goto Failed;
126 }
127
128 //
129 // [...] population of virtqueues [...]
130 //
131 Status = VirtioRingInit (VgpuDev->VirtIo, QueueSize, &VgpuDev->Ring);
132 if (EFI_ERROR (Status)) {
133 goto Failed;
134 }
135 Status = VgpuDev->VirtIo->SetQueueAddress (
136 VgpuDev->VirtIo,
137 &VgpuDev->Ring,
138 0
139 );
140 if (EFI_ERROR (Status)) {
141 goto ReleaseQueue;
142 }
143
144 //
145 // 8. Set the DRIVER_OK status bit.
146 //
147 NextDevStat |= VSTAT_DRIVER_OK;
148 Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);
149 if (EFI_ERROR (Status)) {
150 goto ReleaseQueue;
151 }
152
153 return EFI_SUCCESS;
154
155 ReleaseQueue:
156 VirtioRingUninit (VgpuDev->VirtIo, &VgpuDev->Ring);
157
158 Failed:
159 //
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). [...]
163 //
164 // VirtIo access failure here should not mask the original error.
165 //
166 NextDevStat |= VSTAT_FAILED;
167 VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);
168
169 return Status;
170 }
171
172 /**
173 De-configure the VirtIo GPU device that underlies VgpuDev.
174
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.
180 **/
181 VOID
182 VirtioGpuUninit (
183 IN OUT VGPU_DEV *VgpuDev
184 )
185 {
186 //
187 // Resetting the VirtIo device makes it release its resources and forget its
188 // configuration.
189 //
190 VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, 0);
191 VirtioRingUninit (VgpuDev->VirtIo, &VgpuDev->Ring);
192 }
193
194 /**
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
197 configuration.
198
199 This function may only be called (that is, VGPU_DEV.ExitBoot may only be
200 signaled) after VirtioGpuInit() returns and before VirtioGpuUninit() is
201 called.
202
203 @param[in] Event Event whose notification function is being invoked.
204
205 @param[in] Context Pointer to the associated VGPU_DEV object.
206 **/
207 VOID
208 EFIAPI
209 VirtioGpuExitBoot (
210 IN EFI_EVENT Event,
211 IN VOID *Context
212 )
213 {
214 VGPU_DEV *VgpuDev;
215
216 VgpuDev = Context;
217 VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, 0);
218 }
219
220 /**
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.
223
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.
229
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
233 from the host.
234
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.
239
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:
244
245 - it sets Type from RequestType,
246
247 - it sets Flags and FenceId based on Fence,
248
249 - it zeroes CtxId and Padding.
250
251 @param[in] RequestSize Size of the entire caller-allocated request object,
252 including the leading VIRTIO_GPU_CONTROL_HEADER.
253
254 @retval EFI_SUCCESS Operation successful.
255
256 @retval EFI_DEVICE_ERROR The host rejected the request. The host error
257 code has been logged on the EFI_D_ERROR level.
258
259 @return Codes for unexpected errors in VirtIo
260 messaging.
261 **/
262 STATIC
263 EFI_STATUS
264 VirtioGpuSendCommand (
265 IN OUT VGPU_DEV *VgpuDev,
266 IN VIRTIO_GPU_CONTROL_TYPE RequestType,
267 IN BOOLEAN Fence,
268 IN OUT volatile VIRTIO_GPU_CONTROL_HEADER *Header,
269 IN UINTN RequestSize
270 )
271 {
272 DESC_INDICES Indices;
273 volatile VIRTIO_GPU_CONTROL_HEADER Response;
274 EFI_STATUS Status;
275 UINT32 ResponseSize;
276
277 //
278 // Initialize Header.
279 //
280 Header->Type = RequestType;
281 if (Fence) {
282 Header->Flags = VIRTIO_GPU_FLAG_FENCE;
283 Header->FenceId = VgpuDev->FenceId++;
284 } else {
285 Header->Flags = 0;
286 Header->FenceId = 0;
287 }
288 Header->CtxId = 0;
289 Header->Padding = 0;
290
291 ASSERT (RequestSize >= sizeof *Header);
292 ASSERT (RequestSize <= MAX_UINT32);
293
294 //
295 // Compose the descriptor chain.
296 //
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);
302
303 //
304 // Send the command.
305 //
306 Status = VirtioFlush (VgpuDev->VirtIo, VIRTIO_GPU_CONTROL_QUEUE,
307 &VgpuDev->Ring, &Indices, &ResponseSize);
308 if (EFI_ERROR (Status)) {
309 return Status;
310 }
311
312 //
313 // Parse the response.
314 //
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;
319 }
320
321 if (Response.Type == VirtioGpuRespOkNodata) {
322 return EFI_SUCCESS;
323 }
324
325 DEBUG ((EFI_D_ERROR, "%a: Request=0x%x Response=0x%x\n", __FUNCTION__,
326 (UINT32)RequestType, Response.Type));
327 return EFI_DEVICE_ERROR;
328 }
329
330 /**
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
333 interface details:
334
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.
340
341 @retval EFI_INVALID_PARAMETER Invalid command-specific parameters were
342 detected by this driver.
343
344 @retval EFI_SUCCESS Operation successful.
345
346 @retval EFI_DEVICE_ERROR The host rejected the request. The host error
347 code has been logged on the EFI_D_ERROR level.
348
349 @return Codes for unexpected errors in VirtIo
350 messaging.
351
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").
355 **/
356 EFI_STATUS
357 VirtioGpuResourceCreate2d (
358 IN OUT VGPU_DEV *VgpuDev,
359 IN UINT32 ResourceId,
360 IN VIRTIO_GPU_FORMATS Format,
361 IN UINT32 Width,
362 IN UINT32 Height
363 )
364 {
365 volatile VIRTIO_GPU_RESOURCE_CREATE_2D Request;
366
367 if (ResourceId == 0) {
368 return EFI_INVALID_PARAMETER;
369 }
370
371 Request.ResourceId = ResourceId;
372 Request.Format = (UINT32)Format;
373 Request.Width = Width;
374 Request.Height = Height;
375
376 return VirtioGpuSendCommand (
377 VgpuDev,
378 VirtioGpuCmdResourceCreate2d,
379 FALSE, // Fence
380 &Request.Header,
381 sizeof Request
382 );
383 }
384
385 EFI_STATUS
386 VirtioGpuResourceUnref (
387 IN OUT VGPU_DEV *VgpuDev,
388 IN UINT32 ResourceId
389 )
390 {
391 volatile VIRTIO_GPU_RESOURCE_UNREF Request;
392
393 if (ResourceId == 0) {
394 return EFI_INVALID_PARAMETER;
395 }
396
397 Request.ResourceId = ResourceId;
398 Request.Padding = 0;
399
400 return VirtioGpuSendCommand (
401 VgpuDev,
402 VirtioGpuCmdResourceUnref,
403 FALSE, // Fence
404 &Request.Header,
405 sizeof Request
406 );
407 }
408
409 EFI_STATUS
410 VirtioGpuResourceAttachBacking (
411 IN OUT VGPU_DEV *VgpuDev,
412 IN UINT32 ResourceId,
413 IN VOID *FirstBackingPage,
414 IN UINTN NumberOfPages
415 )
416 {
417 volatile VIRTIO_GPU_RESOURCE_ATTACH_BACKING Request;
418
419 if (ResourceId == 0) {
420 return EFI_INVALID_PARAMETER;
421 }
422
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;
428
429 return VirtioGpuSendCommand (
430 VgpuDev,
431 VirtioGpuCmdResourceAttachBacking,
432 FALSE, // Fence
433 &Request.Header,
434 sizeof Request
435 );
436 }
437
438 EFI_STATUS
439 VirtioGpuResourceDetachBacking (
440 IN OUT VGPU_DEV *VgpuDev,
441 IN UINT32 ResourceId
442 )
443 {
444 volatile VIRTIO_GPU_RESOURCE_DETACH_BACKING Request;
445
446 if (ResourceId == 0) {
447 return EFI_INVALID_PARAMETER;
448 }
449
450 Request.ResourceId = ResourceId;
451 Request.Padding = 0;
452
453 //
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.
458 //
459 return VirtioGpuSendCommand (
460 VgpuDev,
461 VirtioGpuCmdResourceDetachBacking,
462 TRUE, // Fence
463 &Request.Header,
464 sizeof Request
465 );
466 }
467
468 EFI_STATUS
469 VirtioGpuSetScanout (
470 IN OUT VGPU_DEV *VgpuDev,
471 IN UINT32 X,
472 IN UINT32 Y,
473 IN UINT32 Width,
474 IN UINT32 Height,
475 IN UINT32 ScanoutId,
476 IN UINT32 ResourceId
477 )
478 {
479 volatile VIRTIO_GPU_SET_SCANOUT Request;
480
481 //
482 // Unlike for most other commands, ResourceId=0 is valid; it
483 // is used to disable a scanout.
484 //
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;
491
492 return VirtioGpuSendCommand (
493 VgpuDev,
494 VirtioGpuCmdSetScanout,
495 FALSE, // Fence
496 &Request.Header,
497 sizeof Request
498 );
499 }
500
501 EFI_STATUS
502 VirtioGpuTransferToHost2d (
503 IN OUT VGPU_DEV *VgpuDev,
504 IN UINT32 X,
505 IN UINT32 Y,
506 IN UINT32 Width,
507 IN UINT32 Height,
508 IN UINT64 Offset,
509 IN UINT32 ResourceId
510 )
511 {
512 volatile VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D Request;
513
514 if (ResourceId == 0) {
515 return EFI_INVALID_PARAMETER;
516 }
517
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;
524 Request.Padding = 0;
525
526 return VirtioGpuSendCommand (
527 VgpuDev,
528 VirtioGpuCmdTransferToHost2d,
529 FALSE, // Fence
530 &Request.Header,
531 sizeof Request
532 );
533 }
534
535 EFI_STATUS
536 VirtioGpuResourceFlush (
537 IN OUT VGPU_DEV *VgpuDev,
538 IN UINT32 X,
539 IN UINT32 Y,
540 IN UINT32 Width,
541 IN UINT32 Height,
542 IN UINT32 ResourceId
543 )
544 {
545 volatile VIRTIO_GPU_RESOURCE_FLUSH Request;
546
547 if (ResourceId == 0) {
548 return EFI_INVALID_PARAMETER;
549 }
550
551 Request.Rectangle.X = X;
552 Request.Rectangle.Y = Y;
553 Request.Rectangle.Width = Width;
554 Request.Rectangle.Height = Height;
555 Request.ResourceId = ResourceId;
556 Request.Padding = 0;
557
558 return VirtioGpuSendCommand (
559 VgpuDev,
560 VirtioGpuCmdResourceFlush,
561 FALSE, // Fence
562 &Request.Header,
563 sizeof Request
564 );
565 }