]>
Commit | Line | Data |
---|---|---|
c5f235bb LE |
1 | /** @file\r |
2 | \r | |
3 | VirtIo GPU initialization, and commands (primitives) for the GPU device.\r | |
4 | \r | |
5 | Copyright (C) 2016, Red Hat, Inc.\r | |
fc2c1543 | 6 | Copyright (c) 2017, AMD Inc, All rights reserved.<BR>\r |
c5f235bb LE |
7 | \r |
8 | This program and the accompanying materials are licensed and made available\r | |
9 | under the terms and conditions of the BSD License which accompanies this\r | |
10 | distribution. The full text of the license may be found at\r | |
11 | http://opensource.org/licenses/bsd-license.php\r | |
12 | \r | |
13 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r | |
14 | WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
15 | \r | |
16 | **/\r | |
17 | \r | |
c5f235bb LE |
18 | #include <Library/VirtioLib.h>\r |
19 | \r | |
20 | #include "VirtioGpu.h"\r | |
21 | \r | |
22 | /**\r | |
23 | Configure the VirtIo GPU device that underlies VgpuDev.\r | |
24 | \r | |
25 | @param[in,out] VgpuDev The VGPU_DEV object to set up VirtIo messaging for.\r | |
26 | On input, the caller is responsible for having\r | |
27 | initialized VgpuDev->VirtIo. On output, VgpuDev->Ring\r | |
28 | has been initialized, and synchronous VirtIo GPU\r | |
29 | commands (primitives) can be submitted to the device.\r | |
30 | \r | |
31 | @retval EFI_SUCCESS VirtIo GPU configuration successful.\r | |
32 | \r | |
33 | @retval EFI_UNSUPPORTED The host-side configuration of the VirtIo GPU is not\r | |
34 | supported by this driver.\r | |
35 | \r | |
36 | @retval Error codes from underlying functions.\r | |
37 | **/\r | |
38 | EFI_STATUS\r | |
39 | VirtioGpuInit (\r | |
40 | IN OUT VGPU_DEV *VgpuDev\r | |
41 | )\r | |
42 | {\r | |
43 | UINT8 NextDevStat;\r | |
44 | EFI_STATUS Status;\r | |
45 | UINT64 Features;\r | |
46 | UINT16 QueueSize;\r | |
9bc5026c | 47 | UINT64 RingBaseShift;\r |
c5f235bb LE |
48 | \r |
49 | //\r | |
50 | // Execute virtio-v1.0-cs04, 3.1.1 Driver Requirements: Device\r | |
51 | // Initialization.\r | |
52 | //\r | |
53 | // 1. Reset the device.\r | |
54 | //\r | |
55 | NextDevStat = 0;\r | |
56 | Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);\r | |
57 | if (EFI_ERROR (Status)) {\r | |
58 | goto Failed;\r | |
59 | }\r | |
60 | \r | |
61 | //\r | |
62 | // 2. Set the ACKNOWLEDGE status bit [...]\r | |
63 | //\r | |
64 | NextDevStat |= VSTAT_ACK;\r | |
65 | Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);\r | |
66 | if (EFI_ERROR (Status)) {\r | |
67 | goto Failed;\r | |
68 | }\r | |
69 | \r | |
70 | //\r | |
71 | // 3. Set the DRIVER status bit [...]\r | |
72 | //\r | |
73 | NextDevStat |= VSTAT_DRIVER;\r | |
74 | Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);\r | |
75 | if (EFI_ERROR (Status)) {\r | |
76 | goto Failed;\r | |
77 | }\r | |
78 | \r | |
79 | //\r | |
80 | // 4. Read device feature bits...\r | |
81 | //\r | |
82 | Status = VgpuDev->VirtIo->GetDeviceFeatures (VgpuDev->VirtIo, &Features);\r | |
83 | if (EFI_ERROR (Status)) {\r | |
84 | goto Failed;\r | |
85 | }\r | |
86 | if ((Features & VIRTIO_F_VERSION_1) == 0) {\r | |
87 | Status = EFI_UNSUPPORTED;\r | |
88 | goto Failed;\r | |
89 | }\r | |
90 | //\r | |
91 | // We only want the most basic 2D features.\r | |
92 | //\r | |
db528909 | 93 | Features &= VIRTIO_F_VERSION_1 | VIRTIO_F_IOMMU_PLATFORM;\r |
c5f235bb LE |
94 | \r |
95 | //\r | |
96 | // ... and write the subset of feature bits understood by the [...] driver to\r | |
97 | // the device. [...]\r | |
98 | // 5. Set the FEATURES_OK status bit.\r | |
99 | // 6. Re-read device status to ensure the FEATURES_OK bit is still set [...]\r | |
100 | //\r | |
101 | Status = Virtio10WriteFeatures (VgpuDev->VirtIo, Features, &NextDevStat);\r | |
102 | if (EFI_ERROR (Status)) {\r | |
103 | goto Failed;\r | |
104 | }\r | |
105 | \r | |
106 | //\r | |
107 | // 7. Perform device-specific setup, including discovery of virtqueues for\r | |
108 | // the device [...]\r | |
109 | //\r | |
110 | Status = VgpuDev->VirtIo->SetQueueSel (VgpuDev->VirtIo,\r | |
111 | VIRTIO_GPU_CONTROL_QUEUE);\r | |
112 | if (EFI_ERROR (Status)) {\r | |
113 | goto Failed;\r | |
114 | }\r | |
115 | Status = VgpuDev->VirtIo->GetQueueNumMax (VgpuDev->VirtIo, &QueueSize);\r | |
116 | if (EFI_ERROR (Status)) {\r | |
117 | goto Failed;\r | |
118 | }\r | |
119 | \r | |
120 | //\r | |
121 | // We implement each VirtIo GPU command that we use with two descriptors:\r | |
122 | // request, response.\r | |
123 | //\r | |
124 | if (QueueSize < 2) {\r | |
125 | Status = EFI_UNSUPPORTED;\r | |
126 | goto Failed;\r | |
127 | }\r | |
128 | \r | |
129 | //\r | |
130 | // [...] population of virtqueues [...]\r | |
131 | //\r | |
fc2c1543 | 132 | Status = VirtioRingInit (VgpuDev->VirtIo, QueueSize, &VgpuDev->Ring);\r |
c5f235bb LE |
133 | if (EFI_ERROR (Status)) {\r |
134 | goto Failed;\r | |
135 | }\r | |
9bc5026c LE |
136 | //\r |
137 | // If anything fails from here on, we have to release the ring.\r | |
138 | //\r | |
139 | Status = VirtioRingMap (\r | |
140 | VgpuDev->VirtIo,\r | |
141 | &VgpuDev->Ring,\r | |
142 | &RingBaseShift,\r | |
143 | &VgpuDev->RingMap\r | |
144 | );\r | |
145 | if (EFI_ERROR (Status)) {\r | |
146 | goto ReleaseQueue;\r | |
147 | }\r | |
148 | //\r | |
149 | // If anything fails from here on, we have to unmap the ring.\r | |
150 | //\r | |
53a4c604 BS |
151 | Status = VgpuDev->VirtIo->SetQueueAddress (\r |
152 | VgpuDev->VirtIo,\r | |
153 | &VgpuDev->Ring,\r | |
9bc5026c | 154 | RingBaseShift\r |
53a4c604 | 155 | );\r |
c5f235bb | 156 | if (EFI_ERROR (Status)) {\r |
9bc5026c | 157 | goto UnmapQueue;\r |
c5f235bb LE |
158 | }\r |
159 | \r | |
160 | //\r | |
161 | // 8. Set the DRIVER_OK status bit.\r | |
162 | //\r | |
163 | NextDevStat |= VSTAT_DRIVER_OK;\r | |
164 | Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);\r | |
165 | if (EFI_ERROR (Status)) {\r | |
9bc5026c | 166 | goto UnmapQueue;\r |
c5f235bb LE |
167 | }\r |
168 | \r | |
169 | return EFI_SUCCESS;\r | |
170 | \r | |
9bc5026c LE |
171 | UnmapQueue:\r |
172 | VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, VgpuDev->RingMap);\r | |
173 | \r | |
c5f235bb | 174 | ReleaseQueue:\r |
fc2c1543 | 175 | VirtioRingUninit (VgpuDev->VirtIo, &VgpuDev->Ring);\r |
c5f235bb LE |
176 | \r |
177 | Failed:\r | |
178 | //\r | |
179 | // If any of these steps go irrecoverably wrong, the driver SHOULD set the\r | |
180 | // FAILED status bit to indicate that it has given up on the device (it can\r | |
181 | // reset the device later to restart if desired). [...]\r | |
182 | //\r | |
183 | // VirtIo access failure here should not mask the original error.\r | |
184 | //\r | |
185 | NextDevStat |= VSTAT_FAILED;\r | |
186 | VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);\r | |
187 | \r | |
188 | return Status;\r | |
189 | }\r | |
190 | \r | |
191 | /**\r | |
192 | De-configure the VirtIo GPU device that underlies VgpuDev.\r | |
193 | \r | |
194 | @param[in,out] VgpuDev The VGPU_DEV object to tear down VirtIo messaging\r | |
195 | for. On input, the caller is responsible for having\r | |
196 | called VirtioGpuInit(). On output, VgpuDev->Ring has\r | |
197 | been uninitialized; VirtIo GPU commands (primitives)\r | |
198 | can no longer be submitted to the device.\r | |
199 | **/\r | |
200 | VOID\r | |
201 | VirtioGpuUninit (\r | |
202 | IN OUT VGPU_DEV *VgpuDev\r | |
203 | )\r | |
204 | {\r | |
205 | //\r | |
206 | // Resetting the VirtIo device makes it release its resources and forget its\r | |
207 | // configuration.\r | |
208 | //\r | |
209 | VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, 0);\r | |
9bc5026c | 210 | VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, VgpuDev->RingMap);\r |
fc2c1543 | 211 | VirtioRingUninit (VgpuDev->VirtIo, &VgpuDev->Ring);\r |
c5f235bb LE |
212 | }\r |
213 | \r | |
5c8f091b LE |
214 | /**\r |
215 | Allocate, zero and map memory, for bus master common buffer operation, to be\r | |
216 | attached as backing store to a host-side VirtIo GPU resource.\r | |
217 | \r | |
218 | @param[in] VgpuDev The VGPU_DEV object that represents the VirtIo GPU\r | |
219 | device.\r | |
220 | \r | |
221 | @param[in] NumberOfPages The number of whole pages to allocate and map.\r | |
222 | \r | |
223 | @param[out] HostAddress The system memory address of the allocated area.\r | |
224 | \r | |
225 | @param[out] DeviceAddress The bus master device address of the allocated\r | |
226 | area. The VirtIo GPU device may be programmed to\r | |
227 | access the allocated area through DeviceAddress;\r | |
228 | DeviceAddress is to be passed to the\r | |
229 | VirtioGpuResourceAttachBacking() function, as the\r | |
230 | BackingStoreDeviceAddress parameter.\r | |
231 | \r | |
232 | @param[out] Mapping A resulting token to pass to\r | |
233 | VirtioGpuUnmapAndFreeBackingStore().\r | |
234 | \r | |
235 | @retval EFI_SUCCESS The requested number of pages has been allocated, zeroed\r | |
236 | and mapped.\r | |
237 | \r | |
238 | @return Status codes propagated from\r | |
239 | VgpuDev->VirtIo->AllocateSharedPages() and\r | |
240 | VirtioMapAllBytesInSharedBuffer().\r | |
241 | **/\r | |
242 | EFI_STATUS\r | |
243 | VirtioGpuAllocateZeroAndMapBackingStore (\r | |
244 | IN VGPU_DEV *VgpuDev,\r | |
245 | IN UINTN NumberOfPages,\r | |
246 | OUT VOID **HostAddress,\r | |
247 | OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r | |
248 | OUT VOID **Mapping\r | |
249 | )\r | |
250 | {\r | |
251 | EFI_STATUS Status;\r | |
252 | VOID *NewHostAddress;\r | |
253 | \r | |
254 | Status = VgpuDev->VirtIo->AllocateSharedPages (\r | |
255 | VgpuDev->VirtIo,\r | |
256 | NumberOfPages,\r | |
257 | &NewHostAddress\r | |
258 | );\r | |
259 | if (EFI_ERROR (Status)) {\r | |
260 | return Status;\r | |
261 | }\r | |
262 | \r | |
263 | //\r | |
264 | // Avoid exposing stale data to the device even temporarily: zero the area\r | |
265 | // before mapping it.\r | |
266 | //\r | |
267 | ZeroMem (NewHostAddress, EFI_PAGES_TO_SIZE (NumberOfPages));\r | |
268 | \r | |
269 | Status = VirtioMapAllBytesInSharedBuffer (\r | |
270 | VgpuDev->VirtIo, // VirtIo\r | |
271 | VirtioOperationBusMasterCommonBuffer, // Operation\r | |
272 | NewHostAddress, // HostAddress\r | |
273 | EFI_PAGES_TO_SIZE (NumberOfPages), // NumberOfBytes\r | |
274 | DeviceAddress, // DeviceAddress\r | |
275 | Mapping // Mapping\r | |
276 | );\r | |
277 | if (EFI_ERROR (Status)) {\r | |
278 | goto FreeSharedPages;\r | |
279 | }\r | |
280 | \r | |
281 | *HostAddress = NewHostAddress;\r | |
282 | return EFI_SUCCESS;\r | |
283 | \r | |
284 | FreeSharedPages:\r | |
285 | VgpuDev->VirtIo->FreeSharedPages (\r | |
286 | VgpuDev->VirtIo,\r | |
287 | NumberOfPages,\r | |
288 | NewHostAddress\r | |
289 | );\r | |
290 | return Status;\r | |
291 | }\r | |
292 | \r | |
293 | /**\r | |
294 | Unmap and free memory originally allocated and mapped with\r | |
295 | VirtioGpuAllocateZeroAndMapBackingStore().\r | |
296 | \r | |
297 | If the memory allocated and mapped with\r | |
298 | VirtioGpuAllocateZeroAndMapBackingStore() was attached to a host-side VirtIo\r | |
299 | GPU resource with VirtioGpuResourceAttachBacking(), then the caller is\r | |
300 | responsible for detaching the backing store from the same resource, with\r | |
301 | VirtioGpuResourceDetachBacking(), before calling this function.\r | |
302 | \r | |
303 | @param[in] VgpuDev The VGPU_DEV object that represents the VirtIo GPU\r | |
304 | device.\r | |
305 | \r | |
306 | @param[in] NumberOfPages The NumberOfPages parameter originally passed to\r | |
307 | VirtioGpuAllocateZeroAndMapBackingStore().\r | |
308 | \r | |
309 | @param[in] HostAddress The HostAddress value originally output by\r | |
310 | VirtioGpuAllocateZeroAndMapBackingStore().\r | |
311 | \r | |
312 | @param[in] Mapping The token that was originally output by\r | |
313 | VirtioGpuAllocateZeroAndMapBackingStore().\r | |
314 | **/\r | |
315 | VOID\r | |
316 | VirtioGpuUnmapAndFreeBackingStore (\r | |
317 | IN VGPU_DEV *VgpuDev,\r | |
318 | IN UINTN NumberOfPages,\r | |
319 | IN VOID *HostAddress,\r | |
320 | IN VOID *Mapping\r | |
321 | )\r | |
322 | {\r | |
323 | VgpuDev->VirtIo->UnmapSharedBuffer (\r | |
324 | VgpuDev->VirtIo,\r | |
325 | Mapping\r | |
326 | );\r | |
327 | VgpuDev->VirtIo->FreeSharedPages (\r | |
328 | VgpuDev->VirtIo,\r | |
329 | NumberOfPages,\r | |
330 | HostAddress\r | |
331 | );\r | |
332 | }\r | |
333 | \r | |
c5f235bb LE |
334 | /**\r |
335 | EFI_EVENT_NOTIFY function for the VGPU_DEV.ExitBoot event. It resets the\r | |
336 | VirtIo device, causing it to release its resources and to forget its\r | |
337 | configuration.\r | |
338 | \r | |
339 | This function may only be called (that is, VGPU_DEV.ExitBoot may only be\r | |
340 | signaled) after VirtioGpuInit() returns and before VirtioGpuUninit() is\r | |
341 | called.\r | |
342 | \r | |
343 | @param[in] Event Event whose notification function is being invoked.\r | |
344 | \r | |
345 | @param[in] Context Pointer to the associated VGPU_DEV object.\r | |
346 | **/\r | |
347 | VOID\r | |
348 | EFIAPI\r | |
349 | VirtioGpuExitBoot (\r | |
350 | IN EFI_EVENT Event,\r | |
351 | IN VOID *Context\r | |
352 | )\r | |
353 | {\r | |
354 | VGPU_DEV *VgpuDev;\r | |
355 | \r | |
8ddd12e5 | 356 | DEBUG ((DEBUG_VERBOSE, "%a: Context=0x%p\n", __FUNCTION__, Context));\r |
c5f235bb LE |
357 | VgpuDev = Context;\r |
358 | VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, 0);\r | |
359 | }\r | |
a66ea3b5 LE |
360 | \r |
361 | /**\r | |
362 | Internal utility function that sends a request to the VirtIo GPU device\r | |
363 | model, awaits the answer from the host, and returns a status.\r | |
364 | \r | |
365 | @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU\r | |
366 | device. The caller is responsible to have\r | |
367 | successfully invoked VirtioGpuInit() on VgpuDev\r | |
368 | previously, while VirtioGpuUninit() must not have\r | |
369 | been called on VgpuDev.\r | |
370 | \r | |
371 | @param[in] RequestType The type of the request. The caller is responsible\r | |
372 | for providing a VirtioGpuCmd* RequestType which, on\r | |
373 | success, elicits a VirtioGpuRespOkNodata response\r | |
374 | from the host.\r | |
375 | \r | |
376 | @param[in] Fence Whether to enable fencing for this request. Fencing\r | |
377 | forces the host to complete the command before\r | |
378 | producing a response. If Fence is TRUE, then\r | |
379 | VgpuDev->FenceId is consumed, and incremented.\r | |
380 | \r | |
381 | @param[in,out] Header Pointer to the caller-allocated request object. The\r | |
382 | request must start with VIRTIO_GPU_CONTROL_HEADER.\r | |
383 | This function overwrites all fields of Header before\r | |
384 | submitting the request to the host:\r | |
385 | \r | |
386 | - it sets Type from RequestType,\r | |
387 | \r | |
388 | - it sets Flags and FenceId based on Fence,\r | |
389 | \r | |
390 | - it zeroes CtxId and Padding.\r | |
391 | \r | |
392 | @param[in] RequestSize Size of the entire caller-allocated request object,\r | |
393 | including the leading VIRTIO_GPU_CONTROL_HEADER.\r | |
394 | \r | |
395 | @retval EFI_SUCCESS Operation successful.\r | |
396 | \r | |
397 | @retval EFI_DEVICE_ERROR The host rejected the request. The host error\r | |
398 | code has been logged on the EFI_D_ERROR level.\r | |
399 | \r | |
400 | @return Codes for unexpected errors in VirtIo\r | |
067b6483 LE |
401 | messaging, or request/response\r |
402 | mapping/unmapping.\r | |
a66ea3b5 LE |
403 | **/\r |
404 | STATIC\r | |
405 | EFI_STATUS\r | |
406 | VirtioGpuSendCommand (\r | |
407 | IN OUT VGPU_DEV *VgpuDev,\r | |
408 | IN VIRTIO_GPU_CONTROL_TYPE RequestType,\r | |
409 | IN BOOLEAN Fence,\r | |
410 | IN OUT volatile VIRTIO_GPU_CONTROL_HEADER *Header,\r | |
411 | IN UINTN RequestSize\r | |
412 | )\r | |
413 | {\r | |
414 | DESC_INDICES Indices;\r | |
415 | volatile VIRTIO_GPU_CONTROL_HEADER Response;\r | |
416 | EFI_STATUS Status;\r | |
417 | UINT32 ResponseSize;\r | |
067b6483 LE |
418 | EFI_PHYSICAL_ADDRESS RequestDeviceAddress;\r |
419 | VOID *RequestMap;\r | |
420 | EFI_PHYSICAL_ADDRESS ResponseDeviceAddress;\r | |
421 | VOID *ResponseMap;\r | |
a66ea3b5 LE |
422 | \r |
423 | //\r | |
424 | // Initialize Header.\r | |
425 | //\r | |
426 | Header->Type = RequestType;\r | |
427 | if (Fence) {\r | |
428 | Header->Flags = VIRTIO_GPU_FLAG_FENCE;\r | |
429 | Header->FenceId = VgpuDev->FenceId++;\r | |
430 | } else {\r | |
431 | Header->Flags = 0;\r | |
432 | Header->FenceId = 0;\r | |
433 | }\r | |
434 | Header->CtxId = 0;\r | |
435 | Header->Padding = 0;\r | |
436 | \r | |
437 | ASSERT (RequestSize >= sizeof *Header);\r | |
7f1bf51b | 438 | ASSERT (RequestSize <= MAX_UINT32);\r |
a66ea3b5 | 439 | \r |
067b6483 LE |
440 | //\r |
441 | // Map request and response to bus master device addresses.\r | |
442 | //\r | |
443 | Status = VirtioMapAllBytesInSharedBuffer (\r | |
444 | VgpuDev->VirtIo,\r | |
445 | VirtioOperationBusMasterRead,\r | |
446 | (VOID *)Header,\r | |
447 | RequestSize,\r | |
448 | &RequestDeviceAddress,\r | |
449 | &RequestMap\r | |
450 | );\r | |
451 | if (EFI_ERROR (Status)) {\r | |
452 | return Status;\r | |
453 | }\r | |
454 | Status = VirtioMapAllBytesInSharedBuffer (\r | |
455 | VgpuDev->VirtIo,\r | |
456 | VirtioOperationBusMasterWrite,\r | |
457 | (VOID *)&Response,\r | |
458 | sizeof Response,\r | |
459 | &ResponseDeviceAddress,\r | |
460 | &ResponseMap\r | |
461 | );\r | |
462 | if (EFI_ERROR (Status)) {\r | |
463 | goto UnmapRequest;\r | |
464 | }\r | |
465 | \r | |
a66ea3b5 LE |
466 | //\r |
467 | // Compose the descriptor chain.\r | |
468 | //\r | |
469 | VirtioPrepare (&VgpuDev->Ring, &Indices);\r | |
067b6483 LE |
470 | VirtioAppendDesc (\r |
471 | &VgpuDev->Ring,\r | |
472 | RequestDeviceAddress,\r | |
473 | (UINT32)RequestSize,\r | |
474 | VRING_DESC_F_NEXT,\r | |
475 | &Indices\r | |
476 | );\r | |
477 | VirtioAppendDesc (\r | |
478 | &VgpuDev->Ring,\r | |
479 | ResponseDeviceAddress,\r | |
480 | (UINT32)sizeof Response,\r | |
481 | VRING_DESC_F_WRITE,\r | |
482 | &Indices\r | |
483 | );\r | |
a66ea3b5 LE |
484 | \r |
485 | //\r | |
486 | // Send the command.\r | |
487 | //\r | |
488 | Status = VirtioFlush (VgpuDev->VirtIo, VIRTIO_GPU_CONTROL_QUEUE,\r | |
489 | &VgpuDev->Ring, &Indices, &ResponseSize);\r | |
490 | if (EFI_ERROR (Status)) {\r | |
067b6483 | 491 | goto UnmapResponse;\r |
a66ea3b5 LE |
492 | }\r |
493 | \r | |
494 | //\r | |
067b6483 | 495 | // Verify response size.\r |
a66ea3b5 LE |
496 | //\r |
497 | if (ResponseSize != sizeof Response) {\r | |
498 | DEBUG ((EFI_D_ERROR, "%a: malformed response to Request=0x%x\n",\r | |
499 | __FUNCTION__, (UINT32)RequestType));\r | |
067b6483 LE |
500 | Status = EFI_PROTOCOL_ERROR;\r |
501 | goto UnmapResponse;\r | |
a66ea3b5 LE |
502 | }\r |
503 | \r | |
067b6483 LE |
504 | //\r |
505 | // Unmap response and request, in reverse order of mapping. On error, the\r | |
506 | // respective mapping is invalidated anyway, only the data may not have been\r | |
507 | // committed to system memory (in case of VirtioOperationBusMasterWrite).\r | |
508 | //\r | |
509 | Status = VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, ResponseMap);\r | |
510 | if (EFI_ERROR (Status)) {\r | |
511 | goto UnmapRequest;\r | |
512 | }\r | |
513 | Status = VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, RequestMap);\r | |
514 | if (EFI_ERROR (Status)) {\r | |
515 | return Status;\r | |
516 | }\r | |
517 | \r | |
518 | //\r | |
519 | // Parse the response.\r | |
520 | //\r | |
a66ea3b5 LE |
521 | if (Response.Type == VirtioGpuRespOkNodata) {\r |
522 | return EFI_SUCCESS;\r | |
523 | }\r | |
524 | \r | |
525 | DEBUG ((EFI_D_ERROR, "%a: Request=0x%x Response=0x%x\n", __FUNCTION__,\r | |
526 | (UINT32)RequestType, Response.Type));\r | |
527 | return EFI_DEVICE_ERROR;\r | |
067b6483 LE |
528 | \r |
529 | UnmapResponse:\r | |
530 | VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, ResponseMap);\r | |
531 | \r | |
532 | UnmapRequest:\r | |
533 | VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, RequestMap);\r | |
534 | \r | |
535 | return Status;\r | |
a66ea3b5 LE |
536 | }\r |
537 | \r | |
538 | /**\r | |
539 | The following functions send requests to the VirtIo GPU device model, await\r | |
540 | the answer from the host, and return a status. They share the following\r | |
541 | interface details:\r | |
542 | \r | |
543 | @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU\r | |
544 | device. The caller is responsible to have\r | |
545 | successfully invoked VirtioGpuInit() on VgpuDev\r | |
546 | previously, while VirtioGpuUninit() must not have\r | |
547 | been called on VgpuDev.\r | |
548 | \r | |
549 | @retval EFI_INVALID_PARAMETER Invalid command-specific parameters were\r | |
550 | detected by this driver.\r | |
551 | \r | |
552 | @retval EFI_SUCCESS Operation successful.\r | |
553 | \r | |
554 | @retval EFI_DEVICE_ERROR The host rejected the request. The host error\r | |
555 | code has been logged on the EFI_D_ERROR level.\r | |
556 | \r | |
557 | @return Codes for unexpected errors in VirtIo\r | |
558 | messaging.\r | |
559 | \r | |
560 | For the command-specific parameters, please consult the GPU Device section of\r | |
561 | the VirtIo 1.0 specification (see references in\r | |
562 | "OvmfPkg/Include/IndustryStandard/VirtioGpu.h").\r | |
563 | **/\r | |
564 | EFI_STATUS\r | |
565 | VirtioGpuResourceCreate2d (\r | |
566 | IN OUT VGPU_DEV *VgpuDev,\r | |
567 | IN UINT32 ResourceId,\r | |
568 | IN VIRTIO_GPU_FORMATS Format,\r | |
569 | IN UINT32 Width,\r | |
570 | IN UINT32 Height\r | |
571 | )\r | |
572 | {\r | |
573 | volatile VIRTIO_GPU_RESOURCE_CREATE_2D Request;\r | |
574 | \r | |
575 | if (ResourceId == 0) {\r | |
576 | return EFI_INVALID_PARAMETER;\r | |
577 | }\r | |
578 | \r | |
579 | Request.ResourceId = ResourceId;\r | |
580 | Request.Format = (UINT32)Format;\r | |
581 | Request.Width = Width;\r | |
582 | Request.Height = Height;\r | |
583 | \r | |
584 | return VirtioGpuSendCommand (\r | |
585 | VgpuDev,\r | |
586 | VirtioGpuCmdResourceCreate2d,\r | |
587 | FALSE, // Fence\r | |
588 | &Request.Header,\r | |
589 | sizeof Request\r | |
590 | );\r | |
591 | }\r | |
592 | \r | |
593 | EFI_STATUS\r | |
594 | VirtioGpuResourceUnref (\r | |
595 | IN OUT VGPU_DEV *VgpuDev,\r | |
596 | IN UINT32 ResourceId\r | |
597 | )\r | |
598 | {\r | |
599 | volatile VIRTIO_GPU_RESOURCE_UNREF Request;\r | |
600 | \r | |
601 | if (ResourceId == 0) {\r | |
602 | return EFI_INVALID_PARAMETER;\r | |
603 | }\r | |
604 | \r | |
605 | Request.ResourceId = ResourceId;\r | |
606 | Request.Padding = 0;\r | |
607 | \r | |
608 | return VirtioGpuSendCommand (\r | |
609 | VgpuDev,\r | |
610 | VirtioGpuCmdResourceUnref,\r | |
611 | FALSE, // Fence\r | |
612 | &Request.Header,\r | |
613 | sizeof Request\r | |
614 | );\r | |
615 | }\r | |
616 | \r | |
617 | EFI_STATUS\r | |
618 | VirtioGpuResourceAttachBacking (\r | |
5409c6ab LE |
619 | IN OUT VGPU_DEV *VgpuDev,\r |
620 | IN UINT32 ResourceId,\r | |
621 | IN EFI_PHYSICAL_ADDRESS BackingStoreDeviceAddress,\r | |
622 | IN UINTN NumberOfPages\r | |
a66ea3b5 LE |
623 | )\r |
624 | {\r | |
625 | volatile VIRTIO_GPU_RESOURCE_ATTACH_BACKING Request;\r | |
626 | \r | |
627 | if (ResourceId == 0) {\r | |
628 | return EFI_INVALID_PARAMETER;\r | |
629 | }\r | |
630 | \r | |
631 | Request.ResourceId = ResourceId;\r | |
632 | Request.NrEntries = 1;\r | |
5409c6ab | 633 | Request.Entry.Addr = BackingStoreDeviceAddress;\r |
a66ea3b5 LE |
634 | Request.Entry.Length = (UINT32)EFI_PAGES_TO_SIZE (NumberOfPages);\r |
635 | Request.Entry.Padding = 0;\r | |
636 | \r | |
637 | return VirtioGpuSendCommand (\r | |
638 | VgpuDev,\r | |
639 | VirtioGpuCmdResourceAttachBacking,\r | |
640 | FALSE, // Fence\r | |
641 | &Request.Header,\r | |
642 | sizeof Request\r | |
643 | );\r | |
644 | }\r | |
645 | \r | |
646 | EFI_STATUS\r | |
647 | VirtioGpuResourceDetachBacking (\r | |
648 | IN OUT VGPU_DEV *VgpuDev,\r | |
649 | IN UINT32 ResourceId\r | |
650 | )\r | |
651 | {\r | |
652 | volatile VIRTIO_GPU_RESOURCE_DETACH_BACKING Request;\r | |
653 | \r | |
654 | if (ResourceId == 0) {\r | |
655 | return EFI_INVALID_PARAMETER;\r | |
656 | }\r | |
657 | \r | |
658 | Request.ResourceId = ResourceId;\r | |
659 | Request.Padding = 0;\r | |
660 | \r | |
661 | //\r | |
662 | // In this case, we set Fence to TRUE, because after this function returns,\r | |
663 | // the caller might reasonably want to repurpose the backing pages\r | |
664 | // immediately. Thus we should ensure that the host releases all references\r | |
665 | // to the backing pages before we return.\r | |
666 | //\r | |
667 | return VirtioGpuSendCommand (\r | |
668 | VgpuDev,\r | |
669 | VirtioGpuCmdResourceDetachBacking,\r | |
670 | TRUE, // Fence\r | |
671 | &Request.Header,\r | |
672 | sizeof Request\r | |
673 | );\r | |
674 | }\r | |
675 | \r | |
676 | EFI_STATUS\r | |
677 | VirtioGpuSetScanout (\r | |
678 | IN OUT VGPU_DEV *VgpuDev,\r | |
679 | IN UINT32 X,\r | |
680 | IN UINT32 Y,\r | |
681 | IN UINT32 Width,\r | |
682 | IN UINT32 Height,\r | |
683 | IN UINT32 ScanoutId,\r | |
684 | IN UINT32 ResourceId\r | |
685 | )\r | |
686 | {\r | |
687 | volatile VIRTIO_GPU_SET_SCANOUT Request;\r | |
688 | \r | |
689 | //\r | |
690 | // Unlike for most other commands, ResourceId=0 is valid; it\r | |
691 | // is used to disable a scanout.\r | |
692 | //\r | |
693 | Request.Rectangle.X = X;\r | |
694 | Request.Rectangle.Y = Y;\r | |
695 | Request.Rectangle.Width = Width;\r | |
696 | Request.Rectangle.Height = Height;\r | |
697 | Request.ScanoutId = ScanoutId;\r | |
698 | Request.ResourceId = ResourceId;\r | |
699 | \r | |
700 | return VirtioGpuSendCommand (\r | |
701 | VgpuDev,\r | |
702 | VirtioGpuCmdSetScanout,\r | |
703 | FALSE, // Fence\r | |
704 | &Request.Header,\r | |
705 | sizeof Request\r | |
706 | );\r | |
707 | }\r | |
708 | \r | |
709 | EFI_STATUS\r | |
710 | VirtioGpuTransferToHost2d (\r | |
711 | IN OUT VGPU_DEV *VgpuDev,\r | |
712 | IN UINT32 X,\r | |
713 | IN UINT32 Y,\r | |
714 | IN UINT32 Width,\r | |
715 | IN UINT32 Height,\r | |
716 | IN UINT64 Offset,\r | |
717 | IN UINT32 ResourceId\r | |
718 | )\r | |
719 | {\r | |
720 | volatile VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D Request;\r | |
721 | \r | |
722 | if (ResourceId == 0) {\r | |
723 | return EFI_INVALID_PARAMETER;\r | |
724 | }\r | |
725 | \r | |
726 | Request.Rectangle.X = X;\r | |
727 | Request.Rectangle.Y = Y;\r | |
728 | Request.Rectangle.Width = Width;\r | |
729 | Request.Rectangle.Height = Height;\r | |
730 | Request.Offset = Offset;\r | |
731 | Request.ResourceId = ResourceId;\r | |
732 | Request.Padding = 0;\r | |
733 | \r | |
734 | return VirtioGpuSendCommand (\r | |
735 | VgpuDev,\r | |
736 | VirtioGpuCmdTransferToHost2d,\r | |
737 | FALSE, // Fence\r | |
738 | &Request.Header,\r | |
739 | sizeof Request\r | |
740 | );\r | |
741 | }\r | |
742 | \r | |
743 | EFI_STATUS\r | |
744 | VirtioGpuResourceFlush (\r | |
745 | IN OUT VGPU_DEV *VgpuDev,\r | |
746 | IN UINT32 X,\r | |
747 | IN UINT32 Y,\r | |
748 | IN UINT32 Width,\r | |
749 | IN UINT32 Height,\r | |
750 | IN UINT32 ResourceId\r | |
751 | )\r | |
752 | {\r | |
753 | volatile VIRTIO_GPU_RESOURCE_FLUSH Request;\r | |
754 | \r | |
755 | if (ResourceId == 0) {\r | |
756 | return EFI_INVALID_PARAMETER;\r | |
757 | }\r | |
758 | \r | |
759 | Request.Rectangle.X = X;\r | |
760 | Request.Rectangle.Y = Y;\r | |
761 | Request.Rectangle.Width = Width;\r | |
762 | Request.Rectangle.Height = Height;\r | |
763 | Request.ResourceId = ResourceId;\r | |
764 | Request.Padding = 0;\r | |
765 | \r | |
766 | return VirtioGpuSendCommand (\r | |
767 | VgpuDev,\r | |
768 | VirtioGpuCmdResourceFlush,\r | |
769 | FALSE, // Fence\r | |
770 | &Request.Header,\r | |
771 | sizeof Request\r | |
772 | );\r | |
773 | }\r |