]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/VirtioGpuDxe/DriverBinding.c
OvmfPkg/VirtioLib: take VirtIo instance in VirtioRingInit/VirtioRingUninit
[mirror_edk2.git] / OvmfPkg / VirtioGpuDxe / DriverBinding.c
CommitLineData
a2a4fa66
LE
1/** @file\r
2\r
3 Implement the Driver Binding Protocol and the Component Name 2 Protocol for\r
4 the Virtio GPU hybrid driver.\r
5\r
6 Copyright (C) 2016, Red Hat, Inc.\r
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
18#include <Library/BaseMemoryLib.h>\r
19#include <Library/DevicePathLib.h>\r
20#include <Library/MemoryAllocationLib.h>\r
21#include <Library/PrintLib.h>\r
22#include <Library/UefiBootServicesTableLib.h>\r
23#include <Library/UefiLib.h>\r
24#include <Protocol/ComponentName2.h>\r
25#include <Protocol/DevicePath.h>\r
26#include <Protocol/DriverBinding.h>\r
27#include <Protocol/PciIo.h>\r
28\r
29#include "VirtioGpu.h"\r
30\r
a2a4fa66
LE
31//\r
32// The device path node that describes the Video Output Device Attributes for\r
33// the single head (UEFI child handle) that we support.\r
34//\r
35// The ACPI_DISPLAY_ADR() macro corresponds to Table B-2, section "B.4.2 _DOD"\r
36// in the ACPI 3.0b spec, or more recently, to Table B-379, section "B.3.2\r
37// _DOD" in the ACPI 6.0 spec.\r
38//\r
39STATIC CONST ACPI_ADR_DEVICE_PATH mAcpiAdr = {\r
40 { // Header\r
41 ACPI_DEVICE_PATH, // Type\r
42 ACPI_ADR_DP, // SubType\r
43 { sizeof mAcpiAdr, 0 }, // Length\r
44 },\r
45 ACPI_DISPLAY_ADR ( // ADR\r
46 1, // DeviceIdScheme: use the ACPI\r
47 // bit-field definitions\r
48 0, // HeadId\r
49 0, // NonVgaOutput\r
50 1, // BiosCanDetect\r
51 0, // VendorInfo\r
52 ACPI_ADR_DISPLAY_TYPE_EXTERNAL_DIGITAL, // Type\r
53 0, // Port\r
54 0 // Index\r
55 )\r
56};\r
57\r
58//\r
59// Component Name 2 Protocol implementation.\r
60//\r
61STATIC CONST EFI_UNICODE_STRING_TABLE mDriverNameTable[] = {\r
62 { "en", L"Virtio GPU Driver" },\r
63 { NULL, NULL }\r
64};\r
65\r
66STATIC\r
67EFI_STATUS\r
68EFIAPI\r
69VirtioGpuGetDriverName (\r
70 IN EFI_COMPONENT_NAME2_PROTOCOL *This,\r
71 IN CHAR8 *Language,\r
72 OUT CHAR16 **DriverName\r
73 )\r
74{\r
75 return LookupUnicodeString2 (Language, This->SupportedLanguages,\r
76 mDriverNameTable, DriverName, FALSE /* Iso639Language */);\r
77}\r
78\r
79STATIC\r
80EFI_STATUS\r
81EFIAPI\r
82VirtioGpuGetControllerName (\r
83 IN EFI_COMPONENT_NAME2_PROTOCOL *This,\r
84 IN EFI_HANDLE ControllerHandle,\r
85 IN EFI_HANDLE ChildHandle OPTIONAL,\r
86 IN CHAR8 *Language,\r
87 OUT CHAR16 **ControllerName\r
88 )\r
89{\r
90 EFI_STATUS Status;\r
91 VGPU_DEV *VgpuDev;\r
92\r
93 //\r
94 // Look up the VGPU_DEV "protocol interface" on ControllerHandle.\r
95 //\r
96 Status = gBS->OpenProtocol (ControllerHandle, &gEfiCallerIdGuid,\r
97 (VOID **)&VgpuDev, gImageHandle, ControllerHandle,\r
98 EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
99 if (EFI_ERROR (Status)) {\r
100 return Status;\r
101 }\r
102 //\r
103 // Sanity check: if we found gEfiCallerIdGuid on ControllerHandle, then we\r
104 // keep its Virtio Device Protocol interface open BY_DRIVER.\r
105 //\r
106 ASSERT_EFI_ERROR (EfiTestManagedDevice (ControllerHandle, gImageHandle,\r
107 &gVirtioDeviceProtocolGuid));\r
108\r
109 if (ChildHandle == NULL) {\r
110 //\r
111 // The caller is querying the name of the VGPU_DEV controller.\r
112 //\r
113 return LookupUnicodeString2 (Language, This->SupportedLanguages,\r
114 VgpuDev->BusName, ControllerName, FALSE /* Iso639Language */);\r
115 }\r
116\r
117 //\r
118 // Otherwise, the caller is looking for the name of the GOP child controller.\r
119 // Check if it is asking about the GOP child controller that we manage. (The\r
120 // condition below covers the case when we haven't produced the GOP child\r
121 // controller yet, or we've destroyed it since.)\r
122 //\r
123 if (VgpuDev->Child == NULL || ChildHandle != VgpuDev->Child->GopHandle) {\r
124 return EFI_UNSUPPORTED;\r
125 }\r
126 //\r
127 // Sanity check: our GOP child controller keeps the VGPU_DEV controller's\r
128 // Virtio Device Protocol interface open BY_CHILD_CONTROLLER.\r
129 //\r
130 ASSERT_EFI_ERROR (EfiTestChildHandle (ControllerHandle, ChildHandle,\r
131 &gVirtioDeviceProtocolGuid));\r
132\r
133 return LookupUnicodeString2 (Language, This->SupportedLanguages,\r
134 VgpuDev->Child->GopName, ControllerName,\r
135 FALSE /* Iso639Language */);\r
136}\r
137\r
138STATIC CONST EFI_COMPONENT_NAME2_PROTOCOL mComponentName2 = {\r
139 VirtioGpuGetDriverName,\r
140 VirtioGpuGetControllerName,\r
141 "en" // SupportedLanguages (RFC 4646)\r
142};\r
143\r
144//\r
145// Helper functions for the Driver Binding Protocol Implementation.\r
146//\r
147/**\r
148 Format the VGPU_DEV controller name, to be looked up and returned by\r
149 VirtioGpuGetControllerName().\r
150\r
151 @param[in] ControllerHandle The handle that identifies the VGPU_DEV\r
152 controller.\r
153\r
154 @param[in] AgentHandle The handle of the agent that will attempt to\r
155 temporarily open the PciIo protocol. This is the\r
156 DriverBindingHandle member of the\r
157 EFI_DRIVER_BINDING_PROTOCOL whose Start()\r
158 function is calling this function.\r
159\r
160 @param[in] DevicePath The device path that is installed on\r
161 ControllerHandle.\r
162\r
163 @param[out] ControllerName A dynamically allocated unicode string that\r
164 unconditionally says "Virtio GPU Device", with a\r
165 PCI Segment:Bus:Device.Function location\r
166 optionally appended. The latter part is only\r
167 produced if DevicePath contains at least one\r
168 PciIo node; in that case, the most specific such\r
169 node is used for retrieving the location info.\r
170 The caller is responsible for freeing\r
171 ControllerName after use.\r
172\r
173 @retval EFI_SUCCESS ControllerName has been formatted.\r
174\r
175 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for ControllerName.\r
176**/\r
177STATIC\r
178EFI_STATUS\r
179FormatVgpuDevName (\r
180 IN EFI_HANDLE ControllerHandle,\r
181 IN EFI_HANDLE AgentHandle,\r
182 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
183 OUT CHAR16 **ControllerName\r
184 )\r
185{\r
186 EFI_HANDLE PciIoHandle;\r
187 EFI_PCI_IO_PROTOCOL *PciIo;\r
188 UINTN Segment, Bus, Device, Function;\r
189 STATIC CONST CHAR16 ControllerNameStem[] = L"Virtio GPU Device";\r
190 UINTN ControllerNameSize;\r
191\r
192 if (EFI_ERROR (gBS->LocateDevicePath (&gEfiPciIoProtocolGuid, &DevicePath,\r
193 &PciIoHandle)) ||\r
194 EFI_ERROR (gBS->OpenProtocol (PciIoHandle, &gEfiPciIoProtocolGuid,\r
195 (VOID **)&PciIo, AgentHandle, ControllerHandle,\r
196 EFI_OPEN_PROTOCOL_GET_PROTOCOL)) ||\r
197 EFI_ERROR (PciIo->GetLocation (PciIo, &Segment, &Bus, &Device,\r
198 &Function))) {\r
199 //\r
200 // Failed to retrieve location info, return verbatim copy of static string.\r
201 //\r
202 *ControllerName = AllocateCopyPool (sizeof ControllerNameStem,\r
203 ControllerNameStem);\r
204 return (*ControllerName == NULL) ? EFI_OUT_OF_RESOURCES : EFI_SUCCESS;\r
205 }\r
206 //\r
207 // Location info available, format ControllerName dynamically.\r
208 //\r
209 ControllerNameSize = sizeof ControllerNameStem + // includes L'\0'\r
210 sizeof (CHAR16) * (1 + 4 + // Segment\r
211 1 + 2 + // Bus\r
212 1 + 2 + // Device\r
213 1 + 1 // Function\r
214 );\r
215 *ControllerName = AllocatePool (ControllerNameSize);\r
216 if (*ControllerName == NULL) {\r
217 return EFI_OUT_OF_RESOURCES;\r
218 }\r
219\r
220 UnicodeSPrintAsciiFormat (*ControllerName, ControllerNameSize,\r
221 "%s %04x:%02x:%02x.%x", ControllerNameStem, (UINT32)Segment, (UINT32)Bus,\r
222 (UINT32)Device, (UINT32)Function);\r
223 return EFI_SUCCESS;\r
224}\r
225\r
226/**\r
227 Dynamically allocate and initialize the VGPU_GOP child object within an\r
228 otherwise configured parent VGPU_DEV object.\r
229\r
230 This function adds a BY_CHILD_CONTROLLER reference to ParentBusController's\r
231 VIRTIO_DEVICE_PROTOCOL interface.\r
232\r
233 @param[in,out] ParentBus The pre-initialized VGPU_DEV object that the\r
234 newly created VGPU_GOP object will be the\r
235 child of.\r
236\r
237 @param[in] ParentDevicePath The device path protocol instance that is\r
238 installed on ParentBusController.\r
239\r
240 @param[in] ParentBusController The UEFI controller handle on which the\r
241 ParentBus VGPU_DEV object and the\r
242 ParentDevicePath device path protocol are\r
243 installed.\r
244\r
245 @param[in] DriverBindingHandle The DriverBindingHandle member of\r
246 EFI_DRIVER_BINDING_PROTOCOL whose Start()\r
247 function is calling this function. It is\r
248 passed as AgentHandle to gBS->OpenProtocol()\r
249 when creating the BY_CHILD_CONTROLLER\r
250 reference.\r
251\r
252 @retval EFI_SUCCESS ParentBus->Child has been created and\r
253 populated, and ParentBus->Child->GopHandle now\r
254 references ParentBusController->VirtIo\r
255 BY_CHILD_CONTROLLER.\r
256\r
257 @retval EFI_OUT_OF_RESOURCES Memory allocation failed.\r
258\r
259 @return Error codes from underlying functions.\r
260**/\r
261STATIC\r
262EFI_STATUS\r
263InitVgpuGop (\r
264 IN OUT VGPU_DEV *ParentBus,\r
265 IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath,\r
266 IN EFI_HANDLE ParentBusController,\r
267 IN EFI_HANDLE DriverBindingHandle\r
268 )\r
269{\r
270 VGPU_GOP *VgpuGop;\r
271 EFI_STATUS Status;\r
272 CHAR16 *ParentBusName;\r
273 STATIC CONST CHAR16 NameSuffix[] = L" Head #0";\r
274 UINTN NameSize;\r
275 CHAR16 *Name;\r
276 EFI_TPL OldTpl;\r
277 VOID *ParentVirtIo;\r
278\r
279 VgpuGop = AllocateZeroPool (sizeof *VgpuGop);\r
280 if (VgpuGop == NULL) {\r
281 return EFI_OUT_OF_RESOURCES;\r
282 }\r
283\r
284 VgpuGop->Signature = VGPU_GOP_SIG;\r
285 VgpuGop->ParentBus = ParentBus;\r
286\r
287 //\r
288 // Format a human-readable controller name for VGPU_GOP, and stash it for\r
289 // VirtioGpuGetControllerName() to look up. We simply append NameSuffix to\r
290 // ParentBus->BusName.\r
291 //\r
292 Status = LookupUnicodeString2 ("en", mComponentName2.SupportedLanguages,\r
293 ParentBus->BusName, &ParentBusName, FALSE /* Iso639Language */);\r
294 ASSERT_EFI_ERROR (Status);\r
295 NameSize = StrSize (ParentBusName) - sizeof (CHAR16) + sizeof NameSuffix;\r
296 Name = AllocatePool (NameSize);\r
297 if (Name == NULL) {\r
298 Status = EFI_OUT_OF_RESOURCES;\r
299 goto FreeVgpuGop;\r
300 }\r
301 UnicodeSPrintAsciiFormat (Name, NameSize, "%s%s", ParentBusName, NameSuffix);\r
302 Status = AddUnicodeString2 ("en", mComponentName2.SupportedLanguages,\r
303 &VgpuGop->GopName, Name, FALSE /* Iso639Language */);\r
304 FreePool (Name);\r
305 if (EFI_ERROR (Status)) {\r
306 goto FreeVgpuGop;\r
307 }\r
308\r
309 //\r
310 // Create the child device path.\r
311 //\r
312 VgpuGop->GopDevicePath = AppendDevicePathNode (ParentDevicePath,\r
313 &mAcpiAdr.Header);\r
314 if (VgpuGop->GopDevicePath == NULL) {\r
315 Status = EFI_OUT_OF_RESOURCES;\r
316 goto FreeVgpuGopName;\r
317 }\r
318\r
319 //\r
320 // Mask protocol notify callbacks until we're done.\r
321 //\r
322 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
323\r
324 //\r
325 // Create the child handle with the child device path.\r
326 //\r
327 Status = gBS->InstallProtocolInterface (&VgpuGop->GopHandle,\r
328 &gEfiDevicePathProtocolGuid, EFI_NATIVE_INTERFACE,\r
329 VgpuGop->GopDevicePath);\r
330 if (EFI_ERROR (Status)) {\r
331 goto FreeDevicePath;\r
332 }\r
333\r
334 //\r
335 // The child handle must present a reference to the parent handle's Virtio\r
336 // Device Protocol interface.\r
337 //\r
338 Status = gBS->OpenProtocol (ParentBusController, &gVirtioDeviceProtocolGuid,\r
339 &ParentVirtIo, DriverBindingHandle, VgpuGop->GopHandle,\r
340 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);\r
341 if (EFI_ERROR (Status)) {\r
342 goto UninstallDevicePath;\r
343 }\r
344 ASSERT (ParentVirtIo == ParentBus->VirtIo);\r
345\r
346 //\r
347 // Initialize our Graphics Output Protocol.\r
348 //\r
8731debe
LE
349 // Fill in the function members of VgpuGop->Gop from the template, then set\r
350 // up the rest of the GOP infrastructure by calling SetMode() right now.\r
a2a4fa66 351 //\r
8731debe
LE
352 CopyMem (&VgpuGop->Gop, &mGopTemplate, sizeof mGopTemplate);\r
353 Status = VgpuGop->Gop.SetMode (&VgpuGop->Gop, 0);\r
a2a4fa66
LE
354 if (EFI_ERROR (Status)) {\r
355 goto CloseVirtIoByChild;\r
356 }\r
357\r
358 //\r
359 // Install the Graphics Output Protocol on the child handle.\r
360 //\r
361 Status = gBS->InstallProtocolInterface (&VgpuGop->GopHandle,\r
8731debe 362 &gEfiGraphicsOutputProtocolGuid, EFI_NATIVE_INTERFACE,\r
a2a4fa66
LE
363 &VgpuGop->Gop);\r
364 if (EFI_ERROR (Status)) {\r
365 goto UninitGop;\r
366 }\r
367\r
368 //\r
369 // We're done.\r
370 //\r
371 gBS->RestoreTPL (OldTpl);\r
372 ParentBus->Child = VgpuGop;\r
373 return EFI_SUCCESS;\r
374\r
375UninitGop:\r
8731debe 376 ReleaseGopResources (VgpuGop, TRUE /* DisableHead */);\r
a2a4fa66
LE
377\r
378CloseVirtIoByChild:\r
379 gBS->CloseProtocol (ParentBusController, &gVirtioDeviceProtocolGuid,\r
380 DriverBindingHandle, VgpuGop->GopHandle);\r
381\r
382UninstallDevicePath:\r
383 gBS->UninstallProtocolInterface (VgpuGop->GopHandle,\r
384 &gEfiDevicePathProtocolGuid, VgpuGop->GopDevicePath);\r
385\r
386FreeDevicePath:\r
387 gBS->RestoreTPL (OldTpl);\r
388 FreePool (VgpuGop->GopDevicePath);\r
389\r
390FreeVgpuGopName:\r
391 FreeUnicodeStringTable (VgpuGop->GopName);\r
392\r
393FreeVgpuGop:\r
394 FreePool (VgpuGop);\r
395\r
396 return Status;\r
397}\r
398\r
399/**\r
400 Tear down and release the VGPU_GOP child object within the VGPU_DEV parent\r
401 object.\r
402\r
403 This function removes the BY_CHILD_CONTROLLER reference from\r
404 ParentBusController's VIRTIO_DEVICE_PROTOCOL interface.\r
405\r
406 @param[in,out] ParentBus The VGPU_DEV object that the VGPU_GOP child\r
407 object will be removed from.\r
408\r
409 @param[in] ParentBusController The UEFI controller handle on which the\r
410 ParentBus VGPU_DEV object is installed.\r
411\r
412 @param[in] DriverBindingHandle The DriverBindingHandle member of\r
413 EFI_DRIVER_BINDING_PROTOCOL whose Stop()\r
414 function is calling this function. It is\r
415 passed as AgentHandle to gBS->CloseProtocol()\r
416 when removing the BY_CHILD_CONTROLLER\r
417 reference.\r
418**/\r
419STATIC\r
420VOID\r
421UninitVgpuGop (\r
422 IN OUT VGPU_DEV *ParentBus,\r
423 IN EFI_HANDLE ParentBusController,\r
424 IN EFI_HANDLE DriverBindingHandle\r
425 )\r
426{\r
427 VGPU_GOP *VgpuGop;\r
428 EFI_STATUS Status;\r
429\r
430 VgpuGop = ParentBus->Child;\r
431 Status = gBS->UninstallProtocolInterface (VgpuGop->GopHandle,\r
8731debe 432 &gEfiGraphicsOutputProtocolGuid, &VgpuGop->Gop);\r
a2a4fa66
LE
433 ASSERT_EFI_ERROR (Status);\r
434\r
435 //\r
436 // Uninitialize VgpuGop->Gop.\r
437 //\r
8731debe 438 ReleaseGopResources (VgpuGop, TRUE /* DisableHead */);\r
a2a4fa66
LE
439\r
440 Status = gBS->CloseProtocol (ParentBusController, &gVirtioDeviceProtocolGuid,\r
441 DriverBindingHandle, VgpuGop->GopHandle);\r
442 ASSERT_EFI_ERROR (Status);\r
443\r
444 Status = gBS->UninstallProtocolInterface (VgpuGop->GopHandle,\r
445 &gEfiDevicePathProtocolGuid, VgpuGop->GopDevicePath);\r
446 ASSERT_EFI_ERROR (Status);\r
447\r
448 FreePool (VgpuGop->GopDevicePath);\r
449 FreeUnicodeStringTable (VgpuGop->GopName);\r
450 FreePool (VgpuGop);\r
451\r
452 ParentBus->Child = NULL;\r
453}\r
454\r
455//\r
456// Driver Binding Protocol Implementation.\r
457//\r
458STATIC\r
459EFI_STATUS\r
460EFIAPI\r
461VirtioGpuDriverBindingSupported (\r
462 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
463 IN EFI_HANDLE ControllerHandle,\r
464 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
465 )\r
466{\r
467 EFI_STATUS Status;\r
468 VIRTIO_DEVICE_PROTOCOL *VirtIo;\r
469\r
470 //\r
471 // - If RemainingDevicePath is NULL: the caller is interested in creating all\r
472 // child handles.\r
473 // - If RemainingDevicePath points to an end node: the caller is not\r
474 // interested in creating any child handle.\r
475 // - Otherwise, the caller would like to create the one child handle\r
476 // specified in RemainingDevicePath. In this case we have to see if the\r
477 // requested device path is supportable.\r
478 //\r
479 if (RemainingDevicePath != NULL &&\r
480 !IsDevicePathEnd (RemainingDevicePath) &&\r
481 (DevicePathNodeLength (RemainingDevicePath) != sizeof mAcpiAdr ||\r
482 CompareMem (RemainingDevicePath, &mAcpiAdr, sizeof mAcpiAdr) != 0)) {\r
483 return EFI_UNSUPPORTED;\r
484 }\r
485\r
486 //\r
487 // Open the Virtio Device Protocol interface on the controller, BY_DRIVER.\r
488 //\r
489 Status = gBS->OpenProtocol (ControllerHandle, &gVirtioDeviceProtocolGuid,\r
490 (VOID **)&VirtIo, This->DriverBindingHandle,\r
491 ControllerHandle, EFI_OPEN_PROTOCOL_BY_DRIVER);\r
492 if (EFI_ERROR (Status)) {\r
493 //\r
494 // If this fails, then by default we cannot support ControllerHandle. There\r
495 // is one exception: we've already bound the device, have not produced any\r
496 // GOP child controller, and now the caller wants us to produce the child\r
497 // controller (either specifically or as part of "all children"). That's\r
498 // allowed.\r
499 //\r
500 if (Status == EFI_ALREADY_STARTED) {\r
501 EFI_STATUS Status2;\r
502 VGPU_DEV *VgpuDev;\r
503\r
504 Status2 = gBS->OpenProtocol (ControllerHandle, &gEfiCallerIdGuid,\r
505 (VOID **)&VgpuDev, This->DriverBindingHandle,\r
506 ControllerHandle, EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
507 ASSERT_EFI_ERROR (Status2);\r
508\r
509 if (VgpuDev->Child == NULL &&\r
510 (RemainingDevicePath == NULL ||\r
511 !IsDevicePathEnd (RemainingDevicePath))) {\r
512 Status = EFI_SUCCESS;\r
513 }\r
514 }\r
515\r
516 return Status;\r
517 }\r
518\r
519 //\r
520 // First BY_DRIVER open; check the VirtIo revision and subsystem.\r
521 //\r
522 if (VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0) ||\r
523 VirtIo->SubSystemDeviceId != VIRTIO_SUBSYSTEM_GPU_DEVICE) {\r
524 Status = EFI_UNSUPPORTED;\r
525 goto CloseVirtIo;\r
526 }\r
527\r
528 //\r
529 // We'll need the device path of the VirtIo device both for formatting\r
530 // VGPU_DEV.BusName and for populating VGPU_GOP.GopDevicePath.\r
531 //\r
532 Status = gBS->OpenProtocol (ControllerHandle, &gEfiDevicePathProtocolGuid,\r
533 NULL, This->DriverBindingHandle, ControllerHandle,\r
534 EFI_OPEN_PROTOCOL_TEST_PROTOCOL);\r
535\r
536CloseVirtIo:\r
537 gBS->CloseProtocol (ControllerHandle, &gVirtioDeviceProtocolGuid,\r
538 This->DriverBindingHandle, ControllerHandle);\r
539\r
540 return Status;\r
541}\r
542\r
543STATIC\r
544EFI_STATUS\r
545EFIAPI\r
546VirtioGpuDriverBindingStart (\r
547 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
548 IN EFI_HANDLE ControllerHandle,\r
549 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
550 )\r
551{\r
552 EFI_STATUS Status;\r
553 VIRTIO_DEVICE_PROTOCOL *VirtIo;\r
554 BOOLEAN VirtIoBoundJustNow;\r
555 VGPU_DEV *VgpuDev;\r
556 EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
557\r
558 //\r
559 // Open the Virtio Device Protocol.\r
560 //\r
561 // The result of this operation, combined with the checks in\r
562 // VirtioGpuDriverBindingSupported(), uniquely tells us whether we are\r
563 // binding the VirtIo controller on this call (with or without creating child\r
564 // controllers), or else we're *only* creating child controllers.\r
565 //\r
566 Status = gBS->OpenProtocol (ControllerHandle, &gVirtioDeviceProtocolGuid,\r
567 (VOID **)&VirtIo, This->DriverBindingHandle,\r
568 ControllerHandle, EFI_OPEN_PROTOCOL_BY_DRIVER);\r
569 if (EFI_ERROR (Status)) {\r
570 //\r
571 // The assertions below are based on the success of\r
572 // VirtioGpuDriverBindingSupported(): we bound ControllerHandle earlier,\r
573 // without producing child handles, and now we're producing the GOP child\r
574 // handle only.\r
575 //\r
576 ASSERT (Status == EFI_ALREADY_STARTED);\r
577\r
578 Status = gBS->OpenProtocol (ControllerHandle, &gEfiCallerIdGuid,\r
579 (VOID **)&VgpuDev, This->DriverBindingHandle,\r
580 ControllerHandle, EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
581 ASSERT_EFI_ERROR (Status);\r
582\r
583 ASSERT (VgpuDev->Child == NULL);\r
584 ASSERT (\r
585 RemainingDevicePath == NULL || !IsDevicePathEnd (RemainingDevicePath));\r
586\r
587 VirtIoBoundJustNow = FALSE;\r
588 } else {\r
589 VirtIoBoundJustNow = TRUE;\r
590\r
591 //\r
592 // Allocate the private structure.\r
593 //\r
594 VgpuDev = AllocateZeroPool (sizeof *VgpuDev);\r
595 if (VgpuDev == NULL) {\r
596 Status = EFI_OUT_OF_RESOURCES;\r
597 goto CloseVirtIo;\r
598 }\r
599 VgpuDev->VirtIo = VirtIo;\r
600 }\r
601\r
602 //\r
603 // Grab the VirtIo controller's device path. This is necessary regardless of\r
604 // VirtIoBoundJustNow.\r
605 //\r
606 Status = gBS->OpenProtocol (ControllerHandle, &gEfiDevicePathProtocolGuid,\r
607 (VOID **)&DevicePath, This->DriverBindingHandle,\r
608 ControllerHandle, EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
609 if (EFI_ERROR (Status)) {\r
610 goto FreeVgpuDev;\r
611 }\r
612\r
613 //\r
614 // Create VGPU_DEV if we've bound the VirtIo controller right now (that is,\r
615 // if we aren't *only* creating child handles).\r
616 //\r
617 if (VirtIoBoundJustNow) {\r
618 CHAR16 *VgpuDevName;\r
619\r
620 //\r
621 // Format a human-readable controller name for VGPU_DEV, and stash it for\r
622 // VirtioGpuGetControllerName() to look up.\r
623 //\r
624 Status = FormatVgpuDevName (ControllerHandle, This->DriverBindingHandle,\r
625 DevicePath, &VgpuDevName);\r
626 if (EFI_ERROR (Status)) {\r
627 goto FreeVgpuDev;\r
628 }\r
629 Status = AddUnicodeString2 ("en", mComponentName2.SupportedLanguages,\r
630 &VgpuDev->BusName, VgpuDevName, FALSE /* Iso639Language */);\r
631 FreePool (VgpuDevName);\r
632 if (EFI_ERROR (Status)) {\r
633 goto FreeVgpuDev;\r
634 }\r
635\r
c5f235bb
LE
636 Status = VirtioGpuInit (VgpuDev);\r
637 if (EFI_ERROR (Status)) {\r
638 goto FreeVgpuDevBusName;\r
639 }\r
640\r
641 Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,\r
642 VirtioGpuExitBoot, VgpuDev /* NotifyContext */,\r
643 &VgpuDev->ExitBoot);\r
644 if (EFI_ERROR (Status)) {\r
645 goto UninitGpu;\r
646 }\r
647\r
a2a4fa66
LE
648 //\r
649 // Install the VGPU_DEV "protocol interface" on ControllerHandle.\r
650 //\r
651 Status = gBS->InstallProtocolInterface (&ControllerHandle,\r
652 &gEfiCallerIdGuid, EFI_NATIVE_INTERFACE, VgpuDev);\r
653 if (EFI_ERROR (Status)) {\r
c5f235bb 654 goto CloseExitBoot;\r
a2a4fa66
LE
655 }\r
656\r
657 if (RemainingDevicePath != NULL && IsDevicePathEnd (RemainingDevicePath)) {\r
658 //\r
659 // No child handle should be produced; we're done.\r
660 //\r
661 DEBUG ((EFI_D_INFO, "%a: bound VirtIo=%p without producing GOP\n",\r
662 __FUNCTION__, (VOID *)VgpuDev->VirtIo));\r
663 return EFI_SUCCESS;\r
664 }\r
665 }\r
666\r
667 //\r
668 // Below we'll produce our single child handle: the caller requested it\r
669 // either specifically, or as part of all child handles.\r
670 //\r
671 ASSERT (VgpuDev->Child == NULL);\r
672 ASSERT (\r
673 RemainingDevicePath == NULL || !IsDevicePathEnd (RemainingDevicePath));\r
674\r
675 Status = InitVgpuGop (VgpuDev, DevicePath, ControllerHandle,\r
676 This->DriverBindingHandle);\r
677 if (EFI_ERROR (Status)) {\r
678 goto UninstallVgpuDev;\r
679 }\r
680\r
681 //\r
682 // We're done.\r
683 //\r
684 DEBUG ((EFI_D_INFO, "%a: produced GOP %a VirtIo=%p\n", __FUNCTION__,\r
685 VirtIoBoundJustNow ? "while binding" : "for pre-bound",\r
686 (VOID *)VgpuDev->VirtIo));\r
687 return EFI_SUCCESS;\r
688\r
689UninstallVgpuDev:\r
690 if (VirtIoBoundJustNow) {\r
691 gBS->UninstallProtocolInterface (ControllerHandle, &gEfiCallerIdGuid,\r
692 VgpuDev);\r
693 }\r
694\r
c5f235bb
LE
695CloseExitBoot:\r
696 if (VirtIoBoundJustNow) {\r
697 gBS->CloseEvent (VgpuDev->ExitBoot);\r
698 }\r
699\r
700UninitGpu:\r
701 if (VirtIoBoundJustNow) {\r
702 VirtioGpuUninit (VgpuDev);\r
703 }\r
704\r
a2a4fa66
LE
705FreeVgpuDevBusName:\r
706 if (VirtIoBoundJustNow) {\r
707 FreeUnicodeStringTable (VgpuDev->BusName);\r
708 }\r
709\r
710FreeVgpuDev:\r
711 if (VirtIoBoundJustNow) {\r
712 FreePool (VgpuDev);\r
713 }\r
714\r
715CloseVirtIo:\r
716 if (VirtIoBoundJustNow) {\r
717 gBS->CloseProtocol (ControllerHandle, &gVirtioDeviceProtocolGuid,\r
718 This->DriverBindingHandle, ControllerHandle);\r
719 }\r
720\r
721 return Status;\r
722}\r
723\r
724STATIC\r
725EFI_STATUS\r
726EFIAPI\r
727VirtioGpuDriverBindingStop (\r
728 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
729 IN EFI_HANDLE ControllerHandle,\r
730 IN UINTN NumberOfChildren,\r
731 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL\r
732 )\r
733{\r
734 EFI_STATUS Status;\r
735 VGPU_DEV *VgpuDev;\r
736\r
737 //\r
738 // Look up the VGPU_DEV "protocol interface" on ControllerHandle.\r
739 //\r
740 Status = gBS->OpenProtocol (ControllerHandle, &gEfiCallerIdGuid,\r
741 (VOID **)&VgpuDev, This->DriverBindingHandle,\r
742 ControllerHandle, EFI_OPEN_PROTOCOL_GET_PROTOCOL);\r
743 if (EFI_ERROR (Status)) {\r
744 return Status;\r
745 }\r
746 //\r
747 // Sanity check: if we found gEfiCallerIdGuid on ControllerHandle, then we\r
748 // keep its Virtio Device Protocol interface open BY_DRIVER.\r
749 //\r
750 ASSERT_EFI_ERROR (EfiTestManagedDevice (ControllerHandle,\r
751 This->DriverBindingHandle, &gVirtioDeviceProtocolGuid));\r
752\r
753 switch (NumberOfChildren) {\r
754 case 0:\r
755 //\r
756 // The caller wants us to unbind the VirtIo controller.\r
757 //\r
758 if (VgpuDev->Child != NULL) {\r
759 //\r
760 // We still have the GOP child.\r
761 //\r
762 Status = EFI_DEVICE_ERROR;\r
763 break;\r
764 }\r
765\r
766 DEBUG ((EFI_D_INFO, "%a: unbinding GOP-less VirtIo=%p\n", __FUNCTION__,\r
767 (VOID *)VgpuDev->VirtIo));\r
768\r
769 Status = gBS->UninstallProtocolInterface (ControllerHandle,\r
770 &gEfiCallerIdGuid, VgpuDev);\r
771 ASSERT_EFI_ERROR (Status);\r
772\r
c5f235bb
LE
773 Status = gBS->CloseEvent (VgpuDev->ExitBoot);\r
774 ASSERT_EFI_ERROR (Status);\r
775\r
776 VirtioGpuUninit (VgpuDev);\r
a2a4fa66
LE
777 FreeUnicodeStringTable (VgpuDev->BusName);\r
778 FreePool (VgpuDev);\r
779\r
780 Status = gBS->CloseProtocol (ControllerHandle, &gVirtioDeviceProtocolGuid,\r
781 This->DriverBindingHandle, ControllerHandle);\r
782 ASSERT_EFI_ERROR (Status);\r
783 break;\r
784\r
785 case 1:\r
786 //\r
787 // The caller wants us to destroy our child GOP controller.\r
788 //\r
789 if (VgpuDev->Child == NULL ||\r
790 ChildHandleBuffer[0] != VgpuDev->Child->GopHandle) {\r
791 //\r
792 // We have no child controller at the moment, or it differs from the one\r
793 // the caller wants us to destroy. I.e., we don't own the child\r
794 // controller passed in.\r
795 //\r
796 Status = EFI_DEVICE_ERROR;\r
797 break;\r
798 }\r
799 //\r
800 // Sanity check: our GOP child controller keeps the VGPU_DEV controller's\r
801 // Virtio Device Protocol interface open BY_CHILD_CONTROLLER.\r
802 //\r
803 ASSERT_EFI_ERROR (EfiTestChildHandle (ControllerHandle,\r
804 VgpuDev->Child->GopHandle,\r
805 &gVirtioDeviceProtocolGuid));\r
806\r
807 DEBUG ((EFI_D_INFO, "%a: destroying GOP under VirtIo=%p\n", __FUNCTION__,\r
808 (VOID *)VgpuDev->VirtIo));\r
809 UninitVgpuGop (VgpuDev, ControllerHandle, This->DriverBindingHandle);\r
810 break;\r
811\r
812 default:\r
813 //\r
814 // Impossible, we never produced more than one child.\r
815 //\r
816 Status = EFI_DEVICE_ERROR;\r
817 break;\r
818 }\r
819 return Status;\r
820}\r
821\r
822STATIC EFI_DRIVER_BINDING_PROTOCOL mDriverBinding = {\r
823 VirtioGpuDriverBindingSupported,\r
824 VirtioGpuDriverBindingStart,\r
825 VirtioGpuDriverBindingStop,\r
826 0x10, // Version\r
827 NULL, // ImageHandle, overwritten in entry point\r
828 NULL // DriverBindingHandle, ditto\r
829};\r
830\r
831//\r
832// Entry point of the driver.\r
833//\r
834EFI_STATUS\r
835EFIAPI\r
836VirtioGpuEntryPoint (\r
837 IN EFI_HANDLE ImageHandle,\r
838 IN EFI_SYSTEM_TABLE *SystemTable\r
839 )\r
840{\r
841 return EfiLibInstallDriverBindingComponentName2 (ImageHandle, SystemTable,\r
842 &mDriverBinding, ImageHandle, NULL /* ComponentName */,\r
843 &mComponentName2);\r
844}\r